@smthrs/control 0.0.0-stage → 1.0.0-rc.4
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/CHANGELOG.md +194 -0
- package/LICENSE +21 -0
- package/README.md +168 -2
- package/dist/cjs/ApprovalAuthority.d.ts +73 -0
- package/dist/cjs/ApprovalAuthority.d.ts.map +1 -0
- package/dist/cjs/ApprovalAuthority.js +62 -0
- package/dist/cjs/ApprovalAuthority.js.map +7 -0
- package/dist/cjs/Cancellation.d.ts +107 -0
- package/dist/cjs/Cancellation.d.ts.map +1 -0
- package/dist/cjs/Cancellation.js +72 -0
- package/dist/cjs/Cancellation.js.map +7 -0
- package/dist/cjs/Channels.d.ts +170 -0
- package/dist/cjs/Channels.d.ts.map +1 -0
- package/dist/cjs/Channels.js +278 -0
- package/dist/cjs/Channels.js.map +7 -0
- package/dist/cjs/Control.d.ts +202 -0
- package/dist/cjs/Control.d.ts.map +1 -0
- package/dist/cjs/Control.js +47 -0
- package/dist/cjs/Control.js.map +7 -0
- package/dist/cjs/ControlClient.d.ts +52 -0
- package/dist/cjs/ControlClient.d.ts.map +1 -0
- package/dist/cjs/ControlClient.js +191 -0
- package/dist/cjs/ControlClient.js.map +7 -0
- package/dist/cjs/ControlError.d.ts +318 -0
- package/dist/cjs/ControlError.d.ts.map +1 -0
- package/dist/cjs/ControlError.js +249 -0
- package/dist/cjs/ControlError.js.map +7 -0
- package/dist/cjs/ControlExecutor.d.ts +372 -0
- package/dist/cjs/ControlExecutor.d.ts.map +1 -0
- package/dist/cjs/ControlExecutor.js +123 -0
- package/dist/cjs/ControlExecutor.js.map +7 -0
- package/dist/cjs/ControlFacts.d.ts +454 -0
- package/dist/cjs/ControlFacts.d.ts.map +1 -0
- package/dist/cjs/ControlFacts.js +261 -0
- package/dist/cjs/ControlFacts.js.map +7 -0
- package/dist/cjs/ControlLive.d.ts +23 -0
- package/dist/cjs/ControlLive.d.ts.map +1 -0
- package/dist/cjs/ControlLive.js +1293 -0
- package/dist/cjs/ControlLive.js.map +7 -0
- package/dist/cjs/ControlRpcs.d.ts +1204 -0
- package/dist/cjs/ControlRpcs.d.ts.map +1 -0
- package/dist/cjs/ControlRpcs.js +247 -0
- package/dist/cjs/ControlRpcs.js.map +7 -0
- package/dist/cjs/ControlRuntime.d.ts +635 -0
- package/dist/cjs/ControlRuntime.d.ts.map +1 -0
- package/dist/cjs/ControlRuntime.js +744 -0
- package/dist/cjs/ControlRuntime.js.map +7 -0
- package/dist/cjs/ControlSchema.d.ts +2642 -0
- package/dist/cjs/ControlSchema.d.ts.map +1 -0
- package/dist/cjs/ControlSchema.js +634 -0
- package/dist/cjs/ControlSchema.js.map +7 -0
- package/dist/cjs/ControlServer.d.ts +51 -0
- package/dist/cjs/ControlServer.d.ts.map +1 -0
- package/dist/cjs/ControlServer.js +121 -0
- package/dist/cjs/ControlServer.js.map +7 -0
- package/dist/cjs/Credential.d.ts +136 -0
- package/dist/cjs/Credential.d.ts.map +1 -0
- package/dist/cjs/Credential.js +168 -0
- package/dist/cjs/Credential.js.map +7 -0
- package/dist/cjs/CredentialCipher.d.ts +90 -0
- package/dist/cjs/CredentialCipher.d.ts.map +1 -0
- package/dist/cjs/CredentialCipher.js +45 -0
- package/dist/cjs/CredentialCipher.js.map +7 -0
- package/dist/cjs/CredentialStore.d.ts +97 -0
- package/dist/cjs/CredentialStore.d.ts.map +1 -0
- package/dist/cjs/CredentialStore.js +81 -0
- package/dist/cjs/CredentialStore.js.map +7 -0
- package/dist/cjs/DispatchReader.d.ts +112 -0
- package/dist/cjs/DispatchReader.d.ts.map +1 -0
- package/dist/cjs/DispatchReader.js +45 -0
- package/dist/cjs/DispatchReader.js.map +7 -0
- package/dist/cjs/Health.d.ts +333 -0
- package/dist/cjs/Health.d.ts.map +1 -0
- package/dist/cjs/Health.js +311 -0
- package/dist/cjs/Health.js.map +7 -0
- package/dist/cjs/JevSessionChecker.d.ts +57 -0
- package/dist/cjs/JevSessionChecker.d.ts.map +1 -0
- package/dist/cjs/JevSessionChecker.js +113 -0
- package/dist/cjs/JevSessionChecker.js.map +7 -0
- package/dist/cjs/Lineage.d.ts +131 -0
- package/dist/cjs/Lineage.d.ts.map +1 -0
- package/dist/cjs/Lineage.js +81 -0
- package/dist/cjs/Lineage.js.map +7 -0
- package/dist/cjs/Migrations.d.ts +34 -0
- package/dist/cjs/Migrations.d.ts.map +1 -0
- package/dist/cjs/Migrations.js +60 -0
- package/dist/cjs/Migrations.js.map +7 -0
- package/dist/cjs/Monitor.d.ts +282 -0
- package/dist/cjs/Monitor.d.ts.map +1 -0
- package/dist/cjs/Monitor.js +283 -0
- package/dist/cjs/Monitor.js.map +7 -0
- package/dist/cjs/ScopedToken.d.ts +193 -0
- package/dist/cjs/ScopedToken.d.ts.map +1 -0
- package/dist/cjs/ScopedToken.js +135 -0
- package/dist/cjs/ScopedToken.js.map +7 -0
- package/dist/cjs/SqlControlRuntime.d.ts +161 -0
- package/dist/cjs/SqlControlRuntime.d.ts.map +1 -0
- package/dist/cjs/SqlControlRuntime.js +1522 -0
- package/dist/cjs/SqlControlRuntime.js.map +7 -0
- package/dist/cjs/SqlCredentialStore.d.ts +43 -0
- package/dist/cjs/SqlCredentialStore.d.ts.map +1 -0
- package/dist/cjs/SqlCredentialStore.js +113 -0
- package/dist/cjs/SqlCredentialStore.js.map +7 -0
- package/dist/cjs/Steering.d.ts +69 -0
- package/dist/cjs/Steering.d.ts.map +1 -0
- package/dist/cjs/Steering.js +49 -0
- package/dist/cjs/Steering.js.map +7 -0
- package/dist/cjs/SystemFlows.d.ts +223 -0
- package/dist/cjs/SystemFlows.d.ts.map +1 -0
- package/dist/cjs/SystemFlows.js +195 -0
- package/dist/cjs/SystemFlows.js.map +7 -0
- package/dist/cjs/WebCryptoCipher.d.ts +49 -0
- package/dist/cjs/WebCryptoCipher.d.ts.map +1 -0
- package/dist/cjs/WebCryptoCipher.js +129 -0
- package/dist/cjs/WebCryptoCipher.js.map +7 -0
- package/dist/cjs/WebhookChannel.d.ts +113 -0
- package/dist/cjs/WebhookChannel.d.ts.map +1 -0
- package/dist/cjs/WebhookChannel.js +98 -0
- package/dist/cjs/WebhookChannel.js.map +7 -0
- package/dist/cjs/index.d.ts +160 -0
- package/dist/cjs/index.d.ts.map +1 -0
- package/dist/cjs/index.js +91 -0
- package/dist/cjs/index.js.map +7 -0
- package/dist/cjs/internal/MutationBoundary.d.ts +27 -0
- package/dist/cjs/internal/MutationBoundary.d.ts.map +1 -0
- package/dist/cjs/internal/MutationBoundary.js +50 -0
- package/dist/cjs/internal/MutationBoundary.js.map +7 -0
- package/dist/cjs/internal/activeFibers.d.ts +12 -0
- package/dist/cjs/internal/activeFibers.d.ts.map +1 -0
- package/dist/cjs/internal/activeFibers.js +30 -0
- package/dist/cjs/internal/activeFibers.js.map +7 -0
- package/dist/cjs/internal/issues.d.ts +28 -0
- package/dist/cjs/internal/issues.d.ts.map +1 -0
- package/dist/cjs/internal/issues.js +34 -0
- package/dist/cjs/internal/issues.js.map +7 -0
- package/dist/cjs/internal/planning.d.ts +347 -0
- package/dist/cjs/internal/planning.d.ts.map +1 -0
- package/dist/cjs/internal/planning.js +137 -0
- package/dist/cjs/internal/planning.js.map +7 -0
- package/dist/cjs/internal/sqlSchemaErrors.d.ts +18 -0
- package/dist/cjs/internal/sqlSchemaErrors.d.ts.map +1 -0
- package/dist/cjs/internal/sqlSchemaErrors.js +40 -0
- package/dist/cjs/internal/sqlSchemaErrors.js.map +7 -0
- package/dist/cjs/migrations/0001_control_tables.d.ts +18 -0
- package/dist/cjs/migrations/0001_control_tables.d.ts.map +1 -0
- package/dist/cjs/migrations/0001_control_tables.js +115 -0
- package/dist/cjs/migrations/0001_control_tables.js.map +7 -0
- package/dist/cjs/migrations/0002_run_keys.d.ts +15 -0
- package/dist/cjs/migrations/0002_run_keys.d.ts.map +1 -0
- package/dist/cjs/migrations/0002_run_keys.js +44 -0
- package/dist/cjs/migrations/0002_run_keys.js.map +7 -0
- package/dist/cjs/migrations/0003_signal_commands.d.ts +13 -0
- package/dist/cjs/migrations/0003_signal_commands.d.ts.map +1 -0
- package/dist/cjs/migrations/0003_signal_commands.js +50 -0
- package/dist/cjs/migrations/0003_signal_commands.js.map +7 -0
- package/dist/cjs/migrations/0004_approval_decisions.d.ts +13 -0
- package/dist/cjs/migrations/0004_approval_decisions.d.ts.map +1 -0
- package/dist/cjs/migrations/0004_approval_decisions.js +45 -0
- package/dist/cjs/migrations/0004_approval_decisions.js.map +7 -0
- package/dist/cjs/migrations/0005_signal_principals.d.ts +18 -0
- package/dist/cjs/migrations/0005_signal_principals.d.ts.map +1 -0
- package/dist/cjs/migrations/0005_signal_principals.js +45 -0
- package/dist/cjs/migrations/0005_signal_principals.js.map +7 -0
- package/dist/cjs/migrations/0006_run_principals.d.ts +18 -0
- package/dist/cjs/migrations/0006_run_principals.d.ts.map +1 -0
- package/dist/cjs/migrations/0006_run_principals.js +49 -0
- package/dist/cjs/migrations/0006_run_principals.js.map +7 -0
- package/dist/cjs/migrations/0007_resume_consent.d.ts +18 -0
- package/dist/cjs/migrations/0007_resume_consent.d.ts.map +1 -0
- package/dist/cjs/migrations/0007_resume_consent.js +46 -0
- package/dist/cjs/migrations/0007_resume_consent.js.map +7 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/test/TestControl.d.ts +19 -0
- package/dist/cjs/test/TestControl.d.ts.map +1 -0
- package/dist/cjs/test/TestControl.js +62 -0
- package/dist/cjs/test/TestControl.js.map +7 -0
- package/dist/esm/ApprovalAuthority.d.ts +73 -0
- package/dist/esm/ApprovalAuthority.d.ts.map +1 -0
- package/dist/esm/ApprovalAuthority.js +72 -0
- package/dist/esm/ApprovalAuthority.js.map +1 -0
- package/dist/esm/Cancellation.d.ts +107 -0
- package/dist/esm/Cancellation.d.ts.map +1 -0
- package/dist/esm/Cancellation.js +116 -0
- package/dist/esm/Cancellation.js.map +1 -0
- package/dist/esm/Channels.d.ts +170 -0
- package/dist/esm/Channels.d.ts.map +1 -0
- package/dist/esm/Channels.js +312 -0
- package/dist/esm/Channels.js.map +1 -0
- package/dist/esm/Control.d.ts +202 -0
- package/dist/esm/Control.d.ts.map +1 -0
- package/dist/esm/Control.js +42 -0
- package/dist/esm/Control.js.map +1 -0
- package/dist/esm/ControlClient.d.ts +52 -0
- package/dist/esm/ControlClient.d.ts.map +1 -0
- package/dist/esm/ControlClient.js +217 -0
- package/dist/esm/ControlClient.js.map +1 -0
- package/dist/esm/ControlError.d.ts +318 -0
- package/dist/esm/ControlError.d.ts.map +1 -0
- package/dist/esm/ControlError.js +359 -0
- package/dist/esm/ControlError.js.map +1 -0
- package/dist/esm/ControlExecutor.d.ts +372 -0
- package/dist/esm/ControlExecutor.d.ts.map +1 -0
- package/dist/esm/ControlExecutor.js +212 -0
- package/dist/esm/ControlExecutor.js.map +1 -0
- package/dist/esm/ControlFacts.d.ts +454 -0
- package/dist/esm/ControlFacts.d.ts.map +1 -0
- package/dist/esm/ControlFacts.js +324 -0
- package/dist/esm/ControlFacts.js.map +1 -0
- package/dist/esm/ControlLive.d.ts +23 -0
- package/dist/esm/ControlLive.d.ts.map +1 -0
- package/dist/esm/ControlLive.js +1589 -0
- package/dist/esm/ControlLive.js.map +1 -0
- package/dist/esm/ControlRpcs.d.ts +1204 -0
- package/dist/esm/ControlRpcs.d.ts.map +1 -0
- package/dist/esm/ControlRpcs.js +299 -0
- package/dist/esm/ControlRpcs.js.map +1 -0
- package/dist/esm/ControlRuntime.d.ts +635 -0
- package/dist/esm/ControlRuntime.d.ts.map +1 -0
- package/dist/esm/ControlRuntime.js +808 -0
- package/dist/esm/ControlRuntime.js.map +1 -0
- package/dist/esm/ControlSchema.d.ts +2642 -0
- package/dist/esm/ControlSchema.d.ts.map +1 -0
- package/dist/esm/ControlSchema.js +1030 -0
- package/dist/esm/ControlSchema.js.map +1 -0
- package/dist/esm/ControlServer.d.ts +51 -0
- package/dist/esm/ControlServer.d.ts.map +1 -0
- package/dist/esm/ControlServer.js +145 -0
- package/dist/esm/ControlServer.js.map +1 -0
- package/dist/esm/Credential.d.ts +136 -0
- package/dist/esm/Credential.d.ts.map +1 -0
- package/dist/esm/Credential.js +190 -0
- package/dist/esm/Credential.js.map +1 -0
- package/dist/esm/CredentialCipher.d.ts +90 -0
- package/dist/esm/CredentialCipher.d.ts.map +1 -0
- package/dist/esm/CredentialCipher.js +56 -0
- package/dist/esm/CredentialCipher.js.map +1 -0
- package/dist/esm/CredentialStore.d.ts +97 -0
- package/dist/esm/CredentialStore.d.ts.map +1 -0
- package/dist/esm/CredentialStore.js +101 -0
- package/dist/esm/CredentialStore.js.map +1 -0
- package/dist/esm/DispatchReader.d.ts +112 -0
- package/dist/esm/DispatchReader.d.ts.map +1 -0
- package/dist/esm/DispatchReader.js +76 -0
- package/dist/esm/DispatchReader.js.map +1 -0
- package/dist/esm/Health.d.ts +333 -0
- package/dist/esm/Health.d.ts.map +1 -0
- package/dist/esm/Health.js +400 -0
- package/dist/esm/Health.js.map +1 -0
- package/dist/esm/JevSessionChecker.d.ts +57 -0
- package/dist/esm/JevSessionChecker.d.ts.map +1 -0
- package/dist/esm/JevSessionChecker.js +108 -0
- package/dist/esm/JevSessionChecker.js.map +1 -0
- package/dist/esm/Lineage.d.ts +131 -0
- package/dist/esm/Lineage.d.ts.map +1 -0
- package/dist/esm/Lineage.js +174 -0
- package/dist/esm/Lineage.js.map +1 -0
- package/dist/esm/Migrations.d.ts +34 -0
- package/dist/esm/Migrations.d.ts.map +1 -0
- package/dist/esm/Migrations.js +53 -0
- package/dist/esm/Migrations.js.map +1 -0
- package/dist/esm/Monitor.d.ts +282 -0
- package/dist/esm/Monitor.d.ts.map +1 -0
- package/dist/esm/Monitor.js +415 -0
- package/dist/esm/Monitor.js.map +1 -0
- package/dist/esm/ScopedToken.d.ts +193 -0
- package/dist/esm/ScopedToken.d.ts.map +1 -0
- package/dist/esm/ScopedToken.js +224 -0
- package/dist/esm/ScopedToken.js.map +1 -0
- package/dist/esm/SqlControlRuntime.d.ts +161 -0
- package/dist/esm/SqlControlRuntime.d.ts.map +1 -0
- package/dist/esm/SqlControlRuntime.js +1756 -0
- package/dist/esm/SqlControlRuntime.js.map +1 -0
- package/dist/esm/SqlCredentialStore.d.ts +43 -0
- package/dist/esm/SqlCredentialStore.d.ts.map +1 -0
- package/dist/esm/SqlCredentialStore.js +97 -0
- package/dist/esm/SqlCredentialStore.js.map +1 -0
- package/dist/esm/Steering.d.ts +69 -0
- package/dist/esm/Steering.d.ts.map +1 -0
- package/dist/esm/Steering.js +89 -0
- package/dist/esm/Steering.js.map +1 -0
- package/dist/esm/SystemFlows.d.ts +223 -0
- package/dist/esm/SystemFlows.d.ts.map +1 -0
- package/dist/esm/SystemFlows.js +198 -0
- package/dist/esm/SystemFlows.js.map +1 -0
- package/dist/esm/WebCryptoCipher.d.ts +49 -0
- package/dist/esm/WebCryptoCipher.d.ts.map +1 -0
- package/dist/esm/WebCryptoCipher.js +123 -0
- package/dist/esm/WebCryptoCipher.js.map +1 -0
- package/dist/esm/WebhookChannel.d.ts +113 -0
- package/dist/esm/WebhookChannel.d.ts.map +1 -0
- package/dist/esm/WebhookChannel.js +109 -0
- package/dist/esm/WebhookChannel.js.map +1 -0
- package/dist/esm/index.d.ts +160 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +160 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/internal/MutationBoundary.d.ts +27 -0
- package/dist/esm/internal/MutationBoundary.d.ts.map +1 -0
- package/dist/esm/internal/MutationBoundary.js +40 -0
- package/dist/esm/internal/MutationBoundary.js.map +1 -0
- package/dist/esm/internal/activeFibers.d.ts +12 -0
- package/dist/esm/internal/activeFibers.d.ts.map +1 -0
- package/dist/esm/internal/activeFibers.js +17 -0
- package/dist/esm/internal/activeFibers.js.map +1 -0
- package/dist/esm/internal/issues.d.ts +28 -0
- package/dist/esm/internal/issues.d.ts.map +1 -0
- package/dist/esm/internal/issues.js +35 -0
- package/dist/esm/internal/issues.js.map +1 -0
- package/dist/esm/internal/planning.d.ts +347 -0
- package/dist/esm/internal/planning.d.ts.map +1 -0
- package/dist/esm/internal/planning.js +199 -0
- package/dist/esm/internal/planning.js.map +1 -0
- package/dist/esm/internal/sqlSchemaErrors.d.ts +18 -0
- package/dist/esm/internal/sqlSchemaErrors.d.ts.map +1 -0
- package/dist/esm/internal/sqlSchemaErrors.js +38 -0
- package/dist/esm/internal/sqlSchemaErrors.js.map +1 -0
- package/dist/esm/migrations/0001_control_tables.d.ts +18 -0
- package/dist/esm/migrations/0001_control_tables.d.ts.map +1 -0
- package/dist/esm/migrations/0001_control_tables.js +96 -0
- package/dist/esm/migrations/0001_control_tables.js.map +1 -0
- package/dist/esm/migrations/0002_run_keys.d.ts +15 -0
- package/dist/esm/migrations/0002_run_keys.d.ts.map +1 -0
- package/dist/esm/migrations/0002_run_keys.js +22 -0
- package/dist/esm/migrations/0002_run_keys.js.map +1 -0
- package/dist/esm/migrations/0003_signal_commands.d.ts +13 -0
- package/dist/esm/migrations/0003_signal_commands.d.ts.map +1 -0
- package/dist/esm/migrations/0003_signal_commands.js +26 -0
- package/dist/esm/migrations/0003_signal_commands.js.map +1 -0
- package/dist/esm/migrations/0004_approval_decisions.d.ts +13 -0
- package/dist/esm/migrations/0004_approval_decisions.d.ts.map +1 -0
- package/dist/esm/migrations/0004_approval_decisions.js +25 -0
- package/dist/esm/migrations/0004_approval_decisions.js.map +1 -0
- package/dist/esm/migrations/0005_signal_principals.d.ts +18 -0
- package/dist/esm/migrations/0005_signal_principals.d.ts.map +1 -0
- package/dist/esm/migrations/0005_signal_principals.js +27 -0
- package/dist/esm/migrations/0005_signal_principals.js.map +1 -0
- package/dist/esm/migrations/0006_run_principals.d.ts +18 -0
- package/dist/esm/migrations/0006_run_principals.d.ts.map +1 -0
- package/dist/esm/migrations/0006_run_principals.js +30 -0
- package/dist/esm/migrations/0006_run_principals.js.map +1 -0
- package/dist/esm/migrations/0007_resume_consent.d.ts +18 -0
- package/dist/esm/migrations/0007_resume_consent.d.ts.map +1 -0
- package/dist/esm/migrations/0007_resume_consent.js +27 -0
- package/dist/esm/migrations/0007_resume_consent.js.map +1 -0
- package/dist/esm/test/TestControl.d.ts +19 -0
- package/dist/esm/test/TestControl.d.ts.map +1 -0
- package/dist/esm/test/TestControl.js +30 -0
- package/dist/esm/test/TestControl.js.map +1 -0
- package/docs/README.md +189 -0
- package/docs/api.md +982 -0
- package/docs/concepts/authority.md +109 -0
- package/docs/concepts/cancellation.md +129 -0
- package/docs/concepts/lineage.md +132 -0
- package/docs/concepts/ownership.md +139 -0
- package/docs/concepts/projections.md +203 -0
- package/docs/concepts/receipts.md +128 -0
- package/docs/guides/approvals.md +284 -0
- package/docs/guides/cancel-and-resume.md +162 -0
- package/docs/guides/durable-storage.md +147 -0
- package/docs/guides/implement-an-executor.md +173 -0
- package/docs/guides/ingest-a-webhook.md +177 -0
- package/docs/guides/list-runs.md +160 -0
- package/docs/guides/monitor-runs.md +176 -0
- package/docs/guides/observe-health.md +147 -0
- package/docs/guides/postgres-tests.md +7 -0
- package/docs/guides/serve-over-rpc.md +220 -0
- package/docs/guides/signal-a-run.md +53 -0
- package/docs/guides/steer-a-run.md +138 -0
- package/docs/guides/store-credentials.md +164 -0
- package/docs/guides/testing.md +139 -0
- package/docs/guides/watch-a-run.md +154 -0
- package/docs/installation.md +106 -0
- package/docs/quickstart.md +163 -0
- package/docs/troubleshooting.md +208 -0
- package/package.json +405 -3
- package/src/ApprovalAuthority.ts +114 -0
- package/src/Cancellation.ts +172 -0
- package/src/Channels.ts +493 -0
- package/src/Control.ts +337 -0
- package/src/ControlClient.ts +319 -0
- package/src/ControlError.ts +378 -0
- package/src/ControlExecutor.ts +490 -0
- package/src/ControlFacts.ts +383 -0
- package/src/ControlLive.ts +2127 -0
- package/src/ControlRpcs.ts +443 -0
- package/src/ControlRuntime.ts +1601 -0
- package/src/ControlSchema.ts +1380 -0
- package/src/ControlServer.ts +182 -0
- package/src/Credential.ts +310 -0
- package/src/CredentialCipher.ts +110 -0
- package/src/CredentialStore.ts +152 -0
- package/src/DispatchReader.ts +122 -0
- package/src/Health.ts +591 -0
- package/src/JevSessionChecker.ts +127 -0
- package/src/Lineage.ts +203 -0
- package/src/Migrations.ts +56 -0
- package/src/Monitor.ts +600 -0
- package/src/ScopedToken.ts +306 -0
- package/src/SqlControlRuntime.ts +2478 -0
- package/src/SqlCredentialStore.ts +148 -0
- package/src/Steering.ts +96 -0
- package/src/SystemFlows.ts +225 -0
- package/src/WebCryptoCipher.ts +169 -0
- package/src/WebhookChannel.ts +166 -0
- package/src/index.ts +188 -0
- package/src/internal/MutationBoundary.ts +46 -0
- package/src/internal/activeFibers.ts +22 -0
- package/src/internal/issues.ts +40 -0
- package/src/internal/planning.ts +262 -0
- package/src/internal/sqlSchemaErrors.ts +37 -0
- package/src/migrations/0001_control_tables.ts +99 -0
- package/src/migrations/0002_run_keys.ts +23 -0
- package/src/migrations/0003_signal_commands.ts +27 -0
- package/src/migrations/0004_approval_decisions.ts +25 -0
- package/src/migrations/0005_signal_principals.ts +27 -0
- package/src/migrations/0006_run_principals.ts +31 -0
- package/src/migrations/0007_resume_consent.ts +28 -0
- package/src/test/TestControl.ts +47 -0
|
@@ -0,0 +1,415 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Run health over the control plane: what a run's state means, and what to do
|
|
3
|
+
* about it.
|
|
4
|
+
*
|
|
5
|
+
* A control plane answers "what is this run doing". A monitor answers the
|
|
6
|
+
* question after it — "is that all right, and if not, what now" — and the two
|
|
7
|
+
* are deliberately separate modules. {@link classify} is a pure function of
|
|
8
|
+
* one observation, so the vocabulary an operator reads on a dashboard is the
|
|
9
|
+
* same one a heal loop branches on and the same one a test can enumerate.
|
|
10
|
+
* {@link run} is the loop that beats over `Control` and journals what it saw.
|
|
11
|
+
*
|
|
12
|
+
* The classification is decided from durable evidence only: the run summary
|
|
13
|
+
* the control plane projects, and the journal entries `watch` replays. Nothing
|
|
14
|
+
* here reads an in-process fiber, which is what makes a monitor able to watch
|
|
15
|
+
* a run in another process at all.
|
|
16
|
+
*
|
|
17
|
+
* @since 0.1.0
|
|
18
|
+
*/
|
|
19
|
+
import { Journal, JournalEvent } from "@smthrs/journal";
|
|
20
|
+
import { Duration, Effect, Stream } from "effect";
|
|
21
|
+
import { Control } from "./Control.js";
|
|
22
|
+
import { PersistenceError } from "./ControlError.js";
|
|
23
|
+
import * as SubjectHealth from "./Health.js";
|
|
24
|
+
/**
|
|
25
|
+
* The journal event type the engine records when an action attempt starts.
|
|
26
|
+
*
|
|
27
|
+
* Named as a string rather than imported, exactly as in `Lineage`: a monitor
|
|
28
|
+
* reads journals, not engines.
|
|
29
|
+
*
|
|
30
|
+
* @category constants
|
|
31
|
+
* @since 0.1.0
|
|
32
|
+
*/
|
|
33
|
+
export const attemptStartedEventType = "flows.engine.attempt-started";
|
|
34
|
+
/**
|
|
35
|
+
* The journal event type the engine records when an action attempt settles.
|
|
36
|
+
*
|
|
37
|
+
* @category constants
|
|
38
|
+
* @since 0.1.0
|
|
39
|
+
*/
|
|
40
|
+
export const attemptFinishedEventType = "flows.engine.attempt-finished";
|
|
41
|
+
/**
|
|
42
|
+
* The journal event type one monitor beat is recorded under.
|
|
43
|
+
*
|
|
44
|
+
* @category constants
|
|
45
|
+
* @since 0.1.0
|
|
46
|
+
*/
|
|
47
|
+
export const beatEventType = "control.monitor.beat";
|
|
48
|
+
/**
|
|
49
|
+
* The journal event type one applied remedy is recorded under.
|
|
50
|
+
*
|
|
51
|
+
* A beat and a heal are two records because they are two facts, and only one
|
|
52
|
+
* of them is true before the remedy runs. A single record carrying `healed`
|
|
53
|
+
* has to be written either before the remedy — leaving durable evidence of a
|
|
54
|
+
* heal that a crash one instruction later never performed — or after it, which
|
|
55
|
+
* loses the evidence of what a monitor decided when the remedy is what killed
|
|
56
|
+
* it. Splitting them keeps both.
|
|
57
|
+
*
|
|
58
|
+
* @category constants
|
|
59
|
+
* @since 0.1.0
|
|
60
|
+
*/
|
|
61
|
+
export const healedEventType = "control.monitor.healed";
|
|
62
|
+
/**
|
|
63
|
+
* What a run looks like to a monitor.
|
|
64
|
+
*
|
|
65
|
+
* `healthy` covers both "moving" and "finished": a completed run needs nothing
|
|
66
|
+
* done to it, and neither does one that is making progress. The other six name
|
|
67
|
+
* a specific thing that is wrong, because a heal that cannot tell them apart
|
|
68
|
+
* would resume a run that is waiting for a human.
|
|
69
|
+
*
|
|
70
|
+
* @category models
|
|
71
|
+
* @since 0.1.0
|
|
72
|
+
*/
|
|
73
|
+
export const Health = SubjectHealth.HealthState;
|
|
74
|
+
/** How many rounds a trampoline may take before a monitor calls it runaway. */
|
|
75
|
+
const defaultRoundBound = 32;
|
|
76
|
+
const foldAttempt = (state, event) => {
|
|
77
|
+
if (event.kind === attemptStartedEventType)
|
|
78
|
+
state.open += 1;
|
|
79
|
+
if (event.kind === attemptFinishedEventType) {
|
|
80
|
+
state.open -= 1;
|
|
81
|
+
const payload = event.payload;
|
|
82
|
+
state.failed = typeof payload === "object" && payload !== null && !Array.isArray(payload) &&
|
|
83
|
+
payload["state"] === "failed";
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
/**
|
|
87
|
+
* Decides what a run's state means.
|
|
88
|
+
*
|
|
89
|
+
* The order is the order an operator would read it in, and each rule earns its
|
|
90
|
+
* place by naming a different response:
|
|
91
|
+
*
|
|
92
|
+
* | Condition | Health | Because |
|
|
93
|
+
* | --- | --- | --- |
|
|
94
|
+
* | No summary | `unknown` | Nothing to say, and nothing to do. |
|
|
95
|
+
* | `failed` | `failing` | The run itself reported the failure. |
|
|
96
|
+
* | `completed`, `cancelled` | `healthy` | A finished run needs nothing. |
|
|
97
|
+
* | `waiting-approval`, or parked on `approval` | `awaiting-human` | A human owes it an answer; no machine can supply one. |
|
|
98
|
+
* | Parked with no waiting reason | `awaiting-human` | Only an operator's own park writes no waiting reason, so a person stopped it and a person restarts it. |
|
|
99
|
+
* | `roundOrdinal` at or past the bound | `runaway-loop` | The lineage is looping without converging. |
|
|
100
|
+
* | The last settled attempt failed | `failing` | The run is still alive but its work is not landing. |
|
|
101
|
+
* | No progress for `stallBeats`, an attempt open | `wedged-node` | One attempt started and never settled: the work is stuck, not the run. |
|
|
102
|
+
* | No progress for `stallBeats` | `stalled` | Nothing is happening and nothing is in flight. |
|
|
103
|
+
* | Anything else | `healthy` | Entries are still arriving. |
|
|
104
|
+
*
|
|
105
|
+
* `awaiting-human` deliberately outranks `failing`: a run parked for approval
|
|
106
|
+
* after a failed attempt is waiting for a person, and resuming or cancelling
|
|
107
|
+
* it would take the decision away from them.
|
|
108
|
+
*
|
|
109
|
+
* A park with no reason is the same case. The engine names every park it makes
|
|
110
|
+
* — `event`, `approval`, `timer`, `quota`, `released` — so a parked run whose
|
|
111
|
+
* `waitingReason` is absent was parked by an operator through
|
|
112
|
+
* `ControlRuntime.writeStatus`. Calling that a stall and resuming it would undo
|
|
113
|
+
* a deliberate act, which is the worst thing an unattended heal loop can do.
|
|
114
|
+
*
|
|
115
|
+
* @param observation what the beat saw
|
|
116
|
+
* @category projections
|
|
117
|
+
* @since 0.1.0
|
|
118
|
+
*/
|
|
119
|
+
export const classify = (observation) => {
|
|
120
|
+
const attempts = { open: 0, failed: false };
|
|
121
|
+
for (const event of observation.events)
|
|
122
|
+
foldAttempt(attempts, event);
|
|
123
|
+
return classifyState(observation, attempts);
|
|
124
|
+
};
|
|
125
|
+
const classifyState = (observation, attempts) => {
|
|
126
|
+
const summary = observation.summary;
|
|
127
|
+
if (summary === undefined)
|
|
128
|
+
return "unknown";
|
|
129
|
+
if (summary.status === "failed")
|
|
130
|
+
return "failing";
|
|
131
|
+
if (summary.status === "completed" || summary.status === "cancelled")
|
|
132
|
+
return "healthy";
|
|
133
|
+
if (summary.status === "waiting-approval")
|
|
134
|
+
return "awaiting-human";
|
|
135
|
+
if (summary.status === "parked" && (summary.waitingReason === "approval" || summary.waitingReason === undefined)) {
|
|
136
|
+
return "awaiting-human";
|
|
137
|
+
}
|
|
138
|
+
if (SubjectHealth.waitReason(summary.status, summary.waitingReason) !== undefined)
|
|
139
|
+
return "healthy";
|
|
140
|
+
const bound = observation.roundBound ?? defaultRoundBound;
|
|
141
|
+
if (summary.roundOrdinal !== undefined && summary.roundOrdinal >= bound)
|
|
142
|
+
return "runaway-loop";
|
|
143
|
+
if (attempts.failed)
|
|
144
|
+
return "failing";
|
|
145
|
+
if (!observation.semanticProgress && observation.beatsWithoutProgress >= observation.stallBeats) {
|
|
146
|
+
return attempts.open > 0 ? "wedged-node" : "stalled";
|
|
147
|
+
}
|
|
148
|
+
return "healthy";
|
|
149
|
+
};
|
|
150
|
+
/**
|
|
151
|
+
* The remedy a health warrants, before `autoHeal` decides whether to apply it.
|
|
152
|
+
*
|
|
153
|
+
* A stalled or wedged run is one nobody is driving, and a resume claims it. A
|
|
154
|
+
* failing or runaway run is one that will not get better by being driven
|
|
155
|
+
* harder. Everything else is left alone.
|
|
156
|
+
*
|
|
157
|
+
* @param health what the beat classified
|
|
158
|
+
* @category projections
|
|
159
|
+
* @since 0.1.0
|
|
160
|
+
*/
|
|
161
|
+
export const remedyFor = (health) => {
|
|
162
|
+
switch (health) {
|
|
163
|
+
case "stalled":
|
|
164
|
+
case "wedged-node":
|
|
165
|
+
return "resume";
|
|
166
|
+
case "failing":
|
|
167
|
+
case "runaway-loop":
|
|
168
|
+
return "cancel";
|
|
169
|
+
default:
|
|
170
|
+
return "none";
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
const summaryOf = (runId) => Effect.flatMap(Control, (control) => control.list({ _tag: "runs", filters: { runId } }).pipe(Effect.map((listed) => listed._tag === "runs" ? listed.items[0] : undefined)));
|
|
174
|
+
const terminal = (summary) => summary !== undefined &&
|
|
175
|
+
(summary.status === "completed" || summary.status === "failed" || summary.status === "cancelled");
|
|
176
|
+
/**
|
|
177
|
+
* Watches one run and, when told to, heals it.
|
|
178
|
+
*
|
|
179
|
+
* The loop stops early on a terminal run, because a finished run has nothing
|
|
180
|
+
* left to observe and beating at it would fill its journal with heartbeats.
|
|
181
|
+
* Otherwise it takes `maxChecks` beats and reports what it saw.
|
|
182
|
+
*
|
|
183
|
+
* Every beat is journaled as `control.monitor.beat` before any remedy is
|
|
184
|
+
* applied, carrying the `remedy` it is about to attempt, so a monitor that
|
|
185
|
+
* crashes mid-heal leaves the evidence of what it decided. The remedy itself
|
|
186
|
+
* is journaled separately as `control.monitor.healed`, and only once the heal
|
|
187
|
+
* returned a receipt: a heal that failed, or a process that died running one,
|
|
188
|
+
* must not leave a durable record saying the run was healed.
|
|
189
|
+
*
|
|
190
|
+
* `autoHeal` is empty by default. A monitor that healed by default would be a
|
|
191
|
+
* monitor that cancels a run the first time it looks at one, which is the
|
|
192
|
+
* wrong default for a thing an operator points at production.
|
|
193
|
+
*
|
|
194
|
+
* Nothing here leases the run. Two monitors on one run both beat and both
|
|
195
|
+
* remedy, so a remedy has to be idempotent on the control plane — which the
|
|
196
|
+
* two defaults are, through `Control.resume` and `Control.cancel`
|
|
197
|
+
* idempotency keys that name the monitor, the run, and the beat. A custom
|
|
198
|
+
* `heal` owes the same property.
|
|
199
|
+
*
|
|
200
|
+
* @param options how to beat and what to heal
|
|
201
|
+
* @category constructors
|
|
202
|
+
* @since 0.1.0
|
|
203
|
+
*/
|
|
204
|
+
export const run = (options) => Effect.gen(function* () {
|
|
205
|
+
const control = yield* Control;
|
|
206
|
+
const journal = yield* Journal.Journal;
|
|
207
|
+
const intervalMs = options.intervalMs ?? options.healthCheck?.policy.intervalMs ?? 1_000;
|
|
208
|
+
let consecutiveProbeFailures = 0;
|
|
209
|
+
let lastPublished;
|
|
210
|
+
const maxChecks = options.maxChecks ?? 10;
|
|
211
|
+
const stallBeats = options.stallBeats ?? 3;
|
|
212
|
+
const autoHeal = options.autoHeal ?? [];
|
|
213
|
+
const monitorId = options.monitorId ?? "default";
|
|
214
|
+
const heal = options.heal ?? ((input) => input.remedy === "resume"
|
|
215
|
+
? control.resume({
|
|
216
|
+
runId: input.runId,
|
|
217
|
+
idempotencyKey: `monitor:${monitorId}:resume:${input.runId}:${input.beat}`
|
|
218
|
+
})
|
|
219
|
+
: control.cancel({
|
|
220
|
+
runId: input.runId,
|
|
221
|
+
reason: `monitor:${input.health}`,
|
|
222
|
+
idempotencyKey: `monitor:${monitorId}:cancel:${input.runId}:${input.beat}`
|
|
223
|
+
}));
|
|
224
|
+
const emit = (eventType, payload, operation) => {
|
|
225
|
+
const unrecorded = (cause) => new PersistenceError({
|
|
226
|
+
operation: eventType,
|
|
227
|
+
message: `Failed to record ${operation} for ${options.runId}`,
|
|
228
|
+
cause
|
|
229
|
+
});
|
|
230
|
+
// The record is BUILT inside the failure channel, not before it.
|
|
231
|
+
// `JournalEvent.RunId`/`SourceId` refuse an identifier the store cannot
|
|
232
|
+
// tell apart from another one — a lone UTF-16 surrogate, an embedded NUL
|
|
233
|
+
// — by THROWING, and `monitorId` reaches here from an operator's flag. A
|
|
234
|
+
// thrown constructor is a defect, which the RPC boundary reports as an
|
|
235
|
+
// opaque `TransportError` and a caller cannot handle; a beat whose record
|
|
236
|
+
// cannot be built is the same event as a beat whose record cannot be
|
|
237
|
+
// appended, so both fail as `PersistenceError` naming the record.
|
|
238
|
+
return Effect.try({
|
|
239
|
+
try: () => new JournalEvent.Input({
|
|
240
|
+
runId: JournalEvent.RunId.make(options.runId),
|
|
241
|
+
sourceId: JournalEvent.SourceId.make(`/control/monitor/${monitorId}`),
|
|
242
|
+
eventType,
|
|
243
|
+
payload: { runId: options.runId, monitorId, ...payload }
|
|
244
|
+
}),
|
|
245
|
+
catch: unrecorded
|
|
246
|
+
}).pipe(Effect.flatMap((input) => Effect.mapError(journal.emitDurableUnfenced(input), unrecorded)), Effect.asVoid);
|
|
247
|
+
};
|
|
248
|
+
/** What the beat saw, and what it is about to do about it. */
|
|
249
|
+
const record = (beat, remedy) => emit(beatEventType, {
|
|
250
|
+
beat: beat.beat,
|
|
251
|
+
health: beat.health,
|
|
252
|
+
sequence: beat.sequence,
|
|
253
|
+
...(remedy === "none" ? {} : { remedy })
|
|
254
|
+
}, `monitor beat ${beat.beat}`);
|
|
255
|
+
/** What the remedy did, once it returned. */
|
|
256
|
+
const recordHealed = (beat, remedy, receipt) => emit(healedEventType, { beat: beat.beat, health: beat.health, healed: remedy, receipt: receipt._tag }, `monitor heal ${beat.beat}`);
|
|
257
|
+
const beats = [];
|
|
258
|
+
let beatsWithoutProgress = 0;
|
|
259
|
+
let lastSequence = -1;
|
|
260
|
+
let checkpoint = {};
|
|
261
|
+
const attempts = { open: 0, failed: false };
|
|
262
|
+
let sequence = -1;
|
|
263
|
+
for (let beat = 0; beat < maxChecks; beat += 1) {
|
|
264
|
+
if (beat > 0 && intervalMs > 0) {
|
|
265
|
+
yield* Effect.sleep(Duration.millis(options.healthCheck === undefined
|
|
266
|
+
? intervalMs
|
|
267
|
+
: SubjectHealth.nextDelay(options.healthCheck.policy, consecutiveProbeFailures)));
|
|
268
|
+
}
|
|
269
|
+
const summary = yield* summaryOf(options.runId);
|
|
270
|
+
const newEvents = [];
|
|
271
|
+
const sinceCursor = Math.max(0, sequence);
|
|
272
|
+
yield* control.watch({ runId: options.runId, follow: false, ...checkpoint }).pipe(Stream.runForEach((event) => Effect.sync(() => {
|
|
273
|
+
// Advance over bookkeeping too, but never count it as run progress.
|
|
274
|
+
checkpoint = event.cursor === undefined
|
|
275
|
+
? { afterSequence: event.sequence }
|
|
276
|
+
: { afterCursor: event.cursor };
|
|
277
|
+
if (isBookkeepingEvent(event.kind))
|
|
278
|
+
return;
|
|
279
|
+
newEvents.push(event);
|
|
280
|
+
if (newEvents.length > 256)
|
|
281
|
+
newEvents.shift();
|
|
282
|
+
sequence = event.sequence;
|
|
283
|
+
foldAttempt(attempts, event);
|
|
284
|
+
})));
|
|
285
|
+
beatsWithoutProgress = sequence === lastSequence ? beatsWithoutProgress + 1 : 0;
|
|
286
|
+
lastSequence = sequence;
|
|
287
|
+
const health = classifyState({
|
|
288
|
+
...(summary === undefined ? {} : { summary }),
|
|
289
|
+
beatsWithoutProgress,
|
|
290
|
+
stallBeats,
|
|
291
|
+
...(options.roundBound === undefined ? {} : { roundBound: options.roundBound })
|
|
292
|
+
}, attempts);
|
|
293
|
+
if (options.healthCheck !== undefined && summary !== undefined) {
|
|
294
|
+
const check = options.healthCheck;
|
|
295
|
+
const subjectId = `run:${options.runId}`;
|
|
296
|
+
const incarnation = SubjectHealth.runIncarnation(summary);
|
|
297
|
+
const probe = SubjectHealth.evaluate(check, {
|
|
298
|
+
subjectId,
|
|
299
|
+
state: summary.status,
|
|
300
|
+
summary,
|
|
301
|
+
events: newEvents,
|
|
302
|
+
sinceCursor
|
|
303
|
+
}, { monitorId, incarnation, evidenceSeq: Math.max(0, sequence) });
|
|
304
|
+
let observation = yield* (options.withProbePermit?.(probe) ?? probe);
|
|
305
|
+
const current = yield* summaryOf(options.runId);
|
|
306
|
+
if (current === undefined || SubjectHealth.runIncarnation(current) !== incarnation) {
|
|
307
|
+
observation = { ...observation, outcome: "discarded", report: undefined, reason: "owner-changed" };
|
|
308
|
+
}
|
|
309
|
+
consecutiveProbeFailures = observation.outcome === "ok" ? 0 : consecutiveProbeFailures + 1;
|
|
310
|
+
const semanticProgress = observation.outcome === "ok" && observation.report?.activity === "working";
|
|
311
|
+
const baseHealth = classifyState({
|
|
312
|
+
summary,
|
|
313
|
+
beatsWithoutProgress,
|
|
314
|
+
stallBeats,
|
|
315
|
+
roundBound: options.roundBound,
|
|
316
|
+
semanticProgress
|
|
317
|
+
}, attempts);
|
|
318
|
+
observation = { ...observation, baseHealth };
|
|
319
|
+
const status = SubjectHealth.rollup({
|
|
320
|
+
subjectId,
|
|
321
|
+
state: summary.status,
|
|
322
|
+
incarnation,
|
|
323
|
+
waitingReason: summary.waitingReason,
|
|
324
|
+
baseHealth,
|
|
325
|
+
latest: { observation, sequence: 0 },
|
|
326
|
+
now: observation.observedAt,
|
|
327
|
+
updatedAt: summary.updatedAt
|
|
328
|
+
});
|
|
329
|
+
const changed = lastPublished === undefined || lastPublished.incarnation !== observation.incarnation ||
|
|
330
|
+
lastPublished.outcome !== observation.outcome || lastPublished.baseHealth !== observation.baseHealth ||
|
|
331
|
+
lastPublished.evidenceSeq !== observation.evidenceSeq ||
|
|
332
|
+
JSON.stringify(lastPublished.report) !== JSON.stringify(observation.report) ||
|
|
333
|
+
lastPublished.reason !== observation.reason;
|
|
334
|
+
// Renew before expiry, but do not fill the journal with identical per-probe records.
|
|
335
|
+
if (changed || observation.observedAt >= lastPublished.observedAt + check.policy.ttlMs / 2) {
|
|
336
|
+
yield* emit(SubjectHealth.statusObservedEventType, {
|
|
337
|
+
...observation,
|
|
338
|
+
status: summary.status,
|
|
339
|
+
health: status.health,
|
|
340
|
+
activity: status.activity,
|
|
341
|
+
attention: status.attention,
|
|
342
|
+
freshness: status.freshness,
|
|
343
|
+
waitingReason: summary.waitingReason ?? ""
|
|
344
|
+
}, "health observation");
|
|
345
|
+
lastPublished = observation;
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
// Checker output is observational and never participates in remedy authorization.
|
|
349
|
+
const remedy = autoHeal.includes(health) ? remedyFor(health) : "none";
|
|
350
|
+
const observed = { beat, health, sequence };
|
|
351
|
+
if (options.recordBeats !== false)
|
|
352
|
+
yield* record(observed, remedy);
|
|
353
|
+
if (remedy === "none") {
|
|
354
|
+
beats.push(observed);
|
|
355
|
+
}
|
|
356
|
+
else {
|
|
357
|
+
const receipt = yield* heal({ runId: options.runId, health, remedy, beat }).pipe(
|
|
358
|
+
// A remedy that fails must not abort the run of beats: the monitor
|
|
359
|
+
// is an unattended loop, and one failing heal leaves every other
|
|
360
|
+
// beat — and every other run it would have observed — unwatched.
|
|
361
|
+
// The failure is logged with the beat it failed on, and the beat is
|
|
362
|
+
// recorded without `healed` or `receipt`, exactly as a remedy the
|
|
363
|
+
// monitor never got to apply reports.
|
|
364
|
+
Effect.catch((failure) => Effect.annotateLogs(Effect.logWarning("A monitor remedy failed and was skipped"), { runId: options.runId, beat, health, remedy, cause: String(failure) }).pipe(Effect.as(undefined))));
|
|
365
|
+
if (receipt === undefined) {
|
|
366
|
+
// The stall evidence stands: nothing provably moved the run, so the
|
|
367
|
+
// next beat compares against the same progress mark and the remedy
|
|
368
|
+
// is retried with the beat's own idempotency key.
|
|
369
|
+
beats.push(observed);
|
|
370
|
+
}
|
|
371
|
+
else {
|
|
372
|
+
// A remedy that returned is not a remedy that was applied. `Terminal`
|
|
373
|
+
// says the run had already settled, so nothing was healed; `Conflict`
|
|
374
|
+
// says the key belonged to another mutation, so this monitor's remedy
|
|
375
|
+
// never ran. Recording either as `healed` claimed something that did
|
|
376
|
+
// not happen, and resetting the stall count on either erased the
|
|
377
|
+
// evidence the next beat needs to notice the run is still stuck.
|
|
378
|
+
const applied = receipt._tag === "Accepted" || receipt._tag === "AlreadyApplied";
|
|
379
|
+
if (applied) {
|
|
380
|
+
// Journaled here and not a line earlier: `healed` is a claim about
|
|
381
|
+
// something that happened, and until the receipt came back it had not.
|
|
382
|
+
yield* recordHealed(observed, remedy, receipt);
|
|
383
|
+
// The heal moved the run, so the next beat compares against a run
|
|
384
|
+
// that has changed. Counting the beats before it as stall evidence
|
|
385
|
+
// again would heal a second time for the same stall.
|
|
386
|
+
beatsWithoutProgress = 0;
|
|
387
|
+
}
|
|
388
|
+
beats.push(applied ? { ...observed, healed: remedy, receipt } : { ...observed, receipt });
|
|
389
|
+
// A remedy that answered `Terminal` observed the run settle. The loop
|
|
390
|
+
// ends on the same evidence a terminal summary ends it on, rather than
|
|
391
|
+
// beating against a run nothing can move.
|
|
392
|
+
if (receipt._tag === "Terminal")
|
|
393
|
+
break;
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
if (options.retainBeats !== undefined && beats.length > options.retainBeats) {
|
|
397
|
+
beats.splice(0, beats.length - options.retainBeats);
|
|
398
|
+
}
|
|
399
|
+
if (terminal(summary)) {
|
|
400
|
+
break;
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
return {
|
|
404
|
+
runId: options.runId,
|
|
405
|
+
beats,
|
|
406
|
+
health: beats[beats.length - 1]?.health ?? "unknown"
|
|
407
|
+
};
|
|
408
|
+
});
|
|
409
|
+
/** Observer and notification bookkeeping is never execution progress.
|
|
410
|
+
* @category predicates
|
|
411
|
+
* @since 1.0.0
|
|
412
|
+
*/
|
|
413
|
+
export const isBookkeepingEvent = (kind) => kind.startsWith("control.monitor.") || kind.startsWith("control.status.") ||
|
|
414
|
+
kind.startsWith("flows.alerts.") || kind.startsWith("flows.notifications.") || kind.startsWith("flows.notification.");
|
|
415
|
+
//# sourceMappingURL=Monitor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Monitor.js","sourceRoot":"","sources":["../../src/Monitor.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAEH,OAAO,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAA;AACvD,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAA;AACjD,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AAEtC,OAAO,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAA;AAEpD,OAAO,KAAK,aAAa,MAAM,aAAa,CAAA;AAE5C;;;;;;;;GAQG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAAG,8BAA8B,CAAA;AAErE;;;;;GAKG;AACH,MAAM,CAAC,MAAM,wBAAwB,GAAG,+BAA+B,CAAA;AAEvE;;;;;GAKG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG,sBAAsB,CAAA;AAEnD;;;;;;;;;;;;GAYG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,wBAAwB,CAAA;AAEvD;;;;;;;;;;GAUG;AACH,MAAM,CAAC,MAAM,MAAM,GAAG,aAAa,CAAC,WAAW,CAAA;AAqC/C,+EAA+E;AAC/E,MAAM,iBAAiB,GAAG,EAAE,CAAA;AAO5B,MAAM,WAA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,CAAC;gBACD,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,GAAG,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC,CAAC,CAAC,EAAE,GAAG,QAAQ,EAAE,OAAO,EAAE,CAAC,CAAA;gBACzF,sEAAsE;gBACtE,uEAAuE;gBACvE,0CAA0C;gBAC1C,IAAI,OAAO,CAAC,IAAI,KAAK,UAAU;oBAAE,MAAK;YACxC,CAAC;QACH,CAAC;QACD,IAAI,OAAO,CAAC,WAAW,KAAK,SAAS,IAAI,KAAK,CAAC,MAAM,GAAG,OAAO,CAAC,WAAW,EAAE,CAAC;YAC5E,KAAK,CAAC,MAAM,CAAC,CAAC,EAAE,KAAK,CAAC,MAAM,GAAG,OAAO,CAAC,WAAW,CAAC,CAAA;QACrD,CAAC;QACD,IAAI,QAAQ,CAAC,OAAO,CAAC,EAAE,CAAC;YACtB,MAAK;QACP,CAAC;IACH,CAAC;IACD,OAAO;QACL,KAAK,EAAE,OAAO,CAAC,KAAK;QACpB,KAAK;QACL,MAAM,EAAE,KAAK,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,EAAE,MAAM,IAAI,SAAS;KACrD,CAAA;AACH,CAAC,CAAC,CAAA;AAEJ;;;GAGG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAAC,IAAY,EAAW,EAAE,CAC1D,IAAI,CAAC,UAAU,CAAC,kBAAkB,CAAC,IAAI,IAAI,CAAC,UAAU,CAAC,iBAAiB,CAAC;IACzE,IAAI,CAAC,UAAU,CAAC,eAAe,CAAC,IAAI,IAAI,CAAC,UAAU,CAAC,sBAAsB,CAAC,IAAI,IAAI,CAAC,UAAU,CAAC,qBAAqB,CAAC,CAAA","sourcesContent":["/**\n * Run health over the control plane: what a run's state means, and what to do\n * about it.\n *\n * A control plane answers \"what is this run doing\". A monitor answers the\n * question after it — \"is that all right, and if not, what now\" — and the two\n * are deliberately separate modules. {@link classify} is a pure function of\n * one observation, so the vocabulary an operator reads on a dashboard is the\n * same one a heal loop branches on and the same one a test can enumerate.\n * {@link run} is the loop that beats over `Control` and journals what it saw.\n *\n * The classification is decided from durable evidence only: the run summary\n * the control plane projects, and the journal entries `watch` replays. Nothing\n * here reads an in-process fiber, which is what makes a monitor able to watch\n * a run in another process at all.\n *\n * @since 0.1.0\n */\n\nimport { Journal, JournalEvent } from \"@smthrs/journal\"\nimport { Duration, Effect, Stream } from \"effect\"\nimport { Control } from \"./Control.ts\"\nimport type { ControlError } from \"./ControlError.ts\"\nimport { PersistenceError } from \"./ControlError.ts\"\nimport type { ControlEvent, Receipt, RunId, RunSummary, WatchFilter } from \"./ControlSchema.ts\"\nimport * as SubjectHealth from \"./Health.ts\"\n\n/**\n * The journal event type the engine records when an action attempt starts.\n *\n * Named as a string rather than imported, exactly as in `Lineage`: a monitor\n * reads journals, not engines.\n *\n * @category constants\n * @since 0.1.0\n */\nexport const attemptStartedEventType = \"flows.engine.attempt-started\"\n\n/**\n * The journal event type the engine records when an action attempt settles.\n *\n * @category constants\n * @since 0.1.0\n */\nexport const attemptFinishedEventType = \"flows.engine.attempt-finished\"\n\n/**\n * The journal event type one monitor beat is recorded under.\n *\n * @category constants\n * @since 0.1.0\n */\nexport const beatEventType = \"control.monitor.beat\"\n\n/**\n * The journal event type one applied remedy is recorded under.\n *\n * A beat and a heal are two records because they are two facts, and only one\n * of them is true before the remedy runs. A single record carrying `healed`\n * has to be written either before the remedy — leaving durable evidence of a\n * heal that a crash one instruction later never performed — or after it, which\n * loses the evidence of what a monitor decided when the remedy is what killed\n * it. Splitting them keeps both.\n *\n * @category constants\n * @since 0.1.0\n */\nexport const healedEventType = \"control.monitor.healed\"\n\n/**\n * What a run looks like to a monitor.\n *\n * `healthy` covers both \"moving\" and \"finished\": a completed run needs nothing\n * done to it, and neither does one that is making progress. The other six name\n * a specific thing that is wrong, because a heal that cannot tell them apart\n * would resume a run that is waiting for a human.\n *\n * @category models\n * @since 0.1.0\n */\nexport const Health = SubjectHealth.HealthState\n\n/**\n * What a run looks like to a monitor.\n *\n * @category models\n * @since 0.1.0\n */\nexport type Health = typeof Health.Type\n\n/**\n * Everything one classification is decided from.\n *\n * `events` is the run's journal as `watch` projects it, oldest first.\n * `beatsWithoutProgress` counts consecutive beats that added no entry, and\n * `stallBeats` is how many of those make a stall. Splitting the count from the\n * threshold is what lets the same pure function serve a monitor that beats\n * every second and one that beats every hour.\n *\n * @category models\n * @since 0.1.0\n */\nexport interface Observation {\n /** The run's projection, or nothing when the control plane has no such run. */\n readonly summary?: RunSummary | undefined\n readonly events: ReadonlyArray<ControlEvent>\n readonly beatsWithoutProgress: number\n readonly stallBeats: number\n /**\n * The trampoline round at which a lineage stops being a loop and starts\n * being a runaway. A run past it is looping without converging.\n */\n readonly roundBound?: number | undefined\n /** Fresh, explicitly reported semantic work; never inferred from bytes or monitor events. */\n readonly semanticProgress?: boolean | undefined\n}\n\n/** How many rounds a trampoline may take before a monitor calls it runaway. */\nconst defaultRoundBound = 32\n\ninterface AttemptState {\n open: number\n failed: boolean\n}\n\nconst foldAttempt = (state: AttemptState, event: ControlEvent): void => {\n if (event.kind === attemptStartedEventType) state.open += 1\n if (event.kind === attemptFinishedEventType) {\n state.open -= 1\n const payload = event.payload\n state.failed = typeof payload === \"object\" && payload !== null && !Array.isArray(payload) &&\n (payload as { readonly state?: unknown })[\"state\"] === \"failed\"\n }\n}\n\n/**\n * Decides what a run's state means.\n *\n * The order is the order an operator would read it in, and each rule earns its\n * place by naming a different response:\n *\n * | Condition | Health | Because |\n * | --- | --- | --- |\n * | No summary | `unknown` | Nothing to say, and nothing to do. |\n * | `failed` | `failing` | The run itself reported the failure. |\n * | `completed`, `cancelled` | `healthy` | A finished run needs nothing. |\n * | `waiting-approval`, or parked on `approval` | `awaiting-human` | A human owes it an answer; no machine can supply one. |\n * | Parked with no waiting reason | `awaiting-human` | Only an operator's own park writes no waiting reason, so a person stopped it and a person restarts it. |\n * | `roundOrdinal` at or past the bound | `runaway-loop` | The lineage is looping without converging. |\n * | The last settled attempt failed | `failing` | The run is still alive but its work is not landing. |\n * | No progress for `stallBeats`, an attempt open | `wedged-node` | One attempt started and never settled: the work is stuck, not the run. |\n * | No progress for `stallBeats` | `stalled` | Nothing is happening and nothing is in flight. |\n * | Anything else | `healthy` | Entries are still arriving. |\n *\n * `awaiting-human` deliberately outranks `failing`: a run parked for approval\n * after a failed attempt is waiting for a person, and resuming or cancelling\n * it would take the decision away from them.\n *\n * A park with no reason is the same case. The engine names every park it makes\n * — `event`, `approval`, `timer`, `quota`, `released` — so a parked run whose\n * `waitingReason` is absent was parked by an operator through\n * `ControlRuntime.writeStatus`. Calling that a stall and resuming it would undo\n * a deliberate act, which is the worst thing an unattended heal loop can do.\n *\n * @param observation what the beat saw\n * @category projections\n * @since 0.1.0\n */\nexport const classify = (observation: Observation): Health => {\n const attempts: AttemptState = { open: 0, failed: false }\n for (const event of observation.events) foldAttempt(attempts, event)\n return classifyState(observation, attempts)\n}\n\nconst classifyState = (observation: Omit<Observation, \"events\">, attempts: AttemptState): Health => {\n const summary = observation.summary\n if (summary === undefined) return \"unknown\"\n if (summary.status === \"failed\") return \"failing\"\n if (summary.status === \"completed\" || summary.status === \"cancelled\") return \"healthy\"\n if (summary.status === \"waiting-approval\") return \"awaiting-human\"\n if (summary.status === \"parked\" && (summary.waitingReason === \"approval\" || summary.waitingReason === undefined)) {\n return \"awaiting-human\"\n }\n if (SubjectHealth.waitReason(summary.status, summary.waitingReason) !== undefined) return \"healthy\"\n const bound = observation.roundBound ?? defaultRoundBound\n if (summary.roundOrdinal !== undefined && summary.roundOrdinal >= bound) return \"runaway-loop\"\n if (attempts.failed) return \"failing\"\n if (!observation.semanticProgress && observation.beatsWithoutProgress >= observation.stallBeats) {\n return attempts.open > 0 ? \"wedged-node\" : \"stalled\"\n }\n return \"healthy\"\n}\n\n/**\n * What a monitor does about one unhealthy run.\n *\n * @category models\n * @since 0.1.0\n */\nexport type Remedy = \"resume\" | \"cancel\" | \"none\"\n\n/**\n * The remedy a health warrants, before `autoHeal` decides whether to apply it.\n *\n * A stalled or wedged run is one nobody is driving, and a resume claims it. A\n * failing or runaway run is one that will not get better by being driven\n * harder. Everything else is left alone.\n *\n * @param health what the beat classified\n * @category projections\n * @since 0.1.0\n */\nexport const remedyFor = (health: Health): Remedy => {\n switch (health) {\n case \"stalled\":\n case \"wedged-node\":\n return \"resume\"\n case \"failing\":\n case \"runaway-loop\":\n return \"cancel\"\n default:\n return \"none\"\n }\n}\n\n/**\n * One beat of a monitor.\n *\n * @category models\n * @since 0.1.0\n */\nexport interface Beat {\n /** Which beat this was, counting from zero. */\n readonly beat: number\n readonly health: Health\n /** The newest journal sequence the run had, ignoring the monitor's own records. */\n readonly sequence: number\n /**\n * The remedy that ran and returned, absent when none did.\n *\n * Present only after the heal succeeded, which is also when\n * `control.monitor.healed` is journaled. A beat that decided on a remedy the\n * monitor never got to apply reports nothing here.\n */\n readonly healed?: Remedy | undefined\n /** The receipt the remedy returned, absent when none was applied. */\n readonly receipt?: Receipt | undefined\n}\n\n/**\n * What a monitor found.\n *\n * @category models\n * @since 0.1.0\n */\nexport interface Report {\n readonly runId: RunId\n readonly beats: ReadonlyArray<Beat>\n /** The last beat's health, or `unknown` when no beat ran. */\n readonly health: Health\n}\n\n/**\n * How a monitor beats.\n *\n * @category models\n * @since 0.1.0\n */\nexport interface Options {\n readonly runId: RunId\n /** Optional configured observational checker; checker results never authorize a remedy. */\n readonly healthCheck?: SubjectHealth.ResolvedCheck | undefined\n /** Shared host admission limit around probes. */\n readonly withProbePermit?: (<A>(effect: Effect.Effect<A>) => Effect.Effect<A>) | undefined\n /** Bounded retained beat history for long-lived host monitoring. */\n readonly retainBeats?: number | undefined\n /** Hosts with structured status observations omit redundant legacy heartbeat records. */\n readonly recordBeats?: boolean | undefined\n /**\n * Who is watching. Defaults to `default`.\n *\n * It reaches the journal twice: as the source of every record this monitor\n * writes, and as `monitorId` in each payload. Two monitors pointed at one\n * run both beat and both remedy — nothing on the control plane leases a run\n * to one watcher — so the identity is what makes their evidence tellable\n * apart, and what keeps their remedies on separate idempotency keys.\n */\n readonly monitorId?: string | undefined\n /** How long to wait between beats. Defaults to one second. */\n readonly intervalMs?: number | undefined\n /** How many beats to take before reporting. Defaults to ten. */\n readonly maxChecks?: number | undefined\n /** How many beats without progress make a stall. Defaults to three. */\n readonly stallBeats?: number | undefined\n /** The round at which a trampoline is a runaway. Defaults to 32. */\n readonly roundBound?: number | undefined\n /** Which healths the monitor is allowed to act on. Defaults to none. */\n readonly autoHeal?: ReadonlyArray<Health> | undefined\n /**\n * What to do about an unhealthy run. Defaults to `Control.resume` and\n * `Control.cancel` per {@link remedyFor}.\n *\n * A heal that fails does not abort the monitor: the failure is logged, the\n * beat is recorded without `healed` or `receipt`, and the loop moves to the\n * next beat, where the stall evidence still stands and the remedy is\n * retried under that beat's own idempotency key.\n */\n readonly heal?: (\n input: { readonly runId: RunId; readonly health: Health; readonly remedy: Remedy; readonly beat: number }\n ) => Effect.Effect<Receipt, ControlError>\n}\n\nconst summaryOf = (runId: RunId): Effect.Effect<RunSummary | undefined, ControlError, Control> =>\n Effect.flatMap(Control, (control) =>\n control.list({ _tag: \"runs\", filters: { runId } }).pipe(\n Effect.map((listed) => listed._tag === \"runs\" ? listed.items[0] : undefined)\n ))\n\nconst terminal = (summary: RunSummary | undefined): boolean =>\n summary !== undefined &&\n (summary.status === \"completed\" || summary.status === \"failed\" || summary.status === \"cancelled\")\n\n/**\n * Watches one run and, when told to, heals it.\n *\n * The loop stops early on a terminal run, because a finished run has nothing\n * left to observe and beating at it would fill its journal with heartbeats.\n * Otherwise it takes `maxChecks` beats and reports what it saw.\n *\n * Every beat is journaled as `control.monitor.beat` before any remedy is\n * applied, carrying the `remedy` it is about to attempt, so a monitor that\n * crashes mid-heal leaves the evidence of what it decided. The remedy itself\n * is journaled separately as `control.monitor.healed`, and only once the heal\n * returned a receipt: a heal that failed, or a process that died running one,\n * must not leave a durable record saying the run was healed.\n *\n * `autoHeal` is empty by default. A monitor that healed by default would be a\n * monitor that cancels a run the first time it looks at one, which is the\n * wrong default for a thing an operator points at production.\n *\n * Nothing here leases the run. Two monitors on one run both beat and both\n * remedy, so a remedy has to be idempotent on the control plane — which the\n * two defaults are, through `Control.resume` and `Control.cancel`\n * idempotency keys that name the monitor, the run, and the beat. A custom\n * `heal` owes the same property.\n *\n * @param options how to beat and what to heal\n * @category constructors\n * @since 0.1.0\n */\nexport const run = (\n options: Options\n): Effect.Effect<Report, ControlError, Control | Journal.Journal> =>\n Effect.gen(function*() {\n const control = yield* Control\n const journal = yield* Journal.Journal\n const intervalMs = options.intervalMs ?? options.healthCheck?.policy.intervalMs ?? 1_000\n let consecutiveProbeFailures = 0\n let lastPublished: SubjectHealth.HealthObservation | undefined\n const maxChecks = options.maxChecks ?? 10\n const stallBeats = options.stallBeats ?? 3\n const autoHeal = options.autoHeal ?? []\n const monitorId = options.monitorId ?? \"default\"\n const heal = options.heal ?? ((input) =>\n input.remedy === \"resume\"\n ? control.resume({\n runId: input.runId,\n idempotencyKey: `monitor:${monitorId}:resume:${input.runId}:${input.beat}`\n })\n : control.cancel({\n runId: input.runId,\n reason: `monitor:${input.health}`,\n idempotencyKey: `monitor:${monitorId}:cancel:${input.runId}:${input.beat}`\n }))\n\n const emit = (\n eventType: string,\n payload: Record<string, unknown>,\n operation: string\n ): Effect.Effect<void, ControlError> => {\n const unrecorded = (cause: unknown) =>\n new PersistenceError({\n operation: eventType,\n message: `Failed to record ${operation} for ${options.runId}`,\n cause\n })\n // The record is BUILT inside the failure channel, not before it.\n // `JournalEvent.RunId`/`SourceId` refuse an identifier the store cannot\n // tell apart from another one — a lone UTF-16 surrogate, an embedded NUL\n // — by THROWING, and `monitorId` reaches here from an operator's flag. A\n // thrown constructor is a defect, which the RPC boundary reports as an\n // opaque `TransportError` and a caller cannot handle; a beat whose record\n // cannot be built is the same event as a beat whose record cannot be\n // appended, so both fail as `PersistenceError` naming the record.\n return Effect.try({\n try: () =>\n new JournalEvent.Input({\n runId: JournalEvent.RunId.make(options.runId),\n sourceId: JournalEvent.SourceId.make(`/control/monitor/${monitorId}`),\n eventType,\n payload: { runId: options.runId, monitorId, ...payload }\n }),\n catch: unrecorded\n }).pipe(\n Effect.flatMap((input) => Effect.mapError(journal.emitDurableUnfenced(input), unrecorded)),\n Effect.asVoid\n )\n }\n\n /** What the beat saw, and what it is about to do about it. */\n const record = (beat: Beat, remedy: Remedy): Effect.Effect<void, ControlError> =>\n emit(\n beatEventType,\n {\n beat: beat.beat,\n health: beat.health,\n sequence: beat.sequence,\n ...(remedy === \"none\" ? {} : { remedy })\n },\n `monitor beat ${beat.beat}`\n )\n\n /** What the remedy did, once it returned. */\n const recordHealed = (\n beat: Beat,\n remedy: Remedy,\n receipt: Receipt\n ): Effect.Effect<void, ControlError> =>\n emit(\n healedEventType,\n { beat: beat.beat, health: beat.health, healed: remedy, receipt: receipt._tag },\n `monitor heal ${beat.beat}`\n )\n\n const beats: Array<Beat> = []\n let beatsWithoutProgress = 0\n let lastSequence = -1\n let checkpoint: Pick<WatchFilter, \"afterCursor\" | \"afterSequence\"> = {}\n const attempts: AttemptState = { open: 0, failed: false }\n let sequence = -1\n for (let beat = 0; beat < maxChecks; beat += 1) {\n if (beat > 0 && intervalMs > 0) {\n yield* Effect.sleep(Duration.millis(\n options.healthCheck === undefined\n ? intervalMs\n : SubjectHealth.nextDelay(options.healthCheck.policy, consecutiveProbeFailures)\n ))\n }\n const summary = yield* summaryOf(options.runId)\n const newEvents: Array<ControlEvent> = []\n const sinceCursor = Math.max(0, sequence)\n yield* control.watch({ runId: options.runId, follow: false, ...checkpoint }).pipe(\n Stream.runForEach((event) =>\n Effect.sync(() => {\n // Advance over bookkeeping too, but never count it as run progress.\n checkpoint = event.cursor === undefined\n ? { afterSequence: event.sequence }\n : { afterCursor: event.cursor }\n if (isBookkeepingEvent(event.kind)) return\n newEvents.push(event)\n if (newEvents.length > 256) newEvents.shift()\n sequence = event.sequence\n foldAttempt(attempts, event)\n })\n )\n )\n beatsWithoutProgress = sequence === lastSequence ? beatsWithoutProgress + 1 : 0\n lastSequence = sequence\n const health = classifyState({\n ...(summary === undefined ? {} : { summary }),\n beatsWithoutProgress,\n stallBeats,\n ...(options.roundBound === undefined ? {} : { roundBound: options.roundBound })\n }, attempts)\n if (options.healthCheck !== undefined && summary !== undefined) {\n const check = options.healthCheck\n const subjectId = `run:${options.runId}`\n const incarnation = SubjectHealth.runIncarnation(summary)\n const probe = SubjectHealth.evaluate(check, {\n subjectId,\n state: summary.status,\n summary,\n events: newEvents,\n sinceCursor\n }, { monitorId, incarnation, evidenceSeq: Math.max(0, sequence) })\n let observation = yield* (options.withProbePermit?.(probe) ?? probe)\n const current = yield* summaryOf(options.runId)\n if (current === undefined || SubjectHealth.runIncarnation(current) !== incarnation) {\n observation = { ...observation, outcome: \"discarded\", report: undefined, reason: \"owner-changed\" }\n }\n consecutiveProbeFailures = observation.outcome === \"ok\" ? 0 : consecutiveProbeFailures + 1\n const semanticProgress = observation.outcome === \"ok\" && observation.report?.activity === \"working\"\n const baseHealth = classifyState({\n summary,\n beatsWithoutProgress,\n stallBeats,\n roundBound: options.roundBound,\n semanticProgress\n }, attempts)\n observation = { ...observation, baseHealth }\n const status = SubjectHealth.rollup({\n subjectId,\n state: summary.status,\n incarnation,\n waitingReason: summary.waitingReason,\n baseHealth,\n latest: { observation, sequence: 0 },\n now: observation.observedAt,\n updatedAt: summary.updatedAt\n })\n const changed = lastPublished === undefined || lastPublished.incarnation !== observation.incarnation ||\n lastPublished.outcome !== observation.outcome || lastPublished.baseHealth !== observation.baseHealth ||\n lastPublished.evidenceSeq !== observation.evidenceSeq ||\n JSON.stringify(lastPublished.report) !== JSON.stringify(observation.report) ||\n lastPublished.reason !== observation.reason\n // Renew before expiry, but do not fill the journal with identical per-probe records.\n if (changed || observation.observedAt >= lastPublished!.observedAt + check.policy.ttlMs / 2) {\n yield* emit(SubjectHealth.statusObservedEventType, {\n ...observation,\n status: summary.status,\n health: status.health,\n activity: status.activity,\n attention: status.attention,\n freshness: status.freshness,\n waitingReason: summary.waitingReason ?? \"\"\n }, \"health observation\")\n lastPublished = observation\n }\n }\n // Checker output is observational and never participates in remedy authorization.\n const remedy = autoHeal.includes(health) ? remedyFor(health) : \"none\"\n const observed: Beat = { beat, health, sequence }\n if (options.recordBeats !== false) yield* record(observed, remedy)\n if (remedy === \"none\") {\n beats.push(observed)\n } else {\n const receipt = yield* heal({ runId: options.runId, health, remedy, beat }).pipe(\n // A remedy that fails must not abort the run of beats: the monitor\n // is an unattended loop, and one failing heal leaves every other\n // beat — and every other run it would have observed — unwatched.\n // The failure is logged with the beat it failed on, and the beat is\n // recorded without `healed` or `receipt`, exactly as a remedy the\n // monitor never got to apply reports.\n Effect.catch((failure) =>\n Effect.annotateLogs(\n Effect.logWarning(\"A monitor remedy failed and was skipped\"),\n { runId: options.runId, beat, health, remedy, cause: String(failure) }\n ).pipe(Effect.as(undefined))\n )\n )\n if (receipt === undefined) {\n // The stall evidence stands: nothing provably moved the run, so the\n // next beat compares against the same progress mark and the remedy\n // is retried with the beat's own idempotency key.\n beats.push(observed)\n } else {\n // A remedy that returned is not a remedy that was applied. `Terminal`\n // says the run had already settled, so nothing was healed; `Conflict`\n // says the key belonged to another mutation, so this monitor's remedy\n // never ran. Recording either as `healed` claimed something that did\n // not happen, and resetting the stall count on either erased the\n // evidence the next beat needs to notice the run is still stuck.\n const applied = receipt._tag === \"Accepted\" || receipt._tag === \"AlreadyApplied\"\n if (applied) {\n // Journaled here and not a line earlier: `healed` is a claim about\n // something that happened, and until the receipt came back it had not.\n yield* recordHealed(observed, remedy, receipt)\n // The heal moved the run, so the next beat compares against a run\n // that has changed. Counting the beats before it as stall evidence\n // again would heal a second time for the same stall.\n beatsWithoutProgress = 0\n }\n beats.push(applied ? { ...observed, healed: remedy, receipt } : { ...observed, receipt })\n // A remedy that answered `Terminal` observed the run settle. The loop\n // ends on the same evidence a terminal summary ends it on, rather than\n // beating against a run nothing can move.\n if (receipt._tag === \"Terminal\") break\n }\n }\n if (options.retainBeats !== undefined && beats.length > options.retainBeats) {\n beats.splice(0, beats.length - options.retainBeats)\n }\n if (terminal(summary)) {\n break\n }\n }\n return {\n runId: options.runId,\n beats,\n health: beats[beats.length - 1]?.health ?? \"unknown\"\n }\n })\n\n/** Observer and notification bookkeeping is never execution progress.\n * @category predicates\n * @since 1.0.0\n */\nexport const isBookkeepingEvent = (kind: string): boolean =>\n kind.startsWith(\"control.monitor.\") || kind.startsWith(\"control.status.\") ||\n kind.startsWith(\"flows.alerts.\") || kind.startsWith(\"flows.notifications.\") || kind.startsWith(\"flows.notification.\")\n"]}
|