Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(pi): track redirected directory changes

Chris81f8d2fa9387
diff --git a/packages/maker-core/src/agents/pi/__tests__/cindyBridgeSource.test.ts b/packages/maker-core/src/agents/pi/__tests__/cindyBridgeSource.test.ts
index 5353b82a7c..a81ec2047d 100644
--- a/packages/maker-core/src/agents/pi/__tests__/cindyBridgeSource.test.ts
+++ b/packages/maker-core/src/agents/pi/__tests__/cindyBridgeSource.test.ts
@@ -314,6 +314,37 @@ describe('cindy-bridge extension source', () => {
expect(context.collectResolvedCredentialPaths?.(
context.bashInputReadTargets?.({ command: nestedCommand }),
)).toEqual([realpathSync(secretPath)]);
+ const redirectedDirectoryCommands = [
+ `cd ${nestedDir} >/dev/null && cat <nested-innocent.txt`,
+ `cd ${nestedDir} 2>/dev/null 3>&1 && cat <nested-innocent.txt`,
+ `pushd ${nestedDir} &>/dev/null && cat <nested-innocent.txt`,
+ `cd ${nestedDir} </dev/null >>redirect.log && cat <nested-innocent.txt`,
+ `cd ${nestedDir} <<<ready && cat <nested-innocent.txt`,
+ `cd ${nestedDir} >/dev/null \\\n&& cat <nested-innocent.txt`,
+ `cd ${nestedDir} >/dev/null # quiet\ncat <nested-innocent.txt`,
+ ];
+ for (const redirectedCommand of redirectedDirectoryCommands) {
+ expect(context.collectResolvedCredentialPaths?.(
+ context.bashInputReadTargets?.({ command: redirectedCommand }),
+ ), redirectedCommand).toEqual([realpathSync(secretPath)]);
+ }
+ expect(context.bashInputReadEvidence?.({
+ command: `cd ${nestedDir} >/dev/null && cat <${scopedLinkName}`,
+ })).toEqual({ targets: [nestedScopedOrdinaryLink], unresolved: false });
+ expect(context.collectResolvedCredentialPaths?.(
+ context.bashInputReadTargets?.({
+ command: `cd ${nestedDir} >/missing/output; cat <${scopedLinkName}`,
+ }),
+ )).toEqual([realpathSync(secretPath)]);
+ expect(context.bashInputReadEvidence?.({
+ command: `cd ${nestedDir} >$LOG && cat <ordinary-link.txt`,
+ })).toEqual({ targets: [ordinaryLink], unresolved: true });
+ expect(context.bashInputReadEvidence?.({
+ command: `cd ${nestedDir} {fd}>/dev/null && cat <ordinary-link.txt`,
+ })).toEqual({ targets: [ordinaryLink], unresolved: true });
+ expect(context.bashInputReadEvidence?.({
+ command: `cd ${nestedDir} <<EOF\nignored\nEOF\ncat <ordinary-link.txt`,
+ })).toEqual({ targets: [ordinaryLink], unresolved: true });
for (const groupedCommand of [
`(cd ${nestedDir} && cat <>nested-innocent.txt)`,
`{ cd ${nestedDir} && cat 3<>nested-innocent.txt; }`,
diff --git a/packages/maker-core/src/agents/pi/__tests__/pi-agent.integration.test.ts b/packages/maker-core/src/agents/pi/__tests__/pi-agent.integration.test.ts
index dd62fda56f..f62d083b6e 100644
--- a/packages/maker-core/src/agents/pi/__tests__/pi-agent.integration.test.ts
+++ b/packages/maker-core/src/agents/pi/__tests__/pi-agent.integration.test.ts
@@ -2110,14 +2110,16 @@ describe.skipIf(!piAvailable)('PiAgent integration (real pi binary + fake gatewa
const workingDir = mkdtempSync(path.join(tmpdir(), 'pi-perm-auto-bash-symlink-dotenv-'));
try {
const secretPath = path.join(workingDir, 'secrets', '.env');
- const linkPath = path.join(workingDir, 'innocent.txt');
+ const subDir = path.join(workingDir, 'sub');
+ const linkPath = path.join(subDir, 'link');
mkdirSync(path.dirname(secretPath), { recursive: true });
+ mkdirSync(subDir);
writeFileSync(secretPath, 'FAKE_REDIRECT_DOTENV_SECRET=must-not-leak');
- symlinkSync(secretPath, linkPath);
+ symlinkSync('../secrets/.env', linkPath);
scriptedResponses.length = 0;
scriptedResponses.push(
- anthropicToolUseBody('bash', { command: `cat<${linkPath}` }),
+ anthropicToolUseBody('bash', { command: 'cd sub >/dev/null && cat<link' }),
anthropicStreamBody('bash symlink dotenv turn finished'),
);
const reqBefore = seenRequests.length;
@@ -2145,13 +2147,15 @@ describe.skipIf(!piAvailable)('PiAgent integration (real pi binary + fake gatewa
const workingDir = mkdtempSync(path.join(tmpdir(), 'pi-perm-auto-bash-symlink-plain-'));
try {
const targetPath = path.join(workingDir, 'ordinary-target.txt');
- const linkPath = path.join(workingDir, 'ordinary-link.txt');
+ const subDir = path.join(workingDir, 'sub');
+ const linkPath = path.join(subDir, 'link');
+ mkdirSync(subDir);
writeFileSync(targetPath, 'ordinary-bash-symlink-content');
- symlinkSync(targetPath, linkPath);
+ symlinkSync('../ordinary-target.txt', linkPath);
scriptedResponses.length = 0;
scriptedResponses.push(
- anthropicToolUseBody('bash', { command: `cat<${linkPath}` }),
+ anthropicToolUseBody('bash', { command: 'cd sub >/dev/null && cat<link' }),
anthropicStreamBody('bash ordinary symlink turn finished'),
);
const reqBefore = seenRequests.length;
diff --git a/packages/maker-core/src/agents/pi/cindy-bridge-source.ts b/packages/maker-core/src/agents/pi/cindy-bridge-source.ts
index ef2f24e4fd..db4dacea9e 100644
--- a/packages/maker-core/src/agents/pi/cindy-bridge-source.ts
+++ b/packages/maker-core/src/agents/pi/cindy-bridge-source.ts
@@ -35,7 +35,7 @@ const SHELL_INPUT_REDIRECTION_PARSER_SOURCE = shellInputRedirectionParserSource(
const CREDENTIAL_WORD_BOUNDARY = '\u0001';
const SENSITIVE_CREDENTIAL_SELECTOR_GLOBS = [...new Set(
SENSITIVE_CREDENTIAL_GLOB_PATTERNS.flatMap((pattern) => {
- const variants = [pattern];
+ const variants: string[] = [pattern];
const directory = pattern.endsWith('/**') ? pattern.slice(0, -3) : undefined;
if (directory) {
variants.push(directory, directory + CREDENTIAL_WORD_BOUNDARY + '**');
@@ -230,6 +230,67 @@ function bashSubshellScopeRemainsOpen(command: string, start: number, depth: num
return true;
}
+type BashPostCommandOperator = {
+ operator: string;
+ end: number;
+ unresolved: boolean;
+ hadRedirection: boolean;
+};
+
+function bashOperatorAfterRedirections(command: string, start: number): BashPostCommandOperator {
+ let cursor = start;
+ let hadRedirection = false;
+ while (cursor < command.length) {
+ while (true) {
+ while (/[ \t\r]/.test(command[cursor] ?? '')) cursor += 1;
+ if (command[cursor] === '\\' && command[cursor + 1] === '\n') {
+ cursor += 2;
+ continue;
+ }
+ break;
+ }
+ if (command[cursor] === '#') {
+ const newline = command.indexOf('\n', cursor);
+ if (newline < 0) {
+ return { operator: '', end: command.length, unresolved: false, hadRedirection };
+ }
+ cursor = newline;
+ }
+
+ let redirect = cursor;
+ while (/\d/.test(command[redirect] ?? '')) redirect += 1;
+ let redirectOperator = '';
+ if (command.startsWith('&>>', redirect)) redirectOperator = '&>>';
+ else if (command.startsWith('&>', redirect)) redirectOperator = '&>';
+ else {
+ for (const candidate of ['<<<', '<>', '<&', '>&', '>>', '>|', '<', '>']) {
+ if (command.startsWith(candidate, redirect)) {
+ redirectOperator = candidate;
+ break;
+ }
+ }
+ }
+ if (!redirectOperator) {
+ const operator = /^(&&|\|\||[;\n|&])/.exec(command.slice(cursor))?.[1] ?? '';
+ const unresolved = !operator && cursor < command.length;
+ return { operator, end: cursor, unresolved, hadRedirection };
+ }
+ hadRedirection = true;
+ if (command.startsWith('<<', redirect) && !command.startsWith('<<<', redirect)) {
+ return { operator: '', end: cursor, unresolved: true, hadRedirection };
+ }
+
+ cursor = redirect + redirectOperator.length;
+ while (/[ \t\r]/.test(command[cursor] ?? '')) cursor += 1;
+ const target = readShellRedirectionTarget(command, cursor);
+ if (!target.target || target.unresolved) {
+ return { operator: '', end: cursor, unresolved: true, hadRedirection };
+ }
+ cursor = target.end;
+ }
+ return { operator: '', end: cursor, unresolved: false, hadRedirection };
+}
+
function bashWorkingDirectoryCandidates(command: string): BashPathCandidates {
let candidates = [process.cwd()];
let unresolved = false;
@@ -255,7 +316,12 @@ function bashWorkingDirectoryCandidates(command: string): BashPathCandidates {
unresolved = true;
continue;
}
- const operator = /^\s*(&&|\|\||[;\n|&])/.exec(command.slice(parsed.end))?.[1] ?? '';
+ const operatorEvidence = bashOperatorAfterRedirections(command, parsed.end);
+ if (operatorEvidence.unresolved) {
+ unresolved = true;
+ continue;
+ }
+ const operator = operatorEvidence.operator;
if (!operator || operator === '|' || operator === '&') continue;
const changed: string[] = [];
@@ -270,8 +336,11 @@ function bashWorkingDirectoryCandidates(command: string): BashPathCandidates {
}
}
}
- const branchMayHaveBeenSkipped = /\b(?:elif|else|fi|done|esac)\b/.test(codeMask.slice(parsed.end));
- if (operator === '||' || branchMayHaveBeenSkipped || precededByConditionalOperator) {
+ const branchMayHaveBeenSkipped = /\b(?:elif|else|fi|done|esac)\b/.test(
+ codeMask.slice(operatorEvidence.end),
+ );
+ if (operator === '||' || branchMayHaveBeenSkipped || precededByConditionalOperator
+ || (operatorEvidence.hadRedirection && (operator === ';' || operator === '\n'))) {
candidates = [...new Set([...candidates, ...changed])];
} else if (changed.length > 0) {
candidates = [...new Set(changed)];