From 538ad4d3dc0d8cf172cfc7e2e89dffa721923c2a Mon Sep 17 00:00:00 2001 From: Chinesezjc Date: Sun, 2 Aug 2026 13:57:53 +0800 Subject: [PATCH] docs(code-runtime-python): sync README with the shipped backend The package README (both languages) still described this layer as protocol-only with the PythonCodeRuntime implementation deferred to a later PR, contradicting the shipped code. Rewrite the intro to describe the registered runtime, add a Configuration section for every Config cap, and drop the "implementation not in this layer" limitation. Also pin the residual-detach fixture's size invariant: the byteLength assertion only holds above Node's Buffer pool threshold. --- .../code-runtime-python/README.i18n.yaml | 4 +- .../code-runtime-python/README.md | 113 +++------------- .../code-runtime-python/README.zh.md | 121 +++--------------- .../tests/residual-detach.spec.ts | 15 ++- 4 files changed, 48 insertions(+), 205 deletions(-) diff --git a/packages/code-runtime/code-runtime-python/README.i18n.yaml b/packages/code-runtime/code-runtime-python/README.i18n.yaml index 535aee0b2d..e1e1e5bf3a 100644 --- a/packages/code-runtime/code-runtime-python/README.i18n.yaml +++ b/packages/code-runtime/code-runtime-python/README.i18n.yaml @@ -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-python/README.md -README.md: 7aea5ee4c031a36718e66a583762f61832596d04 -README.zh.md: 778cc61ef28be616b8b0ed1ce8f0c9396143a768 +README.md: 31c493ee119f145c7cf11ed22e2c191226919410 +README.zh.md: 6f808ed7c768985be38c6b9f4d6cb701e702cd76 diff --git a/packages/code-runtime/code-runtime-python/README.md b/packages/code-runtime/code-runtime-python/README.md index 7aea5ee4c0..31c493ee11 100644 --- a/packages/code-runtime/code-runtime-python/README.md +++ b/packages/code-runtime/code-runtime-python/README.md @@ -1,100 +1,32 @@ --- -description: "fd-3 wire protocol between a Node host and a CPython subprocess for users and maintainers building or debugging the Python code-execution backend." -kind: "package-library" +description: "CPython subprocess implementation of the DeepSeek Harness code-execution seam, with fd-3 bindings, resource limits, log capture, and process-group teardown." +kind: "package-reference" --- # @deepseek-ai/dsh-code-runtime-python English | [中文](README.zh.md) -## Summary +CPython-subprocess implementation of the [`@deepseek-ai/dsh-code-runtime`](../code-runtime/README.md) seam. Companion to [`@deepseek-ai/dsh-code-runtime-worker-thread`](../code-runtime-worker-thread/README.md); trades the Node worker thread for a fresh `python3` subprocess so model code is Python instead of TypeScript. -`dsh-code-runtime-python` owns the versionless wire protocol between a Node host and a CPython subprocess for the [`dsh-code-runtime`](../code-runtime/README.md) seam: one JSON object per line on the child's fd 3, leaving stdout/stderr free for the program's own output. The package ships the host-side frame codec and hostile-frame validators (`src/protocol.ts`) plus the Python-side mirror of the same message vocabulary (`py/protocol.py`), so every consumer of the wire shares one vocabulary. It is the protocol layer for the Python backend — the package carries no subprocess execution path, so nothing here spawns `python3` outside the cross-language mirror test. The host treats every inbound frame as hostile, because model code has full access to fd 3 and can post anything through it. +The package ships `PythonCodeRuntime` as its default export. The plugin registers as `codeRuntime` with `language: 'python'` and `isolation: 'process'`. Each `run()` spawns a fresh `python3 -I` process, sends a boot frame and the program over fd 3, and resolves a `CodeRunResult` for every program outcome — rejecting only for seam misuse (a malformed binding namespace or non-positive config). The child runs the program as the body of an async function, so top-level `await` and `return` both work; binding calls travel back over fd 3 as JSON-lines. Containment (not a security boundary — model code has bash-equivalent trust) comes from an empty environment, `RLIMIT_CPU`/`RLIMIT_AS`, a wall-clock ceiling, and a `SIGTERM`→grace→`SIGKILL` teardown on the child's process group. -## Table of Contents +## Wire protocol -- [Use this package](#use-this-package) -- [Understand the implementation](#understand-the-implementation) -- [Further Exploration](#further-exploration) -- [Model Experience](#model-experience) -- [Known Limitations and Deferred Work](#known-limitations-and-deferred-work) -- [Dev Note](#dev-note) +The host and the CPython subprocess exchange a versionless, JSON-lines protocol on the child's fd 3 — one JSON object per line, leaving stdout/stderr free for the program's own output. `src/protocol.ts` is the host side; `py/protocol.py` mirrors its message shapes and the shared truncation-marker text on the Python side. ------ +- **fd 3, not stdout** — Node pins the channel positionally with `stdio: ['pipe','pipe','pipe','pipe']`; the Python bootstrap reads the same `PROTOCOL_FD` constant. JSON-lines framing. +- **Host treats every inbound frame as hostile** — model code has full access to fd 3 and can post anything through it, so `validateChildFrame` shape-validates and REBUILDS each frame before the host reads it: forged extra fields never ride along, a non-number call id can never be echoed into a reply, and junk drops to `undefined` rather than throwing in the host's message handler. The Python side trusts host replies (the host is not model-controlled). +- **Lossless-JSON crossing** — completion values and binding arguments cross as exact JSON. `encodeJsonPlain` serializes a `JSON.parse`-produced value without recursion, so a deep value below the byte budget crosses intact instead of dying on `JSON.stringify`'s stack limit; `checkDoneValue` meters a forged completion value's byte length AND number losslessness in one bounded traversal that rejects an over-budget payload before enqueuing its children; `hasUnsafeIntegerToken` reads the raw frame text to catch an integer token that `JSON.parse` would silently round; `hasNonLosslessNumber` rejects a non-finite or negative-zero number in unbounded `call.args`. Beyond-safe-range integral doubles serialize through `BigInt` digits so the exact integer crosses, not the rounded `String()` form. +- **Shared truncation marker** — `logTruncationMarker(maxBytes)` produces byte-identical text on both sides, so a truncated log run reads the same however the cap was hit. The `log` frame's `truncated` flag distinguishes the child ledger's own marker from program output. - -## Use this package +## Configuration -Choose this package when you build or consume the CPython code-runtime wire: implement the Python backend or the host that drives it, or debug a Python code run's framing. The package is the wire protocol intended for a CPython code-runtime provider — such a provider runs each model program in a fresh `python3 -I` subprocess — and this package supplies the protocol both sides speak, so its exports are the single TS-side source of truth for the wire. +Every cap is a validated `Config` field with a default, changeable from `cordis.yml` (no hardcoded tunables). `cpuSeconds` (default 60) is the `RLIMIT_CPU` whole-second budget; the child sets the soft limit to `cpuSeconds` and the hard limit to `cpuSeconds + 1`, so the kernel's `SIGXCPU` at the soft limit classifies as a `timeout` while the +1s hard limit is a `SIGKILL` backstop. `maxWallMs` (default 600000) is the wall-clock ceiling that backstops CPU time for a program awaiting a promise nobody resolves. `addressSpaceMb` (default 512) is the `RLIMIT_AS` cap, not applied on Darwin (the dyld shared cache mapped into every process exceeds any practical cap there; `cpuSeconds` and `maxWallMs` still bound the run). `maxLogBytes` (default 65536) is the shared captured-log byte budget; `maxValueBytes` (default 32768) caps the completion value; `graceMs` (default 3000) is the `SIGTERM`→`SIGKILL` grace window; `pythonBin` (default `python3`) is the interpreter, resolved against `PATH` before the child spawns with an empty environment. -### What you get - -The package re-exports the host-side protocol vocabulary from `src/index.ts`: `validateChildFrame` (rebuilds every inbound frame before the host reads it), the lossless-JSON codec and meters (`encodeJsonPlain`, `checkDoneValue`, `hasUnsafeIntegerToken`, `hasNonLosslessNumber`), and `logTruncationMarker` (the shared truncation-marker text). The Python side mirrors the message shapes as `TypedDict`s in `py/protocol.py` and re-declares the two surfaces both sides execute against — `PROTOCOL_FD = 3` and the marker text. - -### The wire - -Frames travel on the child's fd 3 as JSON-lines — one object per line — so stdout/stderr stay clear for the program's own output. Child → host: `boot-ack`, `call`, `log`, `done`. Host → child: `boot` (first frame, carrying every cap and the namespace declarations), `run` (after `boot-ack`, carrying only the program body), and one `reply` per `call`. A forged frame can carry both `value` and `error` on `done`, so a consumer must check `error` first and ignore `value` when it is set. - -### What can go wrong - -Host-side validation drops junk without throwing, so a malformed or forged frame never crashes the host process: `validateChildFrame` returns `undefined` for anything that does not rebuild cleanly, a non-number call id can never be echoed into a reply, and forged extra fields never ride along. A completion value that is not lossless JSON, or that exceeds the configured byte budget, is rejected explicitly (`non-lossless` / `over-budget`) rather than silently rounded or truncated. - ------ - - -## Understand the implementation - -
-Implementation internals — click to expand - -This section explains the design behind the wire protocol; observable behavior is fully covered in [Use this package](#use-this-package). - -### Design concept - -The protocol assumes one direction of trust: the host treats every inbound frame as hostile (model code can forge anything on fd 3) and REBUILDS it field by field before reading; the Python side trusts host replies, because the host is not model-controlled. The package is deliberately the protocol layer only — the Python-side JSON codec lives in the backend's bootstrap, not in `py/protocol.py`, so the mirror stays the pure wire-vocabulary counterpart of `src/protocol.ts`. - -### Wire contract - -The frames are `boot` / `run` (host → child) and `boot-ack` / `call` / `log` / `done` plus one `reply` per call (child → host). The `log` frame's `truncated` flag marks the frame that IS the child ledger's truncation marker, so the host stops capturing at the same point the child did instead of inferring it from its own budget. `done.error.kind` is one of `exception`, `invalid-output`, `output-limit`; wall/CPU budgets, aborts, and substrate death are observed host-side, not carried as frames. - -### Lossless JSON crossing - -Completion values and binding arguments cross as exact JSON: values serialize without recursion, so a deep payload below the byte budget survives instead of dying on `JSON.stringify`'s stack limit, and integral doubles beyond the safe range cross as exact digits rather than silently rounded tokens; the meters in [`src/protocol.ts`](src/protocol.ts) enforce byte budgets and number losslessness before anything else reads the payload. - -### Mirror alignment - -`tests/protocol-mirror.e2e.ts` spawns a real `python3` and asserts, against `src/protocol.ts`, both `PROTOCOL_FD` / the truncation-marker text and each `TypedDict`'s required/optional wire field set in `py/protocol.py`, so a renamed or dropped field — or one side making a field optional the other requires — fails the test. Field *types* are not compared across the language boundary; that residue stays with review plus the backend's real-subprocess suite. - -### Source map - -| File | Role | -|---|---| -| [`src/index.ts`](src/index.ts) | Plugin entry: re-exports the protocol vocabulary for every consumer of the wire | -| [`src/protocol.ts`](src/protocol.ts) | Host side: frame codec, hostile-frame validators, lossless-JSON meters, shared marker text | -| [`py/protocol.py`](py/protocol.py) | Python side: `PROTOCOL_FD`, `TypedDict` frame mirrors, `log_truncation_marker` | -| [`tests/protocol-mirror.e2e.ts`](tests/protocol-mirror.e2e.ts) | Cross-language mirror test against a real `python3` | -| [`src/invariant.ts`](src/invariant.ts) | Invariant companion (no runtime invariant; the package registers no mutable data relation) | - -
- ------ - - -## Further Exploration - -Read these when the protocol contract is not enough. They move from the seam definition to the protocol's design record and the companion backend. - -- [Code runtime seam](../code-runtime/README.md) — the abstract contract the Python backend implements. -- [fd-3 protocol Agent Note](../../../.agents/notes/implemented/architecture/2026-07-31-code-runtime-python-fd3-protocol.md) — design rationale, wire contract, and the mirror-alignment decision. -- [Worker-thread backend](../code-runtime-worker-thread/README.md) — the shipped TypeScript sibling, the model for the Python backend's behavior. -- [Code runtime subsystem reference](../../../docs/subsystems/code-runtime.md) — request/result vocabulary, bindings, and failure taxonomy. - ------ - - ## Model Experience -Indirectly, through PTC mode in `dsh-tools`, which renders the program's completion value or failure into a retained `run_code` result. +Indirectly, through Code Mode in [`dsh-tools`](../../core/tools/README.md), which renders this backend's exact completion value when it fits (or an explicit `invalid-output` / `output-limit` failure), plus the exact `[dsh-code-runtime-python] log capture truncated at bytes` log marker, into a retained `run_code` result. #### KV Cache effect @@ -102,20 +34,5 @@ No direct invalidation; the named consumer owns any request-prefix changes. ## Known Limitations and Deferred Work - - - -These limits define what the package does and does not cover; they are current package constraints, not a task backlog. - -- **The cross-language guard covers the executed surfaces and the frame field shapes, not the field types** — the mirror e2e compares required/optional field sets, not that `cpuSeconds` is an `int` on both sides; comparing type declarations across TypeScript and Python has no mechanical equivalent here, so a type-level drift is caught by review plus the backend's real-subprocess suite. -- **`src/index.ts` exports the protocol vocabulary only** — the package carries no subprocess execution path and no Python-side JSON codec, so nothing here spawns `python3` outside the mirror test. - - -### Dev Note - -
-Working context for maintainers — click to expand - -None. - -
+- **The cross-language guard covers executed values and frame field sets, not field types** — `tests/protocol-mirror.e2e.ts` compares `PROTOCOL_FD`, the log truncation marker, and each `TypedDict`'s required and optional fields against a real `python3`. Comparing field types across TypeScript and Python has no mechanical equivalent here, so review plus the backend's real-subprocess suite owns type-level drift. +- **`RLIMIT_AS` is not enforced on macOS** — the dyld shared cache mapped into every process at exec exceeds any practical address-space cap, and the kernel rejects the `setrlimit` call, so `addressSpaceMb` is skipped there. `cpuSeconds` and `maxWallMs` still bound every run. diff --git a/packages/code-runtime/code-runtime-python/README.zh.md b/packages/code-runtime/code-runtime-python/README.zh.md index 778cc61ef2..6f808ed7c7 100644 --- a/packages/code-runtime/code-runtime-python/README.zh.md +++ b/packages/code-runtime/code-runtime-python/README.zh.md @@ -1,121 +1,38 @@ --- -description: "Node host 与 CPython 子进程之间的 fd-3 协议格式(wire protocol),供用户与维护者构建或排查 Python 代码执行后端。" -kind: "package-library" +description: "DeepSeek Harness 代码执行 seam 的 CPython 子进程实现,提供 fd-3 binding、资源限制、日志捕获与进程组拆卸。" +kind: "package-reference" --- # @deepseek-ai/dsh-code-runtime-python [English](README.md) | 中文 -## 概述 +[`@deepseek-ai/dsh-code-runtime`](../code-runtime/README.zh.md) seam 的 CPython 子进程实现。与 [`@deepseek-ai/dsh-code-runtime-worker-thread`](../code-runtime-worker-thread/README.zh.md) 配套;以全新的 `python3` 子进程取代 Node worker 线程,让模型代码从 TypeScript 换成 Python。 -`dsh-code-runtime-python` 持有 [`dsh-code-runtime`](../code-runtime/README.zh.md) seam 的 Node host 与 CPython 子进程之间的无版本协议格式(wire protocol):子进程 fd 3 上每行一个 JSON 对象,让 stdout/stderr 空出给程序自己的输出。本包提供 host 侧的帧编解码与敌意帧校验器(`src/protocol.ts`),以及同一套消息词汇的 Python 侧镜像(`py/protocol.py`),因此每个 wire 消费方都共享同一套词汇。它是 Python 后端的协议层——本包不含子进程执行路径,因此除跨语言镜像测试之外,没有任何地方会启动 `python3`。host 把每个入站帧都当作敌意输入,因为模型代码对 fd 3 有完全访问权、可通过它发送任意内容。 +本包以默认导出提供 `PythonCodeRuntime`。该插件以 `language: 'python'`、`isolation: 'process'` 注册为 `codeRuntime`。每次 `run()` 启动一个全新的 `python3 -I` 进程,通过 fd 3 发送 boot 帧和程序,并为每个程序结果 resolve 一个 `CodeRunResult`——仅在 seam 被误用时才 reject(binding 命名空间不合法或 config 非正)。子进程把程序作为 async 函数体运行,因此顶层 `await` 与 `return` 都可用;binding 调用经 fd 3 以 JSON-lines 回传。containment 不是安全边界——模型代码具有等同 bash 的信任级别;空环境、`RLIMIT_CPU`/`RLIMIT_AS`、墙钟上限与对子进程进程组的 `SIGTERM`→grace→`SIGKILL` 拆卸共同提供 containment。 -## 目录 +## Wire protocol -- [使用本包](#use-this-package) -- [理解实现](#understand-the-implementation) -- [进一步探索](#further-exploration) -- [模型体验](#model-experience) -- [已知限制与延期工作](#known-limitations-and-deferred-work) -- [开发备注](#dev-note) +host 与 CPython 子进程在子进程的 fd 3 上交换一个无版本号的 JSON-lines 协议——每行一个 JSON 对象,让 stdout/stderr 空出给程序自己的输出。`src/protocol.ts` 是 host 侧;`py/protocol.py` 在 Python 侧镜像其帧词汇与共享的截断标记文本。 ------ +- **fd 3,而非 stdout** —— Node 通过 `stdio: ['pipe','pipe','pipe','pipe']` 按位置钉住通道;Python bootstrap 读取相同的 `PROTOCOL_FD` 常量。JSON-lines 帧。 +- **host 把每个入站帧当作敌意输入** —— 模型代码对 fd 3 有完全访问权、可通过它发送任意内容,所以 `validateChildFrame` 在 host 读取前对每个帧做形状校验并重建:伪造的额外字段绝不随行,非数字的 call id 绝不会被回显进 reply,垃圾降为 `undefined` 被丢弃,而不是在 host 的 message handler 里抛错。Python 侧信任 host 回复(host 不受模型控制)。 +- **lossless-JSON 穿越** —— 完成值与 binding 参数以精确 JSON 穿越。`encodeJsonPlain` 无递归地序列化一个 `JSON.parse` 产出的值,使低于字节预算的深层值能完整穿越,而不是死在 `JSON.stringify` 的栈限制上;`checkDoneValue` 在一次有界遍历中同时计量伪造完成值的字节长度与数字无损性,在把子节点入栈之前就拒绝超预算 payload;`hasUnsafeIntegerToken` 读取原始帧文本,捕获 `JSON.parse` 会静默舍入的整数 token;`hasNonLosslessNumber` 拒绝无字节上限的 `call.args` 中的非有限数或负零。超出安全范围的整数型 double 通过 `BigInt` 数字序列化,穿越的是精确整数而非 `String()` 的舍入形式。 +- **共享截断标记** —— `logTruncationMarker(maxBytes)` 在两侧产出逐字节一致的文本,使被截断的日志运行无论从哪侧触达上限都读起来一致。`log` 帧的 `truncated` 标志把子进程 ledger 自身的标记与程序输出区分开。 - -## 使用本包 +## Configuration -当你要构建或消费 CPython 代码运行时 wire 时选择本包:实现 Python 后端或驱动它的 host,或排查 Python 代码运行的帧。本包是为 CPython code-runtime 提供方准备的 wire 协议——这样的提供方会在全新的 `python3 -I` 子进程中运行每个模型程序——本包提供两侧共同使用的协议,因此其导出是 wire 的 TS 侧唯一真源。 +每个上限都是带默认值的、经校验的 `Config` 字段,可从 `cordis.yml` 修改(无硬编码可调项)。`cpuSeconds`(默认 60)是 `RLIMIT_CPU` 的整秒预算;子进程把软限设为 `cpuSeconds`、硬限设为 `cpuSeconds + 1`,因此内核在软限处发出的 `SIGXCPU` 被归类为 `timeout`,而 +1 秒的硬限是 `SIGKILL` 兜底。`maxWallMs`(默认 600000)是墙钟上限,为一个在等待无人 resolve 的 promise 的程序兜住 CPU 时间。`addressSpaceMb`(默认 512)是 `RLIMIT_AS` 上限,在 Darwin 上不施加(那里映射进每个进程的 dyld 共享缓存超过任何实际上限;`cpuSeconds` 与 `maxWallMs` 仍约束运行)。`maxLogBytes`(默认 65536)是共享的捕获日志字节预算;`maxValueBytes`(默认 32768)为完成值设上限;`graceMs`(默认 3000)是 `SIGTERM`→`SIGKILL` 的 grace 窗口;`pythonBin`(默认 `python3`)是解释器,在子进程以空环境启动前先对 `PATH` 解析。 -### 你得到什么 +## Model Experience -本包从 `src/index.ts` 重新导出 host 侧的协议词汇:`validateChildFrame`(在 host 读取前重建每个入站帧)、无损 JSON 编解码与计量器(`encodeJsonPlain`、`checkDoneValue`、`hasUnsafeIntegerToken`、`hasNonLosslessNumber`),以及 `logTruncationMarker`(共享的截断标记文本)。Python 侧在 `py/protocol.py` 中把消息形状镜像为 `TypedDict`,并重新声明两侧都执行的两个表面——`PROTOCOL_FD = 3` 与标记文本。 +Indirectly, through Code Mode in [`dsh-tools`](../../core/tools/README.md), which renders this backend's exact completion value when it fits (or an explicit `invalid-output` / `output-limit` failure), plus the exact `[dsh-code-runtime-python] log capture truncated at bytes` log marker, into a retained `run_code` result. -### 协议格式 +#### KV Cache effect -帧在子进程 fd 3 上以 JSON-lines 传输——每行一个对象——因此 stdout/stderr 保持空闲,供程序自己的输出使用。子进程 → host:`boot-ack`、`call`、`log`、`done`。host → 子进程:`boot`(首帧,携带所有上限与命名空间声明)、`run`(在 `boot-ack` 之后,只携带程序主体),以及每个 `call` 一个 `reply`。伪造帧可以在 `done` 上同时携带 `value` 与 `error`,因此消费方必须先检查 `error`,在它存在时忽略 `value`。 +No direct invalidation; the named consumer owns any request-prefix changes. -### 可能出什么问题 +## Known Limitations and Deferred Work -host 侧校验会静默丢弃垃圾,因此格式错误或伪造的帧绝不会让宿主进程崩溃:`validateChildFrame` 对任何无法干净重建的内容返回 `undefined`,非数字的 call id 绝不会被回显进 reply,伪造的额外字段绝不随行。不是无损 JSON、或超出配置字节预算的完成值会被明确拒绝(`non-lossless`/`over-budget`),而不会被静默舍入或截断。 - ------ - - -## 理解实现 - -
-实现细节——点击展开 - -本节解释协议格式(wire protocol)背后的设计;可观察行为已在[使用本包](#use-this-package)中完整说明。 - -### 设计理念 - -协议假定单向信任:host 把每个入站帧都当作敌意输入(模型代码可以在 fd 3 上伪造任何内容),并在读取前逐字段重建;Python 侧信任 host 回复,因为 host 不受模型控制。本包刻意只是协议层——Python 侧 JSON codec 位于后端的 bootstrap 中,而非 `py/protocol.py`,因此镜像保持为 `src/protocol.ts` 的纯 wire 词汇对侧。 - -### 协议约定 - -帧为 `boot`/`run`(host → 子进程)与 `boot-ack`/`call`/`log`/`done` 加每个 call 一个 `reply`(子进程 → host)。`log` 帧的 `truncated` 标志标记「就是子进程 ledger 截断标记」的那个帧,因此 host 在子进程停下的同一点停止捕获,而不是根据自己的预算推断。`done.error.kind` 是 `exception`、`invalid-output`、`output-limit` 之一;墙钟/CPU 预算、中止与基底终止在 host 侧观测,不作为帧携带。 - -### 无损 JSON 穿越 - -完成值与 binding 参数以精确 JSON 穿越:值无递归地序列化,因此低于字节预算的深层 payload 能完整穿越,而不是死在 `JSON.stringify` 的栈限制上;超出安全范围的整数型 double 以精确数字穿越,而不是被静默舍入的 token;[`src/protocol.ts`](src/protocol.ts) 中的计量器在任何其他代码读取 payload 之前强制执行字节预算与数字无损性。 - -### 镜像对齐 - -`tests/protocol-mirror.e2e.ts` 启动一个真实 `python3`,对照 `src/protocol.ts` 断言 `PROTOCOL_FD`/截断标记文本,以及 `py/protocol.py` 中每个 `TypedDict` 的必填/可选 wire 字段集,因此重命名或删除字段——或一侧把另一侧必填的字段改为可选——都会让测试失败。跨语言边界不比较字段*类型*;该残留由 review 加后端的真子进程套件负责。 - -### 源码地图 - -| 文件 | 职责 | -|---|---| -| [`src/index.ts`](src/index.ts) | 插件入口:为每个 wire 消费方重新导出协议词汇 | -| [`src/protocol.ts`](src/protocol.ts) | host 侧:帧编解码、敌意帧校验器、无损 JSON 计量器、共享标记文本 | -| [`py/protocol.py`](py/protocol.py) | Python 侧:`PROTOCOL_FD`、`TypedDict` 帧镜像、`log_truncation_marker` | -| [`tests/protocol-mirror.e2e.ts`](tests/protocol-mirror.e2e.ts) | 对照真实 `python3` 的跨语言镜像测试 | -| [`src/invariant.ts`](src/invariant.ts) | 不变式伴生插件(无运行时不变式;本包不注册任何可变数据关系) | - -
- ------ - - -## 进一步探索 - -当协议约定不够用时阅读以下内容。它们从 seam 定义进入协议的设计记录与配套后端。 - -- [代码运行时 seam](../code-runtime/README.zh.md)——Python 后端实现的抽象约定。 -- [fd-3 协议 Agent Note](../../../.agents/notes/implemented/architecture/2026-07-31-code-runtime-python-fd3-protocol.zh.md)——设计理由、协议约定与镜像对齐决策。 -- [Worker 线程后端](../code-runtime-worker-thread/README.zh.md)——已发布的 TypeScript 兄弟包,是 Python 后端行为的模板。 -- [代码运行时子系统参考](../../../docs/subsystems/code-runtime.zh.md)——请求/结果词汇、绑定与失败分类体系。 - ------ - - -## 模型体验 - -通过 `dsh-tools` 中的 PTC mode 间接提供;后者把程序的完成值或失败渲染进一个保留的 `run_code` 结果。 - -#### KV Cache 影响 - -不会直接失效;由上述消费方负责请求前缀变更。 - -## 已知限制与延期工作 - - - - -这些限制说明本包覆盖什么、不覆盖什么;它们是当前包约束,不是任务积压。 - -- **跨语言 guard 覆盖执行表面与帧字段形状,但不覆盖字段类型**——镜像 e2e 比较必填/可选字段集,而不比较 `cpuSeconds` 两侧是否都是 `int`;跨 TypeScript 与 Python 比较类型声明在此无机械等价物,因此类型级漂移由 review 加后端的真子进程套件捕获。 -- **`src/index.ts` 只导出协议词汇**——本包不含子进程执行路径,也不含 Python 侧的 JSON codec,因此除镜像测试之外没有任何地方会启动 `python3`。 - - -### 开发备注 - -
-维护者的工作上下文——点击展开 - -无。 - -
+- **跨语言 guard 覆盖执行值与帧字段集,但不覆盖字段类型** —— `tests/protocol-mirror.e2e.ts` 使用真实 `python3` 比较 `PROTOCOL_FD`、日志截断标记,以及每个 `TypedDict` 的必填和可选字段。跨 TypeScript 与 Python 比较字段类型在此没有机械等价物,因此类型级漂移由 review 加后端真子进程套件负责。 +- **`RLIMIT_AS` 在 macOS 上不施加** —— 在 exec 时映射进每个进程的 dyld 共享缓存超过任何实际的地址空间上限,内核会拒绝该 `setrlimit` 调用,故 `addressSpaceMb` 在那里被跳过。`cpuSeconds` 与 `maxWallMs` 仍约束每一次运行。 diff --git a/packages/code-runtime/code-runtime-python/tests/residual-detach.spec.ts b/packages/code-runtime/code-runtime-python/tests/residual-detach.spec.ts index 87c040eb00..d19a7cef53 100644 --- a/packages/code-runtime/code-runtime-python/tests/residual-detach.spec.ts +++ b/packages/code-runtime/code-runtime-python/tests/residual-detach.spec.ts @@ -5,6 +5,12 @@ describe('detachResidual — fd-3 residual detachment', () => { it('returns a copy that does NOT share the source frame allocation', () => { // Simulate the data handler's state: one large joined frame from // Buffer.concat, sliced past its newline to leave a small residual VIEW. + // The fixture MUST stay larger than Node's Buffer pool threshold + // (`Buffer.poolSize / 2`, 4 KiB): above it `Buffer.from` allocates a + // dedicated backing store whose `byteLength` equals the copy's length, + // which is what the byteLength assertion below pins. A smaller residual + // would be pooled into an 8 KiB shared ArrayBuffer, making `byteLength` + // report 8192 and the assertion false-fail even though the fix is intact. const joined = Buffer.alloc(1024 * 1024, 0x61) // 1 MiB backing allocation joined[512] = 0x0a // a newline partway through const residual = joined.subarray(513) // a view onto `joined`'s backing store @@ -17,10 +23,13 @@ describe('detachResidual — fd-3 residual detachment', () => { expect(carried).toBeDefined() expect(carried!.length).toBe(residual.length) expect(carried!.equals(residual)).toBe(true) - // The copy's backing store is its own, sized to its content — not the 1 MiB - // frame. A subarray view would report the source's full byteLength here. - expect(carried!.buffer.byteLength).toBe(carried!.length) + // The core invariant: the copy does NOT share the source frame's backing + // store, so retaining it cannot pin the 1 MiB allocation. expect(carried!.buffer).not.toBe(joined.buffer) + // And the copy's own backing store is sized to its content — not the whole + // frame. Holds because the fixture exceeds the pool threshold (see above); + // a subarray view would report the source's full byteLength here. + expect(carried!.buffer.byteLength).toBe(carried!.length) }) it('carries nothing forward for an empty residual', () => {