Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions src/renderer/src/lib/sleeping-record-origin-host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>
sshTargetIds: ReadonlySet<string> | 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`
Expand All @@ -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:<environmentId>` — 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:<id>` has no way back.
*/
export function sleepingRecordOriginHostId(
entry: unknown,
Expand All @@ -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
Comment thread
kiwidream marked this conversation as resolved.
Resolved
}
4 changes: 2 additions & 2 deletions src/renderer/src/lib/workspace-session-host-persistence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
32 changes: 30 additions & 2 deletions src/renderer/src/lib/workspace-session-host-split.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>(),
sshTargetIds: new Set([SSH_TARGET_ID])
}
const SSH_HOST_ID: ExecutionHostId = toSshExecutionHostId(SSH_TARGET_ID)

function makeSleepingRecord(
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,19 @@ function makeApi(partitions: Record<string, WorkspaceSessionState>) {
}

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:<id>` 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.
Comment thread
kiwidream marked this conversation as resolved.
Resolved
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([])
Comment thread
kiwidream marked this conversation as resolved.
Resolved
})

it('never clears a partition holding rows the boot merge parked', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:<environmentId>` 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()
})
})

Expand Down