@deksden-com/dd-flow-cli 0.9.0-beta.14 → 0.9.0-beta.141
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 +740 -0
- package/README.md +143 -1
- package/dist/build-info.json +8 -8
- package/dist/cli/argument-values.js +38 -0
- package/dist/cli/command-inputs.js +376 -0
- package/dist/cli/command-metadata.js +197 -0
- package/dist/cli/command-option-descriptions.js +191 -0
- package/dist/cli/completion-cli.js +70 -0
- package/dist/cli/completion.js +57 -0
- package/dist/cli/error-output.js +52 -0
- package/dist/cli/executor-onboarding.js +76 -0
- package/dist/cli/flows.js +29 -0
- package/dist/cli/harnesses.js +84 -0
- package/dist/cli/help.js +328 -18
- package/dist/cli/hook-ingress.js +78 -0
- package/dist/cli/input-preparation.js +165 -0
- package/dist/cli/memory-flow-admission.js +44 -0
- package/dist/cli/memory-init-invocation.js +78 -0
- package/dist/cli/memory-init.js +240 -0
- package/dist/cli/memory-storage-admission.js +53 -0
- package/dist/cli/models.js +39 -0
- package/dist/cli/native-hook-ingress.js +80 -0
- package/dist/cli/operator-profiles.js +61 -0
- package/dist/cli/output-options.js +35 -0
- package/dist/cli/response-file.js +71 -0
- package/dist/cli/run-cli.js +1689 -545
- package/dist/cli/runtime-maintenance-diagnostics.js +46 -0
- package/dist/cli/runtime-maintenance-ingress.js +52 -0
- package/dist/cli/runtime-process.js +153 -0
- package/dist/cli/setup.js +149 -0
- package/dist/cli.js +71 -2
- package/dist/domain/stage-catalog.js +1 -1
- package/dist/domain/stage-handler-identities.js +20 -0
- package/dist/harness-runtime/DD-ZCODE.md +112 -0
- package/dist/harness-runtime/OPENCODE-LICENSE.txt +24 -0
- package/dist/harness-runtime/bin/dd-agy.mjs +56 -0
- package/dist/harness-runtime/bin/dd-codex.mjs +27 -0
- package/dist/harness-runtime/bin/dd-droid.mjs +34 -0
- package/dist/harness-runtime/bin/dd-grok.mjs +40 -0
- package/dist/harness-runtime/bin/dd-opencode.mjs +22 -0
- package/dist/harness-runtime/bin/dd-zcode.mjs +87 -0
- package/dist/harness-runtime/lib/adapter-timeouts.d.mts +5 -0
- package/dist/harness-runtime/lib/adapter-timeouts.mjs +34 -0
- package/dist/harness-runtime/lib/agy-hooks.d.mts +1 -0
- package/dist/harness-runtime/lib/agy-hooks.mjs +9 -0
- package/dist/harness-runtime/lib/daemon-observation.d.mts +2 -0
- package/dist/harness-runtime/lib/daemon-observation.mjs +163 -0
- package/dist/harness-runtime/lib/daemon-operations.d.mts +1 -0
- package/dist/harness-runtime/lib/daemon-operations.mjs +371 -0
- package/dist/harness-runtime/lib/dd-agy-daemon.d.mts +6 -0
- package/dist/harness-runtime/lib/dd-agy-daemon.mjs +728 -0
- package/dist/harness-runtime/lib/dd-agy.mjs +70 -0
- package/dist/harness-runtime/lib/dd-codex-daemon.d.mts +1 -0
- package/dist/harness-runtime/lib/dd-codex-daemon.mjs +196 -0
- package/dist/harness-runtime/lib/dd-codex.d.mts +1 -0
- package/dist/harness-runtime/lib/dd-codex.mjs +817 -0
- package/dist/harness-runtime/lib/dd-droid-daemon.mjs +140 -0
- package/dist/harness-runtime/lib/dd-droid.mjs +584 -0
- package/dist/harness-runtime/lib/dd-grok-daemon.mjs +498 -0
- package/dist/harness-runtime/lib/dd-grok.mjs +177 -0
- package/dist/harness-runtime/lib/dd-opencode-daemon.mjs +557 -0
- package/dist/harness-runtime/lib/dd-opencode.mjs +363 -0
- package/dist/harness-runtime/lib/dd-zcode-daemon.mjs +751 -0
- package/dist/harness-runtime/lib/dd-zcode.mjs +1145 -0
- package/dist/harness-runtime/lib/delegation-instructions.d.mts +14 -0
- package/dist/harness-runtime/lib/delegation-instructions.mjs +157 -0
- package/dist/harness-runtime/lib/dispatch-fence.mjs +14 -0
- package/dist/harness-runtime/lib/driver-recovery.mjs +139 -0
- package/dist/harness-runtime/lib/droid-observation.mjs +66 -0
- package/dist/harness-runtime/lib/lease-renewal.d.mts +8 -0
- package/dist/harness-runtime/lib/lease-renewal.mjs +37 -0
- package/dist/harness-runtime/lib/lifecycle-command.d.mts +20 -0
- package/dist/harness-runtime/lib/lifecycle-command.mjs +273 -0
- package/dist/harness-runtime/lib/lifecycle-contract.d.mts +10 -0
- package/dist/harness-runtime/lib/lifecycle-contract.mjs +19 -0
- package/dist/harness-runtime/lib/managed-daemon.d.mts +16 -0
- package/dist/harness-runtime/lib/managed-daemon.mjs +683 -0
- package/dist/harness-runtime/lib/managed-flow-env.mjs +15 -0
- package/dist/harness-runtime/lib/model-observations.mjs +101 -0
- package/dist/harness-runtime/lib/native-children.d.mts +16 -0
- package/dist/harness-runtime/lib/native-children.mjs +113 -0
- package/dist/harness-runtime/lib/native-hook-command.d.mts +7 -0
- package/dist/harness-runtime/lib/native-hook-command.mjs +251 -0
- package/dist/harness-runtime/lib/native-request-proof.d.mts +2 -0
- package/dist/harness-runtime/lib/native-request-proof.mjs +65 -0
- package/dist/harness-runtime/lib/native-streams.mjs +45 -0
- package/dist/harness-runtime/lib/observation-clock.d.mts +18 -0
- package/dist/harness-runtime/lib/observation-clock.mjs +92 -0
- package/dist/harness-runtime/lib/opencode-plugin.mjs +85 -0
- package/dist/harness-runtime/lib/operation-context.mjs +5 -0
- package/dist/harness-runtime/lib/operation-errors.d.mts +16 -0
- package/dist/harness-runtime/lib/operation-errors.mjs +127 -0
- package/dist/harness-runtime/lib/operation-progress.d.mts +7 -0
- package/dist/harness-runtime/lib/operation-progress.mjs +114 -0
- package/dist/harness-runtime/lib/process-json.d.mts +5 -0
- package/dist/harness-runtime/lib/process-json.mjs +201 -0
- package/dist/harness-runtime/lib/process-snapshot.mjs +10 -0
- package/dist/harness-runtime/lib/provider-overload-policy.d.mts +13 -0
- package/dist/harness-runtime/lib/provider-overload-policy.mjs +138 -0
- package/dist/harness-runtime/lib/runner-events.mjs +200 -0
- package/dist/harness-runtime/lib/runner-lock.mjs +42 -0
- package/dist/harness-runtime/lib/session-settlement.mjs +30 -0
- package/dist/harness-runtime/lib/setup-progress.d.mts +9 -0
- package/dist/harness-runtime/lib/setup-progress.mjs +51 -0
- package/dist/harness-runtime/lib/tool-observations.d.mts +21 -0
- package/dist/harness-runtime/lib/tool-observations.mjs +228 -0
- package/dist/harness-runtime/lib/zcode-usage.mjs +55 -0
- package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-1y4a697q.js +81 -0
- package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-2ny4vxct.js +92 -0
- package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-6mptkzbc.js +0 -0
- package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-7x67cf1q.js +52 -0
- package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-9rqn6x4v.js +16 -0
- package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-9vdz0zwn.js +1661 -0
- package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-ecs5ftka.js +1466 -0
- package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-frbwqjmf.js +145 -0
- package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-gne7h7mq.js +50 -0
- package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-gx9wnkzr.js +105 -0
- package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-m2eywc8w.js +0 -0
- package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-nwcpakzx.js +12 -0
- package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-q4g3afnw.js +623 -0
- package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-qav6mfdz.js +0 -0
- package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-sxt88q69.js +12 -0
- package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-t1xa31vm.js +39 -0
- package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-ve4aws36.js +103 -0
- package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-vmytr4q8.js +4 -0
- package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-vt9jbch8.js +36 -0
- package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-vyas3vgt.js +0 -0
- package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-wjyeq25y.js +30 -0
- package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-xgnj2w4v.js +20 -0
- package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-y2etg5bs.js +9 -0
- package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-ydhz1zrh.js +238 -0
- package/dist/harness-runtime/vendor/opencode-client/chunks/service-probe-z6qdwcsf.js +62 -0
- package/dist/harness-runtime/vendor/opencode-client/contract.d.ts +1 -0
- package/dist/harness-runtime/vendor/opencode-client/contract.js +12 -0
- package/dist/harness-runtime/vendor/opencode-client/effect/api/api.d.ts +2806 -0
- package/dist/harness-runtime/vendor/opencode-client/effect/api/api.js +0 -0
- package/dist/harness-runtime/vendor/opencode-client/effect/api.d.ts +4 -0
- package/dist/harness-runtime/vendor/opencode-client/effect/api.js +0 -0
- package/dist/harness-runtime/vendor/opencode-client/effect/client.d.ts +2530 -0
- package/dist/harness-runtime/vendor/opencode-client/effect/client.js +15 -0
- package/dist/harness-runtime/vendor/opencode-client/effect/generated/client-error.d.ts +7 -0
- package/dist/harness-runtime/vendor/opencode-client/effect/generated/client-error.js +6 -0
- package/dist/harness-runtime/vendor/opencode-client/effect/generated/client.d.ts +2529 -0
- package/dist/harness-runtime/vendor/opencode-client/effect/generated/client.js +8 -0
- package/dist/harness-runtime/vendor/opencode-client/effect/generated/index.d.ts +2 -0
- package/dist/harness-runtime/vendor/opencode-client/effect/generated/index.js +12 -0
- package/dist/harness-runtime/vendor/opencode-client/effect/index.d.ts +35 -0
- package/dist/harness-runtime/vendor/opencode-client/effect/index.js +75 -0
- package/dist/harness-runtime/vendor/opencode-client/effect/rpc.d.ts +20 -0
- package/dist/harness-runtime/vendor/opencode-client/effect/rpc.js +11 -0
- package/dist/harness-runtime/vendor/opencode-client/effect/service.d.ts +90 -0
- package/dist/harness-runtime/vendor/opencode-client/effect/service.js +187 -0
- package/dist/harness-runtime/vendor/opencode-client/package.json +84 -0
- package/dist/harness-runtime/vendor/opencode-client/promise/api.d.ts +21 -0
- package/dist/harness-runtime/vendor/opencode-client/promise/api.js +0 -0
- package/dist/harness-runtime/vendor/opencode-client/promise/client.d.ts +252 -0
- package/dist/harness-runtime/vendor/opencode-client/promise/client.js +14 -0
- package/dist/harness-runtime/vendor/opencode-client/promise/generated/client-error.d.ts +8 -0
- package/dist/harness-runtime/vendor/opencode-client/promise/generated/client-error.js +6 -0
- package/dist/harness-runtime/vendor/opencode-client/promise/generated/client.d.ts +259 -0
- package/dist/harness-runtime/vendor/opencode-client/promise/generated/client.js +7 -0
- package/dist/harness-runtime/vendor/opencode-client/promise/generated/index.d.ts +3 -0
- package/dist/harness-runtime/vendor/opencode-client/promise/generated/index.js +79 -0
- package/dist/harness-runtime/vendor/opencode-client/promise/generated/types.d.ts +8932 -0
- package/dist/harness-runtime/vendor/opencode-client/promise/generated/types.js +70 -0
- package/dist/harness-runtime/vendor/opencode-client/promise/index.d.ts +6 -0
- package/dist/harness-runtime/vendor/opencode-client/promise/index.js +83 -0
- package/dist/harness-runtime/vendor/opencode-client/promise/rpc.d.ts +32 -0
- package/dist/harness-runtime/vendor/opencode-client/promise/rpc.js +7 -0
- package/dist/harness-runtime/vendor/opencode-client/promise/service.d.ts +24 -0
- package/dist/harness-runtime/vendor/opencode-client/promise/service.js +187 -0
- package/dist/harness-runtime/vendor/opencode-client/pty-handoff.d.ts +12 -0
- package/dist/harness-runtime/vendor/opencode-client/pty-handoff.js +14 -0
- package/dist/harness-runtime/vendor/opencode-client/rpc-runtime.d.ts +20 -0
- package/dist/harness-runtime/vendor/opencode-client/rpc-runtime.js +14 -0
- package/dist/harness-runtime/vendor/opencode-client/service-contender.d.ts +37 -0
- package/dist/harness-runtime/vendor/opencode-client/service-contender.js +10 -0
- package/dist/harness-runtime/vendor/opencode-client/service-probe.d.ts +70 -0
- package/dist/harness-runtime/vendor/opencode-client/service-probe.js +16 -0
- package/dist/harness-runtime/vendor/opencode-client/service-timing.d.ts +12 -0
- package/dist/harness-runtime/vendor/opencode-client/service-timing.js +10 -0
- package/dist/harness-runtime/vendor/opencode-client/service-version.d.ts +2 -0
- package/dist/harness-runtime/vendor/opencode-client/service-version.js +6 -0
- package/dist/harness-runtime/vendor/opencode-client/service.d.ts +52 -0
- package/dist/harness-runtime/vendor/opencode-client/service.js +1 -0
- package/dist/harness-runtime/vendor/opencode-client/shared-events.d.ts +11 -0
- package/dist/harness-runtime/vendor/opencode-client/shared-events.js +8 -0
- package/dist/harness-runtime/vendor/opencode-client/solid/connection.d.ts +37 -0
- package/dist/harness-runtime/vendor/opencode-client/solid/connection.js +10 -0
- package/dist/harness-runtime/vendor/opencode-client/solid/data.d.ts +211 -0
- package/dist/harness-runtime/vendor/opencode-client/solid/data.js +18 -0
- package/dist/harness-runtime/vendor/opencode-client/solid/index.d.ts +3 -0
- package/dist/harness-runtime/vendor/opencode-client/solid/index.js +32 -0
- package/dist/harness-runtime/vendor/opencode-client/solid/pty.d.ts +22 -0
- package/dist/harness-runtime/vendor/opencode-client/solid/pty.js +8 -0
- package/dist/harness-runtime/vendor/opencode-plugin.mjs +3 -0
- package/dist/protocol/local-files.js +18 -9
- package/dist/runtime/context.js +8 -4
- package/dist/schemas/agent-profile.schema.json +1 -1
- package/dist/schemas/code-review-result.schema.json +1 -1
- package/dist/schemas/code-verification.schema.json +7 -5
- package/dist/schemas/code-work-batch.schema.json +2 -2
- package/dist/schemas/discovery-result.schema.json +184 -0
- package/dist/schemas/execution-bundle.schema.json +205 -0
- package/dist/schemas/execution-settings.schema.json +162 -0
- package/dist/schemas/flow-descriptor.schema.json +54 -0
- package/dist/schemas/flow-run.schema.json +28 -0
- package/dist/schemas/harness-catalog.schema.json +80 -0
- package/dist/schemas/mb-init-compose-result.schema.json +457 -0
- package/dist/schemas/mb-init-discover-result.schema.json +500 -0
- package/dist/schemas/mb-init-finish-result.schema.json +422 -0
- package/dist/schemas/mb-init-preflight-result.schema.json +367 -0
- package/dist/schemas/mb-init-review-result.schema.json +396 -0
- package/dist/schemas/mb-init-verify-result.schema.json +391 -0
- package/dist/schemas/memory-bootstrap-receipt-v2.schema.json +67 -0
- package/dist/schemas/memory-bootstrap-receipt.schema.json +354 -0
- package/dist/schemas/memory-composition-work-result.schema.json +38 -0
- package/dist/schemas/memory-repair-work-result.schema.json +49 -0
- package/dist/schemas/memory-review-correction-plan.schema.json +30 -0
- package/dist/schemas/memory-review-work-result.schema.json +43 -0
- package/dist/schemas/model-catalog.schema.json +153 -0
- package/dist/schemas/plan-review-decision.schema.json +3 -3
- package/dist/schemas/plan-review-result.schema.json +2 -2
- package/dist/schemas/profile-settings.schema.json +122 -0
- package/dist/schemas/release-manifest.schema.json +291 -0
- package/dist/schemas/run-control-receipt.schema.json +118 -0
- package/dist/schemas/run-control-request.schema.json +36 -0
- package/dist/schemas/run-engine-binding.schema.json +1 -1
- package/dist/schemas/vnext-protocol-plan.schema.json +2 -2
- package/dist/services/branch-context.js +3 -3
- package/dist/services/canon.js +11 -8
- package/dist/services/check-runner-evidence.js +250 -0
- package/dist/services/cleanup.js +133 -40
- package/dist/services/cli-operation-classifier.js +25 -62
- package/dist/services/cli-upgrade-allowlist.js +27 -0
- package/dist/services/code-checks.js +1168 -107
- package/dist/services/codex-hook-delivery.js +32 -0
- package/dist/services/command-context.js +80 -0
- package/dist/services/compatibility-preflight.js +13 -3
- package/dist/services/config.js +11 -8
- package/dist/services/continuation-outcome.js +20 -0
- package/dist/services/controller-child-capacity.js +13 -0
- package/dist/services/controller-child-observer.js +115 -0
- package/dist/services/controller-execution.js +88 -0
- package/dist/services/controller-fanout.js +202 -0
- package/dist/services/dashboard.js +143 -58
- package/dist/services/descriptor-coordinator-session.js +35 -0
- package/dist/services/descriptor-root-work.js +53 -0
- package/dist/services/descriptor-run-settlement.js +105 -0
- package/dist/services/descriptor-run-start.js +449 -0
- package/dist/services/descriptor-stage-coordinator.js +197 -0
- package/dist/services/descriptor-stage-settlement.js +222 -0
- package/dist/services/descriptor-stage-start.js +139 -0
- package/dist/services/discovery/agy-discovery.js +2 -0
- package/dist/services/discovery/codex-discovery.js +2 -0
- package/dist/services/discovery/droid-discovery.js +2 -0
- package/dist/services/discovery/grok-discovery.js +2 -0
- package/dist/services/discovery/opencode-discovery.js +2 -0
- package/dist/services/discovery/static.js +31 -0
- package/dist/services/discovery/zcode-discovery.js +2 -0
- package/dist/services/engines.js +294 -153
- package/dist/services/eval-snapshots.js +1215 -116
- package/dist/services/execution-bundles.js +263 -0
- package/dist/services/execution-policy.js +185 -0
- package/dist/services/executor-onboarding.js +56 -0
- package/dist/services/external-work-launch.js +308 -0
- package/dist/services/flow-catalog.js +21 -0
- package/dist/services/flow-descriptors.js +82 -0
- package/dist/services/frozen-engine.js +250 -0
- package/dist/services/generic-execution.js +164 -0
- package/dist/services/harness-adapter.js +355 -36
- package/dist/services/harness-config.js +2 -0
- package/dist/services/harness-discovery.js +29 -0
- package/dist/services/harness-doctor.js +75 -0
- package/dist/services/harness-probe-effects.js +51 -0
- package/dist/services/harness-version-probe.js +100 -0
- package/dist/services/hooks.js +771 -325
- package/dist/services/hybrid-contracts.js +483 -0
- package/dist/services/ids.js +29 -1
- package/dist/services/installation-lock.js +95 -0
- package/dist/services/installed-flow-source.js +183 -0
- package/dist/services/lanes.js +78 -65
- package/dist/services/lifecycle-command.js +1 -288
- package/dist/services/lifecycle-contract.js +1 -0
- package/dist/services/lifecycle-invocations.js +2007 -0
- package/dist/services/managed-daemon-binding.js +38 -0
- package/dist/services/managed-lease-monitor.js +133 -0
- package/dist/services/managed-processes.js +475 -55
- package/dist/services/memory-accepted-composition.js +77 -0
- package/dist/services/memory-baseline-preservation.js +65 -0
- package/dist/services/memory-completed-correction-wave.js +320 -0
- package/dist/services/memory-compose-handler.js +258 -0
- package/dist/services/memory-composition-acceptance.js +199 -0
- package/dist/services/memory-composition-graph.js +134 -0
- package/dist/services/memory-composition-registration.js +146 -0
- package/dist/services/memory-composition-work-custody.js +151 -0
- package/dist/services/memory-composition-work-results.js +96 -0
- package/dist/services/memory-corrected-review-acceptance.js +129 -0
- package/dist/services/memory-corrected-review-batch.js +30 -0
- package/dist/services/memory-corrected-review-findings.js +106 -0
- package/dist/services/memory-corrected-review-inputs.js +210 -0
- package/dist/services/memory-corrected-review-projection.js +78 -0
- package/dist/services/memory-corrected-review-receipt.js +170 -0
- package/dist/services/memory-corrected-review-registration.js +191 -0
- package/dist/services/memory-correction-check-receipt.js +219 -0
- package/dist/services/memory-correction-input-owner.js +6 -0
- package/dist/services/memory-discover-handler.js +104 -0
- package/dist/services/memory-finish-evidence.js +435 -0
- package/dist/services/memory-finish-handler.js +215 -0
- package/dist/services/memory-finish-integration.js +280 -0
- package/dist/services/memory-finish-readiness.js +122 -0
- package/dist/services/memory-git-integration.js +151 -0
- package/dist/services/memory-historical-corrected-review.js +206 -0
- package/dist/services/memory-historical-original-review.js +130 -0
- package/dist/services/memory-historical-repair-registration.js +99 -0
- package/dist/services/memory-historical-second-review.js +128 -0
- package/dist/services/memory-init-bootstrap-readiness.js +210 -0
- package/dist/services/memory-init-contexts.js +89 -0
- package/dist/services/memory-init-controller-start.js +84 -0
- package/dist/services/memory-init-engine-release.js +223 -0
- package/dist/services/memory-init-git-policy.js +136 -0
- package/dist/services/memory-init-lint-capability.js +185 -0
- package/dist/services/memory-init-native-capacity.js +296 -0
- package/dist/services/memory-init-preflight.js +330 -0
- package/dist/services/memory-init-project-bootstrap.js +599 -0
- package/dist/services/memory-init-request-candidate.js +157 -0
- package/dist/services/memory-init-request-input.js +66 -0
- package/dist/services/memory-init-resource-storage.js +125 -0
- package/dist/services/memory-init-run-allocation.js +81 -0
- package/dist/services/memory-init-run-bundle.js +98 -0
- package/dist/services/memory-init-start.js +181 -0
- package/dist/services/memory-init-startup-custody.js +80 -0
- package/dist/services/memory-init-startup-input.js +240 -0
- package/dist/services/memory-init-storage.js +81 -0
- package/dist/services/memory-init-stores.js +359 -0
- package/dist/services/memory-init-worktrunk-capability.js +178 -0
- package/dist/services/memory-layer-publication.js +611 -0
- package/dist/services/memory-layer.js +156 -0
- package/dist/services/memory-lint-config.js +85 -0
- package/dist/services/memory-native-review-copy.js +165 -0
- package/dist/services/memory-normal-flow-entry.js +75 -0
- package/dist/services/memory-ordinary-defaults.js +112 -0
- package/dist/services/memory-preflight-bootstrap-custody.js +115 -0
- package/dist/services/memory-preflight-handler.js +259 -0
- package/dist/services/memory-repair-current-view.js +299 -0
- package/dist/services/memory-repair-document-delta.js +451 -0
- package/dist/services/memory-repair-history.js +491 -0
- package/dist/services/memory-repair-physical-binding.js +312 -0
- package/dist/services/memory-repair-publication-files.js +152 -0
- package/dist/services/memory-repair-publication-intent.js +27 -0
- package/dist/services/memory-repair-publication-plan.js +160 -0
- package/dist/services/memory-repair-publication-source.js +225 -0
- package/dist/services/memory-repair-publication.js +794 -0
- package/dist/services/memory-repair-registration.js +239 -0
- package/dist/services/memory-repair-work-baseline.js +413 -0
- package/dist/services/memory-repair-work-completion.js +97 -0
- package/dist/services/memory-repair-work-custody.js +73 -0
- package/dist/services/memory-repair-work-results.js +89 -0
- package/dist/services/memory-repair-workspace.js +252 -0
- package/dist/services/memory-repair-writer-copy.js +735 -0
- package/dist/services/memory-result-binding.js +173 -0
- package/dist/services/memory-retained-review-authority.js +129 -0
- package/dist/services/memory-retained-review-results.js +70 -0
- package/dist/services/memory-retained-second-baseline.js +102 -0
- package/dist/services/memory-retained-second-repairs.js +156 -0
- package/dist/services/memory-review-acceptance.js +214 -0
- package/dist/services/memory-review-correction-plan.js +171 -0
- package/dist/services/memory-review-handler.js +585 -0
- package/dist/services/memory-review-inputs.js +200 -0
- package/dist/services/memory-review-receipt.js +180 -0
- package/dist/services/memory-review-registration.js +108 -0
- package/dist/services/memory-review-work-custody.js +124 -0
- package/dist/services/memory-review-work-input.js +72 -0
- package/dist/services/memory-review-work-results.js +73 -0
- package/dist/services/memory-second-corrected-review-inputs.js +165 -0
- package/dist/services/memory-second-correction-plan.js +76 -0
- package/dist/services/memory-second-repair-baseline.js +91 -0
- package/dist/services/memory-second-repair-launch.js +614 -0
- package/dist/services/memory-second-repair-registration.js +88 -0
- package/dist/services/memory-second-repair-source.js +187 -0
- package/dist/services/memory-second-review-acceptance.js +115 -0
- package/dist/services/memory-source-observations.js +117 -0
- package/dist/services/memory-stage-dispatch.js +28 -0
- package/dist/services/memory-stage-results.js +199 -0
- package/dist/services/memory-verification-acceptance.js +135 -0
- package/dist/services/memory-verification-checks.js +252 -0
- package/dist/services/memory-verification-receipt.js +159 -0
- package/dist/services/memory-verify-handler.js +135 -0
- package/dist/services/memory-work-result-preparation.js +63 -0
- package/dist/services/merge-queue.js +228 -149
- package/dist/services/merge-server.js +161 -35
- package/dist/services/migrations.js +37 -16
- package/dist/services/model-catalog.js +109 -0
- package/dist/services/model-discovery.js +46 -0
- package/dist/services/native-daemon-history.js +49 -0
- package/dist/services/native-session-control.js +39 -0
- package/dist/services/operator-profiles.js +236 -0
- package/dist/services/plan-runtime.js +26 -11
- package/dist/services/plans.js +28 -25
- package/dist/services/projects.js +37 -17
- package/dist/services/prompts.js +28 -17
- package/dist/services/protocols.js +163 -87
- package/dist/services/provider-capacity.js +385 -0
- package/dist/services/recovery-observation-budget.js +121 -0
- package/dist/services/recovery-snapshot-database.js +560 -0
- package/dist/services/release-archive-producer.js +52 -0
- package/dist/services/release-archive.js +143 -0
- package/dist/services/release-cache.js +142 -0
- package/dist/services/release-compatibility.js +18 -0
- package/dist/services/release-download.js +67 -0
- package/dist/services/release-envelope.js +104 -0
- package/dist/services/release-manifest.js +45 -0
- package/dist/services/release-payload.js +114 -0
- package/dist/services/release-producer.js +63 -0
- package/dist/services/release-provenance.js +160 -0
- package/dist/services/release-registry.js +66 -0
- package/dist/services/release-source.js +194 -0
- package/dist/services/repair-intents.js +42 -0
- package/dist/services/review-copy.js +165 -0
- package/dist/services/review-deferral.js +23 -0
- package/dist/services/run-control-receipt.js +56 -0
- package/dist/services/run-control-worker.js +411 -0
- package/dist/services/run-control.js +1056 -0
- package/dist/services/run-controller-adapter.js +399 -0
- package/dist/services/run-controller-capture.js +186 -0
- package/dist/services/run-controller-process.js +208 -0
- package/dist/services/run-controller-recovery.js +289 -0
- package/dist/services/run-controller-state.js +97 -0
- package/dist/services/run-controller.js +1205 -0
- package/dist/services/run-engine-bindings.js +1 -1
- package/dist/services/run-fork.js +147 -0
- package/dist/services/run-health.js +149 -0
- package/dist/services/run-observations.js +152 -0
- package/dist/services/run-projection.js +22 -64
- package/dist/services/run-recovery-guard.js +77 -0
- package/dist/services/run-recovery-runtime.js +112 -0
- package/dist/services/run-recovery.js +489 -0
- package/dist/services/run-start-custody.js +177 -0
- package/dist/services/runs.js +1019 -247
- package/dist/services/runtime-budget.js +430 -0
- package/dist/services/runtime-command.js +43 -0
- package/dist/services/runtime-scope-capture.js +52 -0
- package/dist/services/runtime-scope-control.js +468 -0
- package/dist/services/runtime-scope-resume.js +599 -0
- package/dist/services/runtime-scope-stop.js +210 -0
- package/dist/services/runtime-scope-worker.js +287 -0
- package/dist/services/runtime-service.js +96 -0
- package/dist/services/schema-validation.js +10 -8
- package/dist/services/scope-writer-cleanup.js +27 -0
- package/dist/services/sessions.js +35 -69
- package/dist/services/setup.js +377 -0
- package/dist/services/stage-blocker.js +17 -7
- package/dist/services/stage-context.js +193 -19
- package/dist/services/stage-handlers.js +74 -0
- package/dist/services/stage-lifecycle.js +152 -116
- package/dist/services/stage-pause.js +89 -75
- package/dist/services/stage-report-renderer.js +234 -6
- package/dist/services/stage-work-graph.js +52 -0
- package/dist/services/tooling.js +41 -30
- package/dist/services/usage.js +49 -101
- package/dist/services/verification-matrix.js +375 -0
- package/dist/services/vnext-code-review.js +223 -88
- package/dist/services/vnext-code.js +327 -226
- package/dist/services/vnext-contracts.js +10 -1
- package/dist/services/vnext-execution-profile.js +80 -5
- package/dist/services/vnext-fanout.js +311 -19
- package/dist/services/vnext-merge.js +332 -111
- package/dist/services/vnext-plan-review.js +279 -127
- package/dist/services/vnext-plan.js +228 -104
- package/dist/services/vnext-protocolize.js +531 -95
- package/dist/services/vnext-specify.js +119 -81
- package/dist/services/vnext-workspace-policy.js +7 -2
- package/dist/services/work-registry.js +1627 -199
- package/dist/services/workspace-bootstrap.js +52 -15
- package/dist/services/workspace-files.js +85 -0
- package/dist/services/worktrees.js +105 -48
- package/dist/shared/entity-references.js +8 -0
- package/dist/shared/errors.js +27 -0
- package/dist/storage/agent-output.js +26 -0
- package/dist/storage/database.js +891 -39
- package/dist/storage/foreign-key-transaction.js +39 -0
- package/dist/storage/immutable-input.js +45 -0
- package/dist/storage/import-guard.js +102 -0
- package/dist/storage/native-cleanup.js +52 -0
- package/dist/storage/paths.js +17 -4
- package/dist/storage/projection-lock.js +73 -0
- package/dist/storage/work-references.js +23 -0
- package/dist/storage/writer-contract.js +89 -0
- package/dist/storage/writer-migration.js +85 -0
- package/package.json +43 -13
- package/tools/create-canon-release.mjs +33 -0
- package/tools/produce-hybrid-release.mjs +41 -0
- package/tools/repair-paused-run-status.mjs +84 -0
|
@@ -1,25 +1,97 @@
|
|
|
1
1
|
import crypto from "node:crypto";
|
|
2
|
+
import { candidateFailureFiles, checkRepairDisposition, readRunnerEvidence, readRunnerProgress, runnerContractsValid, runnerEvidencePass, runnerObservationDurations, runnerProofArtifacts } from "./check-runner-evidence.js";
|
|
3
|
+
export { assertSourceRepairEligible, checkRepairDisposition } from "./check-runner-evidence.js";
|
|
4
|
+
import { isGitlessWorkspace, ownedWorkspaceHookPath, projectServicePathFilter, workspaceGitFiles } from "./workspace-files.js";
|
|
2
5
|
import { spawn, spawnSync } from "node:child_process";
|
|
6
|
+
import { ObservationClock, validateObservationDuration } from "../harness-runtime/lib/observation-clock.mjs";
|
|
3
7
|
import fs from "node:fs";
|
|
4
8
|
import path from "node:path";
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
9
|
+
import { isDeepStrictEqual } from "node:util";
|
|
10
|
+
import { parse } from "shell-quote";
|
|
11
|
+
import { assertRunMutationAllowed } from "./run-recovery.js";
|
|
12
|
+
import { AppError, errorRecord } from "../shared/errors.js";
|
|
13
|
+
import { stopProcessGroup } from "../harness-runtime/lib/managed-daemon.mjs";
|
|
14
|
+
import { createRenewalState, maintenanceRetryable, renewalConfirmed, renewalDelay, renewalFailure, renewalRemaining, RENEWAL_POLICY } from "../harness-runtime/lib/lease-renewal.mjs";
|
|
15
|
+
import { confirmManagedProcess, finishManagedProcess, renewManagedProcess, managedProcessStatus, managedProcessRuntimeHome, managedOwnerIsAlive, processTreeIsAlive, registerManagedProcess, reservePorts } from "./managed-processes.js";
|
|
16
|
+
import { admitRuntimeCheck, runtimeBudgetForRun } from "./runtime-budget.js";
|
|
17
|
+
import { getResourceDatabase } from "../storage/database.js";
|
|
18
|
+
import { readBoundedReleaseFile } from "./release-source.js";
|
|
7
19
|
const profileRelativePath = path.join(".memory-bank", "spec", "engineering", "code-check-profile.json");
|
|
8
20
|
// Receipt reservation precedes child spawn so an interrupted CLI leaves an
|
|
9
21
|
// auditable attempt. A second caller must not mistake that small handoff window
|
|
10
22
|
// for a dead check and overwrite its outcome.
|
|
11
23
|
const receiptStartingGraceMs = 30_000;
|
|
12
|
-
|
|
24
|
+
/** The check writer proves an empty tree before marking its registration terminal. */
|
|
25
|
+
export function terminalCheckSettled(context, record) {
|
|
26
|
+
if (record.kind !== "check" || !record.check_id || record.owner_id !== `check:${record.check_id}` || !record.project_id || !record.run_id
|
|
27
|
+
|| !record.pid || !record.pid_started_at || !["stopped", "failed"].includes(record.state) || !record.finished_at)
|
|
28
|
+
return null;
|
|
29
|
+
let metadata;
|
|
30
|
+
try {
|
|
31
|
+
metadata = JSON.parse(record.metadata_json);
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
if (metadata.role !== "check" || managedProcessRuntimeHome(record, context) !== context.ddFlowHome)
|
|
37
|
+
return null;
|
|
38
|
+
let resource;
|
|
39
|
+
try {
|
|
40
|
+
resource = getResourceDatabase(context.env.DD_FLOW_RESOURCE_HOME ?? context.ddFlowHome, "read_existing");
|
|
41
|
+
}
|
|
42
|
+
catch {
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
try {
|
|
46
|
+
const current = resource.get("SELECT * FROM managed_processes WHERE id = ?", [record.id]);
|
|
47
|
+
if (!current || current.lease_token !== record.lease_token || current.owner_id !== record.owner_id || current.pid !== record.pid
|
|
48
|
+
|| current.pid_started_at !== record.pid_started_at || current.metadata_json !== record.metadata_json || current.state !== record.state
|
|
49
|
+
|| current.project_id !== record.project_id || current.run_id !== record.run_id || current.work_id !== record.work_id
|
|
50
|
+
|| current.check_id !== record.check_id || current.finished_at !== record.finished_at)
|
|
51
|
+
return null;
|
|
52
|
+
const receipt = context.db.get("SELECT status, finished_at, work_id FROM check_receipts WHERE id = ? AND project_id = ? AND run_id = ?", [record.check_id, record.project_id, record.run_id]);
|
|
53
|
+
return receipt && receipt.work_id === record.work_id && ["passed", "failed", "aborted"].includes(receipt.status) && receipt.finished_at
|
|
54
|
+
? { status: receipt.status, finished_at: receipt.finished_at } : null;
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
finally {
|
|
60
|
+
resource.close?.();
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
export function codeExecutionEnvironment(workspaceRoot, checkEnvironment) {
|
|
64
|
+
if (checkEnvironment !== undefined && (!checkEnvironment || typeof checkEnvironment !== "object" || Array.isArray(checkEnvironment)
|
|
65
|
+
|| Object.keys(checkEnvironment).length !== 1 || typeof checkEnvironment.CODEX_HOME !== "string"
|
|
66
|
+
|| checkEnvironment.CODEX_HOME.length > 4096 || checkEnvironment.CODEX_HOME.includes("\0") || !path.isAbsolute(checkEnvironment.CODEX_HOME)))
|
|
67
|
+
throw new AppError("invalid_check_environment", "Owned check environment must name one absolute Codex home", 2, { effect: "no_effect" });
|
|
68
|
+
const suffix = crypto.createHash("sha256").update(path.resolve(workspaceRoot)).digest("hex").slice(0, 12);
|
|
69
|
+
// The managed CLI's Node must win over a provider app's PATH: a Rosetta
|
|
70
|
+
// provider can otherwise run x64 checks against arm64-installed modules.
|
|
71
|
+
const environment = { ...process.env, PATH: `${path.dirname(process.execPath)}${path.delimiter}${process.env.PATH ?? ""}`, DD_FLOW_LOCAL_DATABASE_SUFFIX: suffix };
|
|
72
|
+
delete environment.NODE_PATH;
|
|
73
|
+
for (const key of ["DD_FLOW_CHECK_RUNNER_EVIDENCE_PATH", "DD_FLOW_CHECK_RUNNER_PROGRESS_PATH", "DD_FLOW_CHECK_INVOCATION_ID", "DD_FLOW_CHECK_ID", "DD_FLOW_CHECK_INPUT_HASH", "DD_FLOW_CHECK_VERIFICATION_EPOCH", "DD_FLOW_CHECK_COMMAND"])
|
|
74
|
+
delete environment[key];
|
|
75
|
+
if (checkEnvironment)
|
|
76
|
+
environment.CODEX_HOME = checkEnvironment.CODEX_HOME;
|
|
77
|
+
return environment;
|
|
78
|
+
}
|
|
79
|
+
export function workspaceOperationId(workspaceRoot) { return `workspace:${crypto.createHash("sha256").update(path.resolve(workspaceRoot)).digest("hex")}`; }
|
|
13
80
|
export function readCodeCheckProfile(workspaceRoot) {
|
|
14
81
|
const file = path.join(workspaceRoot, profileRelativePath);
|
|
15
82
|
if (!fs.existsSync(file))
|
|
16
83
|
return { profile: null, hash: null, file };
|
|
17
84
|
const bytes = fs.readFileSync(file);
|
|
18
85
|
const value = JSON.parse(bytes.toString("utf8"));
|
|
19
|
-
if (
|
|
20
|
-
throw new AppError("invalid_code_check_profile", "CODE check profile has an unsupported schema", 2, { file, schema_id: value.schema_id ?? null,
|
|
21
|
-
if (!
|
|
22
|
-
throw new AppError("invalid_code_check_profile", "
|
|
86
|
+
if (!["dd-flow/code-check-profile@6", "dd-flow/code-check-profile@7"].includes(value.schema_id ?? ""))
|
|
87
|
+
throw new AppError("invalid_code_check_profile", "CODE check profile has an unsupported schema", 2, { file, schema_id: value.schema_id ?? null, supported: ["dd-flow/code-check-profile@6", "dd-flow/code-check-profile@7"] });
|
|
88
|
+
if (!runnerContractsValid(value.runner_evidence_by_alias) || value.schema_id === "dd-flow/code-check-profile@6" && value.runner_evidence_by_alias !== undefined)
|
|
89
|
+
throw new AppError("invalid_code_check_profile", "Runner evidence requires valid profile@7 relative artifact contracts", 2, { file });
|
|
90
|
+
for (const alias of Object.keys(value.runner_evidence_by_alias ?? {}))
|
|
91
|
+
if (!value.aliases?.[alias])
|
|
92
|
+
throw new AppError("invalid_code_check_profile", "Runner evidence references an unknown alias", 2, { file, alias });
|
|
93
|
+
if (!isStringRecord(value.aliases) || !isStringRecord(value.require_alias_for) || !isGateRecord(value.mandatory_by_gate) || !isPortAliasRecord(value.ports_by_alias) || !isInputAliasRecord(workspaceRoot, value.inputs_by_alias))
|
|
94
|
+
throw new AppError("invalid_code_check_profile", "CODE check profile aliases, require_alias_for, mandatory_by_gate, ports_by_alias or inputs_by_alias are invalid", 2, { file });
|
|
23
95
|
for (const [gate, aliases] of Object.entries(value.mandatory_by_gate))
|
|
24
96
|
for (const alias of aliases ?? [])
|
|
25
97
|
if (!value.aliases[alias])
|
|
@@ -27,19 +99,76 @@ export function readCodeCheckProfile(workspaceRoot) {
|
|
|
27
99
|
for (const alias of Object.keys(value.ports_by_alias ?? {}))
|
|
28
100
|
if (!value.aliases[alias])
|
|
29
101
|
throw new AppError("invalid_code_check_profile", "A port declaration references an unknown alias", 2, { file, alias });
|
|
102
|
+
for (const alias of Object.keys(value.inputs_by_alias ?? {}))
|
|
103
|
+
if (!value.aliases[alias])
|
|
104
|
+
throw new AppError("invalid_code_check_profile", "An input declaration references an unknown alias", 2, { file, alias });
|
|
30
105
|
return { profile: value, hash: crypto.createHash("sha256").update(bytes).digest("hex"), file };
|
|
31
106
|
}
|
|
107
|
+
/**
|
|
108
|
+
* Planned aliases are qualification-owned extensions: their provider Work is
|
|
109
|
+
* expected to materialize them after PLAN. Freeze the project policy and
|
|
110
|
+
* existing aliases, while still validating every planned alias definition at
|
|
111
|
+
* execution time. Unknown edits and changes to existing policy remain part
|
|
112
|
+
* of the hash and therefore fail closed.
|
|
113
|
+
*/
|
|
114
|
+
export function codeCheckProfileHash(workspaceRoot, checks = []) {
|
|
115
|
+
const { profile } = readCodeCheckProfile(workspaceRoot);
|
|
116
|
+
if (!profile)
|
|
117
|
+
return null;
|
|
118
|
+
const planned = new Set(checks.filter(check => check.availability === "planned" && check.command.startsWith("@check/")).map(check => check.command));
|
|
119
|
+
const serialize = (value) => profile.schema_id === "dd-flow/code-check-profile@7"
|
|
120
|
+
? JSON.stringify(value, (_key, item) => item && typeof item === "object" && !Array.isArray(item) ? Object.fromEntries(Object.entries(item).sort(([left], [right]) => left.localeCompare(right))) : item)
|
|
121
|
+
: JSON.stringify(value);
|
|
122
|
+
if (!planned.size)
|
|
123
|
+
return crypto.createHash("sha256").update(serialize(profile)).digest("hex");
|
|
124
|
+
const canonical = {
|
|
125
|
+
...profile,
|
|
126
|
+
aliases: Object.fromEntries(Object.entries(profile.aliases).filter(([alias]) => !planned.has(alias))),
|
|
127
|
+
ports_by_alias: Object.fromEntries(Object.entries(profile.ports_by_alias ?? {}).filter(([alias]) => !planned.has(alias))),
|
|
128
|
+
inputs_by_alias: Object.fromEntries(Object.entries(profile.inputs_by_alias ?? {}).filter(([alias]) => !planned.has(alias))),
|
|
129
|
+
...(profile.schema_id === "dd-flow/code-check-profile@7" ? { runner_evidence_by_alias: Object.fromEntries(Object.entries(profile.runner_evidence_by_alias ?? {}).filter(([alias]) => !planned.has(alias))) } : {})
|
|
130
|
+
};
|
|
131
|
+
return crypto.createHash("sha256").update(serialize(canonical)).digest("hex");
|
|
132
|
+
}
|
|
133
|
+
/** The integration target starts at its baseline profile; the reviewed source
|
|
134
|
+
* may add aliases, but must not change existing check policy or bindings. */
|
|
135
|
+
export function mergeCheckProfileHash(sourceWorkspace, targetWorkspace, checks) {
|
|
136
|
+
const source = readCodeCheckProfile(sourceWorkspace).profile;
|
|
137
|
+
const target = readCodeCheckProfile(targetWorkspace).profile;
|
|
138
|
+
if (!source && !target)
|
|
139
|
+
return null;
|
|
140
|
+
if (!source || !target)
|
|
141
|
+
throw new AppError("check_profile_drift", "Source and integration check profiles differ", 1);
|
|
142
|
+
const retained = structuredClone(source);
|
|
143
|
+
for (const field of ["aliases", "ports_by_alias", "inputs_by_alias", "runner_evidence_by_alias"]) {
|
|
144
|
+
const old = target[field];
|
|
145
|
+
if (!old) {
|
|
146
|
+
delete retained[field];
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
const current = source[field] ?? {};
|
|
150
|
+
retained[field] = Object.fromEntries(Object.keys(old).filter(key => key in current).map(key => [key, current[key]]));
|
|
151
|
+
}
|
|
152
|
+
const existingAliases = new Set(Object.keys(target.aliases));
|
|
153
|
+
const changedExistingMetadata = ["ports_by_alias", "inputs_by_alias", "runner_evidence_by_alias"].some(field => Object.keys(source[field] ?? {}).some(alias => existingAliases.has(alias) && !(alias in (target[field] ?? {}))));
|
|
154
|
+
if (changedExistingMetadata || !isDeepStrictEqual(retained, target)) {
|
|
155
|
+
throw new AppError("check_profile_drift", "Source check profile changes existing integration policy", 1, { source_profile_hash: codeCheckProfileHash(sourceWorkspace, checks), target_profile_hash: codeCheckProfileHash(targetWorkspace, checks) });
|
|
156
|
+
}
|
|
157
|
+
return codeCheckProfileHash(sourceWorkspace, checks);
|
|
158
|
+
}
|
|
32
159
|
export function validateCodeCheckCommands(workspaceRoot, commands) {
|
|
33
160
|
const { profile, file } = readCodeCheckProfile(workspaceRoot);
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
161
|
+
return commands.map((command) => {
|
|
162
|
+
if (command.startsWith("@check/")) {
|
|
163
|
+
if (!profile?.aliases[command]?.trim())
|
|
164
|
+
throw new AppError("unknown_code_check_alias", "CODE check alias is not defined by the project profile", 2, { alias: command, file });
|
|
165
|
+
return command;
|
|
166
|
+
}
|
|
167
|
+
for (const [prefix, alias] of Object.entries(profile?.require_alias_for ?? {}))
|
|
168
|
+
if (command === prefix || command.startsWith(`${prefix} `))
|
|
169
|
+
throw new AppError("raw_code_check_forbidden", "CODE check must use the project alias instead of a raw guarded command", 2, { command, required_alias: alias, file });
|
|
39
170
|
return command;
|
|
40
|
-
}
|
|
41
|
-
if (command === prefix || command.startsWith(`${prefix} `))
|
|
42
|
-
throw new AppError("raw_code_check_forbidden", "CODE check must use the project alias instead of a raw guarded command", 2, { command, required_alias: alias, file }); return command; });
|
|
171
|
+
});
|
|
43
172
|
}
|
|
44
173
|
export function resolveCodeCheckCommands(workspaceRoot, runId, commands) {
|
|
45
174
|
const validated = validateCodeCheckCommands(workspaceRoot, commands);
|
|
@@ -50,7 +179,11 @@ export function resolveCodeCheckCommands(workspaceRoot, runId, commands) {
|
|
|
50
179
|
}
|
|
51
180
|
export function projectPolicyCheckDeclarations(workspaceRoot, gate) {
|
|
52
181
|
const { profile } = readCodeCheckProfile(workspaceRoot);
|
|
53
|
-
return (profile
|
|
182
|
+
return projectPolicyCheckDeclarationsFromProfile(profile, gate);
|
|
183
|
+
}
|
|
184
|
+
/** The caller may already have independently admitted exact profile bytes. */
|
|
185
|
+
export function projectPolicyCheckDeclarationsFromProfile(profile, gate) {
|
|
186
|
+
return (profile?.mandatory_by_gate[gate] ?? []).map((alias) => { const ref = `POLICY/${gate}/${alias.slice(7).replace(/[^a-z0-9]+/gi, "-").replace(/^-|-$/g, "").toLowerCase()}`; return { id: ref, canonical_ref: ref, source: "project_policy", command: alias, purpose: `Mandatory project ${gate} gate.`, run_at: gate, availability: "available", ...(profile?.ports_by_alias?.[alias] ? { ports: profile.ports_by_alias[alias] } : {}), ...(profile?.inputs_by_alias?.[alias] ? { inputs: profile.inputs_by_alias[alias] } : {}) }; });
|
|
54
187
|
}
|
|
55
188
|
export function effectiveCheckDeclarations(workspaceRoot, declared, gates) { return gates.flatMap((gate) => [...declared.filter((check) => check.run_at === gate), ...projectPolicyCheckDeclarations(workspaceRoot, gate)]); }
|
|
56
189
|
export function aggregateCheckDeclarations(workspaceRoot) { return projectPolicyCheckDeclarations(workspaceRoot, "code"); }
|
|
@@ -60,12 +193,21 @@ export function validateCheckPlacement(workspaceRoot, checks) { const { profile
|
|
|
60
193
|
if (check.run_at === "work" && aggregateCommands.has(check.command))
|
|
61
194
|
throw new AppError("aggregate_check_requires_code_gate", "A project aggregate check must run at CODE or readiness, not inside one scoped Work", 2, { check_id: check.id, command: check.command, run_at: check.run_at }); }
|
|
62
195
|
export function validateCheckDeclaration(workspaceRoot, check) {
|
|
63
|
-
if (check.
|
|
64
|
-
|
|
196
|
+
if ((check.required_artifacts ?? []).some(file => path.posix.normalize(file.replaceAll("\\", "/")).split("/")[0] === "engine-outcome.json"))
|
|
197
|
+
throw new AppError("invalid_code_check_profile", "engine-outcome.json is reserved for engine execution evidence, not a candidate artifact", 2, { check_id: check.id });
|
|
198
|
+
if (!["work", "code", "readiness", "merge", "release"].includes(check.run_at))
|
|
199
|
+
throw new AppError("unsupported_check_gate", "Check requires an autonomous executable gate; replan the incompatible declaration", 2, { check_id: check.id, run_at: check.run_at });
|
|
200
|
+
if (check.runner_evidence !== undefined && !runnerContractsValid({ "@check/declaration": check.runner_evidence }))
|
|
201
|
+
throw new AppError("invalid_code_check_profile", "Check runner evidence contract is invalid", 2, { check_id: check.id });
|
|
65
202
|
const { profile } = readCodeCheckProfile(workspaceRoot);
|
|
203
|
+
const resolvedCommand = profile?.aliases[check.command] ?? check.command;
|
|
204
|
+
validateTerminatingCheckCommand(resolvedCommand, check.id);
|
|
66
205
|
if (check.availability === "planned" && check.command.startsWith("@check/") && !profile?.aliases[check.command])
|
|
67
206
|
throw new AppError("planned_check_not_materialized", "A planned check alias was not materialized by its provider Work", 2, { check_id: check.id, command: check.command, provider: check.provided_by ?? null });
|
|
68
207
|
validateCodeCheckCommands(workspaceRoot, [check.command]);
|
|
208
|
+
if (check.availability === "available" && !check.command.startsWith("@check/"))
|
|
209
|
+
assertDirectCheckExecutable(workspaceRoot, check.command, check.id);
|
|
210
|
+
validateCheckInputs(workspaceRoot, check.inputs ?? []);
|
|
69
211
|
if (check.command.startsWith("@check/") && check.definition && profile?.aliases[check.command] !== check.definition)
|
|
70
212
|
throw new AppError("check_definition_drift", "The accepted check alias definition differs from the current project profile", 2, { check_id: check.id, alias: check.command, expected: check.definition, actual: profile?.aliases[check.command] ?? null });
|
|
71
213
|
if (check.availability !== "planned")
|
|
@@ -77,36 +219,379 @@ export function validateCheckDeclaration(workspaceRoot, check) {
|
|
|
77
219
|
if (!profile?.aliases[check.command])
|
|
78
220
|
throw new AppError("planned_check_not_materialized", "A planned check alias was not materialized by its provider Work", 2, { check_id: check.id, command: check.command, provider: check.provided_by ?? null });
|
|
79
221
|
}
|
|
222
|
+
export function validateTerminatingCheckCommand(command, checkId) {
|
|
223
|
+
if (isBareServiceScript(command))
|
|
224
|
+
throw new AppError("non_terminating_check", "A check must terminate; run a finite test instead of a bare dev/start/serve/watch service script", 2, { check_id: checkId, command });
|
|
225
|
+
}
|
|
226
|
+
function isBareServiceScript(command) {
|
|
227
|
+
const words = parse(command.trim());
|
|
228
|
+
if (words.some((word) => typeof word !== "string"))
|
|
229
|
+
return false;
|
|
230
|
+
let args = words;
|
|
231
|
+
while (/^[A-Za-z_][A-Za-z0-9_]*=/.test(args[0] ?? ""))
|
|
232
|
+
args = args.slice(1);
|
|
233
|
+
const manager = path.basename(args.shift() ?? "");
|
|
234
|
+
if (!["pnpm", "npm", "yarn", "bun"].includes(manager))
|
|
235
|
+
return false;
|
|
236
|
+
while (args.length && ["--filter", "-F", "--dir", "-C", "--cwd"].includes(args[0]))
|
|
237
|
+
args.splice(0, 2);
|
|
238
|
+
if (args[0] === "run")
|
|
239
|
+
args.shift();
|
|
240
|
+
return ["dev", "start", "serve", "watch"].includes(args[0] ?? "");
|
|
241
|
+
}
|
|
242
|
+
/** Reject a plan-time typo or a fictitious "manual" command before it becomes
|
|
243
|
+
* a late shell exit. Compound shell programs remain valid: only a simple,
|
|
244
|
+
* directly named executable is preflighted here. */
|
|
245
|
+
function assertDirectCheckExecutable(workspaceRoot, command, checkId) {
|
|
246
|
+
const trimmed = command.trim();
|
|
247
|
+
// A shell operator or expansion makes the launch path dynamic. Do not
|
|
248
|
+
// guess there; this preflight is deliberately limited to a direct program
|
|
249
|
+
// with ordinary arguments and optional literal environment assignments.
|
|
250
|
+
if (/[|&;<>()`$\n\r~]/.test(trimmed))
|
|
251
|
+
return;
|
|
252
|
+
const parsed = parse(trimmed);
|
|
253
|
+
if (parsed.some(word => typeof word !== "string"))
|
|
254
|
+
return;
|
|
255
|
+
const words = parsed;
|
|
256
|
+
let index = 0;
|
|
257
|
+
const env = codeExecutionEnvironment(workspaceRoot);
|
|
258
|
+
while (/^[A-Za-z_][A-Za-z0-9_]*=/.test(words[index] ?? "")) {
|
|
259
|
+
const assignment = words[index++];
|
|
260
|
+
const equals = assignment.indexOf("=");
|
|
261
|
+
env[assignment.slice(0, equals)] = assignment.slice(equals + 1);
|
|
262
|
+
}
|
|
263
|
+
const program = words[index];
|
|
264
|
+
if (!program)
|
|
265
|
+
return;
|
|
266
|
+
const probe = spawnSync("/bin/sh", ["-c", "command -v -- \"$1\" >/dev/null", "dd-flow-check", program], { cwd: workspaceRoot, env, encoding: "utf8", timeout: 5000 });
|
|
267
|
+
if (probe.error || probe.signal)
|
|
268
|
+
throw new AppError("check_command_probe_unavailable", "Executable probe could not be observed; availability is unknown", 2, { check_id: checkId, executable: program, cause: probe.error?.message, signal: probe.signal });
|
|
269
|
+
if (probe.status !== 0)
|
|
270
|
+
throw new AppError("check_command_unavailable", "Available CODE check names an executable that is unavailable in this environment", 2, { check_id: checkId, command, executable: program });
|
|
271
|
+
}
|
|
272
|
+
// An owning handler supplies this identity; it is a reuse boundary, not an
|
|
273
|
+
// authority proof. Omitting it preserves the historical SDLC input hash.
|
|
274
|
+
function validateCheckEvidenceContext(value) {
|
|
275
|
+
if (value !== undefined && (typeof value !== "string" || !/^[A-Za-z0-9][A-Za-z0-9:._-]{0,255}$/.test(value)))
|
|
276
|
+
throw new AppError("check_evidence_context_invalid", "Check evidence context must be a bounded opaque identifier", 2, { effect: "no_effect" });
|
|
277
|
+
}
|
|
278
|
+
const evidenceDigest = (value) => crypto.createHash("sha256").update(JSON.stringify(value)).digest("hex");
|
|
279
|
+
/** Prepares current physical evidence outside a writer and retains an exact
|
|
280
|
+
* SQL-only fence. Declarations must come from the owning handler/policy, never
|
|
281
|
+
* from the result whose acceptance is being decided. This does not supply
|
|
282
|
+
* semantic acceptance or choose a memory verification policy.
|
|
283
|
+
*/
|
|
284
|
+
export class PreparedCheckEvidence {
|
|
285
|
+
#input;
|
|
286
|
+
#snapshot;
|
|
287
|
+
#generation;
|
|
288
|
+
#references;
|
|
289
|
+
#artifacts;
|
|
290
|
+
#completions = [];
|
|
291
|
+
#passed;
|
|
292
|
+
constructor(input, snapshot, generation, references, artifacts, passed) {
|
|
293
|
+
this.#input = structuredClone({ projectId: input.projectId, runId: input.runId, runHome: input.runHome, workspaceRoot: input.workspaceRoot, scope: input.scope, checks: input.checks,
|
|
294
|
+
...(input.mode === undefined ? {} : { mode: input.mode }), ...(input.workId === undefined ? {} : { workId: input.workId }), ...(input.expectedProfileHash === undefined ? {} : { expectedProfileHash: input.expectedProfileHash }), ...(input.evidenceContext === undefined ? {} : { evidenceContext: input.evidenceContext }), ...(input.checkEnvironment === undefined ? {} : { checkEnvironment: input.checkEnvironment }) });
|
|
295
|
+
this.#snapshot = snapshot;
|
|
296
|
+
this.#generation = generation;
|
|
297
|
+
this.#references = references;
|
|
298
|
+
this.#artifacts = artifacts;
|
|
299
|
+
this.#passed = passed;
|
|
300
|
+
Object.freeze(this);
|
|
301
|
+
}
|
|
302
|
+
static prepare(context, input) {
|
|
303
|
+
validateCheckEvidenceContext(input.evidenceContext);
|
|
304
|
+
const fail = () => { throw new AppError("check_evidence_stale", "Current declared checks lack owned passed evidence", 2, { effect: "no_effect" }); };
|
|
305
|
+
if (!input.checks.length || (input.scope === "work") !== Boolean(input.workId) || input.mode !== undefined && input.mode !== "diagnostic")
|
|
306
|
+
fail();
|
|
307
|
+
const generation = assertRunMutationAllowed(context, input.projectId, input.runId);
|
|
308
|
+
const snapshot = checkEvidenceSqlSnapshot(context, input);
|
|
309
|
+
const selected = JSON.parse(snapshot);
|
|
310
|
+
if (!selected.run || fs.realpathSync.native(selected.run.workspace_root) !== fs.realpathSync.native(input.workspaceRoot)
|
|
311
|
+
|| fs.realpathSync.native(selected.run.run_root ?? selected.run.run_dir) !== fs.realpathSync.native(input.runHome))
|
|
312
|
+
fail();
|
|
313
|
+
const rows = selected.receipts;
|
|
314
|
+
const root = fs.realpathSync.native(input.runHome);
|
|
315
|
+
for (const row of rows) {
|
|
316
|
+
const relative = path.relative(path.resolve(input.runHome), row.receipt_path);
|
|
317
|
+
if (!path.isAbsolute(row.receipt_path) || relative.startsWith(`..${path.sep}`) || relative === ".." || path.isAbsolute(relative))
|
|
318
|
+
fail();
|
|
319
|
+
if (fs.existsSync(row.receipt_path)) {
|
|
320
|
+
const physical = path.relative(root, fs.realpathSync.native(row.receipt_path));
|
|
321
|
+
if (physical.startsWith(`..${path.sep}`) || physical === ".." || path.isAbsolute(physical))
|
|
322
|
+
fail();
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
for (const check of input.checks)
|
|
326
|
+
validateCheckDeclaration(input.workspaceRoot, check);
|
|
327
|
+
const { profile } = readCodeCheckProfile(input.workspaceRoot), profileHash = codeCheckProfileHash(input.workspaceRoot, input.checks);
|
|
328
|
+
if (input.expectedProfileHash !== undefined && profileHash !== input.expectedProfileHash)
|
|
329
|
+
fail();
|
|
330
|
+
const checks = input.checks.map(check => {
|
|
331
|
+
const runner = profile?.runner_evidence_by_alias?.[check.command] ?? check.runner_evidence;
|
|
332
|
+
return { ...check, ports: check.ports ?? profile?.ports_by_alias?.[check.command] ?? [], inputs: check.inputs ?? profile?.inputs_by_alias?.[check.command] ?? [],
|
|
333
|
+
...(runner ? { runner_evidence: runner, required_artifacts: [...new Set([...(check.required_artifacts ?? []), runner.path])] } : {}) };
|
|
334
|
+
});
|
|
335
|
+
const commands = resolveCodeCheckCommands(input.workspaceRoot, input.runId, checks.map(check => check.command));
|
|
336
|
+
assertConsistentDeclarations(checks, commands);
|
|
337
|
+
const state = workspaceState(input.workspaceRoot, [input.runHome]);
|
|
338
|
+
const receipts = rows.map(projectCheckReceiptRow);
|
|
339
|
+
const artifacts = [], completions = [];
|
|
340
|
+
let passed = true;
|
|
341
|
+
const references = deduplicate(checks, commands).map(group => {
|
|
342
|
+
const inputHash = checkInputHash(input.workspaceRoot, group, profileHash, workspaceInputFingerprint(input.workspaceRoot, state.files, group.inputs, [input.runHome]), input.evidenceContext, input.checkEnvironment);
|
|
343
|
+
const receipt = receipts.filter(item => item.input_hash === inputHash && item.profile_hash === profileHash && item.scope === input.scope && item.work_id === (input.workId ?? null)).at(-1);
|
|
344
|
+
if (!receipt || !checkReceiptEvidenceValidForMode(bindReceipt(receipt, group), input.mode))
|
|
345
|
+
fail();
|
|
346
|
+
passed = passed && receipt.status === "passed";
|
|
347
|
+
for (const artifact of receipt.artifacts)
|
|
348
|
+
artifacts.push({ root: "run", path: path.relative(input.runHome, path.join(path.dirname(receipt.receipt_path), "artifacts", artifact.path)).split(path.sep).join("/"), sha256: artifact.sha256 });
|
|
349
|
+
const bytes = fs.readFileSync(receipt.receipt_path);
|
|
350
|
+
completions.push({ root: "run", path: path.relative(input.runHome, receipt.completion_path).split(path.sep).join("/"), sha256: crypto.createHash("sha256").update(fs.readFileSync(receipt.completion_path)).digest("hex") });
|
|
351
|
+
return { id: receipt.id, path: receipt.receipt_path, sha256: crypto.createHash("sha256").update(bytes).digest("hex") };
|
|
352
|
+
});
|
|
353
|
+
const prepared = new PreparedCheckEvidence(input, snapshot, generation, references, artifacts, passed);
|
|
354
|
+
prepared.#completions = completions;
|
|
355
|
+
prepared.assertCustody(context);
|
|
356
|
+
return prepared;
|
|
357
|
+
}
|
|
358
|
+
references() { return structuredClone(this.#references); }
|
|
359
|
+
artifactReferences() { return structuredClone(this.#artifacts); }
|
|
360
|
+
sourceReferences() { return [...this.artifactReferences(), ...structuredClone(this.#completions), ...this.resultReferences().map(reference => ({ root: "run", ...reference }))]; }
|
|
361
|
+
qualityPassed() { return this.#passed; }
|
|
362
|
+
resultReferences() {
|
|
363
|
+
return this.#references.map(reference => ({ path: path.relative(this.#input.runHome, reference.path).split(path.sep).join("/"), sha256: reference.sha256 }));
|
|
364
|
+
}
|
|
365
|
+
/** For an owning acceptance writer to retain independently of agent bytes.
|
|
366
|
+
* The row digest pins historical execution, not later workspace policy.
|
|
367
|
+
*/
|
|
368
|
+
retainedReferences(context) {
|
|
369
|
+
return context.db.readSnapshot(() => {
|
|
370
|
+
this.assertCustody(context);
|
|
371
|
+
return this.#references.map(reference => ({ id: reference.id, path: path.relative(this.#input.runHome, reference.path).split(path.sep).join("/"), sha256: reference.sha256,
|
|
372
|
+
sql_sha256: evidenceDigest(context.db.get("SELECT * FROM check_receipts WHERE project_id=? AND run_id=? AND id=?", [this.#input.projectId, this.#input.runId, reference.id])) }));
|
|
373
|
+
});
|
|
374
|
+
}
|
|
375
|
+
assertOwner(scope) {
|
|
376
|
+
if (scope.projectId !== this.#input.projectId || scope.runId !== this.#input.runId || scope.scope !== undefined && scope.scope !== this.#input.scope)
|
|
377
|
+
throw new AppError("check_receipt_changed", "Prepared checks belong to another RUN or scope", 2, { effect: "no_effect" });
|
|
378
|
+
}
|
|
379
|
+
assertCustody(context) {
|
|
380
|
+
assertRunMutationAllowed(context, this.#input.projectId, this.#input.runId, this.#generation);
|
|
381
|
+
if (checkEvidenceSqlSnapshot(context, this.#input) !== this.#snapshot)
|
|
382
|
+
throw new AppError("check_receipt_changed", "Prepared check SQL custody changed", 2, { effect: "no_effect" });
|
|
383
|
+
}
|
|
384
|
+
revalidate(context) {
|
|
385
|
+
const current = PreparedCheckEvidence.prepare(context, this.#input);
|
|
386
|
+
if (!isDeepStrictEqual(current.sourceReferences(), this.sourceReferences()) || !isDeepStrictEqual(current.#references, this.#references) || current.#snapshot !== this.#snapshot)
|
|
387
|
+
throw new AppError("check_evidence_stale", "Prepared check evidence changed", 2, { effect: "no_effect" });
|
|
388
|
+
this.assertCustody(context);
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
/** Historical aggregate execution proof. Selectors must come from an owning
|
|
392
|
+
* committed acceptance receipt, not a proposed result. This reader does not
|
|
393
|
+
* select current declarations, inspect the current workspace or execute checks.
|
|
394
|
+
* Relocation of SQL path fields still requires explicit receipt projection.
|
|
395
|
+
*/
|
|
396
|
+
export class PreparedRetainedCheckEvidence {
|
|
397
|
+
#input;
|
|
398
|
+
#snapshot;
|
|
399
|
+
#generation;
|
|
400
|
+
constructor(input, snapshot, generation) {
|
|
401
|
+
this.#input = structuredClone(input);
|
|
402
|
+
this.#snapshot = snapshot;
|
|
403
|
+
this.#generation = generation;
|
|
404
|
+
Object.freeze(this);
|
|
405
|
+
}
|
|
406
|
+
static prepare(context, input) {
|
|
407
|
+
const fail = () => { throw new AppError("retained_check_evidence_invalid", "Accepted checks lack exact historical execution proof", 2, { effect: "no_effect" }); };
|
|
408
|
+
if (!Array.isArray(input.references) || !input.references.length || input.references.length > 4096
|
|
409
|
+
|| new Set(input.references.map(reference => reference?.id)).size !== input.references.length
|
|
410
|
+
|| input.references.some(reference => !reference || typeof reference.id !== "string" || !reference.id
|
|
411
|
+
|| typeof reference.path !== "string" || !/^[a-f0-9]{64}$/.test(reference.sha256) || !/^[a-f0-9]{64}$/.test(reference.sql_sha256)))
|
|
412
|
+
fail();
|
|
413
|
+
const generation = assertRunMutationAllowed(context, input.projectId, input.runId), snapshot = retainedCheckSqlSnapshot(context, input);
|
|
414
|
+
const selected = JSON.parse(snapshot);
|
|
415
|
+
if (!selected.run || selected.receipts.some(row => !row))
|
|
416
|
+
fail();
|
|
417
|
+
const runRoot = selected.run.run_root ?? selected.run.run_dir;
|
|
418
|
+
if (!path.isAbsolute(runRoot) || fs.realpathSync.native(runRoot) !== path.resolve(runRoot))
|
|
419
|
+
fail();
|
|
420
|
+
for (let index = 0; index < input.references.length; index++) {
|
|
421
|
+
const reference = input.references[index], row = selected.receipts[index];
|
|
422
|
+
if (row.scope !== "aggregate" || row.work_id !== null || evidenceDigest(row) !== reference.sql_sha256
|
|
423
|
+
|| row.receipt_path !== path.join(runRoot, reference.path))
|
|
424
|
+
fail();
|
|
425
|
+
// Admit contained regular receipt/completion files before any helper
|
|
426
|
+
// reads them; escaping paths and symlink parents are rejected.
|
|
427
|
+
const bytes = readRetainedCheckFile(runRoot, reference.path);
|
|
428
|
+
readRetainedCheckFile(runRoot, path.posix.join(path.posix.dirname(reference.path), "completion.json"));
|
|
429
|
+
if (crypto.createHash("sha256").update(bytes).digest("hex") !== reference.sha256 || !checkReceiptEvidenceValidForMode(projectCheckReceiptRow(row), input.mode))
|
|
430
|
+
fail();
|
|
431
|
+
}
|
|
432
|
+
const prepared = new PreparedRetainedCheckEvidence(input, snapshot, generation);
|
|
433
|
+
prepared.assertCustody(context);
|
|
434
|
+
return prepared;
|
|
435
|
+
}
|
|
436
|
+
assertCustody(context) {
|
|
437
|
+
assertRunMutationAllowed(context, this.#input.projectId, this.#input.runId, this.#generation);
|
|
438
|
+
if (retainedCheckSqlSnapshot(context, this.#input) !== this.#snapshot)
|
|
439
|
+
throw new AppError("retained_check_evidence_invalid", "Accepted check SQL custody changed", 2, { effect: "no_effect" });
|
|
440
|
+
}
|
|
441
|
+
revalidate(context) {
|
|
442
|
+
const current = PreparedRetainedCheckEvidence.prepare(context, this.#input);
|
|
443
|
+
if (current.#snapshot !== this.#snapshot)
|
|
444
|
+
throw new AppError("retained_check_evidence_invalid", "Accepted check proof changed", 2, { effect: "no_effect" });
|
|
445
|
+
this.assertCustody(context);
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
function readRetainedCheckFile(root, relative) {
|
|
449
|
+
const fail = () => { throw new AppError("retained_check_evidence_invalid", "Accepted check file is not a contained regular file", 2, { effect: "no_effect" }); };
|
|
450
|
+
if (!relative || path.posix.isAbsolute(relative) || path.win32.isAbsolute(relative) || relative.includes("\\") || relative.includes("\0")
|
|
451
|
+
|| relative.split("/").some(segment => !segment || segment === "." || segment === ".."))
|
|
452
|
+
fail();
|
|
453
|
+
try {
|
|
454
|
+
let parent = root;
|
|
455
|
+
if (!fs.lstatSync(parent).isDirectory())
|
|
456
|
+
fail();
|
|
457
|
+
for (const segment of relative.split("/").slice(0, -1)) {
|
|
458
|
+
parent = path.join(parent, segment);
|
|
459
|
+
if (!fs.lstatSync(parent).isDirectory())
|
|
460
|
+
fail();
|
|
461
|
+
}
|
|
462
|
+
return readBoundedReleaseFile(path.join(root, relative), 16 * 1024 * 1024);
|
|
463
|
+
}
|
|
464
|
+
catch {
|
|
465
|
+
return fail();
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
function retainedCheckSqlSnapshot(context, input) {
|
|
469
|
+
return context.db.readSnapshot(() => JSON.stringify({
|
|
470
|
+
run: context.db.get("SELECT project_id,id,workspace_root,run_root,run_dir FROM runs WHERE project_id=? AND id=?", [input.projectId, input.runId]) ?? null,
|
|
471
|
+
receipts: input.references.map(reference => context.db.get("SELECT * FROM check_receipts WHERE project_id=? AND run_id=? AND id=?", [input.projectId, input.runId, reference.id]) ?? null)
|
|
472
|
+
}));
|
|
473
|
+
}
|
|
474
|
+
function checkEvidenceSqlSnapshot(context, input) {
|
|
475
|
+
return context.db.readSnapshot(() => JSON.stringify({
|
|
476
|
+
run: context.db.get("SELECT project_id,id,workspace_root,run_root,run_dir FROM runs WHERE project_id=? AND id=?", [input.projectId, input.runId]),
|
|
477
|
+
receipts: context.db.all("SELECT * FROM check_receipts WHERE project_id=? AND run_id=? ORDER BY reservation_sequence,rowid", [input.projectId, input.runId])
|
|
478
|
+
}));
|
|
479
|
+
}
|
|
80
480
|
export async function runCodeChecks(context, input) {
|
|
81
|
-
|
|
481
|
+
validateCheckEvidenceContext(input.evidenceContext);
|
|
482
|
+
const generation = assertRunMutationAllowed(context, input.projectId, input.runId);
|
|
483
|
+
let gate;
|
|
484
|
+
for (;;) {
|
|
485
|
+
assertRunMutationAllowed(context, input.projectId, input.runId, generation);
|
|
486
|
+
reconcileUnfinishedChecks(context, input);
|
|
487
|
+
reconcileWorkspaceOwnership(context, input.workspaceRoot);
|
|
488
|
+
try {
|
|
489
|
+
gate = acquireWorkspaceGate(context, input);
|
|
490
|
+
break;
|
|
491
|
+
}
|
|
492
|
+
catch (error) {
|
|
493
|
+
const sameRunGate = managedProcessStatus(context).find(item => item.kind === "workspace-gate" && item.operation_id === workspaceOperationId(input.workspaceRoot) && ["starting", "running", "stopping"].includes(item.state) && item.project_id === input.projectId && item.run_id === input.runId);
|
|
494
|
+
if (input.scope !== "work" || !input.workId || !(error instanceof AppError) || !["check_in_progress", "managed_operation_in_progress"].includes(error.code) || !sameRunGate)
|
|
495
|
+
throw error;
|
|
496
|
+
await new Promise(resolve => setTimeout(resolve, 500));
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
try {
|
|
500
|
+
return await runCodeChecksOwned(context, input, gate.id, generation);
|
|
501
|
+
}
|
|
502
|
+
finally {
|
|
503
|
+
finishManagedProcess(context, { id: gate.id, leaseToken: gate.lease_token, state: "stopped", reason: "workspace_gate_settled" });
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
async function runCodeChecksOwned(context, input, workspaceGateId, generation) {
|
|
507
|
+
assertRunMutationAllowed(context, input.projectId, input.runId, generation);
|
|
82
508
|
for (const check of input.checks)
|
|
83
509
|
validateCheckDeclaration(input.workspaceRoot, check);
|
|
84
|
-
const {
|
|
85
|
-
const
|
|
510
|
+
const { profile } = readCodeCheckProfile(input.workspaceRoot);
|
|
511
|
+
const profileHash = codeCheckProfileHash(input.workspaceRoot, input.checks);
|
|
512
|
+
if (input.expectedProfileHash !== undefined && input.expectedProfileHash !== profileHash)
|
|
513
|
+
throw new AppError("check_profile_drift", "The frozen check profile differs from the workspace profile", 1, { expected_profile_hash: input.expectedProfileHash, actual_profile_hash: profileHash });
|
|
514
|
+
const checks = input.checks.map(check => {
|
|
515
|
+
const ports = profile?.ports_by_alias?.[check.command], inputs = profile?.inputs_by_alias?.[check.command];
|
|
516
|
+
const runner = profile?.runner_evidence_by_alias?.[check.command] ?? check.runner_evidence;
|
|
517
|
+
if (runner && !runnerContractsValid({ "@check/declaration": runner }))
|
|
518
|
+
throw new AppError("invalid_code_check_profile", "Check runner evidence contract is invalid", 2, { check_id: check.id });
|
|
519
|
+
return { ...check, ...(check.ports || !ports ? {} : { ports }), ...(check.inputs || !inputs ? {} : { inputs }), ...(runner ? { runner_evidence: runner, required_artifacts: [...new Set([...(check.required_artifacts ?? []), runner.path])] } : {}) };
|
|
520
|
+
});
|
|
86
521
|
const resolved = resolveCodeCheckCommands(input.workspaceRoot, input.runId, checks.map((check) => check.command));
|
|
87
|
-
|
|
88
|
-
const receipts = [];
|
|
522
|
+
assertConsistentDeclarations(checks, resolved);
|
|
523
|
+
const groups = deduplicate(checks, resolved), receipts = [];
|
|
524
|
+
const retryIds = [...new Set([...(input.retryReceiptId ? [input.retryReceiptId] : []), ...(input.retryReceiptIds ?? [])])];
|
|
525
|
+
const retries = new Map();
|
|
526
|
+
for (const receiptId of retryIds) {
|
|
527
|
+
const predecessor = validateFailedCheckRetry(context, { projectId: input.projectId, runId: input.runId, receiptId, declarations: checks, reason: input.retryReason });
|
|
528
|
+
const snapshot = workspaceState(input.workspaceRoot, [input.runHome]);
|
|
529
|
+
const group = groups.find(candidate => candidate.declarations.some(check => check.run_at === predecessor.gate)
|
|
530
|
+
&& (checkInputHash(input.workspaceRoot, { ...candidate, gate: predecessor.gate }, profileHash, workspaceInputFingerprint(input.workspaceRoot, snapshot.files, candidate.inputs, [input.runHome]), input.evidenceContext, input.checkEnvironment) === predecessor.input_hash
|
|
531
|
+
|| sourceRepairContractMatches(predecessor, candidate, profileHash)));
|
|
532
|
+
if (!group || retries.has(group))
|
|
533
|
+
throw new AppError("retry_check_invalid", "Retry must match exactly one current execution contract", 2, { check_receipt_id: receiptId });
|
|
534
|
+
// Preserve the identity of a pre-deduplication failed execution when an
|
|
535
|
+
// upgraded RUN retries it; the new receipt still proves every grouped ref.
|
|
536
|
+
group.gate = predecessor.gate;
|
|
537
|
+
retries.set(group, receiptId);
|
|
538
|
+
}
|
|
89
539
|
for (let index = 0; index < groups.length; index += 1) {
|
|
90
540
|
const group = groups[index];
|
|
91
541
|
const before = workspaceState(input.workspaceRoot, [input.runHome]);
|
|
92
542
|
const startedAt = context.now();
|
|
93
|
-
const
|
|
543
|
+
const inputFingerprint = workspaceInputFingerprint(input.workspaceRoot, before.files, group.inputs, [input.runHome]);
|
|
544
|
+
const inputHash = checkInputHash(input.workspaceRoot, group, profileHash, inputFingerprint, input.evidenceContext, input.checkEnvironment);
|
|
94
545
|
const epoch = crypto.createHash("sha256").update(`${group.gate}\0${before.fingerprint}`).digest("hex");
|
|
95
|
-
const
|
|
546
|
+
const reservationInput = { ...input, retryReceiptIds: retries.has(group) ? [retries.get(group)] : [], generation, group, before, profileHash, inputHash, epoch, startedAt };
|
|
547
|
+
delete reservationInput.retryReceiptId;
|
|
548
|
+
const decision = reserveOrReuseReceipt(context, reservationInput);
|
|
549
|
+
if (decision.receipt) {
|
|
550
|
+
receipts.push(bindReceipt(decision.receipt, group));
|
|
551
|
+
input.progress?.(`check ${index + 1}/${groups.length} ${decision.receipt.status === "passed" ? "reused" : "requires repair"}: ${group.command}`);
|
|
552
|
+
continue;
|
|
553
|
+
}
|
|
554
|
+
const reserved = decision.reserved;
|
|
96
555
|
const { id, evidenceDir, stdoutPath, stderrPath, receiptPath, completionPath } = reserved;
|
|
97
556
|
let resources = {};
|
|
98
557
|
let result = { exitCode: null, error: "check did not start", aborted: true };
|
|
99
558
|
try {
|
|
100
559
|
fs.mkdirSync(evidenceDir, { recursive: true });
|
|
101
|
-
|
|
102
|
-
|
|
560
|
+
const startingReceipt = receiptFor({ ...reserved, resources, result, status: "running", after: before, mutationPaths: [], artifacts: [] });
|
|
561
|
+
if (group.runnerEvidence)
|
|
562
|
+
startingReceipt.runner_evidence_required = group.runnerEvidence;
|
|
563
|
+
writeReceipt(receiptPath, startingReceipt);
|
|
564
|
+
const budget = runtimeBudgetForRun(context, input.projectId, input.runId);
|
|
565
|
+
if (group.runnerEvidence) {
|
|
566
|
+
// Reject configuration before acquiring a check lease/ports, so there
|
|
567
|
+
// is no unconfirmed managed child to strand on a validation error.
|
|
568
|
+
runnerObservationDurations(process.env);
|
|
569
|
+
}
|
|
570
|
+
const managed = registerManagedProcess(context, { kind: "check", ownerPid: process.pid, ownerId: `check:${id}`, operationId: workspaceOperationId(input.workspaceRoot), projectId: input.projectId, runId: input.runId, workId: input.workId ?? null, checkId: id, stdoutPath, stderrPath, metadata: { command: group.command, role: "check", workspace_root: input.workspaceRoot, workspace_gate_id: workspaceGateId, budget } });
|
|
103
571
|
const allocation = await reservePorts(context, { ownerId: managed.owner_id, processId: managed.id, names: group.ports });
|
|
104
572
|
resources = allocation.ports;
|
|
105
|
-
|
|
573
|
+
try {
|
|
574
|
+
input.progress?.(`check ${index + 1}/${groups.length} preparing: ${group.command}`);
|
|
575
|
+
assertRunMutationAllowed(context, input.projectId, input.runId, generation);
|
|
576
|
+
}
|
|
577
|
+
catch (error) {
|
|
578
|
+
allocation.release();
|
|
579
|
+
finishManagedProcess(context, { id: managed.id, leaseToken: managed.lease_token, state: "failed", reason: "dispatch_fenced_before_spawn" });
|
|
580
|
+
throw error;
|
|
581
|
+
}
|
|
106
582
|
const stdout = fs.openSync(stdoutPath, "a");
|
|
107
583
|
const stderr = fs.openSync(stderrPath, "a");
|
|
584
|
+
let confirmedExpiry = null;
|
|
108
585
|
try {
|
|
109
|
-
|
|
586
|
+
const runnerEnvironment = group.runnerEvidence ? { DD_FLOW_CHECK_RUNNER_EVIDENCE_PATH: path.join(evidenceDir, group.runnerEvidence.path), DD_FLOW_CHECK_RUNNER_PROGRESS_PATH: path.join(evidenceDir, "runner-progress.jsonl"), DD_FLOW_CHECK_INVOCATION_ID: id, DD_FLOW_CHECK_ID: id, DD_FLOW_CHECK_INPUT_HASH: inputHash, DD_FLOW_CHECK_VERIFICATION_EPOCH: epoch } : {};
|
|
587
|
+
if (group.runnerEvidence)
|
|
588
|
+
fs.mkdirSync(path.dirname(path.join(evidenceDir, group.runnerEvidence.path)), { recursive: true });
|
|
589
|
+
result = await runCheck(group.command, input.workspaceRoot, { ...codeExecutionEnvironment(input.workspaceRoot, input.checkEnvironment), DD_FLOW_EVIDENCE_DIR: evidenceDir, DD_FLOW_CHECK_COMPLETION_FILE: completionPath, ...runnerEnvironment, ...portEnvironment(resources) }, stdout, stderr, (elapsed) => input.progress?.(`check ${index + 1}/${groups.length} still running (${elapsed}s): ${group.command}`), (pid) => {
|
|
590
|
+
confirmedExpiry = confirmManagedProcess(context, { id: managed.id, leaseToken: managed.lease_token, pid, processGroupId: process.platform === "win32" ? null : pid }).lease_expires_at;
|
|
591
|
+
assertRunMutationAllowed(context, input.projectId, input.runId, generation);
|
|
592
|
+
if (budget)
|
|
593
|
+
admitRuntimeCheck(context, managed.id, managed.lease_token);
|
|
594
|
+
}, attemptMs => renewManagedProcess({ ...context, resourceDatabaseMode: "prepared_existing", databaseDeadlineAt: Date.now() + attemptMs }, { id: managed.id, leaseToken: managed.lease_token }), { leaseExpiresAt: () => confirmedExpiry });
|
|
110
595
|
}
|
|
111
596
|
finally {
|
|
112
597
|
fs.closeSync(stdout);
|
|
@@ -116,7 +601,8 @@ export async function runCodeChecks(context, input) {
|
|
|
116
601
|
if (processTreeIsAlive(current))
|
|
117
602
|
throw new AppError("check_in_progress", "The check shell exited but its process tree is still live; retain this receipt and its resources until settlement", 2, { process_id: managed.id, check_receipt_id: id });
|
|
118
603
|
allocation.release();
|
|
119
|
-
|
|
604
|
+
if (!result.cleanupUnconfirmed)
|
|
605
|
+
finishManagedProcess(context, { id: managed.id, leaseToken: managed.lease_token, state: result.aborted || result.exitCode !== 0 || Boolean(result.error) ? "failed" : "stopped", reason: result.error });
|
|
120
606
|
}
|
|
121
607
|
catch (error) {
|
|
122
608
|
if (error instanceof AppError && error.code === "check_in_progress")
|
|
@@ -125,7 +611,7 @@ export async function runCodeChecks(context, input) {
|
|
|
125
611
|
}
|
|
126
612
|
const after = workspaceState(input.workspaceRoot, [input.runHome]);
|
|
127
613
|
const mutationPaths = changedPaths(before.files, after.files);
|
|
128
|
-
|
|
614
|
+
let artifacts = collectCheckArtifacts(evidenceDir, group.requiredArtifacts);
|
|
129
615
|
if (fs.existsSync(stderrPath)) {
|
|
130
616
|
if (!artifacts.complete)
|
|
131
617
|
fs.appendFileSync(stderrPath, `\nmissing required evidence artifacts: ${artifacts.missing.join(", ")}\n`);
|
|
@@ -134,13 +620,42 @@ export async function runCodeChecks(context, input) {
|
|
|
134
620
|
if (mutationPaths.length)
|
|
135
621
|
fs.appendFileSync(stderrPath, `\ncheck mutated project workspace: ${mutationPaths.join(", ")}\n`);
|
|
136
622
|
}
|
|
137
|
-
const
|
|
623
|
+
const runnerEvidence = group.runnerEvidence ? readRunnerEvidence(evidenceDir, group.runnerEvidence, { invocation_id: id, check_id: id, command: group.command, input_hash: inputHash, verification_epoch: epoch }) : undefined;
|
|
624
|
+
const physicalFailures = executionFailures({ exitCode: result.exitCode, launcherError: result.error ?? undefined, cleanupUnconfirmed: result.cleanupUnconfirmed, missing: artifacts.missing, mutations: mutationPaths, invalidRunner: Boolean(group.runnerEvidence && !runnerEvidence) });
|
|
625
|
+
if (result.failure)
|
|
626
|
+
physicalFailures.push(result.failure);
|
|
627
|
+
const candidateFiles = candidateFailureFiles(runnerEvidence, input.workspaceRoot);
|
|
628
|
+
if (group.runnerEvidence)
|
|
629
|
+
retainExecutionOutcome(evidenceDir, { id, command: group.command, input_hash: inputHash, verification_epoch: epoch }, input.workspaceRoot, physicalFailures, candidateFiles);
|
|
630
|
+
const requiredArtifacts = [...new Set([...group.requiredArtifacts, ...runnerProofArtifacts(runnerEvidence), ...(group.runnerEvidence ? ["engine-outcome.json"] : [])])];
|
|
631
|
+
artifacts = collectCheckArtifacts(evidenceDir, requiredArtifacts);
|
|
632
|
+
const status = result.aborted ? "aborted" : result.exitCode === 0 && !result.error && artifacts.complete && mutationPaths.length === 0 && (!group.runnerEvidence || runnerEvidencePass(runnerEvidence)) ? "passed" : "failed";
|
|
138
633
|
const receipt = receiptFor({ ...reserved, resources, result, status, after, mutationPaths, artifacts: artifacts.items });
|
|
634
|
+
receipt.required_artifacts = requiredArtifacts;
|
|
635
|
+
receipt.candidate_root = input.workspaceRoot;
|
|
636
|
+
receipt.candidate_files = candidateFiles;
|
|
637
|
+
receipt.execution_failures = physicalFailures;
|
|
638
|
+
if (group.runnerEvidence) {
|
|
639
|
+
receipt.runner_evidence_required = group.runnerEvidence;
|
|
640
|
+
receipt.runner_evidence = runnerEvidence ?? null;
|
|
641
|
+
}
|
|
139
642
|
context.db.run("UPDATE check_receipts SET status = ?, exit_code = ?, after_fingerprint = ?, mutation_paths_json = ?, artifacts_json = ?, finished_at = ? WHERE id = ? AND project_id = ? AND status = 'running'", [receipt.status, receipt.exit_code, receipt.after_fingerprint, JSON.stringify(receipt.mutation_paths), JSON.stringify(receipt.artifacts), receipt.finished_at, id, input.projectId]);
|
|
140
643
|
writeReceipt(receiptPath, receipt);
|
|
644
|
+
if (status === "failed")
|
|
645
|
+
receipt.diagnostic_evidence_valid = checkReceiptEvidenceValidForMode(receipt, "diagnostic");
|
|
141
646
|
receipts.push(receipt);
|
|
142
647
|
input.progress?.(`check ${index + 1}/${groups.length} ${status}: ${group.command}`);
|
|
143
648
|
}
|
|
649
|
+
if (receipts.every(receipt => receipt.status === "passed")) {
|
|
650
|
+
const current = workspaceState(input.workspaceRoot, [input.runHome]), currentProfileHash = codeCheckProfileHash(input.workspaceRoot, input.checks);
|
|
651
|
+
for (const receipt of receipts) {
|
|
652
|
+
const group = groups.find(candidate => candidate.declarations.some(check => receipt.check_refs.includes(checkRef(check))));
|
|
653
|
+
const currentHash = group ? checkInputHash(input.workspaceRoot, group, currentProfileHash, workspaceInputFingerprint(input.workspaceRoot, current.files, group.inputs, [input.runHome]), input.evidenceContext, input.checkEnvironment) : null;
|
|
654
|
+
if (!group || !checkReceiptEvidenceValid(receipt) || receipt.input_hash !== currentHash)
|
|
655
|
+
throw new AppError("check_evidence_stale", "Check evidence changed before the gate could be accepted", 1, { check_receipt_id: receipt.id });
|
|
656
|
+
}
|
|
657
|
+
assertRunMutationAllowed(context, input.projectId, input.runId, generation);
|
|
658
|
+
}
|
|
144
659
|
return receipts;
|
|
145
660
|
}
|
|
146
661
|
/**
|
|
@@ -149,62 +664,143 @@ export async function runCodeChecks(context, input) {
|
|
|
149
664
|
* a dead/terminal process becomes an explicit aborted receipt that can retry.
|
|
150
665
|
*/
|
|
151
666
|
export function reconcileUnfinishedChecks(context, input) {
|
|
152
|
-
|
|
667
|
+
let where = input.workId ? "project_id = ? AND run_id = ? AND work_id = ? AND status = 'running'" : "project_id = ? AND run_id = ? AND status = 'running'";
|
|
153
668
|
const params = input.workId ? [input.projectId, input.runId, input.workId] : [input.projectId, input.runId];
|
|
669
|
+
if (input.receiptId) {
|
|
670
|
+
where += " AND id = ?";
|
|
671
|
+
params.push(input.receiptId);
|
|
672
|
+
}
|
|
154
673
|
const pending = context.db.all(`SELECT id, receipt_path, started_at FROM check_receipts WHERE ${where}`, params);
|
|
155
674
|
if (pending.length === 0)
|
|
156
675
|
return [];
|
|
157
676
|
const processes = managedProcessStatus(context);
|
|
158
677
|
const reconciled = [];
|
|
159
678
|
for (const pendingReceipt of pending) {
|
|
160
|
-
const process = processes.filter((item) => item.check_id === pendingReceipt.id).at(-1);
|
|
679
|
+
const process = processes.filter((item) => item.check_id === pendingReceipt.id && item.project_id === input.projectId && item.run_id === input.runId).at(-1);
|
|
161
680
|
const receipt = readReceipt(pendingReceipt.receipt_path);
|
|
162
681
|
const completion = receipt ? readCompletion(receipt.completion_path || path.join(path.dirname(pendingReceipt.receipt_path), "completion.json")) : null;
|
|
163
682
|
// A stale lease after host sleep is not permission to duplicate a living
|
|
164
683
|
// check. Its durable completion marker or explicit reconciliation decides
|
|
165
684
|
// the outcome before another invocation may start.
|
|
166
|
-
|
|
685
|
+
// A confirmed PID plus its birth time is sufficient to prove that this
|
|
686
|
+
// exact check writer is gone. Do not retain a dead receipt merely because
|
|
687
|
+
// older registrations lack the optional parent-owner identity.
|
|
688
|
+
const processProvenDead = Boolean(process?.pid && process.pid_started_at && !processTreeIsAlive(process));
|
|
689
|
+
if (process && !processProvenDead && (processTreeIsAlive(process) || (["starting", "running", "stopping", "orphaned"].includes(process.state) && managedOwnerIsAlive(process) !== false))) {
|
|
167
690
|
throw new AppError("check_in_progress", "A previous check invocation is still running", 1, { check_receipt_id: pendingReceipt.id, process_id: process.id, pid: process.pid });
|
|
168
691
|
}
|
|
169
692
|
if (!process && !completion && Date.now() - Date.parse(pendingReceipt.started_at) < receiptStartingGraceMs) {
|
|
170
693
|
throw new AppError("check_in_progress", "A previous check invocation is still initializing", 1, { check_receipt_id: pendingReceipt.id, initializing: true });
|
|
171
694
|
}
|
|
172
695
|
const after = workspaceState(input.workspaceRoot, input.runHome ? [input.runHome] : []);
|
|
173
|
-
|
|
696
|
+
let artifacts = receipt ? collectCheckArtifacts(path.join(path.dirname(receipt.completion_path || pendingReceipt.receipt_path), "artifacts"), receipt.required_artifacts ?? []) : { complete: false, missing: [], items: [] };
|
|
174
697
|
const workspaceUnchanged = receipt?.before_fingerprint === after.fingerprint;
|
|
175
|
-
const
|
|
698
|
+
const runnerEvidence = receipt?.runner_evidence_required ? readRunnerEvidence(path.join(path.dirname(pendingReceipt.receipt_path), "artifacts"), receipt.runner_evidence_required, { invocation_id: receipt.id, check_id: receipt.id, command: receipt.command, input_hash: receipt.input_hash, verification_epoch: receipt.verification_epoch }) : undefined;
|
|
176
699
|
const exitCode = completion?.exit_code ?? null;
|
|
177
700
|
const finishedAt = completion?.finished_at ?? context.now();
|
|
701
|
+
const physicalFailures = executionFailures({ exitCode, missing: artifacts.missing.filter(file => file !== "engine-outcome.json"), mutations: workspaceUnchanged ? [] : ["<workspace changed while detached check ran>"], invalidRunner: Boolean(receipt?.runner_evidence_required && !runnerEvidence) });
|
|
702
|
+
let candidateFiles = candidateFailureFiles(runnerEvidence, input.workspaceRoot);
|
|
703
|
+
if (receipt?.runner_evidence_required) {
|
|
704
|
+
const evidenceDir = path.join(path.dirname(pendingReceipt.receipt_path), "artifacts");
|
|
705
|
+
if (fs.existsSync(path.join(evidenceDir, "engine-outcome.json"))) {
|
|
706
|
+
// A completed live writer's bound proof is already authoritative. Never erase its blockers during reconciliation.
|
|
707
|
+
const retained = context.db.get("SELECT artifacts_json FROM check_receipts WHERE id = ?", [pendingReceipt.id]);
|
|
708
|
+
const proven = resolvedReceiptArtifacts(pendingReceipt.receipt_path, retained ? JSON.parse(retained.artifacts_json) : [], receipt);
|
|
709
|
+
if (proven.candidate_root) {
|
|
710
|
+
physicalFailures.push(...(proven.execution_failures ?? []));
|
|
711
|
+
candidateFiles = proven.candidate_files ?? candidateFiles;
|
|
712
|
+
}
|
|
713
|
+
else {
|
|
714
|
+
physicalFailures.push({ code: "engine_outcome_unbound", message: "Existing execution outcome is not bound to retained inventory; prior launcher/cleanup outcome is unknown" });
|
|
715
|
+
fs.renameSync(path.join(evidenceDir, "engine-outcome.json"), path.join(evidenceDir, `engine-outcome-unconfirmed-${crypto.randomUUID()}.json`));
|
|
716
|
+
}
|
|
717
|
+
}
|
|
718
|
+
retainExecutionOutcome(evidenceDir, receipt, input.workspaceRoot, physicalFailures, candidateFiles);
|
|
719
|
+
receipt.required_artifacts = [...new Set([...(receipt.required_artifacts ?? []), ...runnerProofArtifacts(runnerEvidence), "engine-outcome.json"])];
|
|
720
|
+
artifacts = collectCheckArtifacts(evidenceDir, receipt.required_artifacts);
|
|
721
|
+
}
|
|
722
|
+
const status = completion === null ? "aborted" : completion.exit_code === 0 && workspaceUnchanged && artifacts.complete && physicalFailures.length === 0 && (!receipt?.runner_evidence_required || runnerEvidencePass(runnerEvidence)) ? "passed" : "failed";
|
|
178
723
|
context.db.run("UPDATE check_receipts SET status = ?, exit_code = ?, after_fingerprint = ?, mutation_paths_json = ?, artifacts_json = ?, finished_at = ? WHERE id = ? AND project_id = ? AND status = 'running'", [status, exitCode, after.fingerprint, JSON.stringify(workspaceUnchanged ? [] : ["<workspace changed while detached check ran>"]), JSON.stringify(artifacts.items), finishedAt, pendingReceipt.id, input.projectId]);
|
|
179
724
|
if (process)
|
|
180
725
|
finishManagedProcess(context, { id: process.id, leaseToken: process.lease_token, state: status === "passed" ? "stopped" : "failed", reason: status === "passed" ? "reconciled_completion" : completion === null ? "completion_marker_missing" : "reconciled_check_failed" });
|
|
181
726
|
if (receipt) {
|
|
182
|
-
const repaired = { ...receipt, completion_path: receipt.completion_path || path.join(path.dirname(pendingReceipt.receipt_path), "completion.json"), required_artifacts: receipt.required_artifacts ?? [], status, exit_code: exitCode, after_fingerprint: after.fingerprint, mutation_paths: workspaceUnchanged ? [] : ["<workspace changed while detached check ran>"], artifacts: artifacts.items, finished_at: finishedAt };
|
|
727
|
+
const repaired = { ...receipt, ...(receipt.runner_evidence_required ? { runner_evidence: runnerEvidence ?? null } : {}), completion_path: receipt.completion_path || path.join(path.dirname(pendingReceipt.receipt_path), "completion.json"), required_artifacts: receipt.required_artifacts ?? [], ...(status === "aborted" ? { abort_reason: "completion_marker_missing" } : {}), status, exit_code: exitCode, after_fingerprint: after.fingerprint, mutation_paths: workspaceUnchanged ? [] : ["<workspace changed while detached check ran>"], artifacts: artifacts.items, finished_at: finishedAt };
|
|
728
|
+
repaired.candidate_root = input.workspaceRoot;
|
|
729
|
+
repaired.candidate_files = candidateFiles;
|
|
730
|
+
repaired.execution_failures = physicalFailures;
|
|
183
731
|
writeReceipt(pendingReceipt.receipt_path, repaired);
|
|
732
|
+
if (status === "failed")
|
|
733
|
+
repaired.diagnostic_evidence_valid = checkReceiptEvidenceValidForMode(repaired, "diagnostic");
|
|
184
734
|
reconciled.push(repaired);
|
|
185
735
|
}
|
|
186
736
|
}
|
|
187
737
|
return reconciled;
|
|
188
738
|
}
|
|
189
|
-
function
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
739
|
+
function reserveOrReuseReceipt(context, input) {
|
|
740
|
+
// Receipt files and artifact hashes are prepared before reserving SQLite.
|
|
741
|
+
// The compact SQL snapshot below fences races with another check settlement.
|
|
742
|
+
const snapshotSql = "SELECT id, status, exit_code, input_hash, profile_hash, command, gate, retry_of_receipt_id, receipt_path FROM check_receipts WHERE project_id = ? AND run_id = ? ORDER BY reservation_sequence, rowid";
|
|
743
|
+
const snapshot = JSON.stringify(context.db.all(snapshotSql, [input.projectId, input.runId]));
|
|
744
|
+
const receipts = checkReceipts(context, { projectId: input.projectId, runId: input.runId });
|
|
745
|
+
if (JSON.stringify(context.db.all(snapshotSql, [input.projectId, input.runId])) !== snapshot)
|
|
746
|
+
throw new AppError("check_receipt_changed", "Check receipts changed during evidence preparation", 1);
|
|
747
|
+
const prior = receipts.filter((receipt) => receipt.input_hash === input.inputHash && receipt.profile_hash === input.profileHash).at(-1) ?? null;
|
|
748
|
+
const reusablePrior = prior?.status === "passed" && checkReceiptEvidenceValid(prior);
|
|
749
|
+
const retryIds = new Set([...(input.retryReceiptId ? [input.retryReceiptId] : []), ...(input.retryReceiptIds ?? [])]);
|
|
750
|
+
const successor = retryIds.size === 1 ? receipts.find((receipt) => receipt.retry_of_receipt_id === [...retryIds][0] && receipt.input_hash === input.inputHash && receipt.profile_hash === input.profileHash) : null;
|
|
751
|
+
const reusableSuccessor = successor?.status === "passed" && checkReceiptEvidenceValid(successor);
|
|
752
|
+
const retryPredecessor = receipts.find(receipt => retryIds.has(receipt.id));
|
|
753
|
+
const sourceCorrection = Boolean(retryPredecessor && sourceRepairContractMatches(retryPredecessor, input.group, input.profileHash));
|
|
754
|
+
const latestContract = receipts.filter(receipt => receipt.command === input.group.command && receipt.profile_hash === input.profileHash && receipt.gate === input.group.gate && receipt.scope === input.scope && receipt.work_id === (input.workId ?? null)).at(-1);
|
|
755
|
+
return context.db.writeTransaction(() => {
|
|
756
|
+
assertRunMutationAllowed(context, input.projectId, input.runId, input.generation);
|
|
757
|
+
if (JSON.stringify(context.db.all(snapshotSql, [input.projectId, input.runId])) !== snapshot)
|
|
758
|
+
throw new AppError("check_receipt_changed", "Check receipts changed before reservation", 1);
|
|
759
|
+
if (prior?.status === "running")
|
|
760
|
+
throw new AppError("check_in_progress", "A matching check invocation is still running", 1, { check_receipt_id: prior.id });
|
|
761
|
+
if (retryIds.size > 1)
|
|
762
|
+
throw new AppError("retry_check_invalid", "A check execution accepts exactly one retry predecessor", 2);
|
|
763
|
+
const retryId = [...retryIds][0];
|
|
764
|
+
if (retryId) {
|
|
765
|
+
if (!input.retryReason?.trim())
|
|
766
|
+
throw new AppError("retry_check_invalid", "A check retry requires the restored-condition reason", 2, { check_receipt_id: retryId });
|
|
767
|
+
const predecessor = receipts.find(receipt => receipt.id === retryId);
|
|
768
|
+
if (!predecessor || !["failed", "aborted"].includes(predecessor.status) || predecessor.input_hash !== input.inputHash && !sourceCorrection || predecessor.profile_hash !== input.profileHash || predecessor.command !== input.group.command || predecessor.gate !== input.group.gate)
|
|
769
|
+
throw new AppError("retry_check_invalid", "Retry predecessor does not match this exact check contract", 2, { check_receipt_id: retryId });
|
|
770
|
+
if ((sourceCorrection ? latestContract?.id : prior?.id) !== (successor?.id ?? predecessor.id))
|
|
771
|
+
throw new AppError("retry_check_invalid", "This retry was superseded by a newer check outcome; inspect the latest receipt", 2, { check_receipt_id: retryId, latest_check_receipt_id: prior?.id ?? null, latest_status: prior?.status ?? null });
|
|
772
|
+
if (successor?.status === "running")
|
|
773
|
+
throw new AppError("check_in_progress", "The requested retry is already running", 1, { check_receipt_id: successor.id, retry_of_receipt_id: retryId });
|
|
774
|
+
if (successor) {
|
|
775
|
+
if (reusableSuccessor)
|
|
776
|
+
return { receipt: successor };
|
|
777
|
+
throw new AppError("retry_check_invalid", "The requested retry already has a terminal outcome; choose its failed receipt for another retry", 2, { check_receipt_id: successor.id, retry_of_receipt_id: retryId, status: successor.status });
|
|
778
|
+
}
|
|
779
|
+
}
|
|
780
|
+
// An unchanged failed contract needs a concrete restored-condition retry,
|
|
781
|
+
// not repeated coordinator finish until it happens to become green.
|
|
782
|
+
if (prior && ["failed", "aborted"].includes(prior.status) && !retryId)
|
|
783
|
+
return { receipt: prior };
|
|
784
|
+
if (prior?.status === "passed" && !retryIds.has(prior.id) && input.group.reusable && reusablePrior)
|
|
785
|
+
return { receipt: prior };
|
|
786
|
+
let ordinal = (context.db.get("SELECT COALESCE(MAX(reservation_sequence), 0) + 1 AS sequence FROM check_receipts WHERE project_id = ? AND run_id = ?", [input.projectId, input.runId])?.sequence ?? 1);
|
|
787
|
+
let localId = `RCP-${String(ordinal).padStart(3, "0")}`, id = `${input.workId ?? input.runId}/${localId}`;
|
|
788
|
+
while (context.db.get("SELECT 1 FROM check_receipts WHERE id = ?", [id])) {
|
|
789
|
+
ordinal += 1;
|
|
790
|
+
localId = `RCP-${String(ordinal).padStart(3, "0")}`;
|
|
791
|
+
id = `${input.workId ?? input.runId}/${localId}`;
|
|
792
|
+
}
|
|
196
793
|
const directory = path.join(input.runHome, input.artifactDir ?? "05-code", "checks", localId);
|
|
197
|
-
const reserved = { id, localId, directory, evidenceDir: path.join(directory, "artifacts"), stdoutPath: path.join(directory, "stdout.log"), stderrPath: path.join(directory, "stderr.log"), receiptPath: path.join(directory, "receipt.json"), completionPath: path.join(directory, "completion.json"), group: input.group, before: input.before, profileHash: input.profileHash, inputHash: input.inputHash, epoch: input.epoch, startedAt: input.startedAt, scope: input.scope, ...(input.workId ? { workId: input.workId } : {}), projectId: input.projectId, runId: input.runId };
|
|
198
|
-
context.db.run("INSERT INTO check_receipts (id, project_id, run_id, work_id, scope, declaration_id, check_refs_json, gate, command, input_hash, verification_epoch, status, exit_code, stdout_path, stderr_path, receipt_path, workspace_fingerprint, before_fingerprint, after_fingerprint, profile_hash, mutation_paths_json, artifacts_json, started_at, finished_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 'running', NULL, ?, ?, ?, ?, ?, ?, ?, '[]', '[]', ?, ?)", [id, input.projectId, input.runId, input.workId ?? null, input.scope, input.group.declarations[0].id, JSON.stringify(input.group.declarations.map(checkRef)), input.group.gate, input.group.command, input.inputHash, input.epoch, reserved.stdoutPath, reserved.stderrPath, reserved.receiptPath, input.before.fingerprint, input.before.fingerprint, input.before.fingerprint, input.profileHash, input.startedAt, input.startedAt]);
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
}
|
|
794
|
+
const reserved = { id, localId, directory, evidenceDir: path.join(directory, "artifacts"), stdoutPath: path.join(directory, "stdout.log"), stderrPath: path.join(directory, "stderr.log"), receiptPath: path.join(directory, "receipt.json"), completionPath: path.join(directory, "completion.json"), group: input.group, before: input.before, profileHash: input.profileHash, inputHash: input.inputHash, epoch: input.epoch, startedAt: input.startedAt, scope: input.scope, ...(retryId ? { retryOf: retryId, retryReason: input.retryReason } : {}), ...(input.workId ? { workId: input.workId } : {}), projectId: input.projectId, runId: input.runId };
|
|
795
|
+
context.db.run("INSERT INTO check_receipts (id, project_id, run_id, work_id, scope, declaration_id, check_refs_json, gate, command, input_hash, verification_epoch, reservation_sequence, retry_of_receipt_id, retry_reason, status, exit_code, stdout_path, stderr_path, receipt_path, workspace_fingerprint, before_fingerprint, after_fingerprint, profile_hash, mutation_paths_json, artifacts_json, started_at, finished_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 'running', NULL, ?, ?, ?, ?, ?, ?, ?, '[]', '[]', ?, ?)", [id, input.projectId, input.runId, input.workId ?? null, input.scope, input.group.declarations[0].id, JSON.stringify(input.group.declarations.map(checkRef).sort()), input.group.gate, input.group.command, input.inputHash, input.epoch, ordinal, reserved.retryOf ?? null, reserved.retryReason ?? null, reserved.stdoutPath, reserved.stderrPath, reserved.receiptPath, input.before.fingerprint, input.before.fingerprint, input.before.fingerprint, input.profileHash, input.startedAt, input.startedAt]);
|
|
796
|
+
return { reserved };
|
|
797
|
+
}, "checks.reserveOrReuseReceipt");
|
|
798
|
+
}
|
|
799
|
+
function sourceRepairContractMatches(receipt, group, profileHash) {
|
|
800
|
+
return checkRepairDisposition(receipt) === "source_repair" && group.command === receipt.command && profileHash === receipt.profile_hash && isDeepStrictEqual(group.inputs, receipt.inputs)
|
|
801
|
+
&& isDeepStrictEqual(derivedReceiptArtifacts(group, receipt).sort(), [...receipt.required_artifacts].sort()) && isDeepStrictEqual([...group.ports].sort(), Object.keys(receipt.resources.ports).sort());
|
|
206
802
|
}
|
|
207
|
-
function receiptFor(input) { return { id: input.id, local_id: input.localId, declaration_id: input.group.declarations[0].id, check_refs: input.group.declarations.map(checkRef), gate: input.group.gate, scope: input.scope, work_id: input.workId ?? null, command: input.group.command, input_hash: input.inputHash, verification_epoch: input.epoch, status: input.status, exit_code: input.status === "running" ? null : input.result.exitCode, stdout_path: input.stdoutPath, stderr_path: input.stderrPath, receipt_path: input.receiptPath, completion_path: input.completionPath, required_artifacts: input.group.requiredArtifacts, workspace_fingerprint: input.before.fingerprint, before_fingerprint: input.before.fingerprint, after_fingerprint: input.after.fingerprint, profile_hash: input.profileHash, mutation_paths: input.mutationPaths, artifacts: input.artifacts, resources: { ports: input.resources }, started_at: input.startedAt, finished_at: input.status === "running" ? input.startedAt : new Date().toISOString() }; }
|
|
803
|
+
function receiptFor(input) { return { id: input.id, local_id: input.localId, declaration_id: input.group.declarations[0].id, check_refs: input.group.declarations.map(checkRef), gate: input.group.gate, scope: input.scope, work_id: input.workId ?? null, command: input.group.command, input_hash: input.inputHash, verification_epoch: input.epoch, retry_of_receipt_id: input.retryOf ?? null, retry_reason: input.retryReason ?? null, status: input.status, ...(input.status === "aborted" ? { abort_reason: input.result.error } : {}), exit_code: input.status === "running" ? null : input.result.exitCode, stdout_path: input.stdoutPath, stderr_path: input.stderrPath, receipt_path: input.receiptPath, completion_path: input.completionPath, required_artifacts: input.group.requiredArtifacts, inputs: input.group.inputs, workspace_fingerprint: input.before.fingerprint, before_fingerprint: input.before.fingerprint, after_fingerprint: input.after.fingerprint, profile_hash: input.profileHash, mutation_paths: input.mutationPaths, artifacts: input.artifacts, resources: { ports: input.resources }, started_at: input.startedAt, finished_at: input.status === "running" ? input.startedAt : new Date().toISOString() }; }
|
|
208
804
|
function writeReceipt(file, receipt) { const temporary = `${file}.tmp-${process.pid}-${crypto.randomUUID()}`; fs.writeFileSync(temporary, `${JSON.stringify(receipt, null, 2)}\n`); fs.renameSync(temporary, file); }
|
|
209
805
|
function readReceipt(file) { try {
|
|
210
806
|
return JSON.parse(fs.readFileSync(file, "utf8"));
|
|
@@ -220,30 +816,366 @@ function readCompletion(file) { try {
|
|
|
220
816
|
catch {
|
|
221
817
|
return null;
|
|
222
818
|
} }
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
819
|
+
/** Validate an explicit retry; the caller still executes a fresh real gate and retains a new receipt. */
|
|
820
|
+
export function validateFailedCheckRetry(context, input) {
|
|
821
|
+
if (!input.reason?.trim())
|
|
822
|
+
throw new AppError("retry_check_invalid", "--retry-check requires --reason describing the restored environment", 2);
|
|
823
|
+
const receipt = checkReceipts(context, { projectId: input.projectId, runId: input.runId }).find((item) => item.id === input.receiptId);
|
|
824
|
+
if (!receipt || !["failed", "aborted"].includes(receipt.status))
|
|
825
|
+
throw new AppError("retry_check_invalid", "--retry-check must name a failed or aborted receipt in this RUN", 2, { check_receipt_id: input.receiptId });
|
|
826
|
+
const declared = new Set(input.declarations.flatMap((check) => [check.id, checkRef(check)]));
|
|
827
|
+
if (!receipt.check_refs.some((ref) => declared.has(ref)))
|
|
828
|
+
throw new AppError("retry_check_invalid", "--retry-check does not belong to this stage gate", 2, { check_receipt_id: input.receiptId, check_refs: receipt.check_refs });
|
|
829
|
+
return receipt;
|
|
830
|
+
}
|
|
226
831
|
export function checkReceipts(context, input) {
|
|
227
832
|
const where = input.workId ? "project_id = ? AND run_id = ? AND work_id = ?" : "project_id = ? AND run_id = ?";
|
|
228
833
|
const params = input.workId ? [input.projectId, input.runId, input.workId] : [input.projectId, input.runId];
|
|
229
|
-
return context.db.all(`SELECT id, declaration_id, check_refs_json, gate, scope, work_id, command, input_hash, verification_epoch, status, exit_code, stdout_path, stderr_path, receipt_path, workspace_fingerprint, before_fingerprint, after_fingerprint, profile_hash, mutation_paths_json, artifacts_json, started_at, finished_at FROM check_receipts WHERE ${where} ORDER BY
|
|
834
|
+
return context.db.all(`SELECT id, declaration_id, check_refs_json, gate, scope, work_id, command, input_hash, verification_epoch, retry_of_receipt_id, retry_reason, status, exit_code, stdout_path, stderr_path, receipt_path, workspace_fingerprint, before_fingerprint, after_fingerprint, profile_hash, mutation_paths_json, artifacts_json, started_at, finished_at FROM check_receipts WHERE ${where} ORDER BY reservation_sequence, rowid`, params).map(projectCheckReceiptRow);
|
|
835
|
+
}
|
|
836
|
+
function projectCheckReceiptRow(row) {
|
|
837
|
+
const receipt = { ...row, local_id: row.id.slice(row.id.lastIndexOf("/") + 1), check_refs: parseArray(row.check_refs_json, [row.declaration_id]), mutation_paths: parseArray(row.mutation_paths_json, []), resources: readReceiptResources(row.receipt_path), ...resolvedReceiptArtifacts(row.receipt_path, JSON.parse(row.artifacts_json), row) };
|
|
838
|
+
if (receipt.status === "failed")
|
|
839
|
+
receipt.diagnostic_evidence_valid = checkReceiptEvidenceValidForMode(receipt, "diagnostic");
|
|
840
|
+
return receipt;
|
|
841
|
+
}
|
|
842
|
+
/** File bytes remain immutable; only the reader projects their current location. */
|
|
843
|
+
function resolvedReceiptArtifacts(receiptPath, artifacts, binding) {
|
|
844
|
+
let original = {};
|
|
845
|
+
try {
|
|
846
|
+
const value = JSON.parse(fs.readFileSync(receiptPath, "utf8"));
|
|
847
|
+
if (value && typeof value === "object" && !Array.isArray(value))
|
|
848
|
+
original = value;
|
|
849
|
+
}
|
|
850
|
+
catch { /* Historical missing receipts remain visibly incomplete. */ }
|
|
851
|
+
const currentRoot = path.dirname(receiptPath);
|
|
852
|
+
const oldRoot = typeof original.receipt_path === "string" ? path.dirname(original.receipt_path) : currentRoot;
|
|
853
|
+
const relocate = (file) => {
|
|
854
|
+
const relative = path.relative(oldRoot, file);
|
|
855
|
+
return !path.isAbsolute(relative) && relative !== ".." && !relative.startsWith(`..${path.sep}`) ? path.join(currentRoot, relative) : file;
|
|
856
|
+
};
|
|
857
|
+
let evidence = null;
|
|
858
|
+
if (original.runner_evidence_required && original.id && original.command && original.input_hash && original.verification_epoch) {
|
|
859
|
+
const root = path.join(currentRoot, "artifacts");
|
|
860
|
+
const retained = collectCheckArtifacts(root, [original.runner_evidence_required.path]);
|
|
861
|
+
if (retained.complete && retained.items.every(item => artifacts.some(artifact => relocate(artifact.path) === item.path && artifact.sha256 === item.sha256)))
|
|
862
|
+
evidence = readRunnerEvidence(root, original.runner_evidence_required, { invocation_id: binding.id, check_id: binding.id, command: binding.command, input_hash: binding.input_hash, verification_epoch: binding.verification_epoch });
|
|
863
|
+
}
|
|
864
|
+
let outcome = {};
|
|
865
|
+
const outcomePath = path.join(currentRoot, "artifacts", "engine-outcome.json");
|
|
866
|
+
const retainedOutcome = collectCheckArtifacts(path.join(currentRoot, "artifacts"), ["engine-outcome.json"]);
|
|
867
|
+
if (retainedOutcome.complete && retainedOutcome.items.every(item => artifacts.some(artifact => relocate(artifact.path) === item.path && artifact.sha256 === item.sha256))) {
|
|
868
|
+
try {
|
|
869
|
+
const value = JSON.parse(fs.readFileSync(outcomePath, "utf8"));
|
|
870
|
+
if (value.schema_id === "dd-flow/check-execution-outcome@1" && value.id === binding.id && value.command === binding.command && value.input_hash === binding.input_hash && value.verification_epoch === binding.verification_epoch && typeof value.candidate_root === "string" && path.isAbsolute(value.candidate_root) && (value.candidate_files === undefined || Array.isArray(value.candidate_files) && value.candidate_files.length <= 64 && value.candidate_files.every((file) => typeof file === "string" && path.isAbsolute(file) && Buffer.byteLength(file) <= 4096)) && Array.isArray(value.execution_failures) && value.execution_failures.every((failure) => failure && typeof failure === "object" && typeof failure.code === "string" && typeof failure.message === "string"))
|
|
871
|
+
outcome = value;
|
|
872
|
+
}
|
|
873
|
+
catch { /* Missing physical proof cannot grant attribution. */ }
|
|
874
|
+
}
|
|
875
|
+
return { completion_path: path.join(currentRoot, "completion.json"), required_artifacts: original.required_artifacts ?? [], inputs: original.inputs ?? [], artifacts: artifacts.map((artifact) => ({ ...artifact, path: relocate(artifact.path) })), ...(original.runner_evidence_required ? { runner_evidence_required: original.runner_evidence_required, runner_evidence: evidence } : {}), execution_failures: outcome.execution_failures ?? [], ...(outcome.candidate_root ? { candidate_root: outcome.candidate_root } : {}), ...(outcome.candidate_files ? { candidate_files: outcome.candidate_files } : {}) };
|
|
876
|
+
}
|
|
877
|
+
function executionFailures(input) {
|
|
878
|
+
const failures = [];
|
|
879
|
+
if (input.exitCode === null)
|
|
880
|
+
failures.push({ code: "completion_unknown", message: "Check completion is unknown" });
|
|
881
|
+
if (input.launcherError)
|
|
882
|
+
failures.push({ code: "launcher_error", message: input.launcherError });
|
|
883
|
+
if (input.cleanupUnconfirmed)
|
|
884
|
+
failures.push({ code: "cleanup_unconfirmed", message: "Check process cleanup is not confirmed" });
|
|
885
|
+
if (input.missing.length)
|
|
886
|
+
failures.push({ code: "missing_required_artifact", message: input.missing.join(", ") });
|
|
887
|
+
if (input.mutations.length)
|
|
888
|
+
failures.push({ code: "check_input_mutated", message: input.mutations.join(", ") });
|
|
889
|
+
if (input.invalidRunner)
|
|
890
|
+
failures.push({ code: "invalid_runner_evidence", message: "Required runner evidence is unavailable or invalid" });
|
|
891
|
+
return failures;
|
|
892
|
+
}
|
|
893
|
+
function retainExecutionOutcome(evidenceDir, binding, candidateRoot, failures, candidateFiles) {
|
|
894
|
+
const temporary = path.join(evidenceDir, `.engine-outcome-${crypto.randomUUID()}.tmp`);
|
|
895
|
+
if (!fs.lstatSync(evidenceDir).isDirectory() || fs.lstatSync(evidenceDir).isSymbolicLink())
|
|
896
|
+
throw new AppError("check_evidence_stale", "Check evidence directory was replaced", 1);
|
|
897
|
+
fs.writeFileSync(temporary, JSON.stringify({ schema_id: "dd-flow/check-execution-outcome@1", id: binding.id, command: binding.command, input_hash: binding.input_hash, verification_epoch: binding.verification_epoch, candidate_root: candidateRoot, candidate_files: candidateFiles, execution_failures: failures }), { flag: "wx", mode: 0o600 });
|
|
898
|
+
fs.renameSync(temporary, path.join(evidenceDir, "engine-outcome.json"));
|
|
230
899
|
}
|
|
231
900
|
export function workspaceFingerprint(root) { return workspaceState(root).fingerprint; }
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
function
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
901
|
+
/** One NUL-aware source of truth for paths differing from HEAD. Rename/copy
|
|
902
|
+
* records carry a second path; retain both because the old side is a deletion. */
|
|
903
|
+
export function workspaceChangedPaths(root) {
|
|
904
|
+
const status = spawnSync("git", ["status", "--porcelain=v1", "-z", "--untracked-files=all"], { cwd: root, encoding: "utf8", maxBuffer: 64 * 1024 * 1024 });
|
|
905
|
+
// A workspace can be intentionally gitless. Keep the same conservative
|
|
906
|
+
// file inventory used for fingerprints instead of making result validation
|
|
907
|
+
// fail before the work contract can report its actual error.
|
|
908
|
+
if (isGitlessWorkspace(root, status))
|
|
909
|
+
return listFiles(root);
|
|
910
|
+
if (status.status !== 0 || status.error)
|
|
911
|
+
throw new AppError("workspace_status_failed", "Could not determine changed workspace paths", 1, { workspace_root: root, stderr: status.stderr || null, cause: status.error?.message ?? null });
|
|
912
|
+
const records = status.stdout.split("\0");
|
|
913
|
+
const paths = [];
|
|
914
|
+
for (let index = 0; index < records.length; index += 1) {
|
|
915
|
+
const record = records[index];
|
|
916
|
+
if (!record)
|
|
917
|
+
continue;
|
|
918
|
+
if (record.length < 4 || record[2] !== " ")
|
|
919
|
+
throw new AppError("workspace_status_invalid", "Git returned an invalid porcelain-v1 record", 1, { workspace_root: root, record });
|
|
920
|
+
paths.push(normalizeWorkspacePath(root, record.slice(3)));
|
|
921
|
+
if (record[0] === "R" || record[0] === "C" || record[1] === "R" || record[1] === "C") {
|
|
922
|
+
const prior = records[++index];
|
|
923
|
+
if (!prior)
|
|
924
|
+
throw new AppError("workspace_status_invalid", "Git rename/copy record is missing its second path", 1, { workspace_root: root, record });
|
|
925
|
+
paths.push(normalizeWorkspacePath(root, prior));
|
|
926
|
+
}
|
|
927
|
+
}
|
|
928
|
+
const ownedHook = paths.includes(".agents/hooks.json") ? ownedWorkspaceHookPath(root) : null;
|
|
929
|
+
return [...new Set(paths)].filter(file => file !== ownedHook).sort();
|
|
930
|
+
}
|
|
931
|
+
/** Ignored machine state cannot be reported as a product change. Tracked paths
|
|
932
|
+
* remain eligible regardless of their directory name. */
|
|
933
|
+
export function ignoredWorkspacePaths(root, paths) {
|
|
934
|
+
const normalized = [...new Set(paths.map((item) => normalizeWorkspacePath(root, item)))];
|
|
935
|
+
if (!normalized.length)
|
|
936
|
+
return [];
|
|
937
|
+
const status = spawnSync("git", ["check-ignore", "-z", "--stdin"], { cwd: root, encoding: "utf8", input: `${normalized.join("\0")}\0`, maxBuffer: 64 * 1024 * 1024 });
|
|
938
|
+
// The canonical owned hook has one meaning across Git status, check cache
|
|
939
|
+
// and MERGE, without changing a sibling worktree's shared Git excludes.
|
|
940
|
+
const gitless = isGitlessWorkspace(root, status);
|
|
941
|
+
if (!gitless && (![0, 1].includes(status.status ?? -1) || status.error))
|
|
942
|
+
throw new AppError("workspace_ignore_check_failed", "Could not classify reported workspace paths", 1, { workspace_root: root, stderr: status.stderr || null, cause: status.error?.message ?? null });
|
|
943
|
+
const ownedHook = normalized.includes(".agents/hooks.json") ? ownedWorkspaceHookPath(root, gitless ? [] : undefined) : null;
|
|
944
|
+
if (gitless)
|
|
945
|
+
return ownedHook ? [ownedHook] : [];
|
|
946
|
+
return [...new Set([...status.stdout.split("\0").filter(Boolean).map((item) => normalizeWorkspacePath(root, item)), ...(ownedHook ? [ownedHook] : [])])].sort();
|
|
947
|
+
}
|
|
948
|
+
export function normalizeWorkspacePath(root, value) {
|
|
949
|
+
if (typeof value !== "string" || !value || value.includes("\0") || path.isAbsolute(value))
|
|
950
|
+
throw new AppError("workspace_path_invalid", "Workspace path must be a non-empty relative path", 2, { path: value });
|
|
951
|
+
const resolvedRoot = path.resolve(root);
|
|
952
|
+
const resolved = path.resolve(resolvedRoot, value);
|
|
953
|
+
const relative = path.relative(resolvedRoot, resolved);
|
|
954
|
+
if (!relative || relative === ".." || relative.startsWith(`..${path.sep}`) || path.isAbsolute(relative))
|
|
955
|
+
throw new AppError("workspace_path_invalid", "Workspace path escapes its root", 2, { path: value, workspace_root: root });
|
|
956
|
+
return relative.split(path.sep).join("/");
|
|
957
|
+
}
|
|
958
|
+
function deduplicate(checks, commands) {
|
|
959
|
+
const groups = new Map();
|
|
960
|
+
checks.forEach((check, index) => {
|
|
961
|
+
const artifacts = [...(check.required_artifacts ?? [])].sort(), ports = [...(check.ports ?? [])].sort(), inputs = normalizedInputs(check.inputs ?? []);
|
|
962
|
+
const reusable = check.reuse === "deterministic";
|
|
963
|
+
// Stage labels identify obligations, not different processes in this gate.
|
|
964
|
+
// Cross-invocation reuse still requires the explicit deterministic contract.
|
|
965
|
+
const environmentInputs = normalizedEnvironmentInputs(check.environment_inputs);
|
|
966
|
+
const key = JSON.stringify([commands[index], artifacts, ports, inputs, reusable, environmentInputs, check.runner_evidence ?? null]);
|
|
967
|
+
const current = groups.get(key);
|
|
968
|
+
if (current)
|
|
969
|
+
current.declarations.push(check);
|
|
970
|
+
else
|
|
971
|
+
groups.set(key, { gate: check.run_at, command: commands[index], declarations: [check], requiredArtifacts: artifacts, ports, inputs, reusable, ...(environmentInputs === undefined ? {} : { environmentInputs }), ...(check.runner_evidence ? { runnerEvidence: check.runner_evidence } : {}) });
|
|
972
|
+
});
|
|
973
|
+
return [...groups.values()].map(group => ({ ...group, gate: [...group.declarations].map(check => check.run_at).sort()[0], declarations: [...group.declarations].sort((left, right) => checkRef(left).localeCompare(checkRef(right))) }));
|
|
974
|
+
}
|
|
975
|
+
function workspaceState(root, ignoredRoots = []) {
|
|
976
|
+
const git = workspaceGitFiles(root, ["-co", "--exclude-standard"]);
|
|
977
|
+
const ignored = ignoredRoots.map(entry => path.resolve(entry));
|
|
978
|
+
const listing = [...new Set(git ?? listFiles(root))].sort();
|
|
979
|
+
const ownedHook = listing.includes(".agents/hooks.json") ? ownedWorkspaceHookPath(root, git ? undefined : []) : null;
|
|
980
|
+
const files = new Map(), hash = crypto.createHash("sha256");
|
|
981
|
+
for (const relative of listing) {
|
|
982
|
+
if (relative === ownedHook)
|
|
983
|
+
continue;
|
|
984
|
+
const file = path.resolve(root, relative);
|
|
985
|
+
if (ignored.some(ignoredRoot => file === ignoredRoot || file.startsWith(`${ignoredRoot}${path.sep}`)))
|
|
986
|
+
continue;
|
|
987
|
+
// Deleted tracked paths are absent content, both before and after commit.
|
|
988
|
+
// Do not mask other IO errors as an unchanged workspace.
|
|
989
|
+
let stat;
|
|
990
|
+
try {
|
|
991
|
+
stat = fs.lstatSync(file);
|
|
992
|
+
}
|
|
993
|
+
catch (error) {
|
|
994
|
+
if (error.code === "ENOENT")
|
|
995
|
+
continue;
|
|
996
|
+
throw error;
|
|
997
|
+
}
|
|
998
|
+
const content = stat.isSymbolicLink() ? Buffer.from(`symlink\0${fs.readlinkSync(file)}`) : stat.isFile() ? fs.readFileSync(file) : Buffer.from("<directory>");
|
|
999
|
+
const digest = crypto.createHash("sha256").update(content).digest("hex");
|
|
1000
|
+
files.set(relative, digest);
|
|
1001
|
+
hash.update(relative).update("\0").update(digest).update("\0");
|
|
1002
|
+
}
|
|
1003
|
+
return { fingerprint: hash.digest("hex"), files };
|
|
1004
|
+
}
|
|
1005
|
+
function workspaceInputFingerprint(root, files, inputs, ignoredRoots) {
|
|
1006
|
+
const selected = new Map(inputs.length === 0 || inputs.includes(".") ? files : []);
|
|
1007
|
+
for (const [file, digest] of explicitInputFiles(root, inputs.filter(input => input !== "."), ignoredRoots))
|
|
1008
|
+
selected.set(file, digest);
|
|
1009
|
+
const hash = crypto.createHash("sha256");
|
|
1010
|
+
for (const [file, digest] of [...selected].sort(([left], [right]) => left.localeCompare(right)))
|
|
1011
|
+
hash.update(file).update("\0").update(digest).update("\0");
|
|
1012
|
+
return hash.digest("hex");
|
|
1013
|
+
}
|
|
245
1014
|
function changedPaths(before, after) { return [...new Set([...before.keys(), ...after.keys()])].filter((key) => before.get(key) !== after.get(key)).sort(); }
|
|
246
1015
|
function checkRef(check) { return check.canonical_ref ?? check.id; }
|
|
1016
|
+
function assertConsistentDeclarations(checks, commands) {
|
|
1017
|
+
const seen = new Map();
|
|
1018
|
+
for (const [index, check] of checks.entries()) {
|
|
1019
|
+
const contract = JSON.stringify([check.run_at, commands[index], [...(check.required_artifacts ?? [])].sort(), [...(check.ports ?? [])].sort(), normalizedInputs(check.inputs ?? []), check.reuse ?? null, normalizedEnvironmentInputs(check.environment_inputs), check.runner_evidence ?? null]);
|
|
1020
|
+
const ref = checkRef(check), prior = seen.get(ref);
|
|
1021
|
+
if (prior && prior !== contract)
|
|
1022
|
+
throw new AppError("check_declaration_conflict", "The same check reference has conflicting execution definitions", 2, { check_ref: ref });
|
|
1023
|
+
seen.set(ref, contract);
|
|
1024
|
+
}
|
|
1025
|
+
}
|
|
1026
|
+
function normalizedEnvironmentInputs(value) {
|
|
1027
|
+
if (value === undefined)
|
|
1028
|
+
return undefined;
|
|
1029
|
+
if (!Array.isArray(value) || !value.length || value.length > 64 || value.some(name => typeof name !== "string" || !/^[A-Za-z_][A-Za-z0-9_]*\*?$/.test(name)))
|
|
1030
|
+
throw new AppError("invalid_check_environment_inputs", "Internal check environment inputs must be bounded names or prefixes", 2, { effect: "no_effect" });
|
|
1031
|
+
return [...new Set(value)].sort();
|
|
1032
|
+
}
|
|
1033
|
+
function environmentFingerprint(workspaceRoot, inputs, checkEnvironment) {
|
|
1034
|
+
const environment = codeExecutionEnvironment(workspaceRoot, checkEnvironment);
|
|
1035
|
+
// Invocation authority is checked by its own owners, not an application check
|
|
1036
|
+
// input. Preserve arbitrary application variables (including DD_FLOW_PORT_*)
|
|
1037
|
+
// and tool selection; omit only known process/control bookkeeping.
|
|
1038
|
+
const bookkeeping = new Set(["PWD", "OLDPWD", "SHLVL", "_", "DD_FLOW_DAEMON_ID",
|
|
1039
|
+
"DD_FLOW_CURRENT_INVOCATION", "DD_FLOW_INVOCATION_READONLY", "DD_FLOW_INVOCATION_SCOPE", "DD_FLOW_CODEX_STATE_DIR",
|
|
1040
|
+
"DD_FLOW_RUNTIME_OWNER", "DD_FLOW_CONTROLLER_TOKEN", "DD_FLOW_CONTROL_TOKEN", "DD_FLOW_SCOPE_CONTROL_TOKEN",
|
|
1041
|
+
"DD_FLOW_BIN", "DD_FLOW_ENGINE_HOME", "DD_FLOW_RUNTIME_BUDGET", "DD_FLOW_WORK_ID", "DD_FLOW_SESSION_ID", "CODEX_THREAD_ID", "DD_EVAL_OPERATION_ID",
|
|
1042
|
+
"CODEX_PERMISSION_PROFILE", "CODEX_SESSION_ID", "CODEX_VERSION"]);
|
|
1043
|
+
return crypto.createHash("sha256").update(JSON.stringify(Object.entries(environment)
|
|
1044
|
+
.filter(([name, value]) => value !== undefined && !bookkeeping.has(name)
|
|
1045
|
+
&& (inputs === undefined || inputs.some(input => input.endsWith("*") ? name.startsWith(input.slice(0, -1)) : name === input)))
|
|
1046
|
+
.sort(([left], [right]) => left.localeCompare(right)))).digest("hex");
|
|
1047
|
+
}
|
|
1048
|
+
function acquireWorkspaceGate(context, input) {
|
|
1049
|
+
const operationId = workspaceOperationId(input.workspaceRoot);
|
|
1050
|
+
const active = managedProcessStatus(context).find(item => item.operation_id === operationId && ["starting", "running", "stopping"].includes(item.state));
|
|
1051
|
+
if (active)
|
|
1052
|
+
throw new AppError("check_in_progress", "The workspace is owned by another bootstrap or check; observe it before starting a new check", 1, { process_id: active.id, operation_id: operationId });
|
|
1053
|
+
return registerManagedProcess(context, { kind: "workspace-gate", ownerPid: process.pid, ownerId: `workspace-gate:${crypto.randomUUID()}`, operationId, uniqueActiveOperation: true, projectId: input.projectId, runId: input.runId, metadata: { workspace_root: input.workspaceRoot, role: "check_gate" } });
|
|
1054
|
+
}
|
|
1055
|
+
export function reconcileWorkspaceOwnership(context, workspaceRoot) {
|
|
1056
|
+
const operationId = workspaceOperationId(workspaceRoot);
|
|
1057
|
+
const active = managedProcessStatus(context).filter(item => item.operation_id === operationId && ["starting", "running", "stopping", "orphaned"].includes(item.state));
|
|
1058
|
+
if (active.some(item => processTreeIsAlive(item) || managedOwnerIsAlive(item) !== false))
|
|
1059
|
+
return;
|
|
1060
|
+
// Registry IDs are host-wide, whereas receipts belong to one runtime DB.
|
|
1061
|
+
// An unknown/foreign home must not be settled against this client's rows.
|
|
1062
|
+
if (active.some(item => managedProcessRuntimeHome(item, context) !== context.ddFlowHome))
|
|
1063
|
+
return;
|
|
1064
|
+
for (const child of active.filter(item => item.kind !== "workspace-gate")) {
|
|
1065
|
+
if (child.kind !== "check" || !child.project_id || !child.run_id || !child.check_id)
|
|
1066
|
+
return;
|
|
1067
|
+
const run = context.db.get("SELECT run_root FROM runs WHERE project_id = ? AND id = ?", [child.project_id, child.run_id]);
|
|
1068
|
+
if (!run?.run_root || !context.db.get("SELECT 1 FROM check_receipts WHERE id = ? AND project_id = ? AND run_id = ?", [child.check_id, child.project_id, child.run_id]))
|
|
1069
|
+
return;
|
|
1070
|
+
reconcileUnfinishedChecks(context, { projectId: child.project_id, runId: child.run_id, workspaceRoot, runHome: run.run_root, receiptId: child.check_id });
|
|
1071
|
+
// A crash may occur after receipt settlement but before registry settlement.
|
|
1072
|
+
const current = managedProcessStatus(context).find(item => item.id === child.id);
|
|
1073
|
+
if (!current || current.lease_token !== child.lease_token)
|
|
1074
|
+
return;
|
|
1075
|
+
if (!["stopped", "failed"].includes(current.state) && !finishManagedProcess(context, { id: child.id, leaseToken: child.lease_token, state: "stopped", reason: "workspace_check_owner_reconciled" }))
|
|
1076
|
+
return;
|
|
1077
|
+
}
|
|
1078
|
+
for (const gate of active.filter(item => item.kind === "workspace-gate"))
|
|
1079
|
+
finishManagedProcess(context, { id: gate.id, leaseToken: gate.lease_token, state: "stopped", reason: "workspace_gate_owner_reconciled" });
|
|
1080
|
+
}
|
|
1081
|
+
function normalizedInputs(inputs) { for (const input of inputs)
|
|
1082
|
+
validateCheckInputSyntax(input); return [...new Set(inputs.map((input) => { const normalized = path.posix.normalize(input.replaceAll("\\", "/")).replace(/^\.\/$/, ""); return normalized === "" || normalized === "." ? "." : normalized.replace(/\/$/, ""); }))].sort(); }
|
|
1083
|
+
function validateCheckInputs(workspaceRoot, inputs) { const root = fs.realpathSync.native(workspaceRoot); for (const input of normalizedInputs(inputs)) {
|
|
1084
|
+
if (input === ".git" || input.startsWith(".git/"))
|
|
1085
|
+
throw new AppError("invalid_check_input", "Check inputs cannot include Git internals", 2, { input });
|
|
1086
|
+
const candidate = path.resolve(workspaceRoot, input);
|
|
1087
|
+
if (candidate !== workspaceRoot && !candidate.startsWith(`${workspaceRoot}${path.sep}`))
|
|
1088
|
+
throw new AppError("invalid_check_input", "Check inputs must stay within the workspace", 2, { input });
|
|
1089
|
+
if (fs.existsSync(candidate))
|
|
1090
|
+
validateInputTree(root, candidate, input, new Set());
|
|
1091
|
+
else
|
|
1092
|
+
validateMissingInputParent(root, candidate, input);
|
|
1093
|
+
} }
|
|
1094
|
+
function validateCheckInputSyntax(input) { if (typeof input !== "string" || !input.trim() || path.isAbsolute(input) || input.split(/[\\/]/).includes(".."))
|
|
1095
|
+
throw new AppError("invalid_check_input", "Check inputs must be non-empty relative workspace paths", 2, { input }); }
|
|
1096
|
+
function validateMissingInputParent(root, candidate, input) { let parent = candidate; while (!fs.existsSync(parent))
|
|
1097
|
+
parent = path.dirname(parent); const resolved = fs.realpathSync.native(parent); if (resolved !== root && !resolved.startsWith(`${root}${path.sep}`))
|
|
1098
|
+
throw new AppError("invalid_check_input", "Check input parent symlink escapes the workspace", 2, { input }); }
|
|
1099
|
+
function validateInputTree(root, candidate, input, seen) {
|
|
1100
|
+
const resolved = fs.realpathSync.native(candidate);
|
|
1101
|
+
if (resolved !== root && !resolved.startsWith(`${root}${path.sep}`))
|
|
1102
|
+
throw new AppError("invalid_check_input", "Check input symlink escapes the workspace", 2, { input });
|
|
1103
|
+
if (seen.has(resolved))
|
|
1104
|
+
throw new AppError("invalid_check_input", "Check input contains a symbolic-link cycle", 2, { input });
|
|
1105
|
+
seen.add(resolved);
|
|
1106
|
+
const stat = fs.statSync(candidate);
|
|
1107
|
+
if (stat.isDirectory())
|
|
1108
|
+
for (const entry of fs.readdirSync(candidate))
|
|
1109
|
+
validateInputTree(root, path.join(candidate, entry), input, seen);
|
|
1110
|
+
seen.delete(resolved);
|
|
1111
|
+
}
|
|
1112
|
+
function explicitInputFiles(root, inputs, ignoredRoots) {
|
|
1113
|
+
const rootReal = fs.realpathSync.native(root), ignored = ignoredRoots.map(entry => path.resolve(entry)), result = new Map();
|
|
1114
|
+
const visit = (file, relative, seen) => {
|
|
1115
|
+
if (ignored.some(entry => file === entry || file.startsWith(`${entry}${path.sep}`)))
|
|
1116
|
+
return;
|
|
1117
|
+
if (!fs.existsSync(file)) {
|
|
1118
|
+
result.set(relative, "<missing>");
|
|
1119
|
+
return;
|
|
1120
|
+
}
|
|
1121
|
+
const resolved = fs.realpathSync.native(file);
|
|
1122
|
+
if (resolved !== rootReal && !resolved.startsWith(`${rootReal}${path.sep}`))
|
|
1123
|
+
throw new AppError("invalid_check_input", "Check input symlink escapes the workspace", 2, { input: relative });
|
|
1124
|
+
if (seen.has(resolved))
|
|
1125
|
+
throw new AppError("invalid_check_input", "Check input contains a symbolic-link cycle", 2, { input: relative });
|
|
1126
|
+
const stat = fs.statSync(file);
|
|
1127
|
+
if (stat.isDirectory()) {
|
|
1128
|
+
seen.add(resolved);
|
|
1129
|
+
for (const entry of fs.readdirSync(file).sort())
|
|
1130
|
+
visit(path.join(file, entry), path.posix.join(relative, entry), seen);
|
|
1131
|
+
seen.delete(resolved);
|
|
1132
|
+
}
|
|
1133
|
+
else if (stat.isFile())
|
|
1134
|
+
result.set(relative, crypto.createHash("sha256").update(fs.readFileSync(file)).digest("hex"));
|
|
1135
|
+
};
|
|
1136
|
+
for (const input of inputs)
|
|
1137
|
+
visit(path.resolve(root, input), input, new Set());
|
|
1138
|
+
return [...result.entries()];
|
|
1139
|
+
}
|
|
1140
|
+
function checkInputHash(workspaceRoot, group, profileHash, inputFingerprint, evidenceContext, checkEnvironment) { return crypto.createHash("sha256").update(JSON.stringify({ workspace_root: path.resolve(workspaceRoot), gate: group.reusable ? null : group.gate, command: group.command, required_artifacts: group.requiredArtifacts, ports: group.ports, inputs: group.inputs, ...(group.runnerEvidence ? { runner_evidence: group.runnerEvidence } : {}), profile_hash: profileHash, environment: environmentFingerprint(workspaceRoot, group.environmentInputs, checkEnvironment), input_fingerprint: inputFingerprint, ...(group.environmentInputs === undefined ? {} : { environment_inputs: group.environmentInputs }), ...(evidenceContext === undefined ? {} : { evidence_context: evidenceContext }) })).digest("hex"); }
|
|
1141
|
+
function derivedReceiptArtifacts(group, receipt) { return [...new Set([...group.requiredArtifacts, ...runnerProofArtifacts(receipt.runner_evidence), ...(receipt.required_artifacts.includes("engine-outcome.json") ? ["engine-outcome.json"] : [])])]; }
|
|
1142
|
+
function bindReceipt(receipt, group) { return { ...receipt, gate: group.gate, declaration_id: group.declarations[0].id, check_refs: group.declarations.map(checkRef), required_artifacts: derivedReceiptArtifacts(group, receipt), inputs: group.inputs }; }
|
|
1143
|
+
export function checkReceiptEvidenceValid(receipt) { return checkReceiptEvidenceValidForMode(receipt); }
|
|
1144
|
+
function checkReceiptEvidenceValidForMode(receipt, mode) {
|
|
1145
|
+
try {
|
|
1146
|
+
if (!(receipt.status === "passed" && receipt.exit_code === 0 || mode === "diagnostic" && receipt.status === "failed" && typeof receipt.exit_code === "number" && receipt.exit_code !== 0) || receipt.before_fingerprint !== receipt.after_fingerprint
|
|
1147
|
+
|| receipt.mutation_paths.length !== 0 || receipt.completion_path !== path.join(path.dirname(receipt.receipt_path), "completion.json")
|
|
1148
|
+
|| readCompletion(receipt.completion_path)?.exit_code !== receipt.exit_code)
|
|
1149
|
+
return false;
|
|
1150
|
+
const stored = readReceipt(receipt.receipt_path);
|
|
1151
|
+
if (!stored || typeof stored !== "object" || Array.isArray(stored)
|
|
1152
|
+
|| typeof stored.receipt_path !== "string" || !path.isAbsolute(stored.receipt_path)
|
|
1153
|
+
|| path.basename(stored.receipt_path) !== "receipt.json"
|
|
1154
|
+
|| stored.completion_path !== path.join(path.dirname(stored.receipt_path), "completion.json"))
|
|
1155
|
+
return false;
|
|
1156
|
+
// Gate/ref labels may be rebound for explicit deterministic reuse. Physical
|
|
1157
|
+
// execution identity and settled SQL evidence must never be rebound from a
|
|
1158
|
+
// mutable receipt file, including after recovery relocates the RUN home.
|
|
1159
|
+
const fields = ["id", "scope", "work_id", "command", "input_hash", "verification_epoch", "status", "exit_code",
|
|
1160
|
+
"workspace_fingerprint", "before_fingerprint", "after_fingerprint", "profile_hash", "started_at", "finished_at"];
|
|
1161
|
+
if (fields.some(field => stored[field] !== receipt[field]) || !Array.isArray(stored.artifacts)
|
|
1162
|
+
|| !Array.isArray(stored.mutation_paths) || stored.mutation_paths.length !== 0
|
|
1163
|
+
|| JSON.stringify(stored.required_artifacts) !== JSON.stringify(receipt.required_artifacts)
|
|
1164
|
+
|| JSON.stringify(stored.inputs) !== JSON.stringify(receipt.inputs))
|
|
1165
|
+
return false;
|
|
1166
|
+
const retained = resolvedReceiptArtifacts(receipt.receipt_path, stored.artifacts, receipt).artifacts;
|
|
1167
|
+
if (JSON.stringify(retained) !== JSON.stringify(receipt.artifacts))
|
|
1168
|
+
return false;
|
|
1169
|
+
const root = path.join(path.dirname(receipt.receipt_path), "artifacts");
|
|
1170
|
+
if (stored.runner_evidence_required && mode !== "diagnostic" && !runnerEvidencePass(readRunnerEvidence(root, stored.runner_evidence_required, { invocation_id: receipt.id, check_id: receipt.id, command: receipt.command, input_hash: receipt.input_hash, verification_epoch: receipt.verification_epoch })))
|
|
1171
|
+
return false;
|
|
1172
|
+
const artifacts = collectCheckArtifacts(root, receipt.required_artifacts);
|
|
1173
|
+
return artifacts.complete && JSON.stringify(artifacts.items) === JSON.stringify(retained);
|
|
1174
|
+
}
|
|
1175
|
+
catch {
|
|
1176
|
+
return false;
|
|
1177
|
+
}
|
|
1178
|
+
}
|
|
247
1179
|
function parseArray(value, fallback) { try {
|
|
248
1180
|
const parsed = JSON.parse(value);
|
|
249
1181
|
return Array.isArray(parsed) && parsed.every((item) => typeof item === "string") ? parsed : fallback;
|
|
@@ -252,8 +1184,14 @@ catch {
|
|
|
252
1184
|
return fallback;
|
|
253
1185
|
} }
|
|
254
1186
|
function isStringRecord(value) { return Boolean(value) && typeof value === "object" && !Array.isArray(value) && Object.values(value).every((item) => typeof item === "string" && item.trim().length > 0); }
|
|
255
|
-
function isGateRecord(value) { return Boolean(value) && typeof value === "object" && !Array.isArray(value) && Object.entries(value).every(([gate, aliases]) => ["work", "code", "readiness", "merge", "release"
|
|
1187
|
+
function isGateRecord(value) { return Boolean(value) && typeof value === "object" && !Array.isArray(value) && Object.entries(value).every(([gate, aliases]) => ["work", "code", "readiness", "merge", "release"].includes(gate) && Array.isArray(aliases) && aliases.every((item) => typeof item === "string" && item.startsWith("@check/"))); }
|
|
256
1188
|
function isPortAliasRecord(value) { return value === undefined || (Boolean(value) && typeof value === "object" && !Array.isArray(value) && Object.entries(value).every(([alias, ports]) => alias.startsWith("@check/") && Array.isArray(ports) && ports.length > 0 && new Set(ports).size === ports.length && ports.every((port) => typeof port === "string" && /^[A-Za-z][A-Za-z0-9_]*$/.test(port)))); }
|
|
1189
|
+
function isInputAliasRecord(workspaceRoot, value) { try {
|
|
1190
|
+
return value === undefined || (Boolean(value) && typeof value === "object" && !Array.isArray(value) && Object.entries(value).every(([alias, inputs]) => alias.startsWith("@check/") && Array.isArray(inputs) && (validateCheckInputs(workspaceRoot, inputs), true)));
|
|
1191
|
+
}
|
|
1192
|
+
catch {
|
|
1193
|
+
return false;
|
|
1194
|
+
} }
|
|
257
1195
|
function portEnvironment(ports) { return Object.fromEntries(Object.entries(ports).map(([name, value]) => [`DD_FLOW_PORT_${name.toUpperCase()}`, String(value)])); }
|
|
258
1196
|
function readReceiptResources(file) { try {
|
|
259
1197
|
const value = JSON.parse(fs.readFileSync(file, "utf8"));
|
|
@@ -262,51 +1200,132 @@ function readReceiptResources(file) { try {
|
|
|
262
1200
|
catch {
|
|
263
1201
|
return { ports: {} };
|
|
264
1202
|
} }
|
|
265
|
-
function
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
1203
|
+
function collectCheckArtifacts(root, required) {
|
|
1204
|
+
for (const relative of required) {
|
|
1205
|
+
if (path.isAbsolute(relative) || relative.split(/[\\/]/).includes(".."))
|
|
1206
|
+
throw new AppError("invalid_evidence_path", "Required evidence paths must be relative to DD_FLOW_EVIDENCE_DIR", 2, { path: relative });
|
|
1207
|
+
}
|
|
1208
|
+
// Required paths are minimum obligations; retain all explicit check evidence.
|
|
1209
|
+
// Never follow symlinks, including a replaced evidence root.
|
|
1210
|
+
const items = [];
|
|
1211
|
+
const visit = (directory) => {
|
|
1212
|
+
for (const entry of fs.readdirSync(directory, { withFileTypes: true })) {
|
|
1213
|
+
const file = path.join(directory, entry.name);
|
|
1214
|
+
if (entry.isDirectory())
|
|
1215
|
+
visit(file);
|
|
1216
|
+
else if (entry.isFile())
|
|
1217
|
+
items.push({ path: path.relative(root, file), sha256: crypto.createHash("sha256").update(fs.readFileSync(file)).digest("hex") });
|
|
1218
|
+
}
|
|
1219
|
+
};
|
|
1220
|
+
if (fs.existsSync(root) && fs.lstatSync(root).isDirectory())
|
|
1221
|
+
visit(root);
|
|
1222
|
+
items.sort((a, b) => a.path.localeCompare(b.path));
|
|
1223
|
+
const retained = new Set(items.map((item) => item.path));
|
|
1224
|
+
const missing = required.filter((relative) => !retained.has(path.normalize(relative)));
|
|
1225
|
+
return { complete: missing.length === 0, missing, items };
|
|
1226
|
+
}
|
|
1227
|
+
function listFiles(root) {
|
|
1228
|
+
const allowed = projectServicePathFilter(root, [], []);
|
|
1229
|
+
const ownedHook = ownedWorkspaceHookPath(root, []);
|
|
1230
|
+
const visit = (current) => fs.readdirSync(current, { withFileTypes: true }).flatMap(entry => {
|
|
1231
|
+
const absolute = path.join(current, entry.name);
|
|
1232
|
+
if ([".git", "node_modules"].includes(entry.name) || !allowed(absolute))
|
|
1233
|
+
return [];
|
|
1234
|
+
return entry.isDirectory() ? visit(absolute) : [path.relative(root, absolute)];
|
|
1235
|
+
});
|
|
1236
|
+
return visit(root).filter(file => file.split(path.sep).join("/") !== ownedHook).sort();
|
|
1237
|
+
}
|
|
1238
|
+
export function runCheck(command, cwd, environment, stdout, stderr, heartbeat, onSpawn, renewLease, observation = {}) {
|
|
1239
|
+
const inactivityTimeoutMs = validateObservationDuration(observation.inactivityTimeoutMs ?? 30 * 60_000);
|
|
1240
|
+
const { startupMs: runnerStartupMs, executionMs: runnerExecutionMs } = runnerObservationDurations(environment.DD_FLOW_CHECK_RUNNER_PROGRESS_PATH ? environment : {});
|
|
278
1241
|
return new Promise((resolve) => {
|
|
279
1242
|
const started = Date.now();
|
|
280
|
-
const child = spawn("/bin/sh", ["-
|
|
1243
|
+
const child = spawn("/bin/sh", ["-c", checkShellScript()], { cwd, env: { ...environment, DD_FLOW_CHECK_COMMAND: command }, stdio: ["pipe", "pipe", "pipe"], detached: process.platform !== "win32" });
|
|
281
1244
|
let error = null;
|
|
1245
|
+
let failure;
|
|
282
1246
|
let stopping = false;
|
|
1247
|
+
let closed = false;
|
|
283
1248
|
let progress;
|
|
284
|
-
let
|
|
1249
|
+
let cleanup;
|
|
1250
|
+
let cleanupUnconfirmed = false;
|
|
1251
|
+
const renewal = createRenewalState();
|
|
1252
|
+
let renewalTimer;
|
|
1253
|
+
const clock = new ObservationClock({ timeoutMs: inactivityTimeoutMs });
|
|
1254
|
+
let outputCursor = 0;
|
|
1255
|
+
let runnerSequence = 0;
|
|
1256
|
+
let runnerPhase = "startup";
|
|
1257
|
+
const runnerProgress = environment.DD_FLOW_CHECK_RUNNER_PROGRESS_PATH;
|
|
1258
|
+
const runnerClock = runnerProgress ? new ObservationClock({ timeoutMs: runnerStartupMs }) : null;
|
|
1259
|
+
const outputs = [fs.createWriteStream("", { fd: stdout, autoClose: false }), fs.createWriteStream("", { fd: stderr, autoClose: false })];
|
|
1260
|
+
const flushed = outputs.map(output => new Promise(resolve => { output.once("finish", resolve); output.once("error", () => resolve()); }));
|
|
285
1261
|
const stop = (cause) => {
|
|
286
1262
|
if (stopping)
|
|
287
1263
|
return;
|
|
288
1264
|
stopping = true;
|
|
289
1265
|
error = cause instanceof Error ? cause.message : String(cause);
|
|
1266
|
+
if (cause instanceof AppError)
|
|
1267
|
+
failure = { code: cause.code, message: cause.message };
|
|
290
1268
|
if (progress) {
|
|
291
1269
|
clearInterval(progress);
|
|
292
1270
|
progress = undefined;
|
|
293
1271
|
}
|
|
294
|
-
if (
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
1272
|
+
if (renewalTimer)
|
|
1273
|
+
clearTimeout(renewalTimer);
|
|
1274
|
+
if (observationTimer)
|
|
1275
|
+
clearInterval(observationTimer);
|
|
1276
|
+
cleanup = stopProcessGroup(child, 5_000).catch(cause => {
|
|
1277
|
+
cleanupUnconfirmed = true;
|
|
1278
|
+
error = `${error}; cleanup: ${JSON.stringify(errorRecord(cause))}`;
|
|
1279
|
+
resolve({ exitCode: null, error, aborted: true, cleanupUnconfirmed, ...(failure ? { failure } : {}) });
|
|
1280
|
+
});
|
|
298
1281
|
};
|
|
1282
|
+
const observationTimer = setInterval(() => {
|
|
1283
|
+
if (stopping || closed || observation.phase?.() === "serving")
|
|
1284
|
+
return;
|
|
1285
|
+
if (runnerClock && runnerProgress) {
|
|
1286
|
+
try {
|
|
1287
|
+
for (const event of readRunnerProgress(runnerProgress, environment.DD_FLOW_CHECK_INVOCATION_ID ?? "", runnerSequence)) {
|
|
1288
|
+
runnerSequence = event.sequence;
|
|
1289
|
+
if (runnerPhase !== "execution")
|
|
1290
|
+
runnerPhase = event.phase;
|
|
1291
|
+
runnerClock.sample(runnerSequence, Date.parse(event.at));
|
|
1292
|
+
}
|
|
1293
|
+
// The outer guard includes the runner's bounded cleanup tail.
|
|
1294
|
+
runnerClock.timeoutMs = runnerPhase === "execution" ? runnerExecutionMs : runnerStartupMs;
|
|
1295
|
+
if (runnerClock.sample(runnerSequence))
|
|
1296
|
+
stop(new AppError("operation_observation_lost", "Structured test runner stopped making progress", 1, { phase: runnerPhase, sequence: runnerSequence }));
|
|
1297
|
+
}
|
|
1298
|
+
catch (cause) {
|
|
1299
|
+
stop(cause);
|
|
1300
|
+
}
|
|
1301
|
+
return;
|
|
1302
|
+
}
|
|
1303
|
+
if (clock.sample(outputCursor))
|
|
1304
|
+
stop(new AppError("operation_observation_lost", "Owned command has no confirmed progress within its inactivity window", 1, { phase: observation.phase?.() ?? "finite", inactivity_timeout_ms: inactivityTimeoutMs }));
|
|
1305
|
+
}, Math.min(1000, inactivityTimeoutMs));
|
|
299
1306
|
// Register handlers before checking pid or calling persistence callbacks.
|
|
300
1307
|
// Failure is not completion until the spawned process has actually closed.
|
|
301
1308
|
child.once("error", (value) => { error = value.message; });
|
|
302
|
-
child.
|
|
1309
|
+
for (const [index, stream] of [child.stdout, child.stderr].entries()) {
|
|
1310
|
+
outputs[index].once("error", stop);
|
|
1311
|
+
stream.on("data", () => { if (!runnerClock) {
|
|
1312
|
+
outputCursor++;
|
|
1313
|
+
clock.sample(outputCursor);
|
|
1314
|
+
} });
|
|
1315
|
+
stream.pipe(outputs[index]);
|
|
1316
|
+
}
|
|
1317
|
+
child.stdin.on("error", stop);
|
|
1318
|
+
child.once("close", async (exitCode, signal) => {
|
|
1319
|
+
closed = true;
|
|
303
1320
|
if (progress)
|
|
304
1321
|
clearInterval(progress);
|
|
305
|
-
if (
|
|
306
|
-
clearTimeout(
|
|
307
|
-
if (
|
|
308
|
-
|
|
309
|
-
|
|
1322
|
+
if (renewalTimer)
|
|
1323
|
+
clearTimeout(renewalTimer);
|
|
1324
|
+
if (observationTimer)
|
|
1325
|
+
clearInterval(observationTimer);
|
|
1326
|
+
await cleanup;
|
|
1327
|
+
await Promise.all(flushed);
|
|
1328
|
+
resolve({ exitCode, error: error ?? (signal ? `terminated by ${signal}` : null), aborted: stopping || Boolean(signal) || Boolean(error), ...(cleanupUnconfirmed ? { cleanupUnconfirmed } : {}), ...(failure ? { failure } : {}) });
|
|
310
1329
|
});
|
|
311
1330
|
if (!child.pid) {
|
|
312
1331
|
error = "check process did not provide a PID";
|
|
@@ -319,31 +1338,73 @@ export function runCheck(command, cwd, environment, stdout, stderr, heartbeat, o
|
|
|
319
1338
|
stop(cause);
|
|
320
1339
|
return;
|
|
321
1340
|
}
|
|
322
|
-
|
|
1341
|
+
if (observation.leaseExpiresAt) {
|
|
1342
|
+
const expiry = observation.leaseExpiresAt();
|
|
1343
|
+
if (!expiry || !renewalConfirmed(renewal, performance.now(), expiry)) {
|
|
1344
|
+
stop("check confirmation has no current authority");
|
|
1345
|
+
return;
|
|
1346
|
+
}
|
|
1347
|
+
}
|
|
1348
|
+
child.stdin.end("admitted\n");
|
|
1349
|
+
clock.sample(outputCursor);
|
|
1350
|
+
const renew = () => {
|
|
1351
|
+
if (stopping || closed)
|
|
1352
|
+
return;
|
|
1353
|
+
if (renewalTimer) {
|
|
1354
|
+
clearTimeout(renewalTimer);
|
|
1355
|
+
renewalTimer = undefined;
|
|
1356
|
+
}
|
|
323
1357
|
try {
|
|
324
|
-
|
|
1358
|
+
renewal.uncertainSince ??= performance.now();
|
|
1359
|
+
const remaining = renewalRemaining(renewal);
|
|
1360
|
+
if (!remaining) {
|
|
1361
|
+
stop("managed process renewal budget exhausted");
|
|
1362
|
+
return;
|
|
1363
|
+
}
|
|
1364
|
+
const receipt = renewLease(Math.min(RENEWAL_POLICY.attemptMs, remaining));
|
|
1365
|
+
if (receipt === false || typeof receipt === "object" && !receipt.owned) {
|
|
325
1366
|
stop("managed process lease lost");
|
|
326
1367
|
return;
|
|
327
1368
|
}
|
|
1369
|
+
if (typeof receipt === "object" && receipt.owned) {
|
|
1370
|
+
const expiry = Date.parse(receipt.lease_expires_at);
|
|
1371
|
+
if (!Number.isFinite(expiry))
|
|
1372
|
+
throw new AppError("managed_lease_receipt_invalid", "Check renewal returned invalid expiry", 1);
|
|
1373
|
+
if (expiry <= Date.now())
|
|
1374
|
+
throw Object.assign(new Error("Check renewal receipt expired before acknowledgement"), { code: "process_maintenance_timeout" });
|
|
1375
|
+
}
|
|
1376
|
+
if (!renewalConfirmed(renewal, performance.now(), typeof receipt === "object" && receipt.owned ? receipt.lease_expires_at : null)) {
|
|
1377
|
+
stop("managed process renewal budget exhausted");
|
|
1378
|
+
return;
|
|
1379
|
+
}
|
|
1380
|
+
}
|
|
1381
|
+
catch (cause) {
|
|
1382
|
+
if (!maintenanceRetryable(cause) || !renewalFailure(renewal)) {
|
|
1383
|
+
stop(cause);
|
|
1384
|
+
return;
|
|
1385
|
+
}
|
|
1386
|
+
renewalTimer = setTimeout(renew, Math.min(renewalDelay(renewal), renewalRemaining(renewal)));
|
|
1387
|
+
return;
|
|
1388
|
+
}
|
|
1389
|
+
// A progress callback error is not a retryable renewal failure.
|
|
1390
|
+
try {
|
|
328
1391
|
heartbeat(Math.floor((Date.now() - started) / 1000));
|
|
329
1392
|
}
|
|
330
1393
|
catch (cause) {
|
|
331
1394
|
stop(cause);
|
|
332
1395
|
}
|
|
333
|
-
}
|
|
1396
|
+
};
|
|
1397
|
+
progress = setInterval(renew, 15_000);
|
|
334
1398
|
});
|
|
335
1399
|
}
|
|
336
1400
|
function checkShellScript() {
|
|
337
1401
|
return [
|
|
1402
|
+
// The actual command cannot run before PID ownership and admission persist.
|
|
1403
|
+
'IFS= read -r admission && [ "$admission" = admitted ] || exit 125',
|
|
338
1404
|
'completion="${DD_FLOW_CHECK_COMPLETION_FILE:-}"',
|
|
339
|
-
'/bin/sh -
|
|
1405
|
+
'/bin/sh -c "$DD_FLOW_CHECK_COMMAND"',
|
|
340
1406
|
"code=$?",
|
|
341
1407
|
'if [ -n "$completion" ]; then temporary="$completion.tmp.$$"; printf \'{"exit_code":%s,"finished_at":"%s"}\\n\' "$code" "$(date -u +%Y-%m-%dT%H:%M:%SZ)" > "$temporary" && mv "$temporary" "$completion"; fi',
|
|
342
1408
|
'exit "$code"'
|
|
343
1409
|
].join("\n");
|
|
344
1410
|
}
|
|
345
|
-
function terminateProcessGroup(pid, signal) { if (!pid)
|
|
346
|
-
return; try {
|
|
347
|
-
process.kill(process.platform === "win32" ? pid : -pid, signal);
|
|
348
|
-
}
|
|
349
|
-
catch { /* already exited */ } }
|