Merge branch 'codex/code-mode-typed-results' into codex/code-mode-complete-result-card
This commit is contained in:
@@ -2,6 +2,31 @@
|
|||||||
|
|
||||||
import type { CodeJsonValue } from '@deepseek-ai/dsh-code-runtime'
|
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
|
* Validate and detach one worker-boundary value without loading another
|
||||||
* workspace package at runtime. This mirrors the session-owned canonical
|
* 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 (typeof candidate !== 'object') return undefined
|
||||||
|
|
||||||
if (Array.isArray(candidate)) {
|
if (Array.isArray(candidate)) {
|
||||||
if (Object.getPrototypeOf(candidate) !== Array.prototype) return undefined
|
if (!hasPlainArrayPrototype(candidate)) return undefined
|
||||||
if (Reflect.ownKeys(candidate).length !== candidate.length + 1) return undefined
|
const length = candidate.length
|
||||||
|
if (Reflect.ownKeys(candidate).length !== length + 1) return undefined
|
||||||
return within(candidate, () => {
|
return within(candidate, () => {
|
||||||
const result: CodeJsonValue[] = []
|
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
|
if (!Object.hasOwn(candidate, index)) return undefined
|
||||||
const item = copy(candidate[index])
|
const item = copy(candidate[index])
|
||||||
if (item === undefined) return undefined
|
if (item === undefined) return undefined
|
||||||
@@ -46,11 +72,12 @@ export function snapshotCodeJsonValue(value: unknown): CodeJsonValue | undefined
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const prototype = Object.getPrototypeOf(candidate) as unknown
|
if (!hasPlainObjectPrototype(candidate)) return undefined
|
||||||
if (prototype !== Object.prototype && prototype !== null) return undefined
|
const keys = enumerableStringKeys(candidate)
|
||||||
|
if (keys === undefined) return undefined
|
||||||
return within(candidate, () => {
|
return within(candidate, () => {
|
||||||
const result: Record<string, CodeJsonValue> = {}
|
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])
|
const item = copy((candidate as Record<string, unknown>)[key])
|
||||||
if (item === undefined) return undefined
|
if (item === undefined) return undefined
|
||||||
Object.defineProperty(result, key, {
|
Object.defineProperty(result, key, {
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import { runInNewContext } from 'node:vm'
|
||||||
import { describe, expect, it } from 'vitest'
|
import { describe, expect, it } from 'vitest'
|
||||||
import { snapshotJsonValue } from '@deepseek-ai/dsh-session'
|
import { snapshotJsonValue } from '@deepseek-ai/dsh-session'
|
||||||
import { snapshotCodeJsonValue } from '../src/worker-json.ts'
|
import { snapshotCodeJsonValue } from '../src/worker-json.ts'
|
||||||
@@ -24,6 +25,16 @@ describe('snapshotCodeJsonValue', () => {
|
|||||||
expect(snapshot.alias).not.toBe(shared)
|
expect(snapshot.alias).not.toBe(shared)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('accepts intrinsic plain containers from another JavaScript realm', () => {
|
||||||
|
const foreign = runInNewContext('({ object: { nested: [1] }, array: [2, { ok: true }] })') as {
|
||||||
|
object: unknown
|
||||||
|
array: unknown
|
||||||
|
}
|
||||||
|
|
||||||
|
expect(snapshotCodeJsonValue(foreign.object)).toEqual({ nested: [1] })
|
||||||
|
expect(snapshotCodeJsonValue(foreign.array)).toEqual([2, { ok: true }])
|
||||||
|
})
|
||||||
|
|
||||||
it('reads each accepted slot once and preserves a literal __proto__ key', () => {
|
it('reads each accepted slot once and preserves a literal __proto__ key', () => {
|
||||||
let objectReads = 0
|
let objectReads = 0
|
||||||
let arrayReads = 0
|
let arrayReads = 0
|
||||||
@@ -66,6 +77,12 @@ describe('snapshotCodeJsonValue', () => {
|
|||||||
Object.defineProperty(compensatedSparse, 'extra', { value: true })
|
Object.defineProperty(compensatedSparse, 'extra', { value: true })
|
||||||
const symbolDecorated = [1]
|
const symbolDecorated = [1]
|
||||||
Object.defineProperty(symbolDecorated, Symbol('extra'), { value: true })
|
Object.defineProperty(symbolDecorated, Symbol('extra'), { value: true })
|
||||||
|
const hiddenObject = Object.defineProperty({}, 'hidden', { value: true })
|
||||||
|
const symbolObject = { [Symbol('extra')]: true }
|
||||||
|
const forgedPrototype: unknown[] = []
|
||||||
|
Object.setPrototypeOf(forgedPrototype, null)
|
||||||
|
const forgedArray = [1]
|
||||||
|
Object.setPrototypeOf(forgedArray, forgedPrototype)
|
||||||
|
|
||||||
for (const value of [
|
for (const value of [
|
||||||
new ExoticObject(),
|
new ExoticObject(),
|
||||||
@@ -75,6 +92,9 @@ describe('snapshotCodeJsonValue', () => {
|
|||||||
decorated,
|
decorated,
|
||||||
compensatedSparse,
|
compensatedSparse,
|
||||||
symbolDecorated,
|
symbolDecorated,
|
||||||
|
hiddenObject,
|
||||||
|
symbolObject,
|
||||||
|
forgedArray,
|
||||||
cyclic,
|
cyclic,
|
||||||
[undefined],
|
[undefined],
|
||||||
{ value: undefined },
|
{ value: undefined },
|
||||||
@@ -83,6 +103,19 @@ describe('snapshotCodeJsonValue', () => {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('rejects an array whose getter mutates the validated length', () => {
|
||||||
|
const array = [0, 2]
|
||||||
|
Object.defineProperty(array, 0, {
|
||||||
|
enumerable: true,
|
||||||
|
get: () => {
|
||||||
|
array.length = 1
|
||||||
|
return 1
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(snapshotCodeJsonValue(array)).toBeUndefined()
|
||||||
|
})
|
||||||
|
|
||||||
it('propagates a throwing getter and releases its recursion guard', () => {
|
it('propagates a throwing getter and releases its recursion guard', () => {
|
||||||
const failure = new Error('getter failed')
|
const failure = new Error('getter failed')
|
||||||
const source = Object.defineProperty({}, 'value', {
|
const source = Object.defineProperty({}, 'value', {
|
||||||
|
|||||||
Reference in New Issue
Block a user