counterparts 0.3.2 → 0.3.4

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 (153) hide show
  1. package/CHANGELOG.md +153 -0
  2. package/docs/module-map.md +3 -2
  3. package/package.json +1 -1
  4. package/src/adapters/claude-code/NOTES.md +28 -0
  5. package/src/adapters/claude-code/bin/hook.ts +85 -22
  6. package/src/adapters/claude-code/config.ts +7 -0
  7. package/src/adapters/claude-code/doctor.ts +166 -4
  8. package/src/adapters/claude-code/hooks.ts +185 -6
  9. package/src/adapters/claude-code/index.ts +3 -0
  10. package/src/adapters/claude-code/transcript.ts +7 -0
  11. package/src/adapters/cli/CONTRACT.md +11 -0
  12. package/src/adapters/cli/INTERFACE-GAPS.md +16 -0
  13. package/src/adapters/cli/NOTES.md +25 -0
  14. package/src/adapters/cli/commands.ts +184 -4
  15. package/src/adapters/cli/dream-core.ts +126 -0
  16. package/src/adapters/cli/help.ts +17 -0
  17. package/src/adapters/cli/mechanisms.ts +199 -106
  18. package/src/adapters/dashboard/CONTRACT.md +5 -0
  19. package/src/adapters/dashboard/INTERFACE-GAPS.md +12 -0
  20. package/src/adapters/dashboard/NOTES.md +38 -0
  21. package/src/adapters/dashboard/registries.ts +35 -1
  22. package/src/adapters/dashboard/web/README.md +63 -26
  23. package/src/adapters/dashboard/web/app.html +2 -0
  24. package/src/adapters/dashboard/web/flow.ts +14 -0
  25. package/src/adapters/dashboard/web/lanes.ts +141 -0
  26. package/src/adapters/dashboard/web/mechanisms/association/index.js +1 -2
  27. package/src/adapters/dashboard/web/mechanisms/consolidation/index.js +8 -5
  28. package/src/adapters/dashboard/web/mechanisms/consolidation/panel.js +7 -7
  29. package/src/adapters/dashboard/web/mechanisms/decay/index.js +1 -2
  30. package/src/adapters/dashboard/web/mechanisms/dreaming/index.js +19 -0
  31. package/src/adapters/dashboard/web/mechanisms/emotional/index.js +9 -6
  32. package/src/adapters/dashboard/web/mechanisms/episodic-semantic/index.js +4 -5
  33. package/src/adapters/dashboard/web/mechanisms/index.js +5 -3
  34. package/src/adapters/dashboard/web/mechanisms/interference/index.js +1 -2
  35. package/src/adapters/dashboard/web/mechanisms/prospective/index.js +7 -5
  36. package/src/adapters/dashboard/web/mechanisms/reconsolidation/index.js +1 -2
  37. package/src/adapters/dashboard/web/mechanisms/regions.js +1 -1
  38. package/src/adapters/dashboard/web/mechanisms/retrieval/index.js +1 -2
  39. package/src/adapters/dashboard/web/mechanisms/salience/index.js +1 -2
  40. package/src/adapters/dashboard/web/mechanisms/schema/index.js +1 -2
  41. package/src/adapters/dashboard/web/narrate.ts +76 -13
  42. package/src/adapters/dashboard/web/pages/health/sections/checks.js +17 -1
  43. package/src/adapters/dashboard/web/pages/home/home.css +29 -13
  44. package/src/adapters/dashboard/web/pages/home/index.js +2 -0
  45. package/src/adapters/dashboard/web/pages/home/sections/explorer.js +22 -7
  46. package/src/adapters/dashboard/web/pages/home/sections/hero.js +15 -3
  47. package/src/adapters/dashboard/web/pages/home/sections/live-activity.js +15 -9
  48. package/src/adapters/dashboard/web/pages/home/sections/recent-chapters.js +6 -2
  49. package/src/adapters/dashboard/web/pages/home/sections/tiles.js +35 -18
  50. package/src/adapters/dashboard/web/pages/memories/index.js +30 -36
  51. package/src/adapters/dashboard/web/pages/memories/memories.css +97 -72
  52. package/src/adapters/dashboard/web/pages/memories/row.js +51 -0
  53. package/src/adapters/dashboard/web/pages/memories/sections/feel.js +176 -0
  54. package/src/adapters/dashboard/web/pages/memories/sections/hold.js +67 -0
  55. package/src/adapters/dashboard/web/pages/memories/sections/list.js +65 -43
  56. package/src/adapters/dashboard/web/pages/memories/sections/search.js +26 -23
  57. package/src/adapters/dashboard/web/pages/memories/state.js +12 -5
  58. package/src/adapters/dashboard/web/pages/self/index.js +57 -16
  59. package/src/adapters/dashboard/web/pages/self/sections/dreams.js +76 -0
  60. package/src/adapters/dashboard/web/pages/self/sections/journal.js +75 -19
  61. package/src/adapters/dashboard/web/pages/self/sections/page.js +108 -65
  62. package/src/adapters/dashboard/web/pages/self/sections/settling.js +129 -104
  63. package/src/adapters/dashboard/web/pages/self/sections/wake.js +31 -15
  64. package/src/adapters/dashboard/web/pages/self/self.css +122 -59
  65. package/src/adapters/dashboard/web/pages/self/state.js +21 -0
  66. package/src/adapters/dashboard/web/server.ts +9 -0
  67. package/src/adapters/dashboard/web/shared/memory-marks.css +17 -0
  68. package/src/adapters/dashboard/web/shared/memory-marks.js +81 -0
  69. package/src/adapters/dashboard/web/shared/memory-modal.js +240 -53
  70. package/src/adapters/dashboard/web/shared/modal.css +67 -0
  71. package/src/adapters/dashboard/web/shared/tip.js +1 -1
  72. package/src/adapters/dashboard/web/shared/widgets/feed.css +4 -0
  73. package/src/adapters/dashboard/web/shared/widgets/feed.js +56 -14
  74. package/src/adapters/dashboard/web/shared/widgets/light.css +1 -0
  75. package/src/adapters/dashboard/web/shared/widgets/light.js +7 -5
  76. package/src/adapters/dashboard/web/shared/widgets/tips.css +19 -0
  77. package/src/adapters/dashboard/web/shared/widgets/tips.js +56 -0
  78. package/src/adapters/dashboard/web/views/activity.ts +18 -2
  79. package/src/adapters/dashboard/web/views/archive-words.ts +27 -10
  80. package/src/adapters/dashboard/web/views/dreams.ts +199 -0
  81. package/src/adapters/dashboard/web/views/mechanism-panel.ts +116 -35
  82. package/src/adapters/dashboard/web/views/mechanisms.ts +81 -255
  83. package/src/adapters/dashboard/web/views/memories.ts +245 -43
  84. package/src/adapters/dashboard/web/views/memory-words.ts +133 -0
  85. package/src/adapters/dashboard/web/views/memory.ts +226 -4
  86. package/src/adapters/dashboard/web/views/mind.ts +261 -61
  87. package/src/adapters/dashboard/web/views/overview.ts +80 -42
  88. package/src/adapters/dashboard/web/views/search.ts +35 -1
  89. package/src/adapters/dashboard/web/views/shared.ts +14 -0
  90. package/src/adapters/dashboard/web/views.ts +2 -0
  91. package/src/adapters/feelings-line.ts +74 -0
  92. package/src/adapters/fired.ts +163 -23
  93. package/src/adapters/mcp/CONTRACT.md +8 -0
  94. package/src/adapters/mcp/NOTES.md +56 -0
  95. package/src/adapters/mcp/server.ts +254 -6
  96. package/src/adapters/mcp/tools.ts +180 -5
  97. package/src/adapters/mechanism-evidence.ts +465 -0
  98. package/src/core/bridge.ts +21 -16
  99. package/src/core/counterpart.ts +422 -10
  100. package/src/core/dream/CONTRACT.md +143 -0
  101. package/src/core/dream/INTERFACE-GAPS.md +24 -0
  102. package/src/core/dream/NOTES.md +84 -0
  103. package/src/core/dream/index.ts +1067 -0
  104. package/src/core/dream/mark.ts +14 -0
  105. package/src/core/dream/tunables.ts +56 -0
  106. package/src/core/encode/CONTRACT.md +7 -1
  107. package/src/core/feelings-wheel.ts +171 -13
  108. package/src/core/mint.ts +9 -0
  109. package/src/core/physics/CONTRACT.md +132 -21
  110. package/src/core/physics/NOTES.md +233 -0
  111. package/src/core/physics/index.ts +419 -50
  112. package/src/core/prospective/CONTRACT.md +35 -6
  113. package/src/core/prospective/INTERFACE-GAPS.md +10 -5
  114. package/src/core/prospective/NOTES.md +175 -4
  115. package/src/core/prospective/derive.ts +32 -3
  116. package/src/core/prospective/index.ts +362 -39
  117. package/src/core/prospective/tunables.ts +18 -1
  118. package/src/core/prospective/windows.ts +104 -46
  119. package/src/core/recall/CONTRACT.md +28 -0
  120. package/src/core/recall/NOTES.md +55 -0
  121. package/src/core/recall/activate.ts +32 -5
  122. package/src/core/recall/gate.ts +7 -1
  123. package/src/core/recall/index.ts +28 -2
  124. package/src/core/recall/mood.ts +106 -0
  125. package/src/core/recall/tunables.ts +26 -0
  126. package/src/core/remember/CONTRACT.md +5 -0
  127. package/src/core/remember/index.ts +1 -0
  128. package/src/core/remember/proposals.ts +86 -3
  129. package/src/core/remember/spans.ts +11 -1
  130. package/src/core/self/CONTRACT.md +3 -0
  131. package/src/core/self/INTERFACE-GAPS.md +7 -0
  132. package/src/core/self/NOTES.md +30 -0
  133. package/src/core/self/identity.ts +115 -6
  134. package/src/core/self/index.ts +17 -0
  135. package/src/core/self/tunables.ts +17 -0
  136. package/src/core/sleep/CONTRACT.md +17 -1
  137. package/src/core/sleep/INTERFACE-GAPS.md +19 -0
  138. package/src/core/sleep/NOTES.md +36 -0
  139. package/src/core/sleep/consolidate.ts +149 -96
  140. package/src/core/sleep/decay.ts +11 -0
  141. package/src/core/sleep/index.ts +4 -1
  142. package/src/core/sleep/tunables.ts +9 -0
  143. package/src/core/sleep/types.ts +17 -0
  144. package/src/core/sleep/upgrade.ts +168 -0
  145. package/src/core/store/CONTRACT.md +21 -2
  146. package/src/core/store/NOTES.md +68 -0
  147. package/src/core/store/feelings.ts +23 -6
  148. package/src/core/store/index.ts +716 -17
  149. package/src/core/store/operational.ts +329 -4
  150. package/src/core/store/owner-op-seam.ts +79 -1
  151. package/src/core/types.ts +37 -0
  152. package/src/adapters/dashboard/web/pages/memories/sections/constellation.js +0 -178
  153. package/src/adapters/dashboard/web/pages/memories/sections/kinds.js +0 -42
