counterparts 0.3.49 → 0.3.52

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 (95) hide show
  1. package/CHANGELOG.md +96 -0
  2. package/README.md +18 -10
  3. package/docs/QUICKSTART.md +46 -12
  4. package/docs/manual.md +2 -1
  5. package/docs/module-map.md +3 -0
  6. package/package.json +3 -2
  7. package/src/adapters/claude-code/NOTES.md +43 -0
  8. package/src/adapters/claude-code/child.ts +23 -2
  9. package/src/adapters/claude-code/doctor-growth.ts +269 -0
  10. package/src/adapters/claude-code/doctor-host.ts +19 -1
  11. package/src/adapters/claude-code/doctor-recall-steps.ts +84 -0
  12. package/src/adapters/claude-code/doctor.ts +26 -3
  13. package/src/adapters/claude-code/hooks.ts +7 -1
  14. package/src/adapters/cli/INTERFACE-GAPS.md +18 -0
  15. package/src/adapters/cli/NOTES.md +24 -0
  16. package/src/adapters/cli/commands-dashboard.ts +9 -1
  17. package/src/adapters/cli/commands-project.ts +2 -1
  18. package/src/adapters/cli/commands-readings.ts +19 -8
  19. package/src/adapters/cli/commands-verify.ts +1 -1
  20. package/src/adapters/cli/commands.ts +4 -2
  21. package/src/adapters/cli/export.ts +2 -1
  22. package/src/adapters/cli/install.ts +14 -6
  23. package/src/adapters/cli/mechanisms.ts +1 -1
  24. package/src/adapters/cli/wire.ts +22 -6
  25. package/src/adapters/dashboard/activity.ts +6 -3
  26. package/src/adapters/dashboard/registries.ts +11 -0
  27. package/src/adapters/dashboard/web/flow.ts +3 -0
  28. package/src/adapters/dashboard/web/lanes.ts +4 -0
  29. package/src/adapters/dashboard/web/narrate.ts +23 -0
  30. package/src/adapters/dashboard/web/views/activity.ts +4 -2
  31. package/src/adapters/dashboard/web/views/flow-view.ts +6 -0
  32. package/src/adapters/dashboard/web/views/memory.ts +13 -0
  33. package/src/adapters/dashboard/web/views/shared.ts +2 -1
  34. package/src/adapters/fired.ts +113 -11
  35. package/src/adapters/host-wiring.ts +3 -1
  36. package/src/adapters/lifecycle.ts +36 -9
  37. package/src/adapters/mcp/CONTRACT.md +22 -1
  38. package/src/adapters/mcp/server-args.ts +51 -0
  39. package/src/adapters/mcp/server-session-end.ts +8 -2
  40. package/src/adapters/mcp/server-words.ts +14 -1
  41. package/src/adapters/mcp/server.ts +117 -7
  42. package/src/adapters/mcp/tools.ts +4 -3
  43. package/src/adapters/plugin.ts +1 -0
  44. package/src/adapters/presence.ts +74 -0
  45. package/src/adapters/runtime.ts +258 -8
  46. package/src/adapters/scopes.ts +3 -2
  47. package/src/adapters/spawn.ts +3 -0
  48. package/src/adapters/update-check.ts +1 -1
  49. package/src/adapters/windows-shims.mjs +102 -0
  50. package/src/core/close-hint.ts +297 -0
  51. package/src/core/counterpart-boundary.ts +15 -0
  52. package/src/core/counterpart-deposit.ts +24 -3
  53. package/src/core/counterpart-events.ts +14 -0
  54. package/src/core/counterpart-types.ts +6 -0
  55. package/src/core/counterpart-wake-shape.ts +88 -19
  56. package/src/core/counterpart.ts +143 -7
  57. package/src/core/dream/index.ts +1 -1
  58. package/src/core/dream/reflect.ts +54 -34
  59. package/src/core/encode/NOTES.md +55 -0
  60. package/src/core/encode/secrets.ts +477 -3
  61. package/src/core/event-rollup.ts +175 -0
  62. package/src/core/handoff/CONTRACT.md +7 -0
  63. package/src/core/interference.ts +496 -0
  64. package/src/core/open-close.ts +583 -0
  65. package/src/core/prospective/CONTRACT.md +24 -0
  66. package/src/core/prospective/derive.ts +22 -1
  67. package/src/core/prospective/index.ts +115 -1
  68. package/src/core/recall/CONTRACT.md +9 -2
  69. package/src/core/recall/NOTES.md +99 -0
  70. package/src/core/recall/activate.ts +83 -6
  71. package/src/core/recall/index.ts +75 -5
  72. package/src/core/recall/probe.ts +134 -0
  73. package/src/core/recall/steps.ts +84 -0
  74. package/src/core/recall/tunables.ts +11 -0
  75. package/src/core/reconsolidation.ts +14 -4
  76. package/src/core/remember/CONTRACT.md +2 -1
  77. package/src/core/remember/NOTES.md +15 -0
  78. package/src/core/remember/spans.ts +44 -6
  79. package/src/core/self/NOTES.md +21 -0
  80. package/src/core/self/work.ts +9 -5
  81. package/src/core/sleep/CONTRACT.md +36 -2
  82. package/src/core/sleep/NOTES.md +35 -0
  83. package/src/core/sleep/close.ts +77 -0
  84. package/src/core/sleep/cycle.ts +44 -0
  85. package/src/core/sleep/index.ts +4 -0
  86. package/src/core/sleep/interfere.ts +73 -0
  87. package/src/core/sleep/log.ts +27 -2
  88. package/src/core/sleep/tunables.ts +19 -0
  89. package/src/core/sleep/types.ts +20 -0
  90. package/src/core/store/index.ts +157 -10
  91. package/src/core/store/operational.ts +22 -0
  92. package/src/core/store/owner-op-seam.ts +16 -0
  93. package/src/core/store/reads-history.ts +173 -7
  94. package/src/core/store/reads-memories.ts +253 -4
  95. package/src/core/store/shapes.ts +8 -0
