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,342 @@
1
+ /**
2
+ * WHERE EVERY ENTRY POINT LOOKS FOR THE HOST CONFIGURATION. One rule, one file.
3
+ *
4
+ * Until 2026-09-05 there were three answers to "which `claude-code.json`?".
5
+ * `claude-code/bin/hook.ts`, `claude-code/bin/runner.ts` and `mcp/bin/serve.ts`
6
+ * each hard-coded `join(homedir(), ".counterparts", "claude-code.json")` and
7
+ * took no flag; the console looked beside the store and then in the home. That
8
+ * was recorded as LAUNCH-STATUS G1, and it cost two concrete things:
9
+ *
10
+ * - the clean-room install loop could drive the console and the MCP server at
11
+ * a scratch install but could not drive THE HOOK anywhere except the one
12
+ * path its own `$HOME` produced, so "the hook honours a config" was a step
13
+ * nobody could write;
14
+ * - an agent or a reviewer on a machine that already has an install could not
15
+ * run those three entry points at all without redirecting `HOME` — and on
16
+ * 2026-09-04 a critic who could not do that ran `counterparts-mcp` against
17
+ * the owner's live configuration (its embedder knob, its credentials file)
18
+ * while pointing at a scratch store.
19
+ *
20
+ * THE RULE, and it is the same sentence for the console, the hook, the worker
21
+ * and the MCP server:
22
+ *
23
+ * 1. `--config <absolute path>`, when the command line carries one;
24
+ * 2. else `COUNTERPARTS_CONFIG`, the flag's equivalent for a host that
25
+ * launches from a STATIC configuration and has no command line to write
26
+ * into — which is exactly how Claude Code registers an MCP server
27
+ * (measured 2026-09-04, `mcp/bin/serve.ts`);
28
+ * 3. else the documented default, unchanged: `~/.counterparts/claude-code.json`
29
+ * for the hook, the worker and the server; and for the console, whose
30
+ * `--dir` may name any store, `<dir>/../claude-code.json` first and that
31
+ * same home path second (`cli/commands.ts#hostCeiling`).
32
+ *
33
+ * Two properties this file exists to keep:
34
+ *
35
+ * **The default path does not move.** `hook.ts` runs on the owner's live host
36
+ * at the next hook event; a no-flag, no-env run must resolve exactly what it
37
+ * resolved before this file existed. `defaultConfigPath()` is that path and
38
+ * nothing else consults it.
39
+ *
40
+ * **A NAMED configuration that cannot be honoured REFUSES; it never falls
41
+ * back.** An operator who names one has said which memory they mean. Falling
42
+ * back to the default on a bad path is how a scratch run writes into a live
43
+ * store — the failure direction this whole file is a reaction to. Three shapes
44
+ * refuse, at every entry point that READS a configuration: a flag with nothing
45
+ * after it, a relative path (`configRefusal`), and — because the first review
46
+ * of this rule found the hole — an absolute path to a file that is not there
47
+ * or does not parse (`namedConfigRefusal`). That third one is the dangerous
48
+ * one: a mistyped `--config /scratch/typo.json` used to be honoured silently,
49
+ * read as an absent config, resolved to observer, and then fall through to
50
+ * `dataDir()` — which on a real machine is the owner's live store.
51
+ *
52
+ * **An absent DEFAULT stays ordinary.** A fresh machine has no
53
+ * `~/.counterparts/claude-code.json` and must still start; that path resolves
54
+ * to the observer default as it always did. The distinction is `source`, and
55
+ * it is why the check is a separate function rather than a line inside
56
+ * `configRefusal`: `install --config <a path>` goes through the same resolver
57
+ * and the file does not exist yet — writing it is the whole command.
58
+ *
59
+ * It sits beside `sessions.ts` for the same reason that file does: two adapters
60
+ * need it, and neither adapter may import the other (`mcp/INTERFACE-GAPS.md`
61
+ * §7). A sibling both may import is the shape that rule allows.
62
+ */
63
+ import { readFileSync } from "node:fs";
64
+ import { homedir } from "node:os";
65
+ import { isAbsolute, join, resolve } from "node:path";
66
+
67
+ import {
68
+ DEFAULT_DATA_DIR_NAME,
69
+ REQUIRE_EXPLICIT_DIR_ENV,
70
+ explicitDirMalformedRefusal,
71
+ explicitDirSetting,
72
+ } from "../core/store/index.js";
73
+
74
+ /** The flag, spelled once. */
75
+ export const CONFIG_FLAG = "--config";
76
+
77
+ /**
78
+ * The environment variable, spelled once. It is the FLAG'S EQUIVALENT and
79
+ * nothing more: same meaning, same precedence order, lower priority. It exists
80
+ * because `claude mcp add` writes a static command + env into the host's own
81
+ * configuration, so `-e COUNTERPARTS_CONFIG=…` is the only way to tell a server
82
+ * launched that way which file to read.
83
+ */
84
+ export const CONFIG_ENV = "COUNTERPARTS_CONFIG";
85
+
86
+ /** The file's name under the home directory. `install` writes exactly this. */
87
+ export const CONFIG_FILE_NAME = "claude-code.json";
88
+
89
+ /**
90
+ * The RING event a hook adapter leaves naming the file it read — ring-only, like
91
+ * `adapter.credentials.file`, because a new DURABLE event name is a core change
92
+ * (`core/counterpart.ts#AdapterDurableEventName`) and this one is not worth one:
93
+ * the durable trace is the session registry record's `config` field, written
94
+ * under the store the config actually named.
95
+ */
96
+ export const CONFIG_FILE_EVENT = "adapter.config.file";
97
+
98
+ export type ConfigSource = typeof CONFIG_FLAG | typeof CONFIG_ENV | "default";
99
+
100
+ export interface ConfigChoice {
101
+ /** The absolute path to read. Meaningless when `refusal` is set. */
102
+ readonly path: string;
103
+ /** Which of the three answered. Printed or recorded by every entry point. */
104
+ readonly source: ConfigSource;
105
+ /** Non-null when the caller NAMED a configuration this rule cannot honour. */
106
+ readonly refusal: string | null;
107
+ }
108
+
109
+ /**
110
+ * `~/.counterparts/claude-code.json` — the one default, unchanged since the
111
+ * hooks first read it, and the only place a home directory is resolved for a
112
+ * configuration path.
113
+ */
114
+ export function defaultConfigPath(home: string = homedir()): string {
115
+ return join(home, DEFAULT_DATA_DIR_NAME, CONFIG_FILE_NAME);
116
+ }
117
+
118
+ /**
119
+ * The raw `--config` value on a command line, or undefined. Accepts both
120
+ * `--config <path>` and `--config=<path>`; a flag with nothing usable after it
121
+ * returns the empty string, which the resolver turns into a refusal rather than
122
+ * into silence (the same lesson as `cli/commands.ts#unknownFlag`: a valued flag
123
+ * that got no value is an error, not an absence).
124
+ *
125
+ * Hand-rolled rather than `parseArgs`, because three of the four callers parse
126
+ * their own arguments differently and this must mean the same thing in all of
127
+ * them.
128
+ */
129
+ export function configFlag(argv: readonly string[]): string | undefined {
130
+ for (let i = 0; i < argv.length; i += 1) {
131
+ const token = argv[i] ?? "";
132
+ if (token === CONFIG_FLAG) {
133
+ const next = argv[i + 1];
134
+ return next === undefined || next.startsWith("--") ? "" : next;
135
+ }
136
+ if (token.startsWith(`${CONFIG_FLAG}=`)) return token.slice(CONFIG_FLAG.length + 1);
137
+ }
138
+ return undefined;
139
+ }
140
+
141
+ /** Why this named path cannot be used, or null. Absolute is the whole rule. */
142
+ export function configRefusal(value: string, source: ConfigSource): string | null {
143
+ const named = source === CONFIG_FLAG ? `${CONFIG_FLAG} <path>` : CONFIG_ENV;
144
+ if (value.length === 0) {
145
+ return `refused: ${named} was given nothing. It takes an ABSOLUTE path to a configuration file.`;
146
+ }
147
+ if (!isAbsolute(value)) {
148
+ return (
149
+ `refused: ${named} takes an ABSOLUTE path, and '${value}' is relative. ` +
150
+ "The processes that read this file are launched by a host from a working directory " +
151
+ "nobody chose, so a relative path names a different file in every one of them."
152
+ );
153
+ }
154
+ return null;
155
+ }
156
+
157
+ /**
158
+ * The rule itself: flag, else environment, else the default.
159
+ *
160
+ * `argv` is the entry point's own arguments (`process.argv.slice(2)`), `env` its
161
+ * environment, `home` the home directory — all three injected so the whole rule
162
+ * is provable without a process, and so a test never has to reach a real home.
163
+ */
164
+ export function resolveConfigPath(
165
+ argv: readonly string[] = [],
166
+ env: Record<string, string | undefined> = {},
167
+ home: string = homedir(),
168
+ ): ConfigChoice {
169
+ const flag = configFlag(argv);
170
+ if (flag !== undefined) {
171
+ const refusal = configRefusal(flag, CONFIG_FLAG);
172
+ return { path: flag, source: CONFIG_FLAG, refusal };
173
+ }
174
+ const fromEnv = env[CONFIG_ENV];
175
+ // An EMPTY variable is absent, not a refusal: a host that exports the name
176
+ // with no value has said nothing, and `-e COUNTERPARTS_CONFIG=` is a shape a
177
+ // registration can produce by accident. A non-empty one that is relative is a
178
+ // refusal, because somebody meant something by it.
179
+ if (fromEnv !== undefined && fromEnv.trim().length > 0) {
180
+ const value = fromEnv.trim();
181
+ return { path: value, source: CONFIG_ENV, refusal: configRefusal(value, CONFIG_ENV) };
182
+ }
183
+ return { path: defaultConfigPath(home), source: "default", refusal: null };
184
+ }
185
+
186
+ /**
187
+ * Is this choice a configuration somebody NAMED somewhere other than the default?
188
+ *
189
+ * The test is the PATH, not how it was named — the same rule `install` uses to
190
+ * decide whether to print a flag. It matters because `spawn.ts` pins the
191
+ * parent's resolved path onto every worker, default included: without this, a
192
+ * machine with no `~/.counterparts/claude-code.json` (which is ordinary — the
193
+ * hook stands up as an observer) would spawn a worker that saw a NAMED but
194
+ * missing config and stood down, so nothing would ever sweep or sleep there.
195
+ * Found while reviewing the missing-file refusal; the pin is right and the
196
+ * refusal is right, and this is the line that keeps them from meeting.
197
+ */
198
+ export function isNamed(choice: ConfigChoice, home: string = homedir()): boolean {
199
+ return choice.source !== "default" && resolve(choice.path) !== defaultConfigPath(home);
200
+ }
201
+
202
+ /**
203
+ * Why a NAMED configuration was not UNDERSTOOD, or null — the third arm, and the
204
+ * only one that needs the caller's own loader.
205
+ *
206
+ * `loadConfig` reports `unreadable` for a file that parses but whose fields do
207
+ * not typecheck (`"dataDir": 123`), and resolves it to `{ observer: true }` —
208
+ * the right fail direction for a configuration nobody named, and the wrong one
209
+ * for a file the operator pointed at: the store then falls through to the
210
+ * default, opened as an instrument. `reason` is passed in as a string rather
211
+ * than the adapter's own type, so this module still imports no adapter.
212
+ */
213
+ export function namedUnreadableRefusal(
214
+ choice: ConfigChoice,
215
+ reason: string,
216
+ home?: string,
217
+ ): string | null {
218
+ if (!isNamed(choice, home) || reason !== "unreadable") return null;
219
+ const named = choice.source === CONFIG_FLAG ? `${CONFIG_FLAG} <path>` : CONFIG_ENV;
220
+ return (
221
+ `refused: ${named} names ${choice.path}, which could not be understood — a key in it ` +
222
+ "does not hold the type this expects. An unreadable configuration resolves to observer " +
223
+ "and the store falls back to the default one, which is not what you named."
224
+ );
225
+ }
226
+
227
+ /**
228
+ * Why a NAMED configuration cannot be read, or null — the second half of the
229
+ * refusal, for the entry points that READ a config file (`bin/hook.ts`,
230
+ * `bin/runner.ts`, `mcp/bin/serve.ts`).
231
+ *
232
+ * The hole this closes, reproduced by the review of PR #72 on 2026-09-05:
233
+ * `counterparts-hook --config /definitely/not/here/claude-code.json` exited 0,
234
+ * printed a wake, and wrote a store — because a read error is swallowed into
235
+ * `loadConfig(undefined)`, which is the observer default, and the data dir then
236
+ * falls through to `dataDir()`. On a machine with an install that is the live
237
+ * store. A typo in the one flag that says WHICH MEMORY was the one thing nothing
238
+ * would mention (the same sentence the console's `--dirr` refusal already
239
+ * carries).
240
+ *
241
+ * Two things it deliberately does NOT do:
242
+ *
243
+ * - it says nothing about the DEFAULT path. Absent there is ordinary: a fresh
244
+ * machine has no config and the hook must still stand up as an observer.
245
+ * - it is not folded into `configRefusal`, because `counterparts install
246
+ * --config <path>` resolves through the same rule and that file does not
247
+ * exist yet — creating it is the command.
248
+ *
249
+ * The file is read here and read again by the caller's own loader. That is one
250
+ * extra read of a file measured in hundreds of bytes, and it keeps this module
251
+ * free of any adapter's config schema: what it checks is "can this be read, and
252
+ * is it a JSON object", which is the whole of what a caller needs to have been
253
+ * true.
254
+ */
255
+ export function namedConfigRefusal(
256
+ choice: ConfigChoice,
257
+ read: (path: string) => string = (path) => readFileSync(path, "utf8"),
258
+ home?: string,
259
+ ): string | null {
260
+ if (choice.refusal !== null) return choice.refusal;
261
+ if (!isNamed(choice, home)) return null;
262
+ const named = choice.source === CONFIG_FLAG ? `${CONFIG_FLAG} <path>` : CONFIG_ENV;
263
+ let text: string;
264
+ try {
265
+ text = read(choice.path);
266
+ } catch (err) {
267
+ const code =
268
+ err !== null && typeof err === "object" && typeof (err as { code?: unknown }).code === "string"
269
+ ? (err as { code: string }).code
270
+ : "UNREADABLE";
271
+ return (
272
+ `refused: ${named} names ${choice.path}, which could not be read (${code}). ` +
273
+ "A configuration you named is not one this can guess at: the default would be a " +
274
+ "different store, and using it silently is how a scratch run writes into a live one."
275
+ );
276
+ }
277
+ let parsed: unknown;
278
+ try {
279
+ parsed = JSON.parse(text);
280
+ } catch {
281
+ parsed = undefined;
282
+ }
283
+ if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) {
284
+ return (
285
+ `refused: ${named} names ${choice.path}, which is not a JSON object. ` +
286
+ "An unreadable configuration resolves to observer, and an observer that was " +
287
+ "POINTED at a file is a stand-down nobody asked for — so this says so instead."
288
+ );
289
+ }
290
+ return null;
291
+ }
292
+
293
+ /**
294
+ * THE EXPLICIT-DIR GUARD AT THE OTHER DOOR (`store/paths.ts#REQUIRE_EXPLICIT_DIR_ENV`).
295
+ *
296
+ * `COUNTERPARTS_REQUIRE_EXPLICIT_DIR=1` makes `dataDir()` refuse its implicit
297
+ * default. That alone leaves this module's default open, and it is the wider
298
+ * door: a default-sourced `~/.counterparts/claude-code.json` NAMES a store — its
299
+ * `dataDir` field, which `install` always writes — and the credentials beside
300
+ * it, so the hook, the worker and the MCP server reach the live store and the
301
+ * live keys without `dataDir()` ever running; and `install` writes its config
302
+ * and credentials under that same base (`cli/install.ts#installLayout`, which
303
+ * builds the base from `homedir()` itself). The reviewer of PR #72 named this
304
+ * the other way into the live machine, and it is.
305
+ *
306
+ * So the same variable refuses the same shape here: the guard armed AND the
307
+ * configuration resolved to the default. Null when either is false. It is a
308
+ * separate function rather than a clause in `resolveConfigPath`, for the same
309
+ * reason `namedConfigRefusal` is: the console's `rebrief` goes through the
310
+ * resolver too, and its config read is a budget NUMBER for a store already
311
+ * named by `--dir` — refusing it would teach people to unset the guard. The
312
+ * line drawn: the guard refuses an implicit default that LOCATES A STORE or
313
+ * WRITES THE LIVE BASE; a number read from the default config is neither.
314
+ *
315
+ * Callers: the three bins (chained after `namedConfigRefusal`, which already
316
+ * stands them down on a string) and the console's `install`. Read from the
317
+ * injected `env`, like everything else in this file. A value the guard cannot
318
+ * read is refused HERE too, in the same sentence the store uses — but only at
319
+ * the default, because a named configuration never consulted the guard.
320
+ */
321
+ export function implicitConfigRefusal(
322
+ choice: ConfigChoice,
323
+ env: Record<string, string | undefined> = process.env,
324
+ ): string | null {
325
+ if (choice.source !== "default") return null;
326
+ const setting = explicitDirSetting(env);
327
+ if (!setting.armed) {
328
+ return setting.malformed === null ? null : explicitDirMalformedRefusal(setting.malformed);
329
+ }
330
+ return (
331
+ `refused: ${REQUIRE_EXPLICIT_DIR_ENV}=${setting.value} and no configuration was named, so this ` +
332
+ `would have read the default one, ${choice.path} — which on a machine with an install names the ` +
333
+ `live store and its credentials. Name one: ${CONFIG_FLAG} <absolute path>, or ${CONFIG_ENV}.`
334
+ );
335
+ }
336
+
337
+ /** The one line every entry point prints or records. Path first: it is the fact. */
338
+ export function configLine(choice: ConfigChoice): string {
339
+ return choice.source === "default"
340
+ ? `${choice.path} (the default)`
341
+ : `${choice.path} (named by ${choice.source})`;
342
+ }
@@ -0,0 +1,67 @@
1
+ # `dashboard/` — CONTRACT
2
+
3
+ ## 1. Purpose
4
+
5
+ The owner's window: what was remembered, what faded, what changed and why — visually, at a
6
+ glance. The direct instrument of constitution line 16, and the owner's tool for judging
7
+ Counterparts against v1 during the parallel run.
8
+
9
+ ## 2. Brain analog
10
+
11
+ None — this is deliberately outside the analogy (constitution line 12 deviation, named):
12
+ brains are not legible to their owners, and that is one of biology's bugs, not a feature.
13
+ Legibility is the product's answer to the trust question a brain never has to face.
14
+
15
+ ## 3. Keeps
16
+
17
+ - **Render-time id resolution.** [v1] Logs and records carry ids; the dashboard resolves
18
+ them to text at render time — an erased or superseded memory stops resolving immediately,
19
+ where baked-in text would outlive it (v1 DECISIONS, dashboard batch; scar §2.20 kin).
20
+ - **The totality test.** [v1] Every axis the registry knows is displayed or explicitly
21
+ marked absent — a silent missing panel reads as "nothing happening," which is how v1's
22
+ curation starvation hid (scar §2.17; v1's six-axis totality test, generalized).
23
+ - **Narration in the system's own voice.** [v1] The brain-voice framing (what the system
24
+ did, first person, plain sentences) earned its keep as the owner's fastest read.
25
+ - **The revision story view.** [new, from review finding 1] The challenge-pressure log
26
+ (§5.6: lived day, challenger, contributed force) rendered as a watchable narrative per
27
+ contested belief — the ledger's explainability, restored visually.
28
+ - **Identity-band and protected lists, enumerable on demand.** [review finding 2]
29
+ Permanence and inspectability scale together (scar §2.19).
30
+ - **The record, named.** The owner reads the store itself, not only renderings of it
31
+ (constitution line 6), so an opened memory says which record it is: id, revision and
32
+ content hash. For now that is what it prints; until 2026-09-18 it printed the markdown
33
+ file's path instead, which named a filesystem layout rather than the memory, and which
34
+ the bodies-in-rows work takes away.
35
+
36
+ ## 4. Drops / simplifies
37
+
38
+ - v1's 3.1k-line panel system is not ported; views are rebuilt minimal-first
39
+ (Amendment 15): status, memory browser, revision stories, band/protected lists,
40
+ activity feed. Anything further is earned by the owner asking for it.
41
+
42
+ ## 5. Contract
43
+
44
+ **Inputs** — the canonical stores (read-only), the event log, the registry of kinds/bands,
45
+ the written self page and its versions (2026-09-18; shown as a list with bodies, newest
46
+ first — there is no precedent here for a text diff between two revisions, and a page is
47
+ prose a person reads rather than a field that changed).
48
+ **Outputs** — rendered views only. **Guarantees:** **[M]** the dashboard runs in observer
49
+ mode by construction (`docs/observer-mode.md`): it strengthens nothing, deposits nothing,
50
+ resolves no references, and a test asserts its process holds no write handle to any
51
+ canonical store. **[M]** Render-time id resolution everywhere; no memory body text is
52
+ persisted into dashboard state. **[M]** Totality: every registered axis appears or is
53
+ marked absent. **[A]** Views stay readable by a non-engineer — the owner is the acceptance
54
+ test.
55
+
56
+ ## 6. Scars honored
57
+
58
+ **E7** (observer read-only both directions) · **§2.4** (absence is displayed, not silent) ·
59
+ **§2.17** (starved paths become visible) · **§2.19** (inspectability scales with
60
+ permanence) · **§2.20** (ids in state, text at render).
61
+
62
+ ## 7. Open questions
63
+
64
+ 1. Terminal-first (v1's shape) or local web view? Owner's call at build time — the
65
+ contract is identical either way.
66
+ 2. Build staging: minimal brain-view ships DURING the parallel run (owner ruling
67
+ 2026-08-25) — which views are in that minimum beyond status + revision stories?
@@ -0,0 +1,139 @@
1
+ # `dashboard/` — INTERFACE-GAPS
2
+
3
+ What this adapter needed from the core and did not find, in the order it hurt.
4
+ Each item says what exists, what is missing, what the workaround costs, and who
5
+ should own the fix. Nothing here blocks the five views; everything here is a place
6
+ where the dashboard is compensating for a core surface, which is exactly the kind
7
+ of debt that goes silent if it is not written down.
8
+
9
+ ---
10
+
11
+ ## §1 — A read-only open still writes. `store/cache.ts`, `openCache`
12
+
13
+ **Found by:** the byte-identical test, on its first run.
14
+
15
+ **Have:** `Store.open` constructs box 3 through `openCache`, which unconditionally
16
+ runs
17
+
18
+ ```
19
+ INSERT OR REPLACE INTO cache_meta (key, value) VALUES ('schemaVersion', ?)
20
+ ```
21
+
22
+ inside a transaction on the cache database. That transaction is **outside
23
+ `mutate()`** and consults no stance, so an observer's Store rewrites bytes in
24
+ `cache/cache.sqlite` before any caller does anything. Measured: the file's hash
25
+ changes on every open, and again on the next one.
26
+
27
+ **Want:** either the write is skipped under observer (it is idempotent, so an
28
+ observer skipping it loses nothing), or it becomes conditional — read the row,
29
+ write only on mismatch — which fixes it for every caller, not just instruments.
30
+
31
+ **Cost of the workaround:** the guarantee splits in two. `test/dashboard.test.ts`
32
+ proves the strict property for boxes 1 and 2 (prose and operational — hashed
33
+ before the Dashboard exists, after every render, and after close) and the
34
+ across-renders property for box 3 (hashed after open). Both are real, and the
35
+ second is weaker than the CONTRACT's plain reading of "byte-identical dir after
36
+ every view renders". Nothing canonical moves, and box 3 is rebuildable by
37
+ declaration, so this is a *provability* gap rather than a data one — but the
38
+ sentence "an instrument leaves the store as it found it" (scar E7) is not
39
+ currently true of the bytes.
40
+
41
+ **Owner:** `store/`.
42
+
43
+ ---
44
+
45
+ ## §2 — Cycle outcomes are unenumerable. No `Store.metaKeys(prefix)`
46
+
47
+ **Have:** `sleep/` records every promotion, prune and merge under a meta key —
48
+ `sleep.promoted.<id>`, `sleep.pruned.<id>`, `sleep.merged.<id>` — and box 2's meta
49
+ table is reachable only as `getMeta(key)`. There is no listing, prefix scan, or
50
+ iteration.
51
+
52
+ **Want:** `metaKeys(prefix?: string): string[]`, or the real tables
53
+ `sleep/INTERFACE-GAPS.md` §3 already asks for.
54
+
55
+ **Cost of the workaround:** `status` cannot report *what the last cycle actually
56
+ did*. It reports what is durable and enumerable instead: the per-phase completion
57
+ markers (which lived day each finished) and the archived-with-reason state of every
58
+ row, labelled **since birth**. That is honest, and it is less than the view wants.
59
+ A per-cycle promotion count is currently unanswerable from a cold store — the
60
+ `CycleReport` that holds it is returned to its caller and then gone.
61
+
62
+ Note the durable `events` table *does* carry these (`band.promoted`,
63
+ `memory.pruned`, `memory.merged`) with a `dedupKey`, so a day-scoped count is
64
+ reachable through `eventLog({ sinceDay })`. That is the likely fix and it is a view
65
+ decision, not a core one — but events are bounded-retention telemetry and the meta
66
+ records are not, so the two are not interchangeable for an old cycle.
67
+
68
+ **Owner:** `store/` (the surface), `sleep/` (the records).
69
+
70
+ ---
71
+
72
+ ## §3 — `self.enumerate` drops what it cannot read
73
+
74
+ **Found by:** the identity-view test that removes a protected element.
75
+
76
+ **Have:** `self/identity.ts`'s `enumerateOne` returns `null` when `readProse` or
77
+ `physicsOf` throws, and `enumerate` skips it. So a memory the owner removed simply
78
+ stops being in the list, with no record that it was.
79
+
80
+ **Want:** the enumeration to carry unreadable ids as a third bucket —
81
+ `unreadable: { id, reason }[]` — beside `identity` and `protected`.
82
+
83
+ **Cost of the workaround:** the two lists behave differently, and only one is
84
+ fixable here. The identity BAND is queryable (`list({ band: "identity" })`), so the
85
+ view diffs the band against the enumeration and prints the difference as named
86
+ absences ("Identity-band rows I could not read just now"). The PROTECTED set is
87
+ not: protection is a physics flag on the row whose physics is exactly what stopped
88
+ reading, so there is no way to know a removed row was protected. A removed
89
+ protected element therefore leaves the protected list silently — which is scar
90
+ §2.19's failure shape (permanence without inspectability) reached from the other
91
+ side.
92
+
93
+ Related, smaller: there is no `list({ protected: true })` filter, which is why
94
+ `enumerate` scans every live row to build its second list.
95
+
96
+ **Owner:** `self/`, with a `store/` filter as the cheaper half.
97
+
98
+ ---
99
+
100
+ ## §4 — Observer is enforced by wrapper and scan, not by types
101
+
102
+ **Have:** `Dashboard.open` sets `observer: true` itself, and `sourceOf` refuses any
103
+ Counterpart whose stance is not observer. `DashboardSource` narrows the read
104
+ surface to `store`, `schemas`, `self` — but those are the real objects, and their
105
+ write methods are still on them. A view function *could* call one; the reason none
106
+ does is the source scan in `test/dashboard.test.ts`, which strips comments and
107
+ string literals and fails on any `WRITE_METHODS` name or filesystem call anywhere
108
+ in this directory.
109
+
110
+ **Want:** a read-only projection type in the core — `ReadOnlyStore`, structurally
111
+ the read half of `Store` — so "an instrument cannot reach a write method" is a
112
+ compile error rather than a test. `docs/observer-mode.md` G4's spirit is that an
113
+ observer proves its own stand-down rather than relying on the store's; a type would
114
+ carry that further than a runtime check does.
115
+
116
+ **Cost of the workaround:** the mechanism is a test, so it protects this directory
117
+ and nothing else. Any future adapter that wants the same property re-implements the
118
+ scan.
119
+
120
+ **Owner:** `store/`.
121
+
122
+ ---
123
+
124
+ ## §5 — The event log has no distinct-name query
125
+
126
+ **Have:** `eventLog({ name })` filters to one name. To render the durable-event
127
+ vocabulary — every name that *can* be recorded, with its count — the view issues
128
+ one query per known name and takes the length.
129
+
130
+ **Want:** `eventCounts(): { name: string; count: number }[]`, or a `distinct`
131
+ option. It would also let the view discover names the registry does not know,
132
+ which is the more interesting direction: right now `DURABLE_EVENTS` is exhaustive
133
+ by type over the record interfaces and the event-name literals, and a writer that
134
+ appends a raw string name would go unlisted without failing `tsc`.
135
+
136
+ **Cost of the workaround:** one query per known name where one would do, on a table that is
137
+ small by construction. Negligible today; wrong at a hundred names.
138
+
139
+ **Owner:** `store/`.