@remnic/core 9.3.603 → 9.3.605

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 (64) hide show
  1. package/dist/access-cli.js +9 -9
  2. package/dist/access-schema.d.ts +34 -34
  3. package/dist/{chunk-JTDRJQ3K.js → chunk-6JGNHWCI.js} +2 -2
  4. package/dist/{chunk-M6I5Z4SR.js → chunk-6ZZP4EJF.js} +2 -2
  5. package/dist/{chunk-75O6YQ63.js → chunk-G6R5UD3Q.js} +10 -3
  6. package/dist/chunk-G6R5UD3Q.js.map +1 -0
  7. package/dist/{chunk-I4UNL747.js → chunk-GMAG2HS4.js} +7 -3
  8. package/dist/chunk-GMAG2HS4.js.map +1 -0
  9. package/dist/{chunk-5SQ5CQJP.js → chunk-HSVJGWYS.js} +2 -2
  10. package/dist/{chunk-RUYYYLDT.js → chunk-IOTENEVL.js} +3 -3
  11. package/dist/{chunk-M46RYSMW.js → chunk-JHMFYY7L.js} +5 -2
  12. package/dist/chunk-JHMFYY7L.js.map +1 -0
  13. package/dist/{chunk-2ESBDLT5.js → chunk-VJXSUAO7.js} +2 -2
  14. package/dist/{chunk-Z5AAYHUC.js → chunk-VOUOLGIP.js} +13 -2
  15. package/dist/chunk-VOUOLGIP.js.map +1 -0
  16. package/dist/{chunk-OB6353F7.js → chunk-XPSVGJYA.js} +4 -4
  17. package/dist/{chunk-MTGOAU7A.js → chunk-YJ6QCQNE.js} +336 -300
  18. package/dist/chunk-YJ6QCQNE.js.map +1 -0
  19. package/dist/{chunk-6PTSXBPE.js → chunk-ZDTVJXIP.js} +5 -2
  20. package/dist/chunk-ZDTVJXIP.js.map +1 -0
  21. package/dist/cli.js +8 -8
  22. package/dist/{first-start-migration-CKTCTCQI.js → first-start-migration-GYJWIH36.js} +2 -2
  23. package/dist/index.js +12 -12
  24. package/dist/namespaces/migrate.js +6 -6
  25. package/dist/namespaces/search.js +5 -5
  26. package/dist/operator-toolkit.js +7 -7
  27. package/dist/orchestrator.js +9 -9
  28. package/dist/retrieval-agents.js +2 -2
  29. package/dist/schemas.d.ts +42 -42
  30. package/dist/search/factory.js +4 -4
  31. package/dist/search/index.js +4 -4
  32. package/dist/search/lancedb-backend.d.ts +1 -0
  33. package/dist/search/lancedb-backend.js +1 -1
  34. package/dist/search/meilisearch-backend.d.ts +1 -0
  35. package/dist/search/meilisearch-backend.js +1 -1
  36. package/dist/search/orama-backend.d.ts +1 -0
  37. package/dist/search/orama-backend.js +1 -1
  38. package/dist/search/port.d.ts +1 -0
  39. package/dist/shared-context/manager.d.ts +2 -2
  40. package/dist/temporal-index.js +1 -1
  41. package/dist/tier-migration.d.ts +1 -0
  42. package/dist/tier-migration.js +1 -1
  43. package/package.json +1 -1
  44. package/src/orchestrator.ts +51 -8
  45. package/src/search/lancedb-backend.ts +5 -1
  46. package/src/search/meilisearch-backend.ts +7 -2
  47. package/src/search/orama-backend.ts +5 -1
  48. package/src/search/port.ts +1 -0
  49. package/src/temporal-index.test.ts +30 -1
  50. package/src/temporal-index.ts +10 -3
  51. package/src/tier-migration.ts +13 -2
  52. package/dist/chunk-6PTSXBPE.js.map +0 -1
  53. package/dist/chunk-75O6YQ63.js.map +0 -1
  54. package/dist/chunk-I4UNL747.js.map +0 -1
  55. package/dist/chunk-M46RYSMW.js.map +0 -1
  56. package/dist/chunk-MTGOAU7A.js.map +0 -1
  57. package/dist/chunk-Z5AAYHUC.js.map +0 -1
  58. /package/dist/{chunk-JTDRJQ3K.js.map → chunk-6JGNHWCI.js.map} +0 -0
  59. /package/dist/{chunk-M6I5Z4SR.js.map → chunk-6ZZP4EJF.js.map} +0 -0
  60. /package/dist/{chunk-5SQ5CQJP.js.map → chunk-HSVJGWYS.js.map} +0 -0
  61. /package/dist/{chunk-RUYYYLDT.js.map → chunk-IOTENEVL.js.map} +0 -0
  62. /package/dist/{chunk-2ESBDLT5.js.map → chunk-VJXSUAO7.js.map} +0 -0
  63. /package/dist/{chunk-OB6353F7.js.map → chunk-XPSVGJYA.js.map} +0 -0
  64. /package/dist/{first-start-migration-CKTCTCQI.js.map → first-start-migration-GYJWIH36.js.map} +0 -0
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  Orchestrator
3
- } from "./chunk-MTGOAU7A.js";
3
+ } from "./chunk-YJ6QCQNE.js";
4
4
  import "./chunk-5RIRL3XL.js";
5
5
  import "./chunk-KVEVLBKC.js";
