Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

index.ts

463 lines14.1 KB

1// Capability tokens + accessors (sandbox-owned only).
2// LockStore / withLocks / defineLock: import from @tanstack/ai/locks.
3export {
4 SandboxCapability,
5 SandboxPolicyCapability,
6 ToolBridgeProvisionerCapability,
7 getSandbox,
8 provideSandbox,
9 getSandboxPolicy,
10 provideSandboxPolicy,
11 getToolBridgeProvisioner,
12 provideToolBridgeProvisioner,
13} from './capabilities'
14​
15// Durable instance map (resume-or-create across processes).
16// Pass a store to `withSandbox(sandbox, { instances })`; the capability is the
17// ambient alternative for platform-level wiring.
18export {
19 SandboxInstanceStoreCapability,
20 getSandboxInstanceStore,
21 provideSandboxInstanceStore,
22 InMemorySandboxInstanceStore,
23 defineSandboxInstanceStore,
24} from './instance-store'
25export type {
26 SandboxInstanceStore,
27 SandboxInstanceRecord,
28} from './instance-store'
29​
30// Portable immutable sandbox checkpoint metadata.
31export {
32 SandboxCheckpointError,
33 SandboxCheckpointConflictError,
34 SandboxCheckpointDuplicateIdError,
35 SandboxCheckpointInvalidIdError,
36 SandboxCheckpointInvalidEntryError,
37 SandboxCheckpointParentMismatchError,
38 SandboxCheckpointNotHeadError,
39 SandboxCheckpointWriterConflictError,
40 SandboxCheckpointWriterLostError,
41 isForkCapableSandboxCheckpointStore,
42 InMemorySandboxCheckpointStore,
43 defineSandboxCheckpointStore,
44} from './checkpoint-store'
45​
46export type {
47 SandboxCheckpoint,
48 SandboxCheckpointStore,
49 SandboxSnapshotEntry,
50 SandboxSnapshotFileEntry,
51 SandboxSnapshotDirectoryEntry,
52 SandboxSnapshotArtifact,
53 SandboxCheckpointErrorCode,
54 SandboxCheckpointWriter,
55 SandboxCheckpointWriterLease,
56 SandboxCheckpointStoreOptions,
57 SandboxCheckpointForkInput,
58 SandboxCheckpointForkCapability,
59 ForkCapableSandboxCheckpointStore,
60} from './checkpoint-store'
61​
62// File snapshot policy used by provider snapshot create/restore inputs.
63export { SandboxSnapshotError, defaultSandboxSnapshotPolicy } from './snapshots'
64export type {
65 SandboxSnapshotErrorCode,
66 SandboxSnapshotPolicy,
67} from './snapshots'
68export { memorySandboxSnapshots } from './memory-snapshots'
69export type {
70 MemorySandboxSnapshots,
71 MemorySandboxSnapshotsOptions,
72} from './memory-snapshots'
73export { createSandboxSnapshots } from './snapshot-operations'
74export type {
75 CreateSandboxSnapshotsInput,
76 ForkSandboxSnapshotInput,
77 ReadSandboxSnapshotArtifactInput,
78 SandboxSnapshots,
79 SaveSandboxSnapshotInput,
80 SnapshotPersistence,
81} from './snapshot-operations'
82export { createSnapshotTools } from './snapshot-tools'
83export type { CreateSnapshotToolsOptions } from './snapshot-tools'
84​
85// Workspace projection capability (provided by withSandbox, consumed by harness adapters)
86export {
87 ProjectionCapability,
88 getWorkspaceProjection,
89 provideWorkspaceProjection,
90} from './projection'
91export type { WorkspaceProjection } from './projection'
92​
93// Middleware
94export { withSandbox } from './middleware'
95export type { SandboxMiddlewareOptions } from './middleware'
96​
97// Harness tool history: `withSandbox` records the tool calls a harness ran INSIDE the
98// sandbox into the transcript, so a finished run restores its tool cards. This is how
99// an app recognises them — e.g. in its own `MessageStore.saveThread`, to cap or drop
100// what it does not want to store.
101export { isSandboxToolCall } from './tool-history'
102​
103// Sandbox definition + lifecycle
104export { defineSandbox } from './sandbox'
105export type {
106 SandboxConfig,
107 SandboxDefinition,
108 SandboxEnsureContext,
109 SandboxLifecycle,
110 SandboxHooks,
111 ReuseStrategy,
112 SnapshotStrategy,
113} from './sandbox'
114​
115// Workspace
116export {
117 defineWorkspace,
118 gitSource,
119 githubRepo,
120 localSource,
121 fileSkill,
122 agentSkill,
123 mcpSkill,
124 gitSkill,
125} from './workspace'
126export type {
127 WorkspaceDefinition,
128 WorkspaceSource,
129 WorkspaceSkill,
130 PackageManager,
131 McpConfig,
132} from './workspace'
133​
134// Secrets
135export {
136 createSecrets,
137 bearer,
138 isSecretRef,
139 resolveSecret,
140 resolveBearer,
141 resolveAllSecrets,
142} from './secrets'
143export type { SecretRef, Secrets, BearerRef } from './secrets'
144​
145// Policy
146export { defineSandboxPolicy, evaluateCommand, commandAliases } from './policy'
147export type {
148 SandboxPolicy,
149 PolicyDecision,
150 CommandRules,
151 CapabilityRules,
152} from './policy'
153​
154// Provider + handle contracts
155export type {
156 SandboxProvider,
157 SandboxHandle,
158 SandboxCapabilities,
159 SandboxFs,
160 SandboxFsOptions,
161 SandboxFsStat,
162 SandboxGit,
163 SandboxProcess,
164 SandboxPorts,
165 SandboxEnv,
166 SandboxChannel,
167 SpawnHandle,
168 ExecResult,
169 ProcessOptions,
170 SnapshotRef,
171 SandboxCreateInput,
172 SandboxResumeInput,
173 SandboxRestoreInput,
174 SandboxDestroyInput,
175} from './contracts'
176​
177// Bootstrap engine (exported for provider/adapter authors + tests)
178export {
179 bootstrapWorkspace,
180 detectPackageManager,
181 DEFAULT_WORKSPACE_ROOT,
182} from './bootstrap'
183export { resolveHarnessCwd } from './harness-cwd'
184export type { BootstrapResult } from './bootstrap'
185​
186// AGENTS.md writer + gitSkill path helper (used by bootstrap + harness adapters)
187export {
188 writeAgentsFile,
189 resolveGitSkillDir,
190 discoverSkillDirs,
191 formatWorkspaceScriptsSection,
192 mergeAgentsContent,
193} from './agents-file'
194export type { DiscoveredSkillDir } from './agents-file'
195​
196// Exec-backed git helper (for providers without native git)
197export { createExecBackedGit } from './git-exec'
198​
199// Harness runner: spawn an agent CLI in a sandbox + stream NDJSON stdout
200export {
201 spawnNdjson,
202 toLines,
203 startJournaledAgent,
204 readJournalNdjson,
205} from './runner'
206export type { SpawnNdjsonOptions, JournalOptions } from './runner'
207​
208// The agent output journal: the durability boundary for a sandboxed run.
209//
210// journalListCommand/journalMtimeListCommand/parseJournalMtimeListing back the
211// journal-directory sweep (`journal-sweep.ts`); journalExitProbeCommand/
212// parseJournalExit/parseExitSentinel back the reaper's out-of-band exit probe
213// (`reap.ts`) and the streaming reader, which must agree on what "the run
214// ended" means; EXIT_SENTINEL_NONCE_KEY/exitSentinelLine are what make that
215// sentinel unforgeable by the agent's own stdout and are needed by anything
216// that seeds a journal by hand (a fake host, a test); the
217// mechanism that lets a sweep learn a detached run finished WITHOUT driving
218// it; decodeJournalRunId recovers the runId a listed filename encodes, fail
219// closed. `encodeRunId` is exported for adapters that derive their OWN
220// in-sandbox paths from a caller-supplied `runId` (prompt files, MCP bridge
221// configs) and must not hand-roll a second, divergent encoder.
222// `normalizeJournalDir` is intentionally NOT exported: it is a path
223// formatting detail of this module's own commands, not something a caller
224// composes with.
225export {
226 DEFAULT_JOURNAL_DIR,
227 encodeRunId,
228 EXIT_SENTINEL_KEY,
229 EXIT_SENTINEL_NONCE_KEY,
230 exitSentinelLine,
231 parseExitSentinel,
232 journalPaths,
233 journaledCommand,
234 journalFollowCommand,
235 journalReadCommand,
236 journalExistsCommand,
237 journalListCommand,
238 journalMtimeListCommand,
239 parseJournalMtimeListing,
240 journalExitProbeCommand,
241 parseJournalExit,
242 decodeJournalRunId,
243} from './journal'
244export type {
245 JournalPaths,
246 JournalMtimeListing,
247 JournalDirEntry,
248 DecodedJournalRunId,
249} from './journal'
250​
251// Journal-directory sweep: bound the journals a detached run's sentinel never
252// got OBSERVED for (see `journal-sweep.ts`'s module doc for why almost every
253// branch keeps rather than deletes — deleting a live run's journal makes it
254// unresumable, with no undo).
255export {
256 pruneJournals,
257 DEFAULT_ORPHAN_TTL_MS,
258 DEFAULT_MAX_DELETES,
259} from './journal-sweep'
260export type {
261 PruneJournalsOptions,
262 PruneJournalsResult,
263 KeptJournal,
264 KeptJournalReason,
265 PruneJournalsFailure,
266} from './journal-sweep'
267​
268// Detached-run reaper: sweep a `RunStore`'s reclaimable runs, driving a run to
269// terminal ONLY once the out-of-band journal probe (`probeRunExit`) already
270// knows the agent exited, or once its TTL has expired — never to find out
271// whether it finished (see `reap.ts`'s module doc for why that design was
272// rejected).
273export {
274 reapDetachedRuns,
275 probeRunExit,
276 DEFAULT_RUN_BUDGET_MS,
277 DEFAULT_MAX_RUNS,
278 DEFAULT_EXIT_PROBE_BYTES,
279} from './reap'
280export type {
281 RunExitProbe,
282 ReapRunOutcome,
283 ReapRunEntry,
284 ReapResult,
285 ReapOptions,
286} from './reap'
287​
288// Sandbox reclaim: tear down the sandbox behind a terminal run.
289// `sandboxReclaimer` adapts `reclaimSandbox` to `ReapOptions.reclaim`.
290export {
291 reclaimSandbox,
292 sandboxReclaimer,
293 SandboxReclaimFailedError,
294} from './reclaim'
295export type { ReclaimOutcome, ReclaimSandboxOptions } from './reclaim'
296export {
297 DEFAULT_JOURNAL_POLL_MS,
298 journalReadStrategy,
299 readJournal,
300} from './journal-reader'
301export type { ReadJournalOptions } from './journal-reader'
302export { decodeBase64Stream, toJournalLines } from './journal-bytes'
303export type { JournalLine } from './journal-bytes'
304export {
305 createRunScopedIdGen,
306 chunkFingerprint,
307 chunkFingerprintIgnoringThreadId,
308 chunkThreadId,
309} from './chunk-identity'
310export {
311 alignToStoredLog,
312 isBridgeCustomChunk,
313 JournalReplayDivergedError,
314 JournalReplayThreadIdMismatchError,
315 DEFAULT_MAX_OUT_OF_BAND_SKIP,
316} from './align'
317export type { AlignToStoredLogOptions } from './align'
318​
319// Attach preflight: the gate that makes a hopeless attach fail instead of
320// tailing an empty journal forever. `JournalAttachUnavailableError` and its
321// `reason` are the branchable surface (404 / 410 / 504 at an attach route), and
322// the bounded-wait default is exported because it bounds an attach REQUEST.
323export {
324 awaitAttachableJournal,
325 JournalAttachUnavailableError,
326 DEFAULT_ATTACH_JOURNAL_WAIT_MS,
327 DEFAULT_ATTACH_PROBE_INTERVAL_MS,
328} from './attach-preflight'
329export type {
330 AttachUnavailableReason,
331 AwaitAttachableJournalOptions,
332} from './attach-preflight'
333​
334// Durability seam: the `withSandbox(sandbox, { runs, durability })` option
335// shape, the capability harness adapters read back via `getSandboxDurability`,
336// and the two helpers that turn a resolved durability into the pieces a
337// harness adapter actually drives with — a `journalOptionsFor` journal option
338// and an attach-only `alignedIfAttaching` alignment transform.
339//
340// `resolveSandboxDurability` is deliberately NOT exported: it is
341// `withSandbox`'s own path from raw options to the capability payload (see
342// `middleware.ts`), and a harness adapter only ever needs the ALREADY-RESOLVED
343// value read back off the capability bus, never to re-run that resolution
344// itself.
345export {
346 SandboxDurabilityCapability,
347 getSandboxDurability,
348 provideSandboxDurability,
349 DurableAttachNotSupportedError,
350 DurableRunIdRequiredError,
351 resolveDurableRunId,
352 journalOptionsFor,
353 alignedIfAttaching,
354} from './durability'
355export type {
356 SandboxDurabilityOptions,
357 SandboxDurabilityLog,
358 SandboxRunDurability,
359} from './durability'
360​
361// Run driver: fills in core's injected takeover seams (`claim`/`pipe`) with
362// this package's single-writer claim (`claim.ts`) and run log (`run.ts`), so
363// an application wires `request`/`runs`/`locks`/`durability`/`drive` instead of
364// hand-rolling the claim/fence dance itself.
365//
366// `claim.ts`'s own primitives — `withRunClaim`, `fenceDurability`,
367// `awaitLogQuiescence`, `runDriverLockKey`, and their `RunClaim` /
368// `WithRunClaimOptions` types — are deliberately NOT exported. They are
369// exactly the "easy to get wrong" seam `sandboxRunDriver` exists to make
370// impossible (see `driver.ts`'s module doc, points 1-3), and publishing them
371// would invite the same hand-rolled fencing bugs as a supported path. The two
372// error classes below ARE exported despite that: both can surface through
373// `sandboxRunDriver` itself, so a caller needs `instanceof` to branch on them,
374// and `DEFAULT_FENCE_QUIET_MS` is exported because it is the documented
375// default for `sandboxRunDriver`'s own `fenceQuietMs` option.
376export { sandboxRunDriver, RunDriverPipeOutsideClaimError } from './driver'
377export type { SandboxRunDriverOptions } from './driver'
378export {
379 RunClaimNotAcquiredError,
380 RunClaimLostError,
381 DEFAULT_FENCE_QUIET_MS,
382} from './claim'
383​
384// MCP tool-proxy bridge (shared by harness adapters): transport-agnostic core
385// + the node:http host transport + a fetch-friendly JSON-RPC dispatcher.
386export {
387 startHostToolBridge,
388 hostForSandbox,
389 createToolBridgeCore,
390 handleBridgeJsonRpc,
391 timingSafeBearerEqual,
392 nodeHttpBridgeProvisioner,
393 BRIDGED_MCP_SERVER_NAME,
394} from './tool-bridge'
395export type {
396 HostToolBridge,
397 StartBridgeOptions,
398 ToolBridgeCore,
399 ToolBridgeCoreOptions,
400 ToolDescriptor,
401 ToolCallResult,
402 BridgePermission,
403 PermissionToolResult,
404 ToolBridgeProvisioner,
405 ToolBridgeProvisionOptions,
406 ProvisionedBridge,
407} from './tool-bridge'
408​
409// Surface bridged-tool custom events (e.g. code mode console logs) on a harness
410// adapter's live output stream.
411export { createBridgeEventChannel, mergeChunkStreams } from './bridge-events'
412export type { BridgeEventChannel } from './bridge-events'
413​
414// Host-tool delegation for the co-located ("combined") model: harness + bridge
415// run in-container; only chat()-tool EXECUTION crosses back to the orchestrator.
416export {
417 remoteToolStubs,
418 toolDescriptors,
419 httpRemoteToolExecutor,
420 executeHostTool,
421 isToolExecRequest,
422} from './remote-tools'
423export type {
424 RemoteToolExecutor,
425 RemoteToolExecuteOptions,
426 ToolExecRequest,
427} from './remote-tools'
428​
429// Run driver — pumps a chat() stream into core's `StreamDurability` and
430// records run status/lifecycle in core's `RunStore`, so a trigger returns
431// immediately while a durable orchestrator drives the run and clients tail it.
432export { pipeToRunLog, RunController } from './run'
433export type {
434 RunDeps,
435 PipeToRunLogOptions,
436 RunControllerStartInput,
437 RunHandle,
438} from './run'
439​
440// Interactive approvals (shared by harness adapters)
441export {
442 resolveApproval,
443 approvalId,
444 buildApprovalRequestedEvent,
445 APPROVAL_REQUESTED_EVENT,
446} from './approvals'
447export type { ResolveApprovalInput, ApprovalOutcome } from './approvals'
448​
449// File-event watch (low-level workspace observer)
450export { watchWorkspace, diffSnapshots } from './watch'
451export type {
452 SandboxFileEvent,
453 FileEventType,
454 WatchOptions,
455 SandboxWatchHandle,
456} from './watch'
457​
458// Keying
459export { computeSandboxKey, computeWorkspaceHash } from './key'
460export type { SandboxKeyInput } from './key'
461​
462// Errors
463export { UnsupportedCapabilityError, MissingSandboxError } from './errors'