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:
@@ -0,0 +1,36 @@
|
||||
import { copyFile, mkdtemp, rm } from 'node:fs/promises'
|
||||
import { tmpdir } from 'node:os'
|
||||
import { join } from 'node:path'
|
||||
import { Worker } from 'node:worker_threads'
|
||||
import { expect, it } from 'vitest'
|
||||
|
||||
/**
|
||||
* Prove the unbuilt worker is a self-contained source closure. Copying it out
|
||||
* of the workspace makes any package runtime import fail even when local
|
||||
* `lib/` artifacts happen to exist.
|
||||
*/
|
||||
it('boots the source worker without workspace package outputs', async () => {
|
||||
const directory = await mkdtemp(join(tmpdir(), 'dsh-code-source-worker-'))
|
||||
let worker: Worker | undefined
|
||||
try {
|
||||
const files = ['worker.ts', 'bootstrap.ts', 'protocol.ts', 'worker-json.ts']
|
||||
await Promise.all(files.map(async (file) => {
|
||||
await copyFile(new URL(`../src/${file}`, import.meta.url), join(directory, file))
|
||||
}))
|
||||
|
||||
worker = new Worker(join(directory, 'worker.ts'), {
|
||||
workerData: { code: 'return { answer: 42 }', namespaces: [], maxOutputBytes: 65_536 },
|
||||
env: {},
|
||||
execArgv: [],
|
||||
})
|
||||
const message = await new Promise<unknown>((resolve, reject) => {
|
||||
worker?.once('message', resolve)
|
||||
worker?.once('error', reject)
|
||||
})
|
||||
|
||||
expect(message).toEqual({ type: 'done', value: { answer: 42 } })
|
||||
} finally {
|
||||
if (worker) await worker.terminate()
|
||||
await rm(directory, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
@@ -0,0 +1,87 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { snapshotJsonValue } from '@deepseek-ai/dsh-session'
|
||||
import { snapshotCodeJsonValue } from '../src/worker-json.ts'
|
||||
|
||||
describe('snapshotCodeJsonValue', () => {
|
||||
it('matches the canonical scalar boundary', () => {
|
||||
const unsupported = [undefined, 1n, Symbol('value'), () => 1]
|
||||
for (const value of [null, false, 'text', 1.25, -0, Number.NaN, Number.POSITIVE_INFINITY, ...unsupported]) {
|
||||
expect(snapshotCodeJsonValue(value)).toEqual(snapshotJsonValue(value))
|
||||
}
|
||||
})
|
||||
|
||||
it('detaches dense arrays and plain or null-prototype records', () => {
|
||||
const shared = { value: 1 }
|
||||
const nullPrototype = Object.assign(Object.create(null) as Record<string, unknown>, { shared })
|
||||
const source = { list: [nullPrototype, shared], alias: shared }
|
||||
|
||||
const snapshot = snapshotCodeJsonValue(source) as Record<string, unknown>
|
||||
shared.value = 2
|
||||
|
||||
expect(snapshot).toEqual({ list: [{ shared: { value: 1 } }, { value: 1 }], alias: { value: 1 } })
|
||||
expect(snapshot).not.toBe(source)
|
||||
expect((snapshot.list as unknown[])[0]).not.toBe(nullPrototype)
|
||||
expect(snapshot.alias).not.toBe(shared)
|
||||
})
|
||||
|
||||
it('reads each accepted slot once and preserves a literal __proto__ key', () => {
|
||||
let objectReads = 0
|
||||
let arrayReads = 0
|
||||
const source = Object.create(null) as Record<string, unknown>
|
||||
Object.defineProperty(source, '__proto__', {
|
||||
enumerable: true,
|
||||
get: () => {
|
||||
objectReads += 1
|
||||
return { safe: true }
|
||||
},
|
||||
})
|
||||
const array = new Array<unknown>(1)
|
||||
Object.defineProperty(array, 0, {
|
||||
enumerable: true,
|
||||
get: () => {
|
||||
arrayReads += 1
|
||||
return arrayReads === 1 ? source : undefined
|
||||
},
|
||||
})
|
||||
|
||||
const snapshot = snapshotCodeJsonValue(array) as Record<string, unknown>[]
|
||||
|
||||
expect(objectReads).toBe(1)
|
||||
expect(arrayReads).toBe(1)
|
||||
expect(Object.getPrototypeOf(snapshot[0])).toBe(Object.prototype)
|
||||
expect(Object.hasOwn(snapshot[0]!, '__proto__')).toBe(true)
|
||||
expect(snapshot[0]?.['__proto__']).toEqual({ safe: true })
|
||||
})
|
||||
|
||||
it('rejects exotic containers, sparse arrays, cycles, and invalid children', () => {
|
||||
class ExoticObject {
|
||||
readonly value = 1
|
||||
}
|
||||
class ExoticArray extends Array<number> {}
|
||||
const cyclic: Record<string, unknown> = {}
|
||||
cyclic.self = cyclic
|
||||
|
||||
for (const value of [
|
||||
new ExoticObject(),
|
||||
new Map([['value', 1]]),
|
||||
new ExoticArray(1),
|
||||
new Array(1),
|
||||
cyclic,
|
||||
[undefined],
|
||||
{ value: undefined },
|
||||
]) {
|
||||
expect(snapshotCodeJsonValue(value)).toBeUndefined()
|
||||
}
|
||||
})
|
||||
|
||||
it('propagates a throwing getter and releases its recursion guard', () => {
|
||||
const failure = new Error('getter failed')
|
||||
const source = Object.defineProperty({}, 'value', {
|
||||
enumerable: true,
|
||||
get: () => { throw failure },
|
||||
})
|
||||
|
||||
expect(() => snapshotCodeJsonValue(source)).toThrow(failure)
|
||||
expect(snapshotCodeJsonValue({ after: true })).toEqual({ after: true })
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user