counterparts 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (195) hide show
  1. package/CONSTITUTION.md +68 -0
  2. package/LICENSE +21 -0
  3. package/README.md +348 -0
  4. package/docs/ELI5.md +84 -0
  5. package/docs/QUICKSTART.md +1258 -0
  6. package/docs/module-map.md +81 -0
  7. package/package.json +63 -0
  8. package/src/adapters/claude-code/CONTRACT.md +606 -0
  9. package/src/adapters/claude-code/INTERFACE-GAPS.md +655 -0
  10. package/src/adapters/claude-code/NOTES.md +1055 -0
  11. package/src/adapters/claude-code/bin/hook.ts +820 -0
  12. package/src/adapters/claude-code/bin/runner.ts +522 -0
  13. package/src/adapters/claude-code/config.ts +778 -0
  14. package/src/adapters/claude-code/credentials.ts +199 -0
  15. package/src/adapters/claude-code/doctor.ts +2557 -0
  16. package/src/adapters/claude-code/embed-client.ts +617 -0
  17. package/src/adapters/claude-code/hooks.ts +2240 -0
  18. package/src/adapters/claude-code/index.ts +224 -0
  19. package/src/adapters/claude-code/interpret-client.ts +395 -0
  20. package/src/adapters/claude-code/page-writer.ts +462 -0
  21. package/src/adapters/claude-code/primacy.ts +162 -0
  22. package/src/adapters/claude-code/spawn.ts +241 -0
  23. package/src/adapters/claude-code/standdown.ts +536 -0
  24. package/src/adapters/claude-code/transcript.ts +661 -0
  25. package/src/adapters/claude-code/vectors.ts +415 -0
  26. package/src/adapters/cli/CONTRACT.md +255 -0
  27. package/src/adapters/cli/INTERFACE-GAPS.md +350 -0
  28. package/src/adapters/cli/NOTES.md +936 -0
  29. package/src/adapters/cli/bin/counterparts.ts +74 -0
  30. package/src/adapters/cli/commands.ts +6095 -0
  31. package/src/adapters/cli/export.ts +766 -0
  32. package/src/adapters/cli/index.ts +103 -0
  33. package/src/adapters/cli/install.ts +670 -0
  34. package/src/adapters/cli/removal.ts +1186 -0
  35. package/src/adapters/cli/repair-dates.ts +659 -0
  36. package/src/adapters/cli/self-page.ts +229 -0
  37. package/src/adapters/cli/snapshot.ts +279 -0
  38. package/src/adapters/cli/start-fresh.ts +1228 -0
  39. package/src/adapters/config-path.ts +342 -0
  40. package/src/adapters/dashboard/CONTRACT.md +67 -0
  41. package/src/adapters/dashboard/INTERFACE-GAPS.md +139 -0
  42. package/src/adapters/dashboard/NOTES.md +474 -0
  43. package/src/adapters/dashboard/activity.ts +121 -0
  44. package/src/adapters/dashboard/ansi.ts +82 -0
  45. package/src/adapters/dashboard/bin/dashboard.ts +479 -0
  46. package/src/adapters/dashboard/browse.ts +298 -0
  47. package/src/adapters/dashboard/identity.ts +182 -0
  48. package/src/adapters/dashboard/index.ts +183 -0
  49. package/src/adapters/dashboard/layout.ts +149 -0
  50. package/src/adapters/dashboard/registries.ts +291 -0
  51. package/src/adapters/dashboard/resolve.ts +206 -0
  52. package/src/adapters/dashboard/source.ts +48 -0
  53. package/src/adapters/dashboard/status.ts +327 -0
  54. package/src/adapters/dashboard/stories.ts +194 -0
  55. package/src/adapters/dashboard/web/app.html +1731 -0
  56. package/src/adapters/dashboard/web/brain.html +463 -0
  57. package/src/adapters/dashboard/web/fired.ts +31 -0
  58. package/src/adapters/dashboard/web/flow.ts +457 -0
  59. package/src/adapters/dashboard/web/narrate.ts +1042 -0
  60. package/src/adapters/dashboard/web/reveal.ts +135 -0
  61. package/src/adapters/dashboard/web/server.ts +287 -0
  62. package/src/adapters/dashboard/web/views.ts +1744 -0
  63. package/src/adapters/expansions.ts +569 -0
  64. package/src/adapters/fired.ts +1580 -0
  65. package/src/adapters/mcp/CONTRACT.md +240 -0
  66. package/src/adapters/mcp/INTERFACE-GAPS.md +271 -0
  67. package/src/adapters/mcp/NOTES.md +205 -0
  68. package/src/adapters/mcp/bin/serve.ts +349 -0
  69. package/src/adapters/mcp/deliberate.ts +619 -0
  70. package/src/adapters/mcp/index.ts +101 -0
  71. package/src/adapters/mcp/protocol.ts +170 -0
  72. package/src/adapters/mcp/server.ts +1740 -0
  73. package/src/adapters/mcp/stdio.ts +56 -0
  74. package/src/adapters/mcp/tools.ts +779 -0
  75. package/src/adapters/scopes.ts +657 -0
  76. package/src/adapters/sessions.ts +402 -0
  77. package/src/adapters/snapshots.ts +1069 -0
  78. package/src/adapters/stance-env.ts +137 -0
  79. package/src/core/associate/CONTRACT.md +118 -0
  80. package/src/core/associate/INTERFACE-GAPS.md +173 -0
  81. package/src/core/associate/NOTES.md +194 -0
  82. package/src/core/associate/buffer.ts +66 -0
  83. package/src/core/associate/edges.ts +188 -0
  84. package/src/core/associate/index.ts +570 -0
  85. package/src/core/associate/pending.ts +383 -0
  86. package/src/core/associate/spread.ts +149 -0
  87. package/src/core/associate/tunables.ts +123 -0
  88. package/src/core/bridge.ts +272 -0
  89. package/src/core/briefing.ts +67 -0
  90. package/src/core/counterpart.ts +3855 -0
  91. package/src/core/encode/CONTRACT.md +151 -0
  92. package/src/core/encode/INTERFACE-GAPS.md +135 -0
  93. package/src/core/encode/NOTES.md +146 -0
  94. package/src/core/encode/aliases.ts +71 -0
  95. package/src/core/encode/battery.ts +284 -0
  96. package/src/core/encode/chunk.ts +265 -0
  97. package/src/core/encode/emotion.ts +119 -0
  98. package/src/core/encode/floor.ts +76 -0
  99. package/src/core/encode/index.ts +100 -0
  100. package/src/core/encode/precision.ts +162 -0
  101. package/src/core/encode/preselect.ts +269 -0
  102. package/src/core/encode/salience.ts +108 -0
  103. package/src/core/encode/secrets.ts +265 -0
  104. package/src/core/encode/tunables.ts +99 -0
  105. package/src/core/encode/types.ts +286 -0
  106. package/src/core/encode/words.ts +75 -0
  107. package/src/core/handoff/CONTRACT.md +199 -0
  108. package/src/core/handoff/INTERFACE-GAPS.md +166 -0
  109. package/src/core/handoff/NOTES.md +206 -0
  110. package/src/core/handoff/index.ts +957 -0
  111. package/src/core/mint.ts +279 -0
  112. package/src/core/observer.ts +28 -0
  113. package/src/core/physics/CONTRACT.md +341 -0
  114. package/src/core/physics/NOTES.md +155 -0
  115. package/src/core/physics/clock.ts +51 -0
  116. package/src/core/physics/index.ts +1054 -0
  117. package/src/core/prospective/CONTRACT.md +137 -0
  118. package/src/core/prospective/INTERFACE-GAPS.md +112 -0
  119. package/src/core/prospective/NOTES.md +160 -0
  120. package/src/core/prospective/derive.ts +188 -0
  121. package/src/core/prospective/index.ts +1030 -0
  122. package/src/core/prospective/tunables.ts +72 -0
  123. package/src/core/prospective/windows.ts +187 -0
  124. package/src/core/recall/CONTRACT.md +326 -0
  125. package/src/core/recall/INTERFACE-GAPS.md +163 -0
  126. package/src/core/recall/NOTES.md +472 -0
  127. package/src/core/recall/activate.ts +521 -0
  128. package/src/core/recall/cues.ts +275 -0
  129. package/src/core/recall/gate.ts +441 -0
  130. package/src/core/recall/index.ts +692 -0
  131. package/src/core/recall/probe.ts +239 -0
  132. package/src/core/recall/reference.ts +201 -0
  133. package/src/core/recall/render.ts +285 -0
  134. package/src/core/recall/session.ts +403 -0
  135. package/src/core/recall/tunables.ts +329 -0
  136. package/src/core/remember/CONTRACT.md +293 -0
  137. package/src/core/remember/INTERFACE-GAPS.md +189 -0
  138. package/src/core/remember/NOTES.md +373 -0
  139. package/src/core/remember/fallback.ts +452 -0
  140. package/src/core/remember/index.ts +103 -0
  141. package/src/core/remember/owner-strike-seam.ts +479 -0
  142. package/src/core/remember/proposals.ts +609 -0
  143. package/src/core/remember/spans.ts +1310 -0
  144. package/src/core/remember/tunables.ts +111 -0
  145. package/src/core/remember/updates.ts +198 -0
  146. package/src/core/retrieval.ts +96 -0
  147. package/src/core/revision.ts +441 -0
  148. package/src/core/schemas/CONTRACT.md +190 -0
  149. package/src/core/schemas/INTERFACE-GAPS.md +172 -0
  150. package/src/core/schemas/NOTES.md +260 -0
  151. package/src/core/schemas/aliases.ts +160 -0
  152. package/src/core/schemas/index.ts +1459 -0
  153. package/src/core/schemas/tunables.ts +44 -0
  154. package/src/core/schemas/types.ts +387 -0
  155. package/src/core/self/CONTRACT.md +459 -0
  156. package/src/core/self/INTERFACE-GAPS.md +284 -0
  157. package/src/core/self/NOTES.md +1057 -0
  158. package/src/core/self/briefing.ts +980 -0
  159. package/src/core/self/episodes.ts +483 -0
  160. package/src/core/self/freeze.ts +103 -0
  161. package/src/core/self/identity.ts +604 -0
  162. package/src/core/self/index.ts +2130 -0
  163. package/src/core/self/journal-file.ts +760 -0
  164. package/src/core/self/page.ts +404 -0
  165. package/src/core/self/tunables.ts +244 -0
  166. package/src/core/self/writer.ts +714 -0
  167. package/src/core/sleep/CONTRACT.md +298 -0
  168. package/src/core/sleep/INTERFACE-GAPS.md +262 -0
  169. package/src/core/sleep/NOTES.md +770 -0
  170. package/src/core/sleep/briefing.ts +86 -0
  171. package/src/core/sleep/consolidate.ts +241 -0
  172. package/src/core/sleep/cycle.ts +598 -0
  173. package/src/core/sleep/decay.ts +237 -0
  174. package/src/core/sleep/dedup.ts +373 -0
  175. package/src/core/sleep/index.ts +112 -0
  176. package/src/core/sleep/log.ts +116 -0
  177. package/src/core/sleep/markers.ts +164 -0
  178. package/src/core/sleep/prune.ts +170 -0
  179. package/src/core/sleep/strength-cache.ts +176 -0
  180. package/src/core/sleep/tunables.ts +135 -0
  181. package/src/core/sleep/types.ts +542 -0
  182. package/src/core/store/CONTRACT.md +446 -0
  183. package/src/core/store/NOTES.md +1068 -0
  184. package/src/core/store/cache.ts +709 -0
  185. package/src/core/store/db.ts +275 -0
  186. package/src/core/store/errors.ts +134 -0
  187. package/src/core/store/index.ts +2450 -0
  188. package/src/core/store/operational.ts +610 -0
  189. package/src/core/store/owner-op-seam.ts +428 -0
  190. package/src/core/store/paths.ts +494 -0
  191. package/src/core/store/prose.ts +165 -0
  192. package/src/core/store/render.ts +87 -0
  193. package/src/core/store/walk-seam.ts +78 -0
  194. package/src/core/types.ts +68 -0
  195. package/tools/install-loop/run.sh +1063 -0
