Open History to see the last commit
1/**
2 * The "run driver" for the inverted/serverless sandbox model: pump a `chat()`
3 * stream into core's two durable seams — a {@link RunStore} for the run's
4 * lifecycle record and a {@link StreamDurability} for its event log — so a
5 * trigger can return immediately while a durable orchestrator drives the run
6 * and clients tail from an opaque offset.
7 *
8 * The key inversion vs. a classic request/response handler: there is no caller
9 * holding the stream open, so nothing to throw an error *back to*. The event log
10 * is the only channel — every chunk (including a terminal
11 * {@link EventType.RUN_ERROR}) is appended and assigned a resumable offset, and
12 * a thrown stream error is recorded as a synthesized `RUN_ERROR` event plus the
13 * record's `error` field. Tailing clients therefore always observe failures;
14 * {@link pipeToRunLog} never rejects.
15 *
16 * "Never rejects" is load-bearing rather than aspirational: {@link RunController}
17 * consumes the returned promise fire-and-forget, so a rejection would be an
18 * unhandled rejection (process-fatal on modern Node, instance-fatal inside a
19 * Durable Object) with nobody to report it to. Every store/log call is therefore
20 * individually guarded, and because absorbing a failure silently in the one
21 * module whose premise is that nobody is listening would make the failure
22 * invisible, each guard reports through the optional {@link RunDeps.logger}.
23 */
24import { EventType } from '@tanstack/ai'
25import { toRunErrorPayload } from '@tanstack/ai/adapter-internals'
26import type { InternalLogger } from '@tanstack/ai/adapter-internals'
27import type {
28 RunError,
29 RunRecord,
30 RunStore,
31 StreamChunk,
32 StreamDurability,
33 TerminalRunStatus,
34} from '@tanstack/ai'
35
36/** Whether a chunk is the terminal error event the chat engine emits. */
37function isRunErrorChunk(
38 chunk: StreamChunk,
39): chunk is StreamChunk & { message: string; code?: string } {
40 return chunk.type === EventType.RUN_ERROR
41}
42
43/**
44 * Narrow a thrown value (or a `RUN_ERROR` chunk's payload) to the record's
45 * structured error, keeping the provider's `code` when it supplies one: a bare
46 * message is prose that changes between model versions, while `code` is what a
47 * consumer branches on to retry, escalate, or show specific UI.
48 */
49function toRunError(error: unknown): RunError {
50 const payload = toRunErrorPayload(error)
51 return {
52 message: payload.message,
53 ...(payload.code === undefined ? {} : { code: payload.code }),
54 }
55}
56
57/**
58 * Fold a secondary failure into the primary error, mirroring `combineFailures`
59 * in `packages/ai/src/stream-to-response.ts`: the primary cause stays first and
60 * keeps its `code`, and the phase that produced the secondary failure is named.
61 * The secondary must never *replace* the primary: the provider's error is what
62 * an operator needs, and a failure while recording it is the lesser fact.
63 */
64function withSecondaryFailure(
65 primary: RunError,
66 secondary: unknown,
67 phase: string,
68): RunError {
69 return {
70 ...primary,
71 message: `${primary.message}; ${phase}: ${toRunError(secondary).message}`,
72 }
73}
74
75/** Build the synthetic RUN_ERROR chunk appended when the stream throws. */
76function syntheticRunError(error: RunError): StreamChunk {
77 const chunk: { type: EventType.RUN_ERROR; message: string; code?: string } = {
78 type: EventType.RUN_ERROR,
79 message: error.message,
80 ...(error.code === undefined ? {} : { code: error.code }),
81 }
82 return chunk
83}
84
85/**
86 * The two durable seams a run driver needs: lifecycle record + event log.
87 *
88 * Generic in the log's offset type, and DEFAULTED to `string` so every existing
89 * call site keeps compiling unchanged. The parameter is not decoration: a
90 * backend that brands its cursors — `@tanstack/ai-durable-stream`'s
91 * `durableStream` returns `StreamDurability<DurableStreamOffset>` — is NOT
92 * assignable to `StreamDurability<string>`, because `read` takes an offset and
93 * is therefore contravariant in it. Hardcoding the default here made the
94 * production multi-host backend unusable without a cast (see
95 * `tests/offset-generics.test-d.ts`).
96 */
97export interface RunDeps<TOffset extends string = string> {
98 /** Run lifecycle record (status, thread, timings). */
99 runs: RunStore
100 /**
101 * Per-run delivery-durable event log the run's chunks are appended to.
102 *
103 * A FACTORY, not an instance, and that is load-bearing rather than stylistic.
104 * A `StreamDurability` is bound to one run — `memoryStream(request)` resolves
105 * its `runId` from the request, and a backend adapter's offsets embed a cursor
106 * into one log. Holding a single instance made two failures reachable:
107 *
108 * - **Silent mis-binding at concurrency 1.** `start({ runId })` accepted an
109 * arbitrary id while the instance was bound to another, writing the record
110 * under one id and the events under another with no error raised. Resolving
111 * the log FROM the `runId` makes that unrepresentable.
112 * - **Cross-talk at concurrency > 1.** Parallel runs interleaved their chunks
113 * into one log, and whichever finished first called `close()` and
114 * terminalized every other run's stream.
115 *
116 * Called exactly once per run, at the start of {@link pipeToRunLog}. An
117 * implementation MUST return the same instance for the same `runId` within a
118 * process if it wants `snapshot()` to see its own appends.
119 */
120 durability: (runId: string) => StreamDurability<TOffset>
121 /**
122 * Optional sink for failures this driver absorbs rather than rejecting with.
123 * A detached run has no caller to receive an error, so without a logger a
124 * failing store or event log is invisible to an operator. Same
125 * `logger?.errors(...)` contract core uses in `stream-to-response.ts`.
126 */
127 logger?: InternalLogger
128}
129
130export interface PipeToRunLogOptions<
131 TOffset extends string = string,
132> extends RunDeps<TOffset> {
133 runId: string
134 threadId: string
135 /** Abort consumption mid-stream; the run finishes as `aborted`. */
136 signal?: AbortSignal
137}
138
139/** Everything {@link finish} needs, including the fields it rebuilds a record from. */
140interface FinishContext {
141 runs: RunStore
142 /**
143 * `close()` only — see {@link finish}. Narrowed to that one member rather than
144 * threading `TOffset` through here, because `close` is the sole method this
145 * context touches and it is offset-free, so a `Pick` accepts a log at ANY
146 * offset instantiation without making `finish` generic for nothing.
147 */
148 durability: Pick<StreamDurability, 'close'>
149 runId: string
150 threadId: string
151 startedAt: number
152 logger?: InternalLogger
153}
154
155/**
156 * Report through a consumer-supplied logger without letting it break the
157 * caller. Every logger call in this module sits inside a `catch` body, so an
158 * throwing sink would escape that body and defeat the totality the guards
159 * exist to provide. Swallowing here is deliberate: there is no second channel
160 * left to report a reporting failure on.
161 */
162function safeLog(
163 logger: InternalLogger | undefined,
164 message: string,
165 context: Record<string, unknown>,
166): void {
167 try {
168 logger?.errors(message, context)
169 } catch {
170 // Intentionally empty: see above.
171 }
172}
173
174/**
175 * Record the terminal status, terminalize the event log, and answer with the
176 * run's final record.
177 *
178 * TOTAL BY CONSTRUCTION: every step is individually guarded, so this never
179 * throws and never rejects. Two consequences the guards buy:
180 *
181 * - `durability.close()` runs on EVERY exit path, including a failed `update`.
182 * Skipping it would wedge the record at `running` *and* park every live
183 * tailer forever, because a durability `read` only ends once the log closes.
184 * - The re-read of the record is best effort. An eventually-consistent or
185 * read-replica store may answer `null` for a run that was just driven, which
186 * must not turn a successful run into a rejection; the locally rebuilt record
187 * is returned instead. It is also preferred outright when `update` failed,
188 * since the store then still holds the stale `running` row.
189 *
190 * THE TERMINAL WRITE IS NOT GUARANTEED TO LAND, and this function deliberately
191 * does not check whether it did. Under `sandboxRunDriver` the `runs` handed in is
192 * `fenceRunStore`d (`src/claim.ts`), which SUPPRESSES a terminal write — resolving
193 * without writing — when the driver has lost its claim, because a host that no
194 * longer owns the run must not declare it over while the successor is streaming
195 * it. From here that is indistinguishable from a successful write, on purpose:
196 * the epoch belongs to the claim module, not to this generic driver, and the
197 * re-read below then answers with the successor's live record, which is the
198 * truthful thing to resolve with. A driver wired without a claim (a plain
199 * `pipeToRunLog` call) is unfenced and always writes.
200 */
201async function finish(
202 ctx: FinishContext,
203 status: TerminalRunStatus,
204 error?: RunError,
205): Promise<RunRecord> {
206 // NOTE: every logger call below goes through `safeLog`. The logger is
207 // consumer-supplied and is handed arbitrary thrown values, so a sink that
208 // cannot serialize one (a circular payload, say) would otherwise throw from
209 // inside a `catch` body and escape, skipping `durability.close()` and leaving
210 // the run wedged at `'running'` with live tailers parked. The reporting
211 // channel must never be able to break the guarantee it exists to report on.
212 const { runs, durability, runId, logger } = ctx
213 const finishedAt = Date.now()
214 const patch = {
215 status,
216 finishedAt,
217 ...(error === undefined ? {} : { error }),
218 }
219 const local: RunRecord = {
220 runId,
221 threadId: ctx.threadId,
222 startedAt: ctx.startedAt,
223 ...patch,
224 }
225
226 let recorded = true
227 try {
228 await runs.update(runId, patch)
229 } catch (updateError) {
230 recorded = false
231 safeLog(logger, 'run: recording the terminal run record failed', {
232 runId,
233 status,
234 error: updateError,
235 })
236 }
237
238 try {
239 await durability.close()
240 } catch (closeError) {
241 safeLog(logger, 'run: closing the run event log failed', {
242 runId,
243 status,
244 error: closeError,
245 })
246 }
247
248 if (!recorded) return local
249
250 try {
251 const latest = await runs.get(runId)
252 if (latest !== null) return latest
253 safeLog(logger, 'run: record vanished before the terminal re-read', {
254 runId,
255 status,
256 })
257 } catch (getError) {
258 safeLog(logger, 'run: re-reading the terminal run record failed', {
259 runId,
260 status,
261 error: getError,
262 })
263 }
264 return local
265}
266
267/**
268 * Open the run, append every chunk from `stream`, and finish with the right
269 * terminal status. Resolves with the final {@link RunRecord} and never rejects:
270 * a thrown stream error is surfaced as a `RUN_ERROR` event plus the record's
271 * `error`, which is what tailing clients see. A store or event-log failure
272 * along the way is logged through {@link RunDeps.logger} and still terminalizes
273 * the run rather than escaping to a caller that does not exist.
274 *
275 * - normal completion → `completed`
276 * - a `RUN_ERROR` chunk → append it, then `failed`
277 * - the stream throws → append a synthesized `RUN_ERROR`, then `failed`
278 * - `signal` aborts at ANY point before the stream ends → `aborted`, whether the
279 * producer keeps yielding, ends its stream, or is never asked for another
280 * chunk. An abort outranks a clean exit: the run did not complete.
281 */
282export async function pipeToRunLog<TOffset extends string = string>(
283 stream: AsyncIterable<StreamChunk>,
284 opts: PipeToRunLogOptions<TOffset>,
285): Promise<RunRecord> {
286 const { runs, runId, threadId, signal, logger } = opts
287 // Resolved ONCE, from the runId being driven. Everything below — including
288 // `finish`'s `close()` — uses this one instance, so a factory that mints a
289 // fresh log per call cannot split one run across two logs, and the log can
290 // never belong to a run other than the one whose record is being written.
291 const durability = opts.durability(runId)
292 const ctx: FinishContext = {
293 runs,
294 durability,
295 runId,
296 threadId,
297 startedAt: Date.now(),
298 ...(logger === undefined ? {} : { logger }),
299 }
300
301 try {
302 // Inside the `try` so a store failure at creation is handled like any
303 // other: recorded as a failed run with a terminalized log, not rejected.
304 await runs.createOrResume({ runId, threadId, startedAt: ctx.startedAt })
305 if (signal?.aborted) return finish(ctx, 'aborted')
306
307 for await (const chunk of stream) {
308 if (signal?.aborted) return finish(ctx, 'aborted')
309 await durability.append([chunk])
310 if (isRunErrorChunk(chunk)) {
311 return finish(
312 ctx,
313 'failed',
314 toRunError({ message: chunk.message, code: chunk.code }),
315 )
316 }
317 }
318 } catch (streamError) {
319 // Detached run: no caller to throw to. Record the failure in the log so
320 // tailing clients observe it, then return — do NOT rethrow.
321 let recorded = toRunError(streamError)
322 // Deliberately not "the stream failed": `runs.createOrResume` above is
323 // inside this `try`, so an operator reading a wedged run must not be told
324 // the provider stream broke when the store never let the run start.
325 safeLog(logger, 'run: the run failed before completing', {
326 runId,
327 error: streamError,
328 })
329 try {
330 await durability.append([syntheticRunError(recorded)])
331 } catch (appendError) {
332 // The recovery append is itself a failure path. It must not destroy the
333 // cause it was recording, so the provider's error stays primary and this
334 // secondary failure is merged in and logged separately. When the cause IS
335 // a lost claim, this append is refused too and the `finish` below writes
336 // nothing either — a fenced store suppresses the terminal record for the
337 // same reason the fenced log refused the chunk (see `finish`).
338 const phase = 'appending the synthesized RUN_ERROR failed'
339 safeLog(logger, `run: ${phase}`, { runId, error: appendError })
340 recorded = withSecondaryFailure(recorded, appendError, phase)
341 }
342 return finish(ctx, 'failed', recorded)
343 }
344
345 // RE-CHECKED after the loop, and this is the common shape rather than the
346 // exotic one. The in-loop check only fires if the producer yields at least
347 // once MORE after the abort; two ways past it are routine:
348 //
349 // - The producer is signal-aware and reacts by ENDING its stream. `chat()`
350 // does exactly this, so the loop exits NORMALLY.
351 // - The abort lands BETWEEN two chunks, while the loop is suspended on a
352 // producer that then finishes on its own.
353 //
354 // Falling through to `'completed'` in either case is a false transcript, not
355 // a cosmetic mislabel: it was measured on the reaper's TTL-expiry path, where
356 // a run the reaper had force-expired — and whose sandbox it had already
357 // destroyed — was recorded as having completed successfully. Any caller whose
358 // producer ends its stream on abort reaches the same gap, a takeover that
359 // loses its claim mid-drive included, which is why the check belongs here and
360 // not in one caller.
361 //
362 // A producer that THREW on the abort is deliberately untouched: it returned
363 // from inside the `catch` above as `'failed'`, because a thrown value is a
364 // reported failure a tailing client must be shown, and this driver's log is
365 // that client's only channel.
366 if (signal?.aborted) return finish(ctx, 'aborted')
367 return finish(ctx, 'completed')
368}
369
370export interface RunControllerStartInput {
371 runId: string
372 threadId: string
373 stream: AsyncIterable<StreamChunk>
374 /** Abort consumption mid-stream; the run finishes as `aborted`. */
375 signal?: AbortSignal
376}
377
378export interface RunHandle {
379 runId: string
380 /** Resolves with the final record once the run reaches a terminal status. */
381 done: Promise<RunRecord>
382}
383
384/**
385 * Thin orchestration helper over {@link RunDeps}: fire-and-track a run via
386 * {@link pipeToRunLog}, tail one run by id, and `drain()` all in-flight runs
387 * (e.g. inside a `ctx.waitUntil`). Holds no run state of its own beyond the set
388 * of currently in-flight `done` promises.
389 *
390 * Safe for concurrent runs. {@link RunDeps.durability} is a per-run factory, so
391 * each run appends to its own log and no run's `close()` terminalizes another's.
392 * The identity trap this class used to document — `start({ runId })` writing the
393 * lifecycle record under one id and the events under another, silently and at
394 * concurrency 1 — is unrepresentable now that the log is resolved FROM the
395 * `runId`. Every method is keyed by run accordingly: `attach(runId, …)` and
396 * `status(runId)` no longer disagree about whether the surface is per-run.
397 */
398export class RunController<TOffset extends string = string> {
399 private readonly inFlight = new Set<Promise<RunRecord>>()
400
401 constructor(private readonly deps: RunDeps<TOffset>) {}
402
403 /**
404 * Kick off `pipeToRunLog` without awaiting it and return the `runId`
405 * immediately plus a `done` promise the orchestrator may await or detach.
406 */
407 start(input: RunControllerStartInput): RunHandle {
408 const done = pipeToRunLog(input.stream, {
409 ...this.deps,
410 runId: input.runId,
411 threadId: input.threadId,
412 ...(input.signal !== undefined ? { signal: input.signal } : {}),
413 })
414 this.inFlight.add(done)
415 // Two-argument `then`, deliberately NOT `.finally`: `.finally` returns a new
416 // promise that adopts any rejection, and discarding that promise would make
417 // the rejection unhandled (fatal on modern Node defaults, and it kills the
418 // instance inside a Durable Object). Handling both outcomes here means the
419 // derived promise always settles fulfilled, so nothing is left unhandled
420 // even if `pipeToRunLog`'s "never rejects" contract is ever broken.
421 const forget = (): void => void this.inFlight.delete(done)
422 void done.then(forget, forget)
423 return { runId: input.runId, done }
424 }
425
426 /**
427 * Resumable client tail for ONE run — replay from `fromOffset`, then
428 * live-tail. Takes `runId` because the log it reads is per-run; the old
429 * `attach(fromOffset)` signature advertised a multi-run surface the type could
430 * not deliver.
431 */
432 attach(
433 runId: string,
434 fromOffset: TOffset,
435 signal?: AbortSignal,
436 ): AsyncIterable<{ offset: TOffset; chunk: StreamChunk }> {
437 return this.deps.durability(runId).read(fromOffset, signal)
438 }
439
440 /** Current run record, or null when the run is unknown. */
441 status(runId: string): Promise<RunRecord | null> {
442 return this.deps.runs.get(runId)
443 }
444
445 /**
446 * Await every currently in-flight run's `done` promise.
447 *
448 * Uses `allSettled` rather than `all` because this is typically awaited
449 * inside a `ctx.waitUntil`: `all` would reject on the first failure, abandon
450 * the wait on every other run, and surface that rejection to the platform.
451 * Draining is about keeping the isolate alive until the runs settle; each
452 * run's own outcome is already recorded in its record and log.
453 */
454 async drain(): Promise<void> {
455 await Promise.allSettled([...this.inFlight])
456 }
457}