agent-working-memory 0.13.0 → 0.14.0

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 (104) 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 +52 -1
  16. package/dist/core/embeddings.js.map +1 -1
  17. package/dist/core/model-cache.d.ts +28 -0
  18. package/dist/core/model-cache.d.ts.map +1 -0
  19. package/dist/core/model-cache.js +50 -0
  20. package/dist/core/model-cache.js.map +1 -0
  21. package/dist/core/query-expander.d.ts.map +1 -1
  22. package/dist/core/query-expander.js +2 -0
  23. package/dist/core/query-expander.js.map +1 -1
  24. package/dist/core/recall-config.d.ts +52 -0
  25. package/dist/core/recall-config.d.ts.map +1 -0
  26. package/dist/core/recall-config.js +110 -0
  27. package/dist/core/recall-config.js.map +1 -0
  28. package/dist/core/rerank-window.d.ts +61 -0
  29. package/dist/core/rerank-window.d.ts.map +1 -0
  30. package/dist/core/rerank-window.js +153 -0
  31. package/dist/core/rerank-window.js.map +1 -0
  32. package/dist/core/rerank2.d.ts +62 -0
  33. package/dist/core/rerank2.d.ts.map +1 -0
  34. package/dist/core/rerank2.js +75 -0
  35. package/dist/core/rerank2.js.map +1 -0
  36. package/dist/core/reranker.d.ts.map +1 -1
  37. package/dist/core/reranker.js +2 -0
  38. package/dist/core/reranker.js.map +1 -1
  39. package/dist/core/retrieval-text.d.ts +55 -0
  40. package/dist/core/retrieval-text.d.ts.map +1 -0
  41. package/dist/core/retrieval-text.js +87 -0
  42. package/dist/core/retrieval-text.js.map +1 -0
  43. package/dist/core/temporal-query.d.ts +61 -0
  44. package/dist/core/temporal-query.d.ts.map +1 -0
  45. package/dist/core/temporal-query.js +168 -0
  46. package/dist/core/temporal-query.js.map +1 -0
  47. package/dist/core/token-budget.d.ts +75 -0
  48. package/dist/core/token-budget.d.ts.map +1 -0
  49. package/dist/core/token-budget.js +136 -0
  50. package/dist/core/token-budget.js.map +1 -0
  51. package/dist/core/whoami.d.ts +11 -0
  52. package/dist/core/whoami.d.ts.map +1 -1
  53. package/dist/core/whoami.js +10 -0
  54. package/dist/core/whoami.js.map +1 -1
  55. package/dist/core/write-pipeline.d.ts.map +1 -1
  56. package/dist/core/write-pipeline.js +6 -3
  57. package/dist/core/write-pipeline.js.map +1 -1
  58. package/dist/engine/activation.d.ts.map +1 -1
  59. package/dist/engine/activation.js +135 -32
  60. package/dist/engine/activation.js.map +1 -1
  61. package/dist/hooks/prime.d.ts +77 -0
  62. package/dist/hooks/prime.d.ts.map +1 -0
  63. package/dist/hooks/prime.js +92 -0
  64. package/dist/hooks/prime.js.map +1 -0
  65. package/dist/hooks/sidecar.d.ts.map +1 -1
  66. package/dist/hooks/sidecar.js +39 -0
  67. package/dist/hooks/sidecar.js.map +1 -1
  68. package/dist/mcp.js +134 -102
  69. package/dist/mcp.js.map +1 -1
  70. package/dist/storage/pglite.d.ts.map +1 -1
  71. package/dist/storage/pglite.js +10 -2
  72. package/dist/storage/pglite.js.map +1 -1
  73. package/dist/storage/postgres.d.ts.map +1 -1
  74. package/dist/storage/postgres.js +10 -2
  75. package/dist/storage/postgres.js.map +1 -1
  76. package/dist/storage/sqlite.d.ts.map +1 -1
  77. package/dist/storage/sqlite.js +12 -2
  78. package/dist/storage/sqlite.js.map +1 -1
  79. package/dist/types/engram.d.ts +7 -0
  80. package/dist/types/engram.d.ts.map +1 -1
  81. package/package.json +3 -2
  82. package/src/adapters/common.ts +666 -567
  83. package/src/api/routes.ts +1015 -999
  84. package/src/core/alias-map.ts +97 -0
  85. package/src/core/embeddings.ts +172 -113
  86. package/src/core/model-cache.ts +51 -0
  87. package/src/core/query-expander.ts +2 -0
  88. package/src/core/recall-config.ts +115 -0
  89. package/src/core/rerank-window.ts +158 -0
  90. package/src/core/rerank2.ts +82 -0
  91. package/src/core/reranker.ts +2 -0
  92. package/src/core/retrieval-text.ts +82 -0
  93. package/src/core/temporal-query.ts +193 -0
  94. package/src/core/token-budget.ts +160 -0
  95. package/src/core/whoami.ts +110 -92
  96. package/src/core/write-pipeline.ts +6 -3
  97. package/src/engine/activation.ts +1568 -1468
  98. package/src/hooks/prime.ts +136 -0
  99. package/src/hooks/sidecar.ts +43 -0
  100. package/src/mcp.ts +1422 -1387
  101. package/src/storage/pglite.ts +10 -2
  102. package/src/storage/postgres.ts +10 -2
  103. package/src/storage/sqlite.ts +12 -2
  104. package/src/types/engram.ts +7 -0
