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
@@ -10,6 +10,7 @@
10
10
  */
11
11
  import { existsSync } from "node:fs";
12
12
 
13
+ import { spacingWeight } from "../physics/index.js";
13
14
  import type { Band, Kind, MemoryPhysics, Salience } from "../types.js";
14
15
  import type { Db, Row } from "./db.js";
15
16
  import { openDb } from "./db.js";
@@ -19,6 +20,17 @@ import { preMigrationDir, snapshotBeforeMigration } from "./pre-migration.js";
19
20
  import type { ProseType } from "./prose.js";
20
21
 
21
22
  /**
23
+ * Bumped to 8 (2026-09-26, dreaming + consolidation): ADDITIVE again, through
24
+ * the same copy-first seam. `memories` gains `legacy` (every row the upgrade
25
+ * finds keeps the old one-time consolidation path, so nothing drops a band)
26
+ * and the RETURNS aggregate (`returns`, `return_days`, `first_return_day`,
27
+ * `last_return_day`, `last_dream_day` — physics §5.11); six tables are new:
28
+ * `returns` (the history behind that aggregate), `wake_display` (what the
29
+ * wake's hints lane showed), `dreams` + `dream_changes` (a dream, its journal,
30
+ * and every change it made, for undo), `dream_asks` (the once-a-day ask and
31
+ * its snooze) and `core_events` (promotions with their lane, the owner's
32
+ * demotions, a dream's nominations). `store/NOTES.md` 2026-09-26.
33
+ *
22
34
  * Bumped to 7 (2026-09-25, docs/time.md): the first ADDITIVE migration since the
23
35
  * copy-before-migrating seam (#214) shipped. Every table that holds records
24
36
  * gains its MOMENTS — `created_at` / `updated_at`, UTC ms from `store.now()` —
@@ -60,7 +72,7 @@ import type { ProseType } from "./prose.js";
60
72
  * migrated open MUST converge on the identical schema; a test asserts
61
73
  * table_info equality.
62
74
  */
63
- export const SCHEMA_VERSION = 7;
75
+ export const SCHEMA_VERSION = 8;
64
76
  /**
65
77
  * The oldest schema an OBSERVER may open without a migration having run.
66
78
  *
@@ -79,8 +91,13 @@ export const SCHEMA_VERSION = 7;
79
91
  * next hook, which migrates it after taking its copy. A floor below the
80
92
  * version would mean every read tolerating missing columns, which is the
81
93
  * shape v5's exemption had and v6 dropped.
94
+ *
95
+ * Raised to 8 with v8 (2026-09-26), for the same reason: a row read names the
96
+ * returns columns, and the new tables are read by instruments. On a v7 store
97
+ * doctor and the dashboard say "not initialized" until the next hook opens it
98
+ * as a writer, which copies it and migrates it.
82
99
  */
