From 4bae3662ccf8bf1ef54c388854e764343bc01ba8 Mon Sep 17 00:00:00 2001 From: Alexander Miller Date: Wed, 23 Sep 2026 13:14:19 -0400 Subject: [PATCH 1/4] fix(session): treat an unprovable connectionId as no host, not as ssh MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A sleeping record's `connectionId` is a host STAMP, not a host kind. Round four gated the ssh answer on `sshTargetIds` hydration but left the runtime side unguarded, so an id neither list claimed still fell through to `ssh:` — filing a runtime-stamped record captured during a catalog gap, or one whose environment was later removed, into a partition no runtime reader ever opens. Neither id space can disprove an id: the ssh list may not have loaded, and the runtime set is a best-effort union of whatever catalogs this boot holds. So only positive membership answers now, for both spaces. "Cannot prove" is null, which leaves the row local — the partition every reader loads, and the one `adoptStrandedHostPartitionSession` returns to its owner once some list can name it. A guessed `ssh:` has no way back. Co-Authored-By: Claude Opus 5 --- .../src/lib/sleeping-record-origin-host.ts | 22 ++++++---- .../lib/workspace-session-host-persistence.ts | 4 +- .../lib/workspace-session-host-split.test.ts | 32 +++++++++++++- ...ession-sleeping-record-origin-host.test.ts | 42 ++++++++++++++++++- 4 files changed, 86 insertions(+), 14 deletions(-) diff --git a/src/renderer/src/lib/sleeping-record-origin-host.ts b/src/renderer/src/lib/sleeping-record-origin-host.ts index bb6b823d0e64..2be0a7d74f5c 100644 --- a/src/renderer/src/lib/sleeping-record-origin-host.ts +++ b/src/renderer/src/lib/sleeping-record-origin-host.ts @@ -8,15 +8,17 @@ import { isWorkspaceSessionRecord } from '../../../shared/workspace-session-host /** The id spaces a sleeping record's own `connectionId` can be drawn from, so a reader can tell * which one a given value came out of instead of assuming. * - * `sshTargetIds` is null when this client has never loaded the target set: absence from an - * unhydrated list is no evidence the id is not a target (store/slices/ssh.ts `sshTargetsHydrated`). */ + * Neither set is a complete census: `sshTargetIds` is null until this client loads the target set + * (store/slices/ssh.ts `sshTargetsHydrated`), and `runtimeEnvironmentIds` is a best-effort union of + * the catalogs this boot happens to hold. So membership proves an id's kind; absence proves + * nothing about it. */ export type SleepingRecordOriginHosts = { runtimeEnvironmentIds: ReadonlySet sshTargetIds: ReadonlySet | null } /** - * The host a sleeping record's own capture names, or null when its stamp names none. + * The host a sleeping record's own capture names, or null when this client cannot prove one. * * A NONEMPTY connectionId is not automatically an ssh target — it is the third case beside the two * {@link ./sleeping-record-execution-host-scope.ts} names (`undefined` unstamped, `null` @@ -25,8 +27,12 @@ export type SleepingRecordOriginHosts = { * onto the record, so reading every nonempty value as ssh files the pane's only resume handle under * `ssh:` — a partition no runtime reader ever opens. * - * So the kind is proven from the id spaces this client already holds, and an id neither of them - * claims names no host rather than guessing one for it. + * Only positive membership answers, because neither id space can disprove an id: an unhydrated ssh + * list has not loaded, and the runtime union is whatever catalogs this boot holds, so an id both + * lists are silent about is indistinguishable between a catalog gap, a removed environment and an + * unknown target. Null is that "cannot prove", and it leaves the row local — the partition every + * reader loads, and the one `adoptStrandedHostPartitionSession` returns to its owner once some list + * can name it. A guessed `ssh:` has no such way back. */ export function sleepingRecordOriginHostId( entry: unknown, @@ -42,8 +48,8 @@ export function sleepingRecordOriginHostId( if (origins?.runtimeEnvironmentIds.has(targetId)) { return toRuntimeExecutionHostId(targetId) } - if (origins?.sshTargetIds && !origins.sshTargetIds.has(targetId)) { - return null + if (origins?.sshTargetIds?.has(targetId)) { + return toSshExecutionHostId(targetId) } - return toSshExecutionHostId(targetId) + return null } diff --git a/src/renderer/src/lib/workspace-session-host-persistence.ts b/src/renderer/src/lib/workspace-session-host-persistence.ts index 68a5cbc41f23..2cdaf053a334 100644 --- a/src/renderer/src/lib/workspace-session-host-persistence.ts +++ b/src/renderer/src/lib/workspace-session-host-persistence.ts @@ -258,8 +258,8 @@ export function buildHostSessionRouting(state: HostPersistenceState): HostSessio claims, sleepingRecordOrigins: { runtimeEnvironmentIds: collectRuntimeEnvironmentIds(state, runtimeHostIdByWorktreeId), - // Why the hydration gate: an unloaded target list says nothing about an id, and reading its - // silence as "not an ssh target" would strand every ssh record on a client that has no ssh RPC. + // Why the hydration gate: an unloaded target list says nothing about an id, so `null` keeps it + // distinguishable from a loaded list that genuinely does not hold the id. sshTargetIds: state.sshTargetsHydrated ? new Set(state.sshTargetLabels?.keys() ?? []) : null } } diff --git a/src/renderer/src/lib/workspace-session-host-split.test.ts b/src/renderer/src/lib/workspace-session-host-split.test.ts index 746224aa8b3d..1b1df97ef080 100644 --- a/src/renderer/src/lib/workspace-session-host-split.test.ts +++ b/src/renderer/src/lib/workspace-session-host-split.test.ts @@ -70,6 +70,12 @@ function makeBrowserPage(id: string, workspaceId: string, worktreeId: string): B } const SSH_TARGET_ID = 'ssh-1788980107277-d9gffw' +/** A record's own stamp only names a host when some loaded id space claims it; production always + * passes these, and without them a stamp is unprovable and stays local. */ +const HYDRATED_ORIGINS = { + runtimeEnvironmentIds: new Set(), + sshTargetIds: new Set([SSH_TARGET_ID]) +} const SSH_HOST_ID: ExecutionHostId = toSshExecutionHostId(SSH_TARGET_ID) function makeSleepingRecord( @@ -351,12 +357,32 @@ describe('splitWorkspaceSessionByHost', () => { } } - const slices = splitWorkspaceSessionByHost(state, ownerByPrefix()) + const slices = splitWorkspaceSessionByHost(state, ownerByPrefix(), { + sleepingRecordOrigins: HYDRATED_ORIGINS + }) expect(slices[SSH_HOST_ID]?.sleepingAgentSessionsByPaneKey).toHaveProperty('pane-ssh') expect(slices[LOCAL_EXECUTION_HOST_ID]?.sleepingAgentSessionsByPaneKey).toEqual({}) }) + it('keeps an ssh-stamped record local while no id space can prove the stamp', () => { + const state: WorkspaceSessionState = { + ...getDefaultWorkspaceSession(), + sleepingAgentSessionsByPaneKey: { + 'pane-ssh': makeSleepingRecord('pane-ssh', 'unknown-wt', SSH_TARGET_ID) + } + } + + const slices = splitWorkspaceSessionByHost(state, ownerByPrefix(), { + sleepingRecordOrigins: { runtimeEnvironmentIds: new Set(), sshTargetIds: null } + }) + + expect(slices[LOCAL_EXECUTION_HOST_ID]?.sleepingAgentSessionsByPaneKey).toHaveProperty( + 'pane-ssh' + ) + expect(slices[SSH_HOST_ID]).toBeUndefined() + }) + it('keeps a sleeping record put when its connectionId names no host', () => { const state: WorkspaceSessionState = { ...getDefaultWorkspaceSession(), @@ -406,7 +432,9 @@ describe('splitWorkspaceSessionByHost', () => { 'pane-ssh' ]) - const slices = splitWorkspaceSessionByHost(merged, ownerByPrefix()) + const slices = splitWorkspaceSessionByHost(merged, ownerByPrefix(), { + sleepingRecordOrigins: HYDRATED_ORIGINS + }) expect(slices[SSH_HOST_ID]?.sleepingAgentSessionsByPaneKey).toHaveProperty('pane-ssh') expect(slices[LOCAL_EXECUTION_HOST_ID]?.sleepingAgentSessionsByPaneKey).toHaveProperty( 'pane-local' diff --git a/src/renderer/src/lib/workspace-session-sleeping-record-origin-host.test.ts b/src/renderer/src/lib/workspace-session-sleeping-record-origin-host.test.ts index c41217f8f058..5555fd70206f 100644 --- a/src/renderer/src/lib/workspace-session-sleeping-record-origin-host.test.ts +++ b/src/renderer/src/lib/workspace-session-sleeping-record-origin-host.test.ts @@ -92,13 +92,51 @@ describe('sleeping-record origin host, by id space', () => { expect(slices[toSshExecutionHostId('who-knows')]).toBeUndefined() }) - it('treats an unhydrated ssh target list as no evidence and keeps the ssh fallback', () => { + it('keeps a runtime-stamped record local when the environment catalog has not landed', () => { + // The catalog-gap capture: the stamp IS a runtime environment id, but neither list can say so + // yet. `ssh:` is a partition no reader opens, so the guess is unrecoverable. + const slices = split( + sessionWith({ 'pane-rt': makeRecord('pane-rt', RUNTIME_ENVIRONMENT_ID) }), + { + runtimeEnvironmentIds: new Set(), + sshTargetIds: null + } + ) + + expect(slices[LOCAL_EXECUTION_HOST_ID]?.sleepingAgentSessionsByPaneKey).toHaveProperty( + 'pane-rt' + ) + expect(slices[toSshExecutionHostId(RUNTIME_ENVIRONMENT_ID)]).toBeUndefined() + }) + + it('keeps a record local when its runtime environment was removed from the catalog', () => { + const slices = split( + sessionWith({ 'pane-rt': makeRecord('pane-rt', RUNTIME_ENVIRONMENT_ID) }), + { + runtimeEnvironmentIds: new Set(['env-other']), + sshTargetIds: null + } + ) + + expect(slices[LOCAL_EXECUTION_HOST_ID]?.sleepingAgentSessionsByPaneKey).toHaveProperty( + 'pane-rt' + ) + expect(slices[toSshExecutionHostId(RUNTIME_ENVIRONMENT_ID)]).toBeUndefined() + }) + + it('keeps an unhydrated ssh target list local rather than guessing a partition', () => { + // An unhydrated list cannot tell an ssh target apart from a runtime environment id, so the two + // cases above are this same call. Local is the recoverable placement: every reader loads it and + // `adoptStrandedHostPartitionSession` returns the row to its partition once a list can prove it. const slices = split(sessionWith({ 'pane-ssh': makeRecord('pane-ssh', SSH_TARGET_ID) }), { runtimeEnvironmentIds: new Set(), sshTargetIds: null }) - expect(slices[SSH_HOST_ID]?.sleepingAgentSessionsByPaneKey).toHaveProperty('pane-ssh') + expect(slices[LOCAL_EXECUTION_HOST_ID]?.sleepingAgentSessionsByPaneKey).toHaveProperty( + 'pane-ssh' + ) + expect(slices[SSH_HOST_ID]).toBeUndefined() }) }) From 831718828352dbbee614dfb18f0b1637ae5be742 Mon Sep 17 00:00:00 2001 From: Alex Date: Wed, 23 Sep 2026 14:17:28 -0400 Subject: [PATCH 2/4] docs(session): cut the routing rationale to what the code does not say Review follow-up. The six-line block restated the type doc and the function body; what it had to carry is that catalog absence is not authoritative, so an unplaceable id stays local and stays recoverable. Co-Authored-By: Claude Opus 5 (1M context) --- .../src/lib/sleeping-record-origin-host.ts | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/src/renderer/src/lib/sleeping-record-origin-host.ts b/src/renderer/src/lib/sleeping-record-origin-host.ts index 2be0a7d74f5c..8a129f1a0f54 100644 --- a/src/renderer/src/lib/sleeping-record-origin-host.ts +++ b/src/renderer/src/lib/sleeping-record-origin-host.ts @@ -8,10 +8,9 @@ import { isWorkspaceSessionRecord } from '../../../shared/workspace-session-host /** The id spaces a sleeping record's own `connectionId` can be drawn from, so a reader can tell * which one a given value came out of instead of assuming. * - * Neither set is a complete census: `sshTargetIds` is null until this client loads the target set - * (store/slices/ssh.ts `sshTargetsHydrated`), and `runtimeEnvironmentIds` is a best-effort union of - * the catalogs this boot happens to hold. So membership proves an id's kind; absence proves - * nothing about it. */ + * Membership proves an id's kind; absence proves nothing — `sshTargetIds` is null until loaded + * (store/slices/ssh.ts `sshTargetsHydrated`) and `runtimeEnvironmentIds` holds only this boot's + * catalogs. */ export type SleepingRecordOriginHosts = { runtimeEnvironmentIds: ReadonlySet sshTargetIds: ReadonlySet | null @@ -27,12 +26,9 @@ export type SleepingRecordOriginHosts = { * onto the record, so reading every nonempty value as ssh files the pane's only resume handle under * `ssh:` — a partition no runtime reader ever opens. * - * Only positive membership answers, because neither id space can disprove an id: an unhydrated ssh - * list has not loaded, and the runtime union is whatever catalogs this boot holds, so an id both - * lists are silent about is indistinguishable between a catalog gap, a removed environment and an - * unknown target. Null is that "cannot prove", and it leaves the row local — the partition every - * reader loads, and the one `adoptStrandedHostPartitionSession` returns to its owner once some list - * can name it. A guessed `ssh:` has no such way back. + * An id neither list names stays local, because catalog absence is not authoritative: local is the + * partition every reader loads, and `adoptStrandedHostPartitionSession` can still hand it back once + * some list names it. A guessed `ssh:` has no way back. */ export function sleepingRecordOriginHostId( entry: unknown, From 5868a002c9b1a07fb7b6c6fa1d30912f8c5ea933 Mon Sep 17 00:00:00 2001 From: Alex Date: Wed, 23 Sep 2026 14:49:05 -0400 Subject: [PATCH 3/4] test(session): pin where an unprovable connectionId parks MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This test asserted the old contract — a nonempty connectionId reaching the ssh partition — and the routing fix changes that answer, so it failed rather than caught anything. Its setup is still the scenario worth pinning: a catalog with no row for the worktree, so only the record's own stamp names a host. What makes local the right answer is asymmetry, not preference. `fetchWorkspaceSessionWithRuntimeHostOwners` enumerates the local partition and one per KNOWN runtime and ssh host. A phantom `ssh:` is not a known target, so it is never enumerated, never fetched, never merged — a wrong guess loses the pane's only resume handle permanently and silently. `local` is fetched unconditionally, and the next write re-routes the record once the target list can name it, so a mis-park corrects itself. Co-Authored-By: Claude Opus 5 (1M context) --- ...orkspace-session-partition-stale-row-leak.test.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/renderer/src/lib/workspace-session-partition-stale-row-leak.test.ts b/src/renderer/src/lib/workspace-session-partition-stale-row-leak.test.ts index f9311fb5b3c0..7d336904687e 100644 --- a/src/renderer/src/lib/workspace-session-partition-stale-row-leak.test.ts +++ b/src/renderer/src/lib/workspace-session-partition-stale-row-leak.test.ts @@ -62,13 +62,19 @@ function makeApi(partitions: Record) { } describe('a sleeping-record field emptied on a non-local partition', () => { - it('sends no clear to the ssh partition the last record left', async () => { + it('parks an unprovable connectionId locally instead of guessing an ssh partition', async () => { const { api, patch } = makeApi({}) await patchWorkspaceSessionByHost(api, withRecord, catalog) await patchWorkspaceSessionByHost(api, cleared, catalog) await new Promise((resolve) => setTimeout(resolve, 0)) - // The row write went out; the emptying patch names no ssh host, so the copy there survives. - expect(sshPatches(patch)).toEqual([withRecord]) + // The target list has not hydrated, so nothing proves this id names an ssh host. The two + // outcomes are not symmetric: `fetchWorkspaceSessionWithRuntimeHostOwners` enumerates only + // KNOWN ssh hosts, so a wrong `ssh:` guess is never fetched again and the pane's only + // resume handle is gone for good. `local` is always fetched, and the next write re-routes the + // record once the list names it — a mis-park corrects itself, a mis-guess does not. + expect(sshPatches(patch)).toEqual([]) + // Still the accepted leak: no clear is sent anywhere on the emptying patch. + expect(patch.mock.calls.filter(([args]) => args === cleared)).toEqual([]) }) it('never clears a partition holding rows the boot merge parked', async () => { From adeaa9b33ec0b9fc97545b09f7f013d8face6ca3 Mon Sep 17 00:00:00 2001 From: Alex Date: Wed, 23 Sep 2026 15:03:36 -0400 Subject: [PATCH 4/4] test(session): assert where the record actually landed MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Review follow-up on the previous commit. The clear assertion compared the patch argument by reference, but `splitWorkspaceSessionForWrite` builds a new slice, so it could never match and the check passed without testing anything. It now asserts the record reached the local partition — verified to fail if the classifier goes back to guessing ssh — and the rationale is one line instead of a five-line walkthrough of what the assertions already show. Co-Authored-By: Claude Opus 5 (1M context) --- ...e-session-partition-stale-row-leak.test.ts | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/renderer/src/lib/workspace-session-partition-stale-row-leak.test.ts b/src/renderer/src/lib/workspace-session-partition-stale-row-leak.test.ts index 7d336904687e..aa33d89a946c 100644 --- a/src/renderer/src/lib/workspace-session-partition-stale-row-leak.test.ts +++ b/src/renderer/src/lib/workspace-session-partition-stale-row-leak.test.ts @@ -45,6 +45,18 @@ function sshPatches(patch: ReturnType): unknown[] { return patch.mock.calls.filter(([, hostId]) => hostId === SSH_HOST_ID).map(([args]) => args) } +function localRecordWrites(patch: ReturnType): unknown[] { + return ( + patch.mock.calls + // The local slice is written with no host argument at all, not with the local id. + .filter(([, hostId]) => hostId === undefined || hostId === LOCAL_EXECUTION_HOST_ID) + .map(([args]) => { + const slice: Partial = args + return slice.sleepingAgentSessionsByPaneKey + }) + ) +} + function makeApi(partitions: Record) { const patch = vi.fn().mockResolvedValue(undefined) return { @@ -67,14 +79,10 @@ describe('a sleeping-record field emptied on a non-local partition', () => { await patchWorkspaceSessionByHost(api, withRecord, catalog) await patchWorkspaceSessionByHost(api, cleared, catalog) await new Promise((resolve) => setTimeout(resolve, 0)) - // The target list has not hydrated, so nothing proves this id names an ssh host. The two - // outcomes are not symmetric: `fetchWorkspaceSessionWithRuntimeHostOwners` enumerates only - // KNOWN ssh hosts, so a wrong `ssh:` guess is never fetched again and the pane's only - // resume handle is gone for good. `local` is always fetched, and the next write re-routes the - // record once the list names it — a mis-park corrects itself, a mis-guess does not. + // Local is always fetched; an unproven `ssh:` partition never is, so guessing it loses + // the handle for good while parking locally corrects itself on the next write. expect(sshPatches(patch)).toEqual([]) - // Still the accepted leak: no clear is sent anywhere on the emptying patch. - expect(patch.mock.calls.filter(([args]) => args === cleared)).toEqual([]) + expect(localRecordWrites(patch)).toContainEqual({ [record.paneKey]: record }) }) it('never clears a partition holding rows the boot merge parked', async () => {