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): preserve skill identities across settings rewrites

xd-bobo079519b45eb9
diff --git a/docs/dev-rules/pi-harness.md b/docs/dev-rules/pi-harness.md
index 92c16ed6ab..2b8eb9da19 100644
--- a/docs/dev-rules/pi-harness.md
+++ b/docs/dev-rules/pi-harness.md
@@ -82,6 +82,9 @@ Skill 停用适配同时保存物理身份与管理页已扫描的词法发现
仍保留物理路径,并尽力解析发现目录中的符号链接别名;该额外扫描
共享 2048 个条目、16 层深度和 100ms 的遍历预算,先检查各发现根的直接入口,再逐层
进入子目录,避免无关子树抢先耗尽预算;目录流逐项读取并在退出时关闭。
+启动时将额外发现的别名绑定到冻结的物理身份。会话内模型目录刷新、模型切换及上下文
+窗口重新校准都传递这份映射和原生包配置;写 settings 前剔除改指向路径,不从旧 JSON
+中的负路径重新推导物理身份。
预算耗尽后保留已解析路径,不继续扫描;原生 Pi 仍负责资源加载,不能因 Cindy 的扫描
截断而拒绝加载其它资源。时间预算在文件系统调用之间检查,不是对单次系统调用的超时。
diff --git a/packages/maker-core/src/agents/pi/__tests__/pi-native-auto-compact.test.ts b/packages/maker-core/src/agents/pi/__tests__/pi-native-auto-compact.test.ts
index 485e9a5769..99e6712e77 100644
--- a/packages/maker-core/src/agents/pi/__tests__/pi-native-auto-compact.test.ts
+++ b/packages/maker-core/src/agents/pi/__tests__/pi-native-auto-compact.test.ts
@@ -3,7 +3,7 @@
* events and only latches deterministic failures for the next-send rollover.
*/
-import { mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
+import { mkdtempSync, mkdirSync, renameSync, symlinkSync, unlinkSync, readdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import path from "node:path";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
@@ -394,7 +394,7 @@ describe("PiAgent native auto-compaction ownership", () => {
},
);
- function readLatestPiSettings(): { compaction?: { reserveTokens?: number } } {
+ function readLatestPiSettings(): { compaction?: { reserveTokens?: number }; skills?: string[]; packages?: Array<{ source: string; skills?: string[] }> } {
const files: string[] = [];
const walk = (dir: string): void => {
for (const entry of readdirSync(dir, { withFileTypes: true })) {
@@ -585,6 +585,51 @@ describe("PiAgent native auto-compaction ownership", () => {
await handle.close();
});
+ it.each(["unchanged", "alias", "physical", "startup"])("keeps frozen Skill exclusions across both settings rewrites (%s)", async (change) => {
+ const a = path.join(cwd, "a");
+ const b = path.join(cwd, "b");
+ const alias = path.join(cwd, "alias");
+ mkdirSync(a);
+ mkdirSync(b);
+ writeFileSync(path.join(a, "SKILL.md"), "fixture a");
+ writeFileSync(path.join(b, "SKILL.md"), "fixture b");
+ symlinkSync(a, alias, process.platform === "win32" ? "junction" : "dir");
+ const deps = buildDeps();
+ deps.getDisabledSkillPaths = () => [a];
+ deps.resolvePiNativePackagePaths = async () => [{ source: cwd, skills: ["**/*"] }];
+ deps.resolvePiManagedPackageResources = async () => {
+ if (change === "startup") {
+ renameSync(a, `${a}-moved`);
+ symlinkSync(b, a, process.platform === "win32" ? "junction" : "dir");
+ }
+ return { extensions: [], skills: [{ name: "alias", path: alias }], promptTemplates: [], packageRoots: [cwd] };
+ };
+ const handle = await new PiAgent(deps).startSession({ sessionId: "frozen-skills", workingDir: cwd, model: "m" });
+ try {
+ if (change === "startup") expect(readLatestPiSettings().skills ?? []).not.toContain(`-${a}`);
+ else expect(readLatestPiSettings().skills).toContain(`-${alias}`);
+ if (change === "alias") {
+ unlinkSync(alias);
+ symlinkSync(b, alias, process.platform === "win32" ? "junction" : "dir");
+ } else if (change === "physical") {
+ renameSync(a, `${a}-moved`);
+ symlinkSync(b, a, process.platform === "win32" ? "junction" : "dir");
+ }
+ knobs.targetRuntimeContextWindow = 1_000_000;
+ knobs.setModelReportsContextWindow = false;
+ knobs.rpcCalls = [];
+ await handle.setModel!("n");
+ const rewritten = readLatestPiSettings();
+ expect(knobs.rpcCalls.filter((call) => call.type === "switch_session")).toHaveLength(2);
+ expect(rewritten.packages?.[0]?.source).toBe(cwd);
+ expect(rewritten.skills ?? []).not.toContain(`-${b}`);
+ expect(rewritten.packages?.[0]?.skills ?? []).not.toContain("-b");
+ if (change === "unchanged") expect(rewritten.skills).toContain(`-${alias}`);
+ else expect(rewritten.skills ?? []).not.toContain(`-${alias}`);
+ if (change === "physical" || change === "startup") expect(rewritten.skills ?? []).not.toContain(`-${a}`);
+ } finally { await handle.close(); }
+ });
+
it("terminates the session when compaction settings reload fails after a window change", async () => {
const handle = await start();
knobs.switchSessionSuccess = false;
diff --git a/packages/maker-core/src/agents/pi/index.ts b/packages/maker-core/src/agents/pi/index.ts
index 26332bcffa..9d4df43ee8 100644
--- a/packages/maker-core/src/agents/pi/index.ts
+++ b/packages/maker-core/src/agents/pi/index.ts
@@ -1,4 +1,4 @@
-import { snapshotDisabledSkillLaunch, currentDisabledSkillLaunchPaths } from '../shared/skill-activation.js';
+import { snapshotDisabledSkillLaunch, currentDisabledSkillLaunchPaths, extendDisabledSkillLaunchPaths, type DisabledSkillLaunchSnapshot } from '../shared/skill-activation.js';
/**
* PiAgent —— pi coding agent(earendil-works/pi)接入。
*
@@ -1655,6 +1655,7 @@ export class PiAgent extends BaseAgent {
contextWindow?: number;
piCompactionPct?: number;
packages?: readonly PiNativePackageEntry[];
+ disabledSkills?: DisabledSkillLaunchSnapshot;
},
): Promise<string> {
const built = this.buildCurrentPiSettingsJson(
@@ -1678,17 +1679,11 @@ export class PiAgent extends BaseAgent {
: readOrNull(stableUserSettingsPath),
]);
const merged = mergePiUserSettingsPassthrough(built, sessionContent, stableContent);
- // Preserve the session-frozen native Skill exclusions across model/settings rewrites.
- // Never import controller/global Skill configuration into a remote runtime.
- if (sessionContent) {
- try {
- const previous = JSON.parse(sessionContent) as { skills?: unknown };
- if (Array.isArray(previous.skills)) {
- return JSON.stringify(applyPiDisabledSkillSettings(JSON.parse(merged),
- previous.skills.filter((item): item is string => typeof item === 'string' && item.startsWith('-')).map((item) => item.slice(1)),
- ), null, 2) + '\n';
- }
- } catch { /* Malformed prior runtime settings do not replace the rebuilt settings. */ }
+ // Runtime rewrites use the startup bindings, never paths reconstructed from
+ // settings.json: those paths can now point to a different physical Skill.
+ if (opts.disabledSkills) {
+ return JSON.stringify(applyPiDisabledSkillSettings(JSON.parse(merged),
+ currentDisabledSkillLaunchPaths(opts.disabledSkills)), null, 2) + '\n';
}
return merged;
}
@@ -1700,6 +1695,7 @@ export class PiAgent extends BaseAgent {
contextWindow?: number;
piCompactionPct?: number;
packages?: readonly PiNativePackageEntry[];
+ disabledSkills?: DisabledSkillLaunchSnapshot;
} = {},
): Promise<void> {
const settingsJsonPath = joinRemotePosixPath(agentHome, 'settings.json');
@@ -1737,6 +1733,7 @@ export class PiAgent extends BaseAgent {
piCompactionPct?: number;
/** Host-installed roots/specs for Pi's own package discovery. */
packages?: readonly PiNativePackageEntry[];
+ disabledSkills?: DisabledSkillLaunchSnapshot;
} = {},
): Promise<{
gatewayImageInputByModel: Map<string, boolean>;
@@ -3048,7 +3045,7 @@ export class PiAgent extends BaseAgent {
// approval from permission mode, MCP/plugin state, or caller vendor options.
const disabledSkillPaths = opts.remoteHostId || opts.botRuntimeProfile || reviewMode
? [] : [...(this.deps.getDisabledSkillPaths?.() ?? [])];
- const disabledSkillLaunch = snapshotDisabledSkillLaunch(disabledSkillPaths);
+ let disabledSkillLaunch = snapshotDisabledSkillLaunch(disabledSkillPaths);
const disabledSkillSnapshot = disabledSkillLaunch.identities;
let projectResourceAssembly = unavailablePiProjectResourceAssembly(
reviewMode ? 'review-mode-project-resources-disabled' : 'approval-resolver-unavailable',
@@ -3143,10 +3140,13 @@ export class PiAgent extends BaseAgent {
if (disabledSkillPaths.length > 0) {
const settingsPath = path.join(configHome, 'settings.json');
const settings = JSON.parse(await fs.readFile(settingsPath, 'utf8'));
- await fs.writeFile(settingsPath, JSON.stringify(applyPiDisabledSkillSettings(settings, piDisabledDiscoveryPaths(currentDisabledSkillLaunchPaths(disabledSkillLaunch), [
- path.join(configHome, 'skills'), path.join(os.homedir(), '.agents', 'skills'),
- ...managedPackageResources.skills.map((skill) => skill.path),
- ])), null, 2) + '\n', { mode: 0o600 });
+ disabledSkillLaunch = extendDisabledSkillLaunchPaths(disabledSkillLaunch,
+ piDisabledDiscoveryPaths(currentDisabledSkillLaunchPaths(disabledSkillLaunch), [
+ path.join(configHome, 'skills'), path.join(os.homedir(), '.agents', 'skills'),
+ ...managedPackageResources.skills.map((skill) => skill.path),
+ ]));
+ await fs.writeFile(settingsPath, JSON.stringify(applyPiDisabledSkillSettings(settings,
+ currentDisabledSkillLaunchPaths(disabledSkillLaunch)), null, 2) + '\n', { mode: 0o600 });
}
const nativePackageRoots = nativePackagePaths.map((entry) => (
@@ -5279,6 +5279,7 @@ export class PiAgent extends BaseAgent {
contextWindow: ctx.contextWindow || startupContextWindow,
piCompactionPct: sessionPiAutoCompactPct,
packages: nativePackagePaths,
+ disabledSkills: disabledSkillLaunch,
},
);
nativeProviders = previousProviders;
@@ -5357,6 +5358,7 @@ export class PiAgent extends BaseAgent {
contextWindow: ctx.contextWindow || startupContextWindow,
piCompactionPct: sessionPiAutoCompactPct,
packages: nativePackagePaths,
+ disabledSkills: disabledSkillLaunch,
},
);
gatewayApiByModel.clear();
@@ -5687,6 +5689,7 @@ export class PiAgent extends BaseAgent {
contextWindow: nextWindow,
piCompactionPct: sessionPiAutoCompactPct,
packages: nativePackagePaths,
+ disabledSkills: disabledSkillLaunch,
});
if (!sdkSessionId) {
throw new Error('pi: missing session path after model switch; cannot reload compaction settings');
@@ -5741,6 +5744,8 @@ export class PiAgent extends BaseAgent {
fileOps,
contextWindow: verifiedWindow,
piCompactionPct: sessionPiAutoCompactPct,
+ packages: nativePackagePaths,
+ disabledSkills: disabledSkillLaunch,
});
const recalibrated = await proc.request({ type: 'switch_session', sessionPath: sdkSessionId });
if (!recalibrated.success) {
diff --git a/packages/maker-core/src/agents/shared/skill-activation.ts b/packages/maker-core/src/agents/shared/skill-activation.ts
index 5f85c726aa..964237c880 100644
--- a/packages/maker-core/src/agents/shared/skill-activation.ts
+++ b/packages/maker-core/src/agents/shared/skill-activation.ts
@@ -27,6 +27,21 @@ export function snapshotDisabledSkillLaunch(paths: readonly string[]): DisabledS
.map((source) => Object.freeze({ path: source, identity: canonicalSkillPath(source) }))) });
}
+/** Add discovery aliases without changing already-frozen source bindings. */
+export function extendDisabledSkillLaunchPaths(
+ snapshot: DisabledSkillLaunchSnapshot,
+ discovered: readonly string[],
+): DisabledSkillLaunchSnapshot {
+ const paths = new Map(snapshot.paths.map((entry) => [entry.path, entry]));
+ for (const source of discovered) {
+ const identity = canonicalSkillPath(source);
+ if (!paths.has(source) && snapshot.identities.includes(identity)) {
+ paths.set(source, Object.freeze({ path: source, identity }));
+ }
+ }
+ return Object.freeze({ identities: snapshot.identities, paths: Object.freeze([...paths.values()]) });
+}
+
/** Drop stale/retargeted lexical paths immediately before native configuration. */
export function currentDisabledSkillLaunchPaths(snapshot: DisabledSkillLaunchSnapshot): string[] {
return snapshot.paths.filter((entry) => canonicalSkillPath(entry.path) === entry.identity)