83
- export const OBSERVER_READ_FLOOR = 7;
100
+ export const OBSERVER_READ_FLOOR = 8;
84
101
  /** Retention for superseded-version rows, in LIVED days. TUNABLE (module-map ruling 2).
85
102
  *
86
103
  * Owner ruling 1, 2026-09-18: the prune STAYS, at 90. It now deletes the words
@@ -169,7 +186,13 @@ const DDL: readonly string[] = [
169
186
  created_at INTEGER,
170
187
  updated_at INTEGER,
171
188
  model TEXT,
172
- event_date TEXT
189
+ event_date TEXT,
190
+ legacy INTEGER NOT NULL DEFAULT 0,
191
+ returns REAL NOT NULL DEFAULT 0,
192
+ return_days INTEGER NOT NULL DEFAULT 0,
193
+ first_return_day INTEGER,
194
+ last_return_day INTEGER,
195
+ last_dream_day INTEGER
173
196
  )`,
174
197
  `CREATE INDEX IF NOT EXISTS memories_band ON memories (band, archived)`,
175
198
  `CREATE INDEX IF NOT EXISTS memories_kind ON memories (kind, archived)`,
@@ -244,6 +267,103 @@ const DDL: readonly string[] = [
244
267
  updated_at INTEGER NOT NULL
245
268
  )`,
246
269
  `CREATE INDEX IF NOT EXISTS feelings_memory ON feelings (memory_id)`,
270
+ // v8 (2026-09-26, dreaming + consolidation): RETURNS, one row per counted
271
+ // return — an awake credited use after a gap, or a dream replay. The history
272
+ // behind the `returns` / `return_days` / `*_return_day` columns on
273
+ // `memories`, which are recomputed FROM this table whenever it changes, so an
274
+ // undone dream's replays leave no trace in the physics. Ids and numbers only.
275
+ `CREATE TABLE IF NOT EXISTS returns (
276
+ memory_id TEXT NOT NULL REFERENCES memories(id),
277
+ day INTEGER NOT NULL,
278
+ source TEXT NOT NULL,
279
+ weight REAL NOT NULL,
280
+ gap INTEGER NOT NULL,
281
+ dream_id TEXT,
282
+ at INTEGER NOT NULL,
283
+ PRIMARY KEY (memory_id, day, source)
284
+ )`,
285
+ `CREATE INDEX IF NOT EXISTS returns_at ON returns (at)`,
286
+ `CREATE INDEX IF NOT EXISTS returns_dream ON returns (dream_id) WHERE dream_id IS NOT NULL`,
287
+ // v8: WHAT THE WAKE SHOWED in its hints ("Nearby") lane — one row per memory
288
+ // a published bundle kept there: the first and last lived day of the current
289
+ // showing, the day a publish dropped it (NULL while it is still showing), and
290
+ // the habituation load `self/` keeps (#238, harvested). A use while a memory
291
+ // is showing credits as always but is not a RETURN (physics §5.11). The
292
+ // `ever_*` columns are the memory as it stood the FIRST time it was ever
293
+ // shown — its uses and its last use — so the lane can score it on what came
294
+ // back unprompted (`self/identity.ts#hintReading`).
295
+ `CREATE TABLE IF NOT EXISTS wake_display (
296
+ memory_id TEXT PRIMARY KEY REFERENCES memories(id),
297
+ lane TEXT NOT NULL,
298
+ first_day INTEGER NOT NULL,
299
+ shown_day INTEGER NOT NULL,
300
+ closed_day INTEGER,
301
+ load REAL NOT NULL DEFAULT 0,
302
+ ever_day INTEGER NOT NULL,
303
+ ever_uses REAL NOT NULL DEFAULT 0,
304
+ ever_last_used INTEGER NOT NULL,
305
+ updated_at INTEGER NOT NULL
306
+ )`,
307
+ // v8: DREAMS (`core/dream/`). One row per dream: when, for which session, its
308
+ // state (`begun`, `journaled`, `undone`), and its JOURNAL — the dream's own
309
+ // entry, kept here and never as a memory, so it can never be mistaken for a
310
+ // lived event (no decay, no dedup, no prune, no recall). The latest row's day
311
+ // is "last dreamed".
312
+ `CREATE TABLE IF NOT EXISTS dreams (
313
+ id TEXT PRIMARY KEY,
314
+ session TEXT,
315
+ scope TEXT,
316
+ day INTEGER NOT NULL,
317
+ date TEXT,
318
+ state TEXT NOT NULL,
319
+ started_at INTEGER NOT NULL,
320
+ finished_at INTEGER,
321
+ undone_at INTEGER,
322
+ model TEXT,
323
+ title TEXT,
324
+ journal TEXT,
325
+ shown TEXT NOT NULL DEFAULT '[]'
326
+ )`,
327
+ `CREATE INDEX IF NOT EXISTS dreams_day ON dreams (day)`,
328
+ // v8: every change a dream made, in order, with what undoing it needs — ids
329
+ // and numbers only in `detail`, never words. `undone` marks a reversed row.
330
+ `CREATE TABLE IF NOT EXISTS dream_changes (
331
+ dream_id TEXT NOT NULL REFERENCES dreams(id),
332
+ seq INTEGER NOT NULL,
333
+ action TEXT NOT NULL,
334
+ ref TEXT,
335
+ ref2 TEXT,
336
+ detail TEXT NOT NULL DEFAULT '{}',
337
+ at INTEGER NOT NULL,
338
+ undone INTEGER NOT NULL DEFAULT 0,
339
+ PRIMARY KEY (dream_id, seq)
340
+ )`,
341
+ // v8: the dream ASK, one row per calendar day it was raised: `offered` when a
342
+ // session was handed the line, `declined` when the owner said not today.
343
+ `CREATE TABLE IF NOT EXISTS dream_asks (
344
+ date TEXT PRIMARY KEY,
345
+ state TEXT NOT NULL,
346
+ session TEXT,
347
+ day INTEGER NOT NULL,
348
+ at INTEGER NOT NULL
349
+ )`,
350
+ // v8: the CORE's history — every promotion (with its lane), every owner
351
+ // demotion (with the owner's reason) and every dream nomination. Not
352
+ // foreign-keyed: the record outlives a merge. The owner's removal deletes a
353
+ // memory's rows, because a reason is words about it.
354
+ `CREATE TABLE IF NOT EXISTS core_events (
355
+ seq INTEGER PRIMARY KEY AUTOINCREMENT,
356
+ memory_id TEXT NOT NULL,
357
+ action TEXT NOT NULL,
358
+ day INTEGER NOT NULL,
359
+ at INTEGER NOT NULL,
360
+ lane TEXT,
361
+ reason TEXT,
362
+ dream_id TEXT,
363
+ actor TEXT
364
+ )`,
365
+ `CREATE INDEX IF NOT EXISTS core_events_memory ON core_events (memory_id)`,
366
+ `CREATE INDEX IF NOT EXISTS core_events_at ON core_events (at)`,
247
367
  `CREATE INDEX IF NOT EXISTS feelings_whose_core ON feelings (whose, core)`,
248
368
  `CREATE INDEX IF NOT EXISTS feelings_whose_emotion ON feelings (whose, emotion)`,
249
369
  // SEAMS item B — per-session gate state, ONE ROW PER RECORD.
@@ -387,6 +507,97 @@ export interface MemoryRow extends Row {
387
507
  model: string | null;
388
508
  /** v7: the calendar date the memory is about, as said (`time.ts`). */