@@ -0,0 +1,55 @@
1
+ /**
2
+ * Derived retrieval text — the machine-facing view of a memory.
3
+ *
4
+ * WHY THIS EXISTS
5
+ * ---------------
6
+ * Measured on the live 11,294-engram store: 66.2% of topical tag terms never
7
+ * appear in the memory body, and 94.3% of tagged memories are missing at least
8
+ * one of their own topical terms. Tags are indexed by BM25 only — the embedding
9
+ * is built from `concept + content` (write-pipeline.ts) and so is the
10
+ * cross-encoder rerank passage (activation.ts). So that vocabulary is invisible
11
+ * to two of the three retrieval channels, including the one that decides final
12
+ * ordering since phase 9b.
13
+ *
14
+ * The consequence, observed on a real memory: "private plan memory peaked 88%,
15
+ * scale P1v3 -> P2v3" was NOT in the top 40 candidates for "azure app service
16
+ * plan capacity increase internal application". Its body contains no "azure",
17
+ * no "capacity", no "app service plan" — only `topic=azure` as a tag.
18
+ *
19
+ * THE SHAPE THAT MATTERS
20
+ * ----------------------
21
+ * This builds a DERIVED text used for embedding and reranking. It does NOT
22
+ * mutate `content`. That distinction is the whole design:
23
+ *
24
+ * - AWM's model slots have always been ADDITIVE — the embedder, expander and
25
+ * reranker score or expand, they never rewrite what was stored. Appending
26
+ * tag terms into the body would break that invariant, and a normaliser that
27
+ * silently edits stored memories is unrecoverable if it is wrong.
28
+ * - A derived view is recomputable. If the rule turns out to be bad, re-derive
29
+ * and re-embed; the source of truth was never touched.
30
+ * - And it can be BACKFILLED over the existing corpus, which body-only fixes
31
+ * (write-time guidance, future writes) cannot reach. 7,350 memories are
32
+ * already wrong.
33
+ *
34
+ * Only `topic=` / `proj=` / `project=` are included. Date, person and ticket
35
+ * tags are identifiers the body usually already carries, so adding them spends
36
+ * budget without adding reachable words.
37
+ */
38
+ /** Whether the derived retrieval text includes tag vocabulary. Default OFF. */
39
+ export declare function retrievalTextEnabled(): boolean;
40
+ /**
41
+ * Extract the topical vocabulary a future question is likely to use.
42
+ * Deduplicated and lowercased; order follows first appearance so the output is
43
+ * deterministic for a given tag list (important: a non-deterministic embedding
44
+ * input would make re-embedding produce different vectors for the same memory).
45
+ */
46
+ export declare function topicalTerms(tags?: string[]): string[];
47
+ /**
48
+ * Build the text used for EMBEDDING a memory.
49
+ *
50
+ * When disabled this returns exactly `concept + ' ' + content`, byte-identical
51
+ * to the historical input — so leaving the flag off cannot change a single
52
+ * stored vector.
53
+ */
54
+ export declare function buildRetrievalText(concept: string, content: string, tags?: string[]): string;
55
+ //# sourceMappingURL=retrieval-text.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"retrieval-text.d.ts","sourceRoot":"","sources":["../../src/core/retrieval-text.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AAEH,+EAA+E;AAC/E,wBAAgB,oBAAoB,IAAI,OAAO,CAE9C;AAED;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,GAAG,MAAM,EAAE,CAYtD;AAED;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM,EAAE,GAAG,MAAM,CAS5F"}
@@ -0,0 +1,87 @@
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
+ /** Whether the derived retrieval text includes tag vocabulary. Default OFF. */
41
+ export function retrievalTextEnabled() {
42
+ return process.env.AWM_RETRIEVAL_TEXT === '1';
43
+ }
44
+ /**
45
+ * Extract the topical vocabulary a future question is likely to use.
46
+ * Deduplicated and lowercased; order follows first appearance so the output is
47
+ * deterministic for a given tag list (important: a non-deterministic embedding
48
+ * input would make re-embedding produce different vectors for the same memory).
49
+ */
50
+ export function topicalTerms(tags) {
51
+ if (!tags || tags.length === 0)
52
+ return [];
53
+ const out = [];
54
+ const seen = new Set();
55
+ for (const t of tags) {
56
+ const m = /^(?:topic|proj|project)=(.+)$/i.exec(t);
57
+ if (!m)
58
+ continue;
59
+ for (const w of m[1].toLowerCase().split(/[-_\s]+/)) {
60
+ if (w.length > 2 && !seen.has(w)) {
61
+ seen.add(w);
62
+ out.push(w);
63
+ }
64
+ }
65
+ }
66
+ return out;
67
+ }
68
+ /**
69
+ * Build the text used for EMBEDDING a memory.
70
+ *
71
+ * When disabled this returns exactly `concept + ' ' + content`, byte-identical
72
+ * to the historical input — so leaving the flag off cannot change a single
73
+ * stored vector.
74
+ */
75
+ export function buildRetrievalText(concept, content, tags) {
76
+ const base = `${concept} ${content}`;
77
+ if (!retrievalTextEnabled())
78
+ return base;
79
+ const terms = topicalTerms(tags);
80
+ if (terms.length === 0)
81
+ return base;
82
+ // Appended at the END. The embedding model truncates beyond its context
83
+ // window, and the head of the content is what anchors the topic — putting tag
84
+ // terms first would displace the memory's actual subject in the vector.
85
+ return `${base} ${terms.join(' ')}`;
86
+ }
87
+ //# sourceMappingURL=retrieval-text.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"retrieval-text.js","sourceRoot":"","sources":["../../src/core/retrieval-text.ts"],"names":[],"mappings":"AAAA,gDAAgD;AAChD,sCAAsC;AACtC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AAEH,+EAA+E;AAC/E,MAAM,UAAU,oBAAoB;IAClC,OAAO,OAAO,CAAC,GAAG,CAAC,kBAA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ACtC,CAAC"}
@@ -0,0 +1,61 @@
1
+ /**
2
+ * Temporal expressions in a recall query.
3
+ *
4
+ * THE PROBLEM
5
+ * -----------
6
+ * Nothing in the pipeline parsed dates out of query text, and `ActivationQuery`
7
+ * had no date parameter. So "azure app plan from last Thursday or Friday" spent
8
+ * "last", "Thursday" and "Friday" as ordinary BM25 tokens — diluting the subject
9
+ * terms and matching `date=` tags right across the corpus.
10
+ *
11
+ * Measured on the real store (101 probes, tests/realstore-eval):
12
+ * subject only ............ 59.4% success@1 <- control
13
+ * + "from last Friday" .... 56.4% (-3.0pp)
14
+ * + "on 2026-05-01" ....... 51.5% (-7.9pp)
15
+ * ORACLE, week-filtered ... 96.0% (+36.6pp)
16
+ *
17
+ * So the most selective thing the user said was a PENALTY, and a working
18
+ * temporal filter is worth more than anything else measured on this store.
19
+ *
20
+ * DESIGN NOTES
21
+ * ------------
22
+ * - `asOf` is required for relative phrases. Using the wall clock would make a
23
+ * fixture non-reproducible: "last week" would silently mean something new on
24
+ * every run. Callers pass the query time explicitly.
25
+ * - Matching is deliberately CONSERVATIVE. A greedy matcher that fired on
26
+ * ordinary words would reshape every recall in the store, so each pattern is
27
+ * anchored on an unambiguous cue ("last", "yesterday", an ISO date, a month
28
+ * name). When nothing matches, this returns `null` and callers must treat
29
+ * that as a strict no-op.
30
+ * - The window is a PREFERENCE for callers, never a filter. People misremember
31
+ * dates — "last Thursday or Friday" is itself an admission of uncertainty —
32
+ * so a memory outside the window must stay reachable on subject strength.
33
+ */
34
+ export interface TemporalMatch {
35
+ /** Inclusive window start (UTC ms). */
36
+ from: number;
37
+ /** Exclusive window end (UTC ms). */
38
+ to: number;
39
+ /** The literal phrases matched, for logging and for stripping. */
40
+ matched: string[];
41
+ /** The query with temporal phrases removed, for the lexical channel. */
42
+ stripped: string;
43
+ /** How the window was derived — useful when explaining a recall. */
44
+ kind: 'relative-day' | 'relative-week' | 'relative-month' | 'recent' | 'absolute-date' | 'absolute-month';
45
+ }
46
+ /**
47
+ * Parse a temporal expression out of `query`, anchored at `asOf`.
48
+ * Returns `null` when nothing matches — callers MUST treat that as a no-op.
49
+ *
50
+ * Windows are padded generously rather than tightly: the goal is to prefer a
51
+ * neighbourhood, not to pin an exact day the user probably misremembers.
52
+ */
53
+ export declare function parseTemporal(query: string, asOf: Date | number): TemporalMatch | null;
54
+ /** Whether temporal handling is enabled. Default OFF. */
55
+ export declare function temporalEnabled(): boolean;
56
+ /**
57
+ * Boost applied to a candidate whose creation time falls inside the window.
58
+ * A PREFERENCE, not a filter — see the design note above.
59
+ */
60
+ export declare function temporalBoost(): number;
61
+ //# sourceMappingURL=temporal-query.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"temporal-query.d.ts","sourceRoot":"","sources":["../../src/core/temporal-query.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AAEH,MAAM,WAAW,aAAa;IAC5B,uCAAuC;IACvC,IAAI,EAAE,MAAM,CAAC;IACb,qCAAqC;IACrC,EAAE,EAAE,MAAM,CAAC;IACX,kEAAkE;IAClE,OAAO,EAAE,MAAM,EAAE,CAAC;IAClB,wEAAwE;IACxE,QAAQ,EAAE,MAAM,CAAC;IACjB,oEAAoE;IACpE,IAAI,EAAE,cAAc,GAAG,eAAe,GAAG,gBAAgB,GAAG,QAAQ,GAAG,eAAe,GAAG,gBAAgB,CAAC;CAC3G;AASD;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,IAAI,GAAG,MAAM,GAAG,aAAa,GAAG,IAAI,CAmGtF;AAkBD,yDAAyD;AACzD,wBAAgB,eAAe,IAAI,OAAO,CAEzC;AAED;;;GAGG;AACH,wBAAgB,aAAa,IAAI,MAAM,CAGtC"}
@@ -0,0 +1,168 @@
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
+ const DAY = 86400000;
37
+ const WEEKDAYS = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday'];
38
+ const MONTHS = ['january', 'february', 'march', 'april', 'may', 'june',
39
+ 'july', 'august', 'september', 'october', 'november', 'december'];
40
+ const utcDay = (ms) => Math.floor(ms / DAY) * DAY;
41
+ /**
42
+ * Parse a temporal expression out of `query`, anchored at `asOf`.
43
+ * Returns `null` when nothing matches — callers MUST treat that as a no-op.
44
+ *
45
+ * Windows are padded generously rather than tightly: the goal is to prefer a
46
+ * neighbourhood, not to pin an exact day the user probably misremembers.
47
+ */
48
+ export function parseTemporal(query, asOf) {
49
+ const anchorMs = typeof asOf === 'number' ? asOf : asOf.getTime();
50
+ if (!Number.isFinite(anchorMs))
51
+ return null;
52
+ const q = query.toLowerCase();
53
+ const today = utcDay(anchorMs);
54
+ const matched = [];
55
+ const build = (from, to, kind) => ({
56
+ from, to, matched,
57
+ stripped: stripPhrases(query, matched),
58
+ kind,
59
+ });
60
+ // ── absolute ISO date — ONLY with an explicit temporal cue ──
61
+ // A BARE date must not trigger this. Memories routinely carry a date as part
62
+ // of their SUBJECT ("Hive close-out 2026-04-24 PASS", "FIXED 2026-08-21 event
63
+ // River Glen"), where the date IS the fact, not a "when" filter. Treating
64
+ // those as temporal strips a strongly discriminative term and makes recall
65
+ // worse: measured, a bare-date rule fired on 98 of 1,316 (7.4%) real
66
+ // identifier queries and cost success@1 75.8% -> 75.0%. Same reason the month
67
+ // rule below requires an "in"/"during" cue.
68
+ const iso = q.match(/\b(on|from|since|before|after|around|during|dated)\s+(\d{4})-(\d{2})-(\d{2})\b/);
69
+ if (iso) {
70
+ matched.push(iso[0]);
71
+ const d = Date.UTC(+iso[2], +iso[3] - 1, +iso[4]);
72
+ // +/- 3 days: a remembered date is often a day or two off.
73
+ return build(d - 3 * DAY, d + 4 * DAY, 'absolute-date');
74
+ }
75
+ // ── "yesterday" / "today" ──
76
+ if (/\byesterday\b/.test(q)) {
77
+ matched.push('yesterday');
78
+ return build(today - DAY, today, 'relative-day');
79
+ }
80
+ if (/\btoday\b/.test(q)) {
81
+ matched.push('today');
82
+ return build(today, today + DAY, 'relative-day');
83
+ }
84
+ // ── "last <weekday>" / "on <weekday>" ──
85
+ const wd = q.match(/\b(?:last|this|on)\s+(sunday|monday|tuesday|wednesday|thursday|friday|saturday)\b/);
86
+ if (wd) {
87
+ matched.push(wd[0]);
88
+ // A second weekday ("Thursday or Friday") widens rather than confuses.
89
+ const second = q.match(/\bor\s+(sunday|monday|tuesday|wednesday|thursday|friday|saturday)\b/);
90
+ if (second)
91
+ matched.push(second[0]);
92
+ const targets = [WEEKDAYS.indexOf(wd[1])];
93
+ if (second)
94
+ targets.push(WEEKDAYS.indexOf(second[1]));
95
+ const anchorDow = new Date(today).getUTCDay();
96
+ const days = targets.map(t => {
97
+ let back = (anchorDow - t + 7) % 7;
98
+ if (back === 0)
99
+ back = 7; // "last Friday" on a Friday means the previous one
100
+ return today - back * DAY;
101
+ });
102
+ // +/- 1 day of slack around the named day(s).
103
+ return build(Math.min(...days) - DAY, Math.max(...days) + 2 * DAY, 'relative-day');
104
+ }
105
+ // ── "last week" / "this week" / "past week" ──
106
+ if (/\b(?:last|this|past)\s+week\b/.test(q)) {
107
+ matched.push(q.match(/\b(?:last|this|past)\s+week\b/)[0]);
108
+ return build(today - 14 * DAY, today + DAY, 'relative-week');
109
+ }
110
+ // ── "last month" / "this month" / "past month" ──
111
+ if (/\b(?:last|this|past)\s+month\b/.test(q)) {
112
+ matched.push(q.match(/\b(?:last|this|past)\s+month\b/)[0]);
113
+ return build(today - 45 * DAY, today + DAY, 'relative-month');
114
+ }
115
+ // ── "last N days/weeks/months" ──
116
+ const lastN = q.match(/\b(?:last|past)\s+(\d{1,3})\s+(day|week|month)s?\b/);
117
+ if (lastN) {
118
+ matched.push(lastN[0]);
119
+ const n = +lastN[1];
120
+ const mult = lastN[2] === 'day' ? DAY : lastN[2] === 'week' ? 7 * DAY : 30 * DAY;
121
+ return build(today - n * mult, today + DAY, 'relative-week');
122
+ }
123
+ // ── "recently" / "recent" / "the other day" / "a few days ago" ──
124
+ if (/\b(?:recently|the other day|a few days ago|just now)\b/.test(q)) {
125
+ matched.push(q.match(/\b(?:recently|the other day|a few days ago|just now)\b/)[0]);
126
+ return build(today - 14 * DAY, today + DAY, 'recent');
127
+ }
128
+ // ── "in <Month> [Year]" — requires the "in"/"during" cue so a project named
129
+ // "May" or a sentence containing "march" cannot trigger it accidentally.
130
+ 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}))?/);
131
+ if (mon) {
132
+ matched.push(mon[0]);
133
+ const mIdx = MONTHS.indexOf(mon[1]);
134
+ const anchorYear = new Date(today).getUTCFullYear();
135
+ const year = mon[2] ? +mon[2] : anchorYear;
136
+ const from = Date.UTC(year, mIdx, 1);
137
+ const to = Date.UTC(mIdx === 11 ? year + 1 : year, (mIdx + 1) % 12, 1);
138
+ return build(from, to, 'absolute-month');
139
+ }
140
+ return null;
141
+ }
142
+ /** Remove matched temporal phrases (and now-dangling connectives) from a query. */
143
+ function stripPhrases(query, phrases) {
144
+ let out = query;
145
+ // Longest first, so "on 2026-05-01" is removed before the bare date.
146
+ for (const p of [...phrases].sort((a, b) => b.length - a.length)) {
147
+ out = out.replace(new RegExp(escapeRe(p), 'gi'), ' ');
148
+ }
149
+ return out
150
+ .replace(/\b(?:from|on|in|during|back in|since)\s*$/i, ' ')
151
+ .replace(/\s+(?:from|on|in|during)\s+(?=\s|$)/gi, ' ')
152
+ .replace(/\s{2,}/g, ' ')
153
+ .trim();
154
+ }
155
+ const escapeRe = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
156
+ /** Whether temporal handling is enabled. Default OFF. */
157
+ export function temporalEnabled() {
158
+ return process.env.AWM_TEMPORAL === '1';
159
+ }
160
+ /**
161
+ * Boost applied to a candidate whose creation time falls inside the window.
162
+ * A PREFERENCE, not a filter — see the design note above.
163
+ */
164
+ export function temporalBoost() {
165
+ const v = Number(process.env.AWM_TEMPORAL_BOOST ?? 0.35);
166
+ return Number.isFinite(v) && v >= 0 ? v : 0.35;
167
+ }
168
+ //# sourceMappingURL=temporal-query.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"temporal-query.js","sourceRoot":"","sources":["../../src/core/temporal-query.ts"],"names":[],"mappings":"AAAA,gDAAgD;AAChD,sCAAsC;AACtC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AAeH,MAAM,GAAG,GAAG,QAAQ,CAAC;AACr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ED,MAAM,QAAQ,GAAG,CAAC,CAAS,EAAE,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,qBAAqB,EAAE,MAAM,CAAC,CAAC;AAEzE,yDAAyD;AACzD,MAAM,UAAU,eAAe;IAC7B,OAAO,OAAO,CAAC,GAAG,CAAC,YAAY,KAAK,GAAG,CAAC;AAC1C,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,aAAa;IAC3B,MAAM,CAAC,GAAG,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,kBAAkB,IAAI,IAAI,CAAC,CAAC;IACzD,OAAO,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;AACjD,CAAC"}
@@ -0,0 +1,75 @@
1
+ /**
2
+ * Token budgeting for memory_recall (0.13.3).
3
+ *
4
+ * WHY THIS EXISTS
5
+ * ---------------
6
+ * `limit: N` is a COUNT, which is token-blind: a 5-result recall can cost 400
7
+ * tokens or 4,000 depending on how long the memories happen to be. AWM's own
8
+ * benchmark shows the problem — in real sessions AWM retrieval averaged 4,514
9
+ * tokens per call against 2,106 for plain file retrieval. AWM wins 9.8:1 in
10
+ * AGGREGATE (it needs far fewer calls) but loses 2.1:1 PER CALL. Per-call cost
11
+ * is the exposed flank, and a caller had no way to bound it.
12
+ *
13
+ * `max_tokens` lets the caller say "I have 800 tokens of context to spare"
14
+ * instead of guessing at a result count.
15
+ *
16
+ * PACKING STRATEGY
17
+ * ----------------
18
+ * This is a 0/1 knapsack (maximise score within a token budget), so we use the
19
+ * standard greedy density heuristic — order by score-per-token — with one
20
+ * deliberate exception: the top-scored result is always admitted first if it
21
+ * fits at all. Density packing alone can drop the single most relevant memory
22
+ * purely for being long, which is exactly the answer the caller wanted.
23
+ *
24
+ * Selection is by density; OUTPUT is restored to score order, because a reader
25
+ * scanning results expects the best one first.
26
+ *
27
+ * ESTIMATOR
28
+ * ---------
29
+ * Deliberately dependency-free: no tokenizer, no model load, no async. It
30
+ * takes max(words x 1.3, chars / 4) — the two standard English/code
31
+ * approximations — and takes the larger so the estimate errs toward
32
+ * OVER-counting. Over-counting keeps us inside the budget; under-counting
33
+ * would silently blow it, which is the failure that matters.
34
+ */
35
+ import type { ActivationResult } from '../types/engram.js';
36
+ /** Rough token count. Over-estimates by design — see the note above. */
37
+ export declare function estimateTokens(text: string): number;
38
+ export interface PackedRecall {
39
+ /** Formatted lines that fit the budget, in score order. */
40
+ lines: string[];
41
+ /** How many results were kept. */
42
+ kept: number;
43
+ /** How many results were available before budgeting. */
44
+ total: number;
45
+ /** Estimated tokens of the kept lines. */
46
+ tokens: number;
47
+ /** Estimated tokens of everything dropped (0 when nothing was dropped). */
48
+ withheldTokens: number;
49
+ }
50
+ /**
51
+ * Select and format results to fit `maxTokens`.
52
+ *
53
+ * When `maxTokens` is undefined the behaviour is unchanged from pre-0.13.3 —
54
+ * everything is returned — but the accounting fields are still populated, so
55
+ * callers get per-call token visibility without opting into budgeting.
56
+ */
57
+ export declare function packRecallByBudget(results: ActivationResult[], format: (r: ActivationResult, index: number) => string, maxTokens?: number,
58
+ /**
59
+ * Tokens the caller will spend on the SAME reply outside the result lines —
60
+ * the accounting footer, and any peer-decisions suffix. Reserved up front so
61
+ * the budget bounds the whole reply rather than just the part this function
62
+ * happens to build.
63
+ *
64
+ * Found by the end-to-end eval, not by the unit tests: budgets of 600/250/80
65
+ * came back as 601/256/95 because the footer spent tokens it never counted.
66
+ */
67
+ reservedTokens?: number): PackedRecall;
68
+ /**
69
+ * One-line accounting footer. Costs ~15-25 tokens to report, which is a good
70
+ * trade against a recall that can run to thousands — and it is what makes the
71
+ * per-call cost visible to both the human and the harness. Without this,
72
+ * AWM's token behaviour is only observable in an offline benchmark.
73
+ */
74
+ export declare function formatTokenFooter(p: PackedRecall, maxTokens?: number): string;
75
+ //# sourceMappingURL=token-budget.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"token-budget.d.ts","sourceRoot":"","sources":["../../src/core/token-budget.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AAEH,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAE3D,wEAAwE;AACxE,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAKnD;AAED,MAAM,WAAW,YAAY;IAC3B,2DAA2D;IAC3D,KAAK,EAAE,MAAM,EAAE,CAAC;IAChB,kCAAkC;IAClC,IAAI,EAAE,MAAM,CAAC;IACb,wDAAwD;IACxD,KAAK,EAAE,MAAM,CAAC;IACd,0CAA0C;IAC1C,MAAM,EAAE,MAAM,CAAC;IACf,2EAA2E;IAC3E,cAAc,EAAE,MAAM,CAAC;CACxB;AAED;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAChC,OAAO,EAAE,gBAAgB,EAAE,EAC3B,MAAM,EAAE,CAAC,CAAC,EAAE,gBAAgB,EAAE,KAAK,EAAE,MAAM,KAAK,MAAM,EACtD,SAAS,CAAC,EAAE,MAAM;AAClB;;;;;;;;GAQG;AACH,cAAc,SAAI,GACjB,YAAY,CA+Dd;AAED;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAAC,CAAC,EAAE,YAAY,EAAE,SAAS,CAAC,EAAE,MAAM,GAAG,MAAM,CAO7E"}
@@ -0,0 +1,136 @@
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
+ /** Rough token count. Over-estimates by design — see the note above. */
38
+ export function estimateTokens(text) {
39
+ if (!text)
40
+ return 0;
41
+ const chars = text.length;
42
+ const words = text.split(/\s+/).filter(Boolean).length;
43
+ return Math.max(Math.ceil(words * 1.3), Math.ceil(chars / 4));
44
+ }
45
+ /**
46
+ * Select and format results to fit `maxTokens`.
47
+ *
48
+ * When `maxTokens` is undefined the behaviour is unchanged from pre-0.13.3 —
49
+ * everything is returned — but the accounting fields are still populated, so
50
+ * callers get per-call token visibility without opting into budgeting.
51
+ */
52
+ export function packRecallByBudget(results, format, maxTokens,
53
+ /**
54
+ * Tokens the caller will spend on the SAME reply outside the result lines —
55
+ * the accounting footer, and any peer-decisions suffix. Reserved up front so
56
+ * the budget bounds the whole reply rather than just the part this function
57
+ * happens to build.
58
+ *
59
+ * Found by the end-to-end eval, not by the unit tests: budgets of 600/250/80
60
+ * came back as 601/256/95 because the footer spent tokens it never counted.
61
+ */
62
+ reservedTokens = 0) {
63
+ const total = results.length;
64
+ if (total === 0)
65
+ return { lines: [], kept: 0, total: 0, tokens: 0, withheldTokens: 0 };
66
+ const effectiveBudget = maxTokens === undefined ? undefined
67
+ : Math.max(0, maxTokens - reservedTokens);
68
+ // Format once against the ORIGINAL index so displayed numbering matches the
69
+ // caller's mental model of "result 1 is the best match".
70
+ const measured = results.map((r, i) => {
71
+ const line = format(r, i);
72
+ return { line, tokens: estimateTokens(line), score: r.score, order: i };
73
+ });
74
+ const allTokens = measured.reduce((n, m) => n + m.tokens, 0);
75
+ // NOTE the asymmetry, which a unit test caught: `maxTokens` being absent or
76
+ // nonsense (<= 0) means "no budget — return everything", but an
77
+ // *effectiveBudget* of 0 means the reserve ate the entire budget, which must
78
+ // admit NOTHING. Collapsing those two cases returned the full result set on
79
+ // the tightest budgets — the exact opposite of what was asked for.
80
+ const unbudgeted = maxTokens === undefined || maxTokens <= 0;
81
+ if (unbudgeted || allTokens <= effectiveBudget) {
82
+ return {
83
+ lines: measured.map(m => m.line),
84
+ kept: total,
85
+ total,
86
+ tokens: allTokens,
87
+ withheldTokens: 0,
88
+ };
89
+ }
90
+ const chosen = [];
91
+ let spent = 0;
92
+ // 1. Top-scored result gets first refusal, so a long best-match is never
93
+ // dropped in favour of several short weak ones.
94
+ const top = measured[0];
95
+ if (top.tokens <= effectiveBudget) {
96
+ chosen.push(top);
97
+ spent += top.tokens;
98
+ }
99
+ // 2. Everything else by value density (score per token), greedily.
100
+ const rest = measured
101
+ .filter(m => m !== top)
102
+ .sort((a, b) => (b.score / Math.max(b.tokens, 1)) - (a.score / Math.max(a.tokens, 1)));
103
+ for (const m of rest) {
104
+ if (spent + m.tokens > effectiveBudget)
105
+ continue; // skip, don't stop: a later
106
+ chosen.push(m); // shorter result may still fit
107
+ spent += m.tokens;
108
+ }
109
+ // 3. Restore score order for display.
110
+ chosen.sort((a, b) => a.order - b.order);
111
+ return {
112
+ lines: chosen.map(m => m.line),
113
+ kept: chosen.length,
114
+ total,
115
+ tokens: spent,
116
+ withheldTokens: allTokens - spent,
117
+ };
118
+ }
119
+ /**
120
+ * One-line accounting footer. Costs ~15-25 tokens to report, which is a good
121
+ * trade against a recall that can run to thousands — and it is what makes the
122
+ * per-call cost visible to both the human and the harness. Without this,
123
+ * AWM's token behaviour is only observable in an offline benchmark.
124
+ */
125
+ export function formatTokenFooter(p, maxTokens) {
126
+ if (p.total === 0)
127
+ return '';
128
+ const parts = [`~${p.tokens} tok`];
129
+ parts.push(p.kept === p.total ? `${p.total} results` : `${p.kept}/${p.total} results`);
130
+ if (maxTokens !== undefined && maxTokens > 0)
131
+ parts.push(`budget ${maxTokens}`);
132
+ if (p.withheldTokens > 0)
133
+ parts.push(`~${p.withheldTokens} tok withheld`);
134
+ return `\n\n[awm: ${parts.join(' · ')}]`;
135
+ }
136
+ //# sourceMappingURL=token-budget.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"token-budget.js","sourceRoot":"","sources":["../../src/core/token-budget.ts"],"names":[],"mappings":"AAAA,gDAAgD;AAChD,sCAAsC;AACtC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AAIH,wEAAwE;AACxE,MAAM,UAAU,cAAc,CAAC,IAAY;IACzC,IAAI,CAAC,IAAI;QAAE,OAAO,CAAC,CAAC;IACpB,MAAM,KAAK,GAAG,IAAI,CAAC,MAAM,CAAC;IAC1B,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,MAAM,CAAC;IACvD,OAAO,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,GAAG,GAAG,CAAC,EAAE,IAAI,CAAC,IAAI,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC,CAAC;AAChE,CAAC;AAeD;;;;;;GAMG;AACH,MAAM,UAAU,kBAAkB,CAChC,OAA2B,EAC3B,MAAsD,EACtD,SAAkB;AAClB;;;;;;;;GAQG;AACH,cAAc,GAAG,CAAC;IAElB,MAAM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,IAAI,EAAE,MAAM,CAAC,MAAM;QACnB,KAAK;QACL,MAAM,EAAE,KAAK;QACb,cAAc,EAAE,SAAS,GAAG,KAAK;KAClC,CAAC;AACJ,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,iBAAiB,CAAC,CAAe,EAAE,SAAkB;IACnE,IAAI,CAAC,CAAC,KAAK,KAAK,CAAC;QAAE,OAAO,EAAE,CAAC;IAC7B,MAAM,KAAK,GAAG,CAAC,IAAI,CAAC,CAAC,MAAM,MAAM,CAAC,CAAC;IACnC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,UAAU,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,IAAI,IAAI,CAAC,CAAC,KAAK,UAAU,CAAC,CAAC;IACvF,IAAI,SAAS,KAAK,SAAS,IAAI,SAAS,GAAG,CAAC;QAAE,KAAK,CAAC,IAAI,CAAC,UAAU,SAAS,EAAE,CAAC,CAAC;IAChF,IAAI,CAAC,CAAC,cAAc,GAAG,CAAC;QAAE,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,cAAc,eAAe,CAAC,CAAC;IAC1E,OAAO,aAAa,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC;AAC3C,CAAC"}
@@ -12,6 +12,17 @@ export interface WhoamiInfo {
12
12
  http: number | null;
13
13
  hookSidecar: number | null;
14
14
  };
