Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(pi): fail closed on sourced cwd changes
Chris441dfa204f03
diff --git a/packages/maker-core/src/agents/pi/__tests__/cindyBridgeSource.test.ts b/packages/maker-core/src/agents/pi/__tests__/cindyBridgeSource.test.tsindex fb3d4a5604..980fadfb6b 100644--- a/packages/maker-core/src/agents/pi/__tests__/cindyBridgeSource.test.ts+++ b/packages/maker-core/src/agents/pi/__tests__/cindyBridgeSource.test.ts@@ -498,6 +498,38 @@ describe('cindy-bridge extension source', () => {expect(context.collectResolvedCredentialPaths?.(evidence?.targets), command).toEqual([realpathSync(secretPath)]);}+ for (const command of [+ `source change-dir.sh; cat <${cwdSwitchName}`,+ `. ./change-dir.sh && cat <${cwdSwitchName}`,+ `builtin source change-dir.sh; cat <${cwdSwitchName}`,+ `builtin -- . ./change-dir.sh; cat <${cwdSwitchName}`,+ `command eval 'cd nested'; cat <${cwdSwitchName}`,+ `X=1 2>/dev/null source change-dir.sh; cat <${cwdSwitchName}`,+ `false || source change-dir.sh; cat <${cwdSwitchName}`,+ ]) {+ expect(context.bashInputReadEvidence?.({ command }), command).toEqual({+ targets: [rootCwdSwitchOrdinaryLink],+ unresolved: true,+ });+ }+ expect(context.bashInputReadEvidence?.({+ command: 'source change-dir.sh <ordinary-link.txt',+ })).toEqual({ targets: [ordinaryLink], unresolved: false });+ expect(context.bashInputReadEvidence?.({+ command: '(source change-dir.sh); cat <ordinary-link.txt',+ })).toEqual({ targets: [ordinaryLink], unresolved: false });+ expect(context.bashInputReadEvidence?.({+ command: 'bash change-dir.sh; cat <ordinary-link.txt',+ })).toEqual({ targets: [ordinaryLink], unresolved: false });+ expect(context.bashInputReadEvidence?.({+ command: `(source change-dir.sh; cat <${cwdSwitchName})`,+ })).toEqual({ targets: [rootCwdSwitchOrdinaryLink], unresolved: true });+ expect(context.bashInputReadEvidence?.({ command: 'source change-dir.sh' }))+ .toEqual({ targets: [], unresolved: false });+ expect(context.bashInputReadEvidence?.({+ command: `printf '%s' 'source change-dir.sh'; cat <ordinary-link.txt`,+ })).toEqual({ targets: [ordinaryLink], unresolved: false });+expect(context.bashInputReadEvidence?.({command: `CDPATH=${cdPathRoot} cd sub && cat <${cwdSwitchName}`,})).toEqual({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.tsindex f3a7de5721..129a35bf12 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@@ -2121,6 +2121,7 @@ describe.skipIf(!piAvailable)('PiAgent integration (real pi binary + fake gatewamkdirSync(stackOtherDir);writeFileSync(secretPath, 'FAKE_REDIRECT_DOTENV_SECRET=must-not-leak');writeFileSync(ordinaryPath, 'ordinary-content');+ writeFileSync(path.join(workingDir, 'change-dir.sh'), 'cd sub\n');symlinkSync('../secrets/.env', postCdLink);symlinkSync(ordinaryPath, path.join(workingDir, 'link'));symlinkSync(ordinaryPath, path.join(stackOtherDir, 'link'));@@ -2134,6 +2135,10 @@ describe.skipIf(!piAvailable)('PiAgent integration (real pi binary + fake gatewa['perm-auto-bash-symlink-dotenv-popd', 'pushd sub; pushd ../stack-other; popd; cat<link'],['perm-auto-bash-symlink-dotenv-popd-index', 'pushd sub && pushd ../stack-other && popd +0 && cat<link'],['perm-auto-bash-symlink-dotenv-builtin-terminator', 'builtin -- cd sub && cat<link'],+ ['perm-auto-bash-symlink-dotenv-source', 'source change-dir.sh; cat<link'],+ ['perm-auto-bash-symlink-dotenv-dot-source', '. ./change-dir.sh && cat<link'],+ ['perm-auto-bash-symlink-dotenv-builtin-source', 'builtin source change-dir.sh; cat<link'],+ ['perm-auto-bash-symlink-dotenv-eval-source', "eval 'source change-dir.sh'; cat<link"],['perm-auto-bash-symlink-dotenv-dynamic-cd', 'D=cd; $D sub && cat<link'],['perm-auto-bash-symlink-dotenv-interpolated-cd', 'UNSET=; c${UNSET}d sub && cat<link'],['perm-auto-bash-symlink-dotenv-interpolated-builtin', 'UNSET=; bu${UNSET}iltin -- cd sub && cat<link'],@@ -2171,6 +2176,8 @@ describe.skipIf(!piAvailable)('PiAgent integration (real pi binary + fake gatewafor (const [sessionId, command] of [['perm-full-access-bash-popd-plain', 'pushd sub && popd && cat<link'],['perm-full-access-bash-builtin-terminator-plain', 'builtin -- cd sub && cat<ordinary'],+ ['perm-full-access-bash-subshell-source-plain', '(source change-dir.sh); cat<link'],+ ['perm-full-access-bash-external-source-plain', 'bash change-dir.sh; cat<link'],['perm-full-access-bash-popd-index-plain', 'pushd sub && pushd ../stack-other && popd +1 && cat<link'],['perm-full-access-bash-pushd-zero-plain', 'pushd +0 && cat<link'],['perm-full-access-bash-popd-no-cd-plain', 'pushd sub && pushd ../stack-other && popd -n +1 && cat<link'],diff --git a/packages/maker-core/src/agents/pi/cindy-bridge-source.ts b/packages/maker-core/src/agents/pi/cindy-bridge-source.tsindex 4c1498137e..94e117f418 100644--- a/packages/maker-core/src/agents/pi/cindy-bridge-source.ts+++ b/packages/maker-core/src/agents/pi/cindy-bridge-source.ts@@ -466,13 +466,18 @@ function bashInheritedDirectoryChangeIsUncertain(type BashLeadingRedirection = { end: number; unresolved: boolean };type BashAssignmentPrefix = { end: number; name: string; unresolved: boolean };+type BashDirectoryChangeCommand = 'cd' | 'pushd' | 'popd' | 'opaque';type BashDirectoryChangeHead = {cursor: number;- command: 'cd' | 'pushd' | 'popd' | null;+ command: BashDirectoryChangeCommand | null;hadRedirection: boolean;unresolved: boolean;};+const BASH_OPAQUE_CURRENT_SHELL_EVALUATORS: ReadonlySet<string> = new Set([+ 'source', '.', 'eval',+]);+function bashInlinePaddingEnd(command: string, start: number): number {let cursor = start;while (cursor < command.length) {@@ -523,12 +528,16 @@ function bashAssignmentCanChangeDirectory(name: string): boolean {return name === 'CDPATH' || name === 'HOME' || name === 'OLDPWD' || name === 'PWD';}-function bashUnescapedBacktickAt(command: string, index: number): boolean {+function bashCharacterIsEscapedAt(command: string, index: number): boolean {let backslashes = 0;for (let cursor = index - 1; cursor >= 0 && command[cursor] === '\\'; cursor -= 1) {backslashes += 1;}- return backslashes % 2 === 0;+ return backslashes % 2 === 1;+}++function bashUnescapedBacktickAt(command: string, index: number): boolean {+ return !bashCharacterIsEscapedAt(command, index);}function bashUnresolvedHeadHasDirectoryChange(codeMask: string, start: number): boolean {@@ -630,6 +639,9 @@ function bashDirectoryChangeHead(}}+ if (BASH_OPAQUE_CURRENT_SHELL_EVALUATORS.has(word.target) && !word.unresolved) {+ return { cursor: word.end, command: 'opaque', hadRedirection, unresolved };+ }if ((word.target !== 'cd' && word.target !== 'pushd' && word.target !== 'popd') || word.unresolved) {if (unresolved && bashUnresolvedHeadHasDirectoryChange(codeMask, cursor)) {return { cursor, command: null, hadRedirection, unresolved: true };@@ -661,6 +673,43 @@ function bashPopdIndex(operand: string, stackLength: number): number | null {return match[1] === '+' ? offset : stackLength - 1 - offset;}+function bashOpaqueEvaluatorCompletesBeforeRead(+ command: string,+ codeMask: string,+ start: number,+ initialDepth: number,+): boolean {+ let depth = initialDepth;+ let inBacktick = false;+ for (let index = start; index < codeMask.length; index += 1) {+ const char = codeMask.charAt(index);+ if (char.charCodeAt(0) === 96 && bashUnescapedBacktickAt(codeMask, index)) {+ inBacktick = !inBacktick;+ continue;+ }+ if (inBacktick) continue;+ if (char === '#' && (index === 0 || /[\s;&|()]/.test(codeMask.charAt(index - 1)))) {+ const newline = codeMask.indexOf('\n', index + 1);+ if (newline < 0) return false;+ index = newline - 1;+ continue;+ }+ if (char === '(') {+ depth += 1;+ continue;+ }+ if (char === ')') {+ depth -= 1;+ if (depth < initialDepth) return false;+ continue;+ }+ if (depth !== initialDepth || !/[;&|\n]/.test(char)) continue;+ if (char === '\n' && bashCharacterIsEscapedAt(command, index)) continue;+ return true;+ }+ return false;+}+function bashWorkingDirectoryCandidates(command: string, globBudget: BashGlobBudget): BashPathCandidates {let states: BashDirectoryState[] = [{ cwd: process.cwd(), stack: [process.cwd()] }];let unresolved = false;@@ -670,6 +719,15 @@ function bashWorkingDirectoryCandidates(command: string, globBudget: BashGlobBudconst matchIndex = match.index ?? 0;const head = bashDirectoryChangeHead(command, codeMask, matchIndex + match[0].length);if (!head) continue;+ const changeDepth = bashSubshellDepthAt(codeMask, head.cursor);+ if (!bashSubshellScopeRemainsOpen(codeMask, head.cursor, changeDepth)) continue;+ if (head.command === 'opaque') {+ if (head.unresolved+ || bashOpaqueEvaluatorCompletesBeforeRead(command, codeMask, head.cursor, changeDepth)) {+ unresolved = true;+ }+ continue;+ }if (head.unresolved || !head.command) {unresolved = true;continue;@@ -677,8 +735,6 @@ function bashWorkingDirectoryCandidates(command: string, globBudget: BashGlobBudlet cursor = head.cursor;const precededByOrOperator = matchIndex > 0&& codeMask[matchIndex] === '|' && codeMask[matchIndex - 1] === '|';- const changeDepth = bashSubshellDepthAt(codeMask, cursor);- if (!bashSubshellScopeRemainsOpen(codeMask, cursor, changeDepth)) continue;let parsed = readShellRedirectionTarget(command, bashInlinePaddingEnd(command, cursor));let optionsEnded = false;let suppressDirectoryChange = false;