counterparts 0.3.19 → 0.3.21

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 (108) hide show
  1. package/CHANGELOG.md +119 -0
  2. package/README.md +12 -4
  3. package/docs/module-map.md +6 -0
  4. package/package.json +1 -1
  5. package/src/adapters/claude-code/CONTRACT.md +14 -13
  6. package/src/adapters/claude-code/NOTES.md +29 -0
  7. package/src/adapters/claude-code/bin/hook.ts +12 -4
  8. package/src/adapters/claude-code/bin/runner.ts +41 -20
  9. package/src/adapters/claude-code/doctor.ts +123 -30
  10. package/src/adapters/claude-code/hooks.ts +70 -86
  11. package/src/adapters/claude-code/index.ts +0 -3
  12. package/src/adapters/cli/NOTES.md +8 -0
  13. package/src/adapters/cli/commands-delete.ts +273 -0
  14. package/src/adapters/cli/commands-install.ts +29 -10
  15. package/src/adapters/cli/commands-note.ts +1 -1
  16. package/src/adapters/cli/commands-readings.ts +4 -2
  17. package/src/adapters/cli/commands-scope.ts +25 -4
  18. package/src/adapters/cli/commands-self.ts +4 -1
  19. package/src/adapters/cli/commands-start-fresh.ts +1 -1
  20. package/src/adapters/cli/commands.ts +22 -3
  21. package/src/adapters/cli/help.ts +7 -7
  22. package/src/adapters/cli/install.ts +4 -2
  23. package/src/adapters/cli/mechanisms.ts +2 -2
  24. package/src/adapters/cli/removal.ts +1 -1
  25. package/src/adapters/config.ts +112 -30
  26. package/src/adapters/dashboard/CONTRACT.md +14 -0
  27. package/src/adapters/dashboard/NOTES.md +15 -0
  28. package/src/adapters/dashboard/activity.ts +1 -1
  29. package/src/adapters/dashboard/bin/dashboard.ts +3 -3
  30. package/src/adapters/dashboard/browse.ts +1 -1
  31. package/src/adapters/dashboard/identity.ts +1 -1
  32. package/src/adapters/dashboard/registries.ts +14 -14
  33. package/src/adapters/dashboard/resolve.ts +1 -1
  34. package/src/adapters/dashboard/status.ts +5 -2
  35. package/src/adapters/dashboard/web/actions.ts +21 -2
  36. package/src/adapters/dashboard/web/app.html +1 -1
  37. package/src/adapters/dashboard/web/flow.ts +1 -1
  38. package/src/adapters/dashboard/web/mechanisms/consolidation/index.js +2 -2
  39. package/src/adapters/dashboard/web/mechanisms/consolidation/panel.js +1 -1
  40. package/src/adapters/dashboard/web/mechanisms/decay/index.js +1 -1
  41. package/src/adapters/dashboard/web/mechanisms/reconsolidation/index.js +2 -2
  42. package/src/adapters/dashboard/web/mechanisms/retrieval/panel.js +3 -3
  43. package/src/adapters/dashboard/web/mechanisms/salience/panel.js +2 -2
  44. package/src/adapters/dashboard/web/narrate.ts +11 -11
  45. package/src/adapters/dashboard/web/pages/flow/sections/heatmap.js +1 -1
  46. package/src/adapters/dashboard/web/pages/health/sections/checks.js +3 -2
  47. package/src/adapters/dashboard/web/pages/health/sections/cycle.js +5 -5
  48. package/src/adapters/dashboard/web/pages/health/sections/mechanisms.js +1 -1
  49. package/src/adapters/dashboard/web/pages/health/sections/wake.js +2 -2
  50. package/src/adapters/dashboard/web/pages/memories/sections/tools.js +73 -3
  51. package/src/adapters/dashboard/web/pages/self/sections/dreams.js +2 -2
  52. package/src/adapters/dashboard/web/pages/self/sections/journal.js +5 -5
  53. package/src/adapters/dashboard/web/pages/self/sections/page.js +13 -13
  54. package/src/adapters/dashboard/web/pages/self/sections/settling.js +1 -1
  55. package/src/adapters/dashboard/web/pages/self/sections/wake.js +7 -7
  56. package/src/adapters/dashboard/web/server.ts +9 -1
  57. package/src/adapters/dashboard/web/shared/event-modal.js +1 -1
  58. package/src/adapters/dashboard/web/shared/memory-card-words.js +1 -1
  59. package/src/adapters/dashboard/web/shared/memory-modal.js +5 -5
  60. package/src/adapters/dashboard/web/views/activity.ts +1 -1
  61. package/src/adapters/dashboard/web/views/dreams.ts +1 -1
  62. package/src/adapters/dashboard/web/views/health.ts +1 -1
  63. package/src/adapters/dashboard/web/views/mechanisms.ts +6 -6
  64. package/src/adapters/dashboard/web/views/mind.ts +21 -9
  65. package/src/adapters/dashboard/web/views/overview.ts +3 -3
  66. package/src/adapters/expansions.ts +14 -1
  67. package/src/adapters/fired.ts +1 -1
  68. package/src/adapters/lifecycle.ts +25 -4
  69. package/src/adapters/log/index.ts +21 -0
  70. package/src/adapters/mcp/CONTRACT.md +1 -1
  71. package/src/adapters/mcp/server-self-tools.ts +25 -1
  72. package/src/adapters/mcp/server-words.ts +3 -2
  73. package/src/adapters/mcp/server.ts +45 -9
  74. package/src/adapters/mcp/tools.ts +4 -3
  75. package/src/adapters/plugin.ts +44 -8
  76. package/src/adapters/sessions.ts +5 -2
  77. package/src/adapters/snapshots.ts +90 -1
  78. package/src/adapters/store-id.ts +58 -0
  79. package/src/core/counterpart-boundary.ts +9 -0
  80. package/src/core/counterpart.ts +19 -6
  81. package/src/core/dream/CONTRACT.md +8 -0
  82. package/src/core/dream/index.ts +15 -2
  83. package/src/core/handoff/last-here.ts +24 -12
  84. package/src/core/onboarding.ts +231 -62
  85. package/src/core/prospective/CONTRACT.md +4 -1
  86. package/src/core/prospective/index.ts +23 -5
  87. package/src/core/recall/CONTRACT.md +3 -1
  88. package/src/core/recall/INTERFACE-GAPS.md +1 -1
  89. package/src/core/recall/NOTES.md +14 -0
  90. package/src/core/recall/session.ts +40 -1
  91. package/src/core/reconsolidation.ts +1 -1
  92. package/src/core/remember/CONTRACT.md +14 -8
  93. package/src/core/remember/owes.ts +68 -28
  94. package/src/core/remember/owner-strike-seam.ts +6 -3
  95. package/src/core/remember/spans.ts +3 -2
  96. package/src/core/remember/tunables.ts +18 -5
  97. package/src/core/remember/write-up-seam.ts +2 -1
  98. package/src/core/self/CONTRACT.md +25 -9
  99. package/src/core/self/NOTES.md +6 -0
  100. package/src/core/self/briefing-delivery.ts +349 -0
  101. package/src/core/self/briefing-page.ts +315 -0
  102. package/src/core/self/briefing-room.ts +563 -0
  103. package/src/core/self/briefing.ts +304 -1156
  104. package/src/core/self/index.ts +28 -6
  105. package/src/core/store/NOTES.md +6 -5
  106. package/src/core/store/index.ts +39 -0
  107. package/src/core/store/paths.ts +9 -2
  108. package/src/adapters/claude-code/primacy.ts +0 -162
