@systemfsoftware/agent-platform-node 0.0.0-stage → 0.1.0-beta.192
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/LICENSE +21 -0
- package/dist/NodeDurableAgentRuntime-C5SFn9kf.mjs +218 -0
- package/dist/NodeDurableAgentRuntime-C5SFn9kf.mjs.map +1 -0
- package/dist/NodeDurableAgentRuntime-CUrOl39W.d.mts +163 -0
- package/dist/NodeDurableAgentRuntime.d.mts +2 -0
- package/dist/NodeDurableAgentRuntime.mjs +1 -0
- package/dist/NodeDurableHost-BztkzCGh.mjs +271 -0
- package/dist/NodeDurableHost-BztkzCGh.mjs.map +1 -0
- package/dist/NodeDurableHost-CFvVn1w1.d.mts +153 -0
- package/dist/NodeDurableHost.d.mts +2 -0
- package/dist/NodeDurableHost.mjs +1 -0
- package/dist/NodeScheduling-CD_CZvQ6.d.mts +20 -0
- package/dist/NodeScheduling-gNj8jB4g.mjs +64 -0
- package/dist/NodeScheduling-gNj8jB4g.mjs.map +1 -0
- package/dist/NodeScheduling.d.mts +2 -0
- package/dist/NodeScheduling.mjs +1 -0
- package/dist/NodeSubscriptions-Bd_3VxqD.d.mts +23 -0
- package/dist/NodeSubscriptions-KgvkfIMl.mjs +62 -0
- package/dist/NodeSubscriptions-KgvkfIMl.mjs.map +1 -0
- package/dist/NodeSubscriptions.d.mts +2 -0
- package/dist/NodeSubscriptions.mjs +1 -0
- package/dist/NodeWakeScheduler-Dyha4GZJ.d.mts +28 -0
- package/dist/NodeWakeScheduler-dVNAHWsN.mjs +58 -0
- package/dist/NodeWakeScheduler-dVNAHWsN.mjs.map +1 -0
- package/dist/NodeWakeScheduler.d.mts +2 -0
- package/dist/NodeWakeScheduler.mjs +1 -0
- package/dist/index.d.mts +6 -0
- package/dist/index.mjs +6 -0
- package/dist/rolldown-runtime-D3KUYPsf.mjs +12 -0
- package/package.json +68 -3
- package/src/NodeDurableAgentRuntime.ts +603 -0
- package/src/NodeDurableHost.ts +403 -0
- package/src/NodeScheduling.ts +122 -0
- package/src/NodeSubscriptions.ts +124 -0
- package/src/NodeWakeScheduler.ts +94 -0
- package/src/index.ts +5 -0
- package/src/internal/application-context.ts +25 -0
- package/src/internal/exclusive-host.ts +7 -0
- package/src/internal/message-delivery.ts +54 -0
- package/src/internal/prepared-admission.ts +79 -0
- package/src/internal/worker-dispatch.ts +116 -0
- package/README.md +0 -3
|
@@ -0,0 +1,603 @@
|
|
|
1
|
+
import * as NodeCrypto from '@effect/platform-node/NodeCrypto'
|
|
2
|
+
import * as SqliteClient from '@effect/sql-sqlite-node/SqliteClient'
|
|
3
|
+
import { messageDeliveryStoreLayer } from '@systemfsoftware/agent-storage-sqlite/sqlite-message-delivery-store'
|
|
4
|
+
import { scheduleStoreLayer } from '@systemfsoftware/agent-storage-sqlite/sqlite-schedule-store'
|
|
5
|
+
import {
|
|
6
|
+
SqliteStorageConfig,
|
|
7
|
+
SqliteStorageConfigValue,
|
|
8
|
+
} from '@systemfsoftware/agent-storage-sqlite/sqlite-storage-config'
|
|
9
|
+
import { SqliteStorageError } from '@systemfsoftware/agent-storage-sqlite/sqlite-storage-error'
|
|
10
|
+
import {
|
|
11
|
+
SqliteStorageFailpoint,
|
|
12
|
+
type SqliteStorageFailpointHandler,
|
|
13
|
+
} from '@systemfsoftware/agent-storage-sqlite/sqlite-storage-failpoint'
|
|
14
|
+
import { submissionLedgerLayer } from '@systemfsoftware/agent-storage-sqlite/sqlite-submission-ledger'
|
|
15
|
+
import {
|
|
16
|
+
exclusiveHostClientLayer,
|
|
17
|
+
exclusiveRunStorageLayer,
|
|
18
|
+
type SqliteStorageInitializationError,
|
|
19
|
+
storageFailpointLayer,
|
|
20
|
+
threadStoreLayer,
|
|
21
|
+
} from '@systemfsoftware/agent-storage-sqlite/sqlite-thread-store'
|
|
22
|
+
import {
|
|
23
|
+
type AgentRegistration,
|
|
24
|
+
compileRegistrations,
|
|
25
|
+
type ResolvedBinding,
|
|
26
|
+
} from '@systemfsoftware/agent/agent-registration'
|
|
27
|
+
import { DurableAgentRuntime, DurableRuntimeConfig } from '@systemfsoftware/agent/durable-agent-runtime'
|
|
28
|
+
import { DurableRuntimeFailpoint, type DurableRuntimeFailpointHandler } from '@systemfsoftware/agent/durable-failpoint'
|
|
29
|
+
import { type MessageDeliveryError, MessageDeliveryStore } from '@systemfsoftware/agent/message-delivery'
|
|
30
|
+
import { DeploymentId, ProducerId } from '@systemfsoftware/agent/records'
|
|
31
|
+
import {
|
|
32
|
+
CurrentToolFailureObserver,
|
|
33
|
+
type RunContextPreparation,
|
|
34
|
+
RunContextPreparationPassthrough,
|
|
35
|
+
type RunCostEstimator,
|
|
36
|
+
RunToolAuthorization,
|
|
37
|
+
type RunToolFailureObserver,
|
|
38
|
+
toolFailureObserverLayer,
|
|
39
|
+
} from '@systemfsoftware/agent/run-options'
|
|
40
|
+
import { layer as runStorageLayer } from '@systemfsoftware/agent/run-storage'
|
|
41
|
+
import { ThreadReader } from '@systemfsoftware/agent/thread-store'
|
|
42
|
+
import { ToolReconciler } from '@systemfsoftware/agent/tool-reconciler'
|
|
43
|
+
import * as Context from 'effect/Context'
|
|
44
|
+
import * as CryptoDigest from 'effect/crypto/Digest'
|
|
45
|
+
import type * as SecureRandom from 'effect/crypto/SecureRandom'
|
|
46
|
+
import * as Duration from 'effect/Duration'
|
|
47
|
+
import * as Effect from 'effect/Effect'
|
|
48
|
+
import * as Layer from 'effect/Layer'
|
|
49
|
+
import * as Schema from 'effect/Schema'
|
|
50
|
+
import * as Scope from 'effect/Scope'
|
|
51
|
+
import * as SqlClientService from 'effect/sql/SqlClient'
|
|
52
|
+
import * as Statement from 'effect/sql/Statement'
|
|
53
|
+
|
|
54
|
+
import { applicationInvocationContext } from './internal/application-context'
|
|
55
|
+
import { ExclusiveSqliteHost } from './internal/exclusive-host'
|
|
56
|
+
import { NodeWakeSchedulerConfig, nodeWakeSchedulerLayer } from './NodeWakeScheduler'
|
|
57
|
+
|
|
58
|
+
const PositiveMillis = Schema.Int.check(Schema.isGreaterThan(0))
|
|
59
|
+
const NonNegativeMillis = Schema.Int.check(Schema.isGreaterThanOrEqualTo(0))
|
|
60
|
+
|
|
61
|
+
const WorkerConcurrency = Schema.Int.check(
|
|
62
|
+
Schema.isGreaterThanOrEqualTo(1),
|
|
63
|
+
Schema.isLessThanOrEqualTo(64),
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
/** The supplied Node durable runtime configuration failed schema validation (DEPLOY-003). */
|
|
67
|
+
export class NodePlatformConfigError extends Schema.TaggedError<NodePlatformConfigError>()(
|
|
68
|
+
'NodePlatformConfigError',
|
|
69
|
+
{
|
|
70
|
+
message: Schema.String,
|
|
71
|
+
cause: Schema.optionalKey(Schema.Defect()),
|
|
72
|
+
},
|
|
73
|
+
) {}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Validated Node durable runtime configuration (deployment §4: decoded once during Layer
|
|
77
|
+
* construction, exposed as a typed service). Every cadence is in milliseconds and every bound is
|
|
78
|
+
* finite; `workerConcurrency` caps how many worker loops `NodeDurableHost.runWorkers` drives.
|
|
79
|
+
*/
|
|
80
|
+
export class NodeDurableAgentRuntimeConfigValue extends Schema.Class<NodeDurableAgentRuntimeConfigValue>(
|
|
81
|
+
'@effect-agent/platform-node/NodeDurableAgentRuntimeConfigValue',
|
|
82
|
+
)({
|
|
83
|
+
/** SQLite database file backing the Thread Log, Submission Ledger, and delivery obligations. */
|
|
84
|
+
filename: Schema.NonEmptyString,
|
|
85
|
+
deploymentId: DeploymentId,
|
|
86
|
+
producerId: ProducerId,
|
|
87
|
+
/** Finite bound on concurrent worker loops per host (rule 10). */
|
|
88
|
+
workerConcurrency: WorkerConcurrency,
|
|
89
|
+
/** Fallback scan cadence for the Node wake scheduler and independent message delivery recovery. */
|
|
90
|
+
wakeScanInterval: PositiveMillis,
|
|
91
|
+
/** `awaitSettlement` ledger re-check cadence when no wake arrives. */
|
|
92
|
+
settlementPollInterval: PositiveMillis,
|
|
93
|
+
/** Active-Run abort-intent poll cadence. */
|
|
94
|
+
abortPollInterval: PositiveMillis,
|
|
95
|
+
/** Bounded SQLITE_BUSY retry window for write-lock acquisition. */
|
|
96
|
+
busyTimeout: NonNegativeMillis,
|
|
97
|
+
/** SQLite WAL synchronization selected for this host's connection at construction. */
|
|
98
|
+
synchronous: SqliteStorageConfigValue.fields.synchronous,
|
|
99
|
+
/** Canonical observation poll cadence of the SQLite store. */
|
|
100
|
+
observationPollInterval: NonNegativeMillis,
|
|
101
|
+
/** Opt-in full history and active-state validation while opening the store. */
|
|
102
|
+
verifyOnOpen: Schema.Boolean,
|
|
103
|
+
}) {}
|
|
104
|
+
|
|
105
|
+
/** Explicit configuration authority for the assembled Node durable runtime. */
|
|
106
|
+
export class NodeDurableAgentRuntimeConfig extends Context.Service<
|
|
107
|
+
NodeDurableAgentRuntimeConfig,
|
|
108
|
+
NodeDurableAgentRuntimeConfigValue
|
|
109
|
+
>()('@effect-agent/platform-node/NodeDurableAgentRuntimeConfig') {}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Raw (unvalidated) construction options for `NodeDurableAgentRuntime.layer`. Optional fields default
|
|
113
|
+
* to the documented production values; everything is schema-decoded into
|
|
114
|
+
* `NodeDurableAgentRuntimeConfigValue` before any resource opens (deployment §5 gate 1).
|
|
115
|
+
*/
|
|
116
|
+
export interface NodeDurableAgentRuntimeOptions<
|
|
117
|
+
ContextError = never,
|
|
118
|
+
ContextRequirements = never,
|
|
119
|
+
AuthorizationError = never,
|
|
120
|
+
AuthorizationRequirements = never,
|
|
121
|
+
ReconcilerError = never,
|
|
122
|
+
ReconcilerRequirements = never,
|
|
123
|
+
> {
|
|
124
|
+
readonly filename: string
|
|
125
|
+
readonly deploymentId: string
|
|
126
|
+
readonly producerId: string
|
|
127
|
+
/** Default 1; bounded to 1..64. */
|
|
128
|
+
readonly workerConcurrency?: number | undefined
|
|
129
|
+
/** Milliseconds; default 1000. */
|
|
130
|
+
readonly wakeScanInterval?: number | undefined
|
|
131
|
+
/** Milliseconds; default 500. */
|
|
132
|
+
readonly settlementPollInterval?: number | undefined
|
|
133
|
+
/** Milliseconds; default 500. */
|
|
134
|
+
readonly abortPollInterval?: number | undefined
|
|
135
|
+
/** Deployment-owned pricing authority used by durable cost budgets and settlements. */
|
|
136
|
+
readonly estimateCostMicrousd?: RunCostEstimator | undefined
|
|
137
|
+
/** Closed trusted Tool failure reporting. Omission masks ambient observers at construction. */
|
|
138
|
+
readonly toolFailureObserver?: RunToolFailureObserver | undefined
|
|
139
|
+
/** Milliseconds; default 5000. */
|
|
140
|
+
readonly busyTimeout?: number | undefined
|
|
141
|
+
/**
|
|
142
|
+
* Defaults to FULL. NORMAL can lose acknowledged commits after power loss or an OS crash,
|
|
143
|
+
* potentially repeating external effects. The choice belongs to this host's SQLite connection.
|
|
144
|
+
*/
|
|
145
|
+
readonly synchronous?: 'FULL' | 'NORMAL' | undefined
|
|
146
|
+
/** Milliseconds; default 25. */
|
|
147
|
+
readonly observationPollInterval?: number | undefined
|
|
148
|
+
/** Default false. */
|
|
149
|
+
readonly verifyOnOpen?: boolean | undefined
|
|
150
|
+
/** SQLite adapter fault injection (`ledger:*` / `append:*` locations); default none. */
|
|
151
|
+
readonly storageFailpoint?: SqliteStorageFailpointHandler | undefined
|
|
152
|
+
/** Coordinator fault injection (`submit:*` / `terminalize:*` locations); default none. */
|
|
153
|
+
readonly runtimeFailpoint?: DurableRuntimeFailpointHandler | undefined
|
|
154
|
+
/**
|
|
155
|
+
* Reconciliation policy consulted for open ordinary Tool Calls before an Unknown Outcome is
|
|
156
|
+
* recorded (durability §10, DUR-009). Defaults to the fail-closed `ToolReconciler.uncertain`:
|
|
157
|
+
* with no registered policy, every open call stays Unknown and routes to the authorized
|
|
158
|
+
* DUR-017 resolution path. Construction errors and application dependencies remain in the
|
|
159
|
+
* assembled Layer's E and R; the platform supplies Digest and SecureRandom.
|
|
160
|
+
*/
|
|
161
|
+
readonly toolReconciler?:
|
|
162
|
+
| Layer.Layer<
|
|
163
|
+
ToolReconciler,
|
|
164
|
+
ReconcilerError,
|
|
165
|
+
ReconcilerRequirements | CryptoDigest.Digest | SecureRandom.SecureRandom
|
|
166
|
+
>
|
|
167
|
+
| undefined
|
|
168
|
+
/** Host prompt preparation, acquired once with the runtime; default pass-through. */
|
|
169
|
+
readonly runContext?:
|
|
170
|
+
| Layer.Layer<
|
|
171
|
+
RunContextPreparation,
|
|
172
|
+
ContextError,
|
|
173
|
+
ContextRequirements | CryptoDigest.Digest | SecureRandom.SecureRandom
|
|
174
|
+
>
|
|
175
|
+
| undefined
|
|
176
|
+
/**
|
|
177
|
+
* Independent action-time Tool authority, acquired once with the runtime; default allow-all.
|
|
178
|
+
* Construction errors and application dependencies remain in the assembled Layer's E and R.
|
|
179
|
+
* The platform supplies Digest and SecureRandom to these extension Layers.
|
|
180
|
+
*/
|
|
181
|
+
readonly toolAuthorization?:
|
|
182
|
+
| Layer.Layer<
|
|
183
|
+
RunToolAuthorization,
|
|
184
|
+
AuthorizationError,
|
|
185
|
+
AuthorizationRequirements | CryptoDigest.Digest | SecureRandom.SecureRandom
|
|
186
|
+
>
|
|
187
|
+
| undefined
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** Built-in construction failures. `layer` also preserves supplied service Layers' errors. */
|
|
191
|
+
export type NodeDurableAgentRuntimeInitializationError =
|
|
192
|
+
| NodePlatformConfigError
|
|
193
|
+
| MessageDeliveryError
|
|
194
|
+
| SqliteStorageInitializationError
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* The services `NodeDurableAgentRuntime.layer` provides. Additional SQLite adapters can use
|
|
198
|
+
* the same client and storage configuration, sharing its serialized connection and Scope.
|
|
199
|
+
*/
|
|
200
|
+
export type NodeDurableAgentRuntimeServices = Layer.Success<
|
|
201
|
+
ReturnType<typeof NodeDurableAgentRuntime.layer>
|
|
202
|
+
>
|
|
203
|
+
|
|
204
|
+
const decodeConfigValue = Schema.decodeUnknownEffect(NodeDurableAgentRuntimeConfigValue)
|
|
205
|
+
|
|
206
|
+
const configFromOptions = (
|
|
207
|
+
options: Omit<
|
|
208
|
+
NodeDurableAgentRuntimeOptions,
|
|
209
|
+
'runContext' | 'toolAuthorization' | 'toolReconciler'
|
|
210
|
+
>,
|
|
211
|
+
): Effect.Effect<NodeDurableAgentRuntimeConfigValue, NodePlatformConfigError> =>
|
|
212
|
+
decodeConfigValue({
|
|
213
|
+
filename: options.filename,
|
|
214
|
+
deploymentId: options.deploymentId,
|
|
215
|
+
producerId: options.producerId,
|
|
216
|
+
workerConcurrency: options.workerConcurrency ?? 1,
|
|
217
|
+
wakeScanInterval: options.wakeScanInterval ?? 1_000,
|
|
218
|
+
settlementPollInterval: options.settlementPollInterval ?? 500,
|
|
219
|
+
abortPollInterval: options.abortPollInterval ?? 500,
|
|
220
|
+
busyTimeout: options.busyTimeout ?? 5_000,
|
|
221
|
+
synchronous: options.synchronous ?? 'FULL',
|
|
222
|
+
observationPollInterval: options.observationPollInterval ?? 25,
|
|
223
|
+
verifyOnOpen: options.verifyOnOpen ?? false,
|
|
224
|
+
}).pipe(
|
|
225
|
+
Effect.mapError((error) =>
|
|
226
|
+
NodePlatformConfigError.make({
|
|
227
|
+
message: `Invalid Node durable runtime configuration: ${error.message}`,
|
|
228
|
+
cause: error,
|
|
229
|
+
})
|
|
230
|
+
),
|
|
231
|
+
)
|
|
232
|
+
|
|
233
|
+
/** SQLite storage configuration derived from the single validated Node configuration. */
|
|
234
|
+
const sqliteStorageConfigLayer: Layer.Layer<
|
|
235
|
+
SqliteStorageConfig,
|
|
236
|
+
never,
|
|
237
|
+
NodeDurableAgentRuntimeConfig
|
|
238
|
+
> = Layer.effect(SqliteStorageConfig)(
|
|
239
|
+
Effect.gen(function*() {
|
|
240
|
+
const config = yield* NodeDurableAgentRuntimeConfig
|
|
241
|
+
|
|
242
|
+
return SqliteStorageConfigValue.make({
|
|
243
|
+
observationPollInterval: config.observationPollInterval,
|
|
244
|
+
busyTimeout: config.busyTimeout,
|
|
245
|
+
synchronous: config.synchronous,
|
|
246
|
+
verifyOnOpen: config.verifyOnOpen,
|
|
247
|
+
})
|
|
248
|
+
}),
|
|
249
|
+
)
|
|
250
|
+
|
|
251
|
+
/** Thread coordinator configuration derived from the single validated Node configuration. */
|
|
252
|
+
const durableRuntimeConfigLayer = (
|
|
253
|
+
estimateCostMicrousd: RunCostEstimator | undefined,
|
|
254
|
+
): Layer.Layer<DurableRuntimeConfig, never, NodeDurableAgentRuntimeConfig> =>
|
|
255
|
+
Layer.effect(DurableRuntimeConfig)(
|
|
256
|
+
Effect.gen(function*() {
|
|
257
|
+
const config = yield* NodeDurableAgentRuntimeConfig
|
|
258
|
+
|
|
259
|
+
return DurableRuntimeConfig.make({
|
|
260
|
+
deploymentId: config.deploymentId,
|
|
261
|
+
producerId: config.producerId,
|
|
262
|
+
settlementPollInterval: Duration.millis(config.settlementPollInterval),
|
|
263
|
+
abortPollInterval: Duration.millis(config.abortPollInterval),
|
|
264
|
+
...(estimateCostMicrousd === undefined ? {} : { estimateCostMicrousd }),
|
|
265
|
+
})
|
|
266
|
+
}),
|
|
267
|
+
)
|
|
268
|
+
|
|
269
|
+
/** Wake fallback-scan cadence derived from the single validated Node configuration. */
|
|
270
|
+
const wakeSchedulerConfigLayer: Layer.Layer<
|
|
271
|
+
NodeWakeSchedulerConfig,
|
|
272
|
+
never,
|
|
273
|
+
NodeDurableAgentRuntimeConfig
|
|
274
|
+
> = Layer.effect(NodeWakeSchedulerConfig)(
|
|
275
|
+
Effect.gen(function*() {
|
|
276
|
+
const config = yield* NodeDurableAgentRuntimeConfig
|
|
277
|
+
|
|
278
|
+
return { scanInterval: Duration.millis(config.wakeScanInterval) }
|
|
279
|
+
}),
|
|
280
|
+
)
|
|
281
|
+
|
|
282
|
+
/** The managed database stays private even when a caller supplies SQL transformers. */
|
|
283
|
+
const privateMessageDeliveryLayer = Layer.effect(MessageDeliveryStore)(
|
|
284
|
+
Effect.gen(function*() {
|
|
285
|
+
const storageContext = (yield* Effect.context<
|
|
286
|
+
| SqlClientService.SqlClient
|
|
287
|
+
| CryptoDigest.Digest
|
|
288
|
+
| SqliteStorageConfig
|
|
289
|
+
| SqliteStorageFailpoint
|
|
290
|
+
| Scope.Scope
|
|
291
|
+
>()).pipe(
|
|
292
|
+
Context.pick(
|
|
293
|
+
SqlClientService.SqlClient,
|
|
294
|
+
CryptoDigest.Digest,
|
|
295
|
+
SqliteStorageConfig,
|
|
296
|
+
SqliteStorageFailpoint,
|
|
297
|
+
Scope.Scope,
|
|
298
|
+
),
|
|
299
|
+
)
|
|
300
|
+
|
|
301
|
+
const sql = Context.get(storageContext, SqlClientService.SqlClient)
|
|
302
|
+
|
|
303
|
+
const bind = <A, E>(
|
|
304
|
+
effect: Effect.Effect<
|
|
305
|
+
A,
|
|
306
|
+
E,
|
|
307
|
+
| SqlClientService.SqlClient
|
|
308
|
+
| CryptoDigest.Digest
|
|
309
|
+
| SqliteStorageConfig
|
|
310
|
+
| SqliteStorageFailpoint
|
|
311
|
+
| Scope.Scope
|
|
312
|
+
>,
|
|
313
|
+
) =>
|
|
314
|
+
Effect.contextWith((live: Context.Context<never>) =>
|
|
315
|
+
Effect.setContext(
|
|
316
|
+
effect,
|
|
317
|
+
Context.merge(
|
|
318
|
+
Context.omit(Statement.CurrentTransformer, sql.transactionService)(live),
|
|
319
|
+
storageContext,
|
|
320
|
+
),
|
|
321
|
+
)
|
|
322
|
+
)
|
|
323
|
+
|
|
324
|
+
const services = yield* bind(Layer.build(messageDeliveryStoreLayer()))
|
|
325
|
+
|
|
326
|
+
const store = Context.get(services, MessageDeliveryStore)
|
|
327
|
+
|
|
328
|
+
return MessageDeliveryStore.of({
|
|
329
|
+
limits: store.limits,
|
|
330
|
+
maxStoredValueBytes: store.maxStoredValueBytes,
|
|
331
|
+
insert: (record) => bind(store.insert(record)),
|
|
332
|
+
get: (key) => bind(store.get(key)),
|
|
333
|
+
list: (request) => bind(store.list(request)),
|
|
334
|
+
change: (key, change) => bind(store.change(key, change)),
|
|
335
|
+
due: (now, limit, owner) => bind(store.due(now, limit, owner)),
|
|
336
|
+
nextDeadline: (owner) => bind(store.nextDeadline(owner)),
|
|
337
|
+
})
|
|
338
|
+
}),
|
|
339
|
+
)
|
|
340
|
+
|
|
341
|
+
/**
|
|
342
|
+
* The DN Layer assembly (deployment §12: a Layer-assembly library, not an app entrypoint).
|
|
343
|
+
* `layer(options)` decodes the configuration, opens ONE SQLite database serving both the
|
|
344
|
+
* Thread Log and the Submission Ledger under one scoped owner, wires the Node wake scheduler
|
|
345
|
+
* with its ledger-scan fallback, exposes independent message delivery storage, and defaults
|
|
346
|
+
* the Tool reconciliation policy to the fail-closed
|
|
347
|
+
* `ToolReconciler.uncertain` (override via `options.toolReconciler`), and provides a ready
|
|
348
|
+
* `DurableAgentRuntime` on top. Storage compatibility is
|
|
349
|
+
* verified during construction: an incompatible database file fails the Layer with
|
|
350
|
+
* `SqliteStorageCompatibilityError` before anything is mutated (DEPLOY-008).
|
|
351
|
+
*/
|
|
352
|
+
export class NodeDurableAgentRuntime {
|
|
353
|
+
/** Validated configuration Layer; fails typed when the supplied options are out of bounds. */
|
|
354
|
+
static configLayer<
|
|
355
|
+
ContextError = never,
|
|
356
|
+
ContextRequirements = never,
|
|
357
|
+
AuthorizationError = never,
|
|
358
|
+
AuthorizationRequirements = never,
|
|
359
|
+
ReconcilerError = never,
|
|
360
|
+
ReconcilerRequirements = never,
|
|
361
|
+
>(
|
|
362
|
+
options: NodeDurableAgentRuntimeOptions<
|
|
363
|
+
ContextError,
|
|
364
|
+
ContextRequirements,
|
|
365
|
+
AuthorizationError,
|
|
366
|
+
AuthorizationRequirements,
|
|
367
|
+
ReconcilerError,
|
|
368
|
+
ReconcilerRequirements
|
|
369
|
+
>,
|
|
370
|
+
): Layer.Layer<NodeDurableAgentRuntimeConfig, NodePlatformConfigError> {
|
|
371
|
+
return Layer.effect(NodeDurableAgentRuntimeConfig)(configFromOptions(options))
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
/** The full DN runtime stack over one SQLite file. */
|
|
375
|
+
static layer<
|
|
376
|
+
ContextError = never,
|
|
377
|
+
ContextRequirements = never,
|
|
378
|
+
AuthorizationError = never,
|
|
379
|
+
AuthorizationRequirements = never,
|
|
380
|
+
ReconcilerError = never,
|
|
381
|
+
ReconcilerRequirements = never,
|
|
382
|
+
>(
|
|
383
|
+
options: NodeDurableAgentRuntimeOptions<
|
|
384
|
+
ContextError,
|
|
385
|
+
ContextRequirements,
|
|
386
|
+
AuthorizationError,
|
|
387
|
+
AuthorizationRequirements,
|
|
388
|
+
ReconcilerError,
|
|
389
|
+
ReconcilerRequirements
|
|
390
|
+
>,
|
|
391
|
+
) {
|
|
392
|
+
return NodeDurableAgentRuntime.assemble(DurableAgentRuntime.layerWithServices, options)
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
/** Own typed executable registrations for every worker using this Node runtime. */
|
|
396
|
+
static layerRegistered<
|
|
397
|
+
const Entries extends ReadonlyArray<AgentRegistration>,
|
|
398
|
+
ContextError = never,
|
|
399
|
+
ContextRequirements = never,
|
|
400
|
+
AuthorizationError = never,
|
|
401
|
+
AuthorizationRequirements = never,
|
|
402
|
+
ReconcilerError = never,
|
|
403
|
+
ReconcilerRequirements = never,
|
|
404
|
+
>(
|
|
405
|
+
registrations: Entries,
|
|
406
|
+
options: NodeDurableAgentRuntimeOptions<
|
|
407
|
+
ContextError,
|
|
408
|
+
ContextRequirements,
|
|
409
|
+
AuthorizationError,
|
|
410
|
+
AuthorizationRequirements,
|
|
411
|
+
ReconcilerError,
|
|
412
|
+
ReconcilerRequirements
|
|
413
|
+
>,
|
|
414
|
+
) {
|
|
415
|
+
return Layer.unwrap(
|
|
416
|
+
Effect.gen(function*() {
|
|
417
|
+
const bindings = yield* compileRegistrations(registrations)
|
|
418
|
+
const exclusive = yield* ExclusiveSqliteHost
|
|
419
|
+
|
|
420
|
+
// Only the managed Node host owns a private database. Other platforms retain their
|
|
421
|
+
// invocation controls, including the source identity of native maintenance writes.
|
|
422
|
+
const isolate = <A, E>(effect: Effect.Effect<A, E>) =>
|
|
423
|
+
Effect.contextWith((live: Context.Context<never>) =>
|
|
424
|
+
Effect.setContext(effect, applicationInvocationContext(live))
|
|
425
|
+
)
|
|
426
|
+
|
|
427
|
+
return NodeDurableAgentRuntime.layerWithBindings(
|
|
428
|
+
exclusive
|
|
429
|
+
? bindings.map((binding): ResolvedBinding => ({
|
|
430
|
+
...binding,
|
|
431
|
+
attempt: (...args) => isolate(binding.attempt(...args)),
|
|
432
|
+
...(binding.reporting === undefined
|
|
433
|
+
? {}
|
|
434
|
+
: {
|
|
435
|
+
reporting: binding.reporting.map((report) => ({
|
|
436
|
+
...report,
|
|
437
|
+
prepare: (value) => isolate(report.prepare(value)),
|
|
438
|
+
})),
|
|
439
|
+
}),
|
|
440
|
+
}))
|
|
441
|
+
: bindings,
|
|
442
|
+
options,
|
|
443
|
+
)
|
|
444
|
+
}).pipe(Effect.provide(NodeCrypto.layer)),
|
|
445
|
+
)
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
/** Construct from registrations already resolved within the enclosing application Scope. */
|
|
449
|
+
static layerWithBindings<
|
|
450
|
+
ContextError = never,
|
|
451
|
+
ContextRequirements = never,
|
|
452
|
+
AuthorizationError = never,
|
|
453
|
+
AuthorizationRequirements = never,
|
|
454
|
+
ReconcilerError = never,
|
|
455
|
+
ReconcilerRequirements = never,
|
|
456
|
+
>(
|
|
457
|
+
bindings: ReadonlyArray<ResolvedBinding>,
|
|
458
|
+
options: NodeDurableAgentRuntimeOptions<
|
|
459
|
+
ContextError,
|
|
460
|
+
ContextRequirements,
|
|
461
|
+
AuthorizationError,
|
|
462
|
+
AuthorizationRequirements,
|
|
463
|
+
ReconcilerError,
|
|
464
|
+
ReconcilerRequirements
|
|
465
|
+
>,
|
|
466
|
+
) {
|
|
467
|
+
return NodeDurableAgentRuntime.assemble(
|
|
468
|
+
DurableAgentRuntime.layerWithBindings(bindings),
|
|
469
|
+
options,
|
|
470
|
+
)
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
private static assemble<
|
|
474
|
+
RuntimeError,
|
|
475
|
+
RuntimeRequirements,
|
|
476
|
+
ContextError,
|
|
477
|
+
ContextRequirements,
|
|
478
|
+
AuthorizationError,
|
|
479
|
+
AuthorizationRequirements,
|
|
480
|
+
ReconcilerError,
|
|
481
|
+
ReconcilerRequirements,
|
|
482
|
+
>(
|
|
483
|
+
runtimeLayer: Layer.Layer<DurableAgentRuntime, RuntimeError, RuntimeRequirements>,
|
|
484
|
+
options: NodeDurableAgentRuntimeOptions<
|
|
485
|
+
ContextError,
|
|
486
|
+
ContextRequirements,
|
|
487
|
+
AuthorizationError,
|
|
488
|
+
AuthorizationRequirements,
|
|
489
|
+
ReconcilerError,
|
|
490
|
+
ReconcilerRequirements
|
|
491
|
+
>,
|
|
492
|
+
) {
|
|
493
|
+
const assembled = Layer.unwrap(
|
|
494
|
+
Effect.gen(function*() {
|
|
495
|
+
const config = yield* configFromOptions(options)
|
|
496
|
+
const exclusive = yield* ExclusiveSqliteHost
|
|
497
|
+
const nodeConfigLayer = Layer.succeed(NodeDurableAgentRuntimeConfig)(config)
|
|
498
|
+
|
|
499
|
+
// Application extensions are acquired before the private database exists.
|
|
500
|
+
const applicationContext = (yield* Effect.context<never>()).pipe(Context.omit(Scope.Scope))
|
|
501
|
+
|
|
502
|
+
const applicationServices = yield* Layer.build(
|
|
503
|
+
Layer.mergeAll(
|
|
504
|
+
options.runContext ?? RunContextPreparationPassthrough,
|
|
505
|
+
options.toolAuthorization ?? RunToolAuthorization.allowAll,
|
|
506
|
+
options.toolReconciler ?? ToolReconciler.uncertain,
|
|
507
|
+
options.toolFailureObserver === undefined
|
|
508
|
+
? Layer.succeed(CurrentToolFailureObserver)(undefined)
|
|
509
|
+
: toolFailureObserverLayer(options.toolFailureObserver),
|
|
510
|
+
).pipe(Layer.provide(NodeCrypto.layer)),
|
|
511
|
+
)
|
|
512
|
+
|
|
513
|
+
const storageFailpoint = options.storageFailpoint
|
|
514
|
+
const runtimeFailpoint = options.runtimeFailpoint
|
|
515
|
+
|
|
516
|
+
const clientInfrastructure = Layer.mergeAll(
|
|
517
|
+
sqliteStorageConfigLayer,
|
|
518
|
+
storageFailpointLayer({
|
|
519
|
+
filename: config.filename,
|
|
520
|
+
failpoint: storageFailpoint === undefined
|
|
521
|
+
? undefined
|
|
522
|
+
: (location) =>
|
|
523
|
+
Effect.scoped(Effect.suspend(() => storageFailpoint(location))).pipe(
|
|
524
|
+
Effect.setContext(applicationContext),
|
|
525
|
+
),
|
|
526
|
+
}),
|
|
527
|
+
SqliteClient.layer({ filename: config.filename, disableWAL: exclusive }).pipe(
|
|
528
|
+
Layer.catch((cause) =>
|
|
529
|
+
Layer.effectContext<
|
|
530
|
+
Layer.Success<ReturnType<typeof SqliteClient.layer>>,
|
|
531
|
+
SqliteStorageError,
|
|
532
|
+
never
|
|
533
|
+
>(
|
|
534
|
+
Effect.fail(
|
|
535
|
+
SqliteStorageError.make({
|
|
536
|
+
operation: 'open SQLite',
|
|
537
|
+
message: cause.message,
|
|
538
|
+
cause,
|
|
539
|
+
}),
|
|
540
|
+
),
|
|
541
|
+
)
|
|
542
|
+
),
|
|
543
|
+
),
|
|
544
|
+
NodeCrypto.layer,
|
|
545
|
+
).pipe(Layer.provide(nodeConfigLayer))
|
|
546
|
+
|
|
547
|
+
// The managed host no longer composes its database with application SQL hooks.
|
|
548
|
+
// Only the enclosing resource Scope crosses into storage construction.
|
|
549
|
+
const exclusiveInfrastructure = Layer.effectContext(
|
|
550
|
+
Effect.gen(function*() {
|
|
551
|
+
const scope = yield* Effect.scope
|
|
552
|
+
|
|
553
|
+
return yield* Layer.build(
|
|
554
|
+
Layer.fresh(exclusiveHostClientLayer).pipe(Layer.provideMerge(clientInfrastructure)),
|
|
555
|
+
).pipe(Effect.setContext(Context.make(Scope.Scope, scope)))
|
|
556
|
+
}),
|
|
557
|
+
)
|
|
558
|
+
|
|
559
|
+
const runtimeFailpointLayer = runtimeFailpoint === undefined
|
|
560
|
+
? DurableRuntimeFailpoint.layer
|
|
561
|
+
: Layer.succeed(DurableRuntimeFailpoint)({
|
|
562
|
+
hit: (location) =>
|
|
563
|
+
Effect.scoped(Effect.suspend(() => runtimeFailpoint(location))).pipe(
|
|
564
|
+
Effect.setContext(applicationContext),
|
|
565
|
+
),
|
|
566
|
+
})
|
|
567
|
+
|
|
568
|
+
const ordinaryStorage = runStorageLayer.pipe(
|
|
569
|
+
Layer.provideMerge(Layer.mergeAll(threadStoreLayer, submissionLedgerLayer)),
|
|
570
|
+
)
|
|
571
|
+
|
|
572
|
+
const portLayers = Layer.mergeAll(
|
|
573
|
+
ThreadReader.layer(),
|
|
574
|
+
nodeWakeSchedulerLayer,
|
|
575
|
+
exclusive ? privateMessageDeliveryLayer : messageDeliveryStoreLayer(),
|
|
576
|
+
// Custom manual assemblies own schedules; the managed host owns only its runtime.
|
|
577
|
+
exclusive ? Layer.empty : scheduleStoreLayer,
|
|
578
|
+
)
|
|
579
|
+
|
|
580
|
+
const ports = exclusive
|
|
581
|
+
? portLayers.pipe(
|
|
582
|
+
Layer.provideMerge(exclusiveRunStorageLayer),
|
|
583
|
+
Layer.provideMerge(exclusiveInfrastructure),
|
|
584
|
+
)
|
|
585
|
+
: portLayers.pipe(
|
|
586
|
+
Layer.provideMerge(ordinaryStorage),
|
|
587
|
+
Layer.provideMerge(clientInfrastructure),
|
|
588
|
+
)
|
|
589
|
+
|
|
590
|
+
return runtimeLayer.pipe(
|
|
591
|
+
Layer.provideMerge(
|
|
592
|
+
Layer.mergeAll(ports, durableRuntimeConfigLayer(options.estimateCostMicrousd)),
|
|
593
|
+
),
|
|
594
|
+
Layer.provide(Layer.mergeAll(wakeSchedulerConfigLayer, runtimeFailpointLayer)),
|
|
595
|
+
Layer.provideMerge(nodeConfigLayer),
|
|
596
|
+
Layer.provide(Layer.succeedContext(applicationServices)),
|
|
597
|
+
)
|
|
598
|
+
}),
|
|
599
|
+
)
|
|
600
|
+
|
|
601
|
+
return assembled
|
|
602
|
+
}
|
|
603
|
+
}
|