Add Space manager V2 safeguards

This commit is contained in:
Neil
2026-05-14 23:30:22 -07:00
committed by GitHub
parent dc83da7036
commit 2d44ee83b4
35 changed files with 2276 additions and 401 deletions
@@ -123,6 +123,34 @@ describe('SshChannelMultiplexer', () => {
vi.advanceTimersByTime(1_000)
await expect(promise).rejects.toThrow('timed out')
const cancelPayload = JSON.parse(
transport.written
.at(-1)!
.subarray(HEADER_LENGTH, HEADER_LENGTH + transport.written.at(-1)!.readUInt32BE(9))
.toString()
)
expect(cancelPayload).toMatchObject({
method: 'rpc.cancel',
params: { id: 1 }
})
})
it('uses per-request timeout overrides', async () => {
const promise = mux.request('fs.workspaceSpaceScan', {}, { timeoutMs: 60_000 })
for (let i = 0; i < 6; i++) {
vi.advanceTimersByTime(5_000)
transport.dataCallbacks[0](encodeKeepAliveFrame(i + 1, 0))
}
await Promise.resolve()
const requestWrites = transport.written.filter((frame) => frame[0] === MessageType.Regular)
expect(requestWrites).toHaveLength(1)
for (let i = 6; i < 12; i++) {
vi.advanceTimersByTime(5_000)
transport.dataCallbacks[0](encodeKeepAliveFrame(i + 1, 0))
}
await expect(promise).rejects.toThrow('timed out after 60000ms')
})
it('assigns unique request IDs', async () => {
+51 -7
View File
@@ -1,3 +1,5 @@
/* eslint-disable max-lines -- Why: the SSH relay protocol state machine keeps
request, notification, keepalive, and cancellation semantics paired. */
import {
FrameDecoder,
MessageType,
@@ -24,6 +26,7 @@ type PendingRequest = {
resolve: (result: unknown) => void
reject: (error: Error) => void
timer: ReturnType<typeof setTimeout>
cleanup: () => void
}
export type NotificationHandler = (method: string, params: Record<string, unknown>) => void
@@ -127,10 +130,19 @@ export class SshChannelMultiplexer {
/**
* Send a JSON-RPC request and wait for the response.
*/
async request(method: string, params?: Record<string, unknown>): Promise<unknown> {
async request(
method: string,
params?: Record<string, unknown>,
options?: { signal?: AbortSignal; timeoutMs?: number }
): Promise<unknown> {
if (this.disposed) {
throw new Error('Multiplexer disposed')
}
if (options?.signal?.aborted) {
const error = new Error(`Request "${method}" was cancelled`) as Error & { name: string }
error.name = 'AbortError'
throw error
}
const id = this.nextRequestId++
const msg: JsonRpcRequest = {
@@ -139,14 +151,46 @@ export class SshChannelMultiplexer {
method,
...(params !== undefined ? { params } : {})
}
const timeoutMs = options?.timeoutMs ?? REQUEST_TIMEOUT_MS
return new Promise((resolve, reject) => {
const timer = setTimeout(() => {
let timer: ReturnType<typeof setTimeout>
const cleanup = (): void => {
clearTimeout(timer)
if (options?.signal) {
options.signal.removeEventListener('abort', onAbort)
}
}
const onAbort = (): void => {
const pending = this.pendingRequests.get(id)
if (!pending) {
return
}
pending.cleanup()
this.pendingRequests.delete(id)
reject(new Error(`Request "${method}" timed out after ${REQUEST_TIMEOUT_MS}ms`))
}, REQUEST_TIMEOUT_MS)
// Why: Space scans can run long on SSH hosts. Let the relay stop its
// local filesystem work instead of only dropping the client promise.
this.notify('rpc.cancel', { id })
const error = new Error(`Request "${method}" was cancelled`) as Error & { name: string }
error.name = 'AbortError'
pending.reject(error)
}
timer = setTimeout(() => {
const pending = this.pendingRequests.get(id)
if (pending) {
pending.cleanup()
// Why: request timeouts should stop relay-side long-running work,
// not just detach the client from the eventual response.
this.notify('rpc.cancel', { id })
}
this.pendingRequests.delete(id)
reject(new Error(`Request "${method}" timed out after ${timeoutMs}ms`))
}, timeoutMs)
this.pendingRequests.set(id, { resolve, reject, timer })
if (options?.signal) {
options.signal.addEventListener('abort', onAbort, { once: true })
}
this.pendingRequests.set(id, { resolve, reject, timer, cleanup })
this.sendMessage(msg)
})
}
@@ -194,7 +238,7 @@ export class SshChannelMultiplexer {
const errorCode = reason === 'connection_lost' ? 'CONNECTION_LOST' : 'DISPOSED'
for (const [id, pending] of this.pendingRequests) {
clearTimeout(pending.timer)
pending.cleanup()
const err = new Error(errorMessage) as Error & { code: string }
err.code = errorCode
pending.reject(err)
@@ -295,7 +339,7 @@ export class SshChannelMultiplexer {
return
}
clearTimeout(pending.timer)
pending.cleanup()
this.pendingRequests.delete(msg.id)
if (msg.error) {