6
6
  import "./chunk-BFBF3XEF.js";
@@ -23,14 +23,14 @@ import "./chunk-UHGBNIOS.js";
23
23
  import "./chunk-VWT3F4IV.js";
24
24
  import "./chunk-K5O2QY6T.js";
25
25
  import "./chunk-MDYG7VI7.js";
26
- import "./chunk-Z5AAYHUC.js";
26
+ import "./chunk-VOUOLGIP.js";
27
27
  import "./chunk-S75M5ZRK.js";
28
28
  import "./chunk-46GJIW5M.js";
29
29
  import "./chunk-XJNBEDFE.js";
30
30
  import "./chunk-DLJ4IR6M.js";
31
31
  import "./chunk-OZKZ2TRP.js";
32
- import "./chunk-5SQ5CQJP.js";
33
- import "./chunk-75O6YQ63.js";
32
+ import "./chunk-HSVJGWYS.js";
33
+ import "./chunk-G6R5UD3Q.js";
34
34
  import "./chunk-5IZL4DCV.js";
35
35
  import "./chunk-X7XN6YU4.js";
36
36
  import "./chunk-452WDNFO.js";
@@ -82,14 +82,14 @@ import "./chunk-ZRWB5D4H.js";
82
82
  import "./chunk-A6D7A2FW.js";
83
83
  import "./chunk-FF4KLI5W.js";
84
84
  import "./chunk-SFQ6QNL7.js";
85
- import "./chunk-M6I5Z4SR.js";
86
- import "./chunk-OB6353F7.js";
85
+ import "./chunk-6ZZP4EJF.js";
86
+ import "./chunk-XPSVGJYA.js";
87
87
  import "./chunk-JNANKJLN.js";
88
88
  import "./chunk-EWLQPEO6.js";
89
- import "./chunk-6PTSXBPE.js";
90
- import "./chunk-I4UNL747.js";
89
+ import "./chunk-ZDTVJXIP.js";
90
+ import "./chunk-GMAG2HS4.js";
91
91
  import "./chunk-2I5JGH3M.js";
92
- import "./chunk-M46RYSMW.js";
92
+ import "./chunk-JHMFYY7L.js";
93
93
  import "./chunk-CINZGPSJ.js";
94
94
  import "./chunk-Q4CAQGKQ.js";
95
95
  import "./chunk-IK7DCC5H.js";
@@ -104,9 +104,9 @@ declare const recallRequestSchema: z.ZodObject<{
104
104
  includeLowConfidence: z.ZodOptional<z.ZodBoolean>;
105
105
  }, "strip", z.ZodTypeAny, {
106
106
  query: string;
107
- namespace?: string | undefined;
108
107
  sessionKey?: string | undefined;
109
108
  tags?: string[] | undefined;
109
+ namespace?: string | undefined;
110
110
  topK?: number | undefined;
111
111
  mode?: "no_recall" | "minimal" | "full" | "graph_mode" | "auto" | undefined;
112
112
  cwd?: string | undefined;
@@ -125,9 +125,9 @@ declare const recallRequestSchema: z.ZodObject<{
125
125
  tagMatch?: "all" | "any" | undefined;
126
126
  }, {
127
127
  query: string;
128
- namespace?: string | undefined;
129
128
  sessionKey?: string | undefined;
130
129
  tags?: string[] | undefined;
130
+ namespace?: string | undefined;
131
131
  topK?: number | undefined;
132
132
  mode?: "no_recall" | "minimal" | "full" | "graph_mode" | "auto" | undefined;
133
133
  cwd?: string | undefined;
@@ -149,11 +149,11 @@ declare const recallExplainRequestSchema: z.ZodObject<{
149
149
  sessionKey: z.ZodOptional<z.ZodString>;
150
150
  namespace: z.ZodOptional<z.ZodString>;
151
151
  }, "strip", z.ZodTypeAny, {
152
- namespace?: string | undefined;
153
152
  sessionKey?: string | undefined;
154
- }, {
155
153
  namespace?: string | undefined;
154
+ }, {
156
155
  sessionKey?: string | undefined;
156
+ namespace?: string | undefined;
157
157
  }>;
158
158
  /**
159
159
  * Standalone "set coding context" request. Used by the HTTP endpoint
@@ -363,28 +363,28 @@ declare const memoryStoreRequestSchema: z.ZodObject<{
363
363
  sourceReason: z.ZodOptional<z.ZodString>;
364
364
  }, "strip", z.ZodTypeAny, {
365
365
  content: string;
366
- namespace?: string | undefined;
367
- category?: "fact" | "preference" | "correction" | "entity" | "decision" | "relationship" | "principle" | "commitment" | "moment" | "skill" | "rule" | "procedure" | "reasoning_trace" | undefined;
368
- confidence?: number | undefined;
369
- ttl?: string | undefined;
370
366
  schemaVersion?: number | undefined;
371
367
  sessionKey?: string | undefined;
372
368
  tags?: string[] | undefined;
373
369
  dryRun?: boolean | undefined;
370
+ namespace?: string | undefined;
371
+ category?: "fact" | "preference" | "correction" | "entity" | "decision" | "relationship" | "principle" | "commitment" | "moment" | "skill" | "rule" | "procedure" | "reasoning_trace" | undefined;
372
+ confidence?: number | undefined;
374
373
  entityRef?: string | undefined;
374
+ ttl?: string | undefined;
375
375
  sourceReason?: string | undefined;
376
376
  idempotencyKey?: string | undefined;
377
377
  }, {
378
378
  content: string;
379
- namespace?: string | undefined;
380
- category?: "fact" | "preference" | "correction" | "entity" | "decision" | "relationship" | "principle" | "commitment" | "moment" | "skill" | "rule" | "procedure" | "reasoning_trace" | undefined;
381
- confidence?: number | undefined;
382
- ttl?: string | undefined;
383
379
  schemaVersion?: number | undefined;
384
380
  sessionKey?: string | undefined;
385
381
  tags?: string[] | undefined;
386
382
  dryRun?: boolean | undefined;
383
+ namespace?: string | undefined;
384
+ category?: "fact" | "preference" | "correction" | "entity" | "decision" | "relationship" | "principle" | "commitment" | "moment" | "skill" | "rule" | "procedure" | "reasoning_trace" | undefined;
385
+ confidence?: number | undefined;
387
386
  entityRef?: string | undefined;
387
+ ttl?: string | undefined;
388
388
  sourceReason?: string | undefined;
389
389
  idempotencyKey?: string | undefined;
390
390
  }>;
@@ -403,28 +403,28 @@ declare const suggestionSubmitRequestSchema: z.ZodObject<{
403
403
  sourceReason: z.ZodOptional<z.ZodString>;
404
404
  }, "strip", z.ZodTypeAny, {
405
405
  content: string;
406
- namespace?: string | undefined;
407
- category?: "fact" | "preference" | "correction" | "entity" | "decision" | "relationship" | "principle" | "commitment" | "moment" | "skill" | "rule" | "procedure" | "reasoning_trace" | undefined;
408
- confidence?: number | undefined;
409
- ttl?: string | undefined;
410
406
  schemaVersion?: number | undefined;
411
407
  sessionKey?: string | undefined;
412
408
  tags?: string[] | undefined;
413
409
  dryRun?: boolean | undefined;
410
+ namespace?: string | undefined;
411
+ category?: "fact" | "preference" | "correction" | "entity" | "decision" | "relationship" | "principle" | "commitment" | "moment" | "skill" | "rule" | "procedure" | "reasoning_trace" | undefined;
412
+ confidence?: number | undefined;
414
413
  entityRef?: string | undefined;
414
+ ttl?: string | undefined;
415
415
  sourceReason?: string | undefined;
416
416
  idempotencyKey?: string | undefined;
417
417
  }, {
418
418
  content: string;
419
- namespace?: string | undefined;
420
- category?: "fact" | "preference" | "correction" | "entity" | "decision" | "relationship" | "principle" | "commitment" | "moment" | "skill" | "rule" | "procedure" | "reasoning_trace" | undefined;
421
- confidence?: number | undefined;
422
- ttl?: string | undefined;
423
419
  schemaVersion?: number | undefined;
424
420
  sessionKey?: string | undefined;
425
421
  tags?: string[] | undefined;
426
422
  dryRun?: boolean | undefined;
423
+ namespace?: string | undefined;
424
+ category?: "fact" | "preference" | "correction" | "entity" | "decision" | "relationship" | "principle" | "commitment" | "moment" | "skill" | "rule" | "procedure" | "reasoning_trace" | undefined;
425
+ confidence?: number | undefined;
427
426
  entityRef?: string | undefined;
427
+ ttl?: string | undefined;
428
428
  sourceReason?: string | undefined;
429
429
  idempotencyKey?: string | undefined;
430
430
  }>;
@@ -456,18 +456,18 @@ declare const trustZonePromoteRequestSchema: z.ZodObject<{
456
456
  recordId: string;
457
457
  targetZone: "working" | "trusted";
458
458
  promotionReason: string;
459
- namespace?: string | undefined;
460
459
  recordedAt?: string | undefined;
461
460
  summary?: string | undefined;
462
461
  dryRun?: boolean | undefined;
462
+ namespace?: string | undefined;
463
463
  }, {
464
464
  recordId: string;
465
465
  targetZone: "working" | "trusted";
466
466
  promotionReason: string;
467
- namespace?: string | undefined;
468
467
  recordedAt?: string | undefined;
469
468
  summary?: string | undefined;
470
469
  dryRun?: boolean | undefined;
470
+ namespace?: string | undefined;
471
471
  }>;
472
472
  declare const trustZoneDemoSeedRequestSchema: z.ZodObject<{
473
473
  scenario: z.ZodOptional<z.ZodString>;
@@ -475,14 +475,14 @@ declare const trustZoneDemoSeedRequestSchema: z.ZodObject<{
475
475
  dryRun: z.ZodOptional<z.ZodBoolean>;
476
476
  namespace: z.ZodOptional<z.ZodString>;
477
477
  }, "strip", z.ZodTypeAny, {
478
- namespace?: string | undefined;
479
478
  recordedAt?: string | undefined;
480
479
  dryRun?: boolean | undefined;
480
+ namespace?: string | undefined;
481
481
  scenario?: string | undefined;
482
482
  }, {
483
- namespace?: string | undefined;
484
483
  recordedAt?: string | undefined;
485
484
  dryRun?: boolean | undefined;
485
+ namespace?: string | undefined;
486
486
  scenario?: string | undefined;
487
487
  }>;
488
488
  declare const lcmSearchRequestSchema: z.ZodObject<{
@@ -493,15 +493,15 @@ declare const lcmSearchRequestSchema: z.ZodObject<{
493
493
  limit: z.ZodOptional<z.ZodNumber>;
494
494
  }, "strip", z.ZodTypeAny, {
495
495
  query: string;
496
+ sessionKey?: string | undefined;
496
497
  namespace?: string | undefined;
497
498
  limit?: number | undefined;
498
- sessionKey?: string | undefined;
499
499
  sessionPrefix?: string | undefined;
500
500
  }, {
501
501
  query: string;
502
+ sessionKey?: string | undefined;
502
503
  namespace?: string | undefined;
503
504
  limit?: number | undefined;
504
- sessionKey?: string | undefined;
505
505
  sessionPrefix?: string | undefined;
506
506
  }>;
507
507
  declare const lcmCompactionFlushRequestSchema: z.ZodObject<{
@@ -535,12 +535,12 @@ declare const daySummaryRequestSchema: z.ZodObject<{
535
535
  sessionKey: z.ZodOptional<z.ZodString>;
536
536
  namespace: z.ZodOptional<z.ZodString>;
537
537
  }, "strip", z.ZodTypeAny, {
538
- namespace?: string | undefined;
539
538
  sessionKey?: string | undefined;
539
+ namespace?: string | undefined;
540
540
  memories?: string | undefined;
541
541
  }, {
542
- namespace?: string | undefined;
543
542
  sessionKey?: string | undefined;
543
+ namespace?: string | undefined;
544
544
  memories?: string | undefined;
545
545
  }>;
546
546
  declare const capsuleExportRequestSchema: z.ZodObject<{
@@ -730,11 +730,11 @@ declare const actionConfidenceRequestSchema: z.ZodObject<{
730
730
  safety: z.ZodEffects<z.ZodNullable<z.ZodOptional<z.ZodEnum<["safe", "requires-review", "blocked"]>>>, NonNullable<"blocked" | "safe" | "requires-review"> | undefined, "blocked" | "safe" | "requires-review" | null | undefined>;
731
731
  safetyReasons: z.ZodEffects<z.ZodNullable<z.ZodOptional<z.ZodArray<z.ZodString, "many">>>, string[] | undefined, string[] | null | undefined>;
732
732
  }, "strict", z.ZodTypeAny, {
733
- confidence?: number | undefined;
734
733
  source?: string | undefined;
735
734
  stale?: boolean | undefined;
736
735
  created?: string | undefined;
737
736
  updated?: string | undefined;
737
+ confidence?: number | undefined;
738
738
  scope?: string | undefined;
739
739
  retrievalReason?: string | undefined;
740
740
  safety?: NonNullable<"blocked" | "safe" | "requires-review"> | undefined;
@@ -744,11 +744,11 @@ declare const actionConfidenceRequestSchema: z.ZodObject<{
744
744
  safetyReasons?: string[] | undefined;
745
745
  userContextScopes?: string[] | undefined;
746
746
  }, {
747
- confidence?: number | null | undefined;
748
747
  source?: string | null | undefined;
749
748
  stale?: boolean | null | undefined;
750
749
  created?: string | null | undefined;
751
750
  updated?: string | null | undefined;
751
+ confidence?: number | null | undefined;
752
752
  scope?: string | null | undefined;
753
753
  retrievalReason?: string | null | undefined;
754
754
  safety?: "blocked" | "safe" | "requires-review" | null | undefined;
@@ -758,11 +758,11 @@ declare const actionConfidenceRequestSchema: z.ZodObject<{
758
758
  safetyReasons?: string[] | null | undefined;
759
759
  userContextScopes?: string[] | null | undefined;
760
760
  }>, "many">>>, {
761
- confidence?: number | undefined;
762
761
  source?: string | undefined;
763
762
  stale?: boolean | undefined;
764
763
  created?: string | undefined;
765
764
  updated?: string | undefined;
765
+ confidence?: number | undefined;
766
766
  scope?: string | undefined;
767
767
  retrievalReason?: string | undefined;
768
768
  safety?: NonNullable<"blocked" | "safe" | "requires-review"> | undefined;
@@ -772,11 +772,11 @@ declare const actionConfidenceRequestSchema: z.ZodObject<{
772
772
  safetyReasons?: string[] | undefined;
773
773
  userContextScopes?: string[] | undefined;
774
774
  }[] | undefined, {
775
- confidence?: number | null | undefined;
776
775
  source?: string | null | undefined;
777
776
  stale?: boolean | null | undefined;
778
777
  created?: string | null | undefined;
779
778
  updated?: string | null | undefined;
779
+ confidence?: number | null | undefined;
780
780
  scope?: string | null | undefined;
781
781
  retrievalReason?: string | null | undefined;
782
782
  safety?: "blocked" | "safe" | "requires-review" | null | undefined;
@@ -798,11 +798,11 @@ declare const actionConfidenceRequestSchema: z.ZodObject<{
798
798
  matched?: boolean | undefined;
799
799
  }[] | undefined;
800
800
  retrievedMemories?: {
801
- confidence?: number | undefined;
802
801
  source?: string | undefined;
803
802
  stale?: boolean | undefined;
804
803
  created?: string | undefined;
805
804
  updated?: string | undefined;
805
+ confidence?: number | undefined;
806
806
  scope?: string | undefined;
807
807
  retrievalReason?: string | undefined;
808
808
  safety?: NonNullable<"blocked" | "safe" | "requires-review"> | undefined;
@@ -824,11 +824,11 @@ declare const actionConfidenceRequestSchema: z.ZodObject<{
824
824
  matched?: boolean | null | undefined;
825
825
  }[] | null | undefined;
826
826
  retrievedMemories?: {
827
- confidence?: number | null | undefined;
828
827
  source?: string | null | undefined;
829
828
  stale?: boolean | null | undefined;
830
829
  created?: string | null | undefined;
831
830
  updated?: string | null | undefined;
831
+ confidence?: number | null | undefined;
832
832
  scope?: string | null | undefined;
833
833
  retrievalReason?: string | null | undefined;
834
834
  safety?: "blocked" | "safe" | "requires-review" | null | undefined;
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  namespaceCollectionName
3
- } from "./chunk-M6I5Z4SR.js";
3
+ } from "./chunk-6ZZP4EJF.js";
4
4
  import {
5
5
  ALL_CATEGORY_DIRS,
6
6
  NamespaceStorageRouter
@@ -202,4 +202,4 @@ export {
202
202
  verifyNamespaces,
203
203
  runNamespaceMigration
204
204
  };
205
- //# sourceMappingURL=chunk-JTDRJQ3K.js.map
205
+ //# sourceMappingURL=chunk-6JGNHWCI.js.map
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  createSearchBackend
3
- } from "./chunk-OB6353F7.js";
3
+ } from "./chunk-XPSVGJYA.js";
4
4
  import {
5
5
  namespaceIdentityToken,
6
6
  normalizeNamespaceIdentity
@@ -179,4 +179,4 @@ export {
179
179
  namespaceCollectionName,
180
180
  NamespaceSearchRouter
181
181
  };
182
- //# sourceMappingURL=chunk-M6I5Z4SR.js.map
182
+ //# sourceMappingURL=chunk-6ZZP4EJF.js.map
@@ -521,7 +521,7 @@ async function queryByTagsAsync(memoryDir, tags) {
521
521
  try {
522
522
  gIndex = normalizeTagIndex(JSON.parse(raw));
523
523
  } catch {
524
- gIndex = { version: TAG_INDEX_VERSION, tags: {}, aliases: {} };
524
+ return null;
525
525
  }
526
526
  return queryByTagsFromIndex(gIndex, tags);
527
527
  } catch {
@@ -538,7 +538,7 @@ function queryByTagsFromIndex(index, tags) {
538
538
  results.add(pathValue);
539
539
  }
540
540
  }
541
- return results.size > 0 ? results : null;
541
+ return results;
542
542
  }
543
543
  function extractTagsFromPrompt(prompt) {
544
544
  const found = /* @__PURE__ */ new Set();
@@ -566,6 +566,13 @@ async function resolvePromptTagPrefilterAsync(memoryDir, prompt) {
566
566
  matched.add(canonical);
567
567
  }
568
568
  }
569
+ if (matched.size === 0) {
570
+ return {
571
+ matchedTags: [],
572
+ expandedTags: [],
573
+ paths: null
574
+ };
575
+ }
569
576
  const expandedTags = expandCanonicalTags(tagIndex, Array.from(matched));
570
577
  const paths = queryByTagsFromIndex(tagIndex, expandedTags);
571
578
  return {
@@ -788,4 +795,4 @@ export {
788
795
  recencyWindowFromPrompt,
789
796
  recencyWindowBoundsFromPrompt
790
797
  };
791
- //# sourceMappingURL=chunk-75O6YQ63.js.map
798
+ //# sourceMappingURL=chunk-G6R5UD3Q.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 fs.mkdirSync(dir, { recursive: true });\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 lockIsFresh(lockInfo: fs.Stats, owner: IndexLockOwner | null): boolean {\n const ownerCreatedAtMs =\n typeof owner?.createdAt === \"string\" && owner.createdAt.length > 0 ? Date.parse(owner.createdAt) : Number.NaN;\n const referenceMs = Number.isFinite(ownerCreatedAtMs) ? ownerCreatedAtMs : lockInfo.mtimeMs;\n return Date.now() - referenceMs < INDEX_LOCK_STALE_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) {\n if (lockOwnerIsRunning(owner)) return \"wait\";\n }\n if (owner === null && lockIsFresh(info, null)) return \"wait\";\n fs.rmSync(lockDir, { recursive: true, force: true });\n return \"removed\";\n } catch (error) {\n if ((error as NodeJS.ErrnoException)?.code === \"ENOENT\") return \"removed\";\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 === \"ENOENT\") {\n try {\n fs.mkdirSync(path.dirname(lockDir), { recursive: true });\n } catch {\n return;\n }\n sleepSync(INDEX_LOCK_POLL_MS);\n continue;\n }\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 return null;\n }\n\n return queryByTagsFromIndex(gIndex, tags);\n } catch {\n return null;\n }\n}\n\nfunction queryByTagsFromIndex(index: TagIndex, tags: string[]): Set<string> {\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;\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 if (matched.size === 0) {\n return {\n matchedTags: [],\n expandedTags: [],\n paths: null,\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,eAAe,WAAyB;AAC/C,QAA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SAAS,GAAG,GAAG,CAAC,YAAY;AACpG,mBAAS,IAAI,KAAK,EAAE,QAAQ,IAAI,KAAU,EAAE,YAAY,EAAE,MAAM,GAAG,EAAE;AAAA,QACvE,OAAO;AACL,gBAAM,iBAAiB,EAAE,MAAM,uEAAuE;AACtG,cAAI,gBAAgB;AAClB,kBAAM,WAAW,CAAC,UAAU,UAAU,WAAW,aAAa,YAAY,UAAU,UAAU;AAC9F,kBAAM,YAAY,SAAS,QAAQ,eAAe,CAAC,CAAC;AACpD,kBAAM,aAAa,IAAI,OAAO;AAC9B,kBAAM,YAAY,aAAa,YAAY,KAAK,KAAK;AAErD,qBAAS,IAAI,KAAK,SAAS,WAAW,KAAK,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":[]}
@@ -96,13 +96,17 @@ var MeilisearchBackend = class {
96
96
  await this.updateCollection(this.collection, execution);
97
97
  }
98
98
  async updateCollection(collection, execution) {
99
- if (!this.autoIndex || !this.memoryDir) return;
99
+ if (!this.memoryDir) return;
100
+ await this.updateCollectionFromDir(collection, this.memoryDir, execution);
101
+ }
102
+ async updateCollectionFromDir(collection, memoryDir, execution) {
103
+ if (!this.autoIndex) return;
100
104
  if (!this.available) return;
101
105
  if (isSearchAborted(execution)) return;
102
106
  try {
103
107
  const client = await this.ensureClient();
104
108
  if (isSearchAborted(execution)) return;
105
- const docs = await scanMemoryDir(this.memoryDir);
109
+ const docs = await scanMemoryDir(memoryDir);
106
110
  if (isSearchAborted(execution)) return;
107
111
  const index = client.index(collection);
108
112
  const meilDocs = docs.map((d) => ({
@@ -204,4 +208,4 @@ var MeilisearchBackend = class {
204
208
  export {
205
209
  MeilisearchBackend
206
210
  };
207
- //# sourceMappingURL=chunk-I4UNL747.js.map
211
+ //# sourceMappingURL=chunk-GMAG2HS4.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/search/meilisearch-backend.ts"],"sourcesContent":["import { log } from \"../logger.js\";\nimport type { SearchBackend, SearchExecutionOptions, SearchQueryOptions, SearchResult } from \"./port.js\";\nimport { scanMemoryDir } from \"./document-scanner.js\";\nimport { isSearchAborted, throwIfSearchAborted } from \"./abort.js\";\n\nexport interface MeilisearchBackendOptions {\n host: string;\n apiKey?: string;\n collection: string;\n timeoutMs?: number;\n autoIndex?: boolean;\n memoryDir?: string;\n}\n\n/**\n * Meilisearch search backend — server-based SDK client.\n *\n * Requires a running Meilisearch instance. Uses the official `meilisearch` SDK.\n * When `autoIndex` is true, update() pushes docs from the local memory directory.\n */\nexport class MeilisearchBackend implements SearchBackend {\n private readonly host: string;\n private readonly apiKey?: string;\n private readonly collection: string;\n private readonly timeoutMs: number;\n private readonly autoIndex: boolean;\n private readonly memoryDir?: string;\n private available = false;\n private client: any = null;\n private meiliModule: any = null;\n\n constructor(opts: MeilisearchBackendOptions) {\n this.host = opts.host;\n this.apiKey = opts.apiKey;\n this.collection = opts.collection;\n this.timeoutMs = opts.timeoutMs ?? 30_000;\n this.autoIndex = opts.autoIndex ?? false;\n this.memoryDir = opts.memoryDir;\n }\n\n async probe(): Promise<boolean> {\n try {\n const client = await this.ensureClient();\n await client.health();\n this.available = true;\n return true;\n } catch (err) {\n log.debug(`MeilisearchBackend probe failed: ${err}`);\n this.available = false;\n return false;\n }\n }\n\n isAvailable(): boolean {\n return this.available;\n }\n\n debugStatus(): string {\n return `backend=meilisearch available=${this.available} host=${this.host}`;\n }\n\n async search(\n query: string,\n collection?: string,\n maxResults?: number,\n _options?: SearchQueryOptions,\n execution?: SearchExecutionOptions,\n ): Promise<SearchResult[]> {\n if (isSearchAborted(execution)) return [];\n // Try hybrid first; fall back to plain FTS only if hybrid throws (e.g. no embedder configured)\n try {\n return await this.doSearch(query, maxResults ?? 10, { hybrid: { semanticRatio: 0.5, embedder: \"default\" } }, collection, true, execution);\n } catch {\n if (isSearchAborted(execution)) return [];\n return this.bm25Search(query, collection, maxResults, execution);\n }\n }\n\n async searchGlobal(query: string, maxResults?: number, execution?: SearchExecutionOptions): Promise<SearchResult[]> {\n const limit = maxResults ?? 10;\n if (!this.available) return [];\n\n try {\n throwIfSearchAborted(execution, \"MeilisearchBackend global search aborted\");\n const client = await this.ensureClient();\n const indexes = await client.getIndexes();\n throwIfSearchAborted(execution, \"MeilisearchBackend global search aborted\");\n const queries = (indexes.results ?? []).map((idx: any) => ({\n indexUid: idx.uid,\n q: query,\n limit,\n showRankingScore: true,\n }));\n if (queries.length === 0) return [];\n\n const multiResult = await client.multiSearch({ queries });\n throwIfSearchAborted(execution, \"MeilisearchBackend global search aborted\");\n const allResults: SearchResult[] = [];\n for (const result of multiResult.results ?? []) {\n allResults.push(...this.mapHits(result.hits ?? []));\n }\n allResults.sort((a, b) => b.score - a.score);\n return allResults.slice(0, limit);\n } catch (err) {\n log.debug(`MeilisearchBackend searchGlobal failed: ${err}`);\n return [];\n }\n }\n\n async bm25Search(query: string, collection?: string, maxResults?: number, execution?: SearchExecutionOptions): Promise<SearchResult[]> {\n return this.doSearch(query, maxResults ?? 10, undefined, collection, false, execution);\n }\n\n async vectorSearch(query: string, collection?: string, maxResults?: number, execution?: SearchExecutionOptions): Promise<SearchResult[]> {\n return this.doSearch(query, maxResults ?? 10, { hybrid: { semanticRatio: 1.0, embedder: \"default\" } }, collection, false, execution);\n }\n\n async hybridSearch(query: string, collection?: string, maxResults?: number, execution?: SearchExecutionOptions): Promise<SearchResult[]> {\n return this.doSearch(query, maxResults ?? 10, { hybrid: { semanticRatio: 0.5, embedder: \"default\" } }, collection, false, execution);\n }\n\n async update(execution?: SearchExecutionOptions): Promise<void> {\n await this.updateCollection(this.collection, execution);\n }\n\n async updateCollection(collection: string, execution?: SearchExecutionOptions): Promise<void> {\n if (!this.memoryDir) return;\n await this.updateCollectionFromDir(collection, this.memoryDir, execution);\n }\n\n async updateCollectionFromDir(collection: string, memoryDir: string, execution?: SearchExecutionOptions): Promise<void> {\n if (!this.autoIndex) return;\n if (!this.available) return;\n if (isSearchAborted(execution)) return;\n\n try {\n const client = await this.ensureClient();\n if (isSearchAborted(execution)) return;\n const docs = await scanMemoryDir(memoryDir);\n if (isSearchAborted(execution)) return;\n const index = client.index(collection);\n\n const meilDocs = docs.map((d) => ({\n id: d.docid,\n path: d.path,\n content: d.content,\n snippet: d.snippet,\n }));\n\n // Upsert current docs and wait for the task to complete\n if (isSearchAborted(execution)) return;\n const addTask = await index.addDocuments(meilDocs, { primaryKey: \"id\" });\n await client.waitForTask(addTask.taskUid, { timeOutMs: this.timeoutMs });\n if (isSearchAborted(execution)) return;\n\n // Remove docs that no longer exist on disk (paginated to handle large indexes)\n const currentIds = new Set(docs.map((d) => d.docid));\n try {\n const PAGE_SIZE = 1000;\n let offset = 0;\n let staleIds: string[] = [];\n let hasMore = true;\n while (hasMore) {\n if (isSearchAborted(execution)) return;\n const page = await index.getDocuments({ limit: PAGE_SIZE, offset, fields: [\"id\"] });\n const results = page.results ?? [];\n for (const doc of results) {\n const id = doc.id as string;\n if (!currentIds.has(id)) staleIds.push(id);\n }\n offset += results.length;\n hasMore = results.length === PAGE_SIZE;\n }\n if (staleIds.length > 0) {\n if (isSearchAborted(execution)) return;\n const delTask = await index.deleteDocuments(staleIds);\n await client.waitForTask(delTask.taskUid, { timeOutMs: this.timeoutMs });\n }\n } catch {\n // Deletion cleanup is best-effort\n }\n } catch (err) {\n log.debug(`MeilisearchBackend update failed: ${err}`);\n }\n }\n\n async embed(): Promise<void> {\n // Meilisearch handles its own embedding when configured with an embedder\n }\n\n async embedCollection(collection: string): Promise<void> {\n // Meilisearch handles its own embedding when configured with an embedder\n // The collection parameter is accepted for interface compliance but Meilisearch\n // manages embeddings server-side per index (collection).\n }\n\n async ensureCollection(\n _memoryDir: string,\n _execution?: SearchExecutionOptions,\n ): Promise<\"present\" | \"missing\" | \"unknown\" | \"skipped\"> {\n if (!this.available) return \"skipped\";\n try {\n const client = await this.ensureClient();\n try {\n await client.getIndex(this.collection);\n return \"present\";\n } catch {\n // Index doesn't exist — create it\n await client.createIndex(this.collection, { primaryKey: \"id\" });\n return \"present\";\n }\n } catch {\n return \"skipped\";\n }\n }\n\n private async ensureClient(): Promise<any> {\n if (this.client) return this.client;\n if (!this.meiliModule) {\n this.meiliModule = await import(\"meilisearch\");\n }\n const MeiliSearch = this.meiliModule.MeiliSearch ?? this.meiliModule.default?.MeiliSearch;\n this.client = new MeiliSearch({\n host: this.host,\n apiKey: this.apiKey,\n timeout: this.timeoutMs,\n });\n return this.client;\n }\n\n private async doSearch(\n query: string,\n limit: number,\n extra?: Record<string, unknown>,\n collection?: string,\n rethrow = false,\n execution?: SearchExecutionOptions,\n ): Promise<SearchResult[]> {\n if (!this.available) return [];\n if (isSearchAborted(execution)) return [];\n try {\n const client = await this.ensureClient();\n throwIfSearchAborted(execution, \"MeilisearchBackend search aborted\");\n const index = client.index(collection ?? this.collection);\n const result = await index.search(query, { limit, showRankingScore: true, ...extra });\n throwIfSearchAborted(execution, \"MeilisearchBackend search aborted\");\n return this.mapHits(result.hits ?? []);\n } catch (err) {\n log.debug(`MeilisearchBackend search failed: ${err}`);\n if (rethrow) throw err;\n return [];\n }\n }\n\n private mapHits(hits: any[]): SearchResult[] {\n return hits.map((hit) => ({\n docid: hit.id ?? \"\",\n path: hit.path ?? \"\",\n snippet: hit._formatted?.content ?? hit.snippet ?? hit.content?.slice(0, 200) ?? \"\",\n score: hit._rankingScore ?? 0.5,\n }));\n }\n}\n"],"mappings":";;;;;;;;;;;;AAoBO,IAAM,qBAAN,MAAkD;AAAA,EACtC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACT,YAAY;AAAA,EACZ,S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C7B,QAAI,gBAAgB,SAAS,EAAG,QAAO,CAAC;AACxC,QAAI;AACF,YAAM,SAAS,MAAM,KAAK,aAAa;AACvC,2BAAqB,WAAW,mCAAmC;AACnE,YAAM,QAAQ,OAAO,MAAM,cAAc,KAAK,UAAU;AACxD,YAAM,SAAS,MAAM,MAAM,OAAO,OAAO,EAAE,OAAO,kBAAkB,MAAM,GAAG,MAAM,CAAC;AACpF,2BAAqB,WAAW,mCAAmC;AACnE,aAAO,KAAK,QAAQ,OAAO,QAAQ,CAAC,CAAC;AAAA,IACvC,SAAS,KAAK;AACZ,UAAI,MAAM,qCAAqC,GAAG,EAAE;AACpD,UAAI,QAAS,OAAM;AACnB,aAAO,CAAC;AAAA,IACV;AAAA,EACF;AAAA,EAEQ,QAAQ,MAA6B;AAC3C,WAAO,KAAK,IAAI,CAAC,SAAS;AAAA,MACxB,OAAO,IAAI,MAAM;AAAA,MACjB,MAAM,IAAI,QAAQ;AAAA,MAClB,SAAS,IAAI,YAAY,WAAW,IAAI,WAAW,IAAI,SAAS,MAAM,GAAG,GAAG,KAAK;AAAA,MACjF,OAAO,IAAI,iBAAiB;AAAA,IAC9B,EAAE;AAAA,EACJ;AACF;","names":[]}
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  isTemporalQuery,
3
3
  recencyWindowBoundsFromPrompt
4
- } from "./chunk-75O6YQ63.js";
4
+ } from "./chunk-G6R5UD3Q.js";
5
5
  import {
6
6
  isAbortError
7
7
  } from "./chunk-PVGDJXVK.js";
@@ -406,4 +406,4 @@ export {
406
406
  augmentWithDirectAndTemporal,
407
407
  parallelRetrieval
408
408
  };
409
- //# sourceMappingURL=chunk-5SQ5CQJP.js.map
409
+ //# sourceMappingURL=chunk-HSVJGWYS.js.map
@@ -89,12 +89,12 @@ import {
89
89
  runOperatorInventory,
90
90
  runOperatorRepair,
91
91
  runOperatorSetup
92
- } from "./chunk-2ESBDLT5.js";
92
+ } from "./chunk-VJXSUAO7.js";
93
93
  import {
94
94
  listNamespaces,
95
95
  runNamespaceMigration,
96
96
  verifyNamespaces
97
- } from "./chunk-JTDRJQ3K.js";
97
+ } from "./chunk-6JGNHWCI.js";
98
98
  import {
99
99
  collectPatternMemories,
100
100
  explainPatternMemory,
@@ -6616,4 +6616,4 @@ export {
6616
6616
  resolveMemoryDirForNamespace,
6617
6617
  registerCli
6618
6618
  };
6619
- //# sourceMappingURL=chunk-RUYYYLDT.js.map
6619
+ //# sourceMappingURL=chunk-IOTENEVL.js.map
@@ -117,12 +117,15 @@ var OramaBackend = class {
117
117
  await this.updateCollection(this.collection, execution);
118
118
  }
119
119
  async updateCollection(collection, execution) {
120
+ await this.updateCollectionFromDir(collection, this.memoryDir, execution);
121
+ }
122
+ async updateCollectionFromDir(collection, memoryDir, execution) {
120
123
  if (isSearchAborted(execution)) return;
121
124
  const db = await this.ensureDbForCollection(collection);
122
125
  if (isSearchAborted(execution)) return;
123
126
  if (!db) return;
124
127
  const { search: oramaSearch, insert, remove, count, getByID } = this.oramaModule;
125
- const docs = await scanMemoryDir(this.memoryDir);
128
+ const docs = await scanMemoryDir(memoryDir);
126
129
  if (isSearchAborted(execution)) return;
127
130
  const docMap = new Map(docs.map((d) => [d.docid, d]));
128
131
  const { update: oramaUpdate } = this.oramaModule;
@@ -594,4 +597,4 @@ export {
594
597
  resolveOramaCollectionDbFilePath,
595
598
  OramaBackend
596
599
  };
597
- //# sourceMappingURL=chunk-M46RYSMW.js.map
600
+ //# sourceMappingURL=chunk-JHMFYY7L.js.map