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): 将 Codex 全局技能同步给 Pi (#2913)

nanaco69fd12adc2e1
diff --git a/apps/desktop/src/main/__tests__/sharedGlobalSkills.test.ts b/apps/desktop/src/main/__tests__/sharedGlobalSkills.test.ts
index 864de5f128..2638dc5bb5 100644
--- a/apps/desktop/src/main/__tests__/sharedGlobalSkills.test.ts
+++ b/apps/desktop/src/main/__tests__/sharedGlobalSkills.test.ts
@@ -105,7 +105,7 @@ describe('prepareSharedGlobalSkillLinks', () => {
expect(await sameRealPath(path.join(paths.claudeSkillsDir, 'shared-only'), sharedSkill)).toBe(true);
});
- it('links existing Codex skills into Claude without creating a shared duplicate', async () => {
+ it('links existing Codex skills into Claude and the shared skills root', async () => {
const root = await makeTmpDir();
const homeDir = path.join(root, 'home');
const paths = sharedGlobalSkillsPaths(homeDir);
@@ -117,9 +117,112 @@ describe('prepareSharedGlobalSkillLinks', () => {
expect(secondResult.changed).toBe(false);
expect(secondResult.warnings).toEqual([]);
expect(await sameRealPath(path.join(paths.claudeSkillsDir, 'codex-only'), codexSkill)).toBe(true);
- await expect(fs.lstat(path.join(paths.sharedSkillsDir, 'codex-only'))).rejects.toMatchObject({
+ expect(await sameRealPath(path.join(paths.sharedSkillsDir, 'codex-only'), codexSkill)).toBe(true);
+ });
+
+ it('stops before the Codex-to-shared write when the owner changes after Claude fanout', async () => {
+ const root = await makeTmpDir();
+ const homeDir = path.join(root, 'home');
+ const paths = sharedGlobalSkillsPaths(homeDir);
+ await writeSkill(paths.codexSkillsDir, 'codex-owner-change');
+
+ let ownerStable = true;
+ const realSymlink = fs.symlink;
+ const symlinkSpy = vi.spyOn(fs, 'symlink').mockImplementation(async (...args) => {
+ await realSymlink(...args);
+ ownerStable = false;
+ });
+ try {
+ await expect(
+ prepareSharedGlobalSkillLinks({
+ homeDir,
+ assertOwnerStable: () => {
+ if (!ownerStable) throw new Error('owner changed');
+ },
+ }),
+ ).rejects.toThrow('owner changed');
+ } finally {
+ symlinkSpy.mockRestore();
+ }
+
+ expect(await sameRealPath(
+ path.join(paths.claudeSkillsDir, 'codex-owner-change'),
+ path.join(paths.codexSkillsDir, 'codex-owner-change'),
+ )).toBe(true);
+ await expect(
+ fs.lstat(path.join(paths.sharedSkillsDir, 'codex-owner-change')),
+ ).rejects.toMatchObject({ code: 'ENOENT' });
+ });
+
+ it('does not overwrite a conflicting shared skill directory with a Codex skill', async () => {
+ const root = await makeTmpDir();
+ const homeDir = path.join(root, 'home');
+ const paths = sharedGlobalSkillsPaths(homeDir);
+ const sharedSkill = await writeSkill(paths.sharedSkillsDir, 'duplicate');
+ const codexSkill = await writeSkill(paths.codexSkillsDir, 'duplicate');
+
+ const result = await prepareSharedGlobalSkillLinks({ homeDir });
+
+ expect(result.warnings.some((warning) => warning.includes('duplicate'))).toBe(true);
+ expect(await sameRealPath(path.join(paths.sharedSkillsDir, 'duplicate'), sharedSkill)).toBe(true);
+ expect(await sameRealPath(path.join(paths.sharedSkillsDir, 'duplicate'), codexSkill)).toBe(false);
+ });
+
+ it('does not overwrite a user-owned shared symlink with a Codex skill', async () => {
+ const root = await makeTmpDir();
+ const homeDir = path.join(root, 'home');
+ const paths = sharedGlobalSkillsPaths(homeDir);
+ const codexSkill = await writeSkill(paths.codexSkillsDir, 'user-link');
+ const externalSkill = await writeSkill(path.join(root, 'external-skills'), 'user-link');
+ const sharedLink = path.join(paths.sharedSkillsDir, 'user-link');
+ await fs.mkdir(paths.sharedSkillsDir, { recursive: true });
+ await fs.symlink(externalSkill, sharedLink, process.platform === 'win32' ? 'junction' : 'dir');
+
+ const result = await prepareSharedGlobalSkillLinks({ homeDir });
+
+ expect(result.warnings.some((warning) => warning.includes('user-link'))).toBe(true);
+ expect(await sameRealPath(sharedLink, externalSkill)).toBe(true);
+ expect(await sameRealPath(sharedLink, codexSkill)).toBe(false);
+ });
+
+ it('cleans up shared and Claude links after a Codex source skill is removed', async () => {
+ const root = await makeTmpDir();
+ const homeDir = path.join(root, 'home');
+ const paths = sharedGlobalSkillsPaths(homeDir);
+ const codexSkill = await writeSkill(paths.codexSkillsDir, 'removed-codex-skill');
+
+ await prepareSharedGlobalSkillLinks({ homeDir });
+ expect(await sameRealPath(path.join(paths.sharedSkillsDir, 'removed-codex-skill'), codexSkill)).toBe(true);
+ expect(await sameRealPath(path.join(paths.claudeSkillsDir, 'removed-codex-skill'), codexSkill)).toBe(true);
+
+ await fs.rm(codexSkill, { recursive: true, force: true });
+ const result = await prepareSharedGlobalSkillLinks({ homeDir });
+
+ expect(result.changed).toBe(true);
+ await expect(fs.lstat(path.join(paths.sharedSkillsDir, 'removed-codex-skill'))).rejects.toMatchObject({
code: 'ENOENT',
});
+ await expect(fs.lstat(path.join(paths.claudeSkillsDir, 'removed-codex-skill'))).rejects.toMatchObject({
+ code: 'ENOENT',
+ });
+ });
+
+ it('filters Codex links that already point into the shared root', async () => {
+ const root = await makeTmpDir();
+ const homeDir = path.join(root, 'home');
+ const paths = sharedGlobalSkillsPaths(homeDir);
+ const sharedSkill = await writeSkill(paths.sharedSkillsDir, 'shared-backlink');
+ const codexLink = path.join(paths.codexSkillsDir, 'shared-backlink');
+ await fs.mkdir(paths.codexSkillsDir, { recursive: true });
+ await fs.symlink(sharedSkill, codexLink, process.platform === 'win32' ? 'junction' : 'dir');
+
+ await prepareSharedGlobalSkillLinks({ homeDir });
+ const secondResult = await prepareSharedGlobalSkillLinks({ homeDir });
+
+ expect(secondResult.changed).toBe(false);
+ expect(secondResult.warnings).toEqual([]);
+ expect(await sameRealPath(codexLink, sharedSkill)).toBe(true);
+ expect(await sameRealPath(path.join(paths.claudeSkillsDir, 'shared-backlink'), sharedSkill)).toBe(true);
});
it('does not overwrite conflicting real skill directories', async () => {
diff --git a/apps/desktop/src/main/maker-host/__tests__/piAuthEnv.test.ts b/apps/desktop/src/main/maker-host/__tests__/piAuthEnv.test.ts
index a3b2492a86..9a90946e7b 100644
--- a/apps/desktop/src/main/maker-host/__tests__/piAuthEnv.test.ts
+++ b/apps/desktop/src/main/maker-host/__tests__/piAuthEnv.test.ts
@@ -19,6 +19,7 @@ vi.mock('electron', () => ({
vi.mock('../auth-adapters.js', () => ({
readClaudeApiKey: () => h.gatewayKey,
desktopCodexAuthAdapter: { getState: async () => ({ authenticated: false }) },
+ desktopClaudeAuthAdapter: { ensureSharedGlobalSkills: async () => undefined },
}));
vi.mock('../custom-provider-header-secrets.js', () => ({
listCustomProvidersWithSecureHeaders: async () => [
diff --git a/apps/desktop/src/main/maker-host/__tests__/piByomNoCindyAuth.test.ts b/apps/desktop/src/main/maker-host/__tests__/piByomNoCindyAuth.test.ts
index 9d41a81f88..9808128161 100644
--- a/apps/desktop/src/main/maker-host/__tests__/piByomNoCindyAuth.test.ts
+++ b/apps/desktop/src/main/maker-host/__tests__/piByomNoCindyAuth.test.ts
@@ -1,6 +1,6 @@
import { beforeEach, describe, expect, it, vi } from 'vitest';
-const grokOAuth = vi.hoisted(() => ({ loggedIn: false }));
+const state = vi.hoisted(() => ({ loggedIn: false, sharedSkillRefreshes: 0 }));
vi.mock('electron', () => ({
app: {
@@ -11,6 +11,11 @@ vi.mock('electron', () => ({
}));
vi.mock('../auth-adapters.js', () => ({
readClaudeApiKey: () => null,
+ desktopClaudeAuthAdapter: {
+ ensureSharedGlobalSkills: async () => {
+ state.sharedSkillRefreshes += 1;
+ },
+ },
desktopCodexAuthAdapter: { getState: async () => ({ authenticated: false }) },
}));
vi.mock('../custom-provider-header-secrets.js', () => ({
@@ -39,9 +44,9 @@ vi.mock('../../secrets/providerSecretStore.js', () => ({
providerId === 'xai' ? 'legacy-custom-key' : null,
}));
vi.mock('../grok-oauth-login.js', () => ({
- hasGrokOAuthLogin: () => grokOAuth.loggedIn,
+ hasGrokOAuthLogin: () => state.loggedIn,
getGrokAccessToken: async () => 'grok-test-token',
- peekGrokAccessToken: () => (grokOAuth.loggedIn ? 'grok-test-token' : null),
+ peekGrokAccessToken: () => (state.loggedIn ? 'grok-test-token' : null),
recoverGrokAuthAfterRejection: async () => 'superseded' as const,
}));
@@ -49,7 +54,25 @@ import { desktopPiAuthAdapter, resolvePiNativeProviders } from '../pi-host.js';
describe('Pi pure BYOM auth without a Cindy account', () => {
beforeEach(() => {
- grokOAuth.loggedIn = false;
+ state.loggedIn = false;
+ state.sharedSkillRefreshes = 0;
+ });
+
+ it('refreshes local shared skills before provider resolution and skips remote homes', async () => {
+ await resolvePiNativeProviders({
+ workingDir: '/tmp/project',
+ providerId: 'local-keyless',
+ model: 'local-model',
+ });
+ expect(state.sharedSkillRefreshes).toBe(1);
+
+ await resolvePiNativeProviders({
+ workingDir: '/remote/project',
+ remoteHostId: 'remote-1',
+ providerId: 'local-keyless',
+ model: 'local-model',
+ });
+ expect(state.sharedSkillRefreshes).toBe(1);
});
it('authenticates the native provider and only uses an inert gateway placeholder', async () => {
@@ -78,7 +101,7 @@ describe('Pi pure BYOM auth without a Cindy account', () => {
});
it('keeps an official xai resume on SuperGrok when a custom provider has the same model', async () => {
- grokOAuth.loggedIn = true;
+ state.loggedIn = true;
const resolved = await resolvePiNativeProviders({
workingDir: '/tmp/project',
providerId: 'xai',
@@ -102,7 +125,7 @@ describe('Pi pure BYOM auth without a Cindy account', () => {
});
it('restores a migrated custom:xai session from its saved endpoint after SuperGrok is connected', async () => {
- grokOAuth.loggedIn = true;
+ state.loggedIn = true;
const resolved = await resolvePiNativeProviders({
workingDir: '/tmp/project',
providerId: 'custom:xai',
diff --git a/apps/desktop/src/main/maker-host/__tests__/piRosterAssembly.test.ts b/apps/desktop/src/main/maker-host/__tests__/piRosterAssembly.test.ts
index ff2ef22ef8..23335e08f3 100644
--- a/apps/desktop/src/main/maker-host/__tests__/piRosterAssembly.test.ts
+++ b/apps/desktop/src/main/maker-host/__tests__/piRosterAssembly.test.ts
@@ -30,6 +30,7 @@ vi.mock('../../mcp-integrations/piEnvironment.js', () => ({
}));
vi.mock('../auth-adapters.js', () => ({
+ desktopClaudeAuthAdapter: { ensureSharedGlobalSkills: async () => undefined },
desktopCodexAuthAdapter: {},
readClaudeApiKey: () => 'test-key',
}));
diff --git a/apps/desktop/src/main/maker-host/pi-host.ts b/apps/desktop/src/main/maker-host/pi-host.ts
index 223c1a1757..8edd4bc73d 100644
--- a/apps/desktop/src/main/maker-host/pi-host.ts
+++ b/apps/desktop/src/main/maker-host/pi-host.ts
@@ -55,7 +55,11 @@ import { getReadyBinaryPath } from '../agent-binaries/index.js';
import { getPiExtraSpawnConfig } from '../mcp-integrations/piEnvironment.js';
import { listCustomProvidersWithSecureHeaders } from './custom-provider-header-secrets.js';
import { readCustomProviderKey } from '../secrets/providerSecretStore.js';
-import { desktopCodexAuthAdapter, readClaudeApiKey } from './auth-adapters.js';
+import {
+ desktopClaudeAuthAdapter,
+ desktopCodexAuthAdapter,
+ readClaudeApiKey,
+} from './auth-adapters.js';
import {
getClaudeEndpoint,
isAnthropicCompatProxyHandleReady,
@@ -1287,6 +1291,12 @@ export async function resolvePiNativeProviders(ctx: {
model: string;
resumeSessionId?: string;
}): Promise<PiNativeProvidersResult> {
+ if (!ctx.remoteHostId) {
+ // Pi scans the local ~/.agents/skills root when it starts. This hook is awaited by every
+ // Desktop Pi startSession caller, so refresh Codex-only projections added after app startup
+ // before the process snapshots its global skills. Remote Pi has a different HOME/root.
+ await desktopClaudeAuthAdapter.ensureSharedGlobalSkills();
+ }
const piBinaryPath = resolvePiBinaryPath();
const bundledModels = piBinaryPath ? await readPiBundledModels(piBinaryPath) : null;
let subscriptions: PiNativeProvidersResult = { providers: [], env: {} };
diff --git a/apps/desktop/src/main/maker-host/shared-global-skills.ts b/apps/desktop/src/main/maker-host/shared-global-skills.ts
index 3c329d1055..7ee061da1f 100644
--- a/apps/desktop/src/main/maker-host/shared-global-skills.ts
+++ b/apps/desktop/src/main/maker-host/shared-global-skills.ts
@@ -323,12 +323,12 @@ async function linkEntriesIntoRoot(
}
/**
- * Makes global skills usable from both Claude Code and Codex without moving user data.
+ * Makes global skills usable from Claude Code, Codex, and Pi without moving user data.
*
* Rules:
- * - ~/.agents/skills is the shared index that Cindy Codex already scans.
- * - Existing ~/.claude/skills entries are linked into ~/.agents/skills so Codex can see them.
- * - ~/.agents/skills and ~/.codex/skills entries are linked into ~/.claude/skills so Claude can see them.
+ * - ~/.agents/skills is the shared index that Cindy Codex and Pi scan.
+ * - Existing ~/.claude/skills and ~/.codex/skills entries are linked into ~/.agents/skills.
+ * - ~/.agents/skills and ~/.codex/skills entries are linked into ~/.claude/skills.
* - Existing non-symlink paths are never overwritten.
*/
export async function prepareSharedGlobalSkillLinks(
@@ -404,6 +404,16 @@ export async function prepareSharedGlobalSkillLinks(
warnings.push(...codexToClaude.warnings);
changed = changed || codexToClaude.changed;
+ const codexToShared = await linkEntriesIntoRoot(
+ codexEntries,
+ paths.sharedSkillsDir,
+ false,
+ opts.assertOwnerStable,
+ );
+ actions.push(...codexToShared.actions);
+ warnings.push(...codexToShared.warnings);
+ changed = changed || codexToShared.changed;
+
opts.assertOwnerStable?.();
return {
...paths,
diff --git a/apps/desktop/src/main/maker-ipc/__tests__/sessionTreeProviderResume.test.ts b/apps/desktop/src/main/maker-ipc/__tests__/sessionTreeProviderResume.test.ts
index 0dbe56fa69..f5f3cdb5e5 100644
--- a/apps/desktop/src/main/maker-ipc/__tests__/sessionTreeProviderResume.test.ts
+++ b/apps/desktop/src/main/maker-ipc/__tests__/sessionTreeProviderResume.test.ts
@@ -16,6 +16,31 @@ function sourceBetween(startNeedle: string, endNeedle: string): string {
}
describe('Pi session-tree lazy resume provider route', () => {
+ it('refreshes shared global skills before Pi discovery', () => {
+ const listSkills = sourceBetween(
+ 'MAKER_INVOKE.LIST_AGENT_SKILLS',
+ 'MAKER_INVOKE.SCAN_AT_RESOURCES',
+ );
+ const listCustomizations = sourceBetween(
+ 'MAKER_INVOKE.LIST_CUSTOMIZATIONS',
+ '// ── Session 生命周期',
+ );
+
+ expect(listSkills).toContain('const kind = requireAgentKind(agentKind);');
+ expect(listSkills).toContain('await desktopClaudeAuthAdapter.ensureSharedGlobalSkills();');
+ expect(
+ listSkills.indexOf('await desktopClaudeAuthAdapter.ensureSharedGlobalSkills();'),
+ ).toBeLessThan(listSkills.indexOf('maker.listAgentSkills(kind, skillParams)'));
+
+ expect(listCustomizations).toContain("else if (agentKind === 'pi') {");
+ expect(listCustomizations).toContain(
+ 'await desktopClaudeAuthAdapter.ensureSharedGlobalSkills();',
+ );
+ expect(
+ listCustomizations.indexOf('await desktopClaudeAuthAdapter.ensureSharedGlobalSkills();'),
+ ).toBeLessThan(listCustomizations.indexOf('maker.listCustomizations(opts)'));
+ });
+
it('preserves the persisted providerId null/undefined distinction', () => {
const lazyResume = sourceBetween(
'async function getOrResumeSessionTreeSession',
diff --git a/apps/desktop/src/main/maker-ipc/register.ts b/apps/desktop/src/main/maker-ipc/register.ts
index 14c11e7566..4da2ac984c 100644
--- a/apps/desktop/src/main/maker-ipc/register.ts
+++ b/apps/desktop/src/main/maker-ipc/register.ts
@@ -6044,7 +6044,10 @@ export function registerMakerIpc(maker: Maker, options: RegisterMakerIpcOptions)
}
if (kind === 'codex') {
await desktopCodexAuthAdapter.ensureGlobalCodexAssets();
- } else if (kind === 'claude-code') {
+ } else {
+ // Pi scans ~/.agents/skills directly. Refresh the managed projection here so a
+ // Codex-only skill added after Cindy startup is visible without using another agent
+ // or restarting the app first. Claude keeps the same shared-root refresh semantics.
await desktopClaudeAuthAdapter.ensureSharedGlobalSkills();
}
const result = await maker.listAgentSkills(kind, skillParams);
@@ -6169,6 +6172,8 @@ export function registerMakerIpc(maker: Maker, options: RegisterMakerIpcOptions)
const agentKind = p.agentKind !== undefined ? requireAgentKind(p.agentKind) : undefined;
if (agentKind === undefined || agentKind === 'codex') {
await desktopCodexAuthAdapter.ensureGlobalCodexAssets();
+ } else if (agentKind === 'pi') {
+ await desktopClaudeAuthAdapter.ensureSharedGlobalSkills();
}
const opts = {
...(agentKind !== undefined ? { agentKind } : {}),
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.ts
index c0f696291d..58c44c5bcc 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
@@ -203,6 +203,20 @@ function canonicalPath(value: string): string {
}
}
+function expectedExplicitProjectSkillScope(fixture: Fixture): 'project' | 'temporary' {
+ if (process.platform === 'win32') return 'project';
+ // Pi v0.83.0 reports `project` on macOS when the lexical git root is already
+ // canonical. The default /var -> /private/var temp-dir alias instead yields
+ // `temporary`, so keep the fixture deterministic under either TMPDIR form.
+ if (
+ process.platform === 'darwin'
+ && path.resolve(fixture.repoRoot) === canonicalPath(fixture.repoRoot)
+ ) {
+ return 'project';
+ }
+ return 'temporary';
+}
+
async function createFixture(prefix = 'pi-rpc-fixture-'): Promise<Fixture> {
const root = mkdtempSync(path.join(tmpdir(), prefix));
fixtureRoots.push(root);
@@ -677,6 +691,37 @@ describe.skipIf(!existsSync(PI_BINARY))('Pi v0.83.0 RPC resource discovery facts
}]);
});
+ it('loads a shared global skill from the user .agents root without project approval', async () => {
+ const fixture = await createFixture('pi-rpc-shared-global-');
+ const sharedSkill = path.join(
+ fixture.root,
+ 'home',
+ '.agents',
+ 'skills',
+ 'shared-global-skill',
+ );
+ writeSkill(sharedSkill, 'shared-global-skill');
+
+ const result = await runGetCommands({
+ binaryPath: PI_BINARY,
+ cwd: fixture.workingDir,
+ configHome: fixture.configHome,
+ sessionDir: fixture.sessionDir,
+ approve: false,
+ });
+
+ expect(result.commands).toEqual(expect.arrayContaining([
+ expect.objectContaining({
+ name: 'skill:shared-global-skill',
+ source: 'skill',
+ sourceInfo: expect.objectContaining({
+ scope: 'user',
+ path: path.join(sharedSkill, 'SKILL.md'),
+ }),
+ }),
+ ]));
+ });
+
it('scanner superset differs from unapproved runtime only by project trust', async () => {
const fixture = await createFixture('pi-rpc-scanner-trust-gap-');
const result = await runGetCommands({
@@ -868,9 +913,7 @@ describe.skipIf(!existsSync(PI_BINARY))('Pi v0.83.0 RPC resource discovery facts
sourceInfo: expect.objectContaining({
path: path.join(explicitSkill, 'SKILL.md'),
source: 'local',
- // Pi reports direct `--skill` paths as `project` on Windows and
- // `temporary` on macOS.
- scope: process.platform === 'win32' ? 'project' : 'temporary',
+ scope: expectedExplicitProjectSkillScope(fixture),
}),
}),
]));
@@ -934,9 +977,7 @@ describe.skipIf(!existsSync(PI_BINARY))('Pi v0.83.0 RPC resource discovery facts
baseDir: path.dirname(explicitSkill),
path: explicitSkill,
source: 'local',
- // Pi reports direct `--skill` paths as `project` on Windows and
- // `temporary` on macOS.
- scope: process.platform === 'win32' ? 'project' : 'temporary',
+ scope: expectedExplicitProjectSkillScope(fixture),
}),
}),
]));
@@ -969,9 +1010,7 @@ describe.skipIf(!existsSync(PI_BINARY))('Pi v0.83.0 RPC resource discovery facts
sourceInfo: {
baseDir: first,
source: 'local',
- // The bundled Pi binary uses `project` on Windows and `temporary` on
- // macOS for direct explicit paths.
- scope: process.platform === 'win32' ? 'project' : 'temporary',
+ scope: expectedExplicitProjectSkillScope(fixture),
},
});
});