@pleri/olam-cli 0.1.204 → 0.1.205

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.
@@ -1,4 +1,4 @@
1
1
  {
2
- "bundledAt": "2026-06-02T05:18:01.119Z",
2
+ "bundledAt": "2026-06-02T08:06:35.843Z",
3
3
  "kgFirstSha": "29a9ccce1b115d049e375c4a90eb5cf7c123e610e2d0590270a4db2cdbc64a28"
4
4
  }
@@ -118,7 +118,7 @@ spec:
118
118
  # k3d), started by `olam upgrade` Step 0.7 — not inside this Pod.
119
119
  containers:
120
120
  - name: olam-host-cp
121
- image: ghcr.io/pleri/olam-host-cp@sha256:a92c2849bdacd77c1a5dec59c45377af9fd4c76b10da98482e4cd4d59ca7cb33
121
+ image: ghcr.io/pleri/olam-host-cp@sha256:501037587e33d0a1c13df88d72ad9b855508e4c0163cb65673211cd3ddd9227b
122
122
  imagePullPolicy: IfNotPresent
123
123
  securityContext:
124
124
  runAsNonRoot: true
@@ -70,7 +70,7 @@ spec:
70
70
  mountPath: /data
71
71
  containers:
72
72
  - name: olam-auth-service
73
- image: ghcr.io/pleri/olam-auth@sha256:b8165709ae12fe6b84a6e61cf935715af75a141835b7e452f5b80c1d98ced062
73
+ image: ghcr.io/pleri/olam-auth@sha256:06c9cd39405e650141a78927f1701bf7b06a86cd98eeaef9b22eb53d46f6f523
74
74
  imagePullPolicy: IfNotPresent
75
75
  securityContext:
76
76
  runAsNonRoot: true
@@ -61,7 +61,7 @@ spec:
61
61
  mountPath: /data
62
62
  containers:
63
63
  - name: olam-kg-service
64
- image: ghcr.io/pleri/olam-kg-service@sha256:e1f3c74e5b8f7e22b1b093ababd16fba59dd973040fa1d7040c81858a3382c66
64
+ image: ghcr.io/pleri/olam-kg-service@sha256:a739f0fdf60aef0d26b51314698a07bba423c3d43571616cffd7b1195abb205d
65
65
  imagePullPolicy: IfNotPresent
66
66
  securityContext:
67
67
  runAsNonRoot: true
@@ -68,7 +68,7 @@ spec:
68
68
  mountPath: /data
69
69
  containers:
70
70
  - name: olam-mcp-auth-service
71
- image: ghcr.io/pleri/olam-mcp-auth@sha256:0322f58514626ff0c45ced3a7711d03d5bd2d2ec158de46a396531920a15c49b
71
+ image: ghcr.io/pleri/olam-mcp-auth@sha256:cdeb8813619f0198dbbcc80b2c98dbeb3a842e8d1ec38796d459ad254c5e1d98
72
72
  imagePullPolicy: IfNotPresent
73
73
  securityContext:
74
74
  runAsNonRoot: true
@@ -70,7 +70,7 @@ spec:
70
70
  # bootstrap-placeholder comment + run `npm run refresh:manifest-digests`
71
71
  # once ghcr.io/pleri/olam-memory-service has a real published digest.
72
72
  # bootstrap-placeholder: pre-publish; refresh after first release
73
- image: ghcr.io/pleri/olam-memory-service@sha256:a9ede246fc89ad7c47c938400f544f677e92e8f6a3addf49e585f0f314b2c133
73
+ image: ghcr.io/pleri/olam-memory-service@sha256:b10649ba9b56bb11b99e5142559d4b8a37a2b8ae2ad8ce38fb8b535449d26f85
74
74
  imagePullPolicy: IfNotPresent
75
75
  securityContext:
76
76
  runAsNonRoot: true
