@effect-agent/platform-cloudflare 0.1.0-beta.97 → 0.1.0-beta.99
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/Alarm.mjs +10 -6
- package/dist/Alarm.mjs.map +1 -1
- package/dist/CloudflareThreadClient.d.mts +24 -14
- package/dist/{ThreadObject-COli4r4g.d.mts → ThreadObject-DW6cs99q.d.mts} +28 -15
- package/dist/ThreadObject.d.mts +1 -1
- package/dist/index.d.mts +1 -1
- package/package.json +1 -1
- package/src/Alarm.ts +10 -5
package/dist/Alarm.mjs
CHANGED
|
@@ -196,12 +196,12 @@ const ensureTransactionAlarmBy = async (transaction, deadline) => {
|
|
|
196
196
|
if (scheduled === null || scheduled > deadline) await transaction.setAlarm(deadline);
|
|
197
197
|
};
|
|
198
198
|
const stableExternalWait = (snapshot, reports) => {
|
|
199
|
-
const
|
|
200
|
-
if (decision === "SettleAborted") return false;
|
|
199
|
+
const report = reports.get(snapshot.submissionId);
|
|
200
|
+
if (report?.decision._tag === "SettleAborted") return false;
|
|
201
201
|
switch (snapshot.state) {
|
|
202
202
|
case "suspended":
|
|
203
203
|
case "joined": return true;
|
|
204
|
-
case "unknown": return
|
|
204
|
+
case "unknown": return report?.disposition === "unknown";
|
|
205
205
|
case "admitted": return reports.get(snapshot.submissionId)?.decision._tag === "AwaitParentEstablishment";
|
|
206
206
|
case "input-applied":
|
|
207
207
|
case "joining":
|
|
@@ -431,7 +431,10 @@ var ThreadMaintenance = class ThreadMaintenance extends Context.Service()("@effe
|
|
|
431
431
|
const reports = new Map(recovered.map((report) => [report.submissionId, report]));
|
|
432
432
|
const current = yield* Stream.runCollect(ledger.scanNonterminal);
|
|
433
433
|
const heads = /* @__PURE__ */ new Map();
|
|
434
|
-
for (const row of current)
|
|
434
|
+
for (const row of current) {
|
|
435
|
+
if (row.state === "unknown" && stableExternalWait(row, reports)) continue;
|
|
436
|
+
if (!heads.has(row.threadId)) heads.set(row.threadId, row);
|
|
437
|
+
}
|
|
435
438
|
const eligible = [...heads.values()].filter((head) => !stableExternalWait(head, reports)).map((head) => head.threadId).sort();
|
|
436
439
|
const selectionTime = yield* Clock.currentTimeMillis;
|
|
437
440
|
yield* failpoint.hit("maintenance:select:before");
|
|
@@ -453,7 +456,7 @@ var ThreadMaintenance = class ThreadMaintenance extends Context.Service()("@effe
|
|
|
453
456
|
const selected = selection.selected === void 0 ? void 0 : heads.get(selection.selected);
|
|
454
457
|
let retries = selection.retries;
|
|
455
458
|
let bindingFailure;
|
|
456
|
-
const settlement = selected === void 0 ? Option.none() : yield* runtime.processThreadHead(selected.threadId, { yieldAfter }).pipe(Effect.catchTag(
|
|
459
|
+
const settlement = selected === void 0 ? Option.none() : yield* runtime.processThreadHead(selected.threadId, { yieldAfter }).pipe(Effect.catchTag("BindingUnavailable", (failure) => {
|
|
457
460
|
bindingFailure = failure;
|
|
458
461
|
return Effect.succeed(Option.none());
|
|
459
462
|
}));
|
|
@@ -486,13 +489,14 @@ var ThreadMaintenance = class ThreadMaintenance extends Context.Service()("@effe
|
|
|
486
489
|
}));
|
|
487
490
|
yield* failpoint.hit("maintenance:binding-retry:after");
|
|
488
491
|
}
|
|
489
|
-
if (bindingFailure !== void 0) yield* reportBindingFailure ? Effect.logError("Thread awaits a
|
|
492
|
+
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));
|
|
490
493
|
}
|
|
491
494
|
observed.nativeOnly = false;
|
|
492
495
|
yield* finishAuxiliary;
|
|
493
496
|
const remaining = yield* Stream.runCollect(ledger.scanNonterminal);
|
|
494
497
|
const waitingHeads = /* @__PURE__ */ new Map();
|
|
495
498
|
const autonomous = remaining.some((snapshot) => {
|
|
499
|
+
if (snapshot.state === "unknown" && stableExternalWait(snapshot, reports)) return false;
|
|
496
500
|
const headWaiting = waitingHeads.get(snapshot.threadId);
|
|
497
501
|
if (headWaiting === void 0) waitingHeads.set(snapshot.threadId, stableExternalWait(snapshot, reports));
|
|
498
502
|
if (headWaiting === true && snapshot.state === "ready" && reports.get(snapshot.submissionId)?.decision._tag === "ApplyInput") return false;
|
package/dist/Alarm.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Alarm.mjs","names":[],"sources":["../src/Alarm.ts"],"sourcesContent":["import {\n Cause,\n Clock,\n Context,\n DateTime,\n Deferred,\n Effect,\n Exit,\n Fiber,\n Layer,\n Option,\n Random,\n Ref,\n Schema,\n Scope,\n Semaphore,\n Stream,\n Struct,\n} from \"effect\";\nimport { type DurableBindingFailure } from \"effect-agent/agent-registration\";\nimport {\n DurableAgentRuntime,\n type DurableWorkerFailure,\n type RecoveryReport,\n} from \"effect-agent/durable-agent-runtime\";\nimport { ThreadId, SubmissionId } from \"effect-agent/identifiers\";\nimport { SubmissionLedger, type SubmissionSnapshot } from \"effect-agent/submission-ledger\";\nimport {\n ThreadProjectionMaintenance,\n drainDue,\n type ThreadProjectionError,\n} from \"effect-agent/thread-projection-maintenance\";\nimport { SqlClient } from \"effect/unstable/sql/SqlClient\";\n\nimport { DurableObjectContext } from \"./CloudflareBindings.ts\";\nimport { AuxiliaryDispatchMillis, CloudflareDurableRuntimeConfig } from \"./CloudflareConfig.ts\";\nimport { safeCauseMessage } from \"./internal/boundary.ts\";\n\n/**\n * The single multiplexed Durable Object alarm (decision D-P6-2). A Durable Object has ONE\n * alarm slot; every cadence the Node host ran on fibers (wake scan, lease expiry, settlement\n * and abort re-checks, retry backoff) multiplexes into one idempotent maintenance pass, and\n * the slot always holds the EARLIEST deadline any caller asked for.\n *\n * The alarm invariant (plan §1.4): every committed actionable mutation carries a newer durable\n * maintenance generation and a committed alarm. Stable externally-driven waits may be\n * nonterminal without retaining an alarm; their resolving mutation advances the generation and\n * restores the alarm atomically.\n */\n\n/** The Durable Object alarm API failed; surfaces on host entry points as a typed refusal. */\nexport class DurableAlarmError extends Schema.TaggedError<DurableAlarmError>()(\n \"DurableAlarmError\",\n {\n operation: Schema.String,\n message: Schema.String,\n cause: Schema.optionalKey(Schema.Defect()),\n },\n) {}\n\nconst alarmFailure =\n (operation: string) =>\n (cause: unknown): DurableAlarmError =>\n DurableAlarmError.make({\n operation,\n message: safeCauseMessage(cause, \"The Cloudflare alarm API failed without a diagnostic\"),\n cause,\n });\n\n// SQL and raw KV/alarm operations share one physical SQLite transaction. Reserve its\n// connection for each short storage operation, never around a mutation or snapshot body.\nconst makeStorageOperation = Effect.map(\n SqlClient,\n (sql) =>\n <A>(operation: string, execute: () => Promise<A>) =>\n Effect.flatMap(Effect.serviceOption(sql.transactionService), (current) => {\n const body = Effect.uninterruptible(\n Effect.tryPromise({ try: execute, catch: alarmFailure(operation) }),\n );\n\n return current._tag === \"Some\"\n ? body\n : Effect.scoped(\n Effect.andThen(sql.reserve.pipe(Effect.mapError(alarmFailure(operation))), body),\n );\n }),\n);\n\n/** `ctx.storage` alarm slot as an Effect service; storage is truth, never a memory field. */\nexport class DurableAlarmService extends Context.Service<\n DurableAlarmService,\n {\n /** The scheduled deadline in epoch milliseconds, if any. */\n readonly scheduled: Effect.Effect<Option.Option<number>, DurableAlarmError>;\n /** Replace the slot with this deadline. */\n readonly scheduleAt: (epochMillis: number) => Effect.Effect<void, DurableAlarmError>;\n /** Keep the EARLIER of the existing deadline and this one (the multiplexing rule). */\n readonly ensureScheduledBy: (epochMillis: number) => Effect.Effect<void, DurableAlarmError>;\n /**\n * Arm an immediate alarm (the durable, coalescing local wake) — DEFERRED while a\n * maintenance pass is executing. Workerd cancels an in-flight alarm handler when a new\n * EARLIER deadline is written during its execution (`requestScheduledAlarm`), and the\n * maintenance pass runs INSIDE the alarm handler: an immediate wake landing mid-pass\n * (a routed port mutation, a sibling's `wake()`, the coordinator's own local notify)\n * would kill the running Attempt — manufacturing an ownership loss no real eviction\n * caused, and routing open uncertain-class Tool Calls into spurious Unknown Outcomes.\n * Deferral is contract-safe: wakes are droppable hints, every mutating entry point\n * pre-arms BEFORE its first durable mutation (the alarm invariant never rests on this\n * call). The pass's durable generation check observes any racing mutation, so the\n * in-memory hint does not need to be flushed after a stable wait is acknowledged.\n */\n readonly scheduleNow: Effect.Effect<void, DurableAlarmError>;\n /**\n * Run one maintenance pass with wake deferral (see `scheduleNow`). Calls made while `body`\n * executes are droppable promptness hints; correctness rests on the durable generation.\n */\n readonly withWakesDeferred: <A, E, R>(body: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>;\n /** Clear the slot; correctness-sensitive clears live in maintenance generation transactions. */\n readonly cancel: Effect.Effect<void, DurableAlarmError>;\n }\n>()(\"@effect-agent/platform-cloudflare/DurableAlarmService\") {\n static readonly layer: Layer.Layer<DurableAlarmService, never, DurableObjectContext | SqlClient> =\n Layer.effect(DurableAlarmService)(\n Effect.gen(function* () {\n const { ctx } = yield* DurableObjectContext;\n /**\n * In-memory pass bookkeeping — a pure CACHE, never state: a fresh incarnation has no\n * running pass, and a deferred wake lost to eviction was only ever a promptness hint\n * on top of the already-committed pre-armed alarm.\n */\n const runningPasses = yield* Ref.make(0);\n\n const storageOperation = yield* makeStorageOperation;\n\n const scheduled = storageOperation(\"get alarm\", () => ctx.storage.getAlarm()).pipe(\n Effect.map((deadline) =>\n deadline === null ? Option.none<number>() : Option.some(deadline),\n ),\n );\n\n const scheduleAt = (epochMillis: number) =>\n storageOperation(\"set alarm\", () => ctx.storage.setAlarm(epochMillis));\n\n const ensureScheduledBy = (epochMillis: number) =>\n storageOperation(\"ensure alarm\", () =>\n ctx.storage.transaction(async (transaction) => {\n const existing = await transaction.getAlarm();\n\n if (existing === null || existing > epochMillis) {\n await transaction.setAlarm(epochMillis);\n }\n }),\n );\n\n const armNow = Clock.currentTimeMillis.pipe(\n Effect.flatMap((now) => ensureScheduledBy(now)),\n );\n\n const scheduleNow = Ref.get(runningPasses).pipe(\n Effect.flatMap((passes) => (passes > 0 ? Effect.void : armNow)),\n );\n\n const withWakesDeferred = <A, E, R>(body: Effect.Effect<A, E, R>): Effect.Effect<A, E, R> =>\n Ref.update(runningPasses, (passes) => passes + 1).pipe(\n Effect.andThen(body),\n Effect.ensuring(Ref.update(runningPasses, (passes) => passes - 1)),\n );\n\n const cancel = storageOperation(\"delete alarm\", () => ctx.storage.deleteAlarm());\n\n return DurableAlarmService.of({\n scheduled,\n scheduleAt,\n ensureScheduledBy,\n scheduleNow,\n withWakesDeferred,\n cancel,\n });\n }),\n );\n}\n\n/** What one maintenance pass did — auditable evidence mirroring `NodeDurableHost`'s report. */\nexport class MaintenancePassReport extends Schema.Class<MaintenancePassReport>(\n \"@effect-agent/platform-cloudflare/MaintenancePassReport\",\n)({\n /** `caught-up` ran no runtime work (publication may be pending); `actionable` ran recovery. */\n phase: Schema.Literals([\"caught-up\", \"actionable\"]),\n /** Recovery decisions executed (or deferred) BEFORE any new claim in this pass. */\n recovered: Schema.Int.check(Schema.isGreaterThanOrEqualTo(0)),\n /** Whether the head Attempt settled. Joined input may settle with that head. */\n settled: Schema.Int.check(Schema.isGreaterThanOrEqualTo(0)),\n /** Submissions still nonterminal after the pass (suspended/unknown lanes stay honest). */\n nonterminal: Schema.Int.check(Schema.isGreaterThanOrEqualTo(0)),\n /** `rearmed` for dirty/autonomous work, `cleared` for stable waits or settlement. */\n alarm: Schema.Literals([\"rearmed\", \"cleared\"]),\n}) {}\n\n/** Fault boundaries around every maintenance-owned durable mutation. */\nexport type ThreadMaintenanceFailpointLocation =\n | \"maintenance:dirty:before\"\n | \"maintenance:dirty:after\"\n | \"maintenance:mutation:armed\"\n | \"maintenance:mutation:finished\"\n | \"maintenance:ensure:before\"\n | \"maintenance:ensure:after\"\n | \"maintenance:begin:before\"\n | \"maintenance:begin:after\"\n | \"maintenance:select:before\"\n | \"maintenance:select:after\"\n | \"maintenance:binding-retry:before\"\n | \"maintenance:binding-retry:after\"\n | \"maintenance:retry:before\"\n | \"maintenance:retry:after\"\n | \"maintenance:finish:before\"\n | \"maintenance:finish:after\";\n\nexport type ThreadMaintenanceFailpointHandler = (\n location: ThreadMaintenanceFailpointLocation,\n) => Effect.Effect<void>;\n\n/** Test-only fault authority; production uses the inert layer. */\nexport class ThreadMaintenanceFailpoint extends Context.Service<\n ThreadMaintenanceFailpoint,\n {\n readonly hit: ThreadMaintenanceFailpointHandler;\n }\n>()(\"@effect-agent/platform-cloudflare/ThreadMaintenanceFailpoint\") {\n static readonly layer = Layer.succeed(this)({ hit: () => Effect.void });\n}\n\n/**\n * Durable host publication of canonical records and ledger approval/abort/resolution intents.\n * The host owns schema-versioned cursors, destination idempotency and acknowledgement. Delivery\n * is at least once. Hooks must not write the alarm slot or mutate the supplied raw source ports.\n *\n * `invalidate`, `prepareGeneration` and `pendingDeadline` must be bounded local operations.\n * `prepareGeneration` durably invalidates a scan only when its generation changes; repeated\n * calls must preserve partial scan progress. It runs with no source mutation in flight.\n * Use this gate only for publication required before dependent native execution. Independent\n * UI relays and outboxes belong to ThreadHostMaintenance.\n * `drain` performs bounded delivery and persists retries before returning. A pending deadline\n * defers runtime recovery/Attempts, allowing committed host publications to drain first.\n * Unexpected hook failures leave the prearmed generation for retry. Hooks acquire per-call\n * resources with Effect.scoped; Layer construction owns incarnation resources (eviction need\n * not run finalizers). Do not hold a local hook behind network I/O or call back into producers.\n */\nexport interface ThreadPublicationService {\n readonly invalidate: Effect.Effect<void, DurableAlarmError>;\n readonly prepareGeneration: (generation: bigint) => Effect.Effect<void, DurableAlarmError>;\n readonly drain: Effect.Effect<void, DurableAlarmError>;\n readonly pendingDeadline: Effect.Effect<Option.Option<number>, DurableAlarmError>;\n}\n\n/** Opt in with `ThreadObject.layer(registrations, { publication: Layer.effect(ThreadPublication)(...) })`. */\nexport class ThreadPublication extends Context.Service<\n ThreadPublication,\n ThreadPublicationService\n>()(\"@effect-agent/platform-cloudflare/ThreadPublication\") {\n static readonly layer = Layer.succeed(this)({\n invalidate: Effect.void,\n prepareGeneration: () => Effect.void,\n drain: Effect.void,\n pendingDeadline: Effect.succeed(Option.none()),\n });\n}\n\n/**\n * Host-assembled native message recovery. The driver bounds each actual Claim and persists its\n * timeout/retry before this pump returns. Do not add a second timer starting at batch selection:\n * local Claim setup may take time, and expiration still owes the driver's local retry commit.\n */\nexport const ThreadMessageDelivery = Context.Reference<{\n readonly drainUntil: (\n sourceFinished: Effect.Effect<void>,\n dispatchUntil: DateTime.Utc,\n ) => Effect.Effect<void, DurableAlarmError, Scope.Scope>;\n readonly pendingDeadline: Effect.Effect<Option.Option<number>, DurableAlarmError>;\n}>(\"@effect-agent/platform-cloudflare/ThreadMessageDelivery\", {\n defaultValue: () => ({\n drainUntil: () => Effect.void,\n pendingDeadline: Effect.succeed(Option.none()),\n }),\n});\n\n/**\n * Application obligations sharing this Object's alarm. Admit one initial external wave even on\n * a caught-up pass, then respond to wakes while native work runs. sourceFinished closes only\n * admission of NEW external waves. Keep local admission/hub subscriptions in the supplied event\n * Scope until maintenance tears it down; do not scope them to sourceFinished or to this Effect.\n * No deadline sleeps or automatic retry loops. Return after already-admitted waves finish.\n *\n * Declare a finite whole-wave allowance (1..300000ms): maximum for parallel lanes, sum for\n * sequential operations. Admit a wave only if its allowance fits before dispatchUntil. Later\n * arrivals cannot renew the retirement window. Maintenance bounds the join after sourceFinished,\n * interrupts and joins event Scope, then reads local deadlines under the mutation gate.\n *\n * Setup and pendingDeadline are bounded local operations. Persist claims/envelopes before\n * dispatch; interrupted waits leave exact retries/receipts recoverable. Cancellation is local,\n * not remote rollback. Retry accounting is unchanged. Network waits/finalizers must be\n * interruptible; short local atomic commits may be uninterruptible. Hooks never write the raw\n * alarm slot. Required native publication gates belong to ThreadPublication.\n */\nexport const ThreadHostMaintenance = Context.Reference<{\n readonly dispatchTimeoutMillis: number;\n readonly drainUntil: (\n sourceFinished: Effect.Effect<void>,\n dispatchUntil: DateTime.Utc,\n ) => Effect.Effect<void, DurableAlarmError, Scope.Scope>;\n readonly pendingDeadline: Effect.Effect<Option.Option<number>, DurableAlarmError>;\n}>(\"@effect-agent/platform-cloudflare/ThreadHostMaintenance\", {\n defaultValue: () => ({\n dispatchTimeoutMillis: 1,\n drainUntil: () => Effect.void,\n pendingDeadline: Effect.succeed(Option.none()),\n }),\n});\n\nconst earliestDeadline = (\n left: Option.Option<number>,\n right: Option.Option<number>,\n): Option.Option<number> =>\n Option.isSome(left)\n ? Option.isSome(right)\n ? Option.some(Math.min(left.value, right.value))\n : left\n : right;\n\n/** @internal A committed source operation must not become a failed operation because delivery failed. */\nexport const publishCommitted = Effect.gen(function* () {\n const publication = yield* ThreadPublication;\n\n yield* publication.invalidate.pipe(Effect.andThen(publication.drain));\n}).pipe(\n Effect.catchCause((cause) =>\n Cause.hasInterrupts(cause)\n ? Effect.interrupt\n : Effect.logError(\"Thread publication deferred after source commit\", cause),\n ),\n);\n\nconst MaintenanceGeneration = Schema.BigIntFromString.check(\n Schema.isGreaterThanOrEqualToBigInt(0n),\n);\n\nclass BindingRetry extends Schema.Class<BindingRetry>(\"BindingRetry\")({\n threadId: ThreadId,\n submissionId: SubmissionId,\n attempts: Schema.Natural,\n notBefore: Schema.Finite,\n reportedAt: Schema.Finite,\n}) {}\n\ninterface MaintenanceObservation {\n generation?: bigint;\n nativeOnly: boolean;\n}\n\nclass MaintenanceRetry extends Schema.Class<MaintenanceRetry>(\"MaintenanceRetry\")({\n generation: MaintenanceGeneration,\n notBefore: Schema.Finite,\n nativeOnly: Schema.Boolean,\n stalls: Schema.Int.check(Schema.isGreaterThanOrEqualTo(0), Schema.isLessThanOrEqualTo(30)),\n}) {}\n\n/** Versioned, platform-private maintenance state stored through Durable Object KV. */\nclass ThreadMaintenanceState extends Schema.Class<ThreadMaintenanceState>(\n \"@effect-agent/platform-cloudflare/ThreadMaintenanceState\",\n)({\n schemaVersion: Schema.Literal(1),\n dirty: MaintenanceGeneration,\n processed: MaintenanceGeneration,\n nonterminal: Schema.Int.check(Schema.isGreaterThanOrEqualTo(0)),\n /** One physical-owner cursor; old single-lane records need no conversion. */\n lastServedThreadId: Schema.optionalKey(ThreadId),\n bindingRetries: Schema.optionalKey(Schema.Array(BindingRetry)),\n /** Absent on older records. A newer mutation makes this retry obsolete. */\n retry: Schema.optionalKey(MaintenanceRetry),\n}) {}\n\nconst MAINTENANCE_STATE_KEY = \"effect-agent:thread-maintenance:v1\";\nconst decodeMaintenanceState = Schema.decodeUnknownSync(ThreadMaintenanceState);\nconst encodeMaintenanceState = Schema.encodeSync(ThreadMaintenanceState);\n\nconst initialMaintenanceState = (): ThreadMaintenanceState =>\n ThreadMaintenanceState.make({\n schemaVersion: 1,\n // Bootstrap Objects created by the pre-generation release without scanning the ledger in\n // the constructor. One useful pass classifies and acknowledges any existing obligation.\n dirty: 1n,\n processed: 0n,\n nonterminal: 0,\n });\n\nconst readMaintenanceState = async (\n transaction: DurableObjectTransaction,\n): Promise<{ readonly state: ThreadMaintenanceState; readonly initialized: boolean }> => {\n const encoded = await transaction.get(MAINTENANCE_STATE_KEY);\n\n return encoded === undefined\n ? { state: initialMaintenanceState(), initialized: false }\n : { state: decodeMaintenanceState(encoded), initialized: true };\n};\n\nconst ensureTransactionAlarmBy = async (\n transaction: DurableObjectTransaction,\n deadline: number,\n): Promise<void> => {\n const scheduled = await transaction.getAlarm();\n\n if (scheduled === null || scheduled > deadline) {\n await transaction.setAlarm(deadline);\n }\n};\n\nconst stableExternalWait = (\n snapshot: SubmissionSnapshot,\n reports: ReadonlyMap<string, RecoveryReport>,\n): boolean => {\n const decision = reports.get(snapshot.submissionId)?.decision._tag;\n\n // An accepted abort still owes cleanup/settlement even if its claim was deferred this pass.\n if (decision === \"SettleAborted\") return false;\n switch (snapshot.state) {\n case \"suspended\":\n case \"joined\":\n return true;\n case \"unknown\":\n return decision === \"AwaitUnknownResolution\" || decision === \"MarkUnknown\";\n case \"admitted\":\n return reports.get(snapshot.submissionId)?.decision._tag === \"AwaitParentEstablishment\";\n case \"input-applied\":\n case \"joining\":\n case \"ready\":\n case \"running\":\n case \"settled\":\n case \"terminalizing\":\n return false;\n }\n};\n\n/**\n * Shared prearm/acknowledgement boundary for ingress and runtime-owned producers.\n * `ThreadObject.layer` provides this same instance in its Services. Rebuilt runtime/maintenance\n * Layers must reuse that instance; a second gate cannot observe the native producers' activity.\n */\nexport class ThreadMutationGate extends Context.Service<\n ThreadMutationGate,\n {\n readonly withMutation: <A, E, R>(\n body: Effect.Effect<A, E, R>,\n /**\n * Native admission, approval, abort and unknown resolution keep the default true.\n * Projection/relay/reply receipt-only bookkeeping must use false: its local durable\n * pendingDeadline owns scheduling without creating native recovery debt.\n */\n options?: { readonly invalidatesRecovery: boolean },\n ) => Effect.Effect<A, E | DurableAlarmError, R>;\n readonly withSnapshot: <A, E, R>(\n body: (active: number) => Effect.Effect<A, E, R>,\n ) => Effect.Effect<A, E, R>;\n }\n>()(\"@effect-agent/platform-cloudflare/internal/ThreadMutationGate\") {\n static readonly layer = Layer.effect(this)(\n Effect.gen(function* () {\n const { ctx } = yield* DurableObjectContext;\n const config = yield* CloudflareDurableRuntimeConfig;\n const failpoint = yield* ThreadMaintenanceFailpoint;\n // A fresh incarnation has no live mutations; durable generations survive eviction.\n const activeMutations = yield* Ref.make(0);\n const generationGate = yield* Semaphore.make(1);\n const minimumAlarmDelay = Math.max(1, Math.ceil(config.alarmBackoffBase / 2));\n\n const runTransaction = yield* makeStorageOperation;\n\n const beginMutation = Effect.fn(\"ThreadMaintenance.beginMutation\")(function* (\n invalidatesRecovery: boolean,\n ) {\n yield* failpoint.hit(\"maintenance:dirty:before\");\n const now = yield* Clock.currentTimeMillis;\n\n yield* runTransaction(\"advance maintenance generation\", () =>\n ctx.storage.transaction(async (transaction) => {\n const { state, initialized } = await readMaintenanceState(transaction);\n\n const next = ThreadMaintenanceState.make({\n ...state,\n dirty: state.dirty + (invalidatesRecovery ? 1n : 0n),\n });\n\n if (invalidatesRecovery || !initialized)\n await transaction.put(MAINTENANCE_STATE_KEY, encodeMaintenanceState(next));\n // The earliest configured retry bounds a newly actionable mutation without relying\n // on its best-effort immediate wake hint.\n await ensureTransactionAlarmBy(transaction, now + minimumAlarmDelay);\n }),\n );\n yield* failpoint.hit(\"maintenance:dirty:after\");\n yield* Ref.update(activeMutations, (active) => active + 1);\n });\n\n const endMutation = generationGate.withPermit(\n Ref.update(activeMutations, (active) => Math.max(0, active - 1)),\n );\n\n const withMutation = <A, E, R>(\n body: Effect.Effect<A, E, R>,\n options?: { readonly invalidatesRecovery: boolean },\n ): Effect.Effect<A, E | DurableAlarmError, R> =>\n Effect.acquireUseRelease(\n generationGate.withPermit(beginMutation(options?.invalidatesRecovery ?? true)),\n () =>\n failpoint.hit(\"maintenance:mutation:armed\").pipe(\n Effect.andThen(body),\n Effect.tap(() => failpoint.hit(\"maintenance:mutation:finished\")),\n ),\n () => endMutation,\n );\n\n return ThreadMutationGate.of({\n withMutation,\n withSnapshot: (body) =>\n generationGate.withPermit(Effect.flatMap(Ref.get(activeMutations), body)),\n });\n }),\n );\n}\n\nexport type MaintenancePassFailure =\n | DurableWorkerFailure\n | DurableBindingFailure\n | DurableAlarmError\n | ThreadProjectionError;\n\n/**\n * Incremental, quiescent maintenance over a durable dirty/processed generation (issue #93).\n *\n * `pass` = generation snapshot/pre-arm → recovery → one head Attempt → generation acknowledgement:\n *\n * 1. One storage transaction reads dirty/processed and re-arms before work. A caught-up forced\n * alarm takes an O(1) path without recovery, ledger scans, or canonical-history reads.\n * 2. Recovery strictly precedes a new claim. One head Attempt advances the lane and requests\n * a safe yield after ten minutes. The whole event has a fourteen-minute cooperative timeout.\n * 3. Auxiliary dispatch and listeners belong to the event Scope. After native completion,\n * stop new external waves, join native delivery through its driver-owned Claim deadline,\n * and bound host/backfill work by explicit allowances before closing and joining Scope.\n * Ordinary auxiliary setup failures are reported after the one native opportunity.\n * 4. The final transaction acknowledges only the generation observed at pass start. A racing\n * mutation therefore remains `dirty > processed` and retains its atomically-established alarm.\n * 5. Stable external waits acknowledge and clear. Autonomous retry, indeterminate, and lease\n * recovery states leave their generation dirty and retain bounded backoff rearming.\n */\nexport class ThreadMaintenance extends Context.Service<\n ThreadMaintenance,\n {\n /** One idempotent pass; failures propagate after durably scheduling bounded recovery. */\n readonly pass: Effect.Effect<MaintenancePassReport, MaintenancePassFailure>;\n /**\n * Constructor gate: initialize/inspect only the O(1) maintenance record and ensure a dirty\n * generation has an alarm. It never scans the ledger or canonical history.\n */\n readonly ensureAlarm: Effect.Effect<void, MaintenancePassFailure>;\n /**\n * Serialize the pre-arm boundary with pass acknowledgement, advance the durable dirty\n * generation and arm the alarm in one transaction BEFORE running the caller's mutation.\n * A pass cannot acknowledge while that mutation remains in flight.\n */\n readonly withMutation: <A, E, R>(\n body: Effect.Effect<A, E, R>,\n ) => Effect.Effect<A, E | DurableAlarmError, R>;\n }\n>()(\"@effect-agent/platform-cloudflare/ThreadMaintenance\") {\n static readonly layer: Layer.Layer<\n ThreadMaintenance,\n never,\n | ThreadMutationGate\n | ThreadPublication\n | ThreadProjectionMaintenance\n | DurableAgentRuntime\n | SubmissionLedger\n | DurableAlarmService\n | ThreadMaintenanceFailpoint\n | CloudflareDurableRuntimeConfig\n | DurableObjectContext\n | SqlClient\n > = Layer.effect(ThreadMaintenance)(\n Effect.gen(function* () {\n const runtime = yield* DurableAgentRuntime;\n const ledger = yield* SubmissionLedger;\n const alarm = yield* DurableAlarmService;\n const config = yield* CloudflareDurableRuntimeConfig;\n const { ctx } = yield* DurableObjectContext;\n const failpoint = yield* ThreadMaintenanceFailpoint;\n\n const mutations = yield* ThreadMutationGate;\n const publication = yield* ThreadPublication;\n const projection = yield* ThreadProjectionMaintenance;\n const messages = yield* ThreadMessageDelivery;\n const host = yield* ThreadHostMaintenance;\n\n // A broken disposable index still needs a retry alarm and must not prevent startup.\n const projectionDeadline = projection.pendingDeadline.pipe(\n Effect.catchCauseIf(\n (cause) => !Cause.hasInterrupts(cause),\n (cause) =>\n Effect.logError(\"Thread projection deadline unavailable\", cause).pipe(\n Effect.as(Option.some(0)),\n ),\n ),\n );\n\n const pendingDeadline = Effect.gen(function* () {\n return earliestDeadline(\n earliestDeadline(yield* publication.pendingDeadline, yield* messages.pendingDeadline),\n earliestDeadline(yield* projectionDeadline, yield* host.pendingDeadline),\n );\n });\n\n const maintenancePassGate = yield* Semaphore.make(1);\n const minimumAlarmDelay = Math.max(1, Math.ceil(config.alarmBackoffBase / 2));\n\n const runTransaction = yield* makeStorageOperation;\n\n const ensureAlarm = Effect.fn(\"ThreadMaintenance.ensureAlarm\")(function* () {\n yield* failpoint.hit(\"maintenance:ensure:before\");\n const now = yield* Clock.currentTimeMillis;\n\n const retry = yield* runTransaction(\"ensure maintenance alarm\", () =>\n ctx.storage.transaction(async (transaction) => {\n const { state, initialized } = await readMaintenanceState(transaction);\n\n if (!initialized) {\n await transaction.put(MAINTENANCE_STATE_KEY, encodeMaintenanceState(state));\n }\n if (state.dirty > state.processed) {\n await ensureTransactionAlarmBy(\n transaction,\n state.retry?.generation === state.dirty\n ? Math.max(now + minimumAlarmDelay, state.retry.notBefore)\n : now + config.wakeScanInterval,\n );\n }\n\n return state.retry?.generation === state.dirty ? state.retry : undefined;\n }),\n );\n\n const deadline = yield* pendingDeadline;\n\n if (Option.isSome(deadline)) {\n yield* runTransaction(\"ensure publication alarm\", () =>\n ctx.storage.transaction((transaction) =>\n ensureTransactionAlarmBy(\n transaction,\n Math.max(\n now + minimumAlarmDelay,\n deadline.value <= now && retry !== undefined && !retry.nativeOnly\n ? Math.max(deadline.value, retry.notBefore)\n : deadline.value,\n ),\n ),\n ),\n );\n }\n yield* failpoint.hit(\"maintenance:ensure:after\");\n });\n\n const beginPass = Effect.fn(\"ThreadMaintenance.beginPass\")(function* (observed: {\n generation?: bigint;\n }) {\n yield* failpoint.hit(\"maintenance:begin:before\");\n const now = yield* Clock.currentTimeMillis;\n\n const result = yield* runTransaction(\"begin maintenance pass\", () =>\n ctx.storage.transaction(async (transaction) => {\n const { state, initialized } = await readMaintenanceState(transaction);\n\n observed.generation = state.dirty;\n if (!initialized) {\n await transaction.put(MAINTENANCE_STATE_KEY, encodeMaintenanceState(state));\n }\n const retryAt = state.retry?.generation === state.dirty ? state.retry.notBefore : 0;\n\n // Prearm before recovery or any host hook, including publication-only passes.\n // A completed pass may move this crash fallback later to its bounded retry.\n await ensureTransactionAlarmBy(transaction, now + minimumAlarmDelay);\n if (state.processed >= state.dirty || retryAt > now) {\n return {\n _tag: \"CaughtUp\" as const,\n nonterminal: state.nonterminal,\n };\n }\n\n return {\n _tag: \"Actionable\" as const,\n generation: state.dirty,\n nonterminal: state.nonterminal,\n stalls: state.retry?.generation === state.dirty ? state.retry.stalls : 0,\n };\n }),\n );\n\n yield* failpoint.hit(\"maintenance:begin:after\");\n\n return result;\n });\n\n const backoffDelay = (priorStalls: number, jitter: number) => {\n const backoff = Math.min(\n config.alarmBackoffCap,\n config.alarmBackoffBase * 2 ** Math.min(priorStalls, 30),\n );\n\n // Jitter over [backoff/2, backoff] spreads retries without exceeding the cap.\n return Math.ceil(backoff / 2 + (backoff / 2) * jitter);\n };\n\n const rearmDelay = Effect.fn(\"ThreadMaintenance.rearmDelay\")(function* (\n progressed: boolean,\n priorStalls: number,\n ) {\n return progressed ? config.alarmBackoffBase : backoffDelay(priorStalls, yield* Random.next);\n });\n\n const rearmFailure = Effect.fn(\"ThreadMaintenance.rearmFailure\")(function* (\n observed: MaintenanceObservation,\n ) {\n const { generation, nativeOnly } = observed;\n\n if (generation === undefined) return;\n yield* failpoint.hit(\"maintenance:retry:before\");\n yield* mutations.withSnapshot((active) =>\n Effect.gen(function* () {\n // A failed deadline read must not prevent committing the native retry.\n const deadline = yield* pendingDeadline.pipe(\n Effect.catchCause(() => Effect.succeed(Option.none<number>())),\n );\n\n const now = yield* Clock.currentTimeMillis;\n\n const jitter = yield* Random.next;\n\n yield* runTransaction(\"back off failed maintenance\", () =>\n ctx.storage.transaction(async (transaction) => {\n const { state } = await readMaintenanceState(transaction);\n\n const previous = state.retry?.generation === generation ? state.retry : undefined;\n\n const retry = MaintenanceRetry.make({\n generation,\n notBefore: Math.max(\n previous?.notBefore ?? 0,\n now + backoffDelay(previous?.stalls ?? 0, jitter),\n ),\n nativeOnly,\n stalls: Math.min(30, (previous?.stalls ?? 0) + 1),\n });\n\n await transaction.put(\n MAINTENANCE_STATE_KEY,\n encodeMaintenanceState(ThreadMaintenanceState.make({ ...state, retry })),\n );\n\n // Never postpone a producer that raced the failed observation. Host work\n // retains its own deadline; an early delivery skips native recovery below.\n const nativeDeadline =\n active > 0 || state.dirty !== generation\n ? now + minimumAlarmDelay\n : retry.notBefore;\n\n await transaction.setAlarm(\n Math.max(\n now + minimumAlarmDelay,\n Option.isSome(deadline) && (nativeOnly || deadline.value > now)\n ? Math.min(nativeDeadline, deadline.value)\n : nativeDeadline,\n ),\n );\n }),\n );\n }),\n );\n yield* failpoint.hit(\"maintenance:retry:after\");\n });\n\n const pass = Effect.fn(\"ThreadMaintenance.pass\")(function* (\n yieldAfter: DateTime.Utc,\n dispatchUntil: DateTime.Utc,\n observed: MaintenanceObservation,\n ): Effect.fn.Return<MaintenancePassReport, MaintenancePassFailure, Scope.Scope> {\n const annotate = (report: MaintenancePassReport) =>\n Effect.annotateCurrentSpan({\n phase: report.phase,\n recovered: report.recovered,\n settled: report.settled,\n nonterminal: report.nonterminal,\n alarm: report.alarm,\n }).pipe(Effect.as(report));\n\n const started = yield* mutations.withSnapshot((activeAtStart) =>\n Effect.gen(function* () {\n const generation = yield* beginPass(observed);\n\n if (generation._tag === \"Actionable\" && activeAtStart === 0) {\n // The gate excludes a producer starting between the snapshot and certification.\n yield* publication.prepareGeneration(generation.generation);\n }\n\n return { ...generation, activeAtStart };\n }),\n );\n\n // This scope owns auxiliary dispatch and listeners, independently of source completion.\n // Close it before acknowledgement, including on failure or event interruption.\n const auxiliaryScope = yield* Effect.acquireRelease(Scope.make(\"parallel\"), (scope, exit) =>\n Scope.close(scope, exit),\n );\n\n const sourceFinished = yield* Deferred.make<void>();\n const finished = Deferred.await(sourceFinished);\n\n // Fork setup too: an ordinary auxiliary setup failure is reported after native work,\n // rather than gating its opportunity. Event interruption still closes every fiber.\n const deliveryFiber = yield* Effect.forkIn(\n Scope.provide(auxiliaryScope)(messages.drainUntil(finished, dispatchUntil)),\n auxiliaryScope,\n );\n\n const hostFiber = yield* Effect.forkIn(\n Scope.provide(auxiliaryScope)(\n Effect.gen(function* () {\n yield* Schema.decodeEffect(AuxiliaryDispatchMillis)(host.dispatchTimeoutMillis).pipe(\n Effect.mapError((cause) =>\n DurableAlarmError.make({\n operation: \"host dispatch allowance\",\n message:\n \"Declare an integer whole-wave allowance between 1 and 300000 milliseconds\",\n cause,\n }),\n ),\n );\n yield* host.drainUntil(finished, dispatchUntil);\n }),\n ),\n auxiliaryScope,\n );\n\n // Backfill is one disposable wave; its timer starts beside native execution.\n const backfill = yield* Effect.forkIn(\n drainDue.pipe(\n Effect.provideService(ThreadProjectionMaintenance, projection),\n Effect.timeoutOption(config.projectionDispatchTimeoutMillis),\n ),\n auxiliaryScope,\n );\n\n const finishAuxiliary = Effect.gen(function* () {\n yield* Deferred.succeed(sourceFinished, undefined);\n\n const remaining = Math.max(\n 1,\n DateTime.toEpochMillis(dispatchUntil) - (yield* Clock.currentTimeMillis),\n );\n\n const hostJoin = yield* Effect.forkIn(\n Fiber.join(hostFiber).pipe(\n Effect.timeoutOption(Math.min(host.dispatchTimeoutMillis, remaining)),\n Effect.tap((result) =>\n Effect.annotateCurrentSpan({ \"host.timedOut\": Option.isNone(result) }),\n ),\n ),\n auxiliaryScope,\n );\n\n // Native transport uses the driver's actual Claim deadline, through Retry persistence.\n // Only our host/backfill timers suppress their own interruption. Earlier failed exits,\n // including pure self-interruption, remain failures while siblings are still blocked.\n yield* Fiber.joinAll([deliveryFiber, hostJoin, backfill]);\n yield* Scope.close(auxiliaryScope, Exit.void);\n });\n\n const deadline = yield* publication.pendingDeadline;\n\n if (\n started._tag === \"Actionable\" ||\n (Option.isSome(deadline) && deadline.value <= (yield* Clock.currentTimeMillis))\n ) {\n yield* publication.drain;\n }\n const pending = yield* publication.pendingDeadline;\n\n if (started._tag === \"CaughtUp\" || Option.isSome(pending)) {\n yield* finishAuxiliary;\n yield* failpoint.hit(\"maintenance:finish:before\");\n\n const disposition = yield* mutations.withSnapshot((active) =>\n Effect.gen(function* () {\n // Re-read under the producer gate: a concurrent append/host mutation cannot be\n // cleared using a stale empty deadline. Dirty generations bound all producer races.\n const latest = yield* pendingDeadline;\n const now = yield* Clock.currentTimeMillis;\n\n return yield* runTransaction(\"finish publication pass\", () =>\n ctx.storage.transaction(async (transaction) => {\n const { state } = await readMaintenanceState(transaction);\n\n const nativeDeadline =\n active > 0 || state.dirty > state.processed\n ? state.retry?.generation === state.dirty && active === 0\n ? Math.max(now + minimumAlarmDelay, state.retry.notBefore)\n : now + config.wakeScanInterval\n : Infinity;\n\n const next = Option.isSome(latest)\n ? Math.min(nativeDeadline, latest.value)\n : nativeDeadline;\n\n if (Number.isFinite(next)) {\n await transaction.setAlarm(Math.max(now + minimumAlarmDelay, next));\n\n return \"rearmed\" as const;\n }\n await transaction.deleteAlarm();\n\n return \"cleared\" as const;\n }),\n );\n }),\n );\n\n yield* failpoint.hit(\"maintenance:finish:after\");\n\n return yield* annotate(\n MaintenancePassReport.make({\n phase: \"caught-up\",\n recovered: 0,\n settled: 0,\n nonterminal: started.nonterminal,\n alarm: disposition,\n }),\n );\n }\n // Step 2 — reconciliation strictly precedes new work in this pass (exit gate).\n observed.nativeOnly = true;\n const recovered: ReadonlyArray<RecoveryReport> = yield* runtime.runRecovery;\n const reports = new Map(recovered.map((report) => [report.submissionId, report]));\n const current = yield* Stream.runCollect(ledger.scanNonterminal);\n const heads = new Map<ThreadId, SubmissionSnapshot>();\n\n for (const row of current) {\n if (!heads.has(row.threadId)) heads.set(row.threadId, row);\n }\n\n const eligible = [...heads.values()]\n .filter((head) => !stableExternalWait(head, reports))\n .map((head) => head.threadId)\n .sort();\n\n const selectionTime = yield* Clock.currentTimeMillis;\n\n yield* failpoint.hit(\"maintenance:select:before\");\n\n const selection = yield* runTransaction(\"select maintenance lane\", () =>\n ctx.storage.transaction(async (transaction) => {\n const { state } = await readMaintenanceState(transaction);\n\n const retries = (state.bindingRetries ?? []).filter(\n (retry) => heads.get(retry.threadId)?.submissionId === retry.submissionId,\n );\n\n const runnable = eligible.filter(\n (threadId) =>\n !retries.some(\n (retry) => retry.threadId === threadId && retry.notBefore > selectionTime,\n ),\n );\n\n const next =\n runnable.find(\n (threadId) =>\n state.lastServedThreadId === undefined || threadId > state.lastServedThreadId,\n ) ?? runnable[0];\n\n if (next !== undefined) {\n await transaction.put(\n MAINTENANCE_STATE_KEY,\n encodeMaintenanceState(\n ThreadMaintenanceState.make({ ...state, lastServedThreadId: next }),\n ),\n );\n }\n\n return { selected: next, retries };\n }),\n );\n\n yield* failpoint.hit(\"maintenance:select:after\");\n\n const selected =\n selection.selected === undefined ? undefined : heads.get(selection.selected);\n\n let retries = selection.retries;\n let bindingFailure: DurableBindingFailure | undefined;\n\n // One FIFO head per event. An unavailable contract is a durable wait for compatible code,\n // including for children; other local lanes and host deliveries remain independently due.\n const settlement =\n selected === undefined\n ? Option.none()\n : yield* runtime.processThreadHead(selected.threadId, { yieldAfter }).pipe(\n Effect.catchTag([\"BindingUnavailable\", \"BindingDigestMismatch\"], (failure) => {\n bindingFailure = failure;\n\n return Effect.succeed(Option.none());\n }),\n );\n\n if (selected !== undefined) {\n const previous = retries.find((retry) => retry.submissionId === selected.submissionId);\n let retry: BindingRetry | undefined;\n let reportBindingFailure = false;\n\n if (bindingFailure !== undefined) {\n const now = yield* Clock.currentTimeMillis;\n const attempts = Math.min(30, (previous?.attempts ?? 0) + 1);\n\n reportBindingFailure =\n previous === undefined || now - previous.reportedAt >= 15 * 60_000;\n retry = BindingRetry.make({\n threadId: selected.threadId,\n submissionId: selected.submissionId,\n attempts,\n notBefore: now + Math.min(60_000, 5_000 * 2 ** (attempts - 1)),\n reportedAt: reportBindingFailure ? now : (previous?.reportedAt ?? now),\n });\n }\n if (retry !== undefined || previous !== undefined) {\n // The Attempt released its Claim. Commit its binding wait (or clear) once,\n // before joining fallible auxiliary work. This local fact neither acknowledges\n // a generation nor changes the shared alarm; those require event retirement.\n yield* failpoint.hit(\"maintenance:binding-retry:before\");\n retries = yield* runTransaction(\"record submission binding retry\", () =>\n ctx.storage.transaction(async (transaction) => {\n const { state } = await readMaintenanceState(transaction);\n\n const bindingRetries = [\n ...(state.bindingRetries ?? []).filter(\n (entry) => entry.submissionId !== selected.submissionId,\n ),\n ...(retry === undefined ? [] : [retry]),\n ];\n\n await transaction.put(\n MAINTENANCE_STATE_KEY,\n encodeMaintenanceState(ThreadMaintenanceState.make({ ...state, bindingRetries })),\n );\n\n return bindingRetries;\n }),\n );\n yield* failpoint.hit(\"maintenance:binding-retry:after\");\n }\n if (bindingFailure !== undefined) {\n yield* reportBindingFailure\n ? Effect.logError(\n \"Thread awaits a compatible binding; original work remains pending\",\n Cause.fail(bindingFailure),\n )\n : Effect.logDebug(\"Thread binding retry remains pending\", Cause.fail(bindingFailure));\n }\n }\n\n observed.nativeOnly = false;\n yield* finishAuxiliary;\n const remaining = yield* Stream.runCollect(ledger.scanNonterminal);\n const waitingHeads = new Map<ThreadId, boolean>();\n\n const autonomous = remaining.some((snapshot) => {\n const headWaiting = waitingHeads.get(snapshot.threadId);\n\n if (headWaiting === undefined)\n waitingHeads.set(snapshot.threadId, stableExternalWait(snapshot, reports));\n // FIFO followers cannot execute through a stable external wait. Only plain queued\n // input is dormant here; admission repairs and accepted aborts still need a pass.\n if (\n headWaiting === true &&\n snapshot.state === \"ready\" &&\n reports.get(snapshot.submissionId)?.decision._tag === \"ApplyInput\"\n )\n return false;\n\n return !stableExternalWait(snapshot, reports);\n });\n\n const progressed =\n Option.isSome(settlement) ||\n recovered.some((report) => report.disposition === \"repaired\");\n\n const now = yield* Clock.currentTimeMillis;\n const ordinaryDelay = autonomous ? yield* rearmDelay(progressed, started.stalls) : 0;\n\n const nextEligible = eligible.map(\n (threadId) =>\n retries.find((retry) => retry.submissionId === heads.get(threadId)?.submissionId)\n ?.notBefore ?? now,\n );\n\n const bindingDelay =\n nextEligible.length === 0 ? 0 : Math.max(0, Math.min(...nextEligible) - now);\n\n const delay = Math.max(ordinaryDelay, bindingDelay);\n\n yield* failpoint.hit(\"maintenance:finish:before\");\n\n const alarmDisposition = yield* mutations.withSnapshot((active) =>\n Effect.gen(function* () {\n const publicationDeadline = yield* pendingDeadline;\n\n return yield* runTransaction(\"finish maintenance pass\", () =>\n ctx.storage.transaction(async (transaction) => {\n const { state } = await readMaintenanceState(transaction);\n\n // Autonomous work and in-flight mutations intentionally leave the observed\n // generation dirty. Otherwise acknowledge only the pass-start generation.\n const processed =\n autonomous || started.activeAtStart > 0 || active > 0\n ? state.processed\n : state.processed > started.generation\n ? state.processed\n : started.generation;\n\n const next = ThreadMaintenanceState.make({\n ...Struct.omit(state, [\"retry\"]),\n processed,\n nonterminal: remaining.length,\n bindingRetries: (state.bindingRetries ?? []).filter((retry) =>\n remaining.some((row) => row.submissionId === retry.submissionId),\n ),\n ...(autonomous && !progressed\n ? {\n retry: MaintenanceRetry.make({\n generation: started.generation,\n notBefore: now + delay,\n nativeOnly: true,\n stalls: Math.min(30, started.stalls + 1),\n }),\n }\n : {}),\n });\n\n await transaction.put(MAINTENANCE_STATE_KEY, encodeMaintenanceState(next));\n if (autonomous) {\n // Replace the crash-fallback slot with this pass's bounded backoff. The target\n // is never earlier than the begin-pass fallback, so workerd does not cancel\n // this running alarm handler before its report/span can complete.\n const nativeDeadline =\n started.activeAtStart > 0 || active > 0 || state.dirty !== started.generation\n ? now + minimumAlarmDelay\n : now + delay;\n\n await transaction.setAlarm(\n Option.isSome(publicationDeadline)\n ? Math.max(\n now + minimumAlarmDelay,\n Math.min(nativeDeadline, publicationDeadline.value),\n )\n : nativeDeadline,\n );\n\n return \"rearmed\" as const;\n }\n if (started.activeAtStart > 0 || active > 0 || next.dirty > next.processed) {\n // A mutation overlapped this pass's observation window or raced\n // acknowledgement. It stays dirty and its pre-armed bounded alarm survives;\n // unseen effects are never acknowledged. Do not accelerate that future alarm\n // from inside the current handler: workerd cancels a running handler when it\n // writes an earlier slot.\n await ensureTransactionAlarmBy(\n transaction,\n Option.isSome(publicationDeadline)\n ? Math.max(\n now + minimumAlarmDelay,\n Math.min(now + config.wakeScanInterval, publicationDeadline.value),\n )\n : now + config.wakeScanInterval,\n );\n\n return \"rearmed\" as const;\n }\n if (Option.isSome(publicationDeadline)) {\n await transaction.setAlarm(\n Math.max(now + minimumAlarmDelay, publicationDeadline.value),\n );\n\n return \"rearmed\" as const;\n }\n await transaction.deleteAlarm();\n\n return \"cleared\" as const;\n }),\n );\n }),\n );\n\n yield* failpoint.hit(\"maintenance:finish:after\");\n\n return yield* annotate(\n MaintenancePassReport.make({\n phase: \"actionable\",\n recovered: recovered.length,\n settled: Option.isSome(settlement) ? 1 : 0,\n nonterminal: remaining.length,\n alarm: alarmDisposition,\n }),\n );\n });\n\n return ThreadMaintenance.of({\n // A mid-pass immediate hint is droppable; durable dirty state decides the final alarm.\n pass: Effect.gen(function* () {\n const now = yield* Clock.currentTimeMillis;\n const yieldAfter = DateTime.makeUnsafe(now + 10 * 60_000);\n const dispatchUntil = DateTime.makeUnsafe(now + 14 * 60_000);\n const observed: MaintenanceObservation = { nativeOnly: false };\n\n return yield* alarm.withWakesDeferred(\n maintenancePassGate.withPermit(\n Effect.scoped(pass(yieldAfter, dispatchUntil, observed)).pipe(\n // Close event-owned auxiliary work and release Attempt ownership before\n // failure rearming, while still holding the pass permit.\n Effect.onErrorIf(\n () => true,\n () => rearmFailure(observed),\n ),\n ),\n ),\n );\n }).pipe(\n // Include permit waiting, recovery and acknowledgement in the event deadline.\n // Interruption releases Attempt ownership, leaving the prearmed dirty generation\n // for recovery. It never changes the logical Run duration or settles a policy failure.\n // This cooperative timer cannot preempt synchronous CPU work or stuck finalizers.\n Effect.timeoutOrElse({\n duration: \"14 minutes\",\n orElse: () =>\n DurableAlarmError.make({\n operation: \"maintenance pass deadline\",\n message:\n \"The maintenance event exceeded its 14 minute deadline; durable recovery remains pending\",\n }),\n }),\n ),\n ensureAlarm: mutations.withSnapshot(() => ensureAlarm()),\n withMutation: (body) =>\n mutations.withMutation(\n body.pipe(\n Effect.tap(() =>\n publishCommitted.pipe(Effect.provideService(ThreadPublication, publication)),\n ),\n ),\n ),\n });\n }),\n );\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmDA,IAAa,oBAAb,cAAuC,OAAO,YAA+B,CAAC,CAC5E,qBACA;CACE,WAAW,OAAO;CAClB,SAAS,OAAO;CAChB,OAAO,OAAO,YAAY,OAAO,OAAO,CAAC;AAC3C,CACF,CAAC,CAAC,CAAC;AAEH,MAAM,gBACH,eACA,UACC,kBAAkB,KAAK;CACrB;CACA,SAAS,iBAAiB,OAAO,sDAAsD;CACvF;AACF,CAAC;AAIL,MAAM,uBAAuB,OAAO,IAClC,YACC,SACK,WAAmB,YACrB,OAAO,QAAQ,OAAO,cAAc,IAAI,kBAAkB,IAAI,YAAY;CACxE,MAAM,OAAO,OAAO,gBAClB,OAAO,WAAW;EAAE,KAAK;EAAS,OAAO,aAAa,SAAS;CAAE,CAAC,CACpE;CAEA,OAAO,QAAQ,SAAS,SACpB,OACA,OAAO,OACL,OAAO,QAAQ,IAAI,QAAQ,KAAK,OAAO,SAAS,aAAa,SAAS,CAAC,CAAC,GAAG,IAAI,CACjF;AACN,CAAC,CACP;;AAGA,IAAa,sBAAb,MAAa,4BAA4B,QAAQ,QA+B/C,CAAC,CAAC,uDAAuD,CAAC,CAAC;CAC3D,OAAgB,QACd,MAAM,OAAO,mBAAmB,CAAC,CAC/B,OAAO,IAAI,aAAa;EACtB,MAAM,EAAE,QAAQ,OAAO;;;;;;EAMvB,MAAM,gBAAgB,OAAO,IAAI,KAAK,CAAC;EAEvC,MAAM,mBAAmB,OAAO;EAEhC,MAAM,YAAY,iBAAiB,mBAAmB,IAAI,QAAQ,SAAS,CAAC,C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OAAO;KACT;KACA,IAAI,OAAO,OAAO,mBAAmB,GAAG;MACtC,MAAM,YAAY,SAChB,KAAK,IAAI,MAAM,mBAAmB,oBAAoB,KAAK,CAC7D;MAEA,OAAO;KACT;KACA,MAAM,YAAY,YAAY;KAE9B,OAAO;IACT,CAAC,CACH;GACF,CAAC,CACH;GAEA,OAAO,UAAU,IAAI,0BAA0B;GAE/C,OAAO,OAAO,SACZ,sBAAsB,KAAK;IACzB,OAAO;IACP,WAAW,UAAU;IACrB,SAAS,OAAO,OAAO,UAAU,IAAI,IAAI;IACzC,aAAa,UAAU;IACvB,OAAO;GACT,CAAC,CACH;EACF,CAAC;EAED,OAAO,kBAAkB,GAAG;GAE1B,MAAM,OAAO,IAAI,aAAa;IAC5B,MAAM,MAAM,OAAO,MAAM;IACzB,MAAM,aAAa,SAAS,WAAW,MAAM,GAAW;IACxD,MAAM,gBAAgB,SAAS,WAAW,MAAM,IAAW;IAC3D,MAAM,WAAmC,EAAE,YAAY,MAAM;IAE7D,OAAO,OAAO,MAAM,kBAClB,oBAAoB,WAClB,OAAO,OAAO,KAAK,YAAY,eAAe,QAAQ,CAAC,CAAC,CAAC,KAGvD,OAAO,gBACC,YACA,aAAa,QAAQ,CAC7B,CACF,CACF,CACF;GACF,CAAC,CAAC,CAAC,KAKD,OAAO,cAAc;IACnB,UAAU;IACV,cACE,kBAAkB,KAAK;KACrB,WAAW;KACX,SACE;IACJ,CAAC;GACL,CAAC,CACH;GACA,aAAa,UAAU,mBAAmB,YAAY,CAAC;GACvD,eAAe,SACb,UAAU,aACR,KAAK,KACH,OAAO,UACL,iBAAiB,KAAK,OAAO,eAAe,mBAAmB,WAAW,CAAC,CAC7E,CACF,CACF;EACJ,CAAC;CACH,CAAC,CACH;AACF"}
|
|
1
|
+
{"version":3,"file":"Alarm.mjs","names":[],"sources":["../src/Alarm.ts"],"sourcesContent":["import {\n Cause,\n Clock,\n Context,\n DateTime,\n Deferred,\n Effect,\n Exit,\n Fiber,\n Layer,\n Option,\n Random,\n Ref,\n Schema,\n Scope,\n Semaphore,\n Stream,\n Struct,\n} from \"effect\";\nimport { type DurableBindingFailure } from \"effect-agent/agent-registration\";\nimport {\n DurableAgentRuntime,\n type DurableWorkerFailure,\n type RecoveryReport,\n} from \"effect-agent/durable-agent-runtime\";\nimport { ThreadId, SubmissionId } from \"effect-agent/identifiers\";\nimport { SubmissionLedger, type SubmissionSnapshot } from \"effect-agent/submission-ledger\";\nimport {\n ThreadProjectionMaintenance,\n drainDue,\n type ThreadProjectionError,\n} from \"effect-agent/thread-projection-maintenance\";\nimport { SqlClient } from \"effect/unstable/sql/SqlClient\";\n\nimport { DurableObjectContext } from \"./CloudflareBindings.ts\";\nimport { AuxiliaryDispatchMillis, CloudflareDurableRuntimeConfig } from \"./CloudflareConfig.ts\";\nimport { safeCauseMessage } from \"./internal/boundary.ts\";\n\n/**\n * The single multiplexed Durable Object alarm (decision D-P6-2). A Durable Object has ONE\n * alarm slot; every cadence the Node host ran on fibers (wake scan, lease expiry, settlement\n * and abort re-checks, retry backoff) multiplexes into one idempotent maintenance pass, and\n * the slot always holds the EARLIEST deadline any caller asked for.\n *\n * The alarm invariant (plan §1.4): every committed actionable mutation carries a newer durable\n * maintenance generation and a committed alarm. Stable externally-driven waits may be\n * nonterminal without retaining an alarm; their resolving mutation advances the generation and\n * restores the alarm atomically.\n */\n\n/** The Durable Object alarm API failed; surfaces on host entry points as a typed refusal. */\nexport class DurableAlarmError extends Schema.TaggedError<DurableAlarmError>()(\n \"DurableAlarmError\",\n {\n operation: Schema.String,\n message: Schema.String,\n cause: Schema.optionalKey(Schema.Defect()),\n },\n) {}\n\nconst alarmFailure =\n (operation: string) =>\n (cause: unknown): DurableAlarmError =>\n DurableAlarmError.make({\n operation,\n message: safeCauseMessage(cause, \"The Cloudflare alarm API failed without a diagnostic\"),\n cause,\n });\n\n// SQL and raw KV/alarm operations share one physical SQLite transaction. Reserve its\n// connection for each short storage operation, never around a mutation or snapshot body.\nconst makeStorageOperation = Effect.map(\n SqlClient,\n (sql) =>\n <A>(operation: string, execute: () => Promise<A>) =>\n Effect.flatMap(Effect.serviceOption(sql.transactionService), (current) => {\n const body = Effect.uninterruptible(\n Effect.tryPromise({ try: execute, catch: alarmFailure(operation) }),\n );\n\n return current._tag === \"Some\"\n ? body\n : Effect.scoped(\n Effect.andThen(sql.reserve.pipe(Effect.mapError(alarmFailure(operation))), body),\n );\n }),\n);\n\n/** `ctx.storage` alarm slot as an Effect service; storage is truth, never a memory field. */\nexport class DurableAlarmService extends Context.Service<\n DurableAlarmService,\n {\n /** The scheduled deadline in epoch milliseconds, if any. */\n readonly scheduled: Effect.Effect<Option.Option<number>, DurableAlarmError>;\n /** Replace the slot with this deadline. */\n readonly scheduleAt: (epochMillis: number) => Effect.Effect<void, DurableAlarmError>;\n /** Keep the EARLIER of the existing deadline and this one (the multiplexing rule). */\n readonly ensureScheduledBy: (epochMillis: number) => Effect.Effect<void, DurableAlarmError>;\n /**\n * Arm an immediate alarm (the durable, coalescing local wake) — DEFERRED while a\n * maintenance pass is executing. Workerd cancels an in-flight alarm handler when a new\n * EARLIER deadline is written during its execution (`requestScheduledAlarm`), and the\n * maintenance pass runs INSIDE the alarm handler: an immediate wake landing mid-pass\n * (a routed port mutation, a sibling's `wake()`, the coordinator's own local notify)\n * would kill the running Attempt — manufacturing an ownership loss no real eviction\n * caused, and routing open uncertain-class Tool Calls into spurious Unknown Outcomes.\n * Deferral is contract-safe: wakes are droppable hints, every mutating entry point\n * pre-arms BEFORE its first durable mutation (the alarm invariant never rests on this\n * call). The pass's durable generation check observes any racing mutation, so the\n * in-memory hint does not need to be flushed after a stable wait is acknowledged.\n */\n readonly scheduleNow: Effect.Effect<void, DurableAlarmError>;\n /**\n * Run one maintenance pass with wake deferral (see `scheduleNow`). Calls made while `body`\n * executes are droppable promptness hints; correctness rests on the durable generation.\n */\n readonly withWakesDeferred: <A, E, R>(body: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>;\n /** Clear the slot; correctness-sensitive clears live in maintenance generation transactions. */\n readonly cancel: Effect.Effect<void, DurableAlarmError>;\n }\n>()(\"@effect-agent/platform-cloudflare/DurableAlarmService\") {\n static readonly layer: Layer.Layer<DurableAlarmService, never, DurableObjectContext | SqlClient> =\n Layer.effect(DurableAlarmService)(\n Effect.gen(function* () {\n const { ctx } = yield* DurableObjectContext;\n /**\n * In-memory pass bookkeeping — a pure CACHE, never state: a fresh incarnation has no\n * running pass, and a deferred wake lost to eviction was only ever a promptness hint\n * on top of the already-committed pre-armed alarm.\n */\n const runningPasses = yield* Ref.make(0);\n\n const storageOperation = yield* makeStorageOperation;\n\n const scheduled = storageOperation(\"get alarm\", () => ctx.storage.getAlarm()).pipe(\n Effect.map((deadline) =>\n deadline === null ? Option.none<number>() : Option.some(deadline),\n ),\n );\n\n const scheduleAt = (epochMillis: number) =>\n storageOperation(\"set alarm\", () => ctx.storage.setAlarm(epochMillis));\n\n const ensureScheduledBy = (epochMillis: number) =>\n storageOperation(\"ensure alarm\", () =>\n ctx.storage.transaction(async (transaction) => {\n const existing = await transaction.getAlarm();\n\n if (existing === null || existing > epochMillis) {\n await transaction.setAlarm(epochMillis);\n }\n }),\n );\n\n const armNow = Clock.currentTimeMillis.pipe(\n Effect.flatMap((now) => ensureScheduledBy(now)),\n );\n\n const scheduleNow = Ref.get(runningPasses).pipe(\n Effect.flatMap((passes) => (passes > 0 ? Effect.void : armNow)),\n );\n\n const withWakesDeferred = <A, E, R>(body: Effect.Effect<A, E, R>): Effect.Effect<A, E, R> =>\n Ref.update(runningPasses, (passes) => passes + 1).pipe(\n Effect.andThen(body),\n Effect.ensuring(Ref.update(runningPasses, (passes) => passes - 1)),\n );\n\n const cancel = storageOperation(\"delete alarm\", () => ctx.storage.deleteAlarm());\n\n return DurableAlarmService.of({\n scheduled,\n scheduleAt,\n ensureScheduledBy,\n scheduleNow,\n withWakesDeferred,\n cancel,\n });\n }),\n );\n}\n\n/** What one maintenance pass did — auditable evidence mirroring `NodeDurableHost`'s report. */\nexport class MaintenancePassReport extends Schema.Class<MaintenancePassReport>(\n \"@effect-agent/platform-cloudflare/MaintenancePassReport\",\n)({\n /** `caught-up` ran no runtime work (publication may be pending); `actionable` ran recovery. */\n phase: Schema.Literals([\"caught-up\", \"actionable\"]),\n /** Recovery decisions executed (or deferred) BEFORE any new claim in this pass. */\n recovered: Schema.Int.check(Schema.isGreaterThanOrEqualTo(0)),\n /** Whether the head Attempt settled. Joined input may settle with that head. */\n settled: Schema.Int.check(Schema.isGreaterThanOrEqualTo(0)),\n /** Submissions still nonterminal after the pass (suspended/unknown lanes stay honest). */\n nonterminal: Schema.Int.check(Schema.isGreaterThanOrEqualTo(0)),\n /** `rearmed` for dirty/autonomous work, `cleared` for stable waits or settlement. */\n alarm: Schema.Literals([\"rearmed\", \"cleared\"]),\n}) {}\n\n/** Fault boundaries around every maintenance-owned durable mutation. */\nexport type ThreadMaintenanceFailpointLocation =\n | \"maintenance:dirty:before\"\n | \"maintenance:dirty:after\"\n | \"maintenance:mutation:armed\"\n | \"maintenance:mutation:finished\"\n | \"maintenance:ensure:before\"\n | \"maintenance:ensure:after\"\n | \"maintenance:begin:before\"\n | \"maintenance:begin:after\"\n | \"maintenance:select:before\"\n | \"maintenance:select:after\"\n | \"maintenance:binding-retry:before\"\n | \"maintenance:binding-retry:after\"\n | \"maintenance:retry:before\"\n | \"maintenance:retry:after\"\n | \"maintenance:finish:before\"\n | \"maintenance:finish:after\";\n\nexport type ThreadMaintenanceFailpointHandler = (\n location: ThreadMaintenanceFailpointLocation,\n) => Effect.Effect<void>;\n\n/** Test-only fault authority; production uses the inert layer. */\nexport class ThreadMaintenanceFailpoint extends Context.Service<\n ThreadMaintenanceFailpoint,\n {\n readonly hit: ThreadMaintenanceFailpointHandler;\n }\n>()(\"@effect-agent/platform-cloudflare/ThreadMaintenanceFailpoint\") {\n static readonly layer = Layer.succeed(this)({ hit: () => Effect.void });\n}\n\n/**\n * Durable host publication of canonical records and ledger approval/abort/resolution intents.\n * The host owns schema-versioned cursors, destination idempotency and acknowledgement. Delivery\n * is at least once. Hooks must not write the alarm slot or mutate the supplied raw source ports.\n *\n * `invalidate`, `prepareGeneration` and `pendingDeadline` must be bounded local operations.\n * `prepareGeneration` durably invalidates a scan only when its generation changes; repeated\n * calls must preserve partial scan progress. It runs with no source mutation in flight.\n * Use this gate only for publication required before dependent native execution. Independent\n * UI relays and outboxes belong to ThreadHostMaintenance.\n * `drain` performs bounded delivery and persists retries before returning. A pending deadline\n * defers runtime recovery/Attempts, allowing committed host publications to drain first.\n * Unexpected hook failures leave the prearmed generation for retry. Hooks acquire per-call\n * resources with Effect.scoped; Layer construction owns incarnation resources (eviction need\n * not run finalizers). Do not hold a local hook behind network I/O or call back into producers.\n */\nexport interface ThreadPublicationService {\n readonly invalidate: Effect.Effect<void, DurableAlarmError>;\n readonly prepareGeneration: (generation: bigint) => Effect.Effect<void, DurableAlarmError>;\n readonly drain: Effect.Effect<void, DurableAlarmError>;\n readonly pendingDeadline: Effect.Effect<Option.Option<number>, DurableAlarmError>;\n}\n\n/** Opt in with `ThreadObject.layer(registrations, { publication: Layer.effect(ThreadPublication)(...) })`. */\nexport class ThreadPublication extends Context.Service<\n ThreadPublication,\n ThreadPublicationService\n>()(\"@effect-agent/platform-cloudflare/ThreadPublication\") {\n static readonly layer = Layer.succeed(this)({\n invalidate: Effect.void,\n prepareGeneration: () => Effect.void,\n drain: Effect.void,\n pendingDeadline: Effect.succeed(Option.none()),\n });\n}\n\n/**\n * Host-assembled native message recovery. The driver bounds each actual Claim and persists its\n * timeout/retry before this pump returns. Do not add a second timer starting at batch selection:\n * local Claim setup may take time, and expiration still owes the driver's local retry commit.\n */\nexport const ThreadMessageDelivery = Context.Reference<{\n readonly drainUntil: (\n sourceFinished: Effect.Effect<void>,\n dispatchUntil: DateTime.Utc,\n ) => Effect.Effect<void, DurableAlarmError, Scope.Scope>;\n readonly pendingDeadline: Effect.Effect<Option.Option<number>, DurableAlarmError>;\n}>(\"@effect-agent/platform-cloudflare/ThreadMessageDelivery\", {\n defaultValue: () => ({\n drainUntil: () => Effect.void,\n pendingDeadline: Effect.succeed(Option.none()),\n }),\n});\n\n/**\n * Application obligations sharing this Object's alarm. Admit one initial external wave even on\n * a caught-up pass, then respond to wakes while native work runs. sourceFinished closes only\n * admission of NEW external waves. Keep local admission/hub subscriptions in the supplied event\n * Scope until maintenance tears it down; do not scope them to sourceFinished or to this Effect.\n * No deadline sleeps or automatic retry loops. Return after already-admitted waves finish.\n *\n * Declare a finite whole-wave allowance (1..300000ms): maximum for parallel lanes, sum for\n * sequential operations. Admit a wave only if its allowance fits before dispatchUntil. Later\n * arrivals cannot renew the retirement window. Maintenance bounds the join after sourceFinished,\n * interrupts and joins event Scope, then reads local deadlines under the mutation gate.\n *\n * Setup and pendingDeadline are bounded local operations. Persist claims/envelopes before\n * dispatch; interrupted waits leave exact retries/receipts recoverable. Cancellation is local,\n * not remote rollback. Retry accounting is unchanged. Network waits/finalizers must be\n * interruptible; short local atomic commits may be uninterruptible. Hooks never write the raw\n * alarm slot. Required native publication gates belong to ThreadPublication.\n */\nexport const ThreadHostMaintenance = Context.Reference<{\n readonly dispatchTimeoutMillis: number;\n readonly drainUntil: (\n sourceFinished: Effect.Effect<void>,\n dispatchUntil: DateTime.Utc,\n ) => Effect.Effect<void, DurableAlarmError, Scope.Scope>;\n readonly pendingDeadline: Effect.Effect<Option.Option<number>, DurableAlarmError>;\n}>(\"@effect-agent/platform-cloudflare/ThreadHostMaintenance\", {\n defaultValue: () => ({\n dispatchTimeoutMillis: 1,\n drainUntil: () => Effect.void,\n pendingDeadline: Effect.succeed(Option.none()),\n }),\n});\n\nconst earliestDeadline = (\n left: Option.Option<number>,\n right: Option.Option<number>,\n): Option.Option<number> =>\n Option.isSome(left)\n ? Option.isSome(right)\n ? Option.some(Math.min(left.value, right.value))\n : left\n : right;\n\n/** @internal A committed source operation must not become a failed operation because delivery failed. */\nexport const publishCommitted = Effect.gen(function* () {\n const publication = yield* ThreadPublication;\n\n yield* publication.invalidate.pipe(Effect.andThen(publication.drain));\n}).pipe(\n Effect.catchCause((cause) =>\n Cause.hasInterrupts(cause)\n ? Effect.interrupt\n : Effect.logError(\"Thread publication deferred after source commit\", cause),\n ),\n);\n\nconst MaintenanceGeneration = Schema.BigIntFromString.check(\n Schema.isGreaterThanOrEqualToBigInt(0n),\n);\n\nclass BindingRetry extends Schema.Class<BindingRetry>(\"BindingRetry\")({\n threadId: ThreadId,\n submissionId: SubmissionId,\n attempts: Schema.Natural,\n notBefore: Schema.Finite,\n reportedAt: Schema.Finite,\n}) {}\n\ninterface MaintenanceObservation {\n generation?: bigint;\n nativeOnly: boolean;\n}\n\nclass MaintenanceRetry extends Schema.Class<MaintenanceRetry>(\"MaintenanceRetry\")({\n generation: MaintenanceGeneration,\n notBefore: Schema.Finite,\n nativeOnly: Schema.Boolean,\n stalls: Schema.Int.check(Schema.isGreaterThanOrEqualTo(0), Schema.isLessThanOrEqualTo(30)),\n}) {}\n\n/** Versioned, platform-private maintenance state stored through Durable Object KV. */\nclass ThreadMaintenanceState extends Schema.Class<ThreadMaintenanceState>(\n \"@effect-agent/platform-cloudflare/ThreadMaintenanceState\",\n)({\n schemaVersion: Schema.Literal(1),\n dirty: MaintenanceGeneration,\n processed: MaintenanceGeneration,\n nonterminal: Schema.Int.check(Schema.isGreaterThanOrEqualTo(0)),\n /** One physical-owner cursor; old single-lane records need no conversion. */\n lastServedThreadId: Schema.optionalKey(ThreadId),\n bindingRetries: Schema.optionalKey(Schema.Array(BindingRetry)),\n /** Absent on older records. A newer mutation makes this retry obsolete. */\n retry: Schema.optionalKey(MaintenanceRetry),\n}) {}\n\nconst MAINTENANCE_STATE_KEY = \"effect-agent:thread-maintenance:v1\";\nconst decodeMaintenanceState = Schema.decodeUnknownSync(ThreadMaintenanceState);\nconst encodeMaintenanceState = Schema.encodeSync(ThreadMaintenanceState);\n\nconst initialMaintenanceState = (): ThreadMaintenanceState =>\n ThreadMaintenanceState.make({\n schemaVersion: 1,\n // Bootstrap Objects created by the pre-generation release without scanning the ledger in\n // the constructor. One useful pass classifies and acknowledges any existing obligation.\n dirty: 1n,\n processed: 0n,\n nonterminal: 0,\n });\n\nconst readMaintenanceState = async (\n transaction: DurableObjectTransaction,\n): Promise<{ readonly state: ThreadMaintenanceState; readonly initialized: boolean }> => {\n const encoded = await transaction.get(MAINTENANCE_STATE_KEY);\n\n return encoded === undefined\n ? { state: initialMaintenanceState(), initialized: false }\n : { state: decodeMaintenanceState(encoded), initialized: true };\n};\n\nconst ensureTransactionAlarmBy = async (\n transaction: DurableObjectTransaction,\n deadline: number,\n): Promise<void> => {\n const scheduled = await transaction.getAlarm();\n\n if (scheduled === null || scheduled > deadline) {\n await transaction.setAlarm(deadline);\n }\n};\n\nconst stableExternalWait = (\n snapshot: SubmissionSnapshot,\n reports: ReadonlyMap<string, RecoveryReport>,\n): boolean => {\n const report = reports.get(snapshot.submissionId);\n const decision = report?.decision._tag;\n\n // An accepted abort still owes cleanup/settlement even if its claim was deferred this pass.\n if (decision === \"SettleAborted\") return false;\n switch (snapshot.state) {\n case \"suspended\":\n case \"joined\":\n return true;\n case \"unknown\":\n return report?.disposition === \"unknown\";\n case \"admitted\":\n return reports.get(snapshot.submissionId)?.decision._tag === \"AwaitParentEstablishment\";\n case \"input-applied\":\n case \"joining\":\n case \"ready\":\n case \"running\":\n case \"settled\":\n case \"terminalizing\":\n return false;\n }\n};\n\n/**\n * Shared prearm/acknowledgement boundary for ingress and runtime-owned producers.\n * `ThreadObject.layer` provides this same instance in its Services. Rebuilt runtime/maintenance\n * Layers must reuse that instance; a second gate cannot observe the native producers' activity.\n */\nexport class ThreadMutationGate extends Context.Service<\n ThreadMutationGate,\n {\n readonly withMutation: <A, E, R>(\n body: Effect.Effect<A, E, R>,\n /**\n * Native admission, approval, abort and unknown resolution keep the default true.\n * Projection/relay/reply receipt-only bookkeeping must use false: its local durable\n * pendingDeadline owns scheduling without creating native recovery debt.\n */\n options?: { readonly invalidatesRecovery: boolean },\n ) => Effect.Effect<A, E | DurableAlarmError, R>;\n readonly withSnapshot: <A, E, R>(\n body: (active: number) => Effect.Effect<A, E, R>,\n ) => Effect.Effect<A, E, R>;\n }\n>()(\"@effect-agent/platform-cloudflare/internal/ThreadMutationGate\") {\n static readonly layer = Layer.effect(this)(\n Effect.gen(function* () {\n const { ctx } = yield* DurableObjectContext;\n const config = yield* CloudflareDurableRuntimeConfig;\n const failpoint = yield* ThreadMaintenanceFailpoint;\n // A fresh incarnation has no live mutations; durable generations survive eviction.\n const activeMutations = yield* Ref.make(0);\n const generationGate = yield* Semaphore.make(1);\n const minimumAlarmDelay = Math.max(1, Math.ceil(config.alarmBackoffBase / 2));\n\n const runTransaction = yield* makeStorageOperation;\n\n const beginMutation = Effect.fn(\"ThreadMaintenance.beginMutation\")(function* (\n invalidatesRecovery: boolean,\n ) {\n yield* failpoint.hit(\"maintenance:dirty:before\");\n const now = yield* Clock.currentTimeMillis;\n\n yield* runTransaction(\"advance maintenance generation\", () =>\n ctx.storage.transaction(async (transaction) => {\n const { state, initialized } = await readMaintenanceState(transaction);\n\n const next = ThreadMaintenanceState.make({\n ...state,\n dirty: state.dirty + (invalidatesRecovery ? 1n : 0n),\n });\n\n if (invalidatesRecovery || !initialized)\n await transaction.put(MAINTENANCE_STATE_KEY, encodeMaintenanceState(next));\n // The earliest configured retry bounds a newly actionable mutation without relying\n // on its best-effort immediate wake hint.\n await ensureTransactionAlarmBy(transaction, now + minimumAlarmDelay);\n }),\n );\n yield* failpoint.hit(\"maintenance:dirty:after\");\n yield* Ref.update(activeMutations, (active) => active + 1);\n });\n\n const endMutation = generationGate.withPermit(\n Ref.update(activeMutations, (active) => Math.max(0, active - 1)),\n );\n\n const withMutation = <A, E, R>(\n body: Effect.Effect<A, E, R>,\n options?: { readonly invalidatesRecovery: boolean },\n ): Effect.Effect<A, E | DurableAlarmError, R> =>\n Effect.acquireUseRelease(\n generationGate.withPermit(beginMutation(options?.invalidatesRecovery ?? true)),\n () =>\n failpoint.hit(\"maintenance:mutation:armed\").pipe(\n Effect.andThen(body),\n Effect.tap(() => failpoint.hit(\"maintenance:mutation:finished\")),\n ),\n () => endMutation,\n );\n\n return ThreadMutationGate.of({\n withMutation,\n withSnapshot: (body) =>\n generationGate.withPermit(Effect.flatMap(Ref.get(activeMutations), body)),\n });\n }),\n );\n}\n\nexport type MaintenancePassFailure =\n | DurableWorkerFailure\n | DurableBindingFailure\n | DurableAlarmError\n | ThreadProjectionError;\n\n/**\n * Incremental, quiescent maintenance over a durable dirty/processed generation (issue #93).\n *\n * `pass` = generation snapshot/pre-arm → recovery → one head Attempt → generation acknowledgement:\n *\n * 1. One storage transaction reads dirty/processed and re-arms before work. A caught-up forced\n * alarm takes an O(1) path without recovery, ledger scans, or canonical-history reads.\n * 2. Recovery strictly precedes a new claim. One head Attempt advances the lane and requests\n * a safe yield after ten minutes. The whole event has a fourteen-minute cooperative timeout.\n * 3. Auxiliary dispatch and listeners belong to the event Scope. After native completion,\n * stop new external waves, join native delivery through its driver-owned Claim deadline,\n * and bound host/backfill work by explicit allowances before closing and joining Scope.\n * Ordinary auxiliary setup failures are reported after the one native opportunity.\n * 4. The final transaction acknowledges only the generation observed at pass start. A racing\n * mutation therefore remains `dirty > processed` and retains its atomically-established alarm.\n * 5. Stable external waits acknowledge and clear. Autonomous retry, indeterminate, and lease\n * recovery states leave their generation dirty and retain bounded backoff rearming.\n */\nexport class ThreadMaintenance extends Context.Service<\n ThreadMaintenance,\n {\n /** One idempotent pass; failures propagate after durably scheduling bounded recovery. */\n readonly pass: Effect.Effect<MaintenancePassReport, MaintenancePassFailure>;\n /**\n * Constructor gate: initialize/inspect only the O(1) maintenance record and ensure a dirty\n * generation has an alarm. It never scans the ledger or canonical history.\n */\n readonly ensureAlarm: Effect.Effect<void, MaintenancePassFailure>;\n /**\n * Serialize the pre-arm boundary with pass acknowledgement, advance the durable dirty\n * generation and arm the alarm in one transaction BEFORE running the caller's mutation.\n * A pass cannot acknowledge while that mutation remains in flight.\n */\n readonly withMutation: <A, E, R>(\n body: Effect.Effect<A, E, R>,\n ) => Effect.Effect<A, E | DurableAlarmError, R>;\n }\n>()(\"@effect-agent/platform-cloudflare/ThreadMaintenance\") {\n static readonly layer: Layer.Layer<\n ThreadMaintenance,\n never,\n | ThreadMutationGate\n | ThreadPublication\n | ThreadProjectionMaintenance\n | DurableAgentRuntime\n | SubmissionLedger\n | DurableAlarmService\n | ThreadMaintenanceFailpoint\n | CloudflareDurableRuntimeConfig\n | DurableObjectContext\n | SqlClient\n > = Layer.effect(ThreadMaintenance)(\n Effect.gen(function* () {\n const runtime = yield* DurableAgentRuntime;\n const ledger = yield* SubmissionLedger;\n const alarm = yield* DurableAlarmService;\n const config = yield* CloudflareDurableRuntimeConfig;\n const { ctx } = yield* DurableObjectContext;\n const failpoint = yield* ThreadMaintenanceFailpoint;\n\n const mutations = yield* ThreadMutationGate;\n const publication = yield* ThreadPublication;\n const projection = yield* ThreadProjectionMaintenance;\n const messages = yield* ThreadMessageDelivery;\n const host = yield* ThreadHostMaintenance;\n\n // A broken disposable index still needs a retry alarm and must not prevent startup.\n const projectionDeadline = projection.pendingDeadline.pipe(\n Effect.catchCauseIf(\n (cause) => !Cause.hasInterrupts(cause),\n (cause) =>\n Effect.logError(\"Thread projection deadline unavailable\", cause).pipe(\n Effect.as(Option.some(0)),\n ),\n ),\n );\n\n const pendingDeadline = Effect.gen(function* () {\n return earliestDeadline(\n earliestDeadline(yield* publication.pendingDeadline, yield* messages.pendingDeadline),\n earliestDeadline(yield* projectionDeadline, yield* host.pendingDeadline),\n );\n });\n\n const maintenancePassGate = yield* Semaphore.make(1);\n const minimumAlarmDelay = Math.max(1, Math.ceil(config.alarmBackoffBase / 2));\n\n const runTransaction = yield* makeStorageOperation;\n\n const ensureAlarm = Effect.fn(\"ThreadMaintenance.ensureAlarm\")(function* () {\n yield* failpoint.hit(\"maintenance:ensure:before\");\n const now = yield* Clock.currentTimeMillis;\n\n const retry = yield* runTransaction(\"ensure maintenance alarm\", () =>\n ctx.storage.transaction(async (transaction) => {\n const { state, initialized } = await readMaintenanceState(transaction);\n\n if (!initialized) {\n await transaction.put(MAINTENANCE_STATE_KEY, encodeMaintenanceState(state));\n }\n if (state.dirty > state.processed) {\n await ensureTransactionAlarmBy(\n transaction,\n state.retry?.generation === state.dirty\n ? Math.max(now + minimumAlarmDelay, state.retry.notBefore)\n : now + config.wakeScanInterval,\n );\n }\n\n return state.retry?.generation === state.dirty ? state.retry : undefined;\n }),\n );\n\n const deadline = yield* pendingDeadline;\n\n if (Option.isSome(deadline)) {\n yield* runTransaction(\"ensure publication alarm\", () =>\n ctx.storage.transaction((transaction) =>\n ensureTransactionAlarmBy(\n transaction,\n Math.max(\n now + minimumAlarmDelay,\n deadline.value <= now && retry !== undefined && !retry.nativeOnly\n ? Math.max(deadline.value, retry.notBefore)\n : deadline.value,\n ),\n ),\n ),\n );\n }\n yield* failpoint.hit(\"maintenance:ensure:after\");\n });\n\n const beginPass = Effect.fn(\"ThreadMaintenance.beginPass\")(function* (observed: {\n generation?: bigint;\n }) {\n yield* failpoint.hit(\"maintenance:begin:before\");\n const now = yield* Clock.currentTimeMillis;\n\n const result = yield* runTransaction(\"begin maintenance pass\", () =>\n ctx.storage.transaction(async (transaction) => {\n const { state, initialized } = await readMaintenanceState(transaction);\n\n observed.generation = state.dirty;\n if (!initialized) {\n await transaction.put(MAINTENANCE_STATE_KEY, encodeMaintenanceState(state));\n }\n const retryAt = state.retry?.generation === state.dirty ? state.retry.notBefore : 0;\n\n // Prearm before recovery or any host hook, including publication-only passes.\n // A completed pass may move this crash fallback later to its bounded retry.\n await ensureTransactionAlarmBy(transaction, now + minimumAlarmDelay);\n if (state.processed >= state.dirty || retryAt > now) {\n return {\n _tag: \"CaughtUp\" as const,\n nonterminal: state.nonterminal,\n };\n }\n\n return {\n _tag: \"Actionable\" as const,\n generation: state.dirty,\n nonterminal: state.nonterminal,\n stalls: state.retry?.generation === state.dirty ? state.retry.stalls : 0,\n };\n }),\n );\n\n yield* failpoint.hit(\"maintenance:begin:after\");\n\n return result;\n });\n\n const backoffDelay = (priorStalls: number, jitter: number) => {\n const backoff = Math.min(\n config.alarmBackoffCap,\n config.alarmBackoffBase * 2 ** Math.min(priorStalls, 30),\n );\n\n // Jitter over [backoff/2, backoff] spreads retries without exceeding the cap.\n return Math.ceil(backoff / 2 + (backoff / 2) * jitter);\n };\n\n const rearmDelay = Effect.fn(\"ThreadMaintenance.rearmDelay\")(function* (\n progressed: boolean,\n priorStalls: number,\n ) {\n return progressed ? config.alarmBackoffBase : backoffDelay(priorStalls, yield* Random.next);\n });\n\n const rearmFailure = Effect.fn(\"ThreadMaintenance.rearmFailure\")(function* (\n observed: MaintenanceObservation,\n ) {\n const { generation, nativeOnly } = observed;\n\n if (generation === undefined) return;\n yield* failpoint.hit(\"maintenance:retry:before\");\n yield* mutations.withSnapshot((active) =>\n Effect.gen(function* () {\n // A failed deadline read must not prevent committing the native retry.\n const deadline = yield* pendingDeadline.pipe(\n Effect.catchCause(() => Effect.succeed(Option.none<number>())),\n );\n\n const now = yield* Clock.currentTimeMillis;\n\n const jitter = yield* Random.next;\n\n yield* runTransaction(\"back off failed maintenance\", () =>\n ctx.storage.transaction(async (transaction) => {\n const { state } = await readMaintenanceState(transaction);\n\n const previous = state.retry?.generation === generation ? state.retry : undefined;\n\n const retry = MaintenanceRetry.make({\n generation,\n notBefore: Math.max(\n previous?.notBefore ?? 0,\n now + backoffDelay(previous?.stalls ?? 0, jitter),\n ),\n nativeOnly,\n stalls: Math.min(30, (previous?.stalls ?? 0) + 1),\n });\n\n await transaction.put(\n MAINTENANCE_STATE_KEY,\n encodeMaintenanceState(ThreadMaintenanceState.make({ ...state, retry })),\n );\n\n // Never postpone a producer that raced the failed observation. Host work\n // retains its own deadline; an early delivery skips native recovery below.\n const nativeDeadline =\n active > 0 || state.dirty !== generation\n ? now + minimumAlarmDelay\n : retry.notBefore;\n\n await transaction.setAlarm(\n Math.max(\n now + minimumAlarmDelay,\n Option.isSome(deadline) && (nativeOnly || deadline.value > now)\n ? Math.min(nativeDeadline, deadline.value)\n : nativeDeadline,\n ),\n );\n }),\n );\n }),\n );\n yield* failpoint.hit(\"maintenance:retry:after\");\n });\n\n const pass = Effect.fn(\"ThreadMaintenance.pass\")(function* (\n yieldAfter: DateTime.Utc,\n dispatchUntil: DateTime.Utc,\n observed: MaintenanceObservation,\n ): Effect.fn.Return<MaintenancePassReport, MaintenancePassFailure, Scope.Scope> {\n const annotate = (report: MaintenancePassReport) =>\n Effect.annotateCurrentSpan({\n phase: report.phase,\n recovered: report.recovered,\n settled: report.settled,\n nonterminal: report.nonterminal,\n alarm: report.alarm,\n }).pipe(Effect.as(report));\n\n const started = yield* mutations.withSnapshot((activeAtStart) =>\n Effect.gen(function* () {\n const generation = yield* beginPass(observed);\n\n if (generation._tag === \"Actionable\" && activeAtStart === 0) {\n // The gate excludes a producer starting between the snapshot and certification.\n yield* publication.prepareGeneration(generation.generation);\n }\n\n return { ...generation, activeAtStart };\n }),\n );\n\n // This scope owns auxiliary dispatch and listeners, independently of source completion.\n // Close it before acknowledgement, including on failure or event interruption.\n const auxiliaryScope = yield* Effect.acquireRelease(Scope.make(\"parallel\"), (scope, exit) =>\n Scope.close(scope, exit),\n );\n\n const sourceFinished = yield* Deferred.make<void>();\n const finished = Deferred.await(sourceFinished);\n\n // Fork setup too: an ordinary auxiliary setup failure is reported after native work,\n // rather than gating its opportunity. Event interruption still closes every fiber.\n const deliveryFiber = yield* Effect.forkIn(\n Scope.provide(auxiliaryScope)(messages.drainUntil(finished, dispatchUntil)),\n auxiliaryScope,\n );\n\n const hostFiber = yield* Effect.forkIn(\n Scope.provide(auxiliaryScope)(\n Effect.gen(function* () {\n yield* Schema.decodeEffect(AuxiliaryDispatchMillis)(host.dispatchTimeoutMillis).pipe(\n Effect.mapError((cause) =>\n DurableAlarmError.make({\n operation: \"host dispatch allowance\",\n message:\n \"Declare an integer whole-wave allowance between 1 and 300000 milliseconds\",\n cause,\n }),\n ),\n );\n yield* host.drainUntil(finished, dispatchUntil);\n }),\n ),\n auxiliaryScope,\n );\n\n // Backfill is one disposable wave; its timer starts beside native execution.\n const backfill = yield* Effect.forkIn(\n drainDue.pipe(\n Effect.provideService(ThreadProjectionMaintenance, projection),\n Effect.timeoutOption(config.projectionDispatchTimeoutMillis),\n ),\n auxiliaryScope,\n );\n\n const finishAuxiliary = Effect.gen(function* () {\n yield* Deferred.succeed(sourceFinished, undefined);\n\n const remaining = Math.max(\n 1,\n DateTime.toEpochMillis(dispatchUntil) - (yield* Clock.currentTimeMillis),\n );\n\n const hostJoin = yield* Effect.forkIn(\n Fiber.join(hostFiber).pipe(\n Effect.timeoutOption(Math.min(host.dispatchTimeoutMillis, remaining)),\n Effect.tap((result) =>\n Effect.annotateCurrentSpan({ \"host.timedOut\": Option.isNone(result) }),\n ),\n ),\n auxiliaryScope,\n );\n\n // Native transport uses the driver's actual Claim deadline, through Retry persistence.\n // Only our host/backfill timers suppress their own interruption. Earlier failed exits,\n // including pure self-interruption, remain failures while siblings are still blocked.\n yield* Fiber.joinAll([deliveryFiber, hostJoin, backfill]);\n yield* Scope.close(auxiliaryScope, Exit.void);\n });\n\n const deadline = yield* publication.pendingDeadline;\n\n if (\n started._tag === \"Actionable\" ||\n (Option.isSome(deadline) && deadline.value <= (yield* Clock.currentTimeMillis))\n ) {\n yield* publication.drain;\n }\n const pending = yield* publication.pendingDeadline;\n\n if (started._tag === \"CaughtUp\" || Option.isSome(pending)) {\n yield* finishAuxiliary;\n yield* failpoint.hit(\"maintenance:finish:before\");\n\n const disposition = yield* mutations.withSnapshot((active) =>\n Effect.gen(function* () {\n // Re-read under the producer gate: a concurrent append/host mutation cannot be\n // cleared using a stale empty deadline. Dirty generations bound all producer races.\n const latest = yield* pendingDeadline;\n const now = yield* Clock.currentTimeMillis;\n\n return yield* runTransaction(\"finish publication pass\", () =>\n ctx.storage.transaction(async (transaction) => {\n const { state } = await readMaintenanceState(transaction);\n\n const nativeDeadline =\n active > 0 || state.dirty > state.processed\n ? state.retry?.generation === state.dirty && active === 0\n ? Math.max(now + minimumAlarmDelay, state.retry.notBefore)\n : now + config.wakeScanInterval\n : Infinity;\n\n const next = Option.isSome(latest)\n ? Math.min(nativeDeadline, latest.value)\n : nativeDeadline;\n\n if (Number.isFinite(next)) {\n await transaction.setAlarm(Math.max(now + minimumAlarmDelay, next));\n\n return \"rearmed\" as const;\n }\n await transaction.deleteAlarm();\n\n return \"cleared\" as const;\n }),\n );\n }),\n );\n\n yield* failpoint.hit(\"maintenance:finish:after\");\n\n return yield* annotate(\n MaintenancePassReport.make({\n phase: \"caught-up\",\n recovered: 0,\n settled: 0,\n nonterminal: started.nonterminal,\n alarm: disposition,\n }),\n );\n }\n // Step 2 — reconciliation strictly precedes new work in this pass (exit gate).\n observed.nativeOnly = true;\n const recovered: ReadonlyArray<RecoveryReport> = yield* runtime.runRecovery;\n const reports = new Map(recovered.map((report) => [report.submissionId, report]));\n const current = yield* Stream.runCollect(ledger.scanNonterminal);\n const heads = new Map<ThreadId, SubmissionSnapshot>();\n\n for (const row of current) {\n // Parked uncertainty keeps its settlement obligation, but later input can run.\n // Accepted aborts and every other wait remain subject to the lane's FIFO barrier.\n if (row.state === \"unknown\" && stableExternalWait(row, reports)) continue;\n if (!heads.has(row.threadId)) heads.set(row.threadId, row);\n }\n\n const eligible = [...heads.values()]\n .filter((head) => !stableExternalWait(head, reports))\n .map((head) => head.threadId)\n .sort();\n\n const selectionTime = yield* Clock.currentTimeMillis;\n\n yield* failpoint.hit(\"maintenance:select:before\");\n\n const selection = yield* runTransaction(\"select maintenance lane\", () =>\n ctx.storage.transaction(async (transaction) => {\n const { state } = await readMaintenanceState(transaction);\n\n const retries = (state.bindingRetries ?? []).filter(\n (retry) => heads.get(retry.threadId)?.submissionId === retry.submissionId,\n );\n\n const runnable = eligible.filter(\n (threadId) =>\n !retries.some(\n (retry) => retry.threadId === threadId && retry.notBefore > selectionTime,\n ),\n );\n\n const next =\n runnable.find(\n (threadId) =>\n state.lastServedThreadId === undefined || threadId > state.lastServedThreadId,\n ) ?? runnable[0];\n\n if (next !== undefined) {\n await transaction.put(\n MAINTENANCE_STATE_KEY,\n encodeMaintenanceState(\n ThreadMaintenanceState.make({ ...state, lastServedThreadId: next }),\n ),\n );\n }\n\n return { selected: next, retries };\n }),\n );\n\n yield* failpoint.hit(\"maintenance:select:after\");\n\n const selected =\n selection.selected === undefined ? undefined : heads.get(selection.selected);\n\n let retries = selection.retries;\n let bindingFailure: DurableBindingFailure | undefined;\n\n // One runnable FIFO head per event. An absent agent is a durable wait for a deployment,\n // including for children; other local lanes and host deliveries remain independently due.\n const settlement =\n selected === undefined\n ? Option.none()\n : yield* runtime.processThreadHead(selected.threadId, { yieldAfter }).pipe(\n Effect.catchTag(\"BindingUnavailable\", (failure) => {\n bindingFailure = failure;\n\n return Effect.succeed(Option.none());\n }),\n );\n\n if (selected !== undefined) {\n const previous = retries.find((retry) => retry.submissionId === selected.submissionId);\n let retry: BindingRetry | undefined;\n let reportBindingFailure = false;\n\n if (bindingFailure !== undefined) {\n const now = yield* Clock.currentTimeMillis;\n const attempts = Math.min(30, (previous?.attempts ?? 0) + 1);\n\n reportBindingFailure =\n previous === undefined || now - previous.reportedAt >= 15 * 60_000;\n retry = BindingRetry.make({\n threadId: selected.threadId,\n submissionId: selected.submissionId,\n attempts,\n notBefore: now + Math.min(60_000, 5_000 * 2 ** (attempts - 1)),\n reportedAt: reportBindingFailure ? now : (previous?.reportedAt ?? now),\n });\n }\n if (retry !== undefined || previous !== undefined) {\n // The Attempt released its Claim. Commit its binding wait (or clear) once,\n // before joining fallible auxiliary work. This local fact neither acknowledges\n // a generation nor changes the shared alarm; those require event retirement.\n yield* failpoint.hit(\"maintenance:binding-retry:before\");\n retries = yield* runTransaction(\"record submission binding retry\", () =>\n ctx.storage.transaction(async (transaction) => {\n const { state } = await readMaintenanceState(transaction);\n\n const bindingRetries = [\n ...(state.bindingRetries ?? []).filter(\n (entry) => entry.submissionId !== selected.submissionId,\n ),\n ...(retry === undefined ? [] : [retry]),\n ];\n\n await transaction.put(\n MAINTENANCE_STATE_KEY,\n encodeMaintenanceState(ThreadMaintenanceState.make({ ...state, bindingRetries })),\n );\n\n return bindingRetries;\n }),\n );\n yield* failpoint.hit(\"maintenance:binding-retry:after\");\n }\n if (bindingFailure !== undefined) {\n yield* reportBindingFailure\n ? Effect.logError(\n \"Thread awaits a current agent binding; original work remains pending\",\n Cause.fail(bindingFailure),\n )\n : Effect.logDebug(\"Thread binding retry remains pending\", Cause.fail(bindingFailure));\n }\n }\n\n observed.nativeOnly = false;\n yield* finishAuxiliary;\n const remaining = yield* Stream.runCollect(ledger.scanNonterminal);\n const waitingHeads = new Map<ThreadId, boolean>();\n\n const autonomous = remaining.some((snapshot) => {\n if (snapshot.state === \"unknown\" && stableExternalWait(snapshot, reports)) return false;\n const headWaiting = waitingHeads.get(snapshot.threadId);\n\n if (headWaiting === undefined)\n waitingHeads.set(snapshot.threadId, stableExternalWait(snapshot, reports));\n // FIFO followers cannot execute through a stable external wait. Only plain queued\n // input is dormant here; admission repairs and accepted aborts still need a pass.\n if (\n headWaiting === true &&\n snapshot.state === \"ready\" &&\n reports.get(snapshot.submissionId)?.decision._tag === \"ApplyInput\"\n )\n return false;\n\n return !stableExternalWait(snapshot, reports);\n });\n\n const progressed =\n Option.isSome(settlement) ||\n recovered.some((report) => report.disposition === \"repaired\");\n\n const now = yield* Clock.currentTimeMillis;\n const ordinaryDelay = autonomous ? yield* rearmDelay(progressed, started.stalls) : 0;\n\n const nextEligible = eligible.map(\n (threadId) =>\n retries.find((retry) => retry.submissionId === heads.get(threadId)?.submissionId)\n ?.notBefore ?? now,\n );\n\n const bindingDelay =\n nextEligible.length === 0 ? 0 : Math.max(0, Math.min(...nextEligible) - now);\n\n const delay = Math.max(ordinaryDelay, bindingDelay);\n\n yield* failpoint.hit(\"maintenance:finish:before\");\n\n const alarmDisposition = yield* mutations.withSnapshot((active) =>\n Effect.gen(function* () {\n const publicationDeadline = yield* pendingDeadline;\n\n return yield* runTransaction(\"finish maintenance pass\", () =>\n ctx.storage.transaction(async (transaction) => {\n const { state } = await readMaintenanceState(transaction);\n\n // Autonomous work and in-flight mutations intentionally leave the observed\n // generation dirty. Otherwise acknowledge only the pass-start generation.\n const processed =\n autonomous || started.activeAtStart > 0 || active > 0\n ? state.processed\n : state.processed > started.generation\n ? state.processed\n : started.generation;\n\n const next = ThreadMaintenanceState.make({\n ...Struct.omit(state, [\"retry\"]),\n processed,\n nonterminal: remaining.length,\n bindingRetries: (state.bindingRetries ?? []).filter((retry) =>\n remaining.some((row) => row.submissionId === retry.submissionId),\n ),\n ...(autonomous && !progressed\n ? {\n retry: MaintenanceRetry.make({\n generation: started.generation,\n notBefore: now + delay,\n nativeOnly: true,\n stalls: Math.min(30, started.stalls + 1),\n }),\n }\n : {}),\n });\n\n await transaction.put(MAINTENANCE_STATE_KEY, encodeMaintenanceState(next));\n if (autonomous) {\n // Replace the crash-fallback slot with this pass's bounded backoff. The target\n // is never earlier than the begin-pass fallback, so workerd does not cancel\n // this running alarm handler before its report/span can complete.\n const nativeDeadline =\n started.activeAtStart > 0 || active > 0 || state.dirty !== started.generation\n ? now + minimumAlarmDelay\n : now + delay;\n\n await transaction.setAlarm(\n Option.isSome(publicationDeadline)\n ? Math.max(\n now + minimumAlarmDelay,\n Math.min(nativeDeadline, publicationDeadline.value),\n )\n : nativeDeadline,\n );\n\n return \"rearmed\" as const;\n }\n if (started.activeAtStart > 0 || active > 0 || next.dirty > next.processed) {\n // A mutation overlapped this pass's observation window or raced\n // acknowledgement. It stays dirty and its pre-armed bounded alarm survives;\n // unseen effects are never acknowledged. Do not accelerate that future alarm\n // from inside the current handler: workerd cancels a running handler when it\n // writes an earlier slot.\n await ensureTransactionAlarmBy(\n transaction,\n Option.isSome(publicationDeadline)\n ? Math.max(\n now + minimumAlarmDelay,\n Math.min(now + config.wakeScanInterval, publicationDeadline.value),\n )\n : now + config.wakeScanInterval,\n );\n\n return \"rearmed\" as const;\n }\n if (Option.isSome(publicationDeadline)) {\n await transaction.setAlarm(\n Math.max(now + minimumAlarmDelay, publicationDeadline.value),\n );\n\n return \"rearmed\" as const;\n }\n await transaction.deleteAlarm();\n\n return \"cleared\" as const;\n }),\n );\n }),\n );\n\n yield* failpoint.hit(\"maintenance:finish:after\");\n\n return yield* annotate(\n MaintenancePassReport.make({\n phase: \"actionable\",\n recovered: recovered.length,\n settled: Option.isSome(settlement) ? 1 : 0,\n nonterminal: remaining.length,\n alarm: alarmDisposition,\n }),\n );\n });\n\n return ThreadMaintenance.of({\n // A mid-pass immediate hint is droppable; durable dirty state decides the final alarm.\n pass: Effect.gen(function* () {\n const now = yield* Clock.currentTimeMillis;\n const yieldAfter = DateTime.makeUnsafe(now + 10 * 60_000);\n const dispatchUntil = DateTime.makeUnsafe(now + 14 * 60_000);\n const observed: MaintenanceObservation = { nativeOnly: false };\n\n return yield* alarm.withWakesDeferred(\n maintenancePassGate.withPermit(\n Effect.scoped(pass(yieldAfter, dispatchUntil, observed)).pipe(\n // Close event-owned auxiliary work and release Attempt ownership before\n // failure rearming, while still holding the pass permit.\n Effect.onErrorIf(\n () => true,\n () => rearmFailure(observed),\n ),\n ),\n ),\n );\n }).pipe(\n // Include permit waiting, recovery and acknowledgement in the event deadline.\n // Interruption releases Attempt ownership, leaving the prearmed dirty generation\n // for recovery. It never changes the logical Run duration or settles a policy failure.\n // This cooperative timer cannot preempt synchronous CPU work or stuck finalizers.\n Effect.timeoutOrElse({\n duration: \"14 minutes\",\n orElse: () =>\n DurableAlarmError.make({\n operation: \"maintenance pass deadline\",\n message:\n \"The maintenance event exceeded its 14 minute deadline; durable recovery remains pending\",\n }),\n }),\n ),\n ensureAlarm: mutations.withSnapshot(() => ensureAlarm()),\n withMutation: (body) =>\n mutations.withMutation(\n body.pipe(\n Effect.tap(() =>\n publishCommitted.pipe(Effect.provideService(ThreadPublication, publication)),\n ),\n ),\n ),\n });\n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;MAEA,OAAO;KACT;KACA,MAAM,YAAY,YAAY;KAE9B,OAAO;IACT,CAAC,CACH;GACF,CAAC,CACH;GAEA,OAAO,UAAU,IAAI,0BAA0B;GAE/C,OAAO,OAAO,SACZ,sBAAsB,KAAK;IACzB,OAAO;IACP,WAAW,UAAU;IACrB,SAAS,OAAO,OAAO,UAAU,IAAI,IAAI;IACzC,aAAa,UAAU;IACvB,OAAO;GACT,CAAC,CACH;EACF,CAAC;EAED,OAAO,kBAAkB,GAAG;GAE1B,MAAM,OAAO,IAAI,aAAa;IAC5B,MAAM,MAAM,OAAO,MAAM;IACzB,MAAM,aAAa,SAAS,WAAW,MAAM,GAAW;IACxD,MAAM,gBAAgB,SAAS,WAAW,MAAM,IAAW;IAC3D,MAAM,WAAmC,EAAE,YAAY,MAAM;IAE7D,OAAO,OAAO,MAAM,kBAClB,oBAAoB,WAClB,OAAO,OAAO,KAAK,YAAY,eAAe,QAAQ,CAAC,CAAC,CAAC,KAGvD,OAAO,gBACC,YACA,aAAa,QAAQ,CAC7B,CACF,CACF,CACF;GACF,CAAC,CAAC,CAAC,KAKD,OAAO,cAAc;IACnB,UAAU;IACV,cACE,kBAAkB,KAAK;KACrB,WAAW;KACX,SACE;IACJ,CAAC;GACL,CAAC,CACH;GACA,aAAa,UAAU,mBAAmB,YAAY,CAAC;GACvD,eAAe,SACb,UAAU,aACR,KAAK,KACH,OAAO,UACL,iBAAiB,KAAK,OAAO,eAAe,mBAAmB,WAAW,CAAC,CAC7E,CACF,CACF;EACJ,CAAC;CACH,CAAC,CACH;AACF"}
|
|
@@ -746,6 +746,13 @@ declare const encodeHostResponse: (input: AbortRecorded | ApprovalRecorded | Hos
|
|
|
746
746
|
readonly messages?: Schema.Json | undefined;
|
|
747
747
|
} | {
|
|
748
748
|
readonly _tag: "ModelResponseRecorded";
|
|
749
|
+
readonly toolOperations?: readonly {
|
|
750
|
+
readonly toolCallId: string;
|
|
751
|
+
readonly toolName: string;
|
|
752
|
+
readonly executionClass: "idempotent" | "readonly" | "uncertain";
|
|
753
|
+
readonly executionKind: "delegation" | "orchestration" | "ordinary";
|
|
754
|
+
readonly replay: string;
|
|
755
|
+
}[] | undefined;
|
|
749
756
|
readonly toolParameterRejections?: readonly {
|
|
750
757
|
readonly toolCallId: string;
|
|
751
758
|
readonly parameters: Schema.Json;
|
|
@@ -811,6 +818,8 @@ declare const encodeHostResponse: (input: AbortRecorded | ApprovalRecorded | Hos
|
|
|
811
818
|
readonly parameters: Schema.Json;
|
|
812
819
|
readonly parametersDigest: string;
|
|
813
820
|
readonly executionKind?: "delegation" | "orchestration" | "ordinary" | undefined;
|
|
821
|
+
readonly executionClass?: "idempotent" | "readonly" | "uncertain" | undefined;
|
|
822
|
+
readonly replay?: string | undefined;
|
|
814
823
|
} | {
|
|
815
824
|
readonly _tag: "ToolCallSettled";
|
|
816
825
|
readonly toolSelection?: {
|
|
@@ -881,6 +890,7 @@ declare const encodeHostResponse: (input: AbortRecorded | ApprovalRecorded | Hos
|
|
|
881
890
|
readonly _tag: "RunCompleted";
|
|
882
891
|
readonly runId: string;
|
|
883
892
|
readonly output: Schema.Json;
|
|
893
|
+
readonly resultDigest?: string | undefined;
|
|
884
894
|
readonly runDisposition?: Schema.Json | undefined;
|
|
885
895
|
readonly finishReason?: "budget-exhausted" | undefined;
|
|
886
896
|
readonly exhausted?: "tokens" | "tool-calls" | "turns" | undefined;
|
|
@@ -1442,6 +1452,9 @@ declare const encodeHostResponse: (input: AbortRecorded | ApprovalRecorded | Hos
|
|
|
1442
1452
|
} | {
|
|
1443
1453
|
readonly _tag: "HostFailed";
|
|
1444
1454
|
readonly failure: {
|
|
1455
|
+
readonly _tag: "HostProtocolError";
|
|
1456
|
+
readonly message: string;
|
|
1457
|
+
} | {
|
|
1445
1458
|
readonly _tag: "LedgerError";
|
|
1446
1459
|
readonly operation: string;
|
|
1447
1460
|
readonly message: string;
|
|
@@ -1492,7 +1505,7 @@ declare const encodeHostResponse: (input: AbortRecorded | ApprovalRecorded | Hos
|
|
|
1492
1505
|
readonly cause?: Schema.Json | undefined;
|
|
1493
1506
|
} | {
|
|
1494
1507
|
readonly _tag: "DurableRuntimeFailpointError";
|
|
1495
|
-
readonly location: "abort:after-intent" | "approval:after-request-append" | "approval:after-suspend" | "checkpoint:after-save" | "checkpoint:before-save" | "claim:after-claim" | "compaction:after-canonical-append" | "compaction:before-canonical-append" | "input:after-canonical-append" | "join:after-canonical-append" | "join:after-claim" | "policy:after-reservation-append" | "policy:before-reservation-append" | "resolve:after-intent" | "run:after-duration-append" | "run:after-start-append" | "run:before-duration-append" | "run:before-start-append" | "step:after-step-append" | "subagent:after-admit" | "subagent:after-child-abort-intent" | "subagent:after-child-ready" | "subagent:after-join-append" | "subagent:after-release" | "subagent:after-release-pending" | "subagent:after-request-append" | "subagent:after-reserve" | "subagent:after-sibling-settle" | "subagent:after-start-append" | "subagent:after-suspend" | "subagent:before-join-append" | "submit:after-admit" | "submit:after-materialize" | "terminalize:after-canonical-append" | "terminalize:after-reserve" | "tools:after-prepared-append" | "tools:before-prepared-append" | "turn:after-canonical-append" | "turn:after-response-append" | "turn:after-results-append" | "update:after-canonical-append" | "update:after-delivery-insert" | "update:before-canonical-append" | "update:before-delivery-insert" | "worker:after-completion-append" | "worker:after-origin-append" | "worker:after-report-append" | "worker:after-report-delivery" | "worker:after-source-append" | "worker:after-subtree-append" | "worker:before-completion-append" | "worker:before-origin-append" | "worker:before-report-append" | "worker:before-report-delivery" | "worker:before-source-append" | "worker:before-subtree-append";
|
|
1508
|
+
readonly location: "abort:after-intent" | "approval:after-request-append" | "approval:after-suspend" | "checkpoint:after-save" | "checkpoint:before-save" | "claim:after-claim" | "compaction:after-canonical-append" | "compaction:before-canonical-append" | "input:after-canonical-append" | "join:after-canonical-append" | "join:after-claim" | "policy:after-reservation-append" | "policy:before-reservation-append" | "resolve:after-intent" | "run:after-duration-append" | "run:after-start-append" | "run:before-duration-append" | "run:before-start-append" | "step:after-step-append" | "subagent:after-admit" | "subagent:after-child-abort-intent" | "subagent:after-child-ready" | "subagent:after-join-append" | "subagent:after-release" | "subagent:after-release-pending" | "subagent:after-request-append" | "subagent:after-reserve" | "subagent:after-sibling-settle" | "subagent:after-start-append" | "subagent:after-suspend" | "subagent:before-join-append" | "submit:after-admit" | "submit:after-materialize" | "terminalize:after-canonical-append" | "terminalize:after-reserve" | "tools:after-prepared-append" | "tools:after-unavailable-append" | "tools:before-prepared-append" | "tools:before-unavailable-append" | "turn:after-canonical-append" | "turn:after-response-append" | "turn:after-results-append" | "update:after-canonical-append" | "update:after-delivery-insert" | "update:before-canonical-append" | "update:before-delivery-insert" | "worker:after-completion-append" | "worker:after-origin-append" | "worker:after-report-append" | "worker:after-report-delivery" | "worker:after-source-append" | "worker:after-subtree-append" | "worker:before-completion-append" | "worker:before-origin-append" | "worker:before-report-append" | "worker:before-report-delivery" | "worker:before-source-append" | "worker:before-subtree-append";
|
|
1496
1509
|
} | {
|
|
1497
1510
|
readonly _tag: "SettlementConflict";
|
|
1498
1511
|
readonly submissionId: string;
|
|
@@ -1501,19 +1514,6 @@ declare const encodeHostResponse: (input: AbortRecorded | ApprovalRecorded | Hos
|
|
|
1501
1514
|
readonly _tag: "JoinedToHost";
|
|
1502
1515
|
readonly submissionId: string;
|
|
1503
1516
|
readonly hostSubmissionId: string;
|
|
1504
|
-
} | {
|
|
1505
|
-
readonly _tag: "DurableAlarmError";
|
|
1506
|
-
readonly operation: string;
|
|
1507
|
-
readonly message: string;
|
|
1508
|
-
readonly cause?: Schema.Json | undefined;
|
|
1509
|
-
} | {
|
|
1510
|
-
readonly _tag: "AdmissionLimitExceeded";
|
|
1511
|
-
readonly limit: "database-bytes" | "input-bytes" | "queue-depth";
|
|
1512
|
-
readonly actual: number;
|
|
1513
|
-
readonly maximum: number;
|
|
1514
|
-
} | {
|
|
1515
|
-
readonly _tag: "HostProtocolError";
|
|
1516
|
-
readonly message: string;
|
|
1517
1517
|
} | {
|
|
1518
1518
|
readonly _tag: "ApprovalConflict";
|
|
1519
1519
|
readonly submissionId: string;
|
|
@@ -1523,6 +1523,16 @@ declare const encodeHostResponse: (input: AbortRecorded | ApprovalRecorded | Hos
|
|
|
1523
1523
|
readonly _tag: "UnknownResolutionConflict";
|
|
1524
1524
|
readonly submissionId: string;
|
|
1525
1525
|
readonly toolCallId: string;
|
|
1526
|
+
} | {
|
|
1527
|
+
readonly _tag: "AdmissionLimitExceeded";
|
|
1528
|
+
readonly limit: "database-bytes" | "input-bytes" | "queue-depth";
|
|
1529
|
+
readonly actual: number;
|
|
1530
|
+
readonly maximum: number;
|
|
1531
|
+
} | {
|
|
1532
|
+
readonly _tag: "DurableAlarmError";
|
|
1533
|
+
readonly operation: string;
|
|
1534
|
+
readonly message: string;
|
|
1535
|
+
readonly cause?: Schema.Json | undefined;
|
|
1526
1536
|
};
|
|
1527
1537
|
}, Schema.SchemaError, never>;
|
|
1528
1538
|
declare const decodeHostResponse: (input: unknown, options?: import("effect/SchemaAST").ParseOptions) => Effect.Effect<AbortRecorded | ApprovalRecorded | HostFailed | ObservedPage | ProgressCancelled | ProgressObserved | SettlementReached | SubmissionStatusResponse | SubmitSucceeded | UnknownResolutionRecorded, Schema.SchemaError, never>;
|
|
@@ -309,6 +309,19 @@ declare const encodeAdminResponse: (input: AdminFailed | ExplainedRecovery | Obl
|
|
|
309
309
|
readonly toolName: string;
|
|
310
310
|
readonly turn: number;
|
|
311
311
|
}[];
|
|
312
|
+
readonly pendingOperations: readonly {
|
|
313
|
+
readonly _tag: "ToolCallPrepared";
|
|
314
|
+
readonly runId: string;
|
|
315
|
+
readonly turnId: string;
|
|
316
|
+
readonly turn: number;
|
|
317
|
+
readonly toolCallId: string;
|
|
318
|
+
readonly toolName: string;
|
|
319
|
+
readonly parameters: Schema.Json;
|
|
320
|
+
readonly parametersDigest: string;
|
|
321
|
+
readonly executionKind?: "delegation" | "orchestration" | "ordinary" | undefined;
|
|
322
|
+
readonly executionClass?: "idempotent" | "readonly" | "uncertain" | undefined;
|
|
323
|
+
readonly replay?: string | undefined;
|
|
324
|
+
}[];
|
|
312
325
|
readonly openDelegationCalls: readonly {
|
|
313
326
|
readonly toolCallId: string;
|
|
314
327
|
readonly toolName: string;
|
|
@@ -696,18 +709,11 @@ declare const encodeAdminResponse: (input: AdminFailed | ExplainedRecovery | Obl
|
|
|
696
709
|
readonly operation: string;
|
|
697
710
|
readonly message: string;
|
|
698
711
|
readonly cause?: Schema.Json | undefined;
|
|
699
|
-
} | {
|
|
700
|
-
readonly _tag: "HostProtocolError";
|
|
701
|
-
readonly message: string;
|
|
702
712
|
} | {
|
|
703
713
|
readonly _tag: "LedgerError";
|
|
704
714
|
readonly operation: string;
|
|
705
715
|
readonly message: string;
|
|
706
716
|
readonly cause?: Schema.Json | undefined;
|
|
707
|
-
} | {
|
|
708
|
-
readonly _tag: "AdmissionPolicyError";
|
|
709
|
-
readonly reason: "occupied" | "refused" | "unavailable";
|
|
710
|
-
readonly code: string;
|
|
711
717
|
} | {
|
|
712
718
|
readonly _tag: "AppendConflict";
|
|
713
719
|
readonly threadId: string;
|
|
@@ -715,13 +721,6 @@ declare const encodeAdminResponse: (input: AdminFailed | ExplainedRecovery | Obl
|
|
|
715
721
|
readonly reason: "batch-digest" | "record-identity" | "tail";
|
|
716
722
|
readonly actualTailSequence?: number | undefined;
|
|
717
723
|
readonly actualTailDigest?: string | undefined;
|
|
718
|
-
} | {
|
|
719
|
-
readonly _tag: "DigestError";
|
|
720
|
-
readonly message: string;
|
|
721
|
-
readonly cause?: Schema.Json | undefined;
|
|
722
|
-
} | {
|
|
723
|
-
readonly _tag: "DurableRuntimeFailpointError";
|
|
724
|
-
readonly location: "abort:after-intent" | "approval:after-request-append" | "approval:after-suspend" | "checkpoint:after-save" | "checkpoint:before-save" | "claim:after-claim" | "compaction:after-canonical-append" | "compaction:before-canonical-append" | "input:after-canonical-append" | "join:after-canonical-append" | "join:after-claim" | "policy:after-reservation-append" | "policy:before-reservation-append" | "resolve:after-intent" | "run:after-duration-append" | "run:after-start-append" | "run:before-duration-append" | "run:before-start-append" | "step:after-step-append" | "subagent:after-admit" | "subagent:after-child-abort-intent" | "subagent:after-child-ready" | "subagent:after-join-append" | "subagent:after-release" | "subagent:after-release-pending" | "subagent:after-request-append" | "subagent:after-reserve" | "subagent:after-sibling-settle" | "subagent:after-start-append" | "subagent:after-suspend" | "subagent:before-join-append" | "submit:after-admit" | "submit:after-materialize" | "terminalize:after-canonical-append" | "terminalize:after-reserve" | "tools:after-prepared-append" | "tools:before-prepared-append" | "turn:after-canonical-append" | "turn:after-response-append" | "turn:after-results-append" | "update:after-canonical-append" | "update:after-delivery-insert" | "update:before-canonical-append" | "update:before-delivery-insert" | "worker:after-completion-append" | "worker:after-origin-append" | "worker:after-report-append" | "worker:after-report-delivery" | "worker:after-source-append" | "worker:after-subtree-append" | "worker:before-completion-append" | "worker:before-origin-append" | "worker:before-report-append" | "worker:before-report-delivery" | "worker:before-source-append" | "worker:before-subtree-append";
|
|
725
724
|
} | {
|
|
726
725
|
readonly _tag: "FenceRejected";
|
|
727
726
|
readonly threadId: string;
|
|
@@ -741,6 +740,17 @@ declare const encodeAdminResponse: (input: AdminFailed | ExplainedRecovery | Obl
|
|
|
741
740
|
readonly reason: string;
|
|
742
741
|
readonly threadId?: string | undefined;
|
|
743
742
|
readonly submissionId?: string | undefined;
|
|
743
|
+
} | {
|
|
744
|
+
readonly _tag: "AdmissionPolicyError";
|
|
745
|
+
readonly reason: "occupied" | "refused" | "unavailable";
|
|
746
|
+
readonly code: string;
|
|
747
|
+
} | {
|
|
748
|
+
readonly _tag: "DigestError";
|
|
749
|
+
readonly message: string;
|
|
750
|
+
readonly cause?: Schema.Json | undefined;
|
|
751
|
+
} | {
|
|
752
|
+
readonly _tag: "DurableRuntimeFailpointError";
|
|
753
|
+
readonly location: "abort:after-intent" | "approval:after-request-append" | "approval:after-suspend" | "checkpoint:after-save" | "checkpoint:before-save" | "claim:after-claim" | "compaction:after-canonical-append" | "compaction:before-canonical-append" | "input:after-canonical-append" | "join:after-canonical-append" | "join:after-claim" | "policy:after-reservation-append" | "policy:before-reservation-append" | "resolve:after-intent" | "run:after-duration-append" | "run:after-start-append" | "run:before-duration-append" | "run:before-start-append" | "step:after-step-append" | "subagent:after-admit" | "subagent:after-child-abort-intent" | "subagent:after-child-ready" | "subagent:after-join-append" | "subagent:after-release" | "subagent:after-release-pending" | "subagent:after-request-append" | "subagent:after-reserve" | "subagent:after-sibling-settle" | "subagent:after-start-append" | "subagent:after-suspend" | "subagent:before-join-append" | "submit:after-admit" | "submit:after-materialize" | "terminalize:after-canonical-append" | "terminalize:after-reserve" | "tools:after-prepared-append" | "tools:after-unavailable-append" | "tools:before-prepared-append" | "tools:before-unavailable-append" | "turn:after-canonical-append" | "turn:after-response-append" | "turn:after-results-append" | "update:after-canonical-append" | "update:after-delivery-insert" | "update:before-canonical-append" | "update:before-delivery-insert" | "worker:after-completion-append" | "worker:after-origin-append" | "worker:after-report-append" | "worker:after-report-delivery" | "worker:after-source-append" | "worker:after-subtree-append" | "worker:before-completion-append" | "worker:before-origin-append" | "worker:before-report-append" | "worker:before-report-delivery" | "worker:before-source-append" | "worker:before-subtree-append";
|
|
744
754
|
} | {
|
|
745
755
|
readonly _tag: "SettlementConflict";
|
|
746
756
|
readonly submissionId: string;
|
|
@@ -753,6 +763,9 @@ declare const encodeAdminResponse: (input: AdminFailed | ExplainedRecovery | Obl
|
|
|
753
763
|
readonly _tag: "RunJournalError";
|
|
754
764
|
readonly message: string;
|
|
755
765
|
readonly cause?: Schema.Json | undefined;
|
|
766
|
+
} | {
|
|
767
|
+
readonly _tag: "HostProtocolError";
|
|
768
|
+
readonly message: string;
|
|
756
769
|
} | {
|
|
757
770
|
readonly _tag: "RetryRefused";
|
|
758
771
|
readonly submissionId: string;
|
|
@@ -811,4 +824,4 @@ interface Class<EventServices = never> {
|
|
|
811
824
|
declare const make: <ApplicationServices, ApplicationError, EventServices = never, EventLayerError = never>(applicationLayer: Layer.Layer<CloudflareDurableRuntimeServices | ApplicationServices, ApplicationError, CloudflareBootstrapServices | DurableObjectState$1.DurableObjectState | WorkerEnvironment | DurableObjectContext | ThreadObjectNamespace>, options: Options<ApplicationServices, EventServices, EventLayerError>) => Class<ApplicationServices | EventServices>;
|
|
812
825
|
//#endregion
|
|
813
826
|
export { ThreadPublicationOptions as A, portCall as C, CloudflareDurableRuntimeOptions as D, CloudflareDurableRuntimeInitializationError as E, layerConfig as M, layerHostConfig as N, CloudflareDurableRuntimeServices as O, layerInHost as P, make as S, CloudflareBootstrapServices as T, decodeAdminVerifyRequest as _, AdminVerifyRequest as a, encodeAdminResponse as b, Instance as c, RetryExecuted as d, ThreadObject_d_exports as f, decodeAdminResponse as g, decodeAdminExplainRequest as h, AdminResponse as i, layer as j, ThreadObjectPorts as k, ObligationsScanned as l, VerifiedIntegrity as m, AdminFailed as n, Class as o, ThreadRpcOperation as p, AdminFailure as r, ExplainedRecovery as s, AdminExplainRequest as t, Options as u, decodeObligationThresholds as v, submit as w, handleRpc as x, decodeRetryCommand as y };
|
|
814
|
-
//# sourceMappingURL=ThreadObject-
|
|
827
|
+
//# sourceMappingURL=ThreadObject-DW6cs99q.d.mts.map
|
package/dist/ThreadObject.d.mts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { A as ThreadPublicationOptions, C as portCall, D as CloudflareDurableRuntimeOptions, E as CloudflareDurableRuntimeInitializationError, M as layerConfig, N as layerHostConfig, O as CloudflareDurableRuntimeServices, P as layerInHost, S as make, T as CloudflareBootstrapServices, _ as decodeAdminVerifyRequest, a as AdminVerifyRequest, b as encodeAdminResponse, c as Instance, d as RetryExecuted, g as decodeAdminResponse, h as decodeAdminExplainRequest, i as AdminResponse, j as layer, k as ThreadObjectPorts, l as ObligationsScanned, m as VerifiedIntegrity, n as AdminFailed, o as Class, p as ThreadRpcOperation, r as AdminFailure, s as ExplainedRecovery, t as AdminExplainRequest, u as Options, v as decodeObligationThresholds, w as submit, x as handleRpc, y as decodeRetryCommand } from "./ThreadObject-
|
|
1
|
+
import { A as ThreadPublicationOptions, C as portCall, D as CloudflareDurableRuntimeOptions, E as CloudflareDurableRuntimeInitializationError, M as layerConfig, N as layerHostConfig, O as CloudflareDurableRuntimeServices, P as layerInHost, S as make, T as CloudflareBootstrapServices, _ as decodeAdminVerifyRequest, a as AdminVerifyRequest, b as encodeAdminResponse, c as Instance, d as RetryExecuted, g as decodeAdminResponse, h as decodeAdminExplainRequest, i as AdminResponse, j as layer, k as ThreadObjectPorts, l as ObligationsScanned, m as VerifiedIntegrity, n as AdminFailed, o as Class, p as ThreadRpcOperation, r as AdminFailure, s as ExplainedRecovery, t as AdminExplainRequest, u as Options, v as decodeObligationThresholds, w as submit, x as handleRpc, y as decodeRetryCommand } from "./ThreadObject-DW6cs99q.mjs";
|
|
2
2
|
export { AdminExplainRequest, AdminFailed, AdminFailure, AdminResponse, AdminVerifyRequest, type CloudflareBootstrapServices as BootstrapServices, Class, ExplainedRecovery, type CloudflareDurableRuntimeInitializationError as InitializationError, Instance, ObligationsScanned, Options, type ThreadPublicationOptions as PublicationOptions, RetryExecuted, type CloudflareDurableRuntimeOptions as RuntimeOptions, type CloudflareDurableRuntimeServices as Services, ThreadObjectPorts, ThreadRpcOperation, VerifiedIntegrity, decodeAdminExplainRequest, decodeAdminResponse, decodeAdminVerifyRequest, decodeObligationThresholds, decodeRetryCommand, encodeAdminResponse, handleRpc, layer, layerConfig, layerHostConfig, layerInHost, make, portCall, submit };
|
package/dist/index.d.mts
CHANGED
|
@@ -8,6 +8,6 @@ import { t as CloudflareMemory_d_exports } from "./CloudflareMemory.mjs";
|
|
|
8
8
|
import { t as CloudflareScheduling_d_exports } from "./CloudflareScheduling.mjs";
|
|
9
9
|
import { t as CloudflareSubscriptions_d_exports } from "./CloudflareSubscriptions.mjs";
|
|
10
10
|
import { t as CloudflareThreadClient_d_exports } from "./CloudflareThreadClient.mjs";
|
|
11
|
-
import { f as ThreadObject_d_exports } from "./ThreadObject-
|
|
11
|
+
import { f as ThreadObject_d_exports } from "./ThreadObject-DW6cs99q.mjs";
|
|
12
12
|
import { t as WakeScheduler_d_exports } from "./WakeScheduler.mjs";
|
|
13
13
|
export { Alarm_d_exports as Alarm, CloudflareAiGateway_d_exports as CloudflareAiGateway, CloudflareBindings_d_exports as CloudflareBindings, CloudflareBrowser_d_exports as CloudflareBrowser, CloudflareCodeMode_d_exports as CloudflareCodeMode, CloudflareConfig_d_exports as CloudflareConfig, CloudflareMemory_d_exports as CloudflareMemory, CloudflareScheduling_d_exports as CloudflareScheduling, CloudflareSubscriptions_d_exports as CloudflareSubscriptions, CloudflareThreadClient_d_exports as CloudflareThreadClient, ThreadObject_d_exports as ThreadObject, WakeScheduler_d_exports as WakeScheduler };
|
package/package.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"name":"@effect-agent/platform-cloudflare","version":"0.1.0-beta.
|
|
1
|
+
{"name":"@effect-agent/platform-cloudflare","version":"0.1.0-beta.99","dependencies":{"@effect-agent/storage-cloudflare":"0.1.0-beta.99","@effect/platform-browser":"4.0.0-rc.115","@effect/sql-sqlite-do":"4.0.0-rc.115","effect-agent":"0.1.0-beta.99"},"devDependencies":{"@cloudflare/puppeteer":"1.1.0","@cloudflare/vitest-pool-workers":"0.21.3","@cloudflare/workers-types":"5.20260825.1","@effect-agent/testing":"0.1.0-beta.99","@effect/platform-node":"4.0.0-rc.115","@effect/sql-d1":"4.0.0-rc.115","@effect/vitest":"4.0.0-rc.115","effect":"4.0.0-rc.115","effect-cf":"0.44.1","esbuild":"0.28.1","miniflare":"5.20260811.1-alpha","typescript":"7.0.2","vite-plus":"0.3.0","vitest":"4.1.11"},"peerDependencies":{"@cloudflare/puppeteer":"^1.1.0","effect":"^4.0.0-rc.115","effect-cf":"^0.44.1"},"exports":{".":{"types":"./dist/index.d.mts","default":"./dist/index.mjs"},"./alarm":{"types":"./dist/Alarm.d.mts","default":"./dist/Alarm.mjs"},"./browser-rest-capture":{"types":"./dist/BrowserRestCapture.d.mts","default":"./dist/BrowserRestCapture.mjs"},"./browser-rest-crawl":{"types":"./dist/BrowserRestCrawl.d.mts","default":"./dist/BrowserRestCrawl.mjs"},"./cloudflare-bindings":{"types":"./dist/CloudflareBindings.d.mts","default":"./dist/CloudflareBindings.mjs"},"./cloudflare-browser":{"types":"./dist/CloudflareBrowser.d.mts","default":"./dist/CloudflareBrowser.mjs"},"./cloudflare-code-mode":{"types":"./dist/CloudflareCodeMode.d.mts","default":"./dist/CloudflareCodeMode.mjs"},"./cloudflare-config":{"types":"./dist/CloudflareConfig.d.mts","default":"./dist/CloudflareConfig.mjs"},"./cloudflare-memory":{"types":"./dist/CloudflareMemory.d.mts","default":"./dist/CloudflareMemory.mjs"},"./cloudflare-scheduling":{"types":"./dist/CloudflareScheduling.d.mts","default":"./dist/CloudflareScheduling.mjs"},"./cloudflare-subscriptions":{"types":"./dist/CloudflareSubscriptions.d.mts","default":"./dist/CloudflareSubscriptions.mjs"},"./cloudflare-thread-client":{"types":"./dist/CloudflareThreadClient.d.mts","default":"./dist/CloudflareThreadClient.mjs"},"./interactive-browser":{"types":"./dist/InteractiveBrowser.d.mts","default":"./dist/InteractiveBrowser.mjs"},"./protected-browser":{"types":"./dist/ProtectedBrowser.d.mts","default":"./dist/ProtectedBrowser.mjs"},"./thread-object":{"types":"./dist/ThreadObject.d.mts","default":"./dist/ThreadObject.mjs"},"./wake-scheduler":{"types":"./dist/WakeScheduler.d.mts","default":"./dist/WakeScheduler.mjs"},"./cloudflare-ai-gateway":{"types":"./dist/CloudflareAiGateway.d.mts","default":"./dist/CloudflareAiGateway.mjs"}},"description":"Cloudflare Layer assembly for Effect Agent: Durable Objects and Browser Run adapters.","license":"MIT","repository":{"type":"git","url":"git+https://github.com/danieljvdm/effect-agent.git","directory":"packages/platform-cloudflare"},"files":["dist","src"],"type":"module","sideEffects":[],"publishConfig":{"access":"public"},"scripts":{"build":"vp pack","check":"tsc --noEmit -p tsconfig.json"},"peerDependenciesMeta":{"@cloudflare/puppeteer":{"optional":true}}}
|
package/src/Alarm.ts
CHANGED
|
@@ -417,7 +417,8 @@ const stableExternalWait = (
|
|
|
417
417
|
snapshot: SubmissionSnapshot,
|
|
418
418
|
reports: ReadonlyMap<string, RecoveryReport>,
|
|
419
419
|
): boolean => {
|
|
420
|
-
const
|
|
420
|
+
const report = reports.get(snapshot.submissionId);
|
|
421
|
+
const decision = report?.decision._tag;
|
|
421
422
|
|
|
422
423
|
// An accepted abort still owes cleanup/settlement even if its claim was deferred this pass.
|
|
423
424
|
if (decision === "SettleAborted") return false;
|
|
@@ -426,7 +427,7 @@ const stableExternalWait = (
|
|
|
426
427
|
case "joined":
|
|
427
428
|
return true;
|
|
428
429
|
case "unknown":
|
|
429
|
-
return
|
|
430
|
+
return report?.disposition === "unknown";
|
|
430
431
|
case "admitted":
|
|
431
432
|
return reports.get(snapshot.submissionId)?.decision._tag === "AwaitParentEstablishment";
|
|
432
433
|
case "input-applied":
|
|
@@ -948,6 +949,9 @@ export class ThreadMaintenance extends Context.Service<
|
|
|
948
949
|
const heads = new Map<ThreadId, SubmissionSnapshot>();
|
|
949
950
|
|
|
950
951
|
for (const row of current) {
|
|
952
|
+
// Parked uncertainty keeps its settlement obligation, but later input can run.
|
|
953
|
+
// Accepted aborts and every other wait remain subject to the lane's FIFO barrier.
|
|
954
|
+
if (row.state === "unknown" && stableExternalWait(row, reports)) continue;
|
|
951
955
|
if (!heads.has(row.threadId)) heads.set(row.threadId, row);
|
|
952
956
|
}
|
|
953
957
|
|
|
@@ -1002,13 +1006,13 @@ export class ThreadMaintenance extends Context.Service<
|
|
|
1002
1006
|
let retries = selection.retries;
|
|
1003
1007
|
let bindingFailure: DurableBindingFailure | undefined;
|
|
1004
1008
|
|
|
1005
|
-
// One FIFO head per event. An
|
|
1009
|
+
// One runnable FIFO head per event. An absent agent is a durable wait for a deployment,
|
|
1006
1010
|
// including for children; other local lanes and host deliveries remain independently due.
|
|
1007
1011
|
const settlement =
|
|
1008
1012
|
selected === undefined
|
|
1009
1013
|
? Option.none()
|
|
1010
1014
|
: yield* runtime.processThreadHead(selected.threadId, { yieldAfter }).pipe(
|
|
1011
|
-
Effect.catchTag(
|
|
1015
|
+
Effect.catchTag("BindingUnavailable", (failure) => {
|
|
1012
1016
|
bindingFailure = failure;
|
|
1013
1017
|
|
|
1014
1018
|
return Effect.succeed(Option.none());
|
|
@@ -1063,7 +1067,7 @@ export class ThreadMaintenance extends Context.Service<
|
|
|
1063
1067
|
if (bindingFailure !== undefined) {
|
|
1064
1068
|
yield* reportBindingFailure
|
|
1065
1069
|
? Effect.logError(
|
|
1066
|
-
"Thread awaits a
|
|
1070
|
+
"Thread awaits a current agent binding; original work remains pending",
|
|
1067
1071
|
Cause.fail(bindingFailure),
|
|
1068
1072
|
)
|
|
1069
1073
|
: Effect.logDebug("Thread binding retry remains pending", Cause.fail(bindingFailure));
|
|
@@ -1076,6 +1080,7 @@ export class ThreadMaintenance extends Context.Service<
|
|
|
1076
1080
|
const waitingHeads = new Map<ThreadId, boolean>();
|
|
1077
1081
|
|
|
1078
1082
|
const autonomous = remaining.some((snapshot) => {
|
|
1083
|
+
if (snapshot.state === "unknown" && stableExternalWait(snapshot, reports)) return false;
|
|
1079
1084
|
const headWaiting = waitingHeads.get(snapshot.threadId);
|
|
1080
1085
|
|
|
1081
1086
|
if (headWaiting === undefined)
|