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

reclaim.ts

191 lines8.3 KB

1/**
2 * Tear down the sandbox behind a terminal run.
3 *
4 * `RunRecord.sandboxKey` exists so this does not have to re-derive the compound
5 * key: `definition.key(ctx)` folds in the thread, the workspace hash, the tenant
6 * and the reuse strategy, and the reaper has none of those. Phase 3's detach path
7 * records the key at the moment it still knows it.
8 *
9 * DELIBERATELY NOT AN ENUMERATION. `SandboxInstanceStore` is `get`/`upsert`/
10 * `delete` only, and no `list` is added: it would force every backend and the
11 * conformance suite to grow an enumeration for one hypothetical caller. The
12 * consequence is real and documented rather than hidden — a sandbox whose run
13 * record was deleted before a sweep saw it is unreachable from here and leaks
14 * until the provider's own idle reclamation takes it.
15 */
16import type { InternalLogger } from '@tanstack/ai/adapter-internals'
17import type { RunRecord } from '@tanstack/ai'
18import type { SandboxProvider } from './contracts'
19import type { SandboxInstanceStore } from './instance-store'
20​
21export interface ReclaimSandboxOptions {
22 provider: SandboxProvider
23 instances: SandboxInstanceStore
24 logger?: InternalLogger
26​
27export type ReclaimOutcome =
28 /** The provider was asked to destroy it and the instance record is gone. */
29 | 'destroyed'
30 /**
31 * The provider's `destroy` THREW. The instance record was still deleted (see
32 * the ordering note on {@link reclaimSandbox}), so the sandbox — if it is in
33 * fact still running — is now unreachable from here and bills until the
34 * provider's own idle reclamation, if any.
35 *
36 * This is the one outcome that means the cost leak the reaper exists to stop
37 * is still leaking, so it is reported distinctly instead of being folded into
38 * `'destroyed'`, and {@link sandboxReclaimer} logs it above debug level.
39 */
40 | 'destroy-failed'
41 /** The run never ran in a sandbox. */
42 | 'no-sandbox-key'
43 /** No instance record for that key; nothing to do. */
44 | 'not-found'
45 /** The record belongs to a different provider; refused. */
46 | 'provider-mismatch'
47​
48/**
49 * Destroy the sandbox a terminal run was bound to.
50 *
51 * Two orderings are load-bearing:
52 *
53 * - **The provider check before either `destroy` or `delete`.** A multi-provider
54 * application would otherwise hand a Docker container id to Daytona's
55 * `destroy`, which at best errors and at worst matches an unrelated sandbox
56 * in the other provider's id namespace. Getting this wrong destroys a
57 * stranger's workload, so it is the first gate — a mismatch touches NOTHING,
58 * including the record, which the right provider still needs.
59 * - **`destroy` before `delete`, and `delete` regardless of whether `destroy`
60 * succeeded.** The provider sandbox may already be gone (idle-reclaimed, the
61 * region wiped, the container pruned). Keeping an instance record that points
62 * at nothing guarantees a failed `resume` on the thread's next turn, which is
63 * strictly worse than an orphaned provider sandbox — one is a broken user
64 * experience, the other is a bounded cost the provider itself will reclaim.
65 * The delete is therefore unconditional — but a failed `destroy` returns
66 * `'destroy-failed'`, not `'destroyed'`: the record is gone either way, and an
67 * operator has to be able to tell "torn down" from "possibly still billing and
68 * no longer reachable from here".
69 */
70export async function reclaimSandbox(
71 record: RunRecord,
72 options: ReclaimSandboxOptions,
73): Promise<ReclaimOutcome> {
74 const key = record.sandboxKey
75 if (key === undefined) return 'no-sandbox-key'
76​
77 // NOT guarded: a store failure here means we do not know what to destroy, and
78 // the caller (the reaper) records it against the run. Swallowing it would hide
79 // a leaking sandbox entirely.
80 const instance = await options.instances.get(key)
81 if (instance === null) return 'not-found'
82​
83 if (instance.provider !== options.provider.name) {
84 options.logger?.warn(
85 'reclaim: instance record belongs to a different provider; refusing to destroy',
86 {
87 runId: record.runId,
88 sandboxKey: key,
89 recordProvider: instance.provider,
90 reclaimerProvider: options.provider.name,
91 },
92 )
93 return 'provider-mismatch'
94 }
95​
96 let destroyFailed = false
97 try {
98 await options.provider.destroy({ id: instance.providerSandboxId })
99 } catch (error) {
100 destroyFailed = true
101 options.logger?.warn(
102 'reclaim: provider destroy failed; deleting the record anyway',
103 {
104 runId: record.runId,
105 sandboxKey: key,
106 providerSandboxId: instance.providerSandboxId,
107 error,
108 },
109 )
110 }
111 // Unconditional, per the ordering note above — but the OUTCOME must not claim
112 // success when the destroy threw. Reporting `'destroyed'` here made a leaked,
113 // now-unreachable sandbox indistinguishable from a clean teardown.
114 await options.instances.delete(key)
115 return destroyFailed ? 'destroy-failed' : 'destroyed'
117​
118/**
119 * Thrown by {@link sandboxReclaimer} when {@link reclaimSandbox} answers
120 * `'destroy-failed'`.
121 *
122 * WHY AN EXCEPTION AND NOT A RETURN VALUE. `ReapOptions.reclaim` is
123 * `(record) => Promise<void>`, and the sweep's only channel for "the sandbox was
124 * NOT reclaimed" is a rejection — `reapOne` catches one and reports the run
125 * `'reclaim-failed'` with its `status` and `exitCode` intact. A reclaimer that
126 * logged this arm and returned normally therefore reported `'finalized'`, and
127 * `outcomes['reclaim-failed']` read `0` on precisely the leak it watches for.
128 *
129 * It carries no `cause`: `reclaimSandbox` returns a {@link ReclaimOutcome}, not
130 * the provider's rejection, and widening that return to smuggle the error out
131 * would change an outcome contract whose ordering and arms are load-bearing. The
132 * underlying `destroy` rejection is on `reclaimSandbox`'s own `warn` line, which
133 * carries the same `runId` and `sandboxKey` this error does.
134 */
135export class SandboxReclaimFailedError extends Error {
136 readonly runId: string
137 /** Absent only in the impossible case; see the throw site in `sandboxReclaimer`. */
138 readonly sandboxKey: string | undefined
139​
140 constructor(runId: string, sandboxKey: string | undefined) {
141 super(
142 `Reclaiming the sandbox for run "${runId}" failed: the provider's destroy rejected and the instance record${
143 sandboxKey === undefined ? '' : ` for "${sandboxKey}"`
144 } was deleted anyway, so the sandbox may still be running and is no longer reachable from the instance store.`,
145 )
146 this.name = 'SandboxReclaimFailedError'
147 this.runId = runId
148 this.sandboxKey = sandboxKey
149 }
151​
152/**
153 * Adapt {@link reclaimSandbox} to `ReapOptions.reclaim`.
154 *
155 * REJECTS on `'destroy-failed'` — see {@link SandboxReclaimFailedError} for why
156 * that arm must not resolve. Every other outcome resolves: `'destroyed'` did the
157 * job, and `'no-sandbox-key'` / `'not-found'` / `'provider-mismatch'` all mean
158 * there is nothing for this reclaimer to tear down, which is not a sweep failure.
159 */
160export function sandboxReclaimer(
161 options: ReclaimSandboxOptions,
162): (record: RunRecord) => Promise<void> {
163 return async (record) => {
164 const outcome = await reclaimSandbox(record, options)
165 const meta = {
166 runId: record.runId,
167 ...(record.sandboxKey === undefined
168 ? {}
169 : { sandboxKey: record.sandboxKey }),
170 }
171 if (outcome === 'destroy-failed') {
172 // ABOVE DEBUG DELIBERATELY. Every other outcome is bookkeeping an operator
173 // never needs to see; this one says a billed sandbox may still be running
174 // with its only lookup row deleted, which nothing downstream will retry.
175 options.logger?.errors(
176 'reclaim: destroy failed; sandbox may still be running',
177 meta,
178 )
179 // AND THEN THROWS, so the sweep's `'reclaim-failed'` outcome is reachable
180 // through the shipped reclaimer and not only through a custom one. The log
181 // line alone is invisible to a `ReapResult` consumer.
182 //
183 // `record.sandboxKey` is defined on this arm — `reclaimSandbox` answers
184 // `'no-sandbox-key'` before it ever reaches `destroy` otherwise — so this
185 // is passed through rather than asserted: a non-null assertion is banned
186 // here, and inventing a placeholder key would put a fake id in the message.
187 throw new SandboxReclaimFailedError(record.runId, record.sandboxKey)
188 }
189 options.logger?.sandbox(`reclaim: ${outcome}`, meta)
190 }