@@ -0,0 +1,363 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * agentmemory-classify-queue.mjs
4
+ *
5
+ * PostToolUse hook for Claude Code — captures every PostToolUse event
6
+ * as a queue candidate at `~/.olam/agentmemory-queue/<ts>-<rand>.jsonl`
7
+ * for later batch-classification by Phase B2's background classifier.
8
+ *
9
+ * Hot-path SLO: p99 dump latency < 100ms (Phase A plan P1 row).
10
+ * Fail-open: any error exits 0 with no stdout — never blocks the tool loop.
11
+ *
12
+ * Pipeline:
13
+ * 1. Read JSON event from stdin (Claude Code hook protocol).
14
+ * 2. Per-tool capture (closes B1 CP3 ADV-B1-002 + ADV-B1-003): each
15
+ * tool_name has an explicit extraction function instead of generic
16
+ * shape-probing. Write/NotebookEdit DO NOT capture file body
17
+ * (only `wrote <path> (<N> bytes)`) — avoids leaking .env /
18
+ * credentials / id_rsa file writes into the queue. Edit/MultiEdit
19
+ * capture OLD/NEW strings so substantive changes reach B2.
20
+ * 3. Strip secrets from the captured text BEFORE writing to disk
21
+ * (12 patterns; see SECRET_PATTERNS). Includes quoted env-var
22
+ * values, AWS access keys, Slack tokens, Stripe keys, JWTs,
23
+ * private-key armor, DB URLs with creds, and context-anchored
24
+ * hex64 bearer tokens. Closes plan OQ12 + B1 CP3 ADV-B1-001 +
25
+ * ADV-B1-004.
26
+ * 4. Cap queue depth at 50 (force-flush trigger) / 80 (hard drop).
27
+ * Closes OQ10.
28
+ * 5. Atomic write via tmp + rename. 8-byte rand suffix (2^64 space;
29
+ * birthday-paradox-free at any realistic capture rate). Closes
30
+ * B1 CP3 ADV-B1-005.
31
+ *
32
+ * Candidate schema (B1.1 — closes ADV-B1-006):
33
+ * { ts, tool_name, cwd, session_id, captured_text, queue_pressure }
34
+ *
35
+ * Install (operator runs manually):
36
+ * $ cp packages/memory-service/hooks/agentmemory-classify-queue.mjs \
37
+ * ~/.claude/scripts/hooks/agentmemory-classify-queue.mjs
38
+ * $ jq '.hooks.PostToolUse += [{"command": "~/.claude/scripts/hooks/agentmemory-classify-queue.mjs"}]' \
39
+ * ~/.claude/settings.json > ~/.claude/settings.json.new
40
+ * $ mv ~/.claude/settings.json.new ~/.claude/settings.json
41
+ * (Phase B3 will ship `olam memory classifier install-hook` to
42
+ * automate this.)
43
+ *
44
+ * Plan reference:
45
+ * docs/plans/agentmemory-classifier-and-regret/phase-b-tasks.md B1
46
+ * OQ10 (queue depth cap), OQ12 (secret stripper before enqueue)
47
+ * B1 CP3 ADV-B1-001..006 (B1.1 follow-up)
48
+ */
49
+
50
+ import {
51
+ existsSync,
52
+ mkdirSync,
53
+ readdirSync,
54
+ readFileSync,
55
+ realpathSync,
56
+ renameSync,
57
+ writeFileSync,
58
+ } from 'node:fs';
59
+ import { homedir } from 'node:os';
60
+ import { join } from 'node:path';
61
+ import { randomBytes } from 'node:crypto';
62
+ import { fileURLToPath } from 'node:url';
63
+
64
+ /** Max bytes of captured text persisted per candidate. Keeps queue files small. */
65
+ export const MAX_CAPTURED_CHARS = 4000;
66
+
67
+ /** Queue depth at which `main()` triggers a force-flush of the oldest 20 entries. */
68
+ export const QUEUE_FORCE_FLUSH_THRESHOLD = 50;
69
+
70
+ /** Queue depth at which `main()` drops the new event entirely (warn-log only). */
71
+ export const QUEUE_DROP_THRESHOLD = 80;
72
+
73
+ /**
74
+ * Random bytes appended to each queue filename. 8 bytes = 2^64 space;
75
+ * birthday-paradox-free at any realistic capture rate (collision
76
+ * probability < 1e-12 even at 100M events). Was 4 bytes in B1 (~1.6%
77
+ * collision at 360K events/hour bursts — silently lost a candidate per
78
+ * collision per ADV-B1-005).
79
+ */
80
+ const RAND_BYTES = 8;
81
+
82
+ /**
83
+ * Vetted secret patterns. Each pattern matches a specific secret shape;
84
+ * matches are redacted with `<REDACTED:<pattern-name>>` so the queue
85
+ * file stays parseable and the redaction is observable in audits.
86
+ *
87
+ * Pattern set covers (closes ADV-B1-001 quoted-env-var bypass + adds
88
+ * coverage for shapes B1's initial 6-pattern set missed):
89
+ * env-var-assign — `KEY=value`, `KEY='value'`, `KEY="value"`,
90
+ * `"KEY":"value"` JSON-stringified form.
91
+ * Permissive key matcher (any uppercase
92
+ * identifier ending in SECRET|KEY|TOKEN|
93
+ * PASSWORD|PASS|BEARER|AUTH|CREDS|CREDENTIALS)
94
+ * so STRIPE_SECRET_KEY, CUSTOM_API_TOKEN,
95
+ * etc. all fire.
96
+ * bearer — `Bearer <token>` Authorization headers.
97
+ * sk-prefix — Anthropic/OpenAI keys.
98
+ * gh-pat — GitHub personal access tokens.
99
+ * slack-token — `xoxa|xoxb|xoxp|xoxr|xoxs|xoxo-...`
100
+ * aws-access-key-id — `AKIA[A-Z0-9]{16}` access key IDs.
101
+ * stripe-key — `sk_live_...` / `sk_test_...`.
102
+ * jwt — 3-segment base64url tokens.
103
+ * hex64-bearer — 64-hex tokens ONLY when adjacent to
104
+ * bearer|token|secret|key context word.
105
+ * (Was unanchored in B1; false-fired on
106
+ * container digests / sha256sum output per
107
+ * ADV-B1-004.)
108
+ * private-key-armor — PEM/OpenSSH key headers.
109
+ * db-url-with-creds — `postgres|mysql|mongodb|redis://user:pw@host`.
110
+ */
111
+ export const SECRET_PATTERNS = [
112
+ {
113
+ name: 'env-var-assign',
114
+ re: /\b[A-Z][A-Z0-9_]*(?:SECRET|KEY|TOKEN|PASSWORD|PASS|BEARER|AUTH|CREDS|CREDENTIALS)[A-Z0-9_]*\s*[:=]\s*(?:"[^"\n]+"|'[^'\n]+'|[^\s"',;\n]+)/g,
115
+ },
116
+ {
117
+ // Same shape but inside JSON-string keys: `"FOO_KEY":"value"`.
118
+ // env-var-assign catches the unquoted KEY case; this rule catches
119
+ // the JSON-stringified key case where the key itself is wrapped in
120
+ // double-quotes.
121
+ name: 'env-var-assign',
122
+ re: /"[A-Z][A-Z0-9_]*(?:SECRET|KEY|TOKEN|PASSWORD|PASS|BEARER|AUTH|CREDS|CREDENTIALS)[A-Z0-9_]*"\s*:\s*"[^"\n]+"/g,
123
+ },
124
+ { name: 'bearer', re: /\bBearer\s+[A-Za-z0-9._-]{16,}/g },
125
+ { name: 'sk-prefix', re: /\bsk-[A-Za-z0-9_-]{20,}/g },
126
+ { name: 'gh-pat', re: /\bghp_[A-Za-z0-9]{30,}/g },
127
+ { name: 'slack-token', re: /\bxox[abopsr]-[A-Za-z0-9-]{10,}/g },
128
+ { name: 'aws-access-key-id', re: /\bAKIA[A-Z0-9]{16}\b/g },
129
+ { name: 'stripe-key', re: /\bsk_(?:live|test)_[A-Za-z0-9]{20,}/g },
130
+ { name: 'jwt', re: /\beyJ[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}/g },
131
+ { name: 'hex64-bearer', re: /\b(?:bearer|token|secret|key)[\s:=]+[a-f0-9]{64}\b/gi },
132
+ { name: 'private-key-armor', re: /-----BEGIN [A-Z ]+PRIVATE KEY-----/g },
133
+ { name: 'db-url-with-creds', re: /\b(?:postgres|postgresql|mysql|mongodb|redis):\/\/[^:@\s'"]+:[^@\s'"]+@[^\s'"]+/g },
134
+ ];
135
+
136
+ /** Resolved queue directory path under `~/.olam/agentmemory-queue/`. */
137
+ export function queueDir() {
138
+ return join(homedir(), '.olam', 'agentmemory-queue');
139
+ }
140
+
141
+ /**
142
+ * Strip all configured secret patterns from `text`. Returns the
143
+ * redacted text. Idempotent — running twice doesn't re-redact.
144
+ */
145
+ export function stripSecrets(text) {
146
+ if (typeof text !== 'string' || text.length === 0) return text;
147
+ let out = text;
148
+ for (const { name, re } of SECRET_PATTERNS) {
149
+ const pattern = new RegExp(re.source, re.flags);
150
+ out = out.replace(pattern, `<REDACTED:${name}>`);
151
+ }
152
+ return out;
153
+ }
154
+
155
+ /** Ensure the queue directory exists (idempotent). */
156
+ export function ensureQueueDir(dir = queueDir()) {
157
+ if (!existsSync(dir)) {
158
+ mkdirSync(dir, { recursive: true });
159
+ }
160
+ }
161
+
162
+ /** Count `.jsonl` files in the queue dir (excludes the `.warn.log`). */
163
+ export function queueDepth(dir = queueDir()) {
164
+ if (!existsSync(dir)) return 0;
165
+ return readdirSync(dir).filter((n) => n.endsWith('.jsonl')).length;
166
+ }
167
+
168
+ /**
169
+ * Atomic queue-file write. Writes to a tmp path then renames into place
170
+ * so a partial JSONL never ends up in the queue dir. Uses 8-byte rand
171
+ * to eliminate collision class (ADV-B1-005).
172
+ */
173
+ export function writeCandidate(candidate, dir = queueDir()) {
174
+ ensureQueueDir(dir);
175
+ const ts = candidate.ts ?? Date.now();
176
+ const rand = randomBytes(RAND_BYTES).toString('hex');
177
+ const final = join(dir, `${ts}-${rand}.jsonl`);
178
+ const tmp = final + '.tmp';
179
+ writeFileSync(tmp, JSON.stringify(candidate) + '\n', 'utf-8');
180
+ renameSync(tmp, final);
181
+ return final;
182
+ }
183
+
184
+ /** Append a structured warning to the queue dir's `.warn.log` (fail-open). */
185
+ export function warnLog(message, dir = queueDir()) {
186
+ try {
187
+ ensureQueueDir(dir);
188
+ const line = JSON.stringify({ ts: Date.now(), level: 'warn', message }) + '\n';
189
+ writeFileSync(join(dir, '.warn.log'), line, { flag: 'a' });
190
+ } catch {
191
+ // Fail-open: warn-log failure is non-fatal.
192
+ }
193
+ }
194
+
195
+ /**
196
+ * Build a candidate payload from a Claude Code PostToolUse event. The
197
+ * captured text is per-tool (see {@link extractCapturedText}); secrets
198
+ * are stripped before this candidate ever touches disk.
199
+ *
200
+ * Schema (B1.1):
201
+ * ts unix ms timestamp
202
+ * tool_name from event.tool_name
203
+ * cwd from event.cwd
204
+ * session_id from event.session_id (B2 needs this for chain
205
+ * reconstruction — was missing in B1, would have
206
+ * forever-orphaned every pre-fix candidate)
207
+ * captured_text per-tool extraction, secret-stripped, truncated
208
+ * queue_pressure depth at capture time (B2 observability)
209
+ */
210
+ export function buildCandidate(event, depth = 0, now = Date.now()) {
211
+ const rawText = extractCapturedText(event);
212
+ const stripped = stripSecrets(rawText);
213
+ const truncated = stripped.length > MAX_CAPTURED_CHARS
214
+ ? stripped.slice(0, MAX_CAPTURED_CHARS)
215
+ : stripped;
216
+ return {
217
+ ts: now,
218
+ tool_name: event?.tool_name ?? null,
219
+ cwd: event?.cwd ?? null,
220
+ session_id: event?.session_id ?? null,
221
+ captured_text: truncated,
222
+ queue_pressure: depth,
223
+ };
224
+ }
225
+
226
+ /**
227
+ * Per-tool capture map. Each tool_name has an explicit extraction
228
+ * function — generic shape-probing would leak file bodies for Write
229
+ * (per ADV-B1-002) and miss substantive edits for Edit/MultiEdit
230
+ * (per ADV-B1-003). The explicit map closes both classes.
231
+ *
232
+ * Write / NotebookEdit: capture ONLY `wrote <path> (<N> bytes)` — never
233
+ * the file body. Operator writing ~/.env or ~/.aws/credentials gets
234
+ * the metadata in the queue, not the secret content.
235
+ * Edit / MultiEdit: capture OLD + NEW strings (substantive change).
236
+ * stripSecrets runs over the serialized form, so secret-bearing
237
+ * edits are still redacted.
238
+ * Read: capture `read <path>` + first 2KB of content. Same Write
239
+ * reasoning would apply but Read explicitly returns content the
240
+ * agent will reason over — withholding it makes classification
241
+ * useless.
242
+ * Bash: capture command + stdout.
243
+ * Generic fallback: tool_response.content / .text.
244
+ */
245
+ export function extractCapturedText(event) {
246
+ if (!event || typeof event !== 'object') return '';
247
+ const toolName = event.tool_name;
248
+ const ti = event.tool_input ?? event.toolInput ?? null;
249
+ const tr = event.tool_response ?? event.toolResponse ?? null;
250
+
251
+ if (toolName === 'Bash') {
252
+ const command = ti?.command ?? '';
253
+ const stdout = readContentField(tr) ?? '';
254
+ return command ? (stdout ? `$ ${command}\n${stdout}` : `$ ${command}`) : (stdout ?? '');
255
+ }
256
+
257
+ if (toolName === 'Write' || toolName === 'NotebookEdit') {
258
+ const filePath = ti?.file_path ?? ti?.notebook_path ?? '<unknown>';
259
+ const content = ti?.content ?? ti?.new_source ?? '';
260
+ const len = typeof content === 'string' ? content.length : 0;
261
+ return `wrote ${filePath} (${len} bytes)`;
262
+ }
263
+
264
+ if (toolName === 'Edit') {
265
+ const filePath = ti?.file_path ?? '<unknown>';
266
+ const oldStr = ti?.old_string ?? '';
267
+ const newStr = ti?.new_string ?? '';
268
+ return `edit ${filePath}\nOLD:\n${oldStr}\nNEW:\n${newStr}`;
269
+ }
270
+
271
+ if (toolName === 'MultiEdit') {
272
+ const filePath = ti?.file_path ?? '<unknown>';
273
+ const edits = Array.isArray(ti?.edits) ? ti.edits : [];
274
+ const parts = edits.map((e, i) => `[${i}] OLD:\n${e?.old_string ?? ''}\nNEW:\n${e?.new_string ?? ''}`);
275
+ return `multi-edit ${filePath}\n${parts.join('\n---\n')}`;
276
+ }
277
+
278
+ if (toolName === 'Read') {
279
+ const filePath = ti?.file_path ?? '<unknown>';
280
+ const content = readContentField(tr) ?? '';
281
+ const truncated = content.length > MAX_CAPTURED_CHARS / 2
282
+ ? content.slice(0, MAX_CAPTURED_CHARS / 2)
283
+ : content;
284
+ return `read ${filePath}\n${truncated}`;
285
+ }
286
+
287
+ // Generic fallback: tool_response content/text or tool_input content.
288
+ const fromResponse = readContentField(tr);
289
+ if (typeof fromResponse === 'string' && fromResponse.length > 0) return fromResponse;
290
+ if (ti && typeof ti === 'object') {
291
+ if (typeof ti.content === 'string') return ti.content;
292
+ if (typeof ti.command === 'string') return ti.command;
293
+ }
294
+ return '';
295
+ }
296
+
297
+ /**
298
+ * Read a content payload from a tool_response-shaped object. Handles:
299
+ * - string (return as-is)
300
+ * - { content: [{text}, ...] } MCP-style array (concat text blocks)
301
+ * - { text: string }
302
+ * - { content: string }
303
+ */
304
+ function readContentField(obj) {
305
+ if (!obj) return null;
306
+ if (typeof obj === 'string') return obj;
307
+ if (Array.isArray(obj.content)) {
308
+ return obj.content
309
+ .map((c) => (typeof c?.text === 'string' ? c.text : ''))
310
+ .join('\n');
311
+ }
312
+ if (typeof obj.text === 'string') return obj.text;
313
+ if (typeof obj.content === 'string') return obj.content;
314
+ return null;
315
+ }
316
+
317
+ async function main() {
318
+ let event = {};
319
+ try {
320
+ event = JSON.parse(readFileSync(0, 'utf-8'));
321
+ } catch {
322
+ return; // unparseable stdin → fail-open
323
+ }
324
+
325
+ const dir = queueDir();
326
+ ensureQueueDir(dir);
327
+ const depth = queueDepth(dir);
328
+
329
+ if (depth >= QUEUE_DROP_THRESHOLD) {
330
+ warnLog(`queue depth ${depth} >= drop threshold ${QUEUE_DROP_THRESHOLD}; dropping event tool_name=${event?.tool_name ?? '?'}`, dir);
331
+ return;
332
+ }
333
+
334
+ const candidate = buildCandidate(event, depth);
335
+ writeCandidate(candidate, dir);
336
+
337
+ if (depth >= QUEUE_FORCE_FLUSH_THRESHOLD) {
338
+ // Phase B2 will own the force-flush trigger via an out-of-band
339
+ // signal (touching a sentinel file the launchd timer watches).
340
+ // For B1 ship we just warn — the next scheduled flush picks it up.
341
+ warnLog(`queue depth ${depth} >= force-flush threshold ${QUEUE_FORCE_FLUSH_THRESHOLD}; awaiting B2 timer`, dir);
342
+ }
343
+ }
344
+
345
+ // Run main only when invoked directly (allows test files to import the
346
+ // pure functions above without firing the hook side-effects). Compare
347
+ // REAL paths (not raw argv vs file://) so the hook still fires when
348
+ // resolved through a symlink — toolbox sync mounts these scripts via
349
+ // $HOME/.claude/scripts/agentmemory-classifier/ which is a symlink to the
350
+ // canonical atlas-toolbox path. Pre-fix, the raw-string compare missed
351
+ // the symlink and main() never ran.
352
+ function isDirectInvocation() {
353
+ try {
354
+ const argvPath = process.argv[1] ? realpathSync(process.argv[1]) : '';
355
+ const metaPath = realpathSync(fileURLToPath(import.meta.url));
356
+ return argvPath === metaPath;
357
+ } catch {
358
+ return false;
359
+ }
360
+ }
361
+ if (isDirectInvocation()) {
362
+ main().catch(() => process.exit(0));
363
+ }
@@ -0,0 +1,233 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * agentmemory-recall-trigger.mjs
4
+ *
5
+ * PreToolUse hook for Claude Code — derives a prompt-context from the
6
+ * incoming tool event, POSTs to the bridge `/classify/recall` endpoint
7
+ * (Phase C2), and injects the top-3 results as additionalContext so
8
+ * the agent has relevant memory before executing the tool call.
9
+ *
10
+ * Hot-path SLO: p99 latency < 500ms (Phase A plan P1 row). The 500ms
11
+ * budget covers the bridge round-trip + the bridge → container
12
+ * memory_recall hop. Fail-open: any error exits 0 with no stdout —
13
+ * never blocks the tool loop.
14
+ *
15
+ * cwd → store routing (T4 mitigation):
16
+ * Derived from process.cwd() via the same regex pattern the existing
17
+ * SessionStart agentmemory-session-recall.js hook uses. The bridge
18
+ * URL itself is per-store (operators set AGENTMEMORY_BRIDGE_URL per
19
+ * project / per store) so cross-store leak is impossible from this
20
+ * hook — the bridge it talks to is single-store by configuration.
21
+ *
22
+ * Install (operator runs manually):
23
+ * $ cp packages/memory-service/hooks/agentmemory-recall-trigger.mjs \
24
+ * ~/.claude/scripts/hooks/agentmemory-recall-trigger.mjs
25
+ * $ packages/memory-service/scripts/install-recall-hook.sh
26
+ * # the install script does the cp + the settings.json edit + cp BACKUP first
27
+ *
28
+ * Plan reference:
29
+ * docs/plans/agentmemory-classifier-and-regret/phase-c-tasks.md C3
30
+ */
31
+
32
+ import { existsSync, readFileSync, realpathSync } from 'node:fs';
33
+ import { homedir } from 'node:os';
34
+ import { join } from 'node:path';
35
+ import { fileURLToPath } from 'node:url';
36
+
37
+ // Built-in tool-event filter: only fire on tool calls where recall has
38
+ // signal. Bash + Edit + MultiEdit + Read on substantive paths qualify;
39
+ // TodoWrite + Glob + Grep are too lightweight to justify the recall
40
+ // round-trip.
41
+ const RECALL_WORTHY_TOOL_NAMES = new Set([
42
+ 'Bash', 'Edit', 'MultiEdit', 'Write', 'Read', 'NotebookEdit',
43
+ ]);
44
+
45
+ // Default to the LIVE memory-service Worker — the same host the remember/write
46
+ // path targets, so recall reads where the fleet writes. The prior default
47
+ // (olam-agent-memory.ernestcodes.workers.dev) is a dead host (HTTP 404), which
48
+ // made recall reach nothing. env still overrides for per-store / wrangler-dev.
49
+ // env wins → `olam memory connect` artifact → live default. See
50
+ // packages/cli/src/lib/memory-connection.ts (keep precedence in sync).
51
+ function resolveBridgeUrlDefault() {
52
+ if (process.env.AGENTMEMORY_BRIDGE_URL) return process.env.AGENTMEMORY_BRIDGE_URL;
53
+ try {
54
+ const conn = JSON.parse(
55
+ readFileSync(join(homedir(), '.olam', 'memory-connection.json'), 'utf8'),
56
+ );
57
+ if (conn && typeof conn.url === 'string' && conn.url) return conn.url;
58
+ } catch {
59
+ // artifact absent / malformed — fall through
60
+ }
61
+ return 'https://atlas-agent-memory.atlas-kitchen.workers.dev';
62
+ }
63
+ const BRIDGE_URL = resolveBridgeUrlDefault();
64
+
65
+ /**
66
+ * Resolve a secret file path: prefers ~/.olam/secrets/<name>,
67
+ * falls back to ~/.olam/<name>. Inlined here because this .mjs hook
68
+ * has no @olam/core dep.
69
+ */
70
+ function resolveSecretPathInline(name) {
71
+ const olamHome = join(homedir(), '.olam');
72
+ const newPath = join(olamHome, 'secrets', name);
73
+ if (existsSync(newPath)) return newPath;
74
+ return join(olamHome, name);
75
+ }
76
+
77
+ /** Read a trimmed secret file by name, or '' if absent/unreadable. */
78
+ function readSecretFile(name) {
79
+ try {
80
+ return readFileSync(resolveSecretPathInline(name), 'utf8').trim();
81
+ } catch {
82
+ return '';
83
+ }
84
+ }
85
+
86
+ // Bearer resolution (first match wins; no literal embedded):
87
+ // env AGENTMEMORY_BRIDGE_SECRET → env OLAM_AGENT_MEMORY_BEARER
88
+ // → file bridge-secret → file agent-memory-bearer → '' (no-op fail-open).
89
+ // Empty string short-circuits callBridge() to a fail-open no-op so the hook
90
+ // never blocks the tool loop on a missing secret.
91
+ const BEARER =
92
+ process.env.AGENTMEMORY_BRIDGE_SECRET ||
93
+ process.env.OLAM_AGENT_MEMORY_BEARER ||
94
+ readSecretFile('bridge-secret') ||
95
+ readSecretFile('agent-memory-bearer') ||
96
+ '';
97
+ const RECALL_TIMEOUT_MS = 1500;
98
+
99
+ /**
100
+ * Derive the recall prompt from a Claude Code PreToolUse event. Each
101
+ * tool gets a tailored prompt shape:
102
+ * Bash → `$ <command>`
103
+ * Edit → `edit <file_path>: <truncated new_string>`
104
+ * MultiEdit → `multi-edit <file_path>`
105
+ * Write → `wrote <file_path>`
106
+ * Read → `read <file_path>`
107
+ * default → JSON.stringify(tool_input).slice(0, 200)
108
+ */
109
+ export function derivePrompt(event) {
110
+ if (!event || typeof event !== 'object') return '';
111
+ const toolName = event.tool_name;
112
+ const ti = event.tool_input ?? event.toolInput ?? null;
113
+ if (!RECALL_WORTHY_TOOL_NAMES.has(toolName)) return '';
114
+
115
+ if (toolName === 'Bash') {
116
+ const cmd = ti?.command;
117
+ return typeof cmd === 'string' ? `$ ${cmd}` : '';
118
+ }
119
+ if (toolName === 'Edit') {
120
+ const fp = ti?.file_path ?? '';
121
+ const ns = typeof ti?.new_string === 'string'
122
+ ? ti.new_string.slice(0, 200)
123
+ : '';
124
+ return `edit ${fp}: ${ns}`;
125
+ }
126
+ if (toolName === 'MultiEdit') {
127
+ return `multi-edit ${ti?.file_path ?? ''}`;
128
+ }
129
+ if (toolName === 'Write') {
130
+ return `wrote ${ti?.file_path ?? ''}`;
131
+ }
132
+ if (toolName === 'Read') {
133
+ return `read ${ti?.file_path ?? ''}`;
134
+ }
135
+ if (toolName === 'NotebookEdit') {
136
+ return `edit ${ti?.notebook_path ?? ''}`;
137
+ }
138
+ return '';
139
+ }
140
+
141
+ /**
142
+ * Call the bridge /classify/recall endpoint. Returns the parsed
143
+ * response body or null on any failure (fail-open).
144
+ */
145
+ async function callBridge(prompt, cwd) {
146
+ if (!prompt || !BRIDGE_URL || !BEARER) return null;
147
+ try {
148
+ const res = await fetch(`${BRIDGE_URL}/classify/recall`, {
149
+ method: 'POST',
150
+ headers: {
151
+ Authorization: `Bearer ${BEARER}`,
152
+ 'content-type': 'application/json',
153
+ },
154
+ body: JSON.stringify({ prompt, cwd, limit: 3 }),
155
+ signal: AbortSignal.timeout(RECALL_TIMEOUT_MS),
156
+ });
157
+ if (!res.ok) return null;
158
+ return await res.json();
159
+ } catch (err) {
160
+ // Fail-open: one-line breadcrumb to stderr, then null (no output, no block).
161
+ process.stderr.write(`[agentmemory-recall-trigger] recall failed: ${err?.message ?? err}\n`);
162
+ return null;
163
+ }
164
+ }
165
+
166
+ /**
167
+ * Format the recall response as a markdown additionalContext block
168
+ * for hookSpecificOutput. Returns empty string when no results so the
169
+ * hook emits no output and the agent sees no noise.
170
+ */
171
+ export function formatAdditionalContext(response, prompt) {
172
+ if (!response || !Array.isArray(response.results) || response.results.length === 0) {
173
+ return '';
174
+ }
175
+ const lines = [
176
+ `## Recalled from agent memory (prompt: "${prompt.slice(0, 80)}${prompt.length > 80 ? '…' : ''}")`,
177
+ '',
178
+ ];
179
+ for (const r of response.results) {
180
+ const title = r.title || r.id || '(untitled)';
181
+ const score = typeof r.score === 'number' ? r.score.toFixed(1) : '?';
182
+ const conv = typeof r.conviction === 'number' ? r.conviction : '?';
183
+ const sim = typeof r.similarity === 'number' ? r.similarity.toFixed(2) : '?';
184
+ lines.push(`- [score ${score} | conv ${conv} | sim ${sim}] ${title}`);
185
+ if (r.contentExcerpt) {
186
+ lines.push(` ${r.contentExcerpt}`);
187
+ }
188
+ }
189
+ return lines.join('\n');
190
+ }
191
+
192
+ async function main() {
193
+ let event = {};
194
+ try {
195
+ event = JSON.parse(readFileSync(0, 'utf-8'));
196
+ } catch {
197
+ return; // unparseable stdin → fail-open
198
+ }
199
+
200
+ const prompt = derivePrompt(event);
201
+ if (!prompt) return;
202
+
203
+ const response = await callBridge(prompt, event.cwd ?? process.cwd());
204
+ const additionalContext = formatAdditionalContext(response, prompt);
205
+ if (!additionalContext) return;
206
+
207
+ process.stdout.write(JSON.stringify({
208
+ hookSpecificOutput: {
209
+ hookEventName: 'PreToolUse',
210
+ additionalContext,
211
+ },
212
+ }));
213
+ }
214
+
215
+ // Run main only when invoked directly (allows test files to import the
216
+ // pure functions above without firing the hook side-effects). Compare
217
+ // REAL paths (not raw argv vs file://) so the hook still fires when
218
+ // resolved through a symlink — toolbox sync mounts these scripts via
219
+ // $HOME/.claude/scripts/agentmemory-classifier/ which is a symlink to the
220
+ // canonical atlas-toolbox path. Pre-fix, the raw-string compare missed
221
+ // the symlink and main() never ran.
222
+ function isDirectInvocation() {
223
+ try {
224
+ const argvPath = process.argv[1] ? realpathSync(process.argv[1]) : '';
225
+ const metaPath = realpathSync(fileURLToPath(import.meta.url));
226
+ return argvPath === metaPath;
227
+ } catch {
228
+ return false;
229
+ }
230
+ }
231
+ if (isDirectInvocation()) {
232
+ main().catch(() => process.exit(0));
233
+ }