@@ -0,0 +1,241 @@
1
+ /**
2
+ * Detached execution — the "heavy work runs somewhere else" half of scar E4.
3
+ *
4
+ * A hook returns in microseconds. Sleep, the crash-fallback sweep and every
5
+ * model call run in a child this file starts and then forgets about. Four
6
+ * properties, each of them an incident someone lived through:
7
+ *
8
+ * **E4 — a watchdog, validated.** The child gets a timeout, and the timeout is
9
+ * checked against `remember/`'s claim-staleness window BEFORE the spawn. A
10
+ * watchdog that fires after a claim looks stale means two runs can hold the
11
+ * same spans; `remember.validateWatchdog` is the check, and SEAMS queued item
12
+ * 10 asks for it to be PROVEN here rather than asserted in a comment.
13
+ *
14
+ * **E4's widening — a worker that cannot start ESCALATES.** v1's runner
15
+ * starved for two days on a credential it expected to inherit from whatever
16
+ * shell launched the session, and the backlog drained only when a human
17
+ * noticed. So: a plan that cannot run says why, by name, and the same failure
18
+ * repeated crosses an escalation threshold instead of writing the same line
19
+ * forever. The backlog depth rides along as a reported number.
20
+ *
21
+ * **§2.13 — the spawner pins the child's environment LAST.** `dataDir` is
22
+ * written onto the child env after every inherited value, so nothing a caller
23
+ * exported can redirect a run into the wrong store. v1's replay harness
24
+ * silently honored a pre-set data-directory variable, which is the observer
25
+ * scar escalated from training a store to destroying its graph.
26
+ *
27
+ * **§2.18 — the credential comes from the environment we PIN, not the shell we
28
+ * inherited.** It travels on the child's environment like everything else here.
29
+ * It is NOT a precondition of the spawn, and that changed on 2026-09-11 (I32):
30
+ * for a week this file refused `NO_CREDENTIAL` at every boundary, and because
31
+ * the worker never started, the lived-day clock never advanced, the sleep
32
+ * cycle never ran, the Hebbian buffer never flushed and the day's ask cap —
33
+ * keyed on that frozen clock — stayed spent. A refusal that stops FIVE jobs to
34
+ * protect ONE of them is not a safety property; it is a single point of
35
+ * failure with a name. The worker now starts whenever it has a data dir and a
36
+ * sane watchdog, and the one step that needs the key (`runner.ts`'s sweep)
37
+ * degrades and SAYS SO on a durable row. Nothing about that is a licence to
38
+ * run blind: the refusals that remain are the ones where starting would be
39
+ * wrong, not merely unproductive.
40
+ *
41
+ * `planSpawn` is pure and returns a checkable plan; `spawnDetached` is the six
42
+ * lines that actually start a process. The split is deliberate: everything worth
43
+ * testing is in the pure half.
44
+ */
45
+ import { spawn } from "node:child_process";
46
+
47
+ import { TUNABLES as REMEMBER, validateWatchdog } from "../../core/remember/index.js";
48
+
49
+ import { CONFIG_ENV as CONFIG_PATH_ENV } from "../config-path.js";
50
+
51
+ import { TUNABLES } from "./config.js";
52
+ import type { AdapterConfig } from "./config.js";
53
+
54
+ export const DATA_DIR_ENV = "COUNTERPARTS_DATA_DIR";
55
+ /** The child is told its own watchdog, so the worker can arm one internally too. */
56
+ export const WATCHDOG_ENV = "COUNTERPARTS_WATCHDOG_MS";
57
+ /**
58
+ * The session and scope the child is working on behalf of — the same two names
59
+ * the MCP entry point already reads (`mcp/bin/serve.ts` ENV), because a second
60
+ * name for "which session is this" is a second answer waiting to disagree.
61
+ *
62
+ * They exist because the worker's lagged semantic cue is PER-SESSION state: a
63
+ * run that does not know whose turn it just followed can compute a vector for
64
+ * nobody. The TEXT never travels this way — a process environment is visible in
65
+ * `ps` — only the two identifiers, and the worker reads the words themselves out
66
+ * of the span buffer it already owns.
67
+ */
68
+ export const SESSION_ENV = "COUNTERPARTS_SESSION";
69
+ export const SCOPE_ENV = "COUNTERPARTS_SCOPE";
70
+ /**
71
+ * The configuration file the PARENT read, pinned onto the child for exactly the
72
+ * reason the data dir is (§2.13): two processes resolving one value twice is two
73
+ * answers waiting to disagree. A hook driven by `--config <path>` that spawned a
74
+ * worker which then resolved the default file would sweep and sleep against the
75
+ * store the operator did not name.
76
+ *
77
+ * It is the SAME name the entry points accept as an environment variable —
78
+ * imported from `adapters/config-path.ts` rather than retyped, because a second
79
+ * spelling of a variable that redirects a store is a bug nobody would see.
80
+ */
81
+ export { CONFIG_ENV } from "../config-path.js";
82
+
83
+ export type SpawnRefusal =
84
+ | "NO_DATA_DIR"
85
+ | "WATCHDOG_NOT_FINITE"
86
+ | "WATCHDOG_EXCEEDS_STALENESS"
87
+ | "OBSERVER";
88
+
89
+ export interface SpawnPlan {
90
+ readonly ok: boolean;
91
+ readonly reason: SpawnRefusal | "ready";
92
+ readonly command: string;
93
+ readonly args: readonly string[];
94
+ /** The child's complete environment. `COUNTERPARTS_DATA_DIR` is written LAST. */
95
+ readonly env: Readonly<Record<string, string>>;
96
+ readonly timeoutMs: number;
97
+ /** True when this refusal is the kind a human must be told about (E4). */
98
+ readonly escalate: boolean;
99
+ }
100
+
101
+ export interface PlanInput {
102
+ config: AdapterConfig;
103
+ /** The interpreter for the runtime (`~/.bun/bin/bun`, `node`, ...). */
104
+ command: string;
105
+ /** The worker script and its arguments. */
106
+ args: readonly string[];
107
+ /** The environment to inherit from. Defaults to this process's. */
108
+ baseEnv?: Readonly<Record<string, string | undefined>>;
109
+ /** How many times this exact refusal has been seen already (E4's counter). */
110
+ priorFailures?: number;
111
+ /** The session this run follows, pinned onto the child (see `SESSION_ENV`). */
112
+ session?: string;
113
+ /** That session's project scope — where its spans live. */
114
+ scope?: string;
115
+ /** The configuration file the parent read, pinned onto the child (`CONFIG_ENV`). */
116
+ configPath?: string;
117
+ }
118
+
119
+ /**
120
+ * Build the plan and check every precondition. Pure: it starts nothing, and it
121
+ * is the whole of what a test needs to see.
122
+ */
123
+ export function planSpawn(input: PlanInput): SpawnPlan {
124
+ const { config } = input;
125
+ const base = input.baseEnv ?? process.env;
126
+ const timeoutMs = config.watchdogMs ?? TUNABLES.WATCHDOG_MS;
127
+ const priorFailures = input.priorFailures ?? 0;
128
+ const escalate = priorFailures + 1 >= TUNABLES.ESCALATE_AFTER;
129
+
130
+ // The environment, assembled in the ONE order that matters: everything
131
+ // inherited first, then the values this package owns. A caller's exported
132
+ // COUNTERPARTS_DATA_DIR cannot survive this line (scar §2.13).
133
+ const env: Record<string, string> = {};
134
+ for (const [key, value] of Object.entries(base)) {
135
+ if (value !== undefined) env[key] = value;
136
+ }
137
+ const refuse = (reason: SpawnRefusal): SpawnPlan => ({
138
+ ok: false,
139
+ reason,
140
+ command: input.command,
141
+ args: [...input.args],
142
+ env,
143
+ timeoutMs,
144
+ escalate,
145
+ });
146
+
147
+ if (config.observer === true) {
148
+ // An instrument spawns no worker (§15 G3): a cycle advances the clock,
149
+ // decays the store and rewrites the briefing — the instrument mutating what
150
+ // it measures. This refusal never escalates; it is the correct outcome.
151
+ return { ...refuse("OBSERVER"), escalate: false };
152
+ }
153
+ if (config.dataDir === undefined || config.dataDir.trim().length === 0) {
154
+ return refuse("NO_DATA_DIR");
155
+ }
156
+ const watchdog = validateWatchdog(timeoutMs, REMEMBER.STALE_CLAIM_MS);
157
+ if (!watchdog.ok) {
158
+ return refuse(
159
+ watchdog.reason === "TIMEOUT_NOT_FINITE"
160
+ ? "WATCHDOG_NOT_FINITE"
161
+ : "WATCHDOG_EXCEEDS_STALENESS",
162
+ );
163
+ }
164
+ // NO CREDENTIAL CHECK HERE, and the absence is the point. The key rides along
165
+ // on `env` like every other inherited value; whether it is there is the
166
+ // WORKER's question, one step at a time, because four of its five jobs do not
167
+ // need it (I32, and the header above).
168
+ env[DATA_DIR_ENV] = config.dataDir;
169
+ env[WATCHDOG_ENV] = String(timeoutMs);
170
+ // Pinned LAST for the same reason the data dir is: a caller's exported
171
+ // COUNTERPARTS_SESSION (an MCP server's, say) must not decide whose cue this
172
+ // worker computes.
173
+ if (input.session !== undefined && input.session.length > 0) env[SESSION_ENV] = input.session;
174
+ if (input.scope !== undefined && input.scope.length > 0) env[SCOPE_ENV] = input.scope;
175
+ // Last of the three, and for the same reason as the first: whatever the caller
176
+ // exported, the child reads the file its parent read.
177
+ if (input.configPath !== undefined && input.configPath.length > 0) {
178
+ env[CONFIG_PATH_ENV] = input.configPath;
179
+ }
180
+ return {
181
+ ok: true,
182
+ reason: "ready",
183
+ command: input.command,
184
+ args: [...input.args],
185
+ env,
186
+ timeoutMs,
187
+ escalate: false,
188
+ };
189
+ }
190
+
191
+ export interface SpawnOutcome {
192
+ readonly started: boolean;
193
+ readonly reason: SpawnPlan["reason"] | "SPAWN_FAILED";
194
+ readonly pid: number | null;
195
+ readonly escalate: boolean;
196
+ readonly code: string | null;
197
+ }
198
+
199
+ /** Injected so a test can prove the whole path without starting a process. */
200
+ export type Spawner = (plan: SpawnPlan) => { pid?: number | undefined; unref?: () => void };
201
+
202
+ /**
203
+ * Start the child and forget it. Detached, no stdio, unref'd — the parent hook
204
+ * returns immediately and the host never waits on the work.
205
+ */
206
+ export function spawnDetached(
207
+ plan: SpawnPlan,
208
+ opts: { spawner?: Spawner; onEvent?: (name: string, data: Record<string, string | number | boolean | null>) => void } = {},
209
+ ): SpawnOutcome {
210
+ const emit = opts.onEvent ?? ((): void => {});
211
+ if (!plan.ok) {
212
+ emit(plan.escalate ? "spawn.escalated" : "spawn.refused", {
213
+ reason: plan.reason,
214
+ timeoutMs: plan.timeoutMs,
215
+ escalate: plan.escalate,
216
+ });
217
+ return { started: false, reason: plan.reason, pid: null, escalate: plan.escalate, code: null };
218
+ }
219
+ const run =
220
+ opts.spawner ??
221
+ ((p: SpawnPlan) =>
222
+ spawn(p.command, [...p.args], {
223
+ detached: true,
224
+ stdio: "ignore",
225
+ env: { ...p.env },
226
+ timeout: p.timeoutMs,
227
+ }));
228
+ try {
229
+ const child = run(plan);
230
+ child.unref?.();
231
+ emit("spawn.started", { pid: child.pid ?? null, timeoutMs: plan.timeoutMs });
232
+ return { started: true, reason: "ready", pid: child.pid ?? null, escalate: false, code: null };
233
+ } catch (err) {
234
+ const code =
235
+ err !== null && typeof err === "object" && typeof (err as { code?: unknown }).code === "string"
236
+ ? ((err as { code: string }).code)
237
+ : "UNKNOWN";
238
+ emit("spawn.failed", { code, timeoutMs: plan.timeoutMs });
239
+ return { started: false, reason: "SPAWN_FAILED", pid: null, escalate: true, code };
240
+ }
241
+ }