agent-working-memory 0.13.1 → 0.14.1

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 (91) hide show
  1. package/README.md +200 -238
  2. package/dist/adapters/common.d.ts +6 -0
  3. package/dist/adapters/common.d.ts.map +1 -1
  4. package/dist/adapters/common.js +457 -362
  5. package/dist/adapters/common.js.map +1 -1
  6. package/dist/api/routes.d.ts.map +1 -1
  7. package/dist/api/routes.js +24 -8
  8. package/dist/api/routes.js.map +1 -1
  9. package/dist/core/alias-map.d.ts +16 -0
  10. package/dist/core/alias-map.d.ts.map +1 -0
  11. package/dist/core/alias-map.js +102 -0
  12. package/dist/core/alias-map.js.map +1 -0
  13. package/dist/core/embeddings.d.ts +17 -0
  14. package/dist/core/embeddings.d.ts.map +1 -1
  15. package/dist/core/embeddings.js +50 -1
  16. package/dist/core/embeddings.js.map +1 -1
  17. package/dist/core/recall-config.d.ts +52 -0
  18. package/dist/core/recall-config.d.ts.map +1 -0
  19. package/dist/core/recall-config.js +110 -0
  20. package/dist/core/recall-config.js.map +1 -0
  21. package/dist/core/rerank-window.d.ts +61 -0
  22. package/dist/core/rerank-window.d.ts.map +1 -0
  23. package/dist/core/rerank-window.js +153 -0
  24. package/dist/core/rerank-window.js.map +1 -0
  25. package/dist/core/rerank2.d.ts +62 -0
  26. package/dist/core/rerank2.d.ts.map +1 -0
  27. package/dist/core/rerank2.js +75 -0
  28. package/dist/core/rerank2.js.map +1 -0
  29. package/dist/core/retrieval-text.d.ts +55 -0
  30. package/dist/core/retrieval-text.d.ts.map +1 -0
  31. package/dist/core/retrieval-text.js +87 -0
  32. package/dist/core/retrieval-text.js.map +1 -0
  33. package/dist/core/temporal-query.d.ts +61 -0
  34. package/dist/core/temporal-query.d.ts.map +1 -0
  35. package/dist/core/temporal-query.js +168 -0
  36. package/dist/core/temporal-query.js.map +1 -0
  37. package/dist/core/token-budget.d.ts +75 -0
  38. package/dist/core/token-budget.d.ts.map +1 -0
  39. package/dist/core/token-budget.js +136 -0
  40. package/dist/core/token-budget.js.map +1 -0
  41. package/dist/core/whoami.d.ts +11 -0
  42. package/dist/core/whoami.d.ts.map +1 -1
  43. package/dist/core/whoami.js +10 -0
  44. package/dist/core/whoami.js.map +1 -1
  45. package/dist/core/write-pipeline.d.ts.map +1 -1
  46. package/dist/core/write-pipeline.js +6 -3
  47. package/dist/core/write-pipeline.js.map +1 -1
  48. package/dist/engine/activation.d.ts.map +1 -1
  49. package/dist/engine/activation.js +146 -32
  50. package/dist/engine/activation.js.map +1 -1
  51. package/dist/hooks/prime.d.ts +77 -0
  52. package/dist/hooks/prime.d.ts.map +1 -0
  53. package/dist/hooks/prime.js +92 -0
  54. package/dist/hooks/prime.js.map +1 -0
  55. package/dist/hooks/sidecar.d.ts.map +1 -1
  56. package/dist/hooks/sidecar.js +39 -0
  57. package/dist/hooks/sidecar.js.map +1 -1
  58. package/dist/mcp.js +160 -107
  59. package/dist/mcp.js.map +1 -1
  60. package/dist/storage/pglite.d.ts.map +1 -1
  61. package/dist/storage/pglite.js +10 -2
  62. package/dist/storage/pglite.js.map +1 -1
  63. package/dist/storage/postgres.d.ts.map +1 -1
  64. package/dist/storage/postgres.js +10 -2
  65. package/dist/storage/postgres.js.map +1 -1
  66. package/dist/storage/sqlite.d.ts.map +1 -1
  67. package/dist/storage/sqlite.js +12 -2
  68. package/dist/storage/sqlite.js.map +1 -1
  69. package/dist/types/engram.d.ts +31 -0
  70. package/dist/types/engram.d.ts.map +1 -1
  71. package/package.json +3 -2
  72. package/src/adapters/common.ts +666 -567
  73. package/src/api/routes.ts +1015 -999
  74. package/src/core/alias-map.ts +97 -0
  75. package/src/core/embeddings.ts +172 -115
  76. package/src/core/recall-config.ts +115 -0
  77. package/src/core/rerank-window.ts +158 -0
  78. package/src/core/rerank2.ts +82 -0
  79. package/src/core/retrieval-text.ts +82 -0
  80. package/src/core/temporal-query.ts +193 -0
  81. package/src/core/token-budget.ts +160 -0
  82. package/src/core/whoami.ts +110 -92
  83. package/src/core/write-pipeline.ts +6 -3
  84. package/src/engine/activation.ts +1579 -1468
  85. package/src/hooks/prime.ts +136 -0
  86. package/src/hooks/sidecar.ts +43 -0
  87. package/src/mcp.ts +1443 -1387
  88. package/src/storage/pglite.ts +10 -2
  89. package/src/storage/postgres.ts +10 -2
  90. package/src/storage/sqlite.ts +12 -2
  91. package/src/types/engram.ts +32 -0
