@effect-agent/platform-node 0.1.0-beta.9 → 0.1.0-beta.91
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/NodeDurableAgentRuntime-Cs_E9W_9.d.mts +175 -0
- package/dist/NodeDurableAgentRuntime.d.mts +2 -0
- package/dist/NodeDurableAgentRuntime.mjs +221 -0
- package/dist/NodeDurableAgentRuntime.mjs.map +1 -0
- package/dist/NodeDurableHost-CG2xAhfb.mjs +194 -0
- package/dist/NodeDurableHost-CG2xAhfb.mjs.map +1 -0
- package/dist/NodeDurableHost.d.mts +160 -0
- package/dist/NodeDurableHost.mjs +2 -0
- package/dist/NodeScheduling.d.mts +22 -0
- package/dist/NodeScheduling.mjs +59 -0
- package/dist/NodeScheduling.mjs.map +1 -0
- package/dist/NodeSubscriptions.d.mts +27 -0
- package/dist/NodeSubscriptions.mjs +74 -0
- package/dist/NodeSubscriptions.mjs.map +1 -0
- package/dist/NodeWakeScheduler-CCgm1t-G.d.mts +27 -0
- package/dist/NodeWakeScheduler.d.mts +2 -0
- package/dist/NodeWakeScheduler.mjs +65 -0
- package/dist/NodeWakeScheduler.mjs.map +1 -0
- package/dist/NodeWorkflow.d.mts +27 -0
- package/dist/NodeWorkflow.mjs +159 -0
- package/dist/NodeWorkflow.mjs.map +1 -0
- package/dist/index.d.mts +6 -214
- package/dist/index.mjs +6 -286
- package/dist/rolldown-runtime-D7D4PA-g.mjs +13 -0
- package/package.json +1 -45
- package/src/NodeDurableAgentRuntime.ts +590 -0
- package/src/NodeDurableHost.ts +358 -0
- package/src/NodeScheduling.ts +117 -0
- package/src/NodeSubscriptions.ts +158 -0
- package/src/{wake-scheduler.ts → NodeWakeScheduler.ts} +32 -17
- package/src/NodeWorkflow.ts +258 -0
- package/src/index.ts +5 -3
- package/src/internal/message-delivery.ts +53 -0
- package/src/internal/prepared-admission.ts +81 -0
- package/dist/index.mjs.map +0 -1
- package/src/host.ts +0 -215
- package/src/layers.ts +0 -381
|
@@ -0,0 +1,358 @@
|
|
|
1
|
+
import { NodeCrypto } from "@effect/platform-node";
|
|
2
|
+
import { type Stream, Context, Effect, Fiber, Layer, Ref, Schema } from "effect";
|
|
3
|
+
import {
|
|
4
|
+
type IntegrityReport,
|
|
5
|
+
type ObligationReport,
|
|
6
|
+
type ObligationThresholds,
|
|
7
|
+
type RecoveryExplanation,
|
|
8
|
+
type RetryCommand,
|
|
9
|
+
} from "effect-agent/admin";
|
|
10
|
+
import {
|
|
11
|
+
type AgentRegistration,
|
|
12
|
+
type ResolvedBinding,
|
|
13
|
+
type DurableBindingFailure,
|
|
14
|
+
} from "effect-agent/agent-registration";
|
|
15
|
+
import {
|
|
16
|
+
DurableAgentRuntime,
|
|
17
|
+
type DurableAbortFailure,
|
|
18
|
+
type DurableAwaitFailure,
|
|
19
|
+
type DurableExplainFailure,
|
|
20
|
+
type DurableObserveOptions,
|
|
21
|
+
type DurableObligationFailure,
|
|
22
|
+
type DurableRetryFailure,
|
|
23
|
+
type DurableSubmitAgent,
|
|
24
|
+
type DurableSubmitFailure,
|
|
25
|
+
type DurableSubmitOptions,
|
|
26
|
+
type DurableVerifyFailure,
|
|
27
|
+
type DurableWorkerFailure,
|
|
28
|
+
type Receipt,
|
|
29
|
+
type RecoveryReport,
|
|
30
|
+
} from "effect-agent/durable-agent-runtime";
|
|
31
|
+
import { type ThreadId, type SubmissionId } from "effect-agent/identifiers";
|
|
32
|
+
import {
|
|
33
|
+
type MessageDeliveryStore,
|
|
34
|
+
MessageDeliveryDriver,
|
|
35
|
+
type MessageDeliveryError,
|
|
36
|
+
} from "effect-agent/message-delivery";
|
|
37
|
+
import { type OperationDenied } from "effect-agent/operation-authorizer";
|
|
38
|
+
import { PreparedInputAdmission } from "effect-agent/prepared-input-admission";
|
|
39
|
+
import { type CanonicalRecordEnvelope } from "effect-agent/records";
|
|
40
|
+
import {
|
|
41
|
+
type AbortCommand,
|
|
42
|
+
type AbortIntent,
|
|
43
|
+
type Settlement,
|
|
44
|
+
} from "effect-agent/submission-ledger";
|
|
45
|
+
import { type ThreadNotMaterialized, type ThreadStoreError } from "effect-agent/thread-store";
|
|
46
|
+
|
|
47
|
+
import { runNodeMessageDeliveries } from "./internal/message-delivery.ts";
|
|
48
|
+
import { makeNodePreparedInputAdmission, NodeAdmission } from "./internal/prepared-admission.ts";
|
|
49
|
+
import {
|
|
50
|
+
NodeDurableAgentRuntime,
|
|
51
|
+
NodeDurableAgentRuntimeConfig,
|
|
52
|
+
type NodeDurableAgentRuntimeOptions,
|
|
53
|
+
} from "./NodeDurableAgentRuntime.ts";
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Admission is not open on this host: it is shutting down (deployment §6 step 1, DEPLOY-005).
|
|
57
|
+
* Accepted work is unaffected — only NEW admissions are refused.
|
|
58
|
+
*/
|
|
59
|
+
export class AdmissionClosed extends Schema.TaggedError<AdmissionClosed>()("AdmissionClosed", {
|
|
60
|
+
message: Schema.String,
|
|
61
|
+
}) {}
|
|
62
|
+
|
|
63
|
+
const makeHost = Effect.fn("NodeDurableHost.make")(function* (startWorkers: boolean) {
|
|
64
|
+
const runtime = yield* DurableAgentRuntime;
|
|
65
|
+
const config = yield* NodeDurableAgentRuntimeConfig;
|
|
66
|
+
|
|
67
|
+
// Startup gate (deployment §5, plan §host): configuration decoding and storage compatibility
|
|
68
|
+
// already gated this Layer's dependencies; the last gate before admission opens is recovering
|
|
69
|
+
// EVERY nonterminal Submission. Work needing a live Agent Binding is reported `deferred` and
|
|
70
|
+
// stays a visible obligation for `runWorkers`; lanes durably blocked on an Unknown Outcome are
|
|
71
|
+
// reported `unknown` and wait for the authorized `resolveUnknown` path (DUR-017) — they consume
|
|
72
|
+
// no worker permit while the settlement obligation stays owed.
|
|
73
|
+
const startupRecovery = yield* runtime.runRecovery;
|
|
74
|
+
|
|
75
|
+
const admission = yield* Ref.make(true);
|
|
76
|
+
|
|
77
|
+
const requireAdmission: Effect.Effect<void, AdmissionClosed> = Ref.get(admission).pipe(
|
|
78
|
+
Effect.flatMap((open) =>
|
|
79
|
+
open
|
|
80
|
+
? Effect.void
|
|
81
|
+
: Effect.fail(
|
|
82
|
+
AdmissionClosed.make({ message: "The host is shutting down; admission is closed." }),
|
|
83
|
+
),
|
|
84
|
+
),
|
|
85
|
+
);
|
|
86
|
+
|
|
87
|
+
const submit = <InputSchema extends Schema.Top>(
|
|
88
|
+
agent: DurableSubmitAgent<InputSchema>,
|
|
89
|
+
input: InputSchema["Type"],
|
|
90
|
+
options: DurableSubmitOptions,
|
|
91
|
+
): Effect.Effect<
|
|
92
|
+
Receipt,
|
|
93
|
+
AdmissionClosed | DurableSubmitFailure,
|
|
94
|
+
InputSchema["EncodingServices"]
|
|
95
|
+
> => requireAdmission.pipe(Effect.andThen(runtime.submit(agent, input, options)));
|
|
96
|
+
|
|
97
|
+
const deliveryServices = yield* Effect.context<MessageDeliveryStore>();
|
|
98
|
+
|
|
99
|
+
const deliveryContext = yield* Layer.build(
|
|
100
|
+
MessageDeliveryDriver.layer({
|
|
101
|
+
batchSize: 100,
|
|
102
|
+
concurrency: Math.min(config.workerConcurrency, 32),
|
|
103
|
+
}).pipe(
|
|
104
|
+
Layer.provide(NodeCrypto.layer),
|
|
105
|
+
Layer.provide(
|
|
106
|
+
Layer.effect(PreparedInputAdmission, makeNodePreparedInputAdmission).pipe(
|
|
107
|
+
Layer.provide(
|
|
108
|
+
Layer.succeed(NodeAdmission, { submit, submissionStatus: runtime.submissionStatus }),
|
|
109
|
+
),
|
|
110
|
+
),
|
|
111
|
+
),
|
|
112
|
+
),
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
const runDeliveries = runNodeMessageDeliveries(config.wakeScanInterval).pipe(
|
|
116
|
+
Effect.provide(Context.merge(deliveryServices, deliveryContext)),
|
|
117
|
+
);
|
|
118
|
+
|
|
119
|
+
const runWorkers = <A, E, R>(worker: Effect.Effect<A, E, R>): Effect.Effect<void, E, R> =>
|
|
120
|
+
Effect.scoped(
|
|
121
|
+
// Either side exiting stops and joins the other; delivery interruption cannot leave
|
|
122
|
+
// an apparently healthy worker pool running without message recovery.
|
|
123
|
+
Effect.raceFirst(
|
|
124
|
+
Effect.forEach(
|
|
125
|
+
Array.from({ length: config.workerConcurrency }, (_, index) => index),
|
|
126
|
+
() => worker,
|
|
127
|
+
{ concurrency: "unbounded", discard: true },
|
|
128
|
+
),
|
|
129
|
+
runDeliveries,
|
|
130
|
+
),
|
|
131
|
+
);
|
|
132
|
+
|
|
133
|
+
// S2 multi-binding pool: every claimed head resolves its exact registered Binding through
|
|
134
|
+
// the host's exact registrations, so one bounded
|
|
135
|
+
// pool serves parent and child lanes — the spec §12 smallest-pool suspension/wakeup proof
|
|
136
|
+
// runs `workerConcurrency: 1` over exactly this loop.
|
|
137
|
+
const runResolvedWorkers = runWorkers(runtime.runResolvedWorker);
|
|
138
|
+
|
|
139
|
+
const run = startWorkers
|
|
140
|
+
? Fiber.join(
|
|
141
|
+
yield* runResolvedWorkers.pipe(
|
|
142
|
+
Effect.onExit(() => Ref.set(admission, false)),
|
|
143
|
+
Effect.forkScoped,
|
|
144
|
+
),
|
|
145
|
+
)
|
|
146
|
+
: runResolvedWorkers;
|
|
147
|
+
|
|
148
|
+
// Register after the worker fiber: close admission, interrupt/join workers, drain
|
|
149
|
+
// runtime ownership, then close storage and captured application services.
|
|
150
|
+
yield* Effect.addFinalizer(() => Ref.set(admission, false));
|
|
151
|
+
|
|
152
|
+
return NodeDurableHost.of({
|
|
153
|
+
startupRecovery,
|
|
154
|
+
admissionOpen: Ref.get(admission),
|
|
155
|
+
submit,
|
|
156
|
+
awaitSettlement: runtime.awaitSettlement,
|
|
157
|
+
submissionStatus: runtime.submissionStatus,
|
|
158
|
+
observe: runtime.observe,
|
|
159
|
+
abort: runtime.abort,
|
|
160
|
+
explain: runtime.explain,
|
|
161
|
+
explainThread: runtime.explainThread,
|
|
162
|
+
verify: runtime.verify,
|
|
163
|
+
retry: runtime.retry,
|
|
164
|
+
wake: runtime.wake,
|
|
165
|
+
scanObligations: runtime.scanObligations,
|
|
166
|
+
runWorkers,
|
|
167
|
+
run,
|
|
168
|
+
runResolvedWorkers: run,
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Operational host service. Prefer the module's `layer` and `run` for managed workers.
|
|
174
|
+
* The static constructors on this class retain explicit, manual worker ownership.
|
|
175
|
+
*
|
|
176
|
+
* Startup gates run during Layer construction, so the service existing implies readiness:
|
|
177
|
+
* configuration was schema-decoded, the SQLite file passed the exact-version compatibility check,
|
|
178
|
+
* and every nonterminal Submission went through one full recovery pass BEFORE admission opened.
|
|
179
|
+
* `startupRecovery` is the auditable evidence of that reconciliation pass.
|
|
180
|
+
*
|
|
181
|
+
* Shutdown runs in reverse Layer order when the owning Scope closes: `submit` starts refusing
|
|
182
|
+
* with `AdmissionClosed` first, then the runtime Layer's ownership drain releases every claim
|
|
183
|
+
* still held so another host can take over the lanes immediately, then the SQLite resources
|
|
184
|
+
* close. Forced termination at any point stays safe — the durability protocol, not graceful
|
|
185
|
+
* shutdown, provides correctness (DEPLOY-006).
|
|
186
|
+
*/
|
|
187
|
+
export class NodeDurableHost extends Context.Service<
|
|
188
|
+
NodeDurableHost,
|
|
189
|
+
{
|
|
190
|
+
/**
|
|
191
|
+
* The recovery decisions executed (or deferred) by this host's startup reconciliation.
|
|
192
|
+
* Reports with the `unknown` disposition identify lanes blocked on Unknown Outcomes that
|
|
193
|
+
* only the authorized DUR-017 resolution path can release.
|
|
194
|
+
*/
|
|
195
|
+
readonly startupRecovery: ReadonlyArray<RecoveryReport>;
|
|
196
|
+
/** Admission-role readiness (deployment §7): true until shutdown begins. */
|
|
197
|
+
readonly admissionOpen: Effect.Effect<boolean>;
|
|
198
|
+
/** Observe the managed worker pool, preserving its failure. Manual hosts start their pool here. */
|
|
199
|
+
readonly run: Effect.Effect<void, DurableWorkerFailure | DurableBindingFailure>;
|
|
200
|
+
/** `DurableAgentRuntime.submit` behind the host admission gate. */
|
|
201
|
+
readonly submit: <InputSchema extends Schema.Top>(
|
|
202
|
+
agent: DurableSubmitAgent<InputSchema>,
|
|
203
|
+
input: InputSchema["Type"],
|
|
204
|
+
options: DurableSubmitOptions,
|
|
205
|
+
) => Effect.Effect<
|
|
206
|
+
Receipt,
|
|
207
|
+
AdmissionClosed | DurableSubmitFailure,
|
|
208
|
+
InputSchema["EncodingServices"]
|
|
209
|
+
>;
|
|
210
|
+
readonly submissionStatus: DurableAgentRuntime["Service"]["submissionStatus"];
|
|
211
|
+
readonly awaitSettlement: (receipt: Receipt) => Effect.Effect<Settlement, DurableAwaitFailure>;
|
|
212
|
+
readonly observe: (
|
|
213
|
+
receipt: Receipt,
|
|
214
|
+
options?: DurableObserveOptions,
|
|
215
|
+
) => Stream.Stream<
|
|
216
|
+
CanonicalRecordEnvelope,
|
|
217
|
+
ThreadStoreError | ThreadNotMaterialized | OperationDenied
|
|
218
|
+
>;
|
|
219
|
+
readonly abort: (command: AbortCommand) => Effect.Effect<AbortIntent, DurableAbortFailure>;
|
|
220
|
+
/** `DurableAgentRuntime.explain` — read-only recovery explanation of one Submission (P7). */
|
|
221
|
+
readonly explain: (
|
|
222
|
+
submissionId: SubmissionId,
|
|
223
|
+
) => Effect.Effect<RecoveryExplanation, DurableExplainFailure>;
|
|
224
|
+
/** `DurableAgentRuntime.explainThread` — explain every nonterminal lane member. */
|
|
225
|
+
readonly explainThread: (
|
|
226
|
+
threadId: ThreadId,
|
|
227
|
+
) => Effect.Effect<ReadonlyArray<RecoveryExplanation>, DurableExplainFailure>;
|
|
228
|
+
/** `DurableAgentRuntime.verify` — read-only integrity checks, never a repair (P7). */
|
|
229
|
+
readonly verify: (threadId: ThreadId) => Effect.Effect<IntegrityReport, DurableVerifyFailure>;
|
|
230
|
+
/** `DurableAgentRuntime.retry` — audited single-Submission re-drive with typed refusals. */
|
|
231
|
+
readonly retry: (command: RetryCommand) => Effect.Effect<RecoveryReport, DurableRetryFailure>;
|
|
232
|
+
/** `DurableAgentRuntime.wake` — the documented operator liveness nudge for one lane. */
|
|
233
|
+
readonly wake: (threadId: ThreadId) => Effect.Effect<void, OperationDenied>;
|
|
234
|
+
/** `DurableAgentRuntime.scanObligations` — the scan-based DUR-017/OPS-001 report. */
|
|
235
|
+
readonly scanObligations: (
|
|
236
|
+
thresholds: ObligationThresholds,
|
|
237
|
+
) => Effect.Effect<ObligationReport, DurableObligationFailure>;
|
|
238
|
+
/**
|
|
239
|
+
* Run `workerConcurrency` copies of the given worker effect (typically
|
|
240
|
+
* `DurableAgentRuntime.runWorker(agent)`) until the caller's Scope interrupts them. The
|
|
241
|
+
* same Scope drives pending message admission and settlement observation independently
|
|
242
|
+
* of Submission liveness. The host never forks daemon fibers.
|
|
243
|
+
*/
|
|
244
|
+
readonly runWorkers: <A, E, R>(worker: Effect.Effect<A, E, R>) => Effect.Effect<void, E, R>;
|
|
245
|
+
/**
|
|
246
|
+
* Run `workerConcurrency` copies of `DurableAgentRuntime.runResolvedWorker` over the host's
|
|
247
|
+
* registered Bindings (S2): every claimed head resolves its exact stored Binding before any
|
|
248
|
+
* code runs (SUB-023), so one bounded pool serves parent and attached-child lanes.
|
|
249
|
+
* Hosts built with the module-level `layer` instead join their existing pool.
|
|
250
|
+
*/
|
|
251
|
+
readonly runResolvedWorkers: Effect.Effect<void, DurableWorkerFailure | DurableBindingFailure>;
|
|
252
|
+
}
|
|
253
|
+
>()("@effect-agent/platform-node/NodeDurableHost") {
|
|
254
|
+
/**
|
|
255
|
+
* Compile typed registrations and acquire the complete host in one Layer Scope.
|
|
256
|
+
* Node supplies Crypto; model, tool, instruction, and schema services remain required.
|
|
257
|
+
* Startup recovery and shutdown gates are unchanged. Workers start only when the caller
|
|
258
|
+
* runs runResolvedWorkers; this constructor never starts a background worker.
|
|
259
|
+
*/
|
|
260
|
+
static layerRegistered<
|
|
261
|
+
const Entries extends ReadonlyArray<AgentRegistration>,
|
|
262
|
+
ContextError = never,
|
|
263
|
+
ContextRequirements = never,
|
|
264
|
+
AuthorizationError = never,
|
|
265
|
+
AuthorizationRequirements = never,
|
|
266
|
+
ReconcilerError = never,
|
|
267
|
+
ReconcilerRequirements = never,
|
|
268
|
+
>(
|
|
269
|
+
registrations: Entries,
|
|
270
|
+
options: NodeDurableAgentRuntimeOptions<
|
|
271
|
+
ContextError,
|
|
272
|
+
ContextRequirements,
|
|
273
|
+
AuthorizationError,
|
|
274
|
+
AuthorizationRequirements,
|
|
275
|
+
ReconcilerError,
|
|
276
|
+
ReconcilerRequirements
|
|
277
|
+
>,
|
|
278
|
+
) {
|
|
279
|
+
return NodeDurableHost.layer.pipe(
|
|
280
|
+
Layer.provideMerge(NodeDurableAgentRuntime.layerRegistered(registrations, options)),
|
|
281
|
+
);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* Host gates over an assembled `NodeDurableAgentRuntime` stack. The runtime Layer owns
|
|
286
|
+
* executable registrations; omission registers no Agents, so resolved work fails closed.
|
|
287
|
+
*/
|
|
288
|
+
static readonly layer: Layer.Layer<
|
|
289
|
+
NodeDurableHost,
|
|
290
|
+
DurableWorkerFailure | MessageDeliveryError,
|
|
291
|
+
DurableAgentRuntime | NodeDurableAgentRuntimeConfig | MessageDeliveryStore
|
|
292
|
+
> = Layer.effect(NodeDurableHost)(makeHost(false));
|
|
293
|
+
|
|
294
|
+
/** The complete DN host: `NodeDurableAgentRuntime.layer(options)` plus the host lifecycle gates. */
|
|
295
|
+
static layerStack<
|
|
296
|
+
ContextError = never,
|
|
297
|
+
ContextRequirements = never,
|
|
298
|
+
AuthorizationError = never,
|
|
299
|
+
AuthorizationRequirements = never,
|
|
300
|
+
ReconcilerError = never,
|
|
301
|
+
ReconcilerRequirements = never,
|
|
302
|
+
>(
|
|
303
|
+
options: NodeDurableAgentRuntimeOptions<
|
|
304
|
+
ContextError,
|
|
305
|
+
ContextRequirements,
|
|
306
|
+
AuthorizationError,
|
|
307
|
+
AuthorizationRequirements,
|
|
308
|
+
ReconcilerError,
|
|
309
|
+
ReconcilerRequirements
|
|
310
|
+
> & { readonly bindings?: ReadonlyArray<ResolvedBinding> },
|
|
311
|
+
) {
|
|
312
|
+
const { bindings = [], ...runtimeOptions } = options;
|
|
313
|
+
|
|
314
|
+
return NodeDurableHost.layer.pipe(
|
|
315
|
+
Layer.provideMerge(NodeDurableAgentRuntime.layerWithBindings(bindings, runtimeOptions)),
|
|
316
|
+
);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* Acquire a complete Node host and start one bounded, scoped worker pool after recovery.
|
|
322
|
+
* Provide model, tool, instruction, and schema dependencies to this Layer. Reusing the Layer
|
|
323
|
+
* shares the same pool. A worker failure closes admission; observe it with `run` at the process
|
|
324
|
+
* boundary so the application exits and releases the host instead of remaining idle.
|
|
325
|
+
*/
|
|
326
|
+
export const layer = <
|
|
327
|
+
const Entries extends ReadonlyArray<AgentRegistration>,
|
|
328
|
+
ContextError = never,
|
|
329
|
+
ContextRequirements = never,
|
|
330
|
+
AuthorizationError = never,
|
|
331
|
+
AuthorizationRequirements = never,
|
|
332
|
+
ReconcilerError = never,
|
|
333
|
+
ReconcilerRequirements = never,
|
|
334
|
+
>(
|
|
335
|
+
registrations: Entries,
|
|
336
|
+
options: NodeDurableAgentRuntimeOptions<
|
|
337
|
+
ContextError,
|
|
338
|
+
ContextRequirements,
|
|
339
|
+
AuthorizationError,
|
|
340
|
+
AuthorizationRequirements,
|
|
341
|
+
ReconcilerError,
|
|
342
|
+
ReconcilerRequirements
|
|
343
|
+
>,
|
|
344
|
+
) =>
|
|
345
|
+
Layer.effect(NodeDurableHost)(makeHost(true)).pipe(
|
|
346
|
+
Layer.provideMerge(NodeDurableAgentRuntime.layerRegistered(registrations, options)),
|
|
347
|
+
);
|
|
348
|
+
|
|
349
|
+
/**
|
|
350
|
+
* Supervise the host's existing workers without starting another pool. Use with
|
|
351
|
+
* `Effect.provide(HostLive)` and `NodeRuntime.runMain`; race it with a server Effect when
|
|
352
|
+
* the same process also serves requests. Unlike `Layer.launch`, this observes worker failures.
|
|
353
|
+
*/
|
|
354
|
+
export const run = Effect.gen(function* () {
|
|
355
|
+
const host = yield* NodeDurableHost;
|
|
356
|
+
|
|
357
|
+
return yield* host.run;
|
|
358
|
+
});
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { NodeCrypto } from "@effect/platform-node";
|
|
2
|
+
import { Cause, Duration, Effect, Layer, Option, PubSub, Result } from "effect";
|
|
3
|
+
import {
|
|
4
|
+
type ScheduleAuthorizer,
|
|
5
|
+
type SchedulingLimits,
|
|
6
|
+
ScheduleStorageError,
|
|
7
|
+
ScheduleStore,
|
|
8
|
+
type ScheduleValidationError,
|
|
9
|
+
ScheduleWake,
|
|
10
|
+
defaultSchedulingLimits,
|
|
11
|
+
} from "effect-agent/schedule";
|
|
12
|
+
import { type ScheduleProcessFailure, Scheduling, ScheduleDriver } from "effect-agent/scheduling";
|
|
13
|
+
|
|
14
|
+
import type { NodeDurableHost } from "./NodeDurableHost.ts";
|
|
15
|
+
import { nodeScheduledInputAdmissionLayer } from "./NodeSubscriptions.ts";
|
|
16
|
+
|
|
17
|
+
/** One bounded hint slot for the single supported Node scheduler. Indexed polling repairs loss. */
|
|
18
|
+
export const nodeScheduleWakeLayer: Layer.Layer<ScheduleWake> = Layer.effect(
|
|
19
|
+
ScheduleWake,
|
|
20
|
+
Effect.gen(function* () {
|
|
21
|
+
const hints = yield* PubSub.sliding<void>(1);
|
|
22
|
+
const subscription = yield* PubSub.subscribe(hints);
|
|
23
|
+
|
|
24
|
+
yield* Effect.addFinalizer(() => PubSub.shutdown(hints));
|
|
25
|
+
|
|
26
|
+
return ScheduleWake.of({
|
|
27
|
+
notify: PubSub.publish(hints, undefined).pipe(Effect.asVoid),
|
|
28
|
+
await: PubSub.take(subscription),
|
|
29
|
+
});
|
|
30
|
+
}),
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
const reportPassFailure = (cause: Cause.Cause<ScheduleProcessFailure>): Effect.Effect<boolean> =>
|
|
34
|
+
Cause.hasInterruptsOnly(cause)
|
|
35
|
+
? Effect.interrupt
|
|
36
|
+
: Effect.logWarning("Node scheduling pass failed").pipe(
|
|
37
|
+
Effect.annotateLogs({
|
|
38
|
+
failureTag: Option.match(Cause.findErrorOption(cause), {
|
|
39
|
+
onNone: () => "Defect",
|
|
40
|
+
onSome: (error) => error._tag,
|
|
41
|
+
}),
|
|
42
|
+
}),
|
|
43
|
+
Effect.as(false),
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
const nodeSchedulingDriverLayer = (
|
|
47
|
+
limits: SchedulingLimits,
|
|
48
|
+
): Layer.Layer<never, never, ScheduleDriver | ScheduleStore | ScheduleWake> =>
|
|
49
|
+
Layer.effectDiscard(
|
|
50
|
+
Effect.gen(function* () {
|
|
51
|
+
const scheduling = yield* ScheduleDriver;
|
|
52
|
+
const store = yield* ScheduleStore;
|
|
53
|
+
const wake = yield* ScheduleWake;
|
|
54
|
+
|
|
55
|
+
const run = Effect.gen(function* () {
|
|
56
|
+
while (true) {
|
|
57
|
+
const passSucceeded = yield* scheduling.runDue().pipe(
|
|
58
|
+
Effect.map((pass) => pass.failed === 0),
|
|
59
|
+
Effect.catchCause(reportPassFailure),
|
|
60
|
+
);
|
|
61
|
+
|
|
62
|
+
const deadlineResult = passSucceeded
|
|
63
|
+
? yield* store.nextDeadline().pipe(Effect.result)
|
|
64
|
+
: Result.fail(
|
|
65
|
+
ScheduleStorageError.make({ operation: "driver pass", reason: "unavailable" }),
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
if (Result.isFailure(deadlineResult) && passSucceeded) {
|
|
69
|
+
yield* Effect.logWarning("Node scheduling deadline query failed");
|
|
70
|
+
}
|
|
71
|
+
const nowMillis = yield* Effect.clockWith((clock) => clock.currentTimeMillis);
|
|
72
|
+
|
|
73
|
+
const deadlineDelay =
|
|
74
|
+
Result.isSuccess(deadlineResult) && deadlineResult.success !== null
|
|
75
|
+
? Math.max(0, deadlineResult.success - nowMillis)
|
|
76
|
+
: limits.recoveryPollMillis;
|
|
77
|
+
|
|
78
|
+
const delay = Math.min(deadlineDelay, limits.recoveryPollMillis);
|
|
79
|
+
|
|
80
|
+
yield* Effect.raceFirst(wake.await, Effect.sleep(Duration.millis(delay)));
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
yield* Effect.forkScoped(run);
|
|
85
|
+
}),
|
|
86
|
+
);
|
|
87
|
+
|
|
88
|
+
export interface NodeSchedulingOptions {
|
|
89
|
+
readonly limits?: SchedulingLimits | undefined;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Optional Scope-owned Node scheduling service and driver. The caller must provide the existing
|
|
94
|
+
* host, its SQLite ScheduleStore, and an explicit ScheduleAuthorizer.
|
|
95
|
+
*/
|
|
96
|
+
export class NodeScheduling {
|
|
97
|
+
static layer(
|
|
98
|
+
options: NodeSchedulingOptions = {},
|
|
99
|
+
): Layer.Layer<
|
|
100
|
+
Scheduling,
|
|
101
|
+
ScheduleValidationError,
|
|
102
|
+
NodeDurableHost | ScheduleStore | ScheduleAuthorizer
|
|
103
|
+
> {
|
|
104
|
+
const limits = options.limits ?? defaultSchedulingLimits;
|
|
105
|
+
|
|
106
|
+
const schedulingWithDriver = nodeSchedulingDriverLayer(limits).pipe(
|
|
107
|
+
Layer.provide(ScheduleDriver.layer(limits)),
|
|
108
|
+
Layer.merge(Scheduling.layer(limits)),
|
|
109
|
+
);
|
|
110
|
+
|
|
111
|
+
return schedulingWithDriver.pipe(
|
|
112
|
+
Layer.provide(
|
|
113
|
+
Layer.mergeAll(nodeScheduledInputAdmissionLayer, nodeScheduleWakeLayer, NodeCrypto.layer),
|
|
114
|
+
),
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { NodeCrypto } from "@effect/platform-node";
|
|
2
|
+
import { Cause, Duration, Effect, Exit, Layer, Option } from "effect";
|
|
3
|
+
import { type EventSources } from "effect-agent/event-source";
|
|
4
|
+
import { PreparedInputAdmission } from "effect-agent/prepared-input-admission";
|
|
5
|
+
import { type ScheduledEnvelope, ScheduledInputAdmission } from "effect-agent/schedule";
|
|
6
|
+
import {
|
|
7
|
+
type PreparedInput,
|
|
8
|
+
type SubscriptionAuthorizer,
|
|
9
|
+
type SubscriptionError,
|
|
10
|
+
type SubscriptionLimits,
|
|
11
|
+
type SubscriptionStoreFailure,
|
|
12
|
+
SubscriptionStore,
|
|
13
|
+
defaultSubscriptionLimits,
|
|
14
|
+
} from "effect-agent/subscription";
|
|
15
|
+
import { type SubscriptionInputBindings } from "effect-agent/subscription-input";
|
|
16
|
+
import { SubscriptionDriver, SubscriptionIntake, Subscriptions } from "effect-agent/subscriptions";
|
|
17
|
+
|
|
18
|
+
import { makeNodePreparedInputAdmission, NodeAdmission } from "./internal/prepared-admission.ts";
|
|
19
|
+
import { NodeDurableHost } from "./NodeDurableHost.ts";
|
|
20
|
+
|
|
21
|
+
/** Ordinary prepared admission through the Scope-owned Node host gate. */
|
|
22
|
+
export const nodePreparedInputAdmissionLayer: Layer.Layer<
|
|
23
|
+
PreparedInputAdmission,
|
|
24
|
+
never,
|
|
25
|
+
NodeDurableHost
|
|
26
|
+
> = Layer.effect(PreparedInputAdmission, makeNodePreparedInputAdmission).pipe(
|
|
27
|
+
Layer.provide(Layer.effect(NodeAdmission, NodeDurableHost)),
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
const preparedFromSchedule = (envelope: ScheduledEnvelope): PreparedInput => ({
|
|
31
|
+
schemaVersion: 1,
|
|
32
|
+
threadId: envelope.threadId,
|
|
33
|
+
deliveryPrincipal: envelope.deliveryPrincipal,
|
|
34
|
+
...(envelope.admissionGroup === undefined ? {} : { admissionGroup: envelope.admissionGroup }),
|
|
35
|
+
...(envelope.admissionFence === undefined ? {} : { admissionFence: envelope.admissionFence }),
|
|
36
|
+
agentId: envelope.agentId,
|
|
37
|
+
definitions: envelope.definitions,
|
|
38
|
+
input: envelope.input,
|
|
39
|
+
inputDigest: envelope.inputDigest,
|
|
40
|
+
admissionKey: envelope.admissionKey,
|
|
41
|
+
authorization: envelope.authorization,
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
/** Compatibility adapter retaining the public scheduling admission port. */
|
|
45
|
+
const nodeScheduledInputAdmissionFromPreparedLayer: Layer.Layer<
|
|
46
|
+
ScheduledInputAdmission,
|
|
47
|
+
never,
|
|
48
|
+
PreparedInputAdmission
|
|
49
|
+
> = Layer.effect(
|
|
50
|
+
ScheduledInputAdmission,
|
|
51
|
+
Effect.map(PreparedInputAdmission, (admission) =>
|
|
52
|
+
ScheduledInputAdmission.of({
|
|
53
|
+
submit: (envelope) => admission.submit(preparedFromSchedule(envelope)),
|
|
54
|
+
}),
|
|
55
|
+
),
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
export const nodeScheduledInputAdmissionLayer: Layer.Layer<
|
|
59
|
+
ScheduledInputAdmission,
|
|
60
|
+
never,
|
|
61
|
+
NodeDurableHost
|
|
62
|
+
> = nodeScheduledInputAdmissionFromPreparedLayer.pipe(
|
|
63
|
+
Layer.provide(nodePreparedInputAdmissionLayer),
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
const reportPassFailure = (cause: Cause.Cause<SubscriptionStoreFailure>): Effect.Effect<boolean> =>
|
|
67
|
+
Cause.hasInterruptsOnly(cause)
|
|
68
|
+
? Effect.interrupt
|
|
69
|
+
: Effect.logWarning("Node subscription pass failed").pipe(
|
|
70
|
+
Effect.annotateLogs({
|
|
71
|
+
failureTag: Option.match(Cause.findErrorOption(cause), {
|
|
72
|
+
onNone: () => "Defect",
|
|
73
|
+
onSome: (error) => error._tag,
|
|
74
|
+
}),
|
|
75
|
+
}),
|
|
76
|
+
Effect.as(false),
|
|
77
|
+
);
|
|
78
|
+
|
|
79
|
+
const nodeSubscriptionDriverLayer = (
|
|
80
|
+
limits: SubscriptionLimits,
|
|
81
|
+
): Layer.Layer<never, never, SubscriptionDriver | SubscriptionStore> =>
|
|
82
|
+
Layer.effectDiscard(
|
|
83
|
+
Effect.gen(function* () {
|
|
84
|
+
const driver = yield* SubscriptionDriver;
|
|
85
|
+
const store = yield* SubscriptionStore;
|
|
86
|
+
|
|
87
|
+
const run = Effect.gen(function* () {
|
|
88
|
+
while (true) {
|
|
89
|
+
const passSucceeded = yield* driver.runDue.pipe(
|
|
90
|
+
Effect.map((pass) => pass.failed === 0),
|
|
91
|
+
Effect.catchCause(reportPassFailure),
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
if (!passSucceeded) {
|
|
95
|
+
yield* Effect.sleep(Duration.millis(limits.retryMillis));
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const deadline = yield* store.nextDeadline.pipe(Effect.exit);
|
|
100
|
+
|
|
101
|
+
if (Exit.isFailure(deadline)) {
|
|
102
|
+
yield* reportPassFailure(deadline.cause);
|
|
103
|
+
yield* Effect.sleep(Duration.millis(limits.retryMillis));
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const nowMillis = yield* Effect.clockWith((clock) => clock.currentTimeMillis);
|
|
108
|
+
|
|
109
|
+
const delay =
|
|
110
|
+
deadline.value === null
|
|
111
|
+
? limits.retryMillis
|
|
112
|
+
: Math.max(1, Math.min(deadline.value - nowMillis, limits.retryMillis));
|
|
113
|
+
|
|
114
|
+
yield* Effect.sleep(Duration.millis(delay));
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
yield* Effect.forkScoped(run);
|
|
119
|
+
}),
|
|
120
|
+
);
|
|
121
|
+
|
|
122
|
+
export interface NodeSubscriptionsOptions {
|
|
123
|
+
readonly limits?: SubscriptionLimits | undefined;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* One Scope-owned subscription partition in the sole process owning its SQLite database.
|
|
128
|
+
* Indexed polling repairs restart and lost wake state; closing the Scope interrupts the driver.
|
|
129
|
+
*/
|
|
130
|
+
export class NodeSubscriptions {
|
|
131
|
+
static layer(
|
|
132
|
+
options: NodeSubscriptionsOptions = {},
|
|
133
|
+
): Layer.Layer<
|
|
134
|
+
Subscriptions | SubscriptionIntake,
|
|
135
|
+
SubscriptionError,
|
|
136
|
+
| NodeDurableHost
|
|
137
|
+
| SubscriptionStore
|
|
138
|
+
| SubscriptionAuthorizer
|
|
139
|
+
| EventSources
|
|
140
|
+
| SubscriptionInputBindings
|
|
141
|
+
> {
|
|
142
|
+
const limits = options.limits ?? defaultSubscriptionLimits;
|
|
143
|
+
|
|
144
|
+
const publicServices = Layer.merge(
|
|
145
|
+
Subscriptions.layer(limits),
|
|
146
|
+
SubscriptionIntake.layer(limits),
|
|
147
|
+
);
|
|
148
|
+
|
|
149
|
+
const driver = nodeSubscriptionDriverLayer(limits).pipe(
|
|
150
|
+
Layer.provide(SubscriptionDriver.layer(limits)),
|
|
151
|
+
);
|
|
152
|
+
|
|
153
|
+
return Layer.merge(publicServices, driver).pipe(
|
|
154
|
+
Layer.provide(nodePreparedInputAdmissionLayer),
|
|
155
|
+
Layer.provide(NodeCrypto.layer),
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
}
|