counterparts 0.3.0 → 0.3.2

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 (207) hide show
  1. package/CHANGELOG.md +76 -0
  2. package/docs/module-map.md +2 -0
  3. package/package.json +2 -1
  4. package/src/adapters/claude-code/CONTRACT.md +2 -1
  5. package/src/adapters/claude-code/NOTES.md +90 -2
  6. package/src/adapters/claude-code/bin/hook.ts +17 -2
  7. package/src/adapters/claude-code/bin/runner.ts +4 -1
  8. package/src/adapters/claude-code/config.ts +23 -0
  9. package/src/adapters/claude-code/doctor.ts +148 -26
  10. package/src/adapters/claude-code/hooks.ts +37 -12
  11. package/src/adapters/claude-code/index.ts +2 -0
  12. package/src/adapters/claude-code/standdown.ts +12 -1
  13. package/src/adapters/claude-code/transcript.ts +79 -6
  14. package/src/adapters/cli/INTERFACE-GAPS.md +4 -0
  15. package/src/adapters/cli/commands.ts +209 -61
  16. package/src/adapters/cli/export.ts +27 -3
  17. package/src/adapters/cli/help.ts +22 -8
  18. package/src/adapters/cli/install.ts +3 -5
  19. package/src/adapters/cli/mechanisms.ts +295 -0
  20. package/src/adapters/cli/repair-dates.ts +3 -1
  21. package/src/adapters/cli/snapshot.ts +4 -27
  22. package/src/adapters/dashboard/CONTRACT.md +13 -9
  23. package/src/adapters/dashboard/INTERFACE-GAPS.md +4 -0
  24. package/src/adapters/dashboard/NOTES.md +3 -2
  25. package/src/adapters/dashboard/activity.ts +2 -1
  26. package/src/adapters/dashboard/bin/dashboard.ts +18 -3
  27. package/src/adapters/dashboard/upgrade.ts +23 -0
  28. package/src/adapters/dashboard/web/README.md +153 -0
  29. package/src/adapters/dashboard/web/actions.ts +631 -0
  30. package/src/adapters/dashboard/web/app.html +40 -1703
  31. package/src/adapters/dashboard/web/app.js +55 -0
  32. package/src/adapters/dashboard/web/mechanisms/association/index.js +15 -0
  33. package/src/adapters/dashboard/web/mechanisms/association/panel.js +17 -0
  34. package/src/adapters/dashboard/web/mechanisms/consolidation/index.js +15 -0
  35. package/src/adapters/dashboard/web/mechanisms/consolidation/panel.js +23 -0
  36. package/src/adapters/dashboard/web/mechanisms/decay/index.js +15 -0
  37. package/src/adapters/dashboard/web/mechanisms/decay/panel.js +45 -0
  38. package/src/adapters/dashboard/web/mechanisms/emotional/index.js +15 -0
  39. package/src/adapters/dashboard/web/mechanisms/episodic-semantic/index.js +15 -0
  40. package/src/adapters/dashboard/web/mechanisms/index.js +60 -0
  41. package/src/adapters/dashboard/web/mechanisms/interference/index.js +15 -0
  42. package/src/adapters/dashboard/web/mechanisms/picture.js +28 -0
  43. package/src/adapters/dashboard/web/mechanisms/prospective/index.js +15 -0
  44. package/src/adapters/dashboard/web/mechanisms/reconsolidation/index.js +15 -0
  45. package/src/adapters/dashboard/web/mechanisms/reconsolidation/panel.js +15 -0
  46. package/src/adapters/dashboard/web/mechanisms/regions.js +30 -0
  47. package/src/adapters/dashboard/web/mechanisms/retrieval/index.js +15 -0
  48. package/src/adapters/dashboard/web/mechanisms/retrieval/panel.js +20 -0
  49. package/src/adapters/dashboard/web/mechanisms/salience/index.js +15 -0
  50. package/src/adapters/dashboard/web/mechanisms/salience/panel.js +14 -0
  51. package/src/adapters/dashboard/web/mechanisms/schema/index.js +15 -0
  52. package/src/adapters/dashboard/web/narrate.ts +10 -10
  53. package/src/adapters/dashboard/web/pages/flow/flow.css +38 -0
  54. package/src/adapters/dashboard/web/pages/flow/index.js +117 -0
  55. package/src/adapters/dashboard/web/pages/flow/sections/blind.js +12 -0
  56. package/src/adapters/dashboard/web/pages/flow/sections/detail.js +45 -0
  57. package/src/adapters/dashboard/web/pages/flow/sections/diagram.js +291 -0
  58. package/src/adapters/dashboard/web/pages/flow/sections/feed.js +11 -0
  59. package/src/adapters/dashboard/web/pages/flow/sections/fired.js +41 -0
  60. package/src/adapters/dashboard/web/pages/flow/sections/heatmap.js +25 -0
  61. package/src/adapters/dashboard/web/pages/flow/sections/records.js +24 -0
  62. package/src/adapters/dashboard/web/pages/flow/sections/symmetry.js +17 -0
  63. package/src/adapters/dashboard/web/pages/flow/state.js +3 -0
  64. package/src/adapters/dashboard/web/pages/health/health.css +93 -0
  65. package/src/adapters/dashboard/web/pages/health/index.js +41 -0
  66. package/src/adapters/dashboard/web/pages/health/sections/archive.js +84 -0
  67. package/src/adapters/dashboard/web/pages/health/sections/checks.js +172 -0
  68. package/src/adapters/dashboard/web/pages/health/sections/cycle.js +37 -0
  69. package/src/adapters/dashboard/web/pages/health/sections/verify.js +54 -0
  70. package/src/adapters/dashboard/web/pages/home/brain.js +375 -0
  71. package/src/adapters/dashboard/web/pages/home/home.css +134 -0
  72. package/src/adapters/dashboard/web/pages/home/index.js +84 -0
  73. package/src/adapters/dashboard/web/pages/home/sections/explorer.js +206 -0
  74. package/src/adapters/dashboard/web/pages/home/sections/hero.js +22 -0
  75. package/src/adapters/dashboard/web/pages/home/sections/live-activity.js +25 -0
  76. package/src/adapters/dashboard/web/pages/home/sections/recent-chapters.js +13 -0
  77. package/src/adapters/dashboard/web/pages/home/sections/tiles.js +52 -0
  78. package/src/adapters/dashboard/web/pages/memories/index.js +75 -0
  79. package/src/adapters/dashboard/web/pages/memories/memories.css +143 -0
  80. package/src/adapters/dashboard/web/pages/memories/sections/constellation.js +178 -0
  81. package/src/adapters/dashboard/web/pages/memories/sections/kinds.js +42 -0
  82. package/src/adapters/dashboard/web/pages/memories/sections/list.js +114 -0
  83. package/src/adapters/dashboard/web/pages/memories/sections/search.js +102 -0
  84. package/src/adapters/dashboard/web/pages/memories/sections/tools.js +155 -0
  85. package/src/adapters/dashboard/web/pages/memories/state.js +22 -0
  86. package/src/adapters/dashboard/web/pages/self/diff.js +79 -0
  87. package/src/adapters/dashboard/web/pages/self/index.js +43 -0
  88. package/src/adapters/dashboard/web/pages/self/markdown.js +61 -0
  89. package/src/adapters/dashboard/web/pages/self/sections/journal.js +32 -0
  90. package/src/adapters/dashboard/web/pages/self/sections/page.js +149 -0
  91. package/src/adapters/dashboard/web/pages/self/sections/settling.js +150 -0
  92. package/src/adapters/dashboard/web/pages/self/sections/stories.js +47 -0
  93. package/src/adapters/dashboard/web/pages/self/sections/wake.js +74 -0
  94. package/src/adapters/dashboard/web/pages/self/self.css +182 -0
  95. package/src/adapters/dashboard/web/server.ts +305 -28
  96. package/src/adapters/dashboard/web/shared/absence.js +14 -0
  97. package/src/adapters/dashboard/web/shared/actions.js +48 -0
  98. package/src/adapters/dashboard/web/shared/api.js +17 -0
  99. package/src/adapters/dashboard/web/shared/base.css +34 -0
  100. package/src/adapters/dashboard/web/shared/canvas.js +68 -0
  101. package/src/adapters/dashboard/web/shared/colors.js +6 -0
  102. package/src/adapters/dashboard/web/shared/dom.js +4 -0
  103. package/src/adapters/dashboard/web/shared/event-modal.js +25 -0
  104. package/src/adapters/dashboard/web/shared/fonts/OFL-courier-prime.txt +93 -0
  105. package/src/adapters/dashboard/web/shared/fonts/OFL-outfit.txt +93 -0
  106. package/src/adapters/dashboard/web/shared/fonts/courier-prime-latin-400.woff2 +0 -0
  107. package/src/adapters/dashboard/web/shared/fonts/courier-prime-latin-700.woff2 +0 -0
  108. package/src/adapters/dashboard/web/shared/fonts/outfit-latin-wght.woff2 +0 -0
  109. package/src/adapters/dashboard/web/shared/format.js +31 -0
  110. package/src/adapters/dashboard/web/shared/layout.css +32 -0
  111. package/src/adapters/dashboard/web/shared/memory-modal.js +124 -0
  112. package/src/adapters/dashboard/web/shared/modal.css +24 -0
  113. package/src/adapters/dashboard/web/shared/modal.js +22 -0
  114. package/src/adapters/dashboard/web/shared/state.js +12 -0
  115. package/src/adapters/dashboard/web/shared/tip.css +4 -0
  116. package/src/adapters/dashboard/web/shared/tip.js +12 -0
  117. package/src/adapters/dashboard/web/shared/tokens.css +50 -0
  118. package/src/adapters/dashboard/web/shared/type.css +52 -0
  119. package/src/adapters/dashboard/web/shared/utilities.css +5 -0
  120. package/src/adapters/dashboard/web/shared/vendor/MIT-three.txt +21 -0
  121. package/src/adapters/dashboard/web/shared/vendor/three.module.min.js +6 -0
  122. package/src/adapters/dashboard/web/shared/widgets/bar.css +9 -0
  123. package/src/adapters/dashboard/web/shared/widgets/card.css +6 -0
  124. package/src/adapters/dashboard/web/shared/widgets/chapters.js +15 -0
  125. package/src/adapters/dashboard/web/shared/widgets/chart.css +4 -0
  126. package/src/adapters/dashboard/web/shared/widgets/confirm.css +31 -0
  127. package/src/adapters/dashboard/web/shared/widgets/confirm.js +52 -0
  128. package/src/adapters/dashboard/web/shared/widgets/feed.css +13 -0
  129. package/src/adapters/dashboard/web/shared/widgets/feed.js +41 -0
  130. package/src/adapters/dashboard/web/shared/widgets/light.css +4 -0
  131. package/src/adapters/dashboard/web/shared/widgets/light.js +15 -0
  132. package/src/adapters/dashboard/web/shared/widgets/rows.css +7 -0
  133. package/src/adapters/dashboard/web/shared/widgets/table.css +17 -0
  134. package/src/adapters/dashboard/web/shared/widgets/tiles.css +14 -0
  135. package/src/adapters/dashboard/web/shell/pages.js +15 -0
  136. package/src/adapters/dashboard/web/shell/pulse.js +86 -0
  137. package/src/adapters/dashboard/web/shell/tabs.js +49 -0
  138. package/src/adapters/dashboard/web/static.ts +76 -0
  139. package/src/adapters/dashboard/web/views/activity.ts +80 -0
  140. package/src/adapters/dashboard/web/views/archive-words.ts +57 -0
  141. package/src/adapters/dashboard/web/views/flow-view.ts +217 -0
  142. package/src/adapters/dashboard/web/views/health.ts +328 -0
  143. package/src/adapters/dashboard/web/views/mechanism-panel.ts +370 -0
  144. package/src/adapters/dashboard/web/views/mechanisms.ts +302 -0
  145. package/src/adapters/dashboard/web/views/memories.ts +342 -0
  146. package/src/adapters/dashboard/web/views/memory.ts +210 -0
  147. package/src/adapters/dashboard/web/views/meta.ts +64 -0
  148. package/src/adapters/dashboard/web/views/mind.ts +729 -0
  149. package/src/adapters/dashboard/web/views/overview.ts +282 -0
  150. package/src/adapters/dashboard/web/views/pulse.ts +19 -0
  151. package/src/adapters/dashboard/web/views/rows.ts +207 -0
  152. package/src/adapters/dashboard/web/views/search.ts +62 -0
  153. package/src/adapters/dashboard/web/views/shared.ts +161 -0
  154. package/src/adapters/dashboard/web/views.ts +34 -1724
  155. package/src/adapters/fired.ts +138 -31
  156. package/src/adapters/mcp/INTERFACE-GAPS.md +4 -1
  157. package/src/adapters/mcp/NOTES.md +43 -0
  158. package/src/adapters/mcp/bin/serve.ts +9 -1
  159. package/src/adapters/mcp/index.ts +7 -0
  160. package/src/adapters/mcp/server.ts +167 -6
  161. package/src/adapters/mcp/tools.ts +42 -0
  162. package/src/adapters/mcp/write-up.ts +1 -1
  163. package/src/adapters/sessions.ts +18 -1
  164. package/src/adapters/snapshots.ts +71 -39
  165. package/src/core/associate/INTERFACE-GAPS.md +8 -0
  166. package/src/core/counterpart.ts +86 -7
  167. package/src/core/encode/INTERFACE-GAPS.md +12 -0
  168. package/src/core/feelings-wheel.ts +218 -0
  169. package/src/core/handoff/INTERFACE-GAPS.md +2 -0
  170. package/src/core/mint.ts +14 -2
  171. package/src/core/prospective/INTERFACE-GAPS.md +14 -0
  172. package/src/core/prospective/windows.ts +10 -48
  173. package/src/core/recall/INTERFACE-GAPS.md +6 -0
  174. package/src/core/remember/INTERFACE-GAPS.md +10 -0
  175. package/src/core/schemas/INTERFACE-GAPS.md +8 -0
  176. package/src/core/schemas/NOTES.md +118 -1
  177. package/src/core/schemas/index.ts +330 -40
  178. package/src/core/schemas/tunables.ts +23 -0
  179. package/src/core/schemas/types.ts +64 -1
  180. package/src/core/self/CONTRACT.md +3 -0
  181. package/src/core/self/INTERFACE-GAPS.md +10 -0
  182. package/src/core/self/NOTES.md +33 -2
  183. package/src/core/self/calendar.ts +7 -78
  184. package/src/core/self/episodes.ts +52 -12
  185. package/src/core/self/index.ts +40 -12
  186. package/src/core/self/writer.ts +22 -22
  187. package/src/core/sleep/NOTES.md +36 -0
  188. package/src/core/sleep/cycle.ts +38 -3
  189. package/src/core/sleep/fade.ts +76 -0
  190. package/src/core/sleep/index.ts +4 -1
  191. package/src/core/sleep/prune.ts +8 -1
  192. package/src/core/sleep/tunables.ts +5 -0
  193. package/src/core/sleep/types.ts +24 -0
  194. package/src/core/store/CONTRACT.md +36 -7
  195. package/src/core/store/NOTES.md +197 -0
  196. package/src/core/store/errors.ts +22 -0
  197. package/src/core/store/feelings.ts +182 -0
  198. package/src/core/store/index.ts +339 -45
  199. package/src/core/store/operational.ts +240 -22
  200. package/src/core/store/owner-op-seam.ts +13 -3
  201. package/src/core/store/paths.ts +15 -1
  202. package/src/core/store/pre-migration.ts +258 -0
  203. package/src/core/store/prose.ts +8 -1
  204. package/src/core/store/render.ts +3 -0
  205. package/src/core/time.ts +374 -0
  206. package/src/core/types.ts +13 -0
  207. package/src/adapters/dashboard/web/brain.html +0 -463
