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): 收紧 startSession 失败清理 + 修 plan-mode resume 静默失联

zqchris5b0bffb40d8a
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
new file mode 100644
index 0000000000..1ff21355cf
--- /dev/null
+++ b/packages/maker-core/src/agents/pi/__tests__/pi-startsession-cleanup.test.ts
@@ -0,0 +1,125 @@
+/**
+ * PiAgent.startSession 失败清理 —— 纯控制流单测(mock PiRpcProcess,不 spawn 真 pi)。
+ *
+ * 契约:MCP 桥身份 ctx 在 preparePiExtraSpawnConfig 阶段注册后,若 startSession 在
+ * 交出 handle 之前失败,必须注销该 ctx(否则 `?session=` 路由永久残留),并关掉可能
+ * 已 spawn 的子进程(否则僵尸 pi 仍持有 MCP 路由)。两条失败路径:
+ * 1. proc 构造(spawn)同步抛 —— 此刻尚无 proc 可关,只注销 ctx。
+ * 2. 启动期 RPC(get_state 等)拒绝 —— 注销 ctx + 关 proc。
+ * 成功路径不得误注销。
+ *
+ * 用 mock 是因为真 pi 无法确定性地触发"构造同步抛 / 启动 RPC 拒绝"(pi 接受不存在的
+ * resume 路径、启动 RPC 也不会即时拒),控制流本身才是被测对象。
+ */
+
+import { mkdtempSync, rmSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import path from 'node:path';
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+
+// hoisted 控制旋钮:vi.mock 工厂被提升到 import 之上,不能闭包引用普通 let。
+const knobs = vi.hoisted(() => ({ ctorThrows: false, getStateRejects: false, closeCount: 0 }));
+
+vi.mock('../rpc-client.js', () => ({
+ PiRpcProcess: class {
+ isClosed = false;
+ constructor(_opts: unknown) {
+ if (knobs.ctorThrows) throw new Error('spawn failed (mock)');
+ }
+ async request(cmd: { type: string }): Promise<{ success: boolean; data?: unknown; error?: string }> {
+ if (cmd.type === 'get_state') {
+ if (knobs.getStateRejects) throw new Error('get_state rejected (mock)');
+ return { success: true, data: { sessionFile: '/mock/session.jsonl', model: { contextWindow: 200000 } } };
+ }
+ // switch_session / set_thinking_level / set_auto_compaction / get_entries 等一律成功。
+ return { success: true, data: { entries: [] } };
+ }
+ send(): void {}
+ async close(): Promise<void> {
+ knobs.closeCount++;
+ this.isClosed = true;
+ }
+ get pid(): number { return 1234; }
+ },
+}));
+
+import { PiAgent } from '../index.js';
+import type { AgentDeps } from '../../base-agent.js';
+import type { Logger } from '../../../interfaces/logger.js';
+
+const noopLogger: Logger = {
+ trace: () => {}, debug: () => {}, info: () => {}, warn: () => {}, error: () => {}, fatal: () => {},
+ child: () => noopLogger,
+};
+
+describe('PiAgent.startSession failure cleanup (mocked pi process)', () => {
+ let agentHome = '';
+ let cwd = '';
+ let disposed = 0;
+
+ beforeEach(() => {
+ knobs.ctorThrows = false;
+ knobs.getStateRejects = false;
+ knobs.closeCount = 0;
+ disposed = 0;
+ agentHome = mkdtempSync(path.join(tmpdir(), 'pi-cleanup-home-'));
+ cwd = mkdtempSync(path.join(tmpdir(), 'pi-cleanup-cwd-'));
+ });
+
+ afterEach(() => {
+ rmSync(agentHome, { recursive: true, force: true });
+ rmSync(cwd, { recursive: true, force: true });
+ });
+
+ function buildDeps(): AgentDeps {
+ return {
+ auth: {
+ getState: async () => ({ authenticated: true, identity: 'test', authSource: 'api-key' as const }),
+ triggerLogin: async () => ({ authenticated: true }),
+ logout: async () => {},
+ getAuthEnv: async () => ({}),
+ },
+ runtimeConfig: { endpoint: 'http://127.0.0.1:9' },
+ // 不存在的路径即可:BaseAgent 只校验非空;plan-mode 扩展 stat 落空 → 跳过 get_entries。
+ binaryPath: path.join(agentHome, 'pi'),
+ logger: noopLogger,
+ capabilityAdditions: {
+ availableModels: [
+ { id: 'm', displayName: 'M', contextWindow: 200_000, efforts: [], defaultEffort: null },
+ ],
+ },
+ resolvePiAgentHome: () => agentHome,
+ // 注册身份并回传 disposeSessionCtx 探针(servers 空,无需真桥)。
+ preparePiExtraSpawnConfig: async () => ({
+ mcpBridge: { token: 'itest', servers: [] },
+ disposeSessionCtx: () => { disposed++; },
+ }),
+ };
+ }
+
+ const opts = () => ({ sessionId: 's1', workingDir: cwd, model: 'm' });
+
+ it('disposes ctx (and does not close a nonexistent proc) when the process constructor throws synchronously', async () => {
+ knobs.ctorThrows = true;
+ const agent = new PiAgent(buildDeps());
+ await expect(agent.startSession(opts())).rejects.toThrow(/spawn failed/);
+ expect(disposed).toBe(1);
+ expect(knobs.closeCount).toBe(0); // 构造失败没有 proc 可关
+ });
+
+ it('disposes ctx and closes the proc when a startup RPC rejects before handoff', async () => {
+ knobs.getStateRejects = true;
+ const agent = new PiAgent(buildDeps());
+ await expect(agent.startSession(opts())).rejects.toThrow(/get_state rejected/);
+ expect(disposed).toBe(1);
+ expect(knobs.closeCount).toBe(1); // 已 spawn → 必须关掉,避免僵尸持有 ?session= 路由
+ });
+
+ it('does not dispose ctx on the success path (dispose is deferred to close())', async () => {
+ const agent = new PiAgent(buildDeps());
+ const handle = await agent.startSession(opts());
+ expect(disposed).toBe(0);
+ await handle.close();
+ expect(disposed).toBe(1); // close() 才注销
+ });
+});
diff --git a/packages/maker-core/src/agents/pi/index.ts b/packages/maker-core/src/agents/pi/index.ts
index 58194579f3..a13b1f4ccc 100644
--- a/packages/maker-core/src/agents/pi/index.ts
+++ b/packages/maker-core/src/agents/pi/index.ts
@@ -336,39 +336,52 @@ export class PiAgent extends BaseAgent {
// 翻转,故镜像不漂移)。
let planModeActive = false;
- const proc = new PiRpcProcess({
- binaryPath: this.deps.binaryPath,
- args,
- cwd: opts.workingDir,
- env: {
- ...process.env,
- ...authEnv,
- PI_CODING_AGENT_DIR: agentHome,
- CINDY_PI_PERMISSION_FILE: permissionFile,
- ...(mcpBridge && mcpBridge.servers.length > 0
- ? { CINDY_PI_MCP_BRIDGE: JSON.stringify(mcpBridge) }
- : {}),
- },
- logger: this.deps.logger,
- onEvent: (event: PiRpcEvent) => {
- if (event.type === 'extension_ui_request') {
- this.handleExtensionUiRequest(event, proc, () => interactionResolver);
- return;
- }
- translatePiEvent(event, queue, ctx);
- },
- onExit: ({ code, signal }) => {
- if (!closed) {
- // 非用户 close 的进程死亡:terminal error + 收尾,避免 UI 永久 running。
- queue.push({
- type: 'error',
- data: { message: `pi process exited unexpectedly (code=${code}, signal=${signal})`, isTerminal: true },
- source: 'pi',
- });
- }
- queue.end();
- },
- });
+ // proc 构造即 spawn 子进程 —— spawn 参数非法等会**同步**抛。此刻 ctx 已在
+ // preparePiExtraSpawnConfig 注册、但 handle 尚未交出,close() 不会跑 → 单独
+ // 兜底注销 ctx 再抛(构造失败没有 proc 可关)。catch 必抛,故其后 proc 恒已赋值。
+ let proc: PiRpcProcess;
+ try {
+ proc = new PiRpcProcess({
+ binaryPath: this.deps.binaryPath,
+ args,
+ cwd: opts.workingDir,
+ env: {
+ ...process.env,
+ ...authEnv,
+ PI_CODING_AGENT_DIR: agentHome,
+ CINDY_PI_PERMISSION_FILE: permissionFile,
+ ...(mcpBridge && mcpBridge.servers.length > 0
+ ? { CINDY_PI_MCP_BRIDGE: JSON.stringify(mcpBridge) }
+ : {}),
+ },
+ logger: this.deps.logger,
+ onEvent: (event: PiRpcEvent) => {
+ if (event.type === 'extension_ui_request') {
+ this.handleExtensionUiRequest(event, proc, () => interactionResolver);
+ return;
+ }
+ translatePiEvent(event, queue, ctx);
+ },
+ onExit: ({ code, signal }) => {
+ if (!closed) {
+ // 非用户 close 的进程死亡:terminal error + 收尾,避免 UI 永久 running。
+ queue.push({
+ type: 'error',
+ data: { message: `pi process exited unexpectedly (code=${code}, signal=${signal})`, isTerminal: true },
+ source: 'pi',
+ });
+ }
+ queue.end();
+ },
+ });
+ } catch (err) {
+ try {
+ disposeSessionCtx?.();
+ } catch {
+ /* best-effort:注销失败不掩盖原始构造错误 */
+ }
+ throw err;
+ }
// startSession 在把 handle 交给调用方之前若失败(resume 硬失败、启动期 RPC
// 超时/进程夭折等),close() 永远不会被调用。这里 try/catch 兜底:注销 bridge
@@ -431,14 +444,24 @@ export class PiAgent extends BaseAgent {
if (planModeExtAvailable) {
try {
const entriesResp = await proc.request({ type: 'get_entries' });
- const entries =
- (entriesResp.data as { entries?: Array<{ customType?: string; data?: { enabled?: boolean } }> } | undefined)
- ?.entries ?? [];
- for (let i = entries.length - 1; i >= 0; i--) {
- if (entries[i]?.customType === 'plan-mode') {
- const enabled = entries[i]?.data?.enabled;
- if (typeof enabled === 'boolean') planModeActive = enabled;
- break;
+ // request() 对业务失败是 resolve({success:false}) 而非 reject。不查 success
+ // 就会静默落 entries=[]、镜像默认 false —— 若 pi 实际 planModeEnabled=true,
+ // 后续 setPlanMode 的幂等短路会误判、/plan toggle 方向反转。显式查并 warn;
+ // 镜像保持默认 false(宁可不声称保护,也不谎报),与兄弟 RPC 调用的 success 检查一致。
+ if (!entriesResp.success) {
+ this.deps.logger.warn('pi plan-mode state sync: get_entries failed; plan mirror unverified (defaulting off)', {
+ error: entriesResp.error,
+ });
+ } else {
+ const entries =
+ (entriesResp.data as { entries?: Array<{ customType?: string; data?: { enabled?: boolean } }> } | undefined)
+ ?.entries ?? [];
+ for (let i = entries.length - 1; i >= 0; i--) {
+ if (entries[i]?.customType === 'plan-mode') {
+ const enabled = entries[i]?.data?.enabled;
+ if (typeof enabled === 'boolean') planModeActive = enabled;
+ break;
+ }
}
}
} catch (err) {