From 0c59e8de89b30664d884352eff7462d5228401fa Mon Sep 17 00:00:00 2001 From: Jinwoo-H Date: Tue, 10 Nov 2026 05:31:42 -0500 Subject: [PATCH] chore(orchestration): no type assertions in the forwarder --- .../remote-owned/remote-owned-answers.ts | 25 ++++++++++++------- .../remote-owned/remote-owned-route.test.ts | 23 +++++++++++------ .../remote-owned/remote-owned-route.ts | 2 +- .../remote-owned/remote-owned-start.ts | 2 +- 4 files changed, 33 insertions(+), 19 deletions(-) diff --git a/src/main/runtime/orchestration/federation/remote-owned/remote-owned-answers.ts b/src/main/runtime/orchestration/federation/remote-owned/remote-owned-answers.ts index 7a3b9f2f98d..776079f16aa 100644 --- a/src/main/runtime/orchestration/federation/remote-owned/remote-owned-answers.ts +++ b/src/main/runtime/orchestration/federation/remote-owned/remote-owned-answers.ts @@ -29,10 +29,13 @@ export type ServerTerms = { * it came from so recovery (`request-show --on`) asks there. */ export function inServerTerms(value: T, { server, forward, link }: ServerTerms): T { - const pairs: [string, string][] = [ - ...(forward ? [[forward.key, forward.requestId] as [string, string]] : []), - ...(link?.peer_run_id ? [[link.peer_run_id, link.run_id] as [string, string]] : []) - ] + const pairs: [string, string][] = [] + if (forward) { + pairs.push([forward.key, forward.requestId]) + } + if (link?.peer_run_id) { + pairs.push([link.peer_run_id, link.run_id]) + } const swap = (item: unknown): unknown => { if (typeof item === 'string') { return pairs.reduce((text, [from, to]) => text.split(from).join(to), item) @@ -47,12 +50,16 @@ export function inServerTerms(value: T, { server, forward, link }: ServerTerm } if (value instanceof Error) { const code = 'code' in value && typeof value.code === 'string' ? value.code : 'runtime_error' - const data = swap(errorData(value) ?? {}) - // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: an error stays an error with the same code. - return new OrchestrationError(code, String(swap(value.message)), { - ...(data as Record), + const data = Object.entries(errorData(value) ?? {}).map(([field, inner]) => [ + field, + swap(inner) + ]) + const error = new OrchestrationError(code, String(swap(value.message)), { + ...Object.fromEntries(data), server - }) as T + }) + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: an error answer stays an error, with the same code. + return error as T } // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: only string contents change; every shape is kept. return swap(value) as T diff --git a/src/main/runtime/orchestration/federation/remote-owned/remote-owned-route.test.ts b/src/main/runtime/orchestration/federation/remote-owned/remote-owned-route.test.ts index fba0ac13967..4e80ac76236 100644 --- a/src/main/runtime/orchestration/federation/remote-owned/remote-owned-route.test.ts +++ b/src/main/runtime/orchestration/federation/remote-owned/remote-owned-route.test.ts @@ -33,6 +33,10 @@ type Sent = { } type Answer = { result: unknown } | { error: string; data?: unknown } | 'unreachable' +function record(value: unknown): Record { + return value && typeof value === 'object' ? { ...value } : {} +} + describe('commands about a worker a paired server owns', () => { let db: OrchestrationDb let runtime: OrcaRuntimeService @@ -69,7 +73,7 @@ describe('commands about a worker a paired server owns', () => { const prepared = send?.prepareRequest?.({ params, envelope, measure: () => 0 }) sent.push({ method, - params: (prepared?.params ?? params) as Record, + params: record(prepared?.params ?? params), envelope: prepared?.envelope ?? envelope }) await whileSending() @@ -148,7 +152,12 @@ describe('commands about a worker a paired server owns', () => { if (!response.ok) { throw new Error(response.error.message) } - return response.result as { taskId: string; runId: string; dispatchId: string } + const receipt = record(response.result) + return { + taskId: String(receipt.taskId), + runId: String(receipt.runId), + dispatchId: String(receipt.dispatchId) + } } it("starts the worker on a server that owns its workers, then records it under this Orca's Task", async () => { @@ -187,7 +196,7 @@ describe('commands about a worker a paired server owns', () => { { from: 'term_coord', on: 'devB', spec: 'refused', worktree: 'current' }, { requestId: REQUEST } ) - const refusedTask = String((sent[0].params as { adoptTaskId: string }).adoptTaskId) + const refusedTask = String(sent[0].params.adoptTaskId) expect(getRemotePlacement(db, refusedTask)).toBeUndefined() // As a refused local start creates no Task, a refused start there leaves none here. expect(db.getTask(refusedTask)).toBeUndefined() @@ -198,7 +207,7 @@ describe('commands about a worker a paired server owns', () => { { requestId: 'a3f1e9d2-5b6c-4d7e-8f90-1a2b3c4d5e6f' } ) expect(lost.ok ? undefined : lost.error.code).toBe('remote_runtime_unavailable') - const lostTask = String((sent[1].params as { adoptTaskId: string }).adoptTaskId) + const lostTask = String(sent[1].params.adoptTaskId) expect(getRemotePlacement(db, lostTask)).toMatchObject({ state: 'reserved', outcome_unknown: 1 @@ -295,11 +304,9 @@ describe('commands about a worker a paired server owns', () => { it("answers a remote worker's question only from its Run's coordinator, as a local one is", async () => { const receipt = await startOnB() - const link = db.db.prepare('SELECT * FROM remote_run_links').get() as { - conversation_id: string - } + const link = record(db.db.prepare('SELECT conversation_id FROM remote_run_links').get()) importRemoteOwnedItem(db, remoteServerPin(server), { - conversationId: link.conversation_id, + conversationId: String(link.conversation_id), sequence: 1, messageId: 'msg_q1', kind: 'worker_mail', diff --git a/src/main/runtime/orchestration/federation/remote-owned/remote-owned-route.ts b/src/main/runtime/orchestration/federation/remote-owned/remote-owned-route.ts index dc72bb81d56..1df12fab594 100644 --- a/src/main/runtime/orchestration/federation/remote-owned/remote-owned-route.ts +++ b/src/main/runtime/orchestration/federation/remote-owned/remote-owned-route.ts @@ -284,7 +284,7 @@ async function stopRemoteWorker( /** A Dispatch the server's answer names (a follow-up it started) is its; record it so commands about it route there. */ function seedNamedDispatches(db: OrchestrationDb, link: RemoteRunLinkRow, answer: unknown): void { - const named = answer && typeof answer === 'object' ? (answer as Values) : {} + const named: Values = answer && typeof answer === 'object' ? { ...answer } : {} const dispatchId = stringField(named, 'dispatchId') const taskId = stringField(named, 'taskId') if (!dispatchId || !taskId || getRemoteDispatch(db, dispatchId)) { diff --git a/src/main/runtime/orchestration/federation/remote-owned/remote-owned-start.ts b/src/main/runtime/orchestration/federation/remote-owned/remote-owned-start.ts index febdbc38e4b..7a1bcbdc60b 100644 --- a/src/main/runtime/orchestration/federation/remote-owned/remote-owned-start.ts +++ b/src/main/runtime/orchestration/federation/remote-owned/remote-owned-start.ts @@ -38,8 +38,8 @@ export async function serverOwnsPlacedWorkers( peerFingerprint: server.peerFingerprint, expectedRuntimeEpoch: null, capability: ORCHESTRATION_REMOTE_OWNED_WORKERS_RUNTIME_CAPABILITY, - // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: status.get answers RuntimeStatus; the cache reads only its capabilities and runtime ID. probe: async () => + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: status.get answers RuntimeStatus; the cache reads only its capabilities and runtime ID. (await runtime.callOrchestrationWorkerServer( server.environmentId, 'status.get',