@@ -8,14 +8,29 @@
8
8
  *
9
9
  * Every multi-row mutation is wrapped in a transaction by the seam in `index.ts`.
10
10
  */
11
+ import { existsSync } from "node:fs";
12
+
11
13
  import type { Band, Kind, MemoryPhysics, Salience } from "../types.js";
12
14
  import type { Db, Row } from "./db.js";
13
15
  import { openDb } from "./db.js";
14
16
  import { StoreError } from "./errors.js";
15
17
  import { PRE_ROWS_READABLE_BY } from "./paths.js";
18
+ import { preMigrationDir, snapshotBeforeMigration } from "./pre-migration.js";
16
19
  import type { ProseType } from "./prose.js";
17
20
 
18
21
  /**
22
+ * Bumped to 7 (2026-09-25, docs/time.md): the first ADDITIVE migration since the
23
+ * copy-before-migrating seam (#214) shipped. Every table that holds records
24
+ * gains its MOMENTS — `created_at` / `updated_at`, UTC ms from `store.now()` —
25
+ * and `memories` gains `model` (which model wrote the words) and `event_date`
26
+ * (the calendar date the memory is ABOUT, as the person said it: a day, a
27
+ * month, a year or a range). `versions` carries `created_at`, `model` and
28
+ * `event_date` of the words it archived. All nullable, all through
29
+ * `ADDED_COLUMNS`, so a v6 store migrates in one transaction after its copy is
30
+ * taken, and rows written before carry NULL: the owner ruled old dates stay as
31
+ * they are (docs/time.md rule 6). The table-by-table reasoning is in
32
+ * `store/NOTES.md` 2026-09-25.
33
+ *
19
34
  * Bumped to 6 (2026-09-20, the floor): **the memory IS the row.** `title`,
20
35
  * `body`, `meta` and `confidential` are columns on `memories`; a version row
21
36
  * carries its own `title`/`body`/`meta` plus the two provenance dates; and
@@ -45,7 +60,7 @@ import type { ProseType } from "./prose.js";
45
60
  * migrated open MUST converge on the identical schema; a test asserts
46
61
  * table_info equality.
47
62
  */
