@nimbus-sh/fabric 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (103) hide show
  1. package/README.md +84 -55
  2. package/dist/bindings.js +5 -5
  3. package/dist/budgets.d.ts +132 -0
  4. package/dist/budgets.d.ts.map +1 -0
  5. package/dist/budgets.js +248 -0
  6. package/dist/composition.d.ts +87 -0
  7. package/dist/composition.d.ts.map +1 -0
  8. package/dist/composition.js +76 -0
  9. package/dist/connections.d.ts +81 -0
  10. package/dist/connections.d.ts.map +1 -0
  11. package/dist/connections.js +114 -0
  12. package/dist/derived.d.ts +65 -0
  13. package/dist/derived.d.ts.map +1 -0
  14. package/dist/derived.js +95 -0
  15. package/dist/do-calls.d.ts +94 -0
  16. package/dist/do-calls.d.ts.map +1 -0
  17. package/dist/do-calls.js +111 -0
  18. package/dist/facet-pool.d.ts +90 -0
  19. package/dist/facet-pool.d.ts.map +1 -0
  20. package/dist/facet-pool.js +113 -0
  21. package/dist/{fanout-pool.d.ts → fanout.d.ts} +20 -20
  22. package/dist/fanout.d.ts.map +1 -0
  23. package/dist/{fanout-pool.js → fanout.js} +20 -20
  24. package/dist/{launch-journal.d.ts → fenced-work.d.ts} +25 -13
  25. package/dist/fenced-work.d.ts.map +1 -0
  26. package/dist/{launch-journal.js → fenced-work.js} +47 -13
  27. package/dist/generation.d.ts +69 -0
  28. package/dist/generation.d.ts.map +1 -0
  29. package/dist/generation.js +118 -0
  30. package/dist/{facet-image-store.d.ts → image-store.d.ts} +8 -8
  31. package/dist/image-store.d.ts.map +1 -0
  32. package/dist/{facet-image-store.js → image-store.js} +4 -4
  33. package/dist/index.d.ts +16 -8
  34. package/dist/index.d.ts.map +1 -1
  35. package/dist/index.js +16 -8
  36. package/dist/{loader-pool.d.ts → isolate-pool.d.ts} +19 -19
  37. package/dist/isolate-pool.d.ts.map +1 -0
  38. package/dist/{loader-pool.js → isolate-pool.js} +20 -20
  39. package/dist/journal.d.ts +111 -0
  40. package/dist/journal.d.ts.map +1 -0
  41. package/dist/journal.js +177 -0
  42. package/dist/outbox.d.ts +249 -0
  43. package/dist/outbox.d.ts.map +1 -0
  44. package/dist/outbox.js +355 -0
  45. package/dist/process-fabric.d.ts +2 -14
  46. package/dist/process-fabric.d.ts.map +1 -1
  47. package/dist/process-fabric.js +6 -15
  48. package/dist/process-host.d.ts +1 -1
  49. package/dist/process-host.d.ts.map +1 -1
  50. package/dist/process-host.js +10 -9
  51. package/dist/sealed.d.ts +78 -0
  52. package/dist/sealed.d.ts.map +1 -0
  53. package/dist/sealed.js +145 -0
  54. package/dist/timers.d.ts +138 -0
  55. package/dist/timers.d.ts.map +1 -0
  56. package/dist/timers.js +231 -0
  57. package/dist/{launch-pacer.d.ts → turn-budget.d.ts} +19 -21
  58. package/dist/turn-budget.d.ts.map +1 -0
  59. package/dist/{launch-pacer.js → turn-budget.js} +22 -11
  60. package/dist/workerd-facet-host.d.ts +28 -67
  61. package/dist/workerd-facet-host.d.ts.map +1 -1
  62. package/dist/workerd-facet-host.js +49 -171
  63. package/examples/agent-core-adapter.ts +191 -0
  64. package/package.json +4 -2
  65. package/src/bindings.ts +6 -6
  66. package/src/budgets.ts +308 -0
  67. package/src/composition.ts +127 -0
  68. package/src/connections.ts +140 -0
  69. package/src/derived.ts +135 -0
  70. package/src/do-calls.ts +156 -0
  71. package/src/facet-pool.ts +157 -0
  72. package/src/{fanout-pool.ts → fanout.ts} +35 -35
  73. package/src/{launch-journal.ts → fenced-work.ts} +58 -22
  74. package/src/generation.ts +144 -0
  75. package/src/{facet-image-store.ts → image-store.ts} +9 -9
  76. package/src/index.ts +16 -8
  77. package/src/{loader-pool.ts → isolate-pool.ts} +34 -34
  78. package/src/journal.ts +242 -0
  79. package/src/node-async-hooks.d.ts +14 -0
  80. package/src/outbox.ts +520 -0
  81. package/src/process-fabric.ts +6 -33
  82. package/src/process-host.ts +10 -15
  83. package/src/sealed.ts +150 -0
  84. package/src/timers.ts +294 -0
  85. package/src/{launch-pacer.ts → turn-budget.ts} +30 -24
  86. package/src/workerd-facet-host.ts +67 -193
  87. package/dist/alarms.d.ts +0 -134
  88. package/dist/alarms.d.ts.map +0 -1
  89. package/dist/alarms.js +0 -214
  90. package/dist/ctx-exports.d.ts +0 -47
  91. package/dist/ctx-exports.d.ts.map +0 -1
  92. package/dist/ctx-exports.js +0 -54
  93. package/dist/facet-image-store.d.ts.map +0 -1
  94. package/dist/fanout-pool.d.ts.map +0 -1
  95. package/dist/launch-journal.d.ts.map +0 -1
  96. package/dist/launch-pacer.d.ts.map +0 -1
  97. package/dist/loader-ledger.d.ts +0 -57
  98. package/dist/loader-ledger.d.ts.map +0 -1
  99. package/dist/loader-ledger.js +0 -91
  100. package/dist/loader-pool.d.ts.map +0 -1
  101. package/src/alarms.ts +0 -275
  102. package/src/ctx-exports.ts +0 -77
  103. package/src/loader-ledger.ts +0 -112