@@ -512,7 +512,11 @@ export class PGliteEngramStore {
512
512
  sql += ` AND id != $${params.length + 1}`;
513
513
  params.push(excludeId);
514
514
  }
515
- sql += ` ORDER BY created_at DESC LIMIT 1`;
515
+ // FIX 2026-08-23: created_at is millisecond-resolution, so two engrams written in
516
+ // the same millisecond TIE and "most recent" became whichever row the engine
517
+ // happened to return. Postgres has no stable insert-order column (ctid moves on
518
+ // update/vacuum), so id is the tiebreak: deterministic, though not chronological.
519
+ sql += ` ORDER BY created_at DESC, id DESC LIMIT 1`;
516
520
  const result = await this.db.query<any>(sql, params);
517
521
  return result.rows.length > 0 ? rowToEngram(result.rows[0]) : null;
518
522
  }
@@ -734,7 +738,11 @@ export class PGliteEngramStore {
734
738
  params.push(tagLike(tag));
735
739
  }
736
740
  }
737
- sql += ` ORDER BY created_at DESC LIMIT 1`;
741
+ // FIX 2026-08-23: created_at is millisecond-resolution, so two engrams written in
742
+ // the same millisecond TIE and "most recent" became whichever row the engine
743
+ // happened to return. Postgres has no stable insert-order column (ctid moves on
744
+ // update/vacuum), so id is the tiebreak: deterministic, though not chronological.
745
+ sql += ` ORDER BY created_at DESC, id DESC LIMIT 1`;
738
746
  const result = await this.db.query<any>(sql, params);
739
747
  return result.rows.length > 0 ? rowToEngram(result.rows[0]) : null;
740
748
  }
@@ -611,7 +611,11 @@ export class PostgresEngramStore {
611
611
  sql += ` AND id != $${params.length + 1}`;
612
612
  params.push(excludeId);
613
613
  }
614
- sql += ` ORDER BY created_at DESC LIMIT 1`;
614
+ // FIX 2026-08-23: created_at is millisecond-resolution, so two engrams written in
615
+ // the same millisecond TIE and "most recent" became whichever row the engine
616
+ // happened to return. Postgres has no stable insert-order column (ctid moves on
617
+ // update/vacuum), so id is the tiebreak: deterministic, though not chronological.
618
+ sql += ` ORDER BY created_at DESC, id DESC LIMIT 1`;
615
619
  const result = await this.q<any>(sql, params);
616
620
  return result.rows.length > 0 ? rowToEngram(result.rows[0]) : null;
617
621
  }
@@ -833,7 +837,11 @@ export class PostgresEngramStore {
833
837
  params.push(tagLike(tag));
834
838
  }
835
839
  }
836
- sql += ` ORDER BY created_at DESC LIMIT 1`;
840
+ // FIX 2026-08-23: created_at is millisecond-resolution, so two engrams written in
841
+ // the same millisecond TIE and "most recent" became whichever row the engine
842
+ // happened to return. Postgres has no stable insert-order column (ctid moves on
843
+ // update/vacuum), so id is the tiebreak: deterministic, though not chronological.
844
+ sql += ` ORDER BY created_at DESC, id DESC LIMIT 1`;
837
845
  const result = await this.q<any>(sql, params);
838
846
  return result.rows.length > 0 ? rowToEngram(result.rows[0]) : null;
839
847
  }
@@ -956,7 +956,12 @@ export class EngramStore {
956
956
  sql += ' AND id != ?';
957
957
  params.push(excludeId);
958
958
  }