48
- export const SCHEMA_VERSION = 6;
63
+ export const SCHEMA_VERSION = 7;
49
64
  /**
50
65
  * The oldest schema an OBSERVER may open without a migration having run.
51
66
  *
@@ -57,8 +72,15 @@ export const SCHEMA_VERSION = 6;
57
72
  * the refusal a reader actually meets is `STORE_PRE_ROWS`, which names the last
58
73
  * build that CAN read it (`floor/v5-last`) instead of asking for a migration
59
74
  * that does not exist.
75
+ *
76
+ * Raised to 7 with v7 (2026-09-25): every read of a row now names the v7
77
+ * columns, and a v6 file has none of them, so an instrument meets
78
+ * `STORE_UNINITIALIZED` on a v6 store until the first WRITER opens it — the
79
+ * next hook, which migrates it after taking its copy. A floor below the
80
+ * version would mean every read tolerating missing columns, which is the
81
+ * shape v5's exemption had and v6 dropped.
60
82
  */
61
- export const OBSERVER_READ_FLOOR = 6;
83
+ export const OBSERVER_READ_FLOOR = 7;
62
84
  /** Retention for superseded-version rows, in LIVED days. TUNABLE (module-map ruling 2).
63
85
  *
64
86
  * Owner ruling 1, 2026-09-18: the prune STAYS, at 90. It now deletes the words
@@ -93,6 +115,14 @@ const DDL: readonly string[] = [
93
115
  key TEXT PRIMARY KEY,
94
116
  value TEXT NOT NULL
95
117
  )`,
118
+ // v7 (2026-09-25, docs/time.md) appends four columns to `memories`:
119
+ // `created_at` / `updated_at` (MOMENTS, UTC ms from `store.now()` — the words
120
+ // or state changing, not physics bookkeeping), `model` (the model id that
121
+ // wrote the words; NULL when none was named) and `event_date` (the calendar
122
+ // date the memory is ABOUT, as said). No SQL comment sits among them, on
123
+ // purpose: SQLite's `ALTER TABLE ... DROP COLUMN` fails with "incomplete
124
+ // input" on a last column preceded by `--` lines (measured building the v6
125
+ // fixture in `test/store-v7.test.ts`), and a future migration may need one.
96
126
  `CREATE TABLE IF NOT EXISTS memories (
97
127
  id TEXT PRIMARY KEY,
98
128
  type TEXT NOT NULL,
@@ -135,7 +165,11 @@ const DDL: readonly string[] = [
135
165
  -- The one key promoted OUT of that JSON, because it is a GATE: a gate that
136
166
  -- must parse JSON on every read is a gate that will one day fail open
137
167
  -- (confidentialByMeta, the one truth table, computes it at every write).
138
- confidential INTEGER NOT NULL DEFAULT 0
168
+ confidential INTEGER NOT NULL DEFAULT 0,
169
+ created_at INTEGER,
170
+ updated_at INTEGER,
171
+ model TEXT,
172
+ event_date TEXT
139
173
  )`,
140
174
  `CREATE INDEX IF NOT EXISTS memories_band ON memories (band, archived)`,
141
175
  `CREATE INDEX IF NOT EXISTS memories_kind ON memories (kind, archived)`,
@@ -158,6 +192,12 @@ const DDL: readonly string[] = [
158
192
  -- corrected date would have silently rewritten every version behind it.
159
193
  learned_on TEXT NOT NULL DEFAULT '',
160
194
  happened_on TEXT,
195
+ -- v7: the archived words' own moment (the head's updated_at, else its
196
+ -- created_at, when they were archived — archived_at is when they STOPPED
197
+ -- being current), the model that wrote them, and their event date.
198
+ created_at INTEGER,
199
+ model TEXT,
200
+ event_date TEXT,
161
201
  PRIMARY KEY (memory_id, seq)
162
202
  )`,
163
203
  `CREATE TABLE IF NOT EXISTS edges (
@@ -165,6 +205,9 @@ const DDL: readonly string[] = [
165
205
  dst TEXT NOT NULL REFERENCES memories(id),
166
206
  weight REAL NOT NULL,
167
207
  last_day INTEGER NOT NULL,
208
+ -- v7 moments: first linked, last re-weighted.
209
+ created_at INTEGER,
210
+ updated_at INTEGER,
168
211
  PRIMARY KEY (src, dst)
169
212
  )`,
170
213
  `CREATE TABLE IF NOT EXISTS prospective (
@@ -175,8 +218,34 @@ const DDL: readonly string[] = [
175
218
  state TEXT NOT NULL,
176
219
  fires INTEGER NOT NULL DEFAULT 0,
177
220
  last_fired_day INTEGER,
221
+ -- v7 moments: window first recorded, last changed state.
222
+ created_at INTEGER,
223
+ updated_at INTEGER,
178
224
  PRIMARY KEY (memory_id, window_key)
179
225
  )`,
226
+ // v7 (2026-09-25, owner-approved): FEELINGS ON A MEMORY, one row each —
227
+ // `store/feelings.ts` has the shape and the check. Content-bearing
228
+ // (`carried_by`, `other_word`), so the owner's removal DELETES a memory's
229
+ // rows, like its edges and windows. `beneath_id` points at another feeling on
230
+ // the same memory (anger over fear). Moments are NOT NULL here: the table is
231
+ // new, so no row predates them.
232
+ `CREATE TABLE IF NOT EXISTS feelings (
233
+ id TEXT PRIMARY KEY,
234
+ memory_id TEXT NOT NULL REFERENCES memories(id),
235
+ whose TEXT NOT NULL,
236
+ core TEXT NOT NULL,
237
+ emotion TEXT NOT NULL,
238
+ other_word TEXT,
239
+ strength REAL NOT NULL,
240
+ beneath_id TEXT REFERENCES feelings(id),
241
+ carried_by TEXT NOT NULL DEFAULT '',
242
+ model TEXT,
243
+ created_at INTEGER NOT NULL,
244
+ updated_at INTEGER NOT NULL
245
+ )`,
246
+ `CREATE INDEX IF NOT EXISTS feelings_memory ON feelings (memory_id)`,
247
+ `CREATE INDEX IF NOT EXISTS feelings_whose_core ON feelings (whose, core)`,
248
+ `CREATE INDEX IF NOT EXISTS feelings_whose_emotion ON feelings (whose, emotion)`,
180
249
  // SEAMS item B — per-session gate state, ONE ROW PER RECORD.
181
250
  //
182
251
  // It replaces `recall/`'s `meta` row at `recall.gate.<sessionId>`, which was a
@@ -311,6 +380,13 @@ export interface MemoryRow extends Row {
311
380
  /** `confidentialByMeta(meta)` at the last write. A column, never re-derived
312
381
  * per call site: the confidentiality class is a gate (CONTRACT §5 G13). */
