mirror of
https://github.com/deepseek-ai/deepseek-harness.git
synced 2026-08-30 04:40:37 +00:00
Introduce @deepseek-ai/dsh-code-runtime-python with the versionless JSON-lines protocol between the Node host and the CPython subprocess: the host-side hostile-frame codec (validateChildFrame, encodeJsonPlain, checkDoneValue, hasUnsafeIntegerToken, hasNonLosslessNumber, logTruncationMarker) and the Python-side wire-vocabulary mirror (py/protocol.py). This is the protocol layer of the code-runtime-python stack, split from #436 and based on the multi-language seam extension. The PythonCodeRuntime implementation and its Python JSON codec land in the backend-core PR on top of this branch. Ship the minimal buildable package skeleton (package.json, tsconfig, tsdown, barrel index, invariant companion, bilingual README) because the workspace-constraint, coverage, and invariant-topology gates require the package to exist and build the moment its directory does; the backend-core PR extends those files rather than creating them. Align py/protocol.py with src/protocol.ts (the round-12 review of #436 found LogMessage.truncated, DoneMessage.error.kind, and Namespace.errorClass stale) and guard the two runtime-executed surfaces (PROTOCOL_FD and the log truncation marker) with a real-python3 cross-language mirror e2e test.
127 lines
3.6 KiB
Python
127 lines
3.6 KiB
Python
"""Wire protocol vocabulary for the Python side of dsh-code-runtime-python.
|
|
|
|
Mirrors ``src/protocol.ts``. Frames travel on fd 3 as JSON-lines (one JSON
|
|
object per line). The host validates every inbound frame; this side trusts
|
|
host replies.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any, Literal, TypedDict, Union
|
|
|
|
# The protocol fd from the child's perspective. Node passes
|
|
# ``stdio: [pipe, pipe, pipe, pipe]`` so the fourth entry (fd 3) is the
|
|
# framed-JSON channel; stdout/stderr stay clear for the program's own output.
|
|
PROTOCOL_FD = 3
|
|
|
|
|
|
class BootMessage(TypedDict):
|
|
"""Host → child, first frame on fd 3. Carries every cap and the namespaces."""
|
|
|
|
type: Literal["boot"]
|
|
cpuSeconds: int
|
|
addressSpaceBytes: int
|
|
maxLogBytes: int
|
|
maxValueBytes: int
|
|
namespaces: list["Namespace"]
|
|
|
|
|
|
class ErrorClass(TypedDict):
|
|
"""A namespace's program-visible exception class: rejected calls raise its
|
|
instances carrying the failed member name on ``memberNameProperty``."""
|
|
|
|
name: str
|
|
memberNameProperty: str
|
|
|
|
|
|
class Namespace(TypedDict, total=False):
|
|
"""One binding namespace declaration: the global name, its function names,
|
|
and an optional program-visible ``errorClass`` for rejected calls."""
|
|
|
|
global_: str # required; renamed on the wire: JSON field is ``global`` (Python keyword collision)
|
|
names: list[str] # required
|
|
errorClass: ErrorClass # optional — mirrors the TS `errorClass?`
|
|
|
|
|
|
class RunMessage(TypedDict):
|
|
"""Host → child, sent after ``boot-ack``. Carries only the program body."""
|
|
|
|
type: Literal["run"]
|
|
program: str
|
|
|
|
|
|
class BootAckMessage(TypedDict):
|
|
"""Child → host: resource limits applied, ready for the run message."""
|
|
|
|
type: Literal["boot-ack"]
|
|
|
|
|
|
class CallMessage(TypedDict):
|
|
"""Child → host: one bridged binding call from the model program."""
|
|
|
|
type: Literal["call"]
|
|
id: int
|
|
global_: str # wire field is ``global``
|
|
name: str
|
|
args: Any
|
|
|
|
|
|
class LogMessage(TypedDict, total=False):
|
|
"""Child → host: one captured text chunk, streamed eagerly.
|
|
|
|
``truncated`` is set only on the frame that IS the child ledger's truncation
|
|
marker (not program output), so the host stops capturing at the same point
|
|
the child did — mirrors the TS `truncated?`.
|
|
"""
|
|
|
|
type: Literal["log"] # required
|
|
text: str # required
|
|
truncated: bool # optional
|
|
|
|
|
|
class DoneErrorField(TypedDict):
|
|
"""Child → host: the failure carried on a ``done`` frame. ``kind`` is one of
|
|
the three the host validates; ``message`` is the traceback or diagnostic."""
|
|
|
|
kind: Literal["exception", "invalid-output", "output-limit"]
|
|
message: str
|
|
|
|
|
|
class DoneMessage(TypedDict, total=False):
|
|
"""Child → host: the program settled. ``value`` and ``error`` are optional per the TS mirror."""
|
|
|
|
type: Literal["done"] # required — TypedDict(total=False) allows this via a required subclass in Py 3.11+; MVP keeps it flat
|
|
value: Any
|
|
error: DoneErrorField
|
|
|
|
|
|
ChildToHost = Union[BootAckMessage, CallMessage, LogMessage, DoneMessage]
|
|
|
|
|
|
class ReplyOk(TypedDict):
|
|
type: Literal["reply"]
|
|
id: int
|
|
ok: Literal[True]
|
|
value: Any
|
|
|
|
|
|
class ReplyErr(TypedDict):
|
|
type: Literal["reply"]
|
|
id: int
|
|
ok: Literal[False]
|
|
message: str
|
|
|
|
|
|
ReplyMessage = Union[ReplyOk, ReplyErr]
|
|
HostToChild = ReplyMessage
|
|
|
|
|
|
def log_truncation_marker(max_bytes: int) -> str:
|
|
"""Return the in-band marker for a log ledger that exhausted its budget.
|
|
|
|
Byte-identical text on both sides of the wire so a truncated run reads the
|
|
same however the cap was hit.
|
|
"""
|
|
|
|
return f"[dsh-code-runtime-python] log capture truncated at {max_bytes} bytes"
|