Open History to see the last commit
1/**
2 * Conformance for the DURABLE-RUN fields on a `RunStore`.
3 *
4 * These four fields (`sandboxKey`, `detachedSince`, `cancelRequested`,
5 * `driverEpoch`) exist for durable sandboxed runs: detach on disconnect, takeover
6 * by a later host, and the reaper. A chat-only app never writes them, so proving
7 * them is NOT part of `runPersistenceConformance` in `@tanstack/ai-persistence`.
8 * They live here, next to the takeover and reaper suites that depend on them.
9 *
10 * Run this when your app wires `withSandbox(sandbox, { runs, durability })`. The
11 * fields round-trip through the REQUIRED `update`/`get` pair, so a backend can
12 * pass every persistence case while silently dropping one of them, and the
13 * failure then shows up as a run that looks permanently detached or a takeover
14 * that cannot fence a superseded host.
15 *
16 * ```ts
17 * import { runDurableRunFieldsConformance } from '@tanstack/ai-sandbox/testkit'
18 * import { myPersistence } from './persistence'
19 *
20 * runDurableRunFieldsConformance('my postgres runs', () => myPersistence().stores.runs)
21 * ```
22 */
23import { describe, expect, it } from 'vitest'
24import type { RunStore } from '@tanstack/ai'
25
26/** Factory for the store under test. A fresh one per case keeps them isolated. */
27export type MakeRunStore = () => RunStore | Promise<RunStore>
28
29export function runDurableRunFieldsConformance(
30 name: string,
31 makeStore: MakeRunStore,
32): void {
33 describe(`durable run fields conformance: ${name}`, () => {
34 // One case, because the four fields share one failure mode: a backend that
35 // filters `undefined` out of its `SET` clause, or coerces an absent column to
36 // a falsy default, passes every other assertion while breaking detach and
37 // takeover. Splitting it per field would hide that they must all behave the
38 // same way through one `update`.
39 it('round-trips the durable run fields, overwrites driverEpoch, and clears every one of them on explicit undefined', async () => {
40 const store = await makeStore()
41
42 await store.createOrResume({
43 runId: 'fc-1',
44 threadId: 'fc-t',
45 startedAt: 1,
46 })
47
48 // 0. A fresh run that was never patched with these fields must read
49 // back as undefined -- not null, not false, not 0. A backend that
50 // coerces a NULL/absent column to a falsy default (e.g.
51 // `cancelRequested: false`) is claiming knowledge ("explicitly not
52 // cancelled") it does not have, and `toBeFalsy()` would not catch
53 // it since `false` is falsy too.
54 const fresh = await store.get('fc-1')
55 expect(fresh?.cancelRequested).toBeUndefined()
56 expect(fresh?.detachedSince).toBeUndefined()
57 expect(fresh?.sandboxKey).toBeUndefined()
58 expect(fresh?.driverEpoch).toBeUndefined()
59
60 // 1. All four fields round-trip through update -> get.
61 await store.update('fc-1', {
62 sandboxKey: 'sandbox-abc',
63 detachedSince: 500,
64 cancelRequested: true,
65 driverEpoch: 1,
66 })
67 const afterFirstUpdate = await store.get('fc-1')
68 expect(afterFirstUpdate?.sandboxKey).toBe('sandbox-abc')
69 expect(afterFirstUpdate?.detachedSince).toBe(500)
70 expect(afterFirstUpdate?.cancelRequested).toBe(true)
71 expect(afterFirstUpdate?.driverEpoch).toBe(1)
72
73 // 2. A monotonic driverEpoch bump overwrites, it is not ignored (a
74 // takeover host bumping the fencing token must actually stick).
75 await store.update('fc-1', { driverEpoch: 2 })
76 const afterEpochBump = await store.get('fc-1')
77 expect(afterEpochBump?.driverEpoch).toBe(2)
78 // Sibling fields untouched by an update that only names driverEpoch.
79 expect(afterEpochBump?.sandboxKey).toBe('sandbox-abc')
80 expect(afterEpochBump?.cancelRequested).toBe(true)
81
82 // 3. update({ detachedSince: undefined }) actually CLEARS the field.
83 // A backend whose SQL adapter filters `undefined` out of its `SET`
84 // clause leaves the old value, and every re-attached run then looks
85 // permanently detached to the reaper.
86 await store.update('fc-1', { detachedSince: undefined })
87 const afterClear = await store.get('fc-1')
88 expect(afterClear?.detachedSince).toBeUndefined()
89 // Clearing detachedSince must not clobber the other durable fields.
90 expect(afterClear?.sandboxKey).toBe('sandbox-abc')
91 expect(afterClear?.cancelRequested).toBe(true)
92 expect(afterClear?.driverEpoch).toBe(2)
93
94 // 4. cancelRequested: false written EXPLICITLY must round-trip as
95 // `false`, distinct from the fresh-run `undefined` checked in step 0.
96 // A backend storing this boolean in an integer/NULL-able column has
97 // to preserve the false/undefined distinction in both directions,
98 // not just collapse both to falsy.
99 await store.update('fc-1', { cancelRequested: false })
100 const afterExplicitFalse = await store.get('fc-1')
101 expect(afterExplicitFalse?.cancelRequested).toBe(false)
102 expect(afterExplicitFalse?.cancelRequested).not.toBeUndefined()
103
104 // 5. An explicit `undefined` clears EVERY durable field, not just
105 // `detachedSince`. Step 3 only exercised one of the four, so a backend
106 // half-converted to `'field' in patch` -- `in` for `detachedSince`,
107 // still `patch.field !== undefined` for the rest -- passed the whole
108 // suite while its clears silently no-opped. Step 4's explicit `false`
109 // also survives a `!== undefined` guard, so nothing else here bites
110 // either. Re-populate first, so each clear has a value to remove and
111 // an assertion that fails when the clear is dropped.
112 await store.update('fc-1', {
113 sandboxKey: 'sandbox-xyz',
114 detachedSince: 900,
115 cancelRequested: true,
116 driverEpoch: 3,
117 })
118 const beforeFullClear = await store.get('fc-1')
119 expect(beforeFullClear?.sandboxKey).toBe('sandbox-xyz')
120 expect(beforeFullClear?.detachedSince).toBe(900)
121 expect(beforeFullClear?.cancelRequested).toBe(true)
122 expect(beforeFullClear?.driverEpoch).toBe(3)
123
124 await store.update('fc-1', {
125 sandboxKey: undefined,
126 detachedSince: undefined,
127 cancelRequested: undefined,
128 driverEpoch: undefined,
129 })
130 const afterFullClear = await store.get('fc-1')
131 expect(afterFullClear?.sandboxKey).toBeUndefined()
132 expect(afterFullClear?.detachedSince).toBeUndefined()
133 expect(afterFullClear?.cancelRequested).toBeUndefined()
134 expect(afterFullClear?.driverEpoch).toBeUndefined()
135 // Clearing the durable fields is not a delete: the run row survives,
136 // and the fields the patch never named keep their values.
137 expect(afterFullClear?.status).toBe('running')
138 expect(afterFullClear?.startedAt).toBe(1)
139 })
140
141 // `findActiveRun` is REQUIRED on the RunStore contract — every backend that
142 // provides a `runs` store must satisfy these invariants (most-recent-running
143 // wins, thread-scoped, null when idle). Reconnect is built on it, and a
144 // backend that always answers `null` disables reconnect indistinguishably
145 // from one that is merely idle, so this must never degrade to a skip.
146 })
147}