389
509
  event_date: string | null;
510
+ /** v8: 1 for every row the v8 upgrade found (the legacy consolidation path). */
511
+ legacy: number;
512
+ /** v8: the returns aggregate, recomputed from the `returns` table. */
513
+ returns: number;
514
+ return_days: number;
515
+ first_return_day: number | null;
516
+ last_return_day: number | null;
517
+ last_dream_day: number | null;
518
+ }
519
+
520
+ export interface ReturnRow extends Row {
521
+ memory_id: string;
522
+ day: number;
523
+ source: string;
524
+ weight: number;
525
+ gap: number;
526
+ dream_id: string | null;
527
+ at: number;
528
+ }
529
+
530
+ export interface WakeDisplayRow extends Row {
531
+ memory_id: string;
532
+ lane: string;
533
+ first_day: number;
534
+ shown_day: number;
535
+ closed_day: number | null;
536
+ load: number;
537
+ /** The first lived day it was EVER shown, and its uses / last use then. */
538
+ ever_day: number;
539
+ ever_uses: number;
540
+ ever_last_used: number;
541
+ updated_at: number;
542
+ }
543
+
544
+ export interface DreamRow extends Row {
545
+ id: string;
546
+ session: string | null;
547
+ scope: string | null;
548
+ day: number;
549
+ date: string | null;
550
+ state: string;
551
+ started_at: number;
552
+ finished_at: number | null;
553
+ undone_at: number | null;
554
+ model: string | null;
555
+ title: string | null;
556
+ journal: string | null;
557
+ /** The ids the dream was SHOWN (its bundle), as JSON: the only ids it may change. */
558
+ shown: string;
559
+ }
560
+
561
+ export interface DreamChangeRow extends Row {
562
+ dream_id: string;
563
+ seq: number;
564
+ action: string;
565
+ ref: string | null;
566
+ ref2: string | null;
567
+ detail: string;
568
+ at: number;
569
+ undone: number;
570
+ }
571
+
572
+ export interface DreamAskRow extends Row {
573
+ date: string;
574
+ state: string;
575
+ session: string | null;
576
+ day: number;
577
+ at: number;
578
+ }
579
+
580
+ export interface CoreEventRow extends Row {
581
+ seq: number;
582
+ memory_id: string;
583
+ action: string;
584
+ day: number;
585
+ at: number;
586
+ lane: string | null;
587
+ reason: string | null;
588
+ dream_id: string | null;
589
+ actor: string | null;
590
+ }
591
+
592
+ /**
593
+ * NOT A COLUMN: the strongest recorded feeling on a memory, read beside the
594
+ * row by `Store.row()` (`MAX(feelings.strength)`), null when it has none.
595
+ * Optional, and kept off `MemoryRow` itself, so a bare `SELECT * FROM
596
+ * memories` is still a row — such a read simply carries no feeling, and the
597
+ * intensity falls back to the numeric `emotional` score (physics §5.10).
598
+ */
599
+ export interface FeelingPeak {
600
+ feeling_peak?: number | null;
390
601
  }
