# Conflicts: # docs/AGENTS.md # docs/config-catalog.md # packages/bash/bash-sandbox/src/index.ts # packages/bash/bash/src/session-mode.ts # packages/bash/tool-bash/README.md # packages/code-runtime/code-runtime-worker/README.md # packages/compact/compact/src/index.ts # packages/core/agent-core/README.md # packages/hooks/hooks-claude/src/config.ts # packages/hooks/hooks-claude/src/index.ts # packages/hooks/hooks-codex/src/config.ts # packages/hooks/hooks-codex/src/index.ts # packages/llm/llm/README.md # packages/session-persistence/session-persistence-jsonl/README.md # packages/session-persistence/session-persistence/README.md # packages/skill/skill-local/README.md # packages/support/acp-snapshot/README.md # packages/support/invariants/src/index.ts # packages/ui/acp/README.md # packages/ui/jsonrpc-agent/README.md # packages/ui/jsonrpc/README.md # packages/ui/permission/README.md # packages/ui/user-approval/README.md # packages/ui/user-interaction/README.md # packages/web/web-search-deepseek/README.md
5.3 KiB
@deepseek-ai/dsh-code-runtime-worker
Worker-thread implementation of the @deepseek-ai/dsh-code-runtime seam: WorkerCodeRuntime runs each program in ONE fresh Node worker_threads.Worker — TypeScript in, type-stripped host-side, bindings bridged over the message port, { value, logs, error? } out. Containment, not a security boundary: trust posture is bash-equivalent by design (the Code Mode RFC § Trust posture), with containment bash does not have — separate isolate, empty environment, heap cap, hard termination.
Config
- id: code-runtime
name: '@deepseek-ai/dsh-code-runtime-worker'
config:
computeMs: 60000 # busy-time budget (measured event-loop active time)
maxWallMs: 600000 # wall-clock ceiling; never pauses for anything
maxLogBytes: 65536 # shared byte budget for captured log text
maxValueBytes: 32768 # rendered-completion-value cap
maxOldGenerationSizeMb: 512 # worker heap cap (resourceLimits)
Every field is validated (positive numbers) and defaulted; there are no other tunables.
Design
- One fresh worker per run, no pooling — a program's world dies with its worker: no cross-run state to log, state bleed unrepresentable, runs reconstructable from the session log alone.
- Type-strip host-side, in execution context — the program is wrapped in an async-function shell, stripped with
node:module'sstripTypeScriptTypes(erasable syntax only —enum/namespaces are rejected as a programexceptionand no worker spawns), and sliced back out byte-positioned; it then executes as the body of anAsyncFunction, so top-levelawait/returnwork. - The port assumes a hostile peer — model code can reach
parentPortand forge traffic, so every inbound message is shape-validated and REBUILT before anything reads it (null, primitives, junk types, and malformed payloads drop without a throw; forged extra fields never ride along), the host answers each call id at most once, resolves binding names as OWN properties only (a forgedconstructorcannot walk a prototype chain), drops post-settlement replies, and converts a non-cloneable binding resolution into an error reply. Forgedlog/donemessages cannot bypass the caps: one host-side ledger bounds everything that lands inlogs, and the completion value is re-capped host-side. Worker-side namespaces are null-prototype withdefineProperty, so__proto__-shaped binding names are ordinary keys. - Two independent budgets, because the peer is hostile —
computeMsmeters the worker's MEASURED busy time (worker.performance.eventLoopUtilization()polling): a hot loop cannot hide behind a pending decoy dispatch, and a program awaiting a slow tool accrues nothing.maxWallMsbackstops what busy time cannot see (awaiting a promise nobody resolves). Both funnel intoworker.terminate(), which ends hot synchronous loops too; heap overflow surfaces as the worker's OOM exit (kind: 'worker-exit'). - Logs stream eagerly — console/stdout/stderr entries cross the port as they happen, so a timed-out or killed program still shows what it printed. ONE shared
maxLogBytesledger bounds everything: streamed entries, forged port traffic, and pipe bytes that bypass the patched streams (appended after), with the overflow marked in-band once. - Empty environment — the worker gets
env: {}andexecArgv: []: no ambient credentials (stronger than the scrubbed-env rule for spawned commands) and no inherited loader flags. - Dispose to quiescence — teardown fails in-flight runs as
abortand AWAITS each worker's exit before resolving.
The worker entry, unbuilt and built
Source mode loads erasable-only src/worker.ts through Node's native type stripping. Built mode passes the sibling lib/worker.cjs as a filesystem path because pkg's VFS Worker hook expects CommonJS; the same path works under ordinary Node. tests/built-lib.e2e.ts pins the real load path required by docs/testing.md.
Model Experience
Indirectly, through Code Mode in dsh-tools, which renders this worker's capped printed or returned data and exact [dsh-code-runtime-worker] log capture truncated at <maxLogBytes> bytes and … [truncated] markers into a retained run_code result. Binding traffic and worker internals stay outside context.
Known Limitations and Deferred Work
- OS processes a program spawns survive termination —
worker.terminate()ends the thread only, weaker than bash-local's process-group kill; orphan cleanup is a deployment concern until a container backend exists. - Type-strip rides Node's experimental
stripTypeScriptTypesAPI — the relied-on behavior is pinned by unit tests, with amaro/sucrase as named drop-in replacements if it shifts. computeMsexpiry can overshoot by up to one poll interval — busy time is sampled every 25 ms (an internal constant, deliberately not config).- Programs get a five-method
consoleshim (log/info/warn/error/debug) — deliberately not Node's full console surface. - A non-cloneable or oversize completion value does not cross as a value — it arrives as a bounded, truncation-marked
util.inspectrendering invalue's place.