agent-working-memory 0.11.0 → 0.12.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 (99) hide show
  1. package/README.md +29 -0
  2. package/dist/adapters/claude-code.d.ts.map +1 -1
  3. package/dist/adapters/claude-code.js +63 -3
  4. package/dist/adapters/claude-code.js.map +1 -1
  5. package/dist/adapters/common.d.ts.map +1 -1
  6. package/dist/adapters/common.js +329 -306
  7. package/dist/adapters/common.js.map +1 -1
  8. package/dist/api/routes.d.ts.map +1 -1
  9. package/dist/api/routes.js +29 -7
  10. package/dist/api/routes.js.map +1 -1
  11. package/dist/coordination/routes.d.ts.map +1 -1
  12. package/dist/coordination/routes.js +174 -170
  13. package/dist/coordination/routes.js.map +1 -1
  14. package/dist/core/embeddings.d.ts.map +1 -1
  15. package/dist/core/embeddings.js +3 -0
  16. package/dist/core/embeddings.js.map +1 -1
  17. package/dist/core/entity-extract.d.ts +3 -0
  18. package/dist/core/entity-extract.d.ts.map +1 -0
  19. package/dist/core/entity-extract.js +47 -0
  20. package/dist/core/entity-extract.js.map +1 -0
  21. package/dist/core/salience.d.ts.map +1 -1
  22. package/dist/core/salience.js +14 -2
  23. package/dist/core/salience.js.map +1 -1
  24. package/dist/core/whoami.d.ts +24 -0
  25. package/dist/core/whoami.d.ts.map +1 -0
  26. package/dist/core/whoami.js +66 -0
  27. package/dist/core/whoami.js.map +1 -0
  28. package/dist/core/write-pipeline.d.ts +9 -0
  29. package/dist/core/write-pipeline.d.ts.map +1 -1
  30. package/dist/core/write-pipeline.js +109 -68
  31. package/dist/core/write-pipeline.js.map +1 -1
  32. package/dist/core/write-telemetry.d.ts +33 -0
  33. package/dist/core/write-telemetry.d.ts.map +1 -0
  34. package/dist/core/write-telemetry.js +110 -0
  35. package/dist/core/write-telemetry.js.map +1 -0
  36. package/dist/engine/activation.d.ts +22 -12
  37. package/dist/engine/activation.d.ts.map +1 -1
  38. package/dist/engine/activation.js +133 -17
  39. package/dist/engine/activation.js.map +1 -1
  40. package/dist/engine/consolidation-scheduler.d.ts +1 -1
  41. package/dist/engine/consolidation-scheduler.js +1 -1
  42. package/dist/engine/consolidation.d.ts +1 -0
  43. package/dist/engine/consolidation.d.ts.map +1 -1
  44. package/dist/engine/consolidation.js +18 -0
  45. package/dist/engine/consolidation.js.map +1 -1
  46. package/dist/engine/eval.d.ts.map +1 -1
  47. package/dist/engine/eval.js +5 -1
  48. package/dist/engine/eval.js.map +1 -1
  49. package/dist/index.js +20 -2
  50. package/dist/index.js.map +1 -1
  51. package/dist/mcp.d.ts +2 -1
  52. package/dist/mcp.d.ts.map +1 -1
  53. package/dist/mcp.js +168 -100
  54. package/dist/mcp.js.map +1 -1
  55. package/dist/recipes/index.d.ts +57 -0
  56. package/dist/recipes/index.d.ts.map +1 -0
  57. package/dist/recipes/index.js +81 -0
  58. package/dist/recipes/index.js.map +1 -0
  59. package/dist/storage/pglite-schema.d.ts.map +1 -1
  60. package/dist/storage/pglite-schema.js +27 -0
  61. package/dist/storage/pglite-schema.js.map +1 -1
  62. package/dist/storage/pglite.d.ts +5 -0
  63. package/dist/storage/pglite.d.ts.map +1 -1
  64. package/dist/storage/pglite.js +180 -138
  65. package/dist/storage/pglite.js.map +1 -1
  66. package/dist/storage/postgres.d.ts +5 -0
  67. package/dist/storage/postgres.d.ts.map +1 -1
  68. package/dist/storage/postgres.js +180 -138
  69. package/dist/storage/postgres.js.map +1 -1
  70. package/dist/storage/sqlite.d.ts +9 -0
  71. package/dist/storage/sqlite.d.ts.map +1 -1
  72. package/dist/storage/sqlite.js +394 -326
  73. package/dist/storage/sqlite.js.map +1 -1
  74. package/dist/types/engram.d.ts +14 -0
  75. package/dist/types/engram.d.ts.map +1 -1
  76. package/dist/types/engram.js.map +1 -1
  77. package/package.json +1 -1
  78. package/src/adapters/claude-code.ts +66 -3
  79. package/src/adapters/common.ts +538 -515
  80. package/src/api/routes.ts +999 -971
  81. package/src/coordination/routes.ts +2155 -2150
  82. package/src/core/embeddings.ts +3 -0
  83. package/src/core/entity-extract.ts +47 -0
  84. package/src/core/salience.ts +529 -514
  85. package/src/core/whoami.ts +92 -0
  86. package/src/core/write-pipeline.ts +60 -8
  87. package/src/core/write-telemetry.ts +131 -0
  88. package/src/engine/activation.ts +1468 -1369
  89. package/src/engine/consolidation-scheduler.ts +1 -1
  90. package/src/engine/consolidation.ts +887 -869
  91. package/src/engine/eval.ts +6 -1
  92. package/src/index.ts +248 -227
  93. package/src/mcp.ts +1341 -1270
  94. package/src/recipes/index.ts +125 -0
  95. package/src/storage/pglite-schema.ts +27 -0
  96. package/src/storage/pglite.ts +1420 -1372
  97. package/src/storage/postgres.ts +1523 -1475
  98. package/src/storage/sqlite.ts +1936 -1861
  99. package/src/types/engram.ts +22 -0
