Skip to content

armgit / ECC

Public Imported from GitHub
0

The agent harness performance optimization system. Skills, instincts, memory, security, and research-first development for Claude Code, Codex, Opencode, Cursor and beyond.

fix(gateguard): honour directory-qualified include globs

Jeffrey Montoya1221a8727fc3
diff --git a/docs/gateguard/design-notes.md b/docs/gateguard/design-notes.md
index d0bc90a03..0279f8f54 100644
--- a/docs/gateguard/design-notes.md
+++ b/docs/gateguard/design-notes.md
@@ -300,10 +300,14 @@ a switch).
## Include filters
-Basename include globs (`--include`, `-g`, `--glob`, `-name`, PowerShell
-`-Filter`/`-Include`) that all miss the target stop its stem from crediting. An
-include glob past the matching bounds admits the target (the stem, scope and
-exclusion checks still apply); a malformed one does not.
+Include globs (`--include`, `-g`, `--glob`, `-name`, `tree -P`, PowerShell
+`-Filter`/`-Include`, the `Grep` tool's `glob`) that all miss the target stop
+its stem from crediting. A basename glob is matched against the target's file
+name. A glob with a directory part is matched against the target's path
+relative to the search root only for ripgrep and the `Grep` tool, where a
+leading `**/` lets it start at any directory; the other tools match names only,
+so such a glob admits nothing. An include glob that is malformed, past the
+matching bounds, or one of more than 32 filters admits nothing.
## Glob matching without RegExp
diff --git a/scripts/lib/gateguard-search-evidence.js b/scripts/lib/gateguard-search-evidence.js
index e5dd0c8fc..2be6bb446 100644
--- a/scripts/lib/gateguard-search-evidence.js
+++ b/scripts/lib/gateguard-search-evidence.js
@@ -465,13 +465,37 @@ function filtersAdmitTarget(item, ctx, stem) {
return !exclusions.some(exclusion => exclusionCoversTarget(exclusion, segments, stem));
}
+function scopeRelativeSegments(item, ctx) {
+ const scopes = item.scopes || (item.scope ? [item.scope] : []);
+ const relatives = [];
+ for (const parts of scopes) {
+ const scope = ctx.resolveDir(...parts);
+ if (isInsideDir(ctx.targetKey, scope)) relatives.push(ctx.targetKey.slice(scope.replace(/\/+$/, '').length + 1).toLowerCase().split('/'));
+ }
+ return relatives;
+}
+
+function includeAdmitsTarget(include, item, ctx) {
+ const glob = normalizeFilterGlob(include);
+ const unanchored = glob.replace(/^(?:\*\*\/)+/, '');
+ if (!unanchored) return false;
+ if (isBasenameGlob(unanchored)) {
+ const segments = targetSegments(ctx);
+ return globMatches(unanchored, segments[segments.length - 1] || '', false) === true;
+ }
+ if (!item.pathIncludes) return false;
+ return scopeRelativeSegments(item, ctx).some(segments => {
+ const starts = unanchored === glob ? [0] : segments.map((_, start) => start);
+ return starts.some(start => globMatches(unanchored, segments.slice(start).join('/'), false) === true);
+ });
+}
+
// see docs/gateguard/design-notes.md#include-filters
function includesAdmitTarget(item, ctx) {
const includes = Array.isArray(item.includes) ? item.includes : [];
if (includes.length === 0) return true;
- const segments = targetSegments(ctx);
- const base = segments[segments.length - 1] || '';
- return includes.some(include => globMatches(normalizeFilterGlob(include).replace(/^(?:\*\*\/)+/, ''), base, false) !== false);
+ if (includes.length > MAX_FILTERS_PER_SEARCH) return false;
+ return includes.some(include => includeAdmitsTarget(include, item, ctx));
}
function isBasenameGlob(glob) {
@@ -485,7 +509,7 @@ function grepToolFilters(glob) {
if (part.startsWith('!')) filters.exclusions.push(part.slice(1));
else {
filters.positives.push(part);
- if (isBasenameGlob(part)) filters.includes.push(part);
+ filters.includes.push(part);
}
}
return filters;
@@ -513,7 +537,7 @@ function applyShellFilterArg(kind, args, i, filters) {
indexes.forEach(index => filters.dropped.add(index));
};
const include = value => {
- if (isBasenameGlob(value)) filters.includes.push(value);
+ if (value) filters.includes.push(value);
};
if (kind.startsWith('git ') && GIT_EXCLUDE_PATHSPEC.test(arg)) {
exclude(arg.replace(GIT_EXCLUDE_PATHSPEC, ''), [i]);
@@ -657,7 +681,7 @@ function powershellSearchFilters(kind, args, filters) {
filters.exclusions.push(...filter.globs);
for (let k = i; k <= filter.end; k++) filters.dropped.add(k);
} else {
- filter.globs.filter(isBasenameGlob).forEach(glob => filters.includes.push(glob));
+ filter.globs.filter(Boolean).forEach(glob => filters.includes.push(glob));
}
i = filter.end;
} else if (/^-[A-Za-z]/.test(args[i]) && powershellParam(kind, args[i]).name === null) {
@@ -700,6 +724,7 @@ function createSearchEvidence({ quoteAwareSegments, commandBasename, SHELL_SEGME
dirs: explicit ? [[explicit]] : [],
scope: [explicit || '.'],
detail,
+ pathIncludes: true,
...filters
}];
}
@@ -786,6 +811,7 @@ function createSearchEvidence({ quoteAwareSegments, commandBasename, SHELL_SEGME
detail: tokens.join(' '),
exclusions: filters.exclusions,
includes: filters.includes,
+ pathIncludes: kind === 'rg',
opaque: filters.opaque
});
});
diff --git a/tests/fixtures/gateguard-scenarios/13-bypass-search-filters.json b/tests/fixtures/gateguard-scenarios/13-bypass-search-filters.json
index ac5885c31..a2addb244 100644
--- a/tests/fixtures/gateguard-scenarios/13-bypass-search-filters.json
+++ b/tests/fixtures/gateguard-scenarios/13-bypass-search-filters.json
@@ -1,6 +1,6 @@
{
"name": "bypass-search-filters",
- "description": "Searches that mention the target but never searched it: exclusion globs, bracket classes, PowerShell -Exclude spellings, stdin searches, the target as operand, another directory, a generic stem and a missed include; then three controls whose filters leave the target in scope.",
+ "description": "Searches that mention the target but never searched it: exclusion globs, bracket classes, PowerShell -Exclude spellings, stdin searches, the target as operand, another directory, a generic stem, a missed include and a directory-qualified include of another directory; then four controls whose filters leave the target in scope.",
"files": {
"src/rates/converter.js": "function converterValue(x) {\n return x;\n}\n",
"src/rates/rounding.js": "function roundingValue(x) {\n return x;\n}\n",
@@ -19,7 +19,9 @@
"src/rates/settle.js": "function settleValue(x) {\n return x;\n}\n",
"src/rates/clearing.js": "function clearingValue(x) {\n return x;\n}\n",
"src/rates/netting.js": "function nettingValue(x) {\n return x;\n}\n",
- "lib/rates/fallback.js": "function fallbackValue(x) {\n return x;\n}\n"
+ "lib/rates/fallback.js": "function fallbackValue(x) {\n return x;\n}\n",
+ "lib/rates/sweep.js": "function sweepValue(x) {\n return x;\n}\n",
+ "src/rates/sweep.js": "function sweepValue(x) {\n return x;\n}\n"
},
"steps": [
{
@@ -1827,6 +1829,220 @@
"local-callers",
"quote-instruction"
]
+ },
+ {
+ "id": "path-include-other-directory",
+ "note": "A directory-qualified include limits the search to another directory.",
+ "transcript": [
+ {
+ "type": "user",
+ "uuid": "bypass-search-filters-069",
+ "sessionId": "sess-bypass-filters",
+ "isSidechain": false,
+ "promptId": "prompt-filters-1",
+ "message": {
+ "role": "user",
+ "content": [
+ {
+ "type": "tool_result",
+ "tool_use_id": "toolu_bypasssearchfilters_034",
+ "content": "ok"
+ }
+ ]
+ }
+ },
+ {
+ "type": "assistant",
+ "uuid": "bypass-search-filters-070",
+ "sessionId": "sess-bypass-filters",
+ "isSidechain": false,
+ "message": {
+ "id": "msg_bypasssearchfilters_35",
+ "type": "message",
+ "role": "assistant",
+ "model": "claude-sonnet-4-5",
+ "content": [
+ {
+ "type": "tool_use",
+ "id": "toolu_bypasssearchfilters_035",
+ "name": "Bash",
+ "input": {
+ "command": "rg -g 'src/**' sweep ."
+ }
+ }
+ ]
+ }
+ },
+ {
+ "type": "user",
+ "uuid": "bypass-search-filters-071",
+ "sessionId": "sess-bypass-filters",
+ "isSidechain": false,
+ "promptId": "prompt-filters-1",
+ "message": {
+ "role": "user",
+ "content": [
+ {
+ "type": "tool_result",
+ "tool_use_id": "toolu_bypasssearchfilters_035",
+ "content": "ok"
+ }
+ ]
+ }
+ },
+ {
+ "type": "assistant",
+ "uuid": "bypass-search-filters-072",
+ "sessionId": "sess-bypass-filters",
+ "isSidechain": false,
+ "message": {
+ "id": "msg_bypasssearchfilters_36",
+ "type": "message",
+ "role": "assistant",
+ "model": "claude-sonnet-4-5",
+ "content": [
+ {
+ "type": "tool_use",
+ "id": "toolu_bypasssearchfilters_036",
+ "name": "Edit",
+ "input": {
+ "file_path": "{{root}}/lib/rates/sweep.js",
+ "old_string": " return x;",
+ "new_string": " return Math.round(x * 100) / 100;"
+ }
+ }
+ ]
+ }
+ }
+ ],
+ "payload": {
+ "session_id": "sess-bypass-filters",
+ "transcript_path": "{{transcript}}",
+ "cwd": "{{root}}",
+ "permission_mode": "default",
+ "hook_event_name": "PreToolUse",
+ "tool_name": "Edit",
+ "tool_input": {
+ "file_path": "{{root}}/lib/rates/sweep.js",
+ "old_string": " return x;",
+ "new_string": " return Math.round(x * 100) / 100;"
+ },
+ "tool_use_id": "toolu_bypasssearchfilters_036"
+ },
+ "expect": "deny",
+ "mustDeny": true,
+ "redundant": false,
+ "relevantQuestions": [
+ "local-callers",
+ "quote-instruction"
+ ]
+ },
+ {
+ "id": "path-include-matches-target",
+ "note": "Control: the directory-qualified include covers the target, so the search counts.",
+ "transcript": [
+ {
+ "type": "user",
+ "uuid": "bypass-search-filters-073",
+ "sessionId": "sess-bypass-filters",
+ "isSidechain": false,
+ "promptId": "prompt-filters-1",
+ "message": {
+ "role": "user",
+ "content": [
+ {
+ "type": "tool_result",
+ "tool_use_id": "toolu_bypasssearchfilters_036",
+ "content": "ok"
+ }
+ ]
+ }
+ },
+ {
+ "type": "assistant",
+ "uuid": "bypass-search-filters-074",
+ "sessionId": "sess-bypass-filters",
+ "isSidechain": false,
+ "message": {
+ "id": "msg_bypasssearchfilters_37",
+ "type": "message",
+ "role": "assistant",
+ "model": "claude-sonnet-4-5",
+ "content": [
+ {
+ "type": "tool_use",
+ "id": "toolu_bypasssearchfilters_037",
+ "name": "Bash",
+ "input": {
+ "command": "rg -g 'src/**' sweep ."
+ }
+ }
+ ]
+ }
+ },
+ {
+ "type": "user",
+ "uuid": "bypass-search-filters-075",
+ "sessionId": "sess-bypass-filters",
+ "isSidechain": false,
+ "promptId": "prompt-filters-1",
+ "message": {
+ "role": "user",
+ "content": [
+ {
+ "type": "tool_result",
+ "tool_use_id": "toolu_bypasssearchfilters_037",
+ "content": "ok"
+ }
+ ]
+ }
+ },
+ {
+ "type": "assistant",
+ "uuid": "bypass-search-filters-076",
+ "sessionId": "sess-bypass-filters",
+ "isSidechain": false,
+ "message": {
+ "id": "msg_bypasssearchfilters_38",
+ "type": "message",
+ "role": "assistant",
+ "model": "claude-sonnet-4-5",
+ "content": [
+ {
+ "type": "tool_use",
+ "id": "toolu_bypasssearchfilters_038",
+ "name": "Edit",
+ "input": {
+ "file_path": "{{root}}/src/rates/sweep.js",
+ "old_string": " return x;",
+ "new_string": " return Math.round(x * 100) / 100;"
+ }
+ }
+ ]
+ }
+ }
+ ],
+ "payload": {
+ "session_id": "sess-bypass-filters",
+ "transcript_path": "{{transcript}}",
+ "cwd": "{{root}}",
+ "permission_mode": "default",
+ "hook_event_name": "PreToolUse",
+ "tool_name": "Edit",
+ "tool_input": {
+ "file_path": "{{root}}/src/rates/sweep.js",
+ "old_string": " return x;",
+ "new_string": " return Math.round(x * 100) / 100;"
+ },
+ "tool_use_id": "toolu_bypasssearchfilters_038"
+ },
+ "expect": "allow",
+ "mustDeny": false,
+ "redundant": true,
+ "relevantQuestions": [
+ "local-callers",
+ "quote-instruction"
+ ]
}
]
}
diff --git a/tests/hooks/gateguard-fact-force.test.js b/tests/hooks/gateguard-fact-force.test.js
index 8b0100748..04aaa881b 100644
--- a/tests/hooks/gateguard-fact-force.test.js
+++ b/tests/hooks/gateguard-fact-force.test.js
@@ -5632,6 +5632,37 @@ function runTests() {
});
}
+ // --- Directory-qualified include globs restrict the search ---
+ const longInclude = `${'a'.repeat(300)}*.py`;
+ const pathIncludeCases = [
+ ['rg include in another directory', 'lib/fooXjs.js', "rg -g 'src/*.js' fooXjs .", false],
+ ['rg --glob include in another directory', 'src/widget.py', 'rg --glob=docs/*.py widget .', false],
+ ['rg recursive include of another directory', 'src/widget.py', "rg -g 'lib/**' widget .", false],
+ ['rg include anchored at the search root', 'vendor/src/widget.py', "rg -g 'src/*.py' widget .", false],
+ ['rg include that does not cross a directory', 'src/deep/widget.py', "rg -g 'src/*.py' widget .", false],
+ ['rg include relative to a narrower search path', 'src/widget.py', "rg -g 'src/*.py' widget src", false],
+ ['Grep include in another directory', 'src/widget.py', { pattern: 'widget', glob: 'lib/*.py' }, false],
+ ['grep --include matches base names only', 'src/widget.py', 'grep -r --include=src/*.py widget .', false],
+ ['find -name with a slash matches nothing', 'src/widget.py', "find . -name 'src/widget.py'", false],
+ ['PowerShell -Include with a path matches nothing', 'src/widget.py', 'Get-ChildItem -Recurse -Include src/widget.py', false],
+ ['rg include past the length bound admits nothing', 'src/widget.py', `rg -g '${longInclude}' widget .`, false],
+ ['rg include of the target directory', 'src/widget.py', "rg -g 'src/*.py' widget .", true],
+ ['rg include with a leading slash', 'src/widget.py', "rg -g '/src/*.py' widget .", true],
+ ['rg include with a leading **/', 'vendor/src/widget.py', "rg -g '**/src/*.py' widget .", true],
+ ['rg recursive include of the target directory', 'src/deep/widget.py', "rg -g 'src/**' widget .", true],
+ ['rg include relative to the search path', 'src/deep/widget.py', "rg -g 'deep/*.py' widget src", true],
+ ['Grep include of the target directory', 'src/widget.py', { pattern: 'widget', glob: 'src/**/*.py' }, true]
+ ];
+ for (const [label, rel, search, credited] of pathIncludeCases) {
+ gateCase(`path include: ${label}`, () => {
+ const input = typeof search === 'string' ? { command: search } : { ...search, path: creditRoot };
+ const tool = typeof search !== 'string' ? 'Grep' : /^Get-ChildItem/.test(search) ? 'PowerShell' : 'Bash';
+ const out = creditEdit(`${creditRoot}/${rel}`, exclusionTurn(tool, input));
+ if (credited) assertCredited(out, label);
+ else assertNotCredited(out, label);
+ });
+ }
+
// --- Every PowerShell spelling of -Exclude is an exclusion ---
const psExcludeDenied = [
'Get-ChildItem -Recurse -ex widget.py',