diff --git a/src/renderer/src/runtime/web-session-tabs-sync.test.ts b/src/renderer/src/runtime/web-session-tabs-sync.test.ts index f44d81365c6..cc4b91c4d84 100644 --- a/src/renderer/src/runtime/web-session-tabs-sync.test.ts +++ b/src/renderer/src/runtime/web-session-tabs-sync.test.ts @@ -1954,6 +1954,35 @@ describe('applyWebSessionTabsSnapshot', () => { expect(attributionPatch.agentStatusEpoch).toBe(8) expect(attributionPatch.sortEpoch).toBe(12) + const fresherAttributionPatch = applyWebSessionTabsSnapshot( + makeState({ + ...initial, + agentStatusByPaneKey: { + [mirroredPaneKey]: { + ...existing, + updatedAt: NOW, + worktreeId: 'stale-worktree', + tabId: 'stale-tab', + providerSession: undefined + } + }, + agentStatusEpoch: 7, + sortEpoch: 11 + }), + { ...snapshot, snapshotVersion: 3 }, + ENV, + NOW + ) as Partial + + expect(fresherAttributionPatch.agentStatusByPaneKey?.[mirroredPaneKey]).toMatchObject({ + worktreeId: existing.worktreeId, + tabId: existing.tabId, + updatedAt: NOW, + providerSession: { key: 'session_id', id: 'session-1' } + }) + expect(fresherAttributionPatch.agentStatusEpoch).toBe(8) + expect(fresherAttributionPatch.sortEpoch).toBe(12) + const identityPatch = applyWebSessionTabsSnapshot( makeState({ ...initial, @@ -1966,7 +1995,7 @@ describe('applyWebSessionTabsSnapshot', () => { } } }), - { ...snapshot, snapshotVersion: 3 }, + { ...snapshot, snapshotVersion: 4 }, ENV, NOW ) as Partial diff --git a/src/renderer/src/runtime/web-session-tabs-sync.ts b/src/renderer/src/runtime/web-session-tabs-sync.ts index 82f38324fe7..d1b0b4ba538 100644 --- a/src/renderer/src/runtime/web-session-tabs-sync.ts +++ b/src/renderer/src/runtime/web-session-tabs-sync.ts @@ -713,11 +713,14 @@ function buildMirroredAgentStatusPatch( continue } const existing = state.agentStatusByPaneKey[entry.paneKey] - // Why: an active web stream can report a fresher OSC 9999 status before the next host snapshot, so don't rewind it with an older host publication. + // Why: keep fresher OSC state while taking remapped ownership metadata from the authoritative host snapshot. const nextEntry = existing && existing.updatedAt > entry.updatedAt ? { ...normalizeCompatibleAgentStatusEntryForOwner(existing, entry.agentType), + paneKey: entry.paneKey, + worktreeId: entry.worktreeId ?? existing.worktreeId, + tabId: entry.tabId, providerSession: existing.providerSession ?? entry.providerSession } : entry