@proofoftech/flowsafe 0.19.0 → 0.20.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 (172) hide show
  1. package/CHANGELOG.md +25 -0
  2. package/README.md +15 -0
  3. package/deployment-identity-protocol.d.mts +45 -1
  4. package/deployment-identity-protocol.mjs +169 -5
  5. package/dist/agent-host/index.d.ts +1 -1
  6. package/dist/agent-host/index.d.ts.map +1 -1
  7. package/dist/agent-host/index.js.map +1 -1
  8. package/dist/agent-host/router.d.ts.map +1 -1
  9. package/dist/agent-host/router.js +6 -2
  10. package/dist/agent-host/router.js.map +1 -1
  11. package/dist/agent-host/thread-host.d.ts +11 -0
  12. package/dist/agent-host/thread-host.d.ts.map +1 -1
  13. package/dist/agent-host/thread-host.js +53 -1
  14. package/dist/agent-host/thread-host.js.map +1 -1
  15. package/dist/agent-host/thread-topology.d.ts +35 -1
  16. package/dist/agent-host/thread-topology.d.ts.map +1 -1
  17. package/dist/agent-host/thread-topology.js +103 -7
  18. package/dist/agent-host/thread-topology.js.map +1 -1
  19. package/dist/agent-runner/durable-agent-runner.d.ts +19 -4
  20. package/dist/agent-runner/durable-agent-runner.d.ts.map +1 -1
  21. package/dist/agent-runner/durable-agent-runner.js +58 -28
  22. package/dist/agent-runner/durable-agent-runner.js.map +1 -1
  23. package/dist/approval-api/d1-store.d.ts.map +1 -1
  24. package/dist/approval-api/d1-store.js +6 -3
  25. package/dist/approval-api/d1-store.js.map +1 -1
  26. package/dist/approval-api/principal-identity.d.ts +32 -0
  27. package/dist/approval-api/principal-identity.d.ts.map +1 -0
  28. package/dist/approval-api/principal-identity.js +69 -0
  29. package/dist/approval-api/principal-identity.js.map +1 -0
  30. package/dist/approval-api/principal.d.ts +9 -12
  31. package/dist/approval-api/principal.d.ts.map +1 -1
  32. package/dist/approval-api/principal.js +9 -45
  33. package/dist/approval-api/principal.js.map +1 -1
  34. package/dist/approval-api/router.d.ts.map +1 -1
  35. package/dist/approval-api/router.js +9 -1
  36. package/dist/approval-api/router.js.map +1 -1
  37. package/dist/approval-api/service.d.ts +19 -0
  38. package/dist/approval-api/service.d.ts.map +1 -1
  39. package/dist/approval-api/service.js +41 -10
  40. package/dist/approval-api/service.js.map +1 -1
  41. package/dist/approval-api/types.d.ts +12 -0
  42. package/dist/approval-api/types.d.ts.map +1 -1
  43. package/dist/approval-api/types.js +12 -0
  44. package/dist/approval-api/types.js.map +1 -1
  45. package/dist/background-tasks/host.d.ts +127 -14
  46. package/dist/background-tasks/host.d.ts.map +1 -1
  47. package/dist/background-tasks/host.js +579 -50
  48. package/dist/background-tasks/host.js.map +1 -1
  49. package/dist/background-tasks/index.d.ts +2 -2
  50. package/dist/background-tasks/index.d.ts.map +1 -1
  51. package/dist/background-tasks/index.js +11 -6
  52. package/dist/background-tasks/index.js.map +1 -1
  53. package/dist/background-tasks/routes.d.ts +19 -2
  54. package/dist/background-tasks/routes.d.ts.map +1 -1
  55. package/dist/background-tasks/routes.js +4 -4
  56. package/dist/background-tasks/routes.js.map +1 -1
  57. package/dist/deployment-identity-protocol.d.ts +2 -2
  58. package/dist/deployment-identity-protocol.d.ts.map +1 -1
  59. package/dist/deployment-identity-protocol.js +1 -1
  60. package/dist/deployment-identity-protocol.js.map +1 -1
  61. package/dist/do-runner/cause-chain.d.ts +53 -0
  62. package/dist/do-runner/cause-chain.d.ts.map +1 -0
  63. package/dist/do-runner/cause-chain.js +96 -0
  64. package/dist/do-runner/cause-chain.js.map +1 -0
  65. package/dist/do-runner/d1-storage.d.ts +109 -5
  66. package/dist/do-runner/d1-storage.d.ts.map +1 -1
  67. package/dist/do-runner/d1-storage.js +289 -44
  68. package/dist/do-runner/d1-storage.js.map +1 -1
  69. package/dist/do-runner/deployment-identity.d.ts +29 -5
  70. package/dist/do-runner/deployment-identity.d.ts.map +1 -1
  71. package/dist/do-runner/deployment-identity.js +23 -6
  72. package/dist/do-runner/deployment-identity.js.map +1 -1
  73. package/dist/do-runner/do-error-response.d.ts +4 -23
  74. package/dist/do-runner/do-error-response.d.ts.map +1 -1
  75. package/dist/do-runner/do-error-response.js +19 -24
  76. package/dist/do-runner/do-error-response.js.map +1 -1
  77. package/dist/do-runner/do-status-error.d.ts +37 -0
  78. package/dist/do-runner/do-status-error.d.ts.map +1 -0
  79. package/dist/do-runner/do-status-error.js +42 -0
  80. package/dist/do-runner/do-status-error.js.map +1 -0
  81. package/dist/do-runner/durable-object.d.ts +2 -1
  82. package/dist/do-runner/durable-object.d.ts.map +1 -1
  83. package/dist/do-runner/durable-object.js +205 -19
  84. package/dist/do-runner/durable-object.js.map +1 -1
  85. package/dist/do-runner/execution-fence.d.ts +335 -0
  86. package/dist/do-runner/execution-fence.d.ts.map +1 -0
  87. package/dist/do-runner/execution-fence.js +534 -0
  88. package/dist/do-runner/execution-fence.js.map +1 -0
  89. package/dist/do-runner/index.d.ts +10 -3
  90. package/dist/do-runner/index.d.ts.map +1 -1
  91. package/dist/do-runner/index.js +16 -3
  92. package/dist/do-runner/index.js.map +1 -1
  93. package/dist/do-runner/init.d.ts +82 -1
  94. package/dist/do-runner/init.d.ts.map +1 -1
  95. package/dist/do-runner/init.js +58 -3
  96. package/dist/do-runner/init.js.map +1 -1
  97. package/dist/do-runner/inventory.d.ts +294 -0
  98. package/dist/do-runner/inventory.d.ts.map +1 -0
  99. package/dist/do-runner/inventory.js +811 -0
  100. package/dist/do-runner/inventory.js.map +1 -0
  101. package/dist/do-runner/notification-predicate.d.ts +8 -0
  102. package/dist/do-runner/notification-predicate.d.ts.map +1 -0
  103. package/dist/do-runner/notification-predicate.js +9 -0
  104. package/dist/do-runner/notification-predicate.js.map +1 -0
  105. package/dist/do-runner/runtime.d.ts +83 -0
  106. package/dist/do-runner/runtime.d.ts.map +1 -1
  107. package/dist/do-runner/runtime.js +131 -4
  108. package/dist/do-runner/runtime.js.map +1 -1
  109. package/dist/do-runner/start-idempotency.d.ts +508 -0
  110. package/dist/do-runner/start-idempotency.d.ts.map +1 -0
  111. package/dist/do-runner/start-idempotency.js +886 -0
  112. package/dist/do-runner/start-idempotency.js.map +1 -0
  113. package/dist/goals/objective-routes.d.ts +14 -0
  114. package/dist/goals/objective-routes.d.ts.map +1 -1
  115. package/dist/goals/objective-routes.js +45 -24
  116. package/dist/goals/objective-routes.js.map +1 -1
  117. package/dist/host-kit/do-run-topology.d.ts +15 -0
  118. package/dist/host-kit/do-run-topology.d.ts.map +1 -1
  119. package/dist/host-kit/do-run-topology.js +22 -1
  120. package/dist/host-kit/do-run-topology.js.map +1 -1
  121. package/dist/host-kit/flowsafe-worker.d.ts +11 -0
  122. package/dist/host-kit/flowsafe-worker.d.ts.map +1 -1
  123. package/dist/host-kit/flowsafe-worker.js +343 -62
  124. package/dist/host-kit/flowsafe-worker.js.map +1 -1
  125. package/dist/host-kit/host-approval-service.d.ts +20 -0
  126. package/dist/host-kit/host-approval-service.d.ts.map +1 -1
  127. package/dist/host-kit/host-approval-service.js +6 -0
  128. package/dist/host-kit/host-approval-service.js.map +1 -1
  129. package/dist/host-kit/index.d.ts +2 -2
  130. package/dist/host-kit/index.d.ts.map +1 -1
  131. package/dist/host-kit/index.js +1 -1
  132. package/dist/host-kit/index.js.map +1 -1
  133. package/dist/host-kit/run-route-error.d.ts +18 -0
  134. package/dist/host-kit/run-route-error.d.ts.map +1 -1
  135. package/dist/host-kit/run-route-error.js +23 -0
  136. package/dist/host-kit/run-route-error.js.map +1 -1
  137. package/dist/host-kit/run-router.d.ts +58 -1
  138. package/dist/host-kit/run-router.d.ts.map +1 -1
  139. package/dist/host-kit/run-router.js +120 -11
  140. package/dist/host-kit/run-router.js.map +1 -1
  141. package/dist/host-kit/stream-router.d.ts.map +1 -1
  142. package/dist/host-kit/stream-router.js +18 -9
  143. package/dist/host-kit/stream-router.js.map +1 -1
  144. package/dist/schedules/router.d.ts +15 -0
  145. package/dist/schedules/router.d.ts.map +1 -1
  146. package/dist/schedules/router.js +47 -17
  147. package/dist/schedules/router.js.map +1 -1
  148. package/dist/schedules/tick.d.ts +13 -0
  149. package/dist/schedules/tick.d.ts.map +1 -1
  150. package/dist/schedules/tick.js +59 -39
  151. package/dist/schedules/tick.js.map +1 -1
  152. package/dist/signal-providers/host-do.d.ts +12 -1
  153. package/dist/signal-providers/host-do.d.ts.map +1 -1
  154. package/dist/signal-providers/host-do.js +28 -3
  155. package/dist/signal-providers/host-do.js.map +1 -1
  156. package/dist/signal-providers/webhook-route.d.ts +14 -0
  157. package/dist/signal-providers/webhook-route.d.ts.map +1 -1
  158. package/dist/signal-providers/webhook-route.js +28 -5
  159. package/dist/signal-providers/webhook-route.js.map +1 -1
  160. package/dist/signals/notification-dispatch.d.ts +13 -0
  161. package/dist/signals/notification-dispatch.d.ts.map +1 -1
  162. package/dist/signals/notification-dispatch.js +20 -1
  163. package/dist/signals/notification-dispatch.js.map +1 -1
  164. package/dist/signals/notifications-d1.d.ts +8 -0
  165. package/dist/signals/notifications-d1.d.ts.map +1 -1
  166. package/dist/signals/notifications-d1.js +15 -9
  167. package/dist/signals/notifications-d1.js.map +1 -1
  168. package/dist/signals/thread-do-routes.d.ts.map +1 -1
  169. package/dist/signals/thread-do-routes.js +86 -18
  170. package/dist/signals/thread-do-routes.js.map +1 -1
  171. package/package.json +1 -1
  172. package/scripts/seed-deployment-identity.mjs +22 -4
@@ -0,0 +1,335 @@
1
+ import { EXECUTION_FENCE_STATES } from '#deployment-identity-protocol';
2
+ import { DoStatusError } from './do-status-error.js';
3
+ /**
4
+ * The state vocabulary, the table, that table's fixed row key, and the DDL
5
+ * built from all three are IMPORTED, never declared here — and they are not
6
+ * re-exported from this module either.
7
+ *
8
+ * Imported because PROVISIONING creates this table
9
+ * (deployment-identity-protocol.mjs), and `CREATE TABLE IF NOT EXISTS` would
10
+ * silently accept a differently-shaped table it had already made — so a second
11
+ * copy of the schema would not fail loudly, it would quietly drop the CHECK
12
+ * constraints that make every CAS below total rather than "the CAS, on
13
+ * whichever row you meant". That module is also the only file both sides can
14
+ * share: it ships at the package root for the provisioning CLI and
15
+ * fleet-control, neither of which can import this package's TypeScript.
16
+ *
17
+ * Not re-exported because a raw constant with two homes is a constant two
18
+ * consumers can disagree about. `@proofoftech/flowsafe/deployment-identity-protocol`
19
+ * is the one place to import the table name or the state list from; what this
20
+ * module publishes is the TYPED surface built on them — the state type, the
21
+ * store, the refusals, and the admission predicates.
22
+ */
23
+ export type ExecutionFenceState = (typeof EXECUTION_FENCE_STATES)[number];
24
+ /**
25
+ * The suspend-payload key the executor backstop stamps on a task it parked
26
+ * because the deployment was fenced mid-dispatch. Namespaced so it cannot
27
+ * collide with a tool's own suspend payload, and read back by
28
+ * `BackgroundTaskHost.#resumeFenceSuspendedTasks` — which is what makes the
29
+ * parking reversible rather than a quieter kind of loss.
30
+ *
31
+ * PUBLISHED via `./background-tasks` because it is the only way a host can tell
32
+ * a fence-parked row from a tool-suspended one: `listTasks({ status:
33
+ * 'suspended' })` returns both, and the marker lives in the suspend payload
34
+ * where no filter can express it. The drain inventory imports this same
35
+ * definition directly, so a rename cannot silently leave a hard-coded census
36
+ * predicate behind.
37
+ */
38
+ export declare const EXECUTION_FENCE_SUSPEND_KEY = "flowsafe.executionFenced";
39
+ /**
40
+ * How a surface is wired to the fence: a store, or the typed opt-out.
41
+ *
42
+ * Written as a union with no `undefined` so every option type carrying it can
43
+ * be REQUIRED. That is the whole forcing function: a fence option a host may
44
+ * omit is one a host will omit, and a partially wired deployment is worse than
45
+ * an unwired one — an unfenced schedule tick claims a due fire through the CAS
46
+ * (which advances `nextFireAt`) and the fenced runtime then refuses the start,
47
+ * so the fire is consumed and never runs. Making the caller WRITE `'none'`
48
+ * turns that split brain into a decision someone made rather than one they
49
+ * missed, and `'none'` stays honest for the callers that genuinely have no
50
+ * database to fence against (in-memory tests, adapters).
51
+ */
52
+ export type ExecutionFenceWiring = ExecutionFenceStore | 'none';
53
+ /** What one fence read observed. `proofKey`/`proofRunId` exist only in proof-only. */
54
+ export interface ExecutionFenceReading {
55
+ readonly state: ExecutionFenceState;
56
+ /** The key a proof-only start must carry to be admitted. */
57
+ readonly proofKey?: string;
58
+ /** The run the proof-only state has already admitted, once one started. */
59
+ readonly proofRunId?: string;
60
+ }
61
+ /** The reading every unfenced surface uses — see `ExecutionFenceStore` absence. */
62
+ export declare const OPEN_EXECUTION_FENCE: ExecutionFenceReading;
63
+ /**
64
+ * Read the fence a surface was wired with, resolving the typed opt-out.
65
+ *
66
+ * ONE function rather than a `fence ? await fence.read() : OPEN` at every gate,
67
+ * because those are the places a mistake is invisible: an unfenced surface and
68
+ * an open one behave identically until the day an operator closes the fence, so
69
+ * a call site that got the ternary subtly wrong would pass every test written
70
+ * against an open deployment. Every gate resolving absence through here means
71
+ * there is exactly one definition of what "no fence" does.
72
+ *
73
+ * `undefined` is admitted alongside `'none'` for the surfaces whose fence
74
+ * arrives through an object the caller may not have populated (an
75
+ * `InitResult.executionFence` on an unfenced host); it reads as open for the
76
+ * same reason `'none'` does, and never as a silent default a host can reach by
77
+ * forgetting — the option types that feed this are required.
78
+ */
79
+ export declare function readExecutionFence(fence: ExecutionFenceWiring | undefined): Promise<ExecutionFenceReading>;
80
+ export declare function executionFenceFor(db: ExecutionFenceDatabase): ExecutionFenceStore;
81
+ /**
82
+ * Minimal structural D1 surface, the same posture as SnapshotDatabase and
83
+ * ApprovalDatabase: tests back it with node:sqlite, Workers pass env.DB.
84
+ *
85
+ * `all()` rather than `first()` for the one read, deliberately: this exact
86
+ * shape is what DeploymentIdentityDatabase, SnapshotDatabase, and
87
+ * ApprovalDatabase all already satisfy, so every surface that must consult the
88
+ * fence can hand over the binding it already holds with no cast and no second
89
+ * seam. The fence's row is unique by primary key, so the two are equivalent.
90
+ */
91
+ export interface ExecutionFenceDatabase {
92
+ prepare(query: string): ExecutionFenceStatement;
93
+ }
94
+ export interface ExecutionFenceStatement {
95
+ bind(...values: unknown[]): ExecutionFenceStatement;
96
+ run(): Promise<unknown>;
97
+ all<T = unknown>(): Promise<{
98
+ results: T[];
99
+ }>;
100
+ }
101
+ /**
102
+ * The refusal a fenced surface answers with: 503, because the deployment is
103
+ * deliberately not executing right now and will be again — an operator's
104
+ * condition, not the caller's mistake and not a code fault. Providers,
105
+ * schedulers, and clients that honour Retry-After semantics therefore
106
+ * redeliver rather than discard.
107
+ */
108
+ export declare class ExecutionFencedError extends DoStatusError {
109
+ readonly status = 503;
110
+ readonly reason: {
111
+ readonly code: 'EXECUTION_FENCED';
112
+ readonly state: ExecutionFenceState;
113
+ };
114
+ constructor(state: ExecutionFenceState, entry?: string);
115
+ }
116
+ /**
117
+ * A fence transition whose compare-and-set found a different state. 409 rather
118
+ * than 503: the deployment is fine, the CALLER's expectation is stale — two
119
+ * control-plane actors raced, or an operator retried a transition that already
120
+ * landed. The current state rides on the reason so the caller can re-plan
121
+ * without a second round trip.
122
+ */
123
+ export declare class FenceTransitionConflictError extends DoStatusError {
124
+ readonly status = 409;
125
+ readonly reason: {
126
+ readonly code: 'FENCE_CAS_CONFLICT';
127
+ readonly state: ExecutionFenceState;
128
+ };
129
+ constructor(expected: ExecutionFenceState, current: ExecutionFenceState);
130
+ }
131
+ /**
132
+ * The fence could not be READ. Deliberately distinct from
133
+ * ExecutionFencedError: no state was observed, so nothing may conclude the
134
+ * deployment is open — which is why this carries the same 503 a refusal does
135
+ * and every request-path caller lets it propagate.
136
+ */
137
+ export declare class ExecutionFenceUnreadableError extends DoStatusError {
138
+ readonly status = 503;
139
+ readonly reason: {
140
+ readonly code: 'EXECUTION_FENCE_UNREADABLE';
141
+ };
142
+ constructor(message: string, options?: ErrorOptions);
143
+ }
144
+ /**
145
+ * Render a fence refusal as the JSON response the taxonomy specifies.
146
+ *
147
+ * For the surfaces that answer with a Response instead of throwing — a Worker
148
+ * router, or a Durable Object route whose own catch would re-map a thrown
149
+ * status. Built from the ERROR so the body a router writes and the body
150
+ * doErrorResponse writes for the same refusal cannot drift apart.
151
+ */
152
+ export declare function executionFencedResponse(state: ExecutionFenceState, entry?: string): Response;
153
+ /**
154
+ * A fence reading as the JSON body a control-plane read answers with.
155
+ *
156
+ * Beside `executionFencedResponse` and for the same reason: the published
157
+ * `GET /admin/execution-fence` route and the spike's local control probe both
158
+ * project a reading into this exact shape, and a projection written twice is
159
+ * one an operator's tooling can watch drift. Absent fields are OMITTED rather
160
+ * than sent as null — `proofKey`/`proofRunId` exist only in proof-only, and a
161
+ * null would invite a caller to read "no proof run yet" out of a state that has
162
+ * no proof at all.
163
+ */
164
+ export declare function executionFenceReadingPayload(reading: ExecutionFenceReading): {
165
+ state: ExecutionFenceState;
166
+ proofKey?: string;
167
+ proofRunId?: string;
168
+ };
169
+ /**
170
+ * A fence refusal that CROSSED a Durable Object boundary and was rebuilt on the
171
+ * far side.
172
+ *
173
+ * The run object throws ExecutionFencedError, `doErrorResponse` renders it, and
174
+ * `doSummary` reconstructs it as a `RunRouteError` carrying the same status,
175
+ * message and structured reason — but not the same class. This structural arm
176
+ * is what lets a Worker-side caller treat that reconstruction as the refusal it
177
+ * is, with the same three fields every render site already reads.
178
+ */
179
+ export interface WireExecutionFenceRefusal {
180
+ readonly message: string;
181
+ readonly status: number;
182
+ readonly reason: {
183
+ readonly code: string;
184
+ };
185
+ }
186
+ /** Every fence-authored refusal — the family a fenced surface catches as one. */
187
+ export type ExecutionFenceRefusal = ExecutionFencedError | ExecutionFenceUnreadableError | WireExecutionFenceRefusal;
188
+ /**
189
+ * Whether an error is the fence refusing (or failing to answer). The two are
190
+ * one class for every CALLER that must degrade closed — an alarm swallowing
191
+ * both, a tick skipping its pass — because a state it could not read and a
192
+ * state that forbids the work lead to the same action.
193
+ *
194
+ * The CODE counts as well as the class, because a refusal that crossed a
195
+ * Durable Object boundary is no longer an instance of anything: the run object
196
+ * threw ExecutionFencedError, `doErrorResponse` rendered it, and `doSummary`
197
+ * rebuilt it on the Worker side as a `RunRouteError` carrying the same status
198
+ * and the same structured reason. That rebuilt error IS the fence refusing —
199
+ * the callers most in need of recognizing one (a run router deciding whether to
200
+ * give a reservation's claim back, a tick deciding whether to skip a pass) are
201
+ * exactly the ones sitting on the far side of that boundary, and an
202
+ * instanceof-only test would answer "no" for every one of them.
203
+ *
204
+ * The two codes are matched by name rather than by any structural sniff: only
205
+ * refusals this package authors publish them, and both are declared as literals
206
+ * on the classes above, so a code arriving over the wire came from one of them.
207
+ */
208
+ export declare function isExecutionFenceRefusal(error: unknown): error is ExecutionFenceRefusal;
209
+ /**
210
+ * Validate a state name from a control-plane request. Exported because the
211
+ * admin route validates the WIRE shape and the package validates nothing else
212
+ * about a transition: which transitions are legal is host policy, and this
213
+ * package only enforces the state vocabulary, the CAS, and the proof-only
214
+ * key requirement.
215
+ */
216
+ export declare function assertExecutionFenceState(value: unknown, field: string): ExecutionFenceState;
217
+ /** A malformed control-plane fence request — the caller's to fix. */
218
+ export declare class InvalidExecutionFenceRequestError extends DoStatusError {
219
+ readonly status = 400;
220
+ readonly reason: {
221
+ readonly code: 'INVALID_EXECUTION_FENCE_REQUEST';
222
+ };
223
+ constructor(message: string);
224
+ }
225
+ /**
226
+ * May a NEW run be minted? Only `open`, plus the proof-only exception: a start
227
+ * whose idempotency key is the nominated proof key. The key is internal
228
+ * material (never a request-body field, never an open request-context key), so
229
+ * nothing a tenant can send reaches this branch.
230
+ */
231
+ export declare function admitsRunStart(reading: ExecutionFenceReading, idempotencyKey?: string): boolean;
232
+ /**
233
+ * May work proceed on a run that ALREADY exists — resume, approval decide,
234
+ * signal delivery? Through a drain, yes: a drain that refused these could
235
+ * never finish, because finishing is what the suspended runs are waiting for.
236
+ * In proof-only, only the nominated run.
237
+ */
238
+ export declare function admitsExistingRun(reading: ExecutionFenceReading, runId?: string): boolean;
239
+ /**
240
+ * May standing configuration that ARMS future work be authored — a schedule
241
+ * created/updated/resumed, an objective set, a due schedule fire claimed?
242
+ * `open` only. Pausing and deleting stay allowed in every state: they remove
243
+ * future work, which is the direction a drain is going.
244
+ */
245
+ export declare function admitsWorkAuthoring(reading: ExecutionFenceReading): boolean;
246
+ /**
247
+ * May queued, already-owned work be executed — a background task body, a
248
+ * notification dispatch pass? Through a drain, yes; that queue IS the work a
249
+ * drain exists to finish. Not in proof-only: the proof is one run, and a task
250
+ * queue re-driving itself alongside it is not a proof of anything.
251
+ */
252
+ export declare function admitsDrainableExecution(reading: ExecutionFenceReading): boolean;
253
+ export interface ExecutionFenceStoreOptions {
254
+ /** Injectable clock for `updated_at` (tests, deterministic fixtures). */
255
+ now?: () => number;
256
+ }
257
+ export interface ExecutionFenceTransition {
258
+ /** The state the caller believes the fence is in. */
259
+ expected: ExecutionFenceState;
260
+ /** The state to move to. */
261
+ next: ExecutionFenceState;
262
+ /**
263
+ * Required when `next` is 'proof-only'; rejected otherwise.
264
+ *
265
+ * `unknown` rather than `string` because every caller is a control-plane
266
+ * route holding a parsed JSON body, and `#proofKeyFor` already validates it
267
+ * against PATH_SAFE_ID_PATTERN and throws InvalidExecutionFenceRequestError
268
+ * on anything else. Typing it `string` bought nothing and cost something: it
269
+ * made every route write `body.proofKey as string`, an assertion that is
270
+ * false exactly when the caller sent the wrong thing, so the one input this
271
+ * field exists to police arrived pre-blessed at the type level.
272
+ */
273
+ proofKey?: unknown;
274
+ }
275
+ /**
276
+ * The deployment's fence, over one D1 database — the SAME database the runner's
277
+ * snapshots and the deployment sentinel live in, so the fence cannot be
278
+ * separated from the state it fences by any binding mistake.
279
+ */
280
+ export declare class ExecutionFenceStore {
281
+ #private;
282
+ constructor(db: ExecutionFenceDatabase, options?: ExecutionFenceStoreOptions);
283
+ /**
284
+ * The current fence state. NEVER memoized (see the module header) and never
285
+ * a write: this is on every gated request path, and a read path that emits
286
+ * `CREATE TABLE IF NOT EXISTS` is a write path wearing a read's name — it
287
+ * would make a fenced deployment mutate its own database to answer a
288
+ * question, and would turn a read-only replica or a revoked-write incident
289
+ * into an outage instead of a degrade.
290
+ *
291
+ * A missing table and a missing row both read as `open` — the 0.19 upgrade
292
+ * rule. Anything else that fails becomes ExecutionFenceUnreadableError, so
293
+ * no caller can mistake a storage fault for an open deployment.
294
+ */
295
+ read(): Promise<ExecutionFenceReading>;
296
+ /**
297
+ * Provisioning-time seeding: write the deployment's INITIAL fence state.
298
+ *
299
+ * INSERT-if-absent, never an overwrite. Seeding runs on every provisioning
300
+ * pass (including the already-owned early return), so a crash between the
301
+ * deployment sentinel and this row heals on the next attempt — but a
302
+ * re-provision of a LIVE deployment must never silently reopen a fence an
303
+ * operator closed, which an upsert would do.
304
+ *
305
+ * `state` has no default on purpose. The failure this closes is a migration
306
+ * host forgetting to ask for `migration-locked` and silently getting `open`;
307
+ * making the argument required turns that into a compile-time obligation,
308
+ * while still letting a host that wants an open deployment say so.
309
+ */
310
+ seed(state: ExecutionFenceState): Promise<void>;
311
+ /**
312
+ * Move the fence, compare-and-set on the CURRENT state. One conditional
313
+ * UPDATE, so two control-plane actors racing the same transition cannot both
314
+ * win: the loser changes zero rows and gets the state the winner left behind.
315
+ *
316
+ * Unlike `read()` this MAY create the table — a transition is a control-plane
317
+ * write, and a legacy database whose fence is implicitly open has no row to
318
+ * compare against. The row is materialized as `open` first, which is the
319
+ * state the implicit reading already reported, so the CAS that follows means
320
+ * exactly what it would have meant on a seeded database.
321
+ */
322
+ transition(input: ExecutionFenceTransition): Promise<ExecutionFenceReading>;
323
+ /**
324
+ * Bind the proof-only state to the run it admitted, conditionally.
325
+ *
326
+ * The condition is the whole point: between the read that ADMITTED a start
327
+ * and this write-back the fence may have moved, or a different run may have
328
+ * claimed the proof. Zero rows changed means the caller must refuse the
329
+ * start it was about to make — the fence is no longer the one it read.
330
+ * Re-writing the SAME runId is admitted so a retry of an interrupted start
331
+ * converges instead of deadlocking on its own earlier write.
332
+ */
333
+ recordProofRun(proofKey: string, runId: string): Promise<boolean>;
334
+ }
335
+ //# sourceMappingURL=execution-fence.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"execution-fence.d.ts","sourceRoot":"","sources":["../../src/do-runner/execution-fence.ts"],"names":[],"mappings":"AAyCA,OAAO,EAGL,sBAAsB,EAEvB,MAAM,+BAA+B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