forked from cflmy/deepseek-harness
feat(code-runtime): complete sandboxed Node execution and Session fixtures
This commit is contained in:
1 parent
f6904f4615
commit
8e19ec4962
82 files changed
+2580
-348
No files matched your search
@@ -2,5 +2,5 @@
|
||||
# side as of the last confirmed-consistent state. Both languages carry equal authority;
|
||||
# after editing either side, bring the other along and re-record with:
|
||||
# pnpm run verify-translation-pairing --write packages/code-runtime/code-runtime/README.md
|
||||
README.md: 70f29b0508a4466366735f1badad02ec2d85b463
|
||||
README.zh.md: 6edb2b700a5f7861a36e15b5783bb9296b8467f3
|
||||
README.md: 32f5b76fb88c4880cef16abbfa11da52ae13d5ab
|
||||
README.zh.md: 28718c23b8449592ccc0c8c03fa4a9b38963c673
|
||||
@@ -29,19 +29,20 @@ Choose this package when you compose a deployment that executes model-written pr
|
||||
|
||||
### Run a program
|
||||
|
||||
Give the runtime a program source and one or more binding namespaces. Each namespace becomes one global object of async functions inside the program — PTC mode passes one under `tools`. The program runs as the body of an async function, so top-level `await` and `return` work; a lossless-JSON completion becomes `result.value`, each output channel preserves its own order in `result.logs` while cross-channel interleaving is backend-dependent, and any failure is reported in `result.error` with a kind you can branch on. The runtime never rejects for a program failure — rejection means you misused the seam, for example by submitting a run after disposal.
|
||||
Give the runtime a program and binding namespaces, then call `resolve(request)` followed by `run(spec)`. Resolution validates optional cwd, timeout and sandbox policy against the provider's capabilities and fills deployment defaults. The program runs as an async function body, so top-level `await` and `return` work; a lossless-JSON completion becomes `result.value`, captured text becomes `result.logs`, and program failures become `result.error`. Each output channel preserves its own order, while cross-channel interleaving is backend-dependent.
|
||||
|
||||
```text
|
||||
const result = await ctx.codeRuntime.run({
|
||||
const spec = ctx.codeRuntime.resolve({
|
||||
program: 'return await tools.add({ a: 1, b: 2 })',
|
||||
bindings: [{ global: 'tools', functions: { add: async (args) => args.a + args.b } }],
|
||||
})
|
||||
const result = await ctx.codeRuntime.run(spec)
|
||||
// result.value === 3
|
||||
```
|
||||
|
||||
### Choose a backend
|
||||
|
||||
Backends declare two descriptors you can rely on: `language` — what the program must be written in, with `'typescript'` and `'python'` as the well-known values — and `isolation` — the execution substrate (`'worker-thread'`, `'process'`, `'container'`), a label for deployments and diagnostics, not a security claim. [`dsh-code-runtime-node`](../code-runtime-node/README.md) executes TypeScript in a fresh Node worker thread; the private [`dsh-experimental-code-runtime-python`](../../experimental/code-runtime-python/README.md) package executes Python in a fresh CPython subprocess for opt-in compositions.
|
||||
Backends expose `language` and `isolation` as diagnostic descriptors; neither grants authority or proves confinement. [`dsh-code-runtime-node`](../code-runtime-node/README.md) executes erasable TypeScript in a fresh managed Node process under the resolved sandbox policy. The private [`dsh-experimental-code-runtime-python`](../../experimental/code-runtime-python/README.md) provider executes Python in a fresh CPython subprocess without file confinement. `sandboxMode` advertises a provider's deployment file-policy mode, or is absent when that capability is unsupported.
|
||||
|
||||
### Name your bindings portably
|
||||
|
||||
@@ -49,7 +50,7 @@ Binding-global and error-class names are language-portable: they must match `[A-
|
||||
|
||||
### What can go wrong
|
||||
|
||||
Failures arrive as `result.error` with an orthogonal `kind`: the program threw or failed to parse (`exception`), a budget expired (`timeout`), the run was aborted (`abort`), the execution substrate died (`worker-exit`), the completion value was not lossless JSON (`invalid-output`), or the serialized output exceeded the cap (`output-limit`). Each kind carries a model-feedable message. `run()` rejects only for seam misuse, such as a run submitted after disposal or a binding name that fails the portable-identifier rules.
|
||||
Failures arrive as `result.error` with an orthogonal `kind`: `exception`, `timeout`, `abort`, `worker-exit`, `invalid-output`, `output-limit`, `protocol` or `sandbox-unavailable`. Providers return applicable `result.sandbox` facts separately from success or failure. Invalid or unsupported execution options fail during `resolve`; `run` rejects caller misuse, such as unresolved inputs, invalid binding names or a call after disposal.
|
||||
|
||||
-----
|
||||
|
||||
@@ -63,17 +64,17 @@ This section explains the design behind the seam; observable behavior is fully c
|
||||
|
||||
### Design concept
|
||||
|
||||
The package is the Service Definition role of the code-execution capability seam ([capability seams](../../../.agents/notes/implemented/architecture/2026-06-13-capability-seams.md)): an abstract `CodeRuntime extends Service` registered as `ctx.codeRuntime`, plus the vocabulary both backends and the consumer share. Providers subclass `CodeRuntime`, implement `run`, and register the service; the consumer (PTC mode in `dsh-tools`) generates the model-facing SDK and bridges tool dispatch. The runtime stays ignorant of tools and sessions by contract: it receives a program and named async bindings and returns `{ value, logs, error? }`.
|
||||
The package is the Service Definition role of the code-execution capability seam ([capability seams](../../../.agents/notes/implemented/architecture/2026-06-13-capability-seams.md)): an abstract `CodeRuntime extends Service` registered as `ctx.codeRuntime`, plus the vocabulary both backends and the consumer share. Providers subclass `CodeRuntime`, implement `resolve` and `run`, and register the service; the consumer (PTC mode in `dsh-tools`) generates the model-facing SDK and bridges tool dispatch. The runtime stays ignorant of tools and sessions by contract: it receives a program, named async bindings and resolved execution options, then returns captured output, the outcome and applicable sandbox facts.
|
||||
|
||||
### Service API
|
||||
|
||||
The contract is three members a backend implements: `run(request)` executes one program against the request's bindings and resolves every program outcome — parse/transform failure, thrown exception, invalid completion, output overflow, budget expiry, abort, or substrate death — as a result `error` field, with rejection reserved for caller misuse such as a run submitted after disposal; `language` and `isolation` are read-only descriptors labeling the source language and execution substrate for deployments and diagnostics.
|
||||
`resolve(request)` owns supported option validation and deployment defaulting. `run(spec)` executes the complete inputs and resolves program outcomes after cleanup. Language and substrate descriptors guide presentation; `sandboxMode` indicates whether the consumer can pass a resolved file policy. Neither descriptors nor a successful program result substitute for the backend's reported enforcement facts.
|
||||
|
||||
The exhaustive semantics live in the [code runtime subsystem reference](../../../docs/subsystems/code-runtime.md); the exact signatures are in [`src/index.ts`](src/index.ts).
|
||||
|
||||
### Vocabulary
|
||||
|
||||
`CodeRunRequest` (`program`, `bindings`, `signal?`) carries everything the runtime acts on; defaulting (time budgets, output caps) is each provider's validated config, never a hidden `??` inside `run()`. `bindings` is a list of `CodeBindingNamespace`s (`global` + `functions` + optional `errorClass`), each exposed to the program as one global object of async callables returning `CodeJsonValue` — the seam's structural lossless-JSON type. An `errorClass` descriptor names a real program-global constructor and the own property that receives the rejected member name, so backends never learn consumer terms such as `ToolCallError`. `CodeRunResult` reports the lossless-JSON completion `value?`, per-channel-ordered `logs: string[]` with backend-dependent cross-channel interleaving, and `error?` (`CodeRunFailure`: orthogonal `kind` + model-feedable `message`). See `src/types.ts` for the full contracts.
|
||||
`CodeRunRequest` carries the program, host bindings, cancellation and optional execution choices. `CodeRunSpec` requires the resolved cwd and elapsed deadline. `CodeBindingNamespace` declares program globals and optional typed rejection constructors. `CodeRunResult` separates logs/value, failure and `CodeRunSandbox` facts; exact fields and provider obligations live in [`src/types.ts`](src/types.ts).
|
||||
|
||||
### Portable identifiers
|
||||
|
||||
@@ -84,7 +85,7 @@ Binding-global and error-class names are language-portable: they must match the
|
||||
| File | Role |
|
||||
|---|---|
|
||||
| [`src/index.ts`](src/index.ts) | Plugin entry: abstract `CodeRuntime` service and the portable-identifier exclusion sets |
|
||||
| [`src/types.ts`](src/types.ts) | Vocabulary: `CodeRunRequest`, `CodeBindingNamespace`, `CodeJsonValue`, `CodeRunResult`, `CodeRunFailure` |
|
||||
| [`src/types.ts`](src/types.ts) | Vocabulary: `CodeRunRequest`, `CodeRunSpec`, bindings, results, failures and sandbox facts |
|
||||
| — | No runtime invariant companion is published; this package exposes no independent event sequence or mutable data relation beyond contracts enforced at its owning seam. |
|
||||
|
||||
</details>
|
||||
@@ -97,7 +98,7 @@ Binding-global and error-class names are language-portable: they must match the
|
||||
Read these when the package-level contract is not enough. They move from the PTC mode consumer to the backends and the capability-seam model.
|
||||
|
||||
- [PTC mode Agent Note](../../../.agents/notes/implemented/feature/2026-06-15-ptc.md) — how the tool registry consumes `ctx.codeRuntime` and presents `run_code` to the model.
|
||||
- [Worker-thread backend](../code-runtime-node/README.md) — the shipped TypeScript execution backend.
|
||||
- [Node process backend](../code-runtime-node/README.md) — the shipped TypeScript execution backend.
|
||||
- [Experimental Python backend](../../experimental/code-runtime-python/README.md) — the private CPython subprocess provider and its fd-3 protocol.
|
||||
- [Code runtime subsystem reference](../../../docs/subsystems/code-runtime.md) — request/result vocabulary, bindings, and the `ctx.codeRuntime` cordis surface.
|
||||
- [Capability seams](../../../.agents/notes/implemented/architecture/2026-06-13-capability-seams.md) — the Service Definition / Service Provider / Consumer split.
|
||||
@@ -122,8 +123,8 @@ These limits define what the seam cannot do; they are current package constraint
|
||||
|
||||
- **`run()` is one-shot** — `logs` arrive only on the resolved `CodeRunResult`; the seam exposes no streaming-log or progress API for a live program's output.
|
||||
- **No state survives between runs** — every request runs against a fresh world; a persistent REPL-style kernel is deferred until a backend brings its own logging story.
|
||||
- **The worker-thread backend ships; the Python process backend is private experimental; `'container'` has no implementation** — a hard security boundary awaits a container backend.
|
||||
- **Intermediate binding values have no byte cap** — implementations remain subject to structured-clone cost and process memory, while a provider or executor may already have imposed its own acquisition bound.
|
||||
- **Providers have different confinement capabilities** — the shipped Node provider enforces a resolved file policy, while the private experimental Python provider rejects an explicit policy. No container provider is supplied.
|
||||
- **No uniform binding byte cap applies across providers** — each provider owns its transport limits; a binding can still allocate memory before its result reaches those limits.
|
||||
|
||||
<a id="dev-note"></a>
|
||||
### Dev Note
|
||||
|
||||
@@ -29,19 +29,20 @@ kind: "package-reference"
|
||||
|
||||
### 运行一个程序
|
||||
|
||||
向运行时提供程序源码与一个或多个绑定命名空间。每个命名空间会成为程序内的一个全局异步函数对象——PTC mode 在 `tools` 下传入一个。程序作为异步函数的函数体运行,因此顶层 `await`/`return` 可用;无损 JSON 完成值成为 `result.value`,每个输出通道在 `result.logs` 中保留自身顺序而跨通道交错由后端决定,任何失败都以 `result.error` 报告并带有可分支的 kind。运行时绝不会因程序失败而 reject——reject 意味着你误用了 seam,例如在 dispose(资源释放)后提交运行。
|
||||
向运行时提供程序与绑定命名空间,然后依次调用 `resolve(request)` 和 `run(spec)`。解析根据提供方能力验证可选 cwd、timeout 与沙箱策略,并填入部署默认值。程序作为异步函数体运行,支持顶层 `await` 与 `return`;无损 JSON 完成值成为 `result.value`,捕获文本成为 `result.logs`,程序失败成为 `result.error`。每个输出通道保持自身顺序,跨通道交错由后端决定。
|
||||
|
||||
```text
|
||||
const result = await ctx.codeRuntime.run({
|
||||
const spec = ctx.codeRuntime.resolve({
|
||||
program: 'return await tools.add({ a: 1, b: 2 })',
|
||||
bindings: [{ global: 'tools', functions: { add: async (args) => args.a + args.b } }],
|
||||
})
|
||||
const result = await ctx.codeRuntime.run(spec)
|
||||
// result.value === 3
|
||||
```
|
||||
|
||||
### 选择后端
|
||||
|
||||
后端声明两个你可以依赖的描述符:`language`——程序必须使用的源语言,已知值为 `'typescript'` 与 `'python'`——以及 `isolation`——执行基底(`'worker-thread'`、`'process'`、`'container'`),仅供部署与诊断使用,不构成安全声明。[`dsh-code-runtime-node`](../code-runtime-node/README.zh.md) 在全新的 Node Worker 线程中执行 TypeScript;私有的 [`dsh-experimental-code-runtime-python`](../../experimental/code-runtime-python/README.zh.md) 包在全新的 CPython 子进程中执行 Python,供选择性组合使用。
|
||||
后端以 `language` 与 `isolation` 提供诊断描述符;两者都不授予权限或证明约束。[`dsh-code-runtime-node`](../code-runtime-node/README.zh.md) 在全新的受管 Node 进程中按已解析沙箱策略执行可擦除 TypeScript。私有的 [`dsh-experimental-code-runtime-python`](../../experimental/code-runtime-python/README.zh.md) 提供方在全新 CPython 子进程中执行 Python,不提供文件约束。`sandboxMode` 声明提供方的部署文件策略模式;不支持该能力时则缺省。
|
||||
|
||||
### 可移植地命名绑定
|
||||
|
||||
@@ -49,7 +50,7 @@ binding-global 与 error-class 名称是语言可移植的:必须匹配 `[A-Za
|
||||
|
||||
### 可能出什么问题
|
||||
|
||||
失败以 `result.error` 返回,并带正交的 `kind`:程序抛出或解析失败(`exception`)、预算到期(`timeout`)、运行被中止(`abort`)、执行基底终止(`worker-exit`)、完成值不是无损 JSON(`invalid-output`),或序列化输出超过上限(`output-limit`)。每种 kind 都带一条可反馈给模型的消息。`run()` 只在 seam 误用时 reject,例如在 dispose 后提交运行,或绑定名称不符合可移植标识符规则。
|
||||
失败以 `result.error` 返回,带正交的 `kind`:`exception`、`timeout`、`abort`、`worker-exit`、`invalid-output`、`output-limit`、`protocol` 或 `sandbox-unavailable`。提供方在成功或失败之外,单独返回适用的 `result.sandbox` 事实。无效或不支持的执行选项在 `resolve` 期间失败;`run` 拒绝调用方误用,例如未解析输入、无效绑定名或资源释放后的调用。
|
||||
|
||||
-----
|
||||
|
||||
@@ -63,17 +64,17 @@ binding-global 与 error-class 名称是语言可移植的:必须匹配 `[A-Za
|
||||
|
||||
### 设计理念
|
||||
|
||||
本包是代码执行能力 seam 的 Service Definition 角色([能力 seam](../../../.agents/notes/implemented/architecture/2026-06-13-capability-seams.zh.md)):一个注册为 `ctx.codeRuntime` 的抽象 `CodeRuntime extends Service`,加上两个后端与消费方共享的词汇。提供方继承 `CodeRuntime`、实现 `run` 并注册服务;消费方(`dsh-tools` 中的 PTC mode)生成面向模型的 SDK 并桥接工具分发。按约定,运行时不了解工具与会话:它接收程序与具名异步绑定,返回 `{ value, logs, error? }`。
|
||||
本包是代码执行能力 seam 的 Service Definition 角色([能力 seam](../../../.agents/notes/implemented/architecture/2026-06-13-capability-seams.zh.md)):一个注册为 `ctx.codeRuntime` 的抽象 `CodeRuntime extends Service`,加上两个后端与消费方共享的词汇。提供方继承 `CodeRuntime`、实现 `resolve` 和 `run` 并注册服务;消费方(`dsh-tools` 中的 PTC mode)生成面向模型的 SDK 并桥接工具分发。按约定,运行时不了解工具与会话:它接收程序、具名异步绑定和已解析执行选项,然后返回捕获输出、执行结果与适用的沙箱事实。
|
||||
|
||||
### 服务 API
|
||||
|
||||
约定是后端实现的三个成员:`run(request)` 针对请求的绑定执行一段程序,并把每个程序结果——解析/转换失败、抛出异常、无效完成值、输出溢出、预算到期、中止或基底终止——都作为结果 `error` 字段 resolve,reject 只留给调用方误用,例如在 dispose 后提交运行;`language` 与 `isolation` 是只读描述符,为部署与诊断标注源语言与执行基底。
|
||||
`resolve(request)` 负责支持选项的验证与部署默认值。`run(spec)` 执行完整输入,并在清理后返回程序结果。语言和执行基底描述符指导呈现;`sandboxMode` 表示消费方能否传入已解析文件策略。描述符与程序成功结果都不能代替后端报告的强制能力事实。
|
||||
|
||||
穷尽式语义见[代码运行时子系统参考](../../../docs/subsystems/code-runtime.zh.md);确切签名见 [`src/index.ts`](src/index.ts)。
|
||||
|
||||
### 词汇
|
||||
|
||||
`CodeRunRequest`(`program`、`bindings`、`signal?`)携带运行时操作所需的全部内容;默认值(时间预算、输出上限)来自各提供方的已验证配置,绝不是 `run()` 内部隐藏的 `??`。`bindings` 是 `CodeBindingNamespace` 列表(`global` + `functions` + 可选 `errorClass`),每个命名空间作为程序内的一个全局异步可调用函数对象公开,返回 `CodeJsonValue`——seam 的结构性无损 JSON 类型。`errorClass` 描述符点名真实的程序全局构造器,以及用于接收被拒绝成员名称的自有属性,因此后端永远不会得知 `ToolCallError` 之类的 Consumer 术语。`CodeRunResult` 报告无损 JSON 完成值 `value?`、通道内有序且跨通道交错由后端决定的 `logs: string[]`,以及 `error?`(`CodeRunFailure`:正交 `kind` + 可反馈给模型的 `message`)。完整约定见 `src/types.ts`。
|
||||
`CodeRunRequest` 携带程序、Host 绑定、取消和可选执行选择。`CodeRunSpec` 要求已解析的 cwd 与经过时间截止。`CodeBindingNamespace` 声明程序全局对象与可选的类型化拒绝构造器。`CodeRunResult` 将日志/值、失败与 `CodeRunSandbox` 事实分开;确切字段与提供方义务见 [`src/types.ts`](src/types.ts)。
|
||||
|
||||
### 可移植标识符
|
||||
|
||||
@@ -84,7 +85,7 @@ binding-global 与 error-class 名称是语言可移植的:必须匹配标识
|
||||
| 文件 | 职责 |
|
||||
|---|---|
|
||||
| [`src/index.ts`](src/index.ts) | 插件入口:抽象 `CodeRuntime` 服务与可移植标识符排除集 |
|
||||
| [`src/types.ts`](src/types.ts) | 词汇:`CodeRunRequest`、`CodeBindingNamespace`、`CodeJsonValue`、`CodeRunResult`、`CodeRunFailure` |
|
||||
| [`src/types.ts`](src/types.ts) | 词汇:`CodeRunRequest`、`CodeRunSpec`、绑定、结果、失败与沙箱事实 |
|
||||
| — | 不发布运行时不变式伴生入口;本包不公开任何独立的事件序列或可变数据关系,相关约束仅由其所属 seam 的约定实施。 |
|
||||
|
||||
</details>
|
||||
@@ -97,7 +98,7 @@ binding-global 与 error-class 名称是语言可移植的:必须匹配标识
|
||||
当包级约定不够用时阅读以下内容。它们从 PTC mode 消费方进入后端与能力 seam 模型。
|
||||
|
||||
- [PTC mode Agent Note](../../../.agents/notes/implemented/feature/2026-06-15-ptc.zh.md)——工具注册表如何消费 `ctx.codeRuntime` 并把 `run_code` 呈现给模型。
|
||||
- [Worker 线程后端](../code-runtime-node/README.zh.md)——已发布的 TypeScript 执行后端。
|
||||
- [Node 进程后端](../code-runtime-node/README.zh.md)——已发布的 TypeScript 执行后端。
|
||||
- [实验性 Python 后端](../../experimental/code-runtime-python/README.zh.md)——私有的 CPython 子进程提供方及其 fd-3 协议。
|
||||
- [代码运行时子系统参考](../../../docs/subsystems/code-runtime.zh.md)——请求/结果词汇、绑定与 `ctx.codeRuntime` 的 cordis 接口面。
|
||||
- [能力 seam](../../../.agents/notes/implemented/architecture/2026-06-13-capability-seams.zh.md)——Service Definition / Service Provider / Consumer 拆分。
|
||||
@@ -122,8 +123,8 @@ binding-global 与 error-class 名称是语言可移植的:必须匹配标识
|
||||
|
||||
- **`run()` 是一次性的**——`logs` 只有在 `CodeRunResult` resolve 后才能获得;seam 不提供正在运行的程序所产生输出的流式日志或进度接口。
|
||||
- **运行之间不保留状态**——每次请求都在全新环境中运行;持久 REPL 风格内核在某个后端带来自己的日志方案之前保持延期。
|
||||
- **worker 线程后端已发布;Python process 后端是私有实验包;`'container'` 没有实现**——强安全边界需要等待容器后端。
|
||||
- **中间 binding 值没有字节上限**——实现仍受 structured-clone 成本与进程内存约束,而提供方或执行器可能已经应用自己的获取上限。
|
||||
- **提供方的约束能力不同**——已发布 Node 提供方强制执行已解析文件策略,私有实验性 Python 提供方拒绝显式策略。不提供容器提供方。
|
||||
- **提供方之间没有统一的绑定字节上限**——各提供方负责自己的传输限制;绑定仍可能在结果到达这些限制前分配内存。
|
||||
|
||||
<a id="dev-note"></a>
|
||||
### 开发备注
|
||||
|
||||
Reference in new issue
Block a user