@remnic/core 10.0.110 → 10.0.111

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 (176) hide show
  1. package/dist/access-admin-ops-surface.js +51 -50
  2. package/dist/access-cli.js +51 -50
  3. package/dist/access-cli.js.map +1 -1
  4. package/dist/access-extraction-force-flush.js +51 -50
  5. package/dist/access-http-briefing.js +51 -50
  6. package/dist/access-http-query.js +51 -50
  7. package/dist/access-http.js +51 -50
  8. package/dist/access-identity-continuity-surface.js +51 -50
  9. package/dist/access-lcm-surface.js +51 -50
  10. package/dist/access-mcp.js +51 -50
  11. package/dist/access-observe-write-surface.js +51 -50
  12. package/dist/access-operations-batch.js +51 -50
  13. package/dist/access-operations.js +51 -50
  14. package/dist/access-recall-concurrency.js +51 -50
  15. package/dist/access-recall-response.js +51 -50
  16. package/dist/access-recall-surface.js +51 -50
  17. package/dist/access-service.js +51 -50
  18. package/dist/active-recall.js +9 -9
  19. package/dist/adapters/index.js +4 -4
  20. package/dist/adapters/registry.js +2 -2
  21. package/dist/briefing.js +10 -10
  22. package/dist/causal-consolidation.js +11 -11
  23. package/dist/{chunk-7HNQR5MV.js → chunk-25JBIXAV.js} +3 -3
  24. package/dist/{chunk-7SW6EBEF.js → chunk-27YNNLRB.js} +2 -2
  25. package/dist/{chunk-E55J2NFL.js → chunk-477SVZUZ.js} +2 -2
  26. package/dist/{chunk-7ZEF43SR.js → chunk-6DUBULUV.js} +3 -3
  27. package/dist/{chunk-63OWJXGW.js → chunk-6YXIL26Y.js} +2 -2
  28. package/dist/{chunk-GT2TH7FZ.js → chunk-AAF7RX22.js} +3 -3
  29. package/dist/{chunk-B3LGXOM5.js → chunk-AJCXPMDA.js} +2 -2
  30. package/dist/{chunk-5VWN3FJS.js → chunk-BFRNDOHO.js} +2 -2
  31. package/dist/{chunk-K32VAS5B.js → chunk-BGM7O5TD.js} +2 -2
  32. package/dist/{chunk-FQACH7MC.js → chunk-BZAI3ETM.js} +2 -2
  33. package/dist/{chunk-UOIVXBNS.js → chunk-E5DYZM56.js} +2 -2
  34. package/dist/{chunk-WQ4XKIOX.js → chunk-F32RH72R.js} +45 -37
  35. package/dist/chunk-F32RH72R.js.map +1 -0
  36. package/dist/{chunk-WCKLRFGF.js → chunk-F6GNH5UY.js} +2 -2
  37. package/dist/{chunk-DLZGSZVC.js → chunk-FYQUUIPI.js} +4 -4
  38. package/dist/{chunk-XTN54NML.js → chunk-G3T6YMBO.js} +4 -4
  39. package/dist/{chunk-AY535ZIM.js → chunk-G7QANNRD.js} +2 -2
  40. package/dist/chunk-GE5J2T67.js +100 -0
  41. package/dist/chunk-GE5J2T67.js.map +1 -0
  42. package/dist/{chunk-H4XPEXWA.js → chunk-GG3QMQEG.js} +2 -2
  43. package/dist/{chunk-FVMEZ57K.js → chunk-HWMIXG3J.js} +10 -10
  44. package/dist/{chunk-LYR57GEF.js → chunk-I5PCMNN5.js} +3 -3
  45. package/dist/{chunk-QIGEWFXE.js → chunk-INXS64LQ.js} +2 -2
  46. package/dist/{chunk-BP7LNNWG.js → chunk-IOTMEQUC.js} +2 -2
  47. package/dist/{chunk-S2HLHAZ4.js → chunk-L76BQSWB.js} +10 -2
  48. package/dist/chunk-L76BQSWB.js.map +1 -0
  49. package/dist/{chunk-7QIRIF3Z.js → chunk-LFPGCYXE.js} +2 -2
  50. package/dist/{chunk-YD4D4EVO.js → chunk-MPIMRX3I.js} +2 -2
  51. package/dist/{chunk-PFUXPTBT.js → chunk-P3AQ2YSX.js} +4 -4
  52. package/dist/{chunk-ZG4GNPRE.js → chunk-PBE7NFB5.js} +5 -5
  53. package/dist/{chunk-FV2B6XE6.js → chunk-R3LYKUZH.js} +2 -2
  54. package/dist/{chunk-OIOKDTZU.js → chunk-SZXDGB32.js} +17 -6
  55. package/dist/chunk-SZXDGB32.js.map +1 -0
  56. package/dist/{chunk-BUOP3DOK.js → chunk-TR4HFZBD.js} +2 -2
  57. package/dist/{chunk-JODSN3V5.js → chunk-UY3JSQ7H.js} +2 -2
  58. package/dist/{chunk-4TYIHHPW.js → chunk-WDAIXMEU.js} +73 -60
  59. package/dist/{chunk-4TYIHHPW.js.map → chunk-WDAIXMEU.js.map} +1 -1
  60. package/dist/{chunk-VLUWWPKO.js → chunk-X3HLHS5E.js} +7 -7
  61. package/dist/{chunk-AOAREXFV.js → chunk-XSFDRM2P.js} +2 -2
  62. package/dist/{chunk-DOJBV6WR.js → chunk-YK6SFDP2.js} +4 -4
  63. package/dist/{chunk-OLJYBMFW.js → chunk-YQ7H7P4Y.js} +7 -7
  64. package/dist/{chunk-IOGYSADY.js → chunk-YTNLLRHQ.js} +2 -2
  65. package/dist/{chunk-EDVVW655.js → chunk-Z4NOA733.js} +2 -2
  66. package/dist/{chunk-AZYUG2HW.js → chunk-Z64ANMII.js} +2 -2
  67. package/dist/{chunk-OEVJH66T.js → chunk-ZAMVB4JK.js} +3 -3
  68. package/dist/cli.js +51 -50
  69. package/dist/coding/export-okf-codegraph.js +9 -9
  70. package/dist/compounding/engine.js +11 -11
  71. package/dist/config.js +9 -9
  72. package/dist/connectors/codex-materialize-runner.js +10 -10
  73. package/dist/connectors/index.js +10 -10
  74. package/dist/convergence-refresh.js +12 -12
  75. package/dist/corpus-watermark.js +12 -12
  76. package/dist/entity-retrieval.js +10 -10
  77. package/dist/explicit-capture.js +51 -50
  78. package/dist/extraction.js +10 -10
  79. package/dist/harmonic-retrieval.js +10 -10
  80. package/dist/index.js +72 -71
  81. package/dist/lcm/engine.js +2 -2
  82. package/dist/lcm/index.js +5 -5
  83. package/dist/location/index.js +11 -11
  84. package/dist/maintenance/memory-governance.js +10 -10
  85. package/dist/maintenance/rebuild-memory-lifecycle-ledger.js +10 -10
  86. package/dist/maintenance/rebuild-memory-projection-runner.js +12 -12
  87. package/dist/maintenance/rebuild-memory-projection.js +11 -11
  88. package/dist/memory-snapshot.d.ts +7 -1
  89. package/dist/memory-snapshot.d.ts.map +1 -1
  90. package/dist/memory-snapshot.js +5 -3
  91. package/dist/namespaces/migrate.js +11 -11
  92. package/dist/namespaces/search.js +7 -7
  93. package/dist/namespaces/storage.js +10 -10
  94. package/dist/operator-doctor-corpus.js +13 -13
  95. package/dist/operator-toolkit.js +16 -16
  96. package/dist/orchestration/backlog-scheduler.js +52 -51
  97. package/dist/orchestration/backlog-scheduler.js.map +1 -1
  98. package/dist/orchestration/buffer-drain-runner.js +52 -51
  99. package/dist/orchestration/maintenance.js +18 -18
  100. package/dist/orchestration/recall-internal.d.ts.map +1 -1
  101. package/dist/orchestration/workspace-ops.d.ts.map +1 -1
  102. package/dist/orchestrator.js +51 -50
  103. package/dist/recall-navigation.js +51 -50
  104. package/dist/resume-bundles.js +12 -11
  105. package/dist/search/factory.js +6 -6
  106. package/dist/search/index.js +11 -11
  107. package/dist/search/lancedb-backend.js +2 -2
  108. package/dist/search/meilisearch-backend.js +2 -2
  109. package/dist/search/orama-backend.js +2 -2
  110. package/dist/semantic-consolidation.js +11 -11
  111. package/dist/semantic-rule-promotion.js +10 -10
  112. package/dist/semantic-rule-verifier.js +10 -10
  113. package/dist/source-agent-qualifier.js +10 -10
  114. package/dist/standup/build.js +10 -10
  115. package/dist/storage.d.ts +3 -0
  116. package/dist/storage.d.ts.map +1 -1
  117. package/dist/storage.js +9 -9
  118. package/dist/transcript-recent.d.ts +22 -0
  119. package/dist/transcript-recent.d.ts.map +1 -0
  120. package/dist/transcript-recent.js +15 -0
  121. package/dist/transcript-recent.js.map +1 -0
  122. package/dist/transcript.d.ts +11 -3
  123. package/dist/transcript.d.ts.map +1 -1
  124. package/dist/transcript.js +2 -1
  125. package/dist/transfer/export-okf.js +10 -10
  126. package/dist/verified-recall.js +10 -10
  127. package/dist/who-knows-cli.js +11 -11
  128. package/dist/who-knows.js +10 -10
  129. package/package.json +2 -2
  130. package/src/memory-snapshot.ts +14 -1
  131. package/src/orchestration/recall-internal.ts +1 -4
  132. package/src/orchestration/workspace-ops.test.ts +113 -0
  133. package/src/orchestration/workspace-ops.ts +38 -12
  134. package/src/storage-contract/lazy-memory-snapshot.test.ts +194 -0
  135. package/src/storage.ts +15 -4
  136. package/src/transcript-recent-tail.test.ts +355 -0
  137. package/src/transcript-recent.ts +129 -0
  138. package/src/transcript.ts +47 -47
  139. package/dist/chunk-OIOKDTZU.js.map +0 -1
  140. package/dist/chunk-S2HLHAZ4.js.map +0 -1
  141. package/dist/chunk-WQ4XKIOX.js.map +0 -1
  142. /package/dist/{chunk-7HNQR5MV.js.map → chunk-25JBIXAV.js.map} +0 -0
  143. /package/dist/{chunk-7SW6EBEF.js.map → chunk-27YNNLRB.js.map} +0 -0
  144. /package/dist/{chunk-E55J2NFL.js.map → chunk-477SVZUZ.js.map} +0 -0
  145. /package/dist/{chunk-7ZEF43SR.js.map → chunk-6DUBULUV.js.map} +0 -0
  146. /package/dist/{chunk-63OWJXGW.js.map → chunk-6YXIL26Y.js.map} +0 -0
  147. /package/dist/{chunk-GT2TH7FZ.js.map → chunk-AAF7RX22.js.map} +0 -0
  148. /package/dist/{chunk-B3LGXOM5.js.map → chunk-AJCXPMDA.js.map} +0 -0
  149. /package/dist/{chunk-5VWN3FJS.js.map → chunk-BFRNDOHO.js.map} +0 -0
  150. /package/dist/{chunk-K32VAS5B.js.map → chunk-BGM7O5TD.js.map} +0 -0
  151. /package/dist/{chunk-FQACH7MC.js.map → chunk-BZAI3ETM.js.map} +0 -0
  152. /package/dist/{chunk-UOIVXBNS.js.map → chunk-E5DYZM56.js.map} +0 -0
  153. /package/dist/{chunk-WCKLRFGF.js.map → chunk-F6GNH5UY.js.map} +0 -0
  154. /package/dist/{chunk-DLZGSZVC.js.map → chunk-FYQUUIPI.js.map} +0 -0
  155. /package/dist/{chunk-XTN54NML.js.map → chunk-G3T6YMBO.js.map} +0 -0
  156. /package/dist/{chunk-AY535ZIM.js.map → chunk-G7QANNRD.js.map} +0 -0
  157. /package/dist/{chunk-H4XPEXWA.js.map → chunk-GG3QMQEG.js.map} +0 -0
  158. /package/dist/{chunk-FVMEZ57K.js.map → chunk-HWMIXG3J.js.map} +0 -0
  159. /package/dist/{chunk-LYR57GEF.js.map → chunk-I5PCMNN5.js.map} +0 -0
  160. /package/dist/{chunk-QIGEWFXE.js.map → chunk-INXS64LQ.js.map} +0 -0
  161. /package/dist/{chunk-BP7LNNWG.js.map → chunk-IOTMEQUC.js.map} +0 -0
  162. /package/dist/{chunk-7QIRIF3Z.js.map → chunk-LFPGCYXE.js.map} +0 -0
  163. /package/dist/{chunk-YD4D4EVO.js.map → chunk-MPIMRX3I.js.map} +0 -0
  164. /package/dist/{chunk-PFUXPTBT.js.map → chunk-P3AQ2YSX.js.map} +0 -0
  165. /package/dist/{chunk-ZG4GNPRE.js.map → chunk-PBE7NFB5.js.map} +0 -0
  166. /package/dist/{chunk-FV2B6XE6.js.map → chunk-R3LYKUZH.js.map} +0 -0
  167. /package/dist/{chunk-BUOP3DOK.js.map → chunk-TR4HFZBD.js.map} +0 -0
  168. /package/dist/{chunk-JODSN3V5.js.map → chunk-UY3JSQ7H.js.map} +0 -0
  169. /package/dist/{chunk-VLUWWPKO.js.map → chunk-X3HLHS5E.js.map} +0 -0
  170. /package/dist/{chunk-AOAREXFV.js.map → chunk-XSFDRM2P.js.map} +0 -0
  171. /package/dist/{chunk-DOJBV6WR.js.map → chunk-YK6SFDP2.js.map} +0 -0
  172. /package/dist/{chunk-OLJYBMFW.js.map → chunk-YQ7H7P4Y.js.map} +0 -0
  173. /package/dist/{chunk-IOGYSADY.js.map → chunk-YTNLLRHQ.js.map} +0 -0
  174. /package/dist/{chunk-EDVVW655.js.map → chunk-Z4NOA733.js.map} +0 -0
  175. /package/dist/{chunk-AZYUG2HW.js.map → chunk-Z64ANMII.js.map} +0 -0
  176. /package/dist/{chunk-OEVJH66T.js.map → chunk-ZAMVB4JK.js.map} +0 -0
