@tanstack/ai-sandbox
Version:
Provider-agnostic sandbox layer for TanStack AI — run harness adapters inside isolated sandboxes (defineSandbox, defineWorkspace, withSandbox) with a uniform SandboxHandle, workspace bootstrap, policy, and resumable lifecycle.
45 lines (44 loc) • 1.53 kB
JavaScript
// src/snapshot-cleanup.ts
/**
* Bound for one delete call. Like teardown `destroy`, the delete is not fenced
* by the caller's signal (cleanup must outlive whatever cancelled the run), so
* this keeps a wedged provider from pinning the caller.
*/
var DELETE_SNAPSHOT_TIMEOUT_MS = 6e4;
/**
* Delete `snapshotId` through `provider.deleteSnapshot`, if the provider has
* one. Never throws: a failure is logged at warn level and swallowed, because
* a leaked snapshot must not fail the run or the teardown that dropped it.
*/
async function deleteSnapshotQuietly(provider, snapshotId, options = {}) {
const deleteSnapshot = provider.deleteSnapshot?.bind(provider);
if (snapshotId === void 0 || !deleteSnapshot) return;
const controller = new AbortController();
let timer;
const timedOut = new Promise((_, reject) => {
timer = setTimeout(() => {
const reason = /* @__PURE__ */ new Error(`deleteSnapshot timed out after ${DELETE_SNAPSHOT_TIMEOUT_MS}ms`);
controller.abort(reason);
reject(reason);
}, DELETE_SNAPSHOT_TIMEOUT_MS);
});
const deletion = Promise.resolve().then(() => deleteSnapshot({
snapshotId,
signal: controller.signal
}));
deletion.catch(() => void 0);
try {
await Promise.race([deletion, timedOut]);
} catch (error) {
options.logger?.warn("sandbox snapshot delete failed; it is now leaked", {
...options.meta,
provider: provider.name,
snapshotId,
error
});
} finally {
clearTimeout(timer);
}
}
export { deleteSnapshotQuietly };
//# sourceMappingURL=snapshot-cleanup.js.map