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): bind approvals to requested workdir

nana449d30e7325f
diff --git a/packages/maker-core/src/agents/pi/__tests__/pi-startsession-cleanup.test.ts b/packages/maker-core/src/agents/pi/__tests__/pi-startsession-cleanup.test.ts
index c62eb37da8..469a8fcb2b 100644
--- a/packages/maker-core/src/agents/pi/__tests__/pi-startsession-cleanup.test.ts
+++ b/packages/maker-core/src/agents/pi/__tests__/pi-startsession-cleanup.test.ts
@@ -293,6 +293,38 @@ describe('PiAgent.startSession failure cleanup (mocked pi process)', () => {
await Promise.all([approvedHandle.close(), revokedHandle.close()]);
});
+ it('fails closed when the resolver returns another workingDir approval snapshot', async () => {
+ const approvedDir = realpathSync.native(mkdtempSync(path.join(tmpdir(), 'pi-approved-other-')));
+ try {
+ const skillPath = path.join(approvedDir, '.pi', 'skills', 'other-project-skill');
+ mkdirSync(skillPath, { recursive: true });
+ writeFileSync(path.join(skillPath, 'SKILL.md'), '# other project\n');
+ const agent = new PiAgent(buildDeps({
+ resolvePiProjectTrustInput: async () => approvedInput(
+ approvedDir,
+ 'rev-other-project',
+ [skillPath],
+ ),
+ }));
+ const handle = await agent.startSession({ sessionId: 'requested', workingDir: cwd, model: 'm' });
+ try {
+ expect(repeatedArgValues(knobs.spawnedArgs[0]!, '--skill')).toEqual([]);
+ await vi.waitFor(() => {
+ expect(handle.getRuntimeCapabilities?.()?.projectResources).toMatchObject({
+ status: 'approved',
+ reason: 'approval-working-dir-mismatch',
+ approvalRevision: 'rev-other-project',
+ requestedSkillCount: 0,
+ });
+ });
+ } finally {
+ await handle.close();
+ }
+ } finally {
+ rmSync(approvedDir, { recursive: true, force: true });
+ }
+ });
+
it('injects remote MCP secrets only through env and marks them for bash-child stripping', async () => {
const agent = new PiAgent(buildDeps());
const handle = await agent.startSession(opts());
diff --git a/packages/maker-core/src/agents/pi/__tests__/project-resource-assembly.test.ts b/packages/maker-core/src/agents/pi/__tests__/project-resource-assembly.test.ts
index a4f0968373..c8b0703dc5 100644
--- a/packages/maker-core/src/agents/pi/__tests__/project-resource-assembly.test.ts
+++ b/packages/maker-core/src/agents/pi/__tests__/project-resource-assembly.test.ts
@@ -58,7 +58,7 @@ describe('Pi approved project resource assembly', () => {
it('freezes only explicitly eligible project skill paths', async () => {
const workingDir = '/repo-a/packages/app';
const input = inputFor(workingDir, approved(workingDir, 'rev-a'));
- const result = await assembleApprovedPiProjectResources(input, available);
+ const result = await assembleApprovedPiProjectResources(input, workingDir, available);
expect(result.skillPaths).toEqual(input.discovered.skills);
expect(result.diagnostic).toEqual({
@@ -91,6 +91,7 @@ describe('Pi approved project resource assembly', () => {
const workingDir = '/repo-a/packages/app';
const result = await assembleApprovedPiProjectResources(
inputFor(workingDir, approval),
+ workingDir,
available,
);
@@ -122,6 +123,7 @@ describe('Pi approved project resource assembly', () => {
});
const result = await assembleApprovedPiProjectResources(
inputFor(workingDir, approved(workingDir, 'rev-a'), [first, missing]),
+ workingDir,
{ stat, realpath: available.realpath },
);
@@ -138,7 +140,7 @@ describe('Pi approved project resource assembly', () => {
discoveredPath: input.discovered.skills[0]!,
canonicalPath: '/outside/retargeted-skill',
}];
- const result = await assembleApprovedPiProjectResources(input, available);
+ const result = await assembleApprovedPiProjectResources(input, workingDir, available);
expect(result.skillPaths).toEqual([]);
expect(result.diagnostic.reason).toBe('approved-skills-ineligible');
@@ -151,10 +153,12 @@ describe('Pi approved project resource assembly', () => {
const [first, second] = await Promise.all([
assembleApprovedPiProjectResources(
inputFor(firstDir, approved(firstDir, 'rev-a')),
+ firstDir,
available,
),
assembleApprovedPiProjectResources(
inputFor(secondDir, { status: 'revoked', revision: 'rev-b', reason: 'user-revoked' }),
+ secondDir,
available,
),
]);
@@ -172,7 +176,7 @@ describe('Pi approved project resource assembly', () => {
it('invalidates the whole set when a canonical skill path is retargeted after approval', async () => {
const workingDir = '/repo-a/packages/app';
const input = inputFor(workingDir, approved(workingDir, 'rev-a'));
- const result = await assembleApprovedPiProjectResources(input, {
+ const result = await assembleApprovedPiProjectResources(input, workingDir, {
stat: available.stat,
realpath: async (candidate) => candidate === input.discovered.skills[0]
? '/outside/retargeted-skill'
@@ -187,7 +191,7 @@ describe('Pi approved project resource assembly', () => {
const workingDir = '/repo-a/packages/app';
const input = inputFor(workingDir, approved(workingDir, 'rev-a'));
const skillFile = `${input.discovered.skills[0]}/SKILL.md`;
- const result = await assembleApprovedPiProjectResources(input, {
+ const result = await assembleApprovedPiProjectResources(input, workingDir, {
stat: available.stat,
realpath: async (candidate) => candidate === skillFile
? '/outside/retargeted-SKILL.md'
@@ -202,7 +206,7 @@ describe('Pi approved project resource assembly', () => {
const workingDir = '/repo-a/packages/app';
const input = inputFor(workingDir, approved(workingDir, 'rev-a'));
const skillFile = `${input.discovered.skills[0]}/SKILL.md`;
- const result = await assembleApprovedPiProjectResources(input, {
+ const result = await assembleApprovedPiProjectResources(input, workingDir, {
stat: available.stat,
realpath: async (candidate) => candidate === skillFile
? '/repo-a/packages/shared/demo.md'
@@ -218,6 +222,7 @@ describe('Pi approved project resource assembly', () => {
const skillFile = `${workingDir}/.pi/skills/demo.md`;
const result = await assembleApprovedPiProjectResources(
inputFor(workingDir, approved(workingDir, 'rev-a'), [skillFile]),
+ workingDir,
available,
);
@@ -248,7 +253,7 @@ describe('Pi approved project resource assembly', () => {
const workingDir = '/repo-a/packages/app';
const input = inputFor(workingDir, approved(workingDir, 'rev-a'));
const skillFile = `${input.discovered.skills[0]}/SKILL.md`;
- const result = await assembleApprovedPiProjectResources(input, {
+ const result = await assembleApprovedPiProjectResources(input, workingDir, {
stat: async (candidate) => {
if (candidate === skillFile) throw new Error('ENOENT');
return available.stat(candidate);
@@ -263,7 +268,7 @@ describe('Pi approved project resource assembly', () => {
it('invalidates approved skills when the lexical workingDir is retargeted', async () => {
const workingDir = '/repo-a/packages/app-link';
const input = inputFor(workingDir, approved(workingDir, 'rev-a'));
- const result = await assembleApprovedPiProjectResources(input, {
+ const result = await assembleApprovedPiProjectResources(input, workingDir, {
stat: available.stat,
realpath: async (candidate) => candidate === input.identity.workingDir
? '/outside/retargeted-working-dir'
@@ -274,10 +279,30 @@ describe('Pi approved project resource assembly', () => {
expect(result.diagnostic.reason).toBe('approved-project-path-changed');
});
+ it('rejects an internally valid approval snapshot for another requested workingDir', async () => {
+ const approvedDir = '/repo-a/packages/app';
+ const requestedDir = '/repo-b/packages/app';
+ const input = inputFor(approvedDir, approved(approvedDir, 'rev-a'));
+ const result = await assembleApprovedPiProjectResources(
+ input,
+ requestedDir,
+ available,
+ );
+
+ expect(result.skillPaths).toEqual([]);
+ expect(result.diagnostic).toMatchObject({
+ status: 'approved',
+ reason: 'approval-working-dir-mismatch',
+ approvalRevision: 'rev-a',
+ requestedSkillCount: 0,
+ });
+ });
+
it('reports loaded only when this get_commands catalog confirms every explicit path', async () => {
const workingDir = '/repo-a/packages/app';
const assembly = await assembleApprovedPiProjectResources(
inputFor(workingDir, approved(workingDir, 'rev-a')),
+ workingDir,
available,
);
const skillPath = assembly.skillPaths[0]!;
diff --git a/packages/maker-core/src/agents/pi/index.ts b/packages/maker-core/src/agents/pi/index.ts
index e5a1d84663..1169acdfc3 100644
--- a/packages/maker-core/src/agents/pi/index.ts
+++ b/packages/maker-core/src/agents/pi/index.ts
@@ -1059,7 +1059,10 @@ export class PiAgent extends BaseAgent {
workingDir: opts.workingDir,
...(opts.remoteHostId ? { remoteHostId: opts.remoteHostId } : {}),
});
- projectResourceAssembly = await assembleApprovedPiProjectResources(trustInput);
+ projectResourceAssembly = await assembleApprovedPiProjectResources(
+ trustInput,
+ opts.workingDir,
+ );
} catch {
projectResourceAssembly = unavailablePiProjectResourceAssembly(
'approval-resolver-failed',
diff --git a/packages/maker-core/src/agents/pi/project-resource-assembly.ts b/packages/maker-core/src/agents/pi/project-resource-assembly.ts
index 92d1a10c58..627697682e 100644
--- a/packages/maker-core/src/agents/pi/project-resource-assembly.ts
+++ b/packages/maker-core/src/agents/pi/project-resource-assembly.ts
@@ -54,33 +54,39 @@ async function validateSkillPathsImmediatelyBeforeLaunch(
stat: (path: string) => Promise<{ isDirectory(): boolean; isFile(): boolean }>,
realpath: (path: string) => Promise<string>,
identity: PiProjectTrustInputSnapshot['identity'],
-): Promise<'available' | 'unavailable' | 'project-changed' | 'skill-changed'> {
+ requestedWorkingDir: string,
+): Promise<'available' | 'unavailable' | 'request-mismatch' | 'project-changed' | 'skill-changed'> {
try {
const canonicalWorkingDir = identity.canonicalWorkingDir;
const canonicalRepoRoot = identity.canonicalRepoRoot;
if (!canonicalWorkingDir || !canonicalRepoRoot) return 'unavailable';
const pathApi = identity.platform === 'win32' ? path.win32 : path.posix;
- const [resolvedWorkingDir, resolvedRepoRoot, entries] = await Promise.all([
- realpath(identity.workingDir),
- realpath(canonicalRepoRoot),
- Promise.all(skillPaths.map(async (skillPath) => {
- const stats = await stat(skillPath);
- const resolvedPath = await realpath(skillPath);
- if (!stats.isDirectory()) return { skillPath, stats, resolvedPath };
- const skillFile = pathApi.join(skillPath, 'SKILL.md');
- return {
- skillPath,
- stats,
- resolvedPath,
- skillFileStats: await stat(skillFile),
- resolvedSkillFile: await realpath(skillFile),
- };
- })),
- ]);
+ const [resolvedWorkingDir, resolvedRequestedWorkingDir, resolvedRepoRoot, entries] =
+ await Promise.all([
+ realpath(identity.workingDir),
+ realpath(requestedWorkingDir),
+ realpath(canonicalRepoRoot),
+ Promise.all(skillPaths.map(async (skillPath) => {
+ const stats = await stat(skillPath);
+ const resolvedPath = await realpath(skillPath);
+ if (!stats.isDirectory()) return { skillPath, stats, resolvedPath };
+ const skillFile = pathApi.join(skillPath, 'SKILL.md');
+ return {
+ skillPath,
+ stats,
+ resolvedPath,
+ skillFileStats: await stat(skillFile),
+ resolvedSkillFile: await realpath(skillFile),
+ };
+ })),
+ ]);
if (
!piCanonicalPathsEqual(identity, canonicalWorkingDir, resolvedWorkingDir)
|| !piCanonicalPathsEqual(identity, canonicalRepoRoot, resolvedRepoRoot)
) return 'project-changed';
+ if (!piCanonicalPathsEqual(identity, canonicalWorkingDir, resolvedRequestedWorkingDir)) {
+ return 'request-mismatch';
+ }
if (entries.some(({ skillPath, stats, skillFileStats }) =>
(!stats.isDirectory() && (!stats.isFile() || pathApi.extname(skillPath) !== '.md'))
|| (stats.isDirectory() && !skillFileStats?.isFile()))) return 'unavailable';
@@ -100,11 +106,13 @@ async function validateSkillPathsImmediatelyBeforeLaunch(
/**
* Convert one host-owned approval snapshot into a frozen, skills-only launch
- * snapshot. A missing/changed path invalidates the whole approved set so a
- * partial launch cannot silently diverge from the audited evidence.
+ * snapshot. The caller's actual workingDir is rebound to that snapshot here;
+ * a missing/changed path invalidates the whole approved set so a partial or
+ * cross-project launch cannot silently diverge from the audited evidence.
*/
export async function assembleApprovedPiProjectResources(
input: PiProjectTrustInputSnapshot | null,
+ requestedWorkingDir: string,
options: {
stat?: (path: string) => Promise<{ isDirectory(): boolean; isFile(): boolean }>;
realpath?: (path: string) => Promise<string>;
@@ -134,13 +142,13 @@ export async function assembleApprovedPiProjectResources(
options.stat ?? fs.stat,
options.realpath ?? fs.realpath,
input.identity,
+ requestedWorkingDir,
);
if (pathStatus !== 'available') {
- reason = pathStatus === 'project-changed'
- ? 'approved-project-path-changed'
- : pathStatus === 'skill-changed'
- ? 'approved-skill-path-changed'
- : 'approved-skill-path-unavailable';
+ if (pathStatus === 'request-mismatch') reason = 'approval-working-dir-mismatch';
+ else if (pathStatus === 'project-changed') reason = 'approved-project-path-changed';
+ else if (pathStatus === 'skill-changed') reason = 'approved-skill-path-changed';
+ else reason = 'approved-skill-path-unavailable';
skillPaths = [];
}
}