package/src/sealed.ts ADDED
@@ -0,0 +1,150 @@
1
+ /**
2
+ * sealed.ts — prototype-chain RPC sealing, and the versioned surface
3
+ * constants that make it safe to run over the Agents SDK.
4
+ *
5
+ * Cloudflare resolves `stub.foo(...)` on the receiver's PROTOTYPE CHAIN.
6
+ * Proteus verified the three consequences against real workerd
7
+ * (`cf-backend/src/rpc-surface.ts:4-22`, catalog `rpc.prototype_chain`,
8
+ * proven-by-probe): TypeScript `private` is erased, so private methods ARE
9
+ * callable over RPC; superclass methods are reachable too — inherited
10
+ * `Agent.sql` hands any stub-holder arbitrary SQL against the receiver's
11
+ * storage, and `Agent` + `Server` alone contribute hundreds of reachable
12
+ * names; and OWN instance properties are NOT reachable — workerd rejects
13
+ * them exactly as it rejects a missing name, including when the own
14
+ * property shadows a prototype method.
15
+ *
16
+ * That third consequence is the primitive: {@link sealRpcSurface} copies
17
+ * every reachable member that is NOT on the declared surface down onto the
18
+ * instance as a non-enumerable own property. In-process behaviour is
19
+ * unchanged — `this.x(...)` finds the same function object, `super.x()`
20
+ * still reaches the prototype, accessors stay accessors. From outside, the
21
+ * name has ceased to exist. Call it as the LAST statement of the
22
+ * constructor, after every base class installed its wrappers.
23
+ *
24
+ * The surface constants are the part that breaks whenever the SDK moves,
25
+ * which is why fabric owns them: Proteus reverse-engineered its facet
26
+ * surface from `agents/dist` by hand, and a fabric test diffs these
27
+ * constants against the INSTALLED packages so drift is caught by CI, not by
28
+ * a leak. Verified against agents@0.20.1 and partyserver@0.5.10
29
+ * (2026-08-19); an SDK upgrade that changes the cross-stub set fails that
30
+ * test and demands re-derivation, which is the intended failure.
31
+ */
32
+
33
+ /**
34
+ * Every name RPC can resolve on `target`: own property names of every
35
+ * prototype up to (and excluding) Object.prototype, minus `constructor` and
36
+ * minus anything `target` already carries as an own property — own
37
+ * properties are not RPC-reachable, so they need no seal. The single
38
+ * definition {@link sealRpcSurface} and its tests both work from.
39
+ */
40
+ export function rpcReachableNames(target: object): string[] {
41
+ const names = new Set<string>();
42
+ const own = new Set(Object.getOwnPropertyNames(target));
43
+ let proto = Object.getPrototypeOf(target) as object | null;
44
+ while (proto !== null && proto !== Object.prototype) {
45
+ for (const name of Object.getOwnPropertyNames(proto)) {
46
+ if (name === 'constructor' || own.has(name)) continue;
47
+ names.add(name);
48
+ }
49
+ proto = Object.getPrototypeOf(proto) as object | null;
50
+ }
51
+ return [...names].sort();
52
+ }
53
+
54
+ function inheritedDescriptor(target: object, name: string): PropertyDescriptor | undefined {
55
+ let proto = Object.getPrototypeOf(target) as object | null;
56
+ while (proto !== null && proto !== Object.prototype) {
57
+ const descriptor = Object.getOwnPropertyDescriptor(proto, name);
58
+ if (descriptor) return descriptor;
59
+ proto = Object.getPrototypeOf(proto) as object | null;
60
+ }
61
+ return undefined;
62
+ }
63
+
64
+ /**
65
+ * Shadow every RPC-reachable member not on `surface` with a non-enumerable
66
+ * own property carrying the SAME descriptor — the same function object, the
67
+ * same accessor pair — so in-process behaviour cannot change while the name
68
+ * stops resolving over RPC. Surface names the class lacks are ignored: a
69
+ * surface is a ceiling, and the runtime already denies what does not exist.
70
+ */
71
+ export function sealRpcSurface<Instance extends object>(
72
+ instance: Instance,
73
+ surface: readonly string[],
74
+ ): void {
75
+ const allowed = new Set(surface);
76
+ for (const name of rpcReachableNames(instance)) {
77
+ if (allowed.has(name)) continue;
78
+ const descriptor = inheritedDescriptor(instance, name);
79
+ if (descriptor) Object.defineProperty(instance, name, { ...descriptor, enumerable: false });
80
+ }
81
+ }
82
+
83
+ /**
84
+ * The platform half of any surface built on partyserver's `Server` (and
85
+ * therefore the Agents SDK's `Agent`): what the RUNTIME and the SDK's own
86
+ * routing must still reach after sealing. From the consumer's verified list
87
+ * (`rpc-surface.ts:63-86`): `setName` is called by `getServerByName` before
88
+ * the stub is returned; `_initAndFetch` is `setName` plus `fetch`, so it
89
+ * exposes nothing new; the WebSocket handlers' arguments cannot cross an
90
+ * RPC boundary anyway.
91
+ */
92
+ export const PLATFORM_RPC_SURFACE = [
93
+ 'fetch',
94
+ 'setName',
95
+ '_initAndFetch',
96
+ 'alarm',
97
+ 'webSocketMessage',
98
+ 'webSocketClose',
99
+ 'webSocketError',
100
+ ] as const;
101
+
102
+ /**
103
+ * The Agents SDK's own cross-stub facet protocol at agents@0.20.1: every
104
+ * `_cf_` method the SDK calls on a receiver that is not `this`, derived
105
+ * from `agents/dist` (the SDK's ACTUAL cross-stub surface, not a prefix
106
+ * rule — Agent's prototype defines 58 `_cf_` methods and only these are
107
+ * cross-called). An Agent that hosts SDK facets must keep these; one that
108
+ * does not (Proteus's UserDO) should not carry them at all.
109
+ */
110
+ export const AGENTS_FACET_RPC_SURFACE = [
111
+ '_cf_acquireFacetKeepAlive',
112
+ '_cf_broadcastToSubAgent',
113
+ '_cf_cancelScheduleForFacet',
114
+ '_cf_checkRunFibersForFacet',
115
+ '_cf_cleanupFacetPrefix',
116
+ '_cf_closeSubAgentConnection',
117
+ '_cf_destroyDescendantFacet',
118
+ '_cf_dispatchScheduledCallback',
119
+ '_cf_getScheduleForFacet',
120
+ '_cf_handleSubAgentWebSocketClose',
121
+ '_cf_handleSubAgentWebSocketConnect',
122
+ '_cf_handleSubAgentWebSocketMessage',
123
+ '_cf_initAsFacet',
124
+ '_cf_listSchedulesForFacet',
125
+ '_cf_registerFacetRun',
126
+ '_cf_releaseFacetKeepAlive',
127
+ '_cf_scheduleEveryForFacet',
128
+ '_cf_scheduleForFacet',
129
+ '_cf_sendToSubAgentConnection',
130
+ '_cf_setSubAgentConnectionState',
131
+ '_cf_subAgentConnectionMetas',
132
+ '_cf_unregisterFacetRun',
133
+ ] as const;
134
+
135
+ /**
136
+ * Cross-stub `_cf_` members deliberately absent from EVERY surface: each
137
+ * takes a method NAME and calls it on the receiver, which would re-open
138
+ * everything sealing closes. Sealing them fail-closes the SDK features
139
+ * built on them (`parentAgent()` proxies, workflow-to-agent bridges) — the
140
+ * consumer accepts exactly that cost, and fail-closed is the right default
141
+ * for a bridge that dispatches arbitrary names. (`_cf_invokeStubMethod` is
142
+ * only ever self-called in the pinned dist, but it is prototype-defined, so
143
+ * it is named here and sealed.)
144
+ */
145
+ export const AGENTS_INVOKE_BRIDGES = [
146
+ '_cf_invokeAgentPath',
147
+ '_cf_invokeStubMethod',
148
+ '_cf_invokeSubAgent',
149
+ '_cf_invokeSubAgentPath',
150
+ ] as const;
package/src/timers.ts ADDED
@@ -0,0 +1,294 @@
1
+ /**
2
+ * timers.ts — Durable Object alarm multiplexing, persisted across
3
+ * hibernation.
4
+ *
5
+ * A Durable Object has ONE alarm, and a second `setAlarm()` silently
6
+ * overwrites the first — so every alarm-driven subsystem coordinates through
7
+ * a single reason→deadline map and one dispatcher. Reasons are plain strings
8
+ * registered by the embedder: `timers(host, ctx).schedule` arms one, and
9
+ * `timers(host, ctx).dispatch` runs the embedder-supplied handler for every
10
+ * reason whose deadline has passed.
11
+ */
12
+
13
+ import { AsyncLocalStorage } from 'node:async_hooks';
14
+ import { errorText } from '@nimbus-sh/core/_shared/error-text.js';
15
+
16
+ /**
17
+ * The storage the timer map lives in. `setAlarm` is optional because
18
+ * `wrangler dev` serves a storage without it, which is the whole reason
19
+ * scheduling degrades to a no-op instead of throwing.
20
+ */
21
+ export interface TimerStorage {
22
+ get(key: string): Promise<unknown>;
23
+ put(key: string, value: unknown): Promise<void>;
24
+ delete(key: string): Promise<boolean>;
25
+ setAlarm?(scheduledTime: number): Promise<void>;
26
+ }
27
+
28
+ /** The hosting actor's context, as the timer coordination reads it. */
29
+ export interface TimerContext {
30
+ storage: TimerStorage;
31
+ }
32
+
33
+ /**
34
+ * Multi-reason timer coordination map.
35
+ *
36
+ * JSON-serialised `Record<reason, deadlineMsEpoch>` where keys are the
37
+ * embedder's canonical reason strings (e.g. 'w9-flush', 'log-janitor'). The
38
+ * alarm() dispatcher reads this on fire, dispatches every reason whose
39
+ * deadline has passed, and re-arms `ctx.storage.setAlarm` at the earliest
40
+ * remaining deadline.
41
+ *
42
+ * Why a map (not a single nextAlarmAt + reason): two subsystems can have
43
+ * distinct deadlines. Without the map, the later setAlarm() call would
44
+ * overwrite the earlier reason silently, breaking whichever subsystem
45
+ * expected its deadline.
46
+ *
47
+ * Forward-compat: the dispatcher silently drops unknown reasons so a
48
+ * rollback from a future deploy that added new reasons doesn't leave the
49
+ * alarm stuck.
50
+ *
51
+ * The VALUE is live production DO storage ('w1_next_alarm_reasons', from the
52
+ * workstream that introduced it) and must never change — renaming a storage
53
+ * key is a migration, and orphaned rows are the least of what it breaks.
54
+ */
55
+ export const TIMER_REASONS_KEY = 'w1_next_alarm_reasons';
56
+
57
+ /**
58
+ * The host instance carrying the per-instance timer chain. The field lives on
59
+ * the embedder's DO instance so one chain serializes every timer-map
60
+ * read-modify-write for that instance (see {@link Timers.schedule}).
61
+ */
62
+ export interface TimerHost {
63
+ _timerChain?: Promise<unknown>;
64
+ }
65
+
66
+ /**
67
+ * The async context of a running dispatch's handlers. A schedule request
68
+ * made inside it lands in the dispatch's arm collection instead of the
69
+ * chain — a handler that AWAITED a chained schedule would be waiting on an
70
+ * entry queued behind the dispatch it is running inside, which is a
71
+ * deadlock. `Outbox.queue` awaits `timers.schedule`, so any handler that
72
+ * queues into an outbox reaches this. The dispatcher folds the collected
73
+ * arms into the reason map before its own re-arm.
74
+ *
75
+ * AsyncLocalStorage, not a host field, so the redirect is scoped to the
76
+ * dispatch's OWN async context: a concurrent turn that schedules while a
77
+ * handler awaits external IO takes the normal chain path and keeps its own
78
+ * turn-gated persistence. Requires the `nodejs_compat` (or `nodejs_als`)
79
+ * compatibility flag on workerd.
80
+ */
81
+ const dispatchArms = new AsyncLocalStorage<Array<{ reason: string; whenMs: number }>>();
82
+
83
+ /**
84
+ * What one timer handler may return: nothing, or a deadline this reason
85
+ * re-arms itself at. Re-arming through the return value keeps the map's
86
+ * read-modify-write inside the dispatcher, where it is serialized.
87
+ */
88
+ export type TimerHandlerResult = void | { rearmAt: number };
89
+
90
+ /**
91
+ * The platform's alarm-invocation report, forwarded to every handler: the
92
+ * platform retries a failed alarm() with backoff and abandons it after its
93
+ * retry budget, and `isRetry`/`retryCount` are the only way a handler can
94
+ * tell how close it is to that abandonment. Structurally identical to
95
+ * workers-types' AlarmInvocationInfo; declared here so the module stays
96
+ * usable without the ambient types.
97
+ */
98
+ export interface TimerAlarmInfo {
99
+ readonly isRetry: boolean;
100
+ readonly retryCount: number;
101
+ readonly scheduledTime: number;
102
+ }
103
+
104
+ /** The embedder's reasons, each with the handler that answers it. */
105
+ export type TimerHandlers = Record<
106
+ string,
107
+ (now: number, info?: TimerAlarmInfo) => TimerHandlerResult | Promise<TimerHandlerResult>
108
+ >;
109
+
110
+ /**
111
+ * One actor's timers: the reason map over its ONE platform alarm.
112
+ *
113
+ * A cheap accessor over `(host, ctx)` — the chain that serializes the map's
114
+ * read-modify-write lives on the host instance, so every `timers()` call for
115
+ * one instance coordinates through the same chain.
116
+ */
117
+ export function timers(host: TimerHost, ctx: TimerContext): Timers {
118
+ return new Timers(host, ctx);
119
+ }
120
+
121
+ export class Timers {
122
+ constructor(
123
+ private readonly host: TimerHost,
124
+ private readonly ctx: TimerContext,
125
+ ) {}
126
+
127
+ /**
128
+ * Schedule (or re-schedule) a timer reason. Coordinated via a single map in
129
+ * DO storage so multiple subsystems don't clobber each other's `setAlarm()`
130
+ * calls.
131
+ *
132
+ * Semantics:
133
+ * - Reads the existing reasons map.
134
+ * - Sets `map[reason] = whenMs` IF `whenMs` is sooner than the
135
+ * currently-pending deadline for that reason (or no entry exists).
136
+ * Later-than-pending requests are silently ignored — the existing
137
+ * alarm will fire and re-arm anyway.
138
+ * - Writes the map back and calls `ctx.storage.setAlarm(min(deadlines))`.
139
+ *
140
+ * Cost: 1 storage read + 1 storage write + 1 setAlarm per call. setAlarm
141
+ * itself is billed as 1 row written per DO pricing. At a 60s janitor
142
+ * cadence, this is ~$0.05/mo/session at scale — dwarfed by the
143
+ * hibernation duration savings.
144
+ *
145
+ * Fail-soft: any throw is swallowed with a warn. On older runtimes /
146
+ * wrangler-dev where setAlarm is unavailable, this is a no-op (the
147
+ * subsystem's in-isolate setTimeout fallback continues to work).
148
+ */
149
+ schedule(reason: string, whenMs: number): Promise<boolean> {
150
+ const { host, ctx } = this;
151
+ // From inside a dispatch handler, hand the arm to the dispatcher
152
+ // instead of the chain (see dispatchArms): the fold keeps EDF
153
+ // semantics, and the dispatch's own write and re-arm carry it. The
154
+ // setAlarm gate matches the chain path's, so both paths refuse alike
155
+ // on a runtime without alarms.
156
+ const arms = dispatchArms.getStore();
157
+ if (arms) {
158
+ if (typeof ctx?.storage?.setAlarm !== 'function') return Promise.resolve(false);
159
+ arms.push({ reason, whenMs });
160
+ return Promise.resolve(true);
161
+ }
162
+ // Serialize every read-modify-write of the reasons map through one
163
+ // per-instance chain: two schedulers firing back-to-back from one activity
164
+ // hook would otherwise interleave their get→put cycles and silently drop
165
+ // whichever reason wrote first.
166
+ const run = async (): Promise<boolean> => {
167
+ try {
168
+ const setAlarmFn = ctx?.storage?.setAlarm;
169
+ if (typeof setAlarmFn !== 'function') return false;
170
+ const existing = (await ctx.storage.get(TIMER_REASONS_KEY)) as
171
+ | Record<string, number>
172
+ | undefined;
173
+ const map: Record<string, number> = { ...(existing || {}) };
174
+ // Earliest-deadline-first: only update if new request is sooner or
175
+ // this reason has no pending entry.
176
+ if (!(reason in map) || whenMs < map[reason]) {
177
+ map[reason] = whenMs;
178
+ await ctx.storage.put(TIMER_REASONS_KEY, map);
179
+ }
180
+ const earliest = Math.min(...Object.values(map));
181
+ setAlarmFn.call(ctx.storage, earliest);
182
+ return true;
183
+ } catch (e) {
184
+ console.warn('[nimbus/W1] timers.schedule threw:', errorText(e));
185
+ return false;
186
+ }
187
+ };
188
+ const chained = (host._timerChain ?? Promise.resolve()).then(run, run);
189
+ host._timerChain = chained;
190
+ return chained;
191
+ }
192
+
193
+ /**
194
+ * Multi-reason timer dispatcher. Called from the DO's `alarm()` handler
195
+ * with the embedder's handler map.
196
+ *
197
+ * For each pending reason whose deadline has passed, run its handler.
198
+ * Handlers are awaited in place: the alarm invocation is the fresh turn a
199
+ * re-entering subsystem asked for, and it has to stay the one paying for the
200
+ * work it just released.
201
+ *
202
+ * After running fireable reasons, re-arms `ctx.storage.setAlarm` at the
203
+ * earliest remaining deadline. If no reasons remain, deletes the map key and
204
+ * does NOT call setAlarm — the DO becomes hibernation-eligible after the 10s
205
+ * idle window.
206
+ *
207
+ * Forward/back-compat: unknown reasons silently dropped. `onLegacyAlarm`
208
+ * covers an alarm that fires with no map at all — a deploy from before the
209
+ * map existed left a bare `setAlarm` behind, and the embedder decides what
210
+ * that one-time fire means (one dispatch later the map is populated by the
211
+ * next schedule call).
212
+ */
213
+ dispatch(
214
+ handlers: TimerHandlers,
215
+ onLegacyAlarm?: () => void,
216
+ alarmInfo?: TimerAlarmInfo,
217
+ ): Promise<void> {
218
+ const { host, ctx } = this;
219
+ // Same serialization as schedule: the dispatcher's read→handlers→write
220
+ // cycle must not interleave with an activity-hook schedule.
221
+ const chained = (host._timerChain ?? Promise.resolve()).then(
222
+ () => dispatchBody(ctx, handlers, onLegacyAlarm, alarmInfo),
223
+ () => dispatchBody(ctx, handlers, onLegacyAlarm, alarmInfo),
224
+ );
225
+ host._timerChain = chained;
226
+ return chained;
227
+ }
228
+ }
229
+
230
+ async function dispatchBody(
231
+ ctx: TimerContext,
232
+ handlers: TimerHandlers,
233
+ onLegacyAlarm?: () => void,
234
+ alarmInfo?: TimerAlarmInfo,
235
+ ): Promise<void> {
236
+ // Collect schedule requests made inside handler context (see
237
+ // dispatchArms) and fold them into the map below, so an in-dispatch arm
238
+ // neither deadlocks on the chain nor races the write.
239
+ const arms: Array<{ reason: string; whenMs: number }> = [];
240
+ try {
241
+ const now = Date.now();
242
+ const existing = (await ctx?.storage?.get?.(TIMER_REASONS_KEY)) as
243
+ | Record<string, number>
244
+ | undefined;
245
+ const map: Record<string, number> = { ...(existing || {}) };
246
+ const hadMap = Object.keys(map).length > 0;
247
+ if (!hadMap) {
248
+ dispatchArms.run(arms, () => onLegacyAlarm?.());
249
+ } else {
250
+ // Snapshot fireable reasons BEFORE running any of them, so a
251
+ // handler that schedules itself for the next cycle doesn't get
252
+ // immediately re-fired in the same dispatch.
253
+ const fired: string[] = [];
254
+ for (const [reason, when] of Object.entries(map)) {
255
+ if (when <= now) fired.push(reason);
256
+ }
257
+ for (const reason of fired) {
258
+ delete map[reason];
259
+ const handler = handlers[reason];
260
+ // Unknown reasons silently dropped (forward-compat).
261
+ if (!handler) continue;
262
+ try {
263
+ const result = await dispatchArms.run(arms, () => handler(now, alarmInfo));
264
+ if (result && typeof result.rearmAt === 'number') {
265
+ map[reason] = result.rearmAt;
266
+ }
267
+ } catch (e) {
268
+ console.warn(`[nimbus/W1] dispatch ${reason} threw:`, errorText(e));
269
+ }
270
+ }
271
+ }
272
+ // Fold the in-dispatch arms, earliest-deadline-first per reason.
273
+ for (const arm of arms) {
274
+ if (!(arm.reason in map) || arm.whenMs < map[arm.reason]) {
275
+ map[arm.reason] = arm.whenMs;
276
+ }
277
+ }
278
+ // Re-arm or clear.
279
+ const setAlarmFn = ctx?.storage?.setAlarm;
280
+ if (Object.keys(map).length > 0) {
281
+ await ctx.storage.put(TIMER_REASONS_KEY, map);
282
+ const earliest = Math.min(...Object.values(map));
283
+ if (typeof setAlarmFn === 'function') {
284
+ setAlarmFn.call(ctx.storage, earliest);
285
+ }
286
+ } else if (hadMap) {
287
+ try { await ctx.storage.delete(TIMER_REASONS_KEY); } catch {}
288
+ // No remaining reasons → no setAlarm call → DO becomes
289
+ // hibernation-eligible after the 10s idle window.
290
+ }
291
+ } catch (e) {
292
+ console.warn('[nimbus/W1] timers.dispatch threw:', errorText(e));
293
+ }
294
+ }
@@ -1,5 +1,5 @@
1
1
  /**
2
- * launch-pacer.ts — spreading a resident launch across Durable Object turns.
2
+ * turn-budget.ts — spreading a resident launch across Durable Object turns.
3
3
  *
4
4
  * Building a resident process is the largest single span of computation this
5
5
  * session performs: for pi it walks a 17 MB source tree through eight
@@ -33,8 +33,10 @@
33
33
  * bytes and treats its wall guard as coarse.
34
34
  */