@@ -120,9 +120,35 @@ export interface WorkspaceOpsDeps {
120
120
  readonly meetingsServiceByNamespace: Map<string, MeetingsService>;
121
121
  }
122
122
 
123
- function matchesMemoryPath(candidatePath: string, requestedPath: string, memoryDir: string): boolean {
124
- const normalizedCandidate = path.resolve(candidatePath);
125
- return normalizedCandidate === path.resolve(requestedPath) || normalizedCandidate === path.resolve(memoryDir, requestedPath);
123
+ /** A namespace's memories keyed for lookup: first index per resolved path, first memory per id (corpus order wins). */
124
+ interface MemoryIndex {
125
+ readonly list: MemoryFile[];
126
+ readonly byPath: Map<string, number>;
127
+ readonly byId: Map<string, MemoryFile>;
128
+ }
129
+
130
+ // COS-666e: the old per-update `find` over the corpus resolved paths on every comparison (a 40 s stall at 130k memories).
131
+ function indexMemories(list: MemoryFile[]): MemoryIndex {
132
+ const byPath = new Map<string, number>();
133
+ const byId = new Map<string, MemoryFile>();
134
+ list.forEach((memory, i) => {
135
+ const resolved = path.resolve(memory.path);
136
+ if (!byPath.has(resolved)) byPath.set(resolved, i);
137
+ if (!byId.has(memory.frontmatter.id)) byId.set(memory.frontmatter.id, memory);
138
+ });
139
+ return { list, byPath, byId };
140
+ }
141
+
142
+ /** The first memory, in corpus order, whose path is a requested path as given or relative to the memory dir. */
143
+ function findByRequestedPaths(index: MemoryIndex, requestedPaths: string[], memoryDir: string): MemoryFile | undefined {
144
+ let best: number | undefined;
145
+ for (const requested of requestedPaths) {
146
+ for (const key of [path.resolve(requested), path.resolve(memoryDir, requested)]) {
147
+ const i = index.byPath.get(key);
148
+ if (i !== undefined && (best === undefined || i < best)) best = i;
149
+ }
150
+ }
151
+ return best === undefined ? undefined : index.list[best];
126
152
  }
127
153
 
128
154
  function accessTrackingPathCandidates(storageDir: string, memoryPath: string): string[] {
@@ -1016,6 +1042,7 @@ export class WorkspaceOpsCoordinator {
1016
1042
  const entriesByNamespace = new Map<string, AccessTrackingEntry[]>();
1017
1043
  const mergedEntriesByNamespace = new Map<string, Map<string, AccessTrackingEntry>>();
1018
1044
  const storageDirByNamespace = new Map<string, string | null>();
1045
+ const indexByNamespace = new Map<string, MemoryIndex>();
1019
1046
  for (const [, update] of this.deps.accessTrackingBuffer) {
1020
1047
  const memoryPath = update.memoryPath;
1021
1048
  const namespace =
@@ -1036,16 +1063,15 @@ export class WorkspaceOpsCoordinator {
1036
1063
  ...(storageDir ? accessTrackingPathCandidates(storageDir, memoryPath) : []),
1037
1064
  ]
1038
1065
  : [];
1066
+ let index = indexByNamespace.get(namespace);
1067
+ if (index === undefined && namespaceMemories) {
1068
+ index = indexMemories(namespaceMemories);
1069
+ indexByNamespace.set(namespace, index);
1070
+ }
1039
1071
  const memory = memoryPath
1040
- ? namespaceMemories?.find((candidate) =>
1041
- matchesMemoryPath(candidate.path, memoryPath, this.deps.config.memoryDir),
1042
- ) ??
1043
- namespaceMemories?.find((candidate) =>
1044
- requestedPaths.some((requestedPath) =>
1045
- matchesMemoryPath(candidate.path, requestedPath, this.deps.config.memoryDir),
1046
- ),
1047
- )
1048
- : namespaceMemories?.find((candidate) => candidate.frontmatter.id === update.memoryId);
1072
+ ? (index && (findByRequestedPaths(index, [memoryPath], this.deps.config.memoryDir) ??
1073
+ findByRequestedPaths(index, requestedPaths, this.deps.config.memoryDir)))
1074
+ : index?.byId.get(update.memoryId);
1049
1075
  if (!memory) continue;
1050
1076
 
1051
1077
  const namespaceEntries = mergedEntriesByNamespace.get(namespace) ?? new Map();
@@ -0,0 +1,194 @@
1
+ /**
2
+ * COS-666d: loading the corpus built a key-sorted JSON snapshot of every memory
3
+ * (`createMemorySnapshot`), 5-6.6 s of synchronous main-thread work at ~120k
4
+ * facts on every cache rebuild, for a value only `moveMemoryToPath` reads. The
5
+ * snapshot is now derived from the raw file text the loader already keeps, at
6
+ * the moment a tier move needs it, and means the same thing: the memory as it
7
+ * was loaded.
8
+ */
9
+
10
+ import assert from "node:assert/strict";
11
+ import test, { mock } from "node:test";
12
+ import { mkdir, mkdtemp, readFile, rename, rm, writeFile } from "node:fs/promises";
13
+ import os from "node:os";
14
+ import path from "node:path";
15
+
16
+ import { StorageManager } from "../storage.js";
17
+ import type { MemoryFile } from "../types.js";
18
+ import { resetStaticCaches } from "./harness.js";
19
+
20
+ /** Calls to the snapshot serializer: the only JSON.stringify here that is given a replacer function. */
21
+ async function countSnapshotCalls<T>(fn: () => Promise<T>): Promise<{ result: T; calls: number }> {
22
+ const spy = mock.method(JSON, "stringify");
23
+ try {
24
+ const result = await fn();
25
+ return { result, calls: spy.mock.calls.filter((call) => call.arguments[1] instanceof Function).length };
26
+ } finally {
27
+ spy.mock.restore();
28
+ }
29
+ }
30
+
31
+ async function withStorage(run: (storage: StorageManager, dir: string) => Promise<void>): Promise<void> {
32
+ const dir = await mkdtemp(path.join(os.tmpdir(), "remnic-lazy-snapshot-"));
33
+ try {
34
+ resetStaticCaches();
35
+ const storage = new StorageManager(dir);
36
+ await storage.ensureDirectories();
37
+ await run(storage, dir);
38
+ } finally {
39
+ resetStaticCaches();
40
+ await rm(dir, { recursive: true, force: true });
41
+ }
42
+ }
43
+
44
+ test("reading the corpus builds no snapshots", async () => {
45
+ await withStorage(async (storage) => {
46
+ for (let i = 0; i < 12; i++) await storage.writeMemory("fact", `lazy snapshot fact ${i}`);
47
+ resetStaticCaches();
48
+ const { result, calls } = await countSnapshotCalls(() => storage.readAllMemories());
49
+ assert.equal(result.length, 12);
50
+ assert.equal(calls, 0, "no snapshot may be built while loading the corpus");
51
+ const byPath = await countSnapshotCalls(() => storage.readMemoryByPath(result[0].path));
52
+ assert.ok(byPath.result);
53
+ assert.equal(byPath.calls, 0);
54
+ });
55
+ });
56
+
57
+ test("a tier move still succeeds for a loaded memory the caller mutated, and builds the snapshot only then", async () => {
58
+ await withStorage(async (storage) => {
59
+ const { id } = await storage.writeMemory("fact", "original disk content");
60
+ const memory = (await storage.readAllMemories()).find((m) => m.frontmatter.id === id);
61
+ assert.ok(memory);
62
+ memory.content = "caller mutated unpersisted content";
63
+ const targetPath = storage.buildTierMemoryPath(memory, "cold");
64
+ const { result, calls } = await countSnapshotCalls(() => storage.moveMemoryToPath(memory, targetPath));
65
+ assert.equal(result, true);
66
+ assert.ok(calls > 0, "the move compares snapshots");
67
+ assert.ok((await readFile(targetPath, "utf8")).includes("original disk content"));
68
+ });
69
+ });
70
+
71
+ test("a tier move refuses when the file changed on disk since it was loaded", async () => {
72
+ await withStorage(async (storage) => {
73
+ const { id } = await storage.writeMemory("fact", "unmodified content");
74
+ const memory = (await storage.readAllMemories()).find((m) => m.frontmatter.id === id);
75
+ assert.ok(memory);
76
+ const disk = await readFile(memory.path, "utf8");
77
+ await writeFile(memory.path, disk.replace("unmodified content", "external edit on disk"));
78
+ await assert.rejects(
79
+ () => storage.moveMemoryToPath(memory, storage.buildTierMemoryPath(memory, "cold")),
80
+ /changed before its tier move/,
81
+ );
82
+ });
83
+ });
84
+
85
+ test("a tier move ignores a rewrite that only reorders frontmatter keys (the snapshot is key-order independent)", async () => {
86
+ await withStorage(async (storage) => {
87
+ const { id } = await storage.writeMemory("fact", "reordered frontmatter");
88
+ const memory = (await storage.readAllMemories()).find((m) => m.frontmatter.id === id);
89
+ assert.ok(memory);
90
+ const disk = await readFile(memory.path, "utf8");
91
+ const closing = disk.indexOf("\n---\n", 4);
92
+ assert.ok(disk.startsWith("---\n") && closing > 0, "memory file has frontmatter");
93
+ const front = disk.slice(4, closing);
94
+ const body = disk.slice(closing + 5);
95
+ await writeFile(memory.path, `---\n${front.split("\n").reverse().join("\n")}\n---\n${body}`);
96
+ assert.equal(await storage.moveMemoryToPath(memory, storage.buildTierMemoryPath(memory, "cold")), true);
97
+ });
98
+ });
99
+
100
+ test("a memory with no retained file text falls back to an eager snapshot", async () => {
101
+ await withStorage(async (storage) => {
102
+ const { id } = await storage.writeMemory("fact", "no raw text");
103
+ const loaded = (await storage.readAllMemories()).find((m) => m.frontmatter.id === id);
104
+ assert.ok(loaded);
105
+ // A handcrafted object has no retained raw document and was never loaded: the old fallback
106
+ // compares `frontmatter.updated` only, so the same state moves and a changed one is refused.
107
+ const copy: MemoryFile = { path: loaded.path, frontmatter: { ...loaded.frontmatter }, content: loaded.content };
108
+ assert.equal(await storage.moveMemoryToPath(copy, storage.buildTierMemoryPath(copy, "cold")), true);
109
+ });
110
+ });
111
+
112
+ test("a loaded archive-path memory whose file has no status still moves (loaded status is archived)", async () => {
113
+ await withStorage(async (storage, dir) => {
114
+ const { id } = await storage.writeMemory("fact", "archived without status");
115
+ const source = (await storage.readAllMemories()).find((m) => m.frontmatter.id === id);
116
+ assert.ok(source);
117
+ const archivedPath = path.join(dir, "archive", "2026-01-01", `${id}.md`);
118
+ await mkdir(path.dirname(archivedPath), { recursive: true });
119
+ await rename(source.path, archivedPath);
120
+ resetStaticCaches();
121
+ const memory = (await storage.readArchivedMemories()).find((m) => m.frontmatter.id === id);
122
+ assert.ok(memory);
123
+ assert.equal(memory.frontmatter.status, "archived");
124
+ assert.equal(await storage.moveMemoryToPath(memory, storage.buildTierMemoryPath(memory, "cold")), true);
125
+ });
126
+ });
127
+
128
+ test("a tier move ignores caller edits to a loaded memory's frontmatter, not just its content", async () => {
129
+ await withStorage(async (storage) => {
130
+ const { id } = await storage.writeMemory("fact", "frontmatter edited by caller", { tags: ["kept"] });
131
+ const memory = (await storage.readAllMemories()).find((m) => m.frontmatter.id === id);
132
+ assert.ok(memory);
133
+ memory.frontmatter.tags = [...memory.frontmatter.tags, "caller-only"];
134
+ memory.frontmatter.confidence = 0.01;
135
+ memory.frontmatter.created = "2000-01-01T00:00:00.000Z";
136
+ memory.frontmatter.updated = "2000-01-02T00:00:00.000Z";
137
+ assert.equal(await storage.moveMemoryToPath(memory, storage.buildTierMemoryPath(memory, "cold")), true);
138
+ });
139
+ });
140
+
141
+ test("a file with no created or updated is refused on every move, not only when the clock ticks", async () => {
142
+ await withStorage(async (storage) => {
143
+ const memories: MemoryFile[] = [];
144
+ for (let i = 0; i < 12; i++) {
145
+ const { id } = await storage.writeMemory("fact", `no timestamps ${i}`);
146
+ const file = (await storage.readAllMemories()).find((m) => m.frontmatter.id === id);
147
+ assert.ok(file);
148
+ const disk = await readFile(file.path, "utf8");
149
+ await writeFile(file.path, disk.replace(/^(created|updated): .*\n/gm, ""));
150
+ }
151
+ resetStaticCaches();
152
+ for (const m of await storage.readAllMemories()) if (m.content.startsWith("no timestamps")) memories.push(m);
153
+ assert.equal(memories.length, 12);
154
+ await new Promise((resolve) => setTimeout(resolve, 5)); // a fresh parse now stamps a later millisecond
155
+ for (const memory of memories) {
156
+ await assert.rejects(
157
+ () => storage.moveMemoryToPath(memory, storage.buildTierMemoryPath(memory, "cold")),
158
+ /changed before its tier move/,
159
+ );
160
+ }
161
+ });
162
+ });
163
+
164
+ test("a loaded memory under an entities path moves: the snapshot normalizes with the file's content", async () => {
165
+ await withStorage(async (storage, dir) => {
166
+ const { id } = await storage.writeMemory("fact", "**Type:** vendor\n\nan entity-like note");
167
+ const source = (await storage.readAllMemories()).find((m) => m.frontmatter.id === id);
168
+ assert.ok(source);
169
+ const entityPath = path.join(dir, "facts", "entities", `${id}.md`);
170
+ await mkdir(path.dirname(entityPath), { recursive: true });
171
+ await rename(source.path, entityPath);
172
+ resetStaticCaches();
173
+ const memory = (await storage.readAllMemories()).find((m) => m.frontmatter.id === id);
174
+ assert.ok(memory);
175
+ assert.ok(memory.frontmatter.tags.includes("vendor"), "loaded tags carry the type read from the content");
176
+ assert.equal(await storage.moveMemoryToPath(memory, storage.buildTierMemoryPath(memory, "cold")), true);
177
+ });
178
+ });
179
+
180
+ test("an indented created or updated key counts as present: caller edits to it do not refuse the move", async () => {
181
+ await withStorage(async (storage) => {
182
+ const { id } = await storage.writeMemory("fact", "indented timestamp keys");
183
+ const file = (await storage.readAllMemories()).find((m) => m.frontmatter.id === id);
184
+ assert.ok(file);
185
+ const disk = await readFile(file.path, "utf8");
186
+ await writeFile(file.path, disk.replace(/^(created|updated):/gm, " $1:"));
187
+ resetStaticCaches();
188
+ const memory = (await storage.readAllMemories()).find((m) => m.frontmatter.id === id);
189
+ assert.ok(memory);
190
+ memory.frontmatter.created = "2000-01-01T00:00:00.000Z";
191
+ memory.frontmatter.updated = "2000-01-02T00:00:00.000Z";
192
+ assert.equal(await storage.moveMemoryToPath(memory, storage.buildTierMemoryPath(memory, "cold")), true);
193
+ });
194
+ });
package/src/storage.ts CHANGED
@@ -22,7 +22,7 @@ import { selectArtifactMatches, type ArtifactSearchOptions } from "./artifact-se
22
22
  import path from "node:path";
23
23
  import { resolveSafeStoragePath } from "./storage-paths.js";
24
24
  import { log } from "./logger.js";
25
- import { createMemorySnapshot } from "./memory-snapshot.js";
25
+ import { createMemorySnapshot, loadTimeStamps } from "./memory-snapshot.js";
26
26
  import { assertMemoryFrontmatterId, warnProjectionFallback } from "./storage-guards.js";
27
27
  import { createTombstoneMigrationSourceContents } from "./storage/tombstone-migration-sources.js";
28
28
  import { MemoryReadStore, readWindowedMemories, type WindowedMemoryReadOptions, type WindowedMemoryReadResult } from "./storage/memory-read-store.js";
@@ -62,7 +62,7 @@ import {
62
62
  type EntityCanonicalIdMigrationHost,
63
63
  } from "./storage/entity-canonical-id-migration-adapter.js";
64
64
  import { EntityCanonicalIdMigrationRunner } from "./storage/entity-canonical-id-migration-runner.js";
65
- import { rememberRawFrontmatter } from "./storage/memory-frontmatter-metadata.js";
65
+ import { readRawMemoryDocument, rememberRawFrontmatter } from "./storage/memory-frontmatter-metadata.js";
66
66
  import { createMemoryEntityRefSerializer } from "./storage/memory-migration-serialization.js";
67
67
  import { readEntityAliasConfigSync } from "./storage/entity-alias-config.js";
68
68
  import { assertSafeEntityId } from "./storage/entity-id-safety.js";
@@ -4343,13 +4343,24 @@ export class StorageManager extends TombstoneBlockedCaptureIndexHost {
4343
4343
  return lineCount > threshold;
4344
4344
  }
4345
4345
 
4346
+ /** Loaded memories keep their file text, so the load-time snapshot is derived from it when a tier move needs it, not built per memory on every corpus read (COS-666d). */
4346
4347
  private rememberMemorySnapshot<T extends MemoryFile | null>(memory: T): T {
4347
- if (memory !== null && !StorageManager.loadedMemorySnapshots.has(memory)) {
4348
+ if (memory !== null && readRawMemoryDocument(memory) === undefined && !StorageManager.loadedMemorySnapshots.has(memory)) {
4348
4349
  StorageManager.loadedMemorySnapshots.set(memory, createMemorySnapshot(memory));
4349
4350
  }
4350
4351
  return memory;
4351
4352
  }
4352
4353
 
4354
+ /** The snapshot of `memory` as loaded, rebuilt from its retained file text; undefined when none was retained. */
4355
+ private snapshotFromLoadedDocument(memory: MemoryFile): string | undefined {
4356
+ const raw = readRawMemoryDocument(memory);
4357
+ const parsed = raw === undefined ? null : parseFrontmatter(raw);
4358
+ if (raw === undefined || !parsed) return undefined;
4359
+ const frontmatter = { ...parsed.frontmatter, ...loadTimeStamps(raw, parsed.frontmatter, memory.frontmatter) };
4360
+ const normalized = normalizeFrontmatterForPath(frontmatter, toMemoryPathRel(this.baseDir, memory.path), parsed.content);
4361
+ return createMemorySnapshot({ path: memory.path, frontmatter: normalized, content: parsed.content });
4362
+ }
4363
+
4353
4364
  private rememberMemorySnapshots(memories: MemoryFile[]): MemoryFile[] {
4354
4365
  for (const memory of memories) this.rememberMemorySnapshot(memory);
4355
4366
  return memories;
@@ -5015,7 +5026,7 @@ export class StorageManager extends TombstoneBlockedCaptureIndexHost {
5015
5026
  if (current === null && destination?.frontmatter.id === memory.frontmatter.id) return false;
5016
5027
  throw new Error(`memory ${memory.frontmatter.id} changed before its tier move`);
5017
5028
  }
5018
- const callerSnapshot = StorageManager.loadedMemorySnapshots.get(memory);
5029
+ const callerSnapshot = StorageManager.loadedMemorySnapshots.get(memory) ?? this.snapshotFromLoadedDocument(memory);
5019
5030
  const currentSnapshot = createMemorySnapshot(current);
5020
5031
  if (
5021
5032
  callerSnapshot === undefined