fix: keep source Code Mode worker self-contained

A direct runtime import of the session package made the unbuilt worker depend on sibling lib output. Use a parity-tested local JSON snapshotter and pin the isolated source closure with a real-worker test.
This commit is contained in:
Tianyi Cui
2026-07-21 05:19:40 +08:00
parent aca8162ef2
commit 1709b8cfee
5 changed files with 193 additions and 3 deletions

View File

@@ -6,8 +6,8 @@
*/
import { inspect } from 'node:util'
import { snapshotJsonValue } from '@deepseek-ai/dsh-session'
import type { DoneMessage, ReplyMessage, WorkerBootData, WorkerToHost } from './protocol.ts'
import { snapshotCodeJsonValue } from './worker-json.ts'
/** The port surface the bootstrap needs — satisfied by `parentPort` and by the tests' fake. */
export interface BootstrapPort {
@@ -157,7 +157,7 @@ export function prepareCompletion(value: unknown, maxOutputBytes: number): Omit<
if (value === undefined) return {}
let snapshot: unknown
try {
snapshot = snapshotJsonValue(value)
snapshot = snapshotCodeJsonValue(value)
} catch {
snapshot = undefined
}

View File

@@ -0,0 +1,67 @@
/** Lossless-JSON snapshots for the dependency-free source worker closure. @module @deepseek-ai/dsh-code-runtime-worker/worker-json */
import type { CodeJsonValue } from '@deepseek-ai/dsh-code-runtime'
/**
* Validate and detach one worker-boundary value without loading another
* workspace package at runtime. This mirrors the session-owned canonical
* JSON boundary while remaining safe to import from the unbuilt worker.
*
* @param value - the candidate completion value.
* @returns a detached lossless-JSON snapshot, or `undefined` when invalid.
*/
export function snapshotCodeJsonValue(value: unknown): CodeJsonValue | undefined {
const active = new Set<object>()
const within = <T extends CodeJsonValue>(source: object, build: () => T | undefined): T | undefined => {
if (active.has(source)) return undefined
active.add(source)
try {
return build()
} finally {
active.delete(source)
}
}
const copy = (candidate: unknown): CodeJsonValue | undefined => {
if (candidate === null) return null
if (typeof candidate === 'boolean' || typeof candidate === 'string') return candidate
if (typeof candidate === 'number') {
return Number.isFinite(candidate) && !Object.is(candidate, -0) ? candidate : undefined
}
if (typeof candidate !== 'object') return undefined
if (Array.isArray(candidate)) {
if (Object.getPrototypeOf(candidate) !== Array.prototype) return undefined
return within(candidate, () => {
const result: CodeJsonValue[] = []
for (let index = 0; index < candidate.length; index++) {
if (!Object.hasOwn(candidate, index)) return undefined
const item = copy(candidate[index])
if (item === undefined) return undefined
result.push(item)
}
return result
})
}
const prototype = Object.getPrototypeOf(candidate) as unknown
if (prototype !== Object.prototype && prototype !== null) return undefined
return within(candidate, () => {
const result: Record<string, CodeJsonValue> = {}
for (const key of Object.keys(candidate)) {
const item = copy((candidate as Record<string, unknown>)[key])
if (item === undefined) return undefined
Object.defineProperty(result, key, {
value: item,
enumerable: true,
configurable: true,
writable: true,
})
}
return result
})
}
return copy(value)
}