391
602
 
392
603
  export interface VersionRow extends Row {
@@ -698,6 +909,39 @@ export function openOperational(path: string, opts: OpenOperationalOptions = {})
698
909
  opts.localToday,
699
910
  );
700
911
  }
912
+ // v8 (2026-09-26): every row the upgrade finds is LEGACY — the one-time
913
+ // consolidation bonus path stays open for it exactly as it was, so the
914
+ // redesign moves no memory down a band and prunes none sooner (physics
915
+ // §5.2/§5.11). One statement; what it found is recorded for doctor, and
916
+ // the first decay pass after it measures every row old-vs-new
917
+ // (`sleep/decay.ts#upgradeCensus`). The counts doctor prints are of LIVE
918
+ // memories — not chapters, schema rows, archived rows or tombstones,
919
+ // which the flag also lands on (review of #251: 183 "memories" said for
920
+ // 129 on the seeded demo store).
921
+ if (now !== null && Number.parseInt(now, 10) < 8) {
922
+ db.run("UPDATE memories SET legacy = 1");
923
+ const counts = db.get<{ rows: number; consolidated: number; identity: number }>(
924
+ `SELECT COUNT(*) AS rows,
925
+ COALESCE(SUM(consolidated), 0) AS consolidated,
926
+ COALESCE(SUM(promoted_identity), 0) AS identity
927
+ FROM memories WHERE type = 'memory' AND archived = 0`,
928
+ );
929
+ const credited = creditLegacyReturns(db);
930
+ const lived = db.get<{ value: string }>("SELECT value FROM meta WHERE key = 'livedDay'")?.value ?? "0";
931
+ db.run(
932
+ "INSERT OR REPLACE INTO meta (key, value) VALUES (?, ?)",
933
+ V8_UPGRADE_KEY,
934
+ JSON.stringify({
935
+ from: now,
936
+ day: Number.parseInt(lived, 10) || 0,
937
+ at: Date.now(),
938
+ rows: counts?.rows ?? 0,
939
+ consolidated: counts?.consolidated ?? 0,
940
+ identity: counts?.identity ?? 0,
941
+ legacyReturns: credited.memories,
942
+ }),
943
+ );
944
+ }
701
945
  const put = db.prepare("INSERT OR IGNORE INTO meta (key, value) VALUES (?, ?)");
702
946
  put.run("livedDay", "0");
703
947
  put.run("lastActiveDate", "");
