@namzu/sdk 20.3.0 → 20.4.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.
Files changed (50) hide show
  1. package/CHANGELOG.md +142 -0
  2. package/dist/public-runtime.d.ts +1 -0
  3. package/dist/public-runtime.d.ts.map +1 -1
  4. package/dist/public-runtime.js +5 -0
  5. package/dist/public-runtime.js.map +1 -1
  6. package/dist/runtime/query/checkpoint.d.ts +27 -1
  7. package/dist/runtime/query/checkpoint.d.ts.map +1 -1
  8. package/dist/runtime/query/checkpoint.js +34 -4
  9. package/dist/runtime/query/checkpoint.js.map +1 -1
  10. package/dist/runtime/query/index.d.ts +21 -1
  11. package/dist/runtime/query/index.d.ts.map +1 -1
  12. package/dist/runtime/query/index.js +4 -0
  13. package/dist/runtime/query/index.js.map +1 -1
  14. package/dist/runtime/query/resume-run.d.ts +9 -1
  15. package/dist/runtime/query/resume-run.d.ts.map +1 -1
  16. package/dist/runtime/query/resume-run.js +2 -1
  17. package/dist/runtime/query/resume-run.js.map +1 -1
  18. package/dist/store/index.d.ts +1 -1
  19. package/dist/store/index.d.ts.map +1 -1
  20. package/dist/store/index.js +1 -1
  21. package/dist/store/index.js.map +1 -1
  22. package/dist/store/run/checkpoint-disk.d.ts +18 -2
  23. package/dist/store/run/checkpoint-disk.d.ts.map +1 -1
  24. package/dist/store/run/checkpoint-disk.js +50 -5
  25. package/dist/store/run/checkpoint-disk.js.map +1 -1
  26. package/dist/store/run/checkpoint-memory.d.ts +22 -2
  27. package/dist/store/run/checkpoint-memory.d.ts.map +1 -1
  28. package/dist/store/run/checkpoint-memory.js +99 -4
  29. package/dist/store/run/checkpoint-memory.js.map +1 -1
  30. package/dist/store/run/claim-disk.d.ts +130 -0
  31. package/dist/store/run/claim-disk.d.ts.map +1 -0
  32. package/dist/store/run/claim-disk.js +550 -0
  33. package/dist/store/run/claim-disk.js.map +1 -0
  34. package/dist/store/run/listing.d.ts +44 -1
  35. package/dist/store/run/listing.d.ts.map +1 -1
  36. package/dist/store/run/listing.js +92 -1
  37. package/dist/store/run/listing.js.map +1 -1
  38. package/dist/types/run/checkpoint-store.d.ts +178 -2
  39. package/dist/types/run/checkpoint-store.d.ts.map +1 -1
  40. package/package.json +1 -1
  41. package/src/public-runtime.ts +5 -0
  42. package/src/runtime/query/checkpoint.ts +42 -5
  43. package/src/runtime/query/index.ts +26 -1
  44. package/src/runtime/query/resume-run.ts +12 -2
  45. package/src/store/index.ts +4 -0
  46. package/src/store/run/checkpoint-disk.ts +70 -5
  47. package/src/store/run/checkpoint-memory.ts +118 -3
  48. package/src/store/run/claim-disk.ts +593 -0
  49. package/src/store/run/listing.ts +116 -1
  50. package/src/types/run/checkpoint-store.ts +189 -2
@@ -14,12 +14,16 @@ import type {
14
14
  CheckpointListingScope,
15
15
  CheckpointRunScope,
16
16
  CheckpointStore,
17
+ ClaimFence,
18
+ ClaimRunOptions,
19
+ ClaimSummary,
17
20
  DurableRunEntry,
18
21
  DurableRunOrder,
19
22
  DurableRunPage,
20
23
  ListDurableRunsOptions,
21
24
  ParkState,
22
25
  ParkSummary,
26
+ RunClaim,
23
27
  } from '../../types/run/checkpoint-store.js'
24
28
 
25
29
  /** Page size when the caller names none. */
