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): close selector and cwd evidence gaps

Chrisdde2bd4028e7
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 0ec58e8cf3..7699ff4872 100644
--- a/packages/maker-core/src/agents/pi/__tests__/cindyBridgeSource.test.ts
+++ b/packages/maker-core/src/agents/pi/__tests__/cindyBridgeSource.test.ts
@@ -272,6 +272,8 @@ describe('cindy-bridge extension source', () => {
const cwdSwitchName = 'cwd-switch-link';
const rootCwdSwitchOrdinaryLink = path.join(tempRoot, cwdSwitchName);
const nestedCwdSwitchSecretLink = path.join(nestedDir, cwdSwitchName);
+ const stackOtherDir = path.join(tempRoot, 'stack-other');
+ const stackOtherOrdinaryLink = path.join(stackOtherDir, cwdSwitchName);
const ordinaryGlobDir = path.join(tempRoot, 'ordinary-glob');
const ordinaryGlobPath = path.join(ordinaryGlobDir, 'ordinary.txt');
const dotglobDir = path.join(tempRoot, 'dotglob-only');
@@ -283,6 +285,7 @@ describe('cindy-bridge extension source', () => {
mkdirSync(nestedDir, { recursive: true });
mkdirSync(dashDir, { recursive: true });
mkdirSync(cdPathSubDir, { recursive: true });
+ mkdirSync(stackOtherDir);
mkdirSync(ordinaryGlobDir);
mkdirSync(dotglobDir);
mkdirSync(largeGlobDir);
@@ -317,6 +320,7 @@ describe('cindy-bridge extension source', () => {
symlinkSync(secretPath, cdPathSecretLink);
symlinkSync(ordinaryPath, rootCwdSwitchOrdinaryLink);
symlinkSync(secretPath, nestedCwdSwitchSecretLink);
+ symlinkSync(ordinaryPath, stackOtherOrdinaryLink);
symlinkSync(ordinaryPath, ordinaryLink);
expect(context.collectResolvedCredentialPaths?.({ path: secretLink })).toEqual([
@@ -522,6 +526,23 @@ describe('cindy-bridge extension source', () => {
targets: [rootCwdSwitchOrdinaryLink],
unresolved: false,
});
+ for (const rotation of ['+1', '-1']) {
+ const command = `pushd ${nestedDir} >/dev/null; pushd ${stackOtherDir} >/dev/null; pushd ${rotation} >/dev/null; cat <${cwdSwitchName}`;
+ const evidence = context.bashInputReadEvidence?.({ command });
+ expect(evidence?.unresolved, command).toBe(true);
+ expect(evidence?.targets, command).toContain(stackOtherOrdinaryLink);
+ expect(evidence?.targets.every((target) => [
+ rootCwdSwitchOrdinaryLink,
+ nestedCwdSwitchSecretLink,
+ stackOtherOrdinaryLink,
+ ].includes(target)), command).toBe(true);
+ }
+ expect(context.bashInputReadEvidence?.({
+ command: `pushd -n ${nestedDir} >/dev/null && cat <ordinary-link.txt`,
+ })).toEqual({
+ targets: [ordinaryLink],
+ unresolved: false,
+ });
expect(context.bashInputReadEvidence?.({
command: `true && 2>/dev/null builtin pushd ${nestedDir} && 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.ts
index 81c90f4f33..6087f82a75 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
@@ -2112,21 +2112,25 @@ describe.skipIf(!piAvailable)('PiAgent integration (real pi binary + fake gatewa
const secretPath = path.join(workingDir, 'secrets', '.env');
const ordinaryPath = path.join(workingDir, 'ordinary.txt');
const subDir = path.join(workingDir, 'sub');
+ const stackOtherDir = path.join(workingDir, 'stack-other');
const postCdLink = path.join(subDir, 'link');
const escapedLinkName = 'innocent\\q';
const cdRedirectLinkName = 'cd-innocent';
mkdirSync(path.dirname(secretPath), { recursive: true });
mkdirSync(subDir);
+ mkdirSync(stackOtherDir);
writeFileSync(secretPath, 'FAKE_REDIRECT_DOTENV_SECRET=must-not-leak');
writeFileSync(ordinaryPath, 'ordinary-content');
symlinkSync('../secrets/.env', postCdLink);
symlinkSync(ordinaryPath, path.join(workingDir, 'link'));
+ symlinkSync(ordinaryPath, path.join(stackOtherDir, 'link'));
symlinkSync(secretPath, path.join(workingDir, escapedLinkName));
symlinkSync(secretPath, path.join(workingDir, cdRedirectLinkName));
symlinkSync(ordinaryPath, path.join(subDir, cdRedirectLinkName));
symlinkSync(ordinaryPath, path.join(subDir, 'ordinary'));
for (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-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'],
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 51745262f9..e23fb4ab6f 100644
--- a/packages/maker-core/src/agents/pi/cindy-bridge-source.ts
+++ b/packages/maker-core/src/agents/pi/cindy-bridge-source.ts
@@ -464,6 +464,7 @@ type BashLeadingRedirection = { end: number; unresolved: boolean };
type BashAssignmentPrefix = { end: number; name: string; unresolved: boolean };
type BashDirectoryChangeHead = {
cursor: number;
+ command: 'cd' | 'pushd' | null;
hadRedirection: boolean;
unresolved: boolean;
};
@@ -593,7 +594,7 @@ function bashDirectoryChangeHead(
let word = readShellRedirectionTarget(command, cursor);
if (!word.target || word.unresolved) {
if (unresolved && bashUnresolvedHeadHasDirectoryChange(codeMask, cursor)) {
- return { cursor, hadRedirection, unresolved: true };
+ return { cursor, command: null, hadRedirection, unresolved: true };
}
return null;
}
@@ -613,17 +614,17 @@ function bashDirectoryChangeHead(
cursor = bashInlinePaddingEnd(command, word.end);
}
if (!word.target || word.unresolved) return unresolved
- ? { cursor, hadRedirection, unresolved: true }
+ ? { cursor, command: null, hadRedirection, unresolved: true }
: null;
}
if ((word.target !== 'cd' && word.target !== 'pushd') || word.unresolved) {
if (unresolved && bashUnresolvedHeadHasDirectoryChange(codeMask, cursor)) {
- return { cursor, hadRedirection, unresolved: true };
+ return { cursor, command: null, hadRedirection, unresolved: true };
}
return null;
}
- return { cursor: word.end, hadRedirection, unresolved };
+ return { cursor: word.end, command: word.target, hadRedirection, unresolved };
}
function bashWorkingDirectoryCandidates(command: string, globBudget: BashGlobBudget): BashPathCandidates {
@@ -648,6 +649,7 @@ function bashWorkingDirectoryCandidates(command: string, globBudget: BashGlobBud
if (!bashSubshellScopeRemainsOpen(codeMask, cursor, changeDepth)) continue;
let parsed = readShellRedirectionTarget(command, bashInlinePaddingEnd(command, cursor));
let optionsEnded = false;
+ let suppressDirectoryChange = false;
while (true) {
cursor = bashInlinePaddingEnd(command, cursor);
const redirection = bashLeadingRedirectionAt(command, cursor);
@@ -658,11 +660,19 @@ function bashWorkingDirectoryCandidates(command: string, globBudget: BashGlobBud
parsed = readShellRedirectionTarget(command, bashInlinePaddingEnd(command, cursor));
continue;
}
- if (parsed.target !== '--' && !/^-[LPen]+$/.test(parsed.target)) break;
+ const knownOption = head.command === 'pushd'
+ ? parsed.target === '-n'
+ : parsed.target !== '--' && /^-[LPe]+$/.test(parsed.target);
+ if (parsed.target !== '--' && !knownOption) break;
if (parsed.target === '--') optionsEnded = true;
+ if (head.command === 'pushd' && parsed.target === '-n') suppressDirectoryChange = true;
cursor = parsed.end;
parsed = readShellRedirectionTarget(command, bashInlinePaddingEnd(command, cursor));
}
+ if (head.command === 'pushd' && !optionsEnded && /^[+-]\d+$/.test(parsed.target)) {
+ unresolved = true;
+ continue;
+ }
if (!parsed.target || parsed.unresolved || (!optionsEnded && parsed.target.startsWith('-'))) {
unresolved = true;
continue;
@@ -674,6 +684,7 @@ function bashWorkingDirectoryCandidates(command: string, globBudget: BashGlobBud
}
const operator = operatorEvidence.operator;
if (!operator || operator === '|' || operator === '&') continue;
+ if (suppressDirectoryChange) continue;
if (bashInheritedDirectoryChangeIsUncertain(parsed.target)) {
unresolved = true;
continue;
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.ts
index 8d89e1b5c3..5a18107742 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
@@ -465,6 +465,14 @@ describe('对抗语料 — dotenv 凭证路径', () => {
'grep -ef .env',
'sed -nef .env.local',
'rg -g .env KEY .',
+ "rg --type-add 'dotenv:.env' -tdotenv SECRET .",
+ 'rg --type-add=dotenv:.env --type=dotenv SECRET .',
+ 'rg --type-add dotenv:.env -t dotenv SECRET .',
+ 'rg --type-add dotenv:.env -tall SECRET .',
+ 'rg --type-add a:.env --type-add b:include:a -tb SECRET .',
+ 'rg --type-add dotenv:include:json -tdotenv SECRET .',
+ 'rg --type-add dotenv:.env --type-clear dotenv --type-add dotenv:.env -tdotenv SECRET .',
+ 'rg --type-add malformed -tmalformed SECRET .',
'grep -r --include=.env KEY .',
"grep -r --include='.env*' API_KEY .",
"grep -r --include='*' API_KEY .",
@@ -735,6 +743,10 @@ describe('对抗语料 — dotenv 凭证路径', () => {
['grep -ef data.json', 'auto-approve'],
['grep -ef.env data.json', 'auto-approve'],
["rg -g '[!.]*.ts' KEY .", 'auto-approve'],
+ ['rg --type-add safe:ordinary.txt -tsafe KEY .', 'auto-approve'],
+ ['rg --type-add dotenv:.env -Tdotenv SECRET .', 'auto-approve'],
+ ['rg --type-add dotenv:.env SECRET .', 'auto-approve'],
+ ['rg --type-add dotenv:.env --type-clear dotenv --type-add dotenv:ordinary.txt -tdotenv KEY .', 'auto-approve'],
["grep -r --include='[!.]*.json' KEY .", 'auto-approve'],
["rg --hidden -g '[!.]*.ts' KEY .", 'auto-approve'],
['rg --no-ignore KEY .', 'auto-approve'],
diff --git a/packages/maker-core/src/agents/shared/auto-review.ts b/packages/maker-core/src/agents/shared/auto-review.ts
index 01ed6ce59f..9e1a914395 100644
--- a/packages/maker-core/src/agents/shared/auto-review.ts
+++ b/packages/maker-core/src/agents/shared/auto-review.ts
@@ -4736,7 +4736,17 @@ const FIRST_DATA_ARGUMENT_BINS: ReadonlySet<string> = new Set([
'grep', 'egrep', 'fgrep', 'rg', 'ag', 'sed', 'jq', 'yq', 'date',
]);
-type ReaderOptionKind = 'data' | 'data-file' | 'selector' | 'filter' | 'aux-file' | 'aux-file-list';
+type ReaderOptionKind =
+ | 'data'
+ | 'data-file'
+ | 'selector'
+ | 'filter'
+ | 'aux-file'
+ | 'aux-file-list'
+ | 'type-definition'
+ | 'type-include'
+ | 'type-exclude'
+ | 'type-clear';
type ReaderLongOption = { name: string; kind: ReaderOptionKind };
const GREP_LONG_OPTIONS: readonly ReaderLongOption[] = [
@@ -4753,6 +4763,10 @@ const RG_LONG_OPTIONS: readonly ReaderLongOption[] = [
{ name: '--glob', kind: 'selector' },
{ name: '--iglob', kind: 'selector' },
{ name: '--ignore-file', kind: 'aux-file' },
+ { name: '--type-add', kind: 'type-definition' },
+ { name: '--type', kind: 'type-include' },
+ { name: '--type-not', kind: 'type-exclude' },
+ { name: '--type-clear', kind: 'type-clear' },
];
const SED_LONG_OPTIONS: readonly ReaderLongOption[] = [
{ name: '--expression', kind: 'data' },
@@ -4833,6 +4847,8 @@ function readerShortOptionKind(
if (option === 'e') return 'data';
if (option === 'f') return 'data-file';
if (option === 'g') return 'selector';
+ if (option === 't') return 'type-include';
+ if (option === 'T') return 'type-exclude';
}
if ((bin === 'jq' || bin === 'yq') && option === 'f') return 'data-file';
if (bin === 'ag' && option === 'G') return 'selector';
@@ -5060,10 +5076,79 @@ function grepRecursesIntoPotentialDotenv(bin: string, args: readonly string[]):
return recursive && (includes.length === 0 || includes.some(selectorCouldMatchDotenv));
}
+type RgTypeDefinition = { globs: string[]; includes: string[] };
+
+type ParsedRgTypeSpec = {
+ name: string;
+ glob?: string;
+ includes?: string[];
+};
+
+function parseRgTypeSpec(value: string): ParsedRgTypeSpec | null {
+ const separator = value.indexOf(':');
+ if (separator <= 0 || separator === value.length - 1) return null;
+ const name = value.slice(0, separator);
+ if (!/^[\p{L}\p{N}]+$/u.test(name)) return null;
+ const definition = value.slice(separator + 1);
+ if (!definition.startsWith('include:')) return { name, glob: definition };
+ const includes = definition.slice('include:'.length).split(',');
+ return includes.length > 0 && includes.every((included) => /^[\p{L}\p{N}]+$/u.test(included))
+ ? { name, includes }
+ : null;
+}
+
+function rgCustomTypeCouldMatchDotenv(
+ name: string,
+ definitions: ReadonlyMap<string, RgTypeDefinition>,
+ visiting = new Set<string>(),
+): boolean {
+ if (visiting.has(name)) return true;
+ const definition = definitions.get(name);
+ if (!definition) return true;
+ if (definition.globs.some(selectorCouldMatchDotenv)) return true;
+ const nextVisiting = new Set(visiting).add(name);
+ return definition.includes.some((included) =>
+ rgCustomTypeCouldMatchDotenv(included, definitions, nextVisiting));
+}
+
function rgSearchesPotentialDotenv(args: readonly string[]): boolean {
let hidden = false;
let unrestricted = 0;
+ let typeParsingUnresolved = false;
const globs: string[] = [];
+ const definitions = new Map<string, RgTypeDefinition>();
+ const includedTypes = new Set<string>();
+ const excludedTypes = new Set<string>();
+ const applyTypeOption = (kind: ReaderOptionKind, value: string | undefined): void => {
+ if (!value) {
+ typeParsingUnresolved = true;
+ return;
+ }
+ if (kind === 'type-include') {
+ includedTypes.add(value);
+ return;
+ }
+ if (kind === 'type-exclude') {
+ excludedTypes.add(value);
+ return;
+ }
+ if (kind === 'type-clear') {
+ if (!/^[\p{L}\p{N}]+$/u.test(value)) typeParsingUnresolved = true;
+ else definitions.set(value, { globs: [], includes: [] });
+ return;
+ }
+ if (kind !== 'type-definition') return;
+ const parsed = parseRgTypeSpec(value);
+ if (!parsed) {
+ typeParsingUnresolved = true;
+ return;
+ }
+ const definition = definitions.get(parsed.name) ?? { globs: [], includes: [] };
+ if (parsed.glob !== undefined) definition.globs.push(parsed.glob);
+ if (parsed.includes !== undefined) definition.includes.push(...parsed.includes);
+ definitions.set(parsed.name, definition);
+ };
+
for (let index = 0; index < args.length; index += 1) {
const token = args[index];
if (token === '--') break;
@@ -5078,6 +5163,7 @@ function rgSearchesPotentialDotenv(args: readonly string[]): boolean {
const value = attached ?? args[index + 1];
if (attached === undefined) index += 1;
if (kind === 'selector' && value) globs.push(value);
+ applyTypeOption(kind, value);
continue;
}
if (!/^-[^-]/.test(token)) continue;
@@ -5091,9 +5177,18 @@ function rgSearchesPotentialDotenv(args: readonly string[]): boolean {
const value = attached ?? args[index + 1];
if (attached === undefined) index += 1;
if (kind === 'selector' && value) globs.push(value);
+ applyTypeOption(kind, value);
break;
}
}
+
+ if (typeParsingUnresolved) return true;
+ const selectedCustomTypes = includedTypes.has('all')
+ ? [...definitions.keys()]
+ : [...includedTypes].filter((name) => definitions.has(name));
+ if (!excludedTypes.has('all') && selectedCustomTypes.some((name) =>
+ !excludedTypes.has(name) && rgCustomTypeCouldMatchDotenv(name, definitions))) return true;
+
const positive = globs.filter((glob) => !glob.startsWith('!'));
const positiveCouldMatchDotenv = positive.some(selectorCouldMatchDotenv);
const excludesDotenv = !positiveCouldMatchDotenv