Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix: address review — retry PI process termination
Chris844e41c9be27
diff --git a/packages/maker-core/src/agents/pi/__tests__/rpc-client.test.ts b/packages/maker-core/src/agents/pi/__tests__/rpc-client.test.tsindex 61713fda24..d845d19e7a 100644--- a/packages/maker-core/src/agents/pi/__tests__/rpc-client.test.ts+++ b/packages/maker-core/src/agents/pi/__tests__/rpc-client.test.ts@@ -211,6 +211,20 @@ describe('PiRpcProcess close semantics (bridge-disconnect vs explicit close)', (expect(killRemoteSession).toHaveBeenCalledTimes(1);});+ it('retries transport shutdown after an unconfirmed local close', async () => {+ const { transport } = makeFakeTransport();+ const transportClose = vi.mocked(transport.close);+ transportClose+ .mockRejectedValueOnce(new Error('pi process did not confirm exit after SIGKILL'))+ .mockResolvedValueOnce(undefined);+ const { proc } = makeProc({ transport });++ await expect(proc.close()).rejects.toThrow(/did not confirm exit after SIGKILL/);+ await expect(proc.close()).resolves.toBeUndefined();++ expect(transportClose).toHaveBeenCalledTimes(2);+ });+it('retries remote termination after a failed close instead of reporting false success', async () => {const killRemoteSession = vi.fn()diff --git a/packages/maker-core/src/agents/pi/__tests__/transport.test.ts b/packages/maker-core/src/agents/pi/__tests__/transport.test.tsindex cc8e71383c..9a4a126ed0 100644--- a/packages/maker-core/src/agents/pi/__tests__/transport.test.ts+++ b/packages/maker-core/src/agents/pi/__tests__/transport.test.ts@@ -108,32 +108,51 @@ describe('createPiStdioTransport', () => {}});- it('rejects without hanging when SIGKILL exit cannot be confirmed', async () => {+ it('retries a full termination attempt after SIGKILL exit is unconfirmed', async () => {vi.useFakeTimers();try {const { transport, child } = makeTransport();- // child 不响应任何信号:不 emit close, kill 是 no-op。child.kill.mockImplementation(() => true);const onClose = vi.fn();transport.onClose(onClose);- const closePromise = transport.close();- const rejected = expect(closePromise).rejects.toThrow(/did not confirm exit after SIGKILL/);- // SIGTERM 宽限期(3s)→ SIGKILL- await vi.advanceTimersByTimeAsync(3_000);- expect(child.kill).toHaveBeenCalledWith('SIGKILL');- // SIGKILL 确认窗口(5s)超时 → close() 必须收口但不能谎报成功。- await vi.advanceTimersByTimeAsync(5_000);- await rejected;+ const firstClose = transport.close();+ const firstRejected = expect(firstClose).rejects.toThrow(+ /did not confirm exit after SIGKILL/,+ );+ await vi.advanceTimersByTimeAsync(8_000);+ await firstRejected;+ expect(child.kill.mock.calls).toEqual([['SIGTERM'], ['SIGKILL']]);expect(onClose).not.toHaveBeenCalled();expect(transport.isClosed()).toBe(true);+ await expect(transport.writeLine('{"x":1}')).rejects.toThrow(/closed/);- // 未看到真实 exit 前,重复 close 继续 fail closed。- await expect(transport.close()).rejects.toThrow(/did not confirm exit after SIGKILL/);- // 若进程随后终于退出,确认状态解除,后续幂等 close 才可成功。+ // A new owner retry shares one in-flight attempt and sends fresh signals.+ const retry = transport.close();+ const concurrentRetry = transport.close();+ expect(concurrentRetry).toBe(retry);+ expect(child.kill.mock.calls).toEqual([+ ['SIGTERM'],+ ['SIGKILL'],+ ['SIGTERM'],+ ]);+ await vi.advanceTimersByTimeAsync(3_000);+ expect(child.kill.mock.calls).toEqual([+ ['SIGTERM'],+ ['SIGKILL'],+ ['SIGTERM'],+ ['SIGKILL'],+ ]);++ // A real close may arrive late from either kill attempt. It is the sole+ // success signal and settles every waiter exactly once.child.emit('close', null, 'SIGKILL');+ await expect(retry).resolves.toBeUndefined();+ await expect(concurrentRetry).resolves.toBeUndefined();expect(onClose).toHaveBeenCalledTimes(1);+await expect(transport.close()).resolves.toBeUndefined();+ expect(child.kill).toHaveBeenCalledTimes(4);} finally {vi.useRealTimers();}diff --git a/packages/maker-core/src/agents/pi/transport.ts b/packages/maker-core/src/agents/pi/transport.tsindex cc90d7da2e..92e5b78884 100644--- a/packages/maker-core/src/agents/pi/transport.ts+++ b/packages/maker-core/src/agents/pi/transport.ts@@ -113,11 +113,11 @@ export function createPiStdioTransport(opts: PiStdioTransportOptions): PiTranspoconst logger = opts.logger;let closed = false;- // 轮 40-w1 MEDIUM-2:close() 进行中标志 —— 防并发 close 双 SIGTERM(轮 21- // 幂等), 与 closed(已通知)分离, 保主动 close 仍触发 onClose。+ // First close permanently fences writes. Individual termination attempts are+ // shared while in flight, then cleared after failure so a later owner can+ // run a fresh SIGTERM -> SIGKILL sequence.let closing = false;- /** SIGKILL 后未确认退出时,后续 close 继续失败,直到 child 真正发出 close。 */- let closeFailure: Error | null = null;+ let closeAttempt: Promise<void> | null = null;let disposeProcessRegistration: (() => void) | undefined;if (child.pid != null && child.pid > 0) {try {@@ -185,7 +185,6 @@ export function createPiStdioTransport(opts: PiStdioTransportOptions): PiTranspofireClose({ code: null, signal: null, reason: `pi process error: ${err.message}` });});child.on('close', (code, signal) => {- closeFailure = null;fireClose({ code, signal, reason: `pi process exited (code=${code}, signal=${signal})` });});@@ -193,6 +192,55 @@ export function createPiStdioTransport(opts: PiStdioTransportOptions): PiTranspo// SIGKILL 后确认退出的窗口(轮 40-w4-t3 HIGH —— 对齐 session-registry)。const KILL_CONFIRM_MS = 5_000;+ const runCloseAttempt = async (reason: string): Promise<void> => {+ // Every fresh attempt gets its own timers and close listener. A previous+ // unconfirmed SIGKILL does not prove the process exited, so retries must+ // send both signals again instead of replaying a cached error.+ let survived = false;+ await new Promise<void>((resolve) => {+ let done = false;+ let confirmTimer: NodeJS.Timeout | undefined;+ const finish = (): void => {+ if (done) return;+ done = true;+ clearTimeout(killTimer);+ if (confirmTimer) clearTimeout(confirmTimer);+ child.removeListener('close', onClose);+ resolve();+ };+ const onClose = (): void => finish();+ const killTimer = setTimeout(() => {+ try { child.kill('SIGKILL'); } catch { /* already gone */ }+ confirmTimer = setTimeout(() => {+ survived = true;+ logger.error('pi process did not confirm exit after SIGKILL', {+ pid: child.pid,+ });+ finish();+ }, KILL_CONFIRM_MS);+ confirmTimer.unref?.();+ }, KILL_GRACE_MS);+ killTimer.unref?.();+ child.once('close', onClose);+ try {+ child.kill('SIGTERM');+ } catch {+ finish();+ }+ });++ if (survived) {+ // Do not fire onClose: its contract is confirmed process termination.+ // A late real close remains authoritative through the global listener.+ throw new Error('pi process did not confirm exit after SIGKILL');+ }+ fireClose({+ code: null,+ signal: null,+ reason,+ });+ };+return {writeLine(line: string): Promise<void> {if (closed || closing) return Promise.reject(new Error('pi transport already closed'));@@ -218,69 +266,25 @@ export function createPiStdioTransport(opts: PiStdioTransportOptions): PiTransporeturn () => { closeHandlers.delete(handler); };},- async close(reason = 'pi transport close()'): Promise<void> {- if (closeFailure) throw closeFailure;- // 轮 21 H-1 幂等竞态:closed 必须在任何 await 前置位 —— 否则两个并发- // close() 都通过守卫、各发一次 SIGTERM + 各设一个 SIGKILL timer。- // 轮 40-w1 MEDIUM-2:前置 closed 会吞掉子进程 close 事件的 fireClose,- // 主动 close 不再通知 onClose(上层 PiRpcProcess 收不到 pending reject /- // onExit 收口)。拆两个状态:closing 防并发, closed 只在真正通知时置位。- if (closed || closing) return;+ close(reason = 'pi transport close()'): Promise<void> {+ if (closed) return Promise.resolve();+ if (closeAttempt) return closeAttempt;++ // Keep the write fence after a failed attempt: the process may still be+ // alive, but it must never resume protocol traffic while owners retry+ // termination through Session/PiAgent/Maker cleanup paths.closing = true;- // 优雅关闭:SIGTERM → 宽限期 → SIGKILL → 确认退出。幂等。- // 轮 40-w4-t3 HIGH:旧实现 SIGKILL 后无确认超时 —— D-state/平台异常下- // close 事件不来, await close() 永久挂住, 上层取消流程无法收口。这里- // 对齐 session-registry 的 waitForExit 语义:确认窗口到点就结束等待,但- // reject 明确表示“未确认终止”,让 Session 保持 error 并阻止新 handle 并存。- let survived = false;- await new Promise<void>((resolve) => {- let done = false;- let confirmTimer: NodeJS.Timeout | undefined;- const finish = (): void => {- if (done) return;- done = true;- clearTimeout(killTimer);- if (confirmTimer) clearTimeout(confirmTimer);- child.removeListener('close', onClose);- resolve();- };- const onClose = (): void => finish();- const killTimer = setTimeout(() => {- try { child.kill('SIGKILL'); } catch { /* already gone */ }- // SIGKILL 后再给确认窗口;超时结束等待,随后以稳定错误 reject。- confirmTimer = setTimeout(() => {- survived = true;- logger.error('pi process did not confirm exit after SIGKILL', {- pid: child.pid,- });- finish();- }, KILL_CONFIRM_MS);- confirmTimer.unref?.();- }, KILL_GRACE_MS);- killTimer.unref?.();- child.once('close', onClose);- try {- child.kill('SIGTERM');- } catch {- finish();- }- });- // 子进程 close 事件若已 fireClose 则 closed=true(通知已发);否则这里补- // 发一次(主动 close 语义:onClose 必须收到终止通知)。- // 未确认退出只 reject,不能发 onClose(它的语义是进程已终止,PiAgent- // 收到后会清 runtime 文件)。child 以后真的 close 时再由全局 listener 通知。- const terminationError = survived- ? new Error('pi process did not confirm exit after SIGKILL')- : null;- if (terminationError) {- closeFailure = terminationError;- throw terminationError;- }- fireClose({- code: null,- signal: null,- reason,- });+ const attempt = runCloseAttempt(reason);+ closeAttempt = attempt;+ void attempt.then(+ () => {+ if (closeAttempt === attempt) closeAttempt = null;+ },+ () => {+ if (closeAttempt === attempt) closeAttempt = null;+ },+ );+ return attempt;},get pid(): number | undefined {