Files
deepseek-harness/docs/subsystems/ptc-runtime.md
T
imccyu 793ce790a6 feat(ptc): add scoped REPL execution with copied top-level bindings
With replScoped: true, persistent-in-memory runs each call as a separate
VM Script in one retained context. Each call starts with copies of the
top-level bindings left by earlier calls, so a later call may declare any
earlier name again with any keyword while earlier functions and
still-running code keep their values. Results, top-level await, errors
and asynchronous failures follow the existing REPL evaluator; modules
load only through dynamic import(), resolved from the environment's first
working directory.

The control protocol moves to version 3; the upgrade guide covers
preinstalled bootstraps and the new PtcProgramFormat member. The Agent
Note records the decision and its comparison with node:repl and
vm-script, and the ptc-scoped-repl snapshot pins the model guidance.
2026-11-10 18:52:19 +08:00

17 KiB

PTC runtime

English | 中文

The PTC execution capability seam supplies ctx.ptcRuntime through dsh-ptc-runtime. It runs one program against host bindings and reports output, failure and applicable sandbox facts. PTC execution is optional rather than part of the agent-loop spine. The PTC foundation owns registry presentation, the typed-return contract owns binding values, and the sandboxed Node decision owns the shipped execution provider.

Source: packages/ptc-runtime/ptc-runtime/src/types.ts

The run: request in, result out

PtcRunRequest contains the program, bindings, cancellation and optional execution choices. The provider's resolve validates supported choices and applies its deployment defaults; run receives a PtcRunSpec with an explicit directory and deadline choice. An omitted timeout uses provider defaults, a number requests a capped elapsed budget, and null requests no elapsed deadline. Providers reject unsupported choices before execution:

/**
 * Caller inputs for one program. The provider's resolve method validates supported
 * options and supplies directory, deadline, and authority before execution.
 */
interface PtcRunRequest {
  /**
   * The program source, in the runtime's {@link ../index.ts | language}. It
   * follows the provider's programFormat: an async function body supports
   * top-level `await` and `return`; REPL (`repl`, `scoped-repl`) and Script programs support top-level
   * declarations, `await`, and a final expression, but not top-level `return`.
   * The Script provider lowers top-level await; its executionInstructions and
   * documentation define retained-binding semantics. The completion value
   * becomes {@link PtcRunResult.value}.
   */
  program: string
  /** Discard the completion without JSON validation when true; omission preserves the completion value. */
  discardCompletion?: boolean
  /**
   * Object-identity owner for optional in-memory state. Omission requests a fresh
   * environment. Consumers must await runtime.release(owner) when the owner ends.
   * Stateless providers ignore this field; retained state is never durable.
   */
  stateOwner?: object
  /** Host functions exposed to the program, one global object per namespace. */
  bindings: PtcBindingNamespace[]
  /** Working directory in the mounted filesystem and subprocess execution world. */
  cwd?: string
  /**
   * Elapsed execution budget in milliseconds. Omission uses provider defaults;
   * null requests no deadline. Providers validate and cap numeric budgets or reject unsupported choices.
   */
  timeoutMs?: number | null
  /** Resolved authority for this execution. Providers without confinement reject an explicit policy. */
  sandboxPolicy?: SandboxExecutionPolicy
  /**
   * Abort the run: the runtime stops the program (hard, even mid-loop) and
   * resolves with a {@link PtcRunFailure} of kind `'abort'`. In-flight
   * binding calls are the CALLER's to settle — the runtime only stops asking.
   */
  signal?: AbortSignal
}
/** Fully resolved execution inputs; run never supplies a missing directory or deadline choice. */
interface PtcRunSpec extends PtcRunRequest {
  /** Absolute directory in the provider's execution world. */
  cwd: string
  /** Positive finite elapsed budget in milliseconds after provider capping, or null for no deadline. */
  timeoutMs: number | null
}
/** File confinement applied to a program, independently of its terminal outcome. */
interface PtcRunSandbox {
  /** File-effect mode used for this execution. */
  mode: SandboxMode
  /** Program failure text matched backend diagnostics; not enforcement proof or an exhaustive denial record. */
  denied: boolean
  /** Completeness reported by the selected confining backend; absent for full access. */
  enforcement?: SandboxEnforcement
}

Program failures resolve through PtcRunResult.error; invalid caller inputs may reject before execution. Sandbox mode, observed denial and enforcement completeness are separate facts, so a successful program does not by itself prove that every requested restriction was enforced:

/**
 * The outcome of one run. An error is a FIELD on a resolved result, never a
 * rejection of `run()` — reporting a failed program is the caller's job, not
 * an exception path.
 */