35
35
 
36
+ import { runColdStart } from './generation.js';
37
+
36
38
  /** How a paced launch gets back onto a fresh Durable Object turn. */
37
- export interface LaunchTurnScheduler {
39
+ export interface TurnScheduler {
38
40
  /**
39
41
  * Suspend until a fresh turn is running this launch again.
40
42
  *
@@ -54,7 +56,7 @@ export interface LaunchTurnScheduler {
54
56
  * each an alarm round trip — small enough not to dominate a launch. pi's
55
57
  * 22.9 MB map crosses this about a dozen times per phase that handles it.
56
58
  */
57
- export const LAUNCH_CHUNK_MAX_BYTES = 2_000_000;
59
+ export const TURN_CHUNK_MAX_BYTES = 2_000_000;
58
60
 
59
61
  /**
60
62
  * Accounts launch progress and ends the turn when a chunk's worth has been
@@ -66,7 +68,7 @@ export const LAUNCH_CHUNK_MAX_BYTES = 2_000_000;
66
68
  * behaviour and cost. Nothing here decides WHAT the launch does, only where it
67
69
  * is allowed to stop.
68
70
  */
69
- export class LaunchPacer {
71
+ export class TurnBudget {
70
72
  /** Turn handoffs this launch has taken. Reported with the launch. */
71
73
  chunks = 0;
72
74
  /** Total work accounted, for the same report. */
@@ -84,8 +86,8 @@ export class LaunchPacer {
84
86
  * remembered to ask.
85
87
  */
86
88
  constructor(
87
- private readonly scheduler: LaunchTurnScheduler,
88
- private readonly maxChunkBytes: number = LAUNCH_CHUNK_MAX_BYTES,
89
+ private readonly scheduler: TurnScheduler,
90
+ private readonly maxChunkBytes: number = TURN_CHUNK_MAX_BYTES,
89
91
  private readonly stillWanted?: () => void,
90
92
  ) {}
91
93
 
@@ -127,33 +129,26 @@ function withResolvers(): { promise: Promise<void>; resolve: () => void } {
127
129
  return { promise, resolve };
128
130
  }
129
131
 
130
- /** What {@link LaunchTurnPump} needs from the Durable Object hosting it. */
131
- export interface LaunchTurnPumpHost {
132
+ /** What {@link PacedWork} needs from the Durable Object hosting it. */
133
+ export interface PacedWorkHost {
132
134
  /**
133
- * Arrange for {@link LaunchTurnPump.pump} to run on a fresh Durable Object
135
+ * Arrange for {@link PacedWork.pump} to run on a fresh Durable Object
134
136
  * turn.
135
137
  *
136
138
  * The embedder satisfies this with an alarm, which is the only primitive
137
139
  * that genuinely re-enters the object: a fresh turn is both a released
138
140
  * thread and a fresh CPU budget, and a launch needs each for a different
139
141
  * reason. Without it the pump degrades to a same-context timer — see
140
- * {@link LaunchTurnPump.nextTurn}.
142
+ * {@link PacedWork.nextTurn}.
141
143
  */
142
144
  requestTurn?: () => void;
143
- /**
144
- * Awaited first on every pump, before any waiter resumes. Where the
145
- * resident-launch journal's recovery sits: the pump is what an alarm calls,
146
- * and the first turn after a reset is the re-delivered alarm of a launch
147
- * the reset interrupted.
148
- */
149
- recover?: () => Promise<void>;
150
145
  }
151
146
 
152
147
  /**
153
- * The granting side of {@link LaunchTurnScheduler}: parks suspended launches
148
+ * The granting side of {@link TurnScheduler}: parks suspended launches
154
149
  * and resumes every one of them when the host grants a fresh turn.
155
150
  */
156
- export class LaunchTurnPump implements LaunchTurnScheduler {
151
+ export class PacedWork implements TurnScheduler {
157
152
  /**
158
153
  * Launches suspended between chunks, waiting for a turn of their own.
159
154
  *
@@ -166,7 +161,14 @@ export class LaunchTurnPump implements LaunchTurnScheduler {
166
161
  */
167
162
  private waiters: Array<{ resume: () => void; chunkEnded: Promise<void> }> = [];
168
163
 
169
- constructor(private readonly host: LaunchTurnPumpHost) {}
164
+ /**
165
+ * `ctx` keys the cold-start queue the pump drains first on every turn it
166
+ * grants — see {@link pump}.
167
+ */
168
+ constructor(
169
+ private readonly ctx: object,
170
+ private readonly host: PacedWorkHost,
171
+ ) {}
170
172
 
171
173
  /**
172
174
  * How a paced launch asks for a fresh turn.
@@ -198,7 +200,11 @@ export class LaunchTurnPump implements LaunchTurnScheduler {
198
200
  * the runtime may tear the context down mid-chunk.
199
201
  */
200
202
  async pump(): Promise<void> {
201
- await this.host.recover?.();
203
+ // Deferred reconciliation runs first, before any waiter resumes. Where
204
+ // the resident-launch journal's recovery sits (`onColdStart`): the pump
205
+ // is what an alarm calls, and the first turn after a reset is the
206
+ // re-delivered alarm of a launch the reset interrupted.
207
+ await runColdStart(this.ctx);
202
208
  const waiting = this.waiters;
203
209
  if (waiting.length === 0) return;
204
210
  this.waiters = [];
@@ -222,10 +228,10 @@ export class LaunchTurnPump implements LaunchTurnScheduler {
222
228
  * `git/commands.ts` carries `NIMBUS_GIT_CHECKOUT_CHUNK_ENTRIES`. Unset in
223
229
  * production, where the default applies.
224
230
  */
225
- export function launchChunkMaxBytes(env: unknown): number {
231
+ export function turnChunkMaxBytes(env: unknown): number {
226
232
  const raw = (env as { NIMBUS_LAUNCH_CHUNK_BYTES?: string } | null | undefined)
227
233
  ?.NIMBUS_LAUNCH_CHUNK_BYTES;
228
- if (!raw) return LAUNCH_CHUNK_MAX_BYTES;
234
+ if (!raw) return TURN_CHUNK_MAX_BYTES;
229
235
  const parsed = Number(raw);
230
- return Number.isFinite(parsed) && parsed > 0 ? parsed : LAUNCH_CHUNK_MAX_BYTES;
236
+ return Number.isFinite(parsed) && parsed > 0 ? parsed : TURN_CHUNK_MAX_BYTES;
231
237
  }