@@ -1,515 +1,538 @@
1
- // Copyright 2026 Robert Winter / Complete Ideas
2
- // SPDX-License-Identifier: Apache-2.0
3
-
4
- /**
5
- * Shared utilities for CLI adapters.
6
- *
7
- * Extracted from the original setup() in cli.ts — path resolution, secrets,
8
- * environment variables, MCP command building, and the AWM instruction snippet.
9
- */
10
-
11
- import { readFileSync, writeFileSync, mkdirSync, existsSync } from 'node:fs';
12
- import { resolve, join, dirname, basename } from 'node:path';
13
- import { randomBytes } from 'node:crypto';
14
- import { homedir as osHomedir } from 'node:os';
15
- import { fileURLToPath } from 'node:url';
16
- import type { SetupContext } from './types.js';
17
-
18
- const __filename = fileURLToPath(import.meta.url);
19
- const __dirname = dirname(__filename);
20
-
21
- /** Resolve the AWM package root (where src/ and dist/ live). */
22
- export function resolvePackageRoot(): string {
23
- // __dirname is src/adapters/ at dev time, dist/adapters/ at build time
24
- return resolve(__dirname, '..', '..');
25
- }
26
-
27
- /** Resolve the database path — default to <packageRoot>/data/memory.db. */
28
- export function resolveDbPath(packageRoot: string, explicit?: string | null): string {
29
- const dbPath = explicit ?? join(packageRoot, 'data', 'memory.db');
30
- const dbDir = dirname(dbPath);
31
- if (!existsSync(dbDir)) {
32
- mkdirSync(dbDir, { recursive: true });
33
- }
34
- return dbPath;
35
- }
36
-
37
- /** Read or generate the hook secret token. */
38
- export function resolveHookSecret(dbPath: string): string {
39
- const secretPath = join(dirname(dbPath), '.awm-hook-secret');
40
- if (existsSync(secretPath)) {
41
- const existing = readFileSync(secretPath, 'utf-8').trim();
42
- if (existing) return existing;
43
- }
44
- const secret = randomBytes(32).toString('hex');
45
- mkdirSync(dirname(secretPath), { recursive: true });
46
- writeFileSync(secretPath, secret + '\n');
47
- return secret;
48
- }
49
-
50
- /** Build environment variables for the MCP server process. */
51
- export function buildEnvVars(
52
- dbPath: string,
53
- agentId: string,
54
- hookPort: string,
55
- hookSecret: string,
56
- isWindows: boolean,
57
- ): Record<string, string> {
58
- return {
59
- AWM_DB_PATH: isWindows ? dbPath.replace(/\\/g, '/') : dbPath,
60
- AWM_AGENT_ID: agentId,
61
- AWM_HOOK_PORT: hookPort,
62
- AWM_HOOK_SECRET: hookSecret,
63
- };
64
- }
65
-
66
- /**
67
- * Resolve the MCP server command + args.
68
- *
69
- * Prefers absolute path to dist/mcp.js (works from any cwd).
70
- * Falls back to npx tsx src/mcp.ts for dev mode.
71
- */
72
- export function resolveMcpCommand(ctx: SetupContext): {
73
- command: string;
74
- args: string[];
75
- } {
76
- if (ctx.hasDist) {
77
- return {
78
- command: 'node',
79
- args: [ctx.mcpDist.replace(/\\/g, '/')],
80
- };
81
- }
82
- // Dev fallback
83
- if (ctx.isWindows) {
84
- return {
85
- command: 'cmd',
86
- args: ['/c', 'npx', 'tsx', ctx.mcpScript.replace(/\\/g, '/')],
87
- };
88
- }
89
- return {
90
- command: 'npx',
91
- args: ['tsx', ctx.mcpScript],
92
- };
93
- }
94
-
95
- /** Build a full SetupContext from parsed CLI flags. */
96
- export function buildSetupContext(opts: {
97
- agentId?: string;
98
- dbPath?: string | null;
99
- isGlobal: boolean;
100
- hookPort: string;
101
- }): SetupContext {
102
- const cwd = process.cwd();
103
- const projectName = basename(cwd).toLowerCase().replace(/[^a-z0-9-]/g, '-');
104
- const packageRoot = resolvePackageRoot();
105
- const mcpScript = join(packageRoot, 'src', 'mcp.ts');
106
- const mcpDist = join(packageRoot, 'dist', 'mcp.js');
107
- const hasDist = existsSync(mcpDist);
108
- const isWindows = process.platform === 'win32';
109
-
110
- const agentId = opts.agentId ?? (opts.isGlobal ? 'claude' : projectName);
111
- const dbPath = resolveDbPath(packageRoot, opts.dbPath);
112
- const hookSecret = resolveHookSecret(dbPath);
113
- const envVars = buildEnvVars(dbPath, agentId, opts.hookPort, hookSecret, isWindows);
114
-
115
- return {
116
- cwd,
117
- projectName,
118
- agentId,
119
- dbPath,
120
- packageRoot,
121
- mcpDist,
122
- mcpScript,
123
- hasDist,
124
- hookSecret,
125
- hookPort: opts.hookPort,
126
- isGlobal: opts.isGlobal,
127
- isWindows,
128
- envVars,
129
- };
130
- }
131
-
132
- /** Home directory. */
133
- export function homedir(): string {
134
- return osHomedir();
135
- }
136
-
137
- // ─── Instruction content ────────────────────────────────
138
-
139
- /**
140
- * Core AWM instruction snippet — shared across all adapters.
141
- * Each adapter wraps this in the appropriate file format.
142
- */
143
- /**
144
- * Upsert the AWM section into an instruction file (CLAUDE.md, AGENTS.md, .cursorrules).
145
- *
146
- * Behavior:
147
- * - File doesn't exist -> create with title + AWM_INSTRUCTION_CONTENT
148
- * - Section absent -> append
149
- * - Section present + identical -> skip
150
- * - Section present + stale -> REPLACE in place, preserve content above/below
151
- *
152
- * Section is bounded by `## Memory (AWM)` (with optional trailing modifier) at the
153
- * start, and the next `## ` heading or EOF at the end.
154
- *
155
- * Returns a short human-readable status string for the setup command output.
156
- */
157
- export function upsertAwmSection(
158
- filePath: string,
159
- newContent: string,
160
- options: { titleIfNew?: string; suffix?: string } = {},
161
- ): string {
162
- const fname = basename(filePath);
163
- const suffix = options.suffix ?? '';
164
-
165
- if (!existsSync(filePath)) {
166
- const title = options.titleIfNew ?? `# ${basename(dirname(filePath))}`;
167
- mkdirSync(dirname(filePath), { recursive: true });
168
- writeFileSync(filePath, `${title}\n\n${newContent}${suffix}`);
169
- return `${fname}: created with AWM workflow section`;
170
- }
171
-
172
- const existing = readFileSync(filePath, 'utf-8');
173
-
174
- // Find section bounds: `## Memory (AWM)` (possibly with ` — MANDATORY` etc.) until next `## ` or EOF
175
- const startRegex = /^## Memory \(AWM\)[^\n]*$/m;
176
- const startMatch = startRegex.exec(existing);
177
-
178
- if (!startMatch) {
179
- // Section not present — append
180
- writeFileSync(filePath, existing.trimEnd() + '\n\n' + newContent + suffix);
181
- return `${fname}: appended AWM workflow section`;
182
- }
183
-
184
- // Find end: next `## ` heading after the section start, or EOF
185
- const afterStart = startMatch.index + startMatch[0].length;
186
- const nextHeadingRegex = /^## (?!Memory \(AWM\))/m;
187
- nextHeadingRegex.lastIndex = afterStart;
188
- const tail = existing.slice(afterStart);
189
- const nextMatch = nextHeadingRegex.exec(tail);
190
- const sectionEnd = nextMatch ? afterStart + nextMatch.index : existing.length;
191
-
192
- const currentSection = existing.slice(startMatch.index, sectionEnd).trimEnd();
193
- const desiredSection = (newContent + suffix).trimEnd();
194
-
195
- if (currentSection === desiredSection) {
196
- return `${fname}: AWM section already up-to-date (skipped)`;
197
- }
198
-
199
- const before = existing.slice(0, startMatch.index).trimEnd();
200
- const after = existing.slice(sectionEnd).replace(/^\s*\n/, '');
201
- const rebuilt =
202
- (before ? before + '\n\n' : '') +
203
- desiredSection +
204
- (after ? '\n\n' + after : '\n');
205
- writeFileSync(filePath, rebuilt);
206
- return `${fname}: AWM section updated (preserved surrounding content)`;
207
- }
208
-
209
- export const AWM_INSTRUCTION_CONTENT = `
210
- ## Memory (AWM) — MANDATORY
211
-
212
- **AWM is THE memory system.** Use it via the \`agent-working-memory\` MCP server
213
- (preferred) or HTTP at \`http://127.0.0.1:8400\` (fallback). The file-based
214
- auto-memory at \`~/.claude/projects/.../memory/*.md\` is a LEGACY bootstrap path —
215
- **do not write new memories to it.** All persistent knowledge goes through AWM.
216
-
217
- If MCP tools aren't loaded at session start, use ToolSearch with
218
- \`select:mcp__agent-working-memory__memory_recall,mcp__agent-working-memory__memory_write\`
219
- to load them. If the MCP server isn't responsive, restart with \`/mcp\` or use
220
- the HTTP endpoints (\`POST /memory/write-batch\`, \`POST /memory/activate\`)
221
- directly — but **DO NOT fall back to markdown files**. Files drift the moment
222
- you write them; AWM stays current because every agent reads + writes the same store.
223
-
224
- ### Lifecycle (always do these, in this order)
225
- 1. **Session start**: call \`memory_restore\` to recover previous context. If it reports the
226
- store is empty/new (or recall keeps returning nothing), **warm-start first**: recall the
227
- \`onboard a new project\` skill and follow it — or call \`onboard_scan\` on the project's
228
- docs/repo, refine the candidates, run \`onboard_questions\`, and save the good ones with
229
- \`memory_write\` (canonical). A cold store is nearly useless until it's seeded.
230
- 2. **Starting a task**: call \`memory_task_begin\` (checkpoints + recalls relevant memories).
231
- 3. **During work**: call \`memory_recall\` BEFORE stating any fact, BEFORE searching
232
- the filesystem, BEFORE making architectural decisions. Recall is ~300ms — cheaper
233
- than one filesystem search.
234
- 4. **As you learn things**: call \`memory_write\` proactively. Don't batch.
235
- 5. **Finishing a task**: call \`memory_task_end\` with a summary.
236
- 6. **Auto-checkpoint** is handled by hooks (compaction, session-end, 15-min timer). No action needed.
237
-
238
- ### Write memory when:
239
- - A project decision is made or changed
240
- - A root cause is discovered after debugging
241
- - A reusable implementation pattern is established
242
- - A user preference, constraint, or requirement is clarified
243
- - A prior assumption is found to be wrong
244
- - A significant piece of work is completed
245
-
246
- ### Writing for recall (the highest-leverage section)
247
- A memory's recall quality is set the moment you write it. AWM is fast at
248
- finding what's findable — but if the write is shaped wrong, no retriever
249
- can rescue it. Be slightly more verbose at the front than feels natural:
250
- the first 1-2 sentences are what BM25, the embedding model, and concept
251
- extraction all see most strongly.
252
-
253
- - **Lead with the rule or fact.** Don't open with context or backstory.
254
- "Don't mock the database in integration tests." comes first; the reason
255
- comes second. Recall scans the head of the body, not the tail.
256
- - **Pick the most specific topic.** Not \`auth\` — \`auth-magic-link-rate-limit\`.
257
- Topic is a hard filter at recall time. Generic topics hide the memory in
258
- a noisy bucket where it competes with everything else in the area.
259
- - **Include 2+ retrievable identifiers.** File paths, function names, table
260
- columns, ticket IDs, exact error strings, the literal terms a future query
261
- will use. \`AccountingService.closePeriod()\` beats "the accounting code."
262
- \`tblMemberDetails.activation_date\` beats "the activation column."
263
- \`schema/072-period-close.sql\` beats "the migration."
264
- - **Write in the vocabulary of the future question.** When you imagine asking
265
- this in three months, what nouns will you use? Use those nouns. Don't
266
- paraphrase the user's domain language into your own neutral summary.
267
- - **Reserve canonical for stable invariants.** Decisions, requirements,
268
- hard facts, cross-agent shared context. Working class (default) is correct
269
- for findings, observations, and progress notes. The canonical floor is
270
- 0.7 salience — overusing it pollutes the canonical layer and the floor
271
- loses meaning.
272
- - **Include the why for feedback memories.** A rule without a reason can't
273
- be applied to edge cases. "Don't mock the database" is brittle. "Don't
274
- mock the database — last quarter mocked tests masked a broken migration"
275
- is portable to new situations.
276
-
277
- ### Tagging rules (REQUIRED — AWM's prefix-tag retrieval boost depends on these)
278
-
279
- Every \`memory_write\` should pass these structured fields. AWM stores each as a
280
- prefix-tag like \`proj=\`, \`topic=\`, \`intent=\`, etc. and uses them for BM25
281
- and entity-bridge boosts at recall time.
282
-
283
- | Field | Required? | Format | Example |
284
- |---|---|---|---|
285
- | \`project\` | **YES** | one short word matching the current project | \`"EquiHub"\`, \`"AWM"\`, \`"USEA-Agent"\` |
286
- | \`topic\` | **YES** | one or more lowercase area words | \`"database-migration"\`, \`"benchmarks"\` |
287
- | \`intent\` | **YES** | one of: \`decision\` / \`finding\` / \`todo\` / \`question\` / \`context\` | \`"finding"\` |
288
- | \`confidence_level\` | **YES** | \`verified\` (tested) / \`observed\` (read in code) / \`assumed\` (reasoning) | \`"verified"\` |
289
- | \`source\` | recommended | \`code-reading\` / \`debugging\` / \`discussion\` / \`research\` / \`testing\` / \`observation\` | \`"testing"\` |
290
- | \`memory_class\` | when stable | \`canonical\` (source-of-truth, 0.7 floor, never staged) / \`working\` (default) / \`ephemeral\` | \`"canonical"\` |
291
- | \`session_id\` | recommended | current conversation ID for entity-bridge boost | autogenerated |
292
- | \`tags\` | when applicable | extra prefix-tags for IDs and dates | \`["ticket=18360", "date=2026-05-11"]\` |
293
-
294
- **Always add identifier tags when present in the content:**
295
- - \`ticket=<id>\` for Freshdesk tickets
296
- - \`member=<id>\` for member IDs
297
- - \`horse=<id>\` for horse_member_id
298
- - \`usef=<id>\` for USEF lookups
299
- - \`date=YYYY-MM-DD\` for temporal anchoring (ISO format)
300
- - \`person=<Name>\` for stakeholder quotes / decisions
301
- - \`version=<X.Y.Z>\` for release-specific findings
302
-
303
- ### Memory classes (controls how strictly the salience filter gates the write)
304
- - \`memory_class: canonical\` — source-of-truth memories. Floor 0.7 salience, never staged.
305
- Use for: user-stated decisions, project requirements, verified architectural facts,
306
- cross-agent shared context. **In a hive (multi-agent) setup, always use \`canonical\`
307
- for writes that other agents must be able to recall** — the default \`working\` class
308
- may get filtered.
309
- - \`memory_class: working\` (default) — observations and findings. Salience-gated.
310
- - \`memory_class: ephemeral\` — short-lived context that should decay quickly.
311
-
312
- ### Salience auto-promotion (defense in depth)
313
- The salience filter automatically promotes certain content patterns even if you forget
314
- to set \`memory_class\` explicitly:
315
- - **User feedback** — content starting with "Robert said…", "Katherine directed…",
316
- "Nancy decided…" etc. auto-promotes to canonical. So quoting the user verbatim
317
- always preserves the decision.
318
- - **Verified operational records** — content with an action verb (Submitted, Finalized,
319
- Completed, Reconciled, Triaged, Posted, Resolved, Stamped, Pushed, Deployed, Migrated,
320
- Imported, Exported, Backfilled) plus 2+ concrete identifiers (ISO date \`YYYY-MM-DD\`,
321
- or contextual numeric IDs like "event 18969", "ticket #18330", "USEF 341980") gets
322
- a 0.45 salience floor. So batch summaries with real IDs survive even when topic
323
- terms repeat.
324
-
325
- If neither pattern applies and you want a memory to definitely survive, set
326
- \`memory_class: canonical\` explicitly. Don't rely on auto-promotion for important writes.
327
-
328
- ### Recall memory when:
329
- - **BEFORE stating ANY fact about how a system works** — recall first; if AWM doesn't
330
- have it, read the code. Never guess and present it as fact.
331
- - **BEFORE searching the filesystem** — recall first; AWM is faster and has cross-session
332
- knowledge that file search doesn't.
333
- - Starting work on a new task or subsystem
334
- - Re-entering code you haven't touched recently
335
- - After a failed attempt — check if there's prior knowledge
336
- - Before refactoring or making architectural changes
337
- - When a topic comes up that you might have prior context on
338
-
339
- Recall is fast (~300ms typical). Use it freely.
340
-
341
- ### Recall strategy (when one query isn't enough)
342
- AWM's adaptive retrieval handles most query variations natively — synonym
343
- expansion, multi-channel scoring, embedding + BM25 + reranker agreement.
344
- A single recall is usually enough.
345
-
346
- When it isn't:
347
- - **If the first recall returns nothing or returns the wrong things, reformulate.**
348
- Try a second query with different phrasing — synonyms, more specific nouns,
349
- the exact identifier from the code rather than the conceptual name. Two or
350
- three recalls cost less than one filesystem search.
351
- - **Use the words a domain expert would use, not generic English.** "Period
352
- close lock" not "accounting feature"; "magic link rate limit" not "auth issue."
353
- - **For broad exploration, pass \`mode: "exploratory"\`** — wider candidate
354
- pool, lower precision floor. For specific lookups, leave mode unset (auto).
355
- - **Don't ensemble more than 3 reformulations.** If three different phrasings
356
- return nothing, the memory probably isn't there — read the code instead of
357
- burning more recalls.
358
-
359
- ### Recall tuning (0.8.x — opt-in parameters for higher-quality recall)
360
- Default \`memory_recall\` is tuned for the common case. The 0.8.x recall pipeline
361
- exposes four opt-in parameters that change the cost/quality tradeoff. Use them
362
- when the default doesn't match what you actually need.
363
-
364
- - **\`granularity: 'compact'\`** — every result carries a 200-char \`summary\`
365
- field with a query-aware snippet (the densest window of query terms in the
366
- content). Use this when you expect to scan 5+ results to find one — saves
367
- ~70% of recall output tokens. The full content stays available in
368
- \`engram.content\` if you want to drill into a specific result.
369
- - **\`granularity: 'auto'\`** — confidence-adaptive. If the top result is a clear
370
- winner, it gets a longer summary while the rest are compact. If confidence
371
- is uniform across results, everything is compact. Use when you don't know
372
- in advance whether one result will dominate.
373
- - **\`require_confidence: 0.10 | 0.25 | 0.40\`** — opt-in abstention. AWM
374
- returns \`[]\` instead of low-confidence noise. Use when you're about to ACT
375
- on the recalled fact (grounding a decision, citing the memory verbatim,
376
- contradicting a prior assumption). Thresholds: \`0.10\` strict — only abstain
377
- on garbage; \`0.25\` balanced; \`0.40\` aggressive — prefer "I don't know"
378
- over "best of bad." When abstention fires (empty result), treat it as a
379
- signal — either the memory genuinely isn't there (read the code) or your
380
- query missed (reformulate). Don't retry without the threshold.
381
- - **\`workspace: "<name>"\`** — hive-mode recall across all agents in the
382
- workspace. Use when other agents may have written canonical knowledge you
383
- need. Default is agent-scoped (your own memories only). Can also be set
384
- globally via the \`AWM_WORKSPACE\` env var.
385
-
386
- ### Keep memory fresh
387
- - After recalling a memory, if you observe the real state is different → call
388
- \`memory_supersede\` immediately with the corrected version.
389
- - After using a recalled memory: call \`memory_feedback\` (useful/not-useful) so the
390
- activation engine learns what's valuable.
391
- - If you discover a memory is factually wrong: \`memory_retract\` to remove it.
392
- - **If you bypass AWM (file-memory, in-context notes, "I'll just remember"), the memory
393
- drifts out of date. The system relies on you to keep it current. This is the #1
394
- failure mode.**
395
-
396
- ### Content fadewrite-and-forget is safe (0.8.x)
397
- Un-recalled engrams gradually fade their content while preserving cue pathways
398
- (concept + tags + embedding stay intact). This is Paper 1 storage
399
- degradation. Practical implications:
400
-
401
- - **Don't manually purge memories** to "save space." The system already
402
- compresses unused content. Old memories stay findable via cue match even
403
- when their body has decayed.
404
- - **Don't over-pin with \`memory_class: canonical\`** to fight fade. Canonical
405
- only changes salience gating at write time, not fade behavior. Fade
406
- affects un-recalled engrams of any class.
407
- - **Recall keeps content alive.** Every recall touches the engram and resets
408
- its fade clock. Frequently-recalled memories stay full-fidelity automatically.
409
- - **Supersede is the right tool for stale facts.** When you observe a memory
410
- is outdated, call \`memory_supersede\`the new version inherits the old
411
- one's coherent associations (counter-narrative replacement, 0.8.x) so cue
412
- pathways carry forward to the replacement.
413
-
414
- ### Examplegood vs bad memory_write
415
-
416
- **BAD** (no prefix tags, vague concept, can't be recalled by future queries):
417
- \`\`\`
418
- memory_write(
419
- concept="found a bug",
420
- content="The thing I was looking at was broken so I fixed it."
421
- )
422
- \`\`\`
423
-
424
- **GOOD** (rich identifiers, structured metadata, prefix tags):
425
- \`\`\`
426
- memory_write(
427
- concept="EquiHub period-close BLOCKED check missing server-side",
428
- content="apps/web/app/(accounting)/accounting/period-close/page.tsx had client-only BLOCKED enforcement. Fixed by adding server-side check in AccountingService.closePeriod() per schema/072-period-close.sql. Without server-side check a malicious request could bypass via direct API call.",
429
- project="EquiHub",
430
- topic="accounting",
431
- intent="finding",
432
- confidence_level="verified",
433
- source="debugging",
434
- memory_class="canonical",
435
- tags=["ticket=18360", "person=Robert", "date=2026-05-11", "topic=period-close", "topic=security"]
436
- )
437
- \`\`\`
438
-
439
- ### Also:
440
- - To track work items: memory_task_add, memory_task_update, memory_task_list, memory_task_next
441
- - AWM is shared across all agents in real time. When any agent writes or supersedes a
442
- memory, every other agent can recall it immediately.
443
-
444
- ### Output compression (token efficiency, output-only)
445
- When a tool returns a LARGE STRUCTURED result you need to keep in context — a JSON
446
- array of records, query rows, a log dump, an API response pass it through
447
- \`compress_output\` first. It re-encodes the data as TOON (a compact, lossless,
448
- schema-aware tabular form of JSON), cutting ~50-65% of the tokens at no
449
- comprehension cost. This is output-only: it never changes the data or your memories.
450
- - Use it on big STRUCTURED outputs, not on prose. Prose is returned unchanged —
451
- for trimming memory prose, use recall \`granularity: 'compact'\` instead.
452
- - It returns a \`ref\`; call \`retrieve_original(ref)\` if you later need the exact
453
- verbatim source (e.g. to hand it to another tool unchanged).
454
- - Don't bother for small outputs — it only compresses when the saving is worthwhile
455
- and falls back to plain JSON if TOON wouldn't reproduce the data exactly.
456
-
457
- ### Backend (SQLite vs PGlite, 0.8.x)
458
- AWM ships two storage backends. The installer picks SQLite by default; both
459
- are functionally equivalent for cognitive workloads, but differ in operational
460
- guarantees:
461
-
462
- - **SQLite** (default) — embedded, **multi-process safe** via WAL mode. Best
463
- for single-machine setups and MCP scenarios where multiple Claude Code
464
- sessions may open the same database concurrently.
465
- - **PGlite** embedded Postgres (WASM) with pgvector. **Single-process only**
466
- two MCP processes against the same \`memory-pglite/\` directory will
467
- abort the second. Pick via \`AWM_STORE_BACKEND=pglite\` and
468
- \`AWM_DB_PATH=path/to/memory-pglite\`.
469
- - **Auto-detect** if \`AWM_DB_PATH\` points to a directory that already
470
- exists, AWM detects PGlite; a file SQLite. No explicit
471
- \`AWM_STORE_BACKEND\` needed when an existing DB is present.
472
-
473
- For the comparison table (recall quality parity, BM25 vs \`ts_rank_cd\`,
474
- multi-process guarantees), see \`docs/pglite-feature-parity.md\`.
475
-
476
- ### Diagnostics / escape hatches (env vars, only if you know why)
477
- The 0.7.6→0.7.14 work cut recall latency from 11s to ~300ms. The 0.8.x work
478
- added the write-path rewrite (per-write 300+ ms → under 10ms) and PGlite
479
- parity tuning. Each optimization is gated by an env-var so it can be disabled
480
- for A/B testing if a regression appears in your workload:
481
-
482
- Recall pipeline (0.7.x):
483
- - \`AWM_DISABLE_POOL_FILTER=1\`disables the candidate pool reduction
484
- pre-filter in recall. Reverts to scoring all active candidates.
485
- - \`AWM_DISABLE_SLIM_CACHE=1\` disables the in-memory slim cache.
486
- Reverts to per-recall SQL fetch + Buffer→Float32Array conversion.
487
- - \`AWM_DISABLE_RERANK_SKIP=1\` disables the cross-encoder skip on
488
- clear-winner queries. Forces every recall through the reranker.
489
- - \`AWM_DISABLE_EXPANSION_CACHE=1\` disables the query expansion skip
490
- heuristic + LRU cache. Forces every recall through flan-t5-small.
491
-
492
- Write pipeline + lifecycle (0.8.x):
493
- - \`AWM_REINFORCE_MAX_CONTENT_LEN=1500\` — max chars an engram's content
494
- can grow to via merge-on-reinforce (drop-oldest on overflow). Higher =
495
- preserves more reinforced detail; lower = leaner recall output.
496
- - \`AWM_REINFORCE_MERGE_CONTENT=0\` disable content merge on reinforce.
497
- Reverts to pre-0.8.5 behavior (discard new content, only bump confidence).
498
- - \`AWM_NOVELTY_EMBED=0\` disable the cosine channel in novelty
499
- computation. BM25-only fallback. Reverts to pre-0.8.5 novelty.
500
- - \`AWM_GRANULARITY_COMPACT_LEN=200\` — char budget for query-aware snippet
501
- in \`granularity: 'compact'\` mode.
502
- - \`AWM_GRANULARITY_FULL_LEN=1000\` char budget for the top result in
503
- \`granularity: 'auto'\` mode when there's a clear winner.
504
-
505
- PGlite backend (0.8.x):
506
- - \`AWM_PGLITE_BM25_M=1\` multiplier on PGlite \`ts_rank_cd\` to calibrate
507
- against SQLite FTS5 BM25 distribution. M=1 (default) is passthrough;
508
- higher M boosts PGlite scores at the cost of recall-ranking precision
509
- (see CHANGELOG 0.8.5 follow-up).
510
- - \`AWM_IVFFLAT_PROBES=5\` — pgvector ivfflat probes per query. Higher =
511
- more accurate, slower.
512
-
513
- In production, leave these all unset. Use only when diagnosing a suspected
514
- recall-quality regression.
515
- `.trimStart();
1
+ // Copyright 2026 Robert Winter / Complete Ideas
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ /**
5
+ * Shared utilities for CLI adapters.
6
+ *
7
+ * Extracted from the original setup() in cli.ts — path resolution, secrets,
8
+ * environment variables, MCP command building, and the AWM instruction snippet.
9
+ */
10
+
11
+ import { readFileSync, writeFileSync, mkdirSync, existsSync } from 'node:fs';
12
+ import { resolve, join, dirname, basename } from 'node:path';
13
+ import { randomBytes } from 'node:crypto';
14
+ import { homedir as osHomedir } from 'node:os';
15
+ import { fileURLToPath } from 'node:url';
16
+ import type { SetupContext } from './types.js';
17
+
18
+ const __filename = fileURLToPath(import.meta.url);
19
+ const __dirname = dirname(__filename);
20
+
21
+ /** Resolve the AWM package root (where src/ and dist/ live). */
22
+ export function resolvePackageRoot(): string {
23
+ // __dirname is src/adapters/ at dev time, dist/adapters/ at build time
24
+ return resolve(__dirname, '..', '..');
25
+ }
26
+
27
+ /** Resolve the database path — default to <packageRoot>/data/memory.db. */
28
+ export function resolveDbPath(packageRoot: string, explicit?: string | null): string {
29
+ const dbPath = explicit ?? join(packageRoot, 'data', 'memory.db');
30
+ const dbDir = dirname(dbPath);
31
+ if (!existsSync(dbDir)) {
32
+ mkdirSync(dbDir, { recursive: true });
33
+ }
34
+ return dbPath;
35
+ }
36
+
37
+ /** Read or generate the hook secret token. */
38
+ export function resolveHookSecret(dbPath: string): string {
39
+ const secretPath = join(dirname(dbPath), '.awm-hook-secret');
40
+ if (existsSync(secretPath)) {
41
+ const existing = readFileSync(secretPath, 'utf-8').trim();
42
+ if (existing) return existing;
43
+ }
44
+ const secret = randomBytes(32).toString('hex');
45
+ mkdirSync(dirname(secretPath), { recursive: true });
46
+ writeFileSync(secretPath, secret + '\n');
47
+ return secret;
48
+ }
49
+
50
+ /** Build environment variables for the MCP server process. */
51
+ export function buildEnvVars(
52
+ dbPath: string,
53
+ agentId: string,
54
+ hookPort: string,
55
+ hookSecret: string,
56
+ isWindows: boolean,
57
+ ): Record<string, string> {
58
+ return {
59
+ AWM_DB_PATH: isWindows ? dbPath.replace(/\\/g, '/') : dbPath,
60
+ AWM_AGENT_ID: agentId,
61
+ AWM_HOOK_PORT: hookPort,
62
+ AWM_HOOK_SECRET: hookSecret,
63
+ };
64
+ }
65
+
66
+ /**
67
+ * Resolve the MCP server command + args.
68
+ *
69
+ * Prefers absolute path to dist/mcp.js (works from any cwd).
70
+ * Falls back to npx tsx src/mcp.ts for dev mode.
71
+ */
72
+ export function resolveMcpCommand(ctx: SetupContext): {
73
+ command: string;
74
+ args: string[];
75
+ } {
76
+ if (ctx.hasDist) {
77
+ return {
78
+ command: 'node',
79
+ args: [ctx.mcpDist.replace(/\\/g, '/')],
80
+ };
81
+ }
82
+ // Dev fallback
83
+ if (ctx.isWindows) {
84
+ return {
85
+ command: 'cmd',
86
+ args: ['/c', 'npx', 'tsx', ctx.mcpScript.replace(/\\/g, '/')],
87
+ };
88
+ }
89
+ return {
90
+ command: 'npx',
91
+ args: ['tsx', ctx.mcpScript],
92
+ };
93
+ }
94
+
95
+ /** Build a full SetupContext from parsed CLI flags. */
96
+ export function buildSetupContext(opts: {
97
+ agentId?: string;
98
+ dbPath?: string | null;
99
+ isGlobal: boolean;
100
+ hookPort: string;
101
+ }): SetupContext {
102
+ const cwd = process.cwd();
103
+ const projectName = basename(cwd).toLowerCase().replace(/[^a-z0-9-]/g, '-');
104
+ const packageRoot = resolvePackageRoot();
105
+ const mcpScript = join(packageRoot, 'src', 'mcp.ts');
106
+ const mcpDist = join(packageRoot, 'dist', 'mcp.js');
107
+ const hasDist = existsSync(mcpDist);
108
+ const isWindows = process.platform === 'win32';
109
+
110
+ const agentId = opts.agentId ?? (opts.isGlobal ? 'claude' : projectName);
111
+ const dbPath = resolveDbPath(packageRoot, opts.dbPath);
112
+ const hookSecret = resolveHookSecret(dbPath);
113
+ const envVars = buildEnvVars(dbPath, agentId, opts.hookPort, hookSecret, isWindows);
114
+
115
+ return {
116
+ cwd,
117
+ projectName,
118
+ agentId,
119
+ dbPath,
120
+ packageRoot,
121
+ mcpDist,
122
+ mcpScript,
123
+ hasDist,
124
+ hookSecret,
125
+ hookPort: opts.hookPort,
126
+ isGlobal: opts.isGlobal,
127
+ isWindows,
128
+ envVars,
129
+ };
130
+ }
131
+
132
+ /** Home directory. */
133
+ export function homedir(): string {
134
+ return osHomedir();
135
+ }
136
+
137
+ // ─── Instruction content ────────────────────────────────
138
+
139
+ /**
140
+ * Core AWM instruction snippet — shared across all adapters.
141
+ * Each adapter wraps this in the appropriate file format.
142
+ */
143
+ /**
144
+ * Upsert the AWM section into an instruction file (CLAUDE.md, AGENTS.md, .cursorrules).
145
+ *
146
+ * Behavior:
147
+ * - File doesn't exist -> create with title + AWM_INSTRUCTION_CONTENT
148
+ * - Section absent -> append
149
+ * - Section present + identical -> skip
150
+ * - Section present + stale -> REPLACE in place, preserve content above/below
151
+ *
152
+ * Section is bounded by `## Memory (AWM)` (with optional trailing modifier) at the
153
+ * start, and the next `## ` heading or EOF at the end.
154
+ *
155
+ * Returns a short human-readable status string for the setup command output.
156
+ */
157
+ export function upsertAwmSection(
158
+ filePath: string,
159
+ newContent: string,
160
+ options: { titleIfNew?: string; suffix?: string } = {},
161
+ ): string {
162
+ const fname = basename(filePath);
163
+ const suffix = options.suffix ?? '';
164
+
165
+ if (!existsSync(filePath)) {
166
+ const title = options.titleIfNew ?? `# ${basename(dirname(filePath))}`;
167
+ mkdirSync(dirname(filePath), { recursive: true });
168
+ writeFileSync(filePath, `${title}\n\n${newContent}${suffix}`);
169
+ return `${fname}: created with AWM workflow section`;
170
+ }
171
+
172
+ const existing = readFileSync(filePath, 'utf-8');
173
+
174
+ // Find section bounds: `## Memory (AWM)` (possibly with ` — MANDATORY` etc.) until next `## ` or EOF
175
+ const startRegex = /^## Memory \(AWM\)[^\n]*$/m;
176
+ const startMatch = startRegex.exec(existing);
177
+
178
+ if (!startMatch) {
179
+ // Section not present — append
180
+ writeFileSync(filePath, existing.trimEnd() + '\n\n' + newContent + suffix);
181
+ return `${fname}: appended AWM workflow section`;
182
+ }
183
+
184
+ // Find end: next `## ` heading after the section start, or EOF
185
+ const afterStart = startMatch.index + startMatch[0].length;
186
+ const nextHeadingRegex = /^## (?!Memory \(AWM\))/m;
187
+ nextHeadingRegex.lastIndex = afterStart;
188
+ const tail = existing.slice(afterStart);
189
+ const nextMatch = nextHeadingRegex.exec(tail);
190
+ const sectionEnd = nextMatch ? afterStart + nextMatch.index : existing.length;
191
+
192
+ const currentSection = existing.slice(startMatch.index, sectionEnd).trimEnd();
193
+ const desiredSection = (newContent + suffix).trimEnd();
194
+
195
+ if (currentSection === desiredSection) {
196
+ return `${fname}: AWM section already up-to-date (skipped)`;
197
+ }
198
+
199
+ const before = existing.slice(0, startMatch.index).trimEnd();
200
+ const after = existing.slice(sectionEnd).replace(/^\s*\n/, '');
201
+ const rebuilt =
202
+ (before ? before + '\n\n' : '') +
203
+ desiredSection +
204
+ (after ? '\n\n' + after : '\n');
205
+ writeFileSync(filePath, rebuilt);
206
+ return `${fname}: AWM section updated (preserved surrounding content)`;
207
+ }
208
+
209
+ export const AWM_INSTRUCTION_CONTENT = `
210
+ ## Memory (AWM) — MANDATORY
211
+
212
+ **AWM is THE memory system.** Use it via the \`agent-working-memory\` MCP server
213
+ (preferred) or HTTP at \`http://127.0.0.1:8400\` (fallback). The file-based
214
+ auto-memory at \`~/.claude/projects/.../memory/*.md\` is a LEGACY bootstrap path —
215
+ **do not write new memories to it.** All persistent knowledge goes through AWM.
216
+
217
+ If MCP tools aren't loaded at session start, use ToolSearch with
218
+ \`select:mcp__agent-working-memory__memory_recall,mcp__agent-working-memory__memory_write\`
219
+ to load them. If the MCP server isn't responsive, restart with \`/mcp\` or use
220
+ the HTTP endpoints (\`POST /memory/write-batch\`, \`POST /memory/activate\`)
221
+ directly — but **DO NOT fall back to markdown files**. Files drift the moment
222
+ you write them; AWM stays current because every agent reads + writes the same store.
223
+
224
+ ### Lifecycle (always do these, in this order)
225
+ 1. **Session start**: call \`memory_restore\` to recover previous context. If it reports the
226
+ store is empty/new (or recall keeps returning nothing), **warm-start first**: recall the
227
+ \`onboard a new project\` skill and follow it — or call \`onboard_scan\` on the project's
228
+ docs/repo, refine the candidates, run \`onboard_questions\`, and save the good ones with
229
+ \`memory_write\` (canonical). A cold store is nearly useless until it's seeded.
230
+ 2. **Starting a task**: call \`memory_task_begin\` (checkpoints + recalls relevant memories).
231
+ 3. **During work**: call \`memory_recall\` BEFORE stating any fact, BEFORE searching
232
+ the filesystem, BEFORE making architectural decisions. Recall is ~300ms — cheaper
233
+ than one filesystem search.
234
+ 4. **As you learn things**: call \`memory_write\` proactively. Don't batch.
235
+ 5. **Finishing a task**: call \`memory_task_end\` with a summary.
236
+ 6. **Auto-checkpoint** is handled by hooks (compaction, session-end, 15-min timer). No action needed.
237
+
238
+ ### Write memory when:
239
+ - A project decision is made or changed
240
+ - A root cause is discovered after debugging
241
+ - A reusable implementation pattern is established
242
+ - A user preference, constraint, or requirement is clarified
243
+ - A prior assumption is found to be wrong
244
+ - A significant piece of work is completed
245
+
246
+ ### Writing for recall (the highest-leverage section)
247
+ A memory's recall quality is set the moment you write it. AWM is fast at
248
+ finding what's findable — but if the write is shaped wrong, no retriever
249
+ can rescue it. Be slightly more verbose at the front than feels natural:
250
+ the first 1-2 sentences are what BM25, the embedding model, and concept
251
+ extraction all see most strongly.
252
+
253
+ - **Lead with the rule or fact.** Don't open with context or backstory.
254
+ "Don't mock the database in integration tests." comes first; the reason
255
+ comes second. Recall scans the head of the body, not the tail.
256
+ - **Pick the most specific topic.** Not \`auth\` — \`auth-magic-link-rate-limit\`.
257
+ Topic is a hard filter at recall time. Generic topics hide the memory in
258
+ a noisy bucket where it competes with everything else in the area.
259
+ - **Include 2+ retrievable identifiers.** File paths, function names, table
260
+ columns, ticket IDs, exact error strings, the literal terms a future query
261
+ will use. \`AccountingService.closePeriod()\` beats "the accounting code."
262
+ \`tblMemberDetails.activation_date\` beats "the activation column."
263
+ \`schema/072-period-close.sql\` beats "the migration."
264
+ - **Write in the vocabulary of the future question.** When you imagine asking
265
+ this in three months, what nouns will you use? Use those nouns. Don't
266
+ paraphrase the user's domain language into your own neutral summary.
267
+ - **Reserve canonical for stable invariants.** Decisions, requirements,
268
+ hard facts, cross-agent shared context. Working class (default) is correct
269
+ for findings, observations, and progress notes. The canonical floor is
270
+ 0.7 salience — overusing it pollutes the canonical layer and the floor
271
+ loses meaning.
272
+ - **Include the why for feedback memories.** A rule without a reason can't
273
+ be applied to edge cases. "Don't mock the database" is brittle. "Don't
274
+ mock the database — last quarter mocked tests masked a broken migration"
275
+ is portable to new situations.
276
+
277
+ ### Tagging rules (REQUIRED — AWM's prefix-tag retrieval boost depends on these)
278
+
279
+ Every \`memory_write\` should pass these structured fields. AWM stores each as a
280
+ prefix-tag like \`proj=\`, \`topic=\`, \`intent=\`, etc. and uses them for BM25
281
+ and entity-bridge boosts at recall time.
282
+
283
+ | Field | Required? | Format | Example |
284
+ |---|---|---|---|
285
+ | \`project\` | **YES** | one short word matching the current project | \`"EquiHub"\`, \`"AWM"\`, \`"USEA-Agent"\` |
286
+ | \`topic\` | **YES** | one or more lowercase area words | \`"database-migration"\`, \`"benchmarks"\` |
287
+ | \`intent\` | **YES** | one of: \`decision\` / \`finding\` / \`todo\` / \`question\` / \`context\` | \`"finding"\` |
288
+ | \`confidence_level\` | **YES** | \`verified\` (tested) / \`observed\` (read in code) / \`assumed\` (reasoning) | \`"verified"\` |
289
+ | \`source\` | recommended | \`code-reading\` / \`debugging\` / \`discussion\` / \`research\` / \`testing\` / \`observation\` | \`"testing"\` |
290
+ | \`memory_class\` | when stable | \`canonical\` (source-of-truth, 0.7 floor, never staged) / \`working\` (default) / \`ephemeral\` | \`"canonical"\` |
291
+ | \`session_id\` | recommended | current conversation ID for entity-bridge boost | autogenerated |
292
+ | \`tags\` | when applicable | extra prefix-tags for IDs and dates | \`["ticket=18360", "date=2026-05-11"]\` |
293
+
294
+ **Always add identifier tags when present in the content:**
295
+ - \`ticket=<id>\` for Freshdesk tickets
296
+ - \`member=<id>\` for member IDs
297
+ - \`horse=<id>\` for horse_member_id
298
+ - \`usef=<id>\` for USEF lookups
299
+ - \`date=YYYY-MM-DD\` for temporal anchoring (ISO format)
300
+ - \`person=<Name>\` for stakeholder quotes / decisions
301
+ - \`version=<X.Y.Z>\` for release-specific findings
302
+
303
+ ### Memory classes (controls how strictly the salience filter gates the write)
304
+ - \`memory_class: canonical\` — source-of-truth memories. Floor 0.7 salience, never staged.
305
+ Use for: user-stated decisions, project requirements, verified architectural facts,
306
+ cross-agent shared context. **In a hive (multi-agent) setup, always use \`canonical\`
307
+ for writes that other agents must be able to recall** — the default \`working\` class
308
+ may get filtered.
309
+ - \`memory_class: working\` (default) — observations and findings. Salience-gated.
310
+ - \`memory_class: ephemeral\` — short-lived context that should decay quickly.
311
+
312
+ ### Salience auto-promotion (defense in depth)
313
+ The salience filter automatically promotes certain content patterns even if you forget
314
+ to set \`memory_class\` explicitly:
315
+ - **User feedback** — content starting with "Robert said…", "Katherine directed…",
316
+ "Nancy decided…" etc. auto-promotes to canonical. So quoting the user verbatim
317
+ always preserves the decision.
318
+ - **Verified operational records** — content with an action verb (Submitted, Finalized,
319
+ Completed, Reconciled, Triaged, Posted, Resolved, Stamped, Pushed, Deployed, Migrated,
320
+ Imported, Exported, Backfilled) plus 2+ concrete identifiers (ISO date \`YYYY-MM-DD\`,
321
+ or contextual numeric IDs like "event 18969", "ticket #18330", "USEF 341980") gets
322
+ a 0.45 salience floor. So batch summaries with real IDs survive even when topic
323
+ terms repeat.
324
+
325
+ If neither pattern applies and you want a memory to definitely survive, set
326
+ \`memory_class: canonical\` explicitly. Don't rely on auto-promotion for important writes.
327
+
328
+ ### Recall memory when:
329
+ - **BEFORE stating ANY fact about how a system works** — recall first; if AWM doesn't
330
+ have it, read the code. Never guess and present it as fact.
331
+ - **BEFORE searching the filesystem** — recall first; AWM is faster and has cross-session
332
+ knowledge that file search doesn't.
333
+ - Starting work on a new task or subsystem
334
+ - Re-entering code you haven't touched recently
335
+ - After a failed attempt — check if there's prior knowledge
336
+ - Before refactoring or making architectural changes
337
+ - When a topic comes up that you might have prior context on
338
+
339
+ Recall is fast (~300ms typical). Use it freely.
340
+
341
+ ### Recall strategy (when one query isn't enough)
342
+ AWM's adaptive retrieval handles most query variations natively — synonym
343
+ expansion, multi-channel scoring, embedding + BM25 + reranker agreement.
344
+ A single recall is usually enough.
345
+
346
+ When it isn't:
347
+ - **If the first recall returns nothing or returns the wrong things, reformulate.**
348
+ Try a second query with different phrasing — synonyms, more specific nouns,
349
+ the exact identifier from the code rather than the conceptual name. Two or
350
+ three recalls cost less than one filesystem search.
351
+ - **Use the words a domain expert would use, not generic English.** "Period
352
+ close lock" not "accounting feature"; "magic link rate limit" not "auth issue."
353
+ - **For broad exploration, pass \`mode: "exploratory"\`** — wider candidate
354
+ pool, lower precision floor. For specific lookups, leave mode unset (auto).
355
+ - **Don't ensemble more than 3 reformulations.** If three different phrasings
356
+ return nothing, the memory probably isn't there — read the code instead of
357
+ burning more recalls.
358
+
359
+ ### Recall tuning (0.8.x — opt-in parameters for higher-quality recall)
360
+ Default \`memory_recall\` is tuned for the common case. The 0.8.x recall pipeline
361
+ exposes four opt-in parameters that change the cost/quality tradeoff. Use them
362
+ when the default doesn't match what you actually need.
363
+
364
+ - **\`granularity: 'compact'\`** — every result carries a 200-char \`summary\`
365
+ field with a query-aware snippet (the densest window of query terms in the
366
+ content). Use this when you expect to scan 5+ results to find one — saves
367
+ ~70% of recall output tokens. The full content stays available in
368
+ \`engram.content\` if you want to drill into a specific result.
369
+ - **\`granularity: 'auto'\`** — confidence-adaptive. If the top result is a clear
370
+ winner, it gets a longer summary while the rest are compact. If confidence
371
+ is uniform across results, everything is compact. Use when you don't know
372
+ in advance whether one result will dominate.
373
+ - **\`require_confidence: 0.10 | 0.25 | 0.40\`** — opt-in abstention. AWM
374
+ returns \`[]\` instead of low-confidence noise. Use when you're about to ACT
375
+ on the recalled fact (grounding a decision, citing the memory verbatim,
376
+ contradicting a prior assumption). Thresholds: \`0.10\` strict — only abstain
377
+ on garbage; \`0.25\` balanced; \`0.40\` aggressive — prefer "I don't know"
378
+ over "best of bad." When abstention fires (empty result), treat it as a
379
+ signal — either the memory genuinely isn't there (read the code) or your
380
+ query missed (reformulate). Don't retry without the threshold.
381
+ - **\`workspace: "<name>"\`** — hive-mode recall across all agents in the
382
+ workspace. Use when other agents may have written canonical knowledge you
383
+ need. Default is agent-scoped (your own memories only). Can also be set
384
+ globally via the \`AWM_WORKSPACE\` env var.
385
+
386
+ ### Keep memory fresh
387
+ - After recalling a memory, if you observe the real state is different → call
388
+ \`memory_supersede\` immediately with the corrected version.
389
+ - After using a recalled memory: call \`memory_feedback\` (useful/not-useful) so the
390
+ activation engine learns what's valuable.
391
+ - If you discover a memory is factually wrong: \`memory_retract\` to remove it.
392
+ - **If you bypass AWM (file-memory, in-context notes, "I'll just remember"), the memory
393
+ drifts out of date. The system relies on you to keep it current. This is the #1
394
+ failure mode.**
395
+
396
+ ### Cognition recipesYOU do the thinking, AWM keeps the result (0.11.x)
397
+ AWM contains no LLM. When memory needs real thinking — distilling a repeatable
398
+ procedure, reflecting on a failure AWM hands YOU a versioned recipe (prompt +
399
+ strict output shape) and you run it as a SEPARATE focused pass, then write the
400
+ result back as an ordinary memory with provenance.
401
+
402
+ - \`memory_task_end\` responses include the recipe invitations. Honor the gates:
403
+ skill-derivation only after a genuinely procedural task (3+ tool calls or a
404
+ delegated sub-task); friction-lesson only after a failure/retry/wrong assumption.
405
+ - Run each recipe as its own focused pass do NOT bundle it with other
406
+ reasoning; bundled passes reliably drop the output.
407
+ - Write back exactly per the recipe's contract: \`origin_class: 'recipe'\` +
408
+ \`recipe_id\` (e.g. \`skill-derivation@1\`), concept prefixed \`skill: \` or
409
+ \`lesson: \`. AWM validates the shape and rejects malformed or unknown-recipe
410
+ writes with the contract echoed back fix and retry, don't drop the insight.
411
+ - Re-deriving the same skill name reinforces the existing memory instead of
412
+ duplicating it, so don't fear writing a skill you may have written before.
413
+
414
+ ### Content fade write-and-forget is safe (0.8.x)
415
+ Un-recalled engrams gradually fade their content while preserving cue pathways
416
+ (concept + tags + embedding stay intact). This is Paper 1 — storage
417
+ degradation. Practical implications:
418
+
419
+ - **Don't manually purge memories** to "save space." The system already
420
+ compresses unused content. Old memories stay findable via cue match even
421
+ when their body has decayed.
422
+ - **Don't over-pin with \`memory_class: canonical\`** to fight fade. Canonical
423
+ only changes salience gating at write time, not fade behavior. Fade
424
+ affects un-recalled engrams of any class.
425
+ - **Recall keeps content alive.** Every recall touches the engram and resets
426
+ its fade clock. Frequently-recalled memories stay full-fidelity automatically.
427
+ - **Supersede is the right tool for stale facts.** When you observe a memory
428
+ is outdated, call \`memory_supersede\` the new version inherits the old
429
+ one's coherent associations (counter-narrative replacement, 0.8.x) so cue
430
+ pathways carry forward to the replacement.
431
+
432
+ ### Example — good vs bad memory_write
433
+
434
+ **BAD** (no prefix tags, vague concept, can't be recalled by future queries):
435
+ \`\`\`
436
+ memory_write(
437
+ concept="found a bug",
438
+ content="The thing I was looking at was broken so I fixed it."
439
+ )
440
+ \`\`\`
441
+
442
+ **GOOD** (rich identifiers, structured metadata, prefix tags):
443
+ \`\`\`
444
+ memory_write(
445
+ concept="EquiHub period-close BLOCKED check missing server-side",
446
+ content="apps/web/app/(accounting)/accounting/period-close/page.tsx had client-only BLOCKED enforcement. Fixed by adding server-side check in AccountingService.closePeriod() per schema/072-period-close.sql. Without server-side check a malicious request could bypass via direct API call.",
447
+ project="EquiHub",
448
+ topic="accounting",
449
+ intent="finding",
450
+ confidence_level="verified",
451
+ source="debugging",
452
+ memory_class="canonical",
453
+ tags=["ticket=18360", "person=Robert", "date=2026-05-11", "topic=period-close", "topic=security"]
454
+ )
455
+ \`\`\`
456
+
457
+ ### Also:
458
+ - To track work items: memory_task_add, memory_task_update, memory_task_list, memory_task_next
459
+ - AWM is shared across all agents in real time. When any agent writes or supersedes a
460
+ memory, every other agent can recall it immediately.
461
+
462
+ ### Output compression (token efficiency, output-only)
463
+ When a tool returns a LARGE STRUCTURED result you need to keep in context — a JSON
464
+ array of records, query rows, a log dump, an API response — pass it through
465
+ \`compress_output\` first. It re-encodes the data as TOON (a compact, lossless,
466
+ schema-aware tabular form of JSON), cutting ~50-65% of the tokens at no
467
+ comprehension cost. This is output-only: it never changes the data or your memories.
468
+ - Use it on big STRUCTURED outputs, not on prose. Prose is returned unchanged —
469
+ for trimming memory prose, use recall \`granularity: 'compact'\` instead.
470
+ - It returns a \`ref\`; call \`retrieve_original(ref)\` if you later need the exact
471
+ verbatim source (e.g. to hand it to another tool unchanged).
472
+ - Don't bother for small outputs — it only compresses when the saving is worthwhile
473
+ and falls back to plain JSON if TOON wouldn't reproduce the data exactly.
474
+
475
+ ### Backend (SQLite vs PGlite, 0.8.x)
476
+ AWM ships two storage backends. The installer picks SQLite by default; both
477
+ are functionally equivalent for cognitive workloads, but differ in operational
478
+ guarantees:
479
+
480
+ - **SQLite** (default) embedded, **multi-process safe** via WAL mode. Best
481
+ for single-machine setups and MCP scenarios where multiple Claude Code
482
+ sessions may open the same database concurrently.
483
+ - **PGlite**embedded Postgres (WASM) with pgvector. **Single-process only**
484
+ two MCP processes against the same \`memory-pglite/\` directory will
485
+ abort the second. Pick via \`AWM_STORE_BACKEND=pglite\` and
486
+ \`AWM_DB_PATH=path/to/memory-pglite\`.
487
+ - **Auto-detect** — if \`AWM_DB_PATH\` points to a directory that already
488
+ exists, AWM detects PGlite; a file SQLite. No explicit
489
+ \`AWM_STORE_BACKEND\` needed when an existing DB is present.
490
+
491
+ For the comparison table (recall quality parity, BM25 vs \`ts_rank_cd\`,
492
+ multi-process guarantees), see \`docs/pglite-feature-parity.md\`.
493
+
494
+ ### Diagnostics / escape hatches (env vars, only if you know why)
495
+ The 0.7.6→0.7.14 work cut recall latency from 11s to ~300ms. The 0.8.x work
496
+ added the write-path rewrite (per-write 300+ ms under 10ms) and PGlite
497
+ parity tuning. Each optimization is gated by an env-var so it can be disabled
498
+ for A/B testing if a regression appears in your workload:
499
+
500
+ Recall pipeline (0.7.x):
501
+ - \`AWM_DISABLE_POOL_FILTER=1\` — disables the candidate pool reduction
502
+ pre-filter in recall. Reverts to scoring all active candidates.
503
+ - \`AWM_SLOW_WRITE_MS\` slow-write telemetry threshold in ms (default 250;
504
+ 0 disables the always-on slow-write stderr line).
505
+ - \`memory_whoami\` (MCP) / \`GET /whoami\` — identify the instance (agent, mode,
506
+ backend, store path, code provenance, sibling agent spaces) when unsure
507
+ which AWM you are talking to.
508
+ - \`AWM_DISABLE_SLIM_CACHE=1\` disables the in-memory slim cache.
509
+ Reverts to per-recall SQL fetch + Buffer→Float32Array conversion.
510
+ - \`AWM_DISABLE_RERANK_SKIP=1\` — disables the cross-encoder skip on
511
+ clear-winner queries. Forces every recall through the reranker.
512
+ - \`AWM_DISABLE_EXPANSION_CACHE=1\` — disables the query expansion skip
513
+ heuristic + LRU cache. Forces every recall through flan-t5-small.
514
+
515
+ Write pipeline + lifecycle (0.8.x):
516
+ - \`AWM_REINFORCE_MAX_CONTENT_LEN=1500\` — max chars an engram's content
517
+ can grow to via merge-on-reinforce (drop-oldest on overflow). Higher =
518
+ preserves more reinforced detail; lower = leaner recall output.
519
+ - \`AWM_REINFORCE_MERGE_CONTENT=0\` — disable content merge on reinforce.
520
+ Reverts to pre-0.8.5 behavior (discard new content, only bump confidence).
521
+ - \`AWM_NOVELTY_EMBED=0\` — disable the cosine channel in novelty
522
+ computation. BM25-only fallback. Reverts to pre-0.8.5 novelty.
523
+ - \`AWM_GRANULARITY_COMPACT_LEN=200\` — char budget for query-aware snippet
524
+ in \`granularity: 'compact'\` mode.
525
+ - \`AWM_GRANULARITY_FULL_LEN=1000\` — char budget for the top result in
526
+ \`granularity: 'auto'\` mode when there's a clear winner.
527
+
528
+ PGlite backend (0.8.x):
529
+ - \`AWM_PGLITE_BM25_M=1\` — multiplier on PGlite \`ts_rank_cd\` to calibrate
530
+ against SQLite FTS5 BM25 distribution. M=1 (default) is passthrough;
531
+ higher M boosts PGlite scores at the cost of recall-ranking precision
532
+ (see CHANGELOG 0.8.5 follow-up).
533
+ - \`AWM_IVFFLAT_PROBES=5\` — pgvector ivfflat probes per query. Higher =
534
+ more accurate, slower.
535
+
536
+ In production, leave these all unset. Use only when diagnosing a suspected
537
+ recall-quality regression.
538
+ `.trimStart();