interface PtcRunResult {
  /** Applied file policy and observed denial, when the provider enforces file policy. */
  sandbox?: PtcRunSandbox
  /**
   * The program's completion value (a return or final REPL/Script expression), when it ran to
   * completion and the value crossed the runtime's lossless-JSON boundary.
   * Invalid or over-limit completions fail the run instead of substituting a
   * rendered string; a failed or value-less run leaves this absent.
   */
  value?: PtcJsonValue
  /**
   * Captured text fragments retain their emitted line breaks. Concatenate adjacent text without separators.
   * Each source channel preserves emission order; interleaving
   * across independent channels is backend-dependent. Bounded only as part of
   * the outer result.
   */
  output: PtcOutput[]
  /** Present iff the run failed; see {@link PtcRunFailure} for the taxonomy. */
  error?: PtcRunFailure
}
/** Source evaluation form, independent of retained program state. */
type PtcProgramFormat = 'function-body' | 'repl' | 'script' | 'scoped-repl'

programFormat distinguishes async function bodies, REPL statements, scoped REPL statements, and scripts. REPL, scoped REPL, and Script programs accept top-level await, declarations, and a final expression; top-level return is not supported. Retained REPL calls share one scope, while each scoped REPL call starts with copies of the top-level bindings that earlier calls left, so it can declare those names again without changing earlier code. The Node Script provider lowers top-level await and awaits the completion value; its declaration limitations apply to transformed programs. retainsState reports memory retention independently of source syntax. stateOwner is an in-process object identity for optional retained memory; distinct objects remain isolated. Consumers await release(owner) when that owner ends; cleanup failures reject that call. Omitted owners are one-shot, and stateless providers ignore ownership. The persistent Node provider defines reset and recovery behavior; no retained object enters the durable Session format.

Bindings: host functions as program globals

Each PtcBindingNamespace becomes a global object of async callables; PTC passes tools. Arguments and resolutions must be lossless JSON. Providers enforce their own transport caps; the seam sets no uniform binding-byte limit. An optional error-class descriptor creates program-visible typed rejections without naming a consumer inside the runtime. Binding names are own properties, so __proto__ cannot traverse a prototype:

/**
 * Program-visible typed rejection for one binding namespace. The runtime
 * injects a real error constructor under `name`; rejected member calls become
 * its instances and expose the exact member name through
 * `memberNameProperty`. Both strings are runtime data rather than knowledge
 * of a particular consumer such as PTC mode.
 */
interface PtcBindingErrorClass {
  /** Constructor global and resulting `Error.name`; same portable identifier rule as {@link PtcBindingNamespace.global}. */
  name: string
  /**
   * Non-empty own property for the member name. The portable exclusion set is
   * `RESERVED_ERROR_MEMBERS` plus dunder-form names (`__x__`, non-empty
   * middle), enforced identically by every backend; any other name —
   * identifiers or not — is accepted everywhere.
   */
  memberNameProperty: string
}
/**
 * A named group of {@link PtcBindingFunction}s the runtime exposes to the
 * program as one global object (e.g. `tools`). Function names are arbitrary
 * strings — a runtime must treat names like `__proto__` or `constructor` as
 * ordinary own properties (null-prototype construction), never as prototype
 * collisions.
 */
interface PtcBindingNamespace {
  /**
   * The global identifier the program sees. Must match the LANGUAGE-PORTABLE
   * identifier subset `[A-Za-z_][A-Za-z0-9_]*` and no language's reserved
   * words, so the same namespace list works against every backend regardless
   * of `language` — a JS-only spelling like `$tools` is rejected by design,
   * not just by the Python backend. Names that satisfy the identifier rule but
   * name a backend-owned slot (`RESERVED_BINDING_GLOBALS`, e.g. `inspect`, `console`, `Image`,
   * `__dsh_main__`) are also refused everywhere; see its declaration for the
   * exact set and why each entry is reserved.
   */
  global: string
  /** The callable members, keyed by the exact name the program calls. */
  functions: Record<string, PtcBindingFunction>
  /** Optional program-visible typed rejection contract for this namespace. */
  errorClass?: PtcBindingErrorClass
}
/** A lossless JSON value transferable through the dependency-light Service Definition. */
type PtcJsonValue = null | boolean | number | string | PtcJsonValue[] | { [key: string]: PtcJsonValue }
/**
 * One host-side function exposed to the program as an async callable. The
 * runtime bridges calls to it (possibly across a serialization boundary), so
 * `args` must be lossless JSON; resolutions also support host-issued PtcImage values. A runtime rejects a
 * lossy or non-cloneable value with a descriptive error rather than corrupting
 * the run. No seam-level byte cap applies to a binding resolution. A rejection
 * of this function surfaces inside the program as a rejection of the
 * corresponding call.
 */
type PtcBindingFunction = (args: unknown) => Promise<PtcJsonValue>

Captured output and the failure taxonomy

output contains text and image entries in emission order: { type: 'text', text } or { type: 'image', attachment }. Text entries retain emitted newline characters; concatenate adjacent text without separators. Each source channel preserves emission order; interleaving across independent channels is backend-dependent. Host bindings return PtcImage descriptors containing the attachment reference and a readable execution-world path; the program receives an Image with that path and the stored pixel dimensions. The Node provider caps serialized text, image descriptors, and completion or diagnostic payloads together; overflow is an explicit failure.