@@ -34,11 +34,12 @@ import type { Kind } from "../types.js";
34
34
  import { strength } from "../physics/index.js";
35
35
  import type { UseTier } from "../physics/index.js";
36
36
  import type { ProseDoc, ProspectiveInput, ProspectiveRow, Store } from "../store/index.js";
37
+ import { addDays, isDay } from "../time.js";
37
38
  import { derive } from "./derive.js";
38
39
  import type { DerivableMemory, DeriveReason, ExtractedDate, Prospectivity } from "./derive.js";
39
40
  import { TEMPORAL_MAX_TIER, withTunables } from "./tunables.js";
40
41
  import type { ProspectiveTunables } from "./tunables.js";
41
- import { parseWindowKey, phaseOf, precisionOf, rampAt, windowFor } from "./windows.js";
42
+ import { imminence, parseWindowKey, phaseOf, precisionOf, rampAt, windowFor } from "./windows.js";
42
43
  import type { DatePrecision, Phase, Window, WindowPrecision } from "./windows.js";
43
44
 
44
45
  export * from "./derive.js";
@@ -83,6 +84,51 @@ export type WindowState = ProspectiveInput["state"];
83
84
  export const PROSPECTIVE_FIRE_EVENT = "prospective.fire";
84
85
  export const PROSPECTIVE_REFUSED_EVENT = "prospective.fire.refused";
