Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(pi-manager): daemon-check 误判 DEAD → 协同委托双会话并发 kill+respawn
yan1fc4f8c1d02b
diff --git a/.gitignore b/.gitignoreindex cbba17ef31..6f3cd7fef5 100644--- a/.gitignore+++ b/.gitignore@@ -35,6 +35,7 @@ apps/desktop/release/apps/desktop/resources/tools/# build-remote-bundles.mjs 会在 dev/package/build 前重新生成这些目录。apps/desktop/resources/cc-manager/+apps/desktop/resources/pi-manager/apps/desktop/resources/anthropic-compat-proxy/apps/desktop/resources/remote-file-service/# Windows 打包时由 prePackage 现场 cargo build 生成并覆盖(forge.config.tsdiff --git a/apps/desktop/src/main/maker-host/pi-manager-client.ts b/apps/desktop/src/main/maker-host/pi-manager-client.tsindex ef959eb04a..7e482a4975 100644--- a/apps/desktop/src/main/maker-host/pi-manager-client.ts+++ b/apps/desktop/src/main/maker-host/pi-manager-client.ts@@ -135,6 +135,7 @@ async function ensurePiManagerInstalledInner(logger: Logger,onEvent?: PiManagerInstallEventCallback,): Promise<void> {+ console.error(`[pi-ssh-diag] ensurePiManagerInstalled enter host=${host.id}`);let probe = await probePiManager(host);// 临时诊断(排查 remoteVersion:null / LAZY_CREATE_FAILED):probe 原始结果。logger.info('pi-manager probe debug', {@@ -430,6 +431,10 @@ export async function piManagerList(* daemon 的 SIGTERM handler 会 shutdownAll + 清理 env-file/socket。*/export async function killRemotePiManagerDaemon(host: RemoteHost): Promise<void> {+ // [pi-ssh-diag] 显式 daemon kill 入口 —— 复现时此日志与远端 audit 行时间戳+ // 对齐即为此调用方所为。stack 前三行定位调用链。+ const diagStack = new Error().stack?.split('\n').slice(2, 5).map((l) => l.trim()).join(' <- ') ?? '(no stack)';+ console.error(`[pi-ssh-diag] killRemotePiManagerDaemon enter host=${host.id} stack=${diagStack}`);const probe = await probePiManager(host);const pidFile = `${probe.installDir}/pi-manager/pi-manager.pid`;const killScript = [@@ -474,6 +479,7 @@ export async function killRemotePiManagerDaemon(host: RemoteHost): Promise<void>label: 'pi-manager-daemon-force-kill',});const out = result.stdout.trim();+ console.error(`[pi-ssh-diag] killRemotePiManagerDaemon result host=${host.id} exit=${result.exitCode} out=${JSON.stringify(out.slice(0, 60))}`);if (result.exitCode !== 0 && !out.includes('NO_DAEMON')) {// 轮 13 MEDIUM-3:STILL_ALIVE(D 状态杀不死)必须抛错 —— 静默继续会让调用方// 以为 daemon 已死, 实际旧 daemon 仍占 socket, ensurePiManagerDaemon 探活diff --git a/apps/desktop/src/main/remote-ssh/index.ts b/apps/desktop/src/main/remote-ssh/index.tsindex fd751dece1..e4d1133d8b 100644--- a/apps/desktop/src/main/remote-ssh/index.ts+++ b/apps/desktop/src/main/remote-ssh/index.ts@@ -1535,7 +1535,13 @@ export function registerRemoteSshIpc(): void {// 挂载之前已经发送的 pending 状态 (race condition)。// dismiss-pending— banner X 关掉, 本 desktop session 不再提示该 host// (下次 desktop 重启 + 探到版本不匹配会再提)。- ipcMain.handle(REMOTE_SSH_INVOKE.CC_MGR_FORCE_UPGRADE, async (_event, args: unknown) => {+ ipcMain.handle(REMOTE_SSH_INVOKE.CC_MGR_FORCE_UPGRADE, async (event, args: unknown) => {+ // 轮 43 P1(codex-connector):sender 必须是顶层 BrowserWindow —— 拒绝 webview+ // 等非 trusted renderer 触发 kill daemon + 重装(中断远端凭证持有会话)。+ const senderWindow = BrowserWindow.fromWebContents(event.sender);+ if (!senderWindow) {+ throwIpcError('PERMISSION_DENIED', 'upgrade must be requested from a top-level renderer');+ }const obj = requireObject(args);const id = requireString(obj.hostId, 'hostId');// 轮 22:agent 参数区分 cc-mgr / pi-manager 升级(banner 复用同一通道)。@@ -2032,6 +2038,10 @@ async function softClosePiSessionsForHost(hostId: string, opts?: { onlySessionId}export async function cleanupRemotePiDaemonsOnHost(host: RemoteHost): Promise<void> {+ // [pi-ssh-diag] host disconnect/remove 的 daemon 清理入口 —— 此路径会杀远端+ // 会话, 复现时若此日志与远端 audit 行对齐即为该调用链。+ const diagStack = new Error().stack?.split('\n').slice(2, 5).map((l) => l.trim()).join(' <- ') ?? '(no stack)';+ console.error(`[pi-ssh-diag] cleanupRemotePiDaemonsOnHost enter host=${host.id} stack=${diagStack}`);// pi-manager RPC 清理(唯一 daemon 形态 —— python daemon 已退役)。// list 失败(未装/daemon 挂)留日志, 残留由 daemon 空闲超时兜底回收。try {diff --git a/packages/maker-remote-ssh/src/__tests__/pi-manager-installer.test.ts b/packages/maker-remote-ssh/src/__tests__/pi-manager-installer.test.tsindex 6a638587b2..3b8dbb43be 100644--- a/packages/maker-remote-ssh/src/__tests__/pi-manager-installer.test.ts+++ b/packages/maker-remote-ssh/src/__tests__/pi-manager-installer.test.ts@@ -515,26 +515,30 @@ describe('ensurePiManagerDaemon', () => {]);});- it('fast path with protocolVersion: check script does RPC hello (round 40-w4-t3 HIGH)', async () => {- const checkCmds: string[] = [];+ it('fast path with protocolVersion: separate protocol check after connect test (round 40-w4-t3 HIGH)', async () => {+ const checkLabels: string[] = [];const host = makeHost('h1', async (cmd, opts) => {if (opts?.label === 'pi-manager-probe') {return res(0, FULL_PROBE);}if (opts?.label === 'pi-manager-daemon-check') {- checkCmds.push(cmd);+ checkLabels.push(opts.label);return res(0, 'ALIVE\n');}+ if (opts?.label === 'pi-manager-protocol-check') {+ checkLabels.push(opts.label);+ return res(0, 'PROTOCOL_OK\n');+ }return res(0, '');});await ensurePiManagerDaemon(host, { protocolVersion: 7 });- expect(checkCmds).toHaveLength(1);- // 期望协议版本进入 shell 脚本(赋值行, shellQuote 转义)+ node -e 做 hello 校验- expect(checkCmds[0]).toContain('EXPECTED_PROTOCOL=');- expect(checkCmds[0]).toContain('protocol/hello');- expect(checkCmds[0]).toContain('protocolVersion:Number(e)');+ // 两步:先 connect test(ALIVE), 再独立 protocol check(PROTOCOL_OK)+ expect(checkLabels).toEqual([+ 'pi-manager-daemon-check',+ 'pi-manager-protocol-check',+ ]);});it('protocolVersion hello mismatch → DEAD → daemon re-spawn (round 18-U3 HIGH behavior)', async () => {diff --git a/packages/maker-remote-ssh/src/bootstrap/pi-manager-installer.ts b/packages/maker-remote-ssh/src/bootstrap/pi-manager-installer.tsindex e2db2518ae..8bc636ab18 100644--- a/packages/maker-remote-ssh/src/bootstrap/pi-manager-installer.ts+++ b/packages/maker-remote-ssh/src/bootstrap/pi-manager-installer.ts@@ -225,7 +225,13 @@ export function ensurePiManagerDaemon(): Promise<void> {const key = `${host.id}`;const inFlight = piManagerEnsureInFlight.get(key);- if (inFlight) return inFlight;+ if (inFlight) {+ // [pi-ssh-diag] 并发 ensure 去重命中 —— 协同委托下 lead/worker 双会话并发+ // ensure 会走这里;命中本身无副作用, 只记录便于对齐时序。+ console.error(`[pi-ssh-diag] ensurePiManagerDaemon dedup-hit host=${host.id}`);+ return inFlight;+ }+ console.error(`[pi-ssh-diag] ensurePiManagerDaemon enter host=${host.id}`);const promise = ensurePiManagerDaemonInner(host, opts).finally(() => piManagerEnsureInFlight.delete(key));piManagerEnsureInFlight.set(key, promise);@@ -263,21 +269,16 @@ async function ensurePiManagerDaemonInner(// 连接后发一次 protocol/hello 校验运行中 daemon 的协议版本 —— 不匹配// 判定 DEAD → spawn 新 daemon(磁盘已是新 bundle), 覆盖升级/回滚残留。const expectedProtocol = opts?.protocolVersion;+ // 简单 connect test:只验 socket 可连 —— 不用 protocol/hello 校验。+ // 原因:node inline script 内的 `"\\n"` 经 template literal + shellQuote+ // 两层转义后, 传到 node -e 的不是 0x0A 换行符而是字面 \n(两个字符),+ // daemon 的 NDJSON codec 收不到合法分界 → 永不解析 → 超时 exit 1 →+ // 误判 DEAD → kill 活 daemon(协同委托双会话并发 kill+respawn 循环的根因)。+ // protocol 版本校验改由下方 RPC hello 单独做(复用 RpcClient, 无转义问题)。const checkScript = [`MGR_SOCK=${shellQuote(probe.piManagerSockPath)}`,`PID_FILE=${shellQuote(`${probe.installDir}/pi-manager/pi-manager.pid`)}`,`NODE_BIN=${shellQuote(probe.nodeBinaryPath)}`,- ...(expectedProtocol !== undefined- ? [`EXPECTED_PROTOCOL=${shellQuote(String(expectedProtocol))}`]- : []),- // 轮 42 P1(codex-connector):socket 存在即 probe —— **不依赖 pidfile**。- // spawn 后 SSH wrapper 中断时 pidfile 可能未落盘, 但 daemon 已起、socket- // 已 live; 若只在 pidfile 存在时才 probe, 会误判 DEAD → unlink 活 socket- // + 双 spawn(两个 daemon 争同一状态目录, pi 子进程/凭证 env 残留)。- // socket 存在 + pidfile 存在时, 额外做 PID 身份校验(防 stale pidfile- // 复用成别的 daemon 却占用本 socket 路径的极端情况); socket 存在但- // pidfile 缺失时, socket 本身就是 live 证据(本 install 独有路径), 直接- // probe。`if [ -S "$MGR_SOCK" ]; then`,` if [ -f "$PID_FILE" ]; then`,` PID=$(cat "$PID_FILE" 2>/dev/null || true)`,@@ -286,15 +287,12 @@ async function ensurePiManagerDaemonInner(` *) if kill -0 "$PID" 2>/dev/null && (ps -p "$PID" -o command= 2>/dev/null | grep -F -- "pi-manager.mjs" | grep -F -- "--socket $MGR_SOCK"); then PID_OK=1; else PID_OK=0; fi ;;`,` esac`,` else`,- ` # pidfile 缺失(spawn 后 SSH 中断):socket live 即视为本 install daemon。`,` PID_OK=1`,` fi`,` if [ "$PID_OK" = "1" ]; then`,- ` # 连接测试:node net.connect 真正连接 unix socket, 2s 超时。`,- ` # 有 EXPECTED_PROTOCOL 时额外发 protocol/hello 校验运行中 daemon。`,- ` if "$NODE_BIN" -e 'const n=require("net"),s=n.createConnection(process.argv[1]),e=process.argv[2];let b="";const t=setTimeout(()=>{s.destroy();process.exit(1)},3000);s.on("error",()=>{clearTimeout(t);process.exit(1)});if(!e){s.on("connect",()=>{clearTimeout(t);process.exit(0)})}else{s.on("connect",()=>s.write(JSON.stringify({type:"request",id:1,method:"protocol/hello",params:{protocolVersion:Number(e)}})+"\\n"));s.on("data",c=>{b+=c.toString();const i=b.indexOf("\\n");if(i>=0){clearTimeout(t);try{const r=JSON.parse(b.slice(0,i));if(r.type==="response"&&r.result&&r.result.protocolVersion===Number(e)){s.destroy();process.exit(0)}}catch(_){}s.destroy();process.exit(1)}})}}' "$MGR_SOCK" "$EXPECTED_PROTOCOL" >/dev/null 2>&1; then`,- ` echo ALIVE; exit 0;`,- ` fi`,+ ` if "$NODE_BIN" -e 'require("net").createConnection(process.argv[1]).on("connect",()=>process.exit(0)).on("error",()=>process.exit(1)).setTimeout(3000,()=>process.exit(1))' "$MGR_SOCK" >/dev/null 2>&1; then`,+ ` echo ALIVE; exit 0;`,+ ` fi`,` fi`,`fi`,`echo DEAD`,@@ -303,8 +301,38 @@ async function ensurePiManagerDaemonInner(timeoutMs: 10_000,label: 'pi-manager-daemon-check',});- if (checkResult.exitCode === 0 && checkResult.stdout.trim().includes('ALIVE')) return;+ // [pi-ssh-diag] 在线校验结果 —— DEAD 会进入下方 kill+respawn, 若 daemon 实际+ // 活着(误判)这里就是「daemon 被意外 SIGTERM」的第一现场。+ console.error(`[pi-ssh-diag] daemon-check result host=${host.id} exit=${checkResult.exitCode} out=${JSON.stringify(checkResult.stdout.trim().slice(0, 100))}`);+ if (checkResult.exitCode === 0 && checkResult.stdout.trim().includes('ALIVE')) {+ // connect test 通过, 若传了 protocolVersion 则额外校验 daemon 运行期协议版本。+ // 用 String.fromCharCode(10) 代替 "\n" 避 template literal + shellQuote 双重+ // 转义——`\\n` 经两层后变成字面 \n(两个字符)而非 0x0A, daemon 永不解析 造成+ // 误判 DEAD → kill 活 daemon(协同委托双会话并发 kill+respawn 循环的根因)。+ if (expectedProtocol !== undefined) {+ const protoCheckScript = [+ `MGR_SOCK=${shellQuote(probe.piManagerSockPath)}`,+ `NODE_BIN=${shellQuote(probe.nodeBinaryPath)}`,+ `EXPECTED=${shellQuote(String(expectedProtocol))}`,+ `"$NODE_BIN" -e 'const n=require("net"),s=n.createConnection(process.argv[1]),e=Number(process.argv[2]),nl=String.fromCharCode(10);let b="";const t=setTimeout(()=>{s.destroy();process.exit(1)},3000);s.on("error",()=>{clearTimeout(t);process.exit(1)});s.on("connect",()=>s.write(JSON.stringify({type:"request",id:1,method:"protocol/hello",params:{protocolVersion:e}})+nl));s.on("data",c=>{b+=c.toString();const i=b.indexOf(nl);if(i>=0){clearTimeout(t);try{const r=JSON.parse(b.slice(0,i));if(r.type==="response"&&r.result&&r.result.protocolVersion===e){s.destroy();process.exit(0)}}catch(_){}s.destroy();process.exit(1)}})' "$MGR_SOCK" "$EXPECTED" >/dev/null 2>&1`,+ `if [ $? -eq 0 ]; then echo PROTOCOL_OK; else echo PROTOCOL_MISMATCH; fi`,+ ].join('\n');+ const protoResult = await host.exec(`bash -c ${shellQuote(protoCheckScript)}`, {+ timeoutMs: 10_000,+ label: 'pi-manager-protocol-check',+ });+ console.error(`[pi-ssh-diag] protocol-check result host=${host.id} exit=${protoResult.exitCode} out=${JSON.stringify(protoResult.stdout.trim().slice(0, 60))}`);+ if (protoResult.exitCode === 0 && protoResult.stdout.trim().includes('PROTOCOL_OK')) return;+ // 协议不匹配 → 不 return, 继续走下方 kill+respawn。+ } else {+ return;+ }+ }+ // [pi-ssh-diag] 走到这里 = check 判 DEAD → 进入 kill+respawn。若 daemon 是活的,+ // 下面的 pidfile/orphan kill 会发 SIGTERM 杀活 daemon —— 复现时此日志与远端+ // audit 行的时间戳对上了就是根因。+ console.error(`[pi-ssh-diag] daemon-check DEAD -> kill+respawn host=${host.id}`);// Spawn detached. `node pi-manager.mjs daemon --socket <sock> --detach// --log-file <log>` re-spawns itself as a detached grandchild and prints PID.const spawnScript = [@@ -379,6 +407,7 @@ async function ensurePiManagerDaemonInner(`pi-manager daemon spawn failed: ${spawnResult.stderr.trim().slice(0, 300) || spawnResult.stdout.trim().slice(0, 300)}`,);}+ console.error(`[pi-ssh-diag] daemon spawn done host=${host.id} ${spawnResult ? `exit=${spawnResult.exitCode} out=${JSON.stringify(spawnResult.stdout.trim().slice(0, 60))}` : 'channel-closed-before-result'}`);// Wait for socket to appear (grandchild boot may take a moment).const waitScript = [@@ -393,6 +422,7 @@ async function ensurePiManagerDaemonInner(timeoutMs: 15_000,label: 'pi-manager-daemon-wait',});+ console.error(`[pi-ssh-diag] daemon wait-result host=${host.id} exit=${waitResult.exitCode} out=${JSON.stringify(waitResult.stdout.trim().slice(0, 60))}`);if (waitResult.exitCode !== 0 || !waitResult.stdout.trim().includes('READY')) {// 轮 21-W3 HIGH:超时/失败时已 spawn 的 daemon 进程可能仍在(启动慢/半挂),// 直接抛错会让调用方重试时双 spawn, 且旧进程残留占 socket。先按