@@ -264,8 +264,11 @@ export interface WakeShown {
264
264
  readonly identity: readonly string[];
265
265
  readonly craft: readonly string[];
266
266
  readonly hints: readonly string[];
267
- /** What each list lane left out, by id — its "N more" line's. */
268
- readonly more: { readonly identity: readonly string[]; readonly craft: readonly string[] };
267
+ /** What each list lane left out, by id — its "N more" line's. For
268
+ * "Nearby" (2026-10-12), only the hints the lane CHOSE and the trim
269
+ * popped — never its cap's, which is the rest of the store — so a session
270
+ * start can list or count them in its own room, not drop them silently. */
271
+ readonly more: { readonly identity: readonly string[]; readonly craft: readonly string[]; readonly hints: readonly string[] };
269
272
  }
270
273
 
271
274
  /** The record `WAKE_SHOWN_KEY` holds, or null — absent (a bundle published
@@ -284,6 +287,8 @@ export function wakeShown(store: Pick<Store, "getMeta">): WakeShown | null {
284
287
  const hints = ids(r["hints"]);
285
288
  const moreIdentity = ids(more["identity"] ?? []);
286
289
  const moreCraft = ids(more["craft"] ?? []);
290
+ // Absent on a record written before 2026-10-12: nothing carried.
291
+ const moreHints = ids(more["hints"] ?? []) ?? [];
287
292
  const budget = num(r["budget"]);
288
293
  const hash = r["hash"];
289
294
  if (typeof hash !== "string" || hash.length === 0) return null;
@@ -301,7 +306,7 @@ export function wakeShown(store: Pick<Store, "getMeta">): WakeShown | null {
301
306
  identity,
302
307
  craft,
303
308
  hints,
304
- more: { identity: moreIdentity, craft: moreCraft },
309
+ more: { identity: moreIdentity, craft: moreCraft, hints: moreHints },
305
310
  };
306
311
  } catch {
307
312
  return null;
@@ -417,7 +422,7 @@ export interface BoundaryRequest extends BriefingRequest {
417
422
  * ARRIVING ITEMS PAST THE CALLER'S COUNT, by id (review of #367,
418
423
  * 2026-10-10): `prospective/`'s `HORIZON_ITEMS` offers the lane two, and a
419
424
  * third dated item was in no wake line and no count. They join the lane's
420
- * overflow, so its "N more arriving" line names them for recall. The
425
+ * overflow, so its "N more coming up" line names them for recall. The
421
426
  * owner's wake only (ignored with `omit`).
422
427
  */
