fix(code-runtime): flatten worker JSON transport

This commit is contained in:
Tianyi Cui
2026-07-22 19:09:27 +08:00
parent d6f478488d
commit ef2de530ff
13 changed files with 352 additions and 58 deletions
@@ -8,7 +8,7 @@
import { inspect } from 'node:util'
import type { DoneMessage, ReplyMessage, WorkerBootData, WorkerToHost } from './protocol.ts'
import { jsonValueBytesUpTo } from './output-json.ts'
import { snapshotCodeJsonValue } from './worker-json.ts'
import { decodeWorkerJson, encodeWorkerJson, snapshotCodeJsonValue } from './worker-json.ts'
/** The port surface the bootstrap needs — satisfied by `parentPort` and by the tests' fake. */
export interface BootstrapPort {
@@ -152,7 +152,7 @@ export function truncateUtf8Bytes(text: string, maxBytes: number): string {
*
* @param value - the program's completion value.
* @param maxOutputBytes - the byte cap for the outer result.
* @returns the done-message fragment: `{}` for `undefined`, else `{ value }`.
* @returns the done-message fragment: `{}` for `undefined`, else a flat wire `{ value }`.
*/
export function prepareCompletion(value: unknown, maxOutputBytes: number): Omit<DoneMessage, 'type'> {
if (value === undefined) return {}
@@ -168,7 +168,7 @@ export function prepareCompletion(value: unknown, maxOutputBytes: number): Omit<
if (jsonValueBytesUpTo(snapshot, maxOutputBytes) === undefined) {
return { error: { kind: 'output-limit', message: `outer output exceeded ${maxOutputBytes} bytes` } }
}
return { value: snapshot }
return { value: encodeWorkerJson(snapshot) }
}
/** One awaited binding call's settlement handles, keyed by call id in the pending map. */
@@ -208,8 +208,13 @@ export function wireReplies(port: BootstrapPort, pending: Map<number, PendingCal
const entry = pending.get(message.id)
if (!entry) return
pending.delete(message.id)
if (message.ok) entry.resolve(message.value)
else entry.reject(new Error(message.message))
if (message.ok) {
const value = decodeWorkerJson(message.value)
if (value === undefined) entry.reject(new Error('binding resolution must be lossless JSON'))
else entry.resolve(value)
} else {
entry.reject(new Error(message.message))
}
})
}
@@ -238,7 +243,7 @@ export function makeNamespaces(
Object.defineProperty(namespace, name, {
enumerable: true,
value: (args: unknown): Promise<unknown> => {
let detached: unknown
let detached: ReturnType<typeof snapshotCodeJsonValue>
try {
detached = snapshotCodeJsonValue(args)
} catch {
@@ -254,7 +259,7 @@ export function makeNamespaces(
},
})
try {
port.postMessage({ type: 'call', id, global, name, args: detached })
port.postMessage({ type: 'call', id, global, name, args: encodeWorkerJson(detached) })
} catch (error: unknown) {
pending.delete(id)
const message = `binding arguments must be structured-cloneable: ${error instanceof Error ? error.message : String(error)}`
@@ -17,6 +17,8 @@ import type { CodeBindingFunction, CodeJsonValue, CodeRunFailure, CodeRunRequest
import { snapshotJsonValue } from '@deepseek-ai/dsh-session'
import type { ReplyMessage, WorkerBootData, WorkerToHost } from './protocol.ts'
import { jsonStringBytesUpTo, jsonValueBytesUpTo, truncateJsonStringBytes } from './output-json.ts'
import { decodeWorkerJson, encodeWorkerJson } from './worker-json.ts'
import type { WorkerJsonWire } from './worker-json.ts'
/** Plugin config: every execution cap, changeable from `cordis.yml` (no hardcoded tunables). */
export interface Config {
@@ -142,7 +144,7 @@ function parseWorkerMessage(raw: unknown): WorkerToHost | undefined {
switch (m.type) {
case 'call': {
if (typeof m.id !== 'number' || typeof m.global !== 'string' || typeof m.name !== 'string') return undefined
return { type: 'call', id: m.id, global: m.global, name: m.name, args: m.args }
return { type: 'call', id: m.id, global: m.global, name: m.name, args: m.args as WorkerJsonWire }
}
case 'log': {
if (typeof m.text !== 'string') return undefined
@@ -150,7 +152,7 @@ function parseWorkerMessage(raw: unknown): WorkerToHost | undefined {
}
case 'output-limit': return { type: 'output-limit' }
case 'done': {
if (m.error === undefined) return { type: 'done', ...m.value !== undefined ? { value: m.value } : {} }
if (m.error === undefined) return { type: 'done', ...m.value !== undefined ? { value: m.value as WorkerJsonWire } : {} }
const error = m.error
if (typeof error !== 'object' || error === null) return undefined
const { kind, message } = error as Record<string, unknown>
@@ -409,10 +411,7 @@ export class WorkerCodeRuntime extends CodeRuntime {
finish(() => output.success([...logs, ...strayLogs]))
return
}
// The worker-thread boundary has already structured-cloned this
// hostile value, so accessors and proxies cannot survive to throw
// during the lossless-JSON snapshot.
const value = snapshotJsonValue(message.value) as CodeJsonValue | undefined
const value = decodeWorkerJson(message.value)
if (value === undefined) {
finish(() => output.failure([...logs, ...strayLogs], { kind: 'invalid-output', message: 'program completion must be lossless JSON' }))
} else {
@@ -442,9 +441,7 @@ export class WorkerCodeRuntime extends CodeRuntime {
reply({ type: 'reply', id: message.id, ok: false, message: `unknown binding ${JSON.stringify(`${message.global}.${message.name}`)}` })
return
}
// Structured clone has already removed accessors and proxies, so the
// host can repeat the lossless snapshot without a reflective throw.
const args = snapshotJsonValue(message.args) as CodeJsonValue | undefined
const args = decodeWorkerJson(message.args)
if (args === undefined) {
reply({ type: 'reply', id: message.id, ok: false, message: 'binding arguments must be lossless JSON' })
return
@@ -461,7 +458,7 @@ export class WorkerCodeRuntime extends CodeRuntime {
if (value === undefined) {
reply({ type: 'reply', id: message.id, ok: false, message: 'binding resolution must be lossless JSON' })
} else {
reply({ type: 'reply', id: message.id, ok: true, value })
reply({ type: 'reply', id: message.id, ok: true, value: encodeWorkerJson(value) })
}
} catch (error: unknown) {
reply({ type: 'reply', id: message.id, ok: false, message: messageOf(error) })
@@ -5,6 +5,8 @@
* @module @deepseek-ai/dsh-code-runtime-worker/src/protocol
*/
import type { WorkerJsonWire } from './worker-json.ts'
/** What the host hands the worker at spawn, via `workerData`. */
export interface WorkerBootData {
/** The type-stripped (plain JS) program body. */
@@ -24,8 +26,8 @@ interface CallMessage {
global: string
/** The function name within the namespace. */
name: string
/** The single argument, structured-clone-plain. */
args: unknown
/** The single argument as a flat lossless-JSON wire value. */
args: WorkerJsonWire
}
/** Worker → host: captured text, streamed eagerly so output survives a mid-run termination (timeout, abort, OOM). */
@@ -43,13 +45,13 @@ interface OutputLimitMessage {
* Worker → host: the program settled. `error` carries a program exception
* (the only failure the bootstrap itself can report — budgets, aborts, and
* substrate death are observed host-side). `value` is present only on a
* clean completion that produced one (already size-capped and
* clone-safe per the bootstrap's value preparation). Logs are NOT carried
* here — they streamed eagerly as {@link LogMessage}s.
* clean completion that produced one, as a flat wire value already
* size-capped and lossless per the bootstrap. Logs are NOT carried here —
* they streamed eagerly as {@link LogMessage}s.
*/
export interface DoneMessage {
type: 'done'
value?: unknown
value?: WorkerJsonWire
error?: { kind: 'exception' | 'invalid-output' | 'output-limit'; message: string }
}
@@ -58,5 +60,5 @@ export type WorkerToHost = CallMessage | LogMessage | OutputLimitMessage | DoneM
/** Host → worker: the answer to one {@link CallMessage}. */
export type ReplyMessage =
| { type: 'reply'; id: number; ok: true; value: unknown }
| { type: 'reply'; id: number; ok: true; value: WorkerJsonWire }
| { type: 'reply'; id: number; ok: false; message: string }
@@ -150,4 +150,185 @@ export function snapshotCodeJsonValue(value: unknown): CodeJsonValue | undefined
}
return root
}
interface ArrayWireToken {
kind: 'array'
length: number
}
interface ObjectWireToken {
kind: 'object'
keys: string[]
}
type WorkerJsonToken = null | boolean | number | string | ArrayWireToken | ObjectWireToken
/**
* A pre-order, bounded-depth transport for one lossless JSON value. Container
* markers and scalar leaves share one flat token array, so `worker_threads`
* never has to structured-clone the value's application nesting.
*/
export type WorkerJsonWire = WorkerJsonToken[]
/**
* Flatten one validated JSON value for the worker-thread message port.
* @param value - the lossless JSON value to transport.
* @returns a pre-order token stream whose own nesting is bounded.
*/
export function encodeWorkerJson(value: CodeJsonValue): WorkerJsonWire {
const wire: WorkerJsonWire = []
const pending: CodeJsonValue[] = [value]
for (let current = pending.pop(); current !== undefined; current = pending.pop()) {
if (current === null || typeof current === 'boolean' || typeof current === 'number' || typeof current === 'string') {
wire.push(current)
continue
}
if (Array.isArray(current)) {
wire.push({ kind: 'array', length: current.length })
for (let index = current.length - 1; index >= 0; index--) {
const item = current[index]
if (item === undefined) throw new Error('cannot encode a sparse JSON array')
pending.push(item)
}
continue
}
const keys = Object.keys(current)
wire.push({ kind: 'object', keys })
for (let index = keys.length - 1; index >= 0; index--) {
const key = keys[index]
/* v8 ignore next -- the loop is bounded by the captured key count. */
if (key === undefined) throw new Error('cannot encode a missing JSON object key')
const item = current[key]
if (item === undefined) throw new Error('cannot encode an undefined JSON object property')
pending.push(item)
}
}
return wire
}
type DecodeFrame =
| { kind: 'array'; target: CodeJsonValue[]; length: number; index: number }
| { kind: 'object'; target: Record<string, CodeJsonValue>; keys: string[]; index: number }
/** Whether an array contains exactly its dense indexed slots and `length`. */
function isDenseArray(value: unknown[]): boolean {
if (!hasPlainArrayPrototype(value) || Reflect.ownKeys(value).length !== value.length + 1) return false
for (let index = 0; index < value.length; index++) {
if (!Object.hasOwn(value, index)) return false
}
return true
}
/** Return one exact container marker, or reject any extra/missing fields. */
function containerToken(value: object): ArrayWireToken | ObjectWireToken | undefined {
if (Array.isArray(value) || !hasPlainObjectPrototype(value)) return undefined
const keys = enumerableStringKeys(value)
if (keys === undefined) return undefined
const token = value as Record<string, unknown>
if (token.kind === 'array') {
if (keys.length !== 2 || !keys.includes('kind') || !keys.includes('length')) return undefined
const length = token.length
return typeof length === 'number' && Number.isSafeInteger(length) && length >= 0
? { kind: 'array', length }
: undefined
}
if (token.kind === 'object') {
if (keys.length !== 2 || !keys.includes('kind') || !keys.includes('keys')) return undefined
const objectKeys = token.keys
if (!Array.isArray(objectKeys) || !isDenseArray(objectKeys)) return undefined
const unique = new Set<string>()
const normalizedKeys: string[] = []
for (const key of objectKeys as unknown[]) {
if (typeof key !== 'string' || unique.has(key)) return undefined
unique.add(key)
normalizedKeys.push(key)
}
return { kind: 'object', keys: normalizedKeys }
}
return undefined
}
/**
* Rebuild one lossless JSON value from the flat worker-thread wire format.
* Malformed or incomplete traffic returns `undefined`; traversal is iterative
* and therefore independent of the transported value's application depth.
* @param input - untrusted message-port payload.
* @returns the detached JSON value, or `undefined` when the wire is invalid.
*/
export function decodeWorkerJson(input: unknown): CodeJsonValue | undefined {
try {
if (!Array.isArray(input) || !isDenseArray(input) || input.length === 0) return undefined
const wire = input as unknown[]
const frames: DecodeFrame[] = []
let root: CodeJsonValue | undefined
let rootAssigned = false
const attach = (value: CodeJsonValue): boolean => {
const parent = frames.at(-1)
if (!parent) {
if (rootAssigned) return false
root = value
rootAssigned = true
return true
}
/* v8 ignore next -- completed frames are popped before another token can attach. */
if (parent.index >= (parent.kind === 'array' ? parent.length : parent.keys.length)) return false
if (parent.kind === 'array') {
parent.target.push(value)
} else {
const key = parent.keys[parent.index]
/* v8 ignore next -- object frames are built from validated keys and their exact length. */
if (key === undefined) return false
Object.defineProperty(parent.target, key, {
value,
enumerable: true,
configurable: true,
writable: true,
})
}
parent.index += 1
return true
}
for (let tokenIndex = 0; tokenIndex < wire.length; tokenIndex++) {
const token = wire[tokenIndex]
let value: CodeJsonValue
let frame: DecodeFrame | undefined
if (token === null || typeof token === 'boolean' || typeof token === 'string') {
value = token
} else if (typeof token === 'number') {
if (!Number.isFinite(token) || Object.is(token, -0)) return undefined
value = token
} else {
if (typeof token !== 'object') return undefined
const marker = containerToken(token)
if (!marker) return undefined
const remainingTokens = wire.length - tokenIndex - 1
if (marker.kind === 'array') {
if (marker.length > remainingTokens) return undefined
const target: CodeJsonValue[] = []
value = target
if (marker.length > 0) frame = { kind: 'array', target, length: marker.length, index: 0 }
} else {
if (marker.keys.length > remainingTokens) return undefined
const target: Record<string, CodeJsonValue> = {}
value = target
if (marker.keys.length > 0) frame = { kind: 'object', target, keys: marker.keys, index: 0 }
}
}
if (!attach(value)) return undefined
if (frame) frames.push(frame)
while (frames.length > 0) {
const current = frames.at(-1)
/* v8 ignore next -- the loop condition guarantees a final frame. */
if (current === undefined) break
if (current.index < (current.kind === 'array' ? current.length : current.keys.length)) break
frames.pop()
}
}
return frames.length === 0 ? root : undefined
} catch {
return undefined
}
}
/* jscpd:ignore-end */