15
+ /**
16
+ * Effective recall configuration. `whoami` is the "what am I actually
17
+ * running" tool, and the version alone does not answer that: a build can be
18
+ * current while the recall flags that change its behaviour are unset, or set
19
+ * to something unintended. GET /health already reports this, but HTTP is off
20
+ * by default, so an MCP-only session had no way to see it.
21
+ */
22
+ recall: {
23
+ fingerprint: string;
24
+ flags: Record<string, string>;
25
+ };
15
26
  siblingAgents: string[];
16
27
  }
17
28
  /** Store subset whoami needs — every backend implements listAgentIds. */
@@ -1 +1 @@
1
- {"version":3,"file":"whoami.d.ts","sourceRoot":"","sources":["../../src/core/whoami.ts"],"names":[],"mappings":"AAqBA,MAAM,WAAW,UAAU;IACzB,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;IACzB,OAAO,EAAE,KAAK,GAAG,MAAM,CAAC;IACxB,IAAI,EAAE,YAAY,GAAG,MAAM,CAAC;IAC5B,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,MAAM,CAAC;IAClB,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,EAAE,MAAM,CAAC;IACjB,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,EAAE;QAAE,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;QAAC,WAAW,EAAE,MAAM,GAAG,IAAI,CAAA;KAAE,CAAC;IAC3D,aAAa,EAAE,MAAM,EAAE,CAAC;CACzB;AAED,yEAAyE;AACzE,MAAM,WAAW,WAAW;IAC1B,YAAY,IAAI,MAAM,EAAE,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC;CAC9C;AASD,wBAAsB,WAAW,CAC/B,KAAK,EAAE,WAAW,EAClB,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,KAAK,GAAG,MAAM,GACtB,OAAO,CAAC,UAAU,CAAC,CAyBrB;AAED,0DAA0D;AAC1D,wBAAgB,YAAY,CAAC,CAAC,EAAE,UAAU,GAAG,MAAM,CAYlD"}
1
+ {"version":3,"file":"whoami.d.ts","sourceRoot":"","sources":["../../src/core/whoami.ts"],"names":[],"mappings":"AAuBA,MAAM,WAAW,UAAU;IACzB,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;IACzB,OAAO,EAAE,KAAK,GAAG,MAAM,CAAC;IACxB,IAAI,EAAE,YAAY,GAAG,MAAM,CAAC;IAC5B,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,MAAM,CAAC;IAClB,OAAO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