85
86
 
87
+ /**
88
+ * THE THIRD DURABLE ROW (2026-09-26): a PLAIN item was told to the person —
89
+ * one row per memory, window and beat, latched by `dedupKey`, so it doubles as
90
+ * the once-only mark two hook processes race for (`claimPlain`). Ids, the
91
+ * window key, the beat and today's calendar date; never a word of the memory.
92
+ */
93
+ export const PROSPECTIVE_PLAIN_EVENT = "prospective.plain";
94
+
95
+ /**
96
+ * PLAIN OR QUIET (owner decision 2026-09-25/26, a working default). The author
97
+ * of a dated memory says which, on the same `note` / `session_end` entry that
98
+ * carries the date, and it lives in the memory's `meta` bag under this key — no
99
+ * column, no schema bump. Anything but `"plain"` reads as quiet, which is the
100
+ * default and the whole of the tact principle: a quiet item is one more cue,
101
+ * capped at the footnote tier. A plain one is also SAID, plainly, on its day —
102
+ * the one deliberate exception to "no bypass lane", recorded in CONTRACT §3.
103
+ */
104
+ export type CueMode = "plain" | "quiet";
105
+ export const CUE_MODE_META = "remind";
106
+
107
+ export function cueModeOf(doc: Pick<ProseDoc, "meta">): CueMode {
108
+ return doc.meta[CUE_MODE_META] === "plain" ? "plain" : "quiet";
109
+ }
110
+
111
+ /**
112
+ * A REMINDER THAT MOVED (review of #247, 2026-09-26). When a revision takes a
113
+ * dated memory's reminder over (`Counterpart#carryReminder`), the two memories
114
+ * point at each other in their meta bags, ids only:
115
+ *
116
+ * - the successor's `reminderFrom` names the memory it took the reminder
117
+ * from, so what was already said or spent for the SAME window (the plain
118
+ * latch, the quiet fire budget, a referenced stop) is still counted — a
119
+ * move is not a new reminder (`lineage`);
120
+ * - the old memory's `reminderMovedTo` names where it went, so a later
121
+ * revision that addresses the OLD id (which stays live: revising an
122
+ * ordinary memory is link-only) still finds the reminder to reschedule or
123
+ * drop, instead of finding nothing.
124
+ */
125
+ export const DATE_FROM_META = "reminderFrom";
126
+ export const DATE_MOVED_TO_META = "reminderMovedTo";
127
+
128
+ /** How far either pointer is followed. A reminder revised this many times in a
129
+ * row is still found; a longer chain answers as if it ended there. */
130
+ export const DATE_LINEAGE_MAX = 16;
131
+
86
132
  const WINDOW_STATES: readonly WindowState[] = ["armed", "fired", "suppressed", "expired"];
87
133
 
88
134
  /** Box 2 hands back `state` as TEXT. An unrecognized value reads as `armed` — the
@@ -109,6 +155,13 @@ export type SuppressReason =
109
155
  | "over-fired"
110
156
  /** Brake 1: at most one ambient fire per occasion per LIVED day. */
111
157
  | "already-fired-today"
158
+ /** Tune question (b), 2026-09-26: a month or range window's LAST fire is kept
159
+ * for after the span it names, so it gets an "after" beat instead of spending
160
+ * its whole budget in the first days (`HOLD_LAST_FIRE_FOR_AFTER`). */
161
+ | "held-for-after"
162
+ /** A PLAIN item already told outright today (`claimPlain`): the same thing
163
+ * as a footnote on the next turn would be saying it twice (2026-09-26). */
164
+ | "told-plainly-today"
112
165
  /** Brake 3: already offered in this session. */