/** A stored image readable from the program's execution environment. */
declare class PtcImage {
  readonly [key: string]: PtcJsonValue;
  /** Absolute attachment file path readable in the execution world. */
  readonly path: string;
  /** Stored image selected when the program inspects this value. */
  readonly attachment: Readonly<ImageAttachmentRef>;
  /**
   * Describe one immutable image file in the execution world.
   * @param path - Absolute path to the stored image.
   * @param attachment - Reference to the immutable stored image.
   */
  constructor(path: string, attachment: ImageAttachmentRef);
}
/** Raw text fragments and inspected images in emission order; concatenate adjacent text without separators. */
type PtcOutput = { type: 'text'; text: string } | { type: 'image'; attachment: ImageAttachmentRef }

Failure kinds are orthogonal outcomes reported independently (per defensive-patterns): a budget expiry is not an exception, an abort is not a timeout, and a substrate death (e.g. OOM) is neither:

/**
 * Why a run failed. The kinds are orthogonal outcomes reported independently
 * (per docs/defensive-patterns/general.md): a budget expiry is not an exception, an
 * abort is not a timeout, and a substrate death is neither.
 *
 * - `'exception'` — the program threw or failed to parse/transform.
 * - `'timeout'` — an implementation-owned budget expired; the message says which.
 * - `'abort'` — {@link PtcRunRequest.signal} fired.
 * - `'worker-exit'` — the execution substrate died without settling (e.g. OOM).
 * - `'invalid-output'` — the completion value was not lossless JSON.
 * - `'output-limit'` — the serialized outer logs/value/diagnostic exceeded the configured cap.
 * - `'protocol'` — the program sent invalid or over-budget control traffic.
 * - `'sandbox-unavailable'` — required confinement could not be established.
 */
interface PtcRunFailure {
  /** The failure class (see the interface doc for each kind's meaning). */
  kind: 'exception' | 'timeout' | 'abort' | 'worker-exit' | 'invalid-output' | 'output-limit' | 'protocol' | 'sandbox-unavailable'
  /** Human-readable detail, suitable for feeding back to a model to self-correct. */
  message: string
}

The service

PtcRuntime is defined in src/index.ts. resolve(request) returns complete execution inputs, and run(spec) executes them. executionInstructions supplies provider-owned usage guidance for consumer presentation. timeout reports the configured elapsed-time default and maximum when per-call overrides are supported; resolve still validates and caps each request. language selects supported program presentation; isolation describes the substrate without claiming security. sandboxMode advertises file-policy support, with undefined for a provider that does not supply confinement. Each implementation keeps program state separate between runs and terminates and awaits active executions during disposal.

Cordis API

Generated from source by scripts/gen-cordis-catalog.ts (verified fresh by pnpm run verify-cordis-catalog in doc-sync; regenerate with pnpm run gen-cordis-catalog) — the language sides differ only in locale-specific paired document paths. Signature blocks use a ts cordis-catalog fence and keep the original source JSDoc; dispatch modes are defined in the primer, and the framework-inherited ctx API lives in cordis-api/inherited.md.

ctx.ptcRuntime — PtcRuntime (abstract seam)

Registers one ctx.ptcRuntime implementation. Program, budget, abort, and substrate failures resolve in PtcRunResult; only Service Definition contract misuse rejects. Implementations bridge structured-cloneable bindings, materialize each declared namespace rejection class, treat programs as hostile peers, isolate distinct state owners, and terminate and await in-flight runs during disposal.

/**
 * Release an owner's retained environment and await its execution resources.
 * Stateless providers have no retained resources to release.
 * @param owner - The exact object supplied as the request's state owner.
 * @returns Completion after all owned execution resources have been released.
 * @throws When managed resource cleanup fails.
 */
release(owner: object): Promise<void>

/**
 * Register source evaluated once when each execution environment is created.
 * Registration must precede the first run admission; only one initializer may
 * be registered. Current bindings are installed before initialization. Its
 * completion is discarded; logs, timeout, cancellation and output bounds share
 * the first run. Initialization failure discards the environment without
 * executing the user program or rolling back external effects.
 * @param program - Initialization source in the provider's language and format.
 * @returns An idempotent async disposer that stops admission, cancels and drains
 * admitted runs, and releases environments. Failed cleanup blocks execution
 * until the disposer succeeds on retry. The caller owns this disposer.
 * @throws When unsupported, registered twice, registered after admission, or invalid.
 */
registerEnvironmentInitializer(program: string): () => Promise<void>

/**
 * Resolve supported options and provider defaults before execution.
 * @param request - Program, bindings, cancellation and optional execution choices.
 * @returns Complete directory, deadline and supported authority for run.
 * @throws When an explicit choice is invalid or unsupported by this provider.
 */
abstract resolve(request: PtcRunRequest): PtcRunSpec

/**
 * Execute resolved inputs; program outcomes resolve as result fields.
 * @param spec - directory, deadline, program, bindings, cancellation and supported policy.
 * @returns Captured output and the execution outcome.
 */
abstract run(spec: PtcRunSpec): Promise<PtcRunResult>

Source: packages/ptc-runtime/ptc-runtime/src/index.ts