diff --git a/src/renderer/src/lib/sleeping-record-origin-host.ts b/src/renderer/src/lib/sleeping-record-origin-host.ts index bb6b823d0e64..8a129f1a0f54 100644 --- a/src/renderer/src/lib/sleeping-record-origin-host.ts +++ b/src/renderer/src/lib/sleeping-record-origin-host.ts @@ -8,15 +8,16 @@ 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`). */ + * 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 } /** - * 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 +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. * - * 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. + * 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, @@ -42,8 +44,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-partition-stale-row-leak.test.ts b/src/renderer/src/lib/workspace-session-partition-stale-row-leak.test.ts index f9311fb5b3c0..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 { @@ -62,13 +74,15 @@ 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]) + // 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([]) + expect(localRecordWrites(patch)).toContainEqual({ [record.paneKey]: record }) }) it('never clears a partition holding rows the boot merge parked', async () => { 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() }) })