113
166
  | "session-dedup";
114
167
 
@@ -186,6 +239,33 @@ export interface Arrival {
186
239
  * arrival (§12 G10): a future-dated memory that faded before its window was
187
240
  * an occasion that didn't matter. */
188
241
  readonly strength: number;
242
+ /** Plain or quiet, as the author said (`CUE_MODE_META`). Carried so the fire
243
+ * row can count the two apart; it changes nothing about the cue itself. */
244
+ readonly mode: CueMode;
245
+ }
246
+
247
+ /** Which moment of a plain item's window it is being told on. */
248
+ export type PlainBeat =
249
+ /** A day-dated item, on its day. */
250
+ | "day"
251
+ /** A month or range item, the first day it is open and seen. */
252
+ | "opens"
253
+ /** A month or range item, on the last day it names. */
254
+ | "last-day";
255
+
256
+ /**
257
+ * A plain item due to be told today. Records only — like `Arrival`, no words:
258
+ * the adapter that talks to a person owns those (`adapters/claude-code`).
259
+ */
260
+ export interface PlainDue {
261
+ readonly memoryId: string;
262
+ readonly windowKey: string;
263
+ readonly eventDate: string;
264
+ readonly precision: WindowPrecision;
265
+ readonly beat: PlainBeat;
266
+ /** The span the date names, `YYYY-MM-DD` both. */
267
+ readonly firstDay: string;
268
+ readonly lastDay: string;
189
269
  }
190
270
 