@@ -3060,3 +3060,24 @@ never a card's names (`memoryTexts`, `liveMemoryTexts`). Seen only by looking: r
3060
3060
  every store that already existed keeps its own word or off. The harness's `NEW_INSTALL_META` mirrors it, held equal by
3061
3061
  `test/harness-week.test.ts`, so a `week` run is shaped with no `--set`. `WAKE_SHAPE_KEY` moved to `self/tunables.ts` (re-
3062
3062
  exported from the composer) so onboarding reads it from a leaf.
3063
+
3064
+ ## 2026-11-07 — each item shows where it can be acted on (SO1 step 3)
3065
+
3066
+ - **What reached motelier.** Plain reminders to do things, from other folders: Coming up let every plain reminder due
3067
+ today through (`todayAll`'s `|| c.plain`), the lines above the wake told it, and whichever folder woke first that day
3068
+ got it whole and wrote the told latch, so its own folder only saw "N due today, shown earlier". Still open's folder
3069
+ filter held for work rows; what leaked through it was `about: world`, which `threadPlace` read as personal.
3070
+ - **The rule now.** A plain reminder and an open thread cross folders only when marked me, us or owner (or with no
3071
+ folder). Another folder's work, or one marked world, is said in its own folder and the folders joined to it:
3072
+ `threadPlace` reads world as `out` there, Coming up counts it "in other folders", and the delivery holds it back
3073
+ unclaimed (`Counterpart#plainAway`), so its folder's first wake that day tells it whole. `isWorkMemory` is unchanged:
3074
+ world is still personal for Nearby and the craft lane; only where an owed item is shown moved.
3075
+ - **Fix round (the roadmap's calls):** a chat with no folder is told only personal reminders, and a telling counts
3076
+ as "shown earlier" for a folder's work only when it was in that folder. A work reminder unsaid because its folder
3077
+ had no session crosses once, two lived days past its last day (`Counterpart#plainCrossing`, its own latch).
3078
+ "In other folders" names up to two plain titles when the line has room. Doctor: info, not amber, until the
3079
+ crossing day has passed unsaid. Round 3: the crossing reads only at a session start (or an attended wake tool),
3080
+ memoised per day, from the session starts' presence rows (`adapters/presence.ts`), which doctor reads too; checks
3081
+ never cross, and a crossed one gets no late beat at home. Bench: `bun tools/bench/plain-cross.ts`.
3082
+ - **Not built:** the four classes as code (mine, Mike's, world's, personal). Steps 1-2 need only personal against the
3083
+ rest, which `threadPlace` already says. The mapping is in the PR and the decisions fragment, for step 4.
@@ -127,10 +127,14 @@ export function isWorkMemory(m: WorkFacts, rule: WorkRule = {}): boolean {
127
127
  * Mike agreed the lean that morning, lightly held): `here` — written in one of
128
128
  * the reader's folders (`folders`: this folder and its project's, as
129
129
  * `projectFolders` gives them, each `projectFolder`-normalized); `personal` —
130
- * not work by `isWorkMemory` (marked me, us, owner or world, or unmarked and
131
- * personal by kind), shown after this folder's wherever the session wakes;
132
- * `out` — another folder's work, which stays in its folder as "Work here"
133
- * does. A thread with NO FOLDER recorded (null, empty, or a host's name scope
130
+ * not work by `isWorkMemory` and not marked world (marked me, us or owner, or
131
+ * unmarked and personal by kind), shown after this folder's wherever the
132
+ * session wakes; `out` — another folder's work, or a thread marked world
133
+ * written in another folder, which stays in its folder as "Work here" does.
134
+ * MARKED WORLD STAYS HOME (SO1 step 3, 2026-11-07, with Mike: what crossed into
135
+ * motelier was plain things to do): a world thread is something the world owes
136
+ * (a reply, a dated check), acted on where it was written, so only me, us and
137
+ * owner cross. A journal copy is still personal. A thread with NO FOLDER recorded (null, empty, or a host's name scope
134
138
  * such as `claude-desktop:` read from somewhere else) is never `out`, marked
135
139
  * work or not: there is no folder it could be another's work in, and leaving
136
140
  * it out would leave it out of every wake. Pure.
@@ -138,7 +142,7 @@ export function isWorkMemory(m: WorkFacts, rule: WorkRule = {}): boolean {
138
142
  export function threadPlace(m: WorkFacts, folders: ReadonlySet<string>, rule: WorkRule = {}): "here" | "personal" | "out" {
139
143
  const scope = m.originScope === null ? "" : projectFolder(m.originScope);
140
144
  if (scope.length > 0 && folders.has(scope)) return "here";
141
- if (isDirectoryScope(m.originScope) && isWorkMemory(m, rule)) return "out";
145
+ if (isDirectoryScope(m.originScope) && (isWorkMemory(m, rule) || (m.about === "world" && !m.journal))) return "out";
142
146
  return "personal";
143
147
  }
144
148
 
@@ -64,7 +64,20 @@ alone.
64
64
  And the `births` phase first merges the twin cards, once per store
65
65
  (`Schemas#mergeTwinCards`, behind `schemas.TWINS_MERGE`, recorded in
66
66
  `schemas.twins.merged`), before the tally, so a name with two cards reads as held once;
67
- under observer it counts and writes nothing.)*
67
+ under observer it counts and writes nothing.)* *(2026-11-07, FD1 step 3: an `interfere`
68
+ phase runs right after `tags`, every lived day — `interference.ts#interferenceSweep`
69
+ handed in as `interfere.ts#InterfereFn`: an older routine work memory a newer one in the
70
+ same folder reads as the later state of (a later version in the same series, said released or published) is
71
+ settled `changed` under it through the one door, at most `BUDGETS.interfere` a night.
72
+ It archives nothing: the faded memory leaves later, through the prune, under the same
73
+ 14-lived-day wait. The cycle's `interfered` list, and the `sleep.cycle` row's count of
74
+ it beside `pruned`, keep the two apart. Under observer it counts and writes nothing.)* *(2026-11-07, SO1
75
+ step 1: a `close` phase runs right after `interfere`, every lived day —
76
+ `open-close.ts#openCloseSweep` handed in as `close.ts#CloseFn`: an open item whose headline
77
+ reads a release step closes when a later memory in its folder says a higher patch of the
78
+ same series was released (`closeThread`'s write, a `thread.night.closed` row each), at most
79
+ `BUDGETS.close` a night, and the "may be done" list is replaced. It archives nothing. The
80
+ `sleep.cycle` row counts `closed`. Under observer it counts and writes nothing.)*
68
81
  - **Degrade, don't abort** — index rebuild, embedding refresh, horizon computation, backup,
69
82
  and every prune each fail without failing the cycle. [v1 §5 G6]
70
83
  - **The claim is truncated only after results are applied AND persisted.** [v1 §5 G7, E6]
@@ -352,7 +365,28 @@ and their records; the next session's briefing, written last; a per-cycle summar
352
365
  existed, documented this window, and had no caller anywhere in `src/` — every unlatched
353
366
  row ever written was still there, and the dashboard's "I keep events for N lived days;
354
367
  older ones are swept" described a sweep that never ran. Owner ruling the same day: wire
355
- it into sleep.*
368
+ it into sleep.* **The per-turn rows go sooner (FD1 step 2, 2026-11-07, lightly held):**
369
+ the names in `SHORT_RETENTION_NAMES` (`recall.decision`, `recall.credit`,
370
+ `recall.rated`) keep `SHORT_RETENTION_DAYS` = 30 lived days, never more than
371
+ `retentionDays`; every other unlatched row keeps `retentionDays`. Inside the same
372
+ transaction and BEFORE the delete, each lived day of them past the short cutoff is rolled
373
+ into one latched `events.rolled` row (`core/event-rollup.ts`: counts and spans by name,
374
+ the filtered counts `fired.ts` reads, the memory ids credited for use, the session refs,
375
+ and `probe-oq4`'s two reports over the day), so a day the cap leaves half-swept already
376
+ has its whole summary. Every reader on a turn or a night reads at most 14 lived days of
377
+ these names. The all-time readers read the summaries beside the rows still held, and
378
+ skip a rolled day's held rows: the dashboard's per-name counts (`eventCountsAllTime`),
379
+ `counterparts mechanisms`' total and last fired, `probe-oq4` (`mergeProbe`,
380
+ `mergeRatings`), the memory card's days used and the flow tab's sessions seen. A
381
+ CHECK-AFTER query written in SQL against the rows sees 30 lived days; the release
382
+ sheet's copy before install keeps the rest (`docs/RELEASING.md` §5.1). No removed id
383
+ stays in a summary: the roll-up leaves out every id on the deny-list when it writes,
384
+ and the owner's removal takes the id out of summaries already written. A per-turn row
385
+ written later with a lived day already summarised (only a catch-up into the past
386
+ could) is skipped with that day's held rows and goes uncounted. `recall_turns` goes on the same short
387
+ window (`Store.pruneRecallTurns`, after the log). A pass that deleted or rolled anything
388
+ leaves one durable `sleep.log.swept` row, which doctor's `Growth: event log` line
389
+ reads.
356
390
 
357
391
  ## 6. Scars honored
358
392
 
@@ -1174,3 +1174,38 @@ Mike's store the shadow approximates on, it doesn't replay it. A held member is
1174
1174
  unpromoted strength (a promoted row reads 1), so the reach gate and the cap's order read as on
1175
1175
  would. A sleep with no candidate at all and nothing promoted writes no summary row (an empty
1176
1176
  store's nights stay quiet in the log-sweep tests).
1177
+
1178
+ ## 2026-11-07 — the `interfere` phase (FD1 step 3)
1179
+
1180
+ - After `tags`, every lived day. The work is `interference.ts`' (`interferenceSweep`, through
1181
+ `reconsolidation.ts#supersede`); this module owns the cadence and the budget
1182
+ (`BUDGETS.interfere`, 200 fades a night, CAL). A missing fn is `no-interfere-fn`;
1183
+ `INTERFERENCE_TUNABLES.ON` off is a run that did nothing, counted `switched-off`. The
1184
+ guards that kept a memory's curve land in the phase's `skipped` map, so the durable
1185
+ `sleep.cycle` row says which guard held how many.
1186
+ - Not an exit: nothing is archived here. A faded memory reaches the floor sooner and leaves
1187
+ through the prune under the unchanged `D_FLOOR_DAYS` wait, counted then as an age prune.
1188
+ `CycleReport.interfered` (and `interfered` on the `sleep.cycle` row, beside `pruned`) is
1189
+ how a read of the nights tells the two apart (FD1's condition: the age-based wave lands
1190
+ the same days).
1191
+ - A dry run on a copy of the owner's store (lived day 47), after round 3 of the review of
1192
+ #777: 67 fade the first night, observer and applied alike, ~70 ms; `settledOver` p50 27.7 →
1193
+ 29.1 ms with those 67 pairs added (1,357 settled-over memories before). Reading the whole body instead of the
1194
+ headline made one note naming 0.3.50 in its last line the "newer state" of a dozen
1195
+ unrelated ones; the headline rule (title, or the first 120 characters) is the fix.
1196
+
1197
+ ## The close phase (SO1 step 1, 2026-11-07)
1198
+
1199
+ - On a copy of the owner's store (lived day 47, the 10:32 snapshot; 338 flagged open, 248 open as the wake reads
1200
+ them), applied: 2 closes, 4 "may be done", ~80 ms (after the review of #782's narrowing too). The guards held the
1201
+ rest: `asked` 98, `no-subject` 106, `not-open` 90 (flagged but already settled over under a successor, so in no
1202
+ lane: the phase reads open as `openThread` does), `a-check` 30, `a-problem` 7, `not-a-release-step` 3,
1203
+ `conditional` 2. Against the roadmap's hand sweep of the
1204
+ same morning (32 items it judged done): 0 closed, 3 listed as may be done. Their evidence is mostly outside the
1205
+ store (commits, STANDUP, report files), which a store-only rule can't see; the 2 closes are items the sweep
1206
+ didn't list. The model's `closeNow` stays the general close.
1207
+ - Evidence for a close is an authored `about: work` row (`Store#workEventRows`) written `status: done`. A release
1208
+ memory left unmarked (12 genuine releases on the copy are) or not `about: work` is never evidence: recall lost,
1209
+ never a wrong close. Both closes on the copy rest on done rows.
1210
+ - A closed item without `settlesWhen` is no longer an open item to the interference sweep, so a later night can fade
1211
+ it under the release; the reflect tool's `reopen` undoes that fade too (`open-close.ts#reopenNightClosed`).
@@ -0,0 +1,77 @@
1
+ /**
2
+ * The close phase (2026-11-07, SO1 step 1): the night closes an open item a
3
+ * later memory plainly shows done — one rule, a later release in the same
4
+ * series closing the earlier release steps (`open-close.ts`) — and lists what
5
+ * it would not close as "may be done". Right after `interfere`, every lived
6
+ * day.
7
+ *
8
+ * `open-close.ts` owns the verdict and the write (`closeThread`'s write, with
9
+ * who closed it and why, and a durable `thread.night.closed` row each);
10
+ * `sleep/` owns the cadence and the budget (`BUDGETS.close` closes a night).
11
+ * Like `interfere`, the work is handed in by the composition root as a
12
+ * `CloseFn`: this module does not import it. A missing fn is `did-not-run`
13
+ * with reason `no-close-fn`; the switch off is a run that did nothing,
14
+ * counted `switched-off`. Under observer it counts and writes nothing.
15
+ */
16
+
17
+ import type { PhaseCtx, PhaseOutcome } from "./types.js";
18
+ import { countSkip, emptyOutcome } from "./types.js";
19
+
20
+ export interface CloseInput {
21
+ readonly day: number;
22
+ /** False under observer: write nothing, count what a night would do. */
23
+ readonly apply: boolean;
24
+ /** Closes a night at most. */
25
+ readonly budget: number;
26
+ }
27
+
28
+ /** One close: the item, the memory that showed it done, and the rule. Ids only. */
29
+ export interface ClosedRecord {
30
+ readonly id: string;
31
+ readonly shows: string;
32
+ readonly rule: string;
33
+ }
34
+
35
+ export interface CloseSweepOutcome {
36
+ /** False when the sweep is switched off. */
37
+ readonly on: boolean;
38
+ readonly examined: number;
39
+ readonly closed: readonly ClosedRecord[];
40
+ /** How many it listed as "may be done" instead. */
41
+ readonly mayBeDone: readonly unknown[];
42
+ readonly skipped: Readonly<Record<string, number>>;
43
+ readonly skippedForLimit: number;
44
+ }
45
+
46
+ export type CloseFn = (input: CloseInput) => CloseSweepOutcome;
47
+
48
+ export interface CloseResult extends PhaseOutcome {
49
+ readonly closed: readonly ClosedRecord[];
50
+ readonly mayBeDone: number;
51
+ }
52
+
53
+ export function runClose(ctx: PhaseCtx, close: CloseFn | undefined): CloseResult {
54
+ const out = emptyOutcome();
55
+ if (close === undefined) {
56
+ out.skipped["no-close-fn"] = 1;
57
+ return { ...out, closed: [], mayBeDone: 0 };
58
+ }
59
+ const r = close({ day: ctx.day, apply: ctx.apply, budget: ctx.budget });
60
+ if (!r.on) {
61
+ countSkip(out, "switched-off", 1);
62
+ return { ...out, closed: [], mayBeDone: 0 };
63
+ }
64
+ for (const [reason, n] of Object.entries(r.skipped)) countSkip(out, reason, n);
65
+ out.examined = r.examined;
66
+ out.changed = r.closed.length;
67
+ out.budgetExhausted = r.skippedForLimit > 0;
68
+ out.skippedForBudget = r.skippedForLimit;
69
+ ctx.event("sleep.close.sweep", undefined, {
70
+ day: ctx.day,
71
+ examined: r.examined,
72
+ closed: r.closed.length,
73
+ mayBeDone: r.mayBeDone.length,
74
+ skippedForLimit: r.skippedForLimit,
75
+ });
76
+ return { ...out, closed: r.closed.map((c) => ({ id: c.id, shows: c.shows, rule: c.rule })), mayBeDone: r.mayBeDone.length };
77
+ }
@@ -41,6 +41,10 @@ import { runBirths } from "./births.js";
41
41
  import type { BirthsFn } from "./births.js";
42
42
  import { runTags } from "./tags.js";
43
43
  import type { TagsFn } from "./tags.js";
44
+ import { runInterfere } from "./interfere.js";
45
+ import type { InterfereFn, InterferedRecord } from "./interfere.js";
46
+ import { runClose } from "./close.js";
47
+ import type { CloseFn, ClosedRecord } from "./close.js";
44
48
  import type { DedupCandidateSource } from "./dedup.js";
45
49
  import { runLogSweep } from "./log.js";
46
50
  import { advanceMarker, budgetFor, cadenceFor, markerDue, readMarker } from "./markers.js";
@@ -100,6 +104,18 @@ export interface SleepOptions {
100
104
  * Absent ⇒ the tags phase reports `no-tags-fn`, and nothing is tagged.
101
105
  */
102
106
  tags?: TagsFn;
107
+ /**
108
+ * The interference sweep (2026-11-07, FD1 step 3: `interference.ts`),
109
+ * injected like `tags`. Absent ⇒ the interfere phase reports
110
+ * `no-interfere-fn`, and nothing fades under a newer memory.
111
+ */
112
+ interfere?: InterfereFn;
113
+ /**
114
+ * The night's close sweep (2026-11-07, SO1 step 1: `open-close.ts`),
115
+ * injected like `interfere`. Absent ⇒ the close phase reports
116
+ * `no-close-fn`, and the night closes no open item itself.
117
+ */
118
+ close?: CloseFn;
103
119
  /** Injected ranking cache. Absent ⇒ the box-3 SQLite one, opened lazily. */
104
120
  strengthCache?: StrengthCache;
105
121
  /** Extra dedup candidates (embeddings). Isolated: a throw degrades to lexical. */
@@ -159,6 +175,8 @@ export function runCycle(opts: SleepOptions): CycleReport {
159
175
  const pruned: PrunedRecord[] = [];
160
176
  const merged: MergeRecord[] = [];
161
177
  const faded: string[] = [];
178
+ const interfered: InterferedRecord[] = [];
179
+ const closedItems: ClosedRecord[] = [];
162
180
  const bandTransitions: BandTransition[] = [];
163
181
 
164
182
  // ONE CYCLE AT A TIME, PER STORE (`lease.ts`, 2026-10-21). Taken before
@@ -183,6 +201,8 @@ export function runCycle(opts: SleepOptions): CycleReport {
183
201
  pruned: [...pruned],
184
202
  merged: [...merged],
185
203
  faded: [...faded],
204
+ interfered: [...interfered],
205
+ closed: [...closedItems],
186
206
  bandTransitions: [...bandTransitions],
187
207
  symmetry: [],
188
208
  census: census(store, day, pruned, merged, faded),
@@ -415,6 +435,26 @@ export function runCycle(opts: SleepOptions): CycleReport {
415
435
  () => (opts.tags === undefined ? "no-tags-fn" : null),
416
436
  );
417
437
 
438
+ // ── phase 4e: interference (a newer work event fades the older one) ───
439
+ runPhase(
440
+ "interfere",
441
+ (ctx) => runInterfere(ctx, opts.interfere),
442
+ (r) => {
443
+ interfered.push(...r.interfered);
444
+ },
445
+ () => (opts.interfere === undefined ? "no-interfere-fn" : null),
446
+ );
447
+
448
+ // ── phase 4f: close (a later memory shows an open item done) ─────────
449
+ runPhase(
450
+ "close",
451
+ (ctx) => runClose(ctx, opts.close),
452
+ (r) => {
453
+ closedItems.push(...r.closed);
454
+ },
455
+ () => (opts.close === undefined ? "no-close-fn" : null),
456
+ );
457
+
418
458
  // ── phase 5: dedup ────────────────────────────────────────────────────
419
459
  runPhase(
420
460
  "dedup",
@@ -519,6 +559,8 @@ export function runCycle(opts: SleepOptions): CycleReport {
519
559
  pruned,
520
560
  merged,
521
561
  faded,
562
+ interfered,
563
+ closed: closedItems,
522
564
  bandTransitions,
523
565
  symmetry,
524
566
  census: census(store, day, pruned, merged, faded),
@@ -531,6 +573,8 @@ export function runCycle(opts: SleepOptions): CycleReport {
531
573
  pruned: pruned.length,
532
574
  merged: merged.length,
533
575
  faded: faded.length,
576
+ interfered: interfered.length,
577
+ closed: closedItems.length,
534
578
  bandUp: bandTransitions.filter((t) => t.direction === "up").length,
535
579
  bandDown: bandTransitions.filter((t) => t.direction === "down").length,
536
580
  failed: reports.filter((p) => p.status === "failed").length,
@@ -121,6 +121,10 @@ export { runBirths } from "./births.js";
121
121
  export type { BirthsFn, BirthsInput, BirthsResult, BirthsSweepOutcome } from "./births.js";
122
122
  export { runTags } from "./tags.js";
123
123
  export type { TagsFn, TagsInput, TagsSweepOutcome } from "./tags.js";
124
+ export { runInterfere } from "./interfere.js";
125
+ export type { InterfereFn, InterfereInput, InterfereResult, InterfereSweepOutcome, InterferedRecord } from "./interfere.js";
126
+ export { runClose } from "./close.js";
127
+ export type { CloseFn, CloseInput, CloseResult, CloseSweepOutcome, ClosedRecord } from "./close.js";
124
128
 
125
129
  export { runBriefing } from "./briefing.js";
126
130
  export type { BriefingContext, BriefingOutcome, BriefingResult, RenderFn } from "./briefing.js";
@@ -0,0 +1,73 @@
1
+ /**
2
+ * The interference phase (2026-11-07, FD1 step 3): a newer routine work event
3
+ * on the same subject fades the older one, as a write's `updates` does —
4
+ * counted apart from the prune, so a read of the nights can tell a fade from
5
+ * an age exit. Right after `tags`, every lived day.
6
+ *
7
+ * `interference.ts` owns the verdict and the write (through the one door,
8
+ * `reconsolidation.ts#supersede`); `sleep/` owns the cadence and the budget
9
+ * (`BUDGETS.interfere` fades a night). Like the tags, the work is handed in as
10
+ * an `InterfereFn` by the composition root: this module does not import it. A
11
+ * missing fn is `did-not-run` with reason `no-interfere-fn`; the switch off is
12
+ * a run that did nothing, counted `switched-off`. Under observer it counts what
13
+ * a night would fade and writes nothing.
14
+ */
15
+
16
+ import type { PhaseCtx, PhaseOutcome } from "./types.js";
17
+ import { countSkip, emptyOutcome } from "./types.js";
18
+
19
+ export interface InterfereInput {
20
+ readonly day: number;
21
+ /** False under observer: write nothing, count what a night would do. */
22
+ readonly apply: boolean;
23
+ /** Fades a night at most. */
24
+ readonly budget: number;
25
+ }
26
+
27
+ /** One fade: the older memory, the newer one it now sits under, and the rule that matched. Ids only. */
28
+ export interface InterferedRecord {
29
+ readonly over: string;
30
+ readonly holds: string;
31
+ readonly rule: string;
32
+ }
33
+
34
+ export interface InterfereSweepOutcome {
35
+ /** False when the sweep is switched off. */
36
+ readonly on: boolean;
37
+ readonly examined: number;
38
+ readonly faded: readonly InterferedRecord[];
39
+ /** Why each older memory kept its slow curve, by reason. */
40
+ readonly skipped: Readonly<Record<string, number>>;
41
+ readonly skippedForLimit: number;
42
+ }
43
+
44
+ export type InterfereFn = (input: InterfereInput) => InterfereSweepOutcome;
45
+
46
+ export interface InterfereResult extends PhaseOutcome {
47
+ readonly interfered: readonly InterferedRecord[];
48
+ }
49
+
50
+ export function runInterfere(ctx: PhaseCtx, interfere: InterfereFn | undefined): InterfereResult {
51
+ const out = emptyOutcome();
52
+ if (interfere === undefined) {
53
+ out.skipped["no-interfere-fn"] = 1;
54
+ return { ...out, interfered: [] };
55
+ }
56
+ const r = interfere({ day: ctx.day, apply: ctx.apply, budget: ctx.budget });
57
+ if (!r.on) {
58
+ countSkip(out, "switched-off", 1);
59
+ return { ...out, interfered: [] };
60
+ }
61
+ for (const [reason, n] of Object.entries(r.skipped)) countSkip(out, reason, n);
62
+ out.examined = r.examined;
63
+ out.changed = r.faded.length;
64
+ out.budgetExhausted = r.skippedForLimit > 0;
65
+ out.skippedForBudget = r.skippedForLimit;
66
+ ctx.event("sleep.interfere.sweep", undefined, {
67
+ day: ctx.day,
68
+ examined: r.examined,
69
+ faded: r.faded.length,
70
+ skippedForLimit: r.skippedForLimit,
71
+ });
72
+ return { ...out, interfered: r.faded.map((f) => ({ over: f.over, holds: f.holds, rule: f.rule })) };
73
+ }
@@ -90,11 +90,19 @@ export function runLogSweep(ctx: PhaseCtx): LogSweepResult {
90
90
  }
91
91
 
92
92
  const report = store.pruneEvents({ limit: ctx.budget });
93
+ // The per-turn table goes on the same short window (FD1 step 2, R1). Not
94
+ // capped: one indexed delete, and its rows are a turn's ids and a count.
95
+ let recallTurns = 0;
96
+ try {
97
+ recallTurns = store.pruneRecallTurns?.() ?? 0;
98
+ } catch {
99
+ /* the log's sweep stands; the turns wait for tomorrow */
100
+ }
93
101
  out.examined = report.eligible + keptLatched;
94
102
  out.changed = report.pruned;
95
103
  out.skippedForBudget = report.remaining;
96
104
  out.budgetExhausted = report.remaining > 0;
97
- ctx.event(LOG_SWEEP_EVENT, undefined, {
105
+ const swept = {
98
106
  day: ctx.day,
99
107
  count: report.pruned,
100
108
  eligible: report.eligible,
@@ -103,7 +111,24 @@ export function runLogSweep(ctx: PhaseCtx): LogSweepResult {
103
111
  budget: ctx.budget,
104
112
  cutoffDay: report.cutoffDay,
105
113
  retentionDays: report.retentionDays,
106
- });
114
+ shortCutoffDay: report.shortCutoffDay,
115
+ shortRetentionDays: report.shortRetentionDays,
116
+ rolled: report.rolled,
117
+ recallTurns,
118
+ };
119
+ ctx.event(LOG_SWEEP_EVENT, undefined, swept);
120
+ // DURABLE TOO (FD1 step 2): one row a lived day, so doctor's Growth line for
121
+ // the log can say what left beside what came in. Unlatched: it ages out at
122
+ // the long window like any row; a pass that deleted and rolled nothing
123
+ // writes none, so a clean window stays a no-op. A failed write costs only
124
+ // the reading.
125
+ if (report.pruned > 0 || report.rolled > 0 || recallTurns > 0) {
126
+ try {
127
+ store.appendEvent?.({ name: LOG_SWEEP_EVENT, day: ctx.day, payload: swept });
128
+ } catch {
129
+ /* the sweep stands; only its record is missing */
130
+ }
131
+ }
107
132
  return {
108
133
  ...out,
109
134
  eligible: report.eligible,
@@ -77,6 +77,21 @@ export const TUNABLES = {
77
77
  * new links are a few dozen.
78
78
  */
79
79
  tags: 500,
80
+ /**
81
+ * Routine work memories faded under a newer one a night at most
82
+ * (2026-11-07, FD1 step 3). CAL, chosen not measured: a store with weeks
83
+ * of status notes takes its backlog a few nights at a time, so the first
84
+ * nights' fades stay readable beside the age prunes; a night's own new
85
+ * events are a few dozen.
86
+ */
87
+ interfere: 200,
88
+ /**
89
+ * Open items the night closes a night at most (2026-11-07, SO1 step 1).
90
+ * CAL, chosen not measured: a store's first night takes its backlog of
91
+ * release steps at once (a handful on the owner's copy); a night's own
92
+ * are one or two.
93
+ */
94
+ close: 50,
80
95
  dedup: 1_000,
81
96
  versions: 1,
82
97
  briefing: 1,
@@ -110,6 +125,10 @@ export const TUNABLES = {
110
125
  births: 1,
111
126
  /** Every lived day: card names, stems and dead merges are a few reads. */
112
127
  tags: 1,
128
+ /** Every lived day: one read of the live work memories. */
129
+ interfere: 1,
130
+ /** Every lived day: one read of the open items and the work memories. */
131
+ close: 1,
113
132
  dedup: 1,
114
133
  versions: 1,
115
134
  briefing: 1,
@@ -18,6 +18,8 @@
18
18
 
19
19
  import { SELF_PAGE_ROLE_SPELLED, isChapterSummary } from "../store/operational.js";
20
20
  import type { Band, Kind, MemoryPhysics } from "../types.js";
21
+ import type { InterferedRecord } from "./interfere.js";
22
+ import type { ClosedRecord } from "./close.js";
21
23
  import type {
22
24
  EventLogCensus,
23
25
  EventPruneReport,
@@ -158,6 +160,8 @@ export interface SleepStore {
158
160
  * and it never crosses the write seam.
159
161
  */
160
162
  pruneEvents?(opts?: { limit?: number }): EventPruneReport;
163
+ /** FD1 step 2: recall turns past the per-turn rows' short window, deleted; the count. */
164
+ pruneRecallTurns?(): number;
161
165
  eventLogCensus?(): EventLogCensus;
162
166
  /**
163
167
  * The CORE's history (schema v8, 2026-09-26), OPTIONAL like the log. Where
@@ -231,6 +235,8 @@ export const PHASES = [
231
235
  "fade",
232
236
  "births",
233
237
  "tags",
238
+ "interfere",
239
+ "close",
234
240
  "dedup",
235
241
  "versions",
236
242
  "briefing",
@@ -252,6 +258,8 @@ export type PhaseReason =
252
258
  | "no-fade-fn"
253
259
  | "no-births-fn"
254
260
  | "no-tags-fn"
261
+ | "no-interfere-fn"
262
+ | "no-close-fn"
255
263
  | "no-durable-event-log"
256
264
  | "failed";
257
265
 
@@ -537,6 +545,18 @@ export interface CycleReport {
537
545
  readonly merged: readonly MergeRecord[];
538
546
  /** Entity cards the fade phase archived this cycle (or, under observer, would have). Ids only. */
539
547
  readonly faded: readonly string[];
548
+ /**
549
+ * Routine work memories the interference phase faded under a newer one this
550
+ * cycle (or, under observer, would have): ids only (2026-11-07, FD1 step 3).
551
+ * Not exits — each stays live, faded, and leaves later through the prune.
552
+ */
553
+ readonly interfered: readonly InterferedRecord[];
554
+ /**
555
+ * Open items the close phase closed this cycle (or, under observer, would
556
+ * have): ids, the memory that showed each done, the rule (2026-11-07, SO1
557
+ * step 1). Not exits: each stays live, its flag false.
558
+ */
559
+ readonly closed: readonly ClosedRecord[];
540
560
  /** Band crossings THIS cycle, by direction (guarantee 12's input). */
541
561
  readonly bandTransitions: readonly BandTransition[];
542
562
  /**