Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Rust
fix(capture): close remaining ignore_paths gaps in both matchers
AkitaOnRails0c32fcad47d1
diff --git a/crates/ai-memory-cli/src/commands/hook.rs b/crates/ai-memory-cli/src/commands/hook.rsindex 355bcd2b..8247171c 100644--- a/crates/ai-memory-cli/src/commands/hook.rs+++ b/crates/ai-memory-cli/src/commands/hook.rs@@ -3164,6 +3164,123 @@ mod tests {}}+ /// Calls that used to carry an excluded file's content into the spool: a+ /// Gemini `read_many_files` directory entry (read as `entry/**`), Copilot+ /// CLI's runtime `view`/`create`, tools reading back an earlier command's+ /// output, and a shell word with brace expansion or a bracket class. Each+ /// attempt is dropped or stripped to metadata before the spool; the+ /// public controls still spool with their content.+ #[tokio::test]+ async fn excluded_content_cannot_reach_the_spool_through_unevaluated_tool_shapes() {+ let cases = [+ (+ "gemini-cli",+ "read_many_files",+ serde_json::json!({"include": ["docs"]}),+ None,+ ),+ (+ "gemini-cli",+ "read_many_files",+ serde_json::json!({"include": ["."]}),+ None,+ ),+ (+ "gemini-cli",+ "read_many_files",+ serde_json::json!({"include": ["src"]}),+ Some("keep"),+ ),+ (+ "copilot-cli",+ "view",+ serde_json::json!({"path": "docs/adr/0001.md"}),+ None,+ ),+ (+ "copilot-cli",+ "create",+ serde_json::json!({"path": "docs/adr/0002.md", "file_text": "x"}),+ None,+ ),+ (+ "copilot-cli",+ "view",+ serde_json::json!({"path": "src/lib.rs"}),+ Some("keep"),+ ),+ (+ "copilot-cli",+ "read_bash",+ serde_json::json!({"shellId": "s1"}),+ Some("metadata-only"),+ ),+ (+ "claude-code",+ "BashOutput",+ serde_json::json!({"bash_id": "b1"}),+ Some("metadata-only"),+ ),+ (+ "claude-code",+ "TaskOutput",+ serde_json::json!({"task_id": "b1"}),+ Some("metadata-only"),+ ),+ (+ "claude-code",+ "Bash",+ serde_json::json!({"command": "cat docs/{adr,x}/0001.md"}),+ Some("metadata-only"),+ ),+ (+ "claude-code",+ "Bash",+ serde_json::json!({"command": "cat docs/ad[r]/0001.md"}),+ Some("metadata-only"),+ ),+ (+ "claude-code",+ "Bash",+ serde_json::json!({"command": "cat 'docs/{adr,x}/0001.md'"}),+ Some("keep"),+ ),+ ];+ for (agent, tool, input, expected) in cases {+ let tmp = tempfile::tempdir().unwrap();+ std::fs::write(+ tmp.path().join(".ai-memory.toml"),+ "[capture]\nignore_paths = [\"docs/adr/**\"]\n",+ )+ .unwrap();+ let data_dir = tmp.path().join("data");+ let raw = serde_json::json!({+ "session_id": "gaps", "cwd": tmp.path(),+ "tool_name": tool, "tool_input": input,+ "tool_response": {"content": "SENTINEL_CONTENT"},+ "tool_result": {"result_type": "success", "text_result_for_llm": "SENTINEL_CONTENT"},+ });+ let entries = spool_one(&data_dir, agent, None, &raw).await;+ let label = format!("{agent} {tool} {input}");+ match expected {+ None => assert!(entries.is_empty(), "{label}: must not spool"),+ Some(disposition) => {+ assert_eq!(entries.len(), 1, "{label}");+ let body: serde_json::Value = serde_json::from_str(&entries[0].body).unwrap();+ assert_eq!(+ body["_ai_memory_capture"]["disposition"], disposition,+ "{label}"+ );+ assert_eq!(+ entries[0].body.contains("SENTINEL_CONTENT"),+ disposition == "keep",+ "{label}"+ );+ }+ }+ }+ }+#[tokio::test]async fn codex_native_capture_policy_runs_before_spool_and_preserves_identity() {for (tool, input, disposition) in [diff --git a/crates/ai-memory-cli/src/commands/render_shared.rs b/crates/ai-memory-cli/src/commands/render_shared.rsindex 0a41d95d..b446df01 100644--- a/crates/ai-memory-cli/src/commands/render_shared.rs+++ b/crates/ai-memory-cli/src/commands/render_shared.rs@@ -321,7 +321,10 @@ function captureConfig(cwd: string | undefined): CaptureConfig {return { state: "invalid", patterns: [], base: candidateBase, windowsHost };}}-function captureGlob(pattern: string, candidate: string, insensitive: boolean, budget: { work: number }): boolean | undefined { const p = [...pattern]; const c = [...candidate]; const previous = new Array<boolean>(p.length + 1).fill(false); previous[0] = true; for (let j = 1; j <= p.length; j++) previous[j] = p[j - 1] === "*" && p[j] !== "*" && previous[j - 1]; for (const ch of c) { const current = new Array<boolean>(p.length + 1).fill(false); for (let j = 1; j <= p.length; j++) { if (++budget.work > CAPTURE_MAX_WORK) return undefined; const x = p[j - 1]; current[j] = x === "*" && p[j] === "*" ? false : x === "*" && j >= 2 && p[j - 2] === "*" ? current[j - 2] || previous[j] : x === "*" ? current[j - 1] || (ch !== "/" && previous[j]) : x === "?" ? ch !== "/" && previous[j - 1] : captureCharEq(x, ch, insensitive) && previous[j - 1]; } for (let j = 0; j <= p.length; j++) previous[j] = current[j]; } return previous[p.length]; }+function captureGlob(pattern: string, candidate: string, insensitive: boolean, budget: { work: number }): boolean | undefined { const states = captureGlobStates(pattern, candidate, insensitive, budget); return states && states[states.length - 1]; }+// Whether `pattern` matches some path strictly below `directory`: a prefix still alive after `directory/` can always be completed.+function captureGlobBelow(pattern: string, directory: string, insensitive: boolean, budget: { work: number }): boolean | undefined { const states = captureGlobStates(pattern, directory.endsWith("/") ? directory : `${directory}/`, insensitive, budget); return states && states.some(Boolean); }+function captureGlobStates(pattern: string, candidate: string, insensitive: boolean, budget: { work: number }): boolean[] | undefined { const p = [...pattern]; const c = [...candidate]; const previous = new Array<boolean>(p.length + 1).fill(false); previous[0] = true; for (let j = 1; j <= p.length; j++) previous[j] = p[j - 1] === "*" && p[j] !== "*" && previous[j - 1]; for (const ch of c) { const current = new Array<boolean>(p.length + 1).fill(false); for (let j = 1; j <= p.length; j++) { if (++budget.work > CAPTURE_MAX_WORK) return undefined; const x = p[j - 1]; current[j] = x === "*" && p[j] === "*" ? false : x === "*" && j >= 2 && p[j - 2] === "*" ? current[j - 2] || previous[j] : x === "*" ? current[j - 1] || (ch !== "/" && previous[j]) : x === "?" ? ch !== "/" && previous[j - 1] : captureCharEq(x, ch, insensitive) && previous[j - 1]; } for (let j = 0; j <= p.length; j++) previous[j] = current[j]; } return previous; }function captureCharEq(a: string, b: string, insensitive: boolean): boolean { return insensitive && a.charCodeAt(0) < 128 && b.charCodeAt(0) < 128 ? a.toLowerCase() === b.toLowerCase() : a === b; }function captureLiteralPrefix(path: string): string { const glob = path.search(/[*?]/); if (glob < 0) return path; const slash = path.lastIndexOf("/", glob); if (slash < 0) return ""; const head = path.slice(0, slash + 1); const trimmed = head.slice(0, -1); return trimmed && !trimmed.endsWith(":") ? trimmed : head; }function captureStartsWith(path: string, prefix: string, insensitive: boolean): boolean { const p = path[Symbol.iterator](); for (const x of prefix) { const c = p.next(); if (c.done || !captureCharEq(x, c.value, insensitive)) return false; } return true; }@@ -333,6 +336,8 @@ function captureShellCommand(args: Record<string, unknown> | undefined): string// Split only when a policy is active: this runs for every tool event.function captureShellWordList(command: string | string[]): string[] { if (typeof command === "string") return captureShellWords(command); return command.flatMap((item) => { const tokens = captureShellWords(item); return tokens.length === 1 && tokens[0] === item || [...item].length > 256 ? tokens : [item, ...tokens]; }); }function captureShellWords(command: string): string[] { const special = (c: string) => /\s/.test(c) || "|&;<>()".includes(c); const chars = [...command]; const words: string[] = []; let word = ""; let inWord = false; let quote = ""; for (let i = 0; i < chars.length; i++) { const c = chars[i]; const next = chars[i + 1]; if (quote) { if (c === quote) quote = ""; else if (quote === '"' && c === "\\" && (next === '"' || next === "\\")) { word += next; i++; } else word += c; } else if (c === "'" || c === '"') { quote = c; inWord = true; } else if (c === "\\" && next !== undefined && (special(next) || next === "'" || next === '"' || next === "\\")) { word += next; i++; inWord = true; } else if (special(c)) { if (inWord) words.push(word); word = ""; inWord = false; } else { word += c; inWord = true; } } if (inWord) words.push(word); return words; }+// Whether an unquoted word holds a brace expansion (`{a,b}`, `{1..3}`) or a bracket class (`[ab]`), which lexical matching does not evaluate; mirrors `has_unevaluated_expansion`.+function captureUnevaluatedExpansion(command: string | string[]): boolean { if (typeof command !== "string") return command.some((item) => captureUnevaluatedExpansion(item)); const chars = [...command]; let quote = ""; let previous = ""; let parameter = false; let brace: boolean | undefined; let klass: number | undefined; for (let i = 0; i < chars.length; i++) { const c = chars[i]; if (quote) { if (c === quote) quote = ""; else if (quote === '"' && c === "\\") i++; if (klass !== undefined) klass++; previous = ""; continue; } if (c === "'" || c === '"') quote = c; else if (c === "\\") { i++; if (klass !== undefined) klass++; previous = ""; continue; } else if (/\s/.test(c) || "|&;<>()".includes(c)) { parameter = false; brace = undefined; klass = undefined; } else if (c === "{" && previous === "$") parameter = true; else if (c === "}" && parameter) parameter = false; else if (parameter) { /* parameter expansion */ } else if (c === "{") brace = false; else if (c === "," && brace !== undefined) brace = true; else if (c === "." && brace !== undefined && previous === ".") brace = true; else if (c === "}" && brace !== undefined) { if (brace) return true; brace = undefined; } else if (c === "[" && klass === undefined) klass = 0; else if (c === "]" && klass !== undefined) { if (klass > 0) return true; klass = undefined; } else if (klass !== undefined) klass++; previous = c; } return false; }function captureShellArguments(word: string): string[] { const flag = word.startsWith("-"); const out = flag ? [] : [word]; const eq = word.indexOf("="); const colon = flag && eq < 0 ? word.indexOf(":") : -1; if (eq >= 0) out.push(word.slice(eq + 1)); else if (colon >= 0) out.push(word.slice(colon + 1)); return out.filter((argument) => argument.trim() !== ""); }// Lexical only, like the native hook: nothing is expanded or executed, so// variables, command substitution, and `cd` state are not followed. A tool's@@ -343,9 +348,9 @@ function captureMatchCommand(command: string | string[], config: CaptureConfig,// drops when it could expand into an ignored path, and glob syntax the matcher// cannot evaluate (classes, braces, extglobs, negation) fails closed.function captureMatchGlobs(paths: string[], candidates: { path: string; windows: boolean }[], config: CaptureConfig): boolean | undefined { const budget = { work: 0 }; for (let i = 0; i < paths.length; i++) { if (/[\[\]{}()!]/.test(paths[i])) return undefined; const match = captureMatchCandidate(candidates[i], true, config, budget); if (match !== false) return match; } return false; }-function captureMatchCandidate(candidate: { path: string; windows: boolean }, toolGlob: boolean, config: CaptureConfig, budget: { work: number }): boolean | undefined { const glob = /[*?]/.test(candidate.path); for (const pattern of config.patterns) { if (candidate.windows !== pattern.windows) continue; const insensitive = toolGlob || pattern.windows; const under = captureStartsWith(candidate.path, pattern.prefix, insensitive); if (!under && !glob) continue; if (under) { const directory = pattern.directory ? captureGlob(pattern.directory, candidate.path, insensitive, budget) : false; if (directory !== false) return directory; const match = captureGlob(pattern.path, candidate.path, insensitive, budget); if (match !== false) return match; } if (glob) { const reaches = captureGlobstarReaches(candidate.path, pattern.prefix, insensitive, budget); if (reaches !== false) return reaches; } } return false; }+function captureMatchCandidate(candidate: { path: string; windows: boolean }, toolGlob: boolean, config: CaptureConfig, budget: { work: number }): boolean | undefined { const glob = /[*?]/.test(candidate.path); const directory = toolGlob && !glob; for (const pattern of config.patterns) { if (candidate.windows !== pattern.windows) continue; const insensitive = toolGlob || pattern.windows; const under = captureStartsWith(candidate.path, pattern.prefix, insensitive); if (!under && !glob && !directory) continue; if (under) { const directory = pattern.directory ? captureGlob(pattern.directory, candidate.path, insensitive, budget) : false; if (directory !== false) return directory; const match = captureGlob(pattern.path, candidate.path, insensitive, budget); if (match !== false) return match; } if (glob) { const reaches = captureGlobstarReaches(candidate.path, pattern.prefix, insensitive, budget); if (reaches !== false) return reaches; } if (directory) { const below = captureGlobBelow(pattern.path, candidate.path, insensitive, budget); if (below !== false) return below; } } return false; }function captureGlobstarReaches(glob: string, prefix: string, insensitive: boolean, budget: { work: number }): boolean | undefined { const star = glob.indexOf("**"); if (star < 0) return captureGlobReaches(glob, prefix, insensitive, budget); const slash = glob.lastIndexOf("/", star); const head = slash < 0 ? "" : glob.slice(0, slash); const depth = head.split("/").filter(Boolean).length; let seen = 0; let offset = 0; for (const part of prefix.split("/")) { if (seen === depth) break; offset += part.length; if (part) seen++; if (seen < depth) offset++; } if (seen < depth) return captureGlobReaches(glob, prefix, insensitive, budget); return captureGlob(head, prefix.slice(0, offset), insensitive, budget); }-function captureTool(payload: Record<string, unknown>): { family: CaptureProtocol["tool_family"]; paths?: string[]; extraction: CaptureProtocol["extraction_state"]; callID?: string; command?: string | string[]; shell?: boolean; workdir?: string; globs?: boolean } { const name = typeof payload.tool === "string" ? payload.tool.toLowerCase() : ""; const args = payload.args as Record<string, unknown> | undefined; const call = ["tool_use_id","toolUseId","tool_call_id","toolCallId","call_id","callId","callID"].map((k) => payload[k]).find((v): v is string => typeof v === "string" && /^[A-Za-z0-9_.-]{1,128}$/.test(v)); if (["search","grep","glob","find","list","ls","list_files","read_dir","list_dir","grep_search","search_files","find_by_name","list_directory","search_file_content"].includes(name)) return { family: "search-list", extraction: "not-applicable", callID: call }; if (["bash","shell","shell_command","exec","execute","run_command","web_search","search_web","manage_task","manage_subagents","terminal","execute_bash","execute_cmd","powershell","run_shell_command"].includes(name)) { const workdir = ["workdir","dir_path","directory"].map((k) => args?.[k]).find((v) => v !== undefined); return { family: "non-file", extraction: "extracted", callID: call, command: captureShellCommand(args), shell: name !== "web_search", workdir: typeof workdir === "string" && workdir.trim() ? workdir : undefined }; } if (!["read","write","edit","apply_patch","notebookedit","notebook_edit","create_file","delete_file","rename_file","move_file","multi_edit","multiedit","replace","replace_all","read_many_files"].includes(name)) return { family: "unknown", extraction: "extracted", callID: call }; const direct = (o: any): string[] | undefined => { if (!o || typeof o !== "object") return undefined; const r: string[] = []; for (const k of ["file_path","filePath","path","absolute_path","AbsolutePath","notebook_path","TargetFile"]) if (k in o) { if (typeof o[k] !== "string") return undefined; r.push(o[k]); } if ("paths" in o) { if (!Array.isArray(o.paths) || o.paths.some((x: unknown) => typeof x !== "string")) return undefined; r.push(...o.paths); } return r.length && r.length <= CAPTURE_MAX_CANDIDATES ? r : undefined; }; let paths = direct(args); if (["multi_edit","multiedit","replace_all"].includes(name)) { const entries = args?.edits ?? args?.replacements; if (!Array.isArray(entries) || !entries.length || entries.length > CAPTURE_MAX_CANDIDATES) paths = undefined; else { paths = paths ?? []; for (const entry of entries) { const more = direct(entry); if (!more || paths.length + more.length > CAPTURE_MAX_CANDIDATES) { paths = undefined; break; } paths.push(...more); } } } if (name === "read_many_files" && args && "include" in args) { const include = args.include; if (!Array.isArray(include) || include.some((x: unknown) => typeof x !== "string") || (paths?.length ?? 0) + include.length > CAPTURE_MAX_CANDIDATES) paths = undefined; else paths = [...(paths ?? []), ...(include as string[])]; } if (!paths || !paths.length || paths.some((p) => !p.trim() || [...p].length > CAPTURE_MAX_PATH_CHARS)) return { family: "file", extraction: "missing-or-malformed", callID: call }; return { family: "file", paths, extraction: "extracted", callID: call, globs: name === "read_many_files" }; }+function captureTool(payload: Record<string, unknown>): { family: CaptureProtocol["tool_family"]; paths?: string[]; extraction: CaptureProtocol["extraction_state"]; callID?: string; command?: string | string[]; shell?: boolean; workdir?: string; globs?: boolean; reader?: boolean } { const name = typeof payload.tool === "string" ? payload.tool.toLowerCase() : ""; const args = payload.args as Record<string, unknown> | undefined; const call = ["tool_use_id","toolUseId","tool_call_id","toolCallId","call_id","callId","callID"].map((k) => payload[k]).find((v): v is string => typeof v === "string" && /^[A-Za-z0-9_.-]{1,128}$/.test(v)); if (["search","grep","glob","find","list","ls","list_files","read_dir","list_dir","grep_search","search_files","find_by_name","list_directory","search_file_content"].includes(name)) return { family: "search-list", extraction: "not-applicable", callID: call }; if (["bash","shell","shell_command","exec","execute","run_command","web_search","search_web","manage_task","manage_subagents","terminal","execute_bash","execute_cmd","powershell","run_shell_command"].includes(name)) { const workdir = ["workdir","dir_path","directory"].map((k) => args?.[k]).find((v) => v !== undefined); return { family: "non-file", extraction: "extracted", callID: call, command: captureShellCommand(args), shell: name !== "web_search", workdir: typeof workdir === "string" && workdir.trim() ? workdir : undefined }; } if (!["read","write","edit","apply_patch","notebookedit","notebook_edit","create_file","delete_file","rename_file","move_file","multi_edit","multiedit","replace","replace_all","read_many_files"].includes(name)) return { family: "unknown", extraction: "extracted", callID: call, reader: ["bashoutput","taskoutput","write_stdin","process","read_bash","write_bash","read_powershell","write_powershell"].includes(name) }; const direct = (o: any): string[] | undefined => { if (!o || typeof o !== "object") return undefined; const r: string[] = []; for (const k of ["file_path","filePath","path","absolute_path","AbsolutePath","notebook_path","TargetFile"]) if (k in o) { if (typeof o[k] !== "string") return undefined; r.push(o[k]); } if ("paths" in o) { if (!Array.isArray(o.paths) || o.paths.some((x: unknown) => typeof x !== "string")) return undefined; r.push(...o.paths); } return r.length && r.length <= CAPTURE_MAX_CANDIDATES ? r : undefined; }; let paths = direct(args); if (["multi_edit","multiedit","replace_all"].includes(name)) { const entries = args?.edits ?? args?.replacements; if (!Array.isArray(entries) || !entries.length || entries.length > CAPTURE_MAX_CANDIDATES) paths = undefined; else { paths = paths ?? []; for (const entry of entries) { const more = direct(entry); if (!more || paths.length + more.length > CAPTURE_MAX_CANDIDATES) { paths = undefined; break; } paths.push(...more); } } } if (name === "read_many_files" && args && "include" in args) { const include = args.include; if (!Array.isArray(include) || include.some((x: unknown) => typeof x !== "string") || (paths?.length ?? 0) + include.length > CAPTURE_MAX_CANDIDATES) paths = undefined; else paths = [...(paths ?? []), ...(include as string[])]; } if (!paths || !paths.length || paths.some((p) => !p.trim() || [...p].length > CAPTURE_MAX_PATH_CHARS)) return { family: "file", extraction: "missing-or-malformed", callID: call }; return { family: "file", paths, extraction: "extracted", callID: call, globs: name === "read_many_files" }; }// An external lifecycle owner (`AI_MEMORY_CAPTURE_OWNER`, any value that is// non-empty after trimming) takes over capture for this process: the gate runs// before the capture-policy marker scan, so no disposition work, no marker read@@ -361,7 +366,7 @@ function captureOwnedExternally(): boolean { const owner = typeof process === "u// home the walk stops at home, outside it continues past the checkout root to an// organisation-level marker; an unreadable marker counts as a selection.function captureServerRouted(cwd: string | undefined): boolean { let dir = resolve(cwd ?? process.cwd()); const home = homedir(); let boundary: string | undefined; for (let probe = dir; ; probe = dirname(probe)) { if (probe === home) { boundary = home; break; } if (probe === dirname(probe)) break; } for (;;) { try { if (/^\s*server\s*=/m.test(readFileSync(join(dir, ".ai-memory.toml"), "utf8"))) return true; } catch (e) { if (!["ENOENT", "ENOTDIR", "EISDIR"].includes((e as { code?: string })?.code ?? "")) return true; } if (dir === boundary || dir === dirname(dir)) return false; dir = dirname(dir); } }-function capturePolicy(payload: Record<string, unknown>, cwd: string | undefined): { disposition: CaptureDisposition; protocol?: CaptureProtocol; payload: Record<string, unknown> } { if (captureOwnedExternally()) return { disposition: "drop", payload }; if (captureServerRouted(cwd)) return { disposition: "drop", payload }; const markerPresent = !!findMarker(cwd); if (CAPTURE_MODE === "allowlist" && !markerPresent) return { disposition: "drop", payload }; const config = captureConfig(cwd); const tool = captureTool(payload); let disposition: CaptureDisposition = "keep"; if (config.state === "invalid" && (tool.family === "file" || tool.shell)) disposition = "metadata-only"; else if (config.state === "active" && tool.family === "search-list") disposition = "drop"; else if (config.state === "active" && tool.family === "file") { if (!tool.paths) disposition = "metadata-only"; else { const candidates = tool.paths.map((p) => captureNormalize(/^(?:\/|\\\\|[A-Za-z]:[\\/])/.test(p) ? p : captureJoin(config.base, p), config.windowsHost)); if (candidates.some((p) => !p)) disposition = "metadata-only"; else if (tool.globs) { const match = captureMatchGlobs(tool.paths, candidates as { path: string; windows: boolean }[], config); if (match !== false) disposition = match ? "drop" : "metadata-only"; } else { const budget = { work: 0 }; captureMatch: for (const candidate of candidates as { path: string; windows: boolean }[]) for (const pattern of config.patterns) { if (candidate.windows !== pattern.windows) continue; if (pattern.directory && captureGlob(pattern.directory, candidate.path, pattern.windows, budget)) { disposition = "drop"; break captureMatch; } const match = captureGlob(pattern.path, candidate.path, pattern.windows, budget); if (match === undefined) { disposition = "metadata-only"; break; } if (match) { disposition = "drop"; break captureMatch; } } } } } else if (config.state === "active" && tool.family === "non-file" && tool.command !== undefined && captureMatchCommand(tool.command, config, tool.workdir) !== false) disposition = "drop"; if (config.state === "inactive") return { disposition, payload }; const protocol: CaptureProtocol = { version: CAPTURE_POLICY_V1, disposition, policy_state: config.state, tool_family: tool.family, path_count: tool.paths?.length ?? 0, extraction_state: tool.extraction }; if (disposition === "metadata-only") { const session = payload.sessionID ?? payload.sessionId ?? payload.session_id; const routing = typeof payload.cwd === "string" ? payload.cwd : cwd; return { disposition, protocol, payload: { ...(typeof session === "string" ? { session_id: session } : {}), ...(typeof routing === "string" ? { cwd: routing } : {}), tool_family: tool.family, tool_name: tool.family, ...(tool.callID ? { tool_call_id: tool.callID } : {}), _ai_memory_capture: protocol } }; } if (disposition === "keep") return { disposition, protocol, payload: { ...payload, _ai_memory_capture: protocol } }; return { disposition, protocol, payload }; }+function capturePolicy(payload: Record<string, unknown>, cwd: string | undefined): { disposition: CaptureDisposition; protocol?: CaptureProtocol; payload: Record<string, unknown> } { if (captureOwnedExternally()) return { disposition: "drop", payload }; if (captureServerRouted(cwd)) return { disposition: "drop", payload }; const markerPresent = !!findMarker(cwd); if (CAPTURE_MODE === "allowlist" && !markerPresent) return { disposition: "drop", payload }; const config = captureConfig(cwd); const tool = captureTool(payload); let disposition: CaptureDisposition = "keep"; if (config.state === "invalid" && (tool.family === "file" || tool.shell || tool.reader)) disposition = "metadata-only"; else if (config.state === "active" && tool.reader) disposition = "metadata-only"; else if (config.state === "active" && tool.family === "search-list") disposition = "drop"; else if (config.state === "active" && tool.family === "file") { if (!tool.paths) disposition = "metadata-only"; else { const candidates = tool.paths.map((p) => captureNormalize(/^(?:\/|\\\\|[A-Za-z]:[\\/])/.test(p) ? p : captureJoin(config.base, p), config.windowsHost)); if (candidates.some((p) => !p)) disposition = "metadata-only"; else if (tool.globs) { const match = captureMatchGlobs(tool.paths, candidates as { path: string; windows: boolean }[], config); if (match !== false) disposition = match ? "drop" : "metadata-only"; } else { const budget = { work: 0 }; captureMatch: for (const candidate of candidates as { path: string; windows: boolean }[]) for (const pattern of config.patterns) { if (candidate.windows !== pattern.windows) continue; if (pattern.directory && captureGlob(pattern.directory, candidate.path, pattern.windows, budget)) { disposition = "drop"; break captureMatch; } const match = captureGlob(pattern.path, candidate.path, pattern.windows, budget); if (match === undefined) { disposition = "metadata-only"; break; } if (match) { disposition = "drop"; break captureMatch; } } } } } else if (config.state === "active" && tool.family === "non-file" && tool.command !== undefined) { if (captureMatchCommand(tool.command, config, tool.workdir) !== false) disposition = "drop"; else if (captureUnevaluatedExpansion(tool.command)) disposition = "metadata-only"; } if (config.state === "inactive") return { disposition, payload }; const protocol: CaptureProtocol = { version: CAPTURE_POLICY_V1, disposition, policy_state: config.state, tool_family: tool.family, path_count: tool.paths?.length ?? 0, extraction_state: tool.extraction }; if (disposition === "metadata-only") { const session = payload.sessionID ?? payload.sessionId ?? payload.session_id; const routing = typeof payload.cwd === "string" ? payload.cwd : cwd; return { disposition, protocol, payload: { ...(typeof session === "string" ? { session_id: session } : {}), ...(typeof routing === "string" ? { cwd: routing } : {}), tool_family: tool.family, tool_name: tool.family, ...(tool.callID ? { tool_call_id: tool.callID } : {}), _ai_memory_capture: protocol } }; } if (disposition === "keep") return { disposition, protocol, payload: { ...payload, _ai_memory_capture: protocol } }; return { disposition, protocol, payload }; }"##;TEMPLATE.replace("__AI_MEMORY_CAPTURE_MODE__", capture_mode)}@@ -2585,8 +2590,24 @@ for (const vector of fixture.shell.vectors) {{const payload = JSON.parse(JSON.stringify(vector.payload ?? {{ tool: "bash", args: {{ command: vector.command }} }}).replaceAll("{{root}}", shellRoot));expectDecision(payload, cwd, vector.disposition, "non-file", "extracted", 0, `shell-${{vector.disposition}}: ${{JSON.stringify(vector)}}`);}}+// Gemini reads a `read_many_files` directory entry as `entry/**` (shared vectors).+const directoryRoot = marker(`[capture]\nignore_paths = ${{JSON.stringify(fixture.read_many_files_directories.ignore_paths)}}\n`);+for (const vector of fixture.read_many_files_directories.vectors) {{+ expectDecision({{ tool: "read_many_files", args: {{ include: vector.include }} }}, directoryRoot, vector.disposition, "file", "extracted", vector.include.length, `read-many-directory: ${{JSON.stringify(vector)}}`);+}}+// Output readers: stripped under a policy, untouched without one.+const reader = {{ tool: "process", args: {{ action: "log" }}, output: privateBody }};+check(capturePolicy(reader, "/no-marker").disposition === "keep", "reader-inactive-keep");+for (const cwd of [marker(active), marker('[capture')]) {{+ const result = capturePolicy(reader, cwd);+ check(result.disposition === "metadata-only" && result.protocol?.tool_family === "unknown", "reader-metadata-only");+ check(!JSON.stringify(result.payload).includes(privateBody), "reader-redaction");+}}const bash = (command: string) => ({{ tool: "bash", args: {{ command }} }});check(capturePolicy(bash("cat docs/adr/x.md"), "/no-marker").disposition === "keep", "shell-inactive-keep");+check(capturePolicy(bash("cat docs/{{adr,x}}/1.md"), "/no-marker").disposition === "keep", "shell-expansion-inactive-keep");+const shellExpansion = capturePolicy({{ ...bash("cat docs/{{adr,x}}/1.md"), output: privateBody }}, shellRoot);+check(shellExpansion.disposition === "metadata-only" && !JSON.stringify(shellExpansion.payload).includes(privateBody), "shell-expansion-redaction");const shellInvalid = capturePolicy({{ ...bash(`cat ${{privatePath}}`), output: privateBody }}, marker('[capture'));check(shellInvalid.disposition === "metadata-only" && shellInvalid.protocol?.policy_state === "invalid" && shellInvalid.protocol?.tool_family === "non-file", "shell-invalid-metadata-only");for (const forbidden of [privatePath, privateBody]) check(!JSON.stringify(shellInvalid.payload).includes(forbidden), "shell-invalid-redaction");diff --git a/crates/ai-memory-hooks/src/capture_policy.rs b/crates/ai-memory-hooks/src/capture_policy.rsindex 50ef3b27..4410f849 100644--- a/crates/ai-memory-hooks/src/capture_policy.rs+++ b/crates/ai-memory-hooks/src/capture_policy.rs@@ -247,7 +247,7 @@ pub(crate) fn tool_observation_metadata(.is_some(),};has_args.then(|| ToolObservationMetadata {- tool_family: family(name),+ tool_family: agent_family(agent, name),tool_call_id: id.filter(|id| valid_call_id(id)).map(str::to_owned),})}@@ -549,10 +549,20 @@ impl CapturePolicy {// A broken marker cannot prove a shell command's arguments miss// every ignored path, so it fails closed like a file tool, readable// or not.- PolicyState::Invalid if extracted.family == ToolFamily::File || extracted.shell => {+ PolicyState::Invalid+ if extracted.family == ToolFamily::File+ || extracted.shell+ || extracted.output_reader =>+ {(CaptureDisposition::MetadataOnly, extracted.state)}PolicyState::Invalid => (CaptureDisposition::Keep, extracted.state),+ // A tool that reads back an earlier command's output names no+ // path of its own, so nothing proves that command missed every+ // ignored path.+ PolicyState::Active if extracted.output_reader => {+ (CaptureDisposition::MetadataOnly, extracted.state)+ }PolicyState::Active => match extracted.family {ToolFamily::SearchList => {(CaptureDisposition::Drop, ExtractionState::NotApplicable)@@ -600,9 +610,13 @@ impl CapturePolicy {_ => cwd.to_owned(),};self.match_command(&command.words(), &base)+ .map(|matched| (matched, command.has_unevaluated_expansion()))}) {- Some(Ok(true) | Err(())) => (CaptureDisposition::Drop, extracted.state),- Some(Ok(false)) | None => (CaptureDisposition::Keep, extracted.state),+ Some(Ok((true, _)) | Err(())) => (CaptureDisposition::Drop, extracted.state),+ // Brace expansion and bracket classes are not evaluated,+ // so such a word proves nothing either way.+ Some(Ok((false, true))) => (CaptureDisposition::MetadataOnly, extracted.state),+ Some(Ok((false, false))) | None => (CaptureDisposition::Keep, extracted.state),},_ => (CaptureDisposition::Keep, extracted.state),},@@ -706,6 +720,9 @@ impl CapturePolicy {) -> Result<bool, ()> {let insensitive = tool_glob || candidate.flavor == Flavor::Windows;let is_glob = candidate.path.contains(['*', '?']);+ // `read_many_files` reads a directory entry (`docs`, `.`) as+ // `entry/**`, and nothing here can tell a directory from a file.+ let directory = tool_glob && !is_glob;for pattern in self.patterns.iter()@@ -715,11 +732,18 @@ impl CapturePolicy {// quadratic matches below are charged to the budget.let under_prefix =starts_with_chars(&candidate.path, &pattern.literal_prefix, insensitive);- if !under_prefix && !is_glob {+ if !under_prefix && !is_glob && !directory {continue;}*work = work.checked_add(pattern.match_cost(&candidate.path))+ .and_then(|work| {+ work.checked_add(if directory {+ pattern.match_cost(&candidate.path)+ } else {+ 0+ })+ }).ok_or(())?;if *work > MAX_MATCH_WORK {return Err(());@@ -736,6 +760,7 @@ impl CapturePolicy {insensitive,))|| (is_glob && reaches(&candidate.path, &pattern.literal_prefix, insensitive))+ || (directory && glob_matches_below(&pattern.path, &candidate.path, insensitive)){return Ok(true);}@@ -783,6 +808,8 @@ struct Extracted<'a> {search_paths: Option<Vec<String>>,/// The paths are globs (Gemini CLI `read_many_files`), not literal files.globs: bool,+ /// The tool reads back output of an earlier command (`BashOutput`).+ output_reader: bool,call_id: Option<String>,state: ExtractionState,}@@ -840,7 +867,7 @@ fn extract(agent: AgentKind, raw: &Value) -> Extracted<'_> {let Some((name, args)) = extracted else {return Extracted::default();};- let family = family(name);+ let family = agent_family(agent, name);let paths = (family == ToolFamily::File).then(|| args.and_then(|value| extract_paths(name, value))).flatten();@@ -895,11 +922,47 @@ fn extract(agent: AgentKind, raw: &Value) -> Extracted<'_> {workdir,search_paths,globs: name.eq_ignore_ascii_case("read_many_files"),+ output_reader: reads_earlier_output(name),call_id,state,}}+/// [`family`] plus tool names only one agent is documented to use, so a+/// same-named tool of another agent keeps its current classification.+fn agent_family(agent: AgentKind, name: &str) -> ToolFamily {+ if agent == AgentKind::CopilotCli {+ // GitHub Copilot CLI's runtime tool names: `view` reads a file or+ // directory, `create` writes one, `str_replace_editor` edits, each+ // naming its target in `path`; `rg` is its `grep` alias.+ match name.to_ascii_lowercase().as_str() {+ "view" | "create" | "str_replace_editor" => return ToolFamily::File,+ "rg" => return ToolFamily::SearchList,+ _ => {}+ }+ }+ family(name)+}++/// Tools that return output of a command started by an earlier call: Claude+/// Code `BashOutput`/`TaskOutput`, Codex `write_stdin`, Hermes and OpenClaw+/// `process`, Copilot CLI `read_bash`/`write_bash` and their PowerShell twins.+/// That command's own event may have been dropped, so its output must not+/// come back through these.+fn reads_earlier_output(name: &str) -> bool {+ matches!(+ name.to_ascii_lowercase().as_str(),+ "bashoutput"+ | "taskoutput"+ | "write_stdin"+ | "process"+ | "read_bash"+ | "write_bash"+ | "read_powershell"+ | "write_powershell"+ )+}+fn family(name: &str) -> ToolFamily {match name.to_ascii_lowercase().as_str() {"read"@@ -1135,6 +1198,78 @@ impl ShellCommand<'_> {}}}++ /// Whether any word uses brace expansion or a bracket class, which the+ /// lexical matcher does not evaluate. Each argv element is scanned as a+ /// script too, the same over-approximation [`Self::words`] makes.+ fn has_unevaluated_expansion(&self) -> bool {+ match self {+ Self::Line(command) => has_unevaluated_expansion(command),+ Self::Argv(items) => items.iter().any(|item| has_unevaluated_expansion(item)),+ }+ }+}++/// Whether an unquoted word of `command` holds a brace expansion (`{a,b}`,+/// `{1..3}`) or a bracket class (`[ab]`). Quoted or escaped braces and+/// brackets are literal, `${…}` is a parameter expansion, and `{}` or a+/// lone `[` (`find -exec … {}`, `[ -f x ]`) expands nothing.+fn has_unevaluated_expansion(command: &str) -> bool {+ let is_operator = |c: char| matches!(c, '|' | '&' | ';' | '<' | '>' | '(' | ')');+ let mut quote = None;+ let mut previous = None;+ let mut parameter = false;+ let mut brace = None::<bool>;+ let mut class = None::<usize>;+ let mut chars = command.chars();+ while let Some(c) = chars.next() {+ if let Some(open) = quote {+ if c == open {+ quote = None;+ } else if open == '"' && c == '\\' {+ chars.next();+ }+ class = class.map(|len| len + 1);+ previous = None;+ continue;+ }+ match c {+ '\'' | '"' => quote = Some(c),+ '\\' => {+ chars.next();+ class = class.map(|len| len + 1);+ previous = None;+ continue;+ }+ c if c.is_whitespace() || is_operator(c) => {+ parameter = false;+ brace = None;+ class = None;+ }+ '{' if previous == Some('$') => parameter = true,+ '}' if parameter => parameter = false,+ _ if parameter => {}+ '{' => brace = Some(false),+ ',' if brace.is_some() => brace = Some(true),+ '.' if brace.is_some() && previous == Some('.') => brace = Some(true),+ '}' if brace.is_some() => {+ if brace == Some(true) {+ return true;+ }+ brace = None;+ }+ '[' if class.is_none() => class = Some(0),+ ']' if class.is_some() => {+ if class > Some(0) {+ return true;+ }+ class = None;+ }+ _ => class = class.map(|len| len + 1),+ }+ previous = Some(c);+ }+ false}fn shell_command(args: &Value) -> Option<ShellCommand<'_>> {@@ -1452,6 +1587,27 @@ fn glob_match(if directory_base.is_some_and(|base| glob_match(base, candidate, None, insensitive)) {return true;}+ glob_states(pattern, candidate, insensitive)+ .last()+ .copied()+ .unwrap_or(false)+}+/// Whether `pattern` matches some path strictly below `directory`. A pattern+/// prefix still alive after `directory/` can always be completed by spelling+/// out its remaining literals.+fn glob_matches_below(pattern: &str, directory: &str, insensitive: bool) -> bool {+ let directory = if directory.ends_with('/') {+ directory.to_owned()+ } else {+ format!("{directory}/")+ };+ glob_states(pattern, &directory, insensitive)+ .iter()+ .any(|alive| *alive)+}+/// For each prefix length of `pattern`, whether that prefix matches all of+/// `candidate`.+fn glob_states(pattern: &str, candidate: &str, insensitive: bool) -> Vec<bool> {let pattern: Vec<char> = pattern.chars().collect();let candidate: Vec<char> = candidate.chars().collect();let mut previous = vec![false; pattern.len() + 1];@@ -1478,7 +1634,7 @@ fn glob_match(}previous = current;}- previous[pattern.len()]+ previous}/// The whole leading segments of a normalized pattern that contain no glob.fn literal_prefix(path: &str) -> &str {@@ -2752,6 +2908,157 @@ mod tests {);}+ /// Gemini CLI reads a directory entry of `read_many_files` as+ /// `entry/**`, so an entry naming an ancestor of an ignored directory+ /// (`docs`, `.`) reads the ignored files. The shared vectors attempt it+ /// through both payload shapes; `src` and `README.md` are the controls.+ #[test]+ fn read_many_files_directory_entries_cannot_reach_ignored_paths() {+ let fixture: Value =+ serde_json::from_str(include_str!("../tests/fixtures/capture-policy.json")).unwrap();+ let section = &fixture["read_many_files_directories"];+ let ignore_paths = section["ignore_paths"]+ .as_array()+ .unwrap()+ .iter()+ .map(|pattern| pattern.as_str().unwrap().to_owned())+ .collect();+ let policy = CapturePolicy::resolve(+ CaptureSource::Parsed(&CaptureConfig { ignore_paths }),+ "/repo",+ None,+ );+ for vector in section["vectors"].as_array().unwrap() {+ let include = &vector["include"];+ for (agent, raw) in [+ (+ AgentKind::GeminiCli,+ json!({"tool_name": "read_many_files", "tool_input": {"include": include}}),+ ),+ (+ AgentKind::OpenCode,+ json!({"tool": "read_many_files", "args": {"include": include}}),+ ),+ ] {+ let protocol = policy.inspect(agent, &raw, "/repo").protocol().clone();+ assert_eq!(+ serde_json::to_value(protocol.disposition()).unwrap(),+ vector["disposition"],+ "{raw}"+ );+ assert_eq!(protocol.tool_family(), ToolFamily::File);+ }+ }+ assert!(glob_matches_below("/repo/docs/adr/**", "/repo", false));+ assert!(glob_matches_below("/**", "/", false));+ assert!(glob_matches_below("/repo/**/*.md", "/repo/src", false));+ assert!(!glob_matches_below("/repo/*.md", "/repo/README.md", false));+ assert!(!glob_matches_below("/repo/docs/adr/**", "/repo/src", false));+ assert!(glob_matches_below("C:/Docs/**", "c:/docs", true));+ }++ /// Tools that read back an earlier command's output are stripped to+ /// metadata under an active or invalid policy, since the command they+ /// read may have been dropped; with no policy they are untouched.+ #[test]+ fn output_reader_tools_are_metadata_only_only_under_a_policy() {+ let reader = json!({+ "tool_name": "BashOutput", "tool_input": {"bash_id": "b1"},+ "tool_response": "PRIVATE_BODY_SENTINEL",+ });+ let active = shell_policy();+ let invalid = CapturePolicy::resolve(CaptureSource::Invalid, "/repo", None);+ let inactive = CapturePolicy::resolve(CaptureSource::Absent, "/repo", None);+ for policy in [&active, &invalid] {+ let decision = policy.inspect(AgentKind::ClaudeCode, &reader, "/repo");+ assert_eq!(+ decision.protocol().disposition(),+ CaptureDisposition::MetadataOnly+ );+ let body = metadata_only_body(Some("s"), Some("/repo"), &decision).to_string();+ assert!(!body.contains("PRIVATE_BODY_SENTINEL"), "{body}");+ }+ assert_eq!(+ inactive+ .inspect(AgentKind::ClaudeCode, &reader, "/repo")+ .protocol()+ .disposition(),+ CaptureDisposition::Keep+ );+ // Control: an unrelated unknown tool keeps today's behavior.+ let other = json!({"tool_name": "FutureTool", "tool_input": {"bash_id": "b1"}});+ assert_eq!(+ active+ .inspect(AgentKind::ClaudeCode, &other, "/repo")+ .protocol()+ .disposition(),+ CaptureDisposition::Keep+ );+ }++ /// Copilot CLI's runtime `view`/`create` names are file tools for that+ /// agent only; another agent's same-named tool stays unknown.+ #[test]+ fn copilot_cli_runtime_file_tools_are_scoped_to_copilot() {+ assert_eq!(+ agent_family(AgentKind::CopilotCli, "view"),+ ToolFamily::File+ );+ assert_eq!(+ agent_family(AgentKind::CopilotCli, "create"),+ ToolFamily::File+ );+ assert_eq!(+ agent_family(AgentKind::CopilotCli, "str_replace_editor"),+ ToolFamily::File+ );+ assert_eq!(+ agent_family(AgentKind::CopilotCli, "rg"),+ ToolFamily::SearchList+ );+ assert_eq!(+ agent_family(AgentKind::CopilotCli, "edit"),+ ToolFamily::File+ );+ assert_eq!(+ agent_family(AgentKind::ClaudeCode, "view"),+ ToolFamily::Unknown+ );+ assert_eq!(+ agent_family(AgentKind::OpenCode, "create"),+ ToolFamily::Unknown+ );+ let metadata = tool_observation_metadata(+ AgentKind::CopilotCli,+ &json!({"tool_name": "view", "tool_input": {"path": "src/lib.rs"}}),+ true,+ )+ .unwrap();+ assert_eq!(metadata.tool_family, ToolFamily::File);+ }++ #[test]+ fn unevaluated_shell_expansions_respect_quoting() {+ for (command, expected) in [+ ("cat docs/{adr,x}/1.md", true),+ ("cat docs/ad[r]/1.md", true),+ ("echo {1..3}", true),+ ("cat 'docs/{adr,x}'", false),+ ("cat \"docs/[ab]\"", false),+ ("cat docs/\\{a,b}", false),+ ("cat docs/\\[ab]", false),+ ("echo ${a,b} ${arr[0]}", false),+ ("find . -exec cat {} ;", false),+ ("[ -f x ]", false),+ ("[[ -d x ]]", false),+ ("git diff HEAD@{1}..HEAD", false),+ ("echo a, b} {c", false),+ ("cat [] x", false),+ ] {+ assert_eq!(has_unevaluated_expansion(command), expected, "{command}");+ }+ }+#[test]fn globstar_reaches_lets_double_star_span_any_depth() {assert!(globstar_reaches("/repo/**/*.md", "/repo/docs/adr", false));diff --git a/crates/ai-memory-hooks/src/router.rs b/crates/ai-memory-hooks/src/router.rsindex 683517db..e923a57b 100644--- a/crates/ai-memory-hooks/src/router.rs+++ b/crates/ai-memory-hooks/src/router.rs@@ -1178,12 +1178,14 @@ fn metadata_only_protocol_envelope(}/// An invalid marker also strips shell commands, which it cannot prove miss-/// every ignored path; an active policy decides them outright (keep/drop).-/// A stripped shell body carries no paths and an extracted command.+/// every ignored path; an active policy strips one whose words use brace+/// expansion or a bracket class. Either policy strips a tool that reads back+/// an earlier command's output, whose family is unknown. A stripped shell or+/// output-reader body carries no paths and an extracted command.const fn metadata_protocol_is_legal(protocol: &CaptureProtocol) -> bool {match (protocol.policy_state(), protocol.tool_family()) {(PolicyState::Active | PolicyState::Invalid, ToolFamily::File) => true,- (PolicyState::Invalid, ToolFamily::NonFile) => {+ (PolicyState::Active | PolicyState::Invalid, ToolFamily::NonFile | ToolFamily::Unknown) => {protocol.path_count() == 0&& matches!(protocol.extraction_state(), ExtractionState::Extracted)}@@ -17539,13 +17541,21 @@ mod tests {);assert_eq!(env.raw["tool_family"], "non-file");- // An active policy decides a shell call outright, so a metadata-only- // claim for one is impossible and refused.+ // An active policy strips a shell call whose words it cannot+ // evaluate (brace expansion, a bracket class); an inactive one never+ // strips anything, so that claim is refused.assert!(inspect_capture_envelope(HookEnvelope::from_query_and_body(query(),stripped("active")))+ .is_some()+ );+ assert!(+ inspect_capture_envelope(HookEnvelope::from_query_and_body(+ query(),+ stripped("inactive")+ )).is_none());@@ -17578,6 +17588,94 @@ mod tests {assert_eq!(env.raw, search);}+ /// A client that predates these rules keeps a shell call with an+ /// unevaluated brace expansion, or a tool reading back an earlier+ /// command's output; the server's own re-inspection strips both under an+ /// active policy, and keeps a plain command and an unrelated unknown tool+ /// (controls).+ #[test]+ fn capture_protocol_active_keep_of_unprovable_output_is_stripped() {+ let query = || HookQuery {+ event: "post-tool-use".into(),+ agent: Some("claude-code".into()),+ ..Default::default()+ };+ let kept = |tool: &str, input: serde_json::Value, family: &str| {+ serde_json::json!({+ "session_id": "active-keep", "cwd": "/repo",+ "tool_name": tool, "tool_input": input,+ "tool_response": "SENTINEL_SECRET",+ "_ai_memory_capture": capture_protocol("keep", "active", family, 0, "extracted"),+ })+ };+ for (raw, family) in [+ (+ kept(+ "Bash",+ serde_json::json!({"command": "cat docs/{adr,x}/1.md"}),+ "non-file",+ ),+ "non-file",+ ),+ (+ kept(+ "Bash",+ serde_json::json!({"command": "cat docs/ad[r]/1.md"}),+ "non-file",+ ),+ "non-file",+ ),+ (+ kept(+ "BashOutput",+ serde_json::json!({"bash_id": "b1"}),+ "unknown",+ ),+ "unknown",+ ),+ (+ kept(+ "TaskOutput",+ serde_json::json!({"task_id": "b1"}),+ "unknown",+ ),+ "unknown",+ ),+ ] {+ let env =+ inspect_capture_envelope(HookEnvelope::from_query_and_body(query(), raw.clone()))+ .unwrap();+ assert_eq!(+ env.raw["_ai_memory_capture"]["disposition"], "metadata-only",+ "{raw}"+ );+ assert_eq!(env.raw["tool_family"], family, "{raw}");+ let stored = serde_json::to_string(&env).unwrap();+ assert!(!stored.contains("SENTINEL"), "{stored}");+ }+ for raw in [+ kept(+ "Bash",+ serde_json::json!({"command": "git log --format='%h {a,b}'"}),+ "non-file",+ ),+ kept("FutureTool", serde_json::json!({"x": 1}), "unknown"),+ ] {+ let env = HookEnvelope::from_query_and_body(query(), raw.clone());+ assert_eq!(inspect_capture_envelope(env).unwrap().raw, raw);+ }+ // A current client's stripped output-reader body is accepted.+ let stripped = serde_json::json!({+ "session_id": "reader", "cwd": "/repo",+ "tool_family": "unknown", "tool_name": "unknown",+ "_ai_memory_capture": capture_protocol("metadata-only", "active", "unknown", 0, "extracted"),+ });+ assert!(+ inspect_capture_envelope(HookEnvelope::from_query_and_body(query(), stripped))+ .is_some()+ );+ }+#[test]fn capture_protocol_invalid_shell_metadata_claim_must_be_canonical() {let admit = |path_count: u16, extraction: &str| {diff --git a/crates/ai-memory-hooks/tests/fixtures/capture-policy.json b/crates/ai-memory-hooks/tests/fixtures/capture-policy.jsonindex 06be443b..9235c77d 100644--- a/crates/ai-memory-hooks/tests/fixtures/capture-policy.json+++ b/crates/ai-memory-hooks/tests/fixtures/capture-policy.json@@ -73,8 +73,41 @@{"agent":"open-code","payload":{"tool":"run_shell_command","args":{"command":"cat public/a"}},"disposition":"keep","tool_family":"non-file","extraction_state":"extracted","path_count":0},{"agent":"open-code","payload":{"tool":"list_directory","args":{"dir_path":"secret"}},"disposition":"drop","tool_family":"search-list","extraction_state":"not-applicable","path_count":0},{"agent":"open-code","payload":{"tool":"search_file_content","args":{"pattern":"x"}},"disposition":"drop","tool_family":"search-list","extraction_state":"not-applicable","path_count":0},- {"agent":"gemini-cli","payload":{"tool_name":"read_many_files","tool_input":{"include":["public/a","secret/[ab]"]}},"disposition":"metadata-only","tool_family":"file","extraction_state":"missing-or-malformed","path_count":2}+ {"agent":"gemini-cli","payload":{"tool_name":"read_many_files","tool_input":{"include":["public/a","secret/[ab]"]}},"disposition":"metadata-only","tool_family":"file","extraction_state":"missing-or-malformed","path_count":2},+ {"agent":"copilot-cli","payload":{"tool_name":"view","tool_input":{"path":"secret/a"}},"disposition":"drop","tool_family":"file","extraction_state":"extracted","path_count":1},+ {"agent":"copilot-cli","payload":{"tool_name":"create","tool_input":{"path":"secret/a","file_text":"x"}},"disposition":"drop","tool_family":"file","extraction_state":"extracted","path_count":1},+ {"agent":"copilot-cli","payload":{"tool_name":"str_replace_editor","tool_input":{"command":"view","path":"secret/a"}},"disposition":"drop","tool_family":"file","extraction_state":"extracted","path_count":1},+ {"agent":"copilot-cli","payload":{"tool_name":"view","tool_input":{"path":"public/a"}},"disposition":"keep","tool_family":"file","extraction_state":"extracted","path_count":1},+ {"agent":"copilot-cli","payload":{"tool_name":"view","tool_input":{}},"disposition":"metadata-only","tool_family":"file","extraction_state":"missing-or-malformed","path_count":0},+ {"agent":"copilot-cli","payload":{"tool_name":"rg","tool_input":{"pattern":"x"}},"disposition":"drop","tool_family":"search-list","extraction_state":"not-applicable","path_count":0},+ {"agent":"claude-code","payload":{"tool_name":"view","tool_input":{"path":"secret/a"}},"disposition":"keep","tool_family":"unknown","extraction_state":"extracted","path_count":0},+ {"agent":"claude-code","payload":{"tool_name":"BashOutput","tool_input":{"bash_id":"b1"}},"disposition":"metadata-only","tool_family":"unknown","extraction_state":"extracted","path_count":0},+ {"agent":"claude-code","payload":{"tool_name":"TaskOutput","tool_input":{"task_id":"b1"}},"disposition":"metadata-only","tool_family":"unknown","extraction_state":"extracted","path_count":0},+ {"agent":"codex","payload":{"tool_name":"write_stdin","tool_input":{"session_id":1,"chars":""}},"disposition":"metadata-only","tool_family":"unknown","extraction_state":"extracted","path_count":0},+ {"agent":"hermes","payload":{"tool_name":"process","tool_input":{"action":"poll","session_id":"p1"}},"disposition":"metadata-only","tool_family":"unknown","extraction_state":"extracted","path_count":0},+ {"agent":"copilot-cli","payload":{"tool_name":"read_bash","tool_input":{"shellId":"s1"}},"disposition":"metadata-only","tool_family":"unknown","extraction_state":"extracted","path_count":0},+ {"agent":"copilot-cli","payload":{"tool_name":"write_powershell","tool_input":{"shellId":"s1","input":"x"}},"disposition":"metadata-only","tool_family":"unknown","extraction_state":"extracted","path_count":0},+ {"agent":"openclaw","payload":{"tool":"process","args":{"action":"log","sessionId":"p1"}},"disposition":"metadata-only","tool_family":"unknown","extraction_state":"extracted","path_count":0},+ {"agent":"open-code","payload":{"tool":"process","args":{"action":"log"}},"disposition":"metadata-only","tool_family":"unknown","extraction_state":"extracted","path_count":0},+ {"agent":"openclaw","payload":{"tool":"future_tool","args":{"action":"log"}},"disposition":"keep","tool_family":"unknown","extraction_state":"extracted","path_count":0}],+ "read_many_files_directories": {+ "note": "Gemini CLI reads a directory entry as `entry/**`; both matchers run these as `read_many_files` `include` lists from the marker directory.",+ "ignore_paths": ["docs/adr/**"],+ "vectors": [+ {"include":["docs"],"disposition":"drop"},+ {"include":["."],"disposition":"drop"},+ {"include":["./"],"disposition":"drop"},+ {"include":["docs/"],"disposition":"drop"},+ {"include":["Docs"],"disposition":"drop"},+ {"include":["src/../docs"],"disposition":"drop"},+ {"include":["src","docs"],"disposition":"drop"},+ {"include":["src"],"disposition":"keep"},+ {"include":["README.md"],"disposition":"keep"},+ {"include":["docs/adrx"],"disposition":"keep"},+ {"include":["docs/notes"],"disposition":"keep"}+ ]+ },"shell": {"note": "Shell-tool vectors run by both matchers. `{root}` is the marker directory, `cwd` is relative to it, and `command` is shorthand for a `bash` payload.","ignore_paths": ["docs/adr/**","~/notes/**","docs/n?tes/**","C:/Secret/**","private notes/**"],@@ -141,7 +174,22 @@{"payload":{"tool":"bash","args":{"command":"cat 0001.md","workdir":7}},"disposition":"keep"},{"payload":{"tool":"run_shell_command","args":{"command":"cat adr/x.md","dir_path":"docs"}},"disposition":"drop"},{"payload":{"tool":"run_shell_command","args":{"command":"cat 0001.md","directory":"docs/adr"}},"disposition":"drop"},- {"payload":{"tool":"run_shell_command","args":{"command":"cat x.md","dir_path":"src"}},"disposition":"keep"}+ {"payload":{"tool":"run_shell_command","args":{"command":"cat x.md","dir_path":"src"}},"disposition":"keep"},+ {"command":"cat docs/{adr,x}/1.md","disposition":"metadata-only"},+ {"command":"cat docs/ad[r]/1.md","disposition":"metadata-only"},+ {"command":"ls docs/ad{1..3}/x.md","disposition":"metadata-only"},+ {"command":"cat src/main.rs && cat {docs,src}/adr/1.md","disposition":"metadata-only"},+ {"payload":{"tool":"shell","args":{"command":["bash","-lc","cat docs/{adr,x}/1.md"]}},"disposition":"metadata-only"},+ {"command":"cat docs/adr/x.md {a,b}","disposition":"drop"},+ {"command":"cat 'docs/{adr,x}/1.md'","disposition":"keep"},+ {"command":"cat \"docs/ad[r]/1.md\"","disposition":"keep"},+ {"command":"cat docs/\\{adr,x}/1.md","disposition":"keep"},+ {"command":"git log --format='%h {a,b}'","disposition":"keep"},+ {"command":"find . -name '*.rs' -exec wc -l {} ;","disposition":"keep"},+ {"command":"echo ${HOME} ${arr[0]}","disposition":"keep"},+ {"command":"[ -f src/main.rs ] && [[ -d src ]]","disposition":"keep"},+ {"command":"git diff HEAD@{1}..HEAD","disposition":"keep"},+ {"command":"ls src/*.rs","disposition":"keep"}]},"protocol": {"accept":{"version":1,"disposition":"drop","policy_state":"active","tool_family":"file","path_count":1,"extraction_state":"extracted"},"reject":{"version":1,"disposition":"drop","policy_state":"active","tool_family":"file","path_count":1,"extraction_state":"extracted","paths":["SENTINEL"]}}diff --git a/docs/marker-file.md b/docs/marker-file.mdindex 08966495..7827125d 100644--- a/docs/marker-file.md+++ b/docs/marker-file.md@@ -491,8 +491,14 @@ logs, or server storage. With an active policy, recognized search/list tools aredropped conservatively; missing or malformed recognized file candidates, anunsupported recognized schema, or an invalid policy become **metadata-only**.That form contains only bounded routing/tool/decision metadata, never paths,-patterns, arguments, output, errors, titles, or nested payload. Unknown tools-retain current behavior.+patterns, arguments, output, errors, titles, or nested payload. Tools that read+back output of a command started by an earlier call (Claude Code `BashOutput`+and `TaskOutput`, Codex `write_stdin`, Hermes and OpenClaw `process`, GitHub+Copilot CLI `read_bash`/`write_bash` and their PowerShell twins) become+metadata-only under an active or invalid policy, because nothing proves the+command they read missed every ignored path. Other unknown tools retain current+behavior. GitHub Copilot CLI's runtime `view`, `create` and+`str_replace_editor` names are file tools read from their `path` argument.Gemini CLI's `read_many_files` is a file tool whose `include` entries (`paths`in older releases) are globs that Gemini expands case-insensitively, with `**`@@ -500,7 +506,11 @@ reaching any depth. ai-memory reads every entry and drops the whole call whenany one names an ignored path or could expand into one, matching case-insensitivelylike Gemini: `docs/adr/x.md`, `docs/*/x.md`, `docs/**` and `**/*.md` areall dropped under `docs/adr/**`, while `src/**/*.rs` and a root-level `*.md`-are not. Its `exclude` list never subtracts an ignored path. An entry using glob+are not. Gemini reads an entry that names a directory recursively, and the+matcher cannot tell a directory from a file, so an entry that could contain an+ignored path drops the call too: `docs`, `.` and `docs/` are dropped under+`docs/adr/**`, while `src` and `README.md` are not. Its `exclude` list never+subtracts an ignored path. An entry using globsyntax the matcher cannot evaluate (`[…]`, `{…}`, `(…)`, `!`) makes the call**metadata-only**.@@ -519,13 +529,17 @@ one matches a pattern, the whole event is **dropped**, exactly like a matchingfile read. An argument containing `*` or `?` also matches when its glob canreach a pattern's directory: `cat docs/*/0001.md` is dropped under`docs/adr/**`, `cat *.md` at the repository root is not. A command that exceeds-the match budget is dropped. Variables, command substitution, `cd` state, and+the match budget is dropped. A command with an unquoted brace expansion+(`docs/{adr,x}/1.md`, `{1..3}`) or bracket class (`docs/ad[r]/1.md`) that no+argument matches outright is **metadata-only**, because those are not+evaluated; quoted or escaped braces and brackets, `${…}`, `{}` and `[ -f x ]`+are unaffected. Variables, command substitution, `cd` state, andcommands that name no path at all (`rg TODO`, `git diff`) are not followed, sotheir output is still captured. An invalid policy makes a shell command**metadata-only**, like a file tool, because a broken marker cannot prove itsarguments miss every ignored path; this holds even when the command cannot be-read. An older server drops that metadata-only shell event, so upgrade the-server before the clients. Excluding content before transport+read. An older server drops these metadata-only shell and output-reader+events, so upgrade the server before the clients. Excluding content before transportmatters because it cannot then reach observations/FTS, session pages, handoffs,reviewer requests, proposals, or logs.@@ -542,6 +556,18 @@ re-reads too (for every file, not only the ignored ones). Add each relevant visiexplicitly, and do not rely on this feature to detect every way private contentcan be mentioned.+These gaps are inherent to lexical matching and stay open:++- shell variables (`$VAR`) and command substitution are not expanded;+- a `cd` inside a command is not followed (the tool's own `workdir` is);+- symlinks are not resolved, by design;+- a recursive search of a parent directory through a shell (`grep -r x docs`,+ `rg x`) names no ignored path, so its output is kept;+- tools the matcher does not list keep their current behavior.++For a project whose content must never be captured, use allowlist capture mode+(above) and leave that repository without a marker.+### Supported integrations and refreshCapture policy v1 is enforced by native `ai-memory hook` commands (including