From 47978ff53b89453e91aad86339f34fd1234ef341 Mon Sep 17 00:00:00 2001 From: Heewon Oh Date: Sun, 15 Nov 2026 06:22:43 +0900 Subject: [PATCH 1/2] fix(agents): quarantine bad bus files while polling, validate tester fields, contain guard paths, reject non-executing evidence (AGT-3441) - agentBus: a malformed message file no longer aborts every poll; it is quarantined with file/execution/reason and marked processed - tester: success only for literal true, counts finite and non-negative, list entries strings (clipped, capped), error a string - pipelineGuards: changed paths that are absolute-outside, traverse, or leave through a symlink are dropped from file-reading guards and reported by a blocking pathContainment result (guardPaths.ts) - workerValidationEvidence: comments, no-op builtins, lookups and help/version/list/collect-only/make dry-run forms are not validation evidence --- src/agents/agentBus.ts | 48 +++++++++---- src/agents/artifactValidation.test.ts | 36 +++++++++- src/agents/guardPaths.test.ts | 53 +++++++++++++++ src/agents/guardPaths.ts | 75 +++++++++++++++++++++ src/agents/pipelineGuards.test.ts | 38 +++++++++++ src/agents/pipelineGuards.ts | 25 +++---- src/agents/tester.test.ts | 30 +++++++++ src/agents/tester.ts | 42 +++++++++--- src/agents/testerFixPromptBudget.test.ts | 7 +- src/agents/workerValidationEvidence.test.ts | 36 ++++++++++ src/agents/workerValidationEvidence.ts | 14 +++- 11 files changed, 361 insertions(+), 43 deletions(-) create mode 100644 src/agents/guardPaths.test.ts create mode 100644 src/agents/guardPaths.ts diff --git a/src/agents/agentBus.ts b/src/agents/agentBus.ts index 16eb3b3a..0d45c451 100644 --- a/src/agents/agentBus.ts +++ b/src/agents/agentBus.ts @@ -437,8 +437,19 @@ export class AgentBus { .sort(); for (const file of newFiles) { - const content = await fs.readFile(resolve(this.messagesPath, file), 'utf-8'); - const message: AgentMessage = JSON.parse(content); + // One bad file must not wedge the queue: a parse or shape failure used + // to escape to the outer catch, abort this pass and, because the file + // was never marked processed, abort every later pass too. + let message: AgentMessage; + try { + const parsed: unknown = JSON.parse(await fs.readFile(resolve(this.messagesPath, file), 'utf-8')); + if (!isWellFormedMessage(parsed)) throw new Error('message is missing required fields'); + message = parsed; + } catch (error) { + await this.quarantineMessageFile(file, error); + this.markProcessed(file); + continue; + } // Invoke listeners const callbacks = this.listeners.get(message.type); @@ -452,16 +463,32 @@ export class AgentBus { } } - this.processedMessageIds.add(file); - if (this.processedMessageIds.size > MAX_RETAINED_MESSAGES) { - this.processedMessageIds.delete(this.processedMessageIds.values().next().value!); - } + this.markProcessed(file); } } catch { // Ignore } } + private markProcessed(file: string): void { + this.processedMessageIds.add(file); + if (this.processedMessageIds.size > MAX_RETAINED_MESSAGES) { + this.processedMessageIds.delete(this.processedMessageIds.values().next().value!); + } + } + + /** + * Move a malformed message file beside the messages dir (kept for inspection, + * never deleted) and log which file and why. + */ + private async quarantineMessageFile(file: string, reason: unknown): Promise { + const quarantineDir = resolve(this.messagesPath, '..', QUARANTINE_DIRNAME); + const detail = reason instanceof Error ? reason.message : String(reason); + console.warn(`[AgentBus] Quarantined malformed message ${file} (execution ${this.executionId}): ${detail.slice(0, 200)}`); + await fs.mkdir(quarantineDir, { recursive: true }).catch(() => {}); + await fs.rename(resolve(this.messagesPath, file), join(quarantineDir, file)).catch(() => {}); + } + /** * Get all messages */ @@ -478,15 +505,10 @@ export class AgentBus { throw new Error('message is missing required fields'); } messages.push(parsed); - } catch { + } catch (error) { // Malformed history is quarantined beside the messages dir, so a // corrupt payload cannot wedge the whole retrieval pass. - const quarantineDir = resolve(this.messagesPath, '..', QUARANTINE_DIRNAME); - await fs.mkdir(quarantineDir, { recursive: true }).catch(() => {}); - await fs.rename( - resolve(this.messagesPath, file), - join(quarantineDir, file), - ).catch(() => {}); + await this.quarantineMessageFile(file, error); } } diff --git a/src/agents/artifactValidation.test.ts b/src/agents/artifactValidation.test.ts index 75f776ef..dc7da643 100644 --- a/src/agents/artifactValidation.test.ts +++ b/src/agents/artifactValidation.test.ts @@ -4,7 +4,7 @@ import { mkdtemp, readFile, readdir, rm, writeFile } from 'node:fs/promises'; import { tmpdir } from 'node:os'; import { join, resolve } from 'node:path'; -import { afterEach, beforeEach, describe, expect, it } from 'vitest'; +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; import { AgentBus, BUS_DIR } from './agentBus.js'; import { parseAuditorOutput } from './auditor.js'; import { parseDocumenterOutput } from './documenter.js'; @@ -64,3 +64,37 @@ describe('auditor/documenter non-string output guard', () => { expect(result.summary).toBe('Documenter output was not a string'); }); }); + +describe('agentBus polling skips malformed message files (AGT-3441)', () => { + it('quarantines a malformed file and still delivers the later valid message', async () => { + const executionId = `test-poll-${Date.now()}-${Math.random().toString(36).slice(2)}`; + const publisher = new AgentBus(executionId); + const subscriber = new AgentBus(executionId); + await publisher.init('workflow-123'); + const messagesDir = resolve(BUS_DIR, executionId, 'messages'); + // Sorted before every published file, so it is the first one polled. + await writeFile(join(messagesDir, '0000-corrupt.json'), '{ not json', 'utf-8'); + await writeFile(join(messagesDir, '0001-wrong-shape.json'), JSON.stringify({ type: 'log' }), 'utf-8'); + + const seen: unknown[] = []; + subscriber.on('log', (msg) => { seen.push(msg.payload); }); + const warn = vi.spyOn(console, 'warn').mockImplementation(() => undefined); + try { + await publisher.publish('log', 'step-1', 'after the bad files'); + await subscriber.pollOnce(); + expect(seen).toEqual(['after the bad files']); + + const quarantined = await readdir(resolve(messagesDir, '..', 'quarantine')); + expect(quarantined).toEqual(expect.arrayContaining(['0000-corrupt.json', '0001-wrong-shape.json'])); + expect(warn).toHaveBeenCalledWith(expect.stringContaining('0000-corrupt.json')); + + // A later poll sees the next message too; the bad files never come back. + await publisher.publish('log', 'step-2', 'second'); + await subscriber.pollOnce(); + expect(seen).toEqual(['after the bad files', 'second']); + } finally { + warn.mockRestore(); + await publisher.cleanup(); + } + }); +}); diff --git a/src/agents/guardPaths.test.ts b/src/agents/guardPaths.test.ts new file mode 100644 index 00000000..ecb087c0 --- /dev/null +++ b/src/agents/guardPaths.test.ts @@ -0,0 +1,53 @@ +import { mkdirSync, mkdtempSync, rmSync, symlinkSync, writeFileSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; +import { afterEach, beforeEach, describe, expect, it } from 'vitest'; +import { containedProjectPath, lexicallyContainedPath, partitionChangedPaths } from './guardPaths.js'; + +describe('guard path containment (AGT-3441)', () => { + let root: string; + let project: string; + let outside: string; + + beforeEach(() => { + root = mkdtempSync(join(tmpdir(), 'openswarm-guardpaths-')); + project = join(root, 'project'); + outside = join(root, 'outside'); + mkdirSync(join(project, 'src'), { recursive: true }); + mkdirSync(outside, { recursive: true }); + writeFileSync(join(project, 'src', 'a.ts'), 'export const a = 1;\n'); + writeFileSync(join(outside, 'secret.ts'), 'export const secret = 1;\n'); + symlinkSync(outside, join(project, 'linked-dir')); + symlinkSync(join(outside, 'secret.ts'), join(project, 'src', 'linked-file.ts')); + }); + + afterEach(() => rmSync(root, { recursive: true, force: true })); + + it('accepts project-relative paths, including absolute ones inside the project and new files', () => { + expect(containedProjectPath(project, 'src/a.ts')).toBe('src/a.ts'); + expect(containedProjectPath(project, './src/a.ts')).toBe('src/a.ts'); + expect(containedProjectPath(project, join(project, 'src', 'a.ts'))).toBe('src/a.ts'); + expect(containedProjectPath(project, 'src/new/deep/file.ts')).toBe('src/new/deep/file.ts'); + }); + + it('rejects traversal, absolute outside paths, empty and NUL paths lexically', () => { + for (const bad of ['../outside/secret.ts', 'src/../../outside/secret.ts', join(outside, 'secret.ts'), '', '.', 'src/a.ts\0x', '..']) { + expect(lexicallyContainedPath(project, bad)).toBeNull(); + expect(containedProjectPath(project, bad)).toBeNull(); + } + }); + + it('rejects paths that leave the project through a symlinked directory or file', () => { + expect(lexicallyContainedPath(project, 'linked-dir/secret.ts')).toBe('linked-dir/secret.ts'); + expect(containedProjectPath(project, 'linked-dir/secret.ts')).toBeNull(); + expect(containedProjectPath(project, 'linked-dir/not-yet.ts')).toBeNull(); + expect(containedProjectPath(project, 'src/linked-file.ts')).toBeNull(); + }); + + it('partitions a changed-file list', () => { + expect(partitionChangedPaths(project, ['src/a.ts', '../x.ts', 'linked-dir/secret.ts'])).toEqual({ + contained: ['src/a.ts'], + escaped: ['../x.ts', 'linked-dir/secret.ts'], + }); + }); +}); diff --git a/src/agents/guardPaths.ts b/src/agents/guardPaths.ts new file mode 100644 index 00000000..1c21f1fa --- /dev/null +++ b/src/agents/guardPaths.ts @@ -0,0 +1,75 @@ +// ============================================ +// OpenSwarm - Pipeline guard path containment (AGT-3441) +// Changed-file paths come from the worker's report. Before a guard opens one, +// it must name a file inside the project: not absolute, no `..` escape, and no +// symlinked ancestor (or the file itself) that resolves outside the project. +// ============================================ + +import { realpathSync } from 'node:fs'; +import { dirname, isAbsolute, normalize, relative, resolve } from 'node:path'; + +function realpathOrNull(path: string): string | null { + try { + return realpathSync(path); + } catch { + return null; + } +} + +function within(root: string, candidate: string): boolean { + const rel = relative(root, candidate); + return rel === '' || (!rel.startsWith('..') && !isAbsolute(rel)); +} + +/** + * Lexical check: a project-relative path with no absolute prefix, no `..` + * escape and no NUL byte. Returns the normalized relative path, or null. + */ +export function lexicallyContainedPath(projectPath: string, filePath: string): string | null { + if (typeof filePath !== 'string' || filePath.length === 0 || filePath.includes('\0')) return null; + const cleaned = normalize(filePath).replace(/\\/g, '/').replace(/^\.\//, ''); + const absolute = isAbsolute(cleaned) ? cleaned : resolve(projectPath, cleaned); + const rel = normalize(relative(projectPath, absolute)).replace(/\\/g, '/'); + if (rel === '' || rel === '.' || rel === '..' || rel.startsWith('../') || isAbsolute(rel)) return null; + return rel; +} + +/** + * Physical check: the nearest existing path on the way to `rel` (the file + * itself when it exists) must resolve inside the real project root, so a + * symlinked directory or file cannot redirect a guard read outside it. + */ +export function physicallyContained(projectPath: string, rel: string): boolean { + const root = realpathOrNull(projectPath); + if (!root) return false; + let probe = resolve(projectPath, rel); + for (;;) { + const real = realpathOrNull(probe); + if (real) return within(root, real); + const parent = dirname(probe); + if (parent === probe || !within(resolve(projectPath), parent)) return false; + probe = parent; + } +} + +/** Lexical + physical containment; returns the relative path or null. */ +export function containedProjectPath(projectPath: string, filePath: string): string | null { + const rel = lexicallyContainedPath(projectPath, filePath); + return rel && physicallyContained(projectPath, rel) ? rel : null; +} + +/** Split worker-reported changed files into contained and escaping paths. */ +export function partitionChangedPaths( + projectPath: string, + files: readonly string[], +): { contained: string[]; escaped: string[] } { + const contained: string[] = []; + const escaped: string[] = []; + for (const file of files) { + const rel = containedProjectPath(projectPath, file); + if (rel) contained.push(rel); + else escaped.push(String(file)); + } + return { contained, escaped }; +} + diff --git a/src/agents/pipelineGuards.test.ts b/src/agents/pipelineGuards.test.ts index 7cc85a4d..b9022c53 100644 --- a/src/agents/pipelineGuards.test.ts +++ b/src/agents/pipelineGuards.test.ts @@ -828,3 +828,41 @@ describe('pipelineGuards — INT-2388 deterministic guards', () => { }); }); }); + +describe('pipelineGuards — changed paths that escape the project (AGT-3441)', () => { + let root: string; + let repo: string; + + beforeEach(() => { + root = mkdtempSync(join(tmpdir(), 'openswarm-guards-escape-')); + repo = join(root, 'repo'); + mkdirSync(repo, { recursive: true }); + mkdirSync(join(root, 'outside'), { recursive: true }); + writeFileSync(join(root, 'outside', 'evil.ts'), 'const password = "hunter2"; // TODO fake\n'); + execFileSync('git', ['init', '-q', '-b', 'main'], { cwd: repo }); + }); + + afterEach(() => rmSync(root, { recursive: true, force: true })); + + it('blocks and never hands escaping paths to file-reading guards', async () => { + const { symlinkSync } = await import('node:fs'); + symlinkSync(join(root, 'outside'), join(repo, 'link')); + writeFileSync(join(repo, 'ok.ts'), 'export const ok = 1;\n'); + const result = await runGuards( + mockWorker(['ok.ts', '../outside/evil.ts', join(root, 'outside', 'evil.ts'), 'link/evil.ts']), + repo, + { bsDetector: true }, + ); + const containment = result.results.find(r => r.guard === 'pathContainment'); + expect(containment?.blocking).toBe(true); + expect(containment?.issues).toHaveLength(3); + // bsDetector only ever saw ok.ts + expect(guardIssues(result, 'bsDetector').every(issue => issue.includes('ok.ts'))).toBe(true); + }); + + it('adds no containment result when every path is inside the project', async () => { + writeFileSync(join(repo, 'ok.ts'), 'export const ok = 1;\n'); + const result = await runGuards(mockWorker(['ok.ts']), repo, { bsDetector: true }); + expect(result.results.find(r => r.guard === 'pathContainment')).toBeUndefined(); + }); +}); diff --git a/src/agents/pipelineGuards.ts b/src/agents/pipelineGuards.ts index 623dddf6..7bdbb2d5 100644 --- a/src/agents/pipelineGuards.ts +++ b/src/agents/pipelineGuards.ts @@ -5,7 +5,8 @@ import { execFile } from 'node:child_process'; import { readFile } from 'node:fs/promises'; -import { isAbsolute, join, normalize, relative, resolve } from 'node:path'; +import { join } from 'node:path'; +import { containedProjectPath, partitionChangedPaths } from './guardPaths.js'; import { promisify } from 'node:util'; import type { WorkerResult } from './agentPair.js'; import type { PipelineGuardsConfig } from '../core/types.js'; @@ -379,7 +380,7 @@ export async function getAddedLinesForFile(projectPath: string, filePath: string // Fall through to untracked-new handling below. } - if (!isNew) return ''; + if (!isNew || !containedProjectPath(projectPath, filePath)) return ''; try { return await readFile(join(projectPath, filePath), 'utf8'); } catch { @@ -533,14 +534,6 @@ async function literalExistsInHeadSource(projectPath: string, literal: string): } } -function normalizeEvidencePath(projectPath: string, filePath: string): string | null { - const cleaned = normalize(filePath).replace(/\\/g, '/').replace(/^\.\//, ''); - const absolute = isAbsolute(cleaned) ? cleaned : resolve(projectPath, cleaned); - const rel = normalize(relative(projectPath, absolute)).replace(/\\/g, '/'); - if (rel.startsWith('../') || rel === '..' || isAbsolute(rel)) return null; - return rel; -} - async function hasExternalContractEvidence( workerResult: WorkerResult, projectPath: string, @@ -551,7 +544,7 @@ async function hasExternalContractEvidence( const fileRefs = new Map>(); let match: RegExpExecArray | null; while ((match = EVIDENCE_FILE_REF_RE.exec(text))) { - const normalized = normalizeEvidencePath(projectPath, match[1]); + const normalized = containedProjectPath(projectPath, match[1]); if (!normalized) continue; const lineNo = parseInt(match[2], 10); const lines = fileRefs.get(normalized) ?? new Set(); @@ -928,11 +921,11 @@ export async function runGuards( ): Promise { const results: GuardResult[] = []; // Keep the original result for reporting/publication, but never feed - // generated pytest/venv artifacts into guards that open changed files. - const guardWorkerResult = { - ...workerResult, - filesChanged: workerResult.filesChanged.filter((file) => !isEphemeralWorktreeArtifact(file)), - }; + // generated pytest/venv artifacts — or paths that escape the project + // (absolute, `..`, symlinked out) — into guards that open changed files. + const { contained, escaped } = partitionChangedPaths(projectPath, workerResult.filesChanged.filter((file) => !isEphemeralWorktreeArtifact(file))); + const guardWorkerResult = { ...workerResult, filesChanged: contained }; + if (escaped.length > 0) results.push({ passed: false, guard: 'pathContainment', blocking: true, issues: escaped.slice(0, 20).map((file) => `Changed path escapes the project: ${file}`) }); if (config.qualityGate) { console.warn('[Guard:qualityGate] Deprecated: use autonomous.verify baseline-diff verification instead.'); diff --git a/src/agents/tester.test.ts b/src/agents/tester.test.ts index 4d35262e..9e73da1d 100644 --- a/src/agents/tester.test.ts +++ b/src/agents/tester.test.ts @@ -28,3 +28,33 @@ describe('parseTesterOutput fake-pass guard (INT-2521)', () => { expect(parseTesterOutput('2 failed, 3 passed').success).toBe(false); }); }); + +describe('parseTesterOutput validates result field types (AGT-3441)', () => { + const wrap = (obj: unknown) => JSON.stringify({ type: 'result', result: '```json\n' + JSON.stringify(obj) + '\n```' }); + + it('does not coerce a string "false" into success', () => { + const r = parseTesterOutput(wrap({ success: 'false', testsPassed: 1, testsFailed: 2 })); + expect(r.success).toBe(false); + }); + + it('keeps only string list entries, clipped and capped', () => { + const r = parseTesterOutput(wrap({ + success: false, + testsFailed: 1, + failedTests: ['a.test.ts > case', 42, null, { x: 1 }, '', 'z'.repeat(2000)], + suggestions: [{ nested: true }], + })); + expect(r.failedTests?.[0]).toBe('a.test.ts > case'); + expect(r.failedTests).toHaveLength(2); + expect(r.failedTests?.[1].length).toBe(500); + expect(r.suggestions).toBeUndefined(); + }); + + it('drops non-finite or negative counts and non-string errors', () => { + const r = parseTesterOutput(wrap({ success: false, testsPassed: -3, testsFailed: '7', coverage: 'high', error: { code: 1 } })); + expect(r.testsPassed).toBe(0); + expect(r.testsFailed).toBe(0); + expect(r.coverage).toBeUndefined(); + expect(r.error).toBeUndefined(); + }); +}); diff --git a/src/agents/tester.ts b/src/agents/tester.ts index 778360bc..f721b291 100644 --- a/src/agents/tester.ts +++ b/src/agents/tester.ts @@ -267,19 +267,43 @@ function extractResultJson(text: string): TesterResult | null { } } +/** Caps on model-supplied lists so repair feedback stays bounded. */ +const MAX_RESULT_LIST_ITEMS = 50; +const MAX_RESULT_ITEM_CHARS = 500; + +const countField = (value: unknown): number => + typeof value === 'number' && Number.isFinite(value) && value >= 0 ? Math.floor(value) : 0; + +/** Keep only non-empty string entries, clipped; anything else is not a test id or suggestion. */ +function stringListField(value: unknown): string[] | undefined { + if (!Array.isArray(value)) return undefined; + const items = value + .filter((item): item is string => typeof item === 'string' && item.trim().length > 0) + .slice(0, MAX_RESULT_LIST_ITEMS) + .map((item) => item.slice(0, MAX_RESULT_ITEM_CHARS)); + return items.length > 0 ? items : undefined; +} + /** - * Normalize result + * Normalize result. Only schema-valid fields reach repair feedback: `success` + * is true only for a literal boolean true (the string "false" used to coerce + * to true), counts + * must be finite non-negative numbers, list entries must be strings, and + * `error` must be a string. */ -function normalizeResult(parsed: any, output: string): TesterResult { +function normalizeResult(parsed: unknown, output: string): TesterResult | null { + if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) return null; + const raw = parsed as Record; return { - success: Boolean(parsed.success), - testsPassed: typeof parsed.testsPassed === 'number' ? parsed.testsPassed : 0, - testsFailed: typeof parsed.testsFailed === 'number' ? parsed.testsFailed : 0, - coverage: typeof parsed.coverage === 'number' ? parsed.coverage : undefined, + // Fail closed: only a literal true is success. + success: raw.success === true, + testsPassed: countField(raw.testsPassed), + testsFailed: countField(raw.testsFailed), + coverage: typeof raw.coverage === 'number' && Number.isFinite(raw.coverage) ? raw.coverage : undefined, output, - failedTests: Array.isArray(parsed.failedTests) ? parsed.failedTests : undefined, - suggestions: Array.isArray(parsed.suggestions) ? parsed.suggestions : undefined, - error: parsed.error, + failedTests: stringListField(raw.failedTests), + suggestions: stringListField(raw.suggestions), + error: typeof raw.error === 'string' ? raw.error.slice(0, MAX_RESULT_ITEM_CHARS * 4) : undefined, }; } diff --git a/src/agents/testerFixPromptBudget.test.ts b/src/agents/testerFixPromptBudget.test.ts index 51595ae4..e9fd83b7 100644 --- a/src/agents/testerFixPromptBudget.test.ts +++ b/src/agents/testerFixPromptBudget.test.ts @@ -16,13 +16,16 @@ describe('buildTestFixPrompt survives a malformed tester result', () => { // `truncate` reads `.length`, which a number or a plain object does not // have: before the guard the whole repair prompt threw a TypeError and the // self-repair iteration lost its feedback entirely. + // The parser now drops non-string entries (AGT-3441); a result built + // elsewhere can still carry them, so the prompt builder stays tolerant. const parsed = parseTesterOutput(JSON.stringify({ type: 'result', result: JSON.stringify({ success: false, testsPassed: 0, testsFailed: 3, failedTests: [42, { a: 1 }, null] }), })); - expect(parsed.failedTests).toEqual([42, { a: 1 }, null]); + expect(parsed.failedTests).toBeUndefined(); + expect(parsed.testsFailed).toBe(3); - const prompt = buildTestFixPrompt(parsed); + const prompt = buildTestFixPrompt(baseResult({ failedTests: [42, { a: 1 }, null] as unknown as string[] })); expect(prompt).toContain('## Test Failures'); expect(prompt).toContain('42'); expect(prompt).toContain('(malformed entry: 1 keys)'); diff --git a/src/agents/workerValidationEvidence.test.ts b/src/agents/workerValidationEvidence.test.ts index f8b8d95d..564e5371 100644 --- a/src/agents/workerValidationEvidence.test.ts +++ b/src/agents/workerValidationEvidence.test.ts @@ -135,3 +135,39 @@ describe('missingWorkerValidationIssues with observed execution (AGT-4534)', () }))).toEqual([]); }); }); + +describe('non-executing command forms are not validation evidence (AGT-3441)', () => { + const changed = ['src/example.ts']; + const issuesFor = (command: string) => missingWorkerValidationIssues(worker({ filesChanged: changed, commands: [command] })); + + it.each([ + '# npm test', + ': npm test', + 'true npm test', + 'command -v vitest', + 'type pytest', + 'man pytest', + 'npx vitest --help', + 'npx tsc --version', + 'pytest --collect-only', + 'pytest --co -q', + 'npx jest --listTests', + 'npx tsc --showConfig', + 'make -n', + 'make --dry-run test', + ])('rejects %s', (command) => { + expect(issuesFor(command).length).toBeGreaterThan(0); + }); + + it.each([ + 'npm test', + 'pytest -v', + 'pytest -n 4', + 'npx vitest run src/a.test.ts', + 'make test', + 'git diff && npm test', + 'python3 scripts/check.py --help', + ])('still accepts %s', (command) => { + expect(issuesFor(command)).toEqual([]); + }); +}); diff --git a/src/agents/workerValidationEvidence.ts b/src/agents/workerValidationEvidence.ts index a78d04b2..66dcb474 100644 --- a/src/agents/workerValidationEvidence.ts +++ b/src/agents/workerValidationEvidence.ts @@ -21,6 +21,15 @@ const VALIDATION_COMMAND_RE = /\b(npm\s+(?:test|run\s+(?:test|build|lint|typeche // run it), so the segment is skipped rather than the whole command rejected. const INSPECTION_ONLY_COMMAND_RE = /^\s*(?:rg|grep|sed|cat|ls|find|pwd|git\s+(?:status|diff|show|log|grep)|jq|head|tail|wc|tree|du|file|which|echo|printf)\b/i; const SCRIPT_SMOKE_COMMAND_RE = /^\s*(?:python3?|node|tsx|ts-node|uv\s+run|npx|bunx|bash|sh)\b.*\b(?:scripts?\/|tests?\/|\.py|\.js|\.ts|\.sh|--help|--dry-run|smoke|verify|validate|check|test|build|compile)\b/i; +// A segment that names a validation tool but does not run it: a shell comment, +// a no-op builtin (`:`, `true`, `false`), `command -v`/`type`/`man`/`help` +// lookups, or help/version/list/dry-run flags. These print text, they do +// not build or test anything, so they are not validation evidence. +const NON_EXECUTING_PREFIX_RE = /^\s*(?:#|:(?:\s|$)|true\b|false\b|command\s+-[vV]\b|type\b|man\b|help\b|whatis\b|whereis\b)/i; +// Only flags that mean "do not run" for every tool here; `-v`/`-n` stay out +// because pytest uses them for verbose output and parallel workers. +const NON_EXECUTING_FLAG_RE = /(?:^|\s)(?:--help|-h|--version|--listTests|--collect-only|--co|--showConfig|--print-config)(?=\s|=|$)/; +const MAKE_DRY_RUN_RE = /^\s*(?:make|gmake)\b.*\s(?:-n|--dry-run|--just-print|--recon)(?=\s|$)/; const TESTER_CODE_FILE_RE = /\.(ts|tsx|mts|cts|js|jsx|mjs|cjs|py|rs|go|java|rb|c|cpp|h|hpp)$/; export function isValidationRelevantFile(file: string): boolean { @@ -48,8 +57,9 @@ function commandLooksLikeValidation(command: string): boolean { const segments = command.split(/&&|\|\||;|\|/).map(s => s.trim()).filter(Boolean); const candidates = segments.length > 0 ? segments : [command]; return candidates.some(seg => { - if (INSPECTION_ONLY_COMMAND_RE.test(seg)) return false; - return VALIDATION_COMMAND_RE.test(seg) || SCRIPT_SMOKE_COMMAND_RE.test(seg); + if (INSPECTION_ONLY_COMMAND_RE.test(seg) || NON_EXECUTING_PREFIX_RE.test(seg)) return false; + if (VALIDATION_COMMAND_RE.test(seg)) return !NON_EXECUTING_FLAG_RE.test(seg) && !MAKE_DRY_RUN_RE.test(seg); + return SCRIPT_SMOKE_COMMAND_RE.test(seg); }); } From 9e6d0c1c98e85b4b1bd97443f5c77015b3171327 Mon Sep 17 00:00:00 2001 From: Heewon Oh Date: Sun, 15 Nov 2026 06:32:54 +0900 Subject: [PATCH 2/2] fix(agents): report escaping changed paths without blocking the run (AGT-3441) A tracked symlink that points outside the project is legitimate; the guards only need to not open it. --- src/agents/pipelineGuards.test.ts | 5 +++-- src/agents/pipelineGuards.ts | 3 ++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/agents/pipelineGuards.test.ts b/src/agents/pipelineGuards.test.ts index b9022c53..241732fb 100644 --- a/src/agents/pipelineGuards.test.ts +++ b/src/agents/pipelineGuards.test.ts @@ -844,7 +844,7 @@ describe('pipelineGuards — changed paths that escape the project (AGT-3441)', afterEach(() => rmSync(root, { recursive: true, force: true })); - it('blocks and never hands escaping paths to file-reading guards', async () => { + it('reports escaping paths and never hands them to file-reading guards', async () => { const { symlinkSync } = await import('node:fs'); symlinkSync(join(root, 'outside'), join(repo, 'link')); writeFileSync(join(repo, 'ok.ts'), 'export const ok = 1;\n'); @@ -854,7 +854,8 @@ describe('pipelineGuards — changed paths that escape the project (AGT-3441)', { bsDetector: true }, ); const containment = result.results.find(r => r.guard === 'pathContainment'); - expect(containment?.blocking).toBe(true); + expect(containment?.blocking).toBe(false); + expect(containment?.passed).toBe(false); expect(containment?.issues).toHaveLength(3); // bsDetector only ever saw ok.ts expect(guardIssues(result, 'bsDetector').every(issue => issue.includes('ok.ts'))).toBe(true); diff --git a/src/agents/pipelineGuards.ts b/src/agents/pipelineGuards.ts index 7bdbb2d5..1cd3734e 100644 --- a/src/agents/pipelineGuards.ts +++ b/src/agents/pipelineGuards.ts @@ -923,9 +923,10 @@ export async function runGuards( // Keep the original result for reporting/publication, but never feed // generated pytest/venv artifacts — or paths that escape the project // (absolute, `..`, symlinked out) — into guards that open changed files. + // Reported, not blocking: a tracked symlink pointing outside is legitimate. const { contained, escaped } = partitionChangedPaths(projectPath, workerResult.filesChanged.filter((file) => !isEphemeralWorktreeArtifact(file))); const guardWorkerResult = { ...workerResult, filesChanged: contained }; - if (escaped.length > 0) results.push({ passed: false, guard: 'pathContainment', blocking: true, issues: escaped.slice(0, 20).map((file) => `Changed path escapes the project: ${file}`) }); + if (escaped.length > 0) results.push({ passed: false, guard: 'pathContainment', blocking: false, issues: escaped.slice(0, 20).map((file) => `Changed path escapes the project: ${file}`) }); if (config.qualityGate) { console.warn('[Guard:qualityGate] Deprecated: use autonomous.verify baseline-diff verification instead.');