Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Rust
fix(hooks): honor capture exclusions for Gemini CLI shell and read_many_files
AkitaOnRails667e0985d750
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 92e98c07..bbaa9d1b 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -8,6 +8,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0## [Unreleased]### Fixed+- Fixed Gemini CLI's `run_shell_command` and `read_many_files` tools being+ classified as unknown, so `[capture] ignore_paths` never checked them and a+ call reading an ignored file still had its output written to the local hook+ spool and sent to the server, which then discarded it. `run_shell_command` is+ now a shell tool, read like `Bash` and resolved from its `dir_path`, and+ `read_many_files` is a file tool: the call is dropped when any of its+ `include` globs names or could expand into an ignored path, and glob syntax+ the matcher cannot evaluate makes it metadata-only. Gemini's+ `list_directory` and the legacy `search_file_content` name are now search+ tools, dropped under an active policy like `grep_search` and `glob`. This+ applies to the native hook, the server, and the generated TypeScript+ integrations.- Fixed a web page open in the operator's browser being able to act on anunauthenticated server (the loopback default, no token), such as claiming aproject's pending handoff or starting admin jobs. With auth off, the machinediff --git a/crates/ai-memory-cli/src/commands/hook.rs b/crates/ai-memory-cli/src/commands/hook.rsindex d4b85bf2..355bcd2b 100644--- a/crates/ai-memory-cli/src/commands/hook.rs+++ b/crates/ai-memory-cli/src/commands/hook.rs@@ -3069,6 +3069,101 @@ mod tests {}}+ /// Gemini CLI's `run_shell_command` and `read_many_files` were unknown+ /// tools, so the native hook spooled their raw output even when they read+ /// an ignored path. Both events of each call are now dropped before the+ /// spool; public calls are still spooled with their family recorded.+ #[tokio::test]+ async fn gemini_cli_shell_and_read_many_files_exclusions_drop_before_spool() {+ let cases = [+ (+ "run_shell_command",+ serde_json::json!({"command": "cat secret/token.txt"}),+ 0,+ ),+ (+ "run_shell_command",+ serde_json::json!({"command": "cat token.txt", "dir_path": "secret"}),+ 0,+ ),+ (+ "read_many_files",+ serde_json::json!({"include": ["secret/token.txt"]}),+ 0,+ ),+ (+ "read_many_files",+ serde_json::json!({"include": ["README.md", "secret/token.txt"]}),+ 0,+ ),+ (+ "read_many_files",+ serde_json::json!({"include": ["**/*.txt"]}),+ 0,+ ),+ (+ "run_shell_command",+ serde_json::json!({"command": "cat README.md"}),+ 1,+ ),+ (+ "read_many_files",+ serde_json::json!({"include": ["README.md", "src/**/*.rs"]}),+ 1,+ ),+ ];+ for (tool, input, spooled) in cases {+ for event in ["pre-tool-use", "post-tool-use"] {+ let tmp = tempfile::tempdir().unwrap();+ std::fs::write(+ tmp.path().join(".ai-memory.toml"),+ "[capture]\nignore_paths = [\"secret/**\"]\n",+ )+ .unwrap();+ let data_dir = tmp.path().join("data");+ let mut args = devin_hook_args(event);+ args.agent = "gemini-cli".into();+ let raw = serde_json::json!({+ "session_id": "gemini", "cwd": tmp.path(),+ "tool_name": tool, "tool_input": input,+ "tool_response": {"llmContent": "SENTINEL_CONTENT"},+ });+ run_with_payload(+ Some(data_dir.clone()),+ args,+ raw.to_string(),+ &mut Vec::new(),+ |_, _| panic!("tool events below the threshold must only spool"),+ )+ .await+ .unwrap();+ let spool = hook_spool::spool_dir(&data_dir);+ let entries = if spool.exists() {+ read_spooled_entries(&spool)+ } else {+ Vec::new()+ };+ assert_eq!(entries.len(), spooled, "{event}: {tool} {input}");+ for entry in &entries {+ let body: serde_json::Value = serde_json::from_str(&entry.body).unwrap();+ let family = if tool == "read_many_files" {+ "file"+ } else {+ "non-file"+ };+ assert_eq!(+ body["_ai_memory_capture"]["tool_family"], family,+ "{event}: {tool}"+ );+ assert_eq!(+ body["_ai_memory_capture"]["disposition"], "keep",+ "{event}: {tool}"+ );+ }+ }+ }+ }+#[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 0d492c8e..4b17ce98 100644--- a/crates/ai-memory-cli/src/commands/render_shared.rs+++ b/crates/ai-memory-cli/src/commands/render_shared.rs@@ -337,8 +337,15 @@ function captureShellArguments(word: string): string[] { const flag = word.start// Lexical only, like the native hook: nothing is expanded or executed, so// variables, command substitution, and `cd` state are not followed. A tool's// own `workdir` replaces the event cwd for relative arguments.-function captureMatchCommand(command: string | string[], config: CaptureConfig, workdir?: string): boolean | undefined { const budget = { work: 0 }; const home = homedir(); const base = workdir === undefined ? config.base : /^(?:\/|\\\\|[A-Za-z]:[\\/])/.test(workdir) ? workdir : config.base && captureJoin(config.base, workdir); for (const word of captureShellWordList(command)) for (const argument of captureShellArguments(word)) { const expanded = argument.startsWith("~/") ? captureJoin(home, argument.slice(2)) : argument; if ([...expanded].length > CAPTURE_MAX_PATH_CHARS) continue; const absolute = /^(?:\/|\\\\|[A-Za-z]:[\\/])/.test(expanded); if (!absolute && !base) continue; const candidate = captureNormalize(absolute ? expanded : captureJoin(base, expanded), config.windowsHost); if (!candidate) continue; const glob = /[*?]/.test(candidate.path); for (const pattern of config.patterns) { if (candidate.windows !== pattern.windows) continue; const under = captureStartsWith(candidate.path, pattern.prefix, pattern.windows); if (!under && !glob) continue; if (under) { const directory = pattern.directory ? captureGlob(pattern.directory, candidate.path, pattern.windows, budget) : false; if (directory !== false) return directory; const match = captureGlob(pattern.path, candidate.path, pattern.windows, budget); if (match !== false) return match; } if (glob) { const reaches = captureGlobReaches(candidate.path, pattern.prefix, pattern.windows, budget); if (reaches !== false) return reaches; } } } return false; }-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 } { 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"].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"].includes(name)) return { family: "non-file", extraction: "extracted", callID: call, command: captureShellCommand(args), shell: name !== "web_search", workdir: typeof args?.workdir === "string" && args.workdir.trim() ? args.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"].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 (!paths || 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 }; }+function captureMatchCommand(command: string | string[], config: CaptureConfig, workdir?: string): boolean | undefined { const budget = { work: 0 }; const home = homedir(); const base = workdir === undefined ? config.base : /^(?:\/|\\\\|[A-Za-z]:[\\/])/.test(workdir) ? workdir : config.base && captureJoin(config.base, workdir); for (const word of captureShellWordList(command)) for (const argument of captureShellArguments(word)) { const expanded = argument.startsWith("~/") ? captureJoin(home, argument.slice(2)) : argument; if ([...expanded].length > CAPTURE_MAX_PATH_CHARS) continue; const absolute = /^(?:\/|\\\\|[A-Za-z]:[\\/])/.test(expanded); if (!absolute && !base) continue; const candidate = captureNormalize(absolute ? expanded : captureJoin(base, expanded), config.windowsHost); if (!candidate) continue; const match = captureMatchCandidate(candidate, false, config, budget); if (match !== false) return match; } return false; }+// Gemini CLI `read_many_files` globs expand case-insensitively with `**`+// recursing to any depth: a literal path matches like a file tool's, a glob+// 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 = toolGlob ? captureGlobstarReaches(candidate.path, pattern.prefix, insensitive, budget) : captureGlobReaches(candidate.path, pattern.prefix, insensitive, budget); if (reaches !== false) return reaches; } } 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" }; }// 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@@ -354,7 +361,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 { 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)) 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 }; }"##;TEMPLATE.replace("__AI_MEMORY_CAPTURE_MODE__", capture_mode)}@@ -2539,6 +2546,12 @@ expectDecision({{ tool: "edit", args: {{ path: "x" }} }}, marker(`[capture]\nignexpectDecision({{ tool: "edit", args: {{ path: "literal#item" }} }}, marker("[capture]\nignore_paths = [\n 'literal#item',\n \"basic#item\"\n]\n"), "drop", "file", "extracted", 1, "multiline-and-hash");expectDecision({{ tool: "edit", args: {{ path: "C:relative" }} }}, marker(active), "metadata-only", "file", "extracted", 1, "drive-relative");expectDecision({{ tool: "future-tool", args: {{ path: "secret/a" }} }}, marker(active), "keep", "unknown", "extracted", 0, "unknown-tool");+// Gemini `read_many_files` glob syntax the matcher cannot evaluate fails closed;+// an empty glob list proves nothing.+expectDecision({{ tool: "read_many_files", args: {{ include: ["public/a", "secret/[ab]"] }} }}, marker(active), "metadata-only", "file", "extracted", 2, "read-many-unsupported-glob");+expectDecision({{ tool: "read_many_files", args: {{ include: ["src/**/*.rs", "secret/{{a,b}}"] }} }}, marker(active), "metadata-only", "file", "extracted", 2, "read-many-brace-glob");+expectDecision({{ tool: "read_many_files", args: {{ include: [] }} }}, marker(active), "metadata-only", "file", "missing-or-malformed", 0, "read-many-empty");+expectDecision({{ tool: "read_many_files", args: {{ include: ["**/*.md"] }} }}, marker('[capture]\nignore_paths = ["docs/adr/**"]\n'), "drop", "file", "extracted", 1, "read-many-globstar");const adversarial = "a".repeat(4090);expectDecision({{ tool: "multi_edit", args: {{ edits: [{{ path: "secret/a" }}, {{ path: adversarial }}] }} }}, marker('[capture]\nignore_paths = ["secret/**", "*'.concat("a".repeat(1022), '"]\n')), "drop", "file", "extracted", 2, "first-drop-beats-budget");diff --git a/crates/ai-memory-hooks/src/capture_policy.rs b/crates/ai-memory-hooks/src/capture_policy.rsindex 5b44a383..9a2f27b7 100644--- a/crates/ai-memory-hooks/src/capture_policy.rs+++ b/crates/ai-memory-hooks/src/capture_policy.rs@@ -572,14 +572,21 @@ impl CapturePolicy {ExtractionState::MissingOrMalformed,)}- Some(paths) => match self.match_paths(paths, cwd) {- Ok(true) => (CaptureDisposition::Drop, ExtractionState::Extracted),- Ok(false) => (CaptureDisposition::Keep, ExtractionState::Extracted),- Err(()) => (- CaptureDisposition::MetadataOnly,- ExtractionState::MissingOrMalformed,- ),- },+ Some(paths) => {+ let matched = if extracted.globs {+ self.match_globs(paths, cwd)+ } else {+ self.match_paths(paths, cwd)+ };+ match matched {+ Ok(true) => (CaptureDisposition::Drop, ExtractionState::Extracted),+ Ok(false) => (CaptureDisposition::Keep, ExtractionState::Extracted),+ Err(()) => (+ CaptureDisposition::MetadataOnly,+ ExtractionState::MissingOrMalformed,+ ),+ }+ }},// A shell command has no path field, but its arguments can// still name an ignored file whose content lands in the@@ -661,43 +668,81 @@ impl CapturePolicy {let Some(candidate) = normalize_candidate(&expanded, cwd) else {continue;};- let insensitive = candidate.flavor == Flavor::Windows;- let is_glob = candidate.path.contains(['*', '?']);- for pattern in self- .patterns- .iter()- .filter(|pattern| pattern.flavor == candidate.flavor)- {- // Linear and bounded by the command itself; only the- // 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 {- continue;- }- work = work- .checked_add(pattern.match_cost(&candidate.path))- .ok_or(())?;- if work > MAX_MATCH_WORK {- return Err(());- }- if (under_prefix- && glob_match(- &pattern.path,- &candidate.path,- pattern.directory_base.as_deref(),- insensitive,- ))- || (is_glob- && glob_reaches(&candidate.path, &pattern.literal_prefix, insensitive))- {- return Ok(true);- }+ if self.match_pattern_candidate(&candidate, false, &mut work)? {+ return Ok(true);}}}Ok(false)}+ /// Matches the path arguments of Gemini CLI's `read_many_files`, which are+ /// globs expanded case-insensitively (`nocase`) with `**` recursing into+ /// any depth. A literal path matches like a file tool's; a glob drops when+ /// it could expand into an ignored path, the same over-approximation+ /// shell globs get. Glob syntax the matcher cannot evaluate (classes,+ /// braces, extglobs, negation) fails closed.+ fn match_globs(&self, paths: &[String], cwd: &str) -> Result<bool, ()> {+ let mut work = 0_usize;+ for path in paths {+ if path.contains(['[', ']', '{', '}', '(', ')', '!']) {+ return Err(());+ }+ let candidate = normalize_candidate(path, cwd).ok_or(())?;+ if self.match_pattern_candidate(&candidate, true, &mut work)? {+ return Ok(true);+ }+ }+ Ok(false)+ }+ /// Whether one normalized candidate, literal or glob, names or can expand+ /// into an ignored path. `tool_glob` selects `read_many_files` expansion+ /// (case-insensitive, recursive `**`) over a shell's; Windows always+ /// ignores case.+ fn match_pattern_candidate(+ &self,+ candidate: &Normalized,+ tool_glob: bool,+ work: &mut usize,+ ) -> Result<bool, ()> {+ let insensitive = tool_glob || candidate.flavor == Flavor::Windows;+ let is_glob = candidate.path.contains(['*', '?']);+ for pattern in self+ .patterns+ .iter()+ .filter(|pattern| pattern.flavor == candidate.flavor)+ {+ // Linear and bounded by the candidate itself; only the+ // 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 {+ continue;+ }+ *work = work+ .checked_add(pattern.match_cost(&candidate.path))+ .ok_or(())?;+ if *work > MAX_MATCH_WORK {+ return Err(());+ }+ let reaches = if tool_glob {+ globstar_reaches+ } else {+ glob_reaches+ };+ if (under_prefix+ && glob_match(+ &pattern.path,+ &candidate.path,+ pattern.directory_base.as_deref(),+ insensitive,+ ))+ || (is_glob && reaches(&candidate.path, &pattern.literal_prefix, insensitive))+ {+ return Ok(true);+ }+ }+ Ok(false)+ }}/// Constructs a metadata-only replacement from scalar allowlisted envelope values and a decision.@@ -737,6 +782,8 @@ struct Extracted<'a> {/// locate where the tool ran; capture policy drops search output under an/// active marker regardless of it.search_paths: Option<Vec<String>>,+ /// The paths are globs (Gemini CLI `read_many_files`), not literal files.+ globs: bool,call_id: Option<String>,state: ExtractionState,}@@ -805,11 +852,18 @@ fn extract(agent: AgentKind, raw: &Value) -> Extracted<'_> {.then(|| args.and_then(|value| direct_paths(value.as_object()?))).flatten();// OpenCode `bash`, OpenClaw `exec` and Codex `shell` run in `workdir`- // when given, so relative arguments resolve from there.+ // when given, Gemini CLI `run_shell_command` in `dir_path` (`directory`+ // before it was renamed), so relative arguments resolve from there.let shell = family == ToolFamily::NonFile && !name.eq_ignore_ascii_case("web_search");let workdir = command.as_ref()- .and_then(|_| args?.get("workdir")?.as_str())+ .and_then(|_| {+ let args = args?;+ ["workdir", "dir_path", "directory"]+ .iter()+ .find_map(|key| args.get(*key))?+ .as_str()+ }).filter(|dir| !dir.trim().is_empty()).map(str::to_owned);let state = if family == ToolFamily::File && paths.is_none() {@@ -841,6 +895,7 @@ fn extract(agent: AgentKind, raw: &Value) -> Extracted<'_> {shell,workdir,search_paths,+ globs: name.eq_ignore_ascii_case("read_many_files"),call_id,state,}@@ -870,14 +925,21 @@ fn family(name: &str) -> ToolFamily {| "fs_read"| "fs_write" => ToolFamily::File,"read_file" | "write_file" | "edit_file" | "patch" => ToolFamily::File,+ // Gemini CLI reads every file its `include` globs expand to.+ "read_many_files" => ToolFamily::File,"search" | "grep" | "glob" | "find" | "list" | "ls" | "list_files" | "read_dir"| "list_dir" | "grep_search" | "search_files" | "find_by_name" => ToolFamily::SearchList,+ // Gemini CLI's directory listing, and the pre-rename name of its+ // `grep_search` that it still accepts as an alias.+ "list_directory" | "search_file_content" => ToolFamily::SearchList,"bash" | "shell" | "shell_command" | "exec" | "execute" | "run_command" | "web_search"| "search_web" | "manage_task" | "manage_subagents" | "terminal" | "execute_bash"| "execute_cmd" => ToolFamily::NonFile,// Claude Code on Windows exposes `PowerShell` beside `Bash`, with the// same `tool_input.command` field (#1186)."powershell" => ToolFamily::NonFile,+ // Gemini CLI's shell tool, `{command, dir_path?}`.+ "run_shell_command" => ToolFamily::NonFile,// Hermes Agent tool surface (agent/shell_hooks.py envelope; tool// names from model_tools registry, live-captured 2026-10-07). These// execute code, drive a browser, or fetch the web — none touch@@ -929,6 +991,20 @@ fn extract_paths(name: &str, args: &Value) -> Option<Vec<String>> {paths.extend(paths_in_entry);}}+ // Gemini CLI `read_many_files` names its globs in `include` (`paths`,+ // which `direct_paths` reads, before a rename). `exclude` only narrows+ // the read, so it is not a candidate.+ if name.eq_ignore_ascii_case("read_many_files")+ && let Some(include) = object.get("include")+ {+ let entries = include.as_array()?;+ if paths.len().checked_add(entries.len())? > MAX_CAPTURE_CANDIDATES {+ return None;+ }+ for entry in entries {+ paths.push(entry.as_str()?.to_owned());+ }+ }if matches!(name.to_ascii_lowercase().as_str(), "read" | "fs_read")&& let Some(operations) = object.get("operations"){@@ -1450,6 +1526,35 @@ fn glob_reaches(glob: &str, prefix: &str, insensitive: bool) -> bool {}false}+/// `glob_reaches` for a glob whose `**` spans any number of segments, as in+/// Gemini CLI's `read_many_files`: once the segments before the first `**`+/// lead into `prefix` (or `prefix` lies below them), the glob can reach it.+fn globstar_reaches(glob: &str, prefix: &str, insensitive: bool) -> bool {+ let Some(star) = glob.find("**") else {+ return glob_reaches(glob, prefix, insensitive);+ };+ let head = glob[..star].rfind('/').map_or("", |slash| &glob[..slash]);+ let depth = head.split('/').filter(|part| !part.is_empty()).count();+ let mut seen = 0;+ let mut offset = 0;+ for part in prefix.split('/') {+ if seen == depth {+ break;+ }+ offset += part.len();+ if !part.is_empty() {+ seen += 1;+ }+ if seen < depth {+ offset += 1;+ }+ }+ if seen < depth {+ // `prefix` is shallower than the literal head; compare as usual.+ return glob_reaches(glob, prefix, insensitive);+ }+ glob_match(head, &prefix[..offset.min(prefix.len())], None, insensitive)+}fn char_equal(left: char, right: char, insensitive: bool) -> bool {if insensitive && left.is_ascii() && right.is_ascii() {left.eq_ignore_ascii_case(&right)@@ -2374,6 +2479,282 @@ mod tests {}}+ /// Gemini CLI's `run_shell_command` and `read_many_files` fell to the+ /// unknown family, so `ignore_paths` never read their arguments and the+ /// raw output reached the spool. `read_many_files` takes globs that Gemini+ /// expands case-insensitively with a recursive `**`; any one that names or+ /// can expand into an ignored path drops the call, and glob syntax the+ /// matcher cannot evaluate fails closed.+ #[test]+ fn gemini_cli_shell_and_read_many_files_honor_ignored_paths() {+ let policy = CapturePolicy::resolve(+ CaptureSource::Parsed(&CaptureConfig {+ ignore_paths: vec!["docs/adr/**".into()],+ }),+ "/repo",+ None,+ );+ let gemini = |tool: &str, input: Value| {+ json!({"hook_event_name": "AfterTool", "tool_name": tool, "tool_input": input,+ "tool_response": {"llmContent": "SENTINEL_CONTENT"}})+ };+ let read_many = |include: &[&str]| gemini("read_many_files", json!({"include": include}));+ let shell = |input: Value| gemini("run_shell_command", input);+ for (raw, expected, family) in [+ (+ read_many(&["docs/adr/x.md"]),+ CaptureDisposition::Drop,+ ToolFamily::File,+ ),+ (+ read_many(&["README.md", "docs/adr/x.md"]),+ CaptureDisposition::Drop,+ ToolFamily::File,+ ),+ (+ read_many(&["docs/adr"]),+ CaptureDisposition::Drop,+ ToolFamily::File,+ ),+ (+ read_many(&["docs/*/x.md"]),+ CaptureDisposition::Drop,+ ToolFamily::File,+ ),+ (+ read_many(&["**/*.md"]),+ CaptureDisposition::Drop,+ ToolFamily::File,+ ),+ (+ read_many(&["docs/**"]),+ CaptureDisposition::Drop,+ ToolFamily::File,+ ),+ (+ read_many(&["/repo/**/x.md"]),+ CaptureDisposition::Drop,+ ToolFamily::File,+ ),+ (+ read_many(&["Docs/ADR/x.md"]),+ CaptureDisposition::Drop,+ ToolFamily::File,+ ),+ (+ read_many(&[r"docs\adr\x.md"]),+ CaptureDisposition::Drop,+ ToolFamily::File,+ ),+ (+ read_many(&[+ "src/**/*.rs",+ "*.md",+ "docs/guide.md",+ "docs/adr-notes/x.md",+ ]),+ CaptureDisposition::Keep,+ ToolFamily::File,+ ),+ (+ read_many(&["src/**"]),+ CaptureDisposition::Keep,+ ToolFamily::File,+ ),+ (+ read_many(&["/elsewhere/**/*.md"]),+ CaptureDisposition::Keep,+ ToolFamily::File,+ ),+ // `exclude` cannot be trusted to subtract an ignored path.+ (+ gemini(+ "read_many_files",+ json!({"include": ["docs/**"], "exclude": ["docs/adr/**"]}),+ ),+ CaptureDisposition::Drop,+ ToolFamily::File,+ ),+ // Pre-rename spelling of the same tool.+ (+ gemini("read_many_files", json!({"paths": ["docs/adr/x.md"]})),+ CaptureDisposition::Drop,+ ToolFamily::File,+ ),+ (+ gemini(+ "read_many_files",+ json!({"paths": ["README.md"], "include": ["docs/adr/*"]}),+ ),+ CaptureDisposition::Drop,+ ToolFamily::File,+ ),+ // Unprovable globs and shapes fail closed to metadata only.+ (+ read_many(&["docs/ad[r]/x.md"]),+ CaptureDisposition::MetadataOnly,+ ToolFamily::File,+ ),+ (+ read_many(&["docs/{adr,x}/*.md"]),+ CaptureDisposition::MetadataOnly,+ ToolFamily::File,+ ),+ (+ read_many(&["README.md", "!(x)"]),+ CaptureDisposition::MetadataOnly,+ ToolFamily::File,+ ),+ (+ read_many(&[]),+ CaptureDisposition::MetadataOnly,+ ToolFamily::File,+ ),+ (+ gemini("read_many_files", json!({"include": ["README.md", 7]})),+ CaptureDisposition::MetadataOnly,+ ToolFamily::File,+ ),+ (+ shell(json!({"command": "cat docs/adr/x.md"})),+ CaptureDisposition::Drop,+ ToolFamily::NonFile,+ ),+ (+ shell(json!({"command": "cat x.md", "dir_path": "docs/adr"})),+ CaptureDisposition::Drop,+ ToolFamily::NonFile,+ ),+ (+ shell(json!({"command": "cat x.md", "dir_path": "/repo/docs/adr"})),+ CaptureDisposition::Drop,+ ToolFamily::NonFile,+ ),+ (+ shell(json!({"command": "cat x.md", "directory": "docs/adr"})),+ CaptureDisposition::Drop,+ ToolFamily::NonFile,+ ),+ (+ shell(json!({"command": "cat README.md", "description": "show"})),+ CaptureDisposition::Keep,+ ToolFamily::NonFile,+ ),+ (+ shell(json!({"command": "cat x.md", "dir_path": "src"})),+ CaptureDisposition::Keep,+ ToolFamily::NonFile,+ ),+ (+ gemini("list_directory", json!({"dir_path": "docs"})),+ CaptureDisposition::Drop,+ ToolFamily::SearchList,+ ),+ (+ gemini("search_file_content", json!({"pattern": "x"})),+ CaptureDisposition::Drop,+ ToolFamily::SearchList,+ ),+ (+ gemini("grep_search", json!({"pattern": "x"})),+ CaptureDisposition::Drop,+ ToolFamily::SearchList,+ ),+ (+ gemini("glob", json!({"pattern": "**/*.md"})),+ CaptureDisposition::Drop,+ ToolFamily::SearchList,+ ),+ (+ gemini("read_file", json!({"file_path": "docs/adr/x.md"})),+ CaptureDisposition::Drop,+ ToolFamily::File,+ ),+ (+ gemini(+ "write_file",+ json!({"file_path": "docs/adr/x.md", "content": "x"}),+ ),+ CaptureDisposition::Drop,+ ToolFamily::File,+ ),+ (+ gemini(+ "replace",+ json!({"file_path": "docs/adr/x.md", "old_string": "a", "new_string": "b"}),+ ),+ CaptureDisposition::Drop,+ ToolFamily::File,+ ),+ (+ gemini("read_file", json!({"file_path": "README.md"})),+ CaptureDisposition::Keep,+ ToolFamily::File,+ ),+ ] {+ let decision = policy.inspect(AgentKind::GeminiCli, &raw, "/repo");+ assert_eq!(decision.protocol().disposition(), expected, "raw: {raw}");+ assert_eq!(decision.protocol().tool_family(), family, "raw: {raw}");+ }++ // Windows host: Gemini normalizes `\` and ignores case like the drive.+ let windows = CapturePolicy::resolve(+ CaptureSource::Parsed(&CaptureConfig {+ ignore_paths: vec!["docs/adr/**".into()],+ }),+ r"C:\repo",+ None,+ );+ for (include, expected) in [+ (r"DOCS\adr\*.md", CaptureDisposition::Drop),+ (r"C:\repo\**\x.md", CaptureDisposition::Drop),+ (r"src\**\*.rs", CaptureDisposition::Keep),+ ] {+ let raw = read_many(&[include]);+ let decision = windows.inspect(AgentKind::GeminiCli, &raw, r"C:\repo");+ assert_eq!(decision.protocol().disposition(), expected, "{include}");+ }++ // An absolute `dir_path` locates the shell call for rerouting, exactly+ // like a `workdir`; a relative one cannot prove a checkout.+ assert_eq!(+ absolute_tool_location_paths(+ AgentKind::GeminiCli,+ &shell(json!({"command": "ls", "dir_path": "/other/repo"})),+ "/repo"+ ),+ Some(vec!["/other/repo".to_owned()])+ );+ assert_eq!(+ absolute_tool_location_paths(+ AgentKind::GeminiCli,+ &shell(json!({"command": "ls", "dir_path": "sub"})),+ "/repo"+ ),+ None+ );+ }++ #[test]+ fn globstar_reaches_lets_double_star_span_any_depth() {+ assert!(globstar_reaches("/repo/**/*.md", "/repo/docs/adr", false));+ assert!(globstar_reaches("/**", "/repo/docs/adr", false));+ assert!(globstar_reaches("/repo/d*/**", "/repo/docs/adr", false));+ assert!(globstar_reaches(+ "/repo/docs/adr/sub/**",+ "/repo/docs/adr",+ false+ ));+ assert!(globstar_reaches("C:/**/x", "C:/Docs", true));+ assert!(!globstar_reaches("/repo/src/**", "/repo/docs/adr", false));+ assert!(!globstar_reaches("/other/**", "/repo/docs/adr", false));+ // Without `**` it is exactly `glob_reaches`.+ assert!(!globstar_reaches("/repo/*.md", "/repo/docs/adr", false));+ assert!(globstar_reaches("/repo/*/adr/x", "/repo/docs/adr", false));+ // A shell glob does not recurse: `glob_reaches` keeps that reading.+ assert!(!glob_reaches("/repo/**/*.md", "/repo/docs/adr", false));+ }+/// Claude Code's Windows `PowerShell` tool was classified unknown, so its/// output was captured without `ignore_paths` ever seeing the command/// (#1186). Once it is a shell tool, a command naming an ignored path indiff --git a/crates/ai-memory-hooks/tests/fixtures/capture-policy.json b/crates/ai-memory-hooks/tests/fixtures/capture-policy.jsonindex 5c1d0c93..8879855b 100644--- a/crates/ai-memory-hooks/tests/fixtures/capture-policy.json+++ b/crates/ai-memory-hooks/tests/fixtures/capture-policy.json@@ -46,7 +46,34 @@{"agent":"codex","payload":{"tool_name":"Bash","tool_input":{"command":"secret/a"}},"disposition":"drop","tool_family":"non-file","extraction_state":"extracted","path_count":0},{"agent":"codex","payload":{"tool_name":"FutureTool","tool_input":{"path":"secret/a"}},"disposition":"keep","tool_family":"unknown","extraction_state":"extracted","path_count":0},{"agent":"codex","payload":{"tool_name":"Edit","tool_input":{"nested":{"path":"secret/a"}}},"disposition":"metadata-only","tool_family":"file","extraction_state":"missing-or-malformed","path_count":0},- {"agent":"claude-code","payload":{"tool_name":"Edit","tool_input":{"file_path":"//repo/secret/a"}},"disposition":"drop","tool_family":"file","extraction_state":"extracted","path_count":1}+ {"agent":"claude-code","payload":{"tool_name":"Edit","tool_input":{"file_path":"//repo/secret/a"}},"disposition":"drop","tool_family":"file","extraction_state":"extracted","path_count":1},+ {"agent":"gemini-cli","payload":{"tool_name":"read_many_files","tool_input":{"include":["secret/a"]}},"disposition":"drop","tool_family":"file","extraction_state":"extracted","path_count":1},+ {"agent":"gemini-cli","payload":{"tool_name":"read_many_files","tool_input":{"include":["public/a","secret/a"]}},"disposition":"drop","tool_family":"file","extraction_state":"extracted","path_count":2},+ {"agent":"gemini-cli","payload":{"tool_name":"read_many_files","tool_input":{"include":["**/*.md"]}},"disposition":"drop","tool_family":"file","extraction_state":"extracted","path_count":1},+ {"agent":"gemini-cli","payload":{"tool_name":"read_many_files","tool_input":{"include":["s*/a"]}},"disposition":"drop","tool_family":"file","extraction_state":"extracted","path_count":1},+ {"agent":"gemini-cli","payload":{"tool_name":"read_many_files","tool_input":{"include":["SECRET/a"]}},"disposition":"drop","tool_family":"file","extraction_state":"extracted","path_count":1},+ {"agent":"gemini-cli","payload":{"tool_name":"read_many_files","tool_input":{"include":["src/**/*.rs","*.md","public/a"],"exclude":["secret/**"]}},"disposition":"keep","tool_family":"file","extraction_state":"extracted","path_count":3},+ {"agent":"gemini-cli","payload":{"tool_name":"read_many_files","tool_input":{"paths":["secret/a"]}},"disposition":"drop","tool_family":"file","extraction_state":"extracted","path_count":1},+ {"agent":"gemini-cli","payload":{"tool_name":"read_many_files","tool_input":{"include":"secret/a"}},"disposition":"metadata-only","tool_family":"file","extraction_state":"missing-or-malformed","path_count":0},+ {"agent":"gemini-cli","payload":{"tool_name":"run_shell_command","tool_input":{"command":"cat secret/a"}},"disposition":"drop","tool_family":"non-file","extraction_state":"extracted","path_count":0},+ {"agent":"gemini-cli","payload":{"tool_name":"run_shell_command","tool_input":{"command":"cat a","dir_path":"secret"}},"disposition":"drop","tool_family":"non-file","extraction_state":"extracted","path_count":0},+ {"agent":"gemini-cli","payload":{"tool_name":"run_shell_command","tool_input":{"command":"cat public/a"}},"disposition":"keep","tool_family":"non-file","extraction_state":"extracted","path_count":0},+ {"agent":"gemini-cli","payload":{"tool_name":"list_directory","tool_input":{"dir_path":"secret"}},"disposition":"drop","tool_family":"search-list","extraction_state":"not-applicable","path_count":0},+ {"agent":"gemini-cli","payload":{"tool_name":"search_file_content","tool_input":{"pattern":"x"}},"disposition":"drop","tool_family":"search-list","extraction_state":"not-applicable","path_count":0},+ {"agent":"open-code","payload":{"tool":"read_many_files","args":{"include":["secret/a"]}},"disposition":"drop","tool_family":"file","extraction_state":"extracted","path_count":1},+ {"agent":"open-code","payload":{"tool":"read_many_files","args":{"include":["public/a","secret/a"]}},"disposition":"drop","tool_family":"file","extraction_state":"extracted","path_count":2},+ {"agent":"open-code","payload":{"tool":"read_many_files","args":{"include":["**/*.md"]}},"disposition":"drop","tool_family":"file","extraction_state":"extracted","path_count":1},+ {"agent":"open-code","payload":{"tool":"read_many_files","args":{"include":["s*/a"]}},"disposition":"drop","tool_family":"file","extraction_state":"extracted","path_count":1},+ {"agent":"open-code","payload":{"tool":"read_many_files","args":{"include":["SECRET/a"]}},"disposition":"drop","tool_family":"file","extraction_state":"extracted","path_count":1},+ {"agent":"open-code","payload":{"tool":"read_many_files","args":{"include":["src/**/*.rs","*.md","public/a"],"exclude":["secret/**"]}},"disposition":"keep","tool_family":"file","extraction_state":"extracted","path_count":3},+ {"agent":"open-code","payload":{"tool":"read_many_files","args":{"paths":["secret/a"]}},"disposition":"drop","tool_family":"file","extraction_state":"extracted","path_count":1},+ {"agent":"open-code","payload":{"tool":"read_many_files","args":{"include":"secret/a"}},"disposition":"metadata-only","tool_family":"file","extraction_state":"missing-or-malformed","path_count":0},+ {"agent":"open-code","payload":{"tool":"run_shell_command","args":{"command":"cat secret/a"}},"disposition":"drop","tool_family":"non-file","extraction_state":"extracted","path_count":0},+ {"agent":"open-code","payload":{"tool":"run_shell_command","args":{"command":"cat a","dir_path":"secret"}},"disposition":"drop","tool_family":"non-file","extraction_state":"extracted","path_count":0},+ {"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}],"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.",@@ -108,7 +135,10 @@{"payload":{"tool":"bash","args":{"command":"cat ../adr/0001.md","workdir":"docs/x"}},"disposition":"drop"},{"payload":{"tool":"bash","args":{"command":"cat docs/adr/0001.md","workdir":" "}},"disposition":"drop"},{"payload":{"tool":"bash","args":{"command":"cat docs/adr/0001.md","workdir":"sub"}},"disposition":"keep"},- {"payload":{"tool":"bash","args":{"command":"cat 0001.md","workdir":7}},"disposition":"keep"}+ {"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"}]},"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 8b126ccd..08966495 100644--- a/docs/marker-file.md+++ b/docs/marker-file.md@@ -494,8 +494,18 @@ That form contains only bounded routing/tool/decision metadata, never paths,patterns, arguments, output, errors, titles, or nested payload. Unknown toolsretain current behavior.+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 `**`+reaching any depth. ai-memory reads every entry and drops the whole call when+any one names an ignored path or could expand into one, matching case-insensitively+like Gemini: `docs/adr/x.md`, `docs/*/x.md`, `docs/**` and `**/*.md` are+all 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+syntax the matcher cannot evaluate (`[…]`, `{…}`, `(…)`, `!`) makes the call+**metadata-only**.+Recognized shell tools (`Bash`, `PowerShell`, `shell`, `exec`, `execute_bash`,-`terminal`, …) have no path field, so the command line is split into words lexically, the way+`terminal`, Gemini CLI's `run_shell_command`, …) have no path field, so the command line is split into words lexically, the waya POSIX shell quotes and separates them, without expanding or running anything.Codex shell calls, including its `exec_command` path, reach hooks as `Bash`with the command in `tool_input.command`.@@ -504,7 +514,7 @@ with spaces stays whole, up to 256 characters) and also splits each element onits own, so a `bash -lc "<script>"` script is read like any command line. Eachargument that is not a flag, plus the value of a `--flag=value`, `NAME=value`,or PowerShell `-Param:value` word, is resolved like a file-tool path: from the tool's own-`workdir` argument when it has one, otherwise from the event's `cwd`. If+`workdir` argument (Gemini CLI's `dir_path`) when it has one, otherwise from the event's `cwd`. Ifone 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 underdiff --git a/docs/security-boundaries.md b/docs/security-boundaries.mdindex 41249460..b730ac9b 100644--- a/docs/security-boundaries.md+++ b/docs/security-boundaries.md@@ -65,7 +65,7 @@ means the boundary is not built yet.| 10e | Reconcile-tombstone of a disk-deleted page is opt-in, soft, scope-bound, and false-positive-guarded (invariant #16, #929 delete-half) | `ai-memory-wiki/src/watcher.rs` — off by default (`[maintenance] reconcile_tombstones_deleted_pages`); a page is tombstoned only after its file is observed missing on two consecutive passes (streak resets if the path's `PageId` changed), re-verified against the live filesystem immediately before acting, with reserved/indexed-but-unwalked paths (`bootstrap.md`, `_meta.md`, `_pending/`, `sessions/*.md`) excluded and a circuit breaker that skips a whole scope when `> max(3, 50%)` of its pages look missing or a non-partial walk finds nothing; `ai-memory-store/src/ops.rs` `soft_delete_for_reconcile_if_latest` is a soft tombstone (`is_latest=0` + `superseded_at`, decay's shape) through the single writer actor in one transaction — never a filesystem write — scoped to the page's own `(workspace_id, project_id)` | `watcher.rs` `reconcile_delete_disabled_by_default_never_tombstones_a_missing_page`, `reconcile_delete_cross_project_isolation` (project A's deletion never tombstones project B's same-path page), `reconcile_tombstone_refuses_a_stale_latest_id`, `reconcile_tombstone_refuses_when_the_file_still_exists`, `reconcile_tombstone_never_touches_the_filesystem`; `ops.rs` `reconcile_tombstone_resurrects_instead_of_orphaning_on_recreate` | STRONG || 10f | Session-time repair is scope-bound (no cross-project timestamp rewrite) | `ai-memory-store/src/ops.rs` `repair_session_times` — every candidate is read back and updated with `WHERE id=? AND workspace_id=? AND project_id=?`; a row outside that 3-tuple is reported `NotFound` and untouched, same code path as an id that does not exist at all; `ai-memory-mcp/src/admin.rs` `handle_repair_session_times` passes the caller's `(workspace, project)` straight through, never the candidate's own | `ops.rs` `repair_session_times_does_not_touch_a_session_of_another_project` (control: sibling session in-scope repaired in the same batch); `admin_repair_session_times.rs` `cross_project_session_is_not_found_and_untouched`; both bite-checked by dropping the `project_id` filter | STRONG || 11a | Hook backpressure (202/429) + bounded fan-out (invariant #5) | `ai-memory-hooks/src/router.rs` semaphore→429, 202 immediately, `MAX_HOOK_BATCH_ITEMS`, bounded LRU limiter; batch acknowledgements retain only processed indices and classify terminal drops without consuming capacity | `router.rs` `handle_hook_returns_429_when_ingest_saturated`, `ingest_rate_limiter_is_bounded`; `router.rs` `handle_hook_batch_skips_rate_limited_first_item_and_accepts_later_source`, `handle_hook_batch_reports_failed_index_after_rate_limited_skip`, `handle_hook_batch_drops_subagent_events_before_capacity_check` (each asserts result indices and includes an accepted control) | STRONG |-| 11b | Capture exclusions drop before storage | `ai-memory-hooks` `capture_policy.rs` `inspect`→`Drop` (before semaphore/spawn), including shell commands whose arguments name an ignored path (`match_command`; argv elements matched whole and tokenized); an invalid marker makes file and shell calls metadata-only, and the server admits a metadata-only shell body only under an invalid marker (`router.rs` `metadata_protocol_is_legal`); generated OpenCode/OMP/Pi/OpenClaw integrations mirror it in `render_shared.rs` `ts_capture_policy_v1` (`captureMatchCommand`). Candidate/argument flavor (`Flavor::Posix` vs `Flavor::Windows`, used to pick which `ignore_paths` patterns even apply) is derived from the **host** (the cwd), never from the candidate string alone — a POSIX-host candidate spelled with a leading `//` is collapsed to a single `/` before flavor detection (`normalize_candidate`; TS `captureNormalize`'s `windowsHost` parameter, sourced from `captureHostWindows(cwd)`), fixing GHSA-vh98 (a `//`-prefixed candidate used to self-classify as `Flavor::Windows` regardless of host, matching zero POSIX patterns and being captured instead of dropped); a genuine Windows/UNC host (cwd itself windows-flavored) is unaffected; `hook.rs` `--check-capture` preflight uses the same capture policy before any spool or spawn | `capture_policy.rs` per-agent `…honors_exclusions` tests, `shell_fixture_vectors` (shared `capture-policy.json` `shell` drop/keep vectors: tool aliases incl. OpenClaw/Devin `exec` and `PowerShell`, `workdir`, glob directories, Windows paths, and a POSIX-host leading-`//` command via `/{root}/docs/adr/x.md`), `shell_tool_shapes_of_every_adapter_honor_exclusions`, `claude_code_powershell_honors_ignored_paths_on_a_windows_host` (#1186: Windows-spelled and `-Param:value` paths drop, public-path and `git status` controls kept, invalid marker metadata-only), `fixture_vectors` (incl. TS-adapter `bash`/`exec` vectors, and `normalization`'s leading-`//` POSIX vectors alongside the kept Windows-cwd UNC vectors as the no-regression control); `shell_matching_is_off_when_inactive_and_fails_closed_when_invalid_or_over_budget`; `router.rs` `capture_protocol_shell_decisions_survive_server_reinspection`, `capture_protocol_invalid_marker_shell_is_metadata_only` (active metadata-only shell refused, older client's invalid-marker keep stripped, commandless control kept), `capture_protocol_unparseable_marker_strips_shell_events` (server fallback for an unparseable marker), `capture_protocol_invalid_shell_metadata_claim_must_be_canonical` (a stripped shell claim with a path count or non-`extracted` state is refused); `capture_policy.rs` `invalid_marker_strips_shell_calls_with_unparseable_commands`, `long_bash_lc_script_in_argv_is_not_dropped_by_the_match_budget`, **`a_leading_double_slash_candidate_does_not_escape_posix_ignore_paths_via_flavor_mismatch`** (GHSA-vh98 adversarial: attempts the `//repo/secret/...` violation on a POSIX host, proves it now drops, with a plain-single-slash control and a Windows-hosted genuine-UNC control both still correct — fails on the pre-fix code); `hook.rs` `shell_command_reading_an_ignored_path_is_dropped_before_spool`; `render_shared.rs` `generated_capture_policy_v1_node_runtime_evidence` (runs the same fixture sections, including the GHSA-vh98 vectors, against the emitted TypeScript; `#[ignore]` locally, run with `--ignored` under Node 24 by the Linux CI test job); `hook.rs` `check_capture_refuses_partial_scope_and_excluded_content` (ignored-path and partial-scope violations refused, complete public-path control passes), `check_capture_bounds_marker_hints_and_refuses_oversized_scope` (513-byte hints refused, 512-byte control passes; native routing preserved) | STRONG |+| 11b | Capture exclusions drop before storage | `ai-memory-hooks` `capture_policy.rs` `inspect`→`Drop` (before semaphore/spawn), including shell commands whose arguments name an ignored path (`match_command`; argv elements matched whole and tokenized); an invalid marker makes file and shell calls metadata-only, and the server admits a metadata-only shell body only under an invalid marker (`router.rs` `metadata_protocol_is_legal`); generated OpenCode/OMP/Pi/OpenClaw integrations mirror it in `render_shared.rs` `ts_capture_policy_v1` (`captureMatchCommand`). Candidate/argument flavor (`Flavor::Posix` vs `Flavor::Windows`, used to pick which `ignore_paths` patterns even apply) is derived from the **host** (the cwd), never from the candidate string alone — a POSIX-host candidate spelled with a leading `//` is collapsed to a single `/` before flavor detection (`normalize_candidate`; TS `captureNormalize`'s `windowsHost` parameter, sourced from `captureHostWindows(cwd)`), fixing GHSA-vh98 (a `//`-prefixed candidate used to self-classify as `Flavor::Windows` regardless of host, matching zero POSIX patterns and being captured instead of dropped); a genuine Windows/UNC host (cwd itself windows-flavored) is unaffected; `hook.rs` `--check-capture` preflight uses the same capture policy before any spool or spawn | `capture_policy.rs` per-agent `…honors_exclusions` tests, `shell_fixture_vectors` (shared `capture-policy.json` `shell` drop/keep vectors: tool aliases incl. OpenClaw/Devin `exec` and `PowerShell`, `workdir`, glob directories, Windows paths, and a POSIX-host leading-`//` command via `/{root}/docs/adr/x.md`), `shell_tool_shapes_of_every_adapter_honor_exclusions`, `claude_code_powershell_honors_ignored_paths_on_a_windows_host` (#1186: Windows-spelled and `-Param:value` paths drop, public-path and `git status` controls kept, invalid marker metadata-only), `gemini_cli_shell_and_read_many_files_honor_ignored_paths` (Gemini `run_shell_command` incl. `dir_path`/`directory`, and `read_many_files` globs via `match_globs`: excluded literal, mixed public+excluded, `docs/*`, recursive `**` and case-variant entries drop, an `exclude` list cannot rescue them, unsupported glob syntax and malformed lists are metadata-only, public entries kept, POSIX and Windows hosts) and `globstar_reaches_lets_double_star_span_any_depth`, `fixture_vectors` (incl. TS-adapter `bash`/`exec` vectors, and `normalization`'s leading-`//` POSIX vectors alongside the kept Windows-cwd UNC vectors as the no-regression control); `shell_matching_is_off_when_inactive_and_fails_closed_when_invalid_or_over_budget`; `router.rs` `capture_protocol_shell_decisions_survive_server_reinspection`, `capture_protocol_invalid_marker_shell_is_metadata_only` (active metadata-only shell refused, older client's invalid-marker keep stripped, commandless control kept), `capture_protocol_unparseable_marker_strips_shell_events` (server fallback for an unparseable marker), `capture_protocol_invalid_shell_metadata_claim_must_be_canonical` (a stripped shell claim with a path count or non-`extracted` state is refused); `capture_policy.rs` `invalid_marker_strips_shell_calls_with_unparseable_commands`, `long_bash_lc_script_in_argv_is_not_dropped_by_the_match_budget`, **`a_leading_double_slash_candidate_does_not_escape_posix_ignore_paths_via_flavor_mismatch`** (GHSA-vh98 adversarial: attempts the `//repo/secret/...` violation on a POSIX host, proves it now drops, with a plain-single-slash control and a Windows-hosted genuine-UNC control both still correct — fails on the pre-fix code); `hook.rs` `shell_command_reading_an_ignored_path_is_dropped_before_spool`, `gemini_cli_shell_and_read_many_files_exclusions_drop_before_spool` (pre- and post-tool events with an excluded path are never spooled; public controls spool with their family); `render_shared.rs` `generated_capture_policy_v1_node_runtime_evidence` (runs the same fixture sections, including the GHSA-vh98 and Gemini `read_many_files`/`run_shell_command` vectors, plus unsupported-glob, empty-list and recursive-`**` checks, against the emitted TypeScript; `#[ignore]` locally, run with `--ignored` under Node 24 by the Linux CI test job); `hook.rs` `check_capture_refuses_partial_scope_and_excluded_content` (ignored-path and partial-scope violations refused, complete public-path control passes), `check_capture_bounds_marker_hints_and_refuses_oversized_scope` (513-byte hints refused, 512-byte control passes; native routing preserved) | STRONG || 11c | Capture hook ≤200ms budget (invariant #5) | `hooks/_lib.sh` capture path `curl --max-time 0.2` (context-fetch 1.0s and background drain 2.0s are separate, larger-budget paths) | none (shell-script timeout; hard to unit-test) — watch on any capture-path change | WATCH || 11d | Hook server-profile routing: a marker-selected server gets only its own capture and its own token (#992) | `ai-memory-cli/src/server_profiles.rs` `resolve` (validated `ProfileName`, strict `servers.toml` parse, `roots` required once two profiles exist, component-wise root match) and `marker.rs` `find_server_selection` (inherited down the tree, any value shape counts); `commands/hook.rs` `resolve_hook_route` drops a `Rejected` route before spool, handoff and backfill, and hands the drainer no live token for a profile route; `commands/hook_spool.rs` `static_retry_token` (a profile entry retries only with its own stored token), the loopback reroot skip, and chunk splitting on `profile`; generated TS `captureServerRouted` drops a routed repository and gates `fetchHandoff`; `hooks/_lib.sh` `ai_memory_server_routed` (flag refused by `ai_memory_post_hook`/`ai_memory_get_handoff`) and `hooks/lib/ai-memory-hook.ps1` `Test-AiMemoryServerRouted` drop it in the script hooks; `--check-capture` refuses a rejected server-profile selection without printing its token or URL | `hook.rs` `each_repository_spools_to_its_own_profile_with_its_own_token`, `a_selection_that_does_not_resolve_emits_nothing` (unknown / tokenless / outside roots / roots required / invalid name, plus a resolving control), `session_start_handoff_comes_from_the_profile_server_only`, `a_repository_without_a_server_key_keeps_the_install_default`; `hook_spool.rs` `a_profile_entry_is_never_retried_with_the_install_live_token` (server B accepts exactly the install's live token and must still not get it), `a_profile_entry_recovers_with_its_own_rotated_token` (control), `a_profile_entry_on_a_dead_loopback_port_is_not_rerouted_to_the_default`, `profile_and_default_entries_at_one_address_ride_separate_batches`; `server_profiles.rs` roots/registry/name tests; `marker.rs` `nested_markers_without_server_inherit_the_ancestor_selection`; `install_hooks.rs` `generated_integrations_fail_closed_on_a_server_profile_marker`, `openclaw_plugin.rs` `openclaw_plugin_fails_closed_on_a_server_profile_marker`, and the `server-routed-*` checks in `generated_capture_policy_v1_node_runtime_evidence`; `hook.rs` `an_event_without_a_payload_cwd_routes_by_the_process_cwd`, `a_refused_route_prints_nothing_for_kimi_user_prompts`; `hook_spool.rs` `a_profile_entry_is_not_retried_with_a_token_issued_for_a_new_url`; `server_profiles.rs` `changing_the_url_without_a_token_discards_the_old_token`, `omitted_roots_keep_the_registered_ones`; `marker.rs` `outside_home_the_walk_reaches_a_marker_above_the_checkout_root`, `encoding_noise_cannot_hide_a_server_key`, `an_unreadable_marker_is_a_refused_selection`; `backfill.rs` `a_spawned_backfill_authenticates_like_the_hook_that_spawned_it`; `tests/hooks/test_lib.sh` "server profiles (#992)" section; `hook.rs` `a_mixed_spool_drains_each_event_only_to_its_own_server` (two token-gated servers, one spool, one drain: each server receives exactly its own event with exactly its own bearer); `tests/suite/server_profiles.rs` (the built binary: `server add` → `hook` spools to the profile with its token, unknown profile spools nothing, `uninstall` removes the tokens; `two_real_servers_each_receive_only_their_own_repository` runs two real `ai-memory serve` children with different root tokens and checks on each server which repository landed there); `ai-memory-hooks` `powershell_server_routed.rs` `server_routed_guard_mirrors_the_native_walk` (runs `Test-AiMemoryServerRouted` under real PowerShell: inherited, BOM, look-alike keys, the `$HOME` boundary with its control, past a checkout root outside home, current directory); `hook.rs` `a_selection_that_does_not_resolve_emits_nothing` (rejected routes refuse preflight; resolved profile control passes); `tests/hooks/test_lib.sh` and `powershell_home.rs` `a_trailing_separator_on_home_keeps_the_walk_boundary` (a `$HOME` ending in `/` or `\` keeps both script walks at home: a `server` marker above it routes nothing, with a root-home control that does) | STRONG for native hooks, generated TS, `.sh` and `.ps1` hooks. Known gap: an older binary draining a shared spool ignores `profile` || 11e | Native self-upgrade archive confinement (#1025) | `ai-memory-cli/src/commands/upgrade.rs` normalizes every tar/zip path, rejects links and special entries, extracts only the binary and `hooks/`, accepts only the exact non-runtime support layout emitted by `release.yml`, and caps both entry count and declared expanded bytes before writing | `upgrade.rs` `release_workflow_support_assets_are_exact_and_ignored` (shipped support paths admitted, near-miss docs/packaging/template paths refused), `extracts_archive_built_like_release_yml` and `extracts_zip_happy_path_with_hooks` (real workflow-shaped archives succeed but support assets are not written), `validate_rejects_path_traversal`, `zip_extract_rejects_path_traversal`, `tar_extract_rejects_declared_expansion_past_limit`, with binary/hooks extraction as the legitimate control | STRONG |