fix(code-runtime): align worker JSON snapshots

This commit is contained in:
Tianyi Cui
2026-07-21 22:29:55 +08:00
parent 4d7d5d4527
commit 8fd1201cfa
2 changed files with 66 additions and 6 deletions
@@ -2,6 +2,31 @@
import type { CodeJsonValue } from '@deepseek-ai/dsh-code-runtime'
/* jscpd:ignore-start -- the source worker mirrors session JSON helpers without workspace runtime imports */
/** Whether an array uses one realm's intrinsic `Array.prototype`, not a subclass or forged prototype. */
function hasPlainArrayPrototype(value: unknown[]): boolean {
const prototype: unknown = Object.getPrototypeOf(value)
if (!Array.isArray(prototype)) return false
const objectPrototype: unknown = Object.getPrototypeOf(prototype)
return objectPrototype !== null
&& !Array.isArray(objectPrototype)
&& Object.getPrototypeOf(objectPrototype) === null
}
/** Whether an object is a plain or null-prototype record from any JavaScript realm. */
function hasPlainObjectPrototype(value: object): boolean {
const prototype: unknown = Object.getPrototypeOf(value)
return prototype === null || Object.getPrototypeOf(prototype) === null
}
/** Return every JSON-visible object key, or reject own data JSON would discard. */
function enumerableStringKeys(value: object): string[] | undefined {
const keys = Reflect.ownKeys(value)
if (keys.some(key => typeof key !== 'string' || !Object.prototype.propertyIsEnumerable.call(value, key))) return undefined
return keys as string[]
}
/* jscpd:ignore-end */
/**
* Validate and detach one worker-boundary value without loading another
* workspace package at runtime. This mirrors the session-owned canonical
@@ -32,11 +57,12 @@ export function snapshotCodeJsonValue(value: unknown): CodeJsonValue | undefined
if (typeof candidate !== 'object') return undefined
if (Array.isArray(candidate)) {
if (Object.getPrototypeOf(candidate) !== Array.prototype) return undefined
if (Reflect.ownKeys(candidate).length !== candidate.length + 1) return undefined
if (!hasPlainArrayPrototype(candidate)) return undefined
const length = candidate.length
if (Reflect.ownKeys(candidate).length !== length + 1) return undefined
return within(candidate, () => {
const result: CodeJsonValue[] = []
for (let index = 0; index < candidate.length; index++) {
for (let index = 0; index < length; index++) {
if (!Object.hasOwn(candidate, index)) return undefined
const item = copy(candidate[index])
if (item === undefined) return undefined
@@ -46,11 +72,12 @@ export function snapshotCodeJsonValue(value: unknown): CodeJsonValue | undefined
})
}
const prototype = Object.getPrototypeOf(candidate) as unknown
if (prototype !== Object.prototype && prototype !== null) return undefined
if (!hasPlainObjectPrototype(candidate)) return undefined
const keys = enumerableStringKeys(candidate)
if (keys === undefined) return undefined
return within(candidate, () => {
const result: Record<string, CodeJsonValue> = {}
for (const key of Object.keys(candidate)) {
for (const key of keys) {
const item = copy((candidate as Record<string, unknown>)[key])
if (item === undefined) return undefined
Object.defineProperty(result, key, {