The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): a hand-off whose bridge could not be withdrawn quits instead of restarting the backend
Tekniumb08a617bfa5c
diff --git a/apps/desktop/electron/updater/checkout-handoff.test.ts b/apps/desktop/electron/updater/checkout-handoff.test.tsindex 92a31236b34..ff00bb35a3b 100644--- a/apps/desktop/electron/updater/checkout-handoff.test.ts+++ b/apps/desktop/electron/updater/checkout-handoff.test.ts@@ -372,18 +372,46 @@ describe.skipIf(IS_WINDOWS)('protocol 2 hand-off', () => {}})- it('a timeout withdraws the bridge through the script helper (never by unlinking it)', async (): Promise<void> => {+ // Review 5411223284 regression 1: a bridge the helper could not withdraw+ // stays adoptable by a late (setsid, unkillable) daemon. Reporting "did not+ // start" and restarting the backend would run `hermes update` beside it.+ it.each(['busy', 'error'])(+ 'a withdraw that stays `%s` keeps the bridge and quits instead of restarting the backend',+ async (answer: string): Promise<void> => {+ const { root, deps } = handoffFixture(false, fakeHelperScript())+ fs.writeFileSync(path.join(deps.hermesHome, 'helper-verdict'), answer === 'error' ? 'garbage' : answer)+ const runs = mockProtocol2Spawn(deps.hermesHome, 'never')+ const kill = vi.spyOn(updaterProcess, 'killHandoffTree')++ try {+ const result = await createCheckoutStrategy({ ...deps, handoffClaimTimeoutMs: 400 }).apply()++ expect(result).toMatchObject({ ok: true, handedOff: true })+ expect(deps.startHermes).not.toHaveBeenCalled()+ expect(kill).not.toHaveBeenCalled()+ expect(deps.markQuittingForHandoff).toHaveBeenCalled()+ // Asked again while unsettled, and Electron left the bytes alone.+ expect(helperCalls(deps.hermesHome)).toHaveLength(3)+ expect(fs.readFileSync(markerPath(deps.hermesHome), 'utf8')).toBe(runs[0]!.bridgeAtSpawn)+ } finally {+ fs.rmSync(root, { recursive: true, force: true })+ }+ }+ )++ it('a definitive `withdrawn` reports the failure and kills the spawned tree', async (): Promise<void> => {const { root, deps } = handoffFixture(false, fakeHelperScript())- fs.writeFileSync(path.join(deps.hermesHome, 'helper-verdict'), 'busy')- const runs = mockProtocol2Spawn(deps.hermesHome, 'never')+ fs.writeFileSync(path.join(deps.hermesHome, 'helper-verdict'), 'withdrawn')+ mockProtocol2Spawn(deps.hermesHome, 'never')+ const kill = vi.spyOn(updaterProcess, 'killHandoffTree').mockImplementation(() => {})try {const result = await createCheckoutStrategy({ ...deps, handoffClaimTimeoutMs: 400 }).apply()expect(result).toMatchObject({ ok: false, error: 'updater-spawn-failed' })+ expect(kill).toHaveBeenCalledTimes(1)expect(helperCalls(deps.hermesHome)).toHaveLength(1)- // The helper answered `busy` and kept it: Electron left the bytes alone.- expect(fs.readFileSync(markerPath(deps.hermesHome), 'utf8')).toBe(runs[0]!.bridgeAtSpawn)+ expect(deps.quit).not.toHaveBeenCalled()} finally {fs.rmSync(root, { recursive: true, force: true })}diff --git a/apps/desktop/electron/updater/checkout.ts b/apps/desktop/electron/updater/checkout.tsindex 68c78177b26..ea315a36440 100644--- a/apps/desktop/electron/updater/checkout.ts+++ b/apps/desktop/electron/updater/checkout.ts@@ -81,6 +81,18 @@ export interface HandoffPlan {const STILL_RUNNING_VERDICTS = new Set(['held', 'busy', 'live'])+/**+ * Withdraw answers after which no script can adopt this run any more: the+ * bridge was removed (`withdrawn`), is gone (`absent`), or is not this+ * Desktop's bridge for the run (`foreign`). `busy` / `error` / `unsupported`+ * leave the bridge in place.+ */+const WITHDRAW_SETTLED_VERDICTS = new Set(['withdrawn', 'absent', 'foreign'])++/** Withdraw attempts before an unsettled answer stands (a busy lock is usually brief). */+const WITHDRAW_ATTEMPTS = 3+const WITHDRAW_RETRY_MS = 250+/*** The manual command card for a checkout with no staged updater: the exact* `hermes update` line to run, branch-pinned to the checkout's current branch@@ -125,6 +137,25 @@ export function createCheckoutStrategy(deps: CheckoutStrategyDeps): UpdaterStrat})}+ /** `withdraw`, re-asked while the answer leaves the bridge in place; `taken` ends it at once. */+ async function withdrawBridge(runId: string): Promise<MarkerHelperVerdict> {+ let verdict: MarkerHelperVerdict = { kind: 'error' }++ for (let attempt = 0; attempt < WITHDRAW_ATTEMPTS; attempt++) {+ if (attempt > 0) {+ await new Promise(resolve => setTimeout(resolve, WITHDRAW_RETRY_MS))+ }++ verdict = await markerHelper('withdraw', runId)++ if (verdict.kind === 'taken' || WITHDRAW_SETTLED_VERDICTS.has(verdict.kind)) {+ break+ }+ }++ return verdict+ }+function refuse(message: string, error: string): { refusal: UpdaterApplyResultWire } {deps.rememberLog(`[updates] refusing hand-off: ${message}`)deps.emitUpdateProgress({ stage: 'error', message, percent: null })@@ -225,7 +256,7 @@ export function createCheckoutStrategy(deps: CheckoutStrategyDeps): UpdaterStrat// `taken` means a live script adopted our run at the deadline — the// update IS running.if (plan.runId) {- const verdict = await markerHelper('withdraw', plan.runId)+ const verdict = await withdrawBridge(plan.runId)if (verdict.kind === 'taken') {deps.rememberLog(`[updates] hand-off script took the update marker at the deadline (pid ${verdict.pid})`)@@ -233,6 +264,20 @@ export function createCheckoutStrategy(deps: CheckoutStrategyDeps): UpdaterStratreturn null}+ // Not withdrawn means the bridge may still be there, and the daemon+ // (setsid, out of killHandoffTree's reach) adopts it whenever it starts.+ // Reporting "did not start" and restarting the backend would then run+ // `hermes update` beside it. Quit instead: the next boot's update gate+ // judges the bridge or the script's claim under the script's lock.+ if (!WITHDRAW_SETTLED_VERDICTS.has(verdict.kind)) {+ deps.rememberLog(+ `[updates] bridge withdraw stayed ${verdict.kind}: a late hand-off script can still adopt run ${plan.runId}, ` ++ 'so this Desktop quits instead of restarting its backend beside it'+ )++ return null+ }+deps.rememberLog(`[updates] bridge withdraw: ${verdict.kind}`)}