313
382
  confidential: number;
383
+ /** v7 moments (UTC ms). NULL on a row written before v7. */
384
+ created_at: number | null;
385
+ updated_at: number | null;
386
+ /** v7: the model that wrote the current words; NULL = not a named model. */
387
+ model: string | null;
388
+ /** v7: the calendar date the memory is about, as said (`time.ts`). */
389
+ event_date: string | null;
314
390
  }
315
391
 
316
392
  export interface VersionRow extends Row {
@@ -326,6 +402,9 @@ export interface VersionRow extends Row {
326
402
  meta: string;
327
403
  learned_on: string;
328
404
  happened_on: string | null;
405
+ created_at: number | null;
406
+ model: string | null;
407
+ event_date: string | null;
329
408
  }
330
409
 
331
410
  export interface EdgeRow extends Row {
@@ -333,6 +412,8 @@ export interface EdgeRow extends Row {
333
412
  dst: string;
334
413
  weight: number;
335
414
  last_day: number;
415
+ created_at: number | null;
416
+ updated_at: number | null;
336
417
  }
337
418
 
338
419
  export interface ProspectiveRow extends Row {
@@ -343,6 +424,8 @@ export interface ProspectiveRow extends Row {
343
424
  state: string;
344
425
  fires: number;
345
426
  last_fired_day: number | null;
427
+ created_at: number | null;
428
+ updated_at: number | null;
346
429
  }
347
430
 
348
431
  export interface GateSessionRow extends Row {
@@ -401,6 +484,29 @@ export interface OpenOperationalOptions {
401
484
  readonly initialize?: boolean;
402
485
  /** Written once, at creation only. Ignored for an already-initialized store. */
403
486
  readonly retentionDays?: number;
487
+ /**
488
+ * Where the copy taken before a migration goes. Absent ⇒ the default beside
489
+ * the store (`defaultSnapshotsDir`); a store with neither refuses to migrate.
490
+ */
491
+ readonly snapshotsDir?: string;
492
+ /** Told once, after a migration committed, what was copied and where. */
493
+ readonly onMigrated?: (note: MigrationNote) => void;
494
+ /**
495
+ * The person's calendar day at this open (`YYYY-MM-DD`), for a migration
496
+ * that moves dates onto the local calendar. v7 clamps `lastActiveDate` to it.
497
+ */
498
+ readonly localToday?: string;
499
+ }
500
+
501
+ /** What the open did to the schema, for the caller's record. */
502
+ export interface MigrationNote {
503
+ readonly from: string;
504
+ readonly to: number;
505
+ /** The pre-migration copy's name inside `dir`. */
506
+ readonly snapshot: string;
507
+ /** True when a copy an earlier, failed attempt took was used again. */
508
+ readonly reused: boolean;
509
+ readonly dir: string;
404
510
  }
405
511
 
406
512
  /**
@@ -455,6 +561,34 @@ export function isPreRowsDatabase(path: string): boolean {
455
561
  }
456
562
  }
457
563
 
564
+ /**
565
+ * Is the database at `path` on an older schema that the next writer open will
566
+ * migrate (and copy first)? `{ found }` when it is; null when it is absent,
567
+ * current, ahead, unreadable, or a pre-rows database (refused, never migrated).
568
+ * One short read on its own connection; nothing is written.
569
+ */
570
+ export function pendingMigration(path: string): { found: string; expected: number } | null {
571
+ if (!existsSync(path)) return null;
572
+ let db;
573
+ try {
574
+ db = openDb(path, { wal: false });
575
+ const found = readSchemaVersion(db);
576
+ if (found === null || found === String(SCHEMA_VERSION)) return null;
577
+ const n = Number.parseInt(found, 10);
578
+ if (Number.isFinite(n) && n > SCHEMA_VERSION) return null;
579
+ if (!hasBodyColumn(db)) return null;
580
+ return { found, expected: SCHEMA_VERSION };
581
+ } catch {
582
+ return null;
583
+ } finally {
584
+ try {
585
+ db?.close();
586
+ } catch {
587
+ /* nothing to say about a handle that will not close */
588
+ }
589
+ }
590
+ }
591
+
458
592
  /** The schema version recorded in the file, or null if there is not one yet. */
459
593
  function readSchemaVersion(db: Db): string | null {
460
594
  try {
@@ -531,20 +665,91 @@ export function openOperational(path: string, opts: OpenOperationalOptions = {})
531
665
  readableBy: PRE_ROWS_READABLE_BY,
532
666
  });
533
667
  }
534
- db.transaction(() => {
535
- for (const sql of DDL) db.exec(sql);
536
- ensureAddedColumns(db);
537
- const put = db.prepare("INSERT OR IGNORE INTO meta (key, value) VALUES (?, ?)");
538
- put.run("livedDay", "0");
539
- put.run("lastActiveDate", "");
540
- put.run("retentionDays", String(opts.retentionDays ?? DEFAULT_RETENTION_DAYS));
541
- // Last, and REPLACE not IGNORE: the version row is the latch the next open
542
- // reads, so it must be written only after the DDL it describes has run.
543
- db.run("INSERT OR REPLACE INTO meta (key, value) VALUES ('schemaVersion', ?)", String(SCHEMA_VERSION));
544
- });
668
+ let note = null as MigrationNote | null;
669
+ try {
670
+ db.transaction(() => {
671
+ // THE CLAIM. `transaction` is BEGIN IMMEDIATE, so this connection now holds
672
+ // the write lock; the version is read again under it. Of several processes
673
+ // opening an old store at once, the first through here copies and
674
+ // migrates, and the rest find it current and do neither.
675
+ const now = readSchemaVersion(db);
676
+ if (now === String(SCHEMA_VERSION)) return;
677
+ if (now !== null && Number.parseInt(now, 10) > SCHEMA_VERSION) {
678
+ throw new StoreError("SCHEMA_AHEAD", { path, expected: SCHEMA_VERSION, found: now });
679
+ }
680
+ // A store with a version is about to change shape: copy it first. A
681
+ // fresh file (no version) has nothing to lose. VACUUM INTO runs on its
682
+ // own connection, which may read while this one holds the lock, and
683
+ // sees the store as it stands before the DDL below.
684
+ if (now !== null) note = copyBeforeMigrating(path, now, opts);
685
+ for (const sql of DDL) db.exec(sql);
686
+ ensureAddedColumns(db);
687
+ for (const sql of DDL_AFTER_COLUMNS) db.exec(sql);
688
+ // v7 (review S1): `lastActiveDate` was a UTC date and becomes a local
689
+ // one. West of UTC, an evening's last boundary under the old build wrote
690
+ // UTC's TOMORROW, and the first local date after the upgrade would read
691
+ // as the clock going backwards. Clamped to the person's today, once, as
692
+ // the calendar changes. At most one lived day is counted twice (the
693
+ // evening already advanced it); decay does not notice one day.
694
+ if (now !== null && Number.parseInt(now, 10) < 7 && opts.localToday !== undefined && opts.localToday !== "") {
695
+ db.run(
696
+ "UPDATE meta SET value = ? WHERE key = 'lastActiveDate' AND value > ?",
697
+ opts.localToday,
698
+ opts.localToday,
699
+ );
700
+ }
701
+ const put = db.prepare("INSERT OR IGNORE INTO meta (key, value) VALUES (?, ?)");
702
+ put.run("livedDay", "0");
703
+ put.run("lastActiveDate", "");
704
+ put.run("retentionDays", String(opts.retentionDays ?? DEFAULT_RETENTION_DAYS));
705
+ // Last, and REPLACE not IGNORE: the version row is the latch the next open
706
+ // reads, so it must be written only after the DDL it describes has run.
707
+ db.run("INSERT OR REPLACE INTO meta (key, value) VALUES ('schemaVersion', ?)", String(SCHEMA_VERSION));
708
+ });
709
+ } catch (err) {
710
+ db.close();
711
+ throw err;
712
+ }
713
+ if (note !== null) opts.onMigrated?.(note);
545
714
  return db;
546
715
  }
547
716
 
717
+ /**
718
+ * The copy before a migration, or the refusal that stops it. Throws
719
+ * `MIGRATION_SNAPSHOT_FAILED` inside the transaction, which rolls back: the
720
+ * store stays on `found` and the build that wrote it still opens it.
721
+ */
722
+ function copyBeforeMigrating(path: string, found: string, opts: OpenOperationalOptions): MigrationNote {
723
+ const dir = preMigrationDir(path, opts.snapshotsDir);
724
+ try {
725
+ const copy = snapshotBeforeMigration({ dbPath: path, dir, from: found, to: SCHEMA_VERSION });
726
+ return { from: found, to: SCHEMA_VERSION, snapshot: copy.name, reused: copy.reused, dir: dir as string };
727
+ } catch (err) {
728
+ const reason = String((err as Error).message ?? err);
729
+ throw new StoreError("MIGRATION_SNAPSHOT_FAILED", {
730
+ path,
731
+ found,
732
+ expected: SCHEMA_VERSION,
733
+ dir,
734
+ reason,
735
+ remedy:
736
+ `This store is on schema v${found} and this build needs v${SCHEMA_VERSION}, but a copy of it could not ` +
737
+ `be saved first (${reason}), so nothing was changed and the previous build still opens it; ` +
738
+ `fix that and open it again.`,
739
+ });
740
+ }
741
+ }
742
+
743
+ /**
744
+ * Indexes over columns that `ADDED_COLUMNS` may have just added, so they run
745
+ * AFTER it: on a v6 store `event_date` does not exist while `DDL` runs.
746
+ */
747
+ const DDL_AFTER_COLUMNS: readonly string[] = [
748
+ // v7: `Store.datedMemories` without a scan (prospective/INTERFACE-GAPS §2).
749
+ // Partial, because almost no memory carries one.
750
+ `CREATE INDEX IF NOT EXISTS memories_event_date ON memories (event_date) WHERE event_date IS NOT NULL`,
751
+ ];
752
+
548
753
  /**
549
754
  * Columns added to a table AFTER it first shipped. `CREATE TABLE IF NOT EXISTS`
550
755
  * cannot grow an existing table, so a pre-existing store gains these here —
@@ -554,15 +759,28 @@ export function openOperational(path: string, opts: OpenOperationalOptions = {})
554
759
  * asserts table_info equality between a fresh open and a migrated one).
555
760
  */
556
761
  export const ADDED_COLUMNS: readonly { table: string; column: string; ddl: string }[] = [
557
- // EMPTY at v6, and that is the floor's safety rule rather than an accident.
558
- //
559
- // A pre-v6 store is refused by name before anything opens it, so nothing here
560
- // can reach one — and the v6 columns must NOT be listed: `body` added through
561
- // this path to a v5 store would be NULL on every row while the words sat in
562
- // files this build cannot see, and the store would be stamped v6 and
563
- // unreadable by the build that can (`SCHEMA_VERSION` above, `STORE_PRE_ROWS`).
762
+ // The v6 columns are NOT listed, and that is the floor's safety rule rather
763
+ // than an accident. A pre-v6 store is refused by name before anything opens
764
+ // it, so nothing here can reach one — and `body` added through this path to a
765
+ // v5 store would be NULL on every row while the words sat in files this build
766
+ // cannot see (`SCHEMA_VERSION` above, `STORE_PRE_ROWS`).
564
767
  //
565
- // The mechanism stays for whatever v7 adds additively to a v6 store.
768
+ // v7 (2026-09-25): additive, nullable, no default — `ALTER TABLE ADD COLUMN`
769
+ // cannot add NOT NULL without one, and a default would put a made-up moment
770
+ // on every old row. Rows written before carry NULL (docs/time.md rule 6).
771
+ // The fresh-CREATE DDL above lists the same columns in the same order, so
772
+ // both paths converge on one `table_info`.
773
+ { table: "memories", column: "created_at", ddl: "ALTER TABLE memories ADD COLUMN created_at INTEGER" },
774
+ { table: "memories", column: "updated_at", ddl: "ALTER TABLE memories ADD COLUMN updated_at INTEGER" },
775
+ { table: "memories", column: "model", ddl: "ALTER TABLE memories ADD COLUMN model TEXT" },
776
+ { table: "memories", column: "event_date", ddl: "ALTER TABLE memories ADD COLUMN event_date TEXT" },
777
+ { table: "versions", column: "created_at", ddl: "ALTER TABLE versions ADD COLUMN created_at INTEGER" },
778
+ { table: "versions", column: "model", ddl: "ALTER TABLE versions ADD COLUMN model TEXT" },
779
+ { table: "versions", column: "event_date", ddl: "ALTER TABLE versions ADD COLUMN event_date TEXT" },
780
+ { table: "edges", column: "created_at", ddl: "ALTER TABLE edges ADD COLUMN created_at INTEGER" },
781
+ { table: "edges", column: "updated_at", ddl: "ALTER TABLE edges ADD COLUMN updated_at INTEGER" },
782
+ { table: "prospective", column: "created_at", ddl: "ALTER TABLE prospective ADD COLUMN created_at INTEGER" },
783
+ { table: "prospective", column: "updated_at", ddl: "ALTER TABLE prospective ADD COLUMN updated_at INTEGER" },
566
784
  ];
567
785
 
568
786
  function ensureAddedColumns(db: Db): void {
@@ -190,6 +190,10 @@ export function chaseRemoved(store: Store, id: string): ChaseReport {
190
190
  // Rows that carry nothing anyone else points at: they simply go.
191
191
  db.run("DELETE FROM edges WHERE src = ? OR dst = ?", id, id);
192
192
  db.run("DELETE FROM prospective WHERE memory_id = ?", id);
193
+ // v7: a feeling carries words about the moment (`carried_by`), so it goes
194
+ // with the memory. One statement, so a `beneath_id` between two of them is
195
+ // never left dangling mid-delete.
196
+ db.run("DELETE FROM feelings WHERE memory_id = ?", id);
193
197
  db.run("DELETE FROM gate_session WHERE ref = ?", id);
194
198
 
195
199
  // Version rows stay (a successor's predecessor pointer lives here) and lose
@@ -200,7 +204,8 @@ export function chaseRemoved(store: Store, id: string): ChaseReport {
200
204
  db.run(
201
205
  `UPDATE versions
202
206
  SET title = NULL, body = '', meta = '{}', content_hash = '',
203
- learned_on = '', happened_on = NULL
207
+ learned_on = '', happened_on = NULL,
208
+ created_at = NULL, model = NULL, event_date = NULL
204
209
  WHERE memory_id = ?`,
205
210
  id,
206
211
  );
@@ -222,7 +227,8 @@ export function chaseRemoved(store: Store, id: string): ChaseReport {
222
227
  protected = 0, pressure = 0, last_challenged_day = NULL,
223
228
  archived = 1, archived_reason = ?, content_hash = '',
224
229
  title = NULL, body = '', meta = '{}', confidential = 0,
225
- learned_on = '', happened_on = NULL
230
+ learned_on = '', happened_on = NULL,
231
+ created_at = NULL, updated_at = NULL, model = NULL, event_date = NULL
226
232
  WHERE id = ?`,
227
233
  REMOVED_REASON,
228
234
  id,
@@ -387,7 +393,11 @@ export function unarchiveMerged(store: Store, id: string): UnmergeReport {
387
393
  // door being honest by itself.
388
394
  if (noop) return { id, record, noop };
389
395
 
390
- db.run("UPDATE memories SET archived = 0, archived_reason = NULL WHERE id = ?", id);
396
+ db.run(
397
+ "UPDATE memories SET archived = 0, archived_reason = NULL, updated_at = ? WHERE id = ?",
398
+ store.now(),
399
+ id,
400
+ );
391
401
  // Recorded inside the same transaction, and latched on the id, so a second
392
402
  // `--apply` over the same store writes nothing at all (§5 G3).
393
403
  store.appendEvent({
@@ -6,7 +6,7 @@
6
6
  */
7
7
  import { existsSync, readdirSync, statSync } from "node:fs";
8
8
  import { homedir } from "node:os";
9
- import { join, resolve, relative, isAbsolute } from "node:path";
9
+ import { basename, dirname, join, resolve, relative, isAbsolute } from "node:path";
10
10
  import { StoreError, isStoreError } from "./errors.js";
11
11
 
12
12
  export const DATA_DIR_ENV = "COUNTERPARTS_DATA_DIR";
@@ -46,6 +46,20 @@ export const DEFAULT_DATA_DIR_NAME = ".counterparts";
46
46
  */
47
47
  export const DEFAULT_STORE_SUBDIR = "store";
48
48
 
49
+ /** The directory the snapshots live in, beside the store rather than inside it. */
50
+ export const SNAPSHOTS_DIR_NAME = "snapshots";
51
+
52
+ /**
53
+ * The default snapshots directory for a store: a sibling of it, but only inside
54
+ * the layout this package creates (`<base>/store` → `<base>/snapshots`). A store
55
+ * somebody pointed at an arbitrary directory has no default, and null says so.
56
+ */
57
+ export function defaultSnapshotsDir(storeDir: string): string | null {
58
+ const store = resolve(storeDir);
59
+ if (basename(store) !== DEFAULT_STORE_SUBDIR) return null;
60
+ return join(dirname(store), SNAPSHOTS_DIR_NAME);
61
+ }
62
+
49
63
  /**
50
64
  * The canonical database's file name (owner ruling 5, 2026-09-18).
51
65
  *