chore(orchestration): no type assertions in the forwarder

This commit is contained in:
Jinwoo-H
2026-11-10 15:45:30 -05:00
parent 32d592daea
commit 0c59e8de89
4 changed files with 33 additions and 19 deletions
@@ -29,10 +29,13 @@ export type ServerTerms = {
* it came from so recovery (`request-show --on`) asks there.
*/
export function inServerTerms<T>(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<T>(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<string, unknown>),
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
@@ -33,6 +33,10 @@ type Sent = {
}
type Answer = { result: unknown } | { error: string; data?: unknown } | 'unreachable'
function record(value: unknown): Record<string, unknown> {
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<string, unknown>,
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',
@@ -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)) {
@@ -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',