Open History to see the last commit
1---
2id: RunStore
3title: RunStore
4---
5
6Defined in: [packages/ai/src/activities/chat/middleware/run-store.ts:245](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/middleware/run-store.ts#L245)
7
8Durable store for run lifecycle records.
9
10REQUIRED: `createOrResume`, `update`, `get`, `findActiveRun`. Every backend
11must implement all four — they are what the persistence middleware calls
12unconditionally. `findActiveRun` is required rather than feature-detected
13because a backend that has not implemented it is indistinguishable from one
14whose answer is legitimately `null`, so reconnect would silently do nothing
15instead of failing at build time. It was optional for exactly one release
16cycle and cost precisely that.
17
18OPTIONAL: `listByThread`, `listByParentRun`, `listReclaimable`. Each serves
19one higher-level feature (thread history, subagent card reload, reclaim
20reaping) and callers feature-detect them, degrading when a backend omits
21them.
22
23## Properties
24
25### createOrResume
26
27```ts
28createOrResume: (input) => Promise<RunRecord>;
29```
30
31Defined in: [packages/ai/src/activities/chat/middleware/run-store.ts:256](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/middleware/run-store.ts#L256)
32
33Create a run record, or return the existing one unchanged if `runId` is
34already present.
35
36INVARIANT (idempotency): an existing record is returned **unchanged** and
37the passed `threadId`, `startedAt`, `status`, `parentRunId`,
38`subagentRunId`, and `name` are ignored. This is what makes resuming a
39run safe. `status` defaults to `'running'` on first creation. The three
40link fields are copied only on the first insert.
41
42#### Parameters
43
44##### input
45
46`Pick`\<[`RunRecord`](RunRecord.md), `"threadId"` \| `"runId"` \| `"startedAt"`\> & `object` & `Partial`\<`Pick`\<[`RunRecord`](RunRecord.md), `"activity"` \| `"kind"` \| `"agent"` \| `"principal"`\>\>
47
48#### Returns
49
50`Promise`\<[`RunRecord`](RunRecord.md)\>
51
52***
53
54### findActiveRun
55
56```ts
57findActiveRun: (threadId) => Promise<RunRecord | null>;
58```
59
60Defined in: [packages/ai/src/activities/chat/middleware/run-store.ts:340](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/middleware/run-store.ts#L340)
61
62The most recent `'running'` run for `threadId`, or `null` if none is active.
63
64REQUIRED. This resolves "does this thread have a live run to attach to?"
65from the STABLE thread id, which is the durable basis for reconnecting a
66client (a reload, or the same thread opened on another device) — independent
67of the ephemeral run id, which a single turn may mint several of. When more
68than one run is `'running'`, the one with the greatest `startedAt` wins.
69
70A backend that stubs this to `null` turns reconnect off silently, because
71`null` is also the correct answer for an idle thread. A backend with no run
72lifecycle at all should omit the whole `runs` store instead — capability
73tiers belong at the store level, not the method level.
74
75#### Parameters
76
77##### threadId
78
79`string`
80
81#### Returns
82
83`Promise`\<[`RunRecord`](RunRecord.md) \| `null`\>
84
85***
86
87### get
88
89```ts
90get: (runId) => Promise<RunRecord | null>;
91```
92
93Defined in: [packages/ai/src/activities/chat/middleware/run-store.ts:292](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/middleware/run-store.ts#L292)
94
95Current record, or null when unknown.
96
97#### Parameters
98
99##### runId
100
101`string`
102
103#### Returns
104
105`Promise`\<[`RunRecord`](RunRecord.md) \| `null`\>
106
107***
108
109### listByParentRun?
110
111```ts
112optional listByParentRun?: (parentRunId) => Promise<RunRecord[]>;
113```
114
115Defined in: [packages/ai/src/activities/chat/middleware/run-store.ts:306](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/middleware/run-store.ts#L306)
116
117Child runs started by `parentRunId`, ascending by `startedAt`.
118OPTIONAL. `reconstructChat` uses this to put subagent cards back
119on the parent assistant message. A store that omits it reloads the
120text and not the cards.
121
122#### Parameters
123
124##### parentRunId
125
126`string`
127
128#### Returns
129
130`Promise`\<[`RunRecord`](RunRecord.md)[]\>
131
132***
133
134### listByThread?
135
136```ts
137optional listByThread?: (threadId) => Promise<RunRecord[]>;
138```
139
140Defined in: [packages/ai/src/activities/chat/middleware/run-store.ts:299](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/middleware/run-store.ts#L299)
141
142Every run in a conversation, ascending by `startedAt`. OPTIONAL.
143`reconstructChat` calls it to find the parent runs of children that a
144tool call started. Without it those cards stay absent on reload.
145Consumers feature-detect.
146
147#### Parameters
148
149##### threadId
150
151`string`
152
153#### Returns
154
155`Promise`\<[`RunRecord`](RunRecord.md)[]\>
156
157***
158
159### listReclaimable?
160
161```ts
162optional listReclaimable?: (opts) => Promise<RunRecord[]>;
163```
164
165Defined in: [packages/ai/src/activities/chat/middleware/run-store.ts:322](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/middleware/run-store.ts#L322)
166
167Runs that may be reclaimed: ALL THREE of `status === 'running'`,
168`detachedSince` is set, and `detachedSince <= now - ttlMs`. The cutoff is
169**inclusive** — a run detached at exactly `now - ttlMs` IS reclaimable.
170
171OPTIONAL: only needed by a reaper. Consumers feature-detect.
172
173`detachedSince` is populated by `withSandbox`'s detach path (see
174[RunRecord.detachedSince](RunRecord.md#detachedsince)). The sweep over the candidates this
175surfaces is `@tanstack/ai-sandbox`'s `reapDetachedRuns`: it finalizes a run
176whose agent already finished, expires one past its TTL, and reclaims the
177sandbox. That is a function, not a scheduler — the application invokes it
178(cron, queue, `alarm()`, `waitUntil`) — and a backend that omits this
179method cannot be reaped at all.
180
181#### Parameters
182
183##### opts
184
185###### now
186
187`number`
188
189###### ttlMs
190
191`number`
192
193#### Returns
194
195`Promise`\<[`RunRecord`](RunRecord.md)[]\>
196
197***
198
199### update
200
201```ts
202update: (runId, patch) => Promise<void>;
203```
204
205Defined in: [packages/ai/src/activities/chat/middleware/run-store.ts:270](https://github.com/TanStack/ai/blob/main/packages/ai/src/activities/chat/middleware/run-store.ts#L270)
206
207Patch a record's mutable fields.
208
209INVARIANT: updating an unknown `runId` is a **no-op** — it must not throw
210and must not create a record.
211
212#### Parameters
213
214##### runId
215
216`string`
217
218##### patch
219
220`Partial`\<`Pick`\<[`RunRecord`](RunRecord.md),
221 \| `"status"`
222 \| `"finishedAt"`
223 \| `"error"`
224 \| `"usage"`
225 \| `"sandboxKey"`
226 \| `"detachedSince"`
227 \| `"cancelRequested"`
228 \| `"driverEpoch"`
229 \| `"result"`
230 \| `"artifacts"`
231 \| `"leaseOwner"`
232 \| `"leaseExpiresAt"`
233 \| `"checkpoint"`\>\>
234
235#### Returns
236
237`Promise`\<`void`\>