@remnic/core 9.3.595 → 9.3.596

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 (80) hide show
  1. package/dist/access-cli.js +14 -14
  2. package/dist/access-http.js +6 -6
  3. package/dist/access-mcp.js +5 -5
  4. package/dist/access-schema.d.ts +7 -7
  5. package/dist/access-service.js +4 -4
  6. package/dist/briefing.js +2 -2
  7. package/dist/causal-consolidation.js +3 -3
  8. package/dist/{chunk-ARY5OOLG.js → chunk-557IAFPD.js} +2 -2
  9. package/dist/{chunk-VFB2G5YL.js → chunk-5BUGGPBR.js} +4 -4
  10. package/dist/{chunk-USYGGIJZ.js → chunk-D2MMMTDV.js} +2 -2
  11. package/dist/{chunk-XM7BYXT7.js → chunk-D65TSG24.js} +2 -2
  12. package/dist/{chunk-FHBEL473.js → chunk-DOX2CG6Y.js} +54 -5
  13. package/dist/chunk-DOX2CG6Y.js.map +1 -0
  14. package/dist/{chunk-DARLGSFX.js → chunk-ELKI4BB6.js} +4 -4
  15. package/dist/{chunk-QRWZOCJN.js → chunk-F4LM4ULA.js} +12 -12
  16. package/dist/{chunk-JIBCUYIP.js → chunk-IEFHBIU2.js} +10 -10
  17. package/dist/{chunk-KDUFBSBF.js → chunk-IK34DVAC.js} +2 -2
  18. package/dist/{chunk-OPYFD6PD.js → chunk-IK7DCC5H.js} +2 -2
  19. package/dist/{chunk-574MU2Y3.js → chunk-JTDRJQ3K.js} +2 -2
  20. package/dist/{chunk-LAL7WBLY.js → chunk-LYPDMKUT.js} +3 -3
  21. package/dist/{chunk-GBXGCFRH.js → chunk-MA5MWGKP.js} +2 -2
  22. package/dist/{chunk-HQO5EBUC.js → chunk-MLT75J5S.js} +3 -3
  23. package/dist/{chunk-7X7TBJRX.js → chunk-NOMEVTUD.js} +2 -2
  24. package/dist/{chunk-SUTSSOYU.js → chunk-OD5LFAPZ.js} +2 -2
  25. package/dist/{chunk-XT7XVA53.js → chunk-OI27U2HT.js} +2 -2
  26. package/dist/{chunk-MQEIWDYW.js → chunk-QDDHYAKV.js} +2 -2
  27. package/dist/{chunk-ZY6UPHNY.js → chunk-TYICDVQW.js} +3 -3
  28. package/dist/{chunk-XRWTAEZM.js → chunk-W5O2FQTZ.js} +2 -2
  29. package/dist/{chunk-V3RXWQIE.js → chunk-WXACKLKP.js} +209 -59
  30. package/dist/chunk-WXACKLKP.js.map +1 -0
  31. package/dist/{chunk-IRFF6LSF.js → chunk-YFS5OEKO.js} +36 -1
  32. package/dist/chunk-YFS5OEKO.js.map +1 -0
  33. package/dist/cli.js +15 -15
  34. package/dist/compounding/engine.js +2 -2
  35. package/dist/connectors/codex-materialize-runner.js +2 -2
  36. package/dist/connectors/index.js +2 -2
  37. package/dist/entity-retrieval.js +2 -2
  38. package/dist/index.js +22 -22
  39. package/dist/maintenance/memory-governance.js +2 -2
  40. package/dist/maintenance/rebuild-memory-lifecycle-ledger.js +2 -2
  41. package/dist/maintenance/rebuild-memory-projection.js +3 -3
  42. package/dist/namespaces/migrate.js +3 -3
  43. package/dist/namespaces/storage.js +2 -2
  44. package/dist/operator-toolkit.js +5 -5
  45. package/dist/orchestrator.js +11 -11
  46. package/dist/retrieval-agents.js +2 -2
  47. package/dist/semantic-consolidation.js +3 -3
  48. package/dist/semantic-rule-promotion.js +2 -2
  49. package/dist/semantic-rule-verifier.js +2 -2
  50. package/dist/storage.d.ts +2 -0
  51. package/dist/storage.js +1 -1
  52. package/dist/temporal-index.js +1 -1
  53. package/dist/verified-recall.js +2 -2
  54. package/package.json +1 -1
  55. package/src/entity-retrieval.ts +64 -3
  56. package/src/storage.ts +40 -0
  57. package/src/temporal-index.test.ts +191 -0
  58. package/src/temporal-index.ts +291 -100
  59. package/dist/chunk-FHBEL473.js.map +0 -1
  60. package/dist/chunk-IRFF6LSF.js.map +0 -1
  61. package/dist/chunk-V3RXWQIE.js.map +0 -1
  62. /package/dist/{chunk-ARY5OOLG.js.map → chunk-557IAFPD.js.map} +0 -0
  63. /package/dist/{chunk-VFB2G5YL.js.map → chunk-5BUGGPBR.js.map} +0 -0
  64. /package/dist/{chunk-USYGGIJZ.js.map → chunk-D2MMMTDV.js.map} +0 -0
  65. /package/dist/{chunk-XM7BYXT7.js.map → chunk-D65TSG24.js.map} +0 -0
  66. /package/dist/{chunk-DARLGSFX.js.map → chunk-ELKI4BB6.js.map} +0 -0
  67. /package/dist/{chunk-QRWZOCJN.js.map → chunk-F4LM4ULA.js.map} +0 -0
  68. /package/dist/{chunk-JIBCUYIP.js.map → chunk-IEFHBIU2.js.map} +0 -0
  69. /package/dist/{chunk-KDUFBSBF.js.map → chunk-IK34DVAC.js.map} +0 -0
  70. /package/dist/{chunk-OPYFD6PD.js.map → chunk-IK7DCC5H.js.map} +0 -0
  71. /package/dist/{chunk-574MU2Y3.js.map → chunk-JTDRJQ3K.js.map} +0 -0
  72. /package/dist/{chunk-LAL7WBLY.js.map → chunk-LYPDMKUT.js.map} +0 -0
  73. /package/dist/{chunk-GBXGCFRH.js.map → chunk-MA5MWGKP.js.map} +0 -0
  74. /package/dist/{chunk-HQO5EBUC.js.map → chunk-MLT75J5S.js.map} +0 -0
  75. /package/dist/{chunk-7X7TBJRX.js.map → chunk-NOMEVTUD.js.map} +0 -0
  76. /package/dist/{chunk-SUTSSOYU.js.map → chunk-OD5LFAPZ.js.map} +0 -0
  77. /package/dist/{chunk-XT7XVA53.js.map → chunk-OI27U2HT.js.map} +0 -0
  78. /package/dist/{chunk-MQEIWDYW.js.map → chunk-QDDHYAKV.js.map} +0 -0
  79. /package/dist/{chunk-ZY6UPHNY.js.map → chunk-TYICDVQW.js.map} +0 -0
  80. /package/dist/{chunk-XRWTAEZM.js.map → chunk-W5O2FQTZ.js.map} +0 -0
@@ -1,10 +1,17 @@
1
1
  // src/temporal-index.ts
2
+ import { execFileSync } from "child_process";
3
+ import * as crypto from "crypto";
2
4
  import * as fs from "fs";
3
5
  import * as path from "path";
4
6
  var INDEX_VERSION = 1;
5
7
  var TEMPORAL_INDEX_FILE = "index_time.json";
6
8
  var TAG_INDEX_FILE = "index_tags.json";
7
9
  var TAG_INDEX_VERSION = 2;
10
+ var INDEX_LOCK_STALE_MS = 6e4;
11
+ var INDEX_LOCK_POLL_MS = 10;
12
+ var INDEX_PROCESS_START_TOLERANCE_MS = 2e3;
13
+ var INDEX_LOCK_SLEEP = new Int32Array(new SharedArrayBuffer(4));
14
+ var INDEX_PROCESS_STARTED_AT_MS = Date.now() - process.uptime() * 1e3;
8
15
  function stateDir(memoryDir) {
9
16
  return path.join(memoryDir, "state");
10
17
  }
@@ -28,25 +35,159 @@ function readJsonSafe(filePath, fallback) {
28
35
  return fallback;
29
36
  }
30
37
  }
