From 4d4e708c5c5e73dbbd3bc4f40320daaf07375723 Mon Sep 17 00:00:00 2001 From: Mohamed Boudra Date: Sat, 11 Jul 2026 14:16:10 +0200 Subject: [PATCH] fix(app): ignore stale History archive state for active agents A cached History row can outlive a successful reopen. When the workspace exists, live session state now wins so an active agent is not interrupted by another refresh. --- .../app/src/utils/navigate-to-agent/index.ts | 4 +++- .../restore-archived-workspace.test.ts | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/packages/app/src/utils/navigate-to-agent/index.ts b/packages/app/src/utils/navigate-to-agent/index.ts index b6b16f81d9..601c3626de 100644 --- a/packages/app/src/utils/navigate-to-agent/index.ts +++ b/packages/app/src/utils/navigate-to-agent/index.ts @@ -87,7 +87,9 @@ function restoreArchivedWorkspace(target: HistoryWorkspaceRestore): void { // must restore even when its agent survived unarchived; an archived agent must // still reopen when its workspace survived. if (session.workspaces.has(workspaceId)) { - if (agentArchived) { + const liveAgent = session.agents.get(agentId) ?? session.agentDetails.get(agentId); + const shouldReopenAgent = agentArchived && (!liveAgent || Boolean(liveAgent.archivedAt)); + if (shouldReopenAgent) { client.refreshAgent(agentId).catch((error) => { console.error("[HistoryRestore] Failed to reopen archived agent", { serverId, diff --git a/packages/app/src/utils/navigate-to-agent/restore-archived-workspace.test.ts b/packages/app/src/utils/navigate-to-agent/restore-archived-workspace.test.ts index 64134e8362..2b6d447e6d 100644 --- a/packages/app/src/utils/navigate-to-agent/restore-archived-workspace.test.ts +++ b/packages/app/src/utils/navigate-to-agent/restore-archived-workspace.test.ts @@ -297,6 +297,22 @@ describe("restoreArchivedWorkspace via navigateToAgent", () => { expect(status()).toBeNull(); }); + it("does not reopen an active agent from a stale archived History row", () => { + const store = useSessionStore.getState(); + store.setAgents(SERVER_ID, (prev) => { + const next = new Map(prev); + next.set(AGENT_ID, agent(null)); + return next; + }); + store.mergeWorkspaces(SERVER_ID, [workspace()]); + refreshAgent.mockImplementation(() => new Promise(() => {})); + + openFromHistory(); + + expect(refreshAgent).not.toHaveBeenCalled(); + expect(status()).toBeNull(); + }); + it("is a no-op when a non-archived workspace descriptor is already present", () => { const store = useSessionStore.getState(); store.setAgents(SERVER_ID, (prev) => {