@effect-agent/platform-node 0.1.0-beta.6 → 0.1.0-beta.60
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-CFElFzNF.d.mts +179 -0
- package/dist/NodeDurableAgentRuntime.d.mts +2 -0
- package/dist/NodeDurableAgentRuntime.mjs +221 -0
- package/dist/NodeDurableAgentRuntime.mjs.map +1 -0
- package/dist/NodeDurableHost-BMSajuQn.mjs +194 -0
- package/dist/NodeDurableHost-BMSajuQn.mjs.map +1 -0
- package/dist/NodeDurableHost.d.mts +168 -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-4ZeXYwPu.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 +596 -0
- package/src/NodeDurableHost.ts +361 -0
- package/src/NodeScheduling.ts +121 -0
- package/src/NodeSubscriptions.ts +162 -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,596 @@
|
|
|
1
|
+
import { type SubmissionId } from "@effect-agent/core/Identifiers";
|
|
2
|
+
import {
|
|
3
|
+
CurrentToolFailureObserver,
|
|
4
|
+
RunContextPreparationPassthrough,
|
|
5
|
+
RunToolAuthorization,
|
|
6
|
+
toolFailureObserverLayer,
|
|
7
|
+
type RunContextPreparation,
|
|
8
|
+
type RunCostEstimator,
|
|
9
|
+
type RunToolFailureObserver,
|
|
10
|
+
} from "@effect-agent/engine/RunOptions";
|
|
11
|
+
import { messageDeliveryStoreLayer } from "@effect-agent/storage-sqlite/SqliteMessageDeliveryStore";
|
|
12
|
+
import { scheduleStoreLayer } from "@effect-agent/storage-sqlite/SqliteScheduleStore";
|
|
13
|
+
import {
|
|
14
|
+
SqliteStorageConfig,
|
|
15
|
+
SqliteStorageConfigValue,
|
|
16
|
+
} from "@effect-agent/storage-sqlite/SqliteStorageConfig";
|
|
17
|
+
import {
|
|
18
|
+
type SqliteStorageFailpoint,
|
|
19
|
+
type SqliteStorageFailpointHandler,
|
|
20
|
+
} from "@effect-agent/storage-sqlite/SqliteStorageFailpoint";
|
|
21
|
+
import { submissionLedgerLayer } from "@effect-agent/storage-sqlite/SqliteSubmissionLedger";
|
|
22
|
+
import {
|
|
23
|
+
threadStoreLayer,
|
|
24
|
+
storageFailpointLayer,
|
|
25
|
+
type SqliteStorageInitializationError,
|
|
26
|
+
} from "@effect-agent/storage-sqlite/SqliteThreadStore";
|
|
27
|
+
import {
|
|
28
|
+
type AgentRegistration,
|
|
29
|
+
type ResolvedBinding,
|
|
30
|
+
} from "@effect-agent/thread/AgentRegistration";
|
|
31
|
+
import {
|
|
32
|
+
DurableAgentRuntime,
|
|
33
|
+
DurableRuntimeConfig,
|
|
34
|
+
} from "@effect-agent/thread/DurableAgentRuntime";
|
|
35
|
+
import {
|
|
36
|
+
DurableRuntimeFailpoint,
|
|
37
|
+
type DurableRuntimeFailpointHandler,
|
|
38
|
+
} from "@effect-agent/thread/DurableFailpoint";
|
|
39
|
+
import {
|
|
40
|
+
type MessageDeliveryError,
|
|
41
|
+
type MessageDeliveryStore,
|
|
42
|
+
} from "@effect-agent/thread/MessageDelivery";
|
|
43
|
+
import { DeploymentId, ProducerId } from "@effect-agent/thread/Records";
|
|
44
|
+
import { type ScheduleStore } from "@effect-agent/thread/Schedule";
|
|
45
|
+
import {
|
|
46
|
+
DEFAULT_OWNERSHIP_LEASE_DURATION,
|
|
47
|
+
ReleaseOwnershipRequest,
|
|
48
|
+
SubmissionLedger,
|
|
49
|
+
type OwnershipToken,
|
|
50
|
+
} from "@effect-agent/thread/SubmissionLedger";
|
|
51
|
+
import { type ThreadStore } from "@effect-agent/thread/ThreadStore";
|
|
52
|
+
import { ToolReconciler } from "@effect-agent/thread/ToolReconciler";
|
|
53
|
+
import { type WakeScheduler } from "@effect-agent/thread/WakeScheduler";
|
|
54
|
+
import { NodeCrypto } from "@effect/platform-node";
|
|
55
|
+
import { SqliteClient } from "@effect/sql-sqlite-node";
|
|
56
|
+
import { Context, type Crypto, Duration, Effect, Layer, Ref, Schema } from "effect";
|
|
57
|
+
import type * as SqlClientService from "effect/unstable/sql/SqlClient";
|
|
58
|
+
|
|
59
|
+
import { NodeWakeSchedulerConfig, nodeWakeSchedulerLayer } from "./NodeWakeScheduler.ts";
|
|
60
|
+
|
|
61
|
+
const PositiveMillis = Schema.Int.check(Schema.isGreaterThan(0));
|
|
62
|
+
const NonNegativeMillis = Schema.Int.check(Schema.isGreaterThanOrEqualTo(0));
|
|
63
|
+
|
|
64
|
+
const WorkerConcurrency = Schema.Int.check(
|
|
65
|
+
Schema.isGreaterThanOrEqualTo(1),
|
|
66
|
+
Schema.isLessThanOrEqualTo(64),
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
/** The supplied Node durable runtime configuration failed schema validation (DEPLOY-003). */
|
|
70
|
+
export class NodePlatformConfigError extends Schema.TaggedError<NodePlatformConfigError>()(
|
|
71
|
+
"NodePlatformConfigError",
|
|
72
|
+
{
|
|
73
|
+
message: Schema.String,
|
|
74
|
+
cause: Schema.optionalKey(Schema.Defect()),
|
|
75
|
+
},
|
|
76
|
+
) {}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Validated Node durable runtime configuration (deployment §4: decoded once during Layer
|
|
80
|
+
* construction, exposed as a typed service). Every cadence is in milliseconds and every bound is
|
|
81
|
+
* finite; `workerConcurrency` caps how many worker loops `NodeDurableHost.runWorkers` drives.
|
|
82
|
+
*/
|
|
83
|
+
export class NodeDurableAgentRuntimeConfigValue extends Schema.Class<NodeDurableAgentRuntimeConfigValue>(
|
|
84
|
+
"@effect-agent/platform-node/NodeDurableAgentRuntimeConfigValue",
|
|
85
|
+
)({
|
|
86
|
+
/** SQLite database file backing the Thread Log, Submission Ledger, and delivery obligations. */
|
|
87
|
+
filename: Schema.NonEmptyString,
|
|
88
|
+
deploymentId: DeploymentId,
|
|
89
|
+
producerId: ProducerId,
|
|
90
|
+
/** Submission ownership lease duration (D5); liveness hint only, epochs stay authoritative. */
|
|
91
|
+
ownershipLeaseDuration: PositiveMillis,
|
|
92
|
+
/** Finite bound on concurrent worker loops per host (rule 10). */
|
|
93
|
+
workerConcurrency: WorkerConcurrency,
|
|
94
|
+
/** Fallback scan cadence for the Node wake scheduler and independent message delivery recovery. */
|
|
95
|
+
wakeScanInterval: PositiveMillis,
|
|
96
|
+
/** `awaitSettlement` ledger re-check cadence when no wake arrives. */
|
|
97
|
+
settlementPollInterval: PositiveMillis,
|
|
98
|
+
/** Worker ownership-lease renewal cadence. */
|
|
99
|
+
leaseRenewalInterval: PositiveMillis,
|
|
100
|
+
/** Active-Run abort-intent poll cadence. */
|
|
101
|
+
abortPollInterval: PositiveMillis,
|
|
102
|
+
/** Bounded SQLITE_BUSY retry window for write-lock acquisition. */
|
|
103
|
+
busyTimeout: NonNegativeMillis,
|
|
104
|
+
/** Canonical observation poll cadence of the SQLite store. */
|
|
105
|
+
observationPollInterval: NonNegativeMillis,
|
|
106
|
+
/** Opt-in full payload/digest-chain audit while opening the store. */
|
|
107
|
+
verifyOnOpen: Schema.Boolean,
|
|
108
|
+
}) {}
|
|
109
|
+
|
|
110
|
+
/** Explicit configuration authority for the assembled Node durable runtime. */
|
|
111
|
+
export class NodeDurableAgentRuntimeConfig extends Context.Service<
|
|
112
|
+
NodeDurableAgentRuntimeConfig,
|
|
113
|
+
NodeDurableAgentRuntimeConfigValue
|
|
114
|
+
>()("@effect-agent/platform-node/NodeDurableAgentRuntimeConfig") {}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Raw (unvalidated) construction options for `NodeDurableAgentRuntime.layer`. Optional fields default
|
|
118
|
+
* to the documented production values; everything is schema-decoded into
|
|
119
|
+
* `NodeDurableAgentRuntimeConfigValue` before any resource opens (deployment §5 gate 1).
|
|
120
|
+
*/
|
|
121
|
+
export interface NodeDurableAgentRuntimeOptions<
|
|
122
|
+
ContextError = never,
|
|
123
|
+
ContextRequirements = never,
|
|
124
|
+
AuthorizationError = never,
|
|
125
|
+
AuthorizationRequirements = never,
|
|
126
|
+
ReconcilerError = never,
|
|
127
|
+
ReconcilerRequirements = never,
|
|
128
|
+
> {
|
|
129
|
+
readonly filename: string;
|
|
130
|
+
readonly deploymentId: string;
|
|
131
|
+
readonly producerId: string;
|
|
132
|
+
/** Milliseconds; default `DEFAULT_OWNERSHIP_LEASE_DURATION` (30s, D5). */
|
|
133
|
+
readonly ownershipLeaseDuration?: number | undefined;
|
|
134
|
+
/** Default 1; bounded to 1..64. */
|
|
135
|
+
readonly workerConcurrency?: number | undefined;
|
|
136
|
+
/** Milliseconds; default 1000. */
|
|
137
|
+
readonly wakeScanInterval?: number | undefined;
|
|
138
|
+
/** Milliseconds; default 500. */
|
|
139
|
+
readonly settlementPollInterval?: number | undefined;
|
|
140
|
+
/** Milliseconds; default 10000. */
|
|
141
|
+
readonly leaseRenewalInterval?: number | undefined;
|
|
142
|
+
/** Milliseconds; default 500. */
|
|
143
|
+
readonly abortPollInterval?: number | undefined;
|
|
144
|
+
/** Deployment-owned pricing authority used by durable cost budgets and settlements. */
|
|
145
|
+
readonly estimateCostMicrousd?: RunCostEstimator | undefined;
|
|
146
|
+
/** Closed trusted Tool failure reporting. Omission masks ambient observers at construction. */
|
|
147
|
+
readonly toolFailureObserver?: RunToolFailureObserver | undefined;
|
|
148
|
+
/** Milliseconds; default 5000. */
|
|
149
|
+
readonly busyTimeout?: number | undefined;
|
|
150
|
+
/** Milliseconds; default 25. */
|
|
151
|
+
readonly observationPollInterval?: number | undefined;
|
|
152
|
+
/** Default false. */
|
|
153
|
+
readonly verifyOnOpen?: boolean | undefined;
|
|
154
|
+
/** SQLite adapter fault injection (`ledger:*` / `append:*` locations); default none. */
|
|
155
|
+
readonly storageFailpoint?: SqliteStorageFailpointHandler | undefined;
|
|
156
|
+
/** Coordinator fault injection (`submit:*` / `terminalize:*` locations); default none. */
|
|
157
|
+
readonly runtimeFailpoint?: DurableRuntimeFailpointHandler | undefined;
|
|
158
|
+
/**
|
|
159
|
+
* Reconciliation policy consulted for open ordinary Tool Calls before an Unknown Outcome is
|
|
160
|
+
* recorded (durability §10, DUR-009). Defaults to the fail-closed `ToolReconciler.uncertain`:
|
|
161
|
+
* with no registered policy, every open call stays Unknown and routes to the authorized
|
|
162
|
+
* DUR-017 resolution path. Construction errors and application dependencies remain in the
|
|
163
|
+
* assembled Layer's E and R; the platform supplies Crypto.
|
|
164
|
+
*/
|
|
165
|
+
readonly toolReconciler?:
|
|
166
|
+
| Layer.Layer<ToolReconciler, ReconcilerError, ReconcilerRequirements | Crypto.Crypto>
|
|
167
|
+
| undefined;
|
|
168
|
+
/** Host prompt preparation, acquired once with the runtime; default pass-through. */
|
|
169
|
+
readonly runContext?:
|
|
170
|
+
| Layer.Layer<RunContextPreparation, ContextError, ContextRequirements | Crypto.Crypto>
|
|
171
|
+
| undefined;
|
|
172
|
+
/**
|
|
173
|
+
* Independent action-time Tool authority, acquired once with the runtime; default allow-all.
|
|
174
|
+
* Construction errors and application dependencies remain in the assembled Layer's E and R.
|
|
175
|
+
* The platform supplies Crypto to these extension Layers.
|
|
176
|
+
*/
|
|
177
|
+
readonly toolAuthorization?:
|
|
178
|
+
| Layer.Layer<
|
|
179
|
+
RunToolAuthorization,
|
|
180
|
+
AuthorizationError,
|
|
181
|
+
AuthorizationRequirements | Crypto.Crypto
|
|
182
|
+
>
|
|
183
|
+
| undefined;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/** Built-in construction failures. `layer` also preserves supplied service Layers' errors. */
|
|
187
|
+
export type NodeDurableAgentRuntimeInitializationError =
|
|
188
|
+
| NodePlatformConfigError
|
|
189
|
+
| MessageDeliveryError
|
|
190
|
+
| SqliteStorageInitializationError;
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* The services `NodeDurableAgentRuntime.layer` provides. Additional SQLite adapters can use
|
|
194
|
+
* the same client and storage configuration, sharing its serialized connection and Scope.
|
|
195
|
+
*/
|
|
196
|
+
export type NodeDurableAgentRuntimeServices =
|
|
197
|
+
| DurableAgentRuntime
|
|
198
|
+
| SubmissionLedger
|
|
199
|
+
| ThreadStore
|
|
200
|
+
| ScheduleStore
|
|
201
|
+
| MessageDeliveryStore
|
|
202
|
+
| WakeScheduler
|
|
203
|
+
| DurableRuntimeConfig
|
|
204
|
+
| NodeDurableAgentRuntimeConfig
|
|
205
|
+
| SqlClientService.SqlClient
|
|
206
|
+
| SqliteStorageConfig
|
|
207
|
+
| SqliteStorageFailpoint;
|
|
208
|
+
|
|
209
|
+
const decodeConfigValue = Schema.decodeUnknownEffect(NodeDurableAgentRuntimeConfigValue);
|
|
210
|
+
|
|
211
|
+
const configFromOptions = (
|
|
212
|
+
options: Omit<
|
|
213
|
+
NodeDurableAgentRuntimeOptions,
|
|
214
|
+
"runContext" | "toolAuthorization" | "toolReconciler"
|
|
215
|
+
>,
|
|
216
|
+
): Effect.Effect<NodeDurableAgentRuntimeConfigValue, NodePlatformConfigError> =>
|
|
217
|
+
decodeConfigValue({
|
|
218
|
+
filename: options.filename,
|
|
219
|
+
deploymentId: options.deploymentId,
|
|
220
|
+
producerId: options.producerId,
|
|
221
|
+
ownershipLeaseDuration:
|
|
222
|
+
options.ownershipLeaseDuration ?? Duration.toMillis(DEFAULT_OWNERSHIP_LEASE_DURATION),
|
|
223
|
+
workerConcurrency: options.workerConcurrency ?? 1,
|
|
224
|
+
wakeScanInterval: options.wakeScanInterval ?? 1_000,
|
|
225
|
+
settlementPollInterval: options.settlementPollInterval ?? 500,
|
|
226
|
+
leaseRenewalInterval: options.leaseRenewalInterval ?? 10_000,
|
|
227
|
+
abortPollInterval: options.abortPollInterval ?? 500,
|
|
228
|
+
busyTimeout: options.busyTimeout ?? 5_000,
|
|
229
|
+
observationPollInterval: options.observationPollInterval ?? 25,
|
|
230
|
+
verifyOnOpen: options.verifyOnOpen ?? false,
|
|
231
|
+
}).pipe(
|
|
232
|
+
Effect.mapError((error) =>
|
|
233
|
+
NodePlatformConfigError.make({
|
|
234
|
+
message: `Invalid Node durable runtime configuration: ${error.message}`,
|
|
235
|
+
cause: error,
|
|
236
|
+
}),
|
|
237
|
+
),
|
|
238
|
+
);
|
|
239
|
+
|
|
240
|
+
/** SQLite storage configuration derived from the single validated Node configuration. */
|
|
241
|
+
const sqliteStorageConfigLayer: Layer.Layer<
|
|
242
|
+
SqliteStorageConfig,
|
|
243
|
+
never,
|
|
244
|
+
NodeDurableAgentRuntimeConfig
|
|
245
|
+
> = Layer.effect(SqliteStorageConfig)(
|
|
246
|
+
Effect.gen(function* () {
|
|
247
|
+
const config = yield* NodeDurableAgentRuntimeConfig;
|
|
248
|
+
|
|
249
|
+
return SqliteStorageConfigValue.make({
|
|
250
|
+
observationPollInterval: config.observationPollInterval,
|
|
251
|
+
busyTimeout: config.busyTimeout,
|
|
252
|
+
ownershipLeaseDuration: config.ownershipLeaseDuration,
|
|
253
|
+
verifyOnOpen: config.verifyOnOpen,
|
|
254
|
+
});
|
|
255
|
+
}),
|
|
256
|
+
);
|
|
257
|
+
|
|
258
|
+
/** Thread coordinator configuration derived from the single validated Node configuration. */
|
|
259
|
+
const durableRuntimeConfigLayer = (
|
|
260
|
+
estimateCostMicrousd: RunCostEstimator | undefined,
|
|
261
|
+
): Layer.Layer<DurableRuntimeConfig, never, NodeDurableAgentRuntimeConfig> =>
|
|
262
|
+
Layer.effect(DurableRuntimeConfig)(
|
|
263
|
+
Effect.gen(function* () {
|
|
264
|
+
const config = yield* NodeDurableAgentRuntimeConfig;
|
|
265
|
+
|
|
266
|
+
return DurableRuntimeConfig.make({
|
|
267
|
+
deploymentId: config.deploymentId,
|
|
268
|
+
producerId: config.producerId,
|
|
269
|
+
settlementPollInterval: Duration.millis(config.settlementPollInterval),
|
|
270
|
+
leaseRenewalInterval: Duration.millis(config.leaseRenewalInterval),
|
|
271
|
+
abortPollInterval: Duration.millis(config.abortPollInterval),
|
|
272
|
+
...(estimateCostMicrousd === undefined ? {} : { estimateCostMicrousd }),
|
|
273
|
+
});
|
|
274
|
+
}),
|
|
275
|
+
);
|
|
276
|
+
|
|
277
|
+
/** Wake fallback-scan cadence derived from the single validated Node configuration. */
|
|
278
|
+
const wakeSchedulerConfigLayer: Layer.Layer<
|
|
279
|
+
NodeWakeSchedulerConfig,
|
|
280
|
+
never,
|
|
281
|
+
NodeDurableAgentRuntimeConfig
|
|
282
|
+
> = Layer.effect(NodeWakeSchedulerConfig)(
|
|
283
|
+
Effect.gen(function* () {
|
|
284
|
+
const config = yield* NodeDurableAgentRuntimeConfig;
|
|
285
|
+
|
|
286
|
+
return { scanInterval: Duration.millis(config.wakeScanInterval) };
|
|
287
|
+
}),
|
|
288
|
+
);
|
|
289
|
+
|
|
290
|
+
const releaseTrackedOwnership = (
|
|
291
|
+
ledger: SubmissionLedger["Service"],
|
|
292
|
+
registry: Ref.Ref<ReadonlyMap<SubmissionId, OwnershipToken>>,
|
|
293
|
+
): Effect.Effect<void> =>
|
|
294
|
+
Effect.gen(function* () {
|
|
295
|
+
const tracked = yield* Ref.getAndSet(registry, new Map<SubmissionId, OwnershipToken>());
|
|
296
|
+
|
|
297
|
+
for (const [submissionId, ownershipToken] of tracked) {
|
|
298
|
+
yield* ledger
|
|
299
|
+
.releaseOwnership(ReleaseOwnershipRequest.make({ submissionId, ownershipToken }))
|
|
300
|
+
.pipe(
|
|
301
|
+
Effect.catchTags({
|
|
302
|
+
// A newer epoch already owns (or settled) the lane: nothing left to drain.
|
|
303
|
+
OwnershipLost: () => Effect.void,
|
|
304
|
+
// Drain is best-effort by design: the lease still expires and the durability protocol,
|
|
305
|
+
// not graceful shutdown, provides correctness (DEPLOY-006).
|
|
306
|
+
LedgerError: (error) =>
|
|
307
|
+
Effect.logWarning("Ownership drain failed; the lease will expire instead", error),
|
|
308
|
+
}),
|
|
309
|
+
);
|
|
310
|
+
}
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* Shutdown-drain decorator for a `SubmissionLedger` (deployment §6 step 6): every ownership
|
|
315
|
+
* period granted through this Layer is tracked — claims start tracking, renewals follow token
|
|
316
|
+
* rotation, releases and settlement finalizations stop it — and every ownership still held when
|
|
317
|
+
* the Layer's Scope closes is released so another host can claim the lane immediately instead of
|
|
318
|
+
* waiting for lease expiry. The drain is a liveness courtesy only; producer-epoch fencing remains
|
|
319
|
+
* the correctness authority (DUR-006), and a forced kill simply falls back to lease expiry.
|
|
320
|
+
*/
|
|
321
|
+
export const ownershipDrainLayer: Layer.Layer<SubmissionLedger, never, SubmissionLedger> =
|
|
322
|
+
Layer.effect(SubmissionLedger)(
|
|
323
|
+
Effect.gen(function* () {
|
|
324
|
+
const ledger = yield* SubmissionLedger;
|
|
325
|
+
|
|
326
|
+
const registry = yield* Ref.make<ReadonlyMap<SubmissionId, OwnershipToken>>(
|
|
327
|
+
new Map<SubmissionId, OwnershipToken>(),
|
|
328
|
+
);
|
|
329
|
+
|
|
330
|
+
const track = (submissionId: SubmissionId, ownershipToken: OwnershipToken) =>
|
|
331
|
+
Ref.update(registry, (tracked) => new Map(tracked).set(submissionId, ownershipToken));
|
|
332
|
+
|
|
333
|
+
const untrack = (submissionId: SubmissionId) =>
|
|
334
|
+
Ref.update(registry, (tracked) => {
|
|
335
|
+
const next = new Map(tracked);
|
|
336
|
+
|
|
337
|
+
next.delete(submissionId);
|
|
338
|
+
|
|
339
|
+
return next;
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
yield* Effect.addFinalizer(() => releaseTrackedOwnership(ledger, registry));
|
|
343
|
+
|
|
344
|
+
return SubmissionLedger.of({
|
|
345
|
+
capabilities: ledger.capabilities,
|
|
346
|
+
admit: ledger.admit,
|
|
347
|
+
markReady: ledger.markReady,
|
|
348
|
+
lookup: ledger.lookup,
|
|
349
|
+
// The S2 subagent ops forward untouched: none of them grants an ownership period, so
|
|
350
|
+
// the drain has nothing to track for them (`suspend` below already stops tracking the
|
|
351
|
+
// waitingForChild ownership period the moment it ends).
|
|
352
|
+
resolveAdmission: ledger.resolveAdmission,
|
|
353
|
+
recordChildSettled: ledger.recordChildSettled,
|
|
354
|
+
reserveChildBudget: ledger.reserveChildBudget,
|
|
355
|
+
attachChildToReservation: ledger.attachChildToReservation,
|
|
356
|
+
beginChildBudgetRelease: ledger.beginChildBudgetRelease,
|
|
357
|
+
releaseChildBudget: ledger.releaseChildBudget,
|
|
358
|
+
claim: (request) =>
|
|
359
|
+
ledger
|
|
360
|
+
.claim(request)
|
|
361
|
+
.pipe(
|
|
362
|
+
Effect.tap((claimed) =>
|
|
363
|
+
claimed._tag === "Some"
|
|
364
|
+
? track(claimed.value.submissionId, claimed.value.ownershipToken)
|
|
365
|
+
: Effect.void,
|
|
366
|
+
),
|
|
367
|
+
),
|
|
368
|
+
renewOwnership: (request) =>
|
|
369
|
+
ledger.renewOwnership(request).pipe(
|
|
370
|
+
Effect.tap((renewal) => track(request.submissionId, renewal.ownershipToken)),
|
|
371
|
+
Effect.tapError((error) =>
|
|
372
|
+
error._tag === "OwnershipLost" ? untrack(request.submissionId) : Effect.void,
|
|
373
|
+
),
|
|
374
|
+
),
|
|
375
|
+
releaseOwnership: (request) =>
|
|
376
|
+
ledger.releaseOwnership(request).pipe(
|
|
377
|
+
Effect.tap(() => untrack(request.submissionId)),
|
|
378
|
+
Effect.tapError((error) =>
|
|
379
|
+
error._tag === "OwnershipLost" ? untrack(request.submissionId) : Effect.void,
|
|
380
|
+
),
|
|
381
|
+
),
|
|
382
|
+
markInputApplied: ledger.markInputApplied,
|
|
383
|
+
reserveSettlement: ledger.reserveSettlement,
|
|
384
|
+
finalizeSettlement: (request) =>
|
|
385
|
+
ledger.finalizeSettlement(request).pipe(Effect.tap(() => untrack(request.submissionId))),
|
|
386
|
+
requestAbort: ledger.requestAbort,
|
|
387
|
+
readAbortIntent: ledger.readAbortIntent,
|
|
388
|
+
claimJoining: ledger.claimJoining,
|
|
389
|
+
markJoined: ledger.markJoined,
|
|
390
|
+
revertJoining: ledger.revertJoining,
|
|
391
|
+
// Suspension ends the ownership period by contract, so the drain stops tracking it.
|
|
392
|
+
suspend: (request) =>
|
|
393
|
+
ledger.suspend(request).pipe(Effect.tap(() => untrack(request.submissionId))),
|
|
394
|
+
recordApprovalDecision: ledger.recordApprovalDecision,
|
|
395
|
+
markUnknown: ledger.markUnknown,
|
|
396
|
+
recordUnknownResolution: ledger.recordUnknownResolution,
|
|
397
|
+
scanNonterminal: ledger.scanNonterminal,
|
|
398
|
+
loadRecoverySnapshot: ledger.loadRecoverySnapshot,
|
|
399
|
+
});
|
|
400
|
+
}),
|
|
401
|
+
);
|
|
402
|
+
|
|
403
|
+
/**
|
|
404
|
+
* The DN Layer assembly (deployment §12: a Layer-assembly library, not an app entrypoint).
|
|
405
|
+
* `layer(options)` decodes the configuration, opens ONE SQLite database serving both the
|
|
406
|
+
* Thread Log and the Submission Ledger (so claims fence the same producer epochs), wires
|
|
407
|
+
* the Node wake scheduler with its ledger-scan fallback, exposes independent message delivery
|
|
408
|
+
* storage, wraps the ledger with the shutdown
|
|
409
|
+
* ownership drain, defaults the Tool reconciliation policy to the fail-closed
|
|
410
|
+
* `ToolReconciler.uncertain` (override via `options.toolReconciler`), and provides a ready
|
|
411
|
+
* `DurableAgentRuntime` on top. Storage compatibility is
|
|
412
|
+
* verified during construction: an incompatible database file fails the Layer with
|
|
413
|
+
* `SqliteStorageCompatibilityError` before anything is mutated (DEPLOY-008).
|
|
414
|
+
*/
|
|
415
|
+
export class NodeDurableAgentRuntime {
|
|
416
|
+
/** Validated configuration Layer; fails typed when the supplied options are out of bounds. */
|
|
417
|
+
static configLayer<
|
|
418
|
+
ContextError = never,
|
|
419
|
+
ContextRequirements = never,
|
|
420
|
+
AuthorizationError = never,
|
|
421
|
+
AuthorizationRequirements = never,
|
|
422
|
+
ReconcilerError = never,
|
|
423
|
+
ReconcilerRequirements = never,
|
|
424
|
+
>(
|
|
425
|
+
options: NodeDurableAgentRuntimeOptions<
|
|
426
|
+
ContextError,
|
|
427
|
+
ContextRequirements,
|
|
428
|
+
AuthorizationError,
|
|
429
|
+
AuthorizationRequirements,
|
|
430
|
+
ReconcilerError,
|
|
431
|
+
ReconcilerRequirements
|
|
432
|
+
>,
|
|
433
|
+
): Layer.Layer<NodeDurableAgentRuntimeConfig, NodePlatformConfigError> {
|
|
434
|
+
return Layer.effect(NodeDurableAgentRuntimeConfig)(configFromOptions(options));
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
/** The full DN runtime stack over one SQLite file. */
|
|
438
|
+
static layer<
|
|
439
|
+
ContextError = never,
|
|
440
|
+
ContextRequirements = never,
|
|
441
|
+
AuthorizationError = never,
|
|
442
|
+
AuthorizationRequirements = never,
|
|
443
|
+
ReconcilerError = never,
|
|
444
|
+
ReconcilerRequirements = never,
|
|
445
|
+
>(
|
|
446
|
+
options: NodeDurableAgentRuntimeOptions<
|
|
447
|
+
ContextError,
|
|
448
|
+
ContextRequirements,
|
|
449
|
+
AuthorizationError,
|
|
450
|
+
AuthorizationRequirements,
|
|
451
|
+
ReconcilerError,
|
|
452
|
+
ReconcilerRequirements
|
|
453
|
+
>,
|
|
454
|
+
) {
|
|
455
|
+
return NodeDurableAgentRuntime.assemble(DurableAgentRuntime.layerWithServices, options);
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
/** Own typed executable registrations for every worker using this Node runtime. */
|
|
459
|
+
static layerRegistered<
|
|
460
|
+
const Entries extends ReadonlyArray<AgentRegistration>,
|
|
461
|
+
ContextError = never,
|
|
462
|
+
ContextRequirements = never,
|
|
463
|
+
AuthorizationError = never,
|
|
464
|
+
AuthorizationRequirements = never,
|
|
465
|
+
ReconcilerError = never,
|
|
466
|
+
ReconcilerRequirements = never,
|
|
467
|
+
>(
|
|
468
|
+
registrations: Entries,
|
|
469
|
+
options: NodeDurableAgentRuntimeOptions<
|
|
470
|
+
ContextError,
|
|
471
|
+
ContextRequirements,
|
|
472
|
+
AuthorizationError,
|
|
473
|
+
AuthorizationRequirements,
|
|
474
|
+
ReconcilerError,
|
|
475
|
+
ReconcilerRequirements
|
|
476
|
+
>,
|
|
477
|
+
) {
|
|
478
|
+
return NodeDurableAgentRuntime.assemble(
|
|
479
|
+
DurableAgentRuntime.layerRegistered(registrations),
|
|
480
|
+
options,
|
|
481
|
+
);
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
/** Construct from registrations already resolved within the enclosing application Scope. */
|
|
485
|
+
static layerWithBindings<
|
|
486
|
+
ContextError = never,
|
|
487
|
+
ContextRequirements = never,
|
|
488
|
+
AuthorizationError = never,
|
|
489
|
+
AuthorizationRequirements = never,
|
|
490
|
+
ReconcilerError = never,
|
|
491
|
+
ReconcilerRequirements = never,
|
|
492
|
+
>(
|
|
493
|
+
bindings: ReadonlyArray<ResolvedBinding>,
|
|
494
|
+
options: NodeDurableAgentRuntimeOptions<
|
|
495
|
+
ContextError,
|
|
496
|
+
ContextRequirements,
|
|
497
|
+
AuthorizationError,
|
|
498
|
+
AuthorizationRequirements,
|
|
499
|
+
ReconcilerError,
|
|
500
|
+
ReconcilerRequirements
|
|
501
|
+
>,
|
|
502
|
+
) {
|
|
503
|
+
return NodeDurableAgentRuntime.assemble(
|
|
504
|
+
DurableAgentRuntime.layerWithBindings(bindings),
|
|
505
|
+
options,
|
|
506
|
+
);
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
private static assemble<
|
|
510
|
+
RuntimeError,
|
|
511
|
+
RuntimeRequirements,
|
|
512
|
+
ContextError,
|
|
513
|
+
ContextRequirements,
|
|
514
|
+
AuthorizationError,
|
|
515
|
+
AuthorizationRequirements,
|
|
516
|
+
ReconcilerError,
|
|
517
|
+
ReconcilerRequirements,
|
|
518
|
+
>(
|
|
519
|
+
runtimeLayer: Layer.Layer<DurableAgentRuntime, RuntimeError, RuntimeRequirements>,
|
|
520
|
+
options: NodeDurableAgentRuntimeOptions<
|
|
521
|
+
ContextError,
|
|
522
|
+
ContextRequirements,
|
|
523
|
+
AuthorizationError,
|
|
524
|
+
AuthorizationRequirements,
|
|
525
|
+
ReconcilerError,
|
|
526
|
+
ReconcilerRequirements
|
|
527
|
+
>,
|
|
528
|
+
) {
|
|
529
|
+
const assembled = Layer.unwrap(
|
|
530
|
+
Effect.map(configFromOptions(options), (config) => {
|
|
531
|
+
const nodeConfigLayer = Layer.succeed(NodeDurableAgentRuntimeConfig)(config);
|
|
532
|
+
|
|
533
|
+
const infrastructure = Layer.mergeAll(
|
|
534
|
+
sqliteStorageConfigLayer,
|
|
535
|
+
storageFailpointLayer({
|
|
536
|
+
filename: config.filename,
|
|
537
|
+
failpoint: options.storageFailpoint,
|
|
538
|
+
}),
|
|
539
|
+
SqliteClient.layer({ filename: config.filename }),
|
|
540
|
+
NodeCrypto.layer,
|
|
541
|
+
);
|
|
542
|
+
|
|
543
|
+
const runtimeFailpointLayer =
|
|
544
|
+
options.runtimeFailpoint === undefined
|
|
545
|
+
? DurableRuntimeFailpoint.layer
|
|
546
|
+
: Layer.succeed(DurableRuntimeFailpoint)({ hit: options.runtimeFailpoint });
|
|
547
|
+
|
|
548
|
+
const reconcilerLayer = options.toolReconciler ?? ToolReconciler.uncertain;
|
|
549
|
+
|
|
550
|
+
const observerLayer =
|
|
551
|
+
options.toolFailureObserver === undefined
|
|
552
|
+
? Layer.succeed(CurrentToolFailureObserver)(undefined)
|
|
553
|
+
: toolFailureObserverLayer(options.toolFailureObserver);
|
|
554
|
+
|
|
555
|
+
const ports = Layer.mergeAll(
|
|
556
|
+
threadStoreLayer,
|
|
557
|
+
scheduleStoreLayer,
|
|
558
|
+
messageDeliveryStoreLayer(),
|
|
559
|
+
nodeWakeSchedulerLayer.pipe(
|
|
560
|
+
Layer.provideMerge(ownershipDrainLayer.pipe(Layer.provide(submissionLedgerLayer))),
|
|
561
|
+
),
|
|
562
|
+
);
|
|
563
|
+
|
|
564
|
+
return runtimeLayer.pipe(
|
|
565
|
+
Layer.provideMerge(
|
|
566
|
+
Layer.mergeAll(ports, durableRuntimeConfigLayer(options.estimateCostMicrousd)),
|
|
567
|
+
),
|
|
568
|
+
Layer.provide(
|
|
569
|
+
Layer.mergeAll(
|
|
570
|
+
wakeSchedulerConfigLayer,
|
|
571
|
+
runtimeFailpointLayer,
|
|
572
|
+
reconcilerLayer,
|
|
573
|
+
observerLayer,
|
|
574
|
+
),
|
|
575
|
+
),
|
|
576
|
+
Layer.provideMerge(infrastructure),
|
|
577
|
+
Layer.provideMerge(nodeConfigLayer),
|
|
578
|
+
Layer.provide(
|
|
579
|
+
Layer.mergeAll(
|
|
580
|
+
options.runContext ?? RunContextPreparationPassthrough,
|
|
581
|
+
options.toolAuthorization ?? RunToolAuthorization.allowAll,
|
|
582
|
+
).pipe(Layer.provide(NodeCrypto.layer)),
|
|
583
|
+
),
|
|
584
|
+
);
|
|
585
|
+
}),
|
|
586
|
+
);
|
|
587
|
+
|
|
588
|
+
const runtime: Layer.Layer<
|
|
589
|
+
NodeDurableAgentRuntimeServices,
|
|
590
|
+
Layer.Error<typeof assembled>,
|
|
591
|
+
Layer.Services<typeof assembled>
|
|
592
|
+
> = assembled;
|
|
593
|
+
|
|
594
|
+
return runtime;
|
|
595
|
+
}
|
|
596
|
+
}
|