@namzu/sdk 20.0.0 → 20.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +108 -0
- package/dist/contracts/a2a.d.ts +2 -2
- package/dist/manager/run/persistence.d.ts.map +1 -1
- package/dist/manager/run/persistence.js +11 -0
- package/dist/manager/run/persistence.js.map +1 -1
- package/dist/public-runtime.d.ts +3 -1
- package/dist/public-runtime.d.ts.map +1 -1
- package/dist/public-runtime.js +7 -1
- package/dist/public-runtime.js.map +1 -1
- package/dist/runtime/query/checkpoint.d.ts +20 -0
- package/dist/runtime/query/checkpoint.d.ts.map +1 -1
- package/dist/runtime/query/checkpoint.js +47 -0
- package/dist/runtime/query/checkpoint.js.map +1 -1
- package/dist/store/index.d.ts +3 -0
- package/dist/store/index.d.ts.map +1 -1
- package/dist/store/index.js +12 -0
- package/dist/store/index.js.map +1 -1
- package/dist/store/run/checkpoint-disk.d.ts +56 -2
- package/dist/store/run/checkpoint-disk.d.ts.map +1 -1
- package/dist/store/run/checkpoint-disk.js +83 -2
- package/dist/store/run/checkpoint-disk.js.map +1 -1
- package/dist/store/run/checkpoint-memory.d.ts +31 -0
- package/dist/store/run/checkpoint-memory.d.ts.map +1 -0
- package/dist/store/run/checkpoint-memory.js +83 -0
- package/dist/store/run/checkpoint-memory.js.map +1 -0
- package/dist/store/run/disk.d.ts +53 -0
- package/dist/store/run/disk.d.ts.map +1 -1
- package/dist/store/run/disk.js +73 -30
- package/dist/store/run/disk.js.map +1 -1
- package/dist/store/run/listing.d.ts +75 -0
- package/dist/store/run/listing.d.ts.map +1 -0
- package/dist/store/run/listing.js +242 -0
- package/dist/store/run/listing.js.map +1 -0
- package/dist/types/hitl/index.d.ts +27 -0
- package/dist/types/hitl/index.d.ts.map +1 -1
- package/dist/types/hitl/index.js.map +1 -1
- package/dist/types/run/checkpoint-store.d.ts +260 -1
- package/dist/types/run/checkpoint-store.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/manager/run/persistence.ts +18 -4
- package/src/public-runtime.ts +13 -0
- package/src/runtime/query/checkpoint.ts +51 -0
- package/src/store/index.ts +18 -0
- package/src/store/run/checkpoint-disk.ts +128 -4
- package/src/store/run/checkpoint-memory.ts +101 -0
- package/src/store/run/disk.ts +72 -27
- package/src/store/run/listing.ts +307 -0
- package/src/types/hitl/index.ts +28 -0
- package/src/types/run/checkpoint-store.ts +274 -1
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared machinery for {@link CheckpointStore.listDurableRuns}.
|
|
3
|
+
*
|
|
4
|
+
* Every rule the listing promises — the contiguous-prefix refusal, the park
|
|
5
|
+
* precedence, the ordering, the paging — lives here and is used by BOTH
|
|
6
|
+
* shipped implementations. A rule implemented twice is a rule that holds in
|
|
7
|
+
* one store and not the other, and the whole point of a store contract is
|
|
8
|
+
* that a host can swap the backend without swapping the semantics.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { NamzuError } from '../../types/errors/index.js'
|
|
12
|
+
import type { IterationCheckpoint, PendingDecision } from '../../types/hitl/index.js'
|
|
13
|
+
import type {
|
|
14
|
+
CheckpointListingScope,
|
|
15
|
+
CheckpointRunScope,
|
|
16
|
+
CheckpointStore,
|
|
17
|
+
DurableRunEntry,
|
|
18
|
+
DurableRunOrder,
|
|
19
|
+
DurableRunPage,
|
|
20
|
+
ListDurableRunsOptions,
|
|
21
|
+
ParkState,
|
|
22
|
+
ParkSummary,
|
|
23
|
+
} from '../../types/run/checkpoint-store.js'
|
|
24
|
+
|
|
25
|
+
/** Page size when the caller names none. */
|
|
26
|
+
export const DEFAULT_DURABLE_RUN_LIMIT = 100
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Refuse a listing scope with a hole in it.
|
|
30
|
+
*
|
|
31
|
+
* `{ tenantId, sessionId }` reads as "that session under whichever project
|
|
32
|
+
* holds it". A flat backend can answer it; a hierarchical one cannot look up
|
|
33
|
+
* a session without its project. Answering differently per backend is the
|
|
34
|
+
* one thing the contract exists to prevent, so neither answers: the caller
|
|
35
|
+
* names the project it means.
|
|
36
|
+
*/
|
|
37
|
+
export function assertContiguousListingScope(scope: CheckpointListingScope, caller: string): void {
|
|
38
|
+
if (scope.sessionId !== undefined && scope.projectId === undefined) {
|
|
39
|
+
throw new NamzuError({
|
|
40
|
+
code: 'invalid_config',
|
|
41
|
+
message: `${caller}: listing scope has a hole — \`sessionId\` was supplied without \`projectId\`. A run listing scope is a contiguous prefix of tenant → project → session; name the project the session belongs to.`,
|
|
42
|
+
details: { tenantId: scope.tenantId, sessionId: scope.sessionId },
|
|
43
|
+
})
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Whether a park's absolute deadline has passed. No deadline never expires. */
|
|
48
|
+
function isPastDeadline(pending: PendingDecision, now: number): boolean {
|
|
49
|
+
return pending.deadlineAt !== undefined && now >= pending.deadlineAt
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Which of the three states a recorded park is in.
|
|
54
|
+
*
|
|
55
|
+
* `resolved` is checked FIRST: a park that was answered after its deadline
|
|
56
|
+
* passed is answered, not expired. Reading the deadline first would report
|
|
57
|
+
* a decision a human actually made as an expiry nobody made, and the
|
|
58
|
+
* checkpoint is the evidence record for exactly that question.
|
|
59
|
+
*/
|
|
60
|
+
function parkStateOf(pending: PendingDecision, now: number): ParkState {
|
|
61
|
+
if (pending.resolvedAt !== undefined) return 'resolved'
|
|
62
|
+
return isPastDeadline(pending, now) ? 'expired' : 'outstanding'
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function toParkSummary(
|
|
66
|
+
cp: IterationCheckpoint,
|
|
67
|
+
pending: PendingDecision,
|
|
68
|
+
now: number,
|
|
69
|
+
): ParkSummary {
|
|
70
|
+
return {
|
|
71
|
+
state: parkStateOf(pending, now),
|
|
72
|
+
checkpointId: cp.id,
|
|
73
|
+
requestType: pending.request.type,
|
|
74
|
+
parkedAt: pending.parkedAt,
|
|
75
|
+
...(pending.deadlineAt !== undefined ? { deadlineAt: pending.deadlineAt } : {}),
|
|
76
|
+
...(pending.resolvedAt !== undefined ? { resolvedAt: pending.resolvedAt } : {}),
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* The one park that describes what a run is doing now, out of every park it
|
|
82
|
+
* ever recorded.
|
|
83
|
+
*
|
|
84
|
+
* Precedence: newest `outstanding`, else newest `expired`, else newest
|
|
85
|
+
* `resolved`.
|
|
86
|
+
*
|
|
87
|
+
* `outstanding` wins because that is the question an inbox is asking, and
|
|
88
|
+
* because the answer has to be the SAME checkpoint `findPendingCheckpoint`
|
|
89
|
+
* returns. A run can hold several parks — it parks, a human answers, it runs
|
|
90
|
+
* on, it parks again — and it can hold an outstanding one that is older than
|
|
91
|
+
* a resolved one only in the reverse case, where an earlier park expired
|
|
92
|
+
* unanswered and the run was resumed past it. Ranking by recency alone would
|
|
93
|
+
* then hand an inbox a resolved checkpoint and report the live park as
|
|
94
|
+
* nothing.
|
|
95
|
+
*
|
|
96
|
+
* @param checkpoints the run's checkpoints, any order.
|
|
97
|
+
*/
|
|
98
|
+
export function summarizePark(
|
|
99
|
+
checkpoints: readonly IterationCheckpoint[],
|
|
100
|
+
now: number,
|
|
101
|
+
): ParkSummary | undefined {
|
|
102
|
+
let best: ParkSummary | undefined
|
|
103
|
+
let bestRank = -1
|
|
104
|
+
let bestParkedAt = Number.NEGATIVE_INFINITY
|
|
105
|
+
|
|
106
|
+
for (const cp of checkpoints) {
|
|
107
|
+
const pending = cp.pending
|
|
108
|
+
if (!pending) continue
|
|
109
|
+
const summary = toParkSummary(cp, pending, now)
|
|
110
|
+
const rank = PARK_RANK[summary.state]
|
|
111
|
+
if (rank > bestRank || (rank === bestRank && pending.parkedAt > bestParkedAt)) {
|
|
112
|
+
best = summary
|
|
113
|
+
bestRank = rank
|
|
114
|
+
bestParkedAt = pending.parkedAt
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return best
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const PARK_RANK: Record<ParkState, number> = {
|
|
122
|
+
resolved: 0,
|
|
123
|
+
expired: 1,
|
|
124
|
+
outstanding: 2,
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Project one run's checkpoints into a listing entry.
|
|
129
|
+
*
|
|
130
|
+
* Returns `null` for a run with no checkpoints: the listing is of runs with
|
|
131
|
+
* DURABLE state, and a run with nothing stored has nothing a sweeper could
|
|
132
|
+
* resume. A disk walk hits this case for real — a sub-run's directory is
|
|
133
|
+
* created as a bare shell under its own id before anything is written to it.
|
|
134
|
+
*/
|
|
135
|
+
export function toDurableRunEntry(
|
|
136
|
+
scope: CheckpointRunScope,
|
|
137
|
+
checkpoints: readonly IterationCheckpoint[],
|
|
138
|
+
now: number,
|
|
139
|
+
): DurableRunEntry | null {
|
|
140
|
+
if (checkpoints.length === 0) return null
|
|
141
|
+
|
|
142
|
+
let latest = checkpoints[0] as IterationCheckpoint
|
|
143
|
+
// The EARLIEST recorded stamp, not the one on any particular checkpoint.
|
|
144
|
+
// Every checkpoint of a run carries the same value, so under that
|
|
145
|
+
// invariant the minimum is that value. Taking the minimum rather than
|
|
146
|
+
// reading one checkpoint is what makes the read safe if the invariant is
|
|
147
|
+
// ever broken: it can only err toward the run's true attribution, never
|
|
148
|
+
// away from it, and it cannot move when a later checkpoint is added.
|
|
149
|
+
let runCreatedAt: number | undefined
|
|
150
|
+
for (const cp of checkpoints) {
|
|
151
|
+
if (cp.createdAt > latest.createdAt) latest = cp
|
|
152
|
+
if (
|
|
153
|
+
cp.runCreatedAt !== undefined &&
|
|
154
|
+
(runCreatedAt === undefined || cp.runCreatedAt < runCreatedAt)
|
|
155
|
+
) {
|
|
156
|
+
runCreatedAt = cp.runCreatedAt
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const park = summarizePark(checkpoints, now)
|
|
161
|
+
|
|
162
|
+
return {
|
|
163
|
+
tenantId: scope.tenantId,
|
|
164
|
+
projectId: scope.projectId,
|
|
165
|
+
sessionId: scope.sessionId,
|
|
166
|
+
runId: scope.runId,
|
|
167
|
+
...(scope.parentRunId ? { parentRunId: scope.parentRunId } : {}),
|
|
168
|
+
...(runCreatedAt !== undefined ? { runCreatedAt } : {}),
|
|
169
|
+
checkpointCount: checkpoints.length,
|
|
170
|
+
latestCheckpointId: latest.id,
|
|
171
|
+
latestCheckpointAt: latest.createdAt,
|
|
172
|
+
...(park ? { park } : {}),
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Apply the park filter, the contract's ordering and the cursor to a set of
|
|
178
|
+
* entries an implementation has gathered.
|
|
179
|
+
*
|
|
180
|
+
* Both shipped stores gather differently — one walks a directory tree, one
|
|
181
|
+
* reads a map — and then hand the result here, so "ordered by `runId`, page
|
|
182
|
+
* ends where the next begins" is one implementation rather than two.
|
|
183
|
+
*/
|
|
184
|
+
export function paginateDurableRuns(
|
|
185
|
+
entries: readonly DurableRunEntry[],
|
|
186
|
+
options?: ListDurableRunsOptions,
|
|
187
|
+
): DurableRunPage {
|
|
188
|
+
const wanted = options?.park
|
|
189
|
+
const filtered =
|
|
190
|
+
wanted && wanted.length > 0
|
|
191
|
+
? entries.filter((e) => e.park !== undefined && wanted.includes(e.park.state))
|
|
192
|
+
: entries
|
|
193
|
+
|
|
194
|
+
// Both orders sort on a key that cannot move under a paging caller — see
|
|
195
|
+
// the contract comment on `listDurableRuns`.
|
|
196
|
+
const orderBy = options?.orderBy ?? 'runId'
|
|
197
|
+
const ordered = [...filtered].sort((a, b) =>
|
|
198
|
+
compareKeys(sortKey(a, orderBy), sortKey(b, orderBy)),
|
|
199
|
+
)
|
|
200
|
+
|
|
201
|
+
const after = options?.cursor === undefined ? undefined : decodeCursor(options.cursor, orderBy)
|
|
202
|
+
const start =
|
|
203
|
+
after === undefined ? 0 : ordered.findIndex((e) => compareKeys(sortKey(e, orderBy), after) > 0)
|
|
204
|
+
const from = start < 0 ? ordered.length : start
|
|
205
|
+
|
|
206
|
+
const limit = Math.max(1, Math.trunc(options?.limit ?? DEFAULT_DURABLE_RUN_LIMIT))
|
|
207
|
+
const page = ordered.slice(from, from + limit)
|
|
208
|
+
const exhausted = from + page.length >= ordered.length
|
|
209
|
+
|
|
210
|
+
return {
|
|
211
|
+
entries: page,
|
|
212
|
+
// No cursor when there is nothing behind it, so `while (cursor)`
|
|
213
|
+
// terminates rather than fetching one empty page to find out.
|
|
214
|
+
...(exhausted || page.length === 0
|
|
215
|
+
? {}
|
|
216
|
+
: { cursor: encodeCursor(sortKey(page[page.length - 1] as DurableRunEntry, orderBy)) }),
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* A row's position in the requested order, as a comparable tuple.
|
|
222
|
+
*
|
|
223
|
+
* The first element is a rank rather than the timestamp itself, so that
|
|
224
|
+
* "never recorded" is a position in its own right instead of a number
|
|
225
|
+
* standing in for one. In `createdAt` order it ranks 0 and everything
|
|
226
|
+
* stamped ranks 1 — unrecorded runs first, and truthfully so: the stamp is
|
|
227
|
+
* written by the checkpoint manager, so a run without one was checkpointed
|
|
228
|
+
* by a build that predates it, and predates every run that has one.
|
|
229
|
+
*/
|
|
230
|
+
type SortKey = readonly [number, number, string]
|
|
231
|
+
|
|
232
|
+
function sortKey(entry: DurableRunEntry, orderBy: DurableRunOrder): SortKey {
|
|
233
|
+
if (orderBy === 'runId') return [0, 0, entry.runId]
|
|
234
|
+
return entry.runCreatedAt === undefined
|
|
235
|
+
? [0, 0, entry.runId]
|
|
236
|
+
: [1, entry.runCreatedAt, entry.runId]
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
function compareKeys(a: SortKey, b: SortKey): number {
|
|
240
|
+
if (a[0] !== b[0]) return a[0] - b[0]
|
|
241
|
+
if (a[1] !== b[1]) return a[1] - b[1]
|
|
242
|
+
return a[2] < b[2] ? -1 : a[2] > b[2] ? 1 : 0
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* The cursor is the last row's key, and nothing else.
|
|
247
|
+
*
|
|
248
|
+
* Opaque to callers by contract — the shape is written down here rather than
|
|
249
|
+
* in the type so a host is not tempted to construct one. Run ids come from a
|
|
250
|
+
* 36-character lowercase alphabet with a `run_` prefix and contain no
|
|
251
|
+
* separator, so joining on `:` is unambiguous.
|
|
252
|
+
*/
|
|
253
|
+
function encodeCursor(key: SortKey): string {
|
|
254
|
+
return `${key[0]}:${key[1]}:${key[2]}`
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
function decodeCursor(cursor: string, orderBy: DurableRunOrder): SortKey {
|
|
258
|
+
const first = cursor.indexOf(':')
|
|
259
|
+
const second = cursor.indexOf(':', first + 1)
|
|
260
|
+
if (first < 0 || second < 0) {
|
|
261
|
+
throw new NamzuError({
|
|
262
|
+
code: 'invalid_config',
|
|
263
|
+
message: `listDurableRuns: "${cursor}" is not a cursor this listing issued. Pass back the \`cursor\` from the previous page rather than constructing one; its shape is not part of the contract.`,
|
|
264
|
+
details: { cursor, orderBy },
|
|
265
|
+
})
|
|
266
|
+
}
|
|
267
|
+
const rank = Number(cursor.slice(0, first))
|
|
268
|
+
const stamp = Number(cursor.slice(first + 1, second))
|
|
269
|
+
if (!Number.isFinite(rank) || !Number.isFinite(stamp)) {
|
|
270
|
+
throw new NamzuError({
|
|
271
|
+
code: 'invalid_config',
|
|
272
|
+
message: `listDurableRuns: cursor "${cursor}" is malformed — its position fields are not numbers. Pass back the \`cursor\` from the previous page.`,
|
|
273
|
+
details: { cursor, orderBy },
|
|
274
|
+
})
|
|
275
|
+
}
|
|
276
|
+
return [rank, stamp, cursor.slice(second + 1)]
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* Every run with durable state under a scope, refusing when the store cannot
|
|
281
|
+
* answer.
|
|
282
|
+
*
|
|
283
|
+
* The refusal is the point. `listDurableRuns` is optional on the contract so
|
|
284
|
+
* that adding it did not break every host that had already implemented the
|
|
285
|
+
* interface — and an optional capability reached without a check degrades
|
|
286
|
+
* into a wrong answer: a store that cannot list would hand an approval inbox
|
|
287
|
+
* an empty page, and "nothing is waiting on a human" is not what "I cannot
|
|
288
|
+
* tell" means. A host that gets this error knows to supply a backend that
|
|
289
|
+
* implements the listing; a host that got `[]` would ship an inbox that
|
|
290
|
+
* silently never fires.
|
|
291
|
+
*/
|
|
292
|
+
export async function listDurableRuns(
|
|
293
|
+
store: CheckpointStore,
|
|
294
|
+
scope: CheckpointListingScope,
|
|
295
|
+
options?: ListDurableRunsOptions,
|
|
296
|
+
): Promise<DurableRunPage> {
|
|
297
|
+
if (typeof store.listDurableRuns !== 'function') {
|
|
298
|
+
throw new NamzuError({
|
|
299
|
+
code: 'capability_unavailable',
|
|
300
|
+
message:
|
|
301
|
+
'listDurableRuns: the injected checkpoint store does not implement `listDurableRuns`, so it cannot enumerate runs above a run id. Refusing rather than reporting an empty listing, which would read as "no runs are parked" when the truth is that this store cannot tell. Supply a store that implements it (the built-in disk and in-memory stores both do).',
|
|
302
|
+
details: { tenantId: scope.tenantId },
|
|
303
|
+
})
|
|
304
|
+
}
|
|
305
|
+
assertContiguousListingScope(scope, 'listDurableRuns')
|
|
306
|
+
return store.listDurableRuns(scope, options)
|
|
307
|
+
}
|
package/src/types/hitl/index.ts
CHANGED
|
@@ -210,6 +210,34 @@ export interface IterationCheckpoint {
|
|
|
210
210
|
*/
|
|
211
211
|
planStatus?: PlanStatus
|
|
212
212
|
|
|
213
|
+
/**
|
|
214
|
+
* When the RUN was attributed — not when this checkpoint was written.
|
|
215
|
+
* See {@link IterationCheckpoint.createdAt} for the latter.
|
|
216
|
+
*
|
|
217
|
+
* Denormalized onto every checkpoint of the run, identically, and that
|
|
218
|
+
* repetition is the whole point. A listing above the run needs a key it
|
|
219
|
+
* can order by, and a key a paging caller can trust is one that cannot
|
|
220
|
+
* MOVE. Every other time a checkpoint store can derive per run moves: the
|
|
221
|
+
* newest checkpoint's `createdAt` advances every time the run checkpoints
|
|
222
|
+
* again, and the oldest one's advances every time `prune` deletes
|
|
223
|
+
* oldest-first. Carried on all of them, this one survives both — pruning
|
|
224
|
+
* cannot reach a value every survivor also holds.
|
|
225
|
+
*
|
|
226
|
+
* `readonly`, and written exactly once per run by
|
|
227
|
+
* {@link import('../../runtime/query/checkpoint.js').CheckpointManager},
|
|
228
|
+
* which settles it on whichever comes first — adopting it from the
|
|
229
|
+
* checkpoint a resume restores, or minting it from the run's own start
|
|
230
|
+
* instant — and never reassigns after. A field that COULD be updated is
|
|
231
|
+
* one edit away from moving again, which would put the ordering back
|
|
232
|
+
* where it started.
|
|
233
|
+
*
|
|
234
|
+
* Absent on checkpoints written before this existed. That absence is
|
|
235
|
+
* information, not a gap: a run with no stamp on any of its checkpoints
|
|
236
|
+
* was attributed before the stamp existed, and therefore before every
|
|
237
|
+
* run that has one.
|
|
238
|
+
*/
|
|
239
|
+
readonly runCreatedAt?: number
|
|
240
|
+
|
|
213
241
|
/**
|
|
214
242
|
* Present when the run parked at this checkpoint awaiting a human.
|
|
215
243
|
* See {@link PendingDecision}.
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
* five-layer attribution (Convention #17) instead of a filesystem path.
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
|
-
import type { CheckpointId, IterationCheckpoint } from '../hitl/index.js'
|
|
12
|
+
import type { CheckpointId, HITLDecisionRequest, IterationCheckpoint } from '../hitl/index.js'
|
|
13
13
|
import type { RunId, SessionId, TenantId } from '../ids/index.js'
|
|
14
14
|
import type { ProjectId } from '../session/ids.js'
|
|
15
15
|
|
|
@@ -39,6 +39,213 @@ export interface CheckpointRunScope {
|
|
|
39
39
|
parentRunId?: RunId
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
+
/**
|
|
43
|
+
* A CONTIGUOUS PREFIX of the run attribution hierarchy, addressing a SET of
|
|
44
|
+
* runs rather than one.
|
|
45
|
+
*
|
|
46
|
+
* A separate type from {@link CheckpointRunScope} on purpose. That type
|
|
47
|
+
* addresses exactly one run and four accessors depend on it doing so; making
|
|
48
|
+
* its one distinguishing field optional in place would turn "the scope of a
|
|
49
|
+
* run" into "some identifiers, maybe", and every accessor's guarantee with
|
|
50
|
+
* it.
|
|
51
|
+
*
|
|
52
|
+
* Three properties, each deliberate:
|
|
53
|
+
*
|
|
54
|
+
* - **`tenantId` is required.** Isolation is the one boundary that is never
|
|
55
|
+
* optional here. An untenanted listing is a cross-tenant read with a
|
|
56
|
+
* friendly name.
|
|
57
|
+
* - **It stops ABOVE the run.** No `runId`, no `parentRunId`. A caller
|
|
58
|
+
* holding a run id already has a full {@link CheckpointRunScope} and four
|
|
59
|
+
* accessors that take it; admitting one here would make
|
|
60
|
+
* `CheckpointRunScope` structurally assignable to this type and re-merge
|
|
61
|
+
* the two ideas the split exists to keep apart.
|
|
62
|
+
* - **The prefix must be contiguous.** A `sessionId` with no `projectId` is
|
|
63
|
+
* REFUSED, not silently widened to "that session under whichever project
|
|
64
|
+
* holds it". A flat backend can answer it and a hierarchical one cannot,
|
|
65
|
+
* so the answer would depend on the backend's storage shape — which is
|
|
66
|
+
* the one thing a store contract exists to hide.
|
|
67
|
+
*/
|
|
68
|
+
export interface CheckpointListingScope {
|
|
69
|
+
/** Isolation boundary (Convention #17). Never optional. */
|
|
70
|
+
readonly tenantId: TenantId
|
|
71
|
+
/** Narrow to one project. Absent = every project of the tenant. */
|
|
72
|
+
readonly projectId?: ProjectId
|
|
73
|
+
/** Narrow to one session. Requires `projectId`. */
|
|
74
|
+
readonly sessionId?: SessionId
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* What a run's human-in-the-loop park is doing, as far as durable state can
|
|
79
|
+
* tell.
|
|
80
|
+
*
|
|
81
|
+
* A closed union rather than a boolean because the two unanswered states are
|
|
82
|
+
* drained by DIFFERENT operators: `outstanding` is an approval inbox's queue
|
|
83
|
+
* and `expired` is a reclamation sweep's, and serving one to the other either
|
|
84
|
+
* re-presents a dead approval forever or discards a live one.
|
|
85
|
+
*/
|
|
86
|
+
export type ParkState =
|
|
87
|
+
/** `pending` set, no `resolvedAt`, deadline not passed. A human owes an answer. */
|
|
88
|
+
| 'outstanding'
|
|
89
|
+
/** `pending` set, no `resolvedAt`, deadline passed. Nobody will answer it. */
|
|
90
|
+
| 'expired'
|
|
91
|
+
/** `pending` set with `resolvedAt`. Kept as evidence of who decided what. */
|
|
92
|
+
| 'resolved'
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* **Do not widen this union to say who is working on the run.**
|
|
96
|
+
*
|
|
97
|
+
* A consumer switches over `ParkState` exhaustively, so a fourth member is a
|
|
98
|
+
* backward-incompatible change and a `major` — and the pull to add one is
|
|
99
|
+
* real, because the next capability this contract takes is a cross-process
|
|
100
|
+
* claim, and a queue worker draining the inbox wants to skip runs another
|
|
101
|
+
* worker already holds.
|
|
102
|
+
*
|
|
103
|
+
* That is a different fact about a different subject. A park is a question
|
|
104
|
+
* put to a HUMAN; a claim is a lease held by a PROCESS, and one run can have
|
|
105
|
+
* both, neither, or either. Encoding them in one union makes the pair
|
|
106
|
+
* unsayable and loses the state a worker needs most: parked AND unclaimed.
|
|
107
|
+
*
|
|
108
|
+
* The additive shape is a sibling optional field — `claim?: …` on
|
|
109
|
+
* {@link DurableRunEntry}, `claimed?: …` on {@link ListDurableRunsOptions}.
|
|
110
|
+
* A consumer reading rows is not broken by a new optional field, so the
|
|
111
|
+
* claim ships as a second `minor` on this contract rather than a second
|
|
112
|
+
* migration of it. This note exists because the union is the obvious place
|
|
113
|
+
* to reach for and the wrong one.
|
|
114
|
+
*/
|
|
115
|
+
|
|
116
|
+
/** A run's park disposition, projected from the checkpoint that carries it. */
|
|
117
|
+
export interface ParkSummary {
|
|
118
|
+
readonly state: ParkState
|
|
119
|
+
/** The parked checkpoint — address it directly with `readCheckpoint`. */
|
|
120
|
+
readonly checkpointId: CheckpointId
|
|
121
|
+
/** What the human was asked. Enough to route an inbox without a second read. */
|
|
122
|
+
readonly requestType: HITLDecisionRequest['type']
|
|
123
|
+
/** Epoch ms at which the run parked. */
|
|
124
|
+
readonly parkedAt: number
|
|
125
|
+
/** Absolute expiry, when the park carries one. */
|
|
126
|
+
readonly deadlineAt?: number
|
|
127
|
+
/** Epoch ms at which the answer arrived. Only on `resolved`. */
|
|
128
|
+
readonly resolvedAt?: number
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* One run that has durable checkpoint state under the queried scope.
|
|
133
|
+
*
|
|
134
|
+
* **Extends {@link CheckpointRunScope}, and that is the load-bearing part.**
|
|
135
|
+
* A listing whose rows cannot be turned back into an addressable scope is a
|
|
136
|
+
* report, not a work queue. Because an entry IS a run scope,
|
|
137
|
+
* `findPendingCheckpoint(store, entry)`, `new CheckpointManager(store, entry)`
|
|
138
|
+
* and `resumeRun({ scope: entry, … })` all accept a row straight out of the
|
|
139
|
+
* listing — with no re-assembly, and so no chance of assembling it wrong.
|
|
140
|
+
*
|
|
141
|
+
* ### What an entry deliberately does NOT carry
|
|
142
|
+
*
|
|
143
|
+
* A run STATUS. A checkpoint is written mid-flight, so nothing in this store
|
|
144
|
+
* distinguishes a run that finished from one that died — the same fact
|
|
145
|
+
* {@link import('../../runtime/query/run-state.js').loadRunState} already
|
|
146
|
+
* states, where a rebuilt snapshot always reports `running` and the host's
|
|
147
|
+
* own record stays the authority. A `status` field here would answer
|
|
148
|
+
* "mid-flight" for every run that ever succeeded, and a sweeper built on it
|
|
149
|
+
* would resume finished work.
|
|
150
|
+
*
|
|
151
|
+
* A crash sweep is therefore: list every run with durable state under the
|
|
152
|
+
* scope, intersect with the host's own run records, resume the difference.
|
|
153
|
+
*/
|
|
154
|
+
export interface DurableRunEntry extends CheckpointRunScope {
|
|
155
|
+
/**
|
|
156
|
+
* When the run was attributed. Absent when it was never recorded.
|
|
157
|
+
*
|
|
158
|
+
* The only per-run key this store holds that does not move, which is why
|
|
159
|
+
* it is the one an oldest-first listing can page over — see
|
|
160
|
+
* {@link CheckpointStore.listDurableRuns} and
|
|
161
|
+
* `IterationCheckpoint.runCreatedAt`.
|
|
162
|
+
*
|
|
163
|
+
* **Absent means "not recorded", and a caller should render it that way
|
|
164
|
+
* rather than as a date it invents.** Every run checkpointed by a build
|
|
165
|
+
* carrying the stamp has one; a run that has none was checkpointed
|
|
166
|
+
* before the stamp existed.
|
|
167
|
+
*/
|
|
168
|
+
readonly runCreatedAt?: number
|
|
169
|
+
|
|
170
|
+
/** How many checkpoints the run has right now. Pruning lowers it. */
|
|
171
|
+
readonly checkpointCount: number
|
|
172
|
+
/** Newest checkpoint by `createdAt` — the one a resume restores by default. */
|
|
173
|
+
readonly latestCheckpointId: CheckpointId
|
|
174
|
+
/** `createdAt` of {@link DurableRunEntry.latestCheckpointId}. */
|
|
175
|
+
readonly latestCheckpointAt: number
|
|
176
|
+
/** Absent when the run has never parked. */
|
|
177
|
+
readonly park?: ParkSummary
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Which order a listing comes back in.
|
|
182
|
+
*
|
|
183
|
+
* Explicit rather than implied, because the two available orders answer
|
|
184
|
+
* different questions and neither is right for both. "Show me every run
|
|
185
|
+
* waiting on a human" wants stable paging; "show me the one that has been
|
|
186
|
+
* waiting longest" wants chronology. A listing that silently picked one
|
|
187
|
+
* would be the same ambiguity the scope type removed by splitting.
|
|
188
|
+
*/
|
|
189
|
+
export type DurableRunOrder =
|
|
190
|
+
/**
|
|
191
|
+
* By `runId` ascending. Stable and total, and meaningless as chronology —
|
|
192
|
+
* run ids carry no timestamp. The default, because it is what shipped.
|
|
193
|
+
*/
|
|
194
|
+
| 'runId'
|
|
195
|
+
/**
|
|
196
|
+
* Oldest first, by {@link DurableRunEntry.runCreatedAt} then `runId`.
|
|
197
|
+
* This is the triage order: it answers which run has been waiting
|
|
198
|
+
* longest. Safe to page over, because the stamp is recorded once at
|
|
199
|
+
* attribution and never rewritten.
|
|
200
|
+
*
|
|
201
|
+
* **Runs whose creation was never recorded come FIRST**, ordered among
|
|
202
|
+
* themselves by `runId`. That is not a guess dressed up as a date: the
|
|
203
|
+
* stamp is written by the checkpoint manager, so a run lacking it on
|
|
204
|
+
* every checkpoint was checkpointed by a build that predates the stamp,
|
|
205
|
+
* and therefore predates every run that has one. Their
|
|
206
|
+
* `runCreatedAt` is absent on the row, so a caller can render "unknown"
|
|
207
|
+
* instead of a date nobody recorded.
|
|
208
|
+
*/
|
|
209
|
+
| 'createdAt'
|
|
210
|
+
|
|
211
|
+
/** Filters and paging for {@link CheckpointStore.listDurableRuns}. */
|
|
212
|
+
export interface ListDurableRunsOptions {
|
|
213
|
+
/**
|
|
214
|
+
* Ordering, and therefore what the cursor is a position in. Defaults to
|
|
215
|
+
* `'runId'`. A cursor is only meaningful within one order — do not carry
|
|
216
|
+
* one across a change of `orderBy`.
|
|
217
|
+
*/
|
|
218
|
+
readonly orderBy?: DurableRunOrder
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Keep only runs whose park is in one of these states. A run that never
|
|
222
|
+
* parked has no state and is excluded by ANY value here; omit the filter
|
|
223
|
+
* to include it.
|
|
224
|
+
*/
|
|
225
|
+
readonly park?: readonly ParkState[]
|
|
226
|
+
/** Page size. Defaults to 100, clamped to at least 1. */
|
|
227
|
+
readonly limit?: number
|
|
228
|
+
/** Resume token from the previous page's {@link DurableRunPage.cursor}. */
|
|
229
|
+
readonly cursor?: string
|
|
230
|
+
/**
|
|
231
|
+
* Clock for expiry, so a sweep can be tested and so every entry in one
|
|
232
|
+
* page is judged against the same instant. Defaults to `Date.now()` — the
|
|
233
|
+
* same seam `findPendingCheckpoint` already takes.
|
|
234
|
+
*/
|
|
235
|
+
readonly now?: number
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/** One page of {@link DurableRunEntry}. */
|
|
239
|
+
export interface DurableRunPage {
|
|
240
|
+
readonly entries: readonly DurableRunEntry[]
|
|
241
|
+
/**
|
|
242
|
+
* Pass to the next call. **Absent means the listing is exhausted**, so
|
|
243
|
+
* `while (cursor)` terminates; a store never returns a cursor it already
|
|
244
|
+
* knows yields nothing.
|
|
245
|
+
*/
|
|
246
|
+
readonly cursor?: string
|
|
247
|
+
}
|
|
248
|
+
|
|
42
249
|
/**
|
|
43
250
|
* Persistence contract consumed by
|
|
44
251
|
* {@link import('../../runtime/query/checkpoint.js').CheckpointManager} and
|
|
@@ -49,6 +256,27 @@ export interface CheckpointRunScope {
|
|
|
49
256
|
* not-found error. `deleteCheckpoint` is idempotent: deleting an absent
|
|
50
257
|
* checkpoint succeeds as a no-op (mirrors the disk store's ENOENT
|
|
51
258
|
* swallowing).
|
|
259
|
+
*
|
|
260
|
+
* ## Optional capabilities, and the rule that comes with them
|
|
261
|
+
*
|
|
262
|
+
* {@link CheckpointStore.listDurableRuns} is optional, following
|
|
263
|
+
* `SessionStore.listSessionsByProject`. A required method would break every
|
|
264
|
+
* host that has already implemented this interface, which is a `major` for
|
|
265
|
+
* what is otherwise an additive capability.
|
|
266
|
+
*
|
|
267
|
+
* The rule optionality obliges: **a caller of an optional capability REFUSES
|
|
268
|
+
* when it is absent; it never degrades.** An approval inbox built on a store
|
|
269
|
+
* that cannot list has to throw, because an empty page would say "nothing is
|
|
270
|
+
* waiting on a human" when the truth is "I cannot tell" — an optional
|
|
271
|
+
* dependency degrading a check, which this repository has been bitten by
|
|
272
|
+
* before. Reach the capability through
|
|
273
|
+
* {@link import('../../store/run/listing.js').listDurableRuns}, which
|
|
274
|
+
* refuses on absence rather than answering.
|
|
275
|
+
*
|
|
276
|
+
* Any capability added here later takes the same shape — optional method,
|
|
277
|
+
* refusing helper. A cross-process claim is the next one, and a two-worker
|
|
278
|
+
* deployment against a store with no lease has to fail loudly rather than
|
|
279
|
+
* proceed.
|
|
52
280
|
*/
|
|
53
281
|
export interface CheckpointStore {
|
|
54
282
|
/** Persist one checkpoint. Overwrites an existing checkpoint with the same id. */
|
|
@@ -69,4 +297,49 @@ export interface CheckpointStore {
|
|
|
69
297
|
|
|
70
298
|
/** Delete a checkpoint by id. Absent checkpoints succeed as a no-op. */
|
|
71
299
|
deleteCheckpoint(scope: CheckpointRunScope, checkpointId: CheckpointId): Promise<void>
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* Every run with durable checkpoint state under a scope ABOVE the run.
|
|
303
|
+
* OPTIONAL — see the optional-capability rule on this interface.
|
|
304
|
+
*
|
|
305
|
+
* This is the read an approval inbox and a park sweep are built from, and
|
|
306
|
+
* the one thing this contract had no way to express: every other accessor
|
|
307
|
+
* needs a `runId`, so a host could only ask about runs it already knew
|
|
308
|
+
* about. `hitlParkTtlMs` documents a host sweep as the reclamation path
|
|
309
|
+
* for an unanswered park, and until this existed the sweep had no way to
|
|
310
|
+
* enumerate what to sweep.
|
|
311
|
+
*
|
|
312
|
+
* ### Ordering
|
|
313
|
+
*
|
|
314
|
+
* Two orders, named by `options.orderBy`, and the cursor is a position in
|
|
315
|
+
* whichever one was asked for. See {@link DurableRunOrder}.
|
|
316
|
+
*
|
|
317
|
+
* Both sort on a key that cannot MOVE, which is the property a cursor
|
|
318
|
+
* needs — sort on a moving key and a paging caller skips rows and repeats
|
|
319
|
+
* rows. That rules out every time a checkpoint store can derive on its
|
|
320
|
+
* own: the newest checkpoint's timestamp advances whenever the run
|
|
321
|
+
* checkpoints again, and the oldest one's advances whenever
|
|
322
|
+
* `CheckpointManager.prune` deletes oldest-first, which is what pruning
|
|
323
|
+
* does. It leaves `runId`, which is immutable and unique but carries no
|
|
324
|
+
* timestamp, and `runCreatedAt`, which is recorded once at attribution
|
|
325
|
+
* and denormalized onto every checkpoint so pruning cannot reach it.
|
|
326
|
+
*
|
|
327
|
+
* `'runId'` alone is already a total order. `'createdAt'` tiebreaks on
|
|
328
|
+
* `runId`, which is the rule `orderChildren` follows: sort on a key that
|
|
329
|
+
* cannot move, make the order total with an id.
|
|
330
|
+
*
|
|
331
|
+
* A run whose FIRST checkpoint is written after paging began may be
|
|
332
|
+
* missed by that pass, in either order — it lands wherever its key puts
|
|
333
|
+
* it, possibly behind the cursor. That is the right trade for a queue:
|
|
334
|
+
* the sweep runs again and picks it up next pass, whereas a moving sort
|
|
335
|
+
* key loses runs that already existed.
|
|
336
|
+
*
|
|
337
|
+
* @param scope contiguous prefix; `tenantId` required. Implementations
|
|
338
|
+
* reject a hole (`sessionId` with no `projectId`) rather than guessing.
|
|
339
|
+
* @param options filters and paging. See {@link ListDurableRunsOptions}.
|
|
340
|
+
*/
|
|
341
|
+
listDurableRuns?(
|
|
342
|
+
scope: CheckpointListingScope,
|
|
343
|
+
options?: ListDurableRunsOptions,
|
|
344
|
+
): Promise<DurableRunPage>
|
|
72
345
|
}
|