@effect-agent/platform-node 0.1.0-beta.47 → 0.1.0-beta.49
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.mjs +1 -0
- package/dist/NodeDurableAgentRuntime.mjs.map +1 -1
- package/dist/NodeDurableHost.d.mts +44 -3
- package/dist/NodeDurableHost.mjs +28 -8
- package/dist/NodeDurableHost.mjs.map +1 -1
- package/package.json +1 -1
- package/src/NodeDurableAgentRuntime.ts +1 -0
- package/src/NodeDurableHost.ts +59 -9
|
@@ -157,6 +157,7 @@ const ownershipDrainLayer = Layer.effect(SubmissionLedger)(Effect.gen(function*
|
|
|
157
157
|
reserveSettlement: ledger.reserveSettlement,
|
|
158
158
|
finalizeSettlement: (request) => ledger.finalizeSettlement(request).pipe(Effect.tap(() => untrack(request.submissionId))),
|
|
159
159
|
requestAbort: ledger.requestAbort,
|
|
160
|
+
readAbortIntent: ledger.readAbortIntent,
|
|
160
161
|
claimJoining: ledger.claimJoining,
|
|
161
162
|
markJoined: ledger.markJoined,
|
|
162
163
|
revertJoining: ledger.revertJoining,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"NodeDurableAgentRuntime.mjs","names":[],"sources":["../src/NodeDurableAgentRuntime.ts"],"sourcesContent":["import { type SubmissionId } from \"@effect-agent/core/Identifiers\";\nimport {\n CurrentToolFailureObserver,\n RunContextPreparationPassthrough,\n RunToolAuthorization,\n toolFailureObserverLayer,\n type RunContextPreparation,\n type RunCostEstimator,\n type RunToolFailureObserver,\n} from \"@effect-agent/engine/RunOptions\";\nimport { scheduleStoreLayer } from \"@effect-agent/storage-sqlite/SqliteScheduleStore\";\nimport {\n SqliteStorageConfig,\n SqliteStorageConfigValue,\n} from \"@effect-agent/storage-sqlite/SqliteStorageConfig\";\nimport { type SqliteStorageFailpointHandler } from \"@effect-agent/storage-sqlite/SqliteStorageFailpoint\";\nimport { submissionLedgerLayer } from \"@effect-agent/storage-sqlite/SqliteSubmissionLedger\";\nimport {\n threadStoreLayer,\n storageFailpointLayer,\n type SqliteStorageInitializationError,\n} from \"@effect-agent/storage-sqlite/SqliteThreadStore\";\nimport {\n type AgentRegistration,\n type ResolvedBinding,\n} from \"@effect-agent/thread/AgentRegistration\";\nimport {\n DurableAgentRuntime,\n DurableRuntimeConfig,\n} from \"@effect-agent/thread/DurableAgentRuntime\";\nimport {\n DurableRuntimeFailpoint,\n type DurableRuntimeFailpointHandler,\n} from \"@effect-agent/thread/DurableFailpoint\";\nimport { DeploymentId, ProducerId } from \"@effect-agent/thread/Records\";\nimport { type ScheduleStore } from \"@effect-agent/thread/Schedule\";\nimport {\n DEFAULT_OWNERSHIP_LEASE_DURATION,\n ReleaseOwnershipRequest,\n SubmissionLedger,\n type OwnershipToken,\n} from \"@effect-agent/thread/SubmissionLedger\";\nimport { type ThreadStore } from \"@effect-agent/thread/ThreadStore\";\nimport { ToolReconciler } from \"@effect-agent/thread/ToolReconciler\";\nimport { type WakeScheduler } from \"@effect-agent/thread/WakeScheduler\";\nimport { NodeCrypto } from \"@effect/platform-node\";\nimport { SqliteClient } from \"@effect/sql-sqlite-node\";\nimport { Context, type Crypto, Duration, Effect, Layer, Ref, Schema } from \"effect\";\n\nimport { NodeWakeSchedulerConfig, nodeWakeSchedulerLayer } from \"./NodeWakeScheduler.ts\";\n\nconst PositiveMillis = Schema.Int.check(Schema.isGreaterThan(0));\nconst NonNegativeMillis = Schema.Int.check(Schema.isGreaterThanOrEqualTo(0));\n\nconst WorkerConcurrency = Schema.Int.check(\n Schema.isGreaterThanOrEqualTo(1),\n Schema.isLessThanOrEqualTo(64),\n);\n\n/** The supplied Node durable runtime configuration failed schema validation (DEPLOY-003). */\nexport class NodePlatformConfigError extends Schema.TaggedError<NodePlatformConfigError>()(\n \"NodePlatformConfigError\",\n {\n message: Schema.String,\n cause: Schema.optionalKey(Schema.Defect()),\n },\n) {}\n\n/**\n * Validated Node durable runtime configuration (deployment §4: decoded once during Layer\n * construction, exposed as a typed service). Every cadence is in milliseconds and every bound is\n * finite; `workerConcurrency` caps how many worker loops `NodeDurableHost.runWorkers` drives.\n */\nexport class NodeDurableAgentRuntimeConfigValue extends Schema.Class<NodeDurableAgentRuntimeConfigValue>(\n \"@effect-agent/platform-node/NodeDurableAgentRuntimeConfigValue\",\n)({\n /** SQLite database file backing BOTH the Thread Log and the Submission Ledger. */\n filename: Schema.NonEmptyString,\n deploymentId: DeploymentId,\n producerId: ProducerId,\n /** Submission ownership lease duration (D5); liveness hint only, epochs stay authoritative. */\n ownershipLeaseDuration: PositiveMillis,\n /** Finite bound on concurrent worker loops per host (rule 10). */\n workerConcurrency: WorkerConcurrency,\n /** Ledger-scan fallback cadence of the Node wake scheduler (deployment §3). */\n wakeScanInterval: PositiveMillis,\n /** `awaitSettlement` ledger re-check cadence when no wake arrives. */\n settlementPollInterval: PositiveMillis,\n /** Worker ownership-lease renewal cadence. */\n leaseRenewalInterval: PositiveMillis,\n /** Active-Run abort-intent poll cadence. */\n abortPollInterval: PositiveMillis,\n /** Bounded SQLITE_BUSY retry window for write-lock acquisition. */\n busyTimeout: NonNegativeMillis,\n /** Canonical observation poll cadence of the SQLite store. */\n observationPollInterval: NonNegativeMillis,\n /** Opt-in full payload/digest-chain audit while opening the store. */\n verifyOnOpen: Schema.Boolean,\n}) {}\n\n/** Explicit configuration authority for the assembled Node durable runtime. */\nexport class NodeDurableAgentRuntimeConfig extends Context.Service<\n NodeDurableAgentRuntimeConfig,\n NodeDurableAgentRuntimeConfigValue\n>()(\"@effect-agent/platform-node/NodeDurableAgentRuntimeConfig\") {}\n\n/**\n * Raw (unvalidated) construction options for `NodeDurableAgentRuntime.layer`. Optional fields default\n * to the documented production values; everything is schema-decoded into\n * `NodeDurableAgentRuntimeConfigValue` before any resource opens (deployment §5 gate 1).\n */\nexport interface NodeDurableAgentRuntimeOptions<\n ContextError = never,\n ContextRequirements = never,\n AuthorizationError = never,\n AuthorizationRequirements = never,\n> {\n readonly filename: string;\n readonly deploymentId: string;\n readonly producerId: string;\n /** Milliseconds; default `DEFAULT_OWNERSHIP_LEASE_DURATION` (30s, D5). */\n readonly ownershipLeaseDuration?: number | undefined;\n /** Default 1; bounded to 1..64. */\n readonly workerConcurrency?: number | undefined;\n /** Milliseconds; default 1000. */\n readonly wakeScanInterval?: number | undefined;\n /** Milliseconds; default 500. */\n readonly settlementPollInterval?: number | undefined;\n /** Milliseconds; default 10000. */\n readonly leaseRenewalInterval?: number | undefined;\n /** Milliseconds; default 500. */\n readonly abortPollInterval?: number | undefined;\n /** Deployment-owned pricing authority used by durable cost budgets and settlements. */\n readonly estimateCostMicrousd?: RunCostEstimator | undefined;\n /** Closed trusted Tool failure reporting. Omission masks ambient observers at construction. */\n readonly toolFailureObserver?: RunToolFailureObserver | undefined;\n /** Milliseconds; default 5000. */\n readonly busyTimeout?: number | undefined;\n /** Milliseconds; default 25. */\n readonly observationPollInterval?: number | undefined;\n /** Default false. */\n readonly verifyOnOpen?: boolean | undefined;\n /** SQLite adapter fault injection (`ledger:*` / `append:*` locations); default none. */\n readonly storageFailpoint?: SqliteStorageFailpointHandler | undefined;\n /** Coordinator fault injection (`submit:*` / `terminalize:*` locations); default none. */\n readonly runtimeFailpoint?: DurableRuntimeFailpointHandler | undefined;\n /**\n * Reconciliation policy consulted for open ordinary Tool Calls before an Unknown Outcome is\n * recorded (durability §10, DUR-009). Defaults to the fail-closed `ToolReconciler.uncertain`:\n * with no registered policy, every open call stays Unknown and routes to the authorized\n * DUR-017 resolution path.\n */\n readonly toolReconciler?: Layer.Layer<ToolReconciler> | undefined;\n /** Host prompt preparation/compaction, acquired once with the runtime; default pass-through. */\n readonly runContext?:\n | Layer.Layer<RunContextPreparation, ContextError, ContextRequirements | Crypto.Crypto>\n | undefined;\n /**\n * Independent action-time Tool authority, acquired once with the runtime; default allow-all.\n * Construction errors and application dependencies remain in the assembled Layer's E and R.\n * The platform supplies Crypto to both extension Layers.\n */\n readonly toolAuthorization?:\n | Layer.Layer<\n RunToolAuthorization,\n AuthorizationError,\n AuthorizationRequirements | Crypto.Crypto\n >\n | undefined;\n}\n\n/** Built-in construction failures. `layer` also preserves supplied service Layers' errors. */\nexport type NodeDurableAgentRuntimeInitializationError =\n | NodePlatformConfigError\n | SqliteStorageInitializationError;\n\n/** The services `NodeDurableAgentRuntime.layer` provides. */\nexport type NodeDurableAgentRuntimeServices =\n | DurableAgentRuntime\n | SubmissionLedger\n | ThreadStore\n | ScheduleStore\n | WakeScheduler\n | DurableRuntimeConfig\n | NodeDurableAgentRuntimeConfig;\n\nconst decodeConfigValue = Schema.decodeUnknownEffect(NodeDurableAgentRuntimeConfigValue);\n\nconst configFromOptions = (\n options: Omit<NodeDurableAgentRuntimeOptions, \"runContext\" | \"toolAuthorization\">,\n): Effect.Effect<NodeDurableAgentRuntimeConfigValue, NodePlatformConfigError> =>\n decodeConfigValue({\n filename: options.filename,\n deploymentId: options.deploymentId,\n producerId: options.producerId,\n ownershipLeaseDuration:\n options.ownershipLeaseDuration ?? Duration.toMillis(DEFAULT_OWNERSHIP_LEASE_DURATION),\n workerConcurrency: options.workerConcurrency ?? 1,\n wakeScanInterval: options.wakeScanInterval ?? 1_000,\n settlementPollInterval: options.settlementPollInterval ?? 500,\n leaseRenewalInterval: options.leaseRenewalInterval ?? 10_000,\n abortPollInterval: options.abortPollInterval ?? 500,\n busyTimeout: options.busyTimeout ?? 5_000,\n observationPollInterval: options.observationPollInterval ?? 25,\n verifyOnOpen: options.verifyOnOpen ?? false,\n }).pipe(\n Effect.mapError((error) =>\n NodePlatformConfigError.make({\n message: `Invalid Node durable runtime configuration: ${error.message}`,\n cause: error,\n }),\n ),\n );\n\n/** SQLite storage configuration derived from the single validated Node configuration. */\nconst sqliteStorageConfigLayer: Layer.Layer<\n SqliteStorageConfig,\n never,\n NodeDurableAgentRuntimeConfig\n> = Layer.effect(SqliteStorageConfig)(\n Effect.gen(function* () {\n const config = yield* NodeDurableAgentRuntimeConfig;\n\n return SqliteStorageConfigValue.make({\n observationPollInterval: config.observationPollInterval,\n busyTimeout: config.busyTimeout,\n ownershipLeaseDuration: config.ownershipLeaseDuration,\n verifyOnOpen: config.verifyOnOpen,\n });\n }),\n);\n\n/** Thread coordinator configuration derived from the single validated Node configuration. */\nconst durableRuntimeConfigLayer = (\n estimateCostMicrousd: RunCostEstimator | undefined,\n): Layer.Layer<DurableRuntimeConfig, never, NodeDurableAgentRuntimeConfig> =>\n Layer.effect(DurableRuntimeConfig)(\n Effect.gen(function* () {\n const config = yield* NodeDurableAgentRuntimeConfig;\n\n return DurableRuntimeConfig.make({\n deploymentId: config.deploymentId,\n producerId: config.producerId,\n settlementPollInterval: Duration.millis(config.settlementPollInterval),\n leaseRenewalInterval: Duration.millis(config.leaseRenewalInterval),\n abortPollInterval: Duration.millis(config.abortPollInterval),\n ...(estimateCostMicrousd === undefined ? {} : { estimateCostMicrousd }),\n });\n }),\n );\n\n/** Wake fallback-scan cadence derived from the single validated Node configuration. */\nconst wakeSchedulerConfigLayer: Layer.Layer<\n NodeWakeSchedulerConfig,\n never,\n NodeDurableAgentRuntimeConfig\n> = Layer.effect(NodeWakeSchedulerConfig)(\n Effect.gen(function* () {\n const config = yield* NodeDurableAgentRuntimeConfig;\n\n return { scanInterval: Duration.millis(config.wakeScanInterval) };\n }),\n);\n\nconst releaseTrackedOwnership = (\n ledger: SubmissionLedger[\"Service\"],\n registry: Ref.Ref<ReadonlyMap<SubmissionId, OwnershipToken>>,\n): Effect.Effect<void> =>\n Effect.gen(function* () {\n const tracked = yield* Ref.getAndSet(registry, new Map<SubmissionId, OwnershipToken>());\n\n for (const [submissionId, ownershipToken] of tracked) {\n yield* ledger\n .releaseOwnership(ReleaseOwnershipRequest.make({ submissionId, ownershipToken }))\n .pipe(\n Effect.catchTags({\n // A newer epoch already owns (or settled) the lane: nothing left to drain.\n OwnershipLost: () => Effect.void,\n // Drain is best-effort by design: the lease still expires and the durability protocol,\n // not graceful shutdown, provides correctness (DEPLOY-006).\n LedgerError: (error) =>\n Effect.logWarning(\"Ownership drain failed; the lease will expire instead\", error),\n }),\n );\n }\n });\n\n/**\n * Shutdown-drain decorator for a `SubmissionLedger` (deployment §6 step 6): every ownership\n * period granted through this Layer is tracked — claims start tracking, renewals follow token\n * rotation, releases and settlement finalizations stop it — and every ownership still held when\n * the Layer's Scope closes is released so another host can claim the lane immediately instead of\n * waiting for lease expiry. The drain is a liveness courtesy only; producer-epoch fencing remains\n * the correctness authority (DUR-006), and a forced kill simply falls back to lease expiry.\n */\nexport const ownershipDrainLayer: Layer.Layer<SubmissionLedger, never, SubmissionLedger> =\n Layer.effect(SubmissionLedger)(\n Effect.gen(function* () {\n const ledger = yield* SubmissionLedger;\n\n const registry = yield* Ref.make<ReadonlyMap<SubmissionId, OwnershipToken>>(\n new Map<SubmissionId, OwnershipToken>(),\n );\n\n const track = (submissionId: SubmissionId, ownershipToken: OwnershipToken) =>\n Ref.update(registry, (tracked) => new Map(tracked).set(submissionId, ownershipToken));\n\n const untrack = (submissionId: SubmissionId) =>\n Ref.update(registry, (tracked) => {\n const next = new Map(tracked);\n\n next.delete(submissionId);\n\n return next;\n });\n\n yield* Effect.addFinalizer(() => releaseTrackedOwnership(ledger, registry));\n\n return SubmissionLedger.of({\n capabilities: ledger.capabilities,\n admit: ledger.admit,\n markReady: ledger.markReady,\n lookup: ledger.lookup,\n // The S2 subagent ops forward untouched: none of them grants an ownership period, so\n // the drain has nothing to track for them (`suspend` below already stops tracking the\n // waitingForChild ownership period the moment it ends).\n resolveAdmission: ledger.resolveAdmission,\n recordChildSettled: ledger.recordChildSettled,\n reserveChildBudget: ledger.reserveChildBudget,\n attachChildToReservation: ledger.attachChildToReservation,\n beginChildBudgetRelease: ledger.beginChildBudgetRelease,\n releaseChildBudget: ledger.releaseChildBudget,\n claim: (request) =>\n ledger\n .claim(request)\n .pipe(\n Effect.tap((claimed) =>\n claimed._tag === \"Some\"\n ? track(claimed.value.submissionId, claimed.value.ownershipToken)\n : Effect.void,\n ),\n ),\n renewOwnership: (request) =>\n ledger.renewOwnership(request).pipe(\n Effect.tap((renewal) => track(request.submissionId, renewal.ownershipToken)),\n Effect.tapError((error) =>\n error._tag === \"OwnershipLost\" ? untrack(request.submissionId) : Effect.void,\n ),\n ),\n releaseOwnership: (request) =>\n ledger.releaseOwnership(request).pipe(\n Effect.tap(() => untrack(request.submissionId)),\n Effect.tapError((error) =>\n error._tag === \"OwnershipLost\" ? untrack(request.submissionId) : Effect.void,\n ),\n ),\n markInputApplied: ledger.markInputApplied,\n reserveSettlement: ledger.reserveSettlement,\n finalizeSettlement: (request) =>\n ledger.finalizeSettlement(request).pipe(Effect.tap(() => untrack(request.submissionId))),\n requestAbort: ledger.requestAbort,\n claimJoining: ledger.claimJoining,\n markJoined: ledger.markJoined,\n revertJoining: ledger.revertJoining,\n // Suspension ends the ownership period by contract, so the drain stops tracking it.\n suspend: (request) =>\n ledger.suspend(request).pipe(Effect.tap(() => untrack(request.submissionId))),\n recordApprovalDecision: ledger.recordApprovalDecision,\n markUnknown: ledger.markUnknown,\n recordUnknownResolution: ledger.recordUnknownResolution,\n scanNonterminal: ledger.scanNonterminal,\n loadRecoverySnapshot: ledger.loadRecoverySnapshot,\n });\n }),\n );\n\n/**\n * The DN Layer assembly (deployment §12: a Layer-assembly library, not an app entrypoint).\n * `layer(options)` decodes the configuration, opens ONE SQLite database serving both the\n * Thread Log and the Submission Ledger (so claims fence the same producer epochs), wires\n * the Node wake scheduler with its ledger-scan fallback, wraps the ledger with the shutdown\n * ownership drain, defaults the Tool reconciliation policy to the fail-closed\n * `ToolReconciler.uncertain` (override via `options.toolReconciler`), and provides a ready\n * `DurableAgentRuntime` on top. Storage compatibility is\n * verified during construction: an incompatible database file fails the Layer with\n * `SqliteStorageCompatibilityError` before anything is mutated (DEPLOY-008).\n */\nexport class NodeDurableAgentRuntime {\n /** Validated configuration Layer; fails typed when the supplied options are out of bounds. */\n static configLayer<\n ContextError = never,\n ContextRequirements = never,\n AuthorizationError = never,\n AuthorizationRequirements = never,\n >(\n options: NodeDurableAgentRuntimeOptions<\n ContextError,\n ContextRequirements,\n AuthorizationError,\n AuthorizationRequirements\n >,\n ): Layer.Layer<NodeDurableAgentRuntimeConfig, NodePlatformConfigError> {\n return Layer.effect(NodeDurableAgentRuntimeConfig)(configFromOptions(options));\n }\n\n /** The full DN runtime stack over one SQLite file. */\n static layer<\n ContextError = never,\n ContextRequirements = never,\n AuthorizationError = never,\n AuthorizationRequirements = never,\n >(\n options: NodeDurableAgentRuntimeOptions<\n ContextError,\n ContextRequirements,\n AuthorizationError,\n AuthorizationRequirements\n >,\n ): Layer.Layer<\n NodeDurableAgentRuntimeServices,\n NodeDurableAgentRuntimeInitializationError | ContextError | AuthorizationError,\n Exclude<ContextRequirements | AuthorizationRequirements, Crypto.Crypto>\n > {\n return NodeDurableAgentRuntime.assemble(DurableAgentRuntime.layerWithServices, options);\n }\n\n /** Own typed executable registrations for every worker using this Node runtime. */\n static layerRegistered<\n const Entries extends ReadonlyArray<AgentRegistration>,\n ContextError = never,\n ContextRequirements = never,\n AuthorizationError = never,\n AuthorizationRequirements = never,\n >(\n registrations: Entries,\n options: NodeDurableAgentRuntimeOptions<\n ContextError,\n ContextRequirements,\n AuthorizationError,\n AuthorizationRequirements\n >,\n ) {\n return NodeDurableAgentRuntime.assemble(\n DurableAgentRuntime.layerRegistered(registrations),\n options,\n );\n }\n\n /** Construct from registrations already resolved within the enclosing application Scope. */\n static layerWithBindings<\n ContextError = never,\n ContextRequirements = never,\n AuthorizationError = never,\n AuthorizationRequirements = never,\n >(\n bindings: ReadonlyArray<ResolvedBinding>,\n options: NodeDurableAgentRuntimeOptions<\n ContextError,\n ContextRequirements,\n AuthorizationError,\n AuthorizationRequirements\n >,\n ) {\n return NodeDurableAgentRuntime.assemble(\n DurableAgentRuntime.layerWithBindings(bindings),\n options,\n );\n }\n\n private static assemble<\n RuntimeError,\n RuntimeRequirements,\n ContextError,\n ContextRequirements,\n AuthorizationError,\n AuthorizationRequirements,\n >(\n runtimeLayer: Layer.Layer<DurableAgentRuntime, RuntimeError, RuntimeRequirements>,\n options: NodeDurableAgentRuntimeOptions<\n ContextError,\n ContextRequirements,\n AuthorizationError,\n AuthorizationRequirements\n >,\n ) {\n const assembled = Layer.unwrap(\n Effect.map(configFromOptions(options), (config) => {\n const nodeConfigLayer = Layer.succeed(NodeDurableAgentRuntimeConfig)(config);\n\n const infrastructure = Layer.mergeAll(\n sqliteStorageConfigLayer,\n storageFailpointLayer({\n filename: config.filename,\n failpoint: options.storageFailpoint,\n }),\n SqliteClient.layer({ filename: config.filename }),\n NodeCrypto.layer,\n );\n\n const runtimeFailpointLayer =\n options.runtimeFailpoint === undefined\n ? DurableRuntimeFailpoint.layer\n : Layer.succeed(DurableRuntimeFailpoint)({ hit: options.runtimeFailpoint });\n\n const reconcilerLayer = options.toolReconciler ?? ToolReconciler.uncertain;\n\n const observerLayer =\n options.toolFailureObserver === undefined\n ? Layer.succeed(CurrentToolFailureObserver)(undefined)\n : toolFailureObserverLayer(options.toolFailureObserver);\n\n const ports = Layer.mergeAll(\n threadStoreLayer,\n scheduleStoreLayer,\n nodeWakeSchedulerLayer.pipe(\n Layer.provideMerge(ownershipDrainLayer.pipe(Layer.provide(submissionLedgerLayer))),\n ),\n );\n\n return runtimeLayer.pipe(\n Layer.provideMerge(\n Layer.mergeAll(ports, durableRuntimeConfigLayer(options.estimateCostMicrousd)),\n ),\n Layer.provide(\n Layer.mergeAll(\n wakeSchedulerConfigLayer,\n runtimeFailpointLayer,\n reconcilerLayer,\n observerLayer,\n ),\n ),\n Layer.provideMerge(infrastructure),\n Layer.provideMerge(nodeConfigLayer),\n Layer.provide(\n Layer.mergeAll(\n options.runContext ?? RunContextPreparationPassthrough,\n options.toolAuthorization ?? RunToolAuthorization.allowAll,\n ).pipe(Layer.provide(NodeCrypto.layer)),\n ),\n );\n }),\n );\n\n const runtime: Layer.Layer<\n NodeDurableAgentRuntimeServices,\n Layer.Error<typeof assembled>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J,QAAQ,qBAAqB,KAAA,IACzB,wBAAwB,QACxB,MAAM,QAAQ,uBAAuB,CAAC,CAAC,EAAE,KAAK,QAAQ,iBAAiB,CAAC;GAE9E,MAAM,kBAAkB,QAAQ,kBAAkB,eAAe;GAEjE,MAAM,gBACJ,QAAQ,wBAAwB,KAAA,IAC5B,MAAM,QAAQ,0BAA0B,CAAC,CAAC,KAAA,CAAS,IACnD,yBAAyB,QAAQ,mBAAmB;GAE1D,MAAM,QAAQ,MAAM,SAClB,kBACA,oBACA,uBAAuB,KACrB,MAAM,aAAa,oBAAoB,KAAK,MAAM,QAAQ,qBAAqB,CAAC,CAAC,CACnF,CACF;GAEA,OAAO,aAAa,KAClB,MAAM,aACJ,MAAM,SAAS,OAAO,0BAA0B,QAAQ,oBAAoB,CAAC,CAC/E,GACA,MAAM,QACJ,MAAM,SACJ,0BACA,uBACA,iBACA,aACF,CACF,GACA,MAAM,aAAa,cAAc,GACjC,MAAM,aAAa,eAAe,GAClC,MAAM,QACJ,MAAM,SACJ,QAAQ,cAAc,kCACtB,QAAQ,qBAAqB,qBAAqB,QACpD,CAAC,CAAC,KAAK,MAAM,QAAQ,WAAW,KAAK,CAAC,CACxC,CACF;EACF,CAAC,CASU;CACf;AACF"}
|
|
1
|
+
{"version":3,"file":"NodeDurableAgentRuntime.mjs","names":[],"sources":["../src/NodeDurableAgentRuntime.ts"],"sourcesContent":["import { type SubmissionId } from \"@effect-agent/core/Identifiers\";\nimport {\n CurrentToolFailureObserver,\n RunContextPreparationPassthrough,\n RunToolAuthorization,\n toolFailureObserverLayer,\n type RunContextPreparation,\n type RunCostEstimator,\n type RunToolFailureObserver,\n} from \"@effect-agent/engine/RunOptions\";\nimport { scheduleStoreLayer } from \"@effect-agent/storage-sqlite/SqliteScheduleStore\";\nimport {\n SqliteStorageConfig,\n SqliteStorageConfigValue,\n} from \"@effect-agent/storage-sqlite/SqliteStorageConfig\";\nimport { type SqliteStorageFailpointHandler } from \"@effect-agent/storage-sqlite/SqliteStorageFailpoint\";\nimport { submissionLedgerLayer } from \"@effect-agent/storage-sqlite/SqliteSubmissionLedger\";\nimport {\n threadStoreLayer,\n storageFailpointLayer,\n type SqliteStorageInitializationError,\n} from \"@effect-agent/storage-sqlite/SqliteThreadStore\";\nimport {\n type AgentRegistration,\n type ResolvedBinding,\n} from \"@effect-agent/thread/AgentRegistration\";\nimport {\n DurableAgentRuntime,\n DurableRuntimeConfig,\n} from \"@effect-agent/thread/DurableAgentRuntime\";\nimport {\n DurableRuntimeFailpoint,\n type DurableRuntimeFailpointHandler,\n} from \"@effect-agent/thread/DurableFailpoint\";\nimport { DeploymentId, ProducerId } from \"@effect-agent/thread/Records\";\nimport { type ScheduleStore } from \"@effect-agent/thread/Schedule\";\nimport {\n DEFAULT_OWNERSHIP_LEASE_DURATION,\n ReleaseOwnershipRequest,\n SubmissionLedger,\n type OwnershipToken,\n} from \"@effect-agent/thread/SubmissionLedger\";\nimport { type ThreadStore } from \"@effect-agent/thread/ThreadStore\";\nimport { ToolReconciler } from \"@effect-agent/thread/ToolReconciler\";\nimport { type WakeScheduler } from \"@effect-agent/thread/WakeScheduler\";\nimport { NodeCrypto } from \"@effect/platform-node\";\nimport { SqliteClient } from \"@effect/sql-sqlite-node\";\nimport { Context, type Crypto, Duration, Effect, Layer, Ref, Schema } from \"effect\";\n\nimport { NodeWakeSchedulerConfig, nodeWakeSchedulerLayer } from \"./NodeWakeScheduler.ts\";\n\nconst PositiveMillis = Schema.Int.check(Schema.isGreaterThan(0));\nconst NonNegativeMillis = Schema.Int.check(Schema.isGreaterThanOrEqualTo(0));\n\nconst WorkerConcurrency = Schema.Int.check(\n Schema.isGreaterThanOrEqualTo(1),\n Schema.isLessThanOrEqualTo(64),\n);\n\n/** The supplied Node durable runtime configuration failed schema validation (DEPLOY-003). */\nexport class NodePlatformConfigError extends Schema.TaggedError<NodePlatformConfigError>()(\n \"NodePlatformConfigError\",\n {\n message: Schema.String,\n cause: Schema.optionalKey(Schema.Defect()),\n },\n) {}\n\n/**\n * Validated Node durable runtime configuration (deployment §4: decoded once during Layer\n * construction, exposed as a typed service). Every cadence is in milliseconds and every bound is\n * finite; `workerConcurrency` caps how many worker loops `NodeDurableHost.runWorkers` drives.\n */\nexport class NodeDurableAgentRuntimeConfigValue extends Schema.Class<NodeDurableAgentRuntimeConfigValue>(\n \"@effect-agent/platform-node/NodeDurableAgentRuntimeConfigValue\",\n)({\n /** SQLite database file backing BOTH the Thread Log and the Submission Ledger. */\n filename: Schema.NonEmptyString,\n deploymentId: DeploymentId,\n producerId: ProducerId,\n /** Submission ownership lease duration (D5); liveness hint only, epochs stay authoritative. */\n ownershipLeaseDuration: PositiveMillis,\n /** Finite bound on concurrent worker loops per host (rule 10). */\n workerConcurrency: WorkerConcurrency,\n /** Ledger-scan fallback cadence of the Node wake scheduler (deployment §3). */\n wakeScanInterval: PositiveMillis,\n /** `awaitSettlement` ledger re-check cadence when no wake arrives. */\n settlementPollInterval: PositiveMillis,\n /** Worker ownership-lease renewal cadence. */\n leaseRenewalInterval: PositiveMillis,\n /** Active-Run abort-intent poll cadence. */\n abortPollInterval: PositiveMillis,\n /** Bounded SQLITE_BUSY retry window for write-lock acquisition. */\n busyTimeout: NonNegativeMillis,\n /** Canonical observation poll cadence of the SQLite store. */\n observationPollInterval: NonNegativeMillis,\n /** Opt-in full payload/digest-chain audit while opening the store. */\n verifyOnOpen: Schema.Boolean,\n}) {}\n\n/** Explicit configuration authority for the assembled Node durable runtime. */\nexport class NodeDurableAgentRuntimeConfig extends Context.Service<\n NodeDurableAgentRuntimeConfig,\n NodeDurableAgentRuntimeConfigValue\n>()(\"@effect-agent/platform-node/NodeDurableAgentRuntimeConfig\") {}\n\n/**\n * Raw (unvalidated) construction options for `NodeDurableAgentRuntime.layer`. Optional fields default\n * to the documented production values; everything is schema-decoded into\n * `NodeDurableAgentRuntimeConfigValue` before any resource opens (deployment §5 gate 1).\n */\nexport interface NodeDurableAgentRuntimeOptions<\n ContextError = never,\n ContextRequirements = never,\n AuthorizationError = never,\n AuthorizationRequirements = never,\n> {\n readonly filename: string;\n readonly deploymentId: string;\n readonly producerId: string;\n /** Milliseconds; default `DEFAULT_OWNERSHIP_LEASE_DURATION` (30s, D5). */\n readonly ownershipLeaseDuration?: number | undefined;\n /** Default 1; bounded to 1..64. */\n readonly workerConcurrency?: number | undefined;\n /** Milliseconds; default 1000. */\n readonly wakeScanInterval?: number | undefined;\n /** Milliseconds; default 500. */\n readonly settlementPollInterval?: number | undefined;\n /** Milliseconds; default 10000. */\n readonly leaseRenewalInterval?: number | undefined;\n /** Milliseconds; default 500. */\n readonly abortPollInterval?: number | undefined;\n /** Deployment-owned pricing authority used by durable cost budgets and settlements. */\n readonly estimateCostMicrousd?: RunCostEstimator | undefined;\n /** Closed trusted Tool failure reporting. Omission masks ambient observers at construction. */\n readonly toolFailureObserver?: RunToolFailureObserver | undefined;\n /** Milliseconds; default 5000. */\n readonly busyTimeout?: number | undefined;\n /** Milliseconds; default 25. */\n readonly observationPollInterval?: number | undefined;\n /** Default false. */\n readonly verifyOnOpen?: boolean | undefined;\n /** SQLite adapter fault injection (`ledger:*` / `append:*` locations); default none. */\n readonly storageFailpoint?: SqliteStorageFailpointHandler | undefined;\n /** Coordinator fault injection (`submit:*` / `terminalize:*` locations); default none. */\n readonly runtimeFailpoint?: DurableRuntimeFailpointHandler | undefined;\n /**\n * Reconciliation policy consulted for open ordinary Tool Calls before an Unknown Outcome is\n * recorded (durability §10, DUR-009). Defaults to the fail-closed `ToolReconciler.uncertain`:\n * with no registered policy, every open call stays Unknown and routes to the authorized\n * DUR-017 resolution path.\n */\n readonly toolReconciler?: Layer.Layer<ToolReconciler> | undefined;\n /** Host prompt preparation/compaction, acquired once with the runtime; default pass-through. */\n readonly runContext?:\n | Layer.Layer<RunContextPreparation, ContextError, ContextRequirements | Crypto.Crypto>\n | undefined;\n /**\n * Independent action-time Tool authority, acquired once with the runtime; default allow-all.\n * Construction errors and application dependencies remain in the assembled Layer's E and R.\n * The platform supplies Crypto to both extension Layers.\n */\n readonly toolAuthorization?:\n | Layer.Layer<\n RunToolAuthorization,\n AuthorizationError,\n AuthorizationRequirements | Crypto.Crypto\n >\n | undefined;\n}\n\n/** Built-in construction failures. `layer` also preserves supplied service Layers' errors. */\nexport type NodeDurableAgentRuntimeInitializationError =\n | NodePlatformConfigError\n | SqliteStorageInitializationError;\n\n/** The services `NodeDurableAgentRuntime.layer` provides. */\nexport type NodeDurableAgentRuntimeServices =\n | DurableAgentRuntime\n | SubmissionLedger\n | ThreadStore\n | ScheduleStore\n | WakeScheduler\n | DurableRuntimeConfig\n | NodeDurableAgentRuntimeConfig;\n\nconst decodeConfigValue = Schema.decodeUnknownEffect(NodeDurableAgentRuntimeConfigValue);\n\nconst configFromOptions = (\n options: Omit<NodeDurableAgentRuntimeOptions, \"runContext\" | \"toolAuthorization\">,\n): Effect.Effect<NodeDurableAgentRuntimeConfigValue, NodePlatformConfigError> =>\n decodeConfigValue({\n filename: options.filename,\n deploymentId: options.deploymentId,\n producerId: options.producerId,\n ownershipLeaseDuration:\n options.ownershipLeaseDuration ?? Duration.toMillis(DEFAULT_OWNERSHIP_LEASE_DURATION),\n workerConcurrency: options.workerConcurrency ?? 1,\n wakeScanInterval: options.wakeScanInterval ?? 1_000,\n settlementPollInterval: options.settlementPollInterval ?? 500,\n leaseRenewalInterval: options.leaseRenewalInterval ?? 10_000,\n abortPollInterval: options.abortPollInterval ?? 500,\n busyTimeout: options.busyTimeout ?? 5_000,\n observationPollInterval: options.observationPollInterval ?? 25,\n verifyOnOpen: options.verifyOnOpen ?? false,\n }).pipe(\n Effect.mapError((error) =>\n NodePlatformConfigError.make({\n message: `Invalid Node durable runtime configuration: ${error.message}`,\n cause: error,\n }),\n ),\n );\n\n/** SQLite storage configuration derived from the single validated Node configuration. */\nconst sqliteStorageConfigLayer: Layer.Layer<\n SqliteStorageConfig,\n never,\n NodeDurableAgentRuntimeConfig\n> = Layer.effect(SqliteStorageConfig)(\n Effect.gen(function* () {\n const config = yield* NodeDurableAgentRuntimeConfig;\n\n return SqliteStorageConfigValue.make({\n observationPollInterval: config.observationPollInterval,\n busyTimeout: config.busyTimeout,\n ownershipLeaseDuration: config.ownershipLeaseDuration,\n verifyOnOpen: config.verifyOnOpen,\n });\n }),\n);\n\n/** Thread coordinator configuration derived from the single validated Node configuration. */\nconst durableRuntimeConfigLayer = (\n estimateCostMicrousd: RunCostEstimator | undefined,\n): Layer.Layer<DurableRuntimeConfig, never, NodeDurableAgentRuntimeConfig> =>\n Layer.effect(DurableRuntimeConfig)(\n Effect.gen(function* () {\n const config = yield* NodeDurableAgentRuntimeConfig;\n\n return DurableRuntimeConfig.make({\n deploymentId: config.deploymentId,\n producerId: config.producerId,\n settlementPollInterval: Duration.millis(config.settlementPollInterval),\n leaseRenewalInterval: Duration.millis(config.leaseRenewalInterval),\n abortPollInterval: Duration.millis(config.abortPollInterval),\n ...(estimateCostMicrousd === undefined ? {} : { estimateCostMicrousd }),\n });\n }),\n );\n\n/** Wake fallback-scan cadence derived from the single validated Node configuration. */\nconst wakeSchedulerConfigLayer: Layer.Layer<\n NodeWakeSchedulerConfig,\n never,\n NodeDurableAgentRuntimeConfig\n> = Layer.effect(NodeWakeSchedulerConfig)(\n Effect.gen(function* () {\n const config = yield* NodeDurableAgentRuntimeConfig;\n\n return { scanInterval: Duration.millis(config.wakeScanInterval) };\n }),\n);\n\nconst releaseTrackedOwnership = (\n ledger: SubmissionLedger[\"Service\"],\n registry: Ref.Ref<ReadonlyMap<SubmissionId, OwnershipToken>>,\n): Effect.Effect<void> =>\n Effect.gen(function* () {\n const tracked = yield* Ref.getAndSet(registry, new Map<SubmissionId, OwnershipToken>());\n\n for (const [submissionId, ownershipToken] of tracked) {\n yield* ledger\n .releaseOwnership(ReleaseOwnershipRequest.make({ submissionId, ownershipToken }))\n .pipe(\n Effect.catchTags({\n // A newer epoch already owns (or settled) the lane: nothing left to drain.\n OwnershipLost: () => Effect.void,\n // Drain is best-effort by design: the lease still expires and the durability protocol,\n // not graceful shutdown, provides correctness (DEPLOY-006).\n LedgerError: (error) =>\n Effect.logWarning(\"Ownership drain failed; the lease will expire instead\", error),\n }),\n );\n }\n });\n\n/**\n * Shutdown-drain decorator for a `SubmissionLedger` (deployment §6 step 6): every ownership\n * period granted through this Layer is tracked — claims start tracking, renewals follow token\n * rotation, releases and settlement finalizations stop it — and every ownership still held when\n * the Layer's Scope closes is released so another host can claim the lane immediately instead of\n * waiting for lease expiry. The drain is a liveness courtesy only; producer-epoch fencing remains\n * the correctness authority (DUR-006), and a forced kill simply falls back to lease expiry.\n */\nexport const ownershipDrainLayer: Layer.Layer<SubmissionLedger, never, SubmissionLedger> =\n Layer.effect(SubmissionLedger)(\n Effect.gen(function* () {\n const ledger = yield* SubmissionLedger;\n\n const registry = yield* Ref.make<ReadonlyMap<SubmissionId, OwnershipToken>>(\n new Map<SubmissionId, OwnershipToken>(),\n );\n\n const track = (submissionId: SubmissionId, ownershipToken: OwnershipToken) =>\n Ref.update(registry, (tracked) => new Map(tracked).set(submissionId, ownershipToken));\n\n const untrack = (submissionId: SubmissionId) =>\n Ref.update(registry, (tracked) => {\n const next = new Map(tracked);\n\n next.delete(submissionId);\n\n return next;\n });\n\n yield* Effect.addFinalizer(() => releaseTrackedOwnership(ledger, registry));\n\n return SubmissionLedger.of({\n capabilities: ledger.capabilities,\n admit: ledger.admit,\n markReady: ledger.markReady,\n lookup: ledger.lookup,\n // The S2 subagent ops forward untouched: none of them grants an ownership period, so\n // the drain has nothing to track for them (`suspend` below already stops tracking the\n // waitingForChild ownership period the moment it ends).\n resolveAdmission: ledger.resolveAdmission,\n recordChildSettled: ledger.recordChildSettled,\n reserveChildBudget: ledger.reserveChildBudget,\n attachChildToReservation: ledger.attachChildToReservation,\n beginChildBudgetRelease: ledger.beginChildBudgetRelease,\n releaseChildBudget: ledger.releaseChildBudget,\n claim: (request) =>\n ledger\n .claim(request)\n .pipe(\n Effect.tap((claimed) =>\n claimed._tag === \"Some\"\n ? track(claimed.value.submissionId, claimed.value.ownershipToken)\n : Effect.void,\n ),\n ),\n renewOwnership: (request) =>\n ledger.renewOwnership(request).pipe(\n Effect.tap((renewal) => track(request.submissionId, renewal.ownershipToken)),\n Effect.tapError((error) =>\n error._tag === \"OwnershipLost\" ? untrack(request.submissionId) : Effect.void,\n ),\n ),\n releaseOwnership: (request) =>\n ledger.releaseOwnership(request).pipe(\n Effect.tap(() => untrack(request.submissionId)),\n Effect.tapError((error) =>\n error._tag === \"OwnershipLost\" ? untrack(request.submissionId) : Effect.void,\n ),\n ),\n markInputApplied: ledger.markInputApplied,\n reserveSettlement: ledger.reserveSettlement,\n finalizeSettlement: (request) =>\n ledger.finalizeSettlement(request).pipe(Effect.tap(() => untrack(request.submissionId))),\n requestAbort: ledger.requestAbort,\n readAbortIntent: ledger.readAbortIntent,\n claimJoining: ledger.claimJoining,\n markJoined: ledger.markJoined,\n revertJoining: ledger.revertJoining,\n // Suspension ends the ownership period by contract, so the drain stops tracking it.\n suspend: (request) =>\n ledger.suspend(request).pipe(Effect.tap(() => untrack(request.submissionId))),\n recordApprovalDecision: ledger.recordApprovalDecision,\n markUnknown: ledger.markUnknown,\n recordUnknownResolution: ledger.recordUnknownResolution,\n scanNonterminal: ledger.scanNonterminal,\n loadRecoverySnapshot: ledger.loadRecoverySnapshot,\n });\n }),\n );\n\n/**\n * The DN Layer assembly (deployment §12: a Layer-assembly library, not an app entrypoint).\n * `layer(options)` decodes the configuration, opens ONE SQLite database serving both the\n * Thread Log and the Submission Ledger (so claims fence the same producer epochs), wires\n * the Node wake scheduler with its ledger-scan fallback, wraps the ledger with the shutdown\n * ownership drain, defaults the Tool reconciliation policy to the fail-closed\n * `ToolReconciler.uncertain` (override via `options.toolReconciler`), and provides a ready\n * `DurableAgentRuntime` on top. Storage compatibility is\n * verified during construction: an incompatible database file fails the Layer with\n * `SqliteStorageCompatibilityError` before anything is mutated (DEPLOY-008).\n */\nexport class NodeDurableAgentRuntime {\n /** Validated configuration Layer; fails typed when the supplied options are out of bounds. */\n static configLayer<\n ContextError = never,\n ContextRequirements = never,\n AuthorizationError = never,\n AuthorizationRequirements = never,\n >(\n options: NodeDurableAgentRuntimeOptions<\n ContextError,\n ContextRequirements,\n AuthorizationError,\n AuthorizationRequirements\n >,\n ): Layer.Layer<NodeDurableAgentRuntimeConfig, NodePlatformConfigError> {\n return Layer.effect(NodeDurableAgentRuntimeConfig)(configFromOptions(options));\n }\n\n /** The full DN runtime stack over one SQLite file. */\n static layer<\n ContextError = never,\n ContextRequirements = never,\n AuthorizationError = never,\n AuthorizationRequirements = never,\n >(\n options: NodeDurableAgentRuntimeOptions<\n ContextError,\n ContextRequirements,\n AuthorizationError,\n AuthorizationRequirements\n >,\n ): Layer.Layer<\n NodeDurableAgentRuntimeServices,\n NodeDurableAgentRuntimeInitializationError | ContextError | AuthorizationError,\n Exclude<ContextRequirements | AuthorizationRequirements, Crypto.Crypto>\n > {\n return NodeDurableAgentRuntime.assemble(DurableAgentRuntime.layerWithServices, options);\n }\n\n /** Own typed executable registrations for every worker using this Node runtime. */\n static layerRegistered<\n const Entries extends ReadonlyArray<AgentRegistration>,\n ContextError = never,\n ContextRequirements = never,\n AuthorizationError = never,\n AuthorizationRequirements = never,\n >(\n registrations: Entries,\n options: NodeDurableAgentRuntimeOptions<\n ContextError,\n ContextRequirements,\n AuthorizationError,\n AuthorizationRequirements\n >,\n ) {\n return NodeDurableAgentRuntime.assemble(\n DurableAgentRuntime.layerRegistered(registrations),\n options,\n );\n }\n\n /** Construct from registrations already resolved within the enclosing application Scope. */\n static layerWithBindings<\n ContextError = never,\n ContextRequirements = never,\n AuthorizationError = never,\n AuthorizationRequirements = never,\n >(\n bindings: ReadonlyArray<ResolvedBinding>,\n options: NodeDurableAgentRuntimeOptions<\n ContextError,\n ContextRequirements,\n AuthorizationError,\n AuthorizationRequirements\n >,\n ) {\n return NodeDurableAgentRuntime.assemble(\n DurableAgentRuntime.layerWithBindings(bindings),\n options,\n );\n }\n\n private static assemble<\n RuntimeError,\n RuntimeRequirements,\n ContextError,\n ContextRequirements,\n AuthorizationError,\n AuthorizationRequirements,\n >(\n runtimeLayer: Layer.Layer<DurableAgentRuntime, RuntimeError, RuntimeRequirements>,\n options: NodeDurableAgentRuntimeOptions<\n ContextError,\n ContextRequirements,\n AuthorizationError,\n AuthorizationRequirements\n >,\n ) {\n const assembled = Layer.unwrap(\n Effect.map(configFromOptions(options), (config) => {\n const nodeConfigLayer = Layer.succeed(NodeDurableAgentRuntimeConfig)(config);\n\n const infrastructure = Layer.mergeAll(\n sqliteStorageConfigLayer,\n storageFailpointLayer({\n filename: config.filename,\n failpoint: options.storageFailpoint,\n }),\n SqliteClient.layer({ filename: config.filename }),\n NodeCrypto.layer,\n );\n\n const runtimeFailpointLayer =\n options.runtimeFailpoint === undefined\n ? DurableRuntimeFailpoint.layer\n : Layer.succeed(DurableRuntimeFailpoint)({ hit: options.runtimeFailpoint });\n\n const reconcilerLayer = options.toolReconciler ?? ToolReconciler.uncertain;\n\n const observerLayer =\n options.toolFailureObserver === undefined\n ? Layer.succeed(CurrentToolFailureObserver)(undefined)\n : toolFailureObserverLayer(options.toolFailureObserver);\n\n const ports = Layer.mergeAll(\n threadStoreLayer,\n scheduleStoreLayer,\n nodeWakeSchedulerLayer.pipe(\n Layer.provideMerge(ownershipDrainLayer.pipe(Layer.provide(submissionLedgerLayer))),\n ),\n );\n\n return runtimeLayer.pipe(\n Layer.provideMerge(\n Layer.mergeAll(ports, durableRuntimeConfigLayer(options.estimateCostMicrousd)),\n ),\n Layer.provide(\n Layer.mergeAll(\n wakeSchedulerConfigLayer,\n runtimeFailpointLayer,\n reconcilerLayer,\n observerLayer,\n ),\n ),\n Layer.provideMerge(infrastructure),\n Layer.provideMerge(nodeConfigLayer),\n Layer.provide(\n Layer.mergeAll(\n options.runContext ?? RunContextPreparationPassthrough,\n options.toolAuthorization ?? RunToolAuthorization.allowAll,\n ).pipe(Layer.provide(NodeCrypto.layer)),\n ),\n );\n }),\n );\n\n const runtime: Layer.Layer<\n NodeDurableAgentRuntimeServices,\n Layer.Error<typeof assembled>,\n Layer.Services<typeof assembled>\n > = assembled;\n\n return runtime;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmDA,MAAM,iBAAiB,OAAO,IAAI,MAAM,OAAO,cAAc,CAAC,CAAC;AAC/D,MAAM,oBAAoB,OAAO,IAAI,MAAM,OAAO,uBAAuB,CAAC,CAAC;AAE3E,MAAM,oBAAoB,OAAO,IAAI,MACnC,OAAO,uBAAuB,CAAC,GAC/B,OAAO,oBAAoB,EAAE,CAC/B;;AAGA,IAAa,0BAAb,cAA6C,OAAO,YAAqC,CAAC,CACxF,2BACA;CACE,SAAS,OAAO;CAChB,OAAO,OAAO,YAAY,OAAO,OAAO,CAAC;AAC3C,CACF,CAAC,CAAC,CAAC;;;;;;AAOH,IAAa,qCAAb,cAAwD,OAAO,MAC7D,gEACF,CAAC,CAAC;;CAEA,UAAU,OAAO;CACjB,cAAc;CACd,YAAY;;CAEZ,wBAAwB;;CAExB,mBAAmB;;CAEnB,kBAAkB;;CAElB,wBAAwB;;CAExB,sBAAsB;;CAEtB,mBAAmB;;CAEnB,aAAa;;CAEb,yBAAyB;;CAEzB,cAAc,OAAO;AACvB,CAAC,CAAC,CAAC,CAAC;;AAGJ,IAAa,gCAAb,cAAmD,QAAQ,QAGzD,CAAC,CAAC,2DAA2D,CAAC,CAAC,CAAC;AAkFlE,MAAM,oBAAoB,OAAO,oBAAoB,kCAAkC;AAEvF,MAAM,qBACJ,YAEA,kBAAkB;CAChB,UAAU,QAAQ;CAClB,cAAc,QAAQ;CACtB,YAAY,QAAQ;CACpB,wBACE,QAAQ,0BAA0B,SAAS,SAAS,gCAAgC;CACtF,mBAAmB,QAAQ,qBAAqB;CAChD,kBAAkB,QAAQ,oBAAoB;CAC9C,wBAAwB,QAAQ,0BAA0B;CAC1D,sBAAsB,QAAQ,wBAAwB;CACtD,mBAAmB,QAAQ,q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B,CAAC,GACD,aAAa,MAAM,EAAE,UAAU,OAAO,SAAS,CAAC,GAChD,WAAW,KACb;GAEA,MAAM,wBACJ,QAAQ,qBAAqB,KAAA,IACzB,wBAAwB,QACxB,MAAM,QAAQ,uBAAuB,CAAC,CAAC,EAAE,KAAK,QAAQ,iBAAiB,CAAC;GAE9E,MAAM,kBAAkB,QAAQ,kBAAkB,eAAe;GAEjE,MAAM,gBACJ,QAAQ,wBAAwB,KAAA,IAC5B,MAAM,QAAQ,0BAA0B,CAAC,CAAC,KAAA,CAAS,IACnD,yBAAyB,QAAQ,mBAAmB;GAE1D,MAAM,QAAQ,MAAM,SAClB,kBACA,oBACA,uBAAuB,KACrB,MAAM,aAAa,oBAAoB,KAAK,MAAM,QAAQ,qBAAqB,CAAC,CAAC,CACnF,CACF;GAEA,OAAO,aAAa,KAClB,MAAM,aACJ,MAAM,SAAS,OAAO,0BAA0B,QAAQ,oBAAoB,CAAC,CAC/E,GACA,MAAM,QACJ,MAAM,SACJ,0BACA,uBACA,iBACA,aACF,CACF,GACA,MAAM,aAAa,cAAc,GACjC,MAAM,aAAa,eAAe,GAClC,MAAM,QACJ,MAAM,SACJ,QAAQ,cAAc,kCACtB,QAAQ,qBAAqB,qBAAqB,QACpD,CAAC,CAAC,KAAK,MAAM,QAAQ,WAAW,KAAK,CAAC,CACxC,CACF;EACF,CAAC,CASU;CACf;AACF"}
|
|
@@ -10,7 +10,7 @@ import { Context, Crypto, Effect, Layer, Schema, Stream } from "effect";
|
|
|
10
10
|
import { IntegrityReport, ObligationReport, ObligationThresholds, RecoveryExplanation, RetryCommand } from "@effect-agent/thread/Admin";
|
|
11
11
|
import { OperationDenied } from "@effect-agent/thread/OperationAuthorizer";
|
|
12
12
|
declare namespace NodeDurableHost_d_exports {
|
|
13
|
-
export { AdmissionClosed, NodeDurableHost };
|
|
13
|
+
export { AdmissionClosed, NodeDurableHost, layer, run };
|
|
14
14
|
}
|
|
15
15
|
declare const AdmissionClosed_base: Schema.Class<AdmissionClosed, Schema.TaggedStruct<"AdmissionClosed", {
|
|
16
16
|
readonly message: Schema.String;
|
|
@@ -29,6 +29,8 @@ declare const NodeDurableHost_base: Context.ServiceClass<NodeDurableHost, "@effe
|
|
|
29
29
|
readonly startupRecovery: ReadonlyArray<RecoveryReport>;
|
|
30
30
|
/** Admission-role readiness (deployment §7): true until shutdown begins. */
|
|
31
31
|
readonly admissionOpen: Effect.Effect<boolean>;
|
|
32
|
+
/** Observe the managed worker pool, preserving its failure. Manual hosts start their pool here. */
|
|
33
|
+
readonly run: Effect.Effect<void, DurableWorkerFailure | DurableBindingFailure>;
|
|
32
34
|
/** `DurableAgentRuntime.submit` behind the host admission gate. */
|
|
33
35
|
readonly submit: <InputSchema extends Schema.Top>(agent: DurableSubmitAgent<InputSchema>, input: InputSchema["Type"], options: DurableSubmitOptions) => Effect.Effect<Receipt, AdmissionClosed | DurableSubmitFailure, InputSchema["EncodingServices"]>;
|
|
34
36
|
readonly awaitSettlement: (receipt: Receipt) => Effect.Effect<Settlement, DurableAwaitFailure>;
|
|
@@ -56,11 +58,13 @@ declare const NodeDurableHost_base: Context.ServiceClass<NodeDurableHost, "@effe
|
|
|
56
58
|
* Run `workerConcurrency` copies of `DurableAgentRuntime.runResolvedWorker` over the host's
|
|
57
59
|
* registered Bindings (S2): every claimed head resolves its exact stored Binding before any
|
|
58
60
|
* code runs (SUB-023), so one bounded pool serves parent and attached-child lanes.
|
|
61
|
+
* Hosts built with the module-level `layer` instead join their existing pool.
|
|
59
62
|
*/
|
|
60
63
|
readonly runResolvedWorkers: Effect.Effect<void, DurableWorkerFailure | DurableBindingFailure>;
|
|
61
64
|
}>;
|
|
62
65
|
/**
|
|
63
|
-
* Operational host
|
|
66
|
+
* Operational host service. Prefer the module's `layer` and `run` for managed workers.
|
|
67
|
+
* The static constructors on this class retain explicit, manual worker ownership.
|
|
64
68
|
*
|
|
65
69
|
* Startup gates run during Layer construction, so the service existing implies readiness:
|
|
66
70
|
* configuration was schema-decoded, the SQLite file passed the exact-version compatibility check,
|
|
@@ -115,6 +119,43 @@ declare class NodeDurableHost extends NodeDurableHost_base {
|
|
|
115
119
|
readonly bindings?: ReadonlyArray<ResolvedBinding>;
|
|
116
120
|
}): Layer.Layer<NodeDurableHost | NodeDurableAgentRuntimeServices, DurableWorkerFailure | NodeDurableAgentRuntimeInitializationError | ContextError | AuthorizationError, Exclude<ContextRequirements | AuthorizationRequirements, Crypto.Crypto>>;
|
|
117
121
|
}
|
|
122
|
+
/**
|
|
123
|
+
* Acquire a complete Node host and start one bounded, scoped worker pool after recovery.
|
|
124
|
+
* Provide model, tool, instruction, and schema dependencies to this Layer. Reusing the Layer
|
|
125
|
+
* shares the same pool. A worker failure closes admission; observe it with `run` at the process
|
|
126
|
+
* boundary so the application exits and releases the host instead of remaining idle.
|
|
127
|
+
*/
|
|
128
|
+
declare const layer: <const Entries extends ReadonlyArray<AgentRegistration>, ContextError = never, ContextRequirements = never, AuthorizationError = never, AuthorizationRequirements = never>(registrations: Entries, options: NodeDurableAgentRuntimeOptions<ContextError, ContextRequirements, AuthorizationError, AuthorizationRequirements>) => Layer.Layer<NodeDurableHost | NodeDurableAgentRuntimeServices, AuthorizationError | ContextError | NodePlatformConfigError | DurableWorkerFailure | import("@effect-agent/storage-sqlite/SqliteThreadStore").SqliteStorageInitializationError, Exclude<AuthorizationRequirements, Crypto.Crypto> | Exclude<ContextRequirements, Crypto.Crypto> | Exclude<Exclude<Exclude<Exclude<Exclude<Exclude<[Entries[number]] extends [never] ? never : Entries[number] extends (infer T_2) ? T_2 extends Entries[number] ? T_2 extends {
|
|
129
|
+
readonly attemptLayer: (context: import("@effect-agent/thread/AgentRegistration").AgentAttemptContext) => Layer.Layer<infer Provides, never, infer Requires>;
|
|
130
|
+
} ? Requires | Exclude<T_2 extends (infer T_3) ? T_3 extends T_2 ? T_3 extends {
|
|
131
|
+
readonly agent: infer A extends import("@effect-agent/thread/AgentRegistration").ExecutableAgentBinding;
|
|
132
|
+
} ? import("@effect-agent/thread/DurableAgentRuntime").DurableWorkerRequirements<A> : T_3 extends {
|
|
133
|
+
readonly agent: infer D extends import("@effect-agent/core/Agent").Definition<Schema.Top, Schema.Top, unknown, import("effect/unstable/ai/Toolkit").Any, import("@effect-agent/core/Agent").RunDispositionDeclaration<never, Schema.Top> | undefined, unknown> & {
|
|
134
|
+
readonly instructions: import("@effect-agent/core/Agent").InstructionSource<never, unknown, unknown>;
|
|
135
|
+
readonly inputPrompt?: import("@effect-agent/core/Agent").InputPromptSource<never, unknown, unknown> | undefined;
|
|
136
|
+
};
|
|
137
|
+
readonly model: infer M extends import("@effect-agent/thread/AgentRegistration").ExecutableAgentBinding["model"];
|
|
138
|
+
} ? import("@effect-agent/thread/DurableAgentRuntime").DurableWorkerRequirements<{
|
|
139
|
+
readonly definition: D;
|
|
140
|
+
readonly model: M;
|
|
141
|
+
}> : never : never : never, Provides> : T_2 extends {
|
|
142
|
+
readonly agent: infer A extends import("@effect-agent/thread/AgentRegistration").ExecutableAgentBinding;
|
|
143
|
+
} ? import("@effect-agent/thread/DurableAgentRuntime").DurableWorkerRequirements<A> : T_2 extends {
|
|
144
|
+
readonly agent: infer D extends import("@effect-agent/core/Agent").Definition<Schema.Top, Schema.Top, unknown, import("effect/unstable/ai/Toolkit").Any, import("@effect-agent/core/Agent").RunDispositionDeclaration<never, Schema.Top> | undefined, unknown> & {
|
|
145
|
+
readonly instructions: import("@effect-agent/core/Agent").InstructionSource<never, unknown, unknown>;
|
|
146
|
+
readonly inputPrompt?: import("@effect-agent/core/Agent").InputPromptSource<never, unknown, unknown> | undefined;
|
|
147
|
+
};
|
|
148
|
+
readonly model: infer M extends import("@effect-agent/thread/AgentRegistration").ExecutableAgentBinding["model"];
|
|
149
|
+
} ? import("@effect-agent/thread/DurableAgentRuntime").DurableWorkerRequirements<{
|
|
150
|
+
readonly definition: D;
|
|
151
|
+
readonly model: M;
|
|
152
|
+
}> : never : never : never, import("effect/Scope").Scope>, import("@effect-agent/thread/DurableAgentRuntime").DurableRuntimeConfig | import("@effect-agent/thread/Schedule").ScheduleStore | import("@effect-agent/thread/SubmissionLedger").SubmissionLedger | import("@effect-agent/thread/ThreadStore").ThreadStore | import("@effect-agent/thread/WakeScheduler").WakeScheduler>, import("@effect-agent/thread/DurableFailpoint").DurableRuntimeFailpoint | NodeWakeSchedulerConfig | import("@effect-agent/thread/ToolReconciler").ToolReconciler>, Crypto.Crypto | import("effect/unstable/sql/SqlClient").SqlClient | import("@effect/sql-sqlite-node/SqliteClient").SqliteClient | import("@effect-agent/storage-sqlite/SqliteStorageConfig").SqliteStorageConfig | import("@effect-agent/storage-sqlite/SqliteStorageFailpoint").SqliteStorageFailpoint>, NodeDurableAgentRuntimeConfig>, import("@effect-agent/engine/RunOptions").RunContextPreparation | import("@effect-agent/engine/RunOptions").RunToolAuthorization>>;
|
|
153
|
+
/**
|
|
154
|
+
* Supervise the host's existing workers without starting another pool. Use with
|
|
155
|
+
* `Effect.provide(HostLive)` and `NodeRuntime.runMain`; race it with a server Effect when
|
|
156
|
+
* the same process also serves requests. Unlike `Layer.launch`, this observes worker failures.
|
|
157
|
+
*/
|
|
158
|
+
declare const run: Effect.Effect<void, DurableBindingFailure | DurableWorkerFailure, NodeDurableHost>;
|
|
118
159
|
//#endregion
|
|
119
|
-
export { AdmissionClosed, NodeDurableHost, NodeDurableHost_d_exports as t };
|
|
160
|
+
export { AdmissionClosed, NodeDurableHost, layer, run, NodeDurableHost_d_exports as t };
|
|
120
161
|
//# sourceMappingURL=NodeDurableHost.d.mts.map
|
package/dist/NodeDurableHost.mjs
CHANGED
|
@@ -6,25 +6,26 @@ import { DurableAgentRuntime } from "@effect-agent/thread/DurableAgentRuntime";
|
|
|
6
6
|
import "@effect-agent/thread/Records";
|
|
7
7
|
import "@effect-agent/thread/SubmissionLedger";
|
|
8
8
|
import "@effect-agent/thread/ThreadStore";
|
|
9
|
-
import { Context, Effect, Layer, Ref, Schema } from "effect";
|
|
9
|
+
import { Context, Effect, Fiber, Layer, Ref, Schema } from "effect";
|
|
10
10
|
import "@effect-agent/thread/Admin";
|
|
11
11
|
import "@effect-agent/thread/OperationAuthorizer";
|
|
12
12
|
//#region src/NodeDurableHost.ts
|
|
13
13
|
var NodeDurableHost_exports = /* @__PURE__ */ __exportAll({
|
|
14
14
|
AdmissionClosed: () => AdmissionClosed,
|
|
15
|
-
NodeDurableHost: () => NodeDurableHost
|
|
15
|
+
NodeDurableHost: () => NodeDurableHost,
|
|
16
|
+
layer: () => layer,
|
|
17
|
+
run: () => run
|
|
16
18
|
});
|
|
17
19
|
/**
|
|
18
20
|
* Admission is not open on this host: it is shutting down (deployment §6 step 1, DEPLOY-005).
|
|
19
21
|
* Accepted work is unaffected — only NEW admissions are refused.
|
|
20
22
|
*/
|
|
21
23
|
var AdmissionClosed = class extends Schema.TaggedError()("AdmissionClosed", { message: Schema.String }) {};
|
|
22
|
-
const makeHost = Effect.
|
|
24
|
+
const makeHost = Effect.fn("NodeDurableHost.make")(function* (startWorkers) {
|
|
23
25
|
const runtime = yield* DurableAgentRuntime;
|
|
24
26
|
const config = yield* NodeDurableAgentRuntimeConfig;
|
|
25
27
|
const startupRecovery = yield* runtime.runRecovery;
|
|
26
28
|
const admission = yield* Ref.make(true);
|
|
27
|
-
yield* Effect.addFinalizer(() => Ref.set(admission, false));
|
|
28
29
|
const requireAdmission = Ref.get(admission).pipe(Effect.flatMap((open) => open ? Effect.void : Effect.fail(AdmissionClosed.make({ message: "The host is shutting down; admission is closed." }))));
|
|
29
30
|
const submit = (agent, input, options) => requireAdmission.pipe(Effect.andThen(runtime.submit(agent, input, options)));
|
|
30
31
|
const runWorkers = (worker) => Effect.forEach(Array.from({ length: config.workerConcurrency }, (_, index) => index), () => worker, {
|
|
@@ -32,6 +33,8 @@ const makeHost = Effect.gen(function* () {
|
|
|
32
33
|
discard: true
|
|
33
34
|
});
|
|
34
35
|
const runResolvedWorkers = runWorkers(runtime.runResolvedWorker);
|
|
36
|
+
const run = startWorkers ? Fiber.join(yield* runResolvedWorkers.pipe(Effect.onExit(() => Ref.set(admission, false)), Effect.forkScoped)) : runResolvedWorkers;
|
|
37
|
+
yield* Effect.addFinalizer(() => Ref.set(admission, false));
|
|
35
38
|
return NodeDurableHost.of({
|
|
36
39
|
startupRecovery,
|
|
37
40
|
admissionOpen: Ref.get(admission),
|
|
@@ -46,11 +49,13 @@ const makeHost = Effect.gen(function* () {
|
|
|
46
49
|
wake: runtime.wake,
|
|
47
50
|
scanObligations: runtime.scanObligations,
|
|
48
51
|
runWorkers,
|
|
49
|
-
|
|
52
|
+
run,
|
|
53
|
+
runResolvedWorkers: run
|
|
50
54
|
});
|
|
51
55
|
});
|
|
52
56
|
/**
|
|
53
|
-
* Operational host
|
|
57
|
+
* Operational host service. Prefer the module's `layer` and `run` for managed workers.
|
|
58
|
+
* The static constructors on this class retain explicit, manual worker ownership.
|
|
54
59
|
*
|
|
55
60
|
* Startup gates run during Layer construction, so the service existing implies readiness:
|
|
56
61
|
* configuration was schema-decoded, the SQLite file passed the exact-version compatibility check,
|
|
@@ -77,14 +82,29 @@ var NodeDurableHost = class NodeDurableHost extends Context.Service()("@effect-a
|
|
|
77
82
|
* Host gates over an assembled `NodeDurableAgentRuntime` stack. The runtime Layer owns
|
|
78
83
|
* executable registrations; omission registers no Agents, so resolved work fails closed.
|
|
79
84
|
*/
|
|
80
|
-
static layer = Layer.effect(NodeDurableHost)(makeHost);
|
|
85
|
+
static layer = Layer.effect(NodeDurableHost)(makeHost(false));
|
|
81
86
|
/** The complete DN host: `NodeDurableAgentRuntime.layer(options)` plus the host lifecycle gates. */
|
|
82
87
|
static layerStack(options) {
|
|
83
88
|
const { bindings = [], ...runtimeOptions } = options;
|
|
84
89
|
return NodeDurableHost.layer.pipe(Layer.provideMerge(NodeDurableAgentRuntime.layerWithBindings(bindings, runtimeOptions)));
|
|
85
90
|
}
|
|
86
91
|
};
|
|
92
|
+
/**
|
|
93
|
+
* Acquire a complete Node host and start one bounded, scoped worker pool after recovery.
|
|
94
|
+
* Provide model, tool, instruction, and schema dependencies to this Layer. Reusing the Layer
|
|
95
|
+
* shares the same pool. A worker failure closes admission; observe it with `run` at the process
|
|
96
|
+
* boundary so the application exits and releases the host instead of remaining idle.
|
|
97
|
+
*/
|
|
98
|
+
const layer = (registrations, options) => Layer.effect(NodeDurableHost)(makeHost(true)).pipe(Layer.provideMerge(NodeDurableAgentRuntime.layerRegistered(registrations, options)));
|
|
99
|
+
/**
|
|
100
|
+
* Supervise the host's existing workers without starting another pool. Use with
|
|
101
|
+
* `Effect.provide(HostLive)` and `NodeRuntime.runMain`; race it with a server Effect when
|
|
102
|
+
* the same process also serves requests. Unlike `Layer.launch`, this observes worker failures.
|
|
103
|
+
*/
|
|
104
|
+
const run = Effect.gen(function* () {
|
|
105
|
+
return yield* (yield* NodeDurableHost).run;
|
|
106
|
+
});
|
|
87
107
|
//#endregion
|
|
88
|
-
export { AdmissionClosed, NodeDurableHost, NodeDurableHost_exports as t };
|
|
108
|
+
export { AdmissionClosed, NodeDurableHost, layer, run, NodeDurableHost_exports as t };
|
|
89
109
|
|
|
90
110
|
//# sourceMappingURL=NodeDurableHost.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"NodeDurableHost.mjs","names":[],"sources":["../src/NodeDurableHost.ts"],"sourcesContent":["import { type ThreadId, type SubmissionId } from \"@effect-agent/core/Identifiers\";\nimport {\n type IntegrityReport,\n type ObligationReport,\n type ObligationThresholds,\n type RecoveryExplanation,\n type RetryCommand,\n} from \"@effect-agent/thread/Admin\";\nimport {\n type AgentRegistration,\n type ResolvedBinding,\n type DurableBindingFailure,\n} from \"@effect-agent/thread/AgentRegistration\";\nimport {\n DurableAgentRuntime,\n type DurableAbortFailure,\n type DurableAwaitFailure,\n type DurableExplainFailure,\n type DurableObserveOptions,\n type DurableObligationFailure,\n type DurableRetryFailure,\n type DurableSubmitAgent,\n type DurableSubmitFailure,\n type DurableSubmitOptions,\n type DurableVerifyFailure,\n type DurableWorkerFailure,\n type Receipt,\n type RecoveryReport,\n} from \"@effect-agent/thread/DurableAgentRuntime\";\nimport { type OperationDenied } from \"@effect-agent/thread/OperationAuthorizer\";\nimport { type CanonicalRecordEnvelope } from \"@effect-agent/thread/Records\";\nimport {\n type AbortCommand,\n type AbortIntent,\n type Settlement,\n} from \"@effect-agent/thread/SubmissionLedger\";\nimport {\n type ThreadNotMaterialized,\n type ThreadStoreError,\n} from \"@effect-agent/thread/ThreadStore\";\nimport { type Stream, Context, type Crypto, Effect, Layer, Ref, Schema } from \"effect\";\n\nimport {\n NodeDurableAgentRuntime,\n NodeDurableAgentRuntimeConfig,\n type NodeDurableAgentRuntimeInitializationError,\n type NodeDurableAgentRuntimeOptions,\n type NodeDurableAgentRuntimeServices,\n} from \"./NodeDurableAgentRuntime.ts\";\n\n/**\n * Admission is not open on this host: it is shutting down (deployment §6 step 1, DEPLOY-005).\n * Accepted work is unaffected — only NEW admissions are refused.\n */\nexport class AdmissionClosed extends Schema.TaggedError<AdmissionClosed>()(\"AdmissionClosed\", {\n message: Schema.String,\n}) {}\n\nconst makeHost = Effect.gen(function* () {\n const runtime = yield* DurableAgentRuntime;\n const config = yield* NodeDurableAgentRuntimeConfig;\n\n // Startup gate (deployment §5, plan §host): configuration decoding and storage compatibility\n // already gated this Layer's dependencies; the last gate before admission opens is recovering\n // EVERY nonterminal Submission. Work needing a live Agent Binding is reported `deferred` and\n // stays a visible obligation for `runWorkers`; lanes durably blocked on an Unknown Outcome are\n // reported `unknown` and wait for the authorized `resolveUnknown` path (DUR-017) — they consume\n // no worker permit while the settlement obligation stays owed.\n const startupRecovery = yield* runtime.runRecovery;\n\n const admission = yield* Ref.make(true);\n\n // Shutdown step 1 (DEPLOY-005): admission closes before the ownership drain in the runtime\n // Layer below releases claims and before the stores close in reverse acquisition order.\n yield* Effect.addFinalizer(() => Ref.set(admission, false));\n\n const requireAdmission: Effect.Effect<void, AdmissionClosed> = Ref.get(admission).pipe(\n Effect.flatMap((open) =>\n open\n ? Effect.void\n : Effect.fail(\n AdmissionClosed.make({ message: \"The host is shutting down; admission is closed.\" }),\n ),\n ),\n );\n\n const submit = <InputSchema extends Schema.Top>(\n agent: DurableSubmitAgent<InputSchema>,\n input: InputSchema[\"Type\"],\n options: DurableSubmitOptions,\n ): Effect.Effect<\n Receipt,\n AdmissionClosed | DurableSubmitFailure,\n InputSchema[\"EncodingServices\"]\n > => requireAdmission.pipe(Effect.andThen(runtime.submit(agent, input, options)));\n\n const runWorkers = <A, E, R>(worker: Effect.Effect<A, E, R>): Effect.Effect<void, E, R> =>\n Effect.forEach(\n Array.from({ length: config.workerConcurrency }, (_, index) => index),\n () => worker,\n { concurrency: \"unbounded\", discard: true },\n );\n\n // S2 multi-binding pool: every claimed head resolves its exact registered Binding through\n // the host's exact registrations, so one bounded\n // pool serves parent and child lanes — the spec §12 smallest-pool suspension/wakeup proof\n // runs `workerConcurrency: 1` over exactly this loop.\n const runResolvedWorkers = runWorkers(runtime.runResolvedWorker);\n\n return NodeDurableHost.of({\n startupRecovery,\n admissionOpen: Ref.get(admission),\n submit,\n awaitSettlement: runtime.awaitSettlement,\n observe: runtime.observe,\n abort: runtime.abort,\n explain: runtime.explain,\n explainThread: runtime.explainThread,\n verify: runtime.verify,\n retry: runtime.retry,\n wake: runtime.wake,\n scanObligations: runtime.scanObligations,\n runWorkers,\n runResolvedWorkers,\n });\n});\n\n/**\n * Operational host lifecycle for the DN runtime (deployment §2/§5/§6).\n *\n * Startup gates run during Layer construction, so the service existing implies readiness:\n * configuration was schema-decoded, the SQLite file passed the exact-version compatibility check,\n * and every nonterminal Submission went through one full recovery pass BEFORE admission opened.\n * `startupRecovery` is the auditable evidence of that reconciliation pass.\n *\n * Shutdown runs in reverse Layer order when the owning Scope closes: `submit` starts refusing\n * with `AdmissionClosed` first, then the runtime Layer's ownership drain releases every claim\n * still held so another host can take over the lanes immediately, then the SQLite resources\n * close. Forced termination at any point stays safe — the durability protocol, not graceful\n * shutdown, provides correctness (DEPLOY-006).\n */\nexport class NodeDurableHost extends Context.Service<\n NodeDurableHost,\n {\n /**\n * The recovery decisions executed (or deferred) by this host's startup reconciliation.\n * Reports with the `unknown` disposition identify lanes blocked on Unknown Outcomes that\n * only the authorized DUR-017 resolution path can release.\n */\n readonly startupRecovery: ReadonlyArray<RecoveryReport>;\n /** Admission-role readiness (deployment §7): true until shutdown begins. */\n readonly admissionOpen: Effect.Effect<boolean>;\n /** `DurableAgentRuntime.submit` behind the host admission gate. */\n readonly submit: <InputSchema extends Schema.Top>(\n agent: DurableSubmitAgent<InputSchema>,\n input: InputSchema[\"Type\"],\n options: DurableSubmitOptions,\n ) => Effect.Effect<\n Receipt,\n AdmissionClosed | DurableSubmitFailure,\n InputSchema[\"EncodingServices\"]\n >;\n readonly awaitSettlement: (receipt: Receipt) => Effect.Effect<Settlement, DurableAwaitFailure>;\n readonly observe: (\n receipt: Receipt,\n options?: DurableObserveOptions,\n ) => Stream.Stream<\n CanonicalRecordEnvelope,\n ThreadStoreError | ThreadNotMaterialized | OperationDenied\n >;\n readonly abort: (command: AbortCommand) => Effect.Effect<AbortIntent, DurableAbortFailure>;\n /** `DurableAgentRuntime.explain` — read-only recovery explanation of one Submission (P7). */\n readonly explain: (\n submissionId: SubmissionId,\n ) => Effect.Effect<RecoveryExplanation, DurableExplainFailure>;\n /** `DurableAgentRuntime.explainThread` — explain every nonterminal lane member. */\n readonly explainThread: (\n threadId: ThreadId,\n ) => Effect.Effect<ReadonlyArray<RecoveryExplanation>, DurableExplainFailure>;\n /** `DurableAgentRuntime.verify` — read-only integrity checks, never a repair (P7). */\n readonly verify: (threadId: ThreadId) => Effect.Effect<IntegrityReport, DurableVerifyFailure>;\n /** `DurableAgentRuntime.retry` — audited single-Submission re-drive with typed refusals. */\n readonly retry: (command: RetryCommand) => Effect.Effect<RecoveryReport, DurableRetryFailure>;\n /** `DurableAgentRuntime.wake` — the documented operator liveness nudge for one lane. */\n readonly wake: (threadId: ThreadId) => Effect.Effect<void, OperationDenied>;\n /** `DurableAgentRuntime.scanObligations` — the scan-based DUR-017/OPS-001 report. */\n readonly scanObligations: (\n thresholds: ObligationThresholds,\n ) => Effect.Effect<ObligationReport, DurableObligationFailure>;\n /**\n * Run `workerConcurrency` copies of the given worker effect (typically\n * `DurableAgentRuntime.runWorker(agent)`) until the caller's Scope interrupts them. The\n * bound is the validated finite configuration value; the host never forks daemon fibers.\n */\n readonly runWorkers: <A, E, R>(worker: Effect.Effect<A, E, R>) => Effect.Effect<void, E, R>;\n /**\n * Run `workerConcurrency` copies of `DurableAgentRuntime.runResolvedWorker` over the host's\n * registered Bindings (S2): every claimed head resolves its exact stored Binding before any\n * code runs (SUB-023), so one bounded pool serves parent and attached-child lanes.\n */\n readonly runResolvedWorkers: Effect.Effect<void, DurableWorkerFailure | DurableBindingFailure>;\n }\n>()(\"@effect-agent/platform-node/NodeDurableHost\") {\n /**\n * Compile typed registrations and acquire the complete host in one Layer Scope.\n * Node supplies Crypto; model, tool, instruction, and schema services remain required.\n * Startup recovery and shutdown gates are unchanged. Workers start only when the caller\n * runs runResolvedWorkers; this constructor never starts a background worker.\n */\n static layerRegistered<\n const Entries extends ReadonlyArray<AgentRegistration>,\n ContextError = never,\n ContextRequirements = never,\n AuthorizationError = never,\n AuthorizationRequirements = never,\n >(\n registrations: Entries,\n options: NodeDurableAgentRuntimeOptions<\n ContextError,\n ContextRequirements,\n AuthorizationError,\n AuthorizationRequirements\n >,\n ) {\n return NodeDurableHost.layer.pipe(\n Layer.provideMerge(NodeDurableAgentRuntime.layerRegistered(registrations, options)),\n );\n }\n\n /**\n * Host gates over an assembled `NodeDurableAgentRuntime` stack. The runtime Layer owns\n * executable registrations; omission registers no Agents, so resolved work fails closed.\n */\n static readonly layer: Layer.Layer<\n NodeDurableHost,\n DurableWorkerFailure,\n DurableAgentRuntime | NodeDurableAgentRuntimeConfig\n > = Layer.effect(NodeDurableHost)(makeHost);\n\n /** The complete DN host: `NodeDurableAgentRuntime.layer(options)` plus the host lifecycle gates. */\n static layerStack<\n ContextError = never,\n ContextRequirements = never,\n AuthorizationError = never,\n AuthorizationRequirements = never,\n >(\n options: NodeDurableAgentRuntimeOptions<\n ContextError,\n ContextRequirements,\n AuthorizationError,\n AuthorizationRequirements\n > & { readonly bindings?: ReadonlyArray<ResolvedBinding> },\n ): Layer.Layer<\n NodeDurableHost | NodeDurableAgentRuntimeServices,\n | DurableWorkerFailure\n | NodeDurableAgentRuntimeInitializationError\n | ContextError\n | AuthorizationError,\n Exclude<ContextRequirements | AuthorizationRequirements, Crypto.Crypto>\n > {\n const { bindings = [], ...runtimeOptions } = options;\n\n return NodeDurableHost.layer.pipe(\n Layer.provideMerge(NodeDurableAgentRuntime.layerWithBindings(bindings, runtimeOptions)),\n );\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAsDA,IAAa,kBAAb,cAAqC,OAAO,YAA6B,CAAC,CAAC,mBAAmB,EAC5F,SAAS,OAAO,OAClB,CAAC,CAAC,CAAC,CAAC;AAEJ,MAAM,WAAW,OAAO,IAAI,aAAa;CACvC,MAAM,UAAU,OAAO;CACvB,MAAM,SAAS,OAAO;CAQtB,MAAM,kBAAkB,OAAO,QAAQ;CAEvC,MAAM,YAAY,OAAO,IAAI,KAAK,IAAI;CAItC,OAAO,OAAO,mBAAmB,IAAI,IAAI,WAAW,KAAK,CAAC;CAE1D,MAAM,mBAAyD,IAAI,IAAI,SAAS,CAAC,CAAC,KAChF,OAAO,SAAS,SACd,OACI,OAAO,OACP,OAAO,KACL,gBAAgB,KAAK,EAAE,SAAS,kDAAkD,CAAC,CACrF,CACN,CACF;CAEA,MAAM,UACJ,OACA,OACA,YAKG,iBAAiB,KAAK,OAAO,QAAQ,QAAQ,OAAO,OAAO,OAAO,OAAO,CAAC,CAAC;CAEhF,MAAM,cAAuB,WAC3B,OAAO,QACL,MAAM,KAAK,EAAE,QAAQ,OAAO,kBAAkB,IAAI,GAAG,UAAU,KAAK,SAC9D,QACN;EAAE,aAAa;EAAa,SAAS;CAAK,CAC5C;CAMF,MAAM,qBAAqB,WAAW,QAAQ,iBAAiB;CAE/D,OAAO,gBAAgB,GAAG;EACxB;EACA,eAAe,IAAI,IAAI,SAAS;EAChC;EACA,iBAAiB,QAAQ;EACzB,SAAS,QAAQ;EACjB,OAAO,QAAQ;EACf,SAAS,QAAQ;EACjB,eAAe,QAAQ;EACvB,QAAQ,QAAQ;EAChB,OAAO,QAAQ;EACf,MAAM,QAAQ;EACd,iBAAiB,QAAQ;EACzB;EACA;CACF,CAAC;AACH,CAAC;;;;;;;;;;;;;;;AAgBD,IAAa,kBAAb,MAAa,wBAAwB,QAAQ,QA6D3C,CAAC,CAAC,6CAA6C,CAAC,CAAC;;;;;;;CAOjD,OAAO,gBAOL,eACA,SAMA;EACA,OAAO,gBAAgB,MAAM,KAC3B,MAAM,aAAa,wBAAwB,gBAAgB,eAAe,OAAO,CAAC,CACpF;CACF;;;;;CAMA,OAAgB,QAIZ,MAAM,OAAO,eAAe,CAAC,CAAC,QAAQ;;CAG1C,OAAO,WAML,SAaA;EACA,MAAM,EAAE,WAAW,CAAC,GAAG,GAAG,mBAAmB;EAE7C,OAAO,gBAAgB,MAAM,KAC3B,MAAM,aAAa,wBAAwB,kBAAkB,UAAU,cAAc,CAAC,CACxF;CACF;AACF"}
|
|
1
|
+
{"version":3,"file":"NodeDurableHost.mjs","names":[],"sources":["../src/NodeDurableHost.ts"],"sourcesContent":["import { type ThreadId, type SubmissionId } from \"@effect-agent/core/Identifiers\";\nimport {\n type IntegrityReport,\n type ObligationReport,\n type ObligationThresholds,\n type RecoveryExplanation,\n type RetryCommand,\n} from \"@effect-agent/thread/Admin\";\nimport {\n type AgentRegistration,\n type ResolvedBinding,\n type DurableBindingFailure,\n} from \"@effect-agent/thread/AgentRegistration\";\nimport {\n DurableAgentRuntime,\n type DurableAbortFailure,\n type DurableAwaitFailure,\n type DurableExplainFailure,\n type DurableObserveOptions,\n type DurableObligationFailure,\n type DurableRetryFailure,\n type DurableSubmitAgent,\n type DurableSubmitFailure,\n type DurableSubmitOptions,\n type DurableVerifyFailure,\n type DurableWorkerFailure,\n type Receipt,\n type RecoveryReport,\n} from \"@effect-agent/thread/DurableAgentRuntime\";\nimport { type OperationDenied } from \"@effect-agent/thread/OperationAuthorizer\";\nimport { type CanonicalRecordEnvelope } from \"@effect-agent/thread/Records\";\nimport {\n type AbortCommand,\n type AbortIntent,\n type Settlement,\n} from \"@effect-agent/thread/SubmissionLedger\";\nimport {\n type ThreadNotMaterialized,\n type ThreadStoreError,\n} from \"@effect-agent/thread/ThreadStore\";\nimport { type Stream, Context, type Crypto, Effect, Fiber, Layer, Ref, Schema } from \"effect\";\n\nimport {\n NodeDurableAgentRuntime,\n NodeDurableAgentRuntimeConfig,\n type NodeDurableAgentRuntimeInitializationError,\n type NodeDurableAgentRuntimeOptions,\n type NodeDurableAgentRuntimeServices,\n} from \"./NodeDurableAgentRuntime.ts\";\n\n/**\n * Admission is not open on this host: it is shutting down (deployment §6 step 1, DEPLOY-005).\n * Accepted work is unaffected — only NEW admissions are refused.\n */\nexport class AdmissionClosed extends Schema.TaggedError<AdmissionClosed>()(\"AdmissionClosed\", {\n message: Schema.String,\n}) {}\n\nconst makeHost = Effect.fn(\"NodeDurableHost.make\")(function* (startWorkers: boolean) {\n const runtime = yield* DurableAgentRuntime;\n const config = yield* NodeDurableAgentRuntimeConfig;\n\n // Startup gate (deployment §5, plan §host): configuration decoding and storage compatibility\n // already gated this Layer's dependencies; the last gate before admission opens is recovering\n // EVERY nonterminal Submission. Work needing a live Agent Binding is reported `deferred` and\n // stays a visible obligation for `runWorkers`; lanes durably blocked on an Unknown Outcome are\n // reported `unknown` and wait for the authorized `resolveUnknown` path (DUR-017) — they consume\n // no worker permit while the settlement obligation stays owed.\n const startupRecovery = yield* runtime.runRecovery;\n\n const admission = yield* Ref.make(true);\n\n const requireAdmission: Effect.Effect<void, AdmissionClosed> = Ref.get(admission).pipe(\n Effect.flatMap((open) =>\n open\n ? Effect.void\n : Effect.fail(\n AdmissionClosed.make({ message: \"The host is shutting down; admission is closed.\" }),\n ),\n ),\n );\n\n const submit = <InputSchema extends Schema.Top>(\n agent: DurableSubmitAgent<InputSchema>,\n input: InputSchema[\"Type\"],\n options: DurableSubmitOptions,\n ): Effect.Effect<\n Receipt,\n AdmissionClosed | DurableSubmitFailure,\n InputSchema[\"EncodingServices\"]\n > => requireAdmission.pipe(Effect.andThen(runtime.submit(agent, input, options)));\n\n const runWorkers = <A, E, R>(worker: Effect.Effect<A, E, R>): Effect.Effect<void, E, R> =>\n Effect.forEach(\n Array.from({ length: config.workerConcurrency }, (_, index) => index),\n () => worker,\n { concurrency: \"unbounded\", discard: true },\n );\n\n // S2 multi-binding pool: every claimed head resolves its exact registered Binding through\n // the host's exact registrations, so one bounded\n // pool serves parent and child lanes — the spec §12 smallest-pool suspension/wakeup proof\n // runs `workerConcurrency: 1` over exactly this loop.\n const runResolvedWorkers = runWorkers(runtime.runResolvedWorker);\n\n const run = startWorkers\n ? Fiber.join(\n yield* runResolvedWorkers.pipe(\n Effect.onExit(() => Ref.set(admission, false)),\n Effect.forkScoped,\n ),\n )\n : runResolvedWorkers;\n\n // Register after the worker fiber: close admission, interrupt/join workers, drain\n // runtime ownership, then close storage and captured application services.\n yield* Effect.addFinalizer(() => Ref.set(admission, false));\n\n return NodeDurableHost.of({\n startupRecovery,\n admissionOpen: Ref.get(admission),\n submit,\n awaitSettlement: runtime.awaitSettlement,\n observe: runtime.observe,\n abort: runtime.abort,\n explain: runtime.explain,\n explainThread: runtime.explainThread,\n verify: runtime.verify,\n retry: runtime.retry,\n wake: runtime.wake,\n scanObligations: runtime.scanObligations,\n runWorkers,\n run,\n runResolvedWorkers: run,\n });\n});\n\n/**\n * Operational host service. Prefer the module's `layer` and `run` for managed workers.\n * The static constructors on this class retain explicit, manual worker ownership.\n *\n * Startup gates run during Layer construction, so the service existing implies readiness:\n * configuration was schema-decoded, the SQLite file passed the exact-version compatibility check,\n * and every nonterminal Submission went through one full recovery pass BEFORE admission opened.\n * `startupRecovery` is the auditable evidence of that reconciliation pass.\n *\n * Shutdown runs in reverse Layer order when the owning Scope closes: `submit` starts refusing\n * with `AdmissionClosed` first, then the runtime Layer's ownership drain releases every claim\n * still held so another host can take over the lanes immediately, then the SQLite resources\n * close. Forced termination at any point stays safe — the durability protocol, not graceful\n * shutdown, provides correctness (DEPLOY-006).\n */\nexport class NodeDurableHost extends Context.Service<\n NodeDurableHost,\n {\n /**\n * The recovery decisions executed (or deferred) by this host's startup reconciliation.\n * Reports with the `unknown` disposition identify lanes blocked on Unknown Outcomes that\n * only the authorized DUR-017 resolution path can release.\n */\n readonly startupRecovery: ReadonlyArray<RecoveryReport>;\n /** Admission-role readiness (deployment §7): true until shutdown begins. */\n readonly admissionOpen: Effect.Effect<boolean>;\n /** Observe the managed worker pool, preserving its failure. Manual hosts start their pool here. */\n readonly run: Effect.Effect<void, DurableWorkerFailure | DurableBindingFailure>;\n /** `DurableAgentRuntime.submit` behind the host admission gate. */\n readonly submit: <InputSchema extends Schema.Top>(\n agent: DurableSubmitAgent<InputSchema>,\n input: InputSchema[\"Type\"],\n options: DurableSubmitOptions,\n ) => Effect.Effect<\n Receipt,\n AdmissionClosed | DurableSubmitFailure,\n InputSchema[\"EncodingServices\"]\n >;\n readonly awaitSettlement: (receipt: Receipt) => Effect.Effect<Settlement, DurableAwaitFailure>;\n readonly observe: (\n receipt: Receipt,\n options?: DurableObserveOptions,\n ) => Stream.Stream<\n CanonicalRecordEnvelope,\n ThreadStoreError | ThreadNotMaterialized | OperationDenied\n >;\n readonly abort: (command: AbortCommand) => Effect.Effect<AbortIntent, DurableAbortFailure>;\n /** `DurableAgentRuntime.explain` — read-only recovery explanation of one Submission (P7). */\n readonly explain: (\n submissionId: SubmissionId,\n ) => Effect.Effect<RecoveryExplanation, DurableExplainFailure>;\n /** `DurableAgentRuntime.explainThread` — explain every nonterminal lane member. */\n readonly explainThread: (\n threadId: ThreadId,\n ) => Effect.Effect<ReadonlyArray<RecoveryExplanation>, DurableExplainFailure>;\n /** `DurableAgentRuntime.verify` — read-only integrity checks, never a repair (P7). */\n readonly verify: (threadId: ThreadId) => Effect.Effect<IntegrityReport, DurableVerifyFailure>;\n /** `DurableAgentRuntime.retry` — audited single-Submission re-drive with typed refusals. */\n readonly retry: (command: RetryCommand) => Effect.Effect<RecoveryReport, DurableRetryFailure>;\n /** `DurableAgentRuntime.wake` — the documented operator liveness nudge for one lane. */\n readonly wake: (threadId: ThreadId) => Effect.Effect<void, OperationDenied>;\n /** `DurableAgentRuntime.scanObligations` — the scan-based DUR-017/OPS-001 report. */\n readonly scanObligations: (\n thresholds: ObligationThresholds,\n ) => Effect.Effect<ObligationReport, DurableObligationFailure>;\n /**\n * Run `workerConcurrency` copies of the given worker effect (typically\n * `DurableAgentRuntime.runWorker(agent)`) until the caller's Scope interrupts them. The\n * bound is the validated finite configuration value; the host never forks daemon fibers.\n */\n readonly runWorkers: <A, E, R>(worker: Effect.Effect<A, E, R>) => Effect.Effect<void, E, R>;\n /**\n * Run `workerConcurrency` copies of `DurableAgentRuntime.runResolvedWorker` over the host's\n * registered Bindings (S2): every claimed head resolves its exact stored Binding before any\n * code runs (SUB-023), so one bounded pool serves parent and attached-child lanes.\n * Hosts built with the module-level `layer` instead join their existing pool.\n */\n readonly runResolvedWorkers: Effect.Effect<void, DurableWorkerFailure | DurableBindingFailure>;\n }\n>()(\"@effect-agent/platform-node/NodeDurableHost\") {\n /**\n * Compile typed registrations and acquire the complete host in one Layer Scope.\n * Node supplies Crypto; model, tool, instruction, and schema services remain required.\n * Startup recovery and shutdown gates are unchanged. Workers start only when the caller\n * runs runResolvedWorkers; this constructor never starts a background worker.\n */\n static layerRegistered<\n const Entries extends ReadonlyArray<AgentRegistration>,\n ContextError = never,\n ContextRequirements = never,\n AuthorizationError = never,\n AuthorizationRequirements = never,\n >(\n registrations: Entries,\n options: NodeDurableAgentRuntimeOptions<\n ContextError,\n ContextRequirements,\n AuthorizationError,\n AuthorizationRequirements\n >,\n ) {\n return NodeDurableHost.layer.pipe(\n Layer.provideMerge(NodeDurableAgentRuntime.layerRegistered(registrations, options)),\n );\n }\n\n /**\n * Host gates over an assembled `NodeDurableAgentRuntime` stack. The runtime Layer owns\n * executable registrations; omission registers no Agents, so resolved work fails closed.\n */\n static readonly layer: Layer.Layer<\n NodeDurableHost,\n DurableWorkerFailure,\n DurableAgentRuntime | NodeDurableAgentRuntimeConfig\n > = Layer.effect(NodeDurableHost)(makeHost(false));\n\n /** The complete DN host: `NodeDurableAgentRuntime.layer(options)` plus the host lifecycle gates. */\n static layerStack<\n ContextError = never,\n ContextRequirements = never,\n AuthorizationError = never,\n AuthorizationRequirements = never,\n >(\n options: NodeDurableAgentRuntimeOptions<\n ContextError,\n ContextRequirements,\n AuthorizationError,\n AuthorizationRequirements\n > & { readonly bindings?: ReadonlyArray<ResolvedBinding> },\n ): Layer.Layer<\n NodeDurableHost | NodeDurableAgentRuntimeServices,\n | DurableWorkerFailure\n | NodeDurableAgentRuntimeInitializationError\n | ContextError\n | AuthorizationError,\n Exclude<ContextRequirements | AuthorizationRequirements, Crypto.Crypto>\n > {\n const { bindings = [], ...runtimeOptions } = options;\n\n return NodeDurableHost.layer.pipe(\n Layer.provideMerge(NodeDurableAgentRuntime.layerWithBindings(bindings, runtimeOptions)),\n );\n }\n}\n\n/**\n * Acquire a complete Node host and start one bounded, scoped worker pool after recovery.\n * Provide model, tool, instruction, and schema dependencies to this Layer. Reusing the Layer\n * shares the same pool. A worker failure closes admission; observe it with `run` at the process\n * boundary so the application exits and releases the host instead of remaining idle.\n */\nexport const layer = <\n const Entries extends ReadonlyArray<AgentRegistration>,\n ContextError = never,\n ContextRequirements = never,\n AuthorizationError = never,\n AuthorizationRequirements = never,\n>(\n registrations: Entries,\n options: NodeDurableAgentRuntimeOptions<\n ContextError,\n ContextRequirements,\n AuthorizationError,\n AuthorizationRequirements\n >,\n) =>\n Layer.effect(NodeDurableHost)(makeHost(true)).pipe(\n Layer.provideMerge(NodeDurableAgentRuntime.layerRegistered(registrations, options)),\n );\n\n/**\n * Supervise the host's existing workers without starting another pool. Use with\n * `Effect.provide(HostLive)` and `NodeRuntime.runMain`; race it with a server Effect when\n * the same process also serves requests. Unlike `Layer.launch`, this observes worker failures.\n */\nexport const run = Effect.gen(function* () {\n const host = yield* NodeDurableHost;\n\n return yield* host.run;\n});\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAsDA,IAAa,kBAAb,cAAqC,OAAO,YAA6B,CAAC,CAAC,mBAAmB,EAC5F,SAAS,OAAO,OAClB,CAAC,CAAC,CAAC,CAAC;AAEJ,MAAM,WAAW,OAAO,GAAG,sBAAsB,CAAC,CAAC,WAAW,cAAuB;CACnF,MAAM,UAAU,OAAO;CACvB,MAAM,SAAS,OAAO;CAQtB,MAAM,kBAAkB,OAAO,QAAQ;CAEvC,MAAM,YAAY,OAAO,IAAI,KAAK,IAAI;CAEtC,MAAM,mBAAyD,IAAI,IAAI,SAAS,CAAC,CAAC,KAChF,OAAO,SAAS,SACd,OACI,OAAO,OACP,OAAO,KACL,gBAAgB,KAAK,EAAE,SAAS,kDAAkD,CAAC,CACrF,CACN,CACF;CAEA,MAAM,UACJ,OACA,OACA,YAKG,iBAAiB,KAAK,OAAO,QAAQ,QAAQ,OAAO,OAAO,OAAO,OAAO,CAAC,CAAC;CAEhF,MAAM,cAAuB,WAC3B,OAAO,QACL,MAAM,KAAK,EAAE,QAAQ,OAAO,kBAAkB,IAAI,GAAG,UAAU,KAAK,SAC9D,QACN;EAAE,aAAa;EAAa,SAAS;CAAK,CAC5C;CAMF,MAAM,qBAAqB,WAAW,QAAQ,iBAAiB;CAE/D,MAAM,MAAM,eACR,MAAM,KACJ,OAAO,mBAAmB,KACxB,OAAO,aAAa,IAAI,IAAI,WAAW,KAAK,CAAC,GAC7C,OAAO,UACT,CACF,IACA;CAIJ,OAAO,OAAO,mBAAmB,IAAI,IAAI,WAAW,KAAK,CAAC;CAE1D,OAAO,gBAAgB,GAAG;EACxB;EACA,eAAe,IAAI,IAAI,SAAS;EAChC;EACA,iBAAiB,QAAQ;EACzB,SAAS,QAAQ;EACjB,OAAO,QAAQ;EACf,SAAS,QAAQ;EACjB,eAAe,QAAQ;EACvB,QAAQ,QAAQ;EAChB,OAAO,QAAQ;EACf,MAAM,QAAQ;EACd,iBAAiB,QAAQ;EACzB;EACA;EACA,oBAAoB;CACtB,CAAC;AACH,CAAC;;;;;;;;;;;;;;;;AAiBD,IAAa,kBAAb,MAAa,wBAAwB,QAAQ,QAgE3C,CAAC,CAAC,6CAA6C,CAAC,CAAC;;;;;;;CAOjD,OAAO,gBAOL,eACA,SAMA;EACA,OAAO,gBAAgB,MAAM,KAC3B,MAAM,aAAa,wBAAwB,gBAAgB,eAAe,OAAO,CAAC,CACpF;CACF;;;;;CAMA,OAAgB,QAIZ,MAAM,OAAO,eAAe,CAAC,CAAC,SAAS,KAAK,CAAC;;CAGjD,OAAO,WAML,SAaA;EACA,MAAM,EAAE,WAAW,CAAC,GAAG,GAAG,mBAAmB;EAE7C,OAAO,gBAAgB,MAAM,KAC3B,MAAM,aAAa,wBAAwB,kBAAkB,UAAU,cAAc,CAAC,CACxF;CACF;AACF;;;;;;;AAQA,MAAa,SAOX,eACA,YAOA,MAAM,OAAO,eAAe,CAAC,CAAC,SAAS,IAAI,CAAC,CAAC,CAAC,KAC5C,MAAM,aAAa,wBAAwB,gBAAgB,eAAe,OAAO,CAAC,CACpF;;;;;;AAOF,MAAa,MAAM,OAAO,IAAI,aAAa;CAGzC,OAAO,QAAO,OAFM,gBAAA,CAED;AACrB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"name":"@effect-agent/platform-node","version":"0.1.0-beta.
|
|
1
|
+
{"name":"@effect-agent/platform-node","version":"0.1.0-beta.49","dependencies":{"@effect-agent/core":"0.1.0-beta.49","@effect-agent/engine":"0.1.0-beta.49","@effect-agent/storage-sqlite":"0.1.0-beta.49","@effect-agent/thread":"0.1.0-beta.49","@effect-agent/workflow":"0.1.0-beta.49","@effect/platform-node":"4.0.0-rc.112","@effect/sql-sqlite-node":"4.0.0-rc.112"},"devDependencies":{"@effect-agent/capabilities":"0.1.0-beta.49","@effect/vitest":"4.0.0-rc.112","effect":"4.0.0-rc.112","typescript":"7.0.2","vite-plus":"0.3.0"},"peerDependencies":{"effect":"^4.0.0-rc.112"},"exports":{".":{"types":"./dist/index.d.mts","default":"./dist/index.mjs"},"./NodeDurableAgentRuntime":{"types":"./dist/NodeDurableAgentRuntime.d.mts","default":"./dist/NodeDurableAgentRuntime.mjs"},"./NodeDurableHost":{"types":"./dist/NodeDurableHost.d.mts","default":"./dist/NodeDurableHost.mjs"},"./NodeScheduling":{"types":"./dist/NodeScheduling.d.mts","default":"./dist/NodeScheduling.mjs"},"./NodeSubscriptions":{"types":"./dist/NodeSubscriptions.d.mts","default":"./dist/NodeSubscriptions.mjs"},"./NodeWakeScheduler":{"types":"./dist/NodeWakeScheduler.d.mts","default":"./dist/NodeWakeScheduler.mjs"},"./NodeWorkflow":{"types":"./dist/NodeWorkflow.d.mts","default":"./dist/NodeWorkflow.mjs"}},"description":"Class DN layer assembly for Effect Agent: the durable Node/SQLite runtime host, wake scheduler, and ownership drain.","license":"MIT","repository":{"type":"git","url":"git+https://github.com/danieljvdm/effect-agent.git","directory":"packages/platform-node"},"files":["dist","src"],"type":"module","sideEffects":[],"publishConfig":{"access":"public"},"scripts":{"build":"vp pack","check":"tsc --noEmit -p tsconfig.json","test":"vp test --passWithNoTests"}}
|
|
@@ -359,6 +359,7 @@ export const ownershipDrainLayer: Layer.Layer<SubmissionLedger, never, Submissio
|
|
|
359
359
|
finalizeSettlement: (request) =>
|
|
360
360
|
ledger.finalizeSettlement(request).pipe(Effect.tap(() => untrack(request.submissionId))),
|
|
361
361
|
requestAbort: ledger.requestAbort,
|
|
362
|
+
readAbortIntent: ledger.readAbortIntent,
|
|
362
363
|
claimJoining: ledger.claimJoining,
|
|
363
364
|
markJoined: ledger.markJoined,
|
|
364
365
|
revertJoining: ledger.revertJoining,
|
package/src/NodeDurableHost.ts
CHANGED
|
@@ -38,7 +38,7 @@ import {
|
|
|
38
38
|
type ThreadNotMaterialized,
|
|
39
39
|
type ThreadStoreError,
|
|
40
40
|
} from "@effect-agent/thread/ThreadStore";
|
|
41
|
-
import { type Stream, Context, type Crypto, Effect, Layer, Ref, Schema } from "effect";
|
|
41
|
+
import { type Stream, Context, type Crypto, Effect, Fiber, Layer, Ref, Schema } from "effect";
|
|
42
42
|
|
|
43
43
|
import {
|
|
44
44
|
NodeDurableAgentRuntime,
|
|
@@ -56,7 +56,7 @@ export class AdmissionClosed extends Schema.TaggedError<AdmissionClosed>()("Admi
|
|
|
56
56
|
message: Schema.String,
|
|
57
57
|
}) {}
|
|
58
58
|
|
|
59
|
-
const makeHost = Effect.
|
|
59
|
+
const makeHost = Effect.fn("NodeDurableHost.make")(function* (startWorkers: boolean) {
|
|
60
60
|
const runtime = yield* DurableAgentRuntime;
|
|
61
61
|
const config = yield* NodeDurableAgentRuntimeConfig;
|
|
62
62
|
|
|
@@ -70,10 +70,6 @@ const makeHost = Effect.gen(function* () {
|
|
|
70
70
|
|
|
71
71
|
const admission = yield* Ref.make(true);
|
|
72
72
|
|
|
73
|
-
// Shutdown step 1 (DEPLOY-005): admission closes before the ownership drain in the runtime
|
|
74
|
-
// Layer below releases claims and before the stores close in reverse acquisition order.
|
|
75
|
-
yield* Effect.addFinalizer(() => Ref.set(admission, false));
|
|
76
|
-
|
|
77
73
|
const requireAdmission: Effect.Effect<void, AdmissionClosed> = Ref.get(admission).pipe(
|
|
78
74
|
Effect.flatMap((open) =>
|
|
79
75
|
open
|
|
@@ -107,6 +103,19 @@ const makeHost = Effect.gen(function* () {
|
|
|
107
103
|
// runs `workerConcurrency: 1` over exactly this loop.
|
|
108
104
|
const runResolvedWorkers = runWorkers(runtime.runResolvedWorker);
|
|
109
105
|
|
|
106
|
+
const run = startWorkers
|
|
107
|
+
? Fiber.join(
|
|
108
|
+
yield* runResolvedWorkers.pipe(
|
|
109
|
+
Effect.onExit(() => Ref.set(admission, false)),
|
|
110
|
+
Effect.forkScoped,
|
|
111
|
+
),
|
|
112
|
+
)
|
|
113
|
+
: runResolvedWorkers;
|
|
114
|
+
|
|
115
|
+
// Register after the worker fiber: close admission, interrupt/join workers, drain
|
|
116
|
+
// runtime ownership, then close storage and captured application services.
|
|
117
|
+
yield* Effect.addFinalizer(() => Ref.set(admission, false));
|
|
118
|
+
|
|
110
119
|
return NodeDurableHost.of({
|
|
111
120
|
startupRecovery,
|
|
112
121
|
admissionOpen: Ref.get(admission),
|
|
@@ -121,12 +130,14 @@ const makeHost = Effect.gen(function* () {
|
|
|
121
130
|
wake: runtime.wake,
|
|
122
131
|
scanObligations: runtime.scanObligations,
|
|
123
132
|
runWorkers,
|
|
124
|
-
|
|
133
|
+
run,
|
|
134
|
+
runResolvedWorkers: run,
|
|
125
135
|
});
|
|
126
136
|
});
|
|
127
137
|
|
|
128
138
|
/**
|
|
129
|
-
* Operational host
|
|
139
|
+
* Operational host service. Prefer the module's `layer` and `run` for managed workers.
|
|
140
|
+
* The static constructors on this class retain explicit, manual worker ownership.
|
|
130
141
|
*
|
|
131
142
|
* Startup gates run during Layer construction, so the service existing implies readiness:
|
|
132
143
|
* configuration was schema-decoded, the SQLite file passed the exact-version compatibility check,
|
|
@@ -150,6 +161,8 @@ export class NodeDurableHost extends Context.Service<
|
|
|
150
161
|
readonly startupRecovery: ReadonlyArray<RecoveryReport>;
|
|
151
162
|
/** Admission-role readiness (deployment §7): true until shutdown begins. */
|
|
152
163
|
readonly admissionOpen: Effect.Effect<boolean>;
|
|
164
|
+
/** Observe the managed worker pool, preserving its failure. Manual hosts start their pool here. */
|
|
165
|
+
readonly run: Effect.Effect<void, DurableWorkerFailure | DurableBindingFailure>;
|
|
153
166
|
/** `DurableAgentRuntime.submit` behind the host admission gate. */
|
|
154
167
|
readonly submit: <InputSchema extends Schema.Top>(
|
|
155
168
|
agent: DurableSubmitAgent<InputSchema>,
|
|
@@ -197,6 +210,7 @@ export class NodeDurableHost extends Context.Service<
|
|
|
197
210
|
* Run `workerConcurrency` copies of `DurableAgentRuntime.runResolvedWorker` over the host's
|
|
198
211
|
* registered Bindings (S2): every claimed head resolves its exact stored Binding before any
|
|
199
212
|
* code runs (SUB-023), so one bounded pool serves parent and attached-child lanes.
|
|
213
|
+
* Hosts built with the module-level `layer` instead join their existing pool.
|
|
200
214
|
*/
|
|
201
215
|
readonly runResolvedWorkers: Effect.Effect<void, DurableWorkerFailure | DurableBindingFailure>;
|
|
202
216
|
}
|
|
@@ -235,7 +249,7 @@ export class NodeDurableHost extends Context.Service<
|
|
|
235
249
|
NodeDurableHost,
|
|
236
250
|
DurableWorkerFailure,
|
|
237
251
|
DurableAgentRuntime | NodeDurableAgentRuntimeConfig
|
|
238
|
-
> = Layer.effect(NodeDurableHost)(makeHost);
|
|
252
|
+
> = Layer.effect(NodeDurableHost)(makeHost(false));
|
|
239
253
|
|
|
240
254
|
/** The complete DN host: `NodeDurableAgentRuntime.layer(options)` plus the host lifecycle gates. */
|
|
241
255
|
static layerStack<
|
|
@@ -265,3 +279,39 @@ export class NodeDurableHost extends Context.Service<
|
|
|
265
279
|
);
|
|
266
280
|
}
|
|
267
281
|
}
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* Acquire a complete Node host and start one bounded, scoped worker pool after recovery.
|
|
285
|
+
* Provide model, tool, instruction, and schema dependencies to this Layer. Reusing the Layer
|
|
286
|
+
* shares the same pool. A worker failure closes admission; observe it with `run` at the process
|
|
287
|
+
* boundary so the application exits and releases the host instead of remaining idle.
|
|
288
|
+
*/
|
|
289
|
+
export const layer = <
|
|
290
|
+
const Entries extends ReadonlyArray<AgentRegistration>,
|
|
291
|
+
ContextError = never,
|
|
292
|
+
ContextRequirements = never,
|
|
293
|
+
AuthorizationError = never,
|
|
294
|
+
AuthorizationRequirements = never,
|
|
295
|
+
>(
|
|
296
|
+
registrations: Entries,
|
|
297
|
+
options: NodeDurableAgentRuntimeOptions<
|
|
298
|
+
ContextError,
|
|
299
|
+
ContextRequirements,
|
|
300
|
+
AuthorizationError,
|
|
301
|
+
AuthorizationRequirements
|
|
302
|
+
>,
|
|
303
|
+
) =>
|
|
304
|
+
Layer.effect(NodeDurableHost)(makeHost(true)).pipe(
|
|
305
|
+
Layer.provideMerge(NodeDurableAgentRuntime.layerRegistered(registrations, options)),
|
|
306
|
+
);
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* Supervise the host's existing workers without starting another pool. Use with
|
|
310
|
+
* `Effect.provide(HostLive)` and `NodeRuntime.runMain`; race it with a server Effect when
|
|
311
|
+
* the same process also serves requests. Unlike `Layer.launch`, this observes worker failures.
|
|
312
|
+
*/
|
|
313
|
+
export const run = Effect.gen(function* () {
|
|
314
|
+
const host = yield* NodeDurableHost;
|
|
315
|
+
|
|
316
|
+
return yield* host.run;
|
|
317
|
+
});
|