@effect-agent/platform-cloudflare 0.1.0-beta.12 → 0.1.0-beta.121

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 (103) hide show
  1. package/dist/Alarm.d.mts +243 -0
  2. package/dist/Alarm.mjs +882 -0
  3. package/dist/Alarm.mjs.map +1 -0
  4. package/dist/BrowserRestCapture.d.mts +34 -0
  5. package/dist/BrowserRestCapture.mjs +238 -0
  6. package/dist/BrowserRestCapture.mjs.map +1 -0
  7. package/dist/BrowserRestCrawl.d.mts +16 -0
  8. package/dist/BrowserRestCrawl.mjs +244 -0
  9. package/dist/BrowserRestCrawl.mjs.map +1 -0
  10. package/dist/CloudflareAiGateway.d.mts +64 -0
  11. package/dist/CloudflareAiGateway.mjs +70 -0
  12. package/dist/CloudflareAiGateway.mjs.map +1 -0
  13. package/dist/CloudflareBindings.d.mts +115 -0
  14. package/dist/CloudflareBindings.mjs +113 -0
  15. package/dist/CloudflareBindings.mjs.map +1 -0
  16. package/dist/CloudflareBrowser-DV6kya1H.mjs +495 -0
  17. package/dist/CloudflareBrowser-DV6kya1H.mjs.map +1 -0
  18. package/dist/CloudflareBrowser-Doe1M7R5.d.mts +80 -0
  19. package/dist/CloudflareBrowser.d.mts +2 -0
  20. package/dist/CloudflareBrowser.mjs +2 -0
  21. package/dist/CloudflareCodeMode.d.mts +44 -0
  22. package/dist/CloudflareCodeMode.mjs +616 -0
  23. package/dist/CloudflareCodeMode.mjs.map +1 -0
  24. package/dist/CloudflareConfig-DKXGo8L3.d.mts +106 -0
  25. package/dist/CloudflareConfig.d.mts +2 -0
  26. package/dist/CloudflareConfig.mjs +128 -0
  27. package/dist/CloudflareConfig.mjs.map +1 -0
  28. package/dist/CloudflareMemory.d.mts +142 -0
  29. package/dist/CloudflareMemory.mjs +202 -0
  30. package/dist/CloudflareMemory.mjs.map +1 -0
  31. package/dist/CloudflareScheduling.d.mts +52 -0
  32. package/dist/CloudflareScheduling.mjs +396 -0
  33. package/dist/CloudflareScheduling.mjs.map +1 -0
  34. package/dist/CloudflareSubscriptions.d.mts +87 -0
  35. package/dist/CloudflareSubscriptions.mjs +530 -0
  36. package/dist/CloudflareSubscriptions.mjs.map +1 -0
  37. package/dist/CloudflareThreadClient.d.mts +1697 -0
  38. package/dist/CloudflareThreadClient.mjs +382 -0
  39. package/dist/CloudflareThreadClient.mjs.map +1 -0
  40. package/dist/InteractiveBrowser-Cm4xgiGy.d.mts +228 -0
  41. package/dist/InteractiveBrowser-DhOkdUx9.mjs +1780 -0
  42. package/dist/InteractiveBrowser-DhOkdUx9.mjs.map +1 -0
  43. package/dist/InteractiveBrowser.d.mts +2 -0
  44. package/dist/InteractiveBrowser.mjs +2 -0
  45. package/dist/ProtectedBrowser.d.mts +128 -0
  46. package/dist/ProtectedBrowser.mjs +1201 -0
  47. package/dist/ProtectedBrowser.mjs.map +1 -0
  48. package/dist/ThreadObject-DaC-IhCm.mjs +849 -0
  49. package/dist/ThreadObject-DaC-IhCm.mjs.map +1 -0
  50. package/dist/ThreadObject-mt0uxvZZ.d.mts +839 -0
  51. package/dist/ThreadObject.d.mts +2 -0
  52. package/dist/ThreadObject.mjs +3 -0
  53. package/dist/WakeScheduler.d.mts +23 -0
  54. package/dist/WakeScheduler.mjs +57 -0
  55. package/dist/WakeScheduler.mjs.map +1 -0
  56. package/dist/boundary-BguazVkh.mjs +42 -0
  57. package/dist/boundary-BguazVkh.mjs.map +1 -0
  58. package/dist/index.d.mts +13 -1548
  59. package/dist/index.mjs +13 -1636
  60. package/dist/prepared-admission-DU0_T-ev.mjs +73 -0
  61. package/dist/prepared-admission-DU0_T-ev.mjs.map +1 -0
  62. package/dist/rolldown-runtime-D7D4PA-g.mjs +13 -0
  63. package/package.json +1 -56
  64. package/src/Alarm.ts +1830 -0
  65. package/src/BrowserRestCapture.ts +477 -0
  66. package/src/BrowserRestCrawl.ts +540 -0
  67. package/src/CloudflareAiGateway.ts +170 -0
  68. package/src/CloudflareBindings.ts +199 -0
  69. package/src/CloudflareBrowser.ts +15 -0
  70. package/src/{code-mode-executor.ts → CloudflareCodeMode.ts} +372 -204
  71. package/src/{config.ts → CloudflareConfig.ts} +19 -12
  72. package/src/CloudflareMemory.ts +420 -0
  73. package/src/CloudflareScheduling.ts +747 -0
  74. package/src/CloudflareSubscriptions.ts +1041 -0
  75. package/src/CloudflareThreadClient.ts +809 -0
  76. package/src/InteractiveBrowser.ts +3246 -0
  77. package/src/ProtectedBrowser.ts +9 -0
  78. package/src/ThreadObject.ts +1190 -0
  79. package/src/{wake-scheduler.ts → WakeScheduler.ts} +32 -26
  80. package/src/index.ts +12 -23
  81. package/src/internal/boundary.ts +55 -0
  82. package/src/internal/browser-binding.ts +132 -0
  83. package/src/internal/browser-failure.ts +92 -0
  84. package/src/internal/browser-file-selection.ts +126 -0
  85. package/src/internal/browser-quick-action.ts +857 -0
  86. package/src/internal/browser-session-lifecycle.ts +193 -0
  87. package/src/internal/layers.ts +773 -0
  88. package/src/internal/message-delivery.ts +131 -0
  89. package/src/internal/prepared-admission.ts +116 -0
  90. package/src/internal/progress-wait.ts +121 -0
  91. package/src/internal/transport.ts +44 -0
  92. package/src/protected-browser/binding.ts +366 -0
  93. package/src/protected-browser/host.ts +382 -0
  94. package/src/protected-browser/inspect-frame.ts +140 -0
  95. package/src/protected-browser/native.ts +483 -0
  96. package/src/protected-browser/policy.ts +872 -0
  97. package/dist/index.mjs.map +0 -1
  98. package/src/alarm.ts +0 -334
  99. package/src/bindings.ts +0 -145
  100. package/src/client.ts +0 -646
  101. package/src/conversation-object.ts +0 -768
  102. package/src/layers.ts +0 -376
  103. package/src/transport.ts +0 -38
