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

index.ts

462 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 SandboxFsStat,
161 SandboxGit,
162 SandboxProcess,
163 SandboxPorts,
164 SandboxEnv,
165 SandboxChannel,
166 SpawnHandle,
167 ExecResult,
168 ProcessOptions,
169 SnapshotRef,
170 SandboxCreateInput,
171 SandboxResumeInput,
172 SandboxRestoreInput,
173 SandboxDestroyInput,
174} from './contracts'
175​
176// Bootstrap engine (exported for provider/adapter authors + tests)
177export {
178 bootstrapWorkspace,
179 detectPackageManager,
180 DEFAULT_WORKSPACE_ROOT,
181} from './bootstrap'
182export { resolveHarnessCwd } from './harness-cwd'
183export type { BootstrapResult } from './bootstrap'
184​
185// AGENTS.md writer + gitSkill path helper (used by bootstrap + harness adapters)
186export {
187 writeAgentsFile,
188 resolveGitSkillDir,
189 discoverSkillDirs,
190 formatWorkspaceScriptsSection,
191 mergeAgentsContent,
192} from './agents-file'
193export type { DiscoveredSkillDir } from './agents-file'
194​
195// Exec-backed git helper (for providers without native git)
196export { createExecBackedGit } from './git-exec'
197​
198// Harness runner: spawn an agent CLI in a sandbox + stream NDJSON stdout
199export {
200 spawnNdjson,
201 toLines,
202 startJournaledAgent,
203 readJournalNdjson,
204} from './runner'
205export type { SpawnNdjsonOptions, JournalOptions } from './runner'
206​
207// The agent output journal: the durability boundary for a sandboxed run.
208//
209// journalListCommand/journalMtimeListCommand/parseJournalMtimeListing back the
210// journal-directory sweep (`journal-sweep.ts`); journalExitProbeCommand/
211// parseJournalExit/parseExitSentinel back the reaper's out-of-band exit probe
212// (`reap.ts`) and the streaming reader, which must agree on what "the run
213// ended" means; EXIT_SENTINEL_NONCE_KEY/exitSentinelLine are what make that
214// sentinel unforgeable by the agent's own stdout and are needed by anything
215// that seeds a journal by hand (a fake host, a test); the
216// mechanism that lets a sweep learn a detached run finished WITHOUT driving
217// it; decodeJournalRunId recovers the runId a listed filename encodes, fail
218// closed. `encodeRunId` is exported for adapters that derive their OWN
219// in-sandbox paths from a caller-supplied `runId` (prompt files, MCP bridge
220// configs) and must not hand-roll a second, divergent encoder.
221// `normalizeJournalDir` is intentionally NOT exported: it is a path
222// formatting detail of this module's own commands, not something a caller
223// composes with.
224export {
225 DEFAULT_JOURNAL_DIR,
226 encodeRunId,
227 EXIT_SENTINEL_KEY,
228 EXIT_SENTINEL_NONCE_KEY,
229 exitSentinelLine,
230 parseExitSentinel,
231 journalPaths,
232 journaledCommand,
233 journalFollowCommand,
234 journalReadCommand,
235 journalExistsCommand,
236 journalListCommand,
237 journalMtimeListCommand,
238 parseJournalMtimeListing,
239 journalExitProbeCommand,
240 parseJournalExit,
241 decodeJournalRunId,
242} from './journal'
243export type {
244 JournalPaths,
245 JournalMtimeListing,
246 JournalDirEntry,
247 DecodedJournalRunId,
248} from './journal'
249​
250// Journal-directory sweep: bound the journals a detached run's sentinel never
251// got OBSERVED for (see `journal-sweep.ts`'s module doc for why almost every
252// branch keeps rather than deletes — deleting a live run's journal makes it
253// unresumable, with no undo).
254export {
255 pruneJournals,
256 DEFAULT_ORPHAN_TTL_MS,
257 DEFAULT_MAX_DELETES,
258} from './journal-sweep'
259export type {
260 PruneJournalsOptions,
261 PruneJournalsResult,
262 KeptJournal,
263 KeptJournalReason,
264 PruneJournalsFailure,
265} from './journal-sweep'
266​
267// Detached-run reaper: sweep a `RunStore`'s reclaimable runs, driving a run to
268// terminal ONLY once the out-of-band journal probe (`probeRunExit`) already
269// knows the agent exited, or once its TTL has expired — never to find out
270// whether it finished (see `reap.ts`'s module doc for why that design was
271// rejected).
272export {
273 reapDetachedRuns,
274 probeRunExit,
275 DEFAULT_RUN_BUDGET_MS,
276 DEFAULT_MAX_RUNS,
277 DEFAULT_EXIT_PROBE_BYTES,
278} from './reap'
279export type {
280 RunExitProbe,
281 ReapRunOutcome,
282 ReapRunEntry,
283 ReapResult,
284 ReapOptions,
285} from './reap'
286​
287// Sandbox reclaim: tear down the sandbox behind a terminal run.
288// `sandboxReclaimer` adapts `reclaimSandbox` to `ReapOptions.reclaim`.
289export {
290 reclaimSandbox,
291 sandboxReclaimer,
292 SandboxReclaimFailedError,
293} from './reclaim'
294export type { ReclaimOutcome, ReclaimSandboxOptions } from './reclaim'
295export {
296 DEFAULT_JOURNAL_POLL_MS,
297 journalReadStrategy,
298 readJournal,
299} from './journal-reader'
300export type { ReadJournalOptions } from './journal-reader'
301export { decodeBase64Stream, toJournalLines } from './journal-bytes'
302export type { JournalLine } from './journal-bytes'
303export {
304 createRunScopedIdGen,
305 chunkFingerprint,
306 chunkFingerprintIgnoringThreadId,
307 chunkThreadId,
308} from './chunk-identity'
309export {
310 alignToStoredLog,
311 isBridgeCustomChunk,
312 JournalReplayDivergedError,
313 JournalReplayThreadIdMismatchError,
314 DEFAULT_MAX_OUT_OF_BAND_SKIP,
315} from './align'
316export type { AlignToStoredLogOptions } from './align'
317​
318// Attach preflight: the gate that makes a hopeless attach fail instead of
319// tailing an empty journal forever. `JournalAttachUnavailableError` and its
320// `reason` are the branchable surface (404 / 410 / 504 at an attach route), and
321// the bounded-wait default is exported because it bounds an attach REQUEST.
322export {
323 awaitAttachableJournal,
324 JournalAttachUnavailableError,
325 DEFAULT_ATTACH_JOURNAL_WAIT_MS,
326 DEFAULT_ATTACH_PROBE_INTERVAL_MS,
327} from './attach-preflight'
328export type {
329 AttachUnavailableReason,
330 AwaitAttachableJournalOptions,
331} from './attach-preflight'
332​
333// Durability seam: the `withSandbox(sandbox, { runs, durability })` option
334// shape, the capability harness adapters read back via `getSandboxDurability`,
335// and the two helpers that turn a resolved durability into the pieces a
336// harness adapter actually drives with — a `journalOptionsFor` journal option
337// and an attach-only `alignedIfAttaching` alignment transform.
338//
339// `resolveSandboxDurability` is deliberately NOT exported: it is
340// `withSandbox`'s own path from raw options to the capability payload (see
341// `middleware.ts`), and a harness adapter only ever needs the ALREADY-RESOLVED
342// value read back off the capability bus, never to re-run that resolution
343// itself.
344export {
345 SandboxDurabilityCapability,
346 getSandboxDurability,
347 provideSandboxDurability,
348 DurableAttachNotSupportedError,
349 DurableRunIdRequiredError,
350 resolveDurableRunId,
351 journalOptionsFor,
352 alignedIfAttaching,
353} from './durability'
354export type {
355 SandboxDurabilityOptions,
356 SandboxDurabilityLog,
357 SandboxRunDurability,
358} from './durability'
359​
360// Run driver: fills in core's injected takeover seams (`claim`/`pipe`) with
361// this package's single-writer claim (`claim.ts`) and run log (`run.ts`), so
362// an application wires `request`/`runs`/`locks`/`durability`/`drive` instead of
363// hand-rolling the claim/fence dance itself.
364//
365// `claim.ts`'s own primitives — `withRunClaim`, `fenceDurability`,
366// `awaitLogQuiescence`, `runDriverLockKey`, and their `RunClaim` /
367// `WithRunClaimOptions` types — are deliberately NOT exported. They are
368// exactly the "easy to get wrong" seam `sandboxRunDriver` exists to make
369// impossible (see `driver.ts`'s module doc, points 1-3), and publishing them
370// would invite the same hand-rolled fencing bugs as a supported path. The two
371// error classes below ARE exported despite that: both can surface through
372// `sandboxRunDriver` itself, so a caller needs `instanceof` to branch on them,
373// and `DEFAULT_FENCE_QUIET_MS` is exported because it is the documented
374// default for `sandboxRunDriver`'s own `fenceQuietMs` option.
375export { sandboxRunDriver, RunDriverPipeOutsideClaimError } from './driver'
376export type { SandboxRunDriverOptions } from './driver'
377export {
378 RunClaimNotAcquiredError,
379 RunClaimLostError,
380 DEFAULT_FENCE_QUIET_MS,
381} from './claim'
382​
383// MCP tool-proxy bridge (shared by harness adapters): transport-agnostic core
384// + the node:http host transport + a fetch-friendly JSON-RPC dispatcher.
385export {
386 startHostToolBridge,
387 hostForSandbox,
388 createToolBridgeCore,
389 handleBridgeJsonRpc,
390 timingSafeBearerEqual,
391 nodeHttpBridgeProvisioner,
392 BRIDGED_MCP_SERVER_NAME,
393} from './tool-bridge'
394export type {
395 HostToolBridge,
396 StartBridgeOptions,
397 ToolBridgeCore,
398 ToolBridgeCoreOptions,
399 ToolDescriptor,
400 ToolCallResult,
401 BridgePermission,
402 PermissionToolResult,
403 ToolBridgeProvisioner,
404 ToolBridgeProvisionOptions,
405 ProvisionedBridge,
406} from './tool-bridge'
407​
408// Surface bridged-tool custom events (e.g. code mode console logs) on a harness
409// adapter's live output stream.
410export { createBridgeEventChannel, mergeChunkStreams } from './bridge-events'
411export type { BridgeEventChannel } from './bridge-events'
412​
413// Host-tool delegation for the co-located ("combined") model: harness + bridge
414// run in-container; only chat()-tool EXECUTION crosses back to the orchestrator.
415export {
416 remoteToolStubs,
417 toolDescriptors,
418 httpRemoteToolExecutor,
419 executeHostTool,
420 isToolExecRequest,
421} from './remote-tools'
422export type {
423 RemoteToolExecutor,
424 RemoteToolExecuteOptions,
425 ToolExecRequest,
426} from './remote-tools'
427​
428// Run driver — pumps a chat() stream into core's `StreamDurability` and
429// records run status/lifecycle in core's `RunStore`, so a trigger returns
430// immediately while a durable orchestrator drives the run and clients tail it.
431export { pipeToRunLog, RunController } from './run'
432export type {
433 RunDeps,
434 PipeToRunLogOptions,
435 RunControllerStartInput,
436 RunHandle,
437} from './run'
438​
439// Interactive approvals (shared by harness adapters)
440export {
441 resolveApproval,
442 approvalId,
443 buildApprovalRequestedEvent,
444 APPROVAL_REQUESTED_EVENT,
445} from './approvals'
446export type { ResolveApprovalInput, ApprovalOutcome } from './approvals'
447​
448// File-event watch (low-level workspace observer)
449export { watchWorkspace, diffSnapshots } from './watch'
450export type {
451 SandboxFileEvent,
452 FileEventType,
453 WatchOptions,
454 SandboxWatchHandle,
455} from './watch'
456​
457// Keying
458export { computeSandboxKey, computeWorkspaceHash } from './key'
459export type { SandboxKeyInput } from './key'
460​
461// Errors
462export { UnsupportedCapabilityError, MissingSandboxError } from './errors'