@@ -186,11 +190,20 @@ export function paginateDurableRuns(
186
190
  options?: ListDurableRunsOptions,
187
191
  ): DurableRunPage {
188
192
  const wanted = options?.park
189
- const filtered =
193
+ const byPark =
190
194
  wanted && wanted.length > 0
191
195
  ? entries.filter((e) => e.park !== undefined && wanted.includes(e.park.state))
192
196
  : entries
193
197
 
198
+ // An expired claim counts as unheld. That is what expiry means, and a
199
+ // queue reader that treated an expired claim as held would leave a dead
200
+ // worker's runs invisible forever — the exact failure a lease exists to
201
+ // prevent, reintroduced by the filter that reads it.
202
+ const filtered =
203
+ options?.claimed === undefined
204
+ ? byPark
205
+ : byPark.filter((e) => (e.claim !== undefined && !e.claim.expired) === options.claimed)
206
+
194
207
  // Both orders sort on a key that cannot move under a paging caller — see
195
208
  // the contract comment on `listDurableRuns`.
196
209
  const orderBy = options?.orderBy ?? 'runId'
@@ -305,3 +318,105 @@ export async function listDurableRuns(
305
318
  assertContiguousListingScope(scope, 'listDurableRuns')
306
319
  return store.listDurableRuns(scope, options)
307
320
  }
321
+
322
+ /**
323
+ * Take working possession of a run, refusing when the store cannot arbitrate.
324
+ *
325
+ * The refusal is the entire safety property. `claimRun` is optional on the
326
+ * contract, and the natural way to reach an absent optional method is to skip
327
+ * it — which here means every worker proceeds, believing it holds a run
328
+ * nobody arbitrated. Two workers then restore the same checkpoint, both run
329
+ * the tools, and both write under one run id; half the work vanishes with no
330
+ * error anywhere.
331
+ *
332
+ * So a store with no claim support does not get "claimed by default". It gets
333
+ * an error naming the deployment shape it cannot support. A single-writer
334
+ * host never calls this and is unaffected.
335
+ *
336
+ * Returns `null` — not an error — when another holder has the run. That is
337
+ * the ordinary outcome of two readers on one queue, and a caller loops to the
338
+ * next run rather than handling a fault.
339
+ */
340
+ export async function claimRun(
341
+ store: CheckpointStore,
342
+ scope: CheckpointRunScope,
343
+ options: ClaimRunOptions,
344
+ ): Promise<RunClaim | null> {
345
+ if (typeof store.claimRun !== 'function') {
346
+ throw new NamzuError({
347
+ code: 'capability_unavailable',
348
+ message:
349
+ 'claimRun: the injected checkpoint store does not implement `claimRun`, so it cannot arbitrate between two workers taking the same run. Refusing rather than proceeding unclaimed — proceeding would let two workers restore one checkpoint, both execute its tools, and both write under one run id, which loses half the work and reports nothing. Supply a store that implements it, or run a single writer per run.',
350
+ details: { runId: scope.runId },
351
+ })
352
+ }
353
+ if (!Number.isFinite(options.ttlMs) || options.ttlMs <= 0) {
354
+ throw new NamzuError({
355
+ code: 'invalid_config',
356
+ message: `claimRun: ttlMs must be a positive number of milliseconds, got ${String(options.ttlMs)}. A lease that expires immediately is a lease every worker can take at once, which is the condition this call exists to prevent.`,
357
+ details: { runId: scope.runId, ttlMs: options.ttlMs },
358
+ })
359
+ }
360
+ return store.claimRun(scope, options)
361
+ }
362
+
363
+ /**
364
+ * Give a claim up early, refusing when the store cannot arbitrate.
365
+ *
366
+ * Refuses for the same reason as {@link claimRun}: a host that believes it is
367
+ * releasing a claim on a store that has none is a host that believes the
368
+ * whole mechanism is running.
369
+ */
370
+ export async function releaseRun(
371
+ store: CheckpointStore,
372
+ scope: CheckpointRunScope,
373
+ fence: ClaimFence,
374
+ ): Promise<void> {
375
+ if (typeof store.releaseRun !== 'function') {
376
+ throw new NamzuError({
377
+ code: 'capability_unavailable',
378
+ message:
379
+ 'releaseRun: the injected checkpoint store does not implement `releaseRun`. A release that silently does nothing would leave the run held until its lease expires while the caller believes it is back on the queue.',
380
+ details: { runId: scope.runId },
381
+ })
382
+ }
383
+ return store.releaseRun(scope, fence)
384
+ }
385
+
386
+ /**
387
+ * The refusal a store raises when a write presents a superseded fence.
388
+ *
389
+ * Shared so both shipped stores say the same thing, and so a host writing its
390
+ * own backend raises something a caller can branch on rather than a message
391
+ * string. This is the moment a stalled worker learns it lost the run — the
392
+ * only moment it CAN learn, since from the inside a pause and a partition
393
+ * both look like time not passing.
394
+ */
395
+ export function fencedOut(
396
+ scope: CheckpointRunScope,
397
+ presented: number,
398
+ current: number,
399
+ ): NamzuError {
400
+ return new NamzuError({
401
+ code: 'storage_error',
402
+ message: `writeCheckpoint: refusing a write for run ${scope.runId} fenced at ${presented} — the run is now claimed at ${current}. Another worker took this run over, so this process no longer holds it and its work is not the record. Stop the run rather than retrying: the claim is gone, not busy.`,
403
+ details: { runId: scope.runId, presentedFence: presented, currentFence: current },
404
+ retryable: false,
405
+ })
406
+ }
407
+
408
+ /**
409
+ * Whether a recorded claim still holds at `now`, and the summary a listing
410
+ * reports for it.
411
+ */
412
+ export function toClaimSummary(claim: RunClaim, now: number): ClaimSummary {
413
+ return {
414
+ holder: claim.holder,
415
+ fence: claim.fence,
416
+ expiresAt: claim.expiresAt,
417
+ // Judged here, once, against the store's clock. Left to the caller it
418
+ // would be judged against a different one, and a single page could
419
+ // then disagree with itself about which rows are available.
420
+ expired: now >= claim.expiresAt,
421
+ }
422
+ }
@@ -175,6 +175,17 @@ export interface DurableRunEntry extends CheckpointRunScope {
175
175
  readonly latestCheckpointAt: number
176
176
  /** Absent when the run has never parked. */
177
177
  readonly park?: ParkSummary
178
+
179
+ /**
180
+ * Absent when no process has ever claimed the run.
181
+ *
182
+ * A SIBLING of {@link DurableRunEntry.park}, not a member of
183
+ * {@link ParkState} — see the note on that union. A park is a question put
184
+ * to a human; a claim is a lease held by a process. A run can have both,
185
+ * neither, or either, and the state a queue worker needs most is parked
186
+ * AND unclaimed, which one union cannot say.
187
+ */
188
+ readonly claim?: ClaimSummary
178
189
  }
179
190
 
180
191
  /**
@@ -208,6 +219,65 @@ export type DurableRunOrder =
208
219
  */
209
220
  | 'createdAt'
210
221
 
222
+ /**
223
+ * A monotonically increasing number identifying one holding of a run's claim.
224
+ *
225
+ * The load-bearing word is *fencing*. A mutex answers "may I proceed", and a
226
+ * holder that stalls past its lease — a long GC pause, a suspended container,
227
+ * a partitioned network — answers it "yes" and then writes, long after
228
+ * somebody else legitimately took over. A fence answers a different question
229
+ * at the moment of the WRITE: "is the holding I belong to still the current
230
+ * one". Every claim of a run mints a number strictly greater than the last,
231
+ * so a store can reject a write from a superseded holder without knowing
232
+ * anything about processes, clocks or liveness.
233
+ *
234
+ * Not a random token, deliberately: randomness proves identity and cannot
235
+ * establish *order*, and order is the entire mechanism.
236
+ */
237
+ export type ClaimFence = number
238
+
239
+ /** A holding of a run's claim, as issued to the process that took it. */
240
+ export interface RunClaim {
241
+ /** Opaque caller-supplied identity — a worker id, a pod name. Evidence, not authority. */
242
+ readonly holder: string
243
+ /** See {@link ClaimFence}. Present it on every durable write. */
244
+ readonly fence: ClaimFence
245
+ /**
246
+ * Absolute epoch ms after which the claim may be taken by somebody else.
247
+ *
248
+ * Absolute rather than a duration for the same reason a park's deadline
249
+ * is: it has to survive the process that set it. A duration plus an
250
+ * in-process timer cannot — the holder is the thing that dies.
251
+ */
252
+ readonly expiresAt: number
253
+ }
254
+
255
+ /** A run's claim as a listing reports it. */
256
+ export interface ClaimSummary {
257
+ readonly holder: string
258
+ readonly fence: ClaimFence
259
+ readonly expiresAt: number
260
+ /**
261
+ * Whether the claim had expired at the instant the listing was taken.
262
+ *
263
+ * A separate field rather than something the caller derives from
264
+ * `expiresAt`, because the caller would derive it against a DIFFERENT
265
+ * clock than the store used, and one page would then disagree with
266
+ * itself about which rows are available.
267
+ */
268
+ readonly expired: boolean
269
+ }
270
+
271
+ /** What a caller asks for when taking a run. */
272
+ export interface ClaimRunOptions {
273
+ /** Who is taking it. Recorded so an operator can see what holds a stuck run. */
274
+ readonly holder: string
275
+ /** How long the holding is good for, in ms. */
276
+ readonly ttlMs: number
277
+ /** Clock, for tests and so one operation judges every expiry against one instant. */
278
+ readonly now?: number
279
+ }
280
+
211
281
  /** Filters and paging for {@link CheckpointStore.listDurableRuns}. */
212
282
  export interface ListDurableRunsOptions {
213
283
  /**
@@ -223,6 +293,16 @@ export interface ListDurableRunsOptions {
223
293
  * to include it.
224
294
  */
225
295
  readonly park?: readonly ParkState[]
296
+ /**
297
+ * Keep only runs that are, or are not, currently held by a worker.
298
+ *
299
+ * `false` is the queue-reader's filter: give me the work nobody has. A
300
+ * claim that has expired counts as NOT held, because that is what expiry
301
+ * means and a reader that skipped expired claims would leave a dead
302
+ * worker's runs invisible forever — the exact failure the lease exists to
303
+ * prevent.
304
+ */
305
+ readonly claimed?: boolean
226
306
  /** Page size. Defaults to 100, clamped to at least 1. */
227
307
  readonly limit?: number
228
308
  /** Resume token from the previous page's {@link DurableRunPage.cursor}. */
@@ -279,8 +359,27 @@ export interface DurableRunPage {
279
359
  * proceed.
280
360
  */
281
361
  export interface CheckpointStore {
282
- /** Persist one checkpoint. Overwrites an existing checkpoint with the same id. */
283
- writeCheckpoint(scope: CheckpointRunScope, checkpoint: IterationCheckpoint): Promise<void>
362
+ /**
363
+ * Persist one checkpoint. Overwrites an existing checkpoint with the same id.
364
+ *
365
+ * @param fence the {@link ClaimFence} of the holding this write belongs
366
+ * to, when the run is claimed. A store that supports claims REFUSES a
367
+ * write whose fence is below the run's current one — that refusal is
368
+ * what makes a claim a lease rather than a suggestion, because a holder
369
+ * stalled past its expiry believes it still holds and is wrong only at
370
+ * the moment it writes.
371
+ *
372
+ * Omit it and the write is unfenced, which is exactly today's behaviour
373
+ * and correct for a single-writer deployment. A store MUST NOT start
374
+ * refusing unfenced writes because some other write carried a fence:
375
+ * that would make adding a claim to one worker break every worker that
376
+ * has not adopted it yet.
377
+ */
378
+ writeCheckpoint(
379
+ scope: CheckpointRunScope,
380
+ checkpoint: IterationCheckpoint,
381
+ fence?: ClaimFence,
382
+ ): Promise<void>
284
383
 
285
384
  /** Load a single checkpoint by id. Returns `null` when it does not exist. */
286
385
  readCheckpoint(
@@ -342,4 +441,92 @@ export interface CheckpointStore {
342
441
  scope: CheckpointListingScope,
343
442
  options?: ListDurableRunsOptions,
344
443
  ): Promise<DurableRunPage>
444
+
445
+ /**
446
+ * Take exclusive working possession of a run, or report that somebody
447
+ * else has it. OPTIONAL — see the optional-capability rule on this
448
+ * interface.
449
+ *
450
+ * Returns the holding on success and `null` when the run is currently
451
+ * held by somebody else. `null` is not an error: "another worker got
452
+ * there first" is the ordinary outcome of a queue with more than one
453
+ * reader, and a thrown exception would make the normal case look like a
454
+ * fault.
455
+ *
456
+ * ### What it is for
457
+ *
458
+ * Putting parked runs on a queue and letting more than one worker drain
459
+ * it. Without this, two workers restore the same checkpoint, both execute
460
+ * the run's tools, and both write checkpoints under one run id — each
461
+ * write minting a fresh checkpoint id, so two divergent chains land in
462
+ * one list and the pending lookup returns whichever wrote last. Half the
463
+ * work vanishes and nothing reports an error.
464
+ *
465
+ * ### The lease, and why it expires
466
+ *
467
+ * A claim is a LEASE, not a lock. A lock held by a process that dies is
468
+ * held forever, and the runs behind it are unreachable by anything except
469
+ * a human with a shell. The expiry is what makes a dead holder's work
470
+ * recoverable without one.
471
+ *
472
+ * The expiry is also why a fence exists. A holder does not know it has
473
+ * expired — a long pause, a suspended container and a partition all look
474
+ * from the inside like time not passing — so it wakes and writes as
475
+ * though it still holds. Liveness cannot be checked from here. What CAN
476
+ * be checked, at the write, is whether the holding that write belongs to
477
+ * is still the current one, and that is a comparison of two numbers.
478
+ *
479
+ * ### Reclaiming
480
+ *
481
+ * Calling this on a run whose claim has expired SUCCEEDS and mints a
482
+ * fence strictly greater than the expired holding's. The previous holder
483
+ * is not notified — it cannot be, that is the premise — it simply stops
484
+ * being able to write.
485
+ *
486
+ * Calling it again as the CURRENT holder also succeeds and extends the
487
+ * lease, minting a new fence. Renewal and reclamation are the same
488
+ * operation from the store's side, which is why there is no separate
489
+ * `renew`: two code paths that must agree about who holds a run is one
490
+ * more than can be kept correct.
491
+ *
492
+ * ### What a backend implementing this MUST guarantee
493
+ *
494
+ * These are the properties the fence comparison depends on. None of them
495
+ * is checkable from here, and every one of them was violated by the first
496
+ * built-in implementation, so they are written down rather than assumed:
497
+ *
498
+ * 1. **A fence exceeds every fence ever issued for the run** — including
499
+ * across a release, and across deletion of whatever recorded it. A
500
+ * counter that rewinds re-issues a number a stalled worker still
501
+ * believes it holds, and that worker's writes become legal again.
502
+ * 2. **Fences are unique.** The write check is `fence < current`, so two
503
+ * holders at one number are both admitted. Equality is permissive
504
+ * here, which makes a duplicate worse than a gap.
505
+ * 3. **The fence check is atomic with the write.** Reading the current
506
+ * fence and then writing is check-then-act, and the gap is a race. A
507
+ * database gets this free (`UPDATE … WHERE fence >= ?`); a filesystem
508
+ * does not, and the built-in disk store narrows rather than closes it.
509
+ * 4. **`holder` is unique per process.** It is evidence rather than
510
+ * authority, but it is the only thing distinguishing a RENEWAL from a
511
+ * theft — two workers sharing a holder string take a live, unexpired
512
+ * claim from each other instantly. Use something per-process, not a
513
+ * per-deployment name.
514
+ */
515
+ claimRun?(scope: CheckpointRunScope, options: ClaimRunOptions): Promise<RunClaim | null>
516
+
517
+ /**
518
+ * Give a claim up early. Idempotent: releasing a claim that already
519
+ * expired, was superseded, or never existed succeeds as a no-op.
520
+ *
521
+ * Presenting a stale fence releases NOTHING — a worker that stalled past
522
+ * its lease must not be able to hand away a run somebody else is now
523
+ * holding, and that is the same fencing comparison the write path makes.
524
+ *
525
+ * Optional to call, not optional to matter: a worker that finishes and
526
+ * releases returns the run to the queue immediately, where one that just
527
+ * exits leaves it stuck until the lease expires. That is a latency
528
+ * difference, never a correctness one, which is the property that lets a
529
+ * crashed worker be indistinguishable from a slow one.
530
+ */
531
+ releaseRun?(scope: CheckpointRunScope, fence: ClaimFence): Promise<void>
345
532
  }