@@ -781,8 +1025,78 @@ export const ADDED_COLUMNS: readonly { table: string; column: string; ddl: strin
781
1025
  { table: "edges", column: "updated_at", ddl: "ALTER TABLE edges ADD COLUMN updated_at INTEGER" },
782
1026
  { table: "prospective", column: "created_at", ddl: "ALTER TABLE prospective ADD COLUMN created_at INTEGER" },
783
1027
  { table: "prospective", column: "updated_at", ddl: "ALTER TABLE prospective ADD COLUMN updated_at INTEGER" },
1028
+ // v8 (2026-09-26, dreaming + consolidation). `legacy` marks every row the
1029
+ // upgrade found (set to 1 in the migrating transaction, below), so the old
1030
+ // consolidation bonus path stays open for exactly those; the rest are the
1031
+ // returns aggregate (`returns` table). Defaults of 0 are the pre-v8
1032
+ // arithmetic exactly: a factor of 1 on stability, no lane progress.
1033
+ { table: "memories", column: "legacy", ddl: "ALTER TABLE memories ADD COLUMN legacy INTEGER NOT NULL DEFAULT 0" },
1034
+ { table: "memories", column: "returns", ddl: "ALTER TABLE memories ADD COLUMN returns REAL NOT NULL DEFAULT 0" },
1035
+ {
1036
+ table: "memories",
1037
+ column: "return_days",
1038
+ ddl: "ALTER TABLE memories ADD COLUMN return_days INTEGER NOT NULL DEFAULT 0",
1039
+ },
1040
+ { table: "memories", column: "first_return_day", ddl: "ALTER TABLE memories ADD COLUMN first_return_day INTEGER" },
1041
+ { table: "memories", column: "last_return_day", ddl: "ALTER TABLE memories ADD COLUMN last_return_day INTEGER" },
1042
+ { table: "memories", column: "last_dream_day", ddl: "ALTER TABLE memories ADD COLUMN last_dream_day INTEGER" },
784
1043
  ];
785
1044
 
