@proofoftech/flowsafe 0.17.0 → 0.19.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 (78) hide show
  1. package/CHANGELOG.md +103 -0
  2. package/README.md +104 -5
  3. package/deployment-identity-protocol.d.mts +17 -0
  4. package/deployment-identity-protocol.mjs +32 -0
  5. package/dist/agent-host/catalog.js +1 -1
  6. package/dist/agent-host/catalog.js.map +1 -1
  7. package/dist/agent-host/thread-host.d.ts.map +1 -1
  8. package/dist/agent-host/thread-host.js +30 -4
  9. package/dist/agent-host/thread-host.js.map +1 -1
  10. package/dist/agent-runner/durable-agent-runner.d.ts +251 -20
  11. package/dist/agent-runner/durable-agent-runner.d.ts.map +1 -1
  12. package/dist/agent-runner/durable-agent-runner.js +657 -57
  13. package/dist/agent-runner/durable-agent-runner.js.map +1 -1
  14. package/dist/deployment-identity-protocol.d.ts +1 -1
  15. package/dist/deployment-identity-protocol.d.ts.map +1 -1
  16. package/dist/deployment-identity-protocol.js +1 -1
  17. package/dist/deployment-identity-protocol.js.map +1 -1
  18. package/dist/do-runner/deployment-identity.d.ts +2 -11
  19. package/dist/do-runner/deployment-identity.d.ts.map +1 -1
  20. package/dist/do-runner/deployment-identity.js +2 -29
  21. package/dist/do-runner/deployment-identity.js.map +1 -1
  22. package/dist/do-runner/do-error-response.d.ts.map +1 -1
  23. package/dist/do-runner/do-error-response.js +9 -1
  24. package/dist/do-runner/do-error-response.js.map +1 -1
  25. package/dist/do-runner/durable-object.d.ts +13 -1
  26. package/dist/do-runner/durable-object.d.ts.map +1 -1
  27. package/dist/do-runner/durable-object.js +825 -29
  28. package/dist/do-runner/durable-object.js.map +1 -1
  29. package/dist/do-runner/index.d.ts +3 -1
  30. package/dist/do-runner/index.d.ts.map +1 -1
  31. package/dist/do-runner/index.js +2 -1
  32. package/dist/do-runner/index.js.map +1 -1
  33. package/dist/do-runner/runtime.d.ts +43 -0
  34. package/dist/do-runner/runtime.d.ts.map +1 -1
  35. package/dist/do-runner/runtime.js +67 -0
  36. package/dist/do-runner/runtime.js.map +1 -1
  37. package/dist/do-runner/suspension-deadline.d.ts +184 -0
  38. package/dist/do-runner/suspension-deadline.d.ts.map +1 -0
  39. package/dist/do-runner/suspension-deadline.js +452 -0
  40. package/dist/do-runner/suspension-deadline.js.map +1 -0
  41. package/dist/do-runner/thread-do.d.ts +5 -1
  42. package/dist/do-runner/thread-do.d.ts.map +1 -1
  43. package/dist/do-runner/thread-do.js +24 -1
  44. package/dist/do-runner/thread-do.js.map +1 -1
  45. package/dist/signal-providers/delivery.d.ts +32 -0
  46. package/dist/signal-providers/delivery.d.ts.map +1 -1
  47. package/dist/signal-providers/delivery.js +45 -0
  48. package/dist/signal-providers/delivery.js.map +1 -1
  49. package/dist/signal-providers/host-do.d.ts +11 -0
  50. package/dist/signal-providers/host-do.d.ts.map +1 -1
  51. package/dist/signal-providers/host-do.js +48 -14
  52. package/dist/signal-providers/host-do.js.map +1 -1
  53. package/dist/signal-providers/provider.d.ts +8 -0
  54. package/dist/signal-providers/provider.d.ts.map +1 -1
  55. package/dist/signal-providers/provider.js.map +1 -1
  56. package/dist/signal-providers/webhook-route.d.ts +6 -0
  57. package/dist/signal-providers/webhook-route.d.ts.map +1 -1
  58. package/dist/signal-providers/webhook-route.js +109 -21
  59. package/dist/signal-providers/webhook-route.js.map +1 -1
  60. package/dist/signals/client.d.ts +19 -5
  61. package/dist/signals/client.d.ts.map +1 -1
  62. package/dist/signals/client.js +25 -17
  63. package/dist/signals/client.js.map +1 -1
  64. package/dist/signals/index.d.ts +1 -1
  65. package/dist/signals/index.d.ts.map +1 -1
  66. package/dist/signals/index.js.map +1 -1
  67. package/dist/signals/notification-dispatch.d.ts +2 -0
  68. package/dist/signals/notification-dispatch.d.ts.map +1 -1
  69. package/dist/signals/notification-dispatch.js +3 -0
  70. package/dist/signals/notification-dispatch.js.map +1 -1
  71. package/dist/signals/router.d.ts.map +1 -1
  72. package/dist/signals/router.js +3 -2
  73. package/dist/signals/router.js.map +1 -1
  74. package/dist/signals/thread-do-routes.d.ts +48 -10
  75. package/dist/signals/thread-do-routes.d.ts.map +1 -1
  76. package/dist/signals/thread-do-routes.js +624 -118
  77. package/dist/signals/thread-do-routes.js.map +1 -1
  78. package/package.json +8 -6
@@ -0,0 +1,184 @@
1
+ import type { RunSummary } from './runtime.js';
2
+ /** Reserved key a step sets in its `suspend()` payload to arm a deadline. */
3
+ export declare const SUSPENSION_DEADLINE_PAYLOAD_KEY = "flowsafe.deadlineMs";
4
+ /** Reserved key wrapping the resume data an expired deadline resumes with. */
5
+ export declare const SUSPENSION_TIMEOUT_RESUME_KEY = "flowsafe.suspensionTimeout";
6
+ /** Durable Object storage key holding one run's armed deadlines. */
7
+ export declare const SUSPENSION_DEADLINE_STORAGE_KEY = "flowsafe:suspension-deadline:v1";
8
+ /**
9
+ * Requester recorded on a timeout resume. A self-initiated resume must be
10
+ * distinguishable in run provenance from the human or automation that advanced
11
+ * the run to the suspension, so it files under a reserved system id of its own
12
+ * rather than borrowing the recorded requester's identity.
13
+ */
14
+ export declare const SUSPENSION_DEADLINE_PRINCIPAL_ID = "flowsafe-suspension-deadline";
15
+ /** Shortest armable deadline; anything smaller is a tight arm-then-wake loop. */
16
+ export declare const MIN_SUSPENSION_DEADLINE_MS = 1000;
17
+ /** Longest armable deadline (365 days), so week-scale waits still fit. */
18
+ export declare const MAX_SUSPENSION_DEADLINE_MS = 31536000000;
19
+ /** Per-run entry cap; a run cannot grow its object's storage without bound. */
20
+ export declare const MAX_SUSPENSION_DEADLINES_PER_RUN = 32;
21
+ /** Failed timeout resumes tolerated before an entry is abandoned. */
22
+ export declare const MAX_SUSPENSION_DEADLINE_ATTEMPTS = 5;
23
+ /**
24
+ * Mastra's marker for a suspension that happened inside a NESTED workflow: the
25
+ * persisted payload carries the inner step path under `__workflow_meta.path`.
26
+ * Verified against the declared Mastra peer; `runtime.test.ts` pins it, because
27
+ * a rename would otherwise silently re-open the arming of unfenceable nested
28
+ * suspensions. Re-check it on every Mastra upgrade.
29
+ */
30
+ export declare const MASTRA_WORKFLOW_META_KEY = "__workflow_meta";
31
+ export interface SuspensionDeadlineEntry {
32
+ /**
33
+ * The suspended step's DOT-JOINED path, which is how both RunSummary
34
+ * projections key `suspendPayload`, `suspendedAt` and `resumeCount`. Nested
35
+ * paths are never armed, so this is a top-level step id — one that may
36
+ * itself contain a dot.
37
+ */
38
+ step: string;
39
+ /** Epoch milliseconds: the suspension's own time plus its deadline. */
40
+ deadlineAt: number;
41
+ /** Fence: the step's suspension time in the summary that armed this entry. */
42
+ suspendedAt: number;
43
+ /** Fence: the step's resume ordinal, normalized to 0 on a first suspension. */
44
+ resumeCount: number;
45
+ /**
46
+ * Failed timeout resumes so far; absent until the first failure. At the full
47
+ * budget the entry is a TOMBSTONE: abandoned for the suspension it was armed
48
+ * against, never selected and never armed again, and kept in the record only
49
+ * so that suspension cannot re-derive itself a fresh budget.
50
+ */
51
+ attempts?: number;
52
+ /**
53
+ * Epoch milliseconds; paired with `attempts` as its backoff floor. A
54
+ * tombstone carries none, because it is never retried.
55
+ */
56
+ nextAttemptAt?: number;
57
+ /**
58
+ * Epoch milliseconds of the FIRST wake whose authoritative read did not
59
+ * succeed, absent while reads are working. Such a wake charges nothing — an
60
+ * unread run is no evidence about the entry — so this is the only thing
61
+ * bounding an entry whose run became permanently unreadable; the wake that
62
+ * owns it abandons the entry once it has stood for a day. Any successful
63
+ * read clears it.
64
+ */
65
+ unreadableSince?: number;
66
+ }
67
+ export interface SuspensionDeadlineRecord {
68
+ version: 1;
69
+ workflowId: string;
70
+ runId: string;
71
+ entries: SuspensionDeadlineEntry[];
72
+ }
73
+ export interface SuspensionTimeoutEnvelope {
74
+ /** Id of the step whose deadline expired. */
75
+ step: string;
76
+ /** Epoch milliseconds the deadline was armed for. */
77
+ deadlineAt: number;
78
+ /** Epoch milliseconds the wake acted on it. */
79
+ expiredAt: number;
80
+ }
81
+ /** Resume data a timeout resume carries, distinguishable from a real signal. */
82
+ export type SuspensionTimeoutResumeData = {
83
+ [SUSPENSION_TIMEOUT_RESUME_KEY]: SuspensionTimeoutEnvelope;
84
+ };
85
+ /** A derived deadline that will NOT be armed, with the reason to log. */
86
+ export interface RejectedSuspensionDeadline {
87
+ step: string;
88
+ reason: string;
89
+ }
90
+ /**
91
+ * Read every armable deadline out of an authoritative suspended summary.
92
+ *
93
+ * NEVER throws on author data. By the time this runs Mastra has already
94
+ * persisted the suspension, so a malformed `flowsafe.deadlineMs` cannot be
95
+ * rejected by failing the caller: that would leave the run suspended AND the
96
+ * caller erroring. The entry is reported in `rejected` for the caller to log,
97
+ * and the suspension stays an ordinary one.
98
+ *
99
+ * Works on either projection of the same suspension — the live result and the
100
+ * snapshot-rehydrated `status()` — and must agree with itself across the two,
101
+ * because the boundary that arms an entry and the wake that fences it do not
102
+ * read the same one.
103
+ *
104
+ * The arming invariant: an entry may be armed only for a suspension that
105
+ * EXCLUSIVELY OWNS the dot-joined key its fence fields are read from. A
106
+ * nested suspension fails "owns" — the key's fence belongs to the enclosing
107
+ * step — and a key collision fails "exclusively" — two suspensions answer to
108
+ * one key, which Mastra's own snapshot namespace also collides. Refusing is
109
+ * all flowsafe can do there: arming would let a wake deliver one suspension's
110
+ * timeout envelope to an unrelated step. Each refusal below is a detector for
111
+ * one projection's way of breaking that invariant.
112
+ */
113
+ export declare function suspensionDeadlinesOf(summary: RunSummary): {
114
+ entries: SuspensionDeadlineEntry[];
115
+ rejected: RejectedSuspensionDeadline[];
116
+ };
117
+ /**
118
+ * Whether a summary is self-consistent enough to reconcile from: a suspended
119
+ * run always has at least one suspended path, so a 'suspended' status with
120
+ * none describes no suspension anyone could act on. Reconciling from it would
121
+ * derive nothing and wipe the stored record of a run that is still suspended.
122
+ *
123
+ * The SECOND line of defence, not the primary one. It rejects that projection
124
+ * whatever produced it, but it does not cover Mastra's in-memory fallback in
125
+ * general: the fallback reports the Run object's own status, which is
126
+ * 'pending' for a run that has never been resumed — the dominant case, and one
127
+ * this predicate calls readable. The primary guard is the `isFromInMemory`
128
+ * marker check inside `RunnerRuntime.authoritativeStatus`, which is keyed on
129
+ * the read having reached storage rather than on the shape it returned. The
130
+ * two signals are independent, so both are kept.
131
+ */
132
+ export declare function isReadableRunSummary(summary: RunSummary): boolean;
133
+ /** Strict hydration of the stored record; `undefined` in, `undefined` out. */
134
+ export declare function parseSuspensionDeadlineRecord(value: unknown): SuspensionDeadlineRecord | undefined;
135
+ /**
136
+ * A spent entry, kept for the suspension it was armed against: the ledger at
137
+ * the budget, and every field that only means something while it is still
138
+ * being retried — the backoff floor, the unreadable-state clock — dropped,
139
+ * because a tombstone is never selected, never armed and never read again.
140
+ * Lives beside its two recognizers, `abandoned()` and `storedEntry`'s ledger
141
+ * rule, so the shape they accept and the shape written here cannot drift.
142
+ * Exported from this module for the run object that writes it; deliberately
143
+ * NOT from the package barrel, which exposes no part of the stored record.
144
+ */
145
+ export declare function tombstoned(entry: SuspensionDeadlineEntry): SuspensionDeadlineEntry;
146
+ /** Earliest wake the record needs, honouring a retry floor over a due entry. */
147
+ export declare function nextSuspensionDeadlineAt(stored: SuspensionDeadlineRecord | undefined): number | undefined;
148
+ /**
149
+ * Whether a wake at `now` owes this entry work: it has reached its deadline
150
+ * (or the retry floor past it) and its budget is not spent. One wake resumes
151
+ * ONE such entry, but a failed authoritative read is one fact about all of
152
+ * them, so the unreadable-state clock is stamped across the whole due batch.
153
+ */
154
+ export declare function isSuspensionDeadlineDue(entry: SuspensionDeadlineEntry, now: number): boolean;
155
+ /** The one entry a wake at `now` should act on, earliest deadline first. */
156
+ export declare function dueSuspensionDeadline(stored: SuspensionDeadlineRecord | undefined, now: number): SuspensionDeadlineEntry | undefined;
157
+ /** The resume data a timeout resume delivers to the expired step. */
158
+ export declare function suspensionTimeoutResumeData(entry: SuspensionDeadlineEntry, expiredAt: number): SuspensionTimeoutResumeData;
159
+ /**
160
+ * Structural guard a step uses to tell a timeout resume from a real signal,
161
+ * so consumer code branches on this contract instead of a string literal.
162
+ */
163
+ export declare function isSuspensionTimeoutResumeData(value: unknown): value is SuspensionTimeoutResumeData;
164
+ /**
165
+ * Carry the retry ledger of a stored entry onto the freshly derived one for
166
+ * the SAME suspension. An entry whose fence or deadline moved is a different
167
+ * suspension, so it starts with a clean ledger; a stored entry with no derived
168
+ * counterpart is gone and is dropped. This carry is also the tombstone's whole
169
+ * lifecycle: a spent ledger rides every reconcile of the unchanged suspension
170
+ * — which is what keeps the suspension from re-deriving a fresh budget — and
171
+ * is dropped the moment the suspension moves on, so a later suspension of the
172
+ * same step starts over.
173
+ *
174
+ * `unreadableSince` is deliberately NOT carried, and that is what clears it: a
175
+ * reconcile only ever follows evidence that the run is real and current — an
176
+ * authoritative read that succeeded, or a live summary handed back by a
177
+ * lifecycle boundary — and either ends the unreadable stretch the clock was
178
+ * counting. A host that drives boundaries oftener than daily therefore keeps
179
+ * the clock from maturing even while the row read keeps failing; in the case
180
+ * the bound exists for — a registration a deploy dropped for good — the
181
+ * boundaries throw too, so nothing clears it and the bound holds.
182
+ */
183
+ export declare function mergeSuspensionDeadlines(previous: SuspensionDeadlineRecord | undefined, derived: readonly SuspensionDeadlineEntry[]): SuspensionDeadlineEntry[];
184
+ //# sourceMappingURL=suspension-deadline.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"suspension-deadline.d.ts","sourceRoot":"","sources":["../../src/do-runner/suspension-deadline.ts"],"names":[],"mappings":"AAwBA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAE/C,6EAA6E;AAC7E,eAAO,MAAM,+BAA+B,wBAAwB,CAAC;AAErE,8EAA8E;AAC9E,eAAO,MAAM,6BAA6B,+BAA+B,CAAC;AAE1E,oEAAoE;AACpE,eAAO,MAAM,+BAA+B,oCACT,CAAC;AAEpC;;;;;GAKG;AACH,eAAO,MAAM,gCAAgC,iCAAiC,CAAC;AAE/E,iFAAiF;AACjF,eAAO,MAAM,0BAA0B,OAAQ,CAAC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AAO,EAAE,uBAAuB,EAAE,CAAC;IACnC,QAAQ,EAAE,0BAA0B,EAAE,CAAC;CACxC,CA6GA;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,oBAAoB,CAAC,OAAO,EAAE,UAAU,GAAG,OAAO,CAIjE;AAED,8EAA8E;AAC9E,wBAAgB,6BAA6B,CAC3C,KAAK,EAAE,OAAO,GACb,wBAAwB,GAAG,SAAS,CAkBtC;AAED;;;;;;;;;GASG;AACH,wBAAgB,UAAU,CACxB,KAAK,EAAE,uBAAuB,GAC7B,uBAAuB,CAQzB;AAeD,gFAAgF;AAChF,wBAAgB,wBAAwB,CACtC,MAAM,EAAE,wBAAwB,GAAG,SAAS,GAC3C,MAAM,GAAG,SAAS,CAQpB;AAED;;;;;GAKG;AACH,wBAAgB,uBAAuB,CACrC,KAAK,EAAE,uBAAuB,EAC9B,GAAG,EAAE,MAAM,GACV,OAAO,CAET;AAED,4EAA4E;AAC5E,wBAAgB,qBAAqB,CACnC,MAAM,EAAE,wBAAwB,GAAG,SAAS,EAC5C,GAAG,EAAE,MAAM,GACV,uBAAuB,GAAG,SAAS,CAOrC;AAED,qEAAqE;AACrE,wBAAgB,2BAA2B,CACzC,KAAK,EAAE,uBAAuB,EAC9B,SAAS,EAAE,MAAM,GAChB,2BAA2B,CAQ7B;AAED;;;GAGG;AACH,wBAAgB,6BAA6B,CAC3C,KAAK,EAAE,OAAO,GACb,KAAK,IAAI,2BAA2B,CAQtC;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,wBAAwB,CACtC,QAAQ,EAAE,wBAAwB,GAAG,SAAS,EAC9C,OAAO,EAAE,SAAS,uBAAuB,EAAE,GAC1C,uBAAuB,EAAE,CAqB3B"}
@@ -0,0 +1,452 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ // Per-suspension deadlines — the durable wake schedule that lets a run resume
3
+ // itself when the signal a suspended step waits for never arrives.
4
+ //
5
+ // `suspend()` is Mastra's, not flowsafe's, and a suspension is the one
6
+ // lifecycle transition the runtime persists nothing of its own for: the
7
+ // snapshot it writes is Mastra's workflow state. So the arming value cannot be
8
+ // a flowsafe option — it travels inside the payload the step hands `suspend()`,
9
+ // under a reserved dotted key (the `flowsafe.` convention the request-context
10
+ // keys already use), and this module reads it back out of the authoritative
11
+ // RunSummary that start, resume, and status already project.
12
+ //
13
+ // The derived record lives in Durable Object storage, NOT in the snapshot's
14
+ // requestContext beside RunLifecycleState (run-lifecycle.ts). A suspension
15
+ // deadline is one object's wake schedule: it has no meaning once that object's
16
+ // run leaves 'suspended', and the alarm that consumes it must read it before it
17
+ // has loaded any snapshot. Storage is also why the parse below is strict —
18
+ // stored state is untrusted input, so every field is validated and a malformed
19
+ // record throws instead of being coerced, exactly as parseRunLifecycle does.
20
+ //
21
+ // Everything here is pure; arming, wake scheduling, and the timeout resume live
22
+ // in durable-object.ts, so a non-DO host (tests, local runners) is unaffected.
23
+ import { isPathSafeId } from './path-safe-id.js';
24
+ /** Reserved key a step sets in its `suspend()` payload to arm a deadline. */
25
+ export const SUSPENSION_DEADLINE_PAYLOAD_KEY = 'flowsafe.deadlineMs';
26
+ /** Reserved key wrapping the resume data an expired deadline resumes with. */
27
+ export const SUSPENSION_TIMEOUT_RESUME_KEY = 'flowsafe.suspensionTimeout';
28
+ /** Durable Object storage key holding one run's armed deadlines. */
29
+ export const SUSPENSION_DEADLINE_STORAGE_KEY = 'flowsafe:suspension-deadline:v1';
30
+ /**
31
+ * Requester recorded on a timeout resume. A self-initiated resume must be
32
+ * distinguishable in run provenance from the human or automation that advanced
33
+ * the run to the suspension, so it files under a reserved system id of its own
34
+ * rather than borrowing the recorded requester's identity.
35
+ */
36
+ export const SUSPENSION_DEADLINE_PRINCIPAL_ID = 'flowsafe-suspension-deadline';
37
+ /** Shortest armable deadline; anything smaller is a tight arm-then-wake loop. */
38
+ export const MIN_SUSPENSION_DEADLINE_MS = 1_000;
39
+ /** Longest armable deadline (365 days), so week-scale waits still fit. */
40
+ export const MAX_SUSPENSION_DEADLINE_MS = 31_536_000_000;
41
+ /** Per-run entry cap; a run cannot grow its object's storage without bound. */
42
+ export const MAX_SUSPENSION_DEADLINES_PER_RUN = 32;
43
+ /** Failed timeout resumes tolerated before an entry is abandoned. */
44
+ export const MAX_SUSPENSION_DEADLINE_ATTEMPTS = 5;
45
+ /**
46
+ * Mastra's marker for a suspension that happened inside a NESTED workflow: the
47
+ * persisted payload carries the inner step path under `__workflow_meta.path`.
48
+ * Verified against the declared Mastra peer; `runtime.test.ts` pins it, because
49
+ * a rename would otherwise silently re-open the arming of unfenceable nested
50
+ * suspensions. Re-check it on every Mastra upgrade.
51
+ */
52
+ export const MASTRA_WORKFLOW_META_KEY = '__workflow_meta';
53
+ // Step ids are bounded for the same reason every other stored free-text field
54
+ // is: a stored record must stay small enough to read back on every alarm wake.
55
+ const MAX_SUSPENSION_DEADLINE_STEP_LENGTH = 500;
56
+ const MALFORMED = 'stored suspension deadline is malformed';
57
+ function record(value) {
58
+ return value !== null && typeof value === 'object' && !Array.isArray(value)
59
+ ? value
60
+ : undefined;
61
+ }
62
+ // Every number this module persists — an epoch millisecond and the resume
63
+ // ordinal alike — has to clear the same bar, so the predicate is named for the
64
+ // bar rather than for one of its callers. run-lifecycle.ts names the identical
65
+ // check `validTime` because there it only ever guards timestamps.
66
+ function nonNegativeInteger(value) {
67
+ return Number.isSafeInteger(value) && value >= 0;
68
+ }
69
+ function validStep(value) {
70
+ return (typeof value === 'string' &&
71
+ value.length > 0 &&
72
+ value.length <= MAX_SUSPENSION_DEADLINE_STEP_LENGTH);
73
+ }
74
+ // Read Mastra's nesting marker defensively: the payload is author data carrying
75
+ // one framework-owned field, so a renamed, missing or reshaped marker must
76
+ // degrade to "not nested" here rather than throw on a suspension flowsafe has
77
+ // already been handed. The tripwire test is what makes such a rename loud.
78
+ function nestedPath(payload) {
79
+ const path = record(payload[MASTRA_WORKFLOW_META_KEY])?.path;
80
+ return Array.isArray(path) && path.length > 0 ? path : undefined;
81
+ }
82
+ /**
83
+ * Detector for the "exclusively" half of the arming invariant (see
84
+ * suspensionDeadlinesOf): every joined key that more than one suspension of
85
+ * this run answers to. Each projection shows a collision differently, so both
86
+ * rules are needed: two suspended paths joining to one key can only appear on
87
+ * the live projection (the rehydrated one splits unique stored keys), and a
88
+ * key implied by another suspended key's nesting marker only on the rehydrated
89
+ * one (live nested payloads carry no marker).
90
+ *
91
+ * Both rules read only each suspended key's OWN payload, never the
92
+ * nested-payload fallback below, which deliberately reads a different step's
93
+ * payload.
94
+ */
95
+ function ambiguousSuspendedKeys(suspended, payloads) {
96
+ const seen = new Set();
97
+ const ambiguous = new Set();
98
+ for (const path of suspended) {
99
+ const key = path.join('.');
100
+ if (seen.has(key))
101
+ ambiguous.add(key);
102
+ seen.add(key);
103
+ const payload = record(payloads?.[key]);
104
+ const nested = payload && nestedPath(payload);
105
+ if (nested)
106
+ ambiguous.add([key, ...nested].join('.'));
107
+ }
108
+ return ambiguous;
109
+ }
110
+ function storedEntry(value) {
111
+ const stored = record(value);
112
+ if (!stored)
113
+ throw new Error(MALFORMED);
114
+ const { attempts, nextAttemptAt, unreadableSince } = stored;
115
+ // A ledger below the budget is still retrying and needs its floor; one AT the
116
+ // budget is a tombstone, which is never selected again and so must carry no
117
+ // floor. The two fields are otherwise one fact — a count with no floor would
118
+ // retry immediately, a floor with no count would retry forever.
119
+ const retrying = attempts !== undefined && attempts !== MAX_SUSPENSION_DEADLINE_ATTEMPTS;
120
+ if (!validStep(stored.step) ||
121
+ !nonNegativeInteger(stored.deadlineAt) ||
122
+ !nonNegativeInteger(stored.suspendedAt) ||
123
+ !nonNegativeInteger(stored.resumeCount) ||
124
+ // The budget itself reads back, because a spent one stays as a tombstone.
125
+ // More than the budget cannot have been written here, and accepting it
126
+ // would let a foreign writer make this module's own log say "after 6".
127
+ (attempts !== undefined &&
128
+ (!Number.isSafeInteger(attempts) ||
129
+ attempts < 1 ||
130
+ attempts > MAX_SUSPENSION_DEADLINE_ATTEMPTS)) ||
131
+ (nextAttemptAt !== undefined) !== retrying ||
132
+ (nextAttemptAt !== undefined && !nonNegativeInteger(nextAttemptAt)) ||
133
+ // Unpaired with the ledger, unlike the floor above: it records how long
134
+ // reads have been failing, which is independent of how many resumes have.
135
+ (unreadableSince !== undefined && !nonNegativeInteger(unreadableSince))) {
136
+ throw new Error(MALFORMED);
137
+ }
138
+ return {
139
+ step: stored.step,
140
+ deadlineAt: stored.deadlineAt,
141
+ suspendedAt: stored.suspendedAt,
142
+ resumeCount: stored.resumeCount,
143
+ ...(attempts === undefined ? {} : { attempts: attempts }),
144
+ ...(nextAttemptAt === undefined
145
+ ? {}
146
+ : { nextAttemptAt: nextAttemptAt }),
147
+ ...(unreadableSince === undefined
148
+ ? {}
149
+ : { unreadableSince: unreadableSince }),
150
+ };
151
+ }
152
+ // Total order on (deadlineAt, step) so the entry a wake acts on is
153
+ // deterministic even for equal deadlines. It does not order the stored record:
154
+ // a retry charge appends the entry it re-armed.
155
+ function byDueThenStep(left, right) {
156
+ if (left.deadlineAt !== right.deadlineAt) {
157
+ return left.deadlineAt - right.deadlineAt;
158
+ }
159
+ return left.step < right.step ? -1 : left.step > right.step ? 1 : 0;
160
+ }
161
+ /**
162
+ * Read every armable deadline out of an authoritative suspended summary.
163
+ *
164
+ * NEVER throws on author data. By the time this runs Mastra has already
165
+ * persisted the suspension, so a malformed `flowsafe.deadlineMs` cannot be
166
+ * rejected by failing the caller: that would leave the run suspended AND the
167
+ * caller erroring. The entry is reported in `rejected` for the caller to log,
168
+ * and the suspension stays an ordinary one.
169
+ *
170
+ * Works on either projection of the same suspension — the live result and the
171
+ * snapshot-rehydrated `status()` — and must agree with itself across the two,
172
+ * because the boundary that arms an entry and the wake that fences it do not
173
+ * read the same one.
174
+ *
175
+ * The arming invariant: an entry may be armed only for a suspension that
176
+ * EXCLUSIVELY OWNS the dot-joined key its fence fields are read from. A
177
+ * nested suspension fails "owns" — the key's fence belongs to the enclosing
178
+ * step — and a key collision fails "exclusively" — two suspensions answer to
179
+ * one key, which Mastra's own snapshot namespace also collides. Refusing is
180
+ * all flowsafe can do there: arming would let a wake deliver one suspension's
181
+ * timeout envelope to an unrelated step. Each refusal below is a detector for
182
+ * one projection's way of breaking that invariant.
183
+ */
184
+ export function suspensionDeadlinesOf(summary) {
185
+ const entries = [];
186
+ const rejected = [];
187
+ if (summary.status !== 'suspended')
188
+ return { entries, rejected };
189
+ const payloads = record(summary.suspendPayload);
190
+ const suspended = summary.suspended ?? [];
191
+ const ambiguous = ambiguousSuspendedKeys(suspended, payloads);
192
+ for (const path of suspended) {
193
+ // The DOT-JOINED path is the entry key, because it is what indexes
194
+ // `suspendPayload`, `suspendedAt` and `resumeCount` in BOTH projections
195
+ // (['a.b'] live, ['a','b'] rehydrated), so the wake's fence recognizes
196
+ // what a boundary armed.
197
+ const step = path.join('.');
198
+ const payload = record(payloads?.[step]);
199
+ // A LIVE nested suspension is the one shape whose payload is NOT under its
200
+ // own path: Mastra reports the nested path but keys the payload by the
201
+ // TOP-LEVEL step. Reading it there is what turns a nested arming request
202
+ // into a loud rejection instead of silence.
203
+ const nestedPayload = payload === undefined && path.length > 1
204
+ ? record(payloads?.[path[0]])
205
+ : undefined;
206
+ const armed = payload ?? nestedPayload;
207
+ // No payload, or no reserved key in it: an ordinary suspension, silently.
208
+ if (!armed)
209
+ continue;
210
+ const deadlineMs = armed[SUSPENSION_DEADLINE_PAYLOAD_KEY];
211
+ if (deadlineMs === undefined)
212
+ continue;
213
+ // "Exclusively" is checked before "owns" so the log names the sharper
214
+ // reason, and after the silent exits above so a run that asked for nothing
215
+ // still says nothing.
216
+ if (ambiguous.has(step)) {
217
+ rejected.push({ step, reason: 'ambiguous suspended step path' });
218
+ continue;
219
+ }
220
+ // Detector for the "owns" half of the invariant, and a LOUD one — silence
221
+ // here would look to the author exactly like a deadline that was accepted.
222
+ // Each projection presents a nested suspension its own way: the live one
223
+ // as a multi-segment path whose payload sits under its first segment
224
+ // (`nestedPayload` above), the rehydrated one as the top-level step alone,
225
+ // recognizable only by Mastra's nesting marker inside the payload.
226
+ if (nestedPayload !== undefined || nestedPath(armed) !== undefined) {
227
+ rejected.push({
228
+ step,
229
+ reason: 'nested suspension paths are not supported',
230
+ });
231
+ continue;
232
+ }
233
+ if (!validStep(step)) {
234
+ rejected.push({ step, reason: 'suspended step id is unusable' });
235
+ continue;
236
+ }
237
+ if (!Number.isSafeInteger(deadlineMs)) {
238
+ rejected.push({
239
+ step,
240
+ reason: `${SUSPENSION_DEADLINE_PAYLOAD_KEY} must be a safe integer`,
241
+ });
242
+ continue;
243
+ }
244
+ if (deadlineMs < MIN_SUSPENSION_DEADLINE_MS ||
245
+ deadlineMs > MAX_SUSPENSION_DEADLINE_MS) {
246
+ rejected.push({
247
+ step,
248
+ reason: `${SUSPENSION_DEADLINE_PAYLOAD_KEY} must be between ${MIN_SUSPENSION_DEADLINE_MS} and ${MAX_SUSPENSION_DEADLINE_MS} ms`,
249
+ });
250
+ continue;
251
+ }
252
+ const suspendedAt = summary.suspendedAt?.[step];
253
+ // A deadline that cannot be fenced must not be armed: without suspendedAt
254
+ // the alarm could not tell this suspension from a later one at the same
255
+ // step, and would resume a run a real signal had already moved on.
256
+ if (!nonNegativeInteger(suspendedAt)) {
257
+ rejected.push({ step, reason: 'no suspendedAt fence' });
258
+ continue;
259
+ }
260
+ // Absent resumeCount is normal on a step's FIRST suspension; the runtime
261
+ // only stamps the ordinal once the step has been resumed.
262
+ const resumeCount = summary.resumeCount?.[step];
263
+ if (resumeCount !== undefined && !nonNegativeInteger(resumeCount)) {
264
+ rejected.push({ step, reason: 'resumeCount fence is malformed' });
265
+ continue;
266
+ }
267
+ // suspendedAt + deadlineMs, never now + deadlineMs: re-deriving from an
268
+ // unchanged suspension must yield a byte-identical entry, or repeated
269
+ // reconciliation would walk the deadline forward and it would never fire.
270
+ const deadlineAt = suspendedAt + deadlineMs;
271
+ if (!Number.isSafeInteger(deadlineAt)) {
272
+ rejected.push({
273
+ step,
274
+ reason: `${SUSPENSION_DEADLINE_PAYLOAD_KEY} exceeds the supported range`,
275
+ });
276
+ continue;
277
+ }
278
+ entries.push({
279
+ step,
280
+ deadlineAt,
281
+ suspendedAt,
282
+ resumeCount: resumeCount ?? 0,
283
+ });
284
+ }
285
+ entries.sort(byDueThenStep);
286
+ for (const overflow of entries.splice(MAX_SUSPENSION_DEADLINES_PER_RUN)) {
287
+ rejected.push({
288
+ step: overflow.step,
289
+ reason: `run already arms ${MAX_SUSPENSION_DEADLINES_PER_RUN} suspension deadlines`,
290
+ });
291
+ }
292
+ return { entries, rejected };
293
+ }
294
+ /**
295
+ * Whether a summary is self-consistent enough to reconcile from: a suspended
296
+ * run always has at least one suspended path, so a 'suspended' status with
297
+ * none describes no suspension anyone could act on. Reconciling from it would
298
+ * derive nothing and wipe the stored record of a run that is still suspended.
299
+ *
300
+ * The SECOND line of defence, not the primary one. It rejects that projection
301
+ * whatever produced it, but it does not cover Mastra's in-memory fallback in
302
+ * general: the fallback reports the Run object's own status, which is
303
+ * 'pending' for a run that has never been resumed — the dominant case, and one
304
+ * this predicate calls readable. The primary guard is the `isFromInMemory`
305
+ * marker check inside `RunnerRuntime.authoritativeStatus`, which is keyed on
306
+ * the read having reached storage rather than on the shape it returned. The
307
+ * two signals are independent, so both are kept.
308
+ */
309
+ export function isReadableRunSummary(summary) {
310
+ return !(summary.status === 'suspended' && (summary.suspended ?? []).length === 0);
311
+ }
312
+ /** Strict hydration of the stored record; `undefined` in, `undefined` out. */
313
+ export function parseSuspensionDeadlineRecord(value) {
314
+ if (value === undefined)
315
+ return undefined;
316
+ const stored = record(value);
317
+ if (stored?.version !== 1 ||
318
+ !isPathSafeId(stored.workflowId) ||
319
+ !isPathSafeId(stored.runId) ||
320
+ !Array.isArray(stored.entries) ||
321
+ stored.entries.length > MAX_SUSPENSION_DEADLINES_PER_RUN) {
322
+ throw new Error(MALFORMED);
323
+ }
324
+ return {
325
+ version: 1,
326
+ workflowId: stored.workflowId,
327
+ runId: stored.runId,
328
+ entries: stored.entries.map(storedEntry),
329
+ };
330
+ }
331
+ /**
332
+ * A spent entry, kept for the suspension it was armed against: the ledger at
333
+ * the budget, and every field that only means something while it is still
334
+ * being retried — the backoff floor, the unreadable-state clock — dropped,
335
+ * because a tombstone is never selected, never armed and never read again.
336
+ * Lives beside its two recognizers, `abandoned()` and `storedEntry`'s ledger
337
+ * rule, so the shape they accept and the shape written here cannot drift.
338
+ * Exported from this module for the run object that writes it; deliberately
339
+ * NOT from the package barrel, which exposes no part of the stored record.
340
+ */
341
+ export function tombstoned(entry) {
342
+ return {
343
+ step: entry.step,
344
+ deadlineAt: entry.deadlineAt,
345
+ suspendedAt: entry.suspendedAt,
346
+ resumeCount: entry.resumeCount,
347
+ attempts: MAX_SUSPENSION_DEADLINE_ATTEMPTS,
348
+ };
349
+ }
350
+ // A spent ledger is a tombstone: the entry stays stored so its suspension
351
+ // cannot re-derive itself a fresh budget, but both selectors below must skip
352
+ // it — selecting it would retry past the budget, and arming for it would wake
353
+ // the object at its long-past deadline forever.
354
+ function abandoned(entry) {
355
+ return (entry.attempts ?? 0) >= MAX_SUSPENSION_DEADLINE_ATTEMPTS;
356
+ }
357
+ /** When an entry needs its wake: the deadline, or the retry floor past it. */
358
+ function dueAt(entry) {
359
+ return Math.max(entry.deadlineAt, entry.nextAttemptAt ?? 0);
360
+ }
361
+ /** Earliest wake the record needs, honouring a retry floor over a due entry. */
362
+ export function nextSuspensionDeadlineAt(stored) {
363
+ let next;
364
+ for (const entry of stored?.entries ?? []) {
365
+ if (abandoned(entry))
366
+ continue;
367
+ const due = dueAt(entry);
368
+ if (next === undefined || due < next)
369
+ next = due;
370
+ }
371
+ return next;
372
+ }
373
+ /**
374
+ * Whether a wake at `now` owes this entry work: it has reached its deadline
375
+ * (or the retry floor past it) and its budget is not spent. One wake resumes
376
+ * ONE such entry, but a failed authoritative read is one fact about all of
377
+ * them, so the unreadable-state clock is stamped across the whole due batch.
378
+ */
379
+ export function isSuspensionDeadlineDue(entry, now) {
380
+ return !abandoned(entry) && dueAt(entry) <= now;
381
+ }
382
+ /** The one entry a wake at `now` should act on, earliest deadline first. */
383
+ export function dueSuspensionDeadline(stored, now) {
384
+ let due;
385
+ for (const entry of stored?.entries ?? []) {
386
+ if (!isSuspensionDeadlineDue(entry, now))
387
+ continue;
388
+ if (!due || byDueThenStep(entry, due) < 0)
389
+ due = entry;
390
+ }
391
+ return due;
392
+ }
393
+ /** The resume data a timeout resume delivers to the expired step. */
394
+ export function suspensionTimeoutResumeData(entry, expiredAt) {
395
+ return {
396
+ [SUSPENSION_TIMEOUT_RESUME_KEY]: {
397
+ step: entry.step,
398
+ deadlineAt: entry.deadlineAt,
399
+ expiredAt,
400
+ },
401
+ };
402
+ }
403
+ /**
404
+ * Structural guard a step uses to tell a timeout resume from a real signal,
405
+ * so consumer code branches on this contract instead of a string literal.
406
+ */
407
+ export function isSuspensionTimeoutResumeData(value) {
408
+ const envelope = record(record(value)?.[SUSPENSION_TIMEOUT_RESUME_KEY]);
409
+ return (envelope !== undefined &&
410
+ validStep(envelope.step) &&
411
+ nonNegativeInteger(envelope.deadlineAt) &&
412
+ nonNegativeInteger(envelope.expiredAt));
413
+ }
414
+ /**
415
+ * Carry the retry ledger of a stored entry onto the freshly derived one for
416
+ * the SAME suspension. An entry whose fence or deadline moved is a different
417
+ * suspension, so it starts with a clean ledger; a stored entry with no derived
418
+ * counterpart is gone and is dropped. This carry is also the tombstone's whole
419
+ * lifecycle: a spent ledger rides every reconcile of the unchanged suspension
420
+ * — which is what keeps the suspension from re-deriving a fresh budget — and
421
+ * is dropped the moment the suspension moves on, so a later suspension of the
422
+ * same step starts over.
423
+ *
424
+ * `unreadableSince` is deliberately NOT carried, and that is what clears it: a
425
+ * reconcile only ever follows evidence that the run is real and current — an
426
+ * authoritative read that succeeded, or a live summary handed back by a
427
+ * lifecycle boundary — and either ends the unreadable stretch the clock was
428
+ * counting. A host that drives boundaries oftener than daily therefore keeps
429
+ * the clock from maturing even while the row read keeps failing; in the case
430
+ * the bound exists for — a registration a deploy dropped for good — the
431
+ * boundaries throw too, so nothing clears it and the bound holds.
432
+ */
433
+ export function mergeSuspensionDeadlines(previous, derived) {
434
+ return derived.map((entry) => {
435
+ const carried = previous?.entries.find((stored) => stored.step === entry.step &&
436
+ stored.suspendedAt === entry.suspendedAt &&
437
+ stored.resumeCount === entry.resumeCount &&
438
+ stored.deadlineAt === entry.deadlineAt);
439
+ return carried?.attempts === undefined
440
+ ? entry
441
+ : {
442
+ ...entry,
443
+ attempts: carried.attempts,
444
+ // A tombstone carries no floor; spreading `undefined` back in would
445
+ // store a property the strict parser then has to treat as absent.
446
+ ...(carried.nextAttemptAt === undefined
447
+ ? {}
448
+ : { nextAttemptAt: carried.nextAttemptAt }),
449
+ };
450
+ });
451
+ }
452
+ //# sourceMappingURL=suspension-deadline.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"suspension-deadline.js","sourceRoot":"","sources":["../../src/do-runner/suspension-deadline.ts"],"names":[],"mappings":"AAAA,sCAAsC;AACtC,8EAA8E;AAC9E,mEAAmE;AACnE,EAAE;AACF,uEAAuE;AACvE,wEAAwE;AACxE,+EAA+E;AAC/E,gFAAgF;AAChF,8EAA8E;AAC9E,4EAA4E;AAC5E,6DAA6D;AAC7D,EAAE;AACF,4EAA4E;AAC5E,2EAA2E;AAC3E,+EAA+E;AAC/E,gFAAgF;AAChF,2EAA2E;AAC3E,+EAA+E;AAC/E,6EAA6E;AAC7E,EAAE;AACF,gFAAgF;AAChF,+EAA+E;AAE/E,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAGjD,6EAA6E;AAC7E,MAAM,CAAC,MAAM,+BAA+B,GAAG,qBAAqB,CAAC;AAErE,8EAA8E;AAC9E,MAAM,CAAC,MAAM,6BAA6B,GAAG,4BAA4B,CAAC;AAE1E,oEAAoE;AACpE,MAAM,CAAC,MAAM,+BAA+B,GAC1C,iCAAiC,CAAC;AAEpC;;;;;GAKG;AACH,MAA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