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.
This commit is contained in:
Mohamed Boudra
2026-07-16 15:10:39 +02:00
parent 06592061e2
commit 4d4e708c5c
2 changed files with 19 additions and 1 deletions
@@ -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,
@@ -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) => {