1045
+ /**
1046
+ * THE HISTORY A LEGACY ROW ALREADY HAD, credited as returns at the v8 upgrade
1047
+ * (working default 2026-09-26, the review of #251): the old rules could carry
1048
+ * a well-used memory into the identity band; the new ones let a fact go, so
1049
+ * its durability must reflect the reinforcement it earned. Each distinct
1050
+ * reinforced day (`reinforced_days`, a count — the days themselves were never
1051
+ * kept) becomes one `returns` row of source `legacy`, placed evenly between
1052
+ * the memory's birth and its last use (the last one exactly on it) and weighed
1053
+ * by the same spacing rule as any return. `legacy` rows lengthen stability and
1054
+ * nothing else: the core lanes read awake returns only, and invented day
1055
+ * positions are not a lane's evidence. Idempotent (the rows' primary key), in
1056
+ * the migrating transaction. Returns what it credited.
1057
+ */
1058
+ export function creditLegacyReturns(db: Db): { memories: number; rows: number } {
1059
+ const at = Date.now();
1060
+ const candidates = db.all<{ id: string; birth_day: number; last_used_day: number; reinforced_days: number }>(
1061
+ `SELECT id, birth_day, last_used_day, reinforced_days FROM memories
1062
+ WHERE type = 'memory' AND body != '' AND reinforced_days > 0 AND last_used_day > birth_day`,
1063
+ );
1064
+ const insert = db.prepare(
1065
+ `INSERT OR IGNORE INTO returns (memory_id, day, source, weight, gap, dream_id, at)
1066
+ VALUES (?, ?, 'legacy', ?, ?, NULL, ?)`,
1067
+ );
1068
+ let memories = 0;
1069
+ let rows = 0;
1070
+ for (const c of candidates) {
1071
+ const span = c.last_used_day - c.birth_day;
1072
+ const n = Math.min(c.reinforced_days, span);
1073
+ let prev = c.birth_day;
1074
+ let wrote = 0;
1075
+ for (let k = 1; k <= n; k += 1) {
1076
+ const day = k === n ? c.last_used_day : c.birth_day + Math.round((k * span) / n);
1077
+ if (day <= prev) continue;
1078
+ const gap = day - prev;
1079
+ insert.run(c.id, day, spacingWeight(gap), gap, at);
1080
+ prev = day;
1081
+ wrote += 1;
1082
+ }
1083
+ if (wrote > 0) {
1084
+ memories += 1;
1085
+ rows += wrote;
1086
+ db.run(
1087
+ "UPDATE memories SET returns = (SELECT COALESCE(SUM(weight), 0) FROM returns WHERE memory_id = ?) WHERE id = ?",
1088
+ c.id,
1089
+ c.id,
1090
+ );
1091
+ }
1092
+ }
1093
+ return { memories, rows };
1094
+ }
1095
+
1096
+ /** Meta key: what the v8 upgrade found (`physics.v8.upgrade`), for doctor.
1097
+ * `sleep/upgrade.ts` spells the same key for its census latch. */
1098
+ export const V8_UPGRADE_KEY = "physics.v8.upgrade";
1099
+
786
1100
  function ensureAddedColumns(db: Db): void {
787
1101
  const byTable = new Map<string, Set<string>>();
788
1102
  for (const spec of ADDED_COLUMNS) {
@@ -809,7 +1123,7 @@ export function rowToSalience(row: MemoryRow): Salience {
809
1123
  };
810
1124
  }
811
1125
 
812
- export function rowToPhysics(row: MemoryRow): MemoryPhysics {
1126
+ export function rowToPhysics(row: MemoryRow & FeelingPeak): MemoryPhysics {
813
1127
  return {
814
1128
  kind: row.kind,
815
1129
  salience: rowToSalience(row),
@@ -819,7 +1133,18 @@ export function rowToPhysics(row: MemoryRow): MemoryPhysics {
819
1133
  // Distinct lived days that credited a use — `uses` is a weighted sum and
820
1134
  // cannot reconstruct it (physics §5.3/§5.5). Absent reads as 0.
821
1135
  reinforcedDays: row.reinforced_days,
1136
+ // The strongest feeling recorded on it (physics §5.10), when the read
1137
+ // carried one — `Store.row()` always does.
1138
+ feelingPeak: row.feeling_peak ?? null,
822
1139
  consolidated: row.consolidated === 1,
1140
+ // v8 (physics §5.2/§5.11). Read tolerantly — a bare row built by a test or
1141
+ // a tool without the columns reads as a post-upgrade memory with no returns.
1142
+ legacy: row.legacy === 1,
1143
+ returns: row.returns ?? 0,
1144
+ returnDays: row.return_days ?? 0,
1145
+ firstReturnDay: row.first_return_day ?? null,
1146
+ lastReturnDay: row.last_return_day ?? null,
1147
+ lastDreamDay: row.last_dream_day ?? null,
823
1148
  promotedIdentity: row.promoted_identity === 1,
824
1149
  protected: row.protected === 1,
825
1150
  pressure: row.pressure,
@@ -195,6 +195,22 @@ export function chaseRemoved(store: Store, id: string): ChaseReport {
195
195
  // never left dangling mid-delete.
196
196
  db.run("DELETE FROM feelings WHERE memory_id = ?", id);
197
197
  db.run("DELETE FROM gate_session WHERE ref = ?", id);
198
+ // v8: its return history, what the wake showed of it, and its core history
199
+ // (a demotion's or a nomination's reason is words about it) go too. A
200
+ // dream's change log keeps the ORDER of what the dream did, but no longer
201
+ // names this memory: the address is blanked, never the dream's other rows.
202
+ db.run("DELETE FROM returns WHERE memory_id = ?", id);
203
+ db.run("DELETE FROM wake_display WHERE memory_id = ?", id);
204
+ db.run("DELETE FROM core_events WHERE memory_id = ?", id);
205
+ // A DREAM'S JOURNAL about it (working default 2026-09-26, review of #251):
206
+ // a dream that was SHOWN the memory, CHANGED it, or whose journal QUOTES it
207
+ // (a six-word run of its words, or its title) may carry its words in the
208
+ // dream's own. The journal row stays — the dream happened, and its changes
209
+ // stay listed — but its title and entry are replaced by a line saying they
210
+ // were redacted. Asked before the words below are blanked.
211
+ const journals = redactDreamJournals(db, id, row?.title ?? null, row?.body ?? "");
212
+ db.run("UPDATE dream_changes SET ref = NULL WHERE ref = ?", id);
213
+ db.run("UPDATE dream_changes SET ref2 = NULL WHERE ref2 = ?", id);
198
214
 
199
215
  // Version rows stay (a successor's predecessor pointer lives here) and lose
200
216
  // every word they held. `reason`, `version_day` and `successor_id` are
@@ -228,7 +244,9 @@ export function chaseRemoved(store: Store, id: string): ChaseReport {
228
244
  archived = 1, archived_reason = ?, content_hash = '',
229
245
  title = NULL, body = '', meta = '{}', confidential = 0,
230
246
  learned_on = '', happened_on = NULL,
231
- created_at = NULL, updated_at = NULL, model = NULL, event_date = NULL
247
+ created_at = NULL, updated_at = NULL, model = NULL, event_date = NULL,
248
+ returns = 0, return_days = 0, first_return_day = NULL, last_return_day = NULL,
249
+ last_dream_day = NULL
232
250
  WHERE id = ?`,
233
251
  REMOVED_REASON,
234
252
  id,
@@ -268,6 +286,7 @@ export function chaseRemoved(store: Store, id: string): ChaseReport {
268
286
  neutralized: [
269
287
  { surface: "operational.memories", count: row === undefined ? 0 : 1 },
270
288
  { surface: "operational.versions", count: versions },
289
+ ...(journals > 0 ? [{ surface: "operational.dreams", count: journals }] : []),
271
290
  ],
272
291
  survives: ["removal_record", "deny-list", "removal_tombstone"],
273
292
  noop,
@@ -275,6 +294,65 @@ export function chaseRemoved(store: Store, id: string): ChaseReport {
275
294
  });
276
295
  }
277
296
 
297
+ /** What a redacted dream journal says instead, title and entry. */
298
+ const REDACTED_JOURNAL_TITLE = "(redacted)";
299
+ const REDACTED_JOURNAL =
300
+ "[This dream's journal was redacted: it was shown, changed or quoted a memory the owner removed.]";
301
+
302
+ /** Lowercase words, the one normalization the quote check uses on both sides. */
303
+ function wordsOf(text: string): string[] {
304
+ return text
305
+ .toLowerCase()
306
+ .split(/[^\p{L}\p{N}]+/u)
307
+ .filter((w) => w.length > 0);
308
+ }
309
+
310
+ /** Does `journal` carry a run of `window` consecutive words of `body`? */
311
+ function quotes(body: string, journal: string, window = 6): boolean {
312
+ const b = wordsOf(body);
313
+ const j = wordsOf(journal);
314
+ if (b.length < window || j.length < window) return false;
315
+ const seen = new Set<string>();
316
+ for (let i = 0; i + window <= j.length; i += 1) seen.add(j.slice(i, i + window).join(" "));
317
+ for (let i = 0; i + window <= b.length; i += 1) if (seen.has(b.slice(i, i + window).join(" "))) return true;
318
+ return false;
319
+ }
320
+
321
+ /**
322
+ * Redact every dream journal that cites or quotes memory `id`: shown it (the
323
+ * dream's `shown` ids), changed it (`dream_changes`), or carries its title or a
324
+ * six-word run of its words. The row, its date, its state and its change list
325
+ * stay. Returns how many journals were redacted. Inside the caller's transaction.
326
+ */
327
+ function redactDreamJournals(db: Db, id: string, title: string | null, body: string): number {
328
+ const dreams = db.all<{ id: string; shown: string; title: string | null; journal: string | null }>(
329
+ "SELECT id, shown, title, journal FROM dreams WHERE journal IS NOT NULL OR title IS NOT NULL",
330
+ );
331
+ const changed = new Set(
332
+ db
333
+ .all<{ dream_id: string }>("SELECT DISTINCT dream_id FROM dream_changes WHERE ref = ? OR ref2 = ?", id, id)
334
+ .map((r) => r.dream_id),
335
+ );
336
+ const named = title !== null && title.trim().length >= 8 ? title.trim().toLowerCase() : null;
337
+ let n = 0;
338
+ for (const d of dreams) {
339
+ if (d.journal === REDACTED_JOURNAL) continue;
340
+ let shown = false;
341
+ try {
342
+ const ids: unknown = JSON.parse(d.shown);
343
+ shown = Array.isArray(ids) && ids.includes(id);
344
+ } catch {
345
+ shown = false;
346
+ }
347
+ const text = `${d.title ?? ""}\n${d.journal ?? ""}`;
348
+ const quoted = quotes(body, text) || (named !== null && text.toLowerCase().includes(named));
349
+ if (!shown && !changed.has(d.id) && !quoted) continue;
350
+ db.run("UPDATE dreams SET title = ?, journal = ? WHERE id = ?", REDACTED_JOURNAL_TITLE, REDACTED_JOURNAL, d.id);
351
+ n += 1;
352
+ }
353
+ return n;
354
+ }
355
+
278
356
  // ── the repair ──────────────────────────────────────────────────────────────
279
357
 
280
358
  /**
package/src/core/types.ts CHANGED
@@ -26,6 +26,11 @@ export const MEMORY_SOURCES = [
26
26
  "episode",
27
27
  "accommodation",
28
28
  "migrated",
29
+ // A dream's own words (2026-09-26, `core/dream/`): a gist or pattern the
30
+ // dreamer wrote while replaying, citing its sources. Engine-set by the dream
31
+ // seam alone, and it starts lower than lived testimony (physics
32
+ // `DREAMED_CLAIM_CEILING`): it rises only if it proves true awake.
33
+ "dreamed",
29
34
  ] as const;
30
35
  export type MemorySource = (typeof MEMORY_SOURCES)[number];
31
36
 
@@ -71,7 +76,39 @@ export interface MemoryPhysics {
71
76
  * Optional so this stays an extension, not a break; absent reads as 0, which
72
77
  * is the promotion-blocking direction. */
73
78
  reinforcedDays?: number;
79
+ /**
80
+ * The strongest RECORDED feeling on this memory (the `feelings` table's
81
+ * `MAX(strength)`, owner's or self's), or absent/null when it carries none.
82
+ * Read beside the row by `store.row()`; physics folds it into the emotional
83
+ * intensity (`physics.emotionalIntensity`, CONTRACT §5.10). As recorded, never
84
+ * softened: the height and slope a feeling gives a memory are set by how it
85
+ * felt, the way affect stamps a trace at encoding.
86
+ */
87
+ feelingPeak?: number | null;
74
88
  consolidated: boolean;
89
+ /**
90
+ * Born before schema v8 (the dreaming + consolidation redesign, 2026-09-26):
91
+ * the one-time `+CONS_BONUS` consolidation path stays open for it, exactly as
92
+ * it was, so the upgrade moves no memory down (physics §5.2). Absent reads
93
+ * false — every memory made since the upgrade earns durability from RETURNS
94
+ * instead.
95
+ */
96
+ legacy?: boolean;
97
+ /**
98
+ * RETURNS (physics §5.11): the weighted count of spaced returns — a credited
99
+ * organic use on a lived day after the previous counted return, or a dream
100
+ * replay at a fraction of that. Each one slows fading (stability), with
101
+ * diminishing returns; close-together returns count less, never against.
102
+ * Absent reads 0, which is exactly today's arithmetic.
103
+ */
104
+ returns?: number;
105
+ /** Distinct lived days with a counted AWAKE return (the core lanes read it). */
106
+ returnDays?: number;
107
+ /** The first and last lived day of a counted AWAKE return; null when none. */
108
+ firstReturnDay?: number | null;
109
+ lastReturnDay?: number | null;
110
+ /** The last lived day a dream replayed it; null when never. */
111
+ lastDreamDay?: number | null;
75
112
  /** Set only by the explicit promotion crossing or revision inheritance (§5.3). */
76
113
  promotedIdentity: boolean;
77
114
  protected: boolean;