Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(pi): validate approved skill entrypoints
nanaa2f6bd618375
diff --git a/packages/maker-core/src/agents/pi/__tests__/pi-rpc-resource-discovery.integration.test.ts b/packages/maker-core/src/agents/pi/__tests__/pi-rpc-resource-discovery.integration.test.tsindex 24f7e56a78..6edce4d7db 100644--- a/packages/maker-core/src/agents/pi/__tests__/pi-rpc-resource-discovery.integration.test.ts+++ b/packages/maker-core/src/agents/pi/__tests__/pi-rpc-resource-discovery.integration.test.ts@@ -874,6 +874,36 @@ describe.skipIf(!existsSync(PI_BINARY))('Pi v0.83.0 RPC resource discovery facts]));});+ it('reports the exact file provenance for an explicit single-file skill', async () => {+ const fixture = await createFixture('pi-rpc-explicit-file-skill-');+ const explicitSkill = path.join(fixture.workingDir, '.pi', 'skills', 'single-file.md');+ writeFileSync(+ explicitSkill,+ '---\nname: single-file\ndescription: fixture single file\n---\nfixture single file\n',+ );+ const result = await runGetCommands({+ binaryPath: PI_BINARY,+ cwd: fixture.workingDir,+ configHome: fixture.configHome,+ sessionDir: fixture.sessionDir,+ approve: false,+ extraArgs: ['--no-extensions', '--skill', explicitSkill],+ });++ expect(result.commands).toEqual(expect.arrayContaining([+ expect.objectContaining({+ name: 'skill:single-file',+ source: 'skill',+ sourceInfo: expect.objectContaining({+ baseDir: path.dirname(explicitSkill),+ path: explicitSkill,+ source: 'local',+ scope: 'temporary',+ }),+ }),+ ]));+ });+it('reports one deterministic runtime command when explicit approved paths share a skill name', async () => {const fixture = await createFixture('pi-rpc-explicit-duplicate-name-');const first = path.join(fixture.workingDir, '.pi', 'skills', 'duplicate-first');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.tsindex 012debe1f0..a4f0968373 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@@ -47,7 +47,10 @@ const approved = (workingDir: string, revision: string): PiProjectApprovalSnapsh});const available = {- stat: async () => ({ isDirectory: () => true, isFile: () => false }),+ stat: async (candidate: string) => ({+ isDirectory: () => !candidate.toLowerCase().endsWith('.md'),+ isFile: () => candidate.toLowerCase().endsWith('.md'),+ }),realpath: async (skillPath: string) => skillPath,};@@ -122,7 +125,7 @@ describe('Pi approved project resource assembly', () => {{ stat, realpath: available.realpath },);- expect(stat).toHaveBeenCalledTimes(2);+ expect(stat).toHaveBeenCalled();expect(result.skillPaths).toEqual([]);expect(result.diagnostic.reason).toBe('approved-skill-path-unavailable');expect(result.decision?.resources.skills).toBe('eligible');@@ -180,6 +183,83 @@ describe('Pi approved project resource assembly', () => {expect(result.diagnostic.reason).toBe('approved-skill-path-changed');});+ it('invalidates a skill whose SKILL.md entrypoint is retargeted after discovery', async () => {+ 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, {+ stat: available.stat,+ realpath: async (candidate) => candidate === skillFile+ ? '/outside/retargeted-SKILL.md'+ : candidate,+ });++ expect(result.skillPaths).toEqual([]);+ expect(result.diagnostic.reason).toBe('approved-skill-path-changed');+ });++ it('allows a directory SKILL.md symlink that still resolves inside the approved repo', async () => {+ 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, {+ stat: available.stat,+ realpath: async (candidate) => candidate === skillFile+ ? '/repo-a/packages/shared/demo.md'+ : candidate,+ });++ expect(result.skillPaths).toEqual(input.discovered.skills);+ expect(result.diagnostic.reason).toBe('approval-matched');+ });++ it('allows an approved single-file markdown skill', async () => {+ const workingDir = '/repo-a/packages/app';+ const skillFile = `${workingDir}/.pi/skills/demo.md`;+ const result = await assembleApprovedPiProjectResources(+ inputFor(workingDir, approved(workingDir, 'rev-a'), [skillFile]),+ available,+ );++ expect(result.skillPaths).toEqual([skillFile]);+ expect(result.diagnostic.reason).toBe('approval-matched');+ expect(reconcilePiProjectResourceRuntime(result, {+ capturedAt: '2026-08-10T00:00:00.000Z',+ generation: 1,+ status: 'loaded',+ source: 'pi:get_commands',+ commands: [{+ name: 'skill:demo-file',+ source: 'skill',+ sourceInfo: {+ scope: 'temporary',+ source: 'local',+ baseDir: `${workingDir}/.pi/skills`,+ path: skillFile,+ },+ }],+ })).toMatchObject({+ reason: 'runtime-skills-confirmed',+ loadedSkillCount: 1,+ });+ });++ it('invalidates a skill whose SKILL.md entrypoint disappeared before launch', async () => {+ 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, {+ stat: async (candidate) => {+ if (candidate === skillFile) throw new Error('ENOENT');+ return available.stat(candidate);+ },+ realpath: available.realpath,+ });++ expect(result.skillPaths).toEqual([]);+ expect(result.diagnostic.reason).toBe('approved-skill-path-unavailable');+ });+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'));@@ -272,5 +352,22 @@ describe('Pi approved project resource assembly', () => {reason: 'runtime-skills-missing',loadedSkillCount: 0,});++ expect(reconcilePiProjectResourceRuntime(assembly, {+ ...baseManifest,+ commands: [{+ name: 'skill:demo',+ source: 'skill',+ sourceInfo: {+ scope: 'temporary',+ source: 'local',+ baseDir: `${skillPath}\0outside`,+ path: `${skillPath}\0outside/SKILL.md`,+ },+ }],+ })).toMatchObject({+ reason: 'runtime-skills-missing',+ loadedSkillCount: 0,+ });});});diff --git a/packages/maker-core/src/agents/pi/__tests__/project-trust.test.ts b/packages/maker-core/src/agents/pi/__tests__/project-trust.test.tsindex 8759c885fd..acafd254d3 100644--- a/packages/maker-core/src/agents/pi/__tests__/project-trust.test.ts+++ b/packages/maker-core/src/agents/pi/__tests__/project-trust.test.ts@@ -2,6 +2,7 @@ import { describe, expect, it } from 'vitest';import {evaluatePiProjectTrust,+ piCanonicalPathIsWithin,piCanonicalPathsEqual,piProjectKey,} from '../project-trust.js';@@ -63,6 +64,15 @@ describe('Pi project trust contract', () => {}, 'C:\\Repo\\Skill', 'c:/repo/skill')).toBe(true);});+ it('checks canonical containment without prefix or Windows case aliases', () => {+ expect(piCanonicalPathIsWithin(identity, '/repo', '/repo/skills/demo')).toBe(true);+ expect(piCanonicalPathIsWithin(identity, '/repo', '/repository/skills/demo')).toBe(false);+ expect(piCanonicalPathIsWithin({+ platform: 'win32',+ windowsCaseComparison: 'ordinal-insensitive',+ }, 'C:\\Repo', 'c:\\repo\\skills\\demo')).toBe(true);+ });+it('uses canonical repo root + workingDir and isolates sibling workingDirs', () => {expect(piProjectKey(identity)).toBe('/repo\0/repo/packages/app');expect(evaluatePiProjectTrust({ identity, approval: approval(), discovered }).status).toBe('approved');diff --git a/packages/maker-core/src/agents/pi/project-resource-assembly.ts b/packages/maker-core/src/agents/pi/project-resource-assembly.tsindex 9adda57df6..f9fc1bb64f 100644--- a/packages/maker-core/src/agents/pi/project-resource-assembly.ts+++ b/packages/maker-core/src/agents/pi/project-resource-assembly.ts@@ -6,7 +6,11 @@ import type {PiProjectTrustInputSnapshot,PiProjectTrustStatus,} from '../../types/pi-project-trust.js';-import { evaluatePiProjectTrust, piCanonicalPathsEqual } from './project-trust.js';+import {+ evaluatePiProjectTrust,+ piCanonicalPathIsWithin,+ piCanonicalPathsEqual,+} from './project-trust.js';import type {PiProjectResourceRuntimeDiagnostic,PiRuntimeCapabilityManifest,@@ -58,19 +62,34 @@ async function validateSkillPathsImmediatelyBeforeLaunch(const [resolvedWorkingDir, resolvedRepoRoot, entries] = await Promise.all([realpath(identity.workingDir),realpath(canonicalRepoRoot),- Promise.all(skillPaths.map(async (skillPath) => ({- skillPath,- stats: await stat(skillPath),- resolvedPath: await realpath(skillPath),- }))),+ 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 = path.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 (entries.some(({ stats }) => !stats.isDirectory() && !stats.isFile())) return 'unavailable';- return entries.every(({ skillPath, resolvedPath }) =>- piCanonicalPathsEqual(identity, skillPath, resolvedPath))+ if (entries.some(({ skillPath, stats, skillFileStats }) =>+ (!stats.isDirectory() && (!stats.isFile() || path.extname(skillPath) !== '.md'))+ || (stats.isDirectory() && !skillFileStats?.isFile()))) return 'unavailable';+ return entries.every(({ skillPath, resolvedPath, stats, resolvedSkillFile }) =>+ piCanonicalPathsEqual(identity, skillPath, resolvedPath)+ && piCanonicalPathIsWithin(identity, canonicalRepoRoot, resolvedPath)+ && (!stats.isDirectory() || (+ typeof resolvedSkillFile === 'string'+ && piCanonicalPathIsWithin(identity, canonicalRepoRoot, resolvedSkillFile)+ )))? 'available': 'skill-changed';} catch {diff --git a/packages/maker-core/src/agents/pi/project-trust.ts b/packages/maker-core/src/agents/pi/project-trust.tsindex 122e810dd7..5b6f8e8079 100644--- a/packages/maker-core/src/agents/pi/project-trust.ts+++ b/packages/maker-core/src/agents/pi/project-trust.ts@@ -70,6 +70,19 @@ export function piCanonicalPathsEqual(return left !== null && right !== null && left === right;}+/** Check containment using the same canonical comparison identity as approval keys. */+export function piCanonicalPathIsWithin(+ identity: Pick<PiProjectIdentityResolution, 'platform' | 'windowsCaseComparison'>,+ root: string,+ candidate: string,+): boolean {+ const normalizedRoot = normalizePath(root, identity.platform, identity.windowsCaseComparison);+ const normalizedCandidate = normalizePath(candidate, identity.platform, identity.windowsCaseComparison);+ if (normalizedRoot === null || normalizedCandidate === null) return false;+ const rootPrefix = normalizedRoot.endsWith('/') ? normalizedRoot : `${normalizedRoot}/`;+ return normalizedCandidate === normalizedRoot || normalizedCandidate.startsWith(rootPrefix);+}+function hasLosslessCanonicalEncoding(identity: Pick<PiProjectIdentityResolution, 'platform' | 'canonicalPathEncoding' | 'windowsCaseComparison'>,): boolean {diff --git a/packages/maker-core/src/agents/pi/skill-runtime-provenance.ts b/packages/maker-core/src/agents/pi/skill-runtime-provenance.tsindex 58884254d6..729272d35d 100644--- a/packages/maker-core/src/agents/pi/skill-runtime-provenance.ts+++ b/packages/maker-core/src/agents/pi/skill-runtime-provenance.ts@@ -16,8 +16,10 @@ export function piExplicitSkillRuntimePath(command: PiRuntimeCommand): string ||| command.sourceInfo.source !== 'local'|| typeof baseDir !== 'string'|| typeof skillFile !== 'string'- || path.basename(skillFile) !== 'SKILL.md'+ || baseDir.includes('\0')+ || skillFile.includes('\0')|| path.resolve(path.dirname(skillFile)) !== path.resolve(baseDir)) return null;- return baseDir;+ if (path.basename(skillFile) === 'SKILL.md') return baseDir;+ return path.extname(skillFile) === '.md' ? skillFile : null;}diff --git a/packages/maker-core/src/maker.test.ts b/packages/maker-core/src/maker.test.tsindex b9bbfeb38e..e3b5851d50 100644--- a/packages/maker-core/src/maker.test.ts+++ b/packages/maker-core/src/maker.test.ts@@ -931,6 +931,16 @@ describe('Maker Pi runtime skill status', () => {path: `/repo/.pi/skills/${opts.sessionId}-skill/SKILL.md`,},},+ {+ name: 'skill:single-file-frontmatter-name',+ source: 'skill',+ sourceInfo: {+ source: 'local',+ scope: 'temporary',+ baseDir: '/repo/.pi/skills',+ path: '/repo/.pi/skills/single-file.md',+ },+ },{name: 'skill:user-collision',source: 'skill',@@ -963,6 +973,7 @@ describe('Maker Pi runtime skill status', () => {projectSkill('one-skill', '/repo/.pi/skills/one-skill'),projectSkill('one-skill', '/repo/.agents/skills/one-skill'),projectSkill('two-skill', '/repo/.pi/skills/two-skill'),+ projectSkill('single-file', '/repo/.pi/skills/single-file.md'),projectSkill('user-collision', '/repo/.pi/skills/user-collision'),projectSkill('malformed-collision', '/repo/.pi/skills/malformed-collision'),],@@ -997,6 +1008,7 @@ describe('Maker Pi runtime skill status', () => {['one-skill', 'loaded'],['one-skill', 'discovered'],['two-skill', 'discovered'],+ ['single-file', 'loaded'],['user-collision', 'discovered'],['malformed-collision', 'discovered'],]);@@ -1005,10 +1017,16 @@ describe('Maker Pi runtime skill status', () => {runtimeStatus: 'loaded',runtimeCommandName: 'skill:one-frontmatter-name',});+ expect(one.skills[3]).toMatchObject({+ name: 'single-file',+ runtimeStatus: 'loaded',+ runtimeCommandName: 'skill:single-file-frontmatter-name',+ });expect(two.skills.map((skill) => [skill.name, skill.runtimeStatus])).toEqual([['one-skill', 'discovered'],['one-skill', 'discovered'],['two-skill', 'loaded'],+ ['single-file', 'loaded'],['user-collision', 'discovered'],['malformed-collision', 'discovered'],]);