423
428
  readonly horizonMore?: readonly string[];
@@ -955,7 +960,10 @@ export class Self {
955
960
  }
956
961
  return out;
957
962
  };
958
- const hintsScanned = carried(shown.hints, "hints");
963
+ // "Nearby" as the turn-end CHOSE it — what it listed, then what its trim
964
+ // popped (2026-10-12) — so this composition lists or counts them in its
965
+ // own room rather than leaving the popped ones without a word.
966
+ const hintsScanned = carried([...shown.hints, ...shown.more.hints], "hints");
959
967
  const covered = hintsScanned.length === 0 ? new Set<string>() : this.pageCovers(hintsScanned);
960
968
  const lanes: Lanes = {
961
969
  identity: carried(shown.identity, "identity").map((s) => rankedAs(note(s), "identity")),
@@ -2023,6 +2031,16 @@ export class Self {
2023
2031
  hints: briefing.counts.hints,
2024
2032
  horizon: briefing.counts.horizon,
2025
2033
  trimmed: briefing.trimmed.length,
2034
+ // WHAT THE WAKE COUNTS RATHER THAN LISTS, AND WHAT IT LEFT UNSAID
2035
+ // (2026-10-12): `counted` is the step-downs ("N more", a collapsed or
2036
+ // the folded line), never a fault; `untold` is ids no line names —
2037
+ // a silent drop, which doctor's Wake line reads in red.
2038
+ counted: Object.values(briefing.more).reduce((n, k) => n + (k ?? 0), 0),
2039
+ folded: briefing.folded,
2040
+ untold: Object.values(briefing.untold).reduce((n, k) => n + (k ?? 0), 0),
2041
+ untoldLanes: LANE_ORDER.filter((l) => (briefing.untold[l] ?? 0) > 0).join(","),
2042
+ // "Nearby" left uncounted by rank: a step-down, not a drop (2026-10-12).
2043
+ quiet: Object.values(briefing.quiet).reduce((n, k) => n + (k ?? 0), 0),
2026
2044
  // WHETHER THE PAGE RENDERED, AND WHETHER IT WAS CUT. Zero and false mean
2027
2045
  // "no page in this render", which is what a store with none looks like;
2028
2046
  // without these a wake led by a 6 KB page and a wake led by nothing are
@@ -3177,7 +3195,11 @@ function shownRecord(briefing: BriefingResult, lanes: Lanes, req: BoundaryReques
3177
3195
  identity: briefing.kept.identity,
3178
3196
  craft: briefing.kept.craft,
3179
3197
  hints: briefing.kept.hints,
3180
- more: { identity: lost("identity"), craft: lost("craft") },
3198
+ more: {
3199
+ identity: lost("identity"),
3200
+ craft: lost("craft"),
3201
+ hints: lanes.hints.map((r) => r.id).filter((id) => !briefing.kept.hints.includes(id)),
3202
+ },
3181
3203
  };
3182
3204
  }
3183
3205
 
@@ -499,11 +499,12 @@ NUMBER against a store already named by `--dir`; refusing that would teach peopl
499
499
  the guard. The line: **the guard refuses an implicit default that locates a store or
500
500
  writes the live base; a number read from the default config is neither.**
501
501
 
502
- **Not covered, on purpose.** `claude-code/primacy.ts` reads the parallel run's assignment
503
- file at `~/.memory-ab` by default — read-only, no store, and `test/store.test.ts` pins it
504
- read-only. `tools/parallel/bin/{preflight,daily,restart}.ts` carry the live paths as
505
- defaults by their own contract (every real path is a flag; the run directory's overlap
506
- guard needs them named). Neither goes through `dataDir()` and neither opens a store.
502
+ **Not covered, on purpose.** `tools/parallel/bin/{preflight,daily,restart}.ts` carry the
503
+ live paths as defaults by their own contract (every real path is a flag; the run directory's
504
+ overlap guard needs them named). They do not go through `dataDir()` and open no store. (The
505
+ parallel run's assignment resolver was the other one; it left src/ on 2026-10-12 for
506
+ `tools/parallel/primacy.ts`, takes its directory in, and `test/store.test.ts` pins it
507
+ read-only.)
507
508
 
508
509
  **The gap between the two doors, and how it surfaces.** A NAMED configuration that names
509
510
  no store — `--config` at a file with only `injectionBudgetBytes` — and no
@@ -417,6 +417,7 @@ export const WRITE_METHODS = [
417
417
  "updateMeta",
418
418
  "setGateRecords",
419
419
  "pruneGateSessions",
420
+ "rewriteGateValues",
420
421
  "appendEvent",
421
422
  "pruneEvents",
422
423
  "setRanking",
@@ -3272,6 +3273,44 @@ export class Store {
3272
3273
  return report;
3273
3274
  }
3274
3275
 
3276
+ /**
3277
+ * Rewrite one record's `value` across EVERY session, in one transaction:
3278
+ * the rows of `(kind, ref)` whose value contains `contains` are handed to
3279
+ * `rewrite`, and a returned string that differs replaces the value — only
3280
+ * the value; the row, its turn and its day stay. `null` leaves a row alone.
3281
+ * Each update is conditioned on the value it read, so it never writes over
3282
+ * a newer save. Returns how many rows changed. The store knows nothing of
3283
+ * what the value means: `recall/session.ts#clearIdleCarries` does, and uses
3284
+ * this to take typed cue tokens out of sessions that have gone quiet.
3285
+ */
3286
+ rewriteGateValues(kind: string, ref: string, contains: string, rewrite: (value: string) => string | null): number {
3287
+ const changed = this.mutate("rewriteGateValues", () => {
3288
+ const rows = this.ops.all<{ session_id: string; value: string }>(
3289
+ "SELECT session_id, value FROM gate_session WHERE kind = ? AND ref = ? AND value IS NOT NULL AND instr(value, ?) > 0",
3290
+ kind,
3291
+ ref,
3292
+ contains,
3293
+ );
3294
+ let n = 0;
3295
+ for (const row of rows) {
3296
+ const next = rewrite(row.value);
3297
+ if (next === null || next === row.value) continue;
3298
+ this.ops.run(
3299
+ "UPDATE gate_session SET value = ? WHERE session_id = ? AND kind = ? AND ref = ? AND value = ?",
3300
+ next,
3301
+ row.session_id,
3302
+ kind,
3303
+ ref,
3304
+ row.value,
3305
+ );
3306
+ n++;
3307
+ }
3308
+ return n;
3309
+ });
3310
+ this.emit("store.gate.rewritten", undefined, { kind, count: changed });
3311
+ return changed;
3312
+ }
3313
+
3275
3314
  // ── box 2: the durable event log (SEAMS item K) ────────────────────────────
3276
3315
 
3277
3316
  /**
@@ -272,8 +272,15 @@ export const LAYOUT: readonly LayoutEntry[] = [
272
272
  {
273
273
  name: "spans",
274
274
  match: "exact",
275
- backup: true,
276
- why: "remember/'s capture buffer: lived experience awaiting encoding; not reconstructible.",
275
+ // NOT BACKED UP, since 2026-10-13 (Mike): backups never hold conversation
276
+ // text. Removed, not flagged — there is no setting that turns it back on.
277
+ // The cost, accepted: a restored backup has lost whatever was not yet
278
+ // written up. The memories made from it are rows, and they are copied.
279
+ // Still CLASSIFIED (an unclassified name refuses the store at open), and
280
+ // `snapshots.ts#copyKind` keys on classification, so copies taken before
281
+ // this — which hold `spans/` — are still recognised and rotate as before.
282
+ backup: false,
283
+ why: "remember/'s capture buffer: conversation text awaiting its write-up, kept 3 days after that (never more than 14). Never backed up: backups never hold conversation text (Mike, 2026-10-13).",
277
284
  },
278
285
  {
279
286
  name: "journal",
@@ -1,162 +0,0 @@
1
- /**
2
- * THE PRIMACY RESOLVER — who delivers, for the parallel run only.
3
- *
4
- * For one or two weeks v2 runs BESIDE v1 (bansai) on the owner's real sessions.
5
- * The parallel-run contract's G3 is that exactly one system delivers: two wakes,
6
- * two recall blocks and two episode asks in the same context is not a comparison,
7
- * it is noise the owner has to live inside.
8
- *
9
- * v1 already owns the switch. It reads `~/.memory-ab/assignment.json` — the file
10
- * the retired engram pairing left behind — and MUTES its wake, its recall
11
- * injection and its episode ask only when the resolved system is `engram`. v2
12
- * occupies the slot engram vacated: `override: "engram"` means "the other one
13
- * delivers today", and the other one is now this.
14
- *
15
- * {"mode":"alternate-day","anchor":"2026-07-17","override":"bansai"}
16
- *
17
- * **The rule that makes the joint failure state safe: we FAIL TOWARD MUTE.**
18
- * v1 fails toward INJECT — a missing, torn or unrecognized file resolves to
19
- * "both", and v1 speaks. So this resolver delivers on exactly ONE reading of the
20
- * file (it parsed, and `override` is the string `engram`) and stands down on
21
- * every other reading, named. Two failing resolvers therefore land on v1-only,
22
- * never on silence and never on a double injection.
23
- *
24
- * `assignmentHealth()` is the preflight's half. `override: null` is the state
25
- * that must not exist during the run: v1 would fall back to `alternate-day`
26
- * against a system that no longer exists and mute ITSELF every other day, while
27
- * v2 (reading no `engram`) mutes every day — the silence neither resolver's own
28
- * fail direction can produce alone. The preflight asserts it away before Phase P
29
- * starts rather than discovering it as a quiet day.
30
- *
31
- * Nothing here throws, nothing here caches, nothing here writes. The directory
32
- * is resolved AT CALL TIME so a test (and the preflight) can redirect it with
33
- * `MEMORY_AB_DIR` without this module having been loaded in a particular order.
34
- */
35
- import { existsSync, readFileSync } from "node:fs";
36
- import { homedir } from "node:os";
37
- import { join } from "node:path";
38
-
39
- /** The one environment variable that moves the A/B directory. Tests use it. */
40
- export const AB_DIR_ENV = "MEMORY_AB_DIR";
41
-
42
- /** The system v1's assignment file names when it means "stand down". */
43
- export const V2_OVERRIDE = "engram";
44
-
45
- /** Read at call time, never cached: the directory may move between calls. */
46
- export function abDir(): string {
47
- return process.env[AB_DIR_ENV] ?? join(homedir(), ".memory-ab");
48
- }
49
-
50
- export function assignmentPath(): string {
51
- return join(abDir(), "assignment.json");
52
- }
53
-
54
- export type AssignmentState = "ok" | "missing" | "unreadable" | "malformed";
55
-
56
- /** v1's file, as far as v2 is entitled to understand it. Strings or null. */
57
- export interface Assignment {
58
- readonly override: string | null;
59
- readonly mode: string | null;
60
- readonly anchor: string | null;
61
- readonly state: AssignmentState;
62
- }
63
-
64
- /**
65
- * A pure read. Never throws — a resolver that can throw is a resolver that can
66
- * fail a hook, and the fail direction would then be the host's, not ours.
67
- */
68
- export function readAssignment(): Assignment {
69
- const empty = { override: null, mode: null, anchor: null } as const;
70
- const path = assignmentPath();
71
- if (!existsSync(path)) return { ...empty, state: "missing" };
72
- let raw: string;
73
- try {
74
- raw = readFileSync(path, "utf8");
75
- } catch {
76
- return { ...empty, state: "unreadable" };
77
- }
78
- let parsed: unknown;
79
- try {
80
- parsed = JSON.parse(raw);
81
- } catch {
82
- // A torn write mid-run reads as unreadable, not as an assignment.
83
- return { ...empty, state: "unreadable" };
84
- }
85
- if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) {
86
- return { ...empty, state: "malformed" };
87
- }
88
- const rec = parsed as Record<string, unknown>;
89
- const str = (key: string): string | null =>
90
- typeof rec[key] === "string" ? (rec[key] as string) : null;
91
- return { override: str("override"), mode: str("mode"), anchor: str("anchor"), state: "ok" };
92
- }
93
-
94
- export type PrimacyReason =
95
- | "override-engram"
96
- | "override-bansai"
97
- | "override-absent"
98
- | "file-missing"
99
- | "unreadable"
100
- | "malformed";
101
-
102
- export interface Primacy {
103
- /** True on exactly one reading of the file. Everything else is a stand-down. */
104
- readonly deliver: boolean;
105
- /** Who is delivering right now, as the daily record spells it. */
106
- readonly system: "v1" | "v2";
107
- readonly reason: PrimacyReason;
108
- }
109
-
110
- /**
111
- * Should v2 deliver into this session's context?
112
- *
113
- * TRUE iff the file parses and `override === "engram"`. Every other reading is
114
- * a named refusal — the fail-toward-mute rule at the top of this file.
115
- */
116
- export function primacy(): Primacy {
117
- const assignment = readAssignment();
118
- if (assignment.state === "missing") return mute("file-missing");
119
- if (assignment.state === "unreadable") return mute("unreadable");
120
- if (assignment.state === "malformed") return mute("malformed");
121
- if (assignment.override === V2_OVERRIDE) {
122
- return { deliver: true, system: "v2", reason: "override-engram" };
123
- }
124
- if (assignment.override === "bansai") return mute("override-bansai");
125
- // null, "none", "counterparts", or any other string: v1 does not read it as
126
- // "stand down", so v2 must not read it as "deliver".
127
- return mute("override-absent");
128
- }
129
-
130
- function mute(reason: PrimacyReason): Primacy {
131
- return { deliver: false, system: "v1", reason };
132
- }
133
-
134
- export interface AssignmentHealth {
135
- /** Only an EXPLICIT `bansai` or `engram` is a state the run may start in. */
136
- readonly healthy: boolean;
137
- readonly reason: PrimacyReason;
138
- /** Advisory: `alternate-day` against a retired engram is the latent hazard. */
139
- readonly mode: string | null;
140
- readonly override: string | null;
141
- }
142
-
143
- /**
144
- * The preflight's check. Healthy requires `override` to be exactly `bansai` or
145
- * `engram` — the two readings under which the two resolvers cannot both mute.
146
- * `mode` rides along unjudged so the preflight can note that a file whose
147
- * override is ever cleared falls back to day-alternation against a system that
148
- * is not there any more.
149
- */
150
- export function assignmentHealth(): AssignmentHealth {
151
- const assignment = readAssignment();
152
- const verdict = primacy();
153
- const healthy =
154
- assignment.state === "ok" &&
155
- (assignment.override === V2_OVERRIDE || assignment.override === "bansai");
156
- return {
157
- healthy,
158
- reason: verdict.reason,
159
- mode: assignment.mode,
160
- override: assignment.override,
161
- };
162
- }