1
/**2
* Vocabulary types for the PTC execution seam: what a caller hands a3
* {@link ../index.ts | PtcRuntime} and what it gets back. Pure types — no4
* runtime code lives here.5
*6
* @module @deepseek-ai/dsh-ptc-runtime/src/types7
*/9
import type { SandboxEnforcement, SandboxExecutionPolicy, SandboxMode } from '@deepseek-ai/dsh-sandbox'11
/**12
* One host-side function exposed to the program as an async callable. The13
* runtime bridges calls to it (possibly across a serialization boundary), so14
* `args` and the resolution value MUST be lossless JSON. A runtime rejects a15
* lossy or non-cloneable value with a descriptive error rather than corrupting16
* the run. No seam-level byte cap applies to a binding resolution. A rejection17
* of this function surfaces inside the program as a rejection of the18
* corresponding call.19
*/20
export type PtcBindingFunction = (args: unknown) => Promise<PtcJsonValue>22
/** A lossless JSON value transferable through the dependency-light Service Definition. */23
export type PtcJsonValue = null | boolean | number | string | PtcJsonValue[] | { [key: string]: PtcJsonValue }25
/**26
* Program-visible typed rejection for one binding namespace. The runtime27
* injects a real error constructor under `name`; rejected member calls become28
* its instances and expose the exact member name through29
* `memberNameProperty`. Both strings are runtime data rather than knowledge30
* of a particular consumer such as PTC mode.31
*/32
export interface PtcBindingErrorClass {33
/** Constructor global and resulting `Error.name`; same portable identifier rule as {@link PtcBindingNamespace.global}. */34
name: string35
/**36
* Non-empty own property for the member name. The portable exclusion set is37
* `RESERVED_ERROR_MEMBERS` plus dunder-form names (`__x__`, non-empty38
* middle), enforced identically by every backend; any other name —39
* identifiers or not — is accepted everywhere.40
*/41
memberNameProperty: string42
}44
/**45
* A named group of {@link PtcBindingFunction}s the runtime exposes to the46
* program as one global object (e.g. `tools`). Function names are arbitrary47
* strings — a runtime must treat names like `__proto__` or `constructor` as48
* ordinary own properties (null-prototype construction), never as prototype49
* collisions.50
*/51
export interface PtcBindingNamespace {52
/**53
* The global identifier the program sees. Must match the LANGUAGE-PORTABLE54
* identifier subset `[A-Za-z_][A-Za-z0-9_]*` and no language's reserved55
* words, so the same namespace list works against every backend regardless56
* of `language` — a JS-only spelling like `$tools` is rejected by design,57
* not just by the Python backend. Names that satisfy the identifier rule but58
* name a backend-owned slot (`RESERVED_BINDING_GLOBALS`, e.g. `console`,59
* `__dsh_main__`) are also refused everywhere; see its declaration for the60
* exact set and why each entry is reserved.61
*/62
global: string63
/** The callable members, keyed by the exact name the program calls. */64
functions: Record<string, PtcBindingFunction>65
/** Optional program-visible typed rejection contract for this namespace. */66
errorClass?: PtcBindingErrorClass67
}69
/**70
* Caller inputs for one program. The provider's resolve method validates supported71
* options and supplies directory, deadline, and authority before execution.72
*/73
export interface PtcRunRequest {74
/**75
* The program source, in the runtime's {@link ../index.ts | language}. It76
* runs as the body of an async function: top-level `await` and `return`77
* are available, and the completion value becomes78
* {@link PtcRunResult.value}.79
*/80
program: string81
/** Host functions exposed to the program, one global object per namespace. */82
bindings: PtcBindingNamespace[]83
/** Working directory in the mounted filesystem and subprocess execution world. */84
cwd?: string85
/**86
* Elapsed execution budget in milliseconds. Omission uses provider defaults;87
* null requests no deadline. Providers validate and cap numeric budgets or reject unsupported choices.88
*/89
timeoutMs?: number | null90
/** Resolved authority for this execution. Providers without confinement reject an explicit policy. */91
sandboxPolicy?: SandboxExecutionPolicy92
/**93
* Abort the run: the runtime stops the program (hard, even mid-loop) and94
* resolves with a {@link PtcRunFailure} of kind `'abort'`. In-flight95
* binding calls are the CALLER's to settle — the runtime only stops asking.96
*/97
signal?: AbortSignal98
}100
/** Fully resolved execution inputs; run never supplies a missing directory or deadline choice. */101
export interface PtcRunSpec extends PtcRunRequest {102
/** Absolute directory in the provider's execution world. */103
cwd: string104
/** Positive finite elapsed budget in milliseconds after provider capping, or null for no deadline. */105
timeoutMs: number | null106
}108
/** File confinement applied to a program, independently of its terminal outcome. */109
export interface PtcRunSandbox {110
/** File-effect mode used for this execution. */111
mode: SandboxMode112
/** Program failure text matched backend diagnostics; not enforcement proof or an exhaustive denial record. */113
denied: boolean114
/** Completeness reported by the selected confining backend; absent for full access. */115
enforcement?: SandboxEnforcement116
}118
/**119
* Why a run failed. The kinds are orthogonal outcomes reported independently120
* (per docs/defensive-patterns.md): a budget expiry is not an exception, an121
* abort is not a timeout, and a substrate death is neither.122
*123
* - `'exception'` — the program threw or failed to parse/transform.124
* - `'timeout'` — an implementation-owned budget expired; the message says which.125
* - `'abort'` — {@link PtcRunRequest.signal} fired.126
* - `'worker-exit'` — the execution substrate died without settling (e.g. OOM).127
* - `'invalid-output'` — the completion value was not lossless JSON.128
* - `'output-limit'` — the serialized outer logs/value/diagnostic exceeded the configured cap.129
* - `'protocol'` — the program sent invalid or over-budget control traffic.130
* - `'sandbox-unavailable'` — required confinement could not be established.131
*/132
export interface PtcRunFailure {133
/** The failure class (see the interface doc for each kind's meaning). */134
kind: 'exception' | 'timeout' | 'abort' | 'worker-exit' | 'invalid-output' | 'output-limit' | 'protocol' | 'sandbox-unavailable'135
/** Human-readable detail, suitable for feeding back to a model to self-correct. */136
message: string137
}139
/**140
* The outcome of one run. An error is a FIELD on a resolved result, never a141
* rejection of `run()` — reporting a failed program is the caller's job, not142
* an exception path.143
*/144
export interface PtcRunResult {145
/** Applied file policy and observed denial, when the provider enforces file policy. */146
sandbox?: PtcRunSandbox147
/**148
* The program's completion value (its top-level `return`), when it ran to149
* completion and the value crossed the runtime's lossless-JSON boundary.150
* Invalid or over-limit completions fail the run instead of substituting a151
* rendered string; a failed or value-less run leaves this absent.152
*/153
value?: PtcJsonValue154
/**155
* Captured text. Each source channel preserves emission order; interleaving156
* across independent channels is backend-dependent. Bounded only as part of157
* the outer result.158
*/159
logs: string[]160
/** Present iff the run failed; see {@link PtcRunFailure} for the taxonomy. */161
error?: PtcRunFailure162
}