fix: pr-bug-scan validated finding from #2633 (#2637)

* fix: address pr-bug-scan validated finding from #2633

Split the post-stop refresh try/catch so only first-scan failures return ok:false; settled second-scan failures are swallowed since UI is already correct.

* test: cover settled port refresh failures

---------

Co-authored-by: orca-bug-scan-bot <orca-bug-scan-bot@stably.ai>
Co-authored-by: Neil <4138956+nwparker@users.noreply.github.com>
This commit is contained in:
buf0-bot[bot]
2026-05-31 01:41:26 -07:00
committed by GitHub
co-authored by orca-bug-scan-bot Neil
parent 5d5cd06a79
commit ec3c30edbb
2 changed files with 71 additions and 10 deletions
@@ -75,6 +75,7 @@ beforeEach(() => {
activateAndRevealWorktreeMock.mockReset()
clearRuntimeCompatibilityCacheForTests()
vi.stubGlobal('window', {
setTimeout: globalThis.setTimeout,
api: {
workspacePorts: {
scan: localScan,
@@ -300,6 +301,57 @@ describe('PortsPanel runtime routing', () => {
expect(setWorkspacePortScanRefreshing).toHaveBeenNthCalledWith(2, false)
})
it('ignores settled remote post-stop refresh failures after updating state', async () => {
const setWorkspacePortScan = vi.fn()
const setWorkspacePortScanRefreshing = vi.fn()
const firstScan = { ...emptyScan, scannedAt: 2 }
let scanCalls = 0
runtimeEnvironmentCall.mockImplementation(({ method }: { method: string }) => {
if (method === 'status.get') {
return Promise.resolve({
id: method,
ok: true,
result: compatibleStatus,
_meta: { runtimeId: 'runtime-1' }
})
}
if (method === 'workspacePorts.scan') {
scanCalls += 1
if (scanCalls === 1) {
return Promise.resolve({
id: method,
ok: true,
result: firstScan,
_meta: { runtimeId: 'runtime-1' }
})
}
return Promise.reject(new Error('transient RPC timeout'))
}
return Promise.reject(new Error(`Unexpected method ${method}`))
})
await expect(
refreshWorkspacePortScanAfterStop({
runtimeTarget: { kind: 'environment', environmentId: 'env-1' },
setWorkspacePortScan: setWorkspacePortScan as never,
setWorkspacePortScanRefreshing: setWorkspacePortScanRefreshing as never
})
).resolves.toEqual({ ok: true })
expect(runtimeEnvironmentCall.mock.calls.map((call) => call[0].method)).toEqual([
'status.get',
'workspacePorts.scan',
'workspacePorts.scan'
])
expect(setWorkspacePortScan).toHaveBeenCalledTimes(1)
expect(setWorkspacePortScan).toHaveBeenCalledWith({
key: 'environment:env-1:all',
result: firstScan
})
expect(setWorkspacePortScanRefreshing).toHaveBeenNthCalledWith(1, true)
expect(setWorkspacePortScanRefreshing).toHaveBeenNthCalledWith(2, false)
})
it('keeps remote workspace ports in the server-side browser when link routing is off', async () => {
runtimeEnvironmentCall.mockImplementation(({ method }: { method: string }) =>
Promise.resolve({
+19 -10
View File
@@ -126,7 +126,13 @@ export async function refreshWorkspacePortScanAfterStop(args: {
}): Promise<{ ok: true } | { ok: false; reason: string }> {
args.setWorkspacePortScanRefreshing(true)
try {
const firstScan = await scanWorkspacePortsForTarget(args.runtimeTarget)
let firstScan: WorkspacePortScanResult
try {
firstScan = await scanWorkspacePortsForTarget(args.runtimeTarget)
} catch (error) {
const message = error instanceof Error ? error.message : String(error)
return { ok: false, reason: message || 'Workspace port scan failed.' }
}
args.setWorkspacePortScan({
key: `${workspacePortRuntimeTargetKey(args.runtimeTarget)}:all`,
result: firstScan
@@ -134,17 +140,20 @@ export async function refreshWorkspacePortScanAfterStop(args: {
// Why: stopping sends SIGTERM, and the listener can remain visible for a
// short window. A settled re-scan keeps worktree cards from showing a stale
// port row after the process actually exits.
// port row after the process actually exits. Failures here are swallowed
// because the UI is already correct from the first scan; surfacing a
// 'Failed to refresh ports' toast on top of the stop success would lie.
await delay(WORKSPACE_PORT_STOP_SETTLE_MS)
const settledScan = await scanWorkspacePortsForTarget(args.runtimeTarget)
args.setWorkspacePortScan({
key: `${workspacePortRuntimeTargetKey(args.runtimeTarget)}:all`,
result: settledScan
})
try {
const settledScan = await scanWorkspacePortsForTarget(args.runtimeTarget)
args.setWorkspacePortScan({
key: `${workspacePortRuntimeTargetKey(args.runtimeTarget)}:all`,
result: settledScan
})
} catch {
// Intentionally ignored: first scan already updated the UI.
}
return { ok: true }
} catch (error) {
const message = error instanceof Error ? error.message : String(error)
return { ok: false, reason: message || 'Workspace port scan failed.' }
} finally {
args.setWorkspacePortScanRefreshing(false)
}