From ba034c3c3d36dfc91edabf599a84ad755424ac70 Mon Sep 17 00:00:00 2001 From: "buf0-bot[bot]" <252831055+buf0-bot[bot]@users.noreply.github.com> Date: Thu, 7 May 2026 23:07:38 -0700 Subject: [PATCH] fix: pr-bug-scan findings from #1454 (#1567) Addresses two SIGKILL/wake-routing bugs: - setWorkspaceSession now merges prior tab.ptyId / ptyIdsByLeafId when the incoming snapshot's binding is empty, closing the second half of the SIGKILL race that could clobber persistPtyBinding. - DaemonPtyRouter.shutdown skips sessionAdapters.delete when keepHistory=true so legacy-adapter sessions wake on the right adapter on resume. Co-authored-by: orca-bot --- src/main/daemon/daemon-pty-router.ts | 10 ++++++- src/main/persistence.ts | 40 ++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/main/daemon/daemon-pty-router.ts b/src/main/daemon/daemon-pty-router.ts index 70e383f72..b0e6c263a 100644 --- a/src/main/daemon/daemon-pty-router.ts +++ b/src/main/daemon/daemon-pty-router.ts @@ -65,7 +65,15 @@ export class DaemonPtyRouter implements IPtyProvider { async shutdown(id: string, opts: { immediate?: boolean; keepHistory?: boolean }): Promise { await this.adapterFor(id).shutdown(id, opts) - this.sessionAdapters.delete(id) + // Why: sleep passes keepHistory=true and re-spawns against the same + // sessionId on wake. If we delete the routing entry here, adapterFor() + // falls back to `this.current` on wake — for a session that originally + // lived on a legacy adapter (different protocolVersion), the wake-side + // createOrAttach lands on the wrong adapter and creates a fresh session, + // losing the cold-restore from the legacy adapter's history dir. + if (!opts.keepHistory) { + this.sessionAdapters.delete(id) + } } async sendSignal(id: string, signal: string): Promise { diff --git a/src/main/persistence.ts b/src/main/persistence.ts index 770d1e00d..8f88719b9 100644 --- a/src/main/persistence.ts +++ b/src/main/persistence.ts @@ -671,6 +671,46 @@ export class Store { } setWorkspaceSession(session: PersistedState['workspaceSession']): void { + // Why: closes the second half of the SIGKILL race (Issue #217). The + // renderer's debounced session writer captures its state BEFORE pty:spawn + // returns, so the snapshot it later flushes via session:set has no + // tab.ptyId / ptyIdsByLeafId for the just-spawned PTY. If that stale + // snapshot lands AFTER persistPtyBinding's sync flush, it would overwrite + // the durable binding and re-open the orphan window. Merge in any + // existing bindings whenever the incoming snapshot's binding is empty. + const prior = this.state.workspaceSession + if (session && prior) { + const priorTabs = prior.tabsByWorktree ?? {} + const nextTabs = session.tabsByWorktree ?? {} + for (const [worktreeId, tabs] of Object.entries(nextTabs)) { + const priorList = priorTabs[worktreeId] + if (!priorList) { + continue + } + for (const tab of tabs) { + if (tab.ptyId) { + continue + } + const priorTab = priorList.find((t) => t.id === tab.id) + if (priorTab?.ptyId) { + tab.ptyId = priorTab.ptyId + } + } + } + const priorLayouts = prior.terminalLayoutsByTabId ?? {} + const nextLayouts = session.terminalLayoutsByTabId ?? {} + for (const [tabId, layout] of Object.entries(nextLayouts)) { + const priorLayout = priorLayouts[tabId] + if (!priorLayout?.ptyIdsByLeafId) { + continue + } + const incoming = layout.ptyIdsByLeafId + if (incoming && Object.keys(incoming).length > 0) { + continue + } + layout.ptyIdsByLeafId = { ...priorLayout.ptyIdsByLeafId } + } + } this.state.workspaceSession = session this.scheduleSave() }