pi-mono/packages/codemode/src/portable.ts
Mario Zechner 0fb1aee749 feat(codemode): deterministic runs that the host answers (sandbox.start)
For durable code mode: a host can rerun a script after a restart and get the
same calls with the same arguments. start() runs the script with a virtual
clock (startedAt, then each result's time, never backwards), randomness from a
seed, UTC, no WeakRef/FinalizationRegistry, and the run's own tools and
globals; every call goes to onCall with the VM's call id, and the host answers
with run.deliver(seq, now, id, outcome). The VM applies results strictly in
seq order whatever the transport does, reports its fingerprint (engine
versions, prelude hash, limits) before the script runs, the calls issued after
each result (onIdle), and the result it was processing when the script ended
(result.during). Rejections can carry properties for the script's Error.
execute() is unchanged.
2026-10-11 19:50:26 +00:00

53 lines
1.5 KiB
TypeScript

/**
* Sandboxes that need no Node built-ins: `InlineCodemodeSandbox` runs the VM on the calling thread
* and `RemoteCodemodeSandbox` runs it in another isolate. Neither imports `node:*` modules, so this
* entry works in Cloudflare Workers, Durable Objects, and browsers. `CodemodeSandbox`, which uses
* worker threads, stays in the main entry.
*/
export { toCodemodeIdentifier } from "./identifier.ts";
export { CodemodeSandboxBase } from "./runtime/execution.ts";
export {
DEFAULT_INTERRUPT_BUDGET,
InlineCodemodeSandbox,
type InlineCodemodeSandboxOptions,
} from "./runtime/inline.ts";
export {
MAX_OUTPUT_CHARS,
MAX_OUTPUT_ITEMS,
MAX_STORE_TOTAL_CHARS,
MAX_STORE_VALUE_CHARS,
} from "./runtime/prelude-source.ts";
export type { HostToWorkerMessage, VmStartData, WorkerToHostMessage } from "./runtime/protocol.ts";
export {
type CodemodeRemote,
type CodemodeRemoteExchange,
type CodemodeRemoteMessage,
type CodemodeRemoteReply,
type CodemodeRemoteRequest,
type CodemodeServeOptions,
RemoteCodemodeSandbox,
type RemoteCodemodeSandboxOptions,
serveCodemodeRemote,
} from "./runtime/remote.ts";
export type {
CodemodeCall,
CodemodeCallStatus,
CodemodeDeclaredGlobal,
CodemodeDeclaredTool,
CodemodeDuring,
CodemodeError,
CodemodeErrorKind,
CodemodeExecuteOptions,
CodemodeHostCall,
CodemodeJsonSchema,
CodemodeOutcome,
CodemodeOutputItem,
CodemodeResult,
CodemodeRun,
CodemodeRunOptions,
CodemodeSandboxBaseOptions,
CodemodeStoreWrites,
CodemodeTool,
CodemodeToolContext,
} from "./types.ts";
export type { CodemodeWasmModule } from "./wasm.ts";