191
271
  export interface SuppressionRecord {
@@ -309,29 +389,37 @@ export interface ExitReport {
309
389
  readonly exits: Exit[];
310
390
  }
311
391
 
312
- /** Where a memory's own content-dates live on the prose payload. */
313
- export const EVENT_DATE_META = "eventDate";
314
- export const EVENT_DATES_META = "eventDates";
315
-
316
392
  /**
317
- * A memory's content-dates: `happenedOn` (stated precision, never rounded) plus
318
- * the `eventDate` / `eventDates` meta convention this module declares. SHAPE
319
- * ONLY — nothing here reads prose for dates. That is the caller's job, and its
320
- * output arrives through `extraDates` (INTERFACE-GAPS #2).
393
+ * A memory's content-date: its reminder date, `ProseDoc.eventDate` (schema v7,
394
+ * the `event_date` column), exactly as stated. SHAPE ONLY — nothing here reads
395
+ * prose for dates; the model writes the field (INTERFACE-GAPS #2, closed
396
+ * 2026-09-26).
397
+ *
398
+ * Two sources this used to read are gone on purpose. `happenedOn` is a PAST
399
+ * date by name, and `schemas/` writes every belief's `statedOn` into it, so
400
+ * reading it as a future occasion was a category error waiting for data. The
401
+ * `meta.eventDate` / `meta.eventDates` convention was this module's own
402
+ * stand-in for the column, and nothing ever wrote it; keeping it would have
403
+ * meant a second enumeration (a scan of every memory) to find what the index
404
+ * now answers. Caller-extracted dates still arrive through `extraDates`.
321
405
  */
322
- export function contentDates(doc: ProseDoc): ExtractedDate[] {
323
- const out: ExtractedDate[] = [];
324
- const push = (v: unknown): void => {
325
- if (typeof v === "string" && v !== "") out.push({ date: v });
326
- };
327
- push(doc.happenedOn);
328
- push(doc.meta[EVENT_DATE_META]);
329
- const many = doc.meta[EVENT_DATES_META];
330
- if (Array.isArray(many)) for (const v of many) push(v);
331
- const seen = new Set<string>();
332
- return out.filter((d) => (seen.has(d.date) ? false : (seen.add(d.date), true)));
406
+ export function contentDates(doc: Pick<ProseDoc, "eventDate">): ExtractedDate[] {
407
+ const v = doc.eventDate;
408
+ return typeof v === "string" && v !== "" ? [{ date: v }] : [];
409
+ }
410
+
411
+ /** Every memory whose stated date could have an OPEN window on `at`: the span
412
+ * must reach from `at - GRACE_DAYS` to `at + LEAD_DAYS`. */
413
+ function candidateSpan(at: string, t: ProspectiveTunables): { from: string; to: string } | null {
414
+ if (!isDay(at)) return null;
415
+ return { from: addDays(at, -t.GRACE_DAYS), to: addDays(at, t.LEAD_DAYS) };
333
416
  }
334
417
 
418
+ /** Score, then imminence, then key — tune question (d). Scores closer than
419
+ * this are a TIE: two salience means 0.7 and 0.7 must not be split by a float
420
+ * rounding in the ramp. */
421
+ const TIE = 1e-9;
422
+
335
423
  interface Loaded {
336
424
  memory: DerivableMemory;
337
425
  doc: ProseDoc;
@@ -410,8 +498,17 @@ export class Prospective {
410
498
  const suppressed: SuppressionRecord[] = [];
411
499
  const refused: { memoryId: string; reason: DeriveReason }[] = [];
412
500
  const denied = new Set(this.store.deniedIds());
413
- const ids = this.store.list({ archived: false });
501
+ // THE INDEX, not a scan (INTERFACE-GAPS #2, closed 2026-09-26): only a
502
+ // memory whose stated date reaches `at` through lead or grace can have an
503
+ // open window, and `Store.datedMemories` answers exactly that. The ids a
504
+ // caller supplied dates for ride along — the index cannot know about them.
505
+ const span = candidateSpan(input.at, this.tunables);
506
+ const ids = new Set<string>(
507
+ span === null ? [] : this.store.datedMemories(span.from, span.to).map((d) => d.id),
508
+ );
509
+ for (const id of input.extraDates?.keys() ?? []) ids.add(id);
414
510
  let considered = 0;
511
+ const order = new Map<string, readonly [number, number]>();
415
512
 
416
513
  for (const id of ids) {
417
514
  if (denied.has(id)) continue;
@@ -431,15 +528,19 @@ export class Prospective {
431
528
  continue;
432
529
  }
433
530
 
434
- const rows = this.rowsFor(id);
531
+ const rows = this.firingRowsFor(id);
532
+ const mode = cueModeOf(loaded.doc);
533
+ // Read only for a plain item, and only once per memory per call.
534
+ const toldToday = mode === "plain" && this.plainToldOn(id, input.at);
435
535
  for (const w of p.windows) {
436
536
  const stateRow = rows.get(w.key);
437
- const brake = this.brakeFor(w, stateRow, {
438
- at: input.at,
439
- day,
440
- refractory: input.refractory === true,
441
- sessionId: input.sessionId,
442
- });
537
+ const brake =
538
+ this.brakeFor(w, stateRow, {
539
+ at: input.at,
540
+ day,
541
+ refractory: input.refractory === true,
542
+ sessionId: input.sessionId,
543
+ }) ?? (toldToday ? "told-plainly-today" : null);
443
544
  if (brake !== null) {
444
545
  suppressed.push({ memoryId: id, windowKey: w.key, reason: brake });
445
546
  continue;
@@ -460,14 +561,26 @@ export class Prospective {
460
561
  fires: stateRow?.fires ?? 0,
461
562
  lastFiredDay: stateRow?.last_fired_day ?? null,
462
563
  strength: loaded.strength,
564
+ mode,
463
565
  });
566
+ order.set(`${id} ${w.key}`, imminence(w, input.at));
464
567
  }
465
568
  }
466
569
 
467
- arrivals.sort(
468
- (a, b) =>
469
- b.ramp * b.strength - a.ramp * a.strength || (a.windowKey < b.windowKey ? -1 : 1),
470
- );
570
+ // Salience-weighted ramp first; a TIE goes to the more imminent window —
571
+ // sooner, then narrower — so "today" beats "sometime this month" at equal
572
+ // salience (tune question d); then the key, so the order is total.
573
+ const soon = (a: Arrival): readonly [number, number] =>
574
+ order.get(`${a.memoryId} ${a.windowKey}`) ?? [0, 0];
575
+ arrivals.sort((a, b) => {
576
+ const d = b.ramp * b.strength - a.ramp * a.strength;
577
+ if (Math.abs(d) > TIE) return d;
578
+ const [au, aw] = soon(a);
579
+ const [bu, bw] = soon(b);
580
+ if (au !== bu) return au - bu;
581
+ if (aw !== bw) return aw - bw;
582
+ return a.windowKey < b.windowKey ? -1 : a.windowKey > b.windowKey ? 1 : a.memoryId < b.memoryId ? -1 : 1;
583
+ });
471
584
 
472
585
  for (const s of suppressed) {
473
586
  // A window that has not opened yet was NEVER ASKED, which is a different
@@ -513,7 +626,14 @@ export class Prospective {
513
626
  considered,
514
627
  };
515
628
  }
516
- const items = considered.arrivals.slice(0, this.tunables.HORIZON_ITEMS);
629
+ // Tune question (c), 2026-09-26 — the owner's human-memory framing: people
630
+ // date-fire DAY-precision things (trash day, a defense tomorrow). A month
631
+ // or a range lives as footnote warmth and context corroboration only; it
632
+ // still arrives as a cue every day of its window, it just never takes a
633
+ // wake line.
634
+ const items = considered.arrivals
635
+ .filter((a) => a.precision === "day")
636
+ .slice(0, this.tunables.HORIZON_ITEMS);
517
637
  const reason: HorizonReason = items.length === 0 ? "nothing-arrived" : "selected";
518
638
  this.emit("prospective.horizon", undefined, {
519
639
  reason,
@@ -524,6 +644,174 @@ export class Prospective {
524
644
  return { items, reason, suppressedWholesale: false, considered };
525
645
  }
526
646
 
647
+ // ── plain items: the one exception to "a cue, not a command" ──────────────
648
+
649
+ /**
650
+ * PLAIN items due to be told today, not yet told (owner decision 2026-09-25/26,
651
+ * a working default — CONTRACT §3 names it as the deliberate exception to "no
652
+ * bypass lane"). A person who said "don't let me forget to pay taxes before Oct
653
+ * 15th!" asked to be told, and a footnote the model may or may not raise is not
654
+ * telling them.
655
+ *
656
+ * SPARING, by construction — at most two beats per window, each once ever:
657
+ *
658
+ * - a DAY item on its day (`day`);
659
+ * - a MONTH or RANGE item the first day it is open and seen (`opens`), and
660
+ * again on the last day it names (`last-day`) — the deadline end of
661
+ * "before the 15th". Seen first ON its last day, it gets that beat only.
662
+ *
663
+ * Only the stated span counts — no lead days, no grace: "on the day" is what
664
+ * was asked. A YEAR has no day it could mean, so it is never told (the same
665
+ * structural exclusion as a window, §12 G3).
666
+ *
667
+ * It does NOT run the eligibility predicate. The salience floor is the tact
668
+ * principle's gate on UNASKED surfacing; this is asked-for, and an ordinary
669
+ * note sits at the authored default (0.25) under a 0.6 floor, so gating here
670
+ * would make a plain reminder dead for most notes. What still refuses: an
671
+ * archived, superseded, removed or journal row, and an observer.
672
+ *
673
+ * A read — `claimPlain` is the write, and the caller shows a line only for a
674
+ * claim that landed. Records only; the words are the adapter's.
675
+ */
676
+ plainDue(input: { at: string }): PlainDue[] {
677
+ if (this.observer || !isDay(input.at)) return [];
678
+ const at = input.at;
679
+ const denied = new Set(this.store.deniedIds());
680
+ const out: PlainDue[] = [];
681
+ for (const dated of this.store.datedMemories(at, at)) {
682
+ const id = dated.id;
683
+ if (denied.has(id)) continue;
684
+ const row = this.store.row(id);
685
+ if (row === undefined || row.superseded_by !== null) continue;
686
+ let doc: ProseDoc;
687
+ try {
688
+ doc = this.store.read(id).doc;
689
+ } catch {
690
+ continue;
691
+ }
692
+ if (doc.type === "episode" || cueModeOf(doc) !== "plain") continue;
693
+ const precision = precisionOf(dated.eventDate);
694
+ if (precision === null || precision === "year") continue;
695
+ const w = windowFor(dated.eventDate, precision, this.tunables, id);
696
+ if (w === null || at < w.firstDay || at > w.lastDay) continue;
697
+ const beat: PlainBeat =
698
+ w.precision === "day" ? "day" : at === w.lastDay ? "last-day" : "opens";
699
+ if (this.plainTold(id, w.key, beat)) continue;
700
+ out.push({
701
+ memoryId: id,
702
+ windowKey: w.key,
703
+ eventDate: w.eventDate,
704
+ precision: w.precision,
705
+ beat,
706
+ firstDay: w.firstDay,
707
+ lastDay: w.lastDay,
708
+ });
709
+ }
710
+ return out;
711
+ }
712
+
713
+ /**
714
+ * Mark one plain beat as told. TRUE only when this call wrote the mark: the
715
+ * `dedupKey` latch is the whole lock, so two hook processes racing for the
716
+ * same beat cannot both win, and the caller shows the line only on true —
717
+ * the update notice's "mark first, show only if marked" (roadmap E). An
718
+ * observer marks nothing and is therefore told nothing.
719
+ */
720
+ claimPlain(due: PlainDue, opts: { at: string; day?: number }): boolean {
721
+ if (this.observer) return false;
722
+ const day = opts.day ?? this.store.livedDay();
723
+ try {
724
+ const seq = this.store.appendEvent({
725
+ name: PROSPECTIVE_PLAIN_EVENT,
726
+ day,
727
+ ref: due.memoryId,
728
+ payload: { window: due.windowKey, beat: due.beat, precision: due.precision, date: opts.at, mode: "plain" },
729
+ dedupKey: `${PROSPECTIVE_PLAIN_EVENT}:${due.memoryId}:${due.windowKey}:${due.beat}`,
730
+ });
731
+ if (seq > 0) {
732
+ this.emit("prospective.plain", due.memoryId, { window: due.windowKey, beat: due.beat, day });
733
+ }
734
+ return seq > 0;
735
+ } catch {
736
+ // A lost mark costs this line, never the turn (§12 G12's budget).
737
+ return false;
738
+ }
739
+ }
740
+
741
+ /** Has this beat already been told? A read of the latch `claimPlain` writes,
742
+ * so a turn with nothing new to say takes no write lock at all. */
743
+ private plainTold(memoryId: string, windowKey: string, beat: PlainBeat): boolean {
744
+ // The memories this reminder moved through count too: a beat told on the
745
+ // row a revision took the reminder from was TOLD (`DATE_FROM_META`).
746
+ for (const id of this.lineage(memoryId)) {
747
+ for (const e of this.plainRows(id)) {
748
+ if (e.dedup_key === `${PROSPECTIVE_PLAIN_EVENT}:${id}:${windowKey}:${beat}`) return true;
749
+ }
750
+ }
751
+ return false;
752
+ }
753
+
754
+ /** Was this memory — or one its reminder moved from — told plainly on the calendar day `at`? */
755
+ private plainToldOn(memoryId: string, at: string): boolean {
756
+ for (const id of this.lineage(memoryId)) {
757
+ for (const e of this.plainRows(id)) {
758
+ try {
759
+ if ((JSON.parse(e.payload ?? "{}") as { date?: unknown }).date === at) return true;
760
+ } catch {
761
+ /* an unreadable row answers nothing */
762
+ }
763
+ }
764
+ }
765
+ return false;
766
+ }
767
+
768
+ /**
769
+ * The memory, then each one its reminder moved from, newest first — read off
770
+ * `meta.reminderFrom`, bounded, cycle-safe. A row that is gone or whose meta
771
+ * will not parse ends the walk: losing it costs at most one extra mention
772
+ * (§12 G12), never a memory.
773
+ */
774
+ private lineage(memoryId: string): string[] {
775
+ const out = [memoryId];
776
+ let current = memoryId;
777
+ for (let hop = 0; hop < DATE_LINEAGE_MAX; hop++) {
778
+ const raw = this.store.row(current)?.meta;
779
+ if (raw === undefined) break;
780
+ let from: unknown;
781
+ try {
782
+ from = (JSON.parse(raw) as Record<string, unknown>)[DATE_FROM_META];
783
+ } catch {
784
+ break;
785
+ }
786
+ if (typeof from !== "string" || out.includes(from)) break;
787
+ out.push(from);
788
+ current = from;
789
+ }
790
+ return out;
791
+ }
792
+
793
+ /**
794
+ * The firing-state rows the brakes read: this memory's own, and — for a
795
+ * window it has no row for yet — the row of the nearest memory its reminder
796
+ * moved from, for the SAME window key. So a moved reminder keeps its spent
797
+ * budget, its last-fired day and a referenced stop; a rescheduled one (a new
798
+ * date, so a new key) starts fresh, exactly as `reschedule` does (§12 G9).
799
+ * The first fire writes the successor's own row from there.
800
+ */
801
+ private firingRowsFor(memoryId: string): Map<string, ProspectiveRow> {
802
+ const out = this.rowsFor(memoryId);
803
+ for (const id of this.lineage(memoryId).slice(1)) {
804
+ for (const [key, row] of this.rowsFor(id)) if (!out.has(key)) out.set(key, row);
805
+ }
806
+ return out;
807
+ }
808
+
809
+ /** A memory's plain rows, newest first. At most two beats per window, so a
810
+ * small bound is every row a live memory will ever have. */
811
+ private plainRows(memoryId: string): { dedup_key: string | null; payload: string | null }[] {
812
+ return this.store.eventLog({ name: PROSPECTIVE_PLAIN_EVENT, ref: memoryId, order: "desc", limit: 50 });
813
+ }
814
+
527
815
  // ── firing-state transitions (all refuse under observer) ──────────────────
528
816
 
529
817
  /**
@@ -586,7 +874,7 @@ export class Prospective {
586
874
  return { fired: false, reason: "not-eligible", derivation: p.reason, fires: 0, state: "armed" };
587
875
  }
588
876
  const w = p.windows.find((x) => x.key === input.windowKey);
589
- const rows = this.rowsFor(input.memoryId);
877
+ const rows = this.firingRowsFor(input.memoryId);
590
878
  const row = rows.get(input.windowKey);
591
879
  if (w === undefined) {
592
880
  this.emit("prospective.fire.refused", input.memoryId, {
@@ -650,6 +938,13 @@ export class Prospective {
650
938
  precision: w.precision,
651
939
  fires,
652
940
  cap: this.tunables.FIRES_PER_WINDOW,
941
+ // Plain or quiet, so the gauge can count the two apart (2026-09-26).
942
+ mode: cueModeOf(loaded.doc),
943
+ // The calendar day the fire was ABOUT, as the plain row carries it: the
944
+ // fired view dates a row by this field before the wall clock, so a fire
945
+ // near midnight or far from UTC is counted on the day it was asked
946
+ // (2026-09-26 review — the gauge test failed in Pacific/Auckland).
947
+ date: input.at,
653
948
  });
654
949
  return { fired: true, reason: "fired", derivation: null, fires, state: "fired" };
655
950
  }
@@ -759,7 +1054,9 @@ export class Prospective {
759
1054
  }
760
1055
  const nextPrecision = precisionOf(input.nextDate);
761
1056
  const nextWindow =
762
- nextPrecision === null ? null : windowFor(input.nextDate, nextPrecision, this.tunables);
1057
+ nextPrecision === null
1058
+ ? null
1059
+ : windowFor(input.nextDate, nextPrecision, this.tunables, input.memoryId);
763
1060
  if (nextWindow === null) {
764
1061
  this.emit("prospective.reschedule.refused", input.memoryId, { reason: "malformed-date" });
765
1062
  return { rescheduled: false, reason: "malformed-date", retiredKey: null, armedKey: null };
@@ -826,7 +1123,14 @@ export class Prospective {
826
1123
  const exits: Exit[] = [];
827
1124
  const counts = Object.fromEntries(EXIT_KINDS.map((k) => [k, 0])) as Record<ExitKind, number>;
828
1125
 
829
- for (const id of this.store.list({})) {
1126
+ // Every memory that has a date (archived ones included — archived before its
1127
+ // window is the `faded` exit) and every memory that has a firing row (a row
1128
+ // can outlive a cleared date). Two indexed reads, not a scan of the store.
1129
+ const ids = new Set<string>(
1130
+ this.store.datedMemories("0001-01-01", "9999-12-31", { archived: true }).map((m) => m.id),
1131
+ );
1132
+ for (const id of this.store.prospectiveMemoryIds()) ids.add(id);
1133
+ for (const id of [...ids].sort()) {
830
1134
  const loaded = this.load(id, [], d);
831
1135
  if (loaded === null) continue;
832
1136
  const p = derive(loaded.memory, loaded.dates, at, this.tunables);
@@ -836,7 +1140,8 @@ export class Prospective {
836
1140
  for (const key of rows.keys()) {
837
1141
  if (byKey.has(key)) continue;
838
1142
  const parsed = parseWindowKey(key);
839
- const w = parsed === null ? null : windowFor(parsed.eventDate, parsed.precision, this.tunables);
1143
+ const w =
1144
+ parsed === null ? null : windowFor(parsed.eventDate, parsed.precision, this.tunables, id);
840
1145
  byKey.set(key, { window: w, phase: w === null ? null : phaseOf(w, at) });
841
1146
  }
842
1147
 
@@ -898,6 +1203,18 @@ export class Prospective {
898
1203
  if (row.last_fired_day !== null && row.last_fired_day === ctx.day) {
899
1204
  return "already-fired-today";
900
1205
  }
1206
+ // Tune question (b): the last fire of a month or range waits for after
1207
+ // the span it names. Without it, a month item spent its whole budget in
1208
+ // its first days and never knew when "after" was.
1209
+ if (
1210
+ this.tunables.HOLD_LAST_FIRE_FOR_AFTER &&
1211
+ w.precision !== "day" &&
1212
+ this.tunables.FIRES_PER_WINDOW >= 2 &&
1213
+ row.fires >= this.tunables.FIRES_PER_WINDOW - 1 &&
1214
+ ctx.at <= w.lastDay
1215
+ ) {
1216
+ return "held-for-after";
1217
+ }
901
1218
  }
902
1219
  if (ctx.sessionId !== undefined && this.sessionFired.get(ctx.sessionId)?.has(w.key) === true) {
903
1220
  return "session-dedup";
@@ -917,8 +1234,10 @@ export class Prospective {
917
1234
  // A removed memory is not an error here; it simply has no future.
918
1235
  return null;
919
1236
  }
920
- const dates = [...contentDates(read.doc), ...extra];
1237
+ const explicit = contentDates(read.doc);
1238
+ const dates = [...explicit, ...extra];
921
1239
  const seen = new Set<string>();
1240
+ const s = strength(read.physics, day);
922
1241
  return {
923
1242
  memory: {
924
1243
  id: memoryId,
@@ -929,10 +1248,14 @@ export class Prospective {
929
1248
  // The same field `sleep/types.ts#isJournal` reads off the row, taken
930
1249
  // from the doc already in hand. See `DerivableMemory.journal`.
931
1250
  journal: read.doc.type === "episode",
1251
+ // The owner's rule (2026-09-26): an author-written date is its own
1252
+ // importance signal, so it skips the salience floor — never decay.
1253
+ explicitDate: explicit.length > 0,
1254
+ faded: s <= this.tunables.FADED_STRENGTH,
932
1255
  },
933
1256
  doc: read.doc,
934
1257
  dates: dates.filter((d) => (seen.has(d.date) ? false : (seen.add(d.date), true))),
935
- strength: strength(read.physics, day),
1258
+ strength: s,
936
1259
  };
937
1260
  }
938
1261
 
@@ -20,7 +20,10 @@ export interface ProspectiveTunables {
20
20
  /** Salience below this never becomes prospective, however well dated.
21
21
  * Salience, NOT strength: eligibility is a predicate over the event date, the
22
22
  * encode date, salience and flags (§12 G2), and a decay exemption before
23
- * arrival is exactly what G10 forbids. [v1: 0.6] CAL. */
23
+ * arrival is exactly what G10 forbids. [v1: 0.6] CAL.
24
+ * Since 2026-09-26 (owner decision) it gates only a date a CALLER extracted:
25
+ * a memory with an explicit `eventDate` is exempt, because choosing a date
26
+ * is itself the importance signal. Decay (`FADED_STRENGTH`) still holds. */
24
27
  SALIENCE_FLOOR: number;
25
28
  /** Calendar days before the event the window opens. [v1: 3] CAL. */
26
29
  LEAD_DAYS: number;
@@ -46,6 +49,18 @@ export interface ProspectiveTunables {
46
49
  FADED_STRENGTH: number;
47
50
  /** Bound on the in-process session-dedup set (INTERFACE-GAPS #3). */
48
51
  MAX_SESSION_WINDOWS: number;
52
+ /** Tune question (a), 2026-09-26: a month-dated memory peaks on the 1st plus a
53
+ * stable per-memory offset of `0..N-1` days, so a month-heavy store does not
54
+ * warm everything on day 1. 0 or 1 turns the stagger off. CAL — v1 measured
55
+ * the clustering, never a spread; seven is "the first week". */
56
+ MONTH_STAGGER_DAYS: number;
57
+ /** Tune question (b), 2026-09-26: a month or range window keeps its LAST fire
58
+ * for after the span it names — the "after" beat a month item never got in
59
+ * v1 (Rosa's post-surgery check-in). Only meaningful with
60
+ * `FIRES_PER_WINDOW >= 2`; with one fire it would never fire, so it is
61
+ * ignored there. Structural in shape, CAL in whether it is warm (open
62
+ * question 1). */
63
+ HOLD_LAST_FIRE_FOR_AFTER: boolean;
49
64
  }
50
65
 
51
66
  /**
@@ -65,6 +80,8 @@ export const TUNABLES: ProspectiveTunables = {
65
80
  RAMP_CLOSE: 0.2,
66
81
  FADED_STRENGTH: PHYSICS.PHI_PRUNE,
67
82
  MAX_SESSION_WINDOWS: 200,
83
+ MONTH_STAGGER_DAYS: 7,
84
+ HOLD_LAST_FIRE_FOR_AFTER: true,
68
85
  };
69
86
 
70
87
  export function withTunables(overrides: Partial<ProspectiveTunables> = {}): ProspectiveTunables {