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
@@ -0,0 +1,82 @@
1
+ // Copyright 2026 Robert Winter / Complete Ideas
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ /**
4
+ * Second-stage reordering — "rerank the rerank".
5
+ *
6
+ * THE PROBLEM
7
+ * -----------
8
+ * Phase 7 blends the cross-encoder's judgement with the composite score:
9
+ *
10
+ * score = compositeWeight * composite + rerankWeight * rerankerScore
11
+ *
12
+ * with `rerankWeight` capped at 0.7, so `composite` always keeps at least 30%
13
+ * of the vote on final ordering. Composite carries decay, Hebbian and salience
14
+ * terms — useful for deciding WHICH candidates deserve consideration, but a
15
+ * poor judge of which one actually answers the question.
16
+ *
17
+ * Measured on LoCoMo (616 answerable probes, tests/rerank2-eval): the blend
18
+ * disagrees with the cross-encoder about which item deserves rank 1 on **38.6%**
19
+ * of queries. Where that disagreement is decidable — one of the two is the gold
20
+ * evidence — the **cross-encoder is right 77% of the time** (61 vs 18).
21
+ *
22
+ * THE FIX
23
+ * -------
24
+ * Re-sort only the final returned window by `rerankerScore` alone. Simulated
25
+ * gain: **+8.0pp success@1** (39.1% -> 47.1%), fixing 61 queries and breaking
26
+ * 12, with every category improving.
27
+ *
28
+ * WHY THIS IS SAFE
29
+ * ----------------
30
+ * Applied AFTER the channel-agreement gate, AFTER computeRecallConfidence, and
31
+ * AFTER the requireConfidence check. Those read rerankerScore maxima/margins and
32
+ * the score distribution. Reordering a window afterwards changes neither its
33
+ * membership nor any score, so **adversarial abstention is provably
34
+ * unaffected**. Retuning `rerankWeight` inside phase 7 would NOT have that
35
+ * property — it shifts `item.score`, hence which items clear `minScore`, which
36
+ * is exactly what cost adversarial 73.4->71.0 when the pool was last widened.
37
+ *
38
+ * Margin-guarded variants were simulated and are strictly worse: gating on a
39
+ * >0.15 reranker margin cuts breakage 12->1 but also cuts fixes 61->17.
40
+ */
41
+
42
+ /** Minimal shape this needs — anything carrying a cross-encoder score. */
43
+ export interface RerankScored {
44
+ phaseScores: { rerankerScore: number };
45
+ }
46
+
47
+ /**
48
+ * Re-sort the first `k` entries of `ranked` by descending `rerankerScore`,
49
+ * leaving the tail untouched. Returns a new array; never mutates the input.
50
+ *
51
+ * Returns `ranked` unchanged when reordering would be unsound:
52
+ * - `k <= 1`, or fewer than 2 entries to order
53
+ * - any entry in the window has `rerankerScore <= 0`, which means the
54
+ * cross-encoder did not score it (skipped, failed, or timed out). Sorting
55
+ * on a zero would scramble an otherwise sound composite ordering — the
56
+ * reranker is wrapped in try/catch and a 10s timeout upstream, so this is a
57
+ * real path, not a theoretical one.
58
+ */
59
+ export function reorderByReranker<T extends RerankScored>(ranked: T[], k: number): T[] {
60
+ if (k <= 1 || ranked.length <= 1) return ranked;
61
+
62
+ const window = Math.min(k, ranked.length);
63
+ if (window <= 1) return ranked;
64
+
65
+ const head = ranked.slice(0, window);
66
+ for (const r of head) {
67
+ if (!(r.phaseScores.rerankerScore > 0)) return ranked; // also catches NaN
68
+ }
69
+
70
+ const sorted = head.slice().sort((a, b) => b.phaseScores.rerankerScore - a.phaseScores.rerankerScore);
71
+ return sorted.concat(ranked.slice(window));
72
+ }
73
+
74
+ /** Window size for the second stage. */
75
+ export function rerank2WindowSize(): number {
76
+ return Number(process.env.AWM_RERANK2_K ?? 10);
77
+ }
78
+
79
+ /** Whether the second stage is enabled. Default OFF pending the tracer verdict. */
80
+ export function rerank2Enabled(): boolean {
81
+ return process.env.AWM_RERANK2 === '1';
82
+ }
@@ -0,0 +1,82 @@
1
+ // Copyright 2026 Robert Winter / Complete Ideas
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ /**
4
+ * Derived retrieval text — the machine-facing view of a memory.
5
+ *
6
+ * WHY THIS EXISTS
7
+ * ---------------
8
+ * Measured on the live 11,294-engram store: 66.2% of topical tag terms never
9
+ * appear in the memory body, and 94.3% of tagged memories are missing at least
10
+ * one of their own topical terms. Tags are indexed by BM25 only — the embedding
11
+ * is built from `concept + content` (write-pipeline.ts) and so is the
12
+ * cross-encoder rerank passage (activation.ts). So that vocabulary is invisible
13
+ * to two of the three retrieval channels, including the one that decides final
14
+ * ordering since phase 9b.
15
+ *
16
+ * The consequence, observed on a real memory: "private plan memory peaked 88%,
17
+ * scale P1v3 -> P2v3" was NOT in the top 40 candidates for "azure app service
18
+ * plan capacity increase internal application". Its body contains no "azure",
19
+ * no "capacity", no "app service plan" — only `topic=azure` as a tag.
20
+ *
21
+ * THE SHAPE THAT MATTERS
22
+ * ----------------------
23
+ * This builds a DERIVED text used for embedding and reranking. It does NOT
24
+ * mutate `content`. That distinction is the whole design:
25
+ *
26
+ * - AWM's model slots have always been ADDITIVE — the embedder, expander and
27
+ * reranker score or expand, they never rewrite what was stored. Appending
28
+ * tag terms into the body would break that invariant, and a normaliser that
29
+ * silently edits stored memories is unrecoverable if it is wrong.
30
+ * - A derived view is recomputable. If the rule turns out to be bad, re-derive
31
+ * and re-embed; the source of truth was never touched.
32
+ * - And it can be BACKFILLED over the existing corpus, which body-only fixes
33
+ * (write-time guidance, future writes) cannot reach. 7,350 memories are
34
+ * already wrong.
35
+ *
36
+ * Only `topic=` / `proj=` / `project=` are included. Date, person and ticket
37
+ * tags are identifiers the body usually already carries, so adding them spends
38
+ * budget without adding reachable words.
39
+ */
40
+
41
+ /** Whether the derived retrieval text includes tag vocabulary. Default OFF. */
42
+ export function retrievalTextEnabled(): boolean {
43
+ return process.env.AWM_RETRIEVAL_TEXT === '1';
44
+ }
45
+
46
+ /**
47
+ * Extract the topical vocabulary a future question is likely to use.
48
+ * Deduplicated and lowercased; order follows first appearance so the output is
49
+ * deterministic for a given tag list (important: a non-deterministic embedding
50
+ * input would make re-embedding produce different vectors for the same memory).
51
+ */
52
+ export function topicalTerms(tags?: string[]): string[] {
53
+ if (!tags || tags.length === 0) return [];
54
+ const out: string[] = [];
55
+ const seen = new Set<string>();
56
+ for (const t of tags) {
57
+ const m = /^(?:topic|proj|project)=(.+)$/i.exec(t);
58
+ if (!m) continue;
59
+ for (const w of m[1].toLowerCase().split(/[-_\s]+/)) {
60
+ if (w.length > 2 && !seen.has(w)) { seen.add(w); out.push(w); }
61
+ }
62
+ }
63
+ return out;
64
+ }
65
+
66
+ /**
67
+ * Build the text used for EMBEDDING a memory.
68
+ *
69
+ * When disabled this returns exactly `concept + ' ' + content`, byte-identical
70
+ * to the historical input — so leaving the flag off cannot change a single
71
+ * stored vector.
72
+ */
73
+ export function buildRetrievalText(concept: string, content: string, tags?: string[]): string {
74
+ const base = `${concept} ${content}`;
75
+ if (!retrievalTextEnabled()) return base;
76
+ const terms = topicalTerms(tags);
77
+ if (terms.length === 0) return base;
78
+ // Appended at the END. The embedding model truncates beyond its context
79
+ // window, and the head of the content is what anchors the topic — putting tag
80
+ // terms first would displace the memory's actual subject in the vector.
81
+ return `${base} ${terms.join(' ')}`;
82
+ }
@@ -0,0 +1,193 @@
1
+ // Copyright 2026 Robert Winter / Complete Ideas
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ /**
4
+ * Temporal expressions in a recall query.
5
+ *
6
+ * THE PROBLEM
7
+ * -----------
8
+ * Nothing in the pipeline parsed dates out of query text, and `ActivationQuery`
9
+ * had no date parameter. So "azure app plan from last Thursday or Friday" spent
10
+ * "last", "Thursday" and "Friday" as ordinary BM25 tokens — diluting the subject
11
+ * terms and matching `date=` tags right across the corpus.
12
+ *
13
+ * Measured on the real store (101 probes, tests/realstore-eval):
14
+ * subject only ............ 59.4% success@1 <- control
15
+ * + "from last Friday" .... 56.4% (-3.0pp)
16
+ * + "on 2026-05-01" ....... 51.5% (-7.9pp)
17
+ * ORACLE, week-filtered ... 96.0% (+36.6pp)
18
+ *
19
+ * So the most selective thing the user said was a PENALTY, and a working
20
+ * temporal filter is worth more than anything else measured on this store.
21
+ *
22
+ * DESIGN NOTES
23
+ * ------------
24
+ * - `asOf` is required for relative phrases. Using the wall clock would make a
25
+ * fixture non-reproducible: "last week" would silently mean something new on
26
+ * every run. Callers pass the query time explicitly.
27
+ * - Matching is deliberately CONSERVATIVE. A greedy matcher that fired on
28
+ * ordinary words would reshape every recall in the store, so each pattern is
29
+ * anchored on an unambiguous cue ("last", "yesterday", an ISO date, a month
30
+ * name). When nothing matches, this returns `null` and callers must treat
31
+ * that as a strict no-op.
32
+ * - The window is a PREFERENCE for callers, never a filter. People misremember
33
+ * dates — "last Thursday or Friday" is itself an admission of uncertainty —
34
+ * so a memory outside the window must stay reachable on subject strength.
35
+ */
36
+
37
+ export interface TemporalMatch {
38
+ /** Inclusive window start (UTC ms). */
39
+ from: number;
40
+ /** Exclusive window end (UTC ms). */
41
+ to: number;
42
+ /** The literal phrases matched, for logging and for stripping. */
43
+ matched: string[];
44
+ /** The query with temporal phrases removed, for the lexical channel. */
45
+ stripped: string;
46
+ /** How the window was derived — useful when explaining a recall. */
47
+ kind: 'relative-day' | 'relative-week' | 'relative-month' | 'recent' | 'absolute-date' | 'absolute-month';
48
+ }
49
+
50
+ const DAY = 86400000;
51
+ const WEEKDAYS = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday'];
52
+ const MONTHS = ['january', 'february', 'march', 'april', 'may', 'june',
53
+ 'july', 'august', 'september', 'october', 'november', 'december'];
54
+
55
+ const utcDay = (ms: number) => Math.floor(ms / DAY) * DAY;
56
+
57
+ /**
58
+ * Parse a temporal expression out of `query`, anchored at `asOf`.
59
+ * Returns `null` when nothing matches — callers MUST treat that as a no-op.
60
+ *
61
+ * Windows are padded generously rather than tightly: the goal is to prefer a
62
+ * neighbourhood, not to pin an exact day the user probably misremembers.
63
+ */
64
+ export function parseTemporal(query: string, asOf: Date | number): TemporalMatch | null {
65
+ const anchorMs = typeof asOf === 'number' ? asOf : asOf.getTime();
66
+ if (!Number.isFinite(anchorMs)) return null;
67
+ const q = query.toLowerCase();
68
+ const today = utcDay(anchorMs);
69
+ const matched: string[] = [];
70
+
71
+ const build = (from: number, to: number, kind: TemporalMatch['kind']): TemporalMatch => ({
72
+ from, to, matched,
73
+ stripped: stripPhrases(query, matched),
74
+ kind,
75
+ });
76
+
77
+ // ── absolute ISO date — ONLY with an explicit temporal cue ──
78
+ // A BARE date must not trigger this. Memories routinely carry a date as part
79
+ // of their SUBJECT ("Hive close-out 2026-04-24 PASS", "FIXED 2026-08-21 event
80
+ // River Glen"), where the date IS the fact, not a "when" filter. Treating
81
+ // those as temporal strips a strongly discriminative term and makes recall
82
+ // worse: measured, a bare-date rule fired on 98 of 1,316 (7.4%) real
83
+ // identifier queries and cost success@1 75.8% -> 75.0%. Same reason the month
84
+ // rule below requires an "in"/"during" cue.
85
+ const iso = q.match(/\b(on|from|since|before|after|around|during|dated)\s+(\d{4})-(\d{2})-(\d{2})\b/);
86
+ if (iso) {
87
+ matched.push(iso[0]);
88
+ const d = Date.UTC(+iso[2], +iso[3] - 1, +iso[4]);
89
+ // +/- 3 days: a remembered date is often a day or two off.
90
+ return build(d - 3 * DAY, d + 4 * DAY, 'absolute-date');
91
+ }
92
+
93
+ // ── "yesterday" / "today" ──
94
+ if (/\byesterday\b/.test(q)) {
95
+ matched.push('yesterday');
96
+ return build(today - DAY, today, 'relative-day');
97
+ }
98
+ if (/\btoday\b/.test(q)) {
99
+ matched.push('today');
100
+ return build(today, today + DAY, 'relative-day');
101
+ }
102
+
103
+ // ── "last <weekday>" / "on <weekday>" ──
104
+ const wd = q.match(/\b(?:last|this|on)\s+(sunday|monday|tuesday|wednesday|thursday|friday|saturday)\b/);
105
+ if (wd) {
106
+ matched.push(wd[0]);
107
+ // A second weekday ("Thursday or Friday") widens rather than confuses.
108
+ const second = q.match(/\bor\s+(sunday|monday|tuesday|wednesday|thursday|friday|saturday)\b/);
109
+ if (second) matched.push(second[0]);
110
+ const targets = [WEEKDAYS.indexOf(wd[1])];
111
+ if (second) targets.push(WEEKDAYS.indexOf(second[1]));
112
+ const anchorDow = new Date(today).getUTCDay();
113
+ const days = targets.map(t => {
114
+ let back = (anchorDow - t + 7) % 7;
115
+ if (back === 0) back = 7; // "last Friday" on a Friday means the previous one
116
+ return today - back * DAY;
117
+ });
118
+ // +/- 1 day of slack around the named day(s).
119
+ return build(Math.min(...days) - DAY, Math.max(...days) + 2 * DAY, 'relative-day');
120
+ }
121
+
122
+ // ── "last week" / "this week" / "past week" ──
123
+ if (/\b(?:last|this|past)\s+week\b/.test(q)) {
124
+ matched.push((q.match(/\b(?:last|this|past)\s+week\b/) as RegExpMatchArray)[0]);
125
+ return build(today - 14 * DAY, today + DAY, 'relative-week');
126
+ }
127
+
128
+ // ── "last month" / "this month" / "past month" ──
129
+ if (/\b(?:last|this|past)\s+month\b/.test(q)) {
130
+ matched.push((q.match(/\b(?:last|this|past)\s+month\b/) as RegExpMatchArray)[0]);
131
+ return build(today - 45 * DAY, today + DAY, 'relative-month');
132
+ }
133
+
134
+ // ── "last N days/weeks/months" ──
135
+ const lastN = q.match(/\b(?:last|past)\s+(\d{1,3})\s+(day|week|month)s?\b/);
136
+ if (lastN) {
137
+ matched.push(lastN[0]);
138
+ const n = +lastN[1];
139
+ const mult = lastN[2] === 'day' ? DAY : lastN[2] === 'week' ? 7 * DAY : 30 * DAY;
140
+ return build(today - n * mult, today + DAY, 'relative-week');
141
+ }
142
+
143
+ // ── "recently" / "recent" / "the other day" / "a few days ago" ──
144
+ if (/\b(?:recently|the other day|a few days ago|just now)\b/.test(q)) {
145
+ matched.push((q.match(/\b(?:recently|the other day|a few days ago|just now)\b/) as RegExpMatchArray)[0]);
146
+ return build(today - 14 * DAY, today + DAY, 'recent');
147
+ }
148
+
149
+ // ── "in <Month> [Year]" — requires the "in"/"during" cue so a project named
150
+ // "May" or a sentence containing "march" cannot trigger it accidentally.
151
+ const mon = q.match(/\b(?:in|during|back in)\s+(january|february|march|april|may|june|july|august|september|october|november|december)\b(?:\s+(\d{4}))?/);
152
+ if (mon) {
153
+ matched.push(mon[0]);
154
+ const mIdx = MONTHS.indexOf(mon[1]);
155
+ const anchorYear = new Date(today).getUTCFullYear();
156
+ const year = mon[2] ? +mon[2] : anchorYear;
157
+ const from = Date.UTC(year, mIdx, 1);
158
+ const to = Date.UTC(mIdx === 11 ? year + 1 : year, (mIdx + 1) % 12, 1);
159
+ return build(from, to, 'absolute-month');
160
+ }
161
+
162
+ return null;
163
+ }
164
+
165
+ /** Remove matched temporal phrases (and now-dangling connectives) from a query. */
166
+ function stripPhrases(query: string, phrases: string[]): string {
167
+ let out = query;
168
+ // Longest first, so "on 2026-05-01" is removed before the bare date.
169
+ for (const p of [...phrases].sort((a, b) => b.length - a.length)) {
170
+ out = out.replace(new RegExp(escapeRe(p), 'gi'), ' ');
171
+ }
172
+ return out
173
+ .replace(/\b(?:from|on|in|during|back in|since)\s*$/i, ' ')
174
+ .replace(/\s+(?:from|on|in|during)\s+(?=\s|$)/gi, ' ')
175
+ .replace(/\s{2,}/g, ' ')
176
+ .trim();
177
+ }
178
+
179
+ const escapeRe = (s: string) => s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
180
+
181
+ /** Whether temporal handling is enabled. Default OFF. */
182
+ export function temporalEnabled(): boolean {
183
+ return process.env.AWM_TEMPORAL === '1';
184
+ }
185
+
186
+ /**
187
+ * Boost applied to a candidate whose creation time falls inside the window.
188
+ * A PREFERENCE, not a filter — see the design note above.
189
+ */
190
+ export function temporalBoost(): number {
191
+ const v = Number(process.env.AWM_TEMPORAL_BOOST ?? 0.35);
192
+ return Number.isFinite(v) && v >= 0 ? v : 0.35;
193
+ }
@@ -0,0 +1,160 @@
1
+ // Copyright 2026 Robert Winter / Complete Ideas
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ /**
4
+ * Token budgeting for memory_recall (0.13.3).
5
+ *
6
+ * WHY THIS EXISTS
7
+ * ---------------
8
+ * `limit: N` is a COUNT, which is token-blind: a 5-result recall can cost 400
9
+ * tokens or 4,000 depending on how long the memories happen to be. AWM's own
10
+ * benchmark shows the problem — in real sessions AWM retrieval averaged 4,514
11
+ * tokens per call against 2,106 for plain file retrieval. AWM wins 9.8:1 in
12
+ * AGGREGATE (it needs far fewer calls) but loses 2.1:1 PER CALL. Per-call cost
13
+ * is the exposed flank, and a caller had no way to bound it.
14
+ *
15
+ * `max_tokens` lets the caller say "I have 800 tokens of context to spare"
16
+ * instead of guessing at a result count.
17
+ *
18
+ * PACKING STRATEGY
19
+ * ----------------
20
+ * This is a 0/1 knapsack (maximise score within a token budget), so we use the
21
+ * standard greedy density heuristic — order by score-per-token — with one
22
+ * deliberate exception: the top-scored result is always admitted first if it
23
+ * fits at all. Density packing alone can drop the single most relevant memory
24
+ * purely for being long, which is exactly the answer the caller wanted.
25
+ *
26
+ * Selection is by density; OUTPUT is restored to score order, because a reader
27
+ * scanning results expects the best one first.
28
+ *
29
+ * ESTIMATOR
30
+ * ---------
31
+ * Deliberately dependency-free: no tokenizer, no model load, no async. It
32
+ * takes max(words x 1.3, chars / 4) — the two standard English/code
33
+ * approximations — and takes the larger so the estimate errs toward
34
+ * OVER-counting. Over-counting keeps us inside the budget; under-counting
35
+ * would silently blow it, which is the failure that matters.
36
+ */
37
+
38
+ import type { ActivationResult } from '../types/engram.js';
39
+
40
+ /** Rough token count. Over-estimates by design — see the note above. */
41
+ export function estimateTokens(text: string): number {
42
+ if (!text) return 0;
43
+ const chars = text.length;
44
+ const words = text.split(/\s+/).filter(Boolean).length;
45
+ return Math.max(Math.ceil(words * 1.3), Math.ceil(chars / 4));
46
+ }
47
+
48
+ export interface PackedRecall {
49
+ /** Formatted lines that fit the budget, in score order. */
50
+ lines: string[];
51
+ /** How many results were kept. */
52
+ kept: number;
53
+ /** How many results were available before budgeting. */
54
+ total: number;
55
+ /** Estimated tokens of the kept lines. */
56
+ tokens: number;
57
+ /** Estimated tokens of everything dropped (0 when nothing was dropped). */
58
+ withheldTokens: number;
59
+ }
60
+
61
+ /**
62
+ * Select and format results to fit `maxTokens`.
63
+ *
64
+ * When `maxTokens` is undefined the behaviour is unchanged from pre-0.13.3 —
65
+ * everything is returned — but the accounting fields are still populated, so
66
+ * callers get per-call token visibility without opting into budgeting.
67
+ */
68
+ export function packRecallByBudget(
69
+ results: ActivationResult[],
70
+ format: (r: ActivationResult, index: number) => string,
71
+ maxTokens?: number,
72
+ /**
73
+ * Tokens the caller will spend on the SAME reply outside the result lines —
74
+ * the accounting footer, and any peer-decisions suffix. Reserved up front so
75
+ * the budget bounds the whole reply rather than just the part this function
76
+ * happens to build.
77
+ *
78
+ * Found by the end-to-end eval, not by the unit tests: budgets of 600/250/80
79
+ * came back as 601/256/95 because the footer spent tokens it never counted.
80
+ */
81
+ reservedTokens = 0,
82
+ ): PackedRecall {
83
+ const total = results.length;
84
+ if (total === 0) return { lines: [], kept: 0, total: 0, tokens: 0, withheldTokens: 0 };
85
+ const effectiveBudget = maxTokens === undefined ? undefined
86
+ : Math.max(0, maxTokens - reservedTokens);
87
+
88
+ // Format once against the ORIGINAL index so displayed numbering matches the
89
+ // caller's mental model of "result 1 is the best match".
90
+ const measured = results.map((r, i) => {
91
+ const line = format(r, i);
92
+ return { line, tokens: estimateTokens(line), score: r.score, order: i };
93
+ });
94
+
95
+ const allTokens = measured.reduce((n, m) => n + m.tokens, 0);
96
+
97
+ // NOTE the asymmetry, which a unit test caught: `maxTokens` being absent or
98
+ // nonsense (<= 0) means "no budget — return everything", but an
99
+ // *effectiveBudget* of 0 means the reserve ate the entire budget, which must
100
+ // admit NOTHING. Collapsing those two cases returned the full result set on
101
+ // the tightest budgets — the exact opposite of what was asked for.
102
+ const unbudgeted = maxTokens === undefined || maxTokens <= 0;
103
+ if (unbudgeted || allTokens <= effectiveBudget!) {
104
+ return {
105
+ lines: measured.map(m => m.line),
106
+ kept: total,
107
+ total,
108
+ tokens: allTokens,
109
+ withheldTokens: 0,
110
+ };
111
+ }
112
+
113
+ const chosen: typeof measured = [];
114
+ let spent = 0;
115
+
116
+ // 1. Top-scored result gets first refusal, so a long best-match is never
117
+ // dropped in favour of several short weak ones.
118
+ const top = measured[0];
119
+ if (top.tokens <= effectiveBudget!) {
120
+ chosen.push(top);
121
+ spent += top.tokens;
122
+ }
123
+
124
+ // 2. Everything else by value density (score per token), greedily.
125
+ const rest = measured
126
+ .filter(m => m !== top)
127
+ .sort((a, b) => (b.score / Math.max(b.tokens, 1)) - (a.score / Math.max(a.tokens, 1)));
128
+
129
+ for (const m of rest) {
130
+ if (spent + m.tokens > effectiveBudget!) continue; // skip, don't stop: a later
131
+ chosen.push(m); // shorter result may still fit
132
+ spent += m.tokens;
133
+ }
134
+
135
+ // 3. Restore score order for display.
136
+ chosen.sort((a, b) => a.order - b.order);
137
+
138
+ return {
139
+ lines: chosen.map(m => m.line),
140
+ kept: chosen.length,
141
+ total,
142
+ tokens: spent,
143
+ withheldTokens: allTokens - spent,
144
+ };
145
+ }
146
+
147
+ /**
148
+ * One-line accounting footer. Costs ~15-25 tokens to report, which is a good
149
+ * trade against a recall that can run to thousands — and it is what makes the
150
+ * per-call cost visible to both the human and the harness. Without this,
151
+ * AWM's token behaviour is only observable in an offline benchmark.
152
+ */
153
+ export function formatTokenFooter(p: PackedRecall, maxTokens?: number): string {
154
+ if (p.total === 0) return '';
155
+ const parts = [`~${p.tokens} tok`];
156
+ parts.push(p.kept === p.total ? `${p.total} results` : `${p.kept}/${p.total} results`);
157
+ if (maxTokens !== undefined && maxTokens > 0) parts.push(`budget ${maxTokens}`);
158
+ if (p.withheldTokens > 0) parts.push(`~${p.withheldTokens} tok withheld`);
159
+ return `\n\n[awm: ${parts.join(' · ')}]`;
160
+ }