Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(pi): close wrapper and reader evidence gaps
Chrisfe6c4173ebfe
diff --git a/packages/maker-core/src/agents/pi/__tests__/cindyBridgeSource.test.ts b/packages/maker-core/src/agents/pi/__tests__/cindyBridgeSource.test.tsindex 388be4e11a..fb3d4a5604 100644--- a/packages/maker-core/src/agents/pi/__tests__/cindyBridgeSource.test.ts+++ b/packages/maker-core/src/agents/pi/__tests__/cindyBridgeSource.test.ts@@ -487,6 +487,7 @@ describe('cindy-bridge extension source', () => {`builtin 2>/dev/null cd ${nestedDir} && cat <${cwdSwitchName}`,`command -p 2>/dev/null cd ${nestedDir} && cat <${cwdSwitchName}`,`command -- cd ${nestedDir} && cat <${cwdSwitchName}`,+ `builtin -- cd ${nestedDir} && cat <${cwdSwitchName}`,`builtin command cd ${nestedDir} && cat <${cwdSwitchName}`,`command builtin 2>/dev/null pushd ${nestedDir} && cat <${cwdSwitchName}`,`{saved}>/dev/null builtin cd ${nestedDir} && cat <${cwdSwitchName}`,@@ -537,9 +538,24 @@ describe('cindy-bridge extension source', () => {stackOtherOrdinaryLink,].includes(target)), command).toBe(true);}+ for (const command of [+ `D=cd; $D ${nestedDir} && cat <${cwdSwitchName}`,+ `UNSET=; c${'${UNSET}'}d ${nestedDir} && cat <${cwdSwitchName}`,+ `UNSET=; bu${'${UNSET}'}iltin -- cd ${nestedDir} && cat <${cwdSwitchName}`,+ ]) {+ expect(context.bashInputReadEvidence?.({ command }), command)+ .toEqual({ targets: [rootCwdSwitchOrdinaryLink], unresolved: true });+ }+ const mixedConditionalCommand = `true || cd ${nestedDir} && cat <${scopedLinkName}`;+ const mixedConditionalEvidence = context.bashInputReadEvidence?.({ command: mixedConditionalCommand });+ expect(mixedConditionalEvidence?.targets, mixedConditionalCommand)+ .toEqual([rootScopedSecretLink, nestedScopedOrdinaryLink]);+ expect(context.collectResolvedCredentialPaths?.(mixedConditionalEvidence?.targets))+ .toEqual([realpathSync(secretPath)]);for (const command of [`pushd ${nestedDir} && pushd ${stackOtherDir} && popd && cat <${cwdSwitchName}`,`pushd ${nestedDir} && pushd ${stackOtherDir} && builtin popd +0 && cat <${cwdSwitchName}`,+ `pushd ${nestedDir} && pushd ${stackOtherDir} && popd -2 && cat <${cwdSwitchName}`,`pushd -n ${nestedDir} && popd && cat <${cwdSwitchName}`,]) {const evidence = context.bashInputReadEvidence?.({ command });@@ -558,13 +574,36 @@ describe('cindy-bridge extension source', () => {}for (const command of [`pushd ${nestedDir} && pushd ${stackOtherDir} && popd +1 && cat <${cwdSwitchName}`,+ `pushd ${nestedDir} && pushd ${stackOtherDir} && popd +2 && cat <${cwdSwitchName}`,`pushd ${nestedDir} && pushd ${stackOtherDir} && popd -0 && cat <${cwdSwitchName}`,+ `pushd ${nestedDir} && pushd ${stackOtherDir} && popd -1 && cat <${cwdSwitchName}`,]) {expect(context.bashInputReadEvidence?.({ command }), command).toEqual({targets: [stackOtherOrdinaryLink],unresolved: false,});}+ for (const command of [+ `pushd +0 && cat <ordinary-link.txt`,+ `pushd ${nestedDir} && pushd ${stackOtherDir} && pushd +0 && cat <${cwdSwitchName}`,+ `pushd ${nestedDir} && pushd ${stackOtherDir} && popd -n +0 && cat <${cwdSwitchName}`,+ `pushd ${nestedDir} && pushd ${stackOtherDir} && popd -n +1 && cat <${cwdSwitchName}`,+ `pushd ${nestedDir} && pushd ${stackOtherDir} && popd -n -0 && cat <${cwdSwitchName}`,+ `pushd ${nestedDir} && pushd ${stackOtherDir} && popd -n +0 && pushd +0 && cat <${cwdSwitchName}`,+ `pushd ${nestedDir} && pushd ${stackOtherDir} && pushd -n +0 && cat <${cwdSwitchName}`,+ `pushd ${nestedDir} && pushd ${stackOtherDir} && pushd -n +1 && cat <${cwdSwitchName}`,+ ]) {+ expect(context.bashInputReadEvidence?.({ command }), command).toEqual({+ targets: [command.startsWith('pushd +0') ? ordinaryLink : stackOtherOrdinaryLink],+ unresolved: false,+ });+ }+ const pushdRotationCommand = `pushd ${nestedDir} && pushd ${stackOtherDir} && pushd +1 && cat <${cwdSwitchName}`;+ const pushdRotationEvidence = context.bashInputReadEvidence?.({ command: pushdRotationCommand });+ expect(pushdRotationEvidence, pushdRotationCommand)+ .toEqual({ targets: [nestedCwdSwitchSecretLink], unresolved: false });+ expect(context.collectResolvedCredentialPaths?.(pushdRotationEvidence?.targets))+ .toEqual([realpathSync(secretPath)]);const sequentialPopdCommand = `pushd ${nestedDir}; pushd ${stackOtherDir}; popd; cat <${cwdSwitchName}`;const sequentialPopdEvidence = context.bashInputReadEvidence?.({ command: sequentialPopdCommand });expect(sequentialPopdEvidence?.unresolved, sequentialPopdCommand).toBe(true);@@ -606,10 +645,13 @@ describe('cindy-bridge extension source', () => {expect(context.bashInputReadEvidence?.({command: '>$(printf cd) cat <ordinary-link.txt',})).toEqual({ targets: [ordinaryLink], unresolved: false });- const ordinaryPrefixedCommand = `2>/dev/null builtin cd ${nestedDir} && cat <${nestedOrdinaryReadName}`;+ const ordinaryPrefixedCommand = `2>/dev/null builtin -- cd ${nestedDir} && cat <${nestedOrdinaryReadName}`;const ordinaryPrefixedEvidence = context.bashInputReadEvidence?.({ command: ordinaryPrefixedCommand });expect(ordinaryPrefixedEvidence).toEqual({ targets: [nestedOrdinaryReadLink], unresolved: false });expect(context.collectResolvedCredentialPaths?.(ordinaryPrefixedEvidence?.targets)).toEqual([]);+ expect(context.bashInputReadEvidence?.({+ command: `builtin $BUILTIN_OPTION cd ${nestedDir} && cat <ordinary-link.txt`,+ })).toEqual({ targets: [ordinaryLink], unresolved: true });expect(context.bashInputReadEvidence?.({command: `cd ${nestedDir} >/dev/null && cat <${scopedLinkName}`,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 e90be67641..f3a7de5721 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@@ -2132,6 +2132,13 @@ describe.skipIf(!piAvailable)('PiAgent integration (real pi binary + fake gatewafor (const [sessionId, command] of [['perm-auto-bash-symlink-dotenv-pushd-rotation', 'pushd sub >/dev/null; pushd ../stack-other >/dev/null; pushd +1 >/dev/null; cat<link'],['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-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'],+ ['perm-auto-bash-symlink-dotenv-conditional-cd', 'true || cd sub && cat<cd-innocent'],+ ['perm-auto-bash-symlink-dotenv-pushd-index', 'pushd sub && pushd ../stack-other && pushd +1 && cat<link'],['perm-auto-bash-symlink-dotenv-assignment-cd', 'X=1 cd sub && cat<link'],['perm-auto-bash-symlink-dotenv-assignment-leading-redirect', 'X=1 2>/dev/null builtin cd sub && cat<link'],['perm-auto-bash-symlink-dotenv-leading-assignment-wrapper', '2>/dev/null X=1 command -- cd sub && cat<link'],@@ -2161,26 +2168,34 @@ describe.skipIf(!piAvailable)('PiAgent integration (real pi binary + fake gatewaexpect(followUp.some((b) => b.includes('User denied this tool call via Cindy.')), command).toBe(true);}- scriptedResponses.length = 0;- scriptedResponses.push(- anthropicToolUseBody('bash', {- command: 'pushd sub && popd && cat<link',- }),- anthropicStreamBody('bash ordinary popd turn finished'),- );- const ordinaryPopdReqBefore = seenRequests.length;- const ordinaryPopdTurn = await runPermissionTurn({- sessionId: 'perm-full-access-bash-popd-plain',- workingDir,- permissionMode: 'bypassPermissions',- resolverBehavior: 'allow',- });- expect(ordinaryPopdTurn.resolverTools).toEqual([]);- const ordinaryPopdFollowUp = seenRequests.slice(ordinaryPopdReqBefore)- .map((request) => request.body);- expect(ordinaryPopdFollowUp.some((body) => body.includes('ordinary-content'))).toBe(true);- expect(ordinaryPopdFollowUp.some((body) => body.includes('FAKE_REDIRECT_DOTENV_SECRET')))- .toBe(false);+ for (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-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'],+ ['perm-full-access-bash-pushd-no-cd-plain', 'pushd sub && pushd ../stack-other && pushd -n +1 && cat<link'],+ ['perm-full-access-bash-conditional-cd-plain', 'false || cd sub && cat<ordinary'],+ ] as const) {+ scriptedResponses.length = 0;+ scriptedResponses.push(+ anthropicToolUseBody('bash', { command }),+ anthropicStreamBody('bash ordinary cwd turn finished'),+ );+ const ordinaryReqBefore = seenRequests.length;+ const ordinaryTurn = await runPermissionTurn({+ sessionId,+ workingDir,+ permissionMode: 'bypassPermissions',+ resolverBehavior: 'allow',+ });+ expect(ordinaryTurn.resolverTools, command).toEqual([]);+ const ordinaryFollowUp = seenRequests.slice(ordinaryReqBefore)+ .map((request) => request.body);+ expect(ordinaryFollowUp.some((body) => body.includes('ordinary-content')), command).toBe(true);+ expect(ordinaryFollowUp.some((body) => body.includes('FAKE_REDIRECT_DOTENV_SECRET')), command)+ .toBe(false);+ }scriptedResponses.length = 0;scriptedResponses.push(diff --git a/packages/maker-core/src/agents/pi/cindy-bridge-source.ts b/packages/maker-core/src/agents/pi/cindy-bridge-source.tsindex 2dd1b5316d..4c1498137e 100644--- a/packages/maker-core/src/agents/pi/cindy-bridge-source.ts+++ b/packages/maker-core/src/agents/pi/cindy-bridge-source.ts@@ -596,7 +596,10 @@ function bashDirectoryChangeHead(}let word = readShellRedirectionTarget(command, cursor);- if (!word.target || word.unresolved) {+ if (word.target.includes('$') || word.target.charCodeAt(0) === 96) {+ return { cursor, command: null, hadRedirection, unresolved: true };+ }+ if (!word.target) {if (unresolved && bashUnresolvedHeadHasDirectoryChange(codeMask, cursor)) {return { cursor, command: null, hadRedirection, unresolved: true };}@@ -614,12 +617,17 @@ function bashDirectoryChangeHead(continue;}word = readShellRedirectionTarget(command, cursor);- if (wrapper !== 'command' || (word.target !== '--' && !/^-p+$/.test(word.target))) break;+ const wrapperOption = word.target === '--'+ || (wrapper === 'command' && /^-p+$/.test(word.target));+ if (!wrapperOption) break;cursor = bashInlinePaddingEnd(command, word.end);}- if (!word.target || word.unresolved) return unresolved+ if (!word.target) return unresolved? { cursor, command: null, hadRedirection, unresolved: true }: null;+ if (word.target.includes('$') || word.target.charCodeAt(0) === 96) {+ return { cursor, command: null, hadRedirection, unresolved: true };+ }}if ((word.target !== 'cd' && word.target !== 'pushd' && word.target !== 'popd') || word.unresolved) {@@ -631,14 +639,14 @@ function bashDirectoryChangeHead(return { cursor: word.end, command: word.target, hadRedirection, unresolved };}-type BashDirectoryState = { stack: string[] };+type BashDirectoryState = { cwd: string; stack: string[] };const BASH_DIRECTORY_STATE_LIMIT = 128;const BASH_DIRECTORY_STACK_LIMIT = 128;function bashUniqueDirectoryStates(states: readonly BashDirectoryState[]): BashDirectoryState[] | null {const unique = new Map<string, BashDirectoryState>();for (const state of states) {- unique.set(state.stack.join('\u0000'), state);+ unique.set([state.cwd, ...state.stack].join('\u0000'), state);if (unique.size > BASH_DIRECTORY_STATE_LIMIT) return null;}return [...unique.values()];@@ -654,7 +662,7 @@ function bashPopdIndex(operand: string, stackLength: number): number | null {}function bashWorkingDirectoryCandidates(command: string, globBudget: BashGlobBudget): BashPathCandidates {- let states: BashDirectoryState[] = [{ stack: [process.cwd()] }];+ let states: BashDirectoryState[] = [{ cwd: process.cwd(), stack: [process.cwd()] }];let unresolved = false;const codeMask = bashShellCodeMask(command);const commandStart = /(?:^(?=[^;&|(){}\n])|[;&|(){}\n])\s*(?:(?:if|then|elif|else|while|until|do)\s+)?/g;@@ -667,6 +675,8 @@ function bashWorkingDirectoryCandidates(command: string, globBudget: BashGlobBudcontinue;}let 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));@@ -694,12 +704,7 @@ function bashWorkingDirectoryCandidates(command: string, globBudget: BashGlobBudconst pushdRotation = head.command === 'pushd'&& !optionsEnded && /^[+-]\d+$/.test(parsed.target);- if (pushdRotation) {- // Rotation can include an inherited stack that is unavailable to this invocation.- unresolved = true;- continue;- }- if (head.command !== 'popd'+ if (head.command !== 'popd' && !pushdRotation&& (!parsed.target || parsed.unresolved || (!optionsEnded && parsed.target.startsWith('-')))) {unresolved = true;continue;@@ -725,35 +730,52 @@ function bashWorkingDirectoryCandidates(command: string, globBudget: BashGlobBudconst changed: BashDirectoryState[] = [];let stateChangeUnresolved = false;if (head.command === 'popd') {- if (suppressDirectoryChange) {- unresolved = true;- continue;- }for (const state of states) {- const index = bashPopdIndex(parsed.target, state.stack.length);+ let index = bashPopdIndex(parsed.target, state.stack.length);+ // With -n, Bash preserves the current directory in slot 0; +0/-last+ // therefore removes the first saved entry instead of the cwd slot.+ if (suppressDirectoryChange && index === 0) index = 1;if (index === null || index < 0 || index >= state.stack.length- || (index === 0 && state.stack.length < 2)) {+ || (!suppressDirectoryChange && index === 0 && state.stack.length < 2)) {stateChangeUnresolved = true;continue;}const stack = [...state.stack];stack.splice(index, 1);- changed.push({ stack });+ const cwd = !suppressDirectoryChange && index === 0 ? stack[0] : state.cwd;+ if (!cwd) {+ stateChangeUnresolved = true;+ continue;+ }+ changed.push({ cwd, stack });+ }+ } else if (pushdRotation) {+ for (const state of states) {+ const index = bashPopdIndex(parsed.target, state.stack.length);+ if (index === null || index < 0 || index >= state.stack.length) {+ stateChangeUnresolved = true;+ continue;+ }+ const stack = [...state.stack.slice(index), ...state.stack.slice(0, index)];+ // pushd -n rotates saved entries but pins the actual cwd back into slot 0.+ const cwd = suppressDirectoryChange ? state.cwd : stack[0];+ if (suppressDirectoryChange) stack[0] = state.cwd;+ changed.push({ cwd, stack });}} else {for (const state of states) {- const cwd = state.stack[0];+ const cwd = state.cwd;const expanded = bashExpandedPathCandidates(parsed.target, cwd, parsed.mayExpand, globBudget);unresolved ||= expanded.unresolved;for (const candidate of expanded.paths) {try {if (!statSync(candidate).isDirectory()) continue;if (head.command === 'cd') {- changed.push({ stack: [candidate, ...state.stack.slice(1)] });+ changed.push({ cwd: candidate, stack: [candidate, ...state.stack.slice(1)] });} else if (suppressDirectoryChange) {- changed.push({ stack: [cwd, candidate, ...state.stack.slice(1)] });+ changed.push({ cwd, stack: [cwd, candidate, ...state.stack.slice(1)] });} else if (state.stack.length < BASH_DIRECTORY_STACK_LIMIT) {- changed.push({ stack: [candidate, ...state.stack] });+ changed.push({ cwd: candidate, stack: [candidate, ...state.stack] });} else {stateChangeUnresolved = true;}@@ -769,7 +791,7 @@ function bashWorkingDirectoryCandidates(command: string, globBudget: BashGlobBudcodeMask.slice(operatorEvidence.end),);const retainsPriorState = operator === '||' || operator === ';' || operator === '\n'- || branchMayHaveBeenSkipped;+ || branchMayHaveBeenSkipped || precededByOrOperator;const nextStates = retainsPriorState ? [...states, ...changed] : changed;if (nextStates.length > 0) {const unique = bashUniqueDirectoryStates(nextStates);@@ -777,7 +799,7 @@ function bashWorkingDirectoryCandidates(command: string, globBudget: BashGlobBudelse states = unique;}}- return { paths: [...new Set(states.map((state) => state.stack[0]))], unresolved };+ return { paths: [...new Set(states.map((state) => state.cwd))], unresolved };}function bashShellCodeMask(command: string): string {diff --git a/packages/maker-core/src/agents/shared/auto-review.adversarial.test.ts b/packages/maker-core/src/agents/shared/auto-review.adversarial.test.tsindex 9fa9b5e7ef..2dd109e2d5 100644--- a/packages/maker-core/src/agents/shared/auto-review.adversarial.test.ts+++ b/packages/maker-core/src/agents/shared/auto-review.adversarial.test.ts@@ -420,6 +420,10 @@ describe('对抗语料 — dotenv 凭证路径', () => {'column .env','grep --file=.env data.txt','jq --from-file=.env.local data.json',+ "jq --argfile creds .env '$creds' data.json",+ "jq --slurpfile creds '.env.local' '$creds' data.json",+ "jq --rawfile creds .env.production '$creds' data.json",+ "jq --slurpfile creds '.n?trc' '$creds' data.json",'diff --from-file=.env /dev/null','file -f .env','file -f.env.local',@@ -709,6 +713,12 @@ describe('对抗语料 — dotenv 凭证路径', () => {['grep -f patterns.txt data.txt', 'auto-approve'],['sed -f script.sed data.txt', 'prompt'],['jq -f filter.jq data.json', 'auto-approve'],+ ["jq --argfile data ordinary.json '$data' input.json", 'auto-approve'],+ ["jq --slurpfile data ordinary.json '$data' input.json", 'auto-approve'],+ ["jq --rawfile data ordinary.txt '$data' input.json", 'auto-approve'],+ ["jq --arg data ordinary '$data' data.json", 'auto-approve'],+ ["jq -n --arg data '' '$data'", 'auto-approve'],+ ["jq --argjson data '{\"book\":true}' '$data' data.json", 'auto-approve'],['diff --from-file=README.md /dev/null', 'auto-approve'],['diff --from-file README.md /dev/null', 'auto-approve'],['diff --from-f=README.md /dev/null', 'auto-approve'],diff --git a/packages/maker-core/src/agents/shared/auto-review.ts b/packages/maker-core/src/agents/shared/auto-review.tsindex e35ac531c6..12f958a537 100644--- a/packages/maker-core/src/agents/shared/auto-review.ts+++ b/packages/maker-core/src/agents/shared/auto-review.ts@@ -4744,6 +4744,8 @@ type ReaderOptionKind =| 'filter'| 'aux-file'| 'aux-file-list'+ | 'named-data'+ | 'named-file'| 'type-definition'| 'type-include'| 'type-exclude'@@ -4775,6 +4777,11 @@ const SED_LONG_OPTIONS: readonly ReaderLongOption[] = [];const JQ_LONG_OPTIONS: readonly ReaderLongOption[] = [{ name: '--from-file', kind: 'data-file' },+ { name: '--arg', kind: 'named-data' },+ { name: '--argjson', kind: 'named-data' },+ { name: '--argfile', kind: 'named-file' },+ { name: '--slurpfile', kind: 'named-file' },+ { name: '--rawfile', kind: 'named-file' },];const DIFF_LONG_OPTIONS: readonly ReaderLongOption[] = [{ name: '--from-file', kind: 'data-file' },@@ -5137,7 +5144,8 @@ function readerOptionValueIsSensitive(if (kind === 'selector') return selectorCouldMatchCredential(value ?? '');if (!value || (kind !== 'data-file' && kind !== 'aux-file' && kind !== 'aux-file-list')) return false;const operands = kind === 'aux-file-list' ? value.split(/[:;]/) : [value];- return operands.some((operand) => shellOperandCouldMatchDotenv(operand, isSensitiveOperand));+ return operands.some((operand) =>+ isSensitiveOperand(operand) || selectorCouldMatchCredential(operand));}function readerArgumentsReadDotenv(@@ -5162,6 +5170,15 @@ function readerArgumentsReadDotenv(const kind = resolveReaderLongOption(bin, name);if (kind) {const attached = equalsIndex >= 0 ? token.slice(equalsIndex + 1) : undefined;+ if (kind === 'named-data' || kind === 'named-file') {+ const nameValue = attached ?? args[index + 1];+ const secondValue = attached === undefined ? args[index + 2] : args[index + 1];+ if (nameValue === undefined || secondValue === undefined) return true;+ if (kind === 'named-file'+ && readerOptionValueIsSensitive('data-file', secondValue, isSensitiveOperand)) return true;+ index += attached === undefined ? 2 : 1;+ continue;+ }const value = attached ?? args[index + 1];const isSensitive = readerOptionValueIsSensitive(kind, value, isSensitiveOperand);if (kind !== 'data' && isSensitive) return true;