@deksden-com/dd-flow-cli 0.9.0-beta.15 → 0.9.0-beta.150
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 +895 -0
- package/README.md +140 -6
- package/dist/build-info.json +8 -8
- package/dist/cli/argument-values.js +38 -0
- package/dist/cli/command-inputs.js +386 -0
- package/dist/cli/command-metadata.js +232 -0
- package/dist/cli/command-option-descriptions.js +192 -0
- package/dist/cli/completion-cli.js +70 -0
- package/dist/cli/completion.js +57 -0
- package/dist/cli/diagnostic-invocation.js +46 -0
- package/dist/cli/diagnostic.js +120 -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 +396 -19
- package/dist/cli/hook-ingress.js +102 -0
- package/dist/cli/input-preparation.js +165 -0
- package/dist/cli/memory-flow-admission.js +50 -0
- package/dist/cli/memory-init-invocation.js +85 -0
- package/dist/cli/memory-init.js +249 -0
- package/dist/cli/memory-storage-admission.js +54 -0
- package/dist/cli/memory-storage-qualification.js +105 -0
- package/dist/cli/memory-storage-transition.js +123 -0
- package/dist/cli/memory-upgrade.js +189 -0
- package/dist/cli/models.js +39 -0
- package/dist/cli/native-hook-ingress.js +90 -0
- package/dist/cli/native-hook-preparation.js +55 -0
- package/dist/cli/operator-profiles.js +61 -0
- package/dist/cli/output-options.js +35 -0
- package/dist/cli/response-file.js +72 -0
- package/dist/cli/run-cli.js +1734 -550
- 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 +150 -0
- package/dist/cli.js +89 -2
- package/dist/domain/diagnostic-aspects.js +103 -0
- package/dist/domain/diagnostic-flows.js +8 -0
- package/dist/domain/stage-catalog.js +1 -1
- package/dist/domain/stage-handler-identities.js +28 -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-error-boundary.mjs +58 -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/agy-native-inspection.mjs +216 -0
- package/dist/harness-runtime/lib/agy-native_pb.js +117 -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 +2 -0
- package/dist/harness-runtime/lib/daemon-operations.mjs +462 -0
- package/dist/harness-runtime/lib/dd-agy-daemon.d.mts +6 -0
- package/dist/harness-runtime/lib/dd-agy-daemon.mjs +1096 -0
- package/dist/harness-runtime/lib/dd-agy.mjs +86 -0
- package/dist/harness-runtime/lib/dd-codex-daemon.d.mts +1 -0
- package/dist/harness-runtime/lib/dd-codex-daemon.mjs +231 -0
- package/dist/harness-runtime/lib/dd-codex.d.mts +1 -0
- package/dist/harness-runtime/lib/dd-codex.mjs +954 -0
- package/dist/harness-runtime/lib/dd-droid-daemon.mjs +141 -0
- package/dist/harness-runtime/lib/dd-droid.mjs +626 -0
- package/dist/harness-runtime/lib/dd-grok-daemon.mjs +555 -0
- package/dist/harness-runtime/lib/dd-grok.mjs +180 -0
- package/dist/harness-runtime/lib/dd-opencode-daemon.mjs +622 -0
- package/dist/harness-runtime/lib/dd-opencode.mjs +390 -0
- package/dist/harness-runtime/lib/dd-zcode-daemon.mjs +752 -0
- package/dist/harness-runtime/lib/dd-zcode.mjs +1175 -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 +140 -0
- package/dist/harness-runtime/lib/droid-observation.mjs +66 -0
- package/dist/harness-runtime/lib/entity-references.d.mts +2 -0
- package/dist/harness-runtime/lib/entity-references.mjs +9 -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 +698 -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 +23 -0
- package/dist/harness-runtime/lib/native-children.mjs +219 -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 +95 -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 +15 -0
- package/dist/harness-runtime/lib/provider-overload-policy.mjs +151 -0
- package/dist/harness-runtime/lib/runner-events.mjs +202 -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 +53 -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-decision.schema.json +9 -0
- 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/diagnostic-stage-result.schema.json +354 -0
- package/dist/schemas/diagnostic-work-result.schema.json +296 -0
- package/dist/schemas/discovery-result.schema.json +184 -0
- package/dist/schemas/execution-bundle.schema.json +211 -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 +29 -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/mb-upgrade-stage-result.schema.json +863 -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 +56 -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 +12 -3
- package/dist/schemas/plan-review-result.schema.json +2 -2
- package/dist/schemas/profile-settings.schema.json +122 -0
- package/dist/schemas/project-flow-pack-manifest.schema.json +1 -1
- package/dist/schemas/release-impact.schema.json +3 -3
- 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/stage-start-response.schema.json +4 -0
- package/dist/schemas/vnext-protocol-plan.schema.json +2 -2
- package/dist/services/accepted-intent.js +139 -0
- package/dist/services/branch-context.js +3 -3
- package/dist/services/canon.js +54 -15
- package/dist/services/check-environment.js +21 -0
- package/dist/services/check-runner-evidence.js +562 -0
- package/dist/services/cleanup.js +133 -40
- package/dist/services/cli-operation-classifier.js +26 -63
- package/dist/services/cli-upgrade-allowlist.js +27 -0
- package/dist/services/code-checks.js +1398 -119
- package/dist/services/codex-hook-delivery.js +32 -0
- package/dist/services/command-context.js +80 -0
- package/dist/services/compatibility-preflight.js +16 -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 +94 -0
- package/dist/services/controller-fanout.js +226 -0
- package/dist/services/controller-memory-task.js +45 -0
- package/dist/services/controller-memory-worker.js +49 -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 +107 -0
- package/dist/services/descriptor-run-start.js +475 -0
- package/dist/services/descriptor-stage-coordinator.js +250 -0
- package/dist/services/descriptor-stage-settlement.js +223 -0
- package/dist/services/descriptor-stage-start.js +175 -0
- package/dist/services/diagnostic-evidence.js +73 -0
- package/dist/services/diagnostic-report-publication.js +72 -0
- package/dist/services/diagnostic-report.js +67 -0
- package/dist/services/diagnostic-run-bundle.js +74 -0
- package/dist/services/diagnostic-snapshot.js +159 -0
- package/dist/services/diagnostic-stage-handlers.js +108 -0
- package/dist/services/diagnostic-stage-results.js +132 -0
- package/dist/services/diagnostic-start.js +84 -0
- package/dist/services/diagnostic-work-batch.js +34 -0
- package/dist/services/diagnostic-work-collection.js +97 -0
- package/dist/services/diagnostic-work-registration.js +101 -0
- package/dist/services/diagnostic-work-results.js +93 -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 +354 -163
- package/dist/services/eval-snapshots.js +1316 -150
- package/dist/services/execution-bundles.js +275 -0
- package/dist/services/execution-policy.js +185 -0
- package/dist/services/executor-onboarding.js +56 -0
- package/dist/services/external-work-launch.js +310 -0
- package/dist/services/flow-catalog.js +30 -0
- package/dist/services/flow-descriptors.js +82 -0
- package/dist/services/frozen-engine.js +387 -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/hook-memory-routing.js +188 -0
- package/dist/services/hook-recovery-guard.js +14 -0
- package/dist/services/hook-session-observation.js +24 -0
- package/dist/services/hooks.js +791 -326
- package/dist/services/hybrid-contracts.js +474 -0
- package/dist/services/hybrid-digest.js +14 -0
- package/dist/services/ids.js +29 -1
- package/dist/services/installation-lock.js +95 -0
- package/dist/services/installed-flow-source.js +226 -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 +2148 -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 +449 -69
- package/dist/services/memory-accepted-composition.js +78 -0
- package/dist/services/memory-baseline-preservation.js +65 -0
- package/dist/services/memory-completed-correction-wave.js +328 -0
- package/dist/services/memory-compose-handler.js +400 -0
- package/dist/services/memory-composition-acceptance.js +375 -0
- package/dist/services/memory-composition-graph.js +165 -0
- package/dist/services/memory-composition-registration.js +161 -0
- package/dist/services/memory-composition-work-custody.js +156 -0
- package/dist/services/memory-composition-work-results.js +108 -0
- package/dist/services/memory-corrected-review-acceptance.js +129 -0
- package/dist/services/memory-corrected-review-batch.js +37 -0
- package/dist/services/memory-corrected-review-findings.js +108 -0
- package/dist/services/memory-corrected-review-inputs.js +215 -0
- package/dist/services/memory-corrected-review-projection.js +78 -0
- package/dist/services/memory-corrected-review-receipt.js +178 -0
- package/dist/services/memory-corrected-review-registration.js +192 -0
- package/dist/services/memory-correction-check-receipt.js +235 -0
- package/dist/services/memory-correction-input-owner.js +6 -0
- package/dist/services/memory-custody-snapshot.js +36 -0
- package/dist/services/memory-discover-handler.js +104 -0
- package/dist/services/memory-finish-evidence.js +457 -0
- package/dist/services/memory-finish-handler.js +229 -0
- package/dist/services/memory-finish-integration.js +450 -0
- package/dist/services/memory-finish-readiness.js +169 -0
- package/dist/services/memory-git-integration.js +220 -0
- package/dist/services/memory-historical-corrected-review.js +212 -0
- package/dist/services/memory-historical-original-review.js +143 -0
- package/dist/services/memory-historical-repair-registration.js +105 -0
- package/dist/services/memory-historical-second-review.js +147 -0
- package/dist/services/memory-init-bootstrap-readiness.js +210 -0
- package/dist/services/memory-init-contexts.js +131 -0
- package/dist/services/memory-init-controller-start.js +103 -0
- package/dist/services/memory-init-engine-release.js +223 -0
- package/dist/services/memory-init-git-policy.js +165 -0
- package/dist/services/memory-init-lint-capability.js +203 -0
- package/dist/services/memory-init-native-capacity.js +309 -0
- package/dist/services/memory-init-preflight.js +334 -0
- package/dist/services/memory-init-project-bootstrap.js +599 -0
- package/dist/services/memory-init-request-candidate.js +213 -0
- package/dist/services/memory-init-request-input.js +82 -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 +135 -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 +91 -0
- package/dist/services/memory-init-stores.js +359 -0
- package/dist/services/memory-init-worktrunk-capability.js +174 -0
- package/dist/services/memory-layer-publication.js +611 -0
- package/dist/services/memory-layer.js +221 -0
- package/dist/services/memory-lint-config.js +194 -0
- package/dist/services/memory-native-review-copy.js +103 -0
- package/dist/services/memory-normal-flow-entry.js +87 -0
- package/dist/services/memory-ordinary-defaults.js +129 -0
- package/dist/services/memory-preflight-bootstrap-custody.js +115 -0
- package/dist/services/memory-preflight-handler.js +274 -0
- package/dist/services/memory-repair-current-view.js +299 -0
- package/dist/services/memory-repair-document-delta.js +488 -0
- package/dist/services/memory-repair-history.js +496 -0
- package/dist/services/memory-repair-physical-binding.js +194 -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 +162 -0
- package/dist/services/memory-repair-publication-source.js +225 -0
- package/dist/services/memory-repair-publication.js +795 -0
- package/dist/services/memory-repair-registration.js +240 -0
- package/dist/services/memory-repair-work-baseline.js +415 -0
- package/dist/services/memory-repair-work-completion.js +117 -0
- package/dist/services/memory-repair-work-custody.js +73 -0
- package/dist/services/memory-repair-work-results.js +108 -0
- package/dist/services/memory-repair-workspace.js +261 -0
- package/dist/services/memory-repair-writer-copy.js +736 -0
- package/dist/services/memory-result-binding.js +239 -0
- package/dist/services/memory-retained-review-authority.js +138 -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 +162 -0
- package/dist/services/memory-review-acceptance.js +214 -0
- package/dist/services/memory-review-correction-plan.js +206 -0
- package/dist/services/memory-review-handler.js +585 -0
- package/dist/services/memory-review-inputs.js +246 -0
- package/dist/services/memory-review-limits.js +10 -0
- package/dist/services/memory-review-receipt.js +180 -0
- package/dist/services/memory-review-registration.js +117 -0
- package/dist/services/memory-review-work-custody.js +126 -0
- package/dist/services/memory-review-work-input.js +91 -0
- package/dist/services/memory-review-work-results.js +95 -0
- package/dist/services/memory-second-corrected-review-inputs.js +167 -0
- package/dist/services/memory-second-correction-plan.js +77 -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 +65 -0
- package/dist/services/memory-stage-results.js +290 -0
- package/dist/services/memory-tool-probe.js +25 -0
- package/dist/services/memory-upgrade-analysis-handler.js +243 -0
- package/dist/services/memory-upgrade-bootstrap-readiness.js +251 -0
- package/dist/services/memory-upgrade-evidence.js +100 -0
- package/dist/services/memory-upgrade-inventory.js +442 -0
- package/dist/services/memory-upgrade-publication-custody.js +73 -0
- package/dist/services/memory-upgrade-publication.js +413 -0
- package/dist/services/memory-upgrade-selection.js +430 -0
- package/dist/services/memory-upgrade-start.js +60 -0
- package/dist/services/memory-upgrade-storage-readiness.js +340 -0
- package/dist/services/memory-verification-acceptance.js +156 -0
- package/dist/services/memory-verification-checks.js +317 -0
- package/dist/services/memory-verification-receipt.js +160 -0
- package/dist/services/memory-verify-handler.js +137 -0
- package/dist/services/memory-work-result-json.js +12 -0
- package/dist/services/memory-work-result-preparation.js +64 -0
- package/dist/services/merge-queue.js +228 -149
- package/dist/services/merge-server.js +163 -35
- package/dist/services/migrations.js +164 -114
- package/dist/services/model-catalog.js +109 -0
- package/dist/services/model-discovery.js +46 -0
- package/dist/services/native-daemon-history.js +50 -0
- package/dist/services/native-session-control.js +39 -0
- package/dist/services/operator-profiles.js +236 -0
- package/dist/services/physical-startup.js +45 -0
- package/dist/services/plan-runtime.js +26 -11
- package/dist/services/plans.js +28 -25
- package/dist/services/project-registry.js +97 -0
- package/dist/services/project-summary.js +1 -1
- package/dist/services/projects.js +17 -87
- package/dist/services/prompt-delivery.js +71 -0
- package/dist/services/prompts.js +28 -17
- package/dist/services/protocols.js +163 -87
- package/dist/services/provider-capacity.js +455 -0
- package/dist/services/recovery-observation-budget.js +121 -0
- package/dist/services/recovery-snapshot-database.js +588 -0
- package/dist/services/release-archive-producer.js +52 -0
- package/dist/services/release-archive.js +143 -0
- package/dist/services/release-cache.js +150 -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 +31 -0
- package/dist/services/repair-receipt.js +13 -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 +428 -0
- package/dist/services/run-control.js +1092 -0
- package/dist/services/run-controller-adapter.js +400 -0
- package/dist/services/run-controller-capture.js +186 -0
- package/dist/services/run-controller-process.js +205 -0
- package/dist/services/run-controller-recovery.js +295 -0
- package/dist/services/run-controller-state.js +97 -0
- package/dist/services/run-controller.js +1326 -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 +107 -0
- package/dist/services/run-recovery-runtime.js +112 -0
- package/dist/services/run-recovery.js +456 -0
- package/dist/services/run-start-custody.js +189 -0
- package/dist/services/runs.js +1023 -246
- package/dist/services/runtime-budget.js +446 -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 +605 -0
- package/dist/services/runtime-scope-stop.js +211 -0
- package/dist/services/runtime-scope-worker.js +314 -0
- package/dist/services/runtime-service.js +19 -14
- package/dist/services/schema-validation.js +10 -8
- package/dist/services/scope-writer-cleanup.js +27 -0
- package/dist/services/sessions.js +36 -91
- package/dist/services/setup.js +427 -0
- package/dist/services/stage-blocker.js +17 -7
- package/dist/services/stage-context.js +193 -19
- package/dist/services/stage-handlers.js +89 -0
- package/dist/services/stage-lifecycle.js +152 -116
- package/dist/services/stage-pause.js +202 -91
- 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 +393 -0
- package/dist/services/version-status.js +1 -1
- package/dist/services/vnext-code-review.js +235 -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 +355 -21
- package/dist/services/vnext-merge.js +332 -111
- package/dist/services/vnext-plan-review.js +287 -132
- package/dist/services/vnext-plan.js +228 -104
- package/dist/services/vnext-protocolize.js +533 -95
- package/dist/services/vnext-specify.js +123 -81
- package/dist/services/vnext-workspace-policy.js +7 -2
- package/dist/services/work-registry.js +1706 -200
- package/dist/services/workspace-bootstrap.js +53 -15
- package/dist/services/workspace-files.js +85 -0
- package/dist/services/worktrees.js +105 -48
- package/dist/shared/entity-references.js +1 -0
- package/dist/shared/errors.js +27 -0
- package/dist/storage/agent-output.js +26 -0
- package/dist/storage/data-preservation.js +65 -0
- package/dist/storage/database.js +930 -40
- package/dist/storage/foreign-key-transaction.js +39 -0
- package/dist/storage/immutable-input.js +45 -0
- package/dist/storage/import-guard.js +159 -0
- package/dist/storage/memory-upgrade-schema-bootstrap.js +38 -0
- package/dist/storage/native-cleanup.js +52 -0
- package/dist/storage/paired-writer-migration.js +424 -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 +96 -0
- package/dist/storage/writer-migration.js +346 -0
- package/dist/storage/writer-restore.js +217 -0
- package/package.json +50 -13
- package/tools/create-canon-release.mjs +33 -0
- package/tools/probe-codex-history.mjs +37 -0
- package/tools/produce-hybrid-release.mjs +41 -0
- package/tools/repair-paused-run-status.mjs +84 -0
|
@@ -2,14 +2,43 @@ import crypto from "node:crypto";
|
|
|
2
2
|
import { spawnSync } from "node:child_process";
|
|
3
3
|
import fs from "node:fs";
|
|
4
4
|
import path from "node:path";
|
|
5
|
+
import { DatabaseSync } from "node:sqlite";
|
|
6
|
+
import { registerStoreWriter } from "../storage/writer-contract.js";
|
|
7
|
+
import { beginSnapshotImport, completeSnapshotImport } from "../storage/import-guard.js";
|
|
8
|
+
import { foreignKeyTransaction } from "../storage/foreign-key-transaction.js";
|
|
5
9
|
import { createContext } from "../runtime/context.js";
|
|
6
10
|
import { AppError } from "../shared/errors.js";
|
|
7
|
-
import { resolveProjectRoot } from "../storage/paths.js";
|
|
11
|
+
import { canonicalPath, resolveProjectRoot, runtimeProtocolDir, runtimeRunDir } from "../storage/paths.js";
|
|
8
12
|
import { registerProject, requireProjectByRoot } from "./projects.js";
|
|
9
|
-
import { getFlowRunStatus, startFlowRun } from "./runs.js";
|
|
13
|
+
import { freezeRunExecutionRouting, getFlowRunStatus, importFlowRunExecutionProfile, startFlowRun } from "./runs.js";
|
|
14
|
+
import { loadExecutionRoutingInput, resolveExecutionPolicy } from "./execution-policy.js";
|
|
15
|
+
import { validateRunOperationalDecision } from "./vnext-execution-profile.js";
|
|
10
16
|
import { readVnextFlowDefinition } from "./vnext-specify.js";
|
|
17
|
+
import { captureRecoveryDatabase, readRecoveryResourceEvidence } from "./recovery-snapshot-database.js";
|
|
18
|
+
import { resourceHome } from "./managed-processes.js";
|
|
19
|
+
import { getResourceDatabase } from "../storage/database.js";
|
|
20
|
+
import { codexHomeSharedEntries } from "./hooks.js";
|
|
21
|
+
import { prepareOutputFile } from "../cli/input-preparation.js";
|
|
22
|
+
import { localEnvironmentAllowed, projectServicePathFilter } from "./workspace-files.js";
|
|
23
|
+
import { cleanupScopeWriters } from "./scope-writer-cleanup.js";
|
|
24
|
+
import { PreparedInstalledFlowSource } from "./installed-flow-source.js";
|
|
25
|
+
import { readSetupStatus } from "./setup.js";
|
|
26
|
+
import { isDiagnosticFlow } from "../domain/diagnostic-flows.js";
|
|
27
|
+
import { isObservationOperation, nativeInventoryVersion, retainedDaemonOperations } from "./run-control.js";
|
|
11
28
|
/** Capture the only flow boundary which exists before `stage start --bootstrap` creates a RUN. */
|
|
12
|
-
export function
|
|
29
|
+
export function prepareEvalBootstrapSnapshot(context, input) {
|
|
30
|
+
let source;
|
|
31
|
+
if (input.flow !== undefined && input.flow !== "sdlc") {
|
|
32
|
+
if (input.flow !== "mb-init" && input.flow !== "mb-upgrade" && !isDiagnosticFlow(input.flow))
|
|
33
|
+
throw new AppError("snapshot_flow_unsupported", "Bootstrap snapshot flow is not registered", 2, { phase: "prepare", effect: "no_effect" });
|
|
34
|
+
const home = context.env.DD_FLOW_CONFIG_HOME ?? context.ddFlowHome;
|
|
35
|
+
const selected = readSetupStatus(home);
|
|
36
|
+
if (!selected.installation)
|
|
37
|
+
throw new AppError("snapshot_flow_unavailable", "Bootstrap snapshot requires the selected installed flow", 2, { phase: "prepare", effect: "no_effect" });
|
|
38
|
+
source = PreparedInstalledFlowSource.fromInstalledRelease(home, selected.installation.manifest_sha256, input.flow);
|
|
39
|
+
if (source.resolveRegisteredFlow().descriptor.stages[0]?.id !== "preflight")
|
|
40
|
+
throw new AppError("snapshot_flow_unsupported", "Memory bootstrap requires its preflight entry", 2, { phase: "prepare", effect: "no_effect" });
|
|
41
|
+
}
|
|
13
42
|
const projectRoot = resolveProjectRoot(input.projectRoot);
|
|
14
43
|
const output = path.resolve(input.output);
|
|
15
44
|
if (fs.existsSync(output))
|
|
@@ -18,44 +47,161 @@ export function createEvalBootstrapSnapshot(context, input) {
|
|
|
18
47
|
throw new AppError("snapshot_output_inside_project", "Bootstrap snapshot output must be outside the project tree", 1, { output });
|
|
19
48
|
if (isInside(physicalPath(context.ddFlowHome), physicalPath(output)))
|
|
20
49
|
throw new AppError("snapshot_output_inside_runtime", "Bootstrap snapshot output must be outside DD_FLOW_HOME", 1, { output });
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
|
|
50
|
+
prepareOutputFile(output);
|
|
51
|
+
try {
|
|
52
|
+
prepareSnapshotProjectGit(projectRoot);
|
|
53
|
+
}
|
|
54
|
+
catch (error) {
|
|
55
|
+
if (!(error instanceof AppError) || error.code !== "snapshot_project_git_invalid")
|
|
56
|
+
throw error;
|
|
57
|
+
throw new AppError(error.code, error.message, 2, { ...error.details, phase: "prepare", effect: "no_effect", recoverable: true });
|
|
58
|
+
}
|
|
59
|
+
return { projectRoot, output, source };
|
|
60
|
+
}
|
|
61
|
+
export function createEvalBootstrapSnapshot(context, input) {
|
|
62
|
+
const { projectRoot, output, source } = prepareEvalBootstrapSnapshot(context, input);
|
|
63
|
+
const temporary = `${output}.partial-${crypto.randomUUID()}`;
|
|
64
|
+
const before = bootstrapSourceVersion(projectRoot);
|
|
65
|
+
try {
|
|
66
|
+
fs.mkdirSync(temporary, { recursive: true });
|
|
67
|
+
fs.cpSync(projectRoot, path.join(temporary, "project"), { recursive: true, verbatimSymlinks: true, filter: projectPayloadFilter(projectRoot) });
|
|
68
|
+
const after = bootstrapSourceVersion(projectRoot);
|
|
69
|
+
if (before !== after)
|
|
70
|
+
throw new AppError("snapshot_source_changed", "Bootstrap source changed during snapshot capture", 1, { phase: "bootstrap", recoverable: true });
|
|
71
|
+
const manifest = {
|
|
72
|
+
schema_id: source ? "dd-eval/bootstrap-snapshot@2" : "dd-eval/bootstrap-snapshot@1",
|
|
73
|
+
stage: source?.resolveRegisteredFlow().descriptor.stages[0]?.id ?? "specify",
|
|
74
|
+
...(source ? { flow: { id: source.resolveRegisteredFlow().descriptor.id, source_sha256: source.sha256, descriptor_sha256: source.descriptorReference().sha256 } } : {}),
|
|
75
|
+
project_root: projectRoot,
|
|
76
|
+
project_sha256: treeHash(path.join(temporary, "project")),
|
|
77
|
+
project_git: snapshotProjectGit(projectRoot, temporary)
|
|
78
|
+
};
|
|
79
|
+
writeJson(path.join(temporary, "bootstrap.json"), manifest);
|
|
80
|
+
if (after !== bootstrapSourceVersion(projectRoot))
|
|
81
|
+
throw new AppError("snapshot_source_changed", "Bootstrap source changed while preserving Git evidence", 1, { phase: "bootstrap", recoverable: true });
|
|
82
|
+
source?.revalidate();
|
|
83
|
+
syncSnapshotTree(temporary);
|
|
84
|
+
fs.renameSync(temporary, output);
|
|
85
|
+
syncPath(path.dirname(output));
|
|
86
|
+
return { ok: true, snapshot: output, ...manifest };
|
|
87
|
+
}
|
|
88
|
+
catch (error) {
|
|
89
|
+
fs.rmSync(temporary, { recursive: true, force: true });
|
|
90
|
+
throw error;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
function bootstrapSourceVersion(projectRoot) {
|
|
94
|
+
return JSON.stringify([
|
|
95
|
+
treeHash(projectRoot, true),
|
|
96
|
+
treeModesHash(projectRoot, true),
|
|
97
|
+
git(projectRoot, ["symbolic-ref", "--quiet", "HEAD"]),
|
|
98
|
+
git(projectRoot, ["rev-parse", "HEAD"]),
|
|
99
|
+
indexEntries(projectRoot).toString("base64")
|
|
100
|
+
]);
|
|
32
101
|
}
|
|
33
102
|
/** Restore a pre-RUN project state; `stage start --bootstrap` creates runtime state afterwards. */
|
|
34
|
-
export function
|
|
103
|
+
export function prepareEvalBootstrapRestore(input) {
|
|
35
104
|
const snapshot = path.resolve(input.snapshot);
|
|
36
105
|
const manifest = readBootstrapManifest(snapshot);
|
|
37
106
|
const projectRoot = resolveProjectRoot(input.projectRoot);
|
|
107
|
+
assertRestoreTargetSeparation(snapshot, [projectRoot]);
|
|
38
108
|
const sourceProject = path.join(snapshot, "project");
|
|
39
|
-
|
|
40
|
-
|
|
109
|
+
verifyGitSnapshotPayload(snapshot, { name: "project", hash: manifest.project_sha256, git: manifest.project_git });
|
|
110
|
+
return { snapshot, manifest, projectRoot, sourceProject };
|
|
111
|
+
}
|
|
112
|
+
export function restoreEvalBootstrapSnapshot(_context, input, prepared = prepareEvalBootstrapRestore(input)) {
|
|
113
|
+
const { snapshot, manifest, projectRoot, sourceProject } = prepared;
|
|
114
|
+
assertRestoreTargetSeparation(snapshot, [projectRoot]);
|
|
41
115
|
clearProjectTree(projectRoot, false);
|
|
42
116
|
restoreProjectGit(snapshot, manifest.project_git, projectRoot);
|
|
43
117
|
replaceProjectTree(projectRoot, sourceProject);
|
|
44
|
-
return { ok: true, schema_id: "dd-eval/bootstrap-restore@1", snapshot, project_root: projectRoot, target_stage:
|
|
118
|
+
return { ok: true, schema_id: "dd-eval/bootstrap-restore@1", snapshot, project_root: projectRoot, target_stage: manifest.stage, ...(manifest.flow ? { flow: manifest.flow } : {}) };
|
|
45
119
|
}
|
|
46
120
|
/**
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
* to infer a safe subset of SQLite rows.
|
|
121
|
+
* Eval fixtures require a dedicated home. Sealed recovery exports instead
|
|
122
|
+
* retain only the selected RUN's relational and filesystem evidence.
|
|
50
123
|
*/
|
|
51
124
|
export function createEvalRunSnapshot(context, input) {
|
|
125
|
+
const { output } = prepareEvalRunSnapshotCreation(context, input);
|
|
126
|
+
const temporary = `${output}.partial-${crypto.randomUUID()}`;
|
|
127
|
+
let sourceVersion = null;
|
|
128
|
+
try {
|
|
129
|
+
input.boundary?.verify();
|
|
130
|
+
sourceVersion = input.boundary ? snapshotSourceVersion(context, input) : null;
|
|
131
|
+
const result = captureEvalRunSnapshot(context, { ...input, ...(sourceVersion ? { journalCuts: sourceVersion.journals, observationCuts: sourceVersion.observations } : {}), output: temporary });
|
|
132
|
+
if (input.boundary && sourceVersion) {
|
|
133
|
+
Object.assign(result, { consistency: "controller_boundary_writer_barrier@1", boundary_capture: { controller_id: input.boundary.controllerId, operation_id: input.boundary.operationId, boundary_key: input.boundary.boundaryKey, source_sha256: sourceVersion.sha256, observation: input.boundary.observation } });
|
|
134
|
+
writeJson(path.join(temporary, "snapshot.json"), Object.fromEntries(Object.entries(result).filter(([key]) => key !== "ok" && key !== "snapshot")));
|
|
135
|
+
}
|
|
136
|
+
if (input.recoveryId) {
|
|
137
|
+
const projectRoot = resolveProjectRoot(input.projectRoot);
|
|
138
|
+
const project = requireProjectByRoot(context, projectRoot);
|
|
139
|
+
const run = context.db.get("SELECT workspace_root FROM runs WHERE project_id = ? AND id = ?", [project.id, input.runId]);
|
|
140
|
+
verifyRecoverySnapshot(temporary, { runId: input.runId, recoveryId: input.recoveryId, projectRoot, workspaceRoot: run?.workspace_root ?? projectRoot });
|
|
141
|
+
}
|
|
142
|
+
if (fs.existsSync(output))
|
|
143
|
+
throw new AppError("snapshot_exists", "Snapshot output already exists", 1, { output });
|
|
144
|
+
syncSnapshotTree(temporary);
|
|
145
|
+
if (input.boundary) {
|
|
146
|
+
if (sourceVersion) {
|
|
147
|
+
let after;
|
|
148
|
+
try {
|
|
149
|
+
after = snapshotSourceVersion(context, input, sourceVersion.databases, sourceVersion.journals);
|
|
150
|
+
}
|
|
151
|
+
catch (error) {
|
|
152
|
+
if (error.code !== "ENOENT")
|
|
153
|
+
throw error;
|
|
154
|
+
const missing = error.path;
|
|
155
|
+
throw new AppError("snapshot_source_changed", "Snapshot source disappeared during boundary capture", 1, { components: [missing && path.isAbsolute(missing) && isInside(context.ddFlowHome, missing) ? `runtime/${path.relative(context.ddFlowHome, missing)}` : "source_path_disappeared"] });
|
|
156
|
+
}
|
|
157
|
+
if (sourceVersion.sha256 !== after.sha256)
|
|
158
|
+
throw new AppError("snapshot_source_changed", "Snapshot source files or Git state changed during boundary capture", 1, { components: snapshotSourceChanges(sourceVersion.entries, after.entries), changes: snapshotSourceChangeProofs(sourceVersion.entries, after.entries) });
|
|
159
|
+
}
|
|
160
|
+
input.boundary.verify();
|
|
161
|
+
}
|
|
162
|
+
fs.renameSync(temporary, output);
|
|
163
|
+
syncPath(path.dirname(output));
|
|
164
|
+
return { ...result, snapshot: output };
|
|
165
|
+
}
|
|
166
|
+
catch (error) {
|
|
167
|
+
fs.rmSync(temporary, { recursive: true, force: true });
|
|
168
|
+
// Native journal publication creates its directory before requested.json.
|
|
169
|
+
// Retry a gap during any capture phase, without classifying an incomplete
|
|
170
|
+
// request as read-only. Persistent missing inputs still exhaust the budget.
|
|
171
|
+
if (input.boundary && error.code === "ENOENT") {
|
|
172
|
+
const missing = error.path;
|
|
173
|
+
throw new AppError("snapshot_source_changed", "Snapshot source was not fully published during boundary capture", 1, { components: [missing && path.isAbsolute(missing) && isInside(context.ddFlowHome, missing) ? `runtime/${path.relative(context.ddFlowHome, missing)}` : "source_path_disappeared"] });
|
|
174
|
+
}
|
|
175
|
+
throw error;
|
|
176
|
+
}
|
|
177
|
+
finally {
|
|
178
|
+
for (const db of sourceVersion?.databases.values() ?? [])
|
|
179
|
+
db.close();
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
export function prepareEvalRunSnapshotCreation(context, input) {
|
|
183
|
+
if (Number(Boolean(input.candidate)) + Number(Boolean(input.incomplete)) + Number(input.stageEntry !== undefined) + Number(input.recoveryId !== undefined) !== 1)
|
|
184
|
+
throw new AppError("usage", "Use exactly one of candidate, incomplete, recoveryId or stageEntry", 2, { phase: "prepare", effect: "no_effect", recoverable: true });
|
|
52
185
|
const projectRoot = resolveProjectRoot(input.projectRoot);
|
|
53
186
|
// Snapshot is read-only at the control-plane boundary. An invalid workspace
|
|
54
187
|
// root must fail without registering a second project in a dedicated home.
|
|
55
188
|
const project = requireProjectByRoot(context, projectRoot);
|
|
56
189
|
const status = getFlowRunStatus(context, { projectRoot, runId: input.runId });
|
|
57
|
-
|
|
58
|
-
if (input.
|
|
190
|
+
const recovery = typeof input.recoveryId === "string";
|
|
191
|
+
if (!recovery && !input.incomplete && context.db.get("SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'run_controllers'")) {
|
|
192
|
+
const controller = context.db.get("SELECT controller_id,status,owner_pid,owner_token FROM run_controllers WHERE project_id = ? AND run_id = ? AND status NOT IN ('completed','cancelled','superseded','stop_target_reached')", [project.id, input.runId]);
|
|
193
|
+
if (controller && (controller.status !== "capturing" || controller.controller_id !== input.boundary?.controllerId || controller.owner_pid !== process.pid || controller.owner_token !== context.env.DD_FLOW_CONTROLLER_TOKEN))
|
|
194
|
+
throw new AppError("snapshot_controller_capture_required", "A live managed RUN must be captured by its fenced controller, not an external observer", 1, { controller_id: controller.controller_id });
|
|
195
|
+
}
|
|
196
|
+
if (!recovery)
|
|
197
|
+
assertDedicatedHome(context, project.id, input.runId);
|
|
198
|
+
if (recovery) {
|
|
199
|
+
const guard = context.db.get("SELECT status, recovery_id FROM run_recovery_guards WHERE project_id = ? AND run_id = ? AND recovery_id = ?", [project.id, input.runId, input.recoveryId]);
|
|
200
|
+
if (!guard || guard.status !== "sealed")
|
|
201
|
+
throw new AppError("recovery_invalid_state", "Recovery snapshots require the matching sealed recovery guard", 1, { run_id: input.runId, recovery_id: input.recoveryId ?? null, status: guard?.status ?? null });
|
|
202
|
+
}
|
|
203
|
+
if (input.incomplete || recovery) { /* Recovery captures only after the runner seals all writers. */ }
|
|
204
|
+
else if (input.candidate)
|
|
59
205
|
assertTerminalCandidate(status.index.stage_runs ?? []);
|
|
60
206
|
else if (input.stageEntry)
|
|
61
207
|
assertStageEntry(status.index.stage_runs ?? [], input.stageEntry);
|
|
@@ -65,7 +211,7 @@ export function createEvalRunSnapshot(context, input) {
|
|
|
65
211
|
const activeChildren = input.candidate
|
|
66
212
|
? context.db.get("SELECT COUNT(*) AS count FROM works WHERE project_id = ? AND run_id = ? AND parent_work_id IS NOT NULL AND status IN ('created', 'running', 'paused')", [project.id, input.runId])?.count ?? 0
|
|
67
213
|
: context.db.get("SELECT COUNT(*) AS count FROM works WHERE project_id = ? AND run_id = ? AND parent_work_id IS NOT NULL AND (status IN ('running', 'paused') OR (status = 'created' AND (? IS NULL OR COALESCE(result_schema, '') NOT LIKE ?)))", [project.id, input.runId, allowedCreatedResultSchema, allowedCreatedResultSchema ? `${allowedCreatedResultSchema}@%` : ""])?.count ?? 0;
|
|
68
|
-
if (activeChildren > 0)
|
|
214
|
+
if (!input.incomplete && !recovery && activeChildren > 0)
|
|
69
215
|
throw new AppError("snapshot_not_quiescent", `${input.candidate ? "Candidate" : "Stage-entry"} snapshot requires no active child Work`, 1, { run_id: input.runId, active_children: activeChildren });
|
|
70
216
|
const output = path.resolve(input.output);
|
|
71
217
|
if (fs.existsSync(output))
|
|
@@ -75,36 +221,97 @@ export function createEvalRunSnapshot(context, input) {
|
|
|
75
221
|
const workspaceRoot = String(status.run.workspace_root ?? projectRoot);
|
|
76
222
|
if (isInside(physicalPath(projectRoot), physicalPath(output)) || isInside(physicalPath(workspaceRoot), physicalPath(output)))
|
|
77
223
|
throw new AppError("snapshot_output_inside_project", "Snapshot output must be outside the project and RUN workspace trees", 1, { output });
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
});
|
|
224
|
+
prepareOutputFile(output);
|
|
225
|
+
prepareSnapshotProjectGit(projectRoot, recovery && status.run.flow_kind === "mb-init");
|
|
226
|
+
prepareSnapshotWorkspace(workspaceRoot, projectRoot, isDiagnosticFlow(status.run.flow_kind));
|
|
227
|
+
return { projectRoot, project, status, recovery, output, workspaceRoot, activeChildren };
|
|
228
|
+
}
|
|
229
|
+
function captureEvalRunSnapshot(context, input) {
|
|
230
|
+
const { projectRoot, project, status, recovery, output, workspaceRoot, activeChildren } = prepareEvalRunSnapshotCreation(context, input);
|
|
231
|
+
fs.mkdirSync(output, { recursive: true, mode: 0o700 });
|
|
232
|
+
const resourceDatabase = path.join(context.ddFlowHome, "runtime.sqlite");
|
|
233
|
+
const databaseFiles = [context.db.path, resourceDatabase].flatMap(file => [file, `${file}-wal`, `${file}-shm`]);
|
|
234
|
+
const codexExclusions = [...codexHomeSnapshotExclusions(context, project.id, input.runId), ...input.journalCuts?.keys() ?? [], ...input.observationCuts?.keys() ?? []];
|
|
235
|
+
if (recovery) {
|
|
236
|
+
if (typeof status.run.run_root !== "string" || !isInside(physicalPath(context.ddFlowHome), physicalPath(status.run.run_root)) || samePath(physicalPath(context.ddFlowHome), physicalPath(status.run.run_root)))
|
|
237
|
+
throw new AppError("snapshot_run_scope_invalid", "Recovery requires a RUN artifact root within its runtime", 1);
|
|
238
|
+
for (const source of new Set([status.run.run_root, runtimeRunDir(context.ddFlowHome, project.id, input.runId)])) {
|
|
239
|
+
if (fs.existsSync(source))
|
|
240
|
+
copyRuntimeTree(source, path.join(output, "runtime", path.relative(context.ddFlowHome, source)), true, codexExclusions, [], input.observationCuts);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
else
|
|
244
|
+
copyRuntimeTree(context.ddFlowHome, path.join(output, "runtime"), false, [...databaseFiles, ...codexExclusions, ...(isDiagnosticFlow(status.run.flow_kind) ? [workspaceRoot] : [])], grokProviderHomes(context.ddFlowHome), input.observationCuts);
|
|
245
|
+
for (const [file, cut] of input.journalCuts ?? []) {
|
|
246
|
+
const target = path.join(output, "runtime", path.relative(context.ddFlowHome, file));
|
|
247
|
+
fs.mkdirSync(path.dirname(target), { recursive: true });
|
|
248
|
+
fs.writeFileSync(target, cut.payload, { mode: cut.mode });
|
|
249
|
+
}
|
|
250
|
+
// VACUUM INTO is a consistent SQLite read snapshot, including committed WAL
|
|
251
|
+
// pages. Copying the live database and its WAL separately is not atomic.
|
|
252
|
+
const databaseCopy = path.join(output, "runtime", path.relative(context.ddFlowHome, context.db.path));
|
|
253
|
+
fs.mkdirSync(path.dirname(databaseCopy), { recursive: true });
|
|
254
|
+
if (recovery) {
|
|
255
|
+
const report = typeof status.run.run_root === "string" ? path.join(output, "runtime", path.relative(context.ddFlowHome, status.run.run_root), "02-protocolize", "stage-report.json") : null;
|
|
256
|
+
if (report && !isInside(physicalPath(path.join(output, "runtime")), physicalPath(report)))
|
|
257
|
+
throw new AppError("snapshot_protocol_scope_invalid", "Recovery protocol evidence is outside the captured runtime", 1);
|
|
258
|
+
const protocols = report && fs.existsSync(report) ? JSON.parse(fs.readFileSync(report, "utf8")).semantic?.acceptance : [];
|
|
259
|
+
if (!Array.isArray(protocols) || !protocols.every(id => typeof id === "string" && id.startsWith("PRT-")))
|
|
260
|
+
throw new AppError("snapshot_protocol_scope_invalid", "Recovery export requires the accepted PROTOCOLIZE protocol IDs", 1);
|
|
261
|
+
captureRecoveryDatabase(context.db, databaseCopy, project.id, input.runId, protocols);
|
|
262
|
+
}
|
|
263
|
+
else
|
|
264
|
+
context.db.exec(`VACUUM INTO '${databaseCopy.replace(/'/g, "''")}'`);
|
|
265
|
+
// Resource heartbeats can continue after productive RUN writers are sealed.
|
|
266
|
+
// Keep their committed evidence consistent too; never copy a live WAL pair.
|
|
267
|
+
if (!recovery && fs.existsSync(resourceDatabase)) {
|
|
268
|
+
const resources = new DatabaseSync(resourceDatabase, { readOnly: true });
|
|
269
|
+
try {
|
|
270
|
+
const resourceCopy = path.join(output, "runtime", "runtime.sqlite");
|
|
271
|
+
resources.exec("PRAGMA busy_timeout = 4000");
|
|
272
|
+
resources.exec(`VACUUM INTO '${resourceCopy.replace(/'/g, "''")}'`);
|
|
273
|
+
}
|
|
274
|
+
finally {
|
|
275
|
+
resources.close();
|
|
276
|
+
}
|
|
277
|
+
}
|
|
88
278
|
fs.cpSync(projectRoot, path.join(output, "project"), {
|
|
89
279
|
recursive: true,
|
|
90
280
|
verbatimSymlinks: true,
|
|
91
|
-
filter: (
|
|
281
|
+
filter: projectPayloadFilter(projectRoot)
|
|
92
282
|
});
|
|
93
|
-
const projectGit = snapshotProjectGit(projectRoot, output);
|
|
94
|
-
const workspace = snapshotWorkspace(workspaceRoot, projectRoot, output);
|
|
283
|
+
const projectGit = snapshotProjectGit(projectRoot, output, recovery && status.run.flow_kind === "mb-init");
|
|
284
|
+
const workspace = snapshotWorkspace(workspaceRoot, projectRoot, output, isDiagnosticFlow(status.run.flow_kind));
|
|
285
|
+
// Accepted Work results are captured in SQLite. Their file representations
|
|
286
|
+
// are retained evidence, not a second authority or a checkpoint gate.
|
|
287
|
+
if (recovery) {
|
|
288
|
+
const copiedContext = createContext({ ...context.env, DD_FLOW_HOME: path.join(output, "runtime") }, "read_existing");
|
|
289
|
+
try {
|
|
290
|
+
captureRecoveryAuxiliary(context, copiedContext, { projectId: project.id, runId: input.runId, output });
|
|
291
|
+
}
|
|
292
|
+
finally {
|
|
293
|
+
copiedContext.db.close?.();
|
|
294
|
+
}
|
|
295
|
+
}
|
|
95
296
|
const manifest = {
|
|
96
|
-
schema_id: "dd-flow/eval-run-snapshot@5",
|
|
297
|
+
schema_id: workspace.kind === "git_repository" ? "dd-flow/eval-run-snapshot@7" : projectGit.state === "unborn" ? "dd-flow/eval-run-snapshot@6" : "dd-flow/eval-run-snapshot@5",
|
|
97
298
|
run_id: input.runId,
|
|
98
299
|
project_id: project.id,
|
|
99
300
|
project_root: projectRoot,
|
|
100
301
|
dd_flow_home: context.ddFlowHome,
|
|
101
|
-
purpose: input.candidate ? "candidate" : "stage_entry",
|
|
102
|
-
stage_entry: input.candidate ? null : input.stageEntry,
|
|
302
|
+
purpose: input.incomplete ? "incomplete" : recovery ? "recovery" : input.candidate ? "candidate" : "stage_entry",
|
|
303
|
+
stage_entry: input.incomplete || recovery || input.candidate ? null : input.stageEntry,
|
|
304
|
+
...(input.incomplete ? { consistency: "sqlite_read_snapshot_files_copied_nonatomically", active_child_work_count: activeChildren, restorable: false } : recovery ? { recovery_id: input.recoveryId, consistency: "sealed_writer_barrier_required", active_child_work_count: activeChildren, restorable: "same-runtime-or-explicit-restore" } : {}),
|
|
103
305
|
created_at: context.now(),
|
|
104
306
|
runtime_sha256: treeHash(path.join(output, "runtime")),
|
|
105
307
|
project_sha256: treeHash(path.join(output, "project")),
|
|
308
|
+
project_payload_policy: "service-files@1",
|
|
309
|
+
...(!recovery ? { runtime_payload_policy: "flow-evidence@1" } : {}),
|
|
310
|
+
...(input.journalCuts ? { journal_cuts: [...input.journalCuts].map(([file, cut]) => ({ path: path.relative(context.ddFlowHome, file), bytes: cut.payload.length, sha256: cut.sha256 })) } : {}),
|
|
311
|
+
...(input.observationCuts ? { observation_cuts: [...input.observationCuts].map(([file, receipt]) => ({ path: path.relative(context.ddFlowHome, file), receipt })) } : {}),
|
|
106
312
|
project_git: projectGit,
|
|
107
313
|
workspace,
|
|
314
|
+
payload_modes: { project: treeModesHash(path.join(output, "project")), runtime: treeModesHash(path.join(output, "runtime")), ...(workspace.kind !== "project" ? { workspace: treeModesHash(path.join(output, "workspace")) } : {}) },
|
|
108
315
|
source_status: status
|
|
109
316
|
};
|
|
110
317
|
writeJson(path.join(output, "snapshot.json"), manifest);
|
|
@@ -118,57 +325,251 @@ function createdWorkSchemaForStage(stage) {
|
|
|
118
325
|
return "dd-flow/merge-result";
|
|
119
326
|
return null;
|
|
120
327
|
}
|
|
121
|
-
|
|
122
|
-
|
|
328
|
+
/** Validate sealed source data before any target cleanup or copy. */
|
|
329
|
+
export function prepareEvalRunSnapshot(snapshot) {
|
|
123
330
|
const manifest = readManifest(snapshot);
|
|
331
|
+
if (manifest.purpose === "incomplete")
|
|
332
|
+
throw new AppError("snapshot_incomplete_not_restorable", "Incomplete snapshot is forensic evidence, not a stage-entry fixture", 1, { snapshot });
|
|
124
333
|
if (manifest.purpose === "candidate")
|
|
125
334
|
throw new AppError("snapshot_candidate_not_restorable", "A terminal candidate snapshot is evidence, not a stage-entry fixture", 1, { snapshot });
|
|
126
|
-
|
|
335
|
+
verifySnapshotPayload(snapshot, manifest);
|
|
336
|
+
if (manifest.purpose === "stage_entry") {
|
|
337
|
+
const db = new DatabaseSync(path.join(snapshot, "runtime", "db.sqlite"), { readOnly: true });
|
|
338
|
+
try {
|
|
339
|
+
assertSnapshotRuntimeDetachAllowed(sql => db.prepare(sql).get());
|
|
340
|
+
const row = db.prepare("SELECT index_json FROM runs WHERE id = ?").get(manifest.run_id);
|
|
341
|
+
if (!row)
|
|
342
|
+
throw new AppError("snapshot_invalid", "Snapshot RUN is not registered", 1);
|
|
343
|
+
const index = JSON.parse(row.index_json);
|
|
344
|
+
if (!Array.isArray(index?.stage_runs))
|
|
345
|
+
throw new AppError("snapshot_invalid", "Snapshot RUN index is invalid", 1);
|
|
346
|
+
assertStageEntry(index.stage_runs, manifest.stage_entry);
|
|
347
|
+
}
|
|
348
|
+
finally {
|
|
349
|
+
db.close();
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
return manifest;
|
|
353
|
+
}
|
|
354
|
+
export function prepareEvalRunRestore(context, input, preparedManifest) {
|
|
355
|
+
const snapshot = path.resolve(input.snapshot);
|
|
356
|
+
const manifest = preparedManifest ?? prepareEvalRunSnapshot(snapshot);
|
|
357
|
+
if (manifest.purpose === "incomplete")
|
|
358
|
+
throw new AppError("snapshot_incomplete_not_restorable", "Incomplete snapshot is forensic evidence, not a stage-entry fixture", 1, { snapshot });
|
|
359
|
+
const recovery = manifest.purpose === "recovery";
|
|
360
|
+
if (recovery && input.executionRoutingFile !== undefined)
|
|
361
|
+
throw new AppError("snapshot_recovery_policy_frozen", "Recovery must retain its frozen execution policy", 2);
|
|
362
|
+
if (recovery && input.operationalDecisionFile !== undefined)
|
|
363
|
+
throw new AppError("snapshot_recovery_policy_frozen", "Recovery cannot replace its accepted operational decision", 2);
|
|
364
|
+
if (recovery && input.recoveryId !== manifest.recovery_id)
|
|
365
|
+
throw new AppError("snapshot_recovery_requires_explicit_restore", "Recovery snapshots require their explicit --recovery-id", 1, { snapshot });
|
|
366
|
+
if (!recovery && input.recoveryId)
|
|
367
|
+
throw new AppError("snapshot_invalid", "Recovery ID cannot restore a non-recovery snapshot", 1, { snapshot });
|
|
368
|
+
if (manifest.purpose === "candidate")
|
|
369
|
+
throw new AppError("snapshot_candidate_not_restorable", "A terminal candidate snapshot is evidence, not a stage-entry fixture", 1, { snapshot });
|
|
370
|
+
if (!recovery && !manifest.stage_entry)
|
|
127
371
|
throw new AppError("snapshot_invalid", "Stage-entry snapshot has no target stage", 1, { snapshot });
|
|
128
372
|
const projectRoot = resolveProjectRoot(input.projectRoot);
|
|
129
|
-
|
|
130
|
-
if (
|
|
131
|
-
|
|
373
|
+
assertRunRestoreTargets(context, input, snapshot, projectRoot, manifest);
|
|
374
|
+
if (recovery)
|
|
375
|
+
verifyRecoverySnapshot(snapshot, { runId: manifest.run_id, recoveryId: input.recoveryId, projectRoot, workspaceRoot: projectRoot }, false);
|
|
132
376
|
const sourceRuntime = path.join(snapshot, "runtime");
|
|
133
377
|
const sourceProject = path.join(snapshot, "project");
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
378
|
+
const routingInput = input.executionRoutingFile !== undefined ? loadExecutionRoutingInput(input.executionRoutingFile) : undefined;
|
|
379
|
+
const acceptedOperational = recovery ? undefined : prepareSnapshotOperationalDecision({ snapshot, runId: manifest.run_id, projectRoot, now: context.now(), ...(input.operationalDecisionFile ? { file: input.operationalDecisionFile } : {}) });
|
|
380
|
+
const operationalDecisionBytes = acceptedOperational?.bytes;
|
|
381
|
+
let selectedProfile = acceptedOperational?.profile;
|
|
382
|
+
if (routingInput) {
|
|
383
|
+
// Resolve selected profiles before replacing the caller's fresh runtime.
|
|
384
|
+
const sourceDb = new DatabaseSync(path.join(sourceRuntime, "db.sqlite"), { readOnly: true });
|
|
385
|
+
try {
|
|
386
|
+
const row = sourceDb.prepare("SELECT index_json FROM runs WHERE id = ?").get(manifest.run_id);
|
|
387
|
+
const index = row ? JSON.parse(row.index_json) : null;
|
|
388
|
+
if (!index?.execution_profile || !["vnext_specify", "vnext_protocolize"].includes(index.flow_kind))
|
|
389
|
+
throw new AppError("execution_profile_not_frozen", "Stage-entry routing requires a vNext execution snapshot", 1);
|
|
390
|
+
assertStageEntry(index.stage_runs, manifest.stage_entry);
|
|
391
|
+
selectedProfile = freezeRunExecutionRouting(context.ddFlowHome, selectedProfile ?? index.execution_profile, routingInput);
|
|
392
|
+
for (const row of sourceDb.prepare("SELECT payload_json FROM works WHERE run_id = ? AND status = 'created'").all(manifest.run_id)) {
|
|
393
|
+
const payload = row.payload_json ? JSON.parse(row.payload_json) : null;
|
|
394
|
+
if (payload?.execution_override !== undefined)
|
|
395
|
+
resolveExecutionPolicy({ ddFlowHome: context.ddFlowHome, policy: routingInput.execution, profiles: selectedProfile.agent_profiles, stage: manifest.stage_entry, work: payload.execution_override });
|
|
396
|
+
}
|
|
397
|
+
selectedProfile.routing_input.artifact = `execution-routing.${routingInput.source_checksum}.input.json`;
|
|
398
|
+
const sourceRun = sourceDb.prepare("SELECT run_root FROM runs WHERE id = ?").get(manifest.run_id);
|
|
399
|
+
if (typeof sourceRun?.run_root !== "string" || !path.isAbsolute(sourceRun.run_root) || !isInside(manifest.dd_flow_home, sourceRun.run_root))
|
|
400
|
+
throw new AppError("snapshot_invalid", "Snapshot RUN root must be inside its runtime home", 1);
|
|
401
|
+
const existingRouting = path.join(sourceRuntime, path.relative(manifest.dd_flow_home, sourceRun.run_root), selectedProfile.routing_input.artifact);
|
|
402
|
+
if (fs.existsSync(existingRouting) && !fs.readFileSync(existingRouting).equals(routingInput.bytes))
|
|
403
|
+
throw new AppError("snapshot_checksum_mismatch", "Existing routing input does not match its checksum", 1);
|
|
404
|
+
}
|
|
405
|
+
finally {
|
|
406
|
+
sourceDb.close();
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
return { snapshot, manifest, recovery, projectRoot, sourceRuntime, sourceProject, routingInput, selectedProfile, operationalDecisionBytes };
|
|
410
|
+
}
|
|
411
|
+
/** Shared no-effect scope preparation for direct restore and run fork. */
|
|
412
|
+
export function prepareSnapshotOperationalDecision(input) {
|
|
413
|
+
const sourceDb = new DatabaseSync(path.join(input.snapshot, "runtime", "db.sqlite"), { readOnly: true });
|
|
148
414
|
try {
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
next_command: `dd-flow stage start ${status.run.id} --stage ${manifest.stage_entry} --project-root ${JSON.stringify(projectRoot)} --json`
|
|
168
|
-
};
|
|
415
|
+
const row = sourceDb.prepare("SELECT index_json FROM runs WHERE id = ?").get(input.runId);
|
|
416
|
+
const profile = row ? JSON.parse(row.index_json).execution_profile : undefined;
|
|
417
|
+
if (profile?.operational_decision && !input.file)
|
|
418
|
+
throw new AppError("snapshot_operational_acceptance_required", "A new stage-entry execution needs its own accepted operational decision", 2);
|
|
419
|
+
if (!input.file)
|
|
420
|
+
return undefined;
|
|
421
|
+
if (!profile)
|
|
422
|
+
throw new AppError("execution_profile_not_frozen", "Operational stage-entry decision needs a frozen vNext profile", 2);
|
|
423
|
+
const bytes = fs.readFileSync(input.file);
|
|
424
|
+
const decision = validateRunOperationalDecision(JSON.parse(bytes.toString("utf8")), profile.settings);
|
|
425
|
+
if (canonicalPath(decision.scope.project_root, false) !== canonicalPath(input.projectRoot, false))
|
|
426
|
+
throw new AppError("operational_decision_scope_mismatch", "Imported decision must name the new project root", 2);
|
|
427
|
+
const sourceProject = path.join(input.snapshot, "project"), policyFile = path.resolve(sourceProject, decision.policy.path);
|
|
428
|
+
if (!isInside(sourceProject, policyFile) || !fs.existsSync(policyFile) || !isInside(fs.realpathSync(sourceProject), fs.realpathSync(policyFile)) || fileHash(policyFile) !== decision.policy.sha256)
|
|
429
|
+
throw new AppError("operational_decision_policy_changed", "Imported decision policy must match the owned frozen project source", 2);
|
|
430
|
+
if (decision.exception?.expires_at && Date.parse(decision.exception.expires_at) <= Date.parse(input.now))
|
|
431
|
+
throw new AppError("operational_decision_expired", "Imported operational decision has explicitly expired", 2);
|
|
432
|
+
return { profile: { ...profile, operational_decision: decision }, bytes };
|
|
169
433
|
}
|
|
170
434
|
finally {
|
|
171
|
-
|
|
435
|
+
sourceDb.close();
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
function assertRunRestoreTargets(context, input, snapshot, projectRoot, manifest) {
|
|
439
|
+
assertRestoreTargetSeparation(snapshot, [projectRoot, context.ddFlowHome]);
|
|
440
|
+
if (manifest.workspace.kind !== "project") {
|
|
441
|
+
const workspaceRoot = restoredWorkspaceRoot(manifest, projectRoot, context.ddFlowHome);
|
|
442
|
+
assertRestoreTargetSeparation(snapshot, [workspaceRoot]);
|
|
443
|
+
if (isInside(physicalPath(projectRoot), physicalPath(workspaceRoot)) || isInside(physicalPath(workspaceRoot), physicalPath(projectRoot)) || physicalPath(workspaceRoot) === physicalPath(context.ddFlowHome))
|
|
444
|
+
throw new AppError("snapshot_path_conflict", "Feature workspace overlaps a restore target", 2);
|
|
445
|
+
if (fs.existsSync(workspaceRoot))
|
|
446
|
+
throw new AppError("snapshot_workspace_target_exists", "Snapshot feature workspace target already exists", 1, { workspace_root: workspaceRoot });
|
|
447
|
+
}
|
|
448
|
+
if (manifest.purpose === "recovery") {
|
|
449
|
+
if (fs.readdirSync(projectRoot).length || (context.db.get("SELECT COUNT(*) AS count FROM projects")?.count ?? 0) > 0)
|
|
450
|
+
throw new AppError("recovery_restore_target_not_empty", "Recovery restore requires fresh project and runtime targets", 1);
|
|
451
|
+
}
|
|
452
|
+
const existing = (fs.existsSync(context.ddFlowHome) ? fs.readdirSync(context.ddFlowHome) : []).filter((name) => !["db.sqlite", "db.sqlite-wal", "db.sqlite-shm", ...(input.executionRoutingFile !== undefined ? ["agent-profiles"] : [])].includes(name));
|
|
453
|
+
if (existing.length > 0)
|
|
454
|
+
throw new AppError("restore_home_not_empty", "Snapshot restore requires an empty DD_FLOW_HOME", 1, { dd_flow_home: context.ddFlowHome, existing });
|
|
455
|
+
if (context.db.get("SELECT 1 FROM projects LIMIT 1") || context.db.get("SELECT 1 FROM runs LIMIT 1"))
|
|
456
|
+
throw new AppError("restore_home_not_empty", "Snapshot restore cannot replace an initialized runtime", 1);
|
|
457
|
+
}
|
|
458
|
+
export function restoreEvalRunSnapshot(context, input, prepared = prepareEvalRunRestore(context, input), prearmedImportId) {
|
|
459
|
+
const { snapshot, manifest, recovery, projectRoot, sourceRuntime, sourceProject, routingInput, selectedProfile } = prepared;
|
|
460
|
+
if (input.operationalDecisionFile && (!prepared.operationalDecisionBytes || !fs.readFileSync(input.operationalDecisionFile).equals(prepared.operationalDecisionBytes)))
|
|
461
|
+
throw new AppError("operational_decision_changed", "Accepted operational decision changed after import preparation", 2);
|
|
462
|
+
assertRunRestoreTargets(context, input, snapshot, projectRoot, manifest);
|
|
463
|
+
const sourceManifestSha256 = fileHash(path.join(snapshot, "snapshot.json"));
|
|
464
|
+
const importId = prearmedImportId ?? beginSnapshotImport({ home: context.ddFlowHome, projectRoot,
|
|
465
|
+
workspaceRoot: restoredWorkspaceRoot(manifest, projectRoot, context.ddFlowHome), sourceManifestSha256 });
|
|
466
|
+
try {
|
|
467
|
+
clearProjectTree(projectRoot, false);
|
|
468
|
+
restoreProjectGit(snapshot, manifest.project_git, projectRoot);
|
|
469
|
+
replaceProjectTree(projectRoot, sourceProject);
|
|
470
|
+
context.db.close?.();
|
|
471
|
+
fs.rmSync(context.ddFlowHome, { recursive: true, force: true });
|
|
472
|
+
fs.cpSync(sourceRuntime, context.ddFlowHome, { recursive: true, verbatimSymlinks: true });
|
|
473
|
+
const workspaceRoot = restoreWorkspace(snapshot, manifest, projectRoot, context.ddFlowHome);
|
|
474
|
+
// The dispatch context was intentionally closed before replacing db.sqlite.
|
|
475
|
+
// A fresh process owns all subsequent normal commands.
|
|
476
|
+
const restored = createContext({ ...context.env, DD_FLOW_HOME: context.ddFlowHome }, "initialize", importId);
|
|
477
|
+
let result, primary, failed = false;
|
|
478
|
+
try {
|
|
479
|
+
const runtimeLineage = recovery ? null : detachSnapshotRuntimeOwners(restored, snapshot, manifest);
|
|
480
|
+
const originalRunRoot = restored.db.get("SELECT run_root FROM runs WHERE id = ?", [manifest.run_id])?.run_root;
|
|
481
|
+
rebaseRuntime(restored, manifest.project_root, projectRoot, sourceWorkspaceRoot(manifest), workspaceRoot, manifest.dd_flow_home, context.ddFlowHome, recovery);
|
|
482
|
+
const publicationRun = restored.db.get("SELECT project_id, run_root FROM runs WHERE id = ?", [manifest.run_id]);
|
|
483
|
+
if (!originalRunRoot || !publicationRun?.run_root || !isInside(manifest.dd_flow_home, originalRunRoot) || !isInside(context.ddFlowHome, publicationRun.run_root))
|
|
484
|
+
throw new AppError("snapshot_invalid", "Restored publication roots are not bound to their runtime", 1);
|
|
485
|
+
const pathMapping = [{ from: manifest.dd_flow_home, to: context.ddFlowHome }, { from: sourceWorkspaceRoot(manifest), to: workspaceRoot }, { from: manifest.project_root, to: projectRoot }];
|
|
486
|
+
restored.db.run("INSERT INTO audit_events (project_id, event_type, forced, payload_json, created_at) VALUES (?, 'flow_run.publication_relocation', 0, ?, ?)", [publicationRun.project_id, JSON.stringify({ run_id: manifest.run_id, from: originalRunRoot, to: publicationRun.run_root, source_manifest_sha256: sourceManifestSha256, import_id: importId, path_mapping: pathMapping }), restored.now()]);
|
|
487
|
+
registerProject(restored, { root: projectRoot });
|
|
488
|
+
requireProjectByRoot(restored, projectRoot);
|
|
489
|
+
if (selectedProfile && runtimeLineage) {
|
|
490
|
+
const run = restored.db.get("SELECT run_root FROM runs WHERE id = ?", [manifest.run_id]);
|
|
491
|
+
if (routingInput) {
|
|
492
|
+
const artifact = path.join(run.run_root, selectedProfile.routing_input.artifact);
|
|
493
|
+
if (fs.existsSync(artifact)) {
|
|
494
|
+
if (!fs.readFileSync(artifact).equals(routingInput.bytes))
|
|
495
|
+
throw new AppError("snapshot_checksum_mismatch", "Existing routing input does not match its checksum", 1);
|
|
496
|
+
}
|
|
497
|
+
else
|
|
498
|
+
fs.writeFileSync(artifact, routingInput.bytes, { flag: "wx", mode: 0o600 });
|
|
499
|
+
}
|
|
500
|
+
if (input.operationalDecisionFile) {
|
|
501
|
+
if (!prepared.operationalDecisionBytes || !fs.readFileSync(input.operationalDecisionFile).equals(prepared.operationalDecisionBytes))
|
|
502
|
+
throw new AppError("operational_decision_changed", "Accepted operational decision changed before import publication", 2);
|
|
503
|
+
const binding = { project_id: publicationRun.project_id, run_id: manifest.run_id, ...selectedProfile.operational_decision.scope };
|
|
504
|
+
const policyBytes = fs.readFileSync(path.resolve(projectRoot, selectedProfile.operational_decision.policy.path));
|
|
505
|
+
const policySource = { path: `operational-policy.${selectedProfile.operational_decision.policy.sha256}.source.md`, sha256: crypto.createHash("sha256").update(policyBytes).digest("hex") };
|
|
506
|
+
if (policySource.sha256 !== selectedProfile.operational_decision.policy.sha256)
|
|
507
|
+
throw new AppError("operational_decision_policy_changed", "Imported policy changed before binding", 2);
|
|
508
|
+
if (!fs.existsSync(path.join(run.run_root, policySource.path)))
|
|
509
|
+
fs.writeFileSync(path.join(run.run_root, policySource.path), policyBytes, { flag: "wx" });
|
|
510
|
+
const bytes = `${JSON.stringify({ schema_id: "dd-flow/run-operational-contract@1", binding, settings: { merge_delivery: selectedProfile.settings.merge_delivery, merge_cleanup: selectedProfile.settings.merge_cleanup }, decision: selectedProfile.operational_decision, policy_source: policySource }, null, 2)}\n`;
|
|
511
|
+
const artifact = `operational-contract.${crypto.createHash("sha256").update(bytes).digest("hex")}.json`;
|
|
512
|
+
fs.writeFileSync(path.join(run.run_root, artifact), bytes, { flag: "wx" });
|
|
513
|
+
selectedProfile.operational_contract = { path: artifact, sha256: crypto.createHash("sha256").update(bytes).digest("hex"), policy_source: policySource, binding };
|
|
514
|
+
}
|
|
515
|
+
importFlowRunExecutionProfile(restored, { projectRoot, runId: manifest.run_id, stage: manifest.stage_entry, lineageFile: runtimeLineage, profile: selectedProfile });
|
|
516
|
+
}
|
|
517
|
+
const relocation = recovery ? {
|
|
518
|
+
schema_id: "dd-flow/recovery-relocation@1", physical_runtime_id: crypto.randomUUID(), recovery_id: input.recoveryId, run_id: manifest.run_id,
|
|
519
|
+
path_mapping: [{ from: manifest.dd_flow_home, to: context.ddFlowHome }, { from: sourceWorkspaceRoot(manifest), to: workspaceRoot }, { from: manifest.project_root, to: projectRoot }]
|
|
520
|
+
} : null;
|
|
521
|
+
const relocationFile = relocation ? path.join(context.ddFlowHome, "recovery-imports", `${relocation.physical_runtime_id}.json`) : null;
|
|
522
|
+
if (relocationFile) {
|
|
523
|
+
fs.mkdirSync(path.dirname(relocationFile), { recursive: true });
|
|
524
|
+
writeJson(relocationFile, relocation);
|
|
525
|
+
}
|
|
526
|
+
const status = getFlowRunStatus(restored, { projectRoot, runId: manifest.run_id });
|
|
527
|
+
if (!recovery)
|
|
528
|
+
assertStageEntry(status.index.stage_runs ?? [], manifest.stage_entry);
|
|
529
|
+
else
|
|
530
|
+
restored.db.run("UPDATE run_recovery_guards SET capture_path = ? WHERE recovery_id = ? AND status = 'sealed'", [snapshot, input.recoveryId]);
|
|
531
|
+
if (!status.run.run_root)
|
|
532
|
+
throw new AppError("runtime_missing", "Restored RUN has no artifact root", 1, { run_id: manifest.run_id });
|
|
533
|
+
const runHome = status.run.run_root;
|
|
534
|
+
const expectedWorkspaceRoot = restoredWorkspaceRoot(manifest, projectRoot, context.ddFlowHome);
|
|
535
|
+
if (physicalPath(workspaceRoot) !== physicalPath(expectedWorkspaceRoot))
|
|
536
|
+
throw new AppError("snapshot_workspace_restore_failed", "Restored workspace differs from the import target", 1, { workspace_root: workspaceRoot, expected_workspace_root: expectedWorkspaceRoot });
|
|
537
|
+
completeSnapshotImport(context.ddFlowHome, importId, { projectRoot, workspaceRoot: expectedWorkspaceRoot, sourceManifestSha256 });
|
|
538
|
+
result = {
|
|
539
|
+
ok: true,
|
|
540
|
+
schema_id: "dd-flow/eval-run-restore@1",
|
|
541
|
+
snapshot,
|
|
542
|
+
run_id: status.run.id,
|
|
543
|
+
project_root: projectRoot,
|
|
544
|
+
workspace_root: workspaceRoot,
|
|
545
|
+
dd_flow_home: context.ddFlowHome,
|
|
546
|
+
run_home: runHome,
|
|
547
|
+
target_stage: manifest.stage_entry,
|
|
548
|
+
...(runtimeLineage ? { runtime_lineage_file: runtimeLineage } : {}),
|
|
549
|
+
...(recovery ? { recovery_id: input.recoveryId, native_session_portability: "requires_adapter_verification", relocation_file: relocationFile, relocation } : {}),
|
|
550
|
+
next_command: recovery ? `dd-flow run recovery inspect ${status.run.id} --project-root ${JSON.stringify(projectRoot)} --json` : `dd-flow stage start ${status.run.id} --stage ${manifest.stage_entry} --project-root ${JSON.stringify(projectRoot)} --json`
|
|
551
|
+
};
|
|
552
|
+
}
|
|
553
|
+
catch (error) {
|
|
554
|
+
primary = error;
|
|
555
|
+
failed = true;
|
|
556
|
+
}
|
|
557
|
+
const cleanup = cleanupScopeWriters([{ name: restored.ddFlowHome, db: restored.db, rollback: false, close: "always" }]);
|
|
558
|
+
if (failed) {
|
|
559
|
+
if (primary instanceof AppError && cleanup.length)
|
|
560
|
+
primary.details.import_cleanup_errors = cleanup;
|
|
561
|
+
throw primary;
|
|
562
|
+
}
|
|
563
|
+
if (cleanup.length)
|
|
564
|
+
throw new AppError("snapshot_import_cleanup_failed", "Snapshot imported but its connection could not be closed", 1, { effect: "committed", import_complete: true, import_cleanup_errors: cleanup });
|
|
565
|
+
return result;
|
|
566
|
+
}
|
|
567
|
+
catch (error) {
|
|
568
|
+
// The import fence and target writes have already started. A nested
|
|
569
|
+
// prepare/read validator must not report this operation as no-effect.
|
|
570
|
+
if (error instanceof AppError)
|
|
571
|
+
Object.assign(error.details, { phase: "restore", effect: error.details.effect === "committed" ? "committed" : "partial", import_id: importId, next_action: "reconcile_snapshot_import" });
|
|
572
|
+
throw error;
|
|
172
573
|
}
|
|
173
574
|
}
|
|
174
575
|
export function prepareVnextSpecifyRun(context, input) {
|
|
@@ -182,6 +583,7 @@ export function prepareVnextSpecifyRun(context, input) {
|
|
|
182
583
|
subjectType: "discussion",
|
|
183
584
|
subjectId: "SPECIFY",
|
|
184
585
|
slug: input.slug,
|
|
586
|
+
executionRoutingFile: input.executionRoutingFile,
|
|
185
587
|
nextAction: "start_specify"
|
|
186
588
|
});
|
|
187
589
|
return { ok: true, run_id: started.run.id, run_root: started.run.run_root, target_stage: "specify" };
|
|
@@ -196,19 +598,91 @@ function assertDedicatedHome(context, projectId, runId) {
|
|
|
196
598
|
function assertStageEntry(stages, target) {
|
|
197
599
|
if (stages.some((entry) => entry.stage === target))
|
|
198
600
|
throw new AppError("snapshot_target_started", "Stage-entry snapshot requires its target stage to be unstarted", 1, { target });
|
|
199
|
-
const unfinished = stages.filter((entry) => entry.status
|
|
601
|
+
const unfinished = stages.filter((entry) => !["done", "skipped"].includes(entry.status ?? ""));
|
|
200
602
|
if (unfinished.length > 0)
|
|
201
603
|
throw new AppError("snapshot_predecessor_incomplete", "Stage-entry snapshot requires completed predecessor stages", 1, { target, unfinished });
|
|
202
604
|
}
|
|
203
605
|
function assertTerminalCandidate(stages) {
|
|
204
|
-
const unfinished = stages.filter((entry) => entry.status
|
|
606
|
+
const unfinished = stages.filter((entry) => !["done", "skipped"].includes(entry.status ?? ""));
|
|
205
607
|
if (unfinished.length > 0)
|
|
206
608
|
throw new AppError("snapshot_candidate_incomplete", "Candidate snapshot requires every started stage to be done", 1, { unfinished });
|
|
207
609
|
}
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
610
|
+
/** A stage-entry import is a new experiment, not a lease takeover or recovery.
|
|
611
|
+
* Keep source authority as evidence, never as executable target inventory. */
|
|
612
|
+
function assertSnapshotRuntimeDetachAllowed(get) {
|
|
613
|
+
if (get("SELECT 1 FROM sqlite_schema WHERE type = 'table' AND name = 'run_recovery_guards'") && get("SELECT 1 FROM run_recovery_guards WHERE status IN ('draining','sealed','resuming') LIMIT 1"))
|
|
614
|
+
throw new AppError("snapshot_control_requires_recovery", "A fenced RUN requires explicit recovery, not a stage-entry fork", 1);
|
|
615
|
+
if (get("SELECT 1 FROM merge_requests WHERE status IN ('dispatching','active','waiting_user','action_required','recovery_required') LIMIT 1"))
|
|
616
|
+
throw new AppError("snapshot_execution_active", "A stage-entry fork cannot discard an unresolved MERGE execution", 1);
|
|
617
|
+
}
|
|
618
|
+
function detachSnapshotRuntimeOwners(context, snapshot, manifest) {
|
|
619
|
+
const hasTable = (table) => Boolean(context.db.get("SELECT 1 FROM sqlite_schema WHERE type = 'table' AND name = ?", [table]));
|
|
620
|
+
assertSnapshotRuntimeDetachAllowed(sql => context.db.get(sql));
|
|
621
|
+
const detachedTables = ["lifecycle_packet_assignments", "lifecycle_invocations", "run_control_admissions", "run_control_stage_admissions", "run_control_operations", "run_control_workers", "run_controls", "run_controller_events", "run_controller_operations", "run_controllers", "run_recovery_bindings", "work_recovery_segments", "run_recovery_guards", "work_launches", "merge_servers", "merge_sessions", "lane_locks", "lane_waiters"].filter(hasTable);
|
|
622
|
+
const runTables = Object.fromEntries([...detachedTables, "runs", "merge_requests", "sessions", "work_sessions"].map(table => [table, context.db.all(`SELECT * FROM ${table}`)]));
|
|
623
|
+
const resourceFile = path.join(context.ddFlowHome, "runtime.sqlite");
|
|
624
|
+
const resources = fs.existsSync(resourceFile) ? new DatabaseSync(resourceFile) : null;
|
|
625
|
+
let resourceHeld = false, resourceCommitted = false, result, primary, failed = false;
|
|
211
626
|
try {
|
|
627
|
+
if (resources)
|
|
628
|
+
registerStoreWriter(resources);
|
|
629
|
+
const resourceTables = Object.fromEntries(["managed_resources", "managed_processes"].filter(table => resources?.prepare("SELECT 1 FROM sqlite_schema WHERE type = 'table' AND name = ?").get(table)).map(table => [table, resources.prepare(`SELECT * FROM ${table}`).all()]));
|
|
630
|
+
const directory = path.join(context.ddFlowHome, "snapshot-imports");
|
|
631
|
+
fs.mkdirSync(directory, { recursive: true, mode: 0o700 });
|
|
632
|
+
const file = path.join(directory, `${crypto.randomUUID()}.json`);
|
|
633
|
+
fs.writeFileSync(file, `${JSON.stringify({ schema_id: "dd-flow/snapshot-runtime-lineage@1", source_snapshot: snapshot, source_manifest_sha256: fileHash(path.join(snapshot, "snapshot.json")), source_runtime: manifest.dd_flow_home, restored_runtime: context.ddFlowHome, run_id: manifest.run_id, run_tables: runTables, resource_tables: resourceTables }, null, 2)}\n`, { flag: "wx", mode: 0o600 });
|
|
634
|
+
syncPath(file);
|
|
635
|
+
syncPath(directory);
|
|
636
|
+
const lineage = JSON.stringify({ lineage_file: file, lineage_sha256: fileHash(file) });
|
|
637
|
+
context.db.writeTransaction(() => {
|
|
638
|
+
assertSnapshotRuntimeDetachAllowed(sql => context.db.get(sql));
|
|
639
|
+
for (const [table, rows] of Object.entries(runTables))
|
|
640
|
+
if (JSON.stringify(context.db.all(`SELECT * FROM ${table}`)) !== JSON.stringify(rows))
|
|
641
|
+
throw new AppError("snapshot_runtime_changed", "Imported runtime changed during lineage preparation", 1, { table });
|
|
642
|
+
context.db.run("UPDATE sessions SET metadata_json = json_set(metadata_json, '$.snapshot_import', json(?))", [lineage]);
|
|
643
|
+
for (const table of detachedTables) {
|
|
644
|
+
try {
|
|
645
|
+
context.db.exec(`DELETE FROM ${table}`);
|
|
646
|
+
}
|
|
647
|
+
catch (error) {
|
|
648
|
+
throw new AppError("snapshot_runtime_detach_failed", `Could not detach imported ${table} authority`, 1, { table, cause: String(error) });
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
context.db.run("UPDATE merge_requests SET dispatch_owner = NULL, dispatch_lease_token = NULL, dispatch_lease_expires_at = NULL, lock_acquired_at = NULL, adapter_receipt_json = NULL");
|
|
652
|
+
// Only the copied dedicated registry is touched. Source/external homes
|
|
653
|
+
// are historical references in the archive and are never opened here.
|
|
654
|
+
if (resources) {
|
|
655
|
+
resources.exec("BEGIN IMMEDIATE");
|
|
656
|
+
resourceHeld = true;
|
|
657
|
+
for (const [table, rows] of Object.entries(resourceTables)) {
|
|
658
|
+
if (JSON.stringify(resources.prepare(`SELECT * FROM ${table}`).all()) !== JSON.stringify(rows))
|
|
659
|
+
throw new AppError("snapshot_runtime_changed", "Imported resource evidence changed during lineage preparation", 1, { table });
|
|
660
|
+
resources.exec(`DELETE FROM ${table}`);
|
|
661
|
+
}
|
|
662
|
+
resources.exec("COMMIT");
|
|
663
|
+
resourceHeld = false;
|
|
664
|
+
resourceCommitted = true;
|
|
665
|
+
}
|
|
666
|
+
});
|
|
667
|
+
result = file;
|
|
668
|
+
}
|
|
669
|
+
catch (error) {
|
|
670
|
+
primary = error;
|
|
671
|
+
failed = true;
|
|
672
|
+
}
|
|
673
|
+
const cleanup = resources ? cleanupScopeWriters([{ name: resourceFile, db: resources, rollback: resourceHeld, close: "always" }]) : [];
|
|
674
|
+
if (failed || cleanup.length) {
|
|
675
|
+
const details = { effect: resourceCommitted ? "partial" : "unknown", ...(cleanup.length ? { import_cleanup_errors: cleanup } : {}) };
|
|
676
|
+
if (primary instanceof AppError) {
|
|
677
|
+
Object.assign(primary.details, details);
|
|
678
|
+
throw primary;
|
|
679
|
+
}
|
|
680
|
+
throw new AppError("snapshot_runtime_detach_failed", primary instanceof Error ? primary.message : "Imported runtime authority could not be detached", 1, { ...details, ...(failed ? { cause: String(primary) } : {}) });
|
|
681
|
+
}
|
|
682
|
+
return result;
|
|
683
|
+
}
|
|
684
|
+
function rebaseRuntime(context, oldRoot, newRoot, oldWorkspace, newWorkspace, oldHome, newHome, recovery = false) {
|
|
685
|
+
foreignKeyTransaction(context.db, () => {
|
|
212
686
|
context.db.run("UPDATE projects SET root = ?", [newRoot]);
|
|
213
687
|
context.db.run("UPDATE runs SET project_root = ?, workspace_root = ?", [newRoot, newWorkspace]);
|
|
214
688
|
for (const column of ["runtime_path", "run_dir", "run_index_path", "run_home_path", "run_root", "index_json"])
|
|
@@ -216,7 +690,12 @@ function rebaseRuntime(context, oldRoot, newRoot, oldWorkspace, newWorkspace, ol
|
|
|
216
690
|
context.db.run("UPDATE protocols SET project_root = ?", [newRoot]);
|
|
217
691
|
for (const column of ["state_path", "plan_path"])
|
|
218
692
|
rewriteDatabaseColumn(context, "protocols", column, oldRoot, newRoot, oldWorkspace, newWorkspace, oldHome, newHome);
|
|
219
|
-
|
|
693
|
+
for (const column of ["source_workspace", "target_workspace"])
|
|
694
|
+
rewriteDatabaseColumn(context, "merge_requests", column, oldRoot, newRoot, oldWorkspace, newWorkspace, oldHome, newHome);
|
|
695
|
+
if (recovery)
|
|
696
|
+
context.db.run("UPDATE sessions SET project_root = ?, status = CASE WHEN status = 'active' THEN 'stopped' ELSE status END, stop_reason = CASE WHEN status = 'active' THEN 'snapshot_restored' ELSE stop_reason END", [newRoot]);
|
|
697
|
+
else
|
|
698
|
+
context.db.run("UPDATE sessions SET project_root = ?, status = 'stopped', stop_reason = 'snapshot_restored', stopped_at = ?", [newRoot, context.now()]);
|
|
220
699
|
for (const column of ["workspace_path", "cwd", "transcript_path"])
|
|
221
700
|
rewriteDatabaseColumn(context, "sessions", column, oldRoot, newRoot, oldWorkspace, newWorkspace, oldHome, newHome);
|
|
222
701
|
for (const column of ["prompt_path", "result_path"])
|
|
@@ -226,47 +705,55 @@ function rebaseRuntime(context, oldRoot, newRoot, oldWorkspace, newWorkspace, ol
|
|
|
226
705
|
rewriteDatabaseColumn(context, "usage", "source_locator", oldRoot, newRoot, oldWorkspace, newWorkspace, oldHome, newHome);
|
|
227
706
|
for (const column of ["worktree_path", "worktrunk_metadata_json", "last_command_result_json"])
|
|
228
707
|
rewriteDatabaseColumn(context, "worktree_records", column, oldRoot, newRoot, oldWorkspace, newWorkspace, oldHome, newHome);
|
|
229
|
-
|
|
708
|
+
// Recovery imports remain sealed. Their old Work sessions are historical
|
|
709
|
+
// bindings, not completed work; explicit recovery closes them interrupted.
|
|
710
|
+
if (!recovery)
|
|
711
|
+
context.db.run("UPDATE work_sessions SET status = CASE WHEN status = 'running' THEN 'completed' ELSE status END, completed_at = COALESCE(completed_at, ?), updated_at = ?", [context.now(), context.now()]);
|
|
230
712
|
context.db.run("DELETE FROM codex_session_bindings");
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
finally {
|
|
238
|
-
context.db.exec("PRAGMA foreign_keys = ON");
|
|
239
|
-
}
|
|
240
|
-
rewriteStructuredFiles(context.ddFlowHome, oldRoot, newRoot, oldWorkspace, newWorkspace, oldHome, newHome);
|
|
713
|
+
});
|
|
714
|
+
// Only operational database paths move. Imported packets, results and
|
|
715
|
+
// journals retain their historical bytes, including interrupted JSONL tails.
|
|
716
|
+
// The frozen workspace route is an operational receipt consumed by later
|
|
717
|
+
// stages, so its relocated path is the sole structured-file exception.
|
|
718
|
+
rewriteOperationalRouteReceipts(context.ddFlowHome, oldRoot, newRoot, oldWorkspace, newWorkspace, oldHome, newHome);
|
|
241
719
|
}
|
|
242
|
-
function
|
|
243
|
-
const visit = (
|
|
244
|
-
for (const entry of fs.readdirSync(
|
|
245
|
-
const file = path.join(
|
|
246
|
-
|
|
247
|
-
// They are project/tool bytes, not flow records; parsing every *.json
|
|
248
|
-
// there breaks on valid JSON-with-comments such as tsconfig files.
|
|
249
|
-
if (entry.isDirectory() && ![".git", "checkouts", "engines", "node_modules", "tools"].includes(entry.name))
|
|
720
|
+
function rewriteOperationalRouteReceipts(root, oldRoot, newRoot, oldWorkspace, newWorkspace, oldHome, newHome) {
|
|
721
|
+
const visit = (directory) => {
|
|
722
|
+
for (const entry of fs.readdirSync(directory, { withFileTypes: true })) {
|
|
723
|
+
const file = path.join(directory, entry.name);
|
|
724
|
+
if (entry.isDirectory())
|
|
250
725
|
visit(file);
|
|
251
|
-
else if (entry.isFile() && entry.name
|
|
726
|
+
else if (entry.isFile() && entry.name === "workspace-route.json") {
|
|
252
727
|
const value = JSON.parse(fs.readFileSync(file, "utf8"));
|
|
253
|
-
|
|
254
|
-
}
|
|
255
|
-
else if (entry.isFile() && entry.name.endsWith(".jsonl")) {
|
|
256
|
-
const lines = fs.readFileSync(file, "utf8").split("\n").filter(Boolean).map((line) => JSON.stringify(rebaseStructuredValue(JSON.parse(line), oldRoot, newRoot, oldWorkspace, newWorkspace, oldHome, newHome)));
|
|
257
|
-
fs.writeFileSync(file, `${lines.join("\n")}\n`);
|
|
728
|
+
fs.writeFileSync(file, `${JSON.stringify(rebaseStructuredValue(value, oldRoot, newRoot, oldWorkspace, newWorkspace, oldHome, newHome), null, 2)}\n`);
|
|
258
729
|
}
|
|
259
730
|
}
|
|
260
731
|
};
|
|
261
732
|
visit(root);
|
|
262
733
|
}
|
|
263
|
-
function rewriteDatabaseColumn(context, table, column, oldRoot, newRoot, oldWorkspace, newWorkspace, oldHome, newHome) {
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
734
|
+
function rewriteDatabaseColumn(context, table, column, oldRoot, newRoot, oldWorkspace, newWorkspace, oldHome, newHome) {
|
|
735
|
+
for (const row of context.db.all(`SELECT rowid AS rid, ${column} AS value FROM ${table} WHERE ${column} IS NOT NULL`)) {
|
|
736
|
+
if (!row.value)
|
|
737
|
+
continue;
|
|
738
|
+
let value;
|
|
739
|
+
if (column.endsWith("_json") || column === "index_json") {
|
|
740
|
+
const original = JSON.parse(row.value);
|
|
741
|
+
const mapped = rebaseStructuredValue(original, oldRoot, newRoot, oldWorkspace, newWorkspace, oldHome, newHome);
|
|
742
|
+
if (table === "runs" && column === "index_json" && original.execution_profile) {
|
|
743
|
+
// Accepted source/binding are logical provenance, not mutable physical locators.
|
|
744
|
+
if (original.execution_profile.operational_decision)
|
|
745
|
+
mapped.execution_profile.operational_decision = original.execution_profile.operational_decision;
|
|
746
|
+
if (original.execution_profile.operational_contract)
|
|
747
|
+
mapped.execution_profile.operational_contract = original.execution_profile.operational_contract;
|
|
748
|
+
}
|
|
749
|
+
value = JSON.stringify(mapped);
|
|
750
|
+
}
|
|
751
|
+
else
|
|
752
|
+
value = rebasePathString(row.value, oldRoot, newRoot, oldWorkspace, newWorkspace, oldHome, newHome);
|
|
753
|
+
if (value !== row.value)
|
|
754
|
+
context.db.run(`UPDATE ${table} SET ${column} = ? WHERE rowid = ?`, [value, row.rid]);
|
|
755
|
+
}
|
|
756
|
+
}
|
|
270
757
|
function rebaseStructuredValue(value, oldRoot, newRoot, oldWorkspace, newWorkspace, oldHome, newHome) { if (typeof value === "string")
|
|
271
758
|
return rebasePathString(value, oldRoot, newRoot, oldWorkspace, newWorkspace, oldHome, newHome); if (Array.isArray(value))
|
|
272
759
|
return value.map((item) => rebaseStructuredValue(item, oldRoot, newRoot, oldWorkspace, newWorkspace, oldHome, newHome)); if (value && typeof value === "object")
|
|
@@ -274,20 +761,29 @@ function rebaseStructuredValue(value, oldRoot, newRoot, oldWorkspace, newWorkspa
|
|
|
274
761
|
function rebasePathString(value, oldRoot, newRoot, oldWorkspace, newWorkspace, oldHome, newHome) { for (const [oldPrefix, newPrefix] of [[oldHome, newHome], [oldWorkspace, newWorkspace], [oldRoot, newRoot]])
|
|
275
762
|
if (value === oldPrefix || value.startsWith(`${oldPrefix}${path.sep}`))
|
|
276
763
|
return `${newPrefix}${value.slice(oldPrefix.length)}`; return value; }
|
|
277
|
-
function
|
|
764
|
+
function prepareSnapshotWorkspace(workspaceRoot, projectRoot, allowUnborn = false) {
|
|
278
765
|
if (samePath(workspaceRoot, projectRoot))
|
|
279
|
-
return
|
|
766
|
+
return null;
|
|
280
767
|
const branch = git(workspaceRoot, ["symbolic-ref", "--quiet", "--short", "HEAD"]);
|
|
281
768
|
if (!branch)
|
|
282
769
|
throw new AppError("snapshot_workspace_invalid", "Feature workspace must be on a named Git branch", 1, { workspace_root: workspaceRoot });
|
|
283
770
|
const baseRef = git(workspaceRoot, ["rev-parse", "HEAD"]);
|
|
284
|
-
if (!baseRef)
|
|
771
|
+
if (!baseRef && !(allowUnborn && git(workspaceRoot, ['for-each-ref', '--format=%(refname)']) === ''))
|
|
285
772
|
throw new AppError("snapshot_workspace_invalid", "Feature workspace has no readable HEAD", 1, { workspace_root: workspaceRoot });
|
|
773
|
+
return { branch, baseRef };
|
|
774
|
+
}
|
|
775
|
+
function snapshotWorkspace(workspaceRoot, projectRoot, output, independent = false) {
|
|
776
|
+
const prepared = prepareSnapshotWorkspace(workspaceRoot, projectRoot, independent);
|
|
777
|
+
if (!prepared)
|
|
778
|
+
return { kind: "project" };
|
|
779
|
+
const { branch, baseRef } = prepared;
|
|
286
780
|
const workspace = path.join(output, "workspace");
|
|
287
781
|
const bundle = path.join(output, "workspace.bundle");
|
|
288
|
-
fs.cpSync(workspaceRoot, workspace, { recursive: true, verbatimSymlinks: true, filter: (
|
|
289
|
-
|
|
290
|
-
|
|
782
|
+
fs.cpSync(workspaceRoot, workspace, { recursive: true, verbatimSymlinks: true, filter: projectPayloadFilter(workspaceRoot) });
|
|
783
|
+
if (baseRef === null)
|
|
784
|
+
return { kind: 'git_repository', source_root: workspaceRoot, branch, base_ref: null, bundle_sha256: null, state: 'unborn', sha256: treeHash(workspace), ...snapshotIndex(workspaceRoot, output, 'workspace') };
|
|
785
|
+
runGit(independent ? workspaceRoot : projectRoot, ["bundle", "create", bundle, branch]);
|
|
786
|
+
return { kind: independent ? "git_repository" : "git_worktree", source_root: workspaceRoot, branch, base_ref: baseRef, sha256: treeHash(workspace), bundle_sha256: fileHash(bundle), ...snapshotIndex(workspaceRoot, output, "workspace") };
|
|
291
787
|
}
|
|
292
788
|
function restoreWorkspace(snapshot, manifest, projectRoot, ddFlowHome) {
|
|
293
789
|
if (manifest.workspace.kind === "project")
|
|
@@ -296,42 +792,250 @@ function restoreWorkspace(snapshot, manifest, projectRoot, ddFlowHome) {
|
|
|
296
792
|
const bundle = path.join(snapshot, "workspace.bundle");
|
|
297
793
|
if (treeHash(source) !== manifest.workspace.sha256)
|
|
298
794
|
throw new AppError("snapshot_checksum_mismatch", "Snapshot workspace checksum does not match manifest", 1, { snapshot });
|
|
299
|
-
if (!fs.existsSync(bundle) || fileHash(bundle) !== manifest.workspace.bundle_sha256)
|
|
795
|
+
if (manifest.workspace.state === 'unborn' ? fs.existsSync(bundle) : !fs.existsSync(bundle) || fileHash(bundle) !== manifest.workspace.bundle_sha256)
|
|
300
796
|
throw new AppError("snapshot_checksum_mismatch", "Snapshot workspace Git bundle does not match manifest", 1, { snapshot });
|
|
301
|
-
const
|
|
302
|
-
const sourceRoot = physicalPath(manifest.workspace.source_root);
|
|
303
|
-
const workspaceRoot = isInside(managedRoot, sourceRoot)
|
|
304
|
-
? path.join(ddFlowHome, path.relative(managedRoot, sourceRoot))
|
|
305
|
-
: path.join(path.dirname(projectRoot), "workspace");
|
|
797
|
+
const workspaceRoot = restoredWorkspaceRoot(manifest, projectRoot, ddFlowHome);
|
|
306
798
|
if (fs.existsSync(workspaceRoot))
|
|
307
799
|
throw new AppError("snapshot_workspace_target_exists", "Snapshot feature workspace target already exists", 1, { workspace_root: workspaceRoot });
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
800
|
+
if (manifest.workspace.kind === 'git_repository') {
|
|
801
|
+
fs.mkdirSync(path.dirname(workspaceRoot), { recursive: true });
|
|
802
|
+
if (manifest.workspace.state === 'unborn') {
|
|
803
|
+
fs.mkdirSync(workspaceRoot);
|
|
804
|
+
runGit(workspaceRoot, ['-c', 'init.templateDir=', 'init', '--initial-branch', manifest.workspace.branch]);
|
|
805
|
+
}
|
|
806
|
+
else
|
|
807
|
+
runGit(projectRoot, ['clone', '--no-checkout', '--branch', manifest.workspace.branch, bundle, workspaceRoot]);
|
|
808
|
+
}
|
|
809
|
+
else {
|
|
810
|
+
runGit(projectRoot, ["fetch", bundle, `+refs/heads/${manifest.workspace.branch}:refs/heads/${manifest.workspace.branch}`]);
|
|
811
|
+
if (git(projectRoot, ["rev-parse", `refs/heads/${manifest.workspace.branch}`]) !== manifest.workspace.base_ref)
|
|
812
|
+
throw new AppError("snapshot_workspace_restore_failed", "Snapshot Git bundle did not restore the frozen feature base", 1, { branch: manifest.workspace.branch, expected_base: manifest.workspace.base_ref });
|
|
813
|
+
runGit(projectRoot, ["worktree", "add", workspaceRoot, manifest.workspace.branch]);
|
|
814
|
+
}
|
|
815
|
+
if (git(workspaceRoot, ["rev-parse", "HEAD"]) !== manifest.workspace.base_ref)
|
|
816
|
+
throw new AppError("snapshot_workspace_restore_failed", "Snapshot Git bundle did not restore the frozen workspace base", 1, { branch: manifest.workspace.branch, expected_base: manifest.workspace.base_ref });
|
|
312
817
|
replaceProjectTree(workspaceRoot, source);
|
|
818
|
+
restoreIndex(snapshot, "workspace", manifest.workspace, workspaceRoot);
|
|
313
819
|
return fs.realpathSync(workspaceRoot);
|
|
314
820
|
}
|
|
821
|
+
export function restoredWorkspaceRoot(manifest, projectRoot, ddFlowHome) {
|
|
822
|
+
if (manifest.workspace.kind === "project")
|
|
823
|
+
return projectRoot;
|
|
824
|
+
const managedRoot = physicalPath(manifest.dd_flow_home), sourceRoot = physicalPath(manifest.workspace.source_root);
|
|
825
|
+
return isInside(managedRoot, sourceRoot) ? path.join(ddFlowHome, path.relative(managedRoot, sourceRoot)) : path.join(path.dirname(projectRoot), "workspace");
|
|
826
|
+
}
|
|
827
|
+
function assertRestoreTargetSeparation(snapshot, targets) {
|
|
828
|
+
const roots = [snapshot, ...targets].map(physicalPath);
|
|
829
|
+
for (let i = 0; i < roots.length; i++)
|
|
830
|
+
for (let j = i + 1; j < roots.length; j++) {
|
|
831
|
+
if (isInside(roots[i], roots[j]) || isInside(roots[j], roots[i]))
|
|
832
|
+
throw new AppError("snapshot_path_conflict", "Snapshot source and restore targets must not overlap", 2, { source: roots[i], target: roots[j] });
|
|
833
|
+
}
|
|
834
|
+
}
|
|
315
835
|
function sourceWorkspaceRoot(manifest) {
|
|
316
836
|
return manifest.workspace.kind === "project" ? manifest.project_root : manifest.workspace.source_root;
|
|
317
837
|
}
|
|
318
|
-
function
|
|
838
|
+
function prepareSnapshotProjectGit(projectRoot, allowUnborn = false) {
|
|
319
839
|
const branch = git(projectRoot, ["symbolic-ref", "--quiet", "--short", "HEAD"]);
|
|
320
|
-
const head = git(projectRoot, ["rev-parse", "HEAD"]);
|
|
321
|
-
if (!branch || !head)
|
|
322
|
-
throw new AppError("snapshot_project_git_invalid", "Snapshot requires a
|
|
840
|
+
const head = git(projectRoot, ["rev-parse", "--verify", "HEAD"]);
|
|
841
|
+
if (!branch || !head && !(allowUnborn && git(projectRoot, ["for-each-ref", "--format=%(refname)"]) === ""))
|
|
842
|
+
throw new AppError("snapshot_project_git_invalid", "Snapshot requires a named Git branch and readable HEAD, or an admitted empty-ref Memory recovery", 1, { project_root: projectRoot });
|
|
843
|
+
return { branch, head };
|
|
844
|
+
}
|
|
845
|
+
function snapshotProjectGit(projectRoot, output, allowUnborn = false) {
|
|
846
|
+
const { branch, head } = prepareSnapshotProjectGit(projectRoot, allowUnborn);
|
|
847
|
+
if (head === null)
|
|
848
|
+
return { branch, head: null, bundle_sha256: null, state: "unborn", ...snapshotIndex(projectRoot, output, "project") };
|
|
323
849
|
const bundle = path.join(output, "project.bundle");
|
|
324
|
-
runGit(projectRoot, ["bundle", "create", bundle,
|
|
325
|
-
return { branch, head, bundle_sha256: fileHash(bundle) };
|
|
850
|
+
runGit(projectRoot, ["bundle", "create", bundle, `refs/heads/${branch}`]);
|
|
851
|
+
return { branch, head, bundle_sha256: fileHash(bundle), ...snapshotIndex(projectRoot, output, "project") };
|
|
326
852
|
}
|
|
327
853
|
function restoreProjectGit(snapshot, source, projectRoot) {
|
|
328
854
|
const bundle = path.join(snapshot, "project.bundle");
|
|
329
|
-
if (!fs.existsSync(bundle) || fileHash(bundle) !== source.bundle_sha256)
|
|
855
|
+
if (source.state === "unborn" ? fs.existsSync(bundle) : !fs.existsSync(bundle) || fileHash(bundle) !== source.bundle_sha256)
|
|
330
856
|
throw new AppError("snapshot_checksum_mismatch", "Snapshot project Git bundle checksum does not match manifest", 1, { snapshot });
|
|
331
857
|
fs.rmSync(path.join(projectRoot, ".git"), { recursive: true, force: true });
|
|
332
858
|
runGit(projectRoot, ["init", "-b", source.branch]);
|
|
333
|
-
|
|
334
|
-
|
|
859
|
+
if (source.state !== "unborn") {
|
|
860
|
+
runGit(projectRoot, ["fetch", bundle, `+refs/heads/${source.branch}:refs/remotes/snapshot/${source.branch}`]);
|
|
861
|
+
runGit(projectRoot, ["checkout", "-B", source.branch, source.head]);
|
|
862
|
+
}
|
|
863
|
+
restoreIndex(snapshot, "project", source, projectRoot);
|
|
864
|
+
}
|
|
865
|
+
const mergeStateFiles = ["MERGE_HEAD", "MERGE_MSG", "MERGE_MODE", "ORIG_HEAD", "AUTO_MERGE"];
|
|
866
|
+
function indexEntries(root, includeExclude = true) {
|
|
867
|
+
const entries = Buffer.from(gitBytes(root, ["ls-files", "--stage", "-z"])
|
|
868
|
+
.toString("utf8").split("\0").filter(entry => entry && localEnvironmentAllowed(entry.slice(entry.indexOf("\t") + 1)))
|
|
869
|
+
.map(entry => `${entry}\0`).join(""));
|
|
870
|
+
const merge = {};
|
|
871
|
+
for (const name of mergeStateFiles) {
|
|
872
|
+
const file = git(root, ["rev-parse", "--git-path", name]);
|
|
873
|
+
if (!file)
|
|
874
|
+
throw new AppError("snapshot_index_invalid", "Could not locate Git merge state", 1);
|
|
875
|
+
const resolved = path.resolve(root, file);
|
|
876
|
+
if (fs.existsSync(resolved))
|
|
877
|
+
merge[name] = fs.readFileSync(resolved).toString("base64");
|
|
878
|
+
}
|
|
879
|
+
if (!includeExclude)
|
|
880
|
+
return Buffer.from(JSON.stringify({ entries: entries.toString("base64"), merge }));
|
|
881
|
+
const excludePath = git(root, ["rev-parse", "--git-path", "info/exclude"]);
|
|
882
|
+
if (!excludePath)
|
|
883
|
+
throw new AppError("snapshot_index_invalid", "Could not locate Git local excludes", 1);
|
|
884
|
+
const excludeFile = path.resolve(root, excludePath);
|
|
885
|
+
let exclude = null;
|
|
886
|
+
if (fs.existsSync(excludeFile)) {
|
|
887
|
+
const stat = fs.lstatSync(excludeFile);
|
|
888
|
+
if (!stat.isFile() || stat.size > 1024 * 1024)
|
|
889
|
+
throw new AppError("snapshot_index_invalid", "Git local excludes must be a bounded regular file", 1);
|
|
890
|
+
exclude = fs.readFileSync(excludeFile).toString("base64");
|
|
891
|
+
}
|
|
892
|
+
// Local excludes affect review-visible inputs, but are not part of a Git
|
|
893
|
+
// bundle/index. Keep them in the existing hashed, non-executable Git payload.
|
|
894
|
+
return Buffer.from(JSON.stringify({ entries: entries.toString("base64"), merge, exclude }));
|
|
895
|
+
}
|
|
896
|
+
function snapshotIndex(root, output, name) {
|
|
897
|
+
const bytes = indexEntries(root);
|
|
898
|
+
const value = JSON.parse(bytes.toString());
|
|
899
|
+
const objects = new Set();
|
|
900
|
+
for (const entry of Buffer.from(value.entries, "base64").toString("utf8").split("\0").filter(Boolean)) {
|
|
901
|
+
const match = /^\d{6} ([a-f0-9]{40,64}) [0-3]\t/.exec(entry);
|
|
902
|
+
if (!match)
|
|
903
|
+
throw new AppError("snapshot_index_invalid", "Malformed Git index entry", 1);
|
|
904
|
+
// Gitlinks belong to another repository and are preserved as index IDs.
|
|
905
|
+
if (!entry.startsWith("160000 "))
|
|
906
|
+
objects.add(match[1]);
|
|
907
|
+
}
|
|
908
|
+
for (const name of ["MERGE_HEAD", "ORIG_HEAD", "AUTO_MERGE"]) {
|
|
909
|
+
if (value.merge[name])
|
|
910
|
+
for (const oid of Buffer.from(value.merge[name], "base64").toString().trim().split("\n")) {
|
|
911
|
+
if (!/^[a-f0-9]{40,64}$/.test(oid))
|
|
912
|
+
throw new AppError("snapshot_index_invalid", "Malformed Git merge reference", 1);
|
|
913
|
+
objects.add(oid);
|
|
914
|
+
}
|
|
915
|
+
}
|
|
916
|
+
const payload = path.join(output, `${name}.index.patch`);
|
|
917
|
+
const pack = path.join(output, `${name}.index.pack`);
|
|
918
|
+
fs.writeFileSync(payload, bytes);
|
|
919
|
+
fs.writeFileSync(pack, gitBytes(root, ["pack-objects", "--stdout", "--revs"], Buffer.from([...objects].sort().join("\n") + "\n")));
|
|
920
|
+
return { index_format: "entries@1", index_sha256: fileHash(payload), index_objects_sha256: fileHash(pack) };
|
|
921
|
+
}
|
|
922
|
+
function gitBytes(root, args, input) {
|
|
923
|
+
const result = spawnSync("git", ["-C", root, ...args], { input, maxBuffer: 64 * 1024 * 1024 });
|
|
924
|
+
if (result.status !== 0)
|
|
925
|
+
throw new AppError("snapshot_index_invalid", "Could not preserve Git index objects", 1, { args, stderr: result.stderr?.toString().trim() });
|
|
926
|
+
return result.stdout;
|
|
927
|
+
}
|
|
928
|
+
function indexPatch(root) {
|
|
929
|
+
const result = spawnSync("git", ["-C", root, "diff", "--cached", "--binary", "--full-index", "--no-ext-diff", "HEAD", "--", ".", ":(exclude)**/.env", ":(exclude)**/.env.*"], { maxBuffer: 64 * 1024 * 1024 });
|
|
930
|
+
if (result.status !== 0)
|
|
931
|
+
throw new AppError("snapshot_index_invalid", "Could not capture Git index", 1);
|
|
932
|
+
return result.stdout;
|
|
933
|
+
}
|
|
934
|
+
function restoreIndex(snapshot, name, metadata, root) {
|
|
935
|
+
if (!metadata.index_sha256)
|
|
936
|
+
return;
|
|
937
|
+
const file = path.join(snapshot, `${name}.index.patch`);
|
|
938
|
+
if (fileHash(file) !== metadata.index_sha256)
|
|
939
|
+
throw new AppError("snapshot_checksum_mismatch", "Snapshot Git index checksum does not match", 1);
|
|
940
|
+
if (metadata.index_format === "entries@1") {
|
|
941
|
+
const pack = path.join(snapshot, `${name}.index.pack`);
|
|
942
|
+
if (!metadata.index_objects_sha256 || fileHash(pack) !== metadata.index_objects_sha256)
|
|
943
|
+
throw new AppError("snapshot_checksum_mismatch", "Snapshot Git index object checksum does not match", 1);
|
|
944
|
+
const value = JSON.parse(fs.readFileSync(file, "utf8"));
|
|
945
|
+
if (value.exclude !== undefined && value.exclude !== null && (typeof value.exclude !== "string" || Buffer.byteLength(value.exclude) > 1400000))
|
|
946
|
+
throw new AppError("snapshot_index_invalid", "Snapshot Git local excludes are invalid", 1);
|
|
947
|
+
gitBytes(root, ["unpack-objects"], fs.readFileSync(pack));
|
|
948
|
+
runGit(root, ["read-tree", "--empty"]);
|
|
949
|
+
gitBytes(root, ["update-index", "-z", "--index-info"], Buffer.from(value.entries, "base64"));
|
|
950
|
+
for (const name of mergeStateFiles) {
|
|
951
|
+
const target = git(root, ["rev-parse", "--git-path", name]);
|
|
952
|
+
if (!target)
|
|
953
|
+
throw new AppError("snapshot_index_invalid", "Could not locate restored Git merge state", 1);
|
|
954
|
+
if (value.merge[name] !== undefined)
|
|
955
|
+
fs.writeFileSync(path.resolve(root, target), Buffer.from(value.merge[name], "base64"));
|
|
956
|
+
else
|
|
957
|
+
fs.rmSync(path.resolve(root, target), { force: true });
|
|
958
|
+
}
|
|
959
|
+
if (value.exclude !== undefined) {
|
|
960
|
+
const target = git(root, ["rev-parse", "--git-path", "info/exclude"]);
|
|
961
|
+
if (!target)
|
|
962
|
+
throw new AppError("snapshot_index_invalid", "Could not locate restored Git local excludes", 1);
|
|
963
|
+
const destination = path.resolve(root, target);
|
|
964
|
+
if (fs.existsSync(destination) && !fs.lstatSync(destination).isFile())
|
|
965
|
+
throw new AppError("snapshot_index_invalid", "Restored Git local excludes are not a regular file", 1);
|
|
966
|
+
if (value.exclude === null)
|
|
967
|
+
fs.rmSync(destination, { force: true });
|
|
968
|
+
else {
|
|
969
|
+
fs.mkdirSync(path.dirname(destination), { recursive: true });
|
|
970
|
+
fs.writeFileSync(destination, Buffer.from(value.exclude, "base64"));
|
|
971
|
+
}
|
|
972
|
+
}
|
|
973
|
+
return;
|
|
974
|
+
}
|
|
975
|
+
if (fs.statSync(file).size)
|
|
976
|
+
runGit(root, ["apply", "--cached", "--binary", file]);
|
|
977
|
+
}
|
|
978
|
+
function capturedIndexEntries(snapshot, name, root) {
|
|
979
|
+
const captured = JSON.parse(fs.readFileSync(path.join(snapshot, `${name}.index.patch`), "utf8"));
|
|
980
|
+
return indexEntries(root, Object.hasOwn(captured, "exclude"));
|
|
981
|
+
}
|
|
982
|
+
export function verifyRecoverySnapshot(snapshot, input, checkWorkingState = true) {
|
|
983
|
+
const manifest = readManifest(snapshot);
|
|
984
|
+
if (manifest.purpose !== "recovery" || manifest.recovery_id !== input.recoveryId || manifest.run_id !== input.runId)
|
|
985
|
+
throw new AppError("recovery_capture_invalid", "Snapshot does not identify this recovery", 1);
|
|
986
|
+
const entries = [
|
|
987
|
+
{ name: "project", root: input.projectRoot, hash: manifest.project_sha256, git: manifest.project_git, head: manifest.project_git.head },
|
|
988
|
+
...(manifest.workspace.kind !== "project" ? [{ name: "workspace", root: input.workspaceRoot, hash: manifest.workspace.sha256, git: manifest.workspace, head: manifest.workspace.base_ref }] : [])
|
|
989
|
+
];
|
|
990
|
+
verifySnapshotPayload(snapshot, manifest);
|
|
991
|
+
for (const entry of entries) {
|
|
992
|
+
const allowed = manifest.project_payload_policy === "service-files@1" ? projectPayloadFilter(entry.root) : snapshotPayloadAllowed;
|
|
993
|
+
const expectedModes = manifest.payload_modes?.[entry.name];
|
|
994
|
+
if (checkWorkingState && expectedModes && treeModesHash(entry.root, allowed) !== expectedModes)
|
|
995
|
+
throw new AppError("recovery_workspace_drift", "Executable file modes changed after recovery capture", 1, { payload: entry.name });
|
|
996
|
+
if (checkWorkingState && (treeHash(entry.root, allowed) !== entry.hash || git(entry.root, ["rev-parse", "--verify", "HEAD"]) !== entry.head
|
|
997
|
+
|| entry.head === null && git(entry.root, ["for-each-ref", "--format=%(refname)"]) !== ""
|
|
998
|
+
|| git(entry.root, ["symbolic-ref", "--quiet", "--short", "HEAD"]) !== entry.git.branch || (entry.git.index_sha256 && crypto.createHash("sha256").update(entry.git.index_format === "entries@1" ? capturedIndexEntries(snapshot, entry.name, entry.root) : indexPatch(entry.root)).digest("hex") !== entry.git.index_sha256)))
|
|
999
|
+
throw new AppError("recovery_workspace_drift", "Working files or Git state changed after recovery capture", 1, { payload: entry.name });
|
|
1000
|
+
}
|
|
1001
|
+
}
|
|
1002
|
+
/** Reuse an already published boundary only after checking its identity and
|
|
1003
|
+
* every payload, including its Git index objects. Never overwrite evidence. */
|
|
1004
|
+
export function verifyControllerBoundarySnapshot(snapshot, input) {
|
|
1005
|
+
const manifest = readManifest(snapshot), boundary = manifest.boundary_capture;
|
|
1006
|
+
if (!boundary || manifest.consistency !== "controller_boundary_writer_barrier@1" || manifest.run_id !== input.runId || boundary.controller_id !== input.controllerId || boundary.operation_id !== input.operationId || boundary.boundary_key !== input.boundaryKey || manifest.stage_entry !== input.stageEntry || manifest.purpose !== (input.stageEntry ? "stage_entry" : "candidate") || !/^[a-f0-9]{64}$/.test(boundary.source_sha256) || boundary.observation?.settled !== true || boundary.observation.operation_id !== input.operationId)
|
|
1007
|
+
throw new AppError("controller_boundary_snapshot_invalid", "Published snapshot does not prove this controller boundary", 1, { snapshot });
|
|
1008
|
+
verifySnapshotPayload(snapshot, manifest);
|
|
1009
|
+
const file = path.join(snapshot, "snapshot.json");
|
|
1010
|
+
if (input.manifestSha256 !== undefined && fileHash(file) !== input.manifestSha256)
|
|
1011
|
+
throw new AppError("snapshot_checksum_mismatch", "Boundary manifest no longer matches its retained publication receipt", 1, { snapshot });
|
|
1012
|
+
return { snapshot, manifest: file, manifest_sha256: fileHash(file), purpose: manifest.purpose, stage_entry: manifest.stage_entry };
|
|
1013
|
+
}
|
|
1014
|
+
function verifySnapshotPayload(snapshot, manifest) {
|
|
1015
|
+
if (treeHash(path.join(snapshot, "runtime")) !== manifest.runtime_sha256)
|
|
1016
|
+
throw new AppError("snapshot_checksum_mismatch", "Snapshot runtime checksum does not match", 1);
|
|
1017
|
+
verifySnapshotModes(snapshot, manifest);
|
|
1018
|
+
const payloads = [{ name: "project", hash: manifest.project_sha256, git: manifest.project_git }, ...(manifest.workspace.kind !== "project" ? [{ name: "workspace", hash: manifest.workspace.sha256, git: manifest.workspace }] : [])];
|
|
1019
|
+
for (const payload of payloads)
|
|
1020
|
+
verifyGitSnapshotPayload(snapshot, payload);
|
|
1021
|
+
if (manifest.schema_id === "dd-flow/eval-run-snapshot@6") {
|
|
1022
|
+
const db = new DatabaseSync(path.join(snapshot, "runtime", "db.sqlite"), { readOnly: true });
|
|
1023
|
+
try {
|
|
1024
|
+
const row = db.prepare("SELECT flow_kind, project_root, workspace_root FROM runs WHERE id=?").get(manifest.run_id);
|
|
1025
|
+
if (!row || row.flow_kind !== "mb-init" || row.project_root !== manifest.project_root || row.workspace_root !== manifest.project_root)
|
|
1026
|
+
throw new AppError("snapshot_invalid", "Unborn recovery must belong to its captured Memory RUN", 1, { snapshot });
|
|
1027
|
+
}
|
|
1028
|
+
finally {
|
|
1029
|
+
db.close();
|
|
1030
|
+
}
|
|
1031
|
+
}
|
|
1032
|
+
}
|
|
1033
|
+
function verifyGitSnapshotPayload(snapshot, payload) {
|
|
1034
|
+
const bundle = path.join(snapshot, `${payload.name}.bundle`);
|
|
1035
|
+
const invalidBundle = payload.git.state === "unborn" ? payload.git.bundle_sha256 !== null || fs.existsSync(bundle)
|
|
1036
|
+
: !fs.existsSync(bundle) || fileHash(bundle) !== payload.git.bundle_sha256;
|
|
1037
|
+
if (treeHash(path.join(snapshot, payload.name)) !== payload.hash || invalidBundle || payload.git.index_sha256 && fileHash(path.join(snapshot, `${payload.name}.index.patch`)) !== payload.git.index_sha256 || payload.git.index_format === "entries@1" && (!payload.git.index_objects_sha256 || fileHash(path.join(snapshot, `${payload.name}.index.pack`)) !== payload.git.index_objects_sha256))
|
|
1038
|
+
throw new AppError("snapshot_checksum_mismatch", "Snapshot payload or Git index checksum does not match", 1, { payload: payload.name });
|
|
335
1039
|
}
|
|
336
1040
|
function git(root, args) {
|
|
337
1041
|
const result = spawnSync("git", ["-C", root, ...args], { encoding: "utf8" });
|
|
@@ -343,12 +1047,408 @@ function runGit(root, args) {
|
|
|
343
1047
|
throw new AppError("snapshot_workspace_restore_failed", "Could not restore feature worktree", 1, { stderr: result.stderr.trim(), args });
|
|
344
1048
|
}
|
|
345
1049
|
function samePath(left, right) { return path.resolve(left) === path.resolve(right); }
|
|
346
|
-
function
|
|
1050
|
+
function snapshotPayloadAllowed(source) {
|
|
1051
|
+
const name = path.basename(source);
|
|
1052
|
+
return ![".git", "node_modules", ".cache", ".npm", ".pnpm-store"].includes(name)
|
|
1053
|
+
&& localEnvironmentAllowed(source);
|
|
1054
|
+
}
|
|
1055
|
+
function projectPayloadFilter(root) {
|
|
1056
|
+
const serviceAllowed = projectServicePathFilter(root);
|
|
1057
|
+
return file => snapshotPayloadAllowed(file) && serviceAllowed(file);
|
|
1058
|
+
}
|
|
1059
|
+
function runtimePayloadAllowed(source) {
|
|
1060
|
+
return snapshotPayloadAllowed(source) && !["auth.json", "auth.bk", "auth.encrypted", "auth.v2.key", "auth.v2.file", "credentials.json", ".credentials.json", "credentials", ".credentials", "id_rsa", "id_ed25519", "engines", "checkouts"].includes(path.basename(source));
|
|
1061
|
+
}
|
|
1062
|
+
/** Only a Grok daemon's verified provider root is excluded from flow evidence.
|
|
1063
|
+
* An unrelated directory named grok-home remains ordinary project data. */
|
|
1064
|
+
function grokProviderHomes(runtimeRoot) {
|
|
1065
|
+
const homes = [];
|
|
1066
|
+
const visit = (directory) => {
|
|
1067
|
+
for (const entry of fs.readdirSync(directory, { withFileTypes: true })) {
|
|
1068
|
+
const file = path.join(directory, entry.name);
|
|
1069
|
+
if (entry.isDirectory()) {
|
|
1070
|
+
if (![".git", "node_modules", "grok-home"].includes(entry.name))
|
|
1071
|
+
visit(file);
|
|
1072
|
+
}
|
|
1073
|
+
else if (entry.isFile() && entry.name === "daemon.json") {
|
|
1074
|
+
let state;
|
|
1075
|
+
try {
|
|
1076
|
+
state = JSON.parse(fs.readFileSync(file, "utf8"));
|
|
1077
|
+
}
|
|
1078
|
+
catch {
|
|
1079
|
+
continue;
|
|
1080
|
+
}
|
|
1081
|
+
if (state.schema_id !== "dd-grok/daemon-state@1")
|
|
1082
|
+
continue;
|
|
1083
|
+
const expected = path.join(directory, "grok-home");
|
|
1084
|
+
if (state.config?.grokHome !== expected || state.config?.ddFlowHome !== runtimeRoot)
|
|
1085
|
+
throw new AppError("snapshot_native_scope_invalid", "Grok provider home differs from its retained daemon binding", 1, { state_dir: directory });
|
|
1086
|
+
homes.push(expected);
|
|
1087
|
+
}
|
|
1088
|
+
}
|
|
1089
|
+
};
|
|
1090
|
+
visit(runtimeRoot);
|
|
1091
|
+
return homes;
|
|
1092
|
+
}
|
|
1093
|
+
function nativeDaemonFile(file) {
|
|
1094
|
+
if (!fs.lstatSync(file, { throwIfNoEntry: false })?.isFile())
|
|
1095
|
+
return false;
|
|
1096
|
+
try {
|
|
1097
|
+
const state = JSON.parse(fs.readFileSync(file, "utf8"));
|
|
1098
|
+
return typeof state.daemon_id === "string" && (/^dd-(codex|zcode|grok|agy|opencode|droid)\/daemon-state@1$/.test(String(state.schema_id)) || state.schema_id === "dd-opencode/daemon-state@2");
|
|
1099
|
+
}
|
|
1100
|
+
catch {
|
|
1101
|
+
return false;
|
|
1102
|
+
} // Unrecognized files retain full-byte protection.
|
|
1103
|
+
}
|
|
1104
|
+
function sqliteFile(file) {
|
|
1105
|
+
if (!fs.existsSync(file) || !fs.statSync(file).isFile())
|
|
1106
|
+
return false;
|
|
1107
|
+
const descriptor = fs.openSync(file, "r");
|
|
1108
|
+
try {
|
|
1109
|
+
const header = Buffer.alloc(16);
|
|
1110
|
+
return fs.readSync(descriptor, header, 0, 16, 0) === 16 && header.toString() === "SQLite format 3\0";
|
|
1111
|
+
}
|
|
1112
|
+
finally {
|
|
1113
|
+
fs.closeSync(descriptor);
|
|
1114
|
+
}
|
|
1115
|
+
}
|
|
1116
|
+
function sqliteIdentifier(value) { return `"${value.replaceAll('"', '""')}"`; }
|
|
1117
|
+
function sqliteValue(value) {
|
|
1118
|
+
if (value === null || typeof value === "string" || typeof value === "boolean")
|
|
1119
|
+
return value;
|
|
1120
|
+
if (typeof value === "number")
|
|
1121
|
+
return Number.isFinite(value) ? value : String(value);
|
|
1122
|
+
if (typeof value === "bigint")
|
|
1123
|
+
return `${value}n`;
|
|
1124
|
+
if (Buffer.isBuffer(value))
|
|
1125
|
+
return `blob:${value.toString("base64")}`;
|
|
1126
|
+
if (value instanceof Uint8Array)
|
|
1127
|
+
return `blob:${Buffer.from(value).toString("base64")}`;
|
|
1128
|
+
return String(value);
|
|
1129
|
+
}
|
|
1130
|
+
/**
|
|
1131
|
+
* SQLite page layout is not semantic identity: VACUUM, checkpoint and a
|
|
1132
|
+
* delete/insert rewrite can change serialized bytes without changing the
|
|
1133
|
+
* logical database. Snapshot only the schema and ordered logical row values.
|
|
1134
|
+
* This intentionally applies to the small set of managed SQLite files, not
|
|
1135
|
+
* to arbitrary provider databases.
|
|
1136
|
+
*/
|
|
1137
|
+
function sqliteSemanticFingerprint(db) {
|
|
1138
|
+
db.exec("PRAGMA busy_timeout = 4000");
|
|
1139
|
+
db.exec("BEGIN");
|
|
1140
|
+
try {
|
|
1141
|
+
const schema = db.prepare("SELECT type, name, tbl_name, sql FROM sqlite_schema WHERE name NOT LIKE 'sqlite_%' ORDER BY type, name, tbl_name, sql").all();
|
|
1142
|
+
const tables = schema.filter(row => row.type === "table").map(row => {
|
|
1143
|
+
const columns = db.prepare(`PRAGMA table_info(${sqliteIdentifier(row.name)})`).all()
|
|
1144
|
+
.sort((left, right) => Number(left.cid ?? 0) - Number(right.cid ?? 0))
|
|
1145
|
+
.map(column => ({ name: String(column.name ?? ""), type: String(column.type ?? ""), notnull: Number(column.notnull ?? 0), default: sqliteValue(column.dflt_value ?? null), pk: Number(column.pk ?? 0) }));
|
|
1146
|
+
const names = columns.map(column => column.name);
|
|
1147
|
+
const rows = db.prepare(`SELECT * FROM ${sqliteIdentifier(row.name)}`).all()
|
|
1148
|
+
.map(record => names.map(name => [name, sqliteValue(record[name])]))
|
|
1149
|
+
.sort((left, right) => JSON.stringify(left).localeCompare(JSON.stringify(right)));
|
|
1150
|
+
return { name: row.name, columns, rows };
|
|
1151
|
+
});
|
|
1152
|
+
return crypto.createHash("sha256").update(JSON.stringify({ schema, tables })).digest("hex");
|
|
1153
|
+
}
|
|
1154
|
+
finally {
|
|
1155
|
+
try {
|
|
1156
|
+
db.exec("ROLLBACK");
|
|
1157
|
+
}
|
|
1158
|
+
catch { /* The read transaction may already have closed on provider error. */ }
|
|
1159
|
+
}
|
|
1160
|
+
}
|
|
1161
|
+
/** A WAL is copied through its SQLite read snapshot, not as raw bytes. */
|
|
1162
|
+
function runtimeSnapshotKind(file, excluded, providerHomes = []) {
|
|
1163
|
+
if (!runtimePayloadAllowed(file) || providerHomes.some(home => file === home || isInside(home, file)) || excluded.includes(file))
|
|
1164
|
+
return "excluded";
|
|
1165
|
+
if (fs.lstatSync(file).isFile()) {
|
|
1166
|
+
if (/-(?:wal|shm|journal)$/.test(file) && sqliteFile(file.replace(/-(?:wal|shm|journal)$/, "")))
|
|
1167
|
+
return "sidecar";
|
|
1168
|
+
if (sqliteFile(file))
|
|
1169
|
+
return "sqlite";
|
|
1170
|
+
}
|
|
1171
|
+
return "ordinary";
|
|
1172
|
+
}
|
|
1173
|
+
/** Productive writers are fenced separately. Compare the copied source set
|
|
1174
|
+
* without idle diagnostic appends; SQLite includes committed WAL pages without
|
|
1175
|
+
* mistaking a physical checkpoint for new logical content. */
|
|
1176
|
+
function snapshotSourceVersion(context, input, retainedDatabases, retainedJournals) {
|
|
1177
|
+
const databases = retainedDatabases ?? new Map();
|
|
1178
|
+
const journals = retainedJournals ?? new Map();
|
|
1179
|
+
const projectRoot = resolveProjectRoot(input.projectRoot);
|
|
1180
|
+
const project = requireProjectByRoot(context, projectRoot);
|
|
1181
|
+
const run = context.db.get("SELECT workspace_root FROM runs WHERE project_id = ? AND id = ?", [project.id, input.runId]);
|
|
1182
|
+
if (!run)
|
|
1183
|
+
throw new AppError("not_found", "Snapshot RUN is not registered", 1);
|
|
1184
|
+
const entries = new Map();
|
|
1185
|
+
const diagnosticLogs = new Set(getResourceDatabase(resourceHome(context), "read_existing").all("SELECT stdout_path,stderr_path,metadata_json FROM managed_processes WHERE project_id=? AND run_id=? AND kind IN ('codex-daemon','zcode-daemon','grok-daemon','agy-daemon','opencode-daemon','droid-daemon')", [project.id, input.runId]).flatMap(row => {
|
|
1186
|
+
const directory = JSON.parse(row.metadata_json).state_dir;
|
|
1187
|
+
const log = typeof directory === "string" && path.isAbsolute(directory) ? path.join(directory, "daemon.log") : null;
|
|
1188
|
+
return log && isInside(context.ddFlowHome, log) && row.stdout_path === log && row.stderr_path === log ? [log] : [];
|
|
1189
|
+
}));
|
|
1190
|
+
const excluded = codexHomeSnapshotExclusions(context, project.id, input.runId);
|
|
1191
|
+
const providerHomes = input.recoveryId === undefined ? grokProviderHomes(context.ddFlowHome) : [];
|
|
1192
|
+
const observations = new Map();
|
|
1193
|
+
const operationRoots = new Set();
|
|
1194
|
+
const visit = (directory) => {
|
|
1195
|
+
const daemon = path.join(directory, "daemon.json");
|
|
1196
|
+
const native = nativeDaemonFile(daemon);
|
|
1197
|
+
if (native) {
|
|
1198
|
+
operationRoots.add(path.join(directory, "operations"));
|
|
1199
|
+
for (const operation of retainedDaemonOperations(directory))
|
|
1200
|
+
if (isObservationOperation(operation.operation))
|
|
1201
|
+
observations.set(path.join(directory, "operations", crypto.createHash("sha256").update(String(operation.operation_id)).digest("hex")), operation);
|
|
1202
|
+
}
|
|
1203
|
+
for (const entry of fs.readdirSync(directory, { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name))) {
|
|
1204
|
+
const file = path.join(directory, entry.name);
|
|
1205
|
+
// These diagnostic cuts are retained, but their creation cannot
|
|
1206
|
+
// invalidate a productive boundary. Unknown operations remain protected.
|
|
1207
|
+
if (observations.has(file))
|
|
1208
|
+
continue;
|
|
1209
|
+
const kind = runtimeSnapshotKind(file, excluded, providerHomes);
|
|
1210
|
+
if (kind === "excluded" || kind === "sidecar")
|
|
1211
|
+
continue;
|
|
1212
|
+
const key = `runtime/${path.relative(context.ddFlowHome, file)}`;
|
|
1213
|
+
const stat = fs.lstatSync(file);
|
|
1214
|
+
if (stat.isDirectory()) {
|
|
1215
|
+
if (!operationRoots.has(file))
|
|
1216
|
+
entries.set(key, "directory");
|
|
1217
|
+
visit(file);
|
|
1218
|
+
}
|
|
1219
|
+
else if (stat.isSymbolicLink())
|
|
1220
|
+
entries.set(key, `link:${fs.readlinkSync(file)}`);
|
|
1221
|
+
else if (native && file === daemon)
|
|
1222
|
+
entries.set(key, `daemon:${nativeInventoryVersion(directory)}:${stat.mode & 0o111}`);
|
|
1223
|
+
else if (kind === "sqlite") {
|
|
1224
|
+
// Their productive identities are checked by boundary.verify(). A
|
|
1225
|
+
// consistent backup is evidence, not a demand for global DB silence.
|
|
1226
|
+
if (file === context.db.path || file === path.join(resourceHome(context), "runtime.sqlite")) {
|
|
1227
|
+
entries.set(key, `managed-sqlite:${stat.mode & 0o111}`);
|
|
1228
|
+
continue;
|
|
1229
|
+
}
|
|
1230
|
+
let db = databases.get(file);
|
|
1231
|
+
if (!db) {
|
|
1232
|
+
db = new DatabaseSync(file, { readOnly: true });
|
|
1233
|
+
databases.set(file, db);
|
|
1234
|
+
}
|
|
1235
|
+
entries.set(key, `sqlite:${sqliteSemanticFingerprint(db)}:${stat.mode & 0o111}`);
|
|
1236
|
+
}
|
|
1237
|
+
else if (stat.isFile() && diagnosticLogs.has(file)) {
|
|
1238
|
+
// Daemon stdout/stderr can append while an otherwise settled Session is idle.
|
|
1239
|
+
// Keep its identity/mode fenced; the copied bytes remain sealed in the snapshot.
|
|
1240
|
+
entries.set(key, `diagnostic-log:${stat.dev}:${stat.ino}:${stat.mode & 0o111}`);
|
|
1241
|
+
}
|
|
1242
|
+
else if (stat.isFile() && (["adapter.events.jsonl", "events.jsonl", "model-observations.jsonl", "timeline.jsonl", "controller.log"].includes(path.basename(file)) || native && file === path.join(directory, "daemon.log"))) {
|
|
1243
|
+
const bytes = fs.readFileSync(file);
|
|
1244
|
+
let cut = journals.get(file);
|
|
1245
|
+
if (!cut && !retainedJournals) {
|
|
1246
|
+
const payload = bytes.subarray(0, bytes.lastIndexOf(10) + 1);
|
|
1247
|
+
cut = { payload, dev: stat.dev, ino: stat.ino, mode: stat.mode, sha256: crypto.createHash("sha256").update(payload).digest("hex") };
|
|
1248
|
+
journals.set(file, cut);
|
|
1249
|
+
}
|
|
1250
|
+
const valid = cut && cut.dev === stat.dev && cut.ino === stat.ino && bytes.length >= cut.payload.length && bytes.subarray(0, cut.payload.length).equals(cut.payload);
|
|
1251
|
+
entries.set(key, valid && cut ? `journal:${cut.sha256}:${cut.payload.length}:${stat.mode & 0o111}` : "journal:changed");
|
|
1252
|
+
}
|
|
1253
|
+
else if (stat.isFile())
|
|
1254
|
+
entries.set(key, `file:${crypto.createHash("sha256").update(fs.readFileSync(file)).digest("hex")}:${stat.mode & 0o111}`);
|
|
1255
|
+
}
|
|
1256
|
+
};
|
|
1257
|
+
try {
|
|
1258
|
+
visit(context.ddFlowHome);
|
|
1259
|
+
for (const root of new Set([projectRoot, run.workspace_root]))
|
|
1260
|
+
entries.set(`source/${root}`, JSON.stringify([
|
|
1261
|
+
treeHash(root, true), treeModesHash(root, true), git(root, ["symbolic-ref", "--quiet", "HEAD"]), git(root, ["rev-parse", "HEAD"]), indexEntries(root).toString("base64")
|
|
1262
|
+
]));
|
|
1263
|
+
return { entries, databases, journals, observations, sha256: crypto.createHash("sha256").update(JSON.stringify([...entries].sort(([a], [b]) => a.localeCompare(b)))).digest("hex") };
|
|
1264
|
+
}
|
|
1265
|
+
catch (error) {
|
|
1266
|
+
if (!retainedDatabases)
|
|
1267
|
+
for (const db of databases.values())
|
|
1268
|
+
try {
|
|
1269
|
+
db.close();
|
|
1270
|
+
}
|
|
1271
|
+
catch { /* Preserve the original source error. */ }
|
|
1272
|
+
throw error;
|
|
1273
|
+
}
|
|
1274
|
+
}
|
|
1275
|
+
function snapshotSourceChanges(before, after) {
|
|
1276
|
+
return [...new Set([...before.keys(), ...after.keys()])].filter(key => before.get(key) !== after.get(key)).sort().slice(0, 8);
|
|
1277
|
+
}
|
|
1278
|
+
function snapshotSourceChangeProofs(before, after) {
|
|
1279
|
+
return snapshotSourceChanges(before, after).map(component => ({
|
|
1280
|
+
component,
|
|
1281
|
+
before: before.has(component) ? crypto.createHash("sha256").update(before.get(component)).digest("hex") : null,
|
|
1282
|
+
after: after.has(component) ? crypto.createHash("sha256").update(after.get(component)).digest("hex") : null
|
|
1283
|
+
}));
|
|
1284
|
+
}
|
|
1285
|
+
/** Managed Codex homes share host configuration, not owned snapshot payload. */
|
|
1286
|
+
function codexHomeSnapshotExclusions(context, projectId, runId) {
|
|
1287
|
+
const homes = new Set(context.db.all("SELECT target_home FROM codex_home_profiles WHERE project_id = ?", [projectId]).map(row => row.target_home));
|
|
1288
|
+
if (context.db.get("SELECT 1 FROM sqlite_schema WHERE name = 'run_controllers'")) {
|
|
1289
|
+
for (const { home } of context.db.all("SELECT json_extract(value, '$.codex_home') AS home FROM run_controllers, json_each(state_json, '$.sessions') WHERE project_id = ? AND run_id = ?", [projectId, runId]))
|
|
1290
|
+
if (home)
|
|
1291
|
+
homes.add(home);
|
|
1292
|
+
}
|
|
1293
|
+
const excluded = [];
|
|
1294
|
+
for (const home of homes) {
|
|
1295
|
+
if (!path.isAbsolute(home) || !fs.existsSync(home) || fs.lstatSync(home).isSymbolicLink())
|
|
1296
|
+
continue;
|
|
1297
|
+
const marker = path.join(home, ".dd-flow-home.json");
|
|
1298
|
+
if (!fs.existsSync(marker) || fs.lstatSync(marker).isSymbolicLink())
|
|
1299
|
+
continue;
|
|
1300
|
+
const identity = JSON.parse(fs.readFileSync(marker, "utf8"));
|
|
1301
|
+
if (identity.managed_by !== "dd-flow-cli" || identity.project_id !== projectId || identity.target_home !== home || !identity.source_home || !path.isAbsolute(identity.source_home))
|
|
1302
|
+
continue;
|
|
1303
|
+
for (const entry of codexHomeSharedEntries) {
|
|
1304
|
+
const file = path.join(home, entry);
|
|
1305
|
+
// Skip only the exact links installed by initCodexHome, never arbitrary
|
|
1306
|
+
// external links or owned local files with the same convenient name.
|
|
1307
|
+
if (fs.lstatSync(file, { throwIfNoEntry: false })?.isSymbolicLink() && fs.readlinkSync(file) === path.join(identity.source_home, entry))
|
|
1308
|
+
excluded.push(file);
|
|
1309
|
+
}
|
|
1310
|
+
excluded.push(path.join(home, "tmp"), path.join(home, ".tmp"));
|
|
1311
|
+
}
|
|
1312
|
+
return excluded;
|
|
1313
|
+
}
|
|
1314
|
+
/** Copy a dedicated home or scoped recovery tree, preserving SQLite WAL commits. */
|
|
1315
|
+
function copyRuntimeTree(source, destination, scoped = true, excluded = [], providerHomes = [], observationCuts) {
|
|
1316
|
+
if (fs.lstatSync(source).isSymbolicLink())
|
|
1317
|
+
throw new AppError("snapshot_runtime_symlink_outside_scope", "Recovery artifact roots must not be symbolic links", 1, { path: source });
|
|
1318
|
+
const root = physicalPath(source);
|
|
1319
|
+
if (isInside(root, physicalPath(destination)))
|
|
1320
|
+
throw new AppError("snapshot_output_inside_runtime", "Recovery output must be outside its source artifact tree", 1);
|
|
1321
|
+
const databases = [];
|
|
1322
|
+
fs.mkdirSync(path.dirname(destination), { recursive: true });
|
|
1323
|
+
fs.cpSync(source, destination, {
|
|
1324
|
+
recursive: true, verbatimSymlinks: true,
|
|
1325
|
+
filter: file => {
|
|
1326
|
+
if (observationCuts && path.basename(path.dirname(file)) === "operations" && fs.lstatSync(file).isDirectory() && nativeDaemonFile(path.join(path.dirname(path.dirname(file)), "daemon.json"))) {
|
|
1327
|
+
let operations;
|
|
1328
|
+
try {
|
|
1329
|
+
operations = retainedDaemonOperations(path.dirname(path.dirname(file)));
|
|
1330
|
+
}
|
|
1331
|
+
catch (error) {
|
|
1332
|
+
if (error.code !== "ENOENT")
|
|
1333
|
+
throw error;
|
|
1334
|
+
throw new AppError("snapshot_source_changed", "Native operation was not fully published during capture", 1, { components: ["native_operation_publication"] });
|
|
1335
|
+
}
|
|
1336
|
+
const operation = operations.find(row => crypto.createHash("sha256").update(String(row.operation_id)).digest("hex") === path.basename(file));
|
|
1337
|
+
if (operation && isObservationOperation(operation.operation)) {
|
|
1338
|
+
if (!observationCuts.has(file))
|
|
1339
|
+
observationCuts.set(file, operation);
|
|
1340
|
+
return false;
|
|
1341
|
+
}
|
|
1342
|
+
}
|
|
1343
|
+
const kind = runtimeSnapshotKind(file, excluded, providerHomes);
|
|
1344
|
+
if (kind === "excluded" || kind === "sidecar")
|
|
1345
|
+
return false;
|
|
1346
|
+
const stat = fs.lstatSync(file);
|
|
1347
|
+
if (stat.isSymbolicLink()) {
|
|
1348
|
+
if (scoped && !isInside(root, physicalPath(file)))
|
|
1349
|
+
throw new AppError("snapshot_runtime_symlink_outside_scope", "Recovery artifact link leaves its owned tree", 1, { path: file });
|
|
1350
|
+
return true;
|
|
1351
|
+
}
|
|
1352
|
+
if (!stat.isDirectory() && !stat.isFile())
|
|
1353
|
+
return false;
|
|
1354
|
+
if (stat.isFile()) {
|
|
1355
|
+
if (kind === "sqlite") {
|
|
1356
|
+
databases.push(file);
|
|
1357
|
+
return false;
|
|
1358
|
+
}
|
|
1359
|
+
}
|
|
1360
|
+
return true;
|
|
1361
|
+
}
|
|
1362
|
+
});
|
|
1363
|
+
for (const file of databases) {
|
|
1364
|
+
const db = new DatabaseSync(file, { readOnly: true });
|
|
1365
|
+
try {
|
|
1366
|
+
const target = source === file ? destination : path.join(destination, path.relative(source, file));
|
|
1367
|
+
fs.mkdirSync(path.dirname(target), { recursive: true });
|
|
1368
|
+
db.exec("PRAGMA busy_timeout = 4000");
|
|
1369
|
+
db.exec(`VACUUM INTO '${target.replace(/'/g, "''")}'`);
|
|
1370
|
+
}
|
|
1371
|
+
finally {
|
|
1372
|
+
db.close();
|
|
1373
|
+
}
|
|
1374
|
+
}
|
|
1375
|
+
}
|
|
1376
|
+
function captureRecoveryAuxiliary(context, captured, input) {
|
|
1377
|
+
const runtime = path.join(input.output, "runtime");
|
|
1378
|
+
const codexExclusions = codexHomeSnapshotExclusions(context, input.projectId, input.runId);
|
|
1379
|
+
const run = captured.db.get("SELECT run_root, project_root, workspace_root FROM runs WHERE project_id = ? AND id = ?", [input.projectId, input.runId]);
|
|
1380
|
+
const roots = [run.run_root, runtimeRunDir(context.ddFlowHome, input.projectId, input.runId)];
|
|
1381
|
+
for (const protocol of captured.db.all("SELECT id, state_path, plan_path FROM protocols")) {
|
|
1382
|
+
const directory = runtimeProtocolDir(context.ddFlowHome, input.projectId, protocol.id);
|
|
1383
|
+
if (!isInside(physicalPath(context.ddFlowHome), physicalPath(directory)))
|
|
1384
|
+
throw new AppError("snapshot_protocol_scope_invalid", "Protocol runtime leaves DD_FLOW_HOME", 1);
|
|
1385
|
+
if (fs.existsSync(directory)) {
|
|
1386
|
+
copyRuntimeTree(directory, path.join(runtime, path.relative(context.ddFlowHome, directory)), true, codexExclusions);
|
|
1387
|
+
roots.push(directory);
|
|
1388
|
+
}
|
|
1389
|
+
for (const file of [protocol.state_path, protocol.plan_path]) {
|
|
1390
|
+
if (!fs.existsSync(file) || roots.some(root => isInside(physicalPath(root), physicalPath(file))))
|
|
1391
|
+
continue;
|
|
1392
|
+
if (isInside(physicalPath(context.ddFlowHome), physicalPath(file)) && fs.statSync(file).isFile())
|
|
1393
|
+
copyRuntimeTree(file, path.join(runtime, path.relative(context.ddFlowHome, file)));
|
|
1394
|
+
else if (![run.project_root, run.workspace_root].some(root => isInside(physicalPath(root), physicalPath(file))))
|
|
1395
|
+
throw new AppError("snapshot_protocol_scope_invalid", "Protocol evidence is outside captured payloads", 1, { path: file });
|
|
1396
|
+
}
|
|
1397
|
+
}
|
|
1398
|
+
const homes = new Set([context.ddFlowHome, resourceHome(context)]);
|
|
1399
|
+
const queries = {
|
|
1400
|
+
run_controllers: "SELECT json_extract(manifest_json, '$.resource_home') AS home FROM run_controllers UNION SELECT json_extract(value, '$.owner.resource_home') FROM run_controllers, json_each(state_json, '$.sessions')",
|
|
1401
|
+
run_control_workers: "SELECT resource_home AS home FROM run_control_workers",
|
|
1402
|
+
work_launches: "SELECT json_extract(profile_json, '$.runtime_adapter.owner.resource_home') AS home FROM work_launches",
|
|
1403
|
+
merge_requests: "SELECT json_extract(adapter_receipt_json, '$.owner.resource_home') AS home FROM merge_requests",
|
|
1404
|
+
run_recovery_guards: "SELECT value AS home FROM run_recovery_guards, json_each(settlement_json, '$.resource_homes')"
|
|
1405
|
+
};
|
|
1406
|
+
const references = Object.entries(queries).flatMap(([table, query]) => captured.db.get("SELECT 1 FROM sqlite_schema WHERE type = 'table' AND name = ?", [table]) ? captured.db.all(query) : []);
|
|
1407
|
+
for (const { home } of references) {
|
|
1408
|
+
if (home === null)
|
|
1409
|
+
continue;
|
|
1410
|
+
if (typeof home !== "string" || !path.isAbsolute(home))
|
|
1411
|
+
throw new AppError("snapshot_resource_scope_invalid", "Retained resource home must be an absolute path", 1);
|
|
1412
|
+
homes.add(home);
|
|
1413
|
+
}
|
|
1414
|
+
const nativeCaptures = new Map();
|
|
1415
|
+
const registries = [];
|
|
1416
|
+
for (const home of [...homes].sort()) {
|
|
1417
|
+
if (!path.isAbsolute(home))
|
|
1418
|
+
throw new AppError("snapshot_resource_scope_invalid", "Resource home must be an absolute path", 1);
|
|
1419
|
+
const evidence = readRecoveryResourceEvidence(home, input.projectId, input.runId, { db: captured.db, ddFlowHome: context.ddFlowHome });
|
|
1420
|
+
for (const process of evidence.processes) {
|
|
1421
|
+
const directory = JSON.parse(process.metadata_json).state_dir;
|
|
1422
|
+
if (directory === undefined)
|
|
1423
|
+
continue;
|
|
1424
|
+
if (typeof directory !== "string" || !path.isAbsolute(directory))
|
|
1425
|
+
throw new AppError("snapshot_native_scope_invalid", "Native artifact root must be absolute", 1);
|
|
1426
|
+
if (roots.some(root => isInside(physicalPath(root), physicalPath(directory))) || nativeCaptures.has(directory))
|
|
1427
|
+
continue;
|
|
1428
|
+
const state = JSON.parse(fs.readFileSync(path.join(directory, "daemon.json"), "utf8"));
|
|
1429
|
+
const owner = state.config?.runtime_owner;
|
|
1430
|
+
if (!owner || owner.project_id !== input.projectId || owner.run_id !== input.runId || owner.owner_id !== process.owner_id || owner.dd_flow_home !== context.ddFlowHome || owner.resource_home !== home || owner.state_dir !== directory)
|
|
1431
|
+
throw new AppError("snapshot_native_scope_invalid", "External native artifact root has no matching RUN owner", 1, { state_dir: directory });
|
|
1432
|
+
const relative = path.join("native-evidence", crypto.createHash("sha256").update(directory).digest("hex"));
|
|
1433
|
+
copyRuntimeTree(directory, path.join(runtime, relative), true, codexExclusions);
|
|
1434
|
+
nativeCaptures.set(directory, relative);
|
|
1435
|
+
}
|
|
1436
|
+
const relative = path.join("resource-evidence", `${crypto.createHash("sha256").update(home).digest("hex")}.json`);
|
|
1437
|
+
fs.mkdirSync(path.dirname(path.join(runtime, relative)), { recursive: true });
|
|
1438
|
+
writeJson(path.join(runtime, relative), evidence);
|
|
1439
|
+
registries.push(relative);
|
|
1440
|
+
}
|
|
1441
|
+
writeJson(path.join(runtime, "recovery-evidence.json"), { schema_id: "dd-flow/recovery-runtime-evidence@1", project_id: input.projectId, run_id: input.runId, registries, native_captures: [...nativeCaptures].map(([source_root, payload]) => ({ source_root, payload })) });
|
|
1442
|
+
}
|
|
1443
|
+
function treeHash(root, filtered = false) {
|
|
347
1444
|
const hash = crypto.createHash("sha256");
|
|
1445
|
+
const allowed = typeof filtered === "function" ? filtered : filtered ? projectPayloadFilter(root) : () => true;
|
|
348
1446
|
const visit = (dir, relative) => {
|
|
349
1447
|
for (const entry of fs.readdirSync(dir, { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name))) {
|
|
350
1448
|
const file = path.join(dir, entry.name);
|
|
351
1449
|
const rel = path.join(relative, entry.name);
|
|
1450
|
+
if (!allowed(file))
|
|
1451
|
+
continue;
|
|
352
1452
|
if (entry.isDirectory())
|
|
353
1453
|
visit(file, rel);
|
|
354
1454
|
else if (entry.isFile()) {
|
|
@@ -364,18 +1464,80 @@ function treeHash(root) {
|
|
|
364
1464
|
visit(root, "");
|
|
365
1465
|
return hash.digest("hex");
|
|
366
1466
|
}
|
|
1467
|
+
function treeModesHash(root, filtered = false) {
|
|
1468
|
+
const entries = [];
|
|
1469
|
+
const allowed = typeof filtered === "function" ? filtered : filtered ? projectPayloadFilter(root) : () => true;
|
|
1470
|
+
const visit = (directory) => {
|
|
1471
|
+
for (const entry of fs.readdirSync(directory, { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name))) {
|
|
1472
|
+
const file = path.join(directory, entry.name);
|
|
1473
|
+
if (!allowed(file))
|
|
1474
|
+
continue;
|
|
1475
|
+
if (entry.isDirectory())
|
|
1476
|
+
visit(file);
|
|
1477
|
+
else if (entry.isFile())
|
|
1478
|
+
entries.push([path.relative(root, file), fs.statSync(file).mode & 0o111]);
|
|
1479
|
+
}
|
|
1480
|
+
};
|
|
1481
|
+
visit(root);
|
|
1482
|
+
return crypto.createHash("sha256").update(JSON.stringify(entries)).digest("hex");
|
|
1483
|
+
}
|
|
1484
|
+
function verifySnapshotModes(snapshot, manifest) {
|
|
1485
|
+
if (!manifest.payload_modes)
|
|
1486
|
+
return; // Legacy snapshots did not attest modes.
|
|
1487
|
+
for (const name of ["project", "runtime", ...(manifest.workspace.kind !== "project" ? ["workspace"] : [])]) {
|
|
1488
|
+
if (treeModesHash(path.join(snapshot, name)) !== manifest.payload_modes[name])
|
|
1489
|
+
throw new AppError("snapshot_checksum_mismatch", "Snapshot executable modes do not match manifest", 1, { payload: name });
|
|
1490
|
+
}
|
|
1491
|
+
}
|
|
367
1492
|
function fileHash(file) { return crypto.createHash("sha256").update(fs.readFileSync(file)).digest("hex"); }
|
|
1493
|
+
// Flush children before their directory entries, then publish and flush the
|
|
1494
|
+
// parent. Never follow snapshot symlinks into the original project tree.
|
|
1495
|
+
function syncSnapshotTree(root) {
|
|
1496
|
+
for (const entry of fs.readdirSync(root, { withFileTypes: true })) {
|
|
1497
|
+
const file = path.join(root, entry.name);
|
|
1498
|
+
if (entry.isDirectory())
|
|
1499
|
+
syncSnapshotTree(file);
|
|
1500
|
+
else if (entry.isFile())
|
|
1501
|
+
syncPath(file);
|
|
1502
|
+
}
|
|
1503
|
+
syncPath(root);
|
|
1504
|
+
}
|
|
1505
|
+
function syncPath(file) {
|
|
1506
|
+
const descriptor = fs.openSync(file, "r");
|
|
1507
|
+
try {
|
|
1508
|
+
fs.fsyncSync(descriptor);
|
|
1509
|
+
}
|
|
1510
|
+
finally {
|
|
1511
|
+
fs.closeSync(descriptor);
|
|
1512
|
+
}
|
|
1513
|
+
}
|
|
368
1514
|
function readManifest(snapshot) {
|
|
369
1515
|
const file = path.join(snapshot, "snapshot.json");
|
|
370
1516
|
if (!fs.existsSync(file))
|
|
371
1517
|
throw new AppError("snapshot_missing", "Snapshot manifest is missing", 1, { snapshot });
|
|
372
1518
|
const value = JSON.parse(fs.readFileSync(file, "utf8"));
|
|
373
|
-
if (
|
|
1519
|
+
if (!["dd-flow/eval-run-snapshot@5", "dd-flow/eval-run-snapshot@6", "dd-flow/eval-run-snapshot@7"].includes(String(value.schema_id)) || !value.run_id || !value.project_root || !value.dd_flow_home || typeof value.purpose !== "string" || !["stage_entry", "candidate", "incomplete", "recovery"].includes(value.purpose) || (value.purpose === "stage_entry" && !value.stage_entry) || (value.purpose !== "stage_entry" && value.stage_entry !== null) || !value.runtime_sha256 || !value.project_sha256 || !value.project_git || !value.workspace)
|
|
374
1520
|
throw new AppError("snapshot_invalid", "Snapshot manifest is invalid", 1, { snapshot });
|
|
375
|
-
if (
|
|
1521
|
+
if (value.runtime_payload_policy !== undefined && (value.runtime_payload_policy !== "flow-evidence@1" || value.purpose === "recovery"))
|
|
1522
|
+
throw new AppError("snapshot_invalid", "Snapshot runtime payload policy is invalid for its purpose", 1, { snapshot });
|
|
1523
|
+
const unborn = value.schema_id === "dd-flow/eval-run-snapshot@6";
|
|
1524
|
+
if (!value.project_git.branch || (unborn
|
|
1525
|
+
? value.purpose !== "recovery" || value.workspace.kind !== "project" || value.project_git.state !== "unborn"
|
|
1526
|
+
|| value.project_git.head !== null || value.project_git.bundle_sha256 !== null
|
|
1527
|
+
|| value.project_git.index_format !== "entries@1" || !/^[a-f0-9]{64}$/.test(value.project_git.index_sha256 ?? "")
|
|
1528
|
+
|| !/^[a-f0-9]{64}$/.test(value.project_git.index_objects_sha256 ?? "")
|
|
1529
|
+
|| Object.keys(value.project_git).sort().join() !== "branch,bundle_sha256,head,index_format,index_objects_sha256,index_sha256,state"
|
|
1530
|
+
|| typeof value.project_git.branch !== "string"
|
|
1531
|
+
|| spawnSync("git", ["check-ref-format", "--branch", value.project_git.branch], { encoding: "utf8" }).status !== 0
|
|
1532
|
+
: value.project_git.state !== undefined || !value.project_git.head || !value.project_git.bundle_sha256))
|
|
376
1533
|
throw new AppError("snapshot_invalid", "Snapshot project Git metadata is incomplete", 1, { snapshot });
|
|
377
|
-
if (
|
|
378
|
-
|
|
1534
|
+
if (!['project', 'git_worktree', 'git_repository'].includes(value.workspace.kind)
|
|
1535
|
+
|| (value.schema_id === 'dd-flow/eval-run-snapshot@7') !== (value.workspace.kind === 'git_repository')
|
|
1536
|
+
|| value.workspace.kind !== 'project' && (!value.workspace.branch || (value.workspace.state === 'unborn'
|
|
1537
|
+
? value.workspace.kind !== 'git_repository' || value.workspace.base_ref !== null || value.workspace.bundle_sha256 !== null
|
|
1538
|
+
|| value.workspace.index_format !== 'entries@1' || !value.workspace.index_sha256 || !value.workspace.index_objects_sha256
|
|
1539
|
+
: value.workspace.state !== undefined || !value.workspace.base_ref || !value.workspace.bundle_sha256)))
|
|
1540
|
+
throw new AppError("snapshot_invalid", "Snapshot workspace manifest is incomplete or incompatible", 1, { snapshot });
|
|
379
1541
|
return value;
|
|
380
1542
|
}
|
|
381
1543
|
function readBootstrapManifest(snapshot) {
|
|
@@ -383,7 +1545,11 @@ function readBootstrapManifest(snapshot) {
|
|
|
383
1545
|
if (!fs.existsSync(file))
|
|
384
1546
|
throw new AppError("snapshot_missing", "Bootstrap snapshot manifest is missing", 1, { snapshot });
|
|
385
1547
|
const value = JSON.parse(fs.readFileSync(file, "utf8"));
|
|
386
|
-
|
|
1548
|
+
const validBoundary = value.schema_id === "dd-eval/bootstrap-snapshot@1" ? value.stage === "specify" && value.flow === undefined
|
|
1549
|
+
: value.schema_id === "dd-eval/bootstrap-snapshot@2" && (value.flow?.id === "mb-init" || value.flow?.id === "mb-upgrade" || isDiagnosticFlow(value.flow?.id)) && value.stage === "preflight"
|
|
1550
|
+
&& Object.keys(value.flow).sort().join() === "descriptor_sha256,id,source_sha256"
|
|
1551
|
+
&& /^[a-f0-9]{64}$/.test(value.flow.source_sha256) && /^[a-f0-9]{64}$/.test(value.flow.descriptor_sha256);
|
|
1552
|
+
if (!validBoundary || !value.project_root || !value.project_sha256 || !value.project_git?.branch || !value.project_git.head || !value.project_git.bundle_sha256)
|
|
387
1553
|
throw new AppError("snapshot_invalid", "Bootstrap snapshot manifest is invalid", 1, { snapshot });
|
|
388
1554
|
return value;
|
|
389
1555
|
}
|