31
- function writeJsonSafe(filePath, data) {
38
+ function sleepSync(ms) {
39
+ Atomics.wait(INDEX_LOCK_SLEEP, 0, 0, ms);
40
+ }
41
+ function uniqueTempPath(filePath) {
42
+ const dir = path.dirname(filePath);
43
+ const base = path.basename(filePath);
44
+ const nonce = crypto.randomBytes(6).toString("hex");
45
+ return path.join(dir, `.${base}.${process.pid}.${Date.now()}.${nonce}.tmp`);
46
+ }
47
+ function lockOwnerPath(lockDir) {
48
+ return path.join(lockDir, "owner.json");
49
+ }
50
+ function writeIndexLockOwner(lockDir) {
32
51
  try {
33
- fs.writeFileSync(filePath, JSON.stringify(data, null, 2), "utf8");
52
+ fs.writeFileSync(
53
+ lockOwnerPath(lockDir),
54
+ JSON.stringify({
55
+ pid: process.pid,
56
+ createdAt: (/* @__PURE__ */ new Date()).toISOString(),
57
+ processStartedAtMs: INDEX_PROCESS_STARTED_AT_MS
58
+ }),
59
+ {
60
+ encoding: "utf8",
61
+ flag: "wx"
62
+ }
63
+ );
34
64
  } catch {
35
65
  }
36
66
  }
37
- function writeJsonAtomic(filePath, data) {
38
- const tmp = `${filePath}.tmp`;
67
+ function readIndexLockOwner(lockDir) {
68
+ try {
69
+ const parsed = JSON.parse(fs.readFileSync(lockOwnerPath(lockDir), "utf8"));
70
+ if (!(typeof parsed.pid === "number" && Number.isInteger(parsed.pid) && parsed.pid > 0)) return null;
71
+ const owner = { pid: parsed.pid };
72
+ if ("createdAt" in parsed && typeof parsed.createdAt === "string" && parsed.createdAt.length > 0) {
73
+ owner.createdAt = parsed.createdAt;
74
+ }
75
+ const processStartedAtMs = parsed.processStartedAtMs;
76
+ if (typeof processStartedAtMs === "number" && Number.isFinite(processStartedAtMs) && processStartedAtMs > 0) {
77
+ owner.processStartedAtMs = processStartedAtMs;
78
+ }
79
+ return owner;
80
+ } catch {
81
+ return null;
82
+ }
83
+ }
84
+ function processIsAlive(pid) {
85
+ try {
86
+ process.kill(pid, 0);
87
+ return true;
88
+ } catch (error) {
89
+ const code = error?.code;
90
+ return code === "EPERM";
91
+ }
92
+ }
93
+ function readProcessStartedAtMs(pid) {
39
94
  try {
40
- fs.writeFileSync(tmp, JSON.stringify(data, null, 2), "utf8");
41
- fs.renameSync(tmp, filePath);
95
+ const output = execFileSync("ps", ["-p", String(pid), "-o", "lstart="], {
96
+ encoding: "utf8",
97
+ stdio: ["ignore", "pipe", "ignore"],
98
+ timeout: 1e3
99
+ }).trim();
100
+ if (!output) return null;
101
+ const startedAtMs = Date.parse(output);
102
+ return Number.isFinite(startedAtMs) ? startedAtMs : null;
42
103
  } catch {
43
- writeJsonSafe(filePath, data);
104
+ return null;
105
+ }
106
+ }
107
+ function lockOwnerIsRunning(owner) {
108
+ if (!processIsAlive(owner.pid)) return false;
109
+ if (owner.processStartedAtMs === void 0) return true;
110
+ const runningStartedAtMs = readProcessStartedAtMs(owner.pid);
111
+ if (runningStartedAtMs === null) return true;
112
+ return runningStartedAtMs <= owner.processStartedAtMs + INDEX_PROCESS_START_TOLERANCE_MS;
113
+ }
114
+ function removeAbandonedIndexLock(lockDir) {
115
+ try {
116
+ const info = fs.lstatSync(lockDir);
117
+ if (info.isSymbolicLink()) return "blocked";
118
+ if (!info.isDirectory()) {
119
+ fs.rmSync(lockDir, { force: true });
120
+ return "removed";
121
+ }
122
+ const owner = readIndexLockOwner(lockDir);
123
+ if (owner !== null && lockOwnerIsRunning(owner)) return "wait";
124
+ if (owner === null && Date.now() - info.mtimeMs < INDEX_LOCK_STALE_MS) return "wait";
125
+ fs.rmSync(lockDir, { recursive: true, force: true });
126
+ return "removed";
127
+ } catch {
128
+ return "blocked";
129
+ }
130
+ }
131
+ function withIndexFileLock(filePath, update) {
132
+ const lockDir = `${filePath}.lock.d`;
133
+ let acquired = false;
134
+ while (!acquired) {
135
+ try {
136
+ fs.mkdirSync(lockDir);
137
+ writeIndexLockOwner(lockDir);
138
+ acquired = true;
139
+ } catch (error) {
140
+ const code = error?.code;
141
+ if (code !== "EEXIST") return;
142
+ const cleanupResult = removeAbandonedIndexLock(lockDir);
143
+ if (cleanupResult === "blocked") return;
144
+ sleepSync(INDEX_LOCK_POLL_MS);
145
+ }
146
+ }
147
+ try {
148
+ update();
149
+ } finally {
44
150
  try {
45
- fs.unlinkSync(tmp);
151
+ fs.rmSync(lockDir, { recursive: true, force: true });
46
152
  } catch {
47
153
  }
48
154
  }
49
155
  }
156
+ function writeJsonAtomic(filePath, data) {
157
+ const payload = JSON.stringify(data, null, 2);
158
+ for (let attempt = 0; attempt < 3; attempt += 1) {
159
+ const tmp = uniqueTempPath(filePath);
160
+ try {
161
+ fs.writeFileSync(tmp, payload, "utf8");
162
+ fs.renameSync(tmp, filePath);
163
+ return;
164
+ } catch {
165
+ try {
166
+ fs.unlinkSync(tmp);
167
+ } catch {
168
+ }
169
+ sleepSync(INDEX_LOCK_POLL_MS);
170
+ }
171
+ }
172
+ }
173
+ function updateTemporalIndex(memoryDir, update) {
174
+ const indexPath = temporalIndexPath(memoryDir);
175
+ withIndexFileLock(indexPath, () => {
176
+ const index = readJsonSafe(indexPath, { version: INDEX_VERSION, dates: {} });
177
+ update(index);
178
+ writeJsonAtomic(indexPath, index);
179
+ });
180
+ }
181
+ function updateTagIndex(memoryDir, update) {
182
+ const indexPath = tagIndexPath(memoryDir);
183
+ withIndexFileLock(indexPath, () => {
184
+ const index = normalizeTagIndex(
185
+ readJsonSafe(indexPath, { version: TAG_INDEX_VERSION, tags: {}, aliases: {} })
186
+ );
187
+ update(index);
188
+ writeJsonAtomic(indexPath, index);
189
+ });
190
+ }
50
191
  function isoDateFromTimestamp(isoString) {
51
192
  if (typeof isoString !== "string" || isoString.length < 10) {
52
193
  console.warn(`[engram] temporal-index: malformed timestamp "${isoString}", falling back to today`);
@@ -245,46 +386,51 @@ function promptContainsAlias(prompt, alias) {
245
386
  function indexMemory(memoryDir, memoryPath, createdAt, tags) {
246
387
  try {
247
388
  ensureStateDir(memoryDir);
248
- const tPath = temporalIndexPath(memoryDir);
249
- const tIndex = readJsonSafe(tPath, { version: INDEX_VERSION, dates: {} });
250
389
  const dateKey = isoDateFromTimestamp(createdAt);
251
- addPathToSet(tIndex.dates, dateKey, memoryPath);
252
- writeJsonAtomic(tPath, tIndex);
253
- const gPath = tagIndexPath(memoryDir);
254
- const gIndex = normalizeTagIndex(readJsonSafe(gPath, { version: TAG_INDEX_VERSION, tags: {}, aliases: {} }));
255
- for (const tag of tags) {
256
- if (tag && typeof tag === "string") {
257
- addTagGraphEntry(gIndex, tag, memoryPath);
390
+ updateTemporalIndex(memoryDir, (index) => {
391
+ addPathToSet(index.dates, dateKey, memoryPath);
392
+ });
393
+ updateTagIndex(memoryDir, (index) => {
394
+ for (const tag of tags) {
395
+ if (tag && typeof tag === "string") {
396
+ addTagGraphEntry(index, tag, memoryPath);
397
+ }
258
398
  }
259
- }
260
- writeJsonAtomic(gPath, gIndex);
399
+ });
261
400
  } catch {
262
401
  }
263
402
  }
264
403
  function deindexMemory(memoryDir, memoryPath, createdAt, tags) {
265
404
  try {
266
405
  ensureStateDir(memoryDir);
267
- const tPath = temporalIndexPath(memoryDir);
268
- const tIndex = readJsonSafe(tPath, { version: INDEX_VERSION, dates: {} });
269
406
  const dateKey = isoDateFromTimestamp(createdAt);
270
- removePathFromSet(tIndex.dates, dateKey, memoryPath);
271
- writeJsonAtomic(tPath, tIndex);
272
- const gPath = tagIndexPath(memoryDir);
273
- const gIndex = normalizeTagIndex(readJsonSafe(gPath, { version: TAG_INDEX_VERSION, tags: {}, aliases: {} }));
274
- for (const tag of tags) {
275
- if (tag && typeof tag === "string") {
276
- removeTagGraphEntry(gIndex, tag, memoryPath);
407
+ updateTemporalIndex(memoryDir, (index) => {
408
+ removePathFromSet(index.dates, dateKey, memoryPath);
409
+ });
410
+ updateTagIndex(memoryDir, (index) => {
411
+ for (const tag of tags) {
412
+ if (tag && typeof tag === "string") {
413
+ removeTagGraphEntry(index, tag, memoryPath);
414
+ }
277
415
  }
278
- }
279
- writeJsonAtomic(gPath, gIndex);
416
+ });
280
417
  } catch {
281
418
  }
282
419
  }
283
420
  function clearIndexes(memoryDir) {
284
421
  try {
285
422
  ensureStateDir(memoryDir);
286
- writeJsonAtomic(temporalIndexPath(memoryDir), { version: INDEX_VERSION, dates: {} });
287
- writeJsonAtomic(tagIndexPath(memoryDir), { version: TAG_INDEX_VERSION, tags: {}, aliases: {} });
423
+ updateTemporalIndex(memoryDir, (index) => {
424
+ index.version = INDEX_VERSION;
425
+ index.lastRebuildAt = void 0;
426
+ index.dates = {};
427
+ });
428
+ updateTagIndex(memoryDir, (index) => {
429
+ index.version = TAG_INDEX_VERSION;
430
+ index.lastRebuildAt = void 0;
431
+ index.tags = {};
432
+ index.aliases = {};
433
+ });
288
434
  } catch {
289
435
  }
290
436
  }
@@ -299,21 +445,21 @@ function indexMemoriesBatch(memoryDir, entries) {
299
445
  if (entries.length === 0) return;
300
446
  try {
301
447
  ensureStateDir(memoryDir);
302
- const tPath = temporalIndexPath(memoryDir);
303
- const tIndex = readJsonSafe(tPath, { version: INDEX_VERSION, dates: {} });
304
- const gPath = tagIndexPath(memoryDir);
305
- const gIndex = normalizeTagIndex(readJsonSafe(gPath, { version: TAG_INDEX_VERSION, tags: {}, aliases: {} }));
306
- for (const entry of entries) {
307
- const dateKey = isoDateFromTimestamp(entry.createdAt);
308
- addPathToSet(tIndex.dates, dateKey, entry.path);
309
- for (const tag of entry.tags) {
310
- if (tag && typeof tag === "string") {
311
- addTagGraphEntry(gIndex, tag, entry.path);
448
+ updateTemporalIndex(memoryDir, (index) => {
449
+ for (const entry of entries) {
450
+ const dateKey = isoDateFromTimestamp(entry.createdAt);
451
+ addPathToSet(index.dates, dateKey, entry.path);
452
+ }
453
+ });
454
+ updateTagIndex(memoryDir, (index) => {
455
+ for (const entry of entries) {
456
+ for (const tag of entry.tags) {
457
+ if (tag && typeof tag === "string") {
458
+ addTagGraphEntry(index, tag, entry.path);
459
+ }
312
460
  }
313
461
  }
314
- }
315
- writeJsonAtomic(tPath, tIndex);
316
- writeJsonAtomic(gPath, gIndex);
462
+ });
317
463
  } catch {
318
464
  }
319
465
  }
@@ -458,10 +604,12 @@ function recencyWindowFromPrompt(prompt, nowMs = Date.now()) {
458
604
  "november",
459
605
  "december"
460
606
  ];
461
- const monthMatch = p.match(/\b(january|february|march|april|may|june|july|august|september|october|november|december)(?:\s+(\d{4}))?\b/);
607
+ const monthMatch = p.match(
608
+ /\b(january|february|march|april|may|june|july|august|september|october|november|december)(?:\s+(\d{4}))?\b/
609
+ );
462
610
  if (monthMatch) {
463
611
  const monthIdx = monthNames.indexOf(monthMatch[1]);
464
- const year = monthMatch[2] ? parseInt(monthMatch[2], 10) : now.getFullYear();
612
+ const year = monthMatch[2] ? Number.parseInt(monthMatch[2], 10) : now.getFullYear();
465
613
  if (/\bbefore\b/.test(p)) {
466
614
  if (monthMatch[2]) {
467
615
  const monthStart = new Date(year, monthIdx, 1);
@@ -475,19 +623,19 @@ function recencyWindowFromPrompt(prompt, nowMs = Date.now()) {
475
623
  }
476
624
  const weekMatch = p.match(/(\d{1,5})\s*weeks?\s*ago/);
477
625
  if (weekMatch) {
478
- daysBack = Math.min(365, parseInt(weekMatch[1], 10) * 7);
626
+ daysBack = Math.min(365, Number.parseInt(weekMatch[1], 10) * 7);
479
627
  } else {
480
628
  const monthsAgoMatch = p.match(/(\d{1,5})\s*months?\s*ago/);
481
629
  if (monthsAgoMatch) {
482
- daysBack = Math.min(730, parseInt(monthsAgoMatch[1], 10) * 31);
630
+ daysBack = Math.min(730, Number.parseInt(monthsAgoMatch[1], 10) * 31);
483
631
  } else {
484
632
  const numMatch = p.match(/(\d{1,5})\s*days?\s*ago/);
485
633
  if (numMatch) {
486
- daysBack = Math.min(365, parseInt(numMatch[1], 10));
634
+ daysBack = Math.min(365, Number.parseInt(numMatch[1], 10));
487
635
  } else {
488
636
  const hrMatch = p.match(/(\d{1,5})\s*hours?\s*ago/);
489
637
  if (hrMatch) {
490
- daysBack = Math.max(1, Math.ceil(parseInt(hrMatch[1], 10) / 24));
638
+ daysBack = Math.max(1, Math.ceil(Number.parseInt(hrMatch[1], 10) / 24));
491
639
  }
492
640
  }
493
641
  }
@@ -498,7 +646,7 @@ function recencyWindowFromPrompt(prompt, nowMs = Date.now()) {
498
646
  }
499
647
  const usMatch = p.match(/(\d{1,2})\/(\d{1,2})\/(\d{2,4})/);
500
648
  if (usMatch) {
501
- const year = usMatch[3].length === 2 ? 2e3 + parseInt(usMatch[3], 10) : parseInt(usMatch[3], 10);
649
+ const year = usMatch[3].length === 2 ? 2e3 + Number.parseInt(usMatch[3], 10) : Number.parseInt(usMatch[3], 10);
502
650
  return `${year}-${usMatch[1].padStart(2, "0")}-${usMatch[2].padStart(2, "0")}`;
503
651
  }
504
652
  const dayOfWeekMatch = p.match(/\blast\s+(monday|tuesday|wednesday|thursday|friday|saturday|sunday)\b/);
@@ -546,10 +694,12 @@ function recencyWindowBoundsFromPrompt(prompt, nowMs = Date.now()) {
546
694
  "november",
547
695
  "december"
548
696
  ];
549
- const monthMatch = p.match(/\b(january|february|march|april|may|june|july|august|september|october|november|december)(?:\s+(\d{4}))?\b/);
697
+ const monthMatch = p.match(
698
+ /\b(january|february|march|april|may|june|july|august|september|october|november|december)(?:\s+(\d{4}))?\b/
699
+ );
550
700
  if (monthMatch) {
551
701
  const monthIdx = monthNames.indexOf(monthMatch[1]);
552
- const year = monthMatch[2] ? parseInt(monthMatch[2], 10) : now.getFullYear();
702
+ const year = monthMatch[2] ? Number.parseInt(monthMatch[2], 10) : now.getFullYear();
553
703
  const isSinceOrAfter = /\b(since|after)\b/.test(p);
554
704
  const isBefore = /\bbefore\b/.test(p);
555
705
  if (isSinceOrAfter) {
@@ -563,15 +713,15 @@ function recencyWindowBoundsFromPrompt(prompt, nowMs = Date.now()) {
563
713
  let toDaysBack = -1;
564
714
  const weekMatch = p.match(/(\d{1,5})\s*weeks?\s*ago/);
565
715
  if (weekMatch) {
566
- toDaysBack = Math.max(0, Math.min(52, parseInt(weekMatch[1], 10)) - 1) * 7;
716
+ toDaysBack = Math.max(0, Math.min(52, Number.parseInt(weekMatch[1], 10)) - 1) * 7;
567
717
  } else {
568
718
  const monthsAgoMatch = p.match(/(\d{1,5})\s*months?\s*ago/);
569
719
  if (monthsAgoMatch) {
570
- toDaysBack = Math.max(0, Math.min(24, parseInt(monthsAgoMatch[1], 10)) - 1) * 31;
720
+ toDaysBack = Math.max(0, Math.min(24, Number.parseInt(monthsAgoMatch[1], 10)) - 1) * 31;
571
721
  } else {
572
722
  const numMatch = p.match(/(\d{1,5})\s*days?\s*ago/);
573
723
  if (numMatch) {
574
- toDaysBack = Math.max(0, Math.min(365, parseInt(numMatch[1], 10)) - 1);
724
+ toDaysBack = Math.max(0, Math.min(365, Number.parseInt(numMatch[1], 10)) - 1);
575
725
  } else {
576
726
  const hrMatch = p.match(/(\d{1,5})\s*hours?\s*ago/);
577
727
  if (hrMatch) {
@@ -587,7 +737,7 @@ function recencyWindowBoundsFromPrompt(prompt, nowMs = Date.now()) {
587
737
  } else {
588
738
  const usMatch = p.match(/(\d{1,2})\/(\d{1,2})\/(\d{2,4})/);
589
739
  if (usMatch) {
590
- const year = usMatch[3].length === 2 ? 2e3 + parseInt(usMatch[3], 10) : parseInt(usMatch[3], 10);
740
+ const year = usMatch[3].length === 2 ? 2e3 + Number.parseInt(usMatch[3], 10) : Number.parseInt(usMatch[3], 10);
591
741
  const d = /* @__PURE__ */ new Date(`${year}-${usMatch[1].padStart(2, "0")}-${usMatch[2].padStart(2, "0")}T00:00:00Z`);
592
742
  toDate = new Date(d.getTime() + 864e5).toISOString().slice(0, 10);
593
743
  } else {
@@ -623,4 +773,4 @@ export {
623
773
  recencyWindowFromPrompt,
624
774
  recencyWindowBoundsFromPrompt
625
775
  };
626
- //# sourceMappingURL=chunk-V3RXWQIE.js.map
776
+ //# sourceMappingURL=chunk-WXACKLKP.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/temporal-index.ts"],"sourcesContent":["/**\n * Temporal and Tag Indexes (v8.1 — SwiftMem-inspired)\n *\n * Maintains two fast on-disk lookup structures in `state/`:\n * index_time.json — maps YYYY-MM-DD date buckets to memory file paths\n * index_tags.json — maps tag strings to memory file paths\n *\n * Used as an optional prefilter in the retrieval pipeline:\n * given a time range or a set of tags, narrow the candidate set\n * before the QMD hybrid search so we can pass a smaller pool to scoring.\n *\n * Design constraints:\n * - Must be fail-open (any error returns empty / unfiltered)\n * - Reads/writes are batched per extraction run\n * - Both indexes are plain JSON; no external dependencies\n */\n\nimport { execFileSync } from \"node:child_process\";\nimport * as crypto from \"node:crypto\";\nimport * as fs from \"node:fs\";\nimport * as path from \"node:path\";\n\nexport interface TemporalIndex {\n /** version bumped when schema changes */\n version: number;\n /** Last full rebuild timestamp (ISO string) */\n lastRebuildAt?: string;\n /** Map from YYYY-MM-DD → array of memory paths */\n dates: Record<string, string[]>;\n}\n\nexport interface TagIndex {\n version: number;\n lastRebuildAt?: string;\n /** Map from canonical tag string → node metadata */\n tags: Record<string, TagNode | string[]>;\n /** Map from alias string → canonical tags */\n aliases?: Record<string, string[]>;\n}\n\nexport interface TagNode {\n paths: string[];\n aliases?: string[];\n parents?: string[];\n}\n\nconst INDEX_VERSION = 1;\nconst TEMPORAL_INDEX_FILE = \"index_time.json\";\nconst TAG_INDEX_FILE = \"index_tags.json\";\nconst TAG_INDEX_VERSION = 2;\nconst INDEX_LOCK_STALE_MS = 60_000;\nconst INDEX_LOCK_POLL_MS = 10;\nconst INDEX_PROCESS_START_TOLERANCE_MS = 2_000;\nconst INDEX_LOCK_SLEEP = new Int32Array(new SharedArrayBuffer(4));\nconst INDEX_PROCESS_STARTED_AT_MS = Date.now() - process.uptime() * 1000;\n\ninterface IndexLockOwner {\n pid: number;\n createdAt?: string;\n processStartedAtMs?: number;\n}\n\ntype IndexLockCleanupResult = \"removed\" | \"wait\" | \"blocked\";\n\nfunction stateDir(memoryDir: string): string {\n return path.join(memoryDir, \"state\");\n}\n\nfunction temporalIndexPath(memoryDir: string): string {\n return path.join(stateDir(memoryDir), TEMPORAL_INDEX_FILE);\n}\n\nfunction tagIndexPath(memoryDir: string): string {\n return path.join(stateDir(memoryDir), TAG_INDEX_FILE);\n}\n\nfunction ensureStateDir(memoryDir: string): void {\n const dir = stateDir(memoryDir);\n if (!fs.existsSync(dir)) {\n fs.mkdirSync(dir, { recursive: true });\n }\n}\n\nfunction readJsonSafe<T>(filePath: string, fallback: T): T {\n try {\n const raw = fs.readFileSync(filePath, \"utf8\");\n return JSON.parse(raw) as T;\n } catch {\n return fallback;\n }\n}\n\nfunction writeJsonSafe(filePath: string, data: unknown): void {\n try {\n fs.writeFileSync(filePath, JSON.stringify(data, null, 2), \"utf8\");\n } catch {\n // Fail silently — indexes are advisory only\n }\n}\n\nfunction sleepSync(ms: number): void {\n Atomics.wait(INDEX_LOCK_SLEEP, 0, 0, ms);\n}\n\nfunction uniqueTempPath(filePath: string): string {\n const dir = path.dirname(filePath);\n const base = path.basename(filePath);\n const nonce = crypto.randomBytes(6).toString(\"hex\");\n return path.join(dir, `.${base}.${process.pid}.${Date.now()}.${nonce}.tmp`);\n}\n\nfunction lockOwnerPath(lockDir: string): string {\n return path.join(lockDir, \"owner.json\");\n}\n\nfunction writeIndexLockOwner(lockDir: string): void {\n try {\n fs.writeFileSync(\n lockOwnerPath(lockDir),\n JSON.stringify({\n pid: process.pid,\n createdAt: new Date().toISOString(),\n processStartedAtMs: INDEX_PROCESS_STARTED_AT_MS,\n }),\n {\n encoding: \"utf8\",\n flag: \"wx\",\n }\n );\n } catch {\n // Fail silently — the directory lock is still the serialization primitive.\n }\n}\n\nfunction readIndexLockOwner(lockDir: string): IndexLockOwner | null {\n try {\n const parsed = JSON.parse(fs.readFileSync(lockOwnerPath(lockDir), \"utf8\")) as { pid?: unknown };\n if (!(typeof parsed.pid === \"number\" && Number.isInteger(parsed.pid) && parsed.pid > 0)) return null;\n const owner: IndexLockOwner = { pid: parsed.pid };\n if (\n \"createdAt\" in parsed &&\n typeof (parsed as { createdAt?: unknown }).createdAt === \"string\" &&\n (parsed as { createdAt: string }).createdAt.length > 0\n ) {\n owner.createdAt = (parsed as { createdAt: string }).createdAt;\n }\n const processStartedAtMs = (parsed as { processStartedAtMs?: unknown }).processStartedAtMs;\n if (typeof processStartedAtMs === \"number\" && Number.isFinite(processStartedAtMs) && processStartedAtMs > 0) {\n owner.processStartedAtMs = processStartedAtMs;\n }\n return owner;\n } catch {\n return null;\n }\n}\n\nfunction processIsAlive(pid: number): boolean {\n try {\n process.kill(pid, 0);\n return true;\n } catch (error) {\n const code = (error as NodeJS.ErrnoException)?.code;\n return code === \"EPERM\";\n }\n}\n\nfunction readProcessStartedAtMs(pid: number): number | null {\n try {\n const output = execFileSync(\"ps\", [\"-p\", String(pid), \"-o\", \"lstart=\"], {\n encoding: \"utf8\",\n stdio: [\"ignore\", \"pipe\", \"ignore\"],\n timeout: 1_000,\n }).trim();\n if (!output) return null;\n const startedAtMs = Date.parse(output);\n return Number.isFinite(startedAtMs) ? startedAtMs : null;\n } catch {\n return null;\n }\n}\n\nfunction lockOwnerIsRunning(owner: IndexLockOwner): boolean {\n if (!processIsAlive(owner.pid)) return false;\n if (owner.processStartedAtMs === undefined) return true;\n const runningStartedAtMs = readProcessStartedAtMs(owner.pid);\n if (runningStartedAtMs === null) return true;\n return runningStartedAtMs <= owner.processStartedAtMs + INDEX_PROCESS_START_TOLERANCE_MS;\n}\n\nfunction removeAbandonedIndexLock(lockDir: string): IndexLockCleanupResult {\n try {\n const info = fs.lstatSync(lockDir);\n if (info.isSymbolicLink()) return \"blocked\";\n if (!info.isDirectory()) {\n fs.rmSync(lockDir, { force: true });\n return \"removed\";\n }\n const owner = readIndexLockOwner(lockDir);\n if (owner !== null && lockOwnerIsRunning(owner)) return \"wait\";\n if (owner === null && Date.now() - info.mtimeMs < INDEX_LOCK_STALE_MS) return \"wait\";\n fs.rmSync(lockDir, { recursive: true, force: true });\n return \"removed\";\n } catch {\n // Fail silently — indexes are advisory only\n return \"blocked\";\n }\n}\n\nfunction withIndexFileLock(filePath: string, update: () => void): void {\n const lockDir = `${filePath}.lock.d`;\n let acquired = false;\n\n while (!acquired) {\n try {\n fs.mkdirSync(lockDir);\n writeIndexLockOwner(lockDir);\n acquired = true;\n } catch (error) {\n const code = (error as NodeJS.ErrnoException)?.code;\n if (code !== \"EEXIST\") return;\n const cleanupResult = removeAbandonedIndexLock(lockDir);\n if (cleanupResult === \"blocked\") return;\n sleepSync(INDEX_LOCK_POLL_MS);\n }\n }\n\n try {\n update();\n } finally {\n try {\n fs.rmSync(lockDir, { recursive: true, force: true });\n } catch {\n // Fail silently — indexes are advisory only\n }\n }\n}\n\n/**\n * Atomic write: write to a unique `.tmp` sibling then rename so readers never\n * observe a partially-written file.\n */\nfunction writeJsonAtomic(filePath: string, data: unknown): void {\n const payload = JSON.stringify(data, null, 2);\n for (let attempt = 0; attempt < 3; attempt += 1) {\n const tmp = uniqueTempPath(filePath);\n try {\n fs.writeFileSync(tmp, payload, \"utf8\");\n fs.renameSync(tmp, filePath);\n return;\n } catch {\n try {\n fs.unlinkSync(tmp);\n } catch {\n // Fail silently — indexes are advisory only\n }\n sleepSync(INDEX_LOCK_POLL_MS);\n }\n }\n}\n\nfunction updateTemporalIndex(memoryDir: string, update: (index: TemporalIndex) => void): void {\n const indexPath = temporalIndexPath(memoryDir);\n withIndexFileLock(indexPath, () => {\n const index = readJsonSafe<TemporalIndex>(indexPath, { version: INDEX_VERSION, dates: {} });\n update(index);\n writeJsonAtomic(indexPath, index);\n });\n}\n\nfunction updateTagIndex(memoryDir: string, update: (index: TagIndex) => void): void {\n const indexPath = tagIndexPath(memoryDir);\n withIndexFileLock(indexPath, () => {\n const index = normalizeTagIndex(\n readJsonSafe<TagIndex>(indexPath, { version: TAG_INDEX_VERSION, tags: {}, aliases: {} })\n );\n update(index);\n writeJsonAtomic(indexPath, index);\n });\n}\n\nfunction isoDateFromTimestamp(isoString: string): string {\n if (typeof isoString !== \"string\" || isoString.length < 10) {\n // Malformed frontmatter — fall back to today so the memory is still indexed.\n // Log a warning to surface data-quality issues without aborting the write.\n console.warn(`[engram] temporal-index: malformed timestamp \"${isoString}\", falling back to today`);\n return new Date().toISOString().slice(0, 10);\n }\n return isoString.slice(0, 10); // YYYY-MM-DD\n}\n\nfunction addPathToSet(record: Record<string, string[]>, key: string, p: string): void {\n if (!record[key]) {\n record[key] = [];\n }\n if (!record[key].includes(p)) {\n record[key].push(p);\n }\n}\n\nfunction removePathFromSet(record: Record<string, string[]>, key: string, p: string): void {\n if (!record[key]) return;\n record[key] = record[key].filter((x) => x !== p);\n if (record[key].length === 0) {\n delete record[key];\n }\n}\n\nfunction normalizeTagSegment(segment: string): string {\n return segment\n .trim()\n .toLowerCase()\n .replace(/[_\\s]+/g, \"-\")\n .replace(/[^a-z0-9-]/g, \"-\")\n .replace(/-+/g, \"-\")\n .replace(/^-|-$/g, \"\");\n}\n\nfunction normalizeCanonicalTag(tag: string): string {\n return tag\n .trim()\n .toLowerCase()\n .replace(/\\s*[>:|.]+\\s*/g, \"/\")\n .replace(/\\s*\\/\\s*/g, \"/\")\n .split(\"/\")\n .map(normalizeTagSegment)\n .filter(Boolean)\n .join(\"/\");\n}\n\nfunction normalizeAliasKey(tag: string): string {\n return tag\n .trim()\n .toLowerCase()\n .replace(/[\\/_.:-]+/g, \" \")\n .replace(/[-]+/g, \" \")\n .replace(/[^a-z0-9\\s]+/g, \" \")\n .replace(/\\s+/g, \" \")\n .trim();\n}\n\nfunction singularizeAlias(alias: string): string | null {\n if (!alias.endsWith(\"s\") || alias.length <= 3) return null;\n return alias.slice(0, -1);\n}\n\nfunction deriveParentTags(canonical: string): string[] {\n const parts = canonical.split(\"/\").filter(Boolean);\n const parents: string[] = [];\n for (let i = parts.length - 1; i > 0; i -= 1) {\n parents.push(parts.slice(0, i).join(\"/\"));\n }\n return parents;\n}\n\nfunction deriveTagAliases(rawTag: string, canonical: string): string[] {\n const aliases = new Set<string>();\n const canonicalAlias = normalizeAliasKey(canonical);\n const rawAlias = normalizeAliasKey(rawTag);\n const leaf = canonical.split(\"/\").at(-1) ?? canonical;\n const leafAlias = normalizeAliasKey(leaf);\n\n for (const candidate of [canonicalAlias, rawAlias, leafAlias]) {\n if (!candidate) continue;\n aliases.add(candidate);\n const singular = singularizeAlias(candidate);\n if (singular) aliases.add(singular);\n }\n\n return Array.from(aliases);\n}\n\nfunction normalizeTagIndex(raw: TagIndex | null | undefined): TagIndex {\n const normalized: TagIndex = {\n version: TAG_INDEX_VERSION,\n tags: {},\n aliases: {},\n };\n\n if (!raw || typeof raw !== \"object\") {\n return normalized;\n }\n\n const sourceTags = raw.tags ?? {};\n for (const [rawCanonical, nodeOrPaths] of Object.entries(sourceTags)) {\n const canonical = normalizeCanonicalTag(rawCanonical);\n if (!canonical) continue;\n const node: TagNode = Array.isArray(nodeOrPaths)\n ? { paths: [...new Set(nodeOrPaths)] }\n : {\n paths: Array.isArray(nodeOrPaths?.paths) ? [...new Set(nodeOrPaths.paths)] : [],\n aliases: Array.isArray(nodeOrPaths?.aliases) ? [...new Set(nodeOrPaths.aliases)] : [],\n parents: Array.isArray(nodeOrPaths?.parents) ? [...new Set(nodeOrPaths.parents)] : [],\n };\n const existingNode = normalized.tags[canonical];\n if (existingNode && !Array.isArray(existingNode)) {\n existingNode.paths = [...new Set([...existingNode.paths, ...node.paths])];\n existingNode.aliases = [...new Set([...(existingNode.aliases ?? []), ...(node.aliases ?? [])])];\n existingNode.parents = [...new Set([...(existingNode.parents ?? []), ...(node.parents ?? [])])];\n } else if (Array.isArray(existingNode)) {\n normalized.tags[canonical] = {\n paths: [...new Set([...existingNode, ...node.paths])],\n aliases: [...new Set(node.aliases ?? [])],\n parents: [...new Set(node.parents ?? deriveParentTags(canonical))],\n };\n } else {\n normalized.tags[canonical] = node;\n }\n for (const alias of deriveTagAliases(canonical, canonical)) {\n const list = normalized.aliases![alias] ?? [];\n if (!list.includes(canonical)) list.push(canonical);\n normalized.aliases![alias] = list;\n }\n for (const alias of node.aliases ?? []) {\n const aliasKey = normalizeAliasKey(alias);\n if (!aliasKey) continue;\n const list = normalized.aliases![aliasKey] ?? [];\n if (!list.includes(canonical)) list.push(canonical);\n normalized.aliases![aliasKey] = list;\n }\n const mergedNode = normalized.tags[canonical];\n if (mergedNode && !Array.isArray(mergedNode)) {\n mergedNode.parents = [...new Set(mergedNode.parents ?? deriveParentTags(canonical))];\n }\n }\n\n for (const [alias, canonicals] of Object.entries(raw.aliases ?? {})) {\n const aliasKey = normalizeAliasKey(alias);\n if (!aliasKey) continue;\n const list = normalized.aliases![aliasKey] ?? [];\n for (const canonical of canonicals ?? []) {\n const normalizedCanonical = normalizeCanonicalTag(canonical);\n if (normalizedCanonical && !list.includes(normalizedCanonical)) {\n list.push(normalizedCanonical);\n }\n }\n normalized.aliases![aliasKey] = list;\n }\n\n return normalized;\n}\n\nfunction ensureTagNode(index: TagIndex, canonical: string): TagNode {\n const existing = index.tags[canonical];\n if (existing && !Array.isArray(existing)) {\n return existing;\n }\n const created: TagNode = {\n paths: Array.isArray(existing) ? [...new Set(existing)] : [],\n aliases: [],\n parents: deriveParentTags(canonical),\n };\n index.tags[canonical] = created;\n return created;\n}\n\nfunction addTagGraphEntry(index: TagIndex, rawTag: string, memoryPath: string): void {\n const canonical = normalizeCanonicalTag(rawTag);\n if (!canonical) return;\n const node = ensureTagNode(index, canonical);\n if (!node.paths.includes(memoryPath)) {\n node.paths.push(memoryPath);\n }\n\n for (const alias of deriveTagAliases(rawTag, canonical)) {\n const aliasKey = normalizeAliasKey(alias);\n if (!aliasKey) continue;\n if (!node.aliases?.includes(aliasKey)) {\n node.aliases = [...new Set([...(node.aliases ?? []), aliasKey])];\n }\n const list = index.aliases?.[aliasKey] ?? [];\n if (!list.includes(canonical)) {\n index.aliases![aliasKey] = [...list, canonical];\n }\n }\n}\n\nfunction removeTagGraphEntry(index: TagIndex, rawTag: string, memoryPath: string): void {\n const canonical = normalizeCanonicalTag(rawTag);\n if (!canonical) return;\n const node = index.tags[canonical];\n if (!node || Array.isArray(node)) return;\n node.paths = node.paths.filter((value) => value !== memoryPath);\n if (node.paths.length === 0) {\n delete index.tags[canonical];\n }\n}\n\nfunction expandCanonicalTags(index: TagIndex, rawTags: string[]): string[] {\n const canonicals = new Set<string>();\n for (const rawTag of rawTags) {\n const canonical = normalizeCanonicalTag(rawTag);\n if (canonical && index.tags[canonical]) {\n canonicals.add(canonical);\n }\n const aliasKey = normalizeAliasKey(rawTag);\n for (const resolved of index.aliases?.[aliasKey] ?? []) {\n canonicals.add(resolved);\n }\n }\n\n const expanded = new Set<string>();\n for (const canonical of canonicals) {\n expanded.add(canonical);\n const node = index.tags[canonical];\n if (node && !Array.isArray(node)) {\n for (const parent of node.parents ?? []) {\n expanded.add(parent);\n }\n }\n }\n return Array.from(expanded);\n}\n\nfunction aliasPhrase(alias: string): string {\n return alias.replace(/\\//g, \" \").replace(/-/g, \" \").replace(/\\s+/g, \" \").trim();\n}\n\nfunction promptContainsAlias(prompt: string, alias: string): boolean {\n const phrase = aliasPhrase(alias);\n if (!phrase) return false;\n const normalizedPrompt = ` ${normalizeAliasKey(prompt)} `;\n return normalizedPrompt.includes(` ${phrase} `);\n}\n\n// ─── Public API ────────────────────────────────────────────────────────────────\n\n/**\n * Add (or update) a memory file in both indexes.\n *\n * @param memoryDir Root memory directory\n * @param memoryPath Absolute path to the memory file\n * @param createdAt ISO timestamp of the memory's creation date\n * @param tags Array of tag strings from the memory's frontmatter\n */\nexport function indexMemory(memoryDir: string, memoryPath: string, createdAt: string, tags: string[]): void {\n try {\n ensureStateDir(memoryDir);\n\n const dateKey = isoDateFromTimestamp(createdAt);\n updateTemporalIndex(memoryDir, (index) => {\n addPathToSet(index.dates, dateKey, memoryPath);\n });\n\n updateTagIndex(memoryDir, (index) => {\n for (const tag of tags) {\n if (tag && typeof tag === \"string\") {\n addTagGraphEntry(index, tag, memoryPath);\n }\n }\n });\n } catch {\n // Fail silently\n }\n}\n\n/**\n * Remove a memory file from both indexes (called on deletion/archival).\n */\nexport function deindexMemory(memoryDir: string, memoryPath: string, createdAt: string, tags: string[]): void {\n try {\n ensureStateDir(memoryDir);\n\n const dateKey = isoDateFromTimestamp(createdAt);\n updateTemporalIndex(memoryDir, (index) => {\n removePathFromSet(index.dates, dateKey, memoryPath);\n });\n\n updateTagIndex(memoryDir, (index) => {\n for (const tag of tags) {\n if (tag && typeof tag === \"string\") {\n removeTagGraphEntry(index, tag, memoryPath);\n }\n }\n });\n } catch {\n // Fail silently\n }\n}\n\n/**\n * Reset both index files to empty state.\n * Called before a full-corpus rebuild so stale paths in any surviving index\n * file do not persist after the rebuild completes.\n */\nexport function clearIndexes(memoryDir: string): void {\n try {\n ensureStateDir(memoryDir);\n updateTemporalIndex(memoryDir, (index) => {\n index.version = INDEX_VERSION;\n index.lastRebuildAt = undefined;\n index.dates = {};\n });\n updateTagIndex(memoryDir, (index) => {\n index.version = TAG_INDEX_VERSION;\n index.lastRebuildAt = undefined;\n index.tags = {};\n index.aliases = {};\n });\n } catch {\n // Fail silently — indexes are advisory only\n }\n}\n\n/**\n * Returns true when both index files exist on disk.\n * Used to detect first-time enablement so callers can trigger a full rebuild.\n */\nexport function indexesExist(memoryDir: string): boolean {\n try {\n return fs.existsSync(temporalIndexPath(memoryDir)) && fs.existsSync(tagIndexPath(memoryDir));\n } catch {\n return false;\n }\n}\n\n/**\n * Batch-add multiple memories to both indexes in a single read-modify-write cycle.\n * More efficient than calling indexMemory() per file when adding many at once.\n */\nexport function indexMemoriesBatch(\n memoryDir: string,\n entries: Array<{ path: string; createdAt: string; tags: string[] }>\n): void {\n if (entries.length === 0) return;\n try {\n ensureStateDir(memoryDir);\n\n updateTemporalIndex(memoryDir, (index) => {\n for (const entry of entries) {\n const dateKey = isoDateFromTimestamp(entry.createdAt);\n addPathToSet(index.dates, dateKey, entry.path);\n }\n });\n\n updateTagIndex(memoryDir, (index) => {\n for (const entry of entries) {\n for (const tag of entry.tags) {\n if (tag && typeof tag === \"string\") {\n addTagGraphEntry(index, tag, entry.path);\n }\n }\n }\n });\n } catch {\n // Fail silently\n }\n}\n\n/**\n * Return the set of memory paths whose index date falls in [fromDate, toDate).\n *\n * Boundary semantics (exclusive upper bound):\n * - `fromDate` is INCLUSIVE — entries on this date ARE returned.\n * - `toDate` is EXCLUSIVE — entries on this date are NOT returned.\n * Pass `recencyWindowBoundsFromPrompt(query).toDate` to get the correct\n * exclusive boundary; do NOT add +1 day yourself.\n * - Default `toDate` = tomorrow, so omitting it includes all of today.\n *\n * @param memoryDir - root memory directory (contains state/index_time.json)\n * @param fromDate - inclusive start date, YYYY-MM-DD\n * @param toDate - exclusive end date, YYYY-MM-DD (default: tomorrow)\n * @returns Set of matching file paths, or null if the index is unavailable.\n */\nexport async function queryByDateRangeAsync(\n memoryDir: string,\n fromDate: string,\n toDate?: string\n): Promise<Set<string> | null> {\n try {\n const tPath = temporalIndexPath(memoryDir);\n let raw: string;\n try {\n raw = await fs.promises.readFile(tPath, \"utf8\");\n } catch {\n return null; // File missing or unreadable\n }\n let tIndex: TemporalIndex;\n try {\n tIndex = JSON.parse(raw) as TemporalIndex;\n } catch {\n tIndex = { version: INDEX_VERSION, dates: {} };\n }\n // toDate is exclusive (first day NOT included). Default: tomorrow so \"all of today\" is included.\n const end = toDate ?? new Date(Date.now() + 86_400_000).toISOString().slice(0, 10);\n\n const results = new Set<string>();\n for (const [date, paths] of Object.entries(tIndex.dates)) {\n if (date >= fromDate && date < end) {\n for (const p of paths) {\n results.add(p);\n }\n }\n }\n return results;\n } catch {\n return null;\n }\n}\n\n/**\n * Async version of queryByTags — uses non-blocking fs.promises.readFile\n * to avoid blocking the Node.js event loop.\n */\nexport async function queryByTagsAsync(memoryDir: string, tags: string[]): Promise<Set<string> | null> {\n if (tags.length === 0) return null;\n try {\n const gPath = tagIndexPath(memoryDir);\n let raw: string;\n try {\n raw = await fs.promises.readFile(gPath, \"utf8\");\n } catch {\n return null; // File missing or unreadable\n }\n let gIndex: TagIndex;\n try {\n gIndex = normalizeTagIndex(JSON.parse(raw) as TagIndex);\n } catch {\n gIndex = { version: TAG_INDEX_VERSION, tags: {}, aliases: {} };\n }\n\n return queryByTagsFromIndex(gIndex, tags);\n } catch {\n return null;\n }\n}\n\nfunction queryByTagsFromIndex(index: TagIndex, tags: string[]): Set<string> | null {\n const expandedTags = expandCanonicalTags(index, tags);\n const results = new Set<string>();\n for (const canonical of expandedTags) {\n const nodeOrPaths = index.tags[canonical];\n const paths = Array.isArray(nodeOrPaths) ? nodeOrPaths : (nodeOrPaths?.paths ?? []);\n for (const pathValue of paths) {\n results.add(pathValue);\n }\n }\n return results.size > 0 ? results : null;\n}\n\n/**\n * Extract tags from a prompt for tag-based prefiltering.\n * Looks for hashtag-style tokens (#foo).\n * Returns lowercase, deduplicated list.\n */\nexport function extractTagsFromPrompt(prompt: string): string[] {\n const found = new Set<string>();\n\n // Match #tag style tokens\n const hashMatches = prompt.matchAll(/#([a-zA-Z][\\w-]{1,30})/g);\n for (const m of hashMatches) {\n const canonical = normalizeCanonicalTag(m[1]);\n if (canonical) found.add(canonical);\n }\n\n return Array.from(found);\n}\n\nexport async function resolvePromptTagPrefilterAsync(\n memoryDir: string,\n prompt: string\n): Promise<{\n matchedTags: string[];\n expandedTags: string[];\n paths: Set<string> | null;\n}> {\n const explicitTags = extractTagsFromPrompt(prompt);\n try {\n const raw = await fs.promises.readFile(tagIndexPath(memoryDir), \"utf8\");\n const tagIndex = normalizeTagIndex(JSON.parse(raw) as TagIndex);\n const matched = new Set<string>(explicitTags);\n\n for (const canonical of Object.keys(tagIndex.tags)) {\n if (promptContainsAlias(prompt, canonical)) {\n matched.add(canonical);\n }\n }\n for (const [alias, canonicals] of Object.entries(tagIndex.aliases ?? {})) {\n if (!promptContainsAlias(prompt, alias)) continue;\n for (const canonical of canonicals) {\n matched.add(canonical);\n }\n }\n\n const expandedTags = expandCanonicalTags(tagIndex, Array.from(matched));\n const paths = queryByTagsFromIndex(tagIndex, expandedTags);\n return {\n matchedTags: Array.from(matched),\n expandedTags,\n paths,\n };\n } catch {\n return { matchedTags: explicitTags, expandedTags: explicitTags, paths: null };\n }\n}\n\n/**\n * Detect if a prompt is time-sensitive (mentions specific time references).\n * Used to decide whether to activate the temporal prefilter.\n */\nexport function isTemporalQuery(prompt: string): boolean {\n return /\\b(today|yesterday|this week|last week|this month|last month|recent(?:ly)?|lately|just now|earlier today|this morning|last night|last year|this year|\\d+ days? ago|\\d+ hours? ago|\\d+ weeks? ago|\\d+ months? ago|(?:in |on |during |since |before |after )?(?:january|february|march|april|may|june|july|august|september|october|november|december)(?:\\s+\\d{1,4})?|\\d{4}-\\d{2}-\\d{2}|\\d{1,2}\\/\\d{1,2}\\/\\d{2,4}|(?:spring|summer|fall|autumn|winter)\\s+\\d{4}|on the \\d{1,2}(?:st|nd|rd|th)?|last (?:monday|tuesday|wednesday|thursday|friday|saturday|sunday))\\b/i.test(\n prompt\n );\n}\n\n/**\n * Compute a \"from date\" string (YYYY-MM-DD) for a recency-based temporal query.\n * For \"recent\" / \"lately\" returns 7 days ago; for today/yesterday the obvious window.\n */\nexport function recencyWindowFromPrompt(prompt: string, nowMs: number = Date.now()): string {\n const p = prompt.toLowerCase();\n const now = new Date(nowMs);\n let daysBack = 7; // default\n\n if (/\\btoday\\b/.test(p) || /\\bthis morning\\b/.test(p) || /\\bjust now\\b/.test(p) || /\\bearlier today\\b/.test(p)) {\n daysBack = 0; // fromDate = today → window [today, today]\n } else if (/\\byesterday\\b/.test(p) || /\\blast night\\b/.test(p)) {\n daysBack = 1; // fromDate = yesterday → window [yesterday, today]\n } else if (/\\bthis week\\b/.test(p)) {\n daysBack = 7;\n } else if (/\\blast week\\b/.test(p)) {\n daysBack = 14;\n } else if (/\\bthis month\\b/.test(p)) {\n daysBack = 31;\n } else if (/\\blast month\\b/.test(p)) {\n daysBack = 62;\n } else if (/\\bthis year\\b/.test(p)) {\n // From Jan 1 of current year\n const jan1 = new Date(now.getFullYear(), 0, 1);\n return jan1.toISOString().slice(0, 10);\n } else if (/\\blast year\\b/.test(p)) {\n const jan1LastYear = new Date(now.getFullYear() - 1, 0, 1);\n return jan1LastYear.toISOString().slice(0, 10);\n } else {\n // Try specific month references: \"in March\", \"during January\", \"since February\"\n const monthNames = [\n \"january\",\n \"february\",\n \"march\",\n \"april\",\n \"may\",\n \"june\",\n \"july\",\n \"august\",\n \"september\",\n \"october\",\n \"november\",\n \"december\",\n ];\n const monthMatch = p.match(\n /\\b(january|february|march|april|may|june|july|august|september|october|november|december)(?:\\s+(\\d{4}))?\\b/\n );\n if (monthMatch) {\n const monthIdx = monthNames.indexOf(monthMatch[1]);\n const year = monthMatch[2] ? Number.parseInt(monthMatch[2], 10) : now.getFullYear();\n // \"before <month>\" means everything prior to that month: use 2-year lookback\n // as fromDate so the window isn't unbounded. toDate is set to the month start\n // in recencyWindowBoundsFromPrompt.\n // IMPORTANT: when an explicit year is given, anchor the lookback to the named\n // month start (not to today). \"before January 2024\" should produce\n // fromDate ≈ 2022-01 — not today-730 days, which could land after 2024-01 and\n // invert the window for any explicitly past month within the 730-day horizon.\n if (/\\bbefore\\b/.test(p)) {\n if (monthMatch[2]) {\n // Explicit year: anchor fromDate 730 days before the named month start.\n const monthStart = new Date(year, monthIdx, 1);\n return new Date(monthStart.getTime() - 730 * 86_400_000).toISOString().slice(0, 10);\n }\n daysBack = 730;\n } else {\n const monthStart = new Date(year, monthIdx, 1);\n return monthStart.toISOString().slice(0, 10);\n }\n }\n\n // Try \"N weeks ago\"\n const weekMatch = p.match(/(\\d{1,5})\\s*weeks?\\s*ago/);\n if (weekMatch) {\n daysBack = Math.min(365, Number.parseInt(weekMatch[1], 10) * 7);\n } else {\n // Try \"N months ago\"\n const monthsAgoMatch = p.match(/(\\d{1,5})\\s*months?\\s*ago/);\n if (monthsAgoMatch) {\n daysBack = Math.min(730, Number.parseInt(monthsAgoMatch[1], 10) * 31);\n } else {\n const numMatch = p.match(/(\\d{1,5})\\s*days?\\s*ago/);\n if (numMatch) {\n daysBack = Math.min(365, Number.parseInt(numMatch[1], 10)); // no off-by-one: \"3 days ago\" → 3\n } else {\n const hrMatch = p.match(/(\\d{1,5})\\s*hours?\\s*ago/);\n if (hrMatch) {\n // Convert hours to days (ceiling); at least 1 day window\n daysBack = Math.max(1, Math.ceil(Number.parseInt(hrMatch[1], 10) / 24));\n }\n }\n }\n }\n\n // Try explicit date patterns: YYYY-MM-DD or MM/DD/YYYY\n const isoMatch = p.match(/(\\d{4})-(\\d{2})-(\\d{2})/);\n if (isoMatch) {\n return `${isoMatch[1]}-${isoMatch[2]}-${isoMatch[3]}`;\n }\n const usMatch = p.match(/(\\d{1,2})\\/(\\d{1,2})\\/(\\d{2,4})/);\n if (usMatch) {\n const year = usMatch[3].length === 2 ? 2000 + Number.parseInt(usMatch[3], 10) : Number.parseInt(usMatch[3], 10);\n return `${year}-${usMatch[1].padStart(2, \"0\")}-${usMatch[2].padStart(2, \"0\")}`;\n }\n\n // Try \"last Monday/Tuesday/etc\"\n const dayOfWeekMatch = p.match(/\\blast\\s+(monday|tuesday|wednesday|thursday|friday|saturday|sunday)\\b/);\n if (dayOfWeekMatch) {\n const dayNames = [\"sunday\", \"monday\", \"tuesday\", \"wednesday\", \"thursday\", \"friday\", \"saturday\"];\n const targetDay = dayNames.indexOf(dayOfWeekMatch[1]);\n const currentDay = now.getDay();\n daysBack = (currentDay - targetDay + 7) % 7 || 7; // at least 7 days back\n }\n }\n\n const from = new Date(nowMs - daysBack * 24 * 60 * 60 * 1000);\n return from.toISOString().slice(0, 10);\n}\n\n/**\n * Returns both the start and end of the temporal window implied by the prompt.\n *\n * `fromDate` is computed by delegating to `recencyWindowFromPrompt`, so the two\n * functions cannot diverge on the lower bound. `toDate` is computed independently\n * here using the same pattern-priority ordering, because the upper-bound arithmetic\n * differs (exclusive end vs. inclusive start) and would not be expressible as a\n * simple wrapper.\n *\n * Known divergence to be aware of: for \"N hours ago\", `recencyWindowFromPrompt`\n * uses `Math.max(1, Math.ceil(hours/24))` days back, so `fromDate` = yesterday for\n * sub-24h values. `toDate` here is always `tomorrow` when no explicit date is\n * specified (see the `toDaysBack === 0` branch), giving a 2-day window. This is\n * intentional — the window must cover both yesterday and today for recent hour-level\n * queries. Any fix to the hours formula in `recencyWindowFromPrompt` must be\n * manually reflected here.\n *\n * - `fromDate`: first day of the implied window (same as `recencyWindowFromPrompt`)\n * - `toDate`: first day NOT in the window (exclusive upper bound), so callers\n * should filter with `date >= fromDate && date < toDate`.\n */\nexport function recencyWindowBoundsFromPrompt(\n prompt: string,\n nowMs: number = Date.now()\n): { fromDate: string; toDate: string } {\n const fromDate = recencyWindowFromPrompt(prompt, nowMs);\n const p = prompt.toLowerCase();\n const now = new Date(nowMs);\n const today = now.toISOString().slice(0, 10);\n const tomorrow = new Date(nowMs + 86_400_000).toISOString().slice(0, 10);\n\n let toDate: string;\n\n if (/\\btoday\\b|\\bthis morning\\b|\\bjust now\\b|\\bearlier today\\b/.test(p)) {\n toDate = tomorrow; // exclusive: include all of today\n } else if (/\\byesterday\\b|\\blast night\\b/.test(p)) {\n toDate = today; // exclusive: include all of yesterday, stop before today\n } else if (/\\bthis week\\b|\\bthis month\\b|\\bthis year\\b/.test(p)) {\n toDate = tomorrow; // exclusive: include all of today\n } else if (/\\blast week\\b/.test(p)) {\n toDate = new Date(nowMs - 7 * 86_400_000).toISOString().slice(0, 10); // already exclusive\n } else if (/\\blast month\\b/.test(p)) {\n toDate = new Date(nowMs - 31 * 86_400_000).toISOString().slice(0, 10); // approximately exclusive\n } else if (/\\blast year\\b/.test(p)) {\n toDate = `${now.getFullYear()}-01-01`; // exclusive: stop at Jan 1 of current year\n } else {\n // Mirror the structure of recencyWindowFromPrompt's else branch:\n // month name → early set (highest priority), then ago patterns set a\n // working offset, then ISO/US/weekday patterns run AFTER ago patterns\n // and can override them — matching the priority ordering in recencyWindowFromPrompt.\n\n const monthNames = [\n \"january\",\n \"february\",\n \"march\",\n \"april\",\n \"may\",\n \"june\",\n \"july\",\n \"august\",\n \"september\",\n \"october\",\n \"november\",\n \"december\",\n ];\n const monthMatch = p.match(\n /\\b(january|february|march|april|may|june|july|august|september|october|november|december)(?:\\s+(\\d{4}))?\\b/\n );\n if (monthMatch) {\n // \"since <month>\" / \"after <month>\" — open-ended: everything from that month to now.\n // \"before <month>\" — closed upper bound: everything before that month starts.\n // Plain \"<month>\" — just that calendar month.\n const monthIdx = monthNames.indexOf(monthMatch[1]);\n const year = monthMatch[2] ? Number.parseInt(monthMatch[2], 10) : now.getFullYear();\n const isSinceOrAfter = /\\b(since|after)\\b/.test(p);\n const isBefore = /\\bbefore\\b/.test(p);\n if (isSinceOrAfter) {\n toDate = tomorrow;\n } else if (isBefore) {\n // \"before <month>\" means everything prior to that month.\n // toDate = first day of that month (exclusive upper bound).\n toDate = new Date(year, monthIdx, 1).toISOString().slice(0, 10);\n } else {\n // Closed window: first day of the NEXT month is the exclusive upper bound.\n toDate = new Date(year, monthIdx + 1, 1).toISOString().slice(0, 10);\n }\n } else {\n // Ago patterns set a working offset (recent edge of the N-ago window).\n // Same nesting order as recencyWindowFromPrompt.\n // Sentinel -1 means \"no ago pattern matched\" → toDate falls back to tomorrow.\n let toDaysBack = -1;\n const weekMatch = p.match(/(\\d{1,5})\\s*weeks?\\s*ago/);\n if (weekMatch) {\n toDaysBack = Math.max(0, Math.min(52, Number.parseInt(weekMatch[1], 10)) - 1) * 7;\n } else {\n const monthsAgoMatch = p.match(/(\\d{1,5})\\s*months?\\s*ago/);\n if (monthsAgoMatch) {\n toDaysBack = Math.max(0, Math.min(24, Number.parseInt(monthsAgoMatch[1], 10)) - 1) * 31;\n } else {\n const numMatch = p.match(/(\\d{1,5})\\s*days?\\s*ago/);\n if (numMatch) {\n // (N-1) mirrors the weeks/months ago formula: \"3 days ago\" → window [today-3, today-2]\n // N=1 → toDaysBack=0 → toDate=today (exclusive) → window [yesterday, today) = 1 day. ✓\n toDaysBack = Math.max(0, Math.min(365, Number.parseInt(numMatch[1], 10)) - 1);\n } else {\n const hrMatch = p.match(/(\\d{1,5})\\s*hours?\\s*ago/);\n if (hrMatch) {\n // Sub-day precision: keep sentinel -1 so toDate falls back to tomorrow,\n // which includes today. fromDate = yesterday (recencyWindowFromPrompt uses ceiling).\n toDaysBack = -1;\n }\n }\n }\n }\n\n // Explicit date/weekday patterns run AFTER ago patterns (same as recencyWindowFromPrompt)\n // and override the ago-derived offset when present.\n const isoMatch = p.match(/(\\d{4})-(\\d{2})-(\\d{2})/);\n if (isoMatch) {\n // +1 day: exclusive upper bound includes the named date\n const d = new Date(`${isoMatch[1]}-${isoMatch[2]}-${isoMatch[3]}T00:00:00Z`);\n toDate = new Date(d.getTime() + 86_400_000).toISOString().slice(0, 10);\n } else {\n const usMatch = p.match(/(\\d{1,2})\\/(\\d{1,2})\\/(\\d{2,4})/);\n if (usMatch) {\n const year =\n usMatch[3].length === 2 ? 2000 + Number.parseInt(usMatch[3], 10) : Number.parseInt(usMatch[3], 10);\n // +1 day: exclusive upper bound includes the named date\n const d = new Date(`${year}-${usMatch[1].padStart(2, \"0\")}-${usMatch[2].padStart(2, \"0\")}T00:00:00Z`);\n toDate = new Date(d.getTime() + 86_400_000).toISOString().slice(0, 10);\n } else {\n const dayOfWeekMatch = p.match(/\\blast\\s+(monday|tuesday|wednesday|thursday|friday|saturday|sunday)\\b/);\n if (dayOfWeekMatch) {\n const dayNames = [\"sunday\", \"monday\", \"tuesday\", \"wednesday\", \"thursday\", \"friday\", \"saturday\"];\n const targetDay = dayNames.indexOf(dayOfWeekMatch[1]);\n const currentDay = now.getDay();\n const daysBack = (currentDay - targetDay + 7) % 7 || 7;\n // +1 day: exclusive upper bound includes the named weekday\n toDate = new Date(nowMs - (daysBack - 1) * 86_400_000).toISOString().slice(0, 10);\n } else {\n // Use the ago-derived offset.\n // toDaysBack=-1 means no pattern matched (or hours-ago): use tomorrow so today\n // is included in the window. toDaysBack=0 means N=1 ago (e.g. \"1 day ago\"):\n // toDate = today (exclusive) correctly creates a 1-day window [yesterday, today).\n toDate = toDaysBack < 0 ? tomorrow : new Date(nowMs - toDaysBack * 86_400_000).toISOString().slice(0, 10);\n }\n }\n }\n }\n }\n\n // Guard: if toDate would precede fromDate (inverted window from conflicting keywords),\n // fall back to tomorrow (exclusive upper bound that covers today) so we never produce\n // an empty window. Using `today` is insufficient because `date < today` excludes today.\n if (toDate <= fromDate) toDate = tomorrow;\n\n return { fromDate, toDate };\n}\n"],"mappings":";AAiBA,SAAS,oBAAoB;AAC7B,YAAY,YAAY;AACxB,YAAY,QAAQ;AACpB,YAAY,UAAU;AA0BtB,IAAM,gBAAgB;AACtB,IAAM,sBAAsB;AAC5B,IAAM,iBAAiB;AACvB,IAAM,oBAAoB;AAC1B,IAAM,sBAAsB;AAC5B,IAAM,qBAAqB;AAC3B,IAAM,mCAAmC;AACzC,IAAM,mBAAmB,IAAI,WAAW,IAAI,kBAAkB,CAAC,CAAC;AAChE,IAAM,8BAA8B,KAAK,IAAI,IAAI,QAAQ,OAAO,IAAI;AAUpE,SAAS,SAAS,WAA2B;AAC3C,SAAY,UAAK,WAAW,OAAO;AACrC;AAEA,SAAS,kBAAkB,WAA2B;AACpD,SAAY,UAAK,SAAS,SAAS,GAAG,mBAAmB;AAC3D;AAEA,SAAS,aAAa,WAA2B;AAC/C,SAAY,UAAK,SAAS,SAAS,GAAG,cAAc;AACtD;AAEA,SAAS,eA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AAK,KAAU,EAAE,YAAY,EAAE,MAAM,GAAG,EAAE;AAAA,UAClF,OAAO;AAKL,qBAAS,aAAa,IAAI,WAAW,IAAI,KAAK,QAAQ,aAAa,KAAU,EAAE,YAAY,EAAE,MAAM,GAAG,EAAE;AAAA,UAC1G;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAAA,EACF;AAKA,MAAI,UAAU,SAAU,UAAS;AAEjC,SAAO,EAAE,UAAU,OAAO;AAC5B;","names":[]}
@@ -2464,6 +2464,30 @@ ${sanitized.text}
2464
2464
  const hashSource = configuredHashSource !== memory.content ? configuredHashSource : stripDefaultCitationMarkersWithoutRegex(memory.content);
2465
2465
  return ContentHashIndex.computeHash(sanitizeMemoryContent(hashSource).text);
2466
2466
  }
2467
+ async addActiveFactContentHash(memory) {
2468
+ if (memory.frontmatter.category !== "fact") return;
2469
+ if (inferMemoryStatus(memory.frontmatter, memory.path) !== "active") return;
2470
+ const hash = this.factContentHashForRemoval(memory);
2471
+ if (!hash) return;
2472
+ await this.ensureFactHashIndexAuthoritative();
2473
+ const factHashIndex = await this.getFactHashIndex();
2474
+ factHashIndex.addByHash(hash);
2475
+ await factHashIndex.save();
2476
+ }
2477
+ async syncFactHashIndexAfterRewrite(before, after) {
2478
+ if (before.frontmatter.category !== "fact" && after.frontmatter.category !== "fact") return;
2479
+ const beforeHash = this.factContentHashForRemoval(before);
2480
+ const afterHash = this.factContentHashForRemoval(after);
2481
+ const beforeStatus = inferMemoryStatus(before.frontmatter, before.path);
2482
+ const afterStatus = inferMemoryStatus(after.frontmatter, after.path);
2483
+ if (beforeHash === afterHash && beforeStatus === afterStatus) return;
2484
+ if (beforeStatus === "active" && beforeHash && (beforeHash !== afterHash || afterStatus !== "active")) {
2485
+ await this.removeFactContentHashesForMemories([before]);
2486
+ }
2487
+ if (afterStatus === "active" && afterHash && (beforeHash !== afterHash || beforeStatus !== "active")) {
2488
+ await this.addActiveFactContentHash(after);
2489
+ }
2490
+ }
2467
2491
  async removeFactContentHashesForMemories(memories) {
2468
2492
  await this.ensureFactHashIndexAuthoritative();
2469
2493
  const factHashIndex = await this.getFactHashIndex();
@@ -3463,6 +3487,17 @@ ${memory.content}
3463
3487
  if (memory.path.includes(`${path.sep}cold${path.sep}`)) {
3464
3488
  this.invalidateColdMemoriesCache();
3465
3489
  }
3490
+ try {
3491
+ await this.syncFactHashIndexAfterRewrite(
3492
+ memory,
3493
+ {
3494
+ ...memory,
3495
+ frontmatter: updated
3496
+ }
3497
+ );
3498
+ } catch (err) {
3499
+ log.warn(`storage.writeMemoryFrontmatter completed but failed to update fact hash index: ${err}`);
3500
+ }
3466
3501
  await this.appendGeneratedMemoryLifecycleEventFailOpen(
3467
3502
  "storage.writeMemoryFrontmatter",
3468
3503
  {
@@ -5326,4 +5361,4 @@ export {
5326
5361
  serializeEntityFile,
5327
5362
  StorageManager
5328
5363
  };
5329
- //# sourceMappingURL=chunk-IRFF6LSF.js.map
5364
+ //# sourceMappingURL=chunk-YFS5OEKO.js.map