959
- sql += ' ORDER BY created_at DESC LIMIT 1';
959
+ // FIX 2026-08-23: created_at is millisecond-resolution ISO text, so two engrams
960
+ // written in the same millisecond TIE and "most recent" became whichever row the
961
+ // engine happened to return. Surfaced on Linux (same-ms writes) while passing on
962
+ // Windows, whose coarser timer separated them. rowid is monotonic with insert
963
+ // order, so it is both deterministic AND chronologically correct as a tiebreak.
964
+ sql += ' ORDER BY created_at DESC, rowid DESC LIMIT 1';
960
965
  const row = this.db.prepare(sql).get(...params) as any;
961
966
  return row ? this.rowToEngram(row) : null;
962
967
  }
@@ -1058,7 +1063,12 @@ export class EngramStore {
1058
1063
  params.push(`%"${tag}"%`);
1059
1064
  }
1060
1065
  }
1061
- sql += ' ORDER BY created_at DESC LIMIT 1';
1066
+ // FIX 2026-08-23: created_at is millisecond-resolution ISO text, so two engrams
1067
+ // written in the same millisecond TIE and "most recent" became whichever row the
1068
+ // engine happened to return. Surfaced on Linux (same-ms writes) while passing on
1069
+ // Windows, whose coarser timer separated them. rowid is monotonic with insert
1070
+ // order, so it is both deterministic AND chronologically correct as a tiebreak.
1071
+ sql += ' ORDER BY created_at DESC, rowid DESC LIMIT 1';
1062
1072
  const row = this.db.prepare(sql).get(...params) as any;
1063
1073
  return row ? this.rowToEngram(row) : null;
1064
1074
  }
@@ -254,6 +254,22 @@ export interface PhaseScores {
254
254
  */
255
255
  export type QueryMode = 'targeted' | 'exploratory' | 'balanced' | 'auto';
256
256
 
257
+ /** Why a recall returned nothing, when the cause was a gate rather than an empty store. */
258
+ export interface AbstentionInfo {
259
+ /** `confidence` = score-distribution gate (`requireConfidence`).
260
+ * `agreement` = cross-channel agreement gate (`abstentionThreshold`). */
261
+ reason: 'confidence' | 'agreement';
262
+ /** Candidates that survived scoring and were then withheld. Never 0 — a genuinely
263
+ * empty result does not produce an AbstentionInfo at all. */
264
+ candidates: number;
265
+ /** Score of the best withheld candidate. Compare against `minScore`, not against
266
+ * the confidence threshold — they measure different things. */
267
+ topScore: number;
268
+ /** Computed recall confidence, and the threshold it failed. */
269
+ confidence?: number;
270
+ threshold?: number;
271
+ }
272
+
257
273
  export interface ActivationQuery {
258
274
  agentId: string;
259
275
  context: string;
@@ -273,11 +289,27 @@ export interface ActivationQuery {
273
289
  * 0.25 (balanced), 0.40 (aggressive — only return high-confidence recall).
274
290
  */
275
291
  requireConfidence?: number;
292
+ /**
293
+ * Called when a gate withholds results, INSTEAD of silently returning [].
294
+ *
295
+ * An empty array cannot distinguish "nothing matched" from "matches were found
296
+ * and withheld", and callers reliably read the second as the first — including
297
+ * when the withheld results scored well above `minScore`. If you act on an empty
298
+ * recall, wire this up.
299
+ */
300
+ onAbstain?: (info: AbstentionInfo) => void;
276
301
  internal?: boolean; // Skip access count increment, Hebbian update, and event logging (for system calls)
277
302
  spread?: boolean; // R2: when AWM_SPREAD=1, set false to skip iterative spreading activation (connection-discovery uses this so edge-building doesn't recurse)
278
303
  memoryType?: MemoryType; // Filter by memory type (episodic, semantic, procedural)
279
304
  mode?: QueryMode; // Pipeline mode — 'auto' by default
280
305
  workspace?: string; // Search across all agents in this workspace (hive mode). If unset, agent-scoped only.
306
+ /**
307
+ * Anchor for relative temporal expressions ("last Friday", "last week").
308
+ * Defaults to now. Passed explicitly by evals so a fixture stays
309
+ * reproducible — with the wall clock, "last week" would silently mean
310
+ * something different on every run.
311
+ */
312
+ asOf?: number;
281
313
  bm25Only?: boolean; // Skip embedding — fast text-only retrieval for bulk/benchmark scenarios
282
314
  /**
283
315
  * Output granularity (Paper 3: cognitive teaming, Brill 2018 ACT-R collaboration).