package/dist/Alarm.mjs ADDED
@@ -0,0 +1,882 @@
1
+ import { t as __exportAll } from "./rolldown-runtime-D7D4PA-g.mjs";
2
+ import { DurableObjectContext } from "./CloudflareBindings.mjs";
3
+ import { AuxiliaryDispatchMillis, CloudflareDurableRuntimeConfig } from "./CloudflareConfig.mjs";
4
+ import { r as safeCauseMessage } from "./boundary-BguazVkh.mjs";
5
+ import { Cause, Clock, Context, DateTime, Deferred, Effect, Exit, Fiber, Layer, Option, Random, Ref, Result, Schema, Scope, Semaphore, Stream, Struct } from "effect";
6
+ import "effect-agent/agent-registration";
7
+ import { DurableAgentRuntime, DurableRuntimeConfig, RecoveryBlocked, RecoveryFailure, RecoverySweepResult, isolateRecovery } from "effect-agent/durable-agent-runtime";
8
+ import { SubmissionId, ThreadId } from "effect-agent/identifiers";
9
+ import { OperationAuthorizationRequest, OperationAuthorizer } from "effect-agent/operation-authorizer";
10
+ import { AbortIntentRequest, SubmissionLedger } from "effect-agent/submission-ledger";
11
+ import { ThreadProjectionMaintenance, drainDue } from "effect-agent/thread-projection-maintenance";
12
+ import { WakeScheduler } from "effect-agent/wake-scheduler";
13
+ import { SqlClient } from "effect/unstable/sql/SqlClient";
14
+ //#region src/Alarm.ts
15
+ var Alarm_exports = /* @__PURE__ */ __exportAll({
16
+ DurableAlarmError: () => DurableAlarmError,
17
+ DurableAlarmService: () => DurableAlarmService,
18
+ MaintenancePassReport: () => MaintenancePassReport,
19
+ ThreadHostMaintenance: () => ThreadHostMaintenance,
20
+ ThreadMaintenance: () => ThreadMaintenance,
21
+ ThreadMaintenanceFailpoint: () => ThreadMaintenanceFailpoint,
22
+ ThreadMessageDelivery: () => ThreadMessageDelivery,
23
+ ThreadMutationGate: () => ThreadMutationGate,
24
+ ThreadPublication: () => ThreadPublication,
25
+ ThreadRecoveryFault: () => ThreadRecoveryFault,
26
+ publishCommitted: () => publishCommitted
27
+ });
28
+ /**
29
+ * The single multiplexed Durable Object alarm (decision D-P6-2). A Durable Object has ONE
30
+ * alarm slot; every cadence the Node host ran on fibers (wake scan, lease expiry, settlement
31
+ * and abort re-checks, retry backoff) multiplexes into one idempotent maintenance pass, and
32
+ * the slot always holds the EARLIEST deadline any caller asked for.
33
+ *
34
+ * The alarm invariant (plan §1.4): every committed actionable mutation carries a newer durable
35
+ * maintenance generation and a committed alarm. Stable externally-driven waits may be
36
+ * nonterminal without retaining an alarm; their resolving mutation advances the generation and
37
+ * restores the alarm atomically.
38
+ */
39
+ /** The Durable Object alarm API failed; surfaces on host entry points as a typed refusal. */
40
+ var DurableAlarmError = class extends Schema.TaggedError()("DurableAlarmError", {
41
+ operation: Schema.String,
42
+ message: Schema.String,
43
+ cause: Schema.optionalKey(Schema.Defect())
44
+ }) {};
45
+ const alarmFailure = (operation) => (cause) => DurableAlarmError.make({
46
+ operation,
47
+ message: safeCauseMessage(cause, "The Cloudflare alarm API failed without a diagnostic"),
48
+ cause
49
+ });
50
+ const makeStorageOperation = Effect.map(SqlClient, (sql) => (operation, execute) => Effect.flatMap(Effect.serviceOption(sql.transactionService), (current) => {
51
+ const body = Effect.uninterruptible(Effect.tryPromise({
52
+ try: execute,
53
+ catch: alarmFailure(operation)
54
+ }));
55
+ return current._tag === "Some" ? body : Effect.scoped(Effect.andThen(sql.reserve.pipe(Effect.mapError(alarmFailure(operation))), body));
56
+ }));
57
+ /** `ctx.storage` alarm slot as an Effect service; storage is truth, never a memory field. */
58
+ var DurableAlarmService = class DurableAlarmService extends Context.Service()("@effect-agent/platform-cloudflare/DurableAlarmService") {
59
+ static layer = Layer.effect(DurableAlarmService)(Effect.gen(function* () {
60
+ const { ctx } = yield* DurableObjectContext;
61
+ /**
62
+ * In-memory pass bookkeeping — a pure CACHE, never state: a fresh incarnation has no
63
+ * running pass, and a deferred wake lost to eviction was only ever a promptness hint
64
+ * on top of the already-committed pre-armed alarm.
65
+ */
66
+ const runningPasses = yield* Ref.make(0);
67
+ const storageOperation = yield* makeStorageOperation;
68
+ const scheduled = storageOperation("get alarm", () => ctx.storage.getAlarm()).pipe(Effect.map((deadline) => deadline === null ? Option.none() : Option.some(deadline)));
69
+ const scheduleAt = (epochMillis) => storageOperation("set alarm", () => ctx.storage.setAlarm(epochMillis));
70
+ const ensureScheduledBy = (epochMillis) => storageOperation("ensure alarm", () => ctx.storage.transaction(async (transaction) => {
71
+ const existing = await transaction.getAlarm();
72
+ if (existing === null || existing > epochMillis) await transaction.setAlarm(epochMillis);
73
+ }));
74
+ const armNow = Clock.currentTimeMillis.pipe(Effect.flatMap((now) => ensureScheduledBy(now)));
75
+ const scheduleNow = Ref.get(runningPasses).pipe(Effect.flatMap((passes) => passes > 0 ? Effect.void : armNow));
76
+ const withWakesDeferred = (body) => Ref.update(runningPasses, (passes) => passes + 1).pipe(Effect.andThen(body), Effect.ensuring(Ref.update(runningPasses, (passes) => passes - 1)));
77
+ const cancel = storageOperation("delete alarm", () => ctx.storage.deleteAlarm());
78
+ return DurableAlarmService.of({
79
+ scheduled,
80
+ scheduleAt,
81
+ ensureScheduledBy,
82
+ scheduleNow,
83
+ withWakesDeferred,
84
+ cancel
85
+ });
86
+ }));
87
+ };
88
+ /** What one maintenance pass did — auditable evidence mirroring `NodeDurableHost`'s report. */
89
+ var MaintenancePassReport = class extends Schema.Class("@effect-agent/platform-cloudflare/MaintenancePassReport")({
90
+ /** `caught-up` ran no runtime work (publication may be pending); `actionable` ran recovery. */
91
+ phase: Schema.Literals(["caught-up", "actionable"]),
92
+ /** Recovery decisions and Thread faults observed during this event. */
93
+ recovered: Schema.Int.check(Schema.isGreaterThanOrEqualTo(0)),
94
+ /** Head Attempts settled during the event. Joined input may settle with each head. */
95
+ settled: Schema.Int.check(Schema.isGreaterThanOrEqualTo(0)),
96
+ /** Submissions still nonterminal after the pass (suspended/unknown lanes stay honest). */
97
+ nonterminal: Schema.Int.check(Schema.isGreaterThanOrEqualTo(0)),
98
+ /** `rearmed` for dirty/autonomous work, `cleared` for stable waits or settlement. */
99
+ alarm: Schema.Literals(["rearmed", "cleared"])
100
+ }) {};
101
+ /** Test-only fault authority; production uses the inert layer. */
102
+ var ThreadMaintenanceFailpoint = class extends Context.Service()("@effect-agent/platform-cloudflare/ThreadMaintenanceFailpoint") {
103
+ static layer = Layer.succeed(this)({ hit: () => Effect.void });
104
+ };
105
+ /** Opt in with `ThreadObject.layer(registrations, { publication: Layer.effect(ThreadPublication)(...) })`. */
106
+ var ThreadPublication = class extends Context.Service()("@effect-agent/platform-cloudflare/ThreadPublication") {
107
+ static layer = Layer.succeed(this)({
108
+ invalidate: Effect.void,
109
+ prepareGeneration: () => Effect.void,
110
+ drain: Effect.void,
111
+ pendingDeadline: Effect.succeed(Option.none())
112
+ });
113
+ };
114
+ /**
115
+ * Host-assembled native message recovery. Preparation is a bounded local selection; the
116
+ * driver bounds each actual Claim and persists its timeout/retry before `run` returns.
117
+ * The alarm must not add a timer starting at selection: Claim setup and retry commits belong
118
+ * to the driver. The prepared allowance decides only whether a wave fits this event.
119
+ */
120
+ const ThreadMessageDelivery = Context.Reference("@effect-agent/platform-cloudflare/ThreadMessageDelivery", { defaultValue: () => ({
121
+ prepare: Effect.succeed({
122
+ timeoutMillis: 1,
123
+ run: Effect.void
124
+ }),
125
+ pendingDeadline: Effect.succeed(Option.none())
126
+ }) });
127
+ /** Independent lanes share the existing alarm; compose hosts by concatenating their lanes. */
128
+ const ThreadHostMaintenance = Context.Reference("@effect-agent/platform-cloudflare/ThreadHostMaintenance", { defaultValue: () => ({ lanes: [] }) });
129
+ const earliestDeadline = (left, right) => Option.isSome(left) ? Option.isSome(right) ? Option.some(Math.min(left.value, right.value)) : left : right;
130
+ /** @internal A committed source operation must not become a failed operation because delivery failed. */
131
+ const publishCommitted = Effect.gen(function* () {
132
+ const publication = yield* ThreadPublication;
133
+ yield* publication.invalidate.pipe(Effect.andThen(publication.drain));
134
+ }).pipe(Effect.catchCause((cause) => Cause.hasInterrupts(cause) ? Effect.interrupt : Effect.logError("Thread publication deferred after source commit", cause)));
135
+ const MaintenanceGeneration = Schema.BigIntFromString.check(Schema.isGreaterThanOrEqualToBigInt(0n));
136
+ var BindingRetry = class extends Schema.Class("BindingRetry")({
137
+ threadId: ThreadId,
138
+ submissionId: SubmissionId,
139
+ attempts: Schema.Natural,
140
+ notBefore: Schema.Finite,
141
+ reportedAt: Schema.Finite
142
+ }) {};
143
+ /**
144
+ * A durable observation of blocked recovery, independent of the execution journal. This is
145
+ * neither a Settlement nor proof that external effects did not happen. A successful recovery
146
+ * sweep clears it; repair must preserve canonical history and the original admission identity.
147
+ */
148
+ var ThreadRecoveryFault = class extends Schema.Class("@effect-agent/platform-cloudflare/ThreadRecoveryFault")({
149
+ schemaVersion: Schema.Literal(1),
150
+ threadId: ThreadId,
151
+ firstFailedAt: Schema.Finite,
152
+ lastFailedAt: Schema.Finite,
153
+ /** Earliest automatic recovery retry; new admissions do not erase this deadline. */
154
+ retryAt: Schema.Finite,
155
+ /** Saturates at 2^31 - 1; one observation per Thread per recovery sweep. */
156
+ attempts: Schema.Int.check(Schema.isGreaterThan(0), Schema.isLessThanOrEqualTo(2147483647)),
157
+ failure: RecoveryFailure
158
+ }) {};
159
+ const recoveryFaultKey = (threadId) => `effect-agent:thread-recovery-fault:v1:${threadId}`;
160
+ const decodeRecoveryFaultValue = Schema.decodeUnknownSync(ThreadRecoveryFault);
161
+ const decodeRecoveryFault = (threadId, encoded) => {
162
+ const fault = decodeRecoveryFaultValue(encoded);
163
+ if (fault.threadId !== threadId) throw new Error("Recovery status does not match its Thread key");
164
+ return fault;
165
+ };
166
+ const encodeRecoveryFault = Schema.encodeSync(ThreadRecoveryFault);
167
+ var MaintenanceRetry = class extends Schema.Class("MaintenanceRetry")({
168
+ generation: MaintenanceGeneration,
169
+ notBefore: Schema.Finite,
170
+ nativeOnly: Schema.Boolean,
171
+ stalls: Schema.Int.check(Schema.isGreaterThanOrEqualTo(0), Schema.isLessThanOrEqualTo(30))
172
+ }) {};
173
+ /** Versioned, platform-private maintenance state stored through Durable Object KV. */
174
+ var ThreadMaintenanceState = class extends Schema.Class("@effect-agent/platform-cloudflare/ThreadMaintenanceState")({
175
+ schemaVersion: Schema.Literal(1),
176
+ dirty: MaintenanceGeneration,
177
+ processed: MaintenanceGeneration,
178
+ nonterminal: Schema.Int.check(Schema.isGreaterThanOrEqualTo(0)),
179
+ /** One physical-owner cursor; old single-lane records need no conversion. */
180
+ lastServedThreadId: Schema.optionalKey(ThreadId),
181
+ /** Rotate old recovery independently of dispatch, including after eviction or timeout. */
182
+ lastRecoveredThreadId: Schema.optionalKey(ThreadId),
183
+ bindingRetries: Schema.optionalKey(Schema.Array(BindingRetry)),
184
+ /** Absent on older records. A newer mutation makes this retry obsolete. */
185
+ retry: Schema.optionalKey(MaintenanceRetry)
186
+ }) {};
187
+ const MAINTENANCE_STATE_KEY = "effect-agent:thread-maintenance:v1";
188
+ const decodeMaintenanceState = Schema.decodeUnknownSync(ThreadMaintenanceState);
189
+ const encodeMaintenanceState = Schema.encodeSync(ThreadMaintenanceState);
190
+ const initialMaintenanceState = () => ThreadMaintenanceState.make({
191
+ schemaVersion: 1,
192
+ dirty: 1n,
193
+ processed: 0n,
194
+ nonterminal: 0
195
+ });
196
+ const readMaintenanceState = async (transaction) => {
197
+ const encoded = await transaction.get(MAINTENANCE_STATE_KEY);
198
+ return encoded === void 0 ? {
199
+ state: initialMaintenanceState(),
200
+ initialized: false
201
+ } : {
202
+ state: decodeMaintenanceState(encoded),
203
+ initialized: true
204
+ };
205
+ };
206
+ const ensureTransactionAlarmBy = async (transaction, deadline) => {
207
+ const scheduled = await transaction.getAlarm();
208
+ if (scheduled === null || scheduled > deadline) await transaction.setAlarm(deadline);
209
+ };
210
+ const stableExternalWait = (snapshot, reports) => {
211
+ const report = reports.get(snapshot.submissionId);
212
+ if (report?.decision._tag === "SettleAborted") return false;
213
+ switch (snapshot.state) {
214
+ case "suspended":
215
+ case "joined": return true;
216
+ case "unknown": return report?.disposition === "unknown";
217
+ case "admitted": return reports.get(snapshot.submissionId)?.decision._tag === "AwaitParentEstablishment";
218
+ case "input-applied":
219
+ case "joining":
220
+ case "ready":
221
+ case "running":
222
+ case "settled":
223
+ case "terminalizing": return false;
224
+ }
225
+ };
226
+ /**
227
+ * Shared prearm/acknowledgement boundary for ingress and runtime-owned producers.
228
+ * `ThreadObject.layer` provides this same instance in its Services. Rebuilt runtime/maintenance
229
+ * Layers must reuse that instance; a second gate cannot observe the native producers' activity.
230
+ */
231
+ var ThreadMutationGate = class ThreadMutationGate extends Context.Service()("@effect-agent/platform-cloudflare/internal/ThreadMutationGate") {
232
+ static layer = Layer.effect(this)(Effect.gen(function* () {
233
+ const { ctx } = yield* DurableObjectContext;
234
+ const config = yield* CloudflareDurableRuntimeConfig;
235
+ const failpoint = yield* ThreadMaintenanceFailpoint;
236
+ const activeMutations = yield* Ref.make(0);
237
+ const generationGate = yield* Semaphore.make(1);
238
+ const minimumAlarmDelay = Math.max(1, Math.ceil(config.alarmBackoffBase / 2));
239
+ const runTransaction = yield* makeStorageOperation;
240
+ const beginMutation = Effect.fn("ThreadMaintenance.beginMutation")(function* (invalidatesRecovery) {
241
+ yield* failpoint.hit("maintenance:dirty:before");
242
+ const now = yield* Clock.currentTimeMillis;
243
+ yield* runTransaction("advance maintenance generation", () => ctx.storage.transaction(async (transaction) => {
244
+ const { state, initialized } = await readMaintenanceState(transaction);
245
+ const next = ThreadMaintenanceState.make({
246
+ ...state,
247
+ dirty: state.dirty + (invalidatesRecovery ? 1n : 0n)
248
+ });
249
+ if (invalidatesRecovery || !initialized) await transaction.put(MAINTENANCE_STATE_KEY, encodeMaintenanceState(next));
250
+ await ensureTransactionAlarmBy(transaction, now + minimumAlarmDelay);
251
+ }));
252
+ yield* failpoint.hit("maintenance:dirty:after");
253
+ yield* Ref.update(activeMutations, (active) => active + 1);
254
+ });
255
+ const endMutation = generationGate.withPermit(Ref.update(activeMutations, (active) => Math.max(0, active - 1)));
256
+ const withMutation = (body, options) => Effect.acquireUseRelease(generationGate.withPermit(beginMutation(options?.invalidatesRecovery ?? true)), () => failpoint.hit("maintenance:mutation:armed").pipe(Effect.andThen(body), Effect.tap(() => failpoint.hit("maintenance:mutation:finished"))), () => endMutation);
257
+ return ThreadMutationGate.of({
258
+ withMutation,
259
+ withSnapshot: (body) => generationGate.withPermit(Effect.flatMap(Ref.get(activeMutations), body))
260
+ });
261
+ }));
262
+ };
263
+ /**
264
+ * Incremental, quiescent maintenance over a durable dirty/processed generation (issue #93).
265
+ *
266
+ * One physical event owns native scheduling, auxiliary delivery and final alarm rearming.
267
+ *
268
+ * 1. Prearm before any work. A caught-up native step uses the O(1) generation record without
269
+ * recovery, ledger scans or canonical-history reads.
270
+ * 2. Reconcile before each head Attempt, then checkpoint only the observed generation. A racing
271
+ * producer keeps its generation dirty and immediately eligible. Quiescent native retries
272
+ * retain their durable backoff.
273
+ * 3. Keep native and delivery admission open together while finite waves remain active, so
274
+ * fresh replies and abort controls can progress during unrelated cleanup. Close atomically
275
+ * at quiescence, or at the original ten-minute yield deadline, then join admitted waves.
276
+ * 4. Native message delivery retains its driver-owned Claim deadline. Host/backfill waves are
277
+ * bounded independently; incoming native work never restarts or cancels their attempts.
278
+ * Auxiliary failures are reported after the current native opportunity.
279
+ * 5. Close every event resource before the final gated deadline snapshot and alarm decision.
280
+ * The whole event retains one fourteen-minute cooperative timeout.
281
+ */
282
+ var ThreadMaintenance = class ThreadMaintenance extends Context.Service()("@effect-agent/platform-cloudflare/ThreadMaintenance") {
283
+ static layer = Layer.effect(ThreadMaintenance)(Effect.gen(function* () {
284
+ const runtime = yield* DurableAgentRuntime;
285
+ const recoveryConfig = yield* DurableRuntimeConfig;
286
+ const ledger = yield* SubmissionLedger;
287
+ const wakes = yield* WakeScheduler;
288
+ const alarm = yield* DurableAlarmService;
289
+ const config = yield* CloudflareDurableRuntimeConfig;
290
+ const { ctx } = yield* DurableObjectContext;
291
+ const failpoint = yield* ThreadMaintenanceFailpoint;
292
+ const mutations = yield* ThreadMutationGate;
293
+ const publication = yield* ThreadPublication;
294
+ const projection = yield* ThreadProjectionMaintenance;
295
+ const messages = yield* ThreadMessageDelivery;
296
+ const host = yield* ThreadHostMaintenance;
297
+ const authorizer = yield* OperationAuthorizer;
298
+ const projectionDeadline = projection.pendingDeadline.pipe(Effect.catchCauseIf((cause) => !Cause.hasInterrupts(cause), (cause) => Effect.logError("Thread projection deadline unavailable", cause).pipe(Effect.as(Option.some(0)))));
299
+ const pendingDeadline = Effect.gen(function* () {
300
+ return earliestDeadline(earliestDeadline(yield* publication.pendingDeadline, yield* messages.pendingDeadline), earliestDeadline(yield* projectionDeadline, (yield* Effect.forEach(host.lanes, (lane) => lane.pendingDeadline)).reduce(earliestDeadline, Option.none())));
301
+ });
302
+ const maintenancePassGate = yield* Semaphore.make(1);
303
+ const minimumAlarmDelay = Math.max(1, Math.ceil(config.alarmBackoffBase / 2));
304
+ const runTransaction = yield* makeStorageOperation;
305
+ const recoveryStatus = Effect.fn("ThreadMaintenance.recoveryStatus")(function* (threadId) {
306
+ yield* authorizer.authorize(OperationAuthorizationRequest.make({
307
+ operation: "explain",
308
+ threadId
309
+ }));
310
+ return yield* runTransaction("read Thread recovery status", async () => {
311
+ const encoded = await ctx.storage.get(recoveryFaultKey(threadId));
312
+ return encoded === void 0 ? Option.none() : Option.some(decodeRecoveryFault(threadId, encoded));
313
+ });
314
+ });
315
+ const recordRecoveryStatus = Effect.fn("ThreadMaintenance.recordRecoveryStatus")(function* (result) {
316
+ const threads = /* @__PURE__ */ new Map();
317
+ for (const report of result.reports) threads.set(report.threadId, void 0);
318
+ for (const blocked of result.blocked) threads.set(blocked.threadId, blocked.failure);
319
+ if (threads.size === 0) return /* @__PURE__ */ new Map();
320
+ const now = yield* Clock.currentTimeMillis;
321
+ yield* failpoint.hit("maintenance:recovery-status:before");
322
+ const retained = yield* runTransaction("record Thread recovery status", () => ctx.storage.transaction(async (transaction) => {
323
+ const changed = [];
324
+ const newlyBlocked = [];
325
+ const faults = /* @__PURE__ */ new Map();
326
+ for (const [threadId, failure] of threads) {
327
+ const key = recoveryFaultKey(threadId);
328
+ const encoded = await transaction.get(key);
329
+ const previous = encoded === void 0 ? void 0 : decodeRecoveryFault(threadId, encoded);
330
+ if (failure === void 0) {
331
+ if (previous !== void 0) {
332
+ await transaction.delete(key);
333
+ changed.push(threadId);
334
+ }
335
+ continue;
336
+ }
337
+ const fault = ThreadRecoveryFault.make({
338
+ schemaVersion: 1,
339
+ threadId,
340
+ firstFailedAt: previous?.firstFailedAt ?? now,
341
+ lastFailedAt: now,
342
+ attempts: Math.min(2147483647, (previous?.attempts ?? 0) + 1),
343
+ retryAt: now + Math.min(6e4, 5e3 * 2 ** Math.min(30, previous?.attempts ?? 0)),
344
+ failure
345
+ });
346
+ await transaction.put(key, encodeRecoveryFault(fault));
347
+ changed.push(threadId);
348
+ faults.set(threadId, fault);
349
+ if (previous === void 0) newlyBlocked.push(fault);
350
+ }
351
+ return {
352
+ changed,
353
+ newlyBlocked,
354
+ faults
355
+ };
356
+ }));
357
+ yield* failpoint.hit("maintenance:recovery-status:after");
358
+ for (const threadId of retained.changed) yield* wakes.notify(threadId);
359
+ for (const fault of retained.newlyBlocked) yield* Effect.logError("Native Thread recovery blocked; accepted work remains pending", fault.failure.reason === "defect" ? Cause.die(fault.failure) : Cause.fail(fault.failure)).pipe(Effect.annotateLogs({ threadId: fault.threadId }));
360
+ return retained.faults;
361
+ });
362
+ const recoverThread = Effect.fn("ThreadMaintenance.recoverThread")(function* (threadId, recovery) {
363
+ const result = yield* runtime.runRecovery({ threadId });
364
+ const faults = yield* recordRecoveryStatus(result);
365
+ for (const report of result.reports) recovery.reports.set(report.submissionId, report);
366
+ recovery.faults.delete(threadId);
367
+ for (const [id, fault] of faults) recovery.faults.set(id, fault);
368
+ recovery.recovered += result.reports.length + result.blocked.length;
369
+ recovery.repaired ||= result.reports.some((report) => report.disposition === "repaired");
370
+ });
371
+ const ensureAlarm = Effect.fn("ThreadMaintenance.ensureAlarm")(function* () {
372
+ yield* failpoint.hit("maintenance:ensure:before");
373
+ const now = yield* Clock.currentTimeMillis;
374
+ const retry = yield* runTransaction("ensure maintenance alarm", () => ctx.storage.transaction(async (transaction) => {
375
+ const { state, initialized } = await readMaintenanceState(transaction);
376
+ if (!initialized) await transaction.put(MAINTENANCE_STATE_KEY, encodeMaintenanceState(state));
377
+ if (state.dirty > state.processed) await ensureTransactionAlarmBy(transaction, state.retry?.generation === state.dirty ? Math.max(now + minimumAlarmDelay, state.retry.notBefore) : now + config.wakeScanInterval);
378
+ return state.retry?.generation === state.dirty ? state.retry : void 0;
379
+ }));
380
+ const deadline = yield* pendingDeadline;
381
+ if (Option.isSome(deadline)) yield* runTransaction("ensure publication alarm", () => ctx.storage.transaction((transaction) => ensureTransactionAlarmBy(transaction, Math.max(now + minimumAlarmDelay, deadline.value <= now && retry !== void 0 && !retry.nativeOnly ? Math.max(deadline.value, retry.notBefore) : deadline.value))));
382
+ yield* failpoint.hit("maintenance:ensure:after");
383
+ });
384
+ const beginPass = Effect.fn("ThreadMaintenance.beginPass")(function* (observed) {
385
+ yield* failpoint.hit("maintenance:begin:before");
386
+ const now = yield* Clock.currentTimeMillis;
387
+ const result = yield* runTransaction("begin maintenance pass", () => ctx.storage.transaction(async (transaction) => {
388
+ const { state, initialized } = await readMaintenanceState(transaction);
389
+ observed.generation = state.dirty;
390
+ if (!initialized) await transaction.put(MAINTENANCE_STATE_KEY, encodeMaintenanceState(state));
391
+ const retryAt = state.retry?.generation === state.dirty ? state.retry.notBefore : 0;
392
+ await ensureTransactionAlarmBy(transaction, now + minimumAlarmDelay);
393
+ if (state.processed >= state.dirty || retryAt > now) return {
394
+ _tag: "CaughtUp",
395
+ nonterminal: state.nonterminal
396
+ };
397
+ return {
398
+ _tag: "Actionable",
399
+ generation: state.dirty,
400
+ nonterminal: state.nonterminal,
401
+ stalls: state.retry?.generation === state.dirty ? state.retry.stalls : 0
402
+ };
403
+ }));
404
+ yield* failpoint.hit("maintenance:begin:after");
405
+ return result;
406
+ });
407
+ const backoffDelay = (priorStalls, jitter) => {
408
+ const backoff = Math.min(config.alarmBackoffCap, config.alarmBackoffBase * 2 ** Math.min(priorStalls, 30));
409
+ return Math.ceil(backoff / 2 + backoff / 2 * jitter);
410
+ };
411
+ const rearmDelay = Effect.fn("ThreadMaintenance.rearmDelay")(function* (progressed, priorStalls) {
412
+ return progressed ? config.alarmBackoffBase : backoffDelay(priorStalls, yield* Random.next);
413
+ });
414
+ const rearmFailure = Effect.fn("ThreadMaintenance.rearmFailure")(function* (observed) {
415
+ const { generation, nativeOnly } = observed;
416
+ if (generation === void 0) return;
417
+ yield* failpoint.hit("maintenance:retry:before");
418
+ yield* mutations.withSnapshot((active) => Effect.gen(function* () {
419
+ const deadline = yield* pendingDeadline.pipe(Effect.catchCause(() => Effect.succeed(Option.none())));
420
+ const now = yield* Clock.currentTimeMillis;
421
+ const jitter = yield* Random.next;
422
+ yield* runTransaction("back off failed maintenance", () => ctx.storage.transaction(async (transaction) => {
423
+ const { state } = await readMaintenanceState(transaction);
424
+ const previous = state.retry?.generation === generation && state.retry.nativeOnly === nativeOnly ? state.retry : void 0;
425
+ const retry = MaintenanceRetry.make({
426
+ generation,
427
+ notBefore: Math.max(previous?.notBefore ?? 0, now + backoffDelay(previous?.stalls ?? 0, jitter)),
428
+ nativeOnly,
429
+ stalls: Math.min(30, (previous?.stalls ?? 0) + 1)
430
+ });
431
+ await transaction.put(MAINTENANCE_STATE_KEY, encodeMaintenanceState(ThreadMaintenanceState.make({
432
+ ...state,
433
+ retry
434
+ })));
435
+ const nativeDeadline = active > 0 || state.dirty !== generation ? now + minimumAlarmDelay : retry.notBefore;
436
+ await transaction.setAlarm(Math.max(now + minimumAlarmDelay, Option.isSome(deadline) && (nativeOnly || deadline.value > now) ? Math.min(nativeDeadline, deadline.value) : nativeDeadline));
437
+ }));
438
+ }));
439
+ yield* failpoint.hit("maintenance:retry:after");
440
+ });
441
+ const beginNative = Effect.fn("ThreadMaintenance.beginNative")(function* (observed) {
442
+ return yield* mutations.withSnapshot((activeAtStart) => Effect.gen(function* () {
443
+ const generation = yield* beginPass(observed);
444
+ if (generation._tag === "Actionable" && activeAtStart === 0) yield* publication.prepareGeneration(generation.generation);
445
+ return {
446
+ ...generation,
447
+ activeAtStart
448
+ };
449
+ }));
450
+ });
451
+ const advance = Effect.fn("ThreadMaintenance.advance")(function* (started, yieldAfter, observed, recovery, dispatch = true) {
452
+ const deadline = yield* publication.pendingDeadline;
453
+ if (started._tag === "Actionable" || Option.isSome(deadline) && deadline.value <= (yield* Clock.currentTimeMillis)) yield* publication.drain;
454
+ const pending = yield* publication.pendingDeadline;
455
+ if (started._tag === "CaughtUp" || Option.isSome(pending)) {
456
+ const nextAttemptAt = yield* mutations.withSnapshot((active) => Effect.gen(function* () {
457
+ const now = yield* Clock.currentTimeMillis;
458
+ const { state } = yield* runTransaction("read native maintenance deadline", () => ctx.storage.transaction((transaction) => readMaintenanceState(transaction)));
459
+ const native = active > 0 || state.dirty > state.processed ? state.retry?.generation === state.dirty && active === 0 ? state.retry.notBefore : now + minimumAlarmDelay : Infinity;
460
+ const next = Option.isSome(pending) ? pending.value : native;
461
+ return Number.isFinite(next) ? Math.max(now + minimumAlarmDelay, next) : void 0;
462
+ }));
463
+ return {
464
+ phase: "caught-up",
465
+ settled: 0,
466
+ nonterminal: started.nonterminal,
467
+ nextAttemptAt
468
+ };
469
+ }
470
+ observed.nativeOnly = true;
471
+ const observation = recovery.observation ??= {
472
+ generation: started.generation,
473
+ activeAtStart: started.activeAtStart
474
+ };
475
+ const current = yield* Stream.runCollect(ledger.scanNonterminal);
476
+ const selectionTime = yield* Clock.currentTimeMillis;
477
+ yield* runTransaction("read Thread recovery deadlines", async () => {
478
+ for (const threadId of new Set(current.map((row) => row.threadId))) {
479
+ if (recovery.loaded.has(threadId)) continue;
480
+ const encoded = await ctx.storage.get(recoveryFaultKey(threadId));
481
+ if (encoded !== void 0) recovery.faults.set(threadId, decodeRecoveryFault(threadId, encoded));
482
+ recovery.loaded.add(threadId);
483
+ }
484
+ });
485
+ const reports = recovery.reports;
486
+ const recoveryFaults = recovery.faults;
487
+ const waiting = (row) => !recovery.pending.has(row.threadId) && !recoveryFaults.has(row.threadId) && stableExternalWait(row, reports);
488
+ const heads = /* @__PURE__ */ new Map();
489
+ for (const row of current) {
490
+ if (row.state === "unknown" && waiting(row)) continue;
491
+ if (!heads.has(row.threadId)) heads.set(row.threadId, row);
492
+ }
493
+ const stopping = /* @__PURE__ */ new Set();
494
+ for (const head of heads.values()) {
495
+ if (head.state !== "ready" || recovery.pending.has(head.threadId) || recoveryFaults.has(head.threadId)) continue;
496
+ const intent = yield* isolateRecovery(ledger.readAbortIntent(AbortIntentRequest.make({ submissionId: head.submissionId })), {
497
+ timeout: recoveryConfig.recoveryTimeout,
498
+ phase: () => "recovery",
499
+ operation: "read abort intent"
500
+ });
501
+ if (Result.isSuccess(intent)) {
502
+ if (intent.success !== void 0) stopping.add(head.threadId);
503
+ } else {
504
+ const faults = yield* recordRecoveryStatus(RecoverySweepResult.make({
505
+ reports: [],
506
+ blocked: [RecoveryBlocked.make({
507
+ threadId: head.threadId,
508
+ failure: intent.failure
509
+ })]
510
+ }));
511
+ for (const [threadId, fault] of faults) recoveryFaults.set(threadId, fault);
512
+ recovery.recovered++;
513
+ }
514
+ }
515
+ const eligible = [...heads.values()].filter((head) => !recovery.pending.has(head.threadId) && !stopping.has(head.threadId) && !recoveryFaults.has(head.threadId) && !waiting(head) && (head.state === "ready" || reports.has(head.submissionId))).map((head) => head.threadId).sort();
516
+ yield* failpoint.hit("maintenance:select:before");
517
+ const selection = yield* runTransaction("select maintenance lane", () => ctx.storage.transaction(async (transaction) => {
518
+ const { state } = await readMaintenanceState(transaction);
519
+ const retries = (state.bindingRetries ?? []).filter((retry) => heads.get(retry.threadId)?.submissionId === retry.submissionId);
520
+ const runnable = eligible.filter((threadId) => !retries.some((retry) => retry.threadId === threadId && retry.notBefore > selectionTime));
521
+ const next = dispatch ? runnable.find((threadId) => state.lastServedThreadId === void 0 || threadId > state.lastServedThreadId) ?? runnable[0] : void 0;
522
+ if (next !== void 0) await transaction.put(MAINTENANCE_STATE_KEY, encodeMaintenanceState(ThreadMaintenanceState.make({
523
+ ...state,
524
+ lastServedThreadId: next
525
+ })));
526
+ const backlog = recovery.started ? [] : [...heads.keys()].filter((threadId) => {
527
+ const fault = recoveryFaults.get(threadId);
528
+ return !eligible.includes(threadId) && (fault === void 0 || fault.retryAt <= selectionTime);
529
+ });
530
+ const after = backlog.filter((threadId) => state.lastRecoveredThreadId === void 0 || threadId > state.lastRecoveredThreadId);
531
+ const before = backlog.filter((threadId) => state.lastRecoveredThreadId !== void 0 && threadId <= state.lastRecoveredThreadId);
532
+ return {
533
+ selected: next,
534
+ retries,
535
+ backlog: [...after, ...before]
536
+ };
537
+ }));
538
+ yield* failpoint.hit("maintenance:select:after");
539
+ if (!recovery.started) {
540
+ recovery.started = true;
541
+ recovery.needsCheckpoint = selection.backlog.length > 0;
542
+ for (const threadId of selection.backlog) recovery.pending.add(threadId);
543
+ yield* Deferred.succeed(recovery.queue, selection.backlog);
544
+ }
545
+ const selected = selection.selected === void 0 ? void 0 : heads.get(selection.selected);
546
+ if (selected !== void 0) yield* recoverThread(selected.threadId, recovery);
547
+ let retries = selection.retries;
548
+ let bindingFailure;
549
+ const settlement = selected === void 0 || recoveryFaults.has(selected.threadId) ? Option.none() : yield* runtime.processThreadHead(selected.threadId, { yieldAfter }).pipe(Effect.catchTag("BindingUnavailable", (failure) => {
550
+ bindingFailure = failure;
551
+ return Effect.succeed(Option.none());
552
+ }));
553
+ if (selected !== void 0) {
554
+ const previous = retries.find((retry) => retry.submissionId === selected.submissionId);
555
+ let retry;
556
+ let reportBindingFailure = false;
557
+ if (bindingFailure !== void 0) {
558
+ const now = yield* Clock.currentTimeMillis;
559
+ const attempts = Math.min(30, (previous?.attempts ?? 0) + 1);
560
+ reportBindingFailure = previous === void 0 || now - previous.reportedAt >= 9e5;
561
+ retry = BindingRetry.make({
562
+ threadId: selected.threadId,
563
+ submissionId: selected.submissionId,
564
+ attempts,
565
+ notBefore: now + Math.min(6e4, 5e3 * 2 ** (attempts - 1)),
566
+ reportedAt: reportBindingFailure ? now : previous?.reportedAt ?? now
567
+ });
568
+ }
569
+ if (retry !== void 0 || previous !== void 0) {
570
+ yield* failpoint.hit("maintenance:binding-retry:before");
571
+ retries = yield* runTransaction("record submission binding retry", () => ctx.storage.transaction(async (transaction) => {
572
+ const { state } = await readMaintenanceState(transaction);
573
+ const bindingRetries = [...(state.bindingRetries ?? []).filter((entry) => entry.submissionId !== selected.submissionId), ...retry === void 0 ? [] : [retry]];
574
+ await transaction.put(MAINTENANCE_STATE_KEY, encodeMaintenanceState(ThreadMaintenanceState.make({
575
+ ...state,
576
+ bindingRetries
577
+ })));
578
+ return bindingRetries;
579
+ }));
580
+ yield* failpoint.hit("maintenance:binding-retry:after");
581
+ }
582
+ if (bindingFailure !== void 0) yield* reportBindingFailure ? Effect.logError("Thread awaits a current agent binding; original work remains pending", Cause.fail(bindingFailure)) : Effect.logDebug("Thread binding retry remains pending", Cause.fail(bindingFailure));
583
+ }
584
+ const remaining = yield* Stream.runCollect(ledger.scanNonterminal);
585
+ const waitingHeads = /* @__PURE__ */ new Map();
586
+ const autonomous = remaining.some((snapshot) => {
587
+ if (snapshot.state === "unknown" && waiting(snapshot)) return false;
588
+ const headWaiting = waitingHeads.get(snapshot.threadId);
589
+ if (headWaiting === void 0) waitingHeads.set(snapshot.threadId, waiting(snapshot));
590
+ if (headWaiting === true && snapshot.state === "ready" && reports.get(snapshot.submissionId)?.decision._tag === "ApplyInput") return false;
591
+ return !waiting(snapshot);
592
+ });
593
+ const progressed = Option.isSome(settlement) || recovery.repaired;
594
+ const now = yield* Clock.currentTimeMillis;
595
+ const ordinaryDelay = autonomous ? yield* rearmDelay(progressed, started.stalls) : 0;
596
+ const nextEligible = [...recoveryFaults.values()].map((fault) => fault.retryAt).concat(eligible.map((threadId) => retries.find((retry) => retry.submissionId === heads.get(threadId)?.submissionId)?.notBefore ?? now));
597
+ const retryDelay = nextEligible.length === 0 ? 0 : Math.max(0, Math.min(...nextEligible) - now);
598
+ const delay = Math.max(ordinaryDelay, retryDelay);
599
+ yield* failpoint.hit("maintenance:checkpoint:before");
600
+ const nextAttemptAt = yield* mutations.withSnapshot((active) => runTransaction("checkpoint native maintenance", () => ctx.storage.transaction(async (transaction) => {
601
+ const { state } = await readMaintenanceState(transaction);
602
+ const mutationOverlap = observation.activeAtStart > 0 || started.activeAtStart > 0 || active > 0;
603
+ const processed = autonomous || mutationOverlap ? state.processed : state.processed > observation.generation ? state.processed : observation.generation;
604
+ const canBackoff = !mutationOverlap && state.dirty === started.generation;
605
+ const next = ThreadMaintenanceState.make({
606
+ ...Struct.omit(state, ["retry"]),
607
+ processed,
608
+ nonterminal: remaining.length,
609
+ bindingRetries: (state.bindingRetries ?? []).filter((retry) => remaining.some((row) => row.submissionId === retry.submissionId)),
610
+ ...autonomous && !progressed && canBackoff ? { retry: MaintenanceRetry.make({
611
+ generation: started.generation,
612
+ notBefore: now + delay,
613
+ nativeOnly: true,
614
+ stalls: Math.min(30, started.stalls + 1)
615
+ }) } : {}
616
+ });
617
+ await transaction.put(MAINTENANCE_STATE_KEY, encodeMaintenanceState(next));
618
+ if (autonomous) return now + (canBackoff ? delay : minimumAlarmDelay);
619
+ return mutationOverlap || next.dirty > next.processed ? now + minimumAlarmDelay : void 0;
620
+ })));
621
+ yield* failpoint.hit("maintenance:checkpoint:after");
622
+ return {
623
+ phase: "actionable",
624
+ settled: Option.isSome(settlement) ? 1 : 0,
625
+ nonterminal: remaining.length,
626
+ nextAttemptAt
627
+ };
628
+ });
629
+ const pass = Effect.fn("ThreadMaintenance.pass")(function* (yieldAfter, dispatchUntil, observed) {
630
+ const notified = (yield* Stream.toPull(wakes.wakes)).pipe(Effect.asVoid, Effect.catch(() => Effect.never));
631
+ const started = yield* beginNative(observed);
632
+ const auxiliaryScope = yield* Effect.acquireRelease(Scope.make("parallel"), (scope, exit) => Scope.close(scope, exit));
633
+ const fork = (work) => Effect.forkIn(work, auxiliaryScope);
634
+ const lanes = host.lanes.map((lane) => ({
635
+ ...lane,
636
+ initial: true,
637
+ check: true,
638
+ exhausted: false,
639
+ fiber: void 0
640
+ }));
641
+ let messageExhausted = false;
642
+ let delivery;
643
+ let failure;
644
+ const recovery = {
645
+ queue: yield* Deferred.make(),
646
+ pending: /* @__PURE__ */ new Set(),
647
+ loaded: /* @__PURE__ */ new Set(),
648
+ reports: /* @__PURE__ */ new Map(),
649
+ faults: /* @__PURE__ */ new Map(),
650
+ started: false,
651
+ needsCheckpoint: false,
652
+ recovered: 0,
653
+ repaired: false
654
+ };
655
+ const recoveryFiber = yield* fork(Effect.gen(function* () {
656
+ for (const threadId of yield* Deferred.await(recovery.queue)) {
657
+ yield* failpoint.hit("maintenance:select:before");
658
+ yield* runTransaction("select old recovery lane", () => ctx.storage.transaction(async (transaction) => {
659
+ const { state } = await readMaintenanceState(transaction);
660
+ await transaction.put(MAINTENANCE_STATE_KEY, encodeMaintenanceState(ThreadMaintenanceState.make({
661
+ ...state,
662
+ lastRecoveredThreadId: threadId
663
+ })));
664
+ }));
665
+ yield* failpoint.hit("maintenance:select:after");
666
+ yield* recoverThread(threadId, recovery);
667
+ recovery.pending.delete(threadId);
668
+ yield* wakes.notify(threadId);
669
+ }
670
+ }));
671
+ const backfill = yield* fork(drainDue.pipe(Effect.provideService(ThreadProjectionMaintenance, projection), Effect.timeoutOption(config.projectionDispatchTimeoutMillis)));
672
+ let backfillObserved = false;
673
+ let recoveryObserved = false;
674
+ let native = yield* fork(advance(started, yieldAfter, observed, recovery));
675
+ let result = {
676
+ phase: "caught-up",
677
+ settled: 0,
678
+ nonterminal: started.nonterminal,
679
+ nextAttemptAt: void 0
680
+ };
681
+ let phase = result.phase;
682
+ let settled = 0;
683
+ let nativeCheck = false;
684
+ const until = DateTime.toEpochMillis(yieldAfter);
685
+ const dispatchEnd = DateTime.toEpochMillis(dispatchUntil);
686
+ const checkLanes = () => {
687
+ for (const lane of lanes) lane.check = true;
688
+ };
689
+ while (true) {
690
+ if (native?.pollUnsafe() !== void 0) {
691
+ result = yield* Fiber.join(native);
692
+ native = void 0;
693
+ if (result.phase === "actionable") phase = "actionable";
694
+ settled += result.settled;
695
+ observed.nativeOnly = false;
696
+ recovery.started = true;
697
+ yield* Deferred.succeed(recovery.queue, []);
698
+ checkLanes();
699
+ }
700
+ if (!recoveryObserved && recoveryFiber.pollUnsafe() !== void 0) {
701
+ yield* Fiber.join(recoveryFiber);
702
+ recoveryObserved = true;
703
+ nativeCheck = recovery.needsCheckpoint;
704
+ }
705
+ if (!backfillObserved) {
706
+ const exit = backfill.pollUnsafe();
707
+ if (exit !== void 0) {
708
+ backfillObserved = true;
709
+ if (Exit.isFailure(exit)) failure ??= exit.cause;
710
+ }
711
+ }
712
+ for (const lane of lanes) {
713
+ const exit = lane.fiber?.pollUnsafe();
714
+ if (exit === void 0) continue;
715
+ lane.fiber = void 0;
716
+ if (Exit.isFailure(exit)) {
717
+ failure ??= exit.cause;
718
+ lane.exhausted = true;
719
+ } else lane.check ||= exit.value;
720
+ }
721
+ const deliveryExit = delivery?.pollUnsafe();
722
+ if (deliveryExit !== void 0) {
723
+ delivery = void 0;
724
+ if (Exit.isFailure(deliveryExit)) {
725
+ failure ??= deliveryExit.cause;
726
+ messageExhausted = true;
727
+ }
728
+ }
729
+ if (failure !== void 0 && native === void 0) return yield* Effect.failCause(failure);
730
+ const now = yield* Clock.currentTimeMillis;
731
+ if (now >= until) break;
732
+ for (const lane of lanes) {
733
+ if (lane.fiber !== void 0 || lane.exhausted || !lane.check) continue;
734
+ lane.check = false;
735
+ lane.fiber = yield* fork(Effect.gen(function* () {
736
+ yield* Schema.decodeEffect(AuxiliaryDispatchMillis)(lane.dispatchTimeoutMillis).pipe(Effect.mapError((cause) => DurableAlarmError.make({
737
+ operation: "host dispatch allowance",
738
+ message: "Declare an integer whole-wave allowance between 1 and 300000 milliseconds",
739
+ cause
740
+ })));
741
+ if ((yield* Clock.currentTimeMillis) + lane.dispatchTimeoutMillis > dispatchEnd) {
742
+ lane.exhausted = true;
743
+ return false;
744
+ }
745
+ return yield* Effect.gen(function* () {
746
+ if (!lane.initial) {
747
+ const deadline = yield* lane.pendingDeadline;
748
+ if (Option.isNone(deadline) || deadline.value > (yield* Clock.currentTimeMillis)) return false;
749
+ }
750
+ const selectedAt = yield* Clock.currentTimeMillis;
751
+ if (selectedAt >= until || selectedAt + lane.dispatchTimeoutMillis > dispatchEnd) {
752
+ lane.exhausted = true;
753
+ return false;
754
+ }
755
+ lane.initial = false;
756
+ yield* lane.run;
757
+ return true;
758
+ }).pipe(Effect.scoped, Effect.timeoutOrElse({
759
+ duration: lane.dispatchTimeoutMillis,
760
+ orElse: () => DurableAlarmError.make({
761
+ operation: "host dispatch allowance",
762
+ message: "The admitted host wave exceeded its allowance; durable work remains pending"
763
+ })
764
+ }));
765
+ }));
766
+ }
767
+ if (delivery === void 0 && !messageExhausted) {
768
+ const selected = yield* Effect.gen(function* () {
769
+ const deadline = yield* messages.pendingDeadline;
770
+ return Option.isSome(deadline) && deadline.value <= (yield* Clock.currentTimeMillis) ? Option.some(yield* messages.prepare) : Option.none();
771
+ }).pipe(Effect.exit);
772
+ if (Exit.isFailure(selected)) {
773
+ failure ??= selected.cause;
774
+ messageExhausted = true;
775
+ } else if (Option.isSome(selected.value)) {
776
+ const wave = selected.value.value;
777
+ const selectedAt = yield* Clock.currentTimeMillis;
778
+ if (selectedAt >= until || selectedAt + wave.timeoutMillis > dispatchEnd) messageExhausted = true;
779
+ else delivery = yield* fork(wave.run);
780
+ }
781
+ }
782
+ if (native === void 0 && recoveryObserved && !recovery.needsCheckpoint && backfillObserved && delivery === void 0 && lanes.every((lane) => lane.fiber === void 0)) break;
783
+ if (native === void 0 && nativeCheck) {
784
+ nativeCheck = false;
785
+ const checkpoint = recoveryObserved && recovery.needsCheckpoint;
786
+ if (checkpoint) recovery.needsCheckpoint = false;
787
+ native = yield* fork(Effect.gen(function* () {
788
+ const awakened = checkpoint ? started : yield* beginNative(observed);
789
+ return yield* advance(awakened, yieldAfter, observed, recovery, !checkpoint || settled === 0);
790
+ }));
791
+ }
792
+ const next = Math.min(native === void 0 ? result.nextAttemptAt ?? Infinity : Infinity, now + config.wakeScanInterval, until);
793
+ const completing = [
794
+ ...native === void 0 ? [] : [native],
795
+ ...recoveryObserved ? [] : [recoveryFiber],
796
+ ...backfillObserved ? [] : [backfill],
797
+ ...delivery === void 0 ? [] : [delivery],
798
+ ...lanes.flatMap((lane) => lane.fiber === void 0 ? [] : [lane.fiber])
799
+ ];
800
+ if ((yield* Effect.raceAllFirst([...completing.map((fiber) => Fiber.await(fiber).pipe(Effect.as("completed"))), Effect.raceFirst(notified, Effect.sleep(Math.max(0, next - now))).pipe(Effect.as("scan"))])) === "scan") {
801
+ nativeCheck = true;
802
+ checkLanes();
803
+ }
804
+ }
805
+ if (native !== void 0) {
806
+ result = yield* Fiber.join(native);
807
+ if (result.phase === "actionable") phase = "actionable";
808
+ settled += result.settled;
809
+ observed.nativeOnly = false;
810
+ recovery.started = true;
811
+ yield* Deferred.succeed(recovery.queue, []);
812
+ }
813
+ yield* Fiber.joinAll([
814
+ recoveryFiber,
815
+ backfill,
816
+ ...delivery === void 0 ? [] : [delivery],
817
+ ...lanes.flatMap((lane) => lane.fiber === void 0 ? [] : [lane.fiber])
818
+ ]);
819
+ if (failure !== void 0) return yield* Effect.failCause(failure);
820
+ if (recovery.needsCheckpoint) {
821
+ result = yield* advance(started, yieldAfter, observed, recovery, false);
822
+ if (result.phase === "actionable") phase = "actionable";
823
+ settled += result.settled;
824
+ }
825
+ yield* Scope.close(auxiliaryScope, Exit.void);
826
+ yield* failpoint.hit("maintenance:finish:before");
827
+ const disposition = yield* mutations.withSnapshot((active) => Effect.gen(function* () {
828
+ const latest = yield* pendingDeadline;
829
+ const now = yield* Clock.currentTimeMillis;
830
+ return yield* runTransaction("finish maintenance event", () => ctx.storage.transaction(async (transaction) => {
831
+ const { state } = await readMaintenanceState(transaction);
832
+ const native = active > 0 || state.dirty > state.processed ? state.retry?.generation === state.dirty && active === 0 ? Math.max(now + minimumAlarmDelay, state.retry.notBefore) : state.dirty === observed.generation && active === 0 ? result.nextAttemptAt ?? now + config.wakeScanInterval : now + minimumAlarmDelay : Infinity;
833
+ const next = Option.isSome(latest) ? Math.min(native, latest.value) : native;
834
+ if (Number.isFinite(next)) {
835
+ await transaction.setAlarm(Math.max(now + minimumAlarmDelay, next));
836
+ return "rearmed";
837
+ }
838
+ await transaction.deleteAlarm();
839
+ return "cleared";
840
+ }));
841
+ }));
842
+ yield* failpoint.hit("maintenance:finish:after");
843
+ const report = MaintenancePassReport.make({
844
+ phase,
845
+ recovered: recovery.recovered,
846
+ settled,
847
+ nonterminal: result.nonterminal,
848
+ alarm: disposition
849
+ });
850
+ yield* Effect.annotateCurrentSpan({
851
+ phase: report.phase,
852
+ recovered: report.recovered,
853
+ settled: report.settled,
854
+ nonterminal: report.nonterminal,
855
+ alarm: report.alarm
856
+ });
857
+ return report;
858
+ });
859
+ return ThreadMaintenance.of({
860
+ pass: Effect.gen(function* () {
861
+ const now = yield* Clock.currentTimeMillis;
862
+ const yieldAfter = DateTime.makeUnsafe(now + 6e5);
863
+ const dispatchUntil = DateTime.makeUnsafe(now + 84e4);
864
+ const observed = { nativeOnly: false };
865
+ return yield* alarm.withWakesDeferred(maintenancePassGate.withPermit(Effect.scoped(pass(yieldAfter, dispatchUntil, observed)).pipe(Effect.onErrorIf(() => true, () => rearmFailure(observed)))));
866
+ }).pipe(Effect.timeoutOrElse({
867
+ duration: "14 minutes",
868
+ orElse: () => DurableAlarmError.make({
869
+ operation: "maintenance pass deadline",
870
+ message: "The maintenance event exceeded its 14 minute deadline; durable recovery remains pending"
871
+ })
872
+ })),
873
+ ensureAlarm: mutations.withSnapshot(() => ensureAlarm()),
874
+ recoveryStatus,
875
+ withMutation: (body) => mutations.withMutation(body.pipe(Effect.tap(() => publishCommitted.pipe(Effect.provideService(ThreadPublication, publication)))))
876
+ });
877
+ }));
878
+ };
879
+ //#endregion
880
+ export { DurableAlarmError, DurableAlarmService, MaintenancePassReport, ThreadHostMaintenance, ThreadMaintenance, ThreadMaintenanceFailpoint, ThreadMessageDelivery, ThreadMutationGate, ThreadPublication, ThreadRecoveryFault, publishCommitted, Alarm_exports as t };
881
+
882
+ //# sourceMappingURL=Alarm.mjs.map