mirror of
https://github.com/deepseek-ai/deepseek-harness
synced 2026-08-15 21:04:50 +00:00
In-place port of dsh-workflow-vm from the in-process node:vm execution to one worker thread per run (the workflow-workerthread engine of PR #215, adopted as THE engine): the script's vm context moves inside the worker, agent() bridges to ctx.subagents over the message port (host.ts/protocol.ts/session.ts/worker.ts are new; runtime.ts loses the abandon channel — the host's grace timer force-settles and TERMINATES instead), start() pre-parses the body host-side to keep the seam's synchronous SCRIPT_PARSE throw, and a ready→go handshake keeps a run cancelled before start from ever executing the body. start() no longer blocks the host, termination is real, and the value boundary is serialization by construction. The package keeps its name until the follow-up rename commit; scripts see the identical hook surface, and the seam-contract tests hardened ahead of this swap pass unchanged. The run and child-RPC surfaces are class-shaped rather than literal bundles: WorkerRun IMPLEMENTS the seam's WorkflowRun (id/meta are its own clone, separate from event payloads') and start() returns the instance directly — interface parity with the seam is compiler-checked; worker-side, ChildRpcBridge (implements ChildPort; callId allocation + pending book-keeping settled by onChild* entry points) and RpcChildHandle (every member an RPC keyed by its callId) carry names in stacks. ChildPort's method is startAgent — it names what it starts, matching the script-side agent() hook and the agentsStarted / workflow/agent-* vocabulary; the Child* type names deliberately stay (the worker side is cordis- and subagent-free; these are reduced JSON projections, not the seam's types). Review findings from the reference PR are folded in rather than re-introduced: - cancel() drives BOTH child-cancel channels host-side: the request signal aborts AND each registered child's explicit cancel() is called — a worker wedged in a synchronous spin cannot relay its own ChildCancel RPCs (regression: cancel-only provider + wedged worker). - All host warn paths render through the total renderThrown; a child dispose() rejecting a value whose coercion throws still acks ChildDisposed instead of wedging the script's finally (regression). - built-worker.e2e.ts is wired into builtBinSmokeGate and the AGENTS.md CI sequence — the built lib/worker.js resolution contract now runs in an automated gate. - workflow/end payload pinned on the worker-death path (with the cancelled and grace-force-settle pins riding the ported spec). - Real-Worker scripted timing budgets widened (50-300ms → 150-1000ms) for starved CI hosts. Workspace plumbing: the "./worker" subpath export sanctions the second runtime bundle (check-workspace-constraints), tsdown builds two single-entry passes, tsx becomes a devDependency for the unbuilt worker spawn.
57 lines
2.5 KiB
TypeScript
57 lines
2.5 KiB
TypeScript
import { existsSync } from 'node:fs'
|
|
import { rm, writeFile } from 'node:fs/promises'
|
|
import { join } from 'node:path'
|
|
import { execFile } from 'node:child_process'
|
|
import { promisify } from 'node:util'
|
|
import { fileURLToPath } from 'node:url'
|
|
import { describe, expect, it } from 'vitest'
|
|
|
|
const packageRoot = fileURLToPath(new URL('..', import.meta.url))
|
|
const builtIndex = join(packageRoot, 'lib', 'index.js')
|
|
const builtWorker = join(packageRoot, 'lib', 'worker.js')
|
|
const run = promisify(execFile)
|
|
|
|
/**
|
|
* The BUILT-output guard for the worker entry: every other suite runs
|
|
* unbuilt (src/ + tsx), so nothing else proves that `lib/index.js` resolves
|
|
* its sibling `lib/worker.js` and that the bundle boots a worker under plain
|
|
* node (no tsx loader). Keyless — a zero-agent script needs no provider —
|
|
* and self-skips until `pnpm run build` has produced the bundles.
|
|
*/
|
|
describe.skipIf(!existsSync(builtIndex) || !existsSync(builtWorker))('built worker entry (lib/worker.js)', () => {
|
|
it('the built engine spawns its built worker under plain node and completes a run', async () => {
|
|
// ESM resolves bare specifiers from the IMPORTING FILE's location, so the
|
|
// driver must live inside the package for its node_modules to apply — a
|
|
// temp-named file at the package root, removed on the way out.
|
|
const driver = join(packageRoot, `.built-worker-driver-${process.pid}.mjs`)
|
|
try {
|
|
await writeFile(driver, `
|
|
import { Context } from 'cordis'
|
|
import SubagentService from '@deepseek-ai/dsh-subagent'
|
|
import WorkerWorkflowEngine from '@deepseek-ai/dsh-workflow-vm'
|
|
|
|
const ctx = new Context()
|
|
await ctx.plugin(SubagentService)
|
|
await ctx.plugin(WorkerWorkflowEngine, {})
|
|
const run = ctx.workflows.start({
|
|
script: "export const meta = { name: 'built-smoke', description: 'built worker smoke' }\\nreturn 6 * 7",
|
|
// A zero-agent script never touches the provider, so a bare id suffices.
|
|
parent: { id: 'built-smoke-parent', options: {} },
|
|
})
|
|
const result = await run.result
|
|
await run.dispose()
|
|
if (result.stopReason !== 'completed' || result.value !== 42) {
|
|
console.error('unexpected result: ' + JSON.stringify(result))
|
|
process.exit(1)
|
|
}
|
|
console.log('built-worker-smoke-ok')
|
|
`, 'utf8')
|
|
// Plain node — no tsx loader anywhere; the bundle must stand on its own.
|
|
const { stdout } = await run(process.execPath, [driver], { cwd: packageRoot, timeout: 60_000 })
|
|
expect(stdout).toContain('built-worker-smoke-ok')
|
|
} finally {
|
|
await rm(driver, { force: true })
|
|
}
|
|
}, 120_000)
|
|
})
|