From d233a0cca136d35f84c57b77d0e22f1583e654e0 Mon Sep 17 00:00:00 2001 From: Douwe de Vries Date: Sat, 24 Oct 2026 16:08:27 +0200 Subject: [PATCH 01/17] feat(opencode): target OpenCode v2 in Flow 10 --- .agents/plans/18-opencode-v2/README.md | 373 ++ .agents/plans/18-opencode-v2/architecture.md | 96 + .agents/plans/18-opencode-v2/baseline.json | 52 + .../18-opencode-v2/compaction-boundary.md | 29 + .agents/plans/18-opencode-v2/decisions.tsv | 71 + .agents/plans/18-opencode-v2/execution.md | 134 + .../historical-policy-baseline.json | 26 + .../plans/18-opencode-v2/host-version.json | 15 + .../implementation-checkpoint.json | 397 ++ .../probe-extension-verification.json | 98 + .../plans/18-opencode-v2/probe-health.json | 13 + .../18-opencode-v2/probe-verification.json | 106 + .../plans/18-opencode-v2/probe-version.json | 1 + .../probes/server/automatic-extension.mjs | 52 + .../18-opencode-v2/probes/server/bootstrap.py | 13 + .../18-opencode-v2/probes/server/config.json | 29 + .../18-opencode-v2/probes/server/extended.mjs | 33 + .../18-opencode-v2/probes/server/extension.ts | 62 + .../probes/server/fake-provider.mjs | 24 + .../probes/server/final-edges.mjs | 10 + .../18-opencode-v2/probes/server/index.ts | 31 + .../18-opencode-v2/probes/server/inspect.mjs | 8 + .../probes/server/interleavings.mjs | 15 + .../probes/server/log-check.mjs | 6 + .../18-opencode-v2/probes/server/package.json | 9 + .../probes/server/run-cases.mjs | 20 + .../18-opencode-v2/probes/server/run-probe.py | 59 + .../probes/server/shutdown-watch.mjs | 1 + .../probes/server/static-check.mjs | 1 + .../18-opencode-v2/probes/server/steer.mjs | 9 + .../probes/server/verify-extension.py | 66 + .../18-opencode-v2/probes/server/verify.py | 59 + .../probes/server/workspace.json | 1 + .../qualification-dispatch-draft.json | 131 + .agents/plans/18-opencode-v2/registry.json | 88 + .agents/plans/18-opencode-v2/research.md | 163 + .agents/plans/18-opencode-v2/review.md | 52 + .../reviews/compaction-design-r8.md | 188 + .../plans/18-opencode-v2/reviews/core-r1.md | 113 + .../plans/18-opencode-v2/reviews/core-r2.md | 104 + .../plans/18-opencode-v2/reviews/core-r3.md | 77 + .../plans/18-opencode-v2/reviews/core-r4.md | 91 + .../plans/18-opencode-v2/reviews/core-r5.md | 100 + .../reviews/core-r6-decisions.tsv | 10 + .../plans/18-opencode-v2/reviews/core-r6.md | 108 + .../reviews/core-r7-decisions.tsv | 13 + .../plans/18-opencode-v2/reviews/core-r7.md | 168 + .../plans/18-opencode-v2/reviews/core-r8.md | 38 + .../plans/18-opencode-v2/reviews/eval-r1.md | 19 + .../plans/18-opencode-v2/reviews/eval-r2.md | 98 + .../plans/18-opencode-v2/reviews/eval-r3.md | 52 + .../reviews/native-auth-r1-decisions.tsv | 11 + .../18-opencode-v2/reviews/native-auth-r1.md | 184 + .../reviews/native-auth-r2-decisions.tsv | 15 + .../18-opencode-v2/reviews/native-auth-r2.md | 121 + .../reviews/native-auth-r3-decisions.tsv | 10 + .../18-opencode-v2/reviews/native-auth-r3.md | 92 + .../18-opencode-v2/reviews/reconciliation.md | 158 + .../18-opencode-v2/reviews/release-r1.md | 60 + .../18-opencode-v2/reviews/release-r2.md | 104 + .../18-opencode-v2/reviews/typesafe-env-r1.md | 84 + .../plans/18-opencode-v2/runtime/README.md | 31 + .../18-opencode-v2/transport/manifest.json | 6 + .../plans/18-opencode-v2/transport/probe.ts | 51 + .../session-hook-counterexample.json | 119 + .../18-opencode-v2/transport/source-seal.log | 4 + .../transport/verification.json | 179 + .agents/plans/18-opencode-v2/validation.json | 19 + .../plans/18-opencode-v2/wiki/inventory.json | 846 ++++ .../18-opencode-v2/wiki/linkverification.json | 3448 ++++++++++++++++ .../18-opencode-v2/wiki/parent-update.md | 26 + .../wiki/public-readback-gate.json | 71 + .../18-opencode-v2/wiki/review-summary.md | 42 + .agents/plans/18-opencode-v2/wiki/verify.py | 87 + .github/dependabot.yml | 4 +- .github/workflows/evals.yml | 1 - .github/workflows/opencode-compatibility.yml | 23 +- .github/workflows/release.yml | 10 + CHANGELOG.md | 23 + CONTEXT.md | 5 +- README.md | 77 +- bun.lock | 541 ++- docs/adr/0008-bounded-auto-continuation.md | 3 + docs/adr/0018-opencode-v2-only.md | 54 + docs/automatic-continuation.md | 9 +- docs/development.md | 37 +- docs/guarantees.md | 10 +- docs/index.md | 4 +- docs/maintainer-contract.md | 70 +- docs/quickstart.md | 31 +- docs/release-qualification.md | 46 +- docs/troubleshooting.md | 28 +- docs/validation-and-review.md | 14 +- evals/README.md | 113 +- evals/auto-scenario-checks.ts | 5 +- evals/benchmark-run.ts | 7 +- evals/delivery-scenario-checks.ts | 5 +- evals/grader-input.ts | 42 +- evals/harness.ts | 1137 +++--- evals/host-artifacts.ts | 10 +- evals/host-observation.ts | 37 +- evals/host-trace.ts | 503 ++- evals/http-transport-plugin.ts | 12 + evals/native-actors.ts | 7 +- evals/native-auth-config.ts | 57 + evals/native-auth/broker.ts | 342 ++ evals/native-auth/client-plugin.ts | 173 + evals/native-auth/host.ts | 320 ++ evals/native-auth/protocol.ts | 291 ++ evals/native-auth/source-plugin.ts | 184 + evals/opencode-host.ts | 340 ++ evals/recovery-decisions/budget-plugin.ts | 97 +- evals/recovery-decisions/capture-plugin.ts | 28 +- evals/recovery-decisions/episode-host.ts | 31 +- .../live-treatment-plugin.ts | 127 +- evals/recovery-decisions/treatment-plugin.ts | 100 +- evals/release-policy.ts | 70 +- evals/replay.ts | 177 +- evals/reviewer-access.ts | 58 +- evals/reviewer-run.ts | 5 +- evals/run.ts | 44 +- evals/study-manifest.ts | 11 +- evals/study-protocol.ts | 32 +- evals/study-runner.ts | 2 - package.json | 19 +- scripts/canary-run.ts | 639 ++- scripts/eval-canary.ts | 962 ++++- scripts/export-canary-native.ts | 122 + scripts/native-eval-auth.ts | 183 + scripts/probe-opencode-eval-metadata.ts | 130 +- scripts/release-metadata.ts | 4 +- scripts/restore-exact-release-artifact.ts | 35 +- skills/flow-run/SKILL.md | 2 +- src/application/model-preferences-schema.ts | 61 + src/application/model-preferences.ts | 42 + src/application/recovery-policy.ts | 22 +- src/config-shared.ts | 137 +- src/flow-rpc.ts | 22 + .../fs/model-preference-store.ts | 241 ++ src/platform/opencode/auto-compaction.ts | 346 +- src/platform/opencode/auto-drive-messages.ts | 18 + src/platform/opencode/auto-drive.ts | 218 +- src/platform/opencode/command-hook.ts | 342 +- src/platform/opencode/config.ts | 105 +- .../opencode/execution-attribution.ts | 454 +++ src/platform/opencode/flow-tool.ts | 41 + src/platform/opencode/http-transport.ts | 19 + src/platform/opencode/input-admission.ts | 509 +++ src/platform/opencode/lifecycle.ts | 795 ++++ src/platform/opencode/logging.ts | 51 +- src/platform/opencode/model-picker-ui.ts | 186 + src/platform/opencode/model-picker.ts | 138 +- src/platform/opencode/model-preferences.ts | 304 ++ src/platform/opencode/plugin-composition.ts | 486 ++- src/platform/opencode/plugin.ts | 36 +- src/platform/opencode/schema-adapter.ts | 68 - src/platform/opencode/sdk.ts | 37 +- src/platform/opencode/tool-guard.ts | 198 +- src/platform/opencode/tools.ts | 162 +- src/platform/opencode/typesafe-key-env.ts | 23 + src/platform/opencode/validation-capture.ts | 366 +- src/prompt-surfaces.ts | 2 +- src/tui.ts | 235 +- tests/architecture-boundaries.test.ts | 46 +- tests/auto-drive.test.ts | 1371 +++---- tests/canary-run.test.ts | 226 +- tests/canary-v2-live.test.ts | 429 ++ tests/canary-v2-native-auth.test.ts | 1002 +++++ tests/canary-v2-native.test.ts | 950 +++++ tests/config-shared.test.ts | 166 +- tests/delivery-scenarios.test.ts | 20 +- tests/distribution-and-surface.test.ts | 2264 +++-------- tests/documentation-contract.test.ts | 52 +- tests/eval-auto-scenarios.test.ts | 3 +- tests/eval-canary.test.ts | 65 +- tests/eval-harness-native-host.test.ts | 348 ++ tests/eval-harness-native-outcome.test.ts | 186 + tests/eval-host-config.test.ts | 99 +- tests/eval-host-observation.test.ts | 329 ++ tests/eval-host-trace.test.ts | 101 +- tests/eval-native-auth-config.test.ts | 99 + tests/eval-observed-progress.test.ts | 392 +- tests/eval-progress-wait.test.ts | 410 +- tests/eval-release-sampling.test.ts | 49 +- tests/eval-replay.test.ts | 27 +- tests/eval-reporting.test.ts | 284 +- tests/eval-retention.test.ts | 5 +- tests/eval-reviewer-access.test.ts | 7 +- tests/eval-v2-trace-contract.test.ts | 852 ++++ tests/execution-attribution.test.ts | 471 +++ tests/fixtures/continuation-commands.ts | 54 + tests/fixtures/deferred-capture-outcome.ts | 5 +- tests/fixtures/eval-cancellation-child.ts | 115 +- tests/fixtures/native-auth-plugin.ts | 130 + tests/fixtures/native-auth-smoke.ts | 534 +++ tests/fixtures/native-eval-client.ts | 287 ++ tests/fixtures/native-flow-host.ts | 809 ++++ tests/fixtures/opencode-v2-budget-plugin.ts | 48 + tests/fixtures/opencode-v2-capture-setup.ts | 44 + .../opencode-v2-isolated-candidate.ts | 147 + tests/fixtures/opencode-v2-reviewer-return.ts | 249 ++ .../fixtures/opencode-v2-scripted-provider.ts | 132 + tests/fixtures/opencode-v2-smoke-plugin.ts | 168 + tests/fixtures/opencode-v2-test-host.ts | 32 + tests/fixtures/opencode-v2-usage-proof.ts | 48 + .../fixtures/opencode-v2-validation-cases.ts | 216 + .../fixtures/verified-continuation-origin.ts | 145 + tests/host-artifacts-host.test.ts | 1 - tests/input-admission.test.ts | 432 ++ tests/lifecycle.test.ts | 3580 +++++++++++++++++ tests/live-opencode-smoke.test.ts | 1582 ++++---- tests/live-smoke-metadata.test.ts | 160 + tests/materialize-qualification.test.ts | 5 +- tests/model-picker.test.ts | 655 ++- tests/model-preferences-store.test.ts | 230 ++ tests/model-preferences.test.ts | 470 +++ tests/native-auth-broker-cleanup.test.ts | 194 + tests/native-auth-native.test.ts | 41 + tests/native-auth.test.ts | 765 ++++ tests/native-credential-retention.test.ts | 129 + tests/native-eval-auth-cli.test.ts | 211 + tests/opencode-native-core.test.ts | 371 ++ tests/opencode-schema-contract.test.ts | 131 +- tests/package-smoke.test.ts | 12 +- tests/provenance.test.ts | 156 +- tests/qualification-cli.test.ts | 284 +- tests/recovery-authority.test.ts | 359 +- tests/recovery-capture.test.ts | 111 +- tests/recovery-episode-host.test.ts | 88 +- tests/recovery-host.test.ts | 826 ++-- tests/recovery-live-host-child.ts | 31 +- tests/recovery-live-treatment-child.ts | 249 +- tests/recovery-policy.test.ts | 243 +- tests/recovery-request-host.test.ts | 1 - tests/recovery-treatment-host.test.ts | 2 - tests/recovery-treatment.test.ts | 3 +- tests/release-1000.test.ts | 129 + tests/release-940-preparation.test.ts | 3 +- tests/report-artifact.test.ts | 11 +- tests/restore-qualified-artifact.test.ts | 115 + tests/study-host-isolation.test.ts | 23 +- tests/study-host-usage.test.ts | 333 +- tests/study-manifest.test.ts | 56 +- tests/study-runner.test.ts | 2 +- tests/study-support.ts | 4 +- tests/tool-parentage.test.ts | 600 +-- tests/typesafe-key-env.test.ts | 393 ++ tests/validation-capture.test.ts | 1273 +++--- tests/workspace-lifecycle-integration.test.ts | 57 +- 249 files changed, 40436 insertions(+), 9718 deletions(-) create mode 100644 .agents/plans/18-opencode-v2/README.md create mode 100644 .agents/plans/18-opencode-v2/architecture.md create mode 100644 .agents/plans/18-opencode-v2/baseline.json create mode 100644 .agents/plans/18-opencode-v2/compaction-boundary.md create mode 100644 .agents/plans/18-opencode-v2/decisions.tsv create mode 100644 .agents/plans/18-opencode-v2/execution.md create mode 100644 .agents/plans/18-opencode-v2/historical-policy-baseline.json create mode 100644 .agents/plans/18-opencode-v2/host-version.json create mode 100644 .agents/plans/18-opencode-v2/implementation-checkpoint.json create mode 100644 .agents/plans/18-opencode-v2/probe-extension-verification.json create mode 100644 .agents/plans/18-opencode-v2/probe-health.json create mode 100644 .agents/plans/18-opencode-v2/probe-verification.json create mode 100644 .agents/plans/18-opencode-v2/probe-version.json create mode 100644 .agents/plans/18-opencode-v2/probes/server/automatic-extension.mjs create mode 100644 .agents/plans/18-opencode-v2/probes/server/bootstrap.py create mode 100644 .agents/plans/18-opencode-v2/probes/server/config.json create mode 100644 .agents/plans/18-opencode-v2/probes/server/extended.mjs create mode 100644 .agents/plans/18-opencode-v2/probes/server/extension.ts create mode 100644 .agents/plans/18-opencode-v2/probes/server/fake-provider.mjs create mode 100644 .agents/plans/18-opencode-v2/probes/server/final-edges.mjs create mode 100644 .agents/plans/18-opencode-v2/probes/server/index.ts create mode 100644 .agents/plans/18-opencode-v2/probes/server/inspect.mjs create mode 100644 .agents/plans/18-opencode-v2/probes/server/interleavings.mjs create mode 100644 .agents/plans/18-opencode-v2/probes/server/log-check.mjs create mode 100644 .agents/plans/18-opencode-v2/probes/server/package.json create mode 100644 .agents/plans/18-opencode-v2/probes/server/run-cases.mjs create mode 100644 .agents/plans/18-opencode-v2/probes/server/run-probe.py create mode 100644 .agents/plans/18-opencode-v2/probes/server/shutdown-watch.mjs create mode 100644 .agents/plans/18-opencode-v2/probes/server/static-check.mjs create mode 100644 .agents/plans/18-opencode-v2/probes/server/steer.mjs create mode 100644 .agents/plans/18-opencode-v2/probes/server/verify-extension.py create mode 100644 .agents/plans/18-opencode-v2/probes/server/verify.py create mode 100644 .agents/plans/18-opencode-v2/probes/server/workspace.json create mode 100644 .agents/plans/18-opencode-v2/qualification-dispatch-draft.json create mode 100644 .agents/plans/18-opencode-v2/registry.json create mode 100644 .agents/plans/18-opencode-v2/research.md create mode 100644 .agents/plans/18-opencode-v2/review.md create mode 100644 .agents/plans/18-opencode-v2/reviews/compaction-design-r8.md create mode 100644 .agents/plans/18-opencode-v2/reviews/core-r1.md create mode 100644 .agents/plans/18-opencode-v2/reviews/core-r2.md create mode 100644 .agents/plans/18-opencode-v2/reviews/core-r3.md create mode 100644 .agents/plans/18-opencode-v2/reviews/core-r4.md create mode 100644 .agents/plans/18-opencode-v2/reviews/core-r5.md create mode 100644 .agents/plans/18-opencode-v2/reviews/core-r6-decisions.tsv create mode 100644 .agents/plans/18-opencode-v2/reviews/core-r6.md create mode 100644 .agents/plans/18-opencode-v2/reviews/core-r7-decisions.tsv create mode 100644 .agents/plans/18-opencode-v2/reviews/core-r7.md create mode 100644 .agents/plans/18-opencode-v2/reviews/core-r8.md create mode 100644 .agents/plans/18-opencode-v2/reviews/eval-r1.md create mode 100644 .agents/plans/18-opencode-v2/reviews/eval-r2.md create mode 100644 .agents/plans/18-opencode-v2/reviews/eval-r3.md create mode 100644 .agents/plans/18-opencode-v2/reviews/native-auth-r1-decisions.tsv create mode 100644 .agents/plans/18-opencode-v2/reviews/native-auth-r1.md create mode 100644 .agents/plans/18-opencode-v2/reviews/native-auth-r2-decisions.tsv create mode 100644 .agents/plans/18-opencode-v2/reviews/native-auth-r2.md create mode 100644 .agents/plans/18-opencode-v2/reviews/native-auth-r3-decisions.tsv create mode 100644 .agents/plans/18-opencode-v2/reviews/native-auth-r3.md create mode 100644 .agents/plans/18-opencode-v2/reviews/reconciliation.md create mode 100644 .agents/plans/18-opencode-v2/reviews/release-r1.md create mode 100644 .agents/plans/18-opencode-v2/reviews/release-r2.md create mode 100644 .agents/plans/18-opencode-v2/reviews/typesafe-env-r1.md create mode 100644 .agents/plans/18-opencode-v2/runtime/README.md create mode 100644 .agents/plans/18-opencode-v2/transport/manifest.json create mode 100644 .agents/plans/18-opencode-v2/transport/probe.ts create mode 100644 .agents/plans/18-opencode-v2/transport/session-hook-counterexample.json create mode 100644 .agents/plans/18-opencode-v2/transport/source-seal.log create mode 100644 .agents/plans/18-opencode-v2/transport/verification.json create mode 100644 .agents/plans/18-opencode-v2/validation.json create mode 100644 .agents/plans/18-opencode-v2/wiki/inventory.json create mode 100644 .agents/plans/18-opencode-v2/wiki/linkverification.json create mode 100644 .agents/plans/18-opencode-v2/wiki/parent-update.md create mode 100644 .agents/plans/18-opencode-v2/wiki/public-readback-gate.json create mode 100644 .agents/plans/18-opencode-v2/wiki/review-summary.md create mode 100644 .agents/plans/18-opencode-v2/wiki/verify.py create mode 100644 docs/adr/0018-opencode-v2-only.md create mode 100644 evals/http-transport-plugin.ts create mode 100644 evals/native-auth-config.ts create mode 100644 evals/native-auth/broker.ts create mode 100644 evals/native-auth/client-plugin.ts create mode 100644 evals/native-auth/host.ts create mode 100644 evals/native-auth/protocol.ts create mode 100644 evals/native-auth/source-plugin.ts create mode 100644 evals/opencode-host.ts create mode 100644 scripts/export-canary-native.ts create mode 100644 scripts/native-eval-auth.ts create mode 100644 src/application/model-preferences-schema.ts create mode 100644 src/application/model-preferences.ts create mode 100644 src/flow-rpc.ts create mode 100644 src/infrastructure/fs/model-preference-store.ts create mode 100644 src/platform/opencode/execution-attribution.ts create mode 100644 src/platform/opencode/flow-tool.ts create mode 100644 src/platform/opencode/http-transport.ts create mode 100644 src/platform/opencode/input-admission.ts create mode 100644 src/platform/opencode/lifecycle.ts create mode 100644 src/platform/opencode/model-picker-ui.ts create mode 100644 src/platform/opencode/model-preferences.ts delete mode 100644 src/platform/opencode/schema-adapter.ts create mode 100644 src/platform/opencode/typesafe-key-env.ts create mode 100644 tests/canary-v2-live.test.ts create mode 100644 tests/canary-v2-native-auth.test.ts create mode 100644 tests/canary-v2-native.test.ts create mode 100644 tests/eval-harness-native-host.test.ts create mode 100644 tests/eval-harness-native-outcome.test.ts create mode 100644 tests/eval-host-observation.test.ts create mode 100644 tests/eval-native-auth-config.test.ts create mode 100644 tests/eval-v2-trace-contract.test.ts create mode 100644 tests/execution-attribution.test.ts create mode 100644 tests/fixtures/continuation-commands.ts create mode 100644 tests/fixtures/native-auth-plugin.ts create mode 100644 tests/fixtures/native-auth-smoke.ts create mode 100644 tests/fixtures/native-eval-client.ts create mode 100644 tests/fixtures/native-flow-host.ts create mode 100644 tests/fixtures/opencode-v2-budget-plugin.ts create mode 100644 tests/fixtures/opencode-v2-capture-setup.ts create mode 100644 tests/fixtures/opencode-v2-isolated-candidate.ts create mode 100644 tests/fixtures/opencode-v2-reviewer-return.ts create mode 100644 tests/fixtures/opencode-v2-scripted-provider.ts create mode 100644 tests/fixtures/opencode-v2-smoke-plugin.ts create mode 100644 tests/fixtures/opencode-v2-test-host.ts create mode 100644 tests/fixtures/opencode-v2-usage-proof.ts create mode 100644 tests/fixtures/opencode-v2-validation-cases.ts create mode 100644 tests/fixtures/verified-continuation-origin.ts create mode 100644 tests/input-admission.test.ts create mode 100644 tests/lifecycle.test.ts create mode 100644 tests/live-smoke-metadata.test.ts create mode 100644 tests/model-preferences-store.test.ts create mode 100644 tests/model-preferences.test.ts create mode 100644 tests/native-auth-broker-cleanup.test.ts create mode 100644 tests/native-auth-native.test.ts create mode 100644 tests/native-auth.test.ts create mode 100644 tests/native-credential-retention.test.ts create mode 100644 tests/native-eval-auth-cli.test.ts create mode 100644 tests/opencode-native-core.test.ts create mode 100644 tests/release-1000.test.ts create mode 100644 tests/restore-qualified-artifact.test.ts create mode 100644 tests/typesafe-key-env.test.ts diff --git a/.agents/plans/18-opencode-v2/README.md b/.agents/plans/18-opencode-v2/README.md new file mode 100644 index 00000000..1dd01229 --- /dev/null +++ b/.agents/plans/18-opencode-v2/README.md @@ -0,0 +1,373 @@ +# Upgrade Flow to OpenCode v2 + +Deliver Flow 10.0.0 for OpenCode 2.1.4 and newer tested 2.x releases. +Remove OpenCode 1 support. Keep one implementation and Session v5. +Update the README, maintained documentation, and public wiki. +Publish through the existing qualified release workflow. + +Implementation started on 2026-10-24 on `feat/opencode-v2`. +The R8 candidate passes full checks and unchanged packed smoke on macOS and +Linux. Its continuation, validation, reviewer return, recovery harness, and picker +pass exact-package loopback checks. Fresh independent core review passed. +Account-helper cleanup and configurable Typesafe key selection passed their +bounded reviews. Paid qualification and publication have not started. +Current execution evidence is recorded in [execution.md](execution.md). +Research is recorded in [research.md](research.md). Decisions are recorded in +[decisions.tsv](decisions.tsv). + +## Read and execute the plan + +Run this migration under the installed Poteto `figure-it-out` workflow. +Use its hypothesis, change, verify, and retain loop at each phase. +Check a box only when its named evidence exists. +Keep runtime unknowns marked unverified until the real host proves them. +Do not substitute compilation or permission-list inspection for execution proof. + +Use one integration branch for phases 0 through 4. Intermediate host breakage +may exist there. Merge only the complete, verified v2 implementation, tooling, +and documentation. Do not add a temporary V1 adapter. +Use a second PR for qualification evidence after the candidate is frozen. +Keep source, evaluator, and package inputs fixed during that evidence PR. +Publish the wiki from its separate Git repository after the release is verified. + +The parent owns integration, authoritative checks, PRs, and external writes. +Run the repository contribution preflight before commits and pushes. +Link every created PR to the T3 thread. Review before merging. +Use the repository release process rather than replacing it with a new publisher. + +## Hold the scope + +- [x] Set the Flow release to `10.0.0` and record the tested host baseline. +- [x] State the support contract as follows. "For OpenCode v2. Requires + OpenCode 2.1.4. Later 2.x releases are supported after passing the compatibility + check. OpenCode 1.x is unsupported." +- [x] Set the new plugin package range to `>=2.1.4 <3` if the chosen + dependency arrangement passes isolated installation. Pin development and + qualification packages to exact versions. Treat this range as install + eligibility rather than proof of future compatibility. Do not infer host + versions from SDK package versions. +- [x] Use the Promise API from `@opencode/plugin`. Do not introduce an + application-wide Effect rewrite. +- [x] Preserve the application response shape, serial workflow, review rules, + source-bound validation, and Session v5 persistence contract. +- [x] Require users to finish or explicitly close active sessions before + upgrading. Do not promise cross-host continuation of an active session. +- [x] Record the requested immediate V1 removal as an explicit exception to + the prior-announcement cadence in `docs/release-qualification.md`. + Do not add a V1 bridge release to this migration. +- [x] Leave historical tags, qualification bundles, transcripts, and changelog + entries intact. Historical evidence is not an active compatibility path. + +## Phase 0. Prove the required v2 contracts + +Complete this phase before committing to the adapter design. + +- [ ] Record registry metadata and exact versions for `@opencode/cli`, + `@opencode/plugin`, and `@opencode/client`. Recheck latest 2.x at execution + start. Adopt a newer version only before candidate freeze and repeat probes. +- [ ] Capture the existing deterministic baseline with `bun run check` and + the pinned V1 packed smoke. Save commands, SHA, and sanitized results. +- [ ] Launch the selected v2 host with temporary configuration, data, and + cache directories. Leave the user's installed binary and configuration alone. + Resolve the package's platform executable and assert its reported version. + The isolated macOS version command already reports `opencode v2.1.4`. + Repeat this check for the canonical Linux qualification launcher. + Record the exact executable and argv that start a server and pass a health + request. Do not assume V1's `serve` command still starts the required host. +- [ ] Build a disposable plugin probe that loads through `Plugin.define`, + registers a command and tool, reads options, and unloads cleanly. + Keep the probe and its sanitized receipt as rerunnable evidence. + Keep disposable probe files outside the published package. +- [ ] Observe native session parentage and `session.location.directory`. + Prove that a tool call's agent and session identity can be checked against + server-owned facts. Do not confuse child-session parentage with assistant + message parentage. +- [ ] Observe a real shell tool's ID, command, call ID, completed/error result, + exit status, completeness flags, and full-output retrieval path. + Exercise a nonzero command and output larger than the inline limit. +- [ ] Prove custom-tool permission enforcement for allowed and denied roles. + Determine whether each Flow tool needs a declared permission or explicit + `ctx.permission.assert` call. Inspected rule arrays alone do not pass. +- [ ] Prove prompt admission, duplicate admission, assistant execution identity, + queued continuation, interruption, compaction, and event-stream shutdown. + V2 prompt hooks can run more than once before one admission wins. + Synthetic messages bypass that hook. +- [ ] Prove that a server-configured package loads its `./tui` entrypoint + automatically. Exercise dialogs, commands, model discovery, preference + writes, and cleanup against the same v2 server. + Inspect its actual runtime imports in an isolated packed installation. + Resolve Solid and OpenTUI imports through the public module graph. + Choose a supported import path or declare the required compatible peer set + from that proof. Declaration files and dialog signatures cannot settle it. +- [ ] Record the live-control method for the terminal. If none is available, + leave TUI interaction proof blocked. HTTP or unit tests cannot replace it. +- [ ] Resolve each failed probe before phase 1. If v2 cannot expose trustworthy + validation or continuation attribution, report the exact upstream limitation. + Do not weaken Flow's guarantees or silently remove `/flow-auto`. + +Exit with a contract map, probe source, host version, source SHA, and receipts. +Architecture exploration during execution compares native v2 attribution +choices against these receipts before selecting one. It does not compare +unverified assumptions or add a V1 compatibility design. + +## Phase 1. Port server registration and lifecycle enforcement + +Depends on phase 0. Keep host adapters in `src/platform/opencode/` and the +existing shared host configuration in `src/config-shared.ts`. +Keep domain and persistence code independent of the host API. + +- [x] Define the boundary data shapes before logic. Use the released v2 + session, prompt, tool-result, and event types. Represent completed and failed + tool observations with a discriminated union. Translate them once into the + existing application inputs. +- [x] Replace the function entrypoint in `plugin-composition.ts` and `plugin.ts` + with a stable `Plugin.define` ID and `setup(ctx)`. +- [x] Replace `sdk.ts` V1 exports with released v2 types. Remove active imports + from `@opencode-ai/plugin`, its TUI package, and `@opencode-ai/sdk/v2`. +- [x] Replace the mutable config hook in `src/platform/opencode/config.ts` and + host-facing `src/config-shared.ts` with agent, command, skill, and tool transforms as needed. + Keep replayed transform callbacks synchronous and free of one-time effects. +- [x] Register tools through `ctx.tool.transform`. Generate JSON Schema from + existing application schemas and parse inputs at the application boundary. + Delete `schema-adapter.ts` and its old host-Zod bridge when callers move. +- [x] Return Flow responses inside the v2 result's content without changing + `{status, summary, workflowData}`. Keep `flow_guidance` Markdown output. + Recognize accepted mutations from application values before host serialization. +- [x] Port `tool-guard.ts` to verified v2 session identity and location. + Preserve root-manager and reviewer-child restrictions, replay behavior, + and duplicate-runtime rejection. Test both allowed and rejected execution. +- [x] Port validation capture to the observed v2 shell contract. Preserve exact + command matching, call attribution, source binding, failure vetoes, fresh JUnit + checks, and safe full-output reads. Unknown completeness stays ineligible. +- [ ] Port command handling and `auto-drive*.ts` to v2 prompt and execution + events. Use accepted native identities and idempotent admission handling. + Preserve stale-token rejection, approval authority, stop/cancel, one eligible + automatic retry, reviewer return, and compaction recovery. +- [x] Abort subscriptions and release timers, validation captures, continuation, + and leadership during unload. Handle stream failure without leaving an + apparently active but unobserved coordinator. +- [x] Port reserved agent permissions to ordered v2 action/resource/effect rules. + Preserve worker bans on shell, delegation, Flow mutations, `.flow`, and `.git`. + Preserve the reviewer's read-only workspace, `flow_status` access, and sole + authority to submit a new reviewer completion. +- [x] Update bundled guidance that instructs native tool usage. Verify actual + tool IDs independently from permission action names. + +Run focused schema, parentage, leadership, validation, command, continuation, +and compaction tests after their respective changes. Exercise the same contracts +against the real host. Finish with `bun run typecheck` and package import proof. +Do not advance on an inconclusive identity or output-completeness result. + +## Phase 2. Port the model picker + +Depends on phase 1. Keep the `./tui` package export. + +- [x] Replace `src/tui.ts` with `Plugin.define` from `@opencode/plugin/tui`. + Use `keymap.layer`, promise-based dialogs, location data, and `ui.toast.show`. +- [x] Replace old client calls and response assumptions using the released + client declarations and live responses. Do not unwrap `.data` by convention. +- [x] Port `model-picker.ts` and preference configuration to native v2 fields. + Keep preferences owned by the server when they govern server-side roles. + Do not move shared planning and review settings into client-only `cli.json`. +- [x] Preserve project precedence, separate planning/review choices, reset to + default, explicit picker variant clearing, and the user's coding model. +- [x] Recheck connected models, current preferences, location, and activity + before save. Reject stale choices and preserve unrelated configuration. +- [ ] Prove search, unavailable selection, cancellation, save, reset, concurrent + changes, busy-session refusal, reload, and unload in the actual terminal. + Save screenshots or a recording with sanitized host receipts. + +Run picker tests and inspect the packed package's real automatic TUI loading. +The operator reviews the picker screenshots and recording before merge. +Keep JSX out unless rendering requires it. Select dependency declarations from +the runtime import proof in phase 0, including any Solid or OpenTUI peers. + +## Phase 3. Port evaluation tooling and CI + +Start its independent client research after phase 0. Integrate after phases 1 +and 2 so tests exercise the final plugin rather than a placeholder. + +- [ ] Update `package.json`, build externals, declaration output, lockfile, + architecture boundaries, package import assertions, and Dependabot pins. + V2's default export is a definition object, not a function. +- [ ] Make the smoke and eval host launch the exact `@opencode/cli` version. + Remove `opencode-ai@...` launch paths from active tooling. + Avoid an unqualified `opencode` executable in canary execution. +- [ ] Port `evals/harness.ts`, `host-observation.ts`, metadata probes, reviewer + runs, and active benchmark/recovery launchers to the v2 client and config. + Verify session trees, model/variant identity, messages, tools, and errors. +- [ ] Port or explicitly retire the recovery evaluation plugins. Preserve + paid-dispatch accounting when replacing their old hook or fetch assumptions. + Do not rewrite stored historical captures to resemble v2 output. +- [ ] Port canary preparation, verification, and `canary-run.ts`. Use a valid + local plugin directory or the packed package, not a V1 `flow.js` path. + Bind execution to the selected host, package bytes, and prepared fixture. +- [ ] Add a prospective version-specific host policy for 10.0.0 and later + v2-targeting Flow releases. Preserve the existing 9.x host pin and exact + historical policy digests. Route canary host checks through the same + version selection. Do not mutate `RELEASE_HOST_POLICY` globally. + Add regression checks that retained 9.x evidence still verifies offline. + Add a prospective release profile that keeps all 17 current release cases, + including auto-continuation and delivery. Require two route providers unless + the operator approves a documented exception. Keep historical policies stable. +- [ ] Generate the frozen sample and dispatch proposal from that profile. + With unchanged thresholds and two providers, 17 cases imply 144 primary cells + and 34 environmental reserves. Derive dispatch counts separately because + scenario followups and probes can consume additional dispatches. +- [ ] Port release host permissions and model identity checks. Check recovery + mode explicitly. Give the prospective version deliberate equivalents of + `releaseReviewerModel`, `releaseHostPermissions`, and the recovery-key refusal. + Preserve their historical 9.6.0 behavior and policy hashes. +- [ ] Retarget the weekly compatibility workflow to `@opencode/cli` latest 2.x. + Keep it a non-blocking drift signal. + Keep the exact pinned host as the reproducible blocking check. + Record the resolved host version and fail if the major is not 2. +- [ ] Prove raw archive identity across candidate, qualification, canary, and + publication. Reuse the existing sealed-artifact restoration helper for the + new release if repacking changes raw bytes despite equal contents. + Preserve both the tarball and unpacked-manifest digest checks. +- [ ] Preserve publication ref checks, release records, retry reconciliation, + trusted publishing, and asset verification. Test any release workflow edit + offline. Do not broaden historical recovery allowlists without a concrete need. +- [ ] Add checks that prevent active V1 SDK imports, V1 package launches, and + V1 installation examples from returning. Scope exclusions to immutable + historical evidence and explicit migration explanations. + +Run `bun install --frozen-lockfile`, `bun run check`, `bun run replay`, +`bun run package:smoke`, `bun audit --audit-level=high`, and +`bun run smoke:live`. Run `actionlint` when workflows change. +The packed smoke must execute allowed and denied calls, not only list rules. +Keep model-free host checks separate from paid behavioral qualification. + +## Phase 4. Update the public instructions and prepare the wiki + +Finish this phase before freezing package bytes. + +- [ ] Update `README.md`, `docs/index.md`, `docs/quickstart.md`, and + `docs/troubleshooting.md` with the exact tested minimum and V1 removal. +- [ ] Verify the v2 installation command on the selected release before + documenting it. Use `plugins` and object-form options in `opencode.json`. + Name the verified installed executable in the instructions. + Remove instructions to install Flow twice in `opencode.json` and `tui.json`. + A server plugin with `./tui` loads its terminal entrypoint automatically. +- [ ] Show the optional `cli.json` path only for a client-only installation. + Verify the package's actual local/remote behavior before recommending it. +- [ ] Document upgrade order, exact Flow version pins, restart, a clean-project + verification, model preferences, and active-session closure before upgrade. +- [ ] Update `docs/development.md`, `docs/maintainer-contract.md`, + `docs/automatic-continuation.md`, and `docs/release-qualification.md` for the + observed APIs and new release profile. Update other maintained pages only + when their contracts changed. Add a short hard-cutover ADR and changelog entry. +- [ ] Update documentation tests for native v2 configuration, current exports, + exact installation pins, current command inventory, and resolvable links. + Preserve behavioral checks rather than pinning new prose phrases. +- [ ] Prepare a separate clone of `flow-opencode.wiki.git` and record its base + SHA. Review every Markdown page, including `_Sidebar.md` and `Home.md`. + Replace stale product instructions with short current summaries and links to + canonical repository docs. Preserve existing page URLs and useful history. +- [ ] Cover installation, configuration, architecture, commands, tools, + validation/review, development, and release pages. Remove stale instructions + for the deleted installer and source tree. Repair missing video links. + Keep `docs/` authoritative instead of building another documentation system. +- [ ] Verify the staged wiki links against the final release docs. Keep the + wiki diff reviewable locally until the matching release is public. + +Run the documentation-contract test and the complete deterministic check. +Capture the clean-install terminal path for review. Merge the completed +implementation PR only after the code, tooling, and README agree. + +## Phase 5. Freeze, qualify, and publish + +Depends on all earlier gates. This is the usual full release process. +The patch and offline feature exceptions do not cover this host-major migration. + +- [ ] Pass final main CI, deterministic checks, audit, and packed real-host smoke. + Record the candidate source SHA, evaluator SHA, host version, and both archive + digests. Finish all product, dependency, README, and changelog edits first. +- [ ] Present the exact paid-run proposal. Name purpose, models, provider routes, + full dispatch limit, reserve policy, expiry, and separate canary authorization. + Create a ledger only after those values are approved. Preserve it on retries. + A general request to release does not approve a specific paid campaign budget. +- [ ] Check availability with `scripts/check-release-models.ts`. Freeze the + complete profile and case order on the canonical Linux host. +- [ ] Run the authorized release matrix against the reviewed tarball and + unpacked-manifest digests. Retain every attempt and failure. + Do not combine partial campaigns or count aborted runs as scored results. +- [ ] Prepare a fresh exact-artifact canary with `bun run eval:canary -- prepare`. + Run it through the maintained budgeted launcher. Record sanitized native + manager/reviewer evidence and the installed package identity. +- [ ] Seal with `bun run qualify -- --campaign-dir --canary + evals/canary/10.0.0.json`. Reopen and regrade the complete bundle offline. + Respect the seven-day campaign and 72-hour canary freshness limits. +- [ ] Open the evidence PR. Commit only qualification evidence and required + archive descriptors without changing measured product or evaluator inputs. + Review and merge it. Recheck exact packed identity and final main CI. +- [ ] Prepare the exact `v10.0.0` tag target and release notes for review. + Apply the repository's approval rule immediately before the tag action + that triggers npm and GitHub publication. Do not ask to publish an + unprepared candidate. +- [ ] Use the existing release workflow. Verify the event, checkout, fetched + tag, and remote main ref proofs. Retain the durable release record. + Let the existing draft, npm, asset verification, and finalization sequence run. +- [ ] Verify npm version and integrity, GitHub notes and assets, and installation + of `opencode-plugin-flow@10.0.0` into a clean v2 environment. + Confirm one server instance, commands, tools, and automatic TUI loading. +- [ ] Publish the prepared wiki commit under the authorized wiki scope. + Re-read the public pages and links. Record the wiki SHA and release URL. + Do not report the wiki complete from a local diff alone. + +The migration is done when Flow 10.0.0 is published from the verified artifact, +the clean v2 installation passes, current documentation agrees, and the public +wiki is current. No active V1 implementation remains. No v3 support is claimed. + +## Preserve these verification scenarios + +Use these scenarios across focused tests, model-free host checks, and the paid +canary. Record which layer proves each predicate. + +1. Load the packed package with all current commands, tools, and reserved roles. +2. Create a plan without granting implementation authority. +3. Reject a Flow mutation from a worker or an unrelated child session. +4. Accept a verified reviewer's submission and preserve exact accepted replay. +5. Capture passing and failing canonical commands with current-source identity. +6. Reject missing completeness, truncated output, stale JUnit, and wrong location. +7. Continue two approved features once each and close with truthful delivery. +8. Preserve approval, stop/cancel, interruption, and compaction boundaries. +9. Fail safely on duplicate load, unload, stream failure, and stale continuation. +10. Save, cancel, and reset model choices through the actual terminal picker. + +Record startup and continuation timing as diagnostics during live checks. +Set any numeric regression gate from an observed baseline before measurement. +Follow the benchmark checklist before making a measured performance claim. +This plan makes no speedup claim. + +## Keep work independent + +Blocking work is phase 0. Core server, identity, validation, and continuation +changes have one owner because they share lifecycle contracts. +TUI work can use a separate owner after the preference contract is fixed. +Eval tooling and documentation research can proceed independently after probes. +Give concurrent writers separate worktrees or explicit disjoint paths. +Serialize `package.json`, the lockfile, `config-shared.ts`, and final integration. +Use fresh independent review at the complete candidate SHA. + +## Record risks and unresolved proof + +The first blocker is v2 continuation attribution. V1 assistant parent IDs and +compaction markers cannot be assumed. The second is trustworthy shell output +and completeness. Both must pass phase 0 before release design settles. + +TUI interaction, paid model behavior, and the port itself remain unverified. +Only an isolated CLI version command ran during planning. Plugin and server +behavior remain unverified. Ref MCP was absent from the live tool +catalog. Exa, official guides, registry metadata, released declarations, and +repository inspection supplied the current evidence. + +Use Outcome-Oriented Execution to remove V1 in one delivery. +Use Foundational Thinking to prove host contracts before dependent changes. +Use Model the Domain to express host tool outcomes with typed variants. +Use Sequence Work into Verifiable Units to require each phase's evidence. +Use Prove It Works to require the packed host and published wiki checks. +Use Laziness Protocol to reuse the publisher and link the wiki to maintained docs. +Use Encode Lessons in Structure to prevent silent profile coverage loss and +misdirected compatibility checks. diff --git a/.agents/plans/18-opencode-v2/architecture.md b/.agents/plans/18-opencode-v2/architecture.md new file mode 100644 index 00000000..bc8db40f --- /dev/null +++ b/.agents/plans/18-opencode-v2/architecture.md @@ -0,0 +1,96 @@ +# Native v2 host boundary + +The selected design uses a process-local primary request snapshot for each +native session, an admission registry, and immutable assistant bindings. +It does not reconstruct authority from the latest transcript user or the +arrival order of asynchronous events. + +The context hook captures consumed input IDs before the model request. Tool +execution verifies the exact native session, assistant, agent, call, workspace, +and consumed-input prefix. A later input cannot relabel an earlier assistant. +The prompt hook can revoke old authority. Positive authority requires an exact +accepted admission joined to the private dispatch and its live epoch. + +Automatic compaction has its own checked control identity. The causal +compaction hook reads the native running automatic-compaction record. Resume +requires that same record to be completed and the same epoch to remain live. +The model-facing checkpoint uses a user role but grants no new user authority. + +## Selection and proof + +The independent Grok 4.7 cross-judge selected the request snapshot over the +larger event ledger. Its initial findings required more steering and compaction +proof. The later native probe extension supplies those observations. The parent +reran the original 49 receipt assertions and the 44-assertion live extension. +The extension passed all four cases. These counts describe bounded probe +assertions, not release qualification or provider coverage. + +The cross-judge also corrected a duplicate-admission timestamp claim in the +earlier design. Duplicate identity is the input ID and payload. Timestamps and +hook invocation counts are not admission identity. + +Retained compact receipts are `probe-verification.json` and +`probe-extension-verification.json`. The rerunnable probe lives in +`probes/server/`. Run `python3 probes/server/bootstrap.py --exe `. +It copies its owned fixtures into `/tmp/flow-v2-server-probe` and keeps all +host state there. The executable must report OpenCode 2.1.4. The default path +names the isolated Darwin executable used in this session. Linux remains a +separate verification gate. + +## Concurrent ownership + +- The server owner ports registration, role transforms, tools, session guards, + validation capture, and composition. +- The continuation owner ports command handling, request attribution, native + hooks, compaction, and continuation policy integration. +- The picker owner ports the terminal and server-owned model preferences. +- The evaluation owner ports host transport, identity observations, metadata + probes, experimental evaluation plugins, and packed live smoke. +- The canary owner ports preparation, budgeted launch, and native evidence. +- The parent owns package metadata, release policy, workflows, public docs, + the wiki draft, integration, authoritative checks, and external writes. + +Writers share the integration checkout with exclusive file ownership. +The parent runs complete checks after their changes converge. + +## Remaining product gates + +Probe success does not certify the integrated Flow package. Runtime tests must +exercise the actual packed candidate, its role permissions, source-bound +validation, reviewer child, automatic continuation, and model picker. + +No matching terminal live-control skill was installed. The native computer-use +attempt returned `Computer Use is not allowed to use the app 'com.apple.Terminal' +for safety reasons.` That restriction remains in effect. PTY receipts establish +terminal input and returned values. Visual review remains open. + +Paid qualification requires a concrete approved dispatch proposal. Publication +requires the prepared exact candidate and the repository's final approval gate. + +## Selected native account source + +The user chose existing OpenCode 2 accounts. Metadata inspection found saved +OAuth connections for OpenAI and xAI. The authenticated background service +reports 2.1.4. No real credential values or refresh tokens were exported. + +A private export bank was rejected. Native import skips an existing credential +ID, and native create returns a conflict. A cloned OAuth refresh could therefore +leave the original account stale. The key-only research helper is not adopted. + +The selected design keeps refresh in the existing native service. A temporary +project plugin resolves only approved active connections and replaces the +original refresh value with a placeholder before returning data. A private local +relay connects that authenticated native RPC to the isolated test host. The +child overrides only the selected OAuth method and delegates its refresh to the +source. The original refresh token never enters the child. Broker requests and +host leases are serialized. Account switches or source failures are refused. + +The synthetic two-host prototype persisted source rotation, preserved identity +and unrelated accounts, sent the correct bearer to a loopback model, and failed +before another model request when the source was unavailable. Its proof is +`/tmp/flow-v2-native-auth-probe/report.md`. Production integration passed native +bootstrap and shutdown proof with no model generations at +`/tmp/flow-v2-eval-auth-integration/verification.json`. Synthetic private Linux +forwarding passed separately. Actual provider qualification and independent +account-helper review remain open. +No real source-service plugin or account state has changed. diff --git a/.agents/plans/18-opencode-v2/baseline.json b/.agents/plans/18-opencode-v2/baseline.json new file mode 100644 index 00000000..a543375f --- /dev/null +++ b/.agents/plans/18-opencode-v2/baseline.json @@ -0,0 +1,52 @@ +{ + "sourceSha": "8103f1cd42e1b4d111f25d2a311935d4e41fb997", + "bunVersion": "1.4.0", + "isolatedBun": "/tmp/flow-v2-toolchain/node_modules/.bin/bun", + "install": { + "command": "bun install --frozen-lockfile", + "exitCode": 0 + }, + "oldHostPackedSmoke": { + "command": "PATH=/tmp/flow-v2-toolchain/node_modules/.bin:$PATH bun run smoke:live", + "host": "1.18.31", + "pass": 22, + "fail": 0, + "log": "/tmp/flow-v2-baseline-smoke-pinned.log" + }, + "deterministicCheck": { + "command": "PATH=/tmp/flow-v2-toolchain/node_modules/.bin:$PATH bun run check", + "status": "failed", + "log": "/tmp/flow-v2-baseline-check-pinned.log", + "observedFailure": "Two macOS temporary path aliases in materialize-qualification.test.ts and canary-run.test.ts", + "passCount": 4275, + "skipCount": 21, + "failCount": 2 + }, + "fixtureCorrection": { + "file": "tests/materialize-qualification.test.ts", + "command": "PATH=/tmp/flow-v2-toolchain/node_modules/.bin:$PATH bun test tests/materialize-qualification.test.ts", + "pass": 21, + "fail": 0, + "log": "/tmp/flow-v2-materialize-test.log", + "files": [ + "tests/materialize-qualification.test.ts", + "tests/canary-run.test.ts" + ], + "focusedPassCount": 25, + "focusedFailCount": 0 + }, + "correctedCheck": { + "status": "passed", + "log": "/tmp/flow-v2-baseline-check-fixed.log", + "passCount": 4277, + "skipCount": 21, + "failCount": 0, + "totalTests": 4298 + }, + "replay": { + "command": "bun run replay", + "gatedCassettes": 13, + "matched": 13, + "log": "/tmp/flow-v2-baseline-replay.log" + } +} diff --git a/.agents/plans/18-opencode-v2/compaction-boundary.md b/.agents/plans/18-opencode-v2/compaction-boundary.md new file mode 100644 index 00000000..7ab12fb0 --- /dev/null +++ b/.agents/plans/18-opencode-v2/compaction-boundary.md @@ -0,0 +1,29 @@ +# Compaction boundary synthesis + +Base A is selected. The parent read both packages in full. Parent rubric scores were A 15 and B 9. The independent Grok cross-judge scored A 14 and B 10 and selected the same base. These are design judgments on six criteria, not test results. The cross-judge report and both unchanged candidates remain in their original directories. + +The source-state census and exact native reload pair reject the premise that native summary completion requires a live execution origin. A single input ledger owns historical source classification, private readiness, local input reservations, and the admission frontier. A compaction boundary owns native proof and the decision between summary-only permission and continuation. Lifecycle retains execution and policy side effects. No second transcript join, copied admission-id registry, or public lease inspection is introduced. + +Use A's module and deletion plan. Move existing real candidates, accepted real origins, and private host binding into the existing admission knowledge owner. Keep InputOrigin and its revocation semantics. Historical evidence outside that owner is a discriminated ordinary or automatic fact, without a positive origin. Any originally non-null authority is historical automatic classification, including automatic=false inputs. Do not turn arbitrary errors into ordinary history. Unknown marked history after reload can summarize but cannot recover a token or satisfy checkpoint execution. + +Grafts and precise requirements: + +1. Reserve the local input frontier synchronously at command execute and synthetic entry, before session.get or any other await. Real prompt observation uses that same owner. The private registration links to its reservation and cannot bypass a later frontier. A failed or superseded reservation grants nothing. Interrupt advances execution lifetime and invalidates old scopes, but does not reserve a new input frontier. This is why a new ordinary manual summary after interruption can read old history without inventing a new input. +2. A scope captures exact input/lineage, lifecycle lifetime, and the local frontier. Capture rejects an old consumed source that does not cover the already reserved frontier. Every async boundary checks that same scope. New real, command, or synthetic reservations invalidate it, including same-epoch private work. Repeated callbacks for one unchanged input do not invent a new admission. +3. Scope cancellation has its own wake-up promise, separate from admission.ready. Lifetime, frontier, unload, and stream loss reject stale scope waits even when ready never settles. They do not reject a fresh admission's ready promise and do not call revoke on a fresh owner. Old finally removes only its own pending slot. A stale slot must not exclude a replacement operation before its old underlying request settles. +4. All pending private admissions wait for exact readiness before positive authority or source ownership checks. Do not inspect private lease.messageId, add an accessor for it, use current captureAuthority as a pending owner, or gate waiting on a partial-activation Boolean. Match the exact native receipt and payload under the existing admission policy. Readiness alone does not freeze a primary or grant tools. +5. One compaction parser proves exact running control, explicit reason auto/manual, full consumed/native prefix, and actual delivery. If admission readiness introduces another await after the first proof, reread and require the same native control/reason/prefix. The hook has no original control-id argument. Preserve the stated limit about correlation to an unread replaced control rather than inventing a field. +6. Manual summary of unknown or retired ordinary history is a summary-only result with no InputOrigin, authority, execution epoch, recovery renewal, guidance, primary, assistant binding, or tool grant. Do not copy the current host epoch into old provenance. Known automatic history, including revoked registrations/checkpoints, cannot fall back here. Current automatic manual refusal remains. Existing live ordinary auto/manual and live grantless plan/reviewer checkpoint behavior remains. +7. A continuation stores one checked fact and exactly one checkpoint origin. Repeated admit with equal host, control id, consumed ids and proved attributes reuses that fact and the same origin object. Completed context and retry return that object. Do not allocate compactedInputOrigin on every pure proof and compare the new object to the stored object. +8. Keep B's minimal summary-permit fence and dead-source projections. Keep classification/disposition exhaustive and pure inside the existing knowledge boundary when possible. Do not copy B's unavailable snapshot inputs, private messageBound, incomplete permission table, five public functions, or origin-identity reuse rule. +9. Keep execution failure cleanup tied to the exact still-live source. A scope's captured current lifetime is not an execution owner. Pending context, historical summary, or stale callback cannot revoke a fresh invocation. Preserve existing negative guards, genuine active-primary overlap refusal, immutable assistant/Request binding, strict marker/delivery checks, grantless completed-close origin, and no new advice/abandonment intent. + +The production caller surface stays admission capture/readiness plus compaction admit/completed. PrimaryHost in A is explanatory pseudocode, not a new production forwarding class. Delete duplicated lifecycle source lookup, real/accepted maps after migration, the missing-record manual branch, isUnattributedManualSummary, and the current-authority fallback for pending ownership in the same change. Preserve exact real prompt payload matching and automatic policy. + +Implementation ownership is lifecycle.ts, execution-attribution.ts, auto-compaction.ts and their lifecycle/attribution tests. auto-drive.ts, command-hook.ts, application/domain/Sessionv5, auth, Typesafe reader, preferences, harness, package, and docs stay frozen. Modules must remain below the actual 1000-line ceiling without compression. Retain the substantive src/platform/opencode/input-admission.ts module as a boundary around input knowledge. Final ownership audit shows its 509 lines and execution-attribution.ts's 454 lines could fit together below the ceiling before import deduplication. The split is therefore justified by distinct knowledge ownership, not mathematical necessity. This explicitly supersedes the earlier ceiling-dependent authorization. Its single InputAdmissions owner holds registrations, real/accepted input facts, reservations/frontier, scope cancellation, host bindings and non-authorizing lineage. ExecutionAttribution then retains primary/Request/assistant/tool identity and deletes the migrated admission methods; lifecycle calls the ledger directly. No forwarding compatibility methods or second registry remain. Record the exact module/type map before implementing it and proceed within this scope. No pass-through PrimaryHost class or formatting workaround is allowed. + +Acceptance is behavioral. Preserve the original 28-row current-behavior census and provenance correction. New acceptance tests use a fresh output directory and assert interrupted ordinary manual completion, no checkpoint-only tool, no automatic lease/advice, and a later separately accepted real input reaching its own tool. Cover repeated summary lineage and automatic tombstone refusal. Adapt the pending-activation baseline into an actual barrier test and prove no early primary/request/tool, eventual success after exact acceptance, and stale scope cancellation without settling fresh ready. Cover pre-get command/synthetic reservations, same-epoch inserts during native reads, held ready on epoch/frontier/unload, stale finally, duplicate checked-control admission identity, malformed reasons, and all existing controls. + +The implementation owner first proves focused tests, types/lint/architecture, the exact native failed sequence before and after, all nine prior compaction cases, marker/options cases, and both transport families with their intended stimuli actually reached. This is private source proof only. The root then builds one new candidate and runs the unchanged 119-assertion native smoke and other package gates before final source review. Moving full packed smoke ahead of the next review catches integrated regressions before another bounded source PASS. No archive is accepted merely because it was built. A source fix changes measured inputs and requires fresh canonical checks, exact-package proof, and independent review before qualification. + +Actual native overlapping-context reachability and same-model auxiliary retry remain unproved. No real account, credential refresh, paid generation, Git write, package publication, or wiki publication is authorized by this synthesis. The existing user goal authorizes local implementation; paid and publication gates remain separate. diff --git a/.agents/plans/18-opencode-v2/decisions.tsv b/.agents/plans/18-opencode-v2/decisions.tsv new file mode 100644 index 00000000..cde5faf9 --- /dev/null +++ b/.agents/plans/18-opencode-v2/decisions.tsv @@ -0,0 +1,71 @@ +ts phase decision why evidence result +2026-10-24T06:47:23Z scope Plan a V2-only Flow 10.0.0 migration before implementation The user requested a phased plan and removal of OpenCode 1 support .agents/plans/18-opencode-v2/README.md plan drafted +2026-10-24T06:47:23Z research Target OpenCode 2.1.4 and exclude future majors from the compatibility claim Official release notes and registry agree on the latest 2.x release https://opencode.ai/changelog; https://registry.npmjs.org/@opencode%2fcli/latest version verified +2026-10-24T06:47:23Z design Require runtime proof of attribution and shell completeness before the port V2 admission semantics and native result contracts differ from Flow assumptions src/platform/opencode/auto-drive.ts; src/platform/opencode/validation-capture.ts; https://opencode.ai/v2/docs/build/plugins/ runtime proof pending +2026-10-24T06:47:23Z release Retain all 17 cases and propose two-provider qualification for 10.0.0 The generic version profile drops eight cases and historical exceptions do not transfer evals/release-policy.ts; docs/release-qualification.md budget remains unapproved +2026-10-24T06:47:23Z wiki Preserve wiki URLs and point current summaries to maintained docs The wiki has obsolete instructions and no active synchronization workflow docs/index.md; https://github.com/ddv1982/flow-opencode/wiki wiki preparation and publication planned +2026-10-24T06:47:23Z capability Use Exa and primary sources while recording Ref as unavailable No Ref MCP tool was exposed in the live tool catalog .agents/plans/18-opencode-v2/research.md capability gap disclosed +2026-10-24T06:48:15Z verification Check plan links, phase coverage, punctuation, whitespace and trail structure The deliverable is a plan; runtime proof belongs to the planned execution gates .agents/plans/18-opencode-v2/README.md; .agents/plans/18-opencode-v2/registry.json local plan checks passed +2026-10-24T06:53:28Z probe Verify the exact new CLI launcher in isolated directories Independent review questioned the npm wrapper and platform executable name .agents/plans/18-opencode-v2/host-version.json exit 0; opencode v2.1.4; plugin behavior unverified +2026-10-24T06:56:23Z review Preserve historical host policies while adding the prospective V2 policy Independent review proved the shared host constant enters historical hashes and canary checks evals/release-policy.ts; scripts/release-metadata.ts; scripts/eval-canary.ts; .agents/plans/18-opencode-v2/review.md plan corrected; no code changed +2026-10-24T06:56:23Z review Retain CLI executable metadata and require server argv and TUI import proof Version-only execution and declaration files do not prove server or TUI loading .agents/plans/18-opencode-v2/registry.json; .agents/plans/18-opencode-v2/host-version.json; .agents/plans/18-opencode-v2/review.md macOS version launch passed; remaining probes pending +2026-10-24T06:56:24Z review Narrow support wording and correct configuration ownership The compatibility claim must name tested hosts and the shared config path must be editable .agents/plans/18-opencode-v2/README.md; .agents/plans/18-opencode-v2/review.md review findings addressed by parent +2026-10-24T06:56:44Z verification Supersede the earlier validation pointer with the retained command receipt Independent review requested evidence of the local checks rather than a pointer only to the plan .agents/plans/18-opencode-v2/validation.json PASS; implementation and plugin runtime remain unverified +2026-10-24T07:16:17Z implementation Begin the authorized migration on feat/opencode-v2 The user accepted the six-phase plan and requested implementation .agents/plans/18-opencode-v2/execution.md phase 0 in progress +2026-10-24T07:18:20Z baseline Run the old-host packed smoke under isolated Bun 1.4.0 The installed Bun 1.3.14 cannot produce repository evidence /tmp/flow-v2-baseline-smoke-pinned.log 22 pass; 0 fail +2026-10-24T07:18:20Z baseline Canonicalize the qualification test fixture root before deriving expected paths The baseline failed on macOS because the materializer resolves /var to /private/var tests/materialize-qualification.test.ts; /tmp/flow-v2-materialize-test.log focused test passed +2026-10-24T07:19:16Z baseline Canonicalize the canary test fixture root as well The child process resolves the same macOS temporary directory alias tests/canary-run.test.ts; /tmp/flow-v2-canary-run-test.log focused test passed +2026-10-24T07:23:48Z probe Prove v2 agent creation through update instead of requiring static role config The released editor has no add method but runtime update creates a missing role /tmp/flow-v2-server-probe/contract-map.md; /tmp/flow-v2-server-probe/plugin.jsonl native role registration verified +2026-10-24T07:23:48Z design Compare event-prefix attribution with request-hook snapshots before choosing Assistant parent links are absent and live stream arrival is not a causal barrier /tmp/flow-v2-attribution-design/design.md; /tmp/flow-v2-request-design.md independent cross-judge running +2026-10-24T07:23:48Z probe Reject session.log replay as a proven attribution barrier The public client returned only log.synced for omitted, zero, one, and five cursors /tmp/flow-v2-parent-log-probe.mjs; /tmp/flow-v2-parent-log-probe.jsonl replay remains unproved +2026-10-24T07:24:17Z baseline Pass the complete baseline after the two path-fixture fixes The migration needs a known deterministic starting point .agents/plans/18-opencode-v2/baseline.json; /tmp/flow-v2-baseline-check-fixed.log 4277 pass; 21 skip; 0 fail; 13 replay cassettes match +2026-10-24T07:35:37Z dependency Upgrade pinned Zod from 4.4.3 to 4.6.7 before candidate freeze The required high audit flags GHSA-mrhx-c7xc-m3w5 below 4.6.6 https://github.com/advisories/GHSA-mrhx-c7xc-m3w5; /tmp/flow-v2-port-audit.log; package.json schema and replay verification pending +2026-10-24T07:35:37Z probe Parent rerun confirms compaction continuity and both stale-authority vetoes The final experiment had to prove native behavior before continuation implementation /tmp/flow-v2-parent-extension-run.log; /tmp/flow-v2-server-probe/extension-verification.json 44 assertions pass; 49 original receipt assertions pass +2026-10-24T07:35:37Z release Add a prospective host/profile selection without changing retained 9.x inputs A global host-pin change would invalidate published policy and canary evidence evals/release-policy.ts; tests/release-1000.test.ts; /tmp/flow-v2-release-policy-test.log all 17 cases; 144 primary and 34 reserve cells; historical hashes unchanged +2026-10-24T07:35:37Z capability Keep terminal visual review open after the native computer-use attempt was rejected The tool refuses Terminal and that restriction must not be bypassed .agents/plans/18-opencode-v2/execution.md PTY behavior observed; visual approval pending +2026-10-24T07:41:26Z docs Document the v2 setup and host boundary before freezing package bytes The upgrade changes native config, continuation attribution, and picker persistence README.md; docs/adr/0018-opencode-v2-only.md; /tmp/flow-v2-docs-test.log documentation contract passed +2026-10-24T07:41:27Z docs Budget the new host-cutover instructions and ADR explicitly New v2 setup and proof contracts add maintained prose; retained ADR history stays intact tests/documentation-contract.test.ts; docs/adr/0018-opencode-v2-only.md 4500 prose bytes and 3000 record bytes added; per-page limit unchanged +2026-10-24T07:58:01Z wiki Prepare a reviewed local wiki diff while keeping docs authoritative The separate wiki has stale V1 instructions and must publish after the matching release .agents/plans/18-opencode-v2/wiki/wiki.patch; .agents/plans/18-opencode-v2/wiki/linkverification.json 45 files audited; 293 local checks pass; public readback pending +2026-10-24T07:58:01Z release Restore prospective release archives from their verified bundle manifest Raw tarball bytes can differ after an equivalent rebuild and both identities must survive publication scripts/restore-exact-release-artifact.ts; tests/restore-qualified-artifact.test.ts; .github/workflows/release.yml exact bytes restored; changed contents and wrong version refused +2026-10-24T07:58:01Z picker Use one plugin-owned preference file with owner-safe writes and watch refresh Native config cannot write these values and plugin storage has no conditional transaction src/infrastructure/fs/model-preference-store.ts; /tmp/flow-v2-picker-implementation/independent/verification.json 34 focused tests pass; independent native server activity and reload proved +2026-10-24T08:10:53Z integration Preserve native evidence with versioned V2 trace and composite tool identity V2 has no assistant parent or independent part ID; inventing them would falsify release evidence evals/host-trace.ts; /tmp/flow-v2-eval-progress-port/coverage.json Native port assigned; 81 regression predicates retained; proof gates pending +2026-10-24T08:10:53Z verification Replace the debug candidate after native success-event routing was corrected The previous archive did not dispatch continuation after session.execution.succeeded /tmp/flow-v2-continuation-implementation/report.md; /tmp/flow-v2-candidate-refresh/opencode-plugin-flow-10.0.0.tgz Source snapshot passed 40 assertions; refreshed archive awaits same runtime check +2026-10-24T08:14:21Z accounting Version new study accounting for native OpenCode 2.1.4 while retaining old policy decoding The packed host separates completion output and reasoning and subtracts cached input /tmp/flow-v2-packed-eval-parent/validation.json; tests/study-manifest.test.ts; /tmp/flow-v2-study-port.log Provider completion9/reasoning4/cache3 became native output5/reasoning4/input10/cache3; 96 focused tests pass +2026-10-24T08:17:22Z review Fix canary isolation and exact durable-session binding before qualification Independent review reproduced ambient configuration leakage and accepted unrelated closed-session evidence /tmp/flow-v2-release-review-r1/report.md; /tmp/flow-v2-release-review-r1/evidence-splice-repro.json Two P2 defects assigned; historical bytes and hashes independently unchanged; fresh review required +2026-10-24T08:18:49Z review Accept the native evidence port only with strict separate serialized versions The historical V1 schema cannot gain optional fields or synthetic IDs to fit V2 /tmp/flow-v2-evidence-port/report.json; /tmp/flow-v2-evidence-port/v1-schema-receipt.json; tests/eval-v2-trace-contract.test.ts 314 focused tests pass; initial marker and compaction-export limits retained; independent review started +2026-10-24T08:21:58Z review Terminate failed request lifetimes and persist the initial marker without granting early authority Grok core review found a stream phase wedge and the missing native initial token /tmp/flow-v2-core-review-r1/report.md; src/platform/opencode/execution-attribution.ts; src/platform/opencode/lifecycle.ts Two high-severity fixes assigned to continuation owner; final packing held +2026-10-24T08:29:27Z transport Select HTTP with native provider, model, and variant settings The pinned host ignores V1 flags and session HTTP hooks did not prevent a WebSocket route /tmp/flow-v2-http-contract/session-hook-counterexample.json; /tmp/flow-v2-http-contract/verification.json; src/platform/opencode/http-transport.ts Bare and hook-only each used WebSocket; exact production helper selected HTTP even for a WebSocket variant; no paid calls +2026-10-24T08:41:22Z correction Supersede the R1 missing-marker claim for its recorded source identity Independent R2 source-map hashing proves lifecycle hash 1e8d already persisted the marker; the earlier archive counterexample has different source bytes .agents/plans/18-opencode-v2/reviews/reconciliation.md; /tmp/flow-v2-core-review-r2/evidence-checks.json R1 originals retained; rows 3 and 6 corrected; old 0e archive remains a valid missing-marker counterexample +2026-10-24T08:41:22Z verification Retain the complete passing local check before the final R2 edge-case fix All integration ports now pass, while independent review still found a response-less failure path /tmp/flow-v2-full-check-review-candidate.log; .agents/plans/18-opencode-v2/runtime/packed-eval-224f.json 4450 tests pass, 25 skip, zero fail; source freeze held for that run; R2 P2 remains open +2026-10-24T09:09:31Z review Preserve rotated native credentials even when drain fails Native shutdown must list credentials while the server remains alive independently of active-session drain evals/harness.ts; tests/native-credential-retention.test.ts; /tmp/flow-v2-eval-r2-fixes-final.log Five shutdown modes pass; full typecheck passes; new independent eval review pending +2026-10-24T09:11:39Z authentication Use existing OpenCode 2 accounts through a private selected native export The user selected the native accounts; native metadata lists stored OpenAI and xAI connections without exposing credential values /Users/vriesd/.opencode/bin/opencode2 auth list --format json --standalone; /tmp/flow-v2-native-auth-probe Account metadata only; no real export, provider generation, native store writeback, or paid authorization +2026-10-24T09:17:46Z verification Keep stale callback ownership and native OAuth refresh unresolved until proved Core R4 reproduced new-invocation revocation; native import skips existing IDs and create conflicts, so a rotated export cannot update the original account .agents/plans/18-opencode-v2/reviews/core-r4.md; /tmp/flow-v2-native-auth-probe/discovery-receipt.json Core fix assigned with complete async revocation census; native broker alternatives remain disposable probes; no real secrets exported +2026-10-24T09:25:34Z verification Use the pinned Bun YAML parser in the release-workflow test A clean Linux frozen install lacks the undeclared yaml module that the Mac cache retained tests/release-940-preparation.test.ts; /tmp/flow-v2-yaml-parser-port.log; /tmp/flow-v2-linux/early-types-lint.log 25 existing workflow tests and 121 assertions pass on both platforms; Linux typecheck and lint pass +2026-10-24T09:33:13Z verification Keep Linux verification on an isolated source snapshot The release host policy requires Linux, while the Mac workspace is still receiving scoped fixes /tmp/flow-v2-linux/setup.json; /tmp/flow-v2-linux/first-full-check.log Clean frozen install, types, lint, build and 4494 tests pass with 25 skips; snapshot predates current callback and OAuth integration work; no paid calls +2026-10-24T10:07:35Z documentation Document native account setup and configurable Typesafe key names within an explicit envelope The user added credential-name selection and chose native OAuth accounts; their configuration and release setup need discoverable documentation README.md; evals/README.md; docs/release-qualification.md; docs/maintainer-contract.md; docs/troubleshooting.md; tests/documentation-contract.test.ts Removed 367 bytes of repeated new prose, then allocated 800 bytes for the two new credential surfaces; documentation contract rerun recorded separately +2026-10-24T10:22:18Z configuration Ship configurable Typesafe variable selection with no custom-name fallback The user requested a name-only option; one setup-scoped reader keeps activation and authentication consistent /tmp/flow-v2-typesafe-env-review-r1/report.md; src/platform/opencode/typesafe-key-env.ts; README.md Independent bounded PASS; 92 focused tests plus eight probes and 15 documentation tests; final packed proof pending +2026-10-24T10:22:18Z authentication Accept native account boot and shutdown proof without claiming provider qualification Actual isolated EvalHost lifecycle must establish readiness, private configuration, and cleanup before real account use /tmp/flow-v2-eval-auth-integration/verification.json; /tmp/flow-v2-eval-auth-integration/input-verification.json PASS with zero model generations, all six owned native processes stopped, user service untouched +2026-10-24T10:22:18Z documentation Refresh the existing wiki configuration page and preserve the original audit The final user-requested Typesafe option needs the same name-only contract in wiki instructions .agents/plans/18-opencode-v2/wiki/parent-update.md; /tmp/flow-v2-wiki-final-draft/linkverification.json 45 pages and 293 local checks pass; nothing published; public readback remains gated +2026-10-24T10:25:03Z evidence Keep generated release smoke scaffolding outside the commit The repository contribution skill forbids archiving exact smoke artifacts without a maintainer request .agents/plans/18-opencode-v2/runtime/README.md; /tmp/flow-v2-release-smoke-scaffolding Four old compact receipts moved unchanged; original decision-log location is superseded, not rewritten +2026-10-24T10:28:27Z freeze Build the final product archive while evaluator-only account review finishes All packaged source reviews pass and src imports no evals or scripts; native-auth helper edits cannot change package contents /tmp/flow-v2-candidate-final/candidate.json; /tmp/flow-v2-core-review-r5/report.md; /tmp/flow-v2-typesafe-env-review-r1/report.md Product archive d3d46d92 and manifest29a1d475 frozen; final exact native QA running, account source review remains open +2026-10-24T10:28:27Z portability Remove research-directory assumptions from committed auth test fixtures Clean Linux execution proved two fixtures relied on a local temp parent; tar ownership separately caused Git trust failures tests/native-auth.test.ts; tests/fixtures/native-auth-smoke.ts; /tmp/flow-v2-linux/final-full-check.log; /tmp/flow-v2-linux/portability-recheck.log Production unchanged; 13 portable unit tests and native two-host test pass; 35 failed Linux cases rechecked successfully; full Linux repeat running +2026-10-24T10:32:39Z verification Accept full Linux verification and exact raw archive reproduction across platforms Model-free Linux execution catches launcher, process-lifecycle, and packing assumptions that macOS alone cannot prove /tmp/flow-v2-linux/final-full-check-recheck.log; /tmp/flow-v2-linux/final-packed-smoke-init.log; /tmp/flow-v2-linux/packed-reproducibility.log 4603 pass, 27 skip, zero fail; packed smoke119 assertions pass; Linux raw archive exactly matches d3d46d92; first no-init cleanup failure retained as runner correction +2026-10-24T10:36:18Z review Fix account-helper cleanup independently from the frozen product R1 reproduced skipped owned-directory cleanup after broker identity or unload failure; the aborted hook hypothesis and source single-flight held /tmp/flow-v2-native-auth-review-r1/report.md; /tmp/flow-v2-native-auth-review-r1/hook-results.json; /tmp/flow-v2-native-auth-review-r1/repro-results.json P2 broker cleanup assigned; bounded P3 attachment/key-validation corrections included; no product source or archive change +2026-10-24T10:44:30Z runtime Reject d3d46d92 on actual pre-context compaction, despite passing ordinary packed gates A fresh admitted command can reach automatic compaction before its first primary; the handler incorrectly captures the retained previous primary epoch /tmp/flow-v2-final-continuation-qa-zc8ncill/native-stale-callback-verification.json; /tmp/flow-v2-final-continuation-qa-zc8ncill/verification.json Blocking runtime defect assigned with full compaction/admission census; 127 standard native assertions and three key-option cases still pass; no paid work or publication +2026-10-24T10:44:30Z review Review account cleanup fixes in a fresh Codex R2 task The Grok R1 cleanup reproductions are corrected without changing source-owned OAuth or hash formats; code needs an independent current-byte verdict /tmp/flow-v2-native-auth-probe/review-r1-fixes/handoff.md; /tmp/flow-v2-native-auth-probe/review-r1-fixes/freeze.json R2 running in isolated output directory; no product acceptance implied; client add-on must be resealed after its changed bytes +2026-10-24T10:52:04Z review Normalize public auth disposal failures while retaining failed-cleanup truth Independent R2 closed R1 but actual inaccessible-directory and injected relay-stop errors bypass the stable authentication error boundary /tmp/flow-v2-native-auth-review-r2/report.md; /tmp/flow-v2-native-auth-review-r2/failure-results.json P3 assigned in two public finalizers; local failure must remain a generic failure, with no false cleanup success or unpinned RPC +2026-10-24T10:58:46Z review Run fresh Grok R3 on the two disposal-completion error normalizers The public boundary must hide raw filesystem paths while preserving genuine failure and independent cleanup /tmp/flow-v2-native-auth-probe/review-r2-fixes/handoff.md; /tmp/flow-v2-native-auth-probe/review-r2-fixes/freeze.json 27 focused tests pass after four baseline failures; client e8e4bed7 unchanged; independent R3 running while core fix remains separate +2026-10-24T11:04:49Z review Review exact accepted-input compaction authority in fresh core R6 Compaction can precede a provider primary, so retained request identity cannot supply admission authority; the chosen registry keeps both domains separate /tmp/flow-v2-preprimary-compaction-fix-9yngneay/handoff.md; /tmp/flow-v2-preprimary-compaction-fix-9yngneay/freeze-for-r6.json; /tmp/flow-v2-comment-review-r6/manifest.json Five source files frozen,300 focused and5 native cases reported passing; Codex R6 running, no replacement archive accepted +2026-10-24T11:11:37Z verification Pass both canonical suites on frozen R6 source without treating them as source-review or release approval The accepted-input compaction fix and generic auth disposal errors need whole-tree integration checks on both operating systems /tmp/flow-v2-r6-check-macos.log; /tmp/flow-v2-linux/r6-full-check.log; /tmp/flow-v2-r6-documentation.log; /tmp/flow-v2-linux/r6-documentation.log Each full check4643 pass27 skip0 fail; current ADR wording passes15 docs cases on both; fresh core R6 and auth R3 remain pending before a new archive +2026-10-24T11:17:33Z review Keep no-lease coding provenance distinct from automatic Flow authority Fresh R6 independently reproduced ordinary coding failing after real automatic compaction in two prior states; global attribution must not require an automatic lease /tmp/flow-v2-core-review-r6/report.md; /tmp/flow-v2-core-review-r6/native-review-verification.json; /tmp/flow-v2-core-review-r6/same-epoch.json P2 accepted for correction; four marked-authority native cases pass; same-epoch component objection retained without a false native-reachability claim +2026-10-24T11:17:33Z review Accept bounded native-account helper R3 source PASS Actual public disposal methods normalize private errors while retaining failed-cleanup truth and all prior corrections /tmp/flow-v2-native-auth-review-r3/report.md; /tmp/flow-v2-native-auth-review-r3/boundary-results.json 63 scoped tests pass1 native skip; client e8e4bed7 unchanged; native/real-account qualification remains separate; helper source frozen +2026-10-24T11:36:35Z prototype Observe ordinary manual compaction before reviewing the generalized grantless path Removing the no-lease early return sends grantless manual controls to a helper that requires reason auto; native operation behavior is an observable fact /tmp/flow-v2-grantless-compaction-fix-okv2px7d/source.patch; /tmp/flow-v2-grantless-compaction-fix-okv2px7d/handoff.md Bounded native probe assigned before R7, no defect or grant change inferred; existing seven-case source proof remains scoped and immutable +2026-10-24T11:44:42Z visual Build a terminal-cell replay lever from actual recorded picker output The remaining UI review needs a concrete readable artifact; static replay can inspect native PTY output without controlling the denied Terminal app /tmp/flow-v2-terminal-render/frames.cjs; /tmp/flow-v2-terminal-render/prototype/frames.json; /tmp/flow-v2-terminal-render/prototype/replay.html Three120x36 dialogs rendered and inspected; old rejected-candidate prototype only; final capture and operator visual sign-off remain open +2026-10-24T12:10:47Z review Start fresh R7 after both actual manual-compaction regressions are corrected Native summary operations and positive execution provenance need separate handling; reason manual must never carry automatic authority /tmp/flow-v2-manual-compaction-check-su6aydtb/handoff.md; /tmp/flow-v2-manual-compaction-check-su6aydtb/freeze-final.json; /tmp/flow-v2-comment-review-r7-final/manifest.json 336 focused and9 native cases reported passing; Grok R7 and both canonical suites running; no replacement archive or release approval +2026-10-24T12:48:58Z runtime Reject archive 74da6ab8 Both unchanged native smoke suites fail manual compaction after cancelled ordinary input; source PASS was bounded /tmp/flow-v2-candidate-r7/status.json;/tmp/flow-v2-product-r7-qa-47fb9cwz/verification.json Rejected; no release approval +2026-10-24T12:48:58Z evidence Label rewritten census JSON as parent rerun Parent ran fixture in place before noticing afterAll output path; original owner log/test retained; no first-run JSON reconstruction /tmp/flow-v2-compaction-design-r8/census-provenance-correction.json 28 current-behavior cases pass; diagnostic only +2026-10-24T12:48:58Z design Separate summary operation from live authority before next fix 28-row census and exact native reload pair identify retained-origin asymmetry; separate pending activation component reproduced /tmp/flow-v2-compaction-design-r8/root-checkpoint.json Two isolated read-only sketches; production frozen +2026-10-24T14:02:58Z r8-proof Keep the sole input ledger and native summary boundary The old premise rejected interrupted ordinary history; new summary records carry no positive origin .agents/plans/18-opencode-v2/compaction-boundary.md; /tmp/flow-v2-compaction-r8-bnmny7om/freeze-final.json Frozen source, private proof and exact package gates pass; fresh review running +2026-10-24T14:02:58Z r8-package Retain archive fc19ca4a as the proof candidate Both platforms pass unchanged119 smoke and4700 canonical tests; raw Linux archive is identical /tmp/flow-v2-candidate-r8/candidate.json; /tmp/flow-v2-candidate-r8/macos-check.json; /tmp/flow-v2-candidate-r8/linux-check.json Synthetic QA only; paid qualification and publication not authorized +2026-10-24T14:02:58Z r8-comment Delete one redundant lifecycle test comment after QA snapshots close Independent Comment Sicko found narration with no exception and no application-code target /tmp/flow-v2-comment-review-r8-independent/report.md; /tmp/flow-v2-candidate-r8/review-freeze.json One line removed; product bytes and test expressions unchanged +2026-10-24T14:07:14Z r8-review Accept the bounded fresh core R8 PASS Independent review found no actionable causal defect after packed smoke; source, archive and supplemental test hashes match /tmp/flow-v2-core-review-r8/report.md; .agents/plans/18-opencode-v2/reviews/core-r8.md Ready for local contribution gates and paid pre-push approval; no release approval diff --git a/.agents/plans/18-opencode-v2/execution.md b/.agents/plans/18-opencode-v2/execution.md new file mode 100644 index 00000000..6a694a84 --- /dev/null +++ b/.agents/plans/18-opencode-v2/execution.md @@ -0,0 +1,134 @@ +# Implement the OpenCode 2 migration + +The user authorized implementation on 2026-10-24. +The integration branch is `feat/opencode-v2`, based on +`8103f1cd42e1b4d111f25d2a311935d4e41fb997`. + +## Completion gate + +Flow 10.0.0 loads on the pinned OpenCode 2 host. Its commands, tools, +permissions, source-bound validation, automatic continuation, and model picker +pass their real-host checks. The packed package, evaluation tooling, README, +and maintained documentation agree on the v2 contract. Historical release +evidence remains verifiable. Qualification and publication follow the existing +release process after their concrete approval gates. + +## Scope and rigor + +The current host adapter has 17 TypeScript modules plus the terminal entrypoint. +The evaluation host and packed live smoke add two large integration paths. +This migration changes execution identity and permission enforcement, so the +runtime contract probes precede implementation. Domain and persistence behavior +remain fixed. Temporary breakage is confined to the integration branch. + +## Throughput checkpoint + +The first blocking path is runtime proof, followed by the server adapter and +continuation attribution. Two investigators own separate disposable directories. +One probes server behavior and identity. The other probes terminal loading and +dependencies. Both use Codex gpt-6.1-sol with high reasoning and priority service. +They cannot edit this checkout. The parent restores the pinned toolchain, +captures the baseline, integrates code, and runs authoritative checks. + +Prefer a rerunnable probe over repeated manual inspection. Do not parallelize +product writes until the observed contracts establish independent ownership. +Paid model calls and release publication remain gated on concrete proposals. + +## Execution sequence + +1. Capture the baseline and prove phase 0 contracts in isolated host homes. +2. Compare attribution designs against those observations and select one. +3. Port the server adapter and prove its identity and validation boundaries. +4. Port the model picker and prove its terminal behavior. +5. Port evaluation and release tooling while preserving historical hashes. +6. Update maintained documentation and prepare the separate wiki diff. +7. Run complete deterministic and real-host verification before candidate freeze. +8. Prepare qualification, exact-artifact canary, and publication for their gates. + +Each unit records its hypothesis, change, evidence, and verdict in +`decisions.tsv`. An inconclusive runtime observation does not pass a gate. + +## Integration checkpoint + +The first migrated whole-suite run had 133 failures. The second had 4,398 +passes, 25 skips, and 13 failures. The remaining failures were native-event +fixtures, stale host pins, cancellation fixtures, and an exact grader-source +snapshot changing while the qualification test ran. The later complete check passed 4,461 tests with 25 skips and zero failures. +That check predates the combined compaction and retry fix and the evaluator +shutdown and isolation fixes. A final complete run remains required. + +The native evidence port retains strict V1 serialized schemas and adds a V2 +trace with endpoint-bound sessions and composite tool identities. It does not +invent assistant parents or independent part IDs. Its 314 focused tests passed. +The initial native command marker is persisted before admission. Exact packed +host probes prove runtime compaction continuity. The final export alone cannot +prove that continuity. + +Independent release review found ambient configuration inherited by canary +launch and a missing identity join between native close evidence and the durable +Flow session. Both were fixed and passed independent release review R2. Evaluator review R2 +found separate credential-drain and ambient reviewer-variant defects. Their +source fixes pass focused tests and typecheck. A fresh eval review remains due. + +The draft qualification schedule has 180 primary manager dispatches, 46 reserve +dispatches, and two route probes. Its maximum is 228 top-level dispatches. +Internal generation is separate. This draft has no spending authorization and +is not a dollar cap. Canary authorization remains separate. + +The user selected existing OpenCode 2 accounts as the authentication source. +A synthetic source-owned OAuth resolver passed its bounded mechanism tests. +Its production integration remains in progress. This selection does not +authorize paid model calls. No real credential values belong in evidence, logs, +arguments, or repository files. + +## Final Typesafe configuration addition + +The user requested an option to choose the environment variable containing the +Typesafe API key. Implement this after the core and authentication integration, +before final packing and qualification. Preserve `TYPESAFE_API_KEY` as the +default. An explicit name reads only that variable. Missing or blank values keep +advice unavailable, and errors must not include values. Update the README and +maintained configuration documentation. Verify custom-name activation, default +behavior, missing custom values, invalid names, and recovery-off isolation with +a fake transport. No live Typesafe request is authorized by this addition. + + +## R8 frozen candidate + +Attack the Premise separated native summary permission from live execution +provenance after the R7 archive failed cancellation followed by manual compaction. +The selected input ledger owns admission, readiness, lifetime, frontier, and +historical lineage. Request attribution owns primary and tool identity. The +compaction boundary validates native control, reason, prefix, and delivery. +Interrupted ordinary history can summarize without an execution origin. Known +automatic tombstones cannot take that fallback. Unmatched callbacks cannot revoke +a different accepted source. Repeated checked continuations reuse their origin. + +The source freeze is `/tmp/flow-v2-compaction-r8-bnmny7om/freeze-final.json`. +The parent verified its ten files and all 75 actual server map contents. A +separate parent supplement records one accepted test-comment deletion after +all runtime byte audits closed. Product source and archive bytes did not change. + +The proof candidate is `/tmp/flow-v2-candidate-r8/opencode-plugin-flow-10.0.0.tgz`. +Its raw SHA-256 is `fc19ca4a55a8f1684fe9e1c988adce2536a667a339a3a0e622d0c2e39518195d`. +Its unpacked manifest SHA-256 is +`667d6029038248b4222e28c8e5f7656bf1534286441335925faa4c2d20f3c95a`. +Linux reproduced the exact raw archive. Both systems passed the unchanged native +smoke with 119 assertions. Both canonical checks passed 4,700 tests with 27 skips +and zero failures across 184 files. The macOS/Linux assertion counts are 98,734 +and 98,731. These runs precede only the comment deletion, which changes no test +expression. Generated runtime receipts stay outside the commit. + +The exact archive passed all 16 continuation profiles and the original +interrupted-ordinary/manual-summary sequence, then a separately accepted ordinary +input executed its protected tool. The normal recovery harness passed three tests +using four native hosts with zero claims and no overlay. Both picker routes passed +eight phases. Source maps have 75 server rows and five TUI rows across 77 unique +project paths. Recorded 120-by-36 terminal cells passed visual replay inspection; +this does not claim live native font or pixel verification. + +Fresh independent core R8 review passed after packed smoke, with 496 offline +tests and no actionable source findings. The other review verdicts remain scoped. Same-model auxiliary retry, native overlapping context, +and unread original-control correlation remain unproved. The trailing-history +and pre-dispatch concurrency neighbors are component evidence. No real account, +paid provider call, Git publication, or release is covered by these results. diff --git a/.agents/plans/18-opencode-v2/historical-policy-baseline.json b/.agents/plans/18-opencode-v2/historical-policy-baseline.json new file mode 100644 index 00000000..f615ffa1 --- /dev/null +++ b/.agents/plans/18-opencode-v2/historical-policy-baseline.json @@ -0,0 +1,26 @@ +{ + "9.1.0": { + "policy": "sha256:6146ace9217f1c0555e2e0d8c55c79b63f57534b0155f180dbd67e102b2cbd08", + "host": "sha256:bc865ab8b4c506a874c2e380c5a7d9ba973a6fcdb12df82a3574dd6acba36312" + }, + "9.2.0": { + "policy": "sha256:6146ace9217f1c0555e2e0d8c55c79b63f57534b0155f180dbd67e102b2cbd08", + "host": "sha256:bad12101c8bffda4d20ac5e8fc61c595108672b2b08e3012a4f6490e929c7da3" + }, + "9.3.0": { + "policy": "sha256:be567f530fc5d9e1c25519a22a2d96f9b82aeb3849fe032b4fe2bb6c847ff265", + "host": "sha256:e93ef3eaf4a86a954580896b46b719a07e6cfecef91175c8b61987e712388828" + }, + "9.4.0": { + "policy": "sha256:94cbb4ea9f07efb28760ff001a9a32e627a12448fa1fd5e166086fb6ecdaf9b2", + "host": "sha256:0c0f345dddedafdbae340f1809fdfa295e3bb9e49aec0146eaaa3bdebf61ab63" + }, + "9.5.0": { + "policy": "sha256:b969b7aeacbdfb6b5194218e9fb18c014b66148f4b8c8cc0293fbdcee4cdd10c", + "host": "sha256:9d21ff22c74157ce1b9611b38589f9069180b094428b7bdd776a759ba49ea3f3" + }, + "9.6.0": { + "policy": "sha256:8b1ca2667a4ef2df322b07eb73ff1d1bfb585e2c03481d358f89eb25bb6448b8", + "host": "sha256:370ad37456462add03aa780ba00ed7ea417ce066b0631cb03bd5a41f31c1cf82" + } +} diff --git a/.agents/plans/18-opencode-v2/host-version.json b/.agents/plans/18-opencode-v2/host-version.json new file mode 100644 index 00000000..fcaadc57 --- /dev/null +++ b/.agents/plans/18-opencode-v2/host-version.json @@ -0,0 +1,15 @@ +{ + "observedOn": "2026-10-24", + "platform": "Darwin", + "architecture": "arm64", + "package": "@opencode/cli@2.1.4", + "launcher": "npm exec --cache /tmp/flow-opencode-v2-plan-host/npm-cache --yes --package=@opencode/cli@2.1.4 -- opencode --version", + "environment": { + "XDG_CONFIG_HOME": "/tmp/flow-opencode-v2-plan-host/config", + "XDG_DATA_HOME": "/tmp/flow-opencode-v2-plan-host/data", + "XDG_CACHE_HOME": "/tmp/flow-opencode-v2-plan-host/cache" + }, + "exitCode": 0, + "versionOutput": "opencode v2.1.4", + "scope": "Version invocation only. No server, plugin, TUI, or provider behavior verified." +} diff --git a/.agents/plans/18-opencode-v2/implementation-checkpoint.json b/.agents/plans/18-opencode-v2/implementation-checkpoint.json new file mode 100644 index 00000000..3417f67c --- /dev/null +++ b/.agents/plans/18-opencode-v2/implementation-checkpoint.json @@ -0,0 +1,397 @@ +{ + "branch": "feat/opencode-v2", + "base": "8103f1cd42e1b4d111f25d2a311935d4e41fb997", + "bun": "/tmp/flow-v2-toolchain/node_modules/.bin/bun", + "nativeHost": "/tmp/flow-opencode-v2-plan-host/npm-cache/_npx/2314e955696d2fea/node_modules/@opencode/cli-darwin-arm64/bin/opencode", + "candidate": { + "path": "/tmp/flow-v2-candidate-r8/opencode-plugin-flow-10.0.0.tgz", + "sha256": "fc19ca4a55a8f1684fe9e1c988adce2536a667a339a3a0e622d0c2e39518195d", + "unpackedManifestSha256": "667d6029038248b4222e28c8e5f7656bf1534286441335925faa4c2d20f3c95a", + "status": "Synthetic packed QA and fresh independent R8 source review PASS. Candidate is ready for paid pre-push smoke authorization; no release qualification or publication approval.", + "sourceBinding": "/tmp/flow-v2-candidate-r8/review-source-binding.json" + }, + "verification": { + "check": "/tmp/flow-v2-r7-check-macos.log", + "tests": { + "pass": 4679, + "skip": 27, + "fail": 0 + }, + "packed": "/tmp/flow-v2-product-r7-qa-47fb9cwz/verification.json", + "continuation": "/tmp/flow-v2-packed-r7-continuation-qa-uljdjwv7/runs/run-20261024-143740-052985/verification.json", + "picker": "/tmp/flow-v2-picker-implementation/packed-r7-74da6ab8/verification.json", + "canaryFixes": "/tmp/flow-v2-canary-implementation/review-r1-fixes/report.json", + "postCheckTestOnlyCorrection": { + "files": [ + "tests/native-auth.test.ts", + "tests/fixtures/native-auth-smoke.ts" + ], + "reason": "Remove dependence on the existing research temp directory and avoid overwriting earlier receipts", + "focused": "/tmp/flow-v2-native-auth-portability.log", + "native": "/tmp/flow-v2-native-auth-portable-native.log", + "linuxFocused": "/tmp/flow-v2-linux/portability-recheck.log" + }, + "replay": "/tmp/flow-v2-final-replay.log", + "audit": "/tmp/flow-v2-final-audit.log", + "actionlint": "/tmp/flow-v2-final-actionlint.log", + "frozenInstall": "/tmp/flow-v2-final-frozen-install-macos.log", + "documentation": "/tmp/flow-v2-final-documentation.log", + "commentInventory": "/tmp/flow-v2-comment-review-r7-final/manifest.json", + "recovery": "/tmp/flow-v2-product-r7-qa-47fb9cwz/recovery/report.json", + "nativePreContextCompactionCounterexample": "/tmp/flow-v2-final-continuation-qa-zc8ncill/native-stale-callback-verification.json", + "nativeTypesafeOption": "Three actual-native option cases pass on d3d46d92 with zero advice attempts; general candidate remains blocked.", + "nativeAuthParentR3Focused": { + "log": "/tmp/flow-v2-native-auth-parent-r3-focused.log", + "pass": 50, + "fail": 0, + "scope": "Auth helpers, operator command/configuration, and credential-retention cleanup; no native or provider calls" + }, + "r6CanonicalChecks": { + "status": "PASS, both macOS and Linux", + "macOS": "/tmp/flow-v2-r6-check-macos.log", + "linux": "/tmp/flow-v2-linux/r6-full-check.log", + "macOSSession": 54008, + "linuxSession": 48003, + "productionFrozen": true, + "documentationAfterSnapshot": "ADR0018 now distinguishes admission from provider attempts and pre-primary compaction; 15 documentation tests pass in /tmp/flow-v2-r6-documentation.log", + "tests": { + "pass": 4643, + "skip": 27, + "fail": 0 + }, + "linuxDocumentationAfterSnapshot": "/tmp/flow-v2-linux/r6-documentation.log" + }, + "r7CanonicalChecks": { + "status": "PASS, macOS and Linux", + "macOS": "/tmp/flow-v2-r7-check-macos.log", + "linux": "/tmp/flow-v2-linux/r7-full-check.log", + "productionFrozen": true, + "macOSExitCode": 0, + "macOSTests": { + "pass": 4679, + "skip": 27, + "fail": 0, + "assertions": 98623, + "files": 183 + }, + "linuxExitCode": 0, + "linuxTests": { + "pass": 4679, + "skip": 27, + "fail": 0, + "assertions": 98620, + "files": 183 + }, + "scope": "Frozen R7 source before the next fix. Both canonical suites pass while opt-in packed native cancellation/compaction fails. Parent-only plan and review records updated separately.", + "sourceFreeze": "/tmp/flow-v2-manual-compaction-check-su6aydtb/freeze-final.json", + "linuxSnapshotManifest": "/tmp/flow-v2-linux/r7-source-manifest.json", + "ownedSourceFreezeVerified": true, + "logSha256": { + "macOS": "5cd30e799ffc841b6986227045202b157e12fede47763c02def8e793f07eb437", + "linux": "041579f2ed52708728581d39ba46a924fd332db55c002104bc30790675fd2760" + } + }, + "r7NativeAuthRefresh": { + "status": "PASS", + "scope": "Synthetic isolated source and child services only. No real account, original user service, or paid provider.", + "native": "/tmp/flow-v2-native-auth-r7-native.log", + "canary": "/tmp/flow-v2-native-auth-r7-canary.log", + "reason": "Verify current reviewed disposal boundaries and current sealed client through maintained native fixture paths.", + "nativeTests": { + "pass": 1, + "fail": 0, + "assertions": 10 + }, + "canaryTests": { + "pass": 14, + "fail": 0, + "assertions": 111 + }, + "exitCodes": [ + 0, + 0 + ], + "logSha256": { + "native": "313f7482fc4f5766cc1c1670728aa312874121d2f05689f5db4f82726bd42dbd", + "canary": "dc401cd644d35ea3b9550825309c785fc5d57c06e7889b615752071439878b24" + }, + "reviewedAuthSourceFreezeVerified": true, + "staticClientReceipt": "/tmp/flow-v2-auth-addon-r7-WHGmWw/verification.json", + "staticClientAggregate": "sha256:e8e4bed74dd1266ac3e1c4cd149918b8ffa35391afac0b098f95eb2930333c1b" + }, + "r7Packed": { + "status": "ISSUES", + "macOS": "/tmp/flow-v2-product-r7-qa-47fb9cwz/verification.json", + "linuxLog": "/tmp/flow-v2-linux/r7-packed-smoke.log", + "failure": "Native manual summary after interrupted ordinary request returns failed, expected completed", + "picker": "PASS, both routes/eight native phases", + "normalRecovery": "PASS,3tests/four native hosts/zero claims", + "rawLinuxReproduction": "PASS, archive74da6ab8 identical to macOS", + "continuationLimits": "Nine prior compaction cases pass. Two transport families incomplete due initial context veto; forced pending-activation component finding separate from unproved native timing cause." + }, + "r8CanonicalChecks": { + "status": "PASS macOS and Linux", + "macOS": "/tmp/flow-v2-candidate-r8/macos-check.json", + "linux": "/tmp/flow-v2-candidate-r8/linux-check.json", + "tests": { + "pass": 4700, + "skip": 27, + "fail": 0, + "files": 184 + }, + "assertions": { + "macOS": 98734, + "linux": 98731 + }, + "commentOnlySupplement": "/tmp/flow-v2-candidate-r8/review-freeze.json" + }, + "r8Packed": { + "status": "PASS within synthetic loopback QA", + "macOS": "/tmp/flow-v2-product-r8-qa-_bp2a_dw/verification.json", + "linux": "/tmp/flow-v2-linux/r8-packed-smoke.log", + "assertionsEach": 119, + "continuation": "/tmp/flow-v2-packed-r8-b-yc4m9puu/verification.json", + "picker": "/tmp/flow-v2-picker-implementation/packed-r8-fc19ca4a/verification.json", + "recovery": "/tmp/flow-v2-product-r8-qa-_bp2a_dw/recovery/report.json", + "rawLinuxReproduction": true, + "serverMapRows": 75, + "tuiMapRows": 5, + "uniqueProjectMapPaths": 77 + }, + "r8Visual": { + "status": "PASS for recorded terminal-cell replay only", + "receipt": "/tmp/flow-v2-terminal-render/r8-fc19ca4a/responsive/visual-inspection.json", + "ansiSha256": "1da34ba1eaa1c3af9a4f1ab5919a04ea9d40db6698e4ce5351d8b72f2bca5202", + "limit": "No live native pixel/font verification" + }, + "r8Comments": { + "report": "/tmp/flow-v2-comment-review-r8-independent/report.md", + "actualDeletions": 1, + "keeps": 3, + "suppressions": 0, + "mustKill": 0, + "response": "/tmp/flow-v2-comment-review-r8-independent/parent-response.json" + }, + "r8Offline": { + "frozenInstallMacOS": "/tmp/flow-v2-r8-frozen-install-macos.log", + "frozenInstallLinux": "/tmp/flow-v2-linux/r8-frozen-install.log", + "replay": { + "log": "/tmp/flow-v2-r8-replay.log", + "gatedMatches": 13, + "gatedTotal": 13 + }, + "audit": { + "log": "/tmp/flow-v2-r8-audit.log", + "status": "No vulnerabilities found among276packages" + }, + "actionlint": "/tmp/flow-v2-r8-actionlint.log", + "documentation": "/tmp/flow-v2-r8-documentation-final.log", + "wikiLocal": { + "receipt": "/tmp/flow-v2-wiki-r8-local/linkverification.json", + "pages": 45, + "checks": 293, + "status": "PASS; no public readback" + } + }, + "r8IndependentReview": { + "verdict": "bounded PASS", + "report": "/tmp/flow-v2-core-review-r8/report.md", + "reportSha256": "444b09db9873198b26d50e5d5ca3c1f391bb7de030594648a71fcf788f331dca", + "pass": 496, + "filtered": 1, + "fail": 0, + "assertions": 2507, + "files": 17, + "freshNativeRuns": 0, + "sourceDrift": false + } + }, + "pending": { + "coreR3Report": "/tmp/flow-v2-core-review-r3/report.md", + "evalR2Report": "/tmp/flow-v2-eval-review-r2/report.md", + "releaseR2Report": "/tmp/flow-v2-release-review-r2/report.md", + "nativeAccountHandoff": "/tmp/flow-v2-native-auth-probe", + "coreR4Report": "/tmp/flow-v2-core-review-r4/report.md", + "authentication": "Synthetic source-owned OAuth resolver prototype. Existing authenticated native service verified. Export bank remains unadopted.", + "evalR3Report": "/tmp/flow-v2-eval-review-r3/report.md", + "coreR5Report": "/tmp/flow-v2-core-review-r5/report.md", + "coreVerdict": "Bounded R8 PASS after unchanged packed smoke and full checks. No source defect remains in reviewed scope; paid qualification and publication are not approved.", + "typesafeEnvOption": "Implemented; independent bounded PASS at /tmp/flow-v2-typesafe-env-review-r1/report.md; final packed proof pending", + "nativeAuthenticationImplementation": "/tmp/flow-v2-native-auth-probe/review-r2-fixes/handoff.md", + "canaryNativeAuth": "Implemented; root native/sealing fixture14pass. Reviewed helper R3 PASS remains bounded; actual built-in accounts and final qualification pending.", + "nativeAuthRuntime": "/tmp/flow-v2-eval-auth-integration/verification.json, bootstrap and shutdown PASS; zero model generations", + "nativeAuthR1": "Closed by retained R1 fixes and independent R2/R3 reviews. Original report remains unchanged.", + "nativeAuthR2": "Closed by generic disposal error boundaries; independent R3 PASS. Original report remains unchanged.", + "nativeAuthR3": "Bounded PASS at /tmp/flow-v2-native-auth-review-r3/report.md. Current client e8e4bed7; real-account qualification remains pending.", + "coreR6": { + "handoff": "/tmp/flow-v2-preprimary-compaction-fix-9yngneay/handoff.md", + "freeze": "/tmp/flow-v2-preprimary-compaction-fix-9yngneay/freeze-for-r6.json", + "review": "ISSUES. Ordinary grantless compaction failed natively; same-epoch pending ownership failed in a component case. Current source corrects both; native overlap reachability remains unproved.", + "ownerProof": "300 focused tests,6 architecture tests,5 native cases; private source entry only, no new archive", + "report": "/tmp/flow-v2-core-review-r6/report.md", + "sameEpochObjection": "Proved component failure in same-epoch pending slot; native concurrent reachability unproved and must remain explicit." + }, + "nativeAuthVerdict": "Bounded PASS, fresh Grok R3, /tmp/flow-v2-native-auth-review-r3/report.md; real-provider and paid qualification remain pending", + "coreR7Preparation": { + "handoff": "/tmp/flow-v2-manual-compaction-check-su6aydtb/handoff.md", + "freeze": "/tmp/flow-v2-manual-compaction-check-su6aydtb/freeze-final.json", + "ownerProof": "336 focused tests,6 architecture tests,9 native cases reported PASS; private development source entry only", + "additionalEmpiricalCheck": "Actual public session.compact failed in two ordinary-history states on the preceding source. Both are corrected and proved before/after; unattributed summaries grant no execution origin.", + "manualProof": "/tmp/flow-v2-manual-compaction-check-su6aydtb/handoff.md", + "review": "Bounded PASS at /tmp/flow-v2-core-review-r7/report.md. Independent native manual cases and 475 focused tests passed. Same-model auxiliary retry and native context overlap remain unproved." + }, + "coreR7Report": "/tmp/flow-v2-core-review-r7/report.md", + "coreR8Design": { + "directory": "/tmp/flow-v2-compaction-design-r8", + "census": "/tmp/flow-v2-packed-r7-continuation-qa-uljdjwv7/runs/run-20261024-143740-052985/census-handoff.md", + "parentNativeFacts": "/tmp/flow-v2-compaction-design-r8/root-native-facts.json", + "parentCensusProvenance": "/tmp/flow-v2-compaction-design-r8/census-provenance-correction.json", + "pendingActivation": "/tmp/flow-v2-compaction-design-r8/root-pending/pending-activation.json", + "status": "Selected ledger implemented and frozen; exact-package gates passed before fresh R8 source review.", + "decision": ".agents/plans/18-opencode-v2/compaction-boundary.md", + "crossjudge": ".agents/plans/18-opencode-v2/reviews/compaction-design-r8.md", + "parentScores": { + "A": 15, + "B": 9 + }, + "crossjudgeScores": { + "A": 14, + "B": 10 + }, + "implementationThread": "flow-v2-continuation-implementation-20261024", + "grafts": [ + "Synchronous pre-await reservation on one input frontier", + "Cancellable scope wait independent of admission.ready", + "Stable checked record/checkpoint origin reuse" + ], + "newSubstantiveLedgerModuleAllowed": "Retain src/platform/opencode/input-admission.ts for sole input knowledge ownership. Ceiling necessity is explicitly not claimed; earlier ceiling-dependent condition superseded by final ownership reconciliation.", + "additionalTestCallers": [ + "tests/fixtures/verified-continuation-origin.ts", + "tests/recovery-authority.test.ts", + "tests/recovery-host.test.ts" + ], + "callerMigrationRule": "Move removed admission/compaction callers to real ledger and boundary; keep policy/retry/budget/prompt-count assertions; only two superseded-command strings may use new ledger epoch error. No compatibility forwards.", + "unmatchedCleanupReconciliation": "/tmp/flow-v2-compaction-design-r8/synthesis/unmatched-callback-reconciliation.md", + "preDispatchCancellation": { + "status": "Corrected; focused component acceptance and final source proof passed. Exact captured object cleanup retained.", + "beforeLog": "/tmp/flow-v2-compaction-r8-bnmny7om/pre-dispatch-cancel-before.log", + "ownershipRule": "End only the stale callback captured frozen primary. Revoke logical authority only for its exact still-current source. Never end the host-current replacement.", + "regression": "Held post-freeze session.get; same-epoch private acceptance cancels old scope, then fresh context and protected tool succeed with non-null unchanged authority.", + "nativeReachability": "Native overlapping-context reachability remains unproved." + }, + "historicalFrontierLineage": { + "status": "Corrected in sole ledger; both component acceptance cases and final source proof passed.", + "beforeLog": "/tmp/flow-v2-compaction-r8-bnmny7om/lineage-before-correct-reason.log", + "rule": "Unregistered trailing history may retain only historical classification of the scope captured local frontier. Summary aliases retain that existing record; no positive origin, current lease, latest-user join, or new authority store.", + "automaticTombstone": "Known automatic classification cannot be erased by an unregistered trailing input or repeated summary alias.", + "nativeReachability": "Native reachability of the trailing-history sequence remains unproved." + }, + "postReadyReadFailure": { + "status": "Corrected; exact post-ready source refresh and matched native-read failure regression passed.", + "beforeLog": "/tmp/flow-v2-compaction-r8-bnmny7om/ready-read-failure-before.log", + "rule": "Refresh the exact scope positive origin after ready settles and before native read. Catch may revoke only this source while the scope remains current. No pre-ready grant or host-global fallback.", + "regression": "No lease before readiness; matched native-read failure clears its lease; fresh exact input runs protected tool with non-null lease." + }, + "modulePlacementReconciliation": { + "decision": "Keep separate substantive InputAdmissions and ExecutionAttribution modules; no co-location rewrite.", + "reportedActualLines": { + "lifecycle": 795, + "inputAdmissions": 509, + "executionAttribution": 454, + "compactionBoundary": 266 + }, + "ceilingFinding": "Ledger plus attribution total 963 lines before import deduplication; co-location would fit. These counts are owner-reported, not a final frozen receipt.", + "rationale": "Ledger owns reservation/readiness/lifetime/frontier/history. Attribution owns primary/Request/assistant/tool binding. Original admission methods and duplicate lifecycle maps are deleted; callers use the ledger directly.", + "limits": "No forwarding API, second registry, additional abstraction, or source-scope expansion. Final source proof and freeze remain pending." + }, + "sourceFreeze": "/tmp/flow-v2-compaction-r8-bnmny7om/freeze-final.json", + "parentReviewFreeze": "/tmp/flow-v2-candidate-r8/review-freeze.json" + }, + "coreR8Review": "/tmp/flow-v2-core-review-r8/report.md, bounded PASS" + }, + "next": [ + "Finish staged-index review and commit preflight, then commit the reviewed migration.", + "Complete repository contribution gates and commit the reviewed migration.", + "Obtain explicit paid pre-push smoke authorization before account use and push; then Linux CI and separate full qualification approval.", + "Publish through the usual exact-artifact release gate, then wiki publication and public readback." + ], + "authority": { + "mainRepositoryCommits": 0, + "pushes": 0, + "pullRequests": 0, + "paidProviderCalls": 0, + "tags": 0, + "published": false, + "localOpenAiAndXaiApiKeysPresent": false, + "ciModelApiSecretsPresent": false, + "prePushPaidGate": ".agents/skills/flow-contribution-check/references/validation-matrix.md", + "authorizationRule": ".agents/plans/05-release-simplification/README.md#authorize-paid-work", + "matrixDispatchDraft": ".agents/plans/18-opencode-v2/qualification-dispatch-draft.json", + "publicationRequiresImmediateApproval": true, + "authenticationSource": "Existing OpenCode 2 accounts, selected by user; no paid authorization.", + "nativeAccountMetadata": { + "openai": "oauth", + "xai": "oauth", + "secretValuesRead": false + }, + "nativeServiceMetadata": { + "version": "2.1.4", + "pid": 76572, + "local": true, + "authenticatedNativeCliInfo": "PASS", + "originalRefreshValuesRead": false + } + }, + "notes": [ + "Do not reuse a completed review thread for another review round. Use delegate_task with a fresh clientRequestId.", + "The R1 marker provenance claim is superseded in reviews/reconciliation.md; preserve original reports.", + "The HTTP helper is proven on native 2.1.4. Old experimental environment flags are retired from active execution.", + "Wiki draft remains local. Visual appearance was not signed off; native PTY behavior passed." + ], + "linuxRunner": { + "profile": "flow-v2-qualification", + "dockerContext": "colima-flow-v2-qualification", + "platform": "linux/aarch64", + "image": "flow-v2-qualification:1.4.0", + "baseImageDigest": "sha256:5ff609364c049b54eb0ff560ec96319729a972078ef2c755d758f0c6ef89c2d6", + "sourceMount": "read-only", + "authExported": false, + "frozenInstall": "PASS, 272 packages; clean platform exposed and removed undeclared test yaml import", + "firstFullCheck": { + "log": "/tmp/flow-v2-linux/first-full-check.log", + "pass": 4494, + "skip": 25, + "fail": 0, + "scope": "Isolated source snapshot before stale callback ownership fix and native OAuth integration" + }, + "activeContainer": "flow-v2-qualification-init", + "finalFullCheck": { + "log": "/tmp/flow-v2-linux/final-full-check-recheck.log", + "pass": 4603, + "skip": 27, + "fail": 0 + }, + "packedSmoke": { + "log": "/tmp/flow-v2-linux/final-packed-smoke-init.log", + "pass": 2, + "fail": 0, + "assertions": 119, + "evidenceDirectory": "/tmp/flow-v2-linux/final-packed-smoke-init" + }, + "runnerCorrection": "Use Docker --init to reap exited native descendants. First no-init run failed cleanup on one confirmed zombie; no live product process remained.", + "rawArchiveReproducibility": { + "log": "/tmp/flow-v2-linux/packed-reproducibility.log", + "archive": "/tmp/flow-v2-linux/linux-packed-repro/opencode-plugin-flow-10.0.0.tgz", + "matchesMacOSCandidate": true, + "sha256": "d3d46d92148a34d829536c939a528f52c3ef6adeb612230afab860b003c09292" + } + }, + "rejectedCandidates": [ + { + "path": "/tmp/flow-v2-candidate-final/opencode-plugin-flow-10.0.0.tgz", + "sha256": "d3d46d92148a34d829536c939a528f52c3ef6adeb612230afab860b003c09292", + "unpackedManifestSha256": "29a1d475fae346ee7fd52a62ac079ca2af5b0f0c36ee05bb946d2f9fc8aba0d8", + "status": "REJECTED. Retained exact archive has the native pre-primary compaction defect. Current source has additional ordinary automatic/manual compaction corrections; fresh R7 and full checks are running. No replacement archive exists." + } + ] +} diff --git a/.agents/plans/18-opencode-v2/probe-extension-verification.json b/.agents/plans/18-opencode-v2/probe-extension-verification.json new file mode 100644 index 00000000..c053c0bf --- /dev/null +++ b/.agents/plans/18-opencode-v2/probe-extension-verification.json @@ -0,0 +1,98 @@ +{ + "status": "PASS", + "run": "1792827133815", + "host": "OpenCode 2.1.4 Darwin arm64", + "checks": [ + "four extension cases passed", + "compact-uninterrupted exact admission receipt", + "compact-uninterrupted genuine automatic compaction", + "compact-uninterrupted synchronous running control identity", + "compact-uninterrupted compaction consumes original dispatch", + "compact-uninterrupted primary context waits for stream end", + "compact-uninterrupted primary response streams do not overlap", + "compact-uninterrupted old assistant tools precede next primary context", + "compact-uninterrupted old assistant tools precede next primary context", + "compact-uninterrupted primary context waits for stream end", + "compact-uninterrupted primary response streams do not overlap", + "compact-uninterrupted old assistant tools precede next primary context", + "compact-uninterrupted old assistant tools precede next primary context", + "compact-uninterrupted primary context waits for stream end", + "compact-uninterrupted primary response streams do not overlap", + "same epoch crosses exact compaction control", + "post-compaction tool retains epoch one", + "post-compaction exact assistant and call", + "post-compaction prefix matches frozen consumed control", + "compact-steer exact admission receipt", + "compact-steer genuine automatic compaction", + "compact-steer synchronous running control identity", + "compact-steer compaction consumes original dispatch", + "compact-steer primary context waits for stream end", + "compact-steer primary response streams do not overlap", + "compact-steer old assistant tools precede next primary context", + "compact-steer old assistant tools precede next primary context", + "compact-steer primary context waits for stream end", + "compact-steer primary context waits for stream end", + "real steer occurs inside compaction interval", + "steer vetoes resumed automatic context", + "local provider receives no resumed steered request", + "no provider dispatch after compaction steer", + "no authority after compaction steer", + "stale-context exact admission receipt", + "stale synthetic reaches context and is rejected", + "local provider receives zero stale-context requests", + "stale context prevents every HTTP request", + "stale context prevents every tool", + "stale-tool exact admission receipt", + "before-tool veto targets exact native shell", + "rejected native shell effect file absent", + "rejected native shell never reaches shell creation", + "native tool reports rejection rather than success" + ], + "cases": [ + { + "mode": "compact-uninterrupted", + "status": "PASS", + "sessionID": "ses_e932cf06dffeulg1JQls6ASQwR", + "inputID": "msg_ext_compact_uninterrupted_1792827133815", + "compaction": { + "id": "msg_16cd31089001LXf7Gm6k878g0K", + "reason": "auto", + "status": "completed" + } + }, + { + "mode": "compact-steer", + "status": "PASS", + "sessionID": "ses_e932cef45ffegRTncXi8vNZlxo", + "inputID": "msg_ext_compact_steer_1792827133815", + "compaction": { + "id": "msg_16cd310ce002rI6ZX0mx67DxVi", + "reason": "auto", + "status": "completed" + } + }, + { + "mode": "stale-context", + "status": "PASS", + "sessionID": "ses_e932ce9a2ffeJ29k7TUa2Kweev", + "inputID": "msg_ext_stale_context_1792827133815", + "compaction": null + }, + { + "mode": "stale-tool", + "status": "PASS", + "sessionID": "ses_e932ce990ffeiC6dgkHu9qicdr", + "inputID": "msg_ext_stale_tool_1792827133815", + "compaction": null + } + ], + "scope": "Bounded local scripted HTTP provider. No product integration certification.", + "supportedConditions": [ + "Exact admitted synthetic ID and private live epoch exist before positive authority.", + "Freeze primary context and Request identity; reject overlap or immutable assistant binding conflicts.", + "Read and freeze running native auto-compaction ID in the synchronous compaction hook.", + "At resumed context, require that exact completed auto-compaction ID and unchanged live epoch.", + "Reject unknown inputs and real steer; do not recover authority from summary text.", + "Before every native tool, verify exact assistant/call identity, frozen input prefix, and unrevoked epoch." + ] +} \ No newline at end of file diff --git a/.agents/plans/18-opencode-v2/probe-health.json b/.agents/plans/18-opencode-v2/probe-health.json new file mode 100644 index 00000000..ad32593a --- /dev/null +++ b/.agents/plans/18-opencode-v2/probe-health.json @@ -0,0 +1,13 @@ +{ + "version": "2.1.4", + "pid": 46076, + "urls": [ + "http://127.0.0.1:47841" + ], + "paths": { + "tmp": "/private/tmp/flow-v2-server-probe/tmp/opencode" + }, + "capabilities": { + "persistentPty": true + } +} \ No newline at end of file diff --git a/.agents/plans/18-opencode-v2/probe-verification.json b/.agents/plans/18-opencode-v2/probe-verification.json new file mode 100644 index 00000000..687ec15e --- /dev/null +++ b/.agents/plans/18-opencode-v2/probe-verification.json @@ -0,0 +1,106 @@ +{ + "status": "PASS", + "meaning": "Disposable assertions passed. This is not an integrated Flow certification.", + "checks": [ + "exact host health", + "plugin options and lifecycle", + "missing update invokes callback", + "reserved agents created", + "native child parentage", + "allowed custom effects", + "denied custom effects absent", + "explicit denial has native error", + "shell nonzero remains completed", + "shell execution error", + "shell timeout lacks exit", + "exact full output file", + "exact full output API", + "duplicate admission converges", + "duplicate hooks run twice", + "duplicate creates one enqueue", + "rejected hook prevents admission", + "real interruption", + "compaction retained post input", + "static agent public lookup", + "http-pending primary streams do not overlap", + "http-pending response joins same Request", + "http-pending exact assistant and call probe_plain", + "http-pending frozen inputs equal assistant prefix probe_plain", + "http-pending exact assistant and call probe_explicit", + "http-pending frozen inputs equal assistant prefix probe_explicit", + "http-pending old execution not relabeled", + "http-pending context waits for preceding primary stream", + "http-pending old assistant tool stays before next primary context", + "http-pending old assistant tool stays before next primary context", + "http-pending context waits for preceding primary stream", + "http-pending old assistant tool stays before next primary context", + "http-pending old assistant tool stays before next primary context", + "http-pending context waits for preceding primary stream", + "tool-running primary streams do not overlap", + "tool-running response joins same Request", + "tool-running exact assistant and call probe_hold", + "tool-running frozen inputs equal assistant prefix probe_hold", + "tool-running exact assistant and call probe_explicit", + "tool-running frozen inputs equal assistant prefix probe_explicit", + "tool-running old execution not relabeled", + "tool-running context waits for preceding primary stream", + "tool-running old assistant tool stays before next primary context", + "tool-running old assistant tool stays before next primary context", + "tool-running context waits for preceding primary stream", + "tool-running old assistant tool stays before next primary context", + "tool-running old assistant tool stays before next primary context", + "tool-running context waits for preceding primary stream", + "client subscriber terminates on host shutdown" + ], + "causalJoins": [ + { + "phase": "http-pending", + "assistant": "msg_16ccafaf8001mTpLLYBhlWkORb", + "call": "call_probe_0", + "tool": "probe_plain", + "requestSeq": 17, + "consumedInputIDs": [ + "msg_16ccafac1001P7iO2nf1EMMDxu" + ] + }, + { + "phase": "http-pending", + "assistant": "msg_16ccb02df001MEtxYkbQjjHNnm", + "call": "call_probe_0", + "tool": "probe_explicit", + "requestSeq": 18, + "consumedInputIDs": [ + "msg_16ccafac1001P7iO2nf1EMMDxu", + "msg_16ccafb6e001C7MiiXTxQfqQ38" + ] + }, + { + "phase": "tool-running", + "assistant": "msg_16ccb039f001B6k1sK92P2H7RR", + "call": "call_probe_0", + "tool": "probe_hold", + "requestSeq": 20, + "consumedInputIDs": [ + "msg_16ccb02fa001Ex3xaUyrLbcr53" + ] + }, + { + "phase": "tool-running", + "assistant": "msg_16ccb0795001w7pJJ0XQYxSMJx", + "call": "call_probe_0", + "tool": "probe_explicit", + "requestSeq": 21, + "consumedInputIDs": [ + "msg_16ccb02fa001Ex3xaUyrLbcr53", + "msg_16ccb03b4001E3tRSB1cxoO79i" + ] + } + ], + "limitations": [ + "Only scripted local OpenAI-compatible provider and Darwin arm64 tested.", + "No general primary-request serialization guarantee claimed. Fail closed on overlap or unknown joins.", + "No complete async event prefix or durable replay established.", + "Automatic compaction during a live automatic turn remains unverified.", + "Revocation of an already running operation requires parent integration design and checks." + ] +} \ No newline at end of file diff --git a/.agents/plans/18-opencode-v2/probe-version.json b/.agents/plans/18-opencode-v2/probe-version.json new file mode 100644 index 00000000..a1976454 --- /dev/null +++ b/.agents/plans/18-opencode-v2/probe-version.json @@ -0,0 +1 @@ +{"stdout": "opencode v2.1.4", "executable": "/tmp/flow-opencode-v2-plan-host/npm-cache/_npx/2314e955696d2fea/node_modules/@opencode/cli-darwin-arm64/bin/opencode"} \ No newline at end of file diff --git a/.agents/plans/18-opencode-v2/probes/server/automatic-extension.mjs b/.agents/plans/18-opencode-v2/probes/server/automatic-extension.mjs new file mode 100644 index 00000000..f8c869ab --- /dev/null +++ b/.agents/plans/18-opencode-v2/probes/server/automatic-extension.mjs @@ -0,0 +1,52 @@ +import {OpenCode} from '@opencode/client'; +import {readFileSync,writeFileSync,appendFileSync,existsSync} from 'node:fs'; +const root='/tmp/flow-v2-server-probe';const password=readFileSync(root+'/server.stdout','utf8').match(/server password (.+)/)[1];const c=OpenCode.make({baseUrl:'http://127.0.0.1:47841',headers:{authorization:'Basic '+Buffer.from('opencode:'+password).toString('base64'),'x-opencode-directory':root+'/workspace'}}); +const log=(type,data)=>{appendFileSync(root+'/automatic-extension.jsonl',JSON.stringify({type,data})+'\n');console.log(type,JSON.stringify(data).slice(0,800))};const opts=()=>({signal:AbortSignal.timeout(12000)});const run=Date.now().toString();log('run',run); +const rows=()=>existsSync(root+'/extension-plugin.jsonl')?readFileSync(root+'/extension-plugin.jsonl','utf8').trim().split('\n').filter(Boolean).map(JSON.parse):[]; +const waitLog=async(test)=>{const deadline=Date.now()+7000;while(Date.now()setTimeout(r,20))}throw Error('Required extension hook not observed')}; +const results=[]; +for(const mode of ['compact-uninterrupted','compact-steer','stale-context','stale-tool']){ + let session; + try{ + session=await c.session.create({title:'extension '+mode,agent:'build',model:{providerID:'flowprobe',id:'probe'},location:{directory:root+'/workspace'}});log(mode+'-session',session); + const inputID='msg_ext_'+mode.replaceAll('-','_')+'_'+run; + const effect=root+'/extension-native-effect-'+run; + const text=mode.startsWith('compact')?'AUTO_COMPACT_'+mode.replaceAll('-','_').toUpperCase()+'_'+run+' PROBE [{"tool":"probe_plain"}]':mode==='stale-context'?'PROBE [{"tool":"probe_plain"}]':'DELAY PROBE '+JSON.stringify([{tool:'shell',input:{command:'printf forbidden > '+effect}}]); + await c.session.command({sessionID:session.id,name:'probe-auto',text:JSON.stringify({mode,id:inputID,text})},opts());log(mode+'-command-returned',{inputID}); + if(mode==='compact-steer'){ + await waitLog(x=>x.type==='ext-compaction-enter'&&x.data.sessionID===session.id); + log(mode+'-steer',await c.session.prompt({sessionID:session.id,text:'Real new direction fences prior automatic authority',metadata:{extensionSteer:true},delivery:'steer'},opts())); + } + if(mode==='stale-tool'){ + await waitLog(x=>x.type==='ext-primary-dispatch'&&x.data.sessionID===session.id); + log(mode+'-steer',await c.session.prompt({sessionID:session.id,text:'Stop automatic native effect',metadata:{extensionSteer:true},delivery:'steer'},opts())); + } + await c.session.wait({sessionID:session.id},opts()); + log(mode+'-messages',await c.message.list({sessionID:session.id},opts())); + const native=await c.session.context({sessionID:session.id},opts());log(mode+'-context',native); + const hooks=rows().filter(x=>x.data.sessionID===session.id);log(mode+'-hooks',hooks); + const compact=native.find(m=>m.type==='compaction'); + if(mode==='compact-uninterrupted'){ + if(!compact||compact.reason==='manual'||compact.status!=='completed')throw Error('Automatic compaction did not complete'); + if(!hooks.some(x=>x.type==='ext-continuity'))throw Error('No post-compaction continuity snapshot'); + if(!hooks.some(x=>x.type==='ext-tool-authorized'&&x.data.execution.id==='call_post_compaction'&&x.data.epoch===1))throw Error('No post-compaction authorized tool under original epoch'); + } + if(mode==='compact-steer'){ + if(!compact||compact.reason==='manual'||compact.status!=='completed')throw Error('Steered automatic compaction did not complete'); + const revoked=hooks.findIndex(x=>x.type==='ext-revoked'); + if(revoked<0||hooks.slice(revoked+1).some(x=>x.type==='ext-primary-dispatch'||x.type==='ext-tool-authorized'))throw Error('Automatic authority survived real steer'); + if(!hooks.some(x=>x.type==='ext-context-rejected'))throw Error('No stale resumed context rejection'); + } + if(mode==='stale-context'){ + if(!hooks.some(x=>x.type==='ext-context-rejected')||hooks.some(x=>x.type==='ext-primary-dispatch'||x.type==='ext-tool-attempt'))throw Error('Stale synthetic reached provider or tool'); + } + if(mode==='stale-tool'){ + if(!hooks.some(x=>x.type==='ext-tool-rejected'&&x.data.execution.tool==='shell'))throw Error('Native shell was not rejected at before-tool boundary'); + if(existsSync(effect))throw Error('Rejected native shell still wrote its effect file'); + if(hooks.some(x=>x.type==='ext-native-shell-created'))throw Error('Rejected native shell reached shell-create boundary'); + log('native-effect-absent',{path:effect,absent:!existsSync(effect)}); + } + results.push({mode,status:'PASS',sessionID:session.id,inputID,compaction:compact?{id:compact.id,reason:compact.reason,status:compact.status}:null}); + }catch(error){const failure={mode,status:'ISSUES',sessionID:session?.id,error:String(error)};results.push(failure);log('case-failed',failure);if(session)try{await c.session.interrupt({sessionID:session.id,resume:false},opts())}catch{}} +} +const result={status:results.every(x=>x.status==='PASS')?'PASS':'ISSUES',run,cases:results};writeFileSync(root+'/automatic-extension-result.json',JSON.stringify(result,null,2));console.log('RESULT',JSON.stringify(result)); diff --git a/.agents/plans/18-opencode-v2/probes/server/bootstrap.py b/.agents/plans/18-opencode-v2/probes/server/bootstrap.py new file mode 100644 index 00000000..1cafca8e --- /dev/null +++ b/.agents/plans/18-opencode-v2/probes/server/bootstrap.py @@ -0,0 +1,13 @@ +import pathlib, shutil, subprocess, sys +source = pathlib.Path(__file__).resolve().parent +root = pathlib.Path('/tmp/flow-v2-server-probe') +root.mkdir(exist_ok=True) +for file in source.iterdir(): + if file.suffix in {'.mjs', '.ts', '.py'} and file.name != 'bootstrap.py': + shutil.copyfile(file, root / file.name) +shutil.copyfile(source / 'package.json', root / 'package.json') +for name in ['workspace', 'config/opencode']: + (root / name).mkdir(parents=True, exist_ok=True) +shutil.copyfile(source / 'workspace.json', root / 'workspace/opencode.json') +shutil.copyfile(source / 'config.json', root / 'config/opencode/opencode.json') +subprocess.run([sys.executable, str(root / 'run-probe.py'), *sys.argv[1:]], check=True) diff --git a/.agents/plans/18-opencode-v2/probes/server/config.json b/.agents/plans/18-opencode-v2/probes/server/config.json new file mode 100644 index 00000000..6dd10c1c --- /dev/null +++ b/.agents/plans/18-opencode-v2/probes/server/config.json @@ -0,0 +1,29 @@ +{ + "plugins": [ + { + "package": "/tmp/flow-v2-server-probe", + "options": { + "receipt": "isolated-options" + } + } + ], + "agents": { + "flow-static-probe": { + "mode": "subagent", + "description": "Static configuration probe", + "system": "Read only static probe", + "permissions": [ + { + "action": "*", + "resource": "*", + "effect": "deny" + }, + { + "action": "read", + "resource": "*", + "effect": "allow" + } + ] + } + } +} \ No newline at end of file diff --git a/.agents/plans/18-opencode-v2/probes/server/extended.mjs b/.agents/plans/18-opencode-v2/probes/server/extended.mjs new file mode 100644 index 00000000..9ca0e8f1 --- /dev/null +++ b/.agents/plans/18-opencode-v2/probes/server/extended.mjs @@ -0,0 +1,33 @@ +import {OpenCode} from '@opencode/client'; +import {readFileSync,appendFileSync,realpathSync,openSync,fstatSync,readFileSync as read,closeSync,constants} from 'node:fs'; +const root='/tmp/flow-v2-server-probe'; +const password=readFileSync(root+'/server.stdout','utf8').match(/server password (.+)/)[1]; +const c=OpenCode.make({baseUrl:'http://127.0.0.1:47841',headers:{authorization:'Basic '+Buffer.from('opencode:'+password).toString('base64'),'x-opencode-directory':root+'/workspace'}}); +const log=(type,data)=>{appendFileSync(root+'/extended.jsonl',JSON.stringify({type,data})+'\n');console.log(type,JSON.stringify(data).slice(0,800))}; +const options=()=>({signal:AbortSignal.timeout(15000)}); +const create=async(agent,title,parentID)=>c.session.create({title,agent,parentID,model:{providerID:'flowprobe',id:'probe'},location:{directory:root+'/workspace'}}); +const actions=[{tool:'probe_plain'},{tool:'probe_declared'},{tool:'probe_explicit'}]; +log('reserved-agents',(await c.agent.list()).data.filter(a=>a.id.startsWith('flow-'))); +for(const role of ['flow-reviewer','flow-worker']){ + const parent=await create('build','permission parent');const s=await create(role,'permission '+role,parent.id);log(role,s); + await c.session.prompt({sessionID:s.id,text:'PROBE '+JSON.stringify(actions)},options());await c.session.wait({sessionID:s.id},options());log(role+'-messages',await c.message.list({sessionID:s.id},options())); +} +const s=await create('build','Lifecycle');log('lifecycle-session',s); +const ac=new AbortController();const events=[];const stream=(async()=>{try{for await(const event of c.event.subscribe({signal:ac.signal})){events.push(event)}}catch(e){log('subscriber-error',String(e))}})(); +const id='msg_flowprobe_duplicate_'+Date.now(); +log('duplicate-admissions',await Promise.allSettled([c.session.prompt({sessionID:s.id,id,text:'PROBE []',metadata:{flowProbe:'duplicate'},delivery:'queue'},options()),c.session.prompt({sessionID:s.id,id,text:'PROBE []',metadata:{flowProbe:'duplicate'},delivery:'queue'},options())])); +await c.session.wait({sessionID:s.id},options()); +log('synthetic',await c.session.synthetic({sessionID:s.id,text:'probe synthetic',metadata:{flowProbe:'synthetic'},delivery:'queue'},options())); +await c.session.wait({sessionID:s.id},options()); +log('compaction-admission',await c.session.compact({sessionID:s.id,delivery:'queue'},options()));await c.session.wait({sessionID:s.id},options()); +log('lifecycle-messages',await c.message.list({sessionID:s.id},options())); +log('session-log',await Array.fromAsync(c.session.log({sessionID:s.id,follow:false},options()))); +ac.abort();await stream;log('client-subscriber-ended',true);log('client-events',events); +const lines=readFileSync(root+'/plugin.jsonl','utf8').trim().split('\n').map(JSON.parse); +const large=lines.findLast(x=>x.type==='execute.after'&&x.data.tool==='shell'&&x.data.result?.metadata?.truncated); +const text=large.data.result.content.filter(p=>p.type==='text').map(p=>p.text).join('\n'); +const path=text.match(/full output saved to ([^\]\n]+)/)?.[1]; +if(!path||!/^\/tmp\/flow-v2-server-probe\/data\/opencode\/shell\/[a-f0-9]+\/sh_[a-zA-Z0-9]+\.out$/.test(path))throw Error('unexpected managed path'); +if(!realpathSync(path).startsWith(realpathSync(root+'/data/opencode/shell')+'/'))throw Error('escaped managed directory'); +const fd=openSync(path,constants.O_RDONLY|constants.O_NOFOLLOW);try{const st=fstatSync(fd);if(!st.isFile()||st.size>8*1024*1024)throw Error('unsafe size');const value=read(fd,'utf8');log('full-output',{path,size:st.size,exact:value==='probe-line\n'.repeat(12000)});}finally{closeSync(fd)} +const shellID=path.match(/(sh_[a-zA-Z0-9]+)\.out$/)[1];log('shell-get',await c.shell.get({id:shellID},options()));const full=await c.shell.output({id:shellID,cursor:0,limit:200000},options());log('shell-output',{...full,data:{...full.data,output:undefined,exact:full.data.output==='probe-line\n'.repeat(12000)}}); diff --git a/.agents/plans/18-opencode-v2/probes/server/extension.ts b/.agents/plans/18-opencode-v2/probes/server/extension.ts new file mode 100644 index 00000000..7526d0b6 --- /dev/null +++ b/.agents/plans/18-opencode-v2/probes/server/extension.ts @@ -0,0 +1,62 @@ +import {appendFileSync} from 'node:fs'; +export async function setupExtension(ctx,log,snapshots,current){ + const states=new Map(); + const bindings=new Map(); + const ids=messages=>messages.filter(m=>m.role==='user'&&m.id).map(m=>m.id); + const nativeInputs=messages=>messages.filter(m=>m.type==='user'||m.type==='synthetic').map(m=>({id:m.id,type:m.type,metadata:m.metadata})); + const record=(type,data)=>{log(type,data);appendFileSync('/tmp/flow-v2-server-probe/extension-plugin.jsonl',JSON.stringify({type,data})+'\n')}; + await ctx.command.transform(editor=>editor.add({name:'probe-auto',description:'Disposable authority probe',execute:async input=>{ + const data=JSON.parse(input.prompt.text); + let admitted;const admission=new Promise(resolve=>admitted=resolve); + const state={mode:data.mode,epoch:1,revoked:data.mode==='stale-context',inputID:data.id,admission,receipt:null,compaction:null};states.set(input.sessionID,state); + record('ext-registered',{sessionID:input.sessionID,mode:state.mode,epoch:1,inputID:data.id}); + try{const receipt=await ctx.session.synthetic({sessionID:input.sessionID,id:data.id,text:data.text,metadata:{extension:true,epoch:1},delivery:'queue',resume:true});state.receipt=receipt;admitted(receipt.id===data.id);record('ext-admitted',{sessionID:input.sessionID,receipt})}catch(error){admitted(false);throw error} + }})); + await ctx.session.hook('prompt',input=>{const state=states.get(input.sessionID);if(state&&input.metadata?.extensionSteer){state.revoked=true;record('ext-revoked',{sessionID:input.sessionID,inputID:input.messageID,epoch:state.epoch,mode:state.mode})}}); + await ctx.session.hook('compaction',async input=>{ + const state=states.get(input.sessionID);if(!state)return; + const snapshot=snapshots.get(input.sessionID); + const native=await ctx.session.context({sessionID:input.sessionID});const control=native.findLast(m=>m.type==='compaction'); + state.compaction={epoch:state.epoch,prior:structuredClone(snapshot),done:false,revokedAtEntry:state.revoked,nativeID:control?.id,reason:control?.reason}; + record('ext-compaction-control',{sessionID:input.sessionID,control,native}); + if(!control||control.reason!=='auto'||control.status!=='running')throw new Error('EXTENSION_UNPROVEN_COMPACTION_CONTROL'); + if(ids(input.messages).some(id=>id!==state.inputID&&id!==state.compaction.nativeID))throw new Error('EXTENSION_UNKNOWN_COMPACTION_INPUT'); + record('ext-compaction-enter',{sessionID:input.sessionID,epoch:state.epoch,revoked:state.revoked,consumed:ids(input.messages),snapshot,messages:input.messages}); + if(state.mode==='compact-steer')await new Promise(resolve=>setTimeout(resolve,1200)); + input.result={summary:'EXTENSION_COMPACTION deterministic summary',tokens:{input:0,output:0,reasoning:0,cache:{read:0,write:0}}}; + state.compaction.done=true; + record('ext-compaction-exit',{sessionID:input.sessionID,epoch:state.epoch,revoked:state.revoked}); + }); + await ctx.session.hook('context',async input=>{ + const state=states.get(input.sessionID);if(!state)return; + const accepted=await state.admission; + const native=await ctx.session.context({sessionID:input.sessionID}); + const consumed=ids(input.messages); + record('ext-context-attempt',{sessionID:input.sessionID,mode:state.mode,epoch:state.epoch,accepted,revoked:state.revoked,consumed,nativeInputs:nativeInputs(native),messages:input.messages,native}); + if(!accepted||state.revoked){record('ext-context-rejected',{sessionID:input.sessionID,mode:state.mode,consumed});throw new Error('EXTENSION_STALE_SYNTHETIC')} + if(state.compaction?.done){ + const control=native.findLast(m=>m.type==='compaction'); + if(!control||control.id!==state.compaction.nativeID||control.reason!=='auto'||control.status!=='completed'||state.compaction.epoch!==state.epoch||state.compaction.revokedAtEntry)throw new Error('EXTENSION_COMPACTION_IDENTITY_CHANGED'); + if(consumed.some(id=>id!==state.compaction.nativeID&&id!==state.inputID))throw new Error('EXTENSION_UNKNOWN_RESUMED_INPUT'); + const after=native.slice(native.findLastIndex(m=>m.type==='compaction')+1); + if(nativeInputs(after).some(m=>m.id!==state.inputID)){record('ext-context-rejected',{sessionID:input.sessionID,mode:state.mode,reason:'unknown post-compaction input'});throw new Error('EXTENSION_UNKNOWN_POST_COMPACTION_INPUT')} + record('ext-continuity',{sessionID:input.sessionID,epoch:state.epoch,priorConsumed:ids(state.compaction.prior?.messages??[]),consumed,nativeInputs:nativeInputs(native),controlID:state.compaction.nativeID,summaryAuthority:false}); + }else if(!consumed.includes(state.inputID)){throw new Error('EXTENSION_MISSING_OWN_INPUT')} + record('ext-context-authorized',{sessionID:input.sessionID,mode:state.mode,epoch:state.epoch,contextSeq:snapshots.get(input.sessionID)?.contextSeq,consumed}); + }); + await ctx.session.hook('http.request',input=>{const state=states.get(input.sessionID);if(state&&input.kind==='primary'){input.request.headers.set('x-flow-probe-session',input.sessionID);record('ext-primary-dispatch',{sessionID:input.sessionID,mode:state.mode,epoch:state.epoch,request:current.get(input.sessionID)})}}); + await ctx.tool.hook('execute.before',async input=>{ + const state=states.get(input.sessionID);if(!state)return; + const primary=current.get(input.sessionID);const key=input.sessionID+'/'+input.messageID;const known=bindings.get(key); + const native=await ctx.session.context({sessionID:input.sessionID});const assistant=native.find(m=>m.id===input.messageID); + if(!assistant||assistant.type!=='assistant'||assistant.agent!==input.agent||!assistant.content.some(p=>p.type==='tool'&&p.id===input.id&&p.name===input.tool))throw new Error('EXTENSION_IDENTITY_MISMATCH'); + if(known&&known!==primary)throw new Error('EXTENSION_ASSISTANT_REBOUND'); + const i=native.findIndex(m=>m.id===input.messageID);const actual=native.slice(0,i).filter(m=>m.type==='user'||m.type==='synthetic'||m.type==='compaction'&&m.status==='completed').map(m=>m.id); + const frozen=ids(primary.snapshot.messages);if(JSON.stringify(actual)!==JSON.stringify(frozen))throw new Error('EXTENSION_INPUT_PREFIX_MISMATCH'); + bindings.set(key,primary); + record('ext-tool-attempt',{sessionID:input.sessionID,mode:state.mode,epoch:state.epoch,revoked:state.revoked,execution:input,primary,native}); + if(state.revoked){record('ext-tool-rejected',{sessionID:input.sessionID,mode:state.mode,execution:input});throw new Error('EXTENSION_REVOKED_BEFORE_NATIVE_TOOL')} + record('ext-tool-authorized',{sessionID:input.sessionID,mode:state.mode,epoch:state.epoch,execution:input,primary}); + }); + await ctx.shell.hook('create.before',input=>{if(input.command.includes('extension-native-effect'))record('ext-native-shell-created',{command:input.command,cwd:input.cwd})}); +} diff --git a/.agents/plans/18-opencode-v2/probes/server/fake-provider.mjs b/.agents/plans/18-opencode-v2/probes/server/fake-provider.mjs new file mode 100644 index 00000000..8f1ecebb --- /dev/null +++ b/.agents/plans/18-opencode-v2/probes/server/fake-provider.mjs @@ -0,0 +1,24 @@ +import http from 'node:http'; +import {appendFileSync} from 'node:fs'; +const log=(x)=>appendFileSync('/tmp/flow-v2-server-probe/provider.jsonl',JSON.stringify(x)+'\n'); +const compacted=new Set(); +const server=http.createServer(async(req,res)=>{ + let raw='';for await(const b of req)raw+=b;const body=JSON.parse(raw);log({url:req.url,sessionID:req.headers['x-flow-probe-session'],body}); + const user=body.messages.filter(x=>x.role==='user').at(-1)?.content; + const text=typeof user==='string'?user:JSON.stringify(user); + let actions=[]; + try{actions=JSON.parse(text.match(/PROBE (\[.*\])/s)?.[1]??'[]')}catch{} + const last=body.messages.at(-1); + const toolCalls=last?.role==='user'?actions.map((x,i)=>({index:i,id:'call_probe_'+i,type:'function',function:{name:x.tool,arguments:JSON.stringify(x.input??{})}})):[]; + const marker=text.match(/AUTO_COMPACT_[A-Z0-9_]+/)?.[0]; + const forceCompaction=!!marker&&!compacted.has(marker)&&toolCalls.length>0; + if(forceCompaction)compacted.add(marker); + const hasSummary=JSON.stringify(body.messages).includes('EXTENSION_COMPACTION'); + if(hasSummary&&last?.role!=='tool'&&!toolCalls.length)toolCalls.push({index:0,id:'call_post_compaction',type:'function',function:{name:'probe_plain',arguments:'{}'}}); + const base={id:'chatcmpl-local-probe',object:'chat.completion.chunk',created:1,model:'probe'}; + res.writeHead(200,{'content-type':'text/event-stream'}); + const chunk=(delta,finish_reason=null)=>res.write('data: '+JSON.stringify({...base,choices:[{index:0,delta,finish_reason}]})+'\n\n'); + chunk({role:'assistant',content:text.includes('DELAY')?'Probe started ':''}); + if(text.includes('DELAY'))await new Promise(resolve=>setTimeout(resolve,2000)); + chunk(toolCalls.length?{tool_calls:toolCalls}:{content:'Probe finished'});chunk({},toolCalls.length?'tool_calls':'stop');if(forceCompaction)res.write('data: '+JSON.stringify({...base,choices:[],usage:{prompt_tokens:195000,completion_tokens:10,total_tokens:195010}})+'\n\n');res.end('data: [DONE]\n\n'); +});server.listen(47842,'127.0.0.1',()=>log({listening:true})); diff --git a/.agents/plans/18-opencode-v2/probes/server/final-edges.mjs b/.agents/plans/18-opencode-v2/probes/server/final-edges.mjs new file mode 100644 index 00000000..ac784605 --- /dev/null +++ b/.agents/plans/18-opencode-v2/probes/server/final-edges.mjs @@ -0,0 +1,10 @@ +import {OpenCode} from '@opencode/client';import {readFileSync,appendFileSync} from 'node:fs'; +const root='/tmp/flow-v2-server-probe';const password=readFileSync(root+'/server.stdout','utf8').match(/server password (.+)/)[1];const c=OpenCode.make({baseUrl:'http://127.0.0.1:47841',headers:{authorization:'Basic '+Buffer.from('opencode:'+password).toString('base64'),'x-opencode-directory':root+'/workspace'}});const log=(type,data)=>{appendFileSync(root+'/edges.jsonl',JSON.stringify({type,data})+'\n');console.log(type,JSON.stringify(data).slice(0,800))};const opts=()=>({signal:AbortSignal.timeout(10000)}); +const s=await c.session.create({title:'Compaction authority',agent:'build',model:{providerID:'flowprobe',id:'probe'},location:{directory:root+'/workspace'}});log('session',s); +for(const [phase,text,metadata] of [['before','PROBE []',{owned:'before'}],['after','PROBE [{"tool":"probe_plain"}]',{owned:'after-compaction',token:'probe-only-lease'}]]){ + if(phase==='after'){log('compaction',await c.session.compact({sessionID:s.id},opts()));await c.session.wait({sessionID:s.id},opts())} + log(phase+'-admission',await c.session.prompt({sessionID:s.id,text,metadata,delivery:'queue'},opts()));await c.session.wait({sessionID:s.id},opts()); +}log('compacted-context',await c.session.context({sessionID:s.id},opts())); +log('native-error-admission',await c.session.prompt({sessionID:s.id,text:'PROBE '+JSON.stringify([{tool:'shell',input:{command:'/bin/sleep 1',timeout:10}},{tool:'shell',input:{command:'printf absent-directory',workdir:root+'/absent'}}])},opts()));await c.session.wait({sessionID:s.id},opts());log('native-errors',await c.message.list({sessionID:s.id},opts())); +log('static-agent',await c.agent.get({agentID:'flow-static-probe'},opts()));log('static-listed',(await c.agent.list()).data.some(a=>a.id==='flow-static-probe')); +const ac=new AbortController();let ready;const connected=new Promise(r=>ready=r);const events=[];const stream=(async()=>{try{for await(const e of c.event.subscribe({signal:ac.signal})){events.push(e);if(e.type==='server.connected')ready()}}catch(e){log('shutdown-stream-error',String(e))}finally{log('shutdown-stream-ended',true)}})();await connected;log('cleanup-reload-start',true);await c.location.reload();log('cleanup-reload-end',true);ac.abort();await stream; diff --git a/.agents/plans/18-opencode-v2/probes/server/index.ts b/.agents/plans/18-opencode-v2/probes/server/index.ts new file mode 100644 index 00000000..59e55b41 --- /dev/null +++ b/.agents/plans/18-opencode-v2/probes/server/index.ts @@ -0,0 +1,31 @@ +import {Plugin,Provider,Model} from '@opencode/plugin'; +import {setupExtension} from './extension.ts'; +import {appendFileSync} from 'node:fs'; +const log=(type:string,data:unknown)=>appendFileSync('/tmp/flow-v2-server-probe/plugin.jsonl',JSON.stringify({type,data})+'\n'); +export default Plugin.define({id:'flow-v2-runtime-probe',async setup(ctx){ + log('setup',{version:ctx.app.version,location:ctx.location,options:ctx.options}); + await ctx.agent.transform(editor=>{for(const id of ['flow-planner','flow-worker','flow-reviewer'])editor.update(id,agent=>{agent.mode='subagent';agent.description='Probe reserved '+id;agent.permissions=[{action:'*',resource:'*',effect:'allow'},{action:'probe.*',resource:'*',effect:id==='flow-worker'?'deny':'allow'},{action:'probe_plain',resource:'*',effect:id==='flow-worker'?'deny':'allow'}]});log('missing-before',editor.get('flow-probe-missing'));editor.update('flow-probe-missing',agent=>{log('missing-callback',true);agent.description='created?'});log('missing-after',editor.get('flow-probe-missing'));}); + const providerID=Provider.ID.make('flowprobe'); + await ctx.provider.transform(editor=>editor.add({info:{...Provider.Info.empty(providerID),name:'Local probe',activation:'enabled',package:'@opencode/ai/providers/openai-compatible',settings:{baseURL:'http://127.0.0.1:47842/v1'}},models:[{...Model.Info.default(providerID,Model.ID.make('probe')),name:'Local scripted probe',capabilities:{...Model.Info.default(providerID,Model.ID.make('probe')).capabilities,tools:true}}]})); + await ctx.session.hook('title',input=>{input.result='Probe session'}); + await ctx.session.hook('compaction',input=>{log('compaction',input);input.result={summary:'Local probe compacted summary'}}); + let requestSeq=0;let contextSeq=0;const snapshots=new Map();const requests=new WeakMap();const current=new Map();const streaming=new Map(); + const controller=new AbortController(); + const stream=(async()=>{try{for await(const e of ctx.event.subscribe({signal:controller.signal}))log('event',e)}catch(e){log('event-end',String(e))}})(); + await ctx.command.transform(editor=>editor.add({name:'probe',description:'Model-free probe command',execute:async input=>{log('command',input);log('command-session',await ctx.session.get({sessionID:input.sessionID}));}})); + await ctx.tool.transform(editor=>{ + editor.add({name:'probe_hold',options:{codemode:false},description:'Hold tool while steering',input:{type:'object',properties:{}},execute:async(input,t)=>{log('hold-enter',t);await new Promise(resolve=>setTimeout(resolve,1000));log('hold-exit',{...t,aborted:t.signal.aborted});return {content:'Hold finished'};}}); + editor.add({name:'probe_explicit',options:{codemode:false},description:'Probe explicit permission',input:{type:'object',properties:{},additionalProperties:false},execute:async(input,t)=>{log('explicit-enter',t);await ctx.permission.assert({sessionID:t.sessionID,agent:t.agent,action:'probe.explicit',resources:['test'],source:{type:'tool',messageID:t.messageID,id:t.id}});log('explicit-effect',t);return {content:'explicit allowed'};}}); + editor.add({name:'probe_declared',description:'Probe declared permission',input:{type:'object',properties:{},additionalProperties:false},options:{codemode:false,permission:'probe.declared'},execute:async(input,t)=>{log('declared-effect',t);return {content:'declared allowed'};}}); + editor.add({name:'probe_plain',options:{codemode:false},description:'Probe implicit permission',input:{type:'object',properties:{},additionalProperties:false},execute:async(input,t)=>{log('plain-effect',t);return {content:'plain ran'};}}); + }); + for(const name of ['execute.before','execute.after'] as const)await ctx.tool.hook(name,async input=>{log(name,input);log(name+'-session',await ctx.session.get({sessionID:input.sessionID}));if(input.tool.startsWith('probe_')){log(name+'-context',{execution:input,current:current.get(input.sessionID),messages:await ctx.session.context({sessionID:input.sessionID})});}}); + await ctx.permission.hook('evaluate',input=>log('permission',input)); + await ctx.session.hook('prompt',async input=>{log('prompt',input);if(input.metadata?.slowAdmission)await new Promise(resolve=>setTimeout(resolve,100));if(input.metadata?.rejectProbe)throw new Error('Probe stale token rejection');}); + await ctx.session.hook('context',input=>{const snapshot=structuredClone({contextSeq:++contextSeq,sessionID:input.sessionID,agent:input.agent,model:input.model,messages:input.messages});snapshots.set(input.sessionID,snapshot);log('context-snapshot',snapshot);log('context',{sessionID:input.sessionID,agent:input.agent,model:input.model,tools:input.tools,messages:input.messages});}); + await ctx.session.hook('http.request',input=>{if(new URL(input.request.url).hostname!=='127.0.0.1')throw new Error('Probe forbids remote model access');const snapshot=snapshots.get(input.sessionID);const request={requestSeq:++requestSeq,kind:input.kind,snapshot};requests.set(input.request,request);if(input.kind==='primary')current.set(input.sessionID,request);log('http.request',{sessionID:input.sessionID,agent:input.agent,kind:input.kind,url:input.request.url,...request})}); + await ctx.session.hook('http.response',input=>{const request=requests.get(input.request);log('http.response',{sessionID:input.sessionID,kind:input.kind,requestMatched:!!request,request});if(input.kind!=='primary'||!input.response.body)return;const n=(streaming.get(input.sessionID)||0)+1;streaming.set(input.sessionID,n);log('primary-stream-start',{sessionID:input.sessionID,n,request});const reader=input.response.body.getReader();let ended=false;const end=reason=>{if(ended)return;ended=true;streaming.set(input.sessionID,(streaming.get(input.sessionID)||1)-1);log('primary-stream-end',{sessionID:input.sessionID,reason,request})};input.response=new Response(new ReadableStream({async pull(controller){try{const next=await reader.read();if(next.done){end('done');controller.close()}else controller.enqueue(next.value)}catch(error){end('error');controller.error(error)}},async cancel(reason){end('cancel');await reader.cancel(reason)}}),{status:input.response.status,headers:input.response.headers});}); + await setupExtension(ctx,log,snapshots,current); + log('tools',(await ctx.tool.list()).map(({execute,...tool})=>tool)); + return async()=>{controller.abort();await stream;log('cleanup',true)}; +}}); diff --git a/.agents/plans/18-opencode-v2/probes/server/inspect.mjs b/.agents/plans/18-opencode-v2/probes/server/inspect.mjs new file mode 100644 index 00000000..c1ef03b7 --- /dev/null +++ b/.agents/plans/18-opencode-v2/probes/server/inspect.mjs @@ -0,0 +1,8 @@ +import {OpenCode} from '@opencode/client'; +import {readFileSync,writeFileSync} from 'node:fs'; +const password=readFileSync('/tmp/flow-v2-server-probe/server.stdout','utf8').match(/server password (.+)/)[1]; +export const c=OpenCode.make({baseUrl:'http://127.0.0.1:47841',headers:{authorization:'Basic '+Buffer.from('opencode:'+password).toString('base64'),'x-opencode-directory':'/tmp/flow-v2-server-probe/workspace'}}); +await c.location.reload(); +console.log('plugins',JSON.stringify(await c.plugin.list())); +console.log('commands',JSON.stringify(await c.command.list())); +console.log('agents',JSON.stringify(await c.agent.list())); diff --git a/.agents/plans/18-opencode-v2/probes/server/interleavings.mjs b/.agents/plans/18-opencode-v2/probes/server/interleavings.mjs new file mode 100644 index 00000000..abc235fe --- /dev/null +++ b/.agents/plans/18-opencode-v2/probes/server/interleavings.mjs @@ -0,0 +1,15 @@ +import {OpenCode} from '@opencode/client';import {readFileSync,appendFileSync} from 'node:fs'; +const root='/tmp/flow-v2-server-probe';const password=readFileSync(root+'/server.stdout','utf8').match(/server password (.+)/)[1];const c=OpenCode.make({baseUrl:'http://127.0.0.1:47841',headers:{authorization:'Basic '+Buffer.from('opencode:'+password).toString('base64'),'x-opencode-directory':root+'/workspace'}}); +const log=(type,data)=>{appendFileSync(root+'/interleavings.jsonl',JSON.stringify({type,data})+'\n');console.log(type,JSON.stringify(data).slice(0,600))};const opts=()=>({signal:AbortSignal.timeout(10000)}); +const create=()=>c.session.create({title:'Interleaving probe',agent:'build',model:{providerID:'flowprobe',id:'probe'},location:{directory:root+'/workspace'}}); +const ac=new AbortController();let ready;const connected=new Promise(r=>ready=r);const events=[];const pending=[];const stream=(async()=>{try{for await(const e of c.event.subscribe({signal:ac.signal})){events.push(e);if(e.type==='server.connected')ready();for(const w of pending)if(w.test(e))w.resolve(e)}}catch(e){log('stream-error',String(e))}})();await connected; +const waitEvent=(test)=>new Promise((resolve,reject)=>{const existing=events.find(test);if(existing)return resolve(existing);const timer=setTimeout(()=>reject(Error('event deadline')),5000);pending.push({test,resolve:e=>{clearTimeout(timer);resolve(e)}})}); +log('health',await c.server.info()); +const s=await create();log('session',s);const id='msg_flowprobe_race_'+Date.now();log('race',await Promise.allSettled([c.session.prompt({sessionID:s.id,id,text:'PROBE []',metadata:{slowAdmission:true}},opts()),c.session.prompt({sessionID:s.id,id,text:'PROBE []',metadata:{slowAdmission:true}},opts())]));await c.session.wait({sessionID:s.id},opts()); +log('rejected-admission',await Promise.allSettled([c.session.prompt({sessionID:s.id,text:'must not enter',metadata:{rejectProbe:true}},opts())])); +const first=await c.session.prompt({sessionID:s.id,text:'DELAY PROBE []',metadata:{owned:'initial'}},opts());log('first',first);await waitEvent(e=>e.type==='session.step.started'&&e.data.sessionID===s.id&&e.created>=first.time.created); +log('queued',await c.session.prompt({sessionID:s.id,text:'PROBE [{"tool":"probe_plain"}]',metadata:{owned:'continuation'},delivery:'queue'},opts()));log('human-queued',await c.session.prompt({sessionID:s.id,text:'Human input alongside continuation',metadata:{owned:'human'},delivery:'queue'},opts())); +await c.session.wait({sessionID:s.id},opts());log('queue-messages',await c.message.list({sessionID:s.id},opts())); +const interrupted=await create();log('interrupt-session',interrupted);const admission=await c.session.prompt({sessionID:interrupted.id,text:'DELAY PROBE []'},opts());await waitEvent(e=>e.type==='session.step.started'&&e.data.sessionID===interrupted.id);log('interrupt',await c.session.interrupt({sessionID:interrupted.id,resume:false},opts()));await c.session.wait({sessionID:interrupted.id},opts());log('interrupt-messages',await c.message.list({sessionID:interrupted.id},opts())); +log('replay',await Array.fromAsync(c.session.log({sessionID:s.id,after:0,follow:false},opts()))); +log('all-events',events);ac.abort();await stream;log('stream-ended',true); diff --git a/.agents/plans/18-opencode-v2/probes/server/log-check.mjs b/.agents/plans/18-opencode-v2/probes/server/log-check.mjs new file mode 100644 index 00000000..69a1042c --- /dev/null +++ b/.agents/plans/18-opencode-v2/probes/server/log-check.mjs @@ -0,0 +1,6 @@ +import {OpenCode} from '@opencode/client';import {readFileSync,writeFileSync} from 'node:fs'; +const root='/tmp/flow-v2-server-probe';const p=readFileSync(root+'/server.stdout','utf8').match(/server password (.+)/)[1];const entries=readFileSync(root+'/interleavings.jsonl','utf8').trim().split('\n').map(JSON.parse);const id=entries.findLast(x=>x.type==='session').data.id; +const c=OpenCode.make({baseUrl:'http://127.0.0.1:47841',headers:{authorization:'Basic '+Buffer.from('opencode:'+p).toString('base64')},fetch:async(url,init)=>{console.log(String(url));return fetch(url,init)}}); +const out=[]; +for(const after of [undefined,0,1,-1,20]){try{const rows=await Array.fromAsync(c.session.log({sessionID:id,after,follow:false},{signal:AbortSignal.timeout(3000)}));out.push({after,rows});console.log('rows',after,rows.length,rows.map(x=>x.type).slice(0,5))}catch(e){console.log('error',after,String(e));}} +writeFileSync(root+'/log-check.json',JSON.stringify(out,null,2)); diff --git a/.agents/plans/18-opencode-v2/probes/server/package.json b/.agents/plans/18-opencode-v2/probes/server/package.json new file mode 100644 index 00000000..3fb86fd3 --- /dev/null +++ b/.agents/plans/18-opencode-v2/probes/server/package.json @@ -0,0 +1,9 @@ +{ + "name": "flow-v2-runtime-probe", + "private": true, + "type": "module", + "dependencies": { + "@opencode/client": "2.1.4", + "@opencode/plugin": "2.1.4" + } +} diff --git a/.agents/plans/18-opencode-v2/probes/server/run-cases.mjs b/.agents/plans/18-opencode-v2/probes/server/run-cases.mjs new file mode 100644 index 00000000..795c0152 --- /dev/null +++ b/.agents/plans/18-opencode-v2/probes/server/run-cases.mjs @@ -0,0 +1,20 @@ +import {OpenCode} from '@opencode/client'; +import {readFileSync,appendFileSync} from 'node:fs'; +const root='/tmp/flow-v2-server-probe'; +const password=readFileSync(root+'/server.stdout','utf8').match(/server password (.+)/)[1]; +const c=OpenCode.make({baseUrl:'http://127.0.0.1:47841',headers:{authorization:'Basic '+Buffer.from('opencode:'+password).toString('base64'),'x-opencode-directory':root+'/workspace'}}); +const log=(type,data)=>{appendFileSync(root+'/cases.jsonl',JSON.stringify({type,data})+'\n');console.log(type,JSON.stringify(data).slice(0,1200))}; +const req={signal:AbortSignal.timeout(20000)}; +log('health',await c.server.info()); +log('models',await c.model.list()); +const parent=await c.session.create({title:'Native shell',agent:'build',model:{providerID:'flowprobe',id:'probe'},location:{directory:root+'/workspace'}});log('parent',parent); +const child=await c.session.create({title:'Child',agent:'general',parentID:parent.id,model:{providerID:'flowprobe',id:'probe'},location:{directory:root+'/workspace'}});log('child',child); +await c.session.command({sessionID:parent.id,name:'probe',text:'read identity'},req); +for(const [label,session,actions] of [ + ['shell',parent,[{tool:'shell',input:{command:'printf probe-ok',description:'probe success'}},{tool:'shell',input:{command:'printf probe-failed; exit 7',description:'probe nonzero'}},{tool:'shell',input:{command:"/usr/bin/yes probe-line | /usr/bin/head -n 12000",description:'probe large output'}}]], + ['allow',child,[{tool:'probe_plain'},{tool:'probe_declared'},{tool:'probe_explicit'}]] +]){ + log(label+'-admission',await c.session.prompt({sessionID:session.id,text:'PROBE '+JSON.stringify(actions)},req)); + await c.session.wait({sessionID:session.id},req); + log(label+'-messages',await c.message.list({sessionID:session.id},req)); +} diff --git a/.agents/plans/18-opencode-v2/probes/server/run-probe.py b/.agents/plans/18-opencode-v2/probes/server/run-probe.py new file mode 100644 index 00000000..ab25d1b3 --- /dev/null +++ b/.agents/plans/18-opencode-v2/probes/server/run-probe.py @@ -0,0 +1,59 @@ +import argparse,base64,json,os,pathlib,signal,subprocess,time,urllib.request +r=pathlib.Path('/tmp/flow-v2-server-probe') +parser=argparse.ArgumentParser() +parser.add_argument('--exe',default='/tmp/flow-opencode-v2-plan-host/npm-cache/_npx/2314e955696d2fea/node_modules/@opencode/cli-darwin-arm64/bin/opencode') +parser.add_argument('--extension-only',action='store_true') +a=parser.parse_args() +for name in ['home','config','data','cache','state','workspace','tmp']:(r/name).mkdir(exist_ok=True) +env={'PATH':'/opt/homebrew/bin:/usr/bin:/bin:/usr/sbin:/sbin','HOME':str(r/'home'),'XDG_CONFIG_HOME':str(r/'config'),'XDG_DATA_HOME':str(r/'data'),'XDG_CACHE_HOME':str(r/'cache'),'XDG_STATE_HOME':str(r/'state'),'TMPDIR':str(r/'tmp')} +if not (r/'node_modules/@opencode/client').exists():subprocess.run(['npm','install','--prefix',str(r),'--cache',str(r/'npm-cache'),'--ignore-scripts','--no-audit','--no-fund','@opencode/client@2.1.4','@opencode/plugin@2.1.4'],env=env,cwd=r,check=True) +for name in ['launch.json','provider.pid']: + p=r/name + if p.exists(): + pid=json.loads(p.read_text())['pid'] if name=='launch.json' else int(p.read_text()) + command=subprocess.run(['/bin/ps','-p',str(pid),'-o','command='],capture_output=True,text=True).stdout + if str(r) in command or a.exe in command: + try:os.kill(pid,signal.SIGTERM) + except ProcessLookupError:pass +version=subprocess.run([a.exe,'--version'],env=env,cwd=r,capture_output=True,text=True,check=True).stdout.strip() +assert version=='opencode v2.1.4',version +(r/'version.json').write_text(json.dumps({'stdout':version,'executable':a.exe})) +argv=[a.exe,'serve','--hostname','127.0.0.1','--port','47841'] +server=subprocess.Popen(argv,env=env,cwd=r/'workspace',stdout=open(r/'server.stdout','w'),stderr=open(r/'server.stderr','w'),start_new_session=True) +(r/'launch.json').write_text(json.dumps({'pid':server.pid,'argv':argv,'cwd':str(r/'workspace'),'env':env},indent=2)) +os.chmod(r/'server.stdout',0o600) +provider=subprocess.Popen(['node',str(r/'fake-provider.mjs')],env=env,cwd=r,stdout=open(r/'provider.stdout','w'),stderr=open(r/'provider.stderr','w'),start_new_session=True) +(r/'provider.pid').write_text(str(provider.pid)) +try: + deadline=time.monotonic()+10 + while time.monotonic(){appendFileSync(root+'/steer.jsonl',JSON.stringify({type,data})+'\n');console.log(type,JSON.stringify(data).slice(0,700))};const opts=()=>({signal:AbortSignal.timeout(10000)});const ac=new AbortController();let ready;const connected=new Promise(r=>ready=r);const events=[];const waiters=[];const stream=(async()=>{try{for await(const e of c.event.subscribe({signal:ac.signal})){events.push(e);if(e.type==='server.connected')ready();for(const w of waiters)if(w.test(e))w.resolve(e)}}catch(e){log('stream-error',String(e))}})();await connected; +const waitEvent=test=>new Promise((resolve,reject)=>{const e=events.find(test);if(e)return resolve(e);const timer=setTimeout(()=>reject(Error('event timeout')),5000);waiters.push({test,resolve:e=>{clearTimeout(timer);resolve(e)}})}); +for(const phase of ['http-pending','tool-running']){ + const s=await c.session.create({title:phase,agent:'build',model:{providerID:'flowprobe',id:'probe'},location:{directory:root+'/workspace'}});log(phase+'-session',s); + const text=phase==='http-pending'?'DELAY PROBE [{"tool":"probe_plain"}]':'PROBE [{"tool":"probe_hold"}]';log(phase+'-initial',await c.session.synthetic({sessionID:s.id,text,metadata:{owned:'automatic-initial'},delivery:'queue',resume:true},opts())); + if(phase==='http-pending')await waitEvent(e=>e.type==='session.text.delta'&&e.data.sessionID===s.id&&e.data.delta.includes('Probe started')); + else{const until=Date.now()+3000;while(Date.now()x.type==='hold-enter'&&x.data.sessionID===s.id))break;await new Promise(r=>setTimeout(r,10));} } + log(phase+'-steer',await c.session.prompt({sessionID:s.id,text:'PROBE [{"tool":"probe_explicit"}]',metadata:{owned:'real-human-steer'},delivery:'steer'},opts()));await c.session.wait({sessionID:s.id},opts());log(phase+'-messages',await c.message.list({sessionID:s.id},opts())); +}log('events',events);ac.abort();await stream; diff --git a/.agents/plans/18-opencode-v2/probes/server/verify-extension.py b/.agents/plans/18-opencode-v2/probes/server/verify-extension.py new file mode 100644 index 00000000..fb131913 --- /dev/null +++ b/.agents/plans/18-opencode-v2/probes/server/verify-extension.py @@ -0,0 +1,66 @@ +import json,pathlib +r=pathlib.Path('/tmp/flow-v2-server-probe') +result=json.loads((r/'automatic-extension-result.json').read_text()) +plugin=[json.loads(l) for l in (r/'extension-plugin.jsonl').read_text().splitlines()] +raw=[json.loads(l) for l in (r/'plugin.jsonl').read_text().splitlines()] +provider=[json.loads(l) for l in (r/'provider.jsonl').read_text().splitlines()] +checklist=[] +def check(name,value): + assert value,name + checklist.append(name) +check('four extension cases passed',result['status']=='PASS' and len(result['cases'])==4) +for case in result['cases']: + mode=case['mode'];sid=case['sessionID'];hooks=[x for x in plugin if x['data'].get('sessionID')==sid] + native=[x for x in raw if isinstance(x.get('data'),dict) and (x['data'].get('sessionID')==sid or x['data'].get('execution',{}).get('sessionID')==sid)] + check(mode+' exact admission receipt',any(x['type']=='ext-admitted' and x['data']['receipt']['id']==case['inputID'] for x in hooks)) + if mode.startswith('compact'): + check(mode+' genuine automatic compaction',case['compaction']['reason']=='auto' and case['compaction']['status']=='completed') + control=next(x['data']['control'] for x in hooks if x['type']=='ext-compaction-control') + check(mode+' synchronous running control identity',control['id']==case['compaction']['id'] and control['status']=='running' and control['reason']=='auto') + enter=next(x['data'] for x in hooks if x['type']=='ext-compaction-enter') + check(mode+' compaction consumes original dispatch',enter['consumed']==[case['inputID']] and enter['epoch']==1 and enter['revoked'] is False) + stream_active=False;generation=0;assistant_generation={} + for x in native: + d=x['data'] + if x['type']=='context-snapshot': + check(mode+' primary context waits for stream end',not stream_active);generation+=1 + elif x['type']=='primary-stream-start': + check(mode+' primary response streams do not overlap',not stream_active and d['n']==1);stream_active=True + elif x['type']=='primary-stream-end':stream_active=False + elif x['type']=='execute.before':assistant_generation.setdefault(d['messageID'],generation) + if x['type'] in ['execute.before','execute.after']: + check(mode+' old assistant tools precede next primary context',assistant_generation[d['messageID']]==generation) + if mode=='compact-uninterrupted': + carries=[x['data'] for x in hooks if x['type']=='ext-continuity'] + check('same epoch crosses exact compaction control',bool(carries) and all(x['epoch']==1 and x['controlID']==control['id'] and x['priorConsumed']==[case['inputID']] and x['consumed']==[control['id']] and x['summaryAuthority'] is False for x in carries)) + post=next(x['data'] for x in hooks if x['type']=='ext-tool-authorized' and x['data']['execution']['id']=='call_post_compaction') + check('post-compaction tool retains epoch one',post['epoch']==1) + before=next(x['data'] for x in hooks if x['type']=='ext-tool-attempt' and x['data']['execution']['id']=='call_post_compaction') + i=next(i for i,m in enumerate(before['native']) if m['id']==before['execution']['messageID']);assistant=before['native'][i] + check('post-compaction exact assistant and call',assistant['agent']==before['execution']['agent'] and any(c['type']=='tool' and c['id']=='call_post_compaction' and c['name']==before['execution']['tool'] for c in assistant['content'])) + check('post-compaction prefix matches frozen consumed control',[m['id'] for m in before['native'][:i] if m['type'] in ['user','synthetic','compaction']]==[m['id'] for m in before['primary']['snapshot']['messages'] if m['role']=='user']==[control['id']]) + else: + revoked=next(i for i,x in enumerate(hooks) if x['type']=='ext-revoked') + check('real steer occurs inside compaction interval',next(i for i,x in enumerate(hooks) if x['type']=='ext-compaction-enter') ',1)[1] + check('rejected native shell effect file absent',not pathlib.Path(effect).exists()) + check('rejected native shell never reaches shell creation',not any(x['type']=='ext-native-shell-created' and effect in x['data']['command'] for x in plugin)) + messages=[json.loads(l) for l in (r/'automatic-extension.jsonl').read_text().splitlines()] + transcript=next(x['data']['data'] for x in reversed(messages) if x['type']=='stale-tool-messages') + assistant=next(m for m in transcript if m['id']==reject['messageID']) + call=next(c for c in assistant['content'] if c['type']=='tool' and c['id']==reject['id']) + check('native tool reports rejection rather than success',call['state']['status']=='error' and 'EXTENSION_REVOKED_BEFORE_NATIVE_TOOL' in call['state']['error']['message']) +receipt={'status':'PASS','run':result['run'],'host':'OpenCode 2.1.4 Darwin arm64','checks':checklist,'cases':result['cases'],'scope':'Bounded local scripted HTTP provider. No product integration certification.','supportedConditions':['Exact admitted synthetic ID and private live epoch exist before positive authority.','Freeze primary context and Request identity; reject overlap or immutable assistant binding conflicts.','Read and freeze running native auto-compaction ID in the synchronous compaction hook.','At resumed context, require that exact completed auto-compaction ID and unchanged live epoch.','Reject unknown inputs and real steer; do not recover authority from summary text.','Before every native tool, verify exact assistant/call identity, frozen input prefix, and unrevoked epoch.']} +(r/'extension-verification.json').write_text(json.dumps(receipt,indent=2));print('PASS',len(checklist),'extension assertions') diff --git a/.agents/plans/18-opencode-v2/probes/server/verify.py b/.agents/plans/18-opencode-v2/probes/server/verify.py new file mode 100644 index 00000000..c4679a8b --- /dev/null +++ b/.agents/plans/18-opencode-v2/probes/server/verify.py @@ -0,0 +1,59 @@ +import collections,json,pathlib +r=pathlib.Path('/tmp/flow-v2-server-probe') +def rows(name):return [json.loads(line) for line in (r/name).read_text().splitlines() if line.strip()] +def last(xs,kind):return next(x['data'] for x in reversed(xs) if x['type']==kind) +p=rows('plugin.jsonl');extended=rows('extended.jsonl');inter=rows('interleavings.jsonl');steer=rows('steer.jsonl');edges=rows('edges.jsonl');checks=[] +def check(name,value): + assert value,name + checks.append(name) +check('exact host health',json.loads((r/'health.json').read_text())['version']=='2.1.4') +check('plugin options and lifecycle',any(x['type']=='setup' and x['data']['options']=={'receipt':'isolated-options'} for x in p) and any(x['type']=='cleanup' for x in p)) +check('missing update invokes callback',any(x['type']=='missing-callback' and x['data'] is True for x in p)) +check('reserved agents created',{'flow-planner','flow-worker','flow-reviewer'} <= {a['id'] for a in last(extended,'reserved-agents')}) +check('native child parentage',last(extended,'flow-worker')['parentID'] is not None) +worker=last(extended,'flow-worker')['id'];reviewer=last(extended,'flow-reviewer')['id'] +check('allowed custom effects',{'plain-effect','declared-effect','explicit-effect'} <= {x['type'] for x in p if isinstance(x.get('data'),dict) and x['data'].get('sessionID')==reviewer}) +check('denied custom effects absent',not any(x['type'] in ['plain-effect','declared-effect','explicit-effect'] and x['data']['sessionID']==worker for x in p)) +check('explicit denial has native error',any(x['type']=='execute.after' and x['data']['sessionID']==worker and x['data']['tool']=='probe_explicit' and x['data']['status']=='error' for x in p)) +check('shell nonzero remains completed',any(x['type']=='execute.after' and x['data']['tool']=='shell' and x['data']['status']=='completed' and x['data']['result'].get('metadata',{}).get('exit')==7 for x in p)) +check('shell execution error',any(x['type']=='execute.after' and x['data']['tool']=='shell' and x['data']['status']=='error' for x in p)) +check('shell timeout lacks exit',any(x['type']=='execute.after' and x['data']['tool']=='shell' and x['data']['status']=='completed' and x['data']['result'].get('metadata',{}).get('timeout') is True and 'exit' not in x['data']['result']['metadata'] for x in p)) +check('exact full output file',last(extended,'full-output')['exact'] and last(extended,'full-output')['size']==132000) +check('exact full output API',last(extended,'shell-output')['data']['exact'] and last(extended,'shell-output')['data']['truncated'] is False) +race=last(inter,'race');raceid=race[0]['value']['id'];session=last(inter,'session')['id'];events=last(inter,'all-events') +check('duplicate admission converges',all(x['status']=='fulfilled' and x['value']['id']==raceid for x in race)) +check('duplicate hooks run twice',sum(x['type']=='prompt' and x['data']['messageID']==raceid for x in p)==2) +check('duplicate creates one enqueue',sum(e['type']=='session.inbox.enqueued' and e['data']['inboxID']==raceid for e in events)==1) +check('rejected hook prevents admission',last(inter,'rejected-admission')[0]['status']=='rejected') +check('real interruption',last(inter,'interrupt')['interrupted'] is True and any(m['type']=='idle' and m['outcome']=='interrupted' for m in last(inter,'interrupt-messages')['data'])) +check('compaction retained post input',any(m['type']=='user' and m.get('metadata',{}).get('owned')=='after-compaction' for m in last(edges,'compacted-context'))) +check('static agent public lookup',last(edges,'static-agent')['data']['id']=='flow-static-probe' and last(edges,'static-listed') is True) +joined=[] +for phase in ['http-pending','tool-running']: + s=last(steer,phase+'-session')['id'];initial=last(steer,phase+'-initial')['id'];human=last(steer,phase+'-steer')['id'] + starts=[x['data'] for x in p if x['type']=='primary-stream-start' and x['data']['sessionID']==s] + check(phase+' primary streams do not overlap',bool(starts) and all(x['n']==1 for x in starts)) + check(phase+' response joins same Request',all(x['data']['requestMatched'] for x in p if x['type']=='http.response' and x['data']['sessionID']==s)) + joins=[x['data'] for x in p if x['type']=='execute.before-context' and isinstance(x.get('data'),dict) and x['data']['execution']['sessionID']==s] + origins={} + for j in joins: + execution=j['execution'];messages=j['messages'];i=next(i for i,m in enumerate(messages) if m['id']==execution['messageID']);assistant=messages[i];inputs=[m['id'] for m in messages[:i] if m['type'] in ['user','synthetic']];consumed=[m['id'] for m in j['current']['snapshot']['messages'] if m['role']=='user'] + check(phase+' exact assistant and call '+execution['tool'],assistant['type']=='assistant' and assistant['agent']==execution['agent'] and any(c['type']=='tool' and c['id']==execution['id'] and c['name']==execution['tool'] for c in assistant['content'])) + check(phase+' frozen inputs equal assistant prefix '+execution['tool'],inputs==consumed) + origins[execution['tool']]=consumed[-1] + joined.append({'phase':phase,'assistant':execution['messageID'],'call':execution['id'],'tool':execution['tool'],'requestSeq':j['current']['requestSeq'],'consumedInputIDs':consumed}) + check(phase+' old execution not relabeled',origins.get('probe_plain' if phase=='http-pending' else 'probe_hold')==initial and origins.get('probe_explicit')==human) + local=[x for x in p if isinstance(x.get('data'),dict) and (x['data'].get('sessionID')==s or x['data'].get('execution',{}).get('sessionID')==s)] + active=False;context_generation=0;assistant_generation={} + for x in local: + d=x['data'] + if x['type']=='context-snapshot': + check(phase+' context waits for preceding primary stream',not active);context_generation+=1 + elif x['type']=='primary-stream-start':active=True + elif x['type']=='primary-stream-end':active=False + elif x['type']=='execute.before':assistant_generation.setdefault(d['messageID'],context_generation) + if x['type'] in ['execute.before','execute.after']: + check(phase+' old assistant tool stays before next primary context',assistant_generation[d['messageID']]==context_generation) +check('client subscriber terminates on host shutdown',json.loads((r/'shutdown.json').read_text())['ended']) +result={'status':'PASS','meaning':'Disposable assertions passed. This is not an integrated Flow certification.','checks':checks,'causalJoins':joined,'limitations':['Only scripted local OpenAI-compatible provider and Darwin arm64 tested.','No general primary-request serialization guarantee claimed. Fail closed on overlap or unknown joins.','No complete async event prefix or durable replay established.','Automatic compaction during a live automatic turn remains unverified.','Revocation of an already running operation requires parent integration design and checks.']} +(r/'verification.json').write_text(json.dumps(result,indent=2));print('PASS',len(checks),'runtime assertions') diff --git a/.agents/plans/18-opencode-v2/probes/server/workspace.json b/.agents/plans/18-opencode-v2/probes/server/workspace.json new file mode 100644 index 00000000..92605b9e --- /dev/null +++ b/.agents/plans/18-opencode-v2/probes/server/workspace.json @@ -0,0 +1 @@ +{"plugins":[{"package":"/tmp/flow-v2-server-probe","options":{"receipt":"isolated-options"}}]} diff --git a/.agents/plans/18-opencode-v2/qualification-dispatch-draft.json b/.agents/plans/18-opencode-v2/qualification-dispatch-draft.json new file mode 100644 index 00000000..781c0d67 --- /dev/null +++ b/.agents/plans/18-opencode-v2/qualification-dispatch-draft.json @@ -0,0 +1,131 @@ +{ + "status": "draft-not-authorized", + "packageVersion": "10.0.0", + "managerModels": 2, + "cases": [ + { + "caseId": "delivery-summary-completed", + "stepsPerAttempt": 1, + "primaryAttemptsPerModel": 3, + "primaryDispatchesPerModel": 3, + "reserveDispatchesPerModel": 1 + }, + { + "caseId": "delivery-summary-deferred", + "stepsPerAttempt": 2, + "primaryAttemptsPerModel": 3, + "primaryDispatchesPerModel": 6, + "reserveDispatchesPerModel": 2 + }, + { + "caseId": "delivery-summary-observed-failure", + "stepsPerAttempt": 1, + "primaryAttemptsPerModel": 3, + "primaryDispatchesPerModel": 3, + "reserveDispatchesPerModel": 1 + }, + { + "caseId": "delivery-full-detail-followup", + "stepsPerAttempt": 2, + "primaryAttemptsPerModel": 3, + "primaryDispatchesPerModel": 6, + "reserveDispatchesPerModel": 2 + }, + { + "caseId": "delivery-idle-after-close", + "stepsPerAttempt": 2, + "primaryAttemptsPerModel": 3, + "primaryDispatchesPerModel": 6, + "reserveDispatchesPerModel": 2 + }, + { + "caseId": "happy-path", + "stepsPerAttempt": 1, + "primaryAttemptsPerModel": 3, + "primaryDispatchesPerModel": 3, + "reserveDispatchesPerModel": 1 + }, + { + "caseId": "plan-only-stops", + "stepsPerAttempt": 1, + "primaryAttemptsPerModel": 3, + "primaryDispatchesPerModel": 3, + "reserveDispatchesPerModel": 1 + }, + { + "caseId": "goal-change-refused", + "stepsPerAttempt": 2, + "primaryAttemptsPerModel": 3, + "primaryDispatchesPerModel": 6, + "reserveDispatchesPerModel": 2 + }, + { + "caseId": "continuation-accepted", + "stepsPerAttempt": 2, + "primaryAttemptsPerModel": 3, + "primaryDispatchesPerModel": 6, + "reserveDispatchesPerModel": 2 + }, + { + "caseId": "failing-gate-blocks", + "stepsPerAttempt": 1, + "primaryAttemptsPerModel": 10, + "primaryDispatchesPerModel": 10, + "reserveDispatchesPerModel": 1 + }, + { + "caseId": "resumes-after-interruption", + "stepsPerAttempt": 2, + "primaryAttemptsPerModel": 3, + "primaryDispatchesPerModel": 6, + "reserveDispatchesPerModel": 2 + }, + { + "caseId": "unprovable-claim-refused", + "stepsPerAttempt": 1, + "primaryAttemptsPerModel": 10, + "primaryDispatchesPerModel": 10, + "reserveDispatchesPerModel": 1 + }, + { + "caseId": "skipped-case-named-binding", + "stepsPerAttempt": 1, + "primaryAttemptsPerModel": 3, + "primaryDispatchesPerModel": 3, + "reserveDispatchesPerModel": 1 + }, + { + "caseId": "inspection-failed-audit-completes", + "stepsPerAttempt": 1, + "primaryAttemptsPerModel": 10, + "primaryDispatchesPerModel": 10, + "reserveDispatchesPerModel": 1 + }, + { + "caseId": "auto-two-features-evidence", + "stepsPerAttempt": 1, + "primaryAttemptsPerModel": 3, + "primaryDispatchesPerModel": 3, + "reserveDispatchesPerModel": 1 + }, + { + "caseId": "auto-prerequisite-repair", + "stepsPerAttempt": 1, + "primaryAttemptsPerModel": 3, + "primaryDispatchesPerModel": 3, + "reserveDispatchesPerModel": 1 + }, + { + "caseId": "auto-observe-with-required-pass", + "stepsPerAttempt": 1, + "primaryAttemptsPerModel": 3, + "primaryDispatchesPerModel": 3, + "reserveDispatchesPerModel": 1 + } + ], + "primaryDispatches": 180, + "reserveDispatches": 46, + "routeProbes": 2, + "totalDispatchLimit": 228, + "semantics": "Top-level manager steps and route probes only; internal generation is separate; no dollar cap; canary requires separate approval." +} diff --git a/.agents/plans/18-opencode-v2/registry.json b/.agents/plans/18-opencode-v2/registry.json new file mode 100644 index 00000000..f8333dbd --- /dev/null +++ b/.agents/plans/18-opencode-v2/registry.json @@ -0,0 +1,88 @@ +{ + "observedOn": "2026-10-24", + "packages": [ + { + "name": "@opencode/plugin", + "version": "2.1.4", + "source": "https://registry.npmjs.org/@opencode%2fplugin/2.1.4", + "integrity": "sha512-SGAkdGp0VRxos2BEkRiyRl75G7DtuvP0ywgiEiHz1PbdUwUe2EAURMrzFgLCYCpWEm/zaNrguxPDVL0carrCsA==", + "tarball": "https://registry.npmjs.org/@opencode/plugin/-/plugin-2.1.4.tgz" + }, + { + "name": "@opencode/client", + "version": "2.1.4", + "source": "https://registry.npmjs.org/@opencode%2fclient/2.1.4", + "integrity": "sha512-xNHP8Xu4wcq4G849Xnbb8IxYxMTzF0TqvohWVUQZH5aURrVHIIbHMfXbZY6s4i4C9kpVbwjXFFwWAo6KIK3oiw==", + "tarball": "https://registry.npmjs.org/@opencode/client/-/client-2.1.4.tgz" + }, + { + "name": "@opencode/schema", + "version": "2.1.4", + "source": "https://registry.npmjs.org/@opencode%2fschema/2.1.4", + "integrity": "sha512-PLr8eFW81Yz6U7YsSgfSUClUUH//8omh3HiXu/L08VX5arorWNAu2uSjoXyHIGkm7TaTbdXBGPmmDfpvrDM2wQ==", + "tarball": "https://registry.npmjs.org/@opencode/schema/-/schema-2.1.4.tgz" + }, + { + "name": "@opencode/cli", + "version": "2.1.4", + "source": "https://registry.npmjs.org/@opencode%2fcli/2.1.4", + "integrity": "sha512-vtHFa/1frSylpx1cZRuzmM2ecHWn6YB8cNvhw8eWKPvSFvDnckZXugDbi3LJYD9TGjRx2U6JRo725qwqtwMUfQ==", + "tarball": "https://registry.npmjs.org/@opencode/cli/-/cli-2.1.4.tgz", + "bin": { + "opencode": "bin/opencode.exe", + "opencode2": "bin/opencode.exe" + }, + "optionalDependencies": { + "@opencode/cli-linux-x64": "2.1.4", + "@opencode/cli-darwin-x64": "2.1.4", + "@opencode/cli-linux-arm64": "2.1.4", + "@opencode/cli-windows-x64": "2.1.4", + "@opencode/cli-darwin-arm64": "2.1.4", + "@opencode/cli-windows-arm64": "2.1.4", + "@opencode/cli-linux-x64-musl": "2.1.4", + "@opencode/cli-linux-arm64-musl": "2.1.4", + "@opencode/cli-linux-x64-baseline": "2.1.4", + "@opencode/cli-darwin-x64-baseline": "2.1.4", + "@opencode/cli-windows-x64-baseline": "2.1.4", + "@opencode/cli-linux-x64-baseline-musl": "2.1.4" + }, + "fileCount": 3, + "unpackedSize": 8175 + }, + { + "name": "@opencode/sdk", + "version": "2.1.4", + "source": "https://registry.npmjs.org/@opencode%2fsdk/2.1.4", + "integrity": "sha512-V2lxqZR0QbHVfyGoeaRtazI78rMOtErjq6UuIlY9Y3mtH3KenBFgI1v2xKyzFER43JDOIMxCEtRuFiWmRxpjRQ==", + "tarball": "https://registry.npmjs.org/@opencode/sdk/-/sdk-2.1.4.tgz", + "fileCount": 44, + "unpackedSize": 51950 + }, + { + "name": "opencode-ai", + "version": "1.19.4", + "source": "https://registry.npmjs.org/opencode-ai/1.19.4", + "integrity": "sha512-O3rTc9OCd6CeeJohCAjFYD3MX5U0weKzKnySfsJ9sw9uAbNZZK4meIcjd+7k3inmZB5G2qONSen3yA45yECiFA==", + "tarball": "https://registry.npmjs.org/opencode-ai/-/opencode-ai-1.19.4.tgz", + "bin": { + "opencode": "bin/opencode.exe" + }, + "optionalDependencies": { + "opencode-linux-x64": "1.19.4", + "opencode-darwin-x64": "1.19.4", + "opencode-linux-arm64": "1.19.4", + "opencode-windows-x64": "1.19.4", + "opencode-darwin-arm64": "1.19.4", + "opencode-windows-arm64": "1.19.4", + "opencode-linux-x64-musl": "1.19.4", + "opencode-linux-arm64-musl": "1.19.4", + "opencode-linux-x64-baseline": "1.19.4", + "opencode-darwin-x64-baseline": "1.19.4", + "opencode-windows-x64-baseline": "1.19.4", + "opencode-linux-x64-baseline-musl": "1.19.4" + }, + "fileCount": 4, + "unpackedSize": 7852 + } + ] +} diff --git a/.agents/plans/18-opencode-v2/research.md b/.agents/plans/18-opencode-v2/research.md new file mode 100644 index 00000000..7c349455 --- /dev/null +++ b/.agents/plans/18-opencode-v2/research.md @@ -0,0 +1,163 @@ +# OpenCode v2 migration findings + +Research date is 2026-10-24. Flow HEAD is +`8103f1cd42e1b4d111f25d2a311935d4e41fb997`. The package is 9.6.0. +The checkout was clean before the plan was written. + +## The new host requires a plugin port + +The [official plugin migration guide](https://opencode.ai/v2/docs/build/plugins/migrate-v1) +replaces the function-and-hooks API with an ID and `setup(ctx)`. +Domain transforms replace global config mutation. Tools use new schemas and +structured results. Events and cleanup need explicit lifecycle handling. +The guide's optional dual-version export conflicts with the requested scope. +The plan excludes that export. + +The [general migration guide](https://opencode.ai/v2/docs/migrate-v1/) +also identifies changed server clients and terminal configuration. +Native configuration uses `plugins` and object-form package options. +The new terminal configuration is global `cli.json`. + +The [changelog](https://opencode.ai/changelog) lists 2.1.4 on October 23, 2026. +It records further public contract changes in 2.1.0, including compaction and +provider replay data. A claim covering every 2.0 release is therefore broader +than the proposed tested baseline. + +## Registry packages are separate from the legacy CLI + +Registry responses confirmed these current `latest` values. +The selected exact-version metadata and integrity values are retained in +[registry.json](registry.json). + +- [`@opencode/cli`](https://registry.npmjs.org/@opencode%2fcli/latest) is 2.1.4. +- [`@opencode/plugin`](https://registry.npmjs.org/@opencode%2fplugin/latest) is 2.1.4. +- [`@opencode/client`](https://registry.npmjs.org/@opencode%2fclient/latest) is 2.1.4. +- [`@opencode/sdk`](https://registry.npmjs.org/@opencode%2fsdk/latest) is 2.1.4. +- [`opencode-ai`](https://registry.npmjs.org/opencode-ai/latest) remains 1.19.4. + +The [official download page](https://opencode.ai/download) uses `@opencode/cli` +for the new npm installation. `@opencode/sdk` embeds a host. It is not the +replacement for an external network client in eval tooling. + +The existing development plugin pin is `@opencode-ai/plugin@1.18.31`. +Its peer range is `>=1.18.3 <2`. The local `opencode --version` reports 1.18.30. +Neither the installed binary nor package configuration was changed. + +An isolated `npm exec --package=@opencode/cli@2.1.4 -- opencode --version` +then exited zero and reported `opencode v2.1.4` on macOS arm64. +The full command and isolated directory settings are retained in +[host-version.json](host-version.json). The package's `.exe` bin name does not +prevent this launch. Linux startup and plugin behavior still need phase 0 proof. + +Released 2.1.4 plugin, client, and schema declarations were downloaded into +`/tmp/flow-opencode-v2-plan-research` for inspection. They confirm +`Plugin.Context`, domain hooks, structured tool results, session parentage, +and nested session location. These declarations do not prove runtime behavior. + +## Continuation is the highest-risk contract + +The [plugin reference](https://opencode.ai/v2/docs/build/plugins/) +places prompt hooks before durable admission. Concurrent admission can run hooks +more than once. Synthetic messages bypass prompt hooks. Those semantics make a +mechanical replacement of Flow's `chat.message` handling unsafe. + +Flow's current implementation uses assistant parentage and V1 compaction markers +to attribute automatic work. See `src/platform/opencode/auto-drive.ts`, +`auto-compaction.ts`, and `plugin-composition.ts`. +The plan requires a verified v2 attribution design instead of assuming equivalent +message fields or weakening the continuation authority checks. + +## The terminal plugin can remain in the same package + +The [CLI plugin reference](https://opencode.ai/v2/docs/build/plugins/cli/) +provides `Plugin.define`, reactive keymaps, promise-based dialogs, and toasts. +Its released declarations include dialog selection and confirmation. +The existing picker can therefore target public APIs without a custom renderer. +That is a design inference pending interaction proof. +The TUI barrel also re-exports Solid helpers. Its runtime import graph and peer +resolution must be exercised before choosing dependency declarations. + +The [CLI loading guide](https://opencode.ai/v2/docs/cli/plugins/) +loads a server-configured package's TUI component automatically. +It reserves `cli.json` for client-only plugins. +The README's instruction to configure Flow twice must change. + +## The release policy needs an explicit prospective profile + +`evals/release-policy.ts` pins the qualification host independently to 1.18.31. +That shared constant also contributes to historical policy hashes and strict +canary checks. A global edit would invalidate retained records. +The prospective host policy must select by Flow version while preserving +historical hash inputs and historical canary verification. +Its 9.6.0 profile requires 17 cases. The generic profile has nine. +A plain version bump to 10.0.0 loses three autonomous and five delivery cases. +The one-provider exception is also limited to named 9.x versions. + +Keeping the 17 cases on two providers gives 144 primary cells and 34 reserve +cells at unchanged thresholds. This is policy arithmetic, not a measured result +or an approved dispatch budget. The existing 9.6.0 profile has 72 primary and +17 reserve cells on one provider. + +The authoritative process is in `docs/development.md`, +`docs/release-qualification.md`, and +`.agents/plans/05-release-simplification/README.md`. +Paid authorization names purpose, models, dispatch count, and expiry. +Canary authorization is separate. The campaign must retain complete attempts. +Freshness limits are seven days for campaigns and 72 hours for canaries. + +`evals/provenance.ts` compares raw tarball and unpacked-manifest digests. +`.github/workflows/release.yml` already restores sealed tarballs for named +historical releases. A new candidate must preserve exact raw bytes through +publication rather than relying only on equal unpacked files. + +The active eval host, canary fixture, smoke test, metadata probe, and recovery +plugins also use V1 contracts. Their port belongs in the release-critical work. +The phrase "V2 bundle" in qualification docs names an evidence format. +It does not mean the plugin supports OpenCode 2. + +## The wiki has no current synchronization workflow + +`docs/index.md` names `docs/` as the maintained source. +No active wiki synchronization script or workflow was found. +The wiki is a separate Git repository with HEAD +`de4be508451c25564fc4bfb4911fa056d10ce3f3`. +Read-only cloning and repository metadata confirmed that it exists. + +The public [Home](https://github.com/ddv1982/flow-opencode/wiki), +[configuration](https://github.com/ddv1982/flow-opencode/wiki/reference--configuration), +and [deployment](https://github.com/ddv1982/flow-opencode/wiki/deployment) +pages describe older code paths and product behavior. +Home still links deleted `droid-wiki` video artifacts. +The proposed wiki pass preserves page URLs and replaces stale instructions +with current summaries and links to maintained docs. + +## Evidence and capability limits + +Exa search and fetch were used for official documentation. +Ref MCP was not exposed in the live tool catalog. +No Ref result, v2 plugin runtime test, paid evaluation, implementation verification, +release, or wiki publication is claimed. + +Two read-only Codex explorers inspected host integration and release/wiki +contracts at the recorded Flow SHA. Both reported integration issues. +They used `gpt-6.1-sol` with high reasoning effort and priority service. +They shared the checkout without write authority. The parent inspected the +relevant source and upstream declarations before incorporating their findings. + +An independent `grok-4.7` reviewer checked the plan and trail with high reasoning +effort. Its findings and the parent's corrections are recorded in +[review.md](review.md). The review did not certify plugin compatibility. + +## Transport correction during implementation + +The V1 experimental transport flags do not control the pinned v2 host. +The current [provider reference](https://opencode.ai/v2/docs/providers/#websockets) +documents `settings.transport`. Its statement about provider HTTP hooks does not +establish the behavior of global session hooks. A local native Responses probe +showed WebSocket traffic both without a hook and with a global session HTTP hook. + +The selected adapter sets provider, model, and variant transport to `http`. +The exact production helper sent one HTTP request and opened zero WebSockets, +even with a WebSocket variant selected. Both control cases opened a WebSocket. +This proof used native 2.1.4 and a loopback provider, without paid inference. +The retained runtime receipt and source seal are recorded in `decisions.tsv`. diff --git a/.agents/plans/18-opencode-v2/review.md b/.agents/plans/18-opencode-v2/review.md new file mode 100644 index 00000000..10aa80d6 --- /dev/null +++ b/.agents/plans/18-opencode-v2/review.md @@ -0,0 +1,52 @@ +# Review the migration plan corrections + +The independent reviewer was `grok-4.7` with high reasoning effort. +Its first-round verdict was ISSUES. The parent checked the cited source and +corrected the plan. No second independent verdict is claimed. + +## Resolve the five required corrections + +1. The install range overstated tested compatibility. The support wording now + names 2.1.4 and later 2.x releases only after a passing compatibility check. + The peer range remains install eligibility. +2. A global host-policy edit would invalidate historical policy hashes and + canary checks. Phase 3 now selects prospective policies by Flow version and + preserves historical hash inputs. It also explicitly carries reviewer-model, + permission, and recovery-key checks into the new profile. + The parent verified `releasePolicySha256` in `evals/release-policy.ts`, + policy validation in `scripts/release-metadata.ts`, and host comparison in + `scripts/eval-canary.ts`. +3. The shared configuration path was ambiguous. Phase 1 now names + `src/config-shared.ts` and permits its host-facing edits outside the adapter + directory. Domain and persistence remain independent of the host. +4. CLI executable selection needed evidence. `registry.json` now includes bin, + platform-package, file-count, and unpacked-size metadata. The parent launched + the exact npm package in isolated directories and observed version 2.1.4. + Phase 0 still requires exact server argv, health, and Linux launcher proof. +5. TUI dependencies cannot be chosen from dialog signatures. Phase 0 now + requires a real runtime import check in the packed installation. + Phase 2 selects any Solid and OpenTUI peers from that proof. + +## Resolve the additional findings + +The registry receipt now retains `@opencode/sdk@2.1.4` and +`opencode-ai@1.19.4` identities too. The normal installed executable was checked +again after the isolated launch and still reported 1.18.30. +Disposable probes stay outside the published tarball. +The scheduled compatibility check remains a non-blocking drift signal. +The reviewer keeps `flow_status` access as well as completion authority. +Catalog cell counts remain separate from paid dispatch authorization. + +The prompt-admission and synthetic-message statements come from the official +plugin guide cited in `research.md`, rather than from declaration files. +Their execution semantics remain phase 0 proof obligations. + +## Inspect the remaining release risks + +Continuation attribution, trustworthy full command output, actual permission +enforcement, TUI imports, and terminal interaction remain unverified. +Paid qualification needs a concrete approved dispatch proposal. +Ref MCP was unavailable. The plan records that capability gap. + +The review was read-only in the shared checkout. It produced no code changes. +Its direct comment scan found no authored code comments or suppressions. diff --git a/.agents/plans/18-opencode-v2/reviews/compaction-design-r8.md b/.agents/plans/18-opencode-v2/reviews/compaction-design-r8.md new file mode 100644 index 00000000..e7877338 --- /dev/null +++ b/.agents/plans/18-opencode-v2/reviews/compaction-design-r8.md @@ -0,0 +1,188 @@ +# Cross-judge report for the compaction boundary + +Cross-turn sticky behavior is inactive. Trusted hooks did not provide a receipt. This turn follows poteto-mode directly. The independent comment review was not spawned. The parent forbade delegates. The sketch comments stay, because the function bodies throw and those comments are the only behavior specification. + +This is a design judgment. I did not edit the repository, run the suite, launch a host, or touch the failed evidence files. `wc -l` on the frozen tree reports 992 lines in `src/platform/opencode/lifecycle.ts` and 997 lines in `src/platform/opencode/auto-drive.ts`. + +The base is candidate A. Totals are 14 for A and 10 for B. B has design blockers. A still needs two grafts before anyone implements it. The grafts are small because A already owns the admission ledger. B's pure snapshot cannot see the facts the parent named. + +## Scores + +| Criterion | A | B | +| --- | --- | --- | +| 1. Interrupted ordinary manual summary, with no lease, no fabricated input, and no checkpoint grant | 3 | 3 | +| 2. Accepted automatic continuation, exact delivery, retry object identity, and no stale revocation of fresh work | 2 | 1 | +| 3. Pending, live and revoked automatic manual, unknown history, checkpoint tools, and ordinary summaries stay distinct | 2 | 1 | +| 4. Prefix, control, reason, and lifecycle are checked at one boundary, with no latest-user inference and no current-epoch grant | 2 | 2 | +| 5. One owner, a small caller interface, and deleted compensating branches | 2 | 1 | +| 6. Verification plan covers the failed native sequence, the packed smoke, and a component matrix, with honest limits | 3 | 2 | + +Artifacts read end to end: + +| File | SHA-256 | +| --- | --- | +| `candidate-a/candidate.md` | `a9869f2c73ba0cd37e965c2b1bc954c69681701ee3d5327308f2c3df841b1f55` | +| `candidate-a/sketch.ts` | `d9e814ccfee875490117167a59eb3625df9aca6c47a74453be8cb1d86adec371` | +| `candidate-b/candidate.md` | `2e7a1765ccb4aa781e1b3f4709a225c6582665e21b459f982e1286ad79a589ce` | +| `candidate-b/sketch.ts` | `b941508f2c6decc7d6428405a934bef0343a4207aa5c83d42580377ac91295b6` | + +## Source facts the sketches have to meet + +`lifecycle.ts` registers a command only after `revoke` and an awaited `session.get`. `revoke` runs at line 513. `session.get` is awaited at lines 521-526. `attribution.register` runs at line 550. The new message id is created at line 537, still after `get`. + +`synthetic` does not revoke. It captures the current host epoch at line 321, awaits `session.get` at lines 333-339, creates the id at line 345, and registers at line 347. + +`revoke` at lines 146-157 ends the current primary, increments the host epoch, revokes automatic execution, and deletes the checked compaction. It leaves `#admissions` in place. + +`ExecutionAttribution.#admissions` is private at line 156. Public lookup is by a known id: `inputEpoch`, `inputOrigin`, `admitted`, and `hasAdmission` at lines 237-269. There is no enumeration and no frontier generation. `register` at lines 173-199 inserts one admission and returns its `ready` promise. `clear` at lines 547-553 rejects pending admissions. An epoch change does not. + +`compactedInputOrigin` at lines 101-112 always returns a new frozen object. The retry path keeps identity only because `compactions.get` returns the stored `CheckedAutoCompaction` and `compactionOrigins.get` returns the object stored under that same key. The set is at line 788. The get is at lines 407-410. `prepareContinuationRetry` re-enters context. It does not run the compaction hook again. + +`Lease` is a private type. `messageId` is on that type at line 148. `AutoDriveAuthority` at lines 87-91 is `hostSessionId`, `token`, and `epoch`. `captureAuthority` at lines 475-488 returns that value or null. `isAuthorityCurrent` at lines 490-497 compares `messageId` only when the caller passes a source. It returns false both when `messageId` is null and when `messageId` is a different id. No public method returns `messageId`. + +The compaction hook pushes guidance at lines 715-716 before it knows the decision. If an accepted origin still exists, lines 748-764 call `assertOwned` before `session.context`. The unknown-history return is the `inputEpoch === undefined` branch at lines 733-746. Interrupted ordinary work still has an admission, so it takes the `assertOwned` path. `root-native-facts.json` records that throw, consumed id `msg_16deb972a001cSXagGDxecwI3c` on both controls, one provider request, and no added request after the failed compact. + +The context hook does not await a pending `inputOrigin`. Lines 638-644 treat a promise as null. `owns` at lines 174-188 then calls `captureAuthority` when the owner has a source and no origin. `pending-activation.json` records the result. `activate` has made a lease capturable while `messageId` is still null, and context throws `Flow execution epoch was revoked.` The file says this is component-only. + +## Trace 1. Hook entry after a command or synthetic begins, before `session.get` returns + +The new private id does not exist in this window. A snapshot of `#admissions` cannot show it. A sketch that claims unchanged `execution-attribution.ts` also cannot grow a public list for it. + +Command. `revoke` has already moved the host epoch. Both of A's snapshots, if taken after that `revoke`, agree on the new epoch. The old ordinary origin is still in the admission record with the old epoch. A classifies it as `retired-ordinary` (`sketch.ts` lines 59-64 and 91-94, `candidate.md` lines 31-35). Manual admission then takes summary-only (sketch lines 151-153). That record has no `InputOrigin`. This does not mint the command's new epoch into an origin. It also does not see the command. The command is only a local `owner` inside `execute` until line 550. + +Synthetic. Nothing synchronous changes. The host epoch is unchanged. The frontier is unchanged. The `real` map is unchanged. A hook that enters here and finishes before line 347 is looking at the pre-synthetic ledger. If the last consumed origin is still live and the reason is `auto`, A's admit comment at lines 148-149 keeps the existing continuation. That copies the live origin's epoch. It is an execution grant for the old source, issued while `synthetic` has already checked the token and is blocked in `session.get`. + +A's frontier moves on real-prompt observation and on private registration (`sketch.ts` lines 81-86, `candidate.md` lines 31-32). Registration is the late `attribution.register`. The sketch never bumps that frontier at the start of `execute` or `synthetic`. `InputLedger.capture` is defined on `SessionContext` (sketch lines 75-76), which is the compaction and context hooks, not the command or synthetic prologue. + +B cannot see this window either. `OperationSnapshot.admissionIDs` (sketch lines 75-85) requires a list `ExecutionAttribution` does not export. `realCandidateIDs` can see the `real` map, and neither command nor synthetic writes that map. For a command, pre and post can both sit after `revoke`, so `hostEpoch` agrees and step 1 does not fire. Dead ordinary manual then returns summary-only at step 4 (sketch lines 131-132). That part matches A and does not grant. A live automatic source under synthetic still reaches step 6 and returns `continue`. + +Cleanup while the command epoch is already current is the sharp edge. B's `applyCompactionDecision` comment (sketch lines 214-216) runs `failOwned` when the refused snapshot epoch is still the host epoch. After `revoke`, a snapshot taken in the gap has that new epoch. A refuse in that gap can revoke the epoch the command just reserved. A forbids that for a retired source. Sketch lines 159-163 say failure cleanup applies only to an exact captured record that is still live, and `candidate.md` line 38 says retired and unknown summary scopes supply no cleanup authority. The scope's captured lifetime epoch must not be the cleanup key. The source origin's liveness is the key. + +Required graft on A. Bump the same ledger frontier synchronously at the start of command `execute` and `synthetic`, before the awaited `session.get`. Do it again at `register` only if the generation would otherwise stay still. An interrupt revokes and does not take that path, so retired ordinary manual summary still proceeds. A compaction that enters during the gap sees a frontier the consumed prefix does not cover (A sketch lines 86-88) and refuses without `revoke`. + +## Trace 2. A new unconsumed same-epoch private admission while the native read is pending + +Command `revoke` between the two observations is visible without listing admissions. The host epoch changes. A's `assertCurrent` checks lifetime (sketch lines 90-91). B's step 1 refuses when `hostEpoch` disagrees (sketch lines 123-125). That refusal must use the pre-command epoch for any cleanup decision. The post-revoke epoch belongs to the command. + +Synthetic registration does not change the host epoch. It only inserts into `#admissions`. + +A can see that insert if `register` bumps one generation on the same ledger and the scope captured the old generation before the native read. After the read, `assertCurrent` fails closed. Sketch lines 142-144 require that post-await check. This is one writer. It does not need an exported id list. A already changes `execution-attribution.ts`, so this does not contradict its module list. + +B cannot see that insert. The compaction hook does not know the new id, so `hasAdmission(id)` cannot be asked. The native prefix can still equal `consumed` because the synthetic prompt has not landed. Step 1 then sees equal epochs, equal `realCandidateIDs`, and equal readings for the old ids. Step 6 returns `continue` and calls `compactedInputOrigin`, which allocates a new checkpoint. The in-flight synthetic is ignored. Building `admissionIDs` in `lifecycle.ts` would be a second list kept in step with the private map. B's `candidate.md` lines 85 and 125 say `execution-attribution.ts` stays unchanged and no second historical collection is added. Those two sentences leave no legal source for `admissionIDs`. + +## Trace 3. The scope is waiting on `admission.ready`, and epoch, frontier, or unload changes, while `ready` never settles + +Today the compaction hook awaits that promise directly (`lifecycle.ts` lines 724-726). Nothing in `revoke` settles it. The hook stays there. + +A makes the same coupling explicit. `InputScope.settled` is one `Promise` (sketch lines 71-72). The comment at line 89 says it awaits only that record's `ready`. `admit` awaits it and only then calls `assertCurrent` (sketch lines 142-144). While `ready` is pending, the frontier check does not run. `finally` does not run. `#pending` keeps the operation slot (sketch lines 133-140). A later compaction is rejected and does not revoke the holder. The fresh primary stays. The fresh compaction does not. Sketch lines 190-192 say a stale scope neither occupies nor clears a replacement slot. That is true only after `settled` fulfills and `finally` runs. It is false for this trace. + +Unloading is the exception already in the source. `clear` rejects pending `ready`. A waiter of that same promise wakes. Epoch and frontier changes do not call `fail`. + +Rejecting `ready` itself to wake the scope also rejects the admission. If that admission is the fresh command, the wake destroys it. The scope wait has to be a different promise from `admission.ready`. + +B describes the right split and does not provide it. `waitForSettledOrigin` (sketch lines 182-188) takes `stillValid: () => boolean`. A predicate does not fire when the epoch changes. The comment says an epoch change, unload, stream loss, or new admission rejects the wait and does not revoke a newer primary. The function can observe `stillValid` only by polling or by `ready` settling. Polling is not in the sketch. Unchanged `execution-attribution.ts` still leaves `ready` pending across `revoke`. The advertised rejection does not happen. + +Graft on A. The ledger that bumps the frontier also rejects the scope promise when that scope's lifetime, frontier, or open bit no longer matches. Leave `admission.ready` pending unless that admission itself is the stale one. `sourceFailure` stays a no-op when the captured source is not live. Do not call `revoke`. + +## Partial activation + +Current context treats a pending `inputOrigin` as absent, then `owns` reads `captureAuthority`. The lease is capturable. `messageId` is null. `isAuthorityCurrent(authority, source)` is false. Context throws before the native read. + +A's primary callsite (sketch lines 12-21) captures a scope, waits for `scope.settled`, and only then resolves a positive origin. `capture` does not consult `captureAuthority` (sketch lines 81-83). `candidate.md` lines 32-33 and 38 delete the current-host authority fallback for pending ownership. After `ready` resolves, the existing live-origin checks run. No public `messageId` read is required. `auto-drive.ts` can stay frozen. This matches the component pause. `freeze` has not run while `activate` is held. The hang in trace 3 is what remains if `observeAccepted` never settles `ready`. + +B's `contextAdmission` (sketch lines 163-172) waits only when the reading is pending, `captureAuthority` already returns a lease, and `messageBound` is false. The usage in `candidate.md` lines 46-55 sets `messageBound` from `lease.messageId === sourceID`. That field is private. `isAuthorityCurrent(authority, sourceID)` cannot stand in for it, because a lease bound to a different message is also false. B says `auto-drive.ts` is unchanged (`candidate.md` line 85). The wait input is then unavailable unless the shell leaks the lease. The pre-receipt path is intentionally left on today's refusal (`candidate.md` lines 97 and 163-169). The proved window is the one that needs the private field. + +## Repeat proof and the same checkpoint object + +The behavior the current tests exercise is a second context entry after one compaction. `freeze` keeps the primary when `prior.origin === origin`. That origin is the WeakMap value stored on the first compaction. + +A's `completed` (sketch lines 167-175) looks up the stored record by control id and returns the existing `#origins` object. It says never to reconstruct it. The WeakMap is keyed by the `CheckedContinuation` object (sketch lines 131-132). A second `admit` that allocates a new record and overwrites `#records` orphans that key. "Exactly one" checkpoint origin (sketch lines 155-157) is the right rule only if `admit` returns the stored record when host, control id, and consumed ids match, and does not call `compactedInputOrigin` again. The sketch does not write that lookup. An overwrite compiles against the types. + +B writes the reuse rule and writes it on the wrong value. `proveCompaction` step 6 always sets `checkpointOrigin` from `compactedInputOrigin` (sketch lines 137-140). That helper returns a new object on every call. `applyCompactionDecision` keeps a stored continuation only when the origin object is the same object (sketch lines 117-120 and 211-212). The new allocation is not the stored object, so the second proof replaces the WeakMap entry. Permit reuse by control id and consumed ids is specified. Continuation reuse by control id is not. A repeat proof therefore changes the object identity that response-less retry compares. + +## Where a history fact stops being a live `InputOrigin` + +A retained admission after `revoke` is still an `InputOrigin`-shaped object in `#admissions`. Its epoch is stale. `inputOrigin` returns that object today. It is not positive execution authority. `assertLive` fails it. + +A's public evidence for that row is `retired-ordinary` or `retired-automatic` (sketch lines 59-64). The comment at lines 93-94 derives automatic from a non-null historical authority even when `automatic` is false, and it forbids turning an arbitrary throw into `retired-ordinary`. A summary record carries no origin (sketch lines 157-158). `completed` throws for summary-only (sketch lines 170-171). A later real input resolves through its own accepted record. The current host epoch is never copied onto a new origin for this row. + +B's `readSource` drops the object on the way out (sketch lines 65-72). `dead-ordinary` and `dead-automatic` are tags. `ManualSummaryPermit` has no origin and no epoch (sketch lines 91-96). That is the right shape for the reading. The stored admission still has the old origin, and B leaves that store in place (`candidate.md` line 125). Any caller that keeps using `inputOrigin` bypasses the tag. The fence is the reading, not the record. + +B's `OperationSnapshot` still carries `hostEpoch` as a number (sketch lines 75-80). The opaque `SnapshotToken` is a second field on that same object. Step 6 copies the epoch from the live origin, which is the right copy. The shell still receives the current host epoch on every proof. A later edit can pass it to `makeOrigin`. A's summary type has nowhere to put it. + +Interrupted ordinary manual, traced on the written steps: + +1. The ordinary origin stays stored. Interrupt has moved the host epoch. No new input id exists. +2. A reads `retired-ordinary`. Manual, no live guidance requirement for an ordinary host, no active primary. Result is summary-only. No `makeOrigin`, no recovery observation, no guidance append. The current push at `lifecycle.ts` lines 715-716 has to move inside the continuation branch. A says that (`candidate.md` line 34, sketch lines 158-159). +3. B reads `dead-ordinary`. Step 4 returns summary-only and does not call `compactedInputOrigin`. `apply` writes a permit and skips `captureOwner`, `failOwned`, recovery, and `observeCompaction` (sketch lines 206-209). +4. Later context for the completed control finds no continuation origin. The checkpoint tool fails. A later accepted real input uses its own record. + +That is criterion 1 for both. It is not a reason to pick B. + +The neighbor row is the blocker. Live ordinary manual, authority null, reading `live`. Step 4 requires every reading to be absent or `dead-ordinary`. Step 6 requires reason `auto`. Step 5's "non-null authority" clause does not name this row. Step 7 refuses every other shape (sketch lines 131-143). There is no `continue` for manual plus a live null authority. The same hole refuses grantless `flow-plan` and `flow-reviewer` manual continuation. The matrix at `candidate.md` lines 174 and 185 says those rows continue. The steps are the contract. The matrix is a prose guarantee. + +Active primary overlap has the same split. The matrix says the existing refusal remains. Steps 1 through 7 do not read `primaryIdentity` except to see whether pre and post changed. An unchanged live primary passes step 1. A's admit comment refuses that overlap (sketch lines 151-153). + +## Red flags + +A puts source facts and the frontier generation on one ledger, and parsing plus the outcome record on one compaction boundary. `admit` and `completed` are the caller interface. `PrimaryHost` is marked as a sketch of existing lifecycle behavior (`candidate.md` lines 15-16). Implementing it as a real class would be a pass-through. The historical projection repeats `id` and `delivery` so that a retired row is not an `InputOrigin`. That repetition is the fence. Two indexes, id and frontier generation, belong to one writer. I do not see a second authority cache. + +B's five functions hide the policy only if the snapshot can be built. It cannot, under the unchanged modules B named. `admissionIDs` plus a private map is a hand-synced list. `messageBound` is a lease leak. `dead-automatic` drops the authority token and step 5 then asks whether that token was non-null. The tag can carry the classification only if `readSource` is the sole classifier. The shell still has `SourceFacts.record`, which still holds the origin (sketch lines 51-62). + +Both sketches delete `isUnattributedManualSummary` and the `inputEpoch === undefined` branch. That deletion is right. The unknown-history branch is a missing record, not a revoked ordinary record. + +## Verification + +A's plan names the unchanged packed smoke, the interrupted ordinary sequence with its reload control, a copied census, the pending barrier, and the existing lifecycle cases. It says native overlap, auxiliary retry, and the missed transport stimuli are unproved. It says this candidate ran no suite. I agree with that limit. I did not run those checks either. The parent rerun of the census asserts the current failure. It is not acceptance. + +B names the census row flip, the pending test, the stale lifecycle cases, and the smoke's `completed` status. It also says it did not run them. The plan is thinner on the packed smoke's full assertion count and on keeping the original census bytes. More importantly, a suite that expects live ordinary manual continuation will fail if the steps are implemented, and a suite that follows the matrix will not test the steps. + +## Grafts and rejected ideas + +Keep A's ledger, typed summary record, and continuation record. Graft only these. + +1. Synchronous frontier bump at the start of command `execute` and `synthetic`, before `session.get`, on the same generation `register` bumps. Interrupt must not take that path. +2. A scope promise distinct from `admission.ready`. The ledger rejects the scope promise when lifetime, frontier, or open state changes. It does not reject the fresh admission, and it does not call `revoke`. This is the part of B's `stillValid` comment worth keeping. The pull predicate itself is not. +3. `admit` returns the stored continuation record and the stored origin object when host, control id, and consumed ids match. It does not call `compactedInputOrigin` again. B's permit rule by control id and consumed ids is the model. B's comparison against the newly allocated origin is the part to leave behind. + +Reject these. + +- A universal manual bypass. It drops live ordinary and grantless continuation, then needs a second bridge to get them back. A already rejected this. +- Deleting the ordinary admission inside `revoke` so the unknown-history branch returns. A late callback and a real unknown summary become the same missing record. B already rejected this. +- `if (authority === null) skip assertOwned` inside the current hook. The hook would still build a checkpoint whenever a record exists, and pending ownership would still read the partial lease. B already rejected this. +- B's step 7 as the permission table. It refuses live null-authority manual continuation. +- An `admissionIDs` array or a readings map maintained beside `#admissions`. +- Reading `lease.messageId` from outside `AutoDrive`, or treating `isAuthorityCurrent(authority, source) === false` as proof that `messageId` is null. +- Using the current host epoch as the epoch of a new origin for retired, unknown, or summary rows. + +## Convergence + +The sketches agree on one split. A manual summary of history that is not a live positive origin stores no `InputOrigin` and cannot satisfy a checkpoint tool. A live accepted source keeps the existing continuation and the existing epoch. Native control, prefix, and reason are read before that choice. The current `assertOwned` before `session.context` goes away. + +They do not agree on a shape. A moves the knowledge to the module that already performs `register`. B leaves that module unchanged and then reads facts it does not export. Averaging them would put a pure function on top of a second registry. That is the hand-synced list the red-flag screen rejects. + +## Principles that changed this judgment + +Model the domain made A the base. The outcome is a sum of live origin, retired ordinary, retired automatic, pending, rejected, and unknown. B's steps collapse live null-authority manual into the final refuse. + +Type system discipline kept A's summary record, which cannot carry an origin or an epoch. B's permit agrees. B's public `hostEpoch` on the snapshot undoes part of that fence. + +Boundary discipline put the native prefix and control check on the compaction boundary after the read, and left `auto-drive.ts` frozen. That is why A's pending wait is legal and B's `messageBound` input is not. + +Attack the premise kept the census conclusion. The failed premise is that a summary requires a live owned epoch. Another condition inside `assertOwned` would share that premise. + +Laziness and subtract before you add kept the caller interface at `admit` and `completed`, and kept the grafts on the frontier A already needs. They rejected a snapshot list as extra state. + +Minimize reader load rejected `PrimaryHost` as a future production class and rejected five public functions whose inputs the shell cannot build. + +Separate before serializing shared state rejected a lifecycle copy of `#admissions`. One generation counter written by `register`, by the real-prompt path, and by the new pre-`get` reservation is one writer. + +Make operations idempotent required the stored origin object on repeat proof. B's identity check fails that test. The scope-wait graft is the same rule for a wait that must converge when `ready` never settles. + +Fix root causes separated the hung wait from the authority bug. Waking the waiter by rejecting `admission.ready` would punish the fresh admission. The root is that the scope has no promise of its own. + +Redesign from first principles kept the pre-`get` reservation on the frontier rather than adding a special case in the compaction hook. The reservation is what a command or synthetic already is, from the moment `execute` or `synthetic` begins. + +Prove it works and test behavior kept criterion 6 on named sequences and literal summary-versus-tool outcomes. I did not treat either candidate's plan as a run. B's matrix does not match B's steps, so the steps decide the score. + +Exhaust the design space is why this did not collapse into one verdict with no grafts. The two shapes were compared. Only one can observe a same-epoch private insert without a second registry. diff --git a/.agents/plans/18-opencode-v2/reviews/core-r1.md b/.agents/plans/18-opencode-v2/reviews/core-r1.md new file mode 100644 index 00000000..a39e9fb7 --- /dev/null +++ b/.agents/plans/18-opencode-v2/reviews/core-r1.md @@ -0,0 +1,113 @@ +# OpenCode v2 core review, round 1 + +Verdict: ISSUES. + +This is a read-only verdict on the working tree at review time. It is not a pack, release, or full-green claim. HEAD is `8103f1cd42e1b4d111f25d2a311935d4e41fb997` on `feat/opencode-v2`. The migration is uncommitted. Hashes below are the snapshot at 2026-10-24T08:18:16Z. + +Two current defects block a final pack. The native full-output suffix fix and the HTTP 400 release path are present in that snapshot. The initial Flow marker is not. + +## Findings + +### 1. High. A failed or cancelled primary stream wedges the next turn + +`src/platform/opencode/execution-attribution.ts` keeps one frozen primary per session. The next context is legal only when that primary's phase is `ended`. + +`src/platform/opencode/lifecycle.ts` lines 437-439 throw `Flow primary contexts or streams overlap` when the phase is anything else. The catch at lines 553-555 revokes the epoch and does not remove the primary. `ExecutionAttribution` has no per-session rollback. `clear()` closes the whole attribution map. + +Paths that leave the phase short of `ended`: + +- `execution-attribution.ts` lines 342-345. `cancel()` revokes through `failed()` and does not set `ended`. A real steer that cancels the in-flight body takes this path. The epoch changes. The old primary stays `streaming`. The steered context then hits the overlap throw, revokes again, and every later primary context for that session throws the same error until the plugin process restarts. +- Lines 337-339. A thrown `reader.read()` does the same. Phase stays `streaming`. +- Lines 321-324. An ok response with no body revokes and throws. Phase stays `request`. +- `lifecycle.ts` lines 532-534, then 553-555. `freeze()` stores the primary in phase `context`. `session.get` is the next await. If that call throws, or if `assertLive` fails after the freeze, the catch revokes and the primary stays `context`. + +The HTTP 400 path is a different branch. Lines 315-318 set `ended`, call `failed()`, and return the original `Response`. `tests/execution-attribution.test.ts` asserts that the provider body, status, and `x-fixture-error` header stay intact, and that a later freeze is accepted. This review did not re-run that test. No test in `tests/lifecycle.test.ts` or `tests/execution-attribution.test.ts` cancels the wrapped body and then freezes the next input. + +Minimal change: on cancel, pull failure, a missing body, and a context-hook failure after freeze, set that primary to `ended` or delete it before revoking. Add one test that cancels the wrapped stream and then admits the next real input. + +### 2. High. The initial `/flow-auto` user message is persisted without its Flow token + +`src/platform/opencode/lifecycle.ts` lines 334-355 build `promptPayload()` with text and attachments only, then call `ctx.session.prompt`. `command-hook.ts` lines 137-142 call `autoDrive.activate` and return the token only inside `attribution.accept`, after that receipt. `lifecycle.ts` lines 171-174 then pass the token to `autoDrive.observeMessage` as an in-memory part with `synthetic: true`. Nothing writes the token onto the native user message. + +The lifecycle fixture stores that user message as text and id only (`tests/lifecycle.test.ts` lines 163-168). Synthetic dispatches do copy metadata onto the native record. The initial command does not. + +`evals/reviewer-access.ts` lines 549-557 reject a v2 observed trace when the first root user message has `flowTokenSha256 === null`. That digest comes from native message metadata. The gate is strict, and this snapshot cannot satisfy it for an initial `/flow-auto` command. In-process continuation can still run, because the lease sees the memory-only part. A released native transcript of that command has no initial marker. + +No preallocated admission marker is in the hashed `lifecycle.ts`. This review does not judge a patch that is not in the tree. Do not weaken `checkAutonomousLineage`. + +Minimal change: put the private token in the admitted user payload before `session.prompt`, and activate only when the receipt matches that payload. + +## Checked, and not findings + +Request-scoped freeze, the admission registry, immutable assistant and tool bindings, and the live epoch are in `execution-attribution.ts` and `lifecycle.ts`. The prompt hook revokes and records a candidate. It does not grant an origin. Grant happens in the context hook after the native message matches the candidate or an accepted admission. Compaction continuity in `auto-compaction.ts` requires the same running, then completed, auto control id. Summary text is not the continuity key. + +`tool-guard.ts` allows `flow_feature_complete` for a `flow-reviewer` child with a parent id and a matching workspace. It does not require a particular manager id. `tools.ts` sends every other agent to `featureCompleteReplay`. `flow-service.ts` lines 755-764 refuse a new completion when no exact accepted replay exists. + +`config-shared.ts` denies `*` for the planner, reviewer, and worker, then allows read, glob, and grep. The reviewer also allows `flow_status` and `flow_feature_complete`. The worker also allows `edit`, with denials for `.flow` and `.git`. This pass did not execute the host rule matcher. + +`validation-capture.ts` lines 290-292 accept a spilled file that ends in the display tail or that tail plus one LF. Extra LF, changed whitespace, and a changed suffix stay incomplete in `tests/validation-capture.test.ts` lines 604-644. Unknown exit or unknown truncation stays ineligible. The local receipts at `/tmp/flow-v2-core-shell-proof/baseline-run/verification.json` and `fixed-run/verification.json` both describe a 400000-byte native file with digest `sha256:90bf37cbb6f8580766aceb9b7973ffd59e1441599574ec68d4983ba9314f7112`. The baseline observation has `outputComplete: false`. The fixed observation has `outputComplete: true` and the same digest. This pass did not recompute that digest from the output file and did not call the host. + +`package.json` externalizes `@opencode/plugin`, `@opencode/schema`, `@opencode/client`, `zod`, and `effect` for the plugin and TUI builds. `tests/architecture-boundaries.test.ts` rejects `@opencode-ai` imports and confines `@opencode/` imports to `src/platform/opencode/`, `src/tui.ts`, and `src/flow-rpc.ts`. `schema-adapter.ts` is deleted. Model preference writes refuse a stale revision, a stale project, a busy session, and an unavailable model before commit (`model-preferences.ts` lines 199-244). + +## Limitations + +Parent-reported suite figures were not run here. They are not evidence in this verdict. That includes 4398 passed, 25 skipped, 13 failed, 7 distribution fixes, 43 native event reads, 5 remaining fixture updates, and 1 repro drift. This pass does not classify those failures. + +No host, model, or paid call. No Linux CI, packed install, or TUI visual proof. CUA Terminal was already unavailable. The debug tarball was not reviewed as a release artifact. + +Eval tooling tests were not reviewed as completed work. `evals/reviewer-access.ts` was read only for the lineage predicate. + +Agents were forbidden, so there is no second critic lane and no cross-model review of this trail. Comment style was out of scope. Permission match order inside the host was not executed. The core owner can still edit these files. Re-hash before pack. + +## Reviewed file hashes + +SHA-256 at 2026-10-24T08:18:16Z. + +- `974cddff8a802d5a27ec64f0fb2f8bc342dfeb778a0aa63b82d71aaeb447fc4d` `src/platform/opencode/execution-attribution.ts` +- `1e8d5991dcaa756ef19484a6219dcca980551f201d21676c63534019af463772` `src/platform/opencode/lifecycle.ts` +- `a78dbdda8e3e802550149ab6d2bcced886db35fe92e1779d5d20da25d99aef1c` `src/platform/opencode/auto-compaction.ts` +- `9781d3db22d7e8f7451253a19e7d23647db8cadbefbe4818beb1e264c6b7532d` `src/platform/opencode/auto-drive.ts` +- `262002ccf54a74d2a1b2106bde420d96ba279fb7b00d449d95350acca5fc29c3` `src/platform/opencode/auto-drive-messages.ts` +- `de2b4943fb3dd5d9381a3ee621a231f5f0883005c0fffff33e10ef04d69f54d7` `src/platform/opencode/auto-drive-decision.ts` +- `c3bc9296d2adfd5d77f87f9f882eb0f6a423e184fb8c9cc6976a2868586d76ca` `src/platform/opencode/auto-drive-validation.ts` +- `3b05232a9aa772017d845d499b0196b75d02bce35dc344931f4d431c2d7f1e6c` `src/platform/opencode/command-hook.ts` +- `28fcb3d89334e2e7701717e4cff16d888bff5646c7ea53dd19b1bde0564dde53` `src/platform/opencode/plugin-composition.ts` +- `061b163fe0b5ae036643133736fa985e9aac092d86059dbaf12d81c728d23cb2` `src/platform/opencode/plugin.ts` +- `f341b0bed05839cc802d88ee6204f37dadb69bb86d8d4e95c616cf3970ef1b8c` `src/platform/opencode/tool-guard.ts` +- `dd131d591208253296c4bda2bb243175d3470816f8096c4dde80c79328ecb10c` `src/platform/opencode/tools.ts` +- `314b357ee8a70e7c2c0c5dfb291f5a9e5aeb68c290f13e00ea2e058d6d835216` `src/platform/opencode/flow-tool.ts` +- `b0a48f9cdadb95c681a2161a62ec2b4b79b3f932aef1e73b7c394147f97d94aa` `src/platform/opencode/validation-capture.ts` +- `e9f45a4875e8ca936eb21f56cdd6764b7605503d9ecaa367ba13f0cf649b1183` `src/platform/opencode/config.ts` +- `e2496fd5be9db88f71e7c98fca441389665f6f986ba25b9b60d6c02c51db0179` `src/platform/opencode/sdk.ts` +- `461df3b882ca52fa53321e09629d37483f21ede7cd36d3915da6022bd76aae41` `src/platform/opencode/logging.ts` +- `522c5e89a7eb5e0cd20cc19022013f78515939795caffd9fbf871954a005e349` `src/platform/opencode/model-picker.ts` +- `99ecac2cda70f4c26735c7d878d2ad87da7063300ebc67b68e32ea24e38d392e` `src/platform/opencode/model-picker-ui.ts` +- `da47b4eada8f1765024c6956c1a7dd75e7ec0c7423ee1854260c3def4bd2d9d3` `src/platform/opencode/model-preferences.ts` +- `8fa8c7afd7db329f8de34c79889b95e8e600c67d48f849b16eea4f3106c14127` `src/config-shared.ts` +- `c8e9ba09c18220ccdc88288eb2c667caee1580980d695be45af436f95dd944e8` `src/tui.ts` +- `d2e04b71f1244ef9d1ef2859275051596ad79d309de5c4dab9e93e311952c577` `src/flow-rpc.ts` +- `efe1145e953fed122f0811a417bb9b5ed9dfc144b262dd28f750a883981da048` `src/application/model-preferences.ts` +- `3c68f1b8f340e0070e46033ccd7ef8a5610cef6ad1e4f23cd4c4572c365a83b8` `src/application/model-preferences-schema.ts` +- `f616bd10908d49c55e44c3bcfc3315d675cdac8164c65b775f7869e138541b75` `src/application/recovery-policy.ts` +- `ef044a759189b1ab96684b4aed17a3b556480cc5a96bb7dfa38a27efb0d28873` `src/infrastructure/fs/model-preference-store.ts` +- `4277203d63e1bc47a22d93765a9b82c87f4eaa42bb307c89c60fee46e64e6034` `src/prompt-surfaces.ts` +- `09f86fd952364b8d056913bb49775c11901faef0371c42a18b88be660cf3a182` `evals/reviewer-access.ts` +- `b314caed263dda5a8f96817eae8bfa005a89cd313e08248e68bc061a6cfda809` `tests/execution-attribution.test.ts` +- `f08cf080ea35d6a1698dd60f106d8780f90c56639c7b3ec180675ae6dd893b98` `tests/lifecycle.test.ts` +- `d333ee81e19df1c14e0a5cee2bb469c227470ef31a6ea395513be2b7188f2ce5` `tests/validation-capture.test.ts` +- `3412d7b7c9317c7cb0c454cf2d7d1060847683a2a8ed74c01452cf42893e9de0` `tests/architecture-boundaries.test.ts` +- `21435fe77bc56a1dbb005c500009cf8f96a7311e043d37d8c616d3c91bf07cab` `tests/opencode-native-core.test.ts` +- `ed69c3a6e20ff9e14f29b3553698c7aefa9dca6682b12237f1cfe891418a282d` `tests/model-preferences.test.ts` +- `3434cd5c091dc07eafb93671e41d61ad7c34cfa45f601c771a8eab44166d2d8c` `tests/model-preferences-store.test.ts` +- `c9ef62217a90bb1acac70304c9df24fc8be9d7840ad758f04f71bb457eaa5e3d` `tests/model-picker.test.ts` +- `164aba56578d43c740f4ca92d89dc0cfbd2d9cd18cb324a7f96f9cccb3815f39` `tests/auto-drive.test.ts` +- `288d4e0b49698ce5ff0ea9ddf1e7e9894a1ab99bf60ee5d5bbc53f9fd0df59b6` `tests/tool-parentage.test.ts` +- `72b1afd511af1708a764ed3dcb7284a70ee7bc5bda8d5bc56cce02aa17d61cb1` `tests/config-shared.test.ts` +- `57d2b03629a188d2f9f8eabc4c0493ba173bbbd550fbb97acaa4bc7ad5540b81` `/tmp/flow-v2-core-shell-proof/baseline-run/verification.json` +- `c7bfef94b0552d35a68345d3628a36ba6bb0f3ea18417be790c3b9ed7d51b994` `/tmp/flow-v2-core-shell-proof/fixed-run/verification.json` + +## Decision trail + +The log is `/tmp/flow-v2-core-review-r1/decisions.tsv`. This run wrote every row. Each evidence path was read or hashed in this session. No row claims a suite run. Cross-model review of the trail was not available. + +Principles that changed a choice. Prove it works kept the verdict on the hashed source and the two shell JSON files. Explain the number kept the parent suite counts out of the verdict. Model the domain made the wedge recommendation a terminal phase, not another revoke. Boundary discipline made the marker recommendation part of the admitted payload. Test behavior separated the HTTP 400 unit test, which states its observable response, from the missing cancel-then-next-context test. Outcome-oriented execution left the unlanded marker patch unreviewed. diff --git a/.agents/plans/18-opencode-v2/reviews/core-r2.md b/.agents/plans/18-opencode-v2/reviews/core-r2.md new file mode 100644 index 00000000..61eca85e --- /dev/null +++ b/.agents/plans/18-opencode-v2/reviews/core-r2.md @@ -0,0 +1,104 @@ +# OpenCode v2 core review, round 2 + +Verdict: ISSUES. + +One current P2 defect remains. A primary request that fails before the response hook causes the next valid turn to fail. The specific response-stream and context-setup failures named in R1 are fixed. The initial native marker is present. The R1 marker verdict also contradicts its own recorded source identity, as detailed below. + +This is an independent review of the working tree on `feat/opencode-v2`, with HEAD and base both `8103f1cd42e1b4d111f25d2a311935d4e41fb997`. The migration remains uncommitted. No production files, Git state, or other owners' artifacts were changed. All review writes are under `/tmp/flow-v2-core-review-r2`. + +## Finding + +### P2. A request failure before any response rejects the next valid turn + +Primary location: `src/platform/opencode/lifecycle.ts:659-661`. +Related locations: `src/platform/opencode/execution-attribution.ts:294-297`, `src/platform/opencode/lifecycle.ts:441-442`, and `src/platform/opencode/lifecycle.ts:556-560`. + +After the HTTP request hook succeeds, the frozen primary has phase `request`. If the provider connection fails before a Response exists, `ExecutionAttribution.response()` never runs. The lifecycle's `session.execution.failed` handler calls `revoke()` but never ends that primary. The same omission applies to an interruption before a Response exists. + +The next real input or `/flow-auto` command can be accepted and activate its new lease. Its context hook then encounters the old primary in phase `request` and throws `Flow primary contexts or streams overlap`. The catch ends the old primary and revokes the newly activated lease. The user must submit another fresh input to recover. This affects ordinary primary turns as well because the context and HTTP hooks are registered for all sessions. + +The offline reproduction uses the existing in-memory lifecycle fixture and unchanged production `registerFlowLifecycle`. It emits context, then HTTP request, then a native-shaped execution-failed event without a response. It accepts a fresh command and invokes context. The first fresh context throws the overlap error and its authority is null. A second fresh command's context succeeds. The script makes zero network requests. + +Reproduction command: + +```sh +/tmp/flow-v2-toolchain/node_modules/.bin/bun /tmp/flow-v2-core-review-r2/pre-response-failure.ts +``` + +Evidence is [pre-response-failure.ts](pre-response-failure.ts) and [pre-response-failure.json](pre-response-failure.json). This proves the production lifecycle state transition offline. It is not a newly executed native-host connection-failure proof. + +Minimal recommendation: cover request failures and aborts that never produce a response. Terminalize the affected primary before a fresh context is admitted, while preserving exact-primary and epoch protection against a delayed old failure ending a replacement primary. Add regressions for a rejected connection and an interruption before response arrival, each followed by one successful fresh input. Do not rely on the next context's overlap catch to perform cleanup. + +## R1 disposition + +1. The listed cancel, read-error, missing-body, locked-body, stale-response, and context-setup paths are closed. `ExecutionAttribution.end()` marks the exact primary terminal. The failure callback revokes only when that primary is still current. Context failure cleanup ends its owned primary. The focused regressions include late cancellation rejection and a duplicate old response after a replacement exists. The pre-response hole above is a remaining terminal path, not the original permanent wedge. +2. The initial marker issue is closed for the current source. `lifecycle.ts:334-345` allocates the token and includes it in the native user payload before `session.prompt`. Receipt ID, type, and payload are checked before activation. `command-hook.ts:138-142` activates with that same token. Pending or mismatched receipts do not grant execution authority. + +R1 provenance needs a correction. Its `decisions.tsv` scope row at line 3 says the marker patch was unlanded, and its verdict row at line 6 says the missing marker is in the hashed source. Both point to lifecycle hash `1e8d5991dcaa756ef19484a6219dcca980551f201d21676c63534019af463772`. The retained historical marker entry's sourcemap contains source bytes with exactly that hash and already contains token allocation and `metadata: { [FLOW_AUTO_METADATA_KEY]: invocationToken }` at lines 334-344. I recomputed the hash from `sourcesContent`. Therefore the recorded R1 identity does not support its missing-marker claim. I cannot reconstruct a different unhashed source state the R1 reviewer might have seen. + +The copied R1 log is [receipts/08-decisions.tsv](receipts/08-decisions.tsv). Historical receipt identity and current checks are in [evidence-checks.json](evidence-checks.json). Preserve the old trail and add a superseding correction in the parent's audit. I did not edit R1 artifacts. + +## Checked without another finding + +The production model is an admission registry, a frozen primary per native session, immutable assistant and call bindings, and a current epoch. Prompt attempts revoke. They do not grant an origin. Context establishes positive authority only from accepted local admission or a matching native real-input candidate. Stale origins are checked before HTTP and before native tools carrying automatic authority. Flow tools are checked again after permission and identity awaits. No event-prefix positive authority or summary-text authority was introduced. + +Automatic compaction continuity uses the exact native running automatic control ID, then requires that same completed ID and live authority at resume. Manual and mismatched controls cannot retain the grant. Reviewer completion permits any verified `flow-reviewer` child in the matching workspace. It does not require a specific assignment-manager parent. Other permitted callers use exact accepted replay. + +The HTTP helper at `http-transport.ts:6-16` updates every enumerated provider, model, and variant's settings. It preserves other settings. Composition registers it before agents, lifecycle hooks, and tools, and retains disposal in its cleanup list. The provider editor exposes inactive providers as well. A provider or transport override registered later is not silently attributed. The primary WebSocket handshake still throws and revokes. The local transport receipt shows bare and no-op-hook cases using WebSocket, and the forced case using HTTP even with the WebSocket variant selected. This is one local OpenAI Responses fixture, not official-provider qualification or a guarantee about every third-party transport. + +Reserved roles use deny-all followed by explicit read permissions. The worker adds edit with `.flow` and `.git` denials, including absolute-path patterns. The reviewer adds only the two intended Flow actions. Native tools explicitly assert their permission action. I did not run the host permission matcher in this review. + +Validation capture joins session, assistant, call, role, workspace, exact command, exit, and completeness. Unknown exit or completeness stays ineligible. Spilled output requires a native exited shell record and a matching output suffix, with at most one native trailing LF. The safe reader checks its host output directory and file shape. JUnit evidence must be fresh. Persistence recomputes source identity and marks source drift ineligible. I independently read and hashed the retained 400000-byte shell output. It matches the receipt's `sha256:90bf37cbb6f8580766aceb9b7973ffd59e1441599574ec68d4983ba9314f7112`. + +Preference persistence uses a shared atomic store and stale-revision checks. Server writes recheck project options, model availability, activity, abort, and unload before commit. The picker preserves independent roles, project precedence, reset behavior, and configured variant restoration. Picker choices clear the base variant. The independent-server receipt explicitly limits shared activity detection to the shared native store. It does not establish an atomic barrier against a prompt beginning in another process after the scan. + +The server and TUI builds externalize the native packages and Effect. Runtime native imports stay at host adapters, the TUI entry, and the RPC contract. The native plugin package supplies its client and Effect dependencies. V1 schema bridging is deleted. Final isolated install and platform qualification remain release gates. + +## Verification and artifact identity + +I ran the following with Bun 1.4.0 and an owned temporary directory: + +```sh +PATH="/tmp/flow-v2-toolchain/node_modules/.bin:$PATH" TMPDIR=/tmp/flow-v2-core-review-r2/tmp bun test tests/execution-attribution.test.ts tests/lifecycle.test.ts tests/opencode-native-core.test.ts tests/auto-drive.test.ts tests/validation-capture.test.ts tests/tool-parentage.test.ts tests/config-shared.test.ts tests/model-preferences.test.ts tests/model-preferences-store.test.ts tests/model-picker.test.ts +``` + +Result: 233 passed, zero failed, across those ten files. [focused-tests.log](focused-tests.log) contains the output. This is focused unit and component evidence. The separate reproduction above covers the missing terminal case. No full-suite, typecheck, lint, Linux CI, or qualification result is claimed from this run. + +While I reviewed, the continuation owner replaced the top-level native marker and stream receipts with newer packed-candidate receipts. I saved the retained versions in [receipts](receipts) and recorded their SHA-256 values in [receipt-hashes.json](receipt-hashes.json). The retained stream receipt reports 22 named assertions. The marker receipt reports zero lineage issues for the original native user message and two synthetics. These are receipt results, not host runs performed by this reviewer. + +For both updated receipts I independently verified the archive SHA-256, installed entry bytes, loaded entry hash, source map hash, archive entry and map bytes, and every mapped production source against the working tree. All comparisons passed. The candidate archive SHA-256 is `224f3c78b5376a6178356fd7e37fb31420b31c147f4b1db1617ab582e0dd843e`. Its loaded entry SHA-256 is `486ed90cabb81dcd2ff59c7dd5785b0f4226fe3d876edea59c124d78c9f01c63`. See [evidence-checks.json](evidence-checks.json). This binds those specific probes to the reviewed source. It does not make the archive a final release artifact, and it does not cover the finding above. + +No source or initially inventoried test hash changed between the start and end of this review. [initial-hashes.json](initial-hashes.json) and [final-hashes.json](final-hashes.json) record the comparison. Any subsequent production fix requires a fresh independent round and artifact checks. + +## Scope and limitations + +The review inspected every changed or added production source under `src/`, the deleted adapter, package build boundaries, and relevant core and picker tests. Additional surrounding sources were read for completion replay, source binding, and recovery invalidation. [reviewed-files.sha256](reviewed-files.sha256) records 49 reviewed file identities, including a deletion marker for `schema-adapter.ts`. + +Main eval tooling remains another owner's work. It was not reviewed as completed implementation. Its exported lineage predicate is supported here only by the retained marker receipt. Historical parent suite counts were not adopted as my measurements. + +No native host, provider server, model, paid call, network call, browser, connector, delegate, goal, or automation was launched. Preference tests use ordinary local Bun subprocesses. No Git writes occurred. No new terminal visual proof was attempted. The reported Terminal denial, visual gate, official-provider proof, final packed gates, Linux CI, paid qualification, and release approval remain outside this source verdict. Comment style was excluded as instructed. + +## Decision trail and principles + +[decisions.tsv](decisions.tsv) is this run's canonical trail. I audited its rows against the current conversation, actual tool outputs, source, and retained files. The prior R1 trail was independently checked against its cited source identity. No supported history lookup was needed beyond the supplied task digest and live files. No claim of independent review of my own trail is made because this task forbids another agent. + +Model the Domain changed the review from a list of error handlers to a check of terminal transitions for `context`, `request`, and `streaming`. That exposed the response-less request path. + +Boundary Discipline kept the marker check on accepted native payloads and the transport review on provider settings and HTTP or WebSocket hooks. + +Prove It Works changed the evidence check to actual archive, entry, map, compiled source, and shell bytes. It also required the offline reproduction before retaining the P2 finding. + +Test Behavior, Not Implementation kept the reproduction on accepting fresh input and observing its failed context and lost lease. It did not assert private calls. + +Explain the Number kept parent-reported suite totals out of this verdict. The 233 result names the exact focused files and saved output. + +Outcome-Oriented Execution kept this review on the v2-only target. It did not ask for a temporary V1 path or treat intermediate progress as release completion. + +## Attention + +reviewed by gpt-6.1-sol + +- Fix the pre-response terminal path before source freeze. The existing native stream assertions start with a Response and do not exercise that path. +- Supersede R1 decision rows 3 and 6 for the marker's recorded source identity. The retained historical source bytes contradict that part of the original verdict. +- R1's Grok 4.7 identity is parent-reported. This run independently audited its trail, but launched no additional critic and makes no new multi-model delegation claim. +- Retained runtime receipts prove their named local probes. Visual, official-provider, Linux, full-suite, and final release gates remain separate. diff --git a/.agents/plans/18-opencode-v2/reviews/core-r3.md b/.agents/plans/18-opencode-v2/reviews/core-r3.md new file mode 100644 index 00000000..508a0345 --- /dev/null +++ b/.agents/plans/18-opencode-v2/reviews/core-r3.md @@ -0,0 +1,77 @@ +# OpenCode v2 core review, round 3 + +Verdict: **ISSUES**. + +One new P2 defect remains. A response-less native retry after automatic compaction rejects its legitimate reused assistant and revokes automatic continuation. The original R1 stream/setup paths and R2 first-fresh-input failure are closed. The initial marker is present. This is a bounded production source verdict, not release approval. + +The review covers the uncommitted working tree on `feat/opencode-v2`, with HEAD and base `8103f1cd42e1b4d111f25d2a311935d4e41fb997`. All writes by this reviewer are under `/tmp/flow-v2-core-review-r3`. No source or test drift was detected during this run. + +## Finding + +### P2. Preserve the admitted compaction origin across a proven native retry + +Primary location: `src/platform/opencode/lifecycle.ts:473-479`. +Related locations: `src/platform/opencode/execution-attribution.ts:277-295`, `execution-attribution.ts:479`, and `lifecycle.ts:728-733`. + +After an exact automatic compaction completes, every subsequent context callback enters the checked-control branch and calls `makeOrigin()` again. That creates a different `InputOrigin` object for the same control ID and live epoch. Normal user/admitted origins instead reuse their registry object. + +The new response-less retry path correctly finds a fresh unfinished native assistant with a retry record, retires the old attempt, and marks it for retry. The next `freeze()` preserves the original assistant-exclusion snapshot only when `prior.origin === origin`. The newly allocated compaction origin fails that check. `freeze()` therefore snapshots the reused retry assistant as a previous assistant. When the retried provider produces a valid running Flow call, `verify()` rejects that same assistant at line 479. `beforeTool()` then revokes the current lease. The provider may have recovered, but the user's automatic run stops at its first protected tool. Other native tools under automatic authority use the same verification path. + +Reproduction uses the existing in-memory lifecycle fixture and unchanged production `registerFlowLifecycle`. It runs two cases. Both admit `/flow-auto`, freeze a primary, simulate a response-less attempt, add the native unfinished assistant.retry record, freeze the retry, return its successful response, and attempt its exact running `flow_status` call. The second case first completes the existing exact automatic-compaction transition and consumes only that completed control ID. + +Observed results: + +- Original input control succeeds. `toolError` is null and its authority survives. +- Compacted input fails with `Flow exact assistant identity or frozen input prefix did not match.` Its authority becomes null. +- No network request or native host is launched. HTTP hooks receive local Request/Response objects only. + +Run with: + +```sh +PATH="/tmp/flow-v2-toolchain/node_modules/.bin:$PATH" TMPDIR=/tmp/flow-v2-core-review-r3/tmp /tmp/flow-v2-toolchain/node_modules/.bin/bun /tmp/flow-v2-core-review-r3/compaction-retry.ts +``` + +The rerunnable artifact is [compaction-retry.ts](compaction-retry.ts). Actual output is [compaction-retry.json](compaction-retry.json). The counterexample combines two supported transitions already observed separately in native receipts. This review does not claim a newly executed native compaction-plus-refusal run. + +Minimal recommendation. Reuse the exact established origin object for the same checked compaction input during the proven retry. Keep the full input, agent, model, live epoch, fresh retry tuple, unbound-assistant, and immutable assistant/call binding requirements. Do not relax the object-identity check or transfer an already bound assistant. Add a regression combining compaction and native response-less retry, including a delayed old response and a stale-epoch negative case. + +## Prior findings and objections + +1. **R1 stream and setup failures are closed.** Response cancel, read failure, missing or locked body, stale response, and failure after context freeze end the exact primary. Late callbacks cannot end a replacement through the response failure callback. The focused tests cover these cases and pass. +2. **R1 marker behavior is closed, with a provenance correction.** Current `lifecycle.ts:335-346` allocates the token and includes it in the admitted native payload. Activation follows matching receipt acceptance. I recomputed all three historical entry/map/source bindings. R1's recorded lifecycle `1e8d5991...` already has allocation and initial metadata, so its missing-marker claim is unsupported for that identity. The earlier archive `0e4ba80b...` has lifecycle `188ec484...` and actually lacks both. The later `224f3c78...` archive includes them. [evidence-checks.json](evidence-checks.json) retains the comparisons. The parent's reconciliation preserves this distinction and leaves the original R1 report and log unchanged. +3. **R2 response-less failure and interruption are closed for the reported fresh-input sequence.** `revoke()` at lifecycle lines 125-135 ends the captured primary before invalidating its epoch. A fresh command or real prompt retires the old attempt synchronously. Failed/interrupted event cleanup requires a new negative native idle record following the exact frozen input prefix, absent at freeze. The primary object and epoch are checked after the await. The success/idle session read also pins both. Focused tests cover the first fresh input, fresh input before the event, and delayed old reads. +4. **The ambiguous retry-hook objection is addressed in source.** No retry hook grants or retires primary authority. Context reconciliation requires the durable unfinished primary-assistant retry facts at execution-attribution lines 344-372. A consumed tuple cannot excuse another live overlap. The same-model auxiliary retry unit test leaves a healthy primary intact. Real auxiliary retry remains unproven, as the owner states. No concrete auxiliary misattribution was reproduced here. The new compaction finding is separate and demonstrated. + +## Architecture and remaining scope + +The data model remains a local admission registry, request-scoped frozen primary, immutable assistant/call binding, and current epoch. Prompt hooks revoke and record candidates. They do not grant origins. Context establishes authority through accepted payloads or exact real native input, and stale contexts are vetoed before HTTP. Automatic native tools and Flow tools verify their exact native assistant and running call before side effects. Summary text and event-prefix arrival do not grant authority. + +I read the current production sources and the actual base diff, with emphasis on lifetime transitions and retry composition. I verified the complete R2 identity inventory. Only `lifecycle.ts`, `execution-attribution.ts`, and `tests/lifecycle.test.ts` differ from R2. Every changed or added `src/` path is in [reviewed-files.sha256](reviewed-files.sha256), including the deleted V1 adapter. Unchanged HTTP helper, composition, preference/store/picker/TUI, validation, permissions, and loader boundaries retain their exact R2 identities. The relevant current paths were re-read rather than relying solely on a handoff. + +No additional finding emerged in those boundaries. The HTTP helper sets provider/model/variant transport before registration and the primary WebSocket hook fails closed. Reserved role rules retain deny-all followed by explicit allowed actions and worker metadata-path denials. Reviewer completion permits a verified reviewer child in the matching workspace with any parent present; other callers receive exact replay only. Validation retains exact command/call/session/role/workspace joins, native shell output proof, unknown-completeness refusal, fresh JUnit checks, and source-drift ineligibility at persistence. Preference storage retains atomic revisioned writes and stale/busy checks. No cross-process atomic prompt barrier is claimed. Native imports remain at adapters, TUI, and RPC, with host dependencies externalized in both builds. + +## Verification and artifact binding + +I ran the ten focused files from R2 with pinned Bun 1.4.0 and an owned TMPDIR. The result is **244 passed, zero failed**, recorded in [focused-tests.log](focused-tests.log). The files were `execution-attribution`, `lifecycle`, `opencode-native-core`, `auto-drive`, `validation-capture`, `tool-parentage`, `config-shared`, `model-preferences`, `model-preferences-store`, and `model-picker`, each under `tests/` with suffix `.test.ts`. These are offline core and picker unit/component tests. They do not contain the new compaction-plus-retry case. No full suite, build, typecheck, lint, replay, install, audit, Linux CI, or paid qualification was run by this reviewer. + +I independently read the new archive `/tmp/flow-v2-candidate-r3/opencode-plugin-flow-10.0.0.tgz`. Its SHA-256 is `8f29a34a33a562e6d289e7c357aee5f8b3863b6375a0a107ddac91fe94daa084`. Its server entry hashes to `65af1f5d951083ddb44d8a6b59b00186cca0cd338dcc01c841de9635cef7be96`. All 73 mapped server and 5 mapped TUI production sources match the working tree. [archive-source-check.json](archive-source-check.json) records both maps and entries. This proves that the new archive contains the reviewed code, including the finding. It is not packed runtime qualification. + +The retained private-entry receipts report 30 pre-response checks, 41 continuation/compaction checks, and strict marker lineage with no issues. I independently matched their loaded entry bytes, source map bytes, and all mapped production sources. The private entry is the same server entry now in the R3 archive. I also inspected retained native retry observations, which show the durable unfinished assistant retry record at the next context and an unchanged successful retry policy. [native-retry-observations.json](native-retry-observations.json) records the compact observations and source hash. These are owner-executed local native probes, not host runs by this reviewer. They cover retry and compaction separately. Their honest auxiliary limit remains intact. + +The final comparison is [drift.json](drift.json), which is empty. Initial and final hashes inventory 280 source/test files. Parent-reported historical or concurrently running full-suite and exact-package QA results were not promoted to my measurements. Main eval tooling remains another owner's work and was not reviewed as completed implementation. Visual, official-provider, Linux, final packed gates, paid qualification, and release approval remain separate. + +## Decision trail audit and principles + +[decisions.tsv](decisions.tsv) is this run's canonical trail. I audited its rows against actual tool output, files, and this conversation. [prior-trail-audit.json](prior-trail-audit.json) inventories all 54 actual rows in the supplied parent, R1, and R2 trails. All plain local pointers resolve. This is not independent re-execution of every historical or external claim. The R1 marker rows are superseded explicitly, including the parent row that initially adopted them. Other out-of-scope historical execution and external research remain labeled as historical evidence. No private host history store was accessed. + +Model the Domain changed the review to a transition check across compaction, request, retry, and assistant binding. That exposed the new origin-object mismatch. Make Operations Idempotent made the review check repeated retry and terminal delivery. Attack the Premise kept the lifetime census and central revocation change in the closure assessment. Boundary Discipline kept admission authority on accepted native payloads. Prove It Works required an executed counterexample and recomputed archive/map identities. Test Behavior, Not Implementation made the counterexample assert the actual tool rejection and lease loss against a successful control. Explain the Number kept 244 focused tests distinct from native receipt assertions and parent full-suite reports. Minimize Reader Load made the finding trace the two origin paths explicitly. + +The requested Poteto Investigation workflow ran sequentially within this independent reviewer. No sibling or nested delegate was launched. No comment-style verdict is given. No network, host, model, paid call, connector, goal, automation, Git write, commit, push, or PR was performed. Trusted sticky hooks were not verified for this turn. + +## Attention + +reviewed by gpt-6.1-sol + +- Fix and independently recheck the compaction retry case before local commit or treating the archive as reviewed without findings. +- Keep the R1 marker provenance correction. The earlier packed counterexample and the unsupported recorded-source claim are different facts. +- No independent agent reviewed this run's own trail because the task forbids delegation. No new cross-model audit is claimed. diff --git a/.agents/plans/18-opencode-v2/reviews/core-r4.md b/.agents/plans/18-opencode-v2/reviews/core-r4.md new file mode 100644 index 00000000..7bf1d420 --- /dev/null +++ b/.agents/plans/18-opencode-v2/reviews/core-r4.md @@ -0,0 +1,91 @@ +# OpenCode v2 core review, round 4 + +Verdict: **ISSUES**. + +The R3 automatic-compaction retry defect is closed. One separate P2 remains. A delayed old context or compaction callback can revoke a newly accepted `/flow-auto` invocation. The new invocation then fails before HTTP. This is a bounded source review, not release approval. + +The reviewed branch is `feat/opencode-v2`. HEAD and base are `8103f1cd42e1b4d111f25d2a311935d4e41fb997`. The migration is uncommitted. All reviewer writes are under `/tmp/flow-v2-core-review-r4`. + +## Finding + +### P2. A stale callback must not revoke a later accepted invocation + +Primary locations are `src/platform/opencode/lifecycle.ts:563-564` and `src/platform/opencode/lifecycle.ts:606-608`. Supporting paths are `lifecycle.ts:126-135`, `lifecycle.ts:316-317`, and `lifecycle.ts:367-391`. + +Both asynchronous callbacks can retain an old primary while awaiting `ctx.session.context`. Meanwhile a new `/flow-auto` command synchronously revokes that old epoch, receives a matching native admission receipt, activates a fresh lease, and settles its new input origin. + +When the old callback resumes, its stale check correctly throws. Its cleanup then revokes the **current** host epoch and deactivates the fresh lease. + +- In the context callback, the catch checks only whether `attribution.current(sessionID) === owned`. Command admission ends the old primary but does not replace that object before the new context freezes. This equality therefore still holds across the invocation change. It does not establish ownership of the current epoch. +- In the compaction callback, the catch calls `revoke(sessionID)` unconditionally. The old `assertLive(primary.origin)` fails after the await, and that failure revokes the later invocation. + +This is a liveness defect, not an authority grant. The user's newly accepted automatic run loses its lease, and its first valid context then throws `Flow execution epoch was revoked.` The user must issue another command after the stale callback has finished. + +The executed reproduction is [challenges.ts](challenges.ts), with literal observations and assertions in [challenges.json](challenges.json). It uses the retained in-memory native lifecycle fixture and current production `registerFlowLifecycle`. + +1. Admit the original `/flow-auto`, finish its primary response, and establish the exact running automatic control when testing compaction. +2. Block the old callback's native context read. This retains a snapshot across an actual asynchronous boundary. +3. Admit and await a fresh `/flow-auto` command. Confirm its authority is present. +4. Release the old read. The context case throws the stale-input error. The compaction case throws the revoked-epoch error. Both leave the fresh authority null. +5. Submit the fresh native context. Both cases now reject it with the revoked-epoch error. + +A control with no delayed old callback admits the fresh invocation, completes its response, and executes its protected Flow tool successfully. No HTTP request is sent over the network by these scenarios. Request and Response objects remain local. + +Run the reproduction with: + +```sh +PATH=/tmp/flow-v2-toolchain/node_modules/.bin:$PATH TMPDIR=/tmp/flow-v2-core-review-r4/tmp /tmp/flow-v2-toolchain/node_modules/.bin/bun /tmp/flow-v2-core-review-r4/challenges.ts +``` + +Minimal recommendation. Capture callback epoch ownership before the first await. On failure, reject the stale callback and end only its own attempt, but revoke the host only if that epoch still belongs to the callback. Pin compaction cleanup to the captured primary and epoch as well. Keep stale-context and stale-tool vetoes intact. Add both delayed-read interleavings as regressions, asserting that the newly admitted context and protected tool succeed. + +## R3 closure and adversarial checks + +The changed data shape is a private `WeakMap` at `lifecycle.ts:93`. The running-control hook records the origin with the original delivery and epoch at lines 595-605. The resumed context validates the exact completed control and full consumed prefix before retrieving that origin at lines 463-477. + +This fixes the reported cause. Repeated proven retries now reuse the same origin object. `ExecutionAttribution.freeze` retains its strict `prior.origin === origin` condition at `execution-attribution.ts:280`. Global retry identity was not weakened. + +I independently reran the former reproduction twice. [archived-compaction-retry.ts](archived-compaction-retry.ts) uses the lifecycle source from the old `8f29` archive, with only relative imports redirected to the unchanged repository dependencies. Its compacted case rejects the reused assistant and loses authority. The original-input control succeeds. [compaction-retry.ts](compaction-retry.ts) runs current production source. Both cases now execute the protected tool and retain authority. The JSON outputs preserve both results. [r3-to-r4-lifecycle.diff](r3-to-r4-lifecycle.diff) records the actual archive-to-source change. + +Additional executed challenges pass three response-less retries in succession, delayed old HTTP responses at each retry, and repeated verification of the same bound call. An already bound assistant cannot justify response-less retry. An assistant removed before a later freeze and then reintroduced cannot bypass immutable binding. The focused matrix rejects revoked origins and exact control, reason, model, and prefix mismatches. The new finding concerns callback cleanup after fresh admission, which that matrix does not cover. + +## Prior findings and unchanged boundaries + +The R1 stream cancel/read/setup/no-body and R2 pre-response first-fresh-input findings remain closed in their reported sequences. Exact Request finalizers and pinned terminal reads remain present and pass focused tests. This finding does not reopen their specific reproductions. + +The R1 missing-marker claim remains unsupported for its recorded lifecycle hash `1e8d5991dcaa756ef19484a6219dcca980551f201d21676c63534019af463772`. I recomputed the retained entry, map, and embedded source identities. That source already allocates and persists the invocation token. The earlier `0e4ba80b...` archive contains lifecycle `188ec484...`, which genuinely lacks those operations. [marker-provenance.json](marker-provenance.json) preserves this distinction. Original reports and trails were not edited. + +The unchanged authority boundary remains an admission registry, frozen request context, immutable assistant/call binding, and current epoch. Prompt hooks revoke and record candidates. They do not grant origins. Summary text and asynchronous event-prefix arrival do not grant authority. Primary WebSocket requests still fail closed. + +I re-read the current production sources and relevant base diff for permissions, tool registration, validation, reviewer completion, preferences, store, picker, TUI, transport, and composition. No additional causal defect emerged there. Any verified reviewer child in the matching workspace may attempt a new completion when it has a parent. There is no assignment-manager-parent requirement. Other callers use exact accepted replay. Validation retains exact command and execution joins, native shell output proof, completeness refusal, fresh JUnit checks, and source-drift ineligibility at persistence. Preference writes retain atomic revisioned storage and stale/busy checks. Native dependencies remain at the adapters, TUI, and RPC contract, and the builds externalize host packages and Effect. The V1 schema adapter is deleted. + +## Tests and artifact identity + +Pinned Bun 1.4.0 ran the ten focused files for execution attribution, lifecycle, native core, auto-drive, validation capture, parentage, config, model preferences, preference store, and picker. **272 passed, zero failed**, across those ten files. [focused-tests.log](focused-tests.log) records the exact cases. Preference tests use ordinary local Bun subprocesses. The standalone reproductions are additional component evidence, not additional suite counts. + +I independently checked the owner's corrected private-entry receipt. Its entry hashes to `0edcf50fbc98d609c8ce6df6f50c5c167fd348f579baa80d80b96348bede1d87`. The source map hash and all 73 mapped production sources match current source. [source-binding.json](source-binding.json) records those comparisons. The retained receipt reports 24 combined native assertions. That native run belongs to the owner, not this reviewer. + +During review the parent supplied the R4 archive. Its raw SHA-256 is `ec8728416e4000657014acee3b3838e2d034189b4cdeda463cb3f4c6248265b7`. I read it without installation or extraction. Both archive entries and maps match the installed cache bytes. All 73 server and five TUI mapped production source entries match the working tree. The server entry equals the corrected private entry above. [archive-source-check.json](archive-source-check.json) and [verify-archive.py](verify-archive.py) record the checks. This binds the source verdict, including the new finding, to the archive. It does not establish a successful exact-packed runtime rerun. + +## Identity, drift, and limitations + +[reviewed-files.sha256](reviewed-files.sha256) records 44 inspected source, test, package, and surrounding-boundary identities, including the adapter deletion. [initial-hashes.json](initial-hashes.json) and [final-hashes.json](final-hashes.json) inventory all 298 source/test files. There was no production or focused-test drift during review. One outside-scope eval test, `tests/recovery-episode-host.test.ts`, changed concurrently. [drift.json](drift.json) records both hashes. Compared with R3, lifecycle is the only changed production file; lifecycle and release test changes are recorded separately. + +Real auxiliary **retry** remains unproved. The reported pending-primary auxiliary generate observation and bounded component negatives do not prove that case. No final-export compaction lineage claim is made. Main eval tooling and the native account prototype were not reviewed as completed work. The supplied comment inventory is separate; no comment-style verdict is given. + +No host, provider server, model, paid call, network request, connector, browser, delegate, goal, automation, Git write, root build, package creation, or installation was performed. No whole check, full-suite, Linux CI, visual, official-provider, paid qualification, or release approval is claimed. The separate exact-packed compound rerun remains pending. + +## Decision trail audit + +[decisions.tsv](decisions.tsv) is this run's append-only trail. I audited it against the active conversation, source, actual command results, and retained evidence. Its paths resolve. An initial archive-cache-path assumption failed and was corrected before making an identity claim. A premature no-drift result was superseded when the printed comparison showed the concurrently edited eval test. Both corrections remain visible. + +Model the Domain made the review trace origin, attempt, binding, and epoch ownership separately. Make Operations Idempotent drove the repeated-retry and delayed-callback challenges. Fix Root Causes required reproducing the old failure before accepting the stable-origin fix. Boundary Discipline kept the finding on callback cleanup rather than weakening admission checks. Prove It Works required actual archive/map comparisons and an executed failing interleaving. Test Behavior, Not Implementation made the finding assert fresh-authority loss and rejection of the fresh context against a successful control. + +## Attention + +reviewed by gpt-6.1-sol + +- Scope stale callback cleanup to its captured epoch before treating this source as finding-free. +- The current compaction retry fix passes its independent reproduction. Preserve its strict retry and immutable-binding checks. +- No independent agent audited this run's trail because the task forbids delegates. No cross-model audit is claimed. +- Exact-packed runtime, auxiliary retry, final-export lineage, and release gates retain the limits above. diff --git a/.agents/plans/18-opencode-v2/reviews/core-r5.md b/.agents/plans/18-opencode-v2/reviews/core-r5.md new file mode 100644 index 00000000..45b5b8bb --- /dev/null +++ b/.agents/plans/18-opencode-v2/reviews/core-r5.md @@ -0,0 +1,100 @@ +# OpenCode v2 core review, round 5 + +Verdict: **PASS**. + +This is a bounded source review of the callback-ownership fix on `feat/opencode-v2`. HEAD and the review base are `8103f1cd42e1b4d111f25d2a311935d4e41fb997`. The migration is still uncommitted. Release approval, a packed runtime, Linux CI, visual proof, and the future Typesafe API key env option are outside this verdict. + +No concrete source defect remained in the attacked ownership paths. A delayed old context or compaction callback, resolving or rejecting, no longer revokes a newly accepted `/flow-auto` after admission or after the new context has frozen. The new protected tool runs. A stale automatic shell stays rejected. Strict retry object identity still holds. + +## Ownership result + +The rejected premise is that the current primary object still proves ownership of the host epoch after an await. A new command advances the epoch and can leave the ended primary object in the map. `end` marks that object ended and does not remove it (`execution-attribution.ts:390-393`). + +`CallbackOwner` (`lifecycle.ts:75-80`) stores host, epoch, the primary snapshot, and kind `attempt` or `admission`. `owns` (`lifecycle.ts:151-155`) requires the host epoch to match. Attempt cleanup also requires `attribution.current(host)` to be the captured primary. Admission cleanup may cancel its captured logical epoch and does not consult primary identity. `failOwned` (`lifecycle.ts:161-164`) ends the captured primary only when that primary's origin epoch matches, then calls `revoke` only when `owns` is true. + +The remaining direct host `revoke` calls are owned cleanup, the synchronous real-prompt hook (`lifecycle.ts:361`), the synchronous command start (`lifecycle.ts:383`), and confirmed session deletion (`lifecycle.ts:784`). Setup failure, event-stream loss, and dispose still invalidate the whole plugin (`lifecycle.ts:760-767`, `849-858`, `865-871`). + +Checked actor paths: + +- Context captures the owner before the native read (`lifecycle.ts:505-520`). The same-epoch overlap check revokes that epoch. A later epoch replaces the pending slot. The old `finally` deletes the slot only when it is still that owner object (`lifecycle.ts:637-639`). +- Compaction captures the existing automatic primary and its origin epoch (`lifecycle.ts:647-688`). The `WeakMap` from checked compaction to `InputOrigin` is unchanged (`lifecycle.ts:99`, `675-684`). +- Tool verification resolves the immutable assistant or call binding first (`execution-attribution.ts:257-268`). A known old binding cannot take the replacement primary. `verify` checks that owner before and after the native reads (`lifecycle.ts:217-262`). `execute.before` and public `beforeTool` share that function (`lifecycle.ts:743-758`, `863`). +- Registered HTTP requests use the `WeakMap` owner (`lifecycle.ts:692-722`, `execution-attribution.ts:254-255`). A late response for that old `Request` throws the identity error and leaves the new authority in place (`execution-attribution.ts:400-410`). Body cancel calls the failure hook only when `end` still sees that primary as current (`execution-attribution.ts:419-421`, `448-450`). +- Command and synthetic admission capture the logical epoch before the first native read (`lifecycle.ts:282-299`, `383-396`). A superseded admission does not cancel the replacement. +- Accepted-message observation rechecks ownership around `observeMessage` (`lifecycle.ts:193-216`). +- A failed per-host session or context read calls `failOwned` for that host and logs. It does not set `streamLost` (`lifecycle.ts:786-805`, `841-846`). Subscribe error and EOF still set `streamLost`, clear attribution, and call `recovery.revoke()` with no host (`lifecycle.ts:849-858`, `recovery-policy.ts:260-270`). + +## Executed reproductions + +`/tmp/flow-v2-core-review-r5/challenges.ts` imports production `registerFlowLifecycle`. The native context read snapshots the message array after the blocked read resumes, so the old callback observes the post-replacement transcript. Results are in `challenges.json`. + +The matrix is context and compaction, each resolving and rejecting, each after fresh admission and after the new context and HTTP request exist. Every old callback throws. Authority before and after is the same object, epoch 5, token `local-token`. `beforeTool` on the new assistant then resolves. + +Also executed in that file: + +- Three response-less retries, then the same bound call twice. Both tool calls resolve and authority remains. +- An assistant removed before a later freeze and then restored cannot bind again. +- An already bound assistant cannot justify a response-less retry. The context hook rejects overlap. `freeze` still requires `prior.origin === origin` (`execution-attribution.ts:293-299`). +- Cancel of a registered old response body after the fresh tool has run leaves epoch 5 in place, and a second fresh tool call resolves. +- A late response for the old registered `Request` throws `Flow primary response did not match its Request identity.` The fresh tool still resolves. +- An unbound shell whose assistant id is already in the manual context's previous-assistant set is rejected with `Flow tool has no attributable primary request.` The later manual Flow tool resolves. Authority stays absent after the manual steer. + +Pinned Bun 1.4.0 also ran 15 focused files. The command and the pass lines are in `focused-tests.log`. **408 passed, 0 failed**, one run, 939 ms, 2139 `expect()` calls. The files are lifecycle, execution attribution, auto-drive, auto-drive decision, auto-drive validation, recovery host, recovery policy, recovery authority, tool parentage, native core, validation capture, config, model preferences, preference store, and model picker. That count is only those files. It does not measure evals, native auth, a full check, or a packed host. + +The lifecycle file includes the production copies of the ownership matrix, the stale-shell case, stream-loss fail-closed, and the response-less retry cases. Those passed inside the 408. + +## Observation, not a finding + +An unregistered primary `http.response` still ends the live primary and revokes that epoch. The hook falls back to the current primary when `requestOwner` misses (`lifecycle.ts:712-722`). `response` then ends that current primary and calls the failure hook (`execution-attribution.ts:407-414`). In the probe, authority became null and the already registered live request could not finish. + +That path is the existing exact-`Request` failure. A response whose `Request` was registered to the old primary does not take this branch and did not revoke the replacement. I am not holding the source gate on a host that synthesizes a new `Request` object for a primary response. Inspect that branch if a real host can do so. + +## Retained closures and boundaries + +R1 stream, setup, cancel, read, and no-body failures, and the R2 response-less first-input failure, stay closed on the sequences those rounds recorded. This round did not rebuild those historical scripts. The current lifecycle tests that cover stream loss, exact request finalizers, and response-less retry passed. + +The R1 missing-marker claim stays unsupported for lifecycle `1e8d5991dcaa756ef19484a6219dcca980551f201d21676c63534019af463772`. `/tmp/flow-v2-core-review-r4/marker-provenance.json` still records that source as allocating and persisting the token, and archive `0e4` lifecycle `188ec48417cef20250e9208cf69e3eaa9225e15f2d6b1331f4b18a630164fd35` as lacking both. I read that receipt. I did not re-hash the tarballs in this round. + +The R3 compaction retry defect stays closed. Repeated retries in the probe reuse the attempt, and a bound assistant still cannot mint a new origin. The R4 archive hash `ec8728416e4000657014acee3b3838e2d034189b4cdeda463cb3f4c6248265b7` was bound by R4 to lifecycle `bd5fb2eaabf6...`. Current lifecycle is `5ed5b01899572bdbfb8cadb4fdf6fd62ccc29a3067ec28da91c02e3bb11bc2bc`. That archive does not contain this fix. I did not open the tarball again. + +Prompt hooks revoke and record candidates (`lifecycle.ts:342-366`). They do not grant an origin. Summary text is not an origin. The compaction memo is keyed by the checked control object. + +`tool-guard.ts:124-133` permits `flow_feature_complete` for a `flow-reviewer` child that has a parent. It does not require an assignment-manager parent id. Other mutations still require a primary manager. `validation-capture.ts:450-514` still joins the armed shell command, session identity, workspace, and `sourceDigest`, and host gaps stay ineligible. `package.json` build scripts still externalize `@opencode/plugin`, `zod`, `@opencode/schema`, `@opencode/client`, and `effect`. Those files match the R4 reviewed hashes. `schema-adapter.ts` is absent. V1 remains deleted. + +## Identity and drift + +Owned hashes, unchanged from the start of this review through the test run: + +- `src/platform/opencode/lifecycle.ts` `5ed5b01899572bdbfb8cadb4fdf6fd62ccc29a3067ec28da91c02e3bb11bc2bc` +- `src/platform/opencode/execution-attribution.ts` `69f5e7212d42e6919c9c495332587a34b10b83392799323cb778130547117294` +- `tests/lifecycle.test.ts` `d2dee273bb0e63b27bf0ada85d0a1822729ecfa861b2576fe2c505725b3c5a60` + +`initial-hashes.json` and `final-hashes.json` cover 305 `src` and `tests` files at the start, and the same set at the end. No `src` file changed during the review. Compared with R4's final inventory, the only `src` changes are the two owned production files above. Concurrent edits outside the core fence changed `tests/canary-run.test.ts`, `tests/canary-v2-native-auth.test.ts`, and `tests/eval-canary.test.ts` while this review ran. `drift.json` has both hashes. Those files were not in the focused command. Native-auth tests and eval harness edits are not evidence for this source. + +## Limitations + +Actual auxiliary retry is still unproved. The suite shows a same-session auxiliary retry cannot retire a healthy primary. That is a negative component. No native auxiliary retry callback was executed here. + +No packed profile, full check, Linux run, paid model, or official-provider qualification was run after this fix. The root Linux result of 4494 passes predates this source and the auth work. I do not apply it here. + +No final archive contains lifecycle `5ed5b018...`. Root pack is still later. + +The Typesafe API key env name is not in this source and does not affect this verdict. + +Comment inventory is a separate review. Comment Sicko was not run. This round gives no comment verdict. + +No host, network, Git write, root build, package, delegate, or second model was used. + +## Decision trail audit + +`decisions.tsv` is this run's log. Each row matches a command or a code read from this session. `initial-hashes.json`, `focused-tests.log`, `challenges.json`, and `drift.json` exist and say what the rows claim. I did not edit earlier rows. + +Cross-model review of the trail did not run. The parent forbade delegates. + +## Attention + +reviewed by none + +- The unregistered primary response still revokes the live epoch. I left it as exact-request fail-closed. Read `execution-attribution.ts:407-414` if a host can respond with a new `Request` object. +- This PASS is the source ownership gate only. Packed runtime, auxiliary retry, and the final archive are still open. +- Canary and native-auth tests moved during the review. They are outside the hashed core verdict. diff --git a/.agents/plans/18-opencode-v2/reviews/core-r6-decisions.tsv b/.agents/plans/18-opencode-v2/reviews/core-r6-decisions.tsv new file mode 100644 index 00000000..234a9236 --- /dev/null +++ b/.agents/plans/18-opencode-v2/reviews/core-r6-decisions.tsv @@ -0,0 +1,10 @@ +ts phase decision why evidence result +2026-10-24T11:05:19.950622+00:00 start Review frozen core sequentially within the read-only fence The parent forbids delegates and repository writes Current R6 brief; initial-hashes.json Initial source and test hashes recorded +2026-10-24T11:07:28.269312+00:00 verification Bind copied private entry to current source and challenge same-epoch pending contexts A reported suite is not source identity or concurrency proof source-binding.json; focused-tests.log; same-epoch.json 439 focused tests pass; two additional synthetic overlap cases fail and require reachability assessment; diff-writer generator error corrected before identity claim +2026-10-24T11:11:11.482120+00:00 native Rerun four automatic-authority cases on the copied private entry Source review must preserve exact control and retry identity in the actual host native/; native-compaction-retry-verification.json Stale compaction, first invocation, stale context, and actual compaction plus response-less retry succeed +2026-10-24T11:11:11.482229+00:00 finding Retain a P2 for ordinary coding after automatic compaction without a lease Both actual native prior states reject the completed automatic control before HTTP ordinary-compaction.json; native/run-20261024-130946-865865/; native/run-20261024-131002-888264/ Two native failures; offline controls pass and both compacted inputs fail +2026-10-24T11:11:11.482231+00:00 audit Recompute historical marker provenance and exact moved bodies Prior findings cannot be carried on mismatched source bytes marker-provenance.json; moved-code-check.json; cleanup-census.json R1 correction preserved; moved converter and parser are byte-identical after export and rename +2026-10-24T11:11:36.533618+00:00 concurrency Keep the same-epoch pending-source overlap as an unresolved component objection Two direct callback interleavings fail but native concurrent primary reachability is unproved same-epoch.test.ts; initial-same-epoch.json Do not present it as a second native defect; preserve initial output and distinguish tool not reached from tool success +2026-10-24T11:11:36.533719+00:00 cleanup Verify six owned native runs and all twelve stopped processes Native evidence needs exact entry binding and private service state native-review-verification.json Four expected successes and two confirmed failures, all on current entry and 74 matching sources +2026-10-24T11:13:17.135055+00:00 verdict Return ISSUES with one native P2 and the bounded component objection Ordinary coding fails after an exact automatic control without a Flow lease report.md; native-review-verification.json; ordinary-compaction.log; same-epoch.log No source PASS or release approval +2026-10-24T11:13:33.185618+00:00 self-audit Check trail rows, report links, and final hashes Each final claim must resolve to this run or explicitly identified historical evidence report.md; decisions.tsv; final-hashes.json; drift.json; trail-audit.json All report artifact links resolve; all 292 source/test/package hashes unchanged; no independent trail reviewer diff --git a/.agents/plans/18-opencode-v2/reviews/core-r6.md b/.agents/plans/18-opencode-v2/reviews/core-r6.md new file mode 100644 index 00000000..e9d9e1ef --- /dev/null +++ b/.agents/plans/18-opencode-v2/reviews/core-r6.md @@ -0,0 +1,108 @@ +# OpenCode v2 core review, round 6 + +Verdict **ISSUES**. One P2 production liveness defect is reproduced on the actual native host. Ordinary coding without a Flow automatic lease cannot resume after automatic compaction. The current automatic-lease fix passes its decisive native cases, including compaction before the first primary and compaction followed by a response-less retry. + +This verdict covers the frozen core source. It gives no release approval. The branch and base are supplied as `feat/opencode-v2` and `8103f1cd42e1b4d111f25d2a311935d4e41fb997`. I ran no Git command. All reviewer writes are under `/tmp/flow-v2-core-review-r6`. + +## P2. Ordinary coding stops after automatic compaction without a lease + +Primary location is [lifecycle.ts:737](/Users/vriesd/projects/flow-opencode/src/platform/opencode/lifecycle.ts:737). The downstream rejection is [lifecycle.ts:413](/Users/vriesd/projects/flow-opencode/src/platform/opencode/lifecycle.ts:413), through lines 429. The context hook calls that resolver at line 687. + +An ordinary unmarked user prompt records a real-input candidate and revokes prior execution authority. It does not create a Flow automatic lease. If native automatic compaction happens before its primary context, the compaction hook returns at line 737 because both guidance and accepted automatic authority are absent. It never resolves the real candidate or records the exact running automatic control. + +Native compaction then succeeds and prunes the original user. The resumed context consumes only the completed control ID. There is no checked control in `compactions`, no registered admission for that ID, and no real candidate for a compaction record. `resolveOrigin` throws `Flow context has an unknown, synthetic, or stale real input.` The user's normal coding execution fails before its next provider request. + +I reproduced both requested prior states with unchanged production setup and handlers on native OpenCode 2.1.4: + +- After a public plugin reload, no primary is retained in the new lifecycle instance. A fresh ordinary unmarked user is accepted. Automatic compaction completes. The first resumed context fails with the exact error above. There are zero subsequent provider requests and zero protected tool completions. +- With an ended manual primary retained, the same accepted unmarked input, automatic control, pruning, and rejection occur. There are again zero subsequent provider requests and zero protected tool completions. + +The first native run is [run-20261024-130946-865865](native/run-20261024-130946-865865/native-ordinary-compaction-result.json). The second is [run-20261024-131002-888264](native/run-20261024-131002-888264/native-ordinary-compaction-result.json). Their `cases.stdout` files give compact observations. [native-review-verification.json](native-review-verification.json) checks the causal joins, source identity, and cleanup. Both controls have reason `auto`, status `running` at the hook, and status `completed` afterward. Each running control's consumed prefix ends in the fresh native user ID. Each resumed context consumes exactly its completed control ID. Neither case calls `/flow-auto`, admits marked metadata, or creates durable Flow state. + +The probe uses the existing loopback provider's reported token usage to trigger real native automatic compaction. The observer supplies a summary through the public compaction hook. It does not insert transcript records or replace product handlers. This is a native control and context path, not an exported transcript interpreted afterward. + +[ordinary-compaction.test.ts](ordinary-compaction.test.ts) independently reproduces the same production lifecycle transitions offline. Its no-compaction controls execute the protected tool with `authority === null`. Both compacted variants fail. [ordinary-compaction.log](ordinary-compaction.log) records two passing controls and two failing expected-success tests. The native collection scripts exit zero after validating the observed defect. That exit code is not a product success verdict. + +Minimal recommendation. Preserve ordinary input provenance through an exact native automatic control independently of an automatic lease. Check the accepted real input, complete consumed prefix, selected agent and model, running control, and completed control before allowing ordinary primary attribution. Keep its automatic authority absent. Summary text, current epoch, and latest-user position must not mint a grant. A manual-reason control must still fail the existing automatic-authority preservation checks. Add the two ordinary cases with a successful resumed primary and protected tool before closing this finding. + +To rerun the actual cases, use the two `run-ordinary-native.py` commands in [commands.json](commands.json). The private entry and immutable dependency cache must remain present. + +## Current automatic-authority fix + +The data model separates the accepted `InputOrigin`, its registration epoch, a checked compaction control, a frozen provider attempt, and immutable assistant and Request bindings. Pending registrations expose a promise, not an accepted origin. Context and compaction share `resolveOrigin`. The compaction path no longer requires a retained primary to establish an accepted input's provenance. + +The independent native reruns pass: + +- [Stale compaction callback](native/run-20261024-130720-862983/native-stale-callback-result.json). The old callback fails. A distinct fresh running control consumes the newly accepted user. The first fresh context consumes that exact completed control after pruning. The protected `flow_status` completes with active automatic authority. Durable Flow state remains revision 2 with the accepted draft and approval. +- [First invocation with no retained primary](native/run-20261024-130750-590231/native-first-compaction-result.json). Public reload clears the lifecycle after genuine native long history. The fresh marked command compacts before its first primary. Its exact completed control reaches the protected tool. No plan is created implicitly. +- [Automatic compaction plus response-less retry](native/run-20261024-130804-281373/native-compaction-retry-result.json). Public `model.request` selects a closed loopback port after the checked control completes. Native retry policy remains unchanged. The retried primary reuses the unfinished assistant and successfully executes `call_post_compaction`. The copied verifier passes all 24 assertions. See [native-compaction-retry-verification.json](native-compaction-retry-verification.json). +- [Delayed old context entry](native/run-20261024-130823-799926/native-stale-context-result.json). The old context enters after fresh admission and fails its epoch check. The fresh context and protected tool succeed. + +The original causal success assertion was retained. The new native running control consumes the exact fresh accepted user before compaction. The later primary and tool consume the exact completed control after the native context removes that user. No test requires the pruned user to remain present or treats summary text as authority. + +## Ownership checks and an unresolved component objection + +The current source captures input ownership before native reads. Cleanup checks epoch, exact attempt when applicable, and known source. `failOwned` separates ending an owned attempt from revoking a host. Old finally blocks delete only their own pending slot. Known old assistant and Request bindings cannot borrow a replacement primary for cleanup. Both tool entry routes share verification. Accepted-message observation checks ownership around awaited policy work. Per-host native read failures remain host-scoped. Subscription error and EOF still close all sessions. + +[cleanup-census.json](cleanup-census.json) independently regenerates the owner's lexical census. It has 31 cleanup sites and four direct host-revoke sites. This is an actor inventory, not proof of runtime ordering. + +The copied R5 challenge [r5-challenges.ts](r5-challenges.ts) passes all 18 retained cases on current source. It reads the native array after a delayed read resumes, so an old callback can observe the replacement transcript. Delayed context and compaction resolve and reject leave the fresh context and protected tool runnable. Repeated response-less retries, bound-assistant rejection, late registered responses, old stream cancel, and stale automatic shell after manual replacement remain covered. The exact-Request fail-closed observation for an unregistered response is retained without a new native claim. + +One additional component objection remains explicit. [lifecycle.ts:663](/Users/vriesd/projects/flow-opencode/src/platform/opencode/lifecycle.ts:663) compares a pending context's epoch but not its source. In [same-epoch.test.ts](same-epoch.test.ts), an old context is paused in its native read. A new private synthetic input is accepted in the same epoch. A new context then encounters the old pending slot, throws `Flow primary context callbacks overlap.`, and revokes the fresh authority. This occurs for delayed old resolve and reject. If the old callback settles first, both controls execute the fresh protected tool and preserve authority. + +[same-epoch.json](same-epoch.json) records the actual failures. The failed contexts never reach their tool, and the receipt explicitly records `toolRan: false`. Native overlapping primary callback reachability for this exact sequence is unproved. I retain this as an unresolved source-level concurrency objection, not a second demonstrated native production defect. The parent should establish whether the native host serializes these callbacks across cancellation. If overlap is supported, the pending slot must distinguish stale source ownership while continuing to reject genuine same-input overlap. The P2 above independently prevents source PASS. + +## Retained findings and provenance corrections + +R1 stream cancel, read failure, missing body, locked body, stale response, and setup-after-freeze sequences pass current focused tests. HTTP 400 preserves the provider response and releases the exact attempt. R2 pre-response failure and interruption recovery also pass. The specific originally reported sequences stay closed. I did not rerun every historical native stream profile on this private entry. + +R3's origin recreation defect stays closed. The exact `CheckedAutoCompaction` object retains its memoized `InputOrigin`. `freeze` still requires `prior.origin === origin` at execution-attribution line 312. Native compaction plus refusal and reused-assistant retry now passes in this review. Already bound assistants and consumed retry tuples remain rejected. + +R4's old callback cleanup finding stays closed in its recorded command-replacement sequences. R5's PASS applies to its old source only. The later rejected archive is not accepted here. + +I recomputed the historical marker identities in [marker-provenance.json](marker-provenance.json). R1's recorded lifecycle `1e8d5991dcaa756ef19484a6219dcca980551f201d21676c63534019af463772` already allocates and persists the initial marker. Its missing-marker claim is unsupported for those bytes. The earlier `0e4ba80b...` archive contains lifecycle `188ec484...`, which genuinely lacks both operations. The `224f3c78...` source contains both. Original reports and trails remain unchanged. + +The parent's reconciliation was read as historical evidence. Its statement calling d3d46d92 the final product archive is superseded by the supplied rejection brief and retained native failure. The old callback's correct rejection did not prove fresh lease revocation. The distinct fresh preprimary control was the failure in that archive. + +## Other production boundaries + +The relevant current permission, tool, validation, preference, store, picker, TUI, transport, and composition paths were reread. The retained base diff and current five-file patch were inspected. [reviewed-files.sha256](reviewed-files.sha256) records scoped identities, including the deleted V1 adapter. Native dependencies remain external in both package build commands. Inward application layers do not import the native host. The architecture tests pass, including all source ceilings and referenced exports. + +The moved prompt converter and command-name parser have unchanged bodies after accounting for export and rename. [moved-code-check.json](moved-code-check.json) compares actual old archive source with current source. Delimiter-counted lines remain lifecycle 993, auto-drive 998, execution attribution 607, auto-compaction 73, command-hook 247, and recovery-policy 1000. No ceiling exception or compressed formatting was introduced. + +Reviewer completion permits any verified `flow-reviewer` child in the matching workspace with a parent present. It does not require an assignment-manager parent identity. Other callers retain exact accepted replay only. Validation joins exact shell command, native execution, session, workspace, completeness, report freshness, and source digest at persistence. Unknown host output remains ineligible. Preference persistence retains atomic revisioned updates and stale, model, project, activity, abort, and unload checks. No cross-process atomic prompt barrier or TUI visual proof is claimed. + +The supplied comment inventory has three existing public-contract keeps in eval tooling and zero suppressions. Current five-file source markers are the three preexisting auto-drive comments. I give no comment-style findings and no independent Comment Sicko verdict. The parent forbids delegates. + +## Executed checks and exact identity + +Pinned `/tmp/flow-v2-toolchain/node_modules/.bin/bun --version` reports 1.4.0. The ambient Bun is 1.3.14 and was not used for these tests. [focused-command.json](focused-command.json) records the exact 16-file command and isolated environment. [focused-tests.log](focused-tests.log) records **439 passed, zero failed, one filtered**, with 2241 expectations. This includes all six architecture tests. The filtered test constructs a Git-backed recovery fixture. No full suite, root typecheck, build, pack, install, or Git-backed grader preparation ran here. + +The extra review probes are separate from that number. R5's 18 cases pass. The ordinary compaction regression has two passing controls and two expected-success failures. The same-epoch component challenge also has two passing controls and two expected-success failures. Neither set of failures is hidden in the focused suite total. + +The private entry is `169609bd9a4d12ba5dd49f60a47ce8251dc9f0dc6d1e9a4bf4604a8096b5e3b3`. Its map is `20636a358fb5c07ec8edef2bbf70158984e280a73396d3dbfebdb47aa12aa662`. I copied both without building. All 74 mapped production contents equal workspace bytes. [source-binding.json](source-binding.json) records the comparison. The private runtime reports development version `0.0.0`. This is not a published or packed 10.0.0 entry. + +The immutable archive remains `d3d46d92148a34d829536c939a528f52c3ef6adeb612230afab860b003c09292`. Its server map differs in exactly the five owned production files. [archive-to-current.diff](archive-to-current.diff) records that comparison. No replacement archive was produced. + +Initial and final inventories cover 292 source, test, and package files. [drift.json](drift.json) is empty. The owned hashes match freeze-for-r6 throughout. Lifecycle is `8c7b8ff2ffb325afb145fcb1b437732dbf2bf7f1e2fee67217d39a11deacb63e`. Execution attribution is `fbd303985512a0b8a67eb1ae7ff5069b1be4f9b844b9f3815b2179bb64b96b1d`. All scoped hashes are in the inventories. The parent-reported ADR-only update is outside that inventory and explains documentation drift. It does not change this source identity. + +Six native runs used isolated HOME, XDG, cache, default-service, and temporary directories under this review output. Providers and health checks used loopback only. All 12 owned host and provider processes were stopped and awaited. Their PIDs and exit codes are in [native-review-verification.json](native-review-verification.json). No process or operation targeted PID 76572. No real credentials, native-account helpers, external provider, paid call, delegate, goal, automation, connector, or repository write was used. + +## Limits and trail audit + +Actual same-model auxiliary generate retry remains unproved. The current negative component passes. The earlier pending-primary auxiliary generate and unsuccessful reentrant attempt do not establish retry behavior. The owner marker receipt is source-bound evidence that I inspected, not a marker host run by this reviewer. Final export alone does not prove compaction continuity. + +The parent's canonical macOS and Linux checks, documentation check, final archive, packed runtime wave, visual gate, official-provider qualification, and paid qualification remain parent-owned. I adopt no parent suite result as a measurement from this run. Native-account helper review and the separate Typesafe key-name configuration review remain outside this verdict. + +[decisions.tsv](decisions.tsv) is this run's append-only trail. I audited it against the active conversation, source, retained evidence, command outputs, and probe files. An initial diff-writer generator error was corrected before any identity claim. The initial same-epoch receipt was preserved when I made the unreached tool explicit. No historical owner evidence was rewritten. Native observations distinguish expected success from confirmed failure. Cross-model review of this trail did not run because delegates are forbidden. + +Poteto's Model the Domain principle made the review distinguish ordinary input provenance from automatic authority. Attack the Premise extended the census to the no-lease actor missing from the owner's positive matrix. Prove It Works required actual native control-to-context-to-tool behavior and source-map equality. Test Behavior, Not Implementation made the ordinary controls execute a real protected boundary and left failing expected-success tests visible. Minimize Reader Load made the finding trace the single early return and its downstream resolver rejection. The Investigation workflow ran sequentially under the parent's explicit scope. + +## Attention + +reviewed by gpt-6.1-sol, self-audit only + +- Fix ordinary no-lease automatic compaction before claiming bounded source PASS. +- Resolve or bound the same-epoch pending-context objection. Its component failure is proved. Its native concurrent-primary reachability is not. +- Preserve the strict checked-origin retry identity, stale-tool vetoes, initial marker provenance correction, and honest auxiliary retry limit. +- No release approval is given. diff --git a/.agents/plans/18-opencode-v2/reviews/core-r7-decisions.tsv b/.agents/plans/18-opencode-v2/reviews/core-r7-decisions.tsv new file mode 100644 index 00000000..dfa16eb9 --- /dev/null +++ b/.agents/plans/18-opencode-v2/reviews/core-r7-decisions.tsv @@ -0,0 +1,13 @@ +ts phase decision why evidence result +2026-10-24T12:25:52Z start Review the frozen core in this thread and write only under /tmp/flow-v2-core-review-r7 Parent forbids delegates, repository writes, pack, and real credentials Round 7 brief; poteto investigation critique with partial critic coverage Read-only review proceeded +2026-10-24T12:25:52Z identity Treat freeze-final bytes as the review subject and rehash before and after Prove It Works. Handoff hashes are claims until the workspace bytes match initial-hashes.json matchFreeze true; close rehash drift empty; entry 2933a7d7; map 7a280c7b; 74 sources Current private entry is the reviewed source. development 0.0.0 +2026-10-24T12:25:52Z tests Run the 16-file focused set on pinned Bun 1.4.0 with the registered-tools Git case filtered Explain the Number. 475 is this file list and this run focused-tests.log 475 pass, 1 filtered, 0 fail, 2396 expect() calls, 16 files, 814ms, EXIT 0 +2026-10-24T12:25:52Z r6-ordinary Require a completed protected tool and null authority in both no-lease states Test behavior. A non-null lease is not success probes/regressions.json four ordinary rows toolRan true, authority null, recovery mode off, saveCount 0, contextError null +2026-10-24T12:25:52Z r6-same-epoch Require the fresh /flow-auto token to survive old context resolve and reject Separate shared state. Pending slots are per owner probes/regressions.json four same-epoch rows Fresh tool ran. Authority stayed local-token epoch 2. Native overlap stays unproved +2026-10-24T12:25:52Z native-manual Independently rerun both ordinary manual summaries on loopback with an isolated home The parent required native proof before accepting the manual path native-independent.json; both result JSON files Summary windows had zero context, request, tool, and prompt rows. Fresh flow_status completed with revision 0, recovery off, and no autoDrive key +2026-10-24T12:25:52Z reason-classifier Keep the non-manual to auto inference as a recorded boundary Type System Discipline and Boundary Discipline. The overload already returns auto for every non-null caller. A host can already send reason auto. Stable unknown completion revokes auto-compaction.ts 38-53; probes/malformed-reason.json; probes/stable-unknown-reason.json; SDK union auto or manual Does not change the source PASS. In-union manual with authority throws. Stable other revokes. Flipped completed auto preserves an existing token +2026-10-24T12:25:52Z guidance-order Accept compactionContext text that is pushed before a live-authority manual throw Attack the Premise. The hook throws, so the summary does not commit probes/regressions.json manual-after-active-lease row; lifecycle.ts 714-716 and 780-784 Error is the manual preservation error. Authority ends null +2026-10-24T12:25:52Z extraction Accept the coding-model helper move The guard is inverted and the operations match. Line ceilings stay inside the existing counter source.patch lines 108-171; execution-attribution.ts 609-631; lifecycle.ts 702; split lengths 993 and 632 Owner byte-identical wording is stronger than the patch. Behavior matches. No ceiling waiver +2026-10-24T12:25:52Z comments Use the supplied inventory and a direct marker scan No-comments. Delegates are forbidden, so this thread does not self-certify a new comment review comment-review-r7-final; rg on the three production files Three public keeps in evals/harness.ts. Zero suppressions. No markers in the three production files +2026-10-24T12:25:52Z provenance Leave prior receipts immutable R1 marker provenance and the rejected d3d46d92 archive stay historical reconciliation marker hashes as given; native artifact-identity.json No archive bytes rewritten. d3d46d92 was the dependency cache only +2026-10-24T12:25:52Z self-audit Supersede any reading that treats this trail as release approval or as proof of unrun native cases Rows were recorded at report close from artifacts. Live per-decision clocks were not kept This file; report.md Bounded source PASS stands. Auxiliary retry, native context overlap, ordinary-automatic native rerun, five marked native reruns, root canonical suites, visual proof, and pack remain outside this verdict diff --git a/.agents/plans/18-opencode-v2/reviews/core-r7.md b/.agents/plans/18-opencode-v2/reviews/core-r7.md new file mode 100644 index 00000000..2eb39e94 --- /dev/null +++ b/.agents/plans/18-opencode-v2/reviews/core-r7.md @@ -0,0 +1,168 @@ +# Round 7 core source review + +## Verdict + +Bounded PASS on the current core source. + +This is a source review of the frozen private entry. It is not release approval, not a packed 10.0.0 claim, and not proof of Linux CI, the visual gate, paid qualification, or a final archive. + +No blocking source defect remained in the reviewed matrix. One classifier boundary is recorded below and stays outside the verdict. + +## Source identity + +Workspace head is `8103f1cd42e1b4d111f25d2a311935d4e41fb997` on `feat/opencode-v2`. The dirty tree was left untouched. + +Freeze is `/tmp/flow-v2-manual-compaction-check-su6aydtb/freeze-final.json`. + +| File | SHA256 | Delimiter lines | +| --- | --- | --- | +| `src/platform/opencode/lifecycle.ts` | `b9b0f73ca1adb6b0ae927ba8fed6f385cd52ba57cba7d926df8397df3b79241c` | 993 | +| `src/platform/opencode/auto-compaction.ts` | `22992613ba7763141145f77136172530a0eea82e5b8ac639081e7b2fa548ea22` | 129 | +| `src/platform/opencode/execution-attribution.ts` | `eb40ecfc0dbbffa7fed954a9af407896555d4a9360944873b1cb1d0dd9a5691f` | 632 | +| `src/platform/opencode/auto-drive.ts` | `4b482e848c43b44506870c03c03612eb023f833296b439a67a3071baf163da4f` | 998 | +| `src/platform/opencode/command-hook.ts` | `94fff32cf1cb63ffd7903db76b38615f7afe50eeb3ef488676be86058eaeba68` | 247 | +| `src/application/recovery-policy.ts` | `f616bd10908d49c55e44c3bcfc3315d675cdac8164c65b775f7869e138541b75` | 1000 | +| `src/platform/opencode/auto-drive-messages.ts` | `262002ccf54a74d2a1b2106bde420d96ba279fb7b00d449d95350acca5fc29c3` | 39 | +| `tests/lifecycle.test.ts` | `8700196a86a7561c8544bd31965fca13c4bd25f8daa4fef179d91cdd3499af71` | 3305 | + +`initial-hashes.json` recorded `matchFreeze: true` before the probes. The close rehash of these eight files returned `drift []`. + +Private entry SHA256 `2933a7d739742518905ba6ca41d05ef628dafc34b9d1a39c8c716f8a57518f7c`. Source map SHA256 `7a280c7b8e2ea45c045971eae7dbb83daffdde5deae7451513479b5adb74a371`. Both native runs bound 74 `sourcesContent` entries with that map. Observed `packageVersion` is `0.0.0`. + +The tarball SHA256 `d3d46d92148a34d829536c939a528f52c3ef6adeb612230afab860b003c09292` was the dependency cache. The loaded plugin path was the copied current entry. That archive stays rejected. + +## Design that the source implements + +Checked native controls keep the accepted input origin. `checkRunningAutoCompaction` in `auto-compaction.ts` throws at lines 38-45 when the last compaction is absent, not running, or manual while authority is non-null. Manual with null authority returns reason `manual` and authority null at lines 46-52. Every other running reason is stored as reason `auto` with the passed authority at line 53. + +`checkCompletedAutoCompaction` requires the same id, the same stored reason, status completed, and a consumed prefix of that single id, with no later user or synthetic message. + +`isUnattributedManualSummary` is true only for a running manual control whose prefix matches `nativeInputIDs`. The compaction hook in `lifecycle.ts` lines 733-746 uses that path only when the consumed source has no admission epoch and `compactionContext` is null. The return stores no `compactions` entry and no `compactionOrigins` entry. The catch at lines 790-792 calls `failOwned` only after `owner` is assigned, which is after that return. An unknown summary callback therefore has no host cleanup owner. + +`contextPending` removal at lines 707-709 deletes only the slot whose owner object is still current. `rememberCodingModel` runs at line 702 after `assertOwned` and `assertLive`. + +`compactedInputOrigin` copies authority from the source. A truthy authority forces `automatic: true`. It does not read the current host grant. + +Grantless completion calls `recovery.observeMessage(session, checked.id, true, true)` from the existing resolver. The lifecycle tests that passed expect recovery mode `off`, no new repository save, and no advice lease. + +The public registration surface is unchanged by this delta. `command-hook.ts` and `auto-drive.ts` are outside the current patch and remain under the 1000-line counter. `recovery-policy.ts` is 1000 and was not part of the patch. + +## Independent native manual proof + +OpenCode `v2.1.4` listened on `127.0.0.1`. Home, XDG, and TMPDIR were under each run directory. Provider traffic stayed on the paired loopback port. Copied runner base is `/tmp/flow-v2-core-review-r7/native-runner`. The entry file is a byte copy. `node_modules` is a symlink used only so `zod` resolves. Owner freeze files were not written. + +Setup misses retained as misses. A symlinked entry failed the owned-path assertion. Run `run-20261024-141941-648312` then failed plugin load with `Cannot find package 'zod'`. Those processes were stopped before the successful runs. + +### no-prior + +Run `/tmp/flow-v2-core-review-r7/native-runner/native/run-20261024-142027-385044`. Session `ses_e92250ce9ffek05ghlEQVmDpzF`. Server PID 38063 exit 130. Provider PID 38062 exit -15. `bothAwaited` true. + +During the summary window, `contexts`, `requests`, `tools`, and `prompts` are empty. The API receipt type is `compaction` and has no `reason` field. The native running control `msg_16ddaf62d001VEmvlUFQYnCF2l` has status `running` and reason `manual`. The completed context message has the same id, status `completed`, and reason `manual`. `.flow/session.json` was absent. The compaction system text does not contain the in-memory `/flow-auto` continuation. + +A later inbox user, text `Fresh ordinary coding input after completed manual summary.`, metadata `{}`, was consumed together with that compaction id. `flow_status` call `call_manual_fresh` completed. Parsed status is `ok`, summary `No active Flow session.`, revision 0, recovery `{mode: off}`, and `autoDrive` is absent. The script writes `successAfterFreshUser: true` only after those assertions. + +### ended + +Run `/tmp/flow-v2-core-review-r7/native-runner/native/run-20261024-142035-536385`. Session `ses_e9224ed17ffeJQ38f0fkabShUK`. Server PID 38086 exit 130. Provider PID 38085 exit -15. The same summary and fresh-tool facts hold for control `msg_16ddb14a7001fCmvb9HLwlwRcq`. + +After both runs, `pgrep` for this review runner printed no processes. PID 76572 was still present and was not signaled. + +Index of the parsed fields is `native-independent.json`. + +## Focused tests + +Command, cwd `/Users/vriesd/projects/flow-opencode`, `PATH` pinned to `/tmp/flow-v2-toolchain/node_modules/.bin` first, `HOME` `/tmp/flow-v2-core-review-r7/home`, `TMPDIR` and `XDG_*` under the same review directory: + +``` +/tmp/flow-v2-toolchain/node_modules/.bin/bun test \ + tests/auto-drive.test.ts \ + tests/auto-drive-decision.test.ts \ + tests/auto-drive-validation.test.ts \ + tests/execution-attribution.test.ts \ + tests/lifecycle.test.ts \ + tests/recovery-host.test.ts \ + tests/recovery-authority.test.ts \ + tests/recovery-policy.test.ts \ + tests/opencode-native-core.test.ts \ + tests/validation-capture.test.ts \ + tests/tool-parentage.test.ts \ + tests/config-shared.test.ts \ + tests/model-preferences.test.ts \ + tests/model-preferences-store.test.ts \ + tests/model-picker.test.ts \ + tests/architecture-boundaries.test.ts \ + --test-name-pattern '^(?!registered tools share)' +``` + +Bun `1.4.0` (`34cbb9a40`). Log `focused-tests.log`. Result is 475 pass, 1 filtered, 0 fail, 2396 `expect()` calls, 16 files, 814ms, EXIT 0. + +475 counts this file list and this filter. The filtered test is the Git-backed `registered tools share` case. The count is not the owner 336, not root's earlier 4643+27, and not a whole-suite result. The owner 336 and the nine owner native cases stay owner evidence except for the two manual cases rerun here. + +The passing lifecycle names cover pre-primary pending receipts, ordinary none and ended-manual first and retry, grantless plan and reviewer, declassify paths, prefix and reason and model mismatches, grantless recovery bookkeeping, known manual checkpoint identity, unattributed summary then a later real input, synthetic and prefix history, delayed read resolve and reject, unload, late callbacks, and the 128-model bound. + +## R6 probes copied into this output + +`probes/regressions.test.ts` imports the workspace checker and the freeze fixture. It does not modify `/tmp/flow-v2-core-review-r6`. + +Nine behavioral rows passed and are in `probes/regressions.json`. + +- Four ordinary rows, prior none and ended-manual, with and without the automatic control. Each has `toolRan` true, authority null, `saveCount` 0, advice mode `off`, and `contextError` null. +- Four same-epoch rows. Old context reject and resolve, including the old read settling before the fresh context. The fresh protected tool ran. Authority object stayed `{hostSessionId: session-lifecycle, token: local-token, epoch: 2}`. +- One live-lease manual row. Error text is `Flow cannot retain authority through unverified or manual compaction.` Authority is null. The guidance string is present on `input.system` because `lifecycle.ts` lines 715-716 push `compactionContext` before the running check. The hook then throws, so that push is not a committed grantless summary. + +The tenth expect failed on purpose as a reviewer hypothesis. `checkRunningAutoCompaction` accepted running reason `other` and returned reason `auto` with the same authority object. See the residual below. Probe log exit is 1 because of that expect. + +`probes/malformed-reason.test.ts` drove the lifecycle with running reason `other` and then rewrote the completed control reason to `auto`. `runningError` is null. `flow_status` ran. Token `local-token` epoch 2 was preserved. That file's expect also failed. The JSON is `probes/malformed-reason.json`. + +`probes/stable-unknown-reason.test.ts` kept reason `other` on the completed control. Context error is `Flow automatic compaction control identity changed.` The tool did not run. Authority after the attempt is null. EXIT 0. + +## Residual that does not change PASS + +`auto-compaction.ts` lines 46-53 label every non-manual running reason as `auto` and return the caller authority. + +What that does: + +- Manual plus non-null authority still throws at lines 41-44. +- Null authority is not turned into a lease by the label. `compactedInputOrigin` keeps authority null when the source authority is missing. +- A stable unknown reason on the completed control fails the reason match at lines 62-70 and `failOwned` revokes the live lease. +- The component grant needs a running reason outside `manual` and a completed reason of the string `auto`. The probe created that pair by cast and then by mutating the completed snapshot. +- `@opencode/client` types the compaction reason as `"auto" | "manual"` at `types.d.ts` lines 601, 750, 1226, 1999, 2018, and 2041. +- Both independent native cases supplied reason `manual` from `Session.context`. The receipt had no reason field. + +A host that can emit a completed reason of `auto` can already emit a running reason of `auto`. The else branch does not add a grant that an in-union auto control lacks. The stated overload for a non-null caller is auto-only. This stays a recorded boundary. A later pack review can still require `reason === "auto"` in the throw condition if it wants unknown runtime strings to fail closed. That change is not required for this source verdict. + +## Helper move + +`source.patch` deletes the inline model update in lifecycle and adds `rememberCodingModel`. The new guard returns on parent, agent mismatch, or specialist. That is the inverse of the old positive condition. Delete-before-set, variant copy, and eviction of the oldest key above 128 are the same operations. The call remains after ownership and liveness checks. Lifecycle stays at 993 delimiter lines. This is a move across the existing ceiling. It is not a waiver and it is not a byte-identical paste. The bound test in `tests/lifecycle.test.ts` passed. + +## Comments + +`/tmp/flow-v2-comment-review-r7-final/suppressions.json` is an empty list. `comments.json` has three entries, all in `evals/harness.ts` at starts 189, 355, and 1844. Those are existing public contract keeps and sit outside this core fence. A direct scan of `lifecycle.ts`, `auto-compaction.ts`, and `execution-attribution.ts` found no `//`, block comment, `@ts-`, `biome-ignore`, or `eslint-disable` marker. No comment subagent was started. + +## Prior findings left as they were + +R1 stream, setup cancel, read, and no-body closures stay closed on their reported sequences. The R1 missing-marker finding does not describe lifecycle `1e8d5991dcaa756ef19484a6219dcca980551f201d21676c63534019af463772`. Archive `0e4ba80b` with lifecycle `188ec48417cef20250e9208cf69e3eaa9225e15f2d6b1331f4b18a630164fd35` is the source that lacked the marker. Those hashes are carried from reconciliation and were not recomputed here. + +R2 response-less failures stay closed on their reported sequences. R3 origin recreation stays closed by the R4 checked-fact WeakMap. R5 is a bounded PASS on older source only. R6 remains ISSUES on source `169609bd` and its ordinary no-lease automatic P2. This review reran that component failure as passing on the current bytes, in both no-lease states. The R6 same-epoch component failures also pass on the current bytes. Native overlapping-context reachability stays unproved. + +The rejected archive `d3d46d92` and its QA receipts were not rewritten. Root canonical suites from the R6 source are not evidence for this fix. Native-account helper review R3 and client `e8e4bed7` were not re-reviewed. + +## Limits + +Actual same-model auxiliary generate retry remains unproved. The negative component coverage and the older reentrant attempt are unchanged. + +Native overlapping context remains unproved. The same-epoch proof is the component fixture. + +Ordinary automatic native cases and the five marked native scenarios were not rerun in this round. Their owner receipts are owner evidence. The two manual native cases above are this review's evidence. + +No final archive contains this source. No root typecheck, full suite, build, pack, install, or Git operation ran. CUA visual proof was not available. The future `TYPESAFE_API_KEY` option was not reviewed and does not gate this verdict. Eval and native-auth edits outside the fence were not treated as core proof. + +Collection exit 0 is not the product proof. The product proof is the parsed result fields and the assertions that run before `successAfterFreshUser` is written. + +## Attention + +Cross-model trail review was unavailable. The parent forbade delegates, so critique stayed in this thread. Critic coverage is partial. + +Principles that changed a decision were Prove It Works, Attack the Premise, Type System Discipline, Boundary Discipline, Separate Before Serializing Shared State, Test Behavior, Explain the Number, and No-comments. + +throughput checkpoint: n/a, read-only investigation. diff --git a/.agents/plans/18-opencode-v2/reviews/core-r8.md b/.agents/plans/18-opencode-v2/reviews/core-r8.md new file mode 100644 index 00000000..0d7589c2 --- /dev/null +++ b/.agents/plans/18-opencode-v2/reviews/core-r8.md @@ -0,0 +1,38 @@ +# Core review R8 + +The fresh independent reviewer was Codex gpt-6.1-sol with high reasoning effort +and priority service. Verdict is bounded PASS. No actionable causal source +finding or corrective source change was identified. + +The parent read the full report and decision trail. The original report remains +at `/tmp/flow-v2-core-review-r8/report.md`, with SHA-256 +`444b09db9873198b26d50e5d5ca3c1f391bb7de030594648a71fcf788f331dca`. +The reviewer ran 496 offline tests with one filtered Git fixture, zero failures, +and 2,507 assertions across 17 files. This is fresh post-comment execution, not +another full canonical or native run. The parent canonical checks cover that Git +fixture on the pre-comment bytes. + +The reviewer recomputed the exact fc19ca4a archive and 667d6029 unpacked manifest, +matched all twelve installed files, 75 server and five TUI source rows across +77 unique project paths, and verified the supplemental ten-file freeze. No +executable source or archive drift occurred. The sole supplemental change deletes +one narration line from a test after the runtime audits closed. + +The review audited the raw decisive retained native observations, proof bindings, +prior findings and responses, both R8 designs, cross-judge, synthesis, and census +provenance correction. It did not claim a fresh native execution. The exact +interrupted ordinary/manual sequence completes without reload, grants no summary +execution authority, and permits only a separately accepted fresh input's tool. +Stable repeated origins, automatic tombstones, exact readiness, frontier +cancellation, and source-owned cleanup remain checked. + +The architecture split is justified by knowledge ownership. Attribution and the +input ledger could co-locate below the line ceiling. Mathematical necessity is +not claimed. All old admission APIs and duplicated lookup maps are deleted. + +Same-model auxiliary retry, native overlapping-context reachability, and unread +original-control correlation remain unproved. Trailing-history and pre-dispatch +concurrency cases are component evidence. The reviewer found no demonstrated +source defect implied by these limits within the declared contract. Review of +its own decision trail was a self-audit; no second agent reviewed that trail. +This is not actual-account, paid qualification, publication, or release approval. diff --git a/.agents/plans/18-opencode-v2/reviews/eval-r1.md b/.agents/plans/18-opencode-v2/reviews/eval-r1.md new file mode 100644 index 00000000..8f5fdfe6 --- /dev/null +++ b/.agents/plans/18-opencode-v2/reviews/eval-r1.md @@ -0,0 +1,19 @@ +ISSUES. One independently confirmed P2 remains in the reviewed snapshot. This is a read-only verdict on the hashes in [source-reviewed.json](source-reviewed.json), not freeze or release approval. + +P2. Recovery credential inheritance has no native handoff in the caller API. [episode-host.ts:119](/Users/vriesd/projects/flow-opencode/evals/recovery-decisions/episode-host.ts:119) accepts `providerCredentials: "inherit"` but omits `credentialHandoff` from `EpisodeHostOptions`. Its start path passes that object to [harness.ts:2011](/Users/vriesd/projects/flow-opencode/evals/harness.ts:2011), which now rejects inheritance without a handoff. Existing registration accepts this policy, so preparation fails for an advertised live recovery path. Expose the native handoff through the caller and wire an explicitly selected credential snapshot and rotation callback. Keep native credential scoping and simulation isolation. Do not restore V1 `auth.json` copying. [The reproduction](boundary-repro.test.ts) confirms refusal before any spawn. The caller omission is established from source. + +The parent fixed the ambient-config finding during review. Both release preflight and scenario hosts now pass `ambientConfig: "disabled"`. The launch-boundary reproduction confirms removal of `OPENCODE_CONFIG`, `OPENCODE_CONFIG_CONTENT`, and `OPENCODE_CONFIG_DIR`. A separate HOME alone did not provide that isolation. + +Transport correction is still a parent integration item. The saved [native prototype receipt](transport-verification-reviewed.json) falsifies the no-op session HTTP-hook approach. Bare and no-op-hook modes each make one WebSocket upgrade and zero HTTP requests. Updating provider and model settings produces zero upgrades and one HTTP request. The forced prototype still contains the no-op hook and registers its settings transform after the fixture provider. Test the exact final helper and preserve transform ordering. Apply the same constraint to both study arms and preflight. Seal its source through a real import edge or an explicit bundle root. The source-bundle scanner does not follow `new URL` alone. Remove unsupported native-LLM/experimental-flag claims from prospective contracts. Historical branches and hashes stay intact. This receipt proves transport selection, not full inference attribution or real-provider qualification. + +The evidence path otherwise passed this review's focused checks. Native V2 messages retain endpoint-owned session binding, exact location checks, composite tool provenance and raw timestamps. The harness supplies raw native messages at both provenance sites. Grader, actor, auto/delivery, reviewer and abort matching use those identities. Original packet bytes remain separate evidence. Missing initial markers and unproved compaction continuity stay ineligible. Usage reduction retains disjoint categories and missing-data limitations. + +The final focused run passed 351 tests and 1,504 assertions across 14 files. It recorded no source drift during execution. Three launch-boundary reproductions, two historical/comparison tests, and 62 study-statistics tests also passed. Commands and logs are retained beside this report. V1 trace/provenance JSON schemas and both historical study hashes match the base. Historical capture and qualification directories have no tracked changes. Replaying the saved native capture passed exact tool-witness checks and correctly rejected its missing initial marker. + +The parent changed `harness.ts`, `run.ts`, and `release-policy.ts` while this review ran. Start hashes, reviewed hashes, exact source copies and drift patches are retained. The prospective transport rewrite is not part of this completed source snapshot and needs a focused recheck before freeze. + +No full-suite, final packed, Linux, paid qualification, or release approval claim is made. No native host, external network, paid model, connector or secret was used. All writes stayed under `/tmp/flow-v2-eval-review-r1`. No Git or repository writes and no delegates were used. Canary and broader release-policy review stayed with their existing owner. + +Boundary Discipline prioritized external evidence and environment boundaries. Prove It Works required actual bytes and offline checks. Test Behavior, Not Implementation shaped the literal environment/refusal assertions. Build the Lever produced the rerunnable review scripts. Full Poteto instructions and the applicable leaf skills were read. The user's no-delegate and no-comment-style-review instructions governed those scope choices. + +[Full structured report](report.json). [Final test log](focused-tests-final.log). [Reviewed source hashes](source-reviewed.json). [Historical checks](history-contracts.json). [Boundary observations](boundary-repro.json). diff --git a/.agents/plans/18-opencode-v2/reviews/eval-r2.md b/.agents/plans/18-opencode-v2/reviews/eval-r2.md new file mode 100644 index 00000000..67e3b628 --- /dev/null +++ b/.agents/plans/18-opencode-v2/reviews/eval-r2.md @@ -0,0 +1,98 @@ +# Flow 10.0.0 V2 eval review, round 2 + +Verdict: ISSUES. Two P2 findings are on the bytes read at `8103f1cd42e1b4d111f25d2a311935d4e41fb997`. This is not a freeze and not a release approval. + +The round 1 caller omission is closed. `EpisodeHostOptions` now picks `credentialHandoff`. Inherit without a handoff is refused before `EvalHost.start`. A handoff with credentials disabled is refused. The harness returns `V2 credential inheritance requires an explicit native credentialHandoff; auth.json is unsupported.` No `auth.json` copy runs on that path. + +## Overview + +The review covered the eval host, native trace conversion, study protocol, release host policy, HTTP transport selection, and recovery startup. The working tree is dirty: 112 paths differ from the base commit. Hashes of the 28 scoped files are in `source-reviewed.json`. None of those hashes changed between the start of the repro and the check after the tests. + +Pinned Bun was 1.4.0 from `/tmp/flow-v2-toolchain/node_modules/.bin/bun`. The shell Bun 1.3.14 was not used. + +## Key concepts + +Native credential inheritance is an explicit snapshot plus an `onStop` callback. The child home stays under the scratch directory. Rotation has to be copied out before that directory is removed. + +Release isolation is two separate switches. `ambientConfig: "disabled"` deletes `OPENCODE_CONFIG`, `OPENCODE_CONFIG_CONTENT`, and `OPENCODE_CONFIG_DIR`. `reviewerEnvironment: "disabled"` blanks `OPENCODE_FLOW_REVIEWER_MODEL`, `OPENCODE_FLOW_REVIEWER_VARIANT`, and `OPENCODE_FLOW_REVIEWER_STEPS`. The release campaign sets only the first switch. + +HTTP selection is one provider transform. It writes `transport: "http"` onto each provider, model, and variant already in the editor. A provider added by a later transform is left alone. + +## How the review ran + +`/tmp/flow-v2-eval-review-r2/repro.ts` constructed `EvalHost`, drove `stop` with a fake client, called the real credential rejects, resolved reviewer configuration, walked the grader import graph, and built the live treatment plugin into this directory. It did not spawn OpenCode. + +Focused tests, same Bun: + +`bun test tests/recovery-episode-host.test.ts tests/config-shared.test.ts tests/eval-host-trace.test.ts tests/eval-v2-trace-contract.test.ts tests/study-manifest.test.ts tests/study-host-isolation.test.ts` + +114 pass, 0 fail, 468 assertions, 6 files, 6.99 seconds. The isolation test used the in-repo loopback fake. It refused any origin other than its own server. + +## Where the code lives + +Credential shutdown is `evals/harness.ts` `stopOnce`, lines 3616 to 3677. The caller type is `evals/recovery-decisions/episode-host.ts` lines 112 to 122, with the early rejects at lines 180 to 189. + +Release start is `evals/run.ts` lines 1109 to 1126. Preflight is lines 824 to 831. The override gate is `evals/release-policy.ts` lines 463 to 472. Reviewer precedence is `src/config-shared.ts` lines 200 to 259. The child environment copy is `evals/bun-toolchain.ts` lines 62 to 70 and `evals/harness.ts` lines 1980 to 1987 and 2259 to 2268. + +HTTP registration is `src/platform/opencode/http-transport.ts`. Flow calls it first inside `createFlowPlugin`. The control arm imports `evals/http-transport-plugin.ts`. Plugin readiness is `evals/harness.ts` lines 2372 to 2412. + +## Findings + +### P2. Shutdown drops credential rotation when session drain fails + +`stopOnce` puts `session.active`, every interrupt, every wait, and `credential.list` in one promise. A rejection from active or wait skips the list. The method then kills the server, rethrows, and skips `onStop`. `rm` of the scratch directory is after that throw, so the directory stays. + +Repro, real `stop` method, fake client, no server process: + +- `session.active` throws `active-stuck`. `credential.list` was not called. `onStop` was not called. Scratch remained. +- An active session whose `session.wait` throws `wait-timeout` did the same. +- An idle session listed the rotated credential, called `onStop` with that list, and removed scratch. + +`tests/recovery-episode-host.test.ts` still passes. Its fake `stop` calls `onStop` itself, so it never runs this branch. Registered callers still default `providerCredentials` to disabled. Inherit is opt-in. The hole is on the handoff this round exposed. + +Minimum fix: while the server is alive, list credentials even if interrupt or wait fails. Call `onStop` when the list succeeds. Remove scratch only after `onStop` succeeds. + +### P2. Release scenario host applies an ambient reviewer variant + +`runCampaign` rejects `OPENCODE_FLOW_REVIEWER_MODEL` and `OPENCODE_FLOW_REVIEWER_STEPS` through `assertReleaseHost`. That function has no variant argument. A Linux call for package 10.0.0 with neither override is accepted. The same call rejects a model override and a steps override. + +The release scenario start sets `ambientConfig: "disabled"` and leaves `reviewerEnvironment` unset. The ordinary host copies `process.env` through `currentBunToolchain`. Reviewer variables are blanked only when `reviewerEnvironment` is `"disabled"`. Release plugin options carry the pinned model and omit variant and steps. `resolveFlowReviewerConfiguration` then reads the environment. The variant applies because the model is explicit. + +Repro: plugin model `openai/gpt-6.1-sol` plus `OPENCODE_FLOW_REVIEWER_VARIANT=max` and `OPENCODE_FLOW_REVIEWER_STEPS=3` returned variant source `environment` value `max`, and steps source `environment` value 3. Blank strings left the model pinned, omitted the variant, and left steps at `host-default`. + +The campaign gate stops a steps override before launch. It does not stop a variant. The release host-config digest records `reviewerSteps: null` and does not record a variant. Preflight also omits `reviewerEnvironment`. A variant alone does not apply there, because preflight passes no reviewer model. + +Minimum fix: pass `reviewerEnvironment: "disabled"` on the release preflight start and the release scenario start. Reject a set `OPENCODE_FLOW_REVIEWER_VARIANT` in `assertReleaseHost`, alongside model and steps. + +### P3. Benchmark and reviewer campaign starts inherit the parent environment + +`package.json` script `benchmark` runs `evals/benchmark-run.ts`. Its `EvalHost.start` calls at lines 451 and 520 pass neither `ambientConfig: "disabled"` nor `reviewerEnvironment: "disabled"`. `evals/reviewer-run.ts` line 294 passes a reviewer model and omits both flags. Study cells and episode hosts set both. The release scenario sets ambient config only. + +Minimum fix: pass both flags at those three starts. + +## What held + +HTTP. A provider, model, and variant present before `registerHttpTransport` all became `transport: "http"`, including a variant that started as `force-ws`. A provider inserted after that callback stayed `websocket`. Undefined `variants` throws, so the transform fails closed. Flow registers this helper and no later `provider.transform`. The budget plugin hooks fetch and the websocket constructor. It does not transform providers. `withFlow: false` loads only the HTTP plugin. + +The grader walker returned 108 files, including `evals/http-transport-plugin.ts` and `src/platform/opencode/http-transport.ts`. A Bun build of `evals/recovery-decisions/live-treatment-plugin.ts` succeeded as one 2,106,754 byte file. Live `effect/...` imports: 0. The `effect` strings in that file are schema `importDeclarations`. Executable imports are Bun builtins. Neither treatment `Bun.build` call contains an `external` array. + +Plugin readiness accepts the realpath of the configured directory, or the directory name when the listed path ends in `index.ts` or `index.js`. Other paths are ignored. A failed expected plugin throws `An expected native evaluation plugin failed to load.` The isolation fake lists the package directory, so this run did not exercise the `index.ts` fallback. `realpath` is not caught. A listed path that cannot be resolved aborts the poll immediately. + +V1 `HostTrace` schema text matches the base schema after the rename to `HostTraceV1Schema`. `evals/study-statistics.ts` has the same git object id as the base, `be5d51635327a750bac7556a7784ac2269c7a79c`. The legacy and previous study protocol payloads match the base. Active `prepareStudyManifest` refuses OpenCode 1.18.6. The 1.18.6 host record still sets `nativeLlm: false`. Newer records set `transport: "http"`. + +Current protocol digests from this process: + +- legacy `sha256:a0f3454c6370992dfb952160fbba577a856eb8f920a4a49460ac9f8f83927940` +- study `sha256:147c51db430de30bd88a94c5fb4a4ef9b1ff5fac7ae3c05aa2af6f60aafff6d9` +- opencode2 `sha256:31d303b0a7e0132d5fe9b164acdb813ef49bd6a8fe29035ccae600bbeeda36c8` +- statistics `sha256:50795aa7b68e61c86840c0ecd3c0ef52bc835506ed4c1f38f31000181e7dda44` + +The saved packed proof at `/tmp/flow-v2-packed-eval-parent/validation.json` stores native input 10, output 5, reasoning 4, cache read 3, cache write 0, `costUsd` null, completeness `unverified`. The harness adds those five fields separately. That file was read. The host was not run again. + +Trace tests passed, including endpoint-owned session binding, composite tool identity, missing initial marker, and compaction left unproved. Both harness provenance calls pass `entry.native`. No historical evidence directory is in the diff against the base. + +## Limits + +No OpenCode server, no network, no paid model, no full suite, no Linux run, no packed qualification, no release approval. The earlier socket probe under `/tmp/flow-v2-http-contract` was not repeated. Canary policy and the src stream work stayed with their owners. The missing initial flow-token gate was left as it is. Comment wording was out of scope. + +`report.json` holds the raw repro objects. `commands.txt` holds the commands. `source-reviewed.json` holds the file hashes. diff --git a/.agents/plans/18-opencode-v2/reviews/eval-r3.md b/.agents/plans/18-opencode-v2/reviews/eval-r3.md new file mode 100644 index 00000000..df2b3423 --- /dev/null +++ b/.agents/plans/18-opencode-v2/reviews/eval-r3.md @@ -0,0 +1,52 @@ +PASS for the bounded round 3 source review. No actionable correctness finding remains on the reviewed bytes. This verdict closes the scoped R2 credential-drain, ambient-reviewer-variant, and benchmark/reviewer environment findings. It is not a source freeze, real-account handoff approval, provider qualification, or release approval. + +The repository stayed read-only. Reviewer writes stayed under `/tmp/flow-v2-eval-review-r3`. No delegate, host process, connector, paid model call, credential read, repository build, package install, or pack ran. Tests used Bun 1.4.0. The original R1 and R2 reports were read and left unchanged. + +Overview. + +The native host accepts an explicit credential snapshot and callbacks. Startup forwards `onImport` as a function, calls it immediately before cloning/importing native entries, and installs the shutdown handoff before the first native create. Shutdown drains separately from credential export. It exports while the server is available, terminates the server, delivers a successful snapshot, settles project retention, and removes scratch only after successful cleanup. + +Reviewer configuration isolation has two independent controls. The release gate now rejects a reviewer variant override. Release preflight and scenario hosts, both benchmark starts, the reviewer campaign start, study starts, and the recovery episode start disable ambient OpenCode configuration and reviewer environment fallback. + +Key concepts and closed findings. + +- R1 caller omission stays closed at [episode-host.ts:120](/Users/vriesd/projects/flow-opencode/evals/recovery-decisions/episode-host.ts:120). Snapshot data is cloned separately from callbacks at line 165. Inherit without a handoff and simulation credential admission remain rejected. The repository recovery test confirms the selected snapshot is frozen and both callbacks survive forwarding. Its host factory is synthetic. +- R2 credential-drain P2 is closed at [harness.ts:3618](/Users/vriesd/projects/flow-opencode/evals/harness.ts:3618). The original active-query and wait-error reproductions now list and deliver the synthetic rotated snapshot. All five new repository stop modes passed. Additional checks cover interrupt failure, an already-cancelled parent signal, retention failure, combined drain/retention failure, and an uncooperative credential-list promise. Timeout rejects after the 3,000 ms request bound. Failed cleanup retains scratch. Repeated stop cannot bypass a rejected cleanup. +- R2 ambient-reviewer-variant P2 is closed at [release-policy.ts:468](/Users/vriesd/projects/flow-opencode/evals/release-policy.ts:468). [run.ts:911](/Users/vriesd/projects/flow-opencode/evals/run.ts:911) supplies the actual environment variant to the gate. Starts at lines 824 and 1110 disable both fallback sources. The real harness startup method, with in-memory process/client boundaries, removed all three ambient config variables and blanked all three reviewer variables. +- R2 caller-isolation P3 is closed. Both starts in [benchmark-run.ts:451](/Users/vriesd/projects/flow-opencode/evals/benchmark-run.ts:451) and [reviewer-run.ts:294](/Users/vriesd/projects/flow-opencode/evals/reviewer-run.ts:294) set both isolation controls. This caller coverage is source inspection, not a launched campaign. + +How the new boundary behaved. + +[Startup receipts](startup-observations.json) exercise the actual `EvalHost.start` method with fake process creation, a fake port allocator, and a fake native client. No socket opened, no process launched, and Git calls returned synthetic success without performing writes. Seven cases passed. Successful import, an exception from `onImport`, failure on the first create, failure after one successful create, readiness failure, cancellation during import, and version mismatch all reached the expected cleanup. `onImport` preceded the first native create. Failures before import did not fabricate a shutdown handoff. Failures after installing the handoff delivered the available synthetic native snapshot. These checks prove ordering and cleanup of this API, not real native credential import or refresh. + +[Shutdown receipts](shutdown-observations.json) also record a challenged termination failure. A fake process ID and a replaced `process.kill` produced the existing permission-denied error without signalling a process. Credential listing succeeded, finalization callbacks did not run, and scratch remained. I do not classify that as a defect. The retention API requires a quiescent project. Failed termination does not prove quiescence. Calling finalization regardless would need a separate safety contract. The rejection remains memoized, so this is a retained-scratch/manual-recovery path, not a successful handoff. There is no integrated credential source in this reviewed change. I make no claim that an account was copied, refreshed, or synchronized. + +What held across the native evidence integration. + +The [in-memory outcome check](native-outcome.test.ts) drives the real `outcome` reducer. It preserves exact raw UTF-8 tool output and the composite native identity of session, message, content index, and call ID. The resulting grade input passes the native witness boundary. Assistant and compaction counters are added separately. Synthetic totals are input 11, output 7, reasoning 7, cache read 7, and cache write 5. Zero reported cost with nonzero usage becomes unknown. This is not provider-normalization or invoice evidence. + +The pure V2 trace, actor, reviewer-access, and grader checks passed. Endpoint-owned message binding remains required. An explicit conflicting session binding is rejected. Native location equality, raw timestamps, message-level user metadata, and the absence of fabricated part IDs and assistant parents remain intact. Both harness provenance calls supply `entry.native`. Composite witnesses bind all relevant grader paths. Original reviewer packet bytes remain separate evidence. Missing initial markers and compaction continuity without delivery proof remain ineligible. My own initially invalid message IDs were rejected by the native schema; correcting the synthetic fixture made the outcome check pass. + +The [history check](history-contracts.json) compares both V1 serialized JSON schemas with source read directly from base `8103f1cd42e1b4d111f25d2a311935d4e41fb997`. They match exactly. Both historical study protocol digests match. `study-statistics.ts` and `canonical-json.ts` match the base bytes. The old policies remain readable, while active V1 study preparation is rejected. New study preparation uses the V3 protocol digest and the OpenCode 2.1.4 disjoint accounting contract. Literal counters 10, 5, 4, 3, 0 produce generated output 9 and total tokens 22. Completeness remains unverified and cost remains unknown. Base modules had only their import paths rewritten in this review directory; their unchanged dependencies are listed in the receipt. + +No tracked path under `evals/captures`, `evals/qualification`, or `evals/cassettes` differs from the base. This is a tracked-byte comparison, not a claim about all untracked files. Historical evidence was not rewritten. + +HTTP transport remains a provider transform over providers, models, and variants already present. The ordinary control helper and shared implementation remain in the grader source bundle. Future providers added after that transform are not covered. This round inspected those unchanged bytes and the source-bundle regression. It did not repeat a socket probe or prove an official provider's actual transport. Budget tests use injected transports and prove route rejection, reservation-before-dispatch, cancellation, immutable scopes, and consumed reservations after failures. They do not prove a live budget-plugin installation. + +Verification and drift. + +[Commands](commands.json) retain the exact argv, exit status, environment-key allowlist, and log for every check. Test processes received a scratch HOME and TMPDIR. An offline preload denied fetch, socket servers, WebSockets, and unapproved subprocesses. Allowed real subprocesses were Bun version probes and a scratch FIFO fixture. The startup-specific test replaced process and port operations entirely. + +The focused runs ultimately passed 257 distinct repository tests and 16 review checks. Four socket tests and two subprocess budget tests were explicitly filtered out. Typecheck passed with `bun node_modules/typescript/bin/tsc --noEmit --incremental false`. This is not one green whole-suite run. + +The first focused run had 218 pass and three artifact-test failures caused by my guard intercepting Bun's internal subprocess primitive. After correcting the guard, all seven artifact tests passed. The initial startup script omitted a required mocked export. The initial outcome fixture used IDs that failed native schema validation, then a diagnostic import used an incorrect Effect path. Corrected review scripts passed. One malformed typecheck invocation, `bun x /dev/null`, stopped at dependency-format parsing; the correct local compiler invocation passed. These failures remain in the command receipts and are not repository findings. + +[Start hashes](source-start.json) and [end hashes](source-end.json) cover 78 inventoried files, including all 28 production integration files from R2. Drift is empty. Exactly six production files differ from the R2 hashes, matching the stated freeze changes. The source copies under `source/` retain the reviewed production bytes. Additional dependency/test hashes are closeout-only and make no before/after claim. + +Git status gained `tests/release-940-preparation.test.ts`, matching the parent's reported concurrent Bun YAML fix. That test is outside the production review and outside the 78-file hash inventory. I did not run its ten tests or verify the parent's Colima/Linux results. No package or lock change is attributed to that fix by this review. + +Limits and judgment. + +No full check, live OpenCode launch, actual credential source, private export bank, broker, OAuth refresh, official-provider request, packed qualification, Linux execution, canary review, or release approval is established here. The parent still needs an independent review of final main auth wiring. Core lifecycle fixes remain with their owner. Unread V1 environment switches are not treated as enforcement. + +Boundary Discipline kept the review at actual admission, evidence, and environment boundaries. Prove It Works required execution of the real startup, shutdown, and outcome methods within the offline limit. Test Behavior, Not Implementation required literal callback results, retained scratch, exact bytes, and native identities. Build the Lever produced the rerunnable scripts and hash inventory. The user's no-delegate and no-comment-style instructions overrode those optional skill workflows. No model diversity or additional review lane is claimed. diff --git a/.agents/plans/18-opencode-v2/reviews/native-auth-r1-decisions.tsv b/.agents/plans/18-opencode-v2/reviews/native-auth-r1-decisions.tsv new file mode 100644 index 00000000..b3cc3d52 --- /dev/null +++ b/.agents/plans/18-opencode-v2/reviews/native-auth-r1-decisions.tsv @@ -0,0 +1,11 @@ +ts phase decision why evidence result +2026-10-24T10:00:00Z scope Read the repo and write only under /tmp/flow-v2-native-auth-review-r1 Parent forbade repo writes, git, real accounts, and pid 76572 review brief held +2026-10-24T10:20:00Z hypothesis Treat the aborted-primary bind as a hypothesis until a hook continuation is observed prove-it-works and separate-before-serializing-shared-state native PluginHooks.trigger plus Effect.promise later hook skipped +2026-10-24T10:28:00Z repro File broker dispose as P2 Identity failure and DELETE failure both skip rm while the recorded pid is alive /tmp/flow-v2-native-auth-review-r1/repro-results.json brokerLiveIdentityFailure and brokerDeleteFailure P2 +2026-10-24T10:28:00Z repro File host dispose as P3 EISDIR from configuration rm leaves the attachment directory repro-results.json hostDispose P3 +2026-10-24T10:28:00Z repro File duplicate method key as P3 fail-closed One registration and zero relay calls repro-results.json clientCollapse P3 +2026-10-24T10:28:00Z repro Withdraw concurrent source resolve maxResolveDepth is 1 across 26 entries repro-results.json sourceStress withdrawn +2026-10-24T10:31:00Z hook Do not file Request rebinding on abort auth-end ran and flow did not /tmp/flow-v2-native-auth-review-r1/hook-results.json not a finding +2026-10-24T10:31:00Z tests Accept scoped offline results and leave the live canary skipped FLOW_CANARY_NATIVE_AUTH_LIVE was unset bun test 49 pass, 1 skip, 0 fail, 205 expect recorded +2026-10-24T10:34:02Z hashes Separate two test-file hashes from production boundary-discipline. Parent named a portability correction final-hashes.json productionDrift empty test-only drift +2026-10-24T10:34:02Z limits No release approval and no comment verdict Delegates forbidden. Built-in paid qualification absent D verification.json modelGenerationRequests 0 bounded fail on P2 diff --git a/.agents/plans/18-opencode-v2/reviews/native-auth-r1.md b/.agents/plans/18-opencode-v2/reviews/native-auth-r1.md new file mode 100644 index 00000000..566badbf --- /dev/null +++ b/.agents/plans/18-opencode-v2/reviews/native-auth-r1.md @@ -0,0 +1,184 @@ +# Native auth round 1 + +Verdict: actionable cleanup defect. No release approval. + +The source-owned OAuth boundary holds in the executed offline checks. A broker dispose failure can leave the temp project and skip the location unload while the recorded service pid is still alive. That is P2. Two smaller fail-closed gaps are P3. The aborted-primary hook hypothesis was not reproduced. + +throughput checkpoint: n/a, read-only investigation + +## Overview + +This review covered the five `evals/native-auth` modules, the eval harness wiring, the operator command, and the canary binding. The main repo was read only. Writes stayed under `/tmp/flow-v2-native-auth-review-r1`. No real account, no user service, no paid call, and no connection to pid 76572. + +Production hashes for the frozen auth sources match the review-start record. Two test files drifted for the Linux portability correction. That drift is recorded separately below. + +## Key concepts + +The original refresh stays in the existing native 2.1.4 service. The child stores the placeholder refresh `flow-native-auth-source-owned` and the unresolved access marker. The client replaces only the selected OAuth method. The broker is a loopback relay with one lease and a bearer capability. The canary seals four client files and publishes a public receipt. + +`request()` in Flow records a primary `Request` only when the project `http.request` hook runs. Native `PluginHooks.trigger` awaits each registered callback in append order and then returns the same input object. + +## How it works + +The broker discovers `opencode service status`, pins URL, pid, and version `2.1.4`, and repeats that check around each RPC. Dispose stops the relay, then tries to close the RPC and delete the debug location, then removes the temp directory. + +The client guard calls the relay `check` route before it returns. It does not rewrite the `Request`. The source plugin reads `connection.active` and `connection.resolve`, substitutes the placeholder, and refuses a refresh copied into access or metadata. + +Canary preparation verifies the sealed client before a charge. A live attach happens after that charge. Auth receipts are schema 2 and hash the public provider policy. Historical schema 1 records gain no `nativeAuth` field. + +## Where things live + +- `evals/native-auth/broker.ts` dispose is lines 138-171. +- `evals/native-auth/host.ts` attachment dispose is lines 210-219. +- `evals/native-auth/client-plugin.ts` method map is lines 89-133. +- `evals/native-auth/source-plugin.ts` pending map is lines 59-108. +- `src/platform/opencode/lifecycle.ts` `http.request` hook is lines 693-709. +- `src/platform/opencode/execution-attribution.ts` `request()` is lines 331-347. +- Native trigger implementation is the `PluginHooks` service in the local 2.1.4 darwin-arm64 binary. +- Promise hooks are wrapped at `node_modules/@opencode/plugin/dist/promise/adapter.js` line 429 with `Effect.promise(() => Promise.resolve(callback(event)))`. + +## Findings + +### P2. Broker dispose skips removal when a later step throws + +`evals/native-auth/broker.ts:138-171` + +`rm(directory)` sits after the identity check, the close RPC, and `DELETE /api/debug/location`. There is no `finally`. A throw from `refuse()` or from `api()` skips the removal. + +Executed with a fake CLI and Bun 1.4.0. The runner pid was 92003. Signal 0 only. The real `opencode` binary was not started. + +Live pid, and every later CLI call exits 1: + +- dispose throws `Native account authentication failed.` +- the fake CLI log has no `DELETE` and no `/close` +- temp directory `flow-native-account-*` still contains `plugin` and `project` + +Identity still matches, and `DELETE` exits 1: + +- close and `DELETE` are present in the log +- dispose throws the same generic error +- the temp directory still contains `plugin` and `project` + +Dead pid, CLI exits 1: + +- `process.kill(pid, 0)` gets `ESRCH` +- dispose resolves +- the temp directory is gone + +This run did not attach to a real native service. The observed fact is the skipped RPC and the leftover directory. On a discovered service, the same path would also skip the location unload. + +Minimal fix: unload only when the pinned URL and pid still match, and always `rm` the temp directory in a `finally`. Do not send the close or delete RPC to a replacement pid. + +### P3. Host attachment dispose skips the directory when configuration removal throws + +`evals/native-auth/host.ts:210-219` + +`rm(configurationPath)` runs before `rm(directory)`. A throw from the first call skips the second. + +Executed against a local relay. No native CLI. The configuration file was replaced with a directory so the non-recursive `rm` throws `EISDIR`. After `dispose()`, `attachment` still contains `plugin` and `configuration.json`. + +In this repro the capability file had already been removed before the throw. A throw that leaves the file in place would also leave that file, because the directory removal never runs. That second case was not executed. + +Minimal fix: remove the attachment directory once, recursively, from the `finally`. The configuration path is already required to be inside that directory. + +### P3. Duplicate integration and method keeps the last account and fails closed + +`evals/native-auth/client-plugin.ts:89-94` + +The selected map key is `integrationID` plus `methodID`. Two accounts that share that pair collapse to the last one. Startup does not refuse. + +Executed with alpha active and a beta closure on the same integration and method. One method was registered. Refresh threw `Native account authentication failed.` The relay received no request. + +`ensureSelected` compares the active credential id with the closure account before `fetch`. This path does not return beta's access token for alpha. It drops alpha's registration and refuses. + +Minimal fix: refuse at registration when that key already belongs to a different credential id. + +## Hook hypothesis + +Root asked whether a delayed global `http.request` guard can resume after an aborted primary and bind that old `Request` to a fresh `/flow-auto` context. + +Native trigger, from the 2.1.4 binary: + +```js +s=f(function*(l,a,p){ + for(let y of e.get(r(l,a))??[]){ + if(y.options?.providerID!==void 0&&y.options.providerID!==Fu(p))continue; + yield*y.callback(p) + } + return p +}) +``` + +The loop awaits each callback. It does not read `Request.signal`. The model fetch wrapper runs that chain inside an Effect that receives the fetch signal. + +The promise adapter wraps each hook in `Effect.promise` with a zero-argument function, so Effect 4.0.1 does not attach that promise to the abort signal. The promise body can finish after abort. The fiber does not continue to the next hook. + +Executed with Effect 4.0.1 from this repo. Signal abort during the first hook: + +- the first hook logged `auth-start` and `auth-end` +- the second hook did not log `flow` +- `runPromise` rejected with `All fibers interrupted without error` + +A separate state-machine check called the real `ExecutionAttribution`. Ending the old primary, freezing a new one with the same agent and model, then calling `request()` with the old `Request` binds that `Request` to `new-primary`. `request()` will adopt an unseen primary `Request` onto the current context-phase primary. + +That adoption did not happen on the abort path, because the later hook did not run. `session.execution.interrupted` is published from the generation effect's settled cause, and that effect contains the hook chain. This review did not find a scheduler window where a successful new context is frozen and the old hook still runs afterward. + +This is not a finding. + +## Checks that held + +Source single-flight. Twenty-four overlapping `active()` calls, each waiting 15ms, produced `maxResolveDepth` 1. `connection.resolve` was entered 26 times because waiters re-enter after the in-flight operation finishes. Those entries did not overlap. The pending map is set in the same turn as the empty check. No concurrent refresh was observed. + +Dead-pid dispose removes the temp directory. + +Client refresh of a collapsed foreign account makes no relay call. + +Scoped offline tests, Bun 1.4.0, 1.59s, env `FLOW_CANARY_NATIVE_AUTH_LIVE`, `FLOW_OPENCODE_NATIVE_EXECUTABLE`, and `FLOW_EVAL_NATIVE_AUTH` unset: + +- 49 pass +- 1 skip +- 0 fail +- 205 `expect()` calls + +The skip is `native canary loads sealed auth before seeding...` in `tests/canary-v2-native-auth.test.ts`. It runs only when `FLOW_CANARY_NATIVE_AUTH_LIVE` is `1`. It was not run. + +`tests/native-auth-native.test.ts` was not run. The owner receipt at `/tmp/flow-v2-native-auth-probe/production-native-receipt.json` remains the owner-executed native proof. It uses the fixture provider `native-auth-fixture`. It is not a built-in OpenAI or xAI qualification. + +`/tmp/flow-v2-eval-auth-integration/verification.json` is an owner-executed bootstrap proof. Status passed. `modelGenerationRequests` is 0. `actualModelBearerReproved` is false. `productionEdits` is false. `authOwnedFreezeMatches` is true. `sourceRefreshUnchanged: false` is the probe's expected synthetic rotation to `eval-auth-synthetic-rotated-refresh`, and `sourceRefreshIsPlaceholder` is false. That file does not show a placeholder written back over the source refresh. + +Operator tests preserved a replaced output file. The remaining same-user `lstat`-then-`rm` window was not reproduced. It stays a bound. + +Harness and canary still pass the process environment through with provider API keys retained by `canaryLaunchEnvironment`. The guard refuses an active `env` connection and a switched credential id. It does not inspect `Authorization`. Built-in OpenAI and xAI precedence was not re-proved. The local package has no JavaScript provider source. Missing paid qualification is a limit. + +Flow's lifecycle `http.request` hook attributes the same `Request` and does not replace its URL. The recovery budget plugin can replace a URL on the recovery treatment path. That path is outside this auth seal. + +## Hash drift + +Review start and review end match for every frozen production file in `initial-hashes.json`, including `broker.ts`, `client-plugin.ts`, `host.ts`, `protocol.ts`, `source-plugin.ts`, and the fixture plugin. + +Test-only drift, already present in this checkout: + +| file | initial | final | +| --- | --- | --- | +| `tests/native-auth.test.ts` | `sha256:d59e83951783bc6f8aaf6ff46ff3c7c3936d9240aa28fc52fb09cf765682f6e8` | `sha256:67c9dffd8b533851d0cacad76c5742bcc698be328dd3e9565af8866ecca05cfc` | +| `tests/fixtures/native-auth-smoke.ts` | `sha256:7a8a4e1865311eda4fce7cee950a9afa06e9485cd44e7d6c69aa4ac3286e6f90` | `sha256:8ce07fde3cb2e1fead34cdfd0e3d57d82982006d814393b9cbd433a55c7f564b` | + +`tests/native-auth.test.ts` `directory()` uses `mkdtemp(join(tmpdir(), "flow-native-auth-unit-"))` and imports `tmpdir` from `node:os`. `tests/fixtures/native-auth-smoke.ts` uses `mkdtemp(join(tmpdir(), "flow-native-auth-production-"))` and writes `receipt.json` only inside that temp root. Neither file still mentions `/tmp/flow-v2-native-auth-probe`. + +Linux Git ownership from a tar that preserved a Mac uid is an environment fact about the disposable checkout. It is outside this review's product scope. + +## Limits + +No release approval. Prior release and eval verdicts do not certify this auth code. Root's Linux 4494-pass snapshot predates it. + +Comment inventory at `/tmp/flow-v2-comment-review-auth-r1` was not given a comment verdict. The parent forbade delegates, so Comment Sicko was not run. Sticky poteto hooks were not verified. One reviewer. Critic coverage is partial. + +`tests/native-auth-native.test.ts` and the live canary were not run here. + +## Gotchas + +A passing offline count is the number of executed assertions. It is not a cloud refresh proof. + +`request()` trusts the current context-phase primary. Anything that ends that primary and freezes another one before the Flow hook runs can adopt an unseen `Request`. The abort path measured here does not do that, because the later hook is skipped. + +Broker cleanup and host cleanup both depend on every preceding await succeeding. The executed failures leave directories behind. diff --git a/.agents/plans/18-opencode-v2/reviews/native-auth-r2-decisions.tsv b/.agents/plans/18-opencode-v2/reviews/native-auth-r2-decisions.tsv new file mode 100644 index 00000000..dbac4e0e --- /dev/null +++ b/.agents/plans/18-opencode-v2/reviews/native-auth-r2-decisions.tsv @@ -0,0 +1,15 @@ +ts phase decision why evidence result +2026-10-24T10:45:02.385179+00:00 scope Use frozen auth sources and write only own scratch No repo writes, delegates, real service, provider calls or broad builds authorized initial-hashes.json freezeMismatch=0 +2026-10-24T10:49:31.333138+00:00 workflow Use one reviewer and no comment verdict The user forbids delegates; independent comment and cross-model trail lanes cannot run /tmp/flow-v2-comment-review-auth-r1/manifest.json partial independent-lane coverage +2026-10-24T10:49:31.333235+00:00 r1 Close the three original findings on these bytes Adapted original repro removes broker and host directories and rejects method conflict at registration repro-results.json all three causal repros fixed +2026-10-24T10:49:31.333237+00:00 offline Accept the focused offline suite within its limits 59 pass and one explicitly disabled native-canary test exercise real helper functions with synthetic boundaries offline-tests.log 59 pass; 1 skip; 0 fail +2026-10-24T10:49:31.333238+00:00 lease Reject revival of source single-flight or stale-lease claims Original stress resolves at depth one; new fake-CLI late reply and stale release cannot cross a lease repro-results.json; lease-results.json maxResolveDepth 1; late response and stale release 503 +2026-10-24T10:49:31.333240+00:00 failure File one P3 for unsanitized disposal failures Actual chmod-induced removal errors expose private paths and replace generic release failure failure-results.json; evals/native-auth/host.ts:215; evals/native-auth/broker.ts:172 ISSUES P3 +2026-10-24T10:49:31.333241+00:00 failure Keep relay-stop result bounded to fault injection A rejected stop escapes unchanged and skips remote cleanup, but no natural Bun stop rejection was observed failure-probes.ts; failure-results.json owned directory removed; injected error escapes +2026-10-24T10:49:31.333242+00:00 verification Correct the reviewer assertion for macOS path aliases Fake CLI cwd is /private/tmp while rm reports /tmp; the first added assertion compared those spellings failure-probes.ts:82; failure-probes.log corrected basename assertion passed; no source change +2026-10-24T10:49:31.333243+00:00 artifact Require resealing the auth add-on only New client matches retained owner bytes and canonical aggregate; old aggregate refuses active verification artifact-results.json e8e4bed74dd1266ac3e1c4cd149918b8ffa35391afac0b098f95eb2930333c1b +2026-10-24T10:49:31.333244+00:00 history Keep historical results separate from current release approval Six retained 9.x canaries regrade at 2030 without nativeAuth fields artifact-results.json 6 retained regrades passed +2026-10-24T10:49:31.333245+00:00 checks Accept only scoped types and lint No root suite, root tsc, root build or grader-closure preparation is authorized during concurrent core work types.log; lint.log scoped tsc and 20-file Biome passed +2026-10-24T10:49:31.333246+00:00 native Use the retained native proof as owner-executed evidence The private fixture was inspected but not launched; real providers and accounts remain unauthorized /tmp/flow-v2-native-auth-probe/review-r1-fixes/native-receipt.json not independently rerun +2026-10-24T10:50:58.404812+00:00 closeout Return ISSUES for one P3 and close original R1 findings The final report distinguishes raw disposal errors from source ownership and paid-provider qualification report.md; final-hashes.json; decisions.tsv 25 compared files unchanged; no release approval +2026-10-24T10:50:58.405155+00:00 trail Self-audit completed without independent trail review Every decision points to observed evidence; the parent explicitly forbids delegates commands.json; repro-results.json; failure-results.json; lease-results.json; artifact-results.json independent review lane unavailable by instruction diff --git a/.agents/plans/18-opencode-v2/reviews/native-auth-r2.md b/.agents/plans/18-opencode-v2/reviews/native-auth-r2.md new file mode 100644 index 00000000..a976dfc8 --- /dev/null +++ b/.agents/plans/18-opencode-v2/reviews/native-auth-r2.md @@ -0,0 +1,121 @@ +# Native auth round 2 + +ISSUES. One P3 remains in the disposal error boundary. All three original R1 findings are closed on the reviewed source. No release approval. + +This is a fresh independent source review by Codex gpt-6.1-sol with high reasoning effort. Priority was requested by the parent and was not independently reconfigured or measured here. No delegates ran. + +throughput checkpoint: n/a, read-only investigation + +## P3. Local disposal errors bypass the generic authentication error boundary + +The affected statements are [host.ts:215](/Users/vriesd/projects/flow-opencode/evals/native-auth/host.ts:215) and [broker.ts:172](/Users/vriesd/projects/flow-opencode/evals/native-auth/broker.ts:172). Both finalizers await raw filesystem removal without an outer error normalization boundary. A removal failure replaces an earlier generic release or identity error. It exposes the private attachment or broker directory in the returned error. + +The causal reproduction is [failure-probes.ts](failure-probes.ts). It calls the actual current helpers, uses its own loopback relay and fake CLI, and writes only this review directory. No native executable runs. + +For the host case, the relay returns HTTP 503 during release. A synthetic child directory inside the owned attachment contains a file and has mode 000. Disposal rejects with this literal result on Bun 1.4.0: + +```text +ENOTEMPTY: directory not empty, rm '/tmp/flow-v2-native-auth-review-r2/failure-probes/attachment' +``` + +The stable `Native account authentication failed.` release error is replaced. A second disposal returns the same error message. The attachment remains because removal failed. The external presealed client still verifies. The reviewer restores permissions and removes the fixture afterward. + +The broker variant creates the same inaccessible child under its owned directory. Identity, close, and DELETE succeed through a fake CLI. Its final removal also returns raw `ENOTEMPTY` containing the broker path. This variant establishes the broker error leak independently of any injected server failure. + +A separate fault injection makes the relay stop its real local server and then reject with `relay-stop-private-sentinel`. Broker disposal returns that exact text, removes its owned directory, and makes no subsequent close or DELETE call. Duplicate disposal returns the same promise. This proves the current propagation and sequencing if stop rejects. It does not establish that Bun naturally produces such an error. No separate native remote-unload defect is filed from that injection. + +The observed leak is a private scratch path and arbitrary injected teardown text. No source OAuth refresh, access token, capability, metadata, or user account value leaked in this reproduction. The operator CLI and auth canary have additional generic top-level error handling. Public canary leakage from this path was not observed. + +Minimal fix. Normalize failed completion at the public host and broker disposal boundaries with `refuse()`, without attaching the raw error as a public cause. Preserve independent local cleanup, pinned remote operations, and memoized completion. A filesystem that refuses removal can still require retained scratch and manual cleanup. That failure should remain generic. Add regressions for local removal failure as well as remote release failure. + +Evidence is [failure-results.json](failure-results.json) and [failure-probes.log](failure-probes.log). This finding does not reopen the corrected R1 cleanup ordering. + +## R1 findings and withdrawn objections + +The full R1 report, decision trail, original repro and results were read. The fix handoff, freeze, complete change patch, adaptation patch, after results, and native receipt were read. Original evidence was left unchanged. + +The original reproduction was copied into [repro.ts](repro.ts). It uses the owner's recorded adaptation to catch the earlier method-conflict refusal. Only its output destination changed from the owner's adapted version. + +The independently executed [results](repro-results.json) show: + +- A live pinned PID with failed CLI identity lookup returns the generic error, removes its owned broker directory, and sends no close or DELETE. +- A dead fixture PID with failed lookup removes the directory and resolves disposal without unpinned RPC. +- A matching identity with failed DELETE retains the generic error and removes its directory. Close and DELETE were attempted. +- Replacing the host configuration file with a directory no longer blocks attachment removal. Disposal succeeds. +- Conflicting integration and method ownership rejects during registration. There are zero method registrations and zero relay requests. + +The dedicated broker regressions also pass for a reported replacement PID and replacement after close. DELETE is not sent after replacement. Repeated disposal shares completion. The host regressions pass with release failure, a configuration directory, and both together. They compare all external presealed bytes before and after cleanup. + +The same-account alias regression passes. One OAuth registration handles two provider aliases for the same credential. A successful resolve retains the placeholder refresh. Different credentials on the same integration and method reject before any transform. + +The source single-flight objection remains withdrawn. The original stress repro again reports `maxResolveDepth: 1`, across 26 resolve entries and 24 overlapping active lookups. Entry count is not concurrent refresh depth. The aborted-hook objection remains withdrawn on the retained native trigger and Effect evidence. No new causal evidence justifies reviving it. + +## Source ownership, lease isolation, and execution path + +The five auth modules were read in full. The auth config reader, operator command, relevant harness startup and stop paths, campaign wiring, canary preparation, launcher, recording, offline derivation, and qualification join were traced. + +The original refresh stays inside native connection resolution in the source service. The source plugin pins the provider, integration, active credential, method, label, and metadata, serializes resolver calls by credential ID, checks active identity after resolution, substitutes the placeholder before returning, and refuses original refresh duplicated in access or metadata. It has no native credential-store CRUD, model generation, or user-global-config write call. Child credential creation occurs only in the isolated eval or canary host. + +The child overrides only selected OAuth method registrations. Its refresh callback calls the private relay and preserves the placeholder. HTTP and WebSocket guards check the active child credential and source selection before allowing the existing native request to continue. The exercised request-identity test retains the same input and `Request`, including its header. These helpers do not create a replacement model request, transport, prompt, assistant result, usage count, or success status. Unsupported account layouts may still reject honestly. + +[lease-proof.ts](lease-proof.ts) independently exercises the actual broker through a synthetic CLI. Wrong capability returns 401. Overlapping acquisition returns 409. A resolution begun under an old lease returns generic 503 after release and replacement. A stale release returns 503, and the new lease still passes `check` with 200. [lease-results.json](lease-results.json) records those literal outcomes. This is one broker's cross-invocation lease isolation. No global exclusion across separately started brokers or real built-in OAuth refresh implementations is claimed. + +Native identity pinning remains a before-and-after check over default CLI discovery, URL, PID, and version 2.1.4. It is not an atomic service handle. Replacement between checks can receive an attempted command before a later mismatch rejects its result. Native CLI recovery and replacement after the last check remain bounds. The regression demonstrates refusal of a detected replacement, not elimination of every process race. + +Harness auth mode stages beneath private scratch, rejects disabled credentials, manual handoff, and simulation conflicts, and strips inherited `OPENCODE_*` selectors. Its private HOME global config contains the auth plugin with warming off, update disabled, and sharing disabled. The project config remains the ordinary Flow config. Readiness session creation and removal, config readback, and expected local plugin checks precede placeholder seeding. The native SDK's plugin status union is active or failed, so accepting a non-failed listed plugin does not admit a third loading state under that API. + +`run.ts` enables the path only through explicit `FLOW_EVAL_NATIVE_AUTH` and requires concurrency one. Preflight and scenario hosts receive the same selection. Shutdown attempts auth disposal independently of credential-list and callback failure. Failed termination also attempts auth disposal and does not invoke project retention on an unquiescent host. These are source observations and the six executed shutdown tests, not a new actual EvalHost boot proof. + +The operator command requires existing paid authorization and approved models before broker startup. It exclusively opens the output with no-follow and private modes. The executed tests preserve an existing file and a replacement inode, clean new owned output after startup or disposal failure, and avoid source startup on missing authorization or a rejected model. Its lstat-to-removal window against a same-user writer remains a bound. No new proof of atomic conditional unlink is claimed. + +## Exact client and canary bindings + +[artifact-proof.ts](artifact-proof.ts) reads only public static client files from the retained owner artifact. It stages the current client in this review directory and verifies both artifacts against current production source. Their four file hashes match exactly. + +The current aggregate is: + +```text +sha256:e8e4bed74dd1266ac3e1c4cd149918b8ffa35391afac0b098f95eb2930333c1b +``` + +The prior aggregate was `sha256:8e3c149d61b2f4998787e4729ea5207504d566684aea6b0fb0f7f4595ec31e4c`. Active verification refuses that older add-on. This requires auth add-on resealing. It does not require rebuilding or altering the separate product archive. [artifact-results.json](artifact-results.json) retains the exact per-file hashes. + +The aggregate uses the shared `nativeAuthSourceSha256` canonical domain hash. Reversed keys and canonical installation round-trip retain the same hash. Exact four-file verification rejects extra paths, mutated bytes, symlinks, and hardlinks. The source plugin uses a separate hash domain. No executable extras enter the client seal. + +Canary attachment mode and the private source-file argument must agree. Static descriptor, file, and preparation checks happen before the single dispatch charge. Live source attachment happens afterward. Failed acquisition or setup remains charged once. Native collection precedes termination. Cleanup finalizers are independent. Tests cover generic errors and removal of private data after source, dependency, version, startup, and release failure. + +The public auth receipt binds the prepared digest, exact static client descriptor, selected model, host PID and version, public HTTP provider-policy digest, root native session, outcome, and native transcript digest. Strict parsing rejects unknown private fields. The executed tamper tests reject changed host, model, policy, native digest, and source descriptor. Recording requires a matching receipt. The private runtime config, its hash, capabilities, source URL, metadata, and credentials are not added to this public descriptor. + +The installation embeds exact public source bytes. `deriveCanaryResult` includes the parsed installation in `flow-canary-host-config-v2`. Qualification sealing reads installation, session, and transcript by recorded byte lengths and digests. `qualification-regrade.ts` checks those same bundle roles, re-derives the canary, and compares status, checks, actors, and host-config digest. That qualification connection was source-reviewed. No new bundle was prepared or qualified, and no concurrent grader closure was hashed by this reviewer. + +The receipt is a consistency binding over retained evidence, not a cryptographic attestation that defeats an operator fabricating all evidence and hashes together. It does not replace actual provider qualification. + +Six retained 9.x canaries independently regrade at a 2030 clock with retained freshness. Their installation records have no `nativeAuth` field. The no-auth mode test also proves no default field is inserted. No historical evidence was rewritten. + +## Executed checks and source identity + +Bun was `/tmp/flow-v2-toolchain/node_modules/.bin/bun`, version 1.4.0. [commands.json](commands.json) records argv and outcomes. [run-checks.py](run-checks.py) reruns the bounded checks. It clears inherited Flow and OpenCode selectors, assigns a scratch HOME and TMPDIR, and preloads a fetch guard that permits only HTTP loopback. Native tests are not enabled. + +The six-file offline suite passed 59 tests, skipped one opt-in native canary, and failed zero tests, with 252 expect calls. [offline-tests.log](offline-tests.log) names every executed case. This count excludes the separate repro, lease proof, artifact assertions, six retained regrades, and failure probes. It is not a provider qualification count. + +Scoped TypeScript for the auth modules, config reader, and native-auth tests passed. Biome checked 20 scoped files without fixes. Logs are [types.log](types.log) and [lint.log](lint.log). Root tsc, the root suite, builds, pack, Git-backed qualification, and canary preparation did not run. + +The first extra assertion added to the failure probe compared `/private/tmp` from the CLI cwd with `/tmp` from the filesystem error. That reviewer assertion failed. The corrected assertion checks the owned directory basename and passes. This was a macOS path-alias error in the reviewer script, not a product finding. Earlier observations remain supported by the rerun. + +[initial-hashes.json](initial-hashes.json) matches every supplied R1-fix freeze entry. [late-start-hashes.json](late-start-hashes.json) separately identifies later-inventoried dependencies and two closeout-only qualification files. [final-hashes.json](final-hashes.json) reports zero drift across 25 compared files and zero freeze mismatches. [verify-hashes.py](verify-hashes.py) reruns the comparison. Concurrent core, Typesafe, and associated test changes are outside that claim. No whole-tree source hash or full-green claim is made. + +## Limits and attention + +The retained `/tmp/flow-v2-native-auth-probe/review-r1-fixes/native-receipt.json` is owner-executed evidence. The private fixture source was inspected. The receipt reports authenticated default CLI discovery, active plugins before import, source rotation and unrelated-account preservation, one local bearer-authenticated model request, fallback and cached-access source-loss refusal, source-location eviction, and both owned PIDs stopped. Its retained staged client bytes match this review. The native fixture and live canary were not rerun here. + +No actual credentials were read. No user service or PID 76572 was contacted. No cloud OAuth, real provider, paid model, connector, delegate, Git operation, repository edit, root build, pack, goal, automation, or task creation occurred. All reviewer writes stayed under `/tmp/flow-v2-native-auth-review-r2`. + +Built-in OpenAI and xAI routing, metadata, refresh behavior, environment-key precedence, Linux real-account forwarding, actual request budgets, and paid qualification remain unproved here. The auth modules themselves contain no model-dispatch bypass. That source observation does not certify an installed budget plugin. The parent's separate actual EvalHost boot work and concurrent core compaction fix are not certified by this report. The parent reports product `d3d46d92` blocked by that core edge case. This review grants no release approval regardless of earlier green counts or packed smoke receipts. + +Prove It Works required the actual R1 reproduction and direct static artifact comparison. Boundary Discipline focused the new finding on public error completion. Make Operations Idempotent drove repeated-disposal and stale-lease checks. Build the Lever produced the rerunnable probes and hash checker. Test Behavior, Not Implementation required absent directories, preserved bytes, and literal HTTP outcomes. Minimize Reader Load kept the call graph tied to refresh ownership and cleanup ownership. Model the Domain used separate artifact descriptors, lease identities, and cleanup-result records. Unslop and technical-writing shaped the report and decision log. + +The no-delegate instruction overrides optional how critics, Comment Sicko, and cross-model trail review. The supplied comment inventory is context only. No new independent comment verdict is claimed. Poteto sticky hooks were not verified. + +Attention. + +The decision trail was self-audited against this conversation and the retained artifacts. No independent trail reviewer ran because delegates were prohibited. Inspect the P3 reproduction and the distinction between real filesystem failure and injected relay-stop failure. Owner-executed native evidence and missing real-provider qualification remain explicit limits. diff --git a/.agents/plans/18-opencode-v2/reviews/native-auth-r3-decisions.tsv b/.agents/plans/18-opencode-v2/reviews/native-auth-r3-decisions.tsv new file mode 100644 index 00000000..d72a775d --- /dev/null +++ b/.agents/plans/18-opencode-v2/reviews/native-auth-r3-decisions.tsv @@ -0,0 +1,10 @@ +ts phase decision why evidence result +2026-10-24T11:08:03Z start Start a fresh round 3 review in its own directory Parent forbade delegates, repo writes, real accounts, and pid 76572 /tmp/flow-v2-native-auth-probe/review-r2-fixes/freeze.json review directory created +2026-10-24T11:15:47Z hashes Accept the frozen four-file auth delta Start hashes match the round 3 freeze and differ from round 2 only on the two catch lines and their tests /tmp/flow-v2-native-auth-review-r3/initial-hashes.json freezeMismatch empty +2026-10-24T11:15:47Z diff Treat the production change as two catch handlers The pre-fix checkout differs only by .catch(refuse) on the memoized host and broker cleanups /tmp/flow-v2-native-auth-review-r3/broker-diff.log host diff matches the same shape +2026-10-24T11:15:47Z probe Discard the first boundary probe failure The fixture refresh was rejected by parseOAuth and the allowlist flagged Bun source locations /tmp/flow-v2-native-auth-review-r3/commands.json first exit 1, log overwritten +2026-10-24T11:15:47Z probe Accept the corrected public-error probe chmod and ELOOP disposal return NativeAuthError with an empty leak list and honest directory retention /tmp/flow-v2-native-auth-review-r3/boundary-results.json exit 0 +2026-10-24T11:15:47Z retained Keep the earlier auth corrections closed The round 1 repro, lease proof, client aggregate, and alias test still match their accepted results /tmp/flow-v2-native-auth-review-r3/repro-results.json maxResolveDepth 1, old aggregate refused +2026-10-24T11:15:47Z checks Accept one six-file offline run plus scoped types and lint 63 pass, 1 live-canary skip, 0 fail. That count is the file list, not provider qualification /tmp/flow-v2-native-auth-review-r3/offline-tests.log tsc empty, biome 20 files +2026-10-24T11:15:47Z closeout Return bounded PASS The public disposal error no longer carries the scratch path or the injected sentinel /tmp/flow-v2-native-auth-review-r3/report.md no release approval +2026-10-24T11:15:47Z trail Self-audit this run without a second model The parent forbade delegates, so Comment Sicko and cross-model trail review did not run /tmp/flow-v2-native-auth-review-r3/decisions.tsv independent review lane unavailable diff --git a/.agents/plans/18-opencode-v2/reviews/native-auth-r3.md b/.agents/plans/18-opencode-v2/reviews/native-auth-r3.md new file mode 100644 index 00000000..dc0308ac --- /dev/null +++ b/.agents/plans/18-opencode-v2/reviews/native-auth-r3.md @@ -0,0 +1,92 @@ +# Native auth round 3 + +Bounded PASS. Public host and broker disposal now reject with `NativeAuthError` and the message `Native account authentication failed.` The scratch path, the filesystem code, the release body, and the injected relay sentinel stay off that error. No actionable finding. This review gives no release approval. + +throughput checkpoint: n/a, read-only investigation + +This is a fresh review by Grok grok-4.7 with high reasoning. No delegates ran. Poteto sticky hooks were not verified, so this turn follows the skill directly. + +## Overview + +Round 2 left one P3. A failed `rm` during disposal replaced the generic authentication error and put the private directory in the message. An injected relay `stop` rejection did the same with its sentinel. + +The frozen correction appends `.catch(refuse)` to the two memoized cleanup promises. `refuse` throws a new `NativeAuthError` and takes no cause. I diffed the current files against the pre-fix checkout. Those two lines are the entire production change. `protocol.ts`, `client-plugin.ts`, `source-plugin.ts`, and the smoke fixture are byte-identical to that checkout. + +The caller who awaits disposal receives that new error. A removal that the filesystem refuses still leaves the directory in place. A later call returns the same rejection and does not try again. + +## How the public error is built + +`evals/native-auth/protocol.ts` lines 30 through 38 define `NativeAuthError` and `refuse`. The message is fixed. The constructor does not accept a cause. + +`evals/native-auth/host.ts` line 217 applies `refuse` to the attachment cleanup promise stored in `done`. The public attachment method at lines 294 through 297 is a separate async wrapper. Each call to that wrapper returns a new promise. Both calls await the same inner promise. One release runs. The rejection object matches. + +`evals/native-auth/broker.ts` line 174 applies `refuse` to the promise stored in `closing`. A second `dispose` returns that same promise. Relay stop, the pinned close, and `DELETE` still happen inside the `try`. Directory removal stays in the `finally`. + +Those are the only two `rm` calls under `evals/native-auth`. The canary auth path, when cleanup fails, throws `NativeCanaryError` with the phase `cleanup` and drops the raw reason. I read that path. I did not launch a canary. + +## Executed proof + +Bun was `/tmp/flow-v2-toolchain/node_modules/.bin/bun`, version 1.4.0. The runner pid for the corrected probe was 14038. The fake CLI and loopback relay never called the native binary or pid 76572. Fetch was limited to `http://127.0.0.1`. Writes stayed under `/tmp/flow-v2-native-auth-review-r3`. + +`boundary-probe.ts` calls the public `dispose` methods. It records message, name, stack, cause, own properties, Bun location fields, and `util.inspect` output. Needles were the owned path, its basename, `ELOOP`, `ENOTEMPTY`, `EACCES`, the release body `private-release-sentinel`, the relay sentinel, and the capability. + +Results in `boundary-results.json`. + +- Host ELOOP after a successful release. Disposal rejects. The owned directory remains under the renamed parent. One release. A second concurrent call returns the same error. After the probe restores the path, another dispose returns that same error and the directory is still there. The external staged client still verifies. +- Host ELOOP after a 503 release. Same public error. The release body is absent. The directory remains. One release. +- Host mode `000` child after a 503 release. This is the Round 2 chmod setup on this Mac. Removal still fails. The public error is generic. The directory remains. The release body is absent. +- Host release failure with a removable directory. The directory is gone. The public error is generic. +- Host clean disposal. The promise resolves. The directory is gone. +- Broker ELOOP after pinned close and `DELETE`. Both remote calls happen once. The directory remains. Repeated dispose is the same promise and the same error. Restoring the path does not remove it. +- Broker mode `000` child. Close and `DELETE` happen once. The directory remains. The public error is generic. +- Broker relay-stop injection. The real server stops, then the injected sentinel throws. Stop runs once. Close and `DELETE` do not run. The directory is removed. The sentinel is absent from the public error. +- Broker clean disposal. The promise resolves. The directory is gone. + +Every rejected case has `cause` absent. The stack is `NativeAuthError` at `refuse` in `protocol.ts:37`. Bun also sets `line`, `column`, `sourceURL`, `originalLine`, and `originalColumn`. `sourceURL` is `evals/native-auth/protocol.ts`. Those fields do not contain the scratch path. + +The first probe attempt exited 1. `commands.json` still says that. The fixture refresh was not `flow-native-auth-source-owned`, so broker startup correctly refused before disposal. The probe also treated Bun's source-location fields as a leak. I corrected the fixture and the allowlist, reran, and exited 0. `boundary-rerun.json` records that. The first log was overwritten by the rerun. + +The focused offline command was one run of six files. It passed 63 tests, skipped 1, and failed 0, with 306 `expect` calls, in 2.96 seconds. The skip is `native canary loads sealed auth before seeding, exports before cleanup, and uses only the source bearer`. That test is the opt-in live canary. The 63 passes are this file list, on this machine, once. They include the four new disposal regressions, the same-account alias registration, the dead-pid cleanup with no close or `DELETE`, and the presealed-byte checks. They are not an OpenAI or xAI qualification. + +Scoped `tsc` for the two auth modules and the two focused tests produced an empty log and exit 0. Biome checked 20 scoped files and applied no fixes. Root `tsc`, the root suite, build, pack, and Git did not run. + +## Retained corrections + +`repro-results.json` reruns the Round 1 cases against the current source. + +- A live pinned pid with a failed identity lookup removes the directory, returns the generic error, and sends no close or `DELETE`. +- A dead fixture pid removes the directory and resolves, with no close or `DELETE`. +- A matching identity with a failed `DELETE` removes the directory, returns the generic error, and attempts close and `DELETE` once. +- Replacing the host configuration file with a directory no longer blocks attachment removal. Disposal succeeds. +- Conflicting method ownership rejects with an empty registration list and an empty relay log. +- Overlapping source resolves stay at `maxResolveDepth` 1, across 26 resolves and 24 overlapping entries. The single-flight and aborted-hook hypotheses stay withdrawn. + +`lease-results.json` records wrong capability 401, overlap 409, a late old resolve 503, a stale release 503, and the replacement lease check 200. + +`artifact-results.json` stages the current client and matches the retained owner bytes. The aggregate is `sha256:e8e4bed74dd1266ac3e1c4cd149918b8ffa35391afac0b098f95eb2930333c1b`. Reversed canonical keys keep that hash. Active verification refuses the prior aggregate `sha256:8e3c149d61b2f4998787e4729ea5207504d566684aea6b0fb0f7f4595ec31e4c`. Six retained 9.x canaries regrade at a 2030 clock with no `nativeAuth` field. + +The native receipt under `review-r1-fixes` remains owner-executed. I did not rerun `tests/native-auth-native.test.ts`. + +## Hashes + +Start and end scoped hashes match. `closeout-hashes.json` shows no scoped drift after the corrected probe. Freeze mismatch is empty. The only differences from the Round 2 hash map are the four files named in `review-r2-fixes/freeze.json`. + +`src/platform/opencode` was hashed only to keep it out of the auth comparison. 23 files, aggregate `710bb0f711f879e8f9a109acec068ea5652f589cd6cfa4a02b3208c3d6b59a5a`, unchanged from the start of this review to closeout. That is a window observation. It is not a core review. Product `d3d46d92` stays blocked by the separate compaction defect. + +The full scoped map is `initial-hashes.json`. It matches `final-hashes.json`. + +## Bounds + +A failed memoized disposal does not retry removal after the directory becomes removable again. The directory stays until something else removes it. + +The host attachment method returns a new promise on every call. The inner cleanup promise is the memoized one. Concurrent public calls in this probe shared one release and one rejection object. + +Bun's extra error fields name the `refuse` throw site. They are part of the public error object. They do not carry the scratch path in this run. + +No built-in OpenAI or xAI paid qualification ran. A real account route can still reject an unsupported layout. The sealed client hash does not prove a provider accepted the request. + +The supplied comment inventory was read. Comment Sicko did not run. The two added production lines contain no comments. No new comment verdict is claimed. + +## Rerun + +`python3 /tmp/flow-v2-native-auth-review-r3/run-checks.py` repeats the hash gate, the six-file tests, both probes, the lease proof, the Round 1 repro, scoped types, and lint. The boundary probe in that script is the corrected one. It should exit 0. The copy of `commands.json` from this session still shows the first boundary exit of 1. diff --git a/.agents/plans/18-opencode-v2/reviews/reconciliation.md b/.agents/plans/18-opencode-v2/reviews/reconciliation.md new file mode 100644 index 00000000..dfe76b1e --- /dev/null +++ b/.agents/plans/18-opencode-v2/reviews/reconciliation.md @@ -0,0 +1,158 @@ +# Review reconciliation + +Core R1 and R2 are retained unchanged. Core R2 independently found that R1's +missing-marker claim does not match R1's recorded lifecycle source hash. +The retained source for `1e8d5991dcaa756ef19484a6219dcca980551f201d21676c63534019af463772` +already allocates the private token and persists it in native input metadata. +R1 decision rows 3 and 6 are superseded for that source identity. The parent +initially compared it with later stream-fix source and has corrected that +interpretation. This does not invalidate the earlier packed counterexample. + +The earlier `0e4ba80b...` archive has lifecycle source +`188ec48417cef20250e9208cf69e3eaa9225e15f2d6b1331f4b18a630164fd35`. +It lacks the allocation and persisted metadata. The `224f3c78...` archive has +lifecycle source `fa9bb17c719c501b0d236333b67af3f8f265a50d7f0f6d93812ea5333fc09358`. +It contains both. The parent recomputed these facts from each archive's +`dist/index.js.map` sourcesContent, without rewriting either archive. + +Core R2 confirms the response-stream fixes and marker admission boundary. Its +remaining pre-response failure finding is assigned for a new fix and fresh +independent review. No R1 or R2 verdict is a final release approval. + +The parent refreshed the scratch comment inventory in place while applying R1 +findings. Its manifest is a later snapshot, not immutable R1 evidence. The R1 +report retains its exact finding hashes. Subsequent inventories use +`/tmp/flow-v2-comment-review-current` so the snapshots stay distinct. + +Core R3 closed the original stream and response-less failures. It reproduced a +combined automatic-compaction and retry defect caused by recreating the private +origin. That finding is under correction. Release R2 passed its bounded scope. +Eval R2 closed its original caller omission and found credential-drain and +reviewer-environment defects. The parent fixed those paths and added shutdown +regressions. Their independent recheck remains pending. Original reports remain +unchanged. + +Eval R3 passed its bounded review of the shutdown and isolation fixes. Its 78 +scoped file hashes stayed unchanged. Native OAuth integration is a separate, +unfinished unit and was not part of that PASS. Core R4 closed compaction retry +and reproduced stale asynchronous callback cleanup revoking a fresh invocation. +A complete callback-ownership census and fix are in progress before a new review. + +Core R5 passed the complete callback-ownership fix. Typesafe R1 passed the +separate configurable-key reader and internal-off correction. The final product +archive is d3d46d92, with unpacked manifest29a1d475. macOS and Linux full checks +pass. Exact packed product smoke passes on both systems. Final picker and +recovery proof pass. Native-account helpers are outside that product archive +and have their own unfinished independent review. No earlier bounded PASS +constitutes release qualification or publication approval. + +The later native probe rejected d3d46d92. A fresh automatic command reached +compaction before its first primary context and inherited an older primary's +epoch. The earlier package and picker successes remain valid only for their +scoped cases. That archive remains unchanged and is not an accepted candidate. + +Core R6 found ordinary automatic compaction failing without an automatic Flow +lease. It also reproduced a pending-context ownership failure in a component +case. Native concurrent reachability of that overlap remains unproved. The +source fix separates accepted input provenance from automatic authority and +checks the pending callback's exact owner. A subsequent parent-requested native +manual-compaction probe found and corrected a second ordinary-history failure. +Manual summaries after reload store no execution origin and grant no tool +authority. A separately accepted fresh input supplies its own authority. + +The current private source proof is +`/tmp/flow-v2-manual-compaction-check-su6aydtb/freeze-final.json`. Its owner reports +336 focused tests and nine native cases passing. It is a development build, +not a packed Flow 10.0.0 candidate. Fresh core R7 and canonical macOS and Linux +checks were started on the same frozen source. Both complete checks now pass +4,679 tests with 27 skips and zero failures across 183 files. Core R7 remains +running. No replacement archive has been built. + +Core R7 subsequently passed its bounded review of that frozen source. The +reviewer independently ran both ordinary manual cases and 475 focused tests. +Eight reviewed hashes stayed unchanged. The review did not claim a final +archive, native overlapping-context proof, same-model auxiliary retry proof, +or release approval. Its out-of-union compaction-reason probe is retained as +a boundary observation. The pinned native API types reasons as auto or manual; +a stable unknown completion rejects. No source change was made for that probe. +The root is building one new candidate from the reviewed bytes. + +Native-account helper R3 passed its bounded disposal-error review. The source +returns a generic NativeAuthError while preserving genuine cleanup failures. +This closes the scoped R1 and R2 helper findings. It does not qualify actual +OpenAI or xAI accounts, authorize credential refresh, or authorize paid calls. + +The root reran the current native authentication fixture and native canary +fixture after R3. They passed one and fourteen tests respectively. Both used +isolated synthetic services and loopback providers. Current client staging +reproduces aggregate e8e4bed7. No real account or source service was accessed. + +The next exact archive was 74da6ab8 with manifest 5547e887. All 74 reviewed +server sources matched its map. Linux reproduced the raw archive byte for byte. +Both unchanged packed smoke suites then failed native manual compaction after +cancellation of an ordinary request. That archive is rejected. Its picker and +normal recovery scopes passed. R7's earlier source PASS did not cover this +sequence and is not package acceptance. + +The retained native failed control reports reason manual, status failed, and +compaction.failed with `Flow execution epoch was revoked.` The failure occurs +at assertOwned before the compaction native-context read. Public location.reload +then permits the same consumed input IDs to summarize without a fresh prompt. +Native APIs do not expose private epoch or authority. Those values in the +28-row census are separate component observations. + +Two transport launchers also missed their intended stimuli because their +initial automatic context hit an epoch veto. Those families remain incomplete. +A forced component case reproduces context validation during partially active +private admission, before observeAccepted binds the source. It must not be +reported as the independently established native cause of those launcher misses. + +The parent reran census-v2.test.ts in place before noticing that its afterAll +rewrites component-census-v2.json beside the test. That JSON now describes the +parent rerun and supersedes the owner first-run JSON. No exact first-run backup +or pre-rerun digest exists. It was not reconstructed. The original owner log +and test remain unchanged. The parent rerun passed 28 tests and 139 assertions +while asserting the existing defect. Its log and copied result are under +`/tmp/flow-v2-compaction-design-r8`. This is diagnostic evidence, not acceptance. + +All 36 continuation diagnostic processes stopped. Current source is unchanged. +Two isolated read-only designs are comparing summary operations, historical +provenance, pending admission, and live execution authority before the next fix. + +Both designs completed. The parent and independent cross-judge selected A's +single input ledger and typed compaction boundary. The synthesis is recorded +in compaction-boundary.md. It adds synchronous reservations before private +async work, scope cancellation independent of admission.ready, and stable +checked-origin reuse on repeated admission. It rejects a copied admission-id +registry, private lease inspection, and a summary bypass that loses existing +grantless continuation. Implementation is in progress within one core fence. + +The next package's unchanged native smoke will run before its independent +source review. This ordering does not accept an unreviewed archive. It checks +the integrated cancellation sequence before another bounded source verdict. + + +R8 replaced the shared summary/execution premise with the selected input ledger +and compaction boundary. The source owner froze ten files, with 357 focused tests +and 16 private native profiles passing. The root verified that freeze, built one +new archive fc19ca4a, and ran unchanged packed smoke before the new review. Both +macOS and Linux passed all 119 assertions and their full 4,700-test checks. Linux +repacked identical raw bytes. Exact-package continuation, recovery, and picker +lanes passed. This closes the known packed cancellation/manual-summary failure +within those fixtures. R7 stays rejected and its original evidence is unchanged. + +Comment Sicko accepted one redundant test-comment deletion. The parent applied it +only after all runtime source-byte audits closed. The supplementary review freeze +records the new test hash. Executable sources, test expressions, package bytes, +and the original owner freeze are unchanged. Fresh independent core R8 review is +running after those gates. Real-account, paid qualification, CI publication, and +public wiki readback remain open. + + +Fresh core R8 passed its bounded review with no actionable causal finding. It ran +496 offline tests on post-comment bytes and independently recomputed the archive, +manifest, installed files, and source bindings. Ten reviewed files and all 77 +unique mapped paths stayed unchanged. The parent read the full report before +accepting the verdict. Earlier native and full-suite counts remain retained +owner results. Reachability and hook-correlation limits remain explicit. The +candidate is ready for the paid pre-push smoke gate, not release publication. diff --git a/.agents/plans/18-opencode-v2/reviews/release-r1.md b/.agents/plans/18-opencode-v2/reviews/release-r1.md new file mode 100644 index 00000000..3244f1a6 --- /dev/null +++ b/.agents/plans/18-opencode-v2/reviews/release-r1.md @@ -0,0 +1,60 @@ +ISSUES. Two P2 defects remain open at the reviewed snapshot. The parent has read both reproduction artifacts and assigned fixes to the canary owner. Concurrent fixes do not change this round's verdict. They require a fresh review round. + +Reviewer model is gpt-6.1-sol with high reasoning effort, as reported by this runtime. This is one independent read-only review. No siblings or delegates were spawned. + +1. P2. Remove ambient host configuration before canary launch. + +`scripts/canary-run.ts:66` retains OPENCODE_CONFIG, OPENCODE_CONFIG_CONTENT, and OPENCODE_CONFIG_DIR. The later launch cleanup at lines 77 through 93 strips Flow overrides and assigns temporary HOME/XDG paths, but leaves these host selectors intact. The cached pinned 2.1.4 binary reads all three. OPENCODE_CONFIG_DIR selects a configuration root independently of XDG_CONFIG_HOME. + +This lets ambient config and plugins enter a canary whose measured config digest covers only fixture/opencode.json. The run therefore does not prove isolation to the approved fixture. Remove these ambient selectors before dependency setup, version checking, and server launch, or set explicit fixture-owned replacements. Keep required provider access deliberate. Add sentinel tests for the selectors. + +`environment-repro.json` shows the current function preserving all three synthetic values. `vendor-environment-evidence.json` records the pinned binary's code excerpts and SHA-256. The exact function was transpiled without its imports after an unrelated evaluator import temporarily failed. No host was executed. Live contamination is not claimed as observed. + +2. P2. Bind native close evidence to the supplied durable Flow session. + +`scripts/eval-canary.ts:889` accepts a supported assurance projection plus a matching assurance summary from any completed manager flow_session_close. `completionSupportedFromDelivery` at line 296 compares check IDs/statuses and conclusion. It does not link the close request's sessionId or operation to the supplied Session v5 document parsed at line 799. Native calls retain their input at line 720, but this check ignores it. + +A transcript and a different closed Session document can pass when their assurance statuses agree. `recordCanary` and qualification bundle regrading reuse this derivation. Digest checks preserve the selected bytes without proving that they belong together. + +`evidence-splice-repro.ts` uses the current positive native fixture and the actual deriveCanaryResult. It gives the close call a correctly nested request naming session:unrelated while preserving the supplied document's closure fields. `evidence-splice-repro.json` reports passed and all six checks true. The supplied durable session ID is . + +For the v2 path, require the close request and returned operation/closure to match the Session ID, closure operation, and revision. Bind reviewer completion to the durable assignment/run in the same chain. Reject missing/conflicting identities. Add a swapped-session regression. Keep the historical derivation unchanged. A similar weakness exists in the historical verifier; this finding concerns the newly added native path, not historical byte drift. + +The release flow inspected in this round selects a versioned profile, freezes the campaign plan, derives canary checks from retained evidence, seals/regrades the qualification bundle, compares rebuilt unpacked contents, and restores the sealed raw archive before publication. The following checks passed within this review's authority. + +- 76 offline tests passed with zero failures. This comprises 27 release/native-canary/restoration tests, 25 historical canary tests, 9 charging/preparation tests, and 15 documentation contract tests. One Git-creating preparation test and three default-launch tests were filtered out. Logs are focused-tests.txt, historical-canary-tests.txt, charging-tests.txt, and documentation-tests.txt. +- The 10.0.0 profile contains 17 required cases on two distinct route providers, 144 primary cells, and 34 environment reserves. Each cell uses its manager model for review. Recovery is off, and canonical release evaluation requires Linux and host 2.1.4 without host/reviewer overrides. These are scheduling cells, not paid dispatch limits. +- Base-code comparison independently reproduces the original 9.1.0 through 9.6.0 policy and host hashes. All six match the current code and historical-policy-baseline.json. Old host policy remains 1.18.31. See historical-policy-comparison.json. +- All 1,562 tracked retained canary/qualification files match base commit 8103f1cd42e1b4d111f25d2a311935d4e41fb997 byte for byte. The CHANGELOG tail beginning with 9.6.0 is unchanged. See history-check.json. +- All six 9.x canary records verify offline in retained mode at a 2030 clock, without rewriting evidence. See historical-canary-verification.json. This is historical verification, not a current freshness claim. +- The artifact-restoration test proves that an equal unpacked manifest permits sealed raw-byte restoration. Wrong version and changed contents are rejected. The v2 release workflow invokes bundle restoration before strict publication evidence verification. The normal tag/ref, durable-record, trusted-publishing, and reconciliation paths remain in place. No publication was attempted. +- actionlint passed for both changed workflows. The latest-2.x compatibility resolver rejects non-2 stable versions. Pinned Linux live smoke remains a separate CI job. See actionlint.txt. +- Package/lock metadata agrees on Flow 10.0.0, exact 2.1.4 development plugin/client/schema dependencies, plugin/schema peer eligibility >=2.1.4 <3, and exact Zod 4.6.7. The current built TUI imports the plugin's leaf API. A final packed installation was not built or launched in this round. +- README and maintained docs state the tested 2.1.4 minimum, later-tested-2.x support, V1 removal, single native package installation, object options, and active-session closure before upgrading. The release cadence exception is explicit. No active V1 launch path was added in the reviewed canary code; historical preparation is readable but refused for active launch. +- The existing local wiki verifier was copied with only its output path redirected into this review directory. It passed all 293 checks across 45 pages. All inventory hashes match the local wiki. See verify-wiki.py and wiki-linkverification.json. Public pages were not read and publication remains held. + +Existing live receipts were read as evidence with limits. /tmp/flow-v2-canary-implementation/flow-canary-native-live-HYujPD/verification.json reports a real 2.1.4 host, loopback provider, three fake requests, one launch charge, native export before cleanup, and a stopped host. Its test source confirms a synthetic plugin and a fake npm shim/dependency symlink. It is useful launch/export/termination evidence. It does not qualify the actual Flow package, Linux, real provider behavior, or the two findings above. Its JSON is not a source-hash seal for this final working tree. I did not rerun it under the no-host/no-model fence. + +The outstanding release gates are not additional code findings. Full deterministic checks on the final integrated tree, frozen installation, replay/audit, final packed smoke, canonical Linux CI, final TUI interaction proof, exact paid matrix and canary approvals, paid qualification, evidence sealing on the frozen candidate, final artifact identity, publication, clean released installation, and public wiki readback remain unverified by this reviewer. No full-green or ready-to-publish verdict is given. + +The new study protocol port and actual-usage proof are outside this review. The native evidence regrader owner is still porting host traces and grader consumers. This report does not certify those active changes. I read qualification integration only far enough to trace the scoped canary and artifact gates. The supplied 61-canary-test and two-qualification-test owner reports were not substituted for this review's direct checks. + +Snapshot evidence is in snapshot-start.json and snapshot-end.json. HEAD is 8103f1cd42e1b4d111f25d2a311935d4e41fb997 on feat/opencode-v2. This is a dirty integration checkout, not a committed or frozen candidate. The initial manifest hashes 121 scoped files. The final manifest includes six late-read dependency hashes. Review findings apply to these unchanged file hashes. + +- scripts/canary-run.ts SHA-256 bb74105030924591606f66104a1ba469073fffc70462d4d2bcfd457541fea06c. +- scripts/eval-canary.ts SHA-256 a5e62d6e86ddd74404c68410cfdf92d4c40530041c88cbf7f6ce3d5cc29d79b6. +- evals/release-policy.ts SHA-256 3a1c9b15097501440a44762d45e0cff20ee725cd40408498c5cd0cbfdc339a28. +- scripts/release-metadata.ts SHA-256 544d8b4095562016e6e919a273bb097ec4c6856c6f330543a41dcb60cd89cd43. +- scripts/restore-exact-release-artifact.ts SHA-256 d596cd54f8c0faad9030d400960ed402aecc54f895c4079bc925ec5791c7d1c1. + +During review, .agents/plans/18-opencode-v2/decisions.tsv and docs/release-qualification.md changed. I reread the new decision rows and the final canary command instructions. They do not resolve either finding. Exact before/after hashes are in snapshot-end.json. An import check also encountered HostTraceSchema missing from evals/host-trace.ts while another owner was working. This is transient integration evidence, outside the reviewed defects. The direct function reproduction avoided that import chain. Later edits have not been verified in this round. + +The local review runtime was Bun 1.3.14 and Node 26.12.1, rather than the repository's Bun 1.4.0 and CI Node 24. This limits the test receipt. The parent owns authoritative pinned-toolchain reruns. + +Poteto workflow receipt follows. The full poteto-mode SKILL.md and inline Principles index were read. No trusted sticky receipt was present, so cross-turn activation was not asserted. The how Investigation/Critique path ran sequentially because the parent prohibited delegates. The user-requested finding format replaces the general explanation format. The existing comment review was inspected; this round files no style or comment findings and does not self-certify a new independent comment pass. Throughput checkpoint is n/a for a read-only investigation. + +Prove It Works changed the review from relying on owner summaries to executing offline regressions, comparing actual historical bytes/base hashes, and preserving reproductions. Boundary Discipline changed the checks toward the launch environment and cross-file evidence binding. No application data model was written or redesigned. No architecture implementation workflow was invoked. Unslop and technical-writing shaped this report's wording, not code findings. + +All review writes are confined to /tmp/flow-v2-release-review-r1. No main-checkout edits, Git writes, delegates, network/connector calls, host launches, provider/model calls, paid calls, goals, automations, commits, pushes, PRs, or tags were made. + +The handoff drift check is final-drift.json. It records later changes separately from the reviewed hashes. Concurrent fixes remain unverified until a fresh round. diff --git a/.agents/plans/18-opencode-v2/reviews/release-r2.md b/.agents/plans/18-opencode-v2/reviews/release-r2.md new file mode 100644 index 00000000..ae682d46 --- /dev/null +++ b/.agents/plans/18-opencode-v2/reviews/release-r2.md @@ -0,0 +1,104 @@ +# Flow 10.0.0 release review, round 2 + +Verdict: PASS. + +Reviewer model is grok-4.7. This is the cross-family audit of the Codex round 1 review, which reported gpt-6.1-sol. No second reviewer examined this round 2 trail. Critique mode's spawn-critics step was skipped because the parent forbade delegates. That limit fails closed. + +Both round 1 P2 findings are closed on the current source. This round found no new causal defect in the scoped canary, release policy, restoration, workflows, package metadata, or maintained release docs. PASS covers that scoped code. It does not approve publication, paid qualification, or a frozen release. + +HEAD is `8103f1cd42e1b4d111f25d2a311935d4e41fb997` on `feat/opencode-v2`. The checkout is dirty. The history check saw 154 porcelain entries. `CHANGELOG.md` is the only dirty path under `evals/canary`, `evals/qualification`, and `CHANGELOG.md`. Scoped file hashes did not change between `snapshot-start.json`, `snapshot-end.json`, and the final repro. `final-drift.json` records no drift. + +## Closed findings + +### Round 1 P2. Ambient host selectors before canary launch. Closed. + +`scripts/canary-run.ts:62-79` `canaryLaunchEnvironment` copies the inherited environment, sets `PWD` to the fixture, deletes `FLOW_EVAL_AUTHORIZATION` and `TYPESAFE_API_KEY`, deletes every `OPENCODE_*` name except `OPENCODE_API_KEY`, and deletes `NODE_OPTIONS`, `NODE_PATH`, `BUN_OPTIONS`, and `BUN_PRELOAD`. Launch calls this function before `npm install`, the version check, and `opencode serve` (`scripts/canary-run.ts:126-190`). It then sets `OPENCODE_FLOW_REVIEWER_MODEL` from the approved model argument. + +The independent repro injected `OPENCODE_CONFIG`, `OPENCODE_CONFIG_CONTENT`, `OPENCODE_CONFIG_DIR`, `OPENCODE_CLI_CONFIG_CONTENT`, `OPENCODE_MODELS_URL`, `OPENCODE_MODELS_PATH`, `OPENCODE_DISABLE_PROJECT_CONFIG`, `OPENCODE_SERVER_PASSWORD`, an unapproved reviewer model, the experimental websocket and native-LLM names, Node and Bun preload hooks, `FLOW_EVAL_AUTHORIZATION`, and `TYPESAFE_API_KEY`. The returned environment kept `OPENCODE_API_KEY` and the four provider keys. The ambient selectors were absent. `repro-final.json` matches the first repro. + +The pinned local binary at the plan-host cache, SHA-256 `d8a65dd253dbea759ae0dd829792ff219b40178c7decffea822430a4d1ccadb3`, contains `OPENCODE_CONFIG`, `OPENCODE_CONFIG_CONTENT`, `OPENCODE_CONFIG_DIR`, `OPENCODE_CLI_CONFIG_CONTENT`, `OPENCODE_MODELS_URL`, `OPENCODE_MODELS_PATH`, `OPENCODE_SERVER_PASSWORD`, and `OPENCODE_DISABLE_PROJECT_CONFIG`. It does not contain `OPENCODE_EXPERIMENTAL_WEBSOCKETS`, `OPENCODE_EXPERIMENTAL_NATIVE_LLM`, or `XDG_CONFIG_DIRS`. The binary was not executed. `vendor-env-names.json` records the scan. + +`tests/canary-run.test.ts:304` asserts the same removal. That test passed in this round. + +### Round 1 P2. Native close evidence bound to another session. Closed. + +For package versions at or above 10, `deriveCanaryResult` (`scripts/eval-canary.ts:891`) parses the supplied document with `SessionSchema` and applies `nativeClosedSessionMatches` (`scripts/eval-canary.ts:336-373`) and `nativeCompletedRunsMatch` (`scripts/eval-canary.ts:376-426`). Historical versions skip both binds because `boundClose` and `boundRuns` are forced true when `native` is null (`scripts/eval-canary.ts:939-955`). + +The close request must parse and canonical-equal `closureRetryRequest(session)`. That request carries the supplied session id, closure operation id, kind, and expected revision. The response operation id and revision, closure entity, archived projection, and delivery projection must match that same session. Reviewer completion must match the durable feature run's feature id, assignment, summary, expected revision, result, committed operation, and entity, including source and validation digests on the run. `closes-with-delivery` requires both binds and the existing assurance-status check (`scripts/eval-canary.ts:1012-1017`). + +The repro used the current native fixture. The positive control passed all six checks. Putting `session:unrelated` on the close request failed `closes-with-delivery`. A different valid durable session with equal assurance failed. Swapping validation and review source digests failed `captures-validation` and `closes-with-delivery` while the assurance JSON stayed equal. The focused suite also passed the native splice and reviewer-binding tests named in `focused-tests.txt`. + +## Checks run in this round + +Bun was `/tmp/flow-v2-toolchain/node_modules/.bin/bun` 1.4.0. The focused command was `bun test` on these seven files: `tests/release-1000.test.ts`, `tests/canary-v2-native.test.ts`, `tests/canary-run.test.ts`, `tests/restore-qualified-artifact.test.ts`, `tests/documentation-contract.test.ts`, `tests/eval-canary.test.ts`, and `tests/paid-budget.test.ts`. Result: 114 pass, 0 fail, 600 `expect()` calls, 2.17s. Log: `focused-tests.txt`. + +`releaseCatalog("10.0.0")` has 17 cases. Two models on distinct route providers produce 144 primary cells and 34 environment reserves. Every cell's reviewer model is its manager model. `releaseReviewerModel("10.0.0", alternate)` returns that alternate manager. `releaseHostPolicy("10.0.0")` is OpenCode 2.1.4, Linux, `reviewerSteps` null, `ambientConfig` `"disabled"`, `transport` `"http"`. `assertReleaseModels` (`evals/release-policy.ts:330-347`) requires exactly two models on distinct route providers. `assertReleaseHost` (`evals/release-policy.ts:447-472`) rejects a recovery API key, a non-Linux platform, and host or reviewer overrides. The measured v2 policy digest is `sha256:86953cd5da1a0af6a541243abd1d8b92ec5d4c2e0aff26334a5be1d21187c3f6`. The 144 and 34 figures are scheduling cells. `docs/release-qualification.md:132-136` keeps the historical 9.6.0 dispatch example separate and tells the operator to derive the v2 dispatch limit from frozen scenario steps, followups, and route probes. `runPaidCanary` consumes one canary dispatch at `scripts/canary-run.ts:59` before launch, and it refuses a preparation whose `schemaVersion` is not 2 (`scripts/canary-run.ts:47-50`). + +Current `evals/release-policy.ts` reproduces the baseline policy and host digests for 9.1.0 through 9.6.0. All six match `.agents/plans/18-opencode-v2/historical-policy-baseline.json`, and each host pin is 1.18.31. Digests are in `repro-final.json`. This comparison imports the current module. It does not execute the base commit's file as a separate program. The retained evidence bytes are a separate check below. + +`git diff --name-only 8103f1cd42e1b4d111f25d2a311935d4e41fb997 -- evals/canary evals/qualification` is empty. Those directories contain 1,562 tracked files. The `CHANGELOG.md` bytes from `## [9.6.0]` through the end match the base commit. Tail SHA-256 `a57ea2d173e2ba978d06341442c08067ebaf6cc0a719aad902558e8df3ab39be`. The base file begins with the changelog title and then 9.6.0. The working tree inserts the 10.0.0 notes before that heading. + +All six retained 9.x canary records regrade through `canaryRecordIssue` with freshness `retained` at `2030-01-01T00:00:00.000Z`. Each returns no issue, status `passed`, and derivation `canary-evidence-v1`. The same records under freshness `required` return `Canary is expired.` This is offline historical verification. It is not a freshness claim for a 10.0.0 canary. Evidence: `historical-canary-verification.json`. + +`scripts/restore-exact-release-artifact.ts:46-60` checks the sealed digest, compares `unpackedManifestSha256` of the rebuilt tarball and the sealed bytes, and only then overwrites the rebuilt path with the sealed raw bytes. A digest or content mismatch throws before the write. The focused restoration test passed. `.github/workflows/release.yml:132-136` restores a major version of at least 10 on the verify job when `evals/qualification/archives/${version}.json` exists. The publish job at `.github/workflows/release.yml:246-249` always materializes that archive for a major version of at least 10, so a missing archive fails the publish job. `scripts/release.ts:248` sends ordinary publication through `assertStrictReleaseEvidence`, which checks the canary record and rederives it from retained bytes before the bundle regrade (`scripts/release-metadata.ts:490-532`). The verify job's pre-publication command uses `--mode dry-run`. Dry-run prints the verdict and does not set exit code 1 (`scripts/eval-canary.ts:1922-1936`). Strict mode does. No publication was attempted. + +`actionlint` exited 0 for `.github/workflows/release.yml` and `.github/workflows/opencode-compatibility.yml`. Output file `actionlint.txt` is empty. The compatibility job resolves `@opencode/cli@2` and keeps versions matching `/^2\.\d+\.\d+$/` (`.github/workflows/opencode-compatibility.yml:36-38`). Both reviewed workflows pin checkout, setup-node, and setup-bun, and they pin Bun 1.4.0 and Node 24. The live registry was not queried. + +`package.json` is Flow `10.0.0`, `packageManager` `bun@1.4.0`, engines `node >=24`, peers `@opencode/plugin` and `@opencode/schema` `>=2.1.4 <3`, dependencies `zod` `4.6.7` and `saxes` `6.0.0`, and devDependencies `@opencode/plugin`, `@opencode/client`, and `@opencode/schema` at exact `2.1.4`. `bun.lock` records the same versions. Published `files` include `README.md` and `CHANGELOG.md`. They do not include `evals/`. `OPENCODE_HOST_VERSION` is `"2.1.4"` (`evals/opencode-host.ts:13`). `openCodeInvocation` rejects a version below 2.1.4 and any version outside stable 2.x (`evals/opencode-host.ts:15-44`). `prepareCanary` reads `package/package.json` from the packed artifact and refuses a package that does not target OpenCode v2 (`scripts/eval-canary.ts:1376-1386`). + +`registerHttpTransport` sets provider, model, and variant `settings.transport` to `"http"` (`src/platform/opencode/http-transport.ts:3-18`). `src/platform/opencode/plugin-composition.ts:154` calls it. Release and preflight host starts pass `ambientConfig: "disabled"` (`evals/run.ts:828` and `evals/run.ts:1113-1116`). The canary writes the same HTTP setting on provider, model, and default variant (`scripts/canary-run.ts:101-118`) and reads it back before the prompt and again before export (`scripts/canary-run.ts:266-304`). Installed `@opencode/schema` 2.1.4 defines transport as optional `"http"` or `"websocket"`. + +README, CHANGELOG, `docs/index.md`, `docs/quickstart.md`, and `docs/release-qualification.md` state the tested 2.1.4 minimum, later tested 2.x support, removal of OpenCode 1, object options, and active-session closure before an upgrade. The cadence exception for 10.0.0 is explicit in `docs/release-qualification.md:94-97` and `CHANGELOG.md:5-9`. README lines 50-51 disclose HTTP transport selection while Flow is loaded. + +The local wiki verifier was copied from round 1 with its output path moved into this directory. It passed 293 checks across 45 pages. `wiki-summary.json` and `wiki-linkverification.json` record the result. Resolution was local files and anchors. Public pages were not read. + +The parent-pointed tarball `/tmp/flow-v2-candidate-review/opencode-plugin-flow-10.0.0.tgz` hashes to `224f3c78b5376a6178356fd7e37fb31420b31c147f4b1db1617ab582e0dd843e` (366,774 bytes). This round hashed that file. It did not install it, launch it, or treat another owner's packed receipts as measurements. + +## Limits of the PASS + +These are limits of what the passing code proves. They are not additional filed defects. + +Launch-time `assertProviderHTTP` accepts a selected model whose `settings.transport` is absent, and it rejects a present value other than `"http"` (`scripts/canary-run.ts:285-286`). Provider transport and the selected variant must be `"http"`. The fixture writes all three. `deriveCanaryResult` seals artifact, installation, actors, host, and platforms (`scripts/eval-canary.ts:1035-1049`). That digest does not include the provider readback or `run.json`. An offline regrade of a recorded canary does not re-prove HTTP transport. + +`canaryLaunchEnvironment` keeps provider API keys. It also leaves proxy and certificate variables untouched. The repro preserved `HTTPS_PROXY`. Provider access is intentional. A proxy can still redirect that access. This round did not treat that as an ambient OpenCode config selector. + +`evals/harness.ts:1994-2000` deletes only `OPENCODE_CONFIG`, `OPENCODE_CONFIG_CONTENT`, and `OPENCODE_CONFIG_DIR` when `ambientConfig` is `"disabled"`. Release and preflight pass that flag. The canary strip is wider. Harness, study protocol, and active fixture ports were not reviewed to completion. The difference is recorded so a later eval review can close it. It does not reopen the canary finding. + +`evals/README.md:349` says historical 1.18.6 policies remain readable with their original hashes. Release policy, README, CHANGELOG, and `docs/release-qualification.md` state the 9.x release host as 1.18.31. `evals/` is outside the published package files. This sentence was not filed as a packed-doc defect. Study protocol remains outside the completed review. + +The machine Node is v26.12.1. The focused tests and repros ran on Bun 1.4.0. CI pins Node 24. This round did not rerun the suite on Node 24. + +`npm exec --yes` in `openCodeInvocation` can fetch a host package. This round did not launch a host, call a model, or make a network request. + +## Unverified gates + +These gates remain open. They are not code findings from this round. + +Full deterministic `bun run check` on the final integrated tree, frozen installation, replay, and audit were not run here. Canonical Linux CI was not run. A final packed install and smoke were not run here. TUI interaction was not observed. Exact paid matrix approval, paid qualification, evidence sealing of a frozen 10.0.0 candidate, final artifact identity after that seal, publication, a clean released installation, and public wiki readback remain unverified. No 10.0.0 canary record was regraded because that live evidence is not in the retained 9.x set. Owner live and packed receipts were not executed again and are not this reviewer's measurements. + +No commit, push, pull request, tag, or publish was made. + +## Snapshot hashes + +Round 1 hashes for the two files that changed: + +- `scripts/canary-run.ts` was `bb74105030924591606f66104a1ba469073fffc70462d4d2bcfd457541fea06c`. It is now `39fff632e149c3a458c536be6062f7d63e47b7f2d05caf19d2eb1bfe4695fe45`. +- `scripts/eval-canary.ts` was `a5e62d6e86ddd74404c68410cfdf92d4c40530041c88cbf7f6ce3d5cc29d79b6`. It is now `e7f38a3c50b6ee7ffc7c2a19cfa383848206704753a3a27c2649a1b40ab11513`. +- `evals/release-policy.ts` was `3a1c9b15097501440a44762d45e0cff20ee725cd40408498c5cd0cbfdc339a28`. It is now `bd4405901cd5c7576c12f4a4284bc174fd316e805381f0cb572caab4986bf2e9`. + +Unchanged from the round 1 snapshot: + +- `scripts/release-metadata.ts` `544d8b4095562016e6e919a273bb097ec4c6856c6f330543a41dcb60cd89cd43`. +- `scripts/restore-exact-release-artifact.ts` `d596cd54f8c0faad9030d400960ed402aecc54f895c4079bc925ec5791c7d1c1`. + +`scripts/export-canary-native.ts` is `963f2fcba75f7d23e9a0096b820c2cb6b7f876cb14ef7cb41f7dac8fc00b52f2`. The full 36-file manifest is `snapshot-end.json`. + +## Poteto receipt + +The poteto-mode skill and its inline Principles index were read before the reproductions. Leaf skills applied here were how (Critique), principle-prove-it-works, principle-boundary-discipline, principle-fix-root-causes, principle-build-the-lever, principle-test-behavior-not-implementation, principle-explain-the-number, principle-outcome-oriented-execution, unslop, technical-writing, and show-me-your-work. No trusted sticky receipt was present. + +Prove It Works replaced owner summaries with the repro, the 114-test run, the retained-byte diff, and the six-record regrade. Boundary Discipline kept the review on the launch environment and the session identity chain. Fix Root Causes reran the splice and the ambient selectors against the current functions. Build the Lever kept those checks in `repro.ts` and `historical-verify.ts`. Test Behavior Not Implementation judged observable status and check booleans. Outcome-Oriented Execution confirmed schema 2 refusal and the 2.x host gate. Explain the Number ties 17, 144, 34, 114, 1,562, and 293 to the commands above. Unslop and technical-writing shaped this report's wording. Show-me-your-work wrote `decisions.tsv`. No application data model was written. No architecture implementation workflow was invoked. + +throughput checkpoint: n/a, read-only investigation + +All review writes are under `/tmp/flow-v2-release-review-r2`. No main-checkout edits, Git writes, delegates, network calls, host launches, provider calls, paid calls, commits, pushes, pull requests, or tags were made. diff --git a/.agents/plans/18-opencode-v2/reviews/typesafe-env-r1.md b/.agents/plans/18-opencode-v2/reviews/typesafe-env-r1.md new file mode 100644 index 00000000..f3daefb1 --- /dev/null +++ b/.agents/plans/18-opencode-v2/reviews/typesafe-env-r1.md @@ -0,0 +1,84 @@ +Bounded PASS. No causal bug found in the configurable Typesafe key source or the explicit internal-off correction. No release approval is given. + +The review covers the frozen `plugin.ts`, `typesafe-key-env.ts`, and `typesafe-key-env.test.ts`, plus the current name-option documentation. I read the supplied handoff, hash inventory, and decisions. The three implementation and test hashes match the handoff. The three documentation hashes already differed when review began, consistent with the parent's stated shortening. No finding requires a source fix. There are no severity-ranked defects or failing product reproductions to report. + +Overview. + +The data shape is a setup-scoped `() => string | undefined` reader. `resolveTypesafeApiKeyReader` selects and validates one environment variable name. The wrapper closes over that reader for both bare `/flow-auto` defaults and the actual Jev provider. The key value remains a runtime lookup. Each setup creates its own closure and controller. + +For an operator, a custom name selects only that variable. A populated default cannot substitute for a missing or blank custom value. The maintainer inherits a small configuration boundary without changing composition dependency signatures or adding a V1 adapter. + +How it works and reviewed boundaries. + +- [typesafe-key-env.ts:6](/Users/vriesd/projects/flow-opencode/src/platform/opencode/typesafe-key-env.ts:6) treats omission and explicit undefined as the default name. Null and other non-string values are invalid. The two character checks reject empty names, leading digits, punctuation, padded names, Unicode, CR, LF, and NUL. They avoid the trailing-newline exception of an anchored JavaScript `$` alone. Names are not trimmed. +- [typesafe-key-env.ts:13](/Users/vriesd/projects/flow-opencode/src/platform/opencode/typesafe-key-env.ts:13) emits a fixed warning and returns an unavailable reader for invalid configuration. A production probe verifies exactly one warning across repeated commands and provider assessments. Flow status remains operational. Neither the invalid value nor synthetic credentials appear in the observed logs or status. +- [typesafe-key-env.ts:18](/Users/vriesd/projects/flow-opencode/src/platform/opencode/typesafe-key-env.ts:18) captures the environment object, not the key value. Every invocation checks `Object.hasOwn`. Inherited getters are not touched. Own entries named `constructor`, `toString`, and `__proto__` work. Deleting an own value leaves it unavailable even when an inherited value exists. Nonblank values are trimmed. Blank values return undefined. Environment values in production are strings or undefined. +- [plugin.ts:8](/Users/vriesd/projects/flow-opencode/src/platform/opencode/plugin.ts:8) exports the existing `opencode-plugin-flow` ID. The installed SDK is `@opencode/plugin` 2.1.4. Its actual `dist/promise/plugin.js:1-3` implements `define` as an identity function. Its `Plugin.setup` contract permits a cleanup promise. Therefore the inner definition does not register a second native plugin, and returning `.setup(ctx)` preserves the cleanup result. +- [plugin.ts:23](/Users/vriesd/projects/flow-opencode/src/platform/opencode/plugin.ts:23) still passes this module's `import.meta.url` to composition. A production status probe compares `runtimeIdentity.pluginEntrySha256` with the independently computed SHA-256 of that entry file. It matches. This proves the source entry path, not a future packed bundle. +- [plugin-composition.ts:138](/Users/vriesd/projects/flow-opencode/src/platform/opencode/plugin-composition.ts:138) retains disposal ownership. The wrapper probe verifies that disposal removes tools, commands, event subscriptions, and hooks. Repeated disposal is harmless. A fresh setup in the same workspace remains operational. A rejected command registration also cleans its earlier registrations and leadership before a fresh setup succeeds. +- [plugin.ts:25](/Users/vriesd/projects/flow-opencode/src/platform/opencode/plugin.ts:25) uses the same reader as the provider. Two production contexts selecting different names remain isolated after both setups exist. Adding, rotating, and deleting synthetic environment values changes the next command default and assessment header. Mutating the original option after setup does not change the selected name. The wrong default remains unused. +- [jev-decision-provider.ts:82](/Users/vriesd/projects/flow-opencode/src/infrastructure/jev-decision-provider.ts:82) reads the key before transport. The unchanged transport uses `Authorization: Bearer `, the existing Typesafe endpoint, and redirect mode `error`. Literal fake-fetch headers verify the selected trimmed value. The observed request bodies, status, and logs do not contain the synthetic credentials. No new persistence or telemetry path receives the key. + +The off correction. + +[plugin.ts:16-22](/Users/vriesd/projects/flow-opencode/src/platform/opencode/plugin.ts:16) disables this source when the recovery object has mode exactly `off`. It does not broadly interpret another recovery schema. Both ambient and custom keys then remain unavailable to the provider, and bare `/flow-auto` has no shadow defaults. + +[command-hook.ts:119-126](/Users/vriesd/projects/flow-opencode/src/platform/opencode/command-hook.ts:119) still gives explicit command settings precedence. With internal plugin-off, an explicit shadow command can display its explicit limits while the provider remains unavailable. The probe observes two calls and $0.01 in that status, zero fetch calls, and a direct provider result of `missing-key`. This is consistent with the stated source-disable contract. It is not an authentication bypass. + +With command `--recovery=off` alone, status is off and setup, command, and status issue zero fetch calls. A separate direct provider assessment can still use the configured reader. The next bare command can enable the normal shadow defaults again. That direct assessment is synthetic and bypasses the recovery controller deliberately. It is not evidence of a runtime recovery request while off. + +The controller still establishes its existing invocation and retry fencing when advice is off. The wrapper neither supplies delegated settings nor changes authority policy. Shadow remains six attempts and $0.02 by default. Nine surrounding source files, including composition, controller, lifecycle, command handling, leadership, and Jev provider and transport, match the prior core R5 inventory exactly. See [core-comparison.json](/tmp/flow-v2-typesafe-env-review-r1/core-comparison.json). Native OAuth machinery was not reviewed here. + +Verification. + +The tests ran with the pinned `/tmp/flow-v2-toolchain/node_modules/.bin/bun`, version 1.4.0. The driver uses a synthetic environment allowlist, an isolated home and temporary directory, and macOS `sandbox-exec`. The [sandbox profile](/tmp/flow-v2-typesafe-env-review-r1/offline.sb) denies network operations and filesystem writes outside this review directory. Production fixture children also inherit that sandbox. Existing test fetch spies and the added fake fetch handle all assessments. + +- [focused.log](/tmp/flow-v2-typesafe-env-review-r1/focused.log) records 92 passing tests, zero failures, and 331 assertions across the six requested focused files. I read all 37 new tests, including the fixture setup and subprocess isolation. The provider spy preserves the real implementation. Production tests assert native command status, literal auth headers, missing-key results, and zero pre-assessment requests. +- [challenges.log](/tmp/flow-v2-typesafe-env-review-r1/challenges.log) records eight additional passing probes and 576 assertions. They cover ASCII validation, omitted undefined, inherited environment getters, deletion, unusual invalid JavaScript values, per-context isolation, runtime rotation, option freezing, entry hashing, cleanup, setup failure, both off paths, and one generic production warning. +- [documentation.log](/tmp/flow-v2-typesafe-env-review-r1/documentation.log) records 15 passing tests and 126 assertions. Maintained prose is 106,656 bytes within 107,100. The older handoff's budget failure is superseded. I checked the current README, maintainer contract, and troubleshooting text for the name-only option and server environment. Native-account documentation remains surrounding work. + +The first added cleanup probe guessed two subscriptions and observed three. Source inspection identified the existing composition, model-preference, and lifecycle subscriptions. I changed only the temporary probe to assert active subscriptions before cleanup and zero afterward. The original failed run remains in [challenges-first.log](/tmp/flow-v2-typesafe-env-review-r1/challenges-first.log). No product change was made to obtain passing tests. + +Run the complete bounded verification again with: + +```sh +python3 /tmp/flow-v2-typesafe-env-review-r1/run-checks.py +``` + +The driver exits nonzero if any selected check fails. To rerun only the added probes, append `challenges`. + +Identity and drift. + +[scoped-before.sha256](/tmp/flow-v2-typesafe-env-review-r1/scoped-before.sha256) and [scoped-after.sha256](/tmp/flow-v2-typesafe-env-review-r1/scoped-after.sha256) record all six feature-owned files. The code hashes remain: + +```text +9f4e1a64fc3106a98f7496b4f915e6f9c31eabac8c2cd21bc247beb7a517d688 src/platform/opencode/plugin.ts +7d935e078b49a82ec3e1ffab5526158375ee55194b6c6bcf38b4fadfa044132d src/platform/opencode/typesafe-key-env.ts +91bf9142043a12664a8d7c2eaba5d9206f393a878b401df4b53e7b315fa897b0 tests/typesafe-key-env.test.ts +``` + +[before.json](/tmp/flow-v2-typesafe-env-review-r1/before.json) and [after.json](/tmp/flow-v2-typesafe-env-review-r1/after.json) cover 6,746 files under source, tests, scripts, docs, evals, and root-level files. This inventory excludes Git internals, node_modules, dist, and .agents. [drift.json](/tmp/flow-v2-typesafe-env-review-r1/drift.json) is empty. No captured file changed during the review. [handoff-comparison.json](/tmp/flow-v2-typesafe-env-review-r1/handoff-comparison.json) separates pre-existing documentation changes from review-time drift. SDK and supplied evidence hashes are in [evidence-sha256.json](/tmp/flow-v2-typesafe-env-review-r1/evidence-sha256.json). + +Principles and workflow receipt. + +Poteto Mode and its inline Principles index were read in full. How ran sequentially under the user's no-agents fence. The Investigation output is this cited explanation and judgment. Throughput checkpoint is n/a for a read-only investigation. No trusted sticky-hook receipt was present, so no cross-turn activation is claimed. + +- Model the Domain named the setup-scoped reader before writing the probes. The context matrix checks one fixed name with changing values rather than treating options as stored credentials. +- Boundary Discipline focused validation attacks on configuration and environment lookup. It kept OAuth machinery and controller policy outside this option review. +- Test Behavior, Not Implementation chose production setup, observable status, literal headers, and empty registrations after cleanup. It also removed the unsupported exact subscription-count assumption from the temporary probe. +- Prove It Works required inspecting actual SDK semantics and source hashes, then limited proof to source plus the fake host and transport actually exercised. +- Build the Lever produced `run-checks.py`, `snapshot.py`, and the rerunnable `challenges.test.ts` inside the assigned directory. + +The technical-writing and unslop skills shaped the report. The no-comments skill was read. Its independent reviewer could not run under the user's explicit no-agents instruction. The supplied inventory contains three public-contract comment keeps in `evals/harness.ts` and zero suppressions. I read that inventory and scanned the three feature TypeScript files for comment and suppression markers, finding none. This is not an independent comment review. + +Trail audit and limitations. + +[decisions.tsv](/tmp/flow-v2-typesafe-env-review-r1/decisions.tsv) records this run's choices and the probe correction. Rows were checked against this conversation's tool results and the files cited above. No supported task-history tool was used and no private history store was read. The rerunnable [audit.py](/tmp/flow-v2-typesafe-env-review-r1/audit.py) validates evidence pointers, final check results, report links, marker scans, and matching snapshots. Its output is [trail-audit.json](/tmp/flow-v2-typesafe-env-review-r1/trail-audit.json). Evidence files resolve. The first probe failure is preserved rather than hidden. Cross-model trail review did not run because agents were forbidden. + +I made no repository edit, Git write, build, pack, network request, paid call, real-credential read, native user-service operation, connector call, or persistent task creation. Global typecheck and lint were owner-reported and were not independently rerun here. The fixture calls production setup against installed SDK interfaces, but it is not a running OpenCode server. Linux behavior, the final packed artifact and entry hash, native auxiliary runtime behavior, and native OAuth proof remain with their owners. No final archive was claimed to contain this source. + +Attention. + +reviewed by none for the cross-model trail audit. + +The parent must still build once the source reviews close and run the final native packed gates. This PASS closes only the bounded configurable-key source review and the stated internal-off correction. diff --git a/.agents/plans/18-opencode-v2/runtime/README.md b/.agents/plans/18-opencode-v2/runtime/README.md new file mode 100644 index 00000000..451c0f5c --- /dev/null +++ b/.agents/plans/18-opencode-v2/runtime/README.md @@ -0,0 +1,31 @@ +# Local runtime evidence + +The repository contribution skill excludes generated release smoke artifacts from commits unless a maintainer asks to archive each exact artifact. The parent moved these early compact receipts out of the checkout without changing their bytes. Their original decision-log references are historical and are superseded by these locations. + +- [canary-r1-fixes.json](/tmp/flow-v2-release-smoke-scaffolding/canary-r1-fixes.json) has SHA-256 `664f0ba2732b0d816d7d6bbeaec9743e76c72e1d05d5666c7c5b0d6e81c2d36b`. +- [packed-continuation-224f.json](/tmp/flow-v2-release-smoke-scaffolding/packed-continuation-224f.json) has SHA-256 `7da839ed378a26c9cd17898decb5d1f16c78ba64a6bd8c424a12a50c90178514`. +- [packed-eval-224f.json](/tmp/flow-v2-release-smoke-scaffolding/packed-eval-224f.json) has SHA-256 `cdb9385b81e7819f2bb2b67968f7686e1f73162f053dc1551896bbd589b6b99d`. +- [packed-picker-224f.json](/tmp/flow-v2-release-smoke-scaffolding/packed-picker-224f.json) has SHA-256 `af4c84066882c45e81a4d2448c7f4caab53b01dc4a6befb37e68a104685325af`. + +These receipts describe older candidates. Final runtime proof is recorded in the implementation checkpoint and final verification notes. No receipt here authorizes qualification or publication. + +## Terminal replay prototype + +The parent built `/tmp/flow-v2-terminal-render/frames.cjs` to replay recorded native PTY cells through xterm/headless 6.0.0. It extracts role, model, and confirmation screens and emits self-contained HTML. The prototype reads only the old d3d46d92 capture and remains labeled rejected. The original ANSI source is unchanged. + +`/tmp/flow-v2-terminal-render/prototype/replay.html` was rendered at 1200 pixels. All three captured dialogs are readable at the recorded 120-column, 36-row geometry. The viewer excludes raw terminal history and refuses obvious credential-bearing visible frames. It does not control Terminal or constitute accepted-candidate visual sign-off. + +After the new exact archive passes picker verification, rerun the extractor against that archive's recorded ANSI file, with its full raw hash and `candidate` as the last arguments. Verify the capture belongs to that candidate's picker receipt. Review the refreshed frames before merging. Keep generated captures outside the commit. + + +## R8 recorded terminal inspection + +The final replay uses the actual fc19ca4a archive and ANSI source +`1da34ba1eaa1c3af9a4f1ab5919a04ea9d40db6698e4ce5351d8b72f2bca5202`. +The picker receipt binds that capture to its eight native phases. The parent +inspected role, model, and save states at the recorded geometry, plus scaled +728- and 360-pixel replay layouts. The self-contained viewer was previewed with +zero console errors and shown in the task. Evidence remains local at +`/tmp/flow-v2-terminal-render/r8-fc19ca4a/responsive/visual-inspection.json`. +This verifies recorded terminal cells with browser font rendering. It does not +claim a live native terminal pixel capture or release qualification. diff --git a/.agents/plans/18-opencode-v2/transport/manifest.json b/.agents/plans/18-opencode-v2/transport/manifest.json new file mode 100644 index 00000000..cc55a7c6 --- /dev/null +++ b/.agents/plans/18-opencode-v2/transport/manifest.json @@ -0,0 +1,6 @@ +{ + ".agents/plans/18-opencode-v2/transport/source-seal.log": "a632b365d4042b68d0be7d4b201e3a2d3c803d0324f9fe8110498f9cb7b7b17b", + ".agents/plans/18-opencode-v2/transport/probe.ts": "b0d2ff7cbaf38e0ba3b72b396c969f294ce3005a4113f556e872c8821c25eca4", + ".agents/plans/18-opencode-v2/transport/session-hook-counterexample.json": "4258a7a89b31b8291a23c15c108b03ccd30d768bd93c7aeb629330b2df6d4ac6", + ".agents/plans/18-opencode-v2/transport/verification.json": "d6146d81f08d0fedfed908923a49a96ced60cf3143ed842cb4819f436ad66660" +} diff --git a/.agents/plans/18-opencode-v2/transport/probe.ts b/.agents/plans/18-opencode-v2/transport/probe.ts new file mode 100644 index 00000000..17592d5c --- /dev/null +++ b/.agents/plans/18-opencode-v2/transport/probe.ts @@ -0,0 +1,51 @@ +import { spawn } from 'node:child_process'; +import { mkdir, writeFile } from 'node:fs/promises'; +import { join } from 'node:path'; +import { OpenCodeStartup } from '/Users/vriesd/projects/flow-opencode/evals/opencode-host.ts'; +const base='/tmp/flow-v2-http-contract'; +const executable='/tmp/flow-opencode-v2-plan-host/npm-cache/_npx/2314e955696d2fea/node_modules/@opencode/cli-darwin-arm64/bin/opencode'; +const results=[]; +for(const mode of ["bare","hooked","forced"]) { + const hooked=mode!=="bare"; + const root=join(base,mode+'-3'); + for(const leaf of ['home','config/opencode','data','state','cache','tmp','project','plugin']) await mkdir(join(root,leaf),{recursive:true}); + let upgrades=0,http=0,frames=0; + const completed={type:'response.completed',response:{id:'resp_local',object:'response',created_at:1,status:'completed',model:'gpt-6.1-sol',output:[{type:'message',id:'message_local',role:'assistant',status:'completed',content:[{type:'output_text',text:'OK',annotations:[]}]}],usage:{input_tokens:1,output_tokens:1,total_tokens:2}}}; + const provider=Bun.serve({hostname:'127.0.0.1',port:0,fetch(request,server){ + if(request.headers.get('upgrade')?.toLowerCase()==='websocket'){upgrades++; if(server.upgrade(request))return;return new Response('no upgrade',{status:400});} + http++; return new Response(`data: ${JSON.stringify(completed)}\n\n`,{headers:{'content-type':'text/event-stream'}}); + },websocket:{message(ws,message){frames++;ws.send(JSON.stringify(completed));}}}); + const code=`import {Plugin,Provider,Model} from '@opencode/plugin'; +import {registerHttpTransport} from '/Users/vriesd/projects/flow-opencode/src/platform/opencode/http-transport.ts'; +export default Plugin.define({id:'flow.transport.probe',async setup(ctx){ + const id=Provider.ID.make('flow-transport');const model=Model.Info.default(id,Model.ID.make('gpt-6.1-sol')); + await ctx.provider.transform(e=>e.add({info:{...Provider.Info.empty(id),activation:'enabled',package:'@opencode/ai/providers/openai/responses',settings:{baseURL:${JSON.stringify(provider.url+'v1')},apiKey:'local-synthetic-key',transport:'websocket'}},models:[{...model,variants:[{id:'force-ws',settings:{transport:'websocket'}}],settings:{transport:'websocket'},capabilities:{...model.capabilities,tools:true}}]})); + await ctx.session.hook('title',i=>{i.result='Local transport proof'}); + ${mode==="forced"?"await registerHttpTransport(ctx.provider);":""} + ${hooked?"const hook=await ctx.session.hook('http.request',()=>{});return ()=>hook.dispose();":""} +}});`; + await writeFile(join(root,'plugin/index.ts'),code); + await writeFile(join(root,'project/opencode.json'),JSON.stringify({plugins:[{package:join(root,'plugin')}]})); + const link=Bun.spawnSync(['ln','-s','/Users/vriesd/projects/flow-opencode/node_modules',join(root,'node_modules')]); + const listener=Bun.serve({hostname:'127.0.0.1',port:0,fetch:()=>new Response()});const port=listener.port;listener.stop(true); + const env={PATH:process.env.PATH,HOME:join(root,'home'),XDG_CONFIG_HOME:join(root,'config'),XDG_DATA_HOME:join(root,'data'),XDG_CACHE_HOME:join(root,'cache'),XDG_STATE_HOME:join(root,'state'),TMPDIR:join(root,'tmp')}; + const child=spawn(executable,['serve','--hostname','127.0.0.1','--port',String(port)],{cwd:join(root,'project'),env,stdio:['ignore','pipe','pipe']}); + const startup=new OpenCodeStartup();child.stdout.on('data',c=>startup.consume(String(c)));child.stderr.on('data',c=>startup.consume(String(c),'stderr')); + let result; + try{ + let client=null;const deadline=Date.now()+10000; + while(Date.now()child.once('exit',resolve));provider.stop(true);} + results.push(result); +} +await writeFile(join(base,'verification.json'),JSON.stringify({results,pass:results[0].upgrades>0&&results[2].upgrades===0&&results[2].http>0},null,2)); +console.log(JSON.stringify(results.map(({native,...r})=>r))); +if(!(results[0].upgrades>0&&results[2].upgrades===0&&results[2].http>0))process.exitCode=1; diff --git a/.agents/plans/18-opencode-v2/transport/session-hook-counterexample.json b/.agents/plans/18-opencode-v2/transport/session-hook-counterexample.json new file mode 100644 index 00000000..ec21e05d --- /dev/null +++ b/.agents/plans/18-opencode-v2/transport/session-hook-counterexample.json @@ -0,0 +1,119 @@ +{ + "results": [ + { + "hooked": false, + "host": "2.1.4", + "pid": 46940, + "upgrades": 1, + "http": 0, + "frames": 1, + "waitError": null, + "native": [ + { + "id": "msg_16d02df660011JUiWjdDmt8E4u", + "time": { + "created": 1792830267362 + }, + "text": "Reply OK only.", + "type": "user" + }, + { + "id": "msg_16d02dfe7001YjxjVNa12ijxFt", + "time": { + "created": 1792830267371, + "completed": 1792830267377 + }, + "type": "assistant", + "agent": "build", + "model": { + "id": "gpt-6.1-sol", + "providerID": "flow-transport", + "variant": "default" + }, + "content": [], + "finish": "error", + "error": { + "type": "provider.invalid-output", + "message": "OpenAI Responses emitted response.completed before response.created", + "response": { + "body": "{\"type\":\"response.completed\",\"response\":{\"id\":\"resp_local\",\"object\":\"response\",\"created_at\":1,\"status\":\"completed\",\"model\":\"gpt-6.1-sol\",\"output\":[{\"type\":\"message\",\"id\":\"message_local\",\"role\":\"assistant\",\"status\":\"completed\",\"content\":[{\"type\":\"output_text\",\"text\":\"OK\",\"annotations\":[]}]}],\"usage\":{\"input_tokens\":1,\"output_tokens\":1,\"total_tokens\":2}}}" + } + } + }, + { + "id": "msg_16d02dff2001doMUdvgcTmGWJq", + "time": { + "created": 1792830267378 + }, + "type": "idle", + "outcome": "failed", + "error": { + "type": "provider.invalid-output", + "message": "OpenAI Responses emitted response.completed before response.created", + "response": { + "body": "{\"type\":\"response.completed\",\"response\":{\"id\":\"resp_local\",\"object\":\"response\",\"created_at\":1,\"status\":\"completed\",\"model\":\"gpt-6.1-sol\",\"output\":[{\"type\":\"message\",\"id\":\"message_local\",\"role\":\"assistant\",\"status\":\"completed\",\"content\":[{\"type\":\"output_text\",\"text\":\"OK\",\"annotations\":[]}]}],\"usage\":{\"input_tokens\":1,\"output_tokens\":1,\"total_tokens\":2}}}" + } + } + } + ] + }, + { + "hooked": true, + "host": "2.1.4", + "pid": 46942, + "upgrades": 1, + "http": 0, + "frames": 1, + "waitError": null, + "native": [ + { + "id": "msg_16d02e176001YAScqDlzFsu6vi", + "time": { + "created": 1792830267886 + }, + "text": "Reply OK only.", + "type": "user" + }, + { + "id": "msg_16d02e1fa001c4rxGkFY46MGQl", + "time": { + "created": 1792830267902, + "completed": 1792830267907 + }, + "type": "assistant", + "agent": "build", + "model": { + "id": "gpt-6.1-sol", + "providerID": "flow-transport", + "variant": "default" + }, + "content": [], + "finish": "error", + "error": { + "type": "provider.invalid-output", + "message": "OpenAI Responses emitted response.completed before response.created", + "response": { + "body": "{\"type\":\"response.completed\",\"response\":{\"id\":\"resp_local\",\"object\":\"response\",\"created_at\":1,\"status\":\"completed\",\"model\":\"gpt-6.1-sol\",\"output\":[{\"type\":\"message\",\"id\":\"message_local\",\"role\":\"assistant\",\"status\":\"completed\",\"content\":[{\"type\":\"output_text\",\"text\":\"OK\",\"annotations\":[]}]}],\"usage\":{\"input_tokens\":1,\"output_tokens\":1,\"total_tokens\":2}}}" + } + } + }, + { + "id": "msg_16d02e204001OJ11Ll2yfPfPen", + "time": { + "created": 1792830267908 + }, + "type": "idle", + "outcome": "failed", + "error": { + "type": "provider.invalid-output", + "message": "OpenAI Responses emitted response.completed before response.created", + "response": { + "body": "{\"type\":\"response.completed\",\"response\":{\"id\":\"resp_local\",\"object\":\"response\",\"created_at\":1,\"status\":\"completed\",\"model\":\"gpt-6.1-sol\",\"output\":[{\"type\":\"message\",\"id\":\"message_local\",\"role\":\"assistant\",\"status\":\"completed\",\"content\":[{\"type\":\"output_text\",\"text\":\"OK\",\"annotations\":[]}]}],\"usage\":{\"input_tokens\":1,\"output_tokens\":1,\"total_tokens\":2}}}" + } + } + } + ] + } + ], + "pass": false +} \ No newline at end of file diff --git a/.agents/plans/18-opencode-v2/transport/source-seal.log b/.agents/plans/18-opencode-v2/transport/source-seal.log new file mode 100644 index 00000000..8768a34d --- /dev/null +++ b/.agents/plans/18-opencode-v2/transport/source-seal.log @@ -0,0 +1,4 @@ +[ + "evals/http-transport-plugin.ts", + "src/platform/opencode/http-transport.ts" +] diff --git a/.agents/plans/18-opencode-v2/transport/verification.json b/.agents/plans/18-opencode-v2/transport/verification.json new file mode 100644 index 00000000..0e3837b3 --- /dev/null +++ b/.agents/plans/18-opencode-v2/transport/verification.json @@ -0,0 +1,179 @@ +{ + "results": [ + { + "mode": "bare", + "hooked": false, + "host": "2.1.4", + "pid": 50316, + "upgrades": 1, + "http": 0, + "frames": 1, + "waitError": null, + "native": [ + { + "id": "msg_16d051c5c001KcwIw3jRTahR63", + "time": { + "created": 1792830414042 + }, + "text": "Reply OK only.", + "type": "user" + }, + { + "id": "msg_16d051cdf001A4f25mPsi8Eld1", + "time": { + "created": 1792830414050, + "completed": 1792830414056 + }, + "type": "assistant", + "agent": "build", + "model": { + "id": "gpt-6.1-sol", + "providerID": "flow-transport", + "variant": "force-ws" + }, + "content": [], + "finish": "error", + "error": { + "type": "provider.invalid-output", + "message": "OpenAI Responses emitted response.completed before response.created", + "response": { + "body": "{\"type\":\"response.completed\",\"response\":{\"id\":\"resp_local\",\"object\":\"response\",\"created_at\":1,\"status\":\"completed\",\"model\":\"gpt-6.1-sol\",\"output\":[{\"type\":\"message\",\"id\":\"message_local\",\"role\":\"assistant\",\"status\":\"completed\",\"content\":[{\"type\":\"output_text\",\"text\":\"OK\",\"annotations\":[]}]}],\"usage\":{\"input_tokens\":1,\"output_tokens\":1,\"total_tokens\":2}}}" + } + } + }, + { + "id": "msg_16d051ce90019Aw7nxaKzo6RO2", + "time": { + "created": 1792830414057 + }, + "type": "idle", + "outcome": "failed", + "error": { + "type": "provider.invalid-output", + "message": "OpenAI Responses emitted response.completed before response.created", + "response": { + "body": "{\"type\":\"response.completed\",\"response\":{\"id\":\"resp_local\",\"object\":\"response\",\"created_at\":1,\"status\":\"completed\",\"model\":\"gpt-6.1-sol\",\"output\":[{\"type\":\"message\",\"id\":\"message_local\",\"role\":\"assistant\",\"status\":\"completed\",\"content\":[{\"type\":\"output_text\",\"text\":\"OK\",\"annotations\":[]}]}],\"usage\":{\"input_tokens\":1,\"output_tokens\":1,\"total_tokens\":2}}}" + } + } + } + ] + }, + { + "mode": "hooked", + "hooked": true, + "host": "2.1.4", + "pid": 50318, + "upgrades": 1, + "http": 0, + "frames": 1, + "waitError": null, + "native": [ + { + "id": "msg_16d051e76001sISZHxKLQUYsEy", + "time": { + "created": 1792830414586 + }, + "text": "Reply OK only.", + "type": "user" + }, + { + "id": "msg_16d051f00001F9ywkeEU4upcXT", + "time": { + "created": 1792830414595, + "completed": 1792830414600 + }, + "type": "assistant", + "agent": "build", + "model": { + "id": "gpt-6.1-sol", + "providerID": "flow-transport", + "variant": "force-ws" + }, + "content": [], + "finish": "error", + "error": { + "type": "provider.invalid-output", + "message": "OpenAI Responses emitted response.completed before response.created", + "response": { + "body": "{\"type\":\"response.completed\",\"response\":{\"id\":\"resp_local\",\"object\":\"response\",\"created_at\":1,\"status\":\"completed\",\"model\":\"gpt-6.1-sol\",\"output\":[{\"type\":\"message\",\"id\":\"message_local\",\"role\":\"assistant\",\"status\":\"completed\",\"content\":[{\"type\":\"output_text\",\"text\":\"OK\",\"annotations\":[]}]}],\"usage\":{\"input_tokens\":1,\"output_tokens\":1,\"total_tokens\":2}}}" + } + } + }, + { + "id": "msg_16d051f09001tj8REbrSZThS68", + "time": { + "created": 1792830414601 + }, + "type": "idle", + "outcome": "failed", + "error": { + "type": "provider.invalid-output", + "message": "OpenAI Responses emitted response.completed before response.created", + "response": { + "body": "{\"type\":\"response.completed\",\"response\":{\"id\":\"resp_local\",\"object\":\"response\",\"created_at\":1,\"status\":\"completed\",\"model\":\"gpt-6.1-sol\",\"output\":[{\"type\":\"message\",\"id\":\"message_local\",\"role\":\"assistant\",\"status\":\"completed\",\"content\":[{\"type\":\"output_text\",\"text\":\"OK\",\"annotations\":[]}]}],\"usage\":{\"input_tokens\":1,\"output_tokens\":1,\"total_tokens\":2}}}" + } + } + } + ] + }, + { + "mode": "forced", + "hooked": true, + "host": "2.1.4", + "pid": 50327, + "upgrades": 0, + "http": 1, + "frames": 0, + "waitError": null, + "native": [ + { + "id": "msg_16d052076001IPKnXHRMTXFTzz", + "time": { + "created": 1792830415260 + }, + "text": "Reply OK only.", + "type": "user" + }, + { + "id": "msg_16d0521a1001KyGGHNfzyRKz3d", + "time": { + "created": 1792830415269, + "streamed": 1792830415473, + "completed": 1792830415474 + }, + "type": "assistant", + "agent": "build", + "model": { + "id": "gpt-6.1-sol", + "providerID": "flow-transport", + "variant": "force-ws" + }, + "content": [], + "finish": "stop", + "native": { + "responseId": "resp_local" + }, + "cost": 0, + "tokens": { + "input": 1, + "output": 1, + "reasoning": 0, + "cache": { + "read": 0, + "write": 0 + } + } + }, + { + "id": "msg_16d052273002Ox4EuVO75h4w04", + "time": { + "created": 1792830415475 + }, + "type": "idle", + "outcome": "succeeded" + } + ] + } + ], + "pass": true +} \ No newline at end of file diff --git a/.agents/plans/18-opencode-v2/validation.json b/.agents/plans/18-opencode-v2/validation.json new file mode 100644 index 00000000..5ff09bbe --- /dev/null +++ b/.agents/plans/18-opencode-v2/validation.json @@ -0,0 +1,19 @@ +{ + "checkedAt": "2026-10-24T06:56:38.981257+00:00", + "scope": ".agents/plans/18-opencode-v2", + "checks": { + "localMarkdownLinks": "PASS", + "markdownWhitespaceAndPunctuation": "PASS", + "sixOrderedPhases": "PASS", + "decisionLogColumns": "PASS", + "sixRegistryPackageIdentities": "PASS", + "cliExecutableMetadata": "PASS" + }, + "markdownFiles": 3, + "decisionRows": 11, + "limitations": [ + "External link content was researched separately.", + "No plugin runtime or implementation tests ran.", + "Independent review returned issues. Parent corrections are recorded in review.md; no second independent verdict is claimed." + ] +} diff --git a/.agents/plans/18-opencode-v2/wiki/inventory.json b/.agents/plans/18-opencode-v2/wiki/inventory.json new file mode 100644 index 00000000..62afd21c --- /dev/null +++ b/.agents/plans/18-opencode-v2/wiki/inventory.json @@ -0,0 +1,846 @@ +{ + "baseSHA": "de4be508451c25564fc4bfb4911fa056d10ce3f3", + "pageCount": 45, + "canonicalAuthority": "Repository README and docs. URLs have local target verification only until post-release public readback.", + "pages": [ + { + "oldPage": "Home.md", + "wikiURL": "https://github.com/ddv1982/flow-opencode/wiki/Home", + "oldTitle": "Flow plugin for OpenCode", + "oldSha256": "448f35e136fd68e291dd71476a559424f6555b4fb7f4e27a815759a68a756859", + "oldLines": 46, + "currentCanonicalTargets": [ + "https://github.com/ddv1982/flow-opencode/blob/main/README.md#install", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/index.md", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/quickstart.md" + ], + "editReason": "Replace seven-tool and removed source-layout overview with v2-only entry points, install-once picker guidance, upgrade closure, and canonical docs ownership. Remove video and caption links whose repository targets are absent.", + "sourceEvidence": [], + "updatedSha256": "12dfffd340a1fee2268048fff341ca20dd7d1dd63678d7a23c4f89acd97484f2", + "newLines": 17, + "existingPageURLPreserved": true, + "readBeforeEdit": true + }, + { + "oldPage": "_Sidebar.md", + "wikiURL": null, + "oldTitle": "[Flow plugin for OpenCode](overview)", + "oldSha256": "77cb839610ba0e17391120d1018174c8da57bce68368da0d814467d68a2730e8", + "oldLines": 82, + "currentCanonicalTargets": [ + "https://github.com/ddv1982/flow-opencode/blob/main/docs/index.md" + ], + "editReason": "Flatten navigation into a few current groups. Keep every existing page reachable and relabel retired installer pages around bundled guidance and native package installation.", + "sourceEvidence": [], + "updatedSha256": "120b921f37967069baf2a13d4939f020a9681dc0244649be298e393300c58e0f", + "newLines": 59, + "existingPageURLPreserved": true, + "readBeforeEdit": true, + "publicReadbackSurface": "Rendered sidebar on the published wiki. This is a navigation file, not a separate content page." + }, + { + "oldPage": "api--flow-tools.md", + "wikiURL": "https://github.com/ddv1982/flow-opencode/wiki/api--flow-tools", + "oldTitle": "Flow tools", + "oldSha256": "e581922c36c95b72913a346b3d84554f063ed334c8dd94b7a3bf92387bc4c964", + "oldLines": 40, + "currentCanonicalTargets": [ + "https://github.com/ddv1982/flow-opencode/blob/main/docs/maintainer-contract.md#tools", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/validation-and-review.md" + ], + "editReason": "Replace seven old handlers and JSON-string adapter claims with eleven native tools, nested requests, current identity checks, and read-only exact completion replay.", + "sourceEvidence": [ + "src/platform/opencode/tools.ts" + ], + "updatedSha256": "6cf85bfc76fe4da7d56c60f73dce6285748aa43d73836e88683173425de9c5c0", + "newLines": 19, + "existingPageURLPreserved": true, + "readBeforeEdit": true + }, + { + "oldPage": "api--open-code-commands.md", + "wikiURL": "https://github.com/ddv1982/flow-opencode/wiki/api--open-code-commands", + "oldTitle": "OpenCode commands", + "oldSha256": "080fb2637ffb96b3f13b0fea9306b92b69753565f8224e4e7baf2a217d8b336d", + "oldLines": 32, + "currentCanonicalTargets": [ + "https://github.com/ddv1982/flow-opencode/blob/main/README.md#commands", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/adr/0018-opencode-v2-only.md", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/maintainer-contract.md#commands" + ], + "editReason": "Retain five lifecycle commands while adding picker navigation and native coding-model distinction. Remove V1 preflight and sync-warning instructions.", + "sourceEvidence": [], + "updatedSha256": "c68d5affdbe5fe5f9470bf44e0809cbbea75aff43cbcfdaa7cfa17692ca1bf0d", + "newLines": 16, + "existingPageURLPreserved": true, + "readBeforeEdit": true + }, + { + "oldPage": "api.md", + "wikiURL": "https://github.com/ddv1982/flow-opencode/wiki/api", + "oldTitle": "API", + "oldSha256": "fa7e708df6c3dcc2578fdde514542084b9683548c2568782f310bc53b2a1d37f", + "oldLines": 17, + "currentCanonicalTargets": [ + "https://github.com/ddv1982/flow-opencode/blob/main/docs/maintainer-contract.md#opencode-surface" + ], + "editReason": "Correct obsolete seven-tool and npm-binary inventory. Distinguish eleven tools, five lifecycle commands, four guides, three roles, and the extra terminal picker.", + "sourceEvidence": [], + "updatedSha256": "a59e225907d770ed6d2600bd611044fc6e2a603cce272d9dc5fb6315c9f495c1", + "newLines": 9, + "existingPageURLPreserved": true, + "readBeforeEdit": true + }, + { + "oldPage": "background--skills-first-adr.md", + "wikiURL": "https://github.com/ddv1982/flow-opencode/wiki/background--skills-first-adr", + "oldTitle": "Skills-first ADR", + "oldSha256": "8bd84ff8c007e9372300f5183d430ab10c23e1ac01043ea94f2f5b385338f8b9", + "oldLines": 29, + "currentCanonicalTargets": [ + "https://github.com/ddv1982/flow-opencode/blob/main/docs/adr/0001-skills-first-flow-architecture.md", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/adr/0005-flow-v6-session-v5-simplicity-first.md", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/adr/0018-opencode-v2-only.md", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/maintainer-contract.md" + ], + "editReason": "Preserve ADR 0001 date and enduring guidance/runtime split. Label seven-tool and managed-skill details historical and link superseding current decisions.", + "sourceEvidence": [], + "updatedSha256": "432661803b704445369ea044db22e35b83f0b342db0774b870022ef5e44cfa18", + "newLines": 9, + "existingPageURLPreserved": true, + "readBeforeEdit": true + }, + { + "oldPage": "background.md", + "wikiURL": "https://github.com/ddv1982/flow-opencode/wiki/background", + "oldTitle": "Background", + "oldSha256": "672839a3f246046d5add84f8b99d3de4d111017bfccacc7aef868afc253bc9d4", + "oldLines": 15, + "currentCanonicalTargets": [ + "https://github.com/ddv1982/flow-opencode/blob/main/docs/adr/0001-skills-first-flow-architecture.md", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/adr/0005-flow-v6-session-v5-simplicity-first.md", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/adr/0018-opencode-v2-only.md", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/index.md" + ], + "editReason": "Preserve the June skills-first origin while linking superseding Session v5 and native v2 decisions. Separate historical qualification from active runtime support.", + "sourceEvidence": [], + "updatedSha256": "8da8b1dae2c8546c93a19064b90d605b3540c6232bf9dd7c5f280a791fddc7fb", + "newLines": 9, + "existingPageURLPreserved": true, + "readBeforeEdit": true + }, + { + "oldPage": "by-the-numbers.md", + "wikiURL": "https://github.com/ddv1982/flow-opencode/wiki/by-the-numbers", + "oldTitle": "By the numbers", + "oldSha256": "e2576380d3026c82aca430f40562f86112a8288739f9006c1953cbba1826ab31", + "oldLines": 77, + "currentCanonicalTargets": [ + "https://github.com/ddv1982/flow-opencode/blob/main/docs/development.md#source-layout", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/maintainer-contract.md#opencode-surface" + ], + "editReason": "Keep useful July source, test, line, commit, and bot-attribution numbers explicitly labeled as the original historical snapshot. Remove stale current rankings and link current inventories.", + "sourceEvidence": [], + "updatedSha256": "ec8792fe88e7862bf6474321fb901b1c0bc09bfd5564fff46e06fefe628618b4", + "newLines": 17, + "existingPageURLPreserved": true, + "readBeforeEdit": true + }, + { + "oldPage": "cleanup-opportunities.md", + "wikiURL": "https://github.com/ddv1982/flow-opencode/wiki/cleanup-opportunities", + "oldTitle": "Cleanup opportunities", + "oldSha256": "f58528a7b3968bdb7c90b8c3dea9a93dfffbbd5ee0a467867b3254d60c1feb67", + "oldLines": 29, + "currentCanonicalTargets": [ + "https://github.com/ddv1982/flow-opencode/blob/main/docs/adr/0005-flow-v6-session-v5-simplicity-first.md#guardrail", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/development.md#bumping-the-pinned-host", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/development.md#change-discipline" + ], + "editReason": "Label old v4 hotspots and no-TODO snapshot historical instead of current backlog. Replace obsolete extraction suggestions with current canonical simplification and dependency discipline.", + "sourceEvidence": [], + "updatedSha256": "31477e2505c641e4831825ddbbe86d415661f3fc19cdc04ca62fdfc6ae89d2a0", + "newLines": 9, + "existingPageURLPreserved": true, + "readBeforeEdit": true + }, + { + "oldPage": "deployment.md", + "wikiURL": "https://github.com/ddv1982/flow-opencode/wiki/deployment", + "oldTitle": "Deployment", + "oldSha256": "f63c2ca437ef9a0ca70027958cf52a47698f571c4734fbed00a17e4050b47ae1", + "oldLines": 51, + "currentCanonicalTargets": [ + "https://github.com/ddv1982/flow-opencode/blob/main/README.md#install", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/development.md#release", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/release-qualification.md" + ], + "editReason": "Replace obsolete release diagram and multi-Node CI claims with frozen exact-artifact qualification, v2 two-provider profile, existing publish identity gates, and partial-publication recovery.", + "sourceEvidence": [], + "updatedSha256": "02f7a3892db566a4574c2e7ba1fd7be58f6d2dac6b673d8ceb3d4f34231e39cb", + "newLines": 9, + "existingPageURLPreserved": true, + "readBeforeEdit": true + }, + { + "oldPage": "features--execution-and-completion.md", + "wikiURL": "https://github.com/ddv1982/flow-opencode/wiki/features--execution-and-completion", + "oldTitle": "Execution and completion", + "oldSha256": "bd88719487633ec6d02e8b6cdae5dd97003569934ae35c3cdb03fcbecb150608", + "oldLines": 67, + "currentCanonicalTargets": [ + "https://github.com/ddv1982/flow-opencode/blob/main/docs/guarantees.md#assurance-at-close", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/maintainer-contract.md#session-v5", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/validation-and-review.md" + ], + "editReason": "Remove manager-proxy verdicts and two-pass final review. Explain current one-run/one-review completion, fresh retries, inspection scope, and assurance limits.", + "sourceEvidence": [], + "updatedSha256": "78a1348558b03210cb585341cc20ce559db2ff2c681a73c1b6d28a602ceb5761", + "newLines": 9, + "existingPageURLPreserved": true, + "readBeforeEdit": true + }, + { + "oldPage": "features--flow-loop.md", + "wikiURL": "https://github.com/ddv1982/flow-opencode/wiki/features--flow-loop", + "oldTitle": "Flow loop", + "oldSha256": "02ea94c88d98507d8c8c2a8fc6e5fd88220678aba9d30cf2fd9cda654577d89b", + "oldLines": 71, + "currentCanonicalTargets": [ + "https://github.com/ddv1982/flow-opencode/blob/main/docs/automatic-continuation.md", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/quickstart.md", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/troubleshooting.md" + ], + "editReason": "Replace legacy completion-payload loop with reviewer-owned submission, explicit goal authority, current close delivery, and process-local continuation recovery.", + "sourceEvidence": [], + "updatedSha256": "dc965658f7313b996941dccdeea913c68e7c83dcb8f85993292ecb78d8ca7d57", + "newLines": 9, + "existingPageURLPreserved": true, + "readBeforeEdit": true + }, + { + "oldPage": "features--managed-skills.md", + "wikiURL": "https://github.com/ddv1982/flow-opencode/wiki/features--managed-skills", + "oldTitle": "Managed skills", + "oldSha256": "bb62cf779766edf0cd91c64f4cbeab040f9dce2b33af9dde15779fbe2d0ddc03", + "oldLines": 57, + "currentCanonicalTargets": [ + "https://github.com/ddv1982/flow-opencode/blob/main/README.md#install", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/adr/0005-flow-v6-session-v5-simplicity-first.md", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/maintainer-contract.md#guides" + ], + "editReason": "Preserve the URL but replace marker, backup, startup sync, doctor, and uninstall instructions with four embedded guides and native package installation.", + "sourceEvidence": [ + "src/guidance/catalog.ts" + ], + "updatedSha256": "cb63e876113d57fbed0e2a27c9f035615d5b5546f4cf77839c3f033ad94e3a76", + "newLines": 7, + "existingPageURLPreserved": true, + "readBeforeEdit": true + }, + { + "oldPage": "features--parallel-orchestration.md", + "wikiURL": "https://github.com/ddv1982/flow-opencode/wiki/features--parallel-orchestration", + "oldTitle": "Parallel orchestration", + "oldSha256": "1c3196ec00998397d13f8e64b79f842c62dc526e1fab37fdeb6c94a7f061eeb7", + "oldLines": 53, + "currentCanonicalTargets": [ + "https://github.com/ddv1982/flow-opencode/blob/main/docs/adr/0006-bounded-intra-feature-waves.md", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/development.md#model-driven-wave-evidence", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/maintainer-contract.md#bounded-worker-waves" + ], + "editReason": "Replace retired specialized workers and unrestricted fan-out with one bounded flow-worker role, one active feature, audited slice boundaries, and manager-owned convergence.", + "sourceEvidence": [], + "updatedSha256": "5a1842d79e2e4100e04bfb02db831f8cda81eb32f47d946ebf61d20717547182", + "newLines": 9, + "existingPageURLPreserved": true, + "readBeforeEdit": true + }, + { + "oldPage": "features--planning-and-approval.md", + "wikiURL": "https://github.com/ddv1982/flow-opencode/wiki/features--planning-and-approval", + "oldTitle": "Planning and approval", + "oldSha256": "f23344fc671395a9e772fa388d7f3fab2bb4c373057f3e59065a3250a4224578", + "oldLines": 54, + "currentCanonicalTargets": [ + "https://github.com/ddv1982/flow-opencode/blob/main/docs/maintainer-contract.md#session-v5", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/validation-and-review.md" + ], + "editReason": "Replace legacy plan-policy schemas with immutable feature DAG, unified evidence, inspection intent, optional planner advice, and constrained prerequisite amendments.", + "sourceEvidence": [], + "updatedSha256": "475e4ee4ccb9f51fe5c6746fb989347bf2b74a722f03ddf4e1bfded3437afefe", + "newLines": 9, + "existingPageURLPreserved": true, + "readBeforeEdit": true + }, + { + "oldPage": "features--review-and-validation.md", + "wikiURL": "https://github.com/ddv1982/flow-opencode/wiki/features--review-and-validation", + "oldTitle": "Review and validation", + "oldSha256": "7ec084c7708157ef30ae40145e63f7d0a3f56e9423dfcc87717a662ff756fa59", + "oldLines": 54, + "currentCanonicalTargets": [ + "https://github.com/ddv1982/flow-opencode/blob/main/docs/guarantees.md", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/validation-and-review.md" + ], + "editReason": "Remove caller-authored success and old review payloads. Explain native host observations, exact broad gate, paged review evidence, and source-binding recovery.", + "sourceEvidence": [], + "updatedSha256": "05168dc2bc9396ac131fa909b1c5f8453c11c51084a95f1f9f08933f9f8f8e2d", + "newLines": 9, + "existingPageURLPreserved": true, + "readBeforeEdit": true + }, + { + "oldPage": "features.md", + "wikiURL": "https://github.com/ddv1982/flow-opencode/wiki/features", + "oldTitle": "Features", + "oldSha256": "260da776a8753b6631eba9d844e54de1234c070b90409032f1f5e46888c3649d", + "oldLines": 22, + "currentCanonicalTargets": [ + "https://github.com/ddv1982/flow-opencode/blob/main/README.md#how-flow-works", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/guarantees.md" + ], + "editReason": "Replace obsolete feature source trees with capability navigation. Distinguish current bundled guidance and bounded waves from retired installer and worker machinery.", + "sourceEvidence": [], + "updatedSha256": "0419fde99a70000de398d2615eb89ca709478b0f7349a0786846eaf2b6483d06", + "newLines": 12, + "existingPageURLPreserved": true, + "readBeforeEdit": true + }, + { + "oldPage": "fun-facts.md", + "wikiURL": "https://github.com/ddv1982/flow-opencode/wiki/fun-facts", + "oldTitle": "Fun facts", + "oldSha256": "6e5966fd70346be82a933a4078289d558d93d96f53c97d6104d03eacbfe8e67b", + "oldLines": 21, + "currentCanonicalTargets": [ + "https://github.com/ddv1982/flow-opencode/blob/main/docs/adr/0001-skills-first-flow-architecture.md", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/adr/0005-flow-v6-session-v5-simplicity-first.md", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/development.md#bumping-the-pinned-host" + ], + "editReason": "Preserve verified same-day plugin/reviewer origins and label old longest-file and TODO scans historical. Replace outdated host-validator rationale with current host pin evidence.", + "sourceEvidence": [], + "updatedSha256": "dba740ea34e7df1c8015fcaa50992abadecfac43d6ac5c727d3a4c55f9533a4c", + "newLines": 9, + "existingPageURLPreserved": true, + "readBeforeEdit": true + }, + { + "oldPage": "how-to-contribute--debugging.md", + "wikiURL": "https://github.com/ddv1982/flow-opencode/wiki/how-to-contribute--debugging", + "oldTitle": "Debugging", + "oldSha256": "263041732a94513f4c285546a99bf81f1c1767e9b65d54e42914f9cf5a5d6c63", + "oldLines": 34, + "currentCanonicalTargets": [ + "https://github.com/ddv1982/flow-opencode/blob/main/README.md#install", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/troubleshooting.md", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/troubleshooting.md#flow-reports-that-the-project-lock-is-busy" + ], + "editReason": "Remove 4.2.0 doctor and generated-instruction repair. Link native loading recovery and current validation, source-stale review, close-collision, and lock procedures.", + "sourceEvidence": [], + "updatedSha256": "06b5316899cc598d4eaf70e25ec807ef07a9b3bba0e57a991adf5c881fc6c1d4", + "newLines": 9, + "existingPageURLPreserved": true, + "readBeforeEdit": true + }, + { + "oldPage": "how-to-contribute--development-workflow.md", + "wikiURL": "https://github.com/ddv1982/flow-opencode/wiki/how-to-contribute--development-workflow", + "oldTitle": "Development workflow", + "oldSha256": "52992adcc1c4aa9863503550bdbac176c010e61eb7ed2cf96dc2739194a6ffeb", + "oldLines": 37, + "currentCanonicalTargets": [ + "https://github.com/ddv1982/flow-opencode/blob/main/docs/development.md#change-discipline", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/development.md#local-setup", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/development.md#release", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/development.md#source-layout" + ], + "editReason": "Replace obsolete ownership table, old CI matrix, and retired commit skill with pinned setup, inward source ownership, and canonical change discipline.", + "sourceEvidence": [], + "updatedSha256": "f3013ec87da666e3e5b8e70b09bc03ef70661e0a6ff64eb2d7fb950c28db605b", + "newLines": 14, + "existingPageURLPreserved": true, + "readBeforeEdit": true + }, + { + "oldPage": "how-to-contribute--patterns-and-conventions.md", + "wikiURL": "https://github.com/ddv1982/flow-opencode/wiki/how-to-contribute--patterns-and-conventions", + "oldTitle": "Patterns and conventions", + "oldSha256": "96317d95219c492a09f0f4b7f5548c9664af79cdeff0cfee14cc404c6dd8fa82", + "oldLines": 29, + "currentCanonicalTargets": [ + "https://github.com/ddv1982/flow-opencode/blob/main/docs/adr/0005-flow-v6-session-v5-simplicity-first.md", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/architecture/allowed-cross-layer-dependencies.md", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/development.md#change-discipline" + ], + "editReason": "Replace obsolete API and generated-projection details with inward layers, one run aggregate, causal operations, host evidence, and reviewer-owned results.", + "sourceEvidence": [], + "updatedSha256": "6fd2858dfbf0338ddc4667a83ec1a3ddfb946068fb0d4374271f96c74fac0514", + "newLines": 9, + "existingPageURLPreserved": true, + "readBeforeEdit": true + }, + { + "oldPage": "how-to-contribute--testing.md", + "wikiURL": "https://github.com/ddv1982/flow-opencode/wiki/how-to-contribute--testing", + "oldTitle": "Testing", + "oldSha256": "f7bd55017889625a36ce878151f672ce6eb74bddd3901593e5dd2f99413fe723", + "oldLines": 30, + "currentCanonicalTargets": [ + "https://github.com/ddv1982/flow-opencode/blob/main/docs/development.md#local-setup", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/development.md#model-driven-auto-continuation-evidence", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/development.md#model-driven-wave-evidence", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/release-qualification.md" + ], + "editReason": "Replace old distribution/CLI tests and host matrix with actual current suites, packed real-host policy, current platform coverage, and manual model evidence requirements.", + "sourceEvidence": [ + ".github/workflows/ci.yml", + "package.json" + ], + "updatedSha256": "85fcfd89570eba050cac38d04404bc4098258f51b3f06b0576ace8b7dbdf55f8", + "newLines": 16, + "existingPageURLPreserved": true, + "readBeforeEdit": true + }, + { + "oldPage": "how-to-contribute--tooling.md", + "wikiURL": "https://github.com/ddv1982/flow-opencode/wiki/how-to-contribute--tooling", + "oldTitle": "Tooling", + "oldSha256": "ad3ee00e86b58b21d07c1a2e53093c9cdf648748facaffb5840e15dac425532c", + "oldLines": 39, + "currentCanonicalTargets": [ + "https://github.com/ddv1982/flow-opencode/blob/main/README.md#install", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/development.md", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/release-qualification.md#running-it" + ], + "editReason": "Remove copied script definitions and retired doctor/sync/uninstall CLI. Retain useful command purposes and link maintained package/toolchain and qualification instructions.", + "sourceEvidence": [ + "package.json" + ], + "updatedSha256": "d98a268d49c75d5e75e19d5c7a6e09ee9422b68c24f3cef3c977bd833662e9a5", + "newLines": 17, + "existingPageURLPreserved": true, + "readBeforeEdit": true + }, + { + "oldPage": "how-to-contribute.md", + "wikiURL": "https://github.com/ddv1982/flow-opencode/wiki/how-to-contribute", + "oldTitle": "How to contribute", + "oldSha256": "8be335be986eab157135f62e10f2ecfca75ea89427ebf3eade826c05cc3afb5b", + "oldLines": 26, + "currentCanonicalTargets": [ + "https://github.com/ddv1982/flow-opencode/blob/main/docs/architecture/allowed-cross-layer-dependencies.md", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/development.md", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/maintainer-contract.md" + ], + "editReason": "Remove retired distribution and commit-skill ownership. Link current development and contracts, preserve focused-then-broad verification, and separate model evidence from deterministic tests.", + "sourceEvidence": [], + "updatedSha256": "af69fcc7766b90ae605cb08d435f9729c9793d6cd62beb5a0648795399597d23", + "newLines": 13, + "existingPageURLPreserved": true, + "readBeforeEdit": true + }, + { + "oldPage": "lore.md", + "wikiURL": "https://github.com/ddv1982/flow-opencode/wiki/lore", + "oldTitle": "Lore", + "oldSha256": "f5009b04920f6323729581febca6f308f439b610175c91f99f55523255ee3e77", + "oldLines": 59, + "currentCanonicalTargets": [ + "https://github.com/ddv1982/flow-opencode/blob/main/docs/adr/0001-skills-first-flow-architecture.md", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/adr/0005-flow-v6-session-v5-simplicity-first.md", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/adr/0018-opencode-v2-only.md", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/maintainer-contract.md" + ], + "editReason": "Preserve selected verified origin, reviewer, CI, persistence, v4 release, and simplification milestones with actual commit links. Remove stale operational paths and still-active payload claims.", + "sourceEvidence": [], + "updatedSha256": "3e2c49f0a18dd013e7b06e35b5caf070f966f08b42c59aec02a069ee7d716d3c", + "newLines": 15, + "existingPageURLPreserved": true, + "readBeforeEdit": true + }, + { + "oldPage": "overview--architecture.md", + "wikiURL": "https://github.com/ddv1982/flow-opencode/wiki/overview--architecture", + "oldTitle": "Architecture", + "oldSha256": "81e9e36b6364630191a7676372c09525d6260ac6a8ec46be1a107f194948e302", + "oldLines": 71, + "currentCanonicalTargets": [ + "https://github.com/ddv1982/flow-opencode/blob/main/docs/adr/0018-opencode-v2-only.md", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/architecture/allowed-cross-layer-dependencies.md", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/development.md#source-layout" + ], + "editReason": "Replace removed runtime/adapters/distribution graph with inward domain, application, infrastructure, and native v2 platform ownership. Drop uncurrent size claims.", + "sourceEvidence": [], + "updatedSha256": "0883e334bc8fad7da72f1ec371c341af7ba36ce0cffe6cc9cbdd2650e930b8ff", + "newLines": 9, + "existingPageURLPreserved": true, + "readBeforeEdit": true + }, + { + "oldPage": "overview--getting-started.md", + "wikiURL": "https://github.com/ddv1982/flow-opencode/wiki/overview--getting-started", + "oldTitle": "Getting started", + "oldSha256": "6b0f0fbab7d3a385042c30430c6eaee7691183bf1479a1cafc547635c66cb594", + "oldLines": 53, + "currentCanonicalTargets": [ + "https://github.com/ddv1982/flow-opencode/blob/main/README.md#install", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/development.md#local-setup", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/quickstart.md", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/troubleshooting.md#flow-commands-are-missing" + ], + "editReason": "Remove 4.2.0 install and retired skill sync. Link the exact canonical native setup, show install-once picker behavior, and require active-state closure before version changes.", + "sourceEvidence": [], + "updatedSha256": "383bbf356006198eee06bb7621b259fe44a1e54601a8b159855d59d8d061e78c", + "newLines": 9, + "existingPageURLPreserved": true, + "readBeforeEdit": true + }, + { + "oldPage": "overview--glossary.md", + "wikiURL": "https://github.com/ddv1982/flow-opencode/wiki/overview--glossary", + "oldTitle": "Glossary", + "oldSha256": "7354329bb2daf33e02ee504979cff7bbb86424323960d440bc4a13bff574a716", + "oldLines": 23, + "currentCanonicalTargets": [ + "https://github.com/ddv1982/flow-opencode/blob/main/docs/maintainer-contract.md", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/validation-and-review.md" + ], + "editReason": "Replace legacy targeted validation, final-review payload, generated instruction, and skill-sync vocabulary with Session v5 run, evidence, bundled guidance, and preference terms.", + "sourceEvidence": [], + "updatedSha256": "e42ec8793d34b3f9a4ba197f32be345d64a2c3b75a5f3bd4c5bc3a03179fd865", + "newLines": 18, + "existingPageURLPreserved": true, + "readBeforeEdit": true + }, + { + "oldPage": "overview.md", + "wikiURL": "https://github.com/ddv1982/flow-opencode/wiki/overview", + "oldTitle": "Flow plugin for OpenCode", + "oldSha256": "1c76b2e3edb6f036293dd90472291ffc20655120eccc9f73e049adeea499b8b7", + "oldLines": 42, + "currentCanonicalTargets": [ + "https://github.com/ddv1982/flow-opencode/blob/main/README.md", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/index.md" + ], + "editReason": "Replace duplicated generated v4 manual with a bounded product overview and explicit v2 compatibility limits. Remove absent video targets and obsolete source map.", + "sourceEvidence": [], + "updatedSha256": "27a14b0903fdba5a87de86a5f1bfa1e14b69c038fd85ef611441276ae013595c", + "newLines": 12, + "existingPageURLPreserved": true, + "readBeforeEdit": true + }, + { + "oldPage": "primitives--session-plan-feature.md", + "wikiURL": "https://github.com/ddv1982/flow-opencode/wiki/primitives--session-plan-feature", + "oldTitle": "Session, plan, and feature", + "oldSha256": "f083df613a67f3fb0016e9b0d75c1d4c69101497aeeaed81e621e9cc696a65ad", + "oldLines": 50, + "currentCanonicalTargets": [ + "https://github.com/ddv1982/flow-opencode/blob/main/docs/guarantees.md#terminal-recovery-and-inspection-outcomes", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/maintainer-contract.md#session-v5" + ], + "editReason": "Replace persisted version 2, mutable feature-status history, and timestamps with Session v5 features/runs, immutable baselines, derived progress, and terminal disposition.", + "sourceEvidence": [], + "updatedSha256": "5d334022a3820b125746eb928c24239ee3ef7f343ae08136f6cf17e3bafcc97b", + "newLines": 9, + "existingPageURLPreserved": true, + "readBeforeEdit": true + }, + { + "oldPage": "primitives--validation-and-review.md", + "wikiURL": "https://github.com/ddv1982/flow-opencode/wiki/primitives--validation-and-review", + "oldTitle": "Validation and review", + "oldSha256": "a8937f5d20782331245f4a2f241f584b310f71c8a6bddfdeb2fc37d9420777e0", + "oldLines": 50, + "currentCanonicalTargets": [ + "https://github.com/ddv1982/flow-opencode/blob/main/docs/adr/0014-one-evidence-record.md", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/adr/0017-inspection-gate-outcome.md", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/validation-and-review.md" + ], + "editReason": "Replace old validation summaries and manager-recorded feature/final reviews with host observations, unified evidence, typed pass/observe intent, and reviewer submission.", + "sourceEvidence": [], + "updatedSha256": "92bdf7dacd32a3e6890e13a87f6e15584ebd4f6b8237ca49065971fec72c4d05", + "newLines": 9, + "existingPageURLPreserved": true, + "readBeforeEdit": true + }, + { + "oldPage": "primitives.md", + "wikiURL": "https://github.com/ddv1982/flow-opencode/wiki/primitives", + "oldTitle": "Primitives", + "oldSha256": "3aeae81ea7c708de3b1a0221b9be2d83dadf4c02b4a67b00f77a7f2f999e1b6c", + "oldLines": 18, + "currentCanonicalTargets": [ + "https://github.com/ddv1982/flow-opencode/blob/main/docs/guarantees.md", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/maintainer-contract.md#session-v5" + ], + "editReason": "Replace legacy completion/final-review type list with the canonical Session v5 run aggregate and links to exact contract and guarantee tiers.", + "sourceEvidence": [], + "updatedSha256": "55da799da74005656e33d9b3d1203a4432381e7bd0bb22d8454811a7a86bc36c", + "newLines": 8, + "existingPageURLPreserved": true, + "readBeforeEdit": true + }, + { + "oldPage": "reference--configuration.md", + "wikiURL": "https://github.com/ddv1982/flow-opencode/wiki/reference--configuration", + "oldTitle": "Configuration", + "oldSha256": "0b81bed94cfcf4dc2b1d1af9a29ee354d7a3a52611493fb28f1c04a6adf842f0", + "oldLines": 43, + "currentCanonicalTargets": [ + "https://github.com/ddv1982/flow-opencode/blob/main/README.md#install", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/adr/0018-opencode-v2-only.md", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/maintainer-contract.md#opencode-surface", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/quickstart.md#select-flow-models" + ], + "editReason": "Remove V1 config injection and experimental compaction setup. Use plugins/object options and source-verified project/global/base reviewer precedence, reset, variant, steps, server storage, and the selected Typesafe key environment variable.", + "sourceEvidence": [ + "src/application/model-preferences.ts", + "src/application/model-preferences-schema.ts", + "src/platform/opencode/model-picker.ts", + "src/platform/opencode/model-preferences.ts", + "src/infrastructure/fs/model-preference-store.ts", + "src/config-shared.ts", + "src/tui.ts", + "src/platform/opencode/typesafe-key-env.ts" + ], + "updatedSha256": "e2cd0c2c90bf4a8c8a5496781f0ebac3abac90ee4e6fd7c5c880c167360b5dc2", + "newLines": 26, + "existingPageURLPreserved": true, + "readBeforeEdit": true + }, + { + "oldPage": "reference--data-models.md", + "wikiURL": "https://github.com/ddv1982/flow-opencode/wiki/reference--data-models", + "oldTitle": "Data models", + "oldSha256": "c007c1b6e04c5f2a38926ff9bf01fbaf932266adcb564ab2de9733b12e18e6de", + "oldLines": 44, + "currentCanonicalTargets": [ + "https://github.com/ddv1982/flow-opencode/blob/main/docs/maintainer-contract.md#session-v5", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/validation-and-review.md" + ], + "editReason": "Remove obsolete version 2 field tables and WorkerResult/FinalReview protocols. Explain Session v5 runs, unified evidence, derived status and delivery, and current schema locations.", + "sourceEvidence": [ + "src/domain/session.ts", + "src/application/schema.ts" + ], + "updatedSha256": "04c50dd28b935730f9d70b380e3c04735790a6b048f972f3340c198fce38325a", + "newLines": 9, + "existingPageURLPreserved": true, + "readBeforeEdit": true + }, + { + "oldPage": "reference--dependencies.md", + "wikiURL": "https://github.com/ddv1982/flow-opencode/wiki/reference--dependencies", + "oldTitle": "Dependencies", + "oldSha256": "2283558e9386b555ca30a71e0660244402b33ae3c81622e0b57d7915aa90efdf", + "oldLines": 36, + "currentCanonicalTargets": [ + "https://github.com/ddv1982/flow-opencode/blob/main/docs/adr/0018-opencode-v2-only.md", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/development.md#bumping-the-pinned-host" + ], + "editReason": "Replace OpenCode 1 peer range and stale dependency pins with tested v2 range, exact manifest ownership, current toolchain, and compatibility-check support limits.", + "sourceEvidence": [ + "package.json", + ".github/workflows/opencode-compatibility.yml" + ], + "updatedSha256": "e8b4ac98118ddb2c275f696c82665026f3e74242cb6546823e6bf80eca830526", + "newLines": 9, + "existingPageURLPreserved": true, + "readBeforeEdit": true + }, + { + "oldPage": "reference--source-map.md", + "wikiURL": "https://github.com/ddv1982/flow-opencode/wiki/reference--source-map", + "oldTitle": "Source map", + "oldSha256": "7b2b77586ed27c283a6e88238a496187ca80b489b4c8aa272da5b945ce04b132", + "oldLines": 41, + "currentCanonicalTargets": [ + "https://github.com/ddv1982/flow-opencode/blob/main/docs/architecture/allowed-cross-layer-dependencies.md", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/development.md#source-layout" + ], + "editReason": "Replace removed runtime/adapters/distribution/CLI paths with the actual current source census and preference ownership locations.", + "sourceEvidence": [ + "source-census.json" + ], + "updatedSha256": "9d6ac595ff746200f8ac3e237e7f10d66d4cf60571e3abe44d5053983dca888b", + "newLines": 20, + "existingPageURLPreserved": true, + "readBeforeEdit": true + }, + { + "oldPage": "reference.md", + "wikiURL": "https://github.com/ddv1982/flow-opencode/wiki/reference", + "oldTitle": "Reference", + "oldSha256": "711f99cc391adad5aa3258f0d56e0d187bac954ebd24bc9da4de01732d9aa93e", + "oldLines": 14, + "currentCanonicalTargets": [ + "https://github.com/ddv1982/flow-opencode/blob/main/docs/guarantees.md", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/index.md", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/maintainer-contract.md" + ], + "editReason": "Replace duplicated reference-manual framing with short lookup links and authoritative documentation ownership.", + "sourceEvidence": [], + "updatedSha256": "6f3caaa5ba667373b52e0b5bf203393e816d3cc0698da7296d1dea499a0dc4e7", + "newLines": 10, + "existingPageURLPreserved": true, + "readBeforeEdit": true + }, + { + "oldPage": "security.md", + "wikiURL": "https://github.com/ddv1982/flow-opencode/wiki/security", + "oldTitle": "Security", + "oldSha256": "1afb0810311908f2459d556cc20d1b460953abface5e25c2ecd2cdf89c27f46a", + "oldLines": 38, + "currentCanonicalTargets": [ + "https://github.com/ddv1982/flow-opencode/blob/main/README.md#recovery-advice-development-preview", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/guarantees.md#threat-model", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/maintainer-contract.md#persistence" + ], + "editReason": "Remove managed-skill marker guarantees and incorrect manager-only completion. Preserve filesystem and reviewer boundaries while stating host trust, unsandboxed manager, worker path limits, and optional external recovery advice.", + "sourceEvidence": [], + "updatedSha256": "315d90606179112d193cb40660b8dc37a560f00f45e5d58b2e9d157e26aff97e", + "newLines": 9, + "existingPageURLPreserved": true, + "readBeforeEdit": true + }, + { + "oldPage": "systems--cli-and-package.md", + "wikiURL": "https://github.com/ddv1982/flow-opencode/wiki/systems--cli-and-package", + "oldTitle": "CLI and package", + "oldSha256": "aad298f8009459b58fa37e0b9cf7acfd42334922e40242a572aa51ad5e2a3ff7", + "oldLines": 52, + "currentCanonicalTargets": [ + "https://github.com/ddv1982/flow-opencode/blob/main/README.md#install", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/adr/0018-opencode-v2-only.md", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/development.md", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/release-qualification.md" + ], + "editReason": "Retain the URL but remove bin and build:cli claims. Explain server and terminal exports, automatic picker load, one native install, and exact package identity through release.", + "sourceEvidence": [ + "package.json", + "src/index.ts", + "src/tui.ts" + ], + "updatedSha256": "3ce0128dce798af5e93d51949279f502e225f1663df00eeea41cb3401a3dee30", + "newLines": 9, + "existingPageURLPreserved": true, + "readBeforeEdit": true + }, + { + "oldPage": "systems--opencode-adapter.md", + "wikiURL": "https://github.com/ddv1982/flow-opencode/wiki/systems--opencode-adapter", + "oldTitle": "OpenCode adapter", + "oldSha256": "71029f9c48a7df48ecb26f8ca72e003653534ae128e09e8c5059196edae31ab4", + "oldLines": 52, + "currentCanonicalTargets": [ + "https://github.com/ddv1982/flow-opencode/blob/main/docs/adr/0018-opencode-v2-only.md", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/development.md#source-layout", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/maintainer-contract.md#opencode-surface" + ], + "editReason": "Replace V1 preflight/config/compaction hooks with native v2 transforms, exact request attribution, checked compaction, server preferences, and duplicate-instance failure.", + "sourceEvidence": [ + "src/platform/opencode/plugin-composition.ts", + "src/platform/opencode/tools.ts", + "src/tui.ts" + ], + "updatedSha256": "ae3355ef3ec9675068e3e1c0c07aaa1eeab2ccfe86e1e85aac10f76203c5c4cd", + "newLines": 9, + "existingPageURLPreserved": true, + "readBeforeEdit": true + }, + { + "oldPage": "systems--runtime-state-machine.md", + "wikiURL": "https://github.com/ddv1982/flow-opencode/wiki/systems--runtime-state-machine", + "oldTitle": "Runtime state machine", + "oldSha256": "45c6ef909b89cce3cde46830f0608dd865b8dc5d1eeaab04535eefb5d2e86c54", + "oldLines": 62, + "currentCanonicalTargets": [ + "https://github.com/ddv1982/flow-opencode/blob/main/docs/maintainer-contract.md#causality-and-idempotency", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/maintainer-contract.md#session-v5" + ], + "editReason": "Replace legacy API/transitions paths and version 2 lifecycle with domain/application boundaries, revision causality, exact replay, and derived status.", + "sourceEvidence": [], + "updatedSha256": "6251f7a71983e5db75f40c1d4e29b1b4dad5cb7f39d6be351783e4018acc437b", + "newLines": 9, + "existingPageURLPreserved": true, + "readBeforeEdit": true + }, + { + "oldPage": "systems--schema-and-json.md", + "wikiURL": "https://github.com/ddv1982/flow-opencode/wiki/systems--schema-and-json", + "oldTitle": "Schema and JSON", + "oldSha256": "adb80b6f33c322964abb85d9ddc694f13e7be625fcd094afd8351fe484b6e5e5", + "oldLines": 49, + "currentCanonicalTargets": [ + "https://github.com/ddv1982/flow-opencode/blob/main/docs/architecture/allowed-cross-layer-dependencies.md", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/maintainer-contract.md#session-v5", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/troubleshooting.md#session-state-is-unreadable" + ], + "editReason": "Replace obsolete schema locations and host validator coupling with domain values, application schemas, strict filesystem JSON, and v2 boundary JSON Schema.", + "sourceEvidence": [ + "src/application/schema.ts" + ], + "updatedSha256": "c4407449d138a5b36547f4427321112488dae36235c94e3931a60d381499c192", + "newLines": 9, + "existingPageURLPreserved": true, + "readBeforeEdit": true + }, + { + "oldPage": "systems--skill-distribution.md", + "wikiURL": "https://github.com/ddv1982/flow-opencode/wiki/systems--skill-distribution", + "oldTitle": "Skill distribution", + "oldSha256": "a6bdc5e463ff8c92a8b10aeda37f9827ef6a7a2e7b27deeb658cdb88c9883470", + "oldLines": 51, + "currentCanonicalTargets": [ + "https://github.com/ddv1982/flow-opencode/blob/main/README.md#install", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/maintainer-contract.md#distribution-and-release", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/troubleshooting.md#flow-commands-are-missing" + ], + "editReason": "Retain the URL as a current bundled-guidance explanation. Remove skill-root copying, marker manifests, setup-health sync, and retired maintenance CLI procedures.", + "sourceEvidence": [], + "updatedSha256": "678962124cb5be759e03a311916fac5d086cc799ad7754c4828a03353f8282d2", + "newLines": 9, + "existingPageURLPreserved": true, + "readBeforeEdit": true + }, + { + "oldPage": "systems--workspace-persistence.md", + "wikiURL": "https://github.com/ddv1982/flow-opencode/wiki/systems--workspace-persistence", + "oldTitle": "Workspace persistence", + "oldSha256": "daa6c607c42b70c65f573f52ee6d9dd70e780b7ba84419ac51f980177f8bddc9", + "oldLines": 54, + "currentCanonicalTargets": [ + "https://github.com/ddv1982/flow-opencode/blob/main/docs/maintainer-contract.md#persistence", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/troubleshooting.md#status-shows-a-closed-session-still-awaiting-archive-publication" + ], + "editReason": "Remove generated instructions and age-based stale-lock ownership. State canonical v5 persistence, absent-PID/token reclamation, no-overwrite archive, and exact close recovery.", + "sourceEvidence": [ + "src/infrastructure/fs/session-lock.ts", + "src/infrastructure/fs/workspace-paths.ts" + ], + "updatedSha256": "67dba175263baf929734367e08e998bb0c47f231d5e96bc1679de34601d4dadb", + "newLines": 9, + "existingPageURLPreserved": true, + "readBeforeEdit": true + }, + { + "oldPage": "systems.md", + "wikiURL": "https://github.com/ddv1982/flow-opencode/wiki/systems", + "oldTitle": "Systems", + "oldSha256": "9820082973c70ce31bf395966f9bde8e853dbfffff7b50a9d68859afb4387828", + "oldLines": 29, + "currentCanonicalTargets": [ + "https://github.com/ddv1982/flow-opencode/blob/main/docs/architecture/allowed-cross-layer-dependencies.md", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/development.md#source-layout" + ], + "editReason": "Replace removed directory taxonomy with current system navigation and canonical inward ownership. Retain historical distribution and CLI page URLs with current labels.", + "sourceEvidence": [], + "updatedSha256": "cde052e20cc7a7f47056425487850e052344a65717ce7fab3857c13182931f85", + "newLines": 12, + "existingPageURLPreserved": true, + "readBeforeEdit": true + } + ] +} diff --git a/.agents/plans/18-opencode-v2/wiki/linkverification.json b/.agents/plans/18-opencode-v2/wiki/linkverification.json new file mode 100644 index 00000000..c54447f5 --- /dev/null +++ b/.agents/plans/18-opencode-v2/wiki/linkverification.json @@ -0,0 +1,3448 @@ +{ + "baseSHA": "de4be508451c25564fc4bfb4911fa056d10ce3f3", + "pageCount": 45, + "checkedPages": [ + "Home.md", + "_Sidebar.md", + "api--flow-tools.md", + "api--open-code-commands.md", + "api.md", + "background--skills-first-adr.md", + "background.md", + "by-the-numbers.md", + "cleanup-opportunities.md", + "deployment.md", + "features--execution-and-completion.md", + "features--flow-loop.md", + "features--managed-skills.md", + "features--parallel-orchestration.md", + "features--planning-and-approval.md", + "features--review-and-validation.md", + "features.md", + "fun-facts.md", + "how-to-contribute--debugging.md", + "how-to-contribute--development-workflow.md", + "how-to-contribute--patterns-and-conventions.md", + "how-to-contribute--testing.md", + "how-to-contribute--tooling.md", + "how-to-contribute.md", + "lore.md", + "overview--architecture.md", + "overview--getting-started.md", + "overview--glossary.md", + "overview.md", + "primitives--session-plan-feature.md", + "primitives--validation-and-review.md", + "primitives.md", + "reference--configuration.md", + "reference--data-models.md", + "reference--dependencies.md", + "reference--source-map.md", + "reference.md", + "security.md", + "systems--cli-and-package.md", + "systems--opencode-adapter.md", + "systems--runtime-state-machine.md", + "systems--schema-and-json.md", + "systems--skill-distribution.md", + "systems--workspace-persistence.md", + "systems.md" + ], + "verificationScope": "Local file and anchor resolution only. No network requests.", + "status": "PASS", + "checkCount": 293, + "issues": [], + "checks": [ + { + "page": "Home.md", + "label": "installation instructions", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/README.md#install", + "kind": "local-canonical", + "target": "README.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": "install", + "anchorExists": true, + "status": "pass", + "targetSha256": "1407ad086e757bf76240327011e3738bc21e9fb31d98c83eeb46b5500c7ad1df" + }, + { + "page": "Home.md", + "label": "quickstart", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/quickstart.md", + "kind": "local-canonical", + "target": "docs/quickstart.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "cd1d6c96a0ae685214acb3ae18019d71a2f835d36e50fe800b1248dcbad579a2" + }, + { + "page": "Home.md", + "label": "repository documentation", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/index.md", + "kind": "local-canonical", + "target": "docs/index.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "a8674488ab75bc8aef157db9cd50e11cb2815f378e54034e769618f8aecc2488" + }, + { + "page": "Home.md", + "label": "Flow loop", + "url": "features--flow-loop", + "kind": "wiki", + "target": "features--flow-loop.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "dc965658f7313b996941dccdeea913c68e7c83dcb8f85993292ecb78d8ca7d57" + }, + { + "page": "Home.md", + "label": "Configuration and model preferences", + "url": "reference--configuration", + "kind": "wiki", + "target": "reference--configuration.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "e2cd0c2c90bf4a8c8a5496781f0ebac3abac90ee4e6fd7c5c880c167360b5dc2" + }, + { + "page": "Home.md", + "label": "Architecture", + "url": "overview--architecture", + "kind": "wiki", + "target": "overview--architecture.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "0883e334bc8fad7da72f1ec371c341af7ba36ce0cffe6cc9cbdd2650e930b8ff" + }, + { + "page": "Home.md", + "label": "Guarantees and security boundaries", + "url": "security", + "kind": "wiki", + "target": "security.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "315d90606179112d193cb40660b8dc37a560f00f45e5d58b2e9d157e26aff97e" + }, + { + "page": "Home.md", + "label": "Development", + "url": "how-to-contribute", + "kind": "wiki", + "target": "how-to-contribute.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "af69fcc7766b90ae605cb08d435f9729c9793d6cd62beb5a0648795399597d23" + }, + { + "page": "Home.md", + "label": "Release process", + "url": "deployment", + "kind": "wiki", + "target": "deployment.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "02f7a3892db566a4574c2e7ba1fd7be58f6d2dac6b673d8ceb3d4f34231e39cb" + }, + { + "page": "Home.md", + "label": "History", + "url": "lore", + "kind": "wiki", + "target": "lore.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "3e2c49f0a18dd013e7b06e35b5caf070f966f08b42c59aec02a069ee7d716d3c" + }, + { + "page": "_Sidebar.md", + "label": "Home", + "url": "Home", + "kind": "wiki", + "target": "Home.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "12dfffd340a1fee2268048fff341ca20dd7d1dd63678d7a23c4f89acd97484f2" + }, + { + "page": "_Sidebar.md", + "label": "Overview", + "url": "overview", + "kind": "wiki", + "target": "overview.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "27a14b0903fdba5a87de86a5f1bfa1e14b69c038fd85ef611441276ae013595c" + }, + { + "page": "_Sidebar.md", + "label": "Get started", + "url": "overview--getting-started", + "kind": "wiki", + "target": "overview--getting-started.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "383bbf356006198eee06bb7621b259fe44a1e54601a8b159855d59d8d061e78c" + }, + { + "page": "_Sidebar.md", + "label": "Architecture", + "url": "overview--architecture", + "kind": "wiki", + "target": "overview--architecture.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "0883e334bc8fad7da72f1ec371c341af7ba36ce0cffe6cc9cbdd2650e930b8ff" + }, + { + "page": "_Sidebar.md", + "label": "Glossary", + "url": "overview--glossary", + "kind": "wiki", + "target": "overview--glossary.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "e42ec8793d34b3f9a4ba197f32be345d64a2c3b75a5f3bd4c5bc3a03179fd865" + }, + { + "page": "_Sidebar.md", + "label": "Authoritative docs", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/index.md", + "kind": "local-canonical", + "target": "docs/index.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "a8674488ab75bc8aef157db9cd50e11cb2815f378e54034e769618f8aecc2488" + }, + { + "page": "_Sidebar.md", + "label": "Features", + "url": "features", + "kind": "wiki", + "target": "features.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "0419fde99a70000de398d2615eb89ca709478b0f7349a0786846eaf2b6483d06" + }, + { + "page": "_Sidebar.md", + "label": "Flow loop", + "url": "features--flow-loop", + "kind": "wiki", + "target": "features--flow-loop.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "dc965658f7313b996941dccdeea913c68e7c83dcb8f85993292ecb78d8ca7d57" + }, + { + "page": "_Sidebar.md", + "label": "Planning and approval", + "url": "features--planning-and-approval", + "kind": "wiki", + "target": "features--planning-and-approval.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "475e4ee4ccb9f51fe5c6746fb989347bf2b74a722f03ddf4e1bfded3437afefe" + }, + { + "page": "_Sidebar.md", + "label": "Execution and completion", + "url": "features--execution-and-completion", + "kind": "wiki", + "target": "features--execution-and-completion.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "78a1348558b03210cb585341cc20ce559db2ff2c681a73c1b6d28a602ceb5761" + }, + { + "page": "_Sidebar.md", + "label": "Review and validation", + "url": "features--review-and-validation", + "kind": "wiki", + "target": "features--review-and-validation.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "05168dc2bc9396ac131fa909b1c5f8453c11c51084a95f1f9f08933f9f8f8e2d" + }, + { + "page": "_Sidebar.md", + "label": "Bundled guidance", + "url": "features--managed-skills", + "kind": "wiki", + "target": "features--managed-skills.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "cb63e876113d57fbed0e2a27c9f035615d5b5546f4cf77839c3f033ad94e3a76" + }, + { + "page": "_Sidebar.md", + "label": "Bounded worker waves", + "url": "features--parallel-orchestration", + "kind": "wiki", + "target": "features--parallel-orchestration.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "5a1842d79e2e4100e04bfb02db831f8cda81eb32f47d946ebf61d20717547182" + }, + { + "page": "_Sidebar.md", + "label": "Configuration and models", + "url": "reference--configuration", + "kind": "wiki", + "target": "reference--configuration.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "e2cd0c2c90bf4a8c8a5496781f0ebac3abac90ee4e6fd7c5c880c167360b5dc2" + }, + { + "page": "_Sidebar.md", + "label": "Security boundaries", + "url": "security", + "kind": "wiki", + "target": "security.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "315d90606179112d193cb40660b8dc37a560f00f45e5d58b2e9d157e26aff97e" + }, + { + "page": "_Sidebar.md", + "label": "Primitives", + "url": "primitives", + "kind": "wiki", + "target": "primitives.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "55da799da74005656e33d9b3d1203a4432381e7bd0bb22d8454811a7a86bc36c" + }, + { + "page": "_Sidebar.md", + "label": "Session, plan, and feature", + "url": "primitives--session-plan-feature", + "kind": "wiki", + "target": "primitives--session-plan-feature.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "5d334022a3820b125746eb928c24239ee3ef7f343ae08136f6cf17e3bafcc97b" + }, + { + "page": "_Sidebar.md", + "label": "Validation and review evidence", + "url": "primitives--validation-and-review", + "kind": "wiki", + "target": "primitives--validation-and-review.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "92bdf7dacd32a3e6890e13a87f6e15584ebd4f6b8237ca49065971fec72c4d05" + }, + { + "page": "_Sidebar.md", + "label": "API", + "url": "api", + "kind": "wiki", + "target": "api.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "a59e225907d770ed6d2600bd611044fc6e2a603cce272d9dc5fb6315c9f495c1" + }, + { + "page": "_Sidebar.md", + "label": "Flow tools", + "url": "api--flow-tools", + "kind": "wiki", + "target": "api--flow-tools.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "6cf85bfc76fe4da7d56c60f73dce6285748aa43d73836e88683173425de9c5c0" + }, + { + "page": "_Sidebar.md", + "label": "OpenCode commands", + "url": "api--open-code-commands", + "kind": "wiki", + "target": "api--open-code-commands.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "c68d5affdbe5fe5f9470bf44e0809cbbea75aff43cbcfdaa7cfa17692ca1bf0d" + }, + { + "page": "_Sidebar.md", + "label": "Systems", + "url": "systems", + "kind": "wiki", + "target": "systems.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "cde052e20cc7a7f47056425487850e052344a65717ce7fab3857c13182931f85" + }, + { + "page": "_Sidebar.md", + "label": "OpenCode v2 integration", + "url": "systems--opencode-adapter", + "kind": "wiki", + "target": "systems--opencode-adapter.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "ae3355ef3ec9675068e3e1c0c07aaa1eeab2ccfe86e1e85aac10f76203c5c4cd" + }, + { + "page": "_Sidebar.md", + "label": "State machine", + "url": "systems--runtime-state-machine", + "kind": "wiki", + "target": "systems--runtime-state-machine.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "6251f7a71983e5db75f40c1d4e29b1b4dad5cb7f39d6be351783e4018acc437b" + }, + { + "page": "_Sidebar.md", + "label": "Workspace persistence", + "url": "systems--workspace-persistence", + "kind": "wiki", + "target": "systems--workspace-persistence.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "67dba175263baf929734367e08e998bb0c47f231d5e96bc1679de34601d4dadb" + }, + { + "page": "_Sidebar.md", + "label": "Schema and JSON", + "url": "systems--schema-and-json", + "kind": "wiki", + "target": "systems--schema-and-json.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "c4407449d138a5b36547f4427321112488dae36235c94e3931a60d381499c192" + }, + { + "page": "_Sidebar.md", + "label": "Guidance distribution", + "url": "systems--skill-distribution", + "kind": "wiki", + "target": "systems--skill-distribution.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "678962124cb5be759e03a311916fac5d086cc799ad7754c4828a03353f8282d2" + }, + { + "page": "_Sidebar.md", + "label": "Package installation", + "url": "systems--cli-and-package", + "kind": "wiki", + "target": "systems--cli-and-package.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "3ce0128dce798af5e93d51949279f502e225f1663df00eeea41cb3401a3dee30" + }, + { + "page": "_Sidebar.md", + "label": "Contribute", + "url": "how-to-contribute", + "kind": "wiki", + "target": "how-to-contribute.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "af69fcc7766b90ae605cb08d435f9729c9793d6cd62beb5a0648795399597d23" + }, + { + "page": "_Sidebar.md", + "label": "Development workflow", + "url": "how-to-contribute--development-workflow", + "kind": "wiki", + "target": "how-to-contribute--development-workflow.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "f3013ec87da666e3e5b8e70b09bc03ef70661e0a6ff64eb2d7fb950c28db605b" + }, + { + "page": "_Sidebar.md", + "label": "Testing", + "url": "how-to-contribute--testing", + "kind": "wiki", + "target": "how-to-contribute--testing.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "85fcfd89570eba050cac38d04404bc4098258f51b3f06b0576ace8b7dbdf55f8" + }, + { + "page": "_Sidebar.md", + "label": "Debugging", + "url": "how-to-contribute--debugging", + "kind": "wiki", + "target": "how-to-contribute--debugging.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "06b5316899cc598d4eaf70e25ec807ef07a9b3bba0e57a991adf5c881fc6c1d4" + }, + { + "page": "_Sidebar.md", + "label": "Patterns and conventions", + "url": "how-to-contribute--patterns-and-conventions", + "kind": "wiki", + "target": "how-to-contribute--patterns-and-conventions.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "6fd2858dfbf0338ddc4667a83ec1a3ddfb946068fb0d4374271f96c74fac0514" + }, + { + "page": "_Sidebar.md", + "label": "Tooling", + "url": "how-to-contribute--tooling", + "kind": "wiki", + "target": "how-to-contribute--tooling.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "d98a268d49c75d5e75e19d5c7a6e09ee9422b68c24f3cef3c977bd833662e9a5" + }, + { + "page": "_Sidebar.md", + "label": "Release process", + "url": "deployment", + "kind": "wiki", + "target": "deployment.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "02f7a3892db566a4574c2e7ba1fd7be58f6d2dac6b673d8ceb3d4f34231e39cb" + }, + { + "page": "_Sidebar.md", + "label": "Reference", + "url": "reference", + "kind": "wiki", + "target": "reference.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "6f3caaa5ba667373b52e0b5bf203393e816d3cc0698da7296d1dea499a0dc4e7" + }, + { + "page": "_Sidebar.md", + "label": "Data models", + "url": "reference--data-models", + "kind": "wiki", + "target": "reference--data-models.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "04c50dd28b935730f9d70b380e3c04735790a6b048f972f3340c198fce38325a" + }, + { + "page": "_Sidebar.md", + "label": "Dependencies", + "url": "reference--dependencies", + "kind": "wiki", + "target": "reference--dependencies.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "e8b4ac98118ddb2c275f696c82665026f3e74242cb6546823e6bf80eca830526" + }, + { + "page": "_Sidebar.md", + "label": "Source map", + "url": "reference--source-map", + "kind": "wiki", + "target": "reference--source-map.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "9d6ac595ff746200f8ac3e237e7f10d66d4cf60571e3abe44d5053983dca888b" + }, + { + "page": "_Sidebar.md", + "label": "Background", + "url": "background", + "kind": "wiki", + "target": "background.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "8da8b1dae2c8546c93a19064b90d605b3540c6232bf9dd7c5f280a791fddc7fb" + }, + { + "page": "_Sidebar.md", + "label": "Skills-first decision", + "url": "background--skills-first-adr", + "kind": "wiki", + "target": "background--skills-first-adr.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "432661803b704445369ea044db22e35b83f0b342db0774b870022ef5e44cfa18" + }, + { + "page": "_Sidebar.md", + "label": "Lore", + "url": "lore", + "kind": "wiki", + "target": "lore.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "3e2c49f0a18dd013e7b06e35b5caf070f966f08b42c59aec02a069ee7d716d3c" + }, + { + "page": "_Sidebar.md", + "label": "Historical numbers", + "url": "by-the-numbers", + "kind": "wiki", + "target": "by-the-numbers.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "ec8792fe88e7862bf6474321fb901b1c0bc09bfd5564fff46e06fefe628618b4" + }, + { + "page": "_Sidebar.md", + "label": "Repository facts", + "url": "fun-facts", + "kind": "wiki", + "target": "fun-facts.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "dba740ea34e7df1c8015fcaa50992abadecfac43d6ac5c727d3a4c55f9533a4c" + }, + { + "page": "_Sidebar.md", + "label": "Cleanup guidance", + "url": "cleanup-opportunities", + "kind": "wiki", + "target": "cleanup-opportunities.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "31477e2505c641e4831825ddbbe86d415661f3fc19cdc04ca62fdfc6ae89d2a0" + }, + { + "page": "api--flow-tools.md", + "label": "tool contract", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/maintainer-contract.md#tools", + "kind": "local-canonical", + "target": "docs/maintainer-contract.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": "tools", + "anchorExists": true, + "status": "pass", + "targetSha256": "258604b8354eef1530da63f6c223115608356d3adbd42a49c0b4b0bcf418ae90" + }, + { + "page": "api--flow-tools.md", + "label": "review submission rules", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/validation-and-review.md", + "kind": "local-canonical", + "target": "docs/validation-and-review.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "b03d55fbe1342059a4c377142260b1bcdc5d63d8e74f4dc0c7ae6b9e4161e9ac" + }, + { + "page": "api--flow-tools.md", + "kind": "source-reference", + "target": "src/platform/opencode/tools.ts", + "status": "pass" + }, + { + "page": "api--open-code-commands.md", + "label": "README command reference", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/README.md#commands", + "kind": "local-canonical", + "target": "README.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": "commands", + "anchorExists": true, + "status": "pass", + "targetSha256": "1407ad086e757bf76240327011e3738bc21e9fb31d98c83eeb46b5500c7ad1df" + }, + { + "page": "api--open-code-commands.md", + "label": "Configuration", + "url": "reference--configuration", + "kind": "wiki", + "target": "reference--configuration.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "e2cd0c2c90bf4a8c8a5496781f0ebac3abac90ee4e6fd7c5c880c167360b5dc2" + }, + { + "page": "api--open-code-commands.md", + "label": "ADR 0018", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/adr/0018-opencode-v2-only.md", + "kind": "local-canonical", + "target": "docs/adr/0018-opencode-v2-only.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "fd9a389c4811359ef80ea6ca857f6b3105a92d2eb793687cd405d57f0cf15f4c" + }, + { + "page": "api--open-code-commands.md", + "label": "command contract", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/maintainer-contract.md#commands", + "kind": "local-canonical", + "target": "docs/maintainer-contract.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": "commands", + "anchorExists": true, + "status": "pass", + "targetSha256": "258604b8354eef1530da63f6c223115608356d3adbd42a49c0b4b0bcf418ae90" + }, + { + "page": "api.md", + "label": "Flow tools", + "url": "api--flow-tools", + "kind": "wiki", + "target": "api--flow-tools.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "6cf85bfc76fe4da7d56c60f73dce6285748aa43d73836e88683173425de9c5c0" + }, + { + "page": "api.md", + "label": "OpenCode commands", + "url": "api--open-code-commands", + "kind": "wiki", + "target": "api--open-code-commands.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "c68d5affdbe5fe5f9470bf44e0809cbbea75aff43cbcfdaa7cfa17692ca1bf0d" + }, + { + "page": "api.md", + "label": "Package installation", + "url": "systems--cli-and-package", + "kind": "wiki", + "target": "systems--cli-and-package.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "3ce0128dce798af5e93d51949279f502e225f1663df00eeea41cb3401a3dee30" + }, + { + "page": "api.md", + "label": "maintainer contract", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/maintainer-contract.md#opencode-surface", + "kind": "local-canonical", + "target": "docs/maintainer-contract.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": "opencode-surface", + "anchorExists": true, + "status": "pass", + "targetSha256": "258604b8354eef1530da63f6c223115608356d3adbd42a49c0b4b0bcf418ae90" + }, + { + "page": "background--skills-first-adr.md", + "label": "ADR 0001", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/adr/0001-skills-first-flow-architecture.md", + "kind": "local-canonical", + "target": "docs/adr/0001-skills-first-flow-architecture.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "ed0f09b609b9ed772cc0045ce9dcfab1f74ab5ee51304dc181fe0abb3e85fedb" + }, + { + "page": "background--skills-first-adr.md", + "label": "ADR 0005", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/adr/0005-flow-v6-session-v5-simplicity-first.md", + "kind": "local-canonical", + "target": "docs/adr/0005-flow-v6-session-v5-simplicity-first.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "8624a2653261ed4b20bae322e176b0bda706b223ff51648b9c1111068be65aad" + }, + { + "page": "background--skills-first-adr.md", + "label": "ADR 0018", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/adr/0018-opencode-v2-only.md", + "kind": "local-canonical", + "target": "docs/adr/0018-opencode-v2-only.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "fd9a389c4811359ef80ea6ca857f6b3105a92d2eb793687cd405d57f0cf15f4c" + }, + { + "page": "background--skills-first-adr.md", + "label": "current maintainer contract", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/maintainer-contract.md", + "kind": "local-canonical", + "target": "docs/maintainer-contract.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "258604b8354eef1530da63f6c223115608356d3adbd42a49c0b4b0bcf418ae90" + }, + { + "page": "background--skills-first-adr.md", + "label": "Bundled guidance", + "url": "features--managed-skills", + "kind": "wiki", + "target": "features--managed-skills.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "cb63e876113d57fbed0e2a27c9f035615d5b5546f4cf77839c3f033ad94e3a76" + }, + { + "page": "background--skills-first-adr.md", + "label": "Architecture", + "url": "overview--architecture", + "kind": "wiki", + "target": "overview--architecture.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "0883e334bc8fad7da72f1ec371c341af7ba36ce0cffe6cc9cbdd2650e930b8ff" + }, + { + "page": "background.md", + "label": "ADR 0001", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/adr/0001-skills-first-flow-architecture.md", + "kind": "local-canonical", + "target": "docs/adr/0001-skills-first-flow-architecture.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "ed0f09b609b9ed772cc0045ce9dcfab1f74ab5ee51304dc181fe0abb3e85fedb" + }, + { + "page": "background.md", + "label": "ADR 0005", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/adr/0005-flow-v6-session-v5-simplicity-first.md", + "kind": "local-canonical", + "target": "docs/adr/0005-flow-v6-session-v5-simplicity-first.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "8624a2653261ed4b20bae322e176b0bda706b223ff51648b9c1111068be65aad" + }, + { + "page": "background.md", + "label": "ADR 0018", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/adr/0018-opencode-v2-only.md", + "kind": "local-canonical", + "target": "docs/adr/0018-opencode-v2-only.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "fd9a389c4811359ef80ea6ca857f6b3105a92d2eb793687cd405d57f0cf15f4c" + }, + { + "page": "background.md", + "label": "Skills-first decision", + "url": "background--skills-first-adr", + "kind": "wiki", + "target": "background--skills-first-adr.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "432661803b704445369ea044db22e35b83f0b342db0774b870022ef5e44cfa18" + }, + { + "page": "background.md", + "label": "Lore", + "url": "lore", + "kind": "wiki", + "target": "lore.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "3e2c49f0a18dd013e7b06e35b5caf070f966f08b42c59aec02a069ee7d716d3c" + }, + { + "page": "background.md", + "label": "current documentation index", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/index.md", + "kind": "local-canonical", + "target": "docs/index.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "a8674488ab75bc8aef157db9cd50e11cb2815f378e54034e769618f8aecc2488" + }, + { + "page": "by-the-numbers.md", + "label": "repository commit 819182c", + "url": "https://github.com/ddv1982/flow-opencode/commit/819182cab2da32253210ae6c09535f2901779304", + "kind": "historical-commit", + "status": "pass" + }, + { + "page": "by-the-numbers.md", + "label": "development source layout", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/development.md#source-layout", + "kind": "local-canonical", + "target": "docs/development.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": "source-layout", + "anchorExists": true, + "status": "pass", + "targetSha256": "53ab971982e4675a5716542b20bf8176fd3aeaa23ef4d7d4f82bb248ec86cfea" + }, + { + "page": "by-the-numbers.md", + "label": "maintainer contract", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/maintainer-contract.md#opencode-surface", + "kind": "local-canonical", + "target": "docs/maintainer-contract.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": "opencode-surface", + "anchorExists": true, + "status": "pass", + "targetSha256": "258604b8354eef1530da63f6c223115608356d3adbd42a49c0b4b0bcf418ae90" + }, + { + "page": "by-the-numbers.md", + "label": "Lore", + "url": "lore", + "kind": "wiki", + "target": "lore.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "3e2c49f0a18dd013e7b06e35b5caf070f966f08b42c59aec02a069ee7d716d3c" + }, + { + "page": "cleanup-opportunities.md", + "label": "Historical numbers", + "url": "by-the-numbers", + "kind": "wiki", + "target": "by-the-numbers.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "ec8792fe88e7862bf6474321fb901b1c0bc09bfd5564fff46e06fefe628618b4" + }, + { + "page": "cleanup-opportunities.md", + "label": "Lore", + "url": "lore", + "kind": "wiki", + "target": "lore.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "3e2c49f0a18dd013e7b06e35b5caf070f966f08b42c59aec02a069ee7d716d3c" + }, + { + "page": "cleanup-opportunities.md", + "label": "change discipline", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/development.md#change-discipline", + "kind": "local-canonical", + "target": "docs/development.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": "change-discipline", + "anchorExists": true, + "status": "pass", + "targetSha256": "53ab971982e4675a5716542b20bf8176fd3aeaa23ef4d7d4f82bb248ec86cfea" + }, + { + "page": "cleanup-opportunities.md", + "label": "ADR 0005 guardrail", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/adr/0005-flow-v6-session-v5-simplicity-first.md#guardrail", + "kind": "local-canonical", + "target": "docs/adr/0005-flow-v6-session-v5-simplicity-first.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": "guardrail", + "anchorExists": true, + "status": "pass", + "targetSha256": "8624a2653261ed4b20bae322e176b0bda706b223ff51648b9c1111068be65aad" + }, + { + "page": "cleanup-opportunities.md", + "label": "pinned-host procedure", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/development.md#bumping-the-pinned-host", + "kind": "local-canonical", + "target": "docs/development.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": "bumping-the-pinned-host", + "anchorExists": true, + "status": "pass", + "targetSha256": "53ab971982e4675a5716542b20bf8176fd3aeaa23ef4d7d4f82bb248ec86cfea" + }, + { + "page": "cleanup-opportunities.md", + "label": "Patterns and conventions", + "url": "how-to-contribute--patterns-and-conventions", + "kind": "wiki", + "target": "how-to-contribute--patterns-and-conventions.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "6fd2858dfbf0338ddc4667a83ec1a3ddfb946068fb0d4374271f96c74fac0514" + }, + { + "page": "deployment.md", + "label": "qualification guide", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/release-qualification.md", + "kind": "local-canonical", + "target": "docs/release-qualification.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "96b480a214499515c255bcf5b2b1a3c7c7d2de038171ea7d8791cba48f59a928" + }, + { + "page": "deployment.md", + "label": "development release section", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/development.md#release", + "kind": "local-canonical", + "target": "docs/development.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": "release", + "anchorExists": true, + "status": "pass", + "targetSha256": "53ab971982e4675a5716542b20bf8176fd3aeaa23ef4d7d4f82bb248ec86cfea" + }, + { + "page": "deployment.md", + "label": "README installation section", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/README.md#install", + "kind": "local-canonical", + "target": "README.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": "install", + "anchorExists": true, + "status": "pass", + "targetSha256": "1407ad086e757bf76240327011e3738bc21e9fb31d98c83eeb46b5500c7ad1df" + }, + { + "page": "features--execution-and-completion.md", + "label": "Session v5", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/maintainer-contract.md#session-v5", + "kind": "local-canonical", + "target": "docs/maintainer-contract.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": "session-v5", + "anchorExists": true, + "status": "pass", + "targetSha256": "258604b8354eef1530da63f6c223115608356d3adbd42a49c0b4b0bcf418ae90" + }, + { + "page": "features--execution-and-completion.md", + "label": "validation and closure", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/validation-and-review.md", + "kind": "local-canonical", + "target": "docs/validation-and-review.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "b03d55fbe1342059a4c377142260b1bcdc5d63d8e74f4dc0c7ae6b9e4161e9ac" + }, + { + "page": "features--execution-and-completion.md", + "label": "assurance at close", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/guarantees.md#assurance-at-close", + "kind": "local-canonical", + "target": "docs/guarantees.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": "assurance-at-close", + "anchorExists": true, + "status": "pass", + "targetSha256": "01b61636fe4e892a1ffaec84aa513f2db62765d6beceab2bf51ae7d006b21234" + }, + { + "page": "features--execution-and-completion.md", + "label": "Review and validation", + "url": "features--review-and-validation", + "kind": "wiki", + "target": "features--review-and-validation.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "05168dc2bc9396ac131fa909b1c5f8453c11c51084a95f1f9f08933f9f8f8e2d" + }, + { + "page": "features--flow-loop.md", + "label": "quickstart", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/quickstart.md", + "kind": "local-canonical", + "target": "docs/quickstart.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "cd1d6c96a0ae685214acb3ae18019d71a2f835d36e50fe800b1248dcbad579a2" + }, + { + "page": "features--flow-loop.md", + "label": "automatic continuation", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/automatic-continuation.md", + "kind": "local-canonical", + "target": "docs/automatic-continuation.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "2e045047681aef2914bcb23f24e8881861dfd62fc053be01a5d1113fbe14de07" + }, + { + "page": "features--flow-loop.md", + "label": "recovery guidance", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/troubleshooting.md", + "kind": "local-canonical", + "target": "docs/troubleshooting.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "23b055b14e58d5144c6e5aa209d166924aaa28abffbf6ff1ca374cc747073978" + }, + { + "page": "features--flow-loop.md", + "label": "Planning", + "url": "features--planning-and-approval", + "kind": "wiki", + "target": "features--planning-and-approval.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "475e4ee4ccb9f51fe5c6746fb989347bf2b74a722f03ddf4e1bfded3437afefe" + }, + { + "page": "features--flow-loop.md", + "label": "Execution", + "url": "features--execution-and-completion", + "kind": "wiki", + "target": "features--execution-and-completion.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "78a1348558b03210cb585341cc20ce559db2ff2c681a73c1b6d28a602ceb5761" + }, + { + "page": "features--managed-skills.md", + "label": "README installation instructions", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/README.md#install", + "kind": "local-canonical", + "target": "README.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": "install", + "anchorExists": true, + "status": "pass", + "targetSha256": "1407ad086e757bf76240327011e3738bc21e9fb31d98c83eeb46b5500c7ad1df" + }, + { + "page": "features--managed-skills.md", + "label": "maintainer contract", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/maintainer-contract.md#guides", + "kind": "local-canonical", + "target": "docs/maintainer-contract.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": "guides", + "anchorExists": true, + "status": "pass", + "targetSha256": "258604b8354eef1530da63f6c223115608356d3adbd42a49c0b4b0bcf418ae90" + }, + { + "page": "features--managed-skills.md", + "label": "ADR 0005", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/adr/0005-flow-v6-session-v5-simplicity-first.md", + "kind": "local-canonical", + "target": "docs/adr/0005-flow-v6-session-v5-simplicity-first.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "8624a2653261ed4b20bae322e176b0bda706b223ff51648b9c1111068be65aad" + }, + { + "page": "features--managed-skills.md", + "label": "Guidance distribution", + "url": "systems--skill-distribution", + "kind": "wiki", + "target": "systems--skill-distribution.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "678962124cb5be759e03a311916fac5d086cc799ad7754c4828a03353f8282d2" + }, + { + "page": "features--managed-skills.md", + "kind": "source-reference", + "target": "src/guidance/catalog.ts", + "status": "pass" + }, + { + "page": "features--managed-skills.md", + "kind": "source-reference", + "target": "skills/", + "status": "pass" + }, + { + "page": "features--parallel-orchestration.md", + "label": "bounded worker waves", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/maintainer-contract.md#bounded-worker-waves", + "kind": "local-canonical", + "target": "docs/maintainer-contract.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": "bounded-worker-waves", + "anchorExists": true, + "status": "pass", + "targetSha256": "258604b8354eef1530da63f6c223115608356d3adbd42a49c0b4b0bcf418ae90" + }, + { + "page": "features--parallel-orchestration.md", + "label": "ADR 0006", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/adr/0006-bounded-intra-feature-waves.md", + "kind": "local-canonical", + "target": "docs/adr/0006-bounded-intra-feature-waves.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "dd082386adbfdcbc8ce78ba5d90fba9efa56a41a46ce3c688cc65812467dee1a" + }, + { + "page": "features--parallel-orchestration.md", + "label": "manual evidence requirements", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/development.md#model-driven-wave-evidence", + "kind": "local-canonical", + "target": "docs/development.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": "model-driven-wave-evidence", + "anchorExists": true, + "status": "pass", + "targetSha256": "53ab971982e4675a5716542b20bf8176fd3aeaa23ef4d7d4f82bb248ec86cfea" + }, + { + "page": "features--parallel-orchestration.md", + "label": "Security boundaries", + "url": "security", + "kind": "wiki", + "target": "security.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "315d90606179112d193cb40660b8dc37a560f00f45e5d58b2e9d157e26aff97e" + }, + { + "page": "features--planning-and-approval.md", + "label": "Session v5 contract", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/maintainer-contract.md#session-v5", + "kind": "local-canonical", + "target": "docs/maintainer-contract.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": "session-v5", + "anchorExists": true, + "status": "pass", + "targetSha256": "258604b8354eef1530da63f6c223115608356d3adbd42a49c0b4b0bcf418ae90" + }, + { + "page": "features--planning-and-approval.md", + "label": "validation contract", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/validation-and-review.md", + "kind": "local-canonical", + "target": "docs/validation-and-review.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "b03d55fbe1342059a4c377142260b1bcdc5d63d8e74f4dc0c7ae6b9e4161e9ac" + }, + { + "page": "features--planning-and-approval.md", + "label": "Configuration", + "url": "reference--configuration", + "kind": "wiki", + "target": "reference--configuration.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "e2cd0c2c90bf4a8c8a5496781f0ebac3abac90ee4e6fd7c5c880c167360b5dc2" + }, + { + "page": "features--review-and-validation.md", + "label": "validation and review contract", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/validation-and-review.md", + "kind": "local-canonical", + "target": "docs/validation-and-review.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "b03d55fbe1342059a4c377142260b1bcdc5d63d8e74f4dc0c7ae6b9e4161e9ac" + }, + { + "page": "features--review-and-validation.md", + "label": "guarantee map", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/guarantees.md", + "kind": "local-canonical", + "target": "docs/guarantees.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "01b61636fe4e892a1ffaec84aa513f2db62765d6beceab2bf51ae7d006b21234" + }, + { + "page": "features--review-and-validation.md", + "label": "Evidence terms", + "url": "primitives--validation-and-review", + "kind": "wiki", + "target": "primitives--validation-and-review.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "92bdf7dacd32a3e6890e13a87f6e15584ebd4f6b8237ca49065971fec72c4d05" + }, + { + "page": "features.md", + "label": "README workflow", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/README.md#how-flow-works", + "kind": "local-canonical", + "target": "README.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": "how-flow-works", + "anchorExists": true, + "status": "pass", + "targetSha256": "1407ad086e757bf76240327011e3738bc21e9fb31d98c83eeb46b5500c7ad1df" + }, + { + "page": "features.md", + "label": "Flow loop", + "url": "features--flow-loop", + "kind": "wiki", + "target": "features--flow-loop.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "dc965658f7313b996941dccdeea913c68e7c83dcb8f85993292ecb78d8ca7d57" + }, + { + "page": "features.md", + "label": "Planning and approval", + "url": "features--planning-and-approval", + "kind": "wiki", + "target": "features--planning-and-approval.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "475e4ee4ccb9f51fe5c6746fb989347bf2b74a722f03ddf4e1bfded3437afefe" + }, + { + "page": "features.md", + "label": "Execution and completion", + "url": "features--execution-and-completion", + "kind": "wiki", + "target": "features--execution-and-completion.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "78a1348558b03210cb585341cc20ce559db2ff2c681a73c1b6d28a602ceb5761" + }, + { + "page": "features.md", + "label": "Review and validation", + "url": "features--review-and-validation", + "kind": "wiki", + "target": "features--review-and-validation.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "05168dc2bc9396ac131fa909b1c5f8453c11c51084a95f1f9f08933f9f8f8e2d" + }, + { + "page": "features.md", + "label": "Bundled guidance", + "url": "features--managed-skills", + "kind": "wiki", + "target": "features--managed-skills.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "cb63e876113d57fbed0e2a27c9f035615d5b5546f4cf77839c3f033ad94e3a76" + }, + { + "page": "features.md", + "label": "Bounded worker waves", + "url": "features--parallel-orchestration", + "kind": "wiki", + "target": "features--parallel-orchestration.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "5a1842d79e2e4100e04bfb02db831f8cda81eb32f47d946ebf61d20717547182" + }, + { + "page": "features.md", + "label": "guarantee map", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/guarantees.md", + "kind": "local-canonical", + "target": "docs/guarantees.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "01b61636fe4e892a1ffaec84aa513f2db62765d6beceab2bf51ae7d006b21234" + }, + { + "page": "fun-facts.md", + "label": "first plugin commit", + "url": "https://github.com/ddv1982/flow-opencode/commit/c59240e3441b3fd3863d4e4d02a6bfc30d68baf6", + "kind": "historical-commit", + "status": "pass" + }, + { + "page": "fun-facts.md", + "label": "first reviewer-gated execution", + "url": "https://github.com/ddv1982/flow-opencode/commit/9f73adde110be5a6f29b59f43f59364b6608d008", + "kind": "historical-commit", + "status": "pass" + }, + { + "page": "fun-facts.md", + "label": "Historical numbers", + "url": "by-the-numbers", + "kind": "wiki", + "target": "by-the-numbers.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "ec8792fe88e7862bf6474321fb901b1c0bc09bfd5564fff46e06fefe628618b4" + }, + { + "page": "fun-facts.md", + "label": "ADR 0001", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/adr/0001-skills-first-flow-architecture.md", + "kind": "local-canonical", + "target": "docs/adr/0001-skills-first-flow-architecture.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "ed0f09b609b9ed772cc0045ce9dcfab1f74ab5ee51304dc181fe0abb3e85fedb" + }, + { + "page": "fun-facts.md", + "label": "ADR 0005", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/adr/0005-flow-v6-session-v5-simplicity-first.md", + "kind": "local-canonical", + "target": "docs/adr/0005-flow-v6-session-v5-simplicity-first.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "8624a2653261ed4b20bae322e176b0bda706b223ff51648b9c1111068be65aad" + }, + { + "page": "fun-facts.md", + "label": "development guide", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/development.md#bumping-the-pinned-host", + "kind": "local-canonical", + "target": "docs/development.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": "bumping-the-pinned-host", + "anchorExists": true, + "status": "pass", + "targetSha256": "53ab971982e4675a5716542b20bf8176fd3aeaa23ef4d7d4f82bb248ec86cfea" + }, + { + "page": "how-to-contribute--debugging.md", + "label": "troubleshooting guide", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/troubleshooting.md", + "kind": "local-canonical", + "target": "docs/troubleshooting.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "23b055b14e58d5144c6e5aa209d166924aaa28abffbf6ff1ca374cc747073978" + }, + { + "page": "how-to-contribute--debugging.md", + "label": "README", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/README.md#install", + "kind": "local-canonical", + "target": "README.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": "install", + "anchorExists": true, + "status": "pass", + "targetSha256": "1407ad086e757bf76240327011e3738bc21e9fb31d98c83eeb46b5500c7ad1df" + }, + { + "page": "how-to-contribute--debugging.md", + "label": "lock procedure", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/troubleshooting.md#flow-reports-that-the-project-lock-is-busy", + "kind": "local-canonical", + "target": "docs/troubleshooting.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": "flow-reports-that-the-project-lock-is-busy", + "anchorExists": true, + "status": "pass", + "targetSha256": "23b055b14e58d5144c6e5aa209d166924aaa28abffbf6ff1ca374cc747073978" + }, + { + "page": "how-to-contribute--development-workflow.md", + "label": "local setup guide", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/development.md#local-setup", + "kind": "local-canonical", + "target": "docs/development.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": "local-setup", + "anchorExists": true, + "status": "pass", + "targetSha256": "53ab971982e4675a5716542b20bf8176fd3aeaa23ef4d7d4f82bb248ec86cfea" + }, + { + "page": "how-to-contribute--development-workflow.md", + "label": "source layout", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/development.md#source-layout", + "kind": "local-canonical", + "target": "docs/development.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": "source-layout", + "anchorExists": true, + "status": "pass", + "targetSha256": "53ab971982e4675a5716542b20bf8176fd3aeaa23ef4d7d4f82bb248ec86cfea" + }, + { + "page": "how-to-contribute--development-workflow.md", + "label": "change discipline", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/development.md#change-discipline", + "kind": "local-canonical", + "target": "docs/development.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": "change-discipline", + "anchorExists": true, + "status": "pass", + "targetSha256": "53ab971982e4675a5716542b20bf8176fd3aeaa23ef4d7d4f82bb248ec86cfea" + }, + { + "page": "how-to-contribute--development-workflow.md", + "label": "release instructions", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/development.md#release", + "kind": "local-canonical", + "target": "docs/development.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": "release", + "anchorExists": true, + "status": "pass", + "targetSha256": "53ab971982e4675a5716542b20bf8176fd3aeaa23ef4d7d4f82bb248ec86cfea" + }, + { + "page": "how-to-contribute--development-workflow.md", + "label": "Testing", + "url": "how-to-contribute--testing", + "kind": "wiki", + "target": "how-to-contribute--testing.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "85fcfd89570eba050cac38d04404bc4098258f51b3f06b0576ace8b7dbdf55f8" + }, + { + "page": "how-to-contribute--patterns-and-conventions.md", + "label": "dependency map", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/architecture/allowed-cross-layer-dependencies.md", + "kind": "local-canonical", + "target": "docs/architecture/allowed-cross-layer-dependencies.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "a5ee81d7238d25944cf7c87348bc4dc619f7e7df601e24dc0ffc0c152e99b49b" + }, + { + "page": "how-to-contribute--patterns-and-conventions.md", + "label": "change discipline", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/development.md#change-discipline", + "kind": "local-canonical", + "target": "docs/development.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": "change-discipline", + "anchorExists": true, + "status": "pass", + "targetSha256": "53ab971982e4675a5716542b20bf8176fd3aeaa23ef4d7d4f82bb248ec86cfea" + }, + { + "page": "how-to-contribute--patterns-and-conventions.md", + "label": "simplicity-first decision", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/adr/0005-flow-v6-session-v5-simplicity-first.md", + "kind": "local-canonical", + "target": "docs/adr/0005-flow-v6-session-v5-simplicity-first.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "8624a2653261ed4b20bae322e176b0bda706b223ff51648b9c1111068be65aad" + }, + { + "page": "how-to-contribute--patterns-and-conventions.md", + "label": "Schema and JSON", + "url": "systems--schema-and-json", + "kind": "wiki", + "target": "systems--schema-and-json.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "c4407449d138a5b36547f4427321112488dae36235c94e3931a60d381499c192" + }, + { + "page": "how-to-contribute--testing.md", + "label": "development guide", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/development.md#local-setup", + "kind": "local-canonical", + "target": "docs/development.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": "local-setup", + "anchorExists": true, + "status": "pass", + "targetSha256": "53ab971982e4675a5716542b20bf8176fd3aeaa23ef4d7d4f82bb248ec86cfea" + }, + { + "page": "how-to-contribute--testing.md", + "label": "wave evidence", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/development.md#model-driven-wave-evidence", + "kind": "local-canonical", + "target": "docs/development.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": "model-driven-wave-evidence", + "anchorExists": true, + "status": "pass", + "targetSha256": "53ab971982e4675a5716542b20bf8176fd3aeaa23ef4d7d4f82bb248ec86cfea" + }, + { + "page": "how-to-contribute--testing.md", + "label": "continuation evidence", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/development.md#model-driven-auto-continuation-evidence", + "kind": "local-canonical", + "target": "docs/development.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": "model-driven-auto-continuation-evidence", + "anchorExists": true, + "status": "pass", + "targetSha256": "53ab971982e4675a5716542b20bf8176fd3aeaa23ef4d7d4f82bb248ec86cfea" + }, + { + "page": "how-to-contribute--testing.md", + "label": "release qualification", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/release-qualification.md", + "kind": "local-canonical", + "target": "docs/release-qualification.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "96b480a214499515c255bcf5b2b1a3c7c7d2de038171ea7d8791cba48f59a928" + }, + { + "page": "how-to-contribute--testing.md", + "kind": "source-reference", + "target": "tests/domain-transitions.test.ts", + "status": "pass" + }, + { + "page": "how-to-contribute--testing.md", + "kind": "source-reference", + "target": "tests/runtime-gates.test.ts", + "status": "pass" + }, + { + "page": "how-to-contribute--testing.md", + "kind": "source-reference", + "target": "tests/validation-capture.test.ts", + "status": "pass" + }, + { + "page": "how-to-contribute--testing.md", + "kind": "source-reference", + "target": "tests/workspace-persistence.test.ts", + "status": "pass" + }, + { + "page": "how-to-contribute--tooling.md", + "label": "development guide", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/development.md", + "kind": "local-canonical", + "target": "docs/development.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "53ab971982e4675a5716542b20bf8176fd3aeaa23ef4d7d4f82bb248ec86cfea" + }, + { + "page": "how-to-contribute--tooling.md", + "label": "qualification guide", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/release-qualification.md#running-it", + "kind": "local-canonical", + "target": "docs/release-qualification.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": "running-it", + "anchorExists": true, + "status": "pass", + "targetSha256": "96b480a214499515c255bcf5b2b1a3c7c7d2de038171ea7d8791cba48f59a928" + }, + { + "page": "how-to-contribute--tooling.md", + "label": "README", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/README.md#install", + "kind": "local-canonical", + "target": "README.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": "install", + "anchorExists": true, + "status": "pass", + "targetSha256": "1407ad086e757bf76240327011e3738bc21e9fb31d98c83eeb46b5500c7ad1df" + }, + { + "page": "how-to-contribute--tooling.md", + "label": "Deployment", + "url": "deployment", + "kind": "wiki", + "target": "deployment.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "02f7a3892db566a4574c2e7ba1fd7be58f6d2dac6b673d8ceb3d4f34231e39cb" + }, + { + "page": "how-to-contribute.md", + "label": "development guide", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/development.md", + "kind": "local-canonical", + "target": "docs/development.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "53ab971982e4675a5716542b20bf8176fd3aeaa23ef4d7d4f82bb248ec86cfea" + }, + { + "page": "how-to-contribute.md", + "label": "maintainer contract", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/maintainer-contract.md", + "kind": "local-canonical", + "target": "docs/maintainer-contract.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "258604b8354eef1530da63f6c223115608356d3adbd42a49c0b4b0bcf418ae90" + }, + { + "page": "how-to-contribute.md", + "label": "source dependency rules", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/architecture/allowed-cross-layer-dependencies.md", + "kind": "local-canonical", + "target": "docs/architecture/allowed-cross-layer-dependencies.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "a5ee81d7238d25944cf7c87348bc4dc619f7e7df601e24dc0ffc0c152e99b49b" + }, + { + "page": "how-to-contribute.md", + "label": "Development workflow", + "url": "how-to-contribute--development-workflow", + "kind": "wiki", + "target": "how-to-contribute--development-workflow.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "f3013ec87da666e3e5b8e70b09bc03ef70661e0a6ff64eb2d7fb950c28db605b" + }, + { + "page": "how-to-contribute.md", + "label": "Testing", + "url": "how-to-contribute--testing", + "kind": "wiki", + "target": "how-to-contribute--testing.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "85fcfd89570eba050cac38d04404bc4098258f51b3f06b0576ace8b7dbdf55f8" + }, + { + "page": "how-to-contribute.md", + "label": "Debugging", + "url": "how-to-contribute--debugging", + "kind": "wiki", + "target": "how-to-contribute--debugging.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "06b5316899cc598d4eaf70e25ec807ef07a9b3bba0e57a991adf5c881fc6c1d4" + }, + { + "page": "how-to-contribute.md", + "label": "Patterns and conventions", + "url": "how-to-contribute--patterns-and-conventions", + "kind": "wiki", + "target": "how-to-contribute--patterns-and-conventions.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "6fd2858dfbf0338ddc4667a83ec1a3ddfb946068fb0d4374271f96c74fac0514" + }, + { + "page": "how-to-contribute.md", + "label": "Tooling", + "url": "how-to-contribute--tooling", + "kind": "wiki", + "target": "how-to-contribute--tooling.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "d98a268d49c75d5e75e19d5c7a6e09ee9422b68c24f3cef3c977bd833662e9a5" + }, + { + "page": "how-to-contribute.md", + "label": "Deployment", + "url": "deployment", + "kind": "wiki", + "target": "deployment.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "02f7a3892db566a4574c2e7ba1fd7be58f6d2dac6b673d8ceb3d4f34231e39cb" + }, + { + "page": "lore.md", + "label": "first plugin commit", + "url": "https://github.com/ddv1982/flow-opencode/commit/c59240e3441b3fd3863d4e4d02a6bfc30d68baf6", + "kind": "historical-commit", + "status": "pass" + }, + { + "page": "lore.md", + "label": "Reviewer-gated execution", + "url": "https://github.com/ddv1982/flow-opencode/commit/9f73adde110be5a6f29b59f43f59364b6608d008", + "kind": "historical-commit", + "status": "pass" + }, + { + "page": "lore.md", + "label": "GitHub Actions validation", + "url": "https://github.com/ddv1982/flow-opencode/commit/fdcaf7f46437f881423e039b51103c74b49e4bd1", + "kind": "historical-commit", + "status": "pass" + }, + { + "page": "lore.md", + "label": "Persistence hardening", + "url": "https://github.com/ddv1982/flow-opencode/commit/e97b4dc848f8cdbf1c5357e7384bf8f767d1728e", + "kind": "historical-commit", + "status": "pass" + }, + { + "page": "lore.md", + "label": "ADR 0001", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/adr/0001-skills-first-flow-architecture.md", + "kind": "local-canonical", + "target": "docs/adr/0001-skills-first-flow-architecture.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "ed0f09b609b9ed772cc0045ce9dcfab1f74ab5ee51304dc181fe0abb3e85fedb" + }, + { + "page": "lore.md", + "label": "Flow 4.2.0", + "url": "https://github.com/ddv1982/flow-opencode/commit/7aefd0a4eee32793fc4c54b9eacbe9b56377dd8a", + "kind": "historical-commit", + "status": "pass" + }, + { + "page": "lore.md", + "label": "ADR 0005", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/adr/0005-flow-v6-session-v5-simplicity-first.md", + "kind": "local-canonical", + "target": "docs/adr/0005-flow-v6-session-v5-simplicity-first.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "8624a2653261ed4b20bae322e176b0bda706b223ff51648b9c1111068be65aad" + }, + { + "page": "lore.md", + "label": "ADR 0018", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/adr/0018-opencode-v2-only.md", + "kind": "local-canonical", + "target": "docs/adr/0018-opencode-v2-only.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "fd9a389c4811359ef80ea6ca857f6b3105a92d2eb793687cd405d57f0cf15f4c" + }, + { + "page": "lore.md", + "label": "maintainer contract", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/maintainer-contract.md", + "kind": "local-canonical", + "target": "docs/maintainer-contract.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "258604b8354eef1530da63f6c223115608356d3adbd42a49c0b4b0bcf418ae90" + }, + { + "page": "lore.md", + "label": "Historical numbers", + "url": "by-the-numbers", + "kind": "wiki", + "target": "by-the-numbers.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "ec8792fe88e7862bf6474321fb901b1c0bc09bfd5564fff46e06fefe628618b4" + }, + { + "page": "lore.md", + "label": "Background", + "url": "background", + "kind": "wiki", + "target": "background.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "8da8b1dae2c8546c93a19064b90d605b3540c6232bf9dd7c5f280a791fddc7fb" + }, + { + "page": "lore.md", + "label": "Release process", + "url": "deployment", + "kind": "wiki", + "target": "deployment.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "02f7a3892db566a4574c2e7ba1fd7be58f6d2dac6b673d8ceb3d4f34231e39cb" + }, + { + "page": "overview--architecture.md", + "label": "source layout", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/development.md#source-layout", + "kind": "local-canonical", + "target": "docs/development.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": "source-layout", + "anchorExists": true, + "status": "pass", + "targetSha256": "53ab971982e4675a5716542b20bf8176fd3aeaa23ef4d7d4f82bb248ec86cfea" + }, + { + "page": "overview--architecture.md", + "label": "allowed dependencies", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/architecture/allowed-cross-layer-dependencies.md", + "kind": "local-canonical", + "target": "docs/architecture/allowed-cross-layer-dependencies.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "a5ee81d7238d25944cf7c87348bc4dc619f7e7df601e24dc0ffc0c152e99b49b" + }, + { + "page": "overview--architecture.md", + "label": "ADR 0018", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/adr/0018-opencode-v2-only.md", + "kind": "local-canonical", + "target": "docs/adr/0018-opencode-v2-only.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "fd9a389c4811359ef80ea6ca857f6b3105a92d2eb793687cd405d57f0cf15f4c" + }, + { + "page": "overview--architecture.md", + "label": "State machine", + "url": "systems--runtime-state-machine", + "kind": "wiki", + "target": "systems--runtime-state-machine.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "6251f7a71983e5db75f40c1d4e29b1b4dad5cb7f39d6be351783e4018acc437b" + }, + { + "page": "overview--architecture.md", + "label": "Persistence", + "url": "systems--workspace-persistence", + "kind": "wiki", + "target": "systems--workspace-persistence.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "67dba175263baf929734367e08e998bb0c47f231d5e96bc1679de34601d4dadb" + }, + { + "page": "overview--architecture.md", + "label": "Source map", + "url": "reference--source-map", + "kind": "wiki", + "target": "reference--source-map.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "9d6ac595ff746200f8ac3e237e7f10d66d4cf60571e3abe44d5053983dca888b" + }, + { + "page": "overview--getting-started.md", + "label": "README", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/README.md#install", + "kind": "local-canonical", + "target": "README.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": "install", + "anchorExists": true, + "status": "pass", + "targetSha256": "1407ad086e757bf76240327011e3738bc21e9fb31d98c83eeb46b5500c7ad1df" + }, + { + "page": "overview--getting-started.md", + "label": "quickstart", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/quickstart.md", + "kind": "local-canonical", + "target": "docs/quickstart.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "cd1d6c96a0ae685214acb3ae18019d71a2f835d36e50fe800b1248dcbad579a2" + }, + { + "page": "overview--getting-started.md", + "label": "development", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/development.md#local-setup", + "kind": "local-canonical", + "target": "docs/development.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": "local-setup", + "anchorExists": true, + "status": "pass", + "targetSha256": "53ab971982e4675a5716542b20bf8176fd3aeaa23ef4d7d4f82bb248ec86cfea" + }, + { + "page": "overview--getting-started.md", + "label": "troubleshooting", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/troubleshooting.md#flow-commands-are-missing", + "kind": "local-canonical", + "target": "docs/troubleshooting.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": "flow-commands-are-missing", + "anchorExists": true, + "status": "pass", + "targetSha256": "23b055b14e58d5144c6e5aa209d166924aaa28abffbf6ff1ca374cc747073978" + }, + { + "page": "overview--glossary.md", + "label": "maintainer contract", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/maintainer-contract.md", + "kind": "local-canonical", + "target": "docs/maintainer-contract.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "258604b8354eef1530da63f6c223115608356d3adbd42a49c0b4b0bcf418ae90" + }, + { + "page": "overview--glossary.md", + "label": "validation contract", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/validation-and-review.md", + "kind": "local-canonical", + "target": "docs/validation-and-review.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "b03d55fbe1342059a4c377142260b1bcdc5d63d8e74f4dc0c7ae6b9e4161e9ac" + }, + { + "page": "overview--glossary.md", + "label": "Evidence", + "url": "primitives--validation-and-review", + "kind": "wiki", + "target": "primitives--validation-and-review.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "92bdf7dacd32a3e6890e13a87f6e15584ebd4f6b8237ca49065971fec72c4d05" + }, + { + "page": "overview.md", + "label": "README", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/README.md", + "kind": "local-canonical", + "target": "README.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "1407ad086e757bf76240327011e3738bc21e9fb31d98c83eeb46b5500c7ad1df" + }, + { + "page": "overview.md", + "label": "documentation index", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/index.md", + "kind": "local-canonical", + "target": "docs/index.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "a8674488ab75bc8aef157db9cd50e11cb2815f378e54034e769618f8aecc2488" + }, + { + "page": "overview.md", + "label": "Get started", + "url": "overview--getting-started", + "kind": "wiki", + "target": "overview--getting-started.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "383bbf356006198eee06bb7621b259fe44a1e54601a8b159855d59d8d061e78c" + }, + { + "page": "overview.md", + "label": "Understand the architecture", + "url": "overview--architecture", + "kind": "wiki", + "target": "overview--architecture.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "0883e334bc8fad7da72f1ec371c341af7ba36ce0cffe6cc9cbdd2650e930b8ff" + }, + { + "page": "overview.md", + "label": "Look up terms", + "url": "overview--glossary", + "kind": "wiki", + "target": "overview--glossary.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "e42ec8793d34b3f9a4ba197f32be345d64a2c3b75a5f3bd4c5bc3a03179fd865" + }, + { + "page": "overview.md", + "label": "Follow the workflow", + "url": "features--flow-loop", + "kind": "wiki", + "target": "features--flow-loop.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "dc965658f7313b996941dccdeea913c68e7c83dcb8f85993292ecb78d8ca7d57" + }, + { + "page": "primitives--session-plan-feature.md", + "label": "Session v5", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/maintainer-contract.md#session-v5", + "kind": "local-canonical", + "target": "docs/maintainer-contract.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": "session-v5", + "anchorExists": true, + "status": "pass", + "targetSha256": "258604b8354eef1530da63f6c223115608356d3adbd42a49c0b4b0bcf418ae90" + }, + { + "page": "primitives--session-plan-feature.md", + "label": "inspection outcomes", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/guarantees.md#terminal-recovery-and-inspection-outcomes", + "kind": "local-canonical", + "target": "docs/guarantees.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": "terminal-recovery-and-inspection-outcomes", + "anchorExists": true, + "status": "pass", + "targetSha256": "01b61636fe4e892a1ffaec84aa513f2db62765d6beceab2bf51ae7d006b21234" + }, + { + "page": "primitives--session-plan-feature.md", + "label": "Planning", + "url": "features--planning-and-approval", + "kind": "wiki", + "target": "features--planning-and-approval.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "475e4ee4ccb9f51fe5c6746fb989347bf2b74a722f03ddf4e1bfded3437afefe" + }, + { + "page": "primitives--session-plan-feature.md", + "label": "Persistence", + "url": "systems--workspace-persistence", + "kind": "wiki", + "target": "systems--workspace-persistence.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "67dba175263baf929734367e08e998bb0c47f231d5e96bc1679de34601d4dadb" + }, + { + "page": "primitives--validation-and-review.md", + "label": "validation contract", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/validation-and-review.md", + "kind": "local-canonical", + "target": "docs/validation-and-review.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "b03d55fbe1342059a4c377142260b1bcdc5d63d8e74f4dc0c7ae6b9e4161e9ac" + }, + { + "page": "primitives--validation-and-review.md", + "label": "ADR 0014", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/adr/0014-one-evidence-record.md", + "kind": "local-canonical", + "target": "docs/adr/0014-one-evidence-record.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "6e66eda9d3ffcc5f9a40f9b0c938b022cee3a39180682e5b2e546a192703ac37" + }, + { + "page": "primitives--validation-and-review.md", + "label": "ADR 0017", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/adr/0017-inspection-gate-outcome.md", + "kind": "local-canonical", + "target": "docs/adr/0017-inspection-gate-outcome.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "b3de68471014627b72a32b6df89a5cabd3a27beb5f4830e7bae58363e54f3069" + }, + { + "page": "primitives.md", + "label": "Session, plan, and feature", + "url": "primitives--session-plan-feature", + "kind": "wiki", + "target": "primitives--session-plan-feature.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "5d334022a3820b125746eb928c24239ee3ef7f343ae08136f6cf17e3bafcc97b" + }, + { + "page": "primitives.md", + "label": "Validation and review", + "url": "primitives--validation-and-review", + "kind": "wiki", + "target": "primitives--validation-and-review.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "92bdf7dacd32a3e6890e13a87f6e15584ebd4f6b8237ca49065971fec72c4d05" + }, + { + "page": "primitives.md", + "label": "maintainer contract", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/maintainer-contract.md#session-v5", + "kind": "local-canonical", + "target": "docs/maintainer-contract.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": "session-v5", + "anchorExists": true, + "status": "pass", + "targetSha256": "258604b8354eef1530da63f6c223115608356d3adbd42a49c0b4b0bcf418ae90" + }, + { + "page": "primitives.md", + "label": "guarantee map", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/guarantees.md", + "kind": "local-canonical", + "target": "docs/guarantees.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "01b61636fe4e892a1ffaec84aa513f2db62765d6beceab2bf51ae7d006b21234" + }, + { + "page": "primitives.md", + "label": "Data models", + "url": "reference--data-models", + "kind": "wiki", + "target": "reference--data-models.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "04c50dd28b935730f9d70b380e3c04735790a6b048f972f3340c198fce38325a" + }, + { + "page": "reference--configuration.md", + "label": "README", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/README.md#install", + "kind": "local-canonical", + "target": "README.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": "install", + "anchorExists": true, + "status": "pass", + "targetSha256": "1407ad086e757bf76240327011e3738bc21e9fb31d98c83eeb46b5500c7ad1df" + }, + { + "page": "reference--configuration.md", + "label": "quickstart", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/quickstart.md#select-flow-models", + "kind": "local-canonical", + "target": "docs/quickstart.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": "select-flow-models", + "anchorExists": true, + "status": "pass", + "targetSha256": "cd1d6c96a0ae685214acb3ae18019d71a2f835d36e50fe800b1248dcbad579a2" + }, + { + "page": "reference--configuration.md", + "label": "ADR 0018", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/adr/0018-opencode-v2-only.md", + "kind": "local-canonical", + "target": "docs/adr/0018-opencode-v2-only.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "fd9a389c4811359ef80ea6ca857f6b3105a92d2eb793687cd405d57f0cf15f4c" + }, + { + "page": "reference--configuration.md", + "label": "OpenCode contract", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/maintainer-contract.md#opencode-surface", + "kind": "local-canonical", + "target": "docs/maintainer-contract.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": "opencode-surface", + "anchorExists": true, + "status": "pass", + "targetSha256": "258604b8354eef1530da63f6c223115608356d3adbd42a49c0b4b0bcf418ae90" + }, + { + "page": "reference--data-models.md", + "label": "Session v5", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/maintainer-contract.md#session-v5", + "kind": "local-canonical", + "target": "docs/maintainer-contract.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": "session-v5", + "anchorExists": true, + "status": "pass", + "targetSha256": "258604b8354eef1530da63f6c223115608356d3adbd42a49c0b4b0bcf418ae90" + }, + { + "page": "reference--data-models.md", + "label": "validation and review", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/validation-and-review.md", + "kind": "local-canonical", + "target": "docs/validation-and-review.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "b03d55fbe1342059a4c377142260b1bcdc5d63d8e74f4dc0c7ae6b9e4161e9ac" + }, + { + "page": "reference--data-models.md", + "kind": "source-reference", + "target": "src/domain/session.ts", + "status": "pass" + }, + { + "page": "reference--data-models.md", + "kind": "source-reference", + "target": "src/application/schema.ts", + "status": "pass" + }, + { + "page": "reference--dependencies.md", + "label": "host upgrade discipline", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/development.md#bumping-the-pinned-host", + "kind": "local-canonical", + "target": "docs/development.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": "bumping-the-pinned-host", + "anchorExists": true, + "status": "pass", + "targetSha256": "53ab971982e4675a5716542b20bf8176fd3aeaa23ef4d7d4f82bb248ec86cfea" + }, + { + "page": "reference--dependencies.md", + "label": "ADR 0018", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/adr/0018-opencode-v2-only.md", + "kind": "local-canonical", + "target": "docs/adr/0018-opencode-v2-only.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "fd9a389c4811359ef80ea6ca857f6b3105a92d2eb793687cd405d57f0cf15f4c" + }, + { + "page": "reference--dependencies.md", + "label": "Testing", + "url": "how-to-contribute--testing", + "kind": "wiki", + "target": "how-to-contribute--testing.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "85fcfd89570eba050cac38d04404bc4098258f51b3f06b0576ace8b7dbdf55f8" + }, + { + "page": "reference--dependencies.md", + "label": "Deployment", + "url": "deployment", + "kind": "wiki", + "target": "deployment.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "02f7a3892db566a4574c2e7ba1fd7be58f6d2dac6b673d8ceb3d4f34231e39cb" + }, + { + "page": "reference--source-map.md", + "label": "development source layout", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/development.md#source-layout", + "kind": "local-canonical", + "target": "docs/development.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": "source-layout", + "anchorExists": true, + "status": "pass", + "targetSha256": "53ab971982e4675a5716542b20bf8176fd3aeaa23ef4d7d4f82bb248ec86cfea" + }, + { + "page": "reference--source-map.md", + "label": "dependency rules", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/architecture/allowed-cross-layer-dependencies.md", + "kind": "local-canonical", + "target": "docs/architecture/allowed-cross-layer-dependencies.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "a5ee81d7238d25944cf7c87348bc4dc619f7e7df601e24dc0ffc0c152e99b49b" + }, + { + "page": "reference--source-map.md", + "label": "Configuration", + "url": "reference--configuration", + "kind": "wiki", + "target": "reference--configuration.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "e2cd0c2c90bf4a8c8a5496781f0ebac3abac90ee4e6fd7c5c880c167360b5dc2" + }, + { + "page": "reference--source-map.md", + "kind": "source-reference", + "target": "src/domain/", + "status": "pass" + }, + { + "page": "reference--source-map.md", + "kind": "source-reference", + "target": "src/application/", + "status": "pass" + }, + { + "page": "reference--source-map.md", + "kind": "source-reference", + "target": "src/infrastructure/", + "status": "pass" + }, + { + "page": "reference--source-map.md", + "kind": "source-reference", + "target": "src/platform/opencode/", + "status": "pass" + }, + { + "page": "reference--source-map.md", + "kind": "source-reference", + "target": "src/guidance/", + "status": "pass" + }, + { + "page": "reference--source-map.md", + "kind": "source-reference", + "target": "skills/", + "status": "pass" + }, + { + "page": "reference--source-map.md", + "kind": "source-reference", + "target": "src/config-shared.ts", + "status": "pass" + }, + { + "page": "reference--source-map.md", + "kind": "source-reference", + "target": "src/prompt-surfaces.ts", + "status": "pass" + }, + { + "page": "reference--source-map.md", + "kind": "source-reference", + "target": "src/index.ts", + "status": "pass" + }, + { + "page": "reference--source-map.md", + "kind": "source-reference", + "target": "src/tui.ts", + "status": "pass" + }, + { + "page": "reference--source-map.md", + "kind": "source-reference", + "target": "tests/", + "status": "pass" + }, + { + "page": "reference--source-map.md", + "kind": "source-reference", + "target": "docs/", + "status": "pass" + }, + { + "page": "reference--source-map.md", + "kind": "source-reference", + "target": "src/platform/opencode/model-picker.ts", + "status": "pass" + }, + { + "page": "reference--source-map.md", + "kind": "source-reference", + "target": "src/platform/opencode/model-preferences.ts", + "status": "pass" + }, + { + "page": "reference--source-map.md", + "kind": "source-reference", + "target": "src/application/model-preferences.ts", + "status": "pass" + }, + { + "page": "reference--source-map.md", + "kind": "source-reference", + "target": "src/application/model-preferences-schema.ts", + "status": "pass" + }, + { + "page": "reference--source-map.md", + "kind": "source-reference", + "target": "src/infrastructure/fs/model-preference-store.ts", + "status": "pass" + }, + { + "page": "reference.md", + "label": "documentation index", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/index.md", + "kind": "local-canonical", + "target": "docs/index.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "a8674488ab75bc8aef157db9cd50e11cb2815f378e54034e769618f8aecc2488" + }, + { + "page": "reference.md", + "label": "Configuration", + "url": "reference--configuration", + "kind": "wiki", + "target": "reference--configuration.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "e2cd0c2c90bf4a8c8a5496781f0ebac3abac90ee4e6fd7c5c880c167360b5dc2" + }, + { + "page": "reference.md", + "label": "Data models", + "url": "reference--data-models", + "kind": "wiki", + "target": "reference--data-models.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "04c50dd28b935730f9d70b380e3c04735790a6b048f972f3340c198fce38325a" + }, + { + "page": "reference.md", + "label": "Dependencies", + "url": "reference--dependencies", + "kind": "wiki", + "target": "reference--dependencies.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "e8b4ac98118ddb2c275f696c82665026f3e74242cb6546823e6bf80eca830526" + }, + { + "page": "reference.md", + "label": "Source map", + "url": "reference--source-map", + "kind": "wiki", + "target": "reference--source-map.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "9d6ac595ff746200f8ac3e237e7f10d66d4cf60571e3abe44d5053983dca888b" + }, + { + "page": "reference.md", + "label": "maintainer contract", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/maintainer-contract.md", + "kind": "local-canonical", + "target": "docs/maintainer-contract.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "258604b8354eef1530da63f6c223115608356d3adbd42a49c0b4b0bcf418ae90" + }, + { + "page": "reference.md", + "label": "guarantee map", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/guarantees.md", + "kind": "local-canonical", + "target": "docs/guarantees.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "01b61636fe4e892a1ffaec84aa513f2db62765d6beceab2bf51ae7d006b21234" + }, + { + "page": "security.md", + "label": "guarantee map and threat model", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/guarantees.md#threat-model", + "kind": "local-canonical", + "target": "docs/guarantees.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": "threat-model", + "anchorExists": true, + "status": "pass", + "targetSha256": "01b61636fe4e892a1ffaec84aa513f2db62765d6beceab2bf51ae7d006b21234" + }, + { + "page": "security.md", + "label": "persistence contract", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/maintainer-contract.md#persistence", + "kind": "local-canonical", + "target": "docs/maintainer-contract.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": "persistence", + "anchorExists": true, + "status": "pass", + "targetSha256": "258604b8354eef1530da63f6c223115608356d3adbd42a49c0b4b0bcf418ae90" + }, + { + "page": "security.md", + "label": "recovery advice description", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/README.md#recovery-advice-development-preview", + "kind": "local-canonical", + "target": "README.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": "recovery-advice-development-preview", + "anchorExists": true, + "status": "pass", + "targetSha256": "1407ad086e757bf76240327011e3738bc21e9fb31d98c83eeb46b5500c7ad1df" + }, + { + "page": "systems--cli-and-package.md", + "label": "README instructions", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/README.md#install", + "kind": "local-canonical", + "target": "README.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": "install", + "anchorExists": true, + "status": "pass", + "targetSha256": "1407ad086e757bf76240327011e3738bc21e9fb31d98c83eeb46b5500c7ad1df" + }, + { + "page": "systems--cli-and-package.md", + "label": "development", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/development.md", + "kind": "local-canonical", + "target": "docs/development.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "53ab971982e4675a5716542b20bf8176fd3aeaa23ef4d7d4f82bb248ec86cfea" + }, + { + "page": "systems--cli-and-package.md", + "label": "release qualification", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/release-qualification.md", + "kind": "local-canonical", + "target": "docs/release-qualification.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "96b480a214499515c255bcf5b2b1a3c7c7d2de038171ea7d8791cba48f59a928" + }, + { + "page": "systems--cli-and-package.md", + "label": "ADR 0018", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/adr/0018-opencode-v2-only.md", + "kind": "local-canonical", + "target": "docs/adr/0018-opencode-v2-only.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "fd9a389c4811359ef80ea6ca857f6b3105a92d2eb793687cd405d57f0cf15f4c" + }, + { + "page": "systems--cli-and-package.md", + "label": "Deployment", + "url": "deployment", + "kind": "wiki", + "target": "deployment.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "02f7a3892db566a4574c2e7ba1fd7be58f6d2dac6b673d8ceb3d4f34231e39cb" + }, + { + "page": "systems--opencode-adapter.md", + "label": "ADR 0018", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/adr/0018-opencode-v2-only.md", + "kind": "local-canonical", + "target": "docs/adr/0018-opencode-v2-only.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "fd9a389c4811359ef80ea6ca857f6b3105a92d2eb793687cd405d57f0cf15f4c" + }, + { + "page": "systems--opencode-adapter.md", + "label": "OpenCode contract", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/maintainer-contract.md#opencode-surface", + "kind": "local-canonical", + "target": "docs/maintainer-contract.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": "opencode-surface", + "anchorExists": true, + "status": "pass", + "targetSha256": "258604b8354eef1530da63f6c223115608356d3adbd42a49c0b4b0bcf418ae90" + }, + { + "page": "systems--opencode-adapter.md", + "label": "source ownership", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/development.md#source-layout", + "kind": "local-canonical", + "target": "docs/development.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": "source-layout", + "anchorExists": true, + "status": "pass", + "targetSha256": "53ab971982e4675a5716542b20bf8176fd3aeaa23ef4d7d4f82bb248ec86cfea" + }, + { + "page": "systems--opencode-adapter.md", + "label": "Commands", + "url": "api--open-code-commands", + "kind": "wiki", + "target": "api--open-code-commands.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "c68d5affdbe5fe5f9470bf44e0809cbbea75aff43cbcfdaa7cfa17692ca1bf0d" + }, + { + "page": "systems--opencode-adapter.md", + "label": "Configuration", + "url": "reference--configuration", + "kind": "wiki", + "target": "reference--configuration.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "e2cd0c2c90bf4a8c8a5496781f0ebac3abac90ee4e6fd7c5c880c167360b5dc2" + }, + { + "page": "systems--opencode-adapter.md", + "kind": "source-reference", + "target": "src/platform/opencode/", + "status": "pass" + }, + { + "page": "systems--runtime-state-machine.md", + "label": "Session v5 contract", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/maintainer-contract.md#session-v5", + "kind": "local-canonical", + "target": "docs/maintainer-contract.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": "session-v5", + "anchorExists": true, + "status": "pass", + "targetSha256": "258604b8354eef1530da63f6c223115608356d3adbd42a49c0b4b0bcf418ae90" + }, + { + "page": "systems--runtime-state-machine.md", + "label": "causality rules", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/maintainer-contract.md#causality-and-idempotency", + "kind": "local-canonical", + "target": "docs/maintainer-contract.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": "causality-and-idempotency", + "anchorExists": true, + "status": "pass", + "targetSha256": "258604b8354eef1530da63f6c223115608356d3adbd42a49c0b4b0bcf418ae90" + }, + { + "page": "systems--runtime-state-machine.md", + "label": "Execution", + "url": "features--execution-and-completion", + "kind": "wiki", + "target": "features--execution-and-completion.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "78a1348558b03210cb585341cc20ce559db2ff2c681a73c1b6d28a602ceb5761" + }, + { + "page": "systems--runtime-state-machine.md", + "label": "Data models", + "url": "reference--data-models", + "kind": "wiki", + "target": "reference--data-models.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "04c50dd28b935730f9d70b380e3c04735790a6b048f972f3340c198fce38325a" + }, + { + "page": "systems--runtime-state-machine.md", + "kind": "source-reference", + "target": "src/domain/transitions.ts", + "status": "pass" + }, + { + "page": "systems--runtime-state-machine.md", + "kind": "source-reference", + "target": "src/application/flow-service.ts", + "status": "pass" + }, + { + "page": "systems--schema-and-json.md", + "label": "dependency rules", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/architecture/allowed-cross-layer-dependencies.md", + "kind": "local-canonical", + "target": "docs/architecture/allowed-cross-layer-dependencies.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "a5ee81d7238d25944cf7c87348bc4dc619f7e7df601e24dc0ffc0c152e99b49b" + }, + { + "page": "systems--schema-and-json.md", + "label": "Session v5 contract", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/maintainer-contract.md#session-v5", + "kind": "local-canonical", + "target": "docs/maintainer-contract.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": "session-v5", + "anchorExists": true, + "status": "pass", + "targetSha256": "258604b8354eef1530da63f6c223115608356d3adbd42a49c0b4b0bcf418ae90" + }, + { + "page": "systems--schema-and-json.md", + "label": "unreadable-state recovery", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/troubleshooting.md#session-state-is-unreadable", + "kind": "local-canonical", + "target": "docs/troubleshooting.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": "session-state-is-unreadable", + "anchorExists": true, + "status": "pass", + "targetSha256": "23b055b14e58d5144c6e5aa209d166924aaa28abffbf6ff1ca374cc747073978" + }, + { + "page": "systems--schema-and-json.md", + "label": "Data models", + "url": "reference--data-models", + "kind": "wiki", + "target": "reference--data-models.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "04c50dd28b935730f9d70b380e3c04735790a6b048f972f3340c198fce38325a" + }, + { + "page": "systems--schema-and-json.md", + "kind": "source-reference", + "target": "src/domain/session.ts", + "status": "pass" + }, + { + "page": "systems--schema-and-json.md", + "kind": "source-reference", + "target": "src/application/schema.ts", + "status": "pass" + }, + { + "page": "systems--schema-and-json.md", + "kind": "source-reference", + "target": "src/infrastructure/fs/strict-json-object.ts", + "status": "pass" + }, + { + "page": "systems--skill-distribution.md", + "label": "README install section", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/README.md#install", + "kind": "local-canonical", + "target": "README.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": "install", + "anchorExists": true, + "status": "pass", + "targetSha256": "1407ad086e757bf76240327011e3738bc21e9fb31d98c83eeb46b5500c7ad1df" + }, + { + "page": "systems--skill-distribution.md", + "label": "troubleshooting", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/troubleshooting.md#flow-commands-are-missing", + "kind": "local-canonical", + "target": "docs/troubleshooting.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": "flow-commands-are-missing", + "anchorExists": true, + "status": "pass", + "targetSha256": "23b055b14e58d5144c6e5aa209d166924aaa28abffbf6ff1ca374cc747073978" + }, + { + "page": "systems--skill-distribution.md", + "label": "distribution contract", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/maintainer-contract.md#distribution-and-release", + "kind": "local-canonical", + "target": "docs/maintainer-contract.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": "distribution-and-release", + "anchorExists": true, + "status": "pass", + "targetSha256": "258604b8354eef1530da63f6c223115608356d3adbd42a49c0b4b0bcf418ae90" + }, + { + "page": "systems--skill-distribution.md", + "label": "Bundled guidance", + "url": "features--managed-skills", + "kind": "wiki", + "target": "features--managed-skills.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "cb63e876113d57fbed0e2a27c9f035615d5b5546f4cf77839c3f033ad94e3a76" + }, + { + "page": "systems--skill-distribution.md", + "label": "Package installation", + "url": "systems--cli-and-package", + "kind": "wiki", + "target": "systems--cli-and-package.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "3ce0128dce798af5e93d51949279f502e225f1663df00eeea41cb3401a3dee30" + }, + { + "page": "systems--skill-distribution.md", + "kind": "source-reference", + "target": "src/guidance/catalog.ts", + "status": "pass" + }, + { + "page": "systems--skill-distribution.md", + "kind": "source-reference", + "target": "skills/", + "status": "pass" + }, + { + "page": "systems--workspace-persistence.md", + "label": "persistence invariants", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/maintainer-contract.md#persistence", + "kind": "local-canonical", + "target": "docs/maintainer-contract.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": "persistence", + "anchorExists": true, + "status": "pass", + "targetSha256": "258604b8354eef1530da63f6c223115608356d3adbd42a49c0b4b0bcf418ae90" + }, + { + "page": "systems--workspace-persistence.md", + "label": "archive recovery", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/troubleshooting.md#status-shows-a-closed-session-still-awaiting-archive-publication", + "kind": "local-canonical", + "target": "docs/troubleshooting.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": "status-shows-a-closed-session-still-awaiting-archive-publication", + "anchorExists": true, + "status": "pass", + "targetSha256": "23b055b14e58d5144c6e5aa209d166924aaa28abffbf6ff1ca374cc747073978" + }, + { + "page": "systems--workspace-persistence.md", + "label": "Security", + "url": "security", + "kind": "wiki", + "target": "security.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "315d90606179112d193cb40660b8dc37a560f00f45e5d58b2e9d157e26aff97e" + }, + { + "page": "systems--workspace-persistence.md", + "label": "Schema and JSON", + "url": "systems--schema-and-json", + "kind": "wiki", + "target": "systems--schema-and-json.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "c4407449d138a5b36547f4427321112488dae36235c94e3931a60d381499c192" + }, + { + "page": "systems--workspace-persistence.md", + "kind": "source-reference", + "target": "src/infrastructure/fs/", + "status": "pass" + }, + { + "page": "systems.md", + "label": "development source layout", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/development.md#source-layout", + "kind": "local-canonical", + "target": "docs/development.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": "source-layout", + "anchorExists": true, + "status": "pass", + "targetSha256": "53ab971982e4675a5716542b20bf8176fd3aeaa23ef4d7d4f82bb248ec86cfea" + }, + { + "page": "systems.md", + "label": "dependency rules", + "url": "https://github.com/ddv1982/flow-opencode/blob/main/docs/architecture/allowed-cross-layer-dependencies.md", + "kind": "local-canonical", + "target": "docs/architecture/allowed-cross-layer-dependencies.md", + "publicReadback": "pending after verified release", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "a5ee81d7238d25944cf7c87348bc4dc619f7e7df601e24dc0ffc0c152e99b49b" + }, + { + "page": "systems.md", + "label": "OpenCode integration", + "url": "systems--opencode-adapter", + "kind": "wiki", + "target": "systems--opencode-adapter.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "ae3355ef3ec9675068e3e1c0c07aaa1eeab2ccfe86e1e85aac10f76203c5c4cd" + }, + { + "page": "systems.md", + "label": "State machine", + "url": "systems--runtime-state-machine", + "kind": "wiki", + "target": "systems--runtime-state-machine.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "6251f7a71983e5db75f40c1d4e29b1b4dad5cb7f39d6be351783e4018acc437b" + }, + { + "page": "systems.md", + "label": "Workspace persistence", + "url": "systems--workspace-persistence", + "kind": "wiki", + "target": "systems--workspace-persistence.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "67dba175263baf929734367e08e998bb0c47f231d5e96bc1679de34601d4dadb" + }, + { + "page": "systems.md", + "label": "Schema and JSON", + "url": "systems--schema-and-json", + "kind": "wiki", + "target": "systems--schema-and-json.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "c4407449d138a5b36547f4427321112488dae36235c94e3931a60d381499c192" + }, + { + "page": "systems.md", + "label": "Guidance distribution", + "url": "systems--skill-distribution", + "kind": "wiki", + "target": "systems--skill-distribution.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "678962124cb5be759e03a311916fac5d086cc799ad7754c4828a03353f8282d2" + }, + { + "page": "systems.md", + "label": "Package installation", + "url": "systems--cli-and-package", + "kind": "wiki", + "target": "systems--cli-and-package.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "3ce0128dce798af5e93d51949279f502e225f1663df00eeea41cb3401a3dee30" + }, + { + "page": "systems.md", + "label": "Source map", + "url": "reference--source-map", + "kind": "wiki", + "target": "reference--source-map.md", + "exists": true, + "anchor": null, + "anchorExists": true, + "status": "pass", + "targetSha256": "9d6ac595ff746200f8ac3e237e7f10d66d4cf60571e3abe44d5053983dca888b" + } + ], + "publicReadbackGate": "After verified Flow 10.0.0 release, parent confirms public main README/docs match the released source and all linked paths/anchors. After parent publication, read back all 44 content pages and rendered sidebar navigation. This local verification does not satisfy that gate." +} diff --git a/.agents/plans/18-opencode-v2/wiki/parent-update.md b/.agents/plans/18-opencode-v2/wiki/parent-update.md new file mode 100644 index 00000000..4591eb6d --- /dev/null +++ b/.agents/plans/18-opencode-v2/wiki/parent-update.md @@ -0,0 +1,26 @@ +# Final configuration update + +The parent added `options.typesafeApiKeyEnv` to the existing configuration page after the original local wiki audit. The example contains only the variable name. The text requires the key in the OpenCode server environment and explains that custom names never fall back. + +The original audit remains in `/tmp/flow-v2-wiki-draft`. The final local inventory and patch were regenerated in `/tmp/flow-v2-wiki-final-draft`. All 293 checks across 45 pages passed again. The configuration source references now include `src/platform/opencode/typesafe-key-env.ts`. + +The four refreshed artifacts in this directory supersede their earlier local draft versions. The original review summary describes the earlier audit. No wiki commit or publication occurred. The public readback gate remains open and runs only after the verified release. + + +## Patch storage + +Commit preflight rejects blank context lines in a new patch file as trailing +whitespace. The parent moved the patch unchanged to `/tmp/flow-v2-wiki-reviewed-patch/wiki.patch`. Its SHA-256 is +`25016b5d92944d703e45380a27ea184548cd7a7c776c07f4b4424cb0923d12ce`. It matches the final local wiki draft. The separate wiki +checkout remains the publication candidate. The main repository retains review +and link-verification records, with no duplicate wiki content patch. Earlier +references to the plan-local `wiki.patch` are superseded by this location. + + +## Parent local readback + +The parent read all 45 revised pages and compared their v2 support, tool/command, +permission, configuration, evidence, and release claims with maintained source +and documentation. The current local checker passes all 293 checks in +`/tmp/flow-v2-wiki-r8-local/linkverification.json`. No new wiki change was needed. +Public checks remain gated on the verified release and later wiki publication. diff --git a/.agents/plans/18-opencode-v2/wiki/public-readback-gate.json b/.agents/plans/18-opencode-v2/wiki/public-readback-gate.json new file mode 100644 index 00000000..383253a0 --- /dev/null +++ b/.agents/plans/18-opencode-v2/wiki/public-readback-gate.json @@ -0,0 +1,71 @@ +{ + "status": "NOT VERIFIED", + "owner": "Parent", + "releaseGate": "Verify Flow 10.0.0 through the existing qualification and publication process before publishing this wiki diff. This task ran no release or paid campaign.", + "canonicalReadback": { + "urls": [ + "https://github.com/ddv1982/flow-opencode/blob/main/README.md", + "https://github.com/ddv1982/flow-opencode/blob/main/README.md#commands", + "https://github.com/ddv1982/flow-opencode/blob/main/README.md#how-flow-works", + "https://github.com/ddv1982/flow-opencode/blob/main/README.md#install", + "https://github.com/ddv1982/flow-opencode/blob/main/README.md#recovery-advice-development-preview", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/adr/0001-skills-first-flow-architecture.md", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/adr/0005-flow-v6-session-v5-simplicity-first.md", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/adr/0005-flow-v6-session-v5-simplicity-first.md#guardrail", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/adr/0006-bounded-intra-feature-waves.md", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/adr/0014-one-evidence-record.md", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/adr/0017-inspection-gate-outcome.md", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/adr/0018-opencode-v2-only.md", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/architecture/allowed-cross-layer-dependencies.md", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/automatic-continuation.md", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/development.md", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/development.md#bumping-the-pinned-host", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/development.md#change-discipline", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/development.md#local-setup", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/development.md#model-driven-auto-continuation-evidence", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/development.md#model-driven-wave-evidence", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/development.md#release", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/development.md#source-layout", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/guarantees.md", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/guarantees.md#assurance-at-close", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/guarantees.md#terminal-recovery-and-inspection-outcomes", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/guarantees.md#threat-model", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/index.md", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/maintainer-contract.md", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/maintainer-contract.md#bounded-worker-waves", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/maintainer-contract.md#causality-and-idempotency", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/maintainer-contract.md#commands", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/maintainer-contract.md#distribution-and-release", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/maintainer-contract.md#guides", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/maintainer-contract.md#opencode-surface", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/maintainer-contract.md#persistence", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/maintainer-contract.md#session-v5", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/maintainer-contract.md#tools", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/quickstart.md", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/quickstart.md#select-flow-models", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/release-qualification.md", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/release-qualification.md#running-it", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/troubleshooting.md", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/troubleshooting.md#flow-commands-are-missing", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/troubleshooting.md#flow-reports-that-the-project-lock-is-busy", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/troubleshooting.md#session-state-is-unreadable", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/troubleshooting.md#status-shows-a-closed-session-still-awaiting-archive-publication", + "https://github.com/ddv1982/flow-opencode/blob/main/docs/validation-and-review.md" + ], + "requiredFacts": [ + "The released Flow version and public README install pin are 10.0.0.", + "OpenCode 2.1.4 is the tested minimum. Later 2.x requires a successful compatibility check. OpenCode 1 is unsupported and OpenCode 3 is outside the peer range.", + "Native plugins configuration installs Flow once and the server loads the terminal picker automatically.", + "Project role preferences override global picker choices; reset restores base role behavior.", + "The canonical docs and source match the final released contract. Every listed URL and anchor resolves publicly." + ] + }, + "wikiPublication": "Parent reviews actual diff from base de4be508451c25564fc4bfb4911fa056d10ce3f3. Resolve any newer wiki changes before publishing.", + "wikiReadback": { + "contentPages": 44, + "urlSource": "inventory.json pages with non-null wikiURL", + "navigation": "Read the rendered _Sidebar on Home and content pages. Check all 44 page links.", + "required": "Read all published content against the reviewed local Markdown and confirm every canonical and wiki link works. Check object-form options, native install once, model precedence, upgrade closure, bounded security claims, and historical labels." + }, + "doesLocalVerificationSatisfyGate": false +} diff --git a/.agents/plans/18-opencode-v2/wiki/review-summary.md b/.agents/plans/18-opencode-v2/wiki/review-summary.md new file mode 100644 index 00000000..917975b7 --- /dev/null +++ b/.agents/plans/18-opencode-v2/wiki/review-summary.md @@ -0,0 +1,42 @@ +# Local wiki review + +PASS for the requested local draft. Publication remains held. Public readback is NOT VERIFIED. + +The wiki base is `de4be508451c25564fc4bfb4911fa056d10ce3f3`. Its initial worktree was clean. All 45 Markdown files were read before editing. All original filenames remain, comprising 44 content pages and `_Sidebar.md`. All 45 revised files were then read back in full. + +The final diff changes 45 files with 354 insertions and 1741 deletions. `wiki.patch` is the concrete review artifact. No changes were written to the main repository. No commits, pushes, network requests, external writes, credentials, delegates, or model calls were used. + +The draft makes repository docs authoritative. Home and the sidebar lead to v2 setup and current contracts. Removed skill-copy installation, installer CLI instructions, old source paths, V1 configuration, old tool counts, manager-proxy review, two-pass final review, and age-based lock ownership are gone. Former installer page URLs now explain embedded guidance and native package loading. Security limits remain explicit. Selected origin commits and July metrics remain labeled history. The absent overview video and captions were removed. + +Model precedence was traced through application preference types and schemas, native picker resolution, server preference updates, the store, and reviewer defaults. Project role preferences override global picker choices. An absent project role inherits. Default resets to base role behavior. A selected reviewer model clears its variant and retains configured steps. Base plugin options override environment defaults. The same server package loads the picker, and native `/models` owns coding selection. + +`inventory.json` maps each old page to canonical targets and a distinct edit reason. It records old and new hashes, original headings, line counts, and additional source evidence. `baseline.json` records every original page and source reference. `source-census.json` hashes 118 current README, documentation, source, workflow, and guidance files. It is a census, not a claim that every TypeScript implementation was read in full. The requested canonical docs, all current ADRs through 0018, and relevant implementation paths were read. + +The sealing check detected concurrent changes to execution attribution and lifecycle source. Their affected identity, epoch, context, compaction, and revocation paths were rechecked. The wiki summaries still match. `source-drift.json` records that review and `source-recheck.json` captures the later hashes. Other writers remain active, so these local hashes are not a frozen release identity. + +## Verification + +- `python3 /tmp/flow-v2-wiki-draft/verify.py` passes all 293 checks across all 45 files. These include 124 canonical link occurrences, 126 wiki links, 35 source references, and 8 historical commit links. Canonical files and heading anchors are checked locally. Commit links are checked against local Git objects. +- `git -C /tmp/flow-opencode-v2-plan-wiki diff --check` passes. +- The sidebar links all 44 content pages. No Markdown files were added or deleted. +- The single public configuration example parses as JSON and uses `plugins` with a package and object options. There is no duplicate terminal installation entry. +- No long dash, prose colon connector, retired source-tree reference, installer invocation, or obsolete active-schema field remains in the draft. +- Application tests and release gates were not run. This is a local documentation diff, and the main checkout is read-only for this task. + +The smallest rerunnable proof is `verify.py`. It reads the actual wiki and local canonical targets. It also detects stale inventory hashes. There is no new sync system, framework, or page-generation template. + +## Principle decisions + +Build the Lever produced the standalone link checker. Sequence Work into Verifiable Units made each page group end with a link check before the next group. Prove It Works required actual changed-file readback and diff checks. Outcome-Oriented Execution removed retired operating instructions rather than preserving a compatibility manual. Model the Domain kept the audit as one record per page. Laziness Protocol kept the verification script local and small and the wiki shorter than the source manual. + +Technical Writing kept each page in an overview or reference role and linked detailed procedures. Unslop removed generated filler, decorative headings, long dashes, and prose colon connectors. + +## Remaining public readback gate + +Only the parent publishes, after the verified Flow 10.0.0 release through the usual process. Before publication, the parent must confirm that the public README install pin and all 47 distinct canonical URLs and anchors in `public-readback-gate.json` resolve and match the final released source. Recheck any source or documentation changes made after this local census. This task supplies no release verdict. + +After reviewing and publishing the actual wiki diff, the parent must read back all 44 public content pages listed in `inventory.json`, plus the rendered sidebar. Confirm the published content matches the reviewed draft and the page, navigation, and canonical links work. Public readback must confirm v2-only support, the tested minimum, install once, model precedence, close-before-upgrade guidance, security limits, and historical labels. Local target checks do not satisfy this gate. + +## Attention + +Independent reviewer and cross-model review were not run because the user prohibited delegates and models. No independent verdict is claimed. Parent review of `wiki.patch` and the public readback gate remain outstanding by design. The local decision trail is `decisions.tsv` and was checked against the active conversation and live files. diff --git a/.agents/plans/18-opencode-v2/wiki/verify.py b/.agents/plans/18-opencode-v2/wiki/verify.py new file mode 100644 index 00000000..2c9d7472 --- /dev/null +++ b/.agents/plans/18-opencode-v2/wiki/verify.py @@ -0,0 +1,87 @@ +from pathlib import Path +import hashlib +import json +import re +import subprocess +import sys +import unicodedata +from urllib.parse import unquote, urlsplit + +wiki = Path('/tmp/flow-opencode-v2-plan-wiki') +repo = Path('/Users/vriesd/projects/flow-opencode') +evidence = Path('/tmp/flow-v2-wiki-draft') +baseline = json.loads((evidence / 'baseline.json').read_text()) +expected = {p['page'] for p in baseline['pages']} +actual = {p.name for p in wiki.glob('*.md')} +assert actual == expected, (expected - actual, actual - expected) +assert subprocess.check_output(['git', '-C', str(wiki), 'rev-parse', 'HEAD'], text=True).strip() == baseline['baseSHA'] +checks = [] +issues = [] +selected = set(sys.argv[1:]) or actual +assert selected <= actual +for page in sorted(selected): + body = (wiki / page).read_text() + for label, raw in re.findall(r'\[([^\]]+)\]\(([^\s)]+)\)', body): + parsed = urlsplit(raw) + fragment = unquote(parsed.fragment) + path = unquote(parsed.path) + record = {'page': page, 'label': label, 'url': raw} + if parsed.scheme: + prefix = '/ddv1982/flow-opencode/blob/main/' + if parsed.netloc == 'github.com' and path.startswith(prefix): + relative = path[len(prefix):] + target = repo / relative + record.update(kind='local-canonical', target=relative, publicReadback='pending after verified release') + elif parsed.netloc == 'github.com' and path.startswith('/ddv1982/flow-opencode/commit/'): + commit = path.rsplit('/', 1)[1] + result = subprocess.run(['git', '-C', str(repo), 'cat-file', '-e', commit + '^{commit}'], capture_output=True) + record.update(kind='historical-commit', status='pass' if result.returncode == 0 else 'fail') + checks.append(record) + if result.returncode: issues.append(record) + continue + else: + record.update(kind='external-unverified', status='unverified') + checks.append(record) + issues.append(record) + continue + else: + name = path or Path(page).stem + target = wiki / (name if name.endswith('.md') else name + '.md') + record.update(kind='wiki', target=target.name) + exists = target.is_file() + anchor_ok = True + if exists and fragment: + anchors = [] + counts = {} + for heading in re.findall(r'^#{1,6}\s+(.+?)\s*#*$', target.read_text(), re.M): + heading = re.sub(r'\[([^\]]+)\]\([^)]*\)', r'\1', heading) + slug = ''.join(c for c in heading.lower() if c in ' -_' or unicodedata.category(c)[0] in 'LN') + slug = slug.replace(' ', '-') + number = counts.get(slug, 0) + counts[slug] = number + 1 + anchors.append(slug + (f'-{number}' if number else '')) + anchor_ok = fragment in anchors + record.update(exists=exists, anchor=fragment or None, anchorExists=anchor_ok, status='pass' if exists and anchor_ok else 'fail') + if exists: record['targetSha256'] = hashlib.sha256(target.read_bytes()).hexdigest() + checks.append(record) + if record['status'] != 'pass': issues.append(record) + for code in re.findall(r'`([^`\n]+)`', body): + if re.match(r'^(src|skills|tests|docs)/', code) and not any(c in code for c in '* <>|'): + record = {'page': page, 'kind': 'source-reference', 'target': code, 'status': 'pass' if (repo / code.rstrip('/')).exists() else 'fail'} + checks.append(record) + if record['status'] != 'pass': issues.append(record) + banned = re.findall(r'[\u2013\u2014]|src/runtime/|src/adapters/|src/distribution/|src/cli\.ts|npx\s|finalReviewPolicy|FLOW_EXPERIMENTAL_COMPACTION|opencode-instructions\.md', body) + if banned: issues.append({'page': page, 'kind': 'retired-instruction-or-style', 'matches': banned}) +inventory_file = evidence / 'inventory.json' +if inventory_file.exists(): + inventory = json.loads(inventory_file.read_text()) + if {p['oldPage'] for p in inventory['pages']} != actual: + issues.append({'kind': 'inventory-coverage', 'status': 'fail'}) + for item in inventory['pages']: + digest = hashlib.sha256((wiki / item['oldPage']).read_bytes()).hexdigest() + if digest != item['updatedSha256']: + issues.append({'kind': 'inventory-stale', 'page': item['oldPage'], 'status': 'fail'}) +result = {'baseSHA': baseline['baseSHA'], 'pageCount': len(actual), 'checkedPages': sorted(selected), 'verificationScope': 'Local file and anchor resolution only. No network requests.', 'status': 'PASS' if not issues else 'ISSUES', 'checkCount': len(checks), 'issues': issues, 'checks': checks, 'publicReadbackGate': 'After verified Flow 10.0.0 release, parent confirms public main README/docs match the released source and all linked paths/anchors. After parent publication, read back all 44 content pages and rendered sidebar navigation. This local verification does not satisfy that gate.'} +(evidence / 'linkverification.json').write_text(json.dumps(result, indent=2) + '\n') +print(json.dumps({k: result[k] for k in ['status', 'pageCount', 'checkCount', 'issues']}, indent=2)) +raise SystemExit(bool(issues)) diff --git a/.github/dependabot.yml b/.github/dependabot.yml index 3a1162eb..1c7e6098 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -12,4 +12,6 @@ updates: # Deliberately pinned; bump manually with package.json and the focused # compatibility checks described in docs/development.md. - dependency-name: zod - - dependency-name: "@opencode-ai/plugin" + - dependency-name: "@opencode/plugin" + - dependency-name: "@opencode/client" + - dependency-name: "@opencode/schema" diff --git a/.github/workflows/evals.yml b/.github/workflows/evals.yml index 2c458856..d16e4efe 100644 --- a/.github/workflows/evals.yml +++ b/.github/workflows/evals.yml @@ -120,7 +120,6 @@ jobs: OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} XAI_API_KEY: ${{ secrets.XAI_API_KEY }} FLOW_EVAL_MODEL: ${{ steps.matrix.outputs.models }} - FLOW_EVAL_NO_AUTH_COPY: "1" FLOW_EVAL_AUTHORIZATION: ${{ github.workspace }}/.paid-budget # A failing scenario must still publish its report: the durable documents are # the only way to read why, and paying for the pass twice is the alternative. diff --git a/.github/workflows/opencode-compatibility.yml b/.github/workflows/opencode-compatibility.yml index 93375d61..836a6d5b 100644 --- a/.github/workflows/opencode-compatibility.yml +++ b/.github/workflows/opencode-compatibility.yml @@ -8,8 +8,6 @@ on: permissions: contents: read -# This non-blocking scheduled signal catches upcoming OpenCode drift without -# making an unreviewed host release part of the reproducible v6 build. jobs: latest-opencode: runs-on: ubuntu-latest @@ -30,7 +28,24 @@ jobs: - name: Install dependencies run: bun install --frozen-lockfile + - name: Resolve the latest OpenCode 2 release + run: | + node --input-type=module <<'JS' + import { execFileSync } from 'node:child_process'; + import { appendFileSync } from 'node:fs'; + const result = JSON.parse(execFileSync('npm', ['view', '@opencode/cli@2', 'version', '--json'], { encoding: 'utf8' })); + const versions = (Array.isArray(result) ? result : [result]) + .filter((version) => /^2\.\d+\.\d+$/.test(version)) + .sort((a, b) => { + const left = a.split('.').map(Number); + const right = b.split('.').map(Number); + return left[1] - right[1] || left[2] - right[2]; + }); + const version = versions.at(-1); + if (!version) throw new Error('No stable OpenCode 2 release resolved.'); + appendFileSync(process.env.GITHUB_ENV, `FLOW_OPENCODE_SMOKE_VERSION=${version}\n`); + console.log(`Compatibility host: @opencode/cli@${version}`); + JS + - name: Run live OpenCode smoke - env: - FLOW_OPENCODE_SMOKE_VERSION: latest run: bun run smoke:live diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 630ebcdc..9dd01904 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -129,6 +129,11 @@ jobs: "opencode-plugin-flow-${version}.tgz" "$sealed" "$digest" fi fi + if node -e 'process.exit(Number(process.argv[1].split(".")[0]) >= 10 ? 0 : 1)' "$version" && [[ -f "evals/qualification/archives/${version}.json" ]]; then + qualified="$(bun scripts/materialize-qualification.ts --descriptor "evals/qualification/archives/${version}.json" --out "${RUNNER_TEMP}/flow-qualified-${version}")" + bun run scripts/restore-exact-release-artifact.ts \ + "opencode-plugin-flow-${version}.tgz" --bundle "$qualified" "$version" + fi - name: Report release evidence readiness without publishing timeout-minutes: 5 @@ -238,6 +243,11 @@ jobs: "opencode-plugin-flow-${version}.tgz" "$sealed" "$digest" fi fi + if node -e 'process.exit(Number(process.argv[1].split(".")[0]) >= 10 ? 0 : 1)' "$version"; then + qualified="$(bun scripts/materialize-qualification.ts --descriptor "evals/qualification/archives/${version}.json" --out "${RUNNER_TEMP}/flow-qualified-${version}")" + bun run scripts/restore-exact-release-artifact.ts \ + "opencode-plugin-flow-${version}.tgz" --bundle "$qualified" "$version" + fi - name: Restore release record from this workflow run id: restore diff --git a/CHANGELOG.md b/CHANGELOG.md index 12bf8677..000a15e7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,29 @@ One short entry per release, written for users deciding whether to upgrade. +## [10.0.0] + +- Flow now targets OpenCode v2, with 2.1.4 as its tested minimum. OpenCode 1 + support is removed. Finish or explicitly close active Flow sessions before + upgrading, then restart OpenCode. +- Server registration, commands, tools, permissions, validation capture, and + automatic continuation use the native v2 APIs. Continuation binds tool calls + to the inputs consumed by their model request and preserves authority through + verified automatic compaction. +- The terminal model picker loads with the server plugin. Install Flow once + through `opencode plugin add opencode-plugin-flow@10.0.0` or the `plugins` + configuration array. Plugin options use object form. +- Set `options.typesafeApiKeyEnv` to choose the server environment variable + holding your Typesafe key. It defaults to `TYPESAFE_API_KEY`. A custom name + never falls back to the default. +- Session v5 schema remains unchanged. Historical qualification evidence retains its + original host policy. The new release profile requires all 17 cases on two + provider routes and a fresh exact-artifact canary. +- Zod is pinned to 4.6.7, including the fix for + [oversized collection validation](https://github.com/advisories/GHSA-mrhx-c7xc-m3w5). + +Upgrade with `opencode plugin add opencode-plugin-flow@10.0.0`. + ## [9.6.0] - Short delivery handoffs retain goal, closure, progress, assurance, authority, diff --git a/CONTEXT.md b/CONTEXT.md index 3f94c7d7..1090bffd 100644 --- a/CONTEXT.md +++ b/CONTEXT.md @@ -53,7 +53,8 @@ projection. It repeats the exact next action and recovery direction without adding persisted state or authority. **Reviewer configuration**: A process-local model and step selection resolved -once when the plugin loads. Native tuple options override environment fallbacks. +when the plugin loads or model preferences change. Project model preferences +override global picker choices. Plugin object options override environment fallbacks. Status reports the requested selection but cannot prove model availability. **Bounded wave**: An optional ephemeral cohort of two or three `flow-worker` @@ -137,7 +138,7 @@ The root manager owns slice selection, integration, evidence acceptance, authoritative validation, review dispatch, reset, closure, and every lifecycle mutation except reviewer-result submission. It may delegate exact disjoint contributions to `flow-worker`, then audits assigned versus changed paths. -Workers cannot run Bash, edit `.flow` or `.git` metadata paths, use Flow tools, +Workers cannot run Shell, edit `.flow` or `.git` metadata paths, use Flow tools, delegate again, or approve work. `flow-reviewer` remains independent and workspace-read-only; its sole lifecycle mutation is submitting its own pending result. Status may quarantine unreadable Flow state as fail-closed recovery diff --git a/README.md b/README.md index 5913b60f..4f5034e0 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,9 @@ -# Flow Plugin for OpenCode +# Flow Plugin for OpenCode v2 -`opencode-plugin-flow` gives OpenCode a durable workflow for coding work that +`opencode-plugin-flow` requires OpenCode 2.1.4. Later 2.x releases are supported +after passing the compatibility check. OpenCode 1.x is unsupported. + +Flow gives OpenCode a durable workflow for coding work that benefits from an approved plan and an independent review: ```text @@ -35,49 +38,62 @@ change is expensive and you read the review. ## Install -Install the exact npm release through OpenCode: +Install OpenCode v2 and the exact Flow release: -```bash -opencode plugin opencode-plugin-flow@9.6.0 --global --force +```sh +npm install -g @opencode/cli@2.1.4 +opencode2 --version +opencode2 plugin add opencode-plugin-flow@10.0.0 ``` -Omit `--global` for project scope. Version pins are exact and never update on -their own; to update, rerun the command with the new version. +The version command must report `opencode v2.1.4` or a later tested 2.x version. +Flow selects HTTP transport for provider, model, and variant settings while loaded. +This keeps model requests observable for continuation and validation attribution. +The v2 package provides both `opencode` and `opencode2`. Use `opencode2` when an +older `opencode` executable remains on your PATH. The plugin command installs +Flow globally. Exact pins stay fixed until you install another version. -Manual setup needs this entry in both `opencode.json` and `tui.json`: +For project scope, add this entry to the project's `opencode.json`: ```json { - "plugin": ["opencode-plugin-flow@9.6.0"] + "plugins": ["opencode-plugin-flow@10.0.0"] } ``` -`/flow-models` selects planning and review. Saving reloads the server; finish other projects first. -Project preferences take precedence. Selection makes no model call. +Install Flow once. Its server package loads the terminal picker automatically. +`/flow-models` selects planning and review without a model call. Finish active +work before saving. Project preferences take precedence over global choices. Planning defaults to the manager. A selected read-only specialist drafts advice. The manager checks and saves the plan, shows the model handoff, and owns approval and implementation. `/models` selects the coding model. -`opencode.json` reviewer options: +Set plugin defaults: ```json { - "plugin": [["opencode-plugin-flow@9.6.0", { - "reviewer": { "model": "provider/model", "steps": 80 } - }]] + "plugins": [{ + "package": "opencode-plugin-flow@10.0.0", + "options": { + "reviewer": { "model": "provider/model", "steps": 80 }, + "typesafeApiKeyEnv": "MY_TYPESAFE_KEY" + } + }] } ``` -Picker preferences take priority. Otherwise tuple settings override -`OPENCODE_FLOW_REVIEWER_MODEL`, -`OPENCODE_FLOW_REVIEWER_STEPS`, and `OPENCODE_FLOW_REVIEWER_VARIANT`. -`reviewer.variant` requires an explicit model. `/flow-status` shows requested -settings; only successful reviews confirm availability. +Global picker choices override reviewer defaults. Project `modelPreferences` +options override global choices. Choosing Use default restores the role's +configured behavior. Otherwise reviewer options override +`OPENCODE_FLOW_REVIEWER_MODEL`, `OPENCODE_FLOW_REVIEWER_STEPS`, and +`OPENCODE_FLOW_REVIEWER_VARIANT`. `reviewer.variant` requires an explicit model. +An explicit picker selection uses that model's default reasoning. +`/flow-status` shows requested settings. Successful review confirms availability. -[OpenCode](https://opencode.ai/docs/plugins/) owns installation and configuration. -Restart after manual changes. Remove the plugin entry to disable Flow; duplicate -copies fail closed until one remains. +[OpenCode v2](https://opencode.ai/v2/docs/plugins/) owns installation and +configuration. Restart after manual changes. Remove the plugin entry to disable +Flow. Duplicate copies fail closed until one remains. **Changing versions.** Finish or explicitly close any active session first, in either direction. Flow opens only Session v5 active state, and an older build @@ -100,9 +116,11 @@ validate against the real workspace, obtain an independent review — and keeps going through every runnable feature without handing back between them, until it can close the session. -Continuation between features needs a host that reports assistant message -parentage. Flow says so plainly when it detects a host that does not: the lifecycle -still works, one `/flow-run` at a time. +Automatic continuation binds each assistant's tools to the inputs its model +request consumed. Unattributed tools are refused. Interruption, source drift, or +conflicting input stops the affected lease. A stale unmatched callback cannot +stop a fresh invocation. Run `/flow-status` to inspect the pause, +then use `/flow-run` manually or `/flow-auto` within the existing approved scope. Send `/flow-auto stop` or `/flow-auto cancel` in the same OpenCode session to revoke only the in-process continuation. That does not close, defer, abandon, or @@ -212,8 +230,11 @@ and [bounded waves](docs/adr/0006-bounded-intra-feature-waves.md). ## Recovery advice development preview -With `TYPESAFE_API_KEY`, `/flow-auto` defaults to shadow advice, six attempts and -$0.02. Without the key or with `--recovery=off`, advice is off. +With a key, `/flow-auto` defaults to shadow advice, six attempts and $0.02. +`options.typesafeApiKeyEnv` names the server variable (default `TYPESAFE_API_KEY`). +Never put the key in options. Custom names never fall back. Missing/blank keys, +invalid names and `--recovery=off` disable advice. Restart the server after +environment changes if needed. ```text /flow-auto --recovery=shadow --recovery-calls=6 --recovery-usd=0.02 diff --git a/bun.lock b/bun.lock index 85f5eba1..bcdc03fc 100644 --- a/bun.lock +++ b/bun.lock @@ -6,17 +6,20 @@ "name": "opencode-plugin-flow", "dependencies": { "saxes": "6.0.0", - "zod": "4.4.3", + "zod": "4.6.7", }, "devDependencies": { "@biomejs/biome": "2.5.14", - "@opencode-ai/plugin": "1.18.31", + "@opencode/client": "2.1.4", + "@opencode/plugin": "2.1.4", + "@opencode/schema": "2.1.4", "@types/bun": "1.4.2", "@types/node": "26.6.2", "typescript": "7.0.2", }, "peerDependencies": { - "@opencode-ai/plugin": ">=1.18.3 <2", + "@opencode/plugin": ">=2.1.4 <3", + "@opencode/schema": ">=2.1.4 <3", }, }, }, @@ -26,6 +29,54 @@ "packages": { "@ai-sdk/provider": ["@ai-sdk/provider@3.0.8", "", { "dependencies": { "json-schema": "^0.4.0" } }, "sha512-oGMAgGoQdBXbZqNG0Ze56CHjDZ1IDYOwGYxYjO5KLSlz5HiNQ9udIXsPZ61VWaHGZ5XW/jyjmr6t2xz2jGVwbQ=="], + "@aws-crypto/crc32": ["@aws-crypto/crc32@5.2.0", "", { "dependencies": { "@aws-crypto/util": "^5.2.0", "@aws-sdk/types": "^3.222.0", "tslib": "^2.6.2" } }, "sha512-nLbCWqQNgUiwwtFsen1AdzAtvuLRsQS8rYgMuxCrdKf9kOssamGLuPwyTY9wyYblNr9+1XM8v6zoDTPPSIeANg=="], + + "@aws-crypto/sha256-browser": ["@aws-crypto/sha256-browser@5.2.0", "", { "dependencies": { "@aws-crypto/sha256-js": "^5.2.0", "@aws-crypto/supports-web-crypto": "^5.2.0", "@aws-crypto/util": "^5.2.0", "@aws-sdk/types": "^3.222.0", "@aws-sdk/util-locate-window": "^3.0.0", "@smithy/util-utf8": "^2.0.0", "tslib": "^2.6.2" } }, "sha512-AXfN/lGotSQwu6HNcEsIASo7kWXZ5HYWvfOmSNKDsEqC4OashTp8alTmaz+F7TC2L083SFv5RdB+qU3Vs1kZqw=="], + + "@aws-crypto/sha256-js": ["@aws-crypto/sha256-js@5.2.0", "", { "dependencies": { "@aws-crypto/util": "^5.2.0", "@aws-sdk/types": "^3.222.0", "tslib": "^2.6.2" } }, "sha512-FFQQyu7edu4ufvIZ+OadFpHHOt+eSTBaYaki44c+akjg7qZg9oOQeLlk77F6tSYqjDAFClrHJk9tMf0HdVyOvA=="], + + "@aws-crypto/supports-web-crypto": ["@aws-crypto/supports-web-crypto@5.2.0", "", { "dependencies": { "tslib": "^2.6.2" } }, "sha512-iAvUotm021kM33eCdNfwIN//F77/IADDSs58i+MDaOqFrVjZo9bAal0NK7HurRuWLLpF1iLX7gbWrjHjeo+YFg=="], + + "@aws-crypto/util": ["@aws-crypto/util@5.2.0", "", { "dependencies": { "@aws-sdk/types": "^3.222.0", "@smithy/util-utf8": "^2.0.0", "tslib": "^2.6.2" } }, "sha512-4RkU9EsI6ZpBve5fseQlGNUWKMa1RLPQ1dnjnQoe07ldfIzcsGb5hC5W0Dm7u423KWzawlrpbjXBrXCEv9zazQ=="], + + "@aws-sdk/client-cognito-identity": ["@aws-sdk/client-cognito-identity@3.1057.0", "", { "dependencies": { "@aws-crypto/sha256-browser": "5.2.0", "@aws-crypto/sha256-js": "5.2.0", "@aws-sdk/core": "^3.974.15", "@aws-sdk/credential-provider-node": "^3.972.47", "@aws-sdk/types": "^3.973.9", "@smithy/core": "^3.24.5", "@smithy/fetch-http-handler": "^5.4.5", "@smithy/node-http-handler": "^4.7.5", "@smithy/types": "^4.14.2", "tslib": "^2.6.2" } }, "sha512-5MliYkp2u0+2arTp5fZIaxl+xmm90LEKv/VeSxhfNQW4t0fvWJrNO429/jchWQenNoDRrOGE59VfbuZUfwFujg=="], + + "@aws-sdk/core": ["@aws-sdk/core@3.978.5", "", { "dependencies": { "@aws-sdk/types": "^3.975.0", "@aws-sdk/xml-builder": "^3.972.41", "@aws/lambda-invoke-store": "^0.3.0", "@smithy/core": "^3.36.1", "@smithy/signature-v4": "^5.7.4", "@smithy/types": "^4.19.0", "bowser": "^2.11.0", "tslib": "^2.6.2" } }, "sha512-ruI0AWqcrfL20XDS6SwZIJFPOBZ6N0ADa9nLzWlBhxk1aoUjbUX3JkTR9vEmk7o35XpAHi68ahZ4T6RE2U9XyA=="], + + "@aws-sdk/credential-provider-cognito-identity": ["@aws-sdk/credential-provider-cognito-identity@3.972.75", "", { "dependencies": { "@aws-sdk/nested-clients": "^3.997.50", "@aws-sdk/types": "^3.975.0", "@smithy/core": "^3.36.1", "@smithy/types": "^4.19.0", "tslib": "^2.6.2" } }, "sha512-aJdScAJeeXC1cdMDCk8/3eDoHch8o0z+oxlqFALqBK2+hbtSz+hZQ9DhfarXJmva9+FM2gmC5PUJ8BB/HbAqXQ=="], + + "@aws-sdk/credential-provider-env": ["@aws-sdk/credential-provider-env@3.972.76", "", { "dependencies": { "@aws-sdk/core": "^3.978.5", "@aws-sdk/types": "^3.975.0", "@smithy/core": "^3.36.1", "@smithy/types": "^4.19.0", "tslib": "^2.6.2" } }, "sha512-YxmL1Ar7bV9ZkJvtv+lj+MF6le76NnZnbmgySyZuxpZaCv0/1lZ3KxKy6RIj1h9ui2hWnVjZxbmTMeO0y/NrQA=="], + + "@aws-sdk/credential-provider-http": ["@aws-sdk/credential-provider-http@3.972.78", "", { "dependencies": { "@aws-sdk/core": "^3.978.5", "@aws-sdk/types": "^3.975.0", "@smithy/core": "^3.36.1", "@smithy/fetch-http-handler": "^5.8.0", "@smithy/node-http-handler": "^4.12.1", "@smithy/types": "^4.19.0", "tslib": "^2.6.2" } }, "sha512-3VHknDRngoebsaqyT1iR75b5cBT2zfEtODK6yYmYltljkK1HEAQLs2FEd1IpWqAeOYdMGaG9wikngWLTufY7RA=="], + + "@aws-sdk/credential-provider-ini": ["@aws-sdk/credential-provider-ini@3.973.21", "", { "dependencies": { "@aws-sdk/core": "^3.978.5", "@aws-sdk/credential-provider-env": "^3.972.76", "@aws-sdk/credential-provider-http": "^3.972.78", "@aws-sdk/credential-provider-login": "^3.972.83", "@aws-sdk/credential-provider-process": "^3.972.76", "@aws-sdk/credential-provider-sso": "^3.973.20", "@aws-sdk/credential-provider-web-identity": "^3.972.82", "@aws-sdk/nested-clients": "^3.997.50", "@aws-sdk/types": "^3.975.0", "@smithy/core": "^3.36.1", "@smithy/credential-provider-imds": "^4.5.2", "@smithy/types": "^4.19.0", "tslib": "^2.6.2" } }, "sha512-aLx9hbCX6mGAyv4ZZd8UagJ4RASHVJw6BFDSIyuKmLkqNL6H47EjBPK6qaJ4B5TDV6mc+vMa0SJFYyo4mkvVQg=="], + + "@aws-sdk/credential-provider-login": ["@aws-sdk/credential-provider-login@3.972.83", "", { "dependencies": { "@aws-sdk/core": "^3.978.5", "@aws-sdk/nested-clients": "^3.997.50", "@aws-sdk/types": "^3.975.0", "@smithy/core": "^3.36.1", "@smithy/types": "^4.19.0", "tslib": "^2.6.2" } }, "sha512-9bJU6FjRacF4FK/QHf8Dyxhur7B1grRj8C1Yh4IkpoFW2exQflPdCv0VrbemLW9ZctLNle+AE7TETs9KNvuLYg=="], + + "@aws-sdk/credential-provider-node": ["@aws-sdk/credential-provider-node@3.972.88", "", { "dependencies": { "@aws-sdk/credential-provider-env": "^3.972.76", "@aws-sdk/credential-provider-http": "^3.972.78", "@aws-sdk/credential-provider-ini": "^3.973.21", "@aws-sdk/credential-provider-process": "^3.972.76", "@aws-sdk/credential-provider-sso": "^3.973.20", "@aws-sdk/credential-provider-web-identity": "^3.972.82", "@aws-sdk/types": "^3.975.0", "@smithy/core": "^3.36.1", "@smithy/credential-provider-imds": "^4.5.2", "@smithy/types": "^4.19.0", "tslib": "^2.6.2" } }, "sha512-AG0KID5qhQPzrFkh5b0Cws1Amytvzo0L0Xj6hMgJXEwD6ZXAqninDhSNAWJiN5zU4YVVZnuidnqwPwYJ04IamA=="], + + "@aws-sdk/credential-provider-process": ["@aws-sdk/credential-provider-process@3.972.76", "", { "dependencies": { "@aws-sdk/core": "^3.978.5", "@aws-sdk/types": "^3.975.0", "@smithy/core": "^3.36.1", "@smithy/types": "^4.19.0", "tslib": "^2.6.2" } }, "sha512-pdhKFTXv1zPYhh92MTlp0jVbLWvUEV9v+rziuEkoskGGQ2iJskxHTO38nA8UAbT/N4IM3n8OZJ9rIY5IBO15/A=="], + + "@aws-sdk/credential-provider-sso": ["@aws-sdk/credential-provider-sso@3.973.20", "", { "dependencies": { "@aws-sdk/core": "^3.978.5", "@aws-sdk/nested-clients": "^3.997.50", "@aws-sdk/token-providers": "3.1155.0", "@aws-sdk/types": "^3.975.0", "@smithy/core": "^3.36.1", "@smithy/types": "^4.19.0", "tslib": "^2.6.2" } }, "sha512-DlxrAB3MnjNLx/da7b89W+3DyJQYso/q7Ns15MsST2IV2nkAYQOYTUCaffNSjgrpMP99Dm2mV2jxzcbyF94ZfA=="], + + "@aws-sdk/credential-provider-web-identity": ["@aws-sdk/credential-provider-web-identity@3.972.82", "", { "dependencies": { "@aws-sdk/core": "^3.978.5", "@aws-sdk/nested-clients": "^3.997.50", "@aws-sdk/types": "^3.975.0", "@smithy/core": "^3.36.1", "@smithy/types": "^4.19.0", "tslib": "^2.6.2" } }, "sha512-LUo46IqPTJ4xKBsrLuoaxiv/D2GTBCcFDMD1y3jKIqPR/p7Rw14RJFZ0iRWftqZtyLPQX9JMGnSi3mBu4y/pNA=="], + + "@aws-sdk/credential-providers": ["@aws-sdk/credential-providers@3.1057.0", "", { "dependencies": { "@aws-sdk/client-cognito-identity": "3.1057.0", "@aws-sdk/core": "^3.974.15", "@aws-sdk/credential-provider-cognito-identity": "^3.972.38", "@aws-sdk/credential-provider-env": "^3.972.41", "@aws-sdk/credential-provider-http": "^3.972.43", "@aws-sdk/credential-provider-ini": "^3.972.46", "@aws-sdk/credential-provider-login": "^3.972.45", "@aws-sdk/credential-provider-node": "^3.972.47", "@aws-sdk/credential-provider-process": "^3.972.41", "@aws-sdk/credential-provider-sso": "^3.972.45", "@aws-sdk/credential-provider-web-identity": "^3.972.45", "@aws-sdk/nested-clients": "^3.997.13", "@aws-sdk/types": "^3.973.9", "@smithy/core": "^3.24.5", "@smithy/credential-provider-imds": "^4.3.6", "@smithy/types": "^4.14.2", "tslib": "^2.6.2" } }, "sha512-rbrEHtz11g0kxsSkYr3fx2HABNNblp4AhB2MgPvJHgYOWfJ2eBviU7Mvoaef0PW8QH6lbZDfJcnM7eKvtvz3sw=="], + + "@aws-sdk/nested-clients": ["@aws-sdk/nested-clients@3.997.50", "", { "dependencies": { "@aws-sdk/core": "^3.978.5", "@aws-sdk/signature-v4-multi-region": "^3.996.49", "@aws-sdk/types": "^3.975.0", "@smithy/core": "^3.36.1", "@smithy/fetch-http-handler": "^5.8.0", "@smithy/node-http-handler": "^4.12.1", "@smithy/types": "^4.19.0", "tslib": "^2.6.2" } }, "sha512-nKb8qu4QsnpZTq/6JIXc72mzuzfh7BK4sUQkpQznPec2z4W7Ob2xYkcM4i5e1yGTBnbJVeFv/0sf3jcz85hclA=="], + + "@aws-sdk/signature-v4-multi-region": ["@aws-sdk/signature-v4-multi-region@3.996.49", "", { "dependencies": { "@aws-sdk/types": "^3.975.0", "@smithy/signature-v4": "^5.7.4", "@smithy/types": "^4.19.0", "tslib": "^2.6.2" } }, "sha512-nvuAZNRrRRVCiARRKsYlQ0VnXiEQiq7vXPH+UA+LSo+kN3Djl9eo/V2r0udkw85PnsNshmv2Em1clld5M3j67w=="], + + "@aws-sdk/token-providers": ["@aws-sdk/token-providers@3.1155.0", "", { "dependencies": { "@aws-sdk/core": "^3.978.5", "@aws-sdk/nested-clients": "^3.997.50", "@aws-sdk/types": "^3.975.0", "@smithy/core": "^3.36.1", "@smithy/types": "^4.19.0", "tslib": "^2.6.2" } }, "sha512-iIcQnYLhWuq9qOLQbIWFrxF3PF0svVeZvHnUS4DUnO4zUFmFHIyGyjqrntXG+e/5PUgfavV0pYfDIKHxTzN2LA=="], + + "@aws-sdk/types": ["@aws-sdk/types@3.975.0", "", { "dependencies": { "@smithy/types": "^4.19.0", "tslib": "^2.6.2" } }, "sha512-WQlhs6juByzWOzj5fwxOx0mPJaEqMcG/sDpl3Id61B3XhKE63xY+0QWcnG+sWR/sCfprmreyUo1lM7IKZmHMzw=="], + + "@aws-sdk/util-locate-window": ["@aws-sdk/util-locate-window@3.965.10", "", { "dependencies": { "tslib": "^2.6.2" } }, "sha512-ycwH6Zd2GhuSqdXX9ihbCjeGTB6xOJs+O3+Jb8/zDG9978XU80qs75dfkPJRMNKe5MvBZPuNeFpd4JZKPoUF4g=="], + + "@aws-sdk/xml-builder": ["@aws-sdk/xml-builder@3.972.41", "", { "dependencies": { "@smithy/types": "^4.19.0", "tslib": "^2.6.2" } }, "sha512-ctjVSyCMegrWfXlx6VqzSBFI6UqmQ5ZlnfMhdLIiWmhoH8UAQxSCP5N3OpG7X3k4LnS7ou74C4mt20+bfTW2aQ=="], + + "@aws/lambda-invoke-store": ["@aws/lambda-invoke-store@0.3.0", "", {}, "sha512-sl4Bm6yiMNYrZKkqqDFWN0UfnWhlS8ivKxrYl+6t0gCLrqr8y3B2IqZZbFRkfaVVp7C/baApyh71P+LeE1A2sQ=="], + "@biomejs/biome": ["@biomejs/biome@2.5.14", "", { "optionalDependencies": { "@biomejs/cli-darwin-arm64": "2.5.14", "@biomejs/cli-darwin-x64": "2.5.14", "@biomejs/cli-linux-arm64": "2.5.14", "@biomejs/cli-linux-arm64-musl": "2.5.14", "@biomejs/cli-linux-x64": "2.5.14", "@biomejs/cli-linux-x64-musl": "2.5.14", "@biomejs/cli-win32-arm64": "2.5.14", "@biomejs/cli-win32-x64": "2.5.14" }, "bin": { "biome": "bin/biome" } }, "sha512-0FabLIjd4M/dm8VFI86RMaLLdgepzbdfiAL2R8cr7V81OYYrP1w7Z73KfAwPK5h9SrEBXTzNG2y+mBwPo9xRnw=="], "@biomejs/cli-darwin-arm64": ["@biomejs/cli-darwin-arm64@2.5.14", "", { "os": "darwin", "cpu": "arm64" }, "sha512-UnzaXO65L4tsZimFITFP2M121GyhDcWFrT3pL5ZJ5U4XcS/0L5VHYytVohdCf/gnDUFHgl2I9xnt5bV/J1kxHQ=="], @@ -44,28 +95,146 @@ "@biomejs/cli-win32-x64": ["@biomejs/cli-win32-x64@2.5.14", "", { "os": "win32", "cpu": "x64" }, "sha512-oJWmBhoHsnhUKIke+0gXDX0mltJrWHA1UyHsrTlXwX0TL64ilVZAo+TYZm97baecV0esBdpzy3k96q+09JaZUQ=="], - "@msgpackr-extract/msgpackr-extract-darwin-arm64": ["@msgpackr-extract/msgpackr-extract-darwin-arm64@3.0.4", "", { "os": "darwin", "cpu": "arm64" }, "sha512-LCkGo6JDfaBhgST7UpPWgNgLINpcpabaHfyz5OBx75nUYxBsaEPxjnyNjWpeb/xBup/682QnBfRBy2/LvPutZQ=="], + "@effect/opentelemetry": ["@effect/opentelemetry@4.0.1", "", { "peerDependencies": { "@opentelemetry/api": ">=1.9.0 <2.0.0", "@opentelemetry/api-logs": ">=0.203.0 <0.300.0", "@opentelemetry/resources": ">=2.0.0 <3.0.0", "@opentelemetry/sdk-logs": ">=0.203.0 <0.300.0", "@opentelemetry/sdk-metrics": ">=2.0.0 <3.0.0", "@opentelemetry/sdk-trace-base": ">=2.0.0 <3.0.0", "@opentelemetry/sdk-trace-node": ">=2.0.0 <3.0.0", "@opentelemetry/sdk-trace-web": ">=2.0.0 <3.0.0", "@opentelemetry/semantic-conventions": ">=1.33.0 <2.0.0", "effect": "^4.0.1" }, "optionalPeers": ["@opentelemetry/api", "@opentelemetry/api-logs", "@opentelemetry/resources", "@opentelemetry/sdk-logs", "@opentelemetry/sdk-metrics", "@opentelemetry/sdk-trace-base", "@opentelemetry/sdk-trace-node", "@opentelemetry/sdk-trace-web"] }, "sha512-MZ7M1a1dxMqTqeNwIGSECjO7RrReEgeEwHo8BnbOqP+8IfVpdiJi/iPCmkFVWlAxanMl4qhsq69e0Hkul9t+bA=="], + + "@effect/platform-node": ["@effect/platform-node@4.0.1", "", { "dependencies": { "@effect/platform-node-shared": "^4.0.1", "undici": "^8.11.2" }, "peerDependencies": { "effect": "^4.0.1", "redis": ">=5.0.0 <7.0.0" } }, "sha512-FT0Fu5LO0/XP9zBpj0HqrtJNLluK4StNKqf6TSulUsjp8FPBsDVXGJiDuyoN5J2yVMT7tgvhxSwaDcObWVRIaQ=="], + + "@effect/platform-node-shared": ["@effect/platform-node-shared@4.0.1", "", { "dependencies": { "@types/ws": "^8.18.1", "ws": "^8.22.0" }, "peerDependencies": { "effect": "^4.0.1" } }, "sha512-VanaejudEhKJ4mpmqnEzdncVUhN6sGSO9ZDND54mRxAlZxj+4dYAWjDTYAy9o4tJo9aALeP2U8cVcOFzEdSeLg=="], + + "@gar/promise-retry": ["@gar/promise-retry@1.0.3", "", {}, "sha512-GmzA9ckNokPypTg10pgpeHNQe7ph+iIKKmhKu3Ob9ANkswreCx7R3cKmY781K8QK3AqVL3xVh9A42JvIAbkkSA=="], + + "@isaacs/cliui": ["@isaacs/cliui@8.0.2", "", { "dependencies": { "string-width": "^5.1.2", "string-width-cjs": "npm:string-width@^4.2.0", "strip-ansi": "^7.0.1", "strip-ansi-cjs": "npm:strip-ansi@^6.0.1", "wrap-ansi": "^8.1.0", "wrap-ansi-cjs": "npm:wrap-ansi@^7.0.0" } }, "sha512-O8jcjabXaleOG9DQ0+ARXWZBTfnP4WNAqzuiJK7ll44AmxGKv/J2M4TPjxjY3znBCfvBXFzucm1twdyFybFqEA=="], + + "@isaacs/fs-minipass": ["@isaacs/fs-minipass@4.0.1", "", { "dependencies": { "minipass": "^7.0.4" } }, "sha512-wgm9Ehl2jpeqP3zw/7mo3kRHFp5MEDhqAdwy1fTGkHAwnkGOVsgpvQhL8B5n1qlb01jV3n/bI0ZfZp5lWA1k4w=="], + + "@isaacs/string-locale-compare": ["@isaacs/string-locale-compare@1.1.0", "", {}, "sha512-SQ7Kzhh9+D+ZW9MA0zkYv3VXhIDNx+LzM6EJ+/65I3QY+enU6Itte7E5XX7EWrqLW2FN4n06GWzBnPoC3th2aQ=="], + + "@npmcli/agent": ["@npmcli/agent@4.0.2", "", { "dependencies": { "agent-base": "^7.1.0", "http-proxy-agent": "^7.0.0", "https-proxy-agent": "^7.0.1", "lru-cache": "^11.2.1", "socks-proxy-agent": "^8.0.3" } }, "sha512-EUEuWAxnL07Sp5/iC/1X6Xj+XThUvnbei9zfRWZdEXa7lss9RTHMhAHBeg+MZ5To9s/gGaSI+UwZTPdYMvKSeg=="], + + "@npmcli/arborist": ["@npmcli/arborist@9.4.0", "", { "dependencies": { "@isaacs/string-locale-compare": "^1.1.0", "@npmcli/fs": "^5.0.0", "@npmcli/installed-package-contents": "^4.0.0", "@npmcli/map-workspaces": "^5.0.0", "@npmcli/metavuln-calculator": "^9.0.2", "@npmcli/name-from-folder": "^4.0.0", "@npmcli/node-gyp": "^5.0.0", "@npmcli/package-json": "^7.0.0", "@npmcli/query": "^5.0.0", "@npmcli/redact": "^4.0.0", "@npmcli/run-script": "^10.0.0", "bin-links": "^6.0.0", "cacache": "^20.0.1", "common-ancestor-path": "^2.0.0", "hosted-git-info": "^9.0.0", "json-stringify-nice": "^1.1.4", "lru-cache": "^11.2.1", "minimatch": "^10.0.3", "nopt": "^9.0.0", "npm-install-checks": "^8.0.0", "npm-package-arg": "^13.0.0", "npm-pick-manifest": "^11.0.1", "npm-registry-fetch": "^19.0.0", "pacote": "^21.0.2", "parse-conflict-json": "^5.0.1", "proc-log": "^6.0.0", "proggy": "^4.0.0", "promise-all-reject-late": "^1.0.0", "promise-call-limit": "^3.0.1", "semver": "^7.3.7", "ssri": "^13.0.0", "treeverse": "^3.0.0", "walk-up-path": "^4.0.0" }, "bin": { "arborist": "bin/index.js" } }, "sha512-4Bm8hNixJG/sii1PMnag0V9i/sGOX9VRzFrUiZMSBJpGlLR38f+Btl85d07G9GL56xO0l0OZjvrGNYsDYp0xKA=="], + + "@npmcli/config": ["@npmcli/config@10.8.1", "", { "dependencies": { "@npmcli/map-workspaces": "^5.0.0", "@npmcli/package-json": "^7.0.0", "ci-info": "^4.0.0", "ini": "^6.0.0", "nopt": "^9.0.0", "proc-log": "^6.0.0", "semver": "^7.3.5", "walk-up-path": "^4.0.0" } }, "sha512-MAYk9IlIGiyC0c9fnjdBSQfIFPZT0g1MfeSiD1UXTq2zJOLX55jS9/sETJHqw/7LN18JjITrhYfgCfapbmZHiQ=="], + + "@npmcli/fs": ["@npmcli/fs@5.0.0", "", { "dependencies": { "semver": "^7.3.5" } }, "sha512-7OsC1gNORBEawOa5+j2pXN9vsicaIOH5cPXxoR6fJOmH6/EXpJB2CajXOu1fPRFun2m1lktEFX11+P89hqO/og=="], + + "@npmcli/git": ["@npmcli/git@7.0.2", "", { "dependencies": { "@gar/promise-retry": "^1.0.0", "@npmcli/promise-spawn": "^9.0.0", "ini": "^6.0.0", "lru-cache": "^11.2.1", "npm-pick-manifest": "^11.0.1", "proc-log": "^6.0.0", "semver": "^7.3.5", "which": "^6.0.0" } }, "sha512-oeolHDjExNAJAnlYP2qzNjMX/Xi9bmu78C9dIGr4xjobrSKbuMYCph8lTzn4vnW3NjIqVmw/f8BCfouqyJXlRg=="], + + "@npmcli/installed-package-contents": ["@npmcli/installed-package-contents@4.0.0", "", { "dependencies": { "npm-bundled": "^5.0.0", "npm-normalize-package-bin": "^5.0.0" }, "bin": { "installed-package-contents": "bin/index.js" } }, "sha512-yNyAdkBxB72gtZ4GrwXCM0ZUedo9nIbOMKfGjt6Cu6DXf0p8y1PViZAKDC8q8kv/fufx0WTjRBdSlyrvnP7hmA=="], + + "@npmcli/map-workspaces": ["@npmcli/map-workspaces@5.0.3", "", { "dependencies": { "@npmcli/name-from-folder": "^4.0.0", "@npmcli/package-json": "^7.0.0", "glob": "^13.0.0", "minimatch": "^10.0.3" } }, "sha512-o2grssXo1e774E5OtEwwrgoszYRh0lqkJH+Pb9r78UcqdGJRDRfhpM8DvZPjzNLLNYeD/rNbjOKM3Ss5UABROw=="], + + "@npmcli/metavuln-calculator": ["@npmcli/metavuln-calculator@9.0.3", "", { "dependencies": { "cacache": "^20.0.0", "json-parse-even-better-errors": "^5.0.0", "pacote": "^21.0.0", "proc-log": "^6.0.0", "semver": "^7.3.5" } }, "sha512-94GLSYhLXF2t2LAC7pDwLaM4uCARzxShyAQKsirmlNcpidH89VA4/+K1LbJmRMgz5gy65E/QBBWQdUvGLe2Frg=="], + + "@npmcli/name-from-folder": ["@npmcli/name-from-folder@4.0.0", "", {}, "sha512-qfrhVlOSqmKM8i6rkNdZzABj8MKEITGFAY+4teqBziksCQAOLutiAxM1wY2BKEd8KjUSpWmWCYxvXr0y4VTlPg=="], + + "@npmcli/node-gyp": ["@npmcli/node-gyp@5.0.0", "", {}, "sha512-uuG5HZFXLfyFKqg8QypsmgLQW7smiRjVc45bqD/ofZZcR/uxEjgQU8qDPv0s9TEeMUiAAU/GC5bR6++UdTirIQ=="], + + "@npmcli/package-json": ["@npmcli/package-json@7.0.5", "", { "dependencies": { "@npmcli/git": "^7.0.0", "glob": "^13.0.0", "hosted-git-info": "^9.0.0", "json-parse-even-better-errors": "^5.0.0", "proc-log": "^6.0.0", "semver": "^7.5.3", "spdx-expression-parse": "^4.0.0" } }, "sha512-iVuTlG3ORq2iaVa1IWUxAO/jIp77tUKBhoMjuzYW2kL4MLN1bi/ofqkZ7D7OOwh8coAx1/S2ge0rMdGv8sLSOQ=="], + + "@npmcli/promise-spawn": ["@npmcli/promise-spawn@9.0.1", "", { "dependencies": { "which": "^6.0.0" } }, "sha512-OLUaoqBuyxeTqUvjA3FZFiXUfYC1alp3Sa99gW3EUDz3tZ3CbXDdcZ7qWKBzicrJleIgucoWamWH1saAmH/l2Q=="], + + "@npmcli/query": ["@npmcli/query@5.0.0", "", { "dependencies": { "postcss-selector-parser": "^7.0.0" } }, "sha512-8TZWfTQOsODpLqo9SVhVjHovmKXNpevHU0gO9e+y4V4fRIOneiXy0u0sMP9LmS71XivrEWfZWg50ReH4WRT4aQ=="], + + "@npmcli/redact": ["@npmcli/redact@4.0.0", "", {}, "sha512-gOBg5YHMfZy+TfHArfVogwgfBeQnKbbGo3pSUyK/gSI0AVu+pEiDVcKlQb0D8Mg1LNRZILZ6XG8I5dJ4KuAd9Q=="], + + "@npmcli/run-script": ["@npmcli/run-script@10.0.4", "", { "dependencies": { "@npmcli/node-gyp": "^5.0.0", "@npmcli/package-json": "^7.0.0", "@npmcli/promise-spawn": "^9.0.0", "node-gyp": "^12.1.0", "proc-log": "^6.0.0" } }, "sha512-mGUWr1uMnf0le2TwfOZY4SFxZGXGfm4Jtay/nwAa2FLNAKXUoUwaGwBMNH36UHPtinWfTSJ3nqFQr0091CxVGg=="], + + "@opencode/ai": ["@opencode/ai@2.1.4", "", { "dependencies": { "@aws-sdk/credential-providers": "3.1057.0", "@opencode/schema": "2.1.4", "@smithy/eventstream-codec": "4.2.14", "@smithy/util-utf8": "4.2.2", "aws4fetch": "1.0.20", "effect": "4.0.1", "google-auth-library": "10.5.0" } }, "sha512-980bht1i8xv6hN/TQ+3tGujHuxfduje3jW69SKBvmiDxQBZ468Mk7sR4ztOy3NB/4fbI8iWEYhrcc3GbWWMxrw=="], + + "@opencode/client": ["@opencode/client@2.1.4", "", { "dependencies": { "@opencode/protocol": "2.1.4", "@opencode/schema": "2.1.4" }, "peerDependencies": { "effect": "4.0.1", "solid-js": ">=1.9.0" }, "optionalPeers": ["effect", "solid-js"] }, "sha512-xNHP8Xu4wcq4G849Xnbb8IxYxMTzF0TqvohWVUQZH5aURrVHIIbHMfXbZY6s4i4C9kpVbwjXFFwWAo6KIK3oiw=="], + + "@opencode/plugin": ["@opencode/plugin@2.1.4", "", { "dependencies": { "@ai-sdk/provider": "3.0.8", "@opencode/ai": "2.1.4", "@opencode/client": "2.1.4", "@opencode/protocol": "2.1.4", "@opencode/schema": "2.1.4", "@opencode/util": "2.1.4", "effect": "4.0.1" }, "peerDependencies": { "@opencode/theme": "2.1.4", "@opentui/core": ">=0.6.8", "@opentui/solid": ">=0.6.8", "solid-js": ">=1.9.0" }, "optionalPeers": ["@opencode/theme", "@opentui/core", "@opentui/solid", "solid-js"] }, "sha512-SGAkdGp0VRxos2BEkRiyRl75G7DtuvP0ywgiEiHz1PbdUwUe2EAURMrzFgLCYCpWEm/zaNrguxPDVL0carrCsA=="], + + "@opencode/protocol": ["@opencode/protocol@2.1.4", "", { "dependencies": { "@opencode/schema": "2.1.4", "effect": "4.0.1" } }, "sha512-dnifgvY6p4xdHkXRH3PEaobfCqrHb1AhihKxPNRNkGn5SueRCFCfK2pZW1GkeC36fGCmcLxlfihuIMaQL1BVzA=="], + + "@opencode/schema": ["@opencode/schema@2.1.4", "", { "dependencies": { "@standard-schema/spec": "1.1.0", "effect": "4.0.1" } }, "sha512-PLr8eFW81Yz6U7YsSgfSUClUUH//8omh3HiXu/L08VX5arorWNAu2uSjoXyHIGkm7TaTbdXBGPmmDfpvrDM2wQ=="], + + "@opencode/util": ["@opencode/util@2.1.4", "", { "dependencies": { "@effect/opentelemetry": "4.0.1", "@effect/platform-node": "4.0.1", "@effect/platform-node-shared": "4.0.1", "@npmcli/arborist": "9.4.0", "@npmcli/config": "10.8.1", "@opentelemetry/api": "1.9.0", "@opentelemetry/api-logs": "0.219.0", "@opentelemetry/context-async-hooks": "2.8.0", "@opentelemetry/exporter-trace-otlp-http": "0.219.0", "@opentelemetry/resources": "2.8.0", "@opentelemetry/sdk-logs": "0.219.0", "@opentelemetry/sdk-metrics": "2.8.0", "@opentelemetry/sdk-trace-base": "2.8.0", "@opentelemetry/sdk-trace-node": "2.8.0", "cross-spawn": "7.0.6", "effect": "4.0.1", "glob": "13.0.5", "mime-types": "3.0.2", "minimatch": "10.2.5", "npm-package-arg": "13.0.2", "open": "11.0.4", "pacote": "21.5.1" } }, "sha512-Ihj6DqbnnovgUYSXki4PgvGxNbmB1wggDySIdM55Rz+sTfwFZ+zwROh3PrC4a8Vx/cJ6xQg41Uxa4nbcTbGI4A=="], + + "@opentelemetry/api": ["@opentelemetry/api@1.9.0", "", {}, "sha512-3giAOQvZiH5F9bMlMiv8+GSPMeqg0dbaeo58/0SlA9sxSqZhnUtxzX9/2FzyhS9sWQf5S0GJE0AKBrFqjpeYcg=="], + + "@opentelemetry/api-logs": ["@opentelemetry/api-logs@0.219.0", "", { "dependencies": { "@opentelemetry/api": "^1.3.0" } }, "sha512-FFx7YnaYJlIjqWW/AG/yAZ0L/NEY724PipXXXQLdtZPbLwBGbUMTGL1i/esI56TWfTUXxhLfpgrnWJCG8aUJyg=="], + + "@opentelemetry/context-async-hooks": ["@opentelemetry/context-async-hooks@2.8.0", "", { "peerDependencies": { "@opentelemetry/api": ">=1.0.0 <1.10.0" } }, "sha512-/3FIraneMcng67SUJCxvyInk/oxzwsxyadufk0wwfOBLf5wqtAGX4MoQASwSbndBPeARzBryUM9Azr5kHIdWLw=="], + + "@opentelemetry/core": ["@opentelemetry/core@2.8.0", "", { "dependencies": { "@opentelemetry/semantic-conventions": "^1.29.0" }, "peerDependencies": { "@opentelemetry/api": ">=1.0.0 <1.10.0" } }, "sha512-hd1Lfh8p545nNz+jq1Ejfz+Mn1hyLuxYn1YzTfFNrxr8urEWMNQLPf1Th8kjOH+HxwawCrtgBp8JpBUR4ZSgww=="], + + "@opentelemetry/exporter-trace-otlp-http": ["@opentelemetry/exporter-trace-otlp-http@0.219.0", "", { "dependencies": { "@opentelemetry/core": "2.8.0", "@opentelemetry/otlp-exporter-base": "0.219.0", "@opentelemetry/otlp-transformer": "0.219.0", "@opentelemetry/resources": "2.8.0", "@opentelemetry/sdk-trace-base": "2.8.0" }, "peerDependencies": { "@opentelemetry/api": "^1.3.0" } }, "sha512-9t6SvBXXBEjOBcIzgozvBbd3jWrv3Gt3ngGhl1fhdZ/zRc7oZDVOFEqbi2zlBpW9BXhgDMKv422J0DL/3iQWfw=="], + + "@opentelemetry/otlp-exporter-base": ["@opentelemetry/otlp-exporter-base@0.219.0", "", { "dependencies": { "@opentelemetry/core": "2.8.0", "@opentelemetry/otlp-transformer": "0.219.0" }, "peerDependencies": { "@opentelemetry/api": "^1.3.0" } }, "sha512-zvIxQX/AZUVKDU+hCuYx+7UkiP7GRdnk1ZbFQRYzHvYp47cAWR4j3IhoPhV9KaeXEv2xdGq3IA6PnpzDmLcmSA=="], + + "@opentelemetry/otlp-transformer": ["@opentelemetry/otlp-transformer@0.219.0", "", { "dependencies": { "@opentelemetry/api-logs": "0.219.0", "@opentelemetry/core": "2.8.0", "@opentelemetry/resources": "2.8.0", "@opentelemetry/sdk-logs": "0.219.0", "@opentelemetry/sdk-metrics": "2.8.0", "@opentelemetry/sdk-trace-base": "2.8.0" }, "peerDependencies": { "@opentelemetry/api": "^1.3.0" } }, "sha512-aaYKAyXhw9VchKZVGOopD3Gw/kPsyrX2c6IQ0AW32mTjqmZOh5Y6Gf5OYqTNqVktAeBjmFinhyFaCwW6GYK9YQ=="], + + "@opentelemetry/resources": ["@opentelemetry/resources@2.8.0", "", { "dependencies": { "@opentelemetry/core": "2.8.0", "@opentelemetry/semantic-conventions": "^1.29.0" }, "peerDependencies": { "@opentelemetry/api": ">=1.3.0 <1.10.0" } }, "sha512-qmXQ27ilDbUK/vGMqwL8D4/rhn76C+sherM4wTbjlfknR8Nvfc/hCxjRJPhkzZzUsPiNg16SA31NxMabwttRjg=="], + + "@opentelemetry/sdk-logs": ["@opentelemetry/sdk-logs@0.219.0", "", { "dependencies": { "@opentelemetry/api-logs": "0.219.0", "@opentelemetry/core": "2.8.0", "@opentelemetry/resources": "2.8.0", "@opentelemetry/semantic-conventions": "^1.29.0" }, "peerDependencies": { "@opentelemetry/api": ">=1.4.0 <1.10.0" } }, "sha512-s6lTKRakaPClvKoWHRChxnXjDMkM/TQ30ff78jN6EBGf7MI7VzANE5PU3f4z9qDUudWjvZjOLHG0rBnBKYvoXA=="], + + "@opentelemetry/sdk-metrics": ["@opentelemetry/sdk-metrics@2.8.0", "", { "dependencies": { "@opentelemetry/core": "2.8.0", "@opentelemetry/resources": "2.8.0" }, "peerDependencies": { "@opentelemetry/api": ">=1.9.0 <1.10.0" } }, "sha512-UDBGaj6W0Rgy5rTTaoxs8gVGF/aGkAKyjurJv7se6wjRxJu7FoquTLT/vt54DZfo4crbprYfhX/SOK9+BPw1qg=="], - "@msgpackr-extract/msgpackr-extract-darwin-x64": ["@msgpackr-extract/msgpackr-extract-darwin-x64@3.0.4", "", { "os": "darwin", "cpu": "x64" }, "sha512-zExlW9zUJKZH/tOtVMttwjKa4Xm/3KcNjnE3dPN92uCktwavMxpgCA3MoJK/DOnTWsQgo224OaST27/mPNAf+w=="], + "@opentelemetry/sdk-trace-base": ["@opentelemetry/sdk-trace-base@2.8.0", "", { "dependencies": { "@opentelemetry/core": "2.8.0", "@opentelemetry/resources": "2.8.0", "@opentelemetry/semantic-conventions": "^1.29.0" }, "peerDependencies": { "@opentelemetry/api": ">=1.3.0 <1.10.0" } }, "sha512-mhU4jp+vW0mGbFRd+GeXHvmfA4aDqWjBjLC3pE5XMpLs0IE2ryYb019Ts2AQrOq67gaTF25D91+fgvEHDZEnuQ=="], - "@msgpackr-extract/msgpackr-extract-linux-arm": ["@msgpackr-extract/msgpackr-extract-linux-arm@3.0.4", "", { "os": "linux", "cpu": "arm" }, "sha512-Tg3yX65f5GbtXLkrYEHE5oibZG9epyYWas7FogTTEJeDEF9JlXJzKgXaNhT3UXlTOeA+AfZpYZYZ0uPj7Cfquw=="], + "@opentelemetry/sdk-trace-node": ["@opentelemetry/sdk-trace-node@2.8.0", "", { "dependencies": { "@opentelemetry/context-async-hooks": "2.8.0", "@opentelemetry/core": "2.8.0", "@opentelemetry/sdk-trace-base": "2.8.0" }, "peerDependencies": { "@opentelemetry/api": ">=1.0.0 <1.10.0" } }, "sha512-nZt9OGufioAc3AfoLTqA9bsAeaMJAictYDdI2VcNQ+PmT+3rfKjAZDZvgPfd8VPX0O5Bw1hdQF6kDK8VSpZiWg=="], - "@msgpackr-extract/msgpackr-extract-linux-arm64": ["@msgpackr-extract/msgpackr-extract-linux-arm64@3.0.4", "", { "os": "linux", "cpu": "arm64" }, "sha512-dgX0P/9wGPJeHFBG+ZmhgE6bmtMt7NP5CRBGyyktpopdk/mW4POnrpQsSLtKI1dwpc+pPLuXHDh6vvskyQE/sw=="], + "@opentelemetry/semantic-conventions": ["@opentelemetry/semantic-conventions@1.43.0", "", {}, "sha512-eSYWTm620tTk45EKSedaUL8MFYI8hW164hIXsgIHyxu3VobUB3fFCu5t0hQby6OoWRPsG1KkKUG2M5UadiLiVg=="], - "@msgpackr-extract/msgpackr-extract-linux-x64": ["@msgpackr-extract/msgpackr-extract-linux-x64@3.0.4", "", { "os": "linux", "cpu": "x64" }, "sha512-8TNXMEjJc3QEy7R/x1INhgiU+XakDAFUzBhaz7+Rbrs8NH5UQeHQxxmzsSBJGyV6I1jW79undiQm8tOI+D+8FQ=="], + "@pkgjs/parseargs": ["@pkgjs/parseargs@0.11.0", "", {}, "sha512-+1VkjdD0QBLPodGrJUeqarH8VAIvQODIbwh9XpP5Syisf7YoQgsJKPNFoqqLQlu+VQ/tVSshMR6loPMn8U+dPg=="], - "@msgpackr-extract/msgpackr-extract-win32-x64": ["@msgpackr-extract/msgpackr-extract-win32-x64@3.0.4", "", { "os": "win32", "cpu": "x64" }, "sha512-CmCXPQrkbwExx3j946/PtHWHbYJiCRBRDl4BlkRQcJB/YOwQxJRTpoo7aTsortjgoJ1x7opzTSxn7C+ASSLVjQ=="], + "@redis/bloom": ["@redis/bloom@6.3.0", "", { "peerDependencies": { "@redis/client": "^6.3.0" } }, "sha512-NQ5poYpZr0jv6zazN1cy6JY5kIipKFZ3R9GTL4kOUTX3a9YAfY29U8bsE4lWhVQEM+G4V0BXVC57mubIdDijeA=="], - "@opencode-ai/plugin": ["@opencode-ai/plugin@1.18.31", "", { "dependencies": { "@ai-sdk/provider": "3.0.8", "@opencode-ai/sdk": "1.18.31", "effect": "4.0.0-beta.83", "zod": "4.1.8" }, "peerDependencies": { "@opentui/core": ">=0.4.5", "@opentui/keymap": ">=0.4.5", "@opentui/solid": ">=0.4.5" }, "optionalPeers": ["@opentui/core", "@opentui/keymap", "@opentui/solid"] }, "sha512-Rdc1bPK06PByaGyGd0kf7JUZ4pTkexz2OOUNlqZWLpHOiMEZ+/rFGjt46ypZ3QwA697gNdWwwwQbHbKG5NMwGA=="], + "@redis/client": ["@redis/client@6.3.0", "", { "peerDependencies": { "@node-rs/xxhash": "^1.1.0", "@opentelemetry/api": ">=1 <2" }, "optionalPeers": ["@node-rs/xxhash", "@opentelemetry/api"] }, "sha512-fHrjwGCBvAANGi+TuRuwKi83sIOzgHr8eh+Uf48w3fSX7Hhrz174NrWqfJhAC2vnN0d/tfaZpdr6+o6JC3QHwg=="], - "@opencode-ai/sdk": ["@opencode-ai/sdk@1.18.31", "", { "dependencies": { "cross-spawn": "7.0.6" } }, "sha512-Raouthf8Lhe9edjvYeeSK7SgvdoU6bBjH9qV3f70dHoa6h+z0X2TMz/e22/wKp/StlFUZ4kIRpYYxFnY8/k01w=="], + "@redis/json": ["@redis/json@6.3.0", "", { "peerDependencies": { "@redis/client": "^6.3.0" } }, "sha512-yt6vRfPBXtW/3qm53cF81TSQpLMjWdW4/2E9WQ2GgbvsKkuCKCFQIcMiKlil1ZMnezK+IQuS795xKU5psbaegw=="], + + "@redis/search": ["@redis/search@6.3.0", "", { "peerDependencies": { "@redis/client": "^6.3.0" } }, "sha512-RPXAZmKjZZG7Tm7m1w9keFdXzbL91PiSHQBfQRe9xwW40jNsTRbG0FwhIz4QlHPU4gKDAsCy8IQHj4yZl4H9rw=="], + + "@redis/time-series": ["@redis/time-series@6.3.0", "", { "peerDependencies": { "@redis/client": "^6.3.0" } }, "sha512-+fJOB8mN1z5WRakCJNh5lVdz1KqvHGSOistsKeitD24QsCtPwyRQXWC0iq6T/i/ubLE/YcguZqPm8F598MKk4A=="], + + "@sigstore/bundle": ["@sigstore/bundle@4.0.0", "", { "dependencies": { "@sigstore/protobuf-specs": "^0.5.0" } }, "sha512-NwCl5Y0V6Di0NexvkTqdoVfmjTaQwoLM236r89KEojGmq/jMls8S+zb7yOwAPdXvbwfKDlP+lmXgAL4vKSQT+A=="], + + "@sigstore/core": ["@sigstore/core@3.2.1", "", {}, "sha512-qRsxPnCrbC/puegGxKuynfnxgLiHqWStrSjxkoB4YKqq3Z3s4cyZyj42ZdWFAEblNP65C+rBH8EuREHIXoi83g=="], + + "@sigstore/protobuf-specs": ["@sigstore/protobuf-specs@0.5.2", "", {}, "sha512-SQqvFMt4V78fdjcDdYX6HbiVSOR4QK3ZgwCa2KOsopAgPIHy1rU5UDUmzLl02r5oyyaYcYHR1hpwDRk/yUe+Mw=="], + + "@sigstore/sign": ["@sigstore/sign@4.1.1", "", { "dependencies": { "@gar/promise-retry": "^1.0.2", "@sigstore/bundle": "^4.0.0", "@sigstore/core": "^3.2.0", "@sigstore/protobuf-specs": "^0.5.0", "make-fetch-happen": "^15.0.4", "proc-log": "^6.1.0" } }, "sha512-Hf4xglukg0XXQ2RiD5vSoLjdPe8OBUPA8XeVjUObheuDcWdYWrnH/BNmxZCzkAy68MzmNCxXLeurJvs6hcP2OQ=="], + + "@sigstore/tuf": ["@sigstore/tuf@4.0.2", "", { "dependencies": { "@sigstore/protobuf-specs": "^0.5.0", "tuf-js": "^4.1.0" } }, "sha512-TCAzTy0xzdP79EnxSjq9KQ3eaR7+FmudLC6eRKknVKZbV7ZNlGLClAAQb/HMNJ5n2OBNk2GT1tEmU0xuPr+SLQ=="], + + "@sigstore/verify": ["@sigstore/verify@3.1.1", "", { "dependencies": { "@sigstore/bundle": "^4.0.0", "@sigstore/core": "^3.2.1", "@sigstore/protobuf-specs": "^0.5.0" } }, "sha512-qv7+G3J2cc6wwFj3yKvXOamzqhMwSk1ogPGmhpS8iXllcPrJaIIBA+4HbttlHVu1pqWTdmaCH/WE7UOC51kdoA=="], + + "@smithy/core": ["@smithy/core@3.36.2", "", { "dependencies": { "@smithy/types": "^4.19.0", "tslib": "^2.6.2" } }, "sha512-oRw0rB9QSUiCZfEs23G5F4Af+b2rQt1lItIICWu5IejFNd+uPM8SWqiKLMpQ6bX1Q7slsLbjuoE2niErFtkVPQ=="], + + "@smithy/credential-provider-imds": ["@smithy/credential-provider-imds@4.5.2", "", { "dependencies": { "@smithy/core": "^3.33.2", "@smithy/types": "^4.17.2", "tslib": "^2.6.2" } }, "sha512-A9uSdn72ozbRUSit0eib0TW7nXuNPlaeM0zcGkJ+nE6tFcSDbnmtwoxbTCFBukVQcszDAyvsd7+rTduPTXpygg=="], + + "@smithy/eventstream-codec": ["@smithy/eventstream-codec@4.2.14", "", { "dependencies": { "@aws-crypto/crc32": "5.2.0", "@smithy/types": "^4.14.1", "@smithy/util-hex-encoding": "^4.2.2", "tslib": "^2.6.2" } }, "sha512-erZq0nOIpzfeZdCyzZjdJb4nVSKLUmSkaQUVkRGQTXs30gyUGeKnrYEg+Xe1W5gE3aReS7IgsvANwVPxSzY6Pw=="], + + "@smithy/fetch-http-handler": ["@smithy/fetch-http-handler@5.8.0", "", { "dependencies": { "@smithy/core": "^3.33.3", "@smithy/types": "^4.18.0", "tslib": "^2.6.2" } }, "sha512-ycSJu3tFAQ4v04CBB0agqFMVsSQ1iG3yw+SpgxRqKfaURpQD4CZ8Wn0zPMmSnOuTpTh65Vz+EA0rMrw089wvkA=="], + + "@smithy/is-array-buffer": ["@smithy/is-array-buffer@2.2.0", "", { "dependencies": { "tslib": "^2.6.2" } }, "sha512-GGP3O9QFD24uGeAXYUjwSTXARoqpZykHadOmA8G5vfJPK0/DC67qa//0qvqrJzL1xc8WQWX7/yc7fwudjPHPhA=="], + + "@smithy/node-http-handler": ["@smithy/node-http-handler@4.12.1", "", { "dependencies": { "@smithy/core": "^3.33.3", "@smithy/types": "^4.18.0", "tslib": "^2.6.2" } }, "sha512-ThMkboGeONWXAelq9FvGsuJC4rOi+qyC4/zhUF58xYpxUg5sQKx2VXZYJmtNjr4dSuBJ1HeJXETQILCz3wOHvw=="], + + "@smithy/signature-v4": ["@smithy/signature-v4@5.7.4", "", { "dependencies": { "@smithy/core": "^3.35.0", "@smithy/types": "^4.19.0", "tslib": "^2.6.2" } }, "sha512-tHy0K0VtqNd5Y7Y41h0a0Lhh0L1GzC08dTWg0F7vRJWFtTENg7IZikf3wQkanYIRdb7ngoIPMTmqgUi401fEeQ=="], + + "@smithy/types": ["@smithy/types@4.19.0", "", { "dependencies": { "tslib": "^2.6.2" } }, "sha512-r7jh49VJxGerfAcTQA6gXcKc+98zOp/tqRwzYjgOE+iSQsP6cEU1hq2QzbuipmP68QtYdY9wKEhiCQZIzHgZ4Q=="], + + "@smithy/util-buffer-from": ["@smithy/util-buffer-from@4.5.2", "", { "dependencies": { "@smithy/core": "^3.33.2", "tslib": "^2.6.2" } }, "sha512-nxu3SgmAw9JXT2CtkU0m/XNLWpP9MsaBx1zAGAypCbYj15tIFlmcYwpF+Oh18le83d+IM9PT7ENdXnE4C+d5mA=="], + + "@smithy/util-hex-encoding": ["@smithy/util-hex-encoding@4.5.2", "", { "dependencies": { "@smithy/core": "^3.33.2", "tslib": "^2.6.2" } }, "sha512-iq+cW3mAb7vfcxEEpYi3zXKpDtbrIFyanWjQl4zBq4seWD4OSxXDWSfespZxenX6aEaighn+NR3u1nU1DSvs3w=="], + + "@smithy/util-utf8": ["@smithy/util-utf8@4.2.2", "", { "dependencies": { "@smithy/util-buffer-from": "^4.2.2", "tslib": "^2.6.2" } }, "sha512-75MeYpjdWRe8M5E3AW0O4Cx3UadweS+cwdXjwYGBW5h/gxxnbeZ877sLPX/ZJA9GVTlL/qG0dXP29JWFCD1Ayw=="], "@standard-schema/spec": ["@standard-schema/spec@1.1.0", "", {}, "sha512-l2aFy5jALhniG5HgqrD6jXLi/rUWrKvqN/qJx6yoJsgKhblVd+iqqU4RCXavm/jPityDo5TCvKMnpjKnOriy0w=="], + "@tufjs/canonical-json": ["@tufjs/canonical-json@2.0.0", "", {}, "sha512-yVtV8zsdo8qFHe+/3kw81dSLyF7D576A5cCFCi4X7B39tWT7SekaEFUnvnWJHz+9qO7qJTah1JbrDjWKqFtdWA=="], + + "@tufjs/models": ["@tufjs/models@4.1.0", "", { "dependencies": { "@tufjs/canonical-json": "2.0.0", "minimatch": "^10.1.1" } }, "sha512-Y8cK9aggNRsqJVaKUlEYs4s7CvQ1b1ta2DVPyAimb0I2qhzjNk+A+mxvll/klL0RlfuIUei8BF7YWiua4kQqww=="], + "@types/bun": ["@types/bun@1.4.2", "", { "dependencies": { "bun-types": "1.4.2" } }, "sha512-GimotNn7+ZV0uVArItBbriZsR1oNf0+WTzPkdcFrzShI7k2norL0uzEaJT8T33dWr7O/c9ZDuAFQrctKCi72oQ=="], "@types/node": ["@types/node@26.6.2", "", { "dependencies": { "undici-types": "~8.9.0" } }, "sha512-X1P21scMv4zGKLYqjdGjaKa7COa0RKVYYZZN/NfvLQ1JegxFhdhpZG/Lyn8AXx6CDUavKAd11v6BvfpkDByK8g=="], + "@types/ws": ["@types/ws@8.18.2", "", { "dependencies": { "@types/node": "*" } }, "sha512-67MQl+fpWKVTT1NYdnmo3U4sc/xPo/zQBncVnI74qmQa0z/b+1g6iYqNmGCPbxO+zz2aklb08a0oHfegiVd0/w=="], + "@typescript/typescript-aix-ppc64": ["@typescript/typescript-aix-ppc64@7.0.2", "", { "os": "aix", "cpu": "ppc64" }, "sha512-MTKKkWB7p/0E9xi1d1tHtZ5PiLkGEMIq88pK2CubZjOsLtYTLqhgIgi6zepFa+9GHZ6h05NMCkQxGKiPXMxXtQ=="], "@typescript/typescript-darwin-arm64": ["@typescript/typescript-darwin-arm64@7.0.2", "", { "os": "darwin", "cpu": "arm64" }, "sha512-gowzar9MwS/aRWp6f3a4KUqzRjAZjOsmGNCM6LcTgXum+dBfgsBVMN+AgvOCCbguXyick6LJhpBszxMebJ8syA=="], @@ -106,60 +275,378 @@ "@typescript/typescript-win32-x64": ["@typescript/typescript-win32-x64@7.0.2", "", { "os": "win32", "cpu": "x64" }, "sha512-0BQ3HkAHHlKLSp1qRvf3SUhGpGsDuhB/jgFw75guyqbxJqEaS0Cw/VFO8i2nHglJUzQCRtMMR/IBAKE3ETMC4g=="], + "abbrev": ["abbrev@4.0.0", "", {}, "sha512-a1wflyaL0tHtJSmLSOVybYhy22vRih4eduhhrkcjgrWGnRfrZtovJ2FRjxuTtkkj47O/baf0R86QU5OuYpz8fA=="], + + "agent-base": ["agent-base@7.1.4", "", {}, "sha512-MnA+YT8fwfJPgBx3m60MNqakm30XOkyIoH1y6huTQvC0PwZG7ki8NacLBcrPbNoo8vEZy7Jpuk7+jMO+CUovTQ=="], + + "ansi-regex": ["ansi-regex@6.4.0", "", {}, "sha512-KzTVk2tCWAHtYrvvvaP8bJKJq2pVinhLcGEQdtLIYPbmNGNyYe8QwNaTUYQp2J7/vIsUKt5QCqAfUkYyG9DkOw=="], + + "ansi-styles": ["ansi-styles@6.2.3", "", {}, "sha512-4Dj6M28JB+oAH8kFkTLUo+a2jwOFkuqb3yucU0CANcRRUbxS0cP0nZYCGjcc3BNXwRIsUVmDGgzawme7zvJHvg=="], + + "aws4fetch": ["aws4fetch@1.0.20", "", {}, "sha512-/djoAN709iY65ETD6LKCtyyEI04XIBP5xVvfmNxsEP0uJB5tyaGBztSryRr4HqMStr9R06PisQE7m9zDTXKu6g=="], + + "balanced-match": ["balanced-match@4.0.4", "", {}, "sha512-BLrgEcRTwX2o6gGxGOCNyMvGSp35YofuYzw9h1IMTRmKqttAZZVU67bdb9Pr2vUHA8+j3i2tJfjO6C6+4myGTA=="], + + "base64-js": ["base64-js@1.5.1", "", {}, "sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA=="], + + "bignumber.js": ["bignumber.js@9.3.1", "", {}, "sha512-Ko0uX15oIUS7wJ3Rb30Fs6SkVbLmPBAKdlm7q9+ak9bbIeFf0MwuBsQV6z7+X768/cHsfg+WlysDWJcmthjsjQ=="], + + "bin-links": ["bin-links@6.0.2", "", { "dependencies": { "cmd-shim": "^8.0.0", "npm-normalize-package-bin": "^5.0.0", "proc-log": "^6.0.0", "read-cmd-shim": "^6.0.0", "write-file-atomic": "^7.0.0" } }, "sha512-frE1t78WOwJ45PKV2cF2tNPjTcs9L1J9s6VkrV59wanRP4GlaomuxYPVma7BwthMg8WnfSory4w5PTE6FZZ81w=="], + + "bowser": ["bowser@2.14.1", "", {}, "sha512-tzPjzCxygAKWFOJP011oxFHs57HzIhOEracIgAePE4pqB3LikALKnSzUyU4MGs9/iCEUuHlAJTjTc5M+u7YEGg=="], + + "brace-expansion": ["brace-expansion@5.0.12", "", { "dependencies": { "balanced-match": "^4.0.2" } }, "sha512-YovQ3rzhaLMIrDjNDMkNS01tea93qhEhG5xy8f6+R0l+dw3Ki+5sCoIoI942iuLZTHWogWktgwVDhU09iNEimQ=="], + + "buffer-equal-constant-time": ["buffer-equal-constant-time@1.0.1", "", {}, "sha512-zRpUiDwd/xk6ADqPMATG8vc9VPrkck7T07OIx0gnjmJAnHnTVXNQG3vfvWNuiZIkwu9KrKdA1iJKfsfTVxE6NA=="], + "bun-types": ["bun-types@1.4.2", "", { "dependencies": { "@types/node": "*" } }, "sha512-bxV1FgK7yBIzjRe5zBozIM4Bem11ZJcCXSrjWRG3YWLt8yFDePu4cLjpebO8OvPeIE9trbyPF4fuj3Cia4Fj3w=="], + "bundle-name": ["bundle-name@4.1.1", "", { "dependencies": { "run-applescript": "^7.0.0" } }, "sha512-DdH81/zPLVS11EUgWq3tEu/xn+EzljlMYooDNdzWEnFha3R3NBMpMV1UqYIpjYHV/SgpFKMIX1Oh7o06SjM/oA=="], + + "cacache": ["cacache@20.0.4", "", { "dependencies": { "@npmcli/fs": "^5.0.0", "fs-minipass": "^3.0.0", "glob": "^13.0.0", "lru-cache": "^11.1.0", "minipass": "^7.0.3", "minipass-collect": "^2.0.1", "minipass-flush": "^1.0.5", "minipass-pipeline": "^1.2.4", "p-map": "^7.0.2", "ssri": "^13.0.0" } }, "sha512-M3Lab8NPYlZU2exsL3bMVvMrMqgwCnMWfdZbK28bn3pK6APT/Te/I8hjRPNu1uwORY9a1eEQoifXbKPQMfMTOA=="], + + "chownr": ["chownr@3.0.0", "", {}, "sha512-+IxzY9BZOQd/XuYPRmrvEVjF/nqj5kgT4kEq7VofrDoM1MxoRjEWkrCC3EtLi59TVawxTAn+orJwFQcrqEN1+g=="], + + "ci-info": ["ci-info@4.4.0", "", {}, "sha512-77PSwercCZU2Fc4sX94eF8k8Pxte6JAwL4/ICZLFjJLqegs7kCuAsqqj/70NQF6TvDpgFjkubQB2FW2ZZddvQg=="], + + "cmd-shim": ["cmd-shim@8.0.0", "", {}, "sha512-Jk/BK6NCapZ58BKUxlSI+ouKRbjH1NLZCgJkYoab+vEHUY3f6OzpNBN9u7HFSv9J6TRDGs4PLOHezoKGaFRSCA=="], + + "color-convert": ["color-convert@2.0.1", "", { "dependencies": { "color-name": "~1.1.4" } }, "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ=="], + + "color-name": ["color-name@1.1.4", "", {}, "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA=="], + + "common-ancestor-path": ["common-ancestor-path@2.0.0", "", {}, "sha512-dnN3ibLeoRf2HNC+OlCiNc5d2zxbLJXOtiZUudNFSXZrNSydxcCsSpRzXwfu7BBWCIfHPw+xTayeBvJCP/D8Ng=="], + + "content-type": ["content-type@2.1.0", "", {}, "sha512-mj7UPXE0jaqaOsukNZRUEfEi2AcL7C/vwmwcHV0O97eO1E1pxBZuyjlZrx5seTaNBg1U6+o35wpa35Qfcc+7ag=="], + "cross-spawn": ["cross-spawn@7.0.6", "", { "dependencies": { "path-key": "^3.1.0", "shebang-command": "^2.0.0", "which": "^2.0.1" } }, "sha512-uV2QOWP2nWzsy2aMp8aRibhi9dlzF5Hgh5SHaB9OiTGEyDTiJJyx0uy51QXdyWbtAHNua4XJzUKca3OzKUd3vA=="], - "detect-libc": ["detect-libc@2.1.2", "", {}, "sha512-Btj2BOOO83o3WyH59e8MgXsxEQVcarkUOpEYrubB0urwnN10yQ364rsiByU11nZlqWYZm05i/of7io4mzihBtQ=="], + "cssesc": ["cssesc@3.0.0", "", { "bin": { "cssesc": "bin/cssesc" } }, "sha512-/Tb/JcjK111nNScGob5MNtsntNM1aCNUDipB/TkwZFhyDrrE47SOx/18wF2bbjgc3ZzCSKW1T5nt5EbFoAz/Vg=="], + + "data-uri-to-buffer": ["data-uri-to-buffer@4.0.1", "", {}, "sha512-0R9ikRb668HB7QDxT1vkpuUBtqc53YyAwMwGeUFKRojY/NWKvdZ+9UYtRfGmhqNbRkTSVpMbmyhXipFFv2cb/A=="], + + "debug": ["debug@4.4.3", "", { "dependencies": { "ms": "^2.1.3" }, "peerDependencies": { "supports-color": "*" }, "optionalPeers": ["supports-color"] }, "sha512-RGwwWnwQvkVfavKVt22FGLw+xYSdzARwm0ru6DhTVA3umU5hZc28V3kO4stgYryrTlLpuvgI9GiijltAjNbcqA=="], + + "default-browser": ["default-browser@5.5.1", "", { "dependencies": { "bundle-name": "^4.1.0", "default-browser-id": "^5.0.0" } }, "sha512-m1pAzaJgZ/gssEqlOhJkPJp8Xly7QyW6xcrkUa2KKcDeDSEMP7X8xipU3snUcfisTQx0w1AGae+9UtJSfVnXGw=="], + + "default-browser-id": ["default-browser-id@5.0.1", "", {}, "sha512-x1VCxdX4t+8wVfd1so/9w+vQ4vx7lKd2Qp5tDRutErwmR85OgmfX7RlLRMWafRMY7hbEiXIbudNrjOAPa/hL8Q=="], + + "define-lazy-prop": ["define-lazy-prop@3.0.0", "", {}, "sha512-N+MeXYoqr3pOgn8xfyRPREN7gHakLYjhsHhWGT3fWAiL4IkAt0iDw14QiiEm2bE30c5XX5q0FtAA3CK5f9/BUg=="], + + "eastasianwidth": ["eastasianwidth@0.2.0", "", {}, "sha512-I88TYZWc9XiYHRQ4/3c5rjjfgkjhLyW2luGIheGERbNQ6OY7yTybanSpDXZa8y7VUP9YmDcYa+eyq4ca7iLqWA=="], + + "ecdsa-sig-formatter": ["ecdsa-sig-formatter@1.0.11", "", { "dependencies": { "safe-buffer": "^5.0.1" } }, "sha512-nagl3RYrbNv6kQkeJIpt6NJZy8twLB/2vtz6yN9Z4vRKHN4/QZJIEbqohALSgwKdnksuY3k5Addp5lg8sVoVcQ=="], + + "effect": ["effect@4.0.1", "", {}, "sha512-b1VlQG9g8fwxE5QnIZuPoOP/0MsqHJSRKxUijjoM80E5q95FXrX5yWtY5XI8G+GJsBQAVSDbLS458z9a++uVvw=="], + + "emoji-regex": ["emoji-regex@9.2.2", "", {}, "sha512-L18DaJsXSUk2+42pv8mLs5jJT2hqFkFE4j21wOmgbUqsZ2hL72NsUU785g9RXgo3s0ZNgVl42TiHp3ZtOv/Vyg=="], + + "env-paths": ["env-paths@2.2.1", "", {}, "sha512-+h1lkLKhZMTYjog1VEpJNG7NZJWcuc2DDk/qsqSTRRCOXiLjeQ1d1/udrUGhqMxUgAlwKNZ0cf2uqan5GLuS2A=="], + + "exponential-backoff": ["exponential-backoff@3.1.3", "", {}, "sha512-ZgEeZXj30q+I0EN+CbSSpIyPaJ5HVQD18Z1m+u1FXbAeT94mr1zw50q4q6jiiC447Nl/YTcIYSAftiGqetwXCA=="], + + "extend": ["extend@3.0.2", "", {}, "sha512-fjquC59cD7CyW6urNXK0FBufkZcoiGG80wTuPujX590cB5Ttln20E2UB4S/WARVqhXffZl2LNgS+gQdPIIim/g=="], + + "fdir": ["fdir@6.5.0", "", { "peerDependencies": { "picomatch": "^3 || ^4" }, "optionalPeers": ["picomatch"] }, "sha512-tIbYtZbucOs0BRGqPJkshJUYdL+SDH7dVM8gjy+ERp3WAUjLEFJE+02kanyHtwjWOnwrKYBiwAmM0p4kLJAnXg=="], + + "fetch-blob": ["fetch-blob@3.2.0", "", { "dependencies": { "node-domexception": "^1.0.0", "web-streams-polyfill": "^3.0.3" } }, "sha512-7yAQpD2UMJzLi1Dqv7qFYnPbaPx7ZfFK6PiIxQ4PfkGPyNyl2Ugx+a/umUonmKqjhM4DnfbMvdX6otXq83soQQ=="], + + "foreground-child": ["foreground-child@3.3.1", "", { "dependencies": { "cross-spawn": "^7.0.6", "signal-exit": "^4.0.1" } }, "sha512-gIXjKqtFuWEgzFRJA9WCQeSJLZDjgJUOMCMzxtvFq/37KojM1BFGufqsCy0r4qSQmYLsZYMeyRqzIWOMup03sw=="], + + "formdata-polyfill": ["formdata-polyfill@4.0.10", "", { "dependencies": { "fetch-blob": "^3.1.2" } }, "sha512-buewHzMvYL29jdeQTVILecSaZKnt/RJWjoZCF5OW60Z67/GmSLBkOFM7qh1PI3zFNtJbaZL5eQu1vLfazOwj4g=="], + + "fs-minipass": ["fs-minipass@3.0.3", "", { "dependencies": { "minipass": "^7.0.3" } }, "sha512-XUBA9XClHbnJWSfBzjkm6RvPsyg3sryZt06BEQoXcF7EK/xpGaQYJgQKDJSUH5SGZ76Y7pFx1QBnXz09rU5Fbw=="], - "effect": ["effect@4.0.0-beta.83", "", { "dependencies": { "@standard-schema/spec": "^1.1.0", "fast-check": "^4.8.0", "find-my-way-ts": "^0.1.6", "ini": "^7.0.0", "kubernetes-types": "^1.30.0", "msgpackr": "^2.0.1", "multipasta": "^0.2.7", "toml": "^4.1.1", "uuid": "^14.0.0", "yaml": "^2.9.0" } }, "sha512-0wsak8RtgGAr9UWSbVDgJHZcUqMSvicHcvaZv1MbMM7MCGgW4Rn/137J1MHQbwYPcwYGxT/IqehFd+UbYuj78w=="], + "gaxios": ["gaxios@7.3.1", "", { "dependencies": { "extend": "^3.0.2", "https-proxy-agent": "^7.0.1", "node-fetch": "^3.3.2" } }, "sha512-kB3rzJV7d9juLZh8/56QTXCwQfxyhdOMdyYk1HdQKFtF8TJTDTZQJtixWIwXdE9Jji91mC41DUNpjleo4L4eAQ=="], - "fast-check": ["fast-check@4.8.0", "", { "dependencies": { "pure-rand": "^8.0.0" } }, "sha512-GOJ158CUMnN6cSahsv4+ExARvIDuzzinFjkp0E9WtiBa5zcVeLozVkWaE4IzFcc+Y48Wp1EDlUZsXRyAztQcSg=="], + "gcp-metadata": ["gcp-metadata@8.1.4", "", { "dependencies": { "gaxios": "7.1.3", "google-logging-utils": "1.1.3", "json-bigint": "^1.0.0" } }, "sha512-iJ9KMsiu+xKtNRX0PmGLSaIU3bUBAyzWTyqKemKPzNPsmmsBCQYmlNg+brEbES7IHSXtdVwzBPzx1vz3FAaipw=="], - "find-my-way-ts": ["find-my-way-ts@0.1.6", "", {}, "sha512-a85L9ZoXtNAey3Y6Z+eBWW658kO/MwR7zIafkIUPUMf3isZG0NCs2pjW2wtjxAKuJPxMAsHUIP4ZPGv0o5gyTA=="], + "glob": ["glob@13.0.5", "", { "dependencies": { "minimatch": "^10.2.1", "minipass": "^7.1.2", "path-scurry": "^2.0.0" } }, "sha512-BzXxZg24Ibra1pbQ/zE7Kys4Ua1ks7Bn6pKLkVPZ9FZe4JQS6/Q7ef3LG1H+k7lUf5l4T3PLSyYyYJVYUvfgTw=="], - "ini": ["ini@7.0.0", "", {}, "sha512-ifK0CgjALofS5bkrcTy4RaQ9Vx2Knf/eLeIO+NaswQEpH1UblrtTSCIvN71qQDMq0PeQ/SSPojvEJp9vvvfr+w=="], + "google-auth-library": ["google-auth-library@10.5.0", "", { "dependencies": { "base64-js": "^1.3.0", "ecdsa-sig-formatter": "^1.0.11", "gaxios": "^7.0.0", "gcp-metadata": "^8.0.0", "google-logging-utils": "^1.0.0", "gtoken": "^8.0.0", "jws": "^4.0.0" } }, "sha512-7ABviyMOlX5hIVD60YOfHw4/CxOfBhyduaYB+wbFWCWoni4N7SLcV46hrVRktuBbZjFC9ONyqamZITN7q3n32w=="], + + "google-logging-utils": ["google-logging-utils@1.2.0", "", {}, "sha512-WE9av4wKDZgRjBwgVUabocx8T6/7o3Ca1Fat46FXDhXVAFibzNadedcOXrdgd1Kzmk8tsk/9ZH89Wyf/SqeZ3A=="], + + "graceful-fs": ["graceful-fs@4.2.11", "", {}, "sha512-RbJ5/jmFcNNCcDV5o9eTnBLJ/HszWV0P73bc+Ff4nS/rJj+YaS6IGyiOL0VoBYX+l1Wrl3k63h/KrH+nhJ0XvQ=="], + + "gtoken": ["gtoken@8.0.0", "", { "dependencies": { "gaxios": "^7.0.0", "jws": "^4.0.0" } }, "sha512-+CqsMbHPiSTdtSO14O51eMNlrp9N79gmeqmXeouJOhfucAedHw9noVe/n5uJk3tbKE6a+6ZCQg3RPhVhHByAIw=="], + + "hosted-git-info": ["hosted-git-info@9.0.3", "", { "dependencies": { "lru-cache": "^11.1.0" } }, "sha512-Hc+ghLoSt6QaYZUv0WBiIvmMDZuZZ7oaDvdH8MbfOO4lOsxdXLEvuC6ePoGs9H1X9oCLyq6+NVN0MKqD+ydxyg=="], + + "http-cache-semantics": ["http-cache-semantics@4.3.0", "", {}, "sha512-M5t5LlJpS1UHMjvwRQVdFHvPISGeLAxNcrWuJkeGh0KxsqCHZ1O3NXZU/8x7cD0BDcGW8kapxMKTvwlqrNkHkA=="], + + "http-proxy-agent": ["http-proxy-agent@7.0.2", "", { "dependencies": { "agent-base": "^7.1.0", "debug": "^4.3.4" } }, "sha512-T1gkAiYYDWYx3V5Bmyu7HcfcvL7mUrTWiM6yOfa3PIphViJ/gFPbvidQ+veqSOHci/PxBcDabeUNCzpOODJZig=="], + + "https-proxy-agent": ["https-proxy-agent@7.0.6", "", { "dependencies": { "agent-base": "^7.1.2", "debug": "4" } }, "sha512-vK9P5/iUfdl95AI+JVyUuIcVtd4ofvtrOr3HNtM2yxC9bnMbEdp3x01OhQNnjb8IJYi38VlTE3mBXwcfvywuSw=="], + + "iconv-lite": ["iconv-lite@0.7.3", "", { "dependencies": { "safer-buffer": ">= 2.1.2 < 3.0.0" } }, "sha512-IKXpvIzjnC9XTAUbVBcMfGS0EPaIXtW6v+zr+RRp+hqULEpo0owZax6wyRwPOJbWbzjYspQwusTsfVr0ifh4uQ=="], + + "ignore-walk": ["ignore-walk@8.0.0", "", { "dependencies": { "minimatch": "^10.0.3" } }, "sha512-FCeMZT4NiRQGh+YkeKMtWrOmBgWjHjMJ26WQWrRQyoyzqevdaGSakUaJW5xQYmjLlUVk2qUnCjYVBax9EKKg8A=="], + + "ini": ["ini@6.0.0", "", {}, "sha512-IBTdIkzZNOpqm7q3dRqJvMaldXjDHWkEDfrwGEQTs5eaQMWV+djAhR+wahyNNMAa+qpbDUhBMVt4ZKNwpPm7xQ=="], + + "ip-address": ["ip-address@10.7.4", "", {}, "sha512-DWBMKyeM25D70nrIiTkgQth59/G+naKOTep6huF9j+GmeK3DMD60Zvxg5d5qQnz+38AHgou6ean2LECX+lrKDg=="], + + "is-docker": ["is-docker@3.0.0", "", { "bin": { "is-docker": "cli.js" } }, "sha512-eljcgEDlEns/7AXFosB5K/2nCM4P7FQPkGc/DWLy5rmFEWvZayGrik1d9/QIY5nJ4f9YsVvBkA6kJpHn9rISdQ=="], + + "is-fullwidth-code-point": ["is-fullwidth-code-point@3.0.0", "", {}, "sha512-zymm5+u+sCsSWyD9qNaejV3DFvhCKclKdizYaJUuHA83RLjb7nSuGnddCHGv0hk+KY7BMAlsWeK4Ueg6EV6XQg=="], + + "is-in-ssh": ["is-in-ssh@1.0.0", "", {}, "sha512-jYa6Q9rH90kR1vKB6NM7qqd1mge3Fx4Dhw5TVlK1MUBqhEOuCagrEHMevNuCcbECmXZ0ThXkRm+Ymr51HwEPAw=="], + + "is-inside-container": ["is-inside-container@1.0.0", "", { "dependencies": { "is-docker": "^3.0.0" }, "bin": { "is-inside-container": "cli.js" } }, "sha512-KIYLCCJghfHZxqjYBE7rEy0OBuTd5xCHS7tHVgvCLkx7StIoaxwNW3hCALgEUjFfeRk+MG/Qxmp/vtETEF3tRA=="], + + "is-wsl": ["is-wsl@3.1.1", "", { "dependencies": { "is-inside-container": "^1.0.0" } }, "sha512-e6rvdUCiQCAuumZslxRJWR/Doq4VpPR82kqclvcS0efgt430SlGIk05vdCN58+VrzgtIcfNODjozVielycD4Sw=="], "isexe": ["isexe@2.0.0", "", {}, "sha512-RHxMLp9lnKHGHRng9QFhRCMbYAcVpn69smSGcq3f36xjgVVWThj4qqLbTLlq7Ssj8B+fIQ1EuCEGI2lKsyQeIw=="], + "jackspeak": ["jackspeak@3.4.3", "", { "dependencies": { "@isaacs/cliui": "^8.0.2" }, "optionalDependencies": { "@pkgjs/parseargs": "^0.11.0" } }, "sha512-OGlZQpz2yfahA/Rd1Y8Cd9SIEsqvXkLVoSw/cgwhnhFMDbsQFeZYoJJ7bIZBS9BcamUW96asq/npPWugM+RQBw=="], + + "json-bigint": ["json-bigint@1.0.0", "", { "dependencies": { "bignumber.js": "^9.0.0" } }, "sha512-SiPv/8VpZuWbvLSMtTDU8hEfrZWg/mH/nV/b4o0CYbSxu1UIQPLdwKOCIyLQX+VIPO5vrLX3i8qtqFyhdPSUSQ=="], + + "json-parse-even-better-errors": ["json-parse-even-better-errors@5.0.0", "", {}, "sha512-ZF1nxZ28VhQouRWhUcVlUIN3qwSgPuswK05s/HIaoetAoE/9tngVmCHjSxmSQPav1nd+lPtTL0YZ/2AFdR/iYQ=="], + "json-schema": ["json-schema@0.4.0", "", {}, "sha512-es94M3nTIfsEPisRafak+HDLfHXnKBhV3vU5eqPcS3flIWqcxJWgXHXiey3YrpaNsanY5ei1VoYEbOzijuq9BA=="], - "kubernetes-types": ["kubernetes-types@1.30.0", "", {}, "sha512-Dew1okvhM/SQcIa2rcgujNndZwU8VnSapDgdxlYoB84ZlpAD43U6KLAFqYo17ykSFGHNPrg0qry0bP+GJd9v7Q=="], + "json-stringify-nice": ["json-stringify-nice@1.1.4", "", {}, "sha512-5Z5RFW63yxReJ7vANgW6eZFGWaQvnPE3WNmZoOJrSkGju2etKA2L5rrOa1sm877TVTFt57A80BH1bArcmlLfPw=="], + + "jsonparse": ["jsonparse@1.3.1", "", {}, "sha512-POQXvpdL69+CluYsillJ7SUhKvytYjW9vG/GKpnf+xP8UWgYEM/RaMzHHofbALDiKbbP1W8UEYmgGl39WkPZsg=="], + + "just-diff": ["just-diff@6.0.2", "", {}, "sha512-S59eriX5u3/QhMNq3v/gm8Kd0w8OS6Tz2FS1NG4blv+z0MuQcBRJyFWjdovM0Rad4/P4aUPFtnkNjMjyMlMSYA=="], + + "just-diff-apply": ["just-diff-apply@5.5.0", "", {}, "sha512-OYTthRfSh55WOItVqwpefPtNt2VdKsq5AnAK6apdtR6yCH8pr0CmSr710J0Mf+WdQy7K/OzMy7K2MgAfdQURDw=="], + + "jwa": ["jwa@2.0.1", "", { "dependencies": { "buffer-equal-constant-time": "^1.0.1", "ecdsa-sig-formatter": "1.0.11", "safe-buffer": "^5.0.1" } }, "sha512-hRF04fqJIP8Abbkq5NKGN0Bbr3JxlQ+qhZufXVr0DvujKy93ZCbXZMHDL4EOtodSbCWxOqR8MS1tXA5hwqCXDg=="], + + "jws": ["jws@4.0.1", "", { "dependencies": { "jwa": "^2.0.1", "safe-buffer": "^5.0.1" } }, "sha512-EKI/M/yqPncGUUh44xz0PxSidXFr/+r0pA70+gIYhjv+et7yxM+s29Y+VGDkovRofQem0fs7Uvf4+YmAdyRduA=="], + + "lru-cache": ["lru-cache@11.5.3", "", {}, "sha512-U4N8FgzmWxc8k1VH8Kr6lQg18U7Fjvby6wXHVRX/ZZ7IwWbRMgrRbP0Wrb5q5NVinryp4SQampHKdvtecItxUg=="], + + "make-fetch-happen": ["make-fetch-happen@15.0.6", "", { "dependencies": { "@gar/promise-retry": "^1.0.0", "@npmcli/agent": "^4.0.0", "@npmcli/redact": "^4.0.0", "cacache": "^20.0.1", "http-cache-semantics": "^4.1.1", "minipass": "^7.0.2", "minipass-fetch": "^5.0.0", "minipass-flush": "^1.0.5", "minipass-pipeline": "^1.2.4", "negotiator": "^1.0.0", "proc-log": "^6.0.0", "ssri": "^13.0.0" } }, "sha512-Je0fLJ0F5atA7F+eIlLzk+Wkcl57JDf4kf+EW8xiP5E31xOQxkIxTbgf1Oi1Lw9tRI9UEMRdI5Vz2xTzoNU1Jw=="], + + "mime-db": ["mime-db@1.54.0", "", {}, "sha512-aU5EJuIN2WDemCcAp2vFBfp/m4EAhWJnUNSSw0ixs7/kXbd6Pg64EmwJkNdFhB8aWt1sH2CTXrLxo/iAGV3oPQ=="], + + "mime-types": ["mime-types@3.0.2", "", { "dependencies": { "mime-db": "^1.54.0" } }, "sha512-Lbgzdk0h4juoQ9fCKXW4by0UJqj+nOOrI9MJ1sSj4nI8aI2eo1qmvQEie4VD1glsS250n15LsWsYtCugiStS5A=="], + + "minimatch": ["minimatch@10.2.5", "", { "dependencies": { "brace-expansion": "^5.0.5" } }, "sha512-MULkVLfKGYDFYejP07QOurDLLQpcjk7Fw+7jXS2R2czRQzR56yHRveU5NDJEOviH+hETZKSkIk5c+T23GjFUMg=="], + + "minipass": ["minipass@7.1.3", "", {}, "sha512-tEBHqDnIoM/1rXME1zgka9g6Q2lcoCkxHLuc7ODJ5BxbP5d4c2Z5cGgtXAku59200Cx7diuHTOYfSBD8n6mm8A=="], + + "minipass-collect": ["minipass-collect@2.0.1", "", { "dependencies": { "minipass": "^7.0.3" } }, "sha512-D7V8PO9oaz7PWGLbCACuI1qEOsq7UKfLotx/C0Aet43fCUB/wfQ7DYeq2oR/svFJGYDHPr38SHATeaj/ZoKHKw=="], - "msgpackr": ["msgpackr@2.0.4", "", { "optionalDependencies": { "msgpackr-extract": "^3.0.4" } }, "sha512-o1C5KRmuRt+apqMr1HuGSqWStZoRBUpEsCsl15uM9VdAF1qHLtvMOU2En747EnTyEl6c4pzPewRMFF31s1CNbA=="], + "minipass-fetch": ["minipass-fetch@5.0.2", "", { "dependencies": { "minipass": "^7.0.3", "minipass-sized": "^2.0.0", "minizlib": "^3.0.1" }, "optionalDependencies": { "iconv-lite": "^0.7.2" } }, "sha512-2d0q2a8eCi2IRg/IGubCNRJoYbA1+YPXAzQVRFmB45gdGZafyivnZ5YSEfo3JikbjGxOdntGFvBQGqaSMXlAFQ=="], - "msgpackr-extract": ["msgpackr-extract@3.0.4", "", { "dependencies": { "node-gyp-build-optional-packages": "5.2.2" }, "optionalDependencies": { "@msgpackr-extract/msgpackr-extract-darwin-arm64": "3.0.4", "@msgpackr-extract/msgpackr-extract-darwin-x64": "3.0.4", "@msgpackr-extract/msgpackr-extract-linux-arm": "3.0.4", "@msgpackr-extract/msgpackr-extract-linux-arm64": "3.0.4", "@msgpackr-extract/msgpackr-extract-linux-x64": "3.0.4", "@msgpackr-extract/msgpackr-extract-win32-x64": "3.0.4" }, "bin": { "download-msgpackr-prebuilds": "bin/download-prebuilds.js" } }, "sha512-4kmO/MdyUIkLIvTPr8VHLil4AtoKIoniWPIEk5+CDy0xnWC84azhSFmuJ7PxZdsYtiP5kEeQsORAVIeMgxT+Hw=="], + "minipass-flush": ["minipass-flush@1.0.7", "", { "dependencies": { "minipass": "^3.0.0" } }, "sha512-TbqTz9cUwWyHS2Dy89P3ocAGUGxKjjLuR9z8w4WUTGAVgEj17/4nhgo2Du56i0Fm3Pm30g4iA8Lcqctc76jCzA=="], - "multipasta": ["multipasta@0.2.7", "", {}, "sha512-KPA58d68KgGil15oDqXjkUBEBYc00XvbPj5/X+dyzeo/lWm9Nc25pQRlf1D+gv4OpK7NM0J1odrbu9JNNGvynA=="], + "minipass-pipeline": ["minipass-pipeline@1.2.4", "", { "dependencies": { "minipass": "^3.0.0" } }, "sha512-xuIq7cIOt09RPRJ19gdi4b+RiNvDFYe5JH+ggNvBqGqpQXcru3PcRmOZuHBKWK1Txf9+cQ+HMVN4d6z46LZP7A=="], - "node-gyp-build-optional-packages": ["node-gyp-build-optional-packages@5.2.2", "", { "dependencies": { "detect-libc": "^2.0.1" }, "bin": { "node-gyp-build-optional-packages": "bin.js", "node-gyp-build-optional-packages-optional": "optional.js", "node-gyp-build-optional-packages-test": "build-test.js" } }, "sha512-s+w+rBWnpTMwSFbaE0UXsRlg7hU4FjekKU4eyAih5T8nJuNZT1nNsskXpxmeqSK9UzkBl6UgRlnKc8hz8IEqOw=="], + "minipass-sized": ["minipass-sized@2.0.0", "", { "dependencies": { "minipass": "^7.1.2" } }, "sha512-zSsHhto5BcUVM2m1LurnXY6M//cGhVaegT71OfOXoprxT6o780GZd792ea6FfrQkuU4usHZIUczAQMRUE2plzA=="], + + "minizlib": ["minizlib@3.1.0", "", { "dependencies": { "minipass": "^7.1.2" } }, "sha512-KZxYo1BUkWD2TVFLr0MQoM8vUUigWD3LlD83a/75BqC+4qE0Hb1Vo5v1FgcfaNXvfXzr+5EhQ6ing/CaBijTlw=="], + + "ms": ["ms@2.1.3", "", {}, "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA=="], + + "negotiator": ["negotiator@1.1.0", "", { "dependencies": { "content-type": "^2.1.0" } }, "sha512-NMPBRMJgiQHjbd8phG3Vebdx4kZ1H121rbl5IkMqeOsahptB9BKo/d7oJ3zTXqTgagn2bWlNSXkh0QUGM31RYg=="], + + "node-domexception": ["node-domexception@1.0.0", "", {}, "sha512-/jKZoMpw0F8GRwl4/eLROPA3cfcXtLApP0QzLmUT/HuPCZWyB7IY9ZrMeKw2O/nFIqPQB3PVM9aYm0F312AXDQ=="], + + "node-fetch": ["node-fetch@3.3.2", "", { "dependencies": { "data-uri-to-buffer": "^4.0.0", "fetch-blob": "^3.1.4", "formdata-polyfill": "^4.0.10" } }, "sha512-dRB78srN/l6gqWulah9SrxeYnxeddIG30+GOqK/9OlLVyLg3HPnr6SqOWTWOXKRwC2eGYCkZ59NNuSgvSrpgOA=="], + + "node-gyp": ["node-gyp@12.4.0", "", { "dependencies": { "env-paths": "^2.2.0", "exponential-backoff": "^3.1.1", "graceful-fs": "^4.2.6", "nopt": "^9.0.0", "proc-log": "^6.0.0", "semver": "^7.3.5", "tar": "^7.5.4", "tinyglobby": "^0.2.12", "undici": "^6.25.0", "which": "^6.0.0" }, "bin": { "node-gyp": "bin/node-gyp.js" } }, "sha512-OMcPNvqTCFUnNaBlmdgq+lfNqY7gTiSmNRDjY3uAXRyudeKZEZxu3CLtjMQrx4zZxCX2b/mpNqTtwuCJgXhHkw=="], + + "nopt": ["nopt@9.0.0", "", { "dependencies": { "abbrev": "^4.0.0" }, "bin": { "nopt": "bin/nopt.js" } }, "sha512-Zhq3a+yFKrYwSBluL4H9XP3m3y5uvQkB/09CwDruCiRmR/UJYnn9W4R48ry0uGC70aeTPKLynBtscP9efFFcPw=="], + + "npm-bundled": ["npm-bundled@5.0.0", "", { "dependencies": { "npm-normalize-package-bin": "^5.0.0" } }, "sha512-JLSpbzh6UUXIEoqPsYBvVNVmyrjVZ1fzEFbqxKkTJQkWBO3xFzFT+KDnSKQWwOQNbuWRwt5LSD6HOTLGIWzfrw=="], + + "npm-install-checks": ["npm-install-checks@8.0.0", "", { "dependencies": { "semver": "^7.1.1" } }, "sha512-ScAUdMpyzkbpxoNekQ3tNRdFI8SJ86wgKZSQZdUxT+bj0wVFpsEMWnkXP0twVe1gJyNF5apBWDJhhIbgrIViRA=="], + + "npm-normalize-package-bin": ["npm-normalize-package-bin@5.0.0", "", {}, "sha512-CJi3OS4JLsNMmr2u07OJlhcrPxCeOeP/4xq67aWNai6TNWWbTrlNDgl8NcFKVlcBKp18GPj+EzbNIgrBfZhsag=="], + + "npm-package-arg": ["npm-package-arg@13.0.2", "", { "dependencies": { "hosted-git-info": "^9.0.0", "proc-log": "^6.0.0", "semver": "^7.3.5", "validate-npm-package-name": "^7.0.0" } }, "sha512-IciCE3SY3uE84Ld8WZU23gAPPV9rIYod4F+rc+vJ7h7cwAJt9Vk6TVsK60ry7Uj3SRS3bqRRIGuTp9YVlk6WNA=="], + + "npm-packlist": ["npm-packlist@10.0.4", "", { "dependencies": { "ignore-walk": "^8.0.0", "proc-log": "^6.0.0" } }, "sha512-uMW73iajD8hiH4ZBxEV3HC+eTnppIqwakjOYuvgddnalIw2lJguKviK1pcUJDlIWm1wSJkchpDZDSVVsZEYRng=="], + + "npm-pick-manifest": ["npm-pick-manifest@11.0.3", "", { "dependencies": { "npm-install-checks": "^8.0.0", "npm-normalize-package-bin": "^5.0.0", "npm-package-arg": "^13.0.0", "semver": "^7.3.5" } }, "sha512-buzyCfeoGY/PxKqmBqn1IUJrZnUi1VVJTdSSRPGI60tJdUhUoSQFhs0zycJokDdOznQentgrpf8LayEHyyYlqQ=="], + + "npm-registry-fetch": ["npm-registry-fetch@19.1.1", "", { "dependencies": { "@npmcli/redact": "^4.0.0", "jsonparse": "^1.3.1", "make-fetch-happen": "^15.0.0", "minipass": "^7.0.2", "minipass-fetch": "^5.0.0", "minizlib": "^3.0.1", "npm-package-arg": "^13.0.0", "proc-log": "^6.0.0" } }, "sha512-TakBap6OM1w0H73VZVDf44iFXsOS3h+L4wVMXmbWOQroZgFhMch0juN6XSzBNlD965yIKvWg2dfu7NSiaYLxtw=="], + + "open": ["open@11.0.4", "", { "dependencies": { "default-browser": "^5.5.1", "define-lazy-prop": "^3.0.0", "is-in-ssh": "^1.0.0", "is-inside-container": "^1.0.0", "powershell-utils": "^0.2.1", "wsl-utils": "^1.0.0" } }, "sha512-++Zlftm0kVLPmzC06t6epuWmcRMDbI4z5P3NNX979WA/k23+NtSOynEGzsVfZwguKw2mi5umVgnBlJQMwRz4Pg=="], + + "p-map": ["p-map@7.1.1", "", {}, "sha512-6Lh3jQPLthYujSrlw6ccWXBGZTkoTCz27Hd92g79LH/kjAbgoEu0t442cCCNbegPSJsA0lwDX8z0bcKtkpA32g=="], + + "package-json-from-dist": ["package-json-from-dist@1.0.1", "", {}, "sha512-UEZIS3/by4OC8vL3P2dTXRETpebLI2NiI5vIrjaD/5UtrkFX/tNbwjTSRAGC/+7CAo2pIcBaRgWmcBBHcsaCIw=="], + + "pacote": ["pacote@21.5.1", "", { "dependencies": { "@gar/promise-retry": "^1.0.0", "@npmcli/git": "^7.0.0", "@npmcli/installed-package-contents": "^4.0.0", "@npmcli/package-json": "^7.0.0", "@npmcli/promise-spawn": "^9.0.0", "@npmcli/run-script": "^10.0.0", "cacache": "^20.0.0", "fs-minipass": "^3.0.0", "minipass": "^7.0.2", "npm-package-arg": "^13.0.0", "npm-packlist": "^10.0.1", "npm-pick-manifest": "^11.0.1", "npm-registry-fetch": "^19.0.0", "proc-log": "^6.0.0", "sigstore": "^4.0.0", "ssri": "^13.0.0", "tar": "^7.4.3" }, "bin": { "pacote": "bin/index.js" } }, "sha512-KvcJ9iy3crysCsgqc4+PknH/w6jkrp8JN36mpZBPwNaDRwTfMZD37YzRazNstiZUOhuF5pno9f78n9mEJBavwg=="], + + "parse-conflict-json": ["parse-conflict-json@5.0.1", "", { "dependencies": { "json-parse-even-better-errors": "^5.0.0", "just-diff": "^6.0.0", "just-diff-apply": "^5.2.0" } }, "sha512-ZHEmNKMq1wyJXNwLxyHnluPfRAFSIliBvbK/UiOceROt4Xh9Pz0fq49NytIaeaCUf5VR86hwQ/34FCcNU5/LKQ=="], "path-key": ["path-key@3.1.1", "", {}, "sha512-ojmeN0qd+y0jszEtoY48r0Peq5dwMEkIlCOu6Q5f41lfkswXuKtYrhgoTpLnyIcHm24Uhqx+5Tqm2InSwLhE6Q=="], - "pure-rand": ["pure-rand@8.4.0", "", {}, "sha512-IoM8YF/jY0hiugFo/wOWqfmarlE6J0wc6fDK1PhftMk7MGhVZl88sZimmqBBFomLOCSmcCCpsfj7wXASCpvK9A=="], + "path-scurry": ["path-scurry@2.0.2", "", { "dependencies": { "lru-cache": "^11.0.0", "minipass": "^7.1.2" } }, "sha512-3O/iVVsJAPsOnpwWIeD+d6z/7PmqApyQePUtCndjatj/9I5LylHvt5qluFaBT3I5h3r1ejfR056c+FCv+NnNXg=="], + + "picomatch": ["picomatch@4.0.7", "", {}, "sha512-qcJu88Q2IWqJsDD529JKMdwGm/dvInW4HvQnRwiH9JtihJvzGOscDtHE3x1pBKeUOTysQ8kVmLnJ2kJu7yhcGA=="], + + "postcss-selector-parser": ["postcss-selector-parser@7.1.7", "", { "dependencies": { "cssesc": "^3.0.0", "util-deprecate": "^1.0.2" } }, "sha512-wWBro/7hJ40ye6TPiXkUQoXKCw23xSsjriUvqd7Uls9Sso3oLiON1flc18cCdnjgCRXw4fBVqUJJeOdDTh3P3g=="], + + "powershell-utils": ["powershell-utils@0.2.1", "", {}, "sha512-C+y9x90UElAddDZmV4qOx9W53B61PO7cIqWz2dQsWlwswuq4mr8NEwytdGKboYbQlGZ3awrkTeNvcZiZNHnQ8A=="], + + "proc-log": ["proc-log@6.1.0", "", {}, "sha512-iG+GYldRf2BQ0UDUAd6JQ/RwzaQy6mXmsk/IzlYyal4A4SNFw54MeH4/tLkF4I5WoWG9SQwuqWzS99jaFQHBuQ=="], + + "proggy": ["proggy@4.0.0", "", {}, "sha512-MbA4R+WQT76ZBm/5JUpV9yqcJt92175+Y0Bodg3HgiXzrmKu7Ggq+bpn6y6wHH+gN9NcyKn3yg1+d47VaKwNAQ=="], + + "promise-all-reject-late": ["promise-all-reject-late@1.0.1", "", {}, "sha512-vuf0Lf0lOxyQREH7GDIOUMLS7kz+gs8i6B+Yi8dC68a2sychGrHTJYghMBD6k7eUcH0H5P73EckCA48xijWqXw=="], + + "promise-call-limit": ["promise-call-limit@3.0.2", "", {}, "sha512-mRPQO2T1QQVw11E7+UdCJu7S61eJVWknzml9sC1heAdj1jxl0fWMBypIt9ZOcLFf8FkG995ZD7RnVk7HH72fZw=="], + + "read-cmd-shim": ["read-cmd-shim@6.0.0", "", {}, "sha512-1zM5HuOfagXCBWMN83fuFI/x+T/UhZ7k+KIzhrHXcQoeX5+7gmaDYjELQHmmzIodumBHeByBJT4QYS7ufAgs7A=="], + + "redis": ["redis@6.3.0", "", { "dependencies": { "@redis/bloom": "6.3.0", "@redis/client": "6.3.0", "@redis/json": "6.3.0", "@redis/search": "6.3.0", "@redis/time-series": "6.3.0" } }, "sha512-XFQbPie1lGpKeUZ8ySYY43yLnQy/iIQtHNvIr/1XdaABpb96rUSdm3Mfl+98FEVbv9ZJIzINMno5s0Nh8E5LEw=="], + + "rimraf": ["rimraf@5.0.10", "", { "dependencies": { "glob": "^10.3.7" }, "bin": { "rimraf": "dist/esm/bin.mjs" } }, "sha512-l0OE8wL34P4nJH/H2ffoaniAokM2qSmrtXHmlpvYr5AVVX8msAyW0l8NVJFDxlSK4u3Uh/f41cQheDVdnYijwQ=="], + + "run-applescript": ["run-applescript@7.1.0", "", {}, "sha512-DPe5pVFaAsinSaV6QjQ6gdiedWDcRCbUuiQfQa2wmWV7+xC9bGulGI8+TdRmoFkAPaBXk8CrAbnlY2ISniJ47Q=="], + + "safe-buffer": ["safe-buffer@5.2.1", "", {}, "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ=="], + + "safer-buffer": ["safer-buffer@2.1.2", "", {}, "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg=="], "saxes": ["saxes@6.0.0", "", { "dependencies": { "xmlchars": "^2.2.0" } }, "sha512-xAg7SOnEhrm5zI3puOOKyy1OMcMlIJZYNJY7xLBwSze0UjhPLnWfj2GF2EpT0jmzaJKIWKHLsaSSajf35bcYnA=="], + "semver": ["semver@7.8.5", "", { "bin": { "semver": "bin/semver.js" } }, "sha512-Y7/KDsb8LjooZpwaqGyulO6DQlksgCncchHGk+sZIY4SBvUocMBEFH5Ur1fI4dV+Jvl0w6cjvucaIi40puRioA=="], + "shebang-command": ["shebang-command@2.0.0", "", { "dependencies": { "shebang-regex": "^3.0.0" } }, "sha512-kHxr2zZpYtdmrN1qDjrrX/Z1rR1kG8Dx+gkpK1G4eXmvXswmcE1hTWBWYUzlraYw1/yZp6YuDY77YtvbN0dmDA=="], "shebang-regex": ["shebang-regex@3.0.0", "", {}, "sha512-7++dFhtcx3353uBaq8DDR4NuxBetBzC7ZQOhmTQInHEd6bSrXdiEyzCvG07Z44UYdLShWUyXt5M/yhz8ekcb1A=="], - "toml": ["toml@4.3.0", "", {}, "sha512-lVb8X9BsPVuH0M4BKeS91tXAmJvCjQ5UIyAbQFaxkKGyUFK2RPkhwaFSQH8vbpl1d23eu/IBH+dwVMHWaq9A5A=="], + "signal-exit": ["signal-exit@4.1.0", "", {}, "sha512-bzyZ1e88w9O1iNJbKnOlvYTrWPDl46O1bG0D3XInv+9tkPrxrN8jUUTiFlDkkmKWgn1M6CfIA13SuGqOa9Korw=="], + + "sigstore": ["sigstore@4.1.1", "", { "dependencies": { "@sigstore/bundle": "^4.0.0", "@sigstore/core": "^3.2.1", "@sigstore/protobuf-specs": "^0.5.0", "@sigstore/sign": "^4.1.1", "@sigstore/tuf": "^4.0.2", "@sigstore/verify": "^3.1.1" } }, "sha512-endqECJkfhozrXMK5ngu/UAA0xVcVEFdnHJCElGaExypjW+HK5i6zu3NteLoaX/iFbRUbC3+DjttQs0GARr+5w=="], + + "smart-buffer": ["smart-buffer@4.2.0", "", {}, "sha512-94hK0Hh8rPqQl2xXc3HsaBoOXKV20MToPkcXvwbISWLEs+64sBq5kFgn2kJDHb1Pry9yrP0dxrCI9RRci7RXKg=="], + + "socks": ["socks@2.8.10", "", { "dependencies": { "ip-address": "^10.1.1", "smart-buffer": "^4.2.0" } }, "sha512-e0VyvkVTwVYViNovRkZ9aodhxVlyoMn7eJhVUPxZ+eK9P/7CBkxvvsBOHqFPEH416726W8tLXXXjKwqgTErrCQ=="], + + "socks-proxy-agent": ["socks-proxy-agent@8.0.5", "", { "dependencies": { "agent-base": "^7.1.2", "debug": "^4.3.4", "socks": "^2.8.3" } }, "sha512-HehCEsotFqbPW9sJ8WVYB6UbmIMv7kUUORIF2Nncq4VQvBfNBLibW9YZR5dlYCSUhwcD628pRllm7n+E+YTzJw=="], + + "spdx-exceptions": ["spdx-exceptions@2.5.0", "", {}, "sha512-PiU42r+xO4UbUS1buo3LPJkjlO7430Xn5SVAhdpzzsPHsjbYVflnnFdATgabnLude+Cqu25p6N+g2lw/PFsa4w=="], + + "spdx-expression-parse": ["spdx-expression-parse@4.0.0", "", { "dependencies": { "spdx-exceptions": "^2.1.0", "spdx-license-ids": "^3.0.0" } }, "sha512-Clya5JIij/7C6bRR22+tnGXbc4VKlibKSVj2iHvVeX5iMW7s1SIQlqu699JkODJJIhh/pUu8L0/VLh8xflD+LQ=="], + + "spdx-license-ids": ["spdx-license-ids@3.0.24", "", {}, "sha512-cLS9TtWkIQFyLkJ3/5aFQAOHOSKTlOs/7WDut/XPSdjom1fJhUdkepeJAKXa9Y05+FabHd0sti+Et559vDtkpQ=="], + + "ssri": ["ssri@13.0.1", "", { "dependencies": { "minipass": "^7.0.3" } }, "sha512-QUiRf1+u9wPTL/76GTYlKttDEBWV1ga9ZXW8BG6kfdeyyM8LGPix9gROyg9V2+P0xNyF3X2Go526xKFdMZrHSQ=="], + + "string-width": ["string-width@5.1.2", "", { "dependencies": { "eastasianwidth": "^0.2.0", "emoji-regex": "^9.2.2", "strip-ansi": "^7.0.1" } }, "sha512-HnLOCR3vjcY8beoNLtcjZ5/nxn2afmME6lhrDrebokqMap+XbeW8n9TXpPDOqdGK5qcI3oT0GKTW6wC7EMiVqA=="], + + "string-width-cjs": ["string-width@4.2.3", "", { "dependencies": { "emoji-regex": "^8.0.0", "is-fullwidth-code-point": "^3.0.0", "strip-ansi": "^6.0.1" } }, "sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g=="], + + "strip-ansi": ["strip-ansi@7.2.0", "", { "dependencies": { "ansi-regex": "^6.2.2" } }, "sha512-yDPMNjp4WyfYBkHnjIRLfca1i6KMyGCtsVgoKe/z1+6vukgaENdgGBZt+ZmKPc4gavvEZ5OgHfHdrazhgNyG7w=="], + + "strip-ansi-cjs": ["strip-ansi@6.0.1", "", { "dependencies": { "ansi-regex": "^5.0.1" } }, "sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A=="], + + "tar": ["tar@7.5.22", "", { "dependencies": { "@isaacs/fs-minipass": "^4.0.0", "chownr": "^3.0.0", "minipass": "^7.1.2", "minizlib": "^3.1.0", "yallist": "^5.0.0" } }, "sha512-MFO/QzvtAOmJbkhOaCTvbGcFN9L9b+JunIsDwaKljSOdcLMea3NJ1k9Usz/rjdfSXTq4dfzfeS7W4p4YOAAHeA=="], + + "tinyglobby": ["tinyglobby@0.2.17", "", { "dependencies": { "fdir": "^6.5.0", "picomatch": "^4.0.4" } }, "sha512-wXR/dYpcqKmfWpEdZjiKJOwCNFndD0DMnrW/cYjVGttEkBfVgcLFHoNrlj47mjOVic9yyNu65alsgF4NQyTa2g=="], + + "treeverse": ["treeverse@3.0.0", "", {}, "sha512-gcANaAnd2QDZFmHFEOF4k7uc1J/6a6z3DJMd/QwEyxLoKGiptJRwid582r7QIsFlFMIZ3SnxfS52S4hm2DHkuQ=="], + + "tslib": ["tslib@2.8.1", "", {}, "sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w=="], + + "tuf-js": ["tuf-js@4.1.0", "", { "dependencies": { "@tufjs/models": "4.1.0", "debug": "^4.4.3", "make-fetch-happen": "^15.0.1" } }, "sha512-50QV99kCKH5P/Vs4E2Gzp7BopNV+KzTXqWeaxrfu5IQJBOULRsTIS9seSsOVT8ZnGXzCyx55nYWAi4qJzpZKEQ=="], "typescript": ["typescript@7.0.2", "", { "optionalDependencies": { "@typescript/typescript-aix-ppc64": "7.0.2", "@typescript/typescript-darwin-arm64": "7.0.2", "@typescript/typescript-darwin-x64": "7.0.2", "@typescript/typescript-freebsd-arm64": "7.0.2", "@typescript/typescript-freebsd-x64": "7.0.2", "@typescript/typescript-linux-arm": "7.0.2", "@typescript/typescript-linux-arm64": "7.0.2", "@typescript/typescript-linux-loong64": "7.0.2", "@typescript/typescript-linux-mips64el": "7.0.2", "@typescript/typescript-linux-ppc64": "7.0.2", "@typescript/typescript-linux-riscv64": "7.0.2", "@typescript/typescript-linux-s390x": "7.0.2", "@typescript/typescript-linux-x64": "7.0.2", "@typescript/typescript-netbsd-arm64": "7.0.2", "@typescript/typescript-netbsd-x64": "7.0.2", "@typescript/typescript-openbsd-arm64": "7.0.2", "@typescript/typescript-openbsd-x64": "7.0.2", "@typescript/typescript-sunos-x64": "7.0.2", "@typescript/typescript-win32-arm64": "7.0.2", "@typescript/typescript-win32-x64": "7.0.2" }, "bin": { "tsc": "bin/tsc" } }, "sha512-8FYau96o3NKOhbjKi/qNvG/W5jhzxkbdm5sj9AbZ/5T5sWqn3hJgLfGx27sRKZWTvyzCP8dLRBTf5tBTSRVUNA=="], + "undici": ["undici@8.12.3", "", {}, "sha512-+fdndUTwBP86cVjihTp21hZHo7d6oj1c/47XPA3lL8mM0XHs+C4EkcjmgRsFnFk2f/UONGDy9ItBItmXsLOQHw=="], + "undici-types": ["undici-types@8.9.0", "", {}, "sha512-KTDyRTYX8sWmKXAikPHHSyc63CRPETMctyjKFupcC6OBLXT3xsN0e9aF7m+mIXutFWpUXuedtowG7iLOzp0kQg=="], - "uuid": ["uuid@14.0.0", "", { "bin": { "uuid": "dist-node/bin/uuid" } }, "sha512-Qo+uWgilfSmAhXCMav1uYFynlQO7fMFiMVZsQqZRMIXp0O7rR7qjkj+cPvBHLgBqi960QCoo/PH2/6ZtVqKvrg=="], + "util-deprecate": ["util-deprecate@1.0.2", "", {}, "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw=="], + + "validate-npm-package-name": ["validate-npm-package-name@7.0.2", "", {}, "sha512-hVDIBwsRruT73PbK7uP5ebUt+ezEtCmzZz3F59BSr2F6OVFnJ/6h8liuvdLrQ88Xmnk6/+xGGuq+pG9WwTuy3A=="], + + "walk-up-path": ["walk-up-path@4.0.0", "", {}, "sha512-3hu+tD8YzSLGuFYtPRb48vdhKMi0KQV5sn+uWr8+7dMEq/2G/dtLrdDinkLjqq5TIbIBjYJ4Ax/n3YiaW7QM8A=="], + + "web-streams-polyfill": ["web-streams-polyfill@3.3.3", "", {}, "sha512-d2JWLCivmZYTSIoge9MsgFCZrt571BikcWGYkjC1khllbTeDlGqZ2D8vD8E/lJa8WGWbb7Plm8/XJYV7IJHZZw=="], "which": ["which@2.0.2", "", { "dependencies": { "isexe": "^2.0.0" }, "bin": { "node-which": "./bin/node-which" } }, "sha512-BLI3Tl1TW3Pvl70l3yq3Y64i+awpwXqsGBYWkkqMtnbXgrMD+yj7rhW0kuEDxzJaYXGjEW5ogapKNMEKNMjibA=="], + "wrap-ansi": ["wrap-ansi@8.1.0", "", { "dependencies": { "ansi-styles": "^6.1.0", "string-width": "^5.0.1", "strip-ansi": "^7.0.1" } }, "sha512-si7QWI6zUMq56bESFvagtmzMdGOtoxfR+Sez11Mobfc7tm+VkUckk9bW2UeffTGVUbOksxmSw0AA2gs8g71NCQ=="], + + "wrap-ansi-cjs": ["wrap-ansi@7.0.0", "", { "dependencies": { "ansi-styles": "^4.0.0", "string-width": "^4.1.0", "strip-ansi": "^6.0.0" } }, "sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q=="], + + "write-file-atomic": ["write-file-atomic@7.0.1", "", { "dependencies": { "signal-exit": "^4.0.1" } }, "sha512-OTIk8iR8/aCRWBqvxrzxR0hgxWpnYBblY1S5hDWBQfk/VFmJwzmJgQFN3WsoUKHISv2eAwe+PpbUzyL1CKTLXg=="], + + "ws": ["ws@8.22.0", "", { "peerDependencies": { "bufferutil": "^4.0.1", "utf-8-validate": ">=5.0.2" }, "optionalPeers": ["bufferutil", "utf-8-validate"] }, "sha512-Ydggc987+RO0AnWtZ/7Wq9FtNvcrL1b/RO0ud9mWjUPgDrsAAwQSF51sm2hm1XofbU/4jkpGEsLFsZZxU+1DOg=="], + + "wsl-utils": ["wsl-utils@1.0.1", "", { "dependencies": { "is-wsl": "^3.1.0", "powershell-utils": "^0.2.0" } }, "sha512-c16PKLIRxBYpR3jSWsdS7SFp0COm5SOQdiFjY818dYFHJbMGuLBszpF07LBWmOS1uzrHa9VGdynsEgoeKy0oOQ=="], + "xmlchars": ["xmlchars@2.2.0", "", {}, "sha512-JZnDKK8B0RCDw84FNdDAIpZK+JuJw+s7Lz8nksI7SIuU3UXJJslUthsi+uWBUYOwPFwW7W7PRLRfUKpxjtjFCw=="], - "yaml": ["yaml@2.9.0", "", { "bin": { "yaml": "bin.mjs" } }, "sha512-2AvhNX3mb8zd6Zy7INTtSpl1F15HW6Wnqj0srWlkKLcpYl/gMIMJiyuGq2KeI2YFxUPjdlB+3Lc10seMLtL4cA=="], + "yallist": ["yallist@5.0.0", "", {}, "sha512-YgvUTfwqyc7UXVMrB+SImsVYSmTS8X/tSrtdNZMImM+n7+QTriRXyXim0mBrTXNeqzVF0KWGgHPeiyViFFrNDw=="], + + "zod": ["zod@4.6.7", "", {}, "sha512-g8W2D2kU6a8untc053fX1uVBKzM7yqAxW/3zwk83+6RyF2+SKOuV+c0wBDhUwzV1PaGapmZH+rmcUzPf+3CGwg=="], + + "@aws-crypto/sha256-browser/@smithy/util-utf8": ["@smithy/util-utf8@2.3.0", "", { "dependencies": { "@smithy/util-buffer-from": "^2.2.0", "tslib": "^2.6.2" } }, "sha512-R8Rdn8Hy72KKcebgLiv8jQcQkXoLMOGGv5uI1/k0l+snqkOzQ1R0ChUBCxWMlBsFMekWjq0wRudIweFs7sKT5A=="], + + "@aws-crypto/util/@smithy/util-utf8": ["@smithy/util-utf8@2.3.0", "", { "dependencies": { "@smithy/util-buffer-from": "^2.2.0", "tslib": "^2.6.2" } }, "sha512-R8Rdn8Hy72KKcebgLiv8jQcQkXoLMOGGv5uI1/k0l+snqkOzQ1R0ChUBCxWMlBsFMekWjq0wRudIweFs7sKT5A=="], + + "@npmcli/git/which": ["which@6.0.1", "", { "dependencies": { "isexe": "^4.0.0" }, "bin": { "node-which": "bin/which.js" } }, "sha512-oGLe46MIrCRqX7ytPUf66EAYvdeMIZYn3WaocqqKZAxrBpkqHfL/qvTyJ/bTk5+AqHCjXmrv3CEWgy368zhRUg=="], + + "@npmcli/promise-spawn/which": ["which@6.0.1", "", { "dependencies": { "isexe": "^4.0.0" }, "bin": { "node-which": "bin/which.js" } }, "sha512-oGLe46MIrCRqX7ytPUf66EAYvdeMIZYn3WaocqqKZAxrBpkqHfL/qvTyJ/bTk5+AqHCjXmrv3CEWgy368zhRUg=="], + + "gcp-metadata/gaxios": ["gaxios@7.1.3", "", { "dependencies": { "extend": "^3.0.2", "https-proxy-agent": "^7.0.1", "node-fetch": "^3.3.2", "rimraf": "^5.0.1" } }, "sha512-YGGyuEdVIjqxkxVH1pUTMY/XtmmsApXrCVv5EU25iX6inEPbV+VakJfLealkBtJN69AQmh1eGOdCl9Sm1UP6XQ=="], + + "gcp-metadata/google-logging-utils": ["google-logging-utils@1.1.3", "", {}, "sha512-eAmLkjDjAFCVXg7A1unxHsLf961m6y17QFqXqAXGj/gVkKFrEICfStRfwUlGNfeCEjNRa32JEWOUTlYXPyyKvA=="], + + "minipass-flush/minipass": ["minipass@3.3.6", "", { "dependencies": { "yallist": "^4.0.0" } }, "sha512-DxiNidxSEK+tHG6zOIklvNOwm3hvCrbUrdtzY74U6HKTJxvIDfOUL5W5P2Ghd3DTkhhKPYGqeNUIh5qcM4YBfw=="], + + "minipass-pipeline/minipass": ["minipass@3.3.6", "", { "dependencies": { "yallist": "^4.0.0" } }, "sha512-DxiNidxSEK+tHG6zOIklvNOwm3hvCrbUrdtzY74U6HKTJxvIDfOUL5W5P2Ghd3DTkhhKPYGqeNUIh5qcM4YBfw=="], + + "node-gyp/undici": ["undici@6.29.1", "", {}, "sha512-EvRlUt/QqAEvXdQeRMYUfwOQsfzpDztykrG2owGEE/rBOBoYv9uF39ZzsNDh4lTFHhTr2XOZjKkYgueD12ZmGA=="], + + "node-gyp/which": ["which@6.0.1", "", { "dependencies": { "isexe": "^4.0.0" }, "bin": { "node-which": "bin/which.js" } }, "sha512-oGLe46MIrCRqX7ytPUf66EAYvdeMIZYn3WaocqqKZAxrBpkqHfL/qvTyJ/bTk5+AqHCjXmrv3CEWgy368zhRUg=="], + + "rimraf/glob": ["glob@10.5.0", "", { "dependencies": { "foreground-child": "^3.1.0", "jackspeak": "^3.1.2", "minimatch": "^9.0.4", "minipass": "^7.1.2", "package-json-from-dist": "^1.0.0", "path-scurry": "^1.11.1" }, "bin": { "glob": "dist/esm/bin.mjs" } }, "sha512-DfXN8DfhJ7NH3Oe7cFmu3NCu1wKbkReJ8TorzSAFbSKrlNaQSKfIzqYqVY8zlbs2NLBbWpRiU52GX2PbaBVNkg=="], + + "string-width-cjs/emoji-regex": ["emoji-regex@8.0.0", "", {}, "sha512-MSjYzcWNOA0ewAHpz0MxpYFvwg6yjy1NG3xteoqz644VCo/RPgnr1/GGt+ic3iJTzQ8Eu3TdM14SawnVUmGE6A=="], + + "string-width-cjs/strip-ansi": ["strip-ansi@6.0.1", "", { "dependencies": { "ansi-regex": "^5.0.1" } }, "sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A=="], + + "strip-ansi-cjs/ansi-regex": ["ansi-regex@5.0.1", "", {}, "sha512-quJQXlTSUGL2LH9SUXo8VwsY4soanhgo6LNSm84E1LBcE8s3O0wpdiRzyR9z/ZZJMlMWv37qOOb9pdJlMUEKFQ=="], + + "wrap-ansi-cjs/ansi-styles": ["ansi-styles@4.3.0", "", { "dependencies": { "color-convert": "^2.0.1" } }, "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg=="], + + "wrap-ansi-cjs/string-width": ["string-width@4.2.3", "", { "dependencies": { "emoji-regex": "^8.0.0", "is-fullwidth-code-point": "^3.0.0", "strip-ansi": "^6.0.1" } }, "sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g=="], + + "wrap-ansi-cjs/strip-ansi": ["strip-ansi@6.0.1", "", { "dependencies": { "ansi-regex": "^5.0.1" } }, "sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A=="], + + "@aws-crypto/sha256-browser/@smithy/util-utf8/@smithy/util-buffer-from": ["@smithy/util-buffer-from@2.2.0", "", { "dependencies": { "@smithy/is-array-buffer": "^2.2.0", "tslib": "^2.6.2" } }, "sha512-IJdWBbTcMQ6DA0gdNhh/BwrLkDR+ADW5Kr1aZmd4k3DIF6ezMV4R2NIAmT08wQJ3yUK82thHWmC/TnK/wpMMIA=="], + + "@aws-crypto/util/@smithy/util-utf8/@smithy/util-buffer-from": ["@smithy/util-buffer-from@2.2.0", "", { "dependencies": { "@smithy/is-array-buffer": "^2.2.0", "tslib": "^2.6.2" } }, "sha512-IJdWBbTcMQ6DA0gdNhh/BwrLkDR+ADW5Kr1aZmd4k3DIF6ezMV4R2NIAmT08wQJ3yUK82thHWmC/TnK/wpMMIA=="], + + "@npmcli/git/which/isexe": ["isexe@4.0.0", "", {}, "sha512-FFUtZMpoZ8RqHS3XeXEmHWLA4thH+ZxCv2lOiPIn1Xc7CxrqhWzNSDzD+/chS/zbYezmiwWLdQC09JdQKmthOw=="], + + "@npmcli/promise-spawn/which/isexe": ["isexe@4.0.0", "", {}, "sha512-FFUtZMpoZ8RqHS3XeXEmHWLA4thH+ZxCv2lOiPIn1Xc7CxrqhWzNSDzD+/chS/zbYezmiwWLdQC09JdQKmthOw=="], + + "minipass-flush/minipass/yallist": ["yallist@4.0.0", "", {}, "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A=="], + + "minipass-pipeline/minipass/yallist": ["yallist@4.0.0", "", {}, "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A=="], + + "node-gyp/which/isexe": ["isexe@4.0.0", "", {}, "sha512-FFUtZMpoZ8RqHS3XeXEmHWLA4thH+ZxCv2lOiPIn1Xc7CxrqhWzNSDzD+/chS/zbYezmiwWLdQC09JdQKmthOw=="], + + "rimraf/glob/minimatch": ["minimatch@9.0.9", "", { "dependencies": { "brace-expansion": "^2.0.2" } }, "sha512-OBwBN9AL4dqmETlpS2zasx+vTeWclWzkblfZk7KTA5j3jeOONz/tRCnZomUyvNg83wL5Zv9Ss6HMJXAgL8R2Yg=="], + + "rimraf/glob/path-scurry": ["path-scurry@1.11.1", "", { "dependencies": { "lru-cache": "^10.2.0", "minipass": "^5.0.0 || ^6.0.2 || ^7.0.0" } }, "sha512-Xa4Nw17FS9ApQFJ9umLiJS4orGjm7ZzwUrwamcGQuHSzDyth9boKDaycYdDcZDuqYATXw4HFXgaqWTctW/v1HA=="], + + "string-width-cjs/strip-ansi/ansi-regex": ["ansi-regex@5.0.1", "", {}, "sha512-quJQXlTSUGL2LH9SUXo8VwsY4soanhgo6LNSm84E1LBcE8s3O0wpdiRzyR9z/ZZJMlMWv37qOOb9pdJlMUEKFQ=="], + + "wrap-ansi-cjs/string-width/emoji-regex": ["emoji-regex@8.0.0", "", {}, "sha512-MSjYzcWNOA0ewAHpz0MxpYFvwg6yjy1NG3xteoqz644VCo/RPgnr1/GGt+ic3iJTzQ8Eu3TdM14SawnVUmGE6A=="], + + "wrap-ansi-cjs/strip-ansi/ansi-regex": ["ansi-regex@5.0.1", "", {}, "sha512-quJQXlTSUGL2LH9SUXo8VwsY4soanhgo6LNSm84E1LBcE8s3O0wpdiRzyR9z/ZZJMlMWv37qOOb9pdJlMUEKFQ=="], + + "rimraf/glob/minimatch/brace-expansion": ["brace-expansion@2.1.7", "", { "dependencies": { "balanced-match": "^1.0.0" } }, "sha512-uZbew1NqdmPDTMJ8ah1y+b+9QEJrfkXFk3RcTQw3X0jW/xRUvFKsg1CfQdSYGdTbXZWExtU3J3ccxtnfw1Fi0g=="], - "zod": ["zod@4.4.3", "", {}, "sha512-ytENFjIJFl2UwYglde2jchW2Hwm4GJFLDiSXWdTrJQBIN9Fcyp7n4DhxJEiWNAJMV1/BqWfW/kkg71UDcHJyTQ=="], + "rimraf/glob/path-scurry/lru-cache": ["lru-cache@10.4.3", "", {}, "sha512-JNAzZcXrCt42VGLuYz0zfAzDfAvJWW6AfYlDBQyDV5DClI2m5sAmK+OIO7s59XfsRsWHp02jAJrRadPRGTt6SQ=="], - "@opencode-ai/plugin/zod": ["zod@4.1.8", "", {}, "sha512-5R1P+WwQqmmMIEACyzSvo4JXHY5WiAFHRMg+zBZKgKS+Q1viRa0C1hmUKtHltoIFKtIdki3pRxkmpP74jnNYHQ=="], + "rimraf/glob/minimatch/brace-expansion/balanced-match": ["balanced-match@1.0.2", "", {}, "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw=="], } } diff --git a/docs/adr/0008-bounded-auto-continuation.md b/docs/adr/0008-bounded-auto-continuation.md index 4430db28..63ddf8c2 100644 --- a/docs/adr/0008-bounded-auto-continuation.md +++ b/docs/adr/0008-bounded-auto-continuation.md @@ -1,5 +1,8 @@ # ADR 0008: Bounded Auto Continuation +The host integration is superseded by [ADR 0018](0018-opencode-v2-only.md). +Its V1 message and compaction fields describe the earlier host contract. + Date: 2026-07-23 ## Status diff --git a/docs/adr/0018-opencode-v2-only.md b/docs/adr/0018-opencode-v2-only.md new file mode 100644 index 00000000..f49f8306 --- /dev/null +++ b/docs/adr/0018-opencode-v2-only.md @@ -0,0 +1,54 @@ +# ADR 0018. Target OpenCode v2 only + +## Decision + +Flow 10.0.0 targets OpenCode v2 with 2.1.4 as the tested minimum. OpenCode 1 +support is removed in this release. Later 2.x releases require a compatibility +check before they enter the supported set. The peer range excludes OpenCode 3. +Finish or explicitly close active Flow sessions before the upgrade. + +The maintainer requested the immediate cutover. This deliberately waives the +earlier-announcement cadence for this host removal. No bridge release or dual +runtime entrypoint is introduced. Session v5 remains unchanged. + +## Host boundary + +Native v2 definitions register roles, commands, and tools. Application schemas +produce JSON Schema. Structured tool results retain Flow envelopes and guidance. + +Each primary request freezes consumed inputs and assistant bindings within a +live epoch. The prompt hook grants no authority. A tool must match the native session, assistant, agent, call, +workspace, and consumed-input snapshot. Asynchronous event arrival order does +not establish authority. Flow selects HTTP in provider, model, and variant +settings so each request stays observable. + +Automatic compaction can precede the first primary request. It preserves the +epoch of an accepted input only through the exact checked native control. +Its user-role checkpoint grants no approval. The input ledger owns reservations, +readiness, lifetime, and history; request attribution owns primary and tool +bindings. Readiness waits precede authority checks. Interrupted ordinary history +may summarize without an execution origin. Known automatic history cannot take +that fallback. Live ordinary continuations retain their checked origin. +Matched source failures revoke that source; stale unmatched callbacks cannot +revoke a fresh one. Authority never comes from summary text or survives restart. + +The model picker loads automatically with the server package. Server-owned +global preferences are separate from native configuration. Project plugin +options override global choices. Updates check revision, catalog, project, and +activity before changing one role. Role transforms refresh across loaded +instances. Explicit selection clears the role's reasoning variant. Reset +restores the configured base. The coding model stays under native selection. + +## Qualification + +Qualification retains all 17 cases on two provider routes. +Manager and reviewer use the same approved model within each cell. Recovery is +off. Candidate, canary, and publication retain the exact archive and unpacked +manifest identities. + +Historical 9.x pins, digests, transcripts, and qualification bundles remain +verifiable without an active OpenCode 1 implementation. Publication retains +existing approval gates. + +See [release qualification](../release-qualification.md) for the executable gates +and [automatic continuation](../automatic-continuation.md) for operator behavior. diff --git a/docs/automatic-continuation.md b/docs/automatic-continuation.md index 1b6894c7..092f4b75 100644 --- a/docs/automatic-continuation.md +++ b/docs/automatic-continuation.md @@ -2,15 +2,18 @@ `flow_status` adds process-local information without Session v5 state or authority. -- `autoContinuation` reports observed host parentage support. Unknown support is omitted. +- `autoContinuation` reports observed request-attribution support. Unknown support is omitted. - `autoDrive` reports the requesting host session's active, waiting, paused, or inactive state and reason. - `autoTiming` appears only in detail. It measures the latest invocation's wall time in active work or recognized user checkpoints. Paused time is excluded. It resets on reload and does not measure CPU or coding time. Owned validation and review preparation continue after authenticated progress. -Receipts retain manager lineage, capture, source, and named results. Stale evidence +Receipts retain the exact native assistant and call, capture, source, and named results. Stale evidence and required failures without an eligible prerequisite repair pause. Same-goal reversible repairs have no amendment count limit; each accepted amendment permits one repair handoff. Complete failed inspection observations can reach review. -Unchanged revisions never repeat a prompt. Restart discards the lease. +Unchanged revisions never repeat a prompt. Automatic compaction preserves a lease +only through its exact checked native control record. Real input or manual +compaction of automatic work revokes that interval. Interrupted ordinary history +can summarize without restoring execution authority. Restart discards the lease. Read [pause recovery](troubleshooting.md#flow-auto-stops-after-every-feature). diff --git a/docs/development.md b/docs/development.md index a2176a50..34cc2ad7 100644 --- a/docs/development.md +++ b/docs/development.md @@ -23,26 +23,32 @@ bun run build bun run package:smoke ``` -The opt-in real-host check launches the pinned `opencode-ai` package through -`bunx`. It requires registry access or a populated Bun cache, not a -separately installed OpenCode binary: +The opt-in real-host check launches the exact `@opencode/cli` host selected in +`evals/opencode-host.ts`. It uses temporary configuration and local scripted +provider responses. Registry access or a populated package cache is required. +The user's installed OpenCode binary is not the smoke host. -```bash +```sh bun run smoke:live ``` ### Bumping the pinned host -`@opencode-ai/plugin` and `zod` are on `.github/dependabot.yml`'s ignore list, -so they are raised by hand. The plugin pin is also the host version -`smoke:live` launches, so bumping it puts a new host under test: +The plugin, client, schema, and Zod dependencies are deliberately pinned. Update +their package and lockfile entries together when their contracts change. +The host launcher and release policy have independent explicit pins. SDK +package metadata does not choose the qualification host. -1. Move the `devDependencies` pin, and `peerDependencies` if the new version - falls outside the declared range. -2. Run `bun run check`, then `bun run smoke:live` for the real host. +1. Probe the selected host's native registration, identity, permissions, output, + prompt admission, compaction, and terminal behavior. +2. Update the ordinary host pin and the prospective release policy. Keep retained + release pins and policy hashes unchanged. +3. Run frozen install, `bun run check`, replay, audit, and packed live smoke. +4. Qualify changed package bytes through the full release process. -Widen the peer range only for a smoke-tested host; a wider range makes a -compatibility claim no check has run. +The peer range permits installation on 2.x. Support claims cover tested hosts. +The weekly workflow resolves the latest stable 2.x and reports compatibility +outside the blocking pinned-host CI check. ## Source layout @@ -55,7 +61,7 @@ compatibility claim no check has run. (cross-process session lock), `fs/workspace.ts` (session file protocol). - `src/platform/opencode/` owns OpenCode hooks, host schemas, commands, tools, validation capture, and the duplicate-runtime guard: `command-hook.ts` - (slash-command hook), `tool-guard.ts` (leadership and auto-drive guard + (native command registration), `tool-guard.ts` (leadership and auto-drive guard around the tools), `auto-drive-decision.ts` (pure idle-routing decision, `decideOnIdle`, run by `auto-drive.ts`), `plugin.ts` (wiring only). - `src/guidance/`, `skills/`, and prompt surfaces own concise workflow judgment. @@ -68,7 +74,8 @@ ports; the OpenCode platform composes outer layers. There is no distribution/activation subsystem, cache inventory, repair journal, or Flow-owned installer: OpenCode installs and loads the npm package -from its native plugin command and configuration. +from its native plugin command and configuration. Flow model preferences live +in a separate plugin-owned file and reload role transforms across instances. ## Change discipline @@ -110,7 +117,7 @@ when available, with sanitized evidence of: - worker start/end times with a positive common overlap; - assigned versus changed paths and any scope drift; -- permission prompts or denials and worker Bash calls; and +- permission prompts or denials and worker Shell calls; and - reviewer-owned `flow_feature_complete` submission. Every wave-behavior change needs this evidence when marked verified, but it is diff --git a/docs/guarantees.md b/docs/guarantees.md index 791acb21..31489d50 100644 --- a/docs/guarantees.md +++ b/docs/guarantees.md @@ -46,7 +46,7 @@ model judgments. Each tier names what can fail. ## Host-attested - The validated command's **exit code** and **output truncation flag**, read from - the host's own Bash metadata. A model cannot report these; a host that reports + the host's own Shell metadata. A model cannot report these; a host that reports neither yields a durable never-passing observation rather than a silent pass. - That the executed command's bytes equal the armed command. - The **host platform** each observation ran on, normalized from what the runtime @@ -80,7 +80,7 @@ The runtime cannot enforce these judgments. Read the review before trusting its checklist, and failed an unprovable claim instead of passing it conditionally. `unprovable-claim-refused` and `defect-fails-review` put work in front of it that should not pass; neither can force the review path. -- **Reviewer independence.** Without an explicit reviewer model from plugin tuple +- **Reviewer independence.** Without an explicit reviewer model from plugin options configuration or `OPENCODE_FLOW_REVIEWER_MODEL`, the reviewer shares the manager's model; independence rests on structure alone. - **Evidence completeness.** Whether an entry names the requested observation, @@ -99,7 +99,7 @@ The runtime cannot enforce these judgments. Read the review before trusting its - Plans saved before `plan.evidence` existed. They declare none and keep the older, weaker rules: `broad` is the claimant's word, and no acceptance observation is owed. This build does not hydrate `gate` or `externalEvidence`. -- Worker file boundaries beyond `.flow`, `.git`, and Bash denial. Exact per-slice +- Worker file boundaries beyond `.flow`, `.git`, and Shell denial. Exact per-slice write paths are a prompt contract the manager audits afterward. - `/flow-auto` continuation across model turns, which depends on an unversioned host surface. Capability-gated and best-effort: see @@ -114,8 +114,8 @@ Who can break a run, and what each is up against. it. What Flow enforces is that the lifecycle cannot be talked into existence. An armed validation command runs byte-for-byte or the observation is ineligible, the exit code and output-completeness flag come from the host's - Bash metadata, and only the reserved reviewer identity submits a review result. - The hidden worker and reviewer agents do get a deny matrix: no Bash, no + Shell metadata, and only the reserved reviewer identity submits a review result. + The hidden worker and reviewer agents do get a deny matrix: no Shell, no external directories, no skills, no delegation, no Flow lifecycle tools. The reviewer cannot edit at all, and the worker cannot touch `.flow` or `.git`. - **A compromised host.** Nothing. The host's own reports are the root of trust diff --git a/docs/index.md b/docs/index.md index 18acc420..3b5f1934 100644 --- a/docs/index.md +++ b/docs/index.md @@ -1,7 +1,8 @@ # Flow documentation This directory is the maintained documentation source for Flow and Session -v5. The public overview and installation instructions live in the +v5 on OpenCode v2. The tested minimum is 2.1.4. OpenCode 1 is unsupported. +The public overview and installation instructions live in the [README](../README.md). - [Positioning](positioning.md) — what Flow is for, and when not to use it. @@ -45,6 +46,7 @@ v5. The public overview and installation instructions live in the and paired measurement. - [ADR 0014](adr/0014-one-evidence-record.md) — `gate` and `externalEvidence` collapse into one `evidence` record. +- [ADR 0018](adr/0018-opencode-v2-only.md) explains the host-major cutover. - [Allowed cross-layer dependencies](architecture/allowed-cross-layer-dependencies.md) — source ownership. diff --git a/docs/maintainer-contract.md b/docs/maintainer-contract.md index 3b3823c7..57067c5e 100644 --- a/docs/maintainer-contract.md +++ b/docs/maintainer-contract.md @@ -18,7 +18,7 @@ A new required-at-save plan declaration is a major. `evidence` entries own Flow owns a serial durable lifecycle with planning, one active run, observed validation, one independent review, reset, and closure. Implementation may use a -bounded, ephemeral host-native worker wave. Flow exposes ten tools, five commands, +bounded, ephemeral host-native worker wave. Flow exposes eleven tools, five commands, four guides, and three hidden subagents. `flow-planner` is disabled unless selected. Active work uses the manager and reserved planner, worker, and reviewer roles. @@ -42,33 +42,27 @@ work makes no mutation and offers continue, defer, or abandon. Close completed work before a new request. Same-goal approved plan-only requests report plan and progress without mutation. -Within existing implementation authority, the manager continues after plan -approval and each passing feature outcome. Under `/flow-auto`, compact `ready` -and `completed` projections are mechanical loop states, not user handoffs: the -manager starts the next runnable feature or closes the session in the same -authorized auto drive. An internal host-triggered continuation may cross model -turns from idle only after a same-host accepted non-replayed `flow_plan_save` -establishes the created Flow session. For an active provisional baseline, -temporal progress in that same session lets a dispatched pending reviewer submit -its owned result without pretending reviewer submission is manager provenance. -An unchanged already-ready baseline or replacement session fails closed. -Planning awaiting `flow_plan_approve` and any -`await-user-direction`, blocked or ready, remain checkpoints unless an exact -qualified host recovery request authorizes the mutation. Shadow advice cannot. A clarification ending at the same recognized checkpoint revision -re-arms waiting without auto-routing; a reply resumes only after it advances the -same session to a mechanical state through an accepted non-replayed manager -mutation observed in that OpenCode host session. The mutation is credited only -when the tool assistant ID resolves through the cached `message.updated` -`parentID` to the authoritative user reply; a missing mapping or mismatch fails -closed. Another host cannot establish that reply authority, though a reviewer -child may contribute only the one state-constrained successor revision after an -authenticated `flow_review_start`; every other mechanical projection must equal -the credited mutation revision. Compaction requires a same-host trigger, -automatic compaction part, matching summary, and successor user with a synthetic -`compaction_continue` marker. Captured authority must match. Earlier user metadata -updates grant nothing. Missing or stale lineage fails closed. This remains -process-local and adds no Session v5 field. Flow starts the next feature whenever -the proven lease permits it. +Within existing implementation authority, `/flow-auto` continues through runnable +features and closure. An accepted non-replayed manager mutation must establish +progress in the same Flow session before idle can enqueue another prompt. An +unchanged ready baseline, replacement session, unknown attribution, or stale +source fails closed. A dispatched reviewer may contribute only the expected +state-constrained successor revision after `flow_review_start`. + +Planning awaiting approval and `await-user-direction` remain checkpoints. +Shadow advice grants no mutation. A reply resumes only after its accepted +manager mutation advances the same session into a mechanical state. An unchanged +recognized checkpoint returns to waiting. Native assistant and call identities +must match the frozen inputs consumed by that model request. Later input cannot +relabel an older assistant or supply authority from another host. + +Automatic compaction retains the epoch only through its exact checked native +automatic control record. Its user-role checkpoint grants no user approval. +Real input, manual compaction of automatic work, interruption, or stream loss +revokes the interval. Stale unmatched callbacks cannot revoke a fresh source. +Summary-only ordinary history grants no checkpoint execution. These checks add +no Session v5 field. See +[ADR 0018](adr/0018-opencode-v2-only.md). `/flow-auto stop` and `/flow-auto cancel` revoke only the process-local lease in that OpenCode session. They do not mutate or close the durable Flow session. @@ -192,7 +186,7 @@ separate user approval. Workers contribute only inside their assigned boundary. The manager owns shared files, integration, evidence acceptance, the combined diff, authoritative validation, and review dispatch. Ordinary worker edits are allowed so a cohort -can run without approval interruptions; Bash, `.flow` and `.git` metadata +can run without approval interruptions; Shell, `.flow` and `.git` metadata paths, nested delegation, and Flow-state tools are denied. Exact per-assignment paths remain a prompt contract because one static reusable agent cannot express a dynamic file ACL; the manager audits assigned versus changed paths after @@ -253,8 +247,13 @@ hold or retry budget is persisted. At blocked or ready `await-user-direction`, read detail once and report findings. Reset-only compatibility never restores implicit selection. See [Session v5](#session-v5). -With `TYPESAFE_API_KEY`, `/flow-auto` defaults to shadow advice (six attempts, -$0.02). Off disables advice, not retry limits. First retry stays automatic; +`options.typesafeApiKeyEnv` names the server variable (default `TYPESAFE_API_KEY`). +Names match `[A-Za-z_][A-Za-z0-9_]*`; invalid names warn and disable the source. +Custom names never fall back. Missing/blank values disable defaults; others are +trimmed at runtime. The key goes only in auth headers, never options, logs or +telemetry. Restart after environment changes. A key enables shadow defaults +(six attempts, $0.02); plugin `recovery: { "mode": "off" }` disables the source. +`--recovery=off` disables advice, not retry limits. First retry stays automatic; fresh direction permits one retry/start. Shadow grants nothing; release delegation is disabled. `recoveryStatus.last` adds process-local assessment duration, transport attempts, reservation deltas, scores, and threshold checks. `reservedUsd` @@ -287,7 +286,7 @@ user starting point. | `flow_plan_amend` | Record a bounded prerequisite repair after a failed canonical gate. | | `flow_plan_approve` | Approve and lock the draft plan. | | `flow_run_start` | Start one runnable approved feature. | -| `flow_validation_start` | Arm observation of the exact next Bash command. | +| `flow_validation_start` | Arm observation of the exact next Shell command. | | `flow_review_start` | Create the run's independent review assignment. | | `flow_feature_complete` | Reviewer-only result submission; exact accepted requests replay read-only. | | `flow_feature_reset` | Supersede a failed attempt and optionally atomically start the exact authorized retry or dependency-independent feature through `nextFeatureId`. | @@ -312,10 +311,13 @@ mutations. `flow_session_close` additionally returns derived delivery under | Agent | Boundary | | --- | --- | | `flow-planner` | Draft advice using read, glob, and grep. The manager saves and approves. | -| `flow-worker` | Bounded implementation contribution; ordinary edits are allowed, while Bash, `.flow` and `.git` metadata paths, external-directory access, skills, delegation, and Flow tools are denied. | +| `flow-worker` | Bounded implementation contribution; ordinary edits are allowed, while Shell, `.flow` and `.git` metadata paths, external-directory access, skills, delegation, and Flow tools are denied. | | `flow-reviewer` | Independent workspace-read-only inspection; only `flow_status` and its exact `flow_feature_complete` lifecycle submission are allowed among Flow tools. | -Picker preferences take priority over reviewer tuple/environment settings. +Project `modelPreferences` options take precedence over global picker choices. +Global choices override reviewer options and environment defaults. A role reset +restores its base configuration. Explicit picker selections clear that role's +reasoning variant. Status reports requested roles and the latest observed coding model. These are process-local observations. Manager variants survive continuation. Configuration does not confirm provider execution. diff --git a/docs/quickstart.md b/docs/quickstart.md index 12d3311b..3cbb3992 100644 --- a/docs/quickstart.md +++ b/docs/quickstart.md @@ -6,7 +6,8 @@ consequential change. Read the plan and the review before accepting the result. ## Install Flow Install the exact release shown in the [README installation section](../README.md#install). -Restart OpenCode after installation. +Use OpenCode v2 with the tested minimum from the README. Finish or explicitly +close active Flow sessions before upgrading, then restart OpenCode. ## Select Flow models @@ -19,17 +20,29 @@ Without the terminal UI, name the reviewer in the Flow plugin entry and restart: ```json { "$schema": "https://opencode.ai/config.json", - "plugin": [ - [ - "", - { "reviewer": { "model": "provider/model", "steps": 80 } } - ] - ] + "plugins": [{ + "package": "", + "options": { "reviewer": { "model": "provider/model", "steps": 80 } } + }] } ``` -Picker preferences win, then tuple settings, then the -`OPENCODE_FLOW_REVIEWER_MODEL` and `OPENCODE_FLOW_REVIEWER_STEPS` fallbacks. +A project can override either global picker choice through plugin options: + +```json +{ + "modelPreferences": { + "review": { "kind": "model", "model": { "providerID": "provider", "id": "model" } }, + "planning": { "kind": "default" } + } +} +``` + +Place `modelPreferences` inside that plugin's `options` object. An absent role +inherits its global choice. A project `default` restores the role's configured +behavior. Reviewer options, then `OPENCODE_FLOW_REVIEWER_MODEL` and +`OPENCODE_FLOW_REVIEWER_STEPS`, supply the base settings. +The coding model stays under OpenCode's normal model selection. ## Verify the installation diff --git a/docs/release-qualification.md b/docs/release-qualification.md index fa5bf1be..9211f09f 100644 --- a/docs/release-qualification.md +++ b/docs/release-qualification.md @@ -23,10 +23,10 @@ This page owns release thresholds, candidate freezing, and publication order. | `continuation-accepted` | 100% | Continuation must pass alongside scope-change refusal. | | `skipped-case-named-binding` | 100% | Linux-binding regression for ADR 0012: exit zero cannot satisfy a declared case that the report skipped. | | `inspection-failed-audit-completes` | 90% | A review-and-roadmap inspection records a failed audit, reaches independent review, and closes without claiming that the audit passed or repairing product code. | -| `auto-two-features-evidence` | 100% in 9.4.0 through 9.6.0 | Two dependent features, native reviewer packet access and final gate. | -| `auto-prerequisite-repair` | 100% in 9.4.0 through 9.6.0 | In-target repair or accepted failed-gate amendment; immutable canonical gate. | -| `auto-observe-with-required-pass` | 100% in 9.4.0 through 9.6.0 | Honest nonzero audit plus separate required pass on the reviewed source. | -| Five delivery cases | 100% in 9.6.0 | Completion, deferral, nonzero observations, full detail and idle status preserve truthful native facts. | +| `auto-two-features-evidence` | 100% in 9.4.0 through 9.6.0 and the v2 profile | Two dependent features, native reviewer packet access and final gate. | +| `auto-prerequisite-repair` | 100% in 9.4.0 through 9.6.0 and the v2 profile | In-target repair or accepted failed-gate amendment; immutable canonical gate. | +| `auto-observe-with-required-pass` | 100% in 9.4.0 through 9.6.0 and the v2 profile | Honest nonzero audit plus separate required pass on the reviewed source. | +| Five delivery cases | 100% in 9.6.0 and the v2 profile | Completion, deferral, nonzero observations, full detail and idle status preserve truthful native facts. | Ungated exploratory scenarios are listed in [evals](../evals/README.md#scenarios). @@ -54,8 +54,11 @@ Primary/reserve cells on GPT-6 Sol are 38/8 in 9.1.0 and 9.2.0, 48/9 in 9.3.0, and 57/12 in 9.4.0. The 9.4.0 profile adds three autonomous cases. Version 9.5.0 keeps those twelve cases on GPT-6.1 Sol, with 57/12 cells. Version 9.6.0 adds five delivery cases, collects them first, and uses 72/17 -cells. Other versions use 96/18. Changed order requires a new freeze and -approval. Existing campaigns stay immutable; narrowed or merged reports +cells. Flow 10.0.0 and later v2-targeting releases retain all 17 cases on two +provider routes, with 144 primary and 34 environmental reserve cells. Manager +and reviewer share the approved model within each cell. Recovery is off. +Other historical generic profiles use 96/18. Cell counts are not dispatch limits. +Changed order requires a new freeze and approval. Existing campaigns stay immutable; narrowed or merged reports cannot qualify. Reported but ungated: reviewer findings/silent passes, refusals, operational counts, @@ -88,6 +91,11 @@ Finish or close active sessions before changing Flow versions in either directio release and removed no earlier than the next major, so no session is stranded mid-lifecycle by an upgrade. +Flow 10.0.0 deliberately removes OpenCode 1 support without a bridge release, +as requested by the maintainer. This is an explicit exception to the earlier +announcement cadence. The minimum tested host is OpenCode 2.1.4. Historical +9.x qualification keeps its original 1.18.31 host pin and policy digests. + [Publication recovery](../.agents/plans/05-release-simplification/README.md#recovery). ## Running it @@ -95,7 +103,8 @@ Finish or close active sessions before changing Flow versions in either directio Finish code, dependency, version and changelog changes first. Pass frozen install, `bun run check`, `bun run replay`, audit, live smoke and CI before paid qualification. Freeze packed contents and evaluator inputs, then run the versioned matrix -on the canonical Linux host. Run a fresh canary against its exact `artifact.tgz`, +on the canonical Linux host. The v2 profile uses HTTP transport with ambient +OpenCode configuration disabled. Run a fresh canary against its exact `artifact.tgz`, seal/regrade the bundle, and commit only evidence without changing measured inputs. Recheck final main CI and exact artifact identity before tagging `v`. For 9.4.0, use @@ -103,21 +112,32 @@ For 9.4.0, use Authorize dispatches using the [paid-run budget](../.agents/plans/05-release-simplification/README.md#authorize-paid-work). Keep that ledger across retries. Budget-stopped campaigns cannot qualify. +For native accounts, follow the [source setup](../evals/README.md#native-account-authentication). +Prepare with `--native-auth`, launch with `--native-auth-source`, and record with +the matching `--launch-receipt`. Live connection data remains private. For 9.1.0 through 9.4.0, pin GPT-6 Sol. Versions 9.5.0 and 9.6.0 pin GPT-6.1 Sol. ```bash -env -u TYPESAFE_API_KEY -u OPENCODE_FLOW_REVIEWER_MODEL -u OPENCODE_FLOW_REVIEWER_STEPS \ - bun run eval -- --release --model openai/gpt-6.1-sol +env -u TYPESAFE_API_KEY -u OPENCODE_FLOW_REVIEWER_MODEL \ + -u OPENCODE_FLOW_REVIEWER_VARIANT -u OPENCODE_FLOW_REVIEWER_STEPS \ + bun run eval -- --release --model provider-a/model --model provider-b/model bun run eval:canary -- prepare --report /report.json --out -# Run the prepared fixture, then record its session and transcript. -bun run eval:canary -- record +bun scripts/canary-run.ts +bun run eval:canary -- record --prepared --operator \ + --session --transcript bun run qualify -- --campaign-dir \ --canary evals/canary/.json ``` -The 9.6.0 matrix schedules 90 primary manager steps, up to 23 reserve steps and +The canary launcher needs its separately approved ledger in `FLOW_EVAL_AUTHORIZATION` +and provider access through its environment. It prints the native export path, +collects it before stopping the isolated host, and charges failed launches. + +The historical 9.6.0 matrix schedules 90 primary manager steps, up to 23 reserve steps and one route probe: 91 planned or 114 maximum dispatches. Internal generation is -separate and these counts imply no dollar cap. Canary authorization is separate. +separate and these counts imply no dollar cap. Canary authorization is separate. For the v2 profile, derive the dispatch limit +from the frozen scenario steps, followups, and route probes before approval. +Do not copy the historical single-provider budget. Use the [cheaper tiers](../evals/README.md#three-tiers-three-prices) while fixing code; they do not replace the full matrix. `bun run triage` identifies runs worth reading. diff --git a/docs/troubleshooting.md b/docs/troubleshooting.md index 664652f0..2c288ebb 100644 --- a/docs/troubleshooting.md +++ b/docs/troubleshooting.md @@ -8,29 +8,41 @@ relevant `opencode.json` holds that same exact entry. Restart OpenCode and inspect its plugin-loading logs. Flow has no installer, cache cleaner, or configuration-repair CLI. Follow the official -[OpenCode plugin documentation](https://opencode.ai/docs/plugins/) for npm or +[OpenCode plugin documentation](https://opencode.ai/v2/docs/plugins/) for npm or configuration failures. ## Reviewer model looks shared Run `/flow-status` and read the process-local reviewer configuration. Prefer the -native tuple form: +object-form v2 entry: ```json { - "plugin": [["", { "reviewer": { "model": "provider/model", "steps": 80 } }]] + "plugins": [{ "package": "", "options": { "reviewer": { "model": "provider/model", "steps": 80 } } }] } ``` Environment variables remain fallbacks. Flow shows the requested model, but status keeps availability `unverified`; it does not persist reviewer health. +## Recovery advice stays off + +Set the chosen variable in the server environment and restart the server if +needed. `options.typesafeApiKeyEnv` names it (default `TYPESAFE_API_KEY`). +Custom names never fall back. Missing/blank keys or invalid names keep advice +off. Check the command and plugin off settings. See the +[configuration contract](maintainer-contract.md#opencode-surface). + ## `/flow-auto` stops after every feature -Continuation is anchored to the assistant message that owns the lease, so a host -that reports no assistant message parentage cannot carry it. Flow says so at -`/flow-auto` startup when it has observed that, and `flow_status` reports -`autoContinuation.support`. On that host, drive each feature with `/flow-run`. +Confirm that OpenCode reports the supported v2 version from the README. +OpenCode 1 cannot load this release. Restart after the host upgrade. + +Continuation needs a verified model-request snapshot, exact native tool +identity, and a live process-local epoch. Conflicting input, matched source failure, +manual compaction of automatic work, or stream loss stops that authority. +Unmatched callbacks grant nothing and cannot stop a fresh invocation. Restart +discards authority. The durable Flow session remains available. Read `flow_status.autoDrive` for the [activity and pause reason](automatic-continuation.md). A supported host does not imply an active lease. `/flow-run` continues manually. @@ -74,7 +86,7 @@ project, then remove that exact lock directory manually and retry. ## Validation capture was cancelled -`flow_validation_start` arms the exact next Bash command. Any different Bash +`flow_validation_start` arms the exact next Shell command. Any different Shell command cancels capture. Arm it again, run the displayed command unchanged, and wait for the `[flow-validation]` marker. diff --git a/docs/validation-and-review.md b/docs/validation-and-review.md index 718b9f8e..510d5893 100644 --- a/docs/validation-and-review.md +++ b/docs/validation-and-review.md @@ -25,7 +25,7 @@ an unrelated broad pass. The reviewer blocks if that gap reaches review. Beyond declared entries, no skipped-evidence field or parallel blocker ledger exists. Missing evidence checkpoints for direction through reset or explicit closure. -`flow_validation_start` prepares the active run, exact next Bash command, scope, +`flow_validation_start` prepares the active run, exact next Shell command, scope, current workspace-content digest, and the host platform, which infrastructure reads rather than the caller claiming it. The OpenCode after-hook accepts only a structured exit code, records output completeness and digests the output. The observation is @@ -40,9 +40,11 @@ tests. `recordValidation` refuses a broad claim on another command. Nothing decides whether the declared command is a test; [ADR 0010](adr/0010-declared-canonical-gate.md) records why that stays a caller declaration made at planning time. -For truncated Bash output, Flow hashes the host `outputPath` only if its -bounded regular `tool-output` file matches the visible tail. Missing or -mismatched files stay incomplete; the exact command and exit code still bind. +For truncated shell output, Flow joins the managed native shell record to the +session, exact command, working directory, and exit status before reading its +bounded regular output file. The file must match the observed output and belong +to the host's shell storage. Missing, mismatched, timed-out, or incomplete output +stays ineligible. A completed tool with a nonzero shell exit still records failure. `savePlan` requires `evidence`: one gate plus optional extra observations. Every entry names its command, `platform`, and `assertions`. A `gate` or `extra` needs @@ -71,8 +73,8 @@ it. Legacy Session v5 reviews remain valid. Typed assignments must reference eve check as of their creation; completed closure also rechecks immutable plan evidence. [ADR 0009](adr/0009-scope-keyed-validation-veto.md) records why the label binds. -An armed capture waits at most 15 minutes for its exact Bash command to begin. -An unrelated Bash command cancels it. Once the exact command begins, the +An armed capture waits at most 15 minutes for its exact Shell command to begin. +An unrelated Shell command cancels it. Once the exact command begins, the after-hook remains eligible even if the command finishes after that original waiting deadline. Session, run, source, exit-code, and output-completeness gates still apply. diff --git a/evals/README.md b/evals/README.md index 2925819c..25301dd7 100644 --- a/evals/README.md +++ b/evals/README.md @@ -11,14 +11,16 @@ hold. ## Running -Needs provider credentials, so this is never part of `bun run check`. +Model runs require provider credentials and an approved +[dispatch ledger](../.agents/plans/05-release-simplification/README.md#authorize-paid-work). +They are outside `bun run check`. Opt-in Jev corpus scoring needs `TYPESAFE_API_KEY` and is also outside the gate: `bun run eval:jev` writes gitignored `evals/results/jev-alignment-v2.json`. ```bash -bun run eval -- --model openai/gpt-5.6-sol -bun run eval -- --model openai/gpt-5.6-sol --model opencode/claude-opus-5 +bun run eval -- --model provider/model +bun run eval -- --model provider-a/model --model provider-b/model bun run eval -- --scenario happy-path --repeat 3 ``` @@ -26,7 +28,7 @@ To bind a campaign to a reviewed package, supply both `--expected-tarball-sha256 sha256:<64 lowercase hex digits>` and `--expected-manifest-sha256 sha256:<64 lowercase hex digits>`. The runner compares these values against its actual packed archive before cache installation, -credential copying, model probes or workflow dispatches. A raw archive mismatch +credential setup, model probes or workflow dispatches. A raw archive mismatch fails even when the complete unpacked content manifest matches. Missing, repeated, malformed or unpaired hash options fail before building. Both options also accept `--option=value` syntax. Runs without either option retain their existing behavior. @@ -36,17 +38,15 @@ hashes in the launch command. Keep the build process's reviewed file mask. Prote private logs through individual exclusive files with mode `0600`, rather than changing the process file mask for both logging and package creation. -Ids are `providerID/modelID` as the host resolves them, which depends on which -providers you have authenticated — Opus 5 may be `opencode/claude-opus-5` rather -than `anthropic/claude-opus-5`. Only the first slash separates the two halves, so -a gateway model id keeps its own (`openrouter/openai/gpt-5.6-sol`). +Ids are `providerID/modelID` as the host resolves them. Only the first slash +separates provider and model, so routed model IDs may contain further slashes. -A preflight boots one throwaway host and checks two separate things before the -run spends anything: +A preflight boots one throwaway host and checks availability before scenario work. +Its model probes consume authorized dispatches. 1. **The id resolves.** OpenCode builds its catalog from Models.dev overlaid with your configured providers. A miss here is a spelling or configuration problem. -2. **The model answers.** One near-free completion per model. A model can resolve +2. **The model answers.** One authorized completion per model. A model can resolve and still refuse, because the catalog says nothing about whether your account is *entitled* to call it — the normal state for a newly released or preview-gated model. Without this stage that failure surfaces partway into a @@ -55,26 +55,87 @@ run spends anything: Either failure exits 2 before any scenario runs. Losing the probe host itself reports `SKIPPED` and continues, since that is no evidence about the models. -The child host gets its own XDG directories so it never touches your session -database, but credentials live in that same data directory, so `auth.json` is -copied into the throwaway home and removed with it. Set `FLOW_EVAL_NO_AUTH_COPY=1` -to skip the copy and rely on environment credentials only. +Active runs use OpenCode 2.1.4 with private HOME and XDG directories. Provider +environment credentials and the explicit native-account source below are +supported. V1 `auth.json` copying is retired. Programmatic callers can supply +an owned native `credentialHandoff`. Both Flow and ordinary comparison hosts +use HTTP transport. + +### Native account authentication + +Keep OAuth refresh in the existing OpenCode 2.1.4 background service. The test +host receives a placeholder refresh value and resolves access through a private +local relay. Do not export or copy OAuth credentials into a second database. + +Use an existing approved `FLOW_EVAL_AUTHORIZATION` ledger before starting the +source. Its models must include every requested model. Source setup may perform +normal native OAuth refresh. It does not create authorization or consume a model +dispatch. No real account values are printed. + +Keep this process running, with one `--model` argument per approved model: + +```sh +bun scripts/native-eval-auth.ts --opencode opencode2 \ + --model provider/model --out /tmp/flow-eval-auth/connection.json +``` + +The output directory must be owned by the operator with mode 0700. A missing +directory is created privately. The connection file is created exclusively with +mode 0600. The command refuses existing files. Stop it with SIGINT or SIGTERM +after all test hosts have stopped. Cleanup removes its own connection file and +unloads its temporary source location. It leaves the original service running. + +In the evaluation shell, select that private file and the same approved model: + +```sh +export FLOW_EVAL_NATIVE_AUTH=/tmp/flow-eval-auth/connection.json +bun run eval -- --model provider/model --scenario plan-only-stops --concurrency 1 +``` + +Native-account runs require concurrency one. Missing, changed, unavailable or +unapproved accounts fail rather than falling back to another credential. Warming +is disabled. Keep the connection file, capability and account metadata out of +logs, transcripts, Git and published artifacts. The file must be owned by the +evaluation process user; copies need the same private permissions. + +For a Linux worker on another host, keep both endpoints on loopback and use a +private SSH forward. In a private copy of the connection file, change only `url` +to the forwarded loopback address. Preserve the capability and routing data. +Do not forward the native service password or expose the relay on a public +interface. The source service version and PID are checked around native RPC calls. + +Canaries require an explicitly sealed attachment: + +```sh +bun run eval:canary -- prepare --report CAMPAIGN/report.json \ + --out CANARY_DIR --native-auth +bun scripts/canary-run.ts CANARY_DIR/prepared.json provider/model PROMPT_FILE \ + --native-auth-source "$FLOW_EVAL_NATIVE_AUTH" +``` + +Recording an attached canary also requires `--launch-receipt` pointing to that +launch's `run.json`. The installation evidence seals the static four-file client +and public launch receipt. Live connection data stays private. No-auth +preparations reject a native source argument; attached preparations require it. `FLOW_EVAL_MODEL` accepts a comma-separated list as an alternative to `--model`. -`FLOW_OPENCODE_SMOKE_VERSION` overrides the pinned host. +`FLOW_EVAL_OPENCODE_VERSION` selects another eligible host for ordinary runs. +`FLOW_OPENCODE_SMOKE_VERSION` selects the compatibility-smoke host. For an ordinary run with a distinct reviewer, set `OPENCODE_FLOW_REVIEWER_MODEL` and optional `OPENCODE_FLOW_REVIEWER_STEPS`. The runner preflights the reviewer model, writes both values through native -plugin tuple configuration, and records the same selection in provenance. +plugin object options, and records the same selection in provenance. Release sampling rejects reviewer overrides. Ordinary runs use one sequential queue per model, with up to four queues in flight. Release mode is strictly sequential (`--concurrency 1`). Versions 9.1.0 and 9.2.0 pin `openai/gpt-6-sol` with 38 primary targets and eight reserves. Version 9.3.0 pins the same model with 48 primary targets and nine reserves. Version -9.4.0 uses 57 primary targets and 12 reserves on the same route. Other versions -require two providers, 96 primary targets and 18 reserves. Only retained retryable +9.4.0 uses 57 primary targets and 12 reserves on the same route. Flow 10.0.0 +requires two providers across 17 cases, with 144 primary cells and 34 reserves. +Cells are scheduling units; count scenario steps and probes separately for the +dispatch authorization. Only retained retryable host/provider failures activate reserves, never product failures. Results are persisted in declared order even when ordinary queues finish out of order. @@ -93,8 +154,8 @@ so signals reach it; allow shutdown grace (for example `docker stop --timeout 18 `), and wait for exit before removing the container or scratch files. An accepted signal stops new probes, steps, primary jobs and reserves. Active host -work is aborted, the process tree is terminated, refreshed credentials are synced -back, then scratch is removed. Cleanup failures propagate and preserve recoverable +work is aborted and native credentials are collected before the process tree +stops. An explicit handoff callback runs before scratch cleanup. Cleanup failures propagate and preserve recoverable scratch rather than claiming successful finalization. Never share that scratch: it can contain credentials. @@ -341,8 +402,9 @@ Comparisons may isolate artifact or manager changes, measure measure the whole run; isolated reviewer quality needs frozen code and review evidence. Requested profiles remain distinct from observed host metadata. -Studies pin OpenCode 1.18.6 and disable ambient host configuration and reviewer -environment overrides. Catalog membership does not prove model entitlement or +New studies pin OpenCode 2.1.4, select HTTP transport, and disable ambient host +configuration and reviewer environment overrides. Historical 1.18.6 policies +remain readable with their original hashes. Catalog membership does not prove model entitlement or effective effort. Optional entitlement probes have a separate explicit allowance and retained receipts. A failed probe stops the study. @@ -447,9 +509,8 @@ review or release qualification. These limits are derived for existing recording without rewriting their measured bytes or expectations. Nothing credential-shaped is written into a cassette, and the recording host's -project path is replaced by a token rather than baked in. The recording host copies -the developer's real `auth.json` into its throwaway home, so this is a hard rule -rather than a precaution; `tests/eval-replay.test.ts` pins it. +project path is replaced by a token. Native credential snapshots stay outside +recorded model decisions; `tests/eval-replay.test.ts` checks redaction. Only recordings someone has read belong in the committed `evals/cassettes/` set, which is what CI gates on. `--accept` rewrites supported cassette expectations diff --git a/evals/auto-scenario-checks.ts b/evals/auto-scenario-checks.ts index a2f2f512..8e92a754 100644 --- a/evals/auto-scenario-checks.ts +++ b/evals/auto-scenario-checks.ts @@ -1,6 +1,7 @@ import { z } from "zod"; import { canonicalJson } from "./canonical-json.js"; import type { ScenarioGradeInput } from "./grader-input.js"; +import { nativeToolKey } from "./host-trace.js"; import { checkAutonomousLineage, checkReviewerEvidenceAccess, @@ -165,7 +166,9 @@ function primaryTool( ); if (message?.role !== "assistant" || message.agent !== call.agent) return false; - const tool = message.tools.find((value) => value.partId === native.partId); + const tool = message.tools.find( + (value) => nativeToolKey(value) === nativeToolKey(native), + ); if (!tool || tool.tool !== call.tool || tool.status !== call.status) return false; const { tool: _tool, status: _status, ...observed } = tool; diff --git a/evals/benchmark-run.ts b/evals/benchmark-run.ts index 0e1ecd61..153a689e 100644 --- a/evals/benchmark-run.ts +++ b/evals/benchmark-run.ts @@ -55,6 +55,7 @@ import { packPlugin, preparePackageCache, } from "./harness.js"; +import { OPENCODE_HOST_VERSION } from "./opencode-host.js"; import { evaluatorIdentity, hostActorObservation, @@ -358,7 +359,7 @@ async function main(): Promise { const requested = model(options.model); await requirePaidAuthorization(); const root = join(import.meta.dir, ".."); - const opencodeVersion = packageJson.devDependencies["@opencode-ai/plugin"]; + const opencodeVersion = OPENCODE_HOST_VERSION; const runtimeSha256 = evidenceSha256( canonicalJson(await benchmarkRuntimeIdentity()), ); @@ -451,6 +452,8 @@ async function main(): Promise { toolchain, packageCache: cache, opencodeVersion, + ambientConfig: "disabled", + reviewerEnvironment: "disabled", files: { "package.json": '{"name":"paired-preflight"}\n' }, withFlow: false, }); @@ -520,6 +523,8 @@ async function main(): Promise { toolchain, packageCache: cache, opencodeVersion, + ambientConfig: "disabled", + reviewerEnvironment: "disabled", files: benchmark.files, withFlow: flow, }); diff --git a/evals/delivery-scenario-checks.ts b/evals/delivery-scenario-checks.ts index 2c4e4122..13eaf3ef 100644 --- a/evals/delivery-scenario-checks.ts +++ b/evals/delivery-scenario-checks.ts @@ -10,6 +10,7 @@ import { presentationText as prose, } from "./delivery-presentation.js"; import type { ScenarioGradeInput } from "./grader-input.js"; +import { nativeToolKey } from "./host-trace.js"; import { checkReviewerEvidenceAccess } from "./reviewer-access.js"; function invokingScript(command: string): string | null { @@ -234,7 +235,9 @@ function primary( if (message?.role !== "assistant" || message.agent !== call.agent) return false; const tool = message.tools.find( - (item) => item.partId === call.native?.partId, + (item) => + call.native !== undefined && + nativeToolKey(item) === nativeToolKey(call.native), ); if (!tool || tool.tool !== call.tool || tool.status !== call.status) return false; diff --git a/evals/grader-input.ts b/evals/grader-input.ts index 23fcceb0..c6a5d161 100644 --- a/evals/grader-input.ts +++ b/evals/grader-input.ts @@ -7,8 +7,10 @@ import type { ObservedToolCall, Outcome } from "./harness.js"; import { type HostAbortObservation, HostAbortObservationSchema, + type HostTrace, HostTraceSchema, NativeToolProvenanceSchema, + nativeToolKey, } from "./host-trace.js"; import { ReviewerPacketBytesListSchema } from "./reviewer-packet-bytes.js"; @@ -119,7 +121,7 @@ export const ScenarioGradeInputSchema = z for (const message of value.hostTrace.messages) { if (message.role !== "assistant") continue; for (const tool of message.tools) - tools.set(tool.partId, { + tools.set(nativeToolKey(tool), { sessionId: message.sessionId, messageId: message.id, agent: message.agent, @@ -143,9 +145,9 @@ export const ScenarioGradeInputSchema = z continue; } const native = call.native; - const observed = tools.get(native.partId); + const observed = tools.get(nativeToolKey(native)); if ( - seen.has(native.partId) || + seen.has(nativeToolKey(native)) || !observed || observed.sessionId !== native.sessionId || observed.messageId !== native.messageId || @@ -162,7 +164,7 @@ export const ScenarioGradeInputSchema = z path: [field, index, "native"], message: "Native call provenance does not match the host trace.", }); - seen.add(native.partId); + seen.add(nativeToolKey(native)); } } }); @@ -489,13 +491,7 @@ function verifiedAbortPending( const tools: string[] = []; for (const pending of observation.pending) { const native = pending.native; - const identity = canonicalJson([ - native.sessionId, - native.messageId, - native.partId, - native.partIndex, - native.callId, - ]); + const identity = nativeToolKey(native); if ( !owned.has(native.sessionId) || !native.callId || @@ -510,13 +506,7 @@ function verifiedAbortPending( (call) => call.tool === pending.tool && call.native && - canonicalJson([ - call.native.sessionId, - call.native.messageId, - call.native.partId, - call.native.partIndex, - call.native.callId, - ]) === identity, + nativeToolKey(call.native) === identity, ); const call = matches[0]; if ( @@ -531,17 +521,15 @@ function verifiedAbortPending( ) return null; const nativeMatches = trace.messages - .flatMap((message) => (message.role === "assistant" ? message.tools : [])) + .flatMap< + Extract< + Extract["messages"][number], + { role: "assistant" } + >["tools"][number] + >((message) => (message.role === "assistant" ? message.tools : [])) .filter( (tool) => - tool.tool === pending.tool && - canonicalJson([ - tool.sessionId, - tool.messageId, - tool.partId, - tool.partIndex, - tool.callId, - ]) === identity, + tool.tool === pending.tool && nativeToolKey(tool) === identity, ); const counterpart = nativeMatches[0]; if ( diff --git a/evals/harness.ts b/evals/harness.ts index ff8851a2..38fe0bb4 100644 --- a/evals/harness.ts +++ b/evals/harness.ts @@ -35,6 +35,11 @@ import { createServer } from "node:net"; import { homedir, tmpdir } from "node:os"; import { delimiter, dirname, join, resolve } from "node:path"; import { fileURLToPath, pathToFileURL } from "node:url"; +import type { + CredentialEntry, + OpenCodeClient, + SessionInfo, +} from "@opencode/client"; import packageJson from "../package.json" with { type: "json" }; import { consumePaidDispatch } from "../scripts/paid-budget.js"; import { isArtifactPath } from "../src/domain/artifact.js"; @@ -83,6 +88,23 @@ import { type NativeToolProvenance, nativeToolProvenance, } from "./host-trace.js"; +import { HTTP_TRANSPORT_PLUGIN_ID } from "./http-transport-plugin.js"; +import { + connectNativeAccountSource, + type NativeAuthentication, + type NativeAuthHost, +} from "./native-auth/host.js"; +import { + type EvalMessage, + installedOpenCodePluginEntry, + listNativeMessages, + listNativeSessions, + nativeModel, + OpenCodeStartup, + openCodeInvocation, + projectEvalMessage, + validateOpenCodeVersion, +} from "./opencode-host.js"; import { exactPackageVersion, packedPackageManifest, @@ -136,7 +158,7 @@ async function abortable( } /** What OpenCode names the error it stamps on a message an abort killed. */ -const ABORT_ERROR_NAME = "MessageAbortedError"; +const ABORT_ERROR_NAME = "aborted"; const STALLED_MS = 3 * 60_000; /** A single tool invocation observed in the transcript. */ @@ -165,7 +187,7 @@ export type ObservedToolCall = { readonly output: unknown; readonly rawOutput: string; /** - * Host-reported metadata for the call: for Bash, the `exit` code and the + * Host-reported metadata for the call: for shell, the `exit` code and the * truncation flag the validation capture hook reads. Recorded because those two * fields decide whether an observation can ever be eligible. */ @@ -330,7 +352,7 @@ export type Outcome = { readonly hostTrace?: HostTrace | undefined; /** Ordered `flow_*` calls only — the workflow's observable spine. */ readonly flowCalls: readonly ObservedToolCall[]; - /** Every tool call, including host tools like bash/edit/task. */ + /** Every tool call, including host tools like shell/edit/subagent. */ readonly allCalls: readonly ObservedToolCall[]; /** Actor identity observations from completed, non-error assistant messages. */ readonly actors?: readonly ObservedActor[]; @@ -1183,42 +1205,6 @@ export async function terminateChildProcessTree( * defect or a dead server, and the status alone has never been enough to tell * those apart from a scenario's own output. */ -async function fetchJson( - url: string, - timeout = REQUEST_TIMEOUT_MS, - signal?: AbortSignal, -): Promise { - const deadline = AbortSignal.timeout(timeout); - const requestSignal = signal ? AbortSignal.any([signal, deadline]) : deadline; - return abortable(requestSignal, async () => { - const response = await fetch(url, { signal: requestSignal }); - if (!response.ok) { - throw new Error( - `GET ${url} failed with ${response.status}: ${await response.text()}`, - ); - } - return response.json(); - }); -} - -/** `fetchJson` for the requests that drive a session, with the same error rule. */ -async function postJson( - url: string, - body: unknown, - options: { readonly signal?: AbortSignal } = {}, -): Promise { - const signal = options.signal ?? AbortSignal.timeout(REQUEST_TIMEOUT_MS); - return abortable(signal, async () => { - const response = await fetch(url, { - method: "POST", - headers: { "content-type": "application/json" }, - body: JSON.stringify(body), - signal, - }); - return postJsonResponse(url, response); - }); -} - async function postJsonResponse(url: string, response: Response) { if (!response.ok) { throw new Error( @@ -1301,26 +1287,12 @@ export function pendingCallLabel(part: { return `${label} (${line.length > 120 ? `${line.slice(0, 117)}...` : line})`; } -/** - * Whether an error on an assistant message is one this harness caused. - * - * Both abort sites in `runCommand` are Flow ending a wait it cannot win — an - * escalation nothing here answers, or a deadline — and OpenCode stamps - * `MessageAbortedError` on the message it killed. Reporting that as a host error - * put 88 false alarms in front of the 4 real timeouts across 408 recorded runs, - * because escalating is the designed end of six scenarios. - * - * Attributed by the flag rather than by session id: aborting a parent kills its - * reviewer subtask too, and that child's abort has the same cause. It takes the - * flag as an argument because with no abort issued, an abort error is real news — - * something outside this process ended the turn. - */ export function isSelfAbortError( error: unknown, selfAborted: boolean, ): boolean { if (!selfAborted || !error || typeof error !== "object") return false; - return (error as { name?: unknown }).name === ABORT_ERROR_NAME; + return isRecord(error) && error.type === ABORT_ERROR_NAME; } export function isWedged( @@ -1579,7 +1551,7 @@ function providerErrorObservation( record.data && typeof record.data === "object" ? (record.data as Record) : undefined; - const name = data?.name ?? record.name; + const name = data?.name ?? record.type ?? record.name; const message = data?.message ?? record.message; if (typeof name !== "string" || !name.trim()) return null; return { @@ -1651,57 +1623,7 @@ export async function preparePackageCache( return cache; } -/** - * One message as the host's HTTP API returns it, narrowed to what scoring reads. - * - * Declared rather than imported because it is another process's wire format, and a - * host upgrade that drops a field should surface here as a scoring change to think - * about — not as a type error in a dependency, and not as a silent zero. - */ -type MessageEntry = { - info: { - id?: string; - sessionID?: string; - parentID?: string; - summary?: unknown; - role: string; - agent?: string; - model?: { providerID?: unknown; modelID?: unknown }; - providerID?: unknown; - modelID?: unknown; - time?: { created: number; completed?: number }; - error?: unknown; - cost?: number; - tokens?: { - input: number; - output: number; - reasoning: number; - cache?: { read: number; write: number }; - }; - }; - parts: { - id?: string; - sessionID?: string; - callID?: string; - messageID?: string; - type: string; - tool?: string; - text?: string; - time?: { start?: number; end?: number }; - synthetic?: boolean; - auto?: boolean; - metadata?: Record; - state?: { - raw?: string; - time?: { start?: number; end?: number }; - status: string; - input?: Record; - output?: string; - error?: string; - metadata?: Record; - }; - }[]; -}; +type MessageEntry = EvalMessage; type OwnedProgressSession = { id: string; messages: readonly MessageEntry[] }; function isProgressId(value: unknown): value is string { @@ -1720,6 +1642,7 @@ function activeProgressSnapshot( const active: string[] = []; const messageIds = new Set(); const partIds = new Set(); + const toolCallIds = new Set(); for (const session of sessions) { for (const entry of session.messages) { const message = entry.info.id; @@ -1730,13 +1653,12 @@ function activeProgressSnapshot( ) throw new Error("Owned progress transcript is malformed."); messageIds.add(message); - for (const part of entry.parts) { + for (const [partIndex, part] of entry.parts.entries()) { if ( - !isProgressId(part.id) || !isProgressId(part.type) || part.sessionID !== session.id || part.messageID !== message || - partIds.has(part.id) + partIds.has(canonicalJson([message, partIndex])) ) throw new Error("Owned progress transcript is malformed."); if ( @@ -1749,18 +1671,28 @@ function activeProgressSnapshot( )) ) throw new Error("Owned progress tool is malformed."); - partIds.add(part.id); + if (part.type === "tool") { + const callKey = canonicalJson([session.id, message, part.callID]); + if (toolCallIds.has(callKey)) + throw new Error("Owned progress tool is malformed."); + toolCallIds.add(callKey); + } + partIds.add(canonicalJson([message, partIndex])); } const messageKey = canonicalJson([session.id, message]); if ( - entry.info.role !== "assistant" || + (entry.native.type !== "assistant" && + !( + entry.native.type === "compaction" && + entry.native.status === "running" + )) || entry.info.time?.completed !== undefined ) completedMessages.add(messageKey); if (completedMessages.has(messageKey)) continue; active.push(canonicalJson([messageKey, "assistant"])); - for (const part of entry.parts) { - const key = canonicalJson([session.id, message, part.id]); + for (const [partIndex, part] of entry.parts.entries()) { + const key = canonicalJson([session.id, message, partIndex]); const terminal = part.time?.end !== undefined || (part.type === "tool" && @@ -1815,20 +1747,6 @@ type SessionMessages = ObservedSession & { readonly messages: readonly MessageEntry[] | null; }; -/** - * One throwaway OpenCode host, over one fixture repository, for one attempt. - * - * The isolation is the measurement. Every host gets its own port, its own XDG - * directories, its own scratch copy of the plugin and of `auth.json`, and its own - * git fixture — so nothing an attempt does can reach the developer's sessions, and - * nothing about the developer's machine can explain a result. `stop()` is what - * makes that true rather than aspirational, and it is why the credential sync has - * to happen before the scratch directory goes. - * - * Constructed through `start` rather than `new`, because a host is only meaningful - * once the server is listening and the fixture is committed, and a half-booted one - * would be scored as a failed attempt. - */ export type EvalReviewerOptions = Readonly<{ model?: string; variant?: string; @@ -1841,22 +1759,11 @@ export class EvalHost { private retainReviewDocument = false; private serverLog = ""; private baseUrl = ""; - /** - * When this harness last aborted a session itself, or 0 if it never did. - * - * Both abort sites in `runCommand` are Flow ending a wait it cannot win — an - * escalation nothing here answers, or a deadline. OpenCode stamps - * `MessageAbortedError` on the message it killed, and reporting that as a host - * error puts 88 false alarms in front of the 4 real timeouts across 408 - * recorded runs: escalating is the designed end of six scenarios, so almost - * every one of them carried it. - * - * A timestamp rather than a flag, because a scenario runs several commands - * against one host: a message an abort killed was created before that abort, - * so an abort error on a message created *after* the last one this harness - * issued is real news — something outside this process ended a later turn — - * and a bare flag would have swallowed it for the rest of the attempt. - */ + private nativeClient: OpenCodeClient | null = null; + get client(): OpenCodeClient { + if (!this.nativeClient) throw new Error("OpenCode client is not ready."); + return this.nativeClient; + } private lastSelfAbortAt = 0; private commandAbortObservation: HostAbortObservation | undefined; private readonly escalationQuestions = new Map(); @@ -1891,7 +1798,19 @@ export class EvalHost { } readonly project: string; private readonly scratch: string; - private credentialPaths: CredentialSync | null = null; + private credentialHandoff: + | { + before: readonly CredentialEntry[]; + onStop: (input: { + before: readonly CredentialEntry[]; + after: readonly CredentialEntry[]; + }) => Promise; + } + | undefined; + private nativeAuthSource: + | ReturnType + | undefined; + private nativeAuthHost: NativeAuthHost | undefined; private polledProviderFailure: { sessionId: string; messages: MessageEntry[]; @@ -1917,16 +1836,24 @@ export class EvalHost { packageCache: string; packageVersion?: string; reviewerEnvironment?: "inherit" | "disabled"; - nativeLlm?: false; ambientConfig?: "inherit" | "disabled"; opencodeVersion: string; + opencodeExecutable?: string; files: Readonly>; retainReviewDocument?: boolean; - /** Configures the hidden reviewer through the same native tuple users set. */ + /** Configures the hidden reviewer through the native plugin options. */ reviewer?: EvalReviewerOptions; /** False creates the paired benchmark's ordinary OpenCode control host. */ withFlow?: boolean; providerCredentials?: "inherit" | "disabled"; + nativeAuthentication?: NativeAuthentication; + credentialHandoff?: { + entries: readonly CredentialEntry[]; + onStop: (input: { + before: readonly CredentialEntry[]; + after: readonly CredentialEntry[]; + }) => Promise; + }; recoveryTreatment?: RecoveryTreatment; requestBudget?: { scope?: EpisodeReservationScope; @@ -1936,11 +1863,14 @@ export class EvalHost { }; signal?: AbortSignal; }): Promise { + checkCancellation(options.signal); + validateOpenCodeVersion(options.opencodeVersion); if (options.frozenArtifacts) { - const { signal, ...configuration } = options; + const { signal, credentialHandoff, ...configuration } = options; options = { ...structuredClone(configuration), ...(signal ? { signal } : {}), + ...(credentialHandoff ? { credentialHandoff } : {}), }; } if (options.requestBudget) @@ -2034,8 +1964,8 @@ export class EvalHost { ) ) throw new Error("Invalid host request budget."); - if (options.opencodeVersion !== "1.18.31") - throw new Error("Request gate is verified only for OpenCode 1.18.31."); + if (options.opencodeVersion !== "2.1.4") + throw new Error("Request gate is verified only for OpenCode 2.1.4."); } const version = exactPackageVersion( options.packageVersion ?? packageJson.version, @@ -2051,6 +1981,7 @@ export class EvalHost { const childCache = join(scratch, "cache"); const childData = join(childHome, ".local", "share"); await mkdir(childHome, { recursive: true }); + await mkdir(join(scratch, "tmp")); await mkdir(join(project, ".opencode"), { recursive: true }); const gitConfig = join(scratch, "gitconfig"); const gitHooks = join(scratch, "git-hooks"); @@ -2069,9 +2000,21 @@ export class EvalHost { }; delete environment.FLOW_EVAL_AUTHORIZATION; delete environment.TYPESAFE_API_KEY; - if (options.requestBudget) { - environment.OPENCODE_EXPERIMENTAL_WEBSOCKETS = "false"; - environment.OPENCODE_EXPERIMENTAL_NATIVE_LLM = "false"; + delete environment.FLOW_EVAL_NATIVE_AUTH; + if (options.nativeAuthentication) { + for (const name of Object.keys(environment)) + if (name.startsWith("OPENCODE_")) delete environment[name]; + const configurationDirectory = join(childHome, ".config", "opencode"); + await mkdir(configurationDirectory, { recursive: true, mode: 0o700 }); + await writeFile( + join(configurationDirectory, "opencode.json"), + JSON.stringify({ + warming: false, + update: "disable", + share: "disabled", + }), + { mode: 0o600 }, + ); } if (options.ambientConfig === "disabled" || options.requestBudget) { @@ -2082,19 +2025,51 @@ export class EvalHost { ]) delete environment[name]; } - host.credentialPaths = - options.providerCredentials === "disabled" || simulationTreatment - ? null - : await evaluationPhase("host", "credential-copy-failed", true, () => - carryProviderCredentials( - childData, - liveTreatment - ? options.requestBudget?.managerModel === "openai/gpt-6-sol" - ? "openai" - : "xai" - : undefined, - ), - ); + if (options.nativeAuthentication) { + if ( + options.credentialHandoff || + options.providerCredentials === "disabled" || + simulationTreatment + ) + throw new Error( + "Native authentication requires exclusive credential ownership.", + ); + host.nativeAuthSource = connectNativeAccountSource( + options.nativeAuthentication.configuration, + ); + host.nativeAuthHost = await host.nativeAuthSource.prepareHost({ + directory: join(scratch, "native-auth"), + models: options.nativeAuthentication.models, + ...(options.signal ? { signal: options.signal } : {}), + }); + await writeFile( + join(childHome, ".config", "opencode", "opencode.json"), + JSON.stringify({ + warming: false, + update: "disable", + share: "disabled", + plugins: [host.nativeAuthHost.plugin], + }), + { mode: 0o600 }, + ); + options = { + ...options, + providerCredentials: "inherit", + credentialHandoff: host.nativeAuthHost.credentialHandoff, + }; + } + if ( + options.credentialHandoff && + (options.providerCredentials === "disabled" || simulationTreatment) + ) + throw new Error("Native credentials are disabled for this host."); + if ( + options.providerCredentials === "inherit" && + !options.credentialHandoff + ) + throw new Error( + "V2 credential inheritance requires an explicit native credentialHandoff; auth.json is unsupported.", + ); // Flow derives source identity from git, so the fixture must be a repo. for (const [relative, contents] of Object.entries(options.files)) { @@ -2194,19 +2169,28 @@ export class EvalHost { ); } checkCancellation(options.signal); - let pluginEntry = `opencode-plugin-flow@${version}`; + let pluginEntry = + options.withFlow !== false && !options.recoveryTreatment + ? await installedOpenCodePluginEntry( + join( + childCache, + "opencode", + "packages", + `opencode-plugin-flow@${version}`, + "node_modules", + "opencode-plugin-flow", + ), + join(scratch, "flow-plugin-entry"), + ) + : ""; let treatmentBundleDigest: string | undefined; if (options.recoveryTreatment) { if (frozenTreatmentBundle) { - const destination = join( - scratch, - "treatment", - "live-treatment-plugin.js", - ); + const destination = join(scratch, "treatment", "index.js"); await mkdir(dirname(destination), { recursive: true }); await writeFile(destination, frozenTreatmentBundle.bytes); checkCancellation(options.signal); - pluginEntry = pathToFileURL(destination).href; + pluginEntry = dirname(destination); treatmentBundleDigest = frozenTreatmentBundle.sha256; } else { const built = await Bun.build({ @@ -2228,84 +2212,81 @@ export class EvalHost { checkCancellation(options.signal); if (!built.success || !built.outputs[0]) throw new Error("Evaluation treatment bundle failed."); - pluginEntry = pathToFileURL(built.outputs[0].path).href; + await rename( + built.outputs[0].path, + join(scratch, "treatment", "index.js"), + ); + pluginEntry = join(scratch, "treatment"); treatmentBundleDigest = createHash("sha256") - .update(await readFile(built.outputs[0].path)) + .update(await readFile(join(scratch, "treatment", "index.js"))) .digest("hex"); } } - const reviewer = options.reviewer; - const configuredPlugin = options.recoveryTreatment - ? [ - pluginEntry, - { - ...(liveTreatment - ? { origin: "live" } - : { treatment: options.recoveryTreatment }), - budget: options.requestBudget, - readyPath: join(scratch, "treatment-ready.json"), - budgetReadyPath: join(scratch, "budget-ready.json"), - }, - ] - : reviewer && - (reviewer.model !== undefined || - reviewer.variant !== undefined || - reviewer.steps !== undefined) - ? [pluginEntry, { reviewer }] - : pluginEntry; + const budgetEntry = join(scratch, "budget-plugin"); + if (options.requestBudget && !liveTreatment) { + await mkdir(budgetEntry); + await writeFile( + join(budgetEntry, "index.ts"), + `export { default } from ${JSON.stringify(pathToFileURL(fileURLToPath(new URL("./recovery-decisions/budget-plugin.ts", import.meta.url))).href)};\n`, + ); + } + + const pluginOptions = options.recoveryTreatment + ? { + ...(liveTreatment + ? { origin: "live" } + : { treatment: options.recoveryTreatment }), + budget: options.requestBudget, + readyPath: join(scratch, "treatment-ready.json"), + budgetReadyPath: join(scratch, "budget-ready.json"), + } + : options.reviewer + ? { reviewer: options.reviewer } + : {}; + const httpTransportEntry = join(scratch, HTTP_TRANSPORT_PLUGIN_ID); + if (options.withFlow === false) { + await mkdir(httpTransportEntry); + await writeFile( + join(httpTransportEntry, "index.ts"), + `export { default } from ${JSON.stringify(pathToFileURL(fileURLToPath(new URL("./http-transport-plugin.ts", import.meta.url))).href)};\n`, + ); + } + const plugins = [ + ...(options.requestBudget && !liveTreatment + ? [ + { + package: budgetEntry, + options: { + directory: options.requestBudget.directory, + authorizationDigest: + options.requestBudget.authorizationDigest, + ...(options.requestBudget.scope + ? { scope: options.requestBudget.scope } + : {}), + readyPath: join(scratch, "budget-ready.json"), + ...(simulationTreatment + ? { + simulationScript: + options.recoveryTreatment?.origin === "simulation" + ? options.recoveryTreatment.script + : undefined, + } + : {}), + }, + }, + ] + : []), + ...(options.withFlow === false + ? [{ package: httpTransportEntry }] + : [{ package: pluginEntry, options: pluginOptions }]), + ]; + const permissions = releaseHostPermissions(version); + if (permissions !== undefined && !Array.isArray(permissions)) + throw new Error("Active evals require a V2 host permission policy."); await writeFile( join(project, "opencode.json"), - `${JSON.stringify( - { - ...(releaseHostPermissions(version) - ? { permission: releaseHostPermissions(version) } - : {}), - plugin: [ - ...(options.requestBudget && !liveTreatment - ? [ - [ - pathToFileURL( - fileURLToPath( - new URL( - "./recovery-decisions/budget-plugin.ts", - import.meta.url, - ), - ), - ).href, - { - directory: resolve(options.requestBudget.directory), - ...(options.requestBudget.scope - ? { scope: options.requestBudget.scope } - : {}), - authorizationDigest: - options.requestBudget.authorizationDigest, - readyPath: join(scratch, "budget-ready.json"), - ...(options.recoveryTreatment?.origin === "simulation" - ? { - simulationScript: - options.recoveryTreatment.script, - } - : {}), - }, - ], - ] - : []), - ...(options.withFlow === false ? [] : [configuredPlugin]), - ], - ...(options.requestBudget - ? { - model: options.requestBudget.managerModel, - small_model: options.requestBudget.managerModel, - enabled_providers: [ - options.requestBudget.managerModel.split("/")[0], - ], - } - : {}), - }, - null, - 2, - )}\n`, + `${JSON.stringify({ plugins, ...(permissions ? { permissions } : {}) }, null, 2)}\n`, "utf8", ); @@ -2319,69 +2300,85 @@ export class EvalHost { host.baseUrl = `http://127.0.0.1:${port}`; await host.verifyArtifacts(); checkCancellation(options.signal); - host.server = spawn( - host.artifacts?.paths.opencode ?? options.toolchain.executable, - [ - ...(host.artifacts - ? [] - : ["x", `opencode-ai@${options.opencodeVersion}`]), - "serve", - "--port", - String(port), - "--hostname", - "127.0.0.1", - ], - { - cwd: project, - detached: process.platform !== "win32", - env: { - ...environment, - ...(jevKey ? { TYPESAFE_API_KEY: jevKey } : {}), - ...(options.nativeLlm === false - ? { OPENCODE_EXPERIMENTAL_NATIVE_LLM: "false" } - : {}), - ...(options.reviewerEnvironment === "disabled" - ? { - OPENCODE_FLOW_REVIEWER_MODEL: "", - OPENCODE_FLOW_REVIEWER_VARIANT: "", - OPENCODE_FLOW_REVIEWER_STEPS: "", - } - : {}), - HOME: childHome, - XDG_CACHE_HOME: childCache, - XDG_CONFIG_HOME: join(childHome, ".config"), - XDG_DATA_HOME: childData, - XDG_STATE_HOME: join(childHome, ".local", "state"), - }, - stdio: ["ignore", "pipe", "pipe"], + const args = [ + "serve", + "--hostname", + "127.0.0.1", + "--port", + String(port), + ]; + const invocation = host.artifacts + ? { command: host.artifacts.paths.opencode, args } + : options.opencodeExecutable + ? { command: options.opencodeExecutable, args } + : openCodeInvocation(args, options.opencodeVersion); + host.server = spawn(invocation.command, invocation.args, { + cwd: project, + detached: process.platform !== "win32", + env: { + ...environment, + ...(jevKey ? { TYPESAFE_API_KEY: jevKey } : {}), + ...(options.reviewerEnvironment === "disabled" + ? { + OPENCODE_FLOW_REVIEWER_MODEL: "", + OPENCODE_FLOW_REVIEWER_VARIANT: "", + OPENCODE_FLOW_REVIEWER_STEPS: "", + } + : {}), + HOME: childHome, + TMPDIR: join(scratch, "tmp"), + XDG_CACHE_HOME: childCache, + XDG_CONFIG_HOME: join(childHome, ".config"), + XDG_DATA_HOME: childData, + XDG_STATE_HOME: join(childHome, ".local", "state"), }, - ); - const record = (chunk: unknown) => { - host.serverLog += String(chunk); + stdio: ["ignore", "pipe", "pipe"], + }); + const startup = new OpenCodeStartup(); + let loggedBytes = 0; + const record = (chunk: unknown, stream: string) => { + startup.consume(String(chunk), stream); + const safe = startup.log; + host.serverLog += safe.slice(loggedBytes); + loggedBytes = safe.length; }; - host.server.stdout?.on("data", record); - host.server.stderr?.on("data", record); + host.server.stdout?.on("data", (chunk) => record(chunk, "stdout")); + host.server.stderr?.on("data", (chunk) => record(chunk, "stderr")); let spawnError: Error | undefined; host.server.once("error", (error) => { spawnError = error; }); const deadline = Date.now() + STARTUP_TIMEOUT_MS; + const versionMismatch = new Error( + "OpenCode reports an unexpected version.", + ); for (;;) { checkCancellation(options.signal); if (spawnError) throw spawnError; try { - const health = (await fetchJson( - `${host.baseUrl}/global/health`, - 3_000, - options.signal, - )) as { - healthy?: boolean; - }; - if (health.healthy) break; + const client = startup.client(host.baseUrl, project); + if (client) { + const healthSignal = AbortSignal.any([ + ...(options.signal ? [options.signal] : []), + AbortSignal.timeout(3000), + ]); + const health = await abortable(healthSignal, () => + client.server.info({ signal: healthSignal }), + ); + if (health.version !== options.opencodeVersion) + throw versionMismatch; + host.nativeClient = client; + startup.clear(); + break; + } } catch (error) { - if (error instanceof CampaignCancelled) throw error; - // still starting + if ( + error instanceof CampaignCancelled || + error === versionMismatch || + (options.signal?.aborted && error === options.signal.reason) + ) + throw error; } if (Date.now() > deadline) { throw new Error( @@ -2390,15 +2387,92 @@ export class EvalHost { } await abortable(options.signal, () => Bun.sleep(500)); } - const ready = (await host.post( - `${host.baseUrl}/session`, - { title: "flow-eval readiness" }, + const ready = await host.nativeRequest( + (request) => + host.client.session.create( + { + title: "flow-eval readiness", + location: { directory: project }, + }, + request, + ), + options.signal ?? null, Math.max(1, deadline - Date.now()), - )) as { id?: unknown }; - if (typeof ready.id !== "string" || !ready.id) { - throw new Error("OpenCode readiness session had no id."); - } + ); await host.deleteSession(ready.id); + await host.nativeRequest( + (request) => + host.client.config.get( + { location: { directory: project } }, + request, + ), + options.signal ?? null, + Math.max(1, deadline - Date.now()), + ); + const expectedPlugins = new Set( + await Promise.all( + [ + ...(host.nativeAuthHost ? [host.nativeAuthHost.plugin] : []), + ...plugins, + ].map((plugin) => realpath(plugin.package)), + ), + ); + for (;;) { + checkCancellation(options.signal); + if (Date.now() >= deadline) + throw new Error( + "Native evaluation plugins did not become active before the startup deadline.", + ); + const observed = await host.nativeRequest( + (request) => + host.client.plugin.list( + { location: { directory: project } }, + request, + ), + options.signal ?? null, + Math.max(1, deadline - Date.now()), + ); + const active = new Set(); + for (const plugin of observed.data) { + if (plugin.source.type !== "local") continue; + const path = await realpath(plugin.source.path); + const entry = expectedPlugins.has(path) + ? path + : /[/\\]index\.(?:ts|js)$/.test(path) && + expectedPlugins.has(dirname(path)) + ? dirname(path) + : null; + if (!entry) continue; + if (plugin.state.status === "failed") + throw new Error( + "An expected native evaluation plugin failed to load.", + ); + active.add(entry); + } + if ([...expectedPlugins].every((path) => active.has(path))) break; + await abortable(options.signal, () => + Bun.sleep(Math.min(20, Math.max(1, deadline - Date.now()))), + ); + } + if (options.credentialHandoff) { + const before = structuredClone(options.credentialHandoff.entries); + host.credentialHandoff = { + before, + onStop: options.credentialHandoff.onStop, + }; + for (const entry of before) { + await host.client.credential.create( + { + id: entry.id, + integrationID: entry.integrationID, + label: entry.label, + value: entry.value, + activate: entry.active, + }, + host.requestOptions(), + ); + } + } if (options.requestBudget) { const receipt = JSON.parse( await readFile(join(scratch, "budget-ready.json"), "utf8"), @@ -2453,27 +2527,38 @@ export class EvalHost { "Treatment did not initialize with the expected identity.", ); if (simulationTreatment) { - const auth = await fetch( - `${host.baseUrl}/auth/${options.requestBudget?.managerModel.split("/")[0]}`, + const integrationID = + options.requestBudget?.managerModel.split("/")[0]; + if (!integrationID) + throw new Error( + "Simulation manager integration is unavailable.", + ); + const integration = await host.client.integration.get( + { integrationID }, + host.requestOptions(), + ); + const method = integration.data.methods.find( + (method) => method.type === "oauth", + ); + if (method?.type !== "oauth") + throw new Error( + "Simulation requires a native OAuth integration method.", + ); + await host.client.credential.create( { - method: "PUT", - headers: { "content-type": "application/json" }, - body: JSON.stringify({ + integrationID, + label: "Isolated simulated evaluation", + activate: true, + value: { type: "oauth", + methodID: method.id, access: "simulation-access", refresh: "simulation-refresh", expires: Date.now() + 3600000, - }), - signal: options.signal - ? AbortSignal.any([ - options.signal, - AbortSignal.timeout(REQUEST_TIMEOUT_MS), - ]) - : AbortSignal.timeout(REQUEST_TIMEOUT_MS), + }, }, + host.requestOptions(), ); - if (!auth.ok) - throw new Error("Simulation credential installation failed."); } } checkCancellation(options.signal); @@ -2499,16 +2584,26 @@ export class EvalHost { } } - private post( - url: string, - body: unknown, + private requestOptions(signal = this.signal, timeout = REQUEST_TIMEOUT_MS) { + return { + signal: AbortSignal.any([ + ...(signal ? [signal] : []), + AbortSignal.timeout(timeout), + ]), + }; + } + + private nativeRequest( + operation: (options: { signal: AbortSignal }) => Promise, + signal: AbortSignal | null = this.signal ?? null, timeout = REQUEST_TIMEOUT_MS, - ): Promise { - return postJson(url, body, { - signal: this.signal - ? AbortSignal.any([this.signal, AbortSignal.timeout(timeout)]) + ): Promise { + const options = { + signal: signal + ? AbortSignal.any([signal, AbortSignal.timeout(timeout)]) : AbortSignal.timeout(timeout), - }); + }; + return abortable(options.signal, () => operation(options)); } get log(): string { @@ -2535,22 +2630,18 @@ export class EvalHost { async catalogProfiles(): Promise< Array<{ model: string; variants: string[] | null }> > { - const listed = (await fetchJson( - `${this.baseUrl}/config/providers`, - REQUEST_TIMEOUT_MS, - this.signal, - )) as { - providers?: { id: string; models?: Record }[]; - }; - return (listed.providers ?? []).flatMap((provider) => - Object.entries(provider.models ?? {}).map(([modelId, config]) => { - const variants = - isRecord(config) && isRecord(config.variants) - ? Object.keys(config.variants) - : null; - return { model: `${provider.id}/${modelId}`, variants }; - }), + const listed = await this.nativeRequest((options) => + this.client.model.list( + { location: { directory: this.project } }, + options, + ), ); + return listed.data + .filter((model) => model.enabled) + .map((model) => ({ + model: `${model.providerID}/${model.id}`, + variants: model.variants.map((variant) => variant.id), + })); } /** @@ -2577,19 +2668,29 @@ export class EvalHost { checkCancellation(this.signal); await consumePaidDispatch({ model, kind: "probe" }); options?.onDispatch?.(); - const reply = (await this.post( - `${this.baseUrl}/session/${sessionId}/message`, - { - // `/session/:id/message` takes a split model, unlike `/command`, - // which takes the joined string. - model: splitModel(model), - ...(options?.variant === undefined - ? {} - : { variant: options.variant }), - system: "Reply with the single word OK. Call no tools.", - parts: [{ type: "text", text: "ping" }], - }, - )) as MessageEntry; + await this.nativeRequest((request) => + this.client.session.switchModel( + { sessionID: sessionId, model: nativeModel(model, options?.variant) }, + request, + ), + ); + await this.nativeRequest((request) => + this.client.session.prompt( + { + sessionID: sessionId, + text: "Reply with the single word OK. Call no tools.", + }, + request, + ), + ); + await this.nativeRequest((request) => + this.client.session.wait({ sessionID: sessionId }, request), + ); + const entries = await this.messages(sessionId, this.signal); + const reply = entries.findLast( + (entry) => entry.native.type === "assistant", + ); + if (!reply) throw new Error("Model probe produced no assistant message."); const tokens = reply.info?.tokens; usage = normalizeStudyUsage({ tokens: tokens @@ -2622,19 +2723,21 @@ export class EvalHost { } private async deleteSession(sessionId: string): Promise { - const signal = AbortSignal.timeout(ABORT_TIMEOUT_MS); - await abortable(signal, () => - fetch(`${this.baseUrl}/session/${sessionId}`, { - method: "DELETE", - signal, - }), + await this.nativeRequest( + (options) => + this.client.session.remove({ sessionID: sessionId }, options), + null, + ABORT_TIMEOUT_MS, ).catch(() => {}); } async createSession(title: string): Promise { - const session = (await this.post(`${this.baseUrl}/session`, { title })) as { - id: string; - }; + const session = await this.nativeRequest((options) => + this.client.session.create( + { title, agent: "build", location: { directory: this.project } }, + options, + ), + ); return session.id; } @@ -2661,19 +2764,24 @@ export class EvalHost { checkCancellation(this.signal); this.escalationQuestions.delete(sessionId); await consumePaidDispatch({ model, kind: "command" }); - const { variant, ...waitOptions } = options; + await this.nativeRequest((request) => + this.client.session.switchModel( + { sessionID: sessionId, model: nativeModel(model, options.variant) }, + request, + ), + ); + const { variant: _variant, ...waitOptions } = options; return runSessionRequest({ signal: this.signal, waitOwnsCancellation: true, onCancelled: () => this.abortSession(sessionId), - post: postSessionJson, - url: `${this.baseUrl}/session/${sessionId}/command`, - body: { - command, - arguments: args, - model, - ...(variant === undefined ? {} : { variant }), - }, + post: (_url, _body, request) => + this.client.session.command( + { sessionID: sessionId, name: command, text: args }, + { signal: request.signal }, + ), + url: this.baseUrl, + body: null, onRejected: (message) => { this.serverLog += `\ncommand POST rejected: ${message}`; }, @@ -2703,18 +2811,24 @@ export class EvalHost { checkCancellation(this.signal); this.escalationQuestions.delete(sessionId); await consumePaidDispatch({ model, kind: "prompt" }); - const { variant, ...waitOptions } = options; + await this.nativeRequest((request) => + this.client.session.switchModel( + { sessionID: sessionId, model: nativeModel(model, options.variant) }, + request, + ), + ); + const { variant: _variant, ...waitOptions } = options; return runSessionRequest({ signal: this.signal, waitOwnsCancellation: true, onCancelled: () => this.abortSession(sessionId), - post: postSessionJson, - url: `${this.baseUrl}/session/${sessionId}/message`, - body: { - model: splitModel(model), - ...(variant === undefined ? {} : { variant }), - parts: [{ type: "text", text: prompt }], - }, + post: (_url, _body, request) => + this.client.session.prompt( + { sessionID: sessionId, text: prompt }, + { signal: request.signal }, + ), + url: this.baseUrl, + body: null, onRejected: (message) => { this.serverLog += `\nmessage POST rejected: ${message}`; }, @@ -2791,6 +2905,7 @@ export class EvalHost { entry.info, part, partIndex, + entry.native, ); if (!native || !part.tool || !isRecord(part.state.input)) return; pending.push({ @@ -2868,7 +2983,8 @@ export class EvalHost { )) as MessageEntry[]; const failed = messages.find( (entry) => - entry.info.role === "assistant" && + (entry.native.type === "assistant" || + entry.native.type === "compaction") && entry.info.error && !isSelfAbortError( entry.info.error, @@ -2960,7 +3076,10 @@ export class EvalHost { ownedPending.length > 0 || ownedMessages.some( (entry) => - entry.info.role === "assistant" && !entry.info.time?.completed, + (entry.native.type === "assistant" && + !entry.native.time.completed) || + (entry.native.type === "compaction" && + entry.native.status === "running"), ); const counts = sessions .map((session) => ({ @@ -3026,61 +3145,34 @@ export class EvalHost { root: string, messages: readonly MessageEntry[], signal: AbortSignal, - ): Promise { + ): Promise { + const listed = await this.nativeRequest( + (options) => + listNativeSessions(this.client, this.project, options.signal), + signal ?? null, + ); const ids = [root]; const known = new Set(ids); for (const parent of ids) { - const children = await fetchJson( - `${this.baseUrl}/session/${encodeURIComponent(parent)}/children`, - REQUEST_TIMEOUT_MS, - signal, - ); - if (!Array.isArray(children)) - throw new Error("Owned progress children response is malformed."); - for (const child of children) { - if (!isRecord(child)) continue; - const id = nonEmptyString(child.id); + for (const child of listed) { if ( - !id || - id.length > 256 || - /\s/.test(id) || - child.parentID !== parent || - child.directory !== this.project || - known.has(id) + child?.parentID !== parent || + !isProgressId(child.id) || + typeof child.location?.directory !== "string" || + known.has(child.id) || + (await realpath(child.location.directory)) !== + (await realpath(this.project)) ) continue; if (known.size >= 128) throw new Error("Owned progress tree exceeds 128 sessions."); - known.add(id); - ids.push(id); + known.add(child.id); + ids.push(child.id); } } const sessions = [{ id: root, messages }]; - for (const id of ids.slice(1)) { - const entries = await fetchJson( - `${this.baseUrl}/session/${encodeURIComponent(id)}/message`, - REQUEST_TIMEOUT_MS, - signal, - ); - if ( - !Array.isArray(entries) || - entries.some( - (entry) => - !isRecord(entry) || - !isRecord(entry.info) || - entry.info.sessionID !== id || - !Array.isArray(entry.parts) || - entry.parts.some( - (part) => - !isRecord(part) || - part.sessionID !== id || - typeof part.type !== "string", - ), - ) - ) - throw new Error("Owned progress transcript is malformed."); - sessions.push({ id, messages: entries as MessageEntry[] }); - } + for (const id of ids.slice(1)) + sessions.push({ id, messages: await this.messages(id, signal) }); return sessions; } @@ -3094,24 +3186,27 @@ export class EvalHost { */ private async abortSession(sessionId: string): Promise { this.lastSelfAbortAt = Date.now(); - await postJson( - `${this.baseUrl}/session/${sessionId}/abort`, - {}, - { - signal: AbortSignal.timeout(ABORT_TIMEOUT_MS), - }, + await this.nativeRequest( + (options) => + this.client.session.interrupt( + { sessionID: sessionId, resume: false }, + options, + ), + null, + ABORT_TIMEOUT_MS, ).catch(() => {}); } private async messages( sessionId: string, signal?: AbortSignal, - ): Promise { - return fetchJson( - `${this.baseUrl}/session/${sessionId}/message`, - REQUEST_TIMEOUT_MS, - signal, - ); + ): Promise { + return ( + await this.nativeRequest( + (options) => listNativeMessages(this.client, sessionId, options.signal), + signal ?? null, + ) + ).map((message) => projectEvalMessage(message, sessionId)); } /** @@ -3134,46 +3229,43 @@ export class EvalHost { readonly endpointFailed: boolean; readonly metadata: readonly unknown[]; }> { - const metadata: unknown[] = []; + let listed: SessionInfo[]; + try { + listed = await this.nativeRequest( + (options) => + listNativeSessions(this.client, this.project, options.signal), + signal ?? null, + ); + } catch (error) { + if (signal?.aborted) throw error; + return { sessions: [], endpointFailed: true, metadata: [] }; + } const known = new Set(sessionIds); const found: ObservedSession[] = []; - let endpointFailed = false; - let frontier = [...sessionIds]; - while (frontier.length > 0) { - const next: string[] = []; - for (const parent of frontier) { - checkCancellation(signal); - let children: unknown; - try { - children = await fetchJson( - `${this.baseUrl}/session/${parent}/children`, - REQUEST_TIMEOUT_MS, - signal, - ); - } catch (error) { - if (signal?.aborted && error === signal.reason) throw error; - endpointFailed = true; + const metadata: SessionInfo[] = []; + const frontier = [...sessionIds]; + for (const parent of frontier) { + for (const child of listed) { + if ( + child.parentID !== parent || + known.has(child.id) || + (await realpath(child.location.directory)) !== + (await realpath(this.project)) + ) continue; - } - if (!Array.isArray(children)) endpointFailed = true; - for (const child of Array.isArray(children) ? children : []) { - metadata.push(child); - if (!isRecord(child)) continue; - const id = nonEmptyString(child.id); - if (!id || known.has(id)) continue; - const childSession: ObservedSession = { - id, - agent: nonEmptyString(child.agent), - parentID: nonEmptyString(child.parentID), - }; - known.add(id); - found.push(childSession); - next.push(id); - } + if (known.size >= 128) + throw new Error("Outcome tree exceeds 128 sessions."); + known.add(child.id); + frontier.push(child.id); + metadata.push(child); + found.push({ + id: child.id, + agent: child.agent ?? null, + parentID: child.parentID ?? null, + }); } - frontier = next; } - return { sessions: found, endpointFailed, metadata }; + return { sessions: found, endpointFailed: false, metadata }; } /** @@ -3211,10 +3303,10 @@ export class EvalHost { if (!polled) { for (const sessionId of sessionIds) { try { - const metadata = await fetchJson( - `${this.baseUrl}/session/${encodeURIComponent(sessionId)}`, - REQUEST_TIMEOUT_MS, - signal, + const metadata = await this.nativeRequest( + (options) => + this.client.session.get({ sessionID: sessionId }, options), + signal ?? null, ); if (!isRecord(metadata) || metadata.id !== sessionId) rootMetadataComplete = false; @@ -3284,7 +3376,11 @@ export class EvalHost { let guidanceSequence = 0; for (const { sessionIndex, entry } of messages) { - if (entry.info.role === "assistant") { + if ( + entry.info.role === "assistant" || + (entry.native.type === "compaction" && + entry.native.status !== "running") + ) { assistantMessages += 1; const used = entry.info.tokens; const messageCost = reportedCost( @@ -3353,7 +3449,7 @@ export class EvalHost { finalText = part.text ?? finalText; } if (part.type !== "tool" || !part.tool) continue; - const raw = part.state?.output ?? part.state?.error ?? ""; + const raw = part.state?.output || part.state?.error || ""; let parsed: unknown = raw; try { parsed = JSON.parse(raw); @@ -3365,14 +3461,14 @@ export class EvalHost { entry.info, part, partIndex, + entry.native, ); allCalls.push({ ...(native ? { native } : {}), tool: part.tool, sessionIndex, agent: entry.info.agent ?? "", - status: - (part.state?.status as ObservedToolCall["status"]) ?? "pending", + status: part.state?.status ?? "pending", input: part.state?.input ?? {}, output: parsed, rawOutput: raw, @@ -3407,7 +3503,14 @@ export class EvalHost { (candidate) => candidate.id === session.id, ); return messagesForSession - ? [{ id: session.id, messages: messagesForSession.messages }] + ? [ + { + id: session.id, + messages: + messagesForSession.messages?.map((entry) => entry.native) ?? + null, + }, + ] : []; }), }); @@ -3416,7 +3519,7 @@ export class EvalHost { role: "manager", sessions: parentSessions.map((session) => ({ id: session.id, - messages: session.messages, + messages: session.messages?.map((entry) => entry.native) ?? null, })), }), reviewerActor, @@ -3453,7 +3556,7 @@ export class EvalHost { sessionMetadata: nativeSessionMetadata, sessionMessages: sessionMessages.map(({ id, messages }) => ({ sessionId: id, - messages, + messages: messages?.map((entry) => entry.native) ?? null, })), childrenComplete: rootMetadataComplete && !descendantResult.endpointFailed, @@ -3573,16 +3676,76 @@ export class EvalHost { } private async stopOnce(): Promise { - if (this.server) await terminateChildProcessTree(this.server); - // Must happen before the scratch directory is removed: a refresh the child - // performed lives only in its copy of `auth.json`, and losing it here is - // exactly what silently rotates the developer's own stored refresh token - // out from under them. + const handoff = this.credentialHandoff; + const drainResult = + handoff && this.nativeClient + ? await Promise.allSettled([ + (async () => { + const active = await this.nativeRequest( + (options) => this.client.session.active(options), + null, + ABORT_TIMEOUT_MS, + ); + await Promise.all( + Object.keys(active).map(async (sessionID) => { + await this.nativeRequest( + (options) => + this.client.session.interrupt( + { sessionID, resume: false }, + options, + ), + null, + ABORT_TIMEOUT_MS, + ); + await this.nativeRequest( + (options) => + this.client.session.wait({ sessionID }, options), + null, + ABORT_TIMEOUT_MS, + ); + }), + ); + })(), + ]) + : []; + const credentialResult = + handoff && this.nativeClient + ? await Promise.allSettled([ + this.nativeRequest( + (options) => this.client.credential.list(options), + null, + ABORT_TIMEOUT_MS, + ), + ]) + : []; + try { + if (this.server) await terminateChildProcessTree(this.server); + } catch (error) { + try { + await this.nativeAuthSource?.dispose(); + } catch (cleanupError) { + throw new AggregateError( + [error, cleanupError], + "Native host termination and authentication cleanup failed.", + ); + } + throw error; + } + this.nativeClient = null; const results = await Promise.allSettled([ - syncProviderCredentialsBack(this.credentialPaths), + Promise.resolve().then(async () => { + const snapshot = credentialResult[0]; + if (snapshot?.status === "rejected") throw snapshot.reason; + if (snapshot?.status === "fulfilled" && handoff) + await handoff.onStop({ + before: handoff.before, + after: snapshot.value, + }); + }), Promise.resolve().then(() => this.projectRetention?.(this.project)), + Promise.resolve().then(() => this.nativeAuthSource?.dispose()), ]); - const failures = results.flatMap((result) => + const failures = [...drainResult, ...results].flatMap((result) => result.status === "rejected" ? [result.reason] : [], ); if (failures.length === 1) throw failures[0]; diff --git a/evals/host-artifacts.ts b/evals/host-artifacts.ts index e99ccc44..9cdbc4d6 100644 --- a/evals/host-artifacts.ts +++ b/evals/host-artifacts.ts @@ -315,10 +315,12 @@ export async function stageHostArtifacts(input: { env: { PATH: input.directory }, maxBuffer: 100000, }); - if ( - probe.status !== 0 || - probe.stdout.trim() !== input.expected[key].version - ) + const registeredVersion = input.expected[key].version; + const versionOutput = + key === "opencode" && /^2\./.test(registeredVersion) + ? `opencode v${registeredVersion}` + : registeredVersion; + if (probe.status !== 0 || probe.stdout.trim() !== versionOutput) throw new Error( `Artifact ${key} version differs from registered version.`, ); diff --git a/evals/host-observation.ts b/evals/host-observation.ts index d48f2193..81f9d234 100644 --- a/evals/host-observation.ts +++ b/evals/host-observation.ts @@ -59,30 +59,26 @@ function messageModelIdentity(message: unknown): { readonly providerID: string; readonly modelID: string; } | null { - if (!isRecord(message) || !isRecord(message.info)) return null; - const info = message.info; - if (info.role !== "assistant" || "error" in info) return null; + if (!isRecord(message)) return null; + const info = message; + if (info.type !== "assistant" || "error" in info) return null; const time = isRecord(info.time) ? info.time : null; if (typeof time?.completed !== "number") return null; const model = isRecord(info.model) ? info.model : null; - const providerID = - nonEmptyString(model?.providerID) ?? nonEmptyString(info.providerID); - const modelID = - nonEmptyString(model?.modelID) ?? nonEmptyString(info.modelID); + const providerID = nonEmptyString(model?.providerID); + const modelID = nonEmptyString(model?.id); return providerID && modelID ? { providerID, modelID } : null; } -/** Extracts the Phase 0-approved identity fields from a host message response. */ export function extractObservedModelIdentity( messages: readonly unknown[] | null, ): ObservedModelIdentity { if (messages === null) return { kind: "unobserved", reason: "endpoint-failure" }; const completed = messages.filter((message) => { - if (!isRecord(message) || !isRecord(message.info)) return false; - if (message.info.role !== "assistant" || "error" in message.info) - return false; - const time = isRecord(message.info.time) ? message.info.time : null; + if (!isRecord(message)) return false; + if (message.type !== "assistant" || "error" in message) return false; + const time = isRecord(message.time) ? message.time : null; return typeof time?.completed === "number"; }); if (completed.length === 0) @@ -93,7 +89,7 @@ export function extractObservedModelIdentity( (value): value is { providerID: string; modelID: string } => value !== null, ); - if (identities.length === 0) + if (identities.length !== completed.length) return { kind: "unobserved", reason: "field-unavailable" }; const unique = new Map( identities.map((identity) => [ @@ -110,13 +106,13 @@ export function extractObservedModelIdentity( } function messageVariant(message: unknown): string | null { - if (!isRecord(message) || !isRecord(message.info)) return null; - const info = message.info; - if (info.role !== "assistant" || "error" in info) return null; + if (!isRecord(message)) return null; + const info = message; + if (info.type !== "assistant" || "error" in info) return null; if (!isRecord(info.time) || typeof info.time.completed !== "number") return null; const model = isRecord(info.model) ? info.model : null; - return nonEmptyString(info.variant) ?? nonEmptyString(model?.variant); + return nonEmptyString(model?.variant); } export function extractObservedVariant( @@ -125,10 +121,10 @@ export function extractObservedVariant( if (messages === null) return { kind: "unobserved", reason: "endpoint-failure" }; const completed = messages.filter((message) => { - if (!isRecord(message) || !isRecord(message.info)) return false; - const info = message.info; + if (!isRecord(message)) return false; + const info = message; return ( - info.role === "assistant" && + info.type === "assistant" && !("error" in info) && isRecord(info.time) && typeof info.time.completed === "number" @@ -148,7 +144,6 @@ export function extractObservedVariant( : { kind: "unobserved", reason: "field-unavailable" }; } -/** Only children linked to a known session and named flow-reviewer are reviewers. */ export function selectLineageValidatedReviewers( parentSessionIds: readonly string[], children: readonly ObservedSession[], diff --git a/evals/host-trace.ts b/evals/host-trace.ts index d88dbe94..76c181ed 100644 --- a/evals/host-trace.ts +++ b/evals/host-trace.ts @@ -1,4 +1,7 @@ import { createHash } from "node:crypto"; +import { Session } from "@opencode/schema/session"; +import { SessionMessage } from "@opencode/schema/session-message"; +import { Schema } from "effect"; import { z } from "zod"; type TopologyIssue = @@ -33,7 +36,7 @@ function topologyIssue( const Id = z.string().min(1).max(256).regex(/^\S+$/); const Order = z.number().int().safe().nonnegative(); -export const NativeToolProvenanceSchema = z +const NativeToolProvenanceV1Schema = z .object({ sessionId: Id, messageId: Id, @@ -44,7 +47,36 @@ export const NativeToolProvenanceSchema = z completedAt: Order.nullable(), }) .strict(); +const NativeToolProvenanceV2Schema = z + .object({ + version: z.literal(2), + sessionId: Id, + messageId: Id, + partIndex: Order.max(255), + callId: Id, + startedAt: Order, + completedAt: Order.nullable(), + }) + .strict(); +export const NativeToolProvenanceSchema = z.union([ + NativeToolProvenanceV1Schema, + NativeToolProvenanceV2Schema, +]); export type NativeToolProvenance = z.infer; +export function nativeToolKey(native: NativeToolProvenance): string { + return JSON.stringify( + "version" in native + ? [2, native.sessionId, native.messageId, native.partIndex, native.callId] + : [ + 1, + native.sessionId, + native.messageId, + native.partId, + native.partIndex, + native.callId, + ], + ); +} export const HostAbortObservationSchema = z .object({ @@ -139,7 +171,7 @@ const MessageSchema = z.discriminatedUnion("role", [ summary: z.boolean(), tools: z .array( - NativeToolProvenanceSchema.extend({ + NativeToolProvenanceV1Schema.extend({ tool: Id, status: z.enum(["pending", "running", "completed", "error"]), }).strict(), @@ -148,7 +180,7 @@ const MessageSchema = z.discriminatedUnion("role", [ }) .strict(), ]); -export const HostTraceSchema = z +const HostTraceV1Schema = z .discriminatedUnion("kind", [ z .object({ @@ -274,7 +306,7 @@ export const HostTraceSchema = z if (partCount > 16384 || toolCount > 4096) issue("Host trace exceeds aggregate limits."); }); -export type HostTrace = z.infer; +type HostTraceV1 = z.infer; export type HostTraceCollection = Readonly<{ runnerRootSessionIds: readonly string[]; directory: string; @@ -329,15 +361,18 @@ const NativeMessageSchema = z.object({ }); type NativeMessage = z.infer; type NativePart = z.infer; -type ObservedTrace = Extract; -type UnavailableReason = Extract["reason"]; -const unavailable = (reason: UnavailableReason): HostTrace => ({ +type ObservedTrace = Extract; +type UnavailableReason = Extract< + HostTraceV1, + { kind: "unavailable" } +>["reason"]; +const unavailable = (reason: UnavailableReason): HostTraceV1 => ({ version: 1, kind: "unavailable", reason, }); -export function nativeToolProvenance( +function nativeToolProvenanceV1( sessionId: string, message: unknown, part: unknown, @@ -363,7 +398,7 @@ function provenance( messageId: string, part: NativePart, partIndex: number, -): NativeToolProvenance { +): z.infer { return { sessionId, messageId, @@ -375,7 +410,7 @@ function provenance( }; } -export function collectHostTrace(input: HostTraceCollection): HostTrace { +function collectHostTraceV1(input: HostTraceCollection): HostTraceV1 { if (!input.childrenComplete) return unavailable("endpoint-failure"); if ( input.runnerRootSessionIds.length === 0 || @@ -547,3 +582,451 @@ export function collectHostTrace(input: HostTraceCollection): HostTrace { compactionDelivery: "unavailable-from-message-export", }; } + +const Digest = z.string().regex(/^sha256:[a-f0-9]{64}$/); +const V2MessageIdentity = MessageIdentity; +const V2MessageSchema = z.discriminatedUnion("role", [ + z + .object({ + ...V2MessageIdentity, + role: z.literal("user"), + type: z.enum(["user", "synthetic"]), + textBytes: Order, + attachments: Order.max(255), + flowTokenSha256: Digest.nullable(), + compactionContinue: z.boolean().nullable(), + }) + .strict(), + z + .object({ + ...V2MessageIdentity, + role: z.literal("assistant"), + type: z.literal("assistant"), + agent: Id, + contentCount: Order.max(256), + tools: z + .array( + NativeToolProvenanceV2Schema.extend({ + tool: Id, + status: z.enum(["pending", "running", "completed", "error"]), + }).strict(), + ) + .max(256), + }) + .strict(), + z + .object({ + ...V2MessageIdentity, + role: z.literal("event"), + type: z.enum([ + "agent-switched", + "model-switched", + "location-switched", + "system", + "skill", + "shell", + "compaction", + "idle", + ]), + }) + .strict(), +]); +const HostTraceV2Schema = z + .discriminatedUnion("kind", [ + z + .object({ + version: z.literal(2), + kind: z.literal("observed"), + runnerRootSessionIds: z.array(Id).min(1).max(16), + sessions: HostTraceV1Schema.options[0].shape.sessions, + messages: z.array(V2MessageSchema).max(4096), + compactionDelivery: z.literal("unavailable-from-message-export"), + }) + .strict(), + HostTraceV1Schema.options[1].extend({ version: z.literal(2) }).strict(), + ]) + .superRefine((trace, context) => { + if (trace.kind !== "observed") return; + const issue = (message: string) => + context.addIssue({ code: "custom", message }); + const ids = new Set(); + const sessions = new Set(); + for (const session of trace.sessions) { + if (ids.has(session.id)) issue("Duplicate native identity."); + ids.add(session.id); + sessions.add(session.id); + } + const ancestry = topologyIssue(trace.sessions, trace.runnerRootSessionIds); + if (ancestry) issue(ancestry); + if ( + new Set(trace.runnerRootSessionIds).size !== + trace.runnerRootSessionIds.length + ) + issue("Duplicate runner root."); + const orders = new Map(); + const keys = new Set(); + let toolCount = 0; + let contentCount = 0; + for (const message of trace.messages) { + if (ids.has(message.id)) issue("Duplicate native identity."); + ids.add(message.id); + if (!sessions.has(message.sessionId)) + issue("Unknown native message session."); + const previous = orders.get(message.sessionId); + if ( + message.order !== (previous ? previous.order + 1 : 0) || + (previous && message.created < previous.created) + ) + issue("Native chronology conflicts."); + orders.set(message.sessionId, message); + if (message.role === "user") contentCount += 1 + message.attachments; + if (message.role !== "assistant") continue; + contentCount += message.contentCount; + let previousIndex = -1; + for (const tool of message.tools) { + const key = nativeToolKey(tool); + if (keys.has(key)) issue("Duplicate native tool identity."); + keys.add(key); + if ( + tool.partIndex <= previousIndex || + tool.partIndex >= message.contentCount + ) + issue("Native content order conflicts."); + previousIndex = tool.partIndex; + if ( + tool.sessionId !== message.sessionId || + tool.messageId !== message.id + ) + issue("Native tool binding conflicts."); + if ( + tool.startedAt < message.created || + (tool.completedAt !== null && tool.completedAt < tool.startedAt) + ) + issue("Native tool chronology conflicts."); + toolCount++; + } + } + if (toolCount > 4096 || contentCount > 16384) + issue("Host trace exceeds aggregate limits."); + }); +export const HostTraceSchema = z.union([HostTraceV1Schema, HostTraceV2Schema]); +export type HostTrace = z.infer; +type ObservedTraceV2 = Extract< + z.infer, + { kind: "observed" } +>; +const isNativeSessionV2 = Schema.is(Schema.toEncoded(Session.Info)); +const isNativeMessageV2 = Schema.is(Schema.toEncoded(SessionMessage.Info)); +const V2Binding = z.object({ + sessionID: Id.optional(), + parentID: z.never().optional(), +}); +const NativeMetadata = z.object({ + "opencode-plugin-flow/auto": z.string().min(1).max(1024).optional(), + compaction_continue: z.boolean().optional(), +}); +const tokenDigest = (token: string | undefined): string | null => + token === undefined + ? null + : `sha256:${createHash("sha256").update(token).digest("hex")}`; + +function nativeToolProvenanceV2( + sessionId: string, + message: unknown, + partIndex: number, +): z.infer | undefined { + const binding = V2Binding.safeParse(message); + if ( + !Id.safeParse(sessionId).success || + !Order.safeParse(partIndex).success || + partIndex >= 256 || + !binding.success || + (binding.data.sessionID !== undefined && + binding.data.sessionID !== sessionId) || + !isNativeMessageV2(message) || + message.type !== "assistant" + ) + return undefined; + const content = message.content[partIndex]; + if (content?.type !== "tool") return undefined; + if ( + !Order.safeParse(message.time.created).success || + !Order.safeParse(content.time.created).success || + (content.time.ran !== undefined && + (!Order.safeParse(content.time.ran).success || + content.time.ran < content.time.created || + (content.time.completed !== undefined && + content.time.ran > content.time.completed))) + ) + return undefined; + const ref = NativeToolProvenanceV2Schema.safeParse({ + version: 2, + sessionId, + messageId: message.id, + partIndex, + callId: content.id, + startedAt: content.time.created, + completedAt: content.time.completed ?? null, + }); + return ref.success && + ref.data.startedAt >= message.time.created && + (ref.data.completedAt === null || + ref.data.completedAt >= ref.data.startedAt) + ? ref.data + : undefined; +} + +export function nativeToolProvenance( + sessionId: string, + message: unknown, + contentIndex: number, +): NativeToolProvenance | undefined; +export function nativeToolProvenance( + sessionId: string, + message: unknown, + part: unknown, + partIndex: number, + rawNativeMessage?: unknown, +): NativeToolProvenance | undefined; +export function nativeToolProvenance( + sessionId: string, + message: unknown, + part: unknown, + partIndex?: number, + rawNativeMessage?: unknown, +): NativeToolProvenance | undefined { + if (typeof part === "number" && partIndex === undefined) + return nativeToolProvenanceV2(sessionId, message, part); + if (partIndex === undefined) return undefined; + if (rawNativeMessage !== undefined) { + const ref = nativeToolProvenanceV2(sessionId, rawNativeMessage, partIndex); + const projectedMessage = z + .object({ id: Id, sessionID: Id, role: z.literal("assistant") }) + .safeParse(message); + const projectedPart = z + .object({ + type: z.literal("tool"), + sessionID: Id, + messageID: Id, + callID: Id, + tool: Id, + state: z.object({ + status: z.enum(["pending", "running", "completed", "error"]), + }), + }) + .safeParse(part); + if ( + !ref || + !projectedMessage.success || + !projectedPart.success || + !isNativeMessageV2(rawNativeMessage) || + rawNativeMessage.type !== "assistant" + ) + return undefined; + const content = rawNativeMessage.content[partIndex]; + if ( + content?.type !== "tool" || + projectedMessage.data.id !== ref.messageId || + projectedMessage.data.sessionID !== sessionId || + projectedPart.data.sessionID !== sessionId || + projectedPart.data.messageID !== ref.messageId || + projectedPart.data.callID !== ref.callId || + projectedPart.data.tool !== content.name || + projectedPart.data.state.status !== + (content.state.status === "streaming" + ? "pending" + : content.state.status) + ) + return undefined; + return ref; + } + return nativeToolProvenanceV1(sessionId, message, part, partIndex); +} + +function collectHostTraceV2(input: HostTraceCollection): HostTrace { + const fail = (reason: UnavailableReason): HostTrace => ({ + version: 2, + kind: "unavailable", + reason, + }); + if (!input.childrenComplete) return fail("endpoint-failure"); + if ( + !input.runnerRootSessionIds.length || + input.runnerRootSessionIds.length > 16 || + input.sessionMetadata.length > 128 || + input.sessionMessages.length > 128 + ) + return fail("limits-exceeded"); + if ( + new Set(input.runnerRootSessionIds).size !== + input.runnerRootSessionIds.length + ) + return fail("duplicate-native-id"); + const sessions: ObservedTraceV2["sessions"] = []; + const ids = new Set(); + for (const raw of input.sessionMetadata) { + if (!isNativeSessionV2(raw) || !Id.safeParse(raw.id).success) + return fail("malformed-native-session"); + if (ids.has(raw.id)) return fail("duplicate-native-id"); + if (raw.location.directory !== input.directory) + return fail("directory-mismatch"); + ids.add(raw.id); + sessions.push({ + id: raw.id, + parentId: raw.parentID ?? null, + agent: raw.agent ?? null, + projectMatched: true, + }); + } + const ancestry = topologyIssue(sessions, input.runnerRootSessionIds); + if (ancestry) return fail(ancestry); + const sessionIds = new Set(ids); + const transcripts = new Set(); + const messages: ObservedTraceV2["messages"] = []; + let contentCount = 0; + let toolCount = 0; + for (const transcript of input.sessionMessages) { + if (transcripts.has(transcript.sessionId)) + return fail("duplicate-native-id"); + if (!sessionIds.has(transcript.sessionId)) + return fail("message-session-mismatch"); + transcripts.add(transcript.sessionId); + if (!Array.isArray(transcript.messages)) + return fail("incomplete-transcript"); + if (messages.length + transcript.messages.length > 4096) + return fail("limits-exceeded"); + let created = -1; + for (const [order, raw] of transcript.messages.entries()) { + const binding = V2Binding.safeParse(raw); + if ( + !binding.success || + !isNativeMessageV2(raw) || + !Id.safeParse(raw.id).success || + !Order.safeParse(raw.time.created).success + ) + return fail("malformed-native-message"); + if ( + binding.data.sessionID !== undefined && + binding.data.sessionID !== transcript.sessionId + ) + return fail("message-session-mismatch"); + if (ids.has(raw.id)) return fail("duplicate-native-id"); + ids.add(raw.id); + if (raw.time.created < created) return fail("chronology-conflict"); + created = raw.time.created; + if ( + "completed" in raw.time && + raw.time.completed !== undefined && + (!Order.safeParse(raw.time.completed).success || + raw.time.completed < created) + ) + return fail("chronology-conflict"); + if ("text" in raw && Buffer.byteLength(raw.text) > 4 * 1024 * 1024) + return fail("limits-exceeded"); + const identity = { + id: raw.id, + sessionId: transcript.sessionId, + order, + created, + }; + if (raw.type === "assistant") { + contentCount += raw.content.length; + if (raw.content.length > 256 || contentCount > 16384) + return fail("limits-exceeded"); + const tools: Extract< + ObservedTraceV2["messages"][number], + { role: "assistant" } + >["tools"] = []; + for (const [partIndex, content] of raw.content.entries()) { + if ( + (content.type === "text" || content.type === "reasoning") && + Buffer.byteLength(content.text) > 4 * 1024 * 1024 + ) + return fail("limits-exceeded"); + if (content.type !== "tool") continue; + const ref = nativeToolProvenanceV2( + transcript.sessionId, + raw, + partIndex, + ); + if (!ref) return fail("chronology-conflict"); + tools.push({ + ...ref, + tool: content.name, + status: + content.state.status === "streaming" + ? "pending" + : content.state.status, + }); + if (++toolCount > 4096) return fail("limits-exceeded"); + } + messages.push({ + ...identity, + role: "assistant", + type: "assistant", + agent: raw.agent, + contentCount: raw.content.length, + tools, + }); + } else if (raw.type === "user" || raw.type === "synthetic") { + if (Buffer.byteLength(raw.text) > 4 * 1024 * 1024) + return fail("limits-exceeded"); + const attachments = + raw.type === "user" + ? (raw.files?.length ?? 0) + + (raw.agents?.length ?? 0) + + (raw.skills?.length ?? 0) + : 0; + contentCount += 1 + attachments; + if (attachments > 255 || contentCount > 16384) + return fail("limits-exceeded"); + const metadata = NativeMetadata.safeParse(raw.metadata ?? {}); + if (!metadata.success) return fail("malformed-native-message"); + messages.push({ + ...identity, + role: "user", + type: raw.type, + textBytes: Buffer.byteLength(raw.text), + attachments, + flowTokenSha256: tokenDigest( + metadata.data["opencode-plugin-flow/auto"], + ), + compactionContinue: metadata.data.compaction_continue ?? null, + }); + } else { + if ( + raw.type === "location-switched" && + raw.location.directory !== input.directory + ) + return fail("directory-mismatch"); + messages.push({ ...identity, role: "event", type: raw.type }); + } + } + } + if (transcripts.size !== sessions.length) + return fail("incomplete-transcript"); + const trace = { + version: 2 as const, + kind: "observed" as const, + runnerRootSessionIds: [...input.runnerRootSessionIds], + sessions, + messages, + compactionDelivery: "unavailable-from-message-export" as const, + }; + const parsed = HostTraceV2Schema.safeParse(trace); + return parsed.success ? parsed.data : fail("malformed-native-part"); +} +export function collectHostTrace(input: HostTraceCollection): HostTrace { + const v2 = + input.sessionMetadata.some( + (raw) => z.object({ location: z.unknown() }).safeParse(raw).success, + ) || + input.sessionMessages.some( + (transcript) => + Array.isArray(transcript.messages) && + transcript.messages.some( + (raw) => z.object({ type: z.string() }).safeParse(raw).success, + ), + ); + return v2 ? collectHostTraceV2(input) : collectHostTraceV1(input); +} diff --git a/evals/http-transport-plugin.ts b/evals/http-transport-plugin.ts new file mode 100644 index 00000000..ba85997e --- /dev/null +++ b/evals/http-transport-plugin.ts @@ -0,0 +1,12 @@ +import { Plugin } from "@opencode/plugin"; +import { registerHttpTransport } from "../src/platform/opencode/http-transport.js"; + +export const HTTP_TRANSPORT_PLUGIN_ID = "flow.eval.http-transport"; + +export default Plugin.define({ + id: HTTP_TRANSPORT_PLUGIN_ID, + setup: async (ctx) => { + const registration = await registerHttpTransport(ctx.provider); + return () => registration.dispose(); + }, +}); diff --git a/evals/native-actors.ts b/evals/native-actors.ts index a4ba837e..b16419cd 100644 --- a/evals/native-actors.ts +++ b/evals/native-actors.ts @@ -1,5 +1,6 @@ import type { RetainedScenarioEvidence } from "./grader-input.js"; import type { ObservedToolCall } from "./harness.js"; +import { nativeToolKey } from "./host-trace.js"; import { nativeToolWitness } from "./reviewer-access.js"; function record(value: unknown): Record | null { @@ -27,16 +28,16 @@ export function nativeActorBindingIssues( const completionCalls = new Map(); for (const call of evidence.gradeInput.allCalls) { if (call.tool !== "flow_feature_complete" || !call.native) continue; - const calls = completionCalls.get(call.native.partId) ?? []; + const calls = completionCalls.get(nativeToolKey(call.native)) ?? []; calls.push(call); - completionCalls.set(call.native.partId, calls); + completionCalls.set(nativeToolKey(call.native), calls); } const incomplete = trace.messages.some( (message) => message.role === "assistant" && message.tools.some((tool) => { if (tool.tool !== "flow_feature_complete") return false; - const calls = completionCalls.get(tool.partId) ?? []; + const calls = completionCalls.get(nativeToolKey(tool)) ?? []; const call = calls[0]; return ( calls.length !== 1 || !call || nativeToolWitness(call, trace) === null diff --git a/evals/native-auth-config.ts b/evals/native-auth-config.ts new file mode 100644 index 00000000..0a39b331 --- /dev/null +++ b/evals/native-auth-config.ts @@ -0,0 +1,57 @@ +import { constants } from "node:fs"; +import { lstat, open } from "node:fs/promises"; +import { dirname, resolve } from "node:path"; +import type { NativeAuthentication } from "./native-auth/host.js"; +import { + type NativeAccountConnection, + parseConnection, + refuse, +} from "./native-auth/protocol.js"; + +export async function readNativeAccountConnection( + path: string, +): Promise { + try { + const absolute = resolve(path); + const parent = await lstat(dirname(absolute)); + if ( + !parent.isDirectory() || + parent.isSymbolicLink() || + parent.uid !== process.getuid?.() || + (parent.mode & 0o777) !== 0o700 + ) + refuse(); + const file = await open( + absolute, + constants.O_RDONLY | constants.O_NOFOLLOW, + ); + try { + const stat = await file.stat(); + if ( + !stat.isFile() || + stat.nlink !== 1 || + stat.uid !== process.getuid?.() || + (stat.mode & 0o777) !== 0o600 || + stat.size > 1024 * 1024 + ) + refuse(); + return parseConnection(JSON.parse(await file.readFile("utf8"))); + } finally { + await file.close(); + } + } catch { + refuse(); + } +} + +export async function nativeAuthenticationFromEnvironment( + models: readonly string[], + environment: NodeJS.ProcessEnv = process.env, +): Promise { + const path = environment.FLOW_EVAL_NATIVE_AUTH?.trim(); + if (!path) return undefined; + return { + configuration: await readNativeAccountConnection(path), + models: [...models], + }; +} diff --git a/evals/native-auth/broker.ts b/evals/native-auth/broker.ts new file mode 100644 index 00000000..5f7c1c99 --- /dev/null +++ b/evals/native-auth/broker.ts @@ -0,0 +1,342 @@ +import { spawn } from "node:child_process"; +import { randomBytes, timingSafeEqual } from "node:crypto"; +import { chmod, mkdir, mkdtemp, rm, writeFile } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { stageNativeAuthSource } from "./host.js"; +import { + capability, + loopbackURL, + type NativeAccountConnection, + NativeAccountRpc, + object, + parseResolved, + parseSelection, + privateAccount, + providersForModels, + type ResolvedAccount, + refuse, + sameMetadata, +} from "./protocol.js"; + +export type NativeAccountBroker = { + configuration: NativeAccountConnection; + dispose(): Promise; +}; + +export async function nativeAccountCLI( + executable: string, + args: readonly string[], + cwd: string, + signal?: AbortSignal, +): Promise { + if (signal?.aborted) refuse(); + return new Promise((resolve, reject) => { + const child = spawn(executable, [...args], { + cwd, + stdio: ["ignore", "pipe", "pipe"], + }); + const chunks: Buffer[] = []; + let size = 0; + let failed = false; + const stop = () => { + failed = true; + child.kill("SIGKILL"); + }; + const timer = setTimeout(stop, 20000); + signal?.addEventListener("abort", stop, { once: true }); + child.stdout.on("data", (chunk: Buffer) => { + size += chunk.length; + if (size > 1024 * 1024) stop(); + else chunks.push(chunk); + }); + child.stderr.on("data", () => {}); + child.once("error", () => { + failed = true; + }); + child.once("close", (code) => { + clearTimeout(timer); + signal?.removeEventListener("abort", stop); + if (failed || code !== 0 || signal?.aborted) + reject(new Error("Native account authentication failed.")); + else resolve(Buffer.concat(chunks).toString("utf8")); + }); + }); +} +function decode(text: string): unknown { + try { + return JSON.parse(text); + } catch { + refuse(); + } +} + +export async function startNativeAccountBroker(options: { + opencodeExecutable: string; + models: readonly string[]; + signal?: AbortSignal; +}): Promise { + const providers = providersForModels(options.models); + const models = [...options.models]; + if (options.signal?.aborted) refuse(); + const directory = await mkdtemp(join(tmpdir(), "flow-native-account-")); + await chmod(directory, 0o700); + const project = join(directory, "project"); + await mkdir(project, { mode: 0o700 }); + const cli = (args: readonly string[], signal?: AbortSignal) => + nativeAccountCLI(options.opencodeExecutable, args, project, signal); + let initialized = false; + let disposed = false; + let closing: Promise | undefined; + let relay: ReturnType | undefined; + let service: { url: string; pid: number } | undefined; + const currentService = async (signal?: AbortSignal) => { + if ( + !service || + loopbackURL((await cli(["service", "status"], signal)).trim()) !== + service.url + ) + refuse(); + const info = decode(await cli(["api", "GET", "/api/info"], signal)); + if (!object(info) || info.version !== "2.1.4" || info.pid !== service.pid) + refuse(); + }; + const location = new URLSearchParams({ + "location[directory]": project, + }).toString(); + const api = async ( + method: "GET" | "POST" | "DELETE", + path: string, + input?: unknown, + signal: AbortSignal | null = options.signal ?? null, + ) => { + const args = [ + "api", + method, + path, + ...(input === undefined ? [] : ["--data", JSON.stringify({ input })]), + ]; + const result = await cli(args, signal ?? undefined); + return result.trim() ? decode(result) : undefined; + }; + const rpc = async ( + method: "describe" | "resolve" | "check" | "close", + input: unknown, + signal: AbortSignal | null = options.signal ?? null, + ) => { + await currentService(signal ?? undefined); + const result = await api( + "POST", + `/api/rpc/${NativeAccountRpc.id}/${method}?${location}`, + input, + signal, + ); + await currentService(signal ?? undefined); + if (!object(result) || !("output" in result)) refuse(); + return result.output; + }; + const dispose = () => { + closing ??= (async () => { + disposed = true; + options.signal?.removeEventListener("abort", abort); + try { + if (relay) await relay.stop(true); + if (initialized) { + let running = true; + try { + await currentService(); + } catch { + running = false; + if (service) { + try { + process.kill(service.pid, 0); + } catch (error) { + if (object(error) && error.code === "ESRCH") + service = undefined; + } + if (service) refuse(); + } + } + if (running) { + await rpc("close", {}, null).catch(() => {}); + await currentService(); + await api( + "DELETE", + `/api/debug/location?${location}`, + undefined, + null, + ); + } + } + } finally { + await rm(directory, { recursive: true }); + } + })().catch(refuse); + return closing; + }; + const abort = () => { + void dispose().catch(() => {}); + }; + try { + const url = loopbackURL( + (await cli(["service", "status"], options.signal)).trim(), + ); + const health = await api("GET", "/api/info"); + if ( + !object(health) || + health.version !== "2.1.4" || + typeof health.pid !== "number" + ) + refuse(); + service = { url, pid: health.pid }; + const source = await stageNativeAuthSource(join(directory, "plugin")); + await writeFile( + join(project, "opencode.json"), + JSON.stringify({ + plugins: [{ package: source.pluginDirectory, options: { providers } }], + update: "disable", + share: "disabled", + warming: false, + }), + { mode: 0o600, flag: "wx" }, + ); + initialized = true; + const described = await rpc("describe", {}); + if (!object(described) || !Array.isArray(described.accounts)) refuse(); + const accounts = described.accounts.map(parseResolved); + if ( + accounts.length !== providers.length || + providers.some( + (provider) => + !accounts.some((account) => account.route.providerID === provider), + ) + ) + refuse(); + const token = randomBytes(32).toString("hex"); + let lease: { id: string; providers: readonly string[] } | undefined; + const authorized = (request: Request) => { + const actual = Buffer.from(request.headers.get("authorization") ?? ""); + const expected = Buffer.from(`Bearer ${token}`); + return ( + actual.length === expected.length && timingSafeEqual(actual, expected) + ); + }; + const validate = async (account: ResolvedAccount) => { + const result = parseResolved( + await rpc( + "resolve", + { + providerID: account.route.providerID, + credentialID: account.route.credentialID, + }, + options.signal, + ), + ); + if ( + !sameMetadata(result.route, account.route) || + !sameMetadata(result.value.metadata, account.value.metadata) + ) + refuse(); + return result; + }; + relay = Bun.serve({ + hostname: "127.0.0.1", + port: 0, + maxRequestBodySize: 16384, + async fetch(request) { + const reply = (body: unknown, status = 200) => + Response.json(body, { + status, + headers: { "cache-control": "no-store" }, + }); + if (disposed || request.method !== "POST" || !authorized(request)) + return reply({ error: "Native account authentication failed." }, 401); + try { + if (Number(request.headers.get("content-length") ?? 0) > 16384) + refuse(); + const bytes = await request.text(); + if (bytes.length > 16384) refuse(); + const input: unknown = decode(bytes); + if (!object(input)) refuse(); + const path = new URL(request.url).pathname; + if (path === "/acquire") { + if ( + lease !== undefined || + !Array.isArray(input.models) || + !input.models.every( + (model) => typeof model === "string" && models.includes(model), + ) + ) + return reply( + { error: "Native account authentication failed." }, + 409, + ); + const selectedProviders = providersForModels(input.models); + const selected = accounts.filter((account) => + selectedProviders.includes(account.route.providerID), + ); + lease = { + id: randomBytes(32).toString("hex"), + providers: selectedProviders, + }; + const acquired = lease; + try { + for (const account of selected) await validate(account); + } catch { + if (lease === acquired) lease = undefined; + refuse(); + } + if (disposed || lease !== acquired) refuse(); + return reply({ lease: acquired.id }); + } + const current = capability(input.lease); + if (path === "/release") { + if (lease !== undefined && current !== lease.id) refuse(); + lease = undefined; + return reply({}); + } + if ( + (path !== "/resolve" && path !== "/check") || + current !== lease?.id + ) + refuse(); + const selected = parseSelection(input); + if (!lease.providers.includes(selected.providerID)) refuse(); + const account = accounts.find( + (candidate) => + candidate.route.providerID === selected.providerID && + candidate.route.credentialID === selected.credentialID, + ); + if (!account) refuse(); + if (path === "/check") { + await rpc("check", selected, options.signal); + if (disposed || current !== lease?.id) refuse(); + return reply({}); + } + const resolved = await validate(account); + if (disposed || current !== lease?.id) refuse(); + return reply(resolved); + } catch { + return reply({ error: "Native account authentication failed." }, 503); + } + }, + }); + options.signal?.addEventListener("abort", abort, { once: true }); + if (options.signal?.aborted) { + await dispose(); + refuse(); + } + return { + configuration: { + version: 1, + url: relay.url.href, + capability: token, + accounts: accounts.map(privateAccount), + }, + dispose, + }; + } catch { + await dispose().catch(() => {}); + refuse(); + } +} diff --git a/evals/native-auth/client-plugin.ts b/evals/native-auth/client-plugin.ts new file mode 100644 index 00000000..f2a17cad --- /dev/null +++ b/evals/native-auth/client-plugin.ts @@ -0,0 +1,173 @@ +import { constants } from "node:fs"; +import { lstat, open } from "node:fs/promises"; +import { dirname, isAbsolute } from "node:path"; +import { Credential, Integration, Plugin } from "@opencode/plugin"; +import type { Context } from "@opencode/plugin/promise/plugin"; +import { + type NativeAccount, + object, + PLACEHOLDER_REFRESH, + parseChildOptions, + parseResolved, + refuse, + responseJSON, + sameMetadata, +} from "./protocol.js"; + +export async function registerNativeAccountClient( + ctx: Context, +): Promise<() => Promise> { + let configuration: unknown; + try { + if ( + !object(ctx.options) || + typeof ctx.options.configurationPath !== "string" || + !isAbsolute(ctx.options.configurationPath) + ) + refuse(); + const path = ctx.options.configurationPath; + const parent = await lstat(dirname(path)); + if ( + !parent.isDirectory() || + parent.uid !== process.getuid?.() || + (parent.mode & 0o777) !== 0o700 + ) + refuse(); + const file = await open(path, constants.O_RDONLY | constants.O_NOFOLLOW); + try { + const stat = await file.stat(); + if ( + !stat.isFile() || + stat.nlink !== 1 || + stat.uid !== process.getuid?.() || + (stat.mode & 0o777) !== 0o600 || + stat.size > 1024 * 1024 + ) + refuse(); + configuration = JSON.parse(await file.readFile("utf8")); + } finally { + await file.close(); + } + } catch { + refuse(); + } + const options = parseChildOptions(configuration); + const controller = new AbortController(); + const ensureSelected = async (account: NativeAccount) => { + const route = account.route; + const active = await ctx.integration.connection.active(route.integrationID); + if ( + active?.type !== "credential" || + active.id !== route.credentialID || + active.method !== "oauth" || + active.status + ) + refuse(); + }; + const request = async (path: "check" | "resolve", account: NativeAccount) => { + await ensureSelected(account); + return responseJSON( + await fetch(new URL(path, options.connection.url), { + method: "POST", + headers: { + authorization: `Bearer ${options.connection.capability}`, + "content-type": "application/json", + }, + body: JSON.stringify({ + lease: options.lease, + providerID: account.route.providerID, + credentialID: account.route.credentialID, + }), + signal: AbortSignal.any([ + controller.signal, + AbortSignal.timeout(15000), + ]), + redirect: "error", + }), + ); + }; + const selected = new Map(); + for (const account of options.connection.accounts) { + const key = `${account.route.integrationID}\0${account.route.methodID}`; + const previous = selected.get(key); + if (previous && previous.route.credentialID !== account.route.credentialID) + refuse(); + selected.set(key, account); + } + const registration = await ctx.integration.transform((editor) => { + for (const account of selected.values()) { + const route = account.route; + const method = editor.method + .list(route.integrationID) + .find( + (candidate) => + candidate.type === "oauth" && candidate.id === route.methodID, + ); + if (method?.type !== "oauth") refuse(); + editor.method.update({ + integrationID: route.integrationID, + method, + authorize: async () => refuse(), + refresh: async (credential) => { + try { + if ( + credential.refresh !== PLACEHOLDER_REFRESH || + credential.methodID !== route.methodID || + !sameMetadata(credential.metadata, account.metadata) + ) + refuse(); + const resolved = parseResolved(await request("resolve", account)); + if ( + !sameMetadata(resolved.route, route) || + !sameMetadata(resolved.value.metadata, account.metadata) + ) + refuse(); + return Credential.OAuth.make({ + ...resolved.value, + methodID: Integration.MethodID.make(route.methodID), + refresh: PLACEHOLDER_REFRESH, + }); + } catch { + refuse(); + } + }, + }); + } + }); + const guard = async (input: { model: { providerID: string } }) => { + try { + const account = options.connection.accounts.find( + (candidate) => candidate.route.providerID === input.model.providerID, + ); + if (!account) refuse(); + const provider = ( + await ctx.provider.get({ providerID: account.route.providerID }) + ).data; + if ( + provider.activation === "disabled" || + provider.integrationID !== account.route.integrationID + ) + refuse(); + await request("check", account); + } catch { + refuse(); + } + }; + const http = await ctx.session.hook("http.request", guard); + const websocket = await ctx.session.hook("experimental.ws.handshake", guard); + let disposed: Promise | undefined; + return () => { + disposed ??= (async () => { + controller.abort(); + await registration.dispose(); + await http.dispose(); + await websocket.dispose(); + })(); + return disposed; + }; +} + +export default Plugin.define({ + id: "flow.native-account-client", + setup: registerNativeAccountClient, +}); diff --git a/evals/native-auth/host.ts b/evals/native-auth/host.ts new file mode 100644 index 00000000..6f9429e0 --- /dev/null +++ b/evals/native-auth/host.ts @@ -0,0 +1,320 @@ +import { createHash } from "node:crypto"; +import { + chmod, + lstat, + mkdir, + readdir, + readFile, + realpath, + rm, + writeFile, +} from "node:fs/promises"; +import { dirname, isAbsolute, join } from "node:path"; +import { isDeepStrictEqual } from "node:util"; +import type { CredentialEntry } from "@opencode/client"; +import { canonicalSha256 } from "../canonical-json.js"; +import ClientPlugin from "./client-plugin.js"; +import { + capability, + type NativeAccountConnection, + object, + PLACEHOLDER_REFRESH, + parseConnection, + providersForModels, + refuse, + responseJSON, + UNRESOLVED_ACCESS, +} from "./protocol.js"; +import SourcePlugin from "./source-plugin.js"; + +export type StagedNativeAuthClient = { + pluginDirectory: string; + entrySha256: string; + sourceSha256: string; + files: Readonly>; +}; +export type NativeAuthHost = { + plugin: { package: string; options: { configurationPath: string } }; + credentialHandoff: { + entries: readonly CredentialEntry[]; + onStop(input: { + before: readonly CredentialEntry[]; + after: readonly CredentialEntry[]; + }): Promise; + }; + artifactHashes: { entrySha256: string; sourceSha256: string }; + dispose(): Promise; +}; +export type NativeAuthentication = { + configuration: NativeAccountConnection; + models: readonly string[]; +}; + +const sha = (bytes: string | Buffer) => + `sha256:${createHash("sha256").update(bytes).digest("hex")}`; +export function nativeAuthSourceSha256( + files: Readonly>, +): `sha256:${string}` { + return canonicalSha256("flow-native-auth-client-closure-v1", files); +} +async function pluginFiles( + kind: "client" | "source", +): Promise> { + const plugin = kind === "client" ? ClientPlugin : SourcePlugin; + return { + "index.ts": Buffer.from(`export { default } from "./${kind}-plugin.ts";\n`), + "package.json": Buffer.from( + `${JSON.stringify({ name: plugin.id, private: true, type: "module", exports: { ".": "./index.ts" } })}\n`, + ), + "protocol.ts": await readFile(new URL("./protocol.ts", import.meta.url)), + [`${kind}-plugin.ts`]: await readFile( + new URL(`./${kind}-plugin.ts`, import.meta.url), + ), + }; +} +async function stage( + kind: "client" | "source", + directory: string, +): Promise { + try { + if (!isAbsolute(directory)) refuse(); + await mkdir(directory, { mode: 0o700 }); + await chmod(directory, 0o700); + const files = await pluginFiles(kind); + for (const [name, bytes] of Object.entries(files)) + await writeFile(join(directory, name), bytes, { + mode: 0o600, + flag: "wx", + }); + const hashes = Object.fromEntries( + Object.entries(files).map(([name, bytes]) => [name, sha(bytes)]), + ); + return { + pluginDirectory: await realpath(directory), + entrySha256: sha(files["index.ts"] ?? ""), + sourceSha256: + kind === "client" + ? nativeAuthSourceSha256(hashes) + : canonicalSha256("flow-native-auth-source-closure-v1", hashes), + files: hashes, + }; + } catch { + refuse(); + } +} +export function stageNativeAuthClient( + directory: string, +): Promise { + return stage("client", directory); +} +export function stageNativeAuthSource( + directory: string, +): Promise { + return stage("source", directory); +} +export async function verifyNativeAuthClient( + artifact: StagedNativeAuthClient, +): Promise { + try { + const files = await pluginFiles("client"); + const directory = await lstat(artifact.pluginDirectory); + if ( + !directory.isDirectory() || + directory.isSymbolicLink() || + !isDeepStrictEqual( + (await readdir(artifact.pluginDirectory)).sort(), + Object.keys(files).sort(), + ) + ) + refuse(); + const expected = Object.fromEntries( + Object.entries(files).map(([name, bytes]) => [name, sha(bytes)]), + ); + if ( + !isDeepStrictEqual(expected, artifact.files) || + artifact.entrySha256 !== expected["index.ts"] || + artifact.sourceSha256 !== nativeAuthSourceSha256(expected) + ) + refuse(); + for (const [name, expectedHash] of Object.entries(expected)) { + const stat = await lstat(join(artifact.pluginDirectory, name)); + if ( + !stat.isFile() || + stat.isSymbolicLink() || + stat.nlink !== 1 || + sha(await readFile(join(artifact.pluginDirectory, name))) !== + expectedHash + ) + refuse(); + } + } catch { + refuse(); + } +} + +export function connectNativeAccountSource( + configuration: NativeAccountConnection, +) { + const connection = structuredClone(parseConnection(configuration)); + const active = new Set(); + let closed = false; + let closing: Promise | undefined; + const request = async (path: string, body: unknown, signal?: AbortSignal) => { + try { + return await responseJSON( + await fetch(new URL(path, connection.url), { + method: "POST", + headers: { + authorization: `Bearer ${connection.capability}`, + "content-type": "application/json", + }, + body: JSON.stringify(body), + signal: signal + ? AbortSignal.any([signal, AbortSignal.timeout(15000)]) + : AbortSignal.timeout(15000), + redirect: "error", + }), + ); + } catch { + refuse(); + } + }; + return { + async prepareHost(options: { + directory: string; + models: readonly string[]; + signal?: AbortSignal; + stagedClient?: StagedNativeAuthClient; + configurationPath?: string; + }): Promise { + if (closed || options.signal?.aborted || !isAbsolute(options.directory)) + refuse(); + const providers = providersForModels(options.models); + const accounts = connection.accounts.filter((account) => + providers.includes(account.route.providerID), + ); + if (accounts.length !== providers.length) refuse(); + const acquired = await request( + "acquire", + { models: options.models }, + options.signal, + ); + if (!object(acquired)) refuse(); + const lease = capability(acquired.lease); + let ownedDirectory = false; + let done: Promise | undefined; + const configurationPath = + options.configurationPath ?? + join(options.directory, "configuration.json"); + const dispose = () => { + done ??= (async () => { + try { + await request("release", { lease }); + } finally { + if (ownedDirectory) + await rm(options.directory, { recursive: true }); + } + })().catch(refuse); + return done; + }; + try { + await mkdir(options.directory, { mode: 0o700 }); + ownedDirectory = true; + const dir = await lstat(options.directory); + if (dir.uid !== process.getuid?.() || (dir.mode & 0o777) !== 0o700) + refuse(); + if (dirname(configurationPath) !== options.directory) refuse(); + const artifact = + options.stagedClient ?? + (await stageNativeAuthClient(join(options.directory, "plugin"))); + await verifyNativeAuthClient(artifact); + await writeFile( + configurationPath, + JSON.stringify({ connection: { ...connection, accounts }, lease }), + { mode: 0o600, flag: "wx" }, + ); + const entries = [ + ...new Map( + accounts.map((account) => [ + account.route.credentialID, + { + id: account.route.credentialID, + integrationID: account.route.integrationID, + label: account.route.label, + active: true, + value: { + type: "oauth" as const, + methodID: account.route.methodID, + access: UNRESOLVED_ACCESS, + refresh: PLACEHOLDER_REFRESH, + expires: 0, + ...(account.metadata === undefined + ? {} + : { metadata: account.metadata }), + }, + }, + ]), + ).values(), + ]; + const attachment: NativeAuthHost = { + plugin: { + package: artifact.pluginDirectory, + options: { configurationPath }, + }, + artifactHashes: { + entrySha256: artifact.entrySha256, + sourceSha256: artifact.sourceSha256, + }, + credentialHandoff: { + entries: structuredClone(entries), + onStop: async (input) => { + if ( + !isDeepStrictEqual(input.before, entries) || + input.after.length !== entries.length + ) + refuse(); + for (const entry of entries) { + const after = input.after.find( + (candidate) => candidate.id === entry.id, + ); + if ( + !after || + after.integrationID !== entry.integrationID || + after.label !== entry.label || + !after.active || + after.value.type !== "oauth" || + after.value.refresh !== PLACEHOLDER_REFRESH || + after.value.methodID !== entry.value.methodID || + !isDeepStrictEqual(after.value.metadata, entry.value.metadata) + ) + refuse(); + } + }, + }, + dispose: async () => { + await dispose(); + active.delete(attachment); + }, + }; + if (closed || options.signal?.aborted) { + await attachment.dispose(); + refuse(); + } + active.add(attachment); + return attachment; + } catch { + await dispose(); + refuse(); + } + }, + dispose() { + closing ??= (async () => { + closed = true; + await Promise.all( + [...active].map((attachment) => attachment.dispose()), + ); + })(); + return closing; + }, + }; +} diff --git a/evals/native-auth/protocol.ts b/evals/native-auth/protocol.ts new file mode 100644 index 00000000..0729d64e --- /dev/null +++ b/evals/native-auth/protocol.ts @@ -0,0 +1,291 @@ +import type { CredentialEntry, JsonValue } from "@opencode/client"; +import { Rpc } from "@opencode/plugin/rpc"; + +export const PLACEHOLDER_REFRESH = "flow-native-auth-source-owned"; +export const UNRESOLVED_ACCESS = "flow-native-auth-unresolved"; +export type OAuthValue = Extract; +export type AccountRoute = { + providerID: string; + integrationID: string; + credentialID: string; + methodID: string; + label: string; +}; +export type NativeAccount = { + route: AccountRoute; + metadata?: Record; +}; +export type ResolvedAccount = { route: AccountRoute; value: OAuthValue }; +export type NativeAccountConnection = { + version: 1; + url: string; + capability: string; + accounts: readonly NativeAccount[]; +}; +export type ChildOptions = { + connection: NativeAccountConnection; + lease: string; +}; + +export class NativeAuthError extends Error { + constructor() { + super("Native account authentication failed."); + this.name = "NativeAuthError"; + } +} +export function refuse(): never { + throw new NativeAuthError(); +} +export function object(value: unknown): value is Record { + return value !== null && typeof value === "object" && !Array.isArray(value); +} +export function identifier(value: unknown): value is string { + return typeof value === "string" && /^[A-Za-z0-9_.:@+-]{1,256}$/.test(value); +} +function json(value: unknown, depth = 0): value is JsonValue { + if (depth > 64) return false; + return ( + value === null || + typeof value === "string" || + typeof value === "boolean" || + (typeof value === "number" && Number.isFinite(value)) || + (Array.isArray(value) && value.every((item) => json(item, depth + 1))) || + (object(value) && + Object.values(value).every((item) => json(item, depth + 1))) + ); +} +export function containsSecret(value: unknown, secret: string): boolean { + if (!secret) return false; + if (typeof value === "string") return value.includes(secret); + if (Array.isArray(value)) + return value.some((item) => containsSecret(item, secret)); + return ( + object(value) && + Object.entries(value).some( + ([key, item]) => key.includes(secret) || containsSecret(item, secret), + ) + ); +} +export function metadata( + value: unknown, +): Record | undefined { + if (value === undefined) return undefined; + if (!object(value)) refuse(); + return Object.fromEntries( + Object.entries(value).map(([key, item]) => { + if (!json(item)) refuse(); + return [key, item] as const; + }), + ); +} +export function sameMetadata(left: unknown, right: unknown): boolean { + const stable = (value: unknown): string => { + if (Array.isArray(value)) return `[${value.map(stable).join(",")}]`; + if (object(value)) + return `{${Object.keys(value) + .sort() + .map((key) => `${JSON.stringify(key)}:${stable(value[key])}`) + .join(",")}}`; + return JSON.stringify(value) ?? "undefined"; + }; + return stable(left) === stable(right); +} +export function providersForModels(models: readonly string[]): string[] { + if (!Array.isArray(models) || models.length === 0) refuse(); + return [ + ...new Set( + models.map((model) => { + if (typeof model !== "string") refuse(); + const slash = model.indexOf("/"); + const provider = model.slice(0, slash); + if (slash < 1 || slash === model.length - 1 || !identifier(provider)) + refuse(); + return provider; + }), + ), + ]; +} +export function parseRoute(value: unknown): AccountRoute { + if ( + !object(value) || + !identifier(value.providerID) || + !identifier(value.integrationID) || + !identifier(value.credentialID) || + !identifier(value.methodID) || + typeof value.label !== "string" + ) + refuse(); + return { + providerID: value.providerID, + integrationID: value.integrationID, + credentialID: value.credentialID, + methodID: value.methodID, + label: value.label, + }; +} +export function parseOAuth(value: unknown, placeholder = true): OAuthValue { + if ( + !object(value) || + value.type !== "oauth" || + !identifier(value.methodID) || + typeof value.access !== "string" || + typeof value.refresh !== "string" || + (placeholder && value.refresh !== PLACEHOLDER_REFRESH) || + typeof value.expires !== "number" || + !Number.isSafeInteger(value.expires) || + value.expires < 0 + ) + refuse(); + const extra = metadata(value.metadata); + return { + type: "oauth", + methodID: value.methodID, + access: value.access, + refresh: value.refresh, + expires: value.expires, + ...(extra === undefined ? {} : { metadata: extra }), + }; +} +export function parseResolved(value: unknown): ResolvedAccount { + if (!object(value)) refuse(); + const route = parseRoute(value.route); + const credential = parseOAuth(value.value); + if (route.methodID !== credential.methodID) refuse(); + return { route, value: credential }; +} +export function privateAccount(value: ResolvedAccount): NativeAccount { + return { + route: value.route, + ...(value.value.metadata === undefined + ? {} + : { metadata: value.value.metadata }), + }; +} +export function loopbackURL(value: unknown): string { + try { + if (typeof value !== "string") refuse(); + const url = new URL(value); + if ( + url.protocol !== "http:" || + url.hostname !== "127.0.0.1" || + !url.port || + url.username || + url.password || + url.search || + url.hash || + url.pathname !== "/" + ) + refuse(); + return url.href; + } catch { + refuse(); + } +} +export function capability(value: unknown): string { + if (typeof value !== "string" || !/^[a-f0-9]{64}$/.test(value)) refuse(); + return value; +} +export function parseConnection(value: unknown): NativeAccountConnection { + if ( + !object(value) || + value.version !== 1 || + !Array.isArray(value.accounts) || + value.accounts.length === 0 + ) + refuse(); + const accounts = value.accounts.map((account: unknown): NativeAccount => { + if (!object(account)) refuse(); + const extra = metadata(account.metadata); + return { + route: parseRoute(account.route), + ...(extra === undefined ? {} : { metadata: extra }), + }; + }); + if ( + new Set(accounts.map((account) => account.route.providerID)).size !== + accounts.length + ) + refuse(); + const credentials = new Map(); + for (const account of accounts) { + const previous = credentials.get(account.route.credentialID); + if ( + previous && + (previous.route.integrationID !== account.route.integrationID || + previous.route.methodID !== account.route.methodID || + !sameMetadata(previous.metadata, account.metadata)) + ) + refuse(); + credentials.set(account.route.credentialID, account); + } + return { + version: 1, + url: loopbackURL(value.url), + capability: capability(value.capability), + accounts, + }; +} +export function parseChildOptions(value: unknown): ChildOptions { + if (!object(value)) refuse(); + return { + connection: parseConnection(value.connection), + lease: capability(value.lease), + }; +} +export function parseSelection(value: unknown): { + providerID: string; + credentialID: string; +} { + if ( + !object(value) || + !identifier(value.providerID) || + !identifier(value.credentialID) + ) + refuse(); + return { providerID: value.providerID, credentialID: value.credentialID }; +} +export async function responseJSON(response: Response): Promise { + try { + if (!response.ok) refuse(); + const body = await response.text(); + if (body.length > 1024 * 1024) refuse(); + return JSON.parse(body); + } catch { + refuse(); + } +} +const empty = { + type: "object", + properties: {}, + additionalProperties: false, +} as const; +const selection = { + type: "object", + properties: { + providerID: { type: "string" }, + credentialID: { type: "string" }, + }, + required: ["providerID", "credentialID"], + additionalProperties: false, +} as const; +export const NativeAccountRpc = Rpc.define({ + id: "flow.native-account-source", + methods: { + describe: { input: empty, output: { type: "object" } }, + check: { input: selection, output: empty }, + resolve: { + input: { + type: "object", + properties: { + providerID: { type: "string" }, + credentialID: { type: "string" }, + }, + required: ["providerID", "credentialID"], + additionalProperties: false, + }, + output: { type: "object" }, + }, + close: { input: empty, output: empty }, + }, + events: {}, +}); diff --git a/evals/native-auth/source-plugin.ts b/evals/native-auth/source-plugin.ts new file mode 100644 index 00000000..2588cb75 --- /dev/null +++ b/evals/native-auth/source-plugin.ts @@ -0,0 +1,184 @@ +import { Plugin } from "@opencode/plugin"; +import type { Context } from "@opencode/plugin/promise/plugin"; +import type { Registration } from "@opencode/plugin/promise/registration"; +import { + type AccountRoute, + containsSecret, + identifier, + NativeAccountRpc, + object, + PLACEHOLDER_REFRESH, + parseOAuth, + parseSelection, + type ResolvedAccount, + refuse, + sameMetadata, +} from "./protocol.js"; + +export async function registerNativeAccountSource( + ctx: Context, +): Promise<() => Promise> { + const options = ctx.options; + if ( + !object(options) || + !Array.isArray(options.providers) || + options.providers.length === 0 || + !options.providers.every(identifier) + ) + refuse(); + const providers = [...new Set(options.providers)]; + const pins = new Map(); + const pending = new Map>(); + let closed = false; + let initial: Promise | undefined; + let cleanup: Promise | undefined; + let registration: Registration; + const active = async (providerID: string, pin?: AccountRoute) => { + if (closed || !providers.includes(providerID)) refuse(); + const provider = (await ctx.provider.get({ providerID })).data; + if ( + provider.id !== providerID || + provider.activation === "disabled" || + !provider.integrationID || + (pin && provider.integrationID !== pin.integrationID) + ) + refuse(); + const connection = await ctx.integration.connection.active( + provider.integrationID, + ); + if ( + connection?.type !== "credential" || + connection.method !== "oauth" || + connection.status || + (pin && + (connection.id !== pin.credentialID || connection.label !== pin.label)) + ) + refuse(); + return { provider, connection }; + }; + const resolve = async ( + providerID: string, + pin?: ResolvedAccount, + ): Promise => { + const facts = await active(providerID, pin?.route); + const previous = pending.get(facts.connection.id); + if (previous) { + await previous; + return resolve(providerID, pin); + } + const operation = (async (): Promise => { + const credential = parseOAuth( + await ctx.integration.connection.resolve(facts.connection), + false, + ); + const integrationID = facts.provider.integrationID; + if (!integrationID) refuse(); + if ( + containsSecret(credential.access, credential.refresh) || + containsSecret(credential.metadata, credential.refresh) + ) + refuse(); + const integration = (await ctx.integration.get({ integrationID })).data; + if ( + !integration.methods.some( + (method) => + method.type === "oauth" && method.id === credential.methodID, + ) || + (pin && + (credential.methodID !== pin.route.methodID || + !sameMetadata(credential.metadata, pin.value.metadata))) + ) + refuse(); + const route = { + providerID, + integrationID, + credentialID: facts.connection.id, + methodID: credential.methodID, + label: facts.connection.label, + }; + await active(providerID, route); + return { route, value: { ...credential, refresh: PLACEHOLDER_REFRESH } }; + })(); + pending.set(facts.connection.id, operation); + try { + return await operation; + } finally { + if (pending.get(facts.connection.id) === operation) + pending.delete(facts.connection.id); + } + }; + const initialize = () => { + if (closed) refuse(); + initial ??= (async () => { + for (const providerID of providers) + pins.set(providerID, await resolve(providerID)); + })(); + return initial; + }; + const dispose = () => { + cleanup ??= (async () => { + closed = true; + await initial?.catch(() => {}); + await Promise.allSettled([...pending.values()]); + pins.clear(); + await registration.dispose(); + })(); + return cleanup; + }; + registration = await ctx.rpc.register(NativeAccountRpc, { + describe: async () => { + try { + await initialize(); + const accounts: ResolvedAccount[] = []; + for (const [providerID, pin] of pins) + accounts.push(await resolve(providerID, pin)); + return { accounts }; + } catch { + refuse(); + } + }, + resolve: async (input) => { + try { + const selected = parseSelection(input); + await initialize(); + const pin = pins.get(selected.providerID); + if (!pin || pin.route.credentialID !== selected.credentialID) refuse(); + return await resolve(selected.providerID, pin); + } catch { + refuse(); + } + }, + check: async (input) => { + try { + const selected = parseSelection(input); + await initialize(); + const pin = pins.get(selected.providerID); + if (!pin || pin.route.credentialID !== selected.credentialID) refuse(); + await active(selected.providerID, pin.route); + const integration = ( + await ctx.integration.get({ integrationID: pin.route.integrationID }) + ).data; + if ( + !integration.methods.some( + (method) => + method.type === "oauth" && method.id === pin.route.methodID, + ) + ) + refuse(); + return {}; + } catch { + refuse(); + } + }, + close: async () => { + await dispose(); + return {}; + }, + }); + return dispose; +} + +export default Plugin.define({ + id: "flow.native-account-source", + setup: registerNativeAccountSource, +}); diff --git a/evals/opencode-host.ts b/evals/opencode-host.ts new file mode 100644 index 00000000..367421ef --- /dev/null +++ b/evals/opencode-host.ts @@ -0,0 +1,340 @@ +import { mkdir, readFile, realpath, writeFile } from "node:fs/promises"; +import { isAbsolute, join, relative, resolve } from "node:path"; +import { pathToFileURL } from "node:url"; +import { + type ModelRef, + OpenCode, + type OpenCodeClient, + type SessionInfo, + type SessionMessageInfo, +} from "@opencode/client"; +import { isRecord } from "./host-observation.js"; + +export const OPENCODE_HOST_VERSION = "2.1.4"; + +export function validateOpenCodeVersion(version: string): void { + const match = /^2\.(0|[1-9]\d*)\.(0|[1-9]\d*)$/.exec(version); + if ( + !match || + !Number.isSafeInteger(Number(match[1])) || + !Number.isSafeInteger(Number(match[2])) || + Number(match[1]) < 1 || + (Number(match[1]) === 1 && Number(match[2]) < 4) + ) + throw new Error( + "OpenCode host requires an exact tested 2.x version at least 2.1.4.", + ); +} + +export function openCodeInvocation( + args: readonly string[], + version = OPENCODE_HOST_VERSION, +): { command: string; args: string[] } { + validateOpenCodeVersion(version); + return { + command: "npm", + args: [ + "exec", + "--yes", + `--package=@opencode/cli@${version}`, + "--", + "opencode", + ...args, + ], + }; +} + +export class OpenCodeStartup { + private readonly pending = new Map(); + private password: string | undefined; + private output = ""; + consume(chunk: string, stream = "stdout"): void { + let pending = (this.pending.get(stream) ?? "") + chunk; + for (;;) { + const end = pending.indexOf("\n"); + if (end < 0) break; + const line = pending.slice(0, end).replace(/\r$/, ""); + pending = pending.slice(end + 1); + const match = /server password (\S+)/.exec(line); + if (match) this.password = match[1]; + else this.output += `${line}\n`; + } + this.pending.set(stream, pending); + } + get log(): string { + return this.output; + } + client(baseUrl: string, directory: string): OpenCodeClient | null { + if (!this.password) return null; + return OpenCode.make({ + baseUrl, + headers: { + authorization: `Basic ${Buffer.from(`opencode:${this.password}`).toString("base64")}`, + "x-opencode-directory": directory, + }, + }); + } + clear(): void { + this.pending.clear(); + this.password = undefined; + } +} + +export function nativeModel(model: string, variant?: string): ModelRef { + const slash = model.indexOf("/"); + if (slash <= 0 || slash === model.length - 1) + throw new Error("Model must have provider/id form."); + return { + providerID: model.slice(0, slash), + id: model.slice(slash + 1), + ...(variant === undefined ? {} : { variant }), + }; +} + +export async function listNativeMessages( + client: OpenCodeClient, + sessionID: string, + signal?: AbortSignal, +): Promise { + const messages: SessionMessageInfo[] = []; + const cursors = new Set(); + let cursor: string | undefined; + for (;;) { + const page = await client.message.list( + { sessionID, limit: 100, ...(cursor ? { cursor } : { order: "asc" }) }, + { ...(signal ? { signal } : {}) }, + ); + messages.push(...page.data); + cursor = page.cursor.next ?? undefined; + if (messages.length > 4096) + throw new Error("Native message limit exceeded."); + if (!cursor) return messages; + if (cursors.has(cursor)) + throw new Error("Native message pagination did not converge."); + cursors.add(cursor); + } +} + +export async function listNativeSessions( + client: OpenCodeClient, + directory: string, + signal?: AbortSignal, +): Promise { + const sessions: SessionInfo[] = []; + const cursors = new Set(); + let cursor: string | undefined; + for (;;) { + const page = await client.session.list( + { limit: 100, ...(cursor ? { cursor } : { directory, order: "asc" }) }, + { ...(signal ? { signal } : {}) }, + ); + if ( + !isRecord(page) || + !Array.isArray(page.data) || + !isRecord(page.cursor) || + (page.cursor.next !== undefined && + page.cursor.next !== null && + (typeof page.cursor.next !== "string" || + page.cursor.next.length === 0 || + page.cursor.next.length > 4096)) + ) + throw new Error("Owned progress children response is malformed."); + sessions.push(...page.data); + cursor = page.cursor.next ?? undefined; + if (sessions.length > 4096) + throw new Error("Native session limit exceeded."); + if (!cursor) return sessions; + if (cursors.has(cursor)) + throw new Error("Native session pagination did not converge."); + cursors.add(cursor); + } +} +export type EvalMessage = { + native: SessionMessageInfo; + info: { + id?: string; + sessionID?: string; + parentID?: string; + summary?: unknown; + role: string; + agent?: string; + model?: { providerID?: unknown; modelID?: unknown }; + providerID?: unknown; + modelID?: unknown; + time?: { created: number; completed?: number }; + error?: unknown; + cost?: number; + tokens?: { + input: number; + output: number; + reasoning: number; + cache?: { read: number; write: number }; + }; + }; + parts: { + id?: string; + sessionID?: string; + callID?: string; + messageID?: string; + type: string; + tool?: string; + text?: string; + time?: { start?: number; end?: number }; + synthetic?: boolean; + auto?: boolean; + metadata?: Record; + state?: { + raw?: string; + time?: { start?: number; end?: number }; + status: "pending" | "running" | "completed" | "error"; + input?: Record; + output?: string; + error?: string; + metadata?: Record; + }; + }[]; +}; + +export function projectEvalMessage( + native: SessionMessageInfo, + sessionID: string, +): EvalMessage { + const info: EvalMessage["info"] = { + id: native.id, + sessionID, + role: native.type, + time: { ...native.time }, + }; + const parts: EvalMessage["parts"] = []; + if (native.type === "assistant") { + info.agent = native.agent; + info.model = { + providerID: native.model.providerID, + modelID: native.model.id, + }; + if (native.cost !== undefined) info.cost = native.cost; + if (native.tokens !== undefined) info.tokens = native.tokens; + if (native.error !== undefined) info.error = native.error; + for (const content of native.content) { + if (content.type !== "tool") { + parts.push({ + type: content.type, + text: content.text, + sessionID, + messageID: native.id, + }); + continue; + } + const state = content.state; + const output = + state.status === "completed" || state.status === "error" + ? (state.content ?? []) + .flatMap((item) => (item.type === "text" ? [item.text] : [])) + .join("\n") + : undefined; + parts.push({ + type: "tool", + tool: content.name, + callID: content.id, + sessionID, + messageID: native.id, + time: { + start: content.time.created, + ...(content.time.completed === undefined + ? {} + : { end: content.time.completed }), + }, + state: { + status: state.status === "streaming" ? "pending" : state.status, + input: state.status === "streaming" ? {} : state.input, + ...(state.status === "streaming" ? { raw: state.input } : {}), + ...(output === undefined ? {} : { output }), + ...(state.status === "error" ? { error: state.error.message } : {}), + ...(state.status === "streaming" || state.metadata === undefined + ? {} + : { metadata: state.metadata }), + }, + }); + } + } else if (native.type === "compaction") { + info.summary = true; + if (native.status !== "running") { + if (native.status === "failed") info.error = native.error; + if (native.cost !== undefined) info.cost = native.cost; + if (native.tokens !== undefined) info.tokens = native.tokens; + if (native.status === "completed" && native.model) + info.model = { + providerID: native.model.providerID, + modelID: native.model.id, + }; + } + parts.push({ + type: "compaction", + auto: native.reason === "auto", + text: native.status === "failed" ? native.error.message : native.summary, + sessionID, + messageID: native.id, + }); + } else if ( + native.type === "user" || + native.type === "synthetic" || + native.type === "system" + ) { + parts.push({ + type: "text", + text: native.text, + synthetic: native.type === "synthetic", + sessionID, + messageID: native.id, + }); + } + return { native, info, parts }; +} + +export async function installedOpenCodePluginEntry( + installed: string, + entryDirectory: string, +): Promise { + const manifest: unknown = JSON.parse( + await readFile(join(installed, "package.json"), "utf8"), + ); + if (!isRecord(manifest) || !isRecord(manifest.exports)) + throw new Error("Installed plugin has no public exports."); + const root = await realpath(installed); + const entries: { file: string; target: string }[] = []; + for (const [key, file] of [ + [".", "index.js"], + ["./tui", "tui.js"], + ] as const) { + const entry = manifest.exports[key]; + const specifier = + typeof entry === "string" + ? entry + : isRecord(entry) + ? entry.import + : undefined; + if (typeof specifier !== "string" || !specifier.startsWith("./")) + throw new Error( + `Installed plugin export ${key} has no relative import entry.`, + ); + const target = await realpath(resolve(root, specifier)); + const inside = relative(root, target); + if (!inside || isAbsolute(inside) || inside.split(/[\\/]/)[0] === "..") + throw new Error("Installed plugin export escapes its package."); + entries.push({ file, target }); + } + await mkdir(entryDirectory, { recursive: true }); + await writeFile( + join(entryDirectory, "package.json"), + JSON.stringify({ + type: "module", + exports: { ".": "./index.js", "./tui": "./tui.js" }, + }), + ); + for (const entry of entries) + await writeFile( + join(entryDirectory, entry.file), + `export { default } from ${JSON.stringify(pathToFileURL(entry.target).href)};\n`, + ); + return entryDirectory; +} diff --git a/evals/recovery-decisions/budget-plugin.ts b/evals/recovery-decisions/budget-plugin.ts index 9039007d..f242bac7 100644 --- a/evals/recovery-decisions/budget-plugin.ts +++ b/evals/recovery-decisions/budget-plugin.ts @@ -1,5 +1,7 @@ +import { randomUUID } from "node:crypto"; import { dirname, join } from "node:path"; -import type { Plugin } from "@opencode-ai/plugin"; +import { Plugin } from "@opencode/plugin"; +import type { Context } from "@opencode/plugin/promise/plugin"; import { z } from "zod"; import { writeExclusive } from "../../scripts/lib/exclusive-json.js"; import { @@ -29,7 +31,7 @@ let installed: fetch: typeof globalThis.fetch; webSocket: typeof globalThis.WebSocket; directory: string; - controlOrigin: string; + attach: (context: Context) => Promise; scriptDigest: string | null; scopeDigest: string | null; } @@ -38,14 +40,12 @@ function assertInstalledRequestGate(expected: { directory: string; authorizationDigest: string; scope: EpisodeReservationScope; - controlOrigin: string; origin: "live"; }): void { if ( !installed?.ready || installed.digest !== expected.authorizationDigest || installed.directory !== expected.directory || - installed.controlOrigin !== expected.controlOrigin || installed.origin !== expected.origin || installed.scriptDigest !== null || installed.scopeDigest !== datasetDigest(expected.scope) || @@ -54,7 +54,20 @@ function assertInstalledRequestGate(expected: { ) throw new Error("Matching live evaluation request gate is unavailable."); } -const BudgetPlugin: Plugin = async (context, input) => { +let installing = false; +async function install(context: Context, input: unknown): Promise { + if (installing) + throw new Error( + "Evaluation request gate installation is already in progress.", + ); + installing = true; + try { + await installOnce(context, input); + } finally { + installing = false; + } +} +async function installOnce(context: Context, input: unknown): Promise { const options = Options.parse(input); const scopeDigest = options.scope ? datasetDigest(options.scope) : null; const status = await requestBudgetStatus(options.directory); @@ -64,7 +77,6 @@ const BudgetPlugin: Plugin = async (context, input) => { Date.parse(status.authorization.expiresAt) <= Date.now() ) throw new Error("Evaluation request authorization unavailable."); - const controlOrigin = context.serverUrl.origin; if (installed) { if ( !installed.ready || @@ -73,7 +85,6 @@ const BudgetPlugin: Plugin = async (context, input) => { installed.digest !== options.authorizationDigest || installed.directory !== options.directory || installed.scopeDigest !== scopeDigest || - installed.controlOrigin !== controlOrigin || installed.scriptDigest !== (options.simulationScript ? datasetDigest(options.simulationScript) @@ -82,7 +93,8 @@ const BudgetPlugin: Plugin = async (context, input) => { throw new Error( "One evaluation request budget is allowed per host process.", ); - return {}; + await installed.attach(context); + return; } if (options.simulationScript && status.authorization.origin !== "simulation") throw new Error("Simulation script requires simulation authorization."); @@ -99,12 +111,39 @@ const BudgetPlugin: Plugin = async (context, input) => { ) : undefined; const original = globalThis.fetch; - const gated = createRequestGate({ + const pending = new Map(); + let gated: ReturnType; + const proxy = Bun.serve({ + hostname: "127.0.0.1", + port: 0, + idleTimeout: 0, + async fetch(request) { + const key = new URL(request.url).pathname.slice(1); + const originalRequest = pending.get(key); + if (!originalRequest) + return new Response("Unknown evaluation request", { status: 403 }); + pending.delete(key); + try { + return await gated( + new Request(originalRequest, { + signal: AbortSignal.any([request.signal, originalRequest.signal]), + }), + ); + } catch { + return new Response( + "Evaluation request refused; reservation remains consumed", + { status: 503 }, + ); + } + }, + }); + const proxyOrigin = proxy.url.origin; + gated = createRequestGate({ ...options, - controlOrigin, + controlOrigin: proxyOrigin, transport: (request) => status.authorization.origin === "simulation" && - new URL(request.url).origin !== controlOrigin && + new URL(request.url).origin !== proxyOrigin && request.url !== "https://models.dev/api.json" ? simulate ? simulate(request) @@ -113,6 +152,27 @@ const BudgetPlugin: Plugin = async (context, input) => { ) : original(request), }); + const attached = new WeakSet(); + const attach = async (context: Context) => { + if (attached.has(context)) return; + await context.session.hook("http.request", (input) => { + if (pending.size >= 128) + throw new Error("Too many pending evaluation requests."); + const key = randomUUID(); + pending.set(key, input.request); + input.request = new Request(`${proxyOrigin}/${key}`, { + method: "POST", + signal: input.request.signal, + }); + }); + await context.session.hook("experimental.ws.handshake", () => { + throw new Error( + "WebSocket inference is disabled in budgeted evaluations.", + ); + }); + attached.add(context); + }; + await attach(context); globalThis.fetch = Object.assign(gated, { preconnect: original.preconnect }); globalThis.WebSocket = new Proxy(globalThis.WebSocket, { construct() { @@ -139,7 +199,7 @@ const BudgetPlugin: Plugin = async (context, input) => { scopeDigest, digest: options.authorizationDigest, directory: options.directory, - controlOrigin, + attach, scriptDigest: options.simulationScript ? datasetDigest(options.simulationScript) : null, @@ -154,6 +214,13 @@ const BudgetPlugin: Plugin = async (context, input) => { : null, }); installed.ready = true; - return {}; -}; -export default Object.assign(BudgetPlugin, { assertInstalledRequestGate }); + return; +} +const BudgetPlugin = Plugin.define({ + id: "flow-eval-request-budget", + setup: (context) => install(context, context.options), +}); +export default Object.assign(BudgetPlugin, { + assertInstalledRequestGate, + install, +}); diff --git a/evals/recovery-decisions/capture-plugin.ts b/evals/recovery-decisions/capture-plugin.ts index 7270b116..69287c4a 100644 --- a/evals/recovery-decisions/capture-plugin.ts +++ b/evals/recovery-decisions/capture-plugin.ts @@ -1,4 +1,4 @@ -import type { Plugin } from "@opencode-ai/plugin"; +import { Plugin } from "@opencode/plugin"; import { z } from "zod"; import { resolveWorkspaceRoot } from "../../src/infrastructure/fs/workspace-paths.js"; import { createFlowPlugin } from "../../src/platform/opencode/plugin-composition.js"; @@ -10,15 +10,19 @@ const Options = z reviewer: z.unknown().optional(), }) .strict(); -const CapturePlugin: Plugin = async (context, input) => { - const options = Options.parse(input); - const recovery = await CapturingRecoveryController.create( - resolveWorkspaceRoot(context), - options.captureDirectory, - ); - return createFlowPlugin({ - entryUrl: import.meta.url, - createRecovery: () => recovery, - })(context, { reviewer: options.reviewer }); -}; +const CapturePlugin = Plugin.define({ + id: "flow-eval-capture", + async setup(context) { + const input = context.options; + const options = Options.parse(input); + const recovery = await CapturingRecoveryController.create( + resolveWorkspaceRoot({ directory: context.location.directory }), + options.captureDirectory, + ); + return createFlowPlugin({ + entryUrl: import.meta.url, + createRecovery: () => recovery, + }).setup({ ...context, options: { reviewer: options.reviewer } }); + }, +}); export default CapturePlugin; diff --git a/evals/recovery-decisions/episode-host.ts b/evals/recovery-decisions/episode-host.ts index 3b561673..1e3440ca 100644 --- a/evals/recovery-decisions/episode-host.ts +++ b/evals/recovery-decisions/episode-host.ts @@ -117,6 +117,7 @@ export type EpisodeHostOptions = { | "opencodeVersion" | "requestBudget" | "providerCredentials" + | "credentialHandoff" | "recoveryTreatment" >; hostFactory?: (options: StartOptions) => Promise; @@ -161,7 +162,31 @@ async function workload( export async function createEpisodeHostDriver( options: EpisodeHostOptions, ): Promise { - options = { ...options, host: structuredClone(options.host) }; + const { credentialHandoff, ...hostOptions } = options.host; + options = { + ...options, + host: { + ...structuredClone(hostOptions), + ...(credentialHandoff + ? { + credentialHandoff: { + entries: structuredClone(credentialHandoff.entries), + onStop: credentialHandoff.onStop, + }, + } + : {}), + }, + }; + if (options.host.providerCredentials === "inherit" && !credentialHandoff) + throw new Error( + "Native credential inheritance requires an explicit credentialHandoff.", + ); + if ( + credentialHandoff && + (options.host.providerCredentials !== "inherit" || + options.host.recoveryTreatment?.origin === "simulation") + ) + throw new Error("Native credentials are disabled for this host."); if (options.host.requestBudget) options.host.requestBudget.directory = resolve( options.host.requestBudget.directory, @@ -247,7 +272,7 @@ export async function createEpisodeHostDriver( manager, recoveryTreatment: options.host.recoveryTreatment ?? null, treatmentBundleSha256: frozenTreatmentBundle?.sha256 ?? null, - providerCredentials: options.host.providerCredentials ?? "inherit", + providerCredentials: options.host.providerCredentials ?? "disabled", sources, host: { bun: options.host.toolchain.actualVersion, @@ -261,7 +286,6 @@ export async function createEpisodeHostDriver( withFlow: true, ambientConfig: "disabled", reviewerEnvironment: "disabled", - nativeLlm: false, }, }); const lifecycle = new AbortController(); @@ -391,7 +415,6 @@ export async function createEpisodeHostDriver( withFlow: true, ambientConfig: "disabled", reviewerEnvironment: "disabled", - nativeLlm: false, signal: AbortSignal.any([signal, lifecycle.signal]), }); host = await starting; diff --git a/evals/recovery-decisions/live-treatment-plugin.ts b/evals/recovery-decisions/live-treatment-plugin.ts index e97efe4e..2e4f4246 100644 --- a/evals/recovery-decisions/live-treatment-plugin.ts +++ b/evals/recovery-decisions/live-treatment-plugin.ts @@ -1,7 +1,7 @@ import { createHash } from "node:crypto"; import { readFile } from "node:fs/promises"; import { fileURLToPath } from "node:url"; -import type { Plugin } from "@opencode-ai/plugin"; +import { Plugin } from "@opencode/plugin"; import { z } from "zod"; import { writeExclusive } from "../../scripts/lib/exclusive-json.js"; import { RecoveryController } from "../../src/application/recovery-policy.js"; @@ -30,67 +30,72 @@ const Options = z budgetReadyPath: z.string().min(1), }) .strict(); -const LiveTreatmentPlugin: Plugin = async (context, input) => { - const options = Options.parse(input); - const treatment = options.budget.scope.arm === "manager-plus-jev"; - await validateLiveTreatmentBudget(options.budget); - if (treatment && !process.env.TYPESAFE_API_KEY) - throw new Error("Live evaluation credential unavailable."); - const expected = { - ...options.budget, - controlOrigin: context.serverUrl.origin, - origin: options.origin, - }; - await BudgetPlugin(context, { - directory: options.budget.directory, - authorizationDigest: options.budget.authorizationDigest, - scope: options.budget.scope, - readyPath: options.budgetReadyPath, - }); - BudgetPlugin.assertInstalledRequestGate(expected); - const gatedFetch = globalThis.fetch; - const hooks = await createFlowPlugin({ - entryUrl: import.meta.url, - createRecovery: () => - new RecoveryController( - treatment - ? createJevDecisionProvider( - () => { - BudgetPlugin.assertInstalledRequestGate(expected); - return process.env.TYPESAFE_API_KEY; - }, - (url, init) => { - BudgetPlugin.assertInstalledRequestGate(expected); - return gatedFetch(url, init); - }, - ) - : { - async assess() { - return { kind: "unavailable", reason: "manager-only" }; - }, - }, - { profiles: treatment ? [ExperimentalLiveProfile] : [] }, - ), - })(context); - try { - BudgetPlugin.assertInstalledRequestGate(expected); - await writeExclusive(options.readyPath, { - origin: "live", - arm: options.budget.scope.arm, - qualification: "experimental-evaluation", - pid: process.pid, +const LiveTreatmentPlugin = Plugin.define({ + id: "flow-eval-live-treatment", + async setup(context) { + const input = context.options; + const options = Options.parse(input); + const treatment = options.budget.scope.arm === "manager-plus-jev"; + await validateLiveTreatmentBudget(options.budget); + if (treatment && !process.env.TYPESAFE_API_KEY) + throw new Error("Live evaluation credential unavailable."); + const expected = { + ...options.budget, + origin: options.origin, + }; + await BudgetPlugin.install(context, { + directory: options.budget.directory, authorizationDigest: options.budget.authorizationDigest, - scopeDigest: datasetDigest(options.budget.scope), - profileDigest: treatment ? datasetDigest(ExperimentalLiveProfile) : null, - pluginEntrySha256: createHash("sha256") - .update(await readFile(fileURLToPath(import.meta.url))) - .digest("hex"), + scope: options.budget.scope, + readyPath: options.budgetReadyPath, }); BudgetPlugin.assertInstalledRequestGate(expected); - } catch (error) { - await hooks.dispose?.(); - throw error; - } - return hooks; -}; + const gatedFetch = globalThis.fetch; + const cleanup = await createFlowPlugin({ + entryUrl: import.meta.url, + createRecovery: () => + new RecoveryController( + treatment + ? createJevDecisionProvider( + () => { + BudgetPlugin.assertInstalledRequestGate(expected); + return process.env.TYPESAFE_API_KEY; + }, + (url, init) => { + BudgetPlugin.assertInstalledRequestGate(expected); + return gatedFetch(url, init); + }, + ) + : { + async assess() { + return { kind: "unavailable", reason: "manager-only" }; + }, + }, + { profiles: treatment ? [ExperimentalLiveProfile] : [] }, + ), + }).setup({ ...context, options: {} }); + try { + BudgetPlugin.assertInstalledRequestGate(expected); + await writeExclusive(options.readyPath, { + origin: "live", + arm: options.budget.scope.arm, + qualification: "experimental-evaluation", + pid: process.pid, + authorizationDigest: options.budget.authorizationDigest, + scopeDigest: datasetDigest(options.budget.scope), + profileDigest: treatment + ? datasetDigest(ExperimentalLiveProfile) + : null, + pluginEntrySha256: createHash("sha256") + .update(await readFile(fileURLToPath(import.meta.url))) + .digest("hex"), + }); + BudgetPlugin.assertInstalledRequestGate(expected); + } catch (error) { + await cleanup?.(); + throw error; + } + return cleanup; + }, +}); export default LiveTreatmentPlugin; diff --git a/evals/recovery-decisions/treatment-plugin.ts b/evals/recovery-decisions/treatment-plugin.ts index 839e0723..83abc448 100644 --- a/evals/recovery-decisions/treatment-plugin.ts +++ b/evals/recovery-decisions/treatment-plugin.ts @@ -1,7 +1,7 @@ import { createHash } from "node:crypto"; import { readFile } from "node:fs/promises"; import { fileURLToPath } from "node:url"; -import type { Plugin } from "@opencode-ai/plugin"; +import { Plugin } from "@opencode/plugin"; import { z } from "zod"; import { writeExclusive } from "../../scripts/lib/exclusive-json.js"; import { RecoveryController } from "../../src/application/recovery-policy.js"; @@ -30,51 +30,55 @@ const Options = z budgetReadyPath: z.string().min(1), }) .strict(); -const TreatmentPlugin: Plugin = async (context, input) => { - const options = Options.parse(input); - await validateTreatmentBudget(options.treatment, options.budget); - const gate = z - .object({ - pid: z.number(), - scopeDigest: z.string().nullable(), - authorizationDigest: z.string(), - scriptDigest: z.string(), - origin: z.literal("simulation"), - }) - .parse(JSON.parse(await readFile(options.budgetReadyPath, "utf8"))); - if ( - gate.pid !== process.pid || - gate.scopeDigest !== - (options.budget.scope ? datasetDigest(options.budget.scope) : null) || - gate.authorizationDigest !== options.budget.authorizationDigest || - gate.scriptDigest !== datasetDigest(options.treatment.script) - ) - throw new Error("Simulation gate is not installed in this process."); - const hooks = await createFlowPlugin({ - entryUrl: import.meta.url, - createRecovery: () => - new RecoveryController( - createJevDecisionProvider(() => "simulation-jev-credential"), - options.treatment.arm === "manager-plus-jev" - ? { profiles: [ExperimentalProfile] } - : {}, - ), - })(context); - try { - await writeExclusive(options.readyPath, { - origin: "simulation", - arm: options.treatment.arm, - pid: process.pid, - authorizationDigest: options.budget.authorizationDigest, - treatmentDigest: datasetDigest(options.treatment), - pluginEntrySha256: createHash("sha256") - .update(await readFile(fileURLToPath(import.meta.url))) - .digest("hex"), - }); - } catch (error) { - await hooks.dispose?.(); - throw error; - } - return hooks; -}; +const TreatmentPlugin = Plugin.define({ + id: "flow-eval-treatment", + async setup(context) { + const input = context.options; + const options = Options.parse(input); + await validateTreatmentBudget(options.treatment, options.budget); + const gate = z + .object({ + pid: z.number(), + scopeDigest: z.string().nullable(), + authorizationDigest: z.string(), + scriptDigest: z.string(), + origin: z.literal("simulation"), + }) + .parse(JSON.parse(await readFile(options.budgetReadyPath, "utf8"))); + if ( + gate.pid !== process.pid || + gate.scopeDigest !== + (options.budget.scope ? datasetDigest(options.budget.scope) : null) || + gate.authorizationDigest !== options.budget.authorizationDigest || + gate.scriptDigest !== datasetDigest(options.treatment.script) + ) + throw new Error("Simulation gate is not installed in this process."); + const cleanup = await createFlowPlugin({ + entryUrl: import.meta.url, + createRecovery: () => + new RecoveryController( + createJevDecisionProvider(() => "simulation-jev-credential"), + options.treatment.arm === "manager-plus-jev" + ? { profiles: [ExperimentalProfile] } + : {}, + ), + }).setup({ ...context, options: {} }); + try { + await writeExclusive(options.readyPath, { + origin: "simulation", + arm: options.treatment.arm, + pid: process.pid, + authorizationDigest: options.budget.authorizationDigest, + treatmentDigest: datasetDigest(options.treatment), + pluginEntrySha256: createHash("sha256") + .update(await readFile(fileURLToPath(import.meta.url))) + .digest("hex"), + }); + } catch (error) { + await cleanup?.(); + throw error; + } + return cleanup; + }, +}); export default TreatmentPlugin; diff --git a/evals/release-policy.ts b/evals/release-policy.ts index 5cb417c6..9ed9cb7d 100644 --- a/evals/release-policy.ts +++ b/evals/release-policy.ts @@ -191,7 +191,21 @@ const STANDARD_RELEASE: ReleaseProfile = { requiredModels: null, }; +export function targetsOpenCodeV2(packageVersion: string): boolean { + const match = /^(\d+)\./.exec(packageVersion); + return match !== null && Number(match[1]) >= 10; +} + +const V2_RELEASE: ReleaseProfile = { + catalog: DELIVERY_RELEASE_CATALOG.map((row) => ({ + ...row, + minProviders: 2, + })), + requiredModels: null, +}; + export function releaseProfile(packageVersion: string): ReleaseProfile { + if (targetsOpenCodeV2(packageVersion)) return V2_RELEASE; if (packageVersion === "9.6.0") return openAiOnlyRelease(DELIVERY_RELEASE_CATALOG, "gpt-6.1-sol"); if (packageVersion === "9.5.0") @@ -208,7 +222,9 @@ export function releaseProfile(packageVersion: string): ReleaseProfile { export function releaseReviewerModel( packageVersion: string, + managerModel?: ModelIdentity, ): ModelIdentity | null { + if (targetsOpenCodeV2(packageVersion)) return managerModel ?? null; if (packageVersion !== "9.6.0") return null; const model = releaseProfile(packageVersion).requiredModels?.[0]; if (!model) throw new Error("Pinned release reviewer model is absent."); @@ -228,7 +244,29 @@ export const RELEASE_HOST_POLICY = { reviewerSteps: null, } as const; +const V2_RELEASE_HOST_POLICY = { + opencodeVersion: "2.1.4", + platform: "linux", + reviewerSteps: null, + ambientConfig: "disabled", + transport: "http", +} as const; + +export function releaseHostPolicy(packageVersion: string) { + return targetsOpenCodeV2(packageVersion) + ? V2_RELEASE_HOST_POLICY + : RELEASE_HOST_POLICY; +} + export function releaseHostPermissions(packageVersion: string) { + if (targetsOpenCodeV2(packageVersion)) + return [ + { + action: "external_directory", + resource: "*", + effect: "deny" as const, + }, + ]; return packageVersion === "9.5.0" || packageVersion === "9.6.0" ? { external_directory: "deny" as const } : undefined; @@ -242,7 +280,10 @@ export function releasePolicySha256(packageVersion: string): string { ? {} : { requiredModels: profile.requiredModels }), host: { - ...RELEASE_HOST_POLICY, + ...releaseHostPolicy(packageVersion), + ...(targetsOpenCodeV2(packageVersion) + ? { reviewer: "same-as-manager", recovery: "off" } + : {}), ...(releaseHostPermissions(packageVersion) ? { permission: releaseHostPermissions(packageVersion) } : {}), @@ -329,7 +370,7 @@ export function releasePrimaryCellsFor( armToken: null, repetition, managerModel: model, - reviewerModel: releaseReviewerModel(packageVersion), + reviewerModel: releaseReviewerModel(packageVersion, model), schedule: "primary" as const, }; }), @@ -359,7 +400,7 @@ export function releaseCellsFor( armToken: null, repetition: policy.minScoredAttempts, managerModel: model, - reviewerModel: releaseReviewerModel(packageVersion), + reviewerModel: releaseReviewerModel(packageVersion, model), schedule: "environment-reserve" as const, }; }), @@ -383,13 +424,20 @@ export function releaseHostConfigSha256(input: { readonly model: ModelIdentity; }): string { const model = `${input.model.routeProvider}/${input.model.model}`; + const host = releaseHostPolicy(input.packageVersion); return canonicalSha256("flow-eval-host-config-v1", { - opencodeVersion: RELEASE_HOST_POLICY.opencodeVersion, + opencodeVersion: host.opencodeVersion, plugin: `opencode-plugin-flow@${input.packageVersion}`, model, reviewerModel: model, - reviewerSteps: RELEASE_HOST_POLICY.reviewerSteps, - platform: RELEASE_HOST_POLICY.platform, + reviewerSteps: host.reviewerSteps, + platform: host.platform, + ...(targetsOpenCodeV2(input.packageVersion) + ? { + ambientConfig: V2_RELEASE_HOST_POLICY.ambientConfig, + transport: V2_RELEASE_HOST_POLICY.transport, + } + : {}), ...(releaseHostPermissions(input.packageVersion) ? { permission: releaseHostPermissions(input.packageVersion) } : {}), @@ -402,16 +450,22 @@ export function assertReleaseHost(input: { readonly platform: string; readonly opencodeOverride?: string | undefined; readonly reviewerModelOverride?: string | undefined; + readonly reviewerVariantOverride?: string | undefined; readonly reviewerStepsOverride?: string | undefined; }): void { - if (input.packageVersion === "9.6.0" && input.recoveryApiKeySet) + if ( + (input.packageVersion === "9.6.0" || + targetsOpenCodeV2(input.packageVersion ?? "standard")) && + input.recoveryApiKeySet + ) throw new Error( - "9.6.0 release evaluation requires recovery off; unset TYPESAFE_API_KEY.", + `${input.packageVersion} release evaluation requires recovery off; unset TYPESAFE_API_KEY.`, ); if ( input.platform !== RELEASE_HOST_POLICY.platform || input.opencodeOverride?.trim() || input.reviewerModelOverride?.trim() || + input.reviewerVariantOverride?.trim() || input.reviewerStepsOverride?.trim() ) { throw new Error( diff --git a/evals/replay.ts b/evals/replay.ts index 0b222592..4b19fa73 100644 --- a/evals/replay.ts +++ b/evals/replay.ts @@ -1,26 +1,3 @@ -// Replays a cassette against the real Flow tool handlers. -// -// No model, no OpenCode host, no network, no provider spend. The recorded -// arguments go through the same host arg schemas and the same handlers a live host -// calls, against a fresh git workspace, and the scenario's own `check` then grades -// the durable state that just came out. What a live attempt buys and this cannot is -// a *new* decision; what this buys and a live attempt cannot is running the same -// decision again for free after every change to the runtime. -// -// Three things the recording cannot hand over literally, and how each is handled: -// -// - Runtime-issued identifiers. A replayed `flow_plan_save` mints its own session -// id, `flow_review_start` its own assignment id, and a submission its own -// finding ids, so a recorded argument naming one is translated through a map -// learned as the replay goes. Untranslated strings pass through unchanged, -// which is what keeps a recorded *wrong* id a recorded wrong id. -// - The host a command ran on. Injected from the cassette rather than read from -// the replaying machine, so a Linux recording keeps its Linux verdict on a Mac. -// - Bash. Never re-executed. The recorded command, exit code, and truncation flag -// are fed through the real capture coordinator, so the arming rule, the -// command-match rule, and the eligibility rule all run for real; only the -// command itself does not. - import { spawnSync } from "node:child_process"; import { mkdir, @@ -46,7 +23,13 @@ import type { ValidationStartRequest } from "../src/application/schema.js"; import { isArtifactPath } from "../src/domain/artifact.js"; import { createFileSessionRepository } from "../src/infrastructure/fs/session-repository.js"; import { readWorkspaceTestReport } from "../src/infrastructure/fs/workspace-validation.js"; -import { tool } from "../src/platform/opencode/sdk.js"; +import { + Agent, + CallID, + type NativeToolAfter, + Session, + SessionMessage, +} from "../src/platform/opencode/sdk.js"; import { createTools } from "../src/platform/opencode/tools.js"; import { ValidationCaptureCoordinator } from "../src/platform/opencode/validation-capture.js"; import { @@ -63,34 +46,20 @@ export type ReplayedCall = ObservedToolCall & export type ReplayResult = Readonly<{ cassette: Cassette; + evidenceKind: "historical-decision-replay"; outcome: Outcome; /** Divergences between the recorded and replayed Flow responses. */ divergences: readonly string[]; }>; -/** - * One tool as the host sees it: a raw Zod shape for its arguments, and an execute - * that takes the parsed object. Erased to `unknown` here because the replay looks a - * tool up by a recorded name, which no static type can narrow. - */ -type HostTool = Readonly<{ - args: Record; - execute: ( - args: unknown, - context: ReturnType, - ) => Promise; -}>; - function toolContext(workspace: string, sessionIndex: number, agent: string) { return { - sessionID: `replay-${sessionIndex}`, - messageID: `replay-message-${sessionIndex}`, + sessionID: `ses_replay-${sessionIndex}`, + messageID: `msg_replay-${sessionIndex}`, + callID: `replay-call-${sessionIndex}`, agent, directory: workspace, - worktree: workspace, abort: new AbortController().signal, - metadata: () => {}, - ask: async () => {}, }; } @@ -125,7 +94,20 @@ async function seedWorkspace( ["add", "-A"], ["commit", "-m", "fixture"], ]) { - const git = spawnSync("git", argv, { cwd: workspace, encoding: "utf8" }); + const git = spawnSync( + "git", + ["-c", "core.hooksPath=/dev/null", "-c", "commit.gpgSign=false", ...argv], + { + cwd: workspace, + encoding: "utf8", + env: { + PATH: process.env.PATH, + HOME: workspace, + GIT_CONFIG_NOSYSTEM: "1", + GIT_CONFIG_GLOBAL: "/dev/null", + }, + }, + ); if (git.status !== 0) { throw new Error(`git ${argv[0]} failed:\n${git.stderr}`); } @@ -288,53 +270,63 @@ function statusOf(output: unknown): "ok" | "error" { return asRecord(output)?.status === "ok" ? "ok" : "error"; } -/** - * Runs one recorded Bash call through the capture coordinator without executing - * it. - * - * The coordinator is production's, unmodified: it still refuses a command that does - * not match the armed one and still records an ineligible observation when the host - * reported no exit code. Only the subprocess is absent. - */ +export function recordedBashDecisionFixture( + event: Extract, +): NativeToolAfter & { status: "completed" } { + const { exit, truncated } = event.metadata; + return { + tool: "shell", + sessionID: Session.ID.make(`ses_replay-${event.sessionIndex}`), + messageID: SessionMessage.ID.make(`msg_replay-${event.sessionIndex}`), + id: CallID.make( + `replay-call-${event.sessionIndex}-${event.command.length}`, + ), + agent: Agent.ID.make(event.agent), + input: { command: event.command }, + status: "completed", + result: { + content: event.output, + output: { + ...(typeof exit === "number" && Number.isSafeInteger(exit) + ? { exit } + : {}), + ...(typeof truncated === "boolean" ? { truncated } : {}), + output: event.output, + }, + }, + }; +} + async function replayBash( validation: ValidationCaptureCoordinator, event: Extract, workspace: string, ): Promise { - const context = toolContext(workspace, event.sessionIndex, event.agent); - const callID = `replay-call-${event.sessionIndex}-${event.command.length}`; - const before = { tool: "bash", sessionID: context.sessionID, callID }; + const fixture = recordedBashDecisionFixture(event); try { - await validation.observeToolBefore(before, { - args: { command: event.command }, - }); + await validation.observeToolBefore(fixture); } catch (error) { return `[flow-validation-error] ${error instanceof Error ? error.message : String(error)}`; } - const output = { - title: "bash", - output: event.output, - metadata: event.metadata, - }; try { if (event.resultsPath && event.testReport) { - if (!isArtifactPath(event.resultsPath)) { + if (!isArtifactPath(event.resultsPath)) throw new Error("Cassette results path is unsafe."); - } const target = join(workspace, event.resultsPath); await mkdir(dirname(target), { recursive: true }); await Bun.sleep(2); await writeFile(target, event.testReport, "utf8"); await Bun.sleep(2); } - await validation.observeToolAfter( - { ...before, args: { command: event.command } }, - output, - ); + await validation.observeToolAfter(fixture); } catch (error) { - output.output = `${output.output}\n\n[flow-validation-error] ${error instanceof Error ? error.message : String(error)}`; + return `${event.output}\n\n[flow-validation-error] ${error instanceof Error ? error.message : String(error)}`; } - return output.output; + return typeof fixture.result.content === "string" + ? fixture.result.content + : (fixture.result.content ?? []) + .flatMap((part) => (part.type === "text" ? [part.text] : [])) + .join("\n"); } export async function replayCassette( @@ -343,7 +335,13 @@ export async function replayCassette( const started = Date.now(); const workspace = await seedWorkspace(cassette.files); try { + const replayAgents = new Map(); const validation = new ValidationCaptureCoordinator({ + readSession: async (sessionID) => ({ + id: sessionID, + agent: replayAgents.get(sessionID) ?? "build", + location: { directory: workspace }, + }), readReport: readWorkspaceTestReport, persistObservation: (root, observation) => persistObservedValidation( @@ -359,12 +357,13 @@ export async function replayCassette( request, cassette.hostPlatform, ), - }) as unknown as Record; + }); const rebinding = new Rebinding(); const calls: ReplayedCall[] = []; const divergences: string[] = []; for (const [index, event] of cassette.events.entries()) { + replayAgents.set(`ses_replay-${event.sessionIndex}`, event.agent); if (event.kind === "bash") { const output = await replayBash(validation, event, workspace); rebinding.learn(event, output); @@ -414,12 +413,8 @@ export async function replayCassette( const input = rebinding.apply(bindWorkspace(event.input, workspace)); let raw: string; try { - // The host validates arguments against the schema mirror before calling, - // so replay does too: a mirror that drifted from the persisted schema - // fails here rather than silently accepting an argument no host would. - const args = parseArgs(definition, input); - const result = await definition.execute(args, context); - raw = typeof result === "string" ? result : result.output; + const result = await definition.execute(input, context); + raw = typeof result === "string" ? result : JSON.stringify(result); } catch (error) { raw = JSON.stringify({ status: "error", @@ -480,29 +475,13 @@ export async function replayCassette( durationMs: Date.now() - started, providerError: null, }; - return { cassette, outcome, divergences }; + return { + cassette, + outcome, + divergences, + evidenceKind: "historical-decision-replay", + }; } finally { await rm(workspace, { recursive: true, force: true }); } } - -/** - * Validates one recorded argument object against the tool's own host schema. - * - * `tool.schema` is Zod, and `args` is the raw shape a host wraps in an object, so - * this is the same parse the host performs — which makes the schema mirror part of - * what the replay tier covers rather than something only a unit test sees. - * - * Strict, because Zod 4 strips unknown keys by default: a recorded call carrying a - * field the schema has since dropped would have parsed clean and replayed green, - * which is the one thing this parse exists to catch. - */ -function parseArgs( - definition: HostTool, - input: Record, -): unknown { - return tool.schema - .object(definition.args as Parameters[0]) - .strict() - .parse(input); -} diff --git a/evals/reviewer-access.ts b/evals/reviewer-access.ts index e298bc42..f9ac7b06 100644 --- a/evals/reviewer-access.ts +++ b/evals/reviewer-access.ts @@ -5,7 +5,11 @@ import { type ScenarioGradeInput, } from "./grader-input.js"; import type { ObservedToolCall } from "./harness.js"; -import { type HostTrace, HostTraceSchema } from "./host-trace.js"; +import { + type HostTrace, + HostTraceSchema, + nativeToolKey, +} from "./host-trace.js"; import { decodeReviewerPacket } from "./reviewer-packet-bytes.js"; const Id = z.string().min(1).max(256); @@ -481,7 +485,9 @@ export function nativeToolWitness( if (message?.role !== "assistant" || message.agent !== call.agent) return null; const tool = message.tools.find( - (candidate) => candidate.partId === call.native?.partId, + (candidate) => + call.native !== undefined && + nativeToolKey(candidate) === nativeToolKey(call.native), ); if (!tool) return null; const { tool: name, status, ...native } = tool; @@ -534,6 +540,54 @@ export function checkAutonomousLineage( const trace = parsed.data; if (trace.runnerRootSessionIds.length !== 1) return ["Autonomous execution requires one native root session."]; + if (trace.version === 2) { + const issues: string[] = []; + const root = trace.runnerRootSessionIds[0]; + const rootMessages = trace.messages.filter( + (message) => message.sessionId === root, + ); + const users = rootMessages.filter((message) => message.role === "user"); + const first = users[0]; + if ( + first?.role !== "user" || + first.type !== "user" || + first.textBytes === 0 || + first.flowTokenSha256 === null + ) + issues.push("The initial original Flow request marker is missing."); + const token = first?.role === "user" ? first.flowTokenSha256 : null; + if ( + users.filter((user) => user.type === "user" && user.textBytes > 0) + .length !== 1 + ) + issues.push("Expected exactly one original root request."); + for (const user of users) { + if (user.attachments > 0 || user.compactionContinue === true) + issues.push( + "Native compaction or unknown user-part delivery is unproven.", + ); + if ( + user.type === "synthetic" && + (user.flowTokenSha256 === null || user.flowTokenSha256 !== token) + ) + issues.push("Unknown or mismatched synthetic Flow continuation."); + if (user.type === "user" && user.textBytes === 0) + issues.push("Unclassified native root user message."); + } + if ( + rootMessages.some( + (message) => + message.role === "event" && + message.type !== "idle" && + message.type !== "agent-switched" && + message.type !== "model-switched", + ) + ) + issues.push( + "Native compaction or unknown user-part delivery is unproven.", + ); + return [...new Set(issues)]; + } const users = trace.messages.filter( (message) => message.role === "user" && diff --git a/evals/reviewer-run.ts b/evals/reviewer-run.ts index f084d704..d0a88845 100644 --- a/evals/reviewer-run.ts +++ b/evals/reviewer-run.ts @@ -28,6 +28,7 @@ import { packPlugin, preparePackageCache, } from "./harness.js"; +import { OPENCODE_HOST_VERSION } from "./opencode-host.js"; import { evaluatorIdentity, hostActorObservation, @@ -247,7 +248,7 @@ async function main(): Promise { const cases = REVIEWER_CASES.slice(0, options.limit); const model = requestedModel(options.model); const repositoryRoot = join(import.meta.dir, ".."); - const opencodeVersion = packageJson.devDependencies["@opencode-ai/plugin"]; + const opencodeVersion = OPENCODE_HOST_VERSION; const packDir = await mkdtemp(join(tmpdir(), "flow-reviewer-pack-")); const reportDir = join(repositoryRoot, "evals", "results"); await persistEvaluation("report-directory", () => @@ -294,6 +295,8 @@ async function main(): Promise { toolchain, packageCache, opencodeVersion, + ambientConfig: "disabled", + reviewerEnvironment: "disabled", files: entry.files, reviewer: { model: options.model }, }); diff --git a/evals/run.ts b/evals/run.ts index 1fbb4e4a..751b0265 100644 --- a/evals/run.ts +++ b/evals/run.ts @@ -87,6 +87,9 @@ import { type ReviewerActivity, reviewerActivity, } from "./metrics.js"; +import type { NativeAuthentication } from "./native-auth/host.js"; +import { nativeAuthenticationFromEnvironment } from "./native-auth-config.js"; +import { OPENCODE_HOST_VERSION } from "./opencode-host.js"; import { evaluatorIdentity, hostActorObservation, @@ -103,13 +106,13 @@ import { assertReleaseModels, assertReleaseScenarioOrder, RELEASE_ANALYSIS_SHA256, - RELEASE_HOST_POLICY, releaseAttemptsFor, releaseCatalog, releaseCellsFor, releaseGraderBundle, releaseHostConfigSha256, releaseHostPermissions, + releaseHostPolicy, releaseRandomizationSeed, releaseReviewerModel, releaseScenarioCatalog, @@ -289,7 +292,10 @@ export function evalReleaseReviewerConfiguration( managerModel: string, packageVersion: string, ) { - const pinned = releaseReviewerModel(packageVersion); + const pinned = releaseReviewerModel( + packageVersion, + legacyRequestedModel(managerModel), + ); return evalReviewerConfiguration( managerModel, pinned @@ -811,6 +817,7 @@ async function preflight( models: readonly string[], toolchain: BunToolchain, signal: AbortSignal, + nativeAuthentication?: NativeAuthentication, ): Promise { signal.throwIfAborted(); process.stdout.write("- preflight: resolving model ids ... "); @@ -821,6 +828,9 @@ async function preflight( toolchain, packageCache, opencodeVersion, + ambientConfig: "disabled", + reviewerEnvironment: "disabled", + ...(nativeAuthentication ? { nativeAuthentication } : {}), files: { "package.json": '{\n "name": "preflight"\n}\n' }, signal, }); @@ -852,6 +862,7 @@ async function preflight( } } catch (error) { if (error instanceof CampaignCancelled || signal.aborted) throw error; + if (nativeAuthentication) throw error; // Losing the probe host is not evidence about the models, so it must not // block a run that may well work. An actual rejection above is different, // and is fatal. @@ -902,14 +913,31 @@ export async function runCampaign( platform: normalizeEvidencePlatform(process.platform), opencodeOverride: process.env.FLOW_OPENCODE_SMOKE_VERSION, reviewerModelOverride: process.env.OPENCODE_FLOW_REVIEWER_MODEL, + reviewerVariantOverride: process.env.OPENCODE_FLOW_REVIEWER_VARIANT, reviewerStepsOverride: process.env.OPENCODE_FLOW_REVIEWER_STEPS, }); } const opencodeVersion = sampling.kind === "release" - ? RELEASE_HOST_POLICY.opencodeVersion + ? releaseHostPolicy(packageJson.version).opencodeVersion : process.env.FLOW_OPENCODE_SMOKE_VERSION?.trim() || - packageJson.devDependencies["@opencode-ai/plugin"]; + OPENCODE_HOST_VERSION; + const authenticationModels = [ + ...new Set( + models.flatMap((model) => { + const reviewer = ( + sampling.kind === "release" + ? evalReleaseReviewerConfiguration(model, packageJson.version) + : evalReviewerConfiguration(model) + ).pluginOptions?.model; + return reviewer ? [model, reviewer] : [model]; + }), + ), + ]; + const nativeAuthentication = + await nativeAuthenticationFromEnvironment(authenticationModels); + if (nativeAuthentication && concurrency !== 1) + throw new Error("Native account authentication requires --concurrency 1."); const footprint = promptFootprint(); console.log(`Flow ${packageJson.version} on OpenCode ${opencodeVersion}`); @@ -1009,6 +1037,7 @@ export async function runCampaign( [...new Set([...models, ...(reviewerModel ? [reviewerModel] : [])])], toolchain, signal, + nativeAuthentication, ); const persistV2Attempt = async ( result: RunResult, @@ -1105,6 +1134,13 @@ export async function runCampaign( toolchain, packageCache, opencodeVersion, + ...(nativeAuthentication ? { nativeAuthentication } : {}), + ...(sampling.kind === "release" + ? { + ambientConfig: "disabled" as const, + reviewerEnvironment: "disabled" as const, + } + : {}), files: scenario.files, ...(scenario.id === "inspection-failed-audit-completes" ? { retainReviewDocument: true } diff --git a/evals/study-manifest.ts b/evals/study-manifest.ts index f1279a79..5d028dd8 100644 --- a/evals/study-manifest.ts +++ b/evals/study-manifest.ts @@ -18,8 +18,8 @@ import { } from "./report-identities.js"; import { assertStudyBudget, + OPENCODE2_STUDY_PROTOCOL_VERSION_SHA256, PairedStudyPolicySchema, - STUDY_PROTOCOL_VERSION_SHA256, StudyArmSchema, StudyBudgetSchema, } from "./study-protocol.js"; @@ -79,6 +79,13 @@ export type StudyManifest = z.infer; export async function prepareStudyManifest(input: unknown, directory: string) { const manifest = StudyManifestSchema.parse(input); + if ( + manifest.opencodeVersion !== "2.1.4" || + manifest.accounting.hostContract !== "opencode-2.1.4-disjoint-output-v1" + ) + throw new Error( + "Active studies require the verified OpenCode 2.1.4 accounting contract.", + ); const readArm = async (arm: StudyManifest["arms"]["baseline"]) => { if ("kind" in arm.artifact) { return { specification: StudyArmSchema.parse(arm), bytes: null }; @@ -140,7 +147,7 @@ export async function prepareStudyManifest(input: unknown, directory: string) { ? {} : { minimumDetectableEffect: manifest.minimumDetectableEffect }), accounting: manifest.accounting, - versionSha256: STUDY_PROTOCOL_VERSION_SHA256, + versionSha256: OPENCODE2_STUDY_PROTOCOL_VERSION_SHA256, }); const caseIds = new Set( manifest.cases.map((item) => `${item.caseId}\u0000${item.caseVersion}`), diff --git a/evals/study-protocol.ts b/evals/study-protocol.ts index 10999806..fdcbd4a2 100644 --- a/evals/study-protocol.ts +++ b/evals/study-protocol.ts @@ -80,6 +80,15 @@ export const STUDY_PROTOCOL_VERSION_SHA256 = canonicalSha256( }, ); +export const OPENCODE2_STUDY_PROTOCOL_VERSION_SHA256 = canonicalSha256( + "flow-paired-study-protocol-v3", + { + previous: STUDY_PROTOCOL_VERSION_SHA256, + host: "opencode-2.1.4", + accounting: "native-disjoint-input-output-reasoning-cache-v1", + }, +); + export const PairedStudyPolicySchema = z .object({ kind: z.literal("paired-study"), @@ -107,7 +116,10 @@ export const PairedStudyPolicySchema = z accounting: z .object({ schemaVersion: z.literal(1), - hostContract: z.literal("opencode-1.18.6-disjoint-output-v1"), + hostContract: z.enum([ + "opencode-1.18.6-disjoint-output-v1", + "opencode-2.1.4-disjoint-output-v1", + ]), maxGeneratedOutputTokens: Positive, estimate: z .object({ @@ -146,13 +158,17 @@ export const PairedStudyPolicySchema = z message: "Arm differences exceed the declared comparison; pin other settings or declare total-effect.", }); - if (policy.opencodeVersion !== "1.18.6") { + const native = + policy.accounting.hostContract === "opencode-2.1.4-disjoint-output-v1"; + if (policy.opencodeVersion !== (native ? "2.1.4" : "1.18.6")) { context.addIssue({ code: "custom", path: ["opencodeVersion"], - message: "Versioned accounting is verified only for OpenCode 1.18.6.", + message: + "Host version does not match its verified accounting contract.", }); } + const methods = { smoke: "descriptive", exploratory: "equal-task-cluster-bootstrap-v1", @@ -180,8 +196,10 @@ export const PairedStudyPolicySchema = z }); } if ( - policy.versionSha256 !== STUDY_PROTOCOL_VERSION_SHA256 && - policy.versionSha256 !== LEGACY_STUDY_PROTOCOL_VERSION_SHA256 + native + ? policy.versionSha256 !== OPENCODE2_STUDY_PROTOCOL_VERSION_SHA256 + : policy.versionSha256 !== STUDY_PROTOCOL_VERSION_SHA256 && + policy.versionSha256 !== LEGACY_STUDY_PROTOCOL_VERSION_SHA256 ) { context.addIssue({ code: "custom", @@ -209,7 +227,9 @@ export function studyHostConfig(policy: PairedStudyPolicy, arm: StudyArm) { artifact: arm.artifact, reviewer: arm.reviewer, reviewerEnvironment: "disabled", - nativeLlm: false, + ...(policy.opencodeVersion === "1.18.6" + ? { nativeLlm: false } + : { transport: "http" }), ambientConfig: "disabled", syntheticGit: "isolated-project-identity-no-hooks-or-signing", }; diff --git a/evals/study-runner.ts b/evals/study-runner.ts index 701f0cbc..265c6704 100644 --- a/evals/study-runner.ts +++ b/evals/study-runner.ts @@ -292,7 +292,6 @@ export async function executeStudy( toolchain, packageCache: "", opencodeVersion: policy.opencodeVersion, - nativeLlm: false, ambientConfig: "disabled", withFlow: false, reviewerEnvironment: "disabled", @@ -477,7 +476,6 @@ export async function executeStudy( opencodeVersion: policy.opencodeVersion, files: benchmark.files, withFlow: flow, - nativeLlm: false, ambientConfig: "disabled", reviewerEnvironment: "disabled", ...(reviewer diff --git a/package.json b/package.json index 61c1fcb2..d7be9827 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "opencode-plugin-flow", - "version": "9.6.0", - "description": "Small durable planning, validation, and review workflow for OpenCode", + "version": "10.0.0", + "description": "Durable planning, validation, and independent review for OpenCode v2", "type": "module", "repository": { "type": "git", @@ -35,8 +35,8 @@ ], "scripts": { "build": "bun run clean && bun run build:plugin && bun run build:tui && bun run build:types", - "build:plugin": "bun build --target=node --outdir=./dist --entry-naming=index.js --external=@opencode-ai/plugin --external=zod --sourcemap=external ./src/index.ts", - "build:tui": "bun build --target=bun --outdir=./dist --entry-naming=tui.js --sourcemap=external ./src/tui.ts", + "build:plugin": "bun build --target=node --outdir=./dist --entry-naming=index.js --external=@opencode/plugin --external=zod --external=@opencode/schema --external=@opencode/client --external=effect --sourcemap=external ./src/index.ts", + "build:tui": "bun build --target=bun --outdir=./dist --entry-naming=tui.js --external=@opencode/plugin --external=zod --external=@opencode/schema --external=@opencode/client --external=effect --sourcemap=external ./src/tui.ts", "build:types": "tsc -p tsconfig.types.json && bun run scripts/prune-dist-declarations.ts", "clean": "bun run scripts/clean-dist.ts", "eval": "bun run evals/run.ts", @@ -73,18 +73,21 @@ "node": ">=24" }, "peerDependencies": { - "@opencode-ai/plugin": ">=1.18.3 <2" + "@opencode/plugin": ">=2.1.4 <3", + "@opencode/schema": ">=2.1.4 <3" }, "dependencies": { "saxes": "6.0.0", - "zod": "4.4.3" + "zod": "4.6.7" }, "devDependencies": { "@biomejs/biome": "2.5.14", - "@opencode-ai/plugin": "1.18.31", "@types/bun": "1.4.2", "@types/node": "26.6.2", - "typescript": "7.0.2" + "typescript": "7.0.2", + "@opencode/plugin": "2.1.4", + "@opencode/client": "2.1.4", + "@opencode/schema": "2.1.4" }, "overrides": { "@types/node": "26.6.2" diff --git a/scripts/canary-run.ts b/scripts/canary-run.ts index 9ed9ab1f..4189e10b 100644 --- a/scripts/canary-run.ts +++ b/scripts/canary-run.ts @@ -1,38 +1,117 @@ -import { spawn } from "node:child_process"; +import { type ChildProcess, execFile, spawn } from "node:child_process"; import { createHash } from "node:crypto"; -import { readFile } from "node:fs/promises"; +import { + mkdir, + mkdtemp, + readFile, + realpath, + rm, + writeFile, +} from "node:fs/promises"; import { dirname, join, resolve } from "node:path"; +import { promisify } from "node:util"; +import type { OpenCodeClient } from "@opencode/client"; +import { canonicalJson } from "../evals/canonical-json.js"; +import { + mapStrings, + normalizeRecorded, + scrubSecrets, +} from "../evals/cassette.js"; import { terminateChildProcessTree } from "../evals/harness.js"; -import { parsePreparedCanary } from "./eval-canary.js"; +import { + connectNativeAccountSource, + type NativeAuthHost, +} from "../evals/native-auth/host.js"; +import type { NativeAccountConnection } from "../evals/native-auth/protocol.js"; +import { readNativeAccountConnection } from "../evals/native-auth-config.js"; +import { + nativeModel, + OPENCODE_HOST_VERSION, + OpenCodeStartup, + openCodeInvocation, +} from "../evals/opencode-host.js"; +import { + CANARY_PLUGIN_DIRECTORY, + CanaryAuthLaunchReceiptSchema, + canaryAuthLaunchMatches, + canaryProviderConfiguration, + measureCanaryInstallation, + type PreparedCanary, + parsePreparedCanary, +} from "./eval-canary.js"; +import { collectNativeCanary } from "./export-canary-native.js"; import { consumePaidDispatch } from "./paid-budget.js"; +type NativeCanaryPhase = + | "prepare" + | "dependencies" + | "startup" + | "plugins" + | "credentials" + | "execution" + | "export" + | "cleanup"; +class NativeCanaryError extends Error { + constructor(phase: NativeCanaryPhase) { + super(`Native-auth canary execution failed during ${phase}.`); + } +} + export async function runPaidCanary( - input: { prepared: string; model: string; prompt: string }, + input: { + prepared: string; + model: string; + prompt: string; + nativeAuthSource?: string; + }, launch: ( directory: string, model: string, prompt: string, + prepared: PreparedCanary, + nativeAuth?: NativeAccountConnection, ) => Promise = launchCanary, ): Promise { - const directory = dirname(resolve(input.prepared)); + const directory = await realpath(dirname(resolve(input.prepared))); const prepared = parsePreparedCanary( JSON.parse(await readFile(input.prepared, "utf8")), ); - const digest = async (path: string) => - `sha256:${createHash("sha256") - .update(await readFile(path)) - .digest("hex")}`; + if (prepared.schemaVersion !== 2) + throw new Error( + "Active canary launch requires a native OpenCode v2 preparation.", + ); if ( - (await digest(join(directory, prepared.artifactFile))) !== - prepared.artifact.tarballSha256 || - (await digest(join(directory, "fixture/.opencode/plugins/flow.js"))) !== - prepared.pluginEntrySha256 + process.env.FLOW_EVAL_OPENCODE_VERSION && + process.env.FLOW_EVAL_OPENCODE_VERSION !== prepared.runtime.hostVersion ) - throw new Error("Prepared canary bytes changed."); + throw new Error("Unapproved canary host override."); + await measureCanaryInstallation(prepared, directory); + if (Boolean(prepared.runtime.nativeAuth) !== Boolean(input.nativeAuthSource)) + throw new Error( + "Native auth source and prepared attachment must be selected together.", + ); + const nativeAuth = input.nativeAuthSource + ? await readNativeAccountConnection(input.nativeAuthSource) + : undefined; + nativeModel(input.model, "default"); const prompt = await readFile(input.prompt, "utf8"); if (!prompt.trim()) throw new Error("Canary prompt is empty."); await consumePaidDispatch({ model: input.model, kind: "canary" }); - return launch(join(directory, "fixture"), input.model, prompt); + try { + return await launch( + join(directory, "fixture"), + input.model, + prompt, + prepared, + nativeAuth, + ); + } catch (error) { + if (nativeAuth) + throw error instanceof NativeCanaryError + ? error + : new NativeCanaryError("execution"); + throw error; + } } export function canaryLaunchEnvironment( directory: string, @@ -41,65 +120,517 @@ export function canaryLaunchEnvironment( const environment: NodeJS.ProcessEnv = { ...inherited, PWD: directory }; delete environment.FLOW_EVAL_AUTHORIZATION; delete environment.TYPESAFE_API_KEY; + delete environment.FLOW_EVAL_NATIVE_AUTH; + for (const name of Object.keys(environment)) + if (name.startsWith("OPENCODE_") && name !== "OPENCODE_API_KEY") + delete environment[name]; + for (const name of [ + "NODE_OPTIONS", + "NODE_PATH", + "BUN_OPTIONS", + "BUN_PRELOAD", + ]) + delete environment[name]; return environment; } +export { canaryProviderConfiguration } from "./eval-canary.js"; + +export function canaryPublicNativeEvidence( + value: unknown, + privateValues: readonly string[], +): unknown { + const values = [...new Set(privateValues.filter(Boolean))].sort( + (left, right) => right.length - left.length, + ); + return mapStrings(value, (text) => { + let redacted = text; + for (const secret of values) + redacted = redacted.split(secret).join(""); + return scrubSecrets(redacted); + }); +} +function privateStrings(value: unknown): string[] { + if (typeof value === "string") return [value]; + if (Array.isArray(value)) return value.flatMap(privateStrings); + if (value && typeof value === "object") + return Object.entries(value).flatMap(([key, item]) => [ + ...(key.length > 16 ? [key] : []), + ...privateStrings(item), + ]); + return []; +} async function launchCanary( directory: string, model: string, prompt: string, + prepared: PreparedCanary, + nativeAuth?: NativeAccountConnection, ): Promise { - return new Promise((resolve, reject) => { - const environment = canaryLaunchEnvironment(directory); - const child = spawn( - "opencode", - ["run", "--model", model, "--format", "json", "--", prompt], + const environment = canaryLaunchEnvironment(directory); + environment.OPENCODE_FLOW_REVIEWER_MODEL = model; + const requestedModel = nativeModel(model, "default"); + const outputDirectory = await mkdtemp(join(dirname(directory), "run-")); + const privateRoot = nativeAuth + ? join(outputDirectory, "private-host") + : outputDirectory; + const source = nativeAuth + ? connectNativeAccountSource(nativeAuth) + : undefined; + let attachment: NativeAuthHost | undefined; + let server: ChildProcess | undefined; + let client: OpenCodeClient | undefined; + let seeded = false; + let outcomeCode = 1; + let publicReceipt: unknown; + let failure: unknown; + let cleanupFailed = false; + let phase: NativeCanaryPhase = "prepare"; + const startup = new OpenCodeStartup(); + const cancellation = new AbortController(); + let cancelledCode: number | undefined; + const stop = (code: number) => { + cancelledCode ??= code; + cancellation.abort(new Error("Canary interrupted.")); + }; + const interrupt = () => stop(130); + const terminate = () => stop(143); + process.on("SIGINT", interrupt); + process.on("SIGTERM", terminate); + const signal = AbortSignal.any([ + cancellation.signal, + AbortSignal.timeout(30 * 60 * 1000), + ]); + try { + if (nativeAuth) await mkdir(privateRoot, { mode: 0o700 }); + for (const [key, leaf] of Object.entries({ + HOME: "home", + XDG_CONFIG_HOME: "config", + XDG_DATA_HOME: "data", + XDG_CACHE_HOME: "cache", + XDG_STATE_HOME: "state", + TMPDIR: "tmp", + })) { + const path = join(privateRoot, leaf); + await mkdir(path, { mode: 0o700 }); + environment[key] = path; + } + const auth = + prepared.schemaVersion === 2 ? prepared.runtime.nativeAuth : undefined; + if (source && auth) { + attachment = await source.prepareHost({ + directory: join(outputDirectory, "native-auth"), + configurationPath: join( + outputDirectory, + "native-auth/configuration.json", + ), + stagedClient: { + ...auth, + pluginDirectory: resolve(dirname(directory), auth.pluginDirectory), + }, + models: [model], + signal, + }); + if ( + attachment.plugin.package !== + resolve(dirname(directory), auth.pluginDirectory) || + attachment.plugin.options.configurationPath !== + join(outputDirectory, "native-auth/configuration.json") || + canonicalJson(attachment.artifactHashes) !== + canonicalJson({ + entrySha256: auth.entrySha256, + sourceSha256: auth.sourceSha256, + }) + ) + throw new Error("Prepared native auth attachment changed."); + } + const providerConfigPath = join( + privateRoot, + "config/opencode/opencode.json", + ); + await mkdir(dirname(providerConfigPath), { recursive: true, mode: 0o700 }); + const providerPolicy = canaryProviderConfiguration(requestedModel); + const providerConfigBytes = canonicalJson({ + ...providerPolicy, + ...(attachment ? { plugins: [attachment.plugin], warming: false } : {}), + }); + await writeFile(providerConfigPath, providerConfigBytes, { + flag: "wx", + mode: 0o600, + }); + phase = "dependencies"; + await promisify(execFile)( + "npm", + [ + "install", + "--prefix", + join(dirname(directory), CANARY_PLUGIN_DIRECTORY), + "--omit=dev", + "--ignore-scripts", + "--no-audit", + "--no-fund", + "--legacy-peer-deps", + "--no-package-lock", + ], + { cwd: directory, env: environment }, + ); + await measureCanaryInstallation(prepared, dirname(directory)); + phase = "startup"; + const versionInvocation = openCodeInvocation( + ["--version"], + OPENCODE_HOST_VERSION, + ); + const version = await promisify(execFile)( + versionInvocation.command, + versionInvocation.args, + { cwd: directory, env: environment }, + ); + if (version.stdout.trim() !== `opencode v${OPENCODE_HOST_VERSION}`) + throw new Error( + "Canary executable did not report the pinned host version.", + ); + const invocation = openCodeInvocation( + ["serve", "--hostname", "127.0.0.1", "--port", "0"], + OPENCODE_HOST_VERSION, + ); + const child = spawn(invocation.command, invocation.args, { + cwd: directory, + env: environment, + stdio: ["ignore", "pipe", "pipe"], + detached: process.platform !== "win32", + }); + server = child; + child.stdout?.on("data", (chunk) => + startup.consume(String(chunk), "stdout"), + ); + child.stderr?.on("data", (chunk) => + startup.consume(String(chunk), "stderr"), + ); + let spawnError: Error | undefined; + child.once("error", (error) => { + spawnError = error; + }); + const deadline = Date.now() + 180000; + const connected = async () => { + for (;;) { + signal.throwIfAborted(); + if (spawnError) throw spawnError; + if (child.exitCode !== null || child.signalCode !== null) + throw new Error("Canary server exited before readiness."); + const address = + /^server listening on (http:\/\/127\.0\.0\.1:\d+)\s*$/m.exec( + startup.log, + )?.[1]; + const candidate = address ? startup.client(address, directory) : null; + if (candidate) { + const host = await candidate.server.info({ + signal: AbortSignal.any([signal, AbortSignal.timeout(3000)]), + }); + if (host.version !== OPENCODE_HOST_VERSION) + throw new Error( + "Canary server did not report the pinned host version.", + ); + return { client: candidate, host }; + } + if (Date.now() >= deadline) + throw new Error( + "Canary server did not publish authenticated readiness.", + ); + await Bun.sleep(25); + } + }; + const connectedHost = await connected(); + const nativeClient = connectedHost.client; + client = nativeClient; + const host = connectedHost.host; + if (attachment) { + phase = "plugins"; + const ready = await client.session.create( + { title: "Canary plugin readiness", location: { directory } }, + { signal }, + ); + await client.session.remove({ sessionID: ready.id }, { signal }); + await client.config.get({ location: { directory } }, { signal }); + const expected = new Set([ + await realpath(join(dirname(directory), CANARY_PLUGIN_DIRECTORY)), + attachment.plugin.package, + ]); + for (;;) { + signal.throwIfAborted(); + if (Date.now() >= deadline) + throw new Error("Canary plugins did not become active."); + const plugins = await client.plugin.list( + { location: { directory } }, + { signal }, + ); + const active = new Set(); + for (const plugin of plugins.data) { + if ( + plugin.source.type === "builtin" && + plugin.state.status === "active" + ) + continue; + if (plugin.source.type !== "local") + throw new Error("Unprepared canary plugin loaded."); + const path = await realpath(plugin.source.path); + const base = expected.has(path) + ? path + : /[/\\]index\.(?:ts|js)$/.test(path) + ? dirname(path) + : path; + if (!expected.has(base) || plugin.state.status !== "active") + throw new Error("Unprepared or failed canary plugin loaded."); + active.add(base); + } + if ([...expected].every((path) => active.has(path))) break; + await Bun.sleep(20); + } + phase = "credentials"; + seeded = true; + for (const entry of attachment.credentialHandoff.entries) + await client.credential.create( + { + id: entry.id, + integrationID: entry.integrationID, + label: entry.label, + value: entry.value, + activate: entry.active, + }, + { signal }, + ); + } + phase = "execution"; + const manager = await client.session.create( { - cwd: directory, - env: environment, - stdio: "inherit", - detached: process.platform !== "win32", + title: "Flow exact-artifact canary", + agent: "build", + model: requestedModel, + location: { directory }, }, + { signal }, ); - let stopping: Promise | undefined; - let cancelledCode: number | undefined; - const stop = (code: number) => { - cancelledCode ??= code; - stopping ??= terminateChildProcessTree(child); - void stopping.catch(reject); - }; - const interrupt = () => stop(130); - const terminate = () => stop(143); - process.on("SIGINT", interrupt); - process.on("SIGTERM", terminate); - const cleanup = () => { - process.off("SIGINT", interrupt); - process.off("SIGTERM", terminate); + if ( + manager.parentID !== undefined || + (await realpath(manager.location.directory)) !== directory || + canonicalJson(manager.model) !== canonicalJson(requestedModel) + ) + throw new Error( + "Canary manager identity does not match the approved launch.", + ); + const assertProviderHTTP = async () => { + const provider = await nativeClient.provider.get( + { providerID: requestedModel.providerID, location: { directory } }, + { signal }, + ); + const inventory = await nativeClient.model.list( + { location: { directory } }, + { signal }, + ); + const selected = inventory.data.find( + (model) => + model.providerID === requestedModel.providerID && + model.id === requestedModel.id, + ); + const variant = selected?.variants.find( + (variant) => variant.id === requestedModel.variant, + ); + if ( + !selected || + (selected.settings?.transport !== undefined && + selected.settings.transport !== "http") || + variant?.settings?.transport !== "http" || + provider.data.id !== requestedModel.providerID || + provider.data.settings?.transport !== "http" || + (await realpath(provider.location.directory)) !== directory + ) + throw new Error( + `Canary provider did not retain the sealed HTTP transport policy: ${canonicalJson({ provider: provider.data.settings?.transport ?? null, model: selected?.settings?.transport ?? null, variant: variant?.settings?.transport ?? null, variantIds: selected?.variants.map((variant) => variant.id) ?? [] })}`, + ); }; - child.once("error", (error) => { - cleanup(); - reject(error); - }); - child.once("close", (code, signal) => { - cleanup(); - void (stopping ?? Promise.resolve()).then( - () => - resolve(cancelledCode ?? code ?? (signal === "SIGINT" ? 130 : 143)), - reject, + + await client.agent.list({ location: { directory } }, { signal }); + await client.model.list({ location: { directory } }, { signal }); + await assertProviderHTTP(); + await client.session.prompt( + { sessionID: manager.id, text: prompt }, + { signal }, + ); + await client.session.wait({ sessionID: manager.id }, { signal }); + await assertProviderHTTP(); + if ((await readFile(providerConfigPath, "utf8")) !== providerConfigBytes) + throw new Error( + "Canary provider configuration changed during execution.", ); + phase = "export"; + const evidence = await collectNativeCanary(client, { + directory, + rootSessionID: manager.id, + signal, }); - }); + if (evidence.host.pid !== host.pid) + throw new Error("Canary server changed before export."); + const normalized = normalizeRecorded(evidence, directory); + let publicEvidence: unknown = mapStrings(normalized, scrubSecrets); + if (nativeAuth && attachment) { + const rows = await client.credential.list({ signal }); + const privateValues = [ + nativeAuth.capability, + nativeAuth.url, + privateRoot, + outputDirectory, + attachment.plugin.options.configurationPath, + ...nativeAuth.accounts.flatMap((account) => [ + account.route.credentialID, + account.route.label, + ...privateStrings(account.metadata), + ]), + ...rows.flatMap((row) => privateStrings(row.value)), + ]; + publicEvidence = canaryPublicNativeEvidence(normalized, privateValues); + } + const nativeBytes = canonicalJson(publicEvidence); + const nativePath = join(outputDirectory, "native.json"); + const root = evidence.sessions.find( + (session) => session.info.id === manager.id, + ); + const receipt = { + preparedSha256: prepared.sha256, + model: requestedModel, + rootSessionID: manager.id, + host: { pid: host.pid, version: host.version }, + provider: { id: requestedModel.providerID, transport: "http" }, + native: { + path: "native.json", + sha256: `sha256:${createHash("sha256").update(nativeBytes).digest("hex")}`, + }, + outcome: root?.info.outcome ?? "unknown", + }; + const run = auth + ? CanaryAuthLaunchReceiptSchema.parse({ + schemaVersion: 2, + warming: false, + ...receipt, + providerPolicySha256: `sha256:${createHash("sha256").update(canonicalJson(providerPolicy)).digest("hex")}`, + nativeAuth: auth, + }) + : { + schemaVersion: 1, + ...receipt, + providerConfiguration: { + path: "config/opencode/opencode.json", + sha256: `sha256:${createHash("sha256").update(providerConfigBytes).digest("hex")}`, + }, + }; + if ( + auth && + !canaryAuthLaunchMatches({ + preparedSha256: prepared.sha256, + nativeAuth: auth, + receipt: run, + transcript: publicEvidence, + }) + ) + throw new Error("Public native auth evidence lost its launch identity."); + await writeFile(nativePath, nativeBytes, { mode: 0o600, flag: "wx" }); + publicReceipt = run; + outcomeCode = root?.info.outcome === "succeeded" ? 0 : 1; + } catch (error) { + failure = error; + } finally { + process.off("SIGINT", interrupt); + process.off("SIGTERM", terminate); + const snapshotClient = client; + const snapshot = + attachment && snapshotClient && seeded + ? await Promise.allSettled([ + Promise.resolve().then(() => + snapshotClient.credential.list({ + signal: AbortSignal.timeout(3000), + }), + ), + ]) + : []; + const termination = await Promise.allSettled([ + Promise.resolve().then(() => + server ? terminateChildProcessTree(server) : undefined, + ), + ]); + const cleanup = await Promise.allSettled([ + Promise.resolve().then(async () => { + const after = snapshot[0]; + if (after?.status === "rejected") throw after.reason; + if (attachment && after?.status === "fulfilled") + await attachment.credentialHandoff.onStop({ + before: attachment.credentialHandoff.entries, + after: after.value, + }); + }), + Promise.resolve().then(() => attachment?.dispose()), + Promise.resolve().then(() => source?.dispose()), + Promise.resolve().then(() => + nativeAuth + ? rm(privateRoot, { recursive: true, force: true }) + : undefined, + ), + Promise.resolve().then(() => + writeFile( + join(outputDirectory, "server.log"), + nativeAuth + ? "Native authentication diagnostics omitted.\n" + : scrubSecrets(startup.log), + { mode: 0o600, flag: "wx" }, + ), + ), + ]); + startup.clear(); + cleanupFailed = [...termination, ...cleanup].some( + (result) => result.status === "rejected", + ); + } + if (cleanupFailed) + throw nativeAuth + ? new NativeCanaryError("cleanup") + : new Error("Canary cleanup failed."); + if (cancelledCode !== undefined) return cancelledCode; + if (failure !== undefined) + throw nativeAuth ? new NativeCanaryError(phase) : failure; + await writeFile( + join(outputDirectory, "run.json"), + canonicalJson(publicReceipt), + { mode: 0o600, flag: "wx" }, + ); + console.log( + `Native canary evidence: ${join(outputDirectory, "native.json")}`, + ); + return outcomeCode; } + if (import.meta.main) { - const [prepared, model, prompt, ...extra] = process.argv.slice(2); + const args = process.argv.slice(2); + const [prepared, model, prompt, ...extra] = args; if (!prepared || prepared === "--help") console.log( - "canary-run (requires FLOW_EVAL_AUTHORIZATION)", + "canary-run [--native-auth-source PRIVATE_FILE] (requires FLOW_EVAL_AUTHORIZATION; collects native.json before stopping its pinned server; publishes run.json after cleanup)", + ); + else if ( + !model || + !prompt || + (extra.length !== 0 && + (extra.length !== 2 || + extra[0] !== "--native-auth-source" || + !extra[1] || + extra[1].startsWith("--"))) + ) { + console.error( + "Expected prepared.json, model, prompt file, and optional native auth source.", ); - else if (!model || !prompt || extra.length) { - console.error("Expected prepared.json, model, and prompt file."); process.exitCode = 1; } else - runPaidCanary({ prepared, model, prompt }) + runPaidCanary({ + prepared, + model, + prompt, + ...(extra[1] ? { nativeAuthSource: extra[1] } : {}), + }) .then((code) => { process.exitCode = code; }) diff --git a/scripts/eval-canary.ts b/scripts/eval-canary.ts index dafb4c23..a80022ee 100644 --- a/scripts/eval-canary.ts +++ b/scripts/eval-canary.ts @@ -7,6 +7,7 @@ import { mkdir, open, readFile, + realpath, unlink, writeFile, } from "node:fs/promises"; @@ -19,24 +20,47 @@ import { scrubSecrets, } from "../evals/cassette.js"; import { - inspectArtifact, - packedPackageManifest, - samePackedArtifact, -} from "../evals/provenance.js"; + nativeAuthSourceSha256, + stageNativeAuthClient, + verifyNativeAuthClient, +} from "../evals/native-auth/host.js"; +import { OPENCODE_HOST_VERSION } from "../evals/opencode-host.js"; +import { inspectArtifact, samePackedArtifact } from "../evals/provenance.js"; import { - RELEASE_HOST_POLICY, + releaseHostPolicy, releaseReviewerModel, + targetsOpenCodeV2, } from "../evals/release-policy.js"; import type { ActorIdentity, ArtifactIdentity } from "../evals/report.js"; import { reportArtifactForCanary } from "../evals/report-artifact.js"; -import { assuranceProjection } from "../src/application/delivery.js"; -import { SessionSchema } from "../src/application/schema.js"; +import { + assuranceProjection, + deliveryProjection, +} from "../src/application/delivery.js"; +import { + FeatureCompleteInputSchema, + SessionCloseInputSchema, + SessionSchema, +} from "../src/application/schema.js"; +import { archivedProjection } from "../src/application/session-projection.js"; import { MAX_TEST_REPORT_BYTES } from "../src/domain/limits.js"; -import { operationInputDigest } from "../src/domain/operation.js"; +import { + closureRetryRequest, + operationInputDigest, +} from "../src/domain/operation.js"; +import { currentRun } from "../src/domain/session.js"; import { readWorkspaceTestReport } from "../src/infrastructure/fs/workspace-validation.js"; export const CANARY_CHECKLIST_VERSION = "phase9-canary-v1"; export const CANARY_DERIVATION_VERSION = "canary-evidence-v1"; +export const CANARY_V2_DERIVATION_VERSION = "canary-evidence-v2"; +export function canaryDerivationVersion( + packageVersion: string, +): typeof CANARY_DERIVATION_VERSION | typeof CANARY_V2_DERIVATION_VERSION { + return targetsOpenCodeV2(packageVersion) + ? CANARY_V2_DERIVATION_VERSION + : CANARY_DERIVATION_VERSION; +} export const CANARY_CHECK_IDS = [ "installs-packed-artifact", "loads-flow-tools", @@ -59,6 +83,90 @@ export const CANARY_CHECKLIST_SHA256 = canonicalSha256( const DigestSchema = z.string().regex(/^sha256:[a-f0-9]{64}$/); const TextSchema = z.string().min(1).max(4096).regex(/\S/); +export const CANARY_NATIVE_AUTH_DIRECTORY = + "fixture/.opencode/plugins/native-auth-client"; +const CANARY_NATIVE_AUTH_FILES = [ + "index.ts", + "package.json", + "protocol.ts", + "client-plugin.ts", +] as const; +const NativeAuthFilesSchema = z + .object({ + "index.ts": DigestSchema, + "package.json": DigestSchema, + "protocol.ts": DigestSchema, + "client-plugin.ts": DigestSchema, + }) + .strict(); +export const CanaryNativeAuthSchema = z + .object({ + kind: z.literal("native-account-source-v1"), + pluginDirectory: z.literal(CANARY_NATIVE_AUTH_DIRECTORY), + entrySha256: DigestSchema, + sourceSha256: DigestSchema, + files: NativeAuthFilesSchema, + }) + .strict() + .refine( + (value) => + value.entrySha256 === value.files["index.ts"] && + value.sourceSha256 === nativeAuthSourceSha256(value.files), + "Native auth closure digest is invalid.", + ); +export type CanaryNativeAuth = z.infer; +const CanaryNativeModelSchema = z + .object({ + providerID: TextSchema, + id: TextSchema, + variant: TextSchema.optional(), + }) + .strict(); +export const CanaryAuthLaunchReceiptSchema = z + .object({ + schemaVersion: z.literal(2), + warming: z.literal(false), + preparedSha256: DigestSchema, + model: CanaryNativeModelSchema, + rootSessionID: TextSchema, + host: z + .object({ + pid: z.number().int().positive(), + version: z.literal(OPENCODE_HOST_VERSION), + }) + .strict(), + provider: z + .object({ id: TextSchema, transport: z.literal("http") }) + .strict(), + providerPolicySha256: DigestSchema, + nativeAuth: CanaryNativeAuthSchema, + native: z + .object({ path: z.literal("native.json"), sha256: DigestSchema }) + .strict(), + outcome: TextSchema, + }) + .strict(); +export type CanaryAuthLaunchReceipt = z.infer< + typeof CanaryAuthLaunchReceiptSchema +>; +const NativeAuthInstallationSchema = CanaryNativeAuthSchema.safeExtend({ + sourceFiles: z + .object({ + "index.ts": z.string().max(1024 * 1024), + "package.json": z.string().max(1024 * 1024), + "protocol.ts": z.string().max(1024 * 1024), + "client-plugin.ts": z.string().max(1024 * 1024), + }) + .strict(), + launchReceipt: CanaryAuthLaunchReceiptSchema.optional(), +}).refine( + (value) => + CANARY_NATIVE_AUTH_FILES.every( + (name) => + sha256(Buffer.from(value.sourceFiles[name])) === value.files[name], + ), + "Native auth source bytes do not match their seal.", +); const ModelIdentitySchema = z .object({ routeProvider: TextSchema, @@ -99,7 +207,7 @@ const ArtifactIdentitySchema = z unpackedManifestSha256: DigestSchema, }) .strict(); -const InstallationEvidenceSchema = z +const HistoricalInstallationEvidenceSchema = z .object({ schemaVersion: z.literal(1), preparedSha256: DigestSchema, @@ -109,6 +217,20 @@ const InstallationEvidenceSchema = z installedPluginSha256: DigestSchema, }) .strict(); +const NativeInstallationEvidenceSchema = + HistoricalInstallationEvidenceSchema.extend({ + schemaVersion: z.literal(2), + sourcePackageSha256: DigestSchema, + tuiEntrySha256: DigestSchema, + pluginPackageSha256: DigestSchema, + configSha256: DigestSchema, + nativeAuth: NativeAuthInstallationSchema.optional(), + }).strict(); +const InstallationEvidenceSchema = z.discriminatedUnion("schemaVersion", [ + HistoricalInstallationEvidenceSchema, + NativeInstallationEvidenceSchema, +]); + const ChecksSchema = z .object({ "installs-packed-artifact": z.boolean(), @@ -301,6 +423,98 @@ function completionSupportedFromDelivery( }); } +function nativeClosedSessionMatches( + calls: readonly ObservedCall[], + session: z.infer, +): boolean { + const request = closureRetryRequest(session); + if ( + request?.kind !== "completed" || + session.revision !== session.closure?.recordedRevision + ) + return false; + return calls.some((call) => { + if (call.tool !== "flow_session_close" || call.status !== "completed") + return false; + const parsed = SessionCloseInputSchema.safeParse(call.input); + if ( + !parsed.success || + canonicalJson(parsed.data.request) !== canonicalJson(request) + ) + return false; + const workflow = record(record(call.output)?.workflowData); + const operation = record(workflow?.operation); + const projection = record(workflow?.projection); + const delivery = record(workflow?.delivery); + return ( + operation !== null && + projection !== null && + delivery !== null && + operation.operationId === request.operationId && + operation.revision === session.revision && + typeof operation.replayed === "boolean" && + canonicalJson(operation.entity ?? null) === + canonicalJson(session.closure) && + canonicalJson(projection) === + canonicalJson(archivedProjection(session)) && + canonicalJson(delivery) === canonicalJson(deliveryProjection(session)) && + workflow?.closeState === undefined + ); + }); +} + +function nativeCompletedRunsMatch( + reviewerCalls: readonly ObservedCall[], + session: z.infer, +): boolean { + const runs = (session.plan?.features ?? []).map((feature) => + currentRun(session, feature.id), + ); + return ( + runs.length > 0 && + runs.every( + (run) => + run?.state === "completed" && + reviewerCalls.some((call) => { + if ( + call.tool !== "flow_feature_complete" || + call.status !== "completed" + ) + return false; + const parsed = FeatureCompleteInputSchema.safeParse(call.input); + if (!parsed.success) return false; + const request = parsed.data.request; + const assignment = run.reviews.find( + (review) => review.id === request.assignmentId, + ); + if (!assignment?.result) return false; + const { recordedRevision, ...result } = assignment.result; + const committed = session.operations.find( + (operation) => operation.id === request.operationId, + ); + const workflow = record(record(call.output)?.workflowData); + const operation = record(workflow?.operation); + const projection = record(workflow?.projection); + return ( + request.featureId === run.featureId && + request.summary === run.summary && + request.expectedRevision === recordedRevision - 1 && + canonicalJson(request.result) === canonicalJson(result) && + committed?.kind === "feature-complete" && + committed.entityId === run.id && + committed.committedRevision === recordedRevision && + operation?.operationId === committed.id && + operation.revision === committed.committedRevision && + operation.replayed === false && + projection?.sessionId === session.id && + projection.revision === recordedRevision && + canonicalJson(operation.entity ?? null) === canonicalJson(run) + ); + }), + ) + ); +} + function modelIdentity(value: Record): { readonly provider: string; readonly model: string; @@ -457,7 +671,7 @@ function observedHost(entries: readonly Record[]): { return { versions: [...versions].sort(), preparedFixture }; } -function parsedSession( +function parseSessionRepairingClosureDigest( value: unknown, ): | { readonly ok: true; readonly value: z.infer } @@ -505,6 +719,250 @@ function assuranceSatisfied( ); } +const NativeModelSchema = z + .object({ + providerID: TextSchema, + id: TextSchema, + variant: TextSchema.optional(), + }) + .passthrough(); +const NativeLocationSchema = z.object({ directory: TextSchema }).passthrough(); +const NativeSessionSchema = z + .object({ + info: z + .object({ + id: TextSchema, + parentID: TextSchema.optional(), + agent: TextSchema, + model: NativeModelSchema, + location: NativeLocationSchema, + }) + .passthrough(), + messages: z.array( + z.object({ id: TextSchema, type: TextSchema }).passthrough(), + ), + }) + .strict(); +const NativeTranscriptSchema = z + .object({ + schemaVersion: z.literal(2), + host: z + .object({ + version: TextSchema, + pid: z.number().int().positive(), + location: NativeLocationSchema, + }) + .passthrough(), + sessions: z.array(NativeSessionSchema).min(2), + }) + .strict(); +const NativeAssistantSchema = z + .object({ + id: TextSchema, + type: z.literal("assistant"), + agent: TextSchema, + model: NativeModelSchema, + time: z + .object({ created: z.number().finite(), completed: z.number().finite() }) + .passthrough(), + content: z.array( + z.discriminatedUnion("type", [ + z.object({ type: z.literal("text"), text: z.string() }).passthrough(), + z + .object({ type: z.literal("reasoning"), text: z.string() }) + .passthrough(), + z + .object({ + type: z.literal("tool"), + id: TextSchema, + name: TextSchema, + time: z + .object({ + created: z.number().finite(), + completed: z.number().finite().optional(), + }) + .passthrough(), + state: z + .object({ + status: z.enum(["streaming", "running", "completed", "error"]), + input: z.unknown(), + content: z.array(z.unknown()).optional(), + metadata: z.record(z.string(), z.unknown()).optional(), + }) + .passthrough(), + }) + .passthrough(), + ]), + ), + }) + .passthrough(); + +function nativeObservation(value: unknown): { + readonly supported: boolean; + readonly calls: readonly ObservedCall[]; + readonly actors: ReturnType; + readonly host: ReturnType; +} { + const parsed = NativeTranscriptSchema.safeParse(value); + const empty = { + supported: false, + calls: [], + actors: { actors: [], complete: false, managerSessionId: null }, + host: { versions: [], preparedFixture: false }, + }; + if (!parsed.success) return empty; + const { sessions, host } = parsed.data; + const roots = sessions.filter( + (session) => session.info.parentID === undefined, + ); + if ( + roots.length !== 1 || + new Set(sessions.map((session) => session.info.id)).size !== sessions.length + ) + return empty; + const manager = roots[0]; + if (!manager || manager.info.agent.startsWith("flow-")) return empty; + const byID = new Map(sessions.map((session) => [session.info.id, session])); + const calls: ObservedCall[] = []; + const actors = new Map<"manager" | "reviewer", ActorIdentity>(); + let consistent = true; + const messageIDs = new Set(); + for (const session of sessions) { + const info = session.info; + const ancestry = new Set([info.id]); + let parent = info.parentID; + while (parent !== undefined) { + if (ancestry.has(parent)) return empty; + ancestry.add(parent); + const ancestor = byID.get(parent); + if (!ancestor) return empty; + parent = ancestor.info.parentID; + } + if ( + !ancestry.has(manager.info.id) || + info.location.directory !== host.location.directory || + (info.model.variant !== undefined && info.model.variant !== "default") + ) + consistent = false; + const role = + info.id === manager.info.id + ? "manager" + : info.agent === "flow-reviewer" + ? "reviewer" + : null; + if (role === "reviewer" && info.parentID !== manager.info.id) + consistent = false; + let assistants = 0; + for (const message of session.messages) { + if (messageIDs.has(message.id)) consistent = false; + messageIDs.add(message.id); + if (message.type !== "assistant") { + if ( + ![ + "user", + "synthetic", + "system", + "compaction", + "idle", + "agent-switched", + "model-switched", + "location-switched", + "shell", + "skill", + ].includes(message.type) + ) + consistent = false; + continue; + } + const assistant = NativeAssistantSchema.safeParse(message); + if (!assistant.success) { + consistent = false; + continue; + } + assistants++; + if ( + assistant.data.agent !== info.agent || + canonicalJson(assistant.data.model) !== canonicalJson(info.model) || + assistant.data.error !== undefined + ) + consistent = false; + const callIDs = new Set(); + for (const part of assistant.data.content) { + if (part.type !== "tool") continue; + if (callIDs.has(part.id)) consistent = false; + callIDs.add(part.id); + const state = part.state; + const content = records(state.content ?? []); + const output = + content.length === 1 && + content[0]?.type === "text" && + typeof content[0].text === "string" + ? parsedJson(content[0].text) + : null; + const flowOutput = record(output); + const completed = + state.status === "completed" && + content.length === state.content?.length && + record(state.input) !== null && + (state.metadata?.truncated === undefined || + state.metadata.truncated === false) && + (!part.name.startsWith("flow_") || flowOutput?.status === "ok"); + calls.push({ + tool: part.name, + status: completed ? "completed" : "unknown", + sessionId: info.id, + input: record(state.input) ?? {}, + output, + metadata: {}, + }); + if (part.name.startsWith("flow_") && role === null) consistent = false; + } + } + if (role !== null) { + if (assistants === 0) consistent = false; + const model = { + routeProvider: info.model.providerID, + gateway: null, + family: info.model.id, + model: info.model.id, + revision: null, + }; + const prior = actors.get(role); + if (prior && canonicalJson(prior.requestedModel) !== canonicalJson(model)) + consistent = false; + actors.set(role, { + role, + requestedModel: model, + actualModel: { kind: "observed", value: model }, + sessionIds: [...(prior?.sessionIds ?? []), info.id], + }); + } + } + const reviewer = actors.get("reviewer"); + const managerActor = actors.get("manager"); + if ( + !reviewer || + !managerActor || + canonicalJson(reviewer.requestedModel) !== + canonicalJson(managerActor.requestedModel) + ) + consistent = false; + return { + supported: true, + calls: consistent ? calls : [], + actors: { + actors: [...actors.values()], + complete: consistent && reviewer !== undefined, + managerSessionId: consistent ? manager.info.id : null, + }, + host: { + versions: [host.version], + preparedFixture: + host.location.directory === "" && consistent, + }, + }; +} + export function deriveCanaryResult(input: { readonly packageVersion: string; readonly artifactSha256: string; @@ -520,29 +978,86 @@ export function deriveCanaryResult(input: { actors: readonly ActorIdentity[]; hostConfigSha256: string; }> { - const transcript = transcriptShape(input.transcript); - const calls = observedCalls(transcript.entries); - const installation = InstallationEvidenceSchema.safeParse(input.installation); - const session = parsedSession(input.session); + const native = targetsOpenCodeV2(input.packageVersion) + ? nativeObservation(input.transcript) + : null; + const transcript = native ?? transcriptShape(input.transcript); + const calls = native + ? native.calls + : observedCalls(transcriptShape(input.transcript).entries); + const installation = ( + native + ? NativeInstallationEvidenceSchema + : HistoricalInstallationEvidenceSchema + ).safeParse(input.installation); + const nativeSession = native ? SessionSchema.safeParse(input.session) : null; + const session = nativeSession + ? nativeSession.success + ? { ok: true as const, value: nativeSession.data } + : { ok: false as const } + : parseSessionRepairingClosureDigest(input.session); const assurance = session.ok && session.value.closure ? assuranceProjection(session.value) : null; - const actors = derivedActors(transcript.entries, calls); + const actors = native + ? native.actors + : derivedActors(transcriptShape(input.transcript).entries, calls); const managerCalls = actors.managerSessionId ? calls.filter((call) => call.sessionId === actors.managerSessionId) : []; - const host = observedHost(transcript.entries); + const host = native + ? native.host + : observedHost(transcriptShape(input.transcript).entries); const hasCompletedFlowCall = managerCalls.some( (call) => call.tool.startsWith("flow_") && call.status === "completed", ); - const loadedPlugin = loadedPluginMatches( - managerCalls, - input.packageVersion, - input.pluginEntrySha256, - ); + const loadedPlugin = + loadedPluginMatches( + managerCalls, + input.packageVersion, + input.pluginEntrySha256, + ) && + (!native || + managerCalls.some( + (call) => + call.tool === "flow_status" && + call.status === "completed" && + record(record(record(call.output)?.workflowData)?.recovery)?.mode === + "off", + )); + const boundRuns = + !native || + (session.ok && + actors.complete && + nativeCompletedRunsMatch( + calls.filter((call) => + actors.actors.some( + (actor) => + actor.role === "reviewer" && + actor.sessionIds.includes(call.sessionId ?? ""), + ), + ), + session.value, + )); + const boundClose = + !native || + (session.ok && nativeClosedSessionMatches(managerCalls, session.value)); + const auth = + installation.success && installation.data.schemaVersion === 2 + ? installation.data.nativeAuth + : undefined; + const boundAuth = + !auth || + canaryAuthLaunchMatches({ + preparedSha256: input.preparedSha256, + nativeAuth: auth, + receipt: auth.launchReceipt, + transcript: input.transcript, + }); const checks: Checks = { "installs-packed-artifact": + boundAuth && installation.success && installation.data.preparedSha256 === input.preparedSha256 && installation.data.artifactSha256 === input.artifactSha256 && @@ -555,7 +1070,8 @@ export function deriveCanaryResult(input: { "loads-flow-tools": host.preparedFixture && host.versions.length === 1 && - host.versions[0] === RELEASE_HOST_POLICY.opencodeVersion && + host.versions[0] === + releaseHostPolicy(input.packageVersion).opencodeVersion && hasCompletedFlowCall && loadedPlugin, "saves-plan": @@ -567,23 +1083,38 @@ export function deriveCanaryResult(input: { ) && completed(managerCalls, "flow_plan_save"), "captures-validation": + boundRuns && assurance !== null && assuranceSatisfied(assurance, "accepted-validation") && assuranceSatisfied(assurance, "canonical-gate") && assuranceSatisfied(assurance, "declared-evidence") && completed(managerCalls, "flow_validation_start"), "dispatches-reviewer": + boundRuns && assurance !== null && assuranceSatisfied(assurance, "recorded-completion") && actors.complete && completed(managerCalls, "flow_review_start") && - managerCalls.some( - (call) => - call.tool === "task" && - call.status === "completed" && - call.input.subagent_type === "flow-reviewer", - ), + (native + ? calls.some( + (call) => + call.tool === "flow_feature_complete" && + call.status === "completed" && + actors.actors.some( + (actor) => + actor.role === "reviewer" && + actor.sessionIds.includes(call.sessionId ?? ""), + ), + ) + : managerCalls.some( + (call) => + call.tool === "task" && + call.status === "completed" && + call.input.subagent_type === "flow-reviewer", + )), "closes-with-delivery": + boundClose && + boundRuns && assurance !== null && assurance.conclusion === "completion-supported" && completionSupportedFromDelivery(managerCalls, assurance), @@ -623,12 +1154,123 @@ export function deriveCanaryResult(input: { } const PackageMetadataSchema = z .object({ - dependencies: z.object({ zod: TextSchema }).passthrough(), + name: z.literal("opencode-plugin-flow"), + version: TextSchema, + dependencies: z.record(z.string(), TextSchema), devDependencies: z - .object({ "@opencode-ai/plugin": TextSchema }) + .object({ "@opencode/plugin": z.literal(OPENCODE_HOST_VERSION) }) .passthrough(), }) .passthrough(); +function canaryPluginPackage( + metadata: z.infer, +): Buffer { + return Buffer.from( + canonicalJson({ + name: metadata.name, + version: metadata.version, + type: "module", + exports: { ".": "./index.js", "./tui": "./tui.js" }, + dependencies: { + ...metadata.dependencies, + "@opencode/plugin": OPENCODE_HOST_VERSION, + }, + }), + ); +} +function canaryConfig(): Buffer { + return Buffer.from( + canonicalJson({ + plugins: [ + { + package: "./.opencode/plugins/flow", + options: { recovery: { mode: "off" } }, + }, + ], + update: "disable", + share: "disabled", + }), + ); +} +export function canaryProviderConfiguration(model: { + providerID: string; + id: string; + variant?: string | undefined; +}) { + return { + providers: { + [model.providerID]: { + settings: { transport: "http" as const }, + models: { + [model.id]: { + settings: { transport: "http" as const }, + variants: [ + { + id: model.variant ?? "default", + settings: { transport: "http" as const }, + }, + ], + }, + }, + }, + }, + }; +} +function nativeAuthDescriptor(value: CanaryNativeAuth): CanaryNativeAuth { + return { + kind: value.kind, + pluginDirectory: value.pluginDirectory, + entrySha256: value.entrySha256, + sourceSha256: value.sourceSha256, + files: value.files, + }; +} +export function canaryAuthLaunchMatches(input: { + preparedSha256: string; + nativeAuth: CanaryNativeAuth; + receipt: unknown; + transcript: unknown; +}): boolean { + const parsed = CanaryAuthLaunchReceiptSchema.safeParse(input.receipt); + if (!parsed.success) return false; + const receipt = parsed.data; + const transcript = record(input.transcript); + const host = record(transcript?.host); + const root = records(array(transcript?.sessions)) + .map((session) => record(session.info)) + .find( + (info) => + info?.id === receipt.rootSessionID && info.parentID === undefined, + ); + return ( + receipt.preparedSha256 === input.preparedSha256 && + canonicalJson(receipt.nativeAuth) === + canonicalJson(nativeAuthDescriptor(input.nativeAuth)) && + receipt.native.sha256 === + sha256(Buffer.from(canonicalJson(input.transcript))) && + host?.pid === receipt.host.pid && + host.version === receipt.host.version && + root != null && + canonicalJson(root.model) === canonicalJson(receipt.model) && + root.outcome === receipt.outcome && + receipt.provider.id === receipt.model.providerID && + receipt.providerPolicySha256 === + sha256( + Buffer.from(canonicalJson(canaryProviderConfiguration(receipt.model))), + ) + ); +} +export const CANARY_PLUGIN_DIRECTORY = "fixture/.opencode/plugins/flow"; +const NativePreparationSchema = z + .object({ + hostVersion: z.literal(OPENCODE_HOST_VERSION), + sourcePackageSha256: DigestSchema, + tuiEntrySha256: DigestSchema, + pluginPackageSha256: DigestSchema, + configSha256: DigestSchema, + nativeAuth: CanaryNativeAuthSchema.optional(), + }) + .strict(); const EvidenceRefSchema = z .object({ path: TextSchema, @@ -636,7 +1278,7 @@ const EvidenceRefSchema = z bytes: z.number().int().safe().nonnegative().max(MAX_TEST_REPORT_BYTES), }) .strict(); -export const PreparedCanarySchema = z +const HistoricalPreparedCanarySchema = z .object({ schemaVersion: z.literal(1), releaseTag: TextSchema, @@ -650,12 +1292,22 @@ export const PreparedCanarySchema = z sha256: DigestSchema, }) .strict(); +export const PreparedCanarySchema = z.discriminatedUnion("schemaVersion", [ + HistoricalPreparedCanarySchema, + HistoricalPreparedCanarySchema.extend({ + schemaVersion: z.literal(2), + runtime: NativePreparationSchema, + }).strict(), +]); export type PreparedCanary = z.infer; export const CanaryRecordSchema = z .object({ schemaVersion: z.literal(1), - derivationVersion: z.literal(CANARY_DERIVATION_VERSION), + derivationVersion: z.enum([ + CANARY_DERIVATION_VERSION, + CANARY_V2_DERIVATION_VERSION, + ]), preparedSha256: DigestSchema, pluginEntrySha256: DigestSchema, status: z.enum(["passed", "failed", "incomplete"]), @@ -729,7 +1381,9 @@ export function canaryRecordSha256( } export function preparedCanarySha256( - prepared: Omit, + prepared: + | Omit, "sha256"> + | Omit, "sha256">, ): string { return canonicalSha256("flow-canary-preparation-v1", prepared); } @@ -741,7 +1395,9 @@ export function parsePreparedCanary(input: unknown): PreparedCanary { prepared.sha256 !== preparedCanarySha256(withoutHash) || prepared.releaseTag !== `v${prepared.artifact.packageVersion}` || prepared.artifactSha256 !== artifactIdentitySha256(prepared.artifact) || - prepared.checklistSha256 !== CANARY_CHECKLIST_SHA256 + prepared.checklistSha256 !== CANARY_CHECKLIST_SHA256 || + (prepared.schemaVersion === 2) !== + targetsOpenCodeV2(prepared.artifact.packageVersion) ) { throw new Error("Canary preparation bindings are invalid."); } @@ -763,6 +1419,10 @@ export function parseCanaryRecord( const record = parsed.data; const { recordSha256: _recordSha256, ...withoutHash } = record; const issues = [ + ...(record.derivationVersion === + canaryDerivationVersion(record.artifact.packageVersion) + ? [] + : ["Canary derivation does not match the release host."]), ...(record.releaseTag === `v${record.artifact.packageVersion}` ? [] : ["Canary tag does not match its artifact version."]), @@ -852,6 +1512,7 @@ export async function prepareCanary(input: { readonly expectedArtifact?: ArtifactIdentity; readonly outputDirectory: string; readonly preparedAt?: Date; + readonly nativeAuth?: boolean; }): Promise { const inspectedArtifact = await inspectArtifact({ repositoryRoot: input.repositoryRoot, @@ -867,7 +1528,9 @@ export async function prepareCanary(input: { } const artifact = input.expectedArtifact ?? inspectedArtifact; const fixture = join(input.outputDirectory, "fixture"); - await mkdir(join(fixture, ".opencode", "plugins"), { recursive: true }); + await mkdir(join(input.outputDirectory, CANARY_PLUGIN_DIRECTORY), { + recursive: true, + }); const copiedArtifactPath = join(input.outputDirectory, "artifact.tgz"); await copyFile(input.artifactPath, copiedArtifactPath); const copiedArtifact = await inspectArtifact({ @@ -883,29 +1546,51 @@ export async function prepareCanary(input: { copiedArtifactPath, "package/dist/index.js", ); - const packageMetadata = PackageMetadataSchema.parse( - await packedPackageManifest(copiedArtifactPath), + const sourcePackage = extractArtifactFile( + copiedArtifactPath, + "package/package.json", ); - await writeFile( - join(fixture, ".opencode", "plugins", "flow.js"), - pluginEntry, + const packageMetadata = PackageMetadataSchema.parse( + JSON.parse(sourcePackage.toString("utf8")), ); - await writeFile( - join(fixture, ".opencode", "package.json"), - `${JSON.stringify( - { - private: true, - version: packageMetadata.version, - dependencies: { - "@opencode-ai/plugin": - packageMetadata.devDependencies["@opencode-ai/plugin"], - zod: packageMetadata.dependencies.zod, - }, - }, - null, - 2, - )}\n`, + if (!targetsOpenCodeV2(artifact.packageVersion)) + throw new Error( + "Active canary preparation requires a Flow release targeting OpenCode v2.", + ); + const tuiEntry = extractArtifactFile( + copiedArtifactPath, + "package/dist/tui.js", ); + const pluginPackage = canaryPluginPackage(packageMetadata); + const config = canaryConfig(); + for (const [name, bytes] of [ + ["index.js", pluginEntry], + ["tui.js", tuiEntry], + ["package.json", pluginPackage], + ] as const) + await writeFile( + join(input.outputDirectory, CANARY_PLUGIN_DIRECTORY, name), + bytes, + ); + await writeFile(join(fixture, "opencode.json"), config); + const runtime: z.infer = { + hostVersion: OPENCODE_HOST_VERSION, + sourcePackageSha256: sha256(sourcePackage), + tuiEntrySha256: sha256(tuiEntry), + pluginPackageSha256: sha256(pluginPackage), + configSha256: sha256(config), + }; + if (input.nativeAuth) { + const staged = await stageNativeAuthClient( + resolve(input.outputDirectory, CANARY_NATIVE_AUTH_DIRECTORY), + ); + await verifyNativeAuthClient(staged); + runtime.nativeAuth = CanaryNativeAuthSchema.parse({ + ...staged, + kind: "native-account-source-v1", + pluginDirectory: CANARY_NATIVE_AUTH_DIRECTORY, + }); + } await mkdir(join(fixture, "src"), { recursive: true }); await writeFile( join(fixture, "src", "canary.ts"), @@ -915,12 +1600,14 @@ export async function prepareCanary(input: { join(fixture, "README.md"), `# Flow exact-artifact canary\n\nArtifact: ${artifact.tarballSha256}\n\nRun every checklist item and export sanitized JSON session and transcript evidence.\n`, ); - const base: Omit = { - schemaVersion: 1 as const, + const base = { + schemaVersion: 2 as const, + runtime, releaseTag: `v${artifact.packageVersion}`, artifact, artifactSha256: artifactIdentitySha256(artifact), - checklistVersion: CANARY_CHECKLIST_VERSION, + checklistVersion: + CANARY_CHECKLIST_VERSION as typeof CANARY_CHECKLIST_VERSION, checklistSha256: CANARY_CHECKLIST_SHA256, preparedAt: (input.preparedAt ?? new Date()).toISOString(), artifactFile: "artifact.tgz" as const, @@ -943,6 +1630,7 @@ export async function prepareCanaryFromReport(input: { readonly reportPath: string; readonly outputDirectory: string; readonly preparedAt?: Date; + readonly nativeAuth?: boolean; }): Promise { const reportArtifact = await reportArtifactForCanary({ repositoryRoot: input.repositoryRoot, @@ -954,6 +1642,7 @@ export async function prepareCanaryFromReport(input: { expectedArtifact: reportArtifact.artifact, outputDirectory: input.outputDirectory, ...(input.preparedAt ? { preparedAt: input.preparedAt } : {}), + ...(input.nativeAuth ? { nativeAuth: true } : {}), }); } @@ -968,7 +1657,7 @@ function redactEvidence(value: unknown, projectPath: string): unknown { ); } -async function measureInstallation( +export async function measureCanaryInstallation( prepared: PreparedCanary, directory: string, ): Promise> { @@ -982,7 +1671,14 @@ async function measureInstallation( } const [artifactBytes, installedPlugin] = await Promise.all([ readFile(join(directory, prepared.artifactFile)), - readFile(join(directory, "fixture", ".opencode", "plugins", "flow.js")), + readFile( + join( + directory, + prepared.schemaVersion === 2 + ? `${CANARY_PLUGIN_DIRECTORY}/index.js` + : "fixture/.opencode/plugins/flow.js", + ), + ), ]); if (sha256(artifactBytes) !== prepared.artifact.tarballSha256) { throw new Error("Prepared canary tarball bytes do not match the artifact."); @@ -993,6 +1689,89 @@ async function measureInstallation( "Prepared canary installed plugin bytes do not match the artifact.", ); } + if (prepared.schemaVersion === 2) { + const [sourcePackage, tuiEntry, pluginPackage, config] = await Promise.all([ + Promise.resolve( + extractArtifactFile( + join(directory, prepared.artifactFile), + "package/package.json", + ), + ), + readFile(join(directory, CANARY_PLUGIN_DIRECTORY, "tui.js")), + readFile(join(directory, CANARY_PLUGIN_DIRECTORY, "package.json")), + readFile(join(directory, "fixture/opencode.json")), + ]); + const measured = { + sourcePackageSha256: sha256(sourcePackage), + tuiEntrySha256: sha256(tuiEntry), + pluginPackageSha256: sha256(pluginPackage), + configSha256: sha256(config), + }; + const { + hostVersion: _hostVersion, + nativeAuth, + ...expected + } = prepared.runtime; + const metadata = PackageMetadataSchema.parse( + JSON.parse(sourcePackage.toString("utf8")), + ); + if ( + canonicalJson(measured) !== canonicalJson(expected) || + !pluginPackage.equals(canaryPluginPackage(metadata)) || + !config.equals(canaryConfig()) || + sha256( + extractArtifactFile( + join(directory, prepared.artifactFile), + "package/dist/index.js", + ), + ) !== installedPluginSha256 || + sha256( + extractArtifactFile( + join(directory, prepared.artifactFile), + "package/dist/tui.js", + ), + ) !== measured.tuiEntrySha256 + ) + throw new Error("Prepared canary installation bytes changed."); + let measuredAuth: z.infer | undefined; + if (nativeAuth) { + const staged = { + ...nativeAuth, + pluginDirectory: await realpath( + join(directory, nativeAuth.pluginDirectory), + ), + }; + if ( + staged.pluginDirectory !== + resolve(await realpath(directory), nativeAuth.pluginDirectory) + ) + throw new Error("Native auth client path changed."); + await verifyNativeAuthClient(staged); + const sourceFiles = Object.fromEntries( + await Promise.all( + CANARY_NATIVE_AUTH_FILES.map(async (name) => [ + name, + await readFile(join(staged.pluginDirectory, name), "utf8"), + ]), + ), + ); + measuredAuth = NativeAuthInstallationSchema.parse({ + ...nativeAuth, + sourceFiles, + }); + } + return { + schemaVersion: 2, + preparedSha256: prepared.sha256, + artifactSha256: prepared.artifactSha256, + tarballSha256: prepared.artifact.tarballSha256, + pluginEntrySha256: prepared.pluginEntrySha256, + installedPluginSha256, + ...measured, + ...(measuredAuth ? { nativeAuth: measuredAuth } : {}), + }; + } + return { schemaVersion: 1, preparedSha256: prepared.sha256, @@ -1027,10 +1806,11 @@ export async function recordCanary(input: { readonly session: unknown | null; readonly transcript: unknown | null; readonly recordedAt?: Date; + readonly launchReceipt?: unknown; }): Promise<{ readonly path: string; readonly record: CanaryRecord }> { const prepared = parsePreparedCanary(input.prepared); const recordedAt = input.recordedAt ?? new Date(); - const installationValue = await measureInstallation( + const installationValue = await measureCanaryInstallation( prepared, input.preparedDirectory, ); @@ -1043,12 +1823,49 @@ export async function recordCanary(input: { ); } const fixturePath = resolve(input.preparedDirectory, "fixture"); - const redactedSession = - input.session === null ? null : redactEvidence(input.session, fixturePath); - const redactedTranscript = - input.transcript === null + const canonicalFixturePath = + prepared.schemaVersion === 2 ? await realpath(fixturePath) : fixturePath; + const redact = (value: unknown) => + value === null ? null - : redactEvidence(input.transcript, fixturePath); + : redactEvidence( + redactEvidence(value, fixturePath), + canonicalFixturePath, + ); + let redactedSession = redact(input.session); + if (prepared.schemaVersion === 2 && redactedSession !== null) { + const sanitized = parseSessionRepairingClosureDigest(redactedSession); + if (!sanitized.ok) + throw new Error("Redacted canary session violates Session v5."); + redactedSession = sanitized.value; + } + const redactedTranscript = redact(input.transcript); + if (installationValue.schemaVersion === 2 && installationValue.nativeAuth) { + if ( + !canaryAuthLaunchMatches({ + preparedSha256: prepared.sha256, + nativeAuth: installationValue.nativeAuth, + receipt: input.launchReceipt, + transcript: input.transcript, + }) + ) + throw new Error( + "Native auth canary requires a matching public launch receipt.", + ); + const receipt = CanaryAuthLaunchReceiptSchema.parse( + redact(input.launchReceipt), + ); + installationValue.nativeAuth.launchReceipt = { + ...receipt, + native: { + ...receipt.native, + sha256: sha256(Buffer.from(canonicalJson(redactedTranscript))), + }, + }; + } else if (input.launchReceipt !== undefined) + throw new Error( + "Native auth launch receipt supplied without a prepared attachment.", + ); const derived = deriveCanaryResult({ packageVersion: prepared.artifact.packageVersion, artifactSha256: prepared.artifactSha256, @@ -1081,7 +1898,9 @@ export async function recordCanary(input: { }); const base: Omit = { schemaVersion: 1 as const, - derivationVersion: CANARY_DERIVATION_VERSION, + derivationVersion: canaryDerivationVersion( + prepared.artifact.packageVersion, + ), preparedSha256: prepared.sha256, pluginEntrySha256: prepared.pluginEntrySha256, status: derived.status, @@ -1175,7 +1994,10 @@ export async function canaryRecordIssue(input: { if (!samePackedArtifact(record.artifact, input.expectedArtifact)) return "Canary artifact does not match the rebuilt artifact."; if (record.status !== "passed") return `Canary status is ${record.status}.`; - const pinned = releaseReviewerModel(input.version); + const manager = record.actors.find((actor) => actor.role === "manager"); + if (targetsOpenCodeV2(input.version) && !manager) + return "Canary is missing its manager identity."; + const pinned = releaseReviewerModel(input.version, manager?.requestedModel); if (pinned) { for (const role of ["manager", "reviewer"] as const) { const actors = record.actors.filter((item) => item.role === role); @@ -1319,6 +2141,7 @@ async function main(args: readonly string[]): Promise { repositoryRoot, reportPath: required(args, "--report"), outputDirectory: required(args, "--out"), + ...(args.includes("--native-auth") ? { nativeAuth: true } : {}), }); process.stdout.write( `INCONCLUSIVE: manual canary pending\n${canonicalJson(prepared)}\n`, @@ -1338,6 +2161,9 @@ async function main(args: readonly string[]): Promise { transcript: option(args, "--transcript") ? await json(required(args, "--transcript")) : null, + ...(option(args, "--launch-receipt") + ? { launchReceipt: await json(required(args, "--launch-receipt")) } + : {}), }); process.stdout.write(`${result.record.status}: ${result.path}\n`); return; diff --git a/scripts/export-canary-native.ts b/scripts/export-canary-native.ts new file mode 100644 index 00000000..0913b0ce --- /dev/null +++ b/scripts/export-canary-native.ts @@ -0,0 +1,122 @@ +import { realpath } from "node:fs/promises"; +import type { + OpenCodeClient, + ServerInfo, + SessionTransferData, +} from "@opencode/client"; +import { OPENCODE_HOST_VERSION } from "../evals/opencode-host.js"; + +export type NativeCanaryExport = { + readonly schemaVersion: 2; + readonly host: ServerInfo & { + readonly location: { readonly directory: string }; + }; + readonly sessions: readonly SessionTransferData[]; +}; + +type CanaryClient = { + readonly server: Pick; + readonly session: Pick< + OpenCodeClient["session"], + "list" | "export" | "active" + >; +}; +export async function collectNativeCanary( + client: CanaryClient, + input: { + readonly directory: string; + readonly rootSessionID: string; + readonly signal?: AbortSignal; + }, +): Promise { + const directory = await realpath(input.directory); + const request = () => ({ + signal: AbortSignal.any([ + ...(input.signal ? [input.signal] : []), + AbortSignal.timeout(30000), + ]), + }); + const host = await client.server.info(request()); + if (host.version !== OPENCODE_HOST_VERSION) + throw new Error("Canary server is not the pinned OpenCode host."); + const descendants = async () => { + const all: SessionTransferData["info"][] = []; + const cursors = new Set(); + let cursor: string | undefined; + for (;;) { + const page = await client.session.list( + { + limit: 100, + ...(cursor ? { cursor } : { directory, order: "asc" as const }), + }, + request(), + ); + all.push(...page.data); + cursor = page.cursor.next ?? undefined; + if (all.length > 4096) + throw new Error("Canary session census exceeds its bound."); + if (!cursor) break; + if (cursors.has(cursor)) + throw new Error("Canary session pagination did not converge."); + cursors.add(cursor); + } + if (new Set(all.map((session) => session.id)).size !== all.length) + throw new Error("Canary session census contains duplicate identities."); + const root = all.find((session) => session.id === input.rootSessionID); + if (!root || root.parentID !== undefined) + throw new Error("Canary manager must be a native root session."); + const selected = new Set([root.id]); + for (let changed = true; changed; ) { + changed = false; + for (const session of all) { + if ( + session.parentID && + selected.has(session.parentID) && + !selected.has(session.id) + ) { + selected.add(session.id); + changed = true; + } + } + } + return all + .filter((session) => selected.has(session.id)) + .sort((a, b) => a.id.localeCompare(b.id)); + }; + const census = await descendants(); + const sessions: SessionTransferData[] = []; + for (const session of census) { + const exported = await client.session.export( + { + sessionID: session.id, + sanitize: false, + }, + request(), + ); + if ( + exported.info.id !== session.id || + (await realpath(exported.info.location.directory)) !== directory || + exported.info.parentID !== session.parentID || + exported.info.agent !== session.agent || + JSON.stringify(exported.info.model) !== JSON.stringify(session.model) + ) + throw new Error( + "Canary native session identity changed during collection.", + ); + sessions.push(exported); + } + const after = await descendants(); + if (JSON.stringify(after) !== JSON.stringify(census)) + throw new Error("Canary session tree changed during collection."); + const active = await client.session.active(request()); + if (sessions.some((session) => Object.hasOwn(active, session.info.id))) + throw new Error("Canary sessions must be idle before collection."); + const finalHost = await client.server.info(request()); + if (finalHost.version !== host.version || finalHost.pid !== host.pid) + throw new Error("Canary server identity changed during collection."); + return { + schemaVersion: 2, + host: { ...host, location: { directory } }, + sessions, + }; +} diff --git a/scripts/native-eval-auth.ts b/scripts/native-eval-auth.ts new file mode 100644 index 00000000..19b3e215 --- /dev/null +++ b/scripts/native-eval-auth.ts @@ -0,0 +1,183 @@ +import { constants } from "node:fs"; +import { lstat, mkdir, open, rm, rmdir } from "node:fs/promises"; +import { dirname, resolve } from "node:path"; +import { + type NativeAccountBroker, + startNativeAccountBroker, +} from "../evals/native-auth/broker.js"; +import { requirePaidAuthorization } from "./paid-budget.js"; + +export async function runNativeAccountSource( + input: { + opencodeExecutable: string; + models: readonly string[]; + output: string; + signal: AbortSignal; + budgetDirectory?: string; + }, + start: typeof startNativeAccountBroker = startNativeAccountBroker, +): Promise { + input.signal.throwIfAborted(); + const budget = await requirePaidAuthorization(input.budgetDirectory); + if ( + input.models.length === 0 || + input.models.some((model) => !budget.authorization.models.includes(model)) + ) + throw new Error("Native account source models are not authorized."); + const output = resolve(input.output); + const directory = dirname(output); + let directoryCreated = false; + let outputCreated = false; + let broker: NativeAccountBroker | undefined; + let file: Awaited> | undefined; + let outputIdentity: { dev: number; ino: number } | undefined; + let directoryIdentity: { dev: number; ino: number } | undefined; + const failures: unknown[] = []; + try { + try { + await mkdir(directory, { mode: 0o700 }); + directoryCreated = true; + } catch (error) { + if ( + !(error instanceof Error && "code" in error && error.code === "EEXIST") + ) + throw error; + } + const parent = await lstat(directory); + if ( + !parent.isDirectory() || + parent.isSymbolicLink() || + parent.uid !== process.getuid?.() || + (parent.mode & 0o777) !== 0o700 + ) + throw new Error( + "Native account source requires a private output directory.", + ); + directoryIdentity = { dev: parent.dev, ino: parent.ino }; + file = await open( + output, + constants.O_WRONLY | + constants.O_CREAT | + constants.O_EXCL | + constants.O_NOFOLLOW, + 0o600, + ); + outputCreated = true; + const identity = await file.stat(); + outputIdentity = { dev: identity.dev, ino: identity.ino }; + broker = await start({ + opencodeExecutable: input.opencodeExecutable, + models: input.models, + signal: input.signal, + }); + await file.writeFile(`${JSON.stringify(broker.configuration)}\n`); + await file.sync(); + console.log("Native account source ready."); + await new Promise((complete) => { + if (input.signal.aborted) complete(); + else + input.signal.addEventListener("abort", () => complete(), { + once: true, + }); + }); + } catch (error) { + failures.push(error); + } + const settled = await Promise.allSettled([ + Promise.resolve().then(() => broker?.dispose()), + Promise.resolve().then(() => file?.close()), + ]); + for (const result of settled) + if (result.status === "rejected") failures.push(result.reason); + if (outputCreated) { + try { + const current = await lstat(output); + if ( + !outputIdentity || + current.dev !== outputIdentity.dev || + current.ino !== outputIdentity.ino + ) + throw new Error("Native account source output changed."); + await rm(output); + } catch (error) { + if ( + !(error instanceof Error && "code" in error && error.code === "ENOENT") + ) + failures.push(error); + } + } + if (directoryCreated) { + try { + const current = await lstat(directory); + if ( + !directoryIdentity || + current.dev !== directoryIdentity.dev || + current.ino !== directoryIdentity.ino + ) + throw new Error("Native account source directory changed."); + await rmdir(directory); + } catch (error) { + if ( + !(error instanceof Error && "code" in error && error.code === "ENOENT") + ) + failures.push(error); + } + } + if (failures.length === 1) throw failures[0]; + if (failures.length > 1) + throw new AggregateError( + failures, + "Native account source did not close cleanly.", + ); +} + +if (import.meta.main) { + const args = process.argv.slice(2); + if (args.includes("--help")) + console.log( + "native-eval-auth --opencode --model [--model ...] --out (requires FLOW_EVAL_AUTHORIZATION; stop with SIGINT or SIGTERM)", + ); + else { + const models: string[] = []; + let output: string | undefined; + let executable = "opencode2"; + let valid = true; + for (let index = 0; index < args.length; index += 2) { + const flag = args[index]; + const value = args[index + 1]; + if (!value) { + valid = false; + break; + } + if (flag === "--model") models.push(value); + else if (flag === "--out" && output === undefined) output = value; + else if (flag === "--opencode") executable = value; + else valid = false; + } + if (!valid || !output || models.length === 0) { + console.error("Expected model selection and one private output file."); + process.exitCode = 2; + } else { + const controller = new AbortController(); + const stop = () => controller.abort(); + process.once("SIGINT", stop); + process.once("SIGTERM", stop); + try { + await runNativeAccountSource({ + opencodeExecutable: executable, + models, + output, + signal: controller.signal, + }); + } catch { + console.error( + "Native account source failed. Check authorization and private configuration.", + ); + process.exitCode = 2; + } finally { + process.off("SIGINT", stop); + process.off("SIGTERM", stop); + } + } + } +} diff --git a/scripts/probe-opencode-eval-metadata.ts b/scripts/probe-opencode-eval-metadata.ts index a65e314e..7739c812 100644 --- a/scripts/probe-opencode-eval-metadata.ts +++ b/scripts/probe-opencode-eval-metadata.ts @@ -9,7 +9,15 @@ import { preparePackageCache, splitModel, } from "../evals/harness.js"; +import { extractObservedModelIdentity } from "../evals/host-observation.js"; +import { + listNativeMessages, + listNativeSessions, + nativeModel, + OPENCODE_HOST_VERSION, +} from "../evals/opencode-host.js"; import packageJson from "../package.json" with { type: "json" }; +import { consumePaidDispatch } from "./paid-budget.js"; const REQUEST_TIMEOUT_MS = 120_000; const REVIEWER_STEP_LIMIT = 8; @@ -28,22 +36,18 @@ const SENSITIVE_FIELD_WORDS = new Set([ "token", "tokens", ]); -const MODEL_IDENTITY_PATHS = new Set([ - "[].info.model.modelID", - "[].info.model.providerID", - "[].info.modelID", - "[].info.providerID", -]); +const MODEL_IDENTITY_PATHS = new Set(["[].model.id", "[].model.providerID"]); export const HOST_METADATA_CONTRACT = { - hostVersion: packageJson.devDependencies["@opencode-ai/plugin"], + hostVersion: OPENCODE_HOST_VERSION, endpoints: { - agents: "GET /agent", - createSession: "POST /session", - dispatchReview: "POST /session/:id/command", - parentMessages: "GET /session/:id/message", - childSessions: "GET /session/:id/children", - childMessages: "GET /session/:child_id/message", + serverInfo: "GET /api/info", + agents: "GET /api/agent", + createSession: "POST /api/session", + dispatchReview: "POST /api/session/:id/command", + parentMessages: "GET /api/session/:id/message", + childSessions: "GET /api/session?directory=:directory", + childMessages: "GET /api/session/:child_id/message", }, limits: { requestTimeoutMs: REQUEST_TIMEOUT_MS, @@ -169,7 +173,7 @@ function usage(): string { function parseArgs(argv: readonly string[]): ProbeArgs { let model: string | undefined; let output: string | undefined; - let opencodeVersion = HOST_METADATA_CONTRACT.hostVersion; + let opencodeVersion: string = HOST_METADATA_CONTRACT.hostVersion; let allowLiveCredentials = false; for (let index = 0; index < argv.length; index += 1) { const [flag, value] = [argv[index], argv[index + 1]]; @@ -286,23 +290,24 @@ function completedAssistantMessages(value: unknown): readonly unknown[] { return value.filter( (message) => isRecord(message) && - isRecord(message.info) && - message.info.role === "assistant" && - !("error" in message.info) && - isRecord(message.info.time) && - typeof message.info.time.completed === "number", + message.type === "assistant" && + !("error" in message) && + isRecord(message.time) && + typeof message.time.completed === "number", ); } function modelIdentityFields(messages: unknown): readonly string[] { - return collectFieldObservations(completedAssistantMessages(messages)) + const completed = completedAssistantMessages(messages); + if (extractObservedModelIdentity(completed).kind !== "observed") return []; + return collectFieldObservations(completed) .filter((field) => MODEL_IDENTITY_PATHS.has(field.path)) .map((field) => field.path) .sort(); } function hasModelIdentity(fields: readonly string[]): boolean { - return ["providerID", "modelID"].every((suffix) => + return ["providerID", ".id"].every((suffix) => fields.some((path) => path.endsWith(suffix)), ); } @@ -349,7 +354,7 @@ function hostVersion( attempt: EndpointAttempt | undefined, requested: string, ): HostVersionCapability { - const endpoint = HOST_METADATA_CONTRACT.endpoints.createSession; + const endpoint = HOST_METADATA_CONTRACT.endpoints.serverInfo; if (!attempt || attempt.kind === "endpoint-failure") return unobserved("endpoint-failure", [endpoint]); if (!isRecord(attempt.response)) @@ -400,10 +405,11 @@ export function buildHostEvidenceCapabilities( input: ProbeInput, ): HostEvidenceCapabilities { const parent: ParentLabel = "parent-1"; - const createSession = input.endpointResponses.find( + const serverInfo = input.endpointResponses.find( (attempt) => - attempt.endpoint === HOST_METADATA_CONTRACT.endpoints.createSession, + attempt.endpoint === HOST_METADATA_CONTRACT.endpoints.serverInfo, ); + const { children, malformed } = input.childSessions?.kind === "observed" ? parseChildren(input.childSessions.response) @@ -510,7 +516,7 @@ export function buildHostEvidenceCapabilities( ]); })(); const capabilities = { - hostVersion: hostVersion(createSession, input.opencodeVersion), + hostVersion: hostVersion(serverInfo, input.opencodeVersion), parentManagerModelIdentity, childReviewerModelIdentity, childLineage, @@ -560,27 +566,6 @@ export function buildHostEvidenceCapabilities( }; } -async function fetchJson(url: string): Promise { - const response = await fetch(url, { - signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS), - }); - if (!response.ok) - throw new Error(`GET ${url} failed with ${response.status}.`); - return response.json(); -} - -async function postJson(url: string, body: unknown): Promise { - const response = await fetch(url, { - method: "POST", - headers: { "content-type": "application/json" }, - body: JSON.stringify(body), - signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS), - }); - if (!response.ok) - throw new Error(`POST ${url} failed with ${response.status}.`); - return response.json(); -} - async function observeEndpoint( endpoint: EndpointName, task: () => Promise, @@ -617,14 +602,29 @@ async function runProbe(args: ProbeArgs): Promise { opencodeVersion: args.opencodeVersion, files: { "README.md": "# Metadata probe\n" }, }); - const baseUrl = host.url; + const client = host.client; + const directory = host.project; + const request = { signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS) }; + const serverInfo = await observeEndpoint( + HOST_METADATA_CONTRACT.endpoints.serverInfo, + () => client.server.info(request), + ); const agents = await observeEndpoint( HOST_METADATA_CONTRACT.endpoints.agents, - () => fetchJson(`${baseUrl}/agent`), + () => client.agent.list(undefined, request), ); const createSession = await observeEndpoint( HOST_METADATA_CONTRACT.endpoints.createSession, - () => postJson(`${baseUrl}/session`, { title: "flow metadata probe" }), + () => + client.session.create( + { + title: "flow metadata probe", + agent: "build", + location: { directory }, + model: nativeModel(args.model), + }, + request, + ), ); const parentSessionId = sessionId(createSession); if (!parentSessionId) @@ -632,28 +632,36 @@ async function runProbe(args: ProbeArgs): Promise { opencodeVersion: args.opencodeVersion, generatedAt: new Date().toISOString(), parentSessionId: null, - endpointResponses: [agents, createSession], + endpointResponses: [serverInfo, agents, createSession], childSessions: null, parentMessages: null, childMessages: [], }); const dispatchReview = await observeEndpoint( HOST_METADATA_CONTRACT.endpoints.dispatchReview, - () => - postJson(`${baseUrl}/session/${parentSessionId}/command`, { - command: "flow-review", - arguments: - "Review this tiny project and submit the Flow review result.", - model: args.model, - }), + async () => { + await consumePaidDispatch({ model: args.model, kind: "command" }); + await client.session.command( + { + sessionID: parentSessionId, + name: "flow-review", + text: "Review this tiny project and submit the Flow review result.", + }, + request, + ); + await client.session.wait({ sessionID: parentSessionId }, request); + }, ); const childSessions = await observeEndpoint( HOST_METADATA_CONTRACT.endpoints.childSessions, - () => fetchJson(`${baseUrl}/session/${parentSessionId}/children`), + async () => + (await listNativeSessions(client, directory, request.signal)).filter( + (session) => session.parentID === parentSessionId, + ), ); const parentMessages = await observeEndpoint( HOST_METADATA_CONTRACT.endpoints.parentMessages, - () => fetchJson(`${baseUrl}/session/${parentSessionId}/message`), + () => listNativeMessages(client, parentSessionId, request.signal), ); const children = childSessions.kind === "observed" @@ -668,7 +676,7 @@ async function runProbe(args: ProbeArgs): Promise { sessionId: child.id, messages: await observeEndpoint( HOST_METADATA_CONTRACT.endpoints.childMessages, - () => fetchJson(`${baseUrl}/session/${child.id}/message`), + () => listNativeMessages(client, child.id, request.signal), ), })), ); @@ -676,7 +684,7 @@ async function runProbe(args: ProbeArgs): Promise { opencodeVersion: args.opencodeVersion, generatedAt: new Date().toISOString(), parentSessionId, - endpointResponses: [agents, createSession, dispatchReview], + endpointResponses: [serverInfo, agents, createSession, dispatchReview], childSessions, parentMessages, childMessages, diff --git a/scripts/release-metadata.ts b/scripts/release-metadata.ts index bdc8b1ca..1a5e546a 100644 --- a/scripts/release-metadata.ts +++ b/scripts/release-metadata.ts @@ -33,9 +33,9 @@ import { artifactIdentitySha256, CANARY_CHECKLIST_SHA256, CANARY_CHECKLIST_VERSION, - CANARY_DERIVATION_VERSION, type CanaryFreshness, type CanaryRecord, + canaryDerivationVersion, canaryRecordSha256, parseCanaryRecord, canaryRecordIssue as verifyCanaryRecord, @@ -141,7 +141,7 @@ export function canaryRecordIssue( const entry = record as Partial; if (entry.schemaVersion !== 1 || entry.status !== "passed") return `the canary for ${version} is not a passed v1 record`; - if (entry.derivationVersion !== CANARY_DERIVATION_VERSION) + if (entry.derivationVersion !== canaryDerivationVersion(version)) return `the canary record for ${version} is not evidence-derived`; if (entry.releaseTag !== expectedTag) return `the canary tag ${String(entry.releaseTag)} does not match ${expectedTag}`; diff --git a/scripts/restore-exact-release-artifact.ts b/scripts/restore-exact-release-artifact.ts index ab1c8a2a..7fb94e36 100644 --- a/scripts/restore-exact-release-artifact.ts +++ b/scripts/restore-exact-release-artifact.ts @@ -1,8 +1,39 @@ import { createHash } from "node:crypto"; import { readFile, writeFile } from "node:fs/promises"; -import { unpackedManifestSha256 } from "../evals/provenance.js"; +import { join } from "node:path"; +import { + exactPackageVersion, + unpackedManifestSha256, +} from "../evals/provenance.js"; +import { readQualificationBundle } from "../evals/qualification-bundle.js"; -const [rebuiltPath, sealedPath, expectedSha256] = process.argv.slice(2); +let [rebuiltPath, sealedPath, expectedSha256, version, ...extra] = + process.argv.slice(2); +if (sealedPath === "--bundle") { + if (!expectedSha256 || !version || extra.length) + throw new Error( + "Expected rebuilt tarball, --bundle, bundle directory, and version.", + ); + const directory = expectedSha256; + const bundle = await readQualificationBundle(directory); + if ( + bundle.manifest.packageVersion !== exactPackageVersion(version) || + bundle.manifest.verdict !== "VERIFIED" + ) + throw new Error( + "Sealed qualification does not verify the requested version.", + ); + const artifacts = bundle.files.filter((file) => file.ref.role === "artifact"); + const artifact = artifacts[0]; + if (artifacts.length !== 1 || !artifact) + throw new Error("Sealed qualification requires exactly one artifact."); + sealedPath = join(directory, artifact.ref.object); + expectedSha256 = artifact.ref.sha256; +} else if (version || extra.length) { + throw new Error( + "Expected rebuilt tarball, sealed bundle object, and SHA-256.", + ); +} if ( !rebuiltPath || !sealedPath || diff --git a/skills/flow-run/SKILL.md b/skills/flow-run/SKILL.md index 8fa3cd9a..8e3418af 100644 --- a/skills/flow-run/SKILL.md +++ b/skills/flow-run/SKILL.md @@ -100,7 +100,7 @@ releases unless asked separately. Work serially. After a feature run is active, dispatch `flow-worker` only for two or three genuinely independent slices with clear benefit. Workers call no -Flow tools, spawn no children, and run no Bash. Integrate and inspect the +Flow tools, spawn no children, and run no Shell. Integrate and inspect the combined diff before validation. ## Validate diff --git a/src/application/model-preferences-schema.ts b/src/application/model-preferences-schema.ts new file mode 100644 index 00000000..b9b480a5 --- /dev/null +++ b/src/application/model-preferences-schema.ts @@ -0,0 +1,61 @@ +import { z } from "zod"; + +const pickerChoiceSchema = z.discriminatedUnion("kind", [ + z.strictObject({ kind: z.literal("default") }), + z.strictObject({ + kind: z.literal("model"), + model: z.strictObject({ + providerID: z.string().min(1), + id: z.string().min(1), + }), + }), +]); +const roleChoicesSchema = z.strictObject({ + planning: pickerChoiceSchema, + review: pickerChoiceSchema, +}); +export const projectPreferencesSchema = roleChoicesSchema.partial(); +export const storedPreferencesSchema = z.strictObject({ + schemaVersion: z.literal(1), + revision: z.number().int().nonnegative().max(Number.MAX_SAFE_INTEGER), + roles: roleChoicesSchema, +}); +const effectiveSchema = z.strictObject({ + choice: pickerChoiceSchema, + source: z.enum(["project", "global-picker", "role-default"]), +}); +export const pickerSnapshotSchema = z.strictObject({ + location: z.object({ + directory: z.string(), + workspaceID: z.string().optional(), + }), + revision: z.number().int().nonnegative(), + global: roleChoicesSchema, + project: projectPreferencesSchema, + effective: z.strictObject({ + planning: effectiveSchema, + review: effectiveSchema, + }), +}); +export const savePreferenceSchema = z.strictObject({ + role: z.enum(["planning", "review"]), + expectedRevision: z.number().int().nonnegative(), + expectedProject: projectPreferencesSchema, + choice: pickerChoiceSchema, +}); +export const savePreferenceResultSchema = z.union([ + z.strictObject({ + kind: z.enum(["saved", "unchanged"]), + snapshot: pickerSnapshotSchema, + }), + z.strictObject({ + kind: z.literal("refused"), + reason: z.enum([ + "busy", + "stale-preference", + "stale-project", + "unavailable-model", + "unloaded", + ]), + }), +]); diff --git a/src/application/model-preferences.ts b/src/application/model-preferences.ts new file mode 100644 index 00000000..420ba8ba --- /dev/null +++ b/src/application/model-preferences.ts @@ -0,0 +1,42 @@ +export type FlowModelRole = "planning" | "review"; +type PickerModel = Readonly<{ providerID: string; id: string }>; +export type PickerChoice = + | Readonly<{ kind: "default" }> + | Readonly<{ kind: "model"; model: PickerModel }>; +export type RoleChoices = Readonly>; +export type ProjectPreferences = Readonly< + Partial> +>; +export type StoredModelPreferences = Readonly<{ + schemaVersion: 1; + revision: number; + roles: RoleChoices; +}>; +export type EffectivePreference = Readonly<{ + choice: PickerChoice; + source: "project" | "global-picker" | "role-default"; +}>; +export type PickerSnapshot = Readonly<{ + location: Readonly<{ directory: string; workspaceID?: string | undefined }>; + revision: number; + global: RoleChoices; + project: ProjectPreferences; + effective: Readonly>; +}>; +export type SaveModelPreference = Readonly<{ + role: FlowModelRole; + expectedRevision: number; + expectedProject: ProjectPreferences; + choice: PickerChoice; +}>; +export type SaveModelPreferenceResult = + | Readonly<{ kind: "saved" | "unchanged"; snapshot: PickerSnapshot }> + | Readonly<{ + kind: "refused"; + reason: + | "busy" + | "stale-preference" + | "stale-project" + | "unavailable-model" + | "unloaded"; + }>; diff --git a/src/application/recovery-policy.ts b/src/application/recovery-policy.ts index 9f07632b..12dafc86 100644 --- a/src/application/recovery-policy.ts +++ b/src/application/recovery-policy.ts @@ -300,21 +300,22 @@ export class RecoveryController { host.ordinaryAutoMessage = id; if (authority && (!synthetic || trustedAutoContinuation)) { if (authority.parent !== null && authority.parent !== id) { - const lease = this.#lease; - if ( - lease?.authority === authority && - (lease.inFlight || lease.pending) - ) { - lease.controller.abort(); - lease.controller = new AbortController(); - lease.pending = null; - lease.last = { kind: "cancelled" }; - } + this.invalidateOrigins(hostId); if (!synthetic) authority.direction = id; } authority.parent = id; } } + invalidateOrigins(hostId: string): void { + this.#hosts.get(hostId)?.parents.clear(); + const lease = this.#lease; + if (lease?.authority.host === hostId && (lease.inFlight || lease.pending)) { + lease.controller.abort(); + lease.controller = new AbortController(); + lease.pending = null; + lease.last = { kind: "cancelled" }; + } + } observeAssistant(hostId: string, id: string, parent: string): void { const host = this.#hosts.get(hostId); if (!host) return; @@ -483,7 +484,6 @@ export class RecoveryController { session: Session, request: SessionCloseRequest, ): void { - this.#expireLease(); this.#checkProtectedSession(context, session); const authority = this.#origin(context); if (!authority) return; diff --git a/src/config-shared.ts b/src/config-shared.ts index c86a380a..9ee82c1d 100644 --- a/src/config-shared.ts +++ b/src/config-shared.ts @@ -1,23 +1,19 @@ import { compileFlowPromptSurface } from "./prompt-surfaces.js"; -type FlowPermissionConfig = { - edit?: string | Record; - bash?: string | Record; - skill?: string | Record; - task?: Record; - [toolPattern: string]: string | Record | undefined; -}; - +type FlowPermissionRule = Readonly<{ + action: string; + resource: string; + effect: "allow" | "deny" | "ask"; +}>; type FlowAgentConfig = { mode: "subagent"; description: string; - prompt: string; - hidden?: boolean; - disable?: boolean; + system: string; + hidden: boolean; model?: string; variant?: string; steps?: number; - permission?: FlowPermissionConfig; + permissions: FlowPermissionRule[]; }; type CommandBase = { description: string; template: string }; @@ -25,11 +21,6 @@ type FlowCommandConfig = | (CommandBase & { subtask: false; agent?: never }) | (CommandBase & { subtask: true; agent: string }); -export type MutableFlowConfig = { - agent?: Record; - command?: Record; -}; - type FlowEnvironment = Readonly>; type ReviewerSettingSource = "plugin-option" | "environment" | "picker"; @@ -91,63 +82,52 @@ type FlowReviewerStatus = Readonly<{ report: readonly string[]; }>; +const READ_ONLY_RULES: FlowPermissionRule[] = [ + { action: "*", resource: "*", effect: "deny" }, + { action: "read", resource: "*", effect: "allow" }, + { action: "glob", resource: "*", effect: "allow" }, + { action: "grep", resource: "*", effect: "allow" }, +]; export const FLOW_CORE_AGENTS = { "flow-planner": { mode: "subagent", hidden: true, description: "Optional read-only planning specialist. Returns advisory draft analysis to the manager.", - prompt: compileFlowPromptSurface("flow-planner"), - permission: { - "*": "deny", - read: "allow", - glob: "allow", - grep: "allow", - edit: "deny", - bash: "deny", - external_directory: "deny", - skill: "deny", - task: { "*": "deny" }, - "flow_*": "deny", - }, + system: compileFlowPromptSurface("flow-planner"), + permissions: [...READ_ONLY_RULES], }, "flow-reviewer": { mode: "subagent", hidden: true, description: "Independent workspace-read-only reviewer that submits one Flow result.", - prompt: compileFlowPromptSurface("flow-reviewer"), - permission: { - edit: "deny", - bash: "deny", - external_directory: "deny", - skill: "deny", - task: { "*": "deny" }, - "flow_*": "deny", - flow_status: "allow", - flow_feature_complete: "allow", - }, + system: compileFlowPromptSurface("flow-reviewer"), + permissions: [ + ...READ_ONLY_RULES, + { action: "flow_status", resource: "*", effect: "allow" }, + { action: "flow_feature_complete", resource: "*", effect: "allow" }, + ], }, "flow-worker": { mode: "subagent", hidden: true, description: "Bounded worker for one read-only evidence or exact-scope implementation slice.", - prompt: compileFlowPromptSurface("flow-worker"), - permission: { - edit: { - "*": "allow", - ".flow": "deny", - ".flow/**": "deny", - ".git": "deny", - ".git/**": "deny", - }, - bash: "deny", - external_directory: "deny", - skill: "deny", - task: { "*": "deny" }, - "flow_*": "deny", - }, + system: compileFlowPromptSurface("flow-worker"), + permissions: [ + ...READ_ONLY_RULES, + { action: "edit", resource: "*", effect: "allow" }, + ...[".flow", ".git"].flatMap((name) => + [name, `${name}/**`, `**/${name}`, `**/${name}/**`].map( + (resource): FlowPermissionRule => ({ + action: "edit", + resource, + effect: "deny", + }), + ), + ), + ], }, } satisfies Record; @@ -388,10 +368,9 @@ export function createFlowCoreConfigEntries(options?: { agent: { "flow-planner": { ...FLOW_CORE_AGENTS["flow-planner"], - disable: !options?.planningModel, ...(options?.planningModel ? { model: options.planningModel } : {}), - permission: structuredClone( - FLOW_CORE_AGENTS["flow-planner"].permission, + permissions: structuredClone( + FLOW_CORE_AGENTS["flow-planner"].permissions, ), }, "flow-reviewer": { @@ -399,13 +378,15 @@ export function createFlowCoreConfigEntries(options?: { ...(model ? { model } : {}), ...(reviewer.variant ? { variant: reviewer.variant.value } : {}), ...(steps ? { steps } : {}), - permission: structuredClone( - FLOW_CORE_AGENTS["flow-reviewer"].permission, + permissions: structuredClone( + FLOW_CORE_AGENTS["flow-reviewer"].permissions, ), }, "flow-worker": { ...FLOW_CORE_AGENTS["flow-worker"], - permission: structuredClone(FLOW_CORE_AGENTS["flow-worker"].permission), + permissions: structuredClone( + FLOW_CORE_AGENTS["flow-worker"].permissions, + ), }, }, command: Object.fromEntries( @@ -417,38 +398,6 @@ export function createFlowCoreConfigEntries(options?: { }; } -export function applyFlowConfig( - config: MutableFlowConfig, - options?: { - pluginOptions?: FlowPluginOptions | undefined; - reviewerConfiguration?: FlowReviewerConfiguration | undefined; - planningModel?: string | undefined; - onCollision?: (kind: "agent" | "command", name: string) => void; - onWarning?: (warning: string) => void; - onNotice?: (notice: string) => void; - }, -): void { - const entries = createFlowCoreConfigEntries({ - planningModel: options?.planningModel, - ...(options?.onWarning ? { onWarning: options.onWarning } : {}), - ...(options?.onNotice ? { onNotice: options.onNotice } : {}), - ...(options?.pluginOptions ? { pluginOptions: options.pluginOptions } : {}), - ...(options?.reviewerConfiguration - ? { reviewerConfiguration: options.reviewerConfiguration } - : {}), - }); - for (const name of Object.keys(entries.agent)) { - if (config.agent && name in config.agent) - options?.onCollision?.("agent", name); - } - for (const name of Object.keys(entries.command)) { - if (config.command && name in config.command) - options?.onCollision?.("command", name); - } - config.agent = { ...(config.agent ?? {}), ...entries.agent }; - config.command = { ...(config.command ?? {}), ...entries.command }; -} - export type FlowCodingModel = Readonly<{ providerID: string; modelID: string; diff --git a/src/flow-rpc.ts b/src/flow-rpc.ts new file mode 100644 index 00000000..22b82779 --- /dev/null +++ b/src/flow-rpc.ts @@ -0,0 +1,22 @@ +import { Rpc } from "@opencode/plugin/rpc"; +import { z } from "zod"; +import { + pickerSnapshotSchema, + savePreferenceResultSchema, + savePreferenceSchema, +} from "./application/model-preferences-schema.js"; + +export const FlowModelPreferencesRpc = Rpc.define({ + id: "flow.model-preferences", + methods: { + read: { + input: { type: "object", properties: {}, additionalProperties: false }, + output: z.toJSONSchema(pickerSnapshotSchema), + }, + set: { + input: z.toJSONSchema(savePreferenceSchema), + output: z.toJSONSchema(savePreferenceResultSchema), + }, + }, + events: {}, +}); diff --git a/src/infrastructure/fs/model-preference-store.ts b/src/infrastructure/fs/model-preference-store.ts new file mode 100644 index 00000000..432b528b --- /dev/null +++ b/src/infrastructure/fs/model-preference-store.ts @@ -0,0 +1,241 @@ +import { randomUUID } from "node:crypto"; +import { watch } from "node:fs"; +import { + lstat, + mkdir, + open, + readdir, + readFile, + realpath, + rename, + rmdir, + unlink, +} from "node:fs/promises"; +import { homedir } from "node:os"; +import { basename, dirname, isAbsolute, join } from "node:path"; +import { setTimeout as sleep } from "node:timers/promises"; +import { z } from "zod"; +import type { StoredModelPreferences } from "../../application/model-preferences.js"; +import { storedPreferencesSchema } from "../../application/model-preferences-schema.js"; + +const EMPTY_MODEL_PREFERENCES: StoredModelPreferences = { + schemaVersion: 1, + revision: 0, + roles: { planning: { kind: "default" }, review: { kind: "default" } }, +}; +const ownerSchema = z.strictObject({ + token: z.string().uuid(), + pid: z.number().int().positive(), +}); +function hasCode(error: unknown, code: string) { + return error instanceof Error && "code" in error && error.code === code; +} +function dead(pid: number) { + try { + process.kill(pid, 0); + return false; + } catch (error) { + return hasCode(error, "ESRCH"); + } +} + +export function modelPreferencePath(env: NodeJS.ProcessEnv = process.env) { + const state = env.XDG_STATE_HOME; + return join( + state && isAbsolute(state) + ? state + : join( + env.HOME && isAbsolute(env.HOME) ? env.HOME : homedir(), + ".local", + "state", + ), + "opencode-plugin-flow", + "model-preferences.json", + ); +} + +interface ModelPreferenceStore { + read(): Promise; + update( + mutation: (current: StoredModelPreferences) => Promise<{ + value: T; + next?: StoredModelPreferences; + beforeCommit?: () => void; + }>, + ): Promise; + watch(onChange: () => void, onError: (error: Error) => void): () => void; +} + +export async function createModelPreferenceStore( + file = modelPreferencePath(), + options: { lockTimeoutMs?: number } = {}, +): Promise { + await mkdir(dirname(file), { recursive: true, mode: 0o700 }); + const directory = await realpath(dirname(file)); + const target = join(directory, basename(file)); + const lock = join(directory, "model-preferences.lock"); + const read = async () => { + try { + if (!(await lstat(target)).isFile()) + throw new Error("Flow model preferences must be a regular file."); + return storedPreferencesSchema.parse( + JSON.parse(await readFile(target, "utf8")), + ); + } catch (error) { + if (hasCode(error, "ENOENT")) + return structuredClone(EMPTY_MODEL_PREFERENCES); + throw error; + } + }; + const removeOwner = async (name: string) => { + try { + await unlink(join(lock, name)); + } catch (error) { + if (hasCode(error, "ENOENT")) return; + throw error; + } + try { + await rmdir(lock); + } catch (error) { + if ( + !hasCode(error, "ENOENT") && + !hasCode(error, "ENOTEMPTY") && + !hasCode(error, "EEXIST") + ) + throw error; + } + }; + const reclaim = async () => { + try { + if (!(await lstat(lock)).isDirectory()) + throw new Error("Flow model preference lock must be a directory."); + const entries = await readdir(lock); + if (!entries.length) { + try { + await rmdir(lock); + } catch (error) { + if ( + !hasCode(error, "ENOENT") && + !hasCode(error, "ENOTEMPTY") && + !hasCode(error, "EEXIST") + ) + throw error; + } + return; + } + if (entries.length !== 1 || !entries[0]?.startsWith("owner-")) return; + const name = entries[0]; + const owner = ownerSchema.parse( + JSON.parse(await readFile(join(lock, name), "utf8")), + ); + if (name === `owner-${owner.token}.json` && dead(owner.pid)) + await removeOwner(name); + } catch (error) { + if (!hasCode(error, "ENOENT")) throw error; + } + }; + const acquire = async () => { + const token = randomUUID(); + const temporary = join(directory, `.preferences-lock-${token}`); + const name = `owner-${token}.json`; + await mkdir(temporary, { mode: 0o700 }); + await writeNewFile( + join(temporary, name), + JSON.stringify({ token, pid: process.pid }), + ); + const started = Date.now(); + try { + while (true) { + try { + await rename(temporary, lock); + return () => removeOwner(name); + } catch (error) { + if (!hasCode(error, "ENOTEMPTY") && !hasCode(error, "EEXIST")) + throw error; + } + await reclaim(); + if (Date.now() - started >= (options.lockTimeoutMs ?? 5_000)) + throw new Error( + "Timed out waiting for the Flow model preference lock.", + ); + await sleep(10); + } + } finally { + await removeCandidate(temporary, name); + } + }; + return { + read, + async update(mutation) { + const release = await acquire(); + try { + const current = await read(); + const { value, next, beforeCommit } = await mutation(current); + if (next) { + storedPreferencesSchema.parse(next); + if (next.revision !== current.revision + 1) + throw new Error( + "Flow model preference revision must advance once.", + ); + const temporary = join( + directory, + `.model-preferences-${randomUUID()}.json`, + ); + try { + await writeNewFile(temporary, `${JSON.stringify(next)}\n`); + beforeCommit?.(); + await rename(temporary, target); + const parent = await open(directory, "r"); + try { + await parent.sync(); + } finally { + await parent.close(); + } + } finally { + await removeTemporaryFile(temporary); + } + } + return value; + } finally { + await release(); + } + }, + watch(onChange, onError) { + const watcher = watch( + directory, + { persistent: false }, + (_event, name) => { + if (name === null || name.toString() === basename(file)) onChange(); + }, + ); + watcher.on("error", onError); + return () => watcher.close(); + }, + }; +} + +async function writeNewFile(path: string, text: string) { + const handle = await open(path, "wx", 0o600); + try { + await handle.writeFile(text, "utf8"); + await handle.sync(); + } finally { + await handle.close(); + } +} + +async function removeTemporaryFile(path: string) { + try { + await unlink(path); + } catch (error) { + if (!hasCode(error, "ENOENT")) throw error; + } +} +async function removeCandidate(path: string, owner: string) { + try { + await unlink(join(path, owner)); + await rmdir(path); + } catch (error) { + if (!hasCode(error, "ENOENT")) throw error; + } +} diff --git a/src/platform/opencode/auto-compaction.ts b/src/platform/opencode/auto-compaction.ts index 3bca79aa..a7ef6741 100644 --- a/src/platform/opencode/auto-compaction.ts +++ b/src/platform/opencode/auto-compaction.ts @@ -1,96 +1,266 @@ -export type HostMessage = Readonly<{ - id: string; - role: string; - parentID?: string; - summary?: unknown; -}>; +import { isDeepStrictEqual as same } from "node:util"; +import type { SessionMessageInfo } from "@opencode/client"; +import type { + SessionCompaction, + SessionContext, +} from "@opencode/plugin/promise/session"; +import type { AutoDriveDelivery } from "./auto-drive.js"; +import { + type InputOrigin, + modelInputIDs, + nativeDelivery, + nativeInputIDs, + sameDelivery, +} from "./execution-attribution.js"; +import type { InputAdmissions, InputScope } from "./input-admission.js"; -export type HostPart = Readonly<{ - type: string; - messageID: string; - auto?: boolean; - synthetic?: boolean; - metadata?: Readonly>; +type Native = readonly SessionMessageInfo[]; +type ControlProof = Readonly<{ + id: string; + reason: "auto" | "manual"; + consumed: readonly string[]; + delivery: AutoDriveDelivery; }>; - -type Anchor = Readonly<{ authority: string; user: string }>; -type Summary = Anchor & Readonly<{ summary: string }>; -export type Compaction = - | (Anchor & Readonly<{ kind: "awaiting-summary" }>) - | (Summary & Readonly<{ kind: "awaiting-user" }>) - | (Summary & Readonly<{ kind: "awaiting-marker"; candidate: string }>) - | (Summary & Readonly<{ kind: "authenticated"; successor: string }>); - -export function observeCompactionMessage( - chain: Compaction | null, - authority: string | null, - message: HostMessage, -): Compaction | null { - if (!chain) return null; - if (message.role === "assistant") { - if ( - message.summary !== true || - message.parentID !== chain.user || - authority !== chain.authority - ) - return null; - if (chain.kind !== "awaiting-summary") - return chain.summary === message.id ? chain : null; - return { ...chain, kind: "awaiting-user", summary: message.id }; - } - if (message.role !== "user" || message.summary !== undefined) return chain; - if (message.id === chain.user || message.id === chain.authority) return chain; - switch (chain.kind) { - case "awaiting-summary": - return null; - case "awaiting-user": - return { ...chain, kind: "awaiting-marker", candidate: message.id }; - case "awaiting-marker": - return chain.candidate === message.id ? chain : null; - case "authenticated": - return chain.successor === message.id ? chain : null; - } -} - -export function observeCompactionPart( - chain: Compaction | null, - authority: string | null, - lastAssistantParent: string | null, - part: HostPart, -): Compaction | null { - if (part.type === "compaction") { - if (part.auto !== true || !authority || lastAssistantParent !== authority) - return null; - if (chain?.user === part.messageID && chain.authority === authority) - return chain; - return { kind: "awaiting-summary", user: part.messageID, authority }; - } - if (part.metadata?.compaction_continue !== true) return chain; +type CheckedContinuation = ControlProof & + Readonly<{ + kind: "continuation"; + source: string; + sourceOrigin: InputOrigin; + origin: InputOrigin; + }>; +type CheckedSummary = ControlProof & + Readonly<{ kind: "summary-only"; reason: "manual" }>; +type CompactionResult = CheckedContinuation | CheckedSummary; +function running( + input: SessionCompaction, + native: Native, +): Readonly<{ proof: ControlProof; prefix: Native }> { + const position = native.findLastIndex( + (message) => message.type === "compaction", + ); + const control = native[position]; + const reason: unknown = + control?.type === "compaction" ? control.reason : undefined; if ( - part.type !== "text" || - part.synthetic !== true || - !chain || - authority !== chain.authority + control?.type !== "compaction" || + control.status !== "running" || + (reason !== "auto" && reason !== "manual") ) - return null; - if (chain.kind === "authenticated") - return chain.successor === part.messageID ? chain : null; - if (chain.kind !== "awaiting-marker" || chain.candidate !== part.messageID) - return null; + throw new Error( + "Flow cannot retain authority through unverified or manual compaction.", + ); + const consumed = modelInputIDs(input), + prefix = native.slice(0, position); + if ( + !same(nativeInputIDs(native), consumed) || + !same(nativeInputIDs(prefix), consumed) + ) + throw new Error( + "Flow compaction inputs differ from the native control prefix.", + ); return { - kind: "authenticated", - authority: chain.authority, - user: chain.user, - summary: chain.summary, - successor: part.messageID, + proof: Object.freeze({ + id: control.id, + reason, + consumed: Object.freeze(consumed), + delivery: nativeDelivery(input), + }), + prefix, }; } - -export function authenticatedCompactionSuccessor( - chain: Compaction | null, - authority: string, -): string | null { - return chain?.kind === "authenticated" && chain.authority === authority - ? chain.successor - : null; +type CompactionOptions = Readonly<{ + inputs: InputAdmissions; + readNative(host: string): Promise; + failure(scope: InputScope): void; + hasActivePrimary(host: string): boolean; + guidance(host: string): string | null; + observe(host: string, origin: InputOrigin, source: string, id: string): void; +}>; +export class CompactionBoundary { + readonly #records = new Map(); + readonly #pending = new Map(); + readonly #options: CompactionOptions; + constructor(options: CompactionOptions) { + this.#options = options; + } + #key(host: string, id: string) { + return JSON.stringify([host, id]); + } + async admit(input: SessionCompaction): Promise { + const scope = this.#options.inputs.capture(input); + const prior = this.#pending.get(scope.host); + if (prior?.current()) { + scope.release(); + throw new Error("Flow compaction callbacks overlap."); + } + this.#pending.set(scope.host, scope); + try { + const first = running( + input, + await scope.wait(this.#options.readNative(scope.host)), + ); + await scope.ready(); + const second = running( + input, + await scope.wait(this.#options.readNative(scope.host)), + ); + if (!same(first.proof, second.proof)) + throw new Error("Flow automatic compaction control identity changed."); + const evidence = await this.#options.inputs.evidence( + scope, + second.prefix, + second.proof.delivery, + ); + const final = running( + input, + await scope.wait(this.#options.readNative(scope.host)), + ); + if (!same(first.proof, final.proof)) + throw new Error("Flow automatic compaction control identity changed."); + let result: CompactionResult; + if (evidence.kind === "live") { + const origin = this.completed(input, final.prefix) ?? evidence.origin; + this.#options.inputs.assertLive(origin); + if (!sameDelivery(origin.delivery, final.proof.delivery)) + throw new Error( + "Flow compaction delivery differs from its accepted input.", + ); + if (final.proof.reason === "manual" && origin.authority) + throw new Error( + "Flow cannot retain authority through unverified or manual compaction.", + ); + const existing = this.#records.get( + this.#key(scope.host, final.proof.id), + ); + if (existing) { + if ( + existing.kind !== "continuation" || + existing.sourceOrigin !== origin || + !same( + { ...first.proof }, + { + id: existing.id, + reason: existing.reason, + consumed: existing.consumed, + delivery: existing.delivery, + }, + ) + ) + throw new Error( + "Flow automatic compaction control identity changed.", + ); + return existing; + } + const identity = { ...origin, id: final.proof.id }; + const checkpoint: InputOrigin = origin.authority + ? Object.freeze({ + ...identity, + automatic: true, + authority: origin.authority, + }) + : Object.freeze({ ...identity, automatic: false, authority: null }); + result = Object.freeze({ + ...final.proof, + kind: "continuation", + source: scope.source, + sourceOrigin: origin, + origin: checkpoint, + }); + } else { + if ( + evidence.kind === "retired-automatic" || + evidence.kind === "rejected" + ) + throw new Error("Flow execution epoch was revoked."); + if (final.proof.reason !== "manual") + throw new Error( + evidence.kind === "unknown-history" + ? "Flow compaction has no accepted input origin." + : "Flow execution epoch was revoked.", + ); + if ( + this.#options.guidance(scope.host) || + this.#options.hasActivePrimary(scope.host) + ) + throw new Error( + "Flow manual summary cannot overlap a live execution.", + ); + result = Object.freeze({ + ...final.proof, + reason: "manual", + kind: "summary-only", + }); + const existing = this.#records.get( + this.#key(scope.host, final.proof.id), + ); + if (existing) { + if (!same(existing, result)) + throw new Error( + "Flow automatic compaction control identity changed.", + ); + return existing; + } + } + scope.assertCurrent(); + this.#options.inputs.checkpoint( + scope, + result.id, + result.kind === "continuation" ? result.origin : null, + ); + this.#records.set(this.#key(scope.host, result.id), result); + return result; + } catch (error) { + this.#options.failure(scope); + throw error; + } finally { + if (this.#pending.get(scope.host) === scope) + this.#pending.delete(scope.host); + scope.release(); + } + } + completed( + input: Pick, + native: Native, + ): InputOrigin | undefined { + const consumed = modelInputIDs(input), + source = consumed.at(-1); + if (!source) return; + const record = this.#records.get(this.#key(input.sessionID, source)); + if (!record) return; + if (record.kind === "summary-only") + throw new Error( + "Flow context has an unknown, synthetic, or stale real input.", + ); + const control = native.findLast((message) => message.type === "compaction"); + if ( + control?.type !== "compaction" || + control.id !== record.id || + control.reason !== record.reason || + control.status !== "completed" || + !same(consumed, [record.id]) + ) + throw new Error("Flow automatic compaction control identity changed."); + if ( + native + .slice(native.indexOf(control) + 1) + .some( + (message) => message.type === "user" || message.type === "synthetic", + ) + ) + throw new Error( + "Flow automatic compaction was interrupted by new input.", + ); + if (!sameDelivery(record.delivery, nativeDelivery(input))) + throw new Error( + "Flow admitted agent or model differs from its primary request.", + ); + this.#options.inputs.assertLive(record.origin); + this.#options.observe( + input.sessionID, + record.origin, + record.source, + record.id, + ); + return record.origin; + } } diff --git a/src/platform/opencode/auto-drive-messages.ts b/src/platform/opencode/auto-drive-messages.ts index f2d69242..7c243c8d 100644 --- a/src/platform/opencode/auto-drive-messages.ts +++ b/src/platform/opencode/auto-drive-messages.ts @@ -18,3 +18,21 @@ export function autoHandbackMessage(projection: AutoDriveProjection): string { ].join(" "); return `${handback}\n\n${FLOW_MANAGER_KERNEL}`; } + +export const CONTINUATION_ROUTE = [ + "Load flow-run guidance before any feature or closure route;", + "for a fresh close use compact session id/revision plus a fresh operation id,", + "and replay archiveRetry exactly from its projected request.", +].join(" "); + +export function autoCompactionMessage(): string { + const context = [ + "An in-memory /flow-auto continuation remains active.", + "Read compact Flow state first.", + CONTINUATION_ROUTE, + "Do not expand the approved goal; stop for required user direction, a hard blocker, or confirmed closure.", + ].join(" "); + return `${context}\n\n${FLOW_MANAGER_KERNEL}`; +} + +export const AUTO_INITIAL_PROMPT = `Read compact status, load flow-plan, then call flow_plan_save.\n\n${FLOW_MANAGER_KERNEL}`; diff --git a/src/platform/opencode/auto-drive.ts b/src/platform/opencode/auto-drive.ts index afd587e9..44b4afde 100644 --- a/src/platform/opencode/auto-drive.ts +++ b/src/platform/opencode/auto-drive.ts @@ -2,21 +2,18 @@ import type { PreparedValidation } from "../../application/prepare-validation.js import type { RecoveryController } from "../../application/recovery-policy.js"; import type { SourceDigest } from "../../domain/session.js"; import { FLOW_MANAGER_KERNEL } from "../../guidance/catalog.js"; -import { - authenticatedCompactionSuccessor, - type Compaction, - type HostMessage, - type HostPart, - observeCompactionMessage, - observeCompactionPart, -} from "./auto-compaction.js"; import { decideOnIdle, type IdleDecision, isHandback, isPendingReviewer, } from "./auto-drive-decision.js"; -import { autoHandbackMessage } from "./auto-drive-messages.js"; +import { + AUTO_INITIAL_PROMPT, + autoCompactionMessage, + autoHandbackMessage, + CONTINUATION_ROUTE, +} from "./auto-drive-messages.js"; import { type AutoValidationOrigin, type AutoValidationReceipt, @@ -87,13 +84,17 @@ interface AutoDriveMessagePart { readonly synthetic?: boolean; readonly metadata?: Readonly>; } +export type AutoDriveAuthority = Readonly<{ + hostSessionId: string; + token: string; + epoch: number; +}>; export interface AutoDriveDelivery { readonly agent: string; readonly model: Readonly<{ providerID: string; modelID: string }>; readonly variant?: string | undefined; } -/** OpenCode stores the resolved variant inside the model but accepts it beside it. */ export function autoDriveDelivery( message: Readonly<{ agent: string; @@ -145,9 +146,7 @@ type Lease = { inFlight: "status" | "reply-status" | "prompt" | null; idlePending: boolean; messageId: string | null; - assistantParents: Map; - lastAssistantParent: string | null; - compaction: Compaction | null; + assistantOrigins: Map; manualContinuation: { revision: number } | null; validation: { revision: number; @@ -183,13 +182,6 @@ type AutoDriveOptions = Readonly<{ now?: (() => number) | undefined; }>; const STOP = /^(?:(?:stop|cancel) \/flow-auto|\/flow-auto (?:stop|cancel))$/i; -const INITIAL_ROUTE = - "Read compact status, load flow-plan, then call flow_plan_save."; -const CONTINUATION_ROUTE = [ - "Load flow-run guidance before any feature or closure route;", - "for a fresh close use compact session id/revision plus a fresh operation id,", - "and replay archiveRetry exactly from its projected request.", -].join(" "); function inspectMessage(parts: readonly AutoDriveMessagePart[]) { let token: string | null = null; @@ -215,10 +207,8 @@ export class AutoDriveCoordinator { readonly #options: AutoDriveOptions; #repairSession: string | undefined; readonly #repairHandoffs = new Set(); - /** Whether this host has ever reported assistant message parentage. */ - #hostParentage = false; - /** Whether any assistant message has arrived without a parent. */ - #hostMissingParentage = false; + #epoch = 0; + #authorityReady = false; constructor(options: AutoDriveOptions) { this.#options = options; } @@ -261,10 +251,8 @@ export class AutoDriveCoordinator { #rejectOrigin(lease: Lease, kind: "compaction" | "mutation"): void { this.#stop( lease, - this.#hostParentage - ? `Flow: ${kind} origin was unavailable.` - : "Flow: this host reports no assistant message parentage, so /flow-auto cannot continue automatically. Drive each feature with /flow-run.", - this.#hostParentage ? "missing-lineage" : "unsupported", + `Flow: ${kind} origin was unavailable.`, + "missing-lineage", ); } #waitAt(lease: Lease, revision: number): void { @@ -283,6 +271,7 @@ export class AutoDriveCoordinator { if (!isHandback(projection)) return; if (lease.handbackPromptedRevision === projection.revision) return; if (!lease.delivery) return; + const epoch = this.#epoch; const token = lease.token; lease.handbackPromptedRevision = projection.revision; lease.messageId = null; @@ -296,7 +285,7 @@ export class AutoDriveCoordinator { { [FLOW_AUTO_METADATA_KEY]: token }, ); } catch (error) { - if (lease.token !== token) return; + if (lease.token !== token || this.#epoch !== epoch) return; this.#stop( lease, `Flow auto prompt failed: ${String(error)}`, @@ -305,9 +294,12 @@ export class AutoDriveCoordinator { } } async #read(lease: Lease): Promise { + const epoch = this.#epoch; try { - return await this.#options.readProjection(); + const projection = await this.#options.readProjection(); + return this.#epoch === epoch ? projection : null; } catch (error) { + if (this.#epoch !== epoch) return null; this.#stop( lease, `Flow auto status failed: ${String(error)}`, @@ -316,15 +308,21 @@ export class AutoDriveCoordinator { return null; } } + createInvocationToken(): string { + return this.#options.createToken?.() ?? crypto.randomUUID(); + } async activate( hostSessionId: string, requestGoal = "", options: Readonly<{ + token?: string; newlyAnchoredSessionId?: string | null; intent?: AutoGoalIntent; }> = {}, ): Promise> { - const token = this.#options.createToken?.() ?? crypto.randomUUID(); + this.#epoch++; + this.#authorityReady = true; + const token = options.token ?? this.createInvocationToken(); this.#disposition = { state: "active", reason: "invocation" }; this.#dispositionHost = hostSessionId; this.#timing = { @@ -348,18 +346,16 @@ export class AutoDriveCoordinator { inFlight: null, idlePending: false, messageId: null, - assistantParents: new Map(), - lastAssistantParent: null, - compaction: null, + assistantOrigins: new Map(), manualContinuation: null, validation: null, ownInspectDraft: null, }; const lease = this.#lease; const baseline = await this.#read(lease); + if (this.#lease !== lease) throw new Error("Flow auto-drive superseded."); if (!baseline) throw new Error("Flow auto-drive compact status failed."); lease.baseline = baseline; - if (this.#lease !== lease) throw new Error("Flow auto-drive superseded."); if (!isPendingReviewer(baseline)) lease.checkpoint = { revision: baseline.revision, answered: false }; return { [FLOW_AUTO_METADATA_KEY]: token }; @@ -377,11 +373,7 @@ export class AutoDriveCoordinator { } async resumeForCommand(hostSessionId: string): Promise { const lease = this.#lease; - if ( - lease?.hostSessionId !== hostSessionId || - this.continuationSupport() === "unsupported" - ) - return null; + if (lease?.hostSessionId !== hostSessionId) return null; const projection = await this.#read(lease); if (this.#lease !== lease || !projection) return null; if ( @@ -410,6 +402,7 @@ export class AutoDriveCoordinator { { state: "inactive" } >["reason"] = "restarted", ): void { + this.#epoch++; this.#options.recovery?.revoke(); if (this.#lease) { this.#setTiming("inactive"); @@ -436,6 +429,7 @@ export class AutoDriveCoordinator { lease.token !== message.token ) return "stale-continuation"; + this.#authorityReady = true; lease.delivery = delivery; lease.messageId = messageId; if ( @@ -465,6 +459,7 @@ export class AutoDriveCoordinator { if (!lease.checkpoint && lease.inFlight !== "status") this.deactivate(hostSessionId); else { + this.#authorityReady = true; lease.token = crypto.randomUUID(); lease.messageId = messageId; lease.delivery = delivery; @@ -475,53 +470,58 @@ export class AutoDriveCoordinator { } compactionContext(hostSessionId: string): string | null { if (this.#lease?.hostSessionId !== hostSessionId) return null; - const context = [ - "An in-memory /flow-auto continuation remains active.", - "Read compact Flow state first.", - CONTINUATION_ROUTE, - "Do not expand the approved goal; stop for required user direction, a hard blocker, or confirmed closure.", - ].join(" "); - return `${context}\n\n${FLOW_MANAGER_KERNEL}`; + return autoCompactionMessage(); } - observeHostMessage(host: string, message: HostMessage): void { - if (message.role === "assistant") { - if (message.parentID === undefined) this.#hostMissingParentage = true; - else this.#hostParentage = true; - } + captureAuthority(host: string, token?: string): AutoDriveAuthority | null { const lease = this.#lease; - if (lease?.hostSessionId !== host) return; - if (message.role === "assistant" && message.parentID !== undefined) { - lease.assistantParents.set(message.id, message.parentID); - if (message.summary !== true) - lease.lastAssistantParent = message.parentID; - } - lease.compaction = observeCompactionMessage( - lease.compaction, - lease.messageId, - message, + if ( + !this.#authorityReady || + !lease || + lease.hostSessionId !== host || + (token !== undefined && lease.token !== token) + ) + return null; + return Object.freeze({ + hostSessionId: host, + token: lease.token, + epoch: this.#epoch, + }); + } + isAuthorityCurrent(authority: AutoDriveAuthority, source?: string): boolean { + return ( + this.#authorityReady && + this.#lease?.hostSessionId === authority.hostSessionId && + this.#lease.token === authority.token && + this.#epoch === authority.epoch && + (source === undefined || this.#lease.messageId === source) ); } - observeHostPart(host: string, part: HostPart): void { + revokeExecutions(host: string): void { + if (this.#lease?.hostSessionId !== host) return; + this.#epoch++; + this.#authorityReady = false; + this.#lease.assistantOrigins.clear(); + } + observeAssistant(host: string, assistantId: string, origin: string): void { const lease = this.#lease; if (lease?.hostSessionId !== host) return; - lease.compaction = observeCompactionPart( - lease.compaction, - lease.messageId, - lease.lastAssistantParent, - part, - ); + const prior = lease.assistantOrigins.get(assistantId); + if (prior !== undefined && prior !== origin) + return void this.#rejectOrigin(lease, "mutation"); + lease.assistantOrigins.set(assistantId, origin); } - observeCompaction(host: string): void { + observeCompaction( + host: string, + authority: AutoDriveAuthority, + source: string, + controlID: string, + ): void { const lease = this.#lease; - if (lease?.hostSessionId !== host || lease.messageId === null) return; - const successor = authenticatedCompactionSuccessor( - lease.compaction, - lease.messageId, - ); - lease.compaction = null; - if (!successor) return void this.#rejectOrigin(lease, "compaction"); - lease.messageId = successor; - this.#options.recovery?.observeMessage(host, successor, true, true); + if (!lease || lease.hostSessionId !== host) return; + if (!this.isAuthorityCurrent(authority) || lease.messageId !== source) + return void this.#rejectOrigin(lease, "compaction"); + lease.messageId = controlID; + this.#options.recovery?.observeMessage(host, controlID, true, true); } observeMutation( host: string, @@ -536,8 +536,13 @@ export class AutoDriveCoordinator { }>, ): void { const lease = this.#lease; - if (lease?.hostSessionId !== host || !lease.messageId) return; - const origin = lease.assistantParents.get(assistantId); + if ( + !this.#authorityReady || + lease?.hostSessionId !== host || + !lease.messageId + ) + return; + const origin = lease.assistantOrigins.get(assistantId); if (origin === undefined) return void this.#rejectOrigin(lease, "mutation"); if (origin !== lease.messageId) return; lease.validation = null; @@ -588,17 +593,18 @@ export class AutoDriveCoordinator { ): AutoValidationOrigin | null { const lease = this.#lease; if ( + !this.#authorityReady || lease?.hostSessionId !== hostSessionId || !lease.messageId || !lease.baseline?.sessionId ) return null; - const parent = lease.assistantParents.get(assistantId); - if (parent === undefined) { + const source = lease.assistantOrigins.get(assistantId); + if (source === undefined) { this.#rejectOrigin(lease, "mutation"); return null; } - if (parent !== lease.messageId) return null; + if (source !== lease.messageId) return null; const origin: AutoValidationOrigin = { hostSessionId, sessionId: lease.baseline.sessionId, @@ -630,7 +636,7 @@ export class AutoDriveCoordinator { lease.hostSessionId !== origin.hostSessionId || lease.baseline?.sessionId !== origin.sessionId || lease.messageId !== origin.authority || - lease.assistantParents.get(origin.assistantId) !== origin.authority || + lease.assistantOrigins.get(origin.assistantId) !== origin.authority || !validationReceiptMatches(receipt) ) return; @@ -659,13 +665,8 @@ export class AutoDriveCoordinator { ? { scope: "current-plugin-process", ...this.#disposition } : null; } - /** - * Automatic mutations and continuation require attributable parentage. - * Unsupported hosts need a fresh manual command, which retires auto authority. - */ continuationSupport(): ProcessLocalAutoContinuationSupport { - if (this.#hostParentage) return "supported"; - return this.#hostMissingParentage ? "unsupported" : "unknown"; + return "supported"; } timingSnapshot(): AutoTimingSnapshot | null { const timing = this.#timing; @@ -682,10 +683,11 @@ export class AutoDriveCoordinator { pausedTimeExcluded: true, }; } - /** Routes one idle event; every unproven continuation stops or parks. */ async onIdle(hostSessionId: string): Promise { const lease = this.#lease; if (!lease || lease.hostSessionId !== hostSessionId) return; + const epoch = this.#epoch; + if (!this.#authorityReady) return; if (lease.inFlight) { if ( lease.inFlight !== "reply-status" || @@ -699,7 +701,7 @@ export class AutoDriveCoordinator { try { const projection = await this.#read(lease); if (!projection) return; - if (this.#lease !== lease) return; + if (this.#lease !== lease || this.#epoch !== epoch) return; const baseline = lease.baseline; if (!baseline) return this.#stop(lease); if (lease.manualContinuation) { @@ -741,6 +743,7 @@ export class AutoDriveCoordinator { { [FLOW_AUTO_METADATA_KEY]: lease.token }, ); } catch (error) { + if (this.#epoch !== epoch) return; this.#stop( lease, `Flow inspection approval prompt failed: ${String(error)}`, @@ -764,6 +767,7 @@ export class AutoDriveCoordinator { } if ( this.#lease !== lease || + this.#epoch !== epoch || lease.messageId !== authority || lease.validation !== validation ) @@ -831,6 +835,7 @@ export class AutoDriveCoordinator { }) .catch( (error) => + this.#epoch === epoch && lease.token === token && this.#stop( lease, @@ -865,17 +870,14 @@ export class AutoDriveCoordinator { const token = lease.token; lease.lastPromptedRevision = 0; lease.inFlight = "prompt"; - // Narrowing only: decideOnIdle already required hasDelivery. if (!lease.delivery) return; await this.#options - .prompt( - hostSessionId, - `${INITIAL_ROUTE}\n\n${FLOW_MANAGER_KERNEL}`, - lease.delivery, - { [FLOW_AUTO_METADATA_KEY]: token }, - ) + .prompt(hostSessionId, AUTO_INITIAL_PROMPT, lease.delivery, { + [FLOW_AUTO_METADATA_KEY]: token, + }) .catch( (error) => + this.#epoch === epoch && lease.token === token && this.#stop( lease, @@ -888,7 +890,12 @@ export class AutoDriveCoordinator { case "handback-and-wait": { const token = lease.token; await this.#promptHandback(lease, projection); - if (this.#lease !== lease || lease.token !== token) return; + if ( + this.#lease !== lease || + lease.token !== token || + this.#epoch !== epoch + ) + return; this.#disposition = { state: "waiting", reason: @@ -907,7 +914,12 @@ export class AutoDriveCoordinator { const already = lease.handbackPromptedRevision === projection.revision; await this.#promptHandback(lease, projection); - if (this.#lease !== lease || lease.token !== token) return; + if ( + this.#lease !== lease || + lease.token !== token || + this.#epoch !== epoch + ) + return; if ( !already && lease.handbackPromptedRevision === projection.revision @@ -933,7 +945,6 @@ export class AutoDriveCoordinator { return this.#warn(decision.warning); case "continue": { if (decision.clearCheckpoint) lease.checkpoint = null; - // Narrowing only: decideOnIdle already required hasDelivery. if (!lease.delivery) return; if (decision.repairHandoff && repairKey) this.#repairHandoffs.add(repairKey); @@ -958,6 +969,7 @@ export class AutoDriveCoordinator { { [FLOW_AUTO_METADATA_KEY]: lease.token }, ); } catch (error) { + if (this.#epoch !== epoch) return; this.#stop( lease, `Flow auto prompt failed: ${String(error)}`, diff --git a/src/platform/opencode/command-hook.ts b/src/platform/opencode/command-hook.ts index b3127057..c99db480 100644 --- a/src/platform/opencode/command-hook.ts +++ b/src/platform/opencode/command-hook.ts @@ -1,3 +1,4 @@ +import type { SessionPromptInput } from "@opencode/client"; import type { FlowService } from "../../application/flow-service.js"; import type { RecoveryController, @@ -6,17 +7,17 @@ import type { import { FLOW_CORE_COMMANDS } from "../../config-shared.js"; import { requestEvidenceAnchor } from "../../domain/request-evidence.js"; import type { AutoDriveCoordinator, AutoGoalIntent } from "./auto-drive.js"; -import { FLOW_AUTO_METADATA_KEY } from "./auto-drive.js"; -import type { Hooks } from "./sdk.js"; type FlowCommandName = keyof typeof FLOW_CORE_COMMANDS; -type CommandHook = NonNullable; -type CommandOutput = Parameters[1]; -type Part = CommandOutput["parts"][number]; -type TextPart = Extract; -// Host assigns id, sessionID and messageID after the command hook returns. -type DraftTextPart = Omit; -const AUTO_STOPPED = "Flow auto stopped."; +type PreparedFlowCommand = Readonly<{ + name: FlowCommandName; + goal: string; + text: string; + settings: RecoverySettings | null; + explicit: boolean; + stopping: boolean; + intent: AutoGoalIntent; +}>; const PLAN_ONLY_REQUEST = [ /\b(?:plan[- ]only|planning[- ]only)\b/i, /\b(?:only|just)\s+(?:(?:want|need|draft|write|create|make|prepare|give\s+me|show\s+me)\s+)?(?:(?:a|an|the)\s+)?(?:(?:flow|phased|inspection)\s+)?plan\b/i, @@ -44,217 +45,104 @@ function autoGoalIntent(goal: string): AutoGoalIntent { return "inspection-deliverable"; return withoutNegation !== request ? "plan-only" : "uncertain"; } -function isFlowCommand(command: string): command is FlowCommandName { - return Object.hasOwn(FLOW_CORE_COMMANDS, command); -} -export function textPart( - text: string, - synthetic = false, - metadata?: Readonly>, -): DraftTextPart { - return { - type: "text", - text, - ...(synthetic ? { synthetic: true } : {}), - ...(metadata ? { metadata } : {}), - }; -} -/** - * The command hook's parts are typed with the identity the host assigns after - * the hook returns, so a part written here is a draft at runtime. This is the - * one place a draft crosses into the host's array. - */ -function asHostTextPart(part: DraftTextPart): TextPart { - return part as TextPart; -} -function rewriteCommand( - command: FlowCommandName, +export function prepareFlowCommand( + name: FlowCommandName, args: string, - output: CommandOutput, -): void { - const config = FLOW_CORE_COMMANDS[command]; - const promptArgs = config.subtask - ? args - : "the preceding non-synthetic Flow request"; - const prompt = config.template.split("$ARGUMENTS").join(promptArgs); - if (!config.subtask) { - if (output.parts.some((part) => part.type === "subtask")) - throw new Error("Flow manager commands cannot contain subtask parts."); - const preserved = output.parts.filter((part) => part.type !== "text"); - output.parts.splice( - 0, - output.parts.length, - asHostTextPart( - textPart(args.trim() ? `Flow ${command}: ${args}` : `Flow ${command}`), - ), - asHostTextPart(textPart(prompt, true)), - ...preserved, +): PreparedFlowCommand { + const parsed = + name === "flow-auto" + ? parseRecoveryCommand(args) + : { goal: args, settings: null, explicit: false }; + const stopping = + name === "flow-auto" && /^(?:stop|cancel)$/i.test(args.trim()); + const template = FLOW_CORE_COMMANDS[name].template + .split("$ARGUMENTS") + .join( + name === "flow-review" + ? parsed.goal + : "the preceding Flow request in this same input", ); - return; - } - const part = output.parts[0]; - if (output.parts.length !== 1 || part?.type !== "subtask") - throw new Error(`/${command} requires exactly one reviewer subtask.`); - if (part.agent !== config.agent) - throw new Error(`/${command} must dispatch to '${config.agent}'.`); - // The host's subtask type does not declare `command`, but a command-dispatched - // subtask carries it at runtime, so its presence is checked, not asserted. - const declared = "command" in part ? part.command : undefined; - if (typeof declared !== "string" || declared.replace(/^\/+/, "") !== command) - throw new Error(`/${command} subtask identity did not match.`); - part.prompt = prompt; + return Object.freeze({ + name, + ...parsed, + stopping, + intent: autoGoalIntent(parsed.goal), + text: stopping + ? "/flow-auto stop" + : `Flow ${name}${parsed.goal.trim() ? `: ${parsed.goal}` : ""}\n\n${template}`, + }); } + export function createCommandHook( options: Readonly<{ - assertOperational: (action: string) => void; + assertOperational(action: string): void; autoDrive: AutoDriveCoordinator; flow: FlowService; - recovery?: RecoveryController; - defaultRecovery?: () => RecoverySettings | null; - showRefusal?: (message: string) => Promise; + recovery: RecoveryController; + defaultRecovery?: (() => RecoverySettings | null) | undefined; }>, -): CommandHook { - const { assertOperational, autoDrive, flow, recovery } = options; - let generation = 0; - let invocation: { host: string; generation: number } | null = null; - return async (input, output) => { - const command = input.command.replace(/^\/+/, ""); - if (!isFlowCommand(command)) return; - const action = input.arguments.trim(); - const stopping = - command === "flow-auto" && /^(?:stop|cancel)$/i.test(action); - const continuationToken = - (command === "flow-run" || command === "flow-status") && - (await autoDrive.resumeForCommand(input.sessionID)); - const continuing = typeof continuationToken === "string"; - let cancelledPending = false; - if (command === "flow-auto" && !stopping) { - invocation = { host: input.sessionID, generation: ++generation }; - autoDrive.clear(); - recovery?.revoke(); - } else if (!continuing && invocation?.host === input.sessionID) { - cancelledPending = true; - invocation = null; - generation++; - recovery?.revoke(input.sessionID); - autoDrive.deactivate( - input.sessionID, - stopping ? "cancelled" : "interrupted", - ); - } - const entryGeneration = generation; - const assertCurrent = () => { - if (generation !== entryGeneration) - throw new Error("Flow command was superseded."); - }; - const parsed = - command === "flow-auto" - ? parseRecoveryCommand(input.arguments) - : { goal: input.arguments, settings: null, explicit: false }; - if (command === "flow-auto" && /^(?:stop|cancel)$/i.test(action)) { - const confirmed = output.parts.some( - (part) => part.type === "text" && part.text === AUTO_STOPPED, - ); - recovery?.revoke(input.sessionID); - const response = - autoDrive.deactivate(input.sessionID, "cancelled") || - cancelledPending || - confirmed - ? AUTO_STOPPED - : "No Flow auto lease was active in this OpenCode session."; - output.parts[0] = asHostTextPart(textPart(response)); - output.parts.length = 1; - return; +) { + return async ( + command: PreparedFlowCommand, + host: string, + assertCurrent: () => void, + invocationToken?: string, + ): Promise> | null> => { + assertCurrent(); + options.assertOperational(`execute /${command.name}`); + const { autoDrive, recovery, flow } = options; + if (command.stopping) { + recovery.revoke(host); + autoDrive.deactivate(host, "cancelled"); + return null; } - try { - assertOperational(`execute /${command}`); - let newlyAnchoredSessionId: string | null = null; - const settings = - command === "flow-auto" && !parsed.explicit - ? (options.defaultRecovery?.() ?? null) - : parsed.settings; - if (command === "flow-auto") { - if (settings && !recovery) - throw new Error("Recovery is unavailable in this host."); - recovery?.activate( - input.sessionID, - settings, - parsed.explicit ? "explicit" : "default", - ); - } else if (!continuing) recovery?.revoke(input.sessionID); - if (command === "flow-auto" || command === "flow-plan") { - const evidence = requestEvidenceAnchor(parsed.goal, input.sessionID); + if (command.name !== "flow-auto") { + if (command.name === "flow-plan") { + const evidence = requestEvidenceAnchor(command.goal, host); if (evidence) { await flow.status({ request: { view: "compact" } }); assertCurrent(); - newlyAnchoredSessionId = await flow.requestAnchor({ - goal: parsed.goal, - evidence, - }); + await flow.requestAnchor({ goal: command.goal, evidence }); assertCurrent(); } } + const token = + command.name === "flow-run" || command.name === "flow-status" + ? await autoDrive.resumeForCommand(host) + : null; assertCurrent(); - rewriteCommand(command, parsed.goal, output); - if (command !== "flow-auto") { - if (continuing) { - const instruction = output.parts.find( - (part): part is TextPart => - part.type === "text" && part.synthetic === true, - ); - if (!instruction) - throw new Error("Flow continuation instruction missing."); - instruction.metadata = { - ...instruction.metadata, - [FLOW_AUTO_METADATA_KEY]: continuationToken, - }; - } - if (!continuing) autoDrive.deactivate(input.sessionID); - return; + if (!token) { + recovery.revoke(host); + autoDrive.deactivate(host); } - const metadata = await autoDrive.activate(input.sessionID, parsed.goal, { - newlyAnchoredSessionId, - intent: autoGoalIntent(parsed.goal), + return token ? { "opencode-plugin-flow/auto": token } : null; + } + autoDrive.clear(); + recovery.activate( + host, + command.explicit + ? command.settings + : (options.defaultRecovery?.() ?? null), + command.explicit ? "explicit" : "default", + ); + let newlyAnchoredSessionId: string | null = null; + const evidence = requestEvidenceAnchor(command.goal, host); + if (evidence) { + await flow.status({ request: { view: "compact" } }); + assertCurrent(); + newlyAnchoredSessionId = await flow.requestAnchor({ + goal: command.goal, + evidence, }); assertCurrent(); - if (autoDrive.continuationSupport() === "unsupported") { - output.parts.unshift( - asHostTextPart( - textPart( - "Note: this host reports no assistant parentage, so automatic mutations cannot be attributed. Use a fresh manual /flow-plan or /flow-run command.", - ), - ), - ); - } - const instruction = output.parts.find( - (part): part is TextPart => - part.type === "text" && part.synthetic === true, - ); - if (!instruction) { - throw new Error("/flow-auto is missing its synthetic instruction."); - } - instruction.metadata = { ...instruction.metadata, ...metadata }; - } catch (error) { - if ( - command === "flow-auto" && - invocation?.host === input.sessionID && - invocation.generation === entryGeneration - ) { - invocation = null; - recovery?.revoke(input.sessionID); - autoDrive.deactivate(input.sessionID); - } - if ( - command === "flow-auto" && - error instanceof Error && - error.message.startsWith("Delegated recovery is unavailable.") - ) { - try { - await options.showRefusal?.(error.message); - } catch {} - } - throw error; } + const metadata = await autoDrive.activate(host, command.goal, { + ...(invocationToken === undefined ? {} : { token: invocationToken }), + newlyAnchoredSessionId, + intent: command.intent, + }); + assertCurrent(); + return metadata; }; } @@ -302,3 +190,57 @@ export function parseRecoveryCommand(args: string): { explicit: true, }; } + +export function nativePromptPayload( + prompt: import("@opencode/plugin/promise/command").CommandInvocation["prompt"], +): Omit { + return { + text: prompt.text, + ...(prompt.files === undefined + ? {} + : { + files: prompt.files.map((file) => ({ + uri: file.uri, + ...(file.name === undefined ? {} : { name: file.name }), + ...(file.description === undefined + ? {} + : { description: file.description }), + ...(file.mention === undefined + ? {} + : { mention: { ...file.mention } }), + })), + }), + ...(prompt.agents === undefined + ? {} + : { + agents: prompt.agents.map((agent) => ({ + name: agent.name, + ...(agent.mention === undefined + ? {} + : { mention: { ...agent.mention } }), + })), + }), + ...(prompt.skills === undefined + ? {} + : { + skills: prompt.skills.map((skill) => ({ + id: skill.id, + ...(skill.mention === undefined + ? {} + : { mention: { ...skill.mention } }), + })), + }), + }; +} + +export function flowCommandName(name: string): keyof typeof FLOW_CORE_COMMANDS { + if ( + name === "flow-auto" || + name === "flow-plan" || + name === "flow-run" || + name === "flow-review" || + name === "flow-status" + ) + return name; + throw new Error("Unknown Flow command."); +} diff --git a/src/platform/opencode/config.ts b/src/platform/opencode/config.ts index f510e27b..86a70e4a 100644 --- a/src/platform/opencode/config.ts +++ b/src/platform/opencode/config.ts @@ -1,77 +1,46 @@ import { - applyFlowConfig, + createFlowCoreConfigEntries, type FlowReviewerConfiguration, - type MutableFlowConfig, } from "../../config-shared.js"; -import { createFlowLog } from "./logging.js"; -import { - applyReviewerPreference, - FLOW_MODEL_ROLES, - modelPreference, -} from "./model-picker.js"; +import { Model, type PluginContext } from "./sdk.js"; -export function createConfigHook( - ctx: unknown, - options?: { - assertOperational?: (action: string) => void; - reviewerConfiguration?: FlowReviewerConfiguration | undefined; - onPlanningModel?: (model: string | undefined) => void; - onReviewerConfiguration?: ( - configuration: FlowReviewerConfiguration, - ) => void; - }, +export async function registerFlowAgents( + ctx: { agent: Pick }, + options: Readonly<{ + assertOperational(action: string): void; + readPlanningModel(): string | undefined; + readReviewerConfiguration(): FlowReviewerConfiguration; + }>, ) { - const log = createFlowLog(ctx); - let preference: string | undefined; - let planningModel: string | undefined; - return async (config: MutableFlowConfig) => { - try { - options?.assertOperational?.("apply its OpenCode configuration"); - } catch (error) { - log("error", error instanceof Error ? error.message : String(error)); - return; - } - const planning = modelPreference(config, "planning"); - if (planning !== undefined) planningModel = planning || undefined; - options?.onPlanningModel?.(planningModel); - const saved = modelPreference(config, "review"); - if (saved !== undefined) preference = saved; - const reviewer = options?.reviewerConfiguration - ? applyReviewerPreference(options.reviewerConfiguration, preference) - : undefined; - if (reviewer) options?.onReviewerConfiguration?.(reviewer); - for (const { agent, key } of Object.values(FLOW_MODEL_ROLES)) { - const entry = config.agent?.[agent]; - if ( - !entry || - typeof entry !== "object" || - Object.keys(entry).length !== 1 || - !("options" in entry) - ) + return ctx.agent.transform((editor) => { + options.assertOperational("apply its OpenCode configuration"); + const planningModel = options.readPlanningModel(); + const entries = createFlowCoreConfigEntries({ + planningModel, + reviewerConfiguration: options.readReviewerConfiguration(), + }); + for (const [name, definition] of Object.entries(entries.agent)) { + if (name === "flow-planner" && !planningModel) { + editor.remove(name); continue; - const value = entry.options; - if ( - value && - typeof value === "object" && - Object.keys(value).length === 1 && - Object.hasOwn(value, key) && - typeof Reflect.get(value, key) === "string" - ) { - config.agent = { ...config.agent }; - delete config.agent[agent]; } + editor.update(name, (agent) => { + agent.mode = definition.mode; + agent.hidden = definition.hidden; + agent.system = definition.system; + agent.description = definition.description; + agent.permissions = structuredClone(definition.permissions); + agent.request = { settings: {}, headers: {}, body: {} }; + delete agent.model; + delete agent.steps; + if ("model" in definition && definition.model) { + agent.model = Model.Ref.parse(definition.model); + if ("variant" in definition && definition.variant) + agent.model.variant = Model.VariantID.make(definition.variant); + } + if ("steps" in definition && definition.steps) + agent.steps = definition.steps; + }); } - - applyFlowConfig(config, { - planningModel, - ...(reviewer ? { reviewerConfiguration: reviewer } : {}), - onWarning: (warning) => log("warn", warning), - onNotice: (notice) => log("info", notice), - onCollision: (kind, name) => - log( - "warn", - `Flow replaced a user-defined ${kind} named '${name}'; rename the local entry while Flow is enabled.`, - ), - }); - }; + }); } diff --git a/src/platform/opencode/execution-attribution.ts b/src/platform/opencode/execution-attribution.ts new file mode 100644 index 00000000..0541e78d --- /dev/null +++ b/src/platform/opencode/execution-attribution.ts @@ -0,0 +1,454 @@ +import { realpathSync } from "node:fs"; +import { resolve } from "node:path"; +import { isDeepStrictEqual } from "node:util"; +import type { SessionInfo, SessionMessageInfo } from "@opencode/client"; +import type { + SessionContext, + SessionHttpRequest, + SessionHttpResponse, +} from "@opencode/plugin/promise/session"; +import type { ToolContext } from "@opencode/plugin/promise/tool"; +import type { FlowCodingModel } from "../../config-shared.js"; +import type { AutoDriveAuthority, AutoDriveDelivery } from "./auto-drive.js"; + +export type NativeExecution = Pick< + ToolContext, + "sessionID" | "messageID" | "agent" | "id" +>; +export type InputOrigin = Readonly<{ + id: string; + epoch: number; + delivery: AutoDriveDelivery; +}> & + ( + | Readonly<{ automatic: true; authority: AutoDriveAuthority }> + | Readonly<{ automatic: false; authority: AutoDriveAuthority | null }> + ); + +type FrozenPrimary = { + readonly sessionID: string; + readonly agent: string; + readonly model: SessionContext["model"]; + readonly inputs: readonly string[]; + readonly previousAssistants: ReadonlySet; + readonly previousTerminals: ReadonlySet; + readonly previousRetries: ReadonlySet; + readonly origin: InputOrigin; + phase: "context" | "request" | "streaming" | "ended"; + request: Request | null; +}; +const same = isDeepStrictEqual; +function retryIdentity( + message: Extract, +): string { + return JSON.stringify([ + message.id, + message.retry?.attempt, + message.retry?.at, + ]); +} +function sameModel( + left: Readonly<{ + providerID: string; + id: string; + variant?: string | undefined; + }>, + right: Readonly<{ + providerID: string; + id: string; + variant?: string | undefined; + }>, +): boolean { + return ( + left.providerID === right.providerID && + left.id === right.id && + left.variant === right.variant + ); +} +export function nativeInputIDs( + messages: readonly SessionMessageInfo[], +): string[] { + return messages + .filter( + (message) => + message.type === "user" || + message.type === "synthetic" || + (message.type === "compaction" && message.status === "completed"), + ) + .map((message) => message.id); +} +export function modelInputIDs( + input: Pick, +): string[] { + return input.messages + .filter((message) => message.role === "user") + .map((message) => { + if (!message.id) + throw new Error("Flow model context omitted durable input identity."); + return message.id; + }); +} +export function nativeDelivery( + session: Readonly<{ + agent?: string | undefined; + model?: + | Readonly<{ + providerID: string; + id: string; + variant?: string | undefined; + }> + | undefined; + }>, +): AutoDriveDelivery { + if (!session.agent || !session.model) + throw new Error("Flow requires the native selected agent and model."); + return { + agent: session.agent, + model: { providerID: session.model.providerID, modelID: session.model.id }, + ...(session.model.variant === undefined + ? {} + : { variant: session.model.variant }), + }; +} +export function sameDelivery( + left: AutoDriveDelivery, + right: AutoDriveDelivery, +): boolean { + return ( + left.agent === right.agent && + left.model.providerID === right.model.providerID && + left.model.modelID === right.model.modelID && + left.variant === right.variant + ); +} +function canonical(path: string): string { + try { + return realpathSync(path); + } catch { + return resolve(path); + } +} +export class ExecutionAttribution { + readonly #primary = new Map(); + readonly #requests = new WeakMap(); + readonly #retries = new WeakSet(); + readonly #assistants = new Map(); + readonly #bindings = new Map< + string, + Readonly<{ primary: FrozenPrimary; tool: string }> + >(); + readonly #assertLive: (origin: InputOrigin) => void; + #closed = false; + constructor(assertLive: (origin: InputOrigin) => void) { + this.#assertLive = assertLive; + } + #key(session: string, id: string): string { + return JSON.stringify([session, id]); + } + requestOwner(request: Request): FrozenPrimary | undefined { + return this.#requests.get(request); + } + executionOwner(execution: NativeExecution): FrozenPrimary | undefined { + const binding = this.#bindings.get( + JSON.stringify([execution.sessionID, execution.messageID, execution.id]), + ); + const primary = + binding?.primary ?? + this.#assistants.get(this.#key(execution.sessionID, execution.messageID)); + if (primary) return primary; + const current = this.#primary.get(execution.sessionID); + return current?.previousAssistants.has(execution.messageID) + ? undefined + : current; + } + current(sessionID: string): FrozenPrimary | undefined { + return this.#primary.get(sessionID); + } + freeze( + input: SessionContext, + native: readonly SessionMessageInfo[], + origin: InputOrigin, + ): FrozenPrimary { + if (this.#closed) throw new Error("Flow lifecycle is closed."); + const prior = this.#primary.get(input.sessionID); + if (prior && prior.phase !== "ended") + throw new Error("Flow primary contexts or streams overlap."); + this.#assertLive(origin); + const inputs = modelInputIDs(input); + if (!same(inputs, nativeInputIDs(native))) + throw new Error( + "Flow native inputs differ from the model-visible prefix.", + ); + const delivery = nativeDelivery({ agent: input.agent, model: input.model }); + if (!sameDelivery(origin.delivery, delivery)) + throw new Error( + "Flow admitted agent or model differs from its primary request.", + ); + const retry = + prior && + this.#retries.delete(prior) && + prior.origin === origin && + same(prior.inputs, inputs) && + prior.agent === input.agent && + sameModel(prior.model, input.model); + const primary: FrozenPrimary = { + sessionID: input.sessionID, + agent: input.agent, + model: Object.freeze({ ...input.model }), + inputs: Object.freeze(inputs), + previousAssistants: retry + ? prior.previousAssistants + : new Set( + native + .filter((message) => message.type === "assistant") + .map((message) => message.id), + ), + origin, + previousRetries: new Set( + native.flatMap((message) => + message.type === "assistant" && message.retry !== undefined + ? [retryIdentity(message)] + : [], + ), + ), + previousTerminals: new Set( + native + .filter((message) => message.type === "idle") + .map((message) => message.id), + ), + phase: "context", + request: null, + }; + this.#primary.set(input.sessionID, primary); + return primary; + } + request(input: SessionHttpRequest): void { + if (input.kind !== "primary") return; + const primary = this.#primary.get(input.sessionID); + if ( + primary?.phase !== "context" || + primary.agent !== input.agent || + !sameModel(primary.model, input.model) || + this.#requests.has(input.request) + ) + throw new Error( + "Flow primary HTTP request has no unique frozen context.", + ); + this.#assertLive(primary.origin); + primary.request = input.request; + primary.phase = "request"; + this.#requests.set(input.request, primary); + } + hasFailedTerminal( + primary: FrozenPrimary, + native: readonly SessionMessageInfo[], + ): boolean { + return native.some( + (message, position) => + message.type === "idle" && + message.outcome !== "succeeded" && + !primary.previousTerminals.has(message.id) && + same(nativeInputIDs(native.slice(0, position)), primary.inputs), + ); + } + retry( + primary: FrozenPrimary, + native: readonly SessionMessageInfo[], + ): boolean { + if ( + this.#primary.get(primary.sessionID) !== primary || + primary.phase !== "request" || + !same(nativeInputIDs(native), primary.inputs) + ) + return false; + const candidates = native.filter( + (message, position) => + message.type === "assistant" && + message.retry !== undefined && + message.finish === undefined && + message.time.completed === undefined && + message.time.streamed === undefined && + message.content.length === 0 && + message.agent === primary.agent && + sameModel(message.model, primary.model) && + !primary.previousAssistants.has(message.id) && + !primary.previousRetries.has(retryIdentity(message)) && + !this.#assistants.has(this.#key(primary.sessionID, message.id)) && + same(nativeInputIDs(native.slice(0, position)), primary.inputs), + ); + if (candidates.length !== 1) return false; + this.#retries.add(primary); + this.end(primary); + return true; + } + end(primary: FrozenPrimary): boolean { + const active = primary.phase !== "ended"; + primary.phase = "ended"; + return active && this.#primary.get(primary.sessionID) === primary; + } + response( + input: SessionHttpResponse, + failed: (sessionID: string) => void, + ): void { + if (input.kind !== "primary") return; + const primary = this.#requests.get(input.request); + if ( + !primary || + primary !== this.#primary.get(input.sessionID) || + primary.phase !== "request" || + primary.agent !== input.agent || + !sameModel(primary.model, input.model) + ) { + if (primary) { + if (this.end(primary)) failed(primary.sessionID); + } else { + const current = this.#primary.get(input.sessionID); + if (current) this.end(current); + failed(input.sessionID); + } + throw new Error( + "Flow primary response did not match its Request identity.", + ); + } + const finish = () => this.end(primary); + const fail = () => { + if (finish()) failed(primary.sessionID); + }; + if (!input.response.ok) { + fail(); + return; + } + try { + this.#assertLive(primary.origin); + if (!input.response.body) + throw new Error("Flow primary response has no successful stream."); + const reader = input.response.body.getReader(); + primary.phase = "streaming"; + const response = input.response; + input.response = new Response( + new ReadableStream({ + async pull(controller) { + try { + const next = await reader.read(); + if (next.done) { + finish(); + controller.close(); + } else controller.enqueue(next.value); + } catch (error) { + fail(); + controller.error(error); + } + }, + async cancel(reason) { + fail(); + await reader.cancel(reason); + }, + }), + { + status: response.status, + statusText: response.statusText, + headers: response.headers, + }, + ); + } catch (error) { + fail(); + throw error; + } + } + verify( + execution: NativeExecution, + native: readonly SessionMessageInfo[], + session: SessionInfo, + workspace: string, + tool?: string, + ): Readonly<{ primary: FrozenPrimary; tool: string }> { + if ( + this.#closed || + session.id !== execution.sessionID || + session.agent !== execution.agent || + canonical(session.location.directory) !== canonical(workspace) + ) + throw new Error( + "Flow native execution session or workspace identity changed.", + ); + const primary = this.#primary.get(execution.sessionID); + if ( + !primary?.request || + primary.phase === "context" || + primary.agent !== execution.agent + ) + throw new Error("Flow tool has no attributable primary request."); + const position = native.findIndex( + (message) => message.id === execution.messageID, + ); + const assistant = native[position]; + if ( + assistant?.type !== "assistant" || + assistant.agent !== execution.agent || + !sameModel(assistant.model, primary.model) || + primary.previousAssistants.has(assistant.id) || + !same(nativeInputIDs(native.slice(0, position)), primary.inputs) + ) + throw new Error( + "Flow exact assistant identity or frozen input prefix did not match.", + ); + const call = assistant.content.find( + (part) => part.type === "tool" && part.id === execution.id, + ); + if ( + call?.type !== "tool" || + call.state.status !== "running" || + (tool !== undefined && call.name !== tool) + ) + throw new Error("Flow exact native running tool call did not match."); + const assistantKey = this.#key(execution.sessionID, execution.messageID); + const prior = this.#assistants.get(assistantKey); + if (prior && prior !== primary) + throw new Error("Flow assistant cannot be rebound to another request."); + const key = JSON.stringify([ + execution.sessionID, + execution.messageID, + execution.id, + ]); + const binding = this.#bindings.get(key); + if (binding && (binding.primary !== primary || binding.tool !== call.name)) + throw new Error( + "Flow execution identity conflicts with an immutable binding.", + ); + this.#assertLive(primary.origin); + this.#assistants.set(assistantKey, primary); + const verified = binding ?? Object.freeze({ primary, tool: call.name }); + this.#bindings.set(key, verified); + return verified; + } + clear(): void { + this.#closed = true; + this.#primary.clear(); + this.#assistants.clear(); + this.#bindings.clear(); + } +} + +export function rememberCodingModel( + models: Map, + input: Pick, + session: SessionInfo, + specialists: ReadonlySet, +): void { + if ( + session.parentID || + session.agent !== input.agent || + specialists.has(input.agent) + ) + return; + const selected = nativeDelivery(session); + models.delete(input.sessionID); + models.set(input.sessionID, { + ...selected.model, + ...(selected.variant === undefined ? {} : { variant: selected.variant }), + }); + if (models.size > 128) { + const oldest = models.keys().next().value; + if (oldest) models.delete(oldest); + } +} diff --git a/src/platform/opencode/flow-tool.ts b/src/platform/opencode/flow-tool.ts new file mode 100644 index 00000000..764516f0 --- /dev/null +++ b/src/platform/opencode/flow-tool.ts @@ -0,0 +1,41 @@ +import { z } from "zod"; + +export type FlowToolContext = Readonly<{ + sessionID: string; + messageID: string; + callID: string; + agent: string; + directory: string; + abort: AbortSignal; + assertCurrent?: (() => Promise) | undefined; +}>; + +export type FlowToolResponse = Readonly<{ + status: "ok" | "error"; + summary: string; + workflowData: object; +}>; + +export type FlowToolValue = FlowToolResponse | string; +type FlowToolDefinition = Readonly<{ + description: string; + input: z.core.JSONSchema.JSONSchema; + execute(input: unknown, context: FlowToolContext): Promise; +}>; +export type FlowTools = Readonly>; + +export function defineFlowTool(input: { + description: string; + schema: Schema; + execute: ( + args: z.output, + context: FlowToolContext, + ) => Promise; +}): FlowToolDefinition { + return { + description: input.description, + input: z.toJSONSchema(input.schema, { io: "input" }), + execute: (args, context) => + input.execute(input.schema.parse(args), context), + }; +} diff --git a/src/platform/opencode/http-transport.ts b/src/platform/opencode/http-transport.ts new file mode 100644 index 00000000..8b724249 --- /dev/null +++ b/src/platform/opencode/http-transport.ts @@ -0,0 +1,19 @@ +import type { ProviderDomain } from "@opencode/plugin/promise/provider"; + +export function registerHttpTransport( + provider: Pick, +) { + return provider.transform((editor) => { + for (const entry of editor.list()) { + editor.update(entry.provider.id, (current) => { + current.settings = { ...current.settings, transport: "http" }; + }); + for (const model of entry.models.values()) + editor.models.update(entry.provider.id, model.id, (current) => { + current.settings = { ...current.settings, transport: "http" }; + for (const variant of current.variants) + variant.settings = { ...variant.settings, transport: "http" }; + }); + } + }); +} diff --git a/src/platform/opencode/input-admission.ts b/src/platform/opencode/input-admission.ts new file mode 100644 index 00000000..7de29aac --- /dev/null +++ b/src/platform/opencode/input-admission.ts @@ -0,0 +1,509 @@ +import { isDeepStrictEqual as same } from "node:util"; +import type { + SessionInboxSynthetic, + SessionInboxUser, + SessionMessageInfo, + SessionPromptInput, + SessionSyntheticInput, +} from "@opencode/client"; +import type { + SessionContext, + SessionPrompt, +} from "@opencode/plugin/promise/session"; +import type { AutoDriveAuthority, AutoDriveDelivery } from "./auto-drive.js"; +import { nativePromptPayload } from "./command-hook.js"; +import { type InputOrigin, modelInputIDs } from "./execution-attribution.js"; + +type Payload = + | Omit + | Omit; +type InputReservation = Readonly<{ + host: string; + id: string; + epoch: number; +}>; +type Admission = { + kind: "private"; + reservation: InputReservation; + type: "user" | "synthetic"; + payload: Payload; + acceptedPayload?: + | SessionInboxUser["payload"] + | SessionInboxSynthetic["payload"]; + ready: Promise; + settle(origin: InputOrigin): void; + fail(error: Error): void; + state: "pending" | "accepted" | "rejected"; + origin: InputOrigin | null; +}; +type Real = { + kind: "real"; + reservation: InputReservation; + prompt: SessionPrompt["prompt"]; + metadata: SessionPrompt["metadata"]; + origin: InputOrigin | null; + resolving: Promise | null; +}; +type Alias = Readonly<{ + kind: "alias"; + source: Entry | undefined; + origin: InputOrigin | null; +}>; +type Entry = + | Admission + | Real + | Alias + | Readonly<{ kind: "reserved"; reservation: InputReservation }>; +type SourceEvidence = + | Readonly<{ kind: "live"; origin: InputOrigin }> + | Readonly<{ + kind: + | "retired-ordinary" + | "retired-automatic" + | "unknown-history" + | "rejected"; + }>; +export type InputScope = Readonly<{ + host: string; + consumed: readonly string[]; + source: string; + assertCurrent(): void; + current(): boolean; + wait(promise: Promise): Promise; + ready(): Promise; + release(): void; +}>; +type ScopeState = Readonly<{ + entry: Entry | undefined; + frontierEntry: Entry | undefined; + cancel(error: Error): void; +}>; +const revoked = () => new Error("Flow execution epoch was revoked."); +type InputAdmissionOptions = Readonly<{ + assertOpen(): void; + authorityCurrent(authority: AutoDriveAuthority): boolean; + observeReal( + host: string, + id: string, + text: string, + delivery: AutoDriveDelivery, + assertCurrent: () => void, + ): Promise; + authority(host: string): AutoDriveAuthority | null; +}>; +export class InputAdmissions { + readonly #entries = new Map(); + readonly #frontier = new Map(); + readonly #epochs = new Map(); + readonly #hosts = new WeakMap(); + readonly #scopes = new Map(); + #closed = false; + readonly #options: InputAdmissionOptions; + constructor(options: InputAdmissionOptions) { + this.#options = options; + } + #key(host: string, id: string) { + return JSON.stringify([host, id]); + } + epoch(host: string): number { + return this.#epochs.get(host) ?? 0; + } + #cancel(host: string) { + for (const [scope, state] of this.#scopes) + if (scope.host === host) state.cancel(revoked()); + } + invalidate(host: string): void { + this.#epochs.set(host, this.epoch(host) + 1); + this.#cancel(host); + } + reserve(host: string, id: string): InputReservation { + this.#options.assertOpen(); + const key = this.#key(host, id); + if (this.#closed || this.#entries.has(key)) + throw new Error( + "Flow admission identity is unavailable or already registered.", + ); + const reservation = Object.freeze({ host, id, epoch: this.epoch(host) }); + this.#frontier.set(host, reservation); + this.#entries.set(key, { kind: "reserved", reservation }); + this.#cancel(host); + return reservation; + } + assertReservation(reservation: InputReservation): void { + this.#options.assertOpen(); + if ( + this.#closed || + this.epoch(reservation.host) !== reservation.epoch || + this.#frontier.get(reservation.host) !== reservation + ) + throw revoked(); + } + register( + reservation: InputReservation, + input: Readonly<{ type: "user" | "synthetic"; payload: Payload }>, + ): Admission { + this.assertReservation(reservation); + const key = this.#key(reservation.host, reservation.id); + if (this.#entries.get(key)?.kind !== "reserved") + throw new Error( + "Flow admission identity is unavailable or already registered.", + ); + const { + promise: ready, + resolve: settle, + reject: fail, + } = Promise.withResolvers(); + void ready.catch(() => {}); + const admission: Admission = { + kind: "private", + reservation, + ...input, + payload: structuredClone(input.payload), + ready, + settle, + fail, + state: "pending", + origin: null, + }; + this.#entries.set(key, admission); + return admission; + } + async accept( + admission: Admission, + receipt: SessionInboxUser | SessionInboxSynthetic, + activate: () => Promise, + ): Promise { + if (admission.state !== "pending") + throw new Error("Flow admission was already settled."); + try { + this.assertReservation(admission.reservation); + if ( + receipt.sessionID !== admission.reservation.host || + receipt.id !== admission.reservation.id || + receipt.type !== admission.type || + !matchesAdmissionPayload(admission.payload, receipt.payload) + ) + throw new Error( + "Flow admission receipt did not match its exact local payload.", + ); + admission.acceptedPayload = structuredClone(receipt.payload); + const origin = await activate(); + this.assertReservation(admission.reservation); + this.assertLive(origin); + if (origin.epoch !== admission.reservation.epoch) + throw new Error("Flow admission epoch changed before acceptance."); + admission.origin = origin; + admission.state = "accepted"; + admission.settle(origin); + } catch (error) { + this.reject(admission, error); + throw error; + } + } + reject(admission: Admission, error: unknown): void { + if (admission.state !== "pending") return; + admission.state = "rejected"; + admission.fail(error instanceof Error ? error : new Error(String(error))); + } + hasPrivate(host: string, id: string): boolean { + const entry = this.#entries.get(this.#key(host, id)); + return entry?.kind === "private" || entry?.kind === "reserved"; + } + observePrompt(input: SessionPrompt, beforeReserve: () => void): void { + const prior = this.#entries.get( + this.#key(input.sessionID, input.messageID), + ); + if (prior?.kind === "real") { + if ( + !same(prior.prompt, input.prompt) || + !same(prior.metadata, input.metadata) + ) + throw new Error("Flow duplicate prompt changed its payload."); + return; + } + beforeReserve(); + const reservation = this.reserve(input.sessionID, input.messageID); + this.#entries.set(this.#key(input.sessionID, input.messageID), { + kind: "real", + reservation, + prompt: structuredClone(input.prompt), + metadata: structuredClone(input.metadata), + origin: null, + resolving: null, + }); + } + #covers(entry: Entry | undefined, frontier: InputReservation): boolean { + return entry?.kind === "alias" + ? this.#covers(entry.source, frontier) + : entry?.reservation === frontier; + } + capture(input: Pick): InputScope { + this.#options.assertOpen(); + const host = input.sessionID, + consumed = Object.freeze(modelInputIDs(input)), + source = consumed.at(-1); + if (!source) + throw new Error("Flow primary context has no attributable input."); + const epoch = this.epoch(host), + frontier = this.#frontier.get(host); + if ( + frontier && + !consumed.some((id) => + this.#covers(this.#entries.get(this.#key(host, id)), frontier), + ) + ) + throw revoked(); + const entry = this.#entries.get(this.#key(host, source)); + const cancelled = Promise.withResolvers(); + void cancelled.promise.catch(() => {}); + const current = () => + !this.#closed && + this.epoch(host) === epoch && + this.#frontier.get(host) === frontier; + const assertCurrent = () => { + this.#options.assertOpen(); + if (!current()) throw revoked(); + }; + const scope: InputScope = { + host, + consumed, + source, + current, + assertCurrent, + wait: async (promise) => { + assertCurrent(); + const result = await Promise.race([promise, cancelled.promise]); + assertCurrent(); + return result; + }, + ready: async () => { + if (entry?.kind === "private") await scope.wait(entry.ready); + assertCurrent(); + }, + release: () => { + this.#scopes.delete(scope); + }, + }; + this.#scopes.set(scope, { + entry, + frontierEntry: frontier + ? this.#entries.get(this.#key(host, frontier.id)) + : undefined, + cancel: cancelled.reject, + }); + return scope; + } + positive(scope: InputScope): InputOrigin | undefined { + if (!scope.current()) return; + const entry = this.#scopes.get(scope)?.entry; + const origin = entry && entry.kind !== "reserved" ? entry.origin : null; + return origin && this.isLive(origin) ? origin : undefined; + } + async evidence( + scope: InputScope, + native: readonly SessionMessageInfo[], + delivery: AutoDriveDelivery, + ): Promise { + scope.assertCurrent(); + await scope.ready(); + const entry = this.#scopes.get(scope)?.entry; + if (!entry) return this.#history(this.#scopes.get(scope)?.frontierEntry); + if (entry.kind === "reserved") return { kind: "rejected" }; + if (entry.kind === "alias") { + if (entry.origin) return this.#classify(entry.origin); + return this.#history(entry.source); + } + const message = native.find((message) => message.id === scope.source); + if (!message) + throw new Error("Flow consumed input is missing from native context."); + if (entry.kind === "private") { + if (entry.state === "rejected") return { kind: "rejected" }; + if ( + message.type !== entry.type || + (message.type !== "user" && message.type !== "synthetic") + ) + throw new Error( + "Flow native input type conflicts with its admitted dispatch.", + ); + const { id: _id, time: _time, type: _type, ...payload } = message; + if (!same(payload, entry.acceptedPayload)) + throw new Error( + "Flow native input payload conflicts with its admitted dispatch.", + ); + if (!entry.origin) + throw new Error("Flow admitted input has no settled origin."); + return this.#classify(entry.origin); + } + if ( + message.type !== "user" || + !same(entry.metadata ?? {}, message.metadata ?? {}) || + !matchesAdmissionPayload( + { + ...nativePromptPayload(entry.prompt), + ...(message.metadata === undefined + ? {} + : { metadata: message.metadata }), + }, + message, + ) + ) + throw new Error( + "Flow context has an unknown, synthetic, or stale real input.", + ); + if (entry.origin) return this.#classify(entry.origin); + if (entry.reservation.epoch !== this.epoch(scope.host)) + return { kind: "retired-ordinary" }; + entry.resolving ??= (async () => { + await this.#options.observeReal( + scope.host, + scope.source, + message.text, + delivery, + scope.assertCurrent, + ); + scope.assertCurrent(); + const origin = this.makeOrigin( + scope.host, + scope.source, + delivery, + false, + entry.reservation.epoch, + ); + entry.origin = origin; + return origin; + })(); + return this.#classify(await scope.wait(entry.resolving)); + } + #history(entry: Entry | undefined): SourceEvidence { + if (!entry) return { kind: "unknown-history" }; + if (entry.kind === "alias") return this.#history(entry.source); + if ( + entry.kind === "reserved" || + (entry.kind === "private" && entry.state !== "accepted") + ) + return { kind: "rejected" }; + return { + kind: entry.origin?.authority ? "retired-automatic" : "retired-ordinary", + }; + } + #classify(origin: InputOrigin): SourceEvidence { + return this.isLive(origin) + ? { kind: "live", origin } + : { kind: origin.authority ? "retired-automatic" : "retired-ordinary" }; + } + makeOrigin( + host: string, + id: string, + delivery: AutoDriveDelivery, + automatic: boolean, + epoch: number, + ): InputOrigin { + const identity = { + id, + epoch, + delivery: Object.freeze({ + ...delivery, + model: Object.freeze({ ...delivery.model }), + }), + }; + const authority = this.#options.authority(host); + let origin: InputOrigin; + if (automatic) { + if (!authority) + throw new Error( + "Flow automatic input lost its live admitted authority.", + ); + origin = Object.freeze({ ...identity, automatic: true, authority }); + } else origin = Object.freeze({ ...identity, automatic: false, authority }); + this.#hosts.set(origin, host); + this.assertLive(origin); + return origin; + } + checkpoint(scope: InputScope, id: string, origin: InputOrigin | null): void { + scope.assertCurrent(); + const state = this.#scopes.get(scope); + if (!state) + throw new Error("Flow checkpoint has no captured admission scope."); + const key = this.#key(scope.host, id); + if (this.#entries.has(key)) + throw new Error("Flow checkpoint identity conflicts with an input."); + if (origin) this.#hosts.set(origin, scope.host); + this.#entries.set(key, { + kind: "alias", + source: state.entry ?? state.frontierEntry, + origin, + }); + } + isLive(origin: InputOrigin): boolean { + const host = this.#hosts.get(origin); + if (this.#closed || host === undefined || this.epoch(host) !== origin.epoch) + return false; + const frontier = this.#frontier.get(host); + return ( + (!frontier || + this.#covers( + this.#entries.get(this.#key(host, origin.id)), + frontier, + )) && + (!origin.authority || this.#options.authorityCurrent(origin.authority)) + ); + } + assertLive(origin: InputOrigin): void { + this.#options.assertOpen(); + if (!this.isLive(origin)) throw revoked(); + } + clear(): void { + this.#closed = true; + for (const state of this.#scopes.values()) state.cancel(revoked()); + this.#scopes.clear(); + this.#entries.clear(); + this.#frontier.clear(); + } +} +function matchesAdmissionPayload( + expected: Admission["payload"], + actual: SessionInboxUser["payload"] | SessionInboxSynthetic["payload"], +): boolean { + if ( + expected.text !== actual.text || + !same(expected.metadata ?? {}, actual.metadata ?? {}) + ) + return false; + if ("description" in expected || "description" in actual) + return ( + ("description" in expected ? expected.description : undefined) === + ("description" in actual ? actual.description : undefined) + ); + const files = "files" in expected ? (expected.files ?? []) : []; + const nativeFiles = "files" in actual ? (actual.files ?? []) : []; + if ( + files.length !== nativeFiles.length || + files.some((file, i) => { + const native = nativeFiles[i]; + if ( + !native || + file.name !== native.name || + file.description !== native.description || + !same(file.mention, native.mention) + ) + return true; + if (native.source.type === "uri") return native.source.uri !== file.uri; + const match = /^data:([^;,]+);base64,(.*)$/s.exec(file.uri); + return !match || match[1] !== native.mime || match[2] !== native.data; + }) + ) + return false; + const agents = "agents" in expected ? (expected.agents ?? []) : []; + const nativeAgents = "agents" in actual ? (actual.agents ?? []) : []; + if (!same(agents, nativeAgents)) return false; + const skills = "skills" in expected ? (expected.skills ?? []) : []; + const nativeSkills = "skills" in actual ? (actual.skills ?? []) : []; + return ( + skills.length === nativeSkills.length && + skills.every( + (skill, i) => + skill.id === nativeSkills[i]?.id && + same(skill.mention, nativeSkills[i]?.mention), + ) + ); +} diff --git a/src/platform/opencode/lifecycle.ts b/src/platform/opencode/lifecycle.ts new file mode 100644 index 00000000..c8ac2372 --- /dev/null +++ b/src/platform/opencode/lifecycle.ts @@ -0,0 +1,795 @@ +import { isDeepStrictEqual } from "node:util"; +import type { SessionMessageInfo } from "@opencode/client"; +import type { Context } from "@opencode/plugin/promise/plugin"; +import type { Registration } from "@opencode/plugin/promise/registration"; +import type { SessionContext } from "@opencode/plugin/promise/session"; +import type { ToolContext } from "@opencode/plugin/promise/tool"; +import { SessionMessage } from "@opencode/schema/session-message"; +import type { FlowService } from "../../application/flow-service.js"; +import type { + RecoveryController, + RecoverySettings, +} from "../../application/recovery-policy.js"; +import { + FLOW_CORE_COMMANDS, + type FlowCodingModel, +} from "../../config-shared.js"; +import { CompactionBoundary } from "./auto-compaction.js"; +import { + type AutoDriveCoordinator, + type AutoDriveDelivery, + FLOW_AUTO_METADATA_KEY, +} from "./auto-drive.js"; +import { + createCommandHook, + flowCommandName, + nativePromptPayload, + prepareFlowCommand, +} from "./command-hook.js"; +import { + ExecutionAttribution, + type InputOrigin, + type NativeExecution, + nativeDelivery, + nativeInputIDs, + rememberCodingModel, + sameDelivery, +} from "./execution-attribution.js"; +import { InputAdmissions, type InputScope } from "./input-admission.js"; +import type { FlowLeadershipHandle } from "./leadership.js"; + +type LifecycleContext = Readonly<{ + location: Pick; + session: Pick< + Context["session"], + "context" | "get" | "synthetic" | "prompt" | "create" | "hook" + >; + agent: Pick; + command: Pick; + tool: Pick; + event: Context["event"]; +}>; +type LifecycleOptions = Readonly<{ + ctx: LifecycleContext; + autoDrive: AutoDriveCoordinator; + recovery: RecoveryController; + flow: FlowService; + runtimeGuard: FlowLeadershipHandle; + defaultRecovery?: (() => RecoverySettings | null) | undefined; + log( + level: "info" | "warn" | "error", + message: string, + extra?: Readonly>, + ): void; + codingModels: Map; +}>; +type FlowLifecycle = Readonly<{ + beforeTool(nativeToolContext: ToolContext): Promise; + prompt( + sessionID: string, + prompt: string, + delivery: AutoDriveDelivery, + metadata: Readonly>, + ): Promise; + dispose(): Promise; +}>; +type CallbackOwner = Readonly<{ + host: string; + epoch: number; + primary: ReturnType; + kind: "attempt" | "admission"; + input: InputOrigin | undefined; + source: string | undefined; +}>; +const specialists = new Set(["flow-planner", "flow-worker", "flow-reviewer"]); +const errorOf = (error: unknown) => + error instanceof Error ? error : new Error(String(error)); + +export async function registerFlowLifecycle( + options: LifecycleOptions, +): Promise { + const { ctx, autoDrive, recovery, runtimeGuard, codingModels, log } = options; + const registrations: Registration[] = []; + const idle = new Map(); + const outcomes = new Set(); + const contextPending = new Map(); + const controller = new AbortController(); + let closed = false; + let streamLost = false; + let disposal: Promise | null = null; + const epoch = (host: string) => inputs.epoch(host); + const assertOpen = () => { + if (closed || streamLost) + throw new Error( + "Flow lifecycle is unavailable after unload or event-stream loss.", + ); + runtimeGuard.assertOperational("authorize native Flow execution"); + }; + const inputs = new InputAdmissions({ + assertOpen, + authorityCurrent: (authority) => autoDrive.isAuthorityCurrent(authority), + authority: (host) => autoDrive.captureAuthority(host), + observeReal: (host, id, text, delivery, assertCurrent) => + observeAccepted(host, id, text, delivery, null, false, assertCurrent), + }); + const assertLive = (origin: InputOrigin) => inputs.assertLive(origin); + const attribution = new ExecutionAttribution(assertLive); + const revoke = (host: string, deactivate = true) => { + const prior = attribution.current(host); + if (prior) attribution.end(prior); + inputs.invalidate(host); + autoDrive.revokeExecutions(host); + recovery.invalidateOrigins(host); + if (deactivate) { + autoDrive.deactivate(host); + recovery.revoke(host); + } + }; + const captureOwner = ( + host: string, + kind: CallbackOwner["kind"] = "attempt", + primary = attribution.current(host), + expectedEpoch = epoch(host), + input?: InputOrigin, + source = input?.id, + ): CallbackOwner => + Object.freeze({ + host, + kind, + primary, + epoch: expectedEpoch, + input, + source, + }); + const owns = (owner: CallbackOwner, primary = owner.primary) => { + const origin = + owner.input ?? (owner.kind === "attempt" ? primary?.origin : undefined); + return ( + !closed && + !streamLost && + epoch(owner.host) === owner.epoch && + (owner.kind === "admission" || + attribution.current(owner.host) === primary) && + (!origin || inputs.isLive(origin)) + ); + }; + const assertOwned = (owner: CallbackOwner, primary = owner.primary) => { + assertOpen(); + if (!owns(owner, primary)) + throw new Error("Flow execution epoch was revoked."); + }; + const failOwned = (owner: CallbackOwner, primary = owner.primary) => { + if ( + primary?.origin.epoch === owner.epoch && + (!owner.input || primary.origin === owner.input) && + (!owner.source || owns(owner, primary)) + ) + attribution.end(primary); + if (owns(owner, primary)) revoke(owner.host); + }; + const failReservation = ( + reservation: Parameters[0], + owner: CallbackOwner, + ) => { + try { + inputs.assertReservation(reservation); + } catch { + return; + } + failOwned(owner); + }; + const makeOrigin = (...args: Parameters) => + inputs.makeOrigin(...args); + const observeAccepted = async ( + host: string, + id: string, + text: string, + delivery: AutoDriveDelivery, + metadata: Readonly> | null, + synthetic: boolean, + assertCurrent: () => void, + ) => { + assertCurrent(); + const parts = metadata + ? [{ type: "text", text, synthetic: true, metadata }] + : [{ type: "text", text }]; + const result = await autoDrive.observeMessage(host, delivery, parts, id); + assertCurrent(); + if (result === "stale-continuation") + throw new Error("Flow rejected a stale admitted continuation."); + recovery.observeMessage( + host, + id, + synthetic, + result === "accepted-continuation", + ); + }; + const verify = async (execution: NativeExecution, tool?: string) => { + assertOpen(); + const primary = attribution.executionOwner(execution); + if (!primary) + throw new Error("Flow tool has no attributable primary request."); + const owner = captureOwner( + execution.sessionID, + "attempt", + primary, + primary.origin.epoch, + ); + try { + if ( + epoch(owner.host) === owner.epoch && + attribution.current(owner.host) !== primary + ) + throw new Error("Flow assistant cannot be rebound to another request."); + assertOwned(owner); + assertLive(primary.origin); + const [native, session] = await Promise.all([ + ctx.session.context({ sessionID: execution.sessionID }), + ctx.session.get({ sessionID: execution.sessionID }), + ]); + assertOwned(owner); + const verified = attribution.verify( + execution, + native, + session, + ctx.location.directory, + tool, + ); + assertLive(verified.primary.origin); + autoDrive.observeAssistant( + execution.sessionID, + execution.messageID, + verified.primary.origin.id, + ); + recovery.observeAssistant( + execution.sessionID, + execution.messageID, + verified.primary.origin.id, + ); + } catch (error) { + failOwned(owner); + throw error; + } + }; + const commandAccepted = createCommandHook({ + ...options, + assertOperational: (action) => runtimeGuard.assertOperational(action), + }); + const synthetic = async ( + host: string, + text: string, + delivery: AutoDriveDelivery, + metadata: Readonly>, + ) => { + assertOpen(); + const token = metadata[FLOW_AUTO_METADATA_KEY]; + if (typeof token !== "string" || !autoDrive.captureAuthority(host, token)) + throw new Error( + "Flow synthetic dispatch has no live authenticated token.", + ); + if (Object.keys(metadata).some((key) => key !== FLOW_AUTO_METADATA_KEY)) + throw new Error("Flow synthetic metadata has unknown fields."); + const id = SessionMessage.ID.create(); + const reservation = inputs.reserve(host, id); + const expectedEpoch = reservation.epoch; + const owner = captureOwner(host, "admission"); + const authority = autoDrive.captureAuthority(host, token); + if (!authority) throw new Error("Flow synthetic authority expired."); + const assertCurrent = () => { + inputs.assertReservation(reservation); + if ( + epoch(host) !== expectedEpoch || + !autoDrive.isAuthorityCurrent(authority) + ) + throw new Error("Flow synthetic dispatch was superseded."); + }; + const selected = await ctx.session + .get({ sessionID: host }) + .then(nativeDelivery) + .catch((error: unknown) => { + failReservation(reservation, owner); + throw error; + }); + assertCurrent(); + if (!sameDelivery(delivery, selected)) + throw new Error( + "Flow queued continuation cannot change the user's selected agent or model.", + ); + const payload = { text, metadata: { [FLOW_AUTO_METADATA_KEY]: token } }; + const admission = inputs.register(reservation, { + type: "synthetic", + payload, + }); + try { + assertCurrent(); + const receipt = await ctx.session.synthetic( + { sessionID: host, id, ...payload, delivery: "queue", resume: true }, + { signal: controller.signal }, + ); + await inputs.accept(admission, receipt, async () => { + assertCurrent(); + await observeAccepted( + host, + id, + text, + delivery, + payload.metadata, + true, + assertCurrent, + ); + assertCurrent(); + return makeOrigin(host, id, delivery, true, expectedEpoch); + }); + } catch (error) { + inputs.reject(admission, error); + failReservation(reservation, owner); + throw error; + } + }; + const sourceFailure = (scope: InputScope) => { + const origin = inputs.positive(scope); + if (origin) + failOwned( + captureOwner( + scope.host, + "admission", + attribution.current(scope.host), + origin.epoch, + origin, + origin.id, + ), + ); + }; + const compactions = new CompactionBoundary({ + inputs, + readNative: (host) => ctx.session.context({ sessionID: host }), + failure: sourceFailure, + hasActivePrimary: (host) => { + const current = attribution.current(host); + return current !== undefined && current.phase !== "ended"; + }, + guidance: (host) => autoDrive.compactionContext(host), + observe: (host, origin, source, id) => { + if ( + origin.authority && + !autoDrive.isAuthorityCurrent(origin.authority, id) + ) + autoDrive.observeCompaction( + origin.authority.hostSessionId, + origin.authority, + source, + id, + ); + if (!origin.authority) recovery.observeMessage(host, id, true, true); + }, + }); + const resolveOrigin = async ( + input: SessionContext, + native: readonly SessionMessageInfo[], + scope: InputScope, + ): Promise => { + const completed = compactions.completed(input, native); + if (completed) return completed; + const evidence = await inputs.evidence( + scope, + native, + nativeDelivery(input), + ); + if (evidence.kind !== "live") + throw new Error( + "Flow context has an unknown, synthetic, or stale real input.", + ); + assertLive(evidence.origin); + return evidence.origin; + }; + try { + registrations.push( + await ctx.session.hook("prompt", (input) => { + assertOpen(); + if (inputs.hasPrivate(input.sessionID, input.messageID)) return; + if (input.metadata?.[FLOW_AUTO_METADATA_KEY] !== undefined) + throw new Error("Flow tokens require a private admitted dispatch."); + inputs.observePrompt(input, () => revoke(input.sessionID, false)); + }), + ); + registrations.push( + await ctx.command.transform((editor) => { + for (const name of Object.keys(FLOW_CORE_COMMANDS)) { + if (!Object.hasOwn(FLOW_CORE_COMMANDS, name)) continue; + const commandName = flowCommandName(name); + editor.add({ + name, + description: FLOW_CORE_COMMANDS[commandName].description, + execute: async (input) => { + assertOpen(); + const command = prepareFlowCommand( + commandName, + input.prompt.text, + ); + revoke(input.sessionID, false); + const id = SessionMessage.ID.create(); + const reservation = inputs.reserve(input.sessionID, id); + const expectedEpoch = reservation.epoch; + const owner = captureOwner(input.sessionID, "admission"); + const assertCurrent = () => { + inputs.assertReservation(reservation); + if (epoch(input.sessionID) !== expectedEpoch) + throw new Error("Flow command was superseded."); + }; + const session = await ctx.session + .get({ sessionID: input.sessionID }) + .catch((error: unknown) => { + failReservation(reservation, owner); + throw error; + }); + assertCurrent(); + if ( + session.parentID || + !session.agent || + specialists.has(session.agent) + ) + throw new Error( + "Flow commands require the native primary manager.", + ); + const delivery = nativeDelivery(session); + const invocationToken = + command.name === "flow-auto" && !command.stopping + ? autoDrive.createInvocationToken() + : undefined; + const payload = { + ...nativePromptPayload({ ...input.prompt, text: command.text }), + ...(invocationToken === undefined + ? {} + : { + metadata: { [FLOW_AUTO_METADATA_KEY]: invocationToken }, + }), + }; + const admission = inputs.register(reservation, { + type: "user", + payload, + }); + try { + assertCurrent(); + const receipt = await ctx.session.prompt( + { + sessionID: input.sessionID, + id, + ...payload, + delivery: input.delivery, + resume: !command.stopping && command.name !== "flow-review", + }, + { signal: controller.signal }, + ); + await inputs.accept(admission, receipt, async () => { + assertCurrent(); + const metadata = await commandAccepted( + command, + input.sessionID, + assertCurrent, + invocationToken, + ); + assertCurrent(); + await observeAccepted( + input.sessionID, + id, + command.text, + delivery, + metadata, + false, + assertCurrent, + ); + assertCurrent(); + return makeOrigin( + input.sessionID, + id, + delivery, + metadata !== null, + expectedEpoch, + ); + }); + if (command.name === "flow-review") { + const reviewer = await ctx.agent.get({ + agentID: "flow-reviewer", + }); + assertCurrent(); + const child = await ctx.session.create({ + parentID: session.id, + agent: "flow-reviewer", + title: "Flow review", + location: { directory: ctx.location.directory }, + ...((reviewer.data.model ?? session.model) + ? { model: reviewer.data.model ?? session.model } + : {}), + }); + assertCurrent(); + if ( + child.parentID !== session.id || + child.agent !== "flow-reviewer" + ) + throw new Error( + "Flow reviewer child identity was not confirmed.", + ); + await ctx.session.prompt( + { sessionID: child.id, ...payload, delivery: "queue" }, + { signal: controller.signal }, + ); + } + } catch (error) { + inputs.reject(admission, error); + failReservation(reservation, owner); + throw error; + } + }, + }); + } + }), + ); + registrations.push( + await ctx.session.hook("context", async (input) => { + assertOpen(); + const scope = inputs.capture(input); + const pending = contextPending.get(input.sessionID); + if (pending?.current()) { + sourceFailure(scope); + scope.release(); + throw new Error("Flow primary context callbacks overlap."); + } + contextPending.set(input.sessionID, scope); + const prior = attribution.current(input.sessionID); + let frozen: ReturnType | undefined; + let origin = inputs.positive(scope); + try { + await scope.ready(); + origin = inputs.positive(scope); + const native = await scope.wait( + ctx.session.context({ sessionID: input.sessionID }), + ); + if ( + prior && + prior.phase !== "ended" && + !attribution.retry(prior, native) + ) + throw new Error("Flow primary contexts or streams overlap."); + if (!isDeepStrictEqual(scope.consumed, nativeInputIDs(native))) + throw new Error( + "Flow native inputs differ from the model-visible prefix.", + ); + origin = await resolveOrigin(input, native, scope); + scope.assertCurrent(); + frozen = attribution.freeze(input, native, origin); + const session = await scope.wait( + ctx.session.get({ sessionID: input.sessionID }), + ); + assertLive(origin); + rememberCodingModel(codingModels, input, session, specialists); + } catch (error) { + if (frozen) attribution.end(frozen); + if (origin && scope.current()) + failOwned( + captureOwner( + input.sessionID, + "attempt", + frozen ?? prior, + origin.epoch, + origin, + origin.id, + ), + frozen ?? prior, + ); + throw error; + } finally { + if (contextPending.get(input.sessionID) === scope) + contextPending.delete(input.sessionID); + scope.release(); + } + }), + ); + registrations.push( + await ctx.session.hook("compaction", async (input) => { + const result = await compactions.admit(input); + if (result.kind === "continuation") { + const guidance = autoDrive.compactionContext(input.sessionID); + if (guidance) input.system.push({ type: "text", text: guidance }); + } + }), + ); + registrations.push( + await ctx.session.hook("http.request", (input) => { + const primary = + attribution.requestOwner(input.request) ?? + attribution.current(input.sessionID); + const owner = captureOwner( + input.sessionID, + "attempt", + primary, + primary?.origin.epoch ?? epoch(input.sessionID), + ); + try { + attribution.request(input); + } catch (error) { + failOwned(owner); + throw error; + } + }), + ); + registrations.push( + await ctx.session.hook("http.response", (input) => { + const primary = + attribution.requestOwner(input.request) ?? + attribution.current(input.sessionID); + const owner = captureOwner( + input.sessionID, + "attempt", + primary, + primary?.origin.epoch ?? epoch(input.sessionID), + ); + attribution.response(input, () => failOwned(owner)); + }), + ); + registrations.push( + await ctx.session.hook("experimental.ws.handshake", (input) => { + if (input.kind !== "primary") return; + const primary = attribution.current(input.sessionID); + failOwned( + captureOwner( + input.sessionID, + "attempt", + primary, + primary?.origin.epoch ?? epoch(input.sessionID), + ), + ); + throw new Error( + "Flow request attribution has not been proved for WebSocket transports.", + ); + }), + ); + registrations.push( + await ctx.tool.hook("execute.before", async (input) => { + const current = attribution.current(input.sessionID); + const executionOwner = attribution.executionOwner(input); + const staleAssistant = + current !== undefined && executionOwner === undefined; + const protectedAutomatic = Boolean( + current?.origin.authority || executionOwner?.origin.authority, + ); + if ( + !input.tool.startsWith("flow_") && + !protectedAutomatic && + !staleAssistant + ) + return; + await verify(input, input.tool); + }), + ); + } catch (error) { + closed = true; + inputs.clear(); + attribution.clear(); + autoDrive.clear(); + recovery.revoke(); + for (const registration of registrations.reverse()) + await registration.dispose(); + throw error; + } + const subscription = (async () => { + try { + for await (const event of ctx.event.subscribe({ + signal: controller.signal, + })) { + if (closed) break; + if ( + event.type === "session.execution.failed" || + event.type === "session.execution.interrupted" || + event.type === "session.deleted" + ) { + if (outcomes.has(event.id)) continue; + outcomes.add(event.id); + const host = event.data.sessionID; + if (event.type === "session.deleted") { + revoke(host); + codingModels.delete(host); + } else { + const primary = attribution.current(host); + if (!primary) continue; + const owner = captureOwner( + host, + "attempt", + primary, + primary.origin.epoch, + ); + try { + const native = await ctx.session.context({ sessionID: host }); + if (owns(owner) && attribution.hasFailedTerminal(primary, native)) + failOwned(owner); + } catch (error) { + failOwned(owner); + log("warn", "Flow terminal observation failed.", { + host, + message: errorOf(error).message, + }); + } + } + continue; + } + if ( + event.type !== "session.execution.succeeded" && + event.type !== "session.idle" + ) + continue; + const host = event.data.sessionID; + const primary = attribution.current(host); + if (!primary) continue; + const owner = captureOwner( + host, + "attempt", + primary, + primary.origin.epoch, + ); + try { + const session = await ctx.session.get({ sessionID: host }); + if (!owns(owner)) continue; + if (session.id !== host) + throw new Error("Flow native terminal session identity changed."); + if (session.time.idle === undefined || session.outcome === undefined) + continue; + const watermark = String(session.time.idle); + if (idle.get(host) === watermark) continue; + idle.set(host, watermark); + if ( + session.outcome !== "succeeded" || + !runtimeGuard.query().operational + ) { + failOwned(owner); + continue; + } + await autoDrive.onIdle(host); + } catch (error) { + failOwned(owner); + log("warn", "Flow terminal observation failed.", { + host, + message: errorOf(error).message, + }); + } + } + if (!closed) throw new Error("Flow event stream ended unexpectedly."); + } catch (error) { + if (!closed) { + streamLost = true; + inputs.clear(); + attribution.clear(); + autoDrive.clear(); + recovery.revoke(); + log("error", "Flow event stream failed closed.", { + message: errorOf(error).message, + }); + } + } + })(); + return { + beforeTool: (context) => verify(context), + prompt: synthetic, + dispose: () => { + disposal ??= (async () => { + closed = true; + controller.abort(); + inputs.clear(); + attribution.clear(); + autoDrive.clear(); + recovery.revoke(); + codingModels.clear(); + await subscription; + const errors: unknown[] = []; + for (const registration of registrations.reverse()) { + try { + await registration.dispose(); + } catch (error) { + errors.push(error); + } + } + if (errors.length) + throw new AggregateError(errors, "Flow lifecycle cleanup failed."); + })(); + return disposal; + }, + }; +} diff --git a/src/platform/opencode/logging.ts b/src/platform/opencode/logging.ts index c83082a3..8d51b08f 100644 --- a/src/platform/opencode/logging.ts +++ b/src/platform/opencode/logging.ts @@ -1,52 +1,21 @@ type FlowLogLevel = "info" | "warn" | "error"; const MAX_FLOW_LOG_EXTRA_BYTES = 8 * 1024; -function boundedLogExtra( - extra: Readonly> | undefined, -): Record | undefined { - if (!extra) return undefined; - try { - const serialized = JSON.stringify(extra); - if ( - new TextEncoder().encode(serialized).byteLength > MAX_FLOW_LOG_EXTRA_BYTES - ) { - return undefined; - } - const parsed = JSON.parse(serialized); - return parsed && typeof parsed === "object" && !Array.isArray(parsed) - ? (parsed as Record) - : undefined; - } catch { - return undefined; - } -} - -export function createFlowLog(ctx: unknown) { - const client = (ctx as { client?: { app?: { log?: unknown } } } | null) - ?.client; - const log = client?.app?.log; +export function createFlowLog(_ctx?: unknown) { return ( level: FlowLogLevel, message: string, extra?: Readonly>, ): void => { - if (typeof log !== "function") return; try { - const boundedExtra = boundedLogExtra(extra); - Promise.resolve( - log.call(client?.app, { - body: { - service: "opencode-plugin-flow", - level, - message, - ...(boundedExtra ? { extra: boundedExtra } : {}), - }, - }), - ).catch(() => { - // Logging is best-effort; a failed transport must stay silent. - }); - } catch { - // Logging is best-effort and must never break plugin startup. - } + const serialized = extra ? JSON.stringify(extra) : undefined; + const suffix = + serialized && Buffer.byteLength(serialized) <= MAX_FLOW_LOG_EXTRA_BYTES + ? ` ${serialized}` + : ""; + process.stderr.write( + `[opencode-plugin-flow] [${level}] ${message}${suffix}\n`, + ); + } catch {} }; } diff --git a/src/platform/opencode/model-picker-ui.ts b/src/platform/opencode/model-picker-ui.ts new file mode 100644 index 00000000..39e75e28 --- /dev/null +++ b/src/platform/opencode/model-picker-ui.ts @@ -0,0 +1,186 @@ +import type { LocationRef, ProviderInfo } from "@opencode/client"; +import type { Dialog, Toast } from "@opencode/plugin/tui/context"; +import type { + FlowModelRole, + PickerChoice, + PickerSnapshot, + SaveModelPreference, + SaveModelPreferenceResult, +} from "../../application/model-preferences.js"; +import { + choiceKey, + FLOW_MODEL_ROLES, + modelChoices, + type PickerCatalogModel, + sameChoice, + sameProject, +} from "./model-picker.js"; + +export interface ModelPickerHost { + location(): LocationRef; + read(signal: AbortSignal): Promise; + save( + input: SaveModelPreference, + location: LocationRef, + signal: AbortSignal, + ): Promise; + catalog( + location: LocationRef, + signal: AbortSignal, + ): Promise<{ + models: readonly PickerCatalogModel[]; + providers: readonly Pick[]; + }>; + busy(signal: AbortSignal): Promise; + dialog: Pick; + toast: Toast; +} +const refusalMessages = { + busy: "Wait for active work to finish before changing Flow models.", + "stale-preference": "Flow model preferences changed. Open the picker again.", + "stale-project": "Project model preferences changed. Open the picker again.", + "unavailable-model": + "That model is no longer available from a connected provider.", + unloaded: "Flow model preferences have unloaded.", +}; +export function createModelPicker(host: ModelPickerHost) { + const controller = new AbortController(); + let open = false; + const sameLocation = (location: LocationRef) => { + const current = host.location(); + return ( + current.directory === location.directory && + current.workspaceID === location.workspaceID + ); + }; + return { + async open(): Promise { + if (open || controller.signal.aborted) return; + open = true; + try { + const location = host.location(); + const role = await host.dialog.select({ + title: "Flow model settings", + options: (["planning", "review"] as const).map((value) => ({ + title: value === "planning" ? "Planning specialist" : "Reviewer", + value, + description: FLOW_MODEL_ROLES[value].defaultDescription, + })), + }); + if (!role || controller.signal.aborted) return; + if (!sameLocation(location)) + throw new Error("Project changed. Open the picker again."); + const [snapshot, catalog] = await Promise.all([ + host.read(controller.signal), + host.catalog(location, controller.signal), + ]); + if (controller.signal.aborted) return; + if (!sameLocation(location)) + throw new Error("Project changed. Open the picker again."); + const setting = FLOW_MODEL_ROLES[role]; + const current = snapshot.global[role]; + const options = [ + { + title: "Use default", + value: { kind: "default" } satisfies PickerChoice, + category: "Default", + description: setting.defaultDescription, + }, + ...modelChoices(catalog.models, catalog.providers), + ]; + const selected = await host.dialog.select({ + title: `Flow ${setting.title} · global default`, + placeholder: "Search connected models", + current: + options.find((option) => sameChoice(option.value, current)) + ?.value ?? current, + options: [ + ...(current.kind === "model" && + !options.some((option) => sameChoice(option.value, current)) + ? [ + { + title: choiceKey(current), + value: current, + category: "Current preference", + description: + "Unavailable here; reconnect or choose another model", + disabled: true, + }, + ] + : []), + ...options, + ], + }); + if ( + !selected || + controller.signal.aborted || + sameChoice(current, selected) + ) + return; + const override = snapshot.project[role]; + const confirmed = await host.dialog.confirm({ + title: `Save Flow ${setting.title}?`, + message: `${choiceKey(selected) || "Use configured default"}\n\nChanges the global Flow ${setting.title}. Finish active work in all projects first. ${override ? `This project overrides the global setting with ${choiceKey(override) || "its role default"}. ` : ""}Project options take precedence. Picker models use default reasoning. Use default restores the role's configured model and variant. Your coding model stays unchanged.`, + }); + if (!confirmed || controller.signal.aborted) return; + if (!sameLocation(location)) + throw new Error("Project changed. Open the picker again."); + const [latest, available, busy] = await Promise.all([ + host.read(controller.signal), + host.catalog(location, controller.signal), + host.busy(controller.signal), + ]); + if (controller.signal.aborted) return; + if (!sameLocation(location)) + throw new Error("Project changed. Open the picker again."); + if (busy) throw new Error(refusalMessages.busy); + if (latest.revision !== snapshot.revision) + throw new Error(refusalMessages["stale-preference"]); + if (!sameProject(latest.project, snapshot.project)) + throw new Error(refusalMessages["stale-project"]); + if ( + selected.kind === "model" && + !modelChoices(available.models, available.providers).some((option) => + sameChoice(option.value, selected), + ) + ) + throw new Error(refusalMessages["unavailable-model"]); + const result = await host.save( + { + role, + expectedRevision: snapshot.revision, + expectedProject: snapshot.project, + choice: selected, + }, + location, + controller.signal, + ); + if (controller.signal.aborted) return; + if (result.kind === "refused") + throw new Error(refusalMessages[result.reason]); + host.toast.show({ + variant: "success", + title: `Flow ${setting.title} saved`, + message: + "Global preference saved. Use /flow-status to check the effective models.", + }); + } catch (error) { + if (!controller.signal.aborted) + host.toast.show({ + variant: "error", + title: "Flow models", + message: + error instanceof Error + ? error.message + : "Could not update Flow model settings.", + }); + } finally { + open = false; + } + }, + dispose() { + controller.abort(); + if (open) host.dialog.clear(); + }, + }; +} diff --git a/src/platform/opencode/model-picker.ts b/src/platform/opencode/model-picker.ts index 639c89ac..1aa0cb96 100644 --- a/src/platform/opencode/model-picker.ts +++ b/src/platform/opencode/model-picker.ts @@ -1,77 +1,101 @@ +import type { ModelInfo, ProviderInfo } from "@opencode/client"; +import type { + EffectivePreference, + FlowModelRole, + PickerChoice, + ProjectPreferences, + RoleChoices, +} from "../../application/model-preferences.js"; import type { FlowReviewerConfiguration } from "../../config-shared.js"; -import type { Provider } from "./sdk.js"; - -export function modelChoices( - providers: readonly Provider[], - connected: readonly string[], -) { - const enabled = new Set(connected); - return providers - .filter((provider) => enabled.has(provider.id)) - .flatMap((provider) => - Object.values(provider.models) - .filter( - (model) => - model.capabilities.toolcall && - model.capabilities.input.text && - model.capabilities.output.text && - model.status !== "deprecated", - ) - .map((model) => ({ - title: model.name, - value: `${provider.id}/${model.id}`, - category: provider.name, - description: `${provider.id}/${model.id}`, - })), - ) - .sort( - (a, b) => - a.category.localeCompare(b.category) || - a.title.localeCompare(b.title) || - a.value.localeCompare(b.value), - ); -} +export type { FlowModelRole } from "../../application/model-preferences.js"; export const FLOW_MODEL_ROLES = { planning: { - agent: "flow-planner", - key: "flowPlanningModel", title: "planning specialist", defaultDescription: "The coding manager plans directly, without a specialist", }, review: { - agent: "flow-reviewer", - key: "flowReviewerModel", title: "reviewer", defaultDescription: "Existing plugin/environment settings, otherwise the coding model", }, } as const; -export type FlowModelRole = keyof typeof FLOW_MODEL_ROLES; - -export function modelPreference( - config: { agent?: Record }, +export type PickerCatalogModel = Pick< + ModelInfo, + "id" | "providerID" | "name" | "enabled" | "status" +> & { + capabilities: Pick; +}; +export function choiceKey(choice: PickerChoice): string { + return choice.kind === "default" + ? "" + : `${choice.model.providerID}/${choice.model.id}`; +} +export function sameChoice(left: PickerChoice, right: PickerChoice) { + return left.kind === "default" + ? right.kind === "default" + : right.kind === "model" && + left.model.providerID === right.model.providerID && + left.model.id === right.model.id; +} +export function sameProject( + left: ProjectPreferences, + right: ProjectPreferences, +) { + return (["planning", "review"] as const).every((role) => { + const a = left[role]; + const b = right[role]; + return a === undefined + ? b === undefined + : b !== undefined && sameChoice(a, b); + }); +} +export function resolvePreference( + global: RoleChoices, + project: ProjectPreferences, role: FlowModelRole, -): string | undefined { - const setting = FLOW_MODEL_ROLES[role]; - const agent = config.agent?.[setting.agent]; - if (!agent || typeof agent !== "object" || !("options" in agent)) - return undefined; - const options = agent.options; - if (!options || typeof options !== "object" || !(setting.key in options)) - return undefined; - const value = (options as Record)[setting.key]; - if (typeof value !== "string") - throw new Error(`Flow ${setting.title} preference must be a string.`); - return value.trim(); +): EffectivePreference { + const override = project[role]; + if (override) return { choice: override, source: "project" }; + const choice = global[role]; + return { + choice, + source: choice.kind === "model" ? "global-picker" : "role-default", + }; } - -export function modelPreferencePatch(role: FlowModelRole, model: string) { - const setting = FLOW_MODEL_ROLES[role]; - return { agent: { [setting.agent]: { options: { [setting.key]: model } } } }; +export function modelChoices( + models: readonly PickerCatalogModel[], + providers: readonly Pick[], +) { + const names = new Map( + providers.map((provider) => [provider.id, provider.name]), + ); + return models + .filter( + (model) => + model.enabled && + model.status !== "deprecated" && + model.capabilities.tools && + model.capabilities.input.includes("text") && + model.capabilities.output.includes("text"), + ) + .map((model) => ({ + title: model.name, + value: { + kind: "model", + model: { providerID: model.providerID, id: model.id }, + } satisfies PickerChoice, + category: names.get(model.providerID) ?? model.providerID, + description: `${model.providerID}/${model.id}`, + })) + .sort( + (a, b) => + a.category.localeCompare(b.category) || + a.title.localeCompare(b.title) || + a.description.localeCompare(b.description), + ); } - export function applyReviewerPreference( base: FlowReviewerConfiguration, model: string | undefined, diff --git a/src/platform/opencode/model-preferences.ts b/src/platform/opencode/model-preferences.ts new file mode 100644 index 00000000..551c556a --- /dev/null +++ b/src/platform/opencode/model-preferences.ts @@ -0,0 +1,304 @@ +import type { Plugin } from "@opencode/plugin"; +import type { + RpcHandlers, + RpcRegistration, +} from "@opencode/plugin/promise/rpc"; +import type { + PickerSnapshot, + SaveModelPreferenceResult, + StoredModelPreferences, +} from "../../application/model-preferences.js"; +import { + projectPreferencesSchema, + savePreferenceSchema, +} from "../../application/model-preferences-schema.js"; +import type { FlowReviewerConfiguration } from "../../config-shared.js"; +import { FlowModelPreferencesRpc } from "../../flow-rpc.js"; +import { createModelPreferenceStore } from "../../infrastructure/fs/model-preference-store.js"; +import { + applyReviewerPreference, + choiceKey, + modelChoices, + resolvePreference, + sameChoice, + sameProject, +} from "./model-picker.js"; + +export type FlowModelPreferencesContext = Pick< + Plugin.Context, + "location" | "options" | "event" +> & { + readonly model: Pick; + readonly rpc: { + register( + definition: typeof FlowModelPreferencesRpc, + handlers: RpcHandlers, + ): Promise>; + }; + readonly session: Pick< + Plugin.Context["session"], + "hook" | "list" | "context" + >; +}; +export interface FlowModelPreferences { + planningModel(): string | undefined; + reviewerConfiguration( + base: FlowReviewerConfiguration, + ): FlowReviewerConfiguration; + dispose(): Promise; +} + +export async function createFlowModelPreferences( + ctx: FlowModelPreferencesContext, + { onChange }: { onChange: () => Promise | void }, +): Promise { + const store = await createModelPreferenceStore(); + let state: StoredModelPreferences; + let disposed = false; + let ready = false; + let failure: unknown; + let refreshing = Promise.resolve(); + const pending = new Set(); + const writes = new Set>(); + let activityGeneration = 0; + const project = () => + projectPreferencesSchema.parse(ctx.options.modelPreferences ?? {}); + const assertLoaded = () => { + if (disposed) throw new Error("Flow model preferences have unloaded."); + if (failure) throw failure; + }; + const snapshot = (current: StoredModelPreferences): PickerSnapshot => { + const overrides = project(); + return { + location: { + directory: ctx.location.directory, + ...(ctx.location.workspaceID + ? { workspaceID: ctx.location.workspaceID } + : {}), + }, + revision: current.revision, + global: current.roles, + project: overrides, + effective: { + planning: resolvePreference(current.roles, overrides, "planning"), + review: resolvePreference(current.roles, overrides, "review"), + }, + }; + }; + const refresh = () => { + const next = refreshing.then(async () => { + if (disposed) return; + const latest = await store.read(); + if (disposed || latest.revision === state?.revision) return; + state = latest; + if (ready) await onChange(); + }); + refreshing = next.catch((error) => { + failure = error; + }); + return next; + }; + const stopWatch = store.watch( + () => { + void refresh().catch(() => {}); + }, + (error) => { + failure = error; + }, + ); + try { + state = await store.read(); + project(); + await refreshing; + } catch (error) { + stopWatch(); + throw error; + } + const prompt = await ctx.session + .hook("prompt", () => { + activityGeneration++; + }) + .catch((error) => { + stopWatch(); + throw error; + }); + const eventsAbort = new AbortController(); + const events = (async () => { + try { + for await (const event of ctx.event.subscribe({ + signal: eventsAbort.signal, + })) { + if (event.type === "session.execution.started") { + activityGeneration++; + pending.add(event.data.sessionID); + } else if ( + event.type === "session.execution.succeeded" || + event.type === "session.execution.failed" || + event.type === "session.execution.interrupted" || + event.type === "session.deleted" + ) { + activityGeneration++; + pending.delete(event.data.sessionID); + } + } + if (!disposed) + failure = new Error( + "Flow cannot verify session activity after the event stream ended.", + ); + } catch (error) { + if (!disposed) failure = error; + } + })(); + const busy = async () => { + if (pending.size) return true; + let cursor: string | undefined; + do { + const page = await ctx.session.list({ + ...(cursor ? { cursor } : {}), + limit: 100, + }); + for (const session of page.data) { + const messages = await ctx.session.context({ sessionID: session.id }); + const last = messages.at(-1); + if (!last) { + if (session.time.updated > session.time.created) return true; + } else if ( + session.time.idle === undefined || + session.time.idle < session.time.updated || + last.time.created > session.time.idle + ) + return true; + } + cursor = page.cursor.next ?? undefined; + } while (cursor); + return pending.size > 0; + }; + let registration: RpcRegistration; + try { + registration = await ctx.rpc.register(FlowModelPreferencesRpc, { + async read() { + assertLoaded(); + await refresh(); + assertLoaded(); + return snapshot(state); + }, + async set(raw, call): Promise { + const input = savePreferenceSchema.parse(raw); + if (disposed || call.signal.aborted) + return { kind: "refused", reason: "unloaded" }; + assertLoaded(); + const write = store.update( + async (current) => { + const refused = ( + reason: Extract< + SaveModelPreferenceResult, + { kind: "refused" } + >["reason"], + ) => ({ value: { kind: "refused", reason } as const }); + if (disposed || call.signal.aborted) return refused("unloaded"); + if (current.revision !== input.expectedRevision) + return refused("stale-preference"); + if (!sameProject(project(), input.expectedProject)) + return refused("stale-project"); + const generation = activityGeneration; + const [models, active] = await Promise.all([ + ctx.model.list(), + busy(), + ]); + assertLoaded(); + if (disposed || call.signal.aborted) return refused("unloaded"); + if (active || pending.size || generation !== activityGeneration) + return refused("busy"); + if (!sameProject(project(), input.expectedProject)) + return refused("stale-project"); + if ( + input.choice.kind === "model" && + !modelChoices(models.data, []).some((option) => + sameChoice(option.value, input.choice), + ) + ) + return refused("unavailable-model"); + if (sameChoice(current.roles[input.role], input.choice)) + return { + value: { kind: "unchanged", snapshot: snapshot(current) }, + }; + const next = { + ...current, + revision: current.revision + 1, + roles: { ...current.roles, [input.role]: input.choice }, + }; + return { + value: { kind: "saved", snapshot: snapshot(next) }, + next, + beforeCommit() { + assertLoaded(); + if ( + call.signal.aborted || + pending.size || + generation !== activityGeneration || + !sameProject(project(), input.expectedProject) + ) + throw new Error( + "Flow model preference confirmation became stale. Open the picker again.", + ); + }, + }; + }, + ); + writes.add(write); + try { + const result = await write; + await refresh(); + return result; + } finally { + writes.delete(write); + } + }, + }); + } catch (error) { + disposed = true; + stopWatch(); + eventsAbort.abort(); + await Promise.allSettled([prompt.dispose(), events, refreshing]); + throw error; + } + await refreshing; + ready = true; + return { + planningModel(): string | undefined { + const choice = resolvePreference( + state.roles, + project(), + "planning", + ).choice; + return choiceKey(choice) || undefined; + }, + reviewerConfiguration( + base: FlowReviewerConfiguration, + ): FlowReviewerConfiguration { + const choice = resolvePreference(state.roles, project(), "review").choice; + return applyReviewerPreference(base, choiceKey(choice) || undefined); + }, + async dispose(): Promise { + if (disposed) return; + disposed = true; + stopWatch(); + eventsAbort.abort(); + const results = await Promise.allSettled([ + registration.dispose(), + prompt.dispose(), + events, + refreshing, + ...writes, + ]); + const errors = results + .filter((result) => result.status === "rejected") + .map((result) => result.reason); + if (errors.length) + throw new AggregateError( + errors, + "Flow model preference cleanup failed.", + ); + }, + }; +} diff --git a/src/platform/opencode/plugin-composition.ts b/src/platform/opencode/plugin-composition.ts index e2bb7bd2..8f4eb194 100644 --- a/src/platform/opencode/plugin-composition.ts +++ b/src/platform/opencode/plugin-composition.ts @@ -19,18 +19,20 @@ import { readWorkspaceTestReport, } from "../../infrastructure/fs/workspace-validation.js"; import { resolveFlowPluginVersion } from "../../version.js"; -import { AutoDriveCoordinator, autoDriveDelivery } from "./auto-drive.js"; -import { createCommandHook, textPart } from "./command-hook.js"; -import { createConfigHook } from "./config.js"; +import { AutoDriveCoordinator } from "./auto-drive.js"; +import { registerFlowAgents } from "./config.js"; +import { registerHttpTransport } from "./http-transport.js"; import { createFlowPluginInstanceId, FLOW_LEADERSHIP_PROTOCOL_VERSION, registerFlowPluginInstance, } from "./leadership.js"; +import { registerFlowLifecycle } from "./lifecycle.js"; import { createFlowLog } from "./logging.js"; -import type { Hooks, Plugin } from "./sdk.js"; +import { createFlowModelPreferences } from "./model-preferences.js"; +import { Plugin } from "./sdk.js"; import { guardTools } from "./tool-guard.js"; -import { createTools } from "./tools.js"; +import { createTools, registerFlowTools } from "./tools.js"; import { ValidationCaptureCoordinator } from "./validation-capture.js"; const JevModel = z @@ -47,254 +49,240 @@ export function createFlowPlugin(dependencies: { createRecovery(): RecoveryController; defaultRecovery?(): RecoverySettings | null; entryUrl: string; -}): Plugin { - return async (ctx, pluginOptions) => { - const log = createFlowLog(ctx); - let reviewerConfiguration = resolveFlowReviewerConfiguration({ - pluginOptions, - onWarning: (warning) => log("warn", warning), - }); - let planningModel: string | undefined; - const codingModels = new Map(); - const version = resolveFlowPluginVersion(); - const pluginEntrySha256 = `sha256:${createHash("sha256") - .update(await readFile(fileURLToPath(dependencies.entryUrl))) - .digest("hex")}`; - let workspace: string; - try { - workspace = resolveWorkspaceRoot(ctx); - } catch (error) { - const message = error instanceof Error ? error.message : String(error); - log("error", `Flow ${version} cannot start here: ${message}`); - throw error; - } - const runtimeGuard = registerFlowPluginInstance(workspace, { - packageName: "opencode-plugin-flow", - version, - protocolVersion: FLOW_LEADERSHIP_PROTOCOL_VERSION, - instanceId: createFlowPluginInstanceId(), - }); - const initial = runtimeGuard.query(); - const level = initial.operational ? "info" : "error"; - log(level, `Flow ${version}: ${initial.message}`); - const recovery = dependencies.createRecovery(); - const flow = createWorkspaceFlowService(workspace); - const autoDrive = new AutoDriveCoordinator({ - recovery, - readSourceDigest: () => - createFileSourceIdentityProvider(workspace).computeSourceDigest(), - readProjection: async () => { - const response = await flow.status({ request: { view: "compact" } }); - if (response.status !== "ok") throw new Error(response.summary); - const projection = response.workflowData.projection; - if (projection.view !== "compact") - throw new Error("Flow auto-drive received a non-compact projection."); - return { - sessionId: - "sessionId" in projection ? projection.sessionId : undefined, - status: projection.status, - revision: projection.revision, - nextAction: projection.nextAction, - ...("prerequisiteRepair" in projection && - projection.prerequisiteRepair - ? { prerequisiteRepair: projection.prerequisiteRepair } - : {}), - }; - }, - prompt: async (sessionID, prompt, delivery, metadata) => { - await ctx.client.session.promptAsync({ - path: { id: sessionID }, - query: { directory: ctx.directory }, - body: { - agent: delivery.agent, - model: delivery.model, - ...(delivery.variant === undefined - ? {} - : { variant: delivery.variant }), - parts: [textPart(prompt, true, metadata)], - }, - throwOnError: true, - }); - }, - onWarning: (message) => log("warn", message), - }); - const validation = new ValidationCaptureCoordinator({ - persistObservation: persistWorkspaceValidation, - readReport: readWorkspaceTestReport, - onRecorded: (receipt) => autoDrive.observeValidation(receipt), - onReceiptError: (error) => - log("warn", "Flow continuation receipt was not accepted.", { - message: String(error), - }), - }); - const tools = createTools({ - recovery, - onRecoveryOutcome: async (outcome) => { - const mismatch = ModelMismatch.safeParse(outcome); - if (!mismatch.success) return; - await ctx.client.tui.showToast({ - body: { - title: "Jev model changed", - message: `Requested ${mismatch.data.requestedModel}, received ${mismatch.data.model}. Recovery remains blocked pending qualification.`, - variant: "warning", - duration: 15000, - }, - query: { directory: ctx.directory }, - }); - }, - validation, - prepareValidation: prepareWorkspaceValidation, - autoTimingSnapshot: () => autoDrive.timingSnapshot(), - autoContinuationSnapshot: (host) => autoDrive.continuationSnapshot(host), - validationOrigin: (host, assistant, prepared) => - autoDrive.validationOrigin(host, assistant, prepared), - autoContinuationSupport: () => autoDrive.continuationSupport(), - readReviewerConfiguration: () => reviewerConfiguration, - readPlanningModel: () => planningModel, - readCodingModel: (sessionID) => codingModels.get(sessionID), - runtimeIdentity: { packageVersion: version, pluginEntrySha256 }, - }); - return { - config: createConfigHook(ctx, { - assertOperational: (action) => runtimeGuard.assertOperational(action), - reviewerConfiguration, - onPlanningModel: (model) => { - planningModel = model; - }, - onReviewerConfiguration: (configuration) => { - reviewerConfiguration = configuration; - }, - }), - tool: guardTools(tools, runtimeGuard, autoDrive, async (sessionID) => { - const response = await ctx.client.session.get({ - path: { id: sessionID }, - query: { directory: ctx.directory }, - throwOnError: true, - }); - return response.data; - }), - "command.execute.before": createCommandHook({ - assertOperational: (action) => runtimeGuard.assertOperational(action), - autoDrive, - flow, +}): Plugin.Plugin { + return Plugin.define({ + id: "opencode-plugin-flow", + async setup(ctx) { + const log = createFlowLog(ctx); + const baseReviewer = resolveFlowReviewerConfiguration({ + pluginOptions: ctx.options, + onWarning: (warning) => log("warn", warning), + }); + let reviewerConfiguration = baseReviewer; + let planningModel: string | undefined; + const codingModels = new Map(); + const version = resolveFlowPluginVersion(); + const pluginEntrySha256 = `sha256:${createHash("sha256") + .update(await readFile(fileURLToPath(dependencies.entryUrl))) + .digest("hex")}`; + const workspace = resolveWorkspaceRoot({ + directory: ctx.location.directory, + }); + const runtimeGuard = registerFlowPluginInstance(workspace, { + packageName: "opencode-plugin-flow", + version, + protocolVersion: FLOW_LEADERSHIP_PROTOCOL_VERSION, + instanceId: createFlowPluginInstanceId(), + }); + const initial = runtimeGuard.query(); + log( + initial.operational ? "info" : "error", + `Flow ${version}: ${initial.message}`, + ); + let recovery: RecoveryController; + try { + recovery = dependencies.createRecovery(); + } catch (error) { + runtimeGuard.release(); + throw error; + } + const flow = createWorkspaceFlowService(workspace); + let lifecycle: + | Awaited> + | undefined; + const autoDrive = new AutoDriveCoordinator({ recovery, - ...(dependencies.defaultRecovery - ? { defaultRecovery: dependencies.defaultRecovery } - : {}), - showRefusal: async (message) => { - await ctx.client.tui.showToast({ - body: { - title: "Flow recovery unavailable", - message, - variant: "warning", - duration: 12000, - }, - query: { directory: ctx.directory }, - }); + readSourceDigest: () => + createFileSourceIdentityProvider(workspace).computeSourceDigest(), + readProjection: async () => { + const response = await flow.status({ request: { view: "compact" } }); + if (response.status !== "ok") throw new Error(response.summary); + const projection = response.workflowData.projection; + if (projection.view !== "compact") + throw new Error( + "Flow auto-drive received a non-compact projection.", + ); + return { + sessionId: + "sessionId" in projection ? projection.sessionId : undefined, + status: projection.status, + revision: projection.revision, + nextAction: projection.nextAction, + ...("prerequisiteRepair" in projection && + projection.prerequisiteRepair + ? { prerequisiteRepair: projection.prerequisiteRepair } + : {}), + }; }, - }), - "chat.message": async (input, output) => { - if ( - !["flow-planner", "flow-reviewer", "flow-worker"].includes( - output.message.agent, - ) - ) { - const delivery = autoDriveDelivery(output.message, input.variant); - codingModels.delete(input.sessionID); - codingModels.set(input.sessionID, { - ...delivery.model, - ...(delivery.variant ? { variant: delivery.variant } : {}), - }); - if (codingModels.size > 128) { - const oldest = codingModels.keys().next().value; - if (oldest) codingModels.delete(oldest); + prompt: async (sessionID, prompt, delivery, metadata) => { + if (!lifecycle) + throw new Error( + "Flow lifecycle is not ready for continuation dispatch.", + ); + await lifecycle.prompt(sessionID, prompt, delivery, metadata); + }, + onWarning: (message) => log("warn", message), + }); + const validation = new ValidationCaptureCoordinator({ + readSession: (sessionID) => ctx.session.get({ sessionID }), + persistObservation: persistWorkspaceValidation, + readReport: readWorkspaceTestReport, + onRecorded: (receipt) => autoDrive.observeValidation(receipt), + onReceiptError: (error) => + log("warn", "Flow continuation receipt was not accepted.", { + message: String(error), + }), + }); + const cleanups: Array<() => void | Promise> = []; + let disposed = false; + const dispose = async () => { + if (disposed) return; + disposed = true; + autoDrive.clear(); + validation.clear(); + codingModels.clear(); + runtimeGuard.release(); + for (const cleanup of cleanups.reverse()) { + try { + await cleanup(); + } catch (error) { + log("error", "Flow cleanup failed.", { message: String(error) }); } } - - const observed = await autoDrive.observeMessage( - input.sessionID, - autoDriveDelivery(output.message, input.variant), - output.parts, - output.message.id, - ); - if (observed === "stale-continuation") - throw new Error("Discarded a stale Flow auto continuation."); - recovery.observeMessage( - input.sessionID, - output.message.id, - output.parts.every( - (part) => part.type === "text" && part.synthetic === true, + }; + try { + const transport = await registerHttpTransport(ctx.provider); + cleanups.push(() => transport.dispose()); + const controller = new AbortController(); + const events = (async () => { + try { + for await (const event of ctx.event.subscribe({ + signal: controller.signal, + })) { + validation.observeShellEvent(event); + if ( + event.type === "session.deleted" || + event.type === "session.execution.succeeded" || + event.type === "session.execution.failed" || + event.type === "session.execution.interrupted" + ) + validation.cancel(event.data.sessionID); + } + if (!controller.signal.aborted) + throw new Error("The native shell observation stream ended."); + } catch (error) { + if (controller.signal.aborted) return; + validation.clear(); + autoDrive.clear(); + runtimeGuard.release(); + log("error", "Flow lost native shell observation and stopped.", { + message: String(error), + }); + } + })(); + cleanups.push(async () => { + controller.abort(); + await events; + }); + const preferences = await createFlowModelPreferences(ctx, { + onChange: async () => { + if (disposed) return; + planningModel = preferences.planningModel(); + reviewerConfiguration = + preferences.reviewerConfiguration(baseReviewer); + await ctx.agent.reload(); + }, + }); + cleanups.push(() => preferences.dispose()); + planningModel = preferences.planningModel(); + reviewerConfiguration = preferences.reviewerConfiguration(baseReviewer); + const agents = await registerFlowAgents(ctx, { + assertOperational: (action) => runtimeGuard.assertOperational(action), + readPlanningModel: () => planningModel, + readReviewerConfiguration: () => reviewerConfiguration, + }); + cleanups.push(() => agents.dispose()); + lifecycle = await registerFlowLifecycle({ + ctx, + autoDrive, + recovery, + flow, + runtimeGuard, + log, + codingModels, + ...(dependencies.defaultRecovery + ? { defaultRecovery: dependencies.defaultRecovery } + : {}), + }); + const activeLifecycle = lifecycle; + cleanups.push(() => activeLifecycle.dispose()); + const tools = createTools({ + recovery, + validation, + prepareValidation: prepareWorkspaceValidation, + onRecoveryOutcome: async (outcome) => { + const mismatch = ModelMismatch.safeParse(outcome); + if (mismatch.success) + log( + "warn", + `Requested ${mismatch.data.requestedModel}, received ${mismatch.data.model}. Recovery remains blocked pending qualification.`, + ); + }, + autoTimingSnapshot: () => autoDrive.timingSnapshot(), + autoContinuationSnapshot: (host) => + autoDrive.continuationSnapshot(host), + validationOrigin: (host, assistant, prepared) => + autoDrive.validationOrigin(host, assistant, prepared), + autoContinuationSupport: () => autoDrive.continuationSupport(), + readReviewerConfiguration: () => reviewerConfiguration, + readPlanningModel: () => planningModel, + readCodingModel: (sessionID) => codingModels.get(sessionID), + runtimeIdentity: { packageVersion: version, pluginEntrySha256 }, + }); + const registration = await registerFlowTools( + ctx, + guardTools(tools, runtimeGuard, autoDrive, (sessionID) => + ctx.session.get({ sessionID }), ), - observed === "accepted-continuation", + (context) => activeLifecycle.beforeTool(context), ); - }, - "experimental.session.compacting": async (input, output) => { - const context = autoDrive.compactionContext(input.sessionID); - if (context) output.context.push(context); - }, - event: async (input) => { - const event = input.event; - if ( - event.type === "message.updated" && - event.properties.info.role === "assistant" && - event.properties.info.parentID - ) - recovery.observeAssistant( - event.properties.info.sessionID, - event.properties.info.id, - event.properties.info.parentID, - ); - if (event.type === "message.updated") - return autoDrive.observeHostMessage( - event.properties.info.sessionID, - event.properties.info, - ); - if (event.type === "message.part.updated") - return autoDrive.observeHostPart( - event.properties.part.sessionID, - event.properties.part, - ); - if ( - event.type === "session.deleted" || - event.type === "session.error" - ) { - const sessionID = - event.type === "session.deleted" - ? event.properties.info.id - : event.properties.sessionID; - if (event.type === "session.deleted") - codingModels.delete(event.properties.info.id); - if (!sessionID) return autoDrive.clear(); - validation.cancel(sessionID); - return void autoDrive.deactivate(sessionID); - } - if (event.type !== "session.idle" && event.type !== "session.compacted") - return; - const sessionID = event.properties?.sessionID; - validation.cancel(sessionID); - if (event.type === "session.compacted") - return autoDrive.observeCompaction(sessionID); - if (runtimeGuard.query().operational) - return autoDrive.onIdle(sessionID); - autoDrive.deactivate(sessionID); - }, - "tool.execute.before": async (input, output) => - validation.observeToolBefore(input, output), - "tool.execute.after": async (input, output) => { - try { - await validation.observeToolAfter(input, output); - } catch (error) { - const message = - error instanceof Error ? error.message : String(error); - log("error", "Flow validation capture failed closed.", { message }); - output.output = `${output.output}\n\n[flow-validation-error] ${message}`; - } - }, - dispose: async () => { - codingModels.clear(); - autoDrive.clear(); - runtimeGuard.release(); - }, - } satisfies Hooks; - }; + cleanups.push(() => registration.dispose()); + const before = await ctx.tool.hook("execute.before", (input) => + validation.observeToolBefore(input), + ); + cleanups.push(() => before.dispose()); + const after = await ctx.tool.hook("execute.after", async (input) => { + try { + await validation.observeToolAfter(input); + } catch (error) { + validation.cancel(input.sessionID); + log("error", "Flow validation capture failed closed.", { + message: String(error), + }); + if (input.status === "completed") { + const content = input.result.content; + input.result = { + ...input.result, + content: [ + ...(typeof content === "string" + ? [{ type: "text" as const, text: content }] + : (content ?? [])), + { + type: "text", + text: `[flow-validation-error] ${String(error)}`, + }, + ], + }; + } + } + }); + cleanups.push(() => after.dispose()); + + return dispose; + } catch (error) { + await dispose(); + throw error; + } + }, + }); } diff --git a/src/platform/opencode/plugin.ts b/src/platform/opencode/plugin.ts index cb89836c..9e0ca0c1 100644 --- a/src/platform/opencode/plugin.ts +++ b/src/platform/opencode/plugin.ts @@ -1,15 +1,31 @@ import { RecoveryController } from "../../application/recovery-policy.js"; import { createJevDecisionProvider } from "../../infrastructure/jev-decision-provider.js"; +import { createFlowLog } from "./logging.js"; import { createFlowPlugin } from "./plugin-composition.js"; +import { Plugin } from "./sdk.js"; +import { resolveTypesafeApiKeyReader } from "./typesafe-key-env.js"; -export default createFlowPlugin({ - entryUrl: import.meta.url, - defaultRecovery: () => - process.env.TYPESAFE_API_KEY?.trim() - ? { mode: "shadow", maxCalls: 6, maxUsd: 0.02 } - : null, - createRecovery: () => - new RecoveryController( - createJevDecisionProvider(() => process.env.TYPESAFE_API_KEY), - ), +export default Plugin.define({ + id: "opencode-plugin-flow", + setup(ctx) { + const log = createFlowLog(ctx); + const selectedKey = resolveTypesafeApiKeyReader({ + pluginOptions: ctx.options, + onWarning: (warning) => log("warn", warning), + }); + const recovery = ctx.options.recovery; + const recoveryOff = + typeof recovery === "object" && + recovery !== null && + "mode" in recovery && + recovery.mode === "off"; + const readApiKey = recoveryOff ? () => undefined : selectedKey; + return createFlowPlugin({ + entryUrl: import.meta.url, + defaultRecovery: () => + readApiKey() ? { mode: "shadow", maxCalls: 6, maxUsd: 0.02 } : null, + createRecovery: () => + new RecoveryController(createJevDecisionProvider(readApiKey)), + }).setup(ctx); + }, }); diff --git a/src/platform/opencode/schema-adapter.ts b/src/platform/opencode/schema-adapter.ts deleted file mode 100644 index 9f421219..00000000 --- a/src/platform/opencode/schema-adapter.ts +++ /dev/null @@ -1,68 +0,0 @@ -import { z } from "zod"; -import { - type ToolContext, - type ToolDefinition, - type ToolResult, - tool, -} from "./sdk.js"; - -type RequestSchema = z.ZodObject<{ request: z.ZodType }>; -type HostShape = NonNullable[0]>; - -/** Bridges OpenCode's type-incompatible Zod minor and verifies both parsers. */ -function witness(schema: z.core.JSONSchema.BaseSchema): unknown { - if (schema.const !== undefined) return schema.const; - if (schema.enum?.[0] !== undefined) return schema.enum[0]; - const branch = schema.anyOf?.[0] ?? schema.oneOf?.[0]; - if (branch) return witness(branch); - if (schema.type === "object") { - return Object.fromEntries( - (schema.required ?? []).map((name) => { - const child = schema.properties?.[name]; - return [name, witness(typeof child === "object" ? child : {})]; - }), - ); - } - if (schema.type === "array") { - const child = Array.isArray(schema.items) ? schema.items[0] : schema.items; - if (!child || typeof child === "boolean") return []; - return Array.from({ length: schema.minItems ?? 0 }, () => witness(child)); - } - if (schema.type === "number" || schema.type === "integer") - return schema.minimum ?? 0; - if (schema.type === "boolean") return false; - if (schema.type === "null") return null; - return "w"; -} - -function hostShape(schema: RequestSchema): HostShape { - const shape = schema.shape as unknown as HostShape; - const hostSchema = tool.schema.object(shape); - const sample = witness( - z.toJSONSchema(schema, { io: "input", unrepresentable: "any" }), - ); - const application = schema.safeParse(sample); - const hostResult = hostSchema.safeParse(sample); - if ( - !application.success || - !hostResult.success || - JSON.stringify(application.data) !== JSON.stringify(hostResult.data) - ) { - throw new Error("Flow and OpenCode schemas disagree."); - } - tool.schema.toJSONSchema(hostSchema); - return shape; -} - -export function defineFlowTool(input: { - description: string; - schema: Schema; - execute: (args: z.infer, context: ToolContext) => Promise; -}): ToolDefinition { - return tool({ - description: input.description, - args: hostShape(input.schema), - execute: async (args, context) => - input.execute(input.schema.parse(args), context), - }); -} diff --git a/src/platform/opencode/sdk.ts b/src/platform/opencode/sdk.ts index b6f1a0bb..4c626db2 100644 --- a/src/platform/opencode/sdk.ts +++ b/src/platform/opencode/sdk.ts @@ -1,11 +1,30 @@ +export { Agent, Model, Plugin, Provider } from "@opencode/plugin"; +export type { Context as PluginContext } from "@opencode/plugin/promise/plugin"; export type { - Hooks, - Plugin, + Info as NativeToolDefinition, ToolContext, - ToolDefinition, - ToolResult, -} from "@opencode-ai/plugin"; -export { tool } from "@opencode-ai/plugin"; - -export type { TuiPlugin, TuiPluginModule } from "@opencode-ai/plugin/tui"; -export type { Config as HostConfig, Provider } from "@opencode-ai/sdk/v2"; +} from "@opencode/plugin/promise/tool"; +export type NativeToolHook = Parameters< + Parameters< + import("@opencode/plugin/promise/plugin").Context["tool"]["hook"] + >[1] +>[0]; +export type NativeToolBefore = Exclude; +export type NativeToolAfter = Extract; +export type { + OpenCodeEvent, + SessionInfo, + SessionMessageInfo, +} from "@opencode/client"; +export type { Registration } from "@opencode/plugin/promise/registration"; +export type { + SessionContext, + SessionPrompt, +} from "@opencode/plugin/promise/session"; +export { + CallID, + Error as NativeToolError, +} from "@opencode/plugin/promise/tool"; +export { AbsolutePath } from "@opencode/schema/schema"; +export { Session } from "@opencode/schema/session"; +export { SessionMessage } from "@opencode/schema/session-message"; diff --git a/src/platform/opencode/tool-guard.ts b/src/platform/opencode/tool-guard.ts index 447ddcc2..4f953927 100644 --- a/src/platform/opencode/tool-guard.ts +++ b/src/platform/opencode/tool-guard.ts @@ -1,57 +1,49 @@ import { z } from "zod"; import { dataNote } from "../../application/flow-response.js"; import { PlanSaveInputSchema } from "../../application/schema.js"; +import { assertMutableWorkspaceRoot } from "../../infrastructure/fs/workspace-paths.js"; import type { AutoDriveCoordinator } from "./auto-drive.js"; +import type { FlowTools, FlowToolValue } from "./flow-tool.js"; import type { FlowLeadershipHandle, FlowLeadershipReason, FlowLeadershipStatus, } from "./leadership.js"; -import type { Hooks } from "./sdk.js"; const MUTATION = /^flow_(?:plan_save|plan_approve|plan_amend|run_start|review_start|feature_complete|feature_reset|session_close)$/; -function acceptedMutation(tool: string, output: string) { +const AcceptedMutation = z.object({ + status: z.enum(["ok", "error"]), + workflowData: z.object({ + operation: z.object({ replayed: z.literal(false) }), + projection: z.object({ + revision: z.number().int().safe(), + sessionId: z.string().optional(), + }), + closeState: z.object({ durableAccepted: z.boolean() }).optional(), + }), +}); +function acceptedMutation(tool: string, output: FlowToolValue) { if (!MUTATION.test(tool)) return null; - try { - const response = JSON.parse(output); - const data = response.workflowData; - const closeAccepted = - tool === "flow_session_close" && - response.status === "error" && - data?.closeState?.durableAccepted === true; - const revision = data?.projection?.revision; - if ( - data?.operation?.replayed !== false || - (response.status !== "ok" && !closeAccepted) || - typeof revision !== "number" || - !Number.isSafeInteger(revision) - ) - return null; - const sessionId = data.projection?.sessionId; - return { - revision, - sessionId: typeof sessionId === "string" ? sessionId : undefined, - }; - } catch { + const result = AcceptedMutation.safeParse(output); + if ( + !result.success || + (result.data.status !== "ok" && + !( + tool === "flow_session_close" && + result.data.workflowData.closeState?.durableAccepted + )) + ) return null; - } + return result.data.workflowData.projection; } -type FlowTools = NonNullable; const NativeSessionIdentity = z.object({ id: z.string().min(1), - directory: z.string().min(1), + agent: z.string().min(1), + location: z.object({ directory: z.string().min(1) }), parentID: z.string().min(1).optional(), }); -/** - * Tools whose successful output is markdown prose rather than a Flow response - * envelope. A guard rejection must stay in the same shape the caller is reading, - * so these get a markdown failure instead of a JSON blob. - */ -const MARKDOWN_TOOLS = new Set(["flow_guidance"]); - -/** Actionable recovery for each non-operational leadership reason. */ function guardRecovery(reason: FlowLeadershipReason): string { switch (reason) { case "duplicate-instances": @@ -62,29 +54,30 @@ function guardRecovery(reason: FlowLeadershipReason): string { return "Flow is not registered for this project. Restart OpenCode to re-register, then retry."; } } - -function guardRejection(name: string, status: FlowLeadershipStatus): string { - const recovery = guardRecovery(status.reason); - if (MARKDOWN_TOOLS.has(name)) { - return `${status.message}\n\nRecovery: ${recovery}`; - } - // The same envelope every other Flow failure uses, so a caller told to read - // `workflowData.failure.recovery` finds it here too. - return JSON.stringify({ - status: "error", - summary: status.message, - workflowData: { - dataNote: dataNote(), - failure: { summary: status.message, recovery }, - runtimeGuard: status, - }, - }); +function rejection( + name: string, + summary: string, + recovery?: string, + status?: FlowLeadershipStatus, +): FlowToolValue { + return name === "flow_guidance" + ? `${summary}${recovery ? `\n\nRecovery: ${recovery}` : ""}` + : { + status: "error", + summary, + workflowData: { + dataNote: dataNote(), + failure: { summary, ...(recovery ? { recovery } : {}) }, + ...(status ? { runtimeGuard: status } : {}), + }, + }; } +const SPECIALISTS = new Set(["flow-planner", "flow-worker", "flow-reviewer"]); export function guardTools( tools: FlowTools, runtimeGuard: FlowLeadershipHandle, - autoDrive: AutoDriveCoordinator, + autoDrive: Pick, readSession: (sessionId: string) => Promise, ): FlowTools { return Object.fromEntries( @@ -92,57 +85,70 @@ export function guardTools( name, { ...definition, - execute: async (...args: Parameters) => { - if (args[1].agent === "flow-planner") - return JSON.stringify({ - status: "error", - summary: - "The planning specialist supplies advice only; the manager owns all Flow tools.", - workflowData: {}, - }); + execute: async ( + input: unknown, + context: Parameters[1], + ) => { const status = runtimeGuard.query(); if (!status.operational) { autoDrive.clear(); - return guardRejection(name, status); + return rejection( + name, + status.message, + guardRecovery(status.reason), + status, + ); } - const managerMutation = - (MUTATION.test(name) && name !== "flow_feature_complete") || - name === "flow_validation_start" || - (name === "flow_status" && args[0].recoveryProposal !== undefined); - const completion = name === "flow_feature_complete"; - if (managerMutation || completion) { - let permitted = false; - try { - const identity = NativeSessionIdentity.safeParse( - await readSession(args[1].sessionID), - ); - permitted = - identity.success && - identity.data.id === args[1].sessionID && - identity.data.directory === args[1].directory && - (completion - ? identity.data.parentID === undefined - ? args[1].agent !== "flow-reviewer" - : args[1].agent === "flow-reviewer" - : identity.data.parentID === undefined); - } catch {} - if (!permitted) { - const summary = - "Flow mutations require a verified primary manager or verified reviewer child session."; - return JSON.stringify({ - status: "error", - summary, - workflowData: { dataNote: dataNote(), failure: { summary } }, - }); + let permitted = false; + try { + const identity = NativeSessionIdentity.parse( + await readSession(context.sessionID), + ); + const workspace = assertMutableWorkspaceRoot(context.directory); + const sameIdentity = + identity.id === context.sessionID && + identity.agent === context.agent && + assertMutableWorkspaceRoot(identity.location.directory) === + workspace; + const request = + input && typeof input === "object" && "recoveryProposal" in input; + const managerMutation = + MUTATION.test(name) || + name === "flow_validation_start" || + request; + if ( + sameIdentity && + context.agent !== "flow-planner" && + context.agent !== "flow-worker" + ) { + if ( + name === "flow_feature_complete" && + identity.parentID && + context.agent === "flow-reviewer" + ) { + permitted = true; + } else { + permitted = managerMutation + ? !identity.parentID && !SPECIALISTS.has(context.agent) + : true; + } } - } - const output = await definition.execute(...args); - const mutation = acceptedMutation(name, String(output)); - const context = args[1]; + } catch {} + if (!permitted) + return rejection( + name, + "Flow tools require a verified workspace session. Mutations require a primary manager or an independent reviewer child session.", + ); + await context.assertCurrent?.(); + if (context.abort.aborted) + throw new Error("Flow tool execution was cancelled."); + runtimeGuard.assertOperational("execute a Flow tool"); + const output = await definition.execute(input, context); + const mutation = acceptedMutation(name, output); if (mutation) { const saved = name === "flow_plan_save" - ? PlanSaveInputSchema.safeParse(args[0]) + ? PlanSaveInputSchema.safeParse(input) : null; autoDrive.observeMutation( context.sessionID, @@ -165,5 +171,5 @@ export function guardTools( }, }, ]), - ) as FlowTools; + ); } diff --git a/src/platform/opencode/tools.ts b/src/platform/opencode/tools.ts index 577d80a3..720fa865 100644 --- a/src/platform/opencode/tools.ts +++ b/src/platform/opencode/tools.ts @@ -1,3 +1,4 @@ +import { z } from "zod"; import { errorResponse } from "../../application/flow-response.js"; import type { PreparedValidation } from "../../application/prepare-validation.js"; import type { RecoveryController } from "../../application/recovery-policy.js"; @@ -31,12 +32,14 @@ import type { AutoValidationOrigin, ProcessLocalAutoContinuationSupport, } from "./auto-drive.js"; -import { defineFlowTool } from "./schema-adapter.js"; -import { type Hooks, type ToolContext, tool } from "./sdk.js"; +import { + defineFlowTool, + type FlowToolContext, + type FlowToolResponse, + type FlowTools, +} from "./flow-tool.js"; import type { ValidationCaptureCoordinator } from "./validation-capture.js"; -const host = tool.schema; -type FlowTools = NonNullable; type WorkspaceFlowService = ReturnType; type ToolOptions = Readonly<{ @@ -79,22 +82,8 @@ type ToolOptions = Readonly<{ | undefined; }>; -function json(value: unknown): string { - const serialized = JSON.stringify(value, null, 2); - if (serialized === undefined) { - throw new Error("Flow tool response could not be serialized."); - } - return serialized; -} - -type FlowToolResponse = Readonly<{ - status: "ok" | "error"; - summary: string; - workflowData: object; -}>; - -function toolError(error: unknown): string { - return json(errorResponse(error)); +function toolError(error: unknown): FlowToolResponse { + return errorResponse(error); } function bestEffort(read: () => Value): Value | undefined { @@ -143,9 +132,6 @@ function withAutoContext( : undefined; if (continuation) workflowData = { ...workflowData, autoDrive: continuation }; const support = bestEffort(() => options.autoContinuationSupport?.()); - // `unknown` is withheld deliberately: before any assistant message exists it - // is the absence of a signal, and reporting it invites a caller to relay it as - // a limitation. if (support === "supported" || support === "unsupported") { workflowData = { ...workflowData, @@ -154,7 +140,7 @@ function withAutoContext( support, ...(support === "unsupported" ? { - reason: "host-reports-no-assistant-message-parentage", + reason: "host-request-attribution-unavailable", recovery: "Drive each feature with /flow-run.", } : {}), @@ -168,20 +154,18 @@ function withAutoContext( export function createTools(options: ToolOptions): FlowTools { async function execute( - context: ToolContext, + context: FlowToolContext, handler: (flow: WorkspaceFlowService) => Promise, - ): Promise { + ): Promise { try { - return json( - await handler( - createWorkspaceFlowService( - resolveWorkspaceRoot(context), - options.recovery?.guard({ - hostSessionId: context.sessionID, - messageId: context.messageID, - agent: context.agent, - }), - ), + return await handler( + createWorkspaceFlowService( + resolveWorkspaceRoot(context), + options.recovery?.guard({ + hostSessionId: context.sessionID, + messageId: context.messageID, + agent: context.agent, + }), ), ); } catch (error) { @@ -190,19 +174,19 @@ export function createTools(options: ToolOptions): FlowTools { } function executeMutation( - context: ToolContext, + context: FlowToolContext, validation: ValidationCaptureCoordinator, handler: (flow: WorkspaceFlowService) => Promise, - ): Promise { + ): Promise { validation.cancel(context.sessionID); return execute(context, handler); } function executeReviewerMutation( - context: ToolContext, + context: FlowToolContext, handler: (flow: WorkspaceFlowService) => Promise, replayHandler: (flow: WorkspaceFlowService) => Promise, - ): Promise { + ): Promise { if (context.agent !== "flow-reviewer") { return execute(context, replayHandler); } @@ -210,9 +194,9 @@ export function createTools(options: ToolOptions): FlowTools { } return { - flow_guidance: tool({ + flow_guidance: defineFlowTool({ description: "Load one concise package-owned Flow guide.", - args: { id: host.enum(FLOW_GUIDANCE_IDS) }, + schema: z.object({ id: z.enum(FLOW_GUIDANCE_IDS) }), execute: async ({ id }) => getFlowGuidance(id).content, }), flow_status: defineFlowTool({ @@ -228,9 +212,7 @@ export function createTools(options: ToolOptions): FlowTools { ) { try { await options.onRecoveryOutcome(response.workflowData.recovery); - } catch { - // A missing TUI cannot change the Flow status result. - } + } catch {} } const workflowData = "projection" in response.workflowData @@ -296,7 +278,7 @@ export function createTools(options: ToolOptions): FlowTools { }), flow_validation_start: defineFlowTool({ description: - "Arm host observation for the exact next Bash command; its result is recorded directly in Session v5.", + "Arm host observation for the exact next shell command; its result is recorded directly in Session v5.", schema: ValidationStartInputSchema, execute: async (args, context) => { try { @@ -305,9 +287,10 @@ export function createTools(options: ToolOptions): FlowTools { workspace, args.request, ); - return json({ + await context.assertCurrent?.(); + return { status: "ok", - summary: "Validation armed for the exact next Bash command.", + summary: "Validation armed for the exact next shell command.", workflowData: { capture: options.validation.arm( context.sessionID, @@ -323,7 +306,7 @@ export function createTools(options: ToolOptions): FlowTools { scope: prepared.scope, intent: prepared.intent, }, - }); + }; } catch (error) { return toolError(error); } @@ -368,3 +351,84 @@ export function createTools(options: ToolOptions): FlowTools { }), }; } + +export async function registerFlowTools( + ctx: { + tool: Pick; + permission: Pick; + location: Pick; + }, + tools: FlowTools, + beforeTool: (context: import("./sdk.js").ToolContext) => Promise, +) { + const directory = resolveWorkspaceRoot({ directory: ctx.location.directory }); + return ctx.tool.transform((editor) => { + for (const [name, definition] of Object.entries(tools)) { + editor.remove(name); + editor.add({ + name, + description: definition.description, + input: definition.input, + options: { codemode: false, permission: name }, + execute: async (input: unknown, context) => { + const request = + input && typeof input === "object" && "request" in input + ? input.request + : null; + const resources: string[] = []; + if (request && typeof request === "object") { + for (const key of [ + "featureId", + "nextFeatureId", + "sessionId", + ] as const) { + const value = Reflect.get(request, key); + if (typeof value === "string") resources.push(value); + } + } + if ( + input && + typeof input === "object" && + "id" in input && + typeof input.id === "string" + ) + resources.push(input.id); + await ctx.permission.assert({ + sessionID: context.sessionID, + agent: context.agent, + action: name, + resources: resources.length ? resources : [directory], + source: { + type: "tool", + messageID: context.messageID, + id: context.id, + }, + }); + await beforeTool(context); + if (context.signal.aborted) + throw new Error("Flow tool execution was cancelled."); + const value = await definition.execute(input, { + sessionID: context.sessionID, + messageID: context.messageID, + callID: context.id, + agent: context.agent, + directory, + abort: context.signal, + assertCurrent: () => beforeTool(context), + }); + return { + content: [ + { + type: "text", + text: + typeof value === "string" + ? value + : JSON.stringify(value, null, 2), + }, + ], + }; + }, + }); + } + }); +} diff --git a/src/platform/opencode/typesafe-key-env.ts b/src/platform/opencode/typesafe-key-env.ts new file mode 100644 index 00000000..cd3b3cca --- /dev/null +++ b/src/platform/opencode/typesafe-key-env.ts @@ -0,0 +1,23 @@ +export function resolveTypesafeApiKeyReader(options: { + pluginOptions: Readonly>; + env?: Readonly>; + onWarning?: (message: string) => void; +}): () => string | undefined { + const configured = options.pluginOptions.typesafeApiKeyEnv; + const name = configured === undefined ? "TYPESAFE_API_KEY" : configured; + if ( + typeof name !== "string" || + !/^[A-Za-z_]/.test(name) || + /[^A-Za-z0-9_]/.test(name) + ) { + options.onWarning?.( + "Flow plugin option typesafeApiKeyEnv must be a portable environment variable name; Typesafe advice is disabled.", + ); + return () => undefined; + } + const env = options.env ?? process.env; + return () => { + const value = Object.hasOwn(env, name) ? env[name] : undefined; + return value?.trim() || undefined; + }; +} diff --git a/src/platform/opencode/validation-capture.ts b/src/platform/opencode/validation-capture.ts index 91e45930..e23b8b63 100644 --- a/src/platform/opencode/validation-capture.ts +++ b/src/platform/opencode/validation-capture.ts @@ -3,6 +3,7 @@ import { constants } from "node:fs"; import { open, realpath } from "node:fs/promises"; import { homedir } from "node:os"; import { basename, dirname, isAbsolute, join } from "node:path"; +import { z } from "zod"; import type { ObservedValidation, PreparedValidation, @@ -12,13 +13,18 @@ import type { ValidationIneligibleReason, ValidationObservation, } from "../../domain/session.js"; +import { assertMutableWorkspaceRoot } from "../../infrastructure/fs/workspace-paths.js"; import { observeAssertions } from "../../infrastructure/junit-results.js"; import type { AutoValidationOrigin, AutoValidationReceipt, } from "./auto-drive.js"; import { validationContinuationOutcome } from "./auto-drive-validation.js"; -import type { Hooks } from "./sdk.js"; +import type { + NativeToolAfter, + NativeToolBefore, + OpenCodeEvent, +} from "./sdk.js"; const MAX_CAPTURES = 128; const CAPTURE_TTL_MS = 15 * 60 * 1_000; @@ -32,11 +38,17 @@ type PendingCapture = PreparedValidation & armedAt: number; origin: AutoValidationOrigin | null; }> & { - callID: string | null; + execution: { + id: string; + messageID: string; + agent: string; + startedAt: number; + } | null; priorReport: { digest: string; modifiedMs: number } | null; }; type ValidationCaptureOptions = Readonly<{ + readSession: (sessionID: string) => Promise; persistObservation: ( workspace: string, observation: ObservedValidation, @@ -54,41 +66,94 @@ type ValidationCaptureOptions = Readonly<{ onReceiptError?: (error: unknown) => void; }>; -type BeforeInput = Parameters>[0]; -type BeforeOutput = Parameters>[1]; -type AfterInput = Parameters>[0]; -type AfterOutput = Parameters>[1]; +const SessionIdentity = z.object({ + id: z.string(), + agent: z.string(), + parentID: z.string().optional(), + location: z.object({ directory: z.string() }), +}); +const ShellInput = z.object({ + command: z.string(), + workdir: z.string().optional(), +}); +const ShellOutput = z.object({ + exit: z.number().int().safe().optional(), + truncated: z.boolean().optional(), + status: z.string().optional(), + output: z.string().optional(), + timeout: z.boolean().optional(), +}); +const ShellRecord = z.object({ + id: z.string(), + file: z.string(), + command: z.string(), + cwd: z.string(), + status: z.enum(["running", "exited", "timeout", "killed"]), + exit: z.number().int().safe().optional(), + metadata: z.object({ sessionID: z.string() }), + time: z.object({ started: z.number() }), +}); +type NativeShellRecord = z.output; +type ShellObservation = + | { kind: "exited"; exit: number; complete: boolean | null; text: string } + | { kind: "failed"; text: string } + | { kind: "unknown"; text: string }; -export class ValidationCaptureError extends Error { +class ValidationCaptureError extends Error { readonly code = "FLOW_VALIDATION_CAPTURE"; } - -function commandFromArgs(value: unknown): string | null { - if (!value || typeof value !== "object") return null; - const command = (value as Record).command; - return typeof command === "string" ? command : null; -} - -function exitCode(value: unknown): number | null { - if (!value || typeof value !== "object") return null; - const exit = (value as Record).exit; - return typeof exit === "number" && Number.isSafeInteger(exit) ? exit : null; +function textContent( + result: NativeToolAfter & { status: "completed" }, +): string { + const content = result.result.content; + return typeof content === "string" + ? content + : (content ?? []) + .filter((part) => part.type === "text") + .map((part) => part.text) + .join("\n"); } - -/** - * Whether the host reported that it captured the whole command output. - * - * `null` means the host reported neither flag, which is different from reporting - * truncation: the output may well be complete, but Flow has no evidence of it, so - * the observation is recorded as ineligible rather than silently treated as - * truncated. - */ -function completeOutput(value: unknown): boolean | null { - if (!value || typeof value !== "object") return null; - const metadata = value as Record; - if (metadata.truncated === true || metadata.complete === false) return false; - if (metadata.truncated === false || metadata.complete === true) return true; - return null; +function observeShell(input: NativeToolAfter): ShellObservation { + if (input.status === "error") + return { kind: "failed", text: input.error.message }; + const wire = ShellOutput.safeParse(input.result.output); + const meta = ShellOutput.safeParse(input.result.metadata); + const output = wire.success ? wire.data : {}; + const metadata = meta.success ? meta.data : {}; + const text = output.output ?? textContent(input); + if ( + (input.result.output !== undefined && !wire.success) || + (input.result.metadata !== undefined && !meta.success) + ) + return { kind: "unknown", text }; + if ( + output.timeout || + metadata.timeout || + (output.status && output.status !== "completed") || + (metadata.status && metadata.status !== "completed") + ) + return { kind: "unknown", text }; + if ( + output.exit !== undefined && + metadata.exit !== undefined && + output.exit !== metadata.exit + ) + return { kind: "unknown", text }; + if ( + output.truncated !== undefined && + metadata.truncated !== undefined && + output.truncated !== metadata.truncated + ) + return { kind: "unknown", text }; + const exit = output.exit ?? metadata.exit; + if (exit === undefined) return { kind: "unknown", text }; + const truncated = output.truncated ?? metadata.truncated; + return { + kind: "exited", + exit, + complete: truncated === undefined ? null : !truncated, + text, + }; } function digest(value: string) { @@ -100,15 +165,18 @@ export async function readHostToolOutput( dataHome = process.env.XDG_DATA_HOME?.trim() || join(homedir(), ".local", "share"), ): Promise { - if ( - !isAbsolute(path) || - !/^tool_[a-zA-Z0-9]+(?:\.txt)?$/.test(basename(path)) - ) + if (!isAbsolute(path) || !/^sh_[a-zA-Z0-9]+\.out$/.test(basename(path))) + return null; + const root = join(dataHome, "opencode", "shell"); + const directory = dirname(path); + if (dirname(directory) !== root || !/^[a-f0-9]+$/.test(basename(directory))) return null; - const directory = join(dataHome, "opencode", "tool-output"); - if (dirname(path) !== directory) return null; try { - if (dirname(await realpath(path)) !== (await realpath(directory))) + if ( + dirname(await realpath(directory)) !== (await realpath(root)) || + basename(await realpath(directory)) !== basename(directory) || + dirname(await realpath(path)) !== (await realpath(directory)) + ) return null; const file = await open( path, @@ -118,9 +186,18 @@ export async function readHostToolOutput( try { const stat = await file.stat(); if (!stat.isFile() || stat.size > MAX_HOST_OUTPUT_BYTES) return null; - const bytes = await file.readFile(); - if (bytes.byteLength !== stat.size) return null; - return new TextDecoder("utf-8", { fatal: true }).decode(bytes); + const bytes = Buffer.alloc(stat.size + 1); + let length = 0; + while (length < bytes.length) { + const read = await file.read(bytes, length, bytes.length - length); + if (read.bytesRead === 0) break; + length += read.bytesRead; + } + if (length !== stat.size || (await file.stat()).size !== stat.size) + return null; + return new TextDecoder("utf-8", { fatal: true }).decode( + bytes.subarray(0, length), + ); } finally { await file.close(); } @@ -129,11 +206,9 @@ export async function readHostToolOutput( } } -function isBash(tool: string): boolean { - return tool.toLowerCase() === "bash"; -} - export class ValidationCaptureCoordinator { + readonly #readSession: ValidationCaptureOptions["readSession"]; + readonly #shellRecords = new Map(); readonly #persist: ValidationCaptureOptions["persistObservation"]; readonly #readReport: ValidationCaptureOptions["readReport"]; readonly #now: () => number; @@ -144,6 +219,7 @@ export class ValidationCaptureCoordinator { readonly #onReceiptError: ValidationCaptureOptions["onReceiptError"]; constructor(options: ValidationCaptureOptions) { + this.#readSession = options.readSession; this.#persist = options.persistObservation; this.#readReport = options.readReport; this.#now = options.now ?? Date.now; @@ -153,21 +229,67 @@ export class ValidationCaptureCoordinator { this.#onReceiptError = options.onReceiptError; } + observeShellEvent(event: OpenCodeEvent): void { + if (event.type === "shell.created") { + const parsed = ShellRecord.safeParse(event.data.info); + if (parsed.success) this.#shellRecords.set(parsed.data.id, parsed.data); + if (this.#shellRecords.size > MAX_CAPTURES) + this.#shellRecords.delete(this.#shellRecords.keys().next().value ?? ""); + } else if (event.type === "shell.exited") { + const record = this.#shellRecords.get(event.data.id); + if (record) + this.#shellRecords.set(record.id, { + ...record, + status: event.data.status, + ...(event.data.exit === undefined ? {} : { exit: event.data.exit }), + }); + } else if (event.type === "shell.deleted") + this.#shellRecords.delete(event.data.id); + } + async #completeSpilledOutput( - visible: string, - metadata: unknown, + capture: PendingCapture, + input: NativeToolAfter, + observed: ShellObservation, ): Promise { - if (!metadata || typeof metadata !== "object") return null; - const host = metadata as Record; - if (host.truncated !== true || typeof host.outputPath !== "string") + if ( + input.status !== "completed" || + observed.kind !== "exited" || + observed.complete !== false || + !capture.execution + ) + return null; + const matches = [ + ...textContent(input).matchAll(/full output saved to ([^\]\n]+)/g), + ]; + if (matches.length !== 1) return null; + const path = matches[0]?.[1]; + if (!path) return null; + const id = basename(path, ".out"); + const record = this.#shellRecords.get(id); + if ( + !record || + record.file !== path || + record.metadata.sessionID !== capture.sessionID || + record.command !== capture.command || + record.status !== "exited" || + record.exit !== observed.exit || + record.time.started < capture.execution.startedAt || + record.time.started > this.#now() + ) return null; - const marker = `...output truncated...\n\nFull output saved to: ${host.outputPath}\n\n`; - if (!visible.startsWith(marker)) return null; - const tail = visible.slice(marker.length); - if (tail.length < 64) return null; try { - const full = await this.#readFullOutput(host.outputPath); - return full && full.length > tail.length && full.endsWith(tail) + if (assertMutableWorkspaceRoot(record.cwd) !== capture.workspace) + return null; + const tail = observed.text.replace( + /\n\n\[showing lines [0-9]+-[0-9]+ of [0-9]+; full output saved to [^\]\n]+\]$/, + "", + ); + if (tail.length < 64 || tail === observed.text) return null; + const full = await this.#readFullOutput(path); + return full !== null && + full.length > tail.length && + (full.endsWith(tail) || full.endsWith(`${tail}\n`)) ? full : null; } catch { @@ -175,12 +297,6 @@ export class ValidationCaptureCoordinator { } } - /** - * What the command's own report says about each name the plan declared. - * - * Every route that is not a report this command wrote lands on `absent`, because - * they mean the same thing: nothing observed those cases. - */ async #observeAssertions( capture: PendingCapture, observedAt: number, @@ -192,10 +308,6 @@ export class ValidationCaptureCoordinator { })); if (!capture.resultsPath || !this.#readReport) return absent; const report = await this.#report(capture.workspace, capture.resultsPath); - // `<=`, not `<`: the contract is that this command wrote the report, so a report - // whose mtime only *equals* the arming instant has not been shown to postdate it. - // Not a one-millisecond window either — mtime granularity is a whole second on - // some filesystems, which is wide enough for a stale report to share it. if ( !report || report.modifiedMs <= capture.armedAt || @@ -225,7 +337,7 @@ export class ValidationCaptureCoordinator { #prune(): void { const cutoff = this.#now() - CAPTURE_TTL_MS; for (const [sessionID, capture] of this.#pending) { - if (capture.callID === null && capture.armedAt < cutoff) { + if (capture.execution === null && capture.armedAt < cutoff) { this.#pending.delete(sessionID); } } @@ -238,6 +350,7 @@ export class ValidationCaptureCoordinator { origin: AutoValidationOrigin | null = null, ): Readonly<{ captureId: string; expiresInMs: number }> { this.#prune(); + workspace = assertMutableWorkspaceRoot(workspace); if (this.#pending.has(sessionID)) { throw new ValidationCaptureError( "This OpenCode session already has an armed validation command.", @@ -265,7 +378,7 @@ export class ValidationCaptureCoordinator { workspace, armedAt: this.#now(), origin, - callID: null, + execution: null, priorReport: null, }); return { captureId, expiresInMs: CAPTURE_TTL_MS }; @@ -275,21 +388,51 @@ export class ValidationCaptureCoordinator { return this.#pending.delete(sessionID); } - async observeToolBefore( - input: BeforeInput, - output: BeforeOutput, - ): Promise { + clear(): void { + this.#pending.clear(); + this.#shellRecords.clear(); + } + + async observeToolBefore(input: NativeToolBefore): Promise { this.#prune(); - if (!isBash(input.tool)) return; + if (input.tool !== "shell") return; const capture = this.#pending.get(input.sessionID); - if (!capture || capture.callID !== null) return; - if (commandFromArgs(output.args) !== capture.command) { + if (!capture || capture.execution) return; + const args = ShellInput.safeParse(input.input); + capture.execution = { + id: input.id, + messageID: input.messageID, + agent: input.agent, + startedAt: this.#now(), + }; + try { + const identity = SessionIdentity.parse( + await this.#readSession(input.sessionID), + ); + if ( + !args.success || + args.data.command !== capture.command || + identity.id !== input.sessionID || + identity.agent !== input.agent || + identity.parentID !== undefined || + ["flow-planner", "flow-worker", "flow-reviewer"].includes( + input.agent, + ) || + assertMutableWorkspaceRoot(identity.location.directory) !== + capture.workspace || + (args.data.workdir && + assertMutableWorkspaceRoot(args.data.workdir) !== capture.workspace) + ) + throw new Error( + "The shell identity, workspace, or command did not match the armed validation.", + ); + } catch (error) { this.#pending.delete(input.sessionID); throw new ValidationCaptureError( - "The next Bash command did not match the armed validation command; capture was cancelled.", + "The next shell command did not match the armed validation; capture was cancelled.", + { cause: error }, ); } - capture.callID = input.callID; if (capture.resultsPath && capture.assertions.length > 0) { const report = await this.#report(capture.workspace, capture.resultsPath); capture.priorReport = report @@ -299,28 +442,48 @@ export class ValidationCaptureCoordinator { } async observeToolAfter( - input: AfterInput, - output: AfterOutput, + input: NativeToolAfter, ): Promise { this.#prune(); - if (!isBash(input.tool)) return null; + if (input.tool !== "shell") return null; const capture = this.#pending.get(input.sessionID); - if (!capture || capture.callID !== input.callID) return null; + const execution = capture?.execution; + if ( + !capture || + !execution || + execution.id !== input.id || + execution.messageID !== input.messageID || + execution.agent !== input.agent + ) + return null; this.#pending.delete(input.sessionID); - if (commandFromArgs(input.args) !== capture.command) { + const args = ShellInput.safeParse(input.input); + const identity = SessionIdentity.parse( + await this.#readSession(input.sessionID), + ); + if ( + !args.success || + args.data.command !== capture.command || + identity.id !== input.sessionID || + identity.agent !== input.agent || + identity.parentID !== undefined || + assertMutableWorkspaceRoot(identity.location.directory) !== + capture.workspace || + (args.data.workdir && + assertMutableWorkspaceRoot(args.data.workdir) !== capture.workspace) + ) throw new ValidationCaptureError( - "The executed Bash command changed after Flow armed it.", + "The executed shell command or workspace changed after Flow armed it.", ); - } - // A host that reports no exit code or no completeness flag still gets a - // durable observation, recorded as ineligible. Failing closed by throwing - // would lose the record entirely and make Flow unusable on such a host. - const observedExit = exitCode(output.metadata); - const observedComplete = completeOutput(output.metadata); - const fullOutput = - observedComplete === false - ? await this.#completeSpilledOutput(output.output, output.metadata) - : null; + const observed = observeShell(input); + const observedExit = observed.kind === "exited" ? observed.exit : null; + const observedComplete = + observed.kind === "exited" ? observed.complete : null; + const fullOutput = await this.#completeSpilledOutput( + capture, + input, + observed, + ); const outputComplete = observedComplete === true || fullOutput !== null; const hostGap: ValidationIneligibleReason | null = observedExit === null @@ -328,6 +491,7 @@ export class ValidationCaptureCoordinator { : observedComplete === null ? "output-completeness-unknown" : null; + const observedAssertions = await this.#observeAssertions( capture, this.#now(), @@ -345,21 +509,18 @@ export class ValidationCaptureCoordinator { ...(observedAssertions.length > 0 ? { observedAssertions } : {}), captureId: capture.captureId, exitCode: observedExit, - outputDigest: digest(fullOutput ?? output.output), + outputDigest: digest(fullOutput ?? observed.text), outputComplete, ...(hostGap ? { ineligibleReason: hostGap } : {}), }); const outcome = validationContinuationOutcome(capture, observation); - output.output = `${output.output}\n\n[flow-validation] ${JSON.stringify({ + const receipt = `\n\n[flow-validation] ${JSON.stringify({ id: observation.id, scope: observation.scope, intent: observation.intent ?? capture.intent, passed: outcome === "passed", observed: outcome === "observed", recordedRevision: observation.recordedRevision, - // Reported back because a declared case that came out `skipped` or `absent` is - // exactly the state exit zero hides, and the caller has to be told before it - // reads the exit code as proof. ...(observation.observedAssertions ? { assertions: observation.observedAssertions } : {}), @@ -368,6 +529,11 @@ export class ValidationCaptureCoordinator { : {}), ...(fullOutput ? { fullOutputDigest: observation.outputDigest } : {}), })}`; + if (input.status === "completed") + input.result = { + ...input.result, + content: [{ type: "text", text: textContent(input) + receipt }], + }; if (capture.origin) { try { this.#onRecorded?.({ diff --git a/src/prompt-surfaces.ts b/src/prompt-surfaces.ts index c75039d5..ff38b3d6 100644 --- a/src/prompt-surfaces.ts +++ b/src/prompt-surfaces.ts @@ -32,7 +32,7 @@ const FLOW_WORKER_PROMPT = [ ].join(" "), "- Do not delegate, spawn subtasks, or load skills.", "- Do not stage, commit, push, publish, or create a release.", - "- Do not run Bash commands. The manager owns every executable check.", + "- Do not run Shell commands. The manager owns every executable check.", "- Never edit .flow or .git metadata paths; the host denies those paths.", "- A read-only evidence slice must not edit files.", [ diff --git a/src/tui.ts b/src/tui.ts index 27f119d9..c4cd48e5 100644 --- a/src/tui.ts +++ b/src/tui.ts @@ -1,188 +1,57 @@ +import type { LocationRef } from "@opencode/client"; +import { define } from "@opencode/plugin/tui/plugin"; import { - FLOW_MODEL_ROLES, - type FlowModelRole, - modelChoices, - modelPreference, - modelPreferencePatch, -} from "./platform/opencode/model-picker.js"; -import type { TuiPlugin, TuiPluginModule } from "./platform/opencode/sdk.js"; + pickerSnapshotSchema, + savePreferenceResultSchema, +} from "./application/model-preferences-schema.js"; +import { FlowModelPreferencesRpc } from "./flow-rpc.js"; +import { createModelPicker } from "./platform/opencode/model-picker-ui.js"; -const tui: TuiPlugin = async (api) => { - let saving = false; - const error = (cause: unknown) => - api.ui.toast({ - variant: "error", - title: "Flow models", - message: - cause instanceof Error - ? cause.message - : "Could not update Flow model settings.", - }); - const open = async (role: FlowModelRole) => { - const setting = FLOW_MODEL_ROLES[role]; - if (saving || api.lifecycle.signal.aborted) return; - try { - if (!api.state.ready) - throw new Error("OpenCode is still syncing. Try again shortly."); - const directory = api.state.path.directory; - const [global, providers] = await Promise.all([ - api.client.global.config.get({ throwOnError: true }), - api.client.provider.list({ directory }, { throwOnError: true }), - ]); - if (!global.data || !providers.data) - throw new Error( - "OpenCode configuration or model catalog is unavailable.", +export default define({ + id: "opencode-plugin-flow.model-picker", + setup(ctx) { + const rpc = ctx.client.rpc(FlowModelPreferencesRpc); + const location = (): LocationRef => + ctx.location ?? ctx.data.location.default(); + const picker = createModelPicker({ + location, + async read(signal) { + return pickerSnapshotSchema.parse( + await rpc.read({}, { location: location(), signal }), + ); + }, + async save(input, opened, signal) { + return savePreferenceResultSchema.parse( + await rpc.set(input, { location: opened, signal }), + ); + }, + async catalog(opened, signal) { + const [models, providers] = await Promise.all([ + ctx.client.model.list({ location: opened }, { signal }), + ctx.client.provider.list({ location: opened }, { signal }), + ]); + return { models: models.data, providers: providers.data }; + }, + async busy(signal) { + return ( + Object.keys(await ctx.client.session.active({ signal })).length > 0 ); - const config = global.data; - modelPreference(config, role); - const choices = [ + }, + dialog: ctx.ui.dialog, + toast: ctx.ui.toast, + }); + ctx.keymap.layer(() => ({ + commands: [ { - title: "Use default", - value: "", - category: "Default", - description: setting.defaultDescription, - }, - ...modelChoices(providers.data.all, providers.data.connected), - ]; - const current = modelPreference(config, role) ?? ""; - if (current && !choices.some((choice) => choice.value === current)) { - const unavailable = { - title: current, - value: current, - category: "Current preference", - description: "Unavailable here; reconnect or choose another model", - disabled: true, - }; - choices.unshift(unavailable); - } - api.ui.dialog.replace(() => - api.ui.DialogSelect({ - title: `Flow ${setting.title} · global default`, - placeholder: "Search connected models", - current, - options: choices, - onSelect: ({ value }) => { - if (saving || api.lifecycle.signal.aborted) return; - if (value === (modelPreference(config, role) ?? "")) { - api.ui.dialog.clear(); - return; - } - api.ui.dialog.replace(() => - api.ui.DialogConfirm({ - title: `Save Flow ${setting.title}?`, - message: `${value || "Use configured default"}\n\nChanges the global Flow ${setting.title} and reloads OpenCode server instances. Finish work in other projects first. Project picker preferences take precedence. Picker selections use the model’s default reasoning. Use default to restore this role’s default behavior. The coding model stays under OpenCode’s normal model selection.`, - onCancel: () => api.ui.dialog.clear(), - onConfirm: () => { - if (saving || api.lifecycle.signal.aborted) return; - saving = true; - void (async () => { - try { - const [latest, status, catalog] = await Promise.all([ - api.client.global.config.get({ throwOnError: true }), - api.client.session.status( - { directory }, - { throwOnError: true }, - ), - api.client.provider.list( - { directory }, - { throwOnError: true }, - ), - ]); - if (!latest.data || !status.data || !catalog.data) - throw new Error( - "Could not verify current settings and session activity.", - ); - if ( - Object.values(status.data).some( - (session) => session.type !== "idle", - ) - ) - throw new Error( - "Wait for this project's active work to finish before changing Flow models.", - ); - if ( - modelPreference(latest.data, role) !== - modelPreference(config, role) - ) - throw new Error( - "Model preference changed while the picker was open. Open it again.", - ); - if ( - value && - !modelChoices( - catalog.data.all, - catalog.data.connected, - ).some((choice) => choice.value === value) - ) - throw new Error( - "That model is no longer available from a connected provider.", - ); - if (api.lifecycle.signal.aborted) return; - if (api.state.path.directory !== directory) - throw new Error( - "Project changed while the picker was open. Open it again.", - ); - if (value !== (modelPreference(latest.data, role) ?? "")) - await api.client.global.config.update( - { config: modelPreferencePatch(role, value) }, - { throwOnError: true }, - ); - api.ui.dialog.clear(); - api.ui.toast({ - variant: "success", - title: `Flow ${setting.title} saved`, - message: - "Global preference saved. Use /flow-status after reload to check the effective models.", - }); - } catch (cause) { - error(cause); - } finally { - saving = false; - } - })(); - }, - }), - ); - }, - }), - ); - } catch (cause) { - error(cause); - } - }; - api.keymap.registerLayer({ - commands: [ - { - name: "flow.models.select", - title: "Flow: Model settings", - category: "Flow", - namespace: "palette", - slashName: "flow-models", - run: () => { - if (saving || api.lifecycle.signal.aborted) return; - api.ui.dialog.replace(() => - api.ui.DialogSelect({ - title: "Flow model settings", - options: (Object.keys(FLOW_MODEL_ROLES) as FlowModelRole[]).map( - (role) => ({ - title: - role === "planning" ? "Planning specialist" : "Reviewer", - value: role, - description: FLOW_MODEL_ROLES[role].defaultDescription, - }), - ), - onSelect: ({ value }) => { - void open(value); - }, - }), - ); + id: "flow.models.select", + title: "Flow: Model settings", + group: "Flow", + palette: true, + slash: { name: "flow-models" }, + run: () => picker.open(), }, - }, - ], - }); -}; - -export default { - id: "opencode-plugin-flow.model-picker", - tui, -} satisfies TuiPluginModule; + ], + })); + return () => picker.dispose(); + }, +}); diff --git a/tests/architecture-boundaries.test.ts b/tests/architecture-boundaries.test.ts index 13df9bc3..7393acf7 100644 --- a/tests/architecture-boundaries.test.ts +++ b/tests/architecture-boundaries.test.ts @@ -153,18 +153,44 @@ describe("v6 architecture boundaries", () => { expect(unused).toEqual([]); }); - test("routes every OpenCode host import through src/platform/opencode/sdk.ts", async () => { - // One seam holds the peer-dependency surface, and it is the only host import - // the published declarations expose. It had already drifted once, so it is - // enforced here rather than described in prose. - const importers: string[] = []; + test("keeps native OpenCode imports in the host adapters and RPC contract", async () => { + const violations: string[] = []; for (const file of await sourceFiles()) { const source = await readFile(file, "utf8"); - if (/(?:from\s+|import\s*)["']@opencode-ai\//.test(source)) { - importers.push(repositoryPath(file)); - } + const path = repositoryPath(file); + if (/(?:from\s+|import\s*)["']@opencode-ai\//.test(source)) + violations.push(path); + if ( + /(?:from\s+|import\s*)["']@opencode\//.test(source) && + !path.startsWith("src/platform/opencode/") && + path !== "src/tui.ts" && + path !== "src/flow-rpc.ts" + ) + violations.push(path); + } + expect(violations).toEqual([]); + }); + + test("keeps active host tooling free of V1 imports and launch packages", async () => { + const files = ( + await Promise.all( + ["scripts", "evals"].map((directory) => + sourceFiles(join(repositoryRoot, directory)), + ), + ) + ).flat(); + const violations: string[] = []; + for (const file of files) { + const path = repositoryPath(file); + if (path.startsWith("evals/qualification/")) continue; + const source = await readFile(file, "utf8"); + if ( + /(?:from\s+|import\s*)["']@opencode-ai\//.test(source) || + /opencode-ai@/.test(source) + ) + violations.push(path); } - expect(importers).toEqual(["src/platform/opencode/sdk.ts"]); + expect(violations).toEqual([]); }); test("keeps domain, application, and infrastructure dependencies inward", async () => { @@ -185,7 +211,7 @@ describe("v6 architecture boundaries", () => { if ( (layer === "domain" || layer === "application") && - /(?:from\s+|import\s*)["']@opencode-ai\//.test(source) + /(?:from\s+|import\s*)["']@opencode(?:-ai)?\//.test(source) ) { violations.push( `${relative(sourceRoot, file)} imports the OpenCode host`, diff --git a/tests/auto-drive.test.ts b/tests/auto-drive.test.ts index c6da88f6..64e784f7 100644 --- a/tests/auto-drive.test.ts +++ b/tests/auto-drive.test.ts @@ -1,4 +1,8 @@ import { describe, expect, test } from "bun:test"; +import { Agent } from "@opencode/plugin"; +import { CallID } from "@opencode/plugin/promise/tool"; +import { Session } from "@opencode/schema/session"; +import { SessionMessage } from "@opencode/schema/session-message"; import type { ObservedValidation } from "../src/application/prepare-validation.js"; import { FLOW_MANAGER_KERNEL } from "../src/guidance/catalog.js"; import { @@ -15,6 +19,22 @@ const DELIVERY: AutoDriveDelivery = { agent: "build", model: { providerID: "provider", modelID: "model" }, }; +const WORKSPACE = process.cwd(); +const readValidationSession = async () => ({ + id: "session-host1", + agent: "build", + location: { directory: WORKSPACE }, +}); +function shellExecution(command: string, id: string, assistant: string) { + return { + tool: "shell", + sessionID: Session.ID.make("session-host1"), + agent: Agent.ID.make("build"), + messageID: SessionMessage.ID.make(assistant), + id: CallID.make(id), + input: { command }, + }; +} const VALIDATION_SOURCE = `sha256:${"a".repeat(64)}` as const; function observation(input: ObservedValidation, revision: number) { return { ...input, id: input.captureId, recordedRevision: revision }; @@ -30,7 +50,7 @@ function mutate( mutation?: { tool: string; goal: string; features: Array<{ kind?: string }> }, ): void { const id = `assistant-${++assistantSequence}`; - driver.observeHostMessage(host, { id, role: "assistant", parentID: parent }); + driver.observeAssistant(host, id, parent); driver.observeMutation( host, revision, @@ -46,32 +66,10 @@ function compact( authority: string, successor: string, ): void { - const trigger = `assistant-${++assistantSequence}`; - const user = `compaction-${assistantSequence}`; - driver.observeHostMessage(host, { - id: trigger, - role: "assistant", - parentID: authority, - }); - driver.observeHostPart(host, { - type: "compaction", - messageID: user, - auto: true, - }); - driver.observeHostMessage(host, { - id: `summary-${assistantSequence}`, - role: "assistant", - parentID: user, - summary: true, - }); - driver.observeHostMessage(host, { id: successor, role: "user" }); - driver.observeHostPart(host, { - type: "text", - messageID: successor, - synthetic: true, - metadata: { compaction_continue: true }, - }); - driver.observeCompaction(host); + const epoch = driver.captureAuthority(host); + if (!epoch) + throw new Error("Missing accepted authority in compaction fixture."); + driver.observeCompaction(host, epoch, authority, successor); } function harness(initial: AutoDriveProjection) { @@ -101,7 +99,7 @@ function harness(initial: AutoDriveProjection) { prompts, warnings, async activate( - sessionID = "host-1", + sessionID = "session-host1", delivery = DELIVERY, messageId = "command-message", goal = "", @@ -166,16 +164,16 @@ describe("Flow auto-drive coordinator", () => { ...DELIVERY, model: { ...DELIVERY.model, variant: "high" }, }); - await state.activate("host-1", delivery); - compact(state.driver, "host-1", "command-message", "compacted-user"); + await state.activate("session-host1", delivery); + compact(state.driver, "session-host1", "command-message", "compacted-user"); state.setProjection({ sessionId: "flow-1", status: "ready", revision: 12, nextAction: "flow_run_start", }); - mutate(state.driver, "host-1", 12, undefined, "compacted-user"); - await state.driver.onIdle("host-1"); + mutate(state.driver, "session-host1", 12, undefined, "compacted-user"); + await state.driver.onIdle("session-host1"); expect(state.prompts.map((prompt) => prompt.delivery)).toEqual([delivery]); }); @@ -186,10 +184,10 @@ describe("Flow auto-drive coordinator", () => { revision: 4, nextAction: "flow_plan_approve", }); - await state.activate("host-1", { ...DELIVERY, variant: "high" }); - await state.driver.onIdle("host-1"); + await state.activate("session-host1", { ...DELIVERY, variant: "high" }); + await state.driver.onIdle("session-host1"); await state.driver.observeMessage( - "host-1", + "session-host1", autoDriveDelivery(DELIVERY), [{ synthetic: false }], "reply", @@ -200,8 +198,8 @@ describe("Flow auto-drive coordinator", () => { revision: 5, nextAction: "flow_run_start", }); - mutate(state.driver, "host-1", 5, undefined, "reply"); - await state.driver.onIdle("host-1"); + mutate(state.driver, "session-host1", 5, undefined, "reply"); + await state.driver.onIdle("session-host1"); expect(state.prompts.map((prompt) => prompt.delivery)).toEqual([DELIVERY]); }); @@ -219,10 +217,10 @@ describe("Flow auto-drive coordinator", () => { revision: 12, nextAction: "flow_run_start", }); - mutate(state.driver, "host-1", 12); + mutate(state.driver, "session-host1", 12); state.setNow(25); - await state.driver.onIdle("host-1"); + await state.driver.onIdle("session-host1"); expect(state.prompts).toHaveLength(1); expect(state.prompts[0]).toMatchObject({ delivery: DELIVERY, @@ -235,10 +233,10 @@ describe("Flow auto-drive coordinator", () => { ); expect(state.prompts[0]?.text).toContain("replay archiveRetry exactly"); - await state.driver.onIdle("host-1"); + await state.driver.onIdle("session-host1"); expect(state.prompts).toHaveLength(1); expect(state.warnings.at(-1)).toContain("made no lifecycle progress"); - expect(state.driver.compactionContext("host-1")).not.toBeNull(); + expect(state.driver.compactionContext("session-host1")).not.toBeNull(); state.setNow(1_000); expect(state.driver.timingSnapshot()).toMatchObject({ state: "paused", @@ -247,51 +245,17 @@ describe("Flow auto-drive coordinator", () => { }); }); - test("reports continuation support before anything depends on it", async () => { - // The preflight half of the same problem. The warning above arrives after the - // first continuation has already failed; this is what lets `/flow-auto` say so - // at the start, and it has three states because the negative needs evidence: - // no assistant message yet is not a host that cannot do it. - const state = harness({ - sessionId: "flow-1", - status: "ready", - revision: 11, - nextAction: "flow_run_start", - }); - expect(state.driver.continuationSupport()).toBe("unknown"); - state.driver.observeHostMessage("host-1", { - id: "assistant-parentless", - role: "assistant", - }); - expect(state.driver.continuationSupport()).toBe("unsupported"); - // One parented assistant message settles it: the host does report parentage, - // and an earlier message without one says nothing about the capability. - state.driver.observeHostMessage("host-1", { - id: "assistant-parented", - role: "assistant", - parentID: "command-message", - }); - expect(state.driver.continuationSupport()).toBe("supported"); - }); - - test("keeps a user message out of the parentage signal", () => { - // User messages carry no `parentID` and never did; counting them would report - // every host as incapable. - const state = harness({ - sessionId: "flow-1", - status: "ready", - revision: 1, - nextAction: "flow_run_start", - }); - state.driver.observeHostMessage("host-1", { id: "user-1", role: "user" }); - expect(state.driver.continuationSupport()).toBe("unknown"); + test("reports the required v2 request attribution capability", () => { + expect( + harness({ + status: "idle", + revision: 0, + nextAction: null, + }).driver.continuationSupport(), + ).toBe("supported"); }); - test("names a host that reports no assistant message parentage", async () => { - // Continuation anchors on the assistant message that owns the lease, so a - // host emitting no `parentID` can never continue. It must fail closed, but - // the warning has to say the host cannot support it: otherwise stopping - // after every feature is indistinguishable from a Flow defect. + test("rejects a mutation without verified execution origin", async () => { const state = harness({ sessionId: "flow-1", status: "ready", @@ -299,16 +263,20 @@ describe("Flow auto-drive coordinator", () => { nextAction: "flow_run_start", }); await state.activate(); - state.driver.observeMutation("host-1", 12, undefined, "assistant-x", false); + state.driver.observeMutation( + "session-host1", + 12, + undefined, + "assistant-x", + false, + ); - await state.driver.onIdle("host-1"); + await state.driver.onIdle("session-host1"); expect(state.prompts).toHaveLength(0); - expect(state.warnings.at(-1)).toContain( - "reports no assistant message parentage", + expect(state.warnings.at(-1)).toBe( + "Flow: mutation origin was unavailable.", ); - expect(state.warnings.at(-1)).toContain("/flow-run"); - // A host that does report parentage keeps the precise diagnosis. const capable = harness({ sessionId: "flow-1", status: "ready", @@ -316,9 +284,9 @@ describe("Flow auto-drive coordinator", () => { nextAction: "flow_run_start", }); await capable.activate(); - mutate(capable.driver, "host-1", 12); + mutate(capable.driver, "session-host1", 12); capable.driver.observeMutation( - "host-1", + "session-host1", 13, undefined, "assistant-unknown", @@ -338,7 +306,7 @@ describe("Flow auto-drive coordinator", () => { }); await state.activate(); state.setNow(10); - await state.driver.onIdle("host-1"); + await state.driver.onIdle("session-host1"); state.setNow(21_600_010); expect(state.driver.timingSnapshot()).toMatchObject({ @@ -353,7 +321,7 @@ describe("Flow auto-drive coordinator", () => { model: { providerID: "provider", modelID: "new-model" }, }; await state.driver.observeMessage( - "host-1", + "session-host1", resumedDelivery, [{ synthetic: false }], "checkpoint-reply", @@ -364,9 +332,9 @@ describe("Flow auto-drive coordinator", () => { revision: 21, nextAction: "flow_run_start", }); - mutate(state.driver, "host-1", 21, undefined, "checkpoint-reply"); + mutate(state.driver, "session-host1", 21, undefined, "checkpoint-reply"); state.setNow(21_600_015); - await state.driver.onIdle("host-1"); + await state.driver.onIdle("session-host1"); expect(state.prompts).toHaveLength(2); expect(state.prompts[1]?.delivery).toEqual(resumedDelivery); @@ -386,14 +354,14 @@ describe("Flow auto-drive coordinator", () => { nextAction: "await-user-direction", }); await state.activate(); - await state.driver.onIdle("host-1"); + await state.driver.onIdle("session-host1"); if (status === "blocked") { expect(state.prompts[0]?.text).toContain( "Report workflowData.statusReport verbatim before stopping at await-user-direction.", ); expect(state.prompts[0]?.text).toContain("detail view once"); } - await state.driver.onIdle("host-1"); + await state.driver.onIdle("session-host1"); expect(state.prompts).toHaveLength(status === "blocked" ? 1 : 0); expect(state.driver.timingSnapshot()?.state).toBe("waiting-for-user"); }); @@ -407,22 +375,22 @@ describe("Flow auto-drive coordinator", () => { nextAction: "await-user-direction", }); await state.activate(); - await state.driver.onIdle("host-1"); + await state.driver.onIdle("session-host1"); const clarificationDelivery: AutoDriveDelivery = { agent: "build-clarification", model: { providerID: "provider", modelID: "clarification-model" }, }; await state.driver.observeMessage( - "host-1", + "session-host1", clarificationDelivery, [{ synthetic: false }], "clarification-reply", ); - await state.driver.onIdle("host-1"); + await state.driver.onIdle("session-host1"); expect(state.prompts).toHaveLength(1); - expect(state.driver.compactionContext("host-1")).not.toBeNull(); + expect(state.driver.compactionContext("session-host1")).not.toBeNull(); expect(state.driver.timingSnapshot()?.state).toBe("waiting-for-user"); const recommendationDelivery: AutoDriveDelivery = { @@ -430,15 +398,15 @@ describe("Flow auto-drive coordinator", () => { model: { providerID: "provider", modelID: "recommendation-model" }, }; await state.driver.observeMessage( - "host-1", + "session-host1", recommendationDelivery, [{ synthetic: false }], "recommendation-reply", ); - await state.driver.onIdle("host-1"); + await state.driver.onIdle("session-host1"); expect(state.prompts).toHaveLength(1); - expect(state.driver.compactionContext("host-1")).not.toBeNull(); + expect(state.driver.compactionContext("session-host1")).not.toBeNull(); expect(state.driver.timingSnapshot()?.state).toBe("waiting-for-user"); const approvalDelivery: AutoDriveDelivery = { @@ -446,7 +414,7 @@ describe("Flow auto-drive coordinator", () => { model: { providerID: "provider", modelID: "approved-model" }, }; await state.driver.observeMessage( - "host-1", + "session-host1", approvalDelivery, [{ synthetic: false }], "approval-reply", @@ -457,9 +425,9 @@ describe("Flow auto-drive coordinator", () => { revision: 21, nextAction: "flow_run_start", }); - mutate(state.driver, "host-1", 21, undefined, "approval-reply"); - await state.driver.onIdle("host-1"); - await state.driver.onIdle("host-1"); + mutate(state.driver, "session-host1", 21, undefined, "approval-reply"); + await state.driver.onIdle("session-host1"); + await state.driver.onIdle("session-host1"); expect(state.prompts).toHaveLength(2); expect(state.prompts[1]?.delivery).toEqual(approvalDelivery); @@ -473,24 +441,24 @@ describe("Flow auto-drive coordinator", () => { nextAction: "flow_plan_approve", }); await state.activate(); - await state.driver.onIdle("host-1"); + await state.driver.onIdle("session-host1"); state.setProjection({ sessionId: "flow-1", status: "ready", revision: 5, nextAction: "flow_run_start", }); - mutate(state.driver, "host-1", 5); + mutate(state.driver, "session-host1", 5); await state.driver.observeMessage( - "host-1", + "session-host1", DELIVERY, [{ synthetic: false }], "late-reply", ); - await state.driver.onIdle("host-1"); + await state.driver.onIdle("session-host1"); expect(state.prompts).toHaveLength(0); - expect(state.driver.compactionContext("host-1")).toBeNull(); + expect(state.driver.compactionContext("session-host1")).toBeNull(); }); test("keeps initial command authority when its turn advances a checkpoint", async () => { @@ -507,11 +475,11 @@ describe("Flow auto-drive coordinator", () => { revision: 5, nextAction: "flow_run_start", }); - mutate(state.driver, "host-1", 5); - await state.driver.onIdle("host-1"); + mutate(state.driver, "session-host1", 5); + await state.driver.onIdle("session-host1"); expect(state.prompts).toHaveLength(1); - expect(state.driver.compactionContext("host-1")).not.toBeNull(); + expect(state.driver.compactionContext("session-host1")).not.toBeNull(); }); test("rejects unowned checkpoint progress before the first idle", async () => { @@ -521,17 +489,17 @@ describe("Flow auto-drive coordinator", () => { revision: 4, nextAction: "flow_plan_approve", }); - await state.activate("host-1", DELIVERY, "command-message"); + await state.activate("session-host1", DELIVERY, "command-message"); state.setProjection({ sessionId: "flow-1", status: "ready", revision: 5, nextAction: "flow_run_start", }); - await state.driver.onIdle("host-1"); + await state.driver.onIdle("session-host1"); expect(state.prompts).toHaveLength(0); - expect(state.driver.compactionContext("host-1")).toBeNull(); + expect(state.driver.compactionContext("session-host1")).toBeNull(); }); test("allows temporal progress only for an already-pending reviewer", async () => { @@ -541,14 +509,14 @@ describe("Flow auto-drive coordinator", () => { revision: 4, nextAction: "dispatch-flow-reviewer", }); - await state.activate("host-1", DELIVERY, "command-message"); + await state.activate("session-host1", DELIVERY, "command-message"); state.setProjection({ sessionId: "flow-1", status: "ready", revision: 5, nextAction: "flow_run_start", }); - await state.driver.onIdle("host-1"); + await state.driver.onIdle("session-host1"); expect(state.prompts).toHaveLength(1); }); @@ -564,10 +532,10 @@ describe("Flow auto-drive coordinator", () => { revision: 4, nextAction: "flow_plan_approve", }); - await state.activate("host-1", DELIVERY, "command-message"); - await state.driver.onIdle("host-1"); + await state.activate("session-host1", DELIVERY, "command-message"); + await state.driver.onIdle("session-host1"); await state.driver.observeMessage( - "host-1", + "session-host1", DELIVERY, [{ text: "Explain the tradeoff before proceeding." }], "checkpoint-reply", @@ -578,11 +546,11 @@ describe("Flow auto-drive coordinator", () => { revision: 5, nextAction: "flow_run_start", }); - mutate(state.driver, "host-1", 5, undefined, parentMessage); - await state.driver.onIdle("host-1"); + mutate(state.driver, "session-host1", 5, undefined, parentMessage); + await state.driver.onIdle("session-host1"); expect(state.prompts).toHaveLength(expectedPrompts); - expect(state.driver.compactionContext("host-1") !== null).toBe( + expect(state.driver.compactionContext("session-host1") !== null).toBe( expectedPrompts === 1, ); } @@ -595,17 +563,17 @@ describe("Flow auto-drive coordinator", () => { revision: 4, nextAction: "flow_plan_approve", }); - await state.activate("host-1", DELIVERY, "command-message"); - await state.driver.onIdle("host-1"); + await state.activate("session-host1", DELIVERY, "command-message"); + await state.driver.onIdle("session-host1"); await state.driver.observeMessage( - "host-1", + "session-host1", DELIVERY, [{ text: "Approve after compaction." }], "checkpoint-reply", ); compact( state.driver, - "host-1", + "session-host1", "checkpoint-reply", "compaction-continuation", ); @@ -615,8 +583,14 @@ describe("Flow auto-drive coordinator", () => { revision: 5, nextAction: "flow_run_start", }); - mutate(state.driver, "host-1", 5, undefined, "compaction-continuation"); - await state.driver.onIdle("host-1"); + mutate( + state.driver, + "session-host1", + 5, + undefined, + "compaction-continuation", + ); + await state.driver.onIdle("session-host1"); expect(state.prompts).toHaveLength(1); }); @@ -628,17 +602,17 @@ describe("Flow auto-drive coordinator", () => { revision: 4, nextAction: "flow_plan_approve", }); - await state.activate("host-1", DELIVERY, "command-message"); - await state.driver.onIdle("host-1"); + await state.activate("session-host1", DELIVERY, "command-message"); + await state.driver.onIdle("session-host1"); await state.driver.observeMessage( - "host-1", + "session-host1", DELIVERY, [{ text: "Approve after compaction." }], "checkpoint-reply", ); compact( state.driver, - "host-1", + "session-host1", "checkpoint-reply", "compaction-continuation", ); @@ -648,11 +622,11 @@ describe("Flow auto-drive coordinator", () => { revision: 5, nextAction: "flow_run_start", }); - mutate(state.driver, "host-1", 5, undefined, "checkpoint-reply"); - await state.driver.onIdle("host-1"); + mutate(state.driver, "session-host1", 5, undefined, "checkpoint-reply"); + await state.driver.onIdle("session-host1"); expect(state.prompts).toHaveLength(0); - expect(state.driver.compactionContext("host-1")).toBeNull(); + expect(state.driver.compactionContext("session-host1")).toBeNull(); }); test("rejects compaction triggered by an assistant from older authority", async () => { @@ -662,239 +636,28 @@ describe("Flow auto-drive coordinator", () => { revision: 4, nextAction: "flow_plan_approve", }); - await state.activate("host-1", DELIVERY, "command-message"); - await state.driver.onIdle("host-1"); + await state.activate("session-host1", DELIVERY, "command-message"); + await state.driver.onIdle("session-host1"); await state.driver.observeMessage( - "host-1", + "session-host1", DELIVERY, [{ text: "This is the current checkpoint reply." }], "checkpoint-reply", ); - compact(state.driver, "host-1", "older-message", "compaction-continuation"); - - expect(state.prompts).toHaveLength(0); - expect(state.driver.compactionContext("host-1")).toBeNull(); - expect(state.warnings.at(-1)).toContain( - "compaction origin was unavailable", - ); - }); - - test("rejects manual or unclassified compaction markers", async () => { - for (const auto of [false, undefined]) { - const state = harness({ - sessionId: "flow-1", - status: "planning", - revision: 4, - nextAction: "flow_plan_approve", - }); - await state.activate("host-1", DELIVERY, "command-message"); - await state.driver.onIdle("host-1"); - await state.driver.observeMessage( - "host-1", - DELIVERY, - [{ text: "Approve after compaction." }], - "checkpoint-reply", - ); - const trigger = `assistant-${++assistantSequence}`; - const compaction = `compaction-${assistantSequence}`; - state.driver.observeHostMessage("host-1", { - id: trigger, - role: "assistant", - parentID: "checkpoint-reply", - }); - state.driver.observeHostPart( - "host-1", - auto === undefined - ? { type: "compaction", messageID: compaction } - : { type: "compaction", messageID: compaction, auto }, - ); - state.driver.observeHostMessage("host-1", { - id: `summary-${assistantSequence}`, - role: "assistant", - parentID: compaction, - summary: true, - }); - state.driver.observeHostMessage("host-1", { - id: "compaction-continuation", - role: "user", - }); - state.driver.observeCompaction("host-1"); - - expect(state.prompts).toHaveLength(0); - expect(state.driver.compactionContext("host-1")).toBeNull(); - expect(state.warnings.at(-1)).toContain( - "compaction origin was unavailable", - ); - } - }); - - test("rejects a compaction summary with the wrong parent", async () => { - const state = harness({ - sessionId: "flow-1", - status: "planning", - revision: 4, - nextAction: "flow_plan_approve", - }); - await state.activate("host-1", DELIVERY, "command-message"); - await state.driver.onIdle("host-1"); - await state.driver.observeMessage( - "host-1", - DELIVERY, - [{ text: "Approve after compaction." }], - "checkpoint-reply", + compact( + state.driver, + "session-host1", + "older-message", + "compaction-continuation", ); - const trigger = `assistant-${++assistantSequence}`; - const compaction = `compaction-${assistantSequence}`; - state.driver.observeHostMessage("host-1", { - id: trigger, - role: "assistant", - parentID: "checkpoint-reply", - }); - state.driver.observeHostPart("host-1", { - type: "compaction", - messageID: compaction, - auto: true, - }); - state.driver.observeHostMessage("host-1", { - id: `summary-${assistantSequence}`, - role: "assistant", - parentID: "wrong-compaction-message", - summary: true, - }); - state.driver.observeHostMessage("host-1", { - id: "compaction-continuation", - role: "user", - }); - state.driver.observeHostPart("host-1", { - type: "text", - messageID: "compaction-continuation", - synthetic: true, - metadata: { compaction_continue: true }, - }); - state.driver.observeCompaction("host-1"); expect(state.prompts).toHaveLength(0); - expect(state.driver.compactionContext("host-1")).toBeNull(); + expect(state.driver.compactionContext("session-host1")).toBeNull(); expect(state.warnings.at(-1)).toContain( "compaction origin was unavailable", ); }); - test("rejects compaction with a missing or non-adjacent successor", async () => { - for (const successor of ["missing", "non-adjacent"] as const) { - const state = harness({ - sessionId: "flow-1", - status: "planning", - revision: 4, - nextAction: "flow_plan_approve", - }); - await state.activate("host-1", DELIVERY, "command-message"); - await state.driver.onIdle("host-1"); - await state.driver.observeMessage( - "host-1", - DELIVERY, - [{ text: "Approve after compaction." }], - "checkpoint-reply", - ); - const trigger = `assistant-${++assistantSequence}`; - const compaction = `compaction-${assistantSequence}`; - state.driver.observeHostMessage("host-1", { - id: trigger, - role: "assistant", - parentID: "checkpoint-reply", - }); - state.driver.observeHostPart("host-1", { - type: "compaction", - messageID: compaction, - auto: true, - }); - state.driver.observeHostMessage("host-1", { - id: `summary-${assistantSequence}`, - role: "assistant", - parentID: compaction, - summary: true, - }); - if (successor === "non-adjacent") { - state.driver.observeHostMessage("host-1", { - id: `intervening-${assistantSequence}`, - role: "assistant", - parentID: "unrelated-message", - }); - state.driver.observeHostMessage("host-1", { - id: "compaction-continuation", - role: "user", - }); - } - state.driver.observeCompaction("host-1"); - - expect(state.prompts).toHaveLength(0); - expect(state.driver.compactionContext("host-1")).toBeNull(); - expect(state.warnings.at(-1)).toContain( - "compaction origin was unavailable", - ); - } - }); - - test("ignores an unrelated compacted notification without consuming a candidate", async () => { - const state = harness({ - sessionId: "flow-1", - status: "planning", - revision: 4, - nextAction: "flow_plan_approve", - }); - await state.activate("host-1", DELIVERY, "command-message"); - await state.driver.onIdle("host-1"); - await state.driver.observeMessage( - "host-1", - DELIVERY, - [{ text: "Approve after compaction." }], - "checkpoint-reply", - ); - const trigger = `assistant-${++assistantSequence}`; - const compaction = `compaction-${assistantSequence}`; - state.driver.observeHostMessage("host-1", { - id: trigger, - role: "assistant", - parentID: "checkpoint-reply", - }); - state.driver.observeHostPart("host-1", { - type: "compaction", - messageID: compaction, - auto: true, - }); - state.driver.observeHostMessage("host-1", { - id: `summary-${assistantSequence}`, - role: "assistant", - parentID: compaction, - summary: true, - }); - state.driver.observeHostMessage("host-1", { - id: "compaction-continuation", - role: "user", - }); - state.driver.observeHostPart("host-1", { - type: "text", - messageID: "compaction-continuation", - synthetic: true, - metadata: { compaction_continue: true }, - }); - - state.driver.observeCompaction("host-2"); - expect(state.driver.compactionContext("host-1")).not.toBeNull(); - - state.driver.observeCompaction("host-1"); - state.setProjection({ - sessionId: "flow-1", - status: "ready", - revision: 5, - nextAction: "flow_run_start", - }); - mutate(state.driver, "host-1", 5, undefined, "compaction-continuation"); - await state.driver.onIdle("host-1"); - - expect(state.prompts).toHaveLength(1); - }); - test("accepts a baseline checkpoint reply before the first idle", async () => { const state = harness({ sessionId: "flow-1", @@ -908,7 +671,7 @@ describe("Flow auto-drive coordinator", () => { model: { providerID: "provider", modelID: "approved-model" }, }; await state.driver.observeMessage( - "host-1", + "session-host1", replyDelivery, [{ synthetic: false }], "baseline-reply", @@ -920,13 +683,13 @@ describe("Flow auto-drive coordinator", () => { revision: 5, nextAction: "flow_run_start", }); - mutate(state.driver, "host-1", 5, undefined, "baseline-reply"); - await state.driver.onIdle("host-1"); + mutate(state.driver, "session-host1", 5, undefined, "baseline-reply"); + await state.driver.onIdle("session-host1"); expect(state.prompts.map(({ delivery }) => delivery)).toEqual([ replyDelivery, ]); - expect(state.driver.compactionContext("host-1")).not.toBeNull(); + expect(state.driver.compactionContext("session-host1")).not.toBeNull(); }); test("does not carry a reply across a newer checkpoint", async () => { @@ -937,7 +700,7 @@ describe("Flow auto-drive coordinator", () => { nextAction: "flow_plan_approve", }); await state.activate(); - await state.driver.onIdle("host-1"); + await state.driver.onIdle("session-host1"); state.setProjection({ sessionId: "flow-1", status: "blocked", @@ -945,7 +708,7 @@ describe("Flow auto-drive coordinator", () => { nextAction: "await-user-direction", }); await state.driver.observeMessage( - "host-1", + "session-host1", DELIVERY, [{ synthetic: false }], "new-checkpoint-reply", @@ -958,21 +721,21 @@ describe("Flow auto-drive coordinator", () => { revision: 7, nextAction: "flow_run_start", }); - mutate(state.driver, "host-1", 7, undefined, "new-checkpoint-reply"); - await state.driver.onIdle("host-1"); + mutate(state.driver, "session-host1", 7, undefined, "new-checkpoint-reply"); + await state.driver.onIdle("session-host1"); expect(state.prompts).toHaveLength(1); expect(state.prompts[0]?.text).toContain("findingsDigest"); - expect(state.driver.compactionContext("host-1")).toBeNull(); + expect(state.driver.compactionContext("session-host1")).toBeNull(); }); test("requires an accepted mutation from the checkpoint replying host", async () => { for (const [mutationHost, mutationRevision, projectedRevision, prompts] of [ ["host-2", 5, 5, 0], - ["host-1", 4, 5, 0], - ["host-1", 6, 5, 0], - ["host-1", 5, 5, 1], - ["host-1", 5, 6, 0], + ["session-host1", 4, 5, 0], + ["session-host1", 6, 5, 0], + ["session-host1", 5, 5, 1], + ["session-host1", 5, 6, 0], ] as const) { const state = harness({ sessionId: "flow-1", @@ -981,9 +744,9 @@ describe("Flow auto-drive coordinator", () => { nextAction: "flow_plan_approve", }); await state.activate(); - await state.driver.onIdle("host-1"); + await state.driver.onIdle("session-host1"); await state.driver.observeMessage( - "host-1", + "session-host1", DELIVERY, [{ synthetic: false }], "checkpoint-reply", @@ -1001,10 +764,10 @@ describe("Flow auto-drive coordinator", () => { revision: projectedRevision, nextAction: "flow_run_start", }); - await state.driver.onIdle("host-1"); + await state.driver.onIdle("session-host1"); expect(state.prompts).toHaveLength(prompts); - expect(state.driver.compactionContext("host-1") !== null).toBe( + expect(state.driver.compactionContext("session-host1") !== null).toBe( prompts === 1, ); } @@ -1022,24 +785,31 @@ describe("Flow auto-drive coordinator", () => { nextAction: "flow_plan_approve", }); await state.activate(); - await state.driver.onIdle("host-1"); + await state.driver.onIdle("session-host1"); await state.driver.observeMessage( - "host-1", + "session-host1", DELIVERY, [{ synthetic: false }], "checkpoint-reply", ); - mutate(state.driver, "host-1", 5, undefined, "checkpoint-reply", true); + mutate( + state.driver, + "session-host1", + 5, + undefined, + "checkpoint-reply", + true, + ); state.setProjection({ sessionId: "flow-1", status: "ready", revision: projectedRevision, nextAction: "flow_run_start", }); - await state.driver.onIdle("host-1"); + await state.driver.onIdle("session-host1"); expect(state.prompts).toHaveLength(prompts); - expect(state.driver.compactionContext("host-1") !== null).toBe( + expect(state.driver.compactionContext("session-host1") !== null).toBe( prompts === 1, ); } @@ -1060,7 +830,7 @@ describe("Flow auto-drive coordinator", () => { nextAction: "flow_validation_start", }); state.setNow(8); - await state.driver.onIdle("host-1"); + await state.driver.onIdle("session-host1"); state.setNow(1_000); expect(state.driver.timingSnapshot()).toMatchObject({ state: "inactive", @@ -1146,8 +916,8 @@ describe("Flow auto-drive coordinator", () => { await state.activate(); state.setProjection(projection); if (shouldPrompt && projection.status !== "idle") - mutate(state.driver, "host-1", projection.revision); - await state.driver.onIdle("host-1"); + mutate(state.driver, "session-host1", projection.revision); + await state.driver.onIdle("session-host1"); const handback = projection.status === "blocked" || projection.nextAction === "flow_feature_reset" || @@ -1164,16 +934,16 @@ describe("Flow auto-drive coordinator", () => { nextAction: "flow_feature_reset", }); await state.activate(); - await state.driver.onIdle("host-1"); + await state.driver.onIdle("session-host1"); expect(state.prompts).toHaveLength(1); expect(state.prompts[0]?.text).toContain("findingsDigest"); expect(state.prompts[0]?.text).toContain("flow_feature_reset"); - expect(state.driver.compactionContext("host-1")).not.toBeNull(); + expect(state.driver.compactionContext("session-host1")).not.toBeNull(); - await state.driver.onIdle("host-1"); + await state.driver.onIdle("session-host1"); expect(state.prompts).toHaveLength(1); - expect(state.driver.compactionContext("host-1")).toBeNull(); + expect(state.driver.compactionContext("session-host1")).toBeNull(); const afterLifecycle = harness({ status: "idle", @@ -1187,15 +957,15 @@ describe("Flow auto-drive coordinator", () => { revision: 2, nextAction: "flow_run_start", }); - mutate(afterLifecycle.driver, "host-1", 2, "flow-1"); - await afterLifecycle.driver.onIdle("host-1"); + mutate(afterLifecycle.driver, "session-host1", 2, "flow-1"); + await afterLifecycle.driver.onIdle("session-host1"); expect(afterLifecycle.prompts).toHaveLength(1); afterLifecycle.setProjection({ status: "idle", revision: 0, nextAction: "flow_plan_save", }); - await afterLifecycle.driver.onIdle("host-1"); + await afterLifecycle.driver.onIdle("session-host1"); expect(afterLifecycle.prompts).toHaveLength(1); }); @@ -1207,11 +977,11 @@ describe("Flow auto-drive coordinator", () => { nextAction: "dispatch-flow-reviewer", }); await state.activate(); - await state.driver.onIdle("host-1"); + await state.driver.onIdle("session-host1"); expect(state.prompts).toHaveLength(1); expect(state.prompts[0]?.text).toContain("findingsDigest"); - await state.driver.onIdle("host-1"); + await state.driver.onIdle("session-host1"); expect(state.prompts).toHaveLength(1); }); @@ -1223,14 +993,14 @@ describe("Flow auto-drive coordinator", () => { }); await state.activate(); - await state.driver.onIdle("host-1"); + await state.driver.onIdle("session-host1"); expect(state.prompts).toHaveLength(1); expect(state.prompts[0]?.text).toContain("flow_plan_save"); - await state.driver.onIdle("host-1"); + await state.driver.onIdle("session-host1"); expect(state.prompts).toHaveLength(1); - expect(state.driver.compactionContext("host-1")).toBeNull(); - expect(state.driver.continuationSnapshot("host-1")).toMatchObject({ + expect(state.driver.compactionContext("session-host1")).toBeNull(); + expect(state.driver.continuationSnapshot("session-host1")).toMatchObject({ state: "inactive", reason: "no-progress", }); @@ -1244,9 +1014,9 @@ describe("Flow auto-drive coordinator", () => { nextAction: "flow_run_start", }); await unchanged.activate(); - await unchanged.driver.onIdle("host-1"); + await unchanged.driver.onIdle("session-host1"); expect(unchanged.prompts).toHaveLength(0); - expect(unchanged.driver.compactionContext("host-1")).toBeNull(); + expect(unchanged.driver.compactionContext("session-host1")).toBeNull(); const created = harness({ status: "idle", @@ -1260,8 +1030,8 @@ describe("Flow auto-drive coordinator", () => { revision: 2, nextAction: "flow_run_start", }); - mutate(created.driver, "host-1", 2, "flow-new"); - await created.driver.onIdle("host-1"); + mutate(created.driver, "session-host1", 2, "flow-new"); + await created.driver.onIdle("session-host1"); expect(created.prompts).toHaveLength(1); const rebound = harness({ @@ -1270,17 +1040,17 @@ describe("Flow auto-drive coordinator", () => { nextAction: "flow_plan_save", }); await rebound.activate(); - mutate(rebound.driver, "host-1", 1, "flow-first"); - mutate(rebound.driver, "host-1", 1, "flow-replacement"); + mutate(rebound.driver, "session-host1", 1, "flow-first"); + mutate(rebound.driver, "session-host1", 1, "flow-replacement"); rebound.setProjection({ sessionId: "flow-replacement", status: "ready", revision: 2, nextAction: "flow_run_start", }); - await rebound.driver.onIdle("host-1"); + await rebound.driver.onIdle("session-host1"); expect(rebound.prompts).toHaveLength(0); - expect(rebound.driver.compactionContext("host-1")).toBeNull(); + expect(rebound.driver.compactionContext("session-host1")).toBeNull(); const foreignCreated = harness({ status: "idle", @@ -1295,10 +1065,10 @@ describe("Flow auto-drive coordinator", () => { nextAction: "flow_run_start", }); mutate(foreignCreated.driver, "host-2", 1, "flow-foreign"); - mutate(foreignCreated.driver, "host-1", 2); - await foreignCreated.driver.onIdle("host-1"); + mutate(foreignCreated.driver, "session-host1", 2); + await foreignCreated.driver.onIdle("session-host1"); expect(foreignCreated.prompts).toHaveLength(0); - expect(foreignCreated.driver.compactionContext("host-1")).toBeNull(); + expect(foreignCreated.driver.compactionContext("session-host1")).toBeNull(); const replaced = harness({ sessionId: "flow-old", @@ -1313,8 +1083,8 @@ describe("Flow auto-drive coordinator", () => { revision: 2, nextAction: "flow_run_start", }); - await replaced.driver.onIdle("host-1"); - expect(replaced.driver.compactionContext("host-1")).toBeNull(); + await replaced.driver.onIdle("session-host1"); + expect(replaced.driver.compactionContext("session-host1")).toBeNull(); }); test("preserves a fast reply only when the pending read finds a checkpoint", async () => { @@ -1342,28 +1112,28 @@ describe("Flow auto-drive coordinator", () => { }, createToken: () => "token", }); - const metadata = await driver.activate("host-1"); + const metadata = await driver.activate("session-host1"); await driver.observeMessage( - "host-1", + "session-host1", DELIVERY, [{ synthetic: true, metadata }], "command-message", ); - const idle = driver.onIdle("host-1"); + const idle = driver.onIdle("session-host1"); const replyDelivery = { agent: "resumed", model: { providerID: "provider", modelID: "new-model" }, }; await driver.observeMessage( - "host-1", + "session-host1", replyDelivery, [{ synthetic: false }], "checkpoint-reply", ); resolvePending?.(projection); await idle; - expect(driver.compactionContext("host-1")).not.toBeNull(); + expect(driver.compactionContext("session-host1")).not.toBeNull(); projection = { sessionId: "flow-1", @@ -1371,8 +1141,8 @@ describe("Flow auto-drive coordinator", () => { revision: 5, nextAction: "flow_run_start", }; - mutate(driver, "host-1", 5, undefined, "checkpoint-reply"); - await driver.onIdle("host-1"); + mutate(driver, "session-host1", 5, undefined, "checkpoint-reply"); + await driver.onIdle("session-host1"); expect(prompts).toEqual([replyDelivery]); }); @@ -1406,25 +1176,25 @@ describe("Flow auto-drive coordinator", () => { return Promise.resolve(); }, }); - const metadata = await driver.activate("host-1"); + const metadata = await driver.activate("session-host1"); await driver.observeMessage( - "host-1", + "session-host1", DELIVERY, [{ synthetic: true, metadata }], "command-message", ); - await driver.onIdle("host-1"); + await driver.onIdle("session-host1"); const replyDelivery: AutoDriveDelivery = { agent: "resumed", model: { providerID: "provider", modelID: "new-model" }, }; const reply = driver.observeMessage( - "host-1", + "session-host1", replyDelivery, [{ synthetic: false }], "checkpoint-reply", ); - const queuedIdle = driver.onIdle("host-1"); + const queuedIdle = driver.onIdle("session-host1"); expect(reads).toBe(3); await queuedIdle; @@ -1438,13 +1208,13 @@ describe("Flow auto-drive coordinator", () => { revision: 5, nextAction: "flow_run_start", }; - mutate(driver, "host-1", 5, undefined, "checkpoint-reply"); - const finalIdle = driver.onIdle("host-1"); + mutate(driver, "session-host1", 5, undefined, "checkpoint-reply"); + const finalIdle = driver.onIdle("session-host1"); resolveQueued?.(projection); await finalIdle; expect(prompts).toEqual([replyDelivery]); - expect(driver.compactionContext("host-1")).not.toBeNull(); + expect(driver.compactionContext("session-host1")).not.toBeNull(); }); test("replays a post-reply idle when an older status read is in flight", async () => { @@ -1466,29 +1236,29 @@ describe("Flow auto-drive coordinator", () => { }, prompt: () => Promise.resolve(), }); - await driver.activate("host-1"); - const olderIdle = driver.onIdle("host-1"); + await driver.activate("session-host1"); + const olderIdle = driver.onIdle("session-host1"); await driver.observeMessage( - "host-1", + "session-host1", DELIVERY, [{ synthetic: false }], "checkpoint-reply", ); - await driver.onIdle("host-1"); + await driver.onIdle("session-host1"); resolvePending?.(checkpoint); await olderIdle; expect(reads).toBe(3); expect(driver.timingSnapshot()?.state).toBe("waiting-for-user"); - expect(driver.compactionContext("host-1")).not.toBeNull(); + expect(driver.compactionContext("session-host1")).not.toBeNull(); await driver.observeMessage( - "host-1", + "session-host1", DELIVERY, [{ synthetic: false }], "second-reply", ); - expect(driver.compactionContext("host-1")).not.toBeNull(); + expect(driver.compactionContext("session-host1")).not.toBeNull(); }); test("preserves mutation authority through a stale pending checkpoint read", async () => { @@ -1517,21 +1287,21 @@ describe("Flow auto-drive coordinator", () => { }, createToken: () => "token", }); - const metadata = await driver.activate("host-1"); + const metadata = await driver.activate("session-host1"); await driver.observeMessage( - "host-1", + "session-host1", DELIVERY, [{ synthetic: true, metadata }], "command-message", ); - const idle = driver.onIdle("host-1"); + const idle = driver.onIdle("session-host1"); const replyDelivery: AutoDriveDelivery = { agent: "resumed", model: { providerID: "provider", modelID: "new-model" }, }; await driver.observeMessage( - "host-1", + "session-host1", replyDelivery, [{ synthetic: false }], "checkpoint-reply", @@ -1542,15 +1312,15 @@ describe("Flow auto-drive coordinator", () => { revision: 6, nextAction: "flow_run_start", }; - mutate(driver, "host-1", 6, undefined, "checkpoint-reply"); + mutate(driver, "session-host1", 6, undefined, "checkpoint-reply"); resolvePending?.(staleCheckpoint); await idle; expect(prompts).toEqual([]); - expect(driver.compactionContext("host-1")).not.toBeNull(); - await driver.onIdle("host-1"); + expect(driver.compactionContext("session-host1")).not.toBeNull(); + await driver.onIdle("session-host1"); expect(prompts).toEqual([replyDelivery]); - expect(driver.compactionContext("host-1")).not.toBeNull(); + expect(driver.compactionContext("session-host1")).not.toBeNull(); }); test("preserves mutation authority when a pending read sees the mechanical result", async () => { @@ -1576,17 +1346,17 @@ describe("Flow auto-drive coordinator", () => { return Promise.resolve(); }, }); - const metadata = await driver.activate("host-1"); + const metadata = await driver.activate("session-host1"); await driver.observeMessage( - "host-1", + "session-host1", DELIVERY, [{ synthetic: true, metadata }], "command-message", ); - const idle = driver.onIdle("host-1"); + const idle = driver.onIdle("session-host1"); await driver.observeMessage( - "host-1", + "session-host1", DELIVERY, [{ synthetic: false }], "checkpoint-reply", @@ -1597,14 +1367,14 @@ describe("Flow auto-drive coordinator", () => { revision: 5, nextAction: "flow_run_start", }; - mutate(driver, "host-1", 5, undefined, "checkpoint-reply"); - const finalIdle = driver.onIdle("host-1"); + mutate(driver, "session-host1", 5, undefined, "checkpoint-reply"); + const finalIdle = driver.onIdle("session-host1"); await finalIdle; resolvePending?.(projection); await idle; expect(prompts).toEqual([DELIVERY]); - expect(driver.compactionContext("host-1")).not.toBeNull(); + expect(driver.compactionContext("session-host1")).not.toBeNull(); }); test("re-arms a newer checkpoint without stale mutation authority", async () => { @@ -1630,21 +1400,21 @@ describe("Flow auto-drive coordinator", () => { return Promise.resolve(); }, }); - const metadata = await driver.activate("host-1"); + const metadata = await driver.activate("session-host1"); await driver.observeMessage( - "host-1", + "session-host1", DELIVERY, [{ synthetic: true, metadata }], "command-message", ); - const idle = driver.onIdle("host-1"); + const idle = driver.onIdle("session-host1"); await driver.observeMessage( - "host-1", + "session-host1", DELIVERY, [{ synthetic: false }], "checkpoint-reply", ); - mutate(driver, "host-1", 5, undefined, "checkpoint-reply"); + mutate(driver, "session-host1", 5, undefined, "checkpoint-reply"); resolvePending?.({ ...projection, revision: 6, @@ -1659,10 +1429,10 @@ describe("Flow auto-drive coordinator", () => { revision: 7, nextAction: "flow_run_start", }; - await driver.onIdle("host-1"); + await driver.onIdle("session-host1"); expect(prompts).toHaveLength(0); - expect(driver.compactionContext("host-1")).toBeNull(); + expect(driver.compactionContext("session-host1")).toBeNull(); }); test("does not reuse an old baseline checkpoint for later interruptions", async () => { @@ -1688,16 +1458,16 @@ describe("Flow auto-drive coordinator", () => { return Promise.resolve(); }, }); - const metadata = await driver.activate("host-1"); + const metadata = await driver.activate("session-host1"); await driver.observeMessage( - "host-1", + "session-host1", DELIVERY, [{ synthetic: true, metadata }], "command-message", ); - await driver.onIdle("host-1"); + await driver.onIdle("session-host1"); await driver.observeMessage( - "host-1", + "session-host1", DELIVERY, [{ synthetic: false }], "checkpoint-reply", @@ -1708,25 +1478,25 @@ describe("Flow auto-drive coordinator", () => { revision: 5, nextAction: "flow_run_start", }; - mutate(driver, "host-1", 5, undefined, "checkpoint-reply"); - await driver.onIdle("host-1"); + mutate(driver, "session-host1", 5, undefined, "checkpoint-reply"); + await driver.onIdle("session-host1"); expect(prompts).toHaveLength(1); - const laterIdle = driver.onIdle("host-1"); + const laterIdle = driver.onIdle("session-host1"); await driver.observeMessage( - "host-1", + "session-host1", DELIVERY, [{ text: "Change direction." }], "interrupting-message", ); projection = { ...projection, revision: 6 }; - mutate(driver, "host-1", 6, undefined, "interrupting-message"); + mutate(driver, "session-host1", 6, undefined, "interrupting-message"); resolvePending?.(projection); await laterIdle; - await driver.onIdle("host-1"); + await driver.onIdle("session-host1"); expect(prompts).toHaveLength(1); - expect(driver.compactionContext("host-1")).toBeNull(); + expect(driver.compactionContext("session-host1")).toBeNull(); }); test("a second user message interrupts a post-checkpoint status read", async () => { @@ -1753,16 +1523,16 @@ describe("Flow auto-drive coordinator", () => { return Promise.resolve(); }, }); - const metadata = await driver.activate("host-1"); + const metadata = await driver.activate("session-host1"); await driver.observeMessage( - "host-1", + "session-host1", DELIVERY, [{ synthetic: true, metadata }], "command-message", ); - await driver.onIdle("host-1"); + await driver.onIdle("session-host1"); await driver.observeMessage( - "host-1", + "session-host1", DELIVERY, [{ synthetic: false }], "checkpoint-reply", @@ -1774,9 +1544,9 @@ describe("Flow auto-drive coordinator", () => { nextAction: "flow_run_start", }; - const idle = driver.onIdle("host-1"); + const idle = driver.onIdle("session-host1"); await driver.observeMessage( - "host-1", + "session-host1", { agent: "interrupting", model: { providerID: "provider", modelID: "interrupting-model" }, @@ -1788,7 +1558,7 @@ describe("Flow auto-drive coordinator", () => { await idle; expect(prompts).toEqual([]); - expect(driver.compactionContext("host-1")).toBeNull(); + expect(driver.compactionContext("session-host1")).toBeNull(); }); test("a second user message interrupts the checkpoint reply read", async () => { @@ -1810,23 +1580,23 @@ describe("Flow auto-drive coordinator", () => { }, prompt: () => Promise.resolve(), }); - const metadata = await driver.activate("host-1"); + const metadata = await driver.activate("session-host1"); await driver.observeMessage( - "host-1", + "session-host1", DELIVERY, [{ synthetic: true, metadata }], "command-message", ); - await driver.onIdle("host-1"); + await driver.onIdle("session-host1"); const first = driver.observeMessage( - "host-1", + "session-host1", DELIVERY, [{ synthetic: false }], "checkpoint-reply", ); await driver.observeMessage( - "host-1", + "session-host1", DELIVERY, [{ text: "Use a different approach." }], "interrupting-message", @@ -1834,7 +1604,7 @@ describe("Flow auto-drive coordinator", () => { resolveReply?.(projection); await first; - expect(driver.compactionContext("host-1")).toBeNull(); + expect(driver.compactionContext("session-host1")).toBeNull(); }); test("a second message interrupts an already-pending checkpoint reply", async () => { @@ -1861,24 +1631,24 @@ describe("Flow auto-drive coordinator", () => { return Promise.resolve(); }, }); - const metadata = await driver.activate("host-1"); + const metadata = await driver.activate("session-host1"); await driver.observeMessage( - "host-1", + "session-host1", DELIVERY, [{ synthetic: true, metadata }], "command-message", ); - await driver.onIdle("host-1"); + await driver.onIdle("session-host1"); - const idle = driver.onIdle("host-1"); + const idle = driver.onIdle("session-host1"); await driver.observeMessage( - "host-1", + "session-host1", DELIVERY, [{ synthetic: false }], "checkpoint-reply", ); await driver.observeMessage( - "host-1", + "session-host1", { agent: "interrupting", model: { providerID: "provider", modelID: "interrupting-model" }, @@ -1896,7 +1666,7 @@ describe("Flow auto-drive coordinator", () => { await idle; expect(prompts).toEqual([]); - expect(driver.compactionContext("host-1")).toBeNull(); + expect(driver.compactionContext("session-host1")).toBeNull(); }); test("explicit flow-auto stop and cancel deactivate at the same revision", async () => { @@ -1914,16 +1684,16 @@ describe("Flow auto-drive coordinator", () => { nextAction: "flow_plan_approve", }); const metadata = await state.activate(); - await state.driver.onIdle("host-1"); + await state.driver.onIdle("session-host1"); await state.driver.observeMessage( - "host-1", + "session-host1", DELIVERY, [{ text }, { synthetic: true, metadata }], "stop-message", ); expect(state.prompts).toEqual([]); - expect(state.driver.compactionContext("host-1")).toBeNull(); + expect(state.driver.compactionContext("session-host1")).toBeNull(); } const qualified = harness({ @@ -1933,9 +1703,9 @@ describe("Flow auto-drive coordinator", () => { nextAction: "flow_plan_approve", }); await qualified.activate(); - await qualified.driver.onIdle("host-1"); + await qualified.driver.onIdle("session-host1"); await qualified.driver.observeMessage( - "host-1", + "session-host1", DELIVERY, [ { text: "Stop /flow-auto" }, @@ -1943,7 +1713,7 @@ describe("Flow auto-drive coordinator", () => { ], "qualified-stop-message", ); - expect(qualified.driver.compactionContext("host-1")).not.toBeNull(); + expect(qualified.driver.compactionContext("session-host1")).not.toBeNull(); }); test("a real user message interrupts, including during a status read", async () => { @@ -1972,9 +1742,9 @@ describe("Flow auto-drive coordinator", () => { }, createToken: () => "token", }); - await driver.activate("host-1"); + await driver.activate("session-host1"); await driver.observeMessage( - "host-1", + "session-host1", DELIVERY, [ { @@ -1985,9 +1755,9 @@ describe("Flow auto-drive coordinator", () => { "command-message", ); - const idle = driver.onIdle("host-1"); + const idle = driver.onIdle("session-host1"); await driver.observeMessage( - "host-1", + "session-host1", DELIVERY, [{ synthetic: false }], "interrupting-message", @@ -2001,7 +1771,7 @@ describe("Flow auto-drive coordinator", () => { await idle; expect(prompts).toHaveLength(0); - expect(driver.compactionContext("host-1")).toBeNull(); + expect(driver.compactionContext("session-host1")).toBeNull(); }); test("a user interruption during prompt enqueue cannot answer a future checkpoint", async () => { @@ -2027,9 +1797,9 @@ describe("Flow auto-drive coordinator", () => { }); }, }); - const metadata = await driver.activate("host-1"); + const metadata = await driver.activate("session-host1"); await driver.observeMessage( - "host-1", + "session-host1", DELIVERY, [{ synthetic: true, metadata }], "command-message", @@ -2040,11 +1810,11 @@ describe("Flow auto-drive coordinator", () => { revision: 2, nextAction: "flow_run_start", }; - mutate(driver, "host-1", 2); - const idle = driver.onIdle("host-1"); + mutate(driver, "session-host1", 2); + const idle = driver.onIdle("session-host1"); await promptStarted; await driver.observeMessage( - "host-1", + "session-host1", DELIVERY, [{ synthetic: false }], "interrupting-message", @@ -2057,10 +1827,10 @@ describe("Flow auto-drive coordinator", () => { }; resolvePrompt?.(); await idle; - await driver.onIdle("host-1"); + await driver.onIdle("session-host1"); expect(promptCount).toBe(1); - expect(driver.compactionContext("host-1")).toBeNull(); + expect(driver.compactionContext("session-host1")).toBeNull(); }); test("an old rejected read cannot clear a newer same-host lease", async () => { @@ -2084,11 +1854,11 @@ describe("Flow auto-drive coordinator", () => { prompt: () => Promise.resolve(), createToken: () => `token-${reads}`, }); - await driver.activate("host-1"); - const oldIdle = driver.onIdle("host-1"); - const metadata = await driver.activate("host-1"); + await driver.activate("session-host1"); + const oldIdle = driver.onIdle("session-host1"); + const metadata = await driver.activate("session-host1"); await driver.observeMessage( - "host-1", + "session-host1", DELIVERY, [{ synthetic: true, metadata }], "replacement-command", @@ -2096,7 +1866,7 @@ describe("Flow auto-drive coordinator", () => { rejectOld?.(new Error("old status failure")); await oldIdle; - expect(driver.compactionContext("host-1")).not.toBeNull(); + expect(driver.compactionContext("session-host1")).not.toBeNull(); }); test("overlapping baseline reads cannot revive an older activation", async () => { @@ -2118,10 +1888,10 @@ describe("Flow auto-drive coordinator", () => { }, prompt: () => Promise.resolve(), }); - const first = driver.activate("host-1"); - const second = await driver.activate("host-1"); + const first = driver.activate("session-host1"); + const second = await driver.activate("session-host1"); await driver.observeMessage( - "host-1", + "session-host1", DELIVERY, [{ synthetic: true, metadata: second }], "second-command", @@ -2134,7 +1904,7 @@ describe("Flow auto-drive coordinator", () => { }); await expect(first).rejects.toThrow("superseded"); - expect(driver.compactionContext("host-1")).not.toBeNull(); + expect(driver.compactionContext("session-host1")).not.toBeNull(); }); test("rejects stale internal messages and preserves a live lease through compaction", async () => { @@ -2144,21 +1914,21 @@ describe("Flow auto-drive coordinator", () => { nextAction: "flow_run_start", }); await state.activate(); - expect(state.driver.compactionContext("host-1")).toContain( + expect(state.driver.compactionContext("session-host1")).toContain( "/flow-auto continuation", ); - expect(state.driver.compactionContext("host-1")).toContain( + expect(state.driver.compactionContext("session-host1")).toContain( FLOW_MANAGER_KERNEL, ); - expect(state.driver.compactionContext("host-1")).toContain( + expect(state.driver.compactionContext("session-host1")).toContain( "Load flow-run guidance before any feature or closure route", ); - expect(state.driver.compactionContext("host-1")).toContain( + expect(state.driver.compactionContext("session-host1")).toContain( "replay archiveRetry exactly", ); expect( await state.driver.observeMessage( - "host-1", + "session-host1", DELIVERY, [ { @@ -2169,7 +1939,7 @@ describe("Flow auto-drive coordinator", () => { "stale-continuation", ), ).toBe("stale-continuation"); - expect(state.driver.compactionContext("host-1")).not.toBeNull(); + expect(state.driver.compactionContext("session-host1")).not.toBeNull(); }); test("fails closed on replacement state and status or prompt errors", async () => { @@ -2186,15 +1956,15 @@ describe("Flow auto-drive coordinator", () => { revision: 1, nextAction: "flow_run_start", }); - await changed.driver.onIdle("host-1"); + await changed.driver.onIdle("session-host1"); changed.setProjection({ sessionId: "flow-2", status: "ready", revision: 2, nextAction: "flow_run_start", }); - await changed.driver.onIdle("host-1"); - expect(changed.driver.compactionContext("host-1")).toBeNull(); + await changed.driver.onIdle("session-host1"); + expect(changed.driver.compactionContext("session-host1")).toBeNull(); for (const failure of ["status", "prompt"] as const) { let reads = 0; @@ -2211,9 +1981,9 @@ describe("Flow auto-drive coordinator", () => { prompt: () => Promise.reject(new Error("unavailable")), createToken: () => failure, }); - await driver.activate("host-1"); + await driver.activate("session-host1"); await driver.observeMessage( - "host-1", + "session-host1", DELIVERY, [ { @@ -2223,8 +1993,8 @@ describe("Flow auto-drive coordinator", () => { ], `${failure}-command`, ); - await driver.onIdle("host-1"); - expect(driver.compactionContext("host-1")).toBeNull(); + await driver.onIdle("session-host1"); + expect(driver.compactionContext("session-host1")).toBeNull(); } }); @@ -2242,8 +2012,8 @@ describe("Flow auto-drive coordinator", () => { revision: 5, nextAction: "await-user-direction", }); - expect(await replaced.driver.resumeForCommand("host-1")).toBeNull(); - expect(replaced.driver.compactionContext("host-1")).toBeNull(); + expect(await replaced.driver.resumeForCommand("session-host1")).toBeNull(); + expect(replaced.driver.compactionContext("session-host1")).toBeNull(); const interrupted = harness({ sessionId: "flow-1", @@ -2253,19 +2023,21 @@ describe("Flow auto-drive coordinator", () => { }); await interrupted.activate(); await interrupted.driver.observeMessage( - "host-1", + "session-host1", DELIVERY, [{ text: "Answer the blocker" }], "first-reply", ); await interrupted.driver.observeMessage( - "host-1", + "session-host1", DELIVERY, [{ text: "Start a different task" }], "unrelated-turn", ); - expect(await interrupted.driver.resumeForCommand("host-1")).toBeNull(); - expect(interrupted.driver.compactionContext("host-1")).toBeNull(); + expect( + await interrupted.driver.resumeForCommand("session-host1"), + ).toBeNull(); + expect(interrupted.driver.compactionContext("session-host1")).toBeNull(); const dropped = harness({ sessionId: "flow-1", @@ -2274,14 +2046,16 @@ describe("Flow auto-drive coordinator", () => { nextAction: "flow_run_start", }); await dropped.activate(); - expect(await dropped.driver.resumeForCommand("host-1")).not.toBeNull(); + expect( + await dropped.driver.resumeForCommand("session-host1"), + ).not.toBeNull(); await dropped.driver.observeMessage( - "host-1", + "session-host1", DELIVERY, [{ text: "Unrelated task after the command was dropped" }], "unrelated-after-drop", ); - expect(dropped.driver.compactionContext("host-1")).toBeNull(); + expect(dropped.driver.compactionContext("session-host1")).toBeNull(); }); }); @@ -2302,12 +2076,9 @@ describe("authenticated validation continuation", () => { runId?: string; }> = {}, ) { - driver.observeHostMessage("host-1", { - id: "validation-manager", - role: "assistant", - parentID: parent, - }); - const value = driver.validationOrigin("host-1", "validation-manager", { + const assistant = `msg_validation${++assistantSequence}`; + driver.observeAssistant("session-host1", assistant, parent); + const value = driver.validationOrigin("session-host1", assistant, { featureId: "feature", runId: declaration.runId ?? "run-1", sourceDigest: VALIDATION_SOURCE, @@ -2329,8 +2100,8 @@ describe("authenticated validation continuation", () => { }); await state.activate(); state.setProjection(running); - mutate(state.driver, "host-1", 3); - await state.driver.onIdle("host-1"); + mutate(state.driver, "session-host1", 3); + await state.driver.onIdle("session-host1"); expect(state.prompts).toHaveLength(1); for (const [index, command, scope, nextAction] of [ [0, "bun test focused", "focused", "flow_validation_start"], @@ -2338,21 +2109,26 @@ describe("authenticated validation continuation", () => { ] as const) { const messageId = `validation-turn-${index}`; await state.driver.observeMessage( - "host-1", + "session-host1", DELIVERY, [{ synthetic: true, metadata: state.prompts[index]?.metadata ?? {} }], messageId, ); const capture = new ValidationCaptureCoordinator({ + readSession: readValidationSession, persistObservation: async (_workspace, input) => { state.setProjection({ ...running, revision: 4 + index, nextAction }); return observation(input, 4 + index); }, onRecorded: (receipt) => state.driver.observeValidation(receipt), }); + const validationOrigin = origin(state.driver, messageId, { + command, + scope, + }); capture.arm( - "host-1", - "/workspace", + "session-host1", + WORKSPACE, { featureId: "feature", runId: "run-1", @@ -2364,58 +2140,56 @@ describe("authenticated validation continuation", () => { assertions: [], resultsPath: undefined, }, - origin(state.driver, messageId, { command, scope }), + validationOrigin, ); - await capture.observeToolBefore( - { tool: "bash", sessionID: "host-1", callID: command }, - { args: { command } }, + const execution = shellExecution( + command, + command, + validationOrigin.assistantId, ); - await capture.observeToolAfter( - { - tool: "bash", - sessionID: "host-1", - callID: command, - args: { command }, - }, - { - title: "validation", - output: "passed", + await capture.observeToolBefore(execution); + await capture.observeToolAfter({ + ...execution, + status: "completed", + result: { + content: [{ type: "text", text: "passed" }], + output: { exit: 0, truncated: false }, metadata: { exit: 0, truncated: false }, }, - ); - await state.driver.onIdle("host-1"); + }); + await state.driver.onIdle("session-host1"); expect(state.prompts).toHaveLength(index + 2); } await state.driver.observeMessage( - "host-1", + "session-host1", DELIVERY, [{ synthetic: true, metadata: state.prompts[2]?.metadata ?? {} }], "review-turn", ); - mutate(state.driver, "host-1", 6, undefined, "review-turn", true); + mutate(state.driver, "session-host1", 6, undefined, "review-turn", true); state.setProjection({ ...running, revision: 7, status: "completed", nextAction: "flow_session_close", }); - await state.driver.onIdle("host-1"); + await state.driver.onIdle("session-host1"); expect(state.prompts).toHaveLength(4); await state.driver.observeMessage( - "host-1", + "session-host1", DELIVERY, [{ synthetic: true, metadata: state.prompts[3]?.metadata ?? {} }], "close-turn", ); - mutate(state.driver, "host-1", 8, undefined, "close-turn"); + mutate(state.driver, "session-host1", 8, undefined, "close-turn"); state.setProjection({ status: "idle", revision: 0, nextAction: "flow_plan_save", }); - await state.driver.onIdle("host-1"); + await state.driver.onIdle("session-host1"); expect(state.prompts).toHaveLength(4); - expect(state.driver.continuationSnapshot("host-1")).toMatchObject({ + expect(state.driver.continuationSnapshot("session-host1")).toMatchObject({ state: "inactive", reason: "completed", }); @@ -2449,16 +2223,18 @@ describe("authenticated validation continuation", () => { }, }); state.setProjection({ ...running, revision: 4, nextAction }); - await state.driver.onIdle("host-1"); + await state.driver.onIdle("session-host1"); expect(state.prompts).toHaveLength( nextAction === "flow_review_start" ? 1 : 0, ); if (nextAction === "flow_validation_start") { - expect(state.driver.continuationSnapshot("host-1")).toMatchObject({ + expect( + state.driver.continuationSnapshot("session-host1"), + ).toMatchObject({ state: "paused", reason: "validation-failed", }); - await state.driver.onIdle("host-1"); + await state.driver.onIdle("session-host1"); expect(state.prompts).toHaveLength(0); } } @@ -2489,9 +2265,9 @@ describe("authenticated validation continuation", () => { }, }); state.setProjection({ ...running, revision: 4 }); - await state.driver.onIdle("host-1"); + await state.driver.onIdle("session-host1"); expect(state.prompts).toHaveLength(1); - await state.driver.onIdle("host-1"); + await state.driver.onIdle("session-host1"); expect(state.prompts).toHaveLength(1); }); @@ -2524,7 +2300,7 @@ describe("authenticated validation continuation", () => { observation: { ...observation, ...tamper }, }); state.setProjection({ ...running, revision: 4 }); - await state.driver.onIdle("host-1"); + await state.driver.onIdle("session-host1"); expect(state.prompts).toHaveLength(0); } }); @@ -2574,54 +2350,54 @@ describe("authenticated validation continuation", () => { }); failed("gate-1", 4, "command-message"); state.setProjection(projection("gate-1", 4)); - await state.driver.onIdle("host-1"); + await state.driver.onIdle("session-host1"); expect(state.prompts).toHaveLength(1); expect(state.prompts[0]?.text).toContain("flow_plan_amend"); expect(state.prompts[0]?.text).toContain( "Preserve test discovery, assertions, and acceptance criteria; never exclude or skip existing tests.", ); await state.driver.observeMessage( - "host-1", + "session-host1", DELIVERY, [{ synthetic: true, metadata: state.prompts[0]?.metadata ?? {} }], "repair-turn", ); failed("gate-2", 5, "repair-turn"); state.setProjection(projection("gate-2", 5)); - await state.driver.onIdle("host-1"); + await state.driver.onIdle("session-host1"); expect(state.prompts).toHaveLength(1); await state.activate(); - mutate(state.driver, "host-1", 6); + mutate(state.driver, "session-host1", 6); failed("reset-gate", 7, "command-message", "run-2"); state.setProjection(projection("reset-gate", 7, "initial", "run-2")); - await state.driver.onIdle("host-1"); + await state.driver.onIdle("session-host1"); expect(state.prompts).toHaveLength(1); - mutate(state.driver, "host-1", 8); + mutate(state.driver, "session-host1", 8); failed("gate-3", 9, "command-message", "run-2"); state.setProjection( projection("gate-3", 9, "accepted-amendment-1", "run-2"), ); - await state.driver.onIdle("host-1"); + await state.driver.onIdle("session-host1"); expect(state.prompts).toHaveLength(2); for (let index = 2; index <= 5; index++) { const parent = `repair-turn-${index}`; await state.driver.observeMessage( - "host-1", + "session-host1", DELIVERY, [{ synthetic: true, metadata: state.prompts.at(-1)?.metadata ?? {} }], parent, ); const revision = 8 + index * 2; - mutate(state.driver, "host-1", revision, undefined, parent); + mutate(state.driver, "session-host1", revision, undefined, parent); const id = `gate-after-amendment-${index}`; failed(id, revision + 1, parent, "run-2"); state.setProjection( projection(id, revision + 1, `accepted-amendment-${index}`, "run-2"), ); - await state.driver.onIdle("host-1"); + await state.driver.onIdle("session-host1"); expect(state.prompts).toHaveLength(index + 1); } - await state.driver.onIdle("host-1"); + await state.driver.onIdle("session-host1"); expect(state.prompts).toHaveLength(6); }); @@ -2636,13 +2412,18 @@ describe("authenticated validation continuation", () => { const state = harness(running); await state.activate(); const binding = origin(state.driver); - if (change === "stop") state.driver.deactivate("host-1"); + if (change === "stop") state.driver.deactivate("session-host1"); if (change === "compaction") - compact(state.driver, "host-1", "command-message", "compacted-user"); + compact( + state.driver, + "session-host1", + "command-message", + "compacted-user", + ); if (change === "new-invocation") await state.activate(); if (change === "user-interruption") { await state.driver.observeMessage( - "host-1", + "session-host1", DELIVERY, [{ text: "Pause and answer this question" }], "new-user-message", @@ -2673,20 +2454,16 @@ describe("authenticated validation continuation", () => { ? { sessionId: "replacement" } : {}), }); - await state.driver.onIdle("host-1"); + await state.driver.onIdle("session-host1"); expect(state.prompts).toHaveLength(0); } }); test("rejects wrong-parent origins and duplicate or source-stale receipts", async () => { const state = harness(running); await state.activate(); - state.driver.observeHostMessage("host-1", { - id: "old-assistant", - role: "assistant", - parentID: "old-user", - }); + state.driver.observeAssistant("session-host1", "old-assistant", "old-user"); expect( - state.driver.validationOrigin("host-1", "old-assistant", { + state.driver.validationOrigin("session-host1", "old-assistant", { featureId: "feature", runId: "run-1", sourceDigest: VALIDATION_SOURCE, @@ -2718,10 +2495,10 @@ describe("authenticated validation continuation", () => { }; state.driver.observeValidation(receipt); state.setProjection({ ...running, revision: 4 }); - await state.driver.onIdle("host-1"); + await state.driver.onIdle("session-host1"); expect(state.prompts).toHaveLength(1); state.driver.observeValidation(receipt); - await state.driver.onIdle("host-1"); + await state.driver.onIdle("session-host1"); expect(state.prompts).toHaveLength(1); const stale = harness(running); await stale.activate(); @@ -2735,9 +2512,9 @@ describe("authenticated validation continuation", () => { revision: 4, nextAction: "flow_review_start", }); - await stale.driver.onIdle("host-1"); + await stale.driver.onIdle("session-host1"); expect(stale.prompts).toHaveLength(0); - expect(stale.driver.continuationSnapshot("host-1")).toMatchObject({ + expect(stale.driver.continuationSnapshot("session-host1")).toMatchObject({ state: "paused", reason: "validation-ineligible", }); @@ -2770,9 +2547,9 @@ describe("authenticated validation continuation", () => { nextAction: "flow_review_start", }); state.setSource(`sha256:${"b".repeat(64)}`); - await state.driver.onIdle("host-1"); + await state.driver.onIdle("session-host1"); expect(state.prompts).toHaveLength(0); - expect(state.driver.continuationSnapshot("host-1")).toMatchObject({ + expect(state.driver.continuationSnapshot("session-host1")).toMatchObject({ state: "paused", reason: "validation-ineligible", }); @@ -2787,13 +2564,13 @@ describe("authenticated validation continuation", () => { ] as const) { const state = harness(running); await state.activate(); - state.driver.observeHostMessage("host-1", { - id: "assertion-manager", - role: "assistant", - parentID: "command-message", - }); + state.driver.observeAssistant( + "session-host1", + "assertion-manager", + "command-message", + ); const binding = state.driver.validationOrigin( - "host-1", + "session-host1", "assertion-manager", { featureId: "feature", @@ -2829,10 +2606,12 @@ describe("authenticated validation continuation", () => { }, }); state.setProjection({ ...running, revision: 4 }); - await state.driver.onIdle("host-1"); + await state.driver.onIdle("session-host1"); expect(state.prompts).toHaveLength(status === "passed" ? 1 : 0); if (status !== "passed") - expect(state.driver.continuationSnapshot("host-1")).toMatchObject({ + expect( + state.driver.continuationSnapshot("session-host1"), + ).toMatchObject({ state: "paused", reason: "validation-ineligible", }); @@ -2850,13 +2629,13 @@ describe("authenticated validation continuation", () => { ] as const) { const state = harness(running); await state.activate(); - state.driver.observeHostMessage("host-1", { - id: "named-gate-manager", - role: "assistant", - parentID: "command-message", - }); + state.driver.observeAssistant( + "session-host1", + "named-gate-manager", + "command-message", + ); const binding = state.driver.validationOrigin( - "host-1", + "session-host1", "named-gate-manager", { featureId: "feature", @@ -2909,11 +2688,11 @@ describe("authenticated validation continuation", () => { }, }); if (gap === "live-source") state.setSource(`sha256:${"b".repeat(64)}`); - await state.driver.onIdle("host-1"); + await state.driver.onIdle("session-host1"); expect(state.prompts).toHaveLength(gap === null ? 1 : 0); if (gap === null) expect(state.prompts[0]?.text).toContain("flow_plan_amend"); - await state.driver.onIdle("host-1"); + await state.driver.onIdle("session-host1"); expect(state.prompts).toHaveLength(gap === null ? 1 : 0); } }); @@ -2921,17 +2700,24 @@ describe("authenticated validation continuation", () => { const state = harness(running); await state.activate(); let persist!: () => void; + let persisted!: () => void; + const persistenceStarted = new Promise((resolve) => { + persisted = resolve; + }); const capture = new ValidationCaptureCoordinator({ + readSession: readValidationSession, persistObservation: (_workspace, input) => new Promise((resolve) => { persist = () => resolve(observation(input, 4)); + persisted(); }), onRecorded: (receipt) => state.driver.observeValidation(receipt), }); const command = "bun test"; + const validationOrigin = origin(state.driver); capture.arm( - "host-1", - "/workspace", + "session-host1", + WORKSPACE, { featureId: "feature", runId: "run-1", @@ -2943,29 +2729,31 @@ describe("authenticated validation continuation", () => { assertions: [], resultsPath: undefined, }, - origin(state.driver), - ); - await capture.observeToolBefore( - { tool: "bash", sessionID: "host-1", callID: "check" }, - { args: { command } }, + validationOrigin, ); - const output = { - title: "check", - output: "passed", - metadata: { exit: 0, truncated: false }, - }; - const completing = capture.observeToolAfter( - { tool: "bash", sessionID: "host-1", callID: "check", args: { command } }, - output, + const execution = shellExecution( + command, + "check", + validationOrigin.assistantId, ); - await Promise.resolve(); - state.driver.deactivate("host-1", "cancelled"); + await capture.observeToolBefore(execution); + const completing = capture.observeToolAfter({ + ...execution, + status: "completed", + result: { + content: [{ type: "text", text: "passed" }], + output: { exit: 0, truncated: false }, + metadata: { exit: 0, truncated: false }, + }, + }); + await persistenceStarted; + state.driver.deactivate("session-host1", "cancelled"); persist(); expect((await completing)?.recordedRevision).toBe(4); state.setProjection({ ...running, revision: 4 }); - await state.driver.onIdle("host-1"); + await state.driver.onIdle("session-host1"); expect(state.prompts).toHaveLength(0); - expect(state.driver.continuationSnapshot("host-1")).toMatchObject({ + expect(state.driver.continuationSnapshot("session-host1")).toMatchObject({ state: "inactive", reason: "cancelled", }); @@ -2986,24 +2774,32 @@ describe("own inspection draft approval", () => { nextAction: "flow_plan_save", }); await state.activate( - "host-1", + "session-host1", DELIVERY, "command-message", "Review codebase", { newlyAnchoredSessionId: anchored, intent: "inspection-deliverable" }, ); - mutate(state.driver, "host-1", 1, "flow-1", "command-message", false, { - tool: "flow_plan_save", - goal: "Review codebase", - features: [{ kind: "inspect" }], - }); + mutate( + state.driver, + "session-host1", + 1, + "flow-1", + "command-message", + false, + { + tool: "flow_plan_save", + goal: "Review codebase", + features: [{ kind: "inspect" }], + }, + ); state.setProjection({ sessionId: "flow-1", status: "planning", revision: 1, nextAction: "flow_plan_approve", }); - await state.driver.onIdle("host-1"); + await state.driver.onIdle("session-host1"); expect( state.prompts.some((prompt) => prompt.text.includes("Approve this one draft"), @@ -3019,24 +2815,32 @@ describe("own inspection draft approval", () => { nextAction: "flow_plan_save", }); await state.activate( - "host-1", + "session-host1", DELIVERY, "command-message", "Just draft a Flow plan for inspection", options, ); - mutate(state.driver, "host-1", 1, "flow-1", "command-message", false, { - tool: "flow_plan_save", - goal: "Just draft a Flow plan for inspection", - features: [{ kind: "inspect" }], - }); + mutate( + state.driver, + "session-host1", + 1, + "flow-1", + "command-message", + false, + { + tool: "flow_plan_save", + goal: "Just draft a Flow plan for inspection", + features: [{ kind: "inspect" }], + }, + ); state.setProjection({ sessionId: "flow-1", status: "planning", revision: 1, nextAction: "flow_plan_approve", }); - await state.driver.onIdle("host-1"); + await state.driver.onIdle("session-host1"); expect( state.prompts.some((prompt) => prompt.text.includes("Approve this one draft"), @@ -3051,43 +2855,51 @@ describe("own inspection draft approval", () => { nextAction: "flow_plan_save", }); await state.activate( - "host-1", + "session-host1", DELIVERY, "command-message", "Review codebase", { intent: "inspection-deliverable" }, ); - mutate(state.driver, "host-1", 1, "flow-1", "command-message", false, { - tool: "flow_plan_save", - goal: "Review codebase", - features: [{ kind: "inspect" }], - }); + mutate( + state.driver, + "session-host1", + 1, + "flow-1", + "command-message", + false, + { + tool: "flow_plan_save", + goal: "Review codebase", + features: [{ kind: "inspect" }], + }, + ); state.setProjection({ sessionId: "flow-1", status: "planning", revision: 1, nextAction: "flow_plan_approve", }); - await state.driver.onIdle("host-1"); + await state.driver.onIdle("session-host1"); expect(state.prompts.at(-1)?.text).toContain("Approve this one draft"); - await state.driver.onIdle("host-1"); + await state.driver.onIdle("session-host1"); expect(state.prompts).toHaveLength(1); const metadata = state.prompts[0]?.metadata; if (!metadata) throw new Error("Expected approval prompt."); await state.driver.observeMessage( - "host-1", + "session-host1", DELIVERY, [{ synthetic: true, metadata }], "approval-message", ); - mutate(state.driver, "host-1", 2, undefined, "approval-message"); + mutate(state.driver, "session-host1", 2, undefined, "approval-message"); state.setProjection({ sessionId: "flow-1", status: "ready", revision: 2, nextAction: "flow_run_start", }); - await state.driver.onIdle("host-1"); + await state.driver.onIdle("session-host1"); expect(state.prompts.at(-1)?.text).toContain( "Continue the same user-authorized /flow-auto lifecycle", ); @@ -3111,28 +2923,36 @@ describe("own inspection draft approval", () => { : { status: "idle", revision: 0, nextAction: "flow_plan_save" }, ); await state.activate( - "host-1", + "session-host1", DELIVERY, "command-message", "Review codebase", { intent: "inspection-deliverable" }, ); if (!existing) - mutate(state.driver, "host-1", 1, "flow-1", "command-message", false, { - tool: "flow_plan_save", - goal: - variant === "paraphrase" - ? "Review the codebase" - : "Review codebase", - features: [{ kind: variant === "change" ? "change" : "inspect" }], - }); + mutate( + state.driver, + "session-host1", + 1, + "flow-1", + "command-message", + false, + { + tool: "flow_plan_save", + goal: + variant === "paraphrase" + ? "Review the codebase" + : "Review codebase", + features: [{ kind: variant === "change" ? "change" : "inspect" }], + }, + ); state.setProjection({ sessionId: "flow-1", status: "planning", revision: variant === "revised" ? 2 : 1, nextAction: "flow_plan_approve", }); - await state.driver.onIdle("host-1"); + await state.driver.onIdle("session-host1"); expect( state.prompts.some((prompt) => prompt.text.includes("Approve this one draft"), @@ -3147,17 +2967,25 @@ describe("own inspection draft approval", () => { nextAction: "flow_plan_save", }); await state.activate( - "host-1", + "session-host1", DELIVERY, "command-message", "Review codebase", { intent: "inspection-deliverable" }, ); - mutate(state.driver, "host-1", 1, "flow-1", "command-message", false, { - tool: "flow_plan_save", - goal: "Review codebase", - features: [{ kind: "inspect" }], - }); + mutate( + state.driver, + "session-host1", + 1, + "flow-1", + "command-message", + false, + { + tool: "flow_plan_save", + goal: "Review codebase", + features: [{ kind: "inspect" }], + }, + ); state.setProjection({ sessionId: "flow-1", status: "planning", @@ -3165,12 +2993,12 @@ describe("own inspection draft approval", () => { nextAction: "flow_plan_approve", }); await state.driver.observeMessage( - "host-1", + "session-host1", DELIVERY, [{ text: "Change the plan." }], "interruption", ); - await state.driver.onIdle("host-1"); + await state.driver.onIdle("session-host1"); expect( state.prompts.some((prompt) => prompt.text.includes("Approve this one draft"), @@ -3184,36 +3012,44 @@ describe("own inspection draft approval", () => { nextAction: "flow_plan_save", }); await state.activate( - "host-1", + "session-host1", DELIVERY, "command-message", "Review codebase", { intent: "inspection-deliverable" }, ); - mutate(state.driver, "host-1", 1, "flow-1", "command-message", false, { - tool: "flow_plan_save", - goal: "Review codebase", - features: [{ kind: "inspect" }], - }); + mutate( + state.driver, + "session-host1", + 1, + "flow-1", + "command-message", + false, + { + tool: "flow_plan_save", + goal: "Review codebase", + features: [{ kind: "inspect" }], + }, + ); state.setProjection({ sessionId: "flow-1", status: "planning", revision: 1, nextAction: "flow_plan_approve", }); - await state.driver.onIdle("host-1"); + await state.driver.onIdle("session-host1"); const metadata = state.prompts.at(-1)?.metadata; if (!metadata) throw new Error("Expected approval prompt."); await state.driver.observeMessage( - "host-1", + "session-host1", DELIVERY, [{ text: "Change the plan." }], "user-interruption", ); - expect(state.driver.compactionContext("host-1")).toBeNull(); + expect(state.driver.compactionContext("session-host1")).toBeNull(); expect( await state.driver.observeMessage( - "host-1", + "session-host1", DELIVERY, [{ synthetic: true, metadata }], "late-approval", @@ -3221,156 +3057,3 @@ describe("own inspection draft approval", () => { ).toBe("stale-continuation"); }); }); - -test("native historical user summary updates cannot replace the compaction successor", async () => { - const state = harness({ - sessionId: "flow-1", - status: "planning", - revision: 4, - nextAction: "flow_plan_approve", - }); - await state.activate(); - await state.driver.observeMessage( - "host-1", - DELIVERY, - [{ text: "Approve the existing plan." }], - "checkpoint-reply", - ); - state.driver.observeHostMessage("host-1", { - id: "manager-before-compaction", - role: "assistant", - parentID: "checkpoint-reply", - }); - state.driver.observeHostPart("host-1", { - type: "compaction", - messageID: "native-compaction", - auto: true, - }); - const summary = { - id: "native-summary", - role: "assistant", - parentID: "native-compaction", - summary: true, - }; - state.driver.observeHostMessage("host-1", summary); - state.driver.observeHostMessage("host-1", { - id: "command-message", - role: "user", - summary: { diffs: [] }, - }); - state.driver.observeHostMessage("host-1", summary); - state.driver.observeHostMessage("host-1", summary); - state.driver.observeHostMessage("host-1", { - id: "native-successor", - role: "user", - }); - const continuation = { - type: "text", - messageID: "native-successor", - synthetic: true, - metadata: { compaction_continue: true }, - }; - state.driver.observeHostMessage("host-1", { - id: "command-message", - role: "user", - summary: { diffs: [] }, - }); - state.driver.observeHostPart("host-1", continuation); - state.driver.observeHostPart("host-1", continuation); - state.driver.observeHostMessage("host-1", summary); - state.driver.observeHostMessage("host-1", { - id: "command-message", - role: "user", - summary: { diffs: [] }, - }); - state.driver.observeHostPart("host-1", { - type: "compaction", - messageID: "native-compaction", - auto: true, - }); - state.driver.observeCompaction("host-1"); - expect(state.driver.compactionContext("host-1")).not.toBeNull(); - state.setProjection({ - sessionId: "flow-1", - status: "ready", - revision: 5, - nextAction: "flow_run_start", - }); - mutate(state.driver, "host-1", 5, undefined, "native-successor"); - await state.driver.onIdle("host-1"); - expect(state.prompts).toHaveLength(1); -}); - -test.each([ - { label: "missing marker", marker: null }, - { label: "manual marker", marker: { synthetic: false } }, - { label: "unclassified marker", marker: { synthetic: undefined } }, - { label: "unknown native user", marker: { messageID: "unknown-user" } }, - { label: "wrong part type", marker: { type: "tool" } }, - { - label: "false marker", - marker: { metadata: { compaction_continue: false } }, - }, - { - label: "string marker", - marker: { metadata: { compaction_continue: "true" } }, - }, - { label: "wrong host", marker: {}, host: "host-2" }, - { label: "marker before summary", marker: {}, early: true }, - { label: "replaced authority", marker: {}, freshDirection: true }, -])( - "rejects compaction continuation with $label", - async ({ marker, host, early, freshDirection }) => { - const state = harness({ - sessionId: "flow-1", - status: "planning", - revision: 4, - nextAction: "flow_plan_approve", - }); - await state.activate(); - state.driver.observeHostMessage("host-1", { - id: "manager", - role: "assistant", - parentID: "command-message", - }); - state.driver.observeHostPart("host-1", { - type: "compaction", - messageID: "compaction", - auto: true, - }); - const part = { - type: "text", - messageID: "successor", - synthetic: true, - metadata: { compaction_continue: true }, - ...marker, - }; - const { synthetic, ...unclassified } = part; - const nativePart = - synthetic === undefined ? unclassified : { ...unclassified, synthetic }; - if (early) state.driver.observeHostPart("host-1", nativePart); - state.driver.observeHostMessage("host-1", { - id: "summary", - role: "assistant", - parentID: "compaction", - summary: true, - }); - state.driver.observeHostMessage("host-1", { - id: "successor", - role: "user", - }); - if (freshDirection) - await state.driver.observeMessage( - "host-1", - DELIVERY, - [{ text: "Continue from my new direction." }], - "new-user", - ); - if (marker) state.driver.observeHostPart(host ?? "host-1", nativePart); - state.driver.observeCompaction("host-1"); - expect(state.driver.compactionContext("host-1")).toBeNull(); - expect(state.warnings.at(-1)).toContain( - "compaction origin was unavailable", - ); - }, -); diff --git a/tests/canary-run.test.ts b/tests/canary-run.test.ts index a09c70c1..95d3d683 100644 --- a/tests/canary-run.test.ts +++ b/tests/canary-run.test.ts @@ -5,13 +5,16 @@ import { mkdir, mkdtemp, readFile, + realpath, rm, writeFile, } from "node:fs/promises"; import { tmpdir } from "node:os"; import { join } from "node:path"; +import { canonicalJson } from "../evals/canonical-json.js"; import { canaryLaunchEnvironment, + canaryProviderConfiguration, runPaidCanary, } from "../scripts/canary-run.js"; import { @@ -34,23 +37,65 @@ afterEach(async () => { ); }); async function fixture() { - const directory = await mkdtemp(join(tmpdir(), "flow-canary-budget-")); + const directory = await realpath( + await mkdtemp(join(tmpdir(), "flow-canary-budget-")), + ); directories.push(directory); - await mkdir(join(directory, "fixture/.opencode/plugins"), { - recursive: true, - }); - const hash = (text: string) => + const pluginDirectory = join(directory, "fixture/.opencode/plugins/flow"); + await mkdir(pluginDirectory, { recursive: true }); + const hash = (text: string | Uint8Array) => `sha256:${createHash("sha256").update(text).digest("hex")}`; + const sourcePackage = JSON.stringify({ + name: "opencode-plugin-flow", + version: "10.0.0", + dependencies: {}, + devDependencies: { "@opencode/plugin": "2.1.4" }, + }); + const pluginPackage = canonicalJson({ + name: "opencode-plugin-flow", + version: "10.0.0", + type: "module", + exports: { ".": "./index.js", "./tui": "./tui.js" }, + dependencies: { "@opencode/plugin": "2.1.4" }, + }); + const config = canonicalJson({ + plugins: [ + { + package: "./.opencode/plugins/flow", + options: { recovery: { mode: "off" } }, + }, + ], + update: "disable", + share: "disabled", + }); + await mkdir(join(directory, "package/dist"), { recursive: true }); + for (const [path, bytes] of [ + ["package/package.json", sourcePackage], + ["package/dist/index.js", "plugin"], + ["package/dist/tui.js", "tui"], + ["fixture/.opencode/plugins/flow/index.js", "plugin"], + ["fixture/.opencode/plugins/flow/tui.js", "tui"], + ["fixture/.opencode/plugins/flow/package.json", pluginPackage], + ["fixture/opencode.json", config], + ] as const) + await writeFile(join(directory, path), bytes); + const packed = Bun.spawn( + ["tar", "-czf", join(directory, "artifact.tgz"), "package"], + { cwd: directory, stderr: "pipe" }, + ); + if (await packed.exited) + throw new Error(await new Response(packed.stderr).text()); + const artifactBytes = await readFile(join(directory, "artifact.tgz")); const artifact = { - packageVersion: "1.2.3", + packageVersion: "10.0.0", sourceCommit: "fixture", sourceTreeSha256: hash("source"), - tarballSha256: hash("artifact"), + tarballSha256: hash(artifactBytes), unpackedManifestSha256: hash("manifest"), }; const base = { - schemaVersion: 1 as const, - releaseTag: "v1.2.3", + schemaVersion: 2 as const, + releaseTag: "v10.0.0", artifact, artifactSha256: artifactIdentitySha256(artifact), checklistVersion: @@ -59,16 +104,18 @@ async function fixture() { preparedAt: new Date().toISOString(), artifactFile: "artifact.tgz" as const, pluginEntrySha256: hash("plugin"), + runtime: { + hostVersion: "2.1.4" as const, + sourcePackageSha256: hash(sourcePackage), + tuiEntrySha256: hash("tui"), + pluginPackageSha256: hash(pluginPackage), + configSha256: hash(config), + }, }; await writeFile( join(directory, "prepared.json"), JSON.stringify({ ...base, sha256: preparedCanarySha256(base) }), ); - await writeFile(join(directory, "artifact.tgz"), "artifact"); - await writeFile( - join(directory, "fixture/.opencode/plugins/flow.js"), - "plugin", - ); await writeFile(join(directory, "prompt.txt"), "Run fixture"); await authorizePaidRun(directory, { schemaVersion: 1, @@ -108,7 +155,7 @@ test("changed prepared bytes fail before consuming or launching", async () => { runPaidCanary(input, async () => { throw new Error("LAUNCHED"); }), - ).rejects.toThrow("bytes changed"); + ).rejects.toThrow("tarball bytes"); expect((await paidRunStatus(directory)).consumed).toBe(0); }); @@ -120,18 +167,21 @@ test("canary launch environment points PWD at the fixture", () => { TYPESAFE_API_KEY: "synthetic-not-real", FLOW_CANARY_SENTINEL: "retained", }), - ).toEqual({ PWD: "/prepared/fixture", FLOW_CANARY_SENTINEL: "retained" }); + ).toEqual({ + PWD: "/prepared/fixture", + FLOW_CANARY_SENTINEL: "retained", + }); }); test.skipIf(process.platform === "win32")( - "default canary launch excludes ambient TypeSafe credentials", + "default canary server startup excludes ambient TypeSafe credentials", async () => { const { directory, input } = await fixture(); - const executable = join(directory, "opencode"); + const executable = join(directory, "npm"); const record = join(directory, "child-environment.txt"); await writeFile( executable, - '#!/usr/bin/env node\nconst fs = require("node:fs");\nfs.writeFileSync(process.env.FLOW_CANARY_RECORD, [process.env.TYPESAFE_API_KEY ? "present" : "absent", process.env.FLOW_EVAL_AUTHORIZATION ? "present" : "absent", process.env.FLOW_CANARY_SENTINEL, process.env.PWD, process.cwd(), ""].join("\\n"));\n', + '#!/usr/bin/env node\nif (process.argv.includes("--version")) { console.log("opencode v2.1.4"); process.exit(0); }\nconst fs = require("node:fs");\nfs.writeFileSync(process.env.FLOW_CANARY_RECORD, [process.env.TYPESAFE_API_KEY ? "present" : "absent", process.env.FLOW_EVAL_AUTHORIZATION ? "present" : "absent", process.env.FLOW_CANARY_SENTINEL, process.env.PWD, process.cwd(), JSON.stringify(process.argv.slice(2)), ""].join("\\n"));\n', ); await chmod(executable, 0o755); const original = { @@ -147,9 +197,11 @@ test.skipIf(process.platform === "win32")( process.env.TYPESAFE_API_KEY = "synthetic-not-real"; process.env.FLOW_CANARY_SENTINEL = "retained"; process.env.FLOW_CANARY_RECORD = record; - expect(await runPaidCanary(input)).toBe(0); + await expect(runPaidCanary(input)).rejects.toThrow( + "server exited before readiness", + ); expect(await readFile(record, "utf8")).toBe( - `absent\nabsent\nretained\n${join(directory, "fixture")}\n${join(directory, "fixture")}\n`, + `absent\nabsent\nretained\n${join(directory, "fixture")}\n${join(directory, "fixture")}\n${JSON.stringify(["exec", "--yes", "--package=@opencode/cli@2.1.4", "--", "opencode", "serve", "--hostname", "127.0.0.1", "--port", "0"])}\n`, ); expect((await paidRunStatus(directory)).consumed).toBe(1); } finally { @@ -166,3 +218,135 @@ test.skipIf(process.platform === "win32")( } }, ); + +for (const path of ["index.js", "tui.js", "package.json"] as const) + test(`changed prepared ${path} fails before dispatch consumption`, async () => { + const { directory, input } = await fixture(); + await writeFile( + join(directory, "fixture/.opencode/plugins/flow", path), + "mutated", + ); + await expect( + runPaidCanary(input, async () => { + throw new Error("LAUNCHED"); + }), + ).rejects.toThrow(/bytes/); + expect((await paidRunStatus(directory)).consumed).toBe(0); + }); +test("changed prepared config fails before dispatch consumption", async () => { + const { directory, input } = await fixture(); + await writeFile(join(directory, "fixture/opencode.json"), "{}"); + await expect( + runPaidCanary(input, async () => { + throw new Error("LAUNCHED"); + }), + ).rejects.toThrow("installation bytes changed"); + expect((await paidRunStatus(directory)).consumed).toBe(0); +}); +test("unapproved host override is refused before dispatch consumption", async () => { + const { directory, input } = await fixture(); + const previous = process.env.FLOW_EVAL_OPENCODE_VERSION; + try { + process.env.FLOW_EVAL_OPENCODE_VERSION = "2.1.5"; + await expect( + runPaidCanary(input, async () => { + throw new Error("LAUNCHED"); + }), + ).rejects.toThrow("host override"); + expect((await paidRunStatus(directory)).consumed).toBe(0); + } finally { + if (previous === undefined) delete process.env.FLOW_EVAL_OPENCODE_VERSION; + else process.env.FLOW_EVAL_OPENCODE_VERSION = previous; + } +}); +test("historical preparation remains readable but cannot launch an active canary", async () => { + const { directory, input } = await fixture(); + const original = JSON.parse(await readFile(input.prepared, "utf8")); + const { runtime: _runtime, sha256: _sha, ...base } = original; + base.schemaVersion = 1; + base.artifact.packageVersion = "9.6.0"; + base.releaseTag = "v9.6.0"; + base.artifactSha256 = artifactIdentitySha256(base.artifact); + await writeFile( + input.prepared, + JSON.stringify({ ...base, sha256: preparedCanarySha256(base) }), + ); + await expect( + runPaidCanary(input, async () => { + throw new Error("LAUNCHED"); + }), + ).rejects.toThrow("native OpenCode v2 preparation"); + expect((await paidRunStatus(directory)).consumed).toBe(0); +}); +for (const failure of ["setup", "version"] as const) + test(`failed default ${failure} remains consumed without starting a model run`, async () => { + const { directory, input } = await fixture(); + const executable = join(directory, "npm"); + await writeFile( + executable, + `#!/usr/bin/env node\nif (process.argv.includes("install")) process.exit(${failure === "setup" ? 12 : 0});\nif (process.argv.includes("--version")) { console.log("opencode v1.18.31"); process.exit(0); }\nprocess.exit(99);\n`, + ); + await chmod(executable, 0o755); + const previous = process.env.PATH; + try { + process.env.PATH = `${directory}:${previous ?? ""}`; + await expect(runPaidCanary(input)).rejects.toThrow( + failure === "setup" ? /Command failed/ : /pinned host version/, + ); + expect((await paidRunStatus(directory)).consumed).toBe(1); + await expect(runPaidCanary(input)).rejects.toThrow("exhausted"); + } finally { + if (previous === undefined) delete process.env.PATH; + else process.env.PATH = previous; + } + }); + +test("canary launch removes ambient host controls while preserving provider API access", () => { + expect( + canaryLaunchEnvironment("/prepared/fixture", { + FLOW_EVAL_NATIVE_AUTH: "/ambient/connection.json", + OPENCODE_CONFIG: "/ambient/config.json", + OPENCODE_CONFIG_DIR: "/ambient/opencode", + OPENCODE_CONFIG_CONTENT: '{"plugins":["ambient-plugin"]}', + OPENCODE_FLOW_REVIEWER_MODEL: "unapproved/model", + OPENCODE_FLOW_REVIEWER_VARIANT: "max", + OPENCODE_EXPERIMENTAL_WEBSOCKETS: "true", + OPENCODE_EXPERIMENTAL_NATIVE_LLM: "true", + OPENCODE_SERVER_PASSWORD: "synthetic-server-password", + NODE_OPTIONS: "--import /ambient/hook.js", + NODE_PATH: "/ambient/modules", + BUN_OPTIONS: "--preload /ambient/hook.js", + BUN_PRELOAD: "/ambient/hook.js", + OPENAI_API_KEY: "synthetic-openai", + ANTHROPIC_API_KEY: "synthetic-anthropic", + OPENCODE_API_KEY: "synthetic-opencode", + AZURE_OPENAI_API_KEY: "synthetic-azure", + PATH: "/toolchain/bin", + }), + ).toEqual({ + PWD: "/prepared/fixture", + OPENAI_API_KEY: "synthetic-openai", + ANTHROPIC_API_KEY: "synthetic-anthropic", + OPENCODE_API_KEY: "synthetic-opencode", + AZURE_OPENAI_API_KEY: "synthetic-azure", + PATH: "/toolchain/bin", + }); +}); + +test("canary provider configuration uses the native HTTP transport setting for the approved route", () => { + expect( + canaryProviderConfiguration({ providerID: "openai", id: "approved-model" }), + ).toEqual({ + providers: { + openai: { + settings: { transport: "http" }, + models: { + "approved-model": { + settings: { transport: "http" }, + variants: [{ id: "default", settings: { transport: "http" } }], + }, + }, + }, + }, + }); +}); diff --git a/tests/canary-v2-live.test.ts b/tests/canary-v2-live.test.ts new file mode 100644 index 00000000..f6890220 --- /dev/null +++ b/tests/canary-v2-live.test.ts @@ -0,0 +1,429 @@ +import { expect, test } from "bun:test"; +import { createHash } from "node:crypto"; +import { + chmod, + mkdir, + mkdtemp, + readdir, + readFile, + realpath, + rm, + stat, + symlink, + writeFile, +} from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { canonicalJson } from "../evals/canonical-json.js"; +import { unpackedManifestSha256 } from "../evals/provenance.js"; +import { runPaidCanary } from "../scripts/canary-run.js"; +import { + artifactIdentitySha256, + CANARY_CHECKLIST_SHA256, + CANARY_CHECKLIST_VERSION, + deriveCanaryResult, + preparedCanarySha256, +} from "../scripts/eval-canary.js"; +import type { NativeCanaryExport } from "../scripts/export-canary-native.js"; +import { authorizePaidRun, paidRunStatus } from "../scripts/paid-budget.js"; + +const LIVE = process.env.FLOW_CANARY_LIVE === "1"; +const digest = (bytes: string | Uint8Array) => + `sha256:${createHash("sha256").update(bytes).digest("hex")}`; + +test.skipIf(!LIVE)( + "charged canary exports real native manager and reviewer sessions before its pinned host stops", + async () => { + const executable = process.env.FLOW_CANARY_LIVE_EXECUTABLE; + if (!executable) + throw new Error( + "FLOW_CANARY_LIVE_EXECUTABLE must name the existing pinned host.", + ); + const root = await realpath( + await mkdtemp( + join( + process.env.FLOW_CANARY_LIVE_ROOT ?? tmpdir(), + "flow-canary-native-live-", + ), + ), + ); + const old = { + PATH: process.env.PATH, + FLOW_EVAL_AUTHORIZATION: process.env.FLOW_EVAL_AUTHORIZATION, + TYPESAFE_API_KEY: process.env.TYPESAFE_API_KEY, + FLOW_CANARY_SENTINEL: process.env.FLOW_CANARY_SENTINEL, + OPENCODE_CONFIG: process.env.OPENCODE_CONFIG, + OPENCODE_CONFIG_DIR: process.env.OPENCODE_CONFIG_DIR, + OPENCODE_CONFIG_CONTENT: process.env.OPENCODE_CONFIG_CONTENT, + OPENCODE_FLOW_REVIEWER_MODEL: process.env.OPENCODE_FLOW_REVIEWER_MODEL, + }; + let requests = 0; + let upgrades = 0; + const provider = Bun.serve({ + hostname: "127.0.0.1", + port: 0, + async fetch(request) { + if (request.headers.get("upgrade")?.toLowerCase() === "websocket") { + upgrades++; + return new Response("WebSocket is outside this canary", { + status: 400, + }); + } + const body = await request.json(); + if ( + !body || + typeof body !== "object" || + !("input" in body) || + !Array.isArray(body.input) + ) + throw new Error("Invalid native Responses request"); + const reviewer = JSON.stringify(body.input).includes("Review fixture"); + const delivered = body.input.some( + (item) => item.type === "function_call_output", + ); + const spawn = !reviewer && !delivered; + requests++; + const item = spawn + ? { + id: "fc_spawn", + type: "function_call", + call_id: "call_spawn_reviewer", + name: "canary_spawn", + arguments: "{}", + status: "completed", + } + : { + id: `message_${requests}`, + type: "message", + role: "assistant", + status: "completed", + content: [ + { + type: "output_text", + text: reviewer ? "Reviewed locally" : "Finished locally", + annotations: [], + }, + ], + }; + const events: Record[] = spawn + ? [ + { + type: "response.output_item.added", + output_index: 0, + item: { ...item, arguments: "", status: "in_progress" }, + }, + { + type: "response.function_call_arguments.delta", + item_id: item.id, + output_index: 0, + delta: "{}", + }, + { + type: "response.function_call_arguments.done", + item_id: item.id, + output_index: 0, + arguments: "{}", + }, + { type: "response.output_item.done", output_index: 0, item }, + ] + : [{ type: "response.output_item.done", output_index: 0, item }]; + events.push({ + type: "response.completed", + response: { + id: `resp_${requests}`, + object: "response", + created_at: 1, + status: "completed", + model: "model", + output: [item], + usage: { input_tokens: 1, output_tokens: 1, total_tokens: 2 }, + }, + }); + return new Response( + events.map((event) => `data: ${JSON.stringify(event)}\n\n`).join(""), + { headers: { "content-type": "text/event-stream" } }, + ); + }, + }); + + try { + const directory = join(root, "fixture/.opencode/plugins/flow"); + await mkdir(directory, { recursive: true }); + await mkdir(join(root, "package/dist"), { recursive: true }); + const ambient = join(root, "ambient"); + await mkdir(ambient); + const ambientMarker = join(root, "ambient-loaded.json"); + await writeFile( + join(ambient, "index.js"), + `import { writeFileSync } from "node:fs"; writeFileSync(${JSON.stringify(ambientMarker)}, "loaded"); throw new Error("Ambient plugin loaded");`, + ); + const ambientConfig = JSON.stringify({ plugins: [{ package: ambient }] }); + await writeFile(join(ambient, "opencode.json"), ambientConfig); + const receipt = join(root, "cleanup.json"); + const entry = `import { Plugin, Provider, Model } from "@opencode/plugin"; +import { existsSync, writeFileSync } from "node:fs"; +import { dirname, join } from "node:path"; +export default Plugin.define({ id: "flow-canary-local-proof", async setup(ctx) { + const providerID = Provider.ID.make("fake"); + const modelID = Model.ID.make("model"); + await ctx.provider.transform(editor => editor.add({ info: { ...Provider.Info.empty(providerID), name: "Local canary", activation: "enabled", package: "@opencode/ai/providers/openai/responses", settings: { baseURL: ${JSON.stringify(`http://127.0.0.1:${provider.port}/v1`)}, transport: "http", apiKey: "synthetic-local-only" } }, models: [{ ...Model.Info.default(providerID, modelID), settings: { transport: "http" }, variants: [{ id: "default", settings: { transport: "http" } }], capabilities: { ...Model.Info.default(providerID, modelID).capabilities, tools: true } }] })); + await ctx.agent.transform(editor => editor.update("flow-reviewer", agent => { agent.mode = "subagent"; agent.system = "Review the fixture"; agent.permissions = [{ action: "*", resource: "*", effect: "allow" }]; })); + await ctx.session.hook("title", input => { input.result = "Local canary"; }); + await ctx.session.hook("http.request", input => { if (new URL(input.request.url).origin !== ${JSON.stringify(`http://127.0.0.1:${provider.port}`)}) throw new Error("Remote model access forbidden"); }); + await ctx.tool.transform(editor => editor.add({ name: "canary_spawn", description: "Create local reviewer evidence", options: { codemode: false }, input: { type: "object", properties: {} }, async execute(_, context) { + const child = await ctx.session.create({ parentID: context.sessionID, agent: "flow-reviewer", model: { providerID: "fake", id: "model" }, location: { directory: ctx.location.directory } }); + await ctx.session.prompt({ sessionID: child.id, text: "Review fixture" }); + await ctx.session.wait({ sessionID: child.id }); + return { content: "Reviewer finished" }; + } })); + return () => { const output = dirname(process.env.XDG_DATA_HOME); writeFileSync(${JSON.stringify(receipt)}, JSON.stringify({ nativeExistsBeforeCleanup: existsSync(join(output, "native.json")), pid: process.pid, authorization: process.env.FLOW_EVAL_AUTHORIZATION === undefined, typesafe: process.env.TYPESAFE_API_KEY === undefined, reviewer: process.env.OPENCODE_FLOW_REVIEWER_MODEL, recovery: ctx.options.recovery, ambientConfigAbsent: ["OPENCODE_CONFIG", "OPENCODE_CONFIG_DIR", "OPENCODE_CONFIG_CONTENT"].every(key => process.env[key] === undefined) })); }; +} });`; + const tui = "export default {};\n"; + const metadata = JSON.stringify({ + name: "opencode-plugin-flow", + version: "10.0.0", + dependencies: {}, + devDependencies: { "@opencode/plugin": "2.1.4" }, + }); + const pluginPackage = canonicalJson({ + name: "opencode-plugin-flow", + version: "10.0.0", + type: "module", + exports: { ".": "./index.js", "./tui": "./tui.js" }, + dependencies: { "@opencode/plugin": "2.1.4" }, + }); + const config = canonicalJson({ + plugins: [ + { + package: "./.opencode/plugins/flow", + options: { recovery: { mode: "off" } }, + }, + ], + update: "disable", + share: "disabled", + }); + for (const [path, bytes] of [ + ["package/package.json", metadata], + ["package/dist/index.js", entry], + ["package/dist/tui.js", tui], + ["fixture/.opencode/plugins/flow/index.js", entry], + ["fixture/.opencode/plugins/flow/tui.js", tui], + ["fixture/.opencode/plugins/flow/package.json", pluginPackage], + ["fixture/opencode.json", config], + ] as const) + await writeFile(join(root, path), bytes); + await symlink( + join(import.meta.dir, "../node_modules"), + join(directory, "node_modules"), + "dir", + ); + const packed = Bun.spawn( + ["tar", "-czf", join(root, "artifact.tgz"), "package"], + { cwd: root, stderr: "pipe" }, + ); + if (await packed.exited) + throw new Error(await new Response(packed.stderr).text()); + const artifact = { + packageVersion: "10.0.0", + sourceCommit: "local-fixture", + sourceTreeSha256: digest("fixture"), + tarballSha256: digest(await readFile(join(root, "artifact.tgz"))), + unpackedManifestSha256: await unpackedManifestSha256( + join(root, "artifact.tgz"), + ), + }; + const base = { + schemaVersion: 2 as const, + releaseTag: "v10.0.0", + artifact, + artifactSha256: artifactIdentitySha256(artifact), + checklistVersion: + CANARY_CHECKLIST_VERSION as typeof CANARY_CHECKLIST_VERSION, + checklistSha256: CANARY_CHECKLIST_SHA256, + preparedAt: new Date().toISOString(), + artifactFile: "artifact.tgz" as const, + pluginEntrySha256: digest(entry), + runtime: { + hostVersion: "2.1.4" as const, + sourcePackageSha256: digest(metadata), + tuiEntrySha256: digest(tui), + pluginPackageSha256: digest(pluginPackage), + configSha256: digest(config), + }, + }; + const prepared = { ...base, sha256: preparedCanarySha256(base) }; + await writeFile(join(root, "prepared.json"), JSON.stringify(prepared)); + await writeFile(join(root, "prompt.txt"), "Run fixture"); + await writeFile( + join(root, "npm"), + `#!/usr/bin/env node\nconst {spawn} = require("node:child_process");\nconst args = process.argv.slice(2);\nif (args[0] === "install") process.exit(0);\nif (JSON.stringify(args.slice(0,5)) !== JSON.stringify(["exec","--yes","--package=@opencode/cli@2.1.4","--","opencode"])) process.exit(99);\nconst child = spawn(${JSON.stringify(executable)}, args.slice(5), {stdio:"inherit"});\nchild.on("error", () => process.exit(98));\nchild.on("exit", code => process.exit(code ?? 97));\n`, + ); + await chmod(join(root, "npm"), 0o755); + await authorizePaidRun(root, { + schemaVersion: 1, + purpose: "Local fake provider only", + models: ["fake/model"], + maxDispatches: 1, + expiresAt: new Date(Date.now() + 120000).toISOString(), + }); + process.env.FLOW_EVAL_AUTHORIZATION = root; + process.env.PATH = `${root}:${old.PATH ?? ""}`; + process.env.TYPESAFE_API_KEY = "synthetic-not-real"; + process.env.FLOW_CANARY_SENTINEL = "retained"; + process.env.OPENCODE_CONFIG = join(ambient, "opencode.json"); + process.env.OPENCODE_CONFIG_DIR = ambient; + process.env.OPENCODE_CONFIG_CONTENT = ambientConfig; + process.env.OPENCODE_FLOW_REVIEWER_MODEL = "unreviewed/override"; + expect( + await runPaidCanary({ + prepared: join(root, "prepared.json"), + model: "fake/model", + prompt: join(root, "prompt.txt"), + }), + ).toBe(0); + const runs = (await readdir(root)).filter((path) => + path.startsWith("run-"), + ); + expect(runs.length).toBe(1); + const output = join(root, runs[0] ?? "missing"); + const native: NativeCanaryExport = JSON.parse( + await readFile(join(output, "native.json"), "utf8"), + ); + const run = JSON.parse(await readFile(join(output, "run.json"), "utf8")); + const observed = deriveCanaryResult({ + packageVersion: prepared.artifact.packageVersion, + artifactSha256: prepared.artifactSha256, + tarballSha256: prepared.artifact.tarballSha256, + preparedSha256: prepared.sha256, + pluginEntrySha256: prepared.pluginEntrySha256, + installation: null, + session: null, + transcript: native, + }); + expect(observed.actors.map((actor) => actor.role).sort()).toEqual([ + "manager", + "reviewer", + ]); + expect( + JSON.parse( + await readFile(join(output, "config/opencode/opencode.json"), "utf8"), + ), + ).toEqual({ + providers: { + fake: { + settings: { transport: "http" }, + models: { + model: { + settings: { transport: "http" }, + variants: [{ id: "default", settings: { transport: "http" } }], + }, + }, + }, + }, + }); + expect(native.host.version).toBe("2.1.4"); + expect(native.sessions.length).toBe(2); + const manager = native.sessions.find( + (session) => session.info.parentID === undefined, + ); + const reviewer = native.sessions.find( + (session) => session.info.agent === "flow-reviewer", + ); + expect(manager?.info.model).toEqual({ + providerID: "fake", + id: "model", + variant: "default", + }); + expect(reviewer?.info.parentID).toBe(manager?.info.id); + expect(reviewer?.info.model).toEqual({ + providerID: "fake", + id: "model", + variant: "default", + }); + expect( + reviewer?.messages.some( + (message) => + message.type === "assistant" && + message.content.some( + (part) => + part.type === "text" && part.text === "Reviewed locally", + ), + ), + ).toBe(true); + expect(run).toMatchObject({ + preparedSha256: prepared.sha256, + rootSessionID: manager?.info.id, + host: { version: "2.1.4", pid: native.host.pid }, + native: { + path: "native.json", + sha256: digest(await readFile(join(output, "native.json"))), + }, + outcome: "succeeded", + provider: { id: "fake", transport: "http" }, + providerConfiguration: { + path: "config/opencode/opencode.json", + sha256: digest( + await readFile(join(output, "config/opencode/opencode.json")), + ), + }, + }); + expect(JSON.parse(await readFile(receipt, "utf8"))).toEqual({ + nativeExistsBeforeCleanup: true, + pid: native.host.pid, + authorization: true, + typesafe: true, + reviewer: "fake/model", + recovery: { mode: "off" }, + ambientConfigAbsent: true, + }); + await expect(stat(ambientMarker)).rejects.toThrow(); + expect((await paidRunStatus(root)).consumed).toBe(1); + expect(requests).toBe(3); + expect(upgrades).toBe(0); + expect(() => process.kill(native.host.pid, 0)).toThrow(); + const log = await readFile(join(output, "server.log"), "utf8"); + expect(log).not.toContain("server password"); + await expect( + runPaidCanary({ + prepared: join(root, "prepared.json"), + model: "fake/model", + prompt: join(root, "prompt.txt"), + }), + ).rejects.toThrow("exhausted"); + await writeFile( + join(root, "verification.json"), + JSON.stringify( + { + status: "PASS", + requests, + upgrades, + protocol: "native-openai-responses", + nativeHost: native.host, + exportedSessions: native.sessions.map((session) => ({ + id: session.info.id, + parentID: session.info.parentID, + agent: session.info.agent, + model: session.info.model, + })), + charged: 1, + hostStopped: true, + exportedBeforeCleanup: true, + ambientConfigRejected: true, + recovery: "off", + }, + null, + 2, + ), + ); + } finally { + provider.stop(true); + for (const [key, value] of Object.entries(old)) { + if (value === undefined) delete process.env[key]; + else process.env[key] = value; + } + if (!process.env.FLOW_CANARY_LIVE_ROOT) + await rm(root, { recursive: true, force: true }); + } + }, + 60000, +); diff --git a/tests/canary-v2-native-auth.test.ts b/tests/canary-v2-native-auth.test.ts new file mode 100644 index 00000000..d41192b0 --- /dev/null +++ b/tests/canary-v2-native-auth.test.ts @@ -0,0 +1,1002 @@ +import { afterEach, expect, spyOn, test } from "bun:test"; +import { createHash } from "node:crypto"; +import { + chmod, + mkdir, + mkdtemp, + readdir, + readFile, + realpath, + rm, + stat, + symlink, + writeFile, +} from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { pathToFileURL } from "node:url"; +import { type CredentialEntry, OpenCode } from "@opencode/client"; +import { Service } from "@opencode/client/service"; +import { canonicalJson } from "../evals/canonical-json.js"; +import { startNativeAccountBroker } from "../evals/native-auth/broker.js"; +import { + nativeAuthSourceSha256, + stageNativeAuthClient, +} from "../evals/native-auth/host.js"; +import { + type NativeAccountConnection, + object, + PLACEHOLDER_REFRESH, +} from "../evals/native-auth/protocol.js"; +import { OpenCodeStartup } from "../evals/opencode-host.js"; +import { + canaryPublicNativeEvidence, + runPaidCanary, +} from "../scripts/canary-run.js"; +import { + artifactIdentitySha256, + CANARY_CHECKLIST_SHA256, + CANARY_CHECKLIST_VERSION, + CANARY_NATIVE_AUTH_DIRECTORY, + CanaryAuthLaunchReceiptSchema, + CanaryNativeAuthSchema, + canaryAuthLaunchMatches, + canaryProviderConfiguration, + deriveCanaryResult, + measureCanaryInstallation, + parsePreparedCanary, + preparedCanarySha256, + recordCanary, +} from "../scripts/eval-canary.js"; +import { authorizePaidRun, paidRunStatus } from "../scripts/paid-budget.js"; +import { FixtureRpc } from "./fixtures/native-auth-plugin.js"; +import { localScriptedProvider } from "./fixtures/opencode-v2-scripted-provider.js"; + +const roots: string[] = []; +const previousAuthorization = process.env.FLOW_EVAL_AUTHORIZATION; +afterEach(async () => { + if (previousAuthorization === undefined) + delete process.env.FLOW_EVAL_AUTHORIZATION; + else process.env.FLOW_EVAL_AUTHORIZATION = previousAuthorization; + await Promise.all( + roots.splice(0).map((root) => rm(root, { recursive: true, force: true })), + ); +}); +const digest = (value: string | Uint8Array) => + `sha256:${createHash("sha256").update(value).digest("hex")}`; +const connection: NativeAccountConnection = { + version: 1, + url: "http://127.0.0.1:1/", + capability: "a".repeat(64), + accounts: [ + { + route: { + providerID: "fake", + integrationID: "openai", + credentialID: "synthetic-selected", + methodID: "synthetic-method", + label: "synthetic-label", + }, + metadata: { accountId: "private-metadata-sentinel" }, + }, + ], +}; +async function fixture( + options: { auth?: boolean; entry?: string } = { auth: true }, +) { + const root = await realpath( + await mkdtemp(join(tmpdir(), "flow-canary-auth-")), + ); + roots.push(root); + const plugin = join(root, "fixture/.opencode/plugins/flow"); + await mkdir(plugin, { recursive: true }); + await mkdir(join(root, "package/dist"), { recursive: true }); + const entry = options.entry ?? "export default {};\n"; + const sourcePackage = canonicalJson({ + name: "opencode-plugin-flow", + version: "10.0.0", + dependencies: {}, + devDependencies: { "@opencode/plugin": "2.1.4" }, + }); + const pluginPackage = canonicalJson({ + name: "opencode-plugin-flow", + version: "10.0.0", + type: "module", + exports: { ".": "./index.js", "./tui": "./tui.js" }, + dependencies: { "@opencode/plugin": "2.1.4" }, + }); + const config = canonicalJson({ + plugins: [ + { + package: "./.opencode/plugins/flow", + options: { recovery: { mode: "off" } }, + }, + ], + update: "disable", + share: "disabled", + }); + for (const [path, bytes] of [ + ["package/package.json", sourcePackage], + ["package/dist/index.js", entry], + ["package/dist/tui.js", "export default {};\n"], + ["fixture/.opencode/plugins/flow/index.js", entry], + ["fixture/.opencode/plugins/flow/tui.js", "export default {};\n"], + ["fixture/.opencode/plugins/flow/package.json", pluginPackage], + ["fixture/opencode.json", config], + ] as const) + await writeFile(join(root, path), bytes); + const tar = Bun.spawn( + ["tar", "-czf", join(root, "artifact.tgz"), "package"], + { cwd: root, stderr: "pipe" }, + ); + if (await tar.exited) throw new Error(await new Response(tar.stderr).text()); + const artifact = { + packageVersion: "10.0.0", + sourceCommit: "synthetic", + sourceTreeSha256: digest("source"), + tarballSha256: digest(await readFile(join(root, "artifact.tgz"))), + unpackedManifestSha256: digest("manifest"), + }; + const staged = + options.auth === false + ? undefined + : await stageNativeAuthClient(join(root, CANARY_NATIVE_AUTH_DIRECTORY)); + const nativeAuth = staged + ? CanaryNativeAuthSchema.parse({ + ...staged, + pluginDirectory: CANARY_NATIVE_AUTH_DIRECTORY, + kind: "native-account-source-v1", + }) + : undefined; + const base = { + schemaVersion: 2 as const, + releaseTag: "v10.0.0", + artifact, + artifactSha256: artifactIdentitySha256(artifact), + checklistVersion: + CANARY_CHECKLIST_VERSION as typeof CANARY_CHECKLIST_VERSION, + checklistSha256: CANARY_CHECKLIST_SHA256, + preparedAt: "2026-10-24T00:00:00.000Z", + artifactFile: "artifact.tgz" as const, + pluginEntrySha256: digest(entry), + runtime: { + hostVersion: "2.1.4" as const, + sourcePackageSha256: digest(sourcePackage), + tuiEntrySha256: digest("export default {};\n"), + pluginPackageSha256: digest(pluginPackage), + configSha256: digest(config), + ...(nativeAuth ? { nativeAuth } : {}), + }, + }; + const prepared = parsePreparedCanary({ + ...base, + sha256: preparedCanarySha256(base), + }); + await writeFile(join(root, "prepared.json"), canonicalJson(prepared)); + await writeFile(join(root, "prompt.txt"), "Local only"); + const privateDirectory = join(root, "source"); + await mkdir(privateDirectory, { mode: 0o700 }); + const source = join(privateDirectory, "connection.json"); + await writeFile(source, JSON.stringify(connection), { mode: 0o600 }); + await authorizePaidRun(root, { + schemaVersion: 1, + purpose: "Synthetic loopback only", + models: ["fake/model", "native-auth-fixture/model"], + maxDispatches: 1, + expiresAt: new Date(Date.now() + 120000).toISOString(), + }); + process.env.FLOW_EVAL_AUTHORIZATION = root; + return { + root, + prepared, + nativeAuth, + source, + input: { + prepared: join(root, "prepared.json"), + model: "fake/model", + prompt: join(root, "prompt.txt"), + ...(nativeAuth ? { nativeAuthSource: source } : {}), + }, + }; +} +function transcript(directory: string) { + return { + schemaVersion: 2, + host: { version: "2.1.4", pid: 42, location: { directory } }, + sessions: [ + { + info: { + id: "ses_manager", + agent: "build", + model: { providerID: "fake", id: "model", variant: "default" }, + location: { directory }, + outcome: "succeeded", + }, + messages: [], + }, + { + info: { + id: "ses_reviewer", + parentID: "ses_manager", + agent: "flow-reviewer", + model: { providerID: "fake", id: "model", variant: "default" }, + location: { directory }, + }, + messages: [], + }, + ], + }; +} +function receipt( + preparedSha256: string, + nativeAuth: unknown, + native: ReturnType, +) { + const model = native.sessions[0]?.info.model; + if (!model) throw new Error("Missing fixture model"); + return CanaryAuthLaunchReceiptSchema.parse({ + schemaVersion: 2, + warming: false, + preparedSha256, + nativeAuth, + rootSessionID: "ses_manager", + model, + host: { pid: 42, version: "2.1.4" }, + provider: { id: "fake", transport: "http" }, + providerPolicySha256: digest( + canonicalJson(canaryProviderConfiguration(model)), + ), + native: { path: "native.json", sha256: digest(canonicalJson(native)) }, + outcome: "succeeded", + }); +} + +test("sealed client manifest survives key reordering and canonical installation round-trip", async () => { + const f = await fixture(); + if (!f.nativeAuth) throw new Error("Missing fixture attachment"); + const files = Object.fromEntries( + Object.entries(f.nativeAuth.files).reverse(), + ); + expect(String(nativeAuthSourceSha256(files))).toBe(f.nativeAuth.sourceSha256); + const measured = await measureCanaryInstallation(f.prepared, f.root); + const roundTrip = JSON.parse(canonicalJson(measured)); + expect(roundTrip.nativeAuth.files).toEqual(files); + expect(String(nativeAuthSourceSha256(roundTrip.nativeAuth.files))).toBe( + f.nativeAuth.sourceSha256, + ); + expect(Object.keys(roundTrip.nativeAuth.sourceFiles).sort()).toEqual([ + "client-plugin.ts", + "index.ts", + "package.json", + "protocol.ts", + ]); + expect(digest(await readFile(join(f.root, "fixture/opencode.json")))).toBe( + f.prepared.schemaVersion === 2 + ? f.prepared.runtime.configSha256 + : "unexpected", + ); + const native = transcript(""); + const launch = receipt(f.prepared.sha256, f.nativeAuth, native); + const derive = (installation: unknown) => + deriveCanaryResult({ + packageVersion: "10.0.0", + artifactSha256: f.prepared.artifactSha256, + tarballSha256: f.prepared.artifact.tarballSha256, + preparedSha256: f.prepared.sha256, + pluginEntrySha256: f.prepared.pluginEntrySha256, + installation, + session: null, + transcript: native, + }); + roundTrip.nativeAuth.launchReceipt = launch; + expect(derive(roundTrip).hostConfigSha256).toBe( + derive(JSON.parse(canonicalJson(roundTrip))).hostConfigSha256, + ); + expect( + canaryAuthLaunchMatches({ + preparedSha256: f.prepared.sha256, + nativeAuth: f.nativeAuth, + receipt: launch, + transcript: native, + }), + ).toBe(true); + roundTrip.nativeAuth.sourceFiles["protocol.ts"] += "\nchanged"; + expect(derive(roundTrip).status).toBe("incomplete"); + expect(derive(roundTrip).checks["installs-packed-artifact"]).toBe(false); +}); +for (const mutation of ["entry", "extra", "symlink", "manifest"] as const) + test(`prepared auth ${mutation} mutation fails before charge`, async () => { + const f = await fixture(); + if (mutation === "entry") + await writeFile( + join(f.root, CANARY_NATIVE_AUTH_DIRECTORY, "client-plugin.ts"), + "changed", + ); + if (mutation === "extra") + await writeFile( + join(f.root, CANARY_NATIVE_AUTH_DIRECTORY, "extra.ts"), + "extra", + ); + if (mutation === "symlink") { + await rm(join(f.root, CANARY_NATIVE_AUTH_DIRECTORY, "protocol.ts")); + await symlink( + join(f.root, "prompt.txt"), + join(f.root, CANARY_NATIVE_AUTH_DIRECTORY, "protocol.ts"), + ); + } + if (mutation === "manifest") { + const raw = JSON.parse(await readFile(f.input.prepared, "utf8")); + raw.runtime.nativeAuth.files["protocol.ts"] = digest("different"); + await writeFile(f.input.prepared, canonicalJson(raw)); + } + await expect( + runPaidCanary(f.input, async () => { + throw new Error("launched"); + }), + ).rejects.toThrow(); + expect((await paidRunStatus(f.root)).consumed).toBe(0); + }); +test("source/preparation mode mismatch and unsafe source file fail without charging", async () => { + const f = await fixture(); + const { nativeAuthSource: _source, ...without } = f.input; + await expect(runPaidCanary(without)).rejects.toThrow("selected together"); + await chmod(f.source, 0o644); + await expect(runPaidCanary(f.input)).rejects.toThrow( + "Native account authentication failed", + ); + expect((await paidRunStatus(f.root)).consumed).toBe(0); + const noAuth = await fixture({ auth: false }); + await expect( + runPaidCanary({ ...noAuth.input, nativeAuthSource: f.source }), + ).rejects.toThrow("selected together"); + expect((await paidRunStatus(noAuth.root)).consumed).toBe(0); + expect( + Object.hasOwn( + noAuth.prepared.schemaVersion === 2 ? noAuth.prepared.runtime : {}, + "nativeAuth", + ), + ).toBe(false); +}); +test("charged source acquisition failure is generic, remains consumed, and removes private host data", async () => { + const f = await fixture(); + await expect(runPaidCanary(f.input)).rejects.toThrow( + "Native-auth canary execution failed during prepare.", + ); + expect((await paidRunStatus(f.root)).consumed).toBe(1); + const run = (await readdir(f.root)).find((name) => name.startsWith("run-")); + if (!run) throw new Error("Missing failed run"); + expect(await readdir(join(f.root, run))).toEqual(["server.log"]); + await expect(runPaidCanary(f.input)).rejects.toThrow("exhausted"); + expect(await readFile(join(f.root, run, "server.log"), "utf8")).toBe( + "Native authentication diagnostics omitted.\n", + ); +}); +for (const failure of ["setup", "version", "startup", "release"] as const) + test(`charged auth ${failure} failure stops its host and disposes private attachment independently`, async () => { + const f = await fixture(); + let releases = 0; + const relay = Bun.serve({ + hostname: "127.0.0.1", + port: 0, + async fetch(request) { + const path = new URL(request.url).pathname; + if (path === "/acquire") + return Response.json({ lease: "b".repeat(64) }); + if (path === "/release") { + releases++; + return failure === "release" + ? new Response("private-metadata-sentinel", { status: 503 }) + : Response.json({}); + } + return new Response("refused", { status: 403 }); + }, + }); + const oldPath = process.env.PATH; + try { + await writeFile( + f.source, + JSON.stringify({ ...connection, url: relay.url.href }), + { mode: 0o600 }, + ); + await writeFile( + join(f.root, "npm"), + `#!/usr/bin/env node\nconst args=process.argv.slice(2);if(args[0]==="install"){console.error("opaque-access-sentinel");process.exit(${failure === "setup" || failure === "release" ? 12 : 0});}if(args.includes("--version")){console.log(${JSON.stringify(failure === "version" ? "opencode v1.18.31" : "opencode v2.1.4")});process.exit(0);}console.error("opaque-access-sentinel");process.exit(99);\n`, + { mode: 0o700 }, + ); + process.env.PATH = `${f.root}:${oldPath ?? ""}`; + const phase = + failure === "release" + ? "cleanup" + : failure === "setup" + ? "dependencies" + : "startup"; + await expect(runPaidCanary(f.input)).rejects.toThrow( + `Native-auth canary execution failed during ${phase}.`, + ); + expect(releases).toBe(1); + expect((await paidRunStatus(f.root)).consumed).toBe(1); + const run = (await readdir(f.root)).find((name) => + name.startsWith("run-"), + ); + if (!run) throw new Error("Missing failed run"); + expect(await readdir(join(f.root, run))).toEqual(["server.log"]); + expect(await readFile(join(f.root, run, "server.log"), "utf8")).toBe( + "Native authentication diagnostics omitted.\n", + ); + expect( + (await measureCanaryInstallation(f.prepared, f.root)).schemaVersion, + ).toBe(2); + } finally { + relay.stop(true); + if (oldPath === undefined) delete process.env.PATH; + else process.env.PATH = oldPath; + } + }); +test("public evidence removes opaque capability, private paths, access and nested metadata keys", () => { + const values = [ + "opaque-capability-sentinel", + "/private/auth-connection-sentinel.json", + "opaque-access-sentinel", + "metadata-private-sentinel", + ]; + const evidence = { + text: values.join(" "), + nested: { [values[3] ?? "missing"]: values[2] }, + messages: [{ error: values[0] }], + }; + const result = canaryPublicNativeEvidence(evidence, values); + expect(result).toEqual({ + text: " ", + nested: { "": "" }, + messages: [{ error: "" }], + }); + for (const value of values) + expect(canonicalJson(result)).not.toContain(value); +}); +test("auth recorder requires a launch receipt and retains a canonical self-contained public seal", async () => { + const f = await fixture(); + if (!f.nativeAuth) throw new Error("Missing fixture attachment"); + const native = transcript(join(f.root, "fixture")); + const input = { + repositoryRoot: f.root, + prepared: f.prepared, + preparedDirectory: f.root, + operator: "synthetic", + session: null, + transcript: native, + recordedAt: new Date("2026-10-24T00:00:00.000Z"), + }; + await expect(recordCanary(input)).rejects.toThrow( + "matching public launch receipt", + ); + const launchReceipt = receipt(f.prepared.sha256, f.nativeAuth, native); + const invalid = { + ...launchReceipt, + host: { ...launchReceipt.host, pid: 43 }, + }; + await expect( + recordCanary({ ...input, launchReceipt: invalid }), + ).rejects.toThrow("matching public launch receipt"); + const saved = await recordCanary({ ...input, launchReceipt }); + const installation = JSON.parse( + await readFile( + join(f.root, "evals/canary", saved.record.artifacts.installation.path), + "utf8", + ), + ); + const retained = JSON.parse( + await readFile( + join( + f.root, + "evals/canary", + saved.record.artifacts.transcript?.path ?? "missing", + ), + "utf8", + ), + ); + expect( + canaryAuthLaunchMatches({ + preparedSha256: f.prepared.sha256, + nativeAuth: f.nativeAuth, + receipt: installation.nativeAuth.launchReceipt, + transcript: retained, + }), + ).toBe(true); + expect(installation.nativeAuth.launchReceipt.rootSessionID).toMatch( + /^id_[a-f0-9]{16}$/, + ); + for (const secret of [ + connection.capability, + connection.url, + f.source, + "private-metadata-sentinel", + ]) + expect(canonicalJson(installation)).not.toContain(secret); + expect( + (await recordCanary({ ...input, launchReceipt })).record.recordSha256, + ).toBe(saved.record.recordSha256); + for (const change of [ + "host", + "model", + "policy", + "native", + "source", + "unknown", + ] as const) { + const changed = structuredClone(launchReceipt); + if (change === "host") changed.host.pid++; + if (change === "model") changed.model.id = "other"; + if (change === "policy") changed.providerPolicySha256 = digest("other"); + if (change === "native") changed.native.sha256 = digest("other"); + if (change === "source") changed.nativeAuth.sourceSha256 = digest("other"); + const candidate = + change === "unknown" + ? { ...changed, configurationPath: "/private/unexpected" } + : changed; + expect( + canaryAuthLaunchMatches({ + preparedSha256: f.prepared.sha256, + nativeAuth: f.nativeAuth, + receipt: candidate, + transcript: native, + }), + change, + ).toBe(false); + } +}); + +const LIVE = process.env.FLOW_CANARY_NATIVE_AUTH_LIVE === "1"; +test.skipIf(!LIVE)( + "native canary loads sealed auth before seeding, exports before cleanup, and uses only the source bearer", + async () => { + const binary = process.env.FLOW_CANARY_LIVE_EXECUTABLE; + if (!binary) + throw new Error("A private pinned native executable is required."); + const scripted = localScriptedProvider(); + let bearerMatches = true; + let requests = 0; + let websocket = 0; + let dropSource: (() => Promise) | undefined; + const proxy = Bun.serve({ + hostname: "127.0.0.1", + port: 0, + async fetch(request) { + if (request.headers.get("upgrade")) { + websocket++; + return new Response("No WebSocket", { status: 400 }); + } + bearerMatches &&= + request.headers.get("authorization") === + "Bearer synthetic-owner-access-1"; + requests++; + if (dropSource) { + const stop = dropSource; + dropSource = undefined; + await stop(); + } + const url = new URL(request.url); + url.host = scripted.server.url.host; + return fetch(new Request(url.href, request)); + }, + }); + const fixtureURL = pathToFileURL( + join(import.meta.dir, "fixtures/native-auth-plugin.ts"), + ).href; + const entry = `import {Plugin} from "@opencode/plugin"; +import Fixture from ${JSON.stringify(fixtureURL)}; +import {existsSync,writeFileSync} from "node:fs"; +import {dirname,join} from "node:path"; +export default Plugin.define({id:"flow-canary-native-auth-fixture",async setup(ctx){ + const dispose = await Fixture.setup({...ctx, options:{role:"child",baseURL:${JSON.stringify(new URL("v1", proxy.url).href)}}}); + await ctx.agent.transform(editor=>editor.update("flow-reviewer",agent=>{agent.mode="subagent";agent.system="Local review";agent.permissions=[{action:"*",resource:"*",effect:"allow"}];})); + await ctx.tool.transform(editor=>editor.add({name:"canary_spawn",description:"Local reviewer",options:{codemode:false},input:{type:"object",properties:{}},async execute(_,context){const child=await ctx.session.create({parentID:context.sessionID,agent:"flow-reviewer",model:{providerID:"native-auth-fixture",id:"model",variant:"default"},location:{directory:ctx.location.directory}});await ctx.session.prompt({sessionID:child.id,text:"Review locally"});await ctx.session.wait({sessionID:child.id});return {content:"Local reviewer complete"};}})); + return async()=>{writeFileSync(join(dirname(dirname(process.env.XDG_DATA_HOME)),"export-before-stop.json"),JSON.stringify({pid:process.pid,nativeExistsBeforeStop:existsSync(join(dirname(dirname(process.env.XDG_DATA_HOME)),"native.json"))}));await dispose();}; +}});`; + const f = await fixture({ entry }); + const sourceRoot = join(f.root, "owner"); + const env: Record = { PATH: "/usr/bin:/bin" }; + for (const [key, leaf] of Object.entries({ + HOME: "home", + XDG_CONFIG_HOME: "config", + XDG_DATA_HOME: "data", + XDG_CACHE_HOME: "cache", + XDG_STATE_HOME: "state", + TMPDIR: "tmp", + })) { + const path = join(sourceRoot, leaf); + await mkdir(path, { recursive: true, mode: 0o700 }); + env[key] = path; + } + const project = join(sourceRoot, "project"); + await mkdir(project, { mode: 0o700 }); + const sourceFixture = join(sourceRoot, "fixture"); + await mkdir(sourceFixture, { mode: 0o700 }); + await writeFile( + join(sourceFixture, "index.ts"), + `export {default} from ${JSON.stringify(fixtureURL)};\n`, + ); + await mkdir(join(sourceRoot, "config/opencode"), { mode: 0o700 }); + await writeFile( + join(sourceRoot, "config/opencode/opencode.json"), + JSON.stringify({ + plugins: [ + { + package: sourceFixture, + options: { role: "source", baseURL: new URL("v1", proxy.url).href }, + }, + ], + update: "disable", + warming: false, + share: "disabled", + }), + ); + const cli = join(sourceRoot, "native-cli"); + await writeFile( + cli, + `#!${process.execPath}\nconst {spawn}=require("node:child_process");const child=spawn(${JSON.stringify(binary)},process.argv.slice(2),{env:${JSON.stringify(env)},stdio:"inherit"});child.on("exit",code=>process.exit(code??1));\n`, + { mode: 0o700 }, + ); + const allocated = Bun.serve({ + hostname: "127.0.0.1", + port: 0, + fetch: () => new Response(), + }); + const port = allocated.port; + allocated.stop(true); + const configure = Bun.spawn([cli, "service", "set", "port", String(port)], { + cwd: project, + env, + stdout: "ignore", + stderr: "pipe", + }); + if (await configure.exited) + throw new Error("Private native service configuration failed."); + const endpoint = await Service.ensure({ + command: [binary, "serve", "--service"], + env, + file: join(sourceRoot, "state/opencode/service.json"), + version: "2.1.4", + }); + const owner = OpenCode.make({ + baseUrl: endpoint.url, + headers: Service.headers(endpoint), + }); + let ownerPID: number | undefined; + let broker: + | Awaited> + | undefined; + const oldPath = process.env.PATH; + let childStats: unknown; + let childRows: CredentialEntry[] = []; + let privateConfigSha256: string | undefined; + let importAfterActive = false; + let rejectChildList = false; + const factory = OpenCodeStartup.prototype.client; + const observe = spyOn( + OpenCodeStartup.prototype, + "client", + ).mockImplementation(function ( + this: OpenCodeStartup, + baseUrl: string, + directory: string, + ) { + const client = factory.call(this, baseUrl, directory); + if (!client) return null; + const create = client.credential.create; + const list = client.credential.list; + return { + ...client, + credential: { + ...client.credential, + create: async (...args) => { + const plugins = await client.plugin.list({ + location: { directory }, + }); + importAfterActive = plugins.data.some( + (plugin) => + plugin.id === "flow.native-account-client" && + plugin.state.status === "active", + ); + if (!importAfterActive) + throw new Error("Client was not active before import"); + return create(...args); + }, + list: async (...args) => { + if (rejectChildList) + throw new Error( + "opaque-access-sentinel private-account-metadata", + ); + childStats = await client + .rpc(FixtureRpc) + .stats({}, { location: { directory } }); + childRows = await list(...args); + const run = (await readdir(f.root)).find((name) => + name.startsWith("run-"), + ); + if (run) + privateConfigSha256 = digest( + await readFile( + join( + f.root, + run, + "private-host/config/opencode/opencode.json", + ), + ), + ); + return childRows; + }, + }, + }; + }); + try { + ownerPID = (await owner.server.info()).pid; + await owner.model.list({ location: { directory: project } }); + await owner.credential.create({ + id: "synthetic-canary-account", + integrationID: "openai", + label: "Private synthetic account", + activate: true, + value: { + type: "oauth", + methodID: "native-auth-fixture-method", + access: "synthetic-before-access", + refresh: "original-refresh-must-stay-in-owner", + expires: 0, + metadata: { accountId: "private-account-metadata" }, + }, + }); + broker = await startNativeAccountBroker({ + opencodeExecutable: cli, + models: ["native-auth-fixture/model"], + }); + await writeFile(f.source, JSON.stringify(broker.configuration), { + mode: 0o600, + }); + await symlink( + join(import.meta.dir, "../node_modules"), + join(f.root, "fixture/.opencode/plugins/flow/node_modules"), + "dir", + ); + await writeFile( + join(f.root, "npm"), + `#!/usr/bin/env node\nconst{spawn}=require("node:child_process");const args=process.argv.slice(2);if(args[0]==="install")process.exit(0);if(JSON.stringify(args.slice(0,5))!==JSON.stringify(["exec","--yes","--package=@opencode/cli@2.1.4","--","opencode"]))process.exit(99);const env=Object.fromEntries(Object.entries(process.env).filter(([key])=>["PATH","HOME","XDG_CONFIG_HOME","XDG_DATA_HOME","XDG_CACHE_HOME","XDG_STATE_HOME","TMPDIR","PWD","OPENCODE_FLOW_REVIEWER_MODEL"].includes(key)));env.OPENAI_API_KEY="synthetic-ambient-key";const child=spawn(${JSON.stringify(binary)},args.slice(5),{stdio:"inherit",env});child.on("exit",code=>process.exit(code??1));\n`, + { mode: 0o700 }, + ); + process.env.PATH = `${f.root}:${oldPath ?? ""}`; + await writeFile( + f.input.prompt, + 'FLOW_SMOKE_ACTIONS [{"tool":"canary_spawn","input":{}}]', + ); + expect( + await runPaidCanary({ ...f.input, model: "native-auth-fixture/model" }), + ).toBe(0); + const run = (await readdir(f.root)).find((name) => + name.startsWith("run-"), + ); + if (!run) throw new Error("Missing native run"); + const output = join(f.root, run); + expect((await readdir(output)).sort()).toEqual([ + "export-before-stop.json", + "native.json", + "run.json", + "server.log", + ]); + const native = JSON.parse( + await readFile(join(output, "native.json"), "utf8"), + ); + const launch = JSON.parse( + await readFile(join(output, "run.json"), "utf8"), + ); + if (!f.nativeAuth) throw new Error("Missing attachment"); + expect( + canaryAuthLaunchMatches({ + preparedSha256: f.prepared.sha256, + nativeAuth: f.nativeAuth, + receipt: launch, + transcript: native, + }), + ).toBe(true); + expect( + JSON.parse( + await readFile(join(output, "export-before-stop.json"), "utf8"), + ), + ).toEqual({ pid: launch.host.pid, nativeExistsBeforeStop: true }); + expect(native.sessions.length).toBe(2); + expect( + native.sessions.map( + (session: { info: { model: unknown } }) => session.info.model, + ), + ).toEqual([ + { providerID: "native-auth-fixture", id: "model", variant: "default" }, + { providerID: "native-auth-fixture", id: "model", variant: "default" }, + ]); + expect( + native.sessions + .map((session: { info: { agent: string; tokens: unknown } }) => ({ + agent: session.info.agent, + tokens: session.info.tokens, + })) + .sort((left: { agent: string }, right: { agent: string }) => + left.agent.localeCompare(right.agent), + ), + ).toEqual([ + { + agent: "build", + tokens: { + input: 20, + output: 10, + reasoning: 8, + cache: { read: 6, write: 0 }, + }, + }, + { + agent: "flow-reviewer", + tokens: { + input: 10, + output: 5, + reasoning: 4, + cache: { read: 3, write: 0 }, + }, + }, + ]); + expect(importAfterActive).toBe(true); + expect( + childRows.map((row) => + row.value.type === "oauth" ? row.value.refresh : "unexpected", + ), + ).toEqual([PLACEHOLDER_REFRESH]); + expect(childStats).toMatchObject({ + foreignRequests: 0, + directChildRefreshes: 0, + }); + expect(requests).toBe(3); + expect(websocket).toBe(0); + expect(bearerMatches).toBe(true); + expect((await paidRunStatus(f.root)).consumed).toBe(1); + expect(() => process.kill(launch.host.pid, 0)).toThrow(); + expect(() => process.kill(ownerPID ?? 0, 0)).not.toThrow(); + const stats = await owner + .rpc(FixtureRpc) + .stats({}, { location: { directory: project } }); + if (!object(stats)) throw new Error("Invalid synthetic stats"); + expect(stats).toMatchObject({ + foreignRequests: 0, + directChildRefreshes: 0, + }); + const original = await owner.credential.list(); + expect(original).toMatchObject([ + { + id: "synthetic-canary-account", + integrationID: "openai", + active: true, + label: "Private synthetic account", + value: { + type: "oauth", + methodID: "native-auth-fixture-method", + refresh: "synthetic-owner-refresh-1", + access: "synthetic-owner-access-1", + metadata: { accountId: "private-account-metadata" }, + }, + }, + ]); + const publicBytes = ( + await Promise.all( + ["run.json", "native.json", "server.log"].map((name) => + readFile(join(output, name), "utf8"), + ), + ) + ).join("\n"); + if (!privateConfigSha256) + throw new Error("Missing private configuration measurement"); + expect(publicBytes).not.toContain(privateConfigSha256); + expect(publicBytes).not.toContain( + digest(canonicalJson({ accountId: "private-account-metadata" })), + ); + for (const secret of [ + broker.configuration.capability, + broker.configuration.url, + f.source, + "private-account-metadata", + "synthetic-owner-access-1", + "original-refresh-must-stay-in-owner", + ]) + expect(publicBytes).not.toContain(secret); + await expect(stat(join(output, "native-auth"))).rejects.toThrow(); + const next = async () => { + const nextFixture = await fixture({ entry }); + if (!broker) throw new Error("Missing broker"); + await writeFile( + nextFixture.source, + JSON.stringify(broker.configuration), + { mode: 0o600 }, + ); + await writeFile( + nextFixture.input.prompt, + 'FLOW_SMOKE_ACTIONS [{"tool":"canary_spawn","input":{}}]', + ); + await symlink( + join(import.meta.dir, "../node_modules"), + join(nextFixture.root, "fixture/.opencode/plugins/flow/node_modules"), + "dir", + ); + await writeFile( + join(nextFixture.root, "npm"), + await readFile(join(f.root, "npm")), + { mode: 0o700 }, + ); + process.env.PATH = `${nextFixture.root}:${oldPath ?? ""}`; + return nextFixture; + }; + rejectChildList = true; + const failedList = await next(); + await expect( + runPaidCanary({ + ...failedList.input, + model: "native-auth-fixture/model", + }), + ).rejects.toThrow("Native-auth canary execution failed during cleanup."); + const failedRun = (await readdir(failedList.root)).find((name) => + name.startsWith("run-"), + ); + if (!failedRun) throw new Error("Missing list-failed run"); + expect((await readdir(join(failedList.root, failedRun))).sort()).toEqual([ + "export-before-stop.json", + "server.log", + ]); + expect((await paidRunStatus(failedList.root)).consumed).toBe(1); + expect(() => process.kill(ownerPID ?? 0, 0)).not.toThrow(); + rejectChildList = false; + const lostSource = await next(); + const beforeLoss = requests; + dropSource = () => broker?.dispose() ?? Promise.resolve(); + await expect( + runPaidCanary({ + ...lostSource.input, + model: "native-auth-fixture/model", + }), + ).rejects.toThrow("Native-auth canary execution failed"); + expect(requests - beforeLoss).toBe(1); + expect((await paidRunStatus(lostSource.root)).consumed).toBe(1); + const lostRun = (await readdir(lostSource.root)).find((name) => + name.startsWith("run-"), + ); + if (!lostRun) throw new Error("Missing source-lost run"); + await expect( + stat(join(lostSource.root, lostRun, "run.json")), + ).rejects.toThrow(); + await expect( + stat(join(lostSource.root, lostRun, "private-host")), + ).rejects.toThrow(); + expect(() => process.kill(ownerPID ?? 0, 0)).not.toThrow(); + } finally { + observe.mockRestore(); + if (oldPath === undefined) delete process.env.PATH; + else process.env.PATH = oldPath; + const disposed = await Promise.allSettled([ + Promise.resolve().then(() => broker?.dispose()), + ]); + if (ownerPID) { + try { + process.kill(ownerPID, "SIGTERM"); + } catch {} + let stopped = false; + for (let attempt = 0; attempt < 200; attempt++) { + try { + process.kill(ownerPID, 0); + } catch { + stopped = true; + break; + } + await Bun.sleep(25); + } + expect(stopped).toBe(true); + } + proxy.stop(true); + scripted.server.stop(true); + expect(disposed.every((result) => result.status === "fulfilled")).toBe( + true, + ); + } + }, + 120000, +); diff --git a/tests/canary-v2-native.test.ts b/tests/canary-v2-native.test.ts new file mode 100644 index 00000000..77a14fae --- /dev/null +++ b/tests/canary-v2-native.test.ts @@ -0,0 +1,950 @@ +import { afterEach, expect, test } from "bun:test"; +import { createHash } from "node:crypto"; +import { readFileSync } from "node:fs"; +import { mkdtemp, readFile, realpath, rm, writeFile } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import type { + JsonValue, + SessionInfo, + SessionMessageAssistantTool, + SessionTransferData, +} from "@opencode/client"; +import { + canaryRecordIssue, + deriveCanaryResult, +} from "../scripts/eval-canary.js"; +import { + collectNativeCanary, + type NativeCanaryExport, +} from "../scripts/export-canary-native.js"; +import { + assuranceProjection, + deliveryProjection, +} from "../src/application/delivery.js"; +import { operationResult } from "../src/application/flow-response.js"; +import { SessionSchema } from "../src/application/schema.js"; +import { archivedProjection } from "../src/application/session-projection.js"; +import { + closureRetryRequest, + operationInputDigest, +} from "../src/domain/operation.js"; + +const temporary: string[] = []; +afterEach(async () => { + await Promise.all( + temporary + .splice(0) + .map((path) => rm(path, { recursive: true, force: true })), + ); +}); +const hash = (text: string) => + `sha256:${createHash("sha256").update(text).digest("hex")}`; +function flowSession() { + const session = JSON.parse( + readFileSync( + join(import.meta.dir, "../evals/canary/artifacts/8.1.2-session.json"), + "utf8", + ), + ); + for (const run of session.runs) + for (const validation of run.validations) + for (const assertion of validation.observedAssertions ?? []) + assertion.status = "passed"; + const closure = session.closure; + const operation = session.operations.find( + (item: { id: string }) => item.id === closure.operationId, + ); + operation.inputDigest = operationInputDigest({ + operationId: closure.operationId, + expectedRevision: closure.recordedRevision - 1, + sessionId: session.id, + kind: closure.kind, + summary: closure.summary, + }); + return SessionSchema.parse(session); +} +function tool( + name: string, + output: JsonValue, + input: Record = {}, +): SessionMessageAssistantTool { + return { + type: "tool", + id: `call_${name}`, + name, + time: { created: 1, completed: 2 }, + state: { + status: "completed", + input, + content: [{ type: "text", text: JSON.stringify(output) }], + }, + }; +} +function sessionInfo( + id: string, + directory: string, + parentID?: string, +): SessionInfo { + return { + id, + ...(parentID === undefined ? {} : { parentID }), + projectID: "project", + agent: parentID === undefined ? "build" : "flow-reviewer", + model: { providerID: "approved", id: "model" }, + location: { directory }, + cost: 0, + tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } }, + time: { created: 1, updated: 2 }, + }; +} +function native( + directory = "", + session = flowSession(), +): NativeCanaryExport { + const closeRequest = closureRetryRequest(session); + if (!closeRequest) throw new Error("Missing closure request"); + return { + schemaVersion: 2, + host: { + version: "2.1.4", + pid: 99, + urls: [], + paths: { tmp: "/tmp/fixture" }, + location: { directory }, + }, + sessions: [ + { + info: sessionInfo("ses_manager", directory), + messages: [ + { + id: "msg_manager", + type: "assistant", + agent: "build", + model: { providerID: "approved", id: "model" }, + time: { created: 1, completed: 2 }, + content: [ + tool("flow_status", { + status: "ok", + workflowData: { + runtimeIdentity: { + packageVersion: "10.0.0", + pluginEntrySha256: hash("plugin"), + }, + recovery: { mode: "off" }, + }, + }), + tool("flow_plan_save", { status: "ok" }), + tool("flow_validation_start", { status: "ok" }), + tool("flow_review_start", { status: "ok" }), + tool( + "flow_session_close", + JSON.parse( + JSON.stringify({ + status: "ok", + workflowData: { + operation: operationResult( + session, + closeRequest.operationId, + false, + session.closure, + ), + projection: archivedProjection(session), + delivery: deliveryProjection(session), + }, + }), + ), + { request: JSON.parse(JSON.stringify(closeRequest)) }, + ), + ], + }, + ], + }, + { + info: sessionInfo("ses_reviewer", directory, "ses_manager"), + messages: [ + { + id: "msg_reviewer", + type: "assistant", + agent: "flow-reviewer", + model: { providerID: "approved", id: "model" }, + time: { created: 1, completed: 2 }, + content: session.runs + .filter((run) => run.state === "completed") + .map((run) => { + const review = run.reviews.find( + (review) => review.result?.verdict === "passed", + ); + const operation = session.operations.find( + (operation) => + operation.kind === "feature-complete" && + operation.entityId === run.id, + ); + if (!review?.result || !operation || run.summary === null) + throw new Error("Missing accepted reviewer operation"); + const { recordedRevision, ...result } = review.result; + return { + ...tool( + "flow_feature_complete", + JSON.parse( + JSON.stringify({ + status: "ok", + workflowData: { + operation: { + operationId: operation.id, + revision: operation.committedRevision, + replayed: false, + entity: run, + }, + projection: { + sessionId: session.id, + revision: operation.committedRevision, + }, + }, + }), + ), + { + request: JSON.parse( + JSON.stringify({ + operationId: operation.id, + expectedRevision: recordedRevision - 1, + featureId: run.featureId, + assignmentId: review.id, + summary: run.summary, + result, + }), + ), + }, + ), + id: `complete_${run.id}`, + }; + }), + }, + ], + }, + ], + }; +} +function derive(transcript: unknown = native(), session = flowSession()) { + return deriveCanaryResult({ + packageVersion: "10.0.0", + artifactSha256: hash("identity"), + tarballSha256: hash("archive"), + preparedSha256: hash("prepared"), + pluginEntrySha256: hash("plugin"), + installation: { + schemaVersion: 2, + preparedSha256: hash("prepared"), + artifactSha256: hash("identity"), + tarballSha256: hash("archive"), + pluginEntrySha256: hash("plugin"), + installedPluginSha256: hash("plugin"), + sourcePackageSha256: hash("manifest"), + tuiEntrySha256: hash("tui"), + pluginPackageSha256: hash("local-manifest"), + configSha256: hash("config"), + }, + session, + transcript, + }); +} + +test("V2 canary uses native completed Flow tools and server session parentage", () => { + const result = derive(); + expect(result.status).toBe("passed"); + expect(result.checks).toEqual({ + "installs-packed-artifact": true, + "loads-flow-tools": true, + "saves-plan": true, + "captures-validation": true, + "dispatches-reviewer": true, + "closes-with-delivery": true, + }); + expect( + result.actors.map((actor) => [ + actor.role, + actor.requestedModel.routeProvider, + actor.sessionIds, + ]), + ).toEqual([ + ["manager", "approved", ["ses_manager"]], + ["reviewer", "approved", ["ses_reviewer"]], + ]); +}); + +function managerTool(value: NativeCanaryExport, name: string) { + const message = value.sessions[0]?.messages[0]; + if (message?.type !== "assistant") + throw new Error("Missing native assistant"); + const part = message.content.find( + (part) => part.type === "tool" && part.name === name, + ); + if (part?.type !== "tool" || part.state.status !== "completed") + throw new Error("Missing native completed tool"); + return part.state; +} +for (const mutation of [ + "wrong-host", + "wrong-parent", + "missing-parent", + "wrong-model", + "assistant-model", + "assistant-agent", + "wrong-directory", + "reviewer-without-completion", + "application-error", + "missing-content", + "truncated-content", + "unknown-completeness", + "recovery-on", + "runtime-identity", + "unsupported-delivery", + "unknown-message", + "task-metadata-only", + "old-wire-shape", +] as const) + test(`V2 canary rejects ${mutation}`, () => { + const value = structuredClone(native()); + const manager = value.sessions[0], + reviewer = value.sessions[1]; + if (!manager || !reviewer) throw new Error("Missing native sessions"); + switch (mutation) { + case "wrong-host": + value.host.version = "1.18.31"; + break; + case "wrong-parent": + reviewer.info.parentID = "ses_unknown"; + break; + case "missing-parent": + delete reviewer.info.parentID; + break; + case "wrong-model": + reviewer.info.model = { providerID: "unapproved", id: "model" }; + break; + case "assistant-model": { + const message = reviewer.messages[0]; + if (message?.type === "assistant") message.model.id = "different"; + break; + } + case "assistant-agent": { + const message = reviewer.messages[0]; + if (message?.type === "assistant") message.agent = "build"; + break; + } + case "wrong-directory": + reviewer.info.location.directory = "/elsewhere"; + break; + case "reviewer-without-completion": { + const message = reviewer.messages[0]; + if (message?.type === "assistant") message.content = []; + break; + } + case "application-error": + managerTool(value, "flow_plan_save").content = [ + { type: "text", text: '{"status":"error"}' }, + ]; + break; + case "missing-content": + managerTool(value, "flow_plan_save").content = [ + { type: "text", text: "unknown output" }, + ]; + break; + case "truncated-content": + managerTool(value, "flow_plan_save").metadata = { truncated: true }; + break; + case "unknown-completeness": + managerTool(value, "flow_plan_save").metadata = { + truncated: "unknown", + }; + break; + case "recovery-on": + managerTool(value, "flow_status").content = [ + { + type: "text", + text: JSON.stringify({ + status: "ok", + workflowData: { + runtimeIdentity: { + packageVersion: "10.0.0", + pluginEntrySha256: hash("plugin"), + }, + recovery: { mode: "shadow" }, + }, + }), + }, + ]; + break; + case "runtime-identity": + managerTool(value, "flow_status").content = [ + { + type: "text", + text: '{"status":"ok","workflowData":{"runtimeIdentity":{"packageVersion":"10.0.0","pluginEntrySha256":"changed"}}}', + }, + ]; + break; + case "unsupported-delivery": + managerTool(value, "flow_session_close").content = [ + { + type: "text", + text: '{"status":"ok","workflowData":{"delivery":{"assurance":{"conclusion":"unsupported"}}}}', + }, + ]; + break; + case "unknown-message": + return expect( + derive({ + ...value, + sessions: [ + { + ...manager, + messages: [ + ...manager.messages, + { id: "unknown", type: "future" }, + ], + }, + reviewer, + ], + }).status, + ).not.toBe("passed"); + case "task-metadata-only": + return expect( + derive({ + schemaVersion: 2, + host: value.host, + sessions: [manager], + messages: [ + { + tool: "task", + metadata: { + parentSessionId: "ses_manager", + sessionId: "ses_reviewer", + model: { providerID: "approved", id: "model" }, + }, + }, + ], + }).status, + ).not.toBe("passed"); + case "old-wire-shape": + return expect( + derive({ + info: { version: "2.1.4", directory: "" }, + messages: [ + { + info: { role: "assistant", sessionID: "ses_manager" }, + parts: [], + }, + ], + }).status, + ).not.toBe("passed"); + } + expect(derive(value).status).not.toBe("passed"); + }); + +test("retained 9.6 evidence still verifies offline without freshness rewriting", async () => { + const directory = join(import.meta.dir, "../evals/canary"); + const record = JSON.parse( + await readFile(join(directory, "9.6.0.json"), "utf8"), + ); + expect( + await canaryRecordIssue({ + version: "9.6.0", + record, + expectedArtifact: record.artifact, + directory, + now: new Date("2030-01-01"), + freshness: "retained", + }), + ).toBeNull(); +}); + +async function collectionFixture() { + const directory = await realpath( + await mkdtemp(join(tmpdir(), "flow-native-collector-")), + ); + temporary.push(directory); + const exported = native(directory); + const sessions = [...exported.sessions]; + const client = { + server: { info: async () => exported.host }, + session: { + list: async () => ({ + data: sessions.map((session) => session.info), + cursor: {}, + }), + export: async ({ sessionID }: { sessionID: string }) => { + const found = sessions.find((session) => session.info.id === sessionID); + if (!found) throw new Error("Missing session"); + return found; + }, + active: async () => ({}), + }, + }; + return { directory, exported, sessions, client }; +} + +test("native collector retains all descendant reviewer exports and server facts", async () => { + const fixture = await collectionFixture(); + fixture.sessions.push({ + info: { + ...sessionInfo("ses_worker", fixture.directory, "ses_manager"), + agent: "flow-worker", + }, + messages: [], + }); + const result = await collectNativeCanary(fixture.client, { + directory: fixture.directory, + rootSessionID: "ses_manager", + }); + expect(result).toEqual({ ...fixture.exported, sessions: fixture.sessions }); + await writeFile( + join(fixture.directory, "native.json"), + JSON.stringify(result), + ); + expect( + JSON.parse( + await readFile(join(fixture.directory, "native.json"), "utf8"), + ).sessions.map((session: SessionTransferData) => session.info.id), + ).toEqual(["ses_manager", "ses_reviewer", "ses_worker"]); +}); + +test("native collector refuses wrong host, active sessions, pagination loops and changing trees", async () => { + const fixture = await collectionFixture(); + const input = { directory: fixture.directory, rootSessionID: "ses_manager" }; + await expect( + collectNativeCanary( + { + ...fixture.client, + server: { + info: async () => ({ ...fixture.exported.host, version: "2.1.5" }), + }, + }, + input, + ), + ).rejects.toThrow("pinned"); + await expect( + collectNativeCanary( + { + ...fixture.client, + session: { + ...fixture.client.session, + active: async () => ({ + ses_manager: { + type: "running", + agent: "build", + model: { providerID: "approved", id: "model" }, + messageID: "msg_manager", + started: 1, + }, + }), + }, + }, + input, + ), + ).rejects.toThrow("idle"); + await expect( + collectNativeCanary( + { + ...fixture.client, + session: { + ...fixture.client.session, + list: async () => ({ data: [], cursor: { next: "repeat" } }), + }, + }, + input, + ), + ).rejects.toThrow("converge"); + let lists = 0; + await expect( + collectNativeCanary( + { + ...fixture.client, + session: { + ...fixture.client.session, + list: async () => ({ + data: + lists++ === 0 + ? fixture.sessions.map((session) => session.info) + : [fixture.sessions[0]?.info].filter( + (info): info is SessionInfo => info !== undefined, + ), + cursor: {}, + }), + }, + }, + input, + ), + ).rejects.toThrow("tree changed"); +}); + +async function preparedFixture(directory: string) { + const { canonicalJson } = await import("../evals/canonical-json.js"); + const { + artifactIdentitySha256, + CANARY_CHECKLIST_VERSION, + CANARY_CHECKLIST_SHA256, + preparedCanarySha256, + } = await import("../scripts/eval-canary.js"); + const { mkdir } = await import("node:fs/promises"); + const sourcePackage = JSON.stringify({ + name: "opencode-plugin-flow", + version: "10.0.0", + dependencies: {}, + devDependencies: { "@opencode/plugin": "2.1.4" }, + }); + const pluginPackage = canonicalJson({ + name: "opencode-plugin-flow", + version: "10.0.0", + type: "module", + exports: { ".": "./index.js", "./tui": "./tui.js" }, + dependencies: { "@opencode/plugin": "2.1.4" }, + }); + const config = canonicalJson({ + plugins: [ + { + package: "./.opencode/plugins/flow", + options: { recovery: { mode: "off" } }, + }, + ], + update: "disable", + share: "disabled", + }); + await mkdir(join(directory, "package/dist"), { recursive: true }); + await mkdir(join(directory, "fixture/.opencode/plugins/flow"), { + recursive: true, + }); + for (const [path, bytes] of [ + ["package/package.json", sourcePackage], + ["package/dist/index.js", "plugin"], + ["package/dist/tui.js", "tui"], + ["fixture/.opencode/plugins/flow/index.js", "plugin"], + ["fixture/.opencode/plugins/flow/tui.js", "tui"], + ["fixture/.opencode/plugins/flow/package.json", pluginPackage], + ["fixture/opencode.json", config], + ] as const) + await writeFile(join(directory, path), bytes); + const packed = Bun.spawn( + ["tar", "-czf", join(directory, "artifact.tgz"), "package"], + { cwd: directory, stderr: "pipe" }, + ); + if (await packed.exited) + throw new Error(await new Response(packed.stderr).text()); + const archiveHash = `sha256:${createHash("sha256") + .update(await readFile(join(directory, "artifact.tgz"))) + .digest("hex")}`; + const artifact = { + packageVersion: "10.0.0", + sourceCommit: "fixture", + sourceTreeSha256: hash("source"), + tarballSha256: archiveHash, + unpackedManifestSha256: hash("manifest"), + }; + const base = { + schemaVersion: 2 as const, + releaseTag: "v10.0.0", + artifact, + artifactSha256: artifactIdentitySha256(artifact), + checklistVersion: + CANARY_CHECKLIST_VERSION as typeof CANARY_CHECKLIST_VERSION, + checklistSha256: CANARY_CHECKLIST_SHA256, + preparedAt: "2026-10-24T00:00:00Z", + artifactFile: "artifact.tgz" as const, + pluginEntrySha256: hash("plugin"), + runtime: { + hostVersion: "2.1.4" as const, + sourcePackageSha256: hash(sourcePackage), + tuiEntrySha256: hash("tui"), + pluginPackageSha256: hash(pluginPackage), + configSha256: hash(config), + }, + }; + const prepared = { ...base, sha256: preparedCanarySha256(base) }; + await writeFile(join(directory, "prepared.json"), JSON.stringify(prepared)); + return prepared; +} + +test("native canary recording redacts, reopens offline, rejects mutations and replays only exact bytes", async () => { + const { + recordCanary, + parseCanaryRecord, + canaryRecordSha256, + CANARY_V2_DERIVATION_VERSION, + } = await import("../scripts/eval-canary.js"); + const directory = await realpath( + await mkdtemp(join(tmpdir(), "flow-native-record-")), + ); + temporary.push(directory); + const prepared = await preparedFixture(directory); + const originalSession = flowSession(); + const id = "session:1234-abcd"; + const closure = originalSession.closure; + if (!closure) throw new Error("Missing closure"); + const session = { + ...originalSession, + id, + operations: originalSession.operations.map((operation) => + operation.id === closure.operationId + ? { + ...operation, + inputDigest: operationInputDigest({ + operationId: closure.operationId, + expectedRevision: closure.recordedRevision - 1, + sessionId: id, + kind: closure.kind, + summary: closure.summary, + }), + } + : operation, + ), + }; + + SessionSchema.parse(session); + const input = { + repositoryRoot: directory, + prepared, + preparedDirectory: directory, + operator: "fixture", + session, + transcript: native(join(directory, "fixture"), session), + recordedAt: new Date("2026-10-24T01:00:00Z"), + }; + const result = await recordCanary(input); + expect(result.record.status).toBe("passed"); + expect(result.record.derivationVersion).toBe(CANARY_V2_DERIVATION_VERSION); + expect((await recordCanary(input)).record).toEqual(result.record); + const verify = (record: unknown) => + canaryRecordIssue({ + version: "10.0.0", + record, + expectedArtifact: prepared.artifact, + directory: join(directory, "evals/canary"), + now: new Date("2026-10-24T02:00:00Z"), + }); + expect(await verify(result.record)).toBeNull(); + const transcript = result.record.artifacts.transcript; + if (!transcript) throw new Error("Missing native transcript reference"); + const retained = await readFile( + join(directory, "evals/canary", transcript.path), + "utf8", + ); + expect(retained).not.toContain(directory); + expect(retained).not.toContain("ses_manager"); + expect( + JSON.parse(retained).sessions.map( + (session: SessionTransferData) => session.info.location.directory, + ), + ).toEqual(["", ""]); + const { recordSha256: _recordHash, ...base } = result.record; + const altered = { ...base, derivationVersion: "canary-evidence-v1" as const }; + expect( + parseCanaryRecord({ ...altered, recordSha256: canaryRecordSha256(altered) }) + .ok, + ).toBe(false); + await expect( + recordCanary({ ...input, operator: "different" }), + ).rejects.toThrow("conflicts"); + await writeFile( + join(directory, "evals/canary", transcript.path), + retained.replace('"version":"2.1.4"', '"version":"2.1.5"'), + ); + expect(await verify(result.record)).toContain("digest or size"); + await writeFile( + join(directory, "fixture/.opencode/plugins/flow/tui.js"), + "mutated", + ); + await expect(recordCanary(input)).rejects.toThrow( + "installation bytes changed", + ); +}); + +for (const field of [ + "sessionId", + "operationId", + "expectedRevision", + "kind", + "summary", +] as const) + test(`native close rejects a different accepted request ${field}`, () => { + const value = native(); + const close = managerTool(value, "flow_session_close"); + const request = JSON.parse(JSON.stringify(close.input.request)); + request[field] = + field === "expectedRevision" + ? 0 + : field === "kind" + ? "deferred" + : "session:unrelated"; + close.input = { request }; + const result = derive(value); + expect(result.status).toBe("failed"); + expect(result.checks["closes-with-delivery"]).toBe(false); + }); + +for (const mutation of [ + "missing-operation", + "operation-id", + "operation-revision", + "operation-entity", + "operation-replayed", + "projection-session", + "projection-revision", + "projection-archive", + "delivery", + "generic-assurance", +] as const) + test(`native close rejects spliced response ${mutation}`, () => { + const value = native(); + const close = managerTool(value, "flow_session_close"); + const output = JSON.parse(JSON.stringify(close.content[0])); + const response = JSON.parse(output.text); + const workflow = response.workflowData; + switch (mutation) { + case "missing-operation": + delete workflow.operation; + break; + case "operation-id": + workflow.operation.operationId = "other-close"; + break; + case "operation-revision": + workflow.operation.revision--; + break; + case "operation-entity": + workflow.operation.entity.summary = "Another closure"; + break; + case "operation-replayed": + delete workflow.operation.replayed; + break; + case "projection-session": + workflow.projection.sessionId = "session:unrelated"; + break; + case "projection-revision": + workflow.projection.revision--; + break; + case "projection-archive": + workflow.projection.archived = false; + break; + case "delivery": + workflow.delivery.goal = "A different goal"; + break; + case "generic-assurance": + workflow.delivery = { assurance: workflow.delivery.assurance }; + break; + } + close.content = [{ type: "text", text: JSON.stringify(response) }]; + expect(derive(value).status).toBe("failed"); + }); + +test("native evidence cannot substitute another valid durable session behind unchanged assurance statuses", () => { + const original = flowSession(); + const closure = original.closure; + if (!closure) throw new Error("Missing closure"); + const other = SessionSchema.parse({ + ...original, + id: "session:unrelated", + operations: original.operations.map((operation) => + operation.id === closure.operationId + ? { + ...operation, + inputDigest: operationInputDigest({ + operationId: closure.operationId, + expectedRevision: closure.recordedRevision - 1, + sessionId: "session:unrelated", + kind: closure.kind, + summary: closure.summary, + }), + } + : operation, + ), + }); + expect(assuranceProjection(other)).toEqual(assuranceProjection(original)); + const result = derive(native(), other); + expect(result.status).toBe("failed"); + expect(result.checks["closes-with-delivery"]).toBe(false); +}); + +test("native reviewer completion binds the durable source and validation bytes, not only assurance labels", () => { + const original = flowSession(); + const swapped = SessionSchema.parse({ + ...original, + runs: original.runs.map((run) => ({ + ...run, + validations: run.validations.map((validation) => ({ + ...validation, + sourceDigest: hash("unrelated-source"), + outputDigest: hash("unrelated-output"), + })), + reviews: run.reviews.map((review) => ({ + ...review, + sourceDigest: hash("unrelated-source"), + })), + })), + }); + expect(assuranceProjection(swapped)).toEqual(assuranceProjection(original)); + const result = derive(native(), swapped); + expect(result.status).toBe("failed"); + expect(result.checks["captures-validation"]).toBe(false); + expect(result.checks["closes-with-delivery"]).toBe(false); +}); + +for (const mutation of [ + "request-assignment", + "request-operation", + "request-revision", + "request-summary", + "response-operation", + "response-revision", + "response-replay", + "response-session", + "response-validation", +] as const) + test(`native reviewer binding rejects ${mutation}`, () => { + const value = native(); + const message = value.sessions.find( + (session) => session.info.agent === "flow-reviewer", + )?.messages[0]; + if (message?.type !== "assistant") + throw new Error("Missing native reviewer"); + const part = message.content.find( + (part) => part.type === "tool" && part.name === "flow_feature_complete", + ); + if (part?.type !== "tool" || part.state.status !== "completed") + throw new Error("Missing completion"); + const request = JSON.parse(JSON.stringify(part.state.input.request)); + const content = part.state.content[0]; + if (content.type !== "text") throw new Error("Missing response"); + const response = JSON.parse(content.text); + switch (mutation) { + case "request-assignment": + request.assignmentId = "unrelated-review"; + break; + case "request-operation": + request.operationId = "unrelated-operation"; + break; + case "request-revision": + request.expectedRevision--; + break; + case "request-summary": + request.summary = "Other work"; + break; + case "response-operation": + response.workflowData.operation.operationId = "unrelated-operation"; + break; + case "response-revision": + response.workflowData.operation.revision--; + break; + case "response-replay": + response.workflowData.operation.replayed = true; + break; + case "response-session": + response.workflowData.projection.sessionId = "session:unrelated"; + break; + case "response-validation": + response.workflowData.operation.entity.validations[0].outputDigest = + hash("other-output"); + break; + } + part.state.input = { request }; + part.state.content = [{ type: "text", text: JSON.stringify(response) }]; + const result = derive(value); + expect(result.status).toBe("failed"); + expect(result.checks["captures-validation"]).toBe(false); + }); diff --git a/tests/config-shared.test.ts b/tests/config-shared.test.ts index 5a8100a8..31555d2f 100644 --- a/tests/config-shared.test.ts +++ b/tests/config-shared.test.ts @@ -1,12 +1,10 @@ import { describe, expect, test } from "bun:test"; import { - applyFlowConfig, createFlowCoreConfigEntries, FLOW_CORE_AGENTS, flowReviewerStatus, resolveFlowReviewerConfiguration, } from "../src/config-shared.js"; -import { createConfigHook } from "../src/platform/opencode/config.js"; const REVIEWER_STEPS_WARNING = "OPENCODE_FLOW_REVIEWER_STEPS must be an integer from 1 through 1000; ignoring it."; @@ -247,136 +245,54 @@ describe("Flow configuration", () => { expect(warnings).toEqual([]); }); - test("denies edit, bash, skill, and delegation for both hidden agents", () => { + test("uses ordered native permissions with only reviewer completion authority", () => { const entries = createFlowCoreConfigEntries({ env: {} }); - - expect(entries.agent["flow-reviewer"].permission).toEqual({ - edit: "deny", - bash: "deny", - external_directory: "deny", - skill: "deny", - task: { "*": "deny" }, - "flow_*": "deny", - flow_status: "allow", - flow_feature_complete: "allow", - }); - expect(entries.agent["flow-worker"].permission).toEqual({ - edit: { - "*": "allow", - ".flow": "deny", - ".flow/**": "deny", - ".git": "deny", - ".git/**": "deny", - }, - bash: "deny", - external_directory: "deny", - skill: "deny", - task: { "*": "deny" }, - "flow_*": "deny", - }); - }); - - test("gives every call its own deep copy of the deny lists", () => { - // The host owns the object it receives and may mutate it. Sharing any part - // of a deny list with the module constant would let one session's edit - // loosen a later session's permissions. - const first = createFlowCoreConfigEntries({ env: {} }); - const second = createFlowCoreConfigEntries({ env: {} }); - - for (const name of [ + function effect( + role: keyof typeof entries.agent, + action: string, + resource = "*", + ) { + return entries.agent[role].permissions + .filter( + (rule) => + (rule.action === "*" || rule.action === action) && + (rule.resource === "*" || + new Bun.Glob(rule.resource).match(resource)), + ) + .at(-1)?.effect; + } + expect(effect("flow-reviewer", "flow_status")).toBe("allow"); + expect(effect("flow-reviewer", "flow_feature_complete")).toBe("allow"); + for (const role of [ "flow-planner", "flow-reviewer", "flow-worker", ] as const) { - const permission = first.agent[name].permission; - const constant = FLOW_CORE_AGENTS[name].permission; - expect(permission, name).not.toBe(constant); - expect(permission, name).not.toBe(second.agent[name].permission); - expect(permission.task, name).not.toBe(constant.task); - expect(permission.task, name).not.toBe( - second.agent[name].permission.task, - ); + for (const action of ["shell", "subagent", "skill", "flow_plan_save"]) + expect(effect(role, action)).toBe("deny"); } - - const worker = first.agent["flow-worker"].permission.edit; - expect(worker).not.toBe(FLOW_CORE_AGENTS["flow-worker"].permission.edit); - expect(worker).not.toBe(second.agent["flow-worker"].permission.edit); - }); - - test("replaces Flow collisions while preserving unrelated entries", () => { - const unrelatedAgent = { description: "keep this agent" }; - const unrelatedCommand = { description: "keep this command" }; - const config = { - agent: { - "flow-reviewer": { description: "replace this agent" }, - "local-agent": unrelatedAgent, - }, - command: { - "flow-auto": { description: "replace this command" }, - "local-command": unrelatedCommand, - }, - }; - const collisions: Array<["agent" | "command", string]> = []; - - applyFlowConfig(config, { - onCollision: (kind, name) => collisions.push([kind, name]), - }); - - expect(collisions).toEqual([ - ["agent", "flow-reviewer"], - ["command", "flow-auto"], + expect(effect("flow-worker", "edit", "src/main.ts")).toBe("allow"); + expect(effect("flow-worker", "edit", ".flow/session.json")).toBe("deny"); + expect( + entries.agent["flow-worker"].permissions + .filter((rule) => rule.effect === "deny" && rule.action === "edit") + .map((rule) => rule.resource), + ).toEqual([ + ".flow", + ".flow/**", + "**/.flow", + "**/.flow/**", + ".git", + ".git/**", + "**/.git", + "**/.git/**", ]); - expect(config.agent["flow-reviewer"]).toMatchObject({ - mode: "subagent", - hidden: true, - }); - expect(config.command["flow-auto"]).toMatchObject({ subtask: false }); - expect(config.agent["local-agent"]).toBe(unrelatedAgent); - expect(config.command["local-command"]).toBe(unrelatedCommand); }); - - test("leaves configuration untouched and logs when the runtime guard fails", async () => { - const logs: unknown[] = []; - const localAgent = { description: "local agent" }; - const localCommand = { description: "local command" }; - const config = { - agent: { "local-agent": localAgent }, - command: { "local-command": localCommand }, - }; - const hook = createConfigHook( - { - client: { - app: { - log(input: unknown) { - logs.push(input); - }, - }, - }, - }, - { - assertOperational(action) { - expect(action).toBe("apply its OpenCode configuration"); - throw new Error("Flow test guard is closed."); - }, - }, - ); - - await hook(config); - - expect(config).toEqual({ - agent: { "local-agent": localAgent }, - command: { "local-command": localCommand }, - }); - expect(config.agent["local-agent"]).toBe(localAgent); - expect(config.command["local-command"]).toBe(localCommand); - expect(logs).toEqual([ - { - body: { - service: "opencode-plugin-flow", - level: "error", - message: "Flow test guard is closed.", - }, - }, - ]); + test("copies permission rules for each host registration", () => { + const first = createFlowCoreConfigEntries({ env: {} }); + const second = createFlowCoreConfigEntries({ env: {} }); + first.agent["flow-worker"].permissions.splice(0); + expect(second.agent["flow-worker"].permissions.length).toBe(13); + expect(FLOW_CORE_AGENTS["flow-worker"].permissions.length).toBe(13); }); }); diff --git a/tests/delivery-scenarios.test.ts b/tests/delivery-scenarios.test.ts index bb169876..af9b2004 100644 --- a/tests/delivery-scenarios.test.ts +++ b/tests/delivery-scenarios.test.ts @@ -73,7 +73,12 @@ function idle(input: ScenarioGradeInput): ScenarioGradeInput { (call) => call.tool === "flow_session_close", ); const trace = clone.hostTrace; - if (!close?.native || trace?.kind !== "observed") + if ( + !close?.native || + !("partId" in close.native) || + trace?.kind !== "observed" || + trace.version !== 1 + ) throw new Error("Missing native fixture."); const native = { ...close.native, @@ -334,7 +339,13 @@ test("an exact accepted close replay preserves full-detail access without new wo ); const replay = input.allCalls.at(-1); const trace = input.hostTrace; - if (!original || !replay?.native || trace?.kind !== "observed") + if ( + !original || + !replay?.native || + !("partId" in replay.native) || + trace?.kind !== "observed" || + trace.version !== 1 + ) throw new Error("Missing native calls."); const replayCall = { ...replay, @@ -349,7 +360,10 @@ test("an exact accepted close replay preserves full-detail access without new wo ); if (message?.role !== "assistant") throw new Error("Missing native message."); const tool = message.tools.find( - (item) => item.partId === replay.native?.partId, + (item) => + replay.native !== undefined && + "partId" in replay.native && + item.partId === replay.native.partId, ); if (!tool) throw new Error("Missing native part."); tool.tool = "flow_session_close"; diff --git a/tests/distribution-and-surface.test.ts b/tests/distribution-and-surface.test.ts index 59f1b1ec..7f10c541 100644 --- a/tests/distribution-and-surface.test.ts +++ b/tests/distribution-and-surface.test.ts @@ -10,7 +10,7 @@ import { } from "node:fs/promises"; import { homedir, tmpdir } from "node:os"; import { join } from "node:path"; -import type { ToolContext } from "@opencode-ai/plugin"; +import { z } from "zod"; import { createFlowCoreConfigEntries } from "../src/config-shared.js"; import { FLOW_GUIDANCE_IDS, @@ -19,7 +19,13 @@ import { } from "../src/guidance/catalog.js"; import FlowPlugin from "../src/index.js"; import { FLOW_AUTO_METADATA_KEY } from "../src/platform/opencode/auto-drive.js"; -import { createTools } from "../src/platform/opencode/tools.js"; +import { prepareFlowCommand } from "../src/platform/opencode/command-hook.js"; +import { + createNativeFlowHost, + type NativeFlowHost, + nativeText, + projectionOf, +} from "./fixtures/native-flow-host.js"; import { plan } from "./runtime-test-support.js"; const TOOL_NAMES = [ @@ -34,317 +40,135 @@ const TOOL_NAMES = [ "flow_session_close", "flow_status", "flow_validation_start", -] as const; - +]; const COMMAND_NAMES = [ "flow-auto", "flow-plan", "flow-review", "flow-run", "flow-status", -] as const; - -function createRegisteredTools() { - return createTools({ - validation: {} as never, - prepareValidation: async () => { - throw new Error("Validation execution is outside this structural test."); - }, - }); +]; +const hosts: NativeFlowHost[] = []; +const workspaces: string[] = []; +async function load(options: Parameters[1] = {}) { + const workspace = await mkdtemp(join(tmpdir(), "flow-native-surface-")); + workspaces.push(workspace); + const host = await createNativeFlowHost(workspace, options); + hosts.push(host); + await host.load(FlowPlugin); + return { host, workspace }; } - -function pluginContext( - workspace: string, - directory = workspace, - promptCalls?: unknown[], - logCalls?: unknown[], +function exactlyOnce(text: string, fragment: string) { + expect(text.split(fragment)).toHaveLength(2); +} +const statusInput = { request: { view: "compact" } }; +async function save( + host: NativeFlowHost, + hostID = "ses_manager", + goal = "Implement the runtime kernel.", + expectedRevision = 0, + nextPlan = plan, ) { - return { - client: { - app: { - log(input: unknown) { - logCalls?.push(input); - }, - }, - session: { - get(input: { path: { id: string } }) { - return Promise.resolve({ data: { id: input.path.id, directory } }); - }, - promptAsync(input: unknown) { - promptCalls?.push(input); - return Promise.resolve({ data: undefined }); - }, - messages() { - return Promise.resolve({ - data: [ - { - info: { - id: "host-compaction-user", - role: "user", - }, - parts: [], - }, - ], - }); - }, - message(input: unknown) { - const messageID = (input as { path: { messageID: string } }).path - .messageID; - const parentID = messageID.startsWith("assistant:") - ? messageID.slice("assistant:".length) - : null; - return Promise.resolve({ - data: parentID - ? { info: { role: "assistant", parentID } } - : undefined, - }); - }, + return host.response( + "flow_plan_save", + { + request: { + operationId: `save-${expectedRevision}`, + expectedRevision, + goal, + plan: nextPlan, }, }, - project: {}, - directory, - worktree: workspace, - experimental_workspace: { register() {} }, - serverUrl: new URL("http://localhost"), - $: {}, - } as unknown as Parameters[0]; -} - -function toolContext( - workspace: string, - sessionID = "surface-test-session", - parentMessageID?: string, -): ToolContext { - return { - sessionID, - messageID: parentMessageID - ? `assistant:${parentMessageID}` - : "surface-test-message", - agent: "build", - directory: workspace, - worktree: workspace, - abort: new AbortController().signal, - metadata() {}, - async ask() {}, - }; -} - -function expectExactlyOnce(text: string, fragment: string): void { - expect(text.split(fragment)).toHaveLength(2); -} - -type PluginHooks = Awaited>; -const activeHooks: PluginHooks[] = []; -const activeWorkspaces: string[] = []; - -async function createTestWorkspace(prefix: string): Promise { - const workspace = await mkdtemp(join(tmpdir(), prefix)); - activeWorkspaces.push(workspace); - return workspace; -} - -async function loadPlugin( - workspace: string, - directory = workspace, - promptCalls?: unknown[], - pluginOptions?: Readonly>, -): Promise { - const hooks = await FlowPlugin( - pluginContext(workspace, directory, promptCalls), - pluginOptions, + hostID, ); - activeHooks.push(hooks); - return hooks; -} -async function emitMessage( - hooks: PluginHooks, - info: Record, -): Promise { - await hooks.event?.({ - event: { type: "message.updated", properties: { info } }, - } as Parameters>[0]); } -async function emitAssistant( - hooks: PluginHooks, - sessionID: string, - parentID: string, - id = `assistant:${parentID}`, -): Promise { - await emitMessage(hooks, { id, sessionID, role: "assistant", parentID }); +async function approve( + host: NativeFlowHost, + revision: number, + hostID = "ses_manager", + operationId = "approve", +) { + return host.response( + "flow_plan_approve", + { request: { operationId, expectedRevision: revision } }, + hostID, + ); } -async function emitAutoCompaction( - hooks: PluginHooks, - sessionID: string, - authority: string, - successor: string, -): Promise { - const user = `${successor}-marker`; - await emitAssistant(hooks, sessionID, authority, `${successor}-trigger`); - await emitMessage(hooks, { id: user, sessionID, role: "user" }); - await hooks.event?.({ - event: { - type: "message.part.updated", - properties: { - part: { - id: `${user}-part`, - messageID: user, - sessionID, - type: "compaction", - auto: true, - }, - }, - }, - } as Parameters>[0]); - await emitMessage(hooks, { - id: `${successor}-summary`, - sessionID, - role: "assistant", - parentID: user, - summary: true, - }); - await emitMessage(hooks, { id: successor, sessionID, role: "user" }); - await hooks.event?.({ - event: { - type: "message.part.updated", - properties: { - part: { - id: `${successor}-continuation`, - messageID: successor, - sessionID, - type: "text", - text: "Continue the same task.", - synthetic: true, - metadata: { compaction_continue: true }, - }, - }, - }, - }); - await hooks.event?.({ - event: { type: "session.compacted", properties: { sessionID } }, - } as Parameters>[0]); +function field(value: unknown, name: string) { + return z.record(z.string(), z.unknown()).parse(value)[name]; } - afterEach(async () => { - const cleanupErrors: unknown[] = []; - for (const hooks of activeHooks.splice(0).reverse()) { + const errors: unknown[] = []; + for (const host of hosts.splice(0).reverse()) try { - await hooks.dispose?.(); + await host.dispose(); } catch (error) { - cleanupErrors.push(error); + errors.push(error); } - } - for (const workspace of activeWorkspaces.splice(0).reverse()) { + for (const workspace of workspaces.splice(0).reverse()) try { await rm(workspace, { recursive: true, force: true }); } catch (error) { - cleanupErrors.push(error); + errors.push(error); } - } - if (cleanupErrors.length > 0) { - throw new AggregateError(cleanupErrors, "Flow surface cleanup failed."); - } + if (errors.length) + throw new AggregateError(errors, "Native surface cleanup failed."); }); describe("Flow distribution surface", () => { - test("ships eleven tools, five commands, three hidden agent configs, and four guides", async () => { - expect(new Set(Object.keys(createRegisteredTools()))).toEqual( - new Set(TOOL_NAMES), - ); - - const config = createFlowCoreConfigEntries(); - expect(Object.keys(config.command).sort()).toEqual([...COMMAND_NAMES]); - expect(Object.keys(config.agent)).toEqual([ + test("registers eleven native tools, five commands, configured hidden roles, and four guides", async () => { + const { host } = await load({ + modelPreferences: { + planning: { + kind: "model", + model: { providerID: "test", id: "planning" }, + }, + }, + }); + expect(FlowPlugin.id).toBe("opencode-plugin-flow"); + expect([...host.tools.keys()].sort()).toEqual(TOOL_NAMES); + expect([...host.commands.keys()].sort()).toEqual(COMMAND_NAMES); + expect([...host.agents.keys()]).toEqual([ "flow-planner", "flow-reviewer", "flow-worker", ]); - for (const agent of Object.values(config.agent)) { - expect(agent.hidden).toBe(true); - } - + for (const agent of host.agents.values()) expect(agent.hidden).toBe(true); expect(FLOW_GUIDANCE_IDS).toEqual([ "flow", "flow-plan", "flow-run", "flow-review", ]); - - const workspace = await createTestWorkspace("flow-surface-"); - const hooks = await loadPlugin(workspace); - expect(Object.keys(hooks.tool ?? {}).sort()).toEqual([...TOOL_NAMES]); - const status = JSON.parse( - String( - await hooks.tool?.flow_status?.execute( - { request: { view: "compact" } }, - { - agent: "build", - directory: workspace, - worktree: workspace, - sessionID: "runtime-identity", - } as ToolContext, - ), - ), - ) as { workflowData: { runtimeIdentity: { pluginEntrySha256: string } } }; - const pluginBytes = await readFile( + const response = await host.response("flow_status", statusInput); + const entry = await readFile( join(import.meta.dir, "../src/platform/opencode/plugin.ts"), ); - expect(status.workflowData.runtimeIdentity.pluginEntrySha256).toBe( - `sha256:${createHash("sha256").update(pluginBytes).digest("hex")}`, - ); + expect(response.workflowData.runtimeIdentity).toEqual({ + packageVersion: "10.0.0", + pluginEntrySha256: `sha256:${createHash("sha256").update(entry).digest("hex")}`, + }); }); - - test("reports process-local reviewer configuration and deterministic status text", async () => { - const workspace = await createTestWorkspace("flow-config-status-"); - const pluginOptions = { - reviewer: { model: "provider/reviewer", steps: 80 }, - }; - const hooks = await loadPlugin( - workspace, - workspace, - undefined, - pluginOptions, - ); - pluginOptions.reviewer.model = "changed/after-startup"; - pluginOptions.reviewer.steps = 12; - const config: Parameters>[0] = {}; - await hooks.config?.(config); - expect(config.agent?.["flow-reviewer"]).toMatchObject({ - model: "provider/reviewer", + test("captures reviewer plugin options once and preserves deterministic status text", async () => { + const options = { reviewer: { model: "provider/reviewer", steps: 80 } }; + const { host } = await load(options); + options.reviewer.model = "changed/after-startup"; + options.reviewer.steps = 12; + await host.context.agent.reload(); + expect(host.agents.get("flow-reviewer")).toMatchObject({ + model: { providerID: "provider", id: "reviewer" }, steps: 80, }); - - const raw = String( - await hooks.tool?.flow_status?.execute( - { request: { view: "compact" } }, - toolContext(workspace, "configuration-status"), - ), - ); - const response = JSON.parse(raw) as { - workflowData: { - reviewerConfiguration: unknown; - statusReport: unknown; - }; - }; - - expect(response.workflowData.reviewerConfiguration).toEqual({ + const response = await host.response("flow_status", statusInput); + expect(response.workflowData.reviewerConfiguration).toMatchObject({ scope: "current-plugin-process", model: { kind: "explicit", source: "plugin-option", requested: "provider/reviewer", }, - steps: { - kind: "explicit", - source: "plugin-option", - requested: 80, - }, + steps: { requested: 80 }, availability: "unverified", - report: [ - "Reviewer selection: explicit.", - "Requested reviewer model: provider/reviewer (from plugin-option).", - "Requested reviewer step budget: 80 (from plugin-option).", - "Reviewer model availability: unverified; configuration proves only what Flow requested from OpenCode.", - ], }); expect(response.workflowData.statusReport).toEqual([ "View: compact", @@ -356,190 +180,85 @@ describe("Flow distribution surface", () => { "Findings digest: none", ]); }); - - test("isolates worker permissions while keeping manager and reviewer dispatch separate", () => { + test("isolates worker permissions and keeps native reviewer dispatch separate", async () => { const { agent, command } = createFlowCoreConfigEntries(); - const reviewer = agent["flow-reviewer"]; - const worker = agent["flow-worker"]; - expect(reviewer).toBeDefined(); - expect(reviewer?.hidden).toBe(true); - expect(reviewer?.permission).toEqual({ - edit: "deny", - bash: "deny", - external_directory: "deny", - skill: "deny", - task: { "*": "deny" }, - "flow_*": "deny", - flow_status: "allow", - flow_feature_complete: "allow", + expect(agent["flow-worker"]).toMatchObject({ + mode: "subagent", + hidden: true, }); - expect(worker).toBeDefined(); - expect(worker?.hidden).toBe(true); - expect(worker?.mode).toBe("subagent"); - expect(worker?.permission).toEqual({ - edit: { - "*": "allow", - ".flow": "deny", - ".flow/**": "deny", - ".git": "deny", - ".git/**": "deny", - }, - bash: "deny", - external_directory: "deny", - skill: "deny", - task: { "*": "deny" }, - "flow_*": "deny", + expect(agent["flow-worker"]).not.toHaveProperty("model"); + expect(agent["flow-worker"]).not.toHaveProperty("steps"); + expect(agent["flow-reviewer"].permissions).toContainEqual({ + action: "flow_feature_complete", + resource: "*", + effect: "allow", + }); + expect(agent["flow-worker"].permissions).toContainEqual({ + action: "edit", + resource: "**/.git/**", + effect: "deny", }); - expect(worker).not.toHaveProperty("model"); - expect(worker).not.toHaveProperty("steps"); - for (const name of [ "flow-auto", "flow-plan", "flow-run", "flow-status", - ] as const) { + ] as const) expect(command[name]?.subtask).toBe(false); - expect("agent" in (command[name] ?? {})).toBe(false); - } - expect(command["flow-review"]).toMatchObject({ - subtask: true, + const { host } = await load({ reviewer: { model: "test/reviewer" } }); + await host.command("flow-review", "assignment-1"); + const child = [...host.sessions.values()].find( + (session) => session.parentID === "ses_manager", + ); + expect(child).toMatchObject({ agent: "flow-reviewer", + model: { providerID: "test", id: "reviewer" }, + }); + expect(host.prompts.at(-1)).toMatchObject({ + sessionID: child?.id, + text: expect.stringContaining("Assignment: assignment-1"), }); - expect( - Object.values(command).some( - (entry) => "agent" in entry && entry.agent === "flow-worker", - ), - ).toBe(false); }); - - test("refuses to load from the home directory and says so", async () => { - const home = homedir(); - const logCalls: unknown[] = []; - await expect( - FlowPlugin(pluginContext(home, home, undefined, logCalls)), - ).rejects.toThrow( - "Flow refuses to use the home directory itself as mutable state.", + test("refuses to load the home directory before registering native tools", async () => { + const host = await createNativeFlowHost(homedir()); + hosts.push(host); + await expect(host.load(FlowPlugin)).rejects.toThrow( + "Flow refuses to use the home directory itself", ); - expect(logCalls).toHaveLength(1); - expect( - String((logCalls[0] as { body: { message: string } }).body.message), - ).toContain("cannot start here"); + expect(host.tools.size).toBe(0); }); }); -describe("command preflight", () => { - test("rewrites manager text and the exact reviewer subtask", async () => { - const workspace = await createTestWorkspace("flow-command-"); - const hooks = await loadPlugin(workspace); - const before = hooks["command.execute.before"]; - if (!before) throw new Error("Missing command preflight hook."); - - const rawArguments = "\t simplify it\n exactly \t\n"; - const managerOutput = { - parts: [{ type: "text", text: "stale prompt" }], - } as unknown as Parameters[1]; - await before( - { command: "/flow-plan", sessionID: "s", arguments: rawArguments }, - managerOutput, - ); - expect(managerOutput.parts).toHaveLength(2); - expect(managerOutput.parts[0]).toMatchObject({ - type: "text", - text: `Flow flow-plan: ${rawArguments}`, - }); - expect(managerOutput.parts[1]).toMatchObject({ - type: "text", - synthetic: true, - }); - expect((managerOutput.parts[1] as { text?: string }).text).toContain( - "# Flow plan router", - ); - expect((managerOutput.parts[1] as { text?: string }).text).toContain( - "the preceding non-synthetic Flow request", - ); - expectExactlyOnce( - managerOutput.parts - .map((part) => (part.type === "text" ? part.text : "")) - .join("\n"), - rawArguments, - ); - - const emptyOutput = { - parts: [{ type: "text", text: "stale prompt" }], - } as unknown as Parameters[1]; - await before( - { command: "/flow-status", sessionID: "s", arguments: " \t\n" }, - emptyOutput, - ); - expect(emptyOutput.parts[0]).toMatchObject({ - type: "text", - text: "Flow flow-status", +describe("native command preparation", () => { + test("preserves raw manager arguments exactly once and targets the reviewer assignment", async () => { + const { host } = await load(); + const raw = "\t simplify it\n exactly \t\n"; + const command = await host.command("flow-plan", raw); + expect(command?.text).toStartWith(`Flow flow-plan: ${raw}\n\n`); + exactlyOnce(command?.text ?? "", raw); + expect(command?.text).toContain("# Flow plan router"); + const empty = await host.command("flow-status", " \t\n"); + expect(empty?.text).toStartWith("Flow flow-status\n\n"); + await host.command("flow-review", "assignment-1"); + expect(host.prompts.at(-1)?.text).toContain("Assignment: assignment-1"); + }); + test("rejects native command dispatch from child sessions and malformed recovery options", async () => { + const { host } = await load(); + host.ensureSession("ses_child", { + parentID: "ses_parent", + agent: "flow-reviewer", }); - - const reviewerOutput = { - parts: [ - { - type: "subtask", - agent: "flow-reviewer", - command: "flow-review", - prompt: "stale prompt", - description: "review", - }, - ], - } as unknown as Parameters[1]; - await before( - { command: "flow-review", sessionID: "s", arguments: "assignment-1" }, - reviewerOutput, - ); - expect((reviewerOutput.parts[0] as { prompt?: string }).prompt).toContain( - "Assignment: assignment-1", + await expect(host.command("flow-run", "", "ses_child")).rejects.toThrow( + "primary manager", ); - }); - - test("fails closed for mixed manager or malformed reviewer dispatch", async () => { - const workspace = await createTestWorkspace("flow-command-fail-"); - const hooks = await loadPlugin(workspace); - const before = hooks["command.execute.before"]; - if (!before) throw new Error("Missing command preflight hook."); - - await expect( - before({ command: "flow-run", sessionID: "s", arguments: "" }, { - parts: [ - { - type: "subtask", - agent: "flow-reviewer", - command: "flow-run", - prompt: "", - description: "invalid", - }, - ], - } as unknown as Parameters[1]), - ).rejects.toThrow("manager commands cannot contain subtask parts"); - - for (const parts of [ - [], - [{ type: "text", text: "not a subtask" }], - [ - { - type: "subtask", - agent: "other-reviewer", - command: "flow-review", - prompt: "", - description: "invalid", - }, - ], - ] as const) { - await expect( - before({ command: "flow-review", sessionID: "s", arguments: "" }, { - parts: [...parts], - } as unknown as Parameters[1]), - ).rejects.toThrow(); - } + expect(() => + prepareFlowCommand("flow-auto", "--recovery=delegated Repair"), + ).toThrow("Recovery requires"); + expect(host.prompts).toEqual([]); }); }); -describe("flow-auto host continuation", () => { +describe("flow-auto native continuation", () => { for (const [kind, goal, expectedApproval] of [ [ "new-anchor", @@ -627,52 +346,21 @@ describe("flow-auto host continuation", () => { ], ] as const) { test(`inspect approval for ${kind}: ${goal}`, async () => { - const workspace = await createTestWorkspace("flow-auto-inspect-anchor-"); - const prompts: unknown[] = []; - const hooks = await loadPlugin(workspace, workspace, prompts); - const before = hooks["command.execute.before"]; - const chat = hooks["chat.message"]; - const save = hooks.tool?.flow_plan_save; - const status = hooks.tool?.flow_status; - if (!before || !chat || !save || !status || !hooks.event) - throw new Error("Missing Flow hooks."); - const output = () => - ({ parts: [{ type: "text", text: "stale" }] }) as unknown as Parameters< - typeof before - >[1]; + const { host } = await load(); + const manager = "ses_inspect"; if (kind === "preexisting-anchor") - await before( - { command: "flow-plan", sessionID: "inspect-host", arguments: goal }, - output(), - ); - const commandOutput = output(); - await before( - { command: "flow-auto", sessionID: "inspect-host", arguments: goal }, - commandOutput, + await host.command("flow-plan", goal, manager); + await host.command("flow-auto", `--recovery=off ${goal}`, manager); + const provisional = await host.response( + "flow_status", + statusInput, + manager, ); - const provisional = JSON.parse( - String( - await status.execute( - { request: { view: "compact" } }, - toolContext(workspace, "inspect-host"), - ), - ), - ); - expect(provisional.workflowData.projection).toMatchObject({ + expect(projectionOf(provisional)).toMatchObject({ status: "planning", revision: 0, goal, }); - const provisionalId = provisional.workflowData.projection.sessionId; - await chat({ sessionID: "inspect-host" }, { - message: { - id: "inspect-command", - agent: "build", - model: { providerID: "provider", modelID: "model" }, - }, - parts: commandOutput.parts, - } as unknown as Parameters[1]); - await emitAssistant(hooks, "inspect-host", "inspect-command"); const inspectPlan = { ...plan, features: plan.features.map((feature) => ({ @@ -680,1288 +368,417 @@ describe("flow-auto host continuation", () => { kind: "inspect" as const, })), }; - const saved = JSON.parse( - String( - await save.execute( - { - request: { - operationId: `inspect-save-${kind}`, - expectedRevision: 0, - goal, - plan: inspectPlan, - }, - }, - toolContext(workspace, "inspect-host", "inspect-command"), - ), - ), - ); + const saved = await save(host, manager, goal, 0, inspectPlan); expect(saved.status).toBe("ok"); - expect(saved.workflowData.projection.sessionId).toBe(provisionalId); - await hooks.event({ - event: { - type: "session.idle", - properties: { sessionID: "inspect-host" }, - }, - } as Parameters>[0]); - const approvalPrompts = prompts.filter((call) => - JSON.stringify(call).includes("Approve this one draft"), + expect(projectionOf(saved).sessionId).toBe( + projectionOf(provisional).sessionId, ); - expect(approvalPrompts.length, kind).toBe(Number(expectedApproval)); + await host.idle(manager); + expect( + host.synthetics.filter((input) => + input.text.includes("Approve this one draft"), + ), + ).toHaveLength(Number(expectedApproval)); }); } - test("binds an explicitly named acceptance case to its originating host", async () => { - const workspace = await createTestWorkspace("flow-request-anchor-"); - const hooks = await loadPlugin(workspace); - const before = hooks["command.execute.before"]; - const planSave = hooks.tool?.flow_plan_save; - const planApprove = hooks.tool?.flow_plan_approve; - if (!before || !planSave || !planApprove) - throw new Error("Missing request-anchor hooks."); - const output = { - parts: [{ type: "text", text: "stale" }], - } as unknown as Parameters[1]; - await before( - { - command: "flow-auto", - sessionID: "anchor-owner", - arguments: - "Implement this; acceptance has a case named `linux-skipped observation`.", - }, - output, + test("binds a named acceptance case to the originating native host", async () => { + const { host } = await load(); + const owner = "ses_owner"; + await host.command( + "flow-auto", + "--recovery=off Implement this; acceptance has a case named `linux-skipped observation`.", + owner, ); - const save = ( - operationId: string, - expectedRevision: number, - nextPlan = plan, - ) => - planSave.execute( - { - request: { - operationId, - expectedRevision, - goal: "Implement the named case.", - plan: nextPlan, - }, - }, - toolContext(workspace, "anchor-owner"), - ); - const saved = JSON.parse(String(await save("anchor-save-1", 0))); + const saved = await save(host, owner, "Implement the named case."); expect(saved.status).toBe("ok"); - const approval = { - request: { operationId: "anchor-approve-1", expectedRevision: 1 }, - }; expect( - JSON.parse( - String( - await planApprove.execute( - approval, - toolContext(workspace, "other-host"), - ), - ), - ).summary, + (await approve(host, 1, "ses_other", "other-approve")).summary, ).toContain("originating OpenCode session"); - expect( - JSON.parse( - String( - await planApprove.execute( - approval, - toolContext(workspace, "anchor-owner"), - ), - ), - ).summary, - ).toContain("linux-skipped observation"); + expect((await approve(host, 1, owner)).summary).toContain( + "linux-skipped observation", + ); const repairedPlan = { ...plan, evidence: (plan.evidence ?? []).map((entry, index) => - index === 0 - ? { + index + ? entry + : { ...entry, command: "bun test --reporter=junit --reporter-outfile=.flow/results.xml", assertions: ["linux-skipped observation"], - } - : entry, - ), - }; - const repaired = JSON.parse( - String(await save("anchor-save-2", 1, repairedPlan)), - ); - expect(repaired.status).toBe("ok"); - const approved = JSON.parse( - String( - await planApprove.execute( - { - request: { - operationId: "anchor-approve-2", - expectedRevision: 2, }, - }, - toolContext(workspace, "anchor-owner"), - ), ), + }; + expect( + (await save(host, owner, "Implement the named case.", 1, repairedPlan)) + .status, + ).toBe("ok"); + expect((await approve(host, 2, owner, "approve-repaired")).status).toBe( + "ok", ); - expect(approved.status).toBe("ok"); }); - - test("keeps malformed mutation output out of validation-capture errors", async () => { - const workspace = await createTestWorkspace("flow-auto-output-"); - const hooks = await loadPlugin(workspace); - const toolAfter = hooks["tool.execute.after"]; - if (!toolAfter) throw new Error("Missing tool after-hook."); - const output = { title: "", output: "not-json", metadata: {} }; - - await toolAfter( - { - tool: "flow_plan_approve", - sessionID: "auto-host", - callID: "malformed-mutation-output", - args: {}, - }, - output, - ); - - expect(output.output).toBe("not-json"); - expect(output.output).not.toContain("[flow-validation-error]"); + test("leaves non-Flow malformed after-hook content untouched", async () => { + const { host } = await load(); + const native = await host.toolContext("flow_plan_approve", {}); + const result = { content: "not-json", metadata: {} }; + await host.toolHooks.emit("execute.after", { + tool: "flow_plan_approve", + sessionID: native.sessionID, + messageID: native.messageID, + agent: native.agent, + id: native.id, + input: {}, + status: "completed", + result, + }); + expect(result.content).toBe("not-json"); }); - - test("reports local stop or cancel without durable mutation or cross-host revocation", async () => { - const workspace = await createTestWorkspace("flow-auto-stop-"); - const hooks = await loadPlugin(workspace); - const before = hooks["command.execute.before"]; - const chat = hooks["chat.message"]; - const compacting = hooks["experimental.session.compacting"]; - const planSave = hooks.tool?.flow_plan_save; - const status = hooks.tool?.flow_status; - if (!before || !chat || !compacting || !planSave || !status) { - throw new Error("Missing auto-drive hooks."); - } - const saved = JSON.parse( - String( - await planSave.execute( - { - request: { - operationId: "stop-plan-save", - expectedRevision: 0, - goal: "Keep durable state unchanged while stopping auto-drive.", - plan, - }, - }, - toolContext(workspace), - ), - ), + test("stop and cancel preserve durable state and do not revoke another host", async () => { + const { host, workspace } = await load(); + const saved = await save(host); + const original = await readFile( + join(workspace, ".flow/session.json"), + "utf8", ); - const durableBefore = { - sessionId: saved.workflowData.projection.sessionId, - status: saved.workflowData.projection.status, - revision: saved.workflowData.projection.revision, - }; - expect(durableBefore).toMatchObject({ - sessionId: expect.any(String), - status: "planning", - revision: 1, - }); - for (const directive of ["stop", "cancel"]) { - const activation = { - parts: [{ type: "text", text: "stale" }], - } as unknown as Parameters[1]; - await before( - { command: "flow-auto", sessionID: "auto-stop", arguments: "" }, - activation, - ); - const activeContext = { context: [] as string[] }; - await compacting({ sessionID: "auto-stop" }, activeContext); - expect(activeContext.context).not.toEqual([]); - - const stopped = { - parts: [{ type: "text", text: "stale" }], - } as unknown as Parameters[1]; - const retainedParts = stopped.parts; - await before( - { - command: "flow-auto", - sessionID: "auto-stop", - arguments: directive, - }, - stopped, - ); - expect(stopped.parts).toBe(retainedParts); - expect(stopped.parts).toHaveLength(1); - const stoppedText = stopped.parts[0]; - expect(stoppedText?.type).toBe("text"); - if (stoppedText?.type !== "text") { - throw new Error("Expected a plain stop confirmation."); - } - expect(stoppedText.text).toBe("Flow auto stopped."); - expect(stoppedText.synthetic).not.toBe(true); - const stoppedContext = { context: [] as string[] }; - await compacting({ sessionID: "auto-stop" }, stoppedContext); - expect(stoppedContext.context).toEqual([]); + await host.command("flow-auto", "--recovery=off Continue", "ses_owner"); + await host.command("flow-auto", directive, "ses_owner"); + expect(host.prompts.at(-1)).toMatchObject({ + text: "/flow-auto stop", + resume: false, + }); + await host.idle("ses_owner"); + expect(host.synthetics).toEqual([]); + expect( + await readFile(join(workspace, ".flow/session.json"), "utf8"), + ).toBe(original); } - - const alreadyStopped = { - parts: [{ type: "text", text: "stale" }], - } as unknown as Parameters[1]; - await before( - { command: "flow-auto", sessionID: "auto-stop", arguments: "stop" }, - alreadyStopped, - ); - expect(alreadyStopped.parts[0]).toMatchObject({ - type: "text", - text: "No Flow auto lease was active in this OpenCode session.", - }); - - const response = JSON.parse( - String( - await status.execute( - { request: { view: "compact" } }, - toolContext(workspace), - ), - ), - ); - expect(response.workflowData.projection).toMatchObject(durableBefore); - - const ownerActivation = { - parts: [{ type: "text", text: "stale" }], - } as unknown as Parameters[1]; - await before( - { command: "flow-auto", sessionID: "auto-owner", arguments: "" }, - ownerActivation, - ); - const otherHostStop = { - parts: [{ type: "text", text: "stale" }], - } as unknown as Parameters[1]; - const otherHostParts = otherHostStop.parts; - await before( - { command: "flow-auto", sessionID: "other-host", arguments: "stop" }, - otherHostStop, - ); - expect(otherHostStop.parts).toBe(otherHostParts); - expect(otherHostStop.parts[0]).toMatchObject({ - type: "text", - text: "No Flow auto lease was active in this OpenCode session.", - }); - const afterOtherHost = { context: [] as string[] }; - await compacting({ sessionID: "auto-owner" }, afterOtherHost); - expect(afterOtherHost.context).not.toEqual([]); - await chat({ sessionID: "other-host" }, { - message: { - id: "other-host-natural-stop", - agent: "build", - model: { providerID: "provider", modelID: "model" }, - }, - parts: [{ type: "text", text: "stop /flow-auto" }], - } as unknown as Parameters[1]); - const afterOtherHostMessage = { context: [] as string[] }; - await compacting({ sessionID: "auto-owner" }, afterOtherHostMessage); - expect(afterOtherHostMessage.context).not.toEqual([]); - - await mkdir(join(workspace, ".flow"), { recursive: true }); - await writeFile(join(workspace, ".flow", "session.json"), "{broken"); - const unreadableStop = { - parts: [{ type: "text", text: "stale" }], - } as unknown as Parameters[1]; + await host.command("flow-auto", "--recovery=off Continue", "ses_owner"); + await host.command("flow-auto", "stop", "ses_other"); + await host.admit("stop /flow-auto", "ses_other"); + const stoppedOther = await host.response( + "flow_status", + statusInput, + "ses_other", + ); + expect(stoppedOther.workflowData.autoDrive).toBeUndefined(); + const owner = await host.response("flow_status", statusInput, "ses_owner"); + expect(owner.workflowData.autoDrive).toMatchObject({ state: "active" }); + expect(projectionOf(owner).sessionId).toBe(projectionOf(saved).sessionId); + await writeFile(join(workspace, ".flow/session.json"), "{broken"); await expect( - before( - { command: "flow-auto", sessionID: "auto-owner", arguments: "stop" }, - unreadableStop, - ), - ).resolves.toBeUndefined(); - const afterUnreadableStop = { context: [] as string[] }; - await compacting({ sessionID: "auto-owner" }, afterUnreadableStop); - expect(afterUnreadableStop.context).toEqual([]); + host.command("flow-auto", "stop", "ses_owner"), + ).resolves.toMatchObject({ text: "/flow-auto stop", resume: false }); }); - - test("adopts an idle-created session only from its same-host plan save", async () => { - for (const [creatorHost, expectedPrompts] of [ - ["auto-owner", 1], - ["other-host", 0], - ] as const) { - const workspace = await createTestWorkspace("flow-auto-idle-owner-"); - const promptCalls: unknown[] = []; - const hooks = await loadPlugin(workspace, workspace, promptCalls); - const planSave = hooks.tool?.flow_plan_save; - const planApprove = hooks.tool?.flow_plan_approve; - const before = hooks["command.execute.before"]; - const chat = hooks["chat.message"]; - const compacting = hooks["experimental.session.compacting"]; - if ( - !planSave || - !planApprove || - !before || - !chat || - !compacting || - !hooks.event - ) - throw new Error("Missing auto-drive hooks or plan tools."); - const creatorContext = toolContext( - workspace, - creatorHost, - "idle-owner-command", - ); - const ownerContext = toolContext( - workspace, - "auto-owner", - "idle-owner-command", - ); - const commandOutput = { - parts: [{ type: "text", text: "stale" }], - } as unknown as Parameters[1]; - await before( - { command: "flow-auto", sessionID: "auto-owner", arguments: "" }, - commandOutput, - ); - await chat({ sessionID: "auto-owner" }, { - message: { - id: "idle-owner-command", - agent: "build", - model: { providerID: "provider", modelID: "model" }, - }, - parts: commandOutput.parts, - } as unknown as Parameters[1]); - await emitAssistant(hooks, creatorHost, "idle-owner-command"); - const saveArgs = { - request: { - operationId: "idle-owner-plan-save", - expectedRevision: 0, - goal: "Bind idle continuation ownership.", - plan, - }, - }; - const savedOutput = String( - await planSave.execute(saveArgs, creatorContext), - ); - const saved = JSON.parse(savedOutput); - const approveArgs = { - request: { - operationId: "idle-owner-plan-approve", - expectedRevision: saved.workflowData.projection.revision, - }, - }; - const approvedOutput = String( - await planApprove.execute(approveArgs, ownerContext), + test.each([ + ["ses_owner", 1], + ["ses_other", 0], + ] as const)( + "an idle-created workflow is adopted only from same-host save by %s", + async (creator, expected) => { + const { host } = await load(); + await host.command("flow-auto", "--recovery=off", "ses_owner"); + const saved = await save( + host, + creator, + "Bind idle continuation ownership.", ); - expect(JSON.parse(approvedOutput).status).toBe("ok"); - await hooks.event({ - event: { - type: "session.idle", - properties: { sessionID: "auto-owner" }, - }, - } as Parameters>[0]); - - expect(promptCalls).toHaveLength(expectedPrompts); - const compacted = { context: [] as string[] }; - await compacting({ sessionID: "auto-owner" }, compacted); - expect(compacted.context.length > 0).toBe(expectedPrompts === 1); - } + expect(saved.status).toBe("ok"); + expect((await approve(host, 1, "ses_owner")).status).toBe("ok"); + await host.idle("ses_owner"); + expect(host.synthetics).toHaveLength(expected); + }, + ); + test("another host's accepted mutation and its exact owner replay cannot grant automatic authority", async () => { + const { host } = await load(); + await save(host); + await host.command("flow-auto", "--recovery=off", "ses_owner"); + await host.response("flow_status", statusInput, "ses_owner"); + await host.idle("ses_owner"); + await host.admit("Approve the plan.", "ses_owner"); + await host.beginTurn("ses_owner"); + expect((await approve(host, 1, "ses_other")).status).toBe("ok"); + const replay = await approve(host, 1, "ses_owner"); + expect(replay.workflowData.operation?.replayed).toBe(true); + await host.idle("ses_owner"); + expect(host.synthetics).toEqual([]); }); - - test("does not treat another host's accepted mutation as continuation authority", async () => { - const workspace = await createTestWorkspace("flow-auto-other-host-"); - const promptCalls: unknown[] = []; - const hooks = await loadPlugin(workspace, workspace, promptCalls); - const planSave = hooks.tool?.flow_plan_save; - const planApprove = hooks.tool?.flow_plan_approve; - const before = hooks["command.execute.before"]; - const chat = hooks["chat.message"]; - const compacting = hooks["experimental.session.compacting"]; - if ( - !planSave || - !planApprove || - !before || - !chat || - !compacting || - !hooks.event - ) { - throw new Error("Missing auto-drive hooks or plan tools."); - } - const context = toolContext(workspace); - const saved = JSON.parse( - String( - await planSave.execute( - { - request: { - operationId: "other-host-plan-save", - expectedRevision: 0, - goal: "Reject cross-host continuation authority.", - plan, - }, - }, - context, - ), - ), - ); - const commandOutput = { - parts: [{ type: "text", text: "stale" }], - } as unknown as Parameters[1]; - await before( - { command: "flow-auto", sessionID: "auto-owner", arguments: "" }, - commandOutput, - ); - await chat({ sessionID: "auto-owner" }, { - message: { - id: "auto-close-command", - agent: "build", - model: { providerID: "provider", modelID: "model" }, - }, - parts: commandOutput.parts, - } as unknown as Parameters[1]); - await hooks.event({ - event: { - type: "session.idle", - properties: { sessionID: "auto-owner" }, - }, - } as Parameters>[0]); - await chat({ sessionID: "auto-owner" }, { - message: { - id: "auto-owner-reply", - agent: "build-approved", - model: { providerID: "provider", modelID: "approved-model" }, - }, - parts: [{ type: "text", text: "Approve the plan." }], - } as unknown as Parameters[1]); - - const approveArgs = { - request: { - operationId: "other-host-plan-approve", - expectedRevision: saved.workflowData.projection.revision, - }, - }; - const approvedOutput = String( - await planApprove.execute( - approveArgs, - toolContext(workspace, "other-host"), - ), - ); - expect(JSON.parse(approvedOutput).status).toBe("ok"); - const afterCrossHostMutation = { context: [] as string[] }; - await compacting({ sessionID: "auto-owner" }, afterCrossHostMutation); - expect(afterCrossHostMutation.context).not.toEqual([]); - await emitAssistant(hooks, "auto-owner", "auto-owner-reply"); - const replayedOutput = String( - await planApprove.execute( - approveArgs, - toolContext(workspace, "auto-owner", "auto-owner-reply"), + test("missing native request identity rejects a mutation before durable effect", async () => { + const { host, workspace } = await load(); + await save(host); + const before = await readFile( + join(workspace, ".flow/session.json"), + "utf8", + ); + const native = await host.toolContext("flow_plan_approve", { + request: { operationId: "unknown-approve", expectedRevision: 1 }, + }); + host.messages.set("ses_manager", []); + await expect( + host.invoke( + "flow_plan_approve", + { request: { operationId: "unknown-approve", expectedRevision: 1 } }, + native, ), + ).rejects.toThrow("assistant identity"); + expect(await readFile(join(workspace, ".flow/session.json"), "utf8")).toBe( + before, ); - expect(JSON.parse(replayedOutput).workflowData.operation.replayed).toBe( - true, - ); - const afterReplay = { context: [] as string[] }; - await compacting({ sessionID: "auto-owner" }, afterReplay); - expect(afterReplay.context).not.toEqual([]); - await hooks.event({ - event: { - type: "session.idle", - properties: { sessionID: "auto-owner" }, - }, - } as Parameters>[0]); - - expect(promptCalls).toHaveLength(0); - const afterOtherHostMutation = { context: [] as string[] }; - await compacting({ sessionID: "auto-owner" }, afterOtherHostMutation); - expect(afterOtherHostMutation.context).toEqual([]); }); - - test("preserves an accepted mutation but fails closed without its assistant origin", async () => { - const workspace = await createTestWorkspace("flow-auto-missing-origin-"); - const promptCalls: unknown[] = []; - const hooks = await loadPlugin(workspace, workspace, promptCalls); - const planSave = hooks.tool?.flow_plan_save; - const planApprove = hooks.tool?.flow_plan_approve; - const before = hooks["command.execute.before"]; - const chat = hooks["chat.message"]; - const compacting = hooks["experimental.session.compacting"]; - if ( - !planSave || - !planApprove || - !before || - !chat || - !compacting || - !hooks.event - ) { - throw new Error("Missing auto-drive hooks or plan tools."); - } - const saved = JSON.parse( - String( - await planSave.execute( - { - request: { - operationId: "missing-origin-plan-save", - expectedRevision: 0, - goal: "Fail closed when mutation provenance is unavailable.", - plan, - }, - }, - toolContext(workspace), - ), - ), + test("a same-host assistant from an older native request cannot mutate after real steer", async () => { + const { host, workspace } = await load(); + await save(host); + const args = { + request: { operationId: "stale-approve", expectedRevision: 1 }, + }; + const old = await host.toolContext("flow_plan_approve", args); + const before = await readFile( + join(workspace, ".flow/session.json"), + "utf8", ); - const commandOutput = { - parts: [{ type: "text", text: "stale" }], - } as unknown as Parameters[1]; - await before( - { command: "flow-auto", sessionID: "auto-host", arguments: "" }, - commandOutput, + await host.admit("Approve the plan."); + await host.beginTurn(); + await expect(host.invoke("flow_plan_approve", args, old)).rejects.toThrow(); + expect(await readFile(join(workspace, ".flow/session.json"), "utf8")).toBe( + before, ); - await chat({ sessionID: "auto-host" }, { - message: { - id: "missing-origin-command", - agent: "build", - model: { providerID: "provider", modelID: "model" }, - }, - parts: commandOutput.parts, - } as unknown as Parameters[1]); - await chat({ sessionID: "auto-host" }, { - message: { - id: "missing-origin-reply", - agent: "build-approved", - model: { providerID: "provider", modelID: "approved-model" }, - }, - parts: [{ type: "text", text: "Approve the plan." }], - } as unknown as Parameters[1]); - - const approvedOutput = String( - await planApprove.execute( - { - request: { - operationId: "missing-origin-plan-approve", - expectedRevision: saved.workflowData.projection.revision, - }, - }, - toolContext(workspace, "auto-host", "missing-origin-reply"), - ), - ); - expect(JSON.parse(approvedOutput)).toMatchObject({ - status: "ok", - workflowData: { - operation: { replayed: false }, - projection: { status: "ready" }, - }, - }); - await hooks.event({ - event: { - type: "session.idle", - properties: { sessionID: "auto-host" }, - }, - } as Parameters>[0]); - - expect(promptCalls).toHaveLength(0); - const afterMissingOrigin = { context: [] as string[] }; - await compacting({ sessionID: "auto-host" }, afterMissingOrigin); - expect(afterMissingOrigin.context).toEqual([]); }); - - test("does not credit a same-host assistant from an older user turn", async () => { - const workspace = await createTestWorkspace("flow-auto-stale-origin-"); - const promptCalls: unknown[] = []; - const hooks = await loadPlugin(workspace, workspace, promptCalls); - const planSave = hooks.tool?.flow_plan_save; - const planApprove = hooks.tool?.flow_plan_approve; - const before = hooks["command.execute.before"]; - const chat = hooks["chat.message"]; - const compacting = hooks["experimental.session.compacting"]; - if ( - !planSave || - !planApprove || - !before || - !chat || - !compacting || - !hooks.event - ) { - throw new Error("Missing auto-drive hooks or plan tools."); - } - const saved = JSON.parse( - String( - await planSave.execute( - { - request: { - operationId: "stale-origin-plan-save", - expectedRevision: 0, - goal: "Reject stale same-host mutation provenance.", - plan, - }, - }, - toolContext(workspace), - ), - ), - ); - const commandOutput = { - parts: [{ type: "text", text: "stale" }], - } as unknown as Parameters[1]; - await before( - { command: "flow-auto", sessionID: "auto-host", arguments: "" }, - commandOutput, - ); - await chat({ sessionID: "auto-host" }, { - message: { - id: "stale-origin-command", - agent: "build", - model: { providerID: "provider", modelID: "model" }, - }, - parts: commandOutput.parts, - } as unknown as Parameters[1]); - await emitAssistant( - hooks, - "auto-host", - "stale-origin-command", - "assistant:stale-origin-command", - ); - await chat({ sessionID: "auto-host" }, { - message: { - id: "stale-origin-current-reply", - agent: "build-approved", - model: { providerID: "provider", modelID: "approved-model" }, - }, - parts: [{ type: "text", text: "Approve the plan." }], - } as unknown as Parameters[1]); - - const approvedOutput = String( - await planApprove.execute( - { - request: { - operationId: "stale-origin-plan-approve", - expectedRevision: saved.workflowData.projection.revision, - }, + test("a durably accepted close retains retry authority when archive publication fails", async () => { + const { host, workspace } = await load(); + const saved = await save(host); + await host.command("flow-auto", "--recovery=off", "ses_close"); + await host.response("flow_status", statusInput, "ses_close"); + await host.idle("ses_close"); + await host.admit("Defer this session.", "ses_close"); + await writeFile(join(workspace, ".flow/history"), "unsafe\n"); + const closed = await host.response( + "flow_session_close", + { + request: { + operationId: "pending-close", + expectedRevision: 1, + sessionId: projectionOf(saved).sessionId, + kind: "deferred", + summary: "Deferred by user.", }, - toolContext(workspace, "auto-host", "stale-origin-command"), - ), - ); - expect(JSON.parse(approvedOutput)).toMatchObject({ - status: "ok", - workflowData: { - operation: { replayed: false }, - projection: { status: "ready" }, }, - }); - await hooks.event({ - event: { - type: "session.idle", - properties: { sessionID: "auto-host" }, - }, - } as Parameters>[0]); - - expect(promptCalls).toHaveLength(0); - const afterStaleOrigin = { context: [] as string[] }; - await compacting({ sessionID: "auto-host" }, afterStaleOrigin); - expect(afterStaleOrigin.context).toEqual([]); - }); - - test("retains authority when close commits before archive publication fails", async () => { - const workspace = await createTestWorkspace("flow-auto-close-pending-"); - const promptCalls: unknown[] = []; - const hooks = await loadPlugin(workspace, workspace, promptCalls); - const planSave = hooks.tool?.flow_plan_save; - const sessionClose = hooks.tool?.flow_session_close; - const before = hooks["command.execute.before"]; - const chat = hooks["chat.message"]; - if (!planSave || !sessionClose || !before || !chat || !hooks.event) { - throw new Error("Missing auto-drive hooks or close tools."); - } - const context = toolContext(workspace); - const saved = JSON.parse( - String( - await planSave.execute( - { - request: { - operationId: "pending-close-plan", - expectedRevision: 0, - goal: "Recover a durably accepted close.", - plan, - }, - }, - context, - ), - ), + "ses_close", ); - const commandOutput = { - parts: [{ type: "text", text: "stale" }], - } as unknown as Parameters[1]; - await before( - { command: "flow-auto", sessionID: "auto-close", arguments: "" }, - commandOutput, - ); - await chat({ sessionID: "auto-close" }, { - message: { - id: "auto-close-command", - agent: "build", - model: { providerID: "provider", modelID: "model" }, - }, - parts: commandOutput.parts, - } as unknown as Parameters[1]); - await hooks.event({ - event: { - type: "session.idle", - properties: { sessionID: "auto-close" }, - }, - } as Parameters>[0]); - await chat({ sessionID: "auto-close" }, { - message: { - id: "auto-close-reply", - agent: "build", - model: { providerID: "provider", modelID: "model" }, - }, - parts: [{ type: "text", text: "Defer this session." }], - } as unknown as Parameters[1]); - await emitAssistant(hooks, "auto-close", "auto-close-reply"); - - await writeFile(join(workspace, ".flow", "history"), "unsafe\n"); - const closeArgs = { - request: { - operationId: "pending-close", - expectedRevision: saved.workflowData.projection.revision, - sessionId: saved.workflowData.projection.sessionId, - kind: "deferred" as const, - summary: "Deferred by user.", - }, - }; - const closeOutput = String( - await sessionClose.execute( - closeArgs, - toolContext(workspace, "auto-close", "auto-close-reply"), - ), - ); - const close = JSON.parse(closeOutput); - expect(close).toMatchObject({ + expect(closed).toMatchObject({ status: "error", workflowData: { operation: { replayed: false }, closeState: { durableAccepted: true }, }, }); - await unlink(join(workspace, ".flow", "history")); - await mkdir(join(workspace, ".flow", "history")); - await hooks.event({ - event: { - type: "session.idle", - properties: { sessionID: "auto-close" }, - }, - } as Parameters>[0]); - - expect(promptCalls).toHaveLength(1); - expect(promptCalls[0]).toMatchObject({ - path: { id: "auto-close" }, - body: { - parts: [ - { - text: expect.stringContaining("flow_session_close"), - }, - ], - }, - }); + await unlink(join(workspace, ".flow/history")); + await mkdir(join(workspace, ".flow/history")); + await host.idle("ses_close"); + expect(host.synthetics).toHaveLength(1); + expect(host.synthetics[0]?.text).toContain("flow_session_close"); }); - - test("retains auto authority before decorated after-hooks, then delivers promptAsync", async () => { - const workspace = await createTestWorkspace("flow-auto-"); - const promptCalls: unknown[] = []; - const hooks = await loadPlugin(workspace, workspace, promptCalls); - await hooks.config?.({ - agent: { - "flow-planner": { options: { flowPlanningModel: "test/planning" } }, - }, + test("accepted approval survives decorated native after-hooks and submits one queued continuation with the manager model", async () => { + const { host } = await load(); + await save(host); + const manager = "ses_owner"; + const session = host.ensureSession(manager, { + agent: "build", + model: { providerID: "provider", id: "approved-model", variant: "high" }, }); - const context = toolContext(workspace); - const planSave = hooks.tool?.flow_plan_save; - const planApprove = hooks.tool?.flow_plan_approve; - if (!planSave || !planApprove) throw new Error("Missing plan tools."); - const saved = JSON.parse( - String( - await planSave.execute( - { - request: { - operationId: "auto-plan-save", - expectedRevision: 0, - goal: "Exercise durable auto continuation.", - plan, - }, - }, - context, - ), - ), - ); - expect(saved.status).toBe("ok"); - - const before = hooks["command.execute.before"]; - const chat = hooks["chat.message"]; - const compacting = hooks["experimental.session.compacting"]; - const toolAfter = hooks["tool.execute.after"]; - if (!before || !chat || !compacting || !toolAfter || !hooks.event) { - throw new Error("Missing auto-drive hooks."); - } - const commandOutput = { - parts: [{ type: "text", text: "stale" }], - } as unknown as Parameters[1]; - await before( - { command: "flow-auto", sessionID: "auto-host", arguments: "" }, - commandOutput, - ); - const activationToken = ( - commandOutput.parts.find( - (part) => part.type === "text" && part.synthetic === true, - ) as { metadata?: Record } - ).metadata?.[FLOW_AUTO_METADATA_KEY]; - expect(activationToken).toEqual(expect.any(String)); - await chat({ sessionID: "auto-host" }, { - message: { - id: "auto-command", - agent: "build", - model: { providerID: "provider", modelID: "model" }, - }, - parts: commandOutput.parts, - } as unknown as Parameters[1]); - - const compactingOutput = { context: [] as string[] }; - await compacting({ sessionID: "auto-host" }, compactingOutput); - expect(compactingOutput.context.join(" ")).toContain( - "/flow-auto continuation", - ); - expectExactlyOnce(compactingOutput.context.join("\n"), FLOW_MANAGER_KERNEL); - expect(compactingOutput.context.join("\n")).toContain( - "Load flow-run guidance before any feature or closure route", - ); - expect(compactingOutput.context.join("\n")).toContain( - "replay archiveRetry exactly", - ); - await emitAutoCompaction( - hooks, - "auto-host", - "auto-command", - "auto-compaction", - ); - await chat({ sessionID: "auto-host" }, { - message: { - id: "auto-compaction", - agent: "build-compacted", - model: { providerID: "provider", modelID: "compaction-model" }, - }, - parts: [ - { - type: "text", - text: "Continue if you have next steps.", - synthetic: true, - }, - ], - } as unknown as Parameters[1]); - expect(promptCalls).toHaveLength(0); - const afterCompacted = { context: [] as string[] }; - await compacting({ sessionID: "auto-host" }, afterCompacted); - expectExactlyOnce(afterCompacted.context.join("\n"), FLOW_MANAGER_KERNEL); - - await hooks.event({ - event: { - type: "session.idle", - properties: { sessionID: "auto-host" }, - }, - } as Parameters>[0]); - expect(promptCalls).toHaveLength(0); - - await chat({ sessionID: "auto-host" }, { - message: { - id: "auto-clarification", - agent: "build-clarification", - model: { providerID: "provider", modelID: "clarification-model" }, - }, - parts: [{ type: "text", text: "What do you recommend?" }], - } as unknown as Parameters[1]); - await hooks.event({ - event: { - type: "session.idle", - properties: { sessionID: "auto-host" }, - }, - } as Parameters>[0]); - expect(promptCalls).toHaveLength(0); - const afterClarification = { context: [] as string[] }; - await compacting({ sessionID: "auto-host" }, afterClarification); - expect(afterClarification.context.join(" ")).toContain( - "/flow-auto continuation", - ); - expectExactlyOnce( - afterClarification.context.join("\n"), - FLOW_MANAGER_KERNEL, - ); - - await chat({ sessionID: "auto-host", variant: "requested-high" }, { - message: { - id: "auto-approval", - agent: "build-approved", - model: { - providerID: "provider", - modelID: "approved-model", - variant: "resolved-high", - }, - }, - parts: [{ type: "text", text: "Approve the plan." }], - } as unknown as Parameters[1]); - const approveArgs = { - request: { - operationId: "auto-plan-approve", - expectedRevision: saved.workflowData.projection.revision, - }, + await host.command("flow-auto", "--recovery=off", manager); + await host.response("flow_status", statusInput, manager); + await host.idle(manager); + expect(host.synthetics).toEqual([]); + await host.admit("What do you recommend?", manager); + await host.response("flow_status", statusInput, manager); + await host.idle(manager); + expect(host.synthetics).toEqual([]); + await host.admit("Approve the plan.", manager); + const args = { + request: { operationId: "decorated-approve", expectedRevision: 1 }, }; - await emitAssistant(hooks, "auto-host", "auto-approval"); - const approvedOutput = String( - await planApprove.execute( - approveArgs, - toolContext(workspace, "auto-host", "auto-approval"), - ), - ); - const approved = JSON.parse(approvedOutput); - expect(approved.workflowData.projection.status).toBe("ready"); - const decorated = { - title: "", - output: `${approvedOutput}\n\n[audit-footer]`, - metadata: {}, - }; - await toolAfter( - { - tool: "flow_plan_approve", - sessionID: "auto-host", - callID: "auto-plan-approve-call", - args: approveArgs, - }, - decorated, - ); - expect(decorated.output).toEndWith("[audit-footer]"); - await hooks.event({ - event: { - type: "session.idle", - properties: { sessionID: "auto-host" }, - }, - } as Parameters>[0]); - expect(promptCalls).toHaveLength(1); - const continuationText = String( - (promptCalls[0] as { body: { parts: Array<{ text: string }> } }).body - .parts[0]?.text, - ); - expectExactlyOnce(continuationText, FLOW_MANAGER_KERNEL); - expect(continuationText).toContain( - "Load flow-run guidance before any feature or closure route", - ); - expect(continuationText).toContain("replay archiveRetry exactly"); - expect(promptCalls[0]).toMatchObject({ - path: { id: "auto-host" }, - query: { directory: workspace }, - body: { - agent: "build-approved", - model: { providerID: "provider", modelID: "approved-model" }, - variant: "resolved-high", - parts: [ - { - type: "text", - synthetic: true, - text: expect.stringContaining("flow_run_start"), - metadata: { - [FLOW_AUTO_METADATA_KEY]: expect.any(String), - }, - }, - ], - }, - throwOnError: true, + const native = await host.toolContext("flow_plan_approve", args, manager); + const result = await host.invoke("flow_plan_approve", args, native); + const decorated = { content: `${nativeText(result)}\n\n[audit-footer]` }; + await host.toolHooks.emit("execute.after", { + tool: "flow_plan_approve", + sessionID: native.sessionID, + messageID: native.messageID, + agent: native.agent, + id: native.id, + input: args, + status: "completed", + result: decorated, }); - const continuationParts = ( - promptCalls[0] as { - body: { parts: Array<{ metadata: Record }> }; - } - ).body.parts; - expect(continuationParts[0]?.metadata[FLOW_AUTO_METADATA_KEY]).not.toBe( - activationToken, - ); - expect((promptCalls[0] as { body: { model: object } }).body.model).toEqual({ - providerID: "provider", - modelID: "approved-model", + expect(decorated.content).toEndWith("[audit-footer]"); + await host.idle(manager); + expect(host.synthetics).toHaveLength(1); + const dispatched = host.synthetics[0]; + expect(dispatched).toMatchObject({ + sessionID: manager, + delivery: "queue", + resume: true, + metadata: { [FLOW_AUTO_METADATA_KEY]: expect.any(String) }, }); - const status = hooks.tool?.flow_status; - if (!status) throw new Error("Missing status tool."); - const statusResponse = JSON.parse( - String( - await status.execute( - { request: { view: "compact" } }, - { ...context, sessionID: "auto-host" }, - ), - ), - ); - expect(statusResponse.workflowData).not.toHaveProperty("autoTiming"); - expect(statusResponse.workflowData.autoDrive).toEqual({ - scope: "current-plugin-process", - state: "active", - reason: "continuation", + expect(session.model).toEqual({ + providerID: "provider", + id: "approved-model", + variant: "high", }); - expect(statusResponse.workflowData.projection).not.toHaveProperty( - "autoTiming", + exactlyOnce(dispatched?.text ?? "", FLOW_MANAGER_KERNEL); + expect(dispatched?.text).toContain( + "Load flow-run guidance before any feature or closure route", ); - const detailResponse = JSON.parse( - String( - await status.execute( - { request: { view: "detail" } }, - { ...context, sessionID: "auto-host" }, - ), - ), + expect(dispatched?.text).toContain("replay archiveRetry exactly"); + const compact = await host.response("flow_status", statusInput, manager); + expect(compact.workflowData).not.toHaveProperty("autoTiming"); + expect(compact.workflowData.autoDrive).toMatchObject({ state: "active" }); + const detail = await host.response( + "flow_status", + { request: { view: "detail" } }, + manager, ); - expect(detailResponse.workflowData.autoTiming).toMatchObject({ + expect(detail.workflowData.autoTiming).toMatchObject({ scope: "latest-flow-auto-in-current-plugin-process", authoritative: false, state: "active", }); - expect(detailResponse.workflowData.autoDrive).toEqual( - statusResponse.workflowData.autoDrive, - ); - await hooks.event({ - event: { - type: "session.idle", - properties: { sessionID: "auto-host" }, - }, - } as Parameters>[0]); - expect(promptCalls).toHaveLength(1); - - await hooks.event({ - event: { - type: "session.error", - properties: { - sessionID: "auto-host", - error: { name: "MessageAbortedError" }, - }, - }, - } as Parameters>[0]); - const afterOldError = { context: [] as string[] }; - await compacting({ sessionID: "auto-host" }, afterOldError); - expect(afterOldError.context).toEqual([]); - await hooks.event({ - event: { - type: "session.idle", - properties: { sessionID: "auto-host" }, - }, - } as Parameters>[0]); - expect(promptCalls).toHaveLength(1); - - for (const terminalEvent of [ - { - type: "session.deleted", - properties: { info: { id: "auto-host" } }, - }, - { type: "session.error", properties: {} }, - ]) { - await before( - { command: "flow-auto", sessionID: "auto-host", arguments: "" }, - commandOutput, - ); - await chat({ sessionID: "auto-host" }, { - message: { - id: `terminal-${terminalEvent.type}`, - agent: "build", - model: { providerID: "provider", modelID: "model" }, - }, - parts: commandOutput.parts, - } as unknown as Parameters[1]); - await hooks.event({ - event: terminalEvent, - } as Parameters>[0]); - const afterTerminal = { context: [] as string[] }; - await compacting({ sessionID: "auto-host" }, afterTerminal); - expect(afterTerminal.context).toEqual([]); - } + await host.idle(manager); + expect(host.synthetics).toHaveLength(1); + await host.events.emit({ + id: "evt_interrupt", + created: 100, + type: "session.execution.interrupted", + data: { sessionID: manager, reason: "user" }, + durable: { aggregateID: manager, seq: 1, version: 1 }, + }); + await host.idle(manager); + expect(host.synthetics).toHaveLength(1); + }); + test("automatic compaction keeps the manager kernel and checked continuation authority", async () => { + const { host } = await load(); + await save(host); + await host.command("flow-auto", "--recovery=off", "ses_owner"); + await host.response("flow_status", statusInput, "ses_owner"); + const system = await host.compact("ses_owner"); + const text = system + .filter((part) => part.type === "text") + .map((part) => part.text) + .join("\n"); + exactlyOnce(text, FLOW_MANAGER_KERNEL); + expect(text).toContain("/flow-auto continuation"); + expect(text).toContain("replay archiveRetry exactly"); + expect( + (await host.response("flow_status", statusInput, "ses_owner")) + .workflowData.autoDrive, + ).toMatchObject({ state: "active" }); }); }); -describe("duplicate runtime guard", () => { - test("preserves an owner stop confirmation across a no-lease duplicate hook", async () => { - const workspace = await createTestWorkspace("flow-duplicate-stop-"); - const firstDirectory = join(workspace, "first-directory"); - const secondDirectory = join(workspace, "second-directory"); - await Promise.all([mkdir(firstDirectory), mkdir(secondDirectory)]); - const owner = await loadPlugin(workspace, firstDirectory); - const ownerBefore = owner["command.execute.before"]; - const compacting = owner["experimental.session.compacting"]; - if (!ownerBefore || !compacting) { - throw new Error("Missing owner auto-drive hooks."); - } - const activation = { - parts: [{ type: "text", text: "stale" }], - } as unknown as Parameters[1]; - await ownerBefore( - { command: "flow-auto", sessionID: "auto-owner", arguments: "" }, - activation, - ); - const active = { context: [] as string[] }; - await compacting({ sessionID: "auto-owner" }, active); - expect(active.context).not.toEqual([]); - - const duplicate = await loadPlugin(workspace, secondDirectory); - const duplicateBefore = duplicate["command.execute.before"]; - if (!duplicateBefore) throw new Error("Missing duplicate command hook."); - const stopped = { - parts: [{ type: "text", text: "stale" }], - } as unknown as Parameters[1]; - await ownerBefore( - { command: "flow-auto", sessionID: "auto-owner", arguments: "stop" }, - stopped, +describe("duplicate native runtime guard", () => { + test("rejects duplicate setup, releases its failed registration, and preserves owner stop", async () => { + const { host, workspace } = await load(); + await host.command("flow-auto", "--recovery=off", "ses_owner"); + const duplicate = await createNativeFlowHost(workspace); + hosts.push(duplicate); + await expect(duplicate.load(FlowPlugin)).rejects.toThrow( + "duplicate-instances", ); - expect(stopped.parts).toHaveLength(1); - expect(stopped.parts[0]).toMatchObject({ - type: "text", - text: "Flow auto stopped.", - }); - await duplicateBefore( - { command: "flow-auto", sessionID: "auto-owner", arguments: "stop" }, - stopped, - ); - expect(stopped.parts).toHaveLength(1); - expect(stopped.parts[0]).toMatchObject({ - type: "text", - text: "Flow auto stopped.", - }); - const inactive = { context: [] as string[] }; - await compacting({ sessionID: "auto-owner" }, inactive); - expect(inactive.context).toEqual([]); - }); - - test("disables every copy dynamically until one project runtime remains", async () => { - const workspace = await createTestWorkspace("flow-duplicate-"); - const firstDirectory = join(workspace, "first-directory"); - const secondDirectory = join(workspace, "second-directory"); - await Promise.all([mkdir(firstDirectory), mkdir(secondDirectory)]); - const first = await loadPlugin(workspace, firstDirectory); - const second = await loadPlugin(workspace, secondDirectory); - const context = toolContext(workspace); - - for (const hooks of [first, second]) { - const status = hooks.tool?.flow_status; - if (!status) throw new Error("Missing guarded status tool."); - const parsed = JSON.parse( - String(await status.execute({ request: { view: "compact" } }, context)), - ); - expect(parsed).toMatchObject({ - status: "error", - summary: expect.stringContaining("duplicate-instances"), - workflowData: { - runtimeGuard: { - operational: false, - reason: "duplicate-instances", - }, - }, - }); - expect(parsed.workflowData.runtimeGuard).toEqual({ - operational: false, - reason: "duplicate-instances", - message: "Flow is not operational (duplicate-instances).", - }); - // A guard rejection carries the same `failure` envelope every other Flow - // error uses, because the prompts tell the model to read exactly that. - expect(parsed.workflowData.dataNote).toEqual(expect.any(String)); - expect(parsed.workflowData.failure).toEqual({ - summary: "Flow is not operational (duplicate-instances).", - recovery: expect.stringContaining("Remove the duplicate installation"), - }); - - // `flow_guidance` answers in markdown, so its rejection must be markdown - // too rather than a JSON blob the caller is not parsing. - const guidance = hooks.tool?.flow_guidance; - if (!guidance) throw new Error("Missing guarded guidance tool."); - const rejected = String(await guidance.execute({ id: "flow" }, context)); - expect(rejected).not.toContain("{"); - expect(rejected).toContain( - "Flow is not operational (duplicate-instances).", - ); - expect(rejected).toContain("Recovery: "); - } - - await second.dispose?.(); - const guidance = first.tool?.flow_guidance; - if (!guidance) throw new Error("Missing guarded guidance tool."); + expect(duplicate.tools.size).toBe(0); + await expect( + host.command("flow-auto", "stop", "ses_owner"), + ).resolves.toMatchObject({ text: "/flow-auto stop", resume: false }); + expect((await host.response("flow_status", statusInput)).status).toBe("ok"); for (const id of ["flow", "flow-run"] as const) { - const output = String(await guidance.execute({ id }, context)); + const output = nativeText(await host.call("flow_guidance", { id })); expect(output).toBe(getFlowGuidance(id).content); - expect(output.split(FLOW_MANAGER_KERNEL)).toHaveLength(2); + exactlyOnce(output, FLOW_MANAGER_KERNEL); } }); }); -test("planner cannot mutate manager-owned Flow state even through a direct tool call", async () => { - const workspace = await createTestWorkspace("flow-planner-authority-"); - const hooks = await loadPlugin(workspace); - for (const definition of Object.values(hooks.tool ?? {})) { - const result = JSON.parse( - String( - await definition.execute( - {}, - { ...toolContext(workspace), agent: "flow-planner" }, - ), - ), - ); - expect(result.status).toBe("error"); - expect(result.summary).toContain("manager owns all Flow tools"); +test("planner cannot mutate Flow even through direct native tool execution", async () => { + const { host, workspace } = await load(); + host.ensureSession("ses_planner", { + agent: "flow-planner", + parentID: "ses_manager", + }); + for (const name of TOOL_NAMES) { + const output = nativeText(await host.call(name, {}, "ses_planner")); + if (name === "flow_guidance") + expect(output).toContain("verified workspace session"); + else { + const value: unknown = JSON.parse(output); + expect(value).toMatchObject({ status: "error" }); + } } await expect( - readFile(join(workspace, ".flow", "session.json")), + readFile(join(workspace, ".flow/session.json")), ).rejects.toMatchObject({ code: "ENOENT" }); }); -test("model handoff distinguishes requested roles from bounded session observations", async () => { - const workspace = await createTestWorkspace("flow-model-observation-"); - const hooks = await loadPlugin(workspace); - await hooks.config?.({ - agent: { - "flow-planner": { options: { flowPlanningModel: "test/planner" } }, - "flow-reviewer": { options: { flowReviewerModel: "test/reviewer" } }, +test("model handoff distinguishes requested roles from bounded native manager observations", async () => { + const { host } = await load({ + modelPreferences: { + planning: { kind: "model", model: { providerID: "test", id: "planner" } }, + review: { kind: "model", model: { providerID: "test", id: "reviewer" } }, }, }); - const read = async (sessionID: string) => - JSON.parse( - String( - await hooks.tool?.flow_status?.execute( - { request: { view: "compact" } }, - toolContext(workspace, sessionID), - ), - ), - ).workflowData.modelConfiguration; - expect((await read("manager")).implementation.observed).toBeNull(); - const chat = hooks["chat.message"]; - if (!chat) throw new Error("Missing chat hook"); - const output = { - message: { - id: "request", - agent: "build", - model: { providerID: "test", modelID: "coding", variant: "high" }, - }, - parts: [{ type: "text", text: "Plan only" }], - } as unknown as Parameters[1]; - await chat({ sessionID: "manager" }, output); - expect(output.message.model).toMatchObject({ + const selected = host.ensureSession("ses_manager", { + model: { providerID: "test", id: "coding", variant: "high" }, + }); + const result = await host.response("flow_status", statusInput); + expect(selected.model).toEqual({ providerID: "test", - modelID: "coding", + id: "coding", variant: "high", }); - const status = await read("manager"); - expect(status.planning).toMatchObject({ - requested: "test/planner", - mode: "specialist-advice", - availability: "unverified", - }); - expect(status.review).toMatchObject({ - requested: "test/reviewer", - availability: "unverified", + expect(result.workflowData.modelConfiguration).toMatchObject({ + planning: { + requested: "test/planner", + mode: "specialist-advice", + availability: "unverified", + }, + review: { requested: "test/reviewer", availability: "unverified" }, + implementation: { + observed: { providerID: "test", modelID: "coding", variant: "high" }, + }, }); - expect(status.implementation.observed).toEqual({ - providerID: "test", - modelID: "coding", - variant: "high", + host.ensureSession("ses_planner", { + agent: "flow-planner", + parentID: "ses_manager", + model: { providerID: "test", id: "planner" }, }); - expect((await read("other")).implementation.observed).toBeNull(); - output.message.agent = "flow-planner"; - output.message.model.modelID = "planner"; - await chat({ sessionID: "manager" }, output); - expect((await read("manager")).implementation.observed.modelID).toBe( - "coding", - ); + await host.beginTurn("ses_planner"); + expect( + field( + field( + (await host.response("flow_status", statusInput)).workflowData + .modelConfiguration, + "implementation", + ), + "observed", + ), + ).toEqual({ providerID: "test", modelID: "coding", variant: "high" }); for (let index = 0; index < 128; index++) { - output.message.agent = "build"; - await chat({ sessionID: `other-${index}` }, output); + await host.admit("Inspect.", `ses_other_${index}`); + await host.beginTurn(`ses_other_${index}`); } - expect((await read("manager")).implementation.observed).toBeNull(); + expect( + field( + field( + (await host.response("flow_status", statusInput)).workflowData + .modelConfiguration, + "implementation", + ), + "observed", + ), + ).toBeNull(); }); test("production plugin options cannot supply experimental recovery profiles", async () => { - const workspace = await createTestWorkspace("flow-production-recovery-"); - const hooks = await loadPlugin(workspace, workspace, undefined, { + const { host } = await load({ recoveryTreatment: { origin: "simulation", arm: "manager-plus-jev" }, profiles: [ { @@ -1975,96 +792,33 @@ test("production plugin options cannot supply experimental recovery profiles", a }, ], }); - const before = hooks["command.execute.before"]; - if (!before) throw new Error("Missing command hook."); await expect( - before( - { - command: "flow-auto", - sessionID: "production-refusal", - arguments: - "--recovery=delegated --recovery-calls=3 --recovery-usd=0.01 Repair parser", - }, - { parts: [] }, + host.command( + "flow-auto", + "--recovery=delegated --recovery-calls=3 --recovery-usd=0.01 Repair parser", ), ).rejects.toThrow("No release-owned live qualification"); }); -test("attachment-only user direction can close a stopped recovery session", async () => { - const workspace = await createTestWorkspace("flow-recovery-attachment-"); - const hooks = await loadPlugin(workspace); - const planSave = hooks.tool?.flow_plan_save; - const sessionClose = hooks.tool?.flow_session_close; - const before = hooks["command.execute.before"]; - const chat = hooks["chat.message"]; - if (!planSave || !sessionClose || !before || !chat || !hooks.event) - throw new Error("Missing recovery hooks."); - const saved = JSON.parse( - String( - await planSave.execute( - { - request: { - operationId: "attachment-plan", - expectedRevision: 0, - goal: "Honor attachment direction", - plan, - }, - }, - toolContext(workspace, "attachment-host", "initial-user"), - ), - ), - ); - const commandOutput = { - parts: [{ type: "text", text: "stale" }], - } as unknown as Parameters[1]; - await before( - { - command: "flow-auto", - sessionID: "attachment-host", - arguments: - "--recovery=shadow --recovery-calls=2 --recovery-usd=0.01 Continue", - }, - commandOutput, - ); - await chat({ sessionID: "attachment-host" }, { - message: { - id: "initial-user", - agent: "build", - model: { providerID: "test", modelID: "manager" }, - }, - parts: commandOutput.parts, - } as unknown as Parameters[1]); - await emitAssistant(hooks, "attachment-host", "initial-user"); - await before( - { command: "flow-auto", sessionID: "attachment-host", arguments: "stop" }, - { parts: [] } as Parameters[1], +test("attachment-only native user direction can close a stopped recovery workflow", async () => { + const { host } = await load(); + const saved = await save(host); + await host.command( + "flow-auto", + "--recovery=shadow --recovery-calls=2 --recovery-usd=0.01 Continue", ); - await chat({ sessionID: "attachment-host" }, { - message: { - id: "attachment-user", - agent: "build", - model: { providerID: "test", modelID: "manager" }, + await host.command("flow-auto", "stop"); + await host.admit("", "ses_manager", { + files: [{ uri: "data:image/png;base64,AA==" }], + }); + const closed = await host.response("flow_session_close", { + request: { + operationId: "attachment-close", + expectedRevision: 1, + sessionId: projectionOf(saved).sessionId, + kind: "abandoned", + summary: "Directed by the user attachment.", }, - parts: [ - { type: "file", mime: "image/png", url: "data:image/png;base64,AA==" }, - ], - } as unknown as Parameters[1]); - await emitAssistant(hooks, "attachment-host", "attachment-user"); - const close = JSON.parse( - String( - await sessionClose.execute( - { - request: { - operationId: "attachment-close", - expectedRevision: saved.workflowData.projection.revision, - sessionId: saved.workflowData.projection.sessionId, - kind: "abandoned", - summary: "Directed by the user attachment.", - }, - }, - toolContext(workspace, "attachment-host", "attachment-user"), - ), - ), - ); - expect(close.status).toBe("ok"); + }); + expect(closed.status).toBe("ok"); }); diff --git a/tests/documentation-contract.test.ts b/tests/documentation-contract.test.ts index 89c451cd..662cc1b1 100644 --- a/tests/documentation-contract.test.ts +++ b/tests/documentation-contract.test.ts @@ -5,7 +5,12 @@ import { pinnedBunVersion } from "../evals/bun-toolchain.js"; import packageJson from "../package.json" with { type: "json" }; import { FLOW_CORE_AGENTS, FLOW_CORE_COMMANDS } from "../src/config-shared.js"; import { FLOW_GUIDANCE_IDS } from "../src/guidance/ids.js"; -import FlowPlugin from "../src/index.js"; +import { + persistWorkspaceValidation, + prepareWorkspaceValidation, +} from "../src/infrastructure/fs/workspace-validation.js"; +import { createTools } from "../src/platform/opencode/tools.js"; +import { ValidationCaptureCoordinator } from "../src/platform/opencode/validation-capture.js"; // This file asserts that the documentation matches the *runtime surface*, and // that the maintained set stays navigable. It deliberately does not pin prose. @@ -152,13 +157,17 @@ const RETAINED_BASELINE_RULE_BYTES = 400; const RECOVERY_RUNTIME_DOC_BYTES = 1_400; const OBSERVED_INSPECTION_GATE_DOC_BYTES = 600; const INSPECTION_QUALIFICATION_DOC_BYTES = 400; +const OPENCODE_V2_CUTOVER_DOC_BYTES = 4_500; +const NATIVE_AUTH_AND_KEY_SELECTION_DOC_BYTES = 800; const MAX_MAINTAINED_DOC_BYTES = PRIOR_MAINTAINED_DOC_BYTES + FIRST_USE_AND_PILOT_DOC_BYTES + RETAINED_BASELINE_RULE_BYTES + RECOVERY_RUNTIME_DOC_BYTES + OBSERVED_INSPECTION_GATE_DOC_BYTES + - INSPECTION_QUALIFICATION_DOC_BYTES; + INSPECTION_QUALIFICATION_DOC_BYTES + + OPENCODE_V2_CUTOVER_DOC_BYTES + + NATIVE_AUTH_AND_KEY_SELECTION_DOC_BYTES; /** * Decision records under `docs/adr/`, budgeted apart from maintained prose. @@ -177,11 +186,13 @@ const MAX_MAINTAINED_DOC_BYTES = const DELEGATED_RECOVERY_RECORD_BYTES = 2_000; const RECOVERY_RUNTIME_RECORD_BYTES = 1_400; const INSPECTION_OUTCOME_RECORD_BYTES = 1_200; +const OPENCODE_V2_CUTOVER_RECORD_BYTES = 3_000; const MAX_DECISION_RECORD_BYTES = 48_000 + DELEGATED_RECOVERY_RECORD_BYTES + RECOVERY_RUNTIME_RECORD_BYTES + - INSPECTION_OUTCOME_RECORD_BYTES; + INSPECTION_OUTCOME_RECORD_BYTES + + OPENCODE_V2_CUTOVER_RECORD_BYTES; /** * No single maintained document should outgrow the operator-facing README. @@ -298,21 +309,16 @@ async function markdownFiles(directory: string): Promise { ).flat(); } -async function registeredToolNames(): Promise { - const hooks = await FlowPlugin({ - client: { app: { log() {} } }, - project: {}, - directory: process.cwd(), - worktree: process.cwd(), - experimental_workspace: { register() {} }, - serverUrl: new URL("http://localhost"), - $: {}, - } as unknown as Parameters[0]); - try { - return Object.keys(hooks.tool ?? {}).sort(); - } finally { - await hooks.dispose?.(); - } +function registeredToolNames(): string[] { + const validation = new ValidationCaptureCoordinator({ + readSession: async () => { + throw new Error("Inventory must not execute a tool."); + }, + persistObservation: persistWorkspaceValidation, + }); + return Object.keys( + createTools({ validation, prepareValidation: prepareWorkspaceValidation }), + ).sort(); } describe("Flow documentation contract", () => { @@ -323,11 +329,9 @@ describe("Flow documentation contract", () => { const exactPackage = `opencode-plugin-flow@${packageVersion}`; expect(JSON.parse(block ?? "null")).toEqual({ - plugin: [exactPackage], + plugins: [exactPackage], }); - expect(install).toContain( - `opencode plugin ${exactPackage} --global --force`, - ); + expect(install).toContain(`opencode2 plugin add ${exactPackage}`); // Every version mentioned in Install must be the shipped one, so a release // cannot leave a stale pin behind. expect( @@ -335,7 +339,7 @@ describe("Flow documentation contract", () => { (match) => match[1], ), ).toEqual([packageVersion, packageVersion, packageVersion]); - expect(install).toContain("https://opencode.ai/docs/plugins/"); + expect(install).toContain("https://opencode.ai/v2/docs/plugins/"); expect(install).not.toMatch(/\bnpx\b|activation-check/); }); @@ -595,7 +599,7 @@ describe("Flow documentation contract", () => { `[${packageVersion}]${releaseDate ? ` - ${releaseDate}` : ""}`, ); expect(currentReleaseNotes).toContain( - `opencode plugin opencode-plugin-flow@${packageVersion} --global --force`, + `opencode plugin add opencode-plugin-flow@${packageVersion}`, ); // Release notes must state the schema impact explicitly rather than the // uninformative "public surface is unchanged". diff --git a/tests/eval-auto-scenarios.test.ts b/tests/eval-auto-scenarios.test.ts index 050abc25..218191d6 100644 --- a/tests/eval-auto-scenarios.test.ts +++ b/tests/eval-auto-scenarios.test.ts @@ -298,7 +298,8 @@ test("outside-target repair is accepted only with a source-bound failed-gate ame allCalls: [call, ...fixture.allCalls], flowCalls: [call, ...fixture.flowCalls], }; - if (fixture.hostTrace?.kind !== "observed") throw new Error("Missing trace"); + if (fixture.hostTrace?.kind !== "observed" || fixture.hostTrace.version !== 1) + throw new Error("Missing trace"); const reviewStart = amended.allCalls.find( (value) => value.tool === "flow_review_start", ); diff --git a/tests/eval-canary.test.ts b/tests/eval-canary.test.ts index 3b8f9446..e64e87d7 100644 --- a/tests/eval-canary.test.ts +++ b/tests/eval-canary.test.ts @@ -5,6 +5,7 @@ import { mkdir, mkdtemp, readFile, + realpath, rm, symlink, writeFile, @@ -25,6 +26,7 @@ import { canaryRecordIssue, canaryRecordSha256, deriveCanaryResult, + measureCanaryInstallation, type PreparedCanary, parseCanaryRecord, prepareCanary, @@ -221,8 +223,8 @@ function installation(value: PreparedCanary) { }; } -function prepared(): PreparedCanary { - const base: Omit = { +function prepared(): Extract { + const base: Omit, "sha256"> = { schemaVersion: 1 as const, releaseTag: "v1.2.3", artifact, @@ -252,7 +254,7 @@ async function writePreparedFixture(root: string): Promise<{ tarballSha256: sha256(artifactBytes), }; const { sha256: _initialSha256, ...initialBase } = initial; - const base: Omit = { + const base = { ...initialBase, artifact: artifactValue, artifactSha256: artifactIdentitySha256(artifactValue), @@ -1083,7 +1085,7 @@ describe("canary preparation", () => { ]) { const child = Bun.spawn( [ - "bun", + process.execPath, "run", "scripts/eval-canary.ts", "prepare", @@ -1111,14 +1113,15 @@ describe("canary preparation", () => { join(root, "dist/index.js"), "export default async () => ({});\n", ); + await writeFile(join(root, "dist/tui.js"), "export default {};\n"); await writeFile( join(root, "package.json"), JSON.stringify({ name: "opencode-plugin-flow", - version: "1.2.3", - files: ["dist/index.js"], + version: "10.0.0", + files: ["dist/index.js", "dist/tui.js"], dependencies: { zod: "4.4.2" }, - devDependencies: { "@opencode-ai/plugin": "1.18.5" }, + devDependencies: { "@opencode/plugin": "2.1.4" }, }), ); for (const args of [ @@ -1129,15 +1132,15 @@ describe("canary preparation", () => { ["git", "commit", "-m", "fixture"], ]) await run(args, root); - await run(["bun", "pm", "pack", "--destination", output], root); - const artifactPath = join(output, "opencode-plugin-flow-1.2.3.tgz"); + await run([process.execPath, "pm", "pack", "--destination", output], root); + const artifactPath = join(output, "opencode-plugin-flow-10.0.0.tgz"); await writeFile( join(root, "package.json"), JSON.stringify({ name: "opencode-plugin-flow", - version: "1.2.3", + version: "10.0.0", dependencies: { zod: "9.9.9" }, - devDependencies: { "@opencode-ai/plugin": "9.9.9" }, + devDependencies: { "@opencode/plugin": "9.9.9" }, }), ); await expect( @@ -1154,10 +1157,10 @@ describe("canary preparation", () => { outputDirectory: join(output, "prepared"), preparedAt: new Date("2026-08-25T00:00:00.000Z"), }); - expect(prepared.releaseTag).toBe("v1.2.3"); + expect(prepared.releaseTag).toBe("v10.0.0"); expect( await readFile( - join(output, "prepared/fixture/.opencode/plugins/flow.js"), + join(output, "prepared/fixture/.opencode/plugins/flow/index.js"), "utf8", ), ).toContain("export default"); @@ -1167,18 +1170,46 @@ describe("canary preparation", () => { expect( JSON.parse( await readFile( - join(output, "prepared/fixture/.opencode/package.json"), + join(output, "prepared/fixture/.opencode/plugins/flow/package.json"), "utf8", ), ), ).toEqual({ - private: true, - version: "1.2.3", + name: "opencode-plugin-flow", + version: "10.0.0", + type: "module", + exports: { ".": "./index.js", "./tui": "./tui.js" }, dependencies: { - "@opencode-ai/plugin": "1.18.5", + "@opencode/plugin": "2.1.4", zod: "4.4.2", }, }); + const authenticated = await prepareCanary({ + repositoryRoot: root, + artifactPath, + outputDirectory: join(output, "prepared-auth"), + nativeAuth: true, + }); + if (authenticated.schemaVersion !== 2) + throw new Error("Expected V2 preparation"); + expect(authenticated.runtime.nativeAuth?.kind).toBe( + "native-account-source-v1", + ); + expect( + await readFile(join(output, "prepared-auth/fixture/opencode.json")), + ).toEqual(await readFile(join(output, "prepared/fixture/opencode.json"))); + const measured = await measureCanaryInstallation( + authenticated, + await realpath(join(output, "prepared-auth")), + ); + if (measured.schemaVersion !== 2) + throw new Error("Expected V2 installation"); + expect(Object.keys(measured.nativeAuth?.sourceFiles ?? {}).sort()).toEqual([ + "client-plugin.ts", + "index.ts", + "package.json", + "protocol.ts", + ]); }); }); diff --git a/tests/eval-harness-native-host.test.ts b/tests/eval-harness-native-host.test.ts new file mode 100644 index 00000000..3e710607 --- /dev/null +++ b/tests/eval-harness-native-host.test.ts @@ -0,0 +1,348 @@ +import { expect, test } from "bun:test"; +import { type ChildProcess, spawn } from "node:child_process"; +import { + mkdir, + mkdtemp, + readFile, + realpath, + rm, + writeFile, +} from "node:fs/promises"; +import { createServer } from "node:net"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { pathToFileURL } from "node:url"; +import type { OpenCodeClient } from "@opencode/client"; +import { terminateChildProcessTree } from "../evals/harness.js"; +import { extractObservedModelIdentity } from "../evals/host-observation.js"; +import { + installedOpenCodePluginEntry, + listNativeMessages, + listNativeSessions, + OpenCodeStartup, + projectEvalMessage, +} from "../evals/opencode-host.js"; +import { + cancelRequestBudget, + createRequestBudget, + requestBudgetStatus, +} from "../evals/recovery-decisions/request-budget.js"; +import { datasetDigest } from "../evals/recovery-decisions/schema.js"; +import { localScriptedProvider } from "./fixtures/opencode-v2-scripted-provider.js"; +import { proveNativeUsage } from "./fixtures/opencode-v2-usage-proof.js"; + +const executable = process.env.FLOW_OPENCODE_NATIVE_EXECUTABLE; + +for (const mode of ["client", "budget"] as const) + test.skipIf(!executable)( + `native ${mode} helpers and local fixture execute on isolated OpenCode 2.1.4`, + async () => { + if (!executable) + throw new Error("An existing exact 2.1.4 executable is required"); + const root = await mkdtemp(join(tmpdir(), "flow-v2-native-eval-test-")); + const provider = localScriptedProvider(); + const startup = new OpenCodeStartup(); + let child: ChildProcess | undefined; + try { + const project = join(root, "project"); + const fixture = join(root, "plugin"); + for (const path of [ + project, + fixture, + ...["home", "config", "data", "cache", "state", "tmp"].map((name) => + join(root, name), + ), + ]) + await mkdir(path); + await writeFile(join(project, "README.md"), "Native client fixture\n"); + const directory = join(root, "budget"); + const authorization = + mode === "budget" + ? await createRequestBudget(directory, { + schemaVersion: 1, + origin: "simulation", + purpose: "Native local request accounting proof", + maxRequests: 1, + maxMicroUsd: 5000, + expiresAt: new Date(Date.now() + 60000).toISOString(), + models: [ + { + model: "xai/grok-4.6", + reservationMicroUsd: 5000, + basis: { kind: "simulation" }, + }, + ], + }) + : null; + await writeFile( + join(fixture, "index.ts"), + `export { default } from ${JSON.stringify(pathToFileURL(join(import.meta.dir, mode === "budget" ? "fixtures/opencode-v2-budget-plugin.ts" : "fixtures/opencode-v2-smoke-plugin.ts")).href)};\n`, + ); + if (mode === "client") { + await mkdir(join(fixture, "dist")); + await writeFile( + join(fixture, "dist/index.js"), + await readFile(join(fixture, "index.ts"), "utf8"), + ); + await writeFile( + join(fixture, "dist/tui.js"), + "export default { id: 'local-tui-fixture', setup() {} };\n", + ); + await rm(join(fixture, "index.ts")); + await writeFile( + join(fixture, "package.json"), + JSON.stringify({ + name: "native-package-layout-proof", + type: "module", + exports: { ".": "./dist/index.js", "./tui": "./dist/tui.js" }, + }), + ); + } + + const pluginEntry = + mode === "client" + ? await installedOpenCodePluginEntry(fixture, join(root, "entry")) + : fixture; + await writeFile( + join(project, "opencode.json"), + JSON.stringify({ + plugins: [ + { + package: pluginEntry, + options: { + baseURL: provider.baseURL, + receipt: join(root, "receipt.jsonl"), + ...(authorization + ? { + directory, + authorizationDigest: datasetDigest(authorization), + readyPath: join(root, "budget-ready.json"), + } + : {}), + }, + }, + ], + }), + ); + + const listener = createServer(); + await new Promise((resolve) => + listener.listen(0, "127.0.0.1", resolve), + ); + const address = listener.address(); + if (!address || typeof address === "string") + throw new Error("No native test port"); + await new Promise((resolve) => listener.close(() => resolve())); + child = spawn( + executable, + ["serve", "--hostname", "127.0.0.1", "--port", String(address.port)], + { + cwd: project, + detached: process.platform !== "win32", + stdio: ["ignore", "pipe", "pipe"], + env: { + PATH: "/usr/bin:/bin", + HOME: join(root, "home"), + XDG_CONFIG_HOME: join(root, "config"), + XDG_CACHE_HOME: join(root, "cache"), + XDG_DATA_HOME: join(root, "data"), + XDG_STATE_HOME: join(root, "state"), + TMPDIR: join(root, "tmp"), + }, + }, + ); + child.stdout?.on("data", (chunk) => startup.consume(String(chunk))); + child.stderr?.on("data", (chunk) => + startup.consume(String(chunk), "stderr"), + ); + let client: OpenCodeClient | null = null; + const deadline = Date.now() + 15000; + while (Date.now() < deadline) { + if (child.exitCode !== null) + throw new Error(`Native host exited. ${startup.log}`); + client = startup.client(`http://127.0.0.1:${address.port}`, project); + if (client) break; + await Bun.sleep(20); + } + if (!client) + throw new Error(`Native startup timed out. ${startup.log}`); + expect((await client.server.info()).version).toBe("2.1.4"); + const request = () => ({ signal: AbortSignal.timeout(15000) }); + const parent = await client.session.create( + { + title: "Native helpers", + permissions: [{ action: "*", resource: "*", effect: "allow" }], + model: { providerID: "flow-smoke", id: "catalog-model" }, + location: { directory: project }, + }, + request(), + ); + const childSession = await client.session.create( + { + parentID: parent.id, + agent: "general", + location: { directory: project }, + }, + request(), + ); + expect( + (await client.session.get({ sessionID: childSession.id }, request())) + .parentID, + ).toBe(parent.id); + expect(await realpath(parent.location.directory)).toBe( + await realpath(project), + ); + await client.session.prompt( + { sessionID: parent.id, text: "Local native client ping" }, + request(), + ); + await client.session.wait({ sessionID: parent.id }, request()); + if (mode === "budget") { + const native = await listNativeMessages(client, parent.id); + expect( + native.some( + (message) => + message.type === "assistant" && message.error !== undefined, + ), + ).toBe(true); + expect((await requestBudgetStatus(directory)).consumed).toBe(1); + expect((await requestBudgetStatus(directory)).reservedMicroUsd).toBe( + 5000, + ); + await client.session.prompt( + { sessionID: parent.id, text: "Budget exhaustion must refuse" }, + request(), + ); + await client.session.wait({ sessionID: parent.id }, request()); + expect((await requestBudgetStatus(directory)).consumed).toBe(1); + await cancelRequestBudget(directory); + await client.session.prompt( + { sessionID: parent.id, text: "Cancelled budget must refuse" }, + request(), + ); + await client.session.wait({ sessionID: parent.id }, request()); + expect((await requestBudgetStatus(directory)).consumed).toBe(1); + expect(provider.requests).toEqual([]); + return; + } + + const native = await listNativeMessages(client, parent.id); + await proveNativeUsage(client, parent.id, project); + + expect(extractObservedModelIdentity(native)).toEqual({ + kind: "observed", + value: { providerID: "flow-smoke", modelID: "catalog-model" }, + }); + expect( + native + .filter((message) => message.type === "assistant") + .flatMap((message) => + projectEvalMessage(message, parent.id).parts.map( + (part) => part.text, + ), + ), + ).toContain("Local smoke complete"); + expect( + (await listNativeSessions(client, project)).some( + (session) => + session.id === childSession.id && session.parentID === parent.id, + ), + ).toBe(true); + await client.session.prompt( + { + sessionID: parent.id, + text: `FLOW_SMOKE_ACTIONS ${JSON.stringify([{ tool: "shell", input: { command: "printf native-smoke-ok", description: "Local native fixture" } }])}`, + }, + request(), + ); + await client.session.wait({ sessionID: parent.id }, request()); + expect( + (await listNativeMessages(client, parent.id)).flatMap((message) => + message.type === "assistant" + ? message.content + .filter((content) => content.type === "tool") + .map((content) => [content.name, content.state.status]) + : [], + ), + ).toEqual([["shell", "completed"]]); + + await client.session.compact({ sessionID: parent.id }, request()); + await client.session.wait({ sessionID: parent.id }, request()); + expect( + (await listNativeMessages(client, parent.id)) + .filter((message) => message.type === "compaction") + .map((message) => message.status), + ).toEqual(["completed"]); + expect(provider.requests.map((request) => request.model)).toEqual([ + "wire-model", + "wire-model", + "wire-model", + ]); + const errorSession = await client.session.create( + { + model: { providerID: "flow-smoke", id: "catalog-model" }, + location: { directory: project }, + }, + request(), + ); + await client.session.prompt( + { sessionID: errorSession.id, text: "FLOW_SMOKE_PROVIDER_ERROR" }, + request(), + ); + await client.session.wait({ sessionID: errorSession.id }, request()); + expect( + (await listNativeMessages(client, errorSession.id)).some( + (message) => + message.type === "assistant" && + message.error?.message.includes("Local scripted provider error"), + ), + ).toBe(true); + const cancelSession = await client.session.create( + { + model: { providerID: "flow-smoke", id: "catalog-model" }, + location: { directory: project }, + }, + request(), + ); + await client.session.prompt( + { sessionID: cancelSession.id, text: "FLOW_SMOKE_DELAY" }, + request(), + ); + const cancelDeadline = Date.now() + 3000; + while ( + Date.now() < cancelDeadline && + !(await listNativeMessages(client, cancelSession.id)).some( + (message) => + message.type === "assistant" && !message.time.completed, + ) + ) + await Bun.sleep(10); + expect( + ( + await client.session.interrupt( + { sessionID: cancelSession.id, resume: false }, + request(), + ) + ).interrupted, + ).toBe(true); + await client.session.wait({ sessionID: cancelSession.id }, request()); + expect( + (await listNativeMessages(client, cancelSession.id)) + .filter((message) => message.type === "assistant") + .map((message) => message.error?.type), + ).toContain("aborted"); + + expect( + (await readFile(join(root, "receipt.jsonl"), "utf8")).includes( + '"version":"2.1.4"', + ), + ).toBe(true); + } finally { + startup.clear(); + if (child) await terminateChildProcessTree(child); + await provider.server.stop(true); + await rm(root, { recursive: true, force: true }); + } + }, + 45000, + ); diff --git a/tests/eval-harness-native-outcome.test.ts b/tests/eval-harness-native-outcome.test.ts new file mode 100644 index 00000000..068b32a6 --- /dev/null +++ b/tests/eval-harness-native-outcome.test.ts @@ -0,0 +1,186 @@ +import { expect, test } from "bun:test"; +import { mkdtemp, rm } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { OpenCode, type SessionMessageInfo } from "@opencode/client"; +import { + EvalHost, + isSelfAbortError, + runSessionRequest, +} from "../evals/harness.js"; + +const usage = { + input: 7, + output: 3, + reasoning: 2, + cache: { read: 1, write: 4 }, +}; + +test("native outcome retains grader inputs, compaction usage, and provider failure evidence", async () => { + const project = await mkdtemp(join(tmpdir(), "flow-v2-outcome-")); + const messages: SessionMessageInfo[] = [ + { id: "user", type: "user", text: "work", time: { created: 1 } }, + { + id: "assistant", + type: "assistant", + agent: "build", + model: { providerID: "fixture", id: "catalog-id" }, + time: { created: 2, completed: 3 }, + tokens: usage, + cost: 0.01, + content: [ + { type: "text", text: "Done" }, + { + type: "tool", + id: "call", + name: "flow_status", + time: { created: 2, completed: 3 }, + state: { + status: "completed", + input: { request: { view: "compact" } }, + content: [ + { + type: "text", + text: '{"status":"ok","summary":"Ready","workflowData":{}}', + }, + ], + metadata: { native: "retained" }, + }, + }, + ], + }, + { + id: "compaction", + type: "compaction", + status: "completed", + reason: "auto", + summary: "Saved", + time: { created: 4 }, + tokens: usage, + cost: 0.02, + }, + { + id: "failed", + type: "assistant", + agent: "build", + model: { providerID: "fixture", id: "catalog-id" }, + time: { created: 5, completed: 6 }, + content: [], + error: { type: "api", message: "Local provider refused" }, + }, + ]; + const server = Bun.serve({ + hostname: "127.0.0.1", + port: 0, + fetch(request) { + const url = new URL(request.url); + if (url.pathname === "/api/session") + return Response.json({ data: [], cursor: {} }); + if (url.pathname.endsWith("/message")) + return Response.json({ data: messages, cursor: {} }); + return Response.json({ + id: "root", + agent: "build", + location: { directory: project }, + }); + }, + }); + try { + const host: unknown = Reflect.construct(EvalHost, [project, project]); + if (!(host instanceof EvalHost)) + throw new Error("Could not construct isolated outcome reader"); + Reflect.set( + host, + "nativeClient", + OpenCode.make({ baseUrl: server.url.href }), + ); + const outcome = await host.outcome(["root"], 12); + expect(outcome.flowCalls).toEqual([ + { + tool: "flow_status", + status: "completed", + sessionIndex: 0, + agent: "build", + input: { request: { view: "compact" } }, + output: { status: "ok", summary: "Ready", workflowData: {} }, + rawOutput: '{"status":"ok","summary":"Ready","workflowData":{}}', + metadata: { native: "retained" }, + }, + ]); + expect(outcome.tokens).toEqual({ + input: 14, + output: 6, + reasoning: 4, + cacheRead: 2, + cacheWrite: 8, + }); + expect(outcome.costUsd).toBeNull(); + expect(outcome.usageAvailability).toBe("incomplete"); + expect(outcome.providerError).toEqual({ + origin: "provider", + code: "provider-rejected-turn", + detail: '{"type":"api","message":"Local provider refused"}', + retryable: true, + }); + expect(outcome.providerErrorObservation).toEqual({ + sessionId: "root", + name: "api", + message: "Local provider refused", + }); + expect(outcome.finalText).toBe("Done"); + expect(outcome.actors?.[0]?.actualModel).toEqual({ + kind: "observed", + value: { providerID: "fixture", modelID: "catalog-id" }, + }); + } finally { + await server.stop(true); + await rm(project, { recursive: true, force: true }); + } +}); + +test("native self interruption does not swallow an unrelated provider error", () => { + expect( + isSelfAbortError({ type: "aborted", message: "Step interrupted" }, true), + ).toBe(true); + expect( + isSelfAbortError({ type: "aborted", message: "Step interrupted" }, false), + ).toBe(false); + expect( + isSelfAbortError({ type: "api", message: "provider failure" }, true), + ).toBe(false); +}); + +test("cancelled session admission settles and preserves the original cancellation", async () => { + const controller = new AbortController(); + const reason = new Error("Stop the native wait"); + let cancelled = 0; + let rejected = 0; + let admitted = false; + const pending = runSessionRequest({ + signal: controller.signal, + post: async (_url, _body, options) => + new Promise((resolve, reject) => { + admitted = true; + options.signal.addEventListener( + "abort", + () => reject(options.signal.reason), + { once: true }, + ); + void resolve; + }), + url: "local", + body: null, + onRejected: () => { + rejected += 1; + }, + wait: async () => new Promise(() => {}), + onCancelled: async () => { + cancelled += 1; + }, + }); + expect(admitted).toBe(true); + controller.abort(reason); + await expect(pending).rejects.toBe(reason); + expect(cancelled).toBe(1); + expect(rejected).toBe(0); +}); diff --git a/tests/eval-host-config.test.ts b/tests/eval-host-config.test.ts index 1a21d751..a166c9b4 100644 --- a/tests/eval-host-config.test.ts +++ b/tests/eval-host-config.test.ts @@ -4,10 +4,11 @@ import { tmpdir } from "node:os"; import { join } from "node:path"; import { currentBunToolchain } from "../evals/bun-toolchain.js"; import { EvalHost, packPlugin, preparePackageCache } from "../evals/harness.js"; +import { OPENCODE_HOST_VERSION } from "../evals/opencode-host.js"; import { evalReviewerConfiguration } from "../evals/run.js"; import packageJson from "../package.json" with { type: "json" }; -test("eval reviewer configuration keeps provenance and tuple options aligned", () => { +test("eval reviewer configuration keeps provenance and object options aligned", () => { expect( evalReviewerConfiguration("manager/model", { OPENCODE_FLOW_REVIEWER_MODEL: " reviewer/model ", @@ -25,51 +26,65 @@ test("eval reviewer configuration keeps provenance and tuple options aligned", ( }); }); -test("eval host writes reviewer model through native plugin tuple options", async () => { - const repositoryRoot = join(import.meta.dir, ".."); - const toolchain = currentBunToolchain(packageJson.packageManager); - const scratch = await mkdtemp(join(tmpdir(), "flow-eval-host-config-test-")); - const previous = process.env.FLOW_EVAL_NO_AUTH_COPY; - process.env.FLOW_EVAL_NO_AUTH_COPY = "1"; - let host: EvalHost | null = null; - try { - const tarball = await packPlugin(repositoryRoot, scratch, toolchain); - const packageCache = await preparePackageCache(tarball, scratch, toolchain); - host = await EvalHost.start({ - toolchain, - packageCache, - opencodeVersion: packageJson.devDependencies["@opencode-ai/plugin"], - files: { "package.json": '{"name":"eval-host-config-test"}\n' }, - reviewer: { model: "provider/reviewer", steps: 80 }, - // Let startup own cancellation/cleanup before the outer test expires. - signal: AbortSignal.timeout(180_000), - }); +test.skipIf(process.env.FLOW_LIVE_SMOKE !== "1")( + "eval host writes reviewer model through native plugin object options", + async () => { + const repositoryRoot = join(import.meta.dir, ".."); + const toolchain = currentBunToolchain(packageJson.packageManager); + const scratch = await mkdtemp( + join(tmpdir(), "flow-eval-host-config-test-"), + ); + const previous = process.env.FLOW_EVAL_NO_AUTH_COPY; + process.env.FLOW_EVAL_NO_AUTH_COPY = "1"; + let host: EvalHost | null = null; + try { + const tarball = await packPlugin(repositoryRoot, scratch, toolchain); + const packageCache = await preparePackageCache( + tarball, + scratch, + toolchain, + ); + host = await EvalHost.start({ + toolchain, + packageCache, + opencodeVersion: OPENCODE_HOST_VERSION, + files: { "package.json": '{"name":"eval-host-config-test"}\n' }, + providerCredentials: "disabled", + reviewer: { model: "provider/reviewer", steps: 80 }, + // Let startup own cancellation/cleanup before the outer test expires. + signal: AbortSignal.timeout(180_000), + }); - expect( - JSON.parse(await readFile(join(host.project, "opencode.json"), "utf8")), - ).toEqual({ - $schema: "https://opencode.ai/config.json", - permission: { external_directory: "deny" }, - plugin: [ - [ - `opencode-plugin-flow@${packageJson.version}`, - { reviewer: { model: "provider/reviewer", steps: 80 } }, + expect( + JSON.parse(await readFile(join(host.project, "opencode.json"), "utf8")), + ).toEqual({ + permissions: [ + { action: "external_directory", resource: "*", effect: "deny" }, ], - ], - }); - } finally { - try { - await host?.stop(); + plugins: [ + { + package: expect.stringContaining("/flow-plugin-entry"), + options: { reviewer: { model: "provider/reviewer", steps: 80 } }, + }, + ], + }); } finally { - if (previous === undefined) delete process.env.FLOW_EVAL_NO_AUTH_COPY; - else process.env.FLOW_EVAL_NO_AUTH_COPY = previous; - await rm(scratch, { recursive: true, force: true }); + try { + await host?.stop(); + } finally { + if (previous === undefined) delete process.env.FLOW_EVAL_NO_AUTH_COPY; + else process.env.FLOW_EVAL_NO_AUTH_COPY = previous; + await rm(scratch, { recursive: true, force: true }); + } } - } - // The host already permits 180s startup; allow packaging and cleanup as well. -}, 240_000); + // The host already permits 180s startup; allow packaging and cleanup as well. + }, + 240_000, +); -test.skipIf(process.platform !== "linux")( +test.skipIf( + process.platform !== "linux" || process.env.FLOW_LIVE_SMOKE !== "1", +)( "ordinary eval host does not pass ambient TypeSafe credentials to OpenCode", async () => { const toolchain = currentBunToolchain(packageJson.packageManager); @@ -88,7 +103,7 @@ test.skipIf(process.platform !== "linux")( }, packageCache: "", withFlow: false, - opencodeVersion: packageJson.devDependencies["@opencode-ai/plugin"], + opencodeVersion: OPENCODE_HOST_VERSION, ambientConfig: "disabled", providerCredentials: "disabled", files: { "package.json": '{"name":"eval-host-credential-test"}\n' }, diff --git a/tests/eval-host-observation.test.ts b/tests/eval-host-observation.test.ts new file mode 100644 index 00000000..88825734 --- /dev/null +++ b/tests/eval-host-observation.test.ts @@ -0,0 +1,329 @@ +import { expect, test } from "bun:test"; +import { + OpenCode, + type SessionMessageAssistant, + type SessionMessageInfo, +} from "@opencode/client"; +import { + extractObservedActor, + extractObservedModelIdentity, + extractObservedVariant, + selectLineageValidatedReviewers, +} from "../evals/host-observation.js"; +import { + listNativeMessages, + listNativeSessions, + nativeModel, + OpenCodeStartup, + openCodeInvocation, + projectEvalMessage, +} from "../evals/opencode-host.js"; + +function assistant( + overrides: Partial = {}, +): SessionMessageAssistant { + return { + id: "msg_one", + type: "assistant", + agent: "build", + model: { providerID: "fixture", id: "catalog-alias", variant: "high" }, + time: { created: 1, completed: 2 }, + content: [], + ...overrides, + }; +} + +test("native selection uses the catalog identity and exact host invocation", () => { + expect(nativeModel("fixture/vendor/model", "high")).toEqual({ + providerID: "fixture", + id: "vendor/model", + variant: "high", + }); + expect( + openCodeInvocation(["serve", "--hostname", "127.0.0.1", "--port", "43210"]), + ).toEqual({ + command: "npm", + args: [ + "exec", + "--yes", + "--package=@opencode/cli@2.1.4", + "--", + "opencode", + "serve", + "--hostname", + "127.0.0.1", + "--port", + "43210", + ], + }); + for (const version of [ + "1.18.31", + "2.0.9", + "2.1.3", + "latest", + "^2.1.4", + "3.0.0", + "2.1.4 --help", + ]) + expect(() => openCodeInvocation([], version)).toThrow( + "exact tested 2.x version", + ); + expect(openCodeInvocation([], "2.2.0").args[2]).toBe( + "--package=@opencode/cli@2.2.0", + ); +}); + +test("startup authenticates through the public client without exposing split password lines", async () => { + const startup = new OpenCodeStartup(); + const requests: { + path: string; + authorized: boolean; + directory: string | null; + }[] = []; + const server = Bun.serve({ + hostname: "127.0.0.1", + port: 0, + fetch(request) { + requests.push({ + path: new URL(request.url).pathname, + authorized: + request.headers.get("authorization") === + `Basic ${Buffer.from("opencode:fixture-private").toString("base64")}`, + directory: request.headers.get("x-opencode-directory"), + }); + return Response.json({ + version: "2.1.4", + pid: 123, + urls: [], + paths: { tmp: "/fixture/tmp" }, + }); + }, + }); + try { + startup.consume("starting\nserver pass"); + expect(startup.client(server.url.href, "/fixture")).toBeNull(); + startup.consume("separate diagnostic\n", "stderr"); + startup.consume("word fixture-"); + startup.consume("private\nready\n"); + const client = startup.client(server.url.href, "/fixture"); + if (!client) throw new Error("Missing authenticated client"); + expect(await client.server.info()).toEqual({ + version: "2.1.4", + pid: 123, + urls: [], + paths: { tmp: "/fixture/tmp" }, + }); + expect(startup.log).toBe("starting\nseparate diagnostic\nready\n"); + expect(requests).toEqual([ + { path: "/api/info", authorized: true, directory: "/fixture" }, + ]); + startup.clear(); + expect(startup.client(server.url.href, "/fixture")).toBeNull(); + } finally { + await server.stop(true); + } +}); + +test("native observation requires completed assistant identities and rejects mixed aliases", () => { + expect(extractObservedModelIdentity([assistant()])).toEqual({ + kind: "observed", + value: { providerID: "fixture", modelID: "catalog-alias" }, + }); + expect(extractObservedVariant([assistant()])).toEqual({ + kind: "observed", + value: "high", + }); + expect( + extractObservedModelIdentity([assistant({ time: { created: 1 } })]), + ).toEqual({ kind: "unobserved", reason: "no-completed-assistant" }); + expect( + extractObservedModelIdentity([ + assistant(), + assistant({ model: { providerID: "fixture", id: "wire-model" } }), + ]), + ).toEqual({ kind: "unobserved", reason: "conflicting-observations" }); + expect( + extractObservedModelIdentity([ + assistant({ error: { type: "api", message: "provider refused" } }), + ]), + ).toEqual({ kind: "unobserved", reason: "no-completed-assistant" }); + expect( + extractObservedModelIdentity([assistant(), { ...assistant(), model: {} }]), + ).toEqual({ kind: "unobserved", reason: "field-unavailable" }); + expect(extractObservedActor({ role: "reviewer", sessions: [] })).toEqual({ + role: "reviewer", + sessionIds: [], + actualModel: { kind: "unobserved", reason: "reviewer-child-not-observed" }, + }); + expect( + selectLineageValidatedReviewers( + ["root"], + [ + { id: "child", parentID: "root", agent: "flow-reviewer" }, + { id: "stranger", parentID: "other", agent: "flow-reviewer" }, + ], + ), + ).toEqual([{ id: "child", parentID: "root", agent: "flow-reviewer" }]); +}); + +test("native tool content projects into existing grader inputs without invented native parent or part IDs", () => { + const native = assistant({ + content: [ + { + type: "tool", + id: "call_one", + name: "flow_status", + time: { created: 2, completed: 3 }, + state: { + status: "completed", + input: { view: "compact" }, + content: [{ type: "text", text: '{"status":"ok"}' }], + metadata: { exit: 0 }, + }, + }, + ], + }); + const projected = projectEvalMessage(native, "root"); + expect(projected.native).toEqual(native); + expect(projected.info.parentID).toBeUndefined(); + expect(projected.parts).toEqual([ + { + type: "tool", + tool: "flow_status", + callID: "call_one", + sessionID: "root", + messageID: "msg_one", + time: { start: 2, end: 3 }, + state: { + status: "completed", + input: { view: "compact" }, + output: '{"status":"ok"}', + metadata: { exit: 0 }, + }, + }, + ]); +}); + +test("native streaming arguments, tool errors and compaction retain their distinct facts", () => { + const projected = projectEvalMessage( + assistant({ + content: [ + { + type: "tool", + id: "stream", + name: "shell", + time: { created: 2 }, + state: { status: "streaming", input: '{"command":' }, + }, + { + type: "tool", + id: "failed", + name: "shell", + time: { created: 3, completed: 4 }, + state: { + status: "error", + input: { command: "x" }, + error: { type: "execution", message: "cwd missing" }, + }, + }, + ], + }), + "root", + ); + expect(projected.parts.map((part) => part.state)).toEqual([ + { status: "pending", input: {}, raw: '{"command":' }, + { + status: "error", + input: { command: "x" }, + output: "", + error: "cwd missing", + }, + ]); + const compact = projectEvalMessage( + { + id: "compact", + type: "compaction", + reason: "auto", + status: "completed", + time: { created: 4 }, + summary: "saved", + tokens: { + input: 1, + output: 2, + reasoning: 3, + cache: { read: 4, write: 5 }, + }, + cost: 0.01, + }, + "root", + ); + expect(compact.info.tokens).toEqual({ + input: 1, + output: 2, + reasoning: 3, + cache: { read: 4, write: 5 }, + }); + expect(compact.parts).toEqual([ + { + type: "compaction", + auto: true, + text: "saved", + sessionID: "root", + messageID: "compact", + }, + ]); +}); + +test("public list calls drain native cursors and retain session parentage and location", async () => { + const messages: SessionMessageInfo[] = [ + { id: "user", type: "user", text: "hello", time: { created: 0 } }, + assistant(), + ]; + const seen: string[] = []; + const server = Bun.serve({ + hostname: "127.0.0.1", + port: 0, + fetch(request) { + const url = new URL(request.url); + seen.push(url.pathname); + const index = url.searchParams.has("cursor") ? 1 : 0; + return Response.json({ + data: url.pathname.endsWith("/message") + ? [messages[index]] + : [ + { + id: index ? "child" : "root", + ...(index ? { parentID: "root" } : {}), + agent: index ? "flow-reviewer" : "build", + location: { directory: "/fixture" }, + }, + ], + cursor: index ? {} : { next: "second" }, + }); + }, + }); + try { + const client = OpenCode.make({ baseUrl: server.url.href }); + expect(await listNativeMessages(client, "root")).toEqual(messages); + expect( + (await listNativeSessions(client, "/fixture")).map( + ({ id, parentID, location }) => ({ + id, + parentID: parentID ?? null, + location, + }), + ), + ).toEqual([ + { id: "root", parentID: null, location: { directory: "/fixture" } }, + { id: "child", parentID: "root", location: { directory: "/fixture" } }, + ]); + expect(seen).toEqual([ + "/api/session/root/message", + "/api/session/root/message", + "/api/session", + "/api/session", + ]); + } finally { + await server.stop(true); + } +}); diff --git a/tests/eval-host-trace.test.ts b/tests/eval-host-trace.test.ts index 1e836964..c0b1f0d3 100644 --- a/tests/eval-host-trace.test.ts +++ b/tests/eval-host-trace.test.ts @@ -2,6 +2,7 @@ import { expect, test } from "bun:test"; import { mkdtemp, rm } from "node:fs/promises"; import { tmpdir } from "node:os"; import { join } from "node:path"; +import { OpenCode } from "@opencode/client"; import { pseudonymizeEvalIds, RetainedScenarioEvidenceSchema, @@ -74,7 +75,8 @@ const fixture = () => ({ test("retained native call references bind to their observed tool parts", () => { const trace = collectHostTrace(fixture()); - if (trace.kind !== "observed") throw new Error("Expected native facts"); + if (trace.kind !== "observed" || trace.version !== 1) + throw new Error("Expected native facts"); const call = { native: { sessionId: "ses_root", @@ -176,7 +178,8 @@ test("retained native call references bind to their observed tool parts", () => test("collects mixed initial user parts without retaining text or raw token", () => { const trace = collectHostTrace(fixture()); expect(trace.kind).toBe("observed"); - if (trace.kind !== "observed") throw new Error("Expected native facts"); + if (trace.kind !== "observed" || trace.version !== 1) + throw new Error("Expected native facts"); expect(trace.sessions).toEqual([ { id: "ses_root", parentId: null, agent: null, projectMatched: true }, ]); @@ -323,7 +326,8 @@ test("child task user prompts remain outside runner roots", () => { ], }); expect(trace.kind).toBe("observed"); - if (trace.kind !== "observed") throw new Error("Expected native facts"); + if (trace.kind !== "observed" || trace.version !== 1) + throw new Error("Expected native facts"); expect(trace.runnerRootSessionIds).toEqual(["ses_root"]); expect( trace.messages @@ -372,7 +376,8 @@ test("second real root input remains visible despite a supplied count", () => { }; const trace = collectHostTrace(withCount); expect(trace.kind).toBe("observed"); - if (trace.kind !== "observed") throw new Error("Expected native facts"); + if (trace.kind !== "observed" || trace.version !== 1) + throw new Error("Expected native facts"); expect( trace.messages .filter((message) => message.role === "user") @@ -386,7 +391,11 @@ test("missing initial marker is absence, not inferred authority", () => { required(required(input.sessionMessages[0]).messages[0]).parts.splice(1, 1); const trace = collectHostTrace(input); expect(trace.kind).toBe("observed"); - if (trace.kind !== "observed" || trace.messages[0]?.role !== "user") + if ( + trace.kind !== "observed" || + trace.version !== 1 || + trace.messages[0]?.role !== "user" + ) throw new Error("Expected native user facts"); expect(trace.messages[0].parts.map((part) => part.flowTokenSha256)).toEqual([ null, @@ -439,7 +448,8 @@ test("a changed token is retained as distinct native facts without claiming auth }, ], }); - if (trace.kind !== "observed") throw new Error("Expected native facts"); + if (trace.kind !== "observed" || trace.version !== 1) + throw new Error("Expected native facts"); const initial = trace.messages[0], later = trace.messages[2]; if (initial?.role !== "user" || later?.role !== "user") @@ -480,7 +490,8 @@ test("compaction facts cannot attest native event delivery from final messages", }, ], }); - if (trace.kind !== "observed") throw new Error("Expected native facts"); + if (trace.kind !== "observed" || trace.version !== 1) + throw new Error("Expected native facts"); expect(trace.compactionDelivery).toBe("unavailable-from-message-export"); const message = trace.messages[2]; if (message?.role !== "user") throw new Error("Expected compaction user"); @@ -491,7 +502,49 @@ test.each([false, true])( "EvalHost reads actual primary metadata and retains trace through grade input: GET fails $0", async (metadataFails) => { const scratch = await mkdtemp(join(tmpdir(), "flow-trace-wire-")); - const input = fixture(); + const nativeSession = { + id: "ses_root", + location: { directory: scratch }, + projectID: "fixture", + agent: "build", + cost: 0, + tokens: { + input: 0, + output: 0, + reasoning: 0, + cache: { read: 0, write: 0 }, + }, + time: { created: 1, updated: 2 }, + }; + const nativeMessages = [ + { + id: "msg_user", + type: "user", + text: "Implement approved task", + time: { created: 1 }, + metadata: { "opencode-plugin-flow/auto": "fixture-token" }, + }, + { + id: "msg_assistant", + type: "assistant", + agent: "build", + model: { id: "fixture", providerID: "fake" }, + time: { created: 2 }, + content: [ + { + type: "tool", + id: "call_one", + name: "flow_status", + time: { created: 2, completed: 3 }, + state: { + status: "completed", + input: {}, + content: [{ type: "text", text: "{}" }], + }, + }, + ], + }, + ]; const requests: string[] = []; const server = Bun.serve({ hostname: "127.0.0.1", @@ -499,24 +552,30 @@ test.each([false, true])( fetch(request) { const path = new URL(request.url).pathname; requests.push(path); - if (path === "/session/ses_root/children") return Response.json([]); - if (path === "/session/ses_root") + if (path === "/api/session") + return Response.json({ data: [], cursor: {} }); + if (path === "/api/session/ses_root") return metadataFails ? new Response("Unavailable", { status: 503 }) - : Response.json({ id: "ses_root", directory: scratch }); - if (path === "/session/ses_root/message") - return Response.json(required(input.sessionMessages[0]).messages); + : Response.json({ data: nativeSession }); + if (path === "/api/session/ses_root/message") + return Response.json({ data: nativeMessages, cursor: {} }); return new Response("Unknown", { status: 404 }); }, }); try { const host = Reflect.construct(EvalHost, [scratch, scratch]) as EvalHost; - Object.assign(host, { baseUrl: `http://127.0.0.1:${server.port}` }); + Object.assign(host, { + baseUrl: `http://127.0.0.1:${server.port}`, + nativeClient: OpenCode.make({ + baseUrl: `http://127.0.0.1:${server.port}`, + }), + }); const outcome = await host.outcome(["ses_root"], 10); expect(requests).toEqual([ - "/session/ses_root/children", - "/session/ses_root", - "/session/ses_root/message", + "/api/session", + "/api/session/ses_root", + "/api/session/ses_root/message", ]); expect(outcome.hostTrace?.kind).toBe( metadataFails ? "unavailable" : "observed", @@ -526,7 +585,7 @@ test.each([false, true])( expect(gradeInput.allCalls[0]?.native).toEqual({ sessionId: "ses_root", messageId: "msg_assistant", - partId: "prt_tool", + version: 2, partIndex: 0, callId: "call_one", startedAt: 2, @@ -581,7 +640,8 @@ test.each([false, true])( test("retained schema rejects resealed unknown-parent and cyclic child facts", () => { const trace = collectHostTrace(fixture()); - if (trace.kind !== "observed") throw new Error("Expected native facts"); + if (trace.kind !== "observed" || trace.version !== 1) + throw new Error("Expected native facts"); for (const parentId of ["unknown-parent", "ses_child"]) { const mutated = { ...trace, @@ -630,7 +690,8 @@ test("retained schema rejects resealed unknown-parent and cyclic child facts", ( test("retained schema rejects impossible user part fields and missing first-part order", () => { const trace = collectHostTrace(fixture()); - if (trace.kind !== "observed") throw new Error("Expected native facts"); + if (trace.kind !== "observed" || trace.version !== 1) + throw new Error("Expected native facts"); const initial = trace.messages[0]; if (initial?.role !== "user") throw new Error("Expected native user"); const first = required(initial.parts[0]); diff --git a/tests/eval-native-auth-config.test.ts b/tests/eval-native-auth-config.test.ts new file mode 100644 index 00000000..00bb1c01 --- /dev/null +++ b/tests/eval-native-auth-config.test.ts @@ -0,0 +1,99 @@ +import { afterEach, expect, test } from "bun:test"; +import { chmod, link, mkdtemp, rm, symlink, writeFile } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import type { NativeAccountConnection } from "../evals/native-auth/protocol.js"; +import { + nativeAuthenticationFromEnvironment, + readNativeAccountConnection, +} from "../evals/native-auth-config.js"; + +const roots: string[] = []; +afterEach(async () => { + await Promise.all( + roots.splice(0).map((root) => rm(root, { recursive: true, force: true })), + ); +}); +const configuration = { + version: 1, + url: "http://127.0.0.1:43123/", + capability: "f".repeat(64), + accounts: [ + { + route: { + providerID: "fixture", + integrationID: "fixture-native", + credentialID: "fixture-account", + methodID: "fixture-method", + label: "Synthetic account", + }, + }, + ], +} satisfies NativeAccountConnection; +async function fixture() { + const directory = await mkdtemp(join(tmpdir(), "native-auth-config-")); + roots.push(directory); + await chmod(directory, 0o700); + const path = join(directory, "configuration.json"); + await writeFile(path, JSON.stringify(configuration), { mode: 0o600 }); + return { directory, path }; +} + +test("native account configuration remains disabled unless explicitly selected", async () => { + expect( + await nativeAuthenticationFromEnvironment(["fixture/model"], {}), + ).toBeUndefined(); +}); + +test("private native account configuration loads without opening a source connection", async () => { + const { path } = await fixture(); + const models = ["fixture/model"]; + const loaded = await nativeAuthenticationFromEnvironment(models, { + FLOW_EVAL_NATIVE_AUTH: path, + }); + models.push("unapproved/model"); + expect(loaded).toEqual({ configuration, models: ["fixture/model"] }); + expect(await readNativeAccountConnection(path)).toEqual(configuration); +}); + +for (const mode of [ + "public-directory", + "public-file", + "symlink", + "hardlink", + "oversized", + "invalid-json", + "invalid-connection", +] as const) { + test(`native account configuration rejects ${mode} with a generic error`, async () => { + const { directory, path } = await fixture(); + let selected = path; + if (mode === "public-directory") await chmod(directory, 0o755); + if (mode === "public-file") await chmod(path, 0o640); + if (mode === "symlink") { + selected = join(directory, "linked.json"); + await symlink(path, selected); + } + if (mode === "hardlink") await link(path, join(directory, "linked.json")); + if (mode === "oversized") + await writeFile(path, "x".repeat(1024 * 1024 + 1)); + if (mode === "invalid-json") + await writeFile(path, '{"private-value":"synthetic-do-not-echo'); + if (mode === "invalid-connection") + await writeFile( + path, + JSON.stringify({ ...configuration, url: "https://example.invalid/" }), + ); + let message = ""; + try { + await readNativeAccountConnection(selected); + } catch (error) { + message = String(error); + } + expect(message).toBe( + "NativeAuthError: Native account authentication failed.", + ); + expect(message).not.toContain("synthetic-do-not-echo"); + expect(message).not.toContain(path); + }); +} diff --git a/tests/eval-observed-progress.test.ts b/tests/eval-observed-progress.test.ts index 75207e62..290f5290 100644 --- a/tests/eval-observed-progress.test.ts +++ b/tests/eval-observed-progress.test.ts @@ -2,6 +2,11 @@ import { expect, spyOn, test } from "bun:test"; import { mkdtemp, rm } from "node:fs/promises"; import { homedir, tmpdir } from "node:os"; import { join } from "node:path"; +import type { + SessionInterruptResponse, + SessionMessageAssistant, + SessionMessageInfo, +} from "@opencode/client"; import { deriveEnvironmentReserveState } from "../evals/environment-reserves.js"; import { deriveRetainedFailure, @@ -10,7 +15,7 @@ import { retainedFailureObservation, ScenarioGradeInputSchema, } from "../evals/grader-input.js"; -import { EvalHost, type Outcome } from "../evals/harness.js"; +import type { EvalHost, Outcome } from "../evals/harness.js"; import { redactTranscript } from "../evals/provenance.js"; import { releaseCatalog, releaseCellsFor } from "../evals/release-policy.js"; import { deriveReleaseRunState } from "../evals/release-progress.js"; @@ -20,6 +25,21 @@ import type { ScheduledCell, } from "../evals/report.js"; +import { + fixtureModel, + messagePage, + nativeAssistant, + nativeHost, + nativeHttpClient, + nativeSession, + nativeTool, + sessionPage, + sessionReply, + useNativeFixtureBudget, +} from "./fixtures/native-eval-client.js"; + +useNativeFixtureBudget(); + type Mode = | "text" | "reasoning" @@ -55,7 +75,10 @@ const model = { }; async function observe( mode: Mode, - afterWait?: (host: EvalHost) => Promise, + afterWait?: ( + host: EvalHost, + cancel: (reason: Error) => void, + ) => Promise, outcomeRoots: readonly string[] = [root], ) { const project = await mkdtemp(join(tmpdir(), "flow-observed-progress-")); @@ -84,96 +107,75 @@ async function observe( : `Inspect ${root} in ${project} and ${homedir()}/fixture-review.`, apiKey: "synthetic-private-token-for-redaction", }; - const user = (sessionID: string) => ({ - info: { - id: sessionID === root ? "msg_rootuser" : "msg_childuser", - sessionID, - role: "user", - time: { created: 1 }, - }, - parts: [ - { - id: sessionID === root ? "prt_rootprompt" : "prt_childprompt", - sessionID, - messageID: sessionID === root ? "msg_rootuser" : "msg_childuser", - type: "text", - text: "Inspect the fixture.", - }, - ], + const user = (session: string): SessionMessageInfo => ({ + type: "user", + id: session === root ? "msg_rootuser" : "msg_childuser", + time: { created: 1 }, + text: "Inspect the fixture.", }); - const info = (sessionID: string, id: string, history = false) => ({ - id, - sessionID, - parentID: sessionID === root ? "msg_rootuser" : "msg_childuser", - role: "assistant", - agent: sessionID === root ? "build" : "flow-reviewer", - summary: false, - providerID: "openai", - modelID: "gpt-6.1-sol", - time: { - created: 2, - ...(history && !(mode === "history-reopened" && now >= 100_000) - ? { completed: 3 } + const assistant = (session: string, id: string, history = false) => + nativeAssistant(id, { + agent: session === root ? "build" : "flow-reviewer", + model: { providerID: "openai", id: "gpt-6.1-sol" }, + time: { + created: 2, + ...(history && !(mode === "history-reopened" && now >= 100_000) + ? { completed: 3 } + : completed() + ? { completed: 210_000 } + : {}), + }, + }); + const rootMessages = (): SessionMessageInfo[] => { + const task = nativeTool( + "call_task", + "task", + aborted + ? { + status: "error", + input: taskInput, + error: { type: "aborted", message: "Task cancelled" }, + } : completed() - ? { completed: 210_000 } - : {}), - }, - }); - const rootMessages = () => { - const entries = [ - user(root), - { - info: info(root, "msg_root"), - parts: [ - { - id: "prt_task", - sessionID: mode === "foreign-root-part" ? "ses_foreign" : root, - messageID: "msg_root", - type: "tool", - tool: "task", - callID: "call_task", - state: { - status: aborted ? "error" : completed() ? "completed" : "running", + ? { + status: "completed", + input: taskInput, + content: [{ type: "text", text: "review complete" }], + } + : { + status: "running", input: taskInput, - ...(aborted ? { error: "Task cancelled" } : {}), metadata: { sessionId: child, parentSessionId: root, - model: { providerID: "openai", modelID: "gpt-6.1-sol" }, + model: { providerID: "openai", id: "gpt-6.1-sol" }, heartbeat: mode === "metadata" ? now : 0, }, - time: { - start: 2, - ...(aborted - ? { end: now } - : completed() - ? { end: 210_000 } - : {}), - }, }, - }, - ], + { + created: 2, + ...(aborted + ? { completed: now } + : completed() + ? { completed: 210_000 } + : {}), }, - ]; - const part = entries[1]?.parts[0]; - if (!part) throw new Error("Missing root fixture part."); + ); if (!aborted && !completed()) { - if (mode === "object-call") - Reflect.set(part, "callID", { heartbeat: now }); - if (mode === "object-tool") Reflect.set(part, "tool", { heartbeat: now }); + if (mode === "object-call") Reflect.set(task, "id", { heartbeat: now }); + if (mode === "object-tool") Reflect.set(task, "name", { heartbeat: now }); } - return entries; + const entry = { ...assistant(root, "msg_root"), content: [task] }; + if (mode === "foreign-root-part") entry.id = "msg_child"; + return [user(root), entry]; }; - const childMessages = () => { + const childMessages = (): SessionMessageInfo[] => { const tick = Math.floor(Math.min(now, 210_000) / 2_000); const history = [ "completed-history", "history-new-part", "history-reopened", ].includes(mode); - const input = { - page: mode === "pending-input" || mode === "running-input" ? tick : 0, - }; const tool = [ "pending-raw", "pending-input", @@ -181,139 +183,157 @@ async function observe( "running-output", "object-pending-raw", ].includes(mode); - const part = { - id: "prt_stream", - sessionID: mode === "foreign-child-part" ? "ses_foreign" : child, - messageID: "msg_child", - type: tool ? "tool" : mode === "reasoning" ? "reasoning" : "text", - ...(tool - ? { - tool: "read", - callID: "call_read", - state: { - status: completed() - ? "completed" - : mode.startsWith("pending") || mode === "object-pending-raw" - ? "pending" - : "running", + let part: SessionMessageAssistant["content"][number]; + if (tool) { + const input = { + page: mode === "pending-input" || mode === "running-input" ? tick : 0, + }; + part = nativeTool( + "call_read", + "read", + completed() + ? { + status: "completed", input, - raw: mode === "pending-raw" ? `page:${tick}` : "page:0", - output: - mode === "running-output" - ? `unsupported running output:${now}` - : "", - time: { start: 2, ...(completed() ? { end: 210_000 } : {}) }, - }, - } - : { - text: - mode === "replacement" - ? `page${tick % 2}` - : mode === "text" || - mode === "reasoning" || - mode === "deadline" || - mode === "completed-history" - ? `evidence ${now}` - : "evidence", - time: { - start: mode === "timestamps" ? now : 2, - ...(history && !(mode === "history-reopened" && now >= 100_000) - ? { end: 3 } - : completed() - ? { end: 210_000 } - : {}), - }, - }), - metadata: { heartbeat: now }, - }; - const extra = + content: [{ type: "text", text: "read complete" }], + } + : mode.startsWith("pending") || mode === "object-pending-raw" + ? { + status: "streaming", + input: + mode === "pending-input" + ? JSON.stringify(input) + : mode === "pending-raw" + ? `page:${tick}` + : "page:0", + } + : { status: "running", input, metadata: {} }, + { created: 2, ...(completed() ? { completed: 210_000 } : {}) }, + ); + if (mode === "running-output") + Reflect.set(part.state, "content", [ + { type: "text", text: `unsupported running output:${now}` }, + ]); + if (mode === "object-pending-raw" && !aborted && !completed()) + Reflect.set(part.state, "input", { heartbeat: now }); + } else { + part = { + type: mode === "reasoning" ? "reasoning" : "text", + text: + mode === "replacement" + ? `page${tick % 2}` + : ["text", "reasoning", "deadline", "completed-history"].includes( + mode, + ) + ? `evidence ${now}` + : "evidence", + }; + if (mode === "timestamps") Reflect.set(part, "time", { created: now }); + } + const extra: SessionMessageAssistant["content"] = mode === "history-new-part" ? Array.from({ length: tick }, (_, index) => ({ - id: `prt_history_${index}`, - sessionID: child, - messageID: "msg_child", type: "text", text: `old ${index}`, - time: { start: 2, end: 3 }, })) : mode === "duplicate-part" - ? [part] + ? [ + nativeTool("call_read", "read", { + status: "running", + input: {}, + metadata: {}, + }), + nativeTool("call_read", "read", { + status: "running", + input: {}, + metadata: {}, + }), + ] : []; const entry = { - info: info(child, "msg_child", history), - parts: [part, ...extra], + ...assistant(child, "msg_child", history), + content: [part, ...extra], }; if (!aborted && !completed()) { - if (mode === "numeric-message") { - Reflect.set(entry.info, "id", 42); - Reflect.set(part, "messageID", 42); - } - if (mode === "numeric-part") Reflect.set(part, "id", 42); - if (mode === "object-pending-raw") { - const state: unknown = Reflect.get(part, "state"); - if (state && typeof state === "object") - Reflect.set(state, "raw", { heartbeat: now }); - } + if (mode === "numeric-message") Reflect.set(entry, "id", 42); + if (mode === "numeric-part") + entry.content = [ + nativeTool("call_read", "read", { + status: "running", + input: {}, + metadata: {}, + }), + ]; + if (mode === "numeric-part" && entry.content[0]) + Reflect.set(entry.content[0], "id", 42); } + if (mode === "foreign-child-part") entry.id = "msg_root"; return [user(child), entry]; }; - const server = Bun.serve({ - port: 0, - fetch(request) { - const path = new URL(request.url).pathname; - if (path === `/session/${root}/abort`) { - aborts++; - aborted = true; - return Response.json(true); - } - if (path === `/session/${root}/children`) - return Response.json([ - { - id: child, - parentID: root, - directory: project, - agent: "flow-reviewer", - }, - ]); - if (path === `/session/${child}/children`) return Response.json([]); - if (path === `/session/${root}`) - return Response.json({ id: root, directory: project }); - if (path === `/session/${child}`) - return Response.json({ - id: child, + const server = nativeHttpClient(({ url, method }) => { + const path = url.pathname; + if (path === `/api/session/${root}/interrupt`) { + expect(method).toBe("POST"); + expect(url.searchParams.get("resume")).toBe("false"); + aborts++; + aborted = true; + return Response.json({ + interrupted: true, + } satisfies SessionInterruptResponse); + } + if ( + path === `/api/session/${root}/model` || + path === `/api/session/${root}/command` + ) + return new Response(null, { status: 204 }); + if (path === "/api/session") + return sessionPage([ + nativeSession(root, project, { + model: { providerID: "openai", id: "gpt-6.1-sol" }, + }), + nativeSession(child, project, { parentID: root, - directory: project, agent: "flow-reviewer", - }); - if (path === `/session/${root}/message`) - return Response.json(rootMessages()); - if (path === `/session/${child}/message`) - return Response.json(childMessages()); - return new Response("Unexpected fixture endpoint", { status: 404 }); - }, + model: { providerID: "openai", id: "gpt-6.1-sol" }, + }), + ]); + if (path === `/api/session/${root}`) + return sessionReply( + nativeSession(root, project, { + model: { providerID: "openai", id: "gpt-6.1-sol" }, + }), + ); + if (path === `/api/session/${child}`) + return sessionReply( + nativeSession(child, project, { + parentID: root, + agent: "flow-reviewer", + model: { providerID: "openai", id: "gpt-6.1-sol" }, + }), + ); + if (path === `/api/session/${root}/message`) + return messagePage(rootMessages()); + if (path === `/api/session/${child}/message`) + return messagePage(childMessages()); + throw new Error(`Unexpected native fixture endpoint ${method} ${path}`); }); const clock = spyOn(Date, "now").mockImplementation(() => now); const sleep = spyOn(Bun, "sleep").mockImplementation(async () => { now += 2_000; }); try { - const host = Reflect.construct(EvalHost, [project, project]) as EvalHost; - Object.assign(host, { baseUrl: server.url.toString().replace(/\/$/, "") }); - let result: unknown; - try { - result = await Reflect.get(host, "waitForQuiet").call(host, root, { - request: { state: () => ({ kind: "accepted" }), cancel: () => {} }, - }); - } catch (error) { - result = error; - } - await afterWait?.(host); + const controller = new AbortController(); + const host = nativeHost(project, server.client, controller.signal); + const result: unknown = await host + .runCommand(root, "flow-auto", "fixture", fixtureModel) + .catch((error: unknown) => error); + await afterWait?.(host, (reason) => controller.abort(reason)); const outcome = await host.outcome(outcomeRoots, now); return { result, now, aborts, outcome, project }; } finally { sleep.mockRestore(); clock.mockRestore(); - server.stop(true); + server.stop(); await rm(project, { recursive: true, force: true }); } } @@ -509,7 +529,7 @@ for (const mutation of [ if (mutation === "stale-observation") observation.observedAt = task.native.startedAt; if (mutation === "input") Reflect.set(task, "input", { different: true }); - if (mutation === "native") task.native.partId = "prt_different"; + if (mutation === "native") task.native.messageId = "msg_different"; if (mutation === "completed") Reflect.set(task, "status", "completed"); if (mutation === "empty") observation.pending = []; if (mutation === "trace") @@ -634,18 +654,14 @@ test("replayed native abort proof continues scheduling and uses only the predecl for (const method of ["runCommand", "runPrompt"] as const) { test(`a new ${method} that fails before dispatch cannot borrow prior abort proof`, async () => { - const observed = await observe("silent", async (host) => { + const observed = await observe("silent", async (host, cancel) => { const before = await host.outcome([root], 182000); expect(Reflect.get(before, "abortObservation")).toBeDefined(); - Object.assign(host, { - verifyArtifacts: async () => { - throw new Error("Fixture pre-dispatch rejection."); - }, - }); + cancel(new Error("Fixture pre-dispatch rejection.")); const request = method === "runCommand" - ? host.runCommand(root, "flow-auto", "fixture", "openai/gpt-6.1-sol") - : host.runPrompt(root, "fixture", "openai/gpt-6.1-sol"); + ? host.runCommand(root, "flow-auto", "fixture", fixtureModel) + : host.runPrompt(root, "fixture", fixtureModel); await expect(request).rejects.toThrow("Fixture pre-dispatch rejection."); }); expect(Reflect.get(observed.outcome, "abortObservation")).toBeUndefined(); diff --git a/tests/eval-progress-wait.test.ts b/tests/eval-progress-wait.test.ts index 050177d1..f4b66e16 100644 --- a/tests/eval-progress-wait.test.ts +++ b/tests/eval-progress-wait.test.ts @@ -1,13 +1,26 @@ import { expect, spyOn, test } from "bun:test"; -import { mkdtemp, rm } from "node:fs/promises"; +import { mkdir, mkdtemp, rm } from "node:fs/promises"; import { tmpdir } from "node:os"; import { join } from "node:path"; +import type { + SessionInterruptResponse, + SessionMessageInfo, +} from "@opencode/client"; import { CampaignCancelled } from "../evals/campaign-stop.js"; import { - EvalHost, - postSessionJson, - runSessionRequest, -} from "../evals/harness.js"; + fixtureModel, + messagePage, + nativeAssistant, + nativeClient, + nativeHost, + nativeSession, + nativeTool, + sessionPage, + sessionReply, + useNativeFixtureBudget, +} from "./fixtures/native-eval-client.js"; + +useNativeFixtureBudget(); type Mode = | "owned-progress" @@ -35,18 +48,14 @@ type Mode = async function observeWait(mode: Mode) { const project = await mkdtemp(join(tmpdir(), "flow-progress-wait-")); + const outside = join(project, "..", `${project.split("/").at(-1)}-outside`); + await mkdir(outside); const controller = new AbortController(); const cancelled = new CampaignCancelled(143); - const requestTimeout = new DOMException( + const transportTimeout = new DOMException( "Child HTTP request timed out", "TimeoutError", ); - const host = Reflect.construct(EvalHost, [ - project, - project, - controller.signal, - ]) as EvalHost; - Object.assign(host, { baseUrl: "http://progress-fixture" }); let now = 0; let aborts = 0; let childReads = 0; @@ -93,42 +102,42 @@ async function observeWait(mode: Mode) { now += 40_000; slept = true; }); - const childMetadata = { - id: "ses_child", - parentID: mode === "wrong-parent" ? "ses_unrelated" : "ses_root", - directory: mode === "wrong-directory" ? `${project}-unrelated` : project, - agent: "flow-reviewer", - }; - const rootMessages = () => [ + const root = nativeSession("ses_root", project); + const child = nativeSession( + "ses_child", + mode === "wrong-directory" ? outside : project, { - info: { - id: "msg_root", - sessionID: "ses_root", - role: "assistant", - agent: "build", - time: { - created: 1, - ...(finishes && now >= finishesAt ? { completed: finishesAt } : {}), - }, - }, - parts: [ - { - id: "prt_task", - messageID: "msg_root", - sessionID: "ses_root", - type: "tool", - tool: "task", - callID: "call_task", - state: { - status: finishes && now >= finishesAt ? "completed" : "running", - input: { subagent_type: "flow-reviewer" }, - time: { start: 1 }, - }, - }, - ], + parentID: mode === "wrong-parent" ? "ses_unrelated" : "ses_root", + agent: "flow-reviewer", }, - ]; - const childMessages = () => + ); + const rootMessages = (): SessionMessageInfo[] => { + const done = finishes && now >= finishesAt; + return [ + nativeAssistant("msg_root", { + time: { created: 1, ...(done ? { completed: finishesAt } : {}) }, + content: [ + nativeTool( + "call_task", + "task", + done + ? { + status: "completed", + input: { subagent_type: "flow-reviewer" }, + content: [{ type: "text", text: "review complete" }], + } + : { + status: "running", + input: { subagent_type: "flow-reviewer" }, + metadata: {}, + }, + { created: 1, ...(done ? { completed: finishesAt } : {}) }, + ), + ], + }), + ]; + }; + const childMessages = (): SessionMessageInfo[] => Array.from( { length: @@ -137,163 +146,157 @@ async function observeWait(mode: Mode) { : Math.floor(Math.min(now, finishes ? finishesAt : now) / 16_000) + 1, }, - (_, index) => ({ - info: { - id: `msg_child_${index}`, - sessionID: "ses_child", - role: "assistant", + (_, index) => + nativeAssistant(`msg_child_${index}`, { agent: "flow-reviewer", time: { created: index * 16_000, completed: index * 16_000 + 1 }, - }, - parts: [ - { - id: `prt_read_${index}`, - messageID: `msg_child_${index}`, - sessionID: "ses_child", - type: "tool", - tool: "read", - callID: `call_read_${index}`, - state: { - status: "completed", - input: { filePath: "src/index.ts" }, - output: - mode === "in-place-progress" - ? `source page updated at ${now}` - : `source page ${index}`, - }, - }, - ], - }), - ); - const transport = spyOn(globalThis, "fetch").mockImplementation( - Object.assign( - async (input: Parameters[0], init?: RequestInit) => { - const path = new URL(String(input)).pathname; - visits.push(path); - if (path.endsWith("/abort")) { - aborts++; - return Response.json(true); - } - if (path === "/session/ses_root/command") return Response.json({}); - if (path === "/session/ses_root/message") { - if (mode === "clock-boundary" && now >= 60_000 && !clockBoundary) { - now = 59_999; - clockBoundary = true; - } - return Response.json(rootMessages()); - } - if (path === "/session/ses_root") - return Response.json({ id: "ses_root", directory: project }); - if ( - path === "/session/ses_root/children" && - mode === "async-deadline" && - now >= 60_000 - ) { - const signal = init?.signal; - if (!signal) throw new Error("Missing observer cancellation signal"); - return new Promise((_, reject) => { - if (signal.aborted) reject(signal.reason); - else - signal.addEventListener("abort", () => reject(signal.reason), { - once: true, - }); - }); - } - if ( - path === "/session/ses_root/children" && - mode === "malformed-endpoint" - ) - return Response.json({ unavailable: true }); - if (path === "/session/ses_root/children") - return Response.json( - mode === "tree-limit" - ? Array.from({ length: 128 }, (_, index) => ({ - ...childMetadata, - id: `ses_child_${index}`, - })) - : mode === "duplicate-children" - ? [childMetadata, childMetadata] - : mode === "malformed-children" - ? [null, {}, { ...childMetadata, id: 42 }] - : [childMetadata], - ); - if (path === "/session/ses_child") return Response.json(childMetadata); - if ( - path === "/session/ses_child/children" && - mode === "cyclic-children" - ) - return Response.json([ - { id: "ses_root", parentID: "ses_child", directory: project }, - ]); - if ( - path === "/session/ses_child/children" || - /\/session\/ses_child_\d+\/children/.test(path) - ) - return Response.json([]); - if (path === "/session/ses_child/message") { - childReads++; - if (mode === "request-timeout") throw requestTimeout; - if ( - (mode === "deadline-suspend" || mode === "cancel-suspend") && - childReads === 1 - ) { - now += 80_000; - if (mode === "cancel-suspend") controller.abort(cancelled); - else - observerDeadline?.abort( - new DOMException( - "Observer elapsed during suspension", - "TimeoutError", - ), - ); - if (!init?.signal?.aborted) - throw new Error("Fixture observer signal did not abort"); - throw init.signal.reason; - } - if ( - (mode === "child-suspend" || mode === "combined-credit") && - childReads === 1 - ) - now += 40_000; - if (mode === "credit-cap" && childReads <= 3) now += 40_000; - if (mode === "cancel-child") controller.abort(cancelled); - if (mode === "foreign-message") - return Response.json([ + content: [ + nativeTool( + `call_read_${index}`, + "read", { - info: { sessionID: "ses_unrelated", role: "assistant" }, - parts: [], + status: "completed", + input: { filePath: "src/index.ts" }, + content: [ + { + type: "text", + text: + mode === "in-place-progress" + ? `source page updated at ${now}` + : `source page ${index}`, + }, + ], }, - ]); - return Response.json(childMessages()); + { created: index * 16_000, completed: index * 16_000 + 1 }, + ), + ], + }), + ); + const client = nativeClient(async ({ url, init, method }) => { + const path = url.pathname; + visits.push(path); + if (path === "/api/session/ses_root/interrupt") { + expect(method).toBe("POST"); + expect(url.searchParams.get("resume")).toBe("false"); + aborts++; + return Response.json({ + interrupted: true, + } satisfies SessionInterruptResponse); + } + if ( + path === "/api/session/ses_root/model" || + path === "/api/session/ses_root/command" + ) + return new Response(null, { status: 204 }); + if (path === "/api/session/ses_root/message") { + if (mode === "clock-boundary" && now >= 60_000 && !clockBoundary) { + now = 59_999; + clockBoundary = true; + } + return messagePage(rootMessages()); + } + if (path === "/api/session/ses_root") return sessionReply(root); + if (path === "/api/session") { + if (mode === "async-deadline" && now >= 60_000) { + const signal = init.signal; + if (!signal) throw new Error("Missing observer cancellation signal"); + return new Promise((_, reject) => { + if (signal.aborted) reject(signal.reason); + else + signal.addEventListener("abort", () => reject(signal.reason), { + once: true, + }); + }); + } + if (mode === "malformed-endpoint") + return Response.json({ unavailable: true }); + if (mode === "malformed-children") + return Response.json({ + data: [null, {}, { ...child, id: 42 }], + cursor: {}, + }); + if (mode === "tree-limit") + return sessionPage([ + root, + ...Array.from({ length: 128 }, (_, index) => ({ + ...child, + id: `ses_child_${index}`, + })), + ]); + if (mode === "duplicate-children") + return sessionPage([root, child, child]); + if (mode === "cyclic-children") + return sessionPage([{ ...root, parentID: child.id }, child]); + return sessionPage([root, child]); + } + if (path === "/api/session/ses_child/message") { + childReads++; + if (mode === "request-timeout") throw transportTimeout; + if ( + (mode === "deadline-suspend" || mode === "cancel-suspend") && + childReads === 1 + ) { + now += 80_000; + if (mode === "cancel-suspend") controller.abort(cancelled); + else + observerDeadline?.abort( + new DOMException( + "Observer elapsed during suspension", + "TimeoutError", + ), + ); + if (!init.signal?.aborted) + throw new Error("Fixture observer signal did not abort"); + throw init.signal.reason; + } + if ( + (mode === "child-suspend" || mode === "combined-credit") && + childReads === 1 + ) + now += 40_000; + if (mode === "credit-cap" && childReads <= 3) now += 40_000; + if (mode === "cancel-child") controller.abort(cancelled); + if (mode === "foreign-message") { + return messagePage([ + nativeAssistant("msg_root", { agent: "flow-reviewer" }), + ]); + } + return messagePage(childMessages()); + } + if (/^\/api\/session\/ses_child_\d+\/message$/.test(path)) + return messagePage([]); + throw new Error(`Unexpected native progress endpoint ${method} ${path}`); + }); + let requestTimeout: unknown = transportTimeout; + const list = client.message.list; + const messageReads = spyOn(client.message, "list").mockImplementation( + async (input, options) => { + try { + return await list(input, options); + } catch (error) { + if (mode === "request-timeout") { + expect(error).toBeInstanceOf(Error); + if (!(error instanceof Error)) throw error; + expect(error.cause).toBe(transportTimeout); + requestTimeout = error; } - if (/\/session\/ses_child_\d+\/message/.test(path)) - return Response.json([]); - throw new Error(`Unexpected progress endpoint ${path}`); - }, - { preconnect: fetch.preconnect }, - ), + throw error; + } + }, ); + const host = nativeHost(project, client, controller.signal); try { - const result = await runSessionRequest({ - url: "http://progress-fixture/session/ses_root/command", - body: {}, - post: postSessionJson, - signal: controller.signal, - waitOwnsCancellation: true, - onRejected: () => {}, - onCancelled: () => - Reflect.apply(Reflect.get(host, "abortSession"), host, ["ses_root"]), - wait: (request) => - Reflect.apply(Reflect.get(host, "waitForQuiet"), host, [ - "ses_root", - { - request, - ...(timing - ? { timeoutMs: mode === "combined-credit" ? 120_000 : 60_000 } - : {}), - }, - ]), - }).catch((error: unknown) => error); + const result: unknown = await host + .runCommand( + "ses_root", + "flow-auto", + "fixture", + fixtureModel, + timing + ? { timeoutMs: mode === "combined-credit" ? 120_000 : 60_000 } + : {}, + ) + .catch((error: unknown) => error); return { result, now, @@ -304,12 +307,12 @@ async function observeWait(mode: Mode) { requestTimeout, }; } finally { + messageReads.mockRestore(); timeouts.mockRestore(); - transport.mockRestore(); sleep.mockRestore(); clock.mockRestore(); - await host.stop(); await rm(project, { recursive: true, force: true }); + await rm(outside, { recursive: true, force: true }); } } @@ -319,7 +322,7 @@ test("owned reviewer progress prevents a false root-task wedge beyond three minu expect(observed.now).toBeGreaterThanOrEqual(210_000); expect(observed.now).toBeLessThanOrEqual(240_000); expect(observed.aborts).toBe(0); - expect(observed.visits).toContain("/session/ses_child/message"); + expect(observed.visits).toContain("/api/session/ses_child/message"); }); for (const mode of [ @@ -337,7 +340,7 @@ for (const mode of [ expect(observed.now).toBe(182_000); expect(observed.aborts).toBe(1); if (mode !== "wedged") - expect(observed.visits).not.toContain("/session/ses_child/message"); + expect(observed.visits).not.toContain("/api/session/ses_child/message"); }); } @@ -369,10 +372,11 @@ for (const mode of ["duplicate-children", "cyclic-children"] as const) { expect(observed.result).toBe("quiet"); expect(observed.aborts).toBe(0); expect( - observed.visits.filter((path) => path === "/session/ses_child/message") - .length, + observed.visits.filter( + (path) => path === "/api/session/ses_child/message", + ).length, ).toBe( - observed.visits.filter((path) => path === "/session/ses_root/message") + observed.visits.filter((path) => path === "/api/session/ses_root/message") .length, ); }); diff --git a/tests/eval-release-sampling.test.ts b/tests/eval-release-sampling.test.ts index ba89c3bc..d6943b5b 100644 --- a/tests/eval-release-sampling.test.ts +++ b/tests/eval-release-sampling.test.ts @@ -185,9 +185,10 @@ describe("release eval sampling", () => { test("checks configured CI release models before authorization", async () => { for (const [configured, expectedCode] of [ - ["openai/gpt-6.1-sol", 0], + ["openai/gpt-6.1-sol", 1], ["openai/gpt-6-sol", 1], - ["openai/gpt-6.1-sol,xai/grok-4.6", 1], + ["openai/gpt-6.1-sol,xai/grok-4.7", 0], + ["openai/gpt-6.1-sol,openai/gpt-6-sol", 1], ["xai/grok-4.6", 1], ] as const) { const child = Bun.spawn( @@ -205,7 +206,7 @@ describe("release eval sampling", () => { expect(code).toBe(expectedCode); if (expectedCode !== 0) expect(stderr).toContain( - `Release ${packageJson.version} requires exactly openai/gpt-6.1-sol`, + "Release requires exactly 2 models on distinct route providers.", ); } }); @@ -344,18 +345,16 @@ describe("release eval sampling", () => { ).toHaveLength(57); }); - test("current 9.6 candidate retains all seventeen cases on its OpenAI-only grid", () => { - const scenarios = releaseScenarios(); + test("preserves the 9.6 candidate with all seventeen cases on its OpenAI-only grid", () => { + const scenarios = releaseScenarios("9.6.0"); expect(scenarios).toHaveLength(17); - expect( - releaseCatalog(packageJson.version).every( - (row) => row.minProviders === 1, - ), - ).toBe(true); + expect(releaseCatalog("9.6.0").every((row) => row.minProviders === 1)).toBe( + true, + ); const plan = campaignPlanFor({ models: ["openai/gpt-6.1-sol"], scenarios, - sampling: { kind: "release", packageVersion: packageJson.version }, + sampling: { kind: "release", packageVersion: "9.6.0" }, opencodeVersion: "1.18.31", }); expect(plan.stoppingRule.count).toBe(72); @@ -368,12 +367,32 @@ describe("release eval sampling", () => { campaignPlanFor({ models: ["openai/gpt-6-sol"], scenarios, - sampling: { kind: "release", packageVersion: packageJson.version }, + sampling: { kind: "release", packageVersion: "9.6.0" }, opencodeVersion: "1.18.31", }), ).toThrow("openai/gpt-6.1-sol"); }); + test("schedules the current two-provider seventeen-case grid", () => { + const scenarios = releaseScenarios(packageJson.version); + expect(scenarios).toHaveLength(17); + const plan = campaignPlanFor({ + models: ["openai/gpt-6.1-sol", "xai/grok-4.7"], + scenarios, + sampling: { kind: "release", packageVersion: packageJson.version }, + opencodeVersion: "2.1.4", + }); + expect(plan.stoppingRule.count).toBe(144); + expect(plan.budget.maxAttempts).toBe(178); + expect(plan.abortPolicy.maxReplacementBlocks).toBe(34); + expect( + plan.cells.filter((cell) => cell.schedule === "primary"), + ).toHaveLength(144); + expect( + plan.cells.filter((cell) => cell.schedule === "environment-reserve"), + ).toHaveLength(34); + }); + test("keeps ordinary scenario catalogs report-only", () => { const scenarios = SCENARIOS.filter((scenario) => ["happy-path", "skipped-case-named-binding"].includes(scenario.id), @@ -436,7 +455,7 @@ describe("release eval sampling", () => { } }); - test("rejects release grids outside the canonical OpenAI route", async () => { + test("rejects release grids without exactly two distinct route providers", async () => { for (const models of [ ["xai/a"], ["xai/a", "xai/b"], @@ -459,7 +478,7 @@ describe("release eval sampling", () => { ]); expect(exitCode).toBe(2); expect(stderr).toContain( - `Release ${packageJson.version} requires exactly openai/gpt-6.1-sol`, + "Release requires exactly 2 models on distinct route providers.", ); } }); @@ -473,6 +492,8 @@ describe("release eval sampling", () => { "--model", "openai/gpt-6.1-sol", "--release", + "--model", + "xai/grok-4.7", "--concurrency", "2", ], diff --git a/tests/eval-replay.test.ts b/tests/eval-replay.test.ts index 953e1eaf..eb5f576c 100644 --- a/tests/eval-replay.test.ts +++ b/tests/eval-replay.test.ts @@ -26,7 +26,10 @@ import { WORKSPACE_TOKEN, } from "../evals/cassette.js"; import { completionHonesty, type MetricSession } from "../evals/metrics.js"; -import { replayCassette } from "../evals/replay.js"; +import { + recordedBashDecisionFixture, + replayCassette, +} from "../evals/replay.js"; import { SCENARIOS } from "../evals/scenarios.js"; import { SessionSchema } from "../src/application/schema.js"; @@ -476,6 +479,7 @@ describe("decision-layer replay", () => { test("reproduces a passing happy-path run with no model and no host", async () => { const result = await replayCassette(happyPathCassette()); + expect(result.evidenceKind).toBe("historical-decision-replay"); expect(result.divergences).toEqual([]); // The whole point: the graders run against durable state the replay just // produced, not against anything the cassette carried. @@ -699,3 +703,24 @@ describe("cassette hygiene", () => { ); }); }); + +test("historical Bash decoding is a decision fixture with explicit completeness", () => { + const event = { + kind: "bash" as const, + sessionIndex: 0, + agent: "build", + command: "bun test", + output: "recorded output", + metadata: { exit: 7, truncated: false }, + }; + expect(recordedBashDecisionFixture(event).result).toEqual({ + content: "recorded output", + output: { exit: 7, truncated: false, output: "recorded output" }, + }); + expect( + recordedBashDecisionFixture({ ...event, metadata: {} }).result, + ).toEqual({ + content: "recorded output", + output: { output: "recorded output" }, + }); +}); diff --git a/tests/eval-reporting.test.ts b/tests/eval-reporting.test.ts index 5dd41053..821fffd4 100644 --- a/tests/eval-reporting.test.ts +++ b/tests/eval-reporting.test.ts @@ -50,6 +50,10 @@ import { } from "../evals/metrics.js"; import { bestEffortEvaluation } from "../evals/run.js"; import { authorizePaidRun } from "../scripts/paid-budget.js"; +import { + fileCredentialHandoff, + nativeTestClient, +} from "./fixtures/opencode-v2-test-host.js"; function processExists(pid: number): boolean { try { @@ -67,7 +71,18 @@ function mockFetch( ) => ReturnType, ) { return spyOn(globalThis, "fetch").mockImplementation( - Object.assign(implementation, { preconnect: fetch.preconnect }), + Object.assign( + (input: Parameters[0], init?: RequestInit) => + input instanceof Request + ? implementation(input.url, { + ...init, + method: input.method, + headers: input.headers, + signal: init?.signal ?? input.signal, + }) + : implementation(input, init), + { preconnect: fetch.preconnect }, + ), ); } @@ -207,12 +222,12 @@ wait "$child" // escalation nothing answers — the designed end of six scenarios — and // reporting its own abort as a condition of the host buried the real ones. test("does not report its own abort as a host error", () => { - const abort = { name: "MessageAbortedError", data: { message: "Aborted" } }; + const abort = { type: "aborted", message: "Aborted" }; expect(isSelfAbortError(abort, true)).toBe(true); }); test("reports an abort nobody here issued, and every other error always", () => { - const abort = { name: "MessageAbortedError", data: { message: "Aborted" } }; + const abort = { type: "aborted", message: "Aborted" }; // No abort issued makes an abort error real news: something outside this // process ended the turn, which is exactly what the field is for. expect(isSelfAbortError(abort, false)).toBe(false); @@ -493,7 +508,10 @@ describe("eval campaign cancellation", () => { scratch, scratch, ]) as EvalHost; - Object.assign(host, { baseUrl: "http://fixture" }); + Object.assign(host, { + baseUrl: "http://fixture", + nativeClient: nativeTestClient(), + }); const controller = new AbortController(); const reason = new CampaignCancelled(130); const entered = Promise.withResolvers(); @@ -521,29 +539,38 @@ describe("eval campaign cancellation", () => { return read(path, signal); }, }); - const requests = mockFetch(async (input) => - String(input).endsWith("/children") - ? Response.json([]) - : Response.json([ + const requests = mockFetch(async (input) => { + const path = new URL(String(input)).pathname; + if (path === "/api/session") + return Response.json({ data: [], cursor: {} }); + if (path.endsWith("/message")) + return Response.json({ + data: [ { - info: { - id: "message", - sessionID: "id", - role: "assistant", - time: { created: 1, completed: 2 }, - ...(failed - ? { - error: { - name: "FixtureProviderUnavailable", - data: { message: "Fake provider failure" }, - }, - } - : {}), - }, - parts: [], + id: "message", + type: "assistant", + agent: "build", + model: { providerID: "fixture", id: "model" }, + time: { created: 1, completed: 2 }, + content: [], + ...(failed + ? { + error: { + type: "FixtureProviderUnavailable", + message: "Fake provider failure", + }, + } + : {}), }, - ]), - ); + ], + cursor: {}, + }); + return Response.json({ + id: "id", + agent: "build", + location: { directory: scratch }, + }); + }); try { const result = host.outcome(["id"], 1, controller.signal); const observed = result.catch((error) => error); @@ -631,48 +658,49 @@ describe("eval campaign cancellation", () => { scratch, controller.signal, ]) as EvalHost; - Object.assign(host, { baseUrl: "http://fixture" }); + Object.assign(host, { + baseUrl: "http://fixture", + nativeClient: nativeTestClient(), + }); const aborting = Promise.withResolvers(); const cleanup = Promise.withResolvers(); let aborts = 0; const requests = mockFetch(async (input) => { const url = String(input); - if (url.endsWith("/abort")) { + const path = new URL(url).pathname; + if (path.endsWith("/interrupt")) { aborts += 1; aborting.resolve(); return cleanup.promise; } - if (url.endsWith("/command")) return Response.json({}); - if (url.endsWith("/children")) return Response.json([]); - return Response.json( - failure === "timeout" - ? [] - : [ - { - info: { + if (path.endsWith("/command") || path.endsWith("/model")) + return new Response(null, { status: 204 }); + if (path === "/api/session") + return Response.json({ data: [], cursor: {} }); + return Response.json({ + data: + failure === "timeout" + ? [] + : [ + { id: "msg_wedge", - sessionID: "id", - role: "assistant", + type: "assistant", + agent: "build", + model: { providerID: "fixture", id: "model" }, time: { created: 1 }, - }, - parts: [ - { - id: "prt_wedge", - sessionID: "id", - messageID: "msg_wedge", - callID: "call_wedge", - type: "tool", - tool: "bash", - state: { - status: "running", - input: {}, - time: { start: 1 }, + content: [ + { + type: "tool", + name: "shell", + id: "call_wedge", + time: { created: 1 }, + state: { status: "running", input: {}, metadata: {} }, }, - }, - ], - }, - ], - ); + ], + }, + ], + cursor: {}, + }); }); let settled = false; const running = host @@ -722,18 +750,30 @@ describe("eval campaign cancellation", () => { scratch, scratch, ]) as EvalHost; - Object.assign(host, { baseUrl: "http://fixture" }); + Object.assign(host, { + baseUrl: "http://fixture", + nativeClient: nativeTestClient(), + }); const reading = Promise.withResolvers(); let transport: AbortSignal | null | undefined; let reads = 0; const requests = mockFetch(async (input, init) => { reads += 1; - if (String(input).endsWith(`/${endpoint}`)) { + if ( + endpoint === "children" + ? new URL(String(input)).pathname === "/api/session" + : String(input).includes("/message") + ) { transport = init?.signal; reading.resolve(); return new Promise(() => {}); } - return Response.json([]); + return Response.json( + new URL(String(input)).pathname === "/api/session" || + String(input).includes("/message") + ? { data: [], cursor: {} } + : { id: "id", agent: "build", location: { directory: scratch } }, + ); }); try { const observed = host @@ -1009,7 +1049,10 @@ describe("eval campaign cancellation", () => { scratch, controller.signal, ]) as EvalHost; - Object.assign(host, { baseUrl: "http://fixture" }); + Object.assign(host, { + baseUrl: "http://fixture", + nativeClient: nativeTestClient(), + }); const posted = Promise.withResolvers(); let postSignal: AbortSignal | null | undefined; let aborts = 0; @@ -1018,14 +1061,21 @@ describe("eval campaign cancellation", () => { if (url.endsWith("/command")) { postSignal = init?.signal; posted.resolve(); - return accepted ? Response.json({}) : new Promise(() => {}); + return accepted + ? new Response(null, { status: 204 }) + : new Promise(() => {}); } expect(init?.signal?.aborted).not.toBe(true); - if (url.endsWith("/abort")) { + if (new URL(url).pathname.endsWith("/interrupt")) { aborts += 1; - return Response.json(true); + return Response.json({ interrupted: true }); } - return Response.json([]); + if (url.endsWith("/model")) return new Response(null, { status: 204 }); + return Response.json( + new URL(url).pathname === "/api/session" || url.includes("/message") + ? { data: [], cursor: {} } + : { id: "id", agent: "build", location: { directory: scratch } }, + ); }); try { const running = host.runCommand( @@ -1062,15 +1112,18 @@ describe("eval campaign cancellation", () => { scratch, controller.signal, ]) as EvalHost; - Object.assign(host, { baseUrl: "http://fixture" }); + Object.assign(host, { + baseUrl: "http://fixture", + nativeClient: nativeTestClient(), + }); let abortSignal: AbortSignal | null | undefined; const requests = mockFetch(async (input, init) => { - if (String(input).endsWith("/abort")) { + if (new URL(String(input)).pathname.endsWith("/interrupt")) { abortSignal = init?.signal; return new Promise(() => {}); } - controller.abort(reason); - return Response.json({}); + if (String(input).endsWith("/command")) controller.abort(reason); + return new Response(null, { status: 204 }); }); try { await expect( @@ -1089,12 +1142,10 @@ describe("eval campaign cancellation", () => { const rotated = JSON.stringify({ fixture: { refresh: "fake-rotated" } }); await writeFile(target, rotated); const host = Reflect.construct(EvalHost, [scratch, scratch]) as EvalHost; - Object.assign(host, { - credentialPaths: { - source: join(scratch, "missing-parent", "auth.json"), - target, - snapshot: null, - }, + fileCredentialHandoff(host, { + source: join(scratch, "missing-parent", "auth.json"), + target, + snapshot: null, }); const complaints = spyOn(console, "error").mockImplementation(() => {}); try { @@ -1121,7 +1172,7 @@ describe("eval campaign cancellation", () => { await writeFile(source, snapshot); await writeFile(target, JSON.stringify(rotated)); const host = Reflect.construct(EvalHost, [scratch, scratch]) as EvalHost; - Object.assign(host, { credentialPaths: { source, target, snapshot } }); + fileCredentialHandoff(host, { source, target, snapshot }); try { const first = host.stop(); expect(host.stop()).toBe(first); @@ -1142,9 +1193,7 @@ describe("eval campaign cancellation", () => { await writeFile(target, contents); await writeFile(source, '{"fixture":"original"}'); const host = Reflect.construct(EvalHost, [scratch, scratch]) as EvalHost; - Object.assign(host, { - credentialPaths: { source, target, snapshot: null }, - }); + fileCredentialHandoff(host, { source, target, snapshot: null }); try { await expect(host.stop()).rejects.toThrow( `Eval host credentials are invalid; retained at ${target}.`, @@ -1188,19 +1237,23 @@ describe("eval campaign cancellation", () => { scratch, controller.signal, ]) as EvalHost; - Object.assign(host, { baseUrl: "http://fixture" }); + Object.assign(host, { + baseUrl: "http://fixture", + nativeClient: nativeTestClient(), + }); let requestSignal: AbortSignal | null | undefined; let aborts = 0; const requests = mockFetch(async (input, init) => { const url = String(input); - if (url.endsWith("/abort")) { + if (new URL(url).pathname.endsWith("/interrupt")) { aborts += 1; expect(init?.signal?.aborted).toBe(false); - return Response.json(true); + return Response.json({ interrupted: true }); } - if (init?.method === "DELETE") return Response.json(true); - if (operation === "probe" && url.endsWith("/session")) - return Response.json({ id: "id" }); + if (init?.method === "DELETE") + return Response.json({ interrupted: true }); + if (operation === "probe" && new URL(url).pathname === "/api/session") + return Response.json({ data: { id: "id", agent: "build" } }); requestSignal = init?.signal; queueMicrotask(() => controller.abort(reason)); return new Promise(() => {}); @@ -1236,6 +1289,36 @@ describe("eval campaign cancellation", () => { fixture: { refresh: "fake-original" }, }); await writeFile(source, credentials); + const fixturePackage = join( + fixture, + "opencode", + "node_modules", + "opencode-plugin-flow", + ); + await mkdir(join(fixturePackage, "dist"), { recursive: true }); + await writeFile( + join(fixturePackage, "package.json"), + JSON.stringify({ + exports: { + ".": { import: "./dist/index.js" }, + "./tui": { import: "./dist/tui.js" }, + }, + }), + ); + await writeFile( + join(fixturePackage, "dist/index.js"), + "export default {};\n", + ); + await writeFile( + join(fixturePackage, "dist/tui.js"), + "export default {};\n", + ); + const executable = join(fixture, "fake-native-cli"); + await writeFile( + executable, + "#!/bin/sh\nprintf 'server password fixture-private\\n'\n/bin/sleep 60\n", + { mode: 0o700 }, + ); const oldData = process.env.XDG_DATA_HOME; const oldOptOut = process.env.FLOW_EVAL_NO_AUTH_COPY; process.env.XDG_DATA_HOME = fixture; @@ -1255,8 +1338,13 @@ describe("eval campaign cancellation", () => { }, ); const requests = mockFetch(async (input, init) => { - if (phase === "readiness" && String(input).endsWith("/global/health")) - return Response.json({ healthy: true }); + if (phase === "readiness" && String(input).endsWith("/api/info")) + return Response.json({ + version: "2.1.4", + pid: 123, + urls: [], + paths: { tmp: fixture }, + }); if (phase === "readiness") { expect(String(input)).toEndWith("/session"); expect(init?.method).toBe("POST"); @@ -1280,7 +1368,9 @@ describe("eval campaign cancellation", () => { return join(fixture, "missing-cache"); return join(fixture, "opencode"); }, - opencodeVersion: "fixture", + opencodeVersion: "2.1.4", + opencodeExecutable: executable, + providerCredentials: "disabled", files: { "fixture.txt": "no paid calls" }, signal: controller.signal, }); @@ -1310,8 +1400,10 @@ describe("eval campaign cancellation", () => { describe("eval actor and instruction observations", () => { const assistant = (info: Record) => ({ - info: { role: "assistant", time: { created: 1, completed: 2 }, ...info }, - parts: [], + type: "assistant", + time: { created: 1, completed: 2 }, + ...info, + content: [], }); test("extracts nested and top-level model identities, including gateway ids", () => { @@ -1320,7 +1412,7 @@ describe("eval actor and instruction observations", () => { assistant({ model: { providerID: "openrouter", - modelID: "openai/gpt-5.6-sol", + id: "openai/gpt-5.6-sol", }, }), ]), @@ -1333,7 +1425,7 @@ describe("eval actor and instruction observations", () => { }); expect( extractObservedModelIdentity([ - assistant({ providerID: "anthropic", modelID: "claude-sonnet" }), + assistant({ model: { providerID: "anthropic", id: "claude-sonnet" } }), ]), ).toEqual({ kind: "observed", @@ -1369,8 +1461,8 @@ describe("eval actor and instruction observations", () => { }); expect( extractObservedModelIdentity([ - assistant({ providerID: "a", modelID: "one" }), - assistant({ providerID: "b", modelID: "two" }), + assistant({ model: { providerID: "a", id: "one" } }), + assistant({ model: { providerID: "b", id: "two" } }), ]), ).toEqual({ kind: "unobserved", reason: "conflicting-observations" }); }); @@ -1407,7 +1499,9 @@ describe("eval actor and instruction observations", () => { sessions: [ { id: "reviewer", - messages: [assistant({ providerID: "a", modelID: "reviewer" })], + messages: [ + assistant({ model: { providerID: "a", id: "reviewer" } }), + ], }, ], childEndpointFailed: true, @@ -1424,7 +1518,7 @@ describe("eval actor and instruction observations", () => { id: "parent", messages: [ assistant({ - model: { providerID: "openrouter", modelID: "x/y" }, + model: { providerID: "openrouter", id: "x/y" }, }), ], }, @@ -1444,7 +1538,7 @@ describe("eval actor and instruction observations", () => { sessions: [ { id: "parent", - messages: [assistant({ providerID: "a", modelID: "m" })], + messages: [assistant({ model: { providerID: "a", id: "m" } })], }, { id: "resume", messages: null }, ], diff --git a/tests/eval-retention.test.ts b/tests/eval-retention.test.ts index 87de5d63..1dabd9aa 100644 --- a/tests/eval-retention.test.ts +++ b/tests/eval-retention.test.ts @@ -10,6 +10,7 @@ import { import { tmpdir } from "node:os"; import { join } from "node:path"; import { EvalHost } from "../evals/harness.js"; +import { fileCredentialHandoff } from "./fixtures/opencode-v2-test-host.js"; test("shutdown retains the quiescent project once before deleting scratch and rejects late retention", async () => { const scratch = await mkdtemp(join(tmpdir(), "retention-check-")); @@ -55,7 +56,7 @@ test("credential synchronization failure still retains the stopped project and p const target = join(scratch, "invalid-credentials"); await writeFile(target, "invalid-json"); const host: EvalHost = Reflect.construct(EvalHost, [project, scratch]); - Reflect.set(host, "credentialPaths", { + fileCredentialHandoff(host, { source: join(scratch, "source-credentials"), target, snapshot: null, @@ -80,7 +81,7 @@ test("settles synchronous retention failure together with invalid credentials", const target = join(scratch, "invalid-credentials"); await writeFile(target, "invalid-json"); const host: EvalHost = Reflect.construct(EvalHost, [project, scratch]); - Reflect.set(host, "credentialPaths", { + fileCredentialHandoff(host, { source: join(scratch, "source-credentials"), target, snapshot: null, diff --git a/tests/eval-reviewer-access.test.ts b/tests/eval-reviewer-access.test.ts index 2f74552a..4489aa83 100644 --- a/tests/eval-reviewer-access.test.ts +++ b/tests/eval-reviewer-access.test.ts @@ -414,6 +414,8 @@ async function accessFixture( { sessionId: "ses_child", messages: [childUser, ...childMessages] }, ], }); + if (hostTrace.version !== 1) + throw new Error("Expected historical V1 fixture."); const packetBytes = collectReviewerPacketBytes(calls, [document]); return { input: { @@ -852,7 +854,10 @@ test("redacted retained native evidence preserves nulls, token digests and revie firstCall.metadata = { accessToken: "raw-access", api_key: "raw-key" }; const evidence = retainedAccessEvidence(input); const trace = evidence.gradeInput.hostTrace; - const root = trace?.kind === "observed" ? trace.messages[0] : null; + const root = + trace?.kind === "observed" && trace.version === 1 + ? trace.messages[0] + : null; if (root?.role !== "user") throw new Error("Missing retained root request"); expect(root.parts.map((part) => part.flowTokenSha256)).toEqual([ null, diff --git a/tests/eval-v2-trace-contract.test.ts b/tests/eval-v2-trace-contract.test.ts new file mode 100644 index 00000000..6fbc1468 --- /dev/null +++ b/tests/eval-v2-trace-contract.test.ts @@ -0,0 +1,852 @@ +import { expect, test } from "bun:test"; +import type { + SessionMessageAssistant, + SessionMessageAssistantTool, +} from "@opencode/client"; +import { + deriveRetainedFailure, + pseudonymizeEvalIds, + RetainedScenarioEvidenceSchema, + retainedFailureObservation, + ScenarioGradeInputSchema, +} from "../evals/grader-input.js"; +import type { ObservedToolCall } from "../evals/harness.js"; +import { + collectHostTrace, + HostAbortObservationSchema, + type HostTrace, + type HostTraceCollection, + HostTraceSchema, + NativeToolProvenanceSchema, + nativeToolKey, + nativeToolProvenance, +} from "../evals/host-trace.js"; +import { nativeActorBindingIssues } from "../evals/native-actors.js"; +import { projectEvalMessage } from "../evals/opencode-host.js"; +import { + checkAutonomousLineage, + checkReviewerEvidenceAccess, + nativeToolWitness, +} from "../evals/reviewer-access.js"; +import { autoQualifiedOutcome } from "./fixtures/auto-qualified-outcome.js"; + +function session(id = "ses_root", parentID?: string) { + return { + id, + ...(parentID ? { parentID } : {}), + location: { directory: "/workspace" }, + projectID: "fixture", + agent: parentID ? "flow-reviewer" : "build", + cost: 0, + tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } }, + time: { created: 1, updated: 20 }, + }; +} +function tool( + id = "call_repeat", + status: "completed" | "running" = "completed", + created = 3, +): SessionMessageAssistantTool { + return { + type: "tool", + id, + name: "flow_status", + time: { + created, + ...(status === "completed" ? { completed: created + 1 } : {}), + }, + state: + status === "running" + ? { status, input: {}, metadata: {} } + : { status, input: {}, content: [{ type: "text", text: "{}" }] }, + }; +} +function assistant(id = "msg_assistant", created = 2): SessionMessageAssistant { + return { + id, + type: "assistant", + agent: "build", + model: { id: "fixture", providerID: "fake" }, + time: { created }, + content: [ + { type: "text", text: "tool preface" }, + tool("call_repeat", "completed", created + 1), + ], + }; +} +function fixture(): HostTraceCollection { + return { + runnerRootSessionIds: ["ses_root"], + directory: "/workspace", + childrenComplete: true, + sessionMetadata: [session(), session("ses_child", "ses_root")], + sessionMessages: [ + { + sessionId: "ses_root", + messages: [ + { + id: "msg_user", + type: "user", + text: "Implement task", + time: { created: 1 }, + metadata: { "opencode-plugin-flow/auto": "fixture-token" }, + }, + assistant(), + { + id: "msg_synthetic", + type: "synthetic", + text: "Continue", + time: { created: 5 }, + metadata: { "opencode-plugin-flow/auto": "fixture-token" }, + }, + assistant("msg_second", 6), + { + id: "msg_idle", + type: "idle", + outcome: "succeeded", + time: { created: 9 }, + }, + ], + }, + { + sessionId: "ses_child", + messages: [ + { + id: "msg_child_user", + type: "user", + text: "Review", + time: { created: 1 }, + }, + { ...assistant("msg_child", 2), agent: "flow-reviewer" }, + ], + }, + ], + }; +} +function object(value: unknown): Record { + if (typeof value !== "object" || value === null || Array.isArray(value)) + throw new Error("Expected object"); + return value as Record; +} +function array(value: unknown): unknown[] { + if (!Array.isArray(value)) throw new Error("Expected array"); + return value; +} +function required(value: T | undefined): T { + if (value === undefined) throw new Error("Missing fixture item"); + return value; +} +function rootMessages(input: HostTraceCollection): unknown[] { + return array(input.sessionMessages[0]?.messages); +} +function observed( + input = fixture(), +): Extract { + const trace = collectHostTrace(input); + if (trace.version !== 2 || trace.kind !== "observed") + throw new Error(JSON.stringify(trace)); + return trace; +} +function callFor(messageId = "msg_assistant"): ObservedToolCall { + const native = nativeToolProvenance( + "ses_root", + assistant(messageId, messageId === "msg_second" ? 6 : 2), + 1, + ); + if (!native) throw new Error("Missing provenance"); + return { + native, + tool: "flow_status", + status: "completed", + sessionIndex: 0, + agent: "build", + input: {}, + output: {}, + rawOutput: "", + metadata: {}, + }; +} +function gradeInput(trace: HostTrace, calls = [callFor()]) { + return { + schemaVersion: 1 as const, + hostTrace: trace, + allCalls: calls, + flowCalls: calls, + session: null, + archives: [], + finalText: "", + }; +} + +test("V2 preserves native message kinds, endpoint ownership and timestamps without fabricating IDs or parents", () => { + const trace = observed(); + expect(trace.sessions).toEqual([ + { id: "ses_root", parentId: null, agent: "build", projectMatched: true }, + { + id: "ses_child", + parentId: "ses_root", + agent: "flow-reviewer", + projectMatched: true, + }, + ]); + expect(trace.messages.map((m) => [m.role, m.type, m.sessionId])).toEqual([ + ["user", "user", "ses_root"], + ["assistant", "assistant", "ses_root"], + ["user", "synthetic", "ses_root"], + ["assistant", "assistant", "ses_root"], + ["event", "idle", "ses_root"], + ["user", "user", "ses_child"], + ["assistant", "assistant", "ses_child"], + ]); + expect(trace.messages[1]).toEqual({ + id: "msg_assistant", + sessionId: "ses_root", + order: 1, + created: 2, + role: "assistant", + type: "assistant", + agent: "build", + contentCount: 2, + tools: [ + { + version: 2, + sessionId: "ses_root", + messageId: "msg_assistant", + partIndex: 1, + callId: "call_repeat", + startedAt: 3, + completedAt: 4, + tool: "flow_status", + status: "completed", + }, + ], + }); + expect(JSON.stringify(trace)).not.toContain("partId"); + expect(JSON.stringify(trace)).not.toContain("Implement task"); + expect(JSON.stringify(trace)).not.toContain("fixture-token"); + expect(checkAutonomousLineage({ hostTrace: trace })).toEqual([]); + expect(HostTraceSchema.parse(pseudonymizeEvalIds(trace)).version).toBe(2); +}); + +test("raw and projected V2 extraction agree and never accept a mismatched projection", () => { + const raw = assistant(); + const projection = projectEvalMessage(raw, "ses_root"); + const expected = { + version: 2 as const, + sessionId: "ses_root", + messageId: "msg_assistant", + partIndex: 1, + callId: "call_repeat", + startedAt: 3, + completedAt: 4, + }; + expect(nativeToolProvenance("ses_root", raw, 1)).toEqual(expected); + expect( + nativeToolProvenance( + "ses_root", + projection.info, + projection.parts[1], + 1, + raw, + ), + ).toEqual(expected); + expect(nativeToolProvenance("ses_root", raw, 0)).toBeUndefined(); + expect(nativeToolProvenance("ses_root", raw, 256)).toBeUndefined(); + expect( + nativeToolProvenance( + "ses_root", + projection.info, + { ...projection.parts[1], callID: "other" }, + 1, + raw, + ), + ).toBeUndefined(); + expect( + nativeToolProvenance( + "ses_root", + { ...projection.info, id: "msg_other" }, + projection.parts[1], + 1, + raw, + ), + ).toBeUndefined(); + expect( + nativeToolProvenance("ses_root", { ...raw, sessionID: "ses_other" }, 1), + ).toBeUndefined(); + expect( + nativeToolProvenance("ses_root", projection.info, projection.parts[1], 1), + ).toBeUndefined(); +}); + +const mutations: readonly [ + string, + (input: HostTraceCollection) => void, + Extract["reason"], +][] = [ + [ + "wrong workspace", + (input) => { + object(object(input.sessionMetadata[0]).location).directory = + "/elsewhere"; + }, + "directory-mismatch", + ], + [ + "unknown parent", + (input) => { + object(input.sessionMetadata[1]).parentID = "ses_unknown"; + }, + "unknown-session-parent", + ], + [ + "cycle", + (input) => { + object(input.sessionMetadata[1]).parentID = "ses_child"; + }, + "session-cycle", + ], + [ + "root child", + (input) => { + object(input.sessionMetadata[0]).parentID = "ses_child"; + }, + "root-is-child", + ], + [ + "duplicate session", + (input) => { + object(input.sessionMetadata[1]).id = "ses_root"; + }, + "duplicate-native-id", + ], + [ + "duplicate message", + (input) => { + object(rootMessages(input)[3]).id = "msg_assistant"; + }, + "duplicate-native-id", + ], + [ + "session/message collision", + (input) => { + object(rootMessages(input)[3]).id = "ses_root"; + }, + "malformed-native-message", + ], + [ + "wrong explicit session", + (input) => { + object(rootMessages(input)[0]).sessionID = "ses_child"; + }, + "message-session-mismatch", + ], + [ + "fabricated assistant parent", + (input) => { + object(rootMessages(input)[1]).parentID = "msg_user"; + }, + "malformed-native-message", + ], + [ + "missing native text", + (input) => { + delete object(rootMessages(input)[0]).text; + }, + "malformed-native-message", + ], + [ + "unknown native type", + (input) => { + object(rootMessages(input)[0]).type = "invented"; + }, + "malformed-native-message", + ], + [ + "wrong token type", + (input) => { + object(object(rootMessages(input)[0]).metadata)[ + "opencode-plugin-flow/auto" + ] = false; + }, + "malformed-native-message", + ], + [ + "message chronology", + (input) => { + object(object(rootMessages(input)[3]).time).created = 0; + }, + "chronology-conflict", + ], + [ + "tool chronology", + (input) => { + object( + object(array(object(rootMessages(input)[1]).content)[1]).time, + ).completed = 1; + }, + "chronology-conflict", + ], + [ + "tool before message", + (input) => { + object( + object(array(object(rootMessages(input)[1]).content)[1]).time, + ).created = 1; + }, + "chronology-conflict", + ], + [ + "missing tool state", + (input) => { + delete object(array(object(rootMessages(input)[1]).content)[1]).state; + }, + "malformed-native-message", + ], + [ + "content limit", + (input) => { + object(rootMessages(input)[1]).content = Array.from( + { length: 257 }, + () => ({ type: "text", text: "x" }), + ); + }, + "limits-exceeded", + ], +]; +for (const [name, mutate, reason] of mutations) + test(`V2 collection fails closed for ${name}`, () => { + const input = fixture(); + mutate(input); + expect(collectHostTrace(input)).toEqual({ + version: 2, + kind: "unavailable", + reason, + }); + }); + +test("V2 checks transcript coverage, duplicate bindings and endpoint failures", () => { + const input = fixture(); + expect( + collectHostTrace({ + ...input, + sessionMessages: input.sessionMessages.slice(0, 1), + }), + ).toEqual({ + version: 2, + kind: "unavailable", + reason: "incomplete-transcript", + }); + expect( + collectHostTrace({ + ...input, + sessionMessages: [ + ...input.sessionMessages, + required(input.sessionMessages[0]), + ], + }), + ).toEqual({ version: 2, kind: "unavailable", reason: "duplicate-native-id" }); + expect( + collectHostTrace({ + ...input, + sessionMessages: [{ sessionId: "ses_unknown", messages: [] }], + }), + ).toEqual({ + version: 2, + kind: "unavailable", + reason: "message-session-mismatch", + }); + expect(collectHostTrace({ ...input, childrenComplete: false })).toEqual({ + version: 2, + kind: "unavailable", + reason: "endpoint-failure", + }); + expect( + collectHostTrace({ + ...input, + runnerRootSessionIds: ["ses_root", "ses_root"], + }), + ).toEqual({ version: 2, kind: "unavailable", reason: "duplicate-native-id" }); +}); + +test("V2 composite identity accepts repeating call IDs across messages and rejects all mismatched joins", () => { + const trace = observed(); + const first = callFor(), + second = callFor("msg_second"); + expect(first.native?.callId).toBe("call_repeat"); + expect(second.native?.callId).toBe("call_repeat"); + expect(nativeToolKey(required(first.native))).not.toBe( + nativeToolKey(required(second.native)), + ); + expect( + ScenarioGradeInputSchema.parse(gradeInput(trace, [first, second])).allCalls + .length, + ).toBe(2); + expect(nativeToolWitness(first, trace)?.message.id).toBe("msg_assistant"); + for (const [field, value] of [ + ["sessionId", "ses_child"], + ["messageId", "msg_second"], + ["partIndex", 0], + ["callId", "other"], + ["startedAt", 2], + ["completedAt", 5], + ] as const) { + const mutated = structuredClone(first); + object(mutated.native)[field] = value; + expect( + ScenarioGradeInputSchema.safeParse(gradeInput(trace, [mutated])).success, + ).toBe(false); + expect(nativeToolWitness(mutated, trace)).toBeNull(); + } + expect( + ScenarioGradeInputSchema.safeParse(gradeInput(trace, [first, first])) + .success, + ).toBe(false); +}); + +test("V2 serialized validation rejects mixed versions, duplicate indexes, bad topology and impossible bindings", () => { + for (const mutate of [ + (t: ReturnType) => { + required(t.sessions[1]).parentId = "ses_unknown"; + }, + (t: ReturnType) => { + object(t.messages[1]).parentId = "msg_user"; + }, + (t: ReturnType) => { + object(t.messages[1]).tools = [ + { + sessionId: "ses_root", + messageId: "msg_assistant", + partId: "prt_fake", + partIndex: 1, + callId: "call_repeat", + startedAt: 3, + completedAt: 4, + tool: "flow_status", + status: "completed", + }, + ]; + }, + (t: ReturnType) => { + object(t.messages[1]).tools = [ + ...array(object(t.messages[1]).tools), + ...array(object(t.messages[1]).tools), + ]; + }, + (t: ReturnType) => { + object(array(object(t.messages[1]).tools)[0]).sessionId = "ses_child"; + }, + (t: ReturnType) => { + object(array(object(t.messages[1]).tools)[0]).partIndex = 2; + }, + (t: ReturnType) => { + object(t.messages[3]).created = 0; + }, + ]) { + const trace = observed(); + mutate(trace); + expect(HostTraceSchema.safeParse(trace).success).toBe(false); + } + expect( + NativeToolProvenanceSchema.safeParse({ + ...callFor().native, + partId: "prt_fake", + }).success, + ).toBe(false); +}); + +test("V2 lineage uses native metadata and message types and leaves compaction unproved", () => { + const input = fixture(); + delete object(rootMessages(input)[0]).metadata; + expect(checkAutonomousLineage({ hostTrace: observed(input) })).toEqual([ + "The initial original Flow request marker is missing.", + "Unknown or mismatched synthetic Flow continuation.", + ]); + const second = fixture(); + object(object(rootMessages(second)[2]).metadata)[ + "opencode-plugin-flow/auto" + ] = "other-token"; + expect(checkAutonomousLineage({ hostTrace: observed(second) })).toEqual([ + "Unknown or mismatched synthetic Flow continuation.", + ]); + const extra = fixture(); + rootMessages(extra).push({ + id: "msg_another", + type: "user", + text: "Continue", + time: { created: 10 }, + metadata: { "opencode-plugin-flow/auto": "fixture-token" }, + }); + expect(checkAutonomousLineage({ hostTrace: observed(extra) })).toEqual([ + "Expected exactly one original root request.", + ]); + const compacted = fixture(); + rootMessages(compacted).push({ + id: "msg_compaction", + type: "compaction", + status: "completed", + reason: "auto", + summary: "Summary", + time: { created: 10 }, + }); + const trace = observed(compacted); + expect(trace.messages.at(5)).toMatchObject({ + role: "event", + type: "compaction", + }); + expect(trace.compactionDelivery).toBe("unavailable-from-message-export"); + expect(checkAutonomousLineage({ hostTrace: trace })).toEqual([ + "Native compaction or unknown user-part delivery is unproven.", + ]); +}); + +function qualifiedV2() { + const input = autoQualifiedOutcome("single"); + const legacy = input.hostTrace; + if (legacy?.kind !== "observed" || legacy.version !== 1) + throw new Error("Expected V1 fixture"); + const sessionMetadata = legacy.sessions.map((s) => + session(s.id, s.parentId ?? undefined), + ); + const sessionMessages = legacy.sessions.map((s) => ({ + sessionId: s.id, + messages: legacy.messages + .filter((m) => m.sessionId === s.id) + .map((m) => + m.role === "user" + ? { + id: m.id, + type: "user", + text: "Task", + time: { created: m.created }, + ...(legacy.runnerRootSessionIds.includes(s.id) + ? { metadata: { "opencode-plugin-flow/auto": "fixture-token" } } + : {}), + } + : { + id: m.id, + type: "assistant", + agent: m.agent, + model: { id: "fixture", providerID: "fake" }, + time: { created: m.created }, + content: m.tools.map((t) => ({ + type: "tool", + id: "call_repeated", + name: t.tool, + time: { created: t.startedAt, completed: t.completedAt }, + state: { + status: t.status, + input: {}, + content: [{ type: "text", text: "{}" }], + }, + })), + }, + ), + })); + const trace = observed({ + runnerRootSessionIds: legacy.runnerRootSessionIds, + directory: "/workspace", + childrenComplete: true, + sessionMetadata, + sessionMessages, + }); + const calls = input.allCalls.map((call) => { + const message = trace.messages.find( + (m) => m.role === "assistant" && m.id === call.native?.messageId, + ); + if (message?.role !== "assistant" || !message.tools[0]) + throw new Error("Missing native fixture tool"); + const { tool: _tool, status: _status, ...native } = message.tools[0]; + return { ...call, native }; + }); + return { ...input, hostTrace: trace, allCalls: calls, flowCalls: calls }; +} + +test("V2 reviewer reads, submitted child role and original packet evidence survive retained regrading", () => { + const input = qualifiedV2(); + expect(checkAutonomousLineage(input)).toEqual([]); + expect(checkReviewerEvidenceAccess(input, input.archives[0])).toEqual([]); + const retained = RetainedScenarioEvidenceSchema.parse({ + schemaVersion: 1, + attempt: { + attemptId: "v2", + cellId: "v2", + caseId: "v2", + repetition: 0, + model: { + routeProvider: "fake", + gateway: null, + family: "fixture", + model: "fixture", + revision: null, + }, + }, + actors: [ + { + role: "manager", + sessionIds: ["ses_root"], + actualModel: { + kind: "observed", + value: { providerID: "fake", modelID: "fixture" }, + }, + requestedModelId: "fake/fixture", + requestedModel: { + routeProvider: "fake", + gateway: null, + family: "fixture", + model: "fixture", + revision: null, + }, + }, + { + role: "reviewer", + sessionIds: ["ses_reviewer0"], + actualModel: { + kind: "observed", + value: { providerID: "fake", modelID: "fixture" }, + }, + requestedModelId: "fake/fixture", + requestedModel: { + routeProvider: "fake", + gateway: null, + family: "fixture", + model: "fixture", + revision: null, + }, + }, + ], + guidanceLoads: [], + gradeInput: { schemaVersion: 1, ...input }, + usage: { durationMs: 100, outputTokens: 1, costUsd: null }, + }); + expect(nativeActorBindingIssues(retained)).toEqual([]); + const replay = RetainedScenarioEvidenceSchema.parse( + pseudonymizeEvalIds(JSON.parse(JSON.stringify(retained))), + ); + expect(checkAutonomousLineage(replay.gradeInput)).toEqual([]); + expect( + checkReviewerEvidenceAccess( + replay.gradeInput, + replay.gradeInput.archives[0], + ), + ).toEqual([]); + const bad = qualifiedV2(); + const trace = bad.hostTrace; + if (trace?.kind !== "observed" || trace.version !== 2) + throw new Error("Expected V2"); + required(trace.sessions.find((s) => s.parentId !== null)).agent = "build"; + expect(checkReviewerEvidenceAccess(bad, bad.archives[0])).toEqual([ + "parser: One accepted native reviewer-child submission is required.", + ]); +}); + +test("V2 abort observation retains historical wrapper shape and proves exact native composite on regrade", () => { + const raw = assistant(); + raw.content = [tool("call_repeat", "running", 3)]; + const trace = observed({ + ...fixture(), + sessionMessages: [ + { + sessionId: "ses_root", + messages: [ + { id: "msg_user", type: "user", text: "Task", time: { created: 1 } }, + raw, + ], + }, + { sessionId: "ses_child", messages: [] }, + ], + }); + const native = nativeToolProvenance("ses_root", raw, 0); + if (!native) throw new Error("Missing pending"); + const call: ObservedToolCall = { + ...callFor(), + native, + status: "running", + tool: "task", + input: { description: "Review" }, + }; + const message = trace.messages[1]; + if (message?.role !== "assistant") throw new Error("Missing assistant"); + required(message.tools[0]).tool = "task"; + const proof = { + rootSessionId: "ses_root", + observedAt: 180004, + abortRequestedAt: 180004, + startedAt: 1, + excludedMs: 0, + trigger: { + kind: "stall" as const, + unchangedMs: 180000, + lastActivityAt: 4, + thresholdMs: 180000 as const, + }, + pending: [ + { native, tool: "task", status: "running" as const, input: call.input }, + ], + }; + expect(HostAbortObservationSchema.parse(proof)).toEqual(proof); + const input = ScenarioGradeInputSchema.parse(gradeInput(trace, [call])); + const observation = retainedFailureObservation({ + origin: "host", + code: "command-aborted", + retryable: true, + detail: "Actual watchdog abort", + gradeInput: input, + abortObservation: proof, + }); + const retained = RetainedScenarioEvidenceSchema.parse({ + schemaVersion: 1, + attempt: { + attemptId: "v2", + cellId: "v2", + caseId: "v2", + repetition: 0, + model: { + routeProvider: "fake", + gateway: null, + family: "fixture", + model: "fixture", + revision: null, + }, + }, + actors: [ + { + role: "manager", + sessionIds: ["ses_root"], + actualModel: { + kind: "observed", + value: { providerID: "fake", modelID: "fixture" }, + }, + requestedModelId: "fake/fixture", + requestedModel: { + routeProvider: "fake", + gateway: null, + family: "fixture", + model: "fixture", + revision: null, + }, + }, + ], + guidanceLoads: [], + gradeInput: input, + failureObservation: observation, + usage: { durationMs: 180003, outputTokens: 1, costUsd: null }, + }); + expect(deriveRetainedFailure(retained)).toEqual({ + origin: "host", + code: "command-aborted", + retryable: true, + }); + const replay = RetainedScenarioEvidenceSchema.parse( + pseudonymizeEvalIds(JSON.parse(JSON.stringify(retained))), + ); + expect(deriveRetainedFailure(replay)).toEqual({ + origin: "host", + code: "command-aborted", + retryable: true, + }); + const pending = retained.failureObservation; + if (pending?.kind !== "host-phase" || !pending.abortObservation) + throw new Error("Missing proof"); + required(pending.abortObservation.pending[0]).native.messageId = "msg_other"; + expect(deriveRetainedFailure(retained)).toBeNull(); +}); diff --git a/tests/execution-attribution.test.ts b/tests/execution-attribution.test.ts new file mode 100644 index 00000000..52e86bc2 --- /dev/null +++ b/tests/execution-attribution.test.ts @@ -0,0 +1,471 @@ +import { expect, test } from "bun:test"; +import type { SessionInfo, SessionMessageInfo } from "@opencode/client"; +import { Agent, Model, Provider } from "@opencode/plugin"; +import type { SessionContext } from "@opencode/plugin/promise/session"; +import { CallID } from "@opencode/plugin/promise/tool"; +import { Session } from "@opencode/schema/session"; +import { SessionMessage } from "@opencode/schema/session-message"; +import { + ExecutionAttribution, + type InputOrigin, +} from "../src/platform/opencode/execution-attribution.js"; + +const sid = Session.ID.make("session-test"); +const model = { + providerID: Provider.ID.make("local"), + id: Model.ID.make("scripted"), +}; +const agent = Agent.ID.make("build"); +const origin: InputOrigin = { + id: "user-1", + epoch: 0, + automatic: false, + authority: null, + delivery: { + agent, + model: { providerID: model.providerID, modelID: model.id }, + }, +}; +const user: SessionMessageInfo = { + type: "user", + id: "user-1", + text: "Approved task", + time: { created: 0 }, +}; +const assistant: SessionMessageInfo = { + type: "assistant", + id: "msg_assistant1", + agent, + model, + time: { created: 1 }, + content: [ + { + type: "tool", + id: "call-1", + name: "shell", + state: { status: "running", input: {}, metadata: {} }, + time: { created: 1 }, + }, + ], +}; +const context: SessionContext = { + sessionID: sid, + agent, + model, + system: [], + options: {}, + tools: {}, + messages: [ + { + id: "user-1", + role: "user", + content: [{ type: "text", text: "Approved task" }], + }, + ], +}; +const session: SessionInfo = { + id: sid, + projectID: "project", + agent, + model, + cost: 0, + tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } }, + time: { created: 0, updated: 0 }, + location: { directory: "/tmp" }, +}; +const execution = { + sessionID: sid, + agent, + messageID: SessionMessage.ID.make("msg_assistant1"), + id: CallID.make("call-1"), +}; +function fixture() { + let live = true; + const attribution = new ExecutionAttribution(() => { + if (!live) throw new Error("revoked epoch"); + }); + const primary = attribution.freeze(context, [user], origin); + const request = new Request("http://localhost/primary"); + attribution.request({ + sessionID: sid, + agent, + model, + kind: "primary", + request, + }); + return { + attribution, + primary, + request, + revoke() { + live = false; + }, + }; +} +test("native exact execution binds once and a revoked epoch vetoes its second boundary", () => { + const f = fixture(); + expect( + f.attribution.verify(execution, [user, assistant], session, "/tmp", "shell") + .primary.origin.id, + ).toBe("user-1"); + f.revoke(); + expect(() => + f.attribution.verify( + execution, + [user, assistant], + session, + "/tmp", + "shell", + ), + ).toThrow("revoked epoch"); +}); +test("native full prefix, exact call, agent, and workspace are required", () => { + for (const [messages, call, identity, workspace] of [ + [[{ ...user, id: "other-user" }, assistant], execution, session, "/tmp"], + [ + [user, assistant], + { ...execution, id: CallID.make("wrong-call") }, + session, + "/tmp", + ], + [[user, assistant], execution, { ...session, agent: "other" }, "/tmp"], + [[user, assistant], execution, session, "/different-workspace"], + ] as const) + expect(() => + fixture().attribution.verify( + call, + messages, + identity, + workspace, + "shell", + ), + ).toThrow(); +}); +test("overlapping primary context is refused", () => { + const f = fixture(); + expect(() => f.attribution.freeze(context, [user], origin)).toThrow( + "overlap", + ); +}); +test("response must join the same Request object", () => { + const f = fixture(); + expect(() => + f.attribution.response( + { + sessionID: sid, + agent, + model, + kind: "primary", + request: new Request(f.request), + response: new Response("data"), + }, + () => {}, + ), + ).toThrow("Request identity"); +}); +test("an old assistant is rejected after a later context", async () => { + const f = fixture(); + f.attribution.verify(execution, [user, assistant], session, "/tmp", "shell"); + const response = { + sessionID: sid, + agent, + model, + kind: "primary" as const, + request: f.request, + response: new Response("data"), + }; + f.attribution.response(response, () => {}); + expect(await response.response.text()).toBe("data"); + f.attribution.freeze(context, [user, assistant], origin); + f.attribution.request({ + sessionID: sid, + agent, + model, + kind: "primary", + request: new Request("http://localhost/next"), + }); + expect(() => + f.attribution.verify( + execution, + [user, assistant], + session, + "/tmp", + "shell", + ), + ).toThrow("exact assistant"); +}); + +test.each([false, true])( + "HTTP 400 preserves the native provider error and releases authority when already revoked is %s", + async (alreadyRevoked) => { + const f = fixture(); + if (alreadyRevoked) f.revoke(); + const original = Response.json( + { + error: { + message: "Local scripted provider failed", + code: "fixture_error", + }, + }, + { status: 400, headers: { "x-fixture-error": "original" } }, + ); + const input = { + sessionID: sid, + agent, + model, + kind: "primary" as const, + request: f.request, + response: original, + }; + expect(() => f.attribution.response(input, () => f.revoke())).not.toThrow(); + expect(input.response).toBe(original); + expect(input.response.status).toBe(400); + expect(input.response.headers.get("x-fixture-error")).toBe("original"); + expect(await input.response.json()).toEqual({ + error: { + message: "Local scripted provider failed", + code: "fixture_error", + }, + }); + expect(() => + f.attribution.verify( + execution, + [user, assistant], + session, + "/tmp", + "shell", + ), + ).toThrow("revoked epoch"); + }, +); + +test("a failed HTTP response does not leave the next fresh request overlapping", () => { + let current = "user-1"; + const attribution = new ExecutionAttribution((candidate) => { + if (candidate.id !== current) throw new Error("revoked origin"); + }); + attribution.freeze(context, [user], origin); + const request = new Request("http://localhost/failed"); + attribution.request({ + sessionID: sid, + agent, + model, + kind: "primary", + request, + }); + attribution.response( + { + sessionID: sid, + agent, + model, + kind: "primary", + request, + response: Response.json( + { error: { message: "Local scripted provider failed" } }, + { status: 400 }, + ), + }, + () => { + current = "user-2"; + }, + ); + const nextUser: SessionMessageInfo = { + ...user, + id: "user-2", + text: "Fresh direction", + }; + const nextContext: SessionContext = { + ...context, + messages: [ + ...context.messages, + { + id: "user-2", + role: "user", + content: [{ type: "text", text: "Fresh direction" }], + }, + ], + }; + const nextOrigin: InputOrigin = { ...origin, id: "user-2" }; + attribution.freeze(nextContext, [user, nextUser], nextOrigin); + attribution.request({ + sessionID: sid, + agent, + model, + kind: "primary", + request: new Request("http://localhost/fresh"), + }); + expect( + attribution.verify( + execution, + [user, nextUser, assistant], + session, + "/tmp", + "shell", + ).primary.origin.id, + ).toBe("user-2"); +}); + +function failedStreamFixture() { + let direction = "user-1"; + const failures: string[] = []; + const attribution = new ExecutionAttribution((candidate) => { + if (candidate.id !== direction) throw new Error("revoked stream origin"); + }); + attribution.freeze(context, [user], origin); + const request = new Request("http://localhost/stream"); + attribution.request({ + sessionID: sid, + agent, + model, + kind: "primary", + request, + }); + return { + attribution, + request, + failures, + fail(host: string) { + failures.push(host); + direction = "user-2"; + }, + steer() { + direction = "user-2"; + }, + fresh() { + const nextUser: Extract = { + id: "user-2", + type: "user", + text: "Fresh real direction", + time: { created: 2 }, + }; + const oldAssistant: Extract = { + id: execution.messageID, + type: "assistant", + agent, + model, + content: [], + finish: "error", + time: { created: 1 }, + }; + const nextContext: SessionContext = { + ...context, + messages: [ + ...context.messages, + { + id: nextUser.id, + role: "user", + content: [{ type: "text", text: nextUser.text }], + }, + ], + }; + const nextOrigin: InputOrigin = { ...origin, id: nextUser.id }; + const native: SessionMessageInfo[] = [user, oldAssistant, nextUser]; + attribution.freeze(nextContext, native, nextOrigin); + attribution.request({ + sessionID: sid, + agent, + model, + kind: "primary", + request: new Request("http://localhost/fresh"), + }); + const freshID = SessionMessage.ID.make("msg_fresh"); + const freshAssistant: SessionMessageInfo = { ...assistant, id: freshID }; + return () => + attribution.verify( + { ...execution, messageID: freshID }, + [...native, freshAssistant], + session, + "/tmp", + "shell", + ).primary.origin.id; + }, + }; +} + +test.each([ + "cancel", + "read-error", + "no-body", + "locked-body", + "stale-response", +] as const)( + "%s terminates its exact primary so fresh real input can execute", + async (failure) => { + const f = failedStreamFixture(); + const response = + failure === "no-body" + ? new Response(null) + : failure === "read-error" + ? new Response( + new ReadableStream({ + start(controller) { + controller.error(new Error("provider stream broke")); + }, + }), + ) + : failure === "cancel" + ? new Response(new ReadableStream()) + : new Response("body"); + const input = { + sessionID: sid, + agent, + model, + kind: "primary" as const, + request: f.request, + response, + }; + const locked = + failure === "locked-body" ? response.body?.getReader() : undefined; + if (failure === "stale-response") f.steer(); + if ( + failure === "no-body" || + failure === "locked-body" || + failure === "stale-response" + ) { + expect(() => f.attribution.response(input, f.fail)).toThrow(); + locked?.releaseLock(); + } else { + f.attribution.response(input, f.fail); + if (failure === "read-error") + await expect(input.response.text()).rejects.toThrow( + "provider stream broke", + ); + else { + if (!input.response.body) throw new Error("Missing native body."); + await input.response.body.cancel("real steer"); + } + } + const verifyFresh = f.fresh(); + expect(verifyFresh()).toBe("user-2"); + expect(f.failures).toEqual(["session-test"]); + }, +); + +test("a late old-stream cancellation failure cannot end or revoke a newer primary", async () => { + const f = failedStreamFixture(); + const cancelled = Promise.withResolvers(); + const input = { + sessionID: sid, + agent, + model, + kind: "primary" as const, + request: f.request, + response: new Response( + new ReadableStream({ cancel: () => cancelled.promise }), + ), + }; + f.attribution.response(input, f.fail); + if (!input.response.body) throw new Error("Missing wrapped body."); + const pending = input.response.body.cancel("steer"); + const verifyFresh = f.fresh(); + cancelled.reject(new Error("late native cancel failure")); + await expect(pending).rejects.toThrow("late native cancel failure"); + expect(verifyFresh()).toBe("user-2"); + expect(f.failures).toEqual(["session-test"]); + const late = { ...input, response: new Response("late duplicate response") }; + expect(() => f.attribution.response(late, f.fail)).toThrow( + "Request identity", + ); + expect(verifyFresh()).toBe("user-2"); + expect(f.failures).toEqual(["session-test"]); +}); diff --git a/tests/fixtures/continuation-commands.ts b/tests/fixtures/continuation-commands.ts new file mode 100644 index 00000000..e7b57874 --- /dev/null +++ b/tests/fixtures/continuation-commands.ts @@ -0,0 +1,54 @@ +import type { prepareFlowCommand } from "../../src/platform/opencode/command-hook.js"; +import { registerFlowLifecycle } from "../../src/platform/opencode/lifecycle.js"; +import { createNativeFlowHost } from "./native-flow-host.js"; + +type Options = Parameters[0]; +type CommandName = Parameters[0]; +export async function continuationCommands( + options: Omit, +) { + const native = await createNativeFlowHost(process.cwd()); + const lifecycle = await registerFlowLifecycle({ + ...options, + ctx: native.context, + runtimeGuard: { + assertOperational() {}, + query: () => ({ + operational: true, + reason: "sole-instance", + message: "active", + }), + release: () => true, + }, + log() {}, + codingModels: new Map(), + }); + return { + native, + lifecycle, + async execute(name: CommandName, args: string, host = "ses_host") { + native.ensureSession(host, { + agent: "build", + model: { providerID: "test", id: "manager" }, + }); + await native.command(name, args, host); + const input = native.admissions.findLast( + (input) => input.type === "user" && input.sessionID === host, + ); + if (input?.type !== "user") + throw new Error("Missing accepted native command receipt."); + const authority = options.autoDrive.captureAuthority(host); + return { + id: input.id, + text: input.payload.text, + metadata: authority + ? { "opencode-plugin-flow/auto": authority.token } + : null, + }; + }, + dispose: async () => { + await lifecycle.dispose(); + await native.dispose(); + }, + }; +} diff --git a/tests/fixtures/deferred-capture-outcome.ts b/tests/fixtures/deferred-capture-outcome.ts index 01dd9b5d..cefa5385 100644 --- a/tests/fixtures/deferred-capture-outcome.ts +++ b/tests/fixtures/deferred-capture-outcome.ts @@ -67,7 +67,10 @@ export function nativeTrace(calls: ScenarioGradeInput["allCalls"]) { }, parts: [ { - id: call.native?.partId, + id: + call.native && "partId" in call.native + ? call.native.partId + : undefined, sessionID: "ses_root", messageID: call.native?.messageId, callID: call.native?.callId, diff --git a/tests/fixtures/eval-cancellation-child.ts b/tests/fixtures/eval-cancellation-child.ts index 7a0ab912..fd37f21b 100644 --- a/tests/fixtures/eval-cancellation-child.ts +++ b/tests/fixtures/eval-cancellation-child.ts @@ -3,6 +3,7 @@ import { mock } from "bun:test"; import { mkdir, readdir, writeFile } from "node:fs/promises"; import { join } from "node:path"; +import { OpenCode } from "@opencode/client"; import { withCampaignSignals } from "../../evals/campaign-stop.js"; import { providerFailure } from "../../evals/failure-origin.js"; import type { CommandEnd, EvalHost, Outcome } from "../../evals/harness.js"; @@ -51,6 +52,14 @@ globalThis.fetch = Object.assign( }, { preconnect: fetch.preconnect }, ); +const nativeClient = () => + OpenCode.make({ + baseUrl: "http://fixture", + fetch: Object.assign( + (...args: Parameters) => globalThis.fetch(...args), + { preconnect: fetch.preconnect }, + ), + }); const realHarness = { ...(await import("../../evals/harness.js")) }; const realProvenance = { ...(await import("../../evals/provenance.js")) }; const realStore = { ...(await import("../../evals/report-store.js")) }; @@ -201,39 +210,48 @@ class FakeEvalHost { this.project, this.signal, ]) as EvalHost; - Object.assign(this.pollHost, { baseUrl: "http://fixture" }); + Object.assign(this.pollHost, { + baseUrl: "http://fixture", + nativeClient: nativeClient(), + }); const originalFetch = globalThis.fetch; this.restoreFetch = () => { globalThis.fetch = originalFetch; }; globalThis.fetch = Object.assign( async (input: string | URL | Request) => { - const url = String(input); - if (!url.startsWith("http://fixture/")) + const url = new URL(String(input)); + if (url.origin !== "http://fixture") throw new Error("Unexpected fixture request"); - if (url.endsWith("/command")) return Response.json({}); - if (url.endsWith("/message")) { + if ( + url.pathname.endsWith("/command") || + url.pathname.endsWith("/model") + ) + return new Response(null, { status: 204 }); + if (url.pathname.endsWith("/message")) { event("actual-progress-poll"); - return Response.json([ - { - info: { - id: "message", - role: "assistant", - sessionID: sessionId, + return Response.json({ + data: [ + { + id: "msg_fixture_provider", + type: "assistant", + agent: "build", + model: { providerID: "fixture", id: "model" }, time: { created: 1, completed: 2 }, error: { - name: "FixtureProviderUnavailable", - data: { message: "Fake polled provider failure" }, + type: "FixtureProviderUnavailable", + message: "Fake polled provider failure", }, + content: [], }, - parts: [], - }, - ]); + ], + cursor: {}, + }); } - if (url.endsWith("/abort")) { + if (url.pathname.endsWith("/interrupt")) { event("actual-provider-abort"); if (mode === "provider-poll-stop") return stopHere(this.signal); - return Response.json(true); + return Response.json({ interrupted: true }); } event("unexpected-enrichment"); throw new Error( @@ -265,39 +283,62 @@ class FakeEvalHost { return this.pollHost.outcome(sessionIds, 1, this.signal); } if (mode === "provider-transcript-stop" && this.attempt === 2) { + await mkdir(this.project, { recursive: true }); const host = Reflect.construct(realHarness.EvalHost, [ this.project, this.project, ]) as EvalHost; - Object.assign(host, { baseUrl: "http://fixture" }); + Object.assign(host, { + baseUrl: "http://fixture", + nativeClient: nativeClient(), + }); const parent = sessionIds[0] ?? "id"; const originalFetch = globalThis.fetch; globalThis.fetch = Object.assign( async (input: string | URL | Request) => { - const url = String(input); - if (!url.startsWith("http://fixture/")) + const url = new URL(String(input)); + if (url.origin !== "http://fixture") throw new Error("Unexpected fixture request"); - if (url.endsWith(`/session/${parent}/children`)) - return Response.json([{ id: "reviewer", parentID: parent }]); - if (url.endsWith("/children")) return Response.json([]); - if (url.endsWith(`/session/${parent}/message`)) - return Response.json([ - { - info: { - id: "message", - role: "assistant", - sessionID: parent, + if (url.pathname === "/api/session") + return Response.json({ + data: [ + { + id: "reviewer", + parentID: parent, + agent: "flow-reviewer", + location: { directory: this.project }, + }, + ], + cursor: {}, + }); + if (url.pathname === `/api/session/${parent}/message`) + return Response.json({ + data: [ + { + id: "msg_fixture_provider", + type: "assistant", + agent: "build", + model: { providerID: "fixture", id: "model" }, time: { created: 1, completed: 2 }, error: { - name: "FixtureProviderUnavailable", - data: { message: "Fake earlier provider failure" }, + type: "FixtureProviderUnavailable", + message: "Fake earlier provider failure", }, + content: [], }, - parts: [], - }, - ]); - if (url.endsWith("/session/reviewer/message")) + ], + cursor: {}, + }); + if (url.pathname === "/api/session/reviewer/message") return stopHere(this.signal); + if (url.pathname === `/api/session/${parent}`) + return Response.json({ + data: { + id: parent, + agent: "build", + location: { directory: this.project }, + }, + }); throw new Error("Unexpected fixture endpoint"); }, { preconnect: originalFetch.preconnect }, diff --git a/tests/fixtures/native-auth-plugin.ts b/tests/fixtures/native-auth-plugin.ts new file mode 100644 index 00000000..d98406e2 --- /dev/null +++ b/tests/fixtures/native-auth-plugin.ts @@ -0,0 +1,130 @@ +import { + Credential, + Integration, + Model, + Plugin, + Provider, +} from "@opencode/plugin"; +import { Rpc } from "@opencode/plugin/rpc"; +import { object, refuse } from "../../evals/native-auth/protocol.js"; + +export const FixtureRpc = Rpc.define({ + id: "native-auth.fixture", + methods: { + stats: { + input: { type: "object", properties: {} }, + output: { type: "object" }, + }, + }, + events: {}, +}); +let refreshes = 0; +let foreignRequests = 0; +let directChildRefreshes = 0; +let refreshing: Promise | undefined; +let instances = 0; +let original: typeof fetch; + +export default Plugin.define({ + id: "native-auth.fixture", + async setup(ctx) { + if ( + !object(ctx.options) || + (ctx.options.role !== "source" && ctx.options.role !== "child") || + typeof ctx.options.baseURL !== "string" + ) + refuse(); + const role = ctx.options.role; + const baseURL = ctx.options.baseURL; + const origin = new URL(baseURL).origin; + if (new URL(baseURL).hostname !== "127.0.0.1") refuse(); + if (instances++ === 0) { + original = globalThis.fetch; + globalThis.fetch = Object.assign( + async (input: Parameters[0], init?: RequestInit) => { + const url = new URL( + input instanceof Request ? input.url : String(input), + ); + if ( + url.hostname !== "127.0.0.1" && + url.href !== "https://models.dev/api.json" + ) { + foreignRequests++; + throw new Error("Synthetic auth fixture refused network request."); + } + return original(input, init); + }, + { preconnect: original.preconnect }, + ); + } + await ctx.integration.transform((editor) => + editor.method.update({ + integrationID: "openai", + method: { + type: "oauth", + id: "native-auth-fixture-method", + label: "Synthetic OAuth", + }, + authorize: async () => refuse(), + refresh: async (credential) => { + if (role === "child") { + directChildRefreshes++; + refuse(); + } + if (credential.expires > Date.now() + 60000) return credential; + if (refreshing) return refreshing; + refreshing = (async () => { + refreshes++; + await Bun.sleep(10); + return Credential.OAuth.make({ + ...credential, + methodID: Integration.MethodID.make("native-auth-fixture-method"), + access: `synthetic-owner-access-${refreshes}`, + refresh: `synthetic-owner-refresh-${refreshes}`, + expires: Date.now() + 3600000, + }); + })(); + try { + return await refreshing; + } finally { + refreshing = undefined; + } + }, + }), + ); + const providerID = Provider.ID.make("native-auth-fixture"); + const model = Model.Info.default(providerID, Model.ID.make("model")); + await ctx.provider.transform((editor) => + editor.add({ + info: { + ...Provider.Info.empty(providerID), + integrationID: Integration.ID.make("openai"), + activation: "enabled", + name: "Synthetic native auth", + package: "@opencode/ai/providers/openai-compatible", + settings: { baseURL }, + }, + models: [{ ...model, modelID: Model.ID.make("wire-model") }], + }), + ); + await ctx.session.hook("title", (input) => { + input.result = "Synthetic native auth"; + }); + await ctx.session.hook("retry", (input) => { + input.decision = { retry: false }; + }); + await ctx.session.hook("http.request", (input) => { + if (new URL(input.request.url).origin !== origin) { + foreignRequests++; + refuse(); + } + }); + const registration = await ctx.rpc.register(FixtureRpc, { + stats: async () => ({ refreshes, foreignRequests, directChildRefreshes }), + }); + return async () => { + await registration.dispose(); + if (--instances === 0) globalThis.fetch = original; + }; + }, +}); diff --git a/tests/fixtures/native-auth-smoke.ts b/tests/fixtures/native-auth-smoke.ts new file mode 100644 index 00000000..0a82a137 --- /dev/null +++ b/tests/fixtures/native-auth-smoke.ts @@ -0,0 +1,534 @@ +import { strict as assert } from "node:assert"; +import { mkdir, mkdtemp, writeFile } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { pathToFileURL } from "node:url"; +import { isDeepStrictEqual } from "node:util"; +import { OpenCode } from "@opencode/client"; +import { Service } from "@opencode/client/service"; +import { + nativeAccountCLI, + startNativeAccountBroker, +} from "../../evals/native-auth/broker.js"; +import { + connectNativeAccountSource, + stageNativeAuthClient, +} from "../../evals/native-auth/host.js"; +import { + object, + PLACEHOLDER_REFRESH, +} from "../../evals/native-auth/protocol.js"; +import { FixtureRpc } from "./native-auth-plugin.js"; +import { localScriptedProvider } from "./opencode-v2-scripted-provider.js"; + +process.umask(0o077); +const configuredBinary = process.env.FLOW_OPENCODE_NATIVE_EXECUTABLE; +if (!configuredBinary) + throw new Error("A private native executable is required."); +const binary: string = configuredBinary; +const root = await mkdtemp(join(tmpdir(), "flow-native-auth-production-")); +const fixture = localScriptedProvider(); +let bearerVerified = false; +let expectedAccess = ""; +const proxy = Bun.serve({ + hostname: "127.0.0.1", + port: 0, + async fetch(request) { + bearerVerified = + request.headers.get("authorization") === `Bearer ${expectedAccess}`; + const url = new URL(request.url); + url.host = fixture.server.url.host; + return fetch(new Request(url.href, request)); + }, +}); +const children: { + pid: number; + exitCode: number | null; + kill(signal?: "SIGTERM"): void; + exited: Promise; +}[] = []; +const receipt: Record = { root, cloudRequests: 0 }; +let stage = "setup"; +let broker: Awaited> | undefined; +let source: ReturnType | undefined; +async function host( + role: "source" | "child", + plugin?: { package: string; options: object }, +) { + const dir = join(root, role); + await mkdir(dir, { mode: 0o700 }); + for (const name of [ + "home", + "config", + "cache", + "data", + "state", + "tmp", + "project", + ]) + await mkdir(join(dir, name), { mode: 0o700 }); + await mkdir(join(dir, "config/opencode"), { mode: 0o700 }); + const fixtureDirectory = join(dir, "fixture"); + await mkdir(fixtureDirectory, { mode: 0o700 }); + await writeFile( + join(fixtureDirectory, "index.ts"), + `export {default} from ${JSON.stringify(pathToFileURL(join(import.meta.dir, "native-auth-plugin.ts")).href)};\n`, + { mode: 0o600 }, + ); + await writeFile( + join(dir, "config/opencode/opencode.json"), + JSON.stringify({ + update: "disable", + warming: false, + share: "disabled", + plugins: [ + { + package: fixtureDirectory, + options: { role, baseURL: new URL("v1", proxy.url).href }, + }, + ], + }), + { mode: 0o600 }, + ); + if (plugin) + await writeFile( + join(dir, "project/opencode.json"), + JSON.stringify({ plugins: [plugin] }), + { mode: 0o600 }, + ); + const env = { + PATH: "/usr/bin:/bin", + HOME: join(dir, "home"), + XDG_CONFIG_HOME: join(dir, "config"), + XDG_CACHE_HOME: join(dir, "cache"), + XDG_DATA_HOME: join(dir, "data"), + XDG_STATE_HOME: join(dir, "state"), + TMPDIR: join(dir, "tmp"), + ...(role === "child" ? { OPENAI_API_KEY: "synthetic-ambient-key" } : {}), + }; + let client: ReturnType; + let child: { + pid: number; + exitCode: number | null; + kill(signal?: "SIGTERM"): void; + exited: Promise; + }; + if (role === "source") { + for (const key of Object.keys(process.env)) delete process.env[key]; + for (const [key, value] of Object.entries(env)) process.env[key] = value; + const allocation = Bun.serve({ + hostname: "127.0.0.1", + port: 0, + fetch: () => new Response(), + }); + const privatePort = allocation.port; + allocation.stop(true); + await nativeAccountCLI( + binary, + ["service", "set", "port", String(privatePort)], + join(dir, "project"), + ); + const endpoint = await Service.ensure({ + command: [binary, "serve", "--service"], + env, + file: join(dir, "state/opencode/service.json"), + version: "2.1.4", + }); + client = OpenCode.make({ + baseUrl: endpoint.url, + headers: Service.headers(endpoint), + }); + const info = await client.server.info(); + let stopping = false; + let resolveExit: (code: number) => void = () => {}; + let rejectExit: (error: Error) => void = () => {}; + const exited = new Promise((resolve, reject) => { + resolveExit = resolve; + rejectExit = reject; + }); + const managed = { + pid: info.pid, + exitCode: null as number | null, + exited, + kill: () => { + if (stopping) return; + stopping = true; + void (async () => { + try { + process.kill(info.pid, "SIGTERM"); + } catch {} + for (let i = 0; i < 320; i++) { + try { + process.kill(info.pid, 0); + } catch { + managed.exitCode = 0; + resolveExit(0); + return; + } + if (i === 160) + try { + process.kill(info.pid, "SIGKILL"); + } catch {} + await Bun.sleep(25); + } + rejectExit(new Error("Private native service did not stop")); + })(); + }, + }; + child = managed; + } else { + const listener = Bun.serve({ + hostname: "127.0.0.1", + port: 0, + fetch: () => new Response(), + }); + const port = listener.port; + listener.stop(true); + const spawned = Bun.spawn( + [binary, "serve", "--hostname", "127.0.0.1", "--port", String(port)], + { cwd: join(dir, "project"), env, stdout: "pipe", stderr: "pipe" }, + ); + child = spawned; + let password = ""; + async function consume(stream: ReadableStream) { + let pending = ""; + for await (const chunk of stream) { + pending = (pending + new TextDecoder().decode(chunk)).slice(-4096); + password = pending.match(/server password (\S+)/)?.[1] ?? password; + } + } + void consume(spawned.stdout); + void consume(spawned.stderr); + const transport: typeof fetch = Object.assign( + (input: Parameters[0], init?: RequestInit) => { + const headers = new Headers(init?.headers); + headers.set( + "authorization", + `Basic ${Buffer.from(`opencode:${password}`).toString("base64")}`, + ); + return fetch(input, { + ...init, + headers, + signal: AbortSignal.any([ + ...(init?.signal ? [init.signal] : []), + AbortSignal.timeout(15000), + ]), + }); + }, + { preconnect: globalThis.fetch.preconnect }, + ); + client = OpenCode.make({ + baseUrl: `http://127.0.0.1:${port}`, + fetch: transport, + }); + for (let i = 0; ; i++) { + try { + await client.server.info(); + break; + } catch { + if (i === 100) throw new Error("Private source unavailable"); + await Bun.sleep(50); + } + } + } + children.push(child); + const project = join(dir, "project"); + const ready = await client.session.create({ + location: { directory: project }, + title: "Native auth readiness", + }); + await client.session.remove({ sessionID: ready.id }); + await client.config.get({ location: { directory: project } }); + for (let i = 0; ; i++) { + const plugins = ( + await client.plugin.list({ location: { directory: project } }) + ).data; + const required = [ + "native-auth.fixture", + ...(plugin ? ["flow.native-account-client"] : []), + ]; + if ( + required.every((id) => + plugins.some( + (candidate) => + candidate.id === id && candidate.state.status === "active", + ), + ) + ) + break; + if (i === 100) throw new Error("Native auth plugins did not activate"); + await Bun.sleep(50); + } + receipt[`${role}PluginsActiveBeforeCredentialImport`] = true; + return { dir, env, client, child, project: join(dir, "project") }; +} +try { + const owner = await host("source"); + for (const [key, value] of Object.entries(owner.env)) + process.env[key] = value; + stage = "default-cli-discovery"; + const info: unknown = JSON.parse( + await nativeAccountCLI(binary, ["api", "GET", "/api/info"], owner.project), + ); + assert.equal(object(info) && info.version === "2.1.4", true); + const query = new URLSearchParams({ "location[directory]": owner.project }); + const stats: unknown = JSON.parse( + await nativeAccountCLI( + binary, + [ + "api", + "POST", + `/api/rpc/${FixtureRpc.id}/stats?${query}`, + "--data", + JSON.stringify({ input: {} }), + ], + owner.project, + ), + ); + assert.equal(object(stats) && object(stats.output), true); + receipt.defaultCliAuthenticated = true; + receipt.exactRpcQueryAccepted = true; + const baseline = { + id: "native-auth-selected", + integrationID: "openai", + label: "Selected synthetic account", + active: true, + value: { + type: "oauth" as const, + methodID: "native-auth-fixture-method", + access: "synthetic-before-access", + refresh: "synthetic-before-refresh", + expires: 0, + metadata: { accountId: "synthetic-account", nested: { tier: "fixture" } }, + }, + }; + await owner.client.credential.create({ ...baseline, activate: true }); + const other = { + ...baseline, + id: "native-auth-other", + label: "Other synthetic account", + active: false, + value: { + ...baseline.value, + refresh: "synthetic-unrelated-refresh", + expires: 4102444800000, + }, + }; + await owner.client.credential.create({ ...other, activate: false }); + stage = "broker"; + broker = await startNativeAccountBroker({ + opencodeExecutable: binary, + models: ["native-auth-fixture/model"], + }); + source = connectNativeAccountSource(broker.configuration); + const unauthorized = await fetch( + new URL("resolve", broker.configuration.url), + { method: "POST", body: "{}" }, + ); + assert.equal(unauthorized.status, 401); + receipt.capabilityRequired = true; + const unused = await source.prepareHost({ + directory: join(root, "unused-startup"), + models: ["native-auth-fixture/model"], + }); + await unused.dispose(); + await unused.dispose(); + receipt.unusedStartupLeaseReleased = true; + const artifact = await stageNativeAuthClient(join(root, "sealed-client")); + const auth = await source.prepareHost({ + directory: join(root, "attachment"), + models: ["native-auth-fixture/model"], + stagedClient: artifact, + }); + await assert.rejects( + source.prepareHost({ + directory: join(root, "overlap"), + models: ["native-auth-fixture/model"], + }), + (error) => + error instanceof Error && + error.message === "Native account authentication failed.", + ); + receipt.concurrentHostRefused = true; + assert.deepEqual(Object.keys(auth.plugin.options), ["configurationPath"]); + assert.equal( + JSON.stringify(auth.credentialHandoff.entries).includes( + baseline.value.refresh, + ), + false, + ); + const child = await host("child", auth.plugin); + const failedExecution = async () => { + const session = await child.client.session.create({ + title: "Authentication must fail closed", + location: { directory: child.project }, + model: { providerID: "native-auth-fixture", id: "model" }, + }); + const controller = new AbortController(); + let failed = false; + const observe = (async () => { + try { + for await (const event of child.client.event.subscribe({ + signal: controller.signal, + })) + if ( + event.type === "session.execution.failed" && + event.data.sessionID === session.id + ) + failed = true; + } catch {} + })(); + await Bun.sleep(20); + await child.client.session.prompt({ + sessionID: session.id, + text: "No model request may be sent", + }); + await child.client.session.wait({ sessionID: session.id }); + await Bun.sleep(50); + controller.abort(); + await observe; + return failed; + }; + stage = "ambient-fallback-negative"; + assert.equal(await failedExecution(), true); + assert.equal(fixture.requests.length, 0); + receipt.ambientFallbackRefused = true; + for (const entry of auth.credentialHandoff.entries) + await child.client.credential.create({ + id: entry.id, + integrationID: entry.integrationID, + label: entry.label, + value: entry.value, + activate: entry.active, + }); + const ownerRows = await owner.client.credential.list(); + const current = ownerRows.find((entry) => entry.id === baseline.id); + assert.equal(current?.value.type === "oauth", true); + if (current?.value.type !== "oauth") + throw new Error("Synthetic source absent"); + expectedAccess = current.value.access; + receipt.nativeRotationPersisted = + current.value.refresh !== baseline.value.refresh; + receipt.unrelatedPreserved = isDeepStrictEqual( + ownerRows.find((entry) => entry.id === other.id), + other, + ); + assert.equal(receipt.unrelatedPreserved, true); + stage = "child-model"; + const session = await child.client.session.create({ + title: "Auth candidate proof", + location: { directory: child.project }, + model: { providerID: "native-auth-fixture", id: "model" }, + }); + await child.client.session.prompt({ + sessionID: session.id, + text: "Synthetic loopback response", + }); + await child.client.session.wait({ sessionID: session.id }); + const messages = await child.client.message.list({ sessionID: session.id }); + assert.equal( + messages.data.some( + (message) => message.type === "assistant" && !message.error, + ), + true, + ); + assert.equal(fixture.requests.length, 1); + assert.equal(bearerVerified, true); + const after = await child.client.credential.list(); + assert.equal( + after.every( + (entry) => + entry.value.type === "oauth" && + entry.value.refresh === PLACEHOLDER_REFRESH, + ), + true, + ); + await auth.credentialHandoff.onStop({ + before: auth.credentialHandoff.entries, + after, + }); + const fixtureStats = await child.client + .rpc(FixtureRpc) + .stats({}, { location: { directory: child.project } }); + assert.equal( + object(fixtureStats) && + fixtureStats.foreignRequests === 0 && + fixtureStats.directChildRefreshes === 0, + true, + ); + receipt.fixtureStats = fixtureStats; + receipt.nativeBearerVerified = bearerVerified; + receipt.localModelRequests = fixture.requests.length; + stage = "other-account-negative"; + await child.client.credential.create({ + id: "native-auth-unapproved-child", + integrationID: "openai", + activate: true, + value: { type: "key", key: "synthetic-unapproved-key" }, + }); + assert.equal(await failedExecution(), true); + assert.equal(fixture.requests.length, 1); + await child.client.credential.activate({ credentialID: baseline.id }); + receipt.otherAccountRefused = true; + stage = "dispose"; + const beforeLocations = await owner.client.debug.location.list(); + await broker.dispose(); + await broker.dispose(); + broker = undefined; + const afterLocations = await owner.client.debug.location.list(); + receipt.sourceLocationEvicted = + beforeLocations.some((location) => + location.directory.includes("flow-native-account-"), + ) && + !afterLocations.some((location) => + location.directory.includes("flow-native-account-"), + ); + assert.equal(receipt.sourceLocationEvicted, true); + stage = "cached-access-source-loss"; + assert.equal( + after.some( + (entry) => + entry.value.type === "oauth" && entry.value.expires > Date.now(), + ), + true, + ); + assert.equal(await failedExecution(), true); + assert.equal(fixture.requests.length, 1); + receipt.validCachedAccessRefusedWithoutSource = true; + child.child.kill("SIGTERM"); + await child.child.exited; + await auth.dispose().catch(() => {}); + await auth.dispose().catch(() => {}); + await source.dispose().catch(() => {}); + receipt.status = "passed"; +} catch { + receipt.status = "failed"; + receipt.stage = stage; + process.exitCode = 1; +} finally { + await source?.dispose().catch(() => {}); + await broker?.dispose().catch(() => {}); + for (const child of children) + if (child.exitCode === null) child.kill("SIGTERM"); + await Promise.all(children.map((child) => child.exited)); + fixture.server.stop(true); + proxy.stop(true); + const processes = children.map((child) => { + let alive = true; + try { + process.kill(child.pid, 0); + } catch { + alive = false; + } + return { pid: child.pid, alive }; + }); + receipt.processes = processes; + receipt.processesStopped = processes.every((child) => !child.alive); + assert.equal(receipt.processesStopped, true); + await writeFile( + join(root, "receipt.json"), + JSON.stringify(receipt, null, 2), + { mode: 0o600 }, + ); + console.log(JSON.stringify(receipt, null, 2)); +} diff --git a/tests/fixtures/native-eval-client.ts b/tests/fixtures/native-eval-client.ts new file mode 100644 index 00000000..a93b20c8 --- /dev/null +++ b/tests/fixtures/native-eval-client.ts @@ -0,0 +1,287 @@ +import { afterAll, beforeAll } from "bun:test"; +import { mkdtemp, rm } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { + OpenCode, + type OpenCodeClient, + type SessionInfo, + type SessionMessageAssistant, + type SessionMessageAssistantTool, + type SessionMessageInfo, + type SessionMessagesResponse, + type SessionsResponse, +} from "@opencode/client"; +import { EvalHost } from "../../evals/harness.js"; +import { authorizePaidRun } from "../../scripts/paid-budget.js"; + +export const fixtureModel = "fixture/model"; +export type NativeRequest = { url: URL; method: string; init: RequestInit }; +export type NativeHandler = ( + request: NativeRequest, +) => Response | Promise; +export type NativeTokens = NonNullable; +export const zeroNativeTokens: NativeTokens = { + input: 0, + output: 0, + reasoning: 0, + cache: { read: 0, write: 0 }, +}; + +export function nativeSession( + id: string, + directory: string, + options: Partial = {}, +): SessionInfo { + return { + id, + projectID: "project_fixture", + agent: "build", + cost: 0, + tokens: structuredClone(zeroNativeTokens), + time: { created: 1, updated: 1 }, + location: { directory }, + ...options, + }; +} + +export function nativeAssistant( + id: string, + options: Partial> = {}, +): SessionMessageAssistant { + return { + id, + type: "assistant", + agent: "build", + model: { providerID: "fixture", id: "model" }, + time: { created: 1 }, + content: [], + ...options, + }; +} + +export function nativeTool( + id: string, + name: string, + state: SessionMessageAssistantTool["state"], + time: SessionMessageAssistantTool["time"] = { created: 1 }, +): SessionMessageAssistantTool { + return { type: "tool", id, name, state, time }; +} + +export function messagePage( + data: SessionMessageInfo[], + next?: string, +): Response { + return Response.json({ + data, + cursor: next ? { next } : {}, + } satisfies SessionMessagesResponse); +} + +export function sessionPage(data: SessionInfo[], next?: string): Response { + return Response.json({ + data, + cursor: next ? { next } : {}, + } satisfies SessionsResponse); +} + +export function sessionReply(data: SessionInfo): Response { + return Response.json({ data }); +} + +export function nativeClient(handler: NativeHandler): OpenCodeClient { + const transport: typeof fetch = Object.assign( + async (input: Parameters[0], init?: RequestInit) => { + const url = new URL(input instanceof Request ? input.url : String(input)); + if (url.origin !== "http://native-eval.invalid") + throw new Error( + `Fixture refused transport outside its origin ${url.origin}`, + ); + return handler({ url, method: init?.method ?? "GET", init: init ?? {} }); + }, + { preconnect: globalThis.fetch.preconnect }, + ); + return OpenCode.make({ + baseUrl: "http://native-eval.invalid", + fetch: transport, + }); +} + +export function nativeHost( + project: string, + client: OpenCodeClient, + signal?: AbortSignal, +): EvalHost { + const host: unknown = Reflect.construct(EvalHost, [project, project, signal]); + if (!(host instanceof EvalHost)) + throw new Error("Expected EvalHost fixture."); + Reflect.set(host, "nativeClient", client); + return host; +} + +export function useNativeFixtureBudget(): void { + let directory: string; + let previous: string | undefined; + beforeAll(async () => { + previous = process.env.FLOW_EVAL_AUTHORIZATION; + directory = await mkdtemp(join(tmpdir(), "native-fixture-budget-")); + await authorizePaidRun(directory, { + schemaVersion: 1, + purpose: "In-memory native SDK transport fixtures only", + models: [fixtureModel, "fixture/manager"], + maxDispatches: 1000, + expiresAt: new Date(Date.now() + 3600000).toISOString(), + }); + process.env.FLOW_EVAL_AUTHORIZATION = directory; + }); + afterAll(async () => { + if (previous === undefined) delete process.env.FLOW_EVAL_AUTHORIZATION; + else process.env.FLOW_EVAL_AUTHORIZATION = previous; + await rm(directory, { recursive: true, force: true }); + }); +} + +export function nativeHttpClient(handler: NativeHandler): { + client: OpenCodeClient; + stop: () => void; +} { + const server = Bun.serve({ + hostname: "127.0.0.1", + port: 0, + async fetch(request) { + const body = request.method === "GET" ? undefined : await request.text(); + return handler({ + url: new URL(request.url), + method: request.method, + init: { + method: request.method, + signal: request.signal, + ...(body ? { body } : {}), + }, + }); + }, + }); + const transport: typeof fetch = Object.assign( + (input: Parameters[0], init?: RequestInit) => { + const url = new URL(input instanceof Request ? input.url : String(input)); + if (url.origin !== server.url.origin) + throw new Error("Fixture refused external network."); + return globalThis.fetch(input, init); + }, + { preconnect: globalThis.fetch.preconnect }, + ); + return { + client: OpenCode.make({ baseUrl: server.url.toString(), fetch: transport }), + stop: () => { + server.stop(true); + }, + }; +} + +async function runIsolationServer(): Promise { + const { readFile } = await import("node:fs/promises"); + const { join } = await import("node:path"); + for (const name of [ + "OPENCODE_CONFIG", + "OPENCODE_CONFIG_CONTENT", + "OPENCODE_CONFIG_DIR", + ]) + if (process.env[name] !== undefined) + throw new Error(`Ambient config leaked through ${name}`); + const config: unknown = JSON.parse( + await readFile(join(process.cwd(), "opencode.json"), "utf8"), + ); + if ( + !config || + typeof config !== "object" || + !("plugins" in config) || + !Array.isArray(config.plugins) || + config.plugins.length !== 1 || + !config.plugins.some((plugin: { package?: string }) => + plugin.package?.endsWith("flow.eval.http-transport"), + ) + ) + throw new Error("Expected generated native config with no plugins."); + const configuredPlugins = config.plugins; + const info: SessionInfo = { + id: "ses_ready", + projectID: "project_fixture", + agent: "build", + cost: 0, + tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } }, + time: { created: 1, updated: 1 }, + location: { directory: process.cwd() }, + }; + const port = Number(process.argv[process.argv.indexOf("--port") + 1]); + const server = Bun.serve({ + hostname: "127.0.0.1", + port, + fetch(request) { + const path = new URL(request.url).pathname; + if (path === "/api/info") + return Response.json({ + version: "2.1.4", + pid: process.pid, + urls: [], + paths: { tmp: process.env.TMPDIR ?? "" }, + } satisfies Awaited>); + if (path === "/api/session" && request.method === "POST") + return Response.json({ data: info }); + if (path === "/api/session/ses_ready" && request.method === "DELETE") + return new Response(null, { status: 204 }); + if (path === "/api/plugin") { + const plugin = configuredPlugins[0]; + if ( + !plugin || + typeof plugin !== "object" || + !("package" in plugin) || + typeof plugin.package !== "string" + ) + throw new Error("Missing local transport entry"); + return Response.json({ + location: { directory: process.cwd() }, + data: [ + { + id: "flow.eval.http-transport", + source: { type: "local", path: plugin.package }, + features: { server: true }, + state: { status: "active" }, + }, + ], + }); + } + if (path === "/api/config") + return Response.json([ + { type: "document", info: { plugins: [] } }, + ] satisfies Awaited>); + return new Response("Unexpected native isolation endpoint", { + status: 404, + }); + }, + }); + console.log("server password native-fixture-only"); + process.on("SIGTERM", () => { + server.stop(true); + process.exit(0); + }); +} + +export async function nativeIsolationExecutable( + directory: string, +): Promise { + const { chmod, writeFile } = await import("node:fs/promises"); + const script = join(directory, "native-isolation-server.js"); + const executable = join(directory, "opencode-fixture"); + await writeFile( + script, + `(${runIsolationServer.toString()})().catch(error => { console.error(error); process.exit(1); });\n`, + ); + const quote = (value: string) => `'${value.replaceAll("'", "'\\''")}'`; + await writeFile( + executable, + `#!/bin/sh\nexec ${quote(process.execPath)} ${quote(script)} "$@"\n`, + ); + await chmod(executable, 0o755); + return executable; +} diff --git a/tests/fixtures/native-flow-host.ts b/tests/fixtures/native-flow-host.ts new file mode 100644 index 00000000..61353058 --- /dev/null +++ b/tests/fixtures/native-flow-host.ts @@ -0,0 +1,809 @@ +import { realpath } from "node:fs/promises"; +import { join } from "node:path"; +import { + type AgentInfo, + OpenCode, + type OpenCodeEvent, + type SessionInboxSynthetic, + type SessionInboxUser, + type SessionInfo, + type SessionMessageAssistant, + type SessionMessageAssistantTool, + type SessionMessageInfo, + type SessionPromptInput, + type SessionSyntheticInput, +} from "@opencode/client"; +import type { Plugin } from "@opencode/plugin"; +import { Agent, Model } from "@opencode/plugin"; +import type { AgentEditor } from "@opencode/plugin/promise/agent"; +import type { + CommandDefinition, + CommandInvocation, +} from "@opencode/plugin/promise/command"; +import type { Hooks } from "@opencode/plugin/promise/registration"; +import type { + SessionContext, + SessionHooks, +} from "@opencode/plugin/promise/session"; +import type { + Info, + ToolContext, + ToolEditor, +} from "@opencode/plugin/promise/tool"; +import { CallID, Error as ToolError } from "@opencode/plugin/promise/tool"; +import { Location } from "@opencode/schema/location"; +import { Project } from "@opencode/schema/project"; +import { AbsolutePath } from "@opencode/schema/schema"; +import { Session } from "@opencode/schema/session"; +import { SessionMessage } from "@opencode/schema/session-message"; +import { Skill } from "@opencode/schema/skill"; +import { z } from "zod"; +import type { + NativeToolAfter, + NativeToolBefore, +} from "../../src/platform/opencode/sdk.js"; + +class HookRegistry { + readonly callbacks: { + [Key in keyof Spec]?: Array<(input: Spec[Key]) => void | Promise>; + } = {}; + readonly hook: Hooks = async (name, callback) => { + const callbacks = this.callbacks[name] ?? []; + this.callbacks[name] = callbacks; + callbacks.push(callback); + return { + dispose: async () => { + const index = callbacks.indexOf(callback); + if (index >= 0) callbacks.splice(index, 1); + }, + }; + }; + async emit( + name: Key, + input: Spec[Key], + ): Promise { + for (const callback of this.callbacks[name] ?? []) await callback(input); + } +} + +class NativeEvents { + readonly subscribers = new Set<{ + push(event: OpenCodeEvent): Promise; + }>(); + readonly subscribe: Plugin.Context["event"]["subscribe"] = ({ + signal, + } = {}) => { + const subscribers = this.subscribers; + return (async function* () { + type Entry = { event: OpenCodeEvent; done(): void }; + const queue: Entry[] = []; + let wake: (() => void) | undefined; + const abort = () => wake?.(); + const subscriber = { + push(event: OpenCodeEvent) { + return new Promise((done) => { + queue.push({ event, done }); + wake?.(); + }); + }, + }; + subscribers.add(subscriber); + signal?.addEventListener("abort", abort); + try { + while (!signal?.aborted) { + if (!queue.length) + await new Promise((resolve) => { + wake = resolve; + }); + const next = queue.shift(); + if (!next) continue; + try { + yield next.event; + } finally { + next.done(); + } + } + } finally { + subscribers.delete(subscriber); + signal?.removeEventListener("abort", abort); + for (const entry of queue) entry.done(); + } + })(); + }; + async emit(event: OpenCodeEvent) { + await Promise.all( + [...this.subscribers].map((subscriber) => subscriber.push(event)), + ); + } +} +const responseSchema = z.object({ + status: z.enum(["ok", "error"]), + summary: z.string(), + workflowData: z + .object({ + projection: z + .object({ + view: z.string(), + status: z.string(), + revision: z.number(), + sessionId: z.string().optional(), + goal: z.string().optional(), + }) + .passthrough() + .optional(), + operation: z + .object({ + operationId: z.string(), + revision: z.number(), + replayed: z.boolean(), + }) + .passthrough() + .optional(), + }) + .passthrough(), +}); +function parseFlowResponse(value: unknown) { + return responseSchema.parse(value); +} +export function projectionOf(value: unknown) { + const response = parseFlowResponse(value); + const projection = response.workflowData.projection; + if (!projection) throw new Error(response.summary); + return projection; +} +export function nativeText( + result: Awaited>, +): string { + const content = result.content; + return typeof content === "string" + ? content + : (content ?? []) + .filter((part) => part.type === "text") + .map((part) => part.text) + .join("\n"); +} +const unsupported = async (): Promise => { + throw new Error("Unexpected native API in this fixture."); +}; +const usage = () => ({ + input: 0, + output: 0, + reasoning: 0, + cache: { read: 0, write: 0 }, +}); + +export async function createNativeFlowHost( + directory: string, + options: Plugin.Context["options"] = {}, +) { + const workspace = await realpath(directory); + const path = AbsolutePath.make(workspace); + const location = new Location.Info({ + directory: path, + project: { + id: Project.ID.make("project_test"), + directory: path, + canonical: path, + }, + }); + const client = OpenCode.make({ + baseUrl: "http://native-fixture.invalid", + fetch: Object.assign(unsupported, { preconnect() {} }), + }); + const sessionHooks = new HookRegistry(); + const toolHooks = new HookRegistry<{ + "execute.before": NativeToolBefore; + "execute.after": NativeToolAfter; + }>(); + const events = new NativeEvents(); + const sessions = new Map(); + const messages = new Map(); + const prompts: SessionPromptInput[] = []; + const synthetics: SessionSyntheticInput[] = []; + const admissions: Array = []; + const tools = new Map(); + const commands = new Map(); + const agents = new Map>>(); + const transforms = new Set<(editor: AgentEditor) => void>(); + const active = new Map(); + const contexts = new Map(); + const permissions: Array< + Parameters[0] + > = []; + let sequence = 0; + const stamp = () => ++sequence; + function ensureSession( + id = "ses_manager", + input: Partial = {}, + ): SessionInfo { + const present = sessions.get(id); + if (present) return present; + const session: SessionInfo = { + id, + projectID: "project_test", + agent: "build", + model: { providerID: "test", id: "coding" }, + cost: 0, + tokens: usage(), + time: { created: stamp(), updated: sequence }, + location: { directory: workspace }, + ...input, + }; + sessions.set(id, session); + messages.set(id, []); + return session; + } + function history(id: string) { + ensureSession(id); + const value = messages.get(id); + if (!value) throw new Error("Missing native context."); + return value; + } + function agentInfo(id: string): AgentInfo { + const native = agents.get(id); + if (!native) throw new Error(`Unknown native agent ${id}.`); + return { + id: native.id, + name: native.name, + mode: native.mode, + hidden: native.hidden, + permissions: native.permissions, + request: { settings: {}, headers: {}, body: {} }, + ...(native.system ? { system: native.system } : {}), + ...(native.description ? { description: native.description } : {}), + ...(native.steps ? { steps: native.steps } : {}), + ...(native.model + ? { + model: { + providerID: native.model.providerID, + id: native.model.id, + ...(native.model.variant + ? { variant: native.model.variant } + : {}), + }, + } + : {}), + }; + } + const editor: AgentEditor = { + list: () => [...agents.values()], + get: (id) => agents.get(id), + default() {}, + remove(id) { + agents.delete(id); + }, + update(id, update) { + const value = agents.get(id) ?? Agent.Info.default(Agent.ID.make(id)); + update(value); + agents.set(id, value); + }, + }; + const toolEditor: ToolEditor = { + list: () => [...tools].map(([id, tool]) => ({ ...tool, id })), + get(id) { + const tool = tools.get(id); + return tool ? { ...tool, id } : undefined; + }, + namespace() {}, + add(tool) { + tools.set(tool.name, tool); + }, + update(id, update) { + const tool = tools.get(id); + if (tool) update(tool); + }, + remove(id) { + tools.delete(id); + }, + }; + async function prompt(input: SessionPromptInput): Promise { + const session = ensureSession(input.sessionID), + id = input.id ?? SessionMessage.ID.create(); + const hookPrompt: SessionHooks["prompt"]["prompt"] = { + text: input.text, + ...(input.files + ? { files: input.files.map((file) => ({ ...file })) } + : {}), + ...(input.agents + ? { agents: input.agents.map((agent) => ({ ...agent })) } + : {}), + ...(input.skills + ? { + skills: input.skills.map((skill) => ({ + ...skill, + id: Skill.ID.make(skill.id), + })), + } + : {}), + }; + await sessionHooks.emit("prompt", { + sessionID: Session.ID.make(session.id), + messageID: SessionMessage.ID.make(id), + prompt: hookPrompt, + delivery: input.delivery ?? "queue", + ...(input.metadata ? { metadata: input.metadata } : {}), + }); + const payload: SessionInboxUser["payload"] = { + text: input.text, + ...(input.files + ? { + files: input.files.map((file) => ({ + data: file.uri.split(";base64,")[1] ?? "", + mime: /^data:([^;]+)/.exec(file.uri)?.[1] ?? "text/plain", + source: { type: "uri" as const, uri: file.uri }, + ...(file.name ? { name: file.name } : {}), + ...(file.description ? { description: file.description } : {}), + ...(file.mention ? { mention: file.mention } : {}), + })), + } + : {}), + ...(input.agents + ? { agents: input.agents.map((agent) => ({ ...agent })) } + : {}), + ...(input.skills + ? { + skills: input.skills.map((skill) => ({ ...skill, name: skill.id })), + } + : {}), + ...(input.metadata ? { metadata: input.metadata } : {}), + }; + const receipt: SessionInboxUser = { + id, + sessionID: session.id, + type: "user", + payload, + time: { created: stamp() }, + delivery: input.delivery ?? "queue", + }; + prompts.push(structuredClone(input)); + admissions.push(structuredClone(receipt)); + history(session.id).push( + structuredClone({ + id, + type: "user" as const, + time: receipt.time, + ...payload, + }), + ); + active.delete(session.id); + return receipt; + } + async function synthetic( + input: SessionSyntheticInput, + ): Promise { + const id = input.id ?? SessionMessage.ID.create(); + const payload = { + text: input.text, + ...(input.description ? { description: input.description } : {}), + ...(input.metadata ? { metadata: input.metadata } : {}), + }; + const receipt: SessionInboxSynthetic = { + id, + sessionID: input.sessionID, + type: "synthetic", + payload, + time: { created: stamp() }, + delivery: input.delivery ?? "queue", + }; + synthetics.push(structuredClone(input)); + admissions.push(structuredClone(receipt)); + history(input.sessionID).push( + structuredClone({ + id, + type: "synthetic" as const, + time: receipt.time, + ...payload, + }), + ); + active.delete(input.sessionID); + return receipt; + } + const context: Plugin.Context = { + app: { name: "opencode", version: "2.1.4", channel: "test" }, + location, + options, + session: { + ...client.session, + get: async (input) => ensureSession(input.sessionID), + context: async (input) => history(input.sessionID), + list: async () => ({ data: [...sessions.values()], cursor: {} }), + prompt, + synthetic, + hook: sessionHooks.hook, + create: async (input) => + ensureSession(`ses_child_${stamp()}`, { + ...(input?.parentID ? { parentID: input.parentID } : {}), + ...(input?.agent ? { agent: input.agent } : {}), + ...(input?.model ? { model: input.model } : {}), + }), + }, + agent: { + ...client.agent, + get: async (input) => ({ + location: { directory: workspace }, + data: agentInfo(input.agentID), + }), + list: async () => ({ + location: { directory: workspace }, + data: [...agents.keys()].map(agentInfo), + }), + transform: async (apply) => { + transforms.add(apply); + apply(editor); + return { + dispose: async () => { + transforms.delete(apply); + }, + }; + }, + reload: async () => { + for (const apply of transforms) apply(editor); + }, + }, + tool: { + transform: async (apply) => { + apply(toolEditor); + return { + dispose: async () => { + tools.clear(); + }, + }; + }, + reload: async () => {}, + list: async () => toolEditor.list(), + hook: toolHooks.hook, + }, + command: { + ...client.command, + transform: async (apply) => { + const names: string[] = []; + apply({ + add(definition) { + commands.set(definition.name, definition); + names.push(definition.name); + }, + }); + return { + dispose: async () => { + for (const name of names) commands.delete(name); + }, + }; + }, + reload: async () => {}, + }, + event: events, + rpc: Object.assign(client.rpc, { + register: async () => ({ + dispose: async () => {}, + events: { emit: async () => {} }, + }), + }), + permission: { + ...client.permission, + hook: unsupported, + assert: async (input) => { + permissions.push(input); + }, + }, + model: { + ...client.model, + list: async () => ({ location: { directory: workspace }, data: [] }), + transform: unsupported, + reload: unsupported, + }, + provider: { + ...client.provider, + transform: async (apply) => { + apply({ + list: () => [], + get: () => undefined, + add() {}, + update() {}, + remove() {}, + models: { set() {}, update() {}, remove() {} }, + }); + return { dispose: async () => {} }; + }, + reload: unsupported, + }, + aisdk: { hook: unsupported }, + credential: { activate: unsupported }, + experimental: { terminal: { read: unsupported } }, + integration: { + ...client.integration, + transform: unsupported, + reload: unsupported, + connection: { + active: unsupported, + resolve: unsupported, + status: unsupported, + }, + }, + mcp: { ...client.mcp, transform: unsupported, reload: unsupported }, + reference: { + ...client.reference, + transform: unsupported, + reload: unsupported, + }, + shell: { hook: unsupported }, + skill: { ...client.skill, transform: unsupported, reload: unsupported }, + storage: { + get: unsupported, + set: unsupported, + remove: unsupported, + scan: unsupported, + }, + vcs: { ...client.vcs, transform: unsupported, reload: unsupported }, + websearch: { + ...client.websearch, + transform: unsupported, + reload: unsupported, + }, + worktree: { + ...client.worktree, + transform: unsupported, + reload: unsupported, + }, + generate: client.generate, + plugin: client.plugin, + }; + const cleanups: Array<() => void | Promise> = []; + async function beginTurn(host = "ses_manager") { + const session = ensureSession(host); + if ( + !history(host).some( + (message) => message.type === "user" || message.type === "synthetic", + ) + ) + await prompt({ sessionID: host, text: "Inspect current Flow state." }); + if (!session.agent || !session.model) + throw new Error("Unselected native session."); + const native = history(host); + const requestContext: SessionContext = { + sessionID: Session.ID.make(host), + agent: Agent.ID.make(session.agent), + model: Model.Ref.make({ + providerID: Model.Ref.parse( + `${session.model.providerID}/${session.model.id}`, + ).providerID, + id: Model.ID.make(session.model.id), + ...(session.model.variant + ? { variant: Model.VariantID.make(session.model.variant) } + : {}), + }), + system: [], + messages: native.flatMap((message) => + message.type === "user" || + message.type === "synthetic" || + (message.type === "compaction" && message.status === "completed") + ? [ + { + role: "user" as const, + id: message.id, + content: [ + { + type: "text" as const, + text: + message.type === "compaction" + ? message.summary + : message.text, + }, + ], + }, + ] + : [], + ), + tools: {}, + options: {}, + }; + await sessionHooks.emit("context", requestContext); + contexts.set(host, requestContext); + const request = new Request("http://native-fixture.invalid/primary"); + await sessionHooks.emit("http.request", { + ...requestContext, + kind: "primary", + request, + }); + const response = { + ...requestContext, + kind: "primary" as const, + request, + response: new Response("fixture-complete"), + }; + await sessionHooks.emit("http.response", response); + await response.response.text(); + const assistant: SessionMessageAssistant = { + id: SessionMessage.ID.create(), + type: "assistant", + agent: session.agent, + model: session.model, + content: [], + time: { created: stamp() }, + }; + native.push(assistant); + active.set(host, assistant); + return assistant; + } + async function toolContext( + name: string, + input: unknown, + host = "ses_manager", + ): Promise { + const assistant = active.get(host) ?? (await beginTurn(host)); + const id = CallID.make(`call_${stamp()}`); + const call: SessionMessageAssistantTool = { + type: "tool", + id, + name, + state: { + status: "running", + input: z.record(z.string(), z.json()).parse(input), + metadata: {}, + }, + time: { created: stamp() }, + }; + assistant.content.push(call); + return { + sessionID: Session.ID.make(host), + messageID: SessionMessage.ID.make(assistant.id), + agent: Agent.ID.make(assistant.agent), + id, + signal: new AbortController().signal, + progress: async () => {}, + }; + } + async function invoke(name: string, input: unknown, native: ToolContext) { + const definition = tools.get(name); + if (!definition) throw new Error(`Missing native tool ${name}.`); + const before: NativeToolBefore = { + tool: name, + input, + sessionID: native.sessionID, + messageID: native.messageID, + agent: native.agent, + id: native.id, + }; + await toolHooks.emit("execute.before", before); + try { + const result = await definition.execute(input, native); + await toolHooks.emit("execute.after", { + ...before, + status: "completed", + result, + }); + return result; + } catch (error) { + await toolHooks.emit("execute.after", { + ...before, + status: "error", + error: new ToolError({ message: String(error) }), + }); + throw error; + } + } + return { + context, + tools, + commands, + agents, + sessions, + messages, + prompts, + synthetics, + admissions, + permissions, + events, + sessionHooks, + toolHooks, + ensureSession, + history, + beginTurn, + toolContext, + invoke, + async load(plugin: Plugin.Plugin) { + const prior = process.env.XDG_STATE_HOME; + process.env.XDG_STATE_HOME = join(workspace, ".native-fixture-data"); + try { + const cleanup = await plugin.setup(context); + if (cleanup) cleanups.push(cleanup); + } finally { + if (prior === undefined) delete process.env.XDG_STATE_HOME; + else process.env.XDG_STATE_HOME = prior; + } + }, + async dispose() { + for (const cleanup of cleanups.splice(0).reverse()) await cleanup(); + }, + async command( + name: string, + args = "", + host = "ses_manager", + extra: Partial = {}, + ) { + ensureSession(host); + const command = commands.get(name); + if (!command) throw new Error(`Missing command ${name}.`); + await command.execute({ + sessionID: Session.ID.make(host), + prompt: { text: args, ...extra }, + delivery: "queue", + }); + return prompts.at(-1); + }, + async admit( + text: string, + host = "ses_manager", + extra: Omit = {}, + ) { + return prompt({ sessionID: host, text, ...extra }); + }, + async call(name: string, input: unknown, host = "ses_manager") { + return invoke(name, input, await toolContext(name, input, host)); + }, + async response(name: string, input: unknown, host = "ses_manager") { + const result = await this.call(name, input, host); + const parsed: unknown = JSON.parse(nativeText(result)); + return parseFlowResponse(parsed); + }, + async compact(host = "ses_manager", reason: "auto" | "manual" = "auto") { + if (!active.has(host)) await beginTurn(host); + const prior = contexts.get(host); + if (!prior) throw new Error("Missing primary context."); + const control = { + id: SessionMessage.ID.create(), + type: "compaction" as const, + status: "running" as const, + reason, + summary: "Fixture native summary.", + time: { created: stamp() }, + }; + history(host).push(control); + const input: SessionHooks["compaction"] = { ...prior, system: [] }; + await sessionHooks.emit("compaction", input); + messages.set(host, [{ ...control, status: "completed" }]); + active.delete(host); + return input.system; + }, + async idle( + host = "ses_manager", + outcome: "succeeded" | "failed" | "interrupted" = "succeeded", + ) { + const session = ensureSession(host); + const completedAt = stamp(); + session.time.idle = completedAt; + session.outcome = outcome; + history(host).push({ + id: SessionMessage.ID.create(), + type: "idle", + outcome, + time: { created: completedAt }, + }); + await events.emit({ + id: `evt_${stamp()}`, + created: stamp(), + durable: { aggregateID: host, seq: stamp(), version: 1 }, + ...(outcome === "succeeded" + ? { + type: "session.execution.succeeded" as const, + data: { sessionID: host }, + } + : outcome === "failed" + ? { + type: "session.execution.failed" as const, + data: { + sessionID: host, + error: { + type: "fixture", + message: "Fixture execution failed.", + }, + }, + } + : { + type: "session.execution.interrupted" as const, + data: { sessionID: host, reason: "user" as const }, + }), + }); + }, + }; +} +export type NativeFlowHost = Awaited>; diff --git a/tests/fixtures/opencode-v2-budget-plugin.ts b/tests/fixtures/opencode-v2-budget-plugin.ts new file mode 100644 index 00000000..4bbfcea1 --- /dev/null +++ b/tests/fixtures/opencode-v2-budget-plugin.ts @@ -0,0 +1,48 @@ +import { Plugin } from "@opencode/plugin"; +import { z } from "zod"; +import { isRecord } from "../../evals/host-observation.js"; +import BudgetPlugin from "../../evals/recovery-decisions/budget-plugin.js"; +import LocalPlugin from "./opencode-v2-smoke-plugin.js"; + +const Options = z + .object({ + baseURL: z.string(), + receipt: z.string(), + directory: z.string(), + authorizationDigest: z.string(), + readyPath: z.string(), + }) + .strict(); +export default Plugin.define({ + id: "flow-v2-native-budget-proof", + async setup(context) { + const options = Options.parse(context.options); + const cleanup = await LocalPlugin.setup({ + ...context, + options: { baseURL: options.baseURL, receipt: options.receipt }, + }); + await context.session.hook("http.request", async (input) => { + const body: unknown = await input.request.clone().json(); + if (!isRecord(body)) throw new Error("Invalid local budget body"); + input.request = new Request("https://api.x.ai/v1/chat/completions", { + method: "POST", + headers: input.request.headers, + body: JSON.stringify({ ...body, model: "grok-4.6" }), + signal: input.request.signal, + }); + }); + await BudgetPlugin.install(context, { + directory: options.directory, + authorizationDigest: options.authorizationDigest, + readyPath: options.readyPath, + }); + await context.session.hook("http.request", (input) => { + if (new URL(input.request.url).hostname !== "127.0.0.1") + throw new Error("Native budget proof must dispatch only to loopback"); + }); + await context.session.hook("retry", (input) => { + input.decision = { retry: false }; + }); + return cleanup; + }, +}); diff --git a/tests/fixtures/opencode-v2-capture-setup.ts b/tests/fixtures/opencode-v2-capture-setup.ts new file mode 100644 index 00000000..780f362a --- /dev/null +++ b/tests/fixtures/opencode-v2-capture-setup.ts @@ -0,0 +1,44 @@ +import { mock } from "bun:test"; +import assert from "node:assert/strict"; +import { Plugin } from "@opencode/plugin"; +import { CapturingRecoveryController } from "../../evals/recovery-decisions/capture.js"; +import type { RecoveryController } from "../../src/application/recovery-policy.js"; +import type { createFlowPlugin as FlowFactory } from "../../src/platform/opencode/plugin-composition.js"; + +const [workspace, captureDirectory] = process.argv.slice(2); +assert(workspace && captureDirectory); +let injected: RecoveryController | undefined; +let forwarded: unknown; +mock.module("../../src/platform/opencode/plugin-composition.js", () => ({ + createFlowPlugin: (dependencies: Parameters[0]) => + Plugin.define({ + id: "native-capture-composition-unit", + setup(context) { + injected = dependencies.createRecovery(); + forwarded = context.options; + return () => { + injected?.revoke(); + }; + }, + }), +})); +const { default: CapturePlugin } = await import( + "../../evals/recovery-decisions/capture-plugin.js" +); +const reviewer = { model: "provider/independent-reviewer", steps: 80 }; +const cleanup: unknown = await Reflect.apply(CapturePlugin.setup, undefined, [ + { + location: { directory: workspace }, + options: { captureDirectory, reviewer }, + }, +]); +try { + assert(injected instanceof CapturingRecoveryController); + assert.deepEqual(forwarded, { reviewer }); + console.log( + JSON.stringify({ pluginId: CapturePlugin.id, forwarded, capturing: true }), + ); +} finally { + if (typeof cleanup === "function") + await Reflect.apply(cleanup, undefined, []); +} diff --git a/tests/fixtures/opencode-v2-isolated-candidate.ts b/tests/fixtures/opencode-v2-isolated-candidate.ts new file mode 100644 index 00000000..09017982 --- /dev/null +++ b/tests/fixtures/opencode-v2-isolated-candidate.ts @@ -0,0 +1,147 @@ +import { spawnSync } from "node:child_process"; +import { createHash } from "node:crypto"; +import { + cp, + mkdir, + readdir, + readFile, + rm, + symlink, + writeFile, +} from "node:fs/promises"; +import { join, resolve } from "node:path"; + +export async function isolatedCandidate(root: string) { + const repository = resolve(import.meta.dir, "../.."); + const snapshot = join(root, "source"); + await rm(snapshot, { recursive: true, force: true }); + await mkdir(snapshot, { recursive: true }); + for (const path of [ + "src", + "skills", + "scripts", + "package.json", + "tsconfig.json", + "tsconfig.types.json", + "LICENSE", + "README.md", + "CHANGELOG.md", + ]) + await cp(join(repository, path), join(snapshot, path), { recursive: true }); + await symlink( + join(repository, "node_modules"), + join(snapshot, "node_modules"), + "dir", + ); + const sourceFiles = [ + ...(await readdir(join(snapshot, "src"), { recursive: true })), + ...(await readdir(join(snapshot, "skills"), { recursive: true }).then( + (paths) => paths.map((path) => `../skills/${path}`), + )), + ].sort(); + const hash = createHash("sha256"); + for (const path of sourceFiles) { + try { + hash.update(path); + hash.update(await readFile(join(snapshot, "src", path))); + } catch (error) { + if ( + !(error instanceof Error && "code" in error && error.code === "EISDIR") + ) + throw error; + } + } + const sourceSha256 = `sha256:${hash.digest("hex")}`; + await writeFile(join(root, "npm-userconfig"), ""); + await writeFile(join(root, "npm-globalconfig"), ""); + const environment = { + PATH: process.env.PATH, + HOME: root, + npm_config_userconfig: join(root, "npm-userconfig"), + npm_config_globalconfig: join(root, "npm-globalconfig"), + npm_config_offline: "true", + }; + const run = (command: string, args: string[]) => { + const result = spawnSync(command, args, { + cwd: snapshot, + env: environment, + encoding: "utf8", + }); + if (result.status !== 0) + throw new Error( + `Isolated candidate command failed. ${result.stderr || result.stdout}`, + ); + return result.stdout; + }; + const externals = [ + "@opencode/plugin", + "@opencode/schema", + "@opencode/client", + "effect", + ].map((name) => `--external=${name}`); + run(process.execPath, [ + "build", + "--target=node", + "--outdir=dist", + "--entry-naming=index.js", + "--external=zod", + ...externals, + "--sourcemap=external", + "src/index.ts", + ]); + run(process.execPath, [ + "build", + "--target=bun", + "--outdir=dist", + "--entry-naming=tui.js", + ...externals, + "--sourcemap=external", + "src/tui.ts", + ]); + run(process.execPath, [ + join(repository, "node_modules/typescript/bin/tsc"), + "-p", + "tsconfig.types.json", + ]); + run(process.execPath, ["scripts/prune-dist-declarations.ts"]); + const packed: unknown = JSON.parse( + run("npm", [ + "pack", + "--ignore-scripts", + "--json", + "--pack-destination", + root, + ]), + ); + if (!Array.isArray(packed) || typeof packed[0]?.filename !== "string") + throw new Error("Isolated pack returned no archive"); + const tarball = join(root, packed[0].filename); + const cache = join(root, "cache"); + const modules = join(cache, "node_modules"); + await mkdir(modules, { recursive: true }); + const extracted = join(root, "extracted"); + await mkdir(extracted); + const extraction = spawnSync("tar", ["-xzf", tarball, "-C", extracted], { + encoding: "utf8", + }); + if (extraction.status !== 0) + throw new Error("Isolated candidate extraction failed"); + await cp(join(extracted, "package"), join(modules, "opencode-plugin-flow"), { + recursive: true, + }); + await symlink( + join(repository, "node_modules"), + join(modules, "opencode-plugin-flow", "node_modules"), + "dir", + ); + await writeFile( + join(root, "candidate.json"), + `${JSON.stringify({ tarball, packageCache: cache, sourceSha256, authority: "isolated runtime candidate; parent owns authoritative build" }, null, 2)}\n`, + ); + return { tarball, packageCache: cache, sourceSha256 }; +} +if (import.meta.main) { + const root = process.argv[2]; + if (!root) throw new Error("Expected isolated output directory"); + console.log(JSON.stringify(await isolatedCandidate(resolve(root)))); +} diff --git a/tests/fixtures/opencode-v2-reviewer-return.ts b/tests/fixtures/opencode-v2-reviewer-return.ts new file mode 100644 index 00000000..29f93261 --- /dev/null +++ b/tests/fixtures/opencode-v2-reviewer-return.ts @@ -0,0 +1,249 @@ +import { expect } from "bun:test"; +import { readFile } from "node:fs/promises"; +import { join } from "node:path"; +import type { + OpenCodeClient, + SessionInfo, + SessionMessageAssistantTool, +} from "@opencode/client"; +import { isRecord } from "../../evals/host-observation.js"; +import { listNativeMessages } from "../../evals/opencode-host.js"; +import { + FeatureCompleteInputSchema, + ReviewStartInputSchema, + SessionSchema, +} from "../../src/application/schema.js"; +import { validationCommands } from "./opencode-v2-validation-cases.js"; + +type Dispatch = ( + agent: string, + actions: { tool: string; input: Record }[], +) => Promise<{ session: SessionInfo; calls: SessionMessageAssistantTool[] }>; + +function response(calls: SessionMessageAssistantTool[], name: string) { + const call = calls.findLast((entry) => entry.name === name); + if (call?.state.status !== "completed") + throw new Error(`Native ${name} did not complete`); + const value: unknown = JSON.parse( + call.state.content + .filter((item) => item.type === "text") + .map((item) => item.text) + .join("\n"), + ); + if (!isRecord(value)) throw new Error(`Invalid native ${name} response`); + return value; +} + +export async function runPackedReviewerReturn( + project: string, + client: OpenCodeClient, + rootID: string, + dispatch: Dispatch, + evidence: unknown[], +) { + const session = async () => + SessionSchema.parse( + JSON.parse(await readFile(join(project, ".flow/session.json"), "utf8")), + ); + const manager = async (tool: string, request: Record) => { + const result = response( + (await dispatch("build", [{ tool, input: { request } }])).calls, + tool, + ); + expect(result).toMatchObject({ status: "ok" }); + return result; + }; + await manager("flow_feature_reset", { + operationId: "smoke-review-reset", + expectedRevision: (await session()).revision, + featureId: "native-check", + nextFeatureId: "native-return", + }); + const validated = await dispatch("build", [ + { + tool: "flow_validation_start", + input: { + request: { + expectedRevision: (await session()).revision, + featureId: "native-return", + command: validationCommands.pass, + scope: "broad", + }, + }, + }, + { + tool: "shell", + input: { + command: validationCommands.pass, + description: "Validate reviewer return fixture", + }, + }, + ]); + expect(response(validated.calls, "flow_validation_start")).toMatchObject({ + status: "ok", + }); + evidence.push({ + kind: "reviewer-validation", + session: await session(), + calls: validated.calls, + }); + await manager( + "flow_review_start", + ReviewStartInputSchema.parse({ + request: { + operationId: "smoke-review-start", + expectedRevision: (await session()).revision, + featureId: "native-return", + artifactsChanged: [], + packet: { + summary: + "Review the source-bound native gate and return a terminal result.", + riskLenses: [], + }, + }, + }).request, + ); + const pending = await session(); + const run = pending.runs.at(-1); + const assignment = run?.reviews.at(-1); + if (!run || !assignment) + throw new Error("Native review did not create an assignment"); + const completion = FeatureCompleteInputSchema.parse({ + request: { + operationId: "smoke-review-complete", + expectedRevision: pending.revision, + featureId: "native-return", + assignmentId: assignment.id, + summary: "Local scripted reviewer submitted the native gate result.", + result: { + verdict: "passed", + terminalDisposition: "submitted", + findings: [], + }, + }, + }); + const denied = await dispatch("build", [ + { tool: "flow_feature_complete", input: completion }, + ]); + expect(response(denied.calls, "flow_feature_complete")).toMatchObject({ + status: "error", + }); + expect((await session()).revision).toBe(pending.revision); + const reviewer = await dispatch("flow-reviewer", [ + { + tool: "flow_status", + input: { request: { view: "reviewer", assignmentId: assignment.id } }, + }, + { + tool: "flow_status", + input: { + request: { + view: "reviewer-evidence", + assignmentId: assignment.id, + part: "diff", + page: 0, + }, + }, + }, + { tool: "flow_feature_complete", input: completion }, + ]); + const nativeChild = await client.session.get( + { sessionID: reviewer.session.id }, + { signal: AbortSignal.timeout(30000) }, + ); + expect(nativeChild).toMatchObject({ + parentID: rootID, + agent: "flow-reviewer", + }); + for (const call of reviewer.calls) + expect(call.state.status).toBe("completed"); + const packet = response(reviewer.calls.slice(0, 1), "flow_status"); + const diff = response(reviewer.calls.slice(1, 2), "flow_status"); + expect(packet).toMatchObject({ + status: "ok", + workflowData: { + reviewerPager: { assurance: "host-evidence", diffPages: 1 }, + projection: { + assignment: { + id: assignment.id, + sourceDigest: assignment.sourceDigest, + }, + }, + }, + }); + expect(diff).toMatchObject({ + status: "ok", + workflowData: { + projection: { + assignmentId: assignment.id, + part: "diff", + page: 0, + totalPages: 1, + complete: true, + assurance: "host-evidence", + }, + }, + }); + if ( + !isRecord(diff.workflowData) || + !isRecord(diff.workflowData.projection) || + typeof diff.workflowData.projection.text !== "string" + ) + throw new Error("Missing native reviewer evidence packet"); + expect(JSON.parse(diff.workflowData.projection.text)).toMatchObject({ + version: 1, + sessionId: pending.id, + featureId: "native-return", + runId: run.id, + sourceDigest: assignment.sourceDigest, + complete: true, + }); + expect(response(reviewer.calls, "flow_feature_complete")).toMatchObject({ + status: "ok", + }); + const completed = (await session()).runs.find((entry) => entry.id === run.id); + expect(completed).toMatchObject({ + state: "completed", + reviews: [ + { + id: assignment.id, + result: { + verdict: "passed", + terminalDisposition: "submitted", + findings: [], + }, + }, + ], + }); + const observed = await manager("flow_status", { view: "detail" }); + expect(observed).toMatchObject({ + workflowData: { + projection: { + view: "detail", + activeRunId: null, + progress: { completed: 1 }, + runs: [ + { featureId: "native-check", state: "superseded" }, + { id: run.id, featureId: "native-return", state: "completed" }, + ], + }, + }, + }); + evidence.push({ + kind: "reviewer-return", + rootID, + child: { + id: nativeChild.id, + parentID: nativeChild.parentID, + agent: nativeChild.agent, + }, + assignmentId: assignment.id, + sourceDigest: assignment.sourceDigest, + managerDenied: true, + packet, + diff, + childTranscript: await listNativeMessages(client, nativeChild.id), + completed, + manager: observed, + }); +} diff --git a/tests/fixtures/opencode-v2-scripted-provider.ts b/tests/fixtures/opencode-v2-scripted-provider.ts new file mode 100644 index 00000000..05be649a --- /dev/null +++ b/tests/fixtures/opencode-v2-scripted-provider.ts @@ -0,0 +1,132 @@ +import { isRecord } from "../../evals/host-observation.js"; + +export function localScriptedProvider() { + const requests: { model: unknown; tools: string[]; scenario: string }[] = []; + const server = Bun.serve({ + hostname: "127.0.0.1", + port: 0, + async fetch(request) { + const body: unknown = await request.json(); + if (!isRecord(body) || !Array.isArray(body.messages)) + return new Response("Invalid local request", { status: 400 }); + const messages = body.messages.filter(isRecord); + const latestUser = messages.findLast( + (message) => message.role === "user", + ); + const text = + typeof latestUser?.content === "string" + ? latestUser.content + : JSON.stringify(latestUser?.content); + const tools = Array.isArray(body.tools) + ? body.tools + .filter(isRecord) + .flatMap((tool) => + isRecord(tool.function) && typeof tool.function.name === "string" + ? [tool.function.name] + : [], + ) + : []; + requests.push({ + model: body.model, + tools, + scenario: text?.includes("FLOW_SMOKE_PROVIDER_ERROR") + ? "error" + : text?.includes("FLOW_SMOKE_DELAY") + ? "delay" + : text?.includes("FLOW_SMOKE_ACTIONS") + ? "actions" + : "plain", + }); + if (text?.includes("FLOW_SMOKE_PROVIDER_ERROR")) + return Response.json( + { error: { message: "Local scripted provider error" } }, + { status: 400 }, + ); + const actions: unknown = JSON.parse( + text?.match(/FLOW_SMOKE_ACTIONS (\[.*\])/s)?.[1] ?? "[]", + ); + if ( + !Array.isArray(actions) || + actions.some( + (action) => + !isRecord(action) || + typeof action.tool !== "string" || + !isRecord(action.input), + ) + ) + return new Response("Invalid local actions", { status: 400 }); + const userIndex = messages.findLastIndex( + (message) => message.role === "user", + ); + const consumed = messages + .slice(userIndex + 1) + .reduce( + (total, message) => + total + + (Array.isArray(message.tool_calls) ? message.tool_calls.length : 0), + 0, + ); + const next = actions.filter(isRecord)[consumed]; + const calls = next + ? [ + { + index: 0, + id: `call_smoke_${consumed}`, + type: "function", + function: { + name: next.tool, + arguments: JSON.stringify(next.input), + }, + }, + ] + : []; + const base = { + id: "chatcmpl-local-smoke", + object: "chat.completion.chunk", + created: 1, + model: "wire-model", + }; + const encoder = new TextEncoder(); + const chunk = (delta: unknown, finish_reason: string | null = null) => + encoder.encode( + `data: ${JSON.stringify({ ...base, choices: [{ index: 0, delta, finish_reason }] })}\n\n`, + ); + let cancelled = false; + return new Response( + new ReadableStream({ + async start(controller) { + controller.enqueue( + chunk({ + role: "assistant", + content: text?.includes("FLOW_SMOKE_DELAY") + ? "Local request started " + : "", + }), + ); + if (text?.includes("FLOW_SMOKE_DELAY")) await Bun.sleep(1000); + if (cancelled) return; + controller.enqueue( + chunk( + calls.length + ? { tool_calls: calls } + : { content: "Local smoke complete" }, + ), + ); + controller.enqueue(chunk({}, calls.length ? "tool_calls" : "stop")); + controller.enqueue( + encoder.encode( + `data: ${JSON.stringify({ ...base, choices: [], usage: { prompt_tokens: 13, completion_tokens: 9, total_tokens: 22, prompt_tokens_details: { cached_tokens: 3 }, completion_tokens_details: { reasoning_tokens: 4 } } })}\n\ndata: [DONE]\n\n`, + ), + ); + controller.close(); + }, + cancel() { + cancelled = true; + }, + }), + { headers: { "content-type": "text/event-stream" } }, + ); + }, + }); + return { server, requests, baseURL: new URL("v1", server.url).href }; +} diff --git a/tests/fixtures/opencode-v2-smoke-plugin.ts b/tests/fixtures/opencode-v2-smoke-plugin.ts new file mode 100644 index 00000000..7ff2b8d4 --- /dev/null +++ b/tests/fixtures/opencode-v2-smoke-plugin.ts @@ -0,0 +1,168 @@ +import { appendFile } from "node:fs/promises"; +import { Model, Plugin, Provider } from "@opencode/plugin"; +import { z } from "zod"; +import { isRecord } from "../../evals/host-observation.js"; + +const Options = z + .object({ baseURL: z.string().url(), receipt: z.string().min(1) }) + .strict(); + +export default Plugin.define({ + id: "flow-v2-local-smoke", + async setup(context) { + const options = Options.parse(context.options); + const url = new URL(options.baseURL); + if ( + url.protocol !== "http:" || + url.hostname !== "127.0.0.1" || + !url.port || + url.username || + url.password + ) + throw new Error("Smoke provider must be loopback HTTP."); + const providerID = Provider.ID.make("flow-smoke"); + const id = Model.ID.make("catalog-model"); + const model = Model.Info.default(providerID, id); + await context.provider.transform((editor) => + editor.add({ + info: { + ...Provider.Info.empty(providerID), + name: "Local scripted smoke", + activation: "enabled", + package: "@opencode/ai/providers/openai-compatible", + settings: { baseURL: options.baseURL }, + }, + models: [ + { + ...model, + modelID: Model.ID.make("wire-model"), + capabilities: { ...model.capabilities, tools: true }, + }, + ], + }), + ); + await context.session.hook("title", (input) => { + input.result = "Flow smoke"; + }); + await context.session.hook("compaction", (input) => { + input.result = { summary: "Flow smoke compacted summary" }; + }); + await context.session.hook("http.request", (input) => { + const requestURL = new URL(input.request.url); + if (requestURL.origin !== url.origin) + throw new Error("Smoke forbids nonloopback or foreign model requests."); + }); + let sequence = 0; + const record = (value: object) => + appendFile( + options.receipt, + `${JSON.stringify({ sequence: sequence++, time: Date.now(), ...value })}\n`, + ); + const controller = new AbortController(); + const events = (async () => { + try { + for await (const event of context.event.subscribe({ + signal: controller.signal, + })) { + if ( + event.type === "shell.created" || + event.type === "shell.exited" || + event.type === "session.execution.failed" || + event.type === "session.step.failed" + ) + await record({ + kind: "shell-event", + type: event.type, + data: event.data, + }); + } + } catch { + if (!controller.signal.aborted) + await record({ kind: "event-stream-failure" }); + } + })(); + await context.tool.hook("execute.before", async (input) => { + if (input.tool === "shell") + await record({ + kind: "shell-before", + sessionID: input.sessionID, + messageID: input.messageID, + callID: input.id, + input: input.input, + }); + }); + + await context.tool.hook("execute.after", async (input) => { + const output = + input.status === "completed" && isRecord(input.result.output) + ? input.result.output + : undefined; + let application: unknown; + if (input.tool.startsWith("flow_") && input.status === "completed") { + const content = input.result.content; + const text = + typeof content === "string" + ? content + : (content ?? []) + .filter((item) => item.type === "text") + .map((item) => item.text) + .join("\n"); + try { + const parsed: unknown = JSON.parse(text); + if (isRecord(parsed)) + application = { status: parsed.status, summary: parsed.summary }; + } catch {} + } + + await record({ + kind: "tool", + application, + tool: input.tool, + sessionID: input.sessionID, + messageID: input.messageID, + callID: input.id, + agent: input.agent, + status: input.status, + ...(input.tool === "shell" && output + ? { + output: { + exit: output.exit, + truncated: output.truncated, + status: output.status, + timeout: output.timeout, + footer: + typeof output.output === "string" + ? output.output.slice(-240) + : undefined, + }, + } + : {}), + }); + }); + + await context.command.transform((editor) => + editor.add({ + name: "flow-smoke-inventory", + description: "Record local smoke tool IDs", + execute: async () => { + await appendFile( + options.receipt, + `${JSON.stringify({ kind: "inventory", tools: (await context.tool.list()).map((tool) => tool.id).sort() })}\n`, + ); + }, + }), + ); + await appendFile( + options.receipt, + `${JSON.stringify({ kind: "setup", version: context.app.version, directory: context.location.directory })}\n`, + ); + return async () => { + controller.abort(); + await events; + await appendFile( + options.receipt, + `${JSON.stringify({ kind: "cleanup" })}\n`, + ); + }; + }, +}); diff --git a/tests/fixtures/opencode-v2-test-host.ts b/tests/fixtures/opencode-v2-test-host.ts new file mode 100644 index 00000000..92bb77e0 --- /dev/null +++ b/tests/fixtures/opencode-v2-test-host.ts @@ -0,0 +1,32 @@ +import { OpenCode } from "@opencode/client"; +import { + type CredentialSync, + type EvalHost, + syncProviderCredentialsBack, +} from "../../evals/harness.js"; + +export function nativeTestClient() { + return OpenCode.make({ + baseUrl: "http://fixture", + fetch: Object.assign( + (...args: Parameters) => globalThis.fetch(...args), + { preconnect: fetch.preconnect }, + ), + }); +} + +export function fileCredentialHandoff( + host: EvalHost, + paths: CredentialSync, +): void { + Object.assign(host, { + nativeClient: { + session: { active: async () => ({}) }, + credential: { list: async () => [] }, + }, + credentialHandoff: { + before: [], + onStop: () => syncProviderCredentialsBack(paths), + }, + }); +} diff --git a/tests/fixtures/opencode-v2-usage-proof.ts b/tests/fixtures/opencode-v2-usage-proof.ts new file mode 100644 index 00000000..0325b249 --- /dev/null +++ b/tests/fixtures/opencode-v2-usage-proof.ts @@ -0,0 +1,48 @@ +import { expect } from "bun:test"; +import type { OpenCodeClient } from "@opencode/client"; +import { EvalHost } from "../../evals/harness.js"; +import { listNativeMessages } from "../../evals/opencode-host.js"; +import { + generatedOutputTokens, + normalizeStudyUsage, +} from "../../evals/study-usage.js"; + +export async function proveNativeUsage( + client: OpenCodeClient, + sessionID: string, + project: string, +) { + const native = (await listNativeMessages(client, sessionID)).find( + (message) => message.type === "assistant", + ); + expect(native?.tokens).toEqual({ + input: 10, + output: 5, + reasoning: 4, + cache: { read: 3, write: 0 }, + }); + const reader: unknown = Reflect.construct(EvalHost, [project, project]); + if (!(reader instanceof EvalHost)) + throw new Error("Could not create native usage reader"); + Reflect.set(reader, "nativeClient", client); + const observed = await reader.outcome([sessionID], 1); + const usage = normalizeStudyUsage({ + tokens: observed.tokens, + costUsd: observed.costUsd, + ...(observed.usageAvailability + ? { availability: observed.usageAvailability } + : {}), + }); + expect(usage.tokens).toEqual({ + input: 10, + output: 5, + reasoning: 4, + cacheRead: 3, + cacheWrite: 0, + }); + expect( + Object.values(usage.tokens).reduce((total, count) => total + count, 0), + ).toBe(22); + expect(generatedOutputTokens(usage)).toBe(9); + return { nativeTokens: native?.tokens, usage }; +} diff --git a/tests/fixtures/opencode-v2-validation-cases.ts b/tests/fixtures/opencode-v2-validation-cases.ts new file mode 100644 index 00000000..c99b0df4 --- /dev/null +++ b/tests/fixtures/opencode-v2-validation-cases.ts @@ -0,0 +1,216 @@ +import { expect } from "bun:test"; +import { createHash } from "node:crypto"; +import { readFile, writeFile } from "node:fs/promises"; +import { join } from "node:path"; +import type { SessionMessageAssistantTool } from "@opencode/client"; +import { SessionSchema } from "../../src/application/schema.js"; +import { normalizeEvidencePlatform } from "../../src/domain/validation.js"; +import { createFileSourceIdentityProvider } from "../../src/infrastructure/fs/source-identity.js"; + +const platform = normalizeEvidencePlatform(process.platform); + +export const validationCommands = { + pass: "printf flow-smoke-pass", + nonzero: "printf flow-smoke-nonzero; exit 7", + timeout: "/bin/sleep 2", + error: "printf flow-smoke-error", + large: + "/usr/bin/awk 'BEGIN { for (i=0;i<20000;i++) print \"native-smoke-output\" }'", + junit: "./native-junit.sh --reporter-outfile=.flow/results.xml", + stale: "./native-junit-stale.sh --reporter-outfile=.flow/results.xml", +} as const; +const named = "smoke named assertion"; +export const packedValidationPlan = { + summary: "Verify native capture against the installed candidate", + overview: "Only the disposable fixture is used for native shell evidence.", + requirements: ["Capture exact source-bound native validation"], + decisions: ["No remote provider"], + features: [ + { + id: "native-check", + title: "Native capture", + summary: "Exercise native shell outcomes", + targets: ["README.md"], + validation: Object.values(validationCommands), + dependsOn: [], + checks: Object.entries(validationCommands).map(([key, command]) => ({ + command, + intent: "pass", + platform, + ...(["junit", "stale"].includes(key) ? { assertions: [named] } : {}), + })), + }, + { + id: "native-return", + title: "Native reviewer return", + summary: "Complete a fresh independently reviewed native gate", + targets: ["README.md"], + validation: [validationCommands.pass], + dependsOn: [], + checks: [{ command: validationCommands.pass, intent: "pass", platform }], + }, + ], + evidence: [ + { + scope: "gate", + requirement: "Local gate", + environment: "Isolated host", + command: validationCommands.pass, + platform, + assertions: [], + }, + ], +}; + +type Dispatch = ( + agent: string, + actions: { tool: string; input: Record }[], +) => Promise<{ calls: SessionMessageAssistantTool[] }>; +export async function runPackedValidationCases( + project: string, + dispatch: Dispatch, + evidence: unknown[], +) { + const session = async () => + SessionSchema.parse( + JSON.parse(await readFile(join(project, ".flow/session.json"), "utf8")), + ); + const flow = async (tool: string, request: Record) => { + const result = await dispatch("build", [{ tool, input: { request } }]); + const call = result.calls.findLast((call) => call.name === tool); + if (call?.state.status !== "completed") + throw new Error(`Native ${tool} did not complete`); + const content = call.state.content + .filter((item) => item.type === "text") + .map((item) => item.text) + .join("\n"); + const response: unknown = JSON.parse(content); + expect(response).toMatchObject({ status: "ok" }); + return call; + }; + await flow("flow_plan_approve", { + operationId: "smoke-approve", + expectedRevision: (await session()).revision, + }); + await flow("flow_run_start", { + operationId: "smoke-run", + expectedRevision: (await session()).revision, + featureId: "native-check", + }); + const sourceDigest = + await createFileSourceIdentityProvider(project).computeSourceDigest(); + const failures: Error[] = []; + for (const [kind, command] of Object.entries(validationCommands)) { + const before = await session(); + if (kind === "stale") + await writeFile( + join(project, ".flow/results.xml"), + '', + ); + const result = await dispatch("build", [ + { + tool: "flow_validation_start", + input: { + request: { + expectedRevision: before.revision, + featureId: "native-check", + command, + scope: kind === "pass" ? "broad" : "focused", + ...(["junit", "stale"].includes(kind) + ? { resultsPath: ".flow/results.xml" } + : {}), + }, + }, + }, + { + tool: "shell", + input: { + command, + description: `Native ${kind} validation`, + ...(kind === "timeout" ? { timeout: 10 } : {}), + ...(kind === "error" + ? { workdir: join(project, "absent-workdir") } + : {}), + }, + }, + ]); + const armed = result.calls.find( + (call) => call.name === "flow_validation_start", + ); + if (armed?.state.status !== "completed") + throw new Error("Native validation arming did not complete"); + expect( + JSON.parse( + armed.state.content + .filter((item) => item.type === "text") + .map((item) => item.text) + .join("\n"), + ), + ).toMatchObject({ status: "ok" }); + const after = await session(); + const observations = after.runs.at(-1)?.validations ?? []; + const record = observations.at(-1); + evidence.push({ + kind, + command, + sourceDigest, + beforeRevision: before.revision, + afterRevision: after.revision, + record, + calls: result.calls.map((call) => ({ + name: call.name, + state: call.state.status, + time: call.time, + ...(call.state.status === "streaming" + ? {} + : { metadata: call.state.metadata }), + })), + }); + try { + if (kind === "error") { + expect( + result.calls + .filter((call) => call.name === "shell") + .map((call) => [call.name, call.state.status]), + ).toEqual([["shell", "error"]]); + expect(observations.length).toBe( + before.runs.at(-1)?.validations.length ?? 0, + ); + continue; + } + expect(record?.command).toBe(command); + expect(record?.sourceDigest).toBe(sourceDigest); + expect(record?.runId).toBe(after.runs.at(-1)?.id); + if (kind === "timeout") { + expect(record?.exitCode).toBeNull(); + expect(record?.outputComplete).toBe(false); + expect(record?.ineligibleReason).toBe("exit-code-unavailable"); + } else { + expect(record?.exitCode).toBe(kind === "nonzero" ? 7 : 0); + expect(record?.outputComplete).toBe(true); + expect(record?.ineligibleReason).toBeUndefined(); + } + if (kind === "large") + expect(record?.outputDigest).toBe( + `sha256:${createHash("sha256").update("native-smoke-output\n".repeat(20000)).digest("hex")}`, + ); + if (kind === "junit") + expect(record?.observedAssertions).toEqual([ + { name: named, status: "passed" }, + ]); + if (kind === "stale") + expect(record?.observedAssertions).toEqual([ + { name: named, status: "absent" }, + ]); + } catch (error) { + failures.push(error instanceof Error ? error : new Error(String(error))); + } + } + return { sourceDigest, failures }; +} + +export const junitFixtureFiles = { + "native-junit.sh": + '#!/bin/sh\nsleep 0.05\nprintf \'%s\' \'\' > .flow/results.xml\n', + "native-junit-stale.sh": "#!/bin/sh\ncat .flow/results.xml > /dev/null\n", +}; diff --git a/tests/fixtures/verified-continuation-origin.ts b/tests/fixtures/verified-continuation-origin.ts new file mode 100644 index 00000000..154ddd02 --- /dev/null +++ b/tests/fixtures/verified-continuation-origin.ts @@ -0,0 +1,145 @@ +import type { SessionInfo, SessionMessageInfo } from "@opencode/client"; +import { Agent, Model, Provider } from "@opencode/plugin"; +import type { SessionContext } from "@opencode/plugin/promise/session"; +import { CallID } from "@opencode/plugin/promise/tool"; +import { Session } from "@opencode/schema/session"; +import { SessionMessage } from "@opencode/schema/session-message"; +import { + type AutoDriveCoordinator, + type AutoDriveDelivery, + FLOW_AUTO_METADATA_KEY, +} from "../../src/platform/opencode/auto-drive.js"; +import { ExecutionAttribution } from "../../src/platform/opencode/execution-attribution.js"; + +import { InputAdmissions } from "../../src/platform/opencode/input-admission.js"; +export async function verifiedContinuationOrigin( + auto: AutoDriveCoordinator, + host: string, + inputID: string, + assistantID: string, + delivery: AutoDriveDelivery = { + agent: "build", + model: { providerID: "test", modelID: "manager" }, + }, +): Promise { + const sessionID = Session.ID.make(host); + const agent = Agent.ID.make(delivery.agent); + const model = { + providerID: Provider.ID.make(delivery.model.providerID), + id: Model.ID.make(delivery.model.modelID), + ...(delivery.variant === undefined + ? {} + : { variant: Model.VariantID.make(delivery.variant) }), + }; + const messageID = SessionMessage.ID.make(assistantID); + const id = CallID.make("call_fixture"); + const authority = auto.captureAuthority(host); + const payload = { + text: "Source-bound fixture request", + ...(authority + ? { metadata: { [FLOW_AUTO_METADATA_KEY]: authority.token } } + : {}), + }; + const inputs = new InputAdmissions({ + assertOpen: () => {}, + authorityCurrent: (candidate) => auto.isAuthorityCurrent(candidate), + authority: () => auto.captureAuthority(host), + observeReal: async () => {}, + }); + const reservation = inputs.reserve(host, inputID); + const admission = inputs.register(reservation, { type: "user", payload }); + await inputs.accept( + admission, + { + sessionID, + id: inputID, + type: "user", + time: { created: 0 }, + delivery: "queue", + payload, + }, + async () => + inputs.makeOrigin( + host, + inputID, + delivery, + authority !== null, + reservation.epoch, + ), + ); + const user: SessionMessageInfo = { + id: inputID, + type: "user", + ...payload, + time: { created: 0 }, + }; + const attribution = new ExecutionAttribution((origin) => + inputs.assertLive(origin), + ); + const context: SessionContext = { + sessionID, + agent, + model, + system: [], + options: {}, + tools: {}, + messages: [ + { + id: inputID, + role: "user", + content: [{ type: "text", text: "Source-bound fixture request" }], + }, + ], + }; + const scope = inputs.capture(context); + await scope.ready(); + const evidence = await inputs.evidence(scope, [user], delivery); + if (evidence.kind !== "live") + throw new Error("Missing accepted fixture origin."); + attribution.freeze(context, [user], evidence.origin); + attribution.request({ + sessionID, + agent, + model, + kind: "primary", + request: new Request("http://localhost/fixture"), + }); + const assistant: SessionMessageInfo = { + id: messageID, + type: "assistant", + agent, + model, + time: { created: 1 }, + content: [ + { + type: "tool", + id, + name: "flow_status", + state: { status: "running", input: {}, metadata: {} }, + time: { created: 1 }, + }, + ], + }; + const session: SessionInfo = { + id: sessionID, + agent, + model, + projectID: "fixture", + location: { directory: process.cwd() }, + time: { created: 0, updated: 0 }, + cost: 0, + tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } }, + }; + const verified = attribution.verify( + { sessionID, agent, messageID, id }, + [user, assistant], + session, + process.cwd(), + "flow_status", + ); + auto.observeAssistant(host, assistantID, verified.primary.origin.id); + scope.release(); + inputs.clear(); + attribution.clear(); + return verified.primary.origin.id; +} diff --git a/tests/host-artifacts-host.test.ts b/tests/host-artifacts-host.test.ts index d4b8d298..bb976baf 100644 --- a/tests/host-artifacts-host.test.ts +++ b/tests/host-artifacts-host.test.ts @@ -50,7 +50,6 @@ smoke( providerCredentials: "disabled", ambientConfig: "disabled", reviewerEnvironment: "disabled", - nativeLlm: false, signal: AbortSignal.timeout(120000), }); expect(host.artifactIdentity).toEqual(identity); diff --git a/tests/input-admission.test.ts b/tests/input-admission.test.ts new file mode 100644 index 00000000..8ea02935 --- /dev/null +++ b/tests/input-admission.test.ts @@ -0,0 +1,432 @@ +import { expect, test } from "bun:test"; +import { Agent, Model, Provider } from "@opencode/plugin"; +import type { SessionContext } from "@opencode/plugin/promise/session"; +import { Session } from "@opencode/schema/session"; +import { InputAdmissions } from "../src/platform/opencode/input-admission.js"; + +const host = Session.ID.make("session-ledger"); +const agent = Agent.ID.make("build"); +const model = { + providerID: Provider.ID.make("local"), + id: Model.ID.make("scripted"), +}; +const delivery = { + agent, + model: { providerID: model.providerID, modelID: model.id }, +}; +const context = ( + id: string, +): Pick => ({ + sessionID: host, + messages: [ + { role: "user", id, content: [{ type: "text", text: "Approved task" }] }, + ], +}); +function fixture() { + let open = true; + const inputs = new InputAdmissions({ + assertOpen: () => { + if (!open) throw Error("closed"); + }, + authorityCurrent: () => true, + authority: () => null, + observeReal: async () => {}, + }); + const reservation = inputs.reserve(host, "user-1"); + const admission = inputs.register(reservation, { + type: "user", + payload: { text: "Approved task" }, + }); + const receipt = { + id: "user-1", + sessionID: host, + type: "user" as const, + payload: { text: "Approved task" }, + time: { created: 0 }, + delivery: "queue" as const, + }; + return { + inputs, + reservation, + admission, + receipt, + close() { + open = false; + inputs.clear(); + }, + origin: () => + inputs.makeOrigin(host, "user-1", delivery, false, reservation.epoch), + }; +} +test("matching receipt wakes only the exact admission scope", async () => { + const f = fixture(); + const scope = f.inputs.capture(context("user-1")); + const ready = scope.ready(); + expect(f.inputs.positive(scope)).toBeUndefined(); + await f.inputs.accept(f.admission, f.receipt, async () => f.origin()); + await ready; + expect(f.inputs.positive(scope)?.id).toBe("user-1"); + scope.release(); + f.close(); +}); +test("conflicting exact receipt payload rejects admission without materializing an origin", async () => { + const f = fixture(); + let activated = false; + await expect( + f.inputs.accept( + f.admission, + { ...f.receipt, payload: { text: "Different" } }, + async () => { + activated = true; + return f.origin(); + }, + ), + ).rejects.toThrow("exact local payload"); + expect(activated).toBe(false); + await expect(f.admission.ready).rejects.toThrow("exact local payload"); + f.close(); +}); +test("a superseded reservation cannot accept a receipt under the new lifetime", async () => { + const f = fixture(); + f.inputs.invalidate(host); + await expect( + f.inputs.accept(f.admission, f.receipt, async () => f.origin()), + ).rejects.toThrow("epoch was revoked"); + await expect(f.admission.ready).rejects.toThrow("epoch was revoked"); + f.close(); +}); +for (const invalidation of ["lifetime", "frontier", "unload"] as const) + test(`held readiness scope wakes on ${invalidation} without settling admission`, async () => { + const f = fixture(); + const scope = f.inputs.capture(context("user-1")); + let settled = false; + void f.admission.ready.then( + () => { + settled = true; + }, + () => { + settled = true; + }, + ); + const wait = scope.ready(); + const caught = wait.catch((error) => String(error)); + if (invalidation === "lifetime") f.inputs.invalidate(host); + else if (invalidation === "frontier") f.inputs.reserve(host, "fresh"); + else f.close(); + expect(await caught).toBe("Error: Flow execution epoch was revoked."); + await Promise.resolve(); + expect(settled).toBe(false); + scope.release(); + f.close(); + }); +test("pre-registration reservation blocks old consumed lineage in the same lifetime", () => { + const f = fixture(); + f.inputs.reserve(host, "fresh-before-get"); + expect(() => f.inputs.capture(context("user-1"))).toThrow( + "epoch was revoked", + ); + expect(() => + f.inputs.register(f.reservation, { + type: "synthetic", + payload: { text: "Old" }, + }), + ).toThrow("epoch was revoked"); + f.close(); +}); +test("a new frontier cancels old scope without rejecting a fresh exact ready", async () => { + const f = fixture(); + const old = f.inputs.capture(context("user-1")); + const oldWait = old.ready().catch((error) => String(error)); + const reservation = f.inputs.reserve(host, "fresh"); + const admission = f.inputs.register(reservation, { + type: "synthetic", + payload: { text: "Fresh" }, + }); + const fresh = f.inputs.capture(context("fresh")); + let settled = false; + const ready = fresh.ready().then(() => { + settled = true; + }); + expect(await oldWait).toBe("Error: Flow execution epoch was revoked."); + old.release(); + await Promise.resolve(); + expect(settled).toBe(false); + await f.inputs.accept( + admission, + { + ...f.receipt, + id: "fresh", + type: "synthetic", + payload: { text: "Fresh" }, + }, + async () => + f.inputs.makeOrigin(host, "fresh", delivery, false, reservation.epoch), + ); + await ready; + expect(f.inputs.positive(fresh)?.id).toBe("fresh"); + fresh.release(); + f.close(); +}); + +import type { SessionMessageInfo } from "@opencode/client"; +import { CompactionBoundary } from "../src/platform/opencode/auto-compaction.js"; + +async function compactionFixture() { + const f = fixture(); + await f.inputs.accept(f.admission, f.receipt, async () => f.origin()); + const native: SessionMessageInfo[] = [ + { type: "user", id: "user-1", text: "Approved task", time: { created: 0 } }, + { + type: "compaction", + id: "msg_control", + status: "running", + reason: "auto", + summary: "summary", + time: { created: 1 }, + }, + ]; + const input = { + ...context("user-1"), + agent, + model, + system: [], + options: {}, + tools: {}, + }; + const boundary = new CompactionBoundary({ + inputs: f.inputs, + readNative: async () => structuredClone(native), + failure: () => {}, + hasActivePrimary: () => false, + guidance: () => null, + observe: () => {}, + }); + return { ...f, native, input, boundary }; +} +test("duplicate checked control admission returns the same record and checkpoint origin", async () => { + const f = await compactionFixture(); + try { + const first = await f.boundary.admit(f.input); + const second = await f.boundary.admit(f.input); + expect(second).toBe(first); + expect(first.kind).toBe("continuation"); + const control = f.native[1]; + if (control?.type !== "compaction") throw Error("missing control"); + f.native.splice(0, f.native.length, { + ...control, + status: "completed", + summary: "summary", + }); + const input = { ...f.input, ...context("msg_control") }; + const origin = f.boundary.completed(input, f.native); + expect(origin).toBe(f.boundary.completed(input, f.native)); + if (first.kind !== "continuation") throw Error("missing continuation"); + expect(origin).toBe(first.origin); + } finally { + f.close(); + } +}); +for (const corruption of [ + "reason", + "prefix", + "delivery", + "replaced-during-wait", +] as const) + test(`compaction boundary rejects exact ${corruption} mismatch`, async () => { + const f = await compactionFixture(); + try { + if (corruption === "reason") { + const control = f.native[1]; + Object.defineProperty(control, "reason", { value: "future-reason" }); + } + if (corruption === "prefix") + f.native.unshift({ + type: "user", + id: "foreign", + text: "Foreign", + time: { created: 0 }, + }); + if (corruption === "delivery") + f.input.model = { ...model, id: Model.ID.make("different") }; + if (corruption === "replaced-during-wait") { + const first = Promise.withResolvers(), + release = Promise.withResolvers(); + let read = 0; + const boundary = new CompactionBoundary({ + inputs: f.inputs, + readNative: async () => { + const snapshot = structuredClone(f.native); + if (read++ === 0) { + first.resolve(); + await release.promise; + } + return snapshot; + }, + failure: () => {}, + hasActivePrimary: () => false, + guidance: () => null, + observe: () => {}, + }); + const pending = boundary.admit(f.input); + await first.promise; + const control = f.native[1]; + if (control?.type !== "compaction") throw Error("missing control"); + control.id = "msg_replacement"; + release.resolve(); + await expect(pending).rejects.toThrow("control identity changed"); + } else await expect(f.boundary.admit(f.input)).rejects.toThrow(); + } finally { + f.close(); + } + }); + +test("manual summary of unowned trailing history retains the ordinary local frontier lineage", async () => { + const f = await compactionFixture(); + try { + f.inputs.invalidate(host); + const running = f.native[1]; + if (running?.type !== "compaction") throw Error("missing control"); + running.reason = "manual"; + f.native.splice(1, 0, { + type: "synthetic", + id: "msg_unowned", + text: "Unowned native history", + time: { created: 1 }, + }); + f.input.messages.push({ role: "user", id: "msg_unowned", content: [] }); + const first = await f.boundary.admit(f.input); + expect(first.kind).toBe("summary-only"); + expect("origin" in first).toBe(false); + const control = f.native[2]; + if (control?.type !== "compaction") throw Error("missing control"); + f.native.splice( + 0, + f.native.length, + { ...control, status: "completed", summary: "summary" }, + { + type: "compaction", + id: "msg_second_control", + status: "running", + reason: "manual", + summary: "second summary", + time: { created: 2 }, + }, + ); + const completedInput = { ...f.input, ...context("msg_control") }; + expect(() => f.boundary.completed(completedInput, f.native)).toThrow( + "unknown, synthetic, or stale real input", + ); + const scope = f.inputs.capture(completedInput); + expect(f.inputs.positive(scope)).toBeUndefined(); + expect(await f.inputs.evidence(scope, f.native, delivery)).toEqual({ + kind: "retired-ordinary", + }); + scope.release(); + const second = await f.boundary.admit({ + ...f.input, + ...context("msg_control"), + }); + expect(second.kind).toBe("summary-only"); + expect("origin" in second).toBe(false); + } finally { + f.close(); + } +}); +for (const aliasDepth of [0, 2]) + test(`unowned trailing history cannot erase automatic history after ${aliasDepth} checked aliases`, async () => { + const authority = { hostSessionId: host, token: "token", epoch: 1 }; + let live = true; + const inputs = new InputAdmissions({ + assertOpen: () => {}, + authorityCurrent: () => live, + authority: () => (live ? authority : null), + observeReal: async () => {}, + }); + const reservation = inputs.reserve(host, "user-1"); + const admission = inputs.register(reservation, { + type: "user", + payload: { text: "Approved task" }, + }); + await inputs.accept( + admission, + { + sessionID: host, + id: "user-1", + type: "user", + payload: { text: "Approved task" }, + time: { created: 0 }, + delivery: "queue", + }, + async () => + inputs.makeOrigin(host, "user-1", delivery, false, reservation.epoch), + ); + const native: SessionMessageInfo[] = [ + { + type: "user", + id: "user-1", + text: "Approved task", + time: { created: 0 }, + }, + ]; + const input = { + ...context("user-1"), + agent, + model, + system: [], + options: {}, + tools: {}, + }; + const boundary = new CompactionBoundary({ + inputs, + readNative: async () => native, + failure: () => {}, + hasActivePrimary: () => false, + guidance: () => null, + observe: () => {}, + }); + try { + for (let index = 0; index < aliasDepth; index++) { + const control = { + type: "compaction" as const, + id: `msg_alias_${index}`, + status: "running" as const, + reason: "auto" as const, + summary: "Automatic summary", + time: { created: index + 1 }, + }; + native.push(control); + expect((await boundary.admit(input)).kind).toBe("continuation"); + native.splice(0, native.length, { ...control, status: "completed" }); + input.messages = [{ role: "user", id: control.id, content: [] }]; + } + live = false; + inputs.invalidate(host); + native.push( + { + type: "synthetic", + id: "msg_unowned", + text: "Unowned native history", + time: { created: 3 }, + }, + { + type: "compaction", + id: "msg_control", + status: "running", + reason: "manual", + summary: "summary", + time: { created: 4 }, + }, + ); + input.messages.push({ role: "user", id: "msg_unowned", content: [] }); + await expect(boundary.admit(input)).rejects.toThrow("epoch was revoked"); + await expect(boundary.admit(input)).rejects.toThrow("epoch was revoked"); + const scope = inputs.capture(input); + expect(inputs.positive(scope)).toBeUndefined(); + expect(await inputs.evidence(scope, native, delivery)).toEqual({ + kind: "retired-automatic", + }); + scope.release(); + } finally { + inputs.clear(); + } + }); diff --git a/tests/lifecycle.test.ts b/tests/lifecycle.test.ts new file mode 100644 index 00000000..78902e22 --- /dev/null +++ b/tests/lifecycle.test.ts @@ -0,0 +1,3580 @@ +import { expect, spyOn, test } from "bun:test"; +import type { + OpenCodeEvent, + SessionInboxUser, + SessionInfo, + SessionMessageInfo, +} from "@opencode/client"; +import { Agent, Model, Provider } from "@opencode/plugin"; +import type { CommandDefinition } from "@opencode/plugin/promise/command"; +import type { Context } from "@opencode/plugin/promise/plugin"; +import type { + SessionContext, + SessionHooks, +} from "@opencode/plugin/promise/session"; +import { CallID } from "@opencode/plugin/promise/tool"; +import { AbsolutePath } from "@opencode/schema/schema"; +import { Session } from "@opencode/schema/session"; +import { SessionMessage } from "@opencode/schema/session-message"; +import { createFlowService } from "../src/application/flow-service.js"; +import { RecoveryController } from "../src/application/recovery-policy.js"; +import type { FlowCodingModel } from "../src/config-shared.js"; +import { + AutoDriveCoordinator, + type AutoDriveProjection, + FLOW_AUTO_METADATA_KEY, +} from "../src/platform/opencode/auto-drive.js"; +import { ExecutionAttribution } from "../src/platform/opencode/execution-attribution.js"; +import { registerFlowLifecycle } from "../src/platform/opencode/lifecycle.js"; +import { + deterministicEnvironment, + MemorySessionRepository, +} from "./runtime-test-support.js"; + +type Callback = (input: T) => Promise | void; +type HookSets = { [Name in keyof Spec]: Set> }; +const sessionID = Session.ID.make("session-lifecycle"); +const agent = Agent.ID.make("build"); +const model = { + providerID: Provider.ID.make("local"), + id: Model.ID.make("scripted"), +}; +const directory = AbsolutePath.make(process.cwd()); +const delivery = { + agent, + model: { providerID: model.providerID, modelID: model.id }, +}; +function hookSets(): HookSets { + return { + prompt: new Set(), + context: new Set(), + compaction: new Set(), + generate: new Set(), + title: new Set(), + "model.request": new Set(), + "http.request": new Set(), + "http.response": new Set(), + "experimental.ws.handshake": new Set(), + "experimental.ws.send": new Set(), + "experimental.ws.receive": new Set(), + retry: new Set(), + }; +} +async function fixture(selectedAgent = agent) { + const hooks = hookSets(); + const commands = new Map(); + const registrations: string[] = []; + const native: SessionMessageInfo[] = []; + const children: SessionInfo[] = []; + const session: SessionInfo = { + id: sessionID, + agent: selectedAgent, + model, + projectID: "project", + location: { directory }, + time: { created: 0, updated: 0 }, + cost: 0, + tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } }, + }; + let projection: AutoDriveProjection = { + sessionId: "flow-1", + revision: 4, + status: "planning", + nextAction: "flow_plan_approve", + }; + const logs: string[] = []; + const codingModels = new Map(); + const repository = new MemorySessionRepository(); + const flow = createFlowService(repository, deterministicEnvironment()); + const recovery = new RecoveryController({ + assess: async () => ({ kind: "unavailable", reason: "test" }), + }); + let admissionBlocked: (() => void) | null = null; + let admissionGate: Promise | null = null; + let corruptReceipt = false; + let corruptMarker = false; + let sessionReadFailure = false; + let rejectContextRead = false; + let rejectSessionRead = false; + let sessionReadGate: Promise | null = null; + let releaseSessionRead: (() => void) | null = null; + let sessionEntered = Promise.withResolvers(); + let contextReadGate: Promise | null = null; + let releaseContextRead: (() => void) | null = null; + let contextEntered = Promise.withResolvers(); + let lost = false; + let pushEvent: ((event: OpenCodeEvent | null) => void) | null = null; + const queuedEvents: OpenCodeEvent[] = []; + async function waitAdmission(signal?: AbortSignal) { + if (!admissionGate) return; + if (!signal) return admissionGate; + const cancelled = Promise.withResolvers(); + const abort = () => + cancelled.reject(new Error("Native admission aborted.")); + if (signal.aborted) abort(); + else signal.addEventListener("abort", abort, { once: true }); + try { + await Promise.race([admissionGate, cancelled.promise]); + } finally { + signal.removeEventListener("abort", abort); + } + } + async function emit( + name: Name, + input: SessionHooks[Name], + ): Promise { + for (const callback of hooks[name]) await callback(input); + } + type NativeHook = Parameters[1]>[0]; + const toolCallbacks: HookSets<{ + "execute.before": Exclude; + "execute.after": Extract; + }> = { "execute.before": new Set(), "execute.after": new Set() }; + const ctx: Parameters[0]["ctx"] = { + location: { directory }, + session: { + hook: async (name, callback) => { + hooks[name].add(callback); + return { + dispose: async () => { + hooks[name].delete(callback); + registrations.push(name); + }, + }; + }, + get: async (input) => { + if (sessionReadFailure) { + sessionReadFailure = false; + throw new Error("Native session read failed."); + } + const shouldReject = rejectSessionRead; + rejectSessionRead = false; + const snapshot = structuredClone( + children.find((child) => child.id === input.sessionID) ?? session, + ); + const gate = sessionReadGate; + sessionReadGate = null; + if (gate) { + sessionEntered.resolve(); + await gate; + } + if (shouldReject) throw new Error("Native session snapshot failed."); + return snapshot; + }, + context: async () => { + const shouldReject = rejectContextRead; + rejectContextRead = false; + const snapshot = structuredClone(native); + const gate = contextReadGate; + contextReadGate = null; + if (gate) { + contextEntered.resolve(); + await gate; + } + if (shouldReject) throw new Error("Native context snapshot failed."); + return snapshot; + }, + create: async (input) => { + const child = { + ...session, + id: "session-child", + parentID: input?.parentID ?? sessionID, + agent: input?.agent ?? agent, + }; + children.push(child); + return child; + }, + prompt: async (input, request) => { + const id = SessionMessage.ID.make(input.id ?? "msg_external"); + await emit("prompt", { + sessionID: Session.ID.make(input.sessionID), + messageID: id, + prompt: { text: input.text }, + ...(input.metadata === undefined + ? {} + : { metadata: structuredClone(input.metadata) }), + delivery: input.delivery ?? "queue", + }); + const receipt: SessionInboxUser = { + id, + sessionID: input.sessionID, + type: "user", + payload: { + text: input.text, + ...(input.metadata === undefined + ? {} + : { metadata: structuredClone(input.metadata) }), + }, + time: { created: 0 }, + delivery: input.delivery ?? "queue", + }; + native.push({ + type: "user", + id, + text: input.text, + ...(input.metadata === undefined + ? {} + : { metadata: structuredClone(input.metadata) }), + time: { created: 0 }, + }); + await waitAdmission(request?.signal); + return corruptMarker + ? { + ...receipt, + payload: { + ...receipt.payload, + metadata: { [FLOW_AUTO_METADATA_KEY]: "forged-marker" }, + }, + } + : corruptReceipt + ? { ...receipt, id: "msg_wrong" } + : receipt; + }, + synthetic: async (input, request) => { + const id = input.id ?? "msg_synthetic"; + native.push({ + type: "synthetic", + id, + text: input.text, + ...(input.metadata === undefined + ? {} + : { metadata: { ...input.metadata } }), + time: { created: 0 }, + }); + await waitAdmission(request?.signal); + return { + id, + sessionID: input.sessionID, + type: "synthetic", + payload: { + text: input.text, + ...(input.metadata === undefined + ? {} + : { metadata: { ...input.metadata } }), + }, + time: { created: 0 }, + delivery: input.delivery ?? "queue", + }; + }, + }, + command: { + transform: async (callback) => { + callback({ + add: (definition) => { + commands.set(definition.name, definition); + }, + }); + return { + dispose: async () => { + commands.clear(); + registrations.push("command"); + }, + }; + }, + }, + tool: { + hook: async (name, callback) => { + toolCallbacks[name].add(callback); + return { + dispose: async () => { + toolCallbacks[name].delete(callback); + registrations.push("tool"); + }, + }; + }, + }, + agent: { + get: async () => ({ + location: { directory }, + data: { + id: "flow-reviewer", + name: "flow-reviewer", + mode: "subagent", + hidden: true, + permissions: [], + request: { settings: {}, headers: {}, body: {} }, + }, + }), + }, + event: { + subscribe: async function* (request) { + while (!request?.signal?.aborted && !lost) { + const event = + queuedEvents.shift() ?? + (await new Promise((resolve) => { + pushEvent = resolve; + request?.signal?.addEventListener("abort", () => resolve(null), { + once: true, + }); + })); + if (event) yield event; + if (event === null) break; + } + }, + }, + }; + let lifecycle: Awaited>; + const autoDrive = new AutoDriveCoordinator({ + recovery, + readProjection: async () => projection, + prompt: (...args) => lifecycle.prompt(...args), + createToken: () => "local-token", + }); + lifecycle = await registerFlowLifecycle({ + ctx, + autoDrive, + recovery, + flow, + codingModels, + runtimeGuard: { + query: () => ({ + operational: true, + reason: "sole-instance", + message: "active", + }), + assertOperational: () => {}, + release: () => true, + }, + log: (_level, message) => { + logs.push(message); + }, + }); + const context = (): SessionContext => ({ + sessionID, + agent: selectedAgent, + model, + system: [], + options: {}, + tools: {}, + messages: native + .filter( + (message) => + message.type === "user" || + message.type === "synthetic" || + (message.type === "compaction" && message.status === "completed"), + ) + .map((message) => ({ + role: "user", + id: message.id, + content: [ + { + type: "text", + text: "text" in message ? message.text : "checkpoint", + }, + ], + })), + }); + return { + lifecycle, + autoDrive, + recovery, + repository, + hooks, + commands, + logs, + native, + session, + children, + registrations, + codingModels, + emit, + context, + toolCallbacks, + setProjection(value: AutoDriveProjection) { + projection = value; + }, + rejectNextContextRead() { + rejectContextRead = true; + }, + rejectNextSessionRead() { + rejectSessionRead = true; + }, + allowNewAdmissions() { + admissionGate = null; + }, + blockAdmission() { + admissionGate = new Promise((resolve) => { + admissionBlocked = resolve; + }); + }, + releaseAdmission() { + admissionBlocked?.(); + admissionGate = null; + }, + corruptReceipt() { + corruptReceipt = true; + }, + corruptMarkerReceipt() { + corruptMarker = true; + }, + waitSessionRead() { + return sessionEntered.promise; + }, + blockNextSessionRead() { + sessionEntered = Promise.withResolvers(); + sessionReadGate = new Promise((resolve) => { + releaseSessionRead = resolve; + }); + }, + releaseSessionRead() { + releaseSessionRead?.(); + }, + waitContextRead() { + return contextEntered.promise; + }, + blockNextContextRead() { + contextEntered = Promise.withResolvers(); + const gate = Promise.withResolvers(); + contextReadGate = gate.promise; + releaseContextRead = gate.resolve; + return gate.resolve; + }, + releaseContextRead() { + releaseContextRead?.(); + }, + failNextSessionRead() { + sessionReadFailure = true; + }, + loseStream() { + lost = true; + pushEvent?.(null); + }, + sendEvent(event: OpenCodeEvent) { + if (pushEvent) { + const push = pushEvent; + pushEvent = null; + push(event); + } else queuedEvents.push(event); + }, + async command(name: string, text: string) { + const command = commands.get(name); + if (!command) throw new Error("missing command"); + await command.execute({ sessionID, prompt: { text }, delivery: "queue" }); + }, + async flush() { + for (let i = 0; i < 20; i++) await Promise.resolve(); + }, + }; +} + +test("all five native commands register and stop grants no new automatic authority", async () => { + const f = await fixture(); + try { + expect([...f.commands.keys()]).toEqual([ + "flow-auto", + "flow-plan", + "flow-run", + "flow-review", + "flow-status", + ]); + await f.command("flow-auto", "Inspect this project and create a plan"); + expect(f.autoDrive.captureAuthority(sessionID)?.token).toBe("local-token"); + await f.command("flow-auto", "stop"); + expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); + } finally { + await f.lifecycle.dispose(); + } +}); +test("pre-admission command hooks grant no lease or Flow write and join after acceptance", async () => { + const f = await fixture(); + try { + f.blockAdmission(); + const command = f.command( + "flow-auto", + "Inspect this project and create a plan", + ); + await f.flush(); + expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); + expect(f.repository.saveCount).toBe(0); + expect(f.native[0]?.metadata).toEqual({ + [FLOW_AUTO_METADATA_KEY]: "local-token", + }); + let ready = false; + const context = f.emit("context", f.context()).then(() => { + ready = true; + }); + await f.flush(); + expect(ready).toBe(false); + f.releaseAdmission(); + await Promise.all([command, context]); + expect(f.autoDrive.captureAuthority(sessionID)?.token).toBe("local-token"); + await f.lifecycle.prompt( + sessionID, + "Continue the accepted goal", + delivery, + { [FLOW_AUTO_METADATA_KEY]: "local-token" }, + ); + expect(f.native.map((message) => message.metadata)).toEqual([ + { [FLOW_AUTO_METADATA_KEY]: "local-token" }, + { [FLOW_AUTO_METADATA_KEY]: "local-token" }, + ]); + } finally { + await f.lifecycle.dispose(); + } +}); +test("a mismatched command receipt cannot activate automatic execution", async () => { + const f = await fixture(); + try { + f.corruptReceipt(); + await expect( + f.command("flow-auto", "Inspect this project and create a plan"), + ).rejects.toThrow("receipt"); + expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); + expect(f.repository.saveCount).toBe(0); + } finally { + await f.lifecycle.dispose(); + } +}); +test("new real input vetoes an old automatic context and a stale native shell", async () => { + const f = await fixture(); + try { + await f.command("flow-auto", "Inspect this project and create a plan"); + await f.emit("context", f.context()); + await f.emit("http.request", { + sessionID, + agent, + model, + kind: "primary", + request: new Request("http://localhost/primary"), + }); + const assistantID = SessionMessage.ID.make("msg_manager"); + const callID = CallID.make("call_shell"); + f.native.push({ + type: "assistant", + id: assistantID, + agent, + model, + time: { created: 1 }, + content: [ + { + type: "tool", + id: callID, + name: "shell", + time: { created: 1 }, + state: { + status: "running", + input: { command: "touch forbidden" }, + metadata: {}, + }, + }, + ], + }); + const execution = { + sessionID, + agent, + messageID: assistantID, + id: callID, + signal: new AbortController().signal, + progress: async () => {}, + }; + await f.lifecycle.beforeTool(execution); + await f.emit("prompt", { + sessionID, + messageID: SessionMessage.ID.make("msg_stop"), + prompt: { text: "Stop and change direction" }, + delivery: "steer", + }); + await expect(f.lifecycle.beforeTool(execution)).rejects.toThrow("epoch"); + await expect(f.emit("context", f.context())).rejects.toThrow(); + } finally { + await f.lifecycle.dispose(); + } +}); +test("synthetic delivery preserves selected agent and model and refuses stale tokens", async () => { + const f = await fixture(); + try { + await f.command("flow-auto", "Inspect this project and create a plan"); + await expect( + f.lifecycle.prompt( + sessionID, + "next", + { ...delivery, agent: "plan" }, + { [FLOW_AUTO_METADATA_KEY]: "local-token" }, + ), + ).rejects.toThrow("selected agent or model"); + await expect( + f.lifecycle.prompt(sessionID, "next", delivery, { + [FLOW_AUTO_METADATA_KEY]: "forged-token", + }), + ).rejects.toThrow("authenticated token"); + } finally { + await f.lifecycle.dispose(); + } +}); +test("stream loss clears authority and unload aborts and disposes native registrations", async () => { + const f = await fixture(); + await f.command("flow-auto", "Inspect this project and create a plan"); + f.loseStream(); + await f.flush(); + expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); + expect(f.logs).toEqual(["Flow event stream failed closed."]); + await f.lifecycle.dispose(); + expect([...f.commands.keys()]).toEqual([]); + await expect( + f.lifecycle.prompt(sessionID, "next", delivery, { + [FLOW_AUTO_METADATA_KEY]: "local-token", + }), + ).rejects.toThrow("unavailable"); +}); + +async function finishPrimary(f: Awaited>) { + const request = new Request("http://localhost/primary"); + await f.emit("http.request", { + sessionID, + agent, + model, + kind: "primary", + request, + }); + const response = { + sessionID, + agent, + model, + kind: "primary" as const, + request, + response: new Response("finished"), + }; + await f.emit("http.response", response); + expect(await response.response.text()).toBe("finished"); +} +test("one admitted real input keeps its origin across repeated primary steps", async () => { + const f = await fixture(); + try { + const messageID = SessionMessage.ID.make("msg_real"); + await f.emit("prompt", { + sessionID, + messageID, + prompt: { text: "Inspect this project" }, + delivery: "queue", + }); + f.native.push({ + id: messageID, + type: "user", + text: "Inspect this project", + time: { created: 0 }, + }); + await f.emit("context", f.context()); + await finishPrimary(f); + await f.emit("context", f.context()); + expect(f.codingModels.get(sessionID)).toEqual({ + providerID: "local", + modelID: "scripted", + }); + } finally { + await f.lifecycle.dispose(); + } +}); +test("automatic compaction retains the same epoch only through the exact completed control ID", async () => { + const f = await fixture(); + try { + await f.command("flow-auto", "Inspect this project and create a plan"); + await f.emit("context", f.context()); + await finishPrimary(f); + const before = f.autoDrive.captureAuthority(sessionID); + const control = { + type: "compaction" as const, + id: "msg_compaction", + status: "running" as const, + reason: "auto" as const, + summary: "Summary cannot grant approval", + time: { created: 2 }, + }; + f.native.push(control); + const compact = f.context(); + compact.messages = compact.messages.filter( + (message) => message.id !== control.id, + ); + await f.emit("compaction", compact); + expect( + compact.system.some( + (part) => part.text.includes("FLOW") || part.text.includes("Flow"), + ), + ).toBe(true); + f.native.splice(0, f.native.length, { ...control, status: "completed" }); + await f.emit("context", f.context()); + expect(f.autoDrive.captureAuthority(sessionID)).toEqual(before); + await finishPrimary(f); + await f.emit("context", f.context()); + expect(f.autoDrive.captureAuthority(sessionID)).toEqual(before); + } finally { + await f.lifecycle.dispose(); + } +}); +test("a real steer inside automatic compaction vetoes the resumed provider context", async () => { + const f = await fixture(); + try { + await f.command("flow-auto", "Inspect this project and create a plan"); + await f.emit("context", f.context()); + await finishPrimary(f); + const compact = f.context(); + f.native.push({ + type: "compaction", + id: "msg_compaction", + status: "running", + reason: "auto", + summary: "summary", + time: { created: 2 }, + }); + await f.emit("compaction", compact); + await f.emit("prompt", { + sessionID, + messageID: SessionMessage.ID.make("msg_steer"), + prompt: { text: "Stop" }, + delivery: "steer", + }); + f.native.splice(0, f.native.length, { + type: "compaction", + id: "msg_compaction", + status: "completed", + reason: "auto", + summary: "summary", + time: { created: 2 }, + }); + await expect(f.emit("context", f.context())).rejects.toThrow(); + expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); + } finally { + await f.lifecycle.dispose(); + } +}); +test("manual compaction cannot retain an exactly matched automatic grant", async () => { + const f = await fixture(); + try { + await f.command("flow-auto", "Inspect this project and create a plan"); + await f.emit("context", f.context()); + await finishPrimary(f); + const compact = f.context(); + f.native.push({ + type: "compaction", + id: "msg_compaction", + status: "running", + reason: "manual", + summary: "summary", + time: { created: 2 }, + }); + await expect(f.emit("compaction", compact)).rejects.toThrow( + "manual compaction", + ); + expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); + } finally { + await f.lifecycle.dispose(); + } +}); +test("an unmatched control callback rejects without owning the stored checkpoint lease", async () => { + const f = await fixture(); + const freeze = spyOn(ExecutionAttribution.prototype, "freeze"); + try { + await f.command("flow-auto", "Inspect exact source"); + await finishOwnedResponse(f, await startOwnedRequest(f)); + await compactCurrentInput(f, "msg_known_control"); + const valid = structuredClone(f.native); + const authority = f.autoDrive.captureAuthority(sessionID); + const writes = f.repository.saveCount; + freeze.mockClear(); + f.native.splice(0, f.native.length, { + type: "compaction", + id: "msg_foreign_control", + status: "completed", + reason: "auto", + summary: "Foreign", + time: { created: 3 }, + }); + await expect(f.emit("context", f.context())).rejects.toThrow( + "epoch was revoked", + ); + expect(f.autoDrive.captureAuthority(sessionID)).toEqual(authority); + expect(freeze).toHaveBeenCalledTimes(0); + + expect(f.repository.saveCount).toBe(writes); + f.native.splice(0, f.native.length, ...valid); + await finishOwnedResponse(f, await startOwnedRequest(f)); + await f.lifecycle.beforeTool(ownershipNativeCall(f)); + expect(freeze).toHaveBeenCalledTimes(1); + expect(f.autoDrive.captureAuthority(sessionID)).toEqual(authority); + } finally { + freeze.mockRestore(); + await f.lifecycle.dispose(); + } +}); + +test("review dispatch creates the reserved native child after the parent admission", async () => { + const f = await fixture(); + try { + await f.command("flow-review", "Review assignment review-1"); + expect( + f.children.map((child) => ({ + id: child.id, + parentID: child.parentID, + agent: child.agent, + })), + ).toEqual([ + { + id: "session-child", + parentID: "session-lifecycle", + agent: "flow-reviewer", + }, + ]); + const childInput = f.native.at(-1); + expect( + childInput?.type === "user" ? childInput.text.split("\n")[0] : "missing", + ).toBe("Flow flow-review: Review assignment review-1"); + expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); + } finally { + await f.lifecycle.dispose(); + } +}); +test("real-input revocation invalidates an already acquired recovery guard without resetting its budget", async () => { + const f = await fixture(); + try { + await f.command( + "flow-auto", + '--recovery=shadow --recovery-calls=2 --recovery-usd=0.1 Implement the test named "must pass"', + ); + await f.emit("context", f.context()); + await f.emit("http.request", { + sessionID, + agent, + model, + kind: "primary", + request: new Request("http://localhost/primary"), + }); + const messageID = SessionMessage.ID.make("msg_recovery_manager"); + const id = CallID.make("call_close"); + f.native.push({ + type: "assistant", + id: messageID, + agent, + model, + time: { created: 1 }, + content: [ + { + type: "tool", + id, + name: "flow_session_close", + time: { created: 1 }, + state: { status: "running", input: {}, metadata: {} }, + }, + ], + }); + await f.lifecycle.beforeTool({ + sessionID, + agent, + messageID, + id, + signal: new AbortController().signal, + progress: async () => {}, + }); + const guard = f.recovery.guard({ + hostSessionId: sessionID, + messageId: messageID, + agent, + }); + const session = f.repository.session; + if (!session) + throw new Error("Command did not anchor its declared assertion."); + const close = { + operationId: "close", + expectedRevision: session.revision, + sessionId: session.id, + kind: "completed" as const, + summary: "done", + }; + guard.checkClose(session, close); + await f.emit("prompt", { + sessionID, + messageID: SessionMessage.ID.make("msg_new_direction"), + prompt: { text: "Stop" }, + delivery: "steer", + }); + expect(() => guard.checkClose(session, close)).toThrow("lineage expired"); + expect(f.recovery.snapshot(sessionID)).toMatchObject({ + mode: "shadow", + remainingCalls: 2, + maxUsd: 0.1, + }); + } finally { + await f.lifecycle.dispose(); + } +}); +test("duplicate succeeded idle notifications dispatch only one continuation", async () => { + const f = await fixture(); + try { + await f.command("flow-auto", "Implement the approved change"); + await f.emit("context", f.context()); + await finishPrimary(f); + const input = f.native.at(-1); + if (!input) throw new Error("Missing accepted input."); + f.autoDrive.observeAssistant(sessionID, "msg_manager", input.id); + f.autoDrive.observeMutation(sessionID, 5, undefined, "msg_manager", false); + f.setProjection({ + sessionId: "flow-1", + revision: 5, + status: "ready", + nextAction: "flow_run_start", + }); + f.session.outcome = "succeeded"; + f.session.time.idle = 3; + const event: OpenCodeEvent = { + id: "evt_idle", + created: 3, + type: "session.idle", + data: { sessionID }, + }; + f.sendEvent(event); + await f.flush(); + f.sendEvent(event); + await f.flush(); + expect( + f.native + .filter((message) => message.type === "synthetic") + .map((message) => message.type), + ).toEqual(["synthetic"]); + } finally { + await f.lifecycle.dispose(); + } +}); + +test.each(["failed", "interrupted"] as const)( + "duplicate native %s outcomes cannot revoke a later accepted invocation", + async (outcome) => { + const f = await fixture(); + try { + await f.command("flow-auto", "Inspect this project and create a plan"); + await f.emit("context", f.context()); + f.native.push({ + id: "msg_terminal", + type: "idle", + time: { created: 1 }, + outcome, + }); + const durable = { aggregateID: sessionID, seq: 1, version: 1 as const }; + const event: OpenCodeEvent = + outcome === "failed" + ? { + id: "evt_failed", + type: "session.execution.failed", + created: 1, + durable, + data: { + sessionID, + error: { type: "provider", message: "failed" }, + }, + } + : { + id: "evt_interrupted", + type: "session.execution.interrupted", + created: 1, + durable, + data: { sessionID, reason: "user" }, + }; + f.sendEvent(event); + await f.flush(); + expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); + await f.command("flow-auto", "Inspect the new approved direction"); + const accepted = f.autoDrive.captureAuthority(sessionID); + f.sendEvent(event); + await f.flush(); + expect(f.autoDrive.captureAuthority(sessionID)).toEqual(accepted); + expect(f.autoDrive.captureAuthority(sessionID)?.token).toBe( + "local-token", + ); + } finally { + await f.lifecycle.dispose(); + } + }, +); + +test("unload aborts an idle-triggered native admission and waits for subscription return", async () => { + const f = await fixture(); + await f.command("flow-auto", "Implement the approved change"); + await f.emit("context", f.context()); + await finishPrimary(f); + const input = f.native.at(-1); + if (!input) throw new Error("Missing accepted input."); + f.autoDrive.observeAssistant(sessionID, "msg_manager", input.id); + f.autoDrive.observeMutation(sessionID, 5, undefined, "msg_manager", false); + f.setProjection({ + sessionId: "flow-1", + revision: 5, + status: "ready", + nextAction: "flow_run_start", + }); + f.blockAdmission(); + f.session.outcome = "succeeded"; + f.session.time.idle = 3; + f.sendEvent({ + id: "evt_idle", + created: 3, + type: "session.idle", + data: { sessionID }, + }); + await f.flush(); + expect( + f.native.filter((message) => message.type === "synthetic").length, + ).toBe(1); + await f.lifecycle.dispose(); + expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); + expect([...f.commands.keys()]).toEqual([]); + f.releaseAdmission(); +}); + +test("native execution success dispatches from the session watermark without an idle context record", async () => { + const f = await fixture(); + try { + await f.command("flow-auto", "Implement the approved change"); + await f.emit("context", f.context()); + await finishPrimary(f); + const input = f.native.at(-1); + if (!input) throw new Error("Missing accepted input."); + f.autoDrive.observeAssistant(sessionID, "msg_manager", input.id); + f.autoDrive.observeMutation(sessionID, 5, undefined, "msg_manager", false); + f.setProjection({ + sessionId: "flow-1", + revision: 5, + status: "ready", + nextAction: "flow_run_start", + }); + f.session.outcome = "succeeded"; + f.session.time.idle = 3; + const event: OpenCodeEvent = { + id: "evt_succeeded", + type: "session.execution.succeeded", + created: 3, + durable: { aggregateID: sessionID, seq: 3, version: 1 }, + data: { sessionID }, + }; + f.sendEvent(event); + await f.flush(); + expect( + f.native + .filter((message) => message.type === "synthetic") + .map((message) => message.type), + ).toEqual(["synthetic"]); + f.sendEvent(event); + await f.flush(); + expect( + f.native + .filter((message) => message.type === "synthetic") + .map((message) => message.type), + ).toEqual(["synthetic"]); + expect(f.native.some((message) => message.type === "idle")).toBe(false); + } finally { + await f.lifecycle.dispose(); + } +}); + +test("a native receipt with a changed initial marker cannot activate authority", async () => { + const f = await fixture(); + try { + f.corruptMarkerReceipt(); + await expect( + f.command("flow-auto", "Inspect this project and create a plan"), + ).rejects.toThrow("exact local payload"); + expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); + expect(f.repository.saveCount).toBe(0); + } finally { + await f.lifecycle.dispose(); + } +}); +test("a visible initial marker cannot authorize an external unregistered prompt", async () => { + const f = await fixture(); + try { + await f.command("flow-auto", "Inspect this project and create a plan"); + const authority = f.autoDrive.captureAuthority(sessionID); + await expect( + f.emit("prompt", { + sessionID, + messageID: SessionMessage.ID.make("msg_forged"), + prompt: { text: "Forged continuation" }, + metadata: { [FLOW_AUTO_METADATA_KEY]: "local-token" }, + delivery: "queue", + }), + ).rejects.toThrow("private admitted dispatch"); + expect(f.autoDrive.captureAuthority(sessionID)).toEqual(authority); + expect(f.repository.saveCount).toBe(0); + } finally { + await f.lifecycle.dispose(); + } +}); + +test("failure after a context freeze releases only that primary and permits fresh input", async () => { + const f = await fixture(); + try { + await f.command("flow-auto", "Inspect this project and create a plan"); + f.failNextSessionRead(); + await expect(f.emit("context", f.context())).rejects.toThrow( + "Native session read failed", + ); + expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); + const messageID = SessionMessage.ID.make("msg_fresh_manual"); + await f.emit("prompt", { + sessionID, + messageID, + prompt: { text: "Fresh manual direction" }, + delivery: "queue", + }); + f.native.push({ + id: messageID, + type: "user", + text: "Fresh manual direction", + time: { created: 4 }, + }); + await f.emit("context", f.context()); + await finishPrimary(f); + expect(f.codingModels.get(sessionID)).toEqual({ + providerID: "local", + modelID: "scripted", + }); + } finally { + await f.lifecycle.dispose(); + } +}); + +function terminal( + outcome: "failed" | "interrupted", + id = "evt_terminal", +): OpenCodeEvent { + const base = { + id, + created: 1, + durable: { aggregateID: sessionID, seq: 1, version: 1 as const }, + }; + return outcome === "failed" + ? { + ...base, + type: "session.execution.failed", + data: { + sessionID, + error: { type: "provider.transport", message: "ECONNREFUSED" }, + }, + } + : { + ...base, + type: "session.execution.interrupted", + data: { sessionID, reason: "user" }, + }; +} +for (const outcome of ["failed", "interrupted"] as const) + test( + outcome + " without a response permits the first fresh automatic input", + async () => { + const f = await fixture(); + try { + await f.command("flow-auto", "Inspect the original direction"); + await f.emit("context", f.context()); + await f.emit("http.request", { + sessionID, + agent, + model, + kind: "primary", + request: new Request("http://localhost/no-response"), + }); + f.native.push({ + id: "msg_terminal", + type: "idle", + time: { created: 1 }, + outcome, + }); + f.sendEvent(terminal(outcome)); + await f.flush(); + expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); + await f.command("flow-auto", "Inspect the fresh direction"); + const authority = f.autoDrive.captureAuthority(sessionID); + expect(authority?.token).toBe("local-token"); + await f.emit("context", f.context()); + expect(f.autoDrive.captureAuthority(sessionID)).toEqual(authority); + const request = new Request("http://localhost/fresh"); + await f.emit("http.request", { + sessionID, + agent, + model, + kind: "primary", + request, + }); + const response = { + sessionID, + agent, + model, + kind: "primary" as const, + request, + response: new Response("fresh provider response"), + }; + await f.emit("http.response", response); + expect(await response.response.text()).toBe("fresh provider response"); + f.sendEvent(terminal(outcome, "evt_delayed_old")); + await f.flush(); + expect(f.autoDrive.captureAuthority(sessionID)).toEqual(authority); + } finally { + await f.lifecycle.dispose(); + } + }, + ); +test("fresh input retires old request even before its terminal event arrives", async () => { + const f = await fixture(); + try { + await f.command("flow-auto", "Inspect original"); + await f.emit("context", f.context()); + await f.emit("http.request", { + sessionID, + agent, + model, + kind: "primary", + request: new Request("http://localhost/old"), + }); + await f.command("flow-auto", "Inspect new"); + await f.emit("context", f.context()); + expect(f.autoDrive.captureAuthority(sessionID)?.token).toBe("local-token"); + } finally { + await f.lifecycle.dispose(); + } +}); +test("fresh native primary retry state ends its response-less attempt and preserves the lease", async () => { + const f = await fixture(); + try { + await f.command("flow-auto", "Inspect original"); + await f.emit("context", f.context()); + await f.emit("http.request", { + sessionID, + agent, + model, + kind: "primary", + request: new Request("http://localhost/refused"), + }); + const authority = f.autoDrive.captureAuthority(sessionID); + f.native.push({ + type: "assistant", + id: "msg_retrying_primary", + agent, + model, + time: { created: 1 }, + content: [], + retry: { + attempt: 2, + at: 2000, + error: { type: "provider.transport", message: "ECONNREFUSED" }, + }, + }); + await f.emit("retry", { + sessionID, + agent, + model, + error: { type: "provider.transport", message: "ECONNREFUSED" }, + attempt: 2, + decision: { retry: true, delay: 2000 }, + }); + await f.emit("context", f.context()); + expect(f.autoDrive.captureAuthority(sessionID)).toEqual(authority); + } finally { + await f.lifecycle.dispose(); + } +}); +test("an overlapping live epoch still fails closed", async () => { + const f = await fixture(); + try { + await f.command("flow-auto", "Inspect"); + await f.emit("context", f.context()); + await expect(f.emit("context", f.context())).rejects.toThrow("overlap"); + expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); + } finally { + await f.lifecycle.dispose(); + } +}); + +for (const outcome of ["failed", "interrupted"] as const) + test( + "a delayed old " + outcome + " read cannot retire a replacement primary", + async () => { + const f = await fixture(); + try { + await f.command("flow-auto", "Inspect original"); + await f.emit("context", f.context()); + await f.emit("http.request", { + sessionID, + agent, + model, + kind: "primary", + request: new Request("http://localhost/old"), + }); + f.native.push({ + id: "msg_old_terminal", + type: "idle", + time: { created: 1 }, + outcome, + }); + f.blockNextContextRead(); + f.sendEvent(terminal(outcome)); + await f.flush(); + await f.command("flow-auto", "Inspect replacement"); + const authority = f.autoDrive.captureAuthority(sessionID); + await f.emit("context", f.context()); + const request = new Request("http://localhost/replacement"); + await f.emit("http.request", { + sessionID, + agent, + model, + kind: "primary", + request, + }); + f.releaseContextRead(); + await f.flush(); + expect(f.autoDrive.captureAuthority(sessionID)).toEqual(authority); + const response = { + sessionID, + agent, + model, + kind: "primary" as const, + request, + response: new Response("replacement survived"), + }; + await f.emit("http.response", response); + expect(await response.response.text()).toBe("replacement survived"); + } finally { + f.releaseContextRead(); + await f.lifecycle.dispose(); + } + }, + ); + +test("a delayed terminal session read cannot revoke a replacement primary", async () => { + const f = await fixture(); + try { + await f.command("flow-auto", "Inspect original"); + await f.emit("context", f.context()); + await finishPrimary(f); + f.session.outcome = "failed"; + f.session.time.idle = 1; + f.blockNextSessionRead(); + f.sendEvent({ + id: "evt_old_success_notification", + type: "session.execution.succeeded", + created: 1, + durable: { aggregateID: sessionID, seq: 1, version: 1 }, + data: { sessionID }, + }); + await f.flush(); + await f.command("flow-auto", "Inspect replacement"); + const authority = f.autoDrive.captureAuthority(sessionID); + await f.emit("context", f.context()); + const request = new Request("http://localhost/replacement"); + await f.emit("http.request", { + sessionID, + agent, + model, + kind: "primary", + request, + }); + f.releaseSessionRead(); + await f.flush(); + expect(f.autoDrive.captureAuthority(sessionID)).toEqual(authority); + const response = { + sessionID, + agent, + model, + kind: "primary" as const, + request, + response: new Response("replacement survived"), + }; + await f.emit("http.response", response); + expect(await response.response.text()).toBe("replacement survived"); + } finally { + f.releaseSessionRead(); + await f.lifecycle.dispose(); + } +}); + +test("native retry keeps its unfinished assistant and late old responses cannot revoke it", async () => { + const f = await fixture(); + try { + await f.command("flow-auto", "Inspect original"); + await f.emit("context", f.context()); + const oldRequest = new Request("http://localhost/refused"); + await f.emit("http.request", { + sessionID, + agent, + model, + kind: "primary", + request: oldRequest, + }); + const assistantID = SessionMessage.ID.make("msg_retry_assistant"); + const callID = CallID.make("call_retry_status"); + const assistant: Extract = { + type: "assistant", + id: assistantID, + agent, + model, + time: { created: 1 }, + content: [], + }; + f.native.push(assistant); + const authority = f.autoDrive.captureAuthority(sessionID); + assistant.retry = { + attempt: 2, + at: 2000, + error: { type: "provider.transport", message: "ECONNREFUSED" }, + }; + await f.emit("retry", { + sessionID, + agent, + model, + error: { type: "provider.transport", message: "ECONNREFUSED" }, + attempt: 2, + decision: { retry: true, delay: 2000 }, + }); + await f.emit("context", f.context()); + const request = new Request("http://localhost/retry"); + await f.emit("http.request", { + sessionID, + agent, + model, + kind: "primary", + request, + }); + await expect( + f.emit("http.response", { + sessionID, + agent, + model, + kind: "primary", + request: oldRequest, + response: new Response("late old response"), + }), + ).rejects.toThrow("Request identity"); + assistant.content.push({ + type: "tool", + id: callID, + name: "flow_status", + time: { created: 2 }, + state: { + status: "running", + input: { request: { view: "compact" } }, + metadata: {}, + }, + }); + const response = { + sessionID, + agent, + model, + kind: "primary" as const, + request, + response: new Response("retried tool call"), + }; + await f.emit("http.response", response); + expect(await response.response.text()).toBe("retried tool call"); + await f.lifecycle.beforeTool({ + sessionID, + agent, + messageID: assistantID, + id: callID, + signal: new AbortController().signal, + progress: async () => {}, + }); + expect(f.autoDrive.captureAuthority(sessionID)).toEqual(authority); + } finally { + await f.lifecycle.dispose(); + } +}); + +test("same-session same-model auxiliary retry cannot retire a healthy primary", async () => { + const f = await fixture(); + try { + await f.command("flow-auto", "Inspect original"); + await f.emit("context", f.context()); + const request = new Request("http://localhost/healthy-primary"); + await f.emit("http.request", { + sessionID, + agent, + model, + kind: "primary", + request, + }); + const authority = f.autoDrive.captureAuthority(sessionID); + await f.emit("retry", { + sessionID, + agent, + model, + error: { + type: "provider.transport", + message: "Auxiliary generate refused", + }, + attempt: 2, + decision: { retry: true, delay: 2000 }, + }); + const response = { + sessionID, + agent, + model, + kind: "primary" as const, + request, + response: new Response("healthy primary survived auxiliary retry"), + }; + await f.emit("http.response", response); + expect(await response.response.text()).toBe( + "healthy primary survived auxiliary retry", + ); + expect(f.autoDrive.captureAuthority(sessionID)).toEqual(authority); + } finally { + await f.lifecycle.dispose(); + } +}); + +test("an already consumed primary retry record cannot permit another overlapping context", async () => { + const f = await fixture(); + try { + await f.command("flow-auto", "Inspect original"); + await f.emit("context", f.context()); + await f.emit("http.request", { + sessionID, + agent, + model, + kind: "primary", + request: new Request("http://localhost/refused"), + }); + f.native.push({ + type: "assistant", + id: "msg_retrying_primary", + agent, + model, + time: { created: 1 }, + content: [], + retry: { + attempt: 2, + at: 2000, + error: { type: "provider.transport", message: "ECONNREFUSED" }, + }, + }); + await f.emit("context", f.context()); + await f.emit("http.request", { + sessionID, + agent, + model, + kind: "primary", + request: new Request("http://localhost/retry"), + }); + await expect(f.emit("context", f.context())).rejects.toThrow("overlap"); + expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); + } finally { + await f.lifecycle.dispose(); + } +}); + +type ContinuationInputKind = "real" | "synthetic" | "compaction"; +async function finishContinuationContext( + f: Awaited>, +) { + const request = new Request("http://localhost/complete"); + await f.emit("http.request", { + sessionID, + agent, + model, + kind: "primary", + request, + }); + const response = { + sessionID, + agent, + model, + kind: "primary" as const, + request, + response: new Response("completed first turn"), + }; + await f.emit("http.response", response); + expect(await response.response.text()).toBe("completed first turn"); +} +async function nativeContinuationFixture(kind: ContinuationInputKind) { + const f = await fixture(); + await f.command("flow-auto", "Inspect original"); + if (kind !== "real") { + await f.emit("context", f.context()); + await finishContinuationContext(f); + if (kind === "synthetic") + await f.lifecycle.prompt( + sessionID, + "Continue the admitted direction", + delivery, + { "opencode-plugin-flow/auto": "local-token" }, + ); + else { + const control = { + type: "compaction" as const, + id: "msg_checked_checkpoint", + status: "running" as const, + reason: "auto" as const, + summary: "summary", + time: { created: 2 }, + }; + f.native.push(control); + const context = f.context(); + context.messages = context.messages.filter( + (message) => message.id !== control.id, + ); + await f.emit("compaction", context); + f.native.splice(0, f.native.length, { ...control, status: "completed" }); + } + } + return f; +} +for (const kind of ["real", "synthetic", "compaction"] as const) + for (const attempt of ["first", "retry"] as const) + for (const state of ["live", "revoked"] as const) + test( + kind + " " + attempt + " " + state + " exact native continuation", + async () => { + const f = await nativeContinuationFixture(kind); + try { + const authority = f.autoDrive.captureAuthority(sessionID); + expect(authority?.token).toBe("local-token"); + let oldRequest: Request | undefined; + const assistantID = SessionMessage.ID.make("msg_matrix_assistant"); + const callID = CallID.make("call_matrix_status"); + const assistant: Extract< + SessionMessageInfo, + { type: "assistant" } + > = { + type: "assistant", + id: assistantID, + agent, + model, + time: { created: 3 }, + content: [], + }; + if (attempt === "retry") { + await f.emit("context", f.context()); + oldRequest = new Request("http://localhost/refused"); + await f.emit("http.request", { + sessionID, + agent, + model, + kind: "primary", + request: oldRequest, + }); + assistant.retry = { + attempt: 2, + at: 2000, + error: { type: "provider.transport", message: "ECONNREFUSED" }, + }; + f.native.push(assistant); + } + if (state === "revoked") + await f.emit("prompt", { + sessionID, + messageID: SessionMessage.ID.make("msg_new_direction"), + prompt: { text: "Change direction" }, + delivery: "steer", + }); + if (state === "revoked") { + await expect(f.emit("context", f.context())).rejects.toThrow( + "Flow execution epoch was revoked.", + ); + expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); + return; + } + await f.emit("context", f.context()); + const request = new Request("http://localhost/success"); + await f.emit("http.request", { + sessionID, + agent, + model, + kind: "primary", + request, + }); + if (oldRequest) + await expect( + f.emit("http.response", { + sessionID, + agent, + model, + kind: "primary", + request: oldRequest, + response: new Response("late old response"), + }), + ).rejects.toThrow("Request identity"); + const response = { + sessionID, + agent, + model, + kind: "primary" as const, + request, + response: new Response("verified native tool call"), + }; + await f.emit("http.response", response); + expect(await response.response.text()).toBe( + "verified native tool call", + ); + if (attempt === "first") f.native.push(assistant); + delete assistant.retry; + assistant.content.push({ + type: "tool", + id: callID, + name: "flow_status", + time: { created: 4 }, + state: { + status: "running", + input: { request: { view: "compact" } }, + metadata: {}, + }, + }); + await f.lifecycle.beforeTool({ + sessionID, + agent, + messageID: assistantID, + id: callID, + signal: new AbortController().signal, + progress: async () => {}, + }); + expect(f.autoDrive.captureAuthority(sessionID)).toEqual(authority); + } finally { + await f.lifecycle.dispose(); + } + }, + ); + +async function prepareContinuationRetry( + f: Awaited>, + attempt: "first" | "retry", +) { + if (attempt === "first") return; + await f.emit("context", f.context()); + await f.emit("http.request", { + sessionID, + agent, + model, + kind: "primary", + request: new Request("http://localhost/refused"), + }); + f.native.push({ + type: "assistant", + id: "msg_negative_retry_assistant", + agent, + model, + time: { created: 3 }, + content: [], + retry: { + attempt: 2, + at: 2000, + error: { type: "provider.transport", message: "ECONNREFUSED" }, + }, + }); +} +for (const kind of ["real", "synthetic", "compaction"] as const) + for (const attempt of ["first", "retry"] as const) { + test( + kind + " " + attempt + " cannot change the admitted model", + async () => { + const f = await nativeContinuationFixture(kind); + try { + await prepareContinuationRetry(f, attempt); + const original = f.context(); + const context = { + ...original, + model: { ...original.model, id: Model.ID.make("other") }, + }; + await expect(f.emit("context", context)).rejects.toThrow( + "Flow admitted agent or model differs from its primary request.", + ); + expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); + } finally { + await f.lifecycle.dispose(); + } + }, + ); + test( + kind + " " + attempt + " rejects an unmatched native input prefix", + async () => { + const f = await nativeContinuationFixture(kind); + try { + await prepareContinuationRetry(f, attempt); + const context = f.context(); + context.messages.unshift({ + role: "user", + id: "msg_unconsumed", + content: [{ type: "text", text: "unconsumed" }], + }); + await expect(f.emit("context", context)).rejects.toThrow( + "Flow native inputs differ from the model-visible prefix.", + ); + expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); + } finally { + await f.lifecycle.dispose(); + } + }, + ); + } +for (const mismatch of ["control", "reason"] as const) + for (const attempt of ["first", "retry"] as const) + test( + "checked compaction " + attempt + " rejects " + mismatch + " mismatch", + async () => { + const f = await nativeContinuationFixture("compaction"); + try { + await prepareContinuationRetry(f, attempt); + const context = f.context(); + const control = f.native[0]; + if (control?.type !== "compaction" || control.status !== "completed") + throw new Error("Expected completed control"); + f.native[0] = + mismatch === "control" + ? { ...control, id: "msg_other_control" } + : { ...control, reason: "manual" }; + const expected = + mismatch === "reason" + ? "Flow automatic compaction control identity changed." + : attempt === "retry" + ? "Flow primary contexts or streams overlap." + : "Flow native inputs differ from the model-visible prefix."; + await expect(f.emit("context", context)).rejects.toThrow(expected); + expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); + } finally { + await f.lifecycle.dispose(); + } + }, + ); + +type OwnershipFixture = Awaited>; +function ownershipNativeCall(f: OwnershipFixture, tool = "flow_status") { + const messageID = SessionMessage.ID.create(); + const id = CallID.make("call_owned"); + const frame: Extract = { + type: "assistant", + id: messageID, + agent, + model, + time: { created: 4 }, + content: [ + { + type: "tool", + id, + name: tool, + time: { created: 4 }, + state: { + status: "running", + input: + tool === "shell" + ? { command: "printf not-run" } + : { request: { view: "compact" } }, + metadata: {}, + }, + }, + ], + }; + f.native.push(frame); + return { + sessionID, + agent, + messageID, + id, + signal: new AbortController().signal, + progress: async () => {}, + }; +} +async function finishOwnedResponse(f: OwnershipFixture, request: Request) { + const input = { + sessionID, + agent, + model, + kind: "primary" as const, + request, + response: new Response("native provider completed"), + }; + await f.emit("http.response", input); + expect(await input.response.text()).toBe("native provider completed"); +} +async function startOwnedRequest(f: OwnershipFixture) { + await f.emit("context", f.context()); + const request = new Request("http://localhost/owned"); + await f.emit("http.request", { + sessionID, + agent, + model, + kind: "primary", + request, + }); + return request; +} +async function initialOwnershipWork(f: OwnershipFixture) { + await f.command("flow-auto", "Inspect original"); + await finishOwnedResponse(f, await startOwnedRequest(f)); +} +async function freshOwnershipWork(f: OwnershipFixture) { + await f.command("flow-auto", "Inspect fresh direction"); + const authority = f.autoDrive.captureAuthority(sessionID); + expect(authority?.token).toBe("local-token"); + return { + authority, + async verify() { + await finishOwnedResponse(f, await startOwnedRequest(f)); + const call = ownershipNativeCall(f); + await f.lifecycle.beforeTool(call); + expect(f.autoDrive.captureAuthority(sessionID)).toEqual(authority); + }, + }; +} +for (const actor of ["context", "compaction"] as const) + for (const reject of [false, true]) + for (const replacement of ["admitted", "context-started"] as const) + test( + actor + + " old read " + + (reject ? "rejects" : "resumes") + + " after fresh " + + replacement, + async () => { + const f = await fixture(); + try { + await initialOwnershipWork(f); + let old = f.context(); + let control: + | Extract + | undefined; + if (actor === "compaction") { + control = { + type: "compaction", + id: "msg_old_control", + status: "running", + reason: "auto", + summary: "old", + time: { created: 2 }, + }; + f.native.push(control); + old = f.context(); + } + f.blockNextContextRead(); + if (reject) f.rejectNextContextRead(); + const pending = f.emit(actor, old); + const caught = pending.then( + () => null, + (error) => String(error), + ); + await f.waitContextRead(); + const next = await freshOwnershipWork(f); + let request: Request | undefined; + if (replacement === "context-started") + request = await startOwnedRequest(f); + f.releaseContextRead(); + expect(await caught).toBe( + "Error: Flow execution epoch was revoked.", + ); + expect(f.autoDrive.captureAuthority(sessionID)).toEqual( + next.authority, + ); + if (control) f.native.splice(f.native.indexOf(control), 1); + if (request) { + await finishOwnedResponse(f, request); + await f.lifecycle.beforeTool(ownershipNativeCall(f)); + expect(f.autoDrive.captureAuthority(sessionID)).toEqual( + next.authority, + ); + } else await next.verify(); + } finally { + f.releaseContextRead(); + await f.lifecycle.dispose(); + } + }, + ); +for (const boundary of ["flow-wrapper", "global-shell"] as const) + for (const reject of [false, true]) + for (const replacement of ["admitted", "context-started"] as const) + test( + boundary + + " old verification read " + + String(reject) + + " preserves fresh " + + replacement, + async () => { + const f = await fixture(); + try { + await initialOwnershipWork(f); + const call = ownershipNativeCall( + f, + boundary === "global-shell" ? "shell" : "flow_status", + ); + f.blockNextContextRead(); + if (reject) f.rejectNextContextRead(); + const pending = + boundary === "flow-wrapper" + ? f.lifecycle.beforeTool(call) + : (async () => { + for (const callback of f.toolCallbacks["execute.before"]) + await callback({ + ...call, + tool: "shell", + input: { command: "printf not-run" }, + }); + })(); + const caught = pending.then( + () => null, + (error) => String(error), + ); + const next = await freshOwnershipWork(f); + const request = + replacement === "context-started" + ? await startOwnedRequest(f) + : undefined; + f.releaseContextRead(); + expect(await caught).toBe( + reject + ? "Error: Native context snapshot failed." + : "Error: Flow execution epoch was revoked.", + ); + if (request) { + await finishOwnedResponse(f, request); + await f.lifecycle.beforeTool(ownershipNativeCall(f)); + expect(f.autoDrive.captureAuthority(sessionID)).toEqual( + next.authority, + ); + } else await next.verify(); + } finally { + f.releaseContextRead(); + await f.lifecycle.dispose(); + } + }, + ); +for (const actor of ["command", "synthetic"] as const) + test( + actor + " old initial session read rejection preserves fresh work", + async () => { + const f = await fixture(); + try { + await initialOwnershipWork(f); + f.blockNextSessionRead(); + f.rejectNextSessionRead(); + const pending = + actor === "command" + ? f.command("flow-auto", "Inspect old dispatch") + : f.lifecycle.prompt( + sessionID, + "Old synthetic", + { + agent, + model: { providerID: model.providerID, modelID: model.id }, + }, + { "opencode-plugin-flow/auto": "local-token" }, + ); + const caught = pending.then( + () => null, + (error) => String(error), + ); + await f.flush(); + const next = await freshOwnershipWork(f); + f.releaseSessionRead(); + expect(await caught).toBe("Error: Native session snapshot failed."); + await next.verify(); + } finally { + f.releaseSessionRead(); + await f.lifecycle.dispose(); + } + }, + ); +for (const actor of ["command", "synthetic"] as const) + test(actor + " old pending admission cannot cancel replacement", async () => { + const f = await fixture(); + try { + await initialOwnershipWork(f); + f.blockAdmission(); + const pending = + actor === "command" + ? f.command("flow-auto", "Inspect old dispatch") + : f.lifecycle.prompt( + sessionID, + "Old synthetic", + { + agent, + model: { providerID: model.providerID, modelID: model.id }, + }, + { "opencode-plugin-flow/auto": "local-token" }, + ); + const caught = pending.then( + () => null, + (error) => String(error), + ); + await f.flush(); + f.allowNewAdmissions(); + const next = await freshOwnershipWork(f); + f.releaseAdmission(); + expect(await caught).toBe( + actor === "command" + ? "Error: Flow execution epoch was revoked." + : "Error: Flow execution epoch was revoked.", + ); + await next.verify(); + } finally { + f.releaseAdmission(); + await f.lifecycle.dispose(); + } + }); +for (const outcome of ["failed", "succeeded"] as const) + test( + "old " + + outcome + + " event read rejection does not become global stream loss", + async () => { + const f = await fixture(); + try { + await initialOwnershipWork(f); + f.session.outcome = outcome; + f.session.time.idle = 4; + if (outcome === "failed") { + f.native.push({ + type: "idle", + id: "msg_failed", + outcome, + time: { created: 4 }, + }); + f.blockNextContextRead(); + f.rejectNextContextRead(); + } else { + f.blockNextSessionRead(); + f.rejectNextSessionRead(); + } + const event: OpenCodeEvent = + outcome === "failed" + ? { + id: "evt_failed", + created: 4, + type: "session.execution.failed", + durable: { aggregateID: sessionID, seq: 1, version: 1 }, + data: { + sessionID, + error: { type: "provider.transport", message: "refused" }, + }, + } + : { + id: "evt_succeeded", + created: 4, + type: "session.execution.succeeded", + durable: { aggregateID: sessionID, seq: 1, version: 1 }, + data: { sessionID }, + }; + f.sendEvent(event); + await f.flush(); + const next = await freshOwnershipWork(f); + f.releaseContextRead(); + f.releaseSessionRead(); + await f.flush(); + await next.verify(); + expect(f.logs.includes("Flow event stream failed closed.")).toBe(false); + } finally { + f.releaseContextRead(); + f.releaseSessionRead(); + await f.lifecycle.dispose(); + } + }, + ); +test("stale accepted-message observation cannot overwrite replacement recovery direction", async () => { + const f = await fixture(); + const entered = Promise.withResolvers(); + const gate = Promise.withResolvers(); + try { + const observe = f.autoDrive.observeMessage.bind(f.autoDrive); + let first = true; + f.autoDrive.observeMessage = async (...args) => { + const result = await observe(...args); + if (first) { + first = false; + entered.resolve(); + await gate.promise; + } + return result; + }; + const pending = f.command("flow-auto", "Inspect original"); + const caught = pending.then( + () => null, + (error) => String(error), + ); + await entered.promise; + const next = await freshOwnershipWork(f); + const request = await startOwnedRequest(f); + gate.resolve(); + expect(await caught).toBe("Error: Flow execution epoch was revoked."); + await finishOwnedResponse(f, request); + await f.lifecycle.beforeTool(ownershipNativeCall(f)); + expect(f.autoDrive.captureAuthority(sessionID)).toEqual(next.authority); + } finally { + gate.resolve(); + await f.lifecycle.dispose(); + } +}); + +test("old bound call cannot revoke a later request in the same epoch", async () => { + const f = await fixture(); + try { + await initialOwnershipWork(f); + const call = ownershipNativeCall(f); + await f.lifecycle.beforeTool(call); + const authority = f.autoDrive.captureAuthority(sessionID); + await finishOwnedResponse(f, await startOwnedRequest(f)); + await expect(f.lifecycle.beforeTool(call)).rejects.toThrow( + "Flow assistant cannot be rebound to another request.", + ); + await f.lifecycle.beforeTool(ownershipNativeCall(f)); + expect(f.autoDrive.captureAuthority(sessionID)).toEqual(authority); + } finally { + await f.lifecycle.dispose(); + } +}); +test("late duplicate request hook cannot end its replacement", async () => { + const f = await fixture(); + try { + await initialOwnershipWork(f); + const old = await startOwnedRequest(f); + await finishOwnedResponse(f, old); + const next = await freshOwnershipWork(f); + await f.emit("context", f.context()); + await expect( + f.emit("http.request", { + sessionID, + agent, + model, + kind: "primary", + request: old, + }), + ).rejects.toThrow("unique frozen context"); + const request = new Request("http://localhost/new"); + await f.emit("http.request", { + sessionID, + agent, + model, + kind: "primary", + request, + }); + await finishOwnedResponse(f, request); + await f.lifecycle.beforeTool(ownershipNativeCall(f)); + expect(f.autoDrive.captureAuthority(sessionID)).toEqual(next.authority); + } finally { + await f.lifecycle.dispose(); + } +}); +test("old WebSocket veto cannot revoke freshly admitted HTTP work", async () => { + const f = await fixture(); + try { + await initialOwnershipWork(f); + const next = await freshOwnershipWork(f); + await expect( + f.emit("experimental.ws.handshake", { + sessionID, + agent, + model, + kind: "primary", + url: "ws://localhost/unused", + headers: {}, + }), + ).rejects.toThrow("not been proved for WebSocket"); + await next.verify(); + } finally { + await f.lifecycle.dispose(); + } +}); +test("stale automatic shell remains vetoed after a manual replacement context", async () => { + const f = await fixture(); + try { + await initialOwnershipWork(f); + const old = ownershipNativeCall(f, "shell"); + for (const callback of f.toolCallbacks["execute.before"]) + await callback({ + ...old, + tool: "shell", + input: { command: "printf not-run" }, + }); + await f.command("flow-auto", "stop"); + const messageID = SessionMessage.ID.create(); + const text = "Inspect manual direction"; + await f.emit("prompt", { + sessionID, + messageID, + prompt: { text }, + delivery: "steer", + }); + f.native.push({ type: "user", id: messageID, text, time: { created: 5 } }); + await finishOwnedResponse(f, await startOwnedRequest(f)); + expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); + let effects = 0; + const pending = (async () => { + for (const callback of f.toolCallbacks["execute.before"]) + await callback({ + ...old, + tool: "shell", + input: { command: "printf not-run" }, + }); + effects++; + })(); + await expect(pending).rejects.toThrow("epoch was revoked"); + expect(effects).toBe(0); + await f.lifecycle.beforeTool(ownershipNativeCall(f)); + } finally { + await f.lifecycle.dispose(); + } +}); + +test("an old success notification cannot advance a newly admitted command before its first primary", async () => { + const f = await fixture(); + try { + await f.command("flow-auto", "Inspect fresh direction"); + const authority = f.autoDrive.captureAuthority(sessionID); + f.session.outcome = "succeeded"; + f.session.time.idle = 1; + f.sendEvent({ + id: "evt_before_primary", + type: "session.execution.succeeded", + created: 1, + durable: { aggregateID: sessionID, seq: 1, version: 1 }, + data: { sessionID }, + }); + await f.flush(); + expect(f.native.filter((message) => message.type === "synthetic")).toEqual( + [], + ); + expect(f.repository.saveCount).toBe(0); + await finishOwnedResponse(f, await startOwnedRequest(f)); + await f.lifecycle.beforeTool(ownershipNativeCall(f)); + expect(f.autoDrive.captureAuthority(sessionID)).toEqual(authority); + } finally { + await f.lifecycle.dispose(); + } +}); + +for (const prior of ["none", "manual", "automatic"] as const) + for (const admitted of ["command", "synthetic"] as const) + test( + "pre-primary automatic compaction from " + admitted + " after " + prior, + async () => { + const f = await fixture(); + try { + f.native.push({ + type: "user", + id: "msg_historical_input", + text: "Historical input grants no authority", + time: { created: 0 }, + }); + if (prior === "automatic") await initialOwnershipWork(f); + if (prior === "manual") { + await f.command("flow-plan", "Inspect earlier manual direction"); + await finishOwnedResponse(f, await startOwnedRequest(f)); + } + await f.command("flow-auto", "Inspect fresh first direction"); + if (admitted === "synthetic") + await f.lifecycle.prompt( + sessionID, + "Continue exact fresh direction", + delivery, + { + [FLOW_AUTO_METADATA_KEY]: "local-token", + }, + ); + const authority = f.autoDrive.captureAuthority(sessionID); + const compact = f.context(); + const control = { + type: "compaction" as const, + id: "msg_before_first_primary", + status: "running" as const, + reason: "auto" as const, + summary: "Summary grants no authority", + time: { created: 10 }, + }; + f.native.push(control); + await f.emit("compaction", compact); + f.native.splice(0, f.native.length, { + ...control, + status: "completed", + }); + await finishOwnedResponse(f, await startOwnedRequest(f)); + await f.lifecycle.beforeTool(ownershipNativeCall(f)); + expect(f.autoDrive.captureAuthority(sessionID)).toEqual(authority); + expect(f.repository.saveCount).toBe(0); + } finally { + await f.lifecycle.dispose(); + } + }, + ); + +for (const successor of ["command", "synthetic"] as const) + for (const delayed of [ + "before-entry", + "read-resumes", + "read-rejects", + ] as const) + test(`old compaction ${delayed} cannot own fresh ${successor}`, async () => { + const f = await fixture(); + try { + await initialOwnershipWork(f); + const compact = f.context(); + const control = { + type: "compaction" as const, + id: "msg_stale_before_context", + status: "running" as const, + reason: "auto" as const, + summary: "Old summary cannot borrow a fresh admission", + time: { created: 10 }, + }; + f.native.push(control); + if (delayed !== "before-entry") { + f.blockNextContextRead(); + if (delayed === "read-rejects") f.rejectNextContextRead(); + } + const pending = + delayed === "before-entry" ? null : f.emit("compaction", compact); + const caught = pending?.then( + () => null, + (error) => String(error), + ); + if (successor === "command") + await f.command("flow-auto", "Inspect fresh direction"); + else + await f.lifecycle.prompt( + sessionID, + "Fresh exact continuation", + delivery, + { + [FLOW_AUTO_METADATA_KEY]: "local-token", + }, + ); + const authority = f.autoDrive.captureAuthority(sessionID); + const request = await startOwnedRequest(f); + if (delayed === "before-entry") + await expect(f.emit("compaction", compact)).rejects.toThrow( + "epoch was revoked", + ); + else { + f.releaseContextRead(); + expect(await caught).toBe("Error: Flow execution epoch was revoked."); + } + f.native.splice(f.native.indexOf(control), 1); + await finishOwnedResponse(f, request); + await f.lifecycle.beforeTool(ownershipNativeCall(f)); + expect(f.autoDrive.captureAuthority(sessionID)).toEqual(authority); + expect(f.repository.saveCount).toBe(0); + } finally { + f.releaseContextRead(); + await f.lifecycle.dispose(); + } + }); + +for (const mismatch of [ + "prefix", + "model", + "payload", + "control-position", + "manual", + "revoked", +] as const) + test(`pre-primary compaction rejects ${mismatch}`, async () => { + const f = await fixture(); + try { + await f.command("flow-auto", "Inspect exact direction"); + let compact = f.context(); + const control = { + type: "compaction" as const, + id: "msg_untrusted_control", + status: "running" as const, + reason: mismatch === "manual" ? ("manual" as const) : ("auto" as const), + summary: "Summary grants no token", + time: { created: 10 }, + }; + f.native.push(control); + if (mismatch === "prefix") + compact.messages.unshift({ + role: "user", + id: SessionMessage.ID.make("msg_not_consumed"), + content: [], + }); + if (mismatch === "model") + compact = { + ...compact, + model: { ...model, id: Model.ID.make("other") }, + }; + if (mismatch === "payload") { + const input = f.native[0]; + if (input?.type !== "user") throw new Error("Expected native user"); + input.text = "Changed after admission"; + } + if (mismatch === "control-position") { + f.native.pop(); + f.native.unshift(control); + } + if (mismatch === "revoked") await f.command("flow-auto", "stop"); + await expect(f.emit("compaction", compact)).rejects.toThrow(); + f.native.splice(0, f.native.length, { + ...control, + reason: "auto", + status: "completed", + }); + await expect(f.emit("context", f.context())).rejects.toThrow(); + expect(f.repository.saveCount).toBe(0); + expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); + } finally { + await f.lifecycle.dispose(); + } + }); + +test("pre-primary compaction joins pending receipt without granting early authority", async () => { + const f = await fixture(); + try { + f.blockAdmission(); + const command = f.command("flow-auto", "Inspect exact first direction"); + await f.flush(); + const compact = f.context(); + const control = { + type: "compaction" as const, + id: "msg_pending_control", + summary: "Pending summary grants no authority", + status: "running" as const, + reason: "auto" as const, + time: { created: 10 }, + }; + f.native.push(control); + let completed = false; + const pending = f.emit("compaction", compact).then(() => { + completed = true; + }); + await f.flush(); + expect(completed).toBe(false); + expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); + expect(f.repository.saveCount).toBe(0); + f.releaseAdmission(); + await command; + await pending; + f.native.splice(0, f.native.length, { ...control, status: "completed" }); + await finishOwnedResponse(f, await startOwnedRequest(f)); + await f.lifecycle.beforeTool(ownershipNativeCall(f)); + expect(f.autoDrive.captureAuthority(sessionID)?.token).toBe("local-token"); + } finally { + f.releaseAdmission(); + await f.lifecycle.dispose(); + } +}); + +test("automatic compaction owns the accepted input during its existing primary", async () => { + const f = await fixture(); + try { + await f.command("flow-auto", "Inspect existing primary"); + const request = await startOwnedRequest(f); + const compact = f.context(); + const control = { + type: "compaction" as const, + id: "msg_during_primary", + status: "running" as const, + reason: "auto" as const, + summary: "summary", + time: { created: 5 }, + }; + f.native.push(control); + await f.emit("compaction", compact); + await finishOwnedResponse(f, request); + f.native.splice(0, f.native.length, { ...control, status: "completed" }); + await finishOwnedResponse(f, await startOwnedRequest(f)); + await f.lifecycle.beforeTool(ownershipNativeCall(f)); + expect(f.autoDrive.captureAuthority(sessionID)?.token).toBe("local-token"); + expect(f.repository.saveCount).toBe(0); + } finally { + await f.lifecycle.dispose(); + } +}); + +test("another automatic control can precede the first checkpoint primary", async () => { + const f = await nativeContinuationFixture("compaction"); + try { + const compact = f.context(); + const control = { + type: "compaction" as const, + id: "msg_second_pre_primary", + status: "running" as const, + reason: "auto" as const, + summary: "summary", + time: { created: 5 }, + }; + f.native.push(control); + await f.emit("compaction", compact); + f.native.splice(0, f.native.length, { ...control, status: "completed" }); + await finishOwnedResponse(f, await startOwnedRequest(f)); + await f.lifecycle.beforeTool(ownershipNativeCall(f)); + expect(f.autoDrive.captureAuthority(sessionID)?.token).toBe("local-token"); + } finally { + await f.lifecycle.dispose(); + } +}); + +test("real steer before compaction applies existing cancellation policy", async () => { + const f = await fixture(); + try { + await initialOwnershipWork(f); + const messageID = SessionMessage.ID.make( + "msg_real_steer_before_compaction", + ); + await f.emit("prompt", { + sessionID, + messageID, + prompt: { text: "/flow-auto stop" }, + delivery: "steer", + }); + f.native.push({ + type: "user", + id: messageID, + text: "/flow-auto stop", + time: { created: 5 }, + }); + const compact = f.context(); + const control = { + type: "compaction" as const, + id: "msg_after_real_steer", + status: "running" as const, + reason: "auto" as const, + summary: "summary", + time: { created: 6 }, + }; + f.native.push(control); + await f.emit("compaction", compact); + expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); + f.native.splice(0, f.native.length, { ...control, status: "completed" }); + await finishOwnedResponse(f, await startOwnedRequest(f)); + await f.lifecycle.beforeTool(ownershipNativeCall(f)); + expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); + expect(f.repository.saveCount).toBe(0); + } finally { + await f.lifecycle.dispose(); + } +}); + +test("unknown synthetic callbacks cannot borrow or deactivate the exact accepted source", async () => { + const f = await fixture(); + try { + await f.command("flow-auto", "Inspect exact direction"); + const authority = f.autoDrive.captureAuthority(sessionID); + f.native.push({ + type: "synthetic", + id: "msg_unadmitted", + text: "Not a private admission", + metadata: { [FLOW_AUTO_METADATA_KEY]: "local-token" }, + time: { created: 2 }, + }); + const compact = f.context(); + f.native.push({ + type: "compaction", + id: "msg_unadmitted_control", + status: "running", + reason: "auto", + summary: "summary", + time: { created: 3 }, + }); + await expect(f.emit("compaction", compact)).rejects.toThrow( + "epoch was revoked", + ); + expect(f.autoDrive.captureAuthority(sessionID)).toEqual(authority); + await expect(f.emit("context", compact)).rejects.toThrow( + "unknown, synthetic, or stale real input", + ); + expect(f.autoDrive.captureAuthority(sessionID)).toEqual(authority); + const foreignCall = ownershipNativeCall(f); + await expect(f.lifecycle.beforeTool(foreignCall)).rejects.toThrow(); + expect(f.repository.saveCount).toBe(0); + await f.command("flow-auto", "Inspect separately accepted fresh source"); + const fresh = await startOwnedRequest(f); + await finishOwnedResponse(f, fresh); + await expect(f.lifecycle.beforeTool(foreignCall)).rejects.toThrow( + "primary request", + ); + const call = ownershipNativeCall(f); + await f.lifecycle.beforeTool(call); + expect(f.autoDrive.captureAuthority(sessionID)?.token).toBe("local-token"); + expect(f.repository.saveCount).toBe(0); + } finally { + await f.lifecycle.dispose(); + } +}); + +for (const successor of ["command", "synthetic"] as const) + test(`old context entering after fresh ${successor} cannot revoke its request`, async () => { + const f = await fixture(); + try { + await initialOwnershipWork(f); + const old = f.context(); + if (successor === "command") + await f.command("flow-auto", "Inspect fresh context"); + else + await f.lifecycle.prompt( + sessionID, + "Fresh context continuation", + delivery, + { [FLOW_AUTO_METADATA_KEY]: "local-token" }, + ); + const authority = f.autoDrive.captureAuthority(sessionID); + const request = await startOwnedRequest(f); + await expect(f.emit("context", old)).rejects.toThrow(); + await finishOwnedResponse(f, request); + await f.lifecycle.beforeTool(ownershipNativeCall(f)); + expect(f.autoDrive.captureAuthority(sessionID)).toEqual(authority); + } finally { + await f.lifecycle.dispose(); + } + }); + +async function admitOrdinaryInput( + f: OwnershipFixture, + id: string, + text: string, +) { + await f.emit("prompt", { + sessionID, + messageID: SessionMessage.ID.make(id), + prompt: { text }, + delivery: "queue", + }); + f.native.push({ type: "user", id, text, time: { created: 10 } }); +} +function automaticControl(id: string) { + return { + type: "compaction" as const, + id, + status: "running" as const, + reason: "auto" as const, + summary: "Summary is neither a user nor an automatic grant", + time: { created: 11 }, + }; +} +async function compactCurrentInput( + f: OwnershipFixture, + id: string, + reason: "auto" | "manual" = "auto", +) { + const compact = f.context(); + const control = { ...automaticControl(id), reason }; + f.native.push(control); + await f.emit("compaction", compact); + f.native.splice(0, f.native.length, { ...control, status: "completed" }); +} +for (const prior of ["none", "ended-manual"] as const) + for (const attempt of ["first", "retry"] as const) + test(`ordinary ${prior} ${attempt} retains provenance without automatic authority through compaction`, async () => { + const f = await fixture(); + try { + if (prior === "ended-manual") { + await f.command("flow-plan", "Earlier ordinary manual plan"); + await finishOwnedResponse(f, await startOwnedRequest(f)); + } + await admitOrdinaryInput( + f, + "msg_grantless_real", + "Ordinary coding without Flow auto", + ); + await compactCurrentInput(f, "msg_grantless_checkpoint"); + await prepareContinuationRetry(f, attempt); + await finishOwnedResponse(f, await startOwnedRequest(f)); + await f.lifecycle.beforeTool(ownershipNativeCall(f)); + expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); + expect(f.repository.saveCount).toBe(0); + expect(f.recovery.snapshot(sessionID)).toEqual({ mode: "off" }); + } finally { + await f.lifecycle.dispose(); + } + }); + +test("grantless Flow plan input retains protected execution after compaction", async () => { + const f = await fixture(); + try { + await f.command("flow-plan", "Ordinary manual plan"); + await compactCurrentInput(f, "msg_plan_checkpoint"); + await finishOwnedResponse(f, await startOwnedRequest(f)); + await f.lifecycle.beforeTool(ownershipNativeCall(f)); + expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); + expect(f.repository.saveCount).toBe(0); + } finally { + await f.lifecycle.dispose(); + } +}); + +test("grantless reviewer input keeps its exact selected role through compaction", async () => { + const reviewer = Agent.ID.make("flow-reviewer"); + const f = await fixture(reviewer); + try { + f.session.parentID = "session-manager"; + await admitOrdinaryInput( + f, + "msg_reviewer_input", + "Review the assigned existing artifact", + ); + await compactCurrentInput(f, "msg_reviewer_checkpoint"); + await f.emit("context", f.context()); + const request = new Request("http://localhost/reviewer"); + await f.emit("http.request", { + sessionID, + agent: reviewer, + model, + kind: "primary", + request, + }); + const response = { + sessionID, + agent: reviewer, + model, + kind: "primary" as const, + request, + response: new Response("reviewer response"), + }; + await f.emit("http.response", response); + expect(await response.response.text()).toBe("reviewer response"); + const call = ownershipNativeCall(f); + const message = f.native.find((m) => m.id === call.messageID); + if (message?.type !== "assistant") + throw new Error("Missing native reviewer assistant"); + message.agent = reviewer; + await f.lifecycle.beforeTool({ ...call, agent: reviewer }); + expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); + expect(f.repository.saveCount).toBe(0); + } finally { + await f.lifecycle.dispose(); + } +}); + +for (const reject of [false, true]) + for (const oldSettlesFirst of [false, true]) + test(`same-epoch private source replaces stale pending context, reject ${reject}, old first ${oldSettlesFirst}`, async () => { + const f = await fixture(); + try { + await initialOwnershipWork(f); + const old = f.context(); + const releaseOld = f.blockNextContextRead(); + if (reject) f.rejectNextContextRead(); + const pending = f.emit("context", old).then( + () => null, + (error) => String(error), + ); + await f.flush(); + await f.lifecycle.prompt( + sessionID, + "Fresh private continuation", + delivery, + { [FLOW_AUTO_METADATA_KEY]: "local-token" }, + ); + const authority = f.autoDrive.captureAuthority(sessionID); + if (oldSettlesFirst) { + releaseOld(); + expect(await pending).toBe( + "Error: Flow execution epoch was revoked.", + ); + } + await finishOwnedResponse(f, await startOwnedRequest(f)); + await f.lifecycle.beforeTool(ownershipNativeCall(f)); + if (!oldSettlesFirst) { + releaseOld(); + expect(await pending).toBe( + "Error: Flow execution epoch was revoked.", + ); + } + expect(f.autoDrive.captureAuthority(sessionID)).toEqual(authority); + expect(f.repository.saveCount).toBe(0); + } finally { + f.releaseContextRead(); + await f.lifecycle.dispose(); + } + }); + +for (const reject of [false, true]) + test(`stale pending context finally cannot remove the fresh source slot, reject ${reject}`, async () => { + const f = await fixture(); + try { + await initialOwnershipWork(f); + const releaseOld = f.blockNextContextRead(); + if (reject) f.rejectNextContextRead(); + const old = f.emit("context", f.context()).then( + () => null, + (error) => String(error), + ); + await f.flush(); + await f.lifecycle.prompt( + sessionID, + "Fresh private continuation", + delivery, + { [FLOW_AUTO_METADATA_KEY]: "local-token" }, + ); + const releaseFresh = f.blockNextContextRead(); + const fresh = f.emit("context", f.context()).then( + () => null, + (error) => String(error), + ); + await f.flush(); + releaseOld(); + expect(await old).toBe("Error: Flow execution epoch was revoked."); + await expect(f.emit("context", f.context())).rejects.toThrow( + "context callbacks overlap", + ); + releaseFresh(); + expect(await fresh).toBe("Error: Flow execution epoch was revoked."); + expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); + expect(f.repository.saveCount).toBe(0); + } finally { + f.releaseContextRead(); + await f.lifecycle.dispose(); + } + }); + +for (const kind of [ + "unknown-marked", + "revoked-marked", + "manual-marked", +] as const) + test(`compaction cannot declassify ${kind} input into grantless provenance`, async () => { + const f = await fixture(); + try { + if (kind === "unknown-marked") { + const messageID = SessionMessage.ID.make("msg_unknown_marked_real"); + await expect( + f.emit("prompt", { + sessionID, + messageID, + prompt: { text: "Unknown token" }, + metadata: { [FLOW_AUTO_METADATA_KEY]: "unknown-token" }, + delivery: "queue", + }), + ).rejects.toThrow("private admitted dispatch"); + f.native.push({ + type: "user", + id: messageID, + text: "Unknown token", + metadata: { [FLOW_AUTO_METADATA_KEY]: "unknown-token" }, + time: { created: 10 }, + }); + } else await f.command("flow-auto", "Inspect the exact marked direction"); + const compact = f.context(); + if (kind === "revoked-marked") await f.command("flow-auto", "stop"); + const control = { + ...automaticControl("msg_not_declassified"), + reason: + kind === "manual-marked" ? ("manual" as const) : ("auto" as const), + }; + f.native.push(control); + await expect(f.emit("compaction", compact)).rejects.toThrow(); + f.native.splice(0, f.native.length, { ...control, status: "completed" }); + await expect(f.emit("context", f.context())).rejects.toThrow(); + expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); + expect(f.repository.saveCount).toBe(0); + } finally { + await f.lifecycle.dispose(); + } + }); + +for (const mismatch of [ + "prefix", + "completed-control", + "completed-reason", + "resumed-model", +] as const) + test(`grantless automatic compaction rejects ${mismatch} mismatch`, async () => { + const f = await fixture(); + try { + await admitOrdinaryInput( + f, + "msg_exact_ordinary_source", + "Ordinary selected coding", + ); + if (mismatch === "prefix") { + const compact = f.context(); + compact.messages.unshift({ + role: "user", + id: SessionMessage.ID.make("msg_not_consumed"), + content: [], + }); + f.native.push(automaticControl("msg_rejected_prefix_control")); + await expect(f.emit("compaction", compact)).rejects.toThrow( + "native control prefix", + ); + } else { + await compactCurrentInput(f, "msg_exact_ordinary_control"); + const control = f.native[0]; + if (control?.type !== "compaction" || control.status !== "completed") + throw new Error("Missing completed native control"); + if (mismatch === "completed-control") + control.id = "msg_replaced_control"; + if (mismatch === "completed-reason") control.reason = "manual"; + const context = f.context(); + await expect( + f.emit( + "context", + mismatch === "resumed-model" + ? { + ...context, + model: { ...model, id: Model.ID.make("changed") }, + } + : context, + ), + ).rejects.toThrow(); + } + expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); + expect(f.repository.saveCount).toBe(0); + } finally { + await f.lifecycle.dispose(); + } + }); + +for (const reason of ["auto", "manual"] as const) + test(`grantless ${reason} checkpoint retains an existing recovery origin without activating advice`, async () => { + const f = await fixture(); + try { + await f.command("flow-plan", 'Implement the test named "must pass"'); + const session = f.repository.session; + if (!session) + throw new Error("Explicit planning command did not anchor its test"); + const writes = f.repository.saveCount; + f.recovery.activate(sessionID); + await admitOrdinaryInput( + f, + "msg_manual_recovery_direction", + "Ordinary real user direction", + ); + await compactCurrentInput(f, "msg_manual_recovery_checkpoint", reason); + await finishOwnedResponse(f, await startOwnedRequest(f)); + const call = ownershipNativeCall(f); + await f.lifecycle.beforeTool(call); + const guard = f.recovery.guard({ + hostSessionId: sessionID, + messageId: call.messageID, + agent, + }); + expect(() => + guard.checkClose(session, { + operationId: "ordinary-close-check", + expectedRevision: session.revision, + sessionId: session.id, + kind: "completed", + summary: "Completed original real-user direction", + }), + ).not.toThrow(); + expect(() => + guard.checkClose(session, { + operationId: "ordinary-close-disposition", + expectedRevision: session.revision, + sessionId: session.id, + kind: "abandoned", + summary: "A checkpoint cannot authorize a new disposition", + }), + ).toThrow("fresh real user direction"); + expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); + expect(f.recovery.snapshot(sessionID).mode).toBe("off"); + expect(f.repository.saveCount).toBe(writes); + } finally { + await f.lifecycle.dispose(); + } + }); + +for (const attempt of ["first", "retry"] as const) + test(`known grantless manual checkpoint keeps exact ${attempt} provenance`, async () => { + const f = await fixture(); + try { + await admitOrdinaryInput( + f, + "msg_manual_known_user", + "Ordinary coding direction", + ); + await compactCurrentInput(f, "msg_manual_known_control", "manual"); + await prepareContinuationRetry(f, attempt); + await finishOwnedResponse(f, await startOwnedRequest(f)); + await f.lifecycle.beforeTool(ownershipNativeCall(f)); + expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); + expect(f.recovery.snapshot(sessionID)).toEqual({ mode: "off" }); + expect(f.repository.saveCount).toBe(0); + } finally { + await f.lifecycle.dispose(); + } + }); + +test("unattributed manual summary creates no checkpoint grant and permits a later real input", async () => { + const f = await fixture(); + try { + f.native.push({ + type: "user", + id: "msg_historical_unregistered_user", + text: "Historical ordinary input", + time: { created: 1 }, + }); + await compactCurrentInput(f, "msg_summary_only_manual_control", "manual"); + await expect(f.emit("context", f.context())).rejects.toThrow( + "unknown, synthetic, or stale real input", + ); + expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); + await admitOrdinaryInput( + f, + "msg_new_real_after_manual_summary", + "Fresh ordinary input", + ); + await finishOwnedResponse(f, await startOwnedRequest(f)); + await f.lifecycle.beforeTool(ownershipNativeCall(f)); + expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); + expect(f.repository.saveCount).toBe(0); + } finally { + await f.lifecycle.dispose(); + } +}); + +for (const stale of ["marked", "synthetic", "prefix"] as const) + test(`unattributed manual summary never grants from ${stale} history`, async () => { + const f = await fixture(); + try { + f.native.push( + stale === "synthetic" + ? { + type: "synthetic", + id: "msg_unregistered_synthetic", + text: "Unregistered", + time: { created: 1 }, + } + : { + type: "user", + id: "msg_unregistered_manual_source", + text: "Historical", + ...(stale === "marked" + ? { metadata: { [FLOW_AUTO_METADATA_KEY]: "stale-token" } } + : {}), + time: { created: 1 }, + }, + ); + const compact = f.context(); + if (stale === "prefix") + compact.messages.unshift({ + role: "user", + id: SessionMessage.ID.make("msg_missing_native_input"), + content: [], + }); + f.native.push({ + ...automaticControl("msg_rejected_manual_control"), + reason: "manual", + }); + if (stale === "prefix") + await expect(f.emit("compaction", compact)).rejects.toThrow(); + else { + await f.emit("compaction", compact); + const control = f.native.at(-1); + if (control?.type !== "compaction" || control.status !== "running") + throw new Error("Missing summary-only manual control"); + f.native.splice(0, f.native.length, { + ...control, + status: "completed", + }); + await expect(f.emit("context", f.context())).rejects.toThrow( + "unknown, synthetic, or stale real input", + ); + await expect( + f.lifecycle.beforeTool(ownershipNativeCall(f)), + ).rejects.toThrow("primary request"); + } + expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); + expect(f.repository.saveCount).toBe(0); + } finally { + await f.lifecycle.dispose(); + } + }); + +for (const fresh of ["automatic", "ordinary"] as const) + for (const reject of [false, true]) + test(`unattributed manual read ${reject} cannot revoke fresh ${fresh} work`, async () => { + const f = await fixture(); + try { + f.native.push({ + type: "user", + id: "msg_old_history_for_manual", + text: "Historical input", + time: { created: 1 }, + }); + const compact = f.context(); + const control = { + ...automaticControl("msg_old_unattributed_manual"), + reason: "manual" as const, + }; + f.native.push(control); + const release = f.blockNextContextRead(); + if (reject) f.rejectNextContextRead(); + const old = f.emit("compaction", compact).then( + () => null, + (error) => String(error), + ); + await f.flush(); + if (fresh === "automatic") + await f.command("flow-auto", "Inspect fresh exact direction"); + else + await admitOrdinaryInput( + f, + "msg_new_ordinary_source", + "Fresh real ordinary input", + ); + const authority = f.autoDrive.captureAuthority(sessionID); + const request = await startOwnedRequest(f); + release(); + expect(await old).toBe("Error: Flow execution epoch was revoked."); + f.native.splice(f.native.indexOf(control), 1); + await finishOwnedResponse(f, request); + await f.lifecycle.beforeTool(ownershipNativeCall(f)); + expect(f.autoDrive.captureAuthority(sessionID)).toEqual(authority); + expect(f.repository.saveCount).toBe(0); + } finally { + f.releaseContextRead(); + await f.lifecycle.dispose(); + } + }); + +test("known manual checkpoint completion cannot change its reason", async () => { + const f = await fixture(); + try { + await admitOrdinaryInput( + f, + "msg_reason_bound_manual_user", + "Ordinary input", + ); + await compactCurrentInput(f, "msg_reason_bound_manual_control", "manual"); + const control = f.native[0]; + if (control?.type !== "compaction" || control.status !== "completed") + throw new Error("Missing manual checkpoint"); + control.reason = "auto"; + await expect(f.emit("context", f.context())).rejects.toThrow( + "control identity changed", + ); + expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); + expect(f.repository.saveCount).toBe(0); + } finally { + await f.lifecycle.dispose(); + } +}); + +test("actual manager model observations remain bounded and refresh insertion order", async () => { + const f = await fixture(); + try { + for (let i = 0; i < 128; i++) + f.codingModels.set(`host-${i}`, { + providerID: "local", + modelID: "scripted", + }); + await admitOrdinaryInput( + f, + "msg_model_observation_source", + "Ordinary coding", + ); + await f.emit("context", f.context()); + expect(f.codingModels.size).toBe(128); + expect(f.codingModels.has("host-0")).toBe(false); + expect(f.codingModels.get(sessionID)).toEqual({ + providerID: "local", + modelID: "scripted", + }); + await finishContinuationContext(f); + f.codingModels.clear(); + f.codingModels.set(sessionID, { providerID: "local", modelID: "scripted" }); + for (let i = 1; i < 128; i++) + f.codingModels.set(`host-${i}`, { + providerID: "local", + modelID: "scripted", + }); + await f.emit("context", f.context()); + expect(f.codingModels.size).toBe(128); + expect(f.codingModels.keys().next().value).toBe("host-1"); + expect([...f.codingModels.keys()].at(-1)).toBe(sessionID); + } finally { + await f.lifecycle.dispose(); + } +}); + +test("unattributed manual summary cannot continue after lifecycle unload", async () => { + const f = await fixture(); + f.native.push({ + type: "user", + id: "msg_unloaded_manual_history", + text: "Historical input", + time: { created: 1 }, + }); + const compact = f.context(); + f.native.push({ + ...automaticControl("msg_unloaded_manual_control"), + reason: "manual", + }); + const release = f.blockNextContextRead(); + const old = f.emit("compaction", compact).then( + () => null, + (error) => String(error), + ); + await f.flush(); + await f.lifecycle.dispose(); + release(); + expect(await old).toBe("Error: Flow execution epoch was revoked."); + expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); + expect(f.repository.saveCount).toBe(0); +}); + +for (const fresh of ["automatic", "ordinary"] as const) + test(`old unattributed manual callback entering after fresh ${fresh} cannot cancel it`, async () => { + const f = await fixture(); + try { + f.native.push({ + type: "user", + id: "msg_history_before_delayed_manual", + text: "Historical input", + time: { created: 1 }, + }); + const old = f.context(); + const control = { + ...automaticControl("msg_late_unattributed_manual"), + reason: "manual" as const, + }; + f.native.push(control); + if (fresh === "automatic") + await f.command("flow-auto", "Inspect fresh callback source"); + else + await admitOrdinaryInput( + f, + "msg_fresh_delayed_manual_source", + "Fresh ordinary source", + ); + const authority = f.autoDrive.captureAuthority(sessionID); + const request = await startOwnedRequest(f); + await expect(f.emit("compaction", old)).rejects.toThrow(); + f.native.splice(f.native.indexOf(control), 1); + await finishOwnedResponse(f, request); + await f.lifecycle.beforeTool(ownershipNativeCall(f)); + expect(f.autoDrive.captureAuthority(sessionID)).toEqual(authority); + expect(f.repository.saveCount).toBe(0); + } finally { + await f.lifecycle.dispose(); + } + }); + +for (const actor of ["command", "synthetic"] as const) + test(`reservation before ${actor} session read vetoes old callbacks without taking new authority`, async () => { + const f = await fixture(); + try { + await initialOwnershipWork(f); + const old = f.context(); + f.blockNextSessionRead(); + const pending = + actor === "command" + ? f.command("flow-auto", "Inspect fresh pre-get command") + : f.lifecycle.prompt( + sessionID, + "Fresh private pre-get source", + delivery, + { [FLOW_AUTO_METADATA_KEY]: "local-token" }, + ); + await f.flush(); + const before = f.autoDrive.captureAuthority(sessionID); + await expect(f.emit("context", old)).rejects.toThrow("epoch was revoked"); + const control = automaticControl("msg_pre_get_old"); + f.native.push(control); + await expect(f.emit("compaction", old)).rejects.toThrow( + "epoch was revoked", + ); + expect(f.autoDrive.captureAuthority(sessionID)).toEqual(before); + expect(f.repository.saveCount).toBe(0); + f.native.splice(f.native.indexOf(control), 1); + f.releaseSessionRead(); + await pending; + await finishOwnedResponse(f, await startOwnedRequest(f)); + await f.lifecycle.beforeTool(ownershipNativeCall(f)); + expect(f.autoDrive.captureAuthority(sessionID)).not.toBeNull(); + } finally { + f.releaseSessionRead(); + await f.lifecycle.dispose(); + } + }); +test("accepted activation readiness blocks freeze and tools until exact source binding", async () => { + const f = await fixture(); + const entered = Promise.withResolvers(); + const release = Promise.withResolvers(); + const activate = f.autoDrive.activate.bind(f.autoDrive); + f.autoDrive.activate = async (...args) => { + const result = await activate(...args); + entered.resolve(); + await release.promise; + return result; + }; + const freeze = spyOn(ExecutionAttribution.prototype, "freeze"); + try { + const command = f.command( + "flow-auto", + "Inspect accepted pending activation", + ); + await entered.promise; + let settled = false; + const context = f.emit("context", f.context()).then( + () => { + settled = true; + return null; + }, + (error) => { + settled = true; + return error; + }, + ); + await f.flush(); + expect(settled).toBe(false); + expect(freeze).toHaveBeenCalledTimes(0); + expect(f.repository.saveCount).toBe(0); + await expect( + f.lifecycle.beforeTool(ownershipNativeCall(f)), + ).rejects.toThrow("primary request"); + expect(freeze).toHaveBeenCalledTimes(0); + release.resolve(); + await command; + expect(await context).toBeNull(); + expect(freeze).toHaveBeenCalledTimes(1); + const request = new Request("http://localhost/accepted-ready"); + await f.emit("http.request", { + sessionID, + agent, + model, + kind: "primary", + request, + }); + await finishOwnedResponse(f, request); + await f.lifecycle.beforeTool(ownershipNativeCall(f)); + } finally { + release.resolve(); + freeze.mockRestore(); + await f.lifecycle.dispose(); + } +}); +test("interrupted ordinary manual summaries retain lineage without an execution grant", async () => { + const f = await fixture(); + try { + await admitOrdinaryInput( + f, + "msg_interrupted_ordinary", + "Ordinary coding direction", + ); + const request = await startOwnedRequest(f); + const frame = { + sessionID, + agent, + model, + kind: "primary" as const, + request, + response: new Response( + new ReadableStream({ + start(controller) { + controller.enqueue(new Uint8Array([1])); + }, + }), + ), + }; + await f.emit("http.response", frame); + if (!frame.response.body) throw new Error("Missing response body"); + const reader = frame.response.body.getReader(); + await reader.read(); + await reader.cancel(); + for (const id of ["msg_interrupted_summary", "msg_repeated_summary"]) { + const input = f.context(); + const control = { ...automaticControl(id), reason: "manual" as const }; + f.native.push(control); + await f.emit("compaction", input); + expect(input.system).toEqual([]); + f.native.splice(0, f.native.length, { ...control, status: "completed" }); + await expect(f.emit("context", f.context())).rejects.toThrow( + "unknown, synthetic, or stale real input", + ); + await expect( + f.lifecycle.beforeTool(ownershipNativeCall(f)), + ).rejects.toThrow(); + expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); + expect(f.repository.saveCount).toBe(0); + } + await admitOrdinaryInput(f, "msg_separate_fresh", "Fresh ordinary request"); + await finishOwnedResponse(f, await startOwnedRequest(f)); + await f.lifecycle.beforeTool(ownershipNativeCall(f)); + expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); + expect(f.recovery.snapshot(sessionID)).toEqual({ mode: "off" }); + expect(f.repository.saveCount).toBe(0); + } finally { + await f.lifecycle.dispose(); + } +}); + +test("stream loss wakes an exact pending context without granting or settling its private receipt", async () => { + const f = await fixture(); + const freeze = spyOn(ExecutionAttribution.prototype, "freeze"); + try { + f.blockAdmission(); + const command = f + .command("flow-auto", "Held receipt during stream loss") + .catch((error) => String(error)); + await f.flush(); + let settled = false; + const context = f.emit("context", f.context()).then( + () => { + settled = true; + return null; + }, + (error) => { + settled = true; + return String(error); + }, + ); + await f.flush(); + expect(settled).toBe(false); + f.loseStream(); + await f.flush(); + expect(await context).toBe("Error: Flow execution epoch was revoked."); + expect(freeze).toHaveBeenCalledTimes(0); + expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); + expect(f.repository.saveCount).toBe(0); + f.releaseAdmission(); + expect(await command).toContain( + "unavailable after unload or event-stream loss", + ); + } finally { + f.releaseAdmission(); + freeze.mockRestore(); + await f.lifecycle.dispose(); + } +}); + +test("a cancelled same-epoch context releases only its own pre-dispatch frozen primary", async () => { + const f = await fixture(); + try { + await initialOwnershipWork(f); + f.blockNextSessionRead(); + const old = f.emit("context", f.context()).then( + () => null, + (error) => String(error), + ); + await f.waitSessionRead(); + await f.lifecycle.prompt( + sessionID, + "Fresh private source after old freeze", + delivery, + { [FLOW_AUTO_METADATA_KEY]: "local-token" }, + ); + expect(await old).toBe("Error: Flow execution epoch was revoked."); + const authority = f.autoDrive.captureAuthority(sessionID); + expect(authority).not.toBeNull(); + await finishOwnedResponse(f, await startOwnedRequest(f)); + const call = ownershipNativeCall(f); + await f.lifecycle.beforeTool(call); + f.releaseSessionRead(); + await f.flush(); + expect(f.autoDrive.captureAuthority(sessionID)).not.toBeNull(); + expect(f.autoDrive.captureAuthority(sessionID)).toEqual(authority); + await f.lifecycle.beforeTool(call); + } finally { + f.releaseSessionRead(); + await f.lifecycle.dispose(); + } +}); + +test("a source accepted while context waits owns a later native-read failure", async () => { + const f = await fixture(); + try { + f.blockAdmission(); + const command = f.command( + "flow-auto", + "Exact pending source before read failure", + ); + await f.flush(); + f.rejectNextContextRead(); + const context = f.emit("context", f.context()).then( + () => null, + (error) => String(error), + ); + await f.flush(); + expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); + f.releaseAdmission(); + await command; + expect(await context).toBe("Error: Native context snapshot failed."); + expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); + await f.command("flow-auto", "Fresh source after matched read failure"); + await finishOwnedResponse(f, await startOwnedRequest(f)); + await f.lifecycle.beforeTool(ownershipNativeCall(f)); + expect(f.autoDrive.captureAuthority(sessionID)).not.toBeNull(); + } finally { + f.releaseAdmission(); + await f.lifecycle.dispose(); + } +}); diff --git a/tests/live-opencode-smoke.test.ts b/tests/live-opencode-smoke.test.ts index 8567b457..4bc81e35 100644 --- a/tests/live-opencode-smoke.test.ts +++ b/tests/live-opencode-smoke.test.ts @@ -1,52 +1,65 @@ -import { describe, expect, spyOn, test } from "bun:test"; -import { spawn } from "node:child_process"; +import { expect, test } from "bun:test"; +import { type ChildProcess, spawn, spawnSync } from "node:child_process"; +import { createHash } from "node:crypto"; import { + cp, lstat, mkdir, mkdtemp, + readdir, readFile, + realpath, rm, writeFile, } from "node:fs/promises"; import { createServer } from "node:net"; import { tmpdir } from "node:os"; import { join } from "node:path"; +import { pathToFileURL } from "node:url"; +import type { + OpenCodeClient, + SessionMessageAssistantTool, +} from "@opencode/client"; +import { currentBunToolchain } from "../evals/bun-toolchain.js"; import { - currentBunToolchain, - runPinnedBunSync, -} from "../evals/bun-toolchain.js"; -import { packPlugin, terminateChildProcessTree } from "../evals/harness.js"; -import packageJson from "../package.json" with { type: "json" }; + packPlugin, + preparePackageCache, + terminateChildProcessTree, +} from "../evals/harness.js"; +import { + extractObservedModelIdentity, + isRecord, +} from "../evals/host-observation.js"; import { - buildHostEvidenceCapabilities, - collectFieldObservations, - type EndpointAttempt, - HOST_METADATA_CONTRACT, -} from "../scripts/probe-opencode-eval-metadata.js"; + installedOpenCodePluginEntry, + listNativeMessages, + listNativeSessions, + OPENCODE_HOST_VERSION, + OpenCodeStartup, + openCodeInvocation, +} from "../evals/opencode-host.js"; +import { packedPackageManifest, tarballSha256 } from "../evals/provenance.js"; +import packageJson from "../package.json" with { type: "json" }; import { FLOW_CORE_AGENTS } from "../src/config-shared.js"; +import { runPackedReviewerReturn } from "./fixtures/opencode-v2-reviewer-return.js"; +import { localScriptedProvider } from "./fixtures/opencode-v2-scripted-provider.js"; +import { proveNativeUsage } from "./fixtures/opencode-v2-usage-proof.js"; +import { + junitFixtureFiles, + packedValidationPlan, + runPackedValidationCases, +} from "./fixtures/opencode-v2-validation-cases.js"; -// This test deliberately proves only the host boundary. Domain and persistence -// behavior belongs in fast deterministic tests; the live smoke verifies that a -// packed release loads in a real OpenCode process with the intended commands -// and hidden-agent isolation. Actual model-driven fan-out is deliberately not -// part of this smoke: it would require provider credentials and would make a -// deterministic package/host compatibility gate depend on model behavior. const LIVE = process.env.FLOW_LIVE_SMOKE === "1"; -const PINNED_OPENCODE_VERSION = - packageJson.devDependencies["@opencode-ai/plugin"]; -const OPENCODE_VERSION = - process.env.FLOW_OPENCODE_SMOKE_VERSION?.trim() || PINNED_OPENCODE_VERSION; -const STARTUP_TIMEOUT_MS = 180_000; -const REQUEST_TIMEOUT_MS = 120_000; - +const VERSION = + process.env.FLOW_OPENCODE_SMOKE_VERSION?.trim() || OPENCODE_HOST_VERSION; const EXPECTED_COMMANDS = [ "flow-auto", "flow-plan", "flow-review", "flow-run", "flow-status", -] as const; - +]; const EXPECTED_TOOLS = [ "flow_feature_complete", "flow_feature_reset", @@ -59,104 +72,7 @@ const EXPECTED_TOOLS = [ "flow_session_close", "flow_status", "flow_validation_start", -] as const; - -const EXPECTED_AGENTS = ["flow-reviewer", "flow-worker"] as const; - -type PermissionRule = { - permission: string; - pattern: string; - action: "ask" | "allow" | "deny"; -}; - -type ResolvedAgent = { - name: string; - mode?: "subagent" | "primary" | "all"; - model?: { providerID: string; modelID: string }; - variant?: string; - steps?: number; - permission?: PermissionRule[]; -}; - -function wildcardMatches(input: string, pattern: string): boolean { - const expression = pattern - .replace(/[.+^${}()|[\]\\]/g, "\\$&") - .replaceAll("*", ".*") - .replaceAll("?", "."); - return new RegExp(`^${expression}$`, "s").test(input); -} - -function permissionFor( - rules: readonly PermissionRule[], - permission: string, - pattern = "*", -): PermissionRule["action"] { - for (let index = rules.length - 1; index >= 0; index -= 1) { - const rule = rules[index]; - if ( - rule && - wildcardMatches(permission, rule.permission) && - wildcardMatches(pattern, rule.pattern) - ) { - return rule.action; - } - } - return "ask"; -} - -async function fetchJson(url: string, timeout = REQUEST_TIMEOUT_MS) { - let response: Response | undefined; - try { - response = await fetch(url, { signal: AbortSignal.timeout(timeout) }); - if (!response.ok) throw new Error(); - return await response.json(); - } catch (error) { - // Neither response bodies nor native error messages are safe diagnostics. - const reason = - response && !response.ok - ? `HTTP ${response.status}` - : error instanceof Error && - ["TimeoutError", "AbortError", "SyntaxError", "TypeError"].includes( - error.name, - ) - ? error.name - : "Error"; - throw new Error(`GET ${new URL(url).pathname} failed (${reason}).`); - } -} - -async function waitForCommands( - baseUrl: string, - deadline: number, -): Promise> { - for (;;) { - const beforeProbe = deadline - Date.now(); - if (beforeProbe <= 0) - throw new Error("OpenCode startup deadline expired at /global/health."); - let healthy = false; - try { - const health = (await fetchJson( - `${baseUrl}/global/health`, - Math.min(3_000, beforeProbe), - )) as { - healthy?: boolean; - }; - healthy = health.healthy === true; - } catch { - // The process is still starting. - } - const remaining = deadline - Date.now(); - if (healthy) { - if (remaining <= 0) - throw new Error("OpenCode startup deadline expired before /command."); - // Global health does not wait for project/plugin initialization. - return (await fetchJson(`${baseUrl}/command`, remaining)) as Array<{ - name: string; - }>; - } - if (remaining > 0) await Bun.sleep(Math.min(500, remaining)); - } -} +]; async function availablePort(): Promise { const server = createServer(); @@ -165,764 +81,760 @@ async function availablePort(): Promise { server.listen(0, "127.0.0.1", resolve); }); const address = server.address(); - if (!address || typeof address === "string") { - throw new Error("Could not reserve a local port."); - } + if (!address || typeof address === "string") + throw new Error("No loopback port"); await new Promise((resolve, reject) => server.close((error) => (error ? reject(error) : resolve())), ); return address.port; } -describe("live smoke readiness deadlines", () => { - for (const mode of [ - "ready", - "late-health", - "unhealthy", - "command-timeout", - ] as const) { - test(`R19-01 shares one startup budget: ${mode}`, async () => { - let now = mode === "unhealthy" ? 179_750 : 0; - const budgets: number[] = []; - const clock = spyOn(Date, "now").mockImplementation(() => now); - const timeout = AbortSignal.timeout; - const deadlines = spyOn(AbortSignal, "timeout").mockImplementation( - (ms) => { - budgets.push(ms); - return timeout(ms); - }, - ); - const sleep = spyOn(Bun, "sleep").mockImplementation(async (ms) => { - if (typeof ms !== "number") throw new Error("Expected numeric delay"); - now += ms; - }); - const requests = spyOn(globalThis, "fetch").mockImplementation( - Object.assign( - async (input: Parameters[0]) => { - if (String(input).endsWith("/global/health")) { - if (mode === "unhealthy") - return Response.json({ healthy: false }); - now = - mode === "late-health" - ? 180_000 - : mode === "command-timeout" - ? 40_000 - : 10_000; - return Response.json({ healthy: true }); - } - if (mode === "command-timeout") - throw new DOMException("private diagnostic", "TimeoutError"); - now = 155_000; - return Response.json([{ name: "flow-status" }]); - }, - { preconnect: fetch.preconnect }, - ), - ); - try { - const ready = waitForCommands("http://127.0.0.1:1234", 180_000); - if (mode === "ready") { - expect(await ready).toEqual([{ name: "flow-status" }]); - await fetchJson("http://127.0.0.1:1234/agent"); - expect(budgets).toEqual([3_000, 170_000, 120_000]); - expect(requests).toHaveBeenCalledTimes(3); - } else if (mode === "command-timeout") { - await expect(ready).rejects.toThrow( - "GET /command failed (TimeoutError).", - ); - expect(budgets).toEqual([3_000, 140_000]); - expect(requests).toHaveBeenCalledTimes(2); - } else { - await expect(ready).rejects.toThrow( - mode === "late-health" ? "before /command" : "at /global/health", - ); - expect(budgets).toEqual([mode === "late-health" ? 3_000 : 250]); - expect(requests).toHaveBeenCalledTimes(1); - if (mode === "unhealthy") expect(sleep).toHaveBeenCalledWith(250); - } - } finally { - requests.mockRestore(); - sleep.mockRestore(); - deadlines.mockRestore(); - clock.mockRestore(); - } - }); - } - - for (const kind of ["http", "json", "network"] as const) { - test(`R19-01 identifies ${kind} failure without exposing response/server text`, async () => { - const requests = spyOn(globalThis, "fetch").mockImplementation( - Object.assign( - async () => { - if (kind === "network") throw new TypeError("private diagnostic"); - return new Response("private diagnostic", { - status: kind === "http" ? 503 : 200, - }); - }, - { preconnect: fetch.preconnect }, - ), - ); - try { - const expected = - kind === "http" - ? "HTTP 503" - : kind === "json" - ? "SyntaxError" - : "TypeError"; - await expect( - fetchJson("http://127.0.0.1:1234/command?token=private"), - ).rejects.toThrow(`GET /command failed (${expected}).`); - expect(requests).toHaveBeenCalledTimes(1); - } finally { - requests.mockRestore(); - } - }); - } -}); - -function observed( - endpoint: EndpointAttempt["endpoint"], - response: unknown, -): EndpointAttempt { - return { kind: "observed", endpoint, response }; -} - -function createdSession(version = PINNED_OPENCODE_VERSION): EndpointAttempt { - return observed(HOST_METADATA_CONTRACT.endpoints.createSession, { - id: "session", - version, - }); -} - -function answeredModel(modelID = "model", providerID = "provider") { - return { - info: { - role: "assistant", - time: { created: 1, completed: 2 }, - model: { providerID, modelID }, - }, - }; -} - -function reviewerChild(id = "child", parentID = "parent") { - return { agent: "flow-reviewer", id, parentID }; -} - -type EvidenceInput = Parameters[0]; - -function evidence(overrides: Partial = {}) { - return buildHostEvidenceCapabilities({ - opencodeVersion: PINNED_OPENCODE_VERSION, - generatedAt: "2026-08-24T00:00:00.000Z", - parentSessionId: "parent", - endpointResponses: [createdSession()], - childSessions: observed(HOST_METADATA_CONTRACT.endpoints.childSessions, [ - reviewerChild(), - ]), - parentMessages: observed(HOST_METADATA_CONTRACT.endpoints.parentMessages, [ - answeredModel("manager"), - ]), - childMessages: [ - { - sessionId: "child", - messages: observed(HOST_METADATA_CONTRACT.endpoints.childMessages, [ - answeredModel("reviewer"), - ]), - }, - ], - ...overrides, - }); +async function tools( + client: OpenCodeClient, + sessionID: string, +): Promise { + return (await listNativeMessages(client, sessionID)).flatMap((message) => + message.type === "assistant" + ? message.content.filter((content) => content.type === "tool") + : [], + ); } -describe("OpenCode eval metadata probe", () => { - test("pins the current host, endpoints, and reviewer bound", () => { - expect(HOST_METADATA_CONTRACT.hostVersion).toBe("1.18.31"); - expect(HOST_METADATA_CONTRACT.endpoints).toEqual({ - agents: "GET /agent", - createSession: "POST /session", - dispatchReview: "POST /session/:id/command", - parentMessages: "GET /session/:id/message", - childSessions: "GET /session/:id/children", - childMessages: "GET /session/:child_id/message", - }); - expect(HOST_METADATA_CONTRACT.limits).toEqual({ - requestTimeoutMs: 120_000, - reviewerSteps: 8, - }); +async function verifyInstalledCandidate( + tarball: string, + installed: string, + root: string, +): Promise { + const expected = join(root, "expected-package"); + await mkdir(expected); + const extracted = spawnSync("tar", ["-xzf", tarball, "-C", expected], { + encoding: "utf8", }); - - test("redacts compound sensitive keys but keeps identity field labels", () => { - const fields = collectFieldObservations({ - info: { - model: { providerID: "provider-secret", modelID: "model-secret" }, - }, - parts: [ - { - apiKey: "secret", - accessToken: "secret", - promptText: "secret", - toolOutput: "secret", - credential: "secret", - }, - ], - }); - expect(fields).toEqual([ - { path: "info", kind: "object" }, - { path: "info.model", kind: "object" }, - { path: "info.model.modelID", kind: "string" }, - { path: "info.model.providerID", kind: "string" }, - { path: "parts", kind: "array" }, - { path: "parts[]", kind: "object" }, - ]); - }); - - test("records completed parent and reviewer identities and raw parent linkage as labels", () => { - const capabilities = evidence({ - parentSessionId: "ses_parent_raw", - childSessions: observed(HOST_METADATA_CONTRACT.endpoints.childSessions, [ - reviewerChild("ses_child_raw", "ses_parent_raw"), - ]), - parentMessages: observed( - HOST_METADATA_CONTRACT.endpoints.parentMessages, - [answeredModel("manager-secret", "provider-secret")], - ), - childMessages: [ - { - sessionId: "ses_child_raw", - messages: observed(HOST_METADATA_CONTRACT.endpoints.childMessages, [ - answeredModel("reviewer-secret", "provider-secret"), - ]), - }, - ], - }); - expect(capabilities.capabilities).toMatchObject({ - hostVersion: { - kind: "observed", - matchesRequested: true, - fieldPath: "version", - }, - parentManagerModelIdentity: { - kind: "observed", - actors: [ - { - actor: "parent-1", - fieldPaths: ["[].info.model.modelID", "[].info.model.providerID"], - }, - ], - }, - childReviewerModelIdentity: { - kind: "observed", - actors: [ - { - actor: "child-1", - fieldPaths: ["[].info.model.modelID", "[].info.model.providerID"], - }, - ], - }, - childLineage: { - kind: "observed", - links: [ - { - parent: "parent-1", - child: "child-1", - fieldPaths: ["[].id", "[].parentID"], - }, - ], - }, - }); - expect(capabilities).toMatchObject({ - unsupportedClaims: [], - result: { kind: "complete" }, - }); - expect(JSON.stringify(capabilities)).not.toMatch( - /ses_(parent|child)_raw|provider-secret|reviewer-secret/, + if (extracted.status !== 0) + throw new Error("Could not inspect candidate archive"); + const packageRoot = join(expected, "package"); + const files = (await readdir(packageRoot, { recursive: true })).sort(); + for (const file of files) { + const entry = await lstat(join(packageRoot, file)); + if (entry.isDirectory()) continue; + if (!entry.isFile()) + throw new Error("Candidate contains an unsupported non-file entry"); + expect(await readFile(join(installed, file))).toEqual( + await readFile(join(packageRoot, file)), ); - }); - - for (const scenario of [ - { - name: "host-version-mismatch", - overrides: { endpointResponses: [createdSession("9.9.9")] }, - result: "host-version-mismatch", - }, - { - name: "parent-model-field-unavailable", - overrides: { - parentMessages: observed( - HOST_METADATA_CONTRACT.endpoints.parentMessages, - [{ info: { role: "assistant", time: { completed: 2 } } }], - ), - }, - result: "required-capability-unavailable", - unsupported: ["parent-manager-model-identity"], - }, - { - name: "child-model-field-unavailable", - overrides: { - childMessages: [ - { - sessionId: "child", - messages: observed(HOST_METADATA_CONTRACT.endpoints.childMessages, [ - { info: { role: "assistant", time: { completed: 2 } } }, - ]), - }, - ], - }, - result: "required-capability-unavailable", - unsupported: ["child-reviewer-model-identity"], - }, - { - name: "endpoint-failure", - overrides: { - childSessions: { - kind: "endpoint-failure" as const, - endpoint: HOST_METADATA_CONTRACT.endpoints.childSessions, - }, - parentMessages: { - kind: "endpoint-failure" as const, - endpoint: HOST_METADATA_CONTRACT.endpoints.parentMessages, - }, - childMessages: [], - }, - result: "endpoint-failure", - unsupported: [], - }, - { - name: "review-command-failure", - overrides: { - endpointResponses: [ - createdSession(), - { - kind: "endpoint-failure" as const, - endpoint: HOST_METADATA_CONTRACT.endpoints.dispatchReview, - }, - ], - }, - result: "endpoint-failure", - unsupported: [], - }, - { - name: "mismatched-reviewer-parent", - overrides: { - childSessions: observed( - HOST_METADATA_CONTRACT.endpoints.childSessions, - [reviewerChild("child", "different-parent")], - ), - childMessages: [], - }, - result: "reviewer-child-not-observed", - unsupported: ["child-reviewer-model-identity", "child-session-lineage"], - lineage: "parent-mismatch", - }, - { - name: "non-reviewer-child", - overrides: { - childSessions: observed( - HOST_METADATA_CONTRACT.endpoints.childSessions, - [{ agent: "flow-worker", id: "child", parentID: "parent" }], - ), - childMessages: [], - }, - result: "reviewer-child-not-observed", - unsupported: [], - lineage: "reviewer-child-not-observed", - }, - { - name: "unanswered-reviewer", - overrides: { - childMessages: [ - { - sessionId: "child", - messages: observed(HOST_METADATA_CONTRACT.endpoints.childMessages, [ - { - info: { - role: "user", - model: { providerID: "provider", modelID: "reviewer" }, - }, - }, - ]), - }, - ], - }, - result: "model-did-not-answer", - }, - { - name: "failed-parent-message", - overrides: { - parentMessages: observed( - HOST_METADATA_CONTRACT.endpoints.parentMessages, - [ - { - info: { - error: { name: "ProviderError" }, - model: { providerID: "provider", modelID: "manager" }, - }, - }, - ], - ), - }, - result: "model-did-not-answer", - }, - ] as const) { - test(`reports ${scenario.name}`, () => { - const capabilities = evidence(scenario.overrides); - expect(capabilities.result).toEqual({ - kind: "inconclusive", - reason: scenario.result, - }); - if (scenario.unsupported) - expect(capabilities.unsupportedClaims).toEqual(scenario.unsupported); - if (scenario.lineage) - expect(capabilities.capabilities.childLineage).toMatchObject({ - kind: "unobserved", - reason: scenario.lineage, - }); - }); } -}); + const installedFiles = (await readdir(installed, { recursive: true })) + .filter( + (path) => path !== "node_modules" && !path.startsWith("node_modules/"), + ) + .sort(); + expect(installedFiles).toEqual(files); +} -describe("live OpenCode smoke configuration", () => { - test("uses the pinned host unless compatibility monitoring overrides it", () => { - expect(PINNED_OPENCODE_VERSION).toMatch(/^\d+\.\d+\.\d+$/); - expect( - process.env.FLOW_OPENCODE_SMOKE_VERSION?.trim() - ? OPENCODE_VERSION - : PINNED_OPENCODE_VERSION, - ).toBe(OPENCODE_VERSION); - expect(EXPECTED_COMMANDS).toHaveLength(5); - expect(EXPECTED_TOOLS).toHaveLength(11); - expect(EXPECTED_AGENTS).toHaveLength(2); - }); +const save = { + request: { + operationId: "smoke-save", + expectedRevision: 0, + goal: "Verify the installed V2 package", + plan: packedValidationPlan, + }, +}; + +test("reserved native permissions retain the planner boundary", () => { + const rules = FLOW_CORE_AGENTS["flow-planner"].permissions; + const effect = (action: string) => + rules.filter((rule) => rule.action === "*" || rule.action === action).at(-1) + ?.effect; + expect( + [ + "shell", + "subagent", + "flow_plan_save", + "flow_plan_approve", + "flow_run_start", + "flow_review_start", + "flow_feature_complete", + ].map(effect), + ).toEqual(["deny", "deny", "deny", "deny", "deny", "deny", "deny"]); + expect(["read", "glob", "grep"].map(effect)).toEqual([ + "allow", + "allow", + "allow", + ]); }); -describe.skipIf(!LIVE)(`live OpenCode ${OPENCODE_VERSION} smoke`, () => { - test( - "loads the packed plugin and enforces the small public surface", - async () => { - const repositoryRoot = join(import.meta.dir, ".."); +test.skipIf(!LIVE)( + "packed candidate executes allowed and denied native custom calls with a local provider", + async () => { + const root = await mkdtemp(join(tmpdir(), "flow-v2-packed-smoke-")); + const project = join(root, "project"); + const receipt = join(root, "receipt.jsonl"); + const provider = localScriptedProvider(); + const startup = new OpenCodeStartup(); + let child: ChildProcess | undefined; + const validationEvidence: unknown[] = []; + try { const toolchain = currentBunToolchain(packageJson.packageManager); - const scratch = await mkdtemp(join(tmpdir(), "flow-live-smoke-")); - const childHome = join(scratch, "home"); - const childCache = join(scratch, "cache"); - const project = join(scratch, "project"); - await mkdir(childHome, { recursive: true }); - await mkdir(join(project, ".opencode"), { recursive: true }); - - const tarball = await packPlugin(repositoryRoot, scratch, toolchain); - expect((await lstat(tarball)).isFile()).toBe(true); - - // Populate OpenCode's exact-version package cache from the candidate - // tarball so the documented config exercises these exact package bytes. - const packageCache = join( - childCache, - "opencode", - "packages", - `opencode-plugin-flow@${packageJson.version}`, + const tarball = + process.env.FLOW_LIVE_SMOKE_TARBALL ?? + (await packPlugin(join(import.meta.dir, ".."), root, toolchain)); + const manifest = await packedPackageManifest(tarball); + expect(manifest.version).toBe(packageJson.version); + const archiveDigest = await tarballSha256(tarball); + const cache = + process.env.FLOW_LIVE_SMOKE_PACKAGE_CACHE ?? + (await preparePackageCache(tarball, root, toolchain)); + const installed = join(root, "installed"); + await cp(cache, installed, { recursive: true }); + const pluginDirectory = join( + installed, + "node_modules", + "opencode-plugin-flow", ); - await mkdir(packageCache, { recursive: true }); - await writeFile( - join(packageCache, "package.json"), - `${JSON.stringify({ dependencies: { "opencode-plugin-flow": `file:${tarball}` } }, null, 2)}\n`, - "utf8", + await verifyInstalledCandidate(tarball, pluginDirectory, root); + const installedManifest: unknown = JSON.parse( + await readFile(join(pluginDirectory, "package.json"), "utf8"), ); - const install = runPinnedBunSync(toolchain, ["install"], { - cwd: packageCache, + if (!isRecord(installedManifest)) + throw new Error("Invalid installed candidate manifest"); + expect(installedManifest.version).toBe(manifest.version); + expect(installedManifest.exports).toEqual({ + ".": { + types: "./dist/index.d.ts", + import: "./dist/index.js", + default: "./dist/index.js", + }, + "./tui": { types: "./dist/tui.d.ts", import: "./dist/tui.js" }, }); - expect(install.status, `${install.stdout}\n${install.stderr}`).toBe(0); + await mkdir(project); + await writeFile(join(project, "README.md"), "Local V2 smoke fixture\n"); await writeFile( - join(project, "opencode.json"), - `${JSON.stringify( + join(project, ".gitignore"), + "/.flow/\n/opencode.json\n/.opencode/\n", + ); + for (const [name, content] of Object.entries(junitFixtureFiles)) + await writeFile(join(project, name), content, { mode: 0o755 }); + + for (const args of [ + ["init", "--initial-branch=main"], + ["add", "README.md", ".gitignore", ...Object.keys(junitFixtureFiles)], + [ + "-c", + "user.name=Smoke", + "-c", + "user.email=smoke@example.invalid", + "-c", + "commit.gpgSign=false", + "commit", + "-m", + "fixture", + ], + ]) { + const result = spawnSync( + "git", + ["-c", "core.hooksPath=/dev/null", ...args], { - plugin: [ - [ - `opencode-plugin-flow@${packageJson.version}`, - { - reviewer: { - model: "flow-probe/model", - variant: "high", - steps: 80, - }, - }, - ], - ], + cwd: project, + encoding: "utf8", + env: { + PATH: toolchain.environment.PATH, + GIT_CONFIG_NOSYSTEM: "1", + GIT_CONFIG_GLOBAL: "/dev/null", + }, }, - null, - 2, - )}\n`, - "utf8", + ); + if (result.status !== 0) + throw new Error( + "Could not prepare isolated smoke fixture repository", + ); + } + const pluginEntry = await installedOpenCodePluginEntry( + pluginDirectory, + join(root, "flow-plugin-entry"), ); - - const globalConfig = join( - childHome, - ".config", - "opencode", - "opencode.json", + const fixture = join(root, "fixture"); + await mkdir(fixture); + await writeFile( + join(fixture, "index.ts"), + `export { default } from ${JSON.stringify(pathToFileURL(join(import.meta.dir, "fixtures/opencode-v2-smoke-plugin.ts")).href)};\n`, ); - await mkdir(join(childHome, ".config", "opencode"), { recursive: true }); await writeFile( - globalConfig, + join(project, "opencode.json"), JSON.stringify({ - agent: { - "fixture-other": { - options: { reference: "{env:FLOW_PICKER_FIXTURE}" }, + plugins: [ + { package: pluginEntry }, + { + package: fixture, + options: { baseURL: provider.baseURL, receipt }, }, - }, + ], + permissions: [{ action: "*", resource: "*", effect: "allow" }], }), ); + for (const directory of [ + "home", + "config", + "data", + "cache", + "state", + "tmp", + ]) + await mkdir(join(root, directory)); const port = await availablePort(); - const baseUrl = `http://127.0.0.1:${port}`; - const startupDeadline = Date.now() + STARTUP_TIMEOUT_MS; - const server = spawn( - toolchain.executable, - [ - "x", - `opencode-ai@${OPENCODE_VERSION}`, - "serve", - "--port", - String(port), - "--hostname", - "127.0.0.1", - ], - { + const args = ["serve", "--hostname", "127.0.0.1", "--port", String(port)]; + const invocation = process.env.FLOW_OPENCODE_SMOKE_EXECUTABLE + ? { command: process.env.FLOW_OPENCODE_SMOKE_EXECUTABLE, args } + : openCodeInvocation(args, VERSION); + const launch = () => + spawn(invocation.command, invocation.args, { cwd: project, detached: process.platform !== "win32", + stdio: ["ignore", "pipe", "pipe"], env: { - ...toolchain.environment, - HOME: childHome, - OPENCODE_TEST_HOME: childHome, - FLOW_PICKER_FIXTURE: "fixture-resolved-value", - XDG_CACHE_HOME: childCache, - XDG_CONFIG_HOME: join(childHome, ".config"), - XDG_DATA_HOME: join(childHome, ".local", "share"), - XDG_STATE_HOME: join(childHome, ".local", "state"), + PATH: toolchain.environment.PATH, + HOME: join(root, "home"), + XDG_CONFIG_HOME: join(root, "config"), + XDG_DATA_HOME: join(root, "data"), + XDG_CACHE_HOME: join(root, "cache"), + XDG_STATE_HOME: join(root, "state"), + TMPDIR: join(root, "tmp"), }, - stdio: "ignore", + }); + child = launch(); + child.stdout?.on("data", (chunk) => startup.consume(String(chunk))); + child.stderr?.on("data", (chunk) => + startup.consume(String(chunk), "stderr"), + ); + let client: OpenCodeClient | null = null; + const deadline = Date.now() + 180000; + let spawnError: Error | undefined; + child.once("error", (error) => { + spawnError = error; + }); + while (Date.now() < deadline) { + if (spawnError) throw spawnError; + if (child.exitCode !== null) + throw new Error(`OpenCode exited before readiness. ${startup.log}`); + client = startup.client(`http://127.0.0.1:${port}`, project); + if (client) { + const info = await client.server.info({ + signal: AbortSignal.timeout(3000), + }); + expect(info.version).toBe(VERSION); + break; + } + await Bun.sleep(50); + } + if (!client) + throw new Error( + `OpenCode did not publish private startup authentication. ${startup.log}`, + ); + startup.clear(); + const request = () => ({ signal: AbortSignal.timeout(30000) }); + const rootSession = await client.session.create( + { + title: "Packed smoke root", + agent: "build", + model: { providerID: "flow-smoke", id: "catalog-model" }, + location: { directory: project }, }, + request(), + ); + await client.session.prompt( + { sessionID: rootSession.id, text: "FLOW_SMOKE_USAGE_ONLY" }, + request(), ); + await client.session.wait({ sessionID: rootSession.id }, request()); + validationEvidence.push({ + kind: "usage", + ...(await proveNativeUsage(client, rootSession.id, project)), + }); - try { - const commands = await waitForCommands(baseUrl, startupDeadline); - expect( - commands - .map((command) => command.name) - .filter((name) => name.startsWith("flow-")) - .sort(), - ).toEqual([...EXPECTED_COMMANDS].sort()); + await client.session.command( + { sessionID: rootSession.id, name: "flow-smoke-inventory", text: "" }, + request(), + ); + expect( + ( + await client.command.list( + { location: { directory: project } }, + request(), + ) + ).data + .map((command) => command.name) + .filter( + (name) => + name.startsWith("flow-") && name !== "flow-smoke-inventory", + ) + .sort(), + ).toEqual(EXPECTED_COMMANDS); + const agents = ( + await client.agent.list({ location: { directory: project } }, request()) + ).data; + expect( + agents.find((agent) => agent.id === "flow-planner"), + ).toBeUndefined(); + for (const id of ["flow-worker", "flow-reviewer"]) + expect(agents.find((agent) => agent.id === id)?.hidden).toBe(true); + const nativeRoot = await client.session.get( + { sessionID: rootSession.id }, + request(), + ); + expect(nativeRoot.parentID).toBeUndefined(); + expect(await realpath(nativeRoot.location.directory)).toBe( + await realpath(project), + ); + const primaryClient = client; + const dispatch = async ( + agent: string, + actions: { tool: string; input: Record }[], + ) => { + const session = + agent === "build" + ? rootSession + : await primaryClient.session.create( + { + title: agent, + parentID: rootSession.id, + agent, + model: { providerID: "flow-smoke", id: "catalog-model" }, + location: { directory: project }, + }, + request(), + ); + const priorCalls = await tools(primaryClient, session.id); + await primaryClient.session.prompt( + { + sessionID: session.id, + text: `FLOW_SMOKE_ACTIONS ${JSON.stringify(actions)}`, + }, + request(), + ); + await primaryClient.session.wait({ sessionID: session.id }, request()); + return { + session, + calls: (await tools(primaryClient, session.id)).slice( + priorCalls.length, + ), + }; + }; + const denied = await dispatch("flow-worker", [ + { tool: "flow_plan_save", input: save }, + { tool: "flow_feature_complete", input: { request: {} } }, + ]); + expect(denied.calls.length).toBeGreaterThan(0); + for (const call of denied.calls) { + if (call.state.status === "error") continue; + expect(call.state.status).toBe("completed"); + if (call.state.status !== "completed") + throw new Error("Denied native call did not settle"); + const text = call.state.content + .filter((item) => item.type === "text") + .map((item) => item.text) + .join("\n"); + expect(JSON.parse(text)).toMatchObject({ status: "error" }); + } + expect( + await lstat(join(project, ".flow/session.json")).then( + () => true, + () => false, + ), + ).toBe(false); + const reviewer = await dispatch("flow-reviewer", [ + { tool: "flow_status", input: { request: { view: "compact" } } }, + ]); + expect( + reviewer.calls.map((call) => [call.name, call.state.status]), + ).toEqual([["flow_status", "completed"]]); + const allowed = await dispatch("build", [ + { tool: "flow_plan_save", input: save }, + ]); + expect( + allowed.calls + .filter((call) => call.name === "flow_plan_save") + .map((call) => call.state.status), + ).toEqual(["completed"]); + const savedCall = allowed.calls.find( + (call) => call.name === "flow_plan_save", + ); + if (savedCall?.state.status !== "completed") + throw new Error("Manager plan save did not complete"); + expect( + JSON.parse( + savedCall.state.content + .filter((item) => item.type === "text") + .map((item) => item.text) + .join("\n"), + ), + ).toMatchObject({ status: "ok" }); - const toolIds = (await fetchJson( - `${baseUrl}/experimental/tool/ids`, - )) as string[]; - expect( - toolIds.filter((name) => name.startsWith("flow_")).sort(), - ).toEqual([...EXPECTED_TOOLS].sort()); + const durable: unknown = JSON.parse( + await readFile(join(project, ".flow/session.json"), "utf8"), + ); + if (!isRecord(durable)) throw new Error("No durable Flow session"); + expect(durable.goal).toBe("Verify the installed V2 package"); + expect( + extractObservedModelIdentity( + await listNativeMessages(client, rootSession.id), + ), + ).toEqual({ + kind: "observed", + value: { providerID: "flow-smoke", modelID: "catalog-model" }, + }); + expect( + (await listNativeSessions(client, project)) + .filter((session) => session.parentID === rootSession.id) + .map((session) => session.id) + .sort(), + ).toEqual([denied.session.id, reviewer.session.id].sort()); + const validationResult = await runPackedValidationCases( + project, + dispatch, + validationEvidence, + ); + await runPackedReviewerReturn( + project, + client, + rootSession.id, + dispatch, + validationEvidence, + ); - const agents = (await fetchJson(`${baseUrl}/agent`)) as ResolvedAgent[]; - const flowAgents = agents.filter((agent) => - agent.name.startsWith("flow-"), + try { + const errorSession = await client.session.create( + { + title: "Provider failure control", + agent: "build", + model: { providerID: "flow-smoke", id: "catalog-model" }, + location: { directory: project }, + }, + request(), ); - expect(flowAgents.map((agent) => agent.name).sort()).toEqual([ - ...EXPECTED_AGENTS, - ]); - const reviewer = flowAgents.find( - (agent) => agent.name === "flow-reviewer", + await client.session.prompt( + { sessionID: errorSession.id, text: "FLOW_SMOKE_PROVIDER_ERROR" }, + request(), ); - if (!reviewer) throw new Error("Flow reviewer was not registered."); - expect(reviewer.mode).toBe("subagent"); - expect(reviewer.steps).toBe(80); - expect(reviewer.model).toEqual({ - providerID: "flow-probe", - modelID: "model", + await client.session.wait({ sessionID: errorSession.id }, request()); + validationEvidence.push({ + kind: "provider-error", + messages: (await listNativeMessages(client, errorSession.id)).map( + (message) => ({ + id: message.id, + type: message.type, + time: message.time, + ...(message.type === "assistant" ? { error: message.error } : {}), + }), + ), + requests: provider.requests.map(({ model, scenario }) => ({ + model, + scenario, + })), }); - expect(reviewer.variant).toBe("high"); - for (const reader of ["read", "glob", "grep"]) { - expect(toolIds).toContain(reader); - expect( - permissionFor( - reviewer.permission ?? [], - reader, - "src/unchanged.ts", - ), - ).toBe("allow"); - } - for (const permission of [ - "edit", - "bash", - "external_directory", - "skill", - "task", - ]) { - expect(permissionFor(reviewer.permission ?? [], permission)).toBe( - "deny", - ); - } - const reviewerAllowedTools = new Set([ - "flow_status", - "flow_feature_complete", - ]); - for (const toolId of EXPECTED_TOOLS) { - expect(permissionFor(reviewer.permission ?? [], toolId)).toBe( - reviewerAllowedTools.has(toolId) ? "allow" : "deny", - ); - } - const worker = flowAgents.find((agent) => agent.name === "flow-worker"); - if (!worker) throw new Error("Flow worker was not registered."); - expect(worker.mode).toBe("subagent"); expect( - permissionFor(worker.permission ?? [], "edit", "src/index.ts"), - ).toBe("allow"); - for (const protectedPath of [ - ".flow", - ".flow/session.json", - ".git", - ".git/config", - ]) { - expect( - permissionFor(worker.permission ?? [], "edit", protectedPath), - ).toBe("deny"); - } - expect(permissionFor(worker.permission ?? [], "bash", "bun test")).toBe( - "deny", + (await listNativeMessages(client, errorSession.id)).some( + (message) => + message.type === "assistant" && + message.error?.message.includes("Local scripted provider error"), + ), + ).toBe(true); + } catch (error) { + validationResult.failures.push( + error instanceof Error ? error : new Error(String(error)), ); - for (const permission of [ - "external_directory", - "skill", - "task", - "flow_status", - "flow_plan_save", - "flow_feature_complete", - ]) { - expect(permissionFor(worker.permission ?? [], permission)).toBe( - "deny", - ); - } - // Exercise the real global preference API and config reload without - // creating a session, submitting a prompt, or calling a provider. - for (const selected of ["flow-probe/selected", ""]) { - const response = await fetch(`${baseUrl}/global/config`, { - method: "PATCH", - headers: { "Content-Type": "application/json" }, - body: JSON.stringify({ - agent: { - "flow-reviewer": { options: { flowReviewerModel: selected } }, - }, - }), - signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS), - }); - expect(response.ok).toBe(true); - const expected = selected ? "selected" : "model"; - let refreshed: ResolvedAgent | undefined; - const deadline = Date.now() + 5000; - do { - refreshed = ( - (await fetchJson(`${baseUrl}/agent`)) as ResolvedAgent[] - ).find((agent) => agent.name === "flow-reviewer"); - if (refreshed?.model?.modelID === expected) break; - await Bun.sleep(50); - } while (Date.now() < deadline); - expect(refreshed?.model).toEqual({ - providerID: "flow-probe", - modelID: expected, - }); - expect(refreshed?.steps).toBe(80); - expect(refreshed?.variant ?? null).toBe(selected ? null : "high"); - expect(await readFile(globalConfig, "utf8")).toContain( - "{env:FLOW_PICKER_FIXTURE}", + } + try { + const cancelSession = await client.session.create( + { + title: "Cancellation control", + agent: "build", + model: { providerID: "flow-smoke", id: "catalog-model" }, + location: { directory: project }, + }, + request(), + ); + await client.session.prompt( + { sessionID: cancelSession.id, text: "FLOW_SMOKE_DELAY" }, + request(), + ); + const cancelDeadline = Date.now() + 5000; + let running = false; + while (Date.now() < cancelDeadline) { + running = (await listNativeMessages(client, cancelSession.id)).some( + (message) => + message.type === "assistant" && + message.time.completed === undefined, ); + if (running) break; + await Bun.sleep(10); } - for (const selected of ["flow-probe/planning", ""]) { - const response = await fetch(`${baseUrl}/global/config`, { - method: "PATCH", - headers: { "Content-Type": "application/json" }, - body: JSON.stringify({ - agent: { - "flow-planner": { options: { flowPlanningModel: selected } }, + expect(running).toBe(true); + expect( + ( + await client.session.interrupt( + { sessionID: cancelSession.id, resume: false }, + request(), + ) + ).interrupted, + ).toBe(true); + await client.session.wait({ sessionID: cancelSession.id }, request()); + expect( + (await listNativeMessages(client, cancelSession.id)) + .filter((message) => message.type === "assistant") + .map((message) => message.error?.type), + ).toContain("aborted"); + + await client.session.compact( + { sessionID: cancelSession.id }, + request(), + ); + await client.session.wait({ sessionID: cancelSession.id }, request()); + expect( + (await listNativeMessages(client, cancelSession.id)) + .filter((message) => message.type === "compaction") + .map((message) => message.status), + ).toEqual(["completed"]); + validationEvidence.push({ + kind: "cancel-compaction", + sessionID: cancelSession.id, + outcome: ( + await client.session.get({ sessionID: cancelSession.id }, request()) + ).outcome, + messages: await listNativeMessages(client, cancelSession.id), + }); + } catch (error) { + validationResult.failures.push( + error instanceof Error ? error : new Error(String(error)), + ); + } + + const records = (await readFile(receipt, "utf8")) + .trim() + .split("\n") + .map((line) => JSON.parse(line)) + .filter(isRecord); + expect( + records + .filter((row) => row.kind === "inventory") + .flatMap((row) => row.tools) + .filter( + (tool: unknown) => + typeof tool === "string" && tool.startsWith("flow_"), + ) + .sort(), + ).toEqual(EXPECTED_TOOLS); + expect( + records + .filter( + (row) => + row.kind === "tool" && + row.tool === "flow_plan_save" && + row.status === "completed" && + isRecord(row.application) && + row.application.status === "ok", + ) + .map((row) => row.sessionID), + ).toEqual([rootSession.id]); + expect(provider.requests.every((row) => row.model === "wire-model")).toBe( + true, + ); + expect(provider.requests.length).toBeGreaterThan(0); + await writeFile( + join(project, "opencode.json"), + JSON.stringify({ + plugins: [ + { + package: fixture, + options: { baseURL: provider.baseURL, receipt }, + }, + { + package: pluginEntry, + options: { + modelPreferences: { + planning: { + kind: "model", + model: { providerID: "flow-smoke", id: "catalog-model" }, + }, + }, }, - }), - signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS), - }); - expect(response.ok).toBe(true); - let planner: ResolvedAgent | undefined; - const deadline = Date.now() + 5000; - do { - planner = ( - (await fetchJson(`${baseUrl}/agent`)) as ResolvedAgent[] - ).find((agent) => agent.name === "flow-planner"); - if (selected ? planner?.model?.modelID === "planning" : !planner) - break; - await Bun.sleep(50); - } while (Date.now() < deadline); - expect(planner?.model ?? null).toEqual( - selected ? { providerID: "flow-probe", modelID: "planning" } : null, - ); - if (!selected) expect(planner).toBeUndefined(); - if (planner) { - for (const permission of [ - "edit", - "bash", - "task", - "skill", - "mcp_workspace_delete", - "custom_deploy", - "external_directory", - ...EXPECTED_TOOLS, - ]) - expect(permissionFor(planner?.permission ?? [], permission)).toBe( - "deny", - ); - for (const permission of ["read", "glob", "grep"]) - expect(permissionFor(planner.permission ?? [], permission)).toBe( - "allow", - ); - } - const reviewer = ( - (await fetchJson(`${baseUrl}/agent`)) as ResolvedAgent[] - ).find((agent) => agent.name === "flow-reviewer"); - expect(reviewer?.model).toEqual({ - providerID: "flow-probe", - modelID: "model", + }, + ], + permissions: [{ action: "*", resource: "*", effect: "allow" }], + }), + ); + await terminateChildProcessTree(child); + startup.clear(); + child = launch(); + child.stdout?.on("data", (chunk) => startup.consume(String(chunk))); + child.stderr?.on("data", (chunk) => + startup.consume(String(chunk), "stderr"), + ); + let restarted: OpenCodeClient | null = null; + const restartDeadline = Date.now() + 30000; + while (Date.now() < restartDeadline) { + if (child.exitCode !== null) + throw new Error("Configured planner host exited before readiness"); + restarted = startup.client(`http://127.0.0.1:${port}`, project); + if (restarted) break; + await Bun.sleep(20); + } + if (!restarted) + throw new Error("Configured planner host did not become ready"); + client = restarted; + expect((await client.server.info(request())).version).toBe(VERSION); + const ready = await client.session.create( + { + title: "Configured planner readiness", + agent: "build", + location: { directory: project }, + }, + request(), + ); + const configuredPlugins = await client.plugin.list( + { location: { directory: project } }, + request(), + ); + validationEvidence.push({ + kind: "configured-plugins", + plugins: configuredPlugins.data, + }); + expect( + configuredPlugins.data.filter( + (plugin) => plugin.state.status === "failed", + ), + ).toEqual([]); + await client.session.command( + { sessionID: ready.id, name: "flow-smoke-inventory", text: "" }, + request(), + ); + await client.session.remove({ sessionID: ready.id }, request()); + const preferenceSnapshot = await client.rpc.call( + { + rpcID: "flow.model-preferences", + method: "read", + input: {}, + location: { directory: project }, + }, + request(), + ); + validationEvidence.push({ + kind: "planner-rpc", + snapshot: preferenceSnapshot.output, + }); + const configuredAgents = ( + await client.agent.list({ location: { directory: project } }, request()) + ).data; + + const configuration: unknown = await client.config.get( + { location: { directory: project } }, + request(), + ); + validationEvidence.push({ + kind: "planner-configuration", + choices: Array.isArray(configuration) + ? configuration.filter(isRecord).flatMap((entry) => + isRecord(entry.info) && Array.isArray(entry.info.plugins) + ? entry.info.plugins + .filter(isRecord) + .filter((plugin) => plugin.package === pluginEntry) + .map((plugin) => plugin.options) + : [], + ) + : [], + planners: configuredAgents + .filter((agent) => agent.id.includes("planner")) + .map((agent) => ({ id: agent.id, model: agent.model })), + }); + + expect( + configuredAgents.find((agent) => agent.id === "flow-planner"), + ).toMatchObject({ + hidden: true, + model: { providerID: "flow-smoke", id: "catalog-model" }, + }); + + validationEvidence.push({ + kind: "configured-planner", + planner: configuredAgents.find((agent) => agent.id === "flow-planner") + ?.model, + }); + expect( + (await client.session.get({ sessionID: rootSession.id }, request())) + .model, + ).toEqual({ + providerID: "flow-smoke", + id: "catalog-model", + variant: "default", + }); + expect(await tarballSha256(tarball)).toBe(archiveDigest); + if (validationResult.failures.length) + throw new AggregateError( + validationResult.failures, + "Packed native validation gates failed", + ); + } finally { + if (process.env.FLOW_LIVE_SMOKE_EVIDENCE_DIR) { + const destination = process.env.FLOW_LIVE_SMOKE_EVIDENCE_DIR; + await mkdir(destination, { recursive: true, mode: 0o700 }); + await writeFile( + join(destination, "validation.json"), + JSON.stringify( + { hostVersion: VERSION, cases: validationEvidence }, + null, + 2, + ), + { mode: 0o600 }, + ); + await writeFile( + join(destination, "flow.log"), + startup.log + .split("\n") + .filter((line) => line.startsWith("[opencode-plugin-flow]")) + .join("\n"), + { mode: 0o600 }, + ); + const shellRoot = join(root, "data/opencode/shell"); + const shellOutputs: { + original: string; + retained: string; + sha256: string; + bytes: number; + }[] = []; + for (const path of await readdir(shellRoot, { recursive: true }).catch( + () => [], + )) { + if (!/^([a-f0-9]+)\/sh_[a-zA-Z0-9]+\.out$/.test(path)) continue; + const original = join(shellRoot, path); + const bytes = await readFile(original); + const retained = join(destination, "shell-output", path); + await mkdir(join(retained, ".."), { recursive: true, mode: 0o700 }); + await writeFile(retained, bytes, { mode: 0o600 }); + shellOutputs.push({ + original, + retained, + sha256: `sha256:${createHash("sha256").update(bytes).digest("hex")}`, + bytes: bytes.length, }); } + await writeFile( + join(destination, "shell-output.json"), + JSON.stringify(shellOutputs, null, 2), + { mode: 0o600 }, + ); - await expect(lstat(join(project, ".flow"))).rejects.toMatchObject({ - code: "ENOENT", - }); - } finally { - await terminateChildProcessTree(server); - await rm(scratch, { recursive: true, force: true }); + await readFile(receipt, "utf8").then( + (bytes) => + writeFile(join(destination, "native-receipt.jsonl"), bytes, { + mode: 0o600, + }), + () => {}, + ); } - }, - STARTUP_TIMEOUT_MS + 2 * REQUEST_TIMEOUT_MS, - ); -}); - -test("planning policy denies arbitrary custom mutations and allows only workspace readers", () => { - const rules: PermissionRule[] = Object.entries( - FLOW_CORE_AGENTS["flow-planner"].permission, - ).flatMap(([permission, value]) => - typeof value === "string" - ? [ - { - permission, - pattern: "*", - action: value as PermissionRule["action"], - }, - ] - : Object.entries(value).map(([pattern, action]) => ({ - permission, - pattern, - action: action as PermissionRule["action"], - })), - ); - for (const permission of [ - "mcp_workspace_delete", - "custom_deploy", - "edit", - "bash", - "task", - "flow_plan_save", - "external_directory", - ]) - expect(permissionFor(rules, permission)).toBe("deny"); - for (const permission of ["read", "glob", "grep"]) - expect(permissionFor(rules, permission)).toBe("allow"); -}); + startup.clear(); + if (child) await terminateChildProcessTree(child); + await provider.server.stop(true); + await rm(root, { recursive: true, force: true }); + } + }, + 240000, +); diff --git a/tests/live-smoke-metadata.test.ts b/tests/live-smoke-metadata.test.ts new file mode 100644 index 00000000..2fa9a7d0 --- /dev/null +++ b/tests/live-smoke-metadata.test.ts @@ -0,0 +1,160 @@ +import { expect, test } from "bun:test"; +import { + buildHostEvidenceCapabilities, + collectFieldObservations, + type EndpointAttempt, + type EndpointName, + HOST_METADATA_CONTRACT, +} from "../scripts/probe-opencode-eval-metadata.js"; + +const endpoints = HOST_METADATA_CONTRACT.endpoints; +const observed = ( + endpoint: EndpointName, + response: unknown, +): EndpointAttempt => ({ kind: "observed", endpoint, response }); +const manager = [ + { + type: "assistant", + id: "msg_manager", + agent: "build", + model: { providerID: "provider", id: "alias" }, + time: { created: 1, completed: 2 }, + content: [{ type: "text", text: "never retain private output" }], + }, +]; +const reviewer = [ + { ...manager[0], id: "msg_reviewer", agent: "flow-reviewer" }, +]; +function input() { + return { + opencodeVersion: "2.1.4", + generatedAt: "2026-10-24T00:00:00Z", + parentSessionId: "root", + endpointResponses: [ + observed(endpoints.serverInfo, { version: "2.1.4" }), + observed(endpoints.agents, { + location: { directory: "/fixture" }, + data: [], + }), + observed(endpoints.createSession, { + id: "root", + location: { directory: "/fixture" }, + }), + observed(endpoints.dispatchReview, null), + ], + childSessions: observed(endpoints.childSessions, [ + { id: "child", parentID: "root", agent: "flow-reviewer" }, + ]), + parentMessages: observed(endpoints.parentMessages, manager), + childMessages: [ + { + sessionId: "child", + messages: observed(endpoints.childMessages, reviewer), + }, + ], + }; +} + +test("metadata proof joins the public server version and completed native model refs", () => { + const proof = buildHostEvidenceCapabilities(input()); + expect(proof.result).toEqual({ kind: "complete" }); + expect(proof.capabilities.hostVersion).toEqual({ + kind: "observed", + matchesRequested: true, + fieldPath: "version", + }); + expect(proof.capabilities.parentManagerModelIdentity).toEqual({ + kind: "observed", + actors: [ + { actor: "parent-1", fieldPaths: ["[].model.id", "[].model.providerID"] }, + ], + }); + expect(proof.capabilities.childLineage).toEqual({ + kind: "observed", + links: [ + { + parent: "parent-1", + child: "child-1", + fieldPaths: ["[].id", "[].parentID"], + }, + ], + }); + expect(proof.capabilities.childReviewerModelIdentity).toEqual({ + kind: "observed", + actors: [ + { actor: "child-1", fieldPaths: ["[].model.id", "[].model.providerID"] }, + ], + }); + expect(JSON.stringify(proof).includes("never retain private output")).toBe( + false, + ); + expect(JSON.stringify(proof).includes("msg_manager")).toBe(false); +}); + +for (const mode of [ + "host-version", + "missing-version", + "failed-review", + "wrong-parent", + "missing-child-message", + "errored-assistant", + "incomplete-assistant", +] as const) { + test(`metadata proof refuses ${mode}`, () => { + const fixture = input(); + if (mode === "host-version") + fixture.endpointResponses[0] = observed(endpoints.serverInfo, { + version: "2.2.0", + }); + if (mode === "missing-version") + fixture.endpointResponses[0] = observed(endpoints.serverInfo, {}); + if (mode === "failed-review") + fixture.endpointResponses[3] = { + kind: "endpoint-failure", + endpoint: endpoints.dispatchReview, + }; + if (mode === "wrong-parent") + fixture.childSessions = observed(endpoints.childSessions, [ + { id: "child", parentID: "foreign", agent: "flow-reviewer" }, + ]); + if (mode === "missing-child-message") fixture.childMessages = []; + if (mode === "errored-assistant") + fixture.parentMessages = observed(endpoints.parentMessages, [ + { ...manager[0], error: { type: "api", message: "denied" } }, + ]); + if (mode === "incomplete-assistant") + fixture.parentMessages = observed(endpoints.parentMessages, [ + { ...manager[0], time: { created: 1 } }, + ]); + expect(buildHostEvidenceCapabilities(fixture).result).toEqual({ + kind: "inconclusive", + reason: + mode === "host-version" + ? "host-version-mismatch" + : mode === "failed-review" + ? "endpoint-failure" + : mode === "wrong-parent" + ? "reviewer-child-not-observed" + : ["errored-assistant", "incomplete-assistant"].includes(mode) + ? "model-did-not-answer" + : "required-capability-unavailable", + }); + }); +} + +test("metadata maps field shapes without descending into secrets, bodies or tokens", () => { + expect( + collectFieldObservations({ + model: { providerID: "private-provider", id: "private-model" }, + authorization: "private", + content: [{ text: "private" }], + accessToken: "private", + password: "private", + tokens: { input: 2 }, + }), + ).toEqual([ + { path: "model", kind: "object" }, + { path: "model.id", kind: "string" }, + { path: "model.providerID", kind: "string" }, + ]); +}); diff --git a/tests/materialize-qualification.test.ts b/tests/materialize-qualification.test.ts index b07078f6..c5a5b461 100644 --- a/tests/materialize-qualification.test.ts +++ b/tests/materialize-qualification.test.ts @@ -7,6 +7,7 @@ import { mkdir, mkdtemp, readFile, + realpath, rm, symlink, writeFile, @@ -64,7 +65,9 @@ function tar(entries: readonly Entry[]): Buffer { } async function fixture(packageVersion = "9.4.0") { - const root = await mkdtemp(join(tmpdir(), "flow-materialize-test-")); + const root = await realpath( + await mkdtemp(join(tmpdir(), "flow-materialize-test-")), + ); temporary.push(root); const roles = [ "report", diff --git a/tests/model-picker.test.ts b/tests/model-picker.test.ts index 43201185..a7ac7aa2 100644 --- a/tests/model-picker.test.ts +++ b/tests/model-picker.test.ts @@ -1,402 +1,337 @@ import { expect, test } from "bun:test"; -import { - createFlowCoreConfigEntries, - resolveFlowReviewerConfiguration, -} from "../src/config-shared.js"; -import { createConfigHook } from "../src/platform/opencode/config.js"; +import type { LocationRef } from "@opencode/client"; +import type { + DialogSelectOptions, + ToastOptions, +} from "@opencode/plugin/tui/context"; +import type { + FlowModelRole, + PickerChoice, + PickerSnapshot, + SaveModelPreference, +} from "../src/application/model-preferences.js"; +import { resolveFlowReviewerConfiguration } from "../src/config-shared.js"; import { applyReviewerPreference, + choiceKey, modelChoices, - modelPreference, - modelPreferencePatch, + type PickerCatalogModel, + resolvePreference, } from "../src/platform/opencode/model-picker.js"; -import type { HostConfig, Provider } from "../src/platform/opencode/sdk.js"; -import picker from "../src/tui.js"; - -const reviewPreferencePatch = (model: string) => - modelPreferencePatch("review", model); -const reviewPreference = (config: { agent?: Record }) => - modelPreference(config, "review"); +import { + createModelPicker, + type ModelPickerHost, +} from "../src/platform/opencode/model-picker-ui.js"; +import pickerDefinition from "../src/tui.js"; -const config: HostConfig = { - model: "test/coding", - plugin: [ - "other-plugin", - [ - "opencode-plugin-flow@8.4.0", - { - extra: true, - reviewer: { model: "test/old", variant: "high", steps: 80 }, - }, - ], - ], +const model: PickerCatalogModel = { + id: "native-id", + providerID: "test", + name: "Luna", + enabled: true, + status: "active", + capabilities: { tools: true, input: ["text"], output: ["text"] }, }; -const provider = { - id: "test", - name: "Test provider", - models: { - luna: { - id: "luna", - name: "Luna", - status: "active", - capabilities: { - toolcall: true, - input: { text: true }, - output: { text: true }, - }, - }, - image: { - id: "image", - name: "Images", - status: "active", - capabilities: { - toolcall: false, - input: { text: true }, - output: { text: false }, - }, +const selected: PickerChoice = { + kind: "model", + model: { providerID: "test", id: "native-id" }, +}; +const defaults = { + planning: { kind: "default" }, + review: { kind: "default" }, +} as const; +function snapshot(): PickerSnapshot { + return { + location: { directory: "/project" }, + revision: 0, + global: defaults, + project: {}, + effective: { + planning: { choice: defaults.planning, source: "role-default" }, + review: { choice: defaults.review, source: "role-default" }, }, - }, -} as unknown as Provider; + }; +} -test("lists connected coding models without requiring a paid probe", () => { - expect(modelChoices([provider], ["test"]).map((item) => item.value)).toEqual([ - "test/luna", +test("uses the native selection id and filters disabled, nontext, tool-less, and deprecated models", () => { + expect( + modelChoices( + [ + model, + { ...model, id: "disabled", enabled: false }, + { ...model, id: "old", status: "deprecated" }, + { + ...model, + id: "image", + capabilities: { tools: true, input: ["image"], output: ["text"] }, + }, + { + ...model, + id: "no-tools", + capabilities: { ...model.capabilities, tools: false }, + }, + ], + [{ id: "test", name: "Test provider" }], + ), + ).toEqual([ + { + title: "Luna", + value: selected, + category: "Test provider", + description: "test/native-id", + }, ]); - expect(modelChoices([provider], [])).toEqual([]); + expect(pickerDefinition.id).toBe("opencode-plugin-flow.model-picker"); }); -test("writes one preference without copying plugins or credentials", () => { - expect(reviewPreferencePatch("test/luna")).toEqual({ - agent: { "flow-reviewer": { options: { flowReviewerModel: "test/luna" } } }, - }); - expect(reviewPreferencePatch("")).toEqual({ - agent: { "flow-reviewer": { options: { flowReviewerModel: "" } } }, - }); - const base = { - model: { - kind: "explicit" as const, - source: "plugin-option" as const, - value: "test/old", - }, - variant: { - kind: "explicit" as const, - source: "environment" as const, - value: "high", +test("project default suppresses global and reviewer reset restores base variant and steps", () => { + const base = resolveFlowReviewerConfiguration({ + env: {}, + pluginOptions: { + reviewer: { model: "test/base", variant: "high", steps: 80 }, }, - steps: { kind: "host-default" as const }, - }; - expect(applyReviewerPreference(base, "test/luna")).toEqual({ - model: { kind: "explicit", source: "picker", value: "test/luna" }, - steps: base.steps, }); - expect(applyReviewerPreference(base, "")).toBe(base); expect( - reviewPreference({ - agent: { - "flow-reviewer": { options: { flowReviewerModel: "test/luna" } }, - }, - }), - ).toBe("test/luna"); - expect(() => - reviewPreference({ - agent: { "flow-reviewer": { options: { flowReviewerModel: 42 } } }, - }), - ).toThrow(); + resolvePreference( + { ...defaults, review: selected }, + { review: { kind: "default" } }, + "review", + ), + ).toEqual({ choice: { kind: "default" }, source: "project" }); + expect(applyReviewerPreference(base, "test/native-id")).toEqual({ + model: { kind: "explicit", source: "picker", value: "test/native-id" }, + steps: { kind: "explicit", source: "plugin-option", value: 80 }, + }); + expect(applyReviewerPreference(base, undefined)).toEqual(base); + expect(base.variant?.value).toBe("high"); }); -async function host(preference?: string) { - let current = structuredClone(config); - if (preference) current.agent = reviewPreferencePatch(preference).agent; - let connected = ["test"]; - let busy = false; - let writes = 0; - const commands: Array<{ - slashName: string; - title: string; - run(): void | Promise; +function fixture() { + let current = snapshot(); + let catalog = [model]; + let location: LocationRef = { directory: "/project" }; + let role: FlowModelRole | undefined = "review"; + let choice: PickerChoice | undefined = selected; + let confirm: boolean | undefined = true; + let active = false; + let confirmEffect = () => {}; + let readEffect = () => {}; + const saves: SaveModelPreference[] = []; + const toasts: ToastOptions[] = []; + const dialogs: Array<{ + options: readonly { + title: string; + value: unknown; + disabled?: boolean; + category?: string; + description?: string; + }[]; + placeholder?: string; }> = []; - let select: - | { - options: Array<{ value: string }>; - onSelect(item: { value: string }): void; - } - | undefined; - let confirm: { onConfirm(): void; onCancel(): void } | undefined; - const toasts: Array<{ variant: string; message: string }> = []; - const api = { - lifecycle: { signal: new AbortController().signal }, - state: { ready: true, path: { directory: "/fixture" } }, - keymap: { - registerLayer: (layer: { commands: typeof commands }) => { - commands.splice(0, commands.length, ...layer.commands); - }, + let cleared = 0; + const host: ModelPickerHost = { + location: () => location, + async read() { + readEffect(); + return structuredClone(current); }, - ui: { - toast: (toast: (typeof toasts)[number]) => toasts.push(toast), - dialog: { replace: (render: () => unknown) => render(), clear: () => {} }, - DialogSelect: (props: typeof select) => { - select = props; - return null; - }, - DialogConfirm: (props: typeof confirm) => { - confirm = props; - return null; - }, + async catalog() { + return { + models: catalog, + providers: [{ id: "test", name: "Test provider" }], + }; }, - client: { - global: { - config: { - get: async () => ({ data: structuredClone(current) }), - update: async ({ config: next }: { config: HostConfig }) => { - writes++; - current = { - ...current, - ...next, - agent: { ...current.agent, ...next.agent }, - }; - return { data: current }; - }, - }, + async busy() { + return active; + }, + async save(input) { + saves.push(input); + current = { + ...current, + revision: current.revision + 1, + global: { ...current.global, [input.role]: input.choice }, + }; + return { kind: "saved", snapshot: current }; + }, + dialog: { + async select( + options: DialogSelectOptions, + ): Promise { + dialogs.push(options); + const answer = options.title === "Flow model settings" ? role : choice; + const match = options.options.find( + (option) => + !option.disabled && + (typeof option.value === "string" + ? option.value === answer + : JSON.stringify(option.value) === JSON.stringify(answer)), + ); + return match?.value; }, - provider: { - list: async () => ({ data: { all: [provider], connected } }), + async confirm() { + confirmEffect(); + return confirm; }, - session: { - status: async () => ({ - data: { session: { type: busy ? "busy" : "idle" } }, - }), + clear() { + cleared++; + }, + }, + toast: { + show(value) { + toasts.push(value); }, }, }; - await picker.tui( - api as unknown as Parameters[0], - undefined, - {} as Parameters[2], - ); - const run = async (slashName: string) => { - await commands.find((command) => command.slashName === slashName)?.run(); - }; - const chooseRole = async (value: string) => { - select?.onSelect({ value }); - await new Promise((resolve) => setTimeout(resolve, 0)); - }; + const picker = createModelPicker(host); return { - open: async () => { - await run("flow-models"); - await chooseRole("review"); + picker, + saves, + toasts, + dialogs, + current: () => current, + cleared: () => cleared, + setCurrent(value: PickerSnapshot) { + current = value; }, - openModels: () => run("flow-models"), - slashNames: () => commands.map((command) => command.slashName).sort(), - chooseRole, - config: () => structuredClone(current), - choose: (value = "test/luna") => select?.onSelect({ value }), - cancel: () => confirm?.onCancel(), - confirm: async () => { - confirm?.onConfirm(); - await new Promise((resolve) => setTimeout(resolve, 0)); + setRole(value: FlowModelRole | undefined) { + role = value; }, - busy: () => { - busy = true; + setChoice(value: PickerChoice | undefined) { + choice = value; }, - disconnect: () => { - connected = []; + setConfirm(value: boolean | undefined) { + confirm = value; }, - change: () => { - current.agent = { - "flow-reviewer": { options: { flowReviewerModel: "test/other" } }, - }; + setBusy(value: boolean) { + active = value; + }, + onConfirm(effect: () => void) { + confirmEffect = effect; + }, + onRead(effect: () => void) { + readEffect = effect; + }, + setCatalog(value: PickerCatalogModel[]) { + catalog = value; + }, + setLocation(value: LocationRef) { + location = value; }, - choices: () => select?.options, - writes: () => writes, - toasts, }; } -test("native command requires a confirmed selection before saving", async () => { - const h = await host(); - await h.open(); - h.choose(); - h.cancel(); - expect(h.writes()).toBe(0); - await h.open(); - h.choose(); - await h.confirm(); - expect(h.writes()).toBe(1); - expect(h.toasts.at(-1)?.variant).toBe("success"); -}); -test("exposes one model command now that the reviewer alias is gone", async () => { - const h = await host(); - // 8.6.0 announced the deprecation; 9.0.0 removes it. Nothing should reintroduce - // a second entry point into the same picker. - expect(h.slashNames()).toEqual(["flow-models"]); -}); -test("rechecks activity, concurrent edits and provider availability before saving", async () => { - for (const condition of ["busy", "change", "disconnect"] as const) { - const h = await host(); - await h.open(); - h.choose(); - h[condition](); - await h.confirm(); - expect(h.writes()).toBe(0); - expect(h.toasts.at(-1)?.variant).toBe("error"); - } -}); - -test("server config applies the picker preference and restores original defaults", async () => { - const base = resolveFlowReviewerConfiguration({ - env: { - OPENCODE_FLOW_REVIEWER_MODEL: "test/default", - OPENCODE_FLOW_REVIEWER_VARIANT: "high", - }, - }); - let current = base; - const hook = createConfigHook( - {}, +test("saves each role independently and resets only the selected global role", async () => { + const f = fixture(); + await f.picker.open(); + expect(f.saves).toEqual([ { - reviewerConfiguration: base, - onReviewerConfiguration: (value) => { - current = value; - }, + role: "review", + expectedRevision: 0, + expectedProject: {}, + choice: selected, }, - ); - const config = reviewPreferencePatch("test/luna") as { - agent: Record; - }; - await hook(config); - expect(current.model).toMatchObject({ value: "test/luna", source: "picker" }); - expect(current.variant).toBeUndefined(); - expect(config.agent["flow-reviewer"]).toMatchObject({ model: "test/luna" }); - expect(config.agent["flow-reviewer"]).not.toHaveProperty("options"); - await hook(config); - expect(current.model).toMatchObject({ value: "test/luna" }); - await hook(reviewPreferencePatch("")); - expect(current).toBe(base); -}); - -test("shows an unavailable saved preference explicitly", async () => { - const h = await host("disconnected/model"); - await h.open(); - expect(h.choices()).toContainEqual( - expect.objectContaining({ value: "disconnected/model", disabled: true }), - ); - expect(h.writes()).toBe(0); -}); - -test("shared menu saves and resets planning independently of review and coding", async () => { - const h = await host("test/review"); - await h.openModels(); - expect(h.choices()?.map((choice) => choice.value)).toEqual([ - "planning", - "review", ]); - await h.chooseRole("planning"); - h.choose(); - await h.confirm(); - expect(modelPreference(h.config(), "planning")).toBe("test/luna"); - expect(modelPreference(h.config(), "review")).toBe("test/review"); - expect(h.config().model).toBe("test/coding"); - await h.openModels(); - await h.chooseRole("planning"); - h.choose(""); - await h.confirm(); - expect(modelPreference(h.config(), "planning")).toBe(""); - expect(modelPreference(h.config(), "review")).toBe("test/review"); - expect(h.config().model).toBe("test/coding"); + f.setRole("planning"); + await f.picker.open(); + f.setChoice({ kind: "default" }); + await f.picker.open(); + expect(f.current().global).toEqual({ + planning: { kind: "default" }, + review: selected, + }); + expect(f.toasts.map((toast) => toast.variant)).toEqual([ + "success", + "success", + "success", + ]); }); - -test("planning config survives repeated hooks and resets to direct manager planning", async () => { - let requested: string | undefined; - const hook = createConfigHook( - {}, - { - onPlanningModel: (model) => { - requested = model; - }, - }, - ); - const config = modelPreferencePatch("planning", "test/planner"); - await hook(config); - await hook(config); - expect(requested).toBe("test/planner"); - expect(config.agent["flow-planner"]).toMatchObject({ - model: "test/planner", - permission: { - edit: "deny", - bash: "deny", - "flow_*": "deny", - task: { "*": "deny" }, +test("selection and confirmation cancellation do not write", async () => { + const f = fixture(); + f.setRole(undefined); + await f.picker.open(); + f.setRole("review"); + f.setChoice(undefined); + await f.picker.open(); + f.setChoice(selected); + f.setConfirm(undefined); + await f.picker.open(); + expect(f.saves).toEqual([]); + expect(f.toasts).toEqual([]); +}); +test("unavailable current selection is disabled and default remains selectable", async () => { + const f = fixture(); + f.setCurrent({ + ...snapshot(), + global: { + ...defaults, + review: { kind: "model", model: { providerID: "gone", id: "old" } }, }, }); - const reset = modelPreferencePatch("planning", ""); - await hook(reset); - expect(requested).toBeUndefined(); - expect(reset.agent["flow-planner"]).not.toHaveProperty("model"); - expect(reset.agent["flow-planner"]).toHaveProperty("disable", true); - expect(() => - modelPreference( - { agent: { "flow-planner": { options: { flowPlanningModel: 42 } } } }, - "planning", - ), - ).toThrow("must be a string"); -}); - -test("no planning preference disables specialist discovery and leaves command models untouched", () => { - const configured = createFlowCoreConfigEntries({ env: {} }); - expect(configured.agent["flow-planner"].disable).toBe(true); - for (const command of Object.values(configured.command)) - expect(command).not.toHaveProperty("model"); - const enabled = createFlowCoreConfigEntries({ - env: {}, - planningModel: "test/planner", + f.setChoice({ kind: "default" }); + await f.picker.open(); + expect(f.dialogs[1]?.options[0]).toEqual({ + title: "gone/old", + value: { kind: "model", model: { providerID: "gone", id: "old" } }, + category: "Current preference", + description: "Unavailable here; reconnect or choose another model", + disabled: true, }); - expect(enabled.agent["flow-planner"]).toMatchObject({ - disable: false, - model: "test/planner", + expect(f.saves[0]?.choice).toEqual({ kind: "default" }); + expect(f.dialogs[1]?.placeholder).toBe("Search connected models"); +}); +for (const [name, change, expected] of [ + [ + "busy", + (f: ReturnType) => f.setBusy(true), + "Wait for active work", + ], + [ + "stale global", + (f: ReturnType) => + f.setCurrent({ ...snapshot(), revision: 1 }), + "preferences changed", + ], + [ + "stale project", + (f: ReturnType) => + f.setCurrent({ ...snapshot(), project: { planning: selected } }), + "Project model preferences changed", + ], + [ + "disconnected", + (f: ReturnType) => f.setCatalog([]), + "no longer available", + ], + [ + "location", + (f: ReturnType) => f.setLocation({ directory: "/other" }), + "Project changed", + ], +] as const) { + test(`refuses ${name} after confirmation`, async () => { + const f = fixture(); + f.onConfirm(() => change(f)); + await f.picker.open(); + expect(f.saves).toEqual([]); + expect(f.toasts[0]?.variant).toBe("error"); + expect(f.toasts[0]?.message).toContain(expected); }); - expect(enabled.command).toEqual(configured.command); - expect(enabled.agent["flow-reviewer"]).toEqual( - configured.agent["flow-reviewer"], - ); +} +test("unload while open clears the owned dialog and prevents a late save", async () => { + const f = fixture(); + f.onConfirm(() => f.picker.dispose()); + await f.picker.open(); + expect(f.cleared()).toBe(1); + expect(f.saves).toEqual([]); + expect(f.toasts).toEqual([]); + await f.picker.open(); + expect(f.saves).toEqual([]); }); - -test("saved role preferences do not produce collision warnings but custom agents do", async () => { - const warnings: string[] = []; - const ctx = { - client: { - app: { - log: ({ body }: { body: { level: string; message: string } }) => { - if (body.level === "warn") warnings.push(body.message); - }, - }, - }, - }; - const base = resolveFlowReviewerConfiguration({ env: {} }); - for (const role of ["planning", "review"] as const) { - for (const model of ["test/model", ""]) { - warnings.length = 0; - const preference = modelPreferencePatch(role, model); - const original = preference.agent; - const saved = structuredClone(original); - await createConfigHook(ctx, { reviewerConfiguration: base })(preference); - expect(original).toEqual(saved); - expect( - warnings.filter((message) => message.includes("user-defined")), - ).toEqual([]); - } - const stored = modelPreferencePatch(role, "test/model"); - const name = Object.keys(stored.agent)[0]; - if (!name) throw new Error("Missing fixture role"); - for (const entry of [ - { ...stored.agent[name], prompt: "Custom agent" }, - { options: { ...stored.agent[name]?.options, custom: true } }, - ]) { - warnings.length = 0; - await createConfigHook(ctx, { reviewerConfiguration: base })({ - agent: { [name]: entry }, - }); - expect( - warnings.some((message) => - message.includes(`user-defined agent named '${name}'`), - ), - ).toBe(true); - } - } +test("reentrant commands cannot open a second picker", async () => { + const f = fixture(); + f.onRead(() => { + void f.picker.open(); + }); + await f.picker.open(); + expect(f.saves.length).toBe(1); + expect(f.dialogs.length).toBe(2); + expect(choiceKey(f.current().global.review)).toBe("test/native-id"); }); diff --git a/tests/model-preferences-store.test.ts b/tests/model-preferences-store.test.ts new file mode 100644 index 00000000..3b077b90 --- /dev/null +++ b/tests/model-preferences-store.test.ts @@ -0,0 +1,230 @@ +import { afterEach, expect, test } from "bun:test"; +import { randomUUID } from "node:crypto"; +import { + mkdir, + mkdtemp, + readdir, + readFile, + rm, + symlink, + writeFile, +} from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join, resolve } from "node:path"; +import { + createModelPreferenceStore, + modelPreferencePath, +} from "../src/infrastructure/fs/model-preference-store.js"; + +const directories: string[] = []; +afterEach(async () => { + await Promise.all( + directories + .splice(0) + .map((path) => rm(path, { recursive: true, force: true })), + ); +}); +async function fixture() { + const root = await mkdtemp(join(tmpdir(), "flow-model-preferences-")); + directories.push(root); + const file = join(root, "model-preferences.json"); + return { + root, + file, + store: await createModelPreferenceStore(file, { lockTimeoutMs: 150 }), + }; +} +const modulePath = resolve("src/infrastructure/fs/model-preference-store.ts"); +function worker(file: string, mode: "save" | "hold", identity: string) { + return Bun.spawn( + [ + process.execPath, + "-e", + ` + import { createModelPreferenceStore } from ${JSON.stringify(modulePath)}; + import { writeFile } from 'node:fs/promises'; + const [file, mode, identity] = Bun.argv.slice(-3); + const store = await createModelPreferenceStore(file); + const result = await store.update(async current => { + if (mode === 'hold') { + await writeFile(file + '.ready', 'ready'); + await new Promise(() => { setInterval(() => {}, 1000); }); + } + if (current.revision !== 0) return { value: 'stale' }; + return { value: 'saved', next: { ...current, revision: 1, roles: { ...current.roles, review: { kind: 'model', model: { providerID: 'test', id: identity } } } } }; + }); + console.log(result); + `, + file, + mode, + identity, + ], + { stdout: "pipe", stderr: "pipe" }, + ); +} +async function ready(path: string) { + for (let attempt = 0; attempt < 200; attempt++) { + try { + if ((await readFile(path, "utf8")) === "ready") return; + } catch {} + await Bun.sleep(10); + } + throw new Error("Child did not acquire its preference lock."); +} +test("global preference path honors XDG state and never depends on the project", () => { + expect( + modelPreferencePath({ XDG_STATE_HOME: "/state", HOME: "/home/test" }), + ).toBe("/state/opencode-plugin-flow/model-preferences.json"); + expect( + modelPreferencePath({ XDG_STATE_HOME: "relative", HOME: "/home/test" }), + ).toBe("/home/test/.local/state/opencode-plugin-flow/model-preferences.json"); +}); +test("independent processes reject the same stale revision and preserve atomic JSON", async () => { + const f = await fixture(); + const children = [ + worker(f.file, "save", "one"), + worker(f.file, "save", "two"), + worker(f.file, "save", "three"), + ]; + const results = await Promise.all( + children.map(async (child) => { + const output = await new Response(child.stdout).text(); + const errors = await new Response(child.stderr).text(); + expect(await child.exited, errors).toBe(0); + return output.trim(); + }), + ); + expect(results.sort()).toEqual(["saved", "stale", "stale"]); + const stored = await f.store.read(); + expect(stored.revision).toBe(1); + expect(stored.roles.planning).toEqual({ kind: "default" }); + expect(JSON.parse(await readFile(f.file, "utf8"))).toEqual(stored); + expect((await readdir(f.root)).sort()).toEqual(["model-preferences.json"]); +}); +test("a dead writer is reclaimed without losing the last committed preferences", async () => { + const f = await fixture(); + const child = worker(f.file, "hold", "crashed"); + try { + await ready(`${f.file}.ready`); + await expect( + f.store.update(async () => ({ value: "must not enter" })), + ).rejects.toThrow("Timed out"); + } finally { + child.kill("SIGKILL"); + await child.exited; + } + await f.store.update(async (current) => ({ + value: undefined, + next: { + ...current, + revision: 1, + roles: { + planning: { + kind: "model", + model: { providerID: "test", id: "planner" }, + }, + review: { kind: "default" }, + }, + }, + })); + expect(await f.store.read()).toEqual({ + schemaVersion: 1, + revision: 1, + roles: { + planning: { kind: "model", model: { providerID: "test", id: "planner" } }, + review: { kind: "default" }, + }, + }); +}); +test("recovers an empty released lock and ignores abandoned unpublished owner directories", async () => { + const f = await fixture(); + await mkdir(join(f.root, "model-preferences.lock")); + await mkdir(join(f.root, ".preferences-lock-abandoned")); + const result = await f.store.update(async (current) => ({ + value: current.revision, + })); + expect(result).toBe(0); +}); +test("a live replacement owner survives old-owner cleanup", async () => { + const f = await fixture(); + const lock = join(f.root, "model-preferences.lock"); + const replacement = randomUUID(); + await f.store.update(async () => { + await rm(lock, { recursive: true }); + await mkdir(lock); + await writeFile( + join(lock, `owner-${replacement}.json`), + JSON.stringify({ token: replacement, pid: process.pid }), + ); + return { value: undefined }; + }); + expect(await readdir(lock)).toEqual([`owner-${replacement}.json`]); +}); +test("canonical state directories share a lock through path aliases", async () => { + const f = await fixture(); + const alias = `${f.root}-alias`; + directories.push(alias); + await symlink(f.root, alias); + const other = await createModelPreferenceStore( + join(alias, "model-preferences.json"), + { lockTimeoutMs: 50 }, + ); + await f.store.update(async () => { + await expect( + other.update(async () => ({ value: undefined })), + ).rejects.toThrow("Timed out"); + return { value: undefined }; + }); +}); +test("failed mutations and stale precommit guards preserve the committed record", async () => { + const f = await fixture(); + await expect( + f.store.update(async (current) => ({ + value: undefined, + next: { ...current, revision: 1 }, + beforeCommit() { + throw new Error("stale"); + }, + })), + ).rejects.toThrow("stale"); + expect((await f.store.read()).revision).toBe(0); + expect(await readdir(f.root)).toEqual([]); + await expect( + f.store.update(async () => { + throw new Error("rejected"); + }), + ).rejects.toThrow("rejected"); + expect((await f.store.read()).revision).toBe(0); +}); +test("file watch observes another store's atomic replacement", async () => { + const f = await fixture(); + const other = await createModelPreferenceStore(f.file); + const event = Promise.withResolvers(); + const close = f.store.watch(() => event.resolve(), event.reject); + try { + await other.update(async (current) => ({ + value: undefined, + next: { ...current, revision: 1 }, + })); + await Promise.race([ + event.promise, + Bun.sleep(1_000).then(() => { + throw new Error("No preference watch event."); + }), + ]); + expect((await f.store.read()).revision).toBe(1); + } finally { + close(); + } +}); +test("malformed persisted state fails closed", async () => { + const f = await fixture(); + await writeFile(f.file, '{"schemaVersion":1,"revision":0,"roles":{}}'); + await expect(f.store.read()).rejects.toThrow(); + await expect( + f.store.update(async () => ({ value: undefined })), + ).rejects.toThrow(); + expect(await readFile(f.file, "utf8")).toBe( + '{"schemaVersion":1,"revision":0,"roles":{}}', + ); +}); diff --git a/tests/model-preferences.test.ts b/tests/model-preferences.test.ts new file mode 100644 index 00000000..f5034dae --- /dev/null +++ b/tests/model-preferences.test.ts @@ -0,0 +1,470 @@ +import { afterEach, expect, test } from "bun:test"; +import { mkdtemp, rm } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join, resolve } from "node:path"; +import type { + OpenCodeEvent, + SessionInfo, + SessionMessageInfo, +} from "@opencode/client"; +import { Location, Model, Provider } from "@opencode/plugin"; +import type { RpcCallContext } from "@opencode/plugin/promise/rpc"; +import { Schema } from "effect"; +import type { + PickerChoice, + SaveModelPreference, +} from "../src/application/model-preferences.js"; +import { + pickerSnapshotSchema, + savePreferenceResultSchema, +} from "../src/application/model-preferences-schema.js"; +import { resolveFlowReviewerConfiguration } from "../src/config-shared.js"; +import type { FlowModelPreferencesRpc } from "../src/flow-rpc.js"; +import { + createFlowModelPreferences, + type FlowModelPreferences, + type FlowModelPreferencesContext, +} from "../src/platform/opencode/model-preferences.js"; + +const base = resolveFlowReviewerConfiguration({ + env: {}, + pluginOptions: { + reviewer: { model: "test/base", variant: "high", steps: 80 }, + }, +}); +const model: PickerChoice = { + kind: "model", + model: { providerID: "test", id: "native-id" }, +}; +const defaults: PickerChoice = { kind: "default" }; +const directories: string[] = []; +const cleanups: Array<() => Promise> = []; +const previousStateHome = process.env.XDG_STATE_HOME; +afterEach(async () => { + await Promise.all(cleanups.splice(0).map((dispose) => dispose())); + if (previousStateHome === undefined) delete process.env.XDG_STATE_HOME; + else process.env.XDG_STATE_HOME = previousStateHome; + await Promise.all( + directories + .splice(0) + .map((path) => rm(path, { recursive: true, force: true })), + ); +}); +async function isolated() { + const root = await mkdtemp(join(tmpdir(), "flow-model-preferences-rpc-")); + directories.push(root); + process.env.XDG_STATE_HOME = root; +} +async function fixture( + directory = "/project", + options: Record = {}, +) { + let read: (() => Promise) | undefined; + let set: + | ((input: SaveModelPreference, signal?: AbortSignal) => Promise) + | undefined; + let wake = Promise.withResolvers(); + let ended = false; + const queue: OpenCodeEvent[] = []; + const messages = new Map(); + let sessions: SessionInfo[] = []; + let catalog = [ + Model.Info.default(Provider.ID.make("test"), Model.ID.make("native-id")), + ]; + catalog = catalog.map((value) => ({ + ...value, + enabled: true, + capabilities: { + ...value.capabilities, + tools: true, + input: ["text"], + output: ["text"], + }, + })); + let modelEffect = () => {}; + const changed: Array<{ planning: string | undefined; reviewer: unknown }> = + []; + let preferences: FlowModelPreferences | undefined; + let disposed = false; + const context: FlowModelPreferencesContext = { + location: Schema.decodeUnknownSync(Location.Info)({ + directory, + project: { id: "project", directory, canonical: directory }, + }), + options, + model: { + async list() { + modelEffect(); + return { location: { directory }, data: catalog }; + }, + }, + session: { + async hook() { + return { async dispose() {} }; + }, + async list() { + return { data: sessions, cursor: {} }; + }, + async context(input) { + return messages.get(input.sessionID) ?? []; + }, + }, + event: { + async *subscribe({ signal } = {}) { + signal?.addEventListener("abort", () => wake.resolve(), { once: true }); + while (!ended && !signal?.aborted) { + while (queue.length) { + const next = queue.shift(); + if (next) yield next; + } + await wake.promise; + wake = Promise.withResolvers(); + } + }, + }, + rpc: { + async register(_definition, handlers) { + const call = { + signal: new AbortController().signal, + error() { + throw new Error("Not used."); + }, + } satisfies RpcCallContext; + read = async () => handlers.read({}, call); + set = async (input, signal) => + handlers.set(input, { ...call, signal: signal ?? call.signal }); + return { + async dispose() { + disposed = true; + }, + events: { async emit() {} }, + }; + }, + }, + }; + preferences = await createFlowModelPreferences(context, { + onChange() { + if (!preferences) throw new Error("Initial setup must not notify."); + changed.push({ + planning: preferences.planningModel(), + reviewer: preferences.reviewerConfiguration(base), + }); + }, + }); + cleanups.push(() => preferences.dispose()); + return { + preferences, + changed, + options, + async read() { + if (!read) throw new Error("No RPC read."); + return pickerSnapshotSchema.parse(await read()); + }, + async save(input: SaveModelPreference, signal?: AbortSignal) { + if (!set) throw new Error("No RPC set."); + return savePreferenceResultSchema.parse(await set(input, signal)); + }, + catalog(value: typeof catalog) { + catalog = value; + }, + onCatalog(effect: () => void) { + modelEffect = effect; + }, + endEvents() { + ended = true; + wake.resolve(); + }, + emit(event: OpenCodeEvent) { + queue.push(event); + wake.resolve(); + }, + setSession(session: SessionInfo, context: SessionMessageInfo[]) { + sessions = [session]; + messages.set(session.id, context); + }, + disposed: () => disposed, + busy() { + sessions = [ + { + id: "busy", + projectID: "project", + cost: 0, + tokens: { + input: 0, + output: 0, + reasoning: 0, + cache: { read: 0, write: 0 }, + }, + time: { created: 1, updated: 2 }, + location: { directory }, + }, + ]; + messages.set("busy", [ + { type: "user", id: "user", time: { created: 2 }, text: "working" }, + ]); + }, + }; +} +async function request( + f: Awaited>, + role: "planning" | "review", + choice: PickerChoice, +) { + const snapshot = await f.read(); + return { + role, + choice, + expectedRevision: snapshot.revision, + expectedProject: snapshot.project, + }; +} +async function until(condition: () => boolean) { + for (let i = 0; i < 100 && !condition(); i++) await Bun.sleep(10); + expect(condition()).toBe(true); +} +test("RPC saves update role inputs before callback, preserve independent roles, and restore base on reset", async () => { + await isolated(); + const f = await fixture(); + expect(f.changed).toEqual([]); + expect((await f.save(await request(f, "review", model))).kind).toBe("saved"); + expect(f.changed).toEqual([ + { + planning: undefined, + reviewer: { + model: { kind: "explicit", source: "picker", value: "test/native-id" }, + steps: { kind: "explicit", source: "plugin-option", value: 80 }, + }, + }, + ]); + await f.save(await request(f, "planning", model)); + expect(f.preferences.planningModel()).toBe("test/native-id"); + await f.save(await request(f, "review", defaults)); + expect(f.preferences.reviewerConfiguration(base)).toEqual(base); + expect(f.preferences.planningModel()).toBe("test/native-id"); + expect((await f.save(await request(f, "review", defaults))).kind).toBe( + "unchanged", + ); + expect(f.changed.length).toBe(3); +}); +test("every loaded location refreshes global choices and project default suppresses them", async () => { + await isolated(); + const a = await fixture("/a"); + const b = await fixture("/b"); + const project = await fixture("/project", { + modelPreferences: { + review: defaults, + planning: { + kind: "model", + model: { providerID: "project", id: "planner" }, + }, + }, + }); + await a.save(await request(a, "review", model)); + await until(() => b.changed.length === 1 && project.changed.length === 1); + expect(b.preferences.reviewerConfiguration(base).model).toEqual({ + kind: "explicit", + source: "picker", + value: "test/native-id", + }); + expect(project.preferences.reviewerConfiguration(base)).toEqual(base); + expect(project.preferences.planningModel()).toBe("project/planner"); + expect((await project.read()).effective.review).toEqual({ + choice: defaults, + source: "project", + }); +}); +test("concurrent stale confirmations have only one successful writer", async () => { + await isolated(); + const a = await fixture("/a"); + const b = await fixture("/b"); + const [ra, rb] = await Promise.all([ + request(a, "review", model), + request(b, "planning", model), + ]); + const results = await Promise.all([a.save(ra), b.save(rb)]); + expect(results.map((result) => result.kind).sort()).toEqual([ + "refused", + "saved", + ]); + expect(results.find((result) => result.kind === "refused")).toEqual({ + kind: "refused", + reason: "stale-preference", + }); + expect((await a.read()).revision).toBe(1); +}); +test("server refuses unavailable models and fresh project changes under the lock", async () => { + await isolated(); + const f = await fixture(); + const input = await request(f, "review", model); + f.catalog([]); + expect(await f.save(input)).toEqual({ + kind: "refused", + reason: "unavailable-model", + }); + f.options.modelPreferences = { review: defaults }; + expect(await f.save(input)).toEqual({ + kind: "refused", + reason: "stale-project", + }); + expect((await f.read()).revision).toBe(0); +}); +test("project changes during catalog validation refuse before commit", async () => { + await isolated(); + const f = await fixture(); + const input = await request(f, "review", model); + f.onCatalog(() => { + f.options.modelPreferences = { review: defaults }; + }); + expect(await f.save(input)).toEqual({ + kind: "refused", + reason: "stale-project", + }); + expect(f.changed).toEqual([]); +}); +test("server refuses a running transcript even when the client bypasses the TUI activity check", async () => { + await isolated(); + const f = await fixture(); + f.busy(); + expect(await f.save(await request(f, "review", model))).toEqual({ + kind: "refused", + reason: "busy", + }); + expect((await f.read()).revision).toBe(0); +}); +test("aborted and unloaded RPC saves cannot write, and dispose is idempotent", async () => { + await isolated(); + const f = await fixture(); + const input = await request(f, "review", model); + const abort = new AbortController(); + abort.abort(); + expect(await f.save(input, abort.signal)).toEqual({ + kind: "refused", + reason: "unloaded", + }); + await f.preferences.dispose(); + await f.preferences.dispose(); + expect(f.disposed()).toBe(true); + expect(await f.save(input)).toEqual({ kind: "refused", reason: "unloaded" }); + expect(f.changed).toEqual([]); +}); +test("event stream loss refuses further saves", async () => { + await isolated(); + const f = await fixture(); + const input = await request(f, "review", model); + f.endEvents(); + await Bun.sleep(0); + await expect(f.save(input)).rejects.toThrow("event stream ended"); +}); + +test("a separate process refreshes every loaded role input through the shared file", async () => { + await isolated(); + const a = await fixture("/a"); + const b = await fixture("/b"); + const file = join( + process.env.XDG_STATE_HOME ?? "", + "opencode-plugin-flow", + "model-preferences.json", + ); + const child = Bun.spawn( + [ + process.execPath, + "-e", + ` + import { createModelPreferenceStore } from ${JSON.stringify(resolve("src/infrastructure/fs/model-preference-store.ts"))}; + const store = await createModelPreferenceStore(Bun.argv.at(-1)); + await store.update(async current => ({ value: undefined, next: { ...current, revision: current.revision + 1, roles: { ...current.roles, review: {kind:'model',model:{providerID:'test',id:'other-process'}} } } })); + `, + file, + ], + { stdout: "pipe", stderr: "pipe" }, + ); + const errors = await new Response(child.stderr).text(); + expect(await child.exited, errors).toBe(0); + await until(() => a.changed.length === 1 && b.changed.length === 1); + expect(a.preferences.reviewerConfiguration(base).model).toEqual({ + kind: "explicit", + source: "picker", + value: "test/other-process", + }); + expect(b.preferences.reviewerConfiguration(base).model).toEqual({ + kind: "explicit", + source: "picker", + value: "test/other-process", + }); +}); + +for (const outcome of ["succeeded", "failed", "interrupted"] as const) { + test(`native execution events refuse work and release after ${outcome}`, async () => { + await isolated(); + const f = await fixture(); + const input = await request(f, "review", model); + const shared = { + id: "evt_start", + created: 1, + durable: { aggregateID: "busy", seq: 1, version: 1 as const }, + }; + f.emit({ + ...shared, + type: "session.execution.started", + data: { sessionID: "busy" }, + }); + await Bun.sleep(0); + expect(await f.save(input)).toEqual({ kind: "refused", reason: "busy" }); + if (outcome === "failed") + f.emit({ + ...shared, + type: "session.execution.failed", + data: { + sessionID: "busy", + error: { + type: "provider.invalid-request", + message: "Local fixture failure", + }, + }, + }); + else if (outcome === "interrupted") + f.emit({ + ...shared, + type: "session.execution.interrupted", + data: { sessionID: "busy", reason: "user" }, + }); + else + f.emit({ + ...shared, + type: "session.execution.succeeded", + data: { sessionID: "busy" }, + }); + await Bun.sleep(0); + expect((await f.save(input)).kind).toBe("saved"); + }); +} +test("native session timestamps refuse a new execution that retains a previous completed assistant", async () => { + await isolated(); + const f = await fixture(); + const session: SessionInfo = { + id: "busy", + projectID: "project", + cost: 0, + tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } }, + outcome: "succeeded", + time: { created: 1, updated: 4, idle: 3 }, + location: { directory: "/project" }, + }; + const assistant: SessionMessageInfo = { + type: "assistant", + id: "assistant", + agent: "build", + model: { providerID: "test", id: "native-id" }, + time: { created: 2, completed: 3 }, + content: [], + }; + f.setSession(session, [assistant]); + expect(await f.save(await request(f, "review", model))).toEqual({ + kind: "refused", + reason: "busy", + }); + f.setSession({ ...session, time: { ...session.time, updated: 3 } }, [ + assistant, + ]); + expect((await f.save(await request(f, "review", model))).kind).toBe("saved"); +}); diff --git a/tests/native-auth-broker-cleanup.test.ts b/tests/native-auth-broker-cleanup.test.ts new file mode 100644 index 00000000..ad580ea6 --- /dev/null +++ b/tests/native-auth-broker-cleanup.test.ts @@ -0,0 +1,194 @@ +import { expect, test } from "bun:test"; +import { spawnSync } from "node:child_process"; +import { + chmod, + lstat, + mkdir, + mkdtemp, + readFile, + rename, + rm, + writeFile, +} from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { basename, dirname, join } from "node:path"; +import { startNativeAccountBroker } from "../evals/native-auth/broker.js"; +import { NativeAuthError } from "../evals/native-auth/protocol.js"; + +type Call = { cwd: string; args: string[] }; +type Mode = + | "fail" + | "replacement" + | "delete-fail" + | "replace-after-close" + | "local-removal-failure" + | "relay-stop-injection"; +const generic = "Native account authentication failed."; + +async function brokerCleanup(mode: Mode, dead = false) { + if (process.pid === 76572) throw new Error("Unexpected cleanup runner PID"); + const root = await mkdtemp(join(tmpdir(), "native-auth-cleanup-")); + const previousTemporary = process.env.TMPDIR; + const statePath = join(root, "state.json"); + const log = join(root, "calls.jsonl"); + const executable = join(root, "native-cli"); + const temporary = join(root, "temporary"); + const retained = join(root, "retained"); + await mkdir(temporary, { mode: 0o700 }); + const child = dead + ? spawnSync(process.execPath, ["--eval", "process.exit(0)"]) + : undefined; + const pid = dead ? child?.pid : process.pid; + if (pid === undefined || pid === 76572) + throw new Error("Unexpected cleanup fixture PID"); + const state = { mode: "ready", pid }; + await writeFile(statePath, JSON.stringify(state), { mode: 0o600 }); + await writeFile( + executable, + `#!${process.execPath} +import { appendFileSync, readFileSync, renameSync, symlinkSync, writeFileSync } from "node:fs"; +const statePath = ${JSON.stringify(statePath)}; +const state = JSON.parse(readFileSync(statePath, "utf8")); +const args = process.argv.slice(2); +appendFileSync(${JSON.stringify(log)}, JSON.stringify({cwd:process.cwd(),args})+"\\n", {mode:0o600}); +if(state.mode === "fail" || (state.mode === "delete-fail" && args[1] === "DELETE")) process.exit(1); +if(args[0] === "service") { console.log("http://127.0.0.1:9/"); process.exit(0); } +if(args[1] === "GET") { console.log(JSON.stringify({version:"2.1.4",pid:state.pid+(state.mode === "replacement" ? 1 : 0)}));process.exit(0); } +if(args[1] === "DELETE") { + if(state.mode === "local-removal-failure") { renameSync(${JSON.stringify(temporary)},${JSON.stringify(retained)});symlinkSync(${JSON.stringify(temporary)},${JSON.stringify(temporary)}); } + process.exit(0); +} +if(args[2]?.includes("/close")) { + if(state.mode === "replace-after-close") writeFileSync(statePath,JSON.stringify({...state,mode:"replacement"})); + console.log(JSON.stringify({output:{}}));process.exit(0); +} +console.log(JSON.stringify({output:{accounts:[{route:{providerID:"alias",integrationID:"openai",credentialID:"selected",methodID:"oauth",label:"Selected"},value:{type:"oauth",methodID:"oauth",access:"synthetic-access",refresh:"flow-native-auth-source-owned",expires:4102444800000}}]}})); +`, + { mode: 0o700 }, + ); + await chmod(root, 0o700); + let owned: string | undefined; + let broker: Awaited> | undefined; + const serve = Bun.serve; + let rawRelay: ReturnType | undefined; + let stopCalls = 0; + try { + process.env.TMPDIR = temporary; + if (mode === "relay-stop-injection") { + Bun.serve = ((...args: Parameters) => { + const server = serve(...args); + rawRelay = server; + return new Proxy(server, { + get(target, key) { + if (key === "stop") + return async () => { + stopCalls++; + await target.stop(true); + throw new Error("relay-stop-private-sentinel"); + }; + const value = Reflect.get(target, key, target); + return typeof value === "function" ? value.bind(target) : value; + }, + }); + }) as typeof Bun.serve; + } + broker = await startNativeAccountBroker({ + opencodeExecutable: executable, + models: ["alias/model"], + }); + const initial: Call[] = (await readFile(log, "utf8")) + .trim() + .split("\n") + .map((line) => JSON.parse(line)); + const first = initial[0]; + if (!first) throw new Error("Missing cleanup fixture call"); + owned = dirname(first.cwd); + await writeFile(statePath, JSON.stringify({ ...state, mode })); + const disposed = broker.dispose(); + expect(broker.dispose()).toBe(disposed); + if (dead) await disposed; + else if ( + mode === "local-removal-failure" || + mode === "relay-stop-injection" + ) { + const failure: unknown = await disposed.catch((error) => error); + if (!(failure instanceof Error)) + throw new Error("Missing disposal rejection"); + expect(failure.message).toBe("Native account authentication failed."); + expect(failure instanceof NativeAuthError).toBe(true); + expect(failure.name).toBe("NativeAuthError"); + expect(failure.message).toBe(generic); + expect("cause" in failure).toBe(false); + expect(failure.stack?.includes(basename(owned)) ?? false).toBe(false); + expect( + failure.stack?.includes("relay-stop-private-sentinel") ?? false, + ).toBe(false); + expect(await broker.dispose().catch((error) => error)).toBe(failure); + } else await expect(disposed).rejects.toThrow(generic); + if (mode === "local-removal-failure") { + expect((await lstat(join(retained, basename(owned)))).isDirectory()).toBe( + true, + ); + expect((await lstat(temporary)).isSymbolicLink()).toBe(true); + await rm(temporary); + await rename(retained, temporary); + await expect(broker.dispose()).rejects.toThrow(generic); + expect((await lstat(owned)).isDirectory()).toBe(true); + } else await expect(lstat(owned)).rejects.toThrow(); + const calls: Call[] = (await readFile(log, "utf8")) + .trim() + .split("\n") + .map((line) => JSON.parse(line)); + const cleanup = calls.slice(initial.length); + expect( + cleanup.filter((call) => call.args[2]?.includes("/close")).length, + ).toBe( + mode === "delete-fail" || + mode === "replace-after-close" || + mode === "local-removal-failure" + ? 1 + : 0, + ); + expect(cleanup.filter((call) => call.args[1] === "DELETE").length).toBe( + mode === "delete-fail" || mode === "local-removal-failure" ? 1 : 0, + ); + if (mode === "relay-stop-injection") expect(stopCalls).toBe(1); + await expect(fetch(broker.configuration.url)).rejects.toThrow(); + } finally { + Bun.serve = serve; + await rawRelay?.stop(true); + await broker?.dispose().catch(() => {}); + if (previousTemporary === undefined) delete process.env.TMPDIR; + else process.env.TMPDIR = previousTemporary; + if ( + await lstat(retained).then( + () => true, + () => false, + ) + ) { + await rm(temporary); + await rename(retained, temporary); + } + if (owned) await rm(owned, { recursive: true, force: true }); + await rm(root, { recursive: true, force: true }); + } +} + +for (const mode of [ + "fail", + "replacement", + "delete-fail", + "replace-after-close", +] as const) + test(`broker disposal removes its directory and preserves failure through ${mode}`, async () => { + await brokerCleanup(mode); + }); +test("broker disposal removes its directory when the pinned PID is dead without unpinned RPC", async () => { + await brokerCleanup("fail", true); +}); +test("broker disposal hides real local filesystem removal failure without claiming removal", async () => { + await brokerCleanup("local-removal-failure"); +}); +test("broker disposal hides an explicitly injected relay-stop sentinel after attempting safe cleanup", async () => { + await brokerCleanup("relay-stop-injection"); +}); diff --git a/tests/native-auth-native.test.ts b/tests/native-auth-native.test.ts new file mode 100644 index 00000000..26503f55 --- /dev/null +++ b/tests/native-auth-native.test.ts @@ -0,0 +1,41 @@ +import { expect, test } from "bun:test"; +import { object } from "../evals/native-auth/protocol.js"; + +test.skipIf(!process.env.FLOW_OPENCODE_NATIVE_EXECUTABLE)( + "native account broker and private CLI discovery preserve source-owned OAuth with no fallback", + async () => { + const child = Bun.spawn( + [ + process.execPath, + new URL("./fixtures/native-auth-smoke.ts", import.meta.url).pathname, + ], + { + env: { ...process.env }, + stdout: "pipe", + stderr: "pipe", + }, + ); + const [stdout, , code] = await Promise.all([ + new Response(child.stdout).text(), + new Response(child.stderr).text(), + child.exited, + ]); + expect(code).toBe(0); + const receipt: unknown = JSON.parse(stdout); + expect( + object(receipt) && + receipt.status === "passed" && + receipt.processesStopped === true, + ).toBe(true); + if (!object(receipt)) throw new Error("Missing private native proof"); + expect(receipt.defaultCliAuthenticated).toBe(true); + expect(receipt.exactRpcQueryAccepted).toBe(true); + expect(receipt.childPluginsActiveBeforeCredentialImport).toBe(true); + expect(receipt.ambientFallbackRefused).toBe(true); + expect(receipt.otherAccountRefused).toBe(true); + expect(receipt.validCachedAccessRefusedWithoutSource).toBe(true); + expect(receipt.nativeBearerVerified).toBe(true); + expect(receipt.sourceLocationEvicted).toBe(true); + }, + 60000, +); diff --git a/tests/native-auth.test.ts b/tests/native-auth.test.ts new file mode 100644 index 00000000..a99db663 --- /dev/null +++ b/tests/native-auth.test.ts @@ -0,0 +1,765 @@ +import { afterEach, expect, test } from "bun:test"; +import { + chmod, + link, + lstat, + mkdir, + mkdtemp, + readdir, + readFile, + rename, + rm, + symlink, + writeFile, +} from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { Credential, Integration } from "@opencode/plugin"; +import type { IntegrationOAuthMethodRegistration } from "@opencode/plugin/promise/integration"; +import type { Context } from "@opencode/plugin/promise/plugin"; +import { registerNativeAccountClient } from "../evals/native-auth/client-plugin.js"; +import { + connectNativeAccountSource, + nativeAuthSourceSha256, + stageNativeAuthClient, + verifyNativeAuthClient, +} from "../evals/native-auth/host.js"; +import { + type NativeAccountConnection, + NativeAuthError, + PLACEHOLDER_REFRESH, + parseConnection, + parseOAuth, + providersForModels, +} from "../evals/native-auth/protocol.js"; +import { registerNativeAccountSource } from "../evals/native-auth/source-plugin.js"; + +const roots: string[] = []; +const servers: ReturnType[] = []; +afterEach(async () => { + for (const server of servers.splice(0)) await server.stop(true); + for (const root of roots.splice(0)) + await rm(root, { recursive: true, force: true }); +}); +async function directory() { + const root = await mkdtemp(join(tmpdir(), "flow-native-auth-unit-")); + await chmod(root, 0o700); + roots.push(root); + return root; +} +const route = { + providerID: "alias", + integrationID: "openai", + credentialID: "credential-selected", + methodID: "selected-oauth", + label: "Selected", +}; +const account = { + route, + metadata: { accountId: "synthetic-account", nested: { tier: "fixture" } }, +}; +const oauth = { + type: "oauth" as const, + methodID: route.methodID, + access: "synthetic-access", + refresh: PLACEHOLDER_REFRESH, + expires: 4102444800000, + metadata: account.metadata, +}; +const cap = "a".repeat(64), + lease = "b".repeat(64); +function context(value: unknown): Context { + return value as Context; +} +function rpcCapture() { + let handlers: unknown; + let disposed = 0; + return { + rpc: { + register: async (_definition: unknown, value: unknown) => { + handlers = value; + return { + dispose: async () => { + disposed++; + }, + }; + }, + }, + async call(name: string, input: unknown = {}) { + if (!handlers || typeof handlers !== "object") + throw new Error("Missing fixture RPC"); + const handler = Reflect.get(handlers, name); + if (typeof handler !== "function") + throw new Error("Missing fixture RPC method"); + return await Reflect.apply(handler, undefined, [input]); + }, + get disposed() { + return disposed; + }, + }; +} +function sourceFixture() { + const rpc = rpcCapture(); + let active: unknown = { + type: "credential", + id: route.credentialID, + label: route.label, + method: "oauth", + }; + let provider = { + id: route.providerID, + integrationID: route.integrationID, + activation: "enabled", + name: "Alias", + package: "fixture", + }; + let value = { ...oauth, refresh: "synthetic-original-refresh" }; + let concurrent = 0, + maxConcurrent = 0; + return { + rpc, + changeActive(next: unknown) { + active = next; + }, + disable() { + provider = { ...provider, activation: "disabled" }; + }, + changeValue(next: typeof value) { + value = next; + }, + get maxConcurrent() { + return maxConcurrent; + }, + ctx: context({ + options: { providers: [route.providerID] }, + provider: { get: async () => ({ data: provider }) }, + integration: { + get: async () => ({ + data: { methods: [{ type: "oauth", id: route.methodID }] }, + }), + connection: { + active: async () => active, + resolve: async () => { + concurrent++; + maxConcurrent = Math.max(maxConcurrent, concurrent); + await Bun.sleep(1); + concurrent--; + return structuredClone(value); + }, + }, + }, + rpc: rpc.rpc, + }), + }; +} +test("source returns placeholder OAuth fields and preserves metadata without exporting refresh", async () => { + const f = sourceFixture(); + const dispose = await registerNativeAccountSource(f.ctx); + const result = await f.rpc.call("describe"); + expect(JSON.stringify(result).includes("synthetic-original-refresh")).toBe( + false, + ); + expect(result).toEqual({ accounts: [{ route, value: oauth }] }); + await Promise.all([ + f.rpc.call("resolve", route), + f.rpc.call("resolve", route), + ]); + expect(f.maxConcurrent).toBe(1); + await dispose(); + await dispose(); + expect(f.rpc.disposed).toBe(1); + await expect(f.rpc.call("resolve", route)).rejects.toThrow( + "Native account authentication failed.", + ); +}); +test("source refuses switched, env, disabled, relabeled, method, and account metadata mismatches", async () => { + for (const change of [ + "switched", + "env", + "disabled", + "label", + "method", + "metadata", + ]) { + const f = sourceFixture(); + const dispose = await registerNativeAccountSource(f.ctx); + await f.rpc.call("describe"); + if (change === "switched") + f.changeActive({ + type: "credential", + id: "other", + label: route.label, + method: "oauth", + }); + if (change === "env") + f.changeActive({ type: "env", name: "OPENAI_API_KEY" }); + if (change === "disabled") f.disable(); + if (change === "label") + f.changeActive({ + type: "credential", + id: route.credentialID, + label: "changed", + method: "oauth", + }); + if (change === "method") + f.changeValue({ + ...oauth, + refresh: "synthetic-original-refresh", + methodID: "other", + }); + if (change === "metadata") + f.changeValue({ + ...oauth, + refresh: "synthetic-original-refresh", + metadata: { accountId: "other", nested: { tier: "fixture" } }, + }); + await expect(f.rpc.call("resolve", route)).rejects.toThrow( + "Native account authentication failed.", + ); + await dispose(); + } +}); +test("source blocks original refresh duplicated in metadata or access", async () => { + const f = sourceFixture(); + f.changeValue({ + ...oauth, + refresh: "synthetic-original-refresh", + access: "synthetic-original-refresh", + }); + const dispose = await registerNativeAccountSource(f.ctx); + await expect(f.rpc.call("describe")).rejects.toThrow( + "Native account authentication failed.", + ); + await dispose(); +}); +async function relay() { + let alive = true, + held = false, + resolutions = 0; + const server = Bun.serve({ + hostname: "127.0.0.1", + port: 0, + async fetch(request) { + if (!alive) + return new Response("synthetic-secret-diagnostic", { status: 503 }); + const path = new URL(request.url).pathname; + if (path === "/acquire") { + if (held) return new Response(null, { status: 409 }); + held = true; + return Response.json({ lease }); + } + if (path === "/release") { + held = false; + return Response.json({}); + } + if (path === "/check") return Response.json({}); + resolutions++; + return Response.json({ route, value: oauth }); + }, + }); + servers.push(server); + const configuration: NativeAccountConnection = { + version: 1, + url: server.url.href, + capability: cap, + accounts: [account], + }; + return { + configuration, + fail() { + alive = false; + }, + get resolutions() { + return resolutions; + }, + get held() { + return held; + }, + }; +} +async function clientFixture() { + const root = await directory(); + const remote = await relay(); + const path = join(root, "configuration.json"); + await writeFile( + path, + JSON.stringify({ connection: remote.configuration, lease }), + { mode: 0o600 }, + ); + let active: unknown = { + type: "credential", + id: route.credentialID, + method: "oauth", + label: route.label, + }; + const methods: IntegrationOAuthMethodRegistration[] = []; + const hooks = new Map(); + const ctx = context({ + options: { configurationPath: path }, + provider: { + get: async () => ({ + data: { activation: "enabled", integrationID: route.integrationID }, + }), + }, + integration: { + connection: { active: async () => active }, + transform: async (callback: (editor: unknown) => void) => { + callback({ + method: { + list: () => [ + { type: "oauth", id: route.methodID, label: "Selected" }, + { type: "oauth", id: "unrelated", label: "Other" }, + ], + update: (method: IntegrationOAuthMethodRegistration) => + methods.push(method), + }, + }); + return { dispose: async () => {} }; + }, + }, + session: { + hook: async (name: string, callback: unknown) => { + hooks.set(name, callback); + return { + dispose: async () => { + hooks.delete(name); + }, + }; + }, + }, + }); + const dispose = await registerNativeAccountClient(ctx); + const method = methods[0]; + if (!method?.refresh) throw new Error("Missing fixture refresh"); + const credential = Credential.OAuth.make({ + ...oauth, + methodID: Integration.MethodID.make(route.methodID), + }); + return { + remote, + methods, + credential, + dispose, + refresh: method.refresh, + changeActive(value: unknown) { + active = value; + }, + async guard( + name = "http.request", + input: unknown = { + model: { providerID: route.providerID }, + request: new Request("http://127.0.0.1/model"), + }, + ) { + const hook = hooks.get(name); + if (typeof hook !== "function") throw new Error("Missing fixture guard"); + await Reflect.apply(hook, undefined, [input]); + return input; + }, + }; +} +test("child replaces only selected method, uses source even for unexpired access, and preserves request identity", async () => { + const f = await clientFixture(); + expect(f.methods.map((method) => method.method.id)).toEqual([route.methodID]); + expect((await f.refresh(f.credential)).refresh).toBe(PLACEHOLDER_REFRESH); + expect(f.remote.resolutions).toBe(1); + const request = new Request("http://127.0.0.1/model", { + headers: { "x-evidence": "unchanged" }, + }); + const input = { model: { providerID: route.providerID }, request }; + expect(await f.guard("http.request", input)).toBe(input); + expect(input.request).toBe(request); + expect(request.headers.get("x-evidence")).toBe("unchanged"); + await f.guard("experimental.ws.handshake"); + await f.dispose(); + await f.dispose(); +}); +for (const conflict of [true, false]) + test( + conflict + ? "child refuses conflicting method credentials before registering a transform" + : "same-account aliases share one OAuth registration and resolve successfully", + async () => { + const root = await directory(); + const accounts = [ + account, + { + ...account, + route: { + ...route, + providerID: "alias-two", + credentialID: conflict ? "other-credential" : route.credentialID, + }, + }, + ]; + let requests = 0; + const server = Bun.serve({ + hostname: "127.0.0.1", + port: 0, + fetch() { + requests++; + return Response.json({ route: accounts[1]?.route, value: oauth }); + }, + }); + servers.push(server); + const path = join(root, "configuration.json"); + await writeFile( + path, + JSON.stringify({ + connection: { + version: 1, + url: server.url.href, + capability: cap, + accounts, + }, + lease, + }), + { mode: 0o600 }, + ); + let transforms = 0; + const methods: IntegrationOAuthMethodRegistration[] = []; + const ctx = context({ + options: { configurationPath: path }, + integration: { + connection: { + active: async () => ({ + type: "credential", + id: route.credentialID, + method: "oauth", + }), + }, + transform: async (callback: (editor: unknown) => void) => { + transforms++; + callback({ + method: { + list: () => [ + { type: "oauth", id: route.methodID, label: "Selected" }, + ], + update: (method: IntegrationOAuthMethodRegistration) => + methods.push(method), + }, + }); + return { dispose: async () => {} }; + }, + }, + session: { hook: async () => ({ dispose: async () => {} }) }, + }); + if (conflict) { + await expect(registerNativeAccountClient(ctx)).rejects.toThrow( + "Native account authentication failed.", + ); + expect(transforms).toBe(0); + expect(methods.length).toBe(0); + expect(requests).toBe(0); + } else { + const dispose = await registerNativeAccountClient(ctx); + expect(transforms).toBe(1); + expect(methods.map((method) => method.method.id)).toEqual([ + route.methodID, + ]); + const refresh = methods[0]?.refresh; + if (!refresh) throw new Error("Missing alias refresh"); + const credential = Credential.OAuth.make({ + ...oauth, + methodID: Integration.MethodID.make(route.methodID), + }); + expect((await refresh(credential)).refresh).toBe(PLACEHOLDER_REFRESH); + expect(requests).toBe(1); + await dispose(); + } + }, + ); +test("child refuses ambient env and another active child account before HTTP", async () => { + for (const active of [ + { type: "env", name: "OPENAI_API_KEY" }, + { type: "credential", id: "other", method: "oauth", label: "Other" }, + ]) { + const f = await clientFixture(); + f.changeActive(active); + await expect(f.guard()).rejects.toThrow( + "Native account authentication failed.", + ); + expect(f.remote.resolutions).toBe(0); + await f.dispose(); + } +}); +test("valid cached child access fails closed when the source disappears", async () => { + const f = await clientFixture(); + await f.refresh(f.credential); + f.remote.fail(); + await expect(f.guard()).rejects.toThrow( + "Native account authentication failed.", + ); + await expect(f.refresh(f.credential)).rejects.toThrow( + "Native account authentication failed.", + ); + await f.dispose(); +}); +test("client configuration errors are generic and private permissions are required", async () => { + const root = await directory(); + const path = join(root, "config.json"); + await writeFile(path, "synthetic-secret-malformed", { mode: 0o600 }); + await expect( + registerNativeAccountClient( + context({ options: { configurationPath: path } }), + ), + ).rejects.toThrow("Native account authentication failed."); + await chmod(path, 0o644); + await expect( + registerNativeAccountClient( + context({ options: { configurationPath: path } }), + ), + ).rejects.toThrow("Native account authentication failed."); +}); +test("stage produces deterministic bytes and exact directory verification rejects extra executable paths", async () => { + const root = await directory(); + const first = await stageNativeAuthClient(join(root, "one")); + const second = await stageNativeAuthClient(join(root, "two")); + expect(first.sourceSha256).toBe(second.sourceSha256); + expect(first.entrySha256).toBe(second.entrySha256); + expect( + nativeAuthSourceSha256( + Object.fromEntries(Object.entries(first.files).reverse()), + ) === first.sourceSha256, + ).toBe(true); + await verifyNativeAuthClient(first); + for (const kind of ["index.js", "nested", "node_modules"]) { + if (kind === "index.js") + await writeFile(join(first.pluginDirectory, kind), "export default {}\n"); + else await mkdir(join(first.pluginDirectory, kind)); + await expect(verifyNativeAuthClient(first)).rejects.toThrow( + "Native account authentication failed.", + ); + await rm(join(first.pluginDirectory, kind), { recursive: true }); + } + await writeFile(join(first.pluginDirectory, "client-plugin.ts"), "changed"); + await expect(verifyNativeAuthClient(first)).rejects.toThrow( + "Native account authentication failed.", + ); +}); +test("staged manifest rejects hardlinks and symlink entries", async () => { + for (const kind of ["symlink", "hardlink"]) { + const root = await directory(); + const artifact = await stageNativeAuthClient(join(root, "plugin")); + const path = join(artifact.pluginDirectory, "index.ts"); + const backup = join(root, "backup.ts"); + await writeFile(backup, await readFile(path)); + await rm(path); + if (kind === "symlink") await symlink(backup, path); + else await link(backup, path); + await expect(verifyNativeAuthClient(artifact)).rejects.toThrow( + "Native account authentication failed.", + ); + } +}); +test("host lease has only placeholder data, private out-of-project configuration, and startup disposal releases it", async () => { + const root = await directory(); + const remote = await relay(); + const source = connectNativeAccountSource(remote.configuration); + const auth = await source.prepareHost({ + directory: join(root, "attachment"), + models: ["alias/model"], + }); + expect(auth.credentialHandoff.entries.map((entry) => entry.id)).toEqual([ + route.credentialID, + ]); + expect( + JSON.stringify(auth.credentialHandoff.entries).includes( + "synthetic-original-refresh", + ), + ).toBe(false); + expect(Object.keys(auth.plugin.options)).toEqual(["configurationPath"]); + expect( + (await lstat(auth.plugin.options.configurationPath)).mode & 0o777, + ).toBe(0o600); + await expect( + source.prepareHost({ + directory: join(root, "overlap"), + models: ["alias/model"], + }), + ).rejects.toThrow("Native account authentication failed."); + await auth.dispose(); + await auth.dispose(); + expect(remote.held).toBe(false); + expect(await readdir(root)).toEqual([]); + await source.dispose(); +}); +test("presealed staging is verified and never rebuilt or removed by host attachment", async () => { + const root = await directory(); + const remote = await relay(); + const artifact = await stageNativeAuthClient(join(root, "sealed")); + const source = connectNativeAccountSource(remote.configuration); + const auth = await source.prepareHost({ + directory: join(root, "private"), + configurationPath: join(root, "private/capability.json"), + models: ["alias/model"], + stagedClient: artifact, + }); + expect(auth.plugin.package).toBe(artifact.pluginDirectory); + expect(auth.artifactHashes.sourceSha256).toBe(artifact.sourceSha256); + await auth.dispose(); + await verifyNativeAuthClient(artifact); + await source.dispose(); +}); +for (const mode of ["configuration-directory", "release-failure", "both"]) + test(`owned host cleanup survives ${mode} and preserves presealed bytes`, async () => { + const root = await directory(); + const remote = await relay(); + const artifact = await stageNativeAuthClient(join(root, "sealed")); + const names = Object.keys(artifact.files); + const before = await Promise.all( + names.map((name) => readFile(join(artifact.pluginDirectory, name))), + ); + const source = connectNativeAccountSource(remote.configuration); + const owned = join(root, "attachment"); + const auth = await source.prepareHost({ + directory: owned, + models: ["alias/model"], + stagedClient: artifact, + }); + if (mode !== "release-failure") { + await rm(auth.plugin.options.configurationPath); + await mkdir(auth.plugin.options.configurationPath); + } + if (mode !== "configuration-directory") remote.fail(); + if (mode === "configuration-directory") { + await auth.dispose(); + await auth.dispose(); + } else { + await expect(auth.dispose()).rejects.toThrow( + "Native account authentication failed.", + ); + await expect(auth.dispose()).rejects.toThrow( + "Native account authentication failed.", + ); + } + await expect(lstat(owned)).rejects.toThrow(); + const after = await Promise.all( + names.map((name) => readFile(join(artifact.pluginDirectory, name))), + ); + expect( + after.every((bytes, index) => + bytes.equals(before[index] ?? Buffer.alloc(0)), + ), + ).toBe(true); + await verifyNativeAuthClient(artifact); + await source.dispose().catch(() => {}); + }); +test("shutdown rejects identity and refresh corruption but explicit dispose releases consumed ownership", async () => { + const root = await directory(); + const remote = await relay(); + const source = connectNativeAccountSource(remote.configuration); + const auth = await source.prepareHost({ + directory: join(root, "attachment"), + models: ["alias/model"], + }); + const after = structuredClone([...auth.credentialHandoff.entries]); + const entry = after[0]; + if (entry?.value.type !== "oauth") throw new Error("Missing fixture entry"); + entry.value.refresh = "synthetic-unowned-refresh"; + await expect( + auth.credentialHandoff.onStop({ + before: auth.credentialHandoff.entries, + after, + }), + ).rejects.toThrow("Native account authentication failed."); + await auth.dispose(); + expect(remote.held).toBe(false); + await source.dispose(); +}); +for (const releaseFails of [false, true]) + test(`host disposal hides real local filesystem refusal with release failure ${releaseFails}`, async () => { + const root = await directory(); + const parent = join(root, "parent"); + const retained = join(root, "retained"); + await mkdir(parent, { mode: 0o700 }); + let releases = 0; + const server = Bun.serve({ + hostname: "127.0.0.1", + port: 0, + async fetch(request) { + if (new URL(request.url).pathname === "/acquire") + return Response.json({ lease }); + releases++; + await rename(parent, retained); + await symlink(parent, parent); + return releaseFails + ? new Response("private-release-sentinel", { status: 503 }) + : Response.json({}); + }, + }); + servers.push(server); + const source = connectNativeAccountSource({ + version: 1, + url: server.url.href, + capability: cap, + accounts: [account], + }); + const artifact = await stageNativeAuthClient(join(root, "sealed")); + const owned = join(parent, "attachment"); + const auth = await source.prepareHost({ + directory: owned, + models: ["alias/model"], + stagedClient: artifact, + }); + try { + const failure: unknown = await auth.dispose().catch((error) => error); + if (!(failure instanceof Error)) + throw new Error("Missing disposal rejection"); + expect(failure.message).toBe("Native account authentication failed."); + expect(failure instanceof NativeAuthError).toBe(true); + expect(failure.name).toBe("NativeAuthError"); + expect(failure.message).toBe("Native account authentication failed."); + expect("cause" in failure).toBe(false); + expect( + failure.stack?.includes(root.split("/").at(-1) ?? root) ?? false, + ).toBe(false); + expect(failure.stack?.includes("private-release-sentinel") ?? false).toBe( + false, + ); + expect(await auth.dispose().catch((error) => error)).toBe(failure); + expect(releases).toBe(1); + expect((await lstat(join(retained, "attachment"))).isDirectory()).toBe( + true, + ); + await verifyNativeAuthClient(artifact); + await rm(parent); + await rename(retained, parent); + expect(await auth.dispose().catch((error) => error)).toBe(failure); + expect((await lstat(owned)).isDirectory()).toBe(true); + } finally { + if ( + await lstat(retained).then( + () => true, + () => false, + ) + ) { + await rm(parent); + await rename(retained, parent); + } + await source.dispose().catch(() => {}); + } + }); +test("parsers do not echo values or allow nonloopback capabilities", () => { + for (const url of [ + "https://127.0.0.1:12/", + "http://example.com:12/", + "http://token@127.0.0.1:12/", + "http://127.0.0.1:12/?secret=fixture", + ]) { + expect(() => + parseConnection({ + version: 1, + url, + capability: cap, + accounts: [account], + }), + ).toThrow(NativeAuthError); + } + expect(() => + parseOAuth({ ...oauth, refresh: "synthetic-original-refresh" }), + ).toThrow("Native account authentication failed."); + expect(() => providersForModels(["bad-model"])).toThrow( + "Native account authentication failed.", + ); +}); diff --git a/tests/native-credential-retention.test.ts b/tests/native-credential-retention.test.ts new file mode 100644 index 00000000..d1ae5137 --- /dev/null +++ b/tests/native-credential-retention.test.ts @@ -0,0 +1,129 @@ +import { expect, test } from "bun:test"; +import { access, mkdir, mkdtemp, rm } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import type { CredentialEntry } from "@opencode/client"; +import { EvalHost } from "../evals/harness.js"; + +for (const mode of [ + "idle", + "active-reject", + "wait-reject", + "list-reject", + "handoff-reject", +] as const) { + test(`native credential shutdown preserves rotation through ${mode}`, async () => { + const scratch = await mkdtemp( + join(tmpdir(), "native-credential-retention-"), + ); + const project = join(scratch, "project"); + await mkdir(project); + const credential: CredentialEntry = { + id: "credential_fixture", + integrationID: "fixture", + label: "Local test", + active: true, + value: { type: "key", key: "synthetic-before" }, + }; + const before: CredentialEntry[] = [credential]; + const after: CredentialEntry[] = [ + { ...credential, value: { type: "key", key: "synthetic-after" } }, + ]; + let listed = 0; + let authenticationDisposed = 0; + const delivered: unknown[] = []; + const candidate: unknown = Reflect.construct(EvalHost, [project, scratch]); + if (!(candidate instanceof EvalHost)) + throw new Error("Invalid native host fixture"); + Reflect.set(candidate, "nativeClient", { + session: { + active: async () => { + if (mode === "active-reject") + throw new Error("Native active query failed"); + return mode === "wait-reject" + ? { session_fixture: { type: "running" } } + : {}; + }, + interrupt: async () => ({ interrupted: true }), + wait: async () => { + throw new Error("Native drain timed out"); + }, + }, + credential: { + list: async () => { + listed++; + if (mode === "list-reject") + throw new Error("Native credential export failed"); + return structuredClone(after); + }, + }, + }); + Reflect.set(candidate, "credentialHandoff", { + before, + onStop: async (snapshot: unknown) => { + delivered.push(snapshot); + if (mode === "handoff-reject") + throw new Error("Native credential handoff failed"); + }, + }); + Reflect.set(candidate, "nativeAuthSource", { + dispose: async () => { + authenticationDisposed++; + }, + }); + try { + if (mode === "idle") { + await candidate.stop(); + await expect(access(scratch)).rejects.toThrow(); + } else { + await expect(candidate.stop()).rejects.toThrow(); + await access(scratch); + } + expect(listed).toBe(1); + expect(delivered).toEqual( + mode === "list-reject" ? [] : [{ before, after }], + ); + await candidate.stop().catch(() => {}); + expect(listed).toBe(1); + expect(delivered).toHaveLength(mode === "list-reject" ? 0 : 1); + expect(authenticationDisposed).toBe(1); + } finally { + await rm(scratch, { recursive: true, force: true }); + } + }); +} + +test("native authentication closes after termination fails without retaining a live project", async () => { + const scratch = await mkdtemp(join(tmpdir(), "native-auth-termination-")); + const project = join(scratch, "project"); + await mkdir(project); + const candidate: unknown = Reflect.construct(EvalHost, [project, scratch]); + if (!(candidate instanceof EvalHost)) + throw new Error("Invalid native host fixture"); + let disposed = 0; + let retained = 0; + Reflect.set(candidate, "server", { + get pid() { + throw new Error("Native process identity unavailable"); + }, + }); + Reflect.set(candidate, "nativeAuthSource", { + dispose: async () => { + disposed++; + }, + }); + candidate.retainProjectOnStop(async () => { + retained++; + }); + try { + await expect(candidate.stop()).rejects.toThrow( + "Native process identity unavailable", + ); + await candidate.stop().catch(() => {}); + expect(disposed).toBe(1); + expect(retained).toBe(0); + await access(scratch); + } finally { + await rm(scratch, { recursive: true, force: true }); + } +}); diff --git a/tests/native-eval-auth-cli.test.ts b/tests/native-eval-auth-cli.test.ts new file mode 100644 index 00000000..54664894 --- /dev/null +++ b/tests/native-eval-auth-cli.test.ts @@ -0,0 +1,211 @@ +import { afterEach, expect, test } from "bun:test"; +import { + access, + chmod, + lstat, + mkdir, + mkdtemp, + readFile, + rename, + rm, + writeFile, +} from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import type { NativeAccountConnection } from "../evals/native-auth/protocol.js"; +import { readNativeAccountConnection } from "../evals/native-auth-config.js"; +import { runNativeAccountSource } from "../scripts/native-eval-auth.js"; +import { authorizePaidRun } from "../scripts/paid-budget.js"; + +const roots: string[] = []; +afterEach(async () => { + await Promise.all( + roots.splice(0).map((root) => rm(root, { recursive: true, force: true })), + ); +}); +const configuration = { + version: 1, + url: "http://127.0.0.1:43124/", + capability: "a".repeat(64), + accounts: [ + { + route: { + providerID: "fixture", + integrationID: "fixture-native", + credentialID: "fixture-account", + methodID: "fixture-method", + label: "Synthetic", + }, + }, + ], +} satisfies NativeAccountConnection; +async function fixture() { + const root = await mkdtemp(join(tmpdir(), "native-auth-command-")); + roots.push(root); + const budgetDirectory = join(root, "budget"); + await authorizePaidRun(budgetDirectory, { + schemaVersion: 1, + purpose: "Synthetic command lifecycle test", + models: ["fixture/model"], + maxDispatches: 1, + expiresAt: "2099-01-01T00:00:00.000Z", + }); + const controller = new AbortController(); + return { + root, + controller, + input: { + opencodeExecutable: "synthetic-never-executed", + models: ["fixture/model"], + output: join(root, "private", "configuration.json"), + signal: controller.signal, + budgetDirectory, + }, + }; +} +async function waitForConfiguration(path: string) { + for (let attempt = 0; attempt < 1000; attempt++) { + try { + return await readNativeAccountConnection(path); + } catch { + await Bun.sleep(5); + } + } + throw new Error("Synthetic source configuration was not published"); +} + +test("native source command publishes private configuration and closes its owned files", async () => { + const f = await fixture(); + let stopped = 0; + const running = runNativeAccountSource(f.input, async () => ({ + configuration, + dispose: async () => { + stopped++; + }, + })); + try { + expect(await waitForConfiguration(f.input.output)).toEqual(configuration); + expect((await lstat(f.input.output)).mode & 0o777).toBe(0o600); + expect((await lstat(join(f.root, "private"))).mode & 0o777).toBe(0o700); + } finally { + f.controller.abort(); + await running; + } + expect(stopped).toBe(1); + await expect(access(join(f.root, "private"))).rejects.toThrow(); +}); + +test("native source command refuses an unapproved model before touching the source", async () => { + const f = await fixture(); + let starts = 0; + await expect( + runNativeAccountSource( + { ...f.input, models: ["other/model"] }, + async () => { + starts++; + throw new Error("Must not start"); + }, + ), + ).rejects.toThrow("not authorized"); + expect(starts).toBe(0); + await expect(access(join(f.root, "private"))).rejects.toThrow(); +}); + +test("native source command refuses a missing authorization before touching the source", async () => { + const f = await fixture(); + let starts = 0; + await expect( + runNativeAccountSource( + { ...f.input, budgetDirectory: join(f.root, "missing") }, + async () => { + starts++; + throw new Error("Must not start"); + }, + ), + ).rejects.toThrow(); + expect(starts).toBe(0); + await expect(access(join(f.root, "private"))).rejects.toThrow(); +}); + +test("native source command preserves an existing output file and avoids source startup", async () => { + const f = await fixture(); + await mkdir(join(f.root, "private"), { mode: 0o700 }); + await writeFile(f.input.output, "existing private file", { mode: 0o600 }); + let starts = 0; + await expect( + runNativeAccountSource(f.input, async () => { + starts++; + throw new Error("Must not start"); + }), + ).rejects.toThrow(); + expect(starts).toBe(0); + expect(await readFile(f.input.output, "utf8")).toBe("existing private file"); +}); + +test("native source command refuses a public directory without changing its permissions", async () => { + const f = await fixture(); + await mkdir(join(f.root, "private")); + await chmod(join(f.root, "private"), 0o755); + let starts = 0; + await expect( + runNativeAccountSource(f.input, async () => { + starts++; + throw new Error("Must not start"); + }), + ).rejects.toThrow("private output directory"); + expect(starts).toBe(0); + expect((await lstat(join(f.root, "private"))).mode & 0o777).toBe(0o755); +}); + +test("failed native source startup removes only the new owned output", async () => { + const f = await fixture(); + await expect( + runNativeAccountSource(f.input, async () => { + throw new Error("Synthetic startup failed"); + }), + ).rejects.toThrow("Synthetic startup failed"); + await expect(access(join(f.root, "private"))).rejects.toThrow(); +}); + +test("native source disposal failure still removes private files", async () => { + const f = await fixture(); + const result = runNativeAccountSource(f.input, async () => ({ + configuration, + dispose: () => { + throw new Error("Synthetic disposal failed"); + }, + })).then( + () => undefined, + (error: unknown) => error, + ); + await waitForConfiguration(f.input.output); + f.controller.abort(); + expect(String(await result)).toBe("Error: Synthetic disposal failed"); + await expect(access(join(f.root, "private"))).rejects.toThrow(); +}); + +test("native source cleanup preserves a replacement output owned by another run", async () => { + const f = await fixture(); + let disposed = 0; + const result = runNativeAccountSource(f.input, async () => ({ + configuration, + dispose: async () => { + disposed++; + }, + })).then( + () => undefined, + (error: unknown) => error, + ); + await waitForConfiguration(f.input.output); + const replacement = join(f.root, "replacement.json"); + await writeFile(replacement, "replacement owned by another run", { + mode: 0o600, + }); + await rename(replacement, f.input.output); + f.controller.abort(); + expect(await result).toBeInstanceOf(Error); + expect(disposed).toBe(1); + expect(await readFile(f.input.output, "utf8")).toBe( + "replacement owned by another run", + ); +}); diff --git a/tests/opencode-native-core.test.ts b/tests/opencode-native-core.test.ts new file mode 100644 index 00000000..d3a0ce4e --- /dev/null +++ b/tests/opencode-native-core.test.ts @@ -0,0 +1,371 @@ +import { afterAll, expect, test } from "bun:test"; +import { mkdtemp, realpath, rm } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import type { AgentEditor } from "@opencode/plugin/promise/agent"; +import type { ToolEditor } from "@opencode/plugin/promise/tool"; +import { z } from "zod"; +import { + type FlowReviewerConfiguration, + resolveFlowReviewerConfiguration, +} from "../src/config-shared.js"; +import { registerFlowAgents } from "../src/platform/opencode/config.js"; +import { defineFlowTool } from "../src/platform/opencode/flow-tool.js"; +import { + AbsolutePath, + Agent, + CallID, + type NativeToolDefinition, + Session, + SessionMessage, + type ToolContext, +} from "../src/platform/opencode/sdk.js"; +import { + createTools, + registerFlowTools, +} from "../src/platform/opencode/tools.js"; +import { ValidationCaptureCoordinator } from "../src/platform/opencode/validation-capture.js"; + +const directory = await realpath( + await mkdtemp(join(tmpdir(), "flow-native-core-")), +); +afterAll(() => rm(directory, { recursive: true, force: true })); +const context: ToolContext = { + sessionID: Session.ID.make("ses_test"), + messageID: SessionMessage.ID.make("msg_test"), + agent: Agent.ID.make("build"), + id: CallID.make("call_test"), + signal: new AbortController().signal, + progress: async () => {}, +}; +function registry() { + const tools = new Map(); + const editor: ToolEditor = { + list: () => [...tools].map(([id, tool]) => ({ id, ...tool })), + get: (id) => { + const tool = tools.get(id); + return tool ? { id, ...tool } : undefined; + }, + namespace() {}, + add(tool) { + tools.set(tool.name, tool); + }, + update(id, update) { + const tool = tools.get(id); + if (tool) update(tool); + }, + remove(id) { + tools.delete(id); + }, + }; + return { + tools, + require(name: string) { + const definition = tools.get(name); + if (!definition) throw new Error(`Missing native tool ${name}.`); + return definition; + }, + tool: { + transform: async (apply: (editor: ToolEditor) => void) => { + apply(editor); + return { + dispose: async () => { + tools.clear(); + }, + }; + }, + }, + }; +} +test("native registration preserves the response envelope, explicit permission resource, and native call identity", async () => { + const host = registry(), + steps: unknown[] = []; + const envelope = { + status: "ok", + summary: "Applied.", + workflowData: { revision: 3 }, + } as const; + const registration = await registerFlowTools( + { + ...host, + location: { directory: AbsolutePath.make(directory) }, + permission: { + assert: async (input) => { + steps.push({ + action: input.action, + resources: input.resources, + source: input.source, + }); + }, + }, + }, + { + flow_run_start: defineFlowTool({ + description: "Start", + schema: z.object({ request: z.object({ featureId: z.string() }) }), + execute: async (_input, internal) => { + steps.push({ + callID: internal.callID, + directory: internal.directory, + }); + return envelope; + }, + }), + }, + async (native) => { + steps.push({ nativeID: native.id }); + }, + ); + const result = await host + .require("flow_run_start") + .execute({ request: { featureId: "feature" } }, context); + expect(result).toEqual({ + content: [{ type: "text", text: JSON.stringify(envelope, null, 2) }], + }); + expect(steps).toEqual([ + { + action: "flow_run_start", + resources: ["feature"], + source: { type: "tool", messageID: "msg_test", id: "call_test" }, + }, + { nativeID: "call_test" }, + { callID: "call_test", directory }, + ]); + expect(host.tools.get("flow_run_start")?.options).toEqual({ + codemode: false, + permission: "flow_run_start", + }); + await registration.dispose(); + expect(host.tools.size).toBe(0); +}); +test.each(["permission", "causality", "abort"])( + "%s refusal prevents the Flow effect", + async (refusal) => { + const host = registry(); + let effects = 0; + await registerFlowTools( + { + ...host, + location: { directory: AbsolutePath.make(directory) }, + permission: { + assert: async () => { + if (refusal === "permission") + throw new Error("permission rejected"); + }, + }, + }, + { + flow_run_start: { + description: "Start", + input: { type: "object" }, + execute: async () => { + effects++; + return "effect"; + }, + }, + }, + async () => { + if (refusal === "causality") throw new Error("unproven request"); + }, + ); + const controller = new AbortController(); + controller.abort(); + await expect( + host + .require("flow_run_start") + .execute( + {}, + refusal === "abort" + ? { ...context, signal: controller.signal } + : context, + ), + ).rejects.toThrow(); + expect(effects).toBe(0); + }, +); +test("guidance remains Markdown inside structured native content", async () => { + const host = registry(); + const tools = createTools({ + validation: new ValidationCaptureCoordinator({ + readSession: async () => undefined, + persistObservation: async () => { + throw new Error("unused"); + }, + }), + prepareValidation: async () => { + throw new Error("unused"); + }, + }); + await registerFlowTools( + { + ...host, + location: { directory: AbsolutePath.make(directory) }, + permission: { assert: async () => {} }, + }, + tools, + async () => {}, + ); + const value = await host + .require("flow_guidance") + .execute({ id: "flow" }, context); + const content = value.content; + expect( + Array.isArray(content) && content[0]?.type === "text" && content[0].text, + ).toContain("#"); + expect( + Array.isArray(content) && + content[0]?.type === "text" && + content[0].text.startsWith("{"), + ).toBe(false); +}); +test("native execution parses application refinements before any workspace effect", async () => { + const host = registry(); + const tools = createTools({ + validation: new ValidationCaptureCoordinator({ + readSession: async () => undefined, + persistObservation: async () => { + throw new Error("unused"); + }, + }), + prepareValidation: async () => { + throw new Error("unused"); + }, + }); + await registerFlowTools( + { + ...host, + location: { directory: AbsolutePath.make(directory) }, + permission: { assert: async () => {} }, + }, + tools, + async () => {}, + ); + await expect( + host.require("flow_plan_save").execute( + { + request: { + goal: "🧪".repeat(8193), + operationId: "op", + expectedRevision: 0, + plan: { summary: "Plan", features: [] }, + }, + }, + context, + ), + ).rejects.toThrow(); +}); +test("replayed native agent transforms create missing roles and read the latest preference state", async () => { + const agents = new Map>>(); + const editor: AgentEditor = { + list: () => [...agents.values()], + get: (id) => agents.get(id), + default() {}, + remove(id) { + agents.delete(id); + }, + update(id, update) { + const value = agents.get(id) ?? Agent.Info.default(Agent.ID.make(id)); + update(value); + agents.set(id, value); + }, + }; + let replay = (_editor: AgentEditor): void => { + throw new Error("unregistered"); + }; + let planning: string | undefined; + let reviewer: FlowReviewerConfiguration = resolveFlowReviewerConfiguration({ + env: {}, + pluginOptions: { + reviewer: { model: "provider/original", variant: "high", steps: 42 }, + }, + }); + await registerFlowAgents( + { + agent: { + transform: async (apply) => { + replay = apply; + apply(editor); + return { dispose: async () => {} }; + }, + }, + }, + { + assertOperational() {}, + readPlanningModel: () => planning, + readReviewerConfiguration: () => reviewer, + }, + ); + expect([...agents.keys()]).toEqual(["flow-reviewer", "flow-worker"]); + expect(agents.get("flow-reviewer")).toMatchObject({ + mode: "subagent", + hidden: true, + model: { providerID: "provider", id: "original", variant: "high" }, + steps: 42, + }); + planning = "provider/planning"; + reviewer = { + ...reviewer, + model: { kind: "explicit", source: "picker", value: "provider/picked" }, + variant: undefined, + }; + replay(editor); + expect(agents.get("flow-planner")?.model).toEqual({ + providerID: "provider", + id: "planning", + }); + expect(agents.get("flow-reviewer")?.model).toEqual({ + providerID: "provider", + id: "picked", + }); + reviewer = resolveFlowReviewerConfiguration({ + env: {}, + pluginOptions: { + reviewer: { model: "provider/original", variant: "high", steps: 42 }, + }, + }); + planning = undefined; + replay(editor); + expect(agents.get("flow-planner")).toBeUndefined(); + expect(agents.get("flow-reviewer")?.model).toEqual({ + providerID: "provider", + id: "original", + variant: "high", + }); + expect(agents.get("flow-reviewer")?.steps).toBe(42); +}); + +test("unsupported continuation describes native request attribution", async () => { + const tools = createTools({ + validation: new ValidationCaptureCoordinator({ + readSession: async () => undefined, + persistObservation: async () => { + throw new Error("unused"); + }, + }), + prepareValidation: async () => { + throw new Error("unused"); + }, + autoContinuationSupport: () => "unsupported", + }); + const value = await tools.flow_status?.execute( + { request: { view: "compact" } }, + { + sessionID: "ses_test", + messageID: "msg_test", + callID: "call_test", + agent: "build", + directory, + abort: context.signal, + }, + ); + expect(value).toMatchObject({ + workflowData: { + autoContinuation: { + scope: "current-plugin-process", + support: "unsupported", + reason: "host-request-attribution-unavailable", + recovery: "Drive each feature with /flow-run.", + }, + }, + }); +}); diff --git a/tests/opencode-schema-contract.test.ts b/tests/opencode-schema-contract.test.ts index 4715ba20..f73764c7 100644 --- a/tests/opencode-schema-contract.test.ts +++ b/tests/opencode-schema-contract.test.ts @@ -2,7 +2,6 @@ import { describe, expect, test } from "bun:test"; import { mkdtemp, rm } from "node:fs/promises"; import { tmpdir } from "node:os"; import { join } from "node:path"; -import { type ToolContext, tool } from "@opencode-ai/plugin"; import { z } from "zod"; import { FeatureCompleteInputSchema, @@ -17,7 +16,9 @@ import { ValidationStartInputSchema, } from "../src/application/schema.js"; import { FLOW_GUIDANCE_IDS } from "../src/guidance/catalog.js"; +import type { FlowToolContext as ToolContext } from "../src/platform/opencode/flow-tool.js"; import { createTools } from "../src/platform/opencode/tools.js"; +import { ValidationCaptureCoordinator } from "../src/platform/opencode/validation-capture.js"; const LIFECYCLE_TOOL_NAMES = [ "flow_status", @@ -42,7 +43,12 @@ type SafeSchema = { function createRegisteredTools() { return createTools({ - validation: {} as never, + validation: new ValidationCaptureCoordinator({ + readSession: async () => undefined, + persistObservation: async () => { + throw new Error("Unused persistence."); + }, + }), prepareValidation: async () => { throw new Error("Validation execution is outside this schema test."); }, @@ -54,26 +60,21 @@ const registeredTools = createRegisteredTools(); function hostSchema(name: LifecycleToolName) { const definition = registeredTools[name]; if (!definition) throw new Error(`Missing registered ${name}.`); - return tool.schema.object(definition.args); + return z.fromJSONSchema(definition.input); } -type JsonSchema = { - properties?: Record; - required?: string[]; - additionalProperties?: boolean; - anyOf?: JsonSchema[]; - oneOf?: JsonSchema[]; - allOf?: JsonSchema[]; - items?: JsonSchema; -}; +type JsonSchema = z.core.JSONSchema.JSONSchema; function emittedHostSchema(name: LifecycleToolName): JsonSchema { - return tool.schema.toJSONSchema(hostSchema(name)) as JsonSchema; + const definition = registeredTools[name]; + if (!definition) throw new Error(`Missing ${name}.`); + return definition.input; } function property(schema: JsonSchema, name: string): JsonSchema { const result = schema.properties?.[name]; - if (!result) throw new Error(`Missing JSON Schema property '${name}'.`); + if (!result || typeof result === "boolean") + throw new Error(`Missing JSON Schema property '${name}'.`); return result; } @@ -84,12 +85,14 @@ function propertyNames(schema: JsonSchema): string[] { function collectPropertyNames(schema: JsonSchema, result = new Set()) { for (const [name, child] of Object.entries(schema.properties ?? {})) { result.add(name); - collectPropertyNames(child, result); + if (typeof child !== "boolean") collectPropertyNames(child, result); } for (const branch of schema.anyOf ?? []) collectPropertyNames(branch, result); for (const branch of schema.oneOf ?? []) collectPropertyNames(branch, result); for (const branch of schema.allOf ?? []) collectPropertyNames(branch, result); - if (schema.items) collectPropertyNames(schema.items, result); + const items = Array.isArray(schema.items) ? schema.items : [schema.items]; + for (const item of items) + if (item && typeof item !== "boolean") collectPropertyNames(item, result); return result; } @@ -196,34 +199,31 @@ function expectParity( ): void { const hostResult = hostSchema(name).safeParse(input); const applicationResult = applicationSchemas[name].safeParse(input); - expect(hostResult.success, `${name} host schema`).toBe(expected); + if (expected) expect(hostResult.success, `${name} host schema`).toBe(true); + else { + const definition = registeredTools[name]; + if (!definition) throw new Error(`Missing ${name}.`); + expect(() => + definition.execute(input, { + sessionID: "schema-session", + messageID: "schema-message", + callID: "schema-call", + agent: "build", + directory: "/unused", + abort: new AbortController().signal, + }), + ).toThrow(); + } expect(applicationResult.success, `${name} application schema`).toBe( expected, ); - if (hostResult.success && applicationResult.success) { - expect(hostResult.data as Record).toEqual( - applicationResult.data as Record, - ); - } } describe("Flow OpenCode host schemas", () => { - test("registers the authoritative application request schemas", () => { - for (const name of LIFECYCLE_TOOL_NAMES) { - expect( - Object.is( - registeredTools[name]?.args.request, - applicationSchemas[name].shape.request, - ), - name, - ).toBe(true); - } - }); - test("keeps strict requests with optional status-only recovery proposals", () => { for (const name of LIFECYCLE_TOOL_NAMES) { const definition = registeredTools[name]; - expect(Object.keys(definition?.args ?? {}), name).toEqual( + expect(Object.keys(definition?.input.properties ?? {}), name).toEqual( name === "flow_status" ? ["recoveryProposal", "request"] : ["request"], ); const emitted = emittedHostSchema(name); @@ -242,7 +242,7 @@ describe("Flow OpenCode host schemas", () => { }); test("matches the compact v6 request shapes", () => { - const expected = { + const expected: Partial> = { flow_validation_start: [ "command", "expectedRevision", @@ -281,15 +281,16 @@ describe("Flow OpenCode host schemas", () => { ], } as const; - for (const [name, fields] of Object.entries(expected) as Array< - [Exclude, readonly string[]] - >) { + for (const name of LIFECYCLE_TOOL_NAMES) { + if (name === "flow_status") continue; + const fields = expected[name]; + if (!fields) continue; const request = property(emittedHostSchema(name), "request"); expect(propertyNames(request), name).toEqual([...fields]); expect(request.additionalProperties, name).toBe(false); } expect( - property(emittedHostSchema("flow_status"), "request").anyOf, + property(emittedHostSchema("flow_status"), "request").oneOf, ).toHaveLength(5); expectParity( "flow_feature_reset", @@ -309,8 +310,9 @@ describe("Flow OpenCode host schemas", () => { const result = schema.safeParse(validInputs[name]); if (!result.success) throw new Error(`Expected valid ${name} input.`); expect( - (result.data as { request: { assignmentId: string } }).request - .assignmentId, + z + .object({ request: z.object({ assignmentId: z.string() }) }) + .parse(result.data).request.assignmentId, ).toBe(assignmentId); } } @@ -417,23 +419,6 @@ describe("Flow OpenCode host schemas", () => { } }); - test("emits the same field names as the application schema for every tool", () => { - // The host needs its own zod instance to emit JSON Schema, so the two - // surfaces are necessarily separate declarations. Nothing but this parity - // check keeps them from drifting, and drift is silent: the host simply - // rejects a field the runtime still reads. - for (const name of LIFECYCLE_TOOL_NAMES) { - const application = z.toJSONSchema( - applicationSchemas[name] as z.ZodType, - { io: "input", unrepresentable: "any" }, - ) as JsonSchema; - expect( - [...collectPropertyNames(emittedHostSchema(name))].sort(), - name, - ).toEqual([...collectPropertyNames(application)].sort()); - } - }); - test("accepts the review finding fields the reviewer is instructed to send", () => { // `skills/flow-review/SKILL.md` tells the reviewer to send `scopeBlocker` // and `findingId`, and `nextAction` reads `scopeBlocker` to decide between @@ -494,13 +479,19 @@ describe("Flow OpenCode host schemas", () => { test("keeps completion authorization separate from validation cancellation", async () => { const workspace = await mkdtemp(join(tmpdir(), "flow-schema-contract-")); const cancelled: string[] = []; + const validation = new ValidationCaptureCoordinator({ + readSession: async () => undefined, + persistObservation: async () => { + throw new Error("Unused persistence."); + }, + }); + const cancel = validation.cancel.bind(validation); + validation.cancel = (sessionID) => { + cancelled.push(sessionID); + return cancel(sessionID); + }; const tools = createTools({ - validation: { - cancel(sessionID: string) { - cancelled.push(sessionID); - return true; - }, - } as never, + validation, prepareValidation: async () => { throw new Error("Not used by mutation tools."); }, @@ -509,8 +500,10 @@ describe("Flow OpenCode host schemas", () => { sessionID: "schema-contract-session", agent: "build", directory: workspace, - worktree: workspace, - } as ToolContext; + messageID: "schema-message", + callID: "schema-call", + abort: new AbortController().signal, + } satisfies ToolContext; const managerMutations = [ "flow_plan_save", "flow_plan_amend", @@ -552,8 +545,8 @@ describe("Flow OpenCode host schemas", () => { test("keeps guidance as the sole non-request tool", () => { const definition = registeredTools.flow_guidance; if (!definition) throw new Error("Missing flow_guidance."); - const schema = tool.schema.object(definition.args); - expect(Object.keys(definition.args)).toEqual(["id"]); + const schema = z.fromJSONSchema(definition.input); + expect(Object.keys(definition.input.properties ?? {})).toEqual(["id"]); for (const id of FLOW_GUIDANCE_IDS) { expect(schema.safeParse({ id }).success).toBe(true); } diff --git a/tests/package-smoke.test.ts b/tests/package-smoke.test.ts index 19b6b1f4..593d7623 100644 --- a/tests/package-smoke.test.ts +++ b/tests/package-smoke.test.ts @@ -54,6 +54,16 @@ describe("package smoke", () => { const plugin = (await import(`${entryUrl.href}?smoke=${Date.now()}`)) as { default?: unknown; }; - expect(typeof plugin.default).toBe("function"); + expect(plugin.default).toMatchObject({ + id: "opencode-plugin-flow", + setup: expect.any(Function), + }); + const tui = await import( + `${pathToFileURL(new URL("dist/tui.js", repositoryRoot).pathname).href}?smoke=${Date.now()}` + ); + expect(tui.default).toMatchObject({ + id: "opencode-plugin-flow.model-picker", + setup: expect.any(Function), + }); }, 30_000); }); diff --git a/tests/provenance.test.ts b/tests/provenance.test.ts index a6435ca4..577ff5d3 100644 --- a/tests/provenance.test.ts +++ b/tests/provenance.test.ts @@ -1,4 +1,4 @@ -import { afterEach, describe, expect, spyOn, test } from "bun:test"; +import { afterEach, describe, expect, test } from "bun:test"; import { execFile } from "node:child_process"; import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises"; import { homedir, tmpdir } from "node:os"; @@ -97,7 +97,7 @@ function unsafeTarball(path: string): Promise { } describe("eval provenance", () => { - test("forwards requested variants to both native host endpoints without changing wait policy", async () => { + test("selects the requested variant before each native dispatch without changing wait policy", async () => { const previousAuthorization = process.env.FLOW_EVAL_AUTHORIZATION; const authorization = await directory(); await authorizePaidRun(authorization, { @@ -108,31 +108,25 @@ describe("eval provenance", () => { expiresAt: new Date(Date.now() + 60000).toISOString(), }); process.env.FLOW_EVAL_AUTHORIZATION = authorization; - const posted: Array<{ path: string; body: Record }> = []; + const posted: Array<{ method: string; input: unknown }> = []; const waited: object[] = []; - const requests = spyOn(globalThis, "fetch").mockImplementation( - Object.assign( - async ( - url: Parameters[0], - init?: Parameters[1], - ) => { - posted.push({ - path: new URL(String(url)).pathname, - body: JSON.parse(String(init?.body)), - }); - return Response.json({}); - }, - { preconnect: fetch.preconnect }, - ), - ); + const capture = (method: string) => async (input: unknown) => { + posted.push({ method, input: structuredClone(input) }); + }; const host = Reflect.construct(EvalHost, [ "/unused", "/unused", ]) as EvalHost; + Reflect.set(host, "nativeClient", { + session: { + switchModel: capture("switchModel"), + command: capture("command"), + prompt: capture("prompt"), + }, + }); Object.assign(host, { - baseUrl: "http://127.0.0.1:1", waitForQuiet: async ( - _sessionId: string, + _sessionID: string, options: { request: { settled: Promise } }, ) => { waited.push(options); @@ -152,36 +146,39 @@ describe("eval provenance", () => { await host.runPrompt("session", "task", "route/model"); expect(posted).toEqual([ { - path: "/session/session/command", - body: { - command: "flow-auto", - arguments: "task", - model: "route/model", - variant: "high", + method: "switchModel", + input: { + sessionID: "session", + model: { providerID: "route", id: "model", variant: "high" }, }, }, { - path: "/session/session/message", - body: { - model: { providerID: "route", modelID: "model" }, - variant: "low", - parts: [{ type: "text", text: "task" }], + method: "command", + input: { sessionID: "session", name: "flow-auto", text: "task" }, + }, + { + method: "switchModel", + input: { + sessionID: "session", + model: { providerID: "route", id: "model", variant: "low" }, }, }, + { method: "prompt", input: { sessionID: "session", text: "task" } }, { - path: "/session/session/message", - body: { - model: { providerID: "route", modelID: "model" }, - parts: [{ type: "text", text: "task" }], + method: "switchModel", + input: { + sessionID: "session", + model: { providerID: "route", id: "model" }, }, }, + { method: "prompt", input: { sessionID: "session", text: "task" } }, ]); + expect(waited).toHaveLength(3); expect(waited[0]).toHaveProperty("quietMs", 25); expect(waited[1]).toHaveProperty("quietMs", 35); for (const options of waited) expect(options).not.toHaveProperty("variant"); } finally { - requests.mockRestore(); if (previousAuthorization === undefined) delete process.env.FLOW_EVAL_AUTHORIZATION; else process.env.FLOW_EVAL_AUTHORIZATION = previousAuthorization; @@ -214,7 +211,7 @@ describe("eval provenance", () => { ).not.toHaveProperty("variant"); }); - test("retains host model fields when the variant and provider facts are unavailable", () => { + test("retains native model identity when variant and provider facts are unavailable", () => { const actor = extractObservedActor({ role: "reviewer", sessions: [ @@ -222,12 +219,9 @@ describe("eval provenance", () => { id: "reviewer", messages: [ { - info: { - role: "assistant", - time: { completed: 2 }, - providerID: "route", - modelID: "model", - }, + type: "assistant", + time: { completed: 2 }, + model: { providerID: "route", id: "model" }, }, ], }, @@ -243,28 +237,17 @@ describe("eval provenance", () => { expect(hostActorObservation(actor).model).not.toHaveProperty("family"); }); - test("keeps completed-assistant variant observations separate from model identity", () => { - const messages = [ - { - info: { - role: "assistant", - time: { completed: 2 }, - providerID: "route", - modelID: "model", - variant: "high", - }, - }, - { - info: { - role: "assistant", - time: { completed: 3 }, - model: { providerID: "route", modelID: "model", variant: "high" }, - }, - }, - ]; + test("keeps completed native variant observations separate from model identity", () => { + const message = (completed: number, variant: string) => ({ + type: "assistant", + time: { completed }, + model: { providerID: "route", id: "model", variant }, + }); const actor = extractObservedActor({ role: "manager", - sessions: [{ id: "parent", messages }], + sessions: [ + { id: "parent", messages: [message(2, "high"), message(3, "high")] }, + ], }); expect(hostActorObservation(actor)).toEqual({ model: { @@ -276,13 +259,7 @@ describe("eval provenance", () => { const conflicting = extractObservedActor({ role: "manager", sessions: [ - { - id: "parent", - messages: [ - messages[0], - { info: { ...messages[0]?.info, variant: "low" } }, - ], - }, + { id: "parent", messages: [message(2, "high"), message(3, "low")] }, ], }); expect(hostActorObservation(conflicting)).toEqual({ @@ -291,17 +268,21 @@ describe("eval provenance", () => { }); }); - test("does not promote requested, errored, or incomplete variants to observations", () => { - for (const info of [ - { role: "user", time: { completed: 2 }, model: { variant: "high" } }, - { role: "assistant", time: { created: 1 }, variant: "high" }, - { role: "assistant", time: { completed: 2 }, error: {}, variant: "high" }, - ]) { - expect(extractObservedVariant([{ info }])).toEqual({ + test("does not promote requested, errored, or incomplete native variants", () => { + for (const message of [ + { type: "user", time: { completed: 2 }, model: { variant: "high" } }, + { type: "assistant", time: { created: 1 }, model: { variant: "high" } }, + { + type: "assistant", + time: { completed: 2 }, + error: {}, + model: { variant: "high" }, + }, + ]) + expect(extractObservedVariant([message])).toEqual({ kind: "unobserved", reason: "no-completed-assistant", }); - } expect(extractObservedVariant(null)).toEqual({ kind: "unobserved", reason: "endpoint-failure", @@ -309,26 +290,25 @@ describe("eval provenance", () => { expect( extractObservedVariant([ { - info: { role: "assistant", time: { completed: 2 }, variant: "high" }, + type: "assistant", + time: { completed: 2 }, + model: { variant: "high" }, }, - { info: { role: "assistant", time: { completed: 3 } } }, + { type: "assistant", time: { completed: 3 }, model: {} }, ]), ).toEqual({ kind: "unobserved", reason: "field-unavailable" }); }); - test("does not label missing variant evidence as a model identity conflict", () => { + test("does not label missing native variants as a model identity conflict", () => { const actor = extractObservedActor({ role: "manager", sessions: [ { id: "parent", - messages: ["first", "second"].map((modelID) => ({ - info: { - role: "assistant", - time: { completed: 2 }, - providerID: "route", - modelID, - }, + messages: ["first", "second"].map((id) => ({ + type: "assistant", + time: { completed: 2 }, + model: { providerID: "route", id }, })), }, ], diff --git a/tests/qualification-cli.test.ts b/tests/qualification-cli.test.ts index e53fef7b..0eb73139 100644 --- a/tests/qualification-cli.test.ts +++ b/tests/qualification-cli.test.ts @@ -1,9 +1,8 @@ import { expect, test } from "bun:test"; import { createHash } from "node:crypto"; -import { readFileSync } from "node:fs"; import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; -import { join } from "node:path"; +import { dirname, join } from "node:path"; import { canonicalJson } from "../evals/canonical-json.js"; import { mapStrings } from "../evals/cassette.js"; import { @@ -31,6 +30,7 @@ import { releaseCatalog, releaseGraderBundle, releaseHostConfigSha256, + releaseHostPolicy, releaseScenarioCatalog, } from "../evals/release-policy.js"; import { replayCassette } from "../evals/replay.js"; @@ -43,18 +43,41 @@ import { import { scenarioStepInstruction } from "../evals/scenario-steps.js"; import { SCENARIOS } from "../evals/scenarios.js"; import packageJson from "../package.json" with { type: "json" }; -import { prepareCanary, recordCanary } from "../scripts/eval-canary.js"; +import { + CANARY_MAX_AGE_MS, + canaryRecordSha256, + deriveCanaryResult, + parseCanaryRecord, +} from "../scripts/eval-canary.js"; import { materializeQualificationArchive } from "../scripts/materialize-qualification.js"; import { decisionRecordFor, qualifyV2 } from "../scripts/qualify-release.js"; import { assertQualificationBundle } from "../scripts/release-metadata.js"; -import { assuranceProjection } from "../src/application/delivery.js"; -import { SessionSchema } from "../src/application/schema.js"; -import { operationInputDigest } from "../src/domain/operation.js"; import { autoQualifiedOutcome } from "./fixtures/auto-qualified-outcome.js"; -test("current release defaults include three autonomous attempts", () => { - expect(packageJson.version).toBe("9.6.0"); +test("current release defaults retain 17 cases across two providers and three autonomous attempts", () => { + expect(packageJson.version).toBe("10.0.0"); + const scenarios = releaseScenarios(packageJson.version); + const catalog = releaseCatalog(packageJson.version); + expect(scenarios).toHaveLength(17); + expect([...new Set(catalog.map((row) => row.minProviders))]).toEqual([2]); + const plan = campaignPlanFor({ + models: ["openai/fixture-manager", "anthropic/fixture-manager"], + scenarios, + sampling: { kind: "release" }, + opencodeVersion: releaseHostPolicy(packageJson.version).opencodeVersion, + }); + expect(plan.cells.filter((cell) => cell.schedule === "primary")).toHaveLength( + 144, + ); + expect( + plan.cells.filter((cell) => cell.schedule === "environment-reserve"), + ).toHaveLength(34); + expect( + [ + ...new Set(plan.cells.map((cell) => cell.managerModel?.routeProvider)), + ].sort(), + ).toEqual(["anthropic", "openai"]); expect( attemptsForScenario("auto-two-features-evidence", { kind: "release" }), ).toBe(3); @@ -97,47 +120,86 @@ const FIXED_ROLES = [ "canary-transcript", ] as const; -function repairedCanarySession() { - const session = JSON.parse( - readFileSync( - join(import.meta.dir, "../evals/canary/artifacts/8.1.2-session.json"), - "utf8", - ), - ) as { - id: string; - closure: { - kind: "completed"; - summary: string; - operationId: string; - recordedRevision: number; +async function historicalCanaryFixture(input: { + readonly bundle: Awaited>; + readonly root: string; + readonly recordedAt: Date; + readonly hostVersion?: string; +}) { + const retainedFile = input.bundle.files.find( + (file) => file.ref.role === "canary-record", + ); + if (!retainedFile) throw new Error("Historical canary record is absent."); + const retained = parseCanaryRecord( + JSON.parse(retainedFile.bytes.toString("utf8")), + ); + if (!retained.ok) throw new Error(retained.issues.join("; ")); + const record = retained.value; + expect(record.artifact.packageVersion).toBe("9.5.0"); + const directory = join(input.root, "evals/canary"); + const artifacts = { ...record.artifacts }; + const evidence = new Map(); + for (const [kind, role] of [ + ["installation", "canary-installation"], + ["session", "canary-session"], + ["transcript", "canary-transcript"], + ] as const) { + const reference = record.artifacts[kind]; + const file = input.bundle.files.find((file) => file.ref.role === role); + if (!reference || !file) + throw new Error(`Historical ${kind} evidence is absent.`); + expect( + `sha256:${createHash("sha256").update(file.bytes).digest("hex")}`, + ).toBe(reference.sha256); + expect(file.bytes.byteLength).toBe(reference.bytes); + const value = JSON.parse(file.bytes.toString("utf8")); + if (kind === "transcript" && input.hostVersion) + value.info.version = input.hostVersion; + const bytes = + kind === "transcript" && input.hostVersion + ? Buffer.from(canonicalJson(value)) + : file.bytes; + evidence.set(kind, value); + artifacts[kind] = { + path: reference.path, + sha256: `sha256:${createHash("sha256").update(bytes).digest("hex")}`, + bytes: bytes.byteLength, }; - operations: Array<{ id: string; inputDigest: string }>; - runs: Array<{ - validations: Array<{ - observedAssertions?: Array<{ status: string }>; - }>; - }>; - }; - for (const run of session.runs) { - for (const validation of run.validations) { - for (const assertion of validation.observedAssertions ?? []) { - assertion.status = "passed"; - } - } + const path = join(directory, reference.path); + await mkdir(dirname(path), { recursive: true }); + await writeFile(path, bytes); } - const closureOperation = session.operations.find( - (operation) => operation.id === session.closure.operationId, - ); - if (!closureOperation) - throw new Error("Canary closure operation is missing."); - closureOperation.inputDigest = operationInputDigest({ - operationId: session.closure.operationId, - expectedRevision: session.closure.recordedRevision - 1, - sessionId: session.id, - kind: session.closure.kind, - summary: session.closure.summary, - }); - return SessionSchema.parse(session); + const derived = input.hostVersion + ? deriveCanaryResult({ + packageVersion: record.artifact.packageVersion, + artifactSha256: record.artifactSha256, + tarballSha256: record.artifact.tarballSha256, + preparedSha256: record.preparedSha256, + pluginEntrySha256: record.pluginEntrySha256, + installation: evidence.get("installation"), + session: evidence.get("session"), + transcript: evidence.get("transcript"), + }) + : record; + const { recordSha256: _retainedHash, ...retainedBase } = record; + const base = { + ...retainedBase, + status: derived.status, + checks: derived.checks, + actors: [...derived.actors], + hostConfigSha256: derived.hostConfigSha256, + recordedAt: input.recordedAt.toISOString(), + expiresAt: new Date( + input.recordedAt.getTime() + CANARY_MAX_AGE_MS, + ).toISOString(), + artifacts, + }; + const value = { ...base, recordSha256: canaryRecordSha256(base) }; + const parsed = parseCanaryRecord(value); + if (!parsed.ok) throw new Error(parsed.issues.join("; ")); + const path = join(directory, "9.5.0.json"); + await writeFile(path, canonicalJson(value)); + return { path, record: parsed.value }; } function retainedReplayOutcome( @@ -232,81 +294,6 @@ function inspectionAuditOutcome(): Outcome { }; } -function canaryTranscript(input: { - readonly fixture: string; - readonly packageVersion: string; - readonly pluginEntrySha256: string; - readonly session: ReturnType; -}) { - const call = (tool: string, output: unknown = {}) => ({ - type: "tool", - tool, - state: { status: "completed", input: {}, output }, - }); - return { - info: { - directory: input.fixture, - version: RELEASE_HOST_POLICY.opencodeVersion, - }, - messages: [ - { - info: { - role: "assistant", - agent: "build", - providerID: "fixture-provider", - modelID: "fixture-manager", - sessionID: "ses_manager1", - }, - parts: [ - call("flow_status", { - workflowData: { - runtimeIdentity: { - packageVersion: input.packageVersion, - pluginEntrySha256: input.pluginEntrySha256, - }, - }, - }), - call("flow_plan_save"), - call("flow_validation_start"), - call("flow_review_start"), - { - type: "tool", - tool: "task", - state: { - status: "completed", - input: { subagent_type: "flow-reviewer" }, - output: {}, - metadata: { - model: { - providerID: "fixture-provider", - modelID: "fixture-reviewer", - }, - parentSessionId: "ses_manager1", - sessionId: "ses_reviewer1", - }, - }, - }, - call("flow_session_close", { - workflowData: { - delivery: { assurance: assuranceProjection(input.session) }, - }, - }), - ], - }, - { - info: { - role: "assistant", - agent: "flow-reviewer", - providerID: "fixture-provider", - modelID: "fixture-reviewer", - sessionID: "ses_reviewer1", - }, - parts: [], - }, - ], - }; -} - test("qualifies and seals a historical 9.5 exact-artifact fixture campaign through the CLI", async () => { const repositoryRoot = join(import.meta.dir, ".."); const regradeAuthority = { @@ -562,29 +549,9 @@ test("qualifies and seals a historical 9.5 exact-artifact fixture campaign throu }); expect(report.attempts).toHaveLength(58); - const preparedDirectory = join(temporary, "prepared-canary"); - await mkdir(preparedDirectory, { recursive: true }); - const prepared = await prepareCanary({ - repositoryRoot, - artifactPath, - expectedArtifact: artifact, - outputDirectory: preparedDirectory, - preparedAt: recordedAt, - }); - const session = repairedCanarySession(); - const canaryRoot = join(temporary, "canary-root"); - const canary = await recordCanary({ - repositoryRoot: canaryRoot, - prepared, - preparedDirectory, - operator: "qualification-test", - session, - transcript: canaryTranscript({ - fixture: join(preparedDirectory, "fixture"), - packageVersion: artifact.packageVersion, - pluginEntrySha256: prepared.pluginEntrySha256, - session, - }), + const canary = await historicalCanaryFixture({ + bundle: historical, + root: join(temporary, "canary-root"), recordedAt, }); expect(canary.record.status).toBe("passed"); @@ -596,29 +563,16 @@ test("qualifies and seals a historical 9.5 exact-artifact fixture campaign throu }); if (diagnostic.decision.verdict !== "VERIFIED") throw new Error(JSON.stringify(diagnostic.decision.reasons)); - const currentHostTranscript = canaryTranscript({ - fixture: join(preparedDirectory, "fixture"), - packageVersion: artifact.packageVersion, - pluginEntrySha256: prepared.pluginEntrySha256, - session, - }); - const oldHostTranscript = { - ...currentHostTranscript, - info: { ...currentHostTranscript.info, version: "1.18.6" }, - }; - const oldHostCanary = await recordCanary({ - repositoryRoot: join(temporary, "old-host-canary-root"), - prepared, - preparedDirectory, - operator: "qualification-test", - session, - transcript: oldHostTranscript, + const oldHostCanary = await historicalCanaryFixture({ + bundle: historical, + root: join(temporary, "old-host-canary-root"), recordedAt, + hostVersion: "1.18.6", }); expect(oldHostCanary.record.status).toBe("failed"); const rejected = Bun.spawn( [ - "bun", + process.execPath, "run", "qualify", "--", @@ -642,7 +596,7 @@ test("qualifies and seals a historical 9.5 exact-artifact fixture campaign throu const bundlesDirectory = join(temporary, "bundles"); const qualified = Bun.spawn( [ - "bun", + process.execPath, "run", "qualify", "--", @@ -758,7 +712,7 @@ test("qualifies and seals a historical 9.5 exact-artifact fixture campaign throu ); const metadata = Bun.spawn( [ - "bun", + process.execPath, "run", join(repositoryRoot, "scripts/release-metadata.ts"), "--tag", diff --git a/tests/recovery-authority.test.ts b/tests/recovery-authority.test.ts index d5867369..f653e00b 100644 --- a/tests/recovery-authority.test.ts +++ b/tests/recovery-authority.test.ts @@ -1,4 +1,7 @@ -import { expect, test } from "bun:test"; +import { afterEach, expect, test } from "bun:test"; +import type { SessionMessageInfo } from "@opencode/client"; +import { Agent, Model, Provider } from "@opencode/plugin"; +import { Session } from "@opencode/schema/session"; import { createFlowService } from "../src/application/flow-service.js"; import type { DecisionAdvice, @@ -6,8 +9,20 @@ import type { } from "../src/application/ports/decision-provider.js"; import { RecoveryController } from "../src/application/recovery-policy.js"; import { createJevDecisionProvider } from "../src/infrastructure/jev-decision-provider.js"; -import { AutoDriveCoordinator } from "../src/platform/opencode/auto-drive.js"; -import { createCommandHook } from "../src/platform/opencode/command-hook.js"; +import { CompactionBoundary } from "../src/platform/opencode/auto-compaction.js"; +import { + AutoDriveCoordinator, + FLOW_AUTO_METADATA_KEY, +} from "../src/platform/opencode/auto-drive.js"; +import { InputAdmissions } from "../src/platform/opencode/input-admission.js"; +import { continuationCommands } from "./fixtures/continuation-commands.js"; +import { verifiedContinuationOrigin } from "./fixtures/verified-continuation-origin.js"; + +const cleanups: Array<() => Promise> = []; +afterEach(async () => { + for (const dispose of cleanups.splice(0)) await dispose(); +}); + import { approveSession, deterministicEnvironment, @@ -22,6 +37,11 @@ import { submitReview, } from "./runtime-test-support.js"; +function requiredSession(repository: MemorySessionRepository) { + if (!repository.session) throw new Error("Missing active test session."); + return repository.session; +} + async function failedTwice( count = 2, scopeBlocker = false, @@ -38,9 +58,9 @@ async function failedTwice( let findingId: string | undefined; for (let i = 0; i < count; i++) { if (i) await resetFeatureRun(manual, repository, FEATURE, "first-retry"); - await startReviewedRun(manual, repository, { suffix: "failure-" + i }); + await startReviewedRun(manual, repository, { suffix: `failure-${i}` }); await submitReview(manual, repository, { - suffix: "failure-" + i, + suffix: `failure-${i}`, summary: "Guard missing", verdict: "failed", findings: [ @@ -81,35 +101,32 @@ test.each(["off", "no-key", "shadow"] as const)( }), prompt: async () => {}, }); - const hook = createCommandHook({ + const commands = await continuationCommands({ autoDrive: auto, recovery: controller, flow: s.manual, - assertOperational() {}, defaultRecovery: () => null, }); + cleanups.push(commands.dispose); const settings = mode === "shadow" ? "--recovery=shadow --recovery-calls=6 --recovery-usd=0.02 " : mode === "off" ? "--recovery=off " : ""; - await hook( - { - command: "flow-auto", - sessionID: "host", - arguments: settings + "Continue approved goal", - }, - { parts: [] }, + await commands.execute("flow-auto", `${settings}Continue approved goal`); + const native = await commands.native.toolContext( + "flow_status", + {}, + "ses_host", ); - controller.observeMessage("host", "original-user", false); - controller.observeAssistant("host", "manager", "original-user"); + await commands.lifecycle.beforeTool(native); const guarded = createFlowService( s.repository, s.env, controller.guard({ - hostSessionId: "host", - messageId: "manager", + hostSessionId: "ses_host", + messageId: native.messageID, agent: "build", }), ); @@ -141,17 +158,17 @@ function automatic( { now }, ); controller.activate( - "host", + "ses_host", mode === "off" ? null : { mode: "shadow", maxCalls: 6, maxUsd: 0.02 }, ); - controller.observeMessage("host", "original", false); + controller.observeMessage("ses_host", "original", false); const service = (id: string, parent: string) => { - controller.observeAssistant("host", id, parent); + controller.observeAssistant("ses_host", id, parent); return createFlowService( s.repository, s.env, controller.guard({ - hostSessionId: "host", + hostSessionId: "ses_host", messageId: id, agent: "build", }), @@ -188,9 +205,13 @@ test("configured shadow without key reports unavailable without claiming an atte const controller = new RecoveryController( createJevDecisionProvider(() => undefined), ); - controller.activate("host", { mode: "shadow", maxCalls: 6, maxUsd: 0.02 }); - controller.observeMessage("host", "original", false); - controller.observeAssistant("host", "manager", "original"); + controller.activate("ses_host", { + mode: "shadow", + maxCalls: 6, + maxUsd: 0.02, + }); + controller.observeMessage("ses_host", "original", false); + controller.observeAssistant("ses_host", "manager", "original"); expect(controller.snapshot()).toMatchObject({ advice: { configured: true, attempted: false, outcome: "not-attempted" }, }); @@ -198,7 +219,7 @@ test("configured shadow without key reports unavailable without claiming an atte s.repository, s.env, controller.guard({ - hostSessionId: "host", + hostSessionId: "ses_host", messageId: "manager", agent: "build", }), @@ -238,11 +259,15 @@ test.each(["trusted-continuation", "expiry"] as const)( }, }; const controller = new RecoveryController(provider, { now: () => now }); - controller.activate("host", { mode: "shadow", maxCalls: 6, maxUsd: 0.02 }); - controller.observeMessage("host", "original", false); - controller.observeAssistant("host", "manager", "original"); + controller.activate("ses_host", { + mode: "shadow", + maxCalls: 6, + maxUsd: 0.02, + }); + controller.observeMessage("ses_host", "original", false); + controller.observeAssistant("ses_host", "manager", "original"); const guard = controller.guard({ - hostSessionId: "host", + hostSessionId: "ses_host", messageId: "manager", agent: "build", }); @@ -258,7 +283,8 @@ test.each(["trusted-continuation", "expiry"] as const)( if (boundary === "expiry") { now = 2 * 60 * 60 * 1000; controller.snapshot(); - } else controller.observeMessage("host", "authenticated-next", true, true); + } else + controller.observeMessage("ses_host", "authenticated-next", true, true); expect(signal?.aborted).toBe(true); release({ kind: "answered", @@ -277,12 +303,12 @@ test.each(["trusted-continuation", "expiry"] as const)( advice: { attemptsReserved: 1 }, }); const parent = boundary === "expiry" ? "original" : "authenticated-next"; - controller.observeAssistant("host", "next-manager", parent); + controller.observeAssistant("ses_host", "next-manager", parent); const protectedFlow = createFlowService( s.repository, s.env, controller.guard({ - hostSessionId: "host", + hostSessionId: "ses_host", messageId: "next-manager", agent: "build", }), @@ -316,7 +342,7 @@ test.each(["off", "shadow"] as const)( }, }); expect(reset.status).toBe("ok"); - a.controller.observeMessage("host", "continuation", true, true); + a.controller.observeMessage("ses_host", "continuation", true, true); const started = await a.service("start", "continuation").runStart({ request: { operationId: "first-auto-start", @@ -336,7 +362,7 @@ test.each(["off", "shadow"] as const)( async (mode) => { const s = await failedTwice(); const a = automatic(s, mode); - a.controller.observeMessage("host", "fresh-user", false); + a.controller.observeMessage("ses_host", "fresh-user", false); const flow = a.service("fresh-manager", "fresh-user"); const resetRequest = { operationId: "manual-extra-reset", @@ -421,7 +447,7 @@ test.each(["off", "shadow"] as const)( async (mode) => { const s = await failedTwice(); const a = automatic(s, mode); - a.controller.observeMessage("host", "original", false); + a.controller.observeMessage("ses_host", "original", false); const original = a.service("original-manager", "original"); expect( ( @@ -434,7 +460,7 @@ test.each(["off", "shadow"] as const)( }) ).status, ).toBe("error"); - a.controller.observeMessage("host", "untrusted", true); + a.controller.observeMessage("ses_host", "untrusted", true); expect( ( await a.service("untrusted-manager", "untrusted").featureReset({ @@ -446,7 +472,7 @@ test.each(["off", "shadow"] as const)( }) ).status, ).toBe("error"); - a.controller.observeMessage("host", "trusted-continuation", true, true); + a.controller.observeMessage("ses_host", "trusted-continuation", true, true); expect( ( await a @@ -463,7 +489,7 @@ test.each(["off", "shadow"] as const)( expect( a.controller .guard({ - hostSessionId: "host", + hostSessionId: "ses_host", messageId: "trusted-manager", agent: "build", }) @@ -488,7 +514,7 @@ test.each(["off", "shadow"] as const)( }) ).status, ).toBe("error"); - a.controller.observeMessage("host", "approved-scope-user", false); + a.controller.observeMessage("ses_host", "approved-scope-user", false); expect( ( await a @@ -540,20 +566,20 @@ test.each(["off", "shadow"] as const)( featureId: FEATURE, }, }); - a.controller.revoke("host"); + a.controller.revoke("ses_host"); expect( ( await old.sessionClose({ request: { operationId: "old-close", expectedRevision: revision(s.repository), - sessionId: s.repository.session!.id, + sessionId: requiredSession(s.repository).id, kind: "deferred", }, }) ).status, ).toBe("error"); - a.controller.observeMessage("host", "fresh-close-user", false); + a.controller.observeMessage("ses_host", "fresh-close-user", false); expect( ( await a @@ -562,7 +588,7 @@ test.each(["off", "shadow"] as const)( request: { operationId: "fresh-close", expectedRevision: revision(s.repository), - sessionId: s.repository.session!.id, + sessionId: requiredSession(s.repository).id, kind: "deferred", }, }) @@ -585,7 +611,7 @@ test.each(["off", "shadow"] as const)( }, }); s.repository.session = { - ...s.repository.session!, + ...requiredSession(s.repository), goal: "Replacement goal", }; const result = await flow.featureReset({ @@ -674,7 +700,7 @@ test.each(["off", "shadow"] as const)( request: { operationId: "close-repaired", expectedRevision: revision(s.repository), - sessionId: s.repository.session!.id, + sessionId: requiredSession(s.repository).id, kind: "completed", }, }) @@ -744,7 +770,7 @@ for (const scopeBlocker of [false, true]) { }); } -test("simulated missing parentage fails closed for auto but fresh manual flow-run still starts", async () => { +test("unmatched native requests fail closed and fresh flow-run restores a verified origin", async () => { const s = await failedTwice(0); const controller = new RecoveryController({ async assess() { @@ -761,82 +787,52 @@ test("simulated missing parentage fails closed for auto but fresh manual flow-ru }), prompt: async () => {}, }); - const hook = createCommandHook({ + const commands = await continuationCommands({ autoDrive: auto, recovery: controller, flow: s.manual, - assertOperational() {}, - defaultRecovery: () => null, - }); - auto.observeHostMessage("host", { - id: "unsupported-assistant", - role: "assistant", }); - const parts: Parameters[1] = { parts: [] }; - await hook( - { - command: "flow-auto", - sessionID: "host", - arguments: "Continue approved goal", - }, - parts, - ); - expect(JSON.stringify(parts)).toContain( - "automatic mutations cannot be attributed", - ); - controller.observeMessage("host", "auto-user", false); + cleanups.push(commands.dispose); + await commands.execute("flow-auto", "Continue approved goal"); const unavailable = createFlowService( s.repository, s.env, controller.guard({ - hostSessionId: "host", - messageId: "unparented", + hostSessionId: "ses_host", + messageId: "msg_unattributed", agent: "build", }), ); - expect( - ( - await unavailable.runStart({ - request: { - operationId: "unattributed-start", - expectedRevision: revision(s.repository), - featureId: FEATURE, - }, - }) - ).status, - ).toBe("error"); - await hook( - { - command: "flow-run", - sessionID: "host", - arguments: "Continue approved goal", - }, - { parts: [] }, + const request = { + operationId: "unattributed-start", + expectedRevision: revision(s.repository), + featureId: FEATURE, + }; + expect((await unavailable.runStart({ request })).status).toBe("error"); + await commands.execute("flow-run", "Continue approved goal"); + const native = await commands.native.toolContext( + "flow_run_start", + {}, + "ses_host", ); - controller.observeMessage("host", "manual-user", false); + await commands.lifecycle.beforeTool(native); const manual = createFlowService( s.repository, s.env, controller.guard({ - hostSessionId: "host", - messageId: "manual-unparented", + hostSessionId: "ses_host", + messageId: native.messageID, agent: "build", }), ); expect( ( await manual.runStart({ - request: { - operationId: "manual-start", - expectedRevision: revision(s.repository), - featureId: FEATURE, - }, + request: { ...request, operationId: "manual-start" }, }) ).status, ).toBe("ok"); - expect(controller.snapshot()).toMatchObject({ - automation: { active: false }, - }); + expect(controller.snapshot()).toMatchObject({ automation: { active: true } }); }); test.each(["success", "failure"] as const)( @@ -844,7 +840,7 @@ test.each(["success", "failure"] as const)( async (outcome) => { const s = await failedTwice(); const a = automatic(s, "off"); - const id = s.repository.session!.id, + const id = requiredSession(s.repository).id, current = revision(s.repository); let projection = { sessionId: id, @@ -889,41 +885,41 @@ test.each(["success", "failure"] as const)( agent: "build", model: { providerID: "fixture", modelID: "scripted" }, }; - const initial = await driver.activate("host"); + const initial = await driver.activate("ses_host"); await driver.observeMessage( - "host", + "ses_host", delivery, [{ type: "text", synthetic: true, metadata: initial }], "original", ); - await driver.onIdle("host"); + await driver.onIdle("ses_host"); projection = { sessionId: id, status: "blocked", revision: current, nextAction: "await-user-direction", }; - const idle = driver.onIdle("host"); + const idle = driver.onIdle("ses_host"); await pending; await driver.observeMessage( - "host", + "ses_host", delivery, [{ type: "text", text: "Retry the approved task once" }], "fresh-user", ); - a.controller.observeMessage("host", "fresh-user", false); + a.controller.observeMessage("ses_host", "fresh-user", false); const observed = await driver.observeMessage( - "host", + "ses_host", delivery, [{ type: "text", synthetic: true, metadata: lateMetadata }], "late-handback", ); if (observed === "accepted-continuation") - a.controller.observeMessage("host", "late-handback", true, true); + a.controller.observeMessage("ses_host", "late-handback", true, true); release(); await idle; expect(observed).toBe("stale-continuation"); - expect(driver.compactionContext("host")).not.toBeNull(); + expect(driver.compactionContext("ses_host")).not.toBeNull(); const fresh = a.service("fresh-manager", "fresh-user"); const reset = { operationId: "fresh-race-reset", @@ -943,7 +939,7 @@ test.each(["success", "failure"] as const)( }) ).status, ).toBe("ok"); - expect(s.repository.session!.runs).toHaveLength(3); + expect(requiredSession(s.repository).runs).toHaveLength(3); await recordObservedValidation(s.repository, { captureId: "race-third-pass", }); @@ -956,8 +952,9 @@ test.each(["success", "failure"] as const)( packet: { summary: "Independent retry review", riskLenses: [] }, }, }); - const findingId = - s.repository.session!.runs[0]!.reviews[0]!.result!.findings[0]!.findingId; + const findingId = requiredSession(s.repository).runs[0]?.reviews[0]?.result + ?.findings[0]?.findingId; + if (!findingId) throw new Error("Missing source-owned finding."); await submitReview(s.manual, s.repository, { suffix: "race-third-failed", summary: "Guard still missing", @@ -1016,23 +1013,23 @@ test("obsolete initial prompt failure preserves a renewed checkpoint reply", asy agent: "build", model: { providerID: "fixture", modelID: "scripted" }, }; - const metadata = await driver.activate("host"); + const metadata = await driver.activate("ses_host"); await driver.observeMessage( - "host", + "ses_host", delivery, [{ type: "text", synthetic: true, metadata }], "original", ); - const idle = driver.onIdle("host"); + const idle = driver.onIdle("ses_host"); await pending; await driver.observeMessage( - "host", + "ses_host", delivery, [{ type: "text", text: "Use the clarified approved goal" }], "fresh-user", ); const observed = await driver.observeMessage( - "host", + "ses_host", delivery, [{ type: "text", synthetic: true, metadata: initialMetadata }], "late-initial", @@ -1040,7 +1037,7 @@ test("obsolete initial prompt failure preserves a renewed checkpoint reply", asy release(); await idle; expect(observed).toBe("stale-continuation"); - expect(driver.compactionContext("host")).not.toBeNull(); + expect(driver.compactionContext("ses_host")).not.toBeNull(); }); test.each([ @@ -1068,53 +1065,107 @@ test.each([ agent: "build", model: { providerID: "fixture", modelID: "scripted" }, }; - const metadata = await driver.activate("host"); + const metadata = await driver.activate("ses_host"); await driver.observeMessage( - "host", + "ses_host", delivery, [{ type: "text", synthetic: true, metadata }], "original", ); - driver.observeHostMessage("host", { - id: "manager", - role: "assistant", - parentID: "original", - }); - driver.observeHostPart("host", { - type: "compaction", - messageID: "compaction", - auto: true, - }); - const summary = { - id: "summary", - role: "assistant", - parentID: "compaction", - summary: true, + await verifiedContinuationOrigin( + driver, + "ses_host", + "original", + "msg_manager", + delivery, + ); + const epoch = driver.captureAuthority("ses_host"); + if (!epoch) throw new Error("Missing admitted automatic epoch."); + const running = { + type: "compaction" as const, + id: "msg_compaction", + status: "running" as const, + reason: "auto" as const, + summary: "Summary is not fresh direction", + time: { created: 0 }, }; - driver.observeHostMessage("host", summary); - driver.observeHostMessage("host", { - id: "original", - role: "user", - summary: { diffs: [] }, - }); - driver.observeHostMessage("host", summary); - driver.observeHostMessage("host", { id: "successor", role: "user" }); - driver.observeHostMessage("host", { - id: "original", - role: "user", - summary: { diffs: [] }, + const host = Session.ID.make("ses_host"); + const nativeAgent = Agent.ID.make(delivery.agent); + const nativeModel = { + providerID: Provider.ID.make(delivery.model.providerID), + id: Model.ID.make(delivery.model.modelID), + }; + const token = metadata[FLOW_AUTO_METADATA_KEY]; + if (typeof token !== "string") + throw new Error("Missing exact admitted token."); + const payload = { + text: "Source-bound fixture request", + metadata: { [FLOW_AUTO_METADATA_KEY]: token }, + }; + const inputs = new InputAdmissions({ + assertOpen: () => {}, + authority: () => driver.captureAuthority(host), + authorityCurrent: (authority) => driver.isAuthorityCurrent(authority), + observeReal: async () => {}, }); - driver.observeHostPart("host", { - type: "text", - messageID: "successor", - synthetic: true, - metadata: { compaction_continue: true }, + const reservation = inputs.reserve(host, "original"); + const admission = inputs.register(reservation, { type: "user", payload }); + await inputs.accept( + admission, + { + sessionID: host, + id: "original", + type: "user", + payload, + time: { created: 0 }, + delivery: "queue", + }, + async () => + inputs.makeOrigin(host, "original", delivery, true, reservation.epoch), + ); + const native: SessionMessageInfo[] = [ + { type: "user", id: "original", ...payload, time: { created: 0 } }, + running, + ]; + const context = { + sessionID: host, + agent: nativeAgent, + model: nativeModel, + system: [], + options: {}, + tools: {}, + messages: [{ role: "user" as const, id: "original", content: [] }], + }; + const boundary = new CompactionBoundary({ + inputs, + readNative: async () => structuredClone(native), + failure: () => {}, + hasActivePrimary: () => false, + guidance: () => driver.compactionContext(host), + observe: (host, origin, source, id) => { + if ( + origin.authority && + !driver.isAuthorityCurrent(origin.authority, id) + ) + driver.observeCompaction(host, origin.authority, source, id); + }, }); - driver.observeCompaction("host"); - expect(driver.compactionContext("host")).not.toBeNull(); + const checked = await boundary.admit(context); + expect(await boundary.admit(context)).toBe(checked); + native.splice(0, native.length, { ...running, status: "completed" }); + const completed = { + ...context, + messages: [{ role: "user" as const, id: checked.id, content: [] }], + }; + const origin = boundary.completed(completed, native); + expect(boundary.completed(completed, native)).toBe(origin); + expect(origin?.authority).toEqual(epoch); + inputs.clear(); + + expect(driver.compactionContext("ses_host")).not.toBeNull(); const before = revision(state.repository); const result = await authority - .service("successor-manager", "successor") + .service("successor-manager", "msg_compaction") .featureReset({ request: { operationId: "after-native-compaction", diff --git a/tests/recovery-capture.test.ts b/tests/recovery-capture.test.ts index 7f5fdc2a..af2b9773 100644 --- a/tests/recovery-capture.test.ts +++ b/tests/recovery-capture.test.ts @@ -9,24 +9,20 @@ import { rename, rm, symlink, - writeFile, } from "node:fs/promises"; import { tmpdir } from "node:os"; -import { dirname, join } from "node:path"; -import type { ToolContext } from "@opencode-ai/plugin"; +import { join } from "node:path"; import { z } from "zod"; import { CapturingRecoveryController, RawRecoveryCaptureSchema, } from "../evals/recovery-decisions/capture.js"; -import CapturePlugin from "../evals/recovery-decisions/capture-plugin.js"; import { importSnapshot } from "../evals/recovery-decisions/dataset.js"; import { datasetDigest } from "../evals/recovery-decisions/schema.js"; import { RecoveryController, RecoveryProposalSchema, } from "../src/application/recovery-policy.js"; -import FlowPlugin from "../src/index.js"; import { frozenCampaignFixture } from "./recovery-campaign-support.js"; const roots: string[] = []; @@ -226,83 +222,38 @@ test("concurrent reservations cap records while retaining rejected proposal inpu expect(new Set((await records(f.directory)).map((r) => r.id)).size).toBe(128); }); -function pluginContext(workspace: string) { - return { - directory: workspace, - worktree: workspace, - project: {}, - serverUrl: new URL("http://localhost"), - $: {}, - experimental_workspace: { register() {} }, - client: { - app: { log() {} }, - session: { - message: async () => ({ data: undefined }), - get: async (input: { path: { id: string } }) => ({ - data: { id: input.path.id, directory: workspace }, - }), - }, +test("native capture setup forwards reviewer options and injects the capturing controller", async () => { + const root = await mkdtemp(join(tmpdir(), "native-capture-setup-")); + roots.push(root); + const workspace = join(root, "workspace"); + await mkdir(workspace); + const child = Bun.spawn( + [ + process.execPath, + "tests/fixtures/opencode-v2-capture-setup.ts", + workspace, + join(root, "captures"), + ], + { + cwd: join(import.meta.dir, ".."), + stdout: "pipe", + stderr: "pipe", + env: { PATH: process.env.PATH, HOME: root }, }, - } as unknown as Parameters[0]; -} -function toolContext(workspace: string): ToolContext { - return { - directory: workspace, - worktree: workspace, - sessionID: "host", - messageID: "assistant", - agent: "build", - abort: new AbortController().signal, - metadata() {}, - async ask() {}, - }; -} -test("actual opt-in flow_status captures exact workspace inputs; ordinary plugin does not", async () => { - const f = await setup(); - for (const [name, bytes] of Object.entries(f.fixture.files)) { - await mkdir(dirname(join(f.workspace, name)), { recursive: true }); - await writeFile(join(f.workspace, name), bytes); - } - for (const args of [ - ["init", "--initial-branch=main"], - ["add", "."], - ]) - expect(Bun.spawnSync(["git", "-C", f.workspace, ...args]).exitCode).toBe(0); - const ordinary = await FlowPlugin(pluginContext(f.workspace)); - try { - await ordinary.tool?.flow_status?.execute( - { request: { view: "compact" }, recoveryProposal: f.proposal }, - toolContext(f.workspace), - ); - expect(await readdir(f.root)).toEqual(["workspace"]); - } finally { - await ordinary.dispose?.(); - } - const hooks = await CapturePlugin(pluginContext(f.workspace), { - captureDirectory: f.directory, - reviewer: { model: "provider/independent-reviewer", steps: 80 }, + ); + const [exitCode, output, error] = await Promise.all([ + child.exited, + new Response(child.stdout).text(), + new Response(child.stderr).text(), + ]); + expect({ exitCode, error }).toEqual({ exitCode: 0, error: "" }); + expect(JSON.parse(output)).toEqual({ + pluginId: "flow-eval-capture", + forwarded: { + reviewer: { model: "provider/independent-reviewer", steps: 80 }, + }, + capturing: true, }); - try { - const config: Parameters>[0] = {}; - await hooks.config?.(config); - expect(config.agent?.["flow-reviewer"]).toMatchObject({ - model: "provider/independent-reviewer", - steps: 80, - }); - const result = await hooks.tool?.flow_status?.execute( - { request: { view: "compact" }, recoveryProposal: f.proposal }, - toolContext(f.workspace), - ); - expect(result).toContain("explicit active recovery command"); - const [record] = await records(f.directory); - expect(record?.payload).toEqual({ - session: f.session, - sourceDigest: f.sourceDigest, - proposal: f.proposal, - }); - } finally { - await hooks.dispose?.(); - } }); test("encoded record and total byte bounds are enforced before dispatch", async () => { diff --git a/tests/recovery-episode-host.test.ts b/tests/recovery-episode-host.test.ts index 8e3d0e6f..d7e82df0 100644 --- a/tests/recovery-episode-host.test.ts +++ b/tests/recovery-episode-host.test.ts @@ -163,6 +163,87 @@ async function setup(end: "quiet" | "escalated" = "quiet") { }; } +test("native credential inheritance freezes the selected snapshot and preserves rotation delivery", async () => { + const f = await setup(); + await expect( + createEpisodeHostDriver({ + ...f.options, + host: { ...f.options.host, providerCredentials: "inherit" }, + }), + ).rejects.toThrow("explicit credentialHandoff"); + expect(f.starts).toBe(0); + const credential = { + id: "credential_fixture", + integrationID: "test", + label: "Local fixture", + active: true, + value: { type: "key" as const, key: "synthetic-before" }, + }; + const entries = [credential]; + const rotations: unknown[] = []; + const handoff = { + entries, + onStop: async (value: unknown) => { + rotations.push(value); + }, + }; + await expect( + createEpisodeHostDriver({ + ...f.options, + host: { + ...f.options.host, + providerCredentials: "disabled", + credentialHandoff: handoff, + }, + }), + ).rejects.toThrow("Native credentials are disabled"); + const factory = f.options.hostFactory; + if (!factory) throw new Error("Missing fixture host factory"); + const driver = await createEpisodeHostDriver({ + ...f.options, + host: { + ...f.options.host, + providerCredentials: "inherit", + credentialHandoff: handoff, + }, + hostFactory: async (options) => { + const host = await factory(options); + return { + ...host, + async stop() { + await host.stop(); + await options.credentialHandoff?.onStop({ + before: options.credentialHandoff.entries, + after: [ + { + ...credential, + value: { type: "key", key: "synthetic-after" }, + }, + ], + }); + }, + }; + }, + }); + credential.value.key = "synthetic-mutated-after-registration"; + await driver.prepare(new AbortController().signal); + expect(f.startOptions?.credentialHandoff?.entries[0]?.value).toEqual({ + type: "key", + key: "synthetic-before", + }); + await driver.stop(); + expect(rotations).toEqual([ + { + before: [ + { ...entries[0], value: { type: "key", key: "synthetic-before" } }, + ], + after: [ + { ...entries[0], value: { type: "key", key: "synthetic-after" } }, + ], + }, + ]); +}); + test("host adapter binds actual seeded files, manager request, exact completion and idempotent stop", async () => { const f = await setup(), driver = await createEpisodeHostDriver(f.options); @@ -176,7 +257,6 @@ test("host adapter binds actual seeded files, manager request, exact completion expect(f.startOptions).toMatchObject({ ambientConfig: "disabled", reviewerEnvironment: "disabled", - nativeLlm: false, withFlow: true, }); await driver.run({ @@ -663,7 +743,11 @@ test("live source driver binds credential policy and preserves runtime scope whi const driver = await createEpisodeHostDriver(f.options); const inherited = await createEpisodeHostDriver({ ...f.options, - host: { ...f.options.host, providerCredentials: "inherit" }, + host: { + ...f.options.host, + providerCredentials: "inherit", + credentialHandoff: { entries: [], onStop: async () => {} }, + }, }); expect(driver.origin).toBe("live"); expect(driver.harnessDigest).not.toBe(inherited.harnessDigest); diff --git a/tests/recovery-host.test.ts b/tests/recovery-host.test.ts index 0cb067c2..f1b9f972 100644 --- a/tests/recovery-host.test.ts +++ b/tests/recovery-host.test.ts @@ -1,10 +1,20 @@ -import { expect, test } from "bun:test"; +import { afterEach, expect, test } from "bun:test"; +import { + createFlowService, + type FlowService, +} from "../src/application/flow-service.js"; import { RecoveryController } from "../src/application/recovery-policy.js"; import { AutoDriveCoordinator, type AutoDriveProjection, FLOW_AUTO_METADATA_KEY, } from "../src/platform/opencode/auto-drive.js"; +import { continuationCommands } from "./fixtures/continuation-commands.js"; +import { verifiedContinuationOrigin } from "./fixtures/verified-continuation-origin.js"; +import { + deterministicEnvironment, + MemorySessionRepository, +} from "./runtime-test-support.js"; const delivery = { agent: "build", @@ -15,9 +25,68 @@ const unavailable = { return { kind: "unavailable" as const, reason: "simulation" }; }, }; +const cleanups: Array<() => Promise> = []; +afterEach(async () => { + for (const dispose of cleanups.splice(0)) await dispose(); +}); +async function commandFixture( + options: { + flow?: FlowService; + readProjection?: () => Promise; + defaultRecovery?: () => { + mode: "shadow"; + maxCalls: number; + maxUsd: number; + } | null; + now?: () => number; + } = {}, +) { + let projection: AutoDriveProjection = { + status: "idle", + revision: 0, + nextAction: "flow_plan_save", + }; + const recovery = new RecoveryController( + unavailable, + options.now ? { now: options.now } : {}, + ); + const flow = + options.flow ?? + createFlowService( + new MemorySessionRepository(), + deterministicEnvironment(), + ); + const prompts: string[] = []; + const auto = new AutoDriveCoordinator({ + recovery, + readProjection: options.readProjection ?? (async () => projection), + prompt: async (_host, prompt) => { + prompts.push(prompt); + }, + }); + const commands = await continuationCommands({ + autoDrive: auto, + recovery, + flow, + ...(options.defaultRecovery + ? { defaultRecovery: options.defaultRecovery } + : {}), + }); + cleanups.push(commands.dispose); + return { + auto, + recovery, + commands, + prompts, + setProjection(value: AutoDriveProjection) { + projection = value; + }, + }; +} + test("fast recovery continuation receipt survives enqueue and advances to next feature", async () => { const recovery = new RecoveryController(unavailable); - recovery.activate("host", { mode: "shadow", maxCalls: 2, maxUsd: 0.01 }); + recovery.activate("ses_host", { mode: "shadow", maxCalls: 2, maxUsd: 0.01 }); let projection: AutoDriveProjection = { sessionId: "flow", status: "blocked", @@ -34,23 +103,25 @@ test("fast recovery continuation receipt survives enqueue and advances to next f host, model, [{ type: "text", synthetic: true, metadata }], - `continuation-${prompts}`, + `msg_continuation-${prompts}`, + ); + await verifiedContinuationOrigin( + auto, + host, + `msg_continuation-${prompts}`, + `msg_assistant-${prompts}`, + model, ); - auto.observeHostMessage(host, { - id: `assistant-${prompts}`, - role: "assistant", - parentID: `continuation-${prompts}`, - }); }, }); - const metadata = await auto.activate("host"); + const metadata = await auto.activate("ses_host"); await auto.observeMessage( - "host", + "ses_host", delivery, [{ type: "text", synthetic: true, metadata }], - "initial", + "msg_initial", ); - await auto.onIdle("host"); + await auto.onIdle("ses_host"); expect(prompts).toBe(1); projection = { sessionId: "flow", @@ -58,15 +129,20 @@ test("fast recovery continuation receipt survives enqueue and advances to next f revision: 14, nextAction: "flow_run_start", }; - auto.observeMutation("host", 14, undefined, "assistant-1", false); - await auto.onIdle("host"); + auto.observeMutation("ses_host", 14, undefined, "msg_assistant-1", false); + await auto.onIdle("ses_host"); expect(prompts).toBe(2); - expect(auto.compactionContext("host")).not.toBeNull(); + expect(auto.compactionContext("ses_host")).not.toBeNull(); }); + test("recovery never overrides unowned-session stop and prompt failure revokes", async () => { for (const fault of ["replacement", "prompt"]) { const recovery = new RecoveryController(unavailable); - recovery.activate("host", { mode: "shadow", maxCalls: 2, maxUsd: 0.01 }); + recovery.activate("ses_host", { + mode: "shadow", + maxCalls: 2, + maxUsd: 0.01, + }); let prompts = 0; let projection: AutoDriveProjection = { sessionId: "flow", @@ -82,298 +158,212 @@ test("recovery never overrides unowned-session stop and prompt failure revokes", throw new Error("host unavailable"); }, }); - const metadata = await auto.activate("host"); + const metadata = await auto.activate("ses_host"); expect(metadata[FLOW_AUTO_METADATA_KEY]).toBeDefined(); await auto.observeMessage( - "host", + "ses_host", delivery, [{ type: "text", synthetic: true, metadata }], - "initial", + "msg_initial", ); if (fault === "replacement") projection = { ...projection, sessionId: "other" }; - await auto.onIdle("host"); + await auto.onIdle("ses_host"); expect(prompts).toBe(fault === "replacement" ? 0 : 1); expect(recovery.snapshot()).toMatchObject({ mode: "off" }); } }); -test("real command hook captures explicit shadow limits and stop revokes", async () => { - const { createCommandHook } = await import( - "../src/platform/opencode/command-hook.js" - ); - const { createFlowService } = await import( - "../src/application/flow-service.js" - ); - const { MemorySessionRepository, deterministicEnvironment } = await import( - "./runtime-test-support.js" - ); +test("shadow recovery prompt does not add a second handback at one checkpoint", async () => { const recovery = new RecoveryController(unavailable); - const flow = createFlowService( - new MemorySessionRepository(), - deterministicEnvironment(), - ); + recovery.activate("ses_host", { mode: "shadow", maxCalls: 2, maxUsd: 0.01 }); + const prompts: string[] = []; + let revision = 10; const auto = new AutoDriveCoordinator({ recovery, readProjection: async () => ({ - status: "idle", - revision: 0, - nextAction: "flow_plan_save", + sessionId: "flow", + status: "blocked", + revision, + nextAction: "await-user-direction", }), - prompt: async () => { - throw new Error("not used"); + prompt: async (_host, prompt) => { + prompts.push(prompt); }, }); - const visibleRefusals: string[] = []; - const hook = createCommandHook({ + const metadata = await auto.activate("ses_host"); + await auto.observeMessage( + "ses_host", + delivery, + [{ type: "text", synthetic: true, metadata }], + "msg_initial", + ); + await auto.onIdle("ses_host"); + expect(prompts).toHaveLength(1); + await auto.onIdle("ses_host"); + expect(prompts).toHaveLength(1); + revision = 11; + await auto.onIdle("ses_host"); + expect(prompts).toHaveLength(2); + await auto.onIdle("ses_host"); + expect(prompts).toHaveLength(2); +}); + +test("running state cannot trigger Jev even with a direction-shaped next action", async () => { + const recovery = new RecoveryController(unavailable); + recovery.activate("ses_host", { mode: "shadow", maxCalls: 2, maxUsd: 0.01 }); + const prompts: string[] = []; + const auto = new AutoDriveCoordinator({ recovery, - autoDrive: auto, - flow, - assertOperational() {}, - async showRefusal(message) { - visibleRefusals.push(message); + readProjection: async () => ({ + sessionId: "flow", + status: "running", + revision: 10, + nextAction: "await-user-direction", + }), + prompt: async (_host, prompt) => { + prompts.push(prompt); }, }); - const output = { parts: [] } as Parameters[1]; - await hook( - { - command: "flow-auto", - sessionID: "host", - arguments: - "--recovery=shadow --recovery-calls=2 --recovery-usd=0.01 Fix parser", - }, - output, + const metadata = await auto.activate("ses_host"); + await auto.observeMessage( + "ses_host", + delivery, + [{ synthetic: true, metadata }], + "msg_initial", ); - expect(recovery.snapshot()).toMatchObject({ + await auto.onIdle("ses_host"); + expect(prompts).toEqual([]); + expect(recovery.snapshot("ses_host")).toMatchObject({ + mode: "shadow", + remainingCalls: 2, + reservedUsd: 0, + }); +}); + +test("accepted native command captures explicit shadow limits and stop revokes", async () => { + const f = await commandFixture(); + const accepted = await f.commands.execute( + "flow-auto", + "--recovery=shadow --recovery-calls=2 --recovery-usd=0.01 Fix parser", + ); + expect(f.recovery.snapshot()).toMatchObject({ mode: "shadow", remainingCalls: 2, maxUsd: 0.01, }); - expect(JSON.stringify(output)).not.toContain("--recovery"); - expect(JSON.stringify(output)).toContain("Fix parser"); - await hook({ command: "flow-auto", sessionID: "host", arguments: "stop" }, { - parts: [], - } as Parameters[1]); - expect(recovery.snapshot()).toMatchObject({ mode: "off" }); + expect(accepted.text.split("\n")[0]).toBe("Flow flow-auto: Fix parser"); + expect(accepted.text).not.toContain("--recovery"); + await f.commands.execute("flow-auto", "stop"); + expect(f.recovery.snapshot()).toMatchObject({ mode: "off" }); await expect( - hook( - { - command: "flow-auto", - sessionID: "host", - arguments: - "--recovery=delegated --recovery-calls=2 --recovery-usd=0.01 Fix parser", - }, - { parts: [] } as Parameters[1], + f.commands.execute( + "flow-auto", + "--recovery=delegated --recovery-calls=2 --recovery-usd=0.01 Fix parser", ), ).rejects.toThrow("No release-owned live qualification"); - expect(visibleRefusals).toHaveLength(1); - expect(visibleRefusals[0]).toContain("Use shadow."); - expect(recovery.snapshot()).toMatchObject({ mode: "off" }); + expect(f.recovery.snapshot()).toMatchObject({ mode: "off" }); }); test("configured auto defaults to bounded shadow and explicit off wins", async () => { - const { createCommandHook } = await import( - "../src/platform/opencode/command-hook.js" - ); - const { createFlowService } = await import( - "../src/application/flow-service.js" - ); - const { MemorySessionRepository, deterministicEnvironment } = await import( - "./runtime-test-support.js" - ); - const recovery = new RecoveryController(unavailable); - const auto = new AutoDriveCoordinator({ - recovery, - readProjection: async () => ({ - status: "idle", - revision: 0, - nextAction: "flow_plan_save", - }), - prompt: async () => {}, - }); let configured = true; - const hook = createCommandHook({ - recovery, - autoDrive: auto, - flow: createFlowService( - new MemorySessionRepository(), - deterministicEnvironment(), - ), - assertOperational() {}, + const f = await commandFixture({ defaultRecovery: () => configured ? { mode: "shadow", maxCalls: 6, maxUsd: 0.02 } : null, }); - const run = async (argumentsText: string) => { - const output = { parts: [] } as Parameters[1]; - await hook( - { command: "flow-auto", sessionID: "host", arguments: argumentsText }, - output, - ); - return output; - }; - const automatic = await run("Fix parser"); - expect(recovery.snapshot()).toMatchObject({ + const initial = await f.commands.execute("flow-auto", "Fix parser"); + expect(f.recovery.snapshot()).toMatchObject({ mode: "shadow", remainingCalls: 6, maxUsd: 0.02, }); - expect(JSON.stringify(automatic)).toContain("Fix parser"); - expect(JSON.stringify(automatic)).not.toContain("--recovery"); - await run("--recovery=off Fix parser"); - expect(recovery.snapshot()).toMatchObject({ mode: "off" }); - const trailingOptOut = await run("Fix parser --recovery=off"); - expect(recovery.snapshot()).toMatchObject({ mode: "off" }); - expect(JSON.stringify(trailingOptOut)).toContain("Fix parser"); - expect(JSON.stringify(trailingOptOut)).not.toContain("--recovery"); + expect(initial.text.split("\n")[0]).toBe("Flow flow-auto: Fix parser"); + await f.commands.execute("flow-auto", "--recovery=off Fix parser"); + expect(f.recovery.snapshot()).toMatchObject({ mode: "off" }); + const trailing = await f.commands.execute( + "flow-auto", + "Fix parser --recovery=off", + ); + expect(f.recovery.snapshot()).toMatchObject({ mode: "off" }); + expect(trailing.text.split("\n")[0]).toBe("Flow flow-auto: Fix parser"); + expect(trailing.text).not.toContain("--recovery"); configured = false; - await run("Fix parser"); - expect(recovery.snapshot()).toMatchObject({ mode: "off" }); - await run( + await f.commands.execute("flow-auto", "Fix parser"); + expect(f.recovery.snapshot()).toMatchObject({ mode: "off" }); + await f.commands.execute( + "flow-auto", "--recovery=shadow --recovery-calls=2 --recovery-usd=0.01 Fix parser", ); - expect(recovery.snapshot()).toMatchObject({ + expect(f.recovery.snapshot()).toMatchObject({ mode: "shadow", remainingCalls: 2, maxUsd: 0.01, }); }); -test("cross-host plain auto synchronously revokes the prior recovery invocation", async () => { - const { createCommandHook } = await import( - "../src/platform/opencode/command-hook.js" - ); - const { createFlowService } = await import( - "../src/application/flow-service.js" - ); - const { MemorySessionRepository, deterministicEnvironment } = await import( - "./runtime-test-support.js" - ); - const recovery = new RecoveryController(unavailable); - const flow = createFlowService( - new MemorySessionRepository(), - deterministicEnvironment(), - ); - const auto = new AutoDriveCoordinator({ - recovery, - readProjection: async () => ({ - status: "idle", - revision: 0, - nextAction: "flow_plan_save", - }), - prompt: async () => {}, - }); - const hook = createCommandHook({ - recovery, - autoDrive: auto, - flow, - assertOperational() {}, +test("accepted cross-host plain auto revokes the prior recovery invocation", async () => { + const f = await commandFixture(); + await f.commands.execute( + "flow-auto", + "--recovery=shadow --recovery-calls=2 --recovery-usd=0.01 Goal", + "ses_A", + ); + expect(f.recovery.snapshot()).toMatchObject({ mode: "shadow" }); + await f.commands.execute("flow-auto", "Goal", "ses_B"); + expect(f.recovery.snapshot()).toMatchObject({ + mode: "off", + automation: { active: true }, }); - const output = () => ({ parts: [] }) as Parameters[1]; - await hook( - { - command: "flow-auto", - sessionID: "A", - arguments: - "--recovery=shadow --recovery-calls=2 --recovery-usd=0.01 Goal", - }, - output(), - ); - expect(recovery.snapshot()).toMatchObject({ mode: "shadow" }); - const replacement = hook( - { command: "flow-auto", sessionID: "B", arguments: "Goal" }, - output(), - ); - expect(recovery.snapshot()).toMatchObject({ mode: "off" }); - await replacement; }); -test("overlapping command setup cannot revive or revoke a newer invocation", async () => { - const { createCommandHook } = await import( - "../src/platform/opencode/command-hook.js" - ); - const { createFlowService } = await import( - "../src/application/flow-service.js" - ); - const { MemorySessionRepository, deterministicEnvironment } = await import( - "./runtime-test-support.js" - ); +test("overlapping accepted command setup cannot revive or revoke a newer invocation", async () => { for (const waitAt of ["status", "anchor"]) { const base = createFlowService( new MemorySessionRepository(), deterministicEnvironment(), ); - let release: () => void = () => {}; - const barrier = new Promise((resolve) => { - release = resolve; - }); - let entered: () => void = () => {}; - const waiting = new Promise((resolve) => { - entered = resolve; - }); - const flow = { + const barrier = Promise.withResolvers(); + const waiting = Promise.withResolvers(); + const flow: FlowService = { ...base, - status: async (input: unknown) => { + status: async (input) => { if (waitAt === "status") { - entered(); - await barrier; + waiting.resolve(); + await barrier.promise; } return base.status(input); }, requestAnchor: async () => { if (waitAt === "anchor") { - entered(); - await barrier; + waiting.resolve(); + await barrier.promise; throw new Error("old anchor failed"); } return null; }, }; - const recovery = new RecoveryController(unavailable); - const auto = new AutoDriveCoordinator({ - recovery, - readProjection: async () => ({ - status: "idle", - revision: 0, - nextAction: "flow_plan_save", - }), - prompt: async () => {}, - }); - const hook = createCommandHook({ - recovery, - autoDrive: auto, - flow, - assertOperational() {}, - }); - const output = () => ({ parts: [] }) as Parameters[1]; - const old = hook( - { - command: "flow-auto", - sessionID: "A", - arguments: - '--recovery=shadow --recovery-calls=2 --recovery-usd=0.01 Cover test named "old"', - }, - output(), - ).then( - () => null, - (error) => error, - ); - await waiting; - await hook( - { - command: "flow-auto", - sessionID: "A", - arguments: - "--recovery=shadow --recovery-calls=5 --recovery-usd=0.02 New goal", - }, - output(), + const f = await commandFixture({ flow }); + const old = f.commands + .execute( + "flow-auto", + '--recovery=shadow --recovery-calls=2 --recovery-usd=0.01 Cover test named "old"', + "ses_A", + ) + .catch((error: unknown) => error); + await waiting.promise; + await f.commands.execute( + "flow-auto", + "--recovery=shadow --recovery-calls=5 --recovery-usd=0.02 New goal", + "ses_A", ); - release(); - expect((await old).message).toBe( + barrier.resolve(); + const failure = await old; + expect(failure instanceof Error ? failure.message : "missing failure").toBe( waitAt === "anchor" ? "old anchor failed" - : "Flow command was superseded.", + : "Flow execution epoch was revoked.", ); - expect(recovery.snapshot()).toMatchObject({ + expect(f.recovery.snapshot()).toMatchObject({ mode: "shadow", remainingCalls: 5, maxUsd: 0.02, @@ -382,341 +372,139 @@ test("overlapping command setup cannot revive or revoke a newer invocation", asy }); test("same-host stop invalidates a pending request-anchor setup", async () => { - const { createCommandHook } = await import( - "../src/platform/opencode/command-hook.js" - ); - const { createFlowService } = await import( - "../src/application/flow-service.js" - ); - const { MemorySessionRepository, deterministicEnvironment } = await import( - "./runtime-test-support.js" - ); const base = createFlowService( new MemorySessionRepository(), deterministicEnvironment(), ); - let release: () => void = () => {}; - const barrier = new Promise((resolve) => { - release = resolve; - }); - let entered: () => void = () => {}; - const waiting = new Promise((resolve) => { - entered = resolve; - }); - const flow = { - ...base, - requestAnchor: async () => { - entered(); - await barrier; - return null; + const barrier = Promise.withResolvers(); + const waiting = Promise.withResolvers(); + const f = await commandFixture({ + flow: { + ...base, + requestAnchor: async () => { + waiting.resolve(); + await barrier.promise; + return null; + }, }, - }; - const recovery = new RecoveryController(unavailable); - const auto = new AutoDriveCoordinator({ - recovery, - readProjection: async () => ({ - status: "idle", - revision: 0, - nextAction: "flow_plan_save", - }), - prompt: async () => {}, - }); - const hook = createCommandHook({ - recovery, - autoDrive: auto, - flow, - assertOperational() {}, }); - const output = () => ({ parts: [] }) as Parameters[1]; - const old = hook( - { - command: "flow-auto", - sessionID: "A", - arguments: - '--recovery=shadow --recovery-calls=2 --recovery-usd=0.01 Cover test named "old"', - }, - output(), - ).then( - () => null, - (error) => error, - ); - await waiting; - await hook( - { command: "flow-auto", sessionID: "A", arguments: "stop" }, - output(), - ); - release(); - expect(await old).toBeInstanceOf(Error); - expect(recovery.snapshot()).toMatchObject({ mode: "off" }); - expect(auto.compactionContext("A")).toBeNull(); + const old = f.commands + .execute( + "flow-auto", + '--recovery=shadow --recovery-calls=2 --recovery-usd=0.01 Cover test named "old"', + ) + .catch((error: unknown) => error); + await waiting.promise; + await f.commands.execute("flow-auto", "stop"); + barrier.resolve(); + const failure = await old; + expect(failure instanceof Error ? failure.message : "missing failure").toBe( + "Flow execution epoch was revoked.", + ); + expect(f.recovery.snapshot()).toMatchObject({ mode: "off" }); + expect(f.auto.compactionContext("ses_host")).toBeNull(); }); -test("failed auto setup revokes its recovery lease", async () => { - const { createCommandHook } = await import( - "../src/platform/opencode/command-hook.js" - ); - const { createFlowService } = await import( - "../src/application/flow-service.js" - ); - const { MemorySessionRepository, deterministicEnvironment } = await import( - "./runtime-test-support.js" - ); +test("failed accepted auto setup revokes its recovery lease", async () => { const base = createFlowService( new MemorySessionRepository(), deterministicEnvironment(), ); - const recovery = new RecoveryController(unavailable); - const auto = new AutoDriveCoordinator({ - recovery, - readProjection: async () => ({ - status: "idle", - revision: 0, - nextAction: "flow_plan_save", - }), - prompt: async () => {}, - }); - const hook = createCommandHook({ - recovery, - autoDrive: auto, + const f = await commandFixture({ flow: { ...base, requestAnchor: async () => { throw new Error("anchor failed"); }, }, - assertOperational() {}, }); await expect( - hook( - { - command: "flow-auto", - sessionID: "host", - arguments: - '--recovery=shadow --recovery-calls=2 --recovery-usd=0.01 Cover test named "x"', - }, - { parts: [] } as Parameters[1], + f.commands.execute( + "flow-auto", + '--recovery=shadow --recovery-calls=2 --recovery-usd=0.01 Cover test named "x"', ), ).rejects.toThrow("anchor failed"); - expect(recovery.snapshot()).toMatchObject({ mode: "off" }); + expect(f.recovery.snapshot()).toMatchObject({ mode: "off" }); }); -test("shadow recovery prompt does not add a second handback at one checkpoint", async () => { - const recovery = new RecoveryController(unavailable); - recovery.activate("host", { mode: "shadow", maxCalls: 2, maxUsd: 0.01 }); - const prompts: string[] = []; - let revision = 10; - const auto = new AutoDriveCoordinator({ - recovery, - readProjection: async () => ({ - sessionId: "flow", - status: "blocked", - revision, - nextAction: "await-user-direction", - }), - prompt: async (_host, prompt) => { - prompts.push(prompt); - }, - }); - const metadata = await auto.activate("host"); - await auto.observeMessage( - "host", - delivery, - [{ type: "text", synthetic: true, metadata }], - "initial", - ); - await auto.onIdle("host"); - expect(prompts).toHaveLength(1); - await auto.onIdle("host"); - expect(prompts).toHaveLength(1); - revision = 11; - await auto.onIdle("host"); - expect(prompts).toHaveLength(2); - await auto.onIdle("host"); - expect(prompts).toHaveLength(2); -}); - -test("running state cannot trigger Jev even with a direction-shaped next action", async () => { - const recovery = new RecoveryController(unavailable); - recovery.activate("host", { mode: "shadow", maxCalls: 2, maxUsd: 0.01 }); - const prompts: string[] = []; - const auto = new AutoDriveCoordinator({ - recovery, - readProjection: async () => ({ - sessionId: "flow", - status: "running", - revision: 10, - nextAction: "await-user-direction", - }), - prompt: async (_host, prompt) => { - prompts.push(prompt); - }, - }); - const metadata = await auto.activate("host"); - await auto.observeMessage( - "host", - delivery, - [{ synthetic: true, metadata }], - "initial", - ); - await auto.onIdle("host"); - expect(prompts).toEqual([]); - expect(recovery.snapshot("host")).toMatchObject({ - mode: "shadow", - remainingCalls: 2, - reservedUsd: 0, - }); -}); - -test("ordinary handback and flow-run keep one shadow lease for the next checkpoint", async () => { - const { createCommandHook } = await import( - "../src/platform/opencode/command-hook.js" - ); - const { createFlowService } = await import( - "../src/application/flow-service.js" - ); - const { MemorySessionRepository, deterministicEnvironment } = await import( - "./runtime-test-support.js" - ); +test("ordinary handback and native flow-run keep one shadow lease for the next checkpoint", async () => { let now = 0; - let projection: AutoDriveProjection = { + const f = await commandFixture({ now: () => now }); + f.setProjection({ sessionId: "flow", status: "blocked", revision: 10, nextAction: "await-user-direction", - }; - const prompts: string[] = []; - const recovery = new RecoveryController(unavailable, { now: () => now }); - const auto = new AutoDriveCoordinator({ - recovery, - readProjection: async () => projection, - prompt: async (_host, prompt) => { - prompts.push(prompt); - }, - }); - const hook = createCommandHook({ - recovery, - autoDrive: auto, - flow: createFlowService( - new MemorySessionRepository(), - deterministicEnvironment(), - ), - assertOperational() {}, }); - const output = () => ({ parts: [] }) as Parameters[1]; - const initial = output(); - await hook( - { - command: "flow-auto", - sessionID: "host", - arguments: - "--recovery=shadow --recovery-calls=2 --recovery-usd=0.01 Fix parser", - }, - initial, - ); - const metadata = initial.parts.find( - (part) => part.type === "text" && part.synthetic === true, + await f.commands.execute( + "flow-auto", + "--recovery=shadow --recovery-calls=2 --recovery-usd=0.01 Fix parser", ); - if (metadata?.type !== "text") throw new Error("missing auto metadata"); - await auto.observeMessage("host", delivery, [metadata], "initial"); - recovery.observeMessage("host", "initial", true); - await auto.onIdle("host"); - expect(prompts).toHaveLength(1); - expect(prompts[0]).toContain("recoveryProposal"); - - await auto.observeMessage( - "host", + await f.auto.onIdle("ses_host"); + expect(f.prompts).toHaveLength(1); + expect(f.prompts[0]).toContain("recoveryProposal"); + await f.auto.observeMessage( + "ses_host", delivery, [{ text: "Continue this Flow task" }], - "reply", + "msg_reply", ); - recovery.observeMessage("host", "reply", false); - expect(recovery.snapshot("host")).toMatchObject({ + f.recovery.observeMessage("ses_host", "msg_reply", false); + expect(f.recovery.snapshot("ses_host")).toMatchObject({ mode: "shadow", remainingCalls: 2, reservedUsd: 0, maxUsd: 0.01, }); - const inspected = output(); - await hook( - { command: "flow-status", sessionID: "host", arguments: "" }, - inspected, - ); - await auto.observeMessage( - "host", - delivery, - inspected.parts, - "status-command", - ); - recovery.observeMessage("host", "status-command", false); - await auto.onIdle("host"); - expect(prompts).toHaveLength(1); + await f.commands.execute("flow-status", ""); + await f.auto.onIdle("ses_host"); + expect(f.prompts).toHaveLength(1); now = 30 * 60 * 1000; - const resumed = output(); - await hook( - { - command: "flow-run", - sessionID: "host", - arguments: "Continue parser repair", - }, - resumed, - ); - await auto.observeMessage("host", delivery, resumed.parts, "run-command"); - recovery.observeMessage("host", "run-command", false); - auto.observeHostMessage("host", { - id: "reset-assistant", - role: "assistant", - parentID: "run-command", - }); - auto.observeMutation("host", 11, undefined, "reset-assistant", false); - projection = { + const resumed = await f.commands.execute( + "flow-run", + "Continue parser repair", + ); + await verifiedContinuationOrigin( + f.auto, + "ses_host", + resumed.id, + "msg_reset_assistant", + ); + f.auto.observeMutation( + "ses_host", + 11, + undefined, + "msg_reset_assistant", + false, + ); + f.setProjection({ sessionId: "flow", status: "ready", revision: 11, nextAction: "flow_run_start", - }; - const afterReset = output(); - await hook( - { command: "flow-status", sessionID: "host", arguments: "" }, - afterReset, - ); - await auto.observeMessage("host", delivery, afterReset.parts, "ready-status"); - recovery.observeMessage("host", "ready-status", false); - await auto.onIdle("host"); - expect(prompts).toHaveLength(1); - projection = { + }); + await f.commands.execute("flow-status", ""); + await f.auto.onIdle("ses_host"); + expect(f.prompts).toHaveLength(1); + f.setProjection({ sessionId: "flow", status: "running", revision: 12, nextAction: "dispatch-flow-reviewer", - }; - const afterRun = output(); - await hook( - { - command: "flow-run", - sessionID: "host", - arguments: "Review parser repair", - }, - afterRun, - ); - await auto.observeMessage( - "host", - delivery, - afterRun.parts, - "running-command", - ); - recovery.observeMessage("host", "running-command", false); - await auto.onIdle("host"); - expect(prompts).toHaveLength(1); - projection = { + }); + await f.commands.execute("flow-run", "Review parser repair"); + await f.auto.onIdle("ses_host"); + expect(f.prompts).toHaveLength(1); + f.setProjection({ sessionId: "flow", status: "blocked", revision: 13, nextAction: "await-user-direction", - }; - await auto.onIdle("host"); - expect(prompts).toHaveLength(2); - expect(prompts[1]).toContain("recoveryProposal"); - expect(recovery.snapshot("host")).toMatchObject({ + }); + await f.auto.onIdle("ses_host"); + expect(f.prompts).toHaveLength(2); + expect(f.prompts[1]).toContain("recoveryProposal"); + expect(f.recovery.snapshot("ses_host")).toMatchObject({ mode: "shadow", remainingCalls: 2, reservedUsd: 0, @@ -724,9 +512,9 @@ test("ordinary handback and flow-run keep one shadow lease for the next checkpoi }); now = 60 * 60 * 1000; expect( - recovery.proposalPrompt("host", "flow", 12, "await-user-direction"), + f.recovery.proposalPrompt("ses_host", "flow", 12, "await-user-direction"), ).toBeNull(); - expect(recovery.snapshot("host")).toMatchObject({ + expect(f.recovery.snapshot("ses_host")).toMatchObject({ mode: "off", automation: { active: true }, advice: { inactiveReason: "expired" }, diff --git a/tests/recovery-live-host-child.ts b/tests/recovery-live-host-child.ts index 52ee0a0e..63040304 100644 --- a/tests/recovery-live-host-child.ts +++ b/tests/recovery-live-host-child.ts @@ -10,6 +10,7 @@ import { syncProviderCredentialsBack, } from "../evals/harness.js"; import { captureHostArtifacts } from "../evals/host-artifacts.js"; +import { OPENCODE_HOST_VERSION } from "../evals/opencode-host.js"; import { createRequestBudget, type EpisodeReservationScope, @@ -170,7 +171,7 @@ for (const managerModel of ["openai/gpt-6-sol", "xai/grok-4.6"] as const) { toolchain, frozenTreatmentBundle, packageCache: root, - opencodeVersion: "1.18.31", + opencodeVersion: OPENCODE_HOST_VERSION, files: { "result.txt": "unchanged\n" }, providerCredentials: "disabled", recoveryTreatment: { origin: "live", arm }, @@ -182,7 +183,6 @@ for (const managerModel of ["openai/gpt-6-sol", "xai/grok-4.6"] as const) { }, ambientConfig: "disabled", reviewerEnvironment: "disabled", - nativeLlm: false, }; if (mode === "admission") { assert(options.requestBudget); @@ -253,7 +253,7 @@ for (const managerModel of ["openai/gpt-6-sol", "xai/grok-4.6"] as const) { packageCache: null, }, bunVersion: Bun.version, - opencodeVersion: "1.18.31", + opencodeVersion: OPENCODE_HOST_VERSION, }); const host = await EvalHost.start({ ...options, @@ -268,8 +268,8 @@ for (const managerModel of ["openai/gpt-6-sol", "xai/grok-4.6"] as const) { "utf8", ); const config = JSON.parse(configText); - assert.equal(config.plugin.length, 1); - const [entry, args] = config.plugin[0]; + assert.equal(config.plugins.length, 1); + const { package: entry, options: args } = config.plugins[0]; assert.equal(args.origin, "live"); assert.equal(args.treatment, undefined); assert.equal(args.budget.scope.arm, arm); @@ -297,7 +297,7 @@ for (const managerModel of ["openai/gpt-6-sol", "xai/grok-4.6"] as const) { assert.equal( ready.pluginEntrySha256, createHash("sha256") - .update(await readFile(fileURLToPath(entry))) + .update(await readFile(join(entry, "index.js"))) .digest("hex"), ); assert.equal(budget.origin, "live"); @@ -316,24 +316,7 @@ for (const managerModel of ["openai/gpt-6-sol", "xai/grok-4.6"] as const) { arm === "manager-plus-jev", ); } - try { - const auth = JSON.parse( - await readFile( - join(scratch, "home", ".local", "share", "opencode", "auth.json"), - "utf8", - ), - ); - assert.deepEqual(auth, {}); - } catch (error) { - if ( - !( - error instanceof Error && - "code" in error && - error.code === "ENOENT" - ) - ) - throw error; - } + assert.deepEqual(await host.client.credential.list(), []); assert.equal((await requestBudgetStatus(directory)).consumed, 0); assert.deepEqual(host.artifactIdentity, identity); records.push({ diff --git a/tests/recovery-live-treatment-child.ts b/tests/recovery-live-treatment-child.ts index 091ccdeb..a5a3ab60 100644 --- a/tests/recovery-live-treatment-child.ts +++ b/tests/recovery-live-treatment-child.ts @@ -1,10 +1,10 @@ +import { mock } from "bun:test"; import assert from "node:assert/strict"; import { randomUUID } from "node:crypto"; import { mkdir, readdir, readFile, writeFile } from "node:fs/promises"; -import { dirname, join } from "node:path"; -import type { Plugin, ToolContext } from "@opencode-ai/plugin"; +import { join } from "node:path"; +import { Plugin } from "@opencode/plugin"; import BudgetPlugin from "../evals/recovery-decisions/budget-plugin.js"; -import LiveTreatmentPlugin from "../evals/recovery-decisions/live-treatment-plugin.js"; import { cancelRequestBudget, createRequestBudget, @@ -13,7 +13,83 @@ import { requestBudgetStatus, } from "../evals/recovery-decisions/request-budget.js"; import { datasetDigest } from "../evals/recovery-decisions/schema.js"; -import { frozenCampaignFixture } from "./recovery-campaign-support.js"; +import type { RecoveryController } from "../src/application/recovery-policy.js"; +import type { createFlowPlugin as FlowFactory } from "../src/platform/opencode/plugin-composition.js"; +import { + approveSession, + deterministicEnvironment, + MemorySessionRepository, + plan, + resetFeatureRun, + startReviewedRun, + submitReview, +} from "./runtime-test-support.js"; + +let capturedRecovery: RecoveryController | undefined; +mock.module("../src/platform/opencode/plugin-composition.js", () => ({ + createFlowPlugin: (dependencies: Parameters[0]) => + Plugin.define({ + id: "native-composition-unit", + setup() { + capturedRecovery = dependencies.createRecovery(); + return () => { + capturedRecovery?.revoke(); + }; + }, + }), +})); +const { default: LiveTreatmentPlugin } = await import( + "../evals/recovery-decisions/live-treatment-plugin.js" +); +async function blockedFixture() { + const repository = new MemorySessionRepository(); + const flow = await approveSession(repository, deterministicEnvironment(), { + goal: "Repair parser null handling and write the selected output after recovery.", + plan: { + ...plan, + features: [ + { + id: "parser", + title: "Parser recovery", + summary: "Repair parser null handling", + targets: ["parser.ts"], + validation: ["bun test"], + dependsOn: [], + }, + ], + }, + }); + for (let index = 0; index < 2; index += 1) { + if (index) + await resetFeatureRun(flow, repository, "parser", `reset-${index}`); + await startReviewedRun(flow, repository, { + featureId: "parser", + suffix: `failed-${index}`, + }); + await submitReview(flow, repository, { + featureId: "parser", + suffix: `failed-${index}`, + summary: "Missing null guard", + verdict: "failed", + findings: [ + { + severity: "blocking", + summary: "Null input crashes", + evidence: "parser.ts", + ...(index + ? { + findingId: + repository.session?.runs[0]?.reviews[0]?.result?.findings[0] + ?.findingId, + } + : {}), + }, + ], + }); + } + if (!repository.session) throw new Error("Missing blocked recovery session"); + return { session: repository.session, sourceDigest: repository.sourceDigest }; +} const root = process.argv[2]; const scenario = process.argv[3]; @@ -155,22 +231,15 @@ const intercepted = Object.assign( globalThis.fetch = intercepted as typeof fetch; const originalWebSocket = globalThis.WebSocket; const context = { - directory: workspace, - worktree: workspace, - project: {}, - serverUrl: new URL("http://localhost:43210"), - $: {}, - experimental_workspace: { register() {} }, - client: { - app: { log() {} }, - session: { - message: async () => ({ data: undefined }), - get: async (input: { path: { id: string } }) => ({ - data: { id: input.path.id, directory: workspace }, - }), - }, - }, -} as unknown as Parameters[0]; + options: {}, + session: { hook: async () => ({ dispose: async () => {} }) }, +}; +const setupLive = (options: unknown): Promise => + Reflect.apply(LiveTreatmentPlugin.setup, undefined, [ + { ...context, options }, + ]); +const installBudget = (options: unknown): Promise => + Reflect.apply(BudgetPlugin.install, undefined, [context, options]); const gateOptions = { directory, authorizationDigest: options.budget.authorizationDigest, @@ -180,7 +249,6 @@ const gateOptions = { const expected = { ...options.budget, origin: "live" as const, - controlOrigin: context.serverUrl.origin, }; assert.throws(() => BudgetPlugin.assertInstalledRequestGate(expected)); if (scenario === "forged-ready") { @@ -195,7 +263,7 @@ if (scenario === "forged-ready") { }), ); assert.throws(() => BudgetPlugin.assertInstalledRequestGate(expected)); - await assert.rejects(LiveTreatmentPlugin(context, options)); + await assert.rejects(setupLive(options)); assert.throws(() => BudgetPlugin.assertInstalledRequestGate(expected)); console.log(JSON.stringify({ scenario, managerModel, requests, ok: true })); process.exit(0); @@ -223,31 +291,31 @@ if (invalid.includes(scenario)) { input.script = { kind: "guarded-reset-v1", outcome: "accepted" }; if (scenario === "missing-scope") delete (input.budget as Record).scope; - await assert.rejects(LiveTreatmentPlugin(context, input)); + await assert.rejects(setupLive(input)); assert.equal(globalThis.fetch, intercepted); assert.equal(globalThis.WebSocket, originalWebSocket); assert.equal((await requestBudgetStatus(directory)).consumed, 0); } else if (scenario === "failed-ready" || scenario === "pending-ready") { if (scenario === "failed-ready") { await writeFile(options.budgetReadyPath, "occupied"); - await assert.rejects(BudgetPlugin(context, gateOptions)); + await assert.rejects(installBudget(gateOptions)); } else { await writeFile(options.budgetReadyPath, "occupied"); const results = await Promise.allSettled([ - BudgetPlugin(context, gateOptions), - BudgetPlugin(context, gateOptions), + installBudget(gateOptions), + installBudget(gateOptions), ]); assert(results.every((result) => result.status === "rejected")); } assert.notEqual(globalThis.fetch, intercepted); assert.throws(() => BudgetPlugin.assertInstalledRequestGate(expected)); - await assert.rejects(BudgetPlugin(context, gateOptions)); + await assert.rejects(installBudget(gateOptions)); await assert.rejects(fetch("https://unlisted.invalid/inference")); } else if ( scenario === "control-manager-gate" || scenario === "treatment-manager-gate" ) { - await BudgetPlugin(context, gateOptions); + await installBudget(gateOptions); BudgetPlugin.assertInstalledRequestGate(expected); assert.throws(() => { globalThis.fetch = intercepted as typeof fetch; @@ -271,8 +339,8 @@ if (invalid.includes(scenario)) { scenario === "gate-websocket" || scenario === "gate-scope" ) { - await BudgetPlugin(context, gateOptions); - await BudgetPlugin(context, gateOptions); + await installBudget(gateOptions); + await installBudget(gateOptions); BudgetPlugin.assertInstalledRequestGate(expected); if (scenario === "gate-fetch") assert.throws(() => { @@ -285,33 +353,24 @@ if (invalid.includes(scenario)) { if (scenario === "gate-scope") options.budget.scope = { ...scope, executionId: randomUUID() }; if (scenario === "gate-scope") { - await assert.rejects(LiveTreatmentPlugin(context, options)); + await assert.rejects(setupLive(options)); await assert.rejects( - BudgetPlugin(context, { ...gateOptions, scope: options.budget.scope }), + installBudget({ ...gateOptions, scope: options.budget.scope }), ); } else { BudgetPlugin.assertInstalledRequestGate(expected); - await BudgetPlugin(context, gateOptions); + await installBudget(gateOptions); } } else { - const fixture = await frozenCampaignFixture(); - for (const [name, bytes] of Object.entries(fixture.fixture.files)) { - await mkdir(dirname(join(workspace, name)), { recursive: true }); - await writeFile(join(workspace, name), bytes); - } - for (const args of [ - ["init", "--initial-branch=main"], - ["add", "."], - ]) - assert.equal(Bun.spawnSync(["git", "-C", workspace, ...args]).exitCode, 0); + const fixture = await blockedFixture(); if (scenario === "treatment-ready") await writeFile(options.readyPath, "occupied"); if (scenario === "treatment-ready") { - await assert.rejects(LiveTreatmentPlugin(context, options)); + await assert.rejects(setupLive(options)); BudgetPlugin.assertInstalledRequestGate(expected); await assert.rejects(fetch("https://unlisted.invalid/inference")); } else { - const hooks = await LiveTreatmentPlugin(context, options); + const hooks = await setupLive(options); try { assert.equal(requests, 0); const ready = JSON.parse(await readFile(options.readyPath, "utf8")); @@ -331,41 +390,18 @@ if (invalid.includes(scenario)) { assert.equal(requests, 0); } const mode = control ? "shadow" : "delegated"; - await hooks["command.execute.before"]?.( - { - command: "flow-auto", - sessionID: "host", - arguments: `--recovery=${mode} --recovery-calls=${retryReplacedFetch ? 2 : 1} --recovery-usd=0.01`, - }, - { parts: [{ type: "text", text: "Flow", synthetic: true } as never] }, - ); - await hooks["chat.message"]?.( - { sessionID: "host" }, - { - message: { - id: "user", - agent: "build", - model: { - providerID: managerModel.split("/")[0], - modelID: managerModel.split("/")[1], - }, - } as never, - parts: [{ type: "text", text: "Continue" } as never], - }, - ); - await hooks.event?.({ - event: { - type: "message.updated", - properties: { - info: { - id: "assistant", - sessionID: "host", - role: "assistant", - parentID: "user", - }, - }, - } as never, + const recovery = capturedRecovery; + if (!recovery) + throw new Error( + "Native composition did not receive recovery controller", + ); + recovery.activate("host", { + mode, + maxCalls: retryReplacedFetch ? 2 : 1, + maxUsd: 0.01, }); + recovery.observeMessage("host", "user", false); + recovery.observeAssistant("host", "assistant", "user"); if (scenario === "replaced-fetch") assert.throws(() => { globalThis.fetch = intercepted as typeof fetch; @@ -378,36 +414,28 @@ if (invalid.includes(scenario)) { assert(run); const finding = run.reviews.at(-1)?.result?.findings[0]?.findingId; assert(finding); - const result = await hooks.tool?.flow_status?.execute( - { - request: { view: "compact" }, - recoveryProposal: { - id: "proposal", - sessionId: fixture.session.id, - expectedRevision: fixture.session.revision, - candidates: [ - { - id: "retry", - action: "retry", - featureId: run.featureId, - remedy: "Add null guard", - changedFromPreviousAttempt: "Handle null", - findingIds: [finding], - }, - ], - }, - }, - { - directory: workspace, - worktree: workspace, - sessionID: "host", - messageID: "assistant", + const advice = await recovery + .guard({ + hostSessionId: "host", + messageId: "assistant", agent: "build", - abort: new AbortController().signal, - metadata() {}, - async ask() {}, - } as ToolContext, - ); + }) + .propose(fixture.session, fixture.sourceDigest, { + id: "proposal", + sessionId: fixture.session.id, + expectedRevision: fixture.session.revision, + candidates: [ + { + id: "retry", + action: "retry", + featureId: run.featureId, + remedy: "Add null guard", + changedFromPreviousAttempt: "Handle null", + findingIds: [finding], + }, + ], + }); + const result = JSON.stringify({ workflowData: { recovery: advice } }); assert.equal(typeof result, "string"); if ( ["accepted", "replaced-fetch", "replaced-websocket"].includes(scenario) @@ -447,7 +475,8 @@ if (invalid.includes(scenario)) { assert.equal(requests, retryReplacedFetch ? 2 : 1); } } finally { - await hooks.dispose?.(); + if (typeof hooks === "function") + await Reflect.apply(hooks, undefined, []); } } } @@ -470,3 +499,5 @@ for (const name of [ } } console.log(JSON.stringify({ scenario, managerModel, requests, ok: true })); + +process.exit(0); diff --git a/tests/recovery-policy.test.ts b/tests/recovery-policy.test.ts index df2b8b46..de01211c 100644 --- a/tests/recovery-policy.test.ts +++ b/tests/recovery-policy.test.ts @@ -1,4 +1,4 @@ -import { describe, expect, test } from "bun:test"; +import { afterEach, describe, expect, test } from "bun:test"; import { createFlowService } from "../src/application/flow-service.js"; import { type DecisionProvider, @@ -11,6 +11,14 @@ import { } from "../src/application/recovery-policy.js"; import { createJevDecisionProvider } from "../src/infrastructure/jev-decision-provider.js"; import { parseRecoveryCommand } from "../src/platform/opencode/command-hook.js"; +import { continuationCommands } from "./fixtures/continuation-commands.js"; +import { verifiedContinuationOrigin } from "./fixtures/verified-continuation-origin.js"; + +const cleanups: Array<() => Promise> = []; +afterEach(async () => { + for (const dispose of cleanups.splice(0)) await dispose(); +}); + import { approveSession, deterministicEnvironment, @@ -34,7 +42,7 @@ const profile: RecoveryProfile = { suitability: 0.95, }; const context = { - hostSessionId: "host", + hostSessionId: "ses_host", messageId: "assistant", agent: "build", }; @@ -120,9 +128,9 @@ async function setup( }); } const controller = new RecoveryController(custom, { profiles: [profile] }); - controller.activate("host", { mode, maxCalls: 6, maxUsd: 0.05 }); - controller.observeMessage("host", "user", false); - controller.observeAssistant("host", "assistant", "user"); + controller.activate("ses_host", { mode, maxCalls: 6, maxUsd: 0.05 }); + controller.observeMessage("ses_host", "user", false); + controller.observeAssistant("ses_host", "assistant", "user"); const flow = createFlowService(repository, env, controller.guard(context)); const proposal = () => ({ id: "proposal", @@ -172,9 +180,6 @@ describe("process-local recovery", () => { const { AutoDriveCoordinator } = await import( "../src/platform/opencode/auto-drive.js" ); - const { createCommandHook } = await import( - "../src/platform/opencode/command-hook.js" - ); let assessments = 0; const s = await setup("shadow", { assess(packet, options) { @@ -191,11 +196,11 @@ describe("process-local recovery", () => { ).status, ).toBe("ok"); const firstReservedUsd = ( - s.controller.snapshot("host") as { + s.controller.snapshot("ses_host") as { reservedUsd: number; } ).reservedUsd; - expect(s.controller.snapshot("host")).toMatchObject({ + expect(s.controller.snapshot("ses_host")).toMatchObject({ mode: "shadow", remainingCalls: 5, reservedUsd: firstReservedUsd, @@ -213,49 +218,39 @@ describe("process-local recovery", () => { }, prompt: async () => {}, }); - const hook = createCommandHook({ + const commands = await continuationCommands({ autoDrive: auto, recovery: s.controller, flow: s.flow, - assertOperational() {}, }); - const metadata = await auto.activate("host"); + cleanups.push(commands.dispose); + const metadata = await auto.activate("ses_host"); await auto.observeMessage( - "host", + "ses_host", { agent: "build", model: { providerID: "test", modelID: "manager" } }, [{ synthetic: true, metadata }], "auto-start", ); - s.controller.observeMessage("host", "auto-start", true); - await auto.onIdle("host"); + s.controller.observeMessage("ses_host", "auto-start", true); + await auto.onIdle("ses_host"); await auto.observeMessage( - "host", + "ses_host", { agent: "build", model: { providerID: "test", modelID: "manager" } }, [{ text: "Continue this repair" }], "reply", ); - s.controller.observeMessage("host", "reply", false); - const output = { parts: [] } as Parameters[1]; - await hook( - { - command: "flow-run", - sessionID: "host", - arguments: "Continue this repair", - }, - output, - ); - await auto.observeMessage( - "host", - { agent: "build", model: { providerID: "test", modelID: "manager" } }, - output.parts, - "run-command", + s.controller.observeMessage("ses_host", "reply", false); + await commands.execute("flow-run", "Continue this repair"); + const native = await commands.native.toolContext( + "flow_status", + {}, + "ses_host", ); - s.controller.observeMessage("host", "run-command", false); - s.controller.observeAssistant("host", "resumed-assistant", "run-command"); + await commands.lifecycle.beforeTool(native); const resumedFlow = createFlowService( s.repository, s.env, - s.controller.guard({ ...context, messageId: "resumed-assistant" }), + s.controller.guard({ ...context, messageId: native.messageID }), ); const first = s.proposal(); const response = await resumedFlow.status({ @@ -271,7 +266,7 @@ describe("process-local recovery", () => { }); expect(response.status).toBe("ok"); expect(assessments).toBe(2); - expect(s.controller.snapshot("host")).toMatchObject({ + expect(s.controller.snapshot("ses_host")).toMatchObject({ mode: "shadow", remainingCalls: 4, reservedUsd: firstReservedUsd * 2, @@ -316,7 +311,7 @@ describe("process-local recovery", () => { }); test("production rejects delegated activation and command parsing strips only explicit options", () => { expect(() => - new RecoveryController(provider).activate("host", { + new RecoveryController(provider).activate("ses_host", { mode: "delegated", maxCalls: 3, maxUsd: 0.01, @@ -433,7 +428,7 @@ describe("process-local recovery", () => { test("cancellation fences original manager lineage including changed operation ids", async () => { const s = await setup(); const mutation = await recommend(s); - s.controller.revoke("host"); + s.controller.revoke("ses_host"); expect((await apply(s, mutation)).status).toBe("error"); expect( ( @@ -442,8 +437,8 @@ describe("process-local recovery", () => { }) ).status, ).toBe("error"); - s.controller.observeMessage("host", "new-user", false); - s.controller.observeAssistant("host", "new-assistant", "new-user"); + s.controller.observeMessage("ses_host", "new-user", false); + s.controller.observeAssistant("ses_host", "new-assistant", "new-user"); const manual = createFlowService( s.repository, s.env, @@ -465,7 +460,7 @@ describe("process-local recovery", () => { request: { view: "compact" }, recoveryProposal: s.proposal(), }); - s.controller.revoke("host"); + s.controller.revoke("ses_host"); const request = { request: { operationId: "abandon-after-stop", @@ -478,8 +473,8 @@ describe("process-local recovery", () => { expect((await s.flow.sessionClose(request)).status).toBe("error"); expect(s.repository.session?.id).toBe(session.id); expect(s.repository.archives.size).toBe(0); - s.controller.observeMessage("host", "fresh-user", false); - s.controller.observeAssistant("host", "fresh-assistant", "fresh-user"); + s.controller.observeMessage("ses_host", "fresh-user", false); + s.controller.observeAssistant("ses_host", "fresh-assistant", "fresh-user"); const directed = createFlowService( s.repository, s.env, @@ -590,7 +585,7 @@ describe("process-local recovery", () => { ).status, ).toBe("ok"); s.controller.guard(context).retireClosedSession(session.id); - s.controller.observeMessage("host", "ordinary-user", false); + s.controller.observeMessage("ses_host", "ordinary-user", false); const ordinary = { ...session, id: "ordinary-next-session" }; expect(() => s.controller @@ -627,7 +622,7 @@ describe("process-local recovery", () => { const other = await setup("shadow"); const ordinary = other.repository.session; if (!ordinary) throw new Error("Fixture session missing."); - other.controller.revoke("host"); + other.controller.revoke("ses_host"); expect(() => other.controller .guard({ ...context, messageId: "synthetic-continuation" }) @@ -637,7 +632,7 @@ describe("process-local recovery", () => { expectedRevision: ordinary.revision, }), ).toThrow("fresh real user direction"); - other.controller.observeMessage("host", "ordinary-user", false); + other.controller.observeMessage("ses_host", "ordinary-user", false); expect(() => other.controller .guard({ ...context, messageId: "synthetic-continuation" }) @@ -667,20 +662,29 @@ describe("process-local recovery", () => { kind: "completed" as const, summary: "Done", }; - s.controller.revoke("host"); + s.controller.revoke("ses_host"); expect(() => s.controller.guard(context).checkClose(session, request), ).toThrow("fresh real user direction"); - s.controller.observeMessage("host", "ordinary-user", false); - s.controller.observeAssistant("host", "ordinary-first", "ordinary-user"); + s.controller.observeMessage("ses_host", "ordinary-user", false); + s.controller.observeAssistant( + "ses_host", + "ordinary-first", + "ordinary-user", + ); expect(() => s.controller .guard({ ...context, messageId: "ordinary-first" }) .checkClose(session, request), ).not.toThrow(); - s.controller.observeMessage("host", "ordinary-continuation", true, true); + s.controller.observeMessage( + "ses_host", + "ordinary-continuation", + true, + true, + ); s.controller.observeAssistant( - "host", + "ses_host", "ordinary-next", "ordinary-continuation", ); @@ -689,9 +693,9 @@ describe("process-local recovery", () => { .guard({ ...context, messageId: "ordinary-next" }) .checkClose(session, request), ).not.toThrow(); - s.controller.observeMessage("host", "untrusted-continuation", true); + s.controller.observeMessage("ses_host", "untrusted-continuation", true); s.controller.observeAssistant( - "host", + "ses_host", "untrusted-next", "untrusted-continuation", ); @@ -987,7 +991,7 @@ test("completed inspect features are not eligible retry targets", async () => { test("recovery snapshots are visible only to their host", async () => { const s = await setup("shadow"); - expect(s.controller.snapshot("host")).toMatchObject({ mode: "shadow" }); + expect(s.controller.snapshot("ses_host")).toMatchObject({ mode: "shadow" }); expect(s.controller.snapshot("other-host")).toEqual({ mode: "off" }); expect( s.controller.guard({ ...context, hostSessionId: "other-host" }).snapshot(), @@ -1166,15 +1170,19 @@ test("expired advice stops prompting while automatic retry safeguards remain", a profiles: [profile], now: () => now, }); - controller.activate("host", { mode: "shadow", maxCalls: 2, maxUsd: 0.01 }); - controller.observeMessage("host", "old-user", false); - controller.observeAssistant("host", "old-assistant", "old-user"); + controller.activate("ses_host", { + mode: "shadow", + maxCalls: 2, + maxUsd: 0.01, + }); + controller.observeMessage("ses_host", "old-user", false); + controller.observeAssistant("ses_host", "old-assistant", "old-user"); expect( - controller.proposalPrompt("host", "flow", 10, "await-user-direction"), + controller.proposalPrompt("ses_host", "flow", 10, "await-user-direction"), ).not.toBeNull(); now = 60 * 60 * 1000; expect( - controller.proposalPrompt("host", "flow", 11, "await-user-direction"), + controller.proposalPrompt("ses_host", "flow", 11, "await-user-direction"), ).toBeNull(); expect(controller.snapshot()).toMatchObject({ mode: "off", @@ -1196,8 +1204,8 @@ test("expired advice stops prompting while automatic retry safeguards remain", a .guard({ ...context, messageId: "old-assistant" }) .check(session, null, mutation), ).toThrow("explicit user direction"); - controller.observeMessage("host", "new-user", false); - controller.observeAssistant("host", "new-assistant", "new-user"); + controller.observeMessage("ses_host", "new-user", false); + controller.observeAssistant("ses_host", "new-assistant", "new-user"); const flow = createFlowService( s.repository, s.env, @@ -1264,13 +1272,13 @@ test("a new invocation cannot erase the third failed-review limit", async () => verdict: "failed", findings: finding ? [finding] : [], }); - s.controller.activate("host", { + s.controller.activate("ses_host", { mode: "delegated", maxCalls: 6, maxUsd: 0.05, }); - s.controller.observeMessage("host", "new-user", false); - s.controller.observeAssistant("host", "assistant", "new-user"); + s.controller.observeMessage("ses_host", "new-user", false); + s.controller.observeAssistant("ses_host", "assistant", "new-user"); expect( ( await s.flow.status({ @@ -1356,23 +1364,28 @@ test("registered tools share the controller across file-backed service instances }, prompt: async (host, _text, delivery, metadata) => { prompts++; - const user = `recovery-turn-${prompts}`; - ctx.messageID = `assistant-${prompts}`; + const user = `msg_recovery-turn-${prompts}`; + ctx.messageID = `msg_assistant-${prompts}`; await auto.observeMessage( host, delivery, [{ type: "text", synthetic: true, metadata }], user, ); - auto.observeHostMessage(host, { - id: ctx.messageID, - role: "assistant", - parentID: user, - }); + await verifiedContinuationOrigin( + auto, + host, + user, + ctx.messageID, + delivery, + ); s.controller.observeMessage(host, user, true, true); s.controller.observeAssistant(host, ctx.messageID, user); }, }); + const { ValidationCaptureCoordinator } = await import( + "../src/platform/opencode/validation-capture.js" + ); const leadership = registerFlowPluginInstance(directory, { packageName: "opencode-plugin-flow", version: "test", @@ -1383,11 +1396,14 @@ test("registered tools share the controller across file-backed service instances const tools = guardTools( createTools({ recovery: s.controller, - validation: { - cancel() { - return true; - }, - } as never, + validation: new ValidationCaptureCoordinator({ + readSession: async (id) => ({ + id, + agent: "build", + location: { directory }, + }), + persistObservation: persistWorkspaceValidation, + }), prepareValidation: async () => { throw new Error("not used"); }, @@ -1396,40 +1412,35 @@ test("registered tools share the controller across file-backed service instances auto, async (id) => ({ id, - directory, - ...(id === "reviewer-child" ? { parentID: "host" } : {}), + agent: id === "reviewer-child" ? "flow-reviewer" : "build", + location: { directory }, + ...(id === "reviewer-child" ? { parentID: "ses_host" } : {}), }), ); const ctx = { - sessionID: "host", - messageID: "assistant", + sessionID: "ses_host", + messageID: "msg_assistant", + callID: "call_manager", agent: "build", directory, - worktree: directory, abort: new AbortController().signal, - metadata() {}, - async ask() {}, }; - const metadata = await auto.activate("host"); + const metadata = await auto.activate("ses_host"); await auto.observeMessage( - "host", + "ses_host", { agent: "build", model: { providerID: "test", modelID: "manager" } }, [{ type: "text", synthetic: true, metadata }], "user", ); - auto.observeHostMessage("host", { - id: "assistant", - role: "assistant", - parentID: "user", - }); - await auto.onIdle("host"); + await verifiedContinuationOrigin(auto, "ses_host", "user", "msg_assistant"); + await auto.onIdle("ses_host"); expect(prompts).toBe(1); const status = tools.flow_status, reset = tools.flow_feature_reset, complete = tools.flow_feature_complete; if (!status || !reset || !complete) throw new Error("tools"); const otherHostStatus = JSON.parse( - String( + JSON.stringify( await status.execute( { request: { view: "compact" } }, { ...ctx, sessionID: "other-host", messageID: "other-assistant" }, @@ -1440,7 +1451,7 @@ test("registered tools share the controller across file-backed service instances expect(otherHostStatus.workflowData.recovery).toEqual({ mode: "off" }); const before = (await loadSession(directory))?.revision; const advice = JSON.parse( - String( + JSON.stringify( await status.execute( { request: { view: "compact" }, recoveryProposal: s.proposal() }, ctx, @@ -1452,7 +1463,7 @@ test("registered tools share the controller across file-backed service instances const mutation = advice.workflowData.recovery.recommended; expect( JSON.parse( - String( + JSON.stringify( await reset.execute( { request: mutation.request }, { ...ctx, sessionID: "other" }, @@ -1462,14 +1473,14 @@ test("registered tools share the controller across file-backed service instances ).toBe("error"); expect( JSON.parse( - String(await reset.execute({ request: mutation.request }, ctx)), + JSON.stringify(await reset.execute({ request: mutation.request }, ctx)), ).status, ).toBe("ok"); const after = await loadSession(directory); expect(after?.runs.filter((run) => run.state === "active")).toHaveLength(1); expect( JSON.parse( - String( + JSON.stringify( await complete.execute( { request: { @@ -1502,7 +1513,8 @@ test("registered tools share the controller across file-backed service instances }, }; expect( - JSON.parse(String(await reviewTool.execute(reviewInput, ctx))).status, + JSON.parse(JSON.stringify(await reviewTool.execute(reviewInput, ctx))) + .status, ).toBe("error"); const prepared = await prepareWorkspaceValidation(directory, { expectedRevision: after.revision, @@ -1518,7 +1530,7 @@ test("registered tools share the controller across file-backed service instances outputComplete: true, }); const reviewed = JSON.parse( - String( + JSON.stringify( await reviewTool.execute( { request: { @@ -1546,7 +1558,8 @@ test("registered tools share the controller across file-backed service instances }, }; expect( - JSON.parse(String(await complete.execute(completion, ctx))).status, + JSON.parse(JSON.stringify(await complete.execute(completion, ctx))) + .status, ).toBe("error"); const reviewer = { ...ctx, @@ -1554,17 +1567,13 @@ test("registered tools share the controller across file-backed service instances agent: "flow-reviewer", messageID: "reviewer-result", }; - auto.observeHostMessage("host", { - id: reviewer.messageID, - role: "assistant", - parentID: "recovery-turn-1", - }); + const result = JSON.parse( - String(await complete.execute(completion, reviewer)), + JSON.stringify(await complete.execute(completion, reviewer)), ); expect(result.status).toBe("ok"); expect(result.workflowData.projection.nextAction).toBe("flow_run_start"); - await auto.onIdle("host"); + await auto.onIdle("ses_host"); expect(prompts).toBe(2); expect( (await loadSession(directory))?.runs.findLast( @@ -1819,9 +1828,6 @@ test("plain auto in another host rejects the previous host's pending grant", asy const { AutoDriveCoordinator } = await import( "../src/platform/opencode/auto-drive.js" ); - const { createCommandHook } = await import( - "../src/platform/opencode/command-hook.js" - ); const auto = new AutoDriveCoordinator({ recovery: s.controller, readProjection: async () => ({ @@ -1832,28 +1838,21 @@ test("plain auto in another host rejects the previous host's pending grant", asy }), prompt: async () => {}, }); - await auto.activate("host"); - const hook = createCommandHook({ + await auto.activate("ses_host"); + const commands = await continuationCommands({ autoDrive: auto, recovery: s.controller, flow: s.flow, - assertOperational() {}, }); - const replacement = hook( - { - command: "flow-auto", - sessionID: "other-host", - arguments: "Continue goal", - }, - { parts: [] } as Parameters[1], - ); + cleanups.push(commands.dispose); + await commands.execute("flow-auto", "Continue goal", "ses_other-host"); + expect(s.controller.snapshot()).toMatchObject({ mode: "off", automation: { active: true }, advice: { configured: false, attempted: false }, }); expect((await apply(s, mutation)).status).toBe("error"); - await replacement; }); for (const [dimension, values] of [ @@ -1910,7 +1909,7 @@ for (const [dimension, values] of [ }, }); expect(response.workflowData.recovery).not.toHaveProperty("recommended"); - expect(s.controller.snapshot("host")).toMatchObject({ + expect(s.controller.snapshot("ses_host")).toMatchObject({ last: response.workflowData.recovery, }); }); diff --git a/tests/recovery-request-host.test.ts b/tests/recovery-request-host.test.ts index efd7f2eb..651c3304 100644 --- a/tests/recovery-request-host.test.ts +++ b/tests/recovery-request-host.test.ts @@ -60,7 +60,6 @@ for (const managerModel of ["openai/gpt-6-sol", "xai/grok-4.6"] as const) withFlow: false, providerCredentials: "disabled", ambientConfig: "disabled", - nativeLlm: false, requestBudget: { directory, authorizationDigest: datasetDigest(authorization), diff --git a/tests/recovery-treatment-host.test.ts b/tests/recovery-treatment-host.test.ts index 1d1dfd60..5c944623 100644 --- a/tests/recovery-treatment-host.test.ts +++ b/tests/recovery-treatment-host.test.ts @@ -95,7 +95,6 @@ for (const managerModel of ["openai/gpt-6-sol", "xai/grok-4.6"] as const) withFlow: true, providerCredentials: "disabled", ambientConfig: "disabled", - nativeLlm: false, requestBudget: { directory, authorizationDigest: datasetDigest(authorization), @@ -298,7 +297,6 @@ for (const managerModel of ["openai/gpt-6-sol", "xai/grok-4.6"] as const) withFlow: true, providerCredentials: "disabled", ambientConfig: "disabled", - nativeLlm: false, requestBudget: { directory, authorizationDigest: datasetDigest(authorization), diff --git a/tests/recovery-treatment.test.ts b/tests/recovery-treatment.test.ts index 3c3d5841..7ccb4be3 100644 --- a/tests/recovery-treatment.test.ts +++ b/tests/recovery-treatment.test.ts @@ -4,6 +4,7 @@ import { mkdtemp, rm } from "node:fs/promises"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { EvalHost } from "../evals/harness.js"; +import { OPENCODE_HOST_VERSION } from "../evals/opencode-host.js"; import { cancelRequestBudget, createRequestBudget, @@ -139,7 +140,7 @@ test("host refuses treatment without a gate before any process starts", async () environment: {}, }, packageCache: "/unused", - opencodeVersion: "1.18.31", + opencodeVersion: OPENCODE_HOST_VERSION, files: { "README.md": "test" }, recoveryTreatment: treatment, }), diff --git a/tests/release-1000.test.ts b/tests/release-1000.test.ts new file mode 100644 index 00000000..2d32d811 --- /dev/null +++ b/tests/release-1000.test.ts @@ -0,0 +1,129 @@ +import { expect, test } from "bun:test"; +import historical from "../.agents/plans/18-opencode-v2/historical-policy-baseline.json" with { + type: "json", +}; +import { + assertReleaseHost, + assertReleaseModels, + releaseCatalog, + releaseCellsFor, + releaseGraderSourceBundle, + releaseHostConfigSha256, + releaseHostPermissions, + releaseHostPolicy, + releasePolicySha256, + releaseReviewerModel, +} from "../evals/release-policy.js"; +import type { ModelIdentity } from "../evals/report.js"; + +const manager: ModelIdentity = { + routeProvider: "openai", + gateway: null, + family: "gpt-6.1-sol", + model: "gpt-6.1-sol", + revision: null, +}; +const alternate: ModelIdentity = { + routeProvider: "xai", + gateway: null, + family: "grok-4.7", + model: "grok-4.7", + revision: null, +}; + +test("OpenCode 2 release keeps every delivery and autonomous case on two providers", () => { + const catalog = releaseCatalog("10.0.0"); + expect(catalog.map((row) => row.caseId)).toEqual([ + "delivery-summary-completed", + "delivery-summary-deferred", + "delivery-summary-observed-failure", + "delivery-full-detail-followup", + "delivery-idle-after-close", + "happy-path", + "plan-only-stops", + "goal-change-refused", + "continuation-accepted", + "failing-gate-blocks", + "resumes-after-interruption", + "unprovable-claim-refused", + "skipped-case-named-binding", + "inspection-failed-audit-completes", + "auto-two-features-evidence", + "auto-prerequisite-repair", + "auto-observe-with-required-pass", + ]); + expect(catalog.map((row) => row.minProviders)).toEqual(Array(17).fill(2)); + expect(() => assertReleaseModels([manager], "10.0.0")).toThrow( + "exactly 2 models on distinct route providers", + ); + expect(() => + assertReleaseModels( + [manager, { ...alternate, routeProvider: "openai" }], + "10.0.0", + ), + ).toThrow("distinct route providers"); + assertReleaseModels([manager, alternate], "10.0.0"); + const cells = releaseCellsFor([manager, alternate], "10.0.0"); + expect(cells.filter((cell) => cell.schedule === "primary")).toHaveLength(144); + expect( + cells.filter((cell) => cell.schedule === "environment-reserve"), + ).toHaveLength(34); + for (const cell of cells) + expect(cell.reviewerModel).toEqual(cell.managerModel); + expect(releaseReviewerModel("10.0.0", alternate)).toEqual(alternate); +}); + +test("prospective host policy pins OpenCode 2 and refuses recovery or host overrides", () => { + for (const version of ["10.0.0", "10.1.0", "11.0.0"]) { + expect(releaseHostPolicy(version)).toEqual({ + opencodeVersion: "2.1.4", + platform: "linux", + reviewerSteps: null, + ambientConfig: "disabled", + transport: "http", + }); + expect(releaseHostPermissions(version)).toEqual([ + { action: "external_directory", resource: "*", effect: "deny" }, + ]); + expect(() => + assertReleaseHost({ + packageVersion: version, + platform: "linux", + recoveryApiKeySet: true, + }), + ).toThrow("unset TYPESAFE_API_KEY"); + expect(() => + assertReleaseHost({ + packageVersion: version, + platform: "linux", + opencodeOverride: "2.1.5", + }), + ).toThrow("canonical Linux host"); + expect(() => + assertReleaseHost({ + packageVersion: version, + platform: "linux", + reviewerVariantOverride: "max", + }), + ).toThrow("reviewer"); + assertReleaseHost({ packageVersion: version, platform: "linux" }); + } +}); + +test("retained 9.x host and policy identities survive the OpenCode 2 migration", () => { + for (const [packageVersion, hashes] of Object.entries(historical)) { + expect(releaseHostPolicy(packageVersion).opencodeVersion).toBe("1.18.31"); + expect(releasePolicySha256(packageVersion)).toBe(hashes.policy); + expect(releaseHostConfigSha256({ packageVersion, model: manager })).toBe( + hashes.host, + ); + } +}); + +test("the release evaluator seals the ordinary control transport adapter", () => { + const files = releaseGraderSourceBundle(process.cwd()).files.map( + (file) => file.path, + ); + expect(files).toContain("evals/http-transport-plugin.ts"); + expect(files).toContain("src/platform/opencode/http-transport.ts"); +}); diff --git a/tests/release-940-preparation.test.ts b/tests/release-940-preparation.test.ts index 044abf5a..04b1b6c2 100644 --- a/tests/release-940-preparation.test.ts +++ b/tests/release-940-preparation.test.ts @@ -11,7 +11,6 @@ import { } from "node:fs/promises"; import { tmpdir } from "node:os"; import { join, resolve } from "node:path"; -import { parse } from "yaml"; import { z } from "zod"; const temporary: string[] = []; @@ -37,7 +36,7 @@ async function step( name: string, file = ".github/workflows/release.yml", ): Promise { - const workflow = Workflow.parse(parse(await readFile(file, "utf8"))); + const workflow = Workflow.parse(Bun.YAML.parse(await readFile(file, "utf8"))); const run = workflow.jobs[job]?.steps.find( (entry) => entry.name === name, )?.run; diff --git a/tests/report-artifact.test.ts b/tests/report-artifact.test.ts index fb26f9c2..1e8314c8 100644 --- a/tests/report-artifact.test.ts +++ b/tests/report-artifact.test.ts @@ -159,14 +159,15 @@ describe("report artifact handoff", () => { temporary.push(root, campaign); await mkdir(join(root, "dist"), { recursive: true }); await writeFile(join(root, "dist/index.js"), "export default {}\n"); + await writeFile(join(root, "dist/tui.js"), "export default {}\n"); await writeFile( join(root, "package.json"), JSON.stringify({ name: "opencode-plugin-flow", - version: "1.2.3", - files: ["dist/index.js"], - dependencies: { zod: "4.4.3" }, - devDependencies: { "@opencode-ai/plugin": "1.18.6" }, + version: "10.0.0", + files: ["dist/index.js", "dist/tui.js"], + dependencies: { zod: "4.6.7" }, + devDependencies: { "@opencode/plugin": "2.1.4" }, }), ); for (const command of [ @@ -180,7 +181,7 @@ describe("report artifact handoff", () => { } await run(["bun", "pm", "pack", "--destination", campaign], root); await rename( - join(campaign, "opencode-plugin-flow-1.2.3.tgz"), + join(campaign, "opencode-plugin-flow-10.0.0.tgz"), join(campaign, "artifact.tgz"), ); const { inspectArtifact } = await import("../evals/provenance.js"); diff --git a/tests/restore-qualified-artifact.test.ts b/tests/restore-qualified-artifact.test.ts new file mode 100644 index 00000000..2e2bf731 --- /dev/null +++ b/tests/restore-qualified-artifact.test.ts @@ -0,0 +1,115 @@ +import { afterEach, expect, test } from "bun:test"; +import { spawnSync } from "node:child_process"; +import { + chmod, + mkdir, + mkdtemp, + readFile, + rm, + writeFile, +} from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join, resolve } from "node:path"; +import { writeQualificationBundle } from "../evals/qualification-bundle.js"; + +const temporary: string[] = []; +afterEach(async () => { + await Promise.all( + temporary + .splice(0) + .map((path) => rm(path, { recursive: true, force: true })), + ); +}); + +test("prospective release restoration selects the verified bundle artifact and preserves its exact bytes", async () => { + const root = await mkdtemp(join(tmpdir(), "flow-v2-artifact-")); + temporary.push(root); + const directory = join(root, "package"); + await mkdir(directory); + const readme = join(directory, "README.md"); + await writeFile(readme, "qualified package\n"); + const sealed = join(root, "sealed.tgz"); + const rebuilt = join(root, "rebuilt.tgz"); + const pack = (path: string) => { + const result = spawnSync("tar", ["-czf", path, "-C", root, "package"], { + encoding: "utf8", + }); + expect(result.status, result.stderr).toBe(0); + }; + await chmod(readme, 0o664); + pack(sealed); + const bytes = await readFile(sealed); + const roles = [ + "report", + "catalog", + "policy", + "plan", + "completion", + "expected-provenance", + "decision", + "canary-record", + "canary-installation", + "canary-session", + "canary-transcript", + ] as const; + const bundle = await writeQualificationBundle({ + outputRoot: join(root, "bundles"), + input: { + reportId: "restoration-fixture", + packageVersion: "10.0.0", + verdict: "VERIFIED", + files: [ + ...roles.map((role) => ({ + role, + mediaType: "application/json" as const, + bytes: Buffer.from(JSON.stringify({ role })), + })), + { role: "artifact", mediaType: "application/gzip", bytes }, + { + role: "attempt", + id: "attempt-1", + mediaType: "application/json", + bytes: Buffer.from("{}"), + }, + { + role: "transcript", + id: "attempt-1", + mediaType: "application/json", + bytes: Buffer.from("{}"), + }, + { + role: "authority-source", + id: "evals/analysis.ts", + mediaType: "text/typescript", + bytes: Buffer.from("export const fixture = true;\n"), + }, + ], + }, + }); + await chmod(readme, 0o644); + pack(rebuilt); + expect(await readFile(rebuilt)).not.toEqual(bytes); + const restore = (version: string) => + spawnSync( + process.execPath, + [ + resolve("scripts/restore-exact-release-artifact.ts"), + rebuilt, + "--bundle", + bundle.path, + version, + ], + { encoding: "utf8" }, + ); + expect(restore("9.6.0").status).not.toBe(0); + const restored = restore("10.0.0"); + expect(restored.status, restored.stderr).toBe(0); + expect(await readFile(rebuilt)).toEqual(bytes); + await writeFile(readme, "unqualified change\n"); + pack(rebuilt); + const changed = await readFile(rebuilt); + const rejected = restore("10.0.0"); + expect(rejected.status).not.toBe(0); + expect(rejected.stderr).toContain("Rebuilt package contents differ"); + expect(await readFile(rebuilt)).toEqual(changed); +}); diff --git a/tests/study-host-isolation.test.ts b/tests/study-host-isolation.test.ts index 22412d3c..8927c9c2 100644 --- a/tests/study-host-isolation.test.ts +++ b/tests/study-host-isolation.test.ts @@ -13,11 +13,10 @@ import { join } from "node:path"; import { currentBunToolchain } from "../evals/bun-toolchain.js"; import { EvalHost } from "../evals/harness.js"; import packageJson from "../package.json" with { type: "json" }; +import { nativeIsolationExecutable } from "./fixtures/native-eval-client.js"; test("study host ignores ambient OpenCode configuration and synthetic Git hooks/signing", async () => { const directory = await mkdtemp(join(tmpdir(), "study-host-isolation-")); - const noAuthCopy = process.env.FLOW_EVAL_NO_AUTH_COPY; - process.env.FLOW_EVAL_NO_AUTH_COPY = "1"; let host: EvalHost | undefined; try { const hooks = join(directory, "hooks"); @@ -36,19 +35,19 @@ test("study host ignores ambient OpenCode configuration and synthetic Git hooks/ toolchain: { ...toolchain, environment: { - ...toolchain.environment, + PATH: toolchain.environment.PATH, GIT_CONFIG_GLOBAL: gitConfig, OPENCODE_CONFIG: poisonConfig, OPENCODE_CONFIG_CONTENT: "not-json", OPENCODE_CONFIG_DIR: directory, - OPENCODE_EXPERIMENTAL_NATIVE_LLM: "true", }, }, packageCache: "", withFlow: false, - opencodeVersion: "1.18.6", + opencodeVersion: "2.1.4", + opencodeExecutable: await nativeIsolationExecutable(directory), + providerCredentials: "disabled", ambientConfig: "disabled", - nativeLlm: false, reviewerEnvironment: "disabled", files: { "package.json": '{"name":"project","private":true}\n' }, signal: AbortSignal.timeout(180000), @@ -63,16 +62,12 @@ test("study host ignores ambient OpenCode configuration and synthetic Git hooks/ ); expect(log.status).toBe(0); expect(log.stdout.trim()).toBe("Project "); - const baseUrl: unknown = Reflect.get(host, "baseUrl"); - if (typeof baseUrl !== "string") throw new Error("Missing local host URL."); - const response = await fetch(`${baseUrl}/config`); - expect(response.ok).toBe(true); - const config = (await response.json()) as { plugin?: unknown[] }; - expect(config.plugin ?? []).toEqual([]); + const config = await host.client.config.get({ + location: { directory: host.project }, + }); + expect(config).toEqual([{ type: "document", info: { plugins: [] } }]); } finally { await host?.stop(); - if (noAuthCopy === undefined) delete process.env.FLOW_EVAL_NO_AUTH_COPY; - else process.env.FLOW_EVAL_NO_AUTH_COPY = noAuthCopy; await rm(directory, { recursive: true, force: true }); } }, 240000); diff --git a/tests/study-host-usage.test.ts b/tests/study-host-usage.test.ts index 90956c8f..a1e49fde 100644 --- a/tests/study-host-usage.test.ts +++ b/tests/study-host-usage.test.ts @@ -1,25 +1,47 @@ -import { afterAll, beforeAll, describe, expect, spyOn, test } from "bun:test"; +import { describe, expect, test } from "bun:test"; import { mkdtemp, rm } from "node:fs/promises"; import { tmpdir } from "node:os"; import { join } from "node:path"; -import { EvalHost } from "../evals/harness.js"; +import type { + SessionMessageAssistant, + SessionMessageInfo, +} from "@opencode/client"; import type { StudyUsage } from "../evals/study-usage.js"; -import { authorizePaidRun } from "../scripts/paid-budget.js"; +import { + messagePage, + type NativeTokens, + nativeAssistant, + nativeClient, + nativeHost, + nativeSession, + nativeTool, + sessionPage, + sessionReply, + useNativeFixtureBudget, +} from "./fixtures/native-eval-client.js"; -type HostTokens = { - input: number; - output: number; - reasoning: number; - cache: { read: number; write: number }; -}; +useNativeFixtureBudget(); +type HostTokens = NativeTokens; test("entitlement probes distinguish session setup from a provider request attempt", async () => { - const host = Reflect.construct(EvalHost, [ - "/unused-project", - "/unused-scratch", - ]) as EvalHost; + const project = await mkdtemp(join(tmpdir(), "native-probe-")); let dispatches = 0; let usage: StudyUsage | undefined; + let setupFails = true; + const host = nativeHost( + project, + nativeClient(({ url }) => { + if (url.pathname === "/api/session") { + if (setupFails) throw new Error("Session setup failed."); + return Response.json({ data: nativeSession("probe-session", project) }); + } + if (url.pathname === "/api/session/probe-session/model") + throw new Error("Request failed."); + if (url.pathname === "/api/session/probe-session") + return new Response(null, { status: 204 }); + throw new Error(`Unexpected probe endpoint ${url.pathname}`); + }), + ); const settings = { onDispatch: () => { dispatches++; @@ -28,56 +50,81 @@ test("entitlement probes distinguish session setup from a provider request attem usage = value; }, }; - Reflect.set(host, "createSession", async () => { - throw new Error("Session setup failed."); - }); - await expect(host.probeModel("fixture/manager", settings)).rejects.toThrow( - "Session setup failed.", - ); - expect(dispatches).toBe(0); - expect(usage).toBeUndefined(); - Reflect.set(host, "createSession", async () => "probe-session"); - Reflect.set(host, "deleteSession", async () => {}); - Reflect.set(host, "post", async () => { - throw new Error("Request failed."); - }); - expect(await host.probeModel("fixture/manager", settings)).toBe( - "Request failed.", - ); - expect(dispatches).toBe(1); - expect(usage).toMatchObject({ availability: "unobserved", costUsd: null }); + try { + await expect(host.probeModel("fixture/manager", settings)).rejects.toThrow( + "Session setup failed.", + ); + expect(dispatches).toBe(0); + expect(usage).toBeUndefined(); + setupFails = false; + expect(await host.probeModel("fixture/manager", settings)).toBe( + "Transport: Request failed.", + ); + expect(dispatches).toBe(1); + expect(usage).toMatchObject({ availability: "unobserved", costUsd: null }); + } finally { + await rm(project, { recursive: true, force: true }); + } }); test("entitlement probes retain missing cache counters as incomplete usage", async () => { - const host = Reflect.construct(EvalHost, [ - "/unused-project", - "/unused-scratch", - ]) as EvalHost; - Reflect.set(host, "createSession", async () => "probe-session"); - Reflect.set(host, "deleteSession", async () => {}); - Reflect.set(host, "post", async () => ({ - info: { cost: 0.01, tokens: { input: 2, output: 1, reasoning: 0 } }, - })); - let usage: StudyUsage | undefined; - expect( - await host.probeModel("fixture/manager", { - observeUsage: (value) => { - usage = value; - }, - }), - ).toBeNull(); - expect(usage).toMatchObject({ - availability: "incomplete", - upstreamBreakdown: "unobserved", - costUsd: 0.01, + const project = await mkdtemp(join(tmpdir(), "native-probe-")); + const reply = nativeAssistant("message-probe", { + time: { created: 1, completed: 2 }, + cost: 0.01, }); + Reflect.set(reply, "tokens", { input: 2, output: 1, reasoning: 0 }); + const host = nativeHost( + project, + nativeClient(({ url }) => { + if (url.pathname === "/api/session") + return Response.json({ data: nativeSession("probe-session", project) }); + if (url.pathname === "/api/session/probe-session/message") + return messagePage([reply]); + if ( + url.pathname === "/api/session/probe-session/model" || + url.pathname === "/api/session/probe-session" || + url.pathname === "/api/experimental/session/probe-session/wait" + ) + return new Response(null, { status: 204 }); + if (url.pathname === "/api/session/probe-session/prompt") + return Response.json({ + data: { + id: "inbox-probe", + sessionID: "probe-session", + type: "user", + time: { created: 1 }, + payload: { text: "OK" }, + delivery: "queue", + }, + }); + throw new Error(`Unexpected probe endpoint ${url.pathname}`); + }), + ); + let usage: StudyUsage | undefined; + try { + expect( + await host.probeModel("fixture/manager", { + observeUsage: (value) => { + usage = value; + }, + }), + ).toBeNull(); + expect(usage).toMatchObject({ + availability: "incomplete", + upstreamBreakdown: "unobserved", + costUsd: 0.01, + }); + } finally { + await rm(project, { recursive: true, force: true }); + } }); type SessionFixture = { agent: string; parentID: string | null; children?: readonly string[]; - messages: readonly unknown[]; + messages: readonly SessionMessageInfo[]; }; function assistantMessage( @@ -85,78 +132,56 @@ function assistantMessage( agent: string, tokens: HostTokens | undefined, cost: number | undefined, -) { - return { - info: { - id: `message-${sessionId}`, - sessionID: sessionId, - role: "assistant", - agent, - providerID: "fixture-provider", - modelID: `${agent}-model`, - time: { created: 1, completed: 2 }, - ...(tokens === undefined ? {} : { tokens }), - ...(cost === undefined ? {} : { cost }), - }, - parts: [], - }; +): SessionMessageAssistant { + return nativeAssistant(`message-${sessionId}`, { + agent, + model: { providerID: "fixture-provider", id: `${agent}-model` }, + time: { created: 1, completed: 2 }, + ...(tokens === undefined ? {} : { tokens }), + ...(cost === undefined ? {} : { cost }), + }); } async function collectOutcome( fixtures: Readonly>, ) { - const host: unknown = Reflect.construct(EvalHost, [ - "unused-study-host-project", - "unused-study-host-scratch", - ]); - if (!(host instanceof EvalHost)) throw new Error("Expected an EvalHost."); - const origin = "https://study-host.invalid"; - Object.assign(host, { - baseUrl: origin, - readJson: async () => null, - readArchives: async () => [], - }); - const responses = new Map(); - for (const [id, fixture] of Object.entries(fixtures)) { - responses.set(`/session/${id}/message`, fixture.messages); - responses.set( - `/session/${id}/children`, - (fixture.children ?? []).map((childId) => { - const child = fixtures[childId]; - if (!child) throw new Error(`Missing child fixture ${childId}.`); - return { id: childId, agent: child.agent, parentID: child.parentID }; - }), - ); - } - responses.set("/session/root", { - id: "root", - directory: "unused-study-host-project", - }); + const project = await mkdtemp(join(tmpdir(), "study-host-usage-")); const requests: string[] = []; const unexpected: string[] = []; - const transport = Object.assign( - async (...[input, init]: Parameters) => { - const url = new URL(input instanceof Request ? input.url : String(input)); + const metadata = (id: string) => { + const fixture = fixtures[id]; + if (!fixture) throw new Error(`Missing session fixture ${id}`); + return nativeSession(id, project, { + agent: fixture.agent, + ...(fixture.parentID ? { parentID: fixture.parentID } : {}), + }); + }; + const host = nativeHost( + project, + nativeClient(({ url, method }) => { requests.push(url.pathname); - if ( - url.origin !== origin || - (init?.method ?? "GET") !== "GET" || - !responses.has(url.pathname) - ) { - unexpected.push(`${init?.method ?? "GET"} ${url}`); - return Response.json({ error: "Unexpected request." }, { status: 404 }); + if (method !== "GET") throw new Error(`Unexpected method ${method}`); + if (url.pathname === "/api/session") { + const listed = Object.keys(fixtures).map(metadata); + for (const [parentID, fixture] of Object.entries(fixtures)) + for (const child of fixture.children ?? []) + listed.push({ ...metadata(child), parentID }); + return sessionPage(listed); } - return Response.json(responses.get(url.pathname)); - }, - { preconnect: fetch.preconnect }, + if (url.pathname === "/api/session/root") + return sessionReply(metadata("root")); + const id = url.pathname.match(/^\/api\/session\/([^/]+)\/message$/)?.[1]; + if (id && fixtures[id]) return messagePage([...fixtures[id].messages]); + unexpected.push(`${method} ${url}`); + return Response.json({ error: "Unexpected request." }, { status: 404 }); + }), ); - const fetchSpy = spyOn(globalThis, "fetch").mockImplementation(transport); try { const outcome = await host.outcome(["root"], 123); expect(unexpected).toEqual([]); return { outcome, requests }; } finally { - fetchSpy.mockRestore(); + await rm(project, { recursive: true, force: true }); } } @@ -186,13 +211,11 @@ const family = { children: ["worker", "reviewer"], messages: [ { - info: { - ...rootMessage.info, - id: "user-root", - role: "user", - cost: 999, - }, - parts: [], + type: "user", + id: "user-root", + time: { created: 1 }, + text: "Inspect the fixture.", + metadata: { cost: 999 }, }, rootMessage, ], @@ -253,6 +276,27 @@ describe("EvalHost outcome usage collection", () => { { role: "manager", sessionIds: ["root"] }, { role: "reviewer", sessionIds: ["reviewer"] }, ]); + expect( + outcome.actors?.map(({ role, actualModel }) => ({ role, actualModel })), + ).toEqual([ + { + role: "manager", + actualModel: { + kind: "observed", + value: { providerID: "fixture-provider", modelID: "flow-model" }, + }, + }, + { + role: "reviewer", + actualModel: { + kind: "observed", + value: { + providerID: "fixture-provider", + modelID: "flow-reviewer-model", + }, + }, + }, + ]); }); test("fetches repeated descendants once across duplicate edges and cycles", async () => { @@ -269,13 +313,11 @@ describe("EvalHost outcome usage collection", () => { expect(outcome.costUsd).toBe(0.875); expect(outcome.assistantMessages).toBe(3); expect([...requests].sort()).toEqual([ - "/session/reviewer/children", - "/session/reviewer/message", - "/session/root", - "/session/root/children", - "/session/root/message", - "/session/worker/children", - "/session/worker/message", + "/api/session", + "/api/session/reviewer/message", + "/api/session/root", + "/api/session/root/message", + "/api/session/worker/message", ]); }); @@ -287,16 +329,18 @@ describe("EvalHost outcome usage collection", () => { messages: [ { ...rootMessage, - parts: [ - { type: "step-start" }, - { - type: "step-finish", - sessionID: "root", - messageID: rootMessage.info.id, - reason: "stop", - cost: 0.125, - tokens: rootMessage.info.tokens, - }, + content: [ + nativeTool( + "call-usage", + "read", + { + status: "completed", + input: {}, + content: [{ type: "text", text: "fixture" }], + metadata: { cost: 0.125, tokens: rootMessage.tokens ?? {} }, + }, + { created: 1, completed: 2 }, + ), ], }, ], @@ -461,24 +505,3 @@ describe("EvalHost outcome usage collection", () => { }, ); }); - -let authorizationDirectory: string; -let previousAuthorization: string | undefined; -beforeAll(async () => { - previousAuthorization = process.env.FLOW_EVAL_AUTHORIZATION; - authorizationDirectory = await mkdtemp(join(tmpdir(), "flow-fake-budget-")); - await authorizePaidRun(authorizationDirectory, { - schemaVersion: 1, - purpose: "Fake transport tests only", - models: ["fixture/model", "fixture/manager"], - maxDispatches: 100, - expiresAt: new Date(Date.now() + 3600000).toISOString(), - }); - process.env.FLOW_EVAL_AUTHORIZATION = authorizationDirectory; -}); -afterAll(async () => { - if (previousAuthorization === undefined) - delete process.env.FLOW_EVAL_AUTHORIZATION; - else process.env.FLOW_EVAL_AUTHORIZATION = previousAuthorization; - await rm(authorizationDirectory, { recursive: true, force: true }); -}); diff --git a/tests/study-manifest.test.ts b/tests/study-manifest.test.ts index 14bc0640..3fe53e5f 100644 --- a/tests/study-manifest.test.ts +++ b/tests/study-manifest.test.ts @@ -11,6 +11,11 @@ import { PackedArtifactIdentitySchema, } from "../evals/report-identities.js"; import { prepareStudyManifest } from "../evals/study-manifest.js"; +import { + LEGACY_STUDY_PROTOCOL_VERSION_SHA256, + PairedStudyPolicySchema, + STUDY_PROTOCOL_VERSION_SHA256, +} from "../evals/study-protocol.js"; import { runStudyCommand } from "../evals/study-runner.js"; import packageJson from "../package.json" with { type: "json" }; import { studyArtifact, studyManifest } from "./study-support.js"; @@ -67,7 +72,7 @@ describe("paired-study manifest", () => { toolchain, packageCache: "unused", packageVersion: version, - opencodeVersion: "1.18.6", + opencodeVersion: "2.1.4", files: {}, }), ).rejects.toThrow("bounded exact SemVer"); @@ -77,6 +82,51 @@ describe("paired-study manifest", () => { } }); } + test("retains historical accounting policies while refusing active V1 studies", async () => { + const manifest = studyManifest(); + const current = (await prepareStudyManifest(manifest, ".")).policy; + for (const digest of [ + LEGACY_STUDY_PROTOCOL_VERSION_SHA256, + STUDY_PROTOCOL_VERSION_SHA256, + ]) { + const historical = { + ...current, + opencodeVersion: "1.18.6", + versionSha256: digest, + accounting: { + ...current.accounting, + hostContract: "opencode-1.18.6-disjoint-output-v1", + }, + }; + expect(PairedStudyPolicySchema.safeParse(historical).success).toBe(true); + expect( + PairedStudyPolicySchema.safeParse({ + ...historical, + opencodeVersion: "2.1.4", + }).success, + ).toBe(false); + expect( + PairedStudyPolicySchema.safeParse({ ...current, versionSha256: digest }) + .success, + ).toBe(false); + } + await expect( + prepareStudyManifest( + { + ...manifest, + opencodeVersion: "1.18.6", + accounting: { + ...manifest.accounting, + hostContract: "opencode-1.18.6-disjoint-output-v1", + }, + }, + ".", + ), + ).rejects.toThrow( + "Active studies require the verified OpenCode 2.1.4 accounting contract.", + ); + }); + test("requires an explicit total-effect label when more than the declared factor changes", async () => { const manifest = studyManifest(); manifest.comparison = "artifact"; @@ -130,7 +180,9 @@ describe("paired-study manifest", () => { const manifest = studyManifest(); await expect( prepareStudyManifest({ ...manifest, opencodeVersion: "1.19.0" }, "."), - ).rejects.toThrow("verified only for OpenCode 1.18.6"); + ).rejects.toThrow( + "Active studies require the verified OpenCode 2.1.4 accounting contract.", + ); await expect( prepareStudyManifest( { diff --git a/tests/study-runner.test.ts b/tests/study-runner.test.ts index 0c6d2a30..dc22a6a4 100644 --- a/tests/study-runner.test.ts +++ b/tests/study-runner.test.ts @@ -341,7 +341,7 @@ describe("paired-study runner", () => { expect( fake.starts.every( (entry) => - entry.nativeLlm === false && + entry.ambientConfig === "disabled" && entry.reviewerEnvironment === "disabled", ), ).toBe(true); diff --git a/tests/study-support.ts b/tests/study-support.ts index b9aa76df..cd31dbe0 100644 --- a/tests/study-support.ts +++ b/tests/study-support.ts @@ -64,7 +64,7 @@ export function studyManifest(): StudyManifest { purpose: "exploratory", comparison: "total-effect", method: "equal-task-cluster-bootstrap-v1", - opencodeVersion: "1.18.6", + opencodeVersion: "2.1.4", arms: { baseline: structuredClone(arm), candidate: structuredClone(arm) }, cases: [{ caseId: studyCase.id, caseVersion: 1 }], repetitions: 1, @@ -78,7 +78,7 @@ export function studyManifest(): StudyManifest { }, accounting: { schemaVersion: 1, - hostContract: "opencode-1.18.6-disjoint-output-v1", + hostContract: "opencode-2.1.4-disjoint-output-v1", maxGeneratedOutputTokens: 10000, estimate: { generatedOutputTokensPerAttempt: 10, diff --git a/tests/tool-parentage.test.ts b/tests/tool-parentage.test.ts index d334ae4e..4b7efbb6 100644 --- a/tests/tool-parentage.test.ts +++ b/tests/tool-parentage.test.ts @@ -1,317 +1,433 @@ -import { expect, test } from "bun:test"; -import { AutoDriveCoordinator } from "../src/platform/opencode/auto-drive.js"; +import { afterAll, expect, test } from "bun:test"; +import { mkdir, mkdtemp, rm, symlink } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import type { + FlowToolContext, + FlowToolValue, +} from "../src/platform/opencode/flow-tool.js"; import { FLOW_LEADERSHIP_PROTOCOL_VERSION, registerFlowPluginInstance, } from "../src/platform/opencode/leadership.js"; -import { type ToolContext, tool } from "../src/platform/opencode/sdk.js"; import { guardTools } from "../src/platform/opencode/tool-guard.js"; -const nativeChild = { - id: "ses_f0e0474aaffeL2LJokocWs5MFT", - parentID: "ses_f0e048715ffezsym7a0HCdd9dE", - directory: "/fixture", -}; -const context: ToolContext = { - sessionID: nativeChild.id, - messageID: "child-assistant", +const workspace = await mkdtemp(join(tmpdir(), "flow-native-parentage-")); +afterAll(() => rm(workspace, { recursive: true, force: true })); +const context: FlowToolContext = { + sessionID: "child", + messageID: "assistant", + callID: "call", agent: "general", - directory: "/fixture", - worktree: "/fixture", + directory: workspace, abort: new AbortController().signal, - metadata() {}, - async ask() {}, }; - -test.each([ - "flow_plan_save", - "flow_plan_approve", - "flow_plan_amend", - "flow_run_start", - "flow_review_start", - "flow_feature_reset", - "flow_session_close", - "flow_validation_start", -])("native child cannot execute manager mutation %s", async (name) => { - let calls = 0; - const leadership = registerFlowPluginInstance("/parentage-fixture-" + name, { +const child = { + id: "child", + parentID: "parent", + agent: "general", + location: { directory: workspace }, +}; +const parent = { + id: "parent", + agent: "build", + location: { directory: workspace }, +}; +const ok = { status: "ok", summary: "Executed.", workflowData: {} } as const; +function status(result: FlowToolValue) { + return typeof result === "string" ? result : result.status; +} +async function run( + name: string, + agent: string, + identity: unknown, + input: unknown = {}, + readParent: unknown = parent, +) { + let writes = 0; + const runtime = registerFlowPluginInstance(workspace, { packageName: "opencode-plugin-flow", version: "test", protocolVersion: FLOW_LEADERSHIP_PROTOCOL_VERSION, instanceId: name, }); try { - const auto = new AutoDriveCoordinator({ - readProjection: async () => ({ - status: "idle", - revision: 0, - nextAction: "flow_plan_save", - }), - prompt: async () => {}, - }); const tools = guardTools( { - [name]: tool({ - description: "Native child mutation control", - args: {}, + [name]: { + description: "Guarded operation", + input: { type: "object" }, execute: async () => { - calls++; - return JSON.stringify({ status: "ok" }); + writes++; + return ok; }, - }), + }, }, - leadership, - auto, - async () => nativeChild, + runtime, + { clear() {}, observeMutation() {} }, + async (id) => (id === "parent" ? readParent : identity), ); - const result = JSON.parse(String(await tools[name]!.execute({}, context))); - expect(result.status).toBe("error"); - expect(calls).toBe(0); + const definition = tools[name]; + if (!definition) throw new Error("Missing guarded tool."); + const output = await definition.execute(input, { ...context, agent }); + return { writes, status: status(output) }; } finally { - leadership.release(); + runtime.release(); } -}); +} test.each([ - ["missing", async (): Promise => undefined], + "flow_plan_save", + "flow_plan_approve", + "flow_plan_amend", + "flow_run_start", + "flow_review_start", + "flow_feature_reset", + "flow_session_close", + "flow_validation_start", +])("native child cannot execute manager mutation %s", async (name) => { + expect(await run(name, "general", child)).toEqual({ + writes: 0, + status: "error", + }); +}); +test.each([ + ["missing", undefined], + ["wrong-id", { ...child, id: "other" }], + ["wrong-role", { ...parent, id: "child" }], + ["wrong-location", { ...child, location: { directory: tmpdir() } }], + ["v1-location", { id: "child", agent: "general", directory: workspace }], +] as const)("%s native identity fails closed", async (_name, identity) => { + expect(await run("flow_run_start", "general", identity)).toEqual({ + writes: 0, + status: "error", + }); +}); +test("verified child may inspect status but may not request recovery authority", async () => { + expect( + await run("flow_status", "general", child, { + request: { view: "reviewer-evidence" }, + }), + ).toEqual({ writes: 1, status: "ok" }); + expect( + await run("flow_status", "general", child, { recoveryProposal: {} }), + ).toEqual({ writes: 0, status: "error" }); +}); +test.each(["flow-planner", "flow-worker"])( + "%s cannot use Flow tools", + async (agent) => { + expect(await run("flow_status", agent, { ...child, agent })).toEqual({ + writes: 0, + status: "error", + }); + }, +); +test.each([ + ["generic-child", "general", child, parent, 0], [ - "wrong-id", - async () => ({ id: "different-host", directory: context.directory }), + "reviewer-child", + "flow-reviewer", + { ...child, agent: "flow-reviewer" }, + parent, + 1, ], + ["primary-replay", "build", { ...parent, id: "child" }, parent, 1], [ - "wrong-directory", - async () => ({ id: context.sessionID, directory: "/another-project" }), + "primary-reviewer", + "flow-reviewer", + { ...parent, id: "child", agent: "flow-reviewer" }, + parent, + 0, ], [ - "failed", - async () => { - throw new Error("Native session lookup failed"); - }, + "nested-reviewer", + "flow-reviewer", + { ...child, agent: "flow-reviewer" }, + { ...parent, parentID: "grandparent" }, + 1, + ], + [ + "worker-parent", + "flow-reviewer", + { ...child, agent: "flow-reviewer" }, + { ...parent, agent: "flow-worker" }, + 1, + ], + [ + "wrong-parent-location", + "flow-reviewer", + { ...child, agent: "flow-reviewer" }, + { ...parent, location: { directory: tmpdir() } }, + 1, ], ] as const)( - "%s native parentage fails closed before mutation", - async (_name, readSession) => { - const leadership = registerFlowPluginInstance( - "/parentage-unknown-" + _name, - { - packageName: "opencode-plugin-flow", - version: "test", - protocolVersion: FLOW_LEADERSHIP_PROTOCOL_VERSION, - instanceId: _name, - }, - ); - try { - let writes = 0; - const auto = new AutoDriveCoordinator({ - readProjection: async () => ({ - status: "idle", - revision: 0, - nextAction: "flow_plan_save", - }), - prompt: async () => {}, - }); - const tools = guardTools( - { - flow_run_start: tool({ - description: "Mutation", - args: {}, - execute: async () => { - writes++; - return "ok"; - }, - }), - }, - leadership, - auto, - readSession, - ); - expect( - JSON.parse(String(await tools.flow_run_start!.execute({}, context))) - .status, - ).toBe("error"); - expect(writes).toBe(0); - } finally { - leadership.release(); - } + "completion origin %s permits verified reviewer children or primary replay", + async (_name, agent, identity, root, writes) => { + expect( + await run("flow_feature_complete", agent, identity, {}, root), + ).toEqual({ writes, status: writes ? "ok" : "error" }); }, ); - -test("native children retain readonly status but cannot request recovery advice", async () => { - const leadership = registerFlowPluginInstance("/parentage-readonly", { +test("canonical workspace aliases retain native authority", async () => { + if (process.platform === "win32") return; + const actual = join(workspace, "project"), + alias = join(workspace, "alias"); + await mkdir(actual); + await symlink(actual, alias, "dir"); + const runtime = registerFlowPluginInstance(actual, { packageName: "opencode-plugin-flow", version: "test", protocolVersion: FLOW_LEADERSHIP_PROTOCOL_VERSION, - instanceId: "readonly", + instanceId: "alias", }); try { - let calls = 0, - lookups = 0; - const auto = new AutoDriveCoordinator({ - readProjection: async () => ({ - status: "idle", - revision: 0, - nextAction: "flow_plan_save", + const tools = guardTools( + { + flow_run_start: { + description: "Start", + input: {}, + execute: async () => ok, + }, + }, + runtime, + { clear() {}, observeMutation() {} }, + async () => ({ + id: "child", + agent: "build", + location: { directory: alias }, }), - prompt: async () => {}, - }); + ); + expect( + await tools.flow_run_start?.execute( + {}, + { ...context, agent: "build", directory: actual }, + ), + ).toEqual(ok); + } finally { + runtime.release(); + } +}); +test("accepted mutation values notify continuation before host serialization", async () => { + const runtime = registerFlowPluginInstance(workspace, { + packageName: "opencode-plugin-flow", + version: "test", + protocolVersion: FLOW_LEADERSHIP_PROTOCOL_VERSION, + instanceId: "mutation", + }); + const mutations: unknown[][] = []; + const value = { + status: "ok", + summary: "Started.", + workflowData: { + operation: { replayed: false }, + projection: { revision: 7, sessionId: "flow" }, + }, + } as const; + try { const tools = guardTools( { - flow_status: tool({ - description: "Status", - args: {}, - execute: async () => { - calls++; - return JSON.stringify({ status: "ok" }); - }, - }), + flow_run_start: { + description: "Start", + input: {}, + execute: async () => value, + }, }, - leadership, - auto, - async () => { - lookups++; - return nativeChild; + runtime, + { + clear() {}, + observeMutation(...args) { + mutations.push(args); + }, }, + async () => ({ ...parent, id: "child" }), ); expect( - JSON.parse( - String( - await tools.flow_status!.execute( - { request: { view: "reviewer-evidence" } }, - context, - ), - ), - ).status, - ).toBe("ok"); - expect(lookups).toBe(0); - expect( - JSON.parse( - String( - await tools.flow_status!.execute({ recoveryProposal: {} }, context), - ), - ).status, - ).toBe("error"); - expect(calls).toBe(1); + await tools.flow_run_start?.execute({}, { ...context, agent: "build" }), + ).toEqual(value); + expect(mutations).toEqual([ + ["child", 7, undefined, "assistant", false, undefined], + ]); } finally { - leadership.release(); + runtime.release(); } }); -test("another verified primary native host can reach existing manual recovery policy", async () => { - const leadership = registerFlowPluginInstance("/parentage-primary", { +test("revocation during native identity lookup prevents the application mutation", async () => { + const runtime = registerFlowPluginInstance(workspace, { packageName: "opencode-plugin-flow", version: "test", protocolVersion: FLOW_LEADERSHIP_PROTOCOL_VERSION, - instanceId: "primary", + instanceId: "revoked", }); + let revoked = false, + writes = 0; try { - let calls = 0; - const auto = new AutoDriveCoordinator({ - readProjection: async () => ({ - status: "idle", - revision: 0, - nextAction: "flow_plan_save", - }), - prompt: async () => {}, - }); const tools = guardTools( { - flow_feature_reset: tool({ - description: "Reset", - args: {}, + flow_run_start: { + description: "Start", + input: {}, execute: async () => { - calls++; - return JSON.stringify({ status: "ok" }); + writes++; + return ok; }, - }), + }, + }, + runtime, + { clear() {}, observeMutation() {} }, + async () => { + revoked = true; + return { ...parent, id: "child" }; }, - leadership, - auto, - async (id) => ({ id, directory: context.directory }), ); - expect( - JSON.parse( - String( - await tools.flow_feature_reset!.execute( - {}, - { ...context, sessionID: "other-primary" }, - ), - ), - ).status, - ).toBe("ok"); - expect(calls).toBe(1); + await expect( + tools.flow_run_start?.execute( + {}, + { + ...context, + agent: "build", + assertCurrent: async () => { + if (revoked) throw new Error("revoked turn"); + }, + }, + ), + ).rejects.toThrow("revoked turn"); + expect(writes).toBe(0); } finally { - leadership.release(); + runtime.release(); } }); -test.each([ - ["generic-child", { ...nativeChild }, "general", 0], - ["reviewer-child", { ...nativeChild }, "flow-reviewer", 1], - [ - "primary-replay", - { id: context.sessionID, directory: context.directory }, - "build", - 1, - ], - [ - "primary-reviewer", - { id: context.sessionID, directory: context.directory }, - "flow-reviewer", - 0, - ], - ["unknown", undefined, "flow-reviewer", 0], - [ - "invalid-directory", - { ...nativeChild, directory: "/another-project" }, - "flow-reviewer", - 0, - ], -] as const)( - "completion origin %s preserves only reviewer submission or primary replay", - async (name, identity, agent, expected) => { - const leadership = registerFlowPluginInstance( - "/completion-origin-" + name, - { - packageName: "opencode-plugin-flow", - version: "test", - protocolVersion: FLOW_LEADERSHIP_PROTOCOL_VERSION, - instanceId: name, - }, - ); +test.each([true, false])( + "accepted mutation replay=%s only notifies on a new durable effect", + async (replayed) => { + const runtime = registerFlowPluginInstance(workspace, { + packageName: "opencode-plugin-flow", + version: "test", + protocolVersion: FLOW_LEADERSHIP_PROTOCOL_VERSION, + instanceId: "replay", + }); + let mutations = 0; try { - let calls = 0; - const auto = new AutoDriveCoordinator({ - readProjection: async () => ({ - status: "idle", - revision: 0, - nextAction: "flow_plan_save", - }), - prompt: async () => {}, - }); const tools = guardTools( { - flow_feature_complete: tool({ - description: "Existing application submission/replay boundary", - args: {}, - execute: async () => { - calls++; - return JSON.stringify({ status: "ok" }); - }, - }), + flow_session_close: { + description: "Close", + input: {}, + execute: async () => ({ + status: "error", + summary: "Archive pending.", + workflowData: { + operation: { replayed }, + closeState: { durableAccepted: true }, + projection: { revision: 8 }, + }, + }), + }, + }, + runtime, + { + clear() {}, + observeMutation() { + mutations++; + }, }, - leadership, - auto, - async () => identity, + async () => ({ ...parent, id: "child" }), ); - const result = JSON.parse( - String( - await tools.flow_feature_complete!.execute({}, { ...context, agent }), - ), + const value = await tools.flow_session_close?.execute( + {}, + { ...context, agent: "build" }, ); - expect(calls).toBe(expected); - expect(result.status).toBe(expected === 1 ? "ok" : "error"); + expect(value).toMatchObject({ + status: "error", + workflowData: { closeState: { durableAccepted: true } }, + }); + expect(mutations).toBe(replayed ? 0 : 1); } finally { - leadership.release(); + runtime.release(); } }, ); + +test("duplicate runtimes reject effects with the application envelope and guidance Markdown", async () => { + const identity = { + packageName: "opencode-plugin-flow", + version: "test", + protocolVersion: FLOW_LEADERSHIP_PROTOCOL_VERSION, + }; + const first = registerFlowPluginInstance(workspace, { + ...identity, + instanceId: "one", + }); + const second = registerFlowPluginInstance(workspace, { + ...identity, + instanceId: "two", + }); + let effects = 0; + const definition = { + description: "Operation", + input: {}, + execute: async () => { + effects++; + return ok; + }, + }; + try { + const tools = guardTools( + { flow_run_start: definition, flow_guidance: definition }, + first, + { clear() {}, observeMutation() {} }, + async () => parent, + ); + expect(await tools.flow_run_start?.execute({}, context)).toMatchObject({ + status: "error", + workflowData: { runtimeGuard: { reason: "duplicate-instances" } }, + }); + const guidance = await tools.flow_guidance?.execute({}, context); + expect(typeof guidance === "string" && guidance).toContain("Recovery:"); + expect(effects).toBe(0); + } finally { + first.release(); + second.release(); + } +}); + +test("a verified reviewer child of a nonroot parent needs no parent lookup", async () => { + const runtime = registerFlowPluginInstance(workspace, { + packageName: "opencode-plugin-flow", + version: "test", + protocolVersion: FLOW_LEADERSHIP_PROTOCOL_VERSION, + instanceId: "nested-reviewer", + }); + const lookups: string[] = []; + try { + const tools = guardTools( + { + flow_feature_complete: { + description: "Complete", + input: {}, + execute: async () => ok, + }, + }, + runtime, + { clear() {}, observeMutation() {} }, + async (id) => { + lookups.push(id); + if (id !== child.id) + throw new Error( + "Parent lookup is outside the reviewer-child contract.", + ); + return { ...child, agent: "flow-reviewer", parentID: "nonroot-parent" }; + }, + ); + expect( + await tools.flow_feature_complete?.execute( + {}, + { ...context, agent: "flow-reviewer" }, + ), + ).toEqual(ok); + expect(lookups).toEqual([child.id]); + } finally { + runtime.release(); + } +}); diff --git a/tests/typesafe-key-env.test.ts b/tests/typesafe-key-env.test.ts new file mode 100644 index 00000000..133d187e --- /dev/null +++ b/tests/typesafe-key-env.test.ts @@ -0,0 +1,393 @@ +import { expect, spyOn, test } from "bun:test"; +import { mkdtemp, rm } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { z } from "zod"; +import type { DecisionProvider } from "../src/application/ports/decision-provider.js"; +import * as jev from "../src/infrastructure/jev-decision-provider.js"; +import FlowPlugin from "../src/platform/opencode/plugin.js"; +import { resolveTypesafeApiKeyReader } from "../src/platform/opencode/typesafe-key-env.js"; +import { + createNativeFlowHost, + nativeText, +} from "./fixtures/native-flow-host.js"; + +const warning = + "Flow plugin option typesafeApiKeyEnv must be a portable environment variable name; Typesafe advice is disabled."; +const fixtureSchema = z.object({ + pluginOptions: z.record(z.string(), z.unknown()), + args: z.string(), +}); + +async function pluginFixture() { + const root = process.env.XDG_STATE_HOME; + if (!root) throw new Error("Missing isolated fixture directory."); + const options = fixtureSchema.parse(JSON.parse(process.argv[3] ?? "null")); + const native = await createNativeFlowHost(root, options.pluginOptions); + const providers: DecisionProvider[] = []; + const factory = jev.createJevDecisionProvider; + const providerSpy = spyOn( + jev, + "createJevDecisionProvider", + ).mockImplementation((readApiKey) => { + const provider = factory(readApiKey); + providers.push(provider); + return provider; + }); + const requests: Array<{ authorization: string | null; body: string }> = []; + const transport = spyOn(globalThis, "fetch").mockImplementation( + Object.assign( + async ( + _url: Parameters[0], + init?: Parameters[1], + ) => { + requests.push({ + authorization: new Headers(init?.headers).get("Authorization"), + body: String(init?.body), + }); + return Response.json({ + model: "jev-1.13.0", + answers: { + choice: { + type: "choice", + choice: "repair", + probabilities: { repair: 1, abstain: 0 }, + confidence: 1, + }, + goal_0: { type: "noul", noul: 1 }, + fit_0: { type: "noul", noul: 1 }, + }, + usage: { input_tokens: 10, output_tokens: 5 }, + }); + }, + { preconnect() {} }, + ), + ); + try { + await native.load(FlowPlugin); + await native.command("flow-auto", options.args); + const input = { request: { view: "compact" } }; + const status = await native.invoke( + "flow_status", + input, + await native.toolContext("flow_status", input), + ); + const provider = providers[0]; + if (providers.length !== 1 || !provider) + throw new Error("Expected the production plugin's decision provider."); + const requestsBeforeAssessment = requests.length; + const advice = await provider.assess( + { + sessionId: "fixture", + revision: 3, + sourceDigest: "sha256:abc", + goal: "Fix parser", + planDigest: "abc", + rubric: "recovery-v1", + findings: [{ id: "f", summary: "Null input", evidence: "parser.ts" }], + candidates: [ + { + id: "repair", + action: "retry", + featureId: "parser", + remedy: "Guard null", + changedFromPreviousAttempt: "Check before coercion", + findingIds: ["f"], + }, + ], + }, + { signal: new AbortController().signal, reserveAttempt: () => true }, + ); + console.log( + JSON.stringify({ + status: JSON.parse(nativeText(status)), + advice, + requests, + requestsBeforeAssessment, + }), + ); + } finally { + await native.dispose(); + providerSpy.mockRestore(); + transport.mockRestore(); + } +} + +if (process.argv[2] === "--plugin-fixture") { + await pluginFixture(); +} else { + test.each([ + { + name: "default", + pluginOptions: {}, + env: { TYPESAFE_API_KEY: "synthetic-default" }, + expected: "synthetic-default", + }, + { name: "default absent", pluginOptions: {}, env: {}, expected: undefined }, + { + name: "custom only", + pluginOptions: { typesafeApiKeyEnv: "MY_TYPESAFE_KEY" }, + env: { MY_TYPESAFE_KEY: "synthetic-custom" }, + expected: "synthetic-custom", + }, + { + name: "custom overrides default", + pluginOptions: { typesafeApiKeyEnv: "MY_TYPESAFE_KEY" }, + env: { + TYPESAFE_API_KEY: "synthetic-wrong", + MY_TYPESAFE_KEY: "synthetic-custom", + }, + expected: "synthetic-custom", + }, + { + name: "custom absent with default populated", + pluginOptions: { typesafeApiKeyEnv: "MY_TYPESAFE_KEY" }, + env: { TYPESAFE_API_KEY: "synthetic-wrong" }, + expected: undefined, + }, + { + name: "custom empty with default populated", + pluginOptions: { typesafeApiKeyEnv: "MY_TYPESAFE_KEY" }, + env: { TYPESAFE_API_KEY: "synthetic-wrong", MY_TYPESAFE_KEY: "" }, + expected: undefined, + }, + { + name: "custom whitespace with default populated", + pluginOptions: { typesafeApiKeyEnv: "MY_TYPESAFE_KEY" }, + env: { TYPESAFE_API_KEY: "synthetic-wrong", MY_TYPESAFE_KEY: " \t\n " }, + expected: undefined, + }, + { + name: "default empty", + pluginOptions: {}, + env: { TYPESAFE_API_KEY: "" }, + expected: undefined, + }, + { + name: "default whitespace", + pluginOptions: {}, + env: { TYPESAFE_API_KEY: " \t\n " }, + expected: undefined, + }, + { + name: "trimmed custom", + pluginOptions: { typesafeApiKeyEnv: "_my_key2" }, + env: { _my_key2: " synthetic-custom \t" }, + expected: "synthetic-custom", + }, + ])("credential selector $name", ({ pluginOptions, env, expected }) => { + const warnings: string[] = []; + const read = resolveTypesafeApiKeyReader({ + pluginOptions, + env, + onWarning: (message) => warnings.push(message), + }); + expect(read()).toBe(expected); + expect(warnings).toEqual([]); + }); + + test.each( + [ + "", + " ", + " MY_KEY", + "MY_KEY ", + "MY-KEY", + "MY.KEY", + "1KEY", + "MY_KEY\n", + "MY_KEY\r", + "MY_KEY\0", + "KÉY", + null, + 42, + {}, + [], + ].map((typesafeApiKeyEnv) => ({ typesafeApiKeyEnv })), + )( + "invalid credential variable name $typesafeApiKeyEnv disables selection without echoing input", + ({ typesafeApiKeyEnv }) => { + const warnings: string[] = []; + const read = resolveTypesafeApiKeyReader({ + pluginOptions: { typesafeApiKeyEnv }, + env: { TYPESAFE_API_KEY: "synthetic-wrong" }, + onWarning: (message) => warnings.push(message), + }); + expect(read()).toBeUndefined(); + expect(warnings).toEqual([warning]); + }, + ); + + test("reader fixes the selected name and reads only its current value", () => { + const pluginOptions = { typesafeApiKeyEnv: "MY_TYPESAFE_KEY" }; + const env = { + MY_TYPESAFE_KEY: "synthetic-first", + get TYPESAFE_API_KEY(): string { + throw new Error("Default must not be read."); + }, + }; + const read = resolveTypesafeApiKeyReader({ pluginOptions, env }); + expect(read()).toBe("synthetic-first"); + pluginOptions.typesafeApiKeyEnv = "TYPESAFE_API_KEY"; + env.MY_TYPESAFE_KEY = "synthetic-second"; + expect(read()).toBe("synthetic-second"); + }); + test.each(["constructor", "toString", "__proto__"])( + "portable name %s reads only an own environment entry", + (typesafeApiKeyEnv) => { + const pluginOptions = { typesafeApiKeyEnv }; + expect( + resolveTypesafeApiKeyReader({ pluginOptions, env: {} })(), + ).toBeUndefined(); + expect( + resolveTypesafeApiKeyReader({ + pluginOptions, + env: Object.fromEntries([[typesafeApiKeyEnv, "synthetic-own-value"]]), + })(), + ).toBe("synthetic-own-value"); + }, + ); + + for (const scenario of [ + { + name: "default", + pluginOptions: {}, + keys: { TYPESAFE_API_KEY: "synthetic-default" }, + mode: "shadow", + authorization: "Bearer synthetic-default", + }, + { + name: "custom only", + pluginOptions: { typesafeApiKeyEnv: "MY_TYPESAFE_KEY" }, + keys: { MY_TYPESAFE_KEY: "synthetic-custom" }, + mode: "shadow", + authorization: "Bearer synthetic-custom", + }, + { + name: "custom overrides default", + pluginOptions: { typesafeApiKeyEnv: "MY_TYPESAFE_KEY" }, + keys: { + TYPESAFE_API_KEY: "synthetic-wrong", + MY_TYPESAFE_KEY: "synthetic-custom", + }, + mode: "shadow", + authorization: "Bearer synthetic-custom", + }, + { + name: "custom missing", + pluginOptions: { typesafeApiKeyEnv: "MY_TYPESAFE_KEY" }, + keys: { TYPESAFE_API_KEY: "synthetic-wrong" }, + mode: "off", + }, + { + name: "custom blank", + pluginOptions: { typesafeApiKeyEnv: "MY_TYPESAFE_KEY" }, + keys: { TYPESAFE_API_KEY: "synthetic-wrong", MY_TYPESAFE_KEY: " \t " }, + mode: "off", + }, + { + name: "invalid name", + pluginOptions: { typesafeApiKeyEnv: "bad-private-input" }, + keys: { TYPESAFE_API_KEY: "synthetic-wrong" }, + mode: "off", + warning: true, + }, + { + name: "explicit command off", + pluginOptions: { typesafeApiKeyEnv: "MY_TYPESAFE_KEY" }, + keys: { MY_TYPESAFE_KEY: "synthetic-custom" }, + args: "--recovery=off", + mode: "off", + authorization: "Bearer synthetic-custom", + }, + { + name: "internal plugin off", + pluginOptions: { + typesafeApiKeyEnv: "MY_TYPESAFE_KEY", + recovery: { mode: "off" }, + }, + keys: { + TYPESAFE_API_KEY: "synthetic-wrong", + MY_TYPESAFE_KEY: "synthetic-custom", + }, + mode: "off", + }, + ]) { + test(`production plugin setup ${scenario.name}`, async () => { + const root = await mkdtemp(join(tmpdir(), "flow-typesafe-env-")); + let child: ReturnType | undefined; + let timer: ReturnType | undefined; + try { + const fixture = Bun.spawn( + [ + process.execPath, + import.meta.path, + "--plugin-fixture", + JSON.stringify({ + pluginOptions: scenario.pluginOptions, + args: scenario.args ?? "", + }), + ], + { + env: { + PATH: "/usr/bin:/bin", + XDG_STATE_HOME: root, + ...scenario.keys, + }, + stdout: "pipe", + stderr: "pipe", + }, + ); + child = fixture; + timer = setTimeout(() => child?.kill("SIGKILL"), 10000); + const [stdout, stderr, exitCode] = await Promise.all([ + new Response(fixture.stdout).text(), + new Response(fixture.stderr).text(), + fixture.exited, + ]); + expect({ exitCode, stderr: stderr.includes("error") }).toEqual({ + exitCode: 0, + stderr: false, + }); + const result = JSON.parse(stdout); + expect(result.requestsBeforeAssessment).toBe(0); + expect(result.status.status).toBe("ok"); + expect(result.status.workflowData.recoveryStatus.mode).toBe( + scenario.mode, + ); + if (scenario.mode === "shadow") + expect(result.status.workflowData.recoveryStatus).toMatchObject({ + remainingCalls: 6, + maxUsd: 0.02, + }); + if (scenario.authorization) { + expect(result.advice).toMatchObject({ + kind: "answered", + model: "jev-1.13.0", + choice: "repair", + }); + expect(result.requests).toHaveLength(1); + expect(result.requests[0].authorization).toBe(scenario.authorization); + expect(result.requests[0].body).not.toContain("synthetic-"); + } else { + expect(result.advice).toMatchObject({ + kind: "unavailable", + reason: "missing-key", + }); + expect(result.requests).toEqual([]); + } + expect(JSON.stringify(result.status)).not.toContain("synthetic-"); + expect(stderr).not.toContain("synthetic-"); + expect(stderr).not.toContain("bad-private-input"); + expect(stderr.includes(warning)).toBe(scenario.warning ?? false); + } finally { + if (timer) clearTimeout(timer); + if (child && child.exitCode === null) { + child.kill("SIGKILL"); + await child.exited; + } + await rm(root, { recursive: true, force: true }); + } + }, 15000); + } +} diff --git a/tests/validation-capture.test.ts b/tests/validation-capture.test.ts index 4f52db45..de997677 100644 --- a/tests/validation-capture.test.ts +++ b/tests/validation-capture.test.ts @@ -1,4 +1,4 @@ -import { describe, expect, test } from "bun:test"; +import { afterAll, expect, test } from "bun:test"; import { createHash } from "node:crypto"; import { mkdir, mkdtemp, rm, symlink, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; @@ -11,18 +11,28 @@ import type { SourceDigest, ValidationObservation, } from "../src/domain/session.js"; -import type { AutoValidationReceipt } from "../src/platform/opencode/auto-drive.js"; +import { + Agent, + CallID, + type NativeToolAfter, + type NativeToolBefore, + NativeToolError, + type OpenCodeEvent, + Session, + SessionMessage, +} from "../src/platform/opencode/sdk.js"; import { readHostToolOutput, ValidationCaptureCoordinator, - ValidationCaptureError, } from "../src/platform/opencode/validation-capture.js"; -const SOURCE = `sha256:${"a".repeat(64)}` as SourceDigest; +const workspace = await mkdtemp(join(tmpdir(), "flow-native-validation-")); +afterAll(() => rm(workspace, { recursive: true, force: true })); +const SOURCE: SourceDigest = `sha256:${"a".repeat(64)}`; const prepared: PreparedValidation = { - featureId: "runtime-kernel", + featureId: "runtime", runId: "run-1", - command: "bun test tests/runtime-gates.test.ts", + command: "bun test", scope: "focused", sourceDigest: SOURCE, hostPlatform: "linux", @@ -30,11 +40,7 @@ const prepared: PreparedValidation = { assertions: [], resultsPath: undefined, }; - -function persistedObservation( - input: ObservedValidation, - recordedRevision = 4, -): ValidationObservation { +function persisted(input: ObservedValidation): ValidationObservation { return { id: input.captureId, featureId: input.featureId, @@ -46,9 +52,7 @@ function persistedObservation( exitCode: input.exitCode, outputDigest: input.outputDigest, outputComplete: input.outputComplete, - recordedRevision, - ...(input.hostPlatform ? { hostPlatform: input.hostPlatform } : {}), - ...(input.resultsPath ? { resultsPath: input.resultsPath } : {}), + recordedRevision: 4, ...(input.observedAssertions ? { observedAssertions: input.observedAssertions } : {}), @@ -57,744 +61,585 @@ function persistedObservation( : {}), }; } - -const REPORT = ``; - -test("emits one source-bound receipt after observation persistence", async () => { - const origin = { - hostSessionId: "host", - sessionId: "flow", - authority: "user", - assistantId: "manager", - assertions: prepared.assertions, - command: prepared.command, - scope: prepared.scope, - hostPlatform: prepared.hostPlatform, - intent: prepared.intent, - featureId: prepared.featureId, - runId: prepared.runId, - sourceDigest: SOURCE, +function before(overrides: Partial = {}): NativeToolBefore { + return { + tool: "shell", + sessionID: Session.ID.make("ses_host"), + messageID: SessionMessage.ID.make("msg_assistant"), + id: CallID.make("call"), + agent: Agent.ID.make("build"), + input: { command: prepared.command }, + ...overrides, }; - const receipts: AutoValidationReceipt[] = []; - let accept!: (observation: ValidationObservation) => void; - let pending: ObservedValidation | undefined; +} +function after( + output: unknown = { + exit: 0, + truncated: false, + status: "completed", + output: "passed", + }, + metadata: object = {}, + changes: Partial = {}, +): NativeToolAfter { + return { + ...before(changes), + status: "completed", + result: { output, metadata, content: [{ type: "text", text: "passed" }] }, + }; +} +function fixture( + options: Partial< + ConstructorParameters[0] + > = {}, +) { + const observations: ObservedValidation[] = []; const capture = new ValidationCaptureCoordinator({ - randomId: () => "armed-capture", - persistObservation: (_workspace, input) => { - pending = input; - return new Promise((resolve) => { - accept = resolve; - }); - }, - onRecorded: (receipt) => { - receipts.push(receipt); + readSession: async (id) => ({ + id, + agent: "build", + location: { directory: workspace }, + }), + persistObservation: async (_workspace, value) => { + observations.push(value); + return persisted(value); }, + randomId: () => "capture-1", + ...options, }); - const command = prepared.command; - capture.arm("host", "/workspace", prepared, origin); - await capture.observeToolBefore( - { tool: "bash", sessionID: "host", callID: "bash" }, - { args: { command } }, - ); - const output = { - title: "check", - output: "passed", - metadata: { exit: 0, truncated: false }, - }; - const input = { - tool: "bash", - sessionID: "host", - callID: "bash", - args: { command }, - }; - const completing = capture.observeToolAfter(input, output); - await Promise.resolve(); - expect(receipts).toEqual([]); - if (!pending) throw new Error("Observation persistence did not start."); - const observed = persistedObservation(pending, 4); - accept(observed); - expect(await completing).toEqual(observed); - expect(receipts).toEqual([ - { origin, captureId: "armed-capture", observation: observed }, - ]); - expect(pending).not.toHaveProperty("origin"); - expect(await capture.observeToolAfter(input, output)).toBeNull(); - expect(receipts).toHaveLength(1); -}); + return { capture, observations }; +} +function digest(text: string): `sha256:${string}` { + return `sha256:${createHash("sha256").update(text).digest("hex")}`; +} -test("a continuation diagnostic failure cannot erase a durable observation", async () => { - const origin = { - hostSessionId: "host", - sessionId: "flow", - authority: "user", - assistantId: "manager", - assertions: prepared.assertions, - command: prepared.command, - scope: prepared.scope, - hostPlatform: prepared.hostPlatform, - intent: prepared.intent, - featureId: prepared.featureId, - runId: prepared.runId, - sourceDigest: SOURCE, - }; - const capture = new ValidationCaptureCoordinator({ - randomId: () => "accepted-capture", - persistObservation: async (_workspace, input) => - persistedObservation(input), - onRecorded: () => { - throw new Error("diagnostic sink unavailable"); +test("persists the exact source-bound native shell observation and appends a receipt", async () => { + const { capture, observations } = fixture(); + capture.arm("ses_host", workspace, prepared); + await capture.observeToolBefore(before()); + const completed = after(); + const result = await capture.observeToolAfter(completed); + expect(observations).toEqual([ + { + ...prepared, + captureId: "capture-1", + exitCode: 0, + outputComplete: true, + outputDigest: digest("passed"), }, - onReceiptError: () => { - throw new Error("warning sink unavailable"); + ]); + const observation = observations[0]; + if (!observation) throw new Error("Observation was not persisted."); + expect(result).toEqual(persisted(observation)); + expect(completed.status === "completed" && completed.result.content).toEqual([ + { + type: "text", + text: 'passed\n\n[flow-validation] {"id":"capture-1","scope":"focused","intent":"pass","passed":true,"observed":false,"recordedRevision":4}', }, + ]); + expect(capture.pendingCount()).toBe(0); +}); +test.each([ + [ + "nonzero-completed", + { exit: 7, truncated: false, status: "completed", output: "failed" }, + {}, + 7, + true, + undefined, + ], + [ + "timeout", + { truncated: false, status: "completed", output: "timeout" }, + { timeout: true, truncated: false }, + null, + false, + "exit-code-unavailable", + ], + [ + "unknown-exit", + { truncated: false, output: "unknown" }, + {}, + null, + false, + "exit-code-unavailable", + ], + [ + "unknown-completeness", + { exit: 0, output: "unknown" }, + {}, + 0, + false, + "output-completeness-unknown", + ], + [ + "truncated", + { exit: 0, truncated: true, output: "partial" }, + {}, + 0, + false, + undefined, + ], + [ + "conflicting-exit", + { exit: 0, truncated: false, output: "conflict" }, + { exit: 7 }, + null, + false, + "exit-code-unavailable", + ], + [ + "conflicting-truncation", + { exit: 0, truncated: false, output: "conflict" }, + { truncated: true }, + null, + false, + "exit-code-unavailable", + ], + [ + "killed", + { exit: 0, truncated: false, status: "killed", output: "killed" }, + {}, + null, + false, + "exit-code-unavailable", + ], +] as const)( + "%s keeps truthful exit and completeness evidence", + async (_name, output, metadata, exitCode, outputComplete, ineligibleReason) => { + const { capture, observations } = fixture(); + capture.arm("ses_host", workspace, prepared); + await capture.observeToolBefore(before()); + await capture.observeToolAfter(after(output, metadata)); + expect(observations[0]).toMatchObject({ exitCode, outputComplete }); + expect(observations[0]?.ineligibleReason).toBe(ineligibleReason); + }, +); +test("native tool errors are durable ineligible observations", async () => { + const { capture, observations } = fixture(); + capture.arm("ses_host", workspace, prepared); + await capture.observeToolBefore(before()); + await capture.observeToolAfter({ + ...before(), + status: "error", + error: new NativeToolError({ message: "invalid workdir" }), + }); + expect(observations[0]).toMatchObject({ + exitCode: null, + outputComplete: false, + outputDigest: digest("invalid workdir"), + ineligibleReason: "exit-code-unavailable", }); - const command = prepared.command; - capture.arm("host", "/workspace", prepared, origin); - await capture.observeToolBefore( - { tool: "bash", sessionID: "host", callID: "bash" }, - { args: { command } }, - ); - const output = { - title: "check", - output: "passed", - metadata: { exit: 0, truncated: false }, - }; - const result = await capture.observeToolAfter( - { tool: "bash", sessionID: "host", callID: "bash", args: { command } }, - output, - ); - expect(result?.id).toBe("accepted-capture"); - expect(output.output).toContain('"passed":true'); - expect(output.output).toContain('"recordedRevision":4'); }); - -test("reads only bounded OpenCode tool-output files", async () => { - const dataHome = await mkdtemp(join(tmpdir(), "flow-host-output-")); - try { - const directory = join(dataHome, "opencode", "tool-output"); - await mkdir(directory, { recursive: true }); - const path = join(directory, "tool_ABC123"); - await writeFile(path, "complete cargo output\n"); - expect(await readHostToolOutput(path, dataHome)).toBe( - "complete cargo output\n", - ); - const textPath = join(directory, "tool_ABC123.txt"); - await writeFile(textPath, "complete text output\n"); - expect(await readHostToolOutput(textPath, dataHome)).toBe( - "complete text output\n", +test.each([ + ["command", { command: "bun test --changed" }], + ["workdir", { command: prepared.command, workdir: tmpdir() }], +] as const)( + "mismatching %s cancels before shell execution", + async (_name, input) => { + const { capture, observations } = fixture(); + capture.arm("ses_host", workspace, prepared); + await expect(capture.observeToolBefore(before({ input }))).rejects.toThrow( + "capture was cancelled", ); - const otherExtension = join(directory, "tool_ABC123.log"); - await writeFile(otherExtension, "unsupported output\n"); - expect(await readHostToolOutput(otherExtension, dataHome)).toBeNull(); - const outside = join(dataHome, "tool_outside"); - await writeFile(outside, "not host output\n"); - await symlink(outside, join(directory, "tool_link")); - expect( - await readHostToolOutput(join(directory, "tool_link"), dataHome), - ).toBeNull(); - expect(await readHostToolOutput(outside, dataHome)).toBeNull(); - } finally { - await rm(dataHome, { recursive: true, force: true }); - } -}); - -test("binds a truncated Bash view to the host's full output file", async () => { - const path = "/isolated/opencode/tool-output/tool_ABC123"; - const full = `test start\n${"case passed\n".repeat(100)}test result: ok\n`; - const visible = `...output truncated...\n\nFull output saved to: ${path}\n\n${full.slice(-150)}`; - let persisted: ObservedValidation | null = null; - const coordinator = new ValidationCaptureCoordinator({ - readFullOutput: async (requested) => (requested === path ? full : null), - persistObservation: async (_workspace, input) => { - persisted = input; - return persistedObservation(input); - }, + expect(observations).toEqual([]); + expect(capture.pendingCount()).toBe(0); + }, +); +test.each(["flow-reviewer", "flow-worker", "flow-planner"])( + "%s cannot capture manager validation", + async (agent) => { + const { capture, observations } = fixture({ + readSession: async (id) => ({ + id, + agent, + location: { directory: workspace }, + }), + }); + capture.arm("ses_host", workspace, prepared); + await expect( + capture.observeToolBefore(before({ agent: Agent.ID.make(agent) })), + ).rejects.toThrow("capture was cancelled"); + expect(observations).toEqual([]); + }, +); +test("wrong session facts and changed locations fail closed", async () => { + let directory = workspace; + const { capture, observations } = fixture({ + readSession: async (id) => ({ + id, + agent: "build", + location: { directory }, + }), }); - coordinator.arm("session", "/workspace", prepared); - await coordinator.observeToolBefore( - { tool: "bash", sessionID: "session", callID: "bash-1" }, - { args: { command: prepared.command } }, + capture.arm("ses_host", workspace, prepared); + await capture.observeToolBefore(before()); + directory = tmpdir(); + await expect(capture.observeToolAfter(after())).rejects.toThrow( + "workspace changed", ); - const output = { - title: "gate", - output: visible, - metadata: { exit: 0, truncated: true, outputPath: path }, - }; - await coordinator.observeToolAfter( - { - tool: "bash", - sessionID: "session", - callID: "bash-1", - args: { command: prepared.command }, - }, - output, - ); - const fullDigest = `sha256:${createHash("sha256").update(full).digest("hex")}`; - expect(persisted).toMatchObject({ - exitCode: 0, - outputComplete: true, - outputDigest: fullDigest, - }); - expect(output.output).toContain(`"fullOutputDigest":"${fullDigest}"`); + expect(observations).toEqual([]); }); - -test("keeps truncated validation ineligible when the host file cannot be bound", async () => { - const path = "/isolated/opencode/tool-output/tool_ABC123"; - for (const [visible, full] of [ - [ - `...output truncated...\n\nFull output saved to: ${path}\n\n${"tail".repeat(20)}`, - "different output", - ], - [ - `Full output saved to: ${path}\n\n${"tail".repeat(20)}`, - "tail".repeat(40), - ], - ] as const) { - let persisted: ObservedValidation | null = null; - const coordinator = new ValidationCaptureCoordinator({ - readFullOutput: async () => full, - persistObservation: async (_workspace, input) => { - persisted = input; - return persistedObservation(input); - }, +test.each([ + ["assistant", { messageID: SessionMessage.ID.make("msg_other") }], + ["call", { id: CallID.make("other-call") }], + ["agent", { agent: Agent.ID.make("other-agent") }], + ["session", { sessionID: Session.ID.make("ses_other") }], +] as const)( + "a repeated call ID cannot cross the %s boundary", + async (_name, identity) => { + const { capture, observations } = fixture(); + capture.arm("ses_host", workspace, prepared); + await capture.observeToolBefore(before()); + expect( + await capture.observeToolAfter(after(undefined, {}, identity)), + ).toBeNull(); + expect(observations).toEqual([]); + expect(await capture.observeToolAfter(after())).toMatchObject({ + id: "capture-1", + exitCode: 0, }); - coordinator.arm("session", "/workspace", prepared); - await coordinator.observeToolBefore( - { tool: "bash", sessionID: "session", callID: "bash-1" }, - { args: { command: prepared.command } }, - ); - await coordinator.observeToolAfter( - { - tool: "bash", - sessionID: "session", - callID: "bash-1", - args: { command: prepared.command }, - }, - { - title: "gate", - output: visible, - metadata: { exit: 0, truncated: true, outputPath: path }, - }, - ); - expect(persisted).toMatchObject({ exitCode: 0, outputComplete: false }); - } + }, +); +test("later shell input cannot relabel the frozen command", async () => { + const { capture, observations } = fixture(); + capture.arm("ses_host", workspace, prepared); + await capture.observeToolBefore(before()); + await expect( + capture.observeToolAfter( + after(undefined, {}, { input: { command: "different" } }), + ), + ).rejects.toThrow("command or workspace changed"); + expect(observations).toEqual([]); }); - -/** - * Runs one capture to completion and returns what was persisted. - * - * Written as a helper because the interesting variable is only ever the report and - * when it was written; everything else about the capture is the same each time. - */ -async function capture(options: { - prepared: PreparedValidation; - priorReport?: { text: string; modifiedMs: number } | null; - readReport?: ( - workspace: string, - relativePath: string, - ) => Promise<{ text: string; modifiedMs: number } | null>; - armedAt: number; - observedAt?: number; - onOutput?: (output: string) => void; -}): Promise { - let persisted: ObservedValidation | null = null; - let now = options.armedAt; - let reportRead = 0; - const coordinator = new ValidationCaptureCoordinator({ - randomId: () => "capture-1", - now: () => now, - ...(options.readReport || options.priorReport - ? { - readReport: async (workspace, relativePath) => { - if (reportRead++ === 0) return options.priorReport ?? null; - return options.readReport?.(workspace, relativePath) ?? null; - }, - } - : {}), - persistObservation: (_workspace, input) => { - persisted = input; - return Promise.resolve(persistedObservation(input)); - }, +test("only the native shell ID binds capture", async () => { + const { capture, observations } = fixture(); + capture.arm("ses_host", workspace, prepared); + await capture.observeToolBefore(before({ tool: "bash" })); + expect(await capture.observeToolAfter(after())).toBeNull(); + expect(observations).toEqual([]); + await capture.observeToolBefore(before()); + expect(await capture.observeToolAfter(after())).toMatchObject({ + exitCode: 0, }); - coordinator.arm("session", "/workspace", options.prepared); - await coordinator.observeToolBefore( - { tool: "bash", sessionID: "session", callID: "bash-1" }, - { args: { command: options.prepared.command } }, +}); +test("expiry, cancellation, duplicate arming, and clear release captures", () => { + let now = 1; + const { capture } = fixture({ now: () => now }); + capture.arm("ses_host", workspace, prepared); + expect(() => capture.arm("ses_host", workspace, prepared)).toThrow( + "already has an armed", ); - now = options.observedAt ?? options.armedAt + 1_000; - const output = { - title: "gate", - output: "1 pass", - metadata: { exit: 0, truncated: false }, - }; - await coordinator.observeToolAfter( - { - tool: "bash", - sessionID: "session", - callID: "bash-1", - args: { command: options.prepared.command }, - }, - output, + expect(() => capture.arm("other", workspace, prepared)).toThrow( + "workspace already has", ); - options.onOutput?.(output.output); - if (!persisted) throw new Error("Nothing was persisted."); - return persisted; -} - -describe("observing declared assertions from the command's own report", () => { - const declared: PreparedValidation = { - ...prepared, - assertions: ["on Windows"], - resultsPath: "junit.xml", - }; - test("zero exit cannot label skipped, failed or absent named proof as passed", async () => { - for (const report of [ - REPORT.replace("/>", ">"), - REPORT.replace("/>", ">"), - null, - ]) { - let marker = ""; - const observed = await capture({ - prepared: declared, - armedAt: 1_000, - readReport: async () => - report === null ? null : { text: report, modifiedMs: 2_000 }, - onOutput: (output) => { - marker = output; - }, - }); - expect(observed.exitCode).toBe(0); - expect(marker).toContain('"passed":false,"observed":false'); - } - }); - - test("records what the report says when the command wrote it", async () => { - const observed = await capture({ - prepared: declared, - armedAt: 1_000, - readReport: async () => ({ text: REPORT, modifiedMs: 2_000 }), + expect(capture.cancel("ses_host")).toBe(true); + capture.arm("ses_host", workspace, prepared); + now += 16 * 60 * 1000; + expect(capture.pendingCount()).toBe(0); + capture.arm("ses_host", workspace, prepared); + capture.clear(); + expect(capture.pendingCount()).toBe(0); +}); +const report = + ''; +test.each([ + ["fresh", 101, "passed"], + ["equal-arming-time", 100, "absent"], + ["future", 201, "absent"], + ["stale", 99, "absent"], +] as const)( + "%s JUnit freshness preserves declared assertion evidence", + async (_name, modifiedMs, status) => { + let now = 100, + reads = 0; + const { capture, observations } = fixture({ + now: () => now, + readReport: async () => + ++reads === 1 ? null : { text: report, modifiedMs }, }); - expect(observed.observedAssertions).toEqual([ - { name: "on Windows", status: "passed" }, + capture.arm("ses_host", workspace, { + ...prepared, + assertions: ["suite.critical"], + resultsPath: "results.xml", + }); + await capture.observeToolBefore(before()); + now = 200; + await capture.observeToolAfter(after()); + expect(observations[0]?.observedAssertions).toEqual([ + { name: "suite.critical", status }, ]); - expect(observed.resultsPath).toBe("junit.xml"); - }); - - test("ignores a report that does not postdate the arming", async () => { - // The path is the one half of this a caller supplies, so a report left from an - // earlier run — or committed by hand — would otherwise discharge an entry no - // command produced anything for. Exit zero is unchanged; the claim is not. - // - // The equal case is the one that was open: mtime granularity is a whole second on - // some filesystems, so "same millisecond as arming" is not a hypothetical width. - for (const modifiedMs of [4_999, 5_000]) { - const observed = await capture({ - prepared: declared, - armedAt: 5_000, - readReport: async () => ({ text: REPORT, modifiedMs }), - }); - expect(observed.observedAssertions).toEqual([ - { name: "on Windows", status: "absent" }, - ]); - expect(observed.exitCode).toBe(0); - } - }); - - test("ignores an unchanged report and a timestamp after command observation", async () => { - const stale = { text: REPORT, modifiedMs: 1_500 }; - for (const options of [ - { priorReport: stale, readReport: async () => stale }, - { - priorReport: null, - readReport: async () => ({ text: REPORT, modifiedMs: 2_001 }), - }, - ]) { - const observed = await capture({ - prepared: declared, - armedAt: 1_000, - observedAt: 2_000, - ...options, - }); - expect(observed.observedAssertions).toEqual([ - { name: "on Windows", status: "absent" }, - ]); - } - }); - - test("reaches the same absence when no report is named or none can be read", async () => { - for (const options of [ - { prepared: { ...declared, resultsPath: undefined }, armedAt: 1 }, - { prepared: declared, armedAt: 1, readReport: async () => null }, - { - prepared: declared, - armedAt: 1, - readReport: () => - Promise.reject( - Object.assign(new Error("unreadable"), { code: "EACCES" }), - ), - }, - ]) { - const observed = await capture(options); - expect(observed.observedAssertions).toEqual([ - { name: "on Windows", status: "absent" }, - ]); - } + }, +); +test("unchanged JUnit cannot certify the just-run command", async () => { + let now = 100; + const { capture, observations } = fixture({ + now: () => now, + readReport: async () => ({ text: report, modifiedMs: 101 }), }); - - test("records nothing when the plan declared no names for this command", async () => { - const observed = await capture({ prepared, armedAt: 1 }); - expect(observed.observedAssertions).toBeUndefined(); + capture.arm("ses_host", workspace, { + ...prepared, + assertions: ["suite.critical"], + resultsPath: "results.xml", }); + await capture.observeToolBefore(before()); + now = 200; + await capture.observeToolAfter(after()); + expect(observations[0]?.observedAssertions).toEqual([ + { name: "suite.critical", status: "absent" }, + ]); }); - -describe("OpenCode validation capture", () => { - test("captures the exact next Bash command and persists its structured observation directly", async () => { - const calls: Array<{ - workspace: string; - input: ObservedValidation; - }> = []; - const coordinator = new ValidationCaptureCoordinator({ - randomId: () => "capture-1", - persistObservation: (workspace, input) => { - calls.push({ workspace, input }); - return Promise.resolve(persistedObservation(input)); - }, - }); - - expect( - coordinator.arm("opencode-session-1", "/workspace", prepared), - ).toEqual({ captureId: "capture-1", expiresInMs: 15 * 60 * 1_000 }); - coordinator.observeToolBefore( - { tool: "read", sessionID: "opencode-session-1", callID: "read-1" }, - { args: { path: "package.json" } }, - ); - expect(coordinator.pendingCount()).toBe(1); - - coordinator.observeToolBefore( - { tool: "bash", sessionID: "opencode-session-1", callID: "bash-1" }, - { args: { command: prepared.command } }, - ); - const output = { - title: "Focused tests", - output: "3 pass\n0 fail", - metadata: { exit: 0, truncated: false }, - }; - const result = await coordinator.observeToolAfter( - { - tool: "bash", - sessionID: "opencode-session-1", - callID: "bash-1", - args: { command: prepared.command }, - }, - output, - ); - - expect(result).toEqual( - expect.objectContaining({ - id: "capture-1", - exitCode: 0, - outputComplete: true, - recordedRevision: 4, - }), - ); - expect(calls).toHaveLength(1); - expect(calls[0]).toMatchObject({ - workspace: "/workspace", - input: { - captureId: "capture-1", - featureId: "runtime-kernel", - runId: "run-1", +function shellEvent( + path: string, + changes: Record = {}, +): OpenCodeEvent { + return { + id: "event", + created: 110, + type: "shell.created", + location: { directory: workspace }, + data: { + info: { + id: "sh_native", command: prepared.command, - scope: "focused", - sourceDigest: SOURCE, - exitCode: 0, - outputComplete: true, + cwd: workspace, + shell: "/bin/sh", + status: "running", + file: path, + metadata: { sessionID: "ses_host" }, + time: { started: 101 }, + ...changes, }, - }); - expect(calls[0]?.input.outputDigest).toMatch(/^sha256:[a-f0-9]{64}$/); - expect(output.output).toContain( - '[flow-validation] {"id":"capture-1","scope":"focused","intent":"pass","passed":true,"observed":false,"recordedRevision":4}', - ); - expect(output.output).not.toContain("/workspace"); - expect(coordinator.pendingCount()).toBe(0); + }, + }; +} +function exited(): OpenCodeEvent { + return { + id: "exit-event", + created: 120, + type: "shell.exited", + data: { id: "sh_native", exit: 0, status: "exited" }, + }; +} +const tail = "native output\n".repeat(100); +function spilled(path: string): NativeToolAfter { + const text = `${tail}\n\n[showing lines 10001-10100 of 10100; full output saved to ${path}]`; + const value = after({ + exit: 0, + truncated: true, + status: "completed", + output: text, }); - - test("cancels when the next Bash command does not exactly match", async () => { - let persisted = false; - const coordinator = new ValidationCaptureCoordinator({ - randomId: () => "capture-mismatch", - persistObservation: (_workspace, input) => { - persisted = true; - return Promise.resolve(persistedObservation(input)); - }, - }); - coordinator.arm("opencode-session-1", "/workspace", prepared); - - await expect( - coordinator.observeToolBefore( - { - tool: "bash", - sessionID: "opencode-session-1", - callID: "bash-wrong", - }, - { args: { command: `${prepared.command} --watch` } }, - ), - ).rejects.toBeInstanceOf(ValidationCaptureError); - expect(coordinator.pendingCount()).toBe(0); - expect(persisted).toBe(false); + if (value.status === "completed") + value.result = { ...value.result, content: [{ type: "text", text }] }; + return value; +} +test("a verified native shell record permits a secure complete managed-output read", async () => { + const dataHome = join(workspace, "managed-data"); + const directory = join(dataHome, "opencode", "shell", "abc123"); + await mkdir(directory, { recursive: true }); + const path = join(directory, "sh_native.out"), + full = "earlier output\n".repeat(1000) + tail; + await writeFile(path, full); + let now = 100; + const { capture, observations } = fixture({ + now: () => now, + readFullOutput: (path) => readHostToolOutput(path, dataHome), }); - - test("expires an unstarted capture after the arming window", () => { - let now = 1_000; - let persisted = false; - const coordinator = new ValidationCaptureCoordinator({ - now: () => now, - randomId: () => "capture-unstarted", - persistObservation: (_workspace, input) => { - persisted = true; - return Promise.resolve(persistedObservation(input)); - }, - }); - coordinator.arm("opencode-session-1", "/workspace", prepared); - - now += 15 * 60 * 1_000 + 1; - expect(coordinator.pendingCount()).toBe(0); - expect(persisted).toBe(false); + capture.arm("ses_host", workspace, prepared); + await capture.observeToolBefore(before()); + now = 200; + capture.observeShellEvent(shellEvent(path)); + capture.observeShellEvent(exited()); + await capture.observeToolAfter(spilled(path)); + expect(observations[0]).toMatchObject({ + outputComplete: true, + outputDigest: digest(full), + exitCode: 0, }); - - test("persists a started capture when its after-hook arrives after the original arming window", async () => { - let now = 1_000; - const observations: ObservedValidation[] = []; - const coordinator = new ValidationCaptureCoordinator({ +}); +test.each([ + ["no-record", null], + ["wrong-command", { command: "other" }], + ["wrong-session", { metadata: { sessionID: "other" } }], + ["wrong-workspace", { cwd: tmpdir() }], + ["old-record", { time: { started: 99 } }], + ["running-record", { status: "running" }], +] as const)( + "%s never trusts a model-visible output path", + async (name, changes) => { + let reads = 0, + now = 100; + const { capture, observations } = fixture({ now: () => now, - randomId: () => "capture-started", - persistObservation: (_workspace, input) => { - observations.push(input); - return Promise.resolve(persistedObservation(input)); + readFullOutput: async () => { + reads++; + return `prefix${tail}`; }, }); - expect( - coordinator.arm("opencode-session-1", "/workspace", prepared), - ).toEqual({ captureId: "capture-started", expiresInMs: 15 * 60 * 1_000 }); - coordinator.observeToolBefore( - { tool: "bash", sessionID: "opencode-session-1", callID: "bash-1" }, - { args: { command: prepared.command } }, - ); - - now += 15 * 60 * 1_000 + 1; - const output = { - title: "Late validation", - output: "1 pass", - metadata: { exit: 0, truncated: false }, - }; - expect( - await coordinator.observeToolAfter( - { - tool: "bash", - sessionID: "opencode-session-1", - callID: "bash-1", - args: { command: prepared.command }, - }, - output, - ), - ).toEqual(expect.objectContaining({ id: "capture-started", exitCode: 0 })); - expect(observations).toHaveLength(1); - expect(output.output).toContain('"passed":true'); - expect(coordinator.pendingCount()).toBe(0); - }); - - test("keeps a started capture until the existing cancellation path resolves it", () => { - let now = 1_000; - const coordinator = new ValidationCaptureCoordinator({ - now: () => now, - randomId: () => "capture-started", - persistObservation: (_workspace, input) => - Promise.resolve(persistedObservation(input)), - }); - coordinator.arm("opencode-session-1", "/workspace", prepared); - coordinator.observeToolBefore( - { tool: "bash", sessionID: "opencode-session-1", callID: "bash-1" }, - { args: { command: prepared.command } }, - ); - - now += 15 * 60 * 1_000 + 1; - expect(coordinator.pendingCount()).toBe(1); - expect(() => - coordinator.arm("opencode-session-1", "/workspace", prepared), - ).toThrow("already has an armed validation command"); - expect(coordinator.cancel("opencode-session-1")).toBe(true); - expect(coordinator.pendingCount()).toBe(0); - }); - - test("allows only one armed capture per workspace", () => { - const coordinator = new ValidationCaptureCoordinator({ - randomId: () => "capture-workspace", - persistObservation: (_workspace, input) => - Promise.resolve(persistedObservation(input)), - }); - coordinator.arm("opencode-session-1", "/workspace", prepared); - expect(() => - coordinator.arm("opencode-session-2", "/workspace", prepared), - ).toThrow("workspace already has an armed validation command"); - expect( - coordinator.arm("opencode-session-2", "/other-workspace", prepared), - ).toMatchObject({ captureId: "capture-workspace" }); - }); - - test("records an ineligible observation when the host reports no exit code or no completeness", async () => { - // Flow must stay usable on a host that reports either fact differently. - // Throwing here would lose the observation entirely; recording it ineligible - // keeps the evidence durable and can never satisfy a gate. - const cases = [ - { - name: "no exit code", - metadata: { truncated: false }, - exitCode: null, - outputComplete: true, - ineligibleReason: "exit-code-unavailable", - }, - { - name: "no completeness flag", - metadata: { exit: 0 }, - exitCode: 0, - outputComplete: false, - ineligibleReason: "output-completeness-unknown", - }, - ] as const; + const path = join(workspace, "sh_native.out"); + capture.arm("ses_host", workspace, prepared); + await capture.observeToolBefore(before()); + now = 200; + if (changes) { + capture.observeShellEvent(shellEvent(path, changes)); + if (name !== "running-record") capture.observeShellEvent(exited()); + } + await capture.observeToolAfter(spilled(path)); + expect(reads).toBe(0); + expect(observations[0]?.outputComplete).toBe(false); + }, +); +test("secure output reader rejects symlinks, unrelated files, and oversized output", async () => { + const dataHome = join(workspace, "reader-data"), + directory = join(dataHome, "opencode", "shell", "abc123"); + await mkdir(directory, { recursive: true }); + const valid = join(directory, "sh_valid.out"); + await writeFile(valid, "complete"); + expect(await readHostToolOutput(valid, dataHome)).toBe("complete"); + const outside = join(workspace, "outside.out"); + await writeFile(outside, "secret"); + expect(await readHostToolOutput(outside, dataHome)).toBeNull(); + if (process.platform !== "win32") { + const link = join(directory, "sh_link.out"); + await symlink(outside, link); + expect(await readHostToolOutput(link, dataHome)).toBeNull(); + } + const huge = join(directory, "sh_huge.out"); + await writeFile(huge, Buffer.alloc(8 * 1024 * 1024 + 1)); + expect(await readHostToolOutput(huge, dataHome)).toBeNull(); +}); - for (const item of cases) { - const observations: ObservedValidation[] = []; - const coordinator = new ValidationCaptureCoordinator({ - randomId: () => `capture-${item.ineligibleReason}`, - persistObservation: (_workspace, input) => { - observations.push(input); - return Promise.resolve(persistedObservation(input)); - }, +test("receipts become available only after source-bound observation persistence", async () => { + const receipts: unknown[] = []; + let accept: ((value: ValidationObservation) => void) | undefined; + let pending: ObservedValidation | undefined; + const origin = { + hostSessionId: "ses_host", + sessionId: "flow", + authority: "user", + assistantId: "msg_assistant", + featureId: prepared.featureId, + runId: prepared.runId, + sourceDigest: prepared.sourceDigest, + command: prepared.command, + scope: prepared.scope, + hostPlatform: prepared.hostPlatform, + intent: prepared.intent, + assertions: prepared.assertions, + }; + const { capture } = fixture({ + persistObservation: async (_workspace, observation) => { + pending = observation; + return new Promise((resolve) => { + accept = resolve; }); - coordinator.arm("opencode-session-1", "/workspace", prepared); - coordinator.observeToolBefore( - { tool: "bash", sessionID: "opencode-session-1", callID: "bash-1" }, - { args: { command: prepared.command } }, - ); - const output = { - title: "Unknown", - output: "ok", - metadata: item.metadata, - }; - const recorded = await coordinator.observeToolAfter( - { - tool: "bash", - sessionID: "opencode-session-1", - callID: "bash-1", - args: { command: prepared.command }, - }, - output, - ); - - expect(observations, item.name).toEqual([ - expect.objectContaining({ - exitCode: item.exitCode, - outputComplete: item.outputComplete, - ineligibleReason: item.ineligibleReason, - }), - ]); - expect(recorded?.ineligibleReason, item.name).toBe(item.ineligibleReason); - expect(output.output, item.name).toContain('"passed":false'); - expect(output.output, item.name).toContain(item.ineligibleReason); - expect(coordinator.pendingCount()).toBe(0); - } + }, + onRecorded: (receipt) => { + receipts.push(receipt); + }, }); + capture.arm("ses_host", workspace, prepared, origin); + await capture.observeToolBefore(before()); + const completion = capture.observeToolAfter(after()); + for (let step = 0; step < 10 && !pending; step++) await Promise.resolve(); + expect(receipts).toEqual([]); + if (!pending || !accept) + throw new Error("Observation persistence did not start."); + const observation = persisted(pending); + accept(observation); + await completion; + expect(receipts).toEqual([{ origin, captureId: "capture-1", observation }]); + expect(pending).not.toHaveProperty("origin"); +}); - test("persists failed and incomplete observations without presenting either as passed", async () => { - const cases = [ - { - name: "failed", - metadata: { exit: 1, truncated: false }, - exitCode: 1, - outputComplete: true, - }, - { - name: "incomplete", - metadata: { exit: 0, truncated: true }, - exitCode: 0, - outputComplete: false, - }, - ] as const; +test("a nonzero completed shell vetoes a passing receipt", async () => { + const { capture } = fixture(); + capture.arm("ses_host", workspace, prepared); + await capture.observeToolBefore(before()); + const completed = after({ + exit: 7, + truncated: false, + status: "completed", + output: "failed", + }); + await capture.observeToolAfter(completed); + const content = + completed.status === "completed" ? completed.result.content : undefined; + const text = + typeof content === "string" + ? content + : content + ?.filter((part) => part.type === "text") + .map((part) => part.text) + .join("\n"); + expect(text).toContain('"passed":false'); +}); - for (const item of cases) { - const observations: ObservedValidation[] = []; - const coordinator = new ValidationCaptureCoordinator({ - randomId: () => `capture-${item.name}`, - persistObservation: (_workspace, input) => { - observations.push(input); - return Promise.resolve(persistedObservation(input)); - }, +test("competing shell-before callbacks cannot replace the first native call", async () => { + let release: (() => void) | undefined; + const { capture, observations } = fixture({ + readSession: async (id) => { + await new Promise((resolve) => { + release = resolve; }); - coordinator.arm(`session-${item.name}`, "/workspace", prepared); - coordinator.observeToolBefore( - { - tool: "bash", - sessionID: `session-${item.name}`, - callID: `bash-${item.name}`, - }, - { args: { command: prepared.command } }, - ); - const output = { - title: item.name, - output: `${item.name} output`, - metadata: item.metadata, - }; - await coordinator.observeToolAfter( - { - tool: "bash", - sessionID: `session-${item.name}`, - callID: `bash-${item.name}`, - args: { command: prepared.command }, - }, - output, - ); - - expect(observations).toEqual([ - expect.objectContaining({ - captureId: `capture-${item.name}`, - exitCode: item.exitCode, - outputComplete: item.outputComplete, - }), - ]); - expect(output.output).toContain('"passed":false'); - } + return { id, agent: "build", location: { directory: workspace } }; + }, }); + capture.arm("ses_host", workspace, prepared); + const first = capture.observeToolBefore(before()); + await capture.observeToolBefore(before({ id: CallID.make("second") })); + if (!release) throw new Error("First native identity read did not start."); + release(); + await first; + expect( + await capture.observeToolAfter( + after(undefined, {}, { id: CallID.make("second") }), + ), + ).toBeNull(); + expect(observations).toEqual([]); + capture.clear(); +}); - test("marks a source-drifted observation ineligible", async () => { - const coordinator = new ValidationCaptureCoordinator({ - randomId: () => "capture-drifted", - persistObservation: (_workspace, input) => - Promise.resolve({ - ...persistedObservation(input), - ineligibleReason: "source-drift", - }), +test.each([ + ["one final LF omitted from the native display", "\n", true], + ["no final LF", "", true], + ["extra final LF", "\n\n", false], + ["changed trailing whitespace", " \n", false], + ["changed output suffix", "different\n", false], +] as const)( + "managed output matching preserves exact bytes with %s", + async (_name, ending, expectedComplete) => { + const rendered = "native-smoke-output\n".repeat(2000).slice(0, -1); + const full = `${"native-smoke-output\n".repeat(18000)}${rendered}${ending}`; + const path = join(workspace, "sh_native.out"); + let now = 100; + const { capture, observations } = fixture({ + now: () => now, + readFullOutput: async () => full, }); - coordinator.arm("session-drifted", "/workspace", prepared); - coordinator.observeToolBefore( - { tool: "bash", sessionID: "session-drifted", callID: "bash-drifted" }, - { args: { command: prepared.command } }, - ); - const output = { - title: "Drifted", - output: "ok", - metadata: { exit: 0, truncated: false }, - }; - await coordinator.observeToolAfter( - { - tool: "bash", - sessionID: "session-drifted", - callID: "bash-drifted", - args: { command: prepared.command }, - }, + capture.arm("ses_host", workspace, prepared); + await capture.observeToolBefore(before()); + now = 200; + capture.observeShellEvent(shellEvent(path)); + capture.observeShellEvent(exited()); + const output = `${rendered}\n\n[showing lines 18001-20000 of 20000; full output saved to ${path}]`; + const completed = after({ + exit: 0, + truncated: true, + status: "completed", output, - ); - expect(output.output).toContain( - '"passed":false,"observed":false,"recordedRevision":4,"ineligibleReason":"source-drift"', - ); - }); -}); + }); + if (completed.status === "completed") + completed.result = { + ...completed.result, + content: [{ type: "text", text: output }], + }; + await capture.observeToolAfter(completed); + expect(observations[0]).toMatchObject({ + exitCode: 0, + outputComplete: expectedComplete, + outputDigest: digest(expectedComplete ? full : output), + }); + }, +); diff --git a/tests/workspace-lifecycle-integration.test.ts b/tests/workspace-lifecycle-integration.test.ts index 7094fc66..3718f3b0 100644 --- a/tests/workspace-lifecycle-integration.test.ts +++ b/tests/workspace-lifecycle-integration.test.ts @@ -4,8 +4,7 @@ import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { promisify } from "node:util"; -import type { ToolContext } from "@opencode-ai/plugin"; -import type { FlowService } from "../src/application/flow-service.js"; +import { z } from "zod"; import type { SourceDigest } from "../src/domain/session.js"; import { loadArchivedSession, @@ -20,14 +19,32 @@ import { persistWorkspaceValidation, prepareWorkspaceValidation, } from "../src/infrastructure/fs/workspace-validation.js"; +import type { FlowToolContext } from "../src/platform/opencode/flow-tool.js"; import { createTools } from "../src/platform/opencode/tools.js"; +import { ValidationCaptureCoordinator } from "../src/platform/opencode/validation-capture.js"; const execFileAsync = promisify(execFile); const FEATURE_ID = "lifecycle"; -const OUTPUT_DIGEST = `sha256:${"b".repeat(64)}` as SourceDigest; -type FeatureCompleteResponse = Awaited< - ReturnType ->; +const OUTPUT_DIGEST: SourceDigest = `sha256:${"b".repeat(64)}`; +const featureCompleteResponseSchema = z.discriminatedUnion("status", [ + z.object({ + status: z.literal("ok"), + summary: z.string(), + workflowData: z.object({ + projection: z.object({ status: z.string(), revision: z.number() }), + operation: z.object({ + operationId: z.string(), + revision: z.number(), + replayed: z.boolean(), + }), + }), + }), + z.object({ + status: z.literal("error"), + summary: z.string(), + workflowData: z.object({}).passthrough(), + }), +]); function assertOk< T extends Readonly<{ status: "ok" | "error"; summary: string }>, @@ -43,20 +60,18 @@ function ok>( return response; } -function parseFeatureCompleteResponse(value: unknown): FeatureCompleteResponse { - return JSON.parse(String(value)) as FeatureCompleteResponse; +function parseFeatureCompleteResponse(value: unknown) { + return featureCompleteResponseSchema.parse(value); } -function toolContext(workspace: string, agent: string): ToolContext { +function toolContext(workspace: string, agent: string): FlowToolContext { return { sessionID: `lifecycle-${agent}`, messageID: `message-${agent}`, + callID: `call-${agent}`, agent, directory: workspace, - worktree: workspace, abort: new AbortController().signal, - metadata() {}, - async ask() {}, }; } @@ -192,13 +207,19 @@ test("persists one complete workspace lifecycle and replays its exact close", as }, }; let completionCancellations = 0; + const validationCapture = new ValidationCaptureCoordinator({ + readSession: async () => undefined, + persistObservation: async () => { + throw new Error("Unused validation persistence."); + }, + }); + const cancel = validationCapture.cancel.bind(validationCapture); + validationCapture.cancel = (sessionID) => { + completionCancellations++; + return cancel(sessionID); + }; const completionTool = createTools({ - validation: { - cancel() { - completionCancellations += 1; - return true; - }, - } as never, + validation: validationCapture, prepareValidation: async () => { throw new Error("Validation preparation is not used here."); }, From 25a837816299f23f21ed12bc0cffdf0970a2d803 Mon Sep 17 00:00:00 2001 From: Douwe de Vries Date: Sat, 24 Oct 2026 19:09:27 +0200 Subject: [PATCH 02/17] fix(opencode): preserve logical completion and native failure evidence A completed Responses stream can stop its HTTP reader before EOF. End that captured resource without revoking its admitted input. Retain bounded native terminal facts in V2 evaluation traces and keep question escalation scoped to its invocation. Port direct transport fixtures to the pre-dispatch transcript read. --- .agents/plans/18-opencode-v2/README.md | 16 +- .agents/plans/18-opencode-v2/execution.md | 190 +++ .../implementation-checkpoint.json | 143 ++- .../plans/18-opencode-v2/stream-lifetime.md | 116 ++ .../plans/18-opencode-v2/terminal-evidence.md | 68 + docs/adr/0018-opencode-v2-only.md | 27 +- docs/maintainer-contract.md | 9 +- evals/benchmark-run.ts | 2 +- evals/failure-origin.ts | 38 + evals/harness.ts | 619 ++++++--- evals/host-trace.ts | 314 ++++- evals/opencode-host.ts | 15 +- evals/replay.ts | 1 + evals/reviewer-run.ts | 6 +- evals/run.ts | 20 +- evals/study-runner.ts | 2 +- .../opencode/execution-attribution.ts | 2 +- tests/eval-cancellation.test.ts | 5 + tests/eval-native-terminal.test.ts | 1133 +++++++++++++++++ tests/eval-progress-wait.test.ts | 2 +- tests/eval-reporting.test.ts | 11 +- tests/eval-scenario-checks.test.ts | 1 + tests/eval-v2-trace-contract.test.ts | 6 + tests/execution-attribution.test.ts | 243 +++- tests/fixtures/eval-abort-fallback-child.ts | 1 + tests/fixtures/eval-cancellation-child.ts | 21 +- .../eval-reserve-cancellation-child.ts | 1 + tests/fixtures/native-terminal-contexts.json | 151 +++ tests/lifecycle.test.ts | 93 +- tests/provenance.test.ts | 6 + tests/qualification-cli.test.ts | 2 + tests/study-support.ts | 2 + 32 files changed, 2985 insertions(+), 281 deletions(-) create mode 100644 .agents/plans/18-opencode-v2/stream-lifetime.md create mode 100644 .agents/plans/18-opencode-v2/terminal-evidence.md create mode 100644 tests/eval-native-terminal.test.ts create mode 100644 tests/fixtures/native-terminal-contexts.json diff --git a/.agents/plans/18-opencode-v2/README.md b/.agents/plans/18-opencode-v2/README.md index 1dd01229..d1fb62cc 100644 --- a/.agents/plans/18-opencode-v2/README.md +++ b/.agents/plans/18-opencode-v2/README.md @@ -6,14 +6,20 @@ Update the README, maintained documentation, and public wiki. Publish through the existing qualified release workflow. Implementation started on 2026-10-24 on `feat/opencode-v2`. -The R8 candidate passes full checks and unchanged packed smoke on macOS and -Linux. Its continuation, validation, reviewer return, recovery harness, and picker -pass exact-package loopback checks. Fresh independent core review passed. -Account-helper cleanup and configurable Typesafe key selection passed their -bounded reviews. Paid qualification and publication have not started. +The committed R8 candidate passed local and packed checks, but its approved +real-account smoke failed after one completed status call without saving a plan. +Both authorized dispatches are consumed. Offline investigation confirmed lost +native terminal evidence and a Responses logical-completion cancellation case. +Evaluator diagnostics and the core request-lifetime correction are implemented. +The new exact package passes macOS/Linux packed smoke, recovery, picker, and +unwrapped Responses compatibility. Four test fixtures now model the pre-dispatch +read, and final macOS and Linux checks each pass 4,744 tests. Evaluator R2 and +core R9 reviews passed. Commit and pre-push gates remain. +The actual paid cause remains unresolved. No push or release occurred. Current execution evidence is recorded in [execution.md](execution.md). Research is recorded in [research.md](research.md). Decisions are recorded in [decisions.tsv](decisions.tsv). +The stream correction is recorded in [stream lifetime](stream-lifetime.md). ## Read and execute the plan diff --git a/.agents/plans/18-opencode-v2/execution.md b/.agents/plans/18-opencode-v2/execution.md index 6a694a84..cc4436e2 100644 --- a/.agents/plans/18-opencode-v2/execution.md +++ b/.agents/plans/18-opencode-v2/execution.md @@ -132,3 +132,193 @@ tests and no actionable source findings. The other review verdicts remain scoped and unread original-control correlation remain unproved. The trailing-history and pre-dispatch concurrency neighbors are component evidence. No real account, paid provider call, Git publication, or release is covered by these results. + + +## Actual-account smoke and observation gap + +The implementation was committed as +`d233a0cca136d35f84c57b77d0e22f1583e654e0`. Commit and push preflight passed. +The user approved two top-level dispatches on openai/gpt-6.1-sol, one route probe +and one plan-only-stops attempt, including normal native OAuth refresh. The +existing-account relay and route probe passed. The scenario failed after one +completed flow_status with no plan or session document. Both claims remain +consumed. The relay exited cleanly and removed its private connection file. +No new ledger, account use, or paid rerun is authorized by this result. + +The private receipt is `/tmp/flow-v2-prepush-20261024/result.json`. +The retained case replays its failure without divergence. It does not reveal the +paid native idle outcome, assistant finish, or transport close/cancel sequence. +The report's quiet end is not proof of native success. Offline native fixtures +show the evaluator can return quiet for a failed idle without assistant error. + +Two diagnostic designs and a fresh cross-judge selected the existing V2 trace +as the owner of optional terminal facts. The correction preserves V1 schemas and +historical records. It keeps provider errors separate from non-retryable native +host failure. It changes neither plugin cancellation nor product grading on +unobserved terminal evidence. See [terminal evidence](terminal-evidence.md). + +A separate bounded Responses fixture reproduces the completed-tool stopping +shape when protocol completion arrives before physical EOF. The actual native +Responses parser uses Stream.takeUntil on response.completed and cancels the +remaining body. Flow currently treats that consumer cancellation as failure. +The source guard then blocks the automatic next request. The normal-close +control reaches a second request. This is a demonstrated compatibility mechanism, +not proof of the old paid cause. Its receipt is +`/tmp/flow-v2-paid-smoke-investigation/responses-addendum/report.md`. +The final control reproduced the failure without any observer HTTP/body hooks. +Exactly one host and two cases ran; all owned processes were stopped and awaited. +Client cancellation remains unobserved in that control. Its report is +`/tmp/flow-v2-paid-smoke-investigation/responses-addendum/unwrapped-confirmation/report.md`. +Core correction designs must preserve true interruption, error, stale +ownership, pending admission, and retry guards. No epoch may be resurrected. + +The investigator accidentally imported a helper with an argv initialization +side effect. It rewrote initial-binding.json before host launch. The investigator +reconstructed the receipt from preserved ordered content, but no independent +preincident checksum exists. That receipt was not immutable throughout the turn. +The original report, paid inputs, archive, and mapped source hashes have separate +unchanged bindings. Preserve the incident disclosure; do not reconstruct again +or use the reconstructed receipt as independent before-image proof. + +## Terminal evidence and stream contract checkpoint + +The evaluator correction is frozen across 19 source and test files. Its owner +ran 293 focused tests, typecheck, and lint. The parent verified the freeze and +independently ran 74 focused tests with 467 assertions and no failures. Fresh +independent correctness and comment reviews are in progress. Historical paid +records, V1 evidence, product source, and the fc19 archive remain unchanged. + +The core design must distinguish HTTP reader lifetime from input authority and +native tool settlement. Static source embedded in the pinned 2.1.4 executable +publishes assistant finish and completed time after waiting for tool fibers. +Requiring those fields before admitting the first tool would therefore create +a circular dependency. Streamed time alone is not clean-success evidence. +The offset-bound source receipts are in +`/tmp/flow-v2-stream-completion-design/native-source-contract/`. + +The first bounded pre-tool prototype stopped before dispatch because its live +plugin list was empty. It measured no assistant fields and establishes no native +contract failure. One corrected attempt uses explicit location selection and +observed plugin readiness before its two loopback cases. No account or paid +provider calls are authorized. The core design cross-judge remains in progress; +no core implementation has been selected or changed at this checkpoint. + +The stream-completion cross-judge rejected both proposed completion barriers. +They require assistant fields that native publishes after its tool-fiber join. +The request-lifetime alternative remains a question pending the native negative +cases. It retains the response proxy and immediate read/status failures while +separating consumer cancellation from input-epoch revocation. No implementation +authorization follows from that judgment. + +The installed Promise plugin adapter passes the signal supplied by +Effect.tryPromise directly into ToolContext. Effect creates that signal's abort +controller and aborts it when the waiting effect is interrupted. These exact +package-source bindings are recorded in `native-source-contract/tool-signal-receipt.json` +under the disposable design directory. They establish the adapter mechanism; +they do not claim a live held-tool interruption measurement. + +The corrected ordering prototype passed readiness and exact hook ordering on one +host and two cases. Both held native calls gained streamed time after the server +terminal. Finish, error, and completed time remained absent before release for +both completed and failed Responses markers. The unchanged archive refused both +calls after release. This confirms the completion barrier problem but does not +measure the actual Flow callback's signal. The report is +`/tmp/flow-v2-paid-smoke-investigation/responses-addendum/pre-tool-contract-corrected/report.md`. + +One disposable request-lifetime prototype is now authorized locally. It copies +the archive entry and changes only its captured cancel callback plus a bounded +measurement barrier inside the actual tool callback. Repository product source +and archive bytes remain frozen. The prototype tests completed and failed +Responses markers, a public user interrupt before mutation, and a separately +accepted fresh input. Its modified entry is not an exact-package qualification. +No account, paid provider, or new spending ledger is involved. + +Independent evaluator review R1 reproduced one precedence regression. A closing +failed idle replaced an otherwise deliberate unanswered-question escalation, and +outcome collection then retained it as the attempt failure. The parent reproduced +the same result with an output-only copy of the review fixture. The original +review receipts remain unchanged. A bounded fix must keep the native idle on the +trace while preserving the question stop for that invocation. It must reset at +the next command and leave native failure primary at ordinary stall, deadline, +child-read, and cancellation boundaries. The report is +`/tmp/flow-v2-terminal-evidence-review-r1/report.md`. + +The existing evaluator owner is implementing that fix and the seven accepted +comment deletions in its two-file fence. The original 19-file freeze remains an +immutable record; the correction will have a separate freeze and a fresh R2 +review. Product execution source remains untouched by this evaluator work. + +The disposable lifetime prototype completed one host and four synthetic +dispatches. Completed/open allowed the actual tool and automatic next request. +Failed/open left the already-issued read-only tool signal un-aborted; that tool +completed and native idle then failed. Public interruption aborted the actual +plan-save callback signal and persisted no mutation. A fresh ordinary input in +the same session saved revision 1 with only its own operation. The parent checked +the exact two replacement spans and reversal and reread the persisted document. +The report is `/tmp/flow-v2-stream-lifetime-prototype/report.md`. + +The selected synthesis is +`/tmp/flow-v2-stream-completion-design/synthesis/decision.md`. Consumer cancel +ends only the captured HTTP resource, as EOF already does. Exact admitted-input +and native tool proof stay required, and definite read/status failures retain +their owned revocation. Whole-assistant confirmation flights and new state are +rejected. The existing core owner may change attribution and its minimal tests; +lifecycle policy changes require a concrete missing-boundary reproduction first. +The provider-failure cell is a recorded native boundary limit, not rollback or +failed-provider mutation proof. No production package or paid-cause claim is +derived from this prototype. + +The current writers have separate file fences. The evaluator owner changes its +wait and terminal tests. The core owner changes request attribution and its +focused tests. Prior artifacts stay immutable. The parent owns integration and +the next package. This keeps diagnostic precedence separate from execution policy. + +## Combined candidate after the stream correction + +Both implementations are frozen. The core production diff is one cancel callback +calling finish instead of fail. Lifecycle source is unchanged. The evaluator +retains native terminal facts and preserves a selected question escalation for +that invocation. A fresh command or prompt gets its own observation. + +The new archive is `e8d85c5339d1aa058493088a61a6410b5785a6462f65aed09b73de01ef6dd7d9`. +Its unpacked manifest is +`ce2b3bf96a6cf3e57c987df65d8da3ae97abbcd4b20294266841b6c3d2088985`. +Candidate and map bindings are in `/tmp/flow-v2-candidate-stream-lifetime/`. +Linux reproduced the exact raw archive. All 12 installed files match. + +The unchanged packed smoke passes all 119 assertions on macOS and Linux. +Normal recovery passes three tests on four native hosts with zero claims. +Both picker routes pass eight phases. The continuation lane passes 16 profiles +and the exact public-interrupt/manual-summary/fresh-input sequence. The unwrapped +Responses gate passes normal-close and completed/open cases, each with a real +tool result, automatic second request, and succeeded idle. These are synthetic +checks. They neither qualify a paid provider nor identify the original paid cause. + +The first complete checks failed four fixture assumptions about the added +pre-dispatch metadata/transcript read. The parent prepared and verified four +test-only ports outside the repository, then applied them after every package +QA lane closed. The original failing logs and freezes remain. The ports preserve +the single actual progress-poll assertion and separately assert the command +boundary read. All 153 affected tests pass. Production and archive bytes did not +change. Final combined hashes are in `combined-freeze-final.json`. + +Final canonical checks each pass 4,744 tests, with 27 skips and zero failures. +The macOS and Linux assertion counts are 99,135 and 99,132. The logs are +`/tmp/flow-v2-stream-lifetime-check-macos-final.log` and +`/tmp/flow-v2-linux/stream-lifetime/full-check-final.log`. Replay matches all +13 gated cassettes. Dependency audit and actionlint pass. Documentation remains +within both existing byte budgets. Two comment reviews have no open findings. + +Independent evaluator R2 passed and closed F1 after checking the expected +fixture-only supplement. It ran 301 focused tests and independently checked the +27 final hashes. Its report is `/tmp/flow-v2-terminal-evidence-review-r2/report.md`. +Core R9 passed after inspecting the exact archive and running 525 focused tests +with 3,061 assertions and no failures. Its report is +`/tmp/flow-v2-core-review-r9/report.md`. The parent read both full reports and +verified the canonical results separately. Native overlap, auxiliary retry, and +unread-control correlation limits remain. Recorded terminal-cell replay passed +on the new capture; this does not claim live terminal font or pixel verification. + +The pre-push smoke proposal is a draft only. No new authorization or ledger has +been created. The original two claims remain consumed and their failed report +stays unchanged. Commit and push preflight remain before a new paid proposal. diff --git a/.agents/plans/18-opencode-v2/implementation-checkpoint.json b/.agents/plans/18-opencode-v2/implementation-checkpoint.json index 3417f67c..e4519b9e 100644 --- a/.agents/plans/18-opencode-v2/implementation-checkpoint.json +++ b/.agents/plans/18-opencode-v2/implementation-checkpoint.json @@ -4,22 +4,24 @@ "bun": "/tmp/flow-v2-toolchain/node_modules/.bin/bun", "nativeHost": "/tmp/flow-opencode-v2-plan-host/npm-cache/_npx/2314e955696d2fea/node_modules/@opencode/cli-darwin-arm64/bin/opencode", "candidate": { - "path": "/tmp/flow-v2-candidate-r8/opencode-plugin-flow-10.0.0.tgz", - "sha256": "fc19ca4a55a8f1684fe9e1c988adce2536a667a339a3a0e622d0c2e39518195d", - "unpackedManifestSha256": "667d6029038248b4222e28c8e5f7656bf1534286441335925faa4c2d20f3c95a", - "status": "Synthetic packed QA and fresh independent R8 source review PASS. Candidate is ready for paid pre-push smoke authorization; no release qualification or publication approval.", - "sourceBinding": "/tmp/flow-v2-candidate-r8/review-source-binding.json" + "path": "/tmp/flow-v2-candidate-stream-lifetime/opencode-plugin-flow-10.0.0.tgz", + "sha256": "e8d85c5339d1aa058493088a61a6410b5785a6462f65aed09b73de01ef6dd7d9", + "unpackedManifestSha256": "ce2b3bf96a6cf3e57c987df65d8da3ae97abbcd4b20294266841b6c3d2088985", + "status": "PENDING commit/push preflight and a new explicit paid-smoke authorization. Core R9/evaluator R2 passed. Final macOS/Linux4744 checks, exact packed119 both platforms, recovery, continuation, picker and unwrapped Responses compatibility pass. No publication.", + "sourceBinding": "/tmp/flow-v2-candidate-stream-lifetime/source-binding.json" }, "verification": { - "check": "/tmp/flow-v2-r7-check-macos.log", + "check": "/tmp/flow-v2-stream-lifetime-check-macos-final.log", "tests": { - "pass": 4679, + "pass": 4744, "skip": 27, - "fail": 0 + "fail": 0, + "assertions": 99135, + "files": 185 }, - "packed": "/tmp/flow-v2-product-r7-qa-47fb9cwz/verification.json", - "continuation": "/tmp/flow-v2-packed-r7-continuation-qa-uljdjwv7/runs/run-20261024-143740-052985/verification.json", - "picker": "/tmp/flow-v2-picker-implementation/packed-r7-74da6ab8/verification.json", + "packed": "/tmp/flow-v2-stream-lifetime-packed-eval/verification.json", + "continuation": "/tmp/flow-v2-stream-lifetime-packed-continuation/verification.json", + "picker": "/tmp/flow-v2-picker-implementation/packed-stream-lifetime-e8d85c53/verification.json", "canaryFixes": "/tmp/flow-v2-canary-implementation/review-r1-fixes/report.json", "postCheckTestOnlyCorrection": { "files": [ @@ -31,13 +33,13 @@ "native": "/tmp/flow-v2-native-auth-portable-native.log", "linuxFocused": "/tmp/flow-v2-linux/portability-recheck.log" }, - "replay": "/tmp/flow-v2-final-replay.log", - "audit": "/tmp/flow-v2-final-audit.log", - "actionlint": "/tmp/flow-v2-final-actionlint.log", + "replay": "/tmp/flow-v2-stream-lifetime-replay.log", + "audit": "/tmp/flow-v2-stream-lifetime-audit.log", + "actionlint": "/tmp/flow-v2-stream-lifetime-actionlint.log", "frozenInstall": "/tmp/flow-v2-final-frozen-install-macos.log", - "documentation": "/tmp/flow-v2-final-documentation.log", + "documentation": "/tmp/flow-v2-stream-lifetime-documentation-final.log", "commentInventory": "/tmp/flow-v2-comment-review-r7-final/manifest.json", - "recovery": "/tmp/flow-v2-product-r7-qa-47fb9cwz/recovery/report.json", + "recovery": "/tmp/flow-v2-stream-lifetime-packed-eval/recovery/report.json", "nativePreContextCompactionCounterexample": "/tmp/flow-v2-final-continuation-qa-zc8ncill/native-stale-callback-verification.json", "nativeTypesafeOption": "Three actual-native option cases pass on d3d46d92 with zero advice attempts; general candidate remains blocked.", "nativeAuthParentR3Focused": { @@ -204,7 +206,69 @@ "files": 17, "freshNativeRuns": 0, "sourceDrift": false - } + }, + "actualAccountPrePush": { + "status": "FAIL", + "probe": "PASS", + "scenario": "plan-only-stops", + "model": "openai/gpt-6.1-sol", + "consumedDispatches": 2, + "remainingDispatches": 0, + "receipt": "/tmp/flow-v2-prepush-20261024/result.json", + "sourceCleanup": "PASS, source exit0 and private connection removed", + "paidCause": "UNRESOLVED" + }, + "continuationStatus": "PASS for exact e8d archive, bounded16profiles plus interrupted ordinary/manual sequence.", + "commentReview": "/tmp/flow-v2-stream-lifetime-final-comment-review/report.md", + "responsesCompatibility": "/tmp/flow-v2-stream-lifetime-packed-responses/report.md", + "streamLifetimeChecks": { + "macOS": { + "log": "/tmp/flow-v2-stream-lifetime-check-macos-final.log", + "pass": 4744, + "skip": 27, + "fail": 0, + "assertions": 99135 + }, + "linux": { + "log": "/tmp/flow-v2-linux/stream-lifetime/full-check-final.log", + "pass": 4744, + "skip": 27, + "fail": 0, + "assertions": 99132 + }, + "fixturePort": "/tmp/flow-v2-test-boundary-fixture-port/prepared-verification.json", + "fixturePortStatus": "APPLIED. Four test-only fixture ports pass153 affected tests; final canonical checks pass on macOS and Linux. No production or archive byte changes.", + "linuxPackedSmoke": { + "log": "/tmp/flow-v2-linux/stream-lifetime/packed-smoke.log", + "pass": 2, + "fail": 0, + "assertions": 119 + }, + "linuxArchiveReproducibility": "/tmp/flow-v2-linux/stream-lifetime/repack-verification.json", + "coreFreeze": "/tmp/flow-v2-stream-lifetime-core-implementation/freeze.json", + "evaluatorFreeze": "/tmp/flow-v2-terminal-evidence-implementation/review-r1-fixes/freeze.json", + "coreReview": "/tmp/flow-v2-core-review-r9/report.md", + "evaluatorReview": "/tmp/flow-v2-terminal-evidence-review-r2/report.md", + "initialMacOS": { + "log": "/tmp/flow-v2-stream-lifetime-check-macos.log", + "pass": 4740, + "skip": 27, + "fail": 4, + "assertions": 99113 + }, + "initialLinux": { + "log": "/tmp/flow-v2-linux/stream-lifetime/full-check.log", + "pass": 4740, + "skip": 27, + "fail": 4, + "assertions": 99110 + }, + "fixturePortFinalFreeze": "/tmp/flow-v2-candidate-stream-lifetime/combined-freeze-final.json", + "evaluatorReviewStatus": "PASS, F1 closed, final27 hashes checked, expected test-only child drift disclosed.", + "coreReviewStatus": "PASS, bounded source/archive review, 525 focused tests, 3061 assertions. Canonical results verified separately by parent.", + "recordedTerminalVisual": "/tmp/flow-v2-terminal-render/stream-lifetime-e8d85c53/responsive/visual-inspection.json" + }, + "rootFixtureCommentReview": "/tmp/flow-v2-root-fixture-comment-review/report.md" }, "pending": { "coreR3Report": "/tmp/flow-v2-core-review-r3/report.md", @@ -307,19 +371,38 @@ "sourceFreeze": "/tmp/flow-v2-compaction-r8-bnmny7om/freeze-final.json", "parentReviewFreeze": "/tmp/flow-v2-candidate-r8/review-freeze.json" }, - "coreR8Review": "/tmp/flow-v2-core-review-r8/report.md, bounded PASS" + "coreR8Review": "/tmp/flow-v2-core-review-r8/report.md, bounded PASS", + "terminalEvidence": { + "decision": ".agents/plans/18-opencode-v2/terminal-evidence.md", + "implementation": "/tmp/flow-v2-terminal-evidence-implementation", + "status": "Evaluator-only implementation in progress" + }, + "streamCompletion": { + "design": "/tmp/flow-v2-stream-completion-design", + "proof": "/tmp/flow-v2-paid-smoke-investigation/responses-addendum/report.md", + "status": "Two core designs in progress; unwrapped control confirms the matching stopping mechanism persists.", + "unwrappedControl": { + "status": "Failure reproduced without independent HTTP/body observer", + "report": "/tmp/flow-v2-paid-smoke-investigation/responses-addendum/unwrapped-confirmation/report.md", + "hostCount": 1, + "cases": 2, + "ownedProcessesStopped": true, + "clientCancellation": "unobserved", + "paidCause": "UNRESOLVED" + } + } }, "next": [ - "Finish staged-index review and commit preflight, then commit the reviewed migration.", - "Complete repository contribution gates and commit the reviewed migration.", - "Obtain explicit paid pre-push smoke authorization before account use and push; then Linux CI and separate full qualification approval.", - "Publish through the usual exact-artifact release gate, then wiki publication and public readback." + "Finish evaluator-only terminal observation correction and its focused review.", + "Choose and prove the Responses logical-completion compatibility correction without weakening true interruption or stale-owner guards.", + "Run canonical and exact-package gates on the combined frozen changes before proposing another paid diagnostic.", + "Push only after required paid smoke succeeds; release qualification, publication, and wiki public readback remain separate gates." ], "authority": { - "mainRepositoryCommits": 0, + "mainRepositoryCommits": 1, "pushes": 0, "pullRequests": 0, - "paidProviderCalls": 0, + "paidProviderCalls": 2, "tags": 0, "published": false, "localOpenAiAndXaiApiKeysPresent": false, @@ -340,7 +423,8 @@ "local": true, "authenticatedNativeCliInfo": "PASS", "originalRefreshValuesRead": false - } + }, + "paidProviderCallsSemantics": "Two approved top-level dispatches: route probe and plan-only attempt; internal generation is separate." }, "notes": [ "Do not reuse a completed review thread for another review round. Use delegate_task with a fresh clientRequestId.", @@ -393,5 +477,14 @@ "unpackedManifestSha256": "29a1d475fae346ee7fd52a62ac079ca2af5b0f0c36ee05bb946d2f9fc8aba0d8", "status": "REJECTED. Retained exact archive has the native pre-primary compaction defect. Current source has additional ordinary automatic/manual compaction corrections; fresh R7 and full checks are running. No replacement archive exists." } + ], + "supersededCandidates": [ + { + "path": "/tmp/flow-v2-candidate-r8/opencode-plugin-flow-10.0.0.tgz", + "sha256": "fc19ca4a55a8f1684fe9e1c988adce2536a667a339a3a0e622d0c2e39518195d", + "unpackedManifestSha256": "667d6029038248b4222e28c8e5f7656bf1534286441335925faa4c2d20f3c95a", + "status": "HOLD. Actual-account pre-push planning smoke failed. Responses logical-completion cancellation mechanism demonstrated; diagnostics implementation and core design in progress. Actual paid cause unresolved.", + "sourceBinding": "/tmp/flow-v2-candidate-r8/review-source-binding.json" + } ] } diff --git a/.agents/plans/18-opencode-v2/stream-lifetime.md b/.agents/plans/18-opencode-v2/stream-lifetime.md new file mode 100644 index 00000000..c671bac4 --- /dev/null +++ b/.agents/plans/18-opencode-v2/stream-lifetime.md @@ -0,0 +1,116 @@ +# Captured HTTP lifetime synthesis + +Select the tested request-lifetime shape. Candidates A and B are rejected. +The parent read both full candidates, the cross-judge, native binary and adapter +source, the corrected held-tool probe, and the full third-shape prototype. +No whole-assistant completion barrier or pending-confirmation flight survives. + +## Callers and data shape + +An existing primary HTTP response still passes to ExecutionAttribution.response. +Its exact Request captures one FrozenPrimary. Natural EOF and consumer cancel +both end that captured resource. Neither declares model success nor writes the +input epoch. A real read failure, non-OK response, missing/locked body, or Request +mismatch retains its current captured failure callback. Cancellation cleanup is +awaited and its rejection propagates; an old callback never substitutes the +current primary. + +FrozenPrimary keeps its existing context/request/streaming/ended phase. Ended is +a resource state, not a clean assistant or native execution result. No new state, +registry, parser, package, watcher, signal thread, or public method is needed. +The code sketch is async cancel(reason) { finish(); await reader.cancel(reason); }. + +The tool caller still proves the exact live admitted origin, Request, native +assistant, frozen prefix, model, workspace, running call and immutable binding. +The actual native ToolContext.signal remains checked before tool execution and +again through the guarded mutation context. Native user interruption aborts that +signal. Real steer, event-stream loss, unload and observed owned failure retain +their existing epoch revocation. No revoked origin is revived by resource end. + +The native context caller keeps readiness and exact-source admission. Automatic +idle dispatch still requires the owned native session's succeeded outcome. +Compaction keeps its exact control, reason, full prefix, delivery and ledger +source/history proof. No completion field can create InputOrigin or a checkpoint. + +## Why this shape + +The shared false premise was that body cancellation proves user stop or native +execution failure. The installed Responses takeUntil cancels on completed and +failed terminals. Whole-assistant finish/completed is published after tool fibers +exit, so using it to admit the first tool creates a cycle. Streamed is present in +both completed and failed held cases and cannot prove success. + +The disposable prototype changed only this cancel callback and inserted a +measurement-only barrier before the unchanged tool signal check. A completed +open body allowed the actual status tool and automatic second request. A public +interrupt aborted the actual callback signal, caused no persisted mutation and +no follow-up. A fresh ordinary input then saved only its own revision-1 operation. +That is prototype evidence, not exact-package qualification or the paid cause. + +A non-interrupt provider failure did not abort the already-issued read-only tool. +It completed before native idle failed. Preserve this boundary explicitly. +A protocol failure is not a user interrupt, and tools may execute before the model +stream ends even with natural EOF. Do not invent rollback or a whole-step-success +permission barrier. An observed native failure must not be reported as success. +The separate evaluator correction retains that idle and reports its failure. +No failed-provider mutation proof is claimed by this design. + +## Actor census and required gates + +- Natural EOF. Existing resource end and exact tool guards stay unchanged. +- Logical protocol completion before EOF. Consumer end keeps the live input; + first unbound tool and automatic second request must work. +- Consumer cancel without an observed native failure. Ends only the resource; + it is not positive input authority or evidence of native success. +- Read rejection, non-OK HTTP, missing/locked body and Request mismatch. Immediate + captured-owner failure remains. Original HTTP400 diagnostics remain intact. +- True public interrupt. Actual signal or fiber interruption must prevent mutation; + old callbacks cannot affect a separately accepted fresh input. +- Steer and new private admission. Existing epoch/frontier ownership stays exact. +- Old EOF, cancel or delayed cancel rejection. Touches only the captured primary; + fresh authority and protected tools remain usable. +- Pending admission. Await readiness without borrowing a live host lease. +- Normal tool loops and reviewer return. Preserve exact native caller identities. +- Automatic, ordinary and manual compaction. Re-run the original interrupted + ordinary manual-summary sequence without reload or a fabricated user. +- Same-origin request retry. Existing request-phase retry facts and object identity + remain authoritative; resource end grants no retry. +- Auxiliary HTTP kinds. They cannot terminate or prove a primary. +- Owned failed/interrupted native idle. Existing negative observation and succeeded + idle admission stay in force. Test a visible matching terminal and fresh-source + neighbor; do not add speculative guards without a reproduced missing boundary. +- Unload and event-stream loss. Clear and refuse as before. + +Rewrite tests that label raw body.cancel with a reason string as user intent. +They must distinguish consumer-resource end from an explicit steer/native +interrupted terminal. Preserve old-effect refusal and fresh-input assertions. +Do not delete failing tests or replace their effect assertions with callback counts. + +## Scope and verification + +Implement in execution-attribution.ts and its focused tests. Lifecycle source is +read-only unless a concrete census failure proves a required boundary; surface +that exact evidence before changing its policy. Minimum lifecycle test migration +is allowed. InputAdmissions, CompactionBoundary, AutoDrive, recovery policy, +domain persistence, tools, package/auth/eval sources remain outside this fence. + +The parent owns one new archive, unchanged 119-assertion smoke on macOS/Linux, +exact packed continuation/compaction/stop/retry proof, picker checks and a fresh +independent core review. Types, lint, architecture and focused tests precede the +freeze. Real-account smoke needs a new concrete paid authorization. The old two +claims stay consumed, its report stays FAIL, and its cause stays unresolved. + +## Artifact basis + +- /tmp/flow-v2-stream-completion-design/crossjudge/report.md +- /tmp/flow-v2-stream-completion-design/native-source-contract/ +- /tmp/flow-v2-paid-smoke-investigation/responses-addendum/pre-tool-contract-corrected/report.md +- /tmp/flow-v2-stream-lifetime-prototype/report.md +- /tmp/flow-v2-stream-lifetime-prototype/patch-receipt.json +- /tmp/flow-v2-stream-lifetime-prototype/receipt.json + +Attack the Premise separated consumer resource end from user intent. Model the +Domain kept resource phase distinct from admission and terminal evidence. +Boundary Discipline retained exact native tool and interrupt checks. Laziness +Protocol deleted confirmation flights and new state. Prove It Works requires the +new archive and actual effect checks before any release claim. diff --git a/.agents/plans/18-opencode-v2/terminal-evidence.md b/.agents/plans/18-opencode-v2/terminal-evidence.md new file mode 100644 index 00000000..bcd52df2 --- /dev/null +++ b/.agents/plans/18-opencode-v2/terminal-evidence.md @@ -0,0 +1,68 @@ +# Evaluator terminal evidence synthesis + +Choose candidate A as the existing trace-owner base. Parent and independent +Grok cross-judge score A11/B7. Both full candidates and sketches were read. +The actual paid cause remains unresolved. This change fixes a demonstrated +observation gap and does not alter plugin execution policy. + +The existing V2 trace owns optional terminal facts on assistant and idle messages. +Old V2 absence remains unobserved; V1 and both tool provenance schemas stay exact. +Assistant finish, error, streamed, and completed are independent native facts. +No raw finish, retry descriptor, compaction terminal extension, response body, +headers, account/config data, extra telemetry store, or new trace version. +One collector sanitizer and pure native-execution reader serve live wait, +outcome collection, and offline interpretation. No raw-native decision adapter +with a second parsing policy. + +Capture the last native message id immediately before each command/prompt POST, +after model switching. Command scope and abort attribution are invocation-local. +Only the command root's closing idle after that boundary may create this new +host failure. Owned child ends remain evidence and keep existing product grading. +A later succeeded idle supersedes an interim failed one while execution proceeds. +Missing idle/end fields stay unobserved and do not change existing quiet policy. +Failed and non-self interrupted idle produce host-only, non-retryable failures. +They cannot become provider-rejected-turn, retryable command-aborted, or reserve +eligibility. Keep providerError provider-only. Add a native failure result and +migrate the four runner precedence sites plus direct fixtures. No compatibility +alias or field rename. Product missing-plan and false-completion checks remain. + +Keep POST, activity, busy, wedge, deadline, question, and cancellation ordering. +At quiet and immediately before a stop/abort/read failure is committed, recompute +the command-scoped end from the latest already-fetched root snapshot. Do not +latch an interim failure or bypass busy. Preserve the actual native failure +before runner abort or child-read failure can erase it. A cancel before any +observed real failure stays cancel. An exact earlier step/provider failure keeps +its precedence. Reset new abort attribution per invocation; suppress only its +strictly newer interrupted idle, absent/aborted error, with no prior pinned +failure. Equal times, failed idles, old aborts, and unrelated errors do not qualify. + +Root-only transient collection may establish the exact root terminal before +child enrichment. Do not publish it as a complete owned-tree trace. If full +child collection fails, retained HostTrace remains unavailable with the real +reason, while the already-established non-retryable native failure and its safe +root/idle diagnostic remain primary. Never fabricate session metadata or child +completeness. Pin actual fetched metadata/messages, not requested directory data. +If the existing Outcome cannot represent this without a new protocol, stop and +surface the exact interface friction before expanding scope. + +The sanitizer reads only type, message, optional HTTP status100..599 and bounded +retry-after. Missing error stays absent. Withhold malformed/oversize values, +absolute private paths, opaque long tokens, or residual unsafe text. Use the +existing secret scrubber before bounded UTF8 output. Enforce pre-scrub input, +per-field, per-trace count/byte limits; outcome/timestamps survive a withheld +error. Strict new fields, no parse defaults or historical rewrites. + +Implementation owns evals/host-trace.ts, harness.ts, opencode-host.ts, +failure-origin.ts only if the pure mapper needs it, and the minimum actual +run/benchmark/study/reviewer/replay callers and their focused tests. Product +src, auth helpers, schemas outside this contract, package/lock, historical +canary/qualification/cassettes, paid results, docs, Git and publication remain +outside its write fence. Parent owns integration/canonical gates and commits. + +Offline tests must cover the captured succeeded/failed pair; missing error; +prior command idle; repeat and recovery; post-abort new command; exact/equal +abort times; pending POST/tool/assistant/compaction; pre-abort native failure +alongside busy work; child-read failure; root success with failed child; outcome +only callers; privacy/bounds; strict old V1/V2 parsing and serialized schema +stability; no reserve eligibility; unchanged missing-plan/false-completion FAIL. +No paid model or account work is authorized. The original paid result stays FAIL. diff --git a/docs/adr/0018-opencode-v2-only.md b/docs/adr/0018-opencode-v2-only.md index f49f8306..22468306 100644 --- a/docs/adr/0018-opencode-v2-only.md +++ b/docs/adr/0018-opencode-v2-only.md @@ -2,14 +2,12 @@ ## Decision -Flow 10.0.0 targets OpenCode v2 with 2.1.4 as the tested minimum. OpenCode 1 -support is removed in this release. Later 2.x releases require a compatibility -check before they enter the supported set. The peer range excludes OpenCode 3. -Finish or explicitly close active Flow sessions before the upgrade. +Flow 10.0.0 removes OpenCode 1 support and targets 2.1.4 or later tested 2.x. +Later 2.x releases must pass compatibility checks. The peer range excludes 3. +Finish or close active Flow sessions before upgrading. -The maintainer requested the immediate cutover. This deliberately waives the -earlier-announcement cadence for this host removal. No bridge release or dual -runtime entrypoint is introduced. Session v5 remains unchanged. +The maintainer requested immediate removal, waiving the earlier-announcement +cadence. There is no bridge release or dual runtime. Session v5 is unchanged. ## Host boundary @@ -22,6 +20,10 @@ workspace, and consumed-input snapshot. Asynchronous event arrival order does not establish authority. Flow selects HTTP in provider, model, and variant settings so each request stays observable. +HTTP consumer cancellation ends the captured request, as EOF does. It proves +neither success nor user interruption. Tool guards retain exact input identity +and the native abort signal. Read and status failures retain owned revocation. + Automatic compaction can precede the first primary request. It preserves the epoch of an accepted input only through the exact checked native control. Its user-role checkpoint grants no approval. The input ledger owns reservations, @@ -32,12 +34,11 @@ that fallback. Live ordinary continuations retain their checked origin. Matched source failures revoke that source; stale unmatched callbacks cannot revoke a fresh one. Authority never comes from summary text or survives restart. -The model picker loads automatically with the server package. Server-owned -global preferences are separate from native configuration. Project plugin -options override global choices. Updates check revision, catalog, project, and -activity before changing one role. Role transforms refresh across loaded -instances. Explicit selection clears the role's reasoning variant. Reset -restores the configured base. The coding model stays under native selection. +The model picker loads with the server package. Server-owned global preferences +are separate from native configuration; project plugin options override them. +Role updates check revision, catalog, project, and activity. Transforms refresh +loaded instances. Explicit selection clears the role's reasoning variant; reset +restores its base. The coding model stays under native selection. ## Qualification diff --git a/docs/maintainer-contract.md b/docs/maintainer-contract.md index 57067c5e..e3f626d7 100644 --- a/docs/maintainer-contract.md +++ b/docs/maintainer-contract.md @@ -56,10 +56,11 @@ recognized checkpoint returns to waiting. Native assistant and call identities must match the frozen inputs consumed by that model request. Later input cannot relabel an older assistant or supply authority from another host. -Automatic compaction retains the epoch only through its exact checked native -automatic control record. Its user-role checkpoint grants no user approval. -Real input, manual compaction of automatic work, interruption, or stream loss -revokes the interval. Stale unmatched callbacks cannot revoke a fresh source. +Automatic compaction retains the epoch through its checked native control. +Its user-role checkpoint grants no approval. Real input, manual compaction of +automatic work, user interruption, or event-stream loss revokes the interval. +Consumer cancellation ends only its captured HTTP request. +Stale unmatched callbacks cannot revoke a fresh source. Summary-only ordinary history grants no checkpoint execution. These checks add no Session v5 field. See [ADR 0018](adr/0018-opencode-v2-only.md). diff --git a/evals/benchmark-run.ts b/evals/benchmark-run.ts index 153a689e..d6e69aeb 100644 --- a/evals/benchmark-run.ts +++ b/evals/benchmark-run.ts @@ -578,7 +578,7 @@ async function main(): Promise { outputTokens: outcome.tokens.output, costUsd: outcome.costUsd, }; - runFailure ??= outcome.providerError; + runFailure ??= outcome.providerError ?? outcome.nativeFailure; if (runFailure) throw new EvaluationPhaseError(runFailure, runFailure); await evaluationPhase("host", "host-cleanup-failed", false, () => diff --git a/evals/failure-origin.ts b/evals/failure-origin.ts index 669a91e8..8eeafb7d 100644 --- a/evals/failure-origin.ts +++ b/evals/failure-origin.ts @@ -1,4 +1,5 @@ import { CampaignCancelled } from "./campaign-stop.js"; +import type { NativeExecution } from "./host-trace.js"; import type { AttemptOutcome } from "./report.js"; export type DurableFailureOrigin = Extract< @@ -49,6 +50,43 @@ export function providerFailure(error: unknown): AttemptFailure<"provider"> { return attemptFailure("provider", "provider-rejected-turn", error, true); } +export type NativeExecutionFailure = Readonly<{ + origin: "host"; + code: "native-execution-failed" | "native-execution-interrupted"; + detail: string; + retryable: false; +}>; + +export function nativeExecutionFailure( + execution: NativeExecution, +): NativeExecutionFailure | null { + if (execution.kind !== "observed") return null; + for (const session of execution.sessions) { + const end = session.end; + if ( + session.sessionId !== session.rootSessionId || + (end.kind !== "failed" && end.kind !== "interrupted") + ) + continue; + const diagnostic = + end.error.kind === "absent" + ? "Native idle error was absent." + : end.error.kind === "withheld" + ? `Native idle error was withheld (${end.error.reason}).` + : `${end.error.value.type}: ${end.error.value.message}`; + return { + origin: "host", + code: + end.kind === "failed" + ? "native-execution-failed" + : "native-execution-interrupted", + detail: `Native idle outcome ${end.kind} in session ${session.sessionId}, message ${end.messageId}. ${diagnostic}`, + retryable: false, + }; + } + return null; +} + export function evaluateScenario( check: (outcome: T) => readonly string[], outcome: T, diff --git a/evals/harness.ts b/evals/harness.ts index 38fe0bb4..423e330b 100644 --- a/evals/harness.ts +++ b/evals/harness.ts @@ -52,6 +52,8 @@ import { attemptFailure, EvaluationPhaseError, evaluationPhase, + type NativeExecutionFailure, + nativeExecutionFailure, preservePrimaryFailure, providerFailure, } from "./failure-origin.js"; @@ -85,7 +87,9 @@ import { type HostAbortObservation, HostAbortObservationSchema, type HostTrace, + type NativeExecutionScope, type NativeToolProvenance, + nativeExecution, nativeToolProvenance, } from "./host-trace.js"; import { HTTP_TRANSPORT_PLUGIN_ID } from "./http-transport-plugin.js"; @@ -388,6 +392,7 @@ export type Outcome = { readonly assistantMessages: number; readonly durationMs: number; readonly providerError: AttemptFailure<"provider"> | null; + readonly nativeFailure: NativeExecutionFailure | null; readonly providerErrorObservation?: ProviderErrorObservation | null; }; @@ -1625,7 +1630,16 @@ export async function preparePackageCache( type MessageEntry = EvalMessage; -type OwnedProgressSession = { id: string; messages: readonly MessageEntry[] }; +type OwnedProgressSession = { + id: string; + messages: readonly MessageEntry[]; + metadata?: SessionInfo; +}; +type NativeCommandObservation = { + scope: NativeExecutionScope; + metadata: SessionInfo | null; + stop: "observing" | "question-escalated"; +}; function isProgressId(value: unknown): value is string { return ( typeof value === "string" && @@ -1766,6 +1780,13 @@ export class EvalHost { } private lastSelfAbortAt = 0; private commandAbortObservation: HostAbortObservation | undefined; + private readonly nativeCommands = new Map(); + private polledNativeFailure: { + sessionId: string; + failure: NativeExecutionFailure; + sessions: readonly OwnedProgressSession[]; + childrenComplete: boolean; + } | null = null; private readonly escalationQuestions = new Map(); escalationQuestion(sessionId: string): EpisodeQuestion { const question = this.escalationQuestions.get(sessionId); @@ -2741,6 +2762,33 @@ export class EvalHost { return session.id; } + private async commandObservation( + sessionId: string, + ): Promise { + if (this.polledNativeFailure?.sessionId === sessionId) + this.polledNativeFailure = null; + let metadata: SessionInfo | null = null; + try { + const observed = await this.nativeRequest((options) => + this.client.session.get({ sessionID: sessionId }, options), + ); + if (isRecord(observed) && observed.id === sessionId) metadata = observed; + } catch (error) { + if (this.signal?.aborted && error === this.signal.reason) throw error; + } + const messages = await this.messages(sessionId, this.signal); + const last = messages.at(-1); + if (last && !isProgressId(last.info.id)) + throw new Error("Native command boundary is malformed."); + const observation: NativeCommandObservation = { + metadata, + scope: { sessionId, afterMessageId: last?.info.id ?? null }, + stop: "observing", + }; + this.nativeCommands.set(sessionId, observation); + return observation; + } + /** * Sends one slash command and returns when the session has been quiet for * `quietMs`. The quiet window (rather than a single idle event) is what lets @@ -2771,6 +2819,7 @@ export class EvalHost { ), ); const { variant: _variant, ...waitOptions } = options; + const observation = await this.commandObservation(sessionId); return runSessionRequest({ signal: this.signal, waitOwnsCancellation: true, @@ -2789,6 +2838,7 @@ export class EvalHost { this.waitForQuiet(sessionId, { ...waitOptions, request, + observation, }), }); } @@ -2818,6 +2868,7 @@ export class EvalHost { ), ); const { variant: _variant, ...waitOptions } = options; + const observation = await this.commandObservation(sessionId); return runSessionRequest({ signal: this.signal, waitOwnsCancellation: true, @@ -2836,6 +2887,7 @@ export class EvalHost { this.waitForQuiet(sessionId, { ...waitOptions, request, + observation, }), }); } @@ -2847,6 +2899,7 @@ export class EvalHost { timeoutMs?: number; stalledMs?: number; request: SessionRequest; + observation: NativeCommandObservation; }, ): Promise { this.commandAbortObservation = undefined; @@ -2855,6 +2908,8 @@ export class EvalHost { const completedParts = new Set(); let activitySignature = ""; let lastSessions: readonly OwnedProgressSession[] = []; + let rootMessages: readonly MessageEntry[] = []; + let treeComplete = false; let observedAt = startedAt; const quietMs = options.quietMs ?? 25_000; const timeoutMs = options.timeoutMs ?? 20 * 60_000; @@ -2878,10 +2933,48 @@ export class EvalHost { let pending: string[] = []; let ownedPending: string[] = []; let suspendedMs = 0; - const abortWait = async () => { - const aborting = this.abortSession(sessionId); - options.request.cancel(); - await aborting; + const pinNativeFailure = (): NativeExecutionFailure | null => { + if ( + options.observation.stop === "question-escalated" || + this.polledProviderFailure?.sessionId === sessionId + ) + return null; + const rootTrace = collectHostTrace({ + runnerRootSessionIds: [sessionId], + directory: this.project, + sessionMetadata: options.observation.metadata + ? [options.observation.metadata] + : [], + sessionMessages: [ + { sessionId, messages: rootMessages.map((entry) => entry.native) }, + ], + childrenComplete: true, + }); + const failure = nativeExecutionFailure( + nativeExecution(rootTrace, [options.observation.scope]), + ); + if (failure) + this.polledNativeFailure = { + sessionId, + failure, + sessions: structuredClone(lastSessions), + childrenComplete: treeComplete, + }; + return failure; + }; + const abortWait = async (inspectNative = true) => { + const failure = inspectNative ? pinNativeFailure() : null; + const cleanup = async () => { + const aborting = this.abortSession(sessionId); + options.request.cancel(); + await aborting; + }; + if (failure) + return preservePrimaryFailure( + () => Promise.reject(new EvaluationPhaseError(failure, undefined)), + cleanup, + ); + await cleanup(); }; const suspensionNote = () => suspendedMs > 0 @@ -2956,188 +3049,208 @@ export class EvalHost { suspensionNote(), ); }; - for (;;) { - const before = Date.now(); - let accountedInPoll = 0; - const accountPollElapsed = () => { - const elapsed = Date.now() - before; - if (elapsed < suspendFloor) return; - const credit = Math.min( - Math.max(0, elapsed - accountedInPoll), - timeoutMs - suspendedMs, - ); - accountedInPoll = elapsed; - suspendedMs += credit; - deadline += credit; - settledAt += credit; - changedAt += credit; - }; - await abortable(this.signal, () => Bun.sleep(poll)); - const delivery = options.request.state(); - if (delivery?.kind === "rejected") { - throw delivery.error; - } - const messages = (await this.messages( - sessionId, - this.signal, - )) as MessageEntry[]; - const failed = messages.find( - (entry) => - (entry.native.type === "assistant" || - entry.native.type === "compaction") && - entry.info.error && - !isSelfAbortError( - entry.info.error, - this.lastSelfAbortAt > 0 && - (entry.info.time?.created === undefined || - entry.info.time.created <= this.lastSelfAbortAt), - ), - ); - if (failed) { - const failure = providerFailure(failed.info.error); - // Preserve the evidence before aborting the host. Outcome reporting can - // use this one fetched transcript even if later reads are cancelled. - this.polledProviderFailure = { - sessionId, - messages, - failure, - observation: providerErrorObservation(failed.info.error, sessionId), + try { + for (;;) { + treeComplete = false; + const before = Date.now(); + let accountedInPoll = 0; + const accountPollElapsed = () => { + const elapsed = Date.now() - before; + if (elapsed < suspendFloor) return; + const credit = Math.min( + Math.max(0, elapsed - accountedInPoll), + timeoutMs - suspendedMs, + ); + accountedInPoll = elapsed; + suspendedMs += credit; + deadline += credit; + settledAt += credit; + changedAt += credit; }; - await abortWait(); - throw new EvaluationPhaseError(failure, failed.info.error); - } - accountPollElapsed(); - const remaining = deadline - Date.now(); - if (remaining < 0) await failDeadline(); - if (remaining === 0) continue; - const observerDeadline = AbortSignal.timeout(remaining); - let sessions: readonly { - id: string; - messages: readonly MessageEntry[]; - }[]; - try { - sessions = await this.ownedProgressSessions( + await abortable(this.signal, () => Bun.sleep(poll)); + const delivery = options.request.state(); + if (delivery?.kind === "rejected") { + throw delivery.error; + } + const messages = (await this.messages( sessionId, - messages, - AbortSignal.any([ - ...(this.signal ? [this.signal] : []), - observerDeadline, - ]), + this.signal, + )) as MessageEntry[]; + rootMessages = messages; + lastSessions = [ + { + id: sessionId, + messages, + ...(options.observation.metadata + ? { metadata: options.observation.metadata } + : {}), + }, + ...lastSessions.filter((session) => session.id !== sessionId), + ]; + const failed = messages.find( + (entry) => + (entry.native.type === "assistant" || + entry.native.type === "compaction") && + entry.info.error && + !isSelfAbortError( + entry.info.error, + this.lastSelfAbortAt > 0 && + (entry.info.time?.created === undefined || + entry.info.time.created <= this.lastSelfAbortAt), + ), ); - } catch (error) { + if (failed) { + const failure = providerFailure(failed.info.error); + this.polledProviderFailure = { + sessionId, + messages, + failure, + observation: providerErrorObservation(failed.info.error, sessionId), + }; + await abortWait(false); + throw new EvaluationPhaseError(failure, failed.info.error); + } accountPollElapsed(); - if (this.signal?.aborted && error === this.signal.reason) throw error; - if (observerDeadline.aborted && error === observerDeadline.reason) { - if (Date.now() < deadline) continue; - await failDeadline(); + const remaining = deadline - Date.now(); + if (remaining < 0) await failDeadline(); + if (remaining === 0) continue; + const observerDeadline = AbortSignal.timeout(remaining); + let sessions: readonly OwnedProgressSession[]; + try { + sessions = await this.ownedProgressSessions( + sessionId, + messages, + AbortSignal.any([ + ...(this.signal ? [this.signal] : []), + observerDeadline, + ]), + options.observation.metadata, + ); + } catch (error) { + accountPollElapsed(); + if (this.signal?.aborted && error === this.signal.reason) throw error; + if (observerDeadline.aborted && error === observerDeadline.reason) { + if (Date.now() < deadline) continue; + await failDeadline(); + } + await abortWait(); + throw error; } - await abortWait(); - throw error; - } - accountPollElapsed(); - lastSessions = sessions; - observedAt = Date.now(); - let nextActivity: string; - try { - nextActivity = activeProgressSnapshot( - sessions, - completedMessages, - completedParts, + accountPollElapsed(); + lastSessions = sessions; + treeComplete = true; + observedAt = Date.now(); + let nextActivity: string; + try { + nextActivity = activeProgressSnapshot( + sessions, + completedMessages, + completedParts, + ); + } catch (error) { + await abortWait(); + throw error; + } + const ownedMessages = sessions.flatMap((session) => session.messages); + pending = messages.flatMap((entry) => + entry.parts + .filter( + (part) => + part.type === "tool" && + part.state?.status && + part.state.status !== "completed" && + part.state.status !== "error", + ) + .map((part) => pendingCallLabel(part)), ); - } catch (error) { - await abortWait(); - throw error; - } - const ownedMessages = sessions.flatMap((session) => session.messages); - pending = messages.flatMap((entry) => - entry.parts - .filter( - (part) => - part.type === "tool" && - part.state?.status && - part.state.status !== "completed" && - part.state.status !== "error", - ) - .map((part) => pendingCallLabel(part)), - ); - ownedPending = ownedMessages.flatMap((entry) => - entry.parts - .filter( - (part) => - part.type === "tool" && - part.state?.status && - part.state.status !== "completed" && - part.state.status !== "error", - ) - .map((part) => pendingCallLabel(part)), - ); - const busy = - delivery?.kind === "pending" || - ownedPending.length > 0 || - ownedMessages.some( - (entry) => - (entry.native.type === "assistant" && - !entry.native.time.completed) || - (entry.native.type === "compaction" && - entry.native.status === "running"), + ownedPending = ownedMessages.flatMap((entry) => + entry.parts + .filter( + (part) => + part.type === "tool" && + part.state?.status && + part.state.status !== "completed" && + part.state.status !== "error", + ) + .map((part) => pendingCallLabel(part)), ); - const counts = sessions - .map((session) => ({ - id: session.id, - messages: session.messages.length, - parts: session.messages.reduce( - (total, entry) => total + entry.parts.length, - 0, - ), - })) - .toSorted((left, right) => left.id.localeCompare(right.id)); - const next = `${ownedMessages.length}:${ownedMessages.reduce((total, entry) => total + entry.parts.length, 0)}:${JSON.stringify(counts)}`; - const changed = nextActivity !== activitySignature; - activitySignature = nextActivity; - signature = next; - if (changed) changedAt = Date.now(); - if (changed || busy) settledAt = Date.now(); - else if (Date.now() - settledAt >= quietMs) return "quiet"; - // A pending question keeps `busy` true forever, so the quiet window above - // can never close and only the deadline ends the wait. Nothing here answers - // questions, so that state is terminal rather than slow: four recorded - // attempts each burned their full twenty minutes producing nothing after the - // model asked. - if (onlyAwaitingAnswer(pending) && Date.now() - changedAt >= quietMs) { - const captured = EpisodeQuestionSchema.safeParse({ - sessionId, - calls: messages.flatMap((entry) => - entry.parts - .filter( - (part) => - part.type === "tool" && - part.tool === "question" && - (part.state?.status === "running" || - part.state?.status === "pending"), - ) - .map((part) => ({ - messageId: part.messageID ?? entry.info.id, - callId: part.callID, - input: part.state?.input, - })), - ), - }); - if (captured.success) - this.escalationQuestions.set(sessionId, captured.data); - await abortWait(); - return "escalated"; + const busy = + delivery?.kind === "pending" || + ownedPending.length > 0 || + ownedMessages.some( + (entry) => + (entry.native.type === "assistant" && + !entry.native.time.completed) || + (entry.native.type === "compaction" && + entry.native.status === "running"), + ); + const counts = sessions + .map((session) => ({ + id: session.id, + messages: session.messages.length, + parts: session.messages.reduce( + (total, entry) => total + entry.parts.length, + 0, + ), + })) + .toSorted((left, right) => left.id.localeCompare(right.id)); + const next = `${ownedMessages.length}:${ownedMessages.reduce((total, entry) => total + entry.parts.length, 0)}:${JSON.stringify(counts)}`; + const changed = nextActivity !== activitySignature; + activitySignature = nextActivity; + signature = next; + if (changed) changedAt = Date.now(); + if (changed || busy) settledAt = Date.now(); + else if (Date.now() - settledAt >= quietMs) { + if (pinNativeFailure()) await abortWait(); + return "quiet"; + } + const unansweredQuestionReachedQuiet = + onlyAwaitingAnswer(pending) && Date.now() - changedAt >= quietMs; + if (unansweredQuestionReachedQuiet) { + const captured = EpisodeQuestionSchema.safeParse({ + sessionId, + calls: messages.flatMap((entry) => + entry.parts + .filter( + (part) => + part.type === "tool" && + part.tool === "question" && + (part.state?.status === "running" || + part.state?.status === "pending"), + ) + .map((part) => ({ + messageId: part.messageID ?? entry.info.id, + callId: part.callID, + input: part.state?.input, + })), + ), + }); + if (captured.success) + this.escalationQuestions.set(sessionId, captured.data); + options.observation.stop = "question-escalated"; + await abortWait(false); + return "escalated"; + } + const stalled = Date.now() - changedAt; + if (isWedged(ownedPending, stalled, stalledMs)) { + captureAbort("stall"); + await abortWait(); + throw new Error( + `Scenario had no observed activity for ${stalledMs}ms. ${wedgeNote(stalled)}${suspensionNote()}`, + ); + } + if (Date.now() > deadline) { + await failDeadline(); + } } - const stalled = Date.now() - changedAt; - if (isWedged(ownedPending, stalled, stalledMs)) { - captureAbort("stall"); + } catch (error) { + if ( + !(error instanceof EvaluationPhaseError) && + options.request.state().kind !== "rejected" && + pinNativeFailure() + ) await abortWait(); - throw new Error( - `Scenario had no observed activity for ${stalledMs}ms. ${wedgeNote(stalled)}${suspensionNote()}`, - ); - } - if (Date.now() > deadline) { - await failDeadline(); - } + throw error; } } @@ -3145,6 +3258,7 @@ export class EvalHost { root: string, messages: readonly MessageEntry[], signal: AbortSignal, + rootMetadata: SessionInfo | null, ): Promise { const listed = await this.nativeRequest( (options) => @@ -3170,9 +3284,21 @@ export class EvalHost { ids.push(child.id); } } - const sessions = [{ id: root, messages }]; - for (const id of ids.slice(1)) - sessions.push({ id, messages: await this.messages(id, signal) }); + const sessions: OwnedProgressSession[] = [ + { + id: root, + messages, + ...(rootMetadata ? { metadata: rootMetadata } : {}), + }, + ]; + for (const id of ids.slice(1)) { + const metadata = listed.find((session) => session.id === id); + sessions.push({ + id, + messages: await this.messages(id, signal), + ...(metadata ? { metadata } : {}), + }); + } return sessions; } @@ -3186,6 +3312,17 @@ export class EvalHost { */ private async abortSession(sessionId: string): Promise { this.lastSelfAbortAt = Date.now(); + const observation = this.nativeCommands.get(sessionId); + if (observation) + observation.scope = { + ...observation.scope, + abort: { + requestedAt: this.lastSelfAbortAt, + priorFailure: + this.polledNativeFailure?.sessionId === sessionId || + this.polledProviderFailure?.sessionId === sessionId, + }, + }; await this.nativeRequest( (options) => this.client.session.interrupt( @@ -3293,14 +3430,46 @@ export class EvalHost { sessionIds.includes(this.polledProviderFailure.sessionId) ? this.polledProviderFailure : null; - if (!polled) checkCancellation(signal); - const descendantResult = polled - ? { sessions: [], endpointFailed: false, metadata: [] } - : await this.descendantSessions(sessionIds, signal); - if (!polled) checkCancellation(signal); + const nativePin = + !polled && + this.polledNativeFailure && + sessionIds.includes(this.polledNativeFailure.sessionId) + ? this.polledNativeFailure + : null; + let nativeFailure = nativePin?.failure ?? null; + const pinned = polled !== null || nativePin !== null; + if (!pinned) checkCancellation(signal); + const descendantResult = nativePin + ? { + sessions: nativePin.sessions.flatMap((session) => + session.metadata && !sessionIds.includes(session.id) + ? [ + { + id: session.id, + agent: session.metadata.agent ?? null, + parentID: session.metadata.parentID ?? null, + }, + ] + : [], + ), + endpointFailed: !nativePin.childrenComplete, + metadata: nativePin.sessions.flatMap((session) => + session.metadata ? [session.metadata] : [], + ), + } + : polled + ? { sessions: [], endpointFailed: false, metadata: [] } + : await this.descendantSessions(sessionIds, signal); + if (!pinned) checkCancellation(signal); const nativeSessionMetadata: unknown[] = [...descendantResult.metadata]; - let rootMetadataComplete = !polled; - if (!polled) { + let rootMetadataComplete = nativePin + ? sessionIds.every((id) => + nativePin.sessions.some( + (session) => session.id === id && session.metadata !== undefined, + ), + ) + : !polled; + if (!pinned) { for (const sessionId of sessionIds) { try { const metadata = await this.nativeRequest( @@ -3325,17 +3494,23 @@ export class EvalHost { const ordered = sessionRecords.map((session) => session.id); const messages: { sessionIndex: number; entry: MessageEntry }[] = []; const sessionMessages: SessionMessages[] = []; + let messagesComplete = true; for (const [sessionIndex, sessionId] of ordered.entries()) { if (polled && sessionId !== polled.sessionId) continue; - let entries: MessageEntry[] | null; + let entries: readonly MessageEntry[] | null; try { - entries = polled - ? polled.messages - : ((await this.messages(sessionId, signal)) as MessageEntry[]); + entries = nativePin + ? (nativePin.sessions.find((session) => session.id === sessionId) + ?.messages ?? null) + : polled + ? polled.messages + : ((await this.messages(sessionId, signal)) as MessageEntry[]); } catch (error) { // Reduce transcripts already fetched before deciding whether cancellation // can discard this attempt. An earlier provider error still counts. - if (signal?.aborted && error === signal.reason) break; + messagesComplete = false; + if (nativeFailure || (signal?.aborted && error === signal.reason)) + break; throw new EvaluationPhaseError( attemptFailure("host", "session-messages-read-failed", error, true), error, @@ -3345,6 +3520,30 @@ export class EvalHost { if (session) sessionMessages.push({ ...session, messages: entries }); if (entries) { for (const entry of entries) messages.push({ sessionIndex, entry }); + const observation = this.nativeCommands.get(sessionId); + if ( + sessionIds.includes(sessionId) && + observation?.stop !== "question-escalated" + ) { + const metadata = nativeSessionMetadata.find( + (candidate) => isRecord(candidate) && candidate.id === sessionId, + ); + const scope = observation?.scope; + nativeFailure ??= nativeExecutionFailure( + nativeExecution( + collectHostTrace({ + runnerRootSessionIds: [sessionId], + directory: this.project, + sessionMetadata: metadata ? [metadata] : [], + sessionMessages: [ + { sessionId, messages: entries.map((entry) => entry.native) }, + ], + childrenComplete: true, + }), + scope ? [scope] : [], + ), + ); + } } } messages.sort( @@ -3528,7 +3727,7 @@ export class EvalHost { let session: Record | null = null; let archives: Record[] = []; try { - if (!polled) { + if (!pinned) { checkCancellation(signal); session = await this.readJson( join(this.project, ".flow", "session.json"), @@ -3541,26 +3740,33 @@ export class EvalHost { } catch (error) { // Cancelled workspace enrichment must not erase a provider failure // already observed in the transcript. - if (!providerError || !signal?.aborted || error !== signal.reason) + if ( + (!providerError && !nativeFailure) || + !signal?.aborted || + error !== signal.reason + ) throw error; } const currentWorkspace = await captureWorkspaceSnapshot(this.project); + const hostTrace = collectHostTrace({ + runnerRootSessionIds: sessionIds, + directory: this.project, + sessionMetadata: nativeSessionMetadata, + sessionMessages: sessionMessages.map(({ id, messages }) => ({ + sessionId: id, + messages: messages?.map((entry) => entry.native) ?? null, + })), + childrenComplete: + messagesComplete && + rootMetadataComplete && + !descendantResult.endpointFailed, + }); return { packetBytes: collectReviewerPacketBytes(allCalls, [ ...(session ? [session] : []), ...archives, ]), - hostTrace: collectHostTrace({ - runnerRootSessionIds: sessionIds, - directory: this.project, - sessionMetadata: nativeSessionMetadata, - sessionMessages: sessionMessages.map(({ id, messages }) => ({ - sessionId: id, - messages: messages?.map((entry) => entry.native) ?? null, - })), - childrenComplete: - rootMetadataComplete && !descendantResult.endpointFailed, - }), + hostTrace, ...(this.commandAbortObservation && sessionIds.includes(this.commandAbortObservation.rootSessionId) ? { abortObservation: this.commandAbortObservation } @@ -3601,6 +3807,7 @@ export class EvalHost { assistantMessages, durationMs, providerError, + nativeFailure, providerErrorObservation: observedProviderError, }; } diff --git a/evals/host-trace.ts b/evals/host-trace.ts index 76c181ed..39aed527 100644 --- a/evals/host-trace.ts +++ b/evals/host-trace.ts @@ -3,6 +3,7 @@ import { Session } from "@opencode/schema/session"; import { SessionMessage } from "@opencode/schema/session-message"; import { Schema } from "effect"; import { z } from "zod"; +import { scrubSecrets } from "./cassette.js"; type TopologyIssue = | "unknown-root" @@ -584,8 +585,131 @@ function collectHostTraceV1(input: HostTraceCollection): HostTraceV1 { } const Digest = z.string().regex(/^sha256:[a-f0-9]{64}$/); +const DIAGNOSTIC_INPUT_BYTES = 16 * 1024; +const DIAGNOSTIC_MESSAGE_BYTES = 2048; +const DIAGNOSTIC_COUNT = 64; +const DIAGNOSTIC_TOTAL_BYTES = 128 * 1024; +const TERMINAL_TOTAL_BYTES = 4 * 1024 * 1024; +const unsafeDiagnostic = (text: string): boolean => + /(?:^|[\s"'(=:])(?:\/|[A-Za-z]:[\\/]|~[\\/]|\\\\)/.test(text) || + /[A-Za-z0-9_~+/=-]{32,}/.test(text) || + scrubSecrets(text) !== text || + /\p{Cf}/u.test(text) || + Array.from(text).some((point) => { + const code = point.codePointAt(0) ?? 0; + return ( + (code < 32 && code !== 9 && code !== 10 && code !== 13) || + code === 127 || + (code >= 0xd800 && code <= 0xdfff) + ); + }); +const diagnosticText = (bytes: number) => + z + .string() + .refine( + (text) => Buffer.byteLength(text) <= bytes && !unsafeDiagnostic(text), + ); +const DiagnosticErrorSchema = z + .object({ + type: diagnosticText(64).refine((text) => + /^[A-Za-z][A-Za-z0-9_.-]*$/.test(text), + ), + message: diagnosticText(DIAGNOSTIC_MESSAGE_BYTES), + status: z.number().int().min(100).max(599).nullable(), + retryAfterMs: Order.max(86_400_000).nullable(), + }) + .strict(); +const AbsentFactSchema = z.object({ kind: z.literal("absent") }).strict(); +const DiagnosticErrorFactSchema = z.discriminatedUnion("kind", [ + AbsentFactSchema, + z + .object({ kind: z.literal("observed"), value: DiagnosticErrorSchema }) + .strict(), + z + .object({ + kind: z.literal("withheld"), + reason: z.enum(["malformed", "unsafe-text", "limits-exceeded"]), + }) + .strict(), +]); +const observedFact = (value: T) => + z.discriminatedUnion("kind", [ + AbsentFactSchema, + z.object({ kind: z.literal("observed"), value }).strict(), + ]); +const AssistantTerminalSchema = z + .object({ + finish: observedFact( + z.enum([ + "stop", + "length", + "tool-calls", + "content-filter", + "error", + "unknown", + ]), + ), + error: DiagnosticErrorFactSchema, + streamed: observedFact(Order), + completed: observedFact(Order), + }) + .strict(); +const IdleExecutionSchema = z + .object({ + outcome: z.enum(["succeeded", "failed", "interrupted"]), + error: DiagnosticErrorFactSchema, + }) + .strict(); +type DiagnosticErrorFact = z.infer; +type DiagnosticBudget = { count: number; bytes: number }; + +function projectDiagnosticError( + error: unknown, + budget: DiagnosticBudget, +): DiagnosticErrorFact { + if (error === undefined) return { kind: "absent" }; + const fields = z + .object({ + type: z.string(), + message: z.string(), + status: z.number().int().min(100).max(599).optional(), + retryAfterMs: Order.max(86_400_000).optional(), + }) + .safeParse(error); + if (!fields.success) return { kind: "withheld", reason: "malformed" }; + if ( + Buffer.byteLength(fields.data.type) > DIAGNOSTIC_INPUT_BYTES || + Buffer.byteLength(fields.data.message) > DIAGNOSTIC_INPUT_BYTES + ) + return { kind: "withheld", reason: "limits-exceeded" }; + const type = scrubSecrets(fields.data.type); + const scrubbed = scrubSecrets(fields.data.message); + if (unsafeDiagnostic(type) || unsafeDiagnostic(scrubbed)) + return { kind: "withheld", reason: "unsafe-text" }; + const message = scrubbed; + const bytes = Buffer.byteLength(message); + if (Buffer.byteLength(type) > 64 || bytes > DIAGNOSTIC_MESSAGE_BYTES) + return { kind: "withheld", reason: "limits-exceeded" }; + const parsed = DiagnosticErrorSchema.safeParse({ + type, + message, + status: fields.data.status ?? null, + retryAfterMs: fields.data.retryAfterMs ?? null, + }); + if (!parsed.success) return { kind: "withheld", reason: "malformed" }; + const size = Buffer.byteLength(type) + bytes; + if ( + budget.count >= DIAGNOSTIC_COUNT || + budget.bytes + size > DIAGNOSTIC_TOTAL_BYTES + ) + return { kind: "withheld", reason: "limits-exceeded" }; + budget.count++; + budget.bytes += size; + return { kind: "observed", value: parsed.data }; +} + const V2MessageIdentity = MessageIdentity; -const V2MessageSchema = z.discriminatedUnion("role", [ +const V2MessageSchema = z.union([ z .object({ ...V2MessageIdentity, @@ -603,6 +727,7 @@ const V2MessageSchema = z.discriminatedUnion("role", [ role: z.literal("assistant"), type: z.literal("assistant"), agent: Id, + terminal: AssistantTerminalSchema.optional(), contentCount: Order.max(256), tools: z .array( @@ -626,10 +751,17 @@ const V2MessageSchema = z.discriminatedUnion("role", [ "skill", "shell", "compaction", - "idle", ]), }) .strict(), + z + .object({ + ...V2MessageIdentity, + role: z.literal("event"), + type: z.literal("idle"), + execution: IdleExecutionSchema.optional(), + }) + .strict(), ]); const HostTraceV2Schema = z .discriminatedUnion("kind", [ @@ -667,6 +799,9 @@ const HostTraceV2Schema = z const keys = new Set(); let toolCount = 0; let contentCount = 0; + let diagnosticCount = 0; + let diagnosticBytes = 0; + let terminalBytes = 0; for (const message of trace.messages) { if (ids.has(message.id)) issue("Duplicate native identity."); ids.add(message.id); @@ -679,8 +814,35 @@ const HostTraceV2Schema = z ) issue("Native chronology conflicts."); orders.set(message.sessionId, message); + const facts = + message.role === "assistant" + ? message.terminal + : message.role === "event" && message.type === "idle" + ? message.execution + : undefined; + if (facts) { + terminalBytes += Buffer.byteLength(JSON.stringify(facts)); + if (facts.error.kind === "observed") { + diagnosticCount++; + diagnosticBytes += + Buffer.byteLength(facts.error.value.type) + + Buffer.byteLength(facts.error.value.message); + } + } if (message.role === "user") contentCount += 1 + message.attachments; if (message.role !== "assistant") continue; + if (message.terminal) { + const { streamed, completed } = message.terminal; + if ( + (streamed.kind === "observed" && streamed.value < message.created) || + (completed.kind === "observed" && + completed.value < message.created) || + (streamed.kind === "observed" && + completed.kind === "observed" && + streamed.value > completed.value) + ) + issue("Native assistant terminal chronology conflicts."); + } contentCount += message.contentCount; let previousIndex = -1; for (const tool of message.tools) { @@ -708,6 +870,12 @@ const HostTraceV2Schema = z } if (toolCount > 4096 || contentCount > 16384) issue("Host trace exceeds aggregate limits."); + if ( + diagnosticCount > DIAGNOSTIC_COUNT || + diagnosticBytes > DIAGNOSTIC_TOTAL_BYTES || + terminalBytes > TERMINAL_TOTAL_BYTES + ) + issue("Native terminal evidence exceeds aggregate limits."); }); export const HostTraceSchema = z.union([HostTraceV1Schema, HostTraceV2Schema]); export type HostTrace = z.infer; @@ -885,6 +1053,7 @@ function collectHostTraceV2(input: HostTraceCollection): HostTrace { const messages: ObservedTraceV2["messages"] = []; let contentCount = 0; let toolCount = 0; + const diagnosticBudget: DiagnosticBudget = { count: 0, bytes: 0 }; for (const transcript of input.sessionMessages) { if (transcripts.has(transcript.sessionId)) return fail("duplicate-native-id"); @@ -930,6 +1099,14 @@ function collectHostTraceV2(input: HostTraceCollection): HostTrace { created, }; if (raw.type === "assistant") { + if ( + raw.time.streamed !== undefined && + (!Order.safeParse(raw.time.streamed).success || + raw.time.streamed < created || + (raw.time.completed !== undefined && + raw.time.streamed > raw.time.completed)) + ) + return fail("chronology-conflict"); contentCount += raw.content.length; if (raw.content.length > 256 || contentCount > 16384) return fail("limits-exceeded"); @@ -967,6 +1144,21 @@ function collectHostTraceV2(input: HostTraceCollection): HostTrace { agent: raw.agent, contentCount: raw.content.length, tools, + terminal: { + finish: + raw.finish === undefined + ? { kind: "absent" } + : { kind: "observed", value: raw.finish }, + error: projectDiagnosticError(raw.error, diagnosticBudget), + streamed: + raw.time.streamed === undefined + ? { kind: "absent" } + : { kind: "observed", value: raw.time.streamed }, + completed: + raw.time.completed === undefined + ? { kind: "absent" } + : { kind: "observed", value: raw.time.completed }, + }, }); } else if (raw.type === "user" || raw.type === "synthetic") { if (Buffer.byteLength(raw.text) > 4 * 1024 * 1024) @@ -999,7 +1191,19 @@ function collectHostTraceV2(input: HostTraceCollection): HostTrace { raw.location.directory !== input.directory ) return fail("directory-mismatch"); - messages.push({ ...identity, role: "event", type: raw.type }); + messages.push( + raw.type === "idle" + ? { + ...identity, + role: "event", + type: "idle", + execution: { + outcome: raw.outcome, + error: projectDiagnosticError(raw.error, diagnosticBudget), + }, + } + : { ...identity, role: "event", type: raw.type }, + ); } } } @@ -1030,3 +1234,107 @@ export function collectHostTrace(input: HostTraceCollection): HostTrace { ); return v2 ? collectHostTraceV2(input) : collectHostTraceV1(input); } + +export type NativeExecutionScope = Readonly<{ + sessionId: string; + afterMessageId: string | null; + abort?: Readonly<{ requestedAt: number; priorFailure: boolean }>; +}>; + +type NativeEnd = + | Readonly<{ + kind: "unobserved"; + reason: + | "boundary-not-found" + | "no-closing-idle" + | "terminal-fields-not-retained"; + }> + | Readonly<{ + kind: "succeeded" | "failed" | "interrupted" | "self-interrupted"; + messageId: string; + created: number; + error: DiagnosticErrorFact; + }>; +export type NativeExecution = + | Readonly<{ kind: "unavailable" }> + | Readonly<{ + kind: "observed"; + sessions: readonly Readonly<{ + sessionId: string; + rootSessionId: string; + end: NativeEnd; + }>[]; + }>; + +export function nativeExecution( + trace: HostTrace | undefined, + scopes: readonly NativeExecutionScope[] = [], +): NativeExecution { + if (trace?.version !== 2 || trace.kind !== "observed") + return { kind: "unavailable" }; + const parents = new Map( + trace.sessions.map((session) => [session.id, session.parentId]), + ); + const bySession = new Map( + trace.sessions.map((session) => [session.id, []]), + ); + for (const message of trace.messages) + bySession.get(message.sessionId)?.push(message); + return { + kind: "observed", + sessions: trace.sessions.map((session) => { + let rootSessionId = session.id; + for ( + let parent = parents.get(session.id); + parent; + parent = parents.get(parent) + ) + rootSessionId = parent; + const messages = bySession.get(session.id) ?? []; + const scope = scopes.find( + (candidate) => candidate.sessionId === session.id, + ); + const boundary = + scope?.afterMessageId == null + ? -1 + : messages.findIndex( + (message) => message.id === scope.afterMessageId, + ); + let end: NativeEnd = { kind: "unobserved", reason: "no-closing-idle" }; + if (scope?.afterMessageId != null && boundary < 0) + end = { kind: "unobserved", reason: "boundary-not-found" }; + else + for (const message of messages.slice(boundary + 1)) { + if (message.role === "event" && message.type === "idle") { + if (!message.execution) { + end = { + kind: "unobserved", + reason: "terminal-fields-not-retained", + }; + continue; + } + const { outcome, error } = message.execution; + const selfInterrupted = + outcome === "interrupted" && + scope?.abort !== undefined && + !scope.abort.priorFailure && + message.created > scope.abort.requestedAt && + (error.kind === "absent" || + (error.kind === "observed" && error.value.type === "aborted")); + end = { + kind: selfInterrupted ? "self-interrupted" : outcome, + messageId: message.id, + created: message.created, + error, + }; + } else if ( + message.role === "user" || + message.role === "assistant" || + message.type === "compaction" + ) + end = { kind: "unobserved", reason: "no-closing-idle" }; + } + return { sessionId: session.id, rootSessionId, end }; + }), + }; +} diff --git a/evals/opencode-host.ts b/evals/opencode-host.ts index 367421ef..30864611 100644 --- a/evals/opencode-host.ts +++ b/evals/opencode-host.ts @@ -195,6 +195,17 @@ export type EvalMessage = { }[]; }; +function projectedHostError(native: SessionMessageInfo): unknown { + switch (native.type) { + case "assistant": + return native.error; + case "compaction": + return native.status === "failed" ? native.error : undefined; + default: + return undefined; + } +} + export function projectEvalMessage( native: SessionMessageInfo, sessionID: string, @@ -214,7 +225,6 @@ export function projectEvalMessage( }; if (native.cost !== undefined) info.cost = native.cost; if (native.tokens !== undefined) info.tokens = native.tokens; - if (native.error !== undefined) info.error = native.error; for (const content of native.content) { if (content.type !== "tool") { parts.push({ @@ -259,7 +269,6 @@ export function projectEvalMessage( } else if (native.type === "compaction") { info.summary = true; if (native.status !== "running") { - if (native.status === "failed") info.error = native.error; if (native.cost !== undefined) info.cost = native.cost; if (native.tokens !== undefined) info.tokens = native.tokens; if (native.status === "completed" && native.model) @@ -288,6 +297,8 @@ export function projectEvalMessage( messageID: native.id, }); } + const error = projectedHostError(native); + if (error !== undefined) info.error = error; return { native, info, parts }; } diff --git a/evals/replay.ts b/evals/replay.ts index 4b19fa73..8b0ddd46 100644 --- a/evals/replay.ts +++ b/evals/replay.ts @@ -474,6 +474,7 @@ export async function replayCassette( assistantMessages: cassette.assistantMessages, durationMs: Date.now() - started, providerError: null, + nativeFailure: null, }; return { cassette, diff --git a/evals/reviewer-run.ts b/evals/reviewer-run.ts index d0a88845..49587ca6 100644 --- a/evals/reviewer-run.ts +++ b/evals/reviewer-run.ts @@ -350,8 +350,10 @@ async function main(): Promise { false, () => activeHost.outcome([sessionId], Date.now() - started), ); - if (outcome.providerError) { - runFailure = outcome.providerError; + const observedFailure = + outcome.providerError ?? outcome.nativeFailure; + if (observedFailure) { + runFailure = observedFailure; throw new EvaluationPhaseError(runFailure, runFailure); } const submission = await evaluationPhase( diff --git a/evals/run.ts b/evals/run.ts index 751b0265..dd5558cb 100644 --- a/evals/run.ts +++ b/evals/run.ts @@ -1224,11 +1224,17 @@ export async function runCampaign( observedOutcome = outcome; // An interrupted attempt is not a measured product result. Previously // committed attempts remain in the stopped campaign's report. - if (!stepFailure && !outcome.providerError) signal.throwIfAborted(); + if ( + !stepFailure && + !outcome.providerError && + !outcome.nativeFailure + ) + signal.throwIfAborted(); const cell = campaignCells[job.slot]; if (!cell?.managerModel) throw new Error(`Missing v2 campaign cell for slot ${job.slot}.`); - const observedFailure = outcome.providerError; + const observedFailure = + outcome.providerError ?? outcome.nativeFailure; // Asking the user is the designed end of some scenarios, but only at the // wall. `askedScoring` holds the rule and its reasoning. const { escalated, unscored } = askedScoring( @@ -1359,7 +1365,10 @@ export async function runCampaign( finalText: outcome.finalText, questions: askedQuestions(outcome), durationMs: outcome.durationMs, - hostError: outcome.providerError?.detail ?? null, + hostError: + outcome.providerError?.detail ?? + outcome.nativeFailure?.code ?? + null, provenance: { artifact, evaluator, @@ -1624,7 +1633,10 @@ export async function runCampaign( ? bestEffortEvaluation(() => askedQuestions(outcome), []) : [], durationMs: outcome?.durationMs ?? durationMs, - hostError: outcome?.providerError?.detail ?? null, + hostError: + outcome?.providerError?.detail ?? + outcome?.nativeFailure?.code ?? + null, provenance: { artifact, evaluator, diff --git a/evals/study-runner.ts b/evals/study-runner.ts index 265c6704..3dbec145 100644 --- a/evals/study-runner.ts +++ b/evals/study-runner.ts @@ -562,7 +562,7 @@ export async function executeStudy( ? {} : { availability: outcome.usageAvailability }), }); - failure ??= outcome.providerError; + failure ??= outcome.providerError ?? outcome.nativeFailure; if (failure && usage.costUsd === 0) usage = normalizeStudyUsage({ tokens: usage.tokens, diff --git a/src/platform/opencode/execution-attribution.ts b/src/platform/opencode/execution-attribution.ts index 0541e78d..656ad7a7 100644 --- a/src/platform/opencode/execution-attribution.ts +++ b/src/platform/opencode/execution-attribution.ts @@ -340,7 +340,7 @@ export class ExecutionAttribution { } }, async cancel(reason) { - fail(); + finish(); await reader.cancel(reason); }, }), diff --git a/tests/eval-cancellation.test.ts b/tests/eval-cancellation.test.ts index 924ccc43..6fb7369b 100644 --- a/tests/eval-cancellation.test.ts +++ b/tests/eval-cancellation.test.ts @@ -257,6 +257,11 @@ describe("real runner cancellation reports without provider spend", () => { code: "provider-rejected-turn", }); if (mode.startsWith("provider-poll-")) { + expect( + result.events.filter( + (name) => name === "actual-command-boundary", + ), + ).toHaveLength(1); expect( result.events.filter((name) => name === "actual-progress-poll"), ).toHaveLength(1); diff --git a/tests/eval-native-terminal.test.ts b/tests/eval-native-terminal.test.ts new file mode 100644 index 00000000..415f419e --- /dev/null +++ b/tests/eval-native-terminal.test.ts @@ -0,0 +1,1133 @@ +import { expect, spyOn, test } from "bun:test"; +import { mkdtemp, rm } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import type { SessionMessageInfo } from "@opencode/client"; +import { CampaignCancelled } from "../evals/campaign-stop.js"; +import { deriveConformanceOutcome } from "../evals/conformance-evidence.js"; +import { isEligibleEnvironmentFailure } from "../evals/environment-reserves.js"; +import { + EvaluationPhaseError, + evaluationPhase, + nativeExecutionFailure, +} from "../evals/failure-origin.js"; +import { + retainedFailureEvidence, + scenarioGradeInput, +} from "../evals/grader-input.js"; +import { askedScoring } from "../evals/harness.js"; +import { + collectHostTrace, + type HostTraceCollection, + HostTraceSchema, + nativeExecution, +} from "../evals/host-trace.js"; +import { projectEvalMessage } from "../evals/opencode-host.js"; +import { SCENARIOS } from "../evals/scenarios.js"; +import { prepareStudyManifest } from "../evals/study-manifest.js"; +import { executeStudy } from "../evals/study-runner.js"; +import { + fixtureModel, + messagePage, + nativeAssistant, + nativeClient, + nativeHost, + nativeSession, + nativeTool, + sessionPage, + sessionReply, + useNativeFixtureBudget, +} from "./fixtures/native-eval-client.js"; +import captured from "./fixtures/native-terminal-contexts.json" with { + type: "json", +}; +import { + fakeStudyTransport, + studyCase, + studyManifest, +} from "./study-support.js"; + +useNativeFixtureBudget(); +const idle = ( + outcome: "succeeded" | "failed" | "interrupted", + id = "msg_idle", + created = 4, + error?: { type: string; message: string }, +): SessionMessageInfo => ({ + id, + type: "idle", + time: { created }, + outcome, + ...(error ? { error } : {}), +}); +function collection( + messages: unknown[], + sessionId = "ses_root", +): HostTraceCollection { + return { + runnerRootSessionIds: [sessionId], + directory: "/workspace", + childrenComplete: true, + sessionMetadata: [nativeSession(sessionId, "/workspace")], + sessionMessages: [{ sessionId, messages }], + }; +} +function observed(messages: unknown[], sessionId = "ses_root") { + const trace = collectHostTrace(collection(messages, sessionId)); + if (trace.version !== 2 || trace.kind !== "observed") + throw new Error(JSON.stringify(trace)); + return trace; +} + +for (const entry of captured) + test(`literal Responses ${entry.mode} retains independent native terminal facts`, () => { + const trace = observed(entry.messages, entry.sessionId); + const assistant = trace.messages.find( + (message) => message.role === "assistant", + ); + if (assistant?.role !== "assistant") + throw new Error("Missing captured assistant"); + expect(assistant.terminal).toEqual({ + finish: { kind: "observed", value: "tool-calls" }, + error: { kind: "absent" }, + streamed: { + kind: "observed", + value: entry.mode === "normal-close" ? 1792855872683 : 1792855872876, + }, + completed: { + kind: "observed", + value: entry.mode === "normal-close" ? 1792855872690 : 1792855872876, + }, + }); + expect(assistant.tools[0]?.status).toBe("completed"); + const result = nativeExecutionFailure(nativeExecution(trace)); + expect(result?.code ?? null).toBe( + entry.mode === "normal-close" ? null : "native-execution-failed", + ); + for (const message of entry.messages.filter( + (message) => message.type === "idle", + )) { + if (message.outcome !== "succeeded" && message.outcome !== "failed") + throw new Error("Invalid captured outcome"); + expect( + projectEvalMessage( + idle( + message.outcome, + message.id, + message.time.created, + entry.mode === "normal-close" + ? undefined + : { + type: "unknown", + message: + "Flow context has an unknown, synthetic, or stale real input.", + }, + ), + entry.sessionId, + ).info.error, + ).toBeUndefined(); + } + }); + +test("assistant finish, error and times neither imply nor require each other", () => { + const trace = observed([ + nativeAssistant("msg_assistant", { + finish: "error", + time: { created: 1, streamed: 2 }, + }), + ]); + const assistant = trace.messages[0]; + if (assistant?.role !== "assistant") throw new Error("Missing assistant"); + expect(assistant.terminal).toEqual({ + finish: { kind: "observed", value: "error" }, + error: { kind: "absent" }, + streamed: { kind: "observed", value: 2 }, + completed: { kind: "absent" }, + }); + expect(nativeExecutionFailure(nativeExecution(trace))).toBeNull(); + for (const time of [ + { created: 2, streamed: 1 }, + { created: 1, streamed: 3, completed: 2 }, + ]) + expect( + collectHostTrace( + collection([nativeAssistant("msg_assistant", { time })]), + ), + ).toEqual({ + version: 2, + kind: "unavailable", + reason: "chronology-conflict", + }); +}); + +test("native end reader is idempotent, command-scoped, and preserves recovered history", () => { + const trace = observed([ + idle("failed", "msg_old", 1), + { id: "msg_user", type: "user", text: "Next", time: { created: 2 } }, + idle("succeeded", "msg_new", 3), + ]); + const execution = nativeExecution(trace, [ + { sessionId: "ses_root", afterMessageId: "msg_old" }, + ]); + expect(execution).toEqual({ + kind: "observed", + sessions: [ + { + sessionId: "ses_root", + rootSessionId: "ses_root", + end: { + kind: "succeeded", + messageId: "msg_new", + created: 3, + error: { kind: "absent" }, + }, + }, + ], + }); + expect( + nativeExecution(trace, [ + { sessionId: "ses_root", afterMessageId: "msg_old" }, + ]), + ).toEqual(execution); + expect(nativeExecutionFailure(execution)).toBeNull(); + for (const boundary of ["msg_new", "msg_missing"]) + expect( + nativeExecutionFailure( + nativeExecution(trace, [ + { sessionId: "ses_root", afterMessageId: boundary }, + ]), + ), + ).toBeNull(); + expect( + trace.messages.filter( + (message) => message.role === "event" && message.type === "idle", + ), + ).toHaveLength(2); +}); + +test("only a strictly newer attributable interrupted idle is a self interruption", () => { + for (const outcome of ["succeeded", "failed", "interrupted"] as const) + for (const abortAt of [3, 4, 5]) { + const execution = nativeExecution(observed([idle(outcome)]), [ + { + sessionId: "ses_root", + afterMessageId: null, + abort: { requestedAt: abortAt, priorFailure: false }, + }, + ]); + const failure = nativeExecutionFailure(execution); + expect(failure?.code ?? null).toBe( + outcome === "succeeded" || (outcome === "interrupted" && abortAt === 3) + ? null + : `native-execution-${outcome}`, + ); + } + for (const error of [ + { type: "api", message: "Independent failure" }, + { type: "aborted", message: "Stopped" }, + ]) { + const trace = observed([idle("interrupted", "msg_idle", 4, error)]); + expect( + nativeExecutionFailure( + nativeExecution(trace, [ + { + sessionId: "ses_root", + afterMessageId: null, + abort: { requestedAt: 3, priorFailure: false }, + }, + ]), + )?.code ?? null, + ).toBe(error.type === "aborted" ? null : "native-execution-interrupted"); + expect( + nativeExecutionFailure( + nativeExecution(trace, [ + { + sessionId: "ses_root", + afterMessageId: null, + abort: { requestedAt: 3, priorFailure: true }, + }, + ]), + )?.code, + ).toBe("native-execution-interrupted"); + } +}); + +test("diagnostics keep a finite scrubbed allowlist and withhold residual unsafe text", () => { + const body = "private-body-content"; + const trace = observed([ + { + ...idle("failed"), + error: { + type: "unknown", + message: `Bearer ${"s".repeat(40)}`, + response: { body }, + headers: { authorization: "private-header" }, + stack: "private-stack", + status: 429, + retryAfterMs: 1000, + }, + }, + ]); + const message = trace.messages[0]; + if (message?.role !== "event" || message.type !== "idle") + throw new Error("Missing idle"); + expect(message.execution).toEqual({ + outcome: "failed", + error: { + kind: "observed", + value: { + type: "unknown", + message: "[redacted]", + status: 429, + retryAfterMs: 1000, + }, + }, + }); + for (const value of [body, "private-header", "private-stack", "s".repeat(40)]) + expect(JSON.stringify(trace)).not.toContain(value); + for (const text of [ + "/Users/private/account/config", + "C:\\Users\\private\\config", + "https://someone:credential@example.invalid/path?secret=opaque", + "opaque".repeat(10), + ]) { + const withheld = observed([ + { ...idle("failed"), error: { type: "unknown", message: text } }, + ]); + expect(JSON.stringify(withheld)).not.toContain(text); + expect(nativeExecutionFailure(nativeExecution(withheld))?.code).toBe( + "native-execution-failed", + ); + expect(JSON.stringify(withheld)).toContain('"reason":"unsafe-text"'); + } + for (const error of [ + { type: "unknown", message: "x".repeat(16385) }, + { type: "unknown", message: "é ".repeat(683) }, + { type: "unknown", message: "Safe", retryAfterMs: 86_400_001 }, + ]) { + const bounded = observed([{ ...idle("failed"), error }]); + expect(JSON.stringify(bounded)).toContain('"kind":"withheld"'); + expect(nativeExecutionFailure(nativeExecution(bounded))?.retryable).toBe( + false, + ); + } + expect( + collectHostTrace( + collection([ + { + ...idle("failed"), + error: { type: "unknown", message: "Safe", status: 600 }, + }, + ]), + ), + ).toEqual({ + version: 2, + kind: "unavailable", + reason: "malformed-native-message", + }); +}); + +test("diagnostic byte, count and aggregate bounds preserve outcomes and UTF8", () => { + const messages = Array.from({ length: 4096 }, (_, index) => ({ + ...idle("failed", `msg_idle_${index}`, index), + error: { type: "unknown", message: "é ".repeat(682) }, + })); + const trace = observed(messages); + const errors = trace.messages.flatMap((message) => + message.role === "event" && + message.type === "idle" && + message.execution?.error.kind === "observed" + ? [message.execution.error.value.message] + : [], + ); + expect(errors.length).toBe(63); + for (const message of errors) { + expect(Buffer.byteLength(message)).toBeLessThanOrEqual(2048); + expect(message).not.toContain("�"); + } + expect(nativeExecutionFailure(nativeExecution(trace))?.code).toBe( + "native-execution-failed", + ); + expect(HostTraceSchema.safeParse(trace).success).toBe(true); + const forged = structuredClone(trace); + for (const message of forged.messages) + if ( + message.role === "event" && + message.type === "idle" && + message.execution + ) + message.execution.error = { + kind: "observed", + value: { + type: "unknown", + message: "Safe", + status: null, + retryAfterMs: null, + }, + }; + expect(HostTraceSchema.safeParse(forged).success).toBe(false); +}); + +test("old V2 fields stay absent and strict new fields do not accept extra data", () => { + const trace = observed([ + nativeAssistant("msg_assistant", { time: { created: 1, completed: 2 } }), + idle("failed"), + ]); + const historical = structuredClone(trace); + for (const message of historical.messages) { + if (message.role === "assistant") delete message.terminal; + if (message.role === "event" && message.type === "idle") + delete message.execution; + } + const bytes = JSON.stringify(historical); + expect(JSON.stringify(HostTraceSchema.parse(historical))).toBe(bytes); + expect(nativeExecutionFailure(nativeExecution(historical))).toBeNull(); + expect(nativeExecution(historical)).toEqual({ + kind: "observed", + sessions: [ + { + sessionId: "ses_root", + rootSessionId: "ses_root", + end: { kind: "unobserved", reason: "terminal-fields-not-retained" }, + }, + ], + }); + const illegal = JSON.parse(JSON.stringify(trace)); + illegal.messages[1].execution.headers = {}; + expect(HostTraceSchema.safeParse(illegal).success).toBe(false); + illegal.messages[1] = { ...trace.messages[1], type: "skill" }; + expect(HostTraceSchema.safeParse(illegal).success).toBe(false); +}); + +type FixtureOptions = { + messages: (poll: number) => SessionMessageInfo[]; + prior?: SessionMessageInfo[]; + child?: "failed" | "running" | "read-failure"; + pendingPost?: boolean; + cancel?: "before" | "after"; + outcomeOnly?: boolean; + question?: true; +}; +async function observeWait(options: FixtureOptions) { + const project = await mkdtemp(join(tmpdir(), "terminal-wait-")); + let now = 0; + let poll = 0; + let dispatched = options.outcomeOnly ?? false; + let aborts = 0; + let readsAfterAbort = 0; + const controller = new AbortController(); + const reason = new CampaignCancelled(130); + const root = nativeSession("ses_root", project); + const child = nativeSession("ses_child", project, { + parentID: "ses_root", + agent: "flow-reviewer", + }); + const client = nativeClient(async ({ url, init }) => { + const path = url.pathname; + if (path.endsWith("/model")) return new Response(null, { status: 204 }); + if (path.endsWith("/command") || path.endsWith("/prompt")) { + dispatched = true; + if (options.pendingPost) + return new Promise((_, reject) => + init.signal?.addEventListener( + "abort", + () => reject(init.signal?.reason), + { once: true }, + ), + ); + return new Response(null, { status: 204 }); + } + if (path.endsWith("/interrupt")) { + aborts++; + return Response.json({ interrupted: true }); + } + if (aborts) readsAfterAbort++; + if (path === "/api/session/ses_root") return sessionReply(root); + if (path === "/api/session") + return sessionPage(options.child ? [root, child] : [root]); + if (path === "/api/session/ses_root/message") + return messagePage( + dispatched ? options.messages(poll++) : (options.prior ?? []), + ); + if (path === "/api/session/ses_child/message") { + if (options.child === "read-failure") + throw new Error("Fixture child read failed"); + return messagePage( + options.child === "failed" + ? [idle("failed", "msg_child_idle")] + : [ + nativeAssistant("msg_child", { + content: [ + nativeTool("call_read", "read", { + status: "running", + input: {}, + metadata: {}, + }), + ], + }), + ], + ); + } + throw new Error("Unexpected fixture endpoint"); + }); + const clock = spyOn(Date, "now").mockImplementation(() => now); + const sleep = spyOn(Bun, "sleep").mockImplementation(async () => { + now += 2000; + if (options.cancel === "before" || (options.cancel === "after" && poll > 0)) + controller.abort(reason); + }); + const host = nativeHost(project, client, controller.signal); + try { + let result: unknown = "outcome-only"; + if (!options.outcomeOnly) + result = await evaluationPhase("host", "command-aborted", true, () => + host.runCommand("ses_root", "flow-plan", "fixture", fixtureModel, { + quietMs: 0, + timeoutMs: 8000, + stalledMs: 4000, + }), + ).catch((error: unknown) => error); + const outcome = await host.outcome( + ["ses_root"], + now, + result === reason ? undefined : controller.signal, + ); + return { + result, + outcome, + aborts, + now, + reason, + readsAfterAbort, + question: options.question ? host.escalationQuestion("ses_root") : null, + }; + } finally { + sleep.mockRestore(); + clock.mockRestore(); + await rm(project, { recursive: true, force: true }); + } +} + +for (const outcome of ["succeeded", "failed", "interrupted"] as const) + test(`live ${outcome} closes independently from provider errors`, async () => { + const result = await observeWait({ messages: () => [idle(outcome)] }); + if (outcome === "succeeded") expect(result.result).toBe("quiet"); + else { + expect(result.result).toBeInstanceOf(EvaluationPhaseError); + if (!(result.result instanceof EvaluationPhaseError)) throw result.result; + expect(result.result.failure).toEqual({ + origin: "host", + code: `native-execution-${outcome}`, + detail: `Native idle outcome ${outcome} in session ses_root, message msg_idle. Native idle error was absent.`, + retryable: false, + }); + expect( + isEligibleEnvironmentFailure({ + outcome: { kind: "failure", ...result.result.failure }, + }), + ).toBe(false); + } + expect(result.outcome.providerError).toBeNull(); + expect(result.outcome.nativeFailure?.code ?? null).toBe( + outcome === "succeeded" ? null : `native-execution-${outcome}`, + ); + expect(result.readsAfterAbort).toBe(0); + }); + +for (const kind of [ + "assistant", + "tool", + "compaction", + "child", + "post", +] as const) + test(`a failed idle with busy ${kind} is preserved at the existing stop boundary`, async () => { + const busy: SessionMessageInfo = + kind === "compaction" + ? { + id: "msg_compaction", + type: "compaction", + time: { created: 1 }, + status: "running", + reason: "auto", + summary: "", + } + : nativeAssistant("msg_assistant", { + time: { + created: 1, + ...(kind === "assistant" ? {} : { completed: 3 }), + }, + content: + kind === "tool" + ? [ + nativeTool("call_read", "read", { + status: "running", + input: {}, + metadata: {}, + }), + ] + : [], + }); + const result = await observeWait({ + messages: () => [busy, idle("failed")], + ...(kind === "child" ? { child: "running" as const } : {}), + pendingPost: kind === "post", + }); + expect(result.now).toBeGreaterThanOrEqual(6000); + expect(result.result).toBeInstanceOf(EvaluationPhaseError); + if (!(result.result instanceof EvaluationPhaseError)) throw result.result; + expect(result.result.failure.code).toBe("native-execution-failed"); + expect(result.result.failure.retryable).toBe(false); + expect(result.outcome.nativeFailure?.code).toBe("native-execution-failed"); + }); + +test("child-read failure keeps the fetched root failure and an unavailable full tree", async () => { + const result = await observeWait({ + messages: () => [idle("failed")], + child: "read-failure", + }); + expect(result.result).toBeInstanceOf(EvaluationPhaseError); + expect(result.outcome.nativeFailure?.code).toBe("native-execution-failed"); + expect(result.outcome.hostTrace).toEqual({ + version: 2, + kind: "unavailable", + reason: "endpoint-failure", + }); + expect(result.readsAfterAbort).toBe(0); +}); + +test("outcome-only read keeps native failure when subsequent child enrichment fails", async () => { + const result = await observeWait({ + messages: () => [idle("failed")], + child: "read-failure", + outcomeOnly: true, + }); + expect(result.outcome.nativeFailure?.code).toBe("native-execution-failed"); + expect(result.outcome.hostTrace).toEqual({ + version: 2, + kind: "unavailable", + reason: "endpoint-failure", + }); + expect(result.outcome.providerError).toBeNull(); +}); + +test("root success with a failed child still reaches product missing-plan grading", async () => { + const result = await observeWait({ + messages: () => [idle("succeeded")], + child: "failed", + }); + expect(result.result).toBe("quiet"); + expect(result.outcome.nativeFailure).toBeNull(); + const trace = result.outcome.hostTrace; + if (trace?.version !== 2 || trace.kind !== "observed") + throw new Error("Missing owned trace"); + expect(nativeExecution(trace)).toEqual({ + kind: "observed", + sessions: [ + { + sessionId: "ses_child", + rootSessionId: "ses_root", + end: { + kind: "failed", + messageId: "msg_child_idle", + created: 4, + error: { kind: "absent" }, + }, + }, + { + sessionId: "ses_root", + rootSessionId: "ses_root", + end: { + kind: "succeeded", + messageId: "msg_idle", + created: 4, + error: { kind: "absent" }, + }, + }, + ], + }); + const scenario = SCENARIOS.find( + (candidate) => candidate.id === "plan-only-stops", + ); + if (!scenario) throw new Error("Missing scenario"); + expect(scenario.check(result.outcome)).toContain( + "no session document was written; planning did not persist a plan", + ); +}); + +test("previous command idle and missing idle keep the existing quiet policy", async () => { + for (const messages of [[], [idle("failed", "msg_prior")]]) { + const result = await observeWait({ + prior: messages, + messages: () => messages, + }); + expect(result.result).toBe("quiet"); + expect(result.outcome.nativeFailure).toBeNull(); + } +}); + +test("a native retry can supersede failed idle before the wait commits to stop", async () => { + const result = await observeWait({ + messages: (poll) => + poll === 0 + ? [nativeAssistant("msg_assistant"), idle("failed")] + : [ + nativeAssistant("msg_assistant", { + time: { created: 1, completed: 2 }, + }), + idle("failed"), + idle("succeeded", "msg_recovered", 5), + ], + }); + expect(result.result).toBe("quiet"); + expect(result.aborts).toBe(0); + expect(result.outcome.nativeFailure).toBeNull(); +}); + +test("cancellation preserves an already-fetched native failure but invents none before observation", async () => { + const failed = await observeWait({ + messages: () => [nativeAssistant("msg_assistant"), idle("failed")], + cancel: "after", + }); + expect(failed.result).toBeInstanceOf(EvaluationPhaseError); + expect(failed.outcome.nativeFailure?.code).toBe("native-execution-failed"); + const cancelled = await observeWait({ messages: () => [], cancel: "before" }); + expect(cancelled.result).toBe(cancelled.reason); + expect(cancelled.outcome.nativeFailure).toBeNull(); +}); + +test("a fresh command after runner abort observes a new interruption", async () => { + const project = await mkdtemp(join(tmpdir(), "terminal-next-command-")); + let now = 0; + let command = 0; + let prior: SessionMessageInfo[] = []; + let current: SessionMessageInfo[] = []; + let switched = false; + const client = nativeClient(({ url }) => { + if (url.pathname.endsWith("/model")) { + switched = true; + return new Response(null, { status: 204 }); + } + if (url.pathname.endsWith("/command") || url.pathname.endsWith("/prompt")) { + command++; + current = [ + ...prior, + idle( + command === 1 ? "failed" : "interrupted", + `msg_command_${command}`, + now + 1, + ), + ]; + return url.pathname.endsWith("/prompt") + ? Response.json({ data: {} }) + : new Response(null, { status: 204 }); + } + if (url.pathname.endsWith("/interrupt")) { + prior = current; + return Response.json({ interrupted: true }); + } + if (url.pathname === "/api/session") return sessionPage([]); + if (url.pathname === "/api/session/ses_root") + return sessionReply(nativeSession("ses_root", project)); + if (url.pathname.endsWith("/message")) { + expect(switched).toBe(true); + return messagePage(current); + } + throw new Error("Unexpected command fixture endpoint"); + }); + const clock = spyOn(Date, "now").mockImplementation(() => now); + const sleep = spyOn(Bun, "sleep").mockImplementation(async () => { + now += 2000; + }); + const host = nativeHost(project, client); + try { + const first = await host + .runCommand("ses_root", "flow-plan", "fixture", fixtureModel, { + quietMs: 0, + }) + .catch((error: unknown) => error); + expect(first).toBeInstanceOf(EvaluationPhaseError); + const second = await host + .runPrompt("ses_root", "fixture", fixtureModel, { quietMs: 0 }) + .catch((error: unknown) => error); + expect(second).toBeInstanceOf(EvaluationPhaseError); + if (!(second instanceof EvaluationPhaseError)) throw second; + expect(second.failure.code).toBe("native-execution-interrupted"); + expect((await host.outcome(["ses_root"], now)).nativeFailure?.code).toBe( + "native-execution-interrupted", + ); + } finally { + sleep.mockRestore(); + clock.mockRestore(); + await rm(project, { recursive: true, force: true }); + } +}); + +test("an explicit provider error stays primary ahead of its native failed idle", async () => { + const result = await observeWait({ + messages: () => [ + nativeAssistant("msg_provider", { + error: { type: "api", message: "Earlier provider failure" }, + time: { created: 1, completed: 2 }, + }), + idle("failed"), + ], + }); + if (!(result.result instanceof EvaluationPhaseError)) + throw new Error("Missing provider failure"); + expect(result.result.failure).toEqual({ + origin: "provider", + code: "provider-rejected-turn", + detail: '{"type":"api","message":"Earlier provider failure"}', + retryable: true, + }); + expect(result.outcome.providerError?.code).toBe("provider-rejected-turn"); + expect(result.outcome.nativeFailure).toBeNull(); +}); + +test("the diagnostic count budget is independent of byte limits", () => { + const trace = observed( + Array.from({ length: 65 }, (_, index) => ({ + ...idle("failed", `msg_idle_${index}`, index), + error: { type: "unknown", message: "Safe" }, + })), + ); + const errors = trace.messages.flatMap((message) => + message.role === "event" && message.type === "idle" + ? [message.execution?.error] + : [], + ); + expect(errors.filter((error) => error?.kind === "observed")).toHaveLength(64); + expect(errors.at(-1)).toEqual({ + kind: "withheld", + reason: "limits-exceeded", + }); +}); + +test("study runner retains outcome-only native failure without activating a reserve", async () => { + const directory = await mkdtemp(join(tmpdir(), "native-study-failure-")); + try { + const failure = nativeExecutionFailure( + nativeExecution(observed([idle("failed")])), + ); + if (!failure) throw new Error("Missing fixture native failure"); + const fake = fakeStudyTransport({ nativeFailure: failure }); + const result = await executeStudy( + await prepareStudyManifest(studyManifest(), directory), + { + directory, + benchmarks: [studyCase], + transport: fake.transport, + }, + ); + expect(result.report.attempts).toHaveLength(2); + expect(result.report.attempts[0]?.outcome).toEqual({ + kind: "failure", + origin: "host", + code: "native-execution-failed", + retryable: false, + }); + expect(result.report.completion.activatedReserveCellIds).toEqual([]); + } finally { + await rm(directory, { recursive: true, force: true }); + } +}); + +test("withheld assistant diagnostics preserve the other three native facts", () => { + const trace = observed([ + nativeAssistant("msg_assistant", { + finish: "tool-calls", + time: { created: 1, streamed: 2, completed: 3 }, + error: { type: "unknown", message: "é ".repeat(683) }, + }), + ]); + const assistant = trace.messages[0]; + if (assistant?.role !== "assistant") throw new Error("Missing assistant"); + expect(assistant.terminal).toEqual({ + finish: { kind: "observed", value: "tool-calls" }, + error: { kind: "withheld", reason: "limits-exceeded" }, + streamed: { kind: "observed", value: 2 }, + completed: { kind: "observed", value: 3 }, + }); +}); + +function unansweredQuestion(error?: { + type: string; + message: string; +}): SessionMessageInfo { + return nativeAssistant("msg_question", { + time: { created: 1, completed: 3 }, + ...(error ? { error } : {}), + content: [ + nativeTool( + "call_question", + "question", + { + status: "running", + input: { + questions: [ + { + question: "Continue?", + header: "Plan", + options: [{ label: "Yes", description: "Continue the plan" }], + }, + ], + }, + metadata: {}, + }, + { created: 2 }, + ), + ], + }); +} + +for (const outcome of ["failed", "interrupted", "succeeded"] as const) + test(`question escalation stays primary beside ${outcome} idle`, async () => { + const result = await observeWait({ + question: true, + messages: () => [unansweredQuestion(), idle(outcome)], + }); + expect(result.now).toBe(2000); + expect(result.aborts).toBe(1); + expect(result.result).toBe("escalated"); + expect(result.question).toEqual({ + sessionId: "ses_root", + calls: [ + { + messageId: "msg_question", + callId: "call_question", + input: { + questions: [ + { + question: "Continue?", + header: "Plan", + options: [{ label: "Yes", description: "Continue the plan" }], + }, + ], + }, + }, + ], + }); + expect(result.outcome.providerError).toBeNull(); + expect(result.outcome.nativeFailure).toBeNull(); + const trace = result.outcome.hostTrace; + if (trace?.version !== 2 || trace.kind !== "observed") + throw new Error("Missing question native trace"); + expect( + trace.messages.find( + (message) => message.role === "event" && message.type === "idle", + ), + ).toMatchObject({ execution: { outcome, error: { kind: "absent" } } }); + expect(nativeExecutionFailure(nativeExecution(trace))?.code ?? null).toBe( + outcome === "succeeded" ? null : `native-execution-${outcome}`, + ); + }); + +test("allowed question escalation reaches the public runner's product grading", async () => { + const result = await observeWait({ + question: true, + messages: () => [unansweredQuestion(), idle("failed")], + }); + const scenario = SCENARIOS.find((entry) => entry.id === "plan-only-stops"); + if (!scenario) throw new Error("Missing allowed escalation scenario"); + expect(result.result).toBe("escalated"); + expect(askedScoring([0], 1, scenario.mayEscalate === true)).toEqual({ + escalated: true, + unscored: false, + }); + expect( + result.outcome.providerError ?? result.outcome.nativeFailure, + ).toBeNull(); + const evidence = retainedFailureEvidence({ + attempt: { + attemptId: "question-attempt", + cellId: "question-cell", + caseId: scenario.id, + repetition: 0, + model: { + routeProvider: "fixture", + model: "model", + family: "fixture", + gateway: null, + revision: null, + }, + }, + durationMs: result.now, + gradeInput: scenarioGradeInput(result.outcome), + }); + const product = deriveConformanceOutcome({ + evidence, + check: scenario.check, + scenarioId: scenario.id, + model: fixtureModel, + attempt: 1, + }); + expect(product).toMatchObject({ + kind: "product", + passed: false, + endedBy: "user-escalation", + issues: [ + "no session document was written; planning did not persist a plan", + ], + }); +}); + +for (const next of ["command", "prompt"] as const) + test(`question stop is not inherited by the exact next ${next}`, async () => { + const project = await mkdtemp(join(tmpdir(), "question-next-command-")); + let now = 0; + let dispatches = 0; + let messages: SessionMessageInfo[] = []; + const root = nativeSession("ses_root", project); + const client = nativeClient(({ url }) => { + const path = url.pathname; + if (path.endsWith("/model")) return new Response(null, { status: 204 }); + if (path.endsWith("/command") || path.endsWith("/prompt")) { + dispatches++; + messages = + dispatches === 1 + ? [unansweredQuestion(), idle("failed")] + : [...messages, idle("failed", "msg_next_failure", now + 2)]; + return path.endsWith("/prompt") + ? Response.json({ data: {} }) + : new Response(null, { status: 204 }); + } + if (path.endsWith("/interrupt")) { + messages = messages.map((message) => { + if (message.type !== "assistant") return message; + return { + ...message, + content: message.content.map((part) => + part.type === "tool" && part.name === "question" + ? nativeTool( + part.id, + part.name, + { + status: "error", + input: { questions: [{ question: "Continue?" }] }, + error: { + type: "aborted", + message: "Question interrupted", + }, + }, + { created: 2, completed: now }, + ) + : part, + ), + }; + }); + return Response.json({ interrupted: true }); + } + if (path === "/api/session") return sessionPage([root]); + if (path === "/api/session/ses_root") return sessionReply(root); + if (path.endsWith("/message")) return messagePage(messages); + throw new Error("Unexpected question sequence endpoint"); + }); + const clock = spyOn(Date, "now").mockImplementation(() => now); + const sleep = spyOn(Bun, "sleep").mockImplementation(async () => { + now += 2000; + }); + const host = nativeHost(project, client); + try { + expect( + await host.runCommand( + "ses_root", + "flow-plan", + "fixture", + fixtureModel, + { quietMs: 0 }, + ), + ).toBe("escalated"); + expect((await host.outcome(["ses_root"], now)).nativeFailure).toBeNull(); + expect(host.escalationQuestion("ses_root").calls[0]?.callId).toBe( + "call_question", + ); + const second = await (next === "command" + ? host.runCommand("ses_root", "flow-plan", "fixture", fixtureModel, { + quietMs: 0, + }) + : host.runPrompt("ses_root", "fixture", fixtureModel, { quietMs: 0 }) + ).catch((error: unknown) => error); + if (!(second instanceof EvaluationPhaseError)) + throw new Error("New native failure was suppressed"); + expect(second.failure.code).toBe("native-execution-failed"); + expect(second.failure.retryable).toBe(false); + expect( + isEligibleEnvironmentFailure({ + outcome: { kind: "failure", ...second.failure }, + }), + ).toBe(false); + expect((await host.outcome(["ses_root"], now)).nativeFailure?.code).toBe( + "native-execution-failed", + ); + expect(dispatches).toBe(2); + } finally { + sleep.mockRestore(); + clock.mockRestore(); + await rm(project, { recursive: true, force: true }); + } + }); + +test("question-shaped work does not outrank an earlier provider failure", async () => { + const result = await observeWait({ + messages: () => [ + unansweredQuestion({ type: "api", message: "Earlier provider failure" }), + idle("failed"), + ], + }); + if (!(result.result instanceof EvaluationPhaseError)) + throw new Error("Missing earlier provider failure"); + expect(result.result.failure.origin).toBe("provider"); + expect(result.result.failure.code).toBe("provider-rejected-turn"); + expect(result.outcome.providerError?.code).toBe("provider-rejected-turn"); + expect(result.outcome.nativeFailure).toBeNull(); +}); + +test("study runner grades an allowed question stop instead of recording native failure", async () => { + const question = await observeWait({ + question: true, + messages: () => [unansweredQuestion(), idle("failed")], + }); + const end = question.result; + if (end !== "escalated") + throw new Error("Question did not select escalation"); + const directory = await mkdtemp(join(tmpdir(), "question-study-grade-")); + const fake = fakeStudyTransport(); + try { + const result = await executeStudy( + await prepareStudyManifest(studyManifest(), directory), + { + directory, + benchmarks: [studyCase], + transport: { + prepareCache: fake.transport.prepareCache, + startHost: async (input) => { + const host = await fake.transport.startHost(input); + return { + ...host, + runPrompt: async (...args) => { + await host.runPrompt(...args); + return end; + }, + outcome: async (...args) => ({ + ...(await host.outcome(...args)), + providerError: question.outcome.providerError, + nativeFailure: question.outcome.nativeFailure, + }), + }; + }, + }, + }, + ); + expect(result.report.attempts).toHaveLength(2); + expect( + result.report.attempts.map((attempt) => attempt.outcome), + ).toMatchObject([ + { kind: "product", passed: true, endedBy: "user-escalation" }, + { kind: "product", passed: true, endedBy: "user-escalation" }, + ]); + expect(result.report.completion.activatedReserveCellIds).toEqual([]); + } finally { + await rm(directory, { recursive: true, force: true }); + } +}); diff --git a/tests/eval-progress-wait.test.ts b/tests/eval-progress-wait.test.ts index f4b66e16..713c8042 100644 --- a/tests/eval-progress-wait.test.ts +++ b/tests/eval-progress-wait.test.ts @@ -377,7 +377,7 @@ for (const mode of ["duplicate-children", "cyclic-children"] as const) { ).length, ).toBe( observed.visits.filter((path) => path === "/api/session/ses_root/message") - .length, + .length - 1, ); }); } diff --git a/tests/eval-reporting.test.ts b/tests/eval-reporting.test.ts index 821fffd4..40f449d5 100644 --- a/tests/eval-reporting.test.ts +++ b/tests/eval-reporting.test.ts @@ -1122,7 +1122,16 @@ describe("eval campaign cancellation", () => { abortSignal = init?.signal; return new Promise(() => {}); } - if (String(input).endsWith("/command")) controller.abort(reason); + const path = new URL(String(input)).pathname; + if (path.endsWith("/message")) + return Response.json({ data: [], cursor: {} }); + if (path === "/api/session/id") + return Response.json({ + id: "id", + agent: "build", + location: { directory: scratch }, + }); + if (path.endsWith("/command")) controller.abort(reason); return new Response(null, { status: 204 }); }); try { diff --git a/tests/eval-scenario-checks.test.ts b/tests/eval-scenario-checks.test.ts index 8338bf77..6ced5f5b 100644 --- a/tests/eval-scenario-checks.test.ts +++ b/tests/eval-scenario-checks.test.ts @@ -23,6 +23,7 @@ function outcome(overrides: Partial): Outcome { assistantMessages: 0, durationMs: 0, providerError: null, + nativeFailure: null, ...overrides, }; } diff --git a/tests/eval-v2-trace-contract.test.ts b/tests/eval-v2-trace-contract.test.ts index 6fbc1468..ef17f116 100644 --- a/tests/eval-v2-trace-contract.test.ts +++ b/tests/eval-v2-trace-contract.test.ts @@ -207,6 +207,12 @@ test("V2 preserves native message kinds, endpoint ownership and timestamps witho type: "assistant", agent: "build", contentCount: 2, + terminal: { + finish: { kind: "absent" }, + error: { kind: "absent" }, + streamed: { kind: "absent" }, + completed: { kind: "absent" }, + }, tools: [ { version: 2, diff --git a/tests/execution-attribution.test.ts b/tests/execution-attribution.test.ts index 52e86bc2..72d8d69a 100644 --- a/tests/execution-attribution.test.ts +++ b/tests/execution-attribution.test.ts @@ -383,7 +383,7 @@ function failedStreamFixture() { } test.each([ - "cancel", + "steer-then-cancel", "read-error", "no-body", "locked-body", @@ -403,7 +403,7 @@ test.each([ }, }), ) - : failure === "cancel" + : failure === "steer-then-cancel" ? new Response(new ReadableStream()) : new Response("body"); const input = { @@ -432,12 +432,24 @@ test.each([ ); else { if (!input.response.body) throw new Error("Missing native body."); - await input.response.body.cancel("real steer"); + f.steer(); + expect(() => + f.attribution.verify( + execution, + [user, assistant], + session, + "/tmp", + "shell", + ), + ).toThrow("revoked stream origin"); + await input.response.body.cancel("consumer cleanup"); } } const verifyFresh = f.fresh(); expect(verifyFresh()).toBe("user-2"); - expect(f.failures).toEqual(["session-test"]); + expect(f.failures).toEqual( + failure === "steer-then-cancel" ? [] : ["session-test"], + ); }, ); @@ -456,16 +468,233 @@ test("a late old-stream cancellation failure cannot end or revoke a newer primar }; f.attribution.response(input, f.fail); if (!input.response.body) throw new Error("Missing wrapped body."); - const pending = input.response.body.cancel("steer"); + const pending = input.response.body.cancel("consumer cleanup"); + f.steer(); + expect(() => + f.attribution.verify( + execution, + [user, assistant], + session, + "/tmp", + "shell", + ), + ).toThrow("revoked stream origin"); const verifyFresh = f.fresh(); cancelled.reject(new Error("late native cancel failure")); await expect(pending).rejects.toThrow("late native cancel failure"); expect(verifyFresh()).toBe("user-2"); - expect(f.failures).toEqual(["session-test"]); + expect(f.failures).toEqual([]); const late = { ...input, response: new Response("late duplicate response") }; expect(() => f.attribution.response(late, f.fail)).toThrow( "Request identity", ); expect(verifyFresh()).toBe("user-2"); - expect(f.failures).toEqual(["session-test"]); + expect(f.failures).toEqual([]); +}); + +test("consumer cancellation ends its captured resource without revoking the exact unbound tool origin", async () => { + const f = fixture(); + let failures = 0; + const input = { + sessionID: sid, + agent, + model, + kind: "primary" as const, + request: f.request, + response: new Response(new ReadableStream()), + }; + f.attribution.response(input, () => { + failures++; + f.revoke(); + }); + if (!input.response.body) throw new Error("Missing wrapped body."); + await input.response.body.cancel("protocol consumer stopped"); + expect(f.primary.phase).toBe("ended"); + expect(f.primary.origin).toBe(origin); + expect( + f.attribution.verify(execution, [user, assistant], session, "/tmp", "shell") + .primary.origin.id, + ).toBe("user-1"); + expect(failures).toBe(0); + f.attribution.freeze(context, [user, assistant], origin); + const nextRequest = new Request("http://localhost/next-same-origin"); + f.attribution.request({ + sessionID: sid, + agent, + model, + kind: "primary", + request: nextRequest, + }); + const nextID = SessionMessage.ID.make("msg_second"); + const nextAssistant: SessionMessageInfo = { ...assistant, id: nextID }; + expect( + f.attribution.verify( + { ...execution, messageID: nextID }, + [user, assistant, nextAssistant], + session, + "/tmp", + "shell", + ).primary.origin, + ).toBe(origin); + expect(f.attribution.current(sid)?.request).toBe(nextRequest); + expect(failures).toBe(0); +}); + +test("consumer cancellation awaits cleanup rejection without declaring an epoch failure", async () => { + const f = fixture(); + const cleanup = Promise.withResolvers(); + let failures = 0; + const input = { + sessionID: sid, + agent, + model, + kind: "primary" as const, + request: f.request, + response: new Response( + new ReadableStream({ cancel: () => cleanup.promise }), + ), + }; + f.attribution.response(input, () => { + failures++; + f.revoke(); + }); + if (!input.response.body) throw new Error("Missing wrapped body."); + let settled = false; + const cancel = input.response.body.cancel("consumer end").finally(() => { + settled = true; + }); + await Promise.resolve(); + expect(settled).toBe(false); + expect(f.primary.phase).toBe("ended"); + expect( + f.attribution.verify(execution, [user, assistant], session, "/tmp", "shell") + .primary.origin.id, + ).toBe("user-1"); + cleanup.reject(new Error("native cancel cleanup failed")); + await expect(cancel).rejects.toThrow("native cancel cleanup failed"); + expect(failures).toBe(0); + expect( + f.attribution.verify(execution, [user, assistant], session, "/tmp", "shell") + .primary.origin.id, + ).toBe("user-1"); +}); +test("a late captured EOF cannot retire a separately steered fresh primary", async () => { + const f = failedStreamFixture(); + const body = + Promise.withResolvers>(); + const input = { + sessionID: sid, + agent, + model, + kind: "primary" as const, + request: f.request, + response: new Response( + new ReadableStream({ + start: (controller) => body.resolve(controller), + }), + ), + }; + f.attribution.response(input, f.fail); + const text = input.response.text(); + const old = f.attribution.current(sid); + if (!old) throw new Error("Missing old primary"); + f.steer(); + f.attribution.end(old); + const verifyFresh = f.fresh(); + const fresh = f.attribution.current(sid); + expect(verifyFresh()).toBe("user-2"); + (await body.promise).close(); + expect(await text).toBe(""); + expect(f.attribution.current(sid)).toBe(fresh); + expect(verifyFresh()).toBe("user-2"); + expect(f.failures).toEqual([]); +}); +for (const kind of ["compaction", "title", "generate"] as const) + test(`${kind} HTTP resource cannot end or prove the captured primary`, async () => { + const f = fixture(); + const response = new Response("auxiliary"); + const input = { + sessionID: sid, + agent, + model, + kind, + request: new Request("http://localhost/auxiliary"), + response, + }; + let failures = 0; + f.attribution.request(input); + f.attribution.response(input, () => { + failures++; + f.revoke(); + }); + expect(input.response).toBe(response); + expect(await input.response.text()).toBe("auxiliary"); + expect(f.primary.phase).toBe("request"); + expect(f.attribution.requestOwner(input.request)).toBeUndefined(); + expect(failures).toBe(0); + const primary = { + ...input, + kind: "primary" as const, + request: f.request, + response: new Response(new ReadableStream()), + }; + f.attribution.response(primary, () => { + failures++; + f.revoke(); + }); + if (!primary.response.body) throw new Error("Missing primary body"); + await primary.response.body.cancel(); + expect( + f.attribution.verify( + execution, + [user, assistant], + session, + "/tmp", + "shell", + ).primary.origin.id, + ).toBe("user-1"); + expect(failures).toBe(0); + }); +test("resource end grants no same-origin retry and cannot revive an explicitly revoked origin", async () => { + const f = fixture(); + const input = { + sessionID: sid, + agent, + model, + kind: "primary" as const, + request: f.request, + response: new Response(new ReadableStream()), + }; + let failures = 0; + f.attribution.response(input, () => { + failures++; + f.revoke(); + }); + if (!input.response.body) throw new Error("Missing body"); + await input.response.body.cancel(); + const retry: SessionMessageInfo = { + ...assistant, + id: "msg_retry", + content: [], + retry: { + attempt: 2, + at: 2, + error: { type: "provider.transport", message: "Refused" }, + }, + }; + expect(f.attribution.retry(f.primary, [user, retry])).toBe(false); + f.revoke(); + expect(() => + f.attribution.verify( + execution, + [user, assistant], + session, + "/tmp", + "shell", + ), + ).toThrow("revoked epoch"); + expect(() => + f.attribution.freeze(context, [user, assistant], origin), + ).toThrow("revoked epoch"); + expect(failures).toBe(0); }); diff --git a/tests/fixtures/eval-abort-fallback-child.ts b/tests/fixtures/eval-abort-fallback-child.ts index 476e2bd7..56dbd199 100644 --- a/tests/fixtures/eval-abort-fallback-child.ts +++ b/tests/fixtures/eval-abort-fallback-child.ts @@ -167,6 +167,7 @@ class FixtureHost { assistantMessages: 1, durationMs: now - startedAt, providerError: null, + nativeFailure: null, }; } async stop() {} diff --git a/tests/fixtures/eval-cancellation-child.ts b/tests/fixtures/eval-cancellation-child.ts index fd37f21b..553be024 100644 --- a/tests/fixtures/eval-cancellation-child.ts +++ b/tests/fixtures/eval-cancellation-child.ts @@ -144,6 +144,7 @@ function plannedOutcome(sessionId: string): Outcome { assistantMessages: 1, durationMs: 1, providerError: null, + nativeFailure: null, }; } @@ -215,6 +216,7 @@ class FakeEvalHost { nativeClient: nativeClient(), }); const originalFetch = globalThis.fetch; + let dispatched = false; this.restoreFetch = () => { globalThis.fetch = originalFetch; }; @@ -223,12 +225,23 @@ class FakeEvalHost { const url = new URL(String(input)); if (url.origin !== "http://fixture") throw new Error("Unexpected fixture request"); - if ( - url.pathname.endsWith("/command") || - url.pathname.endsWith("/model") - ) + if (url.pathname.endsWith("/command")) { + dispatched = true; + return new Response(null, { status: 204 }); + } + if (url.pathname.endsWith("/model")) return new Response(null, { status: 204 }); + if (url.pathname === `/api/session/${sessionId}` && !dispatched) + return Response.json({ + id: sessionId, + agent: "build", + location: { directory: this.project }, + }); if (url.pathname.endsWith("/message")) { + if (!dispatched) { + event("actual-command-boundary"); + return Response.json({ data: [], cursor: {} }); + } event("actual-progress-poll"); return Response.json({ data: [ diff --git a/tests/fixtures/eval-reserve-cancellation-child.ts b/tests/fixtures/eval-reserve-cancellation-child.ts index b1a79b56..62444ae6 100644 --- a/tests/fixtures/eval-reserve-cancellation-child.ts +++ b/tests/fixtures/eval-reserve-cancellation-child.ts @@ -151,6 +151,7 @@ class FakeReleaseHost { costUsd: 0, assistantMessages: 1, durationMs: 1, + nativeFailure: null, providerError: observation ? providerFailure(observation.message) : null, providerErrorObservation: observation, }; diff --git a/tests/fixtures/native-terminal-contexts.json b/tests/fixtures/native-terminal-contexts.json new file mode 100644 index 00000000..6a271886 --- /dev/null +++ b/tests/fixtures/native-terminal-contexts.json @@ -0,0 +1,151 @@ +[ + { + "mode": "normal-close", + "sessionId": "ses_e91766c78ffewdLyYkNsy5bITL", + "messages": [ + { + "id": "msg_16e89945e001jZ2CkI1PhMZa5u", + "type": "user", + "time": { + "created": 1792855872615 + }, + "text": "Fixture request" + }, + { + "id": "msg_16e899491001FlhwdCxSS3silb", + "type": "assistant", + "time": { + "created": 1792855872663, + "streamed": 1792855872683, + "completed": 1792855872690 + }, + "agent": "build", + "model": { + "id": "gpt-6.1-sol", + "providerID": "responses-local", + "variant": "default" + }, + "finish": "tool-calls", + "content": [ + { + "type": "tool", + "id": "call_responses_status", + "name": "flow_status", + "time": { + "created": 1792855872679, + "ran": 1792855872681, + "completed": 1792855872689 + }, + "state": { + "status": "completed", + "input": { + "request": { + "view": "compact" + } + }, + "content": [ + { + "type": "text", + "text": "{}" + } + ] + } + } + ] + }, + { + "id": "msg_16e8994b7001JVcsiqZtybzJIj", + "type": "assistant", + "time": { + "created": 1792855872700, + "streamed": 1792855872706, + "completed": 1792855872706 + }, + "agent": "build", + "model": { + "id": "gpt-6.1-sol", + "providerID": "responses-local", + "variant": "default" + }, + "finish": "stop", + "content": [] + }, + { + "id": "msg_16e8994c3001NgFoPH3kOc3oB0", + "type": "idle", + "time": { + "created": 1792855872707 + }, + "outcome": "succeeded" + } + ] + }, + { + "mode": "completed-tool-before-terminal", + "sessionId": "ses_e91766ab1ffenjZzLh218mYdkW", + "messages": [ + { + "id": "msg_16e899551001EDDNUfbuAVMaP9", + "type": "user", + "time": { + "created": 1792855872855 + }, + "text": "Fixture request" + }, + { + "id": "msg_16e899559001RYdY0aw1qC8Uic", + "type": "assistant", + "time": { + "created": 1792855872861, + "streamed": 1792855872876, + "completed": 1792855872876 + }, + "agent": "build", + "model": { + "id": "gpt-6.1-sol", + "providerID": "responses-local", + "variant": "default" + }, + "finish": "tool-calls", + "content": [ + { + "type": "tool", + "id": "call_responses_status", + "name": "flow_status", + "time": { + "created": 1792855872865, + "ran": 1792855872866, + "completed": 1792855872869 + }, + "state": { + "status": "completed", + "input": { + "request": { + "view": "compact" + } + }, + "content": [ + { + "type": "text", + "text": "{}" + } + ] + } + } + ] + }, + { + "id": "msg_16e899574001hADN9XI1LunggS", + "type": "idle", + "time": { + "created": 1792855872884 + }, + "outcome": "failed", + "error": { + "type": "unknown", + "message": "Flow context has an unknown, synthetic, or stale real input." + } + } + ] + } +] diff --git a/tests/lifecycle.test.ts b/tests/lifecycle.test.ts index 78902e22..cb99232f 100644 --- a/tests/lifecycle.test.ts +++ b/tests/lifecycle.test.ts @@ -3449,7 +3449,28 @@ test("interrupted ordinary manual summaries retain lineage without an execution if (!frame.response.body) throw new Error("Missing response body"); const reader = frame.response.body.getReader(); await reader.read(); - await reader.cancel(); + await reader.cancel("consumer cleanup"); + f.native.push( + { + type: "assistant", + id: "msg_interrupted_stream", + agent, + model, + content: [], + finish: "error", + error: { type: "aborted", message: "Step interrupted" }, + time: { created: 2, streamed: 3, completed: 4 }, + }, + { + type: "idle", + id: "msg_interrupted_idle", + outcome: "interrupted", + time: { created: 4 }, + }, + ); + f.sendEvent(terminal("interrupted")); + await f.flush(); + expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); for (const id of ["msg_interrupted_summary", "msg_repeated_summary"]) { const input = f.context(); const control = { ...automaticControl(id), reason: "manual" as const }; @@ -3578,3 +3599,73 @@ test("a source accepted while context waits owns a later native-read failure", a await f.lifecycle.dispose(); } }); + +for (const outcome of ["failed", "interrupted"] as const) + test(`observed owned ${outcome} idle revokes after resource-only cancellation and preserves fresh work`, async () => { + const f = await fixture(); + try { + await f.command("flow-auto", "Inspect the original exact source"); + const request = await startOwnedRequest(f); + const call = ownershipNativeCall(f); + const before = f.autoDrive.captureAuthority(sessionID); + expect(before).not.toBeNull(); + const response = { + sessionID, + agent, + model, + kind: "primary" as const, + request, + response: new Response(new ReadableStream()), + }; + await f.emit("http.response", response); + if (!response.response.body) throw new Error("Missing body"); + await response.response.body.cancel("consumer end"); + expect(f.autoDrive.captureAuthority(sessionID)).toEqual(before); + const message = f.native.find((message) => message.id === call.messageID); + if (message?.type !== "assistant") + throw new Error("Missing captured assistant"); + message.content = []; + message.finish = "error"; + message.error = { + type: outcome === "interrupted" ? "aborted" : "provider.transport", + message: + outcome === "interrupted" ? "Step interrupted" : "ECONNREFUSED", + }; + message.time.completed = 5; + f.native.push({ + type: "idle", + id: "msg_owned_terminal", + outcome, + time: { created: 6 }, + }); + const event = terminal(outcome); + f.sendEvent(event); + await f.flush(); + expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); + let oldEffects = 0; + await expect( + (async () => { + await f.lifecycle.beforeTool(call); + oldEffects++; + })(), + ).rejects.toThrow("epoch was revoked"); + expect(oldEffects).toBe(0); + expect(f.repository.saveCount).toBe(0); + await f.command("flow-auto", "Inspect separately accepted fresh source"); + const fresh = f.autoDrive.captureAuthority(sessionID); + expect(fresh).not.toBeNull(); + await finishOwnedResponse(f, await startOwnedRequest(f)); + const freshCall = ownershipNativeCall(f); + await f.lifecycle.beforeTool(freshCall); + f.sendEvent(event); + await f.flush(); + await expect(f.lifecycle.beforeTool(call)).rejects.toThrow(); + await f.lifecycle.beforeTool(freshCall); + expect(f.autoDrive.captureAuthority(sessionID)).toEqual(fresh); + expect(f.autoDrive.captureAuthority(sessionID)).not.toBeNull(); + expect(oldEffects).toBe(0); + expect(f.repository.saveCount).toBe(0); + } finally { + await f.lifecycle.dispose(); + } + }); diff --git a/tests/provenance.test.ts b/tests/provenance.test.ts index 577ff5d3..ce1a3009 100644 --- a/tests/provenance.test.ts +++ b/tests/provenance.test.ts @@ -119,10 +119,16 @@ describe("eval provenance", () => { ]) as EvalHost; Reflect.set(host, "nativeClient", { session: { + get: async () => ({ + id: "session", + agent: "build", + location: { directory: "/unused" }, + }), switchModel: capture("switchModel"), command: capture("command"), prompt: capture("prompt"), }, + message: { list: async () => ({ data: [], cursor: {} }) }, }); Object.assign(host, { waitForQuiet: async ( diff --git a/tests/qualification-cli.test.ts b/tests/qualification-cli.test.ts index 0eb73139..e9c4c013 100644 --- a/tests/qualification-cli.test.ts +++ b/tests/qualification-cli.test.ts @@ -291,6 +291,7 @@ function inspectionAuditOutcome(): Outcome { assistantMessages: 0, durationMs: 0, providerError: null, + nativeFailure: null, }; } @@ -366,6 +367,7 @@ test("qualifies and seals a historical 9.5 exact-artifact fixture campaign throu assistantMessages: 1, durationMs: 1, providerError: null, + nativeFailure: null, }; expect(scenario.check(observed), scenario.id).toEqual([]); replayedByScenario.set(scenario.id, observed); diff --git a/tests/study-support.ts b/tests/study-support.ts index cd31dbe0..1ce42076 100644 --- a/tests/study-support.ts +++ b/tests/study-support.ts @@ -131,6 +131,7 @@ export async function studyArtifact( export function fakeStudyTransport( options: { failStart?: number; + nativeFailure?: Outcome["nativeFailure"]; badProduct?: number; usage?: Outcome["tokens"]; costUsd?: number | null; @@ -227,6 +228,7 @@ export function fakeStudyTransport( }, costUsd: options.costUsd === undefined ? 0.25 : options.costUsd, providerError: null, + nativeFailure: options.nativeFailure ?? null, ...(options.usageAvailability === undefined ? {} : { usageAvailability: options.usageAvailability }), From fc5495b0c23fe1f7db794a6862a38b1e221744de Mon Sep 17 00:00:00 2001 From: Douwe de Vries Date: Sat, 24 Oct 2026 21:15:58 +0200 Subject: [PATCH 03/17] test(validation): cover native session-relative workdirs --- tests/validation-capture.test.ts | 176 ++++++++++++++++++++++++++----- 1 file changed, 148 insertions(+), 28 deletions(-) diff --git a/tests/validation-capture.test.ts b/tests/validation-capture.test.ts index de997677..b27062c7 100644 --- a/tests/validation-capture.test.ts +++ b/tests/validation-capture.test.ts @@ -2,7 +2,7 @@ import { afterAll, expect, test } from "bun:test"; import { createHash } from "node:crypto"; import { mkdir, mkdtemp, rm, symlink, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; -import { join } from "node:path"; +import { basename, isAbsolute, join, relative, resolve } from "node:path"; import type { ObservedValidation, PreparedValidation, @@ -26,8 +26,26 @@ import { ValidationCaptureCoordinator, } from "../src/platform/opencode/validation-capture.js"; -const workspace = await mkdtemp(join(tmpdir(), "flow-native-validation-")); -afterAll(() => rm(workspace, { recursive: true, force: true })); +const temporaryRoot = await mkdtemp(join(tmpdir(), "flow-native-validation-")); +const workspace = join( + temporaryRoot, + ...Array(process.cwd().split(/[\\/]/).length).fill("workspace"), +); +await mkdir(workspace, { recursive: true }); +afterAll(() => rm(temporaryRoot, { recursive: true, force: true })); +await mkdir(join(workspace, "workdir-child")); +if (process.platform !== "win32") { + await symlink(tmpdir(), join(workspace, "workdir-outside"), "dir"); + for (const name of [" ", "\n", "native-alias ", "native-alias\n"]) + await symlink(tmpdir(), join(workspace, name), "dir"); + for (const name of [ + "native-alias", + " ", + "workspace-alias\n", + "workspace-alias ", + ]) + await symlink(workspace, join(workspace, name), "dir"); +} const SOURCE: SourceDigest = `sha256:${"a".repeat(64)}`; const prepared: PreparedValidation = { featureId: "runtime", @@ -113,32 +131,134 @@ function digest(text: string): `sha256:${string}` { return `sha256:${createHash("sha256").update(text).digest("hex")}`; } -test("persists the exact source-bound native shell observation and appends a receipt", async () => { - const { capture, observations } = fixture(); - capture.arm("ses_host", workspace, prepared); - await capture.observeToolBefore(before()); - const completed = after(); - const result = await capture.observeToolAfter(completed); - expect(observations).toEqual([ - { - ...prepared, - captureId: "capture-1", - exitCode: 0, - outputComplete: true, - outputDigest: digest("passed"), - }, - ]); - const observation = observations[0]; - if (!observation) throw new Error("Observation was not persisted."); - expect(result).toEqual(persisted(observation)); - expect(completed.status === "completed" && completed.result.content).toEqual([ - { - type: "text", - text: 'passed\n\n[flow-validation] {"id":"capture-1","scope":"focused","intent":"pass","passed":true,"observed":false,"recordedRevision":4}', +test.each([ + ["omitted", undefined, undefined], + ["empty", "", ""], + ["absolute", workspace, workspace], + ["dot before", ".", workspace], + ["dot after", workspace, "."], + ["dot in both hooks", ".", "."], + ["relative before", `../${basename(workspace)}/.`, workspace], + ["relative after", workspace, `../${basename(workspace)}/.`], + [ + "relative in both hooks", + `../${basename(workspace)}/.`, + `../${basename(workspace)}/.`, + ], + ...(process.platform === "win32" + ? [] + : ([ + ["spaces alias before", " ", workspace], + ["spaces alias after", workspace, " "], + ["newline alias before", "workspace-alias\n", workspace], + ["newline alias after", workspace, "workspace-alias\n"], + ["trailing-space alias before", "workspace-alias ", workspace], + ["trailing-space alias after", workspace, "workspace-alias "], + ] as const)), +] as const)( + "%s workdir preserves exact source-bound evidence and receipt", + async (_name, beforeWorkdir, afterWorkdir) => { + const { capture, observations } = fixture(); + expect(resolve(workspace)).not.toBe(process.cwd()); + capture.arm("ses_host", workspace, prepared); + await capture.observeToolBefore( + before({ + input: { + command: prepared.command, + ...(beforeWorkdir === undefined ? {} : { workdir: beforeWorkdir }), + }, + }), + ); + const completed = after( + undefined, + {}, + { + input: { + command: prepared.command, + ...(afterWorkdir === undefined ? {} : { workdir: afterWorkdir }), + }, + }, + ); + const result = await capture.observeToolAfter(completed); + expect(observations).toEqual([ + { + ...prepared, + captureId: "capture-1", + exitCode: 0, + outputComplete: true, + outputDigest: digest("passed"), + }, + ]); + const observation = observations[0]; + if (!observation) throw new Error("Observation was not persisted."); + expect(result).toEqual(persisted(observation)); + expect( + completed.status === "completed" && completed.result.content, + ).toEqual([ + { + type: "text", + text: 'passed\n\n[flow-validation] {"id":"capture-1","scope":"focused","intent":"pass","passed":true,"observed":false,"recordedRevision":4}', + }, + ]); + expect(capture.pendingCount()).toBe(0); + }, +); +const processCwdWorkdir = relative(process.cwd(), workspace); +for (const phase of ["before", "after"] as const) { + test.each([ + ...(isAbsolute(processCwdWorkdir) + ? [] + : ([["process-cwd-relative", processCwdWorkdir]] as const)), + ["escaping", ".."], + ["subdirectory", "./workdir-child"], + ["absolute outside", tmpdir()], + ...(process.platform === "win32" + ? [] + : ([ + ["symlink-outside", "./workdir-outside"], + ["space-only outside", " "], + ["dot-space outside", "./ "], + ["newline-only outside", "\n"], + ["relative trailing-space outside", "./native-alias "], + ["absolute trailing-space outside", join(workspace, "native-alias ")], + ["relative newline outside", "./native-alias\n"], + ["absolute space-only outside", join(workspace, " ")], + ["absolute newline outside", join(workspace, "\n")], + ["padded absolute", ` ${workspace} `], + ["nonexistent whitespace name", "./missing "], + ] as const)), + ] as const)( + `${phase} rejects %s workdir without a receipt`, + async (name, workdir) => { + const { capture, observations } = fixture(); + if (name === "process-cwd-relative") { + expect(resolve(workdir)).toBe(resolve(workspace)); + expect(resolve(workspace, workdir)).not.toBe(resolve(workspace)); + } + capture.arm("ses_host", workspace, prepared); + const input = { command: prepared.command, workdir }; + const completed = after(undefined, {}, { input }); + if (phase === "before") { + await expect( + capture.observeToolBefore(before({ input })), + ).rejects.toThrow("capture was cancelled"); + expect(await capture.observeToolAfter(completed)).toBeNull(); + } else { + await capture.observeToolBefore( + before({ + input: { command: prepared.command, workdir: workspace }, + }), + ); + await expect(capture.observeToolAfter(completed)).rejects.toThrow(); + } + expect(observations).toEqual([]); + expect(capture.pendingCount()).toBe(0); + expect( + completed.status === "completed" && completed.result.content, + ).toEqual([{ type: "text", text: "passed" }]); }, - ]); - expect(capture.pendingCount()).toBe(0); -}); + ); +} test.each([ [ "nonzero-completed", From 1027988c8f9561857d47b12ac5a3e28c1ce67f7a Mon Sep 17 00:00:00 2001 From: Douwe de Vries Date: Sat, 24 Oct 2026 21:16:01 +0200 Subject: [PATCH 04/17] fix(validation): compare exact session-relative shell directories --- src/platform/opencode/validation-capture.ts | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/platform/opencode/validation-capture.ts b/src/platform/opencode/validation-capture.ts index e23b8b63..82aeb9bf 100644 --- a/src/platform/opencode/validation-capture.ts +++ b/src/platform/opencode/validation-capture.ts @@ -1,8 +1,8 @@ import { createHash, randomUUID } from "node:crypto"; -import { constants } from "node:fs"; +import { constants, realpathSync } from "node:fs"; import { open, realpath } from "node:fs/promises"; import { homedir } from "node:os"; -import { basename, dirname, isAbsolute, join } from "node:path"; +import { basename, dirname, isAbsolute, join, resolve } from "node:path"; import { z } from "zod"; import type { ObservedValidation, @@ -420,8 +420,9 @@ export class ValidationCaptureCoordinator { ) || assertMutableWorkspaceRoot(identity.location.directory) !== capture.workspace || - (args.data.workdir && - assertMutableWorkspaceRoot(args.data.workdir) !== capture.workspace) + realpathSync( + resolve(identity.location.directory, args.data.workdir ?? ""), + ) !== capture.workspace ) throw new Error( "The shell identity, workspace, or command did not match the armed validation.", @@ -469,8 +470,9 @@ export class ValidationCaptureCoordinator { identity.parentID !== undefined || assertMutableWorkspaceRoot(identity.location.directory) !== capture.workspace || - (args.data.workdir && - assertMutableWorkspaceRoot(args.data.workdir) !== capture.workspace) + realpathSync( + resolve(identity.location.directory, args.data.workdir ?? ""), + ) !== capture.workspace ) throw new ValidationCaptureError( "The executed shell command or workspace changed after Flow armed it.", From f2782cd33c6303b01d1249a052e10f19ffa408f4 Mon Sep 17 00:00:00 2001 From: Douwe de Vries Date: Sat, 24 Oct 2026 21:52:33 +0200 Subject: [PATCH 05/17] test(opencode): expose retained request snapshot lifetimes --- tests/execution-attribution.test.ts | 73 +++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/tests/execution-attribution.test.ts b/tests/execution-attribution.test.ts index 72d8d69a..4c3e1d5c 100644 --- a/tests/execution-attribution.test.ts +++ b/tests/execution-attribution.test.ts @@ -698,3 +698,76 @@ test("resource end grants no same-origin retry and cannot revive an explicitly r ).toThrow("revoked epoch"); expect(failures).toBe(0); }); + +function retainedSnapshot(primary: { + previousAssistants?: ReadonlySet; + lifetime?: { + kind: string; + snapshot?: { previousAssistants: ReadonlySet }; + }; +}) { + return primary.lifetime?.kind === "current" + ? primary.lifetime.snapshot + : primary.previousAssistants + ? { previousAssistants: primary.previousAssistants } + : undefined; +} + +test("64 held owners retain one current payload with 63 historical assistant IDs", () => { + const attribution = new ExecutionAttribution(() => {}); + const native: SessionMessageInfo[] = [user]; + const owners: ReturnType[] = []; + const requests: Request[] = []; + for (let index = 0; index < 64; index++) { + const primary = attribution.freeze(context, native, origin); + const request = new Request("http://localhost/retention"); + attribution.request({ + sessionID: sid, + agent, + model, + kind: "primary", + request, + }); + const message = { ...assistant, id: `msg_retention_${index}` }; + native.push(message); + const call = { + ...execution, + messageID: SessionMessage.ID.make(message.id), + }; + expect( + attribution.verify(call, native, session, "/tmp", "shell").primary, + ).toBe(primary); + attribution.end(primary); + owners.push(primary); + requests.push(request); + } + const payloads = owners.flatMap((owner) => retainedSnapshot(owner) ?? []); + expect(payloads.length).toBe(1); + expect( + payloads.reduce( + (total, payload) => total + payload.previousAssistants.size, + 0, + ), + ).toBe(63); + for (const [index, owner] of owners.entries()) { + const request = requests[index]; + if (!request) throw new Error("Missing retained request."); + expect(attribution.requestOwner(request)).toBe(owner); + expect( + attribution.executionOwner({ + ...execution, + messageID: SessionMessage.ID.make(`msg_retention_${index}`), + }), + ).toBe(owner); + } + +}); + +test("clear retires evidence even when callers hold owners and Request keys", () => { + const f = fixture(); + f.attribution.verify(execution, [user, assistant], session, "/tmp", "shell"); + f.attribution.clear(); + expect(retainedSnapshot(f.primary)).toBeUndefined(); + expect(f.attribution.requestOwner(f.request)).toBe(f.primary); + expect(() => f.attribution.freeze(context, [user], origin)).toThrow("closed"); +}); From 22b4230c6fe57fefac1f7b5c5fd2c446160411a8 Mon Sep 17 00:00:00 2001 From: Douwe de Vries Date: Sat, 24 Oct 2026 21:52:50 +0200 Subject: [PATCH 06/17] fix(opencode): release superseded execution snapshots Keep stable callback owners and immutable replay identities while retiring their transcript payloads. Release attribution state on native session deletion without changing HTTP completion or stale-owner cleanup. --- .../opencode/execution-attribution.ts | 239 ++++++++------ src/platform/opencode/lifecycle.ts | 5 +- tests/execution-attribution.test.ts | 293 +++++++++++++++++- tests/lifecycle.test.ts | 289 ++++++++++++++++- 4 files changed, 723 insertions(+), 103 deletions(-) diff --git a/src/platform/opencode/execution-attribution.ts b/src/platform/opencode/execution-attribution.ts index 656ad7a7..e25a6690 100644 --- a/src/platform/opencode/execution-attribution.ts +++ b/src/platform/opencode/execution-attribution.ts @@ -25,18 +25,41 @@ export type InputOrigin = Readonly<{ | Readonly<{ automatic: false; authority: AutoDriveAuthority | null }> ); -type FrozenPrimary = { +type Snapshot = Readonly<{ + inputs: readonly string[]; + previousAssistants: ReadonlySet; + previousTerminals: ReadonlySet; + previousRetries: ReadonlySet; +}>; +type HttpResource = + | Readonly<{ phase: "context"; requestSeen: false }> + | Readonly<{ phase: "ended"; requestSeen: false }> + | Readonly<{ + phase: "request" | "streaming" | "ended"; + requestSeen: true; + }>; +type Lifetime = + | Readonly<{ kind: "current"; snapshot: Snapshot }> + | Readonly<{ kind: "retired" }>; +type PrimaryOwner = { readonly sessionID: string; readonly agent: string; readonly model: SessionContext["model"]; - readonly inputs: readonly string[]; - readonly previousAssistants: ReadonlySet; - readonly previousTerminals: ReadonlySet; - readonly previousRetries: ReadonlySet; readonly origin: InputOrigin; - phase: "context" | "request" | "streaming" | "ended"; - request: Request | null; + http: HttpResource; + lifetime: Lifetime; }; +type Binding = Readonly<{ primary: PrimaryOwner; tool: string }>; +type AssistantRecord = + | Readonly<{ kind: "bound"; primary: PrimaryOwner }> + | Readonly<{ kind: "historical" }>; +type SessionRecord = { + current: PrimaryOwner; + assistants: Map; + bindings: Map; +}; +const retired: Lifetime = Object.freeze({ kind: "retired" }); +const historical: AssistantRecord = Object.freeze({ kind: "historical" }); const same = isDeepStrictEqual; function retryIdentity( message: Extract, @@ -129,49 +152,57 @@ function canonical(path: string): string { } } export class ExecutionAttribution { - readonly #primary = new Map(); - readonly #requests = new WeakMap(); - readonly #retries = new WeakSet(); - readonly #assistants = new Map(); - readonly #bindings = new Map< - string, - Readonly<{ primary: FrozenPrimary; tool: string }> - >(); + readonly #sessions = new Map(); + readonly #requests = new WeakMap(); + readonly #retries = new WeakSet(); readonly #assertLive: (origin: InputOrigin) => void; #closed = false; constructor(assertLive: (origin: InputOrigin) => void) { this.#assertLive = assertLive; } - #key(session: string, id: string): string { - return JSON.stringify([session, id]); + #session(sessionID: string): SessionRecord | undefined { + const record = this.#sessions.get(sessionID); + if (record === null) throw new Error("Flow execution session was deleted."); + return record; } - requestOwner(request: Request): FrozenPrimary | undefined { + #evidence(primary: PrimaryOwner): Snapshot { + if ( + this.current(primary.sessionID) !== primary || + primary.lifetime.kind !== "current" + ) + throw new Error("Flow primary ownership was superseded."); + return primary.lifetime.snapshot; + } + requestOwner(request: Request): PrimaryOwner | undefined { return this.#requests.get(request); } - executionOwner(execution: NativeExecution): FrozenPrimary | undefined { - const binding = this.#bindings.get( - JSON.stringify([execution.sessionID, execution.messageID, execution.id]), + executionOwner(execution: NativeExecution): PrimaryOwner | undefined { + const record = this.#session(execution.sessionID); + if (!record) return undefined; + const binding = record.bindings.get( + JSON.stringify([execution.messageID, execution.id]), ); - const primary = - binding?.primary ?? - this.#assistants.get(this.#key(execution.sessionID, execution.messageID)); - if (primary) return primary; - const current = this.#primary.get(execution.sessionID); - return current?.previousAssistants.has(execution.messageID) + if (binding) return binding.primary; + const assistant = record.assistants.get(execution.messageID); + if (assistant?.kind === "bound") return assistant.primary; + if (assistant?.kind === "historical") return undefined; + const primary = record.current; + return this.#evidence(primary).previousAssistants.has(execution.messageID) ? undefined - : current; + : primary; } - current(sessionID: string): FrozenPrimary | undefined { - return this.#primary.get(sessionID); + current(sessionID: string): PrimaryOwner | undefined { + return this.#sessions.get(sessionID)?.current; } freeze( input: SessionContext, native: readonly SessionMessageInfo[], origin: InputOrigin, - ): FrozenPrimary { + ): PrimaryOwner { if (this.#closed) throw new Error("Flow lifecycle is closed."); - const prior = this.#primary.get(input.sessionID); - if (prior && prior.phase !== "ended") + const record = this.#session(input.sessionID); + const prior = record?.current; + if (prior && prior.http.phase !== "ended") throw new Error("Flow primary contexts or streams overlap."); this.#assertLive(origin); const inputs = modelInputIDs(input); @@ -184,26 +215,24 @@ export class ExecutionAttribution { throw new Error( "Flow admitted agent or model differs from its primary request.", ); + const previous = prior && this.#evidence(prior); const retry = prior && this.#retries.delete(prior) && prior.origin === origin && - same(prior.inputs, inputs) && + same(previous?.inputs, inputs) && prior.agent === input.agent && sameModel(prior.model, input.model); - const primary: FrozenPrimary = { - sessionID: input.sessionID, - agent: input.agent, - model: Object.freeze({ ...input.model }), + const snapshot: Snapshot = Object.freeze({ inputs: Object.freeze(inputs), - previousAssistants: retry - ? prior.previousAssistants - : new Set( - native - .filter((message) => message.type === "assistant") - .map((message) => message.id), - ), - origin, + previousAssistants: + retry && previous + ? previous.previousAssistants + : new Set( + native + .filter((message) => message.type === "assistant") + .map((message) => message.id), + ), previousRetries: new Set( native.flatMap((message) => message.type === "assistant" && message.retry !== undefined @@ -216,17 +245,32 @@ export class ExecutionAttribution { .filter((message) => message.type === "idle") .map((message) => message.id), ), - phase: "context", - request: null, + }); + const primary: PrimaryOwner = { + sessionID: input.sessionID, + agent: input.agent, + model: Object.freeze({ ...input.model }), + origin, + http: { phase: "context", requestSeen: false }, + lifetime: { kind: "current", snapshot }, + }; + const next = record ?? { + current: primary, + assistants: new Map(), + bindings: new Map(), }; - this.#primary.set(input.sessionID, primary); + if (prior) prior.lifetime = retired; + next.current = primary; + this.#sessions.set(input.sessionID, next); + for (const id of snapshot.previousAssistants) + if (!next.assistants.has(id)) next.assistants.set(id, historical); return primary; } request(input: SessionHttpRequest): void { if (input.kind !== "primary") return; - const primary = this.#primary.get(input.sessionID); + const primary = this.#session(input.sessionID)?.current; if ( - primary?.phase !== "context" || + primary?.http.phase !== "context" || primary.agent !== input.agent || !sameModel(primary.model, input.model) || this.#requests.has(input.request) @@ -235,30 +279,28 @@ export class ExecutionAttribution { "Flow primary HTTP request has no unique frozen context.", ); this.#assertLive(primary.origin); - primary.request = input.request; - primary.phase = "request"; + primary.http = { phase: "request", requestSeen: true }; this.#requests.set(input.request, primary); } hasFailedTerminal( - primary: FrozenPrimary, + primary: PrimaryOwner, native: readonly SessionMessageInfo[], ): boolean { + const snapshot = this.#evidence(primary); return native.some( (message, position) => message.type === "idle" && message.outcome !== "succeeded" && - !primary.previousTerminals.has(message.id) && - same(nativeInputIDs(native.slice(0, position)), primary.inputs), + !snapshot.previousTerminals.has(message.id) && + same(nativeInputIDs(native.slice(0, position)), snapshot.inputs), ); } - retry( - primary: FrozenPrimary, - native: readonly SessionMessageInfo[], - ): boolean { + retry(primary: PrimaryOwner, native: readonly SessionMessageInfo[]): boolean { + const snapshot = this.#evidence(primary); + const record = this.#session(primary.sessionID); if ( - this.#primary.get(primary.sessionID) !== primary || - primary.phase !== "request" || - !same(nativeInputIDs(native), primary.inputs) + primary.http.phase !== "request" || + !same(nativeInputIDs(native), snapshot.inputs) ) return false; const candidates = native.filter( @@ -271,38 +313,39 @@ export class ExecutionAttribution { message.content.length === 0 && message.agent === primary.agent && sameModel(message.model, primary.model) && - !primary.previousAssistants.has(message.id) && - !primary.previousRetries.has(retryIdentity(message)) && - !this.#assistants.has(this.#key(primary.sessionID, message.id)) && - same(nativeInputIDs(native.slice(0, position)), primary.inputs), + !snapshot.previousAssistants.has(message.id) && + !snapshot.previousRetries.has(retryIdentity(message)) && + !record?.assistants.has(message.id) && + same(nativeInputIDs(native.slice(0, position)), snapshot.inputs), ); if (candidates.length !== 1) return false; this.#retries.add(primary); this.end(primary); return true; } - end(primary: FrozenPrimary): boolean { - const active = primary.phase !== "ended"; - primary.phase = "ended"; - return active && this.#primary.get(primary.sessionID) === primary; + end(primary: PrimaryOwner): boolean { + const active = primary.http.phase !== "ended"; + primary.http = { phase: "ended", requestSeen: primary.http.requestSeen }; + return active && this.current(primary.sessionID) === primary; } response( input: SessionHttpResponse, failed: (sessionID: string) => void, ): void { if (input.kind !== "primary") return; + this.#session(input.sessionID); const primary = this.#requests.get(input.request); if ( !primary || - primary !== this.#primary.get(input.sessionID) || - primary.phase !== "request" || + primary !== this.current(input.sessionID) || + primary.http.phase !== "request" || primary.agent !== input.agent || !sameModel(primary.model, input.model) ) { if (primary) { if (this.end(primary)) failed(primary.sessionID); } else { - const current = this.#primary.get(input.sessionID); + const current = this.current(input.sessionID); if (current) this.end(current); failed(input.sessionID); } @@ -323,7 +366,7 @@ export class ExecutionAttribution { if (!input.response.body) throw new Error("Flow primary response has no successful stream."); const reader = input.response.body.getReader(); - primary.phase = "streaming"; + primary.http = { phase: "streaming", requestSeen: true }; const response = input.response; input.response = new Response( new ReadableStream({ @@ -361,7 +404,7 @@ export class ExecutionAttribution { session: SessionInfo, workspace: string, tool?: string, - ): Readonly<{ primary: FrozenPrimary; tool: string }> { + ): Readonly<{ primary: PrimaryOwner; tool: string }> { if ( this.#closed || session.id !== execution.sessionID || @@ -371,13 +414,15 @@ export class ExecutionAttribution { throw new Error( "Flow native execution session or workspace identity changed.", ); - const primary = this.#primary.get(execution.sessionID); + const record = this.#session(execution.sessionID); + const primary = record?.current; if ( - !primary?.request || - primary.phase === "context" || + !record || + !primary?.http.requestSeen || primary.agent !== execution.agent ) throw new Error("Flow tool has no attributable primary request."); + const snapshot = this.#evidence(primary); const position = native.findIndex( (message) => message.id === execution.messageID, ); @@ -386,8 +431,8 @@ export class ExecutionAttribution { assistant?.type !== "assistant" || assistant.agent !== execution.agent || !sameModel(assistant.model, primary.model) || - primary.previousAssistants.has(assistant.id) || - !same(nativeInputIDs(native.slice(0, position)), primary.inputs) + snapshot.previousAssistants.has(assistant.id) || + !same(nativeInputIDs(native.slice(0, position)), snapshot.inputs) ) throw new Error( "Flow exact assistant identity or frozen input prefix did not match.", @@ -401,31 +446,35 @@ export class ExecutionAttribution { (tool !== undefined && call.name !== tool) ) throw new Error("Flow exact native running tool call did not match."); - const assistantKey = this.#key(execution.sessionID, execution.messageID); - const prior = this.#assistants.get(assistantKey); - if (prior && prior !== primary) + const prior = record.assistants.get(execution.messageID); + if (prior && (prior.kind === "historical" || prior.primary !== primary)) throw new Error("Flow assistant cannot be rebound to another request."); - const key = JSON.stringify([ - execution.sessionID, - execution.messageID, - execution.id, - ]); - const binding = this.#bindings.get(key); + const key = JSON.stringify([execution.messageID, execution.id]); + const binding = record.bindings.get(key); if (binding && (binding.primary !== primary || binding.tool !== call.name)) throw new Error( "Flow execution identity conflicts with an immutable binding.", ); this.#assertLive(primary.origin); - this.#assistants.set(assistantKey, primary); + if (!prior) + record.assistants.set( + execution.messageID, + Object.freeze({ kind: "bound", primary }), + ); const verified = binding ?? Object.freeze({ primary, tool: call.name }); - this.#bindings.set(key, verified); + if (!binding) record.bindings.set(key, verified); return verified; } + deleteSession(sessionID: string): void { + const record = this.#sessions.get(sessionID); + if (record) record.current.lifetime = retired; + this.#sessions.set(sessionID, null); + } clear(): void { this.#closed = true; - this.#primary.clear(); - this.#assistants.clear(); - this.#bindings.clear(); + for (const record of this.#sessions.values()) + if (record) record.current.lifetime = retired; + this.#sessions.clear(); } } diff --git a/src/platform/opencode/lifecycle.ts b/src/platform/opencode/lifecycle.ts index c8ac2372..b2a33397 100644 --- a/src/platform/opencode/lifecycle.ts +++ b/src/platform/opencode/lifecycle.ts @@ -346,7 +346,7 @@ export async function registerFlowLifecycle( failure: sourceFailure, hasActivePrimary: (host) => { const current = attribution.current(host); - return current !== undefined && current.phase !== "ended"; + return current !== undefined && current.http.phase !== "ended"; }, guidance: (host) => autoDrive.compactionContext(host), observe: (host, origin, source, id) => { @@ -546,7 +546,7 @@ export async function registerFlowLifecycle( ); if ( prior && - prior.phase !== "ended" && + prior.http.phase !== "ended" && !attribution.retry(prior, native) ) throw new Error("Flow primary contexts or streams overlap."); @@ -687,6 +687,7 @@ export async function registerFlowLifecycle( const host = event.data.sessionID; if (event.type === "session.deleted") { revoke(host); + attribution.deleteSession(host); codingModels.delete(host); } else { const primary = attribution.current(host); diff --git a/tests/execution-attribution.test.ts b/tests/execution-attribution.test.ts index 4c3e1d5c..dbb58aa9 100644 --- a/tests/execution-attribution.test.ts +++ b/tests/execution-attribution.test.ts @@ -509,7 +509,7 @@ test("consumer cancellation ends its captured resource without revoking the exac }); if (!input.response.body) throw new Error("Missing wrapped body."); await input.response.body.cancel("protocol consumer stopped"); - expect(f.primary.phase).toBe("ended"); + expect(f.primary.http.phase).toBe("ended"); expect(f.primary.origin).toBe(origin); expect( f.attribution.verify(execution, [user, assistant], session, "/tmp", "shell") @@ -536,7 +536,9 @@ test("consumer cancellation ends its captured resource without revoking the exac "shell", ).primary.origin, ).toBe(origin); - expect(f.attribution.current(sid)?.request).toBe(nextRequest); + expect(f.attribution.requestOwner(nextRequest)).toBe( + f.attribution.current(sid), + ); expect(failures).toBe(0); }); @@ -565,7 +567,7 @@ test("consumer cancellation awaits cleanup rejection without declaring an epoch }); await Promise.resolve(); expect(settled).toBe(false); - expect(f.primary.phase).toBe("ended"); + expect(f.primary.http.phase).toBe("ended"); expect( f.attribution.verify(execution, [user, assistant], session, "/tmp", "shell") .primary.origin.id, @@ -629,7 +631,7 @@ for (const kind of ["compaction", "title", "generate"] as const) }); expect(input.response).toBe(response); expect(await input.response.text()).toBe("auxiliary"); - expect(f.primary.phase).toBe("request"); + expect(f.primary.http.phase).toBe("request"); expect(f.attribution.requestOwner(input.request)).toBeUndefined(); expect(failures).toBe(0); const primary = { @@ -760,7 +762,16 @@ test("64 held owners retain one current payload with 63 historical assistant IDs }), ).toBe(owner); } - + attribution.deleteSession(sid); + expect(owners.flatMap((owner) => retainedSnapshot(owner) ?? []).length).toBe( + 0, + ); + expect(attribution.current(sid)).toBeUndefined(); + for (const [index, owner] of owners.entries()) { + const request = requests[index]; + if (!request) throw new Error("Missing retained request."); + expect(attribution.requestOwner(request)).toBe(owner); + } }); test("clear retires evidence even when callers hold owners and Request keys", () => { @@ -771,3 +782,275 @@ test("clear retires evidence even when callers hold owners and Request keys", () expect(f.attribution.requestOwner(f.request)).toBe(f.primary); expect(() => f.attribution.freeze(context, [user], origin)).toThrow("closed"); }); + +test("end before request admission cannot prove a running tool", () => { + const attribution = new ExecutionAttribution(() => {}); + const primary = attribution.freeze(context, [user], origin); + attribution.end(primary); + expect(() => + attribution.verify(execution, [user, assistant], session, "/tmp", "shell"), + ).toThrow("primary request"); +}); + +test("invalid replacement preserves the exact prior current payload and tool", () => { + const f = fixture(); + f.attribution.end(f.primary); + for (const input of [ + { ...context, messages: [] }, + { ...context, agent: Agent.ID.make("other") }, + { + ...context, + model: { ...model, variant: Model.VariantID.make("changed") }, + }, + ]) + expect(() => f.attribution.freeze(input, [user], origin)).toThrow(); + expect(f.attribution.current(sid)).toBe(f.primary); + expect(retainedSnapshot(f.primary)?.previousAssistants.size).toBe(0); + expect( + f.attribution.verify(execution, [user, assistant], session, "/tmp", "shell") + .primary, + ).toBe(f.primary); +}); + +test("permitted retry shares exclusions and admits the retry assistant's first tool", () => { + const attribution = new ExecutionAttribution(() => {}); + const excluded = { ...assistant, id: "msg_excluded" }; + const first = attribution.freeze(context, [user, excluded], origin); + attribution.request({ + sessionID: sid, + agent, + model, + kind: "primary", + request: new Request("http://localhost/retry-1"), + }); + const retry: SessionMessageInfo = { + ...assistant, + content: [], + retry: { + attempt: 1, + at: 2, + error: { type: "provider.transport", message: "Retry" }, + }, + }; + const exclusions = retainedSnapshot(first)?.previousAssistants; + expect(attribution.retry(first, [user, excluded, retry])).toBe(true); + const next = attribution.freeze(context, [user, excluded, retry], origin); + expect(retainedSnapshot(next)?.previousAssistants).toBe(exclusions); + expect(retainedSnapshot(first)).toBeUndefined(); + attribution.request({ + sessionID: sid, + agent, + model, + kind: "primary", + request: new Request("http://localhost/retry-2"), + }); + attribution.end(next); + expect(attribution.executionOwner(execution)).toBe(next); + expect( + attribution.verify( + execution, + [user, excluded, assistant], + session, + "/tmp", + "shell", + ).primary, + ).toBe(next); + expect( + attribution.executionOwner({ + ...execution, + messageID: SessionMessage.ID.make("msg_excluded"), + }), + ).toBeUndefined(); + expect(() => attribution.retry(first, [user, retry])).toThrow("superseded"); + expect(() => attribution.hasFailedTerminal(first, [user])).toThrow( + "superseded", + ); +}); + +test("known bound and excluded assistants preserve identity after a shorter prefix", () => { + const f = fixture(); + f.attribution.verify(execution, [user, assistant], session, "/tmp", "shell"); + f.attribution.end(f.primary); + const historical = { ...assistant, id: "msg_excluded_unbound" }; + const middle = f.attribution.freeze( + context, + [user, assistant, historical], + origin, + ); + f.attribution.end(middle); + const next = f.attribution.freeze(context, [user], origin); + f.attribution.request({ + sessionID: sid, + agent, + model, + kind: "primary", + request: new Request("http://localhost/shorter"), + }); + expect(f.attribution.executionOwner(execution)).toBe(f.primary); + expect( + f.attribution.executionOwner({ ...execution, id: CallID.make("new-call") }), + ).toBe(f.primary); + expect( + f.attribution.executionOwner({ + ...execution, + messageID: SessionMessage.ID.make(historical.id), + }), + ).toBeUndefined(); + expect( + f.attribution.executionOwner({ + ...execution, + messageID: SessionMessage.ID.make("msg_never_stored"), + }), + ).toBe(next); + expect(() => + f.attribution.verify( + execution, + [user, assistant], + session, + "/tmp", + "shell", + ), + ).toThrow("rebound"); + const fresh = { ...assistant, id: "msg_fresh" }; + expect( + f.attribution.verify( + { ...execution, messageID: SessionMessage.ID.make(fresh.id) }, + [user, fresh], + session, + "/tmp", + "shell", + ).primary, + ).toBe(next); +}); + +test("deletion keeps exact Request ownership, rejects replay, and leaves another session usable", () => { + const f = fixture(); + f.attribution.verify(execution, [user, assistant], session, "/tmp", "shell"); + const otherID = Session.ID.make("session-other"); + const other = f.attribution.freeze( + { ...context, sessionID: otherID }, + [user], + origin, + ); + f.attribution.request({ + sessionID: otherID, + agent, + model, + kind: "primary", + request: new Request("http://localhost/other"), + }); + for (let repeat = 0; repeat < 2; repeat++) { + f.attribution.deleteSession(sid); + expect(retainedSnapshot(f.primary)).toBeUndefined(); + expect(f.attribution.requestOwner(f.request)).toBe(f.primary); + expect(() => f.attribution.executionOwner(execution)).toThrow( + "session was deleted", + ); + expect(() => f.attribution.freeze(context, [user], origin)).toThrow( + "session was deleted", + ); + expect(() => + f.attribution.verify( + execution, + [user, assistant], + session, + "/tmp", + "shell", + ), + ).toThrow("session was deleted"); + let failures = 0; + expect(() => + f.attribution.response( + { + sessionID: sid, + agent, + model, + kind: "primary", + request: f.request, + response: new Response("late"), + }, + () => { + failures++; + }, + ), + ).toThrow(); + expect(failures).toBe(0); + expect(f.attribution.current(sid)).toBeUndefined(); + expect( + f.attribution.verify( + { ...execution, sessionID: otherID }, + [user, assistant], + { ...session, id: otherID }, + "/tmp", + "shell", + ).primary, + ).toBe(other); + } +}); + +for (const mismatch of ["provider", "model", "variant", "agent"] as const) + test(`exact assistant ${mismatch} remains immutable after HTTP end`, () => { + const f = fixture(); + f.attribution.end(f.primary); + const changed: SessionMessageInfo = { + ...assistant, + agent: mismatch === "agent" ? Agent.ID.make("other") : agent, + model: { + ...model, + ...(mismatch === "provider" + ? { providerID: Provider.ID.make("other") } + : {}), + ...(mismatch === "model" ? { id: Model.ID.make("other") } : {}), + ...(mismatch === "variant" + ? { variant: Model.VariantID.make("other") } + : {}), + }, + }; + expect(() => + f.attribution.verify( + execution, + [user, changed], + session, + "/tmp", + "shell", + ), + ).toThrow("exact assistant"); + expect( + f.attribution.verify( + execution, + [user, assistant], + session, + "/tmp", + "shell", + ).primary, + ).toBe(f.primary); + }); + +test("one native call cannot change tool names after its immutable binding", () => { + const f = fixture(); + const binding = f.attribution.verify( + execution, + [user, assistant], + session, + "/tmp", + "shell", + ); + const changed: SessionMessageInfo = { + ...assistant, + content: assistant.content.map((part) => + part.type === "tool" ? { ...part, name: "read" } : part, + ), + }; + expect(() => + f.attribution.verify(execution, [user, changed], session, "/tmp"), + ).toThrow("immutable binding"); + expect( + f.attribution.verify( + execution, + [user, assistant], + session, + "/tmp", + "shell", + ), + ).toBe(binding); +}); diff --git a/tests/lifecycle.test.ts b/tests/lifecycle.test.ts index cb99232f..6a2bbd01 100644 --- a/tests/lifecycle.test.ts +++ b/tests/lifecycle.test.ts @@ -26,6 +26,8 @@ import { } from "../src/platform/opencode/auto-drive.js"; import { ExecutionAttribution } from "../src/platform/opencode/execution-attribution.js"; import { registerFlowLifecycle } from "../src/platform/opencode/lifecycle.js"; +import type { NativeToolDefinition } from "../src/platform/opencode/sdk.js"; +import { registerFlowTools } from "../src/platform/opencode/tools.js"; import { deterministicEnvironment, MemorySessionRepository, @@ -105,6 +107,7 @@ async function fixture(selectedAgent = agent) { let lost = false; let pushEvent: ((event: OpenCodeEvent | null) => void) | null = null; const queuedEvents: OpenCodeEvent[] = []; + const eventHandled = new Map void>(); async function waitAdmission(signal?: AbortSignal) { if (!admissionGate) return; if (!signal) return admissionGate; @@ -307,7 +310,11 @@ async function fixture(selectedAgent = agent) { once: true, }); })); - if (event) yield event; + if (event) { + yield event; + eventHandled.get(event.id)?.(); + eventHandled.delete(event.id); + } if (event === null) break; } }, @@ -439,6 +446,16 @@ async function fixture(selectedAgent = agent) { lost = true; pushEvent?.(null); }, + async sendEventAndWait(event: OpenCodeEvent) { + const handled = Promise.withResolvers(); + eventHandled.set(event.id, handled.resolve); + if (pushEvent) { + const push = pushEvent; + pushEvent = null; + push(event); + } else queuedEvents.push(event); + await handled.promise; + }, sendEvent(event: OpenCodeEvent) { if (pushEvent) { const push = pushEvent; @@ -3669,3 +3686,273 @@ for (const outcome of ["failed", "interrupted"] as const) await f.lifecycle.dispose(); } }); + +async function effectTool(f: OwnershipFixture, effects: string[]) { + const registered = new Map(); + await registerFlowTools( + { + location: { directory }, + permission: { assert: async () => {} }, + tool: { + transform: async (apply) => { + apply({ + list: () => [...registered].map(([id, tool]) => ({ id, ...tool })), + get: (id) => { + const tool = registered.get(id); + return tool ? { id, ...tool } : undefined; + }, + namespace() {}, + add: (tool) => { + registered.set(tool.name, tool); + }, + update: (id, update) => { + const tool = registered.get(id); + if (tool) update(tool); + }, + remove: (id) => { + registered.delete(id); + }, + }); + return { + dispose: async () => { + registered.clear(); + }, + }; + }, + }, + }, + { + flow_status: { + description: "Observe execution effect", + input: { type: "object" }, + execute: async () => { + effects.push("effect"); + return "applied"; + }, + }, + }, + f.lifecycle.beforeTool, + ); + const tool = registered.get("flow_status"); + if (!tool) throw new Error("Missing registered effect tool."); + return tool; +} + +for (const reject of [false, true]) + for (const successor of ["same-epoch", "new-epoch"] as const) + test(`captured tool read ${reject} preserves ${successor} successor effects`, async () => { + const f = await fixture(); + try { + await initialOwnershipWork(f); + const old = ownershipNativeCall(f); + f.blockNextContextRead(); + if (reject) f.rejectNextContextRead(); + const effects: string[] = []; + const tool = await effectTool(f, effects); + const pending = tool.execute({}, old); + const caught = pending.then( + () => null, + (error) => String(error), + ); + await f.waitContextRead(); + if (successor === "new-epoch") await freshOwnershipWork(f); + await finishOwnedResponse(f, await startOwnedRequest(f)); + const fresh = ownershipNativeCall(f); + const authority = f.autoDrive.captureAuthority(sessionID); + expect(authority).not.toBeNull(); + f.releaseContextRead(); + expect(await caught).toBe( + reject + ? "Error: Native context snapshot failed." + : "Error: Flow execution epoch was revoked.", + ); + expect(f.autoDrive.captureAuthority(sessionID)).toEqual(authority); + await tool.execute({}, fresh); + expect(effects).toEqual(["effect"]); + } finally { + f.releaseContextRead(); + await f.lifecycle.dispose(); + } + }); + +for (const known of ["bound", "historical"] as const) + for (const boundary of ["flow", "shell"] as const) + test(`${known} ${boundary} identity stays stale through compaction-truncated history`, async () => { + const f = await fixture(); + try { + await initialOwnershipWork(f); + const old = ownershipNativeCall( + f, + boundary === "shell" ? "shell" : "flow_status", + ); + if (known === "bound") await f.lifecycle.beforeTool(old); + await finishOwnedResponse(f, await startOwnedRequest(f)); + const control = automaticControl("msg_retention_compaction"); + f.native.push(control); + await f.emit("compaction", f.context()); + f.native.splice(0, f.native.length, { + ...control, + status: "completed", + }); + await finishOwnedResponse(f, await startOwnedRequest(f)); + const authority = f.autoDrive.captureAuthority(sessionID); + expect(authority).not.toBeNull(); + const effects: string[] = []; + const tool = await effectTool(f, effects); + for (const id of [old.id, CallID.make("new-call-on-old-assistant")]) { + const replay = async () => { + if (boundary === "flow") + await f.lifecycle.beforeTool({ ...old, id }); + else + for (const callback of f.toolCallbacks["execute.before"]) + await callback({ + sessionID, + agent, + messageID: old.messageID, + id, + tool: "shell", + input: {}, + }); + effects.push("old"); + }; + await expect(replay()).rejects.toThrow(); + expect(f.autoDrive.captureAuthority(sessionID)).toEqual(authority); + } + await tool.execute({}, ownershipNativeCall(f)); + expect(effects).toEqual(["effect"]); + } finally { + await f.lifecycle.dispose(); + } + }); + +test("session.deleted public barrier retires held owners and isolates another session", async () => { + const f = await fixture(); + const freeze = spyOn(ExecutionAttribution.prototype, "freeze"); + try { + await f.command("flow-auto", "Inspect original"); + const oldRequest = await startOwnedRequest(f); + await finishOwnedResponse(f, oldRequest); + const oldCall = ownershipNativeCall(f); + await f.lifecycle.beforeTool(oldCall); + const oldOwner = freeze.mock.results.at(-1)?.value; + if (!oldOwner) throw new Error("Missing captured primary."); + const otherID = Session.ID.make("session-retention-other"); + f.children.push({ ...f.session, id: otherID }); + f.native.splice(0); + const command = f.commands.get("flow-auto"); + if (!command) throw new Error("Missing automatic command."); + await command.execute({ + sessionID: otherID, + prompt: { text: "Inspect other session" }, + delivery: "queue", + }); + await f.emit("context", { ...f.context(), sessionID: otherID }); + const otherRequest = new Request("http://localhost/other-session"); + await f.emit("http.request", { + sessionID: otherID, + agent, + model, + kind: "primary", + request: otherRequest, + }); + const response = { + sessionID: otherID, + agent, + model, + kind: "primary" as const, + request: otherRequest, + response: new Response("other"), + }; + await f.emit("http.response", response); + expect(await response.response.text()).toBe("other"); + const otherCall = { ...ownershipNativeCall(f), sessionID: otherID }; + const authority = f.autoDrive.captureAuthority(otherID); + expect(authority).not.toBeNull(); + const effects: string[] = []; + const tool = await effectTool(f, effects); + for (let repeat = 0; repeat < 2; repeat++) { + await f.sendEventAndWait({ + id: `evt_retention_deleted_${repeat}`, + created: 5, + type: "session.deleted", + durable: { aggregateID: sessionID, seq: repeat + 1, version: 2 }, + data: { sessionID }, + }); + expect(f.codingModels.has(sessionID)).toBe(false); + expect(Reflect.get(oldOwner, "lifetime")?.kind ?? "current").toBe( + "retired", + ); + await expect(tool.execute({}, oldCall)).rejects.toThrow( + "session was deleted", + ); + for (const callback of f.toolCallbacks["execute.before"]) + await expect( + Promise.resolve().then(() => + callback({ + sessionID, + agent, + messageID: oldCall.messageID, + id: oldCall.id, + tool: "shell", + input: {}, + }), + ), + ).rejects.toThrow("session was deleted"); + await expect( + f.emit("http.request", { + sessionID, + agent, + model, + kind: "primary", + request: oldRequest, + }), + ).rejects.toThrow(); + await expect( + f.emit("http.response", { + sessionID, + agent, + model, + kind: "primary", + request: oldRequest, + response: new Response("late"), + }), + ).rejects.toThrow(); + await tool.execute({}, otherCall); + expect(f.autoDrive.captureAuthority(otherID)).toEqual(authority); + } + expect(effects).toEqual(["effect", "effect"]); + } finally { + freeze.mockRestore(); + await f.lifecycle.dispose(); + } +}); + +for (const when of ["before", "during-read"] as const) + test(`native ToolContext abort ${when} blocks the registered tool effect`, async () => { + const f = await fixture(); + try { + await initialOwnershipWork(f); + const effects: string[] = []; + const tool = await effectTool(f, effects); + const controller = new AbortController(); + const call = { ...ownershipNativeCall(f), signal: controller.signal }; + if (when === "before") controller.abort(); + else f.blockNextContextRead(); + const pending = tool.execute({}, call); + const caught = pending.then( + () => null, + (error) => String(error), + ); + if (when === "during-read") { + await f.waitContextRead(); + controller.abort(); + f.releaseContextRead(); + } + expect(await caught).toBe("Error: Flow tool execution was cancelled."); + expect(effects).toEqual([]); + expect(f.autoDrive.captureAuthority(sessionID)).not.toBeNull(); + } finally { + f.releaseContextRead(); + await f.lifecycle.dispose(); + } + }); From b37ad8849770b3ae66bdf450e8cbc867b6077d44 Mon Sep 17 00:00:00 2001 From: Douwe de Vries Date: Sun, 25 Oct 2026 01:03:54 +0200 Subject: [PATCH 07/17] test(opencode): cover unregistered native execution paths --- tests/lifecycle.test.ts | 160 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 160 insertions(+) diff --git a/tests/lifecycle.test.ts b/tests/lifecycle.test.ts index 6a2bbd01..4a304d7c 100644 --- a/tests/lifecycle.test.ts +++ b/tests/lifecycle.test.ts @@ -3956,3 +3956,163 @@ for (const when of ["before", "during-read"] as const) await f.lifecycle.dispose(); } }); + +async function exerciseUnregisteredNativeExecution( + f: OwnershipFixture, + expectedMessages: SessionContext["messages"], +) { + expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); + expect(f.recovery.snapshot(sessionID)).toEqual({ mode: "off" }); + expect(f.repository.saveCount).toBe(0); + const context = f.context(); + await f.emit("context", context); + expect(context.messages).toEqual(expectedMessages); + const request = new Request("http://localhost/unregistered-native", { + method: "POST", + body: "ordinary native request body", + }); + const outbound = { + sessionID, + agent, + model, + kind: "primary" as const, + request, + }; + await f.emit("http.request", outbound); + expect(await outbound.request.clone().text()).toBe( + "ordinary native request body", + ); + const inbound = { + ...outbound, + response: new Response("ordinary native response body"), + }; + await f.emit("http.response", inbound); + expect(await inbound.response.text()).toBe("ordinary native response body"); + const shell = ownershipNativeCall(f, "shell"); + let shellEffects = 0; + for (const callback of f.toolCallbacks["execute.before"]) + await callback({ + ...shell, + tool: "shell", + input: { command: "printf not-run" }, + }); + shellEffects++; + expect(shellEffects).toBe(1); + const assistant = f.native.find((message) => message.id === shell.messageID); + if (assistant?.type !== "assistant") + throw new Error("Missing native tool assistant."); + const flowCall = { ...shell, id: CallID.make("call_unregistered_flow") }; + assistant.content.push({ + type: "tool", + id: flowCall.id, + name: "flow_status", + time: { created: 4 }, + state: { + status: "running", + input: { request: { view: "compact" } }, + metadata: {}, + }, + }); + const effects: string[] = []; + const tool = await effectTool(f, effects); + await expect(tool.execute({}, flowCall)).rejects.toThrow(); + expect(effects).toEqual([]); + expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); + expect(f.recovery.snapshot(sessionID)).toEqual({ mode: "off" }); + expect(f.repository.saveCount).toBe(0); + expect(f.repository.session).toBeNull(); +} + +test("unregistered native regression: reloaded user runs ordinary execution without Flow authority", async () => { + const f = await fixture(); + try { + f.native.push({ + type: "user", + id: "msg_reloaded_user", + text: "Ordinary queued user before reload", + time: { created: 0 }, + }); + await exerciseUnregisteredNativeExecution(f, [ + { + role: "user", + id: "msg_reloaded_user", + content: [{ type: "text", text: "Ordinary queued user before reload" }], + }, + ]); + } finally { + await f.lifecycle.dispose(); + } +}); + +test("unregistered native regression: external synthetic in empty ledger runs ordinary execution without Flow authority", async () => { + const f = await fixture(); + try { + f.native.push({ + type: "synthetic", + id: "msg_external_synthetic", + text: "Ordinary external synthetic request", + time: { created: 0 }, + }); + await exerciseUnregisteredNativeExecution(f, [ + { + role: "user", + id: "msg_external_synthetic", + content: [{ type: "text", text: "Ordinary external synthetic request" }], + }, + ]); + } finally { + await f.lifecycle.dispose(); + } +}); + +test("unregistered native regression: external synthetic after ordinary prompt in same session runs without Flow authority", async () => { + const f = await fixture(); + try { + await admitOrdinaryInput( + f, + "msg_prior_ordinary", + "Ordinary prompt before external notification", + ); + await finishOwnedResponse(f, await startOwnedRequest(f)); + f.native.push( + { + type: "assistant", + id: "msg_prior_ordinary_assistant", + agent, + model, + time: { created: 11, completed: 12 }, + content: [{ type: "text", text: "Fixture finished" }], + }, + { + type: "idle", + id: "msg_prior_ordinary_idle", + outcome: "succeeded", + time: { created: 13 }, + }, + { + type: "synthetic", + id: "msg_external_after_ordinary", + text: "External ordinary notification after prior prompt", + time: { created: 14 }, + }, + ); + await exerciseUnregisteredNativeExecution(f, [ + { + role: "user", + id: "msg_prior_ordinary", + content: [ + { type: "text", text: "Ordinary prompt before external notification" }, + ], + }, + { + role: "user", + id: "msg_external_after_ordinary", + content: [ + { type: "text", text: "External ordinary notification after prior prompt" }, + ], + }, + ]); + } finally { + await f.lifecycle.dispose(); + } +}); From 3c0206647f0247ce9eeaa6b1b5d68f25c8f05e94 Mon Sep 17 00:00:00 2001 From: Douwe de Vries Date: Sun, 25 Oct 2026 01:32:57 +0200 Subject: [PATCH 08/17] fix(opencode): isolate native turns from Flow authority --- docs/adr/0018-opencode-v2-only.md | 71 +- evals/replay.ts | 2 +- src/application/prepare-validation.ts | 2 + src/infrastructure/fs/workspace-validation.ts | 2 + src/platform/opencode/auto-compaction.ts | 4 +- .../opencode/execution-attribution.ts | 88 +- src/platform/opencode/input-admission.ts | 22 +- src/platform/opencode/lifecycle.ts | 319 +++--- src/platform/opencode/plugin-composition.ts | 43 +- src/platform/opencode/validation-capture.ts | 241 +++-- tests/auto-drive.test.ts | 4 +- tests/execution-attribution.test.ts | 379 +++++-- .../fixtures/verified-continuation-origin.ts | 14 +- tests/input-admission.test.ts | 62 +- tests/lifecycle.test.ts | 946 ++++++++++++++++-- tests/recovery-authority.test.ts | 4 +- tests/recovery-policy.test.ts | 2 +- tests/runtime-gates.test.ts | 136 +++ tests/validation-capture.test.ts | 235 ++++- 19 files changed, 2064 insertions(+), 512 deletions(-) diff --git a/docs/adr/0018-opencode-v2-only.md b/docs/adr/0018-opencode-v2-only.md index 22468306..2447647b 100644 --- a/docs/adr/0018-opencode-v2-only.md +++ b/docs/adr/0018-opencode-v2-only.md @@ -11,45 +11,46 @@ cadence. There is no bridge release or dual runtime. Session v5 is unchanged. ## Host boundary -Native v2 definitions register roles, commands, and tools. Application schemas -produce JSON Schema. Structured tool results retain Flow envelopes and guidance. - -Each primary request freezes consumed inputs and assistant bindings within a -live epoch. The prompt hook grants no authority. A tool must match the native session, assistant, agent, call, -workspace, and consumed-input snapshot. Asynchronous event arrival order does -not establish authority. Flow selects HTTP in provider, model, and variant -settings so each request stays observable. - -HTTP consumer cancellation ends the captured request, as EOF does. It proves -neither success nor user interruption. Tool guards retain exact input identity -and the native abort signal. Read and status failures retain owned revocation. - -Automatic compaction can precede the first primary request. It preserves the -epoch of an accepted input only through the exact checked native control. -Its user-role checkpoint grants no approval. The input ledger owns reservations, -readiness, lifetime, and history; request attribution owns primary and tool -bindings. Readiness waits precede authority checks. Interrupted ordinary history -may summarize without an execution origin. Known automatic history cannot take -that fallback. Live ordinary continuations retain their checked origin. -Matched source failures revoke that source; stale unmatched callbacks cannot -revoke a fresh one. Authority never comes from summary text or survives restart. - -The model picker loads with the server package. Server-owned global preferences -are separate from native configuration; project plugin options override them. -Role updates check revision, catalog, project, and activity. Transforms refresh -loaded instances. Explicit selection clears the role's reasoning variant; reset -restores its base. The coding model stays under native selection. +Native definitions register roles, commands, and tools. Application schemas +supply JSON Schema. Results retain Flow envelopes and guidance. +Frozen inputs bind each primary to its native session, agent, model, assistant, +call, and workspace. Flow selects HTTP for providers, models, and variants. +Prompt hooks and event arrival order grant no authority. + +Unregistered native user and synthetic turns may use ordinary tools. They gain +no Flow-tool access, validation, recovery intent, or automatic lease. A fresh +observed prompt or Flow command establishes Flow provenance. Retired request +owners retain no transcript snapshots. + +EOF and consumer cancellation close only the captured HTTP request. Protected +calls retain exact input identity and the native abort signal. Only a proved +owner can revoke. Terminal read failures before proof grant nothing. + +The input ledger owns reservations, readiness, lifetime, and history. Request +attribution owns primary and tool bindings. Readiness precedes authority checks. +Checked native controls preserve accepted epochs through automatic compaction, +including before the first primary. Checkpoints grant no approval. Matched source +failure revokes only that source. Stale callbacks cannot revoke a fresh source. +Summary text and restart never supply authority. + +Ordinary history may summarize without an origin. Summary-only execution and +unknown-history automatic compaction remain refused. Accepted ordinary +continuations retain their checked origin. Known automatic history cannot become +unowned summary history. + +The model picker loads with the server. Server storage owns global preferences. +Project options override them. Role updates check revision, catalog, project, +and activity, then refresh transforms. Selection clears the role's reasoning +variant. Reset restores its base. Native selection owns the coding model. ## Qualification -Qualification retains all 17 cases on two provider routes. -Manager and reviewer use the same approved model within each cell. Recovery is -off. Candidate, canary, and publication retain the exact archive and unpacked -manifest identities. +Qualification retains 17 cases on two provider routes, with the same approved +manager and reviewer model per cell and recovery off. Candidate, canary, and +publication bind the exact archive and unpacked manifest. -Historical 9.x pins, digests, transcripts, and qualification bundles remain -verifiable without an active OpenCode 1 implementation. Publication retains -existing approval gates. +Historical 9.x pins, digests, transcripts, and bundles remain verifiable without +an OpenCode 1 runtime. Publication approval gates remain. See [release qualification](../release-qualification.md) for the executable gates and [automatic continuation](../automatic-continuation.md) for operator behavior. diff --git a/evals/replay.ts b/evals/replay.ts index 8b0ddd46..132732bb 100644 --- a/evals/replay.ts +++ b/evals/replay.ts @@ -304,7 +304,7 @@ async function replayBash( ): Promise { const fixture = recordedBashDecisionFixture(event); try { - await validation.observeToolBefore(fixture); + await validation.observeToolBefore(fixture, () => {}); } catch (error) { return `[flow-validation-error] ${error instanceof Error ? error.message : String(error)}`; } diff --git a/src/application/prepare-validation.ts b/src/application/prepare-validation.ts index 95843c6a..311929c7 100644 --- a/src/application/prepare-validation.ts +++ b/src/application/prepare-validation.ts @@ -175,6 +175,7 @@ export async function prepareValidation( export async function persistObservedValidation( repository: SessionRepository, input: ObservedValidation, + assertAuthorized: () => void = () => {}, ): Promise { return repository.transact(async (transaction) => { const session = await transaction.load(); @@ -187,6 +188,7 @@ export async function persistObservedValidation( ? { ineligibleReason: "source-drift" as const } : {}), }); + assertAuthorized(); await transaction.save(result.session); return result.value; }); diff --git a/src/infrastructure/fs/workspace-validation.ts b/src/infrastructure/fs/workspace-validation.ts index ce3d015b..2c3924a6 100644 --- a/src/infrastructure/fs/workspace-validation.ts +++ b/src/infrastructure/fs/workspace-validation.ts @@ -176,9 +176,11 @@ export async function readWorkspaceTestReport( export function persistWorkspaceValidation( workspace: string, input: ObservedValidation, + assertAuthorized?: () => void, ) { return persistObservedValidation( createFileSessionRepository(workspace), input, + assertAuthorized, ); } diff --git a/src/platform/opencode/auto-compaction.ts b/src/platform/opencode/auto-compaction.ts index a7ef6741..7bc69624 100644 --- a/src/platform/opencode/auto-compaction.ts +++ b/src/platform/opencode/auto-compaction.ts @@ -106,11 +106,13 @@ export class CompactionBoundary { ); if (!same(first.proof, second.proof)) throw new Error("Flow automatic compaction control identity changed."); - const evidence = await this.#options.inputs.evidence( + const source = await this.#options.inputs.evidence( scope, second.prefix, second.proof.delivery, ); + const evidence = + source.kind === "unregistered" ? { kind: source.lineage } : source; const final = running( input, await scope.wait(this.#options.readNative(scope.host)), diff --git a/src/platform/opencode/execution-attribution.ts b/src/platform/opencode/execution-attribution.ts index e25a6690..eac52e01 100644 --- a/src/platform/opencode/execution-attribution.ts +++ b/src/platform/opencode/execution-attribution.ts @@ -25,7 +25,13 @@ export type InputOrigin = Readonly<{ | Readonly<{ automatic: false; authority: AutoDriveAuthority | null }> ); +export type Participation = + | Readonly<{ kind: "flow"; origin: InputOrigin }> + | Readonly<{ kind: "native-only" }>; +type InputClock = Readonly<{ current(): boolean; assertCurrent(): void }>; + type Snapshot = Readonly<{ + clock: InputClock; inputs: readonly string[]; previousAssistants: ReadonlySet; previousTerminals: ReadonlySet; @@ -45,7 +51,7 @@ type PrimaryOwner = { readonly sessionID: string; readonly agent: string; readonly model: SessionContext["model"]; - readonly origin: InputOrigin; + readonly participation: Participation; http: HttpResource; lifetime: Lifetime; }; @@ -173,6 +179,20 @@ export class ExecutionAttribution { throw new Error("Flow primary ownership was superseded."); return primary.lifetime.snapshot; } + assertCurrent(primary: PrimaryOwner): void { + const snapshot = this.#evidence(primary); + snapshot.clock.assertCurrent(); + if (primary.participation.kind === "flow") + this.#assertLive(primary.participation.origin); + } + isCurrent(primary: PrimaryOwner): boolean { + return ( + !this.#closed && + this.current(primary.sessionID) === primary && + primary.lifetime.kind === "current" && + primary.lifetime.snapshot.clock.current() + ); + } requestOwner(request: Request): PrimaryOwner | undefined { return this.#requests.get(request); } @@ -197,33 +217,47 @@ export class ExecutionAttribution { freeze( input: SessionContext, native: readonly SessionMessageInfo[], - origin: InputOrigin, + participation: Participation, + clock: InputClock, ): PrimaryOwner { if (this.#closed) throw new Error("Flow lifecycle is closed."); const record = this.#session(input.sessionID); const prior = record?.current; - if (prior && prior.http.phase !== "ended") + if (prior && this.isCurrent(prior) && prior.http.phase !== "ended") throw new Error("Flow primary contexts or streams overlap."); - this.#assertLive(origin); + clock.assertCurrent(); + if (participation.kind === "flow") this.#assertLive(participation.origin); const inputs = modelInputIDs(input); if (!same(inputs, nativeInputIDs(native))) throw new Error( "Flow native inputs differ from the model-visible prefix.", ); const delivery = nativeDelivery({ agent: input.agent, model: input.model }); - if (!sameDelivery(origin.delivery, delivery)) + if ( + participation.kind === "flow" && + !sameDelivery(participation.origin.delivery, delivery) + ) throw new Error( "Flow admitted agent or model differs from its primary request.", ); - const previous = prior && this.#evidence(prior); + const previous = + prior?.lifetime.kind === "current" ? prior.lifetime.snapshot : undefined; const retry = prior && this.#retries.delete(prior) && - prior.origin === origin && + previous?.clock.current() && + prior.participation.kind === participation.kind && + (participation.kind === "native-only" || + (prior.participation.kind === "flow" && + prior.participation.origin === participation.origin)) && same(previous?.inputs, inputs) && prior.agent === input.agent && sameModel(prior.model, input.model); const snapshot: Snapshot = Object.freeze({ + clock: Object.freeze({ + current: clock.current, + assertCurrent: clock.assertCurrent, + }), inputs: Object.freeze(inputs), previousAssistants: retry && previous @@ -250,7 +284,7 @@ export class ExecutionAttribution { sessionID: input.sessionID, agent: input.agent, model: Object.freeze({ ...input.model }), - origin, + participation: Object.freeze(participation), http: { phase: "context", requestSeen: false }, lifetime: { kind: "current", snapshot }, }; @@ -259,7 +293,7 @@ export class ExecutionAttribution { assistants: new Map(), bindings: new Map(), }; - if (prior) prior.lifetime = retired; + if (prior) this.retire(prior); next.current = primary; this.#sessions.set(input.sessionID, next); for (const id of snapshot.previousAssistants) @@ -278,24 +312,30 @@ export class ExecutionAttribution { throw new Error( "Flow primary HTTP request has no unique frozen context.", ); - this.#assertLive(primary.origin); + this.assertCurrent(primary); primary.http = { phase: "request", requestSeen: true }; this.#requests.set(input.request, primary); } - hasFailedTerminal( + terminal( primary: PrimaryOwner, native: readonly SessionMessageInfo[], - ): boolean { + id?: string, + ): Extract | undefined { + this.assertCurrent(primary); const snapshot = this.#evidence(primary); - return native.some( - (message, position) => + return native.find( + ( + message, + position, + ): message is Extract => message.type === "idle" && - message.outcome !== "succeeded" && + (id === undefined || message.id === id) && !snapshot.previousTerminals.has(message.id) && same(nativeInputIDs(native.slice(0, position)), snapshot.inputs), ); } retry(primary: PrimaryOwner, native: readonly SessionMessageInfo[]): boolean { + this.assertCurrent(primary); const snapshot = this.#evidence(primary); const record = this.#session(primary.sessionID); if ( @@ -328,9 +368,13 @@ export class ExecutionAttribution { primary.http = { phase: "ended", requestSeen: primary.http.requestSeen }; return active && this.current(primary.sessionID) === primary; } + retire(primary: PrimaryOwner): void { + this.end(primary); + primary.lifetime = retired; + } response( input: SessionHttpResponse, - failed: (sessionID: string) => void, + failed: (primary: PrimaryOwner) => void, ): void { if (input.kind !== "primary") return; this.#session(input.sessionID); @@ -343,11 +387,7 @@ export class ExecutionAttribution { !sameModel(primary.model, input.model) ) { if (primary) { - if (this.end(primary)) failed(primary.sessionID); - } else { - const current = this.current(input.sessionID); - if (current) this.end(current); - failed(input.sessionID); + if (this.end(primary)) failed(primary); } throw new Error( "Flow primary response did not match its Request identity.", @@ -355,14 +395,14 @@ export class ExecutionAttribution { } const finish = () => this.end(primary); const fail = () => { - if (finish()) failed(primary.sessionID); + if (finish()) failed(primary); }; if (!input.response.ok) { fail(); return; } try { - this.#assertLive(primary.origin); + this.assertCurrent(primary); if (!input.response.body) throw new Error("Flow primary response has no successful stream."); const reader = input.response.body.getReader(); @@ -455,7 +495,7 @@ export class ExecutionAttribution { throw new Error( "Flow execution identity conflicts with an immutable binding.", ); - this.#assertLive(primary.origin); + this.assertCurrent(primary); if (!prior) record.assistants.set( execution.messageID, diff --git a/src/platform/opencode/input-admission.ts b/src/platform/opencode/input-admission.ts index 7de29aac..36ce71c2 100644 --- a/src/platform/opencode/input-admission.ts +++ b/src/platform/opencode/input-admission.ts @@ -54,15 +54,15 @@ type Entry = | Real | Alias | Readonly<{ kind: "reserved"; reservation: InputReservation }>; +type History = + | "retired-ordinary" + | "retired-automatic" + | "unknown-history" + | "rejected"; type SourceEvidence = + | Readonly<{ kind: "unregistered"; lineage: History }> | Readonly<{ kind: "live"; origin: InputOrigin }> - | Readonly<{ - kind: - | "retired-ordinary" - | "retired-automatic" - | "unknown-history" - | "rejected"; - }>; + | Readonly<{ kind: History }>; export type InputScope = Readonly<{ host: string; consumed: readonly string[]; @@ -307,7 +307,11 @@ export class InputAdmissions { scope.assertCurrent(); await scope.ready(); const entry = this.#scopes.get(scope)?.entry; - if (!entry) return this.#history(this.#scopes.get(scope)?.frontierEntry); + if (!entry) + return { + kind: "unregistered", + lineage: this.#history(this.#scopes.get(scope)?.frontierEntry).kind, + }; if (entry.kind === "reserved") return { kind: "rejected" }; if (entry.kind === "alias") { if (entry.origin) return this.#classify(entry.origin); @@ -374,7 +378,7 @@ export class InputAdmissions { })(); return this.#classify(await scope.wait(entry.resolving)); } - #history(entry: Entry | undefined): SourceEvidence { + #history(entry: Entry | undefined): Readonly<{ kind: History }> { if (!entry) return { kind: "unknown-history" }; if (entry.kind === "alias") return this.#history(entry.source); if ( diff --git a/src/platform/opencode/lifecycle.ts b/src/platform/opencode/lifecycle.ts index b2a33397..b9b2039b 100644 --- a/src/platform/opencode/lifecycle.ts +++ b/src/platform/opencode/lifecycle.ts @@ -32,6 +32,7 @@ import { type NativeExecution, nativeDelivery, nativeInputIDs, + type Participation, rememberCodingModel, sameDelivery, } from "./execution-attribution.js"; @@ -55,6 +56,7 @@ type LifecycleOptions = Readonly<{ recovery: RecoveryController; flow: FlowService; runtimeGuard: FlowLeadershipHandle; + onFlowTerminal?: ((host: string) => void) | undefined; defaultRecovery?: (() => RecoverySettings | null) | undefined; log( level: "info" | "warn" | "error", @@ -63,8 +65,15 @@ type LifecycleOptions = Readonly<{ ): void; codingModels: Map; }>; +type ToolAccess = + | Readonly<{ kind: "flow"; assertCurrent(): void }> + | Readonly<{ kind: "native-only" | "untracked" }>; type FlowLifecycle = Readonly<{ - beforeTool(nativeToolContext: ToolContext): Promise; + verifyTool( + execution: NativeExecution | ToolContext, + use: "native" | "flow", + tool?: string, + ): Promise; prompt( sessionID: string, prompt: string, @@ -114,9 +123,12 @@ export async function registerFlowLifecycle( }); const assertLive = (origin: InputOrigin) => inputs.assertLive(origin); const attribution = new ExecutionAttribution(assertLive); - const revoke = (host: string, deactivate = true) => { - const prior = attribution.current(host); - if (prior) attribution.end(prior); + const revoke = ( + host: string, + deactivate = true, + captured = attribution.current(host) ?? null, + ) => { + if (captured) attribution.end(captured); inputs.invalidate(host); autoDrive.revokeExecutions(host); recovery.invalidateOrigins(host); @@ -128,8 +140,10 @@ export async function registerFlowLifecycle( const captureOwner = ( host: string, kind: CallbackOwner["kind"] = "attempt", - primary = attribution.current(host), - expectedEpoch = epoch(host), + primary: ReturnType = undefined, + expectedEpoch = kind === "attempt" && primary?.participation.kind === "flow" + ? primary.participation.origin.epoch + : epoch(host), input?: InputOrigin, source = input?.id, ): CallbackOwner => @@ -143,13 +157,16 @@ export async function registerFlowLifecycle( }); const owns = (owner: CallbackOwner, primary = owner.primary) => { const origin = - owner.input ?? (owner.kind === "attempt" ? primary?.origin : undefined); + owner.input ?? + (owner.kind === "attempt" && primary?.participation.kind === "flow" + ? primary.participation.origin + : undefined); return ( !closed && !streamLost && epoch(owner.host) === owner.epoch && (owner.kind === "admission" || - attribution.current(owner.host) === primary) && + (primary !== undefined && attribution.isCurrent(primary))) && (!origin || inputs.isLive(origin)) ); }; @@ -160,12 +177,20 @@ export async function registerFlowLifecycle( }; const failOwned = (owner: CallbackOwner, primary = owner.primary) => { if ( - primary?.origin.epoch === owner.epoch && - (!owner.input || primary.origin === owner.input) && + owner.kind === "attempt" && + primary?.participation.kind === "native-only" + ) { + attribution.retire(primary); + return; + } + if ( + primary?.participation.kind === "flow" && + primary.participation.origin.epoch === owner.epoch && + (!owner.input || primary.participation.origin === owner.input) && (!owner.source || owns(owner, primary)) ) attribution.end(primary); - if (owns(owner, primary)) revoke(owner.host); + if (owns(owner, primary)) revoke(owner.host, true, primary ?? null); }; const failReservation = ( reservation: Parameters[0], @@ -204,17 +229,19 @@ export async function registerFlowLifecycle( result === "accepted-continuation", ); }; - const verify = async (execution: NativeExecution, tool?: string) => { + const verifyTool = async ( + execution: NativeExecution, + use: "native" | "flow", + tool?: string, + ): Promise => { assertOpen(); const primary = attribution.executionOwner(execution); - if (!primary) + if (!primary) { + if (use === "native" && !attribution.current(execution.sessionID)) + return { kind: "untracked" }; throw new Error("Flow tool has no attributable primary request."); - const owner = captureOwner( - execution.sessionID, - "attempt", - primary, - primary.origin.epoch, - ); + } + const owner = captureOwner(execution.sessionID, "attempt", primary); try { if ( epoch(owner.host) === owner.epoch && @@ -222,34 +249,46 @@ export async function registerFlowLifecycle( ) throw new Error("Flow assistant cannot be rebound to another request."); assertOwned(owner); - assertLive(primary.origin); + attribution.assertCurrent(primary); const [native, session] = await Promise.all([ ctx.session.context({ sessionID: execution.sessionID }), ctx.session.get({ sessionID: execution.sessionID }), ]); assertOwned(owner); - const verified = attribution.verify( + attribution.verify( execution, native, session, ctx.location.directory, tool, ); - assertLive(verified.primary.origin); - autoDrive.observeAssistant( - execution.sessionID, - execution.messageID, - verified.primary.origin.id, - ); - recovery.observeAssistant( - execution.sessionID, - execution.messageID, - verified.primary.origin.id, - ); } catch (error) { failOwned(owner); throw error; } + if (primary.participation.kind === "native-only") { + if (use === "flow") + throw new Error("Flow tool requires a live admitted input origin."); + return { kind: "native-only" }; + } + const origin = primary.participation.origin; + autoDrive.observeAssistant( + execution.sessionID, + execution.messageID, + origin.id, + ); + recovery.observeAssistant( + execution.sessionID, + execution.messageID, + origin.id, + ); + return { + kind: "flow", + assertCurrent: () => { + assertOpen(); + attribution.assertCurrent(primary); + }, + }; }; const commandAccepted = createCommandHook({ ...options, @@ -272,7 +311,12 @@ export async function registerFlowLifecycle( const id = SessionMessage.ID.create(); const reservation = inputs.reserve(host, id); const expectedEpoch = reservation.epoch; - const owner = captureOwner(host, "admission"); + const owner = captureOwner( + host, + "admission", + attribution.current(host), + expectedEpoch, + ); const authority = autoDrive.captureAuthority(host, token); if (!authority) throw new Error("Flow synthetic authority expired."); const assertCurrent = () => { @@ -326,14 +370,17 @@ export async function registerFlowLifecycle( throw error; } }; - const sourceFailure = (scope: InputScope) => { + const sourceFailure = ( + scope: InputScope, + primary?: ReturnType, + ) => { const origin = inputs.positive(scope); if (origin) failOwned( captureOwner( scope.host, "admission", - attribution.current(scope.host), + primary, origin.epoch, origin, origin.id, @@ -363,24 +410,32 @@ export async function registerFlowLifecycle( if (!origin.authority) recovery.observeMessage(host, id, true, true); }, }); - const resolveOrigin = async ( + const resolveParticipation = async ( input: SessionContext, native: readonly SessionMessageInfo[], scope: InputScope, - ): Promise => { + ): Promise => { const completed = compactions.completed(input, native); - if (completed) return completed; + if (completed) return { kind: "flow", origin: completed }; const evidence = await inputs.evidence( scope, native, nativeDelivery(input), ); - if (evidence.kind !== "live") - throw new Error( - "Flow context has an unknown, synthetic, or stale real input.", - ); - assertLive(evidence.origin); - return evidence.origin; + if (evidence.kind === "live") { + assertLive(evidence.origin); + return { kind: "flow", origin: evidence.origin }; + } + const source = native.find((message) => message.id === scope.source); + if ( + (source?.type === "user" || source?.type === "synthetic") && + (evidence.kind === "retired-ordinary" || + (evidence.kind === "unregistered" && evidence.lineage !== "rejected")) + ) + return { kind: "native-only" }; + throw new Error( + "Flow context has an unknown, synthetic, or stale real input.", + ); }; try { registrations.push( @@ -410,7 +465,12 @@ export async function registerFlowLifecycle( const id = SessionMessage.ID.create(); const reservation = inputs.reserve(input.sessionID, id); const expectedEpoch = reservation.epoch; - const owner = captureOwner(input.sessionID, "admission"); + const owner = captureOwner( + input.sessionID, + "admission", + attribution.current(input.sessionID), + expectedEpoch, + ); const assertCurrent = () => { inputs.assertReservation(reservation); if (epoch(input.sessionID) !== expectedEpoch) @@ -537,15 +597,14 @@ export async function registerFlowLifecycle( contextPending.set(input.sessionID, scope); const prior = attribution.current(input.sessionID); let frozen: ReturnType | undefined; - let origin = inputs.positive(scope); try { await scope.ready(); - origin = inputs.positive(scope); const native = await scope.wait( ctx.session.context({ sessionID: input.sessionID }), ); if ( prior && + attribution.isCurrent(prior) && prior.http.phase !== "ended" && !attribution.retry(prior, native) ) @@ -554,28 +613,31 @@ export async function registerFlowLifecycle( throw new Error( "Flow native inputs differ from the model-visible prefix.", ); - origin = await resolveOrigin(input, native, scope); + const participation = await resolveParticipation( + input, + native, + scope, + ); scope.assertCurrent(); - frozen = attribution.freeze(input, native, origin); const session = await scope.wait( ctx.session.get({ sessionID: input.sessionID }), ); - assertLive(origin); + if ( + participation.kind === "native-only" && + (session.id !== input.sessionID || + !sameDelivery(nativeDelivery(session), nativeDelivery(input))) + ) + throw new Error( + "Flow native selected session agent or model changed.", + ); + scope.assertCurrent(); + frozen = attribution.freeze(input, native, participation, scope); + attribution.assertCurrent(frozen); rememberCodingModel(codingModels, input, session, specialists); } catch (error) { - if (frozen) attribution.end(frozen); - if (origin && scope.current()) - failOwned( - captureOwner( - input.sessionID, - "attempt", - frozen ?? prior, - origin.epoch, - origin, - origin.id, - ), - frozen ?? prior, - ); + if (frozen) + failOwned(captureOwner(input.sessionID, "attempt", frozen)); + else sourceFailure(scope, prior); throw error; } finally { if (contextPending.get(input.sessionID) === scope) @@ -595,35 +657,21 @@ export async function registerFlowLifecycle( ); registrations.push( await ctx.session.hook("http.request", (input) => { - const primary = - attribution.requestOwner(input.request) ?? - attribution.current(input.sessionID); - const owner = captureOwner( - input.sessionID, - "attempt", - primary, - primary?.origin.epoch ?? epoch(input.sessionID), - ); + const primary = attribution.requestOwner(input.request); try { attribution.request(input); } catch (error) { - failOwned(owner); + if (primary) + failOwned(captureOwner(primary.sessionID, "attempt", primary)); throw error; } }), ); registrations.push( await ctx.session.hook("http.response", (input) => { - const primary = - attribution.requestOwner(input.request) ?? - attribution.current(input.sessionID); - const owner = captureOwner( - input.sessionID, - "attempt", - primary, - primary?.origin.epoch ?? epoch(input.sessionID), + attribution.response(input, (primary) => + failOwned(captureOwner(primary.sessionID, "attempt", primary)), ); - attribution.response(input, () => failOwned(owner)); }), ); registrations.push( @@ -635,7 +683,9 @@ export async function registerFlowLifecycle( input.sessionID, "attempt", primary, - primary?.origin.epoch ?? epoch(input.sessionID), + primary?.participation.kind === "flow" + ? primary.participation.origin.epoch + : epoch(input.sessionID), ), ); throw new Error( @@ -650,15 +700,24 @@ export async function registerFlowLifecycle( const staleAssistant = current !== undefined && executionOwner === undefined; const protectedAutomatic = Boolean( - current?.origin.authority || executionOwner?.origin.authority, + (current?.participation.kind === "flow" && + current.participation.origin.authority) || + (executionOwner?.participation.kind === "flow" && + executionOwner.participation.origin.authority), ); if ( !input.tool.startsWith("flow_") && !protectedAutomatic && + current?.participation.kind !== "native-only" && + executionOwner?.participation.kind !== "native-only" && !staleAssistant ) return; - await verify(input, input.tool); + await verifyTool( + input, + input.tool.startsWith("flow_") ? "flow" : "native", + input.tool, + ); }), ); } catch (error) { @@ -677,75 +736,81 @@ export async function registerFlowLifecycle( signal: controller.signal, })) { if (closed) break; - if ( - event.type === "session.execution.failed" || - event.type === "session.execution.interrupted" || - event.type === "session.deleted" - ) { + if (event.type === "session.deleted") { if (outcomes.has(event.id)) continue; outcomes.add(event.id); const host = event.data.sessionID; - if (event.type === "session.deleted") { - revoke(host); - attribution.deleteSession(host); - codingModels.delete(host); - } else { - const primary = attribution.current(host); - if (!primary) continue; - const owner = captureOwner( - host, - "attempt", - primary, - primary.origin.epoch, - ); - try { - const native = await ctx.session.context({ sessionID: host }); - if (owns(owner) && attribution.hasFailedTerminal(primary, native)) - failOwned(owner); - } catch (error) { - failOwned(owner); - log("warn", "Flow terminal observation failed.", { - host, - message: errorOf(error).message, - }); - } - } + revoke(host); + attribution.deleteSession(host); + codingModels.delete(host); continue; } if ( + event.type !== "session.execution.failed" && + event.type !== "session.execution.interrupted" && event.type !== "session.execution.succeeded" && event.type !== "session.idle" ) continue; + if (outcomes.has(event.id)) continue; const host = event.data.sessionID; const primary = attribution.current(host); if (!primary) continue; - const owner = captureOwner( - host, - "attempt", - primary, - primary.origin.epoch, - ); + const owner = captureOwner(host, "attempt", primary); + let proved = false; try { + const native = await ctx.session.context({ sessionID: host }); + if (!owns(owner)) continue; + const terminal = attribution.terminal( + primary, + native, + event.type === "session.idle" + ? undefined + : SessionMessage.ID.fromEvent(event.id), + ); + if ( + !terminal || + (event.type === "session.execution.failed" && + terminal.outcome !== "failed") || + (event.type === "session.execution.interrupted" && + terminal.outcome !== "interrupted") || + (event.type === "session.execution.succeeded" && + terminal.outcome !== "succeeded") + ) + continue; + proved = true; + outcomes.add(event.id); + if (primary.participation.kind === "native-only") { + attribution.retire(primary); + continue; + } + if (terminal.outcome !== "succeeded") { + options.onFlowTerminal?.(host); + failOwned(owner); + continue; + } const session = await ctx.session.get({ sessionID: host }); if (!owns(owner)) continue; if (session.id !== host) throw new Error("Flow native terminal session identity changed."); if (session.time.idle === undefined || session.outcome === undefined) continue; + if ( + session.outcome !== terminal.outcome || + session.time.idle !== terminal.time.created + ) + continue; const watermark = String(session.time.idle); if (idle.get(host) === watermark) continue; idle.set(host, watermark); - if ( - session.outcome !== "succeeded" || - !runtimeGuard.query().operational - ) { + options.onFlowTerminal?.(host); + if (!runtimeGuard.query().operational) { failOwned(owner); continue; } await autoDrive.onIdle(host); } catch (error) { - failOwned(owner); + if (proved && owns(owner)) failOwned(owner); log("warn", "Flow terminal observation failed.", { host, message: errorOf(error).message, @@ -767,7 +832,7 @@ export async function registerFlowLifecycle( } })(); return { - beforeTool: (context) => verify(context), + verifyTool, prompt: synthetic, dispose: () => { disposal ??= (async () => { diff --git a/src/platform/opencode/plugin-composition.ts b/src/platform/opencode/plugin-composition.ts index 8f4eb194..aa43d20b 100644 --- a/src/platform/opencode/plugin-composition.ts +++ b/src/platform/opencode/plugin-composition.ts @@ -160,12 +160,7 @@ export function createFlowPlugin(dependencies: { signal: controller.signal, })) { validation.observeShellEvent(event); - if ( - event.type === "session.deleted" || - event.type === "session.execution.succeeded" || - event.type === "session.execution.failed" || - event.type === "session.execution.interrupted" - ) + if (event.type === "session.deleted") validation.cancel(event.data.sessionID); } if (!controller.signal.aborted) @@ -210,6 +205,9 @@ export function createFlowPlugin(dependencies: { runtimeGuard, log, codingModels, + onFlowTerminal: (host) => { + validation.cancel(host); + }, ...(dependencies.defaultRecovery ? { defaultRecovery: dependencies.defaultRecovery } : {}), @@ -244,36 +242,29 @@ export function createFlowPlugin(dependencies: { guardTools(tools, runtimeGuard, autoDrive, (sessionID) => ctx.session.get({ sessionID }), ), - (context) => activeLifecycle.beforeTool(context), + async (context) => { + await activeLifecycle.verifyTool(context, "flow"); + }, ); cleanups.push(() => registration.dispose()); - const before = await ctx.tool.hook("execute.before", (input) => - validation.observeToolBefore(input), - ); + const before = await ctx.tool.hook("execute.before", async (input) => { + if (input.tool !== "shell" || validation.pendingCount() === 0) return; + const access = await activeLifecycle.verifyTool( + input, + "native", + input.tool, + ); + if (access.kind === "flow") + await validation.observeToolBefore(input, access.assertCurrent); + }); cleanups.push(() => before.dispose()); const after = await ctx.tool.hook("execute.after", async (input) => { try { await validation.observeToolAfter(input); } catch (error) { - validation.cancel(input.sessionID); log("error", "Flow validation capture failed closed.", { message: String(error), }); - if (input.status === "completed") { - const content = input.result.content; - input.result = { - ...input.result, - content: [ - ...(typeof content === "string" - ? [{ type: "text" as const, text: content }] - : (content ?? [])), - { - type: "text", - text: `[flow-validation-error] ${String(error)}`, - }, - ], - }; - } } }); cleanups.push(() => after.dispose()); diff --git a/src/platform/opencode/validation-capture.ts b/src/platform/opencode/validation-capture.ts index 82aeb9bf..b6a8edf0 100644 --- a/src/platform/opencode/validation-capture.ts +++ b/src/platform/opencode/validation-capture.ts @@ -43,6 +43,8 @@ type PendingCapture = PreparedValidation & messageID: string; agent: string; startedAt: number; + assertCurrent(): void; + phase: "running" | "recording"; } | null; priorReport: { digest: string; modifiedMs: number } | null; }; @@ -52,6 +54,7 @@ type ValidationCaptureOptions = Readonly<{ persistObservation: ( workspace: string, observation: ObservedValidation, + assertAuthorized: () => void, ) => Promise; readReport?: | (( @@ -393,22 +396,41 @@ export class ValidationCaptureCoordinator { this.#shellRecords.clear(); } - async observeToolBefore(input: NativeToolBefore): Promise { + #assertCurrent(capture: PendingCapture): void { + if (this.#pending.get(capture.sessionID) !== capture || !capture.execution) + throw new ValidationCaptureError( + "Flow validation capture was superseded.", + ); + capture.execution.assertCurrent(); + } + #remove(capture: PendingCapture): void { + if (this.#pending.get(capture.sessionID) === capture) + this.#pending.delete(capture.sessionID); + } + + async observeToolBefore( + input: NativeToolBefore, + assertCurrent: () => void, + ): Promise { this.#prune(); if (input.tool !== "shell") return; const capture = this.#pending.get(input.sessionID); if (!capture || capture.execution) return; + assertCurrent(); const args = ShellInput.safeParse(input.input); capture.execution = { id: input.id, messageID: input.messageID, agent: input.agent, startedAt: this.#now(), + assertCurrent, + phase: "running", }; try { const identity = SessionIdentity.parse( await this.#readSession(input.sessionID), ); + this.#assertCurrent(capture); if ( !args.success || args.data.command !== capture.command || @@ -428,17 +450,26 @@ export class ValidationCaptureCoordinator { "The shell identity, workspace, or command did not match the armed validation.", ); } catch (error) { - this.#pending.delete(input.sessionID); + this.#remove(capture); throw new ValidationCaptureError( "The next shell command did not match the armed validation; capture was cancelled.", { cause: error }, ); } - if (capture.resultsPath && capture.assertions.length > 0) { - const report = await this.#report(capture.workspace, capture.resultsPath); - capture.priorReport = report - ? { digest: digest(report.text), modifiedMs: report.modifiedMs } - : null; + try { + if (capture.resultsPath && capture.assertions.length > 0) { + const report = await this.#report( + capture.workspace, + capture.resultsPath, + ); + this.#assertCurrent(capture); + capture.priorReport = report + ? { digest: digest(report.text), modifiedMs: report.modifiedMs } + : null; + } + } catch (error) { + this.#remove(capture); + throw error; } } @@ -452,104 +483,126 @@ export class ValidationCaptureCoordinator { if ( !capture || !execution || + execution.phase !== "running" || execution.id !== input.id || execution.messageID !== input.messageID || execution.agent !== input.agent ) return null; - this.#pending.delete(input.sessionID); - const args = ShellInput.safeParse(input.input); - const identity = SessionIdentity.parse( - await this.#readSession(input.sessionID), - ); - if ( - !args.success || - args.data.command !== capture.command || - identity.id !== input.sessionID || - identity.agent !== input.agent || - identity.parentID !== undefined || - assertMutableWorkspaceRoot(identity.location.directory) !== - capture.workspace || - realpathSync( - resolve(identity.location.directory, args.data.workdir ?? ""), - ) !== capture.workspace - ) - throw new ValidationCaptureError( - "The executed shell command or workspace changed after Flow armed it.", + execution.phase = "recording"; + try { + this.#assertCurrent(capture); + const args = ShellInput.safeParse(input.input); + const identity = SessionIdentity.parse( + await this.#readSession(input.sessionID), ); - const observed = observeShell(input); - const observedExit = observed.kind === "exited" ? observed.exit : null; - const observedComplete = - observed.kind === "exited" ? observed.complete : null; - const fullOutput = await this.#completeSpilledOutput( - capture, - input, - observed, - ); - const outputComplete = observedComplete === true || fullOutput !== null; - const hostGap: ValidationIneligibleReason | null = - observedExit === null - ? "exit-code-unavailable" - : observedComplete === null - ? "output-completeness-unknown" - : null; + this.#assertCurrent(capture); + if ( + !args.success || + args.data.command !== capture.command || + identity.id !== input.sessionID || + identity.agent !== input.agent || + identity.parentID !== undefined || + assertMutableWorkspaceRoot(identity.location.directory) !== + capture.workspace || + realpathSync( + resolve(identity.location.directory, args.data.workdir ?? ""), + ) !== capture.workspace + ) + throw new ValidationCaptureError( + "The executed shell command or workspace changed after Flow armed it.", + ); + const observed = observeShell(input); + const observedExit = observed.kind === "exited" ? observed.exit : null; + const observedComplete = + observed.kind === "exited" ? observed.complete : null; + const fullOutput = await this.#completeSpilledOutput( + capture, + input, + observed, + ); + this.#assertCurrent(capture); + const outputComplete = observedComplete === true || fullOutput !== null; + const hostGap: ValidationIneligibleReason | null = + observedExit === null + ? "exit-code-unavailable" + : observedComplete === null + ? "output-completeness-unknown" + : null; - const observedAssertions = await this.#observeAssertions( - capture, - this.#now(), - ); - const observation = await this.#persist(capture.workspace, { - featureId: capture.featureId, - runId: capture.runId, - command: capture.command, - scope: capture.scope, - sourceDigest: capture.sourceDigest, - hostPlatform: capture.hostPlatform, - intent: capture.intent, - assertions: capture.assertions, - resultsPath: capture.resultsPath, - ...(observedAssertions.length > 0 ? { observedAssertions } : {}), - captureId: capture.captureId, - exitCode: observedExit, - outputDigest: digest(fullOutput ?? observed.text), - outputComplete, - ...(hostGap ? { ineligibleReason: hostGap } : {}), - }); - const outcome = validationContinuationOutcome(capture, observation); - const receipt = `\n\n[flow-validation] ${JSON.stringify({ - id: observation.id, - scope: observation.scope, - intent: observation.intent ?? capture.intent, - passed: outcome === "passed", - observed: outcome === "observed", - recordedRevision: observation.recordedRevision, - ...(observation.observedAssertions - ? { assertions: observation.observedAssertions } - : {}), - ...(observation.ineligibleReason - ? { ineligibleReason: observation.ineligibleReason } - : {}), - ...(fullOutput ? { fullOutputDigest: observation.outputDigest } : {}), - })}`; - if (input.status === "completed") - input.result = { - ...input.result, - content: [{ type: "text", text: textContent(input) + receipt }], - }; - if (capture.origin) { - try { - this.#onRecorded?.({ - origin: capture.origin, + const observedAssertions = await this.#observeAssertions( + capture, + this.#now(), + ); + this.#assertCurrent(capture); + const observation = await this.#persist( + capture.workspace, + { + featureId: capture.featureId, + runId: capture.runId, + command: capture.command, + scope: capture.scope, + sourceDigest: capture.sourceDigest, + hostPlatform: capture.hostPlatform, + intent: capture.intent, + assertions: capture.assertions, + resultsPath: capture.resultsPath, + ...(observedAssertions.length > 0 ? { observedAssertions } : {}), captureId: capture.captureId, - observation, - }); - } catch (error) { + exitCode: observedExit, + outputDigest: digest(fullOutput ?? observed.text), + outputComplete, + ...(hostGap ? { ineligibleReason: hostGap } : {}), + }, + () => this.#assertCurrent(capture), + ); + this.#assertCurrent(capture); + const outcome = validationContinuationOutcome(capture, observation); + const receipt = `\n\n[flow-validation] ${JSON.stringify({ + id: observation.id, + scope: observation.scope, + intent: observation.intent ?? capture.intent, + passed: outcome === "passed", + observed: outcome === "observed", + recordedRevision: observation.recordedRevision, + ...(observation.observedAssertions + ? { assertions: observation.observedAssertions } + : {}), + ...(observation.ineligibleReason + ? { ineligibleReason: observation.ineligibleReason } + : {}), + ...(fullOutput ? { fullOutputDigest: observation.outputDigest } : {}), + })}`; + if (input.status === "completed") + input.result = { + ...input.result, + content: [{ type: "text", text: textContent(input) + receipt }], + }; + if (capture.origin) { try { - this.#onReceiptError?.(error); - } catch {} + this.#onRecorded?.({ + origin: capture.origin, + captureId: capture.captureId, + observation, + }); + } catch (error) { + try { + this.#onReceiptError?.(error); + } catch {} + } + } + return observation; + } catch (error) { + try { + this.#assertCurrent(capture); + } catch { + return null; } + + throw error; + } finally { + this.#remove(capture); } - return observation; } pendingCount(): number { diff --git a/tests/auto-drive.test.ts b/tests/auto-drive.test.ts index 64e784f7..028c2913 100644 --- a/tests/auto-drive.test.ts +++ b/tests/auto-drive.test.ts @@ -2147,7 +2147,7 @@ describe("authenticated validation continuation", () => { command, validationOrigin.assistantId, ); - await capture.observeToolBefore(execution); + await capture.observeToolBefore(execution, () => {}); await capture.observeToolAfter({ ...execution, status: "completed", @@ -2736,7 +2736,7 @@ describe("authenticated validation continuation", () => { "check", validationOrigin.assistantId, ); - await capture.observeToolBefore(execution); + await capture.observeToolBefore(execution, () => {}); const completing = capture.observeToolAfter({ ...execution, status: "completed", diff --git a/tests/execution-attribution.test.ts b/tests/execution-attribution.test.ts index dbb58aa9..9c961665 100644 --- a/tests/execution-attribution.test.ts +++ b/tests/execution-attribution.test.ts @@ -79,12 +79,25 @@ const execution = { messageID: SessionMessage.ID.make("msg_assistant1"), id: CallID.make("call-1"), }; +const clock = { current: () => true, assertCurrent: () => {} }; +function flowOrigin( + primary: ReturnType, +): InputOrigin { + if (primary.participation.kind !== "flow") + throw new Error("Expected Flow participation."); + return primary.participation.origin; +} function fixture() { let live = true; const attribution = new ExecutionAttribution(() => { if (!live) throw new Error("revoked epoch"); }); - const primary = attribution.freeze(context, [user], origin); + const primary = attribution.freeze( + context, + [user], + { kind: "flow", origin: origin }, + clock, + ); const request = new Request("http://localhost/primary"); attribution.request({ sessionID: sid, @@ -105,8 +118,15 @@ function fixture() { test("native exact execution binds once and a revoked epoch vetoes its second boundary", () => { const f = fixture(); expect( - f.attribution.verify(execution, [user, assistant], session, "/tmp", "shell") - .primary.origin.id, + flowOrigin( + f.attribution.verify( + execution, + [user, assistant], + session, + "/tmp", + "shell", + ).primary, + ).id, ).toBe("user-1"); f.revoke(); expect(() => @@ -143,9 +163,14 @@ test("native full prefix, exact call, agent, and workspace are required", () => }); test("overlapping primary context is refused", () => { const f = fixture(); - expect(() => f.attribution.freeze(context, [user], origin)).toThrow( - "overlap", - ); + expect(() => + f.attribution.freeze( + context, + [user], + { kind: "flow", origin: origin }, + clock, + ), + ).toThrow("overlap"); }); test("response must join the same Request object", () => { const f = fixture(); @@ -176,7 +201,12 @@ test("an old assistant is rejected after a later context", async () => { }; f.attribution.response(response, () => {}); expect(await response.response.text()).toBe("data"); - f.attribution.freeze(context, [user, assistant], origin); + f.attribution.freeze( + context, + [user, assistant], + { kind: "flow", origin: origin }, + clock, + ); f.attribution.request({ sessionID: sid, agent, @@ -244,7 +274,7 @@ test("a failed HTTP response does not leave the next fresh request overlapping", const attribution = new ExecutionAttribution((candidate) => { if (candidate.id !== current) throw new Error("revoked origin"); }); - attribution.freeze(context, [user], origin); + attribution.freeze(context, [user], { kind: "flow", origin: origin }, clock); const request = new Request("http://localhost/failed"); attribution.request({ sessionID: sid, @@ -286,7 +316,12 @@ test("a failed HTTP response does not leave the next fresh request overlapping", ], }; const nextOrigin: InputOrigin = { ...origin, id: "user-2" }; - attribution.freeze(nextContext, [user, nextUser], nextOrigin); + attribution.freeze( + nextContext, + [user, nextUser], + { kind: "flow", origin: nextOrigin }, + clock, + ); attribution.request({ sessionID: sid, agent, @@ -295,13 +330,15 @@ test("a failed HTTP response does not leave the next fresh request overlapping", request: new Request("http://localhost/fresh"), }); expect( - attribution.verify( - execution, - [user, nextUser, assistant], - session, - "/tmp", - "shell", - ).primary.origin.id, + flowOrigin( + attribution.verify( + execution, + [user, nextUser, assistant], + session, + "/tmp", + "shell", + ).primary, + ).id, ).toBe("user-2"); }); @@ -311,7 +348,7 @@ function failedStreamFixture() { const attribution = new ExecutionAttribution((candidate) => { if (candidate.id !== direction) throw new Error("revoked stream origin"); }); - attribution.freeze(context, [user], origin); + attribution.freeze(context, [user], { kind: "flow", origin: origin }, clock); const request = new Request("http://localhost/stream"); attribution.request({ sessionID: sid, @@ -324,8 +361,8 @@ function failedStreamFixture() { attribution, request, failures, - fail(host: string) { - failures.push(host); + fail(primary: ReturnType) { + failures.push(primary.sessionID); direction = "user-2"; }, steer() { @@ -360,7 +397,12 @@ function failedStreamFixture() { }; const nextOrigin: InputOrigin = { ...origin, id: nextUser.id }; const native: SessionMessageInfo[] = [user, oldAssistant, nextUser]; - attribution.freeze(nextContext, native, nextOrigin); + attribution.freeze( + nextContext, + native, + { kind: "flow", origin: nextOrigin }, + clock, + ); attribution.request({ sessionID: sid, agent, @@ -371,13 +413,15 @@ function failedStreamFixture() { const freshID = SessionMessage.ID.make("msg_fresh"); const freshAssistant: SessionMessageInfo = { ...assistant, id: freshID }; return () => - attribution.verify( - { ...execution, messageID: freshID }, - [...native, freshAssistant], - session, - "/tmp", - "shell", - ).primary.origin.id; + flowOrigin( + attribution.verify( + { ...execution, messageID: freshID }, + [...native, freshAssistant], + session, + "/tmp", + "shell", + ).primary, + ).id; }, }; } @@ -510,13 +554,25 @@ test("consumer cancellation ends its captured resource without revoking the exac if (!input.response.body) throw new Error("Missing wrapped body."); await input.response.body.cancel("protocol consumer stopped"); expect(f.primary.http.phase).toBe("ended"); - expect(f.primary.origin).toBe(origin); + expect(flowOrigin(f.primary)).toBe(origin); expect( - f.attribution.verify(execution, [user, assistant], session, "/tmp", "shell") - .primary.origin.id, + flowOrigin( + f.attribution.verify( + execution, + [user, assistant], + session, + "/tmp", + "shell", + ).primary, + ).id, ).toBe("user-1"); expect(failures).toBe(0); - f.attribution.freeze(context, [user, assistant], origin); + f.attribution.freeze( + context, + [user, assistant], + { kind: "flow", origin: origin }, + clock, + ); const nextRequest = new Request("http://localhost/next-same-origin"); f.attribution.request({ sessionID: sid, @@ -528,13 +584,15 @@ test("consumer cancellation ends its captured resource without revoking the exac const nextID = SessionMessage.ID.make("msg_second"); const nextAssistant: SessionMessageInfo = { ...assistant, id: nextID }; expect( - f.attribution.verify( - { ...execution, messageID: nextID }, - [user, assistant, nextAssistant], - session, - "/tmp", - "shell", - ).primary.origin, + flowOrigin( + f.attribution.verify( + { ...execution, messageID: nextID }, + [user, assistant, nextAssistant], + session, + "/tmp", + "shell", + ).primary, + ), ).toBe(origin); expect(f.attribution.requestOwner(nextRequest)).toBe( f.attribution.current(sid), @@ -569,15 +627,29 @@ test("consumer cancellation awaits cleanup rejection without declaring an epoch expect(settled).toBe(false); expect(f.primary.http.phase).toBe("ended"); expect( - f.attribution.verify(execution, [user, assistant], session, "/tmp", "shell") - .primary.origin.id, + flowOrigin( + f.attribution.verify( + execution, + [user, assistant], + session, + "/tmp", + "shell", + ).primary, + ).id, ).toBe("user-1"); cleanup.reject(new Error("native cancel cleanup failed")); await expect(cancel).rejects.toThrow("native cancel cleanup failed"); expect(failures).toBe(0); expect( - f.attribution.verify(execution, [user, assistant], session, "/tmp", "shell") - .primary.origin.id, + flowOrigin( + f.attribution.verify( + execution, + [user, assistant], + session, + "/tmp", + "shell", + ).primary, + ).id, ).toBe("user-1"); }); test("a late captured EOF cannot retire a separately steered fresh primary", async () => { @@ -647,13 +719,15 @@ for (const kind of ["compaction", "title", "generate"] as const) if (!primary.response.body) throw new Error("Missing primary body"); await primary.response.body.cancel(); expect( - f.attribution.verify( - execution, - [user, assistant], - session, - "/tmp", - "shell", - ).primary.origin.id, + flowOrigin( + f.attribution.verify( + execution, + [user, assistant], + session, + "/tmp", + "shell", + ).primary, + ).id, ).toBe("user-1"); expect(failures).toBe(0); }); @@ -696,23 +770,20 @@ test("resource end grants no same-origin retry and cannot revive an explicitly r ), ).toThrow("revoked epoch"); expect(() => - f.attribution.freeze(context, [user, assistant], origin), + f.attribution.freeze( + context, + [user, assistant], + { kind: "flow", origin: origin }, + clock, + ), ).toThrow("revoked epoch"); expect(failures).toBe(0); }); -function retainedSnapshot(primary: { - previousAssistants?: ReadonlySet; - lifetime?: { - kind: string; - snapshot?: { previousAssistants: ReadonlySet }; - }; -}) { - return primary.lifetime?.kind === "current" +function retainedSnapshot(primary: ReturnType) { + return primary.lifetime.kind === "current" ? primary.lifetime.snapshot - : primary.previousAssistants - ? { previousAssistants: primary.previousAssistants } - : undefined; + : undefined; } test("64 held owners retain one current payload with 63 historical assistant IDs", () => { @@ -721,7 +792,12 @@ test("64 held owners retain one current payload with 63 historical assistant IDs const owners: ReturnType[] = []; const requests: Request[] = []; for (let index = 0; index < 64; index++) { - const primary = attribution.freeze(context, native, origin); + const primary = attribution.freeze( + context, + native, + { kind: "flow", origin: origin }, + clock, + ); const request = new Request("http://localhost/retention"); attribution.request({ sessionID: sid, @@ -780,12 +856,24 @@ test("clear retires evidence even when callers hold owners and Request keys", () f.attribution.clear(); expect(retainedSnapshot(f.primary)).toBeUndefined(); expect(f.attribution.requestOwner(f.request)).toBe(f.primary); - expect(() => f.attribution.freeze(context, [user], origin)).toThrow("closed"); + expect(() => + f.attribution.freeze( + context, + [user], + { kind: "flow", origin: origin }, + clock, + ), + ).toThrow("closed"); }); test("end before request admission cannot prove a running tool", () => { const attribution = new ExecutionAttribution(() => {}); - const primary = attribution.freeze(context, [user], origin); + const primary = attribution.freeze( + context, + [user], + { kind: "flow", origin: origin }, + clock, + ); attribution.end(primary); expect(() => attribution.verify(execution, [user, assistant], session, "/tmp", "shell"), @@ -803,7 +891,14 @@ test("invalid replacement preserves the exact prior current payload and tool", ( model: { ...model, variant: Model.VariantID.make("changed") }, }, ]) - expect(() => f.attribution.freeze(input, [user], origin)).toThrow(); + expect(() => + f.attribution.freeze( + input, + [user], + { kind: "flow", origin: origin }, + clock, + ), + ).toThrow(); expect(f.attribution.current(sid)).toBe(f.primary); expect(retainedSnapshot(f.primary)?.previousAssistants.size).toBe(0); expect( @@ -815,7 +910,12 @@ test("invalid replacement preserves the exact prior current payload and tool", ( test("permitted retry shares exclusions and admits the retry assistant's first tool", () => { const attribution = new ExecutionAttribution(() => {}); const excluded = { ...assistant, id: "msg_excluded" }; - const first = attribution.freeze(context, [user, excluded], origin); + const first = attribution.freeze( + context, + [user, excluded], + { kind: "flow", origin: origin }, + clock, + ); attribution.request({ sessionID: sid, agent, @@ -834,7 +934,12 @@ test("permitted retry shares exclusions and admits the retry assistant's first t }; const exclusions = retainedSnapshot(first)?.previousAssistants; expect(attribution.retry(first, [user, excluded, retry])).toBe(true); - const next = attribution.freeze(context, [user, excluded, retry], origin); + const next = attribution.freeze( + context, + [user, excluded, retry], + { kind: "flow", origin: origin }, + clock, + ); expect(retainedSnapshot(next)?.previousAssistants).toBe(exclusions); expect(retainedSnapshot(first)).toBeUndefined(); attribution.request({ @@ -862,9 +967,7 @@ test("permitted retry shares exclusions and admits the retry assistant's first t }), ).toBeUndefined(); expect(() => attribution.retry(first, [user, retry])).toThrow("superseded"); - expect(() => attribution.hasFailedTerminal(first, [user])).toThrow( - "superseded", - ); + expect(() => attribution.terminal(first, [user])).toThrow("superseded"); }); test("known bound and excluded assistants preserve identity after a shorter prefix", () => { @@ -875,10 +978,16 @@ test("known bound and excluded assistants preserve identity after a shorter pref const middle = f.attribution.freeze( context, [user, assistant, historical], - origin, + { kind: "flow", origin: origin }, + clock, ); f.attribution.end(middle); - const next = f.attribution.freeze(context, [user], origin); + const next = f.attribution.freeze( + context, + [user], + { kind: "flow", origin: origin }, + clock, + ); f.attribution.request({ sessionID: sid, agent, @@ -930,7 +1039,8 @@ test("deletion keeps exact Request ownership, rejects replay, and leaves another const other = f.attribution.freeze( { ...context, sessionID: otherID }, [user], - origin, + { kind: "flow", origin: origin }, + clock, ); f.attribution.request({ sessionID: otherID, @@ -946,9 +1056,14 @@ test("deletion keeps exact Request ownership, rejects replay, and leaves another expect(() => f.attribution.executionOwner(execution)).toThrow( "session was deleted", ); - expect(() => f.attribution.freeze(context, [user], origin)).toThrow( - "session was deleted", - ); + expect(() => + f.attribution.freeze( + context, + [user], + { kind: "flow", origin: origin }, + clock, + ), + ).toThrow("session was deleted"); expect(() => f.attribution.verify( execution, @@ -1054,3 +1169,115 @@ test("one native call cannot change tool names after its immutable binding", () ), ).toBe(binding); }); + +test("an unknown response cannot end or fail the current Request owner", async () => { + const f = fixture(); + const failures: unknown[] = []; + expect(() => + f.attribution.response( + { + sessionID: sid, + agent, + model, + kind: "primary", + request: new Request("http://localhost/unknown"), + response: new Response("unbound"), + }, + (owner) => failures.push(owner), + ), + ).toThrow("Request identity"); + expect(failures).toEqual([]); + expect(f.primary.http.phase).toBe("request"); + const response = { + sessionID: sid, + agent, + model, + kind: "primary" as const, + request: f.request, + response: new Response("still healthy"), + }; + f.attribution.response(response, (owner) => failures.push(owner)); + expect(await response.response.text()).toBe("still healthy"); + expect(failures).toEqual([]); +}); + +for (const barrier of ["delete", "clear"] as const) + test(`growing native-only user prefixes detach from held Request keys on ${barrier}`, () => { + const attribution = new ExecutionAttribution(() => { + throw new Error("Foreign input must never assert an origin."); + }); + const native: SessionMessageInfo[] = []; + const messages: SessionContext["messages"] = []; + const requests: Request[] = []; + const owners: ReturnType[] = []; + for (let index = 0; index < 64; index++) { + const id = `user-growing-${index}`; + native.push({ + type: "user", + id, + text: `Ordinary ${index}`, + time: { created: index }, + }); + messages.push({ + role: "user", + id, + content: [{ type: "text", text: `Ordinary ${index}` }], + }); + const owner = attribution.freeze( + { ...context, messages }, + native, + { kind: "native-only" }, + clock, + ); + const request = new Request(`http://localhost/growing/${index}`); + attribution.request({ + sessionID: sid, + agent, + model, + kind: "primary", + request, + }); + const message = { ...assistant, id: `msg_growing_${index}` }; + native.push(message); + expect( + attribution.verify( + { ...execution, messageID: SessionMessage.ID.make(message.id) }, + native, + session, + "/tmp", + "shell", + ).primary, + ).toBe(owner); + attribution.end(owner); + owners.push(owner); + requests.push(request); + } + const snapshots = owners.flatMap((owner) => retainedSnapshot(owner) ?? []); + expect(snapshots.length).toBe(1); + expect(snapshots[0]?.inputs.length).toBe(64); + expect(snapshots[0]?.previousAssistants.size).toBe(63); + for (const [index, owner] of owners.entries()) { + const request = requests[index]; + if (!request) throw new Error("Expected held Request."); + expect(attribution.requestOwner(request)).toBe(owner); + expect(owner.participation).toEqual({ kind: "native-only" }); + expect(Object.keys(owner).sort()).toEqual([ + "agent", + "http", + "lifetime", + "model", + "participation", + "sessionID", + ]); + if (index !== 63) expect(owner.lifetime).toEqual({ kind: "retired" }); + } + if (barrier === "delete") attribution.deleteSession(sid); + else attribution.clear(); + expect(owners.flatMap((owner) => retainedSnapshot(owner) ?? [])).toEqual( + [], + ); + for (const request of requests) + expect(attribution.requestOwner(request)?.lifetime).toEqual({ + kind: "retired", + }); + }); diff --git a/tests/fixtures/verified-continuation-origin.ts b/tests/fixtures/verified-continuation-origin.ts index 154ddd02..2ab8b409 100644 --- a/tests/fixtures/verified-continuation-origin.ts +++ b/tests/fixtures/verified-continuation-origin.ts @@ -96,7 +96,12 @@ export async function verifiedContinuationOrigin( const evidence = await inputs.evidence(scope, [user], delivery); if (evidence.kind !== "live") throw new Error("Missing accepted fixture origin."); - attribution.freeze(context, [user], evidence.origin); + attribution.freeze( + context, + [user], + { kind: "flow", origin: evidence.origin }, + scope, + ); attribution.request({ sessionID, agent, @@ -137,9 +142,12 @@ export async function verifiedContinuationOrigin( process.cwd(), "flow_status", ); - auto.observeAssistant(host, assistantID, verified.primary.origin.id); + if (verified.primary.participation.kind !== "flow") + throw new Error("Expected Flow fixture participation."); + const origin = verified.primary.participation.origin; + auto.observeAssistant(host, assistantID, origin.id); scope.release(); inputs.clear(); attribution.clear(); - return verified.primary.origin.id; + return origin.id; } diff --git a/tests/input-admission.test.ts b/tests/input-admission.test.ts index 8ea02935..00b5e468 100644 --- a/tests/input-admission.test.ts +++ b/tests/input-admission.test.ts @@ -423,10 +423,70 @@ for (const aliasDepth of [0, 2]) const scope = inputs.capture(input); expect(inputs.positive(scope)).toBeUndefined(); expect(await inputs.evidence(scope, native, delivery)).toEqual({ - kind: "retired-automatic", + kind: "unregistered", + lineage: "retired-automatic", }); scope.release(); } finally { inputs.clear(); } }); + +for (const state of ["pending", "rejected", "accepted"] as const) + test(`missing native input preserves ${state} frontier lineage without creating an origin`, async () => { + const f = fixture(); + try { + if (state === "accepted") + await f.inputs.accept(f.admission, f.receipt, async () => f.origin()); + if (state === "rejected") + f.inputs.reject(f.admission, new Error("rejected receipt")); + const input = { + ...context("foreign"), + messages: [ + ...context("user-1").messages, + ...context("foreign").messages, + ], + }; + const scope = f.inputs.capture(input); + expect( + await f.inputs.evidence( + scope, + [ + { + type: "synthetic", + id: "foreign", + text: "external", + time: { created: 1 }, + }, + ], + delivery, + ), + ).toEqual({ + kind: "unregistered", + lineage: state === "accepted" ? "retired-ordinary" : "rejected", + }); + expect(f.inputs.positive(scope)).toBeUndefined(); + scope.release(); + const old = f.inputs.capture(context("user-1")); + if (state === "accepted") + expect( + ( + await f.inputs.evidence( + old, + [ + { + type: "user", + id: "user-1", + text: "Approved task", + time: { created: 0 }, + }, + ], + delivery, + ) + ).kind, + ).toBe("live"); + old.release(); + } finally { + f.close(); + } + }); diff --git a/tests/lifecycle.test.ts b/tests/lifecycle.test.ts index 4a304d7c..744cc1ee 100644 --- a/tests/lifecycle.test.ts +++ b/tests/lifecycle.test.ts @@ -17,6 +17,10 @@ import { AbsolutePath } from "@opencode/schema/schema"; import { Session } from "@opencode/schema/session"; import { SessionMessage } from "@opencode/schema/session-message"; import { createFlowService } from "../src/application/flow-service.js"; +import type { + ObservedValidation, + PreparedValidation, +} from "../src/application/prepare-validation.js"; import { RecoveryController } from "../src/application/recovery-policy.js"; import type { FlowCodingModel } from "../src/config-shared.js"; import { @@ -26,8 +30,13 @@ import { } from "../src/platform/opencode/auto-drive.js"; import { ExecutionAttribution } from "../src/platform/opencode/execution-attribution.js"; import { registerFlowLifecycle } from "../src/platform/opencode/lifecycle.js"; -import type { NativeToolDefinition } from "../src/platform/opencode/sdk.js"; +import type { + NativeToolAfter, + NativeToolBefore, + NativeToolDefinition, +} from "../src/platform/opencode/sdk.js"; import { registerFlowTools } from "../src/platform/opencode/tools.js"; +import { ValidationCaptureCoordinator } from "../src/platform/opencode/validation-capture.js"; import { deterministicEnvironment, MemorySessionRepository, @@ -62,7 +71,10 @@ function hookSets(): HookSets { retry: new Set(), }; } -async function fixture(selectedAgent = agent) { +async function fixture( + selectedAgent = agent, + onFlowTerminal?: (host: string) => void, +) { const hooks = hookSets(); const commands = new Map(); const registrations: string[] = []; @@ -85,6 +97,7 @@ async function fixture(selectedAgent = agent) { nextAction: "flow_plan_approve", }; const logs: string[] = []; + const flowTerminals: string[] = []; const codingModels = new Map(); const repository = new MemorySessionRepository(); const flow = createFlowService(repository, deterministicEnvironment()); @@ -333,6 +346,10 @@ async function fixture(selectedAgent = agent) { recovery, flow, codingModels, + onFlowTerminal: (host) => { + flowTerminals.push(host); + onFlowTerminal?.(host); + }, runtimeGuard: { query: () => ({ operational: true, @@ -379,6 +396,7 @@ async function fixture(selectedAgent = agent) { hooks, commands, logs, + flowTerminals, native, session, children, @@ -584,14 +602,16 @@ test("new real input vetoes an old automatic context and a stale native shell", signal: new AbortController().signal, progress: async () => {}, }; - await f.lifecycle.beforeTool(execution); + await f.lifecycle.verifyTool(execution, "flow"); await f.emit("prompt", { sessionID, messageID: SessionMessage.ID.make("msg_stop"), prompt: { text: "Stop and change direction" }, delivery: "steer", }); - await expect(f.lifecycle.beforeTool(execution)).rejects.toThrow("epoch"); + await expect(f.lifecycle.verifyTool(execution, "flow")).rejects.toThrow( + "epoch", + ); await expect(f.emit("context", f.context())).rejects.toThrow(); } finally { await f.lifecycle.dispose(); @@ -804,7 +824,7 @@ test("an unmatched control callback rejects without owning the stored checkpoint expect(f.repository.saveCount).toBe(writes); f.native.splice(0, f.native.length, ...valid); await finishOwnedResponse(f, await startOwnedRequest(f)); - await f.lifecycle.beforeTool(ownershipNativeCall(f)); + await f.lifecycle.verifyTool(ownershipNativeCall(f), "flow"); expect(freeze).toHaveBeenCalledTimes(1); expect(f.autoDrive.captureAuthority(sessionID)).toEqual(authority); } finally { @@ -872,14 +892,17 @@ test("real-input revocation invalidates an already acquired recovery guard witho }, ], }); - await f.lifecycle.beforeTool({ - sessionID, - agent, - messageID, - id, - signal: new AbortController().signal, - progress: async () => {}, - }); + await f.lifecycle.verifyTool( + { + sessionID, + agent, + messageID, + id, + signal: new AbortController().signal, + progress: async () => {}, + }, + "flow", + ); const guard = f.recovery.guard({ hostSessionId: sessionID, messageId: messageID, @@ -930,6 +953,12 @@ test("duplicate succeeded idle notifications dispatch only one continuation", as }); f.session.outcome = "succeeded"; f.session.time.idle = 3; + f.native.push({ + type: "idle", + id: "msg_idle", + outcome: "succeeded", + time: { created: 3 }, + }); const event: OpenCodeEvent = { id: "evt_idle", created: 3, @@ -958,7 +987,7 @@ test.each(["failed", "interrupted"] as const)( await f.command("flow-auto", "Inspect this project and create a plan"); await f.emit("context", f.context()); f.native.push({ - id: "msg_terminal", + id: SessionMessage.ID.fromEvent(`evt_${outcome}`), type: "idle", time: { created: 1 }, outcome, @@ -1018,6 +1047,12 @@ test("unload aborts an idle-triggered native admission and waits for subscriptio f.blockAdmission(); f.session.outcome = "succeeded"; f.session.time.idle = 3; + f.native.push({ + type: "idle", + id: "msg_idle", + outcome: "succeeded", + time: { created: 3 }, + }); f.sendEvent({ id: "evt_idle", created: 3, @@ -1034,7 +1069,7 @@ test("unload aborts an idle-triggered native admission and waits for subscriptio f.releaseAdmission(); }); -test("native execution success dispatches from the session watermark without an idle context record", async () => { +test("native execution success waits for exact terminal proof before dispatching once", async () => { const f = await fixture(); try { await f.command("flow-auto", "Implement the approved change"); @@ -1061,6 +1096,18 @@ test("native execution success dispatches from the session watermark without an }; f.sendEvent(event); await f.flush(); + expect(f.native.filter((message) => message.type === "synthetic")).toEqual( + [], + ); + expect(f.native.some((message) => message.type === "idle")).toBe(false); + f.native.push({ + type: "idle", + id: SessionMessage.ID.fromEvent(event.id), + outcome: "succeeded", + time: { created: 3 }, + }); + f.sendEvent(event); + await f.flush(); expect( f.native .filter((message) => message.type === "synthetic") @@ -1073,7 +1120,6 @@ test("native execution success dispatches from the session watermark without an .filter((message) => message.type === "synthetic") .map((message) => message.type), ).toEqual(["synthetic"]); - expect(f.native.some((message) => message.type === "idle")).toBe(false); } finally { await f.lifecycle.dispose(); } @@ -1359,6 +1405,12 @@ test("a delayed terminal session read cannot revoke a replacement primary", asyn await finishPrimary(f); f.session.outcome = "failed"; f.session.time.idle = 1; + f.native.push({ + type: "idle", + id: SessionMessage.ID.fromEvent("evt_old_success_notification"), + outcome: "succeeded", + time: { created: 1 }, + }); f.blockNextSessionRead(); f.sendEvent({ id: "evt_old_success_notification", @@ -1476,14 +1528,17 @@ test("native retry keeps its unfinished assistant and late old responses cannot }; await f.emit("http.response", response); expect(await response.response.text()).toBe("retried tool call"); - await f.lifecycle.beforeTool({ - sessionID, - agent, - messageID: assistantID, - id: callID, - signal: new AbortController().signal, - progress: async () => {}, - }); + await f.lifecycle.verifyTool( + { + sessionID, + agent, + messageID: assistantID, + id: callID, + signal: new AbortController().signal, + progress: async () => {}, + }, + "flow", + ); expect(f.autoDrive.captureAuthority(sessionID)).toEqual(authority); } finally { await f.lifecycle.dispose(); @@ -1729,14 +1784,17 @@ for (const kind of ["real", "synthetic", "compaction"] as const) metadata: {}, }, }); - await f.lifecycle.beforeTool({ - sessionID, - agent, - messageID: assistantID, - id: callID, - signal: new AbortController().signal, - progress: async () => {}, - }); + await f.lifecycle.verifyTool( + { + sessionID, + agent, + messageID: assistantID, + id: callID, + signal: new AbortController().signal, + progress: async () => {}, + }, + "flow", + ); expect(f.autoDrive.captureAuthority(sessionID)).toEqual(authority); } finally { await f.lifecycle.dispose(); @@ -1919,7 +1977,7 @@ async function freshOwnershipWork(f: OwnershipFixture) { async verify() { await finishOwnedResponse(f, await startOwnedRequest(f)); const call = ownershipNativeCall(f); - await f.lifecycle.beforeTool(call); + await f.lifecycle.verifyTool(call, "flow"); expect(f.autoDrive.captureAuthority(sessionID)).toEqual(authority); }, }; @@ -1975,7 +2033,7 @@ for (const actor of ["context", "compaction"] as const) if (control) f.native.splice(f.native.indexOf(control), 1); if (request) { await finishOwnedResponse(f, request); - await f.lifecycle.beforeTool(ownershipNativeCall(f)); + await f.lifecycle.verifyTool(ownershipNativeCall(f), "flow"); expect(f.autoDrive.captureAuthority(sessionID)).toEqual( next.authority, ); @@ -2007,7 +2065,7 @@ for (const boundary of ["flow-wrapper", "global-shell"] as const) if (reject) f.rejectNextContextRead(); const pending = boundary === "flow-wrapper" - ? f.lifecycle.beforeTool(call) + ? f.lifecycle.verifyTool(call, "flow") : (async () => { for (const callback of f.toolCallbacks["execute.before"]) await callback({ @@ -2033,7 +2091,7 @@ for (const boundary of ["flow-wrapper", "global-shell"] as const) ); if (request) { await finishOwnedResponse(f, request); - await f.lifecycle.beforeTool(ownershipNativeCall(f)); + await f.lifecycle.verifyTool(ownershipNativeCall(f), "flow"); expect(f.autoDrive.captureAuthority(sessionID)).toEqual( next.authority, ); @@ -2138,6 +2196,12 @@ for (const outcome of ["failed", "succeeded"] as const) f.blockNextContextRead(); f.rejectNextContextRead(); } else { + f.native.push({ + type: "idle", + id: SessionMessage.ID.fromEvent("evt_succeeded"), + outcome, + time: { created: 4 }, + }); f.blockNextSessionRead(); f.rejectNextSessionRead(); } @@ -2202,7 +2266,7 @@ test("stale accepted-message observation cannot overwrite replacement recovery d gate.resolve(); expect(await caught).toBe("Error: Flow execution epoch was revoked."); await finishOwnedResponse(f, request); - await f.lifecycle.beforeTool(ownershipNativeCall(f)); + await f.lifecycle.verifyTool(ownershipNativeCall(f), "flow"); expect(f.autoDrive.captureAuthority(sessionID)).toEqual(next.authority); } finally { gate.resolve(); @@ -2215,13 +2279,13 @@ test("old bound call cannot revoke a later request in the same epoch", async () try { await initialOwnershipWork(f); const call = ownershipNativeCall(f); - await f.lifecycle.beforeTool(call); + await f.lifecycle.verifyTool(call, "flow"); const authority = f.autoDrive.captureAuthority(sessionID); await finishOwnedResponse(f, await startOwnedRequest(f)); - await expect(f.lifecycle.beforeTool(call)).rejects.toThrow( + await expect(f.lifecycle.verifyTool(call, "flow")).rejects.toThrow( "Flow assistant cannot be rebound to another request.", ); - await f.lifecycle.beforeTool(ownershipNativeCall(f)); + await f.lifecycle.verifyTool(ownershipNativeCall(f), "flow"); expect(f.autoDrive.captureAuthority(sessionID)).toEqual(authority); } finally { await f.lifecycle.dispose(); @@ -2253,7 +2317,7 @@ test("late duplicate request hook cannot end its replacement", async () => { request, }); await finishOwnedResponse(f, request); - await f.lifecycle.beforeTool(ownershipNativeCall(f)); + await f.lifecycle.verifyTool(ownershipNativeCall(f), "flow"); expect(f.autoDrive.captureAuthority(sessionID)).toEqual(next.authority); } finally { await f.lifecycle.dispose(); @@ -2314,7 +2378,7 @@ test("stale automatic shell remains vetoed after a manual replacement context", })(); await expect(pending).rejects.toThrow("epoch was revoked"); expect(effects).toBe(0); - await f.lifecycle.beforeTool(ownershipNativeCall(f)); + await f.lifecycle.verifyTool(ownershipNativeCall(f), "flow"); } finally { await f.lifecycle.dispose(); } @@ -2340,7 +2404,7 @@ test("an old success notification cannot advance a newly admitted command before ); expect(f.repository.saveCount).toBe(0); await finishOwnedResponse(f, await startOwnedRequest(f)); - await f.lifecycle.beforeTool(ownershipNativeCall(f)); + await f.lifecycle.verifyTool(ownershipNativeCall(f), "flow"); expect(f.autoDrive.captureAuthority(sessionID)).toEqual(authority); } finally { await f.lifecycle.dispose(); @@ -2392,7 +2456,7 @@ for (const prior of ["none", "manual", "automatic"] as const) status: "completed", }); await finishOwnedResponse(f, await startOwnedRequest(f)); - await f.lifecycle.beforeTool(ownershipNativeCall(f)); + await f.lifecycle.verifyTool(ownershipNativeCall(f), "flow"); expect(f.autoDrive.captureAuthority(sessionID)).toEqual(authority); expect(f.repository.saveCount).toBe(0); } finally { @@ -2454,7 +2518,7 @@ for (const successor of ["command", "synthetic"] as const) } f.native.splice(f.native.indexOf(control), 1); await finishOwnedResponse(f, request); - await f.lifecycle.beforeTool(ownershipNativeCall(f)); + await f.lifecycle.verifyTool(ownershipNativeCall(f), "flow"); expect(f.autoDrive.captureAuthority(sessionID)).toEqual(authority); expect(f.repository.saveCount).toBe(0); } finally { @@ -2549,7 +2613,7 @@ test("pre-primary compaction joins pending receipt without granting early author await pending; f.native.splice(0, f.native.length, { ...control, status: "completed" }); await finishOwnedResponse(f, await startOwnedRequest(f)); - await f.lifecycle.beforeTool(ownershipNativeCall(f)); + await f.lifecycle.verifyTool(ownershipNativeCall(f), "flow"); expect(f.autoDrive.captureAuthority(sessionID)?.token).toBe("local-token"); } finally { f.releaseAdmission(); @@ -2576,7 +2640,7 @@ test("automatic compaction owns the accepted input during its existing primary", await finishOwnedResponse(f, request); f.native.splice(0, f.native.length, { ...control, status: "completed" }); await finishOwnedResponse(f, await startOwnedRequest(f)); - await f.lifecycle.beforeTool(ownershipNativeCall(f)); + await f.lifecycle.verifyTool(ownershipNativeCall(f), "flow"); expect(f.autoDrive.captureAuthority(sessionID)?.token).toBe("local-token"); expect(f.repository.saveCount).toBe(0); } finally { @@ -2600,7 +2664,7 @@ test("another automatic control can precede the first checkpoint primary", async await f.emit("compaction", compact); f.native.splice(0, f.native.length, { ...control, status: "completed" }); await finishOwnedResponse(f, await startOwnedRequest(f)); - await f.lifecycle.beforeTool(ownershipNativeCall(f)); + await f.lifecycle.verifyTool(ownershipNativeCall(f), "flow"); expect(f.autoDrive.captureAuthority(sessionID)?.token).toBe("local-token"); } finally { await f.lifecycle.dispose(); @@ -2640,7 +2704,7 @@ test("real steer before compaction applies existing cancellation policy", async expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); f.native.splice(0, f.native.length, { ...control, status: "completed" }); await finishOwnedResponse(f, await startOwnedRequest(f)); - await f.lifecycle.beforeTool(ownershipNativeCall(f)); + await f.lifecycle.verifyTool(ownershipNativeCall(f), "flow"); expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); expect(f.repository.saveCount).toBe(0); } finally { @@ -2673,21 +2737,30 @@ test("unknown synthetic callbacks cannot borrow or deactivate the exact accepted "epoch was revoked", ); expect(f.autoDrive.captureAuthority(sessionID)).toEqual(authority); - await expect(f.emit("context", compact)).rejects.toThrow( - "unknown, synthetic, or stale real input", - ); + await f.emit("context", compact); + const request = new Request("http://localhost/foreign-native"); + await f.emit("http.request", { + sessionID, + agent, + model, + kind: "primary", + request, + }); + await finishOwnedResponse(f, request); expect(f.autoDrive.captureAuthority(sessionID)).toEqual(authority); const foreignCall = ownershipNativeCall(f); - await expect(f.lifecycle.beforeTool(foreignCall)).rejects.toThrow(); + await expect(f.lifecycle.verifyTool(foreignCall, "flow")).rejects.toThrow( + "live admitted input origin", + ); expect(f.repository.saveCount).toBe(0); await f.command("flow-auto", "Inspect separately accepted fresh source"); const fresh = await startOwnedRequest(f); await finishOwnedResponse(f, fresh); - await expect(f.lifecycle.beforeTool(foreignCall)).rejects.toThrow( - "primary request", + await expect(f.lifecycle.verifyTool(foreignCall, "flow")).rejects.toThrow( + "cannot be rebound", ); const call = ownershipNativeCall(f); - await f.lifecycle.beforeTool(call); + await f.lifecycle.verifyTool(call, "flow"); expect(f.autoDrive.captureAuthority(sessionID)?.token).toBe("local-token"); expect(f.repository.saveCount).toBe(0); } finally { @@ -2714,7 +2787,7 @@ for (const successor of ["command", "synthetic"] as const) const request = await startOwnedRequest(f); await expect(f.emit("context", old)).rejects.toThrow(); await finishOwnedResponse(f, request); - await f.lifecycle.beforeTool(ownershipNativeCall(f)); + await f.lifecycle.verifyTool(ownershipNativeCall(f), "flow"); expect(f.autoDrive.captureAuthority(sessionID)).toEqual(authority); } finally { await f.lifecycle.dispose(); @@ -2772,7 +2845,7 @@ for (const prior of ["none", "ended-manual"] as const) await compactCurrentInput(f, "msg_grantless_checkpoint"); await prepareContinuationRetry(f, attempt); await finishOwnedResponse(f, await startOwnedRequest(f)); - await f.lifecycle.beforeTool(ownershipNativeCall(f)); + await f.lifecycle.verifyTool(ownershipNativeCall(f), "flow"); expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); expect(f.repository.saveCount).toBe(0); expect(f.recovery.snapshot(sessionID)).toEqual({ mode: "off" }); @@ -2787,7 +2860,7 @@ test("grantless Flow plan input retains protected execution after compaction", a await f.command("flow-plan", "Ordinary manual plan"); await compactCurrentInput(f, "msg_plan_checkpoint"); await finishOwnedResponse(f, await startOwnedRequest(f)); - await f.lifecycle.beforeTool(ownershipNativeCall(f)); + await f.lifecycle.verifyTool(ownershipNativeCall(f), "flow"); expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); expect(f.repository.saveCount).toBe(0); } finally { @@ -2830,7 +2903,7 @@ test("grantless reviewer input keeps its exact selected role through compaction" if (message?.type !== "assistant") throw new Error("Missing native reviewer assistant"); message.agent = reviewer; - await f.lifecycle.beforeTool({ ...call, agent: reviewer }); + await f.lifecycle.verifyTool({ ...call, agent: reviewer }, "flow"); expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); expect(f.repository.saveCount).toBe(0); } finally { @@ -2866,7 +2939,7 @@ for (const reject of [false, true]) ); } await finishOwnedResponse(f, await startOwnedRequest(f)); - await f.lifecycle.beforeTool(ownershipNativeCall(f)); + await f.lifecycle.verifyTool(ownershipNativeCall(f), "flow"); if (!oldSettlesFirst) { releaseOld(); expect(await pending).toBe( @@ -3036,7 +3109,7 @@ for (const reason of ["auto", "manual"] as const) await compactCurrentInput(f, "msg_manual_recovery_checkpoint", reason); await finishOwnedResponse(f, await startOwnedRequest(f)); const call = ownershipNativeCall(f); - await f.lifecycle.beforeTool(call); + await f.lifecycle.verifyTool(call, "flow"); const guard = f.recovery.guard({ hostSessionId: sessionID, messageId: call.messageID, @@ -3080,7 +3153,7 @@ for (const attempt of ["first", "retry"] as const) await compactCurrentInput(f, "msg_manual_known_control", "manual"); await prepareContinuationRetry(f, attempt); await finishOwnedResponse(f, await startOwnedRequest(f)); - await f.lifecycle.beforeTool(ownershipNativeCall(f)); + await f.lifecycle.verifyTool(ownershipNativeCall(f), "flow"); expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); expect(f.recovery.snapshot(sessionID)).toEqual({ mode: "off" }); expect(f.repository.saveCount).toBe(0); @@ -3109,7 +3182,7 @@ test("unattributed manual summary creates no checkpoint grant and permits a late "Fresh ordinary input", ); await finishOwnedResponse(f, await startOwnedRequest(f)); - await f.lifecycle.beforeTool(ownershipNativeCall(f)); + await f.lifecycle.verifyTool(ownershipNativeCall(f), "flow"); expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); expect(f.repository.saveCount).toBe(0); } finally { @@ -3165,7 +3238,7 @@ for (const stale of ["marked", "synthetic", "prefix"] as const) "unknown, synthetic, or stale real input", ); await expect( - f.lifecycle.beforeTool(ownershipNativeCall(f)), + f.lifecycle.verifyTool(ownershipNativeCall(f), "flow"), ).rejects.toThrow("primary request"); } expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); @@ -3213,7 +3286,7 @@ for (const fresh of ["automatic", "ordinary"] as const) expect(await old).toBe("Error: Flow execution epoch was revoked."); f.native.splice(f.native.indexOf(control), 1); await finishOwnedResponse(f, request); - await f.lifecycle.beforeTool(ownershipNativeCall(f)); + await f.lifecycle.verifyTool(ownershipNativeCall(f), "flow"); expect(f.autoDrive.captureAuthority(sessionID)).toEqual(authority); expect(f.repository.saveCount).toBe(0); } finally { @@ -3337,7 +3410,7 @@ for (const fresh of ["automatic", "ordinary"] as const) await expect(f.emit("compaction", old)).rejects.toThrow(); f.native.splice(f.native.indexOf(control), 1); await finishOwnedResponse(f, request); - await f.lifecycle.beforeTool(ownershipNativeCall(f)); + await f.lifecycle.verifyTool(ownershipNativeCall(f), "flow"); expect(f.autoDrive.captureAuthority(sessionID)).toEqual(authority); expect(f.repository.saveCount).toBe(0); } finally { @@ -3375,7 +3448,7 @@ for (const actor of ["command", "synthetic"] as const) f.releaseSessionRead(); await pending; await finishOwnedResponse(f, await startOwnedRequest(f)); - await f.lifecycle.beforeTool(ownershipNativeCall(f)); + await f.lifecycle.verifyTool(ownershipNativeCall(f), "flow"); expect(f.autoDrive.captureAuthority(sessionID)).not.toBeNull(); } finally { f.releaseSessionRead(); @@ -3416,7 +3489,7 @@ test("accepted activation readiness blocks freeze and tools until exact source b expect(freeze).toHaveBeenCalledTimes(0); expect(f.repository.saveCount).toBe(0); await expect( - f.lifecycle.beforeTool(ownershipNativeCall(f)), + f.lifecycle.verifyTool(ownershipNativeCall(f), "flow"), ).rejects.toThrow("primary request"); expect(freeze).toHaveBeenCalledTimes(0); release.resolve(); @@ -3432,7 +3505,7 @@ test("accepted activation readiness blocks freeze and tools until exact source b request, }); await finishOwnedResponse(f, request); - await f.lifecycle.beforeTool(ownershipNativeCall(f)); + await f.lifecycle.verifyTool(ownershipNativeCall(f), "flow"); } finally { release.resolve(); freeze.mockRestore(); @@ -3480,7 +3553,7 @@ test("interrupted ordinary manual summaries retain lineage without an execution }, { type: "idle", - id: "msg_interrupted_idle", + id: SessionMessage.ID.fromEvent(terminal("interrupted").id), outcome: "interrupted", time: { created: 4 }, }, @@ -3499,14 +3572,14 @@ test("interrupted ordinary manual summaries retain lineage without an execution "unknown, synthetic, or stale real input", ); await expect( - f.lifecycle.beforeTool(ownershipNativeCall(f)), + f.lifecycle.verifyTool(ownershipNativeCall(f), "flow"), ).rejects.toThrow(); expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); expect(f.repository.saveCount).toBe(0); } await admitOrdinaryInput(f, "msg_separate_fresh", "Fresh ordinary request"); await finishOwnedResponse(f, await startOwnedRequest(f)); - await f.lifecycle.beforeTool(ownershipNativeCall(f)); + await f.lifecycle.verifyTool(ownershipNativeCall(f), "flow"); expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); expect(f.recovery.snapshot(sessionID)).toEqual({ mode: "off" }); expect(f.repository.saveCount).toBe(0); @@ -3575,12 +3648,12 @@ test("a cancelled same-epoch context releases only its own pre-dispatch frozen p expect(authority).not.toBeNull(); await finishOwnedResponse(f, await startOwnedRequest(f)); const call = ownershipNativeCall(f); - await f.lifecycle.beforeTool(call); + await f.lifecycle.verifyTool(call, "flow"); f.releaseSessionRead(); await f.flush(); expect(f.autoDrive.captureAuthority(sessionID)).not.toBeNull(); expect(f.autoDrive.captureAuthority(sessionID)).toEqual(authority); - await f.lifecycle.beforeTool(call); + await f.lifecycle.verifyTool(call, "flow"); } finally { f.releaseSessionRead(); await f.lifecycle.dispose(); @@ -3609,7 +3682,7 @@ test("a source accepted while context waits owns a later native-read failure", a expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); await f.command("flow-auto", "Fresh source after matched read failure"); await finishOwnedResponse(f, await startOwnedRequest(f)); - await f.lifecycle.beforeTool(ownershipNativeCall(f)); + await f.lifecycle.verifyTool(ownershipNativeCall(f), "flow"); expect(f.autoDrive.captureAuthority(sessionID)).not.toBeNull(); } finally { f.releaseAdmission(); @@ -3651,7 +3724,7 @@ for (const outcome of ["failed", "interrupted"] as const) message.time.completed = 5; f.native.push({ type: "idle", - id: "msg_owned_terminal", + id: SessionMessage.ID.fromEvent(terminal(outcome).id), outcome, time: { created: 6 }, }); @@ -3662,7 +3735,7 @@ for (const outcome of ["failed", "interrupted"] as const) let oldEffects = 0; await expect( (async () => { - await f.lifecycle.beforeTool(call); + await f.lifecycle.verifyTool(call, "flow"); oldEffects++; })(), ).rejects.toThrow("epoch was revoked"); @@ -3673,11 +3746,11 @@ for (const outcome of ["failed", "interrupted"] as const) expect(fresh).not.toBeNull(); await finishOwnedResponse(f, await startOwnedRequest(f)); const freshCall = ownershipNativeCall(f); - await f.lifecycle.beforeTool(freshCall); + await f.lifecycle.verifyTool(freshCall, "flow"); f.sendEvent(event); await f.flush(); - await expect(f.lifecycle.beforeTool(call)).rejects.toThrow(); - await f.lifecycle.beforeTool(freshCall); + await expect(f.lifecycle.verifyTool(call, "flow")).rejects.toThrow(); + await f.lifecycle.verifyTool(freshCall, "flow"); expect(f.autoDrive.captureAuthority(sessionID)).toEqual(fresh); expect(f.autoDrive.captureAuthority(sessionID)).not.toBeNull(); expect(oldEffects).toBe(0); @@ -3687,7 +3760,11 @@ for (const outcome of ["failed", "interrupted"] as const) } }); -async function effectTool(f: OwnershipFixture, effects: string[]) { +async function effectTool( + f: OwnershipFixture, + effects: string[], + onEffect?: () => void, +) { const registered = new Map(); await registerFlowTools( { @@ -3727,11 +3804,14 @@ async function effectTool(f: OwnershipFixture, effects: string[]) { input: { type: "object" }, execute: async () => { effects.push("effect"); + onEffect?.(); return "applied"; }, }, }, - f.lifecycle.beforeTool, + async (context) => { + await f.lifecycle.verifyTool(context, "flow"); + }, ); const tool = registered.get("flow_status"); if (!tool) throw new Error("Missing registered effect tool."); @@ -3785,7 +3865,7 @@ for (const known of ["bound", "historical"] as const) f, boundary === "shell" ? "shell" : "flow_status", ); - if (known === "bound") await f.lifecycle.beforeTool(old); + if (known === "bound") await f.lifecycle.verifyTool(old, "flow"); await finishOwnedResponse(f, await startOwnedRequest(f)); const control = automaticControl("msg_retention_compaction"); f.native.push(control); @@ -3802,7 +3882,7 @@ for (const known of ["bound", "historical"] as const) for (const id of [old.id, CallID.make("new-call-on-old-assistant")]) { const replay = async () => { if (boundary === "flow") - await f.lifecycle.beforeTool({ ...old, id }); + await f.lifecycle.verifyTool({ ...old, id }, "flow"); else for (const callback of f.toolCallbacks["execute.before"]) await callback({ @@ -3833,7 +3913,7 @@ test("session.deleted public barrier retires held owners and isolates another se const oldRequest = await startOwnedRequest(f); await finishOwnedResponse(f, oldRequest); const oldCall = ownershipNativeCall(f); - await f.lifecycle.beforeTool(oldCall); + await f.lifecycle.verifyTool(oldCall, "flow"); const oldOwner = freeze.mock.results.at(-1)?.value; if (!oldOwner) throw new Error("Missing captured primary."); const otherID = Session.ID.make("session-retention-other"); @@ -4057,7 +4137,9 @@ test("unregistered native regression: external synthetic in empty ledger runs or { role: "user", id: "msg_external_synthetic", - content: [{ type: "text", text: "Ordinary external synthetic request" }], + content: [ + { type: "text", text: "Ordinary external synthetic request" }, + ], }, ]); } finally { @@ -4101,14 +4183,20 @@ test("unregistered native regression: external synthetic after ordinary prompt i role: "user", id: "msg_prior_ordinary", content: [ - { type: "text", text: "Ordinary prompt before external notification" }, + { + type: "text", + text: "Ordinary prompt before external notification", + }, ], }, { role: "user", id: "msg_external_after_ordinary", content: [ - { type: "text", text: "External ordinary notification after prior prompt" }, + { + type: "text", + text: "External ordinary notification after prior prompt", + }, ], }, ]); @@ -4116,3 +4204,683 @@ test("unregistered native regression: external synthetic after ordinary prompt i await f.lifecycle.dispose(); } }); + +async function unregisteredFixture() { + const f = await fixture(); + f.native.push({ + type: "synthetic", + id: "msg_foreign", + text: "External request", + time: { created: 0 }, + }); + return f; +} + +for (const replacement of ["new-epoch", "same-epoch"] as const) + for (const boundary of ["request", "response", "shell"] as const) + test(`native-only ${boundary} rejects ${replacement} clock replacement before successor freeze`, async () => { + const f = await unregisteredFixture(); + try { + if (replacement === "same-epoch") { + await f.command("flow-auto", "Establish the same-epoch lease"); + f.native.push({ + type: "synthetic", + id: "msg_foreign_same_epoch", + text: "Foreign above Flow", + time: { created: 1 }, + }); + } + await f.emit("context", f.context()); + const request = new Request("http://localhost/old-native"); + const http = { + sessionID, + agent, + model, + kind: "primary" as const, + request, + }; + if (boundary !== "request") await f.emit("http.request", http); + const call = + boundary === "shell" ? ownershipNativeCall(f, "shell") : undefined; + if (replacement === "new-epoch") + await f.command("flow-auto", "Fresh observed authority"); + else + await f.lifecycle.prompt( + sessionID, + "Private frontier replacement", + delivery, + { [FLOW_AUTO_METADATA_KEY]: "local-token" }, + ); + const authority = f.autoDrive.captureAuthority(sessionID); + if (boundary === "request") + await expect(f.emit("http.request", http)).rejects.toThrow(); + if (boundary === "response") + await expect( + f.emit("http.response", { + ...http, + response: new Response("obsolete"), + }), + ).rejects.toThrow(); + if (call) + await expect( + f.lifecycle.verifyTool(call, "native", "shell"), + ).rejects.toThrow(); + expect(f.autoDrive.captureAuthority(sessionID)).toEqual(authority); + expect(f.repository.saveCount).toBe(0); + await finishOwnedResponse(f, await startOwnedRequest(f)); + await f.lifecycle.verifyTool(ownershipNativeCall(f), "flow"); + expect(f.autoDrive.captureAuthority(sessionID)).toEqual(authority); + } finally { + await f.lifecycle.dispose(); + } + }); + +test("native-only protected denial during streaming keeps the HTTP overlap barrier", async () => { + const f = await unregisteredFixture(); + try { + const request = await startOwnedRequest(f); + const frame = { + sessionID, + agent, + model, + kind: "primary" as const, + request, + response: new Response( + new ReadableStream({ + start(controller) { + controller.enqueue(new TextEncoder().encode("healthy")); + }, + }), + ), + }; + await f.emit("http.response", frame); + const effects: string[] = []; + const tool = await effectTool(f, effects); + await expect(tool.execute({}, ownershipNativeCall(f))).rejects.toThrow( + "live admitted input origin", + ); + expect(effects).toEqual([]); + await expect(f.emit("context", f.context())).rejects.toThrow("overlap"); + if (!frame.response.body) throw new Error("Expected stream."); + const reader = frame.response.body.getReader(); + expect(new TextDecoder().decode((await reader.read()).value)).toBe( + "healthy", + ); + await reader.cancel(); + await finishOwnedResponse(f, await startOwnedRequest(f)); + expect(f.repository.saveCount).toBe(0); + } finally { + await f.lifecycle.dispose(); + } +}); + +for (const outcome of ["failed", "succeeded", "interrupted"] as const) + test(`native-only ${outcome} terminal cannot invoke Flow terminal effects`, async () => { + const f = await fixture(); + try { + await f.command("flow-auto", "Unrelated exact lease"); + const authority = f.autoDrive.captureAuthority(sessionID); + f.native.push({ + type: "synthetic", + id: "msg_foreign_terminal", + text: "Foreign request", + time: { created: 1 }, + }); + await finishOwnedResponse(f, await startOwnedRequest(f)); + const event: OpenCodeEvent = + outcome === "succeeded" + ? { + id: "evt_foreign_terminal", + type: "session.execution.succeeded", + created: 3, + durable: { aggregateID: sessionID, seq: 3, version: 1 }, + data: { sessionID }, + } + : terminal(outcome, "evt_foreign_terminal"); + f.native.push({ + type: "idle", + id: SessionMessage.ID.fromEvent(event.id), + outcome, + time: { created: 3 }, + }); + f.session.outcome = outcome; + f.session.time.idle = 3; + await f.sendEventAndWait(event); + expect(f.flowTerminals).toEqual([]); + expect(f.autoDrive.captureAuthority(sessionID)).toEqual(authority); + expect(f.repository.saveCount).toBe(0); + expect( + f.native + .filter((message) => message.type === "synthetic") + .map((message) => message.id), + ).toEqual(["msg_foreign_terminal"]); + await f.command("flow-auto", "Fresh Flow after foreign terminal"); + await finishOwnedResponse(f, await startOwnedRequest(f)); + await f.lifecycle.verifyTool(ownershipNativeCall(f), "flow"); + } finally { + await f.lifecycle.dispose(); + } + }); + +for (const read of ["context", "status"] as const) + test(`old terminal entering after fresh freeze cannot revoke on ${read} failure`, async () => { + const f = await fixture(); + try { + await initialOwnershipWork(f); + const old = terminal("failed", "evt_before_new_freeze"); + f.native.push({ + type: "idle", + id: SessionMessage.ID.fromEvent(old.id), + outcome: "failed", + time: { created: 1 }, + }); + await f.command("flow-auto", "Fresh source and primary"); + const authority = f.autoDrive.captureAuthority(sessionID); + const request = await startOwnedRequest(f); + if (read === "context") f.rejectNextContextRead(); + else f.rejectNextSessionRead(); + await f.sendEventAndWait(old); + expect(f.autoDrive.captureAuthority(sessionID)).toEqual(authority); + expect(f.flowTerminals).toEqual([]); + await finishOwnedResponse(f, request); + if (read === "status") { + await expect( + f.lifecycle.verifyTool(ownershipNativeCall(f), "flow"), + ).rejects.toThrow("Native session snapshot failed"); + await f.command( + "flow-auto", + "Fresh after exact tool observation failure", + ); + await finishOwnedResponse(f, await startOwnedRequest(f)); + } + await f.lifecycle.verifyTool(ownershipNativeCall(f), "flow"); + expect(f.logs.includes("Flow event stream failed closed.")).toBe(false); + } finally { + await f.lifecycle.dispose(); + } + }); + +for (const command of ["bun test", "printf foreign"]) + test(`foreign shell ${command} preserves armed Flow capture over a populated frontier`, async () => { + let capture: ValidationCaptureCoordinator; + const f = await fixture(agent, (host) => { + capture.cancel(host); + }); + const observations: ObservedValidation[] = []; + const prepared: PreparedValidation = { + featureId: "feature", + runId: "run", + command: "bun test", + scope: "focused", + sourceDigest: `sha256:${"a".repeat(64)}`, + hostPlatform: "linux", + intent: "pass", + assertions: [], + resultsPath: undefined, + }; + let serial = 0; + capture = new ValidationCaptureCoordinator({ + readSession: async () => f.session, + randomId: () => `capture-${++serial}`, + persistObservation: async (_workspace, value, assertAuthorized) => { + assertAuthorized(); + observations.push(value); + return { ...value, id: value.captureId, recordedRevision: 4 }; + }, + }); + try { + await admitOrdinaryInput( + f, + "msg_capture_flow", + "Observed ordinary Flow direction", + ); + await finishOwnedResponse(f, await startOwnedRequest(f)); + const effects: string[] = []; + const tool = await effectTool(f, effects, () => { + capture.arm(sessionID, directory, prepared); + }); + await tool.execute({}, ownershipNativeCall(f)); + expect(capture.pendingCount()).toBe(1); + f.native.push({ + type: "synthetic", + id: "msg_foreign_capture", + text: "External native shell", + time: { created: 1 }, + }); + await finishOwnedResponse(f, await startOwnedRequest(f)); + const foreign = ownershipNativeCall(f, "shell"); + const before: NativeToolBefore = { + sessionID, + agent, + id: foreign.id, + messageID: foreign.messageID, + tool: "shell", + input: { command }, + }; + for (const callback of f.toolCallbacks["execute.before"]) + await callback(before); + const access = await f.lifecycle.verifyTool(before, "native", "shell"); + if (access.kind === "flow") + await capture.observeToolBefore(before, access.assertCurrent); + const after: NativeToolAfter = { + ...before, + status: "completed", + result: { + content: "literal native shell result", + output: { + exit: 0, + truncated: false, + output: "literal native shell result", + }, + }, + }; + const original = structuredClone(after); + expect(await capture.observeToolAfter(after)).toBeNull(); + expect(after).toEqual(original); + expect(observations).toEqual([]); + expect(capture.pendingCount()).toBe(1); + const event: OpenCodeEvent = { + id: "evt_foreign_capture_idle", + type: "session.execution.succeeded", + created: 2, + durable: { aggregateID: sessionID, seq: 2, version: 1 }, + data: { sessionID }, + }; + f.native.push({ + type: "idle", + id: SessionMessage.ID.fromEvent(event.id), + outcome: "succeeded", + time: { created: 2 }, + }); + f.session.time.idle = 2; + f.session.outcome = "succeeded"; + await f.sendEventAndWait(event); + expect(capture.pendingCount()).toBe(1); + expect(f.flowTerminals).toEqual([]); + expect(f.repository.saveCount).toBe(0); + expect(f.recovery.snapshot(sessionID)).toEqual({ mode: "off" }); + capture.cancel(sessionID); + await admitOrdinaryInput( + f, + "msg_fresh_capture", + "Fresh observed validation", + ); + await finishOwnedResponse(f, await startOwnedRequest(f)); + await tool.execute({}, ownershipNativeCall(f)); + const fresh: NativeToolBefore = { + ...ownershipNativeCall(f, "shell"), + tool: "shell", + input: { command: "bun test" }, + }; + const authorized = await f.lifecycle.verifyTool(fresh, "native", "shell"); + if (authorized.kind !== "flow") + throw new Error("Expected exact Flow observation."); + await capture.observeToolBefore(fresh, authorized.assertCurrent); + const result: NativeToolAfter = { + ...fresh, + status: "completed", + result: { + content: "fresh passed", + output: { exit: 0, truncated: false, output: "fresh passed" }, + }, + }; + expect((await capture.observeToolAfter(result))?.id).toBe("capture-2"); + expect(await capture.observeToolAfter(result)).toBeNull(); + expect(observations.map((value) => value.captureId)).toEqual([ + "capture-2", + ]); + expect(JSON.stringify(result)).toContain("[flow-validation]"); + expect(capture.pendingCount()).toBe(0); + expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); + } finally { + capture.clear(); + await f.lifecycle.dispose(); + } + }); + +for (const proof of ["before", "after"] as const) + test(`owned terminal read failure ${proof} prefix proof has bounded cleanup`, async () => { + const f = await fixture(); + try { + await initialOwnershipWork(f); + const authority = f.autoDrive.captureAuthority(sessionID); + const event: OpenCodeEvent = { + id: "evt_owned_proof", + type: "session.execution.succeeded", + created: 4, + durable: { aggregateID: sessionID, seq: 4, version: 1 }, + data: { sessionID }, + }; + f.native.push({ + type: "idle", + id: SessionMessage.ID.fromEvent(event.id), + outcome: "succeeded", + time: { created: 4 }, + }); + f.session.outcome = "succeeded"; + f.session.time.idle = 4; + if (proof === "before") f.rejectNextContextRead(); + else f.rejectNextSessionRead(); + await f.sendEventAndWait(event); + expect(f.autoDrive.captureAuthority(sessionID)).toEqual( + proof === "before" ? authority : null, + ); + expect(f.flowTerminals).toEqual([]); + expect(f.logs.includes("Flow event stream failed closed.")).toBe(false); + await f.command("flow-auto", "Fresh source after terminal read error"); + await finishOwnedResponse(f, await startOwnedRequest(f)); + await f.lifecycle.verifyTool(ownershipNativeCall(f), "flow"); + } finally { + await f.lifecycle.dispose(); + } + }); + +for (const settled of [false, true]) + test(`missing raw native input above ${settled ? "rejected" : "pending"} lineage refuses without authority`, async () => { + const f = await fixture(); + try { + if (settled) f.corruptReceipt(); + else f.blockAdmission(); + const command = f + .command("flow-plan", "Pending plan source") + .catch(String); + await f.flush(); + f.native.push({ + type: "synthetic", + id: "msg_missing_above_unaccepted", + text: "External native", + time: { created: 1 }, + }); + await expect(f.emit("context", f.context())).rejects.toThrow( + "unknown, synthetic, or stale real input", + ); + expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); + expect(f.repository.saveCount).toBe(0); + f.releaseAdmission(); + await command; + } finally { + f.releaseAdmission(); + await f.lifecycle.dispose(); + } + }); + +for (const replacement of ["same-epoch", "new-epoch"] as const) + for (const reject of [false, true]) + test(`native-only delayed shell read ${reject} cannot affect ${replacement} successor`, async () => { + const f = await fixture(); + try { + await f.command("flow-auto", "Establish current Flow lineage"); + f.native.push({ + type: "synthetic", + id: "msg_delayed_native", + text: "Ordinary external request", + time: { created: 1 }, + }); + await finishOwnedResponse(f, await startOwnedRequest(f)); + const call = ownershipNativeCall(f, "shell"); + f.blockNextContextRead(); + if (reject) f.rejectNextContextRead(); + const pending = f.lifecycle + .verifyTool(call, "native", "shell") + .catch(String); + await f.waitContextRead(); + if (replacement === "same-epoch") + await f.lifecycle.prompt(sessionID, "Private successor", delivery, { + [FLOW_AUTO_METADATA_KEY]: "local-token", + }); + else await f.command("flow-auto", "Fresh successor command"); + const authority = f.autoDrive.captureAuthority(sessionID); + const request = await startOwnedRequest(f); + f.releaseContextRead(); + expect(await pending).toBe( + reject + ? "Error: Native context snapshot failed." + : "Error: Flow execution epoch was revoked.", + ); + expect(f.autoDrive.captureAuthority(sessionID)).toEqual(authority); + await finishOwnedResponse(f, request); + await f.lifecycle.verifyTool(ownershipNativeCall(f), "flow"); + expect(f.repository.saveCount).toBe(0); + } finally { + f.releaseContextRead(); + await f.lifecycle.dispose(); + } + }); + +test("native-only unfinished retry preserves exact call identity and isolates the old Request", async () => { + const f = await unregisteredFixture(); + try { + const oldRequest = await startOwnedRequest(f); + const messageID = SessionMessage.ID.make("msg_native_retry"); + const retry: Extract = { + type: "assistant", + id: messageID, + agent, + model, + time: { created: 1 }, + content: [], + retry: { + attempt: 1, + at: 100, + error: { type: "provider.transport", message: "retry" }, + }, + }; + f.native.push(retry); + const next = await startOwnedRequest(f); + await expect( + f.emit("http.response", { + sessionID, + agent, + model, + kind: "primary", + request: oldRequest, + response: new Response("old retry response"), + }), + ).rejects.toThrow("Request identity"); + await finishOwnedResponse(f, next); + const id = CallID.make("call_native_retry"); + retry.content.push({ + type: "tool", + id, + name: "shell", + time: { created: 2 }, + state: { + status: "running", + input: { command: "printf native" }, + metadata: {}, + }, + }); + expect( + await f.lifecycle.verifyTool( + { sessionID, agent, messageID, id }, + "native", + "shell", + ), + ).toEqual({ kind: "native-only" }); + await expect( + f.lifecycle.verifyTool( + { sessionID, agent, messageID, id }, + "flow", + "shell", + ), + ).rejects.toThrow("live admitted input origin"); + expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); + expect(f.recovery.snapshot(sessionID)).toEqual({ mode: "off" }); + expect(f.repository.saveCount).toBe(0); + } finally { + await f.lifecycle.dispose(); + } +}); + +for (const callback of ["http.request", "http.response"] as const) + test(`unbound mismatched ${callback} cannot fail a healthy Flow owner`, async () => { + const f = await fixture(); + try { + await f.command("flow-auto", "Exact healthy Flow source"); + const authority = f.autoDrive.captureAuthority(sessionID); + const request = await startOwnedRequest(f); + const unbound = { + sessionID, + agent, + model: { ...model, id: Model.ID.make("foreign") }, + kind: "primary" as const, + request: new Request("http://localhost/unbound"), + }; + if (callback === "http.request") + await expect(f.emit(callback, unbound)).rejects.toThrow( + "unique frozen context", + ); + else + await expect( + f.emit(callback, { ...unbound, response: new Response("unbound") }), + ).rejects.toThrow("Request identity"); + expect(f.autoDrive.captureAuthority(sessionID)).toEqual(authority); + await finishOwnedResponse(f, request); + const effects: string[] = []; + const tool = await effectTool(f, effects); + await tool.execute({}, ownershipNativeCall(f)); + expect(effects).toEqual(["effect"]); + expect(f.autoDrive.captureAuthority(sessionID)).toEqual(authority); + } finally { + await f.lifecycle.dispose(); + } + }); + +test("a bound native-only shell cannot use the grantless Flow ordinary-tool bypass after replacement", async () => { + const f = await unregisteredFixture(); + try { + await finishOwnedResponse(f, await startOwnedRequest(f)); + const old = ownershipNativeCall(f, "shell"); + await f.lifecycle.verifyTool(old, "native", "shell"); + await admitOrdinaryInput( + f, + "msg_grantless_replacement", + "Fresh observed ordinary", + ); + await finishOwnedResponse(f, await startOwnedRequest(f)); + let staleEffects = 0; + await expect( + (async () => { + for (const callback of f.toolCallbacks["execute.before"]) + await callback({ + sessionID, + agent, + messageID: old.messageID, + id: old.id, + tool: "shell", + input: { command: "printf stale" }, + }); + staleEffects++; + })(), + ).rejects.toThrow(); + expect(staleEffects).toBe(0); + const current = ownershipNativeCall(f, "shell"); + let ordinaryEffects = 0; + for (const callback of f.toolCallbacks["execute.before"]) + await callback({ + sessionID, + agent, + messageID: current.messageID, + id: current.id, + tool: "shell", + input: { command: "printf current" }, + }); + ordinaryEffects++; + expect(ordinaryEffects).toBe(1); + expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); + expect(f.repository.saveCount).toBe(0); + } finally { + await f.lifecycle.dispose(); + } +}); + +for (const identity of ["agent", "model", "session"] as const) + test(`native-only context rejects changed selected ${identity} without revoking Flow lineage`, async () => { + const f = await fixture(); + try { + await f.command("flow-auto", "Unrelated exact Flow authority"); + const authority = f.autoDrive.captureAuthority(sessionID); + f.native.push({ + type: "synthetic", + id: "msg_native_selection", + text: "Foreign ordinary request", + time: { created: 1 }, + }); + if (identity === "agent") f.session.agent = Agent.ID.make("other"); + if (identity === "model") + f.session.model = { ...model, id: Model.ID.make("other") }; + if (identity === "session") + f.session.id = Session.ID.make("session-other"); + await expect(f.emit("context", f.context())).rejects.toThrow( + "native selected session", + ); + expect(f.autoDrive.captureAuthority(sessionID)).toEqual(authority); + expect(f.repository.saveCount).toBe(0); + f.session.agent = agent; + f.session.model = model; + f.session.id = sessionID; + await f.command("flow-auto", "Fresh exact selected source"); + await finishOwnedResponse(f, await startOwnedRequest(f)); + await f.lifecycle.verifyTool(ownershipNativeCall(f), "flow"); + } finally { + await f.lifecycle.dispose(); + } + }); + +for (const rejectedAt of ["model", "agent", "session", "session-read"] as const) + test(`rejected native ${rejectedAt} preserves the previous usable primary`, async () => { + const f = await fixture(); + try { + await initialOwnershipWork(f); + const call = ownershipNativeCall(f); + const access = await f.lifecycle.verifyTool(call, "flow"); + if (access.kind !== "flow") throw new Error("Expected admitted owner."); + access.assertCurrent(); + const effects: string[] = []; + const tool = await effectTool(f, effects); + const authority = f.autoDrive.captureAuthority(sessionID); + f.native.push({ + type: "synthetic", + id: "msg_invalid_replacement", + text: "External candidate", + time: { created: 5 }, + }); + await tool.execute({}, call); + expect(effects).toEqual(["effect"]); + if (rejectedAt === "model") + f.session.model = { + ...model, + id: Model.ID.make("unselected-other-model"), + }; + if (rejectedAt === "agent") + f.session.agent = Agent.ID.make("unselected-agent"); + if (rejectedAt === "session") + f.session.id = Session.ID.make("session-foreign"); + if (rejectedAt === "session-read") f.rejectNextSessionRead(); + await expect(f.emit("context", f.context())).rejects.toThrow( + rejectedAt === "session-read" + ? "Native session snapshot failed" + : "native selected session", + ); + f.session.model = model; + f.session.agent = agent; + f.session.id = sessionID; + expect(f.autoDrive.captureAuthority(sessionID)).toEqual(authority); + access.assertCurrent(); + await tool.execute({}, call); + expect(effects).toEqual(["effect", "effect"]); + await finishOwnedResponse(f, await startOwnedRequest(f)); + expect( + await f.lifecycle.verifyTool( + ownershipNativeCall(f, "shell"), + "native", + "shell", + ), + ).toEqual({ kind: "native-only" }); + expect(() => access.assertCurrent()).toThrow("superseded"); + expect(f.autoDrive.captureAuthority(sessionID)).toEqual(authority); + expect(f.repository.saveCount).toBe(0); + } finally { + await f.lifecycle.dispose(); + } + }); diff --git a/tests/recovery-authority.test.ts b/tests/recovery-authority.test.ts index f653e00b..7798525e 100644 --- a/tests/recovery-authority.test.ts +++ b/tests/recovery-authority.test.ts @@ -120,7 +120,7 @@ test.each(["off", "no-key", "shadow"] as const)( {}, "ses_host", ); - await commands.lifecycle.beforeTool(native); + await commands.lifecycle.verifyTool(native, "flow"); const guarded = createFlowService( s.repository, s.env, @@ -815,7 +815,7 @@ test("unmatched native requests fail closed and fresh flow-run restores a verifi {}, "ses_host", ); - await commands.lifecycle.beforeTool(native); + await commands.lifecycle.verifyTool(native, "flow"); const manual = createFlowService( s.repository, s.env, diff --git a/tests/recovery-policy.test.ts b/tests/recovery-policy.test.ts index de01211c..dc297fa2 100644 --- a/tests/recovery-policy.test.ts +++ b/tests/recovery-policy.test.ts @@ -246,7 +246,7 @@ describe("process-local recovery", () => { {}, "ses_host", ); - await commands.lifecycle.beforeTool(native); + await commands.lifecycle.verifyTool(native, "flow"); const resumedFlow = createFlowService( s.repository, s.env, diff --git a/tests/runtime-gates.test.ts b/tests/runtime-gates.test.ts index 0f521459..f2eaf1b2 100644 --- a/tests/runtime-gates.test.ts +++ b/tests/runtime-gates.test.ts @@ -1942,3 +1942,139 @@ async function priorFindingsFor( } return projection.priorFindings; } + +for (const waitAt of ["lock", "load", "digest"] as const) + test(`validation authorization is decided after the ${waitAt} wait`, async () => { + const repository = new MemorySessionRepository(); + const flow = await approveSession(repository, deterministicEnvironment()); + await startFeatureRun(flow, repository, FEATURE, `guard-${waitAt}`); + const prepared = await prepareValidation( + repository, + { + expectedRevision: revision(repository), + featureId: FEATURE, + command: "bun test", + scope: "focused", + }, + "linux", + ); + const original = structuredClone(repository.session); + const saves = repository.saveCount; + const entered = Promise.withResolvers(); + const release = Promise.withResolvers(); + const wait = async () => { + entered.resolve(); + await release.promise; + }; + const lock = + waitAt === "lock" ? repository.transact(wait) : Promise.resolve(); + let authorized = true; + const guarded = { + read: () => repository.read(), + transact: ( + task: (transaction: typeof repository.transaction) => Promise, + ) => + repository.transact((transaction) => + task({ + ...transaction, + load: async () => { + if (waitAt === "load") await wait(); + return transaction.load(); + }, + computeSourceDigest: async () => { + if (waitAt === "digest") await wait(); + return transaction.computeSourceDigest(); + }, + }), + ), + }; + const pending = persistObservedValidation( + guarded, + { + ...prepared, + captureId: `guard-${waitAt}`, + exitCode: 0, + outputDigest: OUTPUT, + outputComplete: true, + }, + () => { + if (!authorized) throw new Error("stale validation owner"); + }, + ); + const caught = pending.catch(String); + await entered.promise; + authorized = false; + release.resolve(); + await lock; + expect(await caught).toBe("Error: stale validation owner"); + expect(repository.saveCount).toBe(saves); + expect(repository.session).toEqual(original); + const fresh = await persistObservedValidation( + repository, + { + ...prepared, + captureId: `fresh-${waitAt}`, + exitCode: 0, + outputDigest: OUTPUT, + outputComplete: true, + }, + () => {}, + ); + expect(fresh.id).toBe(`fresh-${waitAt}`); + expect(repository.saveCount).toBe(saves + 1); + }); + +test("validation authorized at save is not rolled back by later owner replacement", async () => { + const repository = new MemorySessionRepository(); + const flow = await approveSession(repository, deterministicEnvironment()); + await startFeatureRun(flow, repository, FEATURE, "authorized-save"); + const prepared = await prepareValidation( + repository, + { + expectedRevision: revision(repository), + featureId: FEATURE, + command: "bun test", + scope: "focused", + }, + "linux", + ); + const entered = Promise.withResolvers(); + const release = Promise.withResolvers(); + let authorized = true; + const guarded = { + read: () => repository.read(), + transact: ( + task: (transaction: typeof repository.transaction) => Promise, + ) => + repository.transact((transaction) => + task({ + ...transaction, + save: async (session) => { + entered.resolve(); + await release.promise; + return transaction.save(session); + }, + }), + ), + }; + const pending = persistObservedValidation( + guarded, + { + ...prepared, + captureId: "authorized-save", + exitCode: 0, + outputDigest: OUTPUT, + outputComplete: true, + }, + () => { + if (!authorized) throw new Error("stale validation owner"); + }, + ); + await entered.promise; + authorized = false; + release.resolve(); + expect((await pending).id).toBe("authorized-save"); + expect( + repository.session?.runs[0]?.validations.map((value) => value.id), + ).toEqual(["authorized-save"]); +}); diff --git a/tests/validation-capture.test.ts b/tests/validation-capture.test.ts index b27062c7..d4337fe1 100644 --- a/tests/validation-capture.test.ts +++ b/tests/validation-capture.test.ts @@ -168,6 +168,7 @@ test.each([ ...(beforeWorkdir === undefined ? {} : { workdir: beforeWorkdir }), }, }), + () => {}, ); const completed = after( undefined, @@ -240,7 +241,7 @@ for (const phase of ["before", "after"] as const) { const completed = after(undefined, {}, { input }); if (phase === "before") { await expect( - capture.observeToolBefore(before({ input })), + capture.observeToolBefore(before({ input }), () => {}), ).rejects.toThrow("capture was cancelled"); expect(await capture.observeToolAfter(completed)).toBeNull(); } else { @@ -248,6 +249,7 @@ for (const phase of ["before", "after"] as const) { before({ input: { command: prepared.command, workdir: workspace }, }), + () => {}, ); await expect(capture.observeToolAfter(completed)).rejects.toThrow(); } @@ -329,7 +331,7 @@ test.each([ async (_name, output, metadata, exitCode, outputComplete, ineligibleReason) => { const { capture, observations } = fixture(); capture.arm("ses_host", workspace, prepared); - await capture.observeToolBefore(before()); + await capture.observeToolBefore(before(), () => {}); await capture.observeToolAfter(after(output, metadata)); expect(observations[0]).toMatchObject({ exitCode, outputComplete }); expect(observations[0]?.ineligibleReason).toBe(ineligibleReason); @@ -338,7 +340,7 @@ test.each([ test("native tool errors are durable ineligible observations", async () => { const { capture, observations } = fixture(); capture.arm("ses_host", workspace, prepared); - await capture.observeToolBefore(before()); + await capture.observeToolBefore(before(), () => {}); await capture.observeToolAfter({ ...before(), status: "error", @@ -359,9 +361,9 @@ test.each([ async (_name, input) => { const { capture, observations } = fixture(); capture.arm("ses_host", workspace, prepared); - await expect(capture.observeToolBefore(before({ input }))).rejects.toThrow( - "capture was cancelled", - ); + await expect( + capture.observeToolBefore(before({ input }), () => {}), + ).rejects.toThrow("capture was cancelled"); expect(observations).toEqual([]); expect(capture.pendingCount()).toBe(0); }, @@ -378,7 +380,10 @@ test.each(["flow-reviewer", "flow-worker", "flow-planner"])( }); capture.arm("ses_host", workspace, prepared); await expect( - capture.observeToolBefore(before({ agent: Agent.ID.make(agent) })), + capture.observeToolBefore( + before({ agent: Agent.ID.make(agent) }), + () => {}, + ), ).rejects.toThrow("capture was cancelled"); expect(observations).toEqual([]); }, @@ -393,7 +398,7 @@ test("wrong session facts and changed locations fail closed", async () => { }), }); capture.arm("ses_host", workspace, prepared); - await capture.observeToolBefore(before()); + await capture.observeToolBefore(before(), () => {}); directory = tmpdir(); await expect(capture.observeToolAfter(after())).rejects.toThrow( "workspace changed", @@ -410,7 +415,7 @@ test.each([ async (_name, identity) => { const { capture, observations } = fixture(); capture.arm("ses_host", workspace, prepared); - await capture.observeToolBefore(before()); + await capture.observeToolBefore(before(), () => {}); expect( await capture.observeToolAfter(after(undefined, {}, identity)), ).toBeNull(); @@ -424,7 +429,7 @@ test.each([ test("later shell input cannot relabel the frozen command", async () => { const { capture, observations } = fixture(); capture.arm("ses_host", workspace, prepared); - await capture.observeToolBefore(before()); + await capture.observeToolBefore(before(), () => {}); await expect( capture.observeToolAfter( after(undefined, {}, { input: { command: "different" } }), @@ -435,10 +440,10 @@ test("later shell input cannot relabel the frozen command", async () => { test("only the native shell ID binds capture", async () => { const { capture, observations } = fixture(); capture.arm("ses_host", workspace, prepared); - await capture.observeToolBefore(before({ tool: "bash" })); + await capture.observeToolBefore(before({ tool: "bash" }), () => {}); expect(await capture.observeToolAfter(after())).toBeNull(); expect(observations).toEqual([]); - await capture.observeToolBefore(before()); + await capture.observeToolBefore(before(), () => {}); expect(await capture.observeToolAfter(after())).toMatchObject({ exitCode: 0, }); @@ -483,7 +488,7 @@ test.each([ assertions: ["suite.critical"], resultsPath: "results.xml", }); - await capture.observeToolBefore(before()); + await capture.observeToolBefore(before(), () => {}); now = 200; await capture.observeToolAfter(after()); expect(observations[0]?.observedAssertions).toEqual([ @@ -502,7 +507,7 @@ test("unchanged JUnit cannot certify the just-run command", async () => { assertions: ["suite.critical"], resultsPath: "results.xml", }); - await capture.observeToolBefore(before()); + await capture.observeToolBefore(before(), () => {}); now = 200; await capture.observeToolAfter(after()); expect(observations[0]?.observedAssertions).toEqual([ @@ -567,7 +572,7 @@ test("a verified native shell record permits a secure complete managed-output re readFullOutput: (path) => readHostToolOutput(path, dataHome), }); capture.arm("ses_host", workspace, prepared); - await capture.observeToolBefore(before()); + await capture.observeToolBefore(before(), () => {}); now = 200; capture.observeShellEvent(shellEvent(path)); capture.observeShellEvent(exited()); @@ -599,7 +604,7 @@ test.each([ }); const path = join(workspace, "sh_native.out"); capture.arm("ses_host", workspace, prepared); - await capture.observeToolBefore(before()); + await capture.observeToolBefore(before(), () => {}); now = 200; if (changes) { capture.observeShellEvent(shellEvent(path, changes)); @@ -660,7 +665,7 @@ test("receipts become available only after source-bound observation persistence" }, }); capture.arm("ses_host", workspace, prepared, origin); - await capture.observeToolBefore(before()); + await capture.observeToolBefore(before(), () => {}); const completion = capture.observeToolAfter(after()); for (let step = 0; step < 10 && !pending; step++) await Promise.resolve(); expect(receipts).toEqual([]); @@ -676,7 +681,7 @@ test("receipts become available only after source-bound observation persistence" test("a nonzero completed shell vetoes a passing receipt", async () => { const { capture } = fixture(); capture.arm("ses_host", workspace, prepared); - await capture.observeToolBefore(before()); + await capture.observeToolBefore(before(), () => {}); const completed = after({ exit: 7, truncated: false, @@ -707,8 +712,11 @@ test("competing shell-before callbacks cannot replace the first native call", as }, }); capture.arm("ses_host", workspace, prepared); - const first = capture.observeToolBefore(before()); - await capture.observeToolBefore(before({ id: CallID.make("second") })); + const first = capture.observeToolBefore(before(), () => {}); + await capture.observeToolBefore( + before({ id: CallID.make("second") }), + () => {}, + ); if (!release) throw new Error("First native identity read did not start."); release(); await first; @@ -739,7 +747,7 @@ test.each([ readFullOutput: async () => full, }); capture.arm("ses_host", workspace, prepared); - await capture.observeToolBefore(before()); + await capture.observeToolBefore(before(), () => {}); now = 200; capture.observeShellEvent(shellEvent(path)); capture.observeShellEvent(exited()); @@ -763,3 +771,188 @@ test.each([ }); }, ); + +test("a stale before read cannot delete its successor capture", async () => { + const entered = Promise.withResolvers(); + const release = Promise.withResolvers(); + let first = true; + let current = true; + const { capture, observations } = fixture({ + readSession: async (id) => { + if (first) { + first = false; + entered.resolve(); + await release.promise; + throw new Error("old native read failed"); + } + return { id, agent: "build", location: { directory: workspace } }; + }, + }); + capture.arm("ses_host", workspace, prepared); + const pending = capture + .observeToolBefore(before(), () => { + if (!current) throw new Error("stale owner"); + }) + .catch(String); + await entered.promise; + current = false; + capture.cancel("ses_host"); + capture.arm("ses_host", workspace, prepared); + release.resolve(); + await pending; + expect(capture.pendingCount()).toBe(1); + await capture.observeToolBefore( + before({ id: CallID.make("fresh") }), + () => {}, + ); + const result = after(undefined, {}, { id: CallID.make("fresh") }); + await capture.observeToolAfter(result); + expect(observations.length).toBe(1); + if (result.status !== "completed") + throw new Error("Expected completed shell."); + expect(JSON.stringify(result.result.content)).toContain("[flow-validation]"); +}); + +test("a stale after read cannot persist or append a receipt", async () => { + const entered = Promise.withResolvers(); + const release = Promise.withResolvers(); + let reads = 0; + let current = true; + const { capture, observations } = fixture({ + readSession: async (id) => { + if (++reads === 2) { + entered.resolve(); + await release.promise; + } + return { id, agent: "build", location: { directory: workspace } }; + }, + }); + capture.arm("ses_host", workspace, prepared); + await capture.observeToolBefore(before(), () => { + if (!current) throw new Error("stale owner"); + }); + const result = after(); + const original = structuredClone(result); + const pending = capture.observeToolAfter(result).catch(String); + await entered.promise; + current = false; + capture.cancel("ses_host"); + capture.arm("ses_host", workspace, prepared); + release.resolve(); + await pending; + expect(observations).toEqual([]); + expect(result).toEqual(original); + expect(capture.pendingCount()).toBe(1); +}); + +for (const boundary of ["session", "report", "output", "persistence"] as const) + for (const order of ["old-first", "fresh-first"] as const) + for (const reject of [false, true]) + test(`stale ${boundary} ${reject ? "rejection" : "completion"} preserves ${order} capture and receipts`, async () => { + const entered = Promise.withResolvers(); + const release = Promise.withResolvers(); + let live = true; + let serial = 0; + let reports = 0; + let sessions = 0; + let outputs = 0; + let persists = 0; + const saved: ObservedValidation[] = []; + const receipts: string[] = []; + const pause = async () => { + entered.resolve(); + await release.promise; + if (reject) throw new Error("old observation rejected"); + }; + const { capture } = fixture({ + randomId: () => `capture-${++serial}`, + now: () => 200, + readSession: async (id) => { + if (++sessions === 2 && boundary === "session") await pause(); + return { id, agent: "build", location: { directory: workspace } }; + }, + readReport: async () => { + if (++reports === 2 && boundary === "report") await pause(); + return { + text: '', + modifiedMs: 201, + }; + }, + readFullOutput: async () => { + if (++outputs === 1 && boundary === "output") await pause(); + return `prefix${tail}`; + }, + persistObservation: async (_workspace, value, assertAuthorized) => { + if (++persists === 1 && boundary === "persistence") await pause(); + assertAuthorized(); + saved.push(value); + return persisted(value); + }, + onRecorded: (receipt) => { + receipts.push(receipt.captureId); + }, + }); + const armed = + boundary === "report" + ? { + ...prepared, + assertions: ["passes"], + resultsPath: "results.xml", + } + : prepared; + const origin = { + hostSessionId: "ses_host", + sessionId: "flow", + authority: "user", + assistantId: "msg_assistant", + featureId: prepared.featureId, + runId: prepared.runId, + sourceDigest: prepared.sourceDigest, + command: prepared.command, + scope: prepared.scope, + hostPlatform: prepared.hostPlatform, + intent: prepared.intent, + assertions: armed.assertions, + }; + capture.arm("ses_host", workspace, armed, origin); + await capture.observeToolBefore(before(), () => { + if (!live) throw new Error("stale owner"); + }); + const path = join(workspace, "sh_native.out"); + if (boundary === "output") { + capture.observeShellEvent( + shellEvent(path, { time: { started: 200 } }), + ); + capture.observeShellEvent(exited()); + } + const old = boundary === "output" ? spilled(path) : after(); + const original = structuredClone(old); + const pending = capture.observeToolAfter(old).catch(String); + await entered.promise; + live = false; + capture.cancel("ses_host"); + capture.arm("ses_host", workspace, prepared, origin); + const freshBefore = before({ + id: CallID.make("fresh-call"), + messageID: SessionMessage.ID.make("msg_fresh"), + }); + const freshAfter = after(undefined, {}, freshBefore); + const fresh = async () => { + await capture.observeToolBefore(freshBefore, () => {}); + expect((await capture.observeToolAfter(freshAfter))?.id).toBe( + "capture-2", + ); + }; + if (order === "fresh-first") await fresh(); + release.resolve(); + await pending; + if (order === "old-first") { + expect(capture.pendingCount()).toBe(1); + await fresh(); + } + expect(old).toEqual(original); + expect(saved.map((value) => value.captureId)).toEqual(["capture-2"]); + expect(receipts).toEqual(["capture-2"]); + expect(JSON.stringify(freshAfter)).toContain("[flow-validation]"); + expect(capture.pendingCount()).toBe(0); + }); From 70e7184db3a1b45ecc30d76e205d935a2c4a236e Mon Sep 17 00:00:00 2001 From: Douwe de Vries Date: Sun, 25 Oct 2026 01:44:23 +0200 Subject: [PATCH 09/17] test(opencode): project terminal events into native idle records --- tests/fixtures/native-flow-host.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/fixtures/native-flow-host.ts b/tests/fixtures/native-flow-host.ts index 61353058..0c3d703b 100644 --- a/tests/fixtures/native-flow-host.ts +++ b/tests/fixtures/native-flow-host.ts @@ -770,17 +770,18 @@ export async function createNativeFlowHost( ) { const session = ensureSession(host); const completedAt = stamp(); + const eventID = `evt_${stamp()}`; session.time.idle = completedAt; session.outcome = outcome; history(host).push({ - id: SessionMessage.ID.create(), + id: SessionMessage.ID.fromEvent(eventID), type: "idle", outcome, time: { created: completedAt }, }); await events.emit({ - id: `evt_${stamp()}`, - created: stamp(), + id: eventID, + created: completedAt, durable: { aggregateID: host, seq: stamp(), version: 1 }, ...(outcome === "succeeded" ? { From 3a04a9f2a3efd3f44a1d44b6368afa623eabc966 Mon Sep 17 00:00:00 2001 From: Douwe de Vries Date: Sun, 25 Oct 2026 02:41:27 +0200 Subject: [PATCH 10/17] test(opencode): preserve running turns across queued prompts --- tests/lifecycle.test.ts | 164 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 164 insertions(+) diff --git a/tests/lifecycle.test.ts b/tests/lifecycle.test.ts index 744cc1ee..d1e5de8f 100644 --- a/tests/lifecycle.test.ts +++ b/tests/lifecycle.test.ts @@ -4884,3 +4884,167 @@ for (const rejectedAt of ["model", "agent", "session", "session-read"] as const) await f.lifecycle.dispose(); } }); + +test("queued public input preserves O until exact A consumption", async () => { + const f = await fixture(); + try { + await admitOrdinaryInput(f, "msg_queue_O", "Original O"); + const request = await startOwnedRequest(f); + const originalCall = ownershipNativeCall(f); + const original = await f.lifecycle.verifyTool(originalCall, "flow"); + if (original.kind !== "flow") throw new Error("Expected O Flow access."); + original.assertCurrent(); + const effects: string[] = []; + const tool = await effectTool(f, effects); + expect(await tool.execute({}, originalCall)).toEqual({ + content: [{ type: "text", text: "applied" }], + }); + expect(effects).toEqual(["effect"]); + const response = { + sessionID, + agent, + model, + kind: "primary" as const, + request, + response: new Response(new Uint8Array([0, 65, 10, 255, 13, 10])), + }; + await f.emit("http.response", response); + await f.emit("prompt", { + sessionID, + messageID: SessionMessage.ID.make("msg_queue_A"), + prompt: { text: "Queued A" }, + delivery: "queue", + }); + expect( + f.native.filter((item) => item.type === "user").map((item) => item.id), + ).toEqual(["msg_queue_O"]); + expect(f.context().messages.map((item) => item.id)).toEqual([ + "msg_queue_O", + ]); + expect(() => original.assertCurrent()).not.toThrow(); + expect(await tool.execute({}, originalCall)).toEqual({ + content: [{ type: "text", text: "applied" }], + }); + expect(effects).toEqual(["effect", "effect"]); + expect([...new Uint8Array(await response.response.arrayBuffer())]).toEqual([ + 0, 65, 10, 255, 13, 10, + ]); + await finishOwnedResponse(f, await startOwnedRequest(f)); + expect(() => original.assertCurrent()).toThrow("superseded"); + const nextOCall = ownershipNativeCall(f); + const nextO = await f.lifecycle.verifyTool(nextOCall, "flow"); + if (nextO.kind !== "flow") throw new Error("Expected next O Flow access."); + nextO.assertCurrent(); + expect(await tool.execute({}, nextOCall)).toEqual({ + content: [{ type: "text", text: "applied" }], + }); + expect(effects).toEqual(["effect", "effect", "effect"]); + f.native.push({ + type: "user", + id: "msg_queue_A", + text: "Queued A", + time: { created: 10 }, + }); + await finishOwnedResponse(f, await startOwnedRequest(f)); + const aCall = ownershipNativeCall(f); + const a = await f.lifecycle.verifyTool(aCall, "flow"); + if (a.kind !== "flow") throw new Error("Expected consumed A Flow access."); + a.assertCurrent(); + expect(await tool.execute({}, aCall)).toEqual({ + content: [{ type: "text", text: "applied" }], + }); + expect(() => nextO.assertCurrent()).toThrow(); + await expect(tool.execute({}, nextOCall)).rejects.toThrow(); + a.assertCurrent(); + expect(effects).toEqual(["effect", "effect", "effect", "effect"]); + expect(f.repository.saveCount).toBe(0); + } finally { + await f.lifecycle.dispose(); + } +}); + +test("queued public inputs consume A before pending B without idle", async () => { + const f = await fixture(); + try { + await admitOrdinaryInput(f, "msg_order_O", "Original O"); + await finishOwnedResponse(f, await startOwnedRequest(f)); + const oCall = ownershipNativeCall(f); + const o = await f.lifecycle.verifyTool(oCall, "flow"); + if (o.kind !== "flow") throw new Error("Expected O Flow access."); + const effects: string[] = []; + const tool = await effectTool(f, effects); + expect(await tool.execute({}, oCall)).toEqual({ + content: [{ type: "text", text: "applied" }], + }); + expect(effects).toEqual(["effect"]); + for (const [id, text] of [ + ["msg_order_A", "Queued A"], + ["msg_order_B", "Queued B"], + ] as const) { + await f.emit("prompt", { + sessionID, + messageID: SessionMessage.ID.make(id), + prompt: { text }, + delivery: "queue", + }); + expect(f.context().messages.map((item) => item.id)).toEqual([ + "msg_order_O", + ]); + expect(() => o.assertCurrent()).not.toThrow(); + expect(await tool.execute({}, oCall)).toEqual({ + content: [{ type: "text", text: "applied" }], + }); + } + expect(effects).toEqual(["effect", "effect", "effect"]); + f.native.push({ + type: "user", + id: "msg_order_A", + text: "Queued A", + time: { created: 10 }, + }); + expect(f.context().messages.map((item) => item.id)).toEqual([ + "msg_order_O", + "msg_order_A", + ]); + await finishOwnedResponse(f, await startOwnedRequest(f)); + const aCall = ownershipNativeCall(f); + const a = await f.lifecycle.verifyTool(aCall, "flow"); + if (a.kind !== "flow") throw new Error("Expected consumed A Flow access."); + a.assertCurrent(); + expect(await tool.execute({}, aCall)).toEqual({ + content: [{ type: "text", text: "applied" }], + }); + expect(() => o.assertCurrent()).toThrow(); + await expect(tool.execute({}, oCall)).rejects.toThrow(); + a.assertCurrent(); + expect(effects).toEqual(["effect", "effect", "effect", "effect"]); + f.native.push({ + type: "user", + id: "msg_order_B", + text: "Queued B", + time: { created: 11 }, + }); + expect(f.native.filter((item) => item.type === "idle")).toEqual([]); + expect(f.context().messages.map((item) => item.id)).toEqual([ + "msg_order_O", + "msg_order_A", + "msg_order_B", + ]); + await finishOwnedResponse(f, await startOwnedRequest(f)); + const bCall = ownershipNativeCall(f); + const b = await f.lifecycle.verifyTool(bCall, "flow"); + if (b.kind !== "flow") throw new Error("Expected consumed B Flow access."); + b.assertCurrent(); + expect(await tool.execute({}, bCall)).toEqual({ + content: [{ type: "text", text: "applied" }], + }); + expect(() => a.assertCurrent()).toThrow(); + expect(() => o.assertCurrent()).toThrow(); + await expect(tool.execute({}, aCall)).rejects.toThrow(); + b.assertCurrent(); + expect(effects).toEqual(["effect", "effect", "effect", "effect", "effect"]); + expect(f.repository.saveCount).toBe(0); + } finally { + await f.lifecycle.dispose(); + } +}); From c38dc14e3156e77c3970b7e3825f7a62bdf45d06 Mon Sep 17 00:00:00 2001 From: Douwe de Vries Date: Sun, 25 Oct 2026 03:27:35 +0100 Subject: [PATCH 11/17] fix(opencode): defer queued authority until native consumption --- .agents/plans/18-opencode-v2/decisions.tsv | 1 + .../18-opencode-v2/queued-input-review.md | 65 + .../18-opencode-v2/queued-native-proof.md | 56 + docs/adr/0018-opencode-v2-only.md | 5 +- docs/maintainer-contract.md | 8 +- src/application/recovery-policy.ts | 26 +- src/application/recovery-proposal.ts | 36 + src/platform/opencode/auto-compaction.ts | 167 ++- src/platform/opencode/auto-drive-dispatch.ts | 286 +++++ src/platform/opencode/auto-drive.ts | 506 ++++---- .../opencode/execution-attribution.ts | 111 +- src/platform/opencode/input-admission.ts | 709 +++++++++-- src/platform/opencode/lifecycle.ts | 499 +++++--- src/platform/opencode/plugin-composition.ts | 10 +- tests/auto-drive-dispatch.test.ts | 332 +++++ tests/auto-drive.test.ts | 183 ++- tests/distribution-and-surface.test.ts | 116 +- tests/execution-attribution.test.ts | 20 +- tests/fixtures/native-flow-host.ts | 21 +- .../fixtures/verified-continuation-origin.ts | 92 +- tests/input-admission.test.ts | 796 +++++++++++- tests/lifecycle.test.ts | 1103 ++++++++++++++++- tests/recovery-authority.test.ts | 116 +- tests/recovery-host.test.ts | 86 +- tests/recovery-policy.test.ts | 73 +- tests/recovery-proposal.test.ts | 28 + 26 files changed, 4614 insertions(+), 837 deletions(-) create mode 100644 .agents/plans/18-opencode-v2/queued-input-review.md create mode 100644 .agents/plans/18-opencode-v2/queued-native-proof.md create mode 100644 src/application/recovery-proposal.ts create mode 100644 src/platform/opencode/auto-drive-dispatch.ts create mode 100644 tests/auto-drive-dispatch.test.ts create mode 100644 tests/recovery-proposal.test.ts diff --git a/.agents/plans/18-opencode-v2/decisions.tsv b/.agents/plans/18-opencode-v2/decisions.tsv index 5f522b70..c783149e 100644 --- a/.agents/plans/18-opencode-v2/decisions.tsv +++ b/.agents/plans/18-opencode-v2/decisions.tsv @@ -72,3 +72,4 @@ ts phase decision why evidence result 2026-10-24T17:54:19Z delivery Defer further paid runs until dependency merges and green Sentry-reviewed implementation The operator corrected the release sequence; the completed two-dispatch smoke is separate from qualification /tmp/flow-v2-release-order-20261024/sequence.json; /tmp/flow-v2-prepush-e8d85c53-20261024/result.json; /tmp/flow-v2-prepush-e8d85c53-20261024/replay.log Dependency PRs merged; main CI pending; v2 branch still unpublished; no paid budget remains 2026-10-24T17:54:19Z dependencies Merge independently verified action updates before the v2 PR Each reviewed pin-only patch passed current checks and parent verification; the combined workflow tree passed actionlint /tmp/flow-dep-pr-195-review-20261024/review.txt; /tmp/flow-dep-pr-196-review-20261024/review.md; /tmp/flow-v2-release-order-20261024/pr196-parent-verification.json; b348552dcc03c5d58aa5410de2de66bf05ce1f89 Both squash merges confirmed on remote main; combined CI started; local merge has no conflicts 2026-10-24T17:56:12Z gate Hold the v2 push because merged main audit is red The operator explicitly required green main before publishing the v2 PR; existing v1 Zod and TOML dependencies now fail audit while the v2 dependency graph audits clean /tmp/flow-v2-release-order-20261024/main-dependency-audit.log; /tmp/flow-v2-release-order-20261024/v2-current-audit.log; https://github.com/ddv1982/flow-opencode/actions/runs/40118156331 Both dependency PRs merged; v2 remains local; no audit suppression or further paid work +2026-10-25T02:17:33Z queued-input-review Separate queued preparation from exact consumed provenance Native reproduction confirmed premature epoch revocation; four independent review defects were corrected without expanding automatic authority .agents/plans/18-opencode-v2/queued-input-review.md; /tmp/flow-v2-queued-input-20261025/core-review-r2/review.md; /tmp/flow-v2-queued-input-20261025/dispatch-review-r2/review.md Fresh source reviews and full check pass; exact archive/native replay still required; paid qualification remains on hold diff --git a/.agents/plans/18-opencode-v2/queued-input-review.md b/.agents/plans/18-opencode-v2/queued-input-review.md new file mode 100644 index 00000000..d645c987 --- /dev/null +++ b/.agents/plans/18-opencode-v2/queued-input-review.md @@ -0,0 +1,65 @@ +# Queued input review + +This records the source boundary reviewed for PR 197 after commit `70e7184`. +Paid qualification, merge and publication remain on hold. + +## Confirmed defect + +Native OpenCode 2.1.4 prepares queued input before delivering it. Flow revoked +the active request at preparation. In the pinned native reproduction, the +original shell completed, but its next provider request failed with an epoch +error. The queued follow-up completed. A final succeeded session concealed the +original failed idle. + +The correction keeps preparation as a registration without authority. Exact +native consumption proves the payload, selected identity and complete input +prefix before a new immutable scope replaces the predecessor. Explicit steer +keeps its immediate barrier. Inbox events can veto local scheduling but cannot +grant provenance. Authenticated dispatch intents commit send state only when a +matching receipt activates them. + +## Review findings + +The first source review found four unique defects. All have component controls. + +- D1. An obsolete retry's rejected projection read revoked a fresh primary. + Failure cleanup now checks the captured terminal owner. +- D2. Recovery expiry could throw after send state changed. The throwing claim + commitment now precedes those changes. +- D3. A checked checkpoint could not bridge a new public prefix. Only the + existing owned compaction record supplies that bridge. +- C1. An observed manual-compaction inbox item conflicted with its own control + identity. Final alias installation now permits only the checked non-positive + observation transfer and retains genuine identity conflicts. + +Fresh independent OpenAI core and dispatch reviews passed. The parent ran all +594 focused tests, including the Git-backed case, and all 25 original reviewer +control executions. The full source check passed 4,958 tests, skipped 27 and failed none. +The two original queue regressions were committed first in `3a04a9f2`; their +assertions remain unchanged. Earlier failed checks and review receipts remain. + +The shared fixture now recognizes completed controls rather than inventing a +prompt. Matched decision cases observe the actual coordinator completion, +synthetic submission or public waiting state. All 42 surface cases and their +original assertions remain. Post-stop ignored idles and the final unmatched +interrupt and idle still prove intake only. They do not prove universal +terminal-job completion. + +## Remaining qualification limits + +The native API exposes no atomic plugin admission mechanism for a public +preparation or delayed inbox event arriving after the final local gate. That +retained race is unqualified. Hook-only preparation cannot prove a pending +admission. No timeout supplies absence proof and automatic use stays enabled. +Native context overlap and auxiliary retry also remain unproved. + +Source checks do not replace the required new exact archive and unchanged +native reproduction. The queue control must show two requests for each turn, +both individual idles succeeded, the literal original shell result and real +`flow_status` with application status `ok`. Steer remains a separate control. +No account or paid-provider qualification follows from these synthetic checks. + +Local evidence is under `/tmp/flow-v2-queued-input-20261025/`. Independent reports +are `core-review-r2/review.md` and `dispatch-review-r2/review.md`. Parent checks +are in `parent-integration-r2/`. Package and native evidence get their own +identities rather than replacing the original failed reproduction. diff --git a/.agents/plans/18-opencode-v2/queued-native-proof.md b/.agents/plans/18-opencode-v2/queued-native-proof.md new file mode 100644 index 00000000..9755beea --- /dev/null +++ b/.agents/plans/18-opencode-v2/queued-native-proof.md @@ -0,0 +1,56 @@ +# Queued native completion proof + +This supersedes the two-idle acceptance requirement recorded in +[queued input review](queued-input-review.md). It changes the evidence contract, +not production behavior. The original failing gate remains preserved. + +OpenCode 2.1.4 settles an execution drain after processing its queued inputs. +The pinned runner and projector show that idle records describe that settlement. +They do not promise one idle per prompt. The successful queue run has one +closing succeeded idle after both inputs complete. + +The corrected contract keeps the request counts, exact input receipts, literal +tool results, application success, runtime identity and cleanup checks. It also +requires the original turn's completed error-free stop assistant before the +queued native user, the original second request replaying its shell result, +the queued request replaying that terminal text, and a matching successful root +settlement. Every retained idle must succeed. A final succeeded root alone is +insufficient. + +The exact new archive is +`sha256:a1f47a5086e237be0051ba7eacebfd6147893b7a9756a2392e3b01f0e03cfe9f`. +Its unpacked manifest is +`sha256:a390dc144123c871d6755b10f911bf8a0c23fdcca4daea4016f58701ddfac67f`. +The loaded server entry is +`sha256:51d620aec23ec37a3d86fb755c0b244fc61f21c3eacb8880d3f4857e78781c6f`. +All 12 installed files and 81 server plus five TUI map rows match the archive. +Those rows cover 83 unique paths. All 27 frozen source and documentation paths +match before and after native testing. + +The unchanged native driver observed original and queued request counts of +two and two. The literal shell completed with exit zero. The queued real +`flow_status` returned application status `ok`. All four assistants completed +without errors. Steer retained its separate one-and-two request control. +The candidate host and runner were awaited and confirmed stopped. No account +credentials or paid calls were used. + +An independent OpenAI adjudication verified the binary source and retained +trace. Its 78-check proposed contract passes the candidate and rejects the +baseline on eight checks. The parent reran it independently. Seven altered +evidence controls are rejected. A successful intermediate idle remains valid. +The original candidate gate remains failed on its two-idle assumption. Both +old and new driver logs retain duplicate-release cleanup messages, which do not +represent failed native assistants or execution idles. + +The same archive passed the unchanged native smoke's 119 assertions. Fresh core +and dispatch source reviews pass. Full source checks passed 4,958 tests with +27 skips and zero failures. These are synthetic compatibility checks. Paid +qualification remains on hold, and no original paid failure was reclassified. +The previously recorded host arbitration, overlap, auxiliary-retry and +intake-only observation limits remain. + +Evidence is under `/tmp/flow-v2-queued-input-20261025/`. The independent report is +`native-idle-contract-review-r1/review.md`; the parent rerun is +`parent-native-contract-r1/verification.json`. The accepted contract is recorded +separately in `candidate-r1/queue-native-drain-acceptance.json`. This proof record +is added after the measured source snapshot; it does not change packaged bytes. diff --git a/docs/adr/0018-opencode-v2-only.md b/docs/adr/0018-opencode-v2-only.md index 2447647b..0141831b 100644 --- a/docs/adr/0018-opencode-v2-only.md +++ b/docs/adr/0018-opencode-v2-only.md @@ -16,10 +16,11 @@ supply JSON Schema. Results retain Flow envelopes and guidance. Frozen inputs bind each primary to its native session, agent, model, assistant, call, and workspace. Flow selects HTTP for providers, models, and variants. Prompt hooks and event arrival order grant no authority. +Queued prompts preserve the running request. Unregistered native user and synthetic turns may use ordinary tools. They gain -no Flow-tool access, validation, recovery intent, or automatic lease. A fresh -observed prompt or Flow command establishes Flow provenance. Retired request +no Flow-tool access, validation, recovery intent, or automatic lease. A consumed +registered prompt or Flow command establishes Flow provenance. Retired request owners retain no transcript snapshots. EOF and consumer cancellation close only the captured HTTP request. Protected diff --git a/docs/maintainer-contract.md b/docs/maintainer-contract.md index e3f626d7..0db24a2d 100644 --- a/docs/maintainer-contract.md +++ b/docs/maintainer-contract.md @@ -57,10 +57,12 @@ must match the frozen inputs consumed by that model request. Later input cannot relabel an older assistant or supply authority from another host. Automatic compaction retains the epoch through its checked native control. -Its user-role checkpoint grants no approval. Real input, manual compaction of -automatic work, user interruption, or event-stream loss revokes the interval. +Its user-role checkpoint grants no approval. Steer or consumed input, manual +compaction of automatic work, user interruption, or event-stream loss revokes +the interval. Consumer cancellation ends only its captured HTTP request. -Stale unmatched callbacks cannot revoke a fresh source. +Queued prompts preserve the running request. +Stale callbacks cannot revoke a fresh source. Summary-only ordinary history grants no checkpoint execution. These checks add no Session v5 field. See [ADR 0018](adr/0018-opencode-v2-only.md). diff --git a/src/application/recovery-policy.ts b/src/application/recovery-policy.ts index 12dafc86..6767d9ab 100644 --- a/src/application/recovery-policy.ts +++ b/src/application/recovery-policy.ts @@ -5,6 +5,7 @@ import type { RecoveryProposal, RecoverySettings, } from "./recovery-contracts.js"; +import { RecoveryProposals } from "./recovery-proposal.js"; export { type RecoveryMutation, @@ -86,7 +87,7 @@ type Lease = { | Readonly<{ kind: "stale" | "cancelled" }> | Readonly<{ kind: "executed"; operationId: string; featureId: string }> | null; - proposalPrompt: string | null; + proposals: RecoveryProposals; }; type Host = { parents: Map; @@ -230,7 +231,7 @@ export class RecoveryController { pending: null, inFlight: false, last: null, - proposalPrompt: null, + proposals: new RecoveryProposals(), }; } #profile() { @@ -403,12 +404,12 @@ export class RecoveryController { } : { mode: "off", ...status }; } - proposalPrompt( + prepareProposalPrompt( host: string, session: string | undefined, revision: number, nextAction: string | null, - ): string | null { + ) { this.#expireLease(); const lease = this.#lease; if ( @@ -418,15 +419,14 @@ export class RecoveryController { nextAction !== "await-user-direction" ) return null; - const key = `${session}/${revision}`; - if ( - lease.proposalPrompt === key || - lease.calls >= lease.settings.maxCalls || - lease.controller.signal.aborted - ) - return null; - lease.proposalPrompt = key; - return "Recovery advice is enabled for this process. Call flow_status with a top-level recoveryProposal containing id, sessionId, expectedRevision and at most three candidates. Each candidate needs id, action retry or independent-feature, exact featureId, remedy, changedFromPreviousAttempt and actual findingIds. Advice cannot expand the approved plan or replace evidence. In shadow mode report the advice and stop. In delegated mode call only the exact recommended existing mutation, then repeat full validation and independent review."; + return lease.proposals.prepare(`${session}/${revision}`, () => { + this.#expireLease(); + return ( + this.#lease === lease && + !lease.controller.signal.aborted && + lease.calls < lease.settings.maxCalls + ); + }); } guard(context: RecoveryContext): RecoveryGuard { const identity = this.#lease; diff --git a/src/application/recovery-proposal.ts b/src/application/recovery-proposal.ts new file mode 100644 index 00000000..177b6063 --- /dev/null +++ b/src/application/recovery-proposal.ts @@ -0,0 +1,36 @@ +export type PreparedRecoveryProposal = Readonly<{ + text: string; + assertCurrent(): void; + commit(): void; +}>; +const advice = + "Recovery advice is enabled for this process. Call flow_status with a top-level recoveryProposal containing id, sessionId, expectedRevision and at most three candidates. Each candidate needs id, action retry or independent-feature, exact featureId, remedy, changedFromPreviousAttempt and actual findingIds. Advice cannot expand the approved plan or replace evidence. In shadow mode report the advice and stop. In delegated mode call only the exact recommended existing mutation, then repeat full validation and independent review."; +export class RecoveryProposals { + #committed: string | null = null; + #generation = 0; + prepare(key: string, valid: () => boolean): PreparedRecoveryProposal | null { + if (key === this.#committed || !valid()) return null; + const previous = this.#committed, + generation = this.#generation; + let committed = false; + const assertCurrent = () => { + if ( + committed || + !valid() || + this.#committed !== previous || + this.#generation !== generation + ) + throw new Error("Flow recovery proposal was superseded."); + }; + return Object.freeze({ + text: advice, + assertCurrent, + commit: () => { + assertCurrent(); + committed = true; + this.#committed = key; + this.#generation++; + }, + }); + } +} diff --git a/src/platform/opencode/auto-compaction.ts b/src/platform/opencode/auto-compaction.ts index 7bc69624..dfddb281 100644 --- a/src/platform/opencode/auto-compaction.ts +++ b/src/platform/opencode/auto-compaction.ts @@ -1,5 +1,5 @@ import { isDeepStrictEqual as same } from "node:util"; -import type { SessionMessageInfo } from "@opencode/client"; +import type { SessionInfo, SessionMessageInfo } from "@opencode/client"; import type { SessionCompaction, SessionContext, @@ -12,8 +12,19 @@ import { nativeInputIDs, sameDelivery, } from "./execution-attribution.js"; -import type { InputAdmissions, InputScope } from "./input-admission.js"; +import type { + InputAdmissions, + ResolvedSource, + SourceOperation, +} from "./input-admission.js"; +const completedPrefix = Symbol(); +export type CompletedPrefix = Readonly<{ + [completedPrefix]: true; + sourceOrigin: InputOrigin; + consumed: readonly string[]; + checkpoint: string; +}>; type Native = readonly SessionMessageInfo[]; type ControlProof = Readonly<{ id: string; @@ -71,14 +82,20 @@ function running( type CompactionOptions = Readonly<{ inputs: InputAdmissions; readNative(host: string): Promise; - failure(scope: InputScope): void; + readSelected(host: string): Promise; + captureReplacement(host: string): Readonly<{ + check(input: SessionCompaction, native: Native, changing: boolean): void; + current(): boolean; + supersede(): void; + }>; + failure(operation: SourceOperation, stillExpected: () => boolean): void; hasActivePrimary(host: string): boolean; guidance(host: string): string | null; observe(host: string, origin: InputOrigin, source: string, id: string): void; }>; export class CompactionBoundary { readonly #records = new Map(); - readonly #pending = new Map(); + readonly #pending = new Map(); readonly #options: CompactionOptions; constructor(options: CompactionOptions) { this.#options = options; @@ -86,14 +103,22 @@ export class CompactionBoundary { #key(host: string, id: string) { return JSON.stringify([host, id]); } - async admit(input: SessionCompaction): Promise { - const scope = this.#options.inputs.capture(input); + async admit(value: SessionCompaction): Promise { + const input = { + ...value, + model: { ...value.model }, + messages: structuredClone(value.messages), + }; + const effects = this.#options.captureReplacement(input.sessionID); + const operation = this.#options.inputs.openSource(input); + let scope = operation.scope; + let resolved: ResolvedSource | undefined; const prior = this.#pending.get(scope.host); if (prior?.current()) { - scope.release(); + operation.release(); throw new Error("Flow compaction callbacks overlap."); } - this.#pending.set(scope.host, scope); + this.#pending.set(scope.host, operation); try { const first = running( input, @@ -106,19 +131,84 @@ export class CompactionBoundary { ); if (!same(first.proof, second.proof)) throw new Error("Flow automatic compaction control identity changed."); - const source = await this.#options.inputs.evidence( - scope, - second.prefix, - second.proof.delivery, - ); - const evidence = - source.kind === "unregistered" ? { kind: source.lineage } : source; + const selected = await scope.wait(this.#options.readSelected(scope.host)); const final = running( input, await scope.wait(this.#options.readNative(scope.host)), ); if (!same(first.proof, final.proof)) throw new Error("Flow automatic compaction control identity changed."); + const existing = this.#records.get(this.#key(scope.host, final.proof.id)); + const transfer = existing + ? undefined + : this.#options.inputs.assertCheckpointAvailable( + scope.host, + final.proof.id, + ); + resolved = await operation.resolve(final.prefix, selected, { + check: (changing, source) => { + if ( + existing && + (!source || + !same(final.proof, { + id: existing.id, + reason: existing.reason, + consumed: existing.consumed, + delivery: existing.delivery, + }) || + (existing.kind === "continuation" && + (source.kind !== "live" || + existing.sourceOrigin !== source.origin))) + ) + throw new Error( + "Flow automatic compaction control identity changed.", + ); + const evidence = + source?.kind === "unregistered" ? { kind: source.lineage } : source; + if ( + evidence?.kind === "retired-automatic" || + evidence?.kind === "rejected" + ) + throw new Error("Flow execution epoch was revoked."); + if (evidence && evidence.kind !== "live") { + if (final.proof.reason !== "manual") + throw new Error( + evidence.kind === "unknown-history" + ? "Flow compaction has no accepted input origin." + : "Flow execution epoch was revoked.", + ); + if ( + this.#options.guidance(scope.host) || + this.#options.hasActivePrimary(scope.host) + ) + throw new Error( + "Flow manual summary cannot overlap a live execution.", + ); + } + if ( + final.proof.reason === "manual" && + evidence?.kind === "live" && + evidence.origin.authority + ) + throw new Error( + "Flow cannot retain authority through unverified or manual compaction.", + ); + effects.check(input, final.prefix, changing); + if ( + final.proof.reason === "manual" && + changing && + this.#options.guidance(scope.host) + ) + throw new Error( + "Flow cannot retain authority through unverified or manual compaction.", + ); + }, + supersede: effects.supersede, + }); + scope = resolved.scope; + const source = resolved.evidence; + const evidence = + source.kind === "unregistered" ? { kind: source.lineage } : source; let result: CompactionResult; if (evidence.kind === "live") { const origin = this.completed(input, final.prefix) ?? evidence.origin; @@ -208,17 +298,58 @@ export class CompactionBoundary { scope, result.id, result.kind === "continuation" ? result.origin : null, + transfer, ); this.#records.set(this.#key(scope.host, result.id), result); return result; } catch (error) { - this.#options.failure(scope); + this.#options.failure(operation, effects.current); throw error; } finally { - if (this.#pending.get(scope.host) === scope) + if (this.#pending.get(scope.host) === operation) this.#pending.delete(scope.host); - scope.release(); + operation.release(); + } + } + prefixBridge( + input: Pick, + native: Native, + predecessor?: InputOrigin, + ): CompletedPrefix | undefined { + const first = modelInputIDs(input)[0]; + if (!first || !predecessor) return; + const record = this.#records.get(this.#key(input.sessionID, first)); + if (record?.kind !== "continuation") return; + const control = native.findLast((message) => message.type === "compaction"); + if ( + control?.type !== "compaction" || + control.id !== record.id || + control.reason !== record.reason || + control.status !== "completed" || + nativeInputIDs(native)[0] !== record.id + ) + throw new Error("Flow automatic compaction control identity changed."); + this.#options.inputs.assertLive(record.origin); + let prior = record; + while (prior.sourceOrigin !== predecessor) { + const checkpoint = prior.consumed[0]; + if (prior.consumed.length !== 1 || !checkpoint) return; + const previous = this.#records.get( + this.#key(input.sessionID, checkpoint), + ); + if ( + previous?.kind !== "continuation" || + prior.sourceOrigin !== previous.origin + ) + return; + prior = previous; } + return Object.freeze({ + [completedPrefix]: true as const, + sourceOrigin: prior.sourceOrigin, + consumed: prior.consumed, + checkpoint: record.id, + }); } completed( input: Pick, diff --git a/src/platform/opencode/auto-drive-dispatch.ts b/src/platform/opencode/auto-drive-dispatch.ts new file mode 100644 index 00000000..dd9513d4 --- /dev/null +++ b/src/platform/opencode/auto-drive-dispatch.ts @@ -0,0 +1,286 @@ +import type { PreparedRecoveryProposal } from "../../application/recovery-proposal.js"; +import type { + AutoDriveAuthority, + AutoDriveDelivery, + AutoDriveProjection, +} from "./auto-drive.js"; + +const dispatchBrand = Symbol(); +export type DispatchIntent = Readonly<{ [dispatchBrand]: true }>; +export type DispatchResult = + | Readonly<{ kind: "sent"; source: string }> + | Readonly<{ kind: "withheld"; reason: "pending-user" | "input-settlement" }>; +export type IdleOwner = Readonly<{ + current(): boolean; + recheck(): Promise; +}>; +export type DispatchCapture = Readonly<{ + authority: AutoDriveAuthority; + source: string | null; + current(): boolean; +}>; +type Checkpoint = { revision: number; answered: boolean; advance?: number }; +type Draft = { sessionId: string; revision: number; prompted: boolean }; +export type SendEffect = + | Readonly<{ kind: "initial" }> + | Readonly<{ + kind: "continuation"; + revision: number; + clearCheckpoint: boolean; + repairKey: string | null; + }> + | Readonly<{ kind: "draft"; draft: Draft }> + | Readonly<{ + kind: "recovery"; + revision: number; + proposal: PreparedRecoveryProposal; + }> + | Readonly<{ + kind: "handback-and-wait" | "handback-or-deactivate"; + projection: AutoDriveProjection; + }>; +export type AutomaticPrompt = ( + host: string, + text: string, + delivery: AutoDriveDelivery, + metadata: Readonly>, + intent: DispatchIntent, +) => Promise; +type PendingDispatch = { + readonly intent: DispatchIntent; + readonly capture: DispatchCapture; + readonly checkpoint: Checkpoint | null; + readonly draft: Draft | null; + readonly draftSession: string | null; + readonly draftRevision: number | null; + readonly owner: IdleOwner | undefined; + readonly effect: SendEffect; + readonly clearPendingReply: boolean; + state: "prepared" | "accepted" | "withheld"; + source: string | null; +}; +type SendActivation = Readonly<{ + timing: "active" | "waiting-for-user" | "paused"; + disposition?: + | "continuation" + | "plan-approval" + | "user-direction" + | "handback"; +}>; +export class AutoDriveDispatch { + delivery: AutoDriveDelivery | null = null; + lastPromptedRevision: number | null = null; + handbackPromptedRevision: number | null = null; + checkpoint: Checkpoint | null = null; + pendingReply = false; + messageId: string | null = null; + manualContinuation: { revision: number } | null = null; + ownInspectDraft: Draft | null = null; + #pending: PendingDispatch | null = null; + #deferred: Readonly<{ owner: IdleOwner; capture: DispatchCapture }> | null = + null; + #invalidated = false; + #cancelled = Promise.withResolvers(); + readonly #repairs: Set; + constructor(repairs: Set) { + this.#repairs = repairs; + void this.#cancelled.promise.catch(() => {}); + } + invalidate(): void { + this.#invalidated = true; + this.#cancelled.reject(new Error("Flow automatic lease was revoked.")); + this.#pending = null; + this.#deferred = null; + } + renew(): void { + this.invalidate(); + this.#cancelled = Promise.withResolvers(); + void this.#cancelled.promise.catch(() => {}); + this.#invalidated = false; + } + get cancelled(): Promise { + return this.#cancelled.promise; + } + get replacesCheckpoint(): boolean { + return ( + this.#pending?.state === "prepared" && + this.#pending.effect.kind === "continuation" && + this.#pending.effect.clearCheckpoint + ); + } + get deferred(): boolean { + return this.#deferred !== null; + } + waitAt(revision: number, sent = false): void { + this.checkpoint = + this.checkpoint?.revision === revision + ? this.checkpoint + : { revision, answered: false }; + this.checkpoint.answered = false; + if (!sent) this.messageId = null; + this.lastPromptedRevision = null; + } + isPrepared(intent: DispatchIntent): boolean { + const record = this.#pending; + return ( + !this.#invalidated && + record?.intent === intent && + record.state === "prepared" && + record.capture.current() && + (!record.owner || record.owner.current()) && + this.checkpoint === record.checkpoint && + this.ownInspectDraft === record.draft && + (record.effect.kind !== "draft" || + (record.draft?.sessionId === record.draftSession && + record.draft?.revision === record.draftRevision && + !record.draft?.prompted)) + ); + } + activate( + intent: DispatchIntent | undefined, + source: string, + delivery: AutoDriveDelivery, + ): SendActivation { + const record = this.#pending; + if ( + intent && + (!record || + record.intent !== intent || + record.state !== "prepared" || + this.#invalidated || + !record.capture.current() || + this.checkpoint !== record.checkpoint || + this.ownInspectDraft !== record.draft) + ) + throw new Error("Flow automatic dispatch intent was superseded."); + const effect = intent ? record?.effect : undefined; + if ( + effect?.kind === "draft" && + (this.ownInspectDraft !== effect.draft || + effect.draft.prompted || + effect.draft.sessionId !== record?.draftSession || + effect.draft.revision !== record?.draftRevision) + ) + throw new Error("Flow inspection draft changed before admission."); + if (effect?.kind === "recovery") { + effect.proposal.assertCurrent(); + effect.proposal.commit(); + } + if (effect?.kind === "initial") this.lastPromptedRevision = 0; + if (effect?.kind === "continuation") { + if (effect.clearCheckpoint) this.checkpoint = null; + this.lastPromptedRevision = effect.revision; + } + if (effect?.kind === "recovery") this.waitAt(effect.revision, true); + this.delivery = delivery; + this.messageId = source; + if ( + !this.manualContinuation && + !this.checkpoint && + this.lastPromptedRevision !== null + ) + this.checkpoint = { + revision: this.lastPromptedRevision, + answered: false, + }; + let activation: SendActivation = { timing: "active" }; + if (effect && record) { + if (record.clearPendingReply) this.pendingReply = false; + if (effect.kind === "draft") effect.draft.prompted = true; + if (effect.kind === "continuation") { + if (effect.repairKey) this.#repairs.add(effect.repairKey); + activation = { timing: "active", disposition: "continuation" }; + } + if (effect.kind === "recovery") { + this.handbackPromptedRevision = effect.revision; + } + if ( + effect.kind === "handback-and-wait" || + effect.kind === "handback-or-deactivate" + ) { + this.handbackPromptedRevision = effect.projection.revision; + if (effect.kind === "handback-and-wait") { + this.waitAt(effect.projection.revision, true); + activation = { + timing: "waiting-for-user", + disposition: + effect.projection.nextAction === "flow_plan_approve" + ? "plan-approval" + : "user-direction", + }; + } else activation = { timing: "paused", disposition: "handback" }; + } + record.source = source; + record.state = "accepted"; + } + return activation; + } + async send( + prompt: AutomaticPrompt, + host: string, + text: string, + metadata: Readonly>, + effect: SendEffect, + clearPendingReply: boolean, + capture: DispatchCapture, + owner?: IdleOwner, + ): Promise { + if ( + this.#invalidated || + !capture.current() || + !this.delivery || + (owner && !owner.current()) + ) + throw new Error("Flow automatic dispatch was superseded."); + if (this.#pending) throw new Error("Flow automatic dispatches overlap."); + const record: PendingDispatch = { + intent: Object.freeze({ [dispatchBrand]: true }), + capture, + checkpoint: this.checkpoint, + draft: this.ownInspectDraft, + draftSession: this.ownInspectDraft?.sessionId ?? null, + draftRevision: this.ownInspectDraft?.revision ?? null, + owner, + effect, + clearPendingReply, + state: "prepared", + source: null, + }; + this.#pending = record; + try { + const result = await prompt( + host, + text, + this.delivery, + metadata, + record.intent, + ); + if (result.kind === "withheld") { + if (record.state !== "prepared") + throw new Error("Flow admitted dispatch cannot be withheld."); + record.state = "withheld"; + if (!this.#invalidated && capture.current() && owner?.current()) + this.#deferred = { owner, capture }; + } else if (record.state !== "accepted" || record.source !== result.source) + throw new Error( + "Flow send completed without authenticated intent activation.", + ); + return result; + } finally { + if (this.#pending === record) this.#pending = null; + } + } + async takeDeferred(): Promise { + const ticket = this.#deferred; + this.#deferred = null; + if ( + !this.#invalidated && + ticket?.capture.current() && + ticket.owner.current() && + (await ticket.owner.recheck()) && + ticket.capture.current() + ) + return ticket.owner; + return null; + } +} diff --git a/src/platform/opencode/auto-drive.ts b/src/platform/opencode/auto-drive.ts index 44b4afde..c5d30739 100644 --- a/src/platform/opencode/auto-drive.ts +++ b/src/platform/opencode/auto-drive.ts @@ -8,6 +8,14 @@ import { isHandback, isPendingReviewer, } from "./auto-drive-decision.js"; +import { + AutoDriveDispatch, + type AutomaticPrompt, + type DispatchCapture, + type DispatchIntent, + type IdleOwner, + type SendEffect, +} from "./auto-drive-dispatch.js"; import { AUTO_INITIAL_PROMPT, autoCompactionMessage, @@ -116,7 +124,6 @@ export function autoDriveDelivery( ...(variant === undefined ? {} : { variant }), }; } -type Checkpoint = { revision: number; answered: boolean; advance?: number }; export type ProcessLocalAutoContinuationSupport = | "supported" | "unsupported" @@ -129,7 +136,6 @@ export interface AutoTimingSnapshot { readonly waitingForUserMs: number; readonly pausedTimeExcluded: true; } -/** The single in-memory continuation lease; nothing here is durable. */ type Lease = { hostSessionId: string; requestGoal: string; @@ -137,17 +143,10 @@ type Lease = { intent: AutoGoalIntent; token: string; baseline: AutoDriveProjection | null; - delivery: AutoDriveDelivery | null; - lastPromptedRevision: number | null; - handbackPromptedRevision: number | null; - checkpoint: Checkpoint | null; - pendingReply: boolean; - /** Serializes work so concurrent idle events cannot double-prompt. */ + send: AutoDriveDispatch; inFlight: "status" | "reply-status" | "prompt" | null; idlePending: boolean; - messageId: string | null; assistantOrigins: Map; - manualContinuation: { revision: number } | null; validation: { revision: number; outcome: ValidationContinuationOutcome; @@ -157,11 +156,6 @@ type Lease = { featureId: string; runId: string; } | null; - ownInspectDraft: { - sessionId: string; - revision: number; - prompted: boolean; - } | null; }; type TimingField = "state" | "activeMs" | "waitingForUserMs"; type Timing = { @@ -171,12 +165,7 @@ type AutoDriveOptions = Readonly<{ recovery?: RecoveryController; readProjection: () => Promise; readSourceDigest?: () => Promise; - prompt: ( - sessionID: string, - prompt: string, - delivery: AutoDriveDelivery, - metadata: Readonly>, - ) => Promise; + prompt: AutomaticPrompt; onWarning?: ((message: string) => void) | undefined; createToken?: (() => string) | undefined; now?: (() => number) | undefined; @@ -232,9 +221,7 @@ export class AutoDriveCoordinator { #warn(message: string): void { try { this.#options.onWarning?.(message); - } catch { - // Warning delivery is best-effort; a failed host sink must not stop the lease. - } + } catch {} } #stop( lease: Lease, @@ -256,50 +243,75 @@ export class AutoDriveCoordinator { ); } #waitAt(lease: Lease, revision: number): void { - const current = lease.checkpoint; - lease.checkpoint = - current?.revision === revision ? current : { revision, answered: false }; - lease.checkpoint.answered = false; - lease.messageId = null; - lease.lastPromptedRevision = null; + lease.send.waitAt(revision); this.#setTiming("waiting-for-user"); } - async #promptHandback( + #waiting(lease: Lease, projection: AutoDriveProjection): void { + this.#disposition = { + state: "waiting", + reason: + projection.nextAction === "flow_plan_approve" + ? "plan-approval" + : "user-direction", + }; + this.#waitAt(lease, projection.revision); + } + async #send( lease: Lease, - projection: AutoDriveProjection, + text: string, + effect: SendEffect, + clearPendingReply: boolean, + owner?: IdleOwner, ): Promise { - if (!isHandback(projection)) return; - if (lease.handbackPromptedRevision === projection.revision) return; - if (!lease.delivery) return; - const epoch = this.#epoch; - const token = lease.token; - lease.handbackPromptedRevision = projection.revision; - lease.messageId = null; - this.#setTiming("active"); + const capture = this.captureDispatch(lease.hostSessionId, lease.token); + if (!capture || !lease.send.delivery || (owner && !owner.current())) return; lease.inFlight = "prompt"; try { - await this.#options.prompt( + const result = await lease.send.send( + this.#options.prompt, lease.hostSessionId, - autoHandbackMessage(projection), - lease.delivery, - { [FLOW_AUTO_METADATA_KEY]: token }, + text, + { [FLOW_AUTO_METADATA_KEY]: lease.token }, + effect, + clearPendingReply, + capture, + owner, ); + if (result.kind === "withheld") lease.idlePending = false; } catch (error) { - if (lease.token !== token || this.#epoch !== epoch) return; - this.#stop( - lease, - `Flow auto prompt failed: ${String(error)}`, - "prompt-error", - ); + if ( + (!owner || owner.current()) && + (capture.current() || + (this.#lease === lease && + this.#epoch === capture.authority.epoch && + lease.token === capture.authority.token)) + ) + this.#stop( + lease, + `Flow auto prompt failed: ${String(error)}`, + "prompt-error", + ); } } - async #read(lease: Lease): Promise { + async resumeDeferred(host: string): Promise { + const lease = this.#lease; + if (lease?.hostSessionId !== host) return; + const owner = await lease.send.takeDeferred(); + if (owner && this.#lease === lease) await this.onIdle(host, owner); + } + async #read( + lease: Lease, + owner?: IdleOwner, + ): Promise { const epoch = this.#epoch; try { - const projection = await this.#options.readProjection(); + const projection = await Promise.race([ + this.#options.readProjection(), + lease.send.cancelled, + ]); return this.#epoch === epoch ? projection : null; } catch (error) { - if (this.#epoch !== epoch) return null; + if (this.#epoch !== epoch || (owner && !owner.current())) return null; this.#stop( lease, `Flow auto status failed: ${String(error)}`, @@ -320,6 +332,7 @@ export class AutoDriveCoordinator { intent?: AutoGoalIntent; }> = {}, ): Promise> { + this.#lease?.send.invalidate(); this.#epoch++; this.#authorityReady = true; const token = options.token ?? this.createInvocationToken(); @@ -338,18 +351,11 @@ export class AutoDriveCoordinator { intent: options.intent ?? "uncertain", token, baseline: null, - delivery: null, - lastPromptedRevision: null, - handbackPromptedRevision: null, - checkpoint: null, - pendingReply: false, + send: new AutoDriveDispatch(this.#repairHandoffs), inFlight: null, idlePending: false, - messageId: null, assistantOrigins: new Map(), - manualContinuation: null, validation: null, - ownInspectDraft: null, }; const lease = this.#lease; const baseline = await this.#read(lease); @@ -357,7 +363,7 @@ export class AutoDriveCoordinator { if (!baseline) throw new Error("Flow auto-drive compact status failed."); lease.baseline = baseline; if (!isPendingReviewer(baseline)) - lease.checkpoint = { revision: baseline.revision, answered: false }; + lease.send.checkpoint = { revision: baseline.revision, answered: false }; return { [FLOW_AUTO_METADATA_KEY]: token }; } deactivate( @@ -379,8 +385,8 @@ export class AutoDriveCoordinator { if ( projection.sessionId !== lease.baseline?.sessionId || projection.revision < - (lease.manualContinuation?.revision ?? - lease.checkpoint?.revision ?? + (lease.send.manualContinuation?.revision ?? + lease.send.checkpoint?.revision ?? 0) || !["blocked", "ready", "running"].includes(projection.status) ) { @@ -388,11 +394,14 @@ export class AutoDriveCoordinator { return null; } if (projection.nextAction === "await-user-direction") { - lease.checkpoint = { revision: projection.revision, answered: false }; - lease.manualContinuation = null; + lease.send.checkpoint = { + revision: projection.revision, + answered: false, + }; + lease.send.manualContinuation = null; } else { - lease.checkpoint = null; - lease.manualContinuation = { revision: projection.revision }; + lease.send.checkpoint = null; + lease.send.manualContinuation = { revision: projection.revision }; } return lease.token; } @@ -408,6 +417,7 @@ export class AutoDriveCoordinator { this.#setTiming("inactive"); this.#disposition = { state: "inactive", reason }; } + this.#lease?.send.invalidate(); this.#lease = null; } async observeMessage( @@ -415,6 +425,7 @@ export class AutoDriveCoordinator { delivery: AutoDriveDelivery, parts: readonly AutoDriveMessagePart[], messageId: string, + intent?: DispatchIntent, ): Promise<"accepted" | "accepted-continuation" | "stale-continuation"> { const lease = this.#lease; const message = inspectMessage(parts); @@ -429,41 +440,46 @@ export class AutoDriveCoordinator { lease.token !== message.token ) return "stale-continuation"; + const activation = lease.send.activate(intent, messageId, delivery); this.#authorityReady = true; - lease.delivery = delivery; - lease.messageId = messageId; + this.#setTiming(activation.timing); + if (activation.disposition === "continuation") + this.#disposition = { state: "active", reason: "continuation" }; if ( - !lease.manualContinuation && - !lease.checkpoint && - lease.lastPromptedRevision !== null + activation.disposition === "plan-approval" || + activation.disposition === "user-direction" ) - lease.checkpoint = { - revision: lease.lastPromptedRevision, - answered: false, + this.#disposition = { + state: "waiting", + reason: activation.disposition, }; - this.#setTiming("active"); + if (activation.disposition === "handback") + this.#disposition = { state: "paused", reason: "handback" }; return "accepted-continuation"; } if (lease?.hostSessionId !== hostSessionId || !message.user) return "accepted"; - if (lease.ownInspectDraft?.prompted) { + if (lease.send.ownInspectDraft?.prompted) { this.deactivate(hostSessionId); return "accepted"; } - lease.ownInspectDraft = null; - if (lease.checkpoint?.answered || lease.pendingReply) { + lease.send.ownInspectDraft = null; + if (lease.send.checkpoint?.answered || lease.send.pendingReply) { this.deactivate(hostSessionId); return "accepted"; } - if (lease.checkpoint) delete lease.checkpoint.advance; - if (!lease.checkpoint && lease.inFlight !== "status") + if (lease.send.checkpoint) delete lease.send.checkpoint.advance; + if ( + (!lease.send.checkpoint || lease.send.replacesCheckpoint) && + lease.inFlight !== "status" + ) this.deactivate(hostSessionId); else { this.#authorityReady = true; lease.token = crypto.randomUUID(); - lease.messageId = messageId; - lease.delivery = delivery; - lease.pendingReply = true; + lease.send.messageId = messageId; + lease.send.delivery = delivery; + lease.send.pendingReply = true; if (!lease.inFlight) await this.onIdle(hostSessionId); } return "accepted"; @@ -487,13 +503,32 @@ export class AutoDriveCoordinator { epoch: this.#epoch, }); } + captureDispatch( + host: string, + token: string, + intent?: DispatchIntent, + ): DispatchCapture | null { + const authority = this.captureAuthority(host, token), + lease = this.#lease; + if (!authority || !lease) return null; + const source = lease.send.messageId; + return Object.freeze({ + authority, + source, + current: () => + this.#lease === lease && + this.isAuthorityCurrent(authority) && + lease.send.messageId === source && + (!intent || lease.send.isPrepared(intent)), + }); + } isAuthorityCurrent(authority: AutoDriveAuthority, source?: string): boolean { return ( this.#authorityReady && this.#lease?.hostSessionId === authority.hostSessionId && this.#lease.token === authority.token && this.#epoch === authority.epoch && - (source === undefined || this.#lease.messageId === source) + (source === undefined || this.#lease.send.messageId === source) ); } revokeExecutions(host: string): void { @@ -501,6 +536,7 @@ export class AutoDriveCoordinator { this.#epoch++; this.#authorityReady = false; this.#lease.assistantOrigins.clear(); + this.#lease.send.renew(); } observeAssistant(host: string, assistantId: string, origin: string): void { const lease = this.#lease; @@ -518,9 +554,9 @@ export class AutoDriveCoordinator { ): void { const lease = this.#lease; if (!lease || lease.hostSessionId !== host) return; - if (!this.isAuthorityCurrent(authority) || lease.messageId !== source) + if (!this.isAuthorityCurrent(authority) || lease.send.messageId !== source) return void this.#rejectOrigin(lease, "compaction"); - lease.messageId = controlID; + lease.send.messageId = controlID; this.#options.recovery?.observeMessage(host, controlID, true, true); } observeMutation( @@ -539,12 +575,12 @@ export class AutoDriveCoordinator { if ( !this.#authorityReady || lease?.hostSessionId !== host || - !lease.messageId + !lease.send.messageId ) return; const origin = lease.assistantOrigins.get(assistantId); if (origin === undefined) return void this.#rejectOrigin(lease, "mutation"); - if (origin !== lease.messageId) return; + if (origin !== lease.send.messageId) return; lease.validation = null; const baseline = lease.baseline; const freshIdle = @@ -567,10 +603,14 @@ export class AutoDriveCoordinator { mutation.features.length > 0 && mutation.features.every((feature) => feature.kind === "inspect") ) - lease.ownInspectDraft = { sessionId: created, revision, prompted: false }; + lease.send.ownInspectDraft = { + sessionId: created, + revision, + prompted: false, + }; if (baseline && baseline.sessionId === undefined && created) lease.baseline = { ...baseline, sessionId: created }; - const point = lease.checkpoint; + const point = lease.send.checkpoint; if (!point) return; if (revision > point.revision) point.advance = revision + Number(reviewerPending); @@ -595,7 +635,7 @@ export class AutoDriveCoordinator { if ( !this.#authorityReady || lease?.hostSessionId !== hostSessionId || - !lease.messageId || + !lease.send.messageId || !lease.baseline?.sessionId ) return null; @@ -604,11 +644,11 @@ export class AutoDriveCoordinator { this.#rejectOrigin(lease, "mutation"); return null; } - if (source !== lease.messageId) return null; + if (source !== lease.send.messageId) return null; const origin: AutoValidationOrigin = { hostSessionId, sessionId: lease.baseline.sessionId, - authority: lease.messageId, + authority: lease.send.messageId, assistantId, featureId: prepared.featureId, runId: prepared.runId, @@ -635,12 +675,12 @@ export class AutoDriveCoordinator { owner !== lease || lease.hostSessionId !== origin.hostSessionId || lease.baseline?.sessionId !== origin.sessionId || - lease.messageId !== origin.authority || + lease.send.messageId !== origin.authority || lease.assistantOrigins.get(origin.assistantId) !== origin.authority || !validationReceiptMatches(receipt) ) return; - const checkpoint = lease.checkpoint; + const checkpoint = lease.send.checkpoint; if ( !checkpoint || observation.recordedRevision <= checkpoint.revision || @@ -683,73 +723,69 @@ export class AutoDriveCoordinator { pausedTimeExcluded: true, }; } - async onIdle(hostSessionId: string): Promise { + async onIdle(hostSessionId: string, owner?: IdleOwner): Promise { const lease = this.#lease; if (!lease || lease.hostSessionId !== hostSessionId) return; const epoch = this.#epoch; - if (!this.#authorityReady) return; + if (!this.#authorityReady || (owner && !owner.current())) return; + const capture = this.captureDispatch(hostSessionId, lease.token); + if (!capture) return; if (lease.inFlight) { if ( lease.inFlight !== "reply-status" || - !lease.pendingReply || - lease.checkpoint?.advance !== undefined + !lease.send.pendingReply || + lease.send.checkpoint?.advance !== undefined ) lease.idlePending = true; return; } - lease.inFlight = lease.pendingReply ? "reply-status" : "status"; + lease.inFlight = lease.send.pendingReply ? "reply-status" : "status"; try { - const projection = await this.#read(lease); + const projection = await this.#read(lease, owner); if (!projection) return; - if (this.#lease !== lease || this.#epoch !== epoch) return; + if ( + this.#lease !== lease || + this.#epoch !== epoch || + (owner && !owner.current()) + ) + return; const baseline = lease.baseline; if (!baseline) return this.#stop(lease); - if (lease.manualContinuation) { + if (lease.send.manualContinuation) { if ( projection.sessionId !== baseline.sessionId || - projection.revision < lease.manualContinuation.revision + projection.revision < lease.send.manualContinuation.revision ) return this.#stop(lease, undefined, "unowned"); if ( (projection.status === "ready" || projection.status === "running") && projection.nextAction !== "await-user-direction" ) { - lease.manualContinuation.revision = projection.revision; + lease.send.manualContinuation.revision = projection.revision; this.#setTiming("paused"); this.#disposition = { state: "paused", reason: "manual-command" }; return; } - lease.manualContinuation = null; + lease.send.manualContinuation = null; } - const draft = lease.ownInspectDraft; + const draft = lease.send.ownInspectDraft; if ( draft && !draft.prompted && - !lease.pendingReply && + !lease.send.pendingReply && projection.sessionId === draft.sessionId && projection.revision === draft.revision && projection.status === "planning" && projection.nextAction === "flow_plan_approve" && - lease.delivery + lease.send.delivery ) { - draft.prompted = true; - lease.messageId = null; - lease.inFlight = "prompt"; - try { - await this.#options.prompt( - hostSessionId, - `The accepted draft belongs to this /flow-auto invocation, keeps its exact goal, and contains only inspect features. Approve this one draft, then continue the ordinary Flow lifecycle. Do not edit product files.\n\n${FLOW_MANAGER_KERNEL}`, - lease.delivery, - { [FLOW_AUTO_METADATA_KEY]: lease.token }, - ); - } catch (error) { - if (this.#epoch !== epoch) return; - this.#stop( - lease, - `Flow inspection approval prompt failed: ${String(error)}`, - "prompt-error", - ); - } + await this.#send( + lease, + `The accepted draft belongs to this /flow-auto invocation, keeps its exact goal, and contains only inspect features. Approve this one draft, then continue the ordinary Flow lifecycle. Do not edit product files.\n\n${FLOW_MANAGER_KERNEL}`, + { kind: "draft", draft }, + false, + owner, + ); return; } const validation = lease.validation; @@ -758,18 +794,23 @@ export class AutoDriveCoordinator { !validation.checked && validation.revision === projection.revision ) { - const authority = lease.messageId; + const authority = lease.send.messageId; let currentSource: SourceDigest | undefined; try { - currentSource = await this.#options.readSourceDigest?.(); + const read = this.#options.readSourceDigest?.(); + currentSource = read + ? await Promise.race([read, lease.send.cancelled]) + : undefined; } catch { currentSource = undefined; } if ( this.#lease !== lease || this.#epoch !== epoch || - lease.messageId !== authority || - lease.validation !== validation + lease.send.messageId !== authority || + lease.validation !== validation || + !capture.current() || + (owner !== undefined && !owner.current()) ) return; validation.checked = true; @@ -802,47 +843,45 @@ export class AutoDriveCoordinator { const decision: IdleDecision = decideOnIdle( { baseline, - checkpoint: lease.checkpoint, - pendingReply: lease.pendingReply, - lastPromptedRevision: lease.lastPromptedRevision, - hasDelivery: lease.delivery !== null, + checkpoint: lease.send.checkpoint, + pendingReply: lease.send.pendingReply, + lastPromptedRevision: lease.send.lastPromptedRevision, + hasDelivery: lease.send.delivery !== null, validation: lease.validation, repairHandoffAvailable: repairAvailable, }, projection, ); - if (lease.pendingReply && projection.status !== "idle") - lease.pendingReply = false; + const clearPendingReply = + lease.send.pendingReply && projection.status !== "idle"; + if ( + ![ + "prompt-initial", + "continue", + "handback-and-wait", + "handback-or-deactivate", + ].includes(decision.kind) && + clearPendingReply + ) + lease.send.pendingReply = false; const proposal = (projection.status === "blocked" || projection.status === "ready") && decision.kind === "handback-and-wait" - ? this.#options.recovery?.proposalPrompt( + ? this.#options.recovery?.prepareProposalPrompt( hostSessionId, projection.sessionId, projection.revision, projection.nextAction, ) : null; - if (proposal && lease.delivery) { - const token = lease.token; - this.#waitAt(lease, projection.revision); - lease.handbackPromptedRevision = projection.revision; - this.#setTiming("active"); - lease.inFlight = "prompt"; - await this.#options - .prompt(hostSessionId, proposal, lease.delivery, { - [FLOW_AUTO_METADATA_KEY]: token, - }) - .catch( - (error) => - this.#epoch === epoch && - lease.token === token && - this.#stop( - lease, - `Flow recovery prompt failed: ${String(error)}`, - "prompt-error", - ), - ); + if (proposal && lease.send.delivery) { + await this.#send( + lease, + proposal.text, + { kind: "recovery", revision: projection.revision, proposal }, + clearPendingReply, + owner, + ); return; } switch (decision.kind) { @@ -862,76 +901,46 @@ export class AutoDriveCoordinator { decision.warning, projection.sessionId !== baseline.sessionId ? "unowned" - : !lease.delivery + : !lease.send.delivery ? "no-delivery" : "no-progress", ); - case "prompt-initial": { - const token = lease.token; - lease.lastPromptedRevision = 0; - lease.inFlight = "prompt"; - if (!lease.delivery) return; - await this.#options - .prompt(hostSessionId, AUTO_INITIAL_PROMPT, lease.delivery, { - [FLOW_AUTO_METADATA_KEY]: token, - }) - .catch( - (error) => - this.#epoch === epoch && - lease.token === token && - this.#stop( - lease, - `Flow auto prompt failed: ${String(error)}`, - "prompt-error", - ), + case "prompt-initial": + await this.#send( + lease, + AUTO_INITIAL_PROMPT, + { kind: "initial" }, + clearPendingReply, + owner, + ); + return; + case "handback-and-wait": + case "handback-or-deactivate": { + const needed = + isHandback(projection) && + lease.send.handbackPromptedRevision !== projection.revision && + lease.send.delivery; + if (needed) + await this.#send( + lease, + autoHandbackMessage(projection), + { kind: decision.kind, projection }, + clearPendingReply, + owner, ); + else if (decision.kind === "handback-and-wait") { + if (clearPendingReply) lease.send.pendingReply = false; + this.#waiting(lease, projection); + } else this.deactivate(hostSessionId, "unsupported-action"); return; } - case "handback-and-wait": { - const token = lease.token; - await this.#promptHandback(lease, projection); - if ( - this.#lease !== lease || - lease.token !== token || - this.#epoch !== epoch - ) - return; - this.#disposition = { - state: "waiting", - reason: - projection.nextAction === "flow_plan_approve" - ? "plan-approval" - : "user-direction", - }; - return void this.#waitAt(lease, projection.revision); - } case "answered": - if (lease.checkpoint) lease.checkpoint.answered = true; + if (lease.send.checkpoint) lease.send.checkpoint.answered = true; this.#setTiming("active"); return; - case "handback-or-deactivate": { - const token = lease.token; - const already = - lease.handbackPromptedRevision === projection.revision; - await this.#promptHandback(lease, projection); - if ( - this.#lease !== lease || - lease.token !== token || - this.#epoch !== epoch - ) - return; - if ( - !already && - lease.handbackPromptedRevision === projection.revision - ) { - this.#setTiming("paused"); - this.#disposition = { state: "paused", reason: "handback" }; - return; - } - return void this.deactivate(hostSessionId, "unsupported-action"); - } + case "pause": - if (decision.clearCheckpoint) lease.checkpoint = null; + if (decision.clearCheckpoint) lease.send.checkpoint = null; this.#setTiming("paused"); this.#disposition = { state: "paused", @@ -944,38 +953,26 @@ export class AutoDriveCoordinator { }; return this.#warn(decision.warning); case "continue": { - if (decision.clearCheckpoint) lease.checkpoint = null; - if (!lease.delivery) return; - if (decision.repairHandoff && repairKey) - this.#repairHandoffs.add(repairKey); - lease.lastPromptedRevision = projection.revision; - lease.messageId = null; - this.#setTiming("active"); - this.#disposition = { state: "active", reason: "continuation" }; - lease.inFlight = "prompt"; - try { - const continuation = [ - `Continue the same user-authorized /flow-auto lifecycle from compact revision ${projection.revision}.`, - "Call flow_status with the compact view first.", - CONTINUATION_ROUTE, - decision.repairHandoff - ? "A complete failed required gate permits one bounded same-goal prerequisite handoff. Use flow_plan_amend before repair edits. Preserve test discovery, assertions, and acceptance criteria; never exclude or skip existing tests. Test the supported reversible remedy, then rerun the unchanged command. Do not infer new outcome or external-action authority." - : `Then follow ${projection.nextAction} without expanding the approved goal.`, - ].join(" "); - await this.#options.prompt( - hostSessionId, - `${continuation}\n\n${FLOW_MANAGER_KERNEL}`, - lease.delivery, - { [FLOW_AUTO_METADATA_KEY]: lease.token }, - ); - } catch (error) { - if (this.#epoch !== epoch) return; - this.#stop( - lease, - `Flow auto prompt failed: ${String(error)}`, - "prompt-error", - ); - } + const continuation = [ + `Continue the same user-authorized /flow-auto lifecycle from compact revision ${projection.revision}.`, + "Call flow_status with the compact view first.", + CONTINUATION_ROUTE, + decision.repairHandoff + ? "A complete failed required gate permits one bounded same-goal prerequisite handoff. Use flow_plan_amend before repair edits. Preserve test discovery, assertions, and acceptance criteria; never exclude or skip existing tests. Test the supported reversible remedy, then rerun the unchanged command. Do not infer new outcome or external-action authority." + : `Then follow ${projection.nextAction} without expanding the approved goal.`, + ].join(" "); + await this.#send( + lease, + `${continuation}\n\n${FLOW_MANAGER_KERNEL}`, + { + kind: "continuation", + revision: projection.revision, + clearCheckpoint: decision.clearCheckpoint, + repairKey: decision.repairHandoff ? repairKey : null, + }, + clearPendingReply, + owner, + ); return; } default: { @@ -987,10 +984,11 @@ export class AutoDriveCoordinator { } } finally { if (this.#lease === lease) { - const rerun = lease.idlePending; + const rerun = lease.idlePending && !lease.send.deferred; lease.idlePending = false; lease.inFlight = null; - if (rerun) await this.onIdle(hostSessionId); + if (rerun && this.#epoch === epoch && (!owner || owner.current())) + await this.onIdle(hostSessionId, owner); } } } diff --git a/src/platform/opencode/execution-attribution.ts b/src/platform/opencode/execution-attribution.ts index eac52e01..294b231a 100644 --- a/src/platform/opencode/execution-attribution.ts +++ b/src/platform/opencode/execution-attribution.ts @@ -9,7 +9,9 @@ import type { } from "@opencode/plugin/promise/session"; import type { ToolContext } from "@opencode/plugin/promise/tool"; import type { FlowCodingModel } from "../../config-shared.js"; +import type { CompletedPrefix } from "./auto-compaction.js"; import type { AutoDriveAuthority, AutoDriveDelivery } from "./auto-drive.js"; +import type { InputScope } from "./input-admission.js"; export type NativeExecution = Pick< ToolContext, @@ -28,7 +30,7 @@ export type InputOrigin = Readonly<{ export type Participation = | Readonly<{ kind: "flow"; origin: InputOrigin }> | Readonly<{ kind: "native-only" }>; -type InputClock = Readonly<{ current(): boolean; assertCurrent(): void }>; +type InputClock = Pick; type Snapshot = Readonly<{ clock: InputClock; @@ -64,6 +66,14 @@ type SessionRecord = { assistants: Map; bindings: Map; }; +const replacementBrand = Symbol(); +type Replacement = Readonly<{ + [replacementBrand]: ExecutionAttribution; + expected: PrimaryOwner | undefined; + retry: boolean; + inputs: readonly string[]; + delivery: AutoDriveDelivery; +}>; const retired: Lifetime = Object.freeze({ kind: "retired" }); const historical: AssistantRecord = Object.freeze({ kind: "historical" }); const same = isDeepStrictEqual; @@ -160,7 +170,6 @@ function canonical(path: string): string { export class ExecutionAttribution { readonly #sessions = new Map(); readonly #requests = new WeakMap(); - readonly #retries = new WeakSet(); readonly #assertLive: (origin: InputOrigin) => void; #closed = false; constructor(assertLive: (origin: InputOrigin) => void) { @@ -214,16 +223,87 @@ export class ExecutionAttribution { current(sessionID: string): PrimaryOwner | undefined { return this.#sessions.get(sessionID)?.current; } + assertExpected(host: string, expected: PrimaryOwner | undefined): void { + if (this.#closed) throw new Error("Flow lifecycle is closed."); + this.#session(host); + if (this.current(host) !== expected) + throw new Error("Flow primary ownership was superseded."); + } + prepareReplacement( + input: Pick, + native: readonly SessionMessageInfo[], + expected: PrimaryOwner | undefined, + changingPublicSource: boolean, + bridge?: CompletedPrefix, + ): Replacement { + this.assertExpected(input.sessionID, expected); + const inputs = modelInputIDs(input), + delivery = nativeDelivery(input); + + const previous = + expected?.lifetime.kind === "current" + ? expected.lifetime.snapshot + : undefined; + if ( + changingPublicSource && + previous?.clock.current() && + !same(inputs.slice(0, previous.inputs.length), previous.inputs) && + !( + bridge && + expected?.participation.kind === "flow" && + bridge.sourceOrigin === expected.participation.origin && + same(bridge.consumed, previous.inputs) && + inputs[0] === bridge.checkpoint + ) + ) + throw new Error( + "Flow new public input does not extend the frozen consumed prefix.", + ); + const retry = + !!expected && + this.isCurrent(expected) && + !changingPublicSource && + this.#retryEligible(expected, native); + if ( + expected && + this.isCurrent(expected) && + expected.http.phase !== "ended" && + !retry + ) + throw new Error("Flow primary contexts or streams overlap."); + if (!same(inputs, nativeInputIDs(native))) + throw new Error( + "Flow native inputs differ from the model-visible prefix.", + ); + return Object.freeze({ + [replacementBrand]: this, + expected, + retry, + inputs: Object.freeze(inputs), + delivery, + }); + } freeze( input: SessionContext, native: readonly SessionMessageInfo[], participation: Participation, clock: InputClock, + replacement?: Replacement, ): PrimaryOwner { if (this.#closed) throw new Error("Flow lifecycle is closed."); const record = this.#session(input.sessionID); const prior = record?.current; - if (prior && this.isCurrent(prior) && prior.http.phase !== "ended") + if (replacement) { + this.assertExpected(input.sessionID, replacement.expected); + if (replacement[replacementBrand] !== this) + throw new Error("Flow replacement permit belongs to another owner."); + } + if ( + !replacement && + prior && + this.isCurrent(prior) && + prior.http.phase !== "ended" + ) throw new Error("Flow primary contexts or streams overlap."); clock.assertCurrent(); if (participation.kind === "flow") this.#assertLive(participation.origin); @@ -240,11 +320,17 @@ export class ExecutionAttribution { throw new Error( "Flow admitted agent or model differs from its primary request.", ); + if ( + replacement && + (!same(inputs, replacement.inputs) || + !sameDelivery(delivery, replacement.delivery)) + ) + throw new Error("Flow replacement candidate changed after preparation."); const previous = prior?.lifetime.kind === "current" ? prior.lifetime.snapshot : undefined; const retry = prior && - this.#retries.delete(prior) && + replacement?.retry && previous?.clock.current() && prior.participation.kind === participation.kind && (participation.kind === "native-only" || @@ -253,6 +339,13 @@ export class ExecutionAttribution { same(previous?.inputs, inputs) && prior.agent === input.agent && sameModel(prior.model, input.model); + if ( + prior && + this.isCurrent(prior) && + prior.http.phase !== "ended" && + !retry + ) + throw new Error("Flow primary contexts or streams overlap."); const snapshot: Snapshot = Object.freeze({ clock: Object.freeze({ current: clock.current, @@ -334,7 +427,10 @@ export class ExecutionAttribution { same(nativeInputIDs(native.slice(0, position)), snapshot.inputs), ); } - retry(primary: PrimaryOwner, native: readonly SessionMessageInfo[]): boolean { + #retryEligible( + primary: PrimaryOwner, + native: readonly SessionMessageInfo[], + ): boolean { this.assertCurrent(primary); const snapshot = this.#evidence(primary); const record = this.#session(primary.sessionID); @@ -358,10 +454,7 @@ export class ExecutionAttribution { !record?.assistants.has(message.id) && same(nativeInputIDs(native.slice(0, position)), snapshot.inputs), ); - if (candidates.length !== 1) return false; - this.#retries.add(primary); - this.end(primary); - return true; + return candidates.length === 1; } end(primary: PrimaryOwner): boolean { const active = primary.http.phase !== "ended"; diff --git a/src/platform/opencode/input-admission.ts b/src/platform/opencode/input-admission.ts index 36ce71c2..09ebdd66 100644 --- a/src/platform/opencode/input-admission.ts +++ b/src/platform/opencode/input-admission.ts @@ -1,7 +1,9 @@ import { isDeepStrictEqual as same } from "node:util"; import type { + OpenCodeEvent, SessionInboxSynthetic, SessionInboxUser, + SessionInfo, SessionMessageInfo, SessionPromptInput, SessionSyntheticInput, @@ -12,7 +14,14 @@ import type { } from "@opencode/plugin/promise/session"; import type { AutoDriveAuthority, AutoDriveDelivery } from "./auto-drive.js"; import { nativePromptPayload } from "./command-hook.js"; -import { type InputOrigin, modelInputIDs } from "./execution-attribution.js"; +import { + type InputOrigin, + modelInputIDs, + nativeDelivery, + nativeInputIDs, + type Participation, + sameDelivery, +} from "./execution-attribution.js"; type Payload = | Omit @@ -36,13 +45,50 @@ type Admission = { state: "pending" | "accepted" | "rejected"; origin: InputOrigin | null; }; +type InboxEvent = Extract< + OpenCodeEvent, + { + type: + | "session.inbox.enqueued" + | "session.inbox.cancelled" + | "session.inbox.delivered" + | "session.inbox.delivery.changed"; + } +>; +type InboxEnqueued = Extract; +type EnqueuedUserPayload = Extract< + InboxEnqueued["data"]["item"], + { type: "user" } +>["payload"]; +type Observation = { + seq: number; + generation: number | null; + item: InboxEnqueued["data"]["item"] | null; + state: "pending" | "delivered" | "cancelled" | "unseen" | "consumed"; +}; +type RealState = + | Readonly<{ kind: "deferred" }> + | Readonly<{ kind: "immediate"; reservation: InputReservation }> + | Readonly<{ + kind: "activating"; + token: symbol; + reservation: InputReservation | null; + }> + | Readonly<{ + kind: "settled"; + reservation: InputReservation | null; + origin: InputOrigin | null; + automaticHistory: boolean; + }> + | Readonly<{ kind: "failed" }>; type Real = { kind: "real"; - reservation: InputReservation; + registration: symbol; prompt: SessionPrompt["prompt"]; metadata: SessionPrompt["metadata"]; - origin: InputOrigin | null; - resolving: Promise | null; + state: RealState; + observation: Observation | null; + reuse: { cancelled: number; hook: boolean } | null; }; type Alias = Readonly<{ kind: "alias"; @@ -52,6 +98,7 @@ type Alias = Readonly<{ type Entry = | Admission | Real + | { kind: "observed"; observation: Observation } | Alias | Readonly<{ kind: "reserved"; reservation: InputReservation }>; type History = @@ -73,6 +120,28 @@ export type InputScope = Readonly<{ ready(): Promise; release(): void; }>; +export type ResolvedSource = Readonly<{ + scope: InputScope; + evidence: SourceEvidence; + participation: Participation | null; + native: readonly SessionMessageInfo[]; + selected: SessionInfo; +}>; +export type SourceOperation = Readonly<{ + token: symbol; + current(): boolean; + scope: InputScope; + resolve( + native: readonly SessionMessageInfo[], + selected: SessionInfo, + effects: Readonly<{ + check(changing: boolean, evidence: SourceEvidence | null): void; + supersede(): void; + }>, + ): Promise; + fail(stillExpected: () => boolean): boolean; + release(): void; +}>; type ScopeState = Readonly<{ entry: Entry | undefined; frontierEntry: Entry | undefined; @@ -96,6 +165,8 @@ export class InputAdmissions { readonly #frontier = new Map(); readonly #epochs = new Map(); readonly #hosts = new WeakMap(); + readonly #operations = new Map(); + readonly #deleted = new Set(); readonly #scopes = new Map(); #closed = false; readonly #options: InputAdmissionOptions; @@ -119,7 +190,7 @@ export class InputAdmissions { reserve(host: string, id: string): InputReservation { this.#options.assertOpen(); const key = this.#key(host, id); - if (this.#closed || this.#entries.has(key)) + if (this.#closed || this.#deleted.has(host) || this.#entries.has(key)) throw new Error( "Flow admission identity is unavailable or already registered.", ); @@ -209,43 +280,189 @@ export class InputAdmissions { const entry = this.#entries.get(this.#key(host, id)); return entry?.kind === "private" || entry?.kind === "reserved"; } - observePrompt(input: SessionPrompt, beforeReserve: () => void): void { - const prior = this.#entries.get( - this.#key(input.sessionID, input.messageID), - ); + observePrompt(input: SessionPrompt, immediate: () => void): void { + this.#options.assertOpen(); + if (this.#deleted.has(input.sessionID)) throw revoked(); + const key = this.#key(input.sessionID, input.messageID), + prior = this.#entries.get(key); if (prior?.kind === "real") { if ( !same(prior.prompt, input.prompt) || !same(prior.metadata, input.metadata) ) throw new Error("Flow duplicate prompt changed its payload."); + if (prior.reuse && !prior.reuse.hook && prior.state.kind !== "settled") { + prior.reuse.hook = true; + prior.registration = Symbol(); + } return; } - beforeReserve(); - const reservation = this.reserve(input.sessionID, input.messageID); - this.#entries.set(this.#key(input.sessionID, input.messageID), { + if (prior && prior.kind !== "observed") + throw new Error( + "Flow admission identity is unavailable or already registered.", + ); + const real: Real = { kind: "real", - reservation, + registration: Symbol(), prompt: structuredClone(input.prompt), metadata: structuredClone(input.metadata), - origin: null, - resolving: null, - }); + state: { kind: "deferred" }, + observation: prior?.observation ?? null, + reuse: + prior?.observation.state === "cancelled" + ? { cancelled: prior.observation.seq, hook: true } + : null, + }; + if (input.delivery === "steer" && !real.reuse) { + immediate(); + if (prior) this.#entries.delete(key); + real.state = { + kind: "immediate", + reservation: this.reserve(input.sessionID, input.messageID), + }; + } + this.#entries.set(key, real); + } + observeInbox( + event: InboxEvent, + immediate: (host: string) => void, + ): Readonly<{ retryHost: string | null }> { + const { sessionID: host, inboxID: id } = event.data, + key = this.#key(host, id); + if ( + this.#deleted.has(host) || + event.durable.aggregateID !== host || + event.durable.version !== 1 + ) + return { retryHost: null }; + let entry = this.#entries.get(key); + if (entry && entry.kind !== "real" && entry.kind !== "observed") + return { retryHost: null }; + if ( + entry?.kind === "real" && + (entry.state.kind === "settled" || + entry.state.kind === "failed" || + entry.state.kind === "activating") + ) + return { retryHost: null }; + if (entry?.kind === "observed" && entry.observation.state === "consumed") + return { retryHost: null }; + const prior = entry?.observation; + if (prior && event.durable.seq <= prior.seq) return { retryHost: null }; + const observation: Observation = prior ?? { + seq: 0, + generation: null, + item: null, + state: "unseen", + }; + observation.seq = event.durable.seq; + if (!entry) { + entry = { kind: "observed", observation }; + this.#entries.set(key, entry); + } else entry.observation = observation; + if (event.type === "session.inbox.enqueued") { + observation.generation = event.durable.seq; + observation.item = structuredClone(event.data.item); + observation.state = "pending"; + } else if ( + observation.generation !== null && + observation.state !== "cancelled" + ) { + if (event.type === "session.inbox.delivered") + observation.state = "delivered"; + if ( + event.type === "session.inbox.cancelled" && + observation.state === "pending" + ) { + observation.state = "cancelled"; + if (entry.kind === "real") + entry.reuse = { cancelled: event.durable.seq, hook: false }; + return { retryHost: host }; + } + if (event.type === "session.inbox.delivery.changed" && observation.item) { + observation.item = { + ...observation.item, + delivery: event.data.delivery, + }; + if ( + event.data.delivery === "steer" && + entry.kind === "real" && + entry.state.kind === "deferred" && + observation.item.type === "user" && + this.#matchesPayload(entry, observation.item.payload) && + (!entry.reuse || + (entry.reuse.hook && + observation.generation > entry.reuse.cancelled)) + ) { + immediate(host); + const reservation = Object.freeze({ + host, + id, + epoch: this.epoch(host), + }); + this.#frontier.set(host, reservation); + entry.state = { kind: "immediate", reservation }; + this.#cancel(host); + } + } + } + return { retryHost: null }; + } + automaticBlock(host: string): "pending-user" | "input-settlement" | null { + if ([...this.#operations.values()].includes(host)) + return "input-settlement"; + for (const [key, entry] of this.#entries) { + if ( + JSON.parse(key)[0] !== host || + (entry.kind !== "real" && entry.kind !== "observed") + ) + continue; + if ( + entry.kind === "real" && + (entry.state.kind === "settled" || entry.state.kind === "failed") + ) + continue; + const observation = entry.observation; + if ( + observation?.item?.type === "user" && + (observation.state === "pending" || observation.state === "delivered") + ) + return "pending-user"; + } + return null; + } + captureBarrier(host: string): Pick { + const epoch = this.epoch(host), + frontier = this.#frontier.get(host); + const current = () => + !this.#closed && + !this.#deleted.has(host) && + this.epoch(host) === epoch && + this.#frontier.get(host) === frontier; + const assertCurrent = () => { + this.#options.assertOpen(); + if (!current()) throw revoked(); + }; + return Object.freeze({ current, assertCurrent }); } #covers(entry: Entry | undefined, frontier: InputReservation): boolean { return entry?.kind === "alias" ? this.#covers(entry.source, frontier) - : entry?.reservation === frontier; + : entry?.kind === "real" + ? (entry.state.kind === "immediate" || + entry.state.kind === "activating" || + entry.state.kind === "settled") && + entry.state.reservation === frontier + : entry?.kind !== "observed" && entry?.reservation === frontier; } - capture(input: Pick): InputScope { + #capture(input: Pick): InputScope { this.#options.assertOpen(); const host = input.sessionID, consumed = Object.freeze(modelInputIDs(input)), source = consumed.at(-1); if (!source) throw new Error("Flow primary context has no attributable input."); - const epoch = this.epoch(host), - frontier = this.#frontier.get(host); + const frontier = this.#frontier.get(host); if ( frontier && !consumed.some((id) => @@ -256,14 +473,7 @@ export class InputAdmissions { const entry = this.#entries.get(this.#key(host, source)); const cancelled = Promise.withResolvers(); void cancelled.promise.catch(() => {}); - const current = () => - !this.#closed && - this.epoch(host) === epoch && - this.#frontier.get(host) === frontier; - const assertCurrent = () => { - this.#options.assertOpen(); - if (!current()) throw revoked(); - }; + const { current, assertCurrent } = this.captureBarrier(host); const scope: InputScope = { host, consumed, @@ -277,7 +487,8 @@ export class InputAdmissions { return result; }, ready: async () => { - if (entry?.kind === "private") await scope.wait(entry.ready); + if (entry?.kind === "private" && entry.state !== "rejected") + await scope.wait(entry.ready); assertCurrent(); }, release: () => { @@ -293,101 +504,339 @@ export class InputAdmissions { }); return scope; } - positive(scope: InputScope): InputOrigin | undefined { - if (!scope.current()) return; - const entry = this.#scopes.get(scope)?.entry; - const origin = entry && entry.kind !== "reserved" ? entry.origin : null; - return origin && this.isLive(origin) ? origin : undefined; + #matchesPayload( + entry: Real, + payload: SessionInboxUser["payload"] | EnqueuedUserPayload, + ): boolean { + return ( + same(entry.metadata ?? {}, payload.metadata ?? {}) && + matchesAdmissionPayload(nativePromptPayload(entry.prompt), payload, false) + ); } - async evidence( - scope: InputScope, - native: readonly SessionMessageInfo[], - delivery: AutoDriveDelivery, - ): Promise { - scope.assertCurrent(); - await scope.ready(); - const entry = this.#scopes.get(scope)?.entry; - if (!entry) - return { - kind: "unregistered", - lineage: this.#history(this.#scopes.get(scope)?.frontierEntry).kind, - }; - if (entry.kind === "reserved") return { kind: "rejected" }; - if (entry.kind === "alias") { - if (entry.origin) return this.#classify(entry.origin); - return this.#history(entry.source); - } - const message = native.find((message) => message.id === scope.source); - if (!message) - throw new Error("Flow consumed input is missing from native context."); - if (entry.kind === "private") { - if (entry.state === "rejected") return { kind: "rejected" }; - if ( - message.type !== entry.type || - (message.type !== "user" && message.type !== "synthetic") - ) - throw new Error( - "Flow native input type conflicts with its admitted dispatch.", - ); - const { id: _id, time: _time, type: _type, ...payload } = message; - if (!same(payload, entry.acceptedPayload)) - throw new Error( - "Flow native input payload conflicts with its admitted dispatch.", + #matchesReal(entry: Real, message: SessionMessageInfo): boolean { + return message.type === "user" && this.#matchesPayload(entry, message); + } + openSource( + input: Pick, + ): SourceOperation { + input = { + sessionID: input.sessionID, + agent: input.agent, + model: { ...input.model }, + messages: structuredClone(input.messages), + }; + const scope = this.#capture(input), + token = Symbol(), + host = scope.host; + const captured = scope.consumed.map((id) => ({ + id, + entry: this.#entries.get(this.#key(host, id)), + })); + const fresh = captured.filter( + (item): item is { id: string; entry: Real } => + item.entry?.kind === "real" && + (item.entry.state.kind === "deferred" || + item.entry.state.kind === "immediate"), + ); + const registrations = fresh.map(({ entry }) => entry.registration); + if (fresh.length) this.#operations.set(token, host); + const final = captured.at(-1)?.entry; + let successor: InputScope | undefined, + owned = false, + resolved = false; + const failClaimed = () => { + for (const { entry } of fresh) + if (entry.state.kind === "activating" && entry.state.token === token) + entry.state = { kind: "failed" }; + }; + + const release = () => { + this.#operations.delete(token); + scope.release(); + successor?.release(); + }; + return { + token, + current: () => (successor ?? scope).current(), + scope, + release, + fail: (stillExpected) => { + const active = successor ?? scope; + const origin = + final?.kind === "private" || final?.kind === "alias" + ? final.origin + : final?.kind === "real" && final.state.kind === "settled" + ? final.state.origin + : null; + if (!fresh.length) owned ||= !!origin && this.isLive(origin); + if (!owned || !active.current() || !stillExpected()) return false; + failClaimed(); + this.invalidate(host); + return true; + }, + resolve: async (rawNative, rawSelected, effects) => { + if (resolved) + throw new Error("Flow source operation was already resolved."); + resolved = true; + scope.assertCurrent(); + const native = structuredClone(rawNative), + selected = structuredClone(rawSelected), + delivery = nativeDelivery(input); + if (!same(scope.consumed, nativeInputIDs(native))) + throw new Error( + "Flow native inputs differ from the model-visible prefix.", + ); + if ( + selected.id !== host || + !sameDelivery(nativeDelivery(selected), delivery) + ) + throw new Error( + final?.kind === "real" || + final?.kind === "private" || + final?.kind === "alias" + ? "Flow admitted agent or model differs from its primary request." + : "Flow native selected session agent or model changed.", + ); + for (const { id, entry } of captured) + if ( + entry?.kind === "private" && + id === scope.source && + entry.state === "pending" + ) + await scope.wait(entry.ready); + for (const { id, entry } of captured) { + if (entry?.kind === "real") { + const message = native.find((message) => message.id === id); + if (!message || !this.#matchesReal(entry, message)) + throw new Error( + "Flow context has an unknown, synthetic, or stale real input.", + ); + if ( + entry.state.kind === "activating" || + (entry.state.kind === "failed" && id === scope.source) + ) + throw revoked(); + } + if (entry?.kind === "reserved") throw revoked(); + if (entry?.kind === "private") { + if (entry.state === "accepted" || id === scope.source) + this.#privateEvidence(entry, id, native); + else { + const message = native.find((message) => message.id === id); + if ( + !message || + message.type !== entry.type || + (message.type !== "user" && message.type !== "synthetic") || + !matchesAdmissionPayload(entry.payload, message) + ) + throw new Error( + "Flow native input payload conflicts with its admitted dispatch.", + ); + } + } + } + for (const [i, item] of fresh.entries()) { + if ( + this.#entries.get(this.#key(host, item.id)) !== item.entry || + registrations[i] !== item.entry.registration + ) + throw revoked(); + const reuse = item.entry.reuse, + observation = item.entry.observation; + if ( + reuse && + (!reuse.hook || + !observation?.generation || + observation.generation <= reuse.cancelled || + observation.state === "cancelled" || + observation.item?.type !== "user" || + !this.#matchesPayload(item.entry, observation.item.payload)) + ) + throw new Error( + "Flow cancelled input reuse has no matching new hook and enqueue.", + ); + } + const changing = fresh.at(-1)?.entry.state.kind === "deferred"; + const firstNew = captured.findIndex((item) => + fresh.some((f) => f.entry === item.entry), ); - if (!entry.origin) - throw new Error("Flow admitted input has no settled origin."); - return this.#classify(entry.origin); - } + if ( + fresh.length > 0 && + captured + .slice(firstNew) + .some( + (item) => + item.entry?.kind === "private" || + item.entry?.kind === "reserved", + ) + ) + throw new Error( + "Flow mixed input batch cannot rebase a private admission.", + ); + const finalEntry = captured.at(-1)?.entry; + const finalFresh = fresh.some((item) => item.id === scope.source); + if ( + finalFresh && + finalEntry?.kind === "real" && + finalEntry.state.kind === "immediate" && + (finalEntry.state.reservation.epoch !== this.epoch(host) || + this.#frontier.get(host) !== finalEntry.state.reservation) + ) + throw revoked(); + const preparedEvidence = finalFresh + ? null + : this.#evidence(scope, native); + if ( + preparedEvidence?.kind === "live" && + !sameDelivery(preparedEvidence.origin.delivery, delivery) + ) + throw new Error( + "Flow admitted agent or model differs from its primary request.", + ); + effects.check(changing, preparedEvidence); + scope.assertCurrent(); + if (changing) { + this.invalidate(host); + const last = fresh.at(-1); + if (!last) throw revoked(); + const reservation = Object.freeze({ + host, + id: last.id, + epoch: this.epoch(host), + }); + this.#frontier.set(host, reservation); + last.entry.state = { kind: "immediate", reservation }; + effects.supersede(); + } + successor = this.#capture(input); + owned ||= fresh.length > 0; + const frontier = this.#frontier.get(host); + for (const { id, entry } of fresh) + entry.state = { + kind: "activating", + token, + reservation: frontier?.id === id ? frontier : null, + }; + for (const { id, entry } of fresh) { + try { + successor.assertCurrent(); + const message = native.find((message) => message.id === id); + if (message?.type !== "user") throw revoked(); + await successor.wait( + this.#options.observeReal( + host, + id, + message.text, + delivery, + successor.assertCurrent, + ), + ); + successor.assertCurrent(); + const authority = this.#options.authority(host); + const origin = + id === scope.source + ? this.makeOrigin(host, id, delivery, false, this.epoch(host)) + : null; + entry.state = { + kind: "settled", + reservation: frontier?.id === id ? frontier : null, + origin, + automaticHistory: authority !== null, + }; + } catch (error) { + failClaimed(); + throw error; + } + } + successor.assertCurrent(); + for (const { entry } of captured) + if (entry?.kind === "observed") entry.observation.state = "consumed"; + const evidence = this.#evidence(successor, native); + if ( + evidence.kind === "live" && + !sameDelivery(evidence.origin.delivery, delivery) + ) + throw new Error( + "Flow admitted agent or model differs from its primary request.", + ); + const source = native.find((message) => message.id === scope.source); + const participation: Participation | null = + evidence.kind === "live" + ? { kind: "flow", origin: evidence.origin } + : (source?.type === "user" || source?.type === "synthetic") && + (evidence.kind === "retired-ordinary" || + (evidence.kind === "unregistered" && + evidence.lineage !== "rejected")) + ? { kind: "native-only" } + : null; + owned ||= evidence.kind === "live"; + return { scope: successor, evidence, participation, native, selected }; + }, + }; + } + #privateEvidence( + entry: Admission, + id: string, + native: readonly SessionMessageInfo[], + ): SourceEvidence { + if (entry.state === "rejected") return { kind: "rejected" }; + const message = native.find((message) => message.id === id); if ( - message.type !== "user" || - !same(entry.metadata ?? {}, message.metadata ?? {}) || - !matchesAdmissionPayload( - { - ...nativePromptPayload(entry.prompt), - ...(message.metadata === undefined - ? {} - : { metadata: message.metadata }), - }, - message, - ) + !message || + message.type !== entry.type || + (message.type !== "user" && message.type !== "synthetic") ) throw new Error( - "Flow context has an unknown, synthetic, or stale real input.", - ); - if (entry.origin) return this.#classify(entry.origin); - if (entry.reservation.epoch !== this.epoch(scope.host)) - return { kind: "retired-ordinary" }; - entry.resolving ??= (async () => { - await this.#options.observeReal( - scope.host, - scope.source, - message.text, - delivery, - scope.assertCurrent, + "Flow native input type conflicts with its admitted dispatch.", ); - scope.assertCurrent(); - const origin = this.makeOrigin( - scope.host, - scope.source, - delivery, - false, - entry.reservation.epoch, + const { id: _id, time: _time, type: _type, ...payload } = message; + if (!same(payload, entry.acceptedPayload)) + throw new Error( + "Flow native input payload conflicts with its admitted dispatch.", ); - entry.origin = origin; - return origin; - })(); - return this.#classify(await scope.wait(entry.resolving)); + if (!entry.origin) + throw new Error("Flow admitted input has no settled origin."); + return this.#classify(entry.origin); + } + #evidence( + scope: InputScope, + native: readonly SessionMessageInfo[], + ): SourceEvidence { + const state = this.#scopes.get(scope), + entry = state?.entry; + if (!entry || entry.kind === "observed") + return { + kind: "unregistered", + lineage: this.#history(state?.frontierEntry).kind, + }; + if (entry.kind === "reserved") return { kind: "rejected" }; + if (entry.kind === "alias") + return entry.origin + ? this.#classify(entry.origin) + : this.#history(entry.source); + if (entry.kind === "private") + return this.#privateEvidence(entry, scope.source, native); + return entry.state.kind === "settled" && entry.state.origin + ? this.#classify(entry.state.origin) + : this.#history(entry); } #history(entry: Entry | undefined): Readonly<{ kind: History }> { - if (!entry) return { kind: "unknown-history" }; + if (!entry || entry.kind === "observed") return { kind: "unknown-history" }; if (entry.kind === "alias") return this.#history(entry.source); if ( entry.kind === "reserved" || + (entry.kind === "real" && entry.state.kind === "failed") || (entry.kind === "private" && entry.state !== "accepted") ) return { kind: "rejected" }; return { - kind: entry.origin?.authority ? "retired-automatic" : "retired-ordinary", + kind: ( + entry.kind === "real" + ? entry.state.kind === "settled" && entry.state.automaticHistory + : entry.origin?.authority + ) + ? "retired-automatic" + : "retired-ordinary", }; } #classify(origin: InputOrigin): SourceEvidence { @@ -423,13 +872,33 @@ export class InputAdmissions { this.assertLive(origin); return origin; } - checkpoint(scope: InputScope, id: string, origin: InputOrigin | null): void { + assertCheckpointAvailable(host: string, id: string): () => boolean { + const key = this.#key(host, id), + entry = this.#entries.get(key); + if (!entry) return () => true; + if ( + entry.kind !== "observed" || + (entry.observation.item && entry.observation.item.type !== "compaction") + ) + throw new Error("Flow checkpoint identity conflicts with an input."); + const generation = entry.observation.generation; + return () => + this.#entries.get(key) === entry && + entry.observation.generation === generation; + } + checkpoint( + scope: InputScope, + id: string, + origin: InputOrigin | null, + matchesTransfer?: () => boolean, + ): void { scope.assertCurrent(); const state = this.#scopes.get(scope); if (!state) throw new Error("Flow checkpoint has no captured admission scope."); const key = this.#key(scope.host, id); - if (this.#entries.has(key)) + this.assertCheckpointAvailable(scope.host, id); + if (matchesTransfer && !matchesTransfer()) throw new Error("Flow checkpoint identity conflicts with an input."); if (origin) this.#hosts.set(origin, scope.host); this.#entries.set(key, { @@ -456,21 +925,37 @@ export class InputAdmissions { this.#options.assertOpen(); if (!this.isLive(origin)) throw revoked(); } + deleteHost(host: string): void { + this.#deleted.add(host); + this.#cancel(host); + this.#frontier.delete(host); + for (const key of this.#entries.keys()) + if (JSON.parse(key)[0] === host) this.#entries.delete(key); + for (const [scope] of this.#scopes) + if (scope.host === host) this.#scopes.delete(scope); + for (const [token, operationHost] of this.#operations) + if (operationHost === host) this.#operations.delete(token); + } clear(): void { this.#closed = true; for (const state of this.#scopes.values()) state.cancel(revoked()); this.#scopes.clear(); this.#entries.clear(); this.#frontier.clear(); + this.#operations.clear(); } } function matchesAdmissionPayload( expected: Admission["payload"], - actual: SessionInboxUser["payload"] | SessionInboxSynthetic["payload"], + actual: + | SessionInboxUser["payload"] + | SessionInboxSynthetic["payload"] + | EnqueuedUserPayload, + checkMetadata = true, ): boolean { if ( expected.text !== actual.text || - !same(expected.metadata ?? {}, actual.metadata ?? {}) + (checkMetadata && !same(expected.metadata ?? {}, actual.metadata ?? {})) ) return false; if ("description" in expected || "description" in actual) diff --git a/src/platform/opencode/lifecycle.ts b/src/platform/opencode/lifecycle.ts index b9b2039b..dfdb4ccd 100644 --- a/src/platform/opencode/lifecycle.ts +++ b/src/platform/opencode/lifecycle.ts @@ -2,7 +2,6 @@ import { isDeepStrictEqual } from "node:util"; import type { SessionMessageInfo } from "@opencode/client"; import type { Context } from "@opencode/plugin/promise/plugin"; import type { Registration } from "@opencode/plugin/promise/registration"; -import type { SessionContext } from "@opencode/plugin/promise/session"; import type { ToolContext } from "@opencode/plugin/promise/tool"; import { SessionMessage } from "@opencode/schema/session-message"; import type { FlowService } from "../../application/flow-service.js"; @@ -20,6 +19,11 @@ import { type AutoDriveDelivery, FLOW_AUTO_METADATA_KEY, } from "./auto-drive.js"; +import type { + DispatchIntent, + DispatchResult, + IdleOwner, +} from "./auto-drive-dispatch.js"; import { createCommandHook, flowCommandName, @@ -32,11 +36,10 @@ import { type NativeExecution, nativeDelivery, nativeInputIDs, - type Participation, rememberCodingModel, sameDelivery, } from "./execution-attribution.js"; -import { InputAdmissions, type InputScope } from "./input-admission.js"; +import { InputAdmissions, type SourceOperation } from "./input-admission.js"; import type { FlowLeadershipHandle } from "./leadership.js"; type LifecycleContext = Readonly<{ @@ -79,7 +82,8 @@ type FlowLifecycle = Readonly<{ prompt: string, delivery: AutoDriveDelivery, metadata: Readonly>, - ): Promise; + intent?: DispatchIntent, + ): Promise; dispose(): Promise; }>; type CallbackOwner = Readonly<{ @@ -101,8 +105,10 @@ export async function registerFlowLifecycle( const registrations: Registration[] = []; const idle = new Map(); const outcomes = new Set(); - const contextPending = new Map(); + const contextPending = new Map(); const controller = new AbortController(); + const readController = new AbortController(); + const readOptions = { signal: readController.signal }; let closed = false; let streamLost = false; let disposal: Promise | null = null; @@ -123,6 +129,10 @@ export async function registerFlowLifecycle( }); const assertLive = (origin: InputOrigin) => inputs.assertLive(origin); const attribution = new ExecutionAttribution(assertLive); + const revokeEffects = (host: string) => { + autoDrive.revokeExecutions(host); + recovery.invalidateOrigins(host); + }; const revoke = ( host: string, deactivate = true, @@ -130,8 +140,7 @@ export async function registerFlowLifecycle( ) => { if (captured) attribution.end(captured); inputs.invalidate(host); - autoDrive.revokeExecutions(host); - recovery.invalidateOrigins(host); + revokeEffects(host); if (deactivate) { autoDrive.deactivate(host); recovery.revoke(host); @@ -213,12 +222,19 @@ export async function registerFlowLifecycle( metadata: Readonly> | null, synthetic: boolean, assertCurrent: () => void, + intent?: DispatchIntent, ) => { assertCurrent(); const parts = metadata ? [{ type: "text", text, synthetic: true, metadata }] : [{ type: "text", text }]; - const result = await autoDrive.observeMessage(host, delivery, parts, id); + const result = await autoDrive.observeMessage( + host, + delivery, + parts, + id, + intent, + ); assertCurrent(); if (result === "stale-continuation") throw new Error("Flow rejected a stale admitted continuation."); @@ -251,8 +267,8 @@ export async function registerFlowLifecycle( assertOwned(owner); attribution.assertCurrent(primary); const [native, session] = await Promise.all([ - ctx.session.context({ sessionID: execution.sessionID }), - ctx.session.get({ sessionID: execution.sessionID }), + ctx.session.context({ sessionID: execution.sessionID }, readOptions), + ctx.session.get({ sessionID: execution.sessionID }, readOptions), ]); assertOwned(owner); attribution.verify( @@ -299,17 +315,36 @@ export async function registerFlowLifecycle( text: string, delivery: AutoDriveDelivery, metadata: Readonly>, - ) => { + intent?: DispatchIntent, + ): Promise => { assertOpen(); const token = metadata[FLOW_AUTO_METADATA_KEY]; - if (typeof token !== "string" || !autoDrive.captureAuthority(host, token)) + if (typeof token !== "string") + throw new Error( + "Flow synthetic dispatch has no live authenticated token.", + ); + const capture = autoDrive.captureDispatch(host, token, intent); + if (!capture) throw new Error( "Flow synthetic dispatch has no live authenticated token.", ); if (Object.keys(metadata).some((key) => key !== FLOW_AUTO_METADATA_KEY)) throw new Error("Flow synthetic metadata has unknown fields."); - const id = SessionMessage.ID.create(); - const reservation = inputs.reserve(host, id); + const barrier = inputs.captureBarrier(host); + const block = inputs.automaticBlock(host); + if (block) return { kind: "withheld", reason: block }; + const session = await ctx.session.get({ sessionID: host }, readOptions); + barrier.assertCurrent(); + if (!capture.current()) + throw new Error("Flow synthetic dispatch was superseded."); + if (session.id !== host || !sameDelivery(delivery, nativeDelivery(session))) + throw new Error( + "Flow queued continuation cannot change the user's selected agent or model.", + ); + const reason = inputs.automaticBlock(host); + if (reason) return { kind: "withheld", reason }; + const id = SessionMessage.ID.create(), + reservation = inputs.reserve(host, id); const expectedEpoch = reservation.epoch; const owner = captureOwner( host, @@ -317,39 +352,22 @@ export async function registerFlowLifecycle( attribution.current(host), expectedEpoch, ); - const authority = autoDrive.captureAuthority(host, token); - if (!authority) throw new Error("Flow synthetic authority expired."); const assertCurrent = () => { inputs.assertReservation(reservation); - if ( - epoch(host) !== expectedEpoch || - !autoDrive.isAuthorityCurrent(authority) - ) + if (!autoDrive.isAuthorityCurrent(capture.authority)) throw new Error("Flow synthetic dispatch was superseded."); }; - const selected = await ctx.session - .get({ sessionID: host }) - .then(nativeDelivery) - .catch((error: unknown) => { - failReservation(reservation, owner); - throw error; - }); - assertCurrent(); - if (!sameDelivery(delivery, selected)) - throw new Error( - "Flow queued continuation cannot change the user's selected agent or model.", - ); const payload = { text, metadata: { [FLOW_AUTO_METADATA_KEY]: token } }; const admission = inputs.register(reservation, { type: "synthetic", payload, }); try { - assertCurrent(); - const receipt = await ctx.session.synthetic( + const pending = ctx.session.synthetic( { sessionID: host, id, ...payload, delivery: "queue", resume: true }, { signal: controller.signal }, ); + const receipt = await pending; await inputs.accept(admission, receipt, async () => { assertCurrent(); await observeAccepted( @@ -360,10 +378,12 @@ export async function registerFlowLifecycle( payload.metadata, true, assertCurrent, + intent, ); assertCurrent(); return makeOrigin(host, id, delivery, true, expectedEpoch); }); + return { kind: "sent", source: id }; } catch (error) { inputs.reject(admission, error); failReservation(reservation, owner); @@ -371,25 +391,49 @@ export async function registerFlowLifecycle( } }; const sourceFailure = ( - scope: InputScope, - primary?: ReturnType, + operation: SourceOperation, + stillExpected = () => true, ) => { - const origin = inputs.positive(scope); - if (origin) - failOwned( - captureOwner( - scope.host, - "admission", - primary, - origin.epoch, - origin, - origin.id, - ), - ); + if (!operation.fail(stillExpected)) return; + autoDrive.deactivate(operation.scope.host); + recovery.revoke(operation.scope.host); + }; + const replacementEffects = (host: string) => { + const expected = attribution.current(host); + return { + check: ( + input: Parameters[0], + native: readonly SessionMessageInfo[], + changing: boolean, + ) => { + attribution.assertExpected(host, expected); + if (changing) + attribution.prepareReplacement( + input, + native, + expected, + true, + compactions.prefixBridge( + input, + native, + expected?.participation.kind === "flow" + ? expected.participation.origin + : undefined, + ), + ); + }, + current: () => attribution.current(host) === expected, + supersede: () => { + if (expected) attribution.end(expected); + revokeEffects(host); + }, + }; }; const compactions = new CompactionBoundary({ inputs, - readNative: (host) => ctx.session.context({ sessionID: host }), + readNative: (host) => ctx.session.context({ sessionID: host }, readOptions), + readSelected: (host) => ctx.session.get({ sessionID: host }, readOptions), + captureReplacement: replacementEffects, failure: sourceFailure, hasActivePrimary: (host) => { const current = attribution.current(host); @@ -410,33 +454,6 @@ export async function registerFlowLifecycle( if (!origin.authority) recovery.observeMessage(host, id, true, true); }, }); - const resolveParticipation = async ( - input: SessionContext, - native: readonly SessionMessageInfo[], - scope: InputScope, - ): Promise => { - const completed = compactions.completed(input, native); - if (completed) return { kind: "flow", origin: completed }; - const evidence = await inputs.evidence( - scope, - native, - nativeDelivery(input), - ); - if (evidence.kind === "live") { - assertLive(evidence.origin); - return { kind: "flow", origin: evidence.origin }; - } - const source = native.find((message) => message.id === scope.source); - if ( - (source?.type === "user" || source?.type === "synthetic") && - (evidence.kind === "retired-ordinary" || - (evidence.kind === "unregistered" && evidence.lineage !== "rejected")) - ) - return { kind: "native-only" }; - throw new Error( - "Flow context has an unknown, synthetic, or stale real input.", - ); - }; try { registrations.push( await ctx.session.hook("prompt", (input) => { @@ -477,7 +494,7 @@ export async function registerFlowLifecycle( throw new Error("Flow command was superseded."); }; const session = await ctx.session - .get({ sessionID: input.sessionID }) + .get({ sessionID: input.sessionID }, readOptions) .catch((error: unknown) => { failReservation(reservation, owner); throw error; @@ -585,64 +602,98 @@ export async function registerFlowLifecycle( }), ); registrations.push( - await ctx.session.hook("context", async (input) => { + await ctx.session.hook("context", async (value) => { assertOpen(); - const scope = inputs.capture(input); - const pending = contextPending.get(input.sessionID); - if (pending?.current()) { - sourceFailure(scope); - scope.release(); + const input = { + ...value, + model: { ...value.model }, + messages: structuredClone(value.messages), + }; + const prior = attribution.current(input.sessionID), + operation = inputs.openSource(input); + if (contextPending.get(input.sessionID)?.current()) { + sourceFailure( + operation, + () => attribution.current(input.sessionID) === prior, + ); + operation.release(); throw new Error("Flow primary context callbacks overlap."); } - contextPending.set(input.sessionID, scope); - const prior = attribution.current(input.sessionID); + contextPending.set(input.sessionID, operation); let frozen: ReturnType | undefined; + let completed: InputOrigin | undefined; + let replacement: + | ReturnType + | undefined; try { - await scope.ready(); - const native = await scope.wait( - ctx.session.context({ sessionID: input.sessionID }), + await operation.scope.ready(); + const native = await operation.scope.wait( + ctx.session.context({ sessionID: input.sessionID }, readOptions), ); - if ( - prior && - attribution.isCurrent(prior) && - prior.http.phase !== "ended" && - !attribution.retry(prior, native) - ) - throw new Error("Flow primary contexts or streams overlap."); - if (!isDeepStrictEqual(scope.consumed, nativeInputIDs(native))) + const session = await operation.scope.wait( + ctx.session.get({ sessionID: input.sessionID }, readOptions), + ); + attribution.prepareReplacement(input, native, prior, false); + + const resolved = await operation.resolve(native, session, { + check: (changing) => { + replacement = attribution.prepareReplacement( + input, + native, + prior, + changing, + changing + ? compactions.prefixBridge( + input, + native, + prior?.participation.kind === "flow" + ? prior.participation.origin + : undefined, + ) + : undefined, + ); + completed = compactions.completed(input, native); + }, + supersede: () => { + if (prior) attribution.end(prior); + revokeEffects(input.sessionID); + }, + }); + const participation = completed + ? { kind: "flow" as const, origin: completed } + : resolved.participation; + if (!participation) throw new Error( - "Flow native inputs differ from the model-visible prefix.", + "Flow context has an unknown, synthetic, or stale real input.", ); - const participation = await resolveParticipation( + attribution.assertExpected(input.sessionID, prior); + frozen = attribution.freeze( input, - native, - scope, - ); - scope.assertCurrent(); - const session = await scope.wait( - ctx.session.get({ sessionID: input.sessionID }), + resolved.native, + participation, + resolved.scope, + replacement, ); - if ( - participation.kind === "native-only" && - (session.id !== input.sessionID || - !sameDelivery(nativeDelivery(session), nativeDelivery(input))) - ) - throw new Error( - "Flow native selected session agent or model changed.", - ); - scope.assertCurrent(); - frozen = attribution.freeze(input, native, participation, scope); attribution.assertCurrent(frozen); - rememberCodingModel(codingModels, input, session, specialists); + rememberCodingModel( + codingModels, + input, + resolved.selected, + specialists, + ); } catch (error) { if (frozen) failOwned(captureOwner(input.sessionID, "attempt", frozen)); - else sourceFailure(scope, prior); + else + sourceFailure( + operation, + () => attribution.current(input.sessionID) === prior, + ); throw error; } finally { - if (contextPending.get(input.sessionID) === scope) + if (contextPending.get(input.sessionID) === operation) contextPending.delete(input.sessionID); - scope.release(); + operation.release(); } }), ); @@ -730,19 +781,70 @@ export async function registerFlowLifecycle( await registration.dispose(); throw error; } + const tasks = new Set>(), + chains = new Map>(), + scheduled = new Set(); + const cancelled = Promise.withResolvers(); + void cancelled.promise.catch(() => {}); + readController.signal.addEventListener( + "abort", + () => cancelled.reject(new Error("Flow lifecycle disposed.")), + { once: true }, + ); + const wait = (promise: Promise) => + Promise.race([promise, cancelled.promise]); + const enqueue = (host: string, job: () => Promise) => { + const task = (chains.get(host) ?? Promise.resolve()) + .then(async () => { + if (!closed && !streamLost) await job(); + }) + .catch((error) => + log("warn", "Flow terminal observation failed.", { + host, + message: errorOf(error).message, + }), + ) + .finally(() => { + tasks.delete(task); + if (chains.get(host) === task) chains.delete(host); + }); + chains.set(host, task); + tasks.add(task); + }; const subscription = (async () => { try { for await (const event of ctx.event.subscribe({ signal: controller.signal, })) { if (closed) break; + const host = + "sessionID" in event.data ? event.data.sessionID : undefined; + if ( + event.type === "session.inbox.enqueued" || + event.type === "session.inbox.cancelled" || + event.type === "session.inbox.delivered" || + event.type === "session.inbox.delivery.changed" + ) { + const { retryHost } = inputs.observeInbox(event, (host) => + revoke(host, false), + ); + if (retryHost) { + const primary = attribution.current(retryHost), + owner = captureOwner(retryHost, "attempt", primary); + enqueue(retryHost, async () => { + if (owns(owner) && !inputs.automaticBlock(retryHost)) + await autoDrive.resumeDeferred(retryHost); + }); + } + continue; + } if (event.type === "session.deleted") { if (outcomes.has(event.id)) continue; outcomes.add(event.id); - const host = event.data.sessionID; - revoke(host); - attribution.deleteSession(host); - codingModels.delete(host); + revoke(event.data.sessionID); + inputs.deleteHost(event.data.sessionID); + attribution.deleteSession(event.data.sessionID); + codingModels.delete(event.data.sessionID); continue; } if ( @@ -752,75 +854,108 @@ export async function registerFlowLifecycle( event.type !== "session.idle" ) continue; - if (outcomes.has(event.id)) continue; - const host = event.data.sessionID; + if (!host || outcomes.has(event.id) || scheduled.has(event.id)) + continue; const primary = attribution.current(host); if (!primary) continue; const owner = captureOwner(host, "attempt", primary); - let proved = false; - try { - const native = await ctx.session.context({ sessionID: host }); - if (!owns(owner)) continue; - const terminal = attribution.terminal( - primary, - native, - event.type === "session.idle" - ? undefined - : SessionMessage.ID.fromEvent(event.id), - ); - if ( - !terminal || - (event.type === "session.execution.failed" && - terminal.outcome !== "failed") || - (event.type === "session.execution.interrupted" && - terminal.outcome !== "interrupted") || - (event.type === "session.execution.succeeded" && - terminal.outcome !== "succeeded") - ) - continue; - proved = true; - outcomes.add(event.id); - if (primary.participation.kind === "native-only") { - attribution.retire(primary); - continue; - } - if (terminal.outcome !== "succeeded") { + scheduled.add(event.id); + enqueue(host, async () => { + let proved = false; + try { + if (!owns(owner)) return; + const native = await wait( + ctx.session.context({ sessionID: host }, readOptions), + ); + if (!owns(owner)) return; + const terminal = attribution.terminal( + primary, + native, + event.type === "session.idle" + ? undefined + : SessionMessage.ID.fromEvent(event.id), + ); + if ( + !terminal || + (event.type === "session.execution.failed" && + terminal.outcome !== "failed") || + (event.type === "session.execution.interrupted" && + terminal.outcome !== "interrupted") || + (event.type === "session.execution.succeeded" && + terminal.outcome !== "succeeded") + ) + return; + proved = true; + outcomes.add(event.id); + if (primary.participation.kind === "native-only") { + attribution.retire(primary); + return; + } + if (terminal.outcome !== "succeeded") { + options.onFlowTerminal?.(host); + failOwned(owner); + return; + } + const session = await wait( + ctx.session.get({ sessionID: host }, readOptions), + ); + if (!owns(owner)) return; + if (session.id !== host) + throw new Error("Flow native terminal session identity changed."); + if ( + session.outcome !== terminal.outcome || + session.time.idle !== terminal.time.created + ) + return; + const watermark = String(session.time.idle); + if (idle.get(host) === watermark) return; + idle.set(host, watermark); options.onFlowTerminal?.(host); - failOwned(owner); - continue; - } - const session = await ctx.session.get({ sessionID: host }); - if (!owns(owner)) continue; - if (session.id !== host) - throw new Error("Flow native terminal session identity changed."); - if (session.time.idle === undefined || session.outcome === undefined) - continue; - if ( - session.outcome !== terminal.outcome || - session.time.idle !== terminal.time.created - ) - continue; - const watermark = String(session.time.idle); - if (idle.get(host) === watermark) continue; - idle.set(host, watermark); - options.onFlowTerminal?.(host); - if (!runtimeGuard.query().operational) { - failOwned(owner); - continue; + if (!runtimeGuard.query().operational) { + failOwned(owner); + return; + } + const consumed = nativeInputIDs(native), + delivery = nativeDelivery(session); + const current = () => owns(owner) && attribution.isCurrent(primary); + const idleOwner: IdleOwner = { + current, + recheck: async () => { + if (!current()) return false; + const fresh = await wait( + ctx.session.context({ sessionID: host }, readOptions), + ); + if (!current()) return false; + const exact = attribution.terminal(primary, fresh, terminal.id); + const selected = await wait( + ctx.session.get({ sessionID: host }, readOptions), + ); + return ( + current() && + exact?.outcome === "succeeded" && + exact.time.created === terminal.time.created && + isDeepStrictEqual(nativeInputIDs(fresh), consumed) && + selected.id === host && + selected.time.idle === terminal.time.created && + selected.outcome === "succeeded" && + sameDelivery(nativeDelivery(selected), delivery) + ); + }, + }; + await autoDrive.onIdle(host, idleOwner); + } catch (error) { + if (proved && owns(owner)) failOwned(owner); + throw error; + } finally { + scheduled.delete(event.id); } - await autoDrive.onIdle(host); - } catch (error) { - if (proved && owns(owner)) failOwned(owner); - log("warn", "Flow terminal observation failed.", { - host, - message: errorOf(error).message, - }); - } + }); } if (!closed) throw new Error("Flow event stream ended unexpectedly."); } catch (error) { if (!closed) { streamLost = true; + readController.abort(); inputs.clear(); attribution.clear(); autoDrive.clear(); @@ -838,12 +973,14 @@ export async function registerFlowLifecycle( disposal ??= (async () => { closed = true; controller.abort(); + readController.abort(); inputs.clear(); attribution.clear(); autoDrive.clear(); recovery.revoke(); codingModels.clear(); await subscription; + await Promise.allSettled([...tasks]); const errors: unknown[] = []; for (const registration of registrations.reverse()) { try { diff --git a/src/platform/opencode/plugin-composition.ts b/src/platform/opencode/plugin-composition.ts index aa43d20b..e88ec6bd 100644 --- a/src/platform/opencode/plugin-composition.ts +++ b/src/platform/opencode/plugin-composition.ts @@ -114,12 +114,18 @@ export function createFlowPlugin(dependencies: { : {}), }; }, - prompt: async (sessionID, prompt, delivery, metadata) => { + prompt: async (sessionID, prompt, delivery, metadata, intent) => { if (!lifecycle) throw new Error( "Flow lifecycle is not ready for continuation dispatch.", ); - await lifecycle.prompt(sessionID, prompt, delivery, metadata); + return lifecycle.prompt( + sessionID, + prompt, + delivery, + metadata, + intent, + ); }, onWarning: (message) => log("warn", message), }); diff --git a/tests/auto-drive-dispatch.test.ts b/tests/auto-drive-dispatch.test.ts new file mode 100644 index 00000000..4dc97794 --- /dev/null +++ b/tests/auto-drive-dispatch.test.ts @@ -0,0 +1,332 @@ +import { expect, test } from "bun:test"; +import { RecoveryController } from "../src/application/recovery-policy.js"; +import { RecoveryProposals } from "../src/application/recovery-proposal.js"; +import { + AutoDriveDispatch, + type DispatchCapture, + type SendEffect, +} from "../src/platform/opencode/auto-drive-dispatch.js"; + +const delivery = { + agent: "build", + model: { providerID: "local", modelID: "test" }, +}; +function fixture() { + const repairs = new Set(), + dispatch = new AutoDriveDispatch(repairs); + dispatch.delivery = delivery; + dispatch.messageId = "O"; + dispatch.checkpoint = { revision: 4, answered: false, advance: 5 }; + dispatch.pendingReply = true; + dispatch.ownInspectDraft = { + sessionId: "flow", + revision: 5, + prompted: false, + }; + let live = true; + const capture: DispatchCapture = { + authority: { hostSessionId: "host", token: "token", epoch: 1 }, + source: "O", + current: () => live && dispatch.messageId === "O", + }; + return { + dispatch, + repairs, + capture, + revoke: () => { + live = false; + dispatch.invalidate(); + }, + }; +} +for (const branch of [ + "initial", + "continuation", + "draft", + "recovery", + "handback-and-wait", + "handback-or-deactivate", +] as const) + test(`withheld ${branch} preserves its exact send opportunity before authenticated acceptance`, async () => { + const f = fixture(), + proposals = new RecoveryProposals(), + proposal = proposals.prepare("flow/5", () => true); + if (!proposal || !f.dispatch.ownInspectDraft) + throw new Error("Missing fixture claim."); + const effect: SendEffect = + branch === "continuation" + ? { + kind: branch, + revision: 5, + clearCheckpoint: true, + repairKey: "repair", + } + : branch === "draft" + ? { kind: branch, draft: f.dispatch.ownInspectDraft } + : branch === "recovery" + ? { kind: branch, revision: 5, proposal } + : branch === "initial" + ? { kind: branch } + : { + kind: branch, + projection: { + sessionId: "flow", + revision: 5, + status: "blocked", + nextAction: "await-user-direction", + }, + }; + const before = { + checkpoint: f.dispatch.checkpoint, + draft: f.dispatch.ownInspectDraft, + }; + const result = await f.dispatch.send( + async () => ({ kind: "withheld", reason: "pending-user" }), + "host", + "intended prompt", + {}, + effect, + true, + f.capture, + ); + expect(result).toEqual({ kind: "withheld", reason: "pending-user" }); + expect(f.dispatch.checkpoint).toBe(before.checkpoint); + expect(f.dispatch.ownInspectDraft).toBe(before.draft); + expect(f.dispatch.ownInspectDraft.prompted).toBe(false); + expect(f.dispatch.pendingReply).toBe(true); + expect(f.dispatch.lastPromptedRevision).toBeNull(); + expect(f.dispatch.handbackPromptedRevision).toBeNull(); + expect(f.dispatch.messageId).toBe("O"); + expect([...f.repairs]).toEqual([]); + expect(proposals.prepare("flow/5", () => true)).not.toBeNull(); + const admitted = await f.dispatch.send( + async (_host, _text, selected, _metadata, intent) => { + f.dispatch.activate(intent, "S", selected); + return { kind: "sent", source: "S" }; + }, + "host", + "intended prompt", + {}, + effect, + true, + f.capture, + ); + expect(admitted).toEqual({ kind: "sent", source: "S" }); + expect(f.dispatch.messageId).toBe("S"); + expect(f.dispatch.pendingReply).toBe(false); + if (branch === "continuation") { + expect(f.dispatch.checkpoint).toEqual({ revision: 5, answered: false }); + expect([...f.repairs]).toEqual(["repair"]); + } + if (branch === "initial") expect(f.dispatch.lastPromptedRevision).toBe(0); + if (branch === "draft") + expect(f.dispatch.ownInspectDraft.prompted).toBe(true); + if (branch === "recovery") + expect(proposals.prepare("flow/5", () => true)).toBeNull(); + }); +test("initial authenticated activation creates revision zero before returning and rejects repeated activation", async () => { + const f = fixture(); + f.dispatch.checkpoint = null; + await f.dispatch.send( + async (_host, _text, selected, _metadata, intent) => { + f.dispatch.activate(intent, "initial", selected); + expect(f.dispatch.checkpoint).toEqual({ revision: 0, answered: false }); + expect(() => f.dispatch.activate(intent, "duplicate", selected)).toThrow( + "superseded", + ); + return { kind: "sent", source: "initial" }; + }, + "host", + "initial", + {}, + { kind: "initial" }, + false, + f.capture, + ); + expect(f.dispatch.messageId).toBe("initial"); +}); +test("stale intent rejects before checkpoint, draft, reply or recovery fields change", async () => { + const f = fixture(), + entered = Promise.withResolvers(), + release = Promise.withResolvers(); + const send = f.dispatch.send( + async (_host, _text, selected, _metadata, intent) => { + entered.resolve(); + await release.promise; + f.dispatch.activate(intent, "stale", selected); + return { kind: "sent", source: "stale" }; + }, + "host", + "continuation", + {}, + { + kind: "continuation", + revision: 5, + clearCheckpoint: true, + repairKey: "repair", + }, + true, + f.capture, + ); + const failed = send.catch((error) => String(error)); + await entered.promise; + f.revoke(); + release.resolve(); + expect(await failed).toBe( + "Error: Flow automatic dispatch intent was superseded.", + ); + expect(f.dispatch.messageId).toBe("O"); + expect(f.dispatch.checkpoint).toEqual({ + revision: 4, + answered: false, + advance: 5, + }); + expect(f.dispatch.pendingReply).toBe(true); + expect([...f.repairs]).toEqual([]); +}); +test("a deferred terminal ticket is consumed once and rechecks before reevaluation", async () => { + const f = fixture(); + let rechecks = 0; + const owner = { + current: () => true, + recheck: async () => { + rechecks++; + return true; + }, + }; + await f.dispatch.send( + async () => ({ kind: "withheld", reason: "input-settlement" }), + "host", + "continuation", + {}, + { kind: "initial" }, + false, + f.capture, + owner, + ); + expect(await f.dispatch.takeDeferred()).toBe(owner); + expect(await f.dispatch.takeDeferred()).toBeNull(); + expect(rechecks).toBe(1); +}); + +test("an in-place draft revision change rejects a prepared approval without stamps", async () => { + const f = fixture(), + entered = Promise.withResolvers(), + release = Promise.withResolvers(); + const draft = f.dispatch.ownInspectDraft; + if (!draft) throw new Error("Missing draft."); + const send = f.dispatch + .send( + async (_host, _text, selected, _metadata, intent) => { + entered.resolve(); + await release.promise; + f.dispatch.activate(intent, "stale", selected); + return { kind: "sent", source: "stale" }; + }, + "host", + "approve", + {}, + { kind: "draft", draft }, + true, + f.capture, + ) + .catch(String); + await entered.promise; + draft.revision = 6; + release.resolve(); + expect(await send).toBe( + "Error: Flow inspection draft changed before admission.", + ); + expect(draft.prompted).toBe(false); + expect(f.dispatch.pendingReply).toBe(true); + expect(f.dispatch.messageId).toBe("O"); +}); + +const unavailable = { + assess: async () => ({ + kind: "unavailable" as const, + reason: "offline control", + }), +}; +for (const mode of [ + "before-activation", + "between-assert-and-commit", +] as const) { + test(`review proposal expiry ${mode}`, async () => { + const now = 0; + let activation = false; + let checks = 0; + const recovery = new RecoveryController(unavailable, { + now: () => { + if (!activation) return now; + checks++; + return mode === "before-activation" || checks > 1 ? 3600000 : 3599999; + }, + }); + recovery.activate("host", { mode: "shadow", maxCalls: 2, maxUsd: 0.01 }); + const proposal = recovery.prepareProposalPrompt( + "host", + "flow", + 5, + "await-user-direction", + ); + expect(proposal).not.toBeNull(); + if (!proposal) throw new Error("Missing claim"); + const send = new AutoDriveDispatch(new Set()); + send.delivery = delivery; + send.messageId = "O"; + send.checkpoint = { revision: 4, answered: false, advance: 5 }; + send.pendingReply = true; + const capture = { + authority: { hostSessionId: "host", token: "token", epoch: 1 }, + source: "O", + current: () => send.messageId === "O", + }; + const failure = await send + .send( + async (_host, _text, selected, _metadata, intent) => { + activation = true; + send.activate(intent, "S", selected); + return { kind: "sent", source: "S" }; + }, + "host", + proposal.text, + {}, + { kind: "recovery", revision: 5, proposal }, + true, + capture, + ) + .catch(String); + expect(failure).toBe("Error: Flow recovery proposal was superseded."); + expect(send.messageId).toBe("O"); + expect(send.checkpoint).toEqual({ + revision: 4, + answered: false, + advance: 5, + }); + expect(send.pendingReply).toBe(true); + }); +} +test("review real recovery claim is invalid after exact lease replacement", () => { + const recovery = new RecoveryController(unavailable, { now: () => 0 }); + recovery.activate("host", { mode: "shadow", maxCalls: 2, maxUsd: 0.01 }); + const old = recovery.prepareProposalPrompt( + "host", + "flow", + 5, + "await-user-direction", + ); + recovery.activate("host", { mode: "shadow", maxCalls: 2, maxUsd: 0.01 }); + expect(() => old?.commit()).toThrow("superseded"); + const current = recovery.prepareProposalPrompt( + "host", + "flow", + 5, + "await-user-direction", + ); + expect(current).not.toBeNull(); + current?.commit(); + expect( + recovery.prepareProposalPrompt("host", "flow", 5, "await-user-direction"), + ).toBeNull(); +}); diff --git a/tests/auto-drive.test.ts b/tests/auto-drive.test.ts index 028c2913..9a7dc030 100644 --- a/tests/auto-drive.test.ts +++ b/tests/auto-drive.test.ts @@ -14,6 +14,7 @@ import { FLOW_AUTO_METADATA_KEY, } from "../src/platform/opencode/auto-drive.js"; import { ValidationCaptureCoordinator } from "../src/platform/opencode/validation-capture.js"; +import { fixtureAutomaticPrompt } from "./fixtures/verified-continuation-origin.js"; const DELIVERY: AutoDriveDelivery = { agent: "build", @@ -45,12 +46,17 @@ function mutate( host: string, revision: number, created?: string, - parent = "command-message", + parent?: string, reviewerPending = false, mutation?: { tool: string; goal: string; features: Array<{ kind?: string }> }, ): void { const id = `assistant-${++assistantSequence}`; - driver.observeAssistant(host, id, parent); + const token = driver.captureAuthority(host)?.token; + const source = + parent ?? + (token ? driver.captureDispatch(host, token)?.source : null) ?? + "command-message"; + driver.observeAssistant(host, id, source); driver.observeMutation( host, revision, @@ -86,10 +92,13 @@ function harness(initial: AutoDriveProjection) { const driver = new AutoDriveCoordinator({ readProjection: () => Promise.resolve(projection), readSourceDigest: () => Promise.resolve(source), - prompt: (_sessionID, text, delivery, metadata) => { - prompts.push({ text, delivery, metadata }); - return Promise.resolve(); - }, + prompt: fixtureAutomaticPrompt( + () => driver, + (_sessionID, text, delivery, metadata) => { + prompts.push({ text, delivery, metadata }); + return Promise.resolve(); + }, + ), onWarning: (message) => warnings.push(message), createToken: () => `token-${++token}`, now: () => now, @@ -235,11 +244,15 @@ describe("Flow auto-drive coordinator", () => { await state.driver.onIdle("session-host1"); expect(state.prompts).toHaveLength(1); - expect(state.warnings.at(-1)).toContain("made no lifecycle progress"); - expect(state.driver.compactionContext("session-host1")).not.toBeNull(); + expect(state.warnings).toEqual([]); + expect(state.driver.continuationSnapshot("session-host1")).toMatchObject({ + state: "inactive", + reason: "no-progress", + }); + expect(state.driver.compactionContext("session-host1")).toBeNull(); state.setNow(1_000); expect(state.driver.timingSnapshot()).toMatchObject({ - state: "paused", + state: "inactive", activeMs: 25, waitingForUserMs: 0, }); @@ -1106,10 +1119,13 @@ describe("Flow auto-drive coordinator", () => { } return Promise.resolve(projection); }, - prompt: (_sessionID, _prompt, delivery) => { - prompts.push(delivery); - return Promise.resolve(); - }, + prompt: fixtureAutomaticPrompt( + () => driver, + (_sessionID, _prompt, delivery) => { + prompts.push(delivery); + return Promise.resolve(); + }, + ), createToken: () => "token", }); const metadata = await driver.activate("session-host1"); @@ -1171,10 +1187,13 @@ describe("Flow auto-drive coordinator", () => { }); return Promise.resolve(projection); }, - prompt: (_sessionID, _prompt, delivery) => { - prompts.push(delivery); - return Promise.resolve(); - }, + prompt: fixtureAutomaticPrompt( + () => driver, + (_sessionID, _prompt, delivery) => { + prompts.push(delivery); + return Promise.resolve(); + }, + ), }); const metadata = await driver.activate("session-host1"); await driver.observeMessage( @@ -1234,7 +1253,10 @@ describe("Flow auto-drive coordinator", () => { }); return Promise.resolve(checkpoint); }, - prompt: () => Promise.resolve(), + prompt: fixtureAutomaticPrompt( + () => driver, + () => Promise.resolve(), + ), }); await driver.activate("session-host1"); const olderIdle = driver.onIdle("session-host1"); @@ -1281,10 +1303,13 @@ describe("Flow auto-drive coordinator", () => { } return Promise.resolve(projection); }, - prompt: (_sessionID, _prompt, delivery) => { - prompts.push(delivery); - return Promise.resolve(); - }, + prompt: fixtureAutomaticPrompt( + () => driver, + (_sessionID, _prompt, delivery) => { + prompts.push(delivery); + return Promise.resolve(); + }, + ), createToken: () => "token", }); const metadata = await driver.activate("session-host1"); @@ -1341,10 +1366,13 @@ describe("Flow auto-drive coordinator", () => { }); return Promise.resolve(projection); }, - prompt: (_sessionID, _prompt, delivery) => { - prompts.push(delivery); - return Promise.resolve(); - }, + prompt: fixtureAutomaticPrompt( + () => driver, + (_sessionID, _prompt, delivery) => { + prompts.push(delivery); + return Promise.resolve(); + }, + ), }); const metadata = await driver.activate("session-host1"); await driver.observeMessage( @@ -1395,10 +1423,13 @@ describe("Flow auto-drive coordinator", () => { }); return Promise.resolve(projection); }, - prompt: (_sessionID, _prompt, delivery) => { - prompts.push(delivery); - return Promise.resolve(); - }, + prompt: fixtureAutomaticPrompt( + () => driver, + (_sessionID, _prompt, delivery) => { + prompts.push(delivery); + return Promise.resolve(); + }, + ), }); const metadata = await driver.activate("session-host1"); await driver.observeMessage( @@ -1443,20 +1474,23 @@ describe("Flow auto-drive coordinator", () => { nextAction: "flow_plan_approve", }; let resolvePending: ((projection: AutoDriveProjection) => void) | undefined; - let reads = 0; + let holdRead = false; const prompts: AutoDriveDelivery[] = []; const driver = new AutoDriveCoordinator({ readProjection: () => { - if (++reads === 5) + if (holdRead) return new Promise((resolve) => { resolvePending = resolve; }); return Promise.resolve(projection); }, - prompt: (_sessionID, _prompt, delivery) => { - prompts.push(delivery); - return Promise.resolve(); - }, + prompt: fixtureAutomaticPrompt( + () => driver, + (_sessionID, _prompt, delivery) => { + prompts.push(delivery); + return Promise.resolve(); + }, + ), }); const metadata = await driver.activate("session-host1"); await driver.observeMessage( @@ -1482,6 +1516,13 @@ describe("Flow auto-drive coordinator", () => { await driver.onIdle("session-host1"); expect(prompts).toHaveLength(1); + await driver.observeMessage( + "session-host1", + DELIVERY, + [{ text: "Acknowledged continuation checkpoint." }], + "continued-checkpoint-reply", + ); + holdRead = true; const laterIdle = driver.onIdle("session-host1"); await driver.observeMessage( "session-host1", @@ -1518,10 +1559,13 @@ describe("Flow auto-drive coordinator", () => { } return Promise.resolve(projection); }, - prompt: (_sessionID, _prompt, delivery) => { - prompts.push(delivery); - return Promise.resolve(); - }, + prompt: fixtureAutomaticPrompt( + () => driver, + (_sessionID, _prompt, delivery) => { + prompts.push(delivery); + return Promise.resolve(); + }, + ), }); const metadata = await driver.activate("session-host1"); await driver.observeMessage( @@ -1578,7 +1622,10 @@ describe("Flow auto-drive coordinator", () => { }); return Promise.resolve(projection); }, - prompt: () => Promise.resolve(), + prompt: fixtureAutomaticPrompt( + () => driver, + () => Promise.resolve(), + ), }); const metadata = await driver.activate("session-host1"); await driver.observeMessage( @@ -1626,10 +1673,13 @@ describe("Flow auto-drive coordinator", () => { } return Promise.resolve(projection); }, - prompt: (_sessionID, _prompt, delivery) => { - prompts.push(delivery); - return Promise.resolve(); - }, + prompt: fixtureAutomaticPrompt( + () => driver, + (_sessionID, _prompt, delivery) => { + prompts.push(delivery); + return Promise.resolve(); + }, + ), }); const metadata = await driver.activate("session-host1"); await driver.observeMessage( @@ -1736,10 +1786,13 @@ describe("Flow auto-drive coordinator", () => { resolveProjection = resolve; }); }, - prompt: (_sessionID, prompt) => { - prompts.push(prompt); - return Promise.resolve(); - }, + prompt: fixtureAutomaticPrompt( + () => driver, + (_sessionID, prompt) => { + prompts.push(prompt); + return Promise.resolve(); + }, + ), createToken: () => "token", }); await driver.activate("session-host1"); @@ -1789,13 +1842,16 @@ describe("Flow auto-drive coordinator", () => { }); const driver = new AutoDriveCoordinator({ readProjection: () => Promise.resolve(projection), - prompt: () => { - promptCount += 1; - markPromptStarted?.(); - return new Promise((resolve) => { - resolvePrompt = resolve; - }); - }, + prompt: fixtureAutomaticPrompt( + () => driver, + () => { + promptCount += 1; + markPromptStarted?.(); + return new Promise((resolve) => { + resolvePrompt = resolve; + }); + }, + ), }); const metadata = await driver.activate("session-host1"); await driver.observeMessage( @@ -1851,7 +1907,10 @@ describe("Flow auto-drive coordinator", () => { nextAction: "flow_run_start", }); }, - prompt: () => Promise.resolve(), + prompt: fixtureAutomaticPrompt( + () => driver, + () => Promise.resolve(), + ), createToken: () => `token-${reads}`, }); await driver.activate("session-host1"); @@ -1886,7 +1945,10 @@ describe("Flow auto-drive coordinator", () => { nextAction: "flow_run_start", }); }, - prompt: () => Promise.resolve(), + prompt: fixtureAutomaticPrompt( + () => driver, + () => Promise.resolve(), + ), }); const first = driver.activate("session-host1"); const second = await driver.activate("session-host1"); @@ -1978,7 +2040,10 @@ describe("Flow auto-drive coordinator", () => { revision: reads, nextAction: "flow_run_start", }), - prompt: () => Promise.reject(new Error("unavailable")), + prompt: fixtureAutomaticPrompt( + () => driver, + () => Promise.reject(new Error("unavailable")), + ), createToken: () => failure, }); await driver.activate("session-host1"); diff --git a/tests/distribution-and-surface.test.ts b/tests/distribution-and-surface.test.ts index 7f10c541..83f20bcb 100644 --- a/tests/distribution-and-surface.test.ts +++ b/tests/distribution-and-surface.test.ts @@ -18,7 +18,10 @@ import { getFlowGuidance, } from "../src/guidance/catalog.js"; import FlowPlugin from "../src/index.js"; -import { FLOW_AUTO_METADATA_KEY } from "../src/platform/opencode/auto-drive.js"; +import { + AutoDriveCoordinator, + FLOW_AUTO_METADATA_KEY, +} from "../src/platform/opencode/auto-drive.js"; import { prepareFlowCommand } from "../src/platform/opencode/command-hook.js"; import { createNativeFlowHost, @@ -50,6 +53,7 @@ const COMMAND_NAMES = [ ]; const hosts: NativeFlowHost[] = []; const workspaces: string[] = []; +const pendingDecisionObservations = new Set<() => void>(); async function load(options: Parameters[1] = {}) { const workspace = await mkdtemp(join(tmpdir(), "flow-native-surface-")); workspaces.push(workspace); @@ -58,6 +62,79 @@ async function load(options: Parameters[1] = {}) { await host.load(FlowPlugin); return { host, workspace }; } +async function idleDecision( + host: NativeFlowHost, + id: string, + receiver?: AutoDriveCoordinator, +) { + if (pendingDecisionObservations.size) + throw new Error("Overlapping coordinator observation."); + const token = host.admissions.findLast( + (receipt) => + receipt.sessionID === id && + typeof receipt.payload.metadata?.[FLOW_AUTO_METADATA_KEY] === "string", + )?.payload.metadata?.[FLOW_AUTO_METADATA_KEY]; + if (!receiver && typeof token !== "string") + throw new Error("Missing fixture invocation token."); + const original = AutoDriveCoordinator.prototype.onIdle, + completed = Promise.withResolvers(); + let failure: unknown; + const observed = async function ( + this: AutoDriveCoordinator, + ...args: Parameters + ) { + if ( + args[0] !== id || + (receiver + ? this !== receiver + : this.captureAuthority(id)?.token !== token) + ) + return original.apply(this, args); + receiver = this; + try { + await original.apply(this, args); + } catch (error) { + failure = error; + throw error; + } finally { + completed.resolve(); + } + }; + AutoDriveCoordinator.prototype.onIdle = observed; + const cancel = () => { + if (AutoDriveCoordinator.prototype.onIdle === observed) + AutoDriveCoordinator.prototype.onIdle = original; + completed.reject( + new Error("Fixture disposed before the coordinator decision completed."), + ); + }; + pendingDecisionObservations.add(cancel); + try { + await host.idle(id); + await completed.promise; + if (AutoDriveCoordinator.prototype.onIdle !== observed) + throw new Error("Coordinator observation was replaced."); + if (failure) throw failure; + if (!receiver) throw new Error("No matching coordinator completed."); + return receiver; + } finally { + pendingDecisionObservations.delete(cancel); + if (AutoDriveCoordinator.prototype.onIdle === observed) + AutoDriveCoordinator.prototype.onIdle = original; + } +} +async function waitingForPlanApproval(host: NativeFlowHost, id: string) { + for (;;) { + const status = await host.response("flow_status", statusInput, id); + const state = status.workflowData.autoDrive; + if ( + state && + field(state, "state") === "waiting" && + field(state, "reason") === "plan-approval" + ) + return; + } +} function exactlyOnce(text: string, fragment: string) { expect(text.split(fragment)).toHaveLength(2); } @@ -98,6 +175,8 @@ function field(value: unknown, name: string) { return z.record(z.string(), z.unknown()).parse(value)[name]; } afterEach(async () => { + for (const cancel of pendingDecisionObservations) cancel(); + pendingDecisionObservations.clear(); const errors: unknown[] = []; for (const host of hosts.splice(0).reverse()) try { @@ -373,7 +452,14 @@ describe("flow-auto native continuation", () => { expect(projectionOf(saved).sessionId).toBe( projectionOf(provisional).sessionId, ); - await host.idle(manager); + const approval = expectedApproval + ? host.nextSynthetic(manager, (input) => + input.text.includes("Approve this one draft"), + ) + : null; + await idleDecision(host, manager); + if (approval) await approval; + else await waitingForPlanApproval(host, manager); expect( host.synthetics.filter((input) => input.text.includes("Approve this one draft"), @@ -486,7 +572,9 @@ describe("flow-auto native continuation", () => { ); expect(saved.status).toBe("ok"); expect((await approve(host, 1, "ses_owner")).status).toBe("ok"); - await host.idle("ses_owner"); + const continued = expected ? host.nextSynthetic("ses_owner") : null; + await idleDecision(host, "ses_owner"); + if (continued) await continued; expect(host.synthetics).toHaveLength(expected); }, ); @@ -495,13 +583,14 @@ describe("flow-auto native continuation", () => { await save(host); await host.command("flow-auto", "--recovery=off", "ses_owner"); await host.response("flow_status", statusInput, "ses_owner"); - await host.idle("ses_owner"); + const coordinator = await idleDecision(host, "ses_owner"); + await waitingForPlanApproval(host, "ses_owner"); await host.admit("Approve the plan.", "ses_owner"); await host.beginTurn("ses_owner"); expect((await approve(host, 1, "ses_other")).status).toBe("ok"); const replay = await approve(host, 1, "ses_owner"); expect(replay.workflowData.operation?.replayed).toBe(true); - await host.idle("ses_owner"); + await idleDecision(host, "ses_owner", coordinator); expect(host.synthetics).toEqual([]); }); test("missing native request identity rejects a mutation before durable effect", async () => { @@ -549,7 +638,8 @@ describe("flow-auto native continuation", () => { const saved = await save(host); await host.command("flow-auto", "--recovery=off", "ses_close"); await host.response("flow_status", statusInput, "ses_close"); - await host.idle("ses_close"); + await idleDecision(host, "ses_close"); + await waitingForPlanApproval(host, "ses_close"); await host.admit("Defer this session.", "ses_close"); await writeFile(join(workspace, ".flow/history"), "unsafe\n"); const closed = await host.response( @@ -574,7 +664,9 @@ describe("flow-auto native continuation", () => { }); await unlink(join(workspace, ".flow/history")); await mkdir(join(workspace, ".flow/history")); + const retried = host.nextSynthetic("ses_close"); await host.idle("ses_close"); + await retried; expect(host.synthetics).toHaveLength(1); expect(host.synthetics[0]?.text).toContain("flow_session_close"); }); @@ -588,11 +680,13 @@ describe("flow-auto native continuation", () => { }); await host.command("flow-auto", "--recovery=off", manager); await host.response("flow_status", statusInput, manager); - await host.idle(manager); + const coordinator = await idleDecision(host, manager); + await waitingForPlanApproval(host, manager); expect(host.synthetics).toEqual([]); await host.admit("What do you recommend?", manager); await host.response("flow_status", statusInput, manager); - await host.idle(manager); + await idleDecision(host, manager, coordinator); + await waitingForPlanApproval(host, manager); expect(host.synthetics).toEqual([]); await host.admit("Approve the plan.", manager); const args = { @@ -612,7 +706,9 @@ describe("flow-auto native continuation", () => { result: decorated, }); expect(decorated.content).toEndWith("[audit-footer]"); - await host.idle(manager); + const continuation = host.nextSynthetic(manager); + await idleDecision(host, manager, coordinator); + await continuation; expect(host.synthetics).toHaveLength(1); const dispatched = host.synthetics[0]; expect(dispatched).toMatchObject({ @@ -644,7 +740,7 @@ describe("flow-auto native continuation", () => { authoritative: false, state: "active", }); - await host.idle(manager); + await idleDecision(host, manager, coordinator); expect(host.synthetics).toHaveLength(1); await host.events.emit({ id: "evt_interrupt", diff --git a/tests/execution-attribution.test.ts b/tests/execution-attribution.test.ts index 9c961665..1be6bc12 100644 --- a/tests/execution-attribution.test.ts +++ b/tests/execution-attribution.test.ts @@ -758,7 +758,10 @@ test("resource end grants no same-origin retry and cannot revive an explicitly r error: { type: "provider.transport", message: "Refused" }, }, }; - expect(f.attribution.retry(f.primary, [user, retry])).toBe(false); + expect( + f.attribution.prepareReplacement(context, [user, retry], f.primary, false) + .retry, + ).toBe(false); f.revoke(); expect(() => f.attribution.verify( @@ -933,12 +936,21 @@ test("permitted retry shares exclusions and admits the retry assistant's first t }, }; const exclusions = retainedSnapshot(first)?.previousAssistants; - expect(attribution.retry(first, [user, excluded, retry])).toBe(true); + const permit = attribution.prepareReplacement( + context, + [user, excluded, retry], + first, + false, + ); + expect(permit.retry).toBe(true); + expect(first.http.phase).toBe("request"); + expect(attribution.current(sid)).toBe(first); const next = attribution.freeze( context, [user, excluded, retry], { kind: "flow", origin: origin }, clock, + permit, ); expect(retainedSnapshot(next)?.previousAssistants).toBe(exclusions); expect(retainedSnapshot(first)).toBeUndefined(); @@ -966,7 +978,9 @@ test("permitted retry shares exclusions and admits the retry assistant's first t messageID: SessionMessage.ID.make("msg_excluded"), }), ).toBeUndefined(); - expect(() => attribution.retry(first, [user, retry])).toThrow("superseded"); + expect(() => + attribution.prepareReplacement(context, [user, retry], first, false), + ).toThrow("superseded"); expect(() => attribution.terminal(first, [user])).toThrow("superseded"); }); diff --git a/tests/fixtures/native-flow-host.ts b/tests/fixtures/native-flow-host.ts index 0c3d703b..b9238717 100644 --- a/tests/fixtures/native-flow-host.ts +++ b/tests/fixtures/native-flow-host.ts @@ -200,6 +200,7 @@ export async function createNativeFlowHost( const messages = new Map(); const prompts: SessionPromptInput[] = []; const synthetics: SessionSyntheticInput[] = []; + const syntheticObservers = new Set<(input: SessionSyntheticInput) => void>(); const admissions: Array = []; const tools = new Map(); const commands = new Map(); @@ -386,6 +387,7 @@ export async function createNativeFlowHost( delivery: input.delivery ?? "queue", }; synthetics.push(structuredClone(input)); + for (const observe of syntheticObservers) observe(input); admissions.push(structuredClone(receipt)); history(input.sessionID).push( structuredClone({ @@ -552,7 +554,10 @@ export async function createNativeFlowHost( const session = ensureSession(host); if ( !history(host).some( - (message) => message.type === "user" || message.type === "synthetic", + (message) => + message.type === "user" || + message.type === "synthetic" || + (message.type === "compaction" && message.status === "completed"), ) ) await prompt({ sessionID: host, text: "Inspect current Flow state." }); @@ -690,6 +695,19 @@ export async function createNativeFlowHost( messages, prompts, synthetics, + nextSynthetic( + host: string, + matches: (input: SessionSyntheticInput) => boolean = () => true, + ) { + return new Promise((resolve) => { + const observe = (input: SessionSyntheticInput) => { + if (input.sessionID !== host || !matches(input)) return; + syntheticObservers.delete(observe); + resolve(structuredClone(input)); + }; + syntheticObservers.add(observe); + }); + }, admissions, permissions, events, @@ -712,6 +730,7 @@ export async function createNativeFlowHost( } }, async dispose() { + syntheticObservers.clear(); for (const cleanup of cleanups.splice(0).reverse()) await cleanup(); }, async command( diff --git a/tests/fixtures/verified-continuation-origin.ts b/tests/fixtures/verified-continuation-origin.ts index 2ab8b409..3f2bae15 100644 --- a/tests/fixtures/verified-continuation-origin.ts +++ b/tests/fixtures/verified-continuation-origin.ts @@ -91,9 +91,23 @@ export async function verifiedContinuationOrigin( }, ], }; - const scope = inputs.capture(context); - await scope.ready(); - const evidence = await inputs.evidence(scope, [user], delivery); + const session: SessionInfo = { + id: sessionID, + agent, + model, + projectID: "fixture", + location: { directory: process.cwd() }, + time: { created: 0, updated: 0 }, + cost: 0, + tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } }, + }; + const operation = inputs.openSource(context); + const resolved = await operation.resolve([user], session, { + check: () => {}, + supersede: () => {}, + }); + const scope = resolved.scope, + evidence = resolved.evidence; if (evidence.kind !== "live") throw new Error("Missing accepted fixture origin."); attribution.freeze( @@ -125,16 +139,7 @@ export async function verifiedContinuationOrigin( }, ], }; - const session: SessionInfo = { - id: sessionID, - agent, - model, - projectID: "fixture", - location: { directory: process.cwd() }, - time: { created: 0, updated: 0 }, - cost: 0, - tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } }, - }; + const verified = attribution.verify( { sessionID, agent, messageID, id }, [user, assistant], @@ -146,8 +151,67 @@ export async function verifiedContinuationOrigin( throw new Error("Expected Flow fixture participation."); const origin = verified.primary.participation.origin; auto.observeAssistant(host, assistantID, origin.id); - scope.release(); + operation.release(); inputs.clear(); attribution.clear(); return origin.id; } + +export function fixtureAutomaticPrompt( + auto: () => AutoDriveCoordinator, + observe: ( + ...args: Parameters< + import("../../src/platform/opencode/auto-drive-dispatch.js").AutomaticPrompt + > + ) => void | Promise = () => {}, + afterAdmission = false, +): import("../../src/platform/opencode/auto-drive-dispatch.js").AutomaticPrompt { + return async (...args) => { + const [host, text, delivery, metadata, intent] = args; + const token = metadata[FLOW_AUTO_METADATA_KEY]; + if (typeof token !== "string") throw new Error("Missing fixture token."); + const driver = auto(), + id = SessionMessage.ID.create(); + const inputs = new InputAdmissions({ + assertOpen: () => {}, + authority: () => driver.captureAuthority(host), + authorityCurrent: (authority) => driver.isAuthorityCurrent(authority), + observeReal: async () => {}, + }); + const payload = { text, metadata: { [FLOW_AUTO_METADATA_KEY]: token } }; + const reservation = inputs.reserve(host, id), + admission = inputs.register(reservation, { type: "synthetic", payload }); + const accept = () => + inputs.accept( + admission, + { + sessionID: host, + id, + type: "synthetic", + payload, + time: { created: 0 }, + delivery: "queue", + }, + async () => { + const result = await driver.observeMessage( + host, + delivery, + [{ type: "text", text, synthetic: true, metadata }], + id, + intent, + ); + if (result !== "accepted-continuation") + throw new Error("Fixture dispatch was not accepted."); + return inputs.makeOrigin(host, id, delivery, true, reservation.epoch); + }, + ); + try { + if (afterAdmission) await accept(); + await observe(...args); + if (!afterAdmission) await accept(); + return { kind: "sent", source: id }; + } finally { + inputs.clear(); + } + }; +} diff --git a/tests/input-admission.test.ts b/tests/input-admission.test.ts index 00b5e468..101f431c 100644 --- a/tests/input-admission.test.ts +++ b/tests/input-admission.test.ts @@ -2,6 +2,7 @@ import { expect, test } from "bun:test"; import { Agent, Model, Provider } from "@opencode/plugin"; import type { SessionContext } from "@opencode/plugin/promise/session"; import { Session } from "@opencode/schema/session"; +import { Skill } from "@opencode/schema/skill"; import { InputAdmissions } from "../src/platform/opencode/input-admission.js"; const host = Session.ID.make("session-ledger"); @@ -16,12 +17,25 @@ const delivery = { }; const context = ( id: string, -): Pick => ({ +): Pick => ({ + agent, + model, sessionID: host, messages: [ { role: "user", id, content: [{ type: "text", text: "Approved task" }] }, ], }); +const selected: SessionInfo = { + id: host, + agent, + model, + projectID: "test", + location: { directory: process.cwd() }, + time: { created: 0, updated: 0 }, + cost: 0, + tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } }, +}; +const effects = { check: () => {}, supersede: () => {} }; function fixture() { let open = true; const inputs = new InputAdmissions({ @@ -60,12 +74,26 @@ function fixture() { } test("matching receipt wakes only the exact admission scope", async () => { const f = fixture(); - const scope = f.inputs.capture(context("user-1")); - const ready = scope.ready(); - expect(f.inputs.positive(scope)).toBeUndefined(); + const scope = f.inputs.openSource(context("user-1")); + const ready = scope.scope.ready(); + expect(f.admission.state).toBe("pending"); await f.inputs.accept(f.admission, f.receipt, async () => f.origin()); await ready; - expect(f.inputs.positive(scope)?.id).toBe("user-1"); + const resolved = await scope.resolve( + [ + { + type: "user", + id: "user-1", + text: "Approved task", + time: { created: 0 }, + }, + ], + selected, + effects, + ); + expect( + resolved.evidence.kind === "live" ? resolved.evidence.origin.id : null, + ).toBe("user-1"); scope.release(); f.close(); }); @@ -98,7 +126,7 @@ test("a superseded reservation cannot accept a receipt under the new lifetime", for (const invalidation of ["lifetime", "frontier", "unload"] as const) test(`held readiness scope wakes on ${invalidation} without settling admission`, async () => { const f = fixture(); - const scope = f.inputs.capture(context("user-1")); + const scope = f.inputs.openSource(context("user-1")); let settled = false; void f.admission.ready.then( () => { @@ -108,7 +136,7 @@ for (const invalidation of ["lifetime", "frontier", "unload"] as const) settled = true; }, ); - const wait = scope.ready(); + const wait = scope.scope.ready(); const caught = wait.catch((error) => String(error)); if (invalidation === "lifetime") f.inputs.invalidate(host); else if (invalidation === "frontier") f.inputs.reserve(host, "fresh"); @@ -122,7 +150,7 @@ for (const invalidation of ["lifetime", "frontier", "unload"] as const) test("pre-registration reservation blocks old consumed lineage in the same lifetime", () => { const f = fixture(); f.inputs.reserve(host, "fresh-before-get"); - expect(() => f.inputs.capture(context("user-1"))).toThrow( + expect(() => f.inputs.openSource(context("user-1"))).toThrow( "epoch was revoked", ); expect(() => @@ -135,16 +163,16 @@ test("pre-registration reservation blocks old consumed lineage in the same lifet }); test("a new frontier cancels old scope without rejecting a fresh exact ready", async () => { const f = fixture(); - const old = f.inputs.capture(context("user-1")); - const oldWait = old.ready().catch((error) => String(error)); + const old = f.inputs.openSource(context("user-1")); + const oldWait = old.scope.ready().catch((error) => String(error)); const reservation = f.inputs.reserve(host, "fresh"); const admission = f.inputs.register(reservation, { type: "synthetic", payload: { text: "Fresh" }, }); - const fresh = f.inputs.capture(context("fresh")); + const fresh = f.inputs.openSource(context("fresh")); let settled = false; - const ready = fresh.ready().then(() => { + const ready = fresh.scope.ready().then(() => { settled = true; }); expect(await oldWait).toBe("Error: Flow execution epoch was revoked."); @@ -163,12 +191,19 @@ test("a new frontier cancels old scope without rejecting a fresh exact ready", a f.inputs.makeOrigin(host, "fresh", delivery, false, reservation.epoch), ); await ready; - expect(f.inputs.positive(fresh)?.id).toBe("fresh"); + const resolved = await fresh.resolve( + [{ type: "synthetic", id: "fresh", text: "Fresh", time: { created: 0 } }], + selected, + effects, + ); + expect( + resolved.evidence.kind === "live" ? resolved.evidence.origin.id : null, + ).toBe("fresh"); fresh.release(); f.close(); }); -import type { SessionMessageInfo } from "@opencode/client"; +import type { SessionInfo, SessionMessageInfo } from "@opencode/client"; import { CompactionBoundary } from "../src/platform/opencode/auto-compaction.js"; async function compactionFixture() { @@ -196,6 +231,12 @@ async function compactionFixture() { const boundary = new CompactionBoundary({ inputs: f.inputs, readNative: async () => structuredClone(native), + readSelected: async () => selected, + captureReplacement: () => ({ + check: () => {}, + supersede: () => {}, + current: () => true, + }), failure: () => {}, hasActivePrimary: () => false, guidance: () => null, @@ -262,6 +303,12 @@ for (const corruption of [ } return snapshot; }, + readSelected: async () => selected, + captureReplacement: () => ({ + check: () => {}, + supersede: () => {}, + current: () => true, + }), failure: () => {}, hasActivePrimary: () => false, guidance: () => null, @@ -316,9 +363,10 @@ test("manual summary of unowned trailing history retains the ordinary local fron expect(() => f.boundary.completed(completedInput, f.native)).toThrow( "unknown, synthetic, or stale real input", ); - const scope = f.inputs.capture(completedInput); - expect(f.inputs.positive(scope)).toBeUndefined(); - expect(await f.inputs.evidence(scope, f.native, delivery)).toEqual({ + const scope = f.inputs.openSource(completedInput); + const resolved = await scope.resolve(f.native, selected, effects); + expect("origin" in resolved.evidence).toBe(false); + expect(resolved.evidence).toEqual({ kind: "retired-ordinary", }); scope.release(); @@ -379,6 +427,12 @@ for (const aliasDepth of [0, 2]) const boundary = new CompactionBoundary({ inputs, readNative: async () => native, + readSelected: async () => selected, + captureReplacement: () => ({ + check: () => {}, + supersede: () => {}, + current: () => true, + }), failure: () => {}, hasActivePrimary: () => false, guidance: () => null, @@ -420,9 +474,17 @@ for (const aliasDepth of [0, 2]) input.messages.push({ role: "user", id: "msg_unowned", content: [] }); await expect(boundary.admit(input)).rejects.toThrow("epoch was revoked"); await expect(boundary.admit(input)).rejects.toThrow("epoch was revoked"); - const scope = inputs.capture(input); - expect(inputs.positive(scope)).toBeUndefined(); - expect(await inputs.evidence(scope, native, delivery)).toEqual({ + const scope = inputs.openSource(input); + const resolved = await scope.resolve( + native.filter( + (message) => + message.type !== "compaction" || message.status === "completed", + ), + selected, + effects, + ); + expect("origin" in resolved.evidence).toBe(false); + expect(resolved.evidence).toEqual({ kind: "unregistered", lineage: "retired-automatic", }); @@ -433,24 +495,22 @@ for (const aliasDepth of [0, 2]) }); for (const state of ["pending", "rejected", "accepted"] as const) - test(`missing native input preserves ${state} frontier lineage without creating an origin`, async () => { + test(`missing native input preserves ${state} frontier without granting authority`, async () => { const f = fixture(); try { if (state === "accepted") await f.inputs.accept(f.admission, f.receipt, async () => f.origin()); if (state === "rejected") f.inputs.reject(f.admission, new Error("rejected receipt")); - const input = { + const operation = f.inputs.openSource({ ...context("foreign"), messages: [ ...context("user-1").messages, ...context("foreign").messages, ], - }; - const scope = f.inputs.capture(input); - expect( - await f.inputs.evidence( - scope, + }); + await expect( + operation.resolve( [ { type: "synthetic", @@ -459,34 +519,666 @@ for (const state of ["pending", "rejected", "accepted"] as const) time: { created: 1 }, }, ], - delivery, + selected, + effects, ), - ).toEqual({ - kind: "unregistered", - lineage: state === "accepted" ? "retired-ordinary" : "rejected", + ).rejects.toThrow("native inputs differ"); + operation.release(); + expect(f.admission.state).toBe(state === "pending" ? "pending" : state); + if (state === "accepted") { + const old = f.inputs.openSource(context("user-1")); + const resolved = await old.resolve( + [ + { + type: "user", + id: "user-1", + text: "Approved task", + time: { created: 0 }, + }, + ], + selected, + effects, + ); + expect(resolved.evidence.kind).toBe("live"); + old.release(); + } + } finally { + f.close(); + } + }); + +import type { OpenCodeEvent } from "@opencode/client"; +import { SessionMessage } from "@opencode/schema/session-message"; + +type InboxEvent = Extract< + OpenCodeEvent, + { + type: + | "session.inbox.enqueued" + | "session.inbox.cancelled" + | "session.inbox.delivered" + | "session.inbox.delivery.changed"; + } +>; +function publicLedger(observe?: (id: string) => Promise) { + const intents: string[] = []; + const inputs = new InputAdmissions({ + assertOpen: () => {}, + authority: () => null, + authorityCurrent: () => true, + observeReal: async (_host, id) => { + intents.push(id); + await observe?.(id); + }, + }); + const prepare = ( + id: string, + text = id, + delivery: "queue" | "steer" = "queue", + ) => + inputs.observePrompt( + { + sessionID: host, + messageID: SessionMessage.ID.make(id), + prompt: { text }, + delivery, + }, + () => inputs.invalidate(host), + ); + const message = (id: string, text = id): SessionMessageInfo => ({ + type: "user", + id, + text, + time: { created: 1 }, + }); + const input = (ids: string[]) => ({ + ...context(ids.at(-1) ?? "empty"), + messages: ids.map((id) => ({ + id, + role: "user" as const, + content: [{ type: "text" as const, text: id }], + })), + }); + const consume = async ( + ids: string[], + native = ids.map((id) => message(id)), + ) => { + const operation = inputs.openSource(input(ids)); + try { + return await operation.resolve(native, selected, effects); + } finally { + operation.release(); + } + }; + const event = ( + type: InboxEvent["type"], + id: string, + seq: number, + delivery: "queue" | "steer" = "queue", + aggregateID = host, + ): InboxEvent => { + const base = { + id: `evt_${seq}`, + created: seq, + durable: { aggregateID, seq, version: 1 as const }, + }; + if (type === "session.inbox.enqueued") + return { + ...base, + type, + data: { + sessionID: host, + inboxID: id, + item: { type: "user", payload: { text: id }, delivery }, + }, + }; + if (type === "session.inbox.delivery.changed") + return { + ...base, + type, + data: { sessionID: host, inboxID: id, delivery }, + }; + return { ...base, type, data: { sessionID: host, inboxID: id } }; + }; + const reduce = (e: InboxEvent) => + inputs.observeInbox(e, (h) => inputs.invalidate(h)); + return { inputs, intents, prepare, message, input, consume, event, reduce }; +} +test("duplicate queue hooks, repeated contexts and late negatives observe one real intent", async () => { + const f = publicLedger(); + try { + f.prepare("msg_O"); + const original = await f.consume(["msg_O"]); + f.prepare("msg_A"); + f.prepare("msg_A"); + expect(() => f.prepare("msg_A", "conflicting payload")).toThrow( + "changed its payload", + ); + expect(() => original.scope.assertCurrent()).not.toThrow(); + f.reduce(f.event("session.inbox.enqueued", "msg_A", 1)); + const first = await f.consume(["msg_O", "msg_A"]), + again = await f.consume(["msg_O", "msg_A"]); + expect(first.evidence.kind === "live" ? first.evidence.origin : null).toBe( + again.evidence.kind === "live" ? again.evidence.origin : null, + ); + expect(f.intents).toEqual(["msg_O", "msg_A"]); + for (const type of [ + "session.inbox.cancelled", + "session.inbox.delivered", + "session.inbox.delivery.changed", + "session.inbox.enqueued", + ] as const) + f.reduce(f.event(type, "msg_A", 2 + f.intents.length, "steer")); + expect(f.inputs.automaticBlock(host)).toBeNull(); + expect(() => first.scope.assertCurrent()).not.toThrow(); + expect(() => original.scope.assertCurrent()).toThrow("revoked"); + } finally { + f.inputs.clear(); + } +}); +test("hook-only failed preparation neither replaces O nor vetoes local automatic scheduling", async () => { + const f = publicLedger(); + try { + f.prepare("msg_O"); + const old = await f.consume(["msg_O"]); + f.prepare("msg_A"); + expect(f.inputs.automaticBlock(host)).toBeNull(); + expect(() => old.scope.assertCurrent()).not.toThrow(); + f.prepare("msg_A"); + const next = await f.consume(["msg_O", "msg_A"]); + expect(next.participation?.kind).toBe("flow"); + expect(f.intents).toEqual(["msg_O", "msg_A"]); + } finally { + f.inputs.clear(); + } +}); +for (const mismatch of [ + "payload", + "session", + "agent", + "provider", + "model", + "variant", + "prefix", +] as const) + test(`rejected deferred ${mismatch} preserves O and releases the operation veto`, async () => { + const f = publicLedger(); + try { + f.prepare("msg_O"); + const old = await f.consume(["msg_O"]); + f.prepare("msg_A"); + const operation = f.inputs.openSource(f.input(["msg_O", "msg_A"])); + const session = structuredClone(selected), + native = [ + f.message("msg_O"), + f.message("msg_A", mismatch === "payload" ? "changed" : "msg_A"), + ]; + if (mismatch === "session") session.id = "wrong"; + if (mismatch === "agent") session.agent = Agent.ID.make("wrong"); + if (mismatch === "provider") + session.model = { ...model, providerID: Provider.ID.make("wrong") }; + if (mismatch === "model") + session.model = { ...model, id: Model.ID.make("wrong") }; + if (mismatch === "variant") + session.model = { ...model, variant: Model.VariantID.make("wrong") }; + if (mismatch === "prefix") native.shift(); + expect(f.inputs.automaticBlock(host)).toBe("input-settlement"); + await expect( + operation.resolve(native, session, effects), + ).rejects.toThrow(); + expect(operation.fail(() => true)).toBe(false); + operation.release(); + expect(f.inputs.automaticBlock(host)).toBeNull(); + expect(() => old.scope.assertCurrent()).not.toThrow(); + expect(f.intents).toEqual(["msg_O"]); + expect((await f.consume(["msg_O", "msg_A"])).participation?.kind).toBe( + "flow", + ); + } finally { + f.inputs.clear(); + } + }); +test("one batch consumes new real inputs in native order and grants only its exact final source", async () => { + const f = publicLedger(); + try { + f.prepare("msg_O"); + const old = await f.consume(["msg_O"]); + for (const id of ["msg_B", "msg_S2", "msg_A", "msg_S1"]) f.prepare(id); + f.reduce(f.event("session.inbox.enqueued", "msg_B", 1)); + const next = await f.consume(["msg_O", "msg_A", "msg_S1", "msg_S2"]); + expect(f.intents).toEqual(["msg_O", "msg_A", "msg_S1", "msg_S2"]); + expect(next.evidence.kind === "live" ? next.evidence.origin.id : null).toBe( + "msg_S2", + ); + expect(() => old.scope.assertCurrent()).toThrow("revoked"); + expect(f.inputs.automaticBlock(host)).toBe("pending-user"); + expect( + (await f.consume(["msg_O", "msg_A", "msg_S1", "msg_S2"])).evidence, + ).toBeDefined(); + expect(f.intents).toEqual(["msg_O", "msg_A", "msg_S1", "msg_S2"]); + } finally { + f.inputs.clear(); + } +}); +test("an intermediate registered source cannot donate its Flow provenance to an unknown suffix", async () => { + const f = publicLedger(); + try { + f.prepare("msg_O"); + await f.consume(["msg_O"]); + f.prepare("msg_A"); + const next = await f.consume(["msg_O", "msg_A", "msg_U"]); + expect(next.participation).toEqual({ kind: "native-only" }); + expect(next.evidence).toEqual({ + kind: "unregistered", + lineage: "retired-ordinary", + }); + expect(f.intents).toEqual(["msg_O", "msg_A"]); + } finally { + f.inputs.clear(); + } +}); +test("a consumed public batch cannot rebase a private suffix or replay its activation", async () => { + const f = publicLedger(); + try { + f.prepare("msg_O"); + const old = await f.consume(["msg_O"]); + f.prepare("msg_A"); + const reservation = f.inputs.reserve(host, "msg_P"), + admission = f.inputs.register(reservation, { + type: "synthetic", + payload: { text: "private" }, }); - expect(f.inputs.positive(scope)).toBeUndefined(); - scope.release(); - const old = f.inputs.capture(context("user-1")); - if (state === "accepted") - expect( - ( - await f.inputs.evidence( - old, - [ - { - type: "user", - id: "user-1", - text: "Approved task", - time: { created: 0 }, - }, - ], - delivery, - ) - ).kind, - ).toBe("live"); - old.release(); + let activations = 0; + await f.inputs.accept( + admission, + { + id: "msg_P", + sessionID: host, + type: "synthetic", + payload: { text: "private" }, + delivery: "queue", + time: { created: 1 }, + }, + async () => { + activations++; + return f.inputs.makeOrigin( + host, + "msg_P", + delivery, + false, + reservation.epoch, + ); + }, + ); + const barrier = f.inputs.captureBarrier(host), + operation = f.inputs.openSource(f.input(["msg_O", "msg_A", "msg_P"])); + await expect( + operation.resolve( + [ + f.message("msg_O"), + f.message("msg_A"), + { + type: "synthetic", + id: "msg_P", + text: "private", + time: { created: 1 }, + }, + ], + selected, + effects, + ), + ).rejects.toThrow("cannot rebase"); + expect(operation.fail(() => true)).toBe(false); + operation.release(); + expect(() => barrier.assertCurrent()).not.toThrow(); + expect(f.intents).toEqual(["msg_O"]); + expect(activations).toBe(1); + expect(() => old.scope.assertCurrent()).toThrow("revoked"); + } finally { + f.inputs.clear(); + } +}); +test("cancelled ID reuse needs a later hook and a matching observed enqueue generation", async () => { + const f = publicLedger(); + try { + f.prepare("msg_O"); + const old = await f.consume(["msg_O"]); + f.prepare("msg_A"); + f.reduce(f.event("session.inbox.enqueued", "msg_A", 10)); + expect(f.reduce(f.event("session.inbox.cancelled", "msg_A", 11))).toEqual({ + retryHost: host, + }); + f.reduce(f.event("session.inbox.enqueued", "msg_A", 20)); + f.reduce(f.event("session.inbox.cancelled", "msg_A", 11)); + f.reduce(f.event("session.inbox.delivered", "msg_A", 12)); + expect(f.inputs.automaticBlock(host)).toBe("pending-user"); + await expect(f.consume(["msg_O", "msg_A"])).rejects.toThrow( + "matching new hook and enqueue", + ); + expect(() => old.scope.assertCurrent()).not.toThrow(); + f.prepare("msg_A"); + const applied = await f.consume(["msg_O", "msg_A"]); + f.reduce(f.event("session.inbox.cancelled", "msg_A", 30)); + expect(applied.participation?.kind).toBe("flow"); + expect(f.inputs.automaticBlock(host)).toBeNull(); + expect(f.intents).toEqual(["msg_O", "msg_A"]); + } finally { + f.inputs.clear(); + } +}); +test("cancellation removes only its matching blocker and a confirmed steer change revokes once", async () => { + const f = publicLedger(); + try { + f.prepare("msg_O"); + const old = await f.consume(["msg_O"]); + f.prepare("msg_A"); + f.prepare("msg_B"); + f.reduce(f.event("session.inbox.enqueued", "msg_A", 1)); + f.reduce(f.event("session.inbox.enqueued", "msg_B", 2)); + const other = Session.ID.make("session-other"); + expect( + f.reduce(f.event("session.inbox.cancelled", "msg_A", 3, "queue", other)), + ).toEqual({ retryHost: null }); + expect(f.inputs.automaticBlock(host)).toBe("pending-user"); + f.reduce(f.event("session.inbox.cancelled", "msg_A", 4)); + expect(f.inputs.automaticBlock(host)).toBe("pending-user"); + f.reduce(f.event("session.inbox.delivery.changed", "msg_B", 5, "steer")); + const epoch = f.inputs.epoch(host); + expect(() => old.scope.assertCurrent()).toThrow("revoked"); + f.reduce(f.event("session.inbox.delivery.changed", "msg_B", 6, "steer")); + f.reduce(f.event("session.inbox.delivery.changed", "msg_B", 7, "queue")); + expect(f.inputs.epoch(host)).toBe(epoch); + expect(f.intents).toEqual(["msg_O"]); + const applied = await f.consume(["msg_O", "msg_B"]); + f.reduce(f.event("session.inbox.delivery.changed", "msg_B", 8, "steer")); + expect(() => applied.scope.assertCurrent()).not.toThrow(); + expect(f.intents).toEqual(["msg_O", "msg_B"]); + } finally { + f.inputs.clear(); + } +}); +test("host deletion retains its epoch tombstone while another host remains usable", async () => { + const f = publicLedger(); + try { + f.prepare("msg_O"); + await f.consume(["msg_O"]); + const other = Session.ID.make("session-other"), + reservation = f.inputs.reserve(other, "msg_P"); + f.inputs.invalidate(host); + const epoch = f.inputs.epoch(host); + f.inputs.deleteHost(host); + expect(f.inputs.epoch(host)).toBe(epoch); + expect(() => f.prepare("msg_late")).toThrow("revoked"); + expect(() => f.inputs.assertReservation(reservation)).not.toThrow(); + f.reduce(f.event("session.inbox.enqueued", "msg_late", 99)); + expect(f.inputs.automaticBlock(host)).toBeNull(); + } finally { + f.inputs.clear(); + } +}); + +for (const changed of ["text", "metadata", "file", "agent", "skill"] as const) + test(`duplicate queued registration rejects changed ${changed} before any intent`, () => { + const f = publicLedger(); + try { + const original = { + sessionID: host, + messageID: SessionMessage.ID.make("msg_duplicate"), + delivery: "queue" as const, + prompt: { + text: "task", + files: [{ uri: "file:///tmp/file", name: "one" }], + agents: [{ name: "build" }], + skills: [{ id: Skill.ID.make("skill") }], + }, + metadata: { key: "original" }, + }; + f.inputs.observePrompt(original, () => f.inputs.invalidate(host)); + const next = structuredClone(original); + if (changed === "text") next.prompt.text = "changed"; + if (changed === "metadata") next.metadata.key = "changed"; + if (changed === "file" && next.prompt.files[0]) + next.prompt.files[0].name = "changed"; + if (changed === "agent" && next.prompt.agents[0]) + next.prompt.agents[0].name = "changed"; + if (changed === "skill" && next.prompt.skills[0]) + next.prompt.skills[0].id = Skill.ID.make("changed"); + expect(() => + f.inputs.observePrompt(next, () => f.inputs.invalidate(host)), + ).toThrow("changed its payload"); + expect(f.intents).toEqual([]); + expect(f.inputs.epoch(host)).toBe(0); + expect(f.inputs.automaticBlock(host)).toBeNull(); } finally { + f.inputs.clear(); + } + }); + +test("a consumed batch settles intermediate steer history without rebasing its current frontier", async () => { + const f = publicLedger(); + try { + f.prepare("msg_O"); + await f.consume(["msg_O"]); + f.prepare("msg_A"); + f.prepare("msg_S1", "msg_S1", "steer"); + f.prepare("msg_S2", "msg_S2", "steer"); + f.prepare("msg_B"); + f.reduce(f.event("session.inbox.enqueued", "msg_B", 1)); + const epoch = f.inputs.epoch(host), + next = await f.consume(["msg_O", "msg_A", "msg_S1", "msg_S2"]); + expect(f.inputs.epoch(host)).toBe(epoch); + expect(f.intents).toEqual(["msg_O", "msg_A", "msg_S1", "msg_S2"]); + expect(next.evidence.kind === "live" ? next.evidence.origin.id : null).toBe( + "msg_S2", + ); + await expect(f.consume(["msg_O", "msg_A"])).rejects.toThrow("revoked"); + expect(f.inputs.automaticBlock(host)).toBe("pending-user"); + expect(f.intents).toEqual(["msg_O", "msg_A", "msg_S1", "msg_S2"]); + } finally { + f.inputs.clear(); + } +}); +test("native-only consumption settles an event observation without creating a Flow origin", async () => { + const f = publicLedger(); + try { + f.prepare("msg_O"); + await f.consume(["msg_O"]); + f.reduce(f.event("session.inbox.enqueued", "msg_unknown", 1)); + expect(f.inputs.automaticBlock(host)).toBe("pending-user"); + const next = await f.consume(["msg_O", "msg_unknown"]); + expect(next.participation).toEqual({ kind: "native-only" }); + f.reduce(f.event("session.inbox.enqueued", "msg_unknown", 2)); + expect(f.inputs.automaticBlock(host)).toBeNull(); + expect(f.intents).toEqual(["msg_O"]); + } finally { + f.inputs.clear(); + } +}); + +for (const conflict of [ + "user", + "registered-user", + "private", + "reserved", + "alias", +] as const) + test(`checkpoint observation transfer preserves actual ${conflict} identity conflicts`, async () => { + const f = await compactionFixture(); + try { + const id = "msg_checkpoint_conflict"; + if (conflict === "registered-user") + f.inputs.observePrompt( + { + sessionID: host, + messageID: SessionMessage.ID.make(id), + prompt: { text: "registered" }, + delivery: "queue", + }, + () => {}, + ); + if (conflict === "user") + f.inputs.observeInbox( + { + id: "evt_user", + created: 1, + type: "session.inbox.enqueued", + durable: { aggregateID: host, seq: 1, version: 1 }, + data: { + sessionID: host, + inboxID: id, + item: { + type: "user", + payload: { text: "real pending" }, + delivery: "queue", + }, + }, + }, + () => {}, + ); + if (conflict === "private" || conflict === "reserved") { + const reservation = f.inputs.reserve(host, id); + if (conflict === "private") + f.inputs.register(reservation, { + type: "synthetic", + payload: { text: "private" }, + }); + } + if (conflict === "alias") { + const operation = f.inputs.openSource(context("user-1")); + try { + const resolved = await operation.resolve( + f.native.filter((message) => message.type !== "compaction"), + selected, + effects, + ); + f.inputs.checkpoint(resolved.scope, id, null); + } finally { + operation.release(); + } + } + expect(() => f.inputs.assertCheckpointAvailable(host, id)).toThrow( + "identity conflicts", + ); + } finally { + f.close(); + } + }); +for (const changed of ["user-type", "enqueue-generation"] as const) + test(`checkpoint final installation refuses changed observed ${changed}`, async () => { + const f = await compactionFixture(), + id = "msg_control_transfer"; + const operation = f.inputs.openSource(context("user-1")); + try { + const resolved = await operation.resolve( + f.native.filter((message) => message.type !== "compaction"), + selected, + effects, + ); + f.inputs.observeInbox( + { + id: "evt_compact1", + created: 1, + type: "session.inbox.enqueued", + durable: { aggregateID: host, seq: 1, version: 1 }, + data: { + sessionID: host, + inboxID: id, + item: { type: "compaction", payload: {}, delivery: "queue" }, + }, + }, + () => {}, + ); + const transfer = f.inputs.assertCheckpointAvailable(host, id); + await Promise.resolve(); + const base = { + id: "evt_compact2", + created: 2, + durable: { aggregateID: host, seq: 2, version: 1 as const }, + }; + if (changed === "user-type") + f.inputs.observeInbox( + { + ...base, + type: "session.inbox.enqueued", + data: { + sessionID: host, + inboxID: id, + item: { + type: "user", + payload: { text: "new real" }, + delivery: "queue", + }, + }, + }, + () => {}, + ); + else + f.inputs.observeInbox( + { + ...base, + type: "session.inbox.enqueued", + data: { + sessionID: host, + inboxID: id, + item: { type: "compaction", payload: {}, delivery: "queue" }, + }, + }, + () => {}, + ); + expect(() => + f.inputs.checkpoint(resolved.scope, id, null, transfer), + ).toThrow("identity conflicts"); + expect(() => resolved.scope.assertCurrent()).not.toThrow(); + expect(() => f.inputs.openSource(context(id))).toThrow("revoked"); + } finally { + operation.release(); f.close(); } }); + +test("a matching delivered compaction observation transfers at final alias installation", async () => { + const f = await compactionFixture(), + id = "msg_matching_transfer"; + const operation = f.inputs.openSource(context("user-1")); + try { + const resolved = await operation.resolve( + f.native.filter((message) => message.type !== "compaction"), + selected, + effects, + ); + f.inputs.observeInbox( + { + id: "evt_matching_enqueued", + created: 1, + type: "session.inbox.enqueued", + durable: { aggregateID: host, seq: 1, version: 1 }, + data: { + sessionID: host, + inboxID: id, + item: { type: "compaction", payload: {}, delivery: "queue" }, + }, + }, + () => {}, + ); + const transfer = f.inputs.assertCheckpointAvailable(host, id); + await Promise.resolve(); + f.inputs.observeInbox( + { + id: "evt_matching_delivered", + created: 2, + type: "session.inbox.delivered", + durable: { aggregateID: host, seq: 2, version: 1 }, + data: { sessionID: host, inboxID: id }, + }, + () => {}, + ); + f.inputs.checkpoint(resolved.scope, id, null, transfer); + expect(() => f.inputs.assertCheckpointAvailable(host, id)).toThrow( + "identity conflicts", + ); + expect(() => f.inputs.openSource(context(id))).not.toThrow(); + } finally { + operation.release(); + f.close(); + } +}); diff --git a/tests/lifecycle.test.ts b/tests/lifecycle.test.ts index d1e5de8f..cbd6334a 100644 --- a/tests/lifecycle.test.ts +++ b/tests/lifecycle.test.ts @@ -104,6 +104,16 @@ async function fixture( const recovery = new RecoveryController({ assess: async () => ({ kind: "unavailable", reason: "test" }), }); + let dispatchReturnGate: { + entered: () => void; + promise: Promise; + } | null = null; + const automaticAccepted = Promise.withResolvers(); + let projectionReads = 0; + let projectionGate: { entered: () => void; promise: Promise } | null = + null; + let digestGate: { entered: () => void; promise: Promise } | null = null; + const sourceDigest = `sha256:${"a".repeat(64)}` as const; let admissionBlocked: (() => void) | null = null; let admissionGate: Promise | null = null; let corruptReceipt = false; @@ -209,7 +219,7 @@ async function fixture( ...(input.metadata === undefined ? {} : { metadata: structuredClone(input.metadata) }), - delivery: input.delivery ?? "queue", + delivery: input.delivery ?? "steer", }); const receipt: SessionInboxUser = { id, @@ -222,7 +232,7 @@ async function fixture( : { metadata: structuredClone(input.metadata) }), }, time: { created: 0 }, - delivery: input.delivery ?? "queue", + delivery: input.delivery ?? "steer", }; native.push({ type: "user", @@ -269,7 +279,7 @@ async function fixture( : { metadata: { ...input.metadata } }), }, time: { created: 0 }, - delivery: input.delivery ?? "queue", + delivery: input.delivery ?? "steer", }; }, }, @@ -336,8 +346,37 @@ async function fixture( let lifecycle: Awaited>; const autoDrive = new AutoDriveCoordinator({ recovery, - readProjection: async () => projection, - prompt: (...args) => lifecycle.prompt(...args), + readProjection: async () => { + projectionReads++; + const snapshot = structuredClone(projection), + gate = projectionGate; + projectionGate = null; + if (gate) { + gate.entered(); + await gate.promise; + } + return snapshot; + }, + readSourceDigest: async () => { + const gate = digestGate; + digestGate = null; + if (gate) { + gate.entered(); + await gate.promise; + } + return sourceDigest; + }, + prompt: async (...args) => { + const result = await lifecycle.prompt(...args), + gate = dispatchReturnGate; + dispatchReturnGate = null; + if (gate) { + gate.entered(); + await gate.promise; + } + if (result.kind === "sent") automaticAccepted.resolve(result.source); + return result; + }, createToken: () => "local-token", }); lifecycle = await registerFlowLifecycle({ @@ -390,6 +429,8 @@ async function fixture( }); return { lifecycle, + ctxSession: ctx.session, + ctxSessionContext: ctx.session.context.bind(ctx.session), autoDrive, recovery, repository, @@ -405,6 +446,33 @@ async function fixture( emit, context, toolCallbacks, + waitAutomaticSource: () => automaticAccepted.promise, + projectionReadCount: () => projectionReads, + blockNextDispatchReturn() { + const entered = Promise.withResolvers(), + release = Promise.withResolvers(); + dispatchReturnGate = { + entered: entered.resolve, + promise: release.promise, + }; + return { + entered: entered.promise, + release: release.resolve, + }; + }, + sourceDigest, + blockPolicyRead(stage: "projection" | "digest") { + const entered = Promise.withResolvers(), + release = Promise.withResolvers(); + const gate = { entered: entered.resolve, promise: release.promise }; + if (stage === "projection") projectionGate = gate; + else digestGate = gate; + return { + entered: entered.promise, + release: release.resolve, + reject: release.reject, + }; + }, setProjection(value: AutoDriveProjection) { projection = value; }, @@ -473,6 +541,7 @@ async function fixture( push(event); } else queuedEvents.push(event); await handled.promise; + for (let i = 0; i < 20; i++) await Promise.resolve(); }, sendEvent(event: OpenCodeEvent) { if (pushEvent) { @@ -3419,7 +3488,7 @@ for (const fresh of ["automatic", "ordinary"] as const) }); for (const actor of ["command", "synthetic"] as const) - test(`reservation before ${actor} session read vetoes old callbacks without taking new authority`, async () => { + test(`${actor} selected read respects its input reservation boundary`, async () => { const f = await fixture(); try { await initialOwnershipWork(f); @@ -3436,12 +3505,18 @@ for (const actor of ["command", "synthetic"] as const) ); await f.flush(); const before = f.autoDrive.captureAuthority(sessionID); - await expect(f.emit("context", old)).rejects.toThrow("epoch was revoked"); + if (actor === "command") + await expect(f.emit("context", old)).rejects.toThrow( + "epoch was revoked", + ); + else await finishOwnedResponse(f, await startOwnedRequest(f)); const control = automaticControl("msg_pre_get_old"); f.native.push(control); - await expect(f.emit("compaction", old)).rejects.toThrow( - "epoch was revoked", - ); + if (actor === "command") + await expect(f.emit("compaction", old)).rejects.toThrow( + "epoch was revoked", + ); + else await f.emit("compaction", old); expect(f.autoDrive.captureAuthority(sessionID)).toEqual(before); expect(f.repository.saveCount).toBe(0); f.native.splice(f.native.indexOf(control), 1); @@ -5048,3 +5123,1011 @@ test("queued public inputs consume A before pending B without idle", async () => await f.lifecycle.dispose(); } }); + +function inboxEvent( + type: "session.inbox.enqueued" | "session.inbox.cancelled", + id: string, + seq: number, +): Extract< + OpenCodeEvent, + { type: "session.inbox.enqueued" | "session.inbox.cancelled" } +> { + const base = { + id: `evt_queue_${seq}`, + created: seq, + durable: { aggregateID: sessionID, seq, version: 1 as const }, + }; + return type === "session.inbox.enqueued" + ? { + ...base, + type, + data: { + sessionID, + inboxID: id, + item: { type: "user", payload: { text: id }, delivery: "queue" }, + }, + } + : { ...base, type, data: { sessionID, inboxID: id } }; +} +async function queuePublic(f: OwnershipFixture, id: string, seq?: number) { + await f.emit("prompt", { + sessionID, + messageID: SessionMessage.ID.make(id), + prompt: { text: id }, + delivery: "queue", + }); + if (seq !== undefined) + await f.sendEventAndWait(inboxEvent("session.inbox.enqueued", id, seq)); +} +function continuationProjection(f: OwnershipFixture) { + const source = f.context().messages.at(-1)?.id; + if (!source) throw new Error("Missing original input."); + f.autoDrive.observeAssistant(sessionID, "msg_validation_boundary", source); + const origin = f.autoDrive.validationOrigin( + sessionID, + "msg_validation_boundary", + { + featureId: "feature", + runId: "run", + sourceDigest: f.sourceDigest, + command: "bun test", + scope: "focused", + hostPlatform: "linux", + intent: "pass", + assertions: [], + }, + ); + if (!origin) throw new Error("Missing exact validation origin."); + f.autoDrive.observeValidation({ + origin, + captureId: "validation", + observation: { + id: "validation", + featureId: "feature", + runId: "run", + sourceDigest: f.sourceDigest, + outputDigest: f.sourceDigest, + command: "bun test", + scope: "focused", + intent: "pass", + hostPlatform: "linux", + outputComplete: true, + exitCode: 0, + recordedRevision: 5, + }, + }); + f.setProjection({ + sessionId: "flow-1", + status: "running", + revision: 5, + nextAction: "flow_review_start", + }); + return source; +} +for (const stage of ["projection", "digest", "selected"] as const) + test(`confirmed pending queue during ${stage} withholds synthetic admission without consuming the continuation`, async () => { + const f = await fixture(); + let release = () => {}; + try { + await initialOwnershipWork(f); + const guard = await f.lifecycle.verifyTool( + ownershipNativeCall(f), + "flow", + ); + if (guard.kind !== "flow") throw new Error("Missing original guard."); + const source = continuationProjection(f); + let entered: Promise; + if (stage === "selected") { + f.blockNextSessionRead(); + entered = f.waitSessionRead(); + release = f.releaseSessionRead; + } else { + const gate = f.blockPolicyRead(stage); + entered = gate.entered; + release = gate.release; + } + const idle = f.autoDrive.onIdle(sessionID); + await entered; + await queuePublic(f, "msg_queued_A", 100); + release(); + await idle; + expect( + f.native.filter((message) => message.type === "synthetic"), + ).toEqual([]); + expect(() => guard.assertCurrent()).not.toThrow(); + const authority = f.autoDrive.captureAuthority(sessionID); + if (!authority) + throw new Error("Withholding consumed the automatic lease."); + expect( + f.autoDrive.captureDispatch(sessionID, authority.token)?.source, + ).toBe(source); + await f.sendEventAndWait( + inboxEvent("session.inbox.cancelled", "msg_queued_A", 101), + ); + await f.autoDrive.onIdle(sessionID); + const synthetic = f.native.filter( + (message) => message.type === "synthetic", + ); + expect(synthetic).toHaveLength(1); + expect( + f.autoDrive.captureDispatch(sessionID, authority.token)?.source, + ).toBe(synthetic[0]?.id); + } finally { + release(); + f.releaseSessionRead(); + await f.lifecycle.dispose(); + } + }); +test("hook-only preparation still permits an authenticated automatic continuation", async () => { + const f = await fixture(); + try { + await initialOwnershipWork(f); + continuationProjection(f); + await queuePublic(f, "msg_failed_prepare"); + await f.autoDrive.onIdle(sessionID); + const synthetic = f.native.filter( + (message) => message.type === "synthetic", + ); + expect(synthetic).toHaveLength(1); + const authority = f.autoDrive.captureAuthority(sessionID); + expect( + authority && + f.autoDrive.captureDispatch(sessionID, authority.token)?.source, + ).toBe(synthetic[0]?.id); + } finally { + await f.lifecycle.dispose(); + } +}); +test("a real activation keeps its busy token after handoff and late cleanup cannot touch steer", async () => { + const f = await fixture(), + entered = Promise.withResolvers(), + release = Promise.withResolvers(); + try { + await initialOwnershipWork(f); + const observe = f.autoDrive.observeMessage.bind(f.autoDrive); + f.autoDrive.observeMessage = async (...args) => { + const result = await observe(...args); + if (args[3] === "msg_A") { + entered.resolve(); + await release.promise; + } + return result; + }; + await queuePublic(f, "msg_A"); + f.native.push({ + type: "user", + id: "msg_A", + text: "msg_A", + time: { created: 20 }, + }); + const pending = f.emit("context", f.context()).catch(String); + await entered.promise; + await expect(f.emit("context", f.context())).rejects.toThrow( + "callbacks overlap", + ); + await f.emit("prompt", { + sessionID, + messageID: SessionMessage.ID.make("msg_steer"), + prompt: { text: "msg_steer" }, + delivery: "steer", + }); + f.native.push({ + type: "user", + id: "msg_steer", + text: "msg_steer", + time: { created: 21 }, + }); + expect(await pending).toContain("epoch was revoked"); + const request = await startOwnedRequest(f); + const guard = await f.lifecycle.verifyTool(ownershipNativeCall(f), "flow"); + release.resolve(); + await f.flush(); + if (guard.kind !== "flow") throw new Error("Missing fresh steer guard."); + expect(() => guard.assertCurrent()).not.toThrow(); + await finishOwnedResponse(f, request); + } finally { + release.resolve(); + await f.lifecycle.dispose(); + } +}); +test("nested idle during first real settlement withholds promptly and finishes the new origin", async () => { + const f = await fixture(); + try { + f.setProjection({ + status: "idle", + revision: 0, + nextAction: "flow_plan_save", + }); + const metadata = await f.autoDrive.activate(sessionID); + await f.autoDrive.observeMessage( + sessionID, + delivery, + [{ synthetic: true, metadata }], + "msg_O", + ); + await queuePublic(f, "msg_A"); + f.native.push({ + type: "user", + id: "msg_A", + text: "msg_A", + time: { created: 1 }, + }); + const request = await startOwnedRequest(f); + expect(f.native.filter((message) => message.type === "synthetic")).toEqual( + [], + ); + expect( + f.autoDrive.captureDispatch( + sessionID, + f.autoDrive.captureAuthority(sessionID)?.token ?? "", + )?.source, + ).toBe("msg_A"); + const guard = await f.lifecycle.verifyTool(ownershipNativeCall(f), "flow"); + expect(guard.kind).toBe("flow"); + await finishOwnedResponse(f, request); + } finally { + await f.lifecycle.dispose(); + } +}); +test("registered queue compacts before its first primary and resumes with Flow provenance", async () => { + const f = await fixture(); + try { + await initialOwnershipWork(f); + const old = await f.lifecycle.verifyTool(ownershipNativeCall(f), "flow"); + if (old.kind !== "flow") throw new Error("Missing predecessor."); + await queuePublic(f, "msg_A"); + f.native.push({ + type: "user", + id: "msg_A", + text: "msg_A", + time: { created: 20 }, + }); + await compactCurrentInput(f, "queued_checkpoint"); + expect(() => old.assertCurrent()).toThrow(); + const request = await startOwnedRequest(f); + expect( + (await f.lifecycle.verifyTool(ownershipNativeCall(f), "flow")).kind, + ).toBe("flow"); + await finishOwnedResponse(f, request); + } finally { + await f.lifecycle.dispose(); + } +}); + +function succeededIdle(f: OwnershipFixture, id = "evt_queue_terminal") { + const time = 30; + f.native.push({ + type: "idle", + id: SessionMessage.ID.fromEvent(id), + outcome: "succeeded", + time: { created: time }, + }); + f.session.outcome = "succeeded"; + f.session.time.idle = time; + const event: OpenCodeEvent = { + id, + type: "session.execution.succeeded", + created: time, + durable: { aggregateID: sessionID, seq: 30, version: 1 }, + data: { sessionID }, + }; + return event; +} +test("inbox cancellation is reduced during blocked terminal work and wakes one exact deferred ticket", async () => { + const f = await fixture(); + let release = () => {}; + try { + await initialOwnershipWork(f); + continuationProjection(f); + await queuePublic(f, "msg_A", 100); + const gate = f.blockNextDispatchReturn(); + release = gate.release; + f.sendEvent(succeededIdle(f)); + await gate.entered; + await f.sendEventAndWait( + inboxEvent("session.inbox.cancelled", "msg_A", 101), + ); + expect(f.native.filter((message) => message.type === "synthetic")).toEqual( + [], + ); + release(); + const source = await f.waitAutomaticSource(); + expect( + f.native + .filter((message) => message.type === "synthetic") + .map((message) => message.id), + ).toEqual([source]); + await f.sendEventAndWait( + inboxEvent("session.inbox.cancelled", "msg_A", 101), + ); + expect( + f.native + .filter((message) => message.type === "synthetic") + .map((message) => message.id), + ).toEqual([source]); + } finally { + release(); + await f.lifecycle.dispose(); + } +}); +for (const change of [ + "consumption", + "primary", + "selected", + "idle-time", + "prefix", +] as const) + test(`deferred terminal retry refuses changed ${change} ownership`, async () => { + const f = await fixture(); + let release = () => {}; + try { + await initialOwnershipWork(f); + continuationProjection(f); + await queuePublic(f, "msg_A", 100); + const resumed = Promise.withResolvers(), + terminalFinished = Promise.withResolvers(); + const resume = f.autoDrive.resumeDeferred.bind(f.autoDrive), + onIdle = f.autoDrive.onIdle.bind(f.autoDrive); + const retry = spyOn(f.autoDrive, "resumeDeferred").mockImplementation( + async (...args) => { + try { + await resume(...args); + } finally { + resumed.resolve(); + } + }, + ); + const terminal = spyOn(f.autoDrive, "onIdle").mockImplementation( + async (...args) => { + try { + await onIdle(...args); + } finally { + terminalFinished.resolve(); + } + }, + ); + const gate = f.blockNextDispatchReturn(); + release = gate.release; + f.sendEvent(succeededIdle(f)); + await gate.entered; + if (change === "consumption") { + f.native.push({ + type: "user", + id: "msg_A", + text: "msg_A", + time: { created: 31 }, + }); + await f.emit("context", f.context()); + } + if (change === "primary") await f.emit("context", f.context()); + if (change === "selected") + f.session.model = { ...model, id: Model.ID.make("changed") }; + if (change === "idle-time") f.session.time.idle = 31; + if (change === "prefix") + f.native.push({ + type: "user", + id: "unknown", + text: "unknown", + time: { created: 31 }, + }); + await f.sendEventAndWait( + inboxEvent("session.inbox.cancelled", "msg_A", 101), + ); + release(); + if (change === "consumption") await terminalFinished.promise; + else await resumed.promise; + retry.mockRestore(); + terminal.mockRestore(); + expect( + f.native.filter((message) => message.type === "synthetic"), + ).toEqual([]); + } finally { + release(); + await f.lifecycle.dispose(); + } + }); +test("registered queue compaction rejects a final changed control before replacing O", async () => { + const f = await fixture(); + try { + await initialOwnershipWork(f); + const old = await f.lifecycle.verifyTool(ownershipNativeCall(f), "flow"); + if (old.kind !== "flow") throw new Error("Missing old guard."); + await queuePublic(f, "msg_A"); + f.native.push({ + type: "user", + id: "msg_A", + text: "msg_A", + time: { created: 20 }, + }); + const input = f.context(); + f.native.push(automaticControl("queued_running")); + const read = f.ctxSessionContext; + let reads = 0; + const observation = spyOn(f.ctxSession, "context").mockImplementation( + async (...args) => { + const native = await read(...args); + if (++reads === 3) { + const control = native.findLast( + (message) => message.type === "compaction", + ); + if (control?.type === "compaction") control.reason = "manual"; + } + return native; + }, + ); + try { + await expect(f.emit("compaction", input)).rejects.toThrow( + "control identity changed", + ); + expect(reads).toBe(3); + expect(() => old.assertCurrent()).not.toThrow(); + } finally { + observation.mockRestore(); + } + } finally { + await f.lifecycle.dispose(); + } +}); + +test("dispose cancels a held terminal policy read and drains its tracked job", async () => { + const f = await fixture(); + let release = () => {}; + try { + await initialOwnershipWork(f); + continuationProjection(f); + const gate = f.blockPolicyRead("projection"); + release = gate.release; + f.sendEvent(succeededIdle(f)); + await gate.entered; + await f.lifecycle.dispose(); + release(); + await f.flush(); + expect(f.native.filter((message) => message.type === "synthetic")).toEqual( + [], + ); + expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); + expect(f.registrations).toContain("context"); + } finally { + release(); + await f.lifecycle.dispose(); + } +}); +test("a corrupt synthetic receipt cannot activate a dispatch intent", async () => { + const f = await fixture(); + try { + await initialOwnershipWork(f); + continuationProjection(f); + const native = f.ctxSession.synthetic.bind(f.ctxSession); + const corrupt = spyOn(f.ctxSession, "synthetic").mockImplementation( + async (...args) => ({ + ...(await native(...args)), + id: "msg_wrong_receipt", + }), + ); + const observe = spyOn(f.autoDrive, "observeMessage"); + try { + await f.autoDrive.onIdle(sessionID); + expect(observe).not.toHaveBeenCalled(); + expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); + expect( + f.native.filter((message) => message.type === "synthetic"), + ).toHaveLength(1); + } finally { + corrupt.mockRestore(); + observe.mockRestore(); + } + } finally { + await f.lifecycle.dispose(); + } +}); + +test("terminal synthetic selected read cannot reserve over a same-epoch primary replacement", async () => { + const f = await fixture(), + entered = Promise.withResolvers(), + release = Promise.withResolvers(), + finished = Promise.withResolvers(); + try { + await initialOwnershipWork(f); + continuationProjection(f); + const before = f.autoDrive.captureAuthority(sessionID), + get = f.ctxSession.get.bind(f.ctxSession), + onIdle = f.autoDrive.onIdle.bind(f.autoDrive); + let reads = 0; + const selected = spyOn(f.ctxSession, "get").mockImplementation( + async (...args) => { + const snapshot = await get(...args); + if (++reads === 2) { + entered.resolve(); + await release.promise; + } + return snapshot; + }, + ); + const idle = spyOn(f.autoDrive, "onIdle").mockImplementation( + async (...args) => { + try { + await onIdle(...args); + } finally { + finished.resolve(); + } + }, + ); + try { + f.sendEvent(succeededIdle(f)); + await entered.promise; + const request = await startOwnedRequest(f), + access = await f.lifecycle.verifyTool(ownershipNativeCall(f), "flow"); + release.resolve(); + await finished.promise; + expect( + f.native.filter((message) => message.type === "synthetic"), + ).toEqual([]); + expect(f.autoDrive.captureAuthority(sessionID)).toEqual(before); + if (access.kind !== "flow") throw new Error("Missing replacement guard."); + expect(() => access.assertCurrent()).not.toThrow(); + await finishOwnedResponse(f, request); + } finally { + selected.mockRestore(); + idle.mockRestore(); + } + } finally { + release.resolve(); + await f.lifecycle.dispose(); + } +}); + +test("compaction rechecks its third control after the held selected-session read", async () => { + const f = await fixture(); + try { + await initialOwnershipWork(f); + const old = await f.lifecycle.verifyTool(ownershipNativeCall(f), "flow"); + if (old.kind !== "flow") throw new Error("Missing predecessor guard."); + await queuePublic(f, "msg_A"); + f.native.push({ + type: "user", + id: "msg_A", + text: "msg_A", + time: { created: 20 }, + }); + const input = f.context(); + const control: Extract = + automaticControl("queued_selected_control"); + f.native.push(control); + f.blockNextSessionRead(); + const pending = f.emit("compaction", input).catch(String); + await f.waitSessionRead(); + control.reason = "manual"; + f.releaseSessionRead(); + expect(await pending).toBe( + "Error: Flow automatic compaction control identity changed.", + ); + expect(() => old.assertCurrent()).not.toThrow(); + } finally { + f.releaseSessionRead(); + await f.lifecycle.dispose(); + } +}); + +for (const read of ["native", "selected"] as const) + test(`failed deferred queue ${read} read preserves the usable predecessor and permits exact retry`, async () => { + const f = await fixture(); + try { + await initialOwnershipWork(f); + const old = await f.lifecycle.verifyTool(ownershipNativeCall(f), "flow"), + authority = f.autoDrive.captureAuthority(sessionID); + if (old.kind !== "flow") throw new Error("Missing old guard."); + await queuePublic(f, "msg_A"); + f.native.push({ + type: "user", + id: "msg_A", + text: "msg_A", + time: { created: 20 }, + }); + if (read === "native") f.rejectNextContextRead(); + else f.rejectNextSessionRead(); + await expect(f.emit("context", f.context())).rejects.toThrow( + read === "native" + ? "Native context snapshot failed." + : "Native session snapshot failed.", + ); + expect(() => old.assertCurrent()).not.toThrow(); + expect(f.autoDrive.captureAuthority(sessionID)).toEqual(authority); + const request = await startOwnedRequest(f); + expect(() => old.assertCurrent()).toThrow(); + expect( + (await f.lifecycle.verifyTool(ownershipNativeCall(f), "flow")).kind, + ).toBe("flow"); + await finishOwnedResponse(f, request); + } finally { + await f.lifecycle.dispose(); + } + }); +test("a reused compaction control ID rejects a new queued source before handoff", async () => { + const f = await fixture(); + try { + await initialOwnershipWork(f); + await compactCurrentInput(f, "msg_existing_control"); + await finishOwnedResponse(f, await startOwnedRequest(f)); + const old = await f.lifecycle.verifyTool(ownershipNativeCall(f), "flow"), + authority = f.autoDrive.captureAuthority(sessionID); + if (old.kind !== "flow") throw new Error("Missing old compaction guard."); + await queuePublic(f, "msg_A"); + f.native.push({ + type: "user", + id: "msg_A", + text: "msg_A", + time: { created: 20 }, + }); + const input = f.context(); + f.native.push(automaticControl("msg_existing_control")); + await expect(f.emit("compaction", input)).rejects.toThrow( + "control identity changed", + ); + expect(() => old.assertCurrent()).not.toThrow(); + expect(f.autoDrive.captureAuthority(sessionID)).toEqual(authority); + } finally { + await f.lifecycle.dispose(); + } +}); + +test("stream loss cancels a held terminal read before disposal drains its job", async () => { + const f = await fixture(); + try { + await initialOwnershipWork(f); + f.blockNextContextRead(); + f.sendEvent(succeededIdle(f)); + await f.waitContextRead(); + f.loseStream(); + await f.flush(); + await f.lifecycle.dispose(); + f.releaseContextRead(); + expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); + expect( + f.logs.filter( + (message) => message === "Flow event stream failed closed.", + ), + ).toHaveLength(1); + expect(f.native.filter((message) => message.type === "synthetic")).toEqual( + [], + ); + } finally { + f.releaseContextRead(); + await f.lifecycle.dispose(); + } +}); + +for (const rejectRead of [false, true]) + test(`review cancellation retry policy ${rejectRead ? "rejection" : "success"} preserves same-epoch replacement`, async () => { + const f = await fixture(); + const terminalFinished = Promise.withResolvers(); + const retryFinished = Promise.withResolvers(); + let release = () => {}; + try { + await initialOwnershipWork(f); + continuationProjection(f); + await queuePublic(f, "msg_A", 100); + const before = f.autoDrive.captureAuthority(sessionID); + const onIdle = f.autoDrive.onIdle.bind(f.autoDrive); + let idles = 0; + const idleSpy = spyOn(f.autoDrive, "onIdle").mockImplementation( + async (...args) => { + const sequence = ++idles; + try { + await onIdle(...args); + } finally { + (sequence === 1 ? terminalFinished : retryFinished).resolve(); + } + }, + ); + f.sendEvent(succeededIdle(f)); + await terminalFinished.promise; + const gate = f.blockPolicyRead("projection"); + release = gate.release; + await f.sendEventAndWait( + inboxEvent("session.inbox.cancelled", "msg_A", 101), + ); + await gate.entered; + await finishOwnedResponse(f, await startOwnedRequest(f)); + const fresh = await f.lifecycle.verifyTool( + ownershipNativeCall(f), + "flow", + ); + expect(fresh.kind).toBe("flow"); + expect(f.autoDrive.captureAuthority(sessionID)).toEqual(before); + if (rejectRead) + gate.reject(new Error("obsolete retry projection read failed")); + else gate.release(); + await retryFinished.promise; + idleSpy.mockRestore(); + let guardError: string | null = null; + try { + if (fresh.kind === "flow") fresh.assertCurrent(); + } catch (error) { + guardError = String(error); + } + expect(f.autoDrive.captureAuthority(sessionID)).toEqual(before); + expect(guardError).toBeNull(); + } finally { + release(); + await f.lifecycle.dispose(); + } + }); +test("review current terminal policy rejection still deactivates its owned lease", async () => { + const f = await fixture(); + const finished = Promise.withResolvers(); + let release = () => {}; + try { + await initialOwnershipWork(f); + continuationProjection(f); + const gate = f.blockPolicyRead("projection"); + release = gate.release; + const onIdle = f.autoDrive.onIdle.bind(f.autoDrive); + const idleSpy = spyOn(f.autoDrive, "onIdle").mockImplementation( + async (...args) => { + try { + await onIdle(...args); + } finally { + finished.resolve(); + } + }, + ); + f.sendEvent(succeededIdle(f)); + await gate.entered; + gate.reject(new Error("current projection read failed")); + await finished.promise; + idleSpy.mockRestore(); + expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); + expect(f.autoDrive.continuationSnapshot(sessionID)).toMatchObject({ + state: "inactive", + reason: "status-error", + }); + } finally { + release(); + await f.lifecycle.dispose(); + } +}); + +for (const prior of ["ordinary", "automatic"] as const) + test(`independent review valid queued user extends completed compaction alias before checkpoint primary ${prior}`, async () => { + const f = await fixture(); + try { + if (prior === "automatic") await initialOwnershipWork(f); + else { + await admitOrdinaryInput(f, "msg_review_O", "Ordinary predecessor"); + await finishOwnedResponse(f, await startOwnedRequest(f)); + } + await compactCurrentInput(f, "msg_review_checkpoint"); + await queuePublic(f, "msg_review_after_compaction"); + f.native.push({ + type: "user", + id: "msg_review_after_compaction", + text: "msg_review_after_compaction", + time: { created: 22 }, + }); + const request = await startOwnedRequest(f); + const effects: string[] = []; + const tool = await effectTool(f, effects); + await tool.execute({}, ownershipNativeCall(f)); + expect(effects).toEqual(["effect"]); + await finishOwnedResponse(f, request); + } finally { + await f.lifecycle.dispose(); + } + }); +test("independent review manual compaction alias followed by queued user retains ordinary Flow access", async () => { + const f = await fixture(); + try { + await admitOrdinaryInput(f, "msg_review_O", "Ordinary predecessor"); + await finishOwnedResponse(f, await startOwnedRequest(f)); + await compactCurrentInput(f, "msg_review_manual_checkpoint", "manual"); + await queuePublic(f, "msg_review_after_manual"); + f.native.push({ + type: "user", + id: "msg_review_after_manual", + text: "msg_review_after_manual", + time: { created: 22 }, + }); + const request = await startOwnedRequest(f); + const effects: string[] = []; + const tool = await effectTool(f, effects); + await tool.execute({}, ownershipNativeCall(f)); + expect(effects).toEqual(["effect"]); + await finishOwnedResponse(f, request); + } finally { + await f.lifecycle.dispose(); + } +}); +test("independent review observed native manual compaction inbox does not collide with its checkpoint identity", async () => { + const f = await fixture(); + try { + await admitOrdinaryInput(f, "msg_review_O", "Ordinary predecessor"); + await finishOwnedResponse(f, await startOwnedRequest(f)); + await f.sendEventAndWait({ + id: "evt_review_compaction_enqueued", + created: 40, + durable: { aggregateID: sessionID, seq: 40, version: 1 }, + type: "session.inbox.enqueued", + data: { + sessionID, + inboxID: "msg_review_manual_control", + item: { type: "compaction", payload: {}, delivery: "queue" }, + }, + }); + await f.sendEventAndWait({ + id: "evt_review_compaction_delivered", + created: 41, + durable: { aggregateID: sessionID, seq: 41, version: 1 }, + type: "session.inbox.delivered", + data: { sessionID, inboxID: "msg_review_manual_control" }, + }); + await compactCurrentInput(f, "msg_review_manual_control", "manual"); + const request = await startOwnedRequest(f); + const effects: string[] = []; + const tool = await effectTool(f, effects); + await tool.execute({}, ownershipNativeCall(f)); + expect(effects).toEqual(["effect"]); + await finishOwnedResponse(f, request); + } finally { + await f.lifecycle.dispose(); + } +}); + +test("a delivered-only compaction observation does not require an enqueue rendezvous", async () => { + const f = await fixture(); + try { + await admitOrdinaryInput(f, "msg_observed_O", "Ordinary predecessor"); + await finishOwnedResponse(f, await startOwnedRequest(f)); + await f.sendEventAndWait({ + id: "evt_compaction_delivered_only", + created: 41, + durable: { aggregateID: sessionID, seq: 41, version: 1 }, + type: "session.inbox.delivered", + data: { sessionID, inboxID: "msg_delivered_control" }, + }); + await compactCurrentInput(f, "msg_delivered_control", "manual"); + const request = await startOwnedRequest(f), + effects: string[] = [], + tool = await effectTool(f, effects); + await tool.execute({}, ownershipNativeCall(f)); + expect(effects).toEqual(["effect"]); + await finishOwnedResponse(f, request); + } finally { + await f.lifecycle.dispose(); + } +}); +for (const control of ["unknown", "summary-only", "wrong-reason"] as const) + test(`fresh queued input cannot use an unproved ${control} checkpoint prefix bridge`, async () => { + const f = await fixture(); + try { + await admitOrdinaryInput(f, "msg_bridge_O", "Ordinary predecessor"); + await finishOwnedResponse(f, await startOwnedRequest(f)); + const old = await f.lifecycle.verifyTool(ownershipNativeCall(f), "flow"); + if (old.kind !== "flow") throw new Error("Missing predecessor."); + if (control === "unknown") + f.native.splice(0, f.native.length, { + ...automaticControl("msg_unproved_C"), + status: "completed", + }); + else { + if (control === "summary-only") + f.native.push({ + type: "user", + id: "msg_unregistered_U", + text: "Unknown history", + time: { created: 15 }, + }); + await compactCurrentInput( + f, + "msg_unproved_C", + control === "summary-only" ? "manual" : "auto", + ); + if (control === "wrong-reason") { + const completed = f.native[0]; + if (completed?.type !== "compaction") + throw new Error("Missing completed control."); + completed.reason = "manual"; + } + } + await queuePublic(f, "msg_new_A"); + f.native.push({ + type: "user", + id: "msg_new_A", + text: "msg_new_A", + time: { created: 30 }, + }); + await expect(f.emit("context", f.context())).rejects.toThrow( + control === "unknown" + ? "epoch was revoked" + : control === "wrong-reason" + ? "control identity changed" + : "does not extend", + ); + expect(() => old.assertCurrent()).not.toThrow(); + } finally { + await f.lifecycle.dispose(); + } + }); + +test("a fresh queued source after a checked checkpoint proves its own selected model", async () => { + const f = await fixture(); + try { + await admitOrdinaryInput(f, "msg_model_O", "Ordinary predecessor"); + await finishOwnedResponse(f, await startOwnedRequest(f)); + await compactCurrentInput(f, "msg_model_C"); + await queuePublic(f, "msg_model_A"); + f.native.push({ + type: "user", + id: "msg_model_A", + text: "msg_model_A", + time: { created: 30 }, + }); + const chosen = { + ...model, + id: Model.ID.make("new-selected-model"), + variant: Model.VariantID.make("new-variant"), + }; + f.session.model = chosen; + await f.emit("context", { ...f.context(), model: chosen }); + const request = new Request("http://localhost/checked-new-model"); + await f.emit("http.request", { + sessionID, + agent, + model: chosen, + kind: "primary", + request, + }); + const call = ownershipNativeCall(f); + const assistant = f.native.find((message) => message.id === call.messageID); + if (assistant?.type !== "assistant") + throw new Error("Missing selected-model assistant."); + assistant.model = chosen; + const effects: string[] = [], + tool = await effectTool(f, effects); + await tool.execute({}, call); + expect(effects).toEqual(["effect"]); + expect(f.codingModels.get(sessionID)).toMatchObject({ + modelID: "new-selected-model", + variant: "new-variant", + }); + const response = { + sessionID, + agent, + model: chosen, + kind: "primary" as const, + request, + response: new Response("selected model completed"), + }; + await f.emit("http.response", response); + expect(await response.response.text()).toBe("selected model completed"); + } finally { + await f.lifecycle.dispose(); + } +}); + +for (const interveningPrimary of [false, true]) + test(`queued input follows consecutive owned checkpoints, intermediate primary ${interveningPrimary}`, async () => { + const f = await fixture(); + try { + await admitOrdinaryInput(f, "msg_chain_O", "Ordinary predecessor"); + await finishOwnedResponse(f, await startOwnedRequest(f)); + await compactCurrentInput(f, "msg_chain_C1"); + if (interveningPrimary) + await finishOwnedResponse(f, await startOwnedRequest(f)); + await compactCurrentInput(f, "msg_chain_C2"); + await queuePublic(f, "msg_chain_A"); + f.native.push({ + type: "user", + id: "msg_chain_A", + text: "msg_chain_A", + time: { created: 40 }, + }); + const request = await startOwnedRequest(f), + effects: string[] = [], + tool = await effectTool(f, effects); + await tool.execute({}, ownershipNativeCall(f)); + expect(effects).toEqual(["effect"]); + await finishOwnedResponse(f, request); + } finally { + await f.lifecycle.dispose(); + } + }); diff --git a/tests/recovery-authority.test.ts b/tests/recovery-authority.test.ts index 7798525e..34037cd2 100644 --- a/tests/recovery-authority.test.ts +++ b/tests/recovery-authority.test.ts @@ -16,7 +16,10 @@ import { } from "../src/platform/opencode/auto-drive.js"; import { InputAdmissions } from "../src/platform/opencode/input-admission.js"; import { continuationCommands } from "./fixtures/continuation-commands.js"; -import { verifiedContinuationOrigin } from "./fixtures/verified-continuation-origin.js"; +import { + fixtureAutomaticPrompt, + verifiedContinuationOrigin, +} from "./fixtures/verified-continuation-origin.js"; const cleanups: Array<() => Promise> = []; afterEach(async () => { @@ -99,7 +102,10 @@ test.each(["off", "no-key", "shadow"] as const)( revision: revision(s.repository), nextAction: "await-user-direction", }), - prompt: async () => {}, + prompt: fixtureAutomaticPrompt( + () => auto, + async () => {}, + ), }); const commands = await continuationCommands({ autoDrive: auto, @@ -785,7 +791,10 @@ test("unmatched native requests fail closed and fresh flow-run restores a verifi revision: revision(s.repository), nextAction: "flow_run_start", }), - prompt: async () => {}, + prompt: fixtureAutomaticPrompt( + () => auto, + async () => {}, + ), }); const commands = await continuationCommands({ autoDrive: auto, @@ -859,27 +868,31 @@ test.each(["success", "failure"] as const)( recovery: a.controller, createToken: () => "fixed-old-token", readProjection: async () => projection, - prompt: async (host, _text, delivery, metadata) => { - count++; - if (count === 1) { - await driver.observeMessage( - host, - delivery, - [{ type: "text", synthetic: true, metadata }], - "first-handback", - ); - a.controller.observeMessage(host, "first-handback", true, true); - return; - } - lateMetadata = metadata; - started(); - await new Promise((resolve, reject) => { - release = () => - outcome === "failure" - ? reject(new Error("Obsolete prompt failed")) - : resolve(); - }); - }, + prompt: fixtureAutomaticPrompt( + () => driver, + async (host, _text, delivery, metadata) => { + count++; + if (count === 1) { + await driver.observeMessage( + host, + delivery, + [{ type: "text", synthetic: true, metadata }], + "first-handback", + ); + a.controller.observeMessage(host, "first-handback", true, true); + return; + } + lateMetadata = metadata; + started(); + await new Promise((resolve, reject) => { + release = () => + outcome === "failure" + ? reject(new Error("Obsolete prompt failed")) + : resolve(); + }); + }, + true, + ), }); const delivery = { agent: "build", @@ -995,19 +1008,23 @@ test("obsolete initial prompt failure preserves a renewed checkpoint reply", asy revision: 0, nextAction: "flow_plan_save", }), - prompt: async (host, _text, delivery, metadata) => { - initialMetadata = metadata; - await driver.observeMessage( - host, - delivery, - [{ type: "text", synthetic: true, metadata }], - "initial-route", - ); - started(); - await new Promise((_resolve, reject) => { - release = () => reject(new Error("Obsolete initial prompt failed")); - }); - }, + prompt: fixtureAutomaticPrompt( + () => driver, + async (host, _text, delivery, metadata) => { + initialMetadata = metadata; + await driver.observeMessage( + host, + delivery, + [{ type: "text", synthetic: true, metadata }], + "initial-route", + ); + started(); + await new Promise((_resolve, reject) => { + release = () => reject(new Error("Obsolete initial prompt failed")); + }); + }, + true, + ), }); const delivery = { agent: "build", @@ -1059,7 +1076,10 @@ test.each([ nextAction: failures === 1 ? "flow_feature_reset" : "await-user-direction", }), - prompt: async () => {}, + prompt: fixtureAutomaticPrompt( + () => driver, + async () => {}, + ), }); const delivery = { agent: "build", @@ -1139,6 +1159,26 @@ test.each([ const boundary = new CompactionBoundary({ inputs, readNative: async () => structuredClone(native), + readSelected: async () => ({ + id: host, + agent: nativeAgent, + model: nativeModel, + projectID: "test", + location: { directory: process.cwd() }, + time: { created: 0, updated: 0 }, + cost: 0, + tokens: { + input: 0, + output: 0, + reasoning: 0, + cache: { read: 0, write: 0 }, + }, + }), + captureReplacement: () => ({ + check: () => {}, + supersede: () => {}, + current: () => true, + }), failure: () => {}, hasActivePrimary: () => false, guidance: () => driver.compactionContext(host), diff --git a/tests/recovery-host.test.ts b/tests/recovery-host.test.ts index f1b9f972..6631ce4f 100644 --- a/tests/recovery-host.test.ts +++ b/tests/recovery-host.test.ts @@ -10,7 +10,10 @@ import { FLOW_AUTO_METADATA_KEY, } from "../src/platform/opencode/auto-drive.js"; import { continuationCommands } from "./fixtures/continuation-commands.js"; -import { verifiedContinuationOrigin } from "./fixtures/verified-continuation-origin.js"; +import { + fixtureAutomaticPrompt, + verifiedContinuationOrigin, +} from "./fixtures/verified-continuation-origin.js"; import { deterministicEnvironment, MemorySessionRepository, @@ -60,9 +63,12 @@ async function commandFixture( const auto = new AutoDriveCoordinator({ recovery, readProjection: options.readProjection ?? (async () => projection), - prompt: async (_host, prompt) => { - prompts.push(prompt); - }, + prompt: fixtureAutomaticPrompt( + () => auto, + async (_host, prompt) => { + prompts.push(prompt); + }, + ), }); const commands = await continuationCommands({ autoDrive: auto, @@ -97,22 +103,26 @@ test("fast recovery continuation receipt survives enqueue and advances to next f const auto = new AutoDriveCoordinator({ recovery, readProjection: async () => projection, - prompt: async (host, _text, model, metadata) => { - prompts++; - await auto.observeMessage( - host, - model, - [{ type: "text", synthetic: true, metadata }], - `msg_continuation-${prompts}`, - ); - await verifiedContinuationOrigin( - auto, - host, - `msg_continuation-${prompts}`, - `msg_assistant-${prompts}`, - model, - ); - }, + prompt: fixtureAutomaticPrompt( + () => auto, + async (host, _text, model, metadata) => { + prompts++; + await auto.observeMessage( + host, + model, + [{ type: "text", synthetic: true, metadata }], + `msg_continuation-${prompts}`, + ); + await verifiedContinuationOrigin( + auto, + host, + `msg_continuation-${prompts}`, + `msg_assistant-${prompts}`, + model, + ); + }, + true, + ), }); const metadata = await auto.activate("ses_host"); await auto.observeMessage( @@ -153,10 +163,13 @@ test("recovery never overrides unowned-session stop and prompt failure revokes", const auto = new AutoDriveCoordinator({ recovery, readProjection: async () => projection, - prompt: async () => { - prompts++; - throw new Error("host unavailable"); - }, + prompt: fixtureAutomaticPrompt( + () => auto, + async () => { + prompts++; + throw new Error("host unavailable"); + }, + ), }); const metadata = await auto.activate("ses_host"); expect(metadata[FLOW_AUTO_METADATA_KEY]).toBeDefined(); @@ -187,9 +200,12 @@ test("shadow recovery prompt does not add a second handback at one checkpoint", revision, nextAction: "await-user-direction", }), - prompt: async (_host, prompt) => { - prompts.push(prompt); - }, + prompt: fixtureAutomaticPrompt( + () => auto, + async (_host, prompt) => { + prompts.push(prompt); + }, + ), }); const metadata = await auto.activate("ses_host"); await auto.observeMessage( @@ -221,9 +237,12 @@ test("running state cannot trigger Jev even with a direction-shaped next action" revision: 10, nextAction: "await-user-direction", }), - prompt: async (_host, prompt) => { - prompts.push(prompt); - }, + prompt: fixtureAutomaticPrompt( + () => auto, + async (_host, prompt) => { + prompts.push(prompt); + }, + ), }); const metadata = await auto.activate("ses_host"); await auto.observeMessage( @@ -512,7 +531,12 @@ test("ordinary handback and native flow-run keep one shadow lease for the next c }); now = 60 * 60 * 1000; expect( - f.recovery.proposalPrompt("ses_host", "flow", 12, "await-user-direction"), + f.recovery.prepareProposalPrompt( + "ses_host", + "flow", + 12, + "await-user-direction", + ), ).toBeNull(); expect(f.recovery.snapshot("ses_host")).toMatchObject({ mode: "off", diff --git a/tests/recovery-policy.test.ts b/tests/recovery-policy.test.ts index dc297fa2..f6277078 100644 --- a/tests/recovery-policy.test.ts +++ b/tests/recovery-policy.test.ts @@ -12,7 +12,10 @@ import { import { createJevDecisionProvider } from "../src/infrastructure/jev-decision-provider.js"; import { parseRecoveryCommand } from "../src/platform/opencode/command-hook.js"; import { continuationCommands } from "./fixtures/continuation-commands.js"; -import { verifiedContinuationOrigin } from "./fixtures/verified-continuation-origin.js"; +import { + fixtureAutomaticPrompt, + verifiedContinuationOrigin, +} from "./fixtures/verified-continuation-origin.js"; const cleanups: Array<() => Promise> = []; afterEach(async () => { @@ -216,7 +219,10 @@ describe("process-local recovery", () => { throw new Error("compact status unavailable"); return status.workflowData.projection; }, - prompt: async () => {}, + prompt: fixtureAutomaticPrompt( + () => auto, + async () => {}, + ), }); const commands = await continuationCommands({ autoDrive: auto, @@ -1178,11 +1184,21 @@ test("expired advice stops prompting while automatic retry safeguards remain", a controller.observeMessage("ses_host", "old-user", false); controller.observeAssistant("ses_host", "old-assistant", "old-user"); expect( - controller.proposalPrompt("ses_host", "flow", 10, "await-user-direction"), + controller.prepareProposalPrompt( + "ses_host", + "flow", + 10, + "await-user-direction", + ), ).not.toBeNull(); now = 60 * 60 * 1000; expect( - controller.proposalPrompt("ses_host", "flow", 11, "await-user-direction"), + controller.prepareProposalPrompt( + "ses_host", + "flow", + 11, + "await-user-direction", + ), ).toBeNull(); expect(controller.snapshot()).toMatchObject({ mode: "off", @@ -1362,26 +1378,30 @@ test("registered tools share the controller across file-backed service instances throw new Error("projection"); return status.workflowData.projection; }, - prompt: async (host, _text, delivery, metadata) => { - prompts++; - const user = `msg_recovery-turn-${prompts}`; - ctx.messageID = `msg_assistant-${prompts}`; - await auto.observeMessage( - host, - delivery, - [{ type: "text", synthetic: true, metadata }], - user, - ); - await verifiedContinuationOrigin( - auto, - host, - user, - ctx.messageID, - delivery, - ); - s.controller.observeMessage(host, user, true, true); - s.controller.observeAssistant(host, ctx.messageID, user); - }, + prompt: fixtureAutomaticPrompt( + () => auto, + async (host, _text, delivery, metadata) => { + prompts++; + const user = `msg_recovery-turn-${prompts}`; + ctx.messageID = `msg_assistant-${prompts}`; + await auto.observeMessage( + host, + delivery, + [{ type: "text", synthetic: true, metadata }], + user, + ); + await verifiedContinuationOrigin( + auto, + host, + user, + ctx.messageID, + delivery, + ); + s.controller.observeMessage(host, user, true, true); + s.controller.observeAssistant(host, ctx.messageID, user); + }, + true, + ), }); const { ValidationCaptureCoordinator } = await import( "../src/platform/opencode/validation-capture.js" @@ -1836,7 +1856,10 @@ test("plain auto in another host rejects the previous host's pending grant", asy revision: revision(s.repository), nextAction: "await-user-direction", }), - prompt: async () => {}, + prompt: fixtureAutomaticPrompt( + () => auto, + async () => {}, + ), }); await auto.activate("ses_host"); const commands = await continuationCommands({ diff --git a/tests/recovery-proposal.test.ts b/tests/recovery-proposal.test.ts new file mode 100644 index 00000000..a61d241e --- /dev/null +++ b/tests/recovery-proposal.test.ts @@ -0,0 +1,28 @@ +import { expect, test } from "bun:test"; +import { RecoveryProposals } from "../src/application/recovery-proposal.js"; + +test("proposal preparation consumes no key and only one competing exact claim commits", () => { + const owner = new RecoveryProposals(); + const first = owner.prepare("flow/1", () => true), + competing = owner.prepare("flow/2", () => true); + expect(first?.text).toContain("top-level recoveryProposal"); + expect(owner.prepare("flow/1", () => true)).not.toBeNull(); + competing?.commit(); + expect(() => first?.commit()).toThrow("superseded"); + expect(owner.prepare("flow/2", () => true)).toBeNull(); + expect(() => competing?.commit()).toThrow("superseded"); + expect(owner.prepare("flow/2", () => true)).toBeNull(); +}); +test("an expired or replaced exact lease invalidates its prepared proposal", () => { + const old = new RecoveryProposals(); + let valid = true; + const claim = old.prepare("flow/1", () => valid); + valid = false; + expect(() => claim?.assertCurrent()).toThrow("superseded"); + expect(() => claim?.commit()).toThrow("superseded"); + const fresh = new RecoveryProposals(); + const replacement = fresh.prepare("flow/1", () => true); + replacement?.commit(); + expect(fresh.prepare("flow/1", () => true)).toBeNull(); + expect(old.prepare("flow/1", () => true)).not.toBeNull(); +}); From 3a51aba7a3fd6535977f6469710db9c17352c7c9 Mon Sep 17 00:00:00 2001 From: Douwe de Vries Date: Sun, 25 Oct 2026 03:51:23 +0100 Subject: [PATCH 12/17] test(opencode): expose failed validation capture to the model --- tests/distribution-and-surface.test.ts | 104 +++++++++++++++++++++++++ 1 file changed, 104 insertions(+) diff --git a/tests/distribution-and-surface.test.ts b/tests/distribution-and-surface.test.ts index 83f20bcb..55ac25b7 100644 --- a/tests/distribution-and-surface.test.ts +++ b/tests/distribution-and-surface.test.ts @@ -918,3 +918,107 @@ test("attachment-only native user direction can close a stopped recovery workflo }); expect(closed.status).toBe("ok"); }); + +test("current armed validation persistence failure remains visible to the model", async () => { + const { spyOn } = await import("bun:test"); + const { realpath } = await import("node:fs/promises"); + const { ValidationCaptureCoordinator } = await import( + "../src/platform/opencode/validation-capture.js" + ); + type ValidationCaptureCoordinator = + import("../src/platform/opencode/validation-capture.js").ValidationCaptureCoordinator; + type NativeToolAfter = + import("../src/platform/opencode/sdk.js").NativeToolAfter; + type NativeToolBefore = + import("../src/platform/opencode/sdk.js").NativeToolBefore; + const { host, workspace } = await load(); + let receiver: ValidationCaptureCoordinator | undefined; + const rejected: unknown[] = []; + const original = ValidationCaptureCoordinator.prototype.observeToolAfter; + const observer = spyOn( + ValidationCaptureCoordinator.prototype, + "observeToolAfter", + ).mockImplementation(async function ( + this: ValidationCaptureCoordinator, + input, + ) { + receiver = this; + try { + return await original.call(this, input); + } catch (error) { + rejected.push(error); + throw error; + } + }); + try { + const command = "printf 'literal raw shell output\\n0 failed\\n'"; + const native = await host.toolContext("shell", { command }); + const before: NativeToolBefore = { + tool: "shell", + sessionID: native.sessionID, + messageID: native.messageID, + id: native.id, + agent: native.agent, + input: { command }, + }; + await host.toolHooks.emit("execute.after", { + ...before, + status: "completed", + result: { content: "unarmed literal" }, + }); + function coordinator(): ValidationCaptureCoordinator { + if (!receiver) + throw new Error("Composition did not invoke its coordinator."); + return receiver; + } + const capture = coordinator(); + capture.arm(native.sessionID, workspace, { + featureId: "capture-research", + runId: "run-research", + command, + scope: "focused", + sourceDigest: `sha256:${"a".repeat(64)}`, + hostPlatform: "other", + intent: "pass", + assertions: [], + resultsPath: undefined, + }); + await host.toolHooks.emit("execute.before", before); + expect(capture.pendingCount()).toBe(1); + await mkdir(join(workspace, ".flow"), { recursive: true }); + await writeFile( + join(workspace, ".flow/session.json"), + "unchanged durable bytes\n", + ); + await writeFile( + join(workspace, ".flow/session.lock"), + "deliberate capture failure\n", + ); + const result = { + content: "literal raw shell output\n0 failed\n", + metadata: { exit: 0, truncated: false }, + output: { + exit: 0, + truncated: false, + output: "literal raw shell output\n0 failed\n", + }, + }; + const after: NativeToolAfter = { ...before, status: "completed", result }; + await host.toolHooks.emit("execute.after", after); + expect(rejected.map(String)).toEqual([ + `Error: Flow requires the Flow session lock to be a directory: ${join(await realpath(workspace), ".flow/session.lock")}.`, + ]); + expect(capture.pendingCount()).toBe(0); + expect(await readFile(join(workspace, ".flow/session.json"), "utf8")).toBe( + "unchanged durable bytes\n", + ); + expect(after.result.metadata).toBe(result.metadata); + expect(after.result.output).toBe(result.output); + expect(JSON.stringify(after)).not.toContain("[flow-validation]"); + expect(after.result.content).toBe( + "literal raw shell output\n0 failed\n\n\n[flow-validation-error] Flow validation capture failed. No validation receipt was produced.", + ); + } finally { + observer.mockRestore(); + } +}); From 0228d8af7a913e275c161d8eacc00983ae452d02 Mon Sep 17 00:00:00 2001 From: Douwe de Vries Date: Sun, 25 Oct 2026 04:07:33 +0100 Subject: [PATCH 13/17] fix(opencode): surface validation capture failures --- .../18-opencode-v2/capture-error-reporting.md | 42 +++ .agents/plans/18-opencode-v2/decisions.tsv | 1 + src/platform/opencode/plugin-composition.ts | 12 + tests/validation-capture-output.test.ts | 332 ++++++++++++++++++ 4 files changed, 387 insertions(+) create mode 100644 .agents/plans/18-opencode-v2/capture-error-reporting.md create mode 100644 tests/validation-capture-output.test.ts diff --git a/.agents/plans/18-opencode-v2/capture-error-reporting.md b/.agents/plans/18-opencode-v2/capture-error-reporting.md new file mode 100644 index 00000000..cd713bda --- /dev/null +++ b/.agents/plans/18-opencode-v2/capture-error-reporting.md @@ -0,0 +1,42 @@ +# Capture error reporting + +Sentry discussion 4369391412 identified a real v2 reporting regression. The +completed shell could succeed while Flow capture failed, leaving the model with +only raw shell output. The host log contained the exception. No validation +receipt was fabricated. + +The parent reproduced this on the exact `c38dc14e` candidate and native OpenCode +2.1.4. Plan save, approval, run start and validation arming all succeeded before +the disposable command deliberately made its own session JSON unreadable. The +shell exited zero; the next model request contained only its literal output. +The first wrong-platform setup attempt remains excluded from that proof. + +The fix adds a static `[flow-validation-error]` notice to completed v2 content. +It preserves existing text or file parts, metadata and structured output. +Detailed errors remain in the existing log. The notice says no validation +receipt was produced; it does not claim a write could never partially persist. +Native error objects remain unchanged because v2 can render a nested cause +instead of an outer message. Stale null refusals and unrelated calls remain +silent. The marker grants no validation, receipt or continuation authority. + +The regression was committed first as `3a51aba7`. Five visibility cases failed +before the fix while three guards passed. The same eight cases pass afterward. +The parent passed all 157 focused cases. Independent OpenAI source review and +comment review pass. The production change is 12 lines in the existing catch, +with no new production helper, state or API. + +The component persistence probe uses a regular lock file and preserves sentinel +durable bytes. That differs from the intentionally corrupted native fixture, +which does not preserve a valid session document. SDK-optional absent content is +tested for field preservation only. It is not native fallback qualification. + +The parent must replay the same corrected native driver on a new exact archive +and require the notice in both durable native tool content and the next model +request, with no false validation receipt. Full contribution gates precede push. +Paid qualification, merge and publication remain on hold. + +Local evidence is under `/tmp/flow-v2-capture-error-20261025/`. The source review +is `source-review-r1/review.md`, the accepted native before receipt is +`parent-native/before-accepted.json`, and the unchanged before/after driver is +`parent-native/driver-corrected.ts`. Parent package and native-after evidence +retain their own identities. diff --git a/.agents/plans/18-opencode-v2/decisions.tsv b/.agents/plans/18-opencode-v2/decisions.tsv index c783149e..67e1eb93 100644 --- a/.agents/plans/18-opencode-v2/decisions.tsv +++ b/.agents/plans/18-opencode-v2/decisions.tsv @@ -73,3 +73,4 @@ ts phase decision why evidence result 2026-10-24T17:54:19Z dependencies Merge independently verified action updates before the v2 PR Each reviewed pin-only patch passed current checks and parent verification; the combined workflow tree passed actionlint /tmp/flow-dep-pr-195-review-20261024/review.txt; /tmp/flow-dep-pr-196-review-20261024/review.md; /tmp/flow-v2-release-order-20261024/pr196-parent-verification.json; b348552dcc03c5d58aa5410de2de66bf05ce1f89 Both squash merges confirmed on remote main; combined CI started; local merge has no conflicts 2026-10-24T17:56:12Z gate Hold the v2 push because merged main audit is red The operator explicitly required green main before publishing the v2 PR; existing v1 Zod and TOML dependencies now fail audit while the v2 dependency graph audits clean /tmp/flow-v2-release-order-20261024/main-dependency-audit.log; /tmp/flow-v2-release-order-20261024/v2-current-audit.log; https://github.com/ddv1982/flow-opencode/actions/runs/40118156331 Both dependency PRs merged; v2 remains local; no audit suppression or further paid work 2026-10-25T02:17:33Z queued-input-review Separate queued preparation from exact consumed provenance Native reproduction confirmed premature epoch revocation; four independent review defects were corrected without expanding automatic authority .agents/plans/18-opencode-v2/queued-input-review.md; /tmp/flow-v2-queued-input-20261025/core-review-r2/review.md; /tmp/flow-v2-queued-input-20261025/dispatch-review-r2/review.md Fresh source reviews and full check pass; exact archive/native replay still required; paid qualification remains on hold +2026-10-25T02:59:54Z capture-error-reporting Restore a static v2 completed-result failure notice Real armed capture persistence failure was logged but invisible to the model; preserve native errors and stale null refusals .agents/plans/18-opencode-v2/capture-error-reporting.md; /tmp/flow-v2-capture-error-20261025/parent-native/before-accepted.json; /tmp/flow-v2-capture-error-20261025/source-review-r1/review.md Red-first 3a51aba7; focused and independent reviews pass; new exact native after and contribution gates remain parent proof; paid qualification on hold diff --git a/src/platform/opencode/plugin-composition.ts b/src/platform/opencode/plugin-composition.ts index e88ec6bd..a4b2b9a4 100644 --- a/src/platform/opencode/plugin-composition.ts +++ b/src/platform/opencode/plugin-composition.ts @@ -271,6 +271,18 @@ export function createFlowPlugin(dependencies: { log("error", "Flow validation capture failed closed.", { message: String(error), }); + if (input.status === "completed") { + const marker = + "\n\n[flow-validation-error] Flow validation capture failed. No validation receipt was produced."; + const content = input.result.content; + input.result = { + ...input.result, + content: + typeof content === "string" + ? content + marker + : [...(content ?? []), { type: "text", text: marker }], + }; + } } }); cleanups.push(() => after.dispose()); diff --git a/tests/validation-capture-output.test.ts b/tests/validation-capture-output.test.ts new file mode 100644 index 00000000..3129d6f8 --- /dev/null +++ b/tests/validation-capture-output.test.ts @@ -0,0 +1,332 @@ +import { afterEach, expect, spyOn, test } from "bun:test"; +import { + mkdir, + mkdtemp, + readdir, + readFile, + realpath, + rm, + writeFile, +} from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import type { Result } from "@opencode/plugin/promise/tool"; +import type { PreparedValidation } from "../src/application/prepare-validation.js"; +import FlowPlugin from "../src/index.js"; +import { + CallID, + type NativeToolAfter, + type NativeToolBefore, + NativeToolError, +} from "../src/platform/opencode/sdk.js"; +import { ValidationCaptureCoordinator } from "../src/platform/opencode/validation-capture.js"; +import { createNativeFlowHost } from "./fixtures/native-flow-host.js"; + +const marker = + "\n\n[flow-validation-error] Flow validation capture failed. No validation receipt was produced."; +const raw = "literal raw shell output\n0 failed\n"; +const prepared: PreparedValidation = { + featureId: "capture-research", + runId: "run-research", + command: "printf 'literal raw shell output\\n0 failed\\n'", + scope: "focused", + sourceDigest: `sha256:${"a".repeat(64)}`, + hostPlatform: "other", + intent: "pass", + assertions: [], + resultsPath: undefined, +}; +const file = { + type: "file", + uri: "file:///fixture/report.txt", + mime: "text/plain", + name: "report.txt", +} satisfies NonNullable>[number]; +const cases: ReadonlyArray<{ + name: string; + content?: Result["content"]; + expected: Result["content"]; +}> = [ + { name: "string", content: raw, expected: raw + marker }, + { + name: "text and file array", + content: [{ type: "text", text: raw }, file], + expected: [ + { type: "text", text: raw }, + file, + { type: "text", text: marker }, + ], + }, + { + name: "file only", + content: [file], + expected: [file, { type: "text", text: marker }], + }, + { name: "absent content", expected: [{ type: "text", text: marker }] }, +]; + +const cleanups: Array<() => Promise> = []; +afterEach(async () => { + for (const cleanup of cleanups.splice(0).reverse()) await cleanup(); +}); + +async function fixture() { + const workspace = await realpath(await mkdtemp(join(tmpdir(), "component-"))); + const host = await createNativeFlowHost(workspace); + let receiver: ValidationCaptureCoordinator | undefined; + const rejections: unknown[] = []; + const original = ValidationCaptureCoordinator.prototype.observeToolAfter; + const probe = spyOn( + ValidationCaptureCoordinator.prototype, + "observeToolAfter", + ).mockImplementation(async function ( + this: ValidationCaptureCoordinator, + input, + ) { + receiver = this; + try { + return await original.call(this, input); + } catch (error) { + rejections.push(error); + throw error; + } + }); + cleanups.push(async () => { + probe.mockRestore(); + try { + await host.dispose(); + } finally { + await rm(workspace, { recursive: true, force: true }); + } + }); + await host.load(FlowPlugin); + const native = await host.toolContext("shell", { command: prepared.command }); + const before: NativeToolBefore = { + tool: "shell", + sessionID: native.sessionID, + messageID: native.messageID, + id: native.id, + agent: native.agent, + input: { command: prepared.command }, + }; + await host.toolHooks.emit("execute.after", { + ...before, + status: "completed", + result: { content: "unarmed literal" }, + }); + function coordinator(): ValidationCaptureCoordinator { + if (!receiver) + throw new Error("Composition did not invoke the real coordinator."); + return receiver; + } + const capture = coordinator(); + async function arm() { + capture.arm(native.sessionID, workspace, prepared); + await host.toolHooks.emit("execute.before", before); + expect(capture.pendingCount()).toBe(1); + } + async function failPersistence() { + await mkdir(join(workspace, ".flow"), { recursive: true }); + await writeFile( + join(workspace, ".flow/session.json"), + "unchanged durable bytes\n", + ); + await writeFile( + join(workspace, ".flow/session.lock"), + "deliberate regular-file lock failure\n", + ); + } + async function assertFailed() { + expect(rejections.map(String)).toEqual([ + `Error: Flow requires the Flow session lock to be a directory: ${join(workspace, ".flow/session.lock")}.`, + ]); + expect(capture.pendingCount()).toBe(0); + expect(await readFile(join(workspace, ".flow/session.json"), "utf8")).toBe( + "unchanged durable bytes\n", + ); + expect((await readdir(join(workspace, ".flow"))).sort()).toEqual([ + ".gitignore", + "session.json", + "session.lock", + ]); + } + return { + workspace, + host, + capture, + before, + arm, + failPersistence, + assertFailed, + rejections, + async dispose() { + const cleanup = cleanups.pop(); + if (!cleanup) throw new Error("Missing capture fixture cleanup."); + await cleanup(); + }, + }; +} + +for (const item of cases) + test(`current armed filesystem failure is visible in ${item.name}`, async () => { + const f = await fixture(); + try { + await f.arm(); + await f.failPersistence(); + const result: Result = { + ...(item.content === undefined ? {} : { content: item.content }), + metadata: { + exit: 0, + truncated: false, + privateKey: "preserved-metadata", + }, + output: { exit: 0, truncated: false, output: raw }, + }; + const after: NativeToolAfter = { + ...f.before, + status: "completed", + result, + }; + await f.host.toolHooks.emit("execute.after", after); + await f.assertFailed(); + expect(after.result.metadata).toBe(result.metadata); + expect(after.result.output).toBe(result.output); + expect(JSON.stringify(after)).not.toContain("[flow-validation]"); + expect(JSON.stringify(after.result.content ?? null)).not.toContain( + f.workspace, + ); + expect(JSON.stringify(after.result.content ?? null)).not.toContain( + "Flow requires the Flow session lock", + ); + expect(after.result.content).toEqual(item.expected); + if (Array.isArray(item.content) && Array.isArray(after.result.content)) + for (const [index, part] of item.content.entries()) + expect(after.result.content[index]).toBe(part); + } finally { + await f.dispose(); + } + }); + +test("current armed capture failure preserves the existing native error channel", async () => { + const f = await fixture(); + try { + await f.arm(); + await f.failPersistence(); + const originalCause = { diagnostic: "existing-private-cause" }; + const error = new NativeToolError({ + message: "literal native shell failure", + metadata: { existing: "metadata" }, + error: originalCause, + }); + const after: NativeToolAfter = { ...f.before, status: "error", error }; + await f.host.toolHooks.emit("execute.after", after); + await f.assertFailed(); + expect(after.status).toBe("error"); + expect(after.error._tag).toBe("Tool.Error"); + expect(after.error.metadata).toBe(error.metadata); + expect(after.error.error).toBe(originalCause); + expect(after.error.message).toBe("literal native shell failure"); + expect(after.error).toBe(error); + } finally { + await f.dispose(); + } +}); + +test("unarmed, nonshell, unmatched and stale completions keep literal content", async () => { + const f = await fixture(); + try { + const after: NativeToolAfter = { + ...f.before, + status: "completed", + result: { content: raw, metadata: { exit: 0, truncated: false } }, + }; + await f.host.toolHooks.emit("execute.after", after); + expect(after.result.content).toBe(raw); + await f.arm(); + const nonshell: NativeToolAfter = { ...after, tool: "read" }; + await f.host.toolHooks.emit("execute.after", nonshell); + expect(nonshell.result.content).toBe(raw); + expect(f.capture.pendingCount()).toBe(1); + const unmatched: NativeToolAfter = { + ...after, + id: CallID.make("unmatched-call"), + }; + await f.host.toolHooks.emit("execute.after", unmatched); + expect(unmatched.result).toBe(after.result); + expect(unmatched.result.content).toBe(raw); + expect(f.capture.pendingCount()).toBe(1); + f.capture.cancel(f.before.sessionID); + f.capture.arm(f.before.sessionID, f.workspace, prepared); + await f.host.toolHooks.emit("execute.after", after); + expect(after.result.content).toBe(raw); + expect(f.capture.pendingCount()).toBe(1); + expect(f.rejections).toEqual([]); + expect(await readdir(f.workspace)).not.toContain(".flow"); + } finally { + await f.dispose(); + } +}); + +test("an ordinary session read failure for a current capture is visible", async () => { + const f = await fixture(); + try { + await f.arm(); + const get = spyOn(f.host.context.session, "get").mockRejectedValue( + new Error("private read diagnostic"), + ); + const after: NativeToolAfter = { + ...f.before, + status: "completed", + result: { content: raw }, + }; + try { + await f.host.toolHooks.emit("execute.after", after); + } finally { + get.mockRestore(); + } + expect(f.rejections.map(String)).toEqual([ + "Error: private read diagnostic", + ]); + expect(f.capture.pendingCount()).toBe(0); + expect(after.result.content).toBe(raw + marker); + } finally { + await f.dispose(); + } +}); + +test("stale rejection during an after read cannot annotate an old call or remove the next capture", async () => { + const f = await fixture(); + try { + await f.arm(); + const entered = Promise.withResolvers(); + const release = Promise.withResolvers(); + const get = spyOn(f.host.context.session, "get").mockImplementation( + async () => { + entered.resolve(); + await release.promise; + throw new Error("stale private read diagnostic"); + }, + ); + const after: NativeToolAfter = { + ...f.before, + status: "completed", + result: { content: raw, metadata: { exit: 0, truncated: false } }, + }; + try { + const pending = f.host.toolHooks.emit("execute.after", after); + await entered.promise; + f.capture.cancel(f.before.sessionID); + f.capture.arm(f.before.sessionID, f.workspace, prepared); + release.resolve(); + await pending; + } finally { + get.mockRestore(); + } + expect(after.result.content).toBe(raw); + expect(f.capture.pendingCount()).toBe(1); + expect(f.rejections).toEqual([]); + expect(await readdir(f.workspace)).not.toContain(".flow"); + } finally { + await f.dispose(); + } +}); From 7198573ab666eac9ca0f4524e33ce84cccc5d3f2 Mon Sep 17 00:00:00 2001 From: Douwe de Vries Date: Sun, 25 Oct 2026 05:47:49 +0100 Subject: [PATCH 14/17] test(opencode): reproduce failed reservation blocking queued turns --- tests/input-admission.test.ts | 87 ++++ tests/lifecycle.test.ts | 816 ++++++++++++++++++++++++++++++++++ 2 files changed, 903 insertions(+) diff --git a/tests/input-admission.test.ts b/tests/input-admission.test.ts index 101f431c..b100f0bb 100644 --- a/tests/input-admission.test.ts +++ b/tests/input-admission.test.ts @@ -1182,3 +1182,90 @@ test("a matching delivered compaction observation transfers at final alias insta f.close(); } }); + +import { SessionMessage as TriageSessionMessage } from "@opencode/schema/session-message"; + +test("reservation triage rejected absent private identity cannot veto a new consumed public queue", async () => { + const f = fixture(); + try { + f.inputs.reject( + f.admission, + new Error("Private RPC failed before history."), + ); + f.inputs.invalidate(host); + await expect(f.admission.ready).rejects.toThrow( + "Private RPC failed before history.", + ); + f.inputs.observePrompt( + { + sessionID: host, + messageID: TriageSessionMessage.ID.make("msg_candidate_b_fresh"), + prompt: { text: "Fresh queue" }, + delivery: "queue", + }, + () => { + throw new Error("Queue unexpectedly requested immediate revocation."); + }, + ); + const outcome = await (async () => { + const operation = f.inputs.openSource(context("msg_candidate_b_fresh")); + try { + const result = await operation.resolve( + [ + { + type: "user", + id: "msg_candidate_b_fresh", + text: "Fresh queue", + time: { created: 1 }, + }, + ], + selected, + effects, + ); + expect( + result.evidence.kind === "live" + ? result.evidence.origin.id + : result.evidence.kind, + ).toBe("msg_candidate_b_fresh"); + expect(result.participation?.kind).toBe("flow"); + return "fresh queue accepted"; + } finally { + operation.release(); + } + })().catch(String); + console.log(`TRIAGE ledger rejected-absent-id ${outcome}`); + expect(outcome).toBe("fresh queue accepted"); + } finally { + f.close(); + } +}); + +test("reservation triage exact rejected ledger identity remains negative without granting native-only", async () => { + const f = fixture(); + try { + f.inputs.reject(f.admission, new Error("Rejected private dispatch.")); + f.inputs.invalidate(host); + const operation = f.inputs.openSource(context("user-1")); + try { + const result = await operation.resolve( + [ + { + type: "user", + id: "user-1", + text: "Approved task", + time: { created: 0 }, + }, + ], + selected, + effects, + ); + expect(result.evidence).toEqual({ kind: "rejected" }); + expect(result.participation).toBeNull(); + console.log("TRIAGE ledger exact-rejected-id participation=null"); + } finally { + operation.release(); + } + } finally { + f.close(); + } +}); diff --git a/tests/lifecycle.test.ts b/tests/lifecycle.test.ts index cbd6334a..68fc44e1 100644 --- a/tests/lifecycle.test.ts +++ b/tests/lifecycle.test.ts @@ -6131,3 +6131,819 @@ for (const interveningPrimary of [false, true]) await f.lifecycle.dispose(); } }); + +type FailedReservationCase = + | "selected-read" + | "parent" + | "specialist" + | "missing-agent" + | "missing-model" + | "native-rpc-before-history" + | "native-rpc-after-history" + | "receipt" + | "activation" + | "review-child"; +async function triageRejectedCommand( + f: OwnershipFixture, + failure: FailedReservationCase, +) { + let restore = () => {}; + let failedID: string | undefined; + const before = f.native.filter((item) => item.type === "user").length; + let command = "flow-status"; + let expected = ""; + if (failure === "selected-read") { + f.failNextSessionRead(); + expected = "Native session read failed."; + } else if (failure === "parent") { + f.session.parentID = Session.ID.make("session-parent"); + restore = () => { + delete f.session.parentID; + }; + expected = "Flow commands require the native primary manager."; + } else if (failure === "specialist") { + f.session.agent = Agent.ID.make("flow-worker"); + restore = () => { + f.session.agent = agent; + }; + expected = "Flow commands require the native primary manager."; + } else if (failure === "missing-agent") { + delete f.session.agent; + restore = () => { + f.session.agent = agent; + }; + expected = "Flow commands require the native primary manager."; + } else if (failure === "missing-model") { + delete f.session.model; + restore = () => { + f.session.model = model; + }; + expected = "Flow requires the native selected agent and model."; + } else if (failure === "native-rpc-before-history") { + const prompt = spyOn(f.ctxSession, "prompt").mockImplementationOnce( + async (input) => { + failedID = input.id ?? undefined; + throw new Error("Native prompt rejected before materialization."); + }, + ); + restore = () => prompt.mockRestore(); + expected = "Native prompt rejected before materialization."; + } else if (failure === "native-rpc-after-history") { + const original = f.ctxSession.prompt; + const prompt = spyOn(f.ctxSession, "prompt").mockImplementationOnce( + async (...args) => { + failedID = args[0].id ?? undefined; + await original(...args); + throw new Error("Native prompt rejected after unknown acceptance."); + }, + ); + restore = () => prompt.mockRestore(); + expected = "Native prompt rejected after unknown acceptance."; + } else if (failure === "receipt") { + f.corruptReceipt(); + expected = "Flow admission receipt did not match its exact local payload."; + } else if (failure === "activation") { + const observe = spyOn(f.autoDrive, "observeMessage").mockRejectedValueOnce( + new Error("Accepted activation failed."), + ); + restore = () => observe.mockRestore(); + expected = "Accepted activation failed."; + } else { + command = "flow-review"; + const create = spyOn(f.ctxSession, "create").mockRejectedValueOnce( + new Error("Reviewer child creation failed."), + ); + restore = () => create.mockRestore(); + expected = "Reviewer child creation failed."; + } + try { + const outcome = await f + .command(command, "Inspect failed reservation") + .then(() => "accepted", String); + expect(outcome).toBe(`Error: ${expected}`); + console.log(`TRIAGE ${failure} command=${outcome}`); + } finally { + restore(); + } + const materialized = + f.native.filter((item) => item.type === "user").length - before; + expect(materialized).toBe( + [ + "native-rpc-after-history", + "receipt", + "activation", + "review-child", + ].includes(failure) + ? 1 + : 0, + ); + console.log(`TRIAGE ${failure} materialized=${materialized}`); + return { failedID }; +} +async function triageOrdinaryPredecessor(f: OwnershipFixture) { + await admitOrdinaryInput(f, "msg_triage_old", "Original ordinary request"); + await finishOwnedResponse(f, await startOwnedRequest(f)); + const oldCall = ownershipNativeCall(f); + const old = await f.lifecycle.verifyTool(oldCall, "flow"); + if (old.kind !== "flow") throw new Error("Missing ordinary predecessor."); + return { oldCall, old }; +} +async function triageFreshPublic(f: OwnershipFixture, mode: "queue" | "steer") { + const id = "msg_triage_fresh", + text = "Fresh ordinary request after rejection"; + await f.emit("prompt", { + sessionID, + messageID: SessionMessage.ID.make(id), + prompt: { text }, + delivery: mode, + }); + f.native.push({ type: "user", id, text, time: { created: 20 } }); + const context = f.context(); + const outcome = await f + .emit("context", context) + .then(() => "context accepted", String); + console.log(`TRIAGE ${mode} ${outcome}`); + expect(outcome).toBe("context accepted"); + expect(context.messages.at(-1)).toEqual({ + role: "user", + id, + content: [{ type: "text", text }], + }); + const request = new Request("http://localhost/triage-fresh", { + method: "POST", + body: text, + }); + const outbound = { + sessionID, + agent, + model, + kind: "primary" as const, + request, + }; + await f.emit("http.request", outbound); + expect(await outbound.request.clone().text()).toBe( + "Fresh ordinary request after rejection", + ); + await finishOwnedResponse(f, request); + const effects: string[] = []; + const tool = await effectTool(f, effects); + const freshCall = ownershipNativeCall(f); + expect(await tool.execute({}, freshCall)).toEqual({ + content: [{ type: "text", text: "applied" }], + }); + expect(effects).toEqual(["effect"]); + console.log( + `TRIAGE ${mode} fresh-model-body=Fresh ordinary request after rejection fresh-tool=applied`, + ); + return { freshCall, tool, effects }; +} +for (const failure of [ + "selected-read", + "parent", + "specialist", + "missing-agent", + "missing-model", + "native-rpc-before-history", + "native-rpc-after-history", + "receipt", + "activation", + "review-child", +] as const) + for (const mode of ["queue", "steer"] as const) + test(`reservation triage ${failure} then public ${mode} executes fresh exact tools`, async () => { + const f = await fixture(); + try { + const predecessor = await triageOrdinaryPredecessor(f); + await triageRejectedCommand(f, failure); + expect(() => predecessor.old.assertCurrent()).toThrow(); + const fresh = await triageFreshPublic(f, mode); + await expect( + fresh.tool.execute({}, predecessor.oldCall), + ).rejects.toThrow(); + expect(fresh.effects).toEqual(["effect"]); + expect(await fresh.tool.execute({}, fresh.freshCall)).toEqual({ + content: [{ type: "text", text: "applied" }], + }); + expect(fresh.effects).toEqual(["effect", "effect"]); + } finally { + await f.lifecycle.dispose(); + } + }); + +test("reservation triage static parse failure keeps predecessor usable and permits queue", async () => { + const f = await fixture(); + try { + const predecessor = await triageOrdinaryPredecessor(f); + await expect(f.command("flow-auto", "--recovery=shadow")).rejects.toThrow( + "Recovery requires", + ); + predecessor.old.assertCurrent(); + const effects: string[] = [], + tool = await effectTool(f, effects); + expect(await tool.execute({}, predecessor.oldCall)).toEqual({ + content: [{ type: "text", text: "applied" }], + }); + expect(effects).toEqual(["effect"]); + console.log("TRIAGE static-parse predecessor-tool=applied"); + await triageFreshPublic(f, "queue"); + } finally { + await f.lifecycle.dispose(); + } +}); + +test("reservation triage pending command read blocks old context and tools immediately", async () => { + const f = await fixture(); + let pending: Promise | undefined; + try { + const predecessor = await triageOrdinaryPredecessor(f); + const oldContext = f.context(); + f.blockNextSessionRead(); + f.rejectNextSessionRead(); + pending = f.command("flow-status", "Held command").then(() => null, String); + await f.waitSessionRead(); + await expect(f.emit("context", oldContext)).rejects.toThrow( + "epoch was revoked", + ); + expect(() => predecessor.old.assertCurrent()).toThrow(); + await expect( + f.lifecycle.verifyTool(predecessor.oldCall, "flow"), + ).rejects.toThrow(); + console.log("TRIAGE pending-read old-context=denied old-tool=denied"); + f.releaseSessionRead(); + expect(await pending).toBe("Error: Native session snapshot failed."); + } finally { + f.releaseSessionRead(); + await pending; + await f.lifecycle.dispose(); + } +}); + +test("reservation triage exact rejected private ID cannot replay as Flow or native-only", async () => { + const f = await fixture(); + try { + const { failedID } = await triageRejectedCommand( + f, + "native-rpc-before-history", + ); + if (!failedID) throw new Error("Missing rejected ID."); + await f.emit("prompt", { + sessionID, + messageID: SessionMessage.ID.make(failedID), + prompt: { text: "Replay private rejection" }, + delivery: "queue", + }); + f.native.push({ + type: "user", + id: failedID, + text: "Replay private rejection", + time: { created: 20 }, + }); + await expect(f.emit("context", f.context())).rejects.toThrow(); + await expect( + f.emit("http.request", { + sessionID, + agent, + model, + kind: "primary", + request: new Request("http://localhost/rejected-replay"), + }), + ).rejects.toThrow(); + await expect( + f.lifecycle.verifyTool(ownershipNativeCall(f), "flow"), + ).rejects.toThrow(); + console.log( + "TRIAGE exact-rejected-id context=denied model-request=denied Flow-tool=denied", + ); + } finally { + await f.lifecycle.dispose(); + } +}); + +for (const replacement of [ + "same-epoch-synthetic", + "new-epoch-command", +] as const) + test(`reservation triage late old RPC reject preserves ${replacement} frontier and readiness`, async () => { + const f = await fixture(); + let old: Promise | undefined; + const entered = Promise.withResolvers(), + gate = Promise.withResolvers(); + const original = f.ctxSession.synthetic; + let calls = 0; + const mock = spyOn(f.ctxSession, "synthetic").mockImplementation( + async (...args) => { + calls++; + if (calls === 1) { + entered.resolve(); + await gate.promise; + throw new Error("Old synthetic rejected before history."); + } + return original(...args); + }, + ); + try { + await initialOwnershipWork(f); + old = f.lifecycle + .prompt(sessionID, "Old synthetic dispatch", delivery, { + [FLOW_AUTO_METADATA_KEY]: "local-token", + }) + .then(() => null, String); + await entered.promise; + f.blockAdmission(); + const fresh = + replacement === "same-epoch-synthetic" + ? f.lifecycle.prompt( + sessionID, + "Fresh synthetic dispatch", + delivery, + { [FLOW_AUTO_METADATA_KEY]: "local-token" }, + ) + : f.command("flow-auto", "Fresh command dispatch"); + await f.flush(); + const authority = f.autoDrive.captureAuthority(sessionID); + const context = f + .emit("context", f.context()) + .then(() => "context accepted", String); + let settled = false; + void context.then(() => { + settled = true; + }); + await f.flush(); + expect(settled).toBe(false); + gate.resolve(); + expect(await old).toBe("Error: Old synthetic rejected before history."); + await f.flush(); + expect(settled).toBe(false); + expect(f.autoDrive.captureAuthority(sessionID)).toEqual(authority); + f.releaseAdmission(); + await fresh; + expect(await context).toBe("context accepted"); + const request = new Request("http://localhost/late-reject-fresh"); + await f.emit("http.request", { + sessionID, + agent, + model, + kind: "primary", + request, + }); + await finishOwnedResponse(f, request); + const effects: string[] = [], + tool = await effectTool(f, effects); + expect(await tool.execute({}, ownershipNativeCall(f))).toEqual({ + content: [{ type: "text", text: "applied" }], + }); + expect(effects).toEqual(["effect"]); + console.log( + `TRIAGE late-old-reject ${replacement} ready=preserved fresh-tool=applied`, + ); + } finally { + gate.resolve(); + f.releaseAdmission(); + await old; + mock.mockRestore(); + await f.lifecycle.dispose(); + } + }); + +test("reservation triage failed synthetic before history also permits fresh public queue", async () => { + const f = await fixture(); + const original = f.ctxSession.synthetic; + try { + await initialOwnershipWork(f); + f.ctxSession.synthetic = async () => { + throw new Error("Synthetic rejected before materialization."); + }; + await expect( + f.lifecycle.prompt(sessionID, "Failed synthetic", delivery, { + [FLOW_AUTO_METADATA_KEY]: "local-token", + }), + ).rejects.toThrow("Synthetic rejected before materialization."); + f.ctxSession.synthetic = original; + await triageFreshPublic(f, "queue"); + } finally { + f.ctxSession.synthetic = original; + await f.lifecycle.dispose(); + } +}); + +for (const reason of ["auto", "manual"] as const) + test(`reservation triage known automatic history plus failed private ID still refuses unowned ${reason} summary`, async () => { + const f = await fixture(); + try { + await initialOwnershipWork(f); + await triageRejectedCommand(f, "native-rpc-before-history"); + f.native.push({ + type: "user", + id: "msg_unregistered_suffix", + text: "External history is not an authority reset", + time: { created: 20 }, + }); + const input = f.context(); + f.native.push({ + ...automaticControl("msg_triage_negative_control"), + reason, + }); + await expect(f.emit("compaction", input)).rejects.toThrow(); + expect(f.repository.saveCount).toBe(0); + console.log(`TRIAGE known-automatic ${reason}-summary=denied`); + } finally { + await f.lifecycle.dispose(); + } + }); + +for (const changed of ["text", "metadata"] as const) + test(`reservation triage a rejected materialized private prefix retains its ${changed} guard`, async () => { + const f = await fixture(); + try { + await triageOrdinaryPredecessor(f); + await triageRejectedCommand(f, "native-rpc-after-history"); + const failed = f.native.findLast((item) => item.type === "user"); + if (failed?.type !== "user") + throw new Error("Missing materialized private prefix."); + if (changed === "text") failed.text = "Changed rejected dispatch"; + else failed.metadata = { forged: true }; + await admitOrdinaryInput( + f, + "msg_payload_guard_fresh", + "Fresh cannot launder changed history", + ); + await expect(f.emit("context", f.context())).rejects.toThrow( + "payload conflicts", + ); + expect(f.repository.saveCount).toBe(0); + console.log(`TRIAGE rejected-prefix changed-${changed}=denied`); + } finally { + await f.lifecycle.dispose(); + } + }); + +test("reservation triage public queue after rejection still refuses forged automatic metadata", async () => { + const f = await fixture(); + try { + await triageRejectedCommand(f, "selected-read"); + await expect( + f.emit("prompt", { + sessionID, + messageID: SessionMessage.ID.make("msg_forged_fresh"), + prompt: { text: "Forged fresh" }, + delivery: "queue", + metadata: { [FLOW_AUTO_METADATA_KEY]: "forged-token" }, + }), + ).rejects.toThrow("private admitted dispatch"); + expect(f.repository.saveCount).toBe(0); + console.log("TRIAGE forged-public-auto-metadata=denied"); + } finally { + await f.lifecycle.dispose(); + } +}); + +test("reservation triage failed private RPC cannot veto a fresh native-only user in the same ledger", async () => { + const f = await fixture(); + try { + await triageRejectedCommand(f, "native-rpc-before-history"); + f.native.push({ + type: "user", + id: "msg_triage_native_only", + text: "Fresh native-only input", + time: { created: 30 }, + }); + await exerciseUnregisteredNativeExecution(f, [ + { + role: "user", + id: "msg_triage_native_only", + content: [{ type: "text", text: "Fresh native-only input" }], + }, + ]); + console.log( + "TRIAGE fresh-native-only ordinary-tool=allowed Flow-tool=denied", + ); + } finally { + await f.lifecycle.dispose(); + } +}); + +for (const mode of ["queue", "steer"] as const) + test(`candidate B claimed real activation failure then fresh ${mode}`, async () => { + const f = await fixture(); + let reject: ReturnType | undefined; + try { + const prior = await triageOrdinaryPredecessor(f); + await admitOrdinaryInput( + f, + "msg_b_real_failed", + "Consumed real activation fails", + ); + reject = spyOn(f.autoDrive, "observeMessage").mockRejectedValueOnce( + new Error("Real activation failed after claim."), + ); + const failed = await f + .emit("context", f.context()) + .then(() => "unexpected accepted", String); + expect(failed).toBe("Error: Real activation failed after claim."); + expect(reject.mock.calls[0]?.[3]).toBe("msg_b_real_failed"); + expect( + f.native.filter((m) => m.type === "user").map((m) => m.id), + ).toEqual(["msg_triage_old", "msg_b_real_failed"]); + expect(() => prior.old.assertCurrent()).toThrow(); + reject.mockRestore(); + console.log( + `CANDIDATE_B claimed-real native-ID-present=true local-activation=failed old-clock=revoked next=${mode}`, + ); + const fresh = await triageFreshPublic(f, mode); + await expect(fresh.tool.execute({}, prior.oldCall)).rejects.toThrow(); + expect(fresh.effects).toEqual(["effect"]); + } finally { + reject?.mockRestore(); + await f.lifecycle.dispose(); + } + }); + +for (const changed of ["none", "text", "metadata", "last"] as const) + test(`candidate B materialized rejected private prefix ${changed}`, async () => { + const f = await fixture(); + const original = f.ctxSession.prompt; + let failedID: string | undefined; + const prompt = spyOn(f.ctxSession, "prompt").mockImplementationOnce( + async (...args) => { + failedID = args[0].id ?? undefined; + await original(...args); + throw new Error("Locally rejected after native materialization."); + }, + ); + try { + await expect( + f.command("flow-status", "Rejected exact payload"), + ).rejects.toThrow("Locally rejected after native materialization."); + prompt.mockRestore(); + const failed = f.native.find((m) => m.id === failedID); + if (failed?.type !== "user") + throw new Error("Expected real fixture materialization."); + expect(failed.text).toContain("Rejected exact payload"); + if (changed === "text") failed.text = "Tampered private text"; + if (changed === "metadata") failed.metadata = { changed: true }; + console.log( + `CANDIDATE_B materialized-private id=${failedID} state=locally-rejected variant=${changed}`, + ); + if (changed === "none") await triageFreshPublic(f, "queue"); + else { + if (changed !== "last") + await admitOrdinaryInput(f, "msg_b_guard_fresh", "Fresh exact queue"); + await expect(f.emit("context", f.context())).rejects.toThrow( + changed === "last" + ? "unknown, synthetic, or stale" + : "payload conflicts", + ); + await expect( + f.emit("http.request", { + sessionID, + agent, + model, + kind: "primary", + request: new Request("http://localhost/b-no-request"), + }), + ).rejects.toThrow(); + expect(f.repository.saveCount).toBe(0); + } + } finally { + prompt.mockRestore(); + await f.lifecycle.dispose(); + } + }); + +for (const type of ["user", "synthetic"] as const) + test(`candidate B fresh unregistered ${type} after failed private keeps compaction negative`, async () => { + const f = await fixture(); + try { + await triageRejectedCommand(f, "native-rpc-before-history"); + const id = "msg_b_native_fresh", + text = "Different exact native-only source"; + f.native.push({ type, id, text, time: { created: 30 } }); + const compaction = f.context(); + const control = { + ...automaticControl("msg_b_native_manual"), + reason: "manual" as const, + }; + f.native.push(control); + await expect(f.emit("compaction", compaction)).rejects.toThrow(); + f.native.splice(f.native.indexOf(control), 1); + console.log(`CANDIDATE_B native-only type=${type} unowned-manual=denied`); + await exerciseUnregisteredNativeExecution(f, [ + { role: "user", id, content: [{ type: "text", text }] }, + ]); + console.log( + `CANDIDATE_B native-only type=${type} ordinary=allowed Flow=denied`, + ); + } finally { + await f.lifecycle.dispose(); + } + }); + +import { mkdtempSync, realpathSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { createTools as candidateRealTools } from "../src/platform/opencode/tools.js"; + +async function candidateStatusTool( + f: OwnershipFixture, + workspace: typeof directory, +) { + const registered = new Map(); + const validation = new ValidationCaptureCoordinator({ + readSession: async () => f.session, + persistObservation: async () => { + throw new Error("Status must not persist validation."); + }, + }); + const status = candidateRealTools({ + validation, + prepareValidation: async () => { + throw new Error("Status must not prepare validation."); + }, + }).flow_status; + if (!status) throw new Error("Missing real status definition."); + await registerFlowTools( + { + location: { directory: workspace }, + permission: { assert: async () => {} }, + tool: { + transform: async (apply) => { + apply({ + list: () => [...registered].map(([id, tool]) => ({ id, ...tool })), + get: (id) => { + const tool = registered.get(id); + return tool ? { id, ...tool } : undefined; + }, + namespace() {}, + add: (tool) => { + registered.set(tool.name, tool); + }, + update: (id, update) => { + const tool = registered.get(id); + if (tool) update(tool); + }, + remove: (id) => { + registered.delete(id); + }, + }); + return { + dispose: async () => { + registered.clear(); + }, + }; + }, + }, + }, + { flow_status: status }, + async (context) => { + await f.lifecycle.verifyTool(context, "flow"); + }, + ); + const tool = registered.get("flow_status"); + if (!tool) throw new Error("Missing real registered status wrapper."); + return tool; +} +for (const mode of ["queue", "steer"] as const) + test(`candidate B failed parent command then ${mode} executes actual tracked application flow_status`, async () => { + const f = await fixture(); + const workspace = AbsolutePath.make( + realpathSync(mkdtempSync(join(tmpdir(), "flow-reservation-status-"))), + ); + try { + const old = await triageOrdinaryPredecessor(f); + await triageRejectedCommand(f, "parent"); + const fresh = await triageFreshPublic(f, mode); + const tool = await candidateStatusTool(f, workspace); + const result = await tool.execute( + { request: { view: "compact" } }, + fresh.freshCall, + ); + const content = result.content; + const part = + typeof content === "string" + ? undefined + : content?.find((part) => part.type === "text"); + const text = + typeof content === "string" + ? content + : part?.type === "text" + ? part.text + : undefined; + if (text === undefined) throw new Error("Missing real Flow status text."); + const response = JSON.parse(text); + expect(response.status).toBe("ok"); + expect(response.summary).toBe("No active Flow session."); + await expect( + tool.execute({ request: { view: "compact" } }, old.oldCall), + ).rejects.toThrow(); + expect(f.repository.saveCount).toBe(0); + console.log( + `CANDIDATE_B actual-flow-status mode=${mode} status=ok summary=No active Flow session. old-call=denied`, + ); + } finally { + await f.lifecycle + .dispose() + .finally(() => rmSync(workspace, { recursive: true, force: true })); + } + }); + +for (const type of ["user", "synthetic"] as const) + test(`candidate B materialized rejected prefix then fresh native-only ${type}`, async () => { + const f = await fixture(); + try { + const { failedID } = await triageRejectedCommand( + f, + "native-rpc-after-history", + ); + const failed = f.native.find((m) => m.id === failedID); + if (failed?.type !== "user") + throw new Error("Expected materialized failed prefix."); + const id = "msg_b_materialized_native", + text = "Different native-only source after materialized rejection"; + f.native.push({ type, id, text, time: { created: 30 } }); + const compaction = f.context(); + const control = { + ...automaticControl("msg_b_materialized_manual"), + reason: "manual" as const, + }; + f.native.push(control); + await expect(f.emit("compaction", compaction)).rejects.toThrow(); + f.native.splice(f.native.indexOf(control), 1); + console.log( + `CANDIDATE_B materialized native-only type=${type} manual-summary=denied`, + ); + await exerciseUnregisteredNativeExecution(f, [ + { + role: "user", + id: failed.id, + content: [{ type: "text", text: failed.text }], + }, + { role: "user", id, content: [{ type: "text", text }] }, + ]); + } finally { + await f.lifecycle.dispose(); + } + }); + +test("parent checked ordinary boundary resets unowned summary lineage without executing its summary", async () => { + const f = await fixture(); + try { + await initialOwnershipWork(f); + await triageRejectedCommand(f, "native-rpc-before-history"); + await triageFreshPublic(f, "steer"); + expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); + f.failNextSessionRead(); + await expect(f.emit("context", f.context())).rejects.toThrow( + "Native session read failed.", + ); + f.native.push({ + type: "user", + id: "msg_parent_unregistered", + text: "Unregistered ordinary suffix", + time: { created: 40 }, + }); + const input = f.context(); + const control = { + ...automaticControl("msg_parent_summary"), + reason: "manual" as const, + }; + f.native.push(control); + await f.emit("compaction", input); + expect(f.repository.saveCount).toBe(0); + f.native.splice(0, f.native.length, { ...control, status: "completed" }); + await expect(f.emit("context", f.context())).rejects.toThrow( + "unknown, synthetic, or stale", + ); + console.log( + "PARENT scoped-history manual-summary=accepted summary-execution=refused", + ); + } finally { + await f.lifecycle.dispose(); + } +}); + +test("parent consecutive public queues after a checked checkpoint need no stale bridge", async () => { + const f = await fixture(); + try { + await initialOwnershipWork(f); + await compactCurrentInput(f, "msg_parent_checked_checkpoint"); + await finishOwnedResponse(f, await startOwnedRequest(f)); + await admitOrdinaryInput( + f, + "msg_parent_queue_one", + "First ordinary after checkpoint", + ); + await finishOwnedResponse(f, await startOwnedRequest(f)); + await admitOrdinaryInput( + f, + "msg_parent_queue_two", + "Second ordinary after checkpoint", + ); + const result = await f + .emit("context", f.context()) + .then(() => "accepted", String); + console.log(`PARENT consecutive-after-checkpoint=${result}`); + expect(result).toBe("accepted"); + } finally { + await f.lifecycle.dispose(); + } +}); From 40356323000840fdfc3341294e06ae1a98d9b4f0 Mon Sep 17 00:00:00 2001 From: Douwe de Vries Date: Sun, 25 Oct 2026 06:56:30 +0100 Subject: [PATCH 15/17] fix(opencode): recover checked inputs after failed reservations --- .agents/plans/18-opencode-v2/decisions.tsv | 5 + .../failed-reservation-review.md | 95 ++ .../opencode/execution-attribution.ts | 29 +- src/platform/opencode/input-admission.ts | 468 +++--- src/platform/opencode/input-evidence.ts | 371 +++++ src/platform/opencode/lifecycle.ts | 122 +- tests/architecture-boundaries.test.ts | 26 + tests/execution-attribution.test.ts | 90 ++ tests/input-admission.test.ts | 1286 ++++++++++++++++- tests/lifecycle.test.ts | 749 +++++++++- 10 files changed, 2832 insertions(+), 409 deletions(-) create mode 100644 .agents/plans/18-opencode-v2/failed-reservation-review.md create mode 100644 src/platform/opencode/input-evidence.ts diff --git a/.agents/plans/18-opencode-v2/decisions.tsv b/.agents/plans/18-opencode-v2/decisions.tsv index 67e1eb93..f66b6b56 100644 --- a/.agents/plans/18-opencode-v2/decisions.tsv +++ b/.agents/plans/18-opencode-v2/decisions.tsv @@ -74,3 +74,8 @@ ts phase decision why evidence result 2026-10-24T17:56:12Z gate Hold the v2 push because merged main audit is red The operator explicitly required green main before publishing the v2 PR; existing v1 Zod and TOML dependencies now fail audit while the v2 dependency graph audits clean /tmp/flow-v2-release-order-20261024/main-dependency-audit.log; /tmp/flow-v2-release-order-20261024/v2-current-audit.log; https://github.com/ddv1982/flow-opencode/actions/runs/40118156331 Both dependency PRs merged; v2 remains local; no audit suppression or further paid work 2026-10-25T02:17:33Z queued-input-review Separate queued preparation from exact consumed provenance Native reproduction confirmed premature epoch revocation; four independent review defects were corrected without expanding automatic authority .agents/plans/18-opencode-v2/queued-input-review.md; /tmp/flow-v2-queued-input-20261025/core-review-r2/review.md; /tmp/flow-v2-queued-input-20261025/dispatch-review-r2/review.md Fresh source reviews and full check pass; exact archive/native replay still required; paid qualification remains on hold 2026-10-25T02:59:54Z capture-error-reporting Restore a static v2 completed-result failure notice Real armed capture persistence failure was logged but invisible to the model; preserve native errors and stale null refusals .agents/plans/18-opencode-v2/capture-error-reporting.md; /tmp/flow-v2-capture-error-20261025/parent-native/before-accepted.json; /tmp/flow-v2-capture-error-20261025/source-review-r1/review.md Red-first 3a51aba7; focused and independent reviews pass; new exact native after and contribution gates remain parent proof; paid qualification on hold +2026-10-25T05:47:34Z failed-reservation-design Select retained failed identity with checked successor consumption A permanent host history flag would reject established ordinary summary behavior /tmp/flow-v2-failed-reservation-20261025/crossjudge-r1/report.md; /tmp/flow-v2-failed-reservation-20261025/synthesis-r1/decision.md B selected with atomic cleanup, stable phase and lazy bridge repairs +2026-10-25T05:47:35Z failed-reservation-review Reject the first candidate for a late same-ID registration race One-time entry validation did not remain valid through awaited freeze and tool use /tmp/flow-v2-failed-reservation-20261025/source-review-r1/report.md; /tmp/flow-v2-failed-reservation-20261025/parent/source-r1-confirmation/observed-bypass.log aecbabe1 remains rejected; earlier runtime passes retained with their original bounds +2026-10-25T05:47:35Z failed-reservation-repair Retain exact terminal scope identity through freeze and effects Registration must not turn a captured unknown source into unchecked registered execution /tmp/flow-v2-failed-reservation-20261025/implementation-r1/review-r1-fixes/paired-source-test-proof.json; /tmp/flow-v2-failed-reservation-20261025/source-review-r2/report.md Fresh source and comment reviews pass; full check 5161 pass, 27 skip, zero fail; exact new package proof required +2026-10-25T05:47:35Z failed-reservation-scope Preserve the characterized ordinary-native nonterminal policy The same behavior exists in the original implementation and shows no terminal or Flow authority bypass /tmp/flow-v2-failed-reservation-20261025/source-review-r2/report.md; .agents/plans/18-opencode-v2/failed-reservation-review.md No broader admission guarantee or policy expansion; paid qualification remains on hold +2026-10-25T05:52:56Z failed-reservation-proof Add direct proof pointers for the repair checkpoint and exact corrected package Supersede row 79 evidence omissions without rewriting its historical result /tmp/flow-v2-failed-reservation-20261025/source-review-r2/report.md; /tmp/flow-v2-failed-reservation-20261025/comment-review-r2/report.md; /tmp/flow-v2-failed-reservation-20261025/parent/check-r2/check.log; /tmp/flow-v2-failed-reservation-20261025/parent-native/after-r2-recovered-verification.json; /tmp/flow-v2-failed-reservation-20261025/candidate-r2/smoke/command.json; /tmp/flow-v2-failed-reservation-20261025/trail-review-r1/review.md Source and comment reviews pass; full check 5161 pass, 27 skip, zero fail; af10177f native 23 checks and packed smoke 119 assertions pass; paid qualification on hold diff --git a/.agents/plans/18-opencode-v2/failed-reservation-review.md b/.agents/plans/18-opencode-v2/failed-reservation-review.md new file mode 100644 index 00000000..ae251fbd --- /dev/null +++ b/.agents/plans/18-opencode-v2/failed-reservation-review.md @@ -0,0 +1,95 @@ +# Failed command reservation recovery + +A failed Flow command can leave a private input ID outside native history. +The next ordinary queued input then fails before provider dispatch because the +input ledger still requires that failed ID in the consumed prefix. Explicit +steer recovers the session. Pinned OpenCode 2.1.4 reproduced this for both a +specialist root and a child session. This is a recoverable availability defect. + +## Ownership and recovery + +InputAdmissions remains the only owner of entries, reservations, epochs, scopes +and operations. The subordinate input-evidence module owns entry shapes and pure +failure, coverage, history, receipt and payload decisions. Its import fence permits +only the ledger to use it. The source ceiling remains 1,000 lines. + +A local terminal failure settles only its exact pending admission and records a +failure fact on that entry. Only an exact current reservation and epoch permit +invalidation and captured lifecycle cleanup. Accepted parents retain their accepted +receipt and resolved readiness after reviewer-child failure. Failed real activation +retains its final reservation and operation token. Earlier settled batch members +remain settled. No cleanup selects a replacement owner by host. + +The failed identity stays rejected. A different registered queue must prove its +own complete native prefix, selected delivery, literal payload and metadata, reuse, +ordering and prior primary before gaining a new origin. A distinct unknown native +user or synthetic input can receive ordinary execution only. It gains no Flow +origin, automatic lease or recovery intent and preserves negative compaction +history. A genuinely checked ordinary boundary can reset the history anchor. +Exact old failed IDs remain negative afterward. There is no permanent host taint. + +Pending commands still create an immediate barrier before selected-session reads. +Static parse errors still precede reservation. Command post-reservation failures +share one catch. Synthetic selected and automatic-intake checks remain before its +reservation. No RPC rejection is treated as proof of native absence. + +Private records have one stable handle and one replaceable phase. Acceptance +captures the pending phase around activation and rechecks that same phase before +settling. No mirrored flat state, compatibility getter or second registry is kept. + +## Review corrections + +The changing-prefix guard requests a compaction bridge lazily. It asks only when +the existing strict predicate needs a bridge. Both lifecycle callers use this +rule. Direct prefix extension avoids an irrelevant retired checkpoint. Actual +checkpoint-only execution and required bridges retain their live-origin guards. + +Independent review rejected the first source candidate for a late same-ID queue +registration race. Its once-only entry check could expire between resolve and +freeze. The corrected terminal scope retains its exact final entry, registration +and failure fact through release, freeze, HTTP and tool use. Cancellation wakes +only the exact affected stale source. Unrelated queued IDs retain their clocks. +The earlier archive and its bounded runtime passes remain historical evidence, +not acceptance of that rejected source. + +One old settled-receipt test required a distinct external suffix to fail outright. +The selected contract instead permits ordinary execution with no Flow authority. +The pending row still refuses. Settled user and synthetic rows now verify the +receipt failure, exact private prefix, ordinary HTTP and shell passage, zero Flow +effects, off recovery and both compaction denials after execution. This narrow +reconciliation is separate from failed-ID replay, which remains refused. + +The independent review also compared nonterminal and nonfinal unknown-input timing +controls with the original implementation. Those ordinary-native behaviors remain +unchanged. This repair is not a general input-admission arbitration guarantee. + +## Evidence and limits + +Red commit 7198573ab666eac9ca0f4524e33ce84cccc5d3f2 preserves the failing recovery +regressions before the fix. Its selected matrix had 32 passes and 16 failures. +All 48 recovery rows pass on the corrected source. The registration correction +also has a paired source replay with identical final tests, 7 passes and 70 failures +on the prior source versus 77 passes on the corrected source. + +Fresh independent source and comment reviews pass. The parent full repository +check on Bun 1.4.0 has 5,161 passes, 27 skips and zero failures. The focused owner +run has 702 distinct passing tests. Subset reruns are not additional distinct tests. +Both new paths have literal application and zero-effect denial controls. + +The native acceptance contract reuses the exact BEFORE driver on a new archive. +Both invalid commands must still reject. Each same-session queued input, both +steer controls and the fresh queue must complete with one fixture request each, +five total. The maintained packed smoke has 119 assertions. Parent byte checks +bind the archive, installed files, source maps and frozen sources before and after. +The current exact archive and resulting native receipts belong in the PR evidence. + +Local evidence is under /tmp/flow-v2-failed-reservation-20261025. The selected design +is synthesis-r1/decision.md, with the explicit baseline amendment in +synthesis-r1/baseline-reconciliation.md. Source-review-r2/report.md records the +independent verdict and retained ordinary-native characterization. Implementation +and paired replay receipts are in implementation-r1/review-r1-fixes. + +Native overlap, auxiliary retry, public delivery defaults, runtime frequency and +broader L1 arbitration remain unqualified. The historical paid failure stays +unresolved. Paid qualification and release work remain on hold. This fix does not +merge PR 197, publish Flow 10 or qualify a real provider. diff --git a/src/platform/opencode/execution-attribution.ts b/src/platform/opencode/execution-attribution.ts index 294b231a..ca4e3481 100644 --- a/src/platform/opencode/execution-attribution.ts +++ b/src/platform/opencode/execution-attribution.ts @@ -234,7 +234,7 @@ export class ExecutionAttribution { native: readonly SessionMessageInfo[], expected: PrimaryOwner | undefined, changingPublicSource: boolean, - bridge?: CompletedPrefix, + resolveBridge?: () => CompletedPrefix | undefined, ): Replacement { this.assertExpected(input.sessionID, expected); const inputs = modelInputIDs(input), @@ -247,18 +247,23 @@ export class ExecutionAttribution { if ( changingPublicSource && previous?.clock.current() && - !same(inputs.slice(0, previous.inputs.length), previous.inputs) && - !( - bridge && - expected?.participation.kind === "flow" && - bridge.sourceOrigin === expected.participation.origin && - same(bridge.consumed, previous.inputs) && - inputs[0] === bridge.checkpoint + !same(inputs.slice(0, previous.inputs.length), previous.inputs) + ) { + const bridge = + expected?.participation.kind === "flow" ? resolveBridge?.() : undefined; + if ( + !( + bridge && + expected?.participation.kind === "flow" && + bridge.sourceOrigin === expected.participation.origin && + same(bridge.consumed, previous.inputs) && + inputs[0] === bridge.checkpoint + ) ) - ) - throw new Error( - "Flow new public input does not extend the frozen consumed prefix.", - ); + throw new Error( + "Flow new public input does not extend the frozen consumed prefix.", + ); + } const retry = !!expected && this.isCurrent(expected) && diff --git a/src/platform/opencode/input-admission.ts b/src/platform/opencode/input-admission.ts index 09ebdd66..c8e16d19 100644 --- a/src/platform/opencode/input-admission.ts +++ b/src/platform/opencode/input-admission.ts @@ -1,19 +1,15 @@ import { isDeepStrictEqual as same } from "node:util"; import type { - OpenCodeEvent, SessionInboxSynthetic, SessionInboxUser, SessionInfo, SessionMessageInfo, - SessionPromptInput, - SessionSyntheticInput, } from "@opencode/client"; import type { SessionContext, SessionPrompt, } from "@opencode/plugin/promise/session"; import type { AutoDriveAuthority, AutoDriveDelivery } from "./auto-drive.js"; -import { nativePromptPayload } from "./command-hook.js"; import { type InputOrigin, modelInputIDs, @@ -23,93 +19,23 @@ import { sameDelivery, } from "./execution-attribution.js"; -type Payload = - | Omit - | Omit; -type InputReservation = Readonly<{ - host: string; - id: string; - epoch: number; -}>; -type Admission = { - kind: "private"; - reservation: InputReservation; - type: "user" | "synthetic"; - payload: Payload; - acceptedPayload?: - | SessionInboxUser["payload"] - | SessionInboxSynthetic["payload"]; - ready: Promise; - settle(origin: InputOrigin): void; - fail(error: Error): void; - state: "pending" | "accepted" | "rejected"; - origin: InputOrigin | null; -}; -type InboxEvent = Extract< - OpenCodeEvent, - { - type: - | "session.inbox.enqueued" - | "session.inbox.cancelled" - | "session.inbox.delivered" - | "session.inbox.delivery.changed"; - } ->; -type InboxEnqueued = Extract; -type EnqueuedUserPayload = Extract< - InboxEnqueued["data"]["item"], - { type: "user" } ->["payload"]; -type Observation = { - seq: number; - generation: number | null; - item: InboxEnqueued["data"]["item"] | null; - state: "pending" | "delivered" | "cancelled" | "unseen" | "consumed"; -}; -type RealState = - | Readonly<{ kind: "deferred" }> - | Readonly<{ kind: "immediate"; reservation: InputReservation }> - | Readonly<{ - kind: "activating"; - token: symbol; - reservation: InputReservation | null; - }> - | Readonly<{ - kind: "settled"; - reservation: InputReservation | null; - origin: InputOrigin | null; - automaticHistory: boolean; - }> - | Readonly<{ kind: "failed" }>; -type Real = { - kind: "real"; - registration: symbol; - prompt: SessionPrompt["prompt"]; - metadata: SessionPrompt["metadata"]; - state: RealState; - observation: Observation | null; - reuse: { cancelled: number; hook: boolean } | null; -}; -type Alias = Readonly<{ - kind: "alias"; - source: Entry | undefined; - origin: InputOrigin | null; -}>; -type Entry = - | Admission - | Real - | { kind: "observed"; observation: Observation } - | Alias - | Readonly<{ kind: "reserved"; reservation: InputReservation }>; -type History = - | "retired-ordinary" - | "retired-automatic" - | "unknown-history" - | "rejected"; -type SourceEvidence = - | Readonly<{ kind: "unregistered"; lineage: History }> - | Readonly<{ kind: "live"; origin: InputOrigin }> - | Readonly<{ kind: History }>; +import { + type Admission, + checkConsumed, + checkReceipt, + type Entry, + type FailureFact, + facts, + type InboxEvent, + type InputReservation, + matchesReal, + type Observation, + type Payload, + type Real, + type SourceEvidence, + terminalize, +} from "./input-evidence.js"; + export type InputScope = Readonly<{ host: string; consumed: readonly string[]; @@ -145,6 +71,9 @@ export type SourceOperation = Readonly<{ type ScopeState = Readonly<{ entry: Entry | undefined; frontierEntry: Entry | undefined; + gate: + | Readonly<{ kind: "covered"; failure: FailureFact | null }> + | Readonly<{ kind: "inspection"; failure: FailureFact }>; cancel(error: Error): void; }>; const revoked = () => new Error("Flow execution epoch was revoked."); @@ -179,9 +108,13 @@ export class InputAdmissions { epoch(host: string): number { return this.#epochs.get(host) ?? 0; } - #cancel(host: string) { + #cancel(host: string, source?: string) { for (const [scope, state] of this.#scopes) - if (scope.host === host) state.cancel(revoked()); + if ( + scope.host === host && + (source === undefined || (scope.source === source && !scope.current())) + ) + state.cancel(revoked()); } invalidate(host: string): void { this.#epochs.set(host, this.epoch(host) + 1); @@ -233,8 +166,7 @@ export class InputAdmissions { ready, settle, fail, - state: "pending", - origin: null, + phase: { state: "pending" }, }; this.#entries.set(key, admission); return admission; @@ -244,41 +176,75 @@ export class InputAdmissions { receipt: SessionInboxUser | SessionInboxSynthetic, activate: () => Promise, ): Promise { - if (admission.state !== "pending") + if (admission.phase.state !== "pending") throw new Error("Flow admission was already settled."); try { this.assertReservation(admission.reservation); - if ( - receipt.sessionID !== admission.reservation.host || - receipt.id !== admission.reservation.id || - receipt.type !== admission.type || - !matchesAdmissionPayload(admission.payload, receipt.payload) - ) - throw new Error( - "Flow admission receipt did not match its exact local payload.", - ); - admission.acceptedPayload = structuredClone(receipt.payload); + checkReceipt(admission, receipt); + const acceptedPayload = structuredClone(receipt.payload); + const pending: Extract = { + state: "pending", + acceptedPayload, + }; + admission.phase = pending; const origin = await activate(); this.assertReservation(admission.reservation); this.assertLive(origin); if (origin.epoch !== admission.reservation.epoch) throw new Error("Flow admission epoch changed before acceptance."); - admission.origin = origin; - admission.state = "accepted"; + if (admission.phase !== pending) throw revoked(); + admission.phase = { + state: "accepted", + acceptedPayload, + origin, + failure: null, + }; admission.settle(origin); } catch (error) { - this.reject(admission, error); + this.#reject(admission, error); throw error; } } - reject(admission: Admission, error: unknown): void { - if (admission.state !== "pending") return; - admission.state = "rejected"; - admission.fail(error instanceof Error ? error : new Error(String(error))); + #reject(admission: Admission, error: unknown): void { + if (admission.phase.state !== "pending") return; + const next = terminalize(admission, { + kind: "reservation", + reservation: admission.reservation, + }); + if (next?.kind !== "private") return; + admission.phase = next.phase; + if (next.reject) + admission.fail(error instanceof Error ? error : new Error(String(error))); + } + finishReservation(reservation: InputReservation, error: unknown): boolean { + const key = this.#key(reservation.host, reservation.id), + entry = this.#entries.get(key); + if (!entry) return false; + const next = terminalize(entry, { kind: "reservation", reservation }); + if (!next) return false; + if (next.kind === "reserved") this.#entries.set(key, next.next); + if (next.kind === "private" && entry.kind === "private") { + entry.phase = next.phase; + if (next.reject) + entry.fail(error instanceof Error ? error : new Error(String(error))); + } + if ( + this.#closed || + this.#deleted.has(reservation.host) || + this.#frontier.get(reservation.host) !== reservation || + this.epoch(reservation.host) !== reservation.epoch + ) + return false; + this.invalidate(reservation.host); + return true; } hasPrivate(host: string, id: string): boolean { const entry = this.#entries.get(this.#key(host, id)); - return entry?.kind === "private" || entry?.kind === "reserved"; + return ( + entry?.kind === "private" || + entry?.kind === "reserved" || + entry?.kind === "failed-reservation" + ); } observePrompt(input: SessionPrompt, immediate: () => void): void { this.#options.assertOpen(); @@ -294,6 +260,7 @@ export class InputAdmissions { if (prior.reuse && !prior.reuse.hook && prior.state.kind !== "settled") { prior.reuse.hook = true; prior.registration = Symbol(); + this.#cancel(input.sessionID, input.messageID); } return; } @@ -322,6 +289,7 @@ export class InputAdmissions { }; } this.#entries.set(key, real); + this.#cancel(input.sessionID, input.messageID); } observeInbox( event: InboxEvent, @@ -359,6 +327,7 @@ export class InputAdmissions { if (!entry) { entry = { kind: "observed", observation }; this.#entries.set(key, entry); + this.#cancel(host, id); } else entry.observation = observation; if (event.type === "session.inbox.enqueued") { observation.generation = event.durable.seq; @@ -389,7 +358,7 @@ export class InputAdmissions { entry.kind === "real" && entry.state.kind === "deferred" && observation.item.type === "user" && - this.#matchesPayload(entry, observation.item.payload) && + matchesReal(entry, observation.item.payload) && (!entry.reuse || (entry.reuse.hook && observation.generation > entry.reuse.cancelled)) @@ -445,16 +414,6 @@ export class InputAdmissions { }; return Object.freeze({ current, assertCurrent }); } - #covers(entry: Entry | undefined, frontier: InputReservation): boolean { - return entry?.kind === "alias" - ? this.#covers(entry.source, frontier) - : entry?.kind === "real" - ? (entry.state.kind === "immediate" || - entry.state.kind === "activating" || - entry.state.kind === "settled") && - entry.state.reservation === frontier - : entry?.kind !== "observed" && entry?.reservation === frontier; - } #capture(input: Pick): InputScope { this.#options.assertOpen(); const host = input.sessionID, @@ -462,18 +421,45 @@ export class InputAdmissions { source = consumed.at(-1); if (!source) throw new Error("Flow primary context has no attributable input."); - const frontier = this.#frontier.get(host); - if ( - frontier && - !consumed.some((id) => - this.#covers(this.#entries.get(this.#key(host, id)), frontier), - ) - ) - throw revoked(); - const entry = this.#entries.get(this.#key(host, source)); + const frontier = this.#frontier.get(host), + frontierEntry = frontier + ? this.#entries.get(this.#key(host, frontier.id)) + : undefined, + entry = this.#entries.get(this.#key(host, source)), + failure = facts(frontierEntry).failure; + const terminal = + failure?.reservation === frontier && + source !== frontier?.id && + (!entry || + entry.kind === "observed" || + (entry.kind === "real" && entry.state.kind === "deferred")) + ? failure + : null; + const covered = + !frontier || + consumed.some( + (id) => + facts(this.#entries.get(this.#key(host, id))).coverage === frontier, + ); + let gate: ScopeState["gate"]; + if (covered) gate = { kind: "covered", failure: terminal }; + else if (terminal) gate = { kind: "inspection", failure: terminal }; + else throw revoked(); const cancelled = Promise.withResolvers(); void cancelled.promise.catch(() => {}); - const { current, assertCurrent } = this.captureBarrier(host); + const barrier = this.captureBarrier(host), + registration = entry?.kind === "real" ? entry.registration : undefined; + const current = () => + barrier.current() && + (!terminal || + (facts(this.#entries.get(this.#key(host, terminal.reservation.id))) + .failure === terminal && + this.#entries.get(this.#key(host, source)) === entry && + (entry?.kind !== "real" || entry.registration === registration))); + const assertCurrent = () => { + this.#options.assertOpen(); + if (!current()) throw revoked(); + }; const scope: InputScope = { host, consumed, @@ -487,7 +473,7 @@ export class InputAdmissions { return result; }, ready: async () => { - if (entry?.kind === "private" && entry.state !== "rejected") + if (entry?.kind === "private" && entry.phase.state !== "rejected") await scope.wait(entry.ready); assertCurrent(); }, @@ -497,25 +483,12 @@ export class InputAdmissions { }; this.#scopes.set(scope, { entry, - frontierEntry: frontier - ? this.#entries.get(this.#key(host, frontier.id)) - : undefined, + frontierEntry, + gate, cancel: cancelled.reject, }); return scope; } - #matchesPayload( - entry: Real, - payload: SessionInboxUser["payload"] | EnqueuedUserPayload, - ): boolean { - return ( - same(entry.metadata ?? {}, payload.metadata ?? {}) && - matchesAdmissionPayload(nativePromptPayload(entry.prompt), payload, false) - ); - } - #matchesReal(entry: Real, message: SessionMessageInfo): boolean { - return message.type === "user" && this.#matchesPayload(entry, message); - } openSource( input: Pick, ): SourceOperation { @@ -541,13 +514,16 @@ export class InputAdmissions { const registrations = fresh.map(({ entry }) => entry.registration); if (fresh.length) this.#operations.set(token, host); const final = captured.at(-1)?.entry; + const capturedState = this.#scopes.get(scope); + let claimed: InputReservation | undefined; let successor: InputScope | undefined, owned = false, resolved = false; const failClaimed = () => { - for (const { entry } of fresh) - if (entry.state.kind === "activating" && entry.state.token === token) - entry.state = { kind: "failed" }; + for (const { entry } of fresh) { + const next = terminalize(entry, { kind: "claim", token }); + if (next?.kind === "real") entry.state = next.state; + } }; const release = () => { @@ -563,14 +539,26 @@ export class InputAdmissions { fail: (stillExpected) => { const active = successor ?? scope; const origin = - final?.kind === "private" || final?.kind === "alias" - ? final.origin - : final?.kind === "real" && final.state.kind === "settled" - ? final.state.origin - : null; + final?.kind === "private" && final.phase.state === "accepted" + ? final.phase.origin + : final?.kind === "alias" + ? final.origin + : final?.kind === "real" && final.state.kind === "settled" + ? final.state.origin + : null; if (!fresh.length) owned ||= !!origin && this.isLive(origin); - if (!owned || !active.current() || !stillExpected()) return false; failClaimed(); + if ( + !owned || + !active.current() || + !stillExpected() || + (fresh.length > 0 && + (this.#operations.get(token) !== host || + !claimed || + this.#frontier.get(host) !== claimed || + this.epoch(host) !== claimed.epoch)) + ) + return false; this.invalidate(host); return true; }, @@ -601,40 +589,14 @@ export class InputAdmissions { if ( entry?.kind === "private" && id === scope.source && - entry.state === "pending" + entry.phase.state === "pending" ) await scope.wait(entry.ready); - for (const { id, entry } of captured) { - if (entry?.kind === "real") { - const message = native.find((message) => message.id === id); - if (!message || !this.#matchesReal(entry, message)) - throw new Error( - "Flow context has an unknown, synthetic, or stale real input.", - ); - if ( - entry.state.kind === "activating" || - (entry.state.kind === "failed" && id === scope.source) - ) - throw revoked(); - } - if (entry?.kind === "reserved") throw revoked(); - if (entry?.kind === "private") { - if (entry.state === "accepted" || id === scope.source) - this.#privateEvidence(entry, id, native); - else { - const message = native.find((message) => message.id === id); - if ( - !message || - message.type !== entry.type || - (message.type !== "user" && message.type !== "synthetic") || - !matchesAdmissionPayload(entry.payload, message) - ) - throw new Error( - "Flow native input payload conflicts with its admitted dispatch.", - ); - } - } - } + for (const { id, entry } of captured) + if (entry) + checkConsumed(entry, id, native, id === scope.source, (origin) => + this.#classify(origin), + ); for (const [i, item] of fresh.entries()) { if ( this.#entries.get(this.#key(host, item.id)) !== item.entry || @@ -650,7 +612,7 @@ export class InputAdmissions { observation.generation <= reuse.cancelled || observation.state === "cancelled" || observation.item?.type !== "user" || - !this.#matchesPayload(item.entry, observation.item.payload)) + !matchesReal(item.entry, observation.item.payload)) ) throw new Error( "Flow cancelled input reuse has no matching new hook and enqueue.", @@ -695,6 +657,7 @@ export class InputAdmissions { ); effects.check(changing, preparedEvidence); scope.assertCurrent(); + const terminal = capturedState?.gate.failure; if (changing) { this.invalidate(host); const last = fresh.at(-1); @@ -711,6 +674,7 @@ export class InputAdmissions { successor = this.#capture(input); owned ||= fresh.length > 0; const frontier = this.#frontier.get(host); + if (fresh.length) claimed = frontier; for (const { id, entry } of fresh) entry.state = { kind: "activating", @@ -766,7 +730,7 @@ export class InputAdmissions { : (source?.type === "user" || source?.type === "synthetic") && (evidence.kind === "retired-ordinary" || (evidence.kind === "unregistered" && - evidence.lineage !== "rejected")) + (evidence.lineage !== "rejected" || !!terminal))) ? { kind: "native-only" } : null; owned ||= evidence.kind === "live"; @@ -774,30 +738,6 @@ export class InputAdmissions { }, }; } - #privateEvidence( - entry: Admission, - id: string, - native: readonly SessionMessageInfo[], - ): SourceEvidence { - if (entry.state === "rejected") return { kind: "rejected" }; - const message = native.find((message) => message.id === id); - if ( - !message || - message.type !== entry.type || - (message.type !== "user" && message.type !== "synthetic") - ) - throw new Error( - "Flow native input type conflicts with its admitted dispatch.", - ); - const { id: _id, time: _time, type: _type, ...payload } = message; - if (!same(payload, entry.acceptedPayload)) - throw new Error( - "Flow native input payload conflicts with its admitted dispatch.", - ); - if (!entry.origin) - throw new Error("Flow admitted input has no settled origin."); - return this.#classify(entry.origin); - } #evidence( scope: InputScope, native: readonly SessionMessageInfo[], @@ -807,37 +747,11 @@ export class InputAdmissions { if (!entry || entry.kind === "observed") return { kind: "unregistered", - lineage: this.#history(state?.frontierEntry).kind, + lineage: facts(state?.frontierEntry).history, }; - if (entry.kind === "reserved") return { kind: "rejected" }; - if (entry.kind === "alias") - return entry.origin - ? this.#classify(entry.origin) - : this.#history(entry.source); - if (entry.kind === "private") - return this.#privateEvidence(entry, scope.source, native); - return entry.state.kind === "settled" && entry.state.origin - ? this.#classify(entry.state.origin) - : this.#history(entry); - } - #history(entry: Entry | undefined): Readonly<{ kind: History }> { - if (!entry || entry.kind === "observed") return { kind: "unknown-history" }; - if (entry.kind === "alias") return this.#history(entry.source); - if ( - entry.kind === "reserved" || - (entry.kind === "real" && entry.state.kind === "failed") || - (entry.kind === "private" && entry.state !== "accepted") - ) - return { kind: "rejected" }; - return { - kind: ( - entry.kind === "real" - ? entry.state.kind === "settled" && entry.state.automaticHistory - : entry.origin?.authority - ) - ? "retired-automatic" - : "retired-ordinary", - }; + return checkConsumed(entry, scope.source, native, true, (origin) => + this.#classify(origin), + ); } #classify(origin: InputOrigin): SourceEvidence { return this.isLive(origin) @@ -906,6 +820,7 @@ export class InputAdmissions { source: state.entry ?? state.frontierEntry, origin, }); + this.#cancel(scope.host, id); } isLive(origin: InputOrigin): boolean { const host = this.#hosts.get(origin); @@ -914,10 +829,8 @@ export class InputAdmissions { const frontier = this.#frontier.get(host); return ( (!frontier || - this.#covers( - this.#entries.get(this.#key(host, origin.id)), - frontier, - )) && + facts(this.#entries.get(this.#key(host, origin.id))).coverage === + frontier) && (!origin.authority || this.#options.authorityCurrent(origin.authority)) ); } @@ -945,54 +858,3 @@ export class InputAdmissions { this.#operations.clear(); } } -function matchesAdmissionPayload( - expected: Admission["payload"], - actual: - | SessionInboxUser["payload"] - | SessionInboxSynthetic["payload"] - | EnqueuedUserPayload, - checkMetadata = true, -): boolean { - if ( - expected.text !== actual.text || - (checkMetadata && !same(expected.metadata ?? {}, actual.metadata ?? {})) - ) - return false; - if ("description" in expected || "description" in actual) - return ( - ("description" in expected ? expected.description : undefined) === - ("description" in actual ? actual.description : undefined) - ); - const files = "files" in expected ? (expected.files ?? []) : []; - const nativeFiles = "files" in actual ? (actual.files ?? []) : []; - if ( - files.length !== nativeFiles.length || - files.some((file, i) => { - const native = nativeFiles[i]; - if ( - !native || - file.name !== native.name || - file.description !== native.description || - !same(file.mention, native.mention) - ) - return true; - if (native.source.type === "uri") return native.source.uri !== file.uri; - const match = /^data:([^;,]+);base64,(.*)$/s.exec(file.uri); - return !match || match[1] !== native.mime || match[2] !== native.data; - }) - ) - return false; - const agents = "agents" in expected ? (expected.agents ?? []) : []; - const nativeAgents = "agents" in actual ? (actual.agents ?? []) : []; - if (!same(agents, nativeAgents)) return false; - const skills = "skills" in expected ? (expected.skills ?? []) : []; - const nativeSkills = "skills" in actual ? (actual.skills ?? []) : []; - return ( - skills.length === nativeSkills.length && - skills.every( - (skill, i) => - skill.id === nativeSkills[i]?.id && - same(skill.mention, nativeSkills[i]?.mention), - ) - ); -} diff --git a/src/platform/opencode/input-evidence.ts b/src/platform/opencode/input-evidence.ts new file mode 100644 index 00000000..99202063 --- /dev/null +++ b/src/platform/opencode/input-evidence.ts @@ -0,0 +1,371 @@ +import { isDeepStrictEqual as same } from "node:util"; +import type { + OpenCodeEvent, + SessionInboxSynthetic, + SessionInboxUser, + SessionMessageInfo, + SessionPromptInput, + SessionSyntheticInput, +} from "@opencode/client"; +import type { SessionPrompt } from "@opencode/plugin/promise/session"; +import { nativePromptPayload } from "./command-hook.js"; +import type { InputOrigin } from "./execution-attribution.js"; + +export type Payload = + | Omit + | Omit; +export type InputReservation = Readonly<{ + host: string; + id: string; + epoch: number; +}>; +type AcceptedPayload = + | SessionInboxUser["payload"] + | SessionInboxSynthetic["payload"]; +export type FailureFact = Readonly<{ + reservation: InputReservation; + outcome: + | "before-registration" + | "private-rejected" + | "after-acceptance" + | "real-claim"; +}>; +type AdmissionPhase = + | Readonly<{ state: "pending"; acceptedPayload?: AcceptedPayload }> + | Readonly<{ + state: "rejected"; + failure: FailureFact; + acceptedPayload?: AcceptedPayload; + }> + | Readonly<{ + state: "accepted"; + acceptedPayload: AcceptedPayload; + origin: InputOrigin; + failure: FailureFact | null; + }>; +export type Admission = { + readonly kind: "private"; + readonly reservation: InputReservation; + readonly type: "user" | "synthetic"; + readonly payload: Payload; + readonly ready: Promise; + readonly settle: (origin: InputOrigin) => void; + readonly fail: (error: Error) => void; + phase: AdmissionPhase; +}; +export type InboxEvent = Extract< + OpenCodeEvent, + { + type: + | "session.inbox.enqueued" + | "session.inbox.cancelled" + | "session.inbox.delivered" + | "session.inbox.delivery.changed"; + } +>; +type InboxEnqueued = Extract; +type EnqueuedUserPayload = Extract< + InboxEnqueued["data"]["item"], + { type: "user" } +>["payload"]; +export type Observation = { + seq: number; + generation: number | null; + item: InboxEnqueued["data"]["item"] | null; + state: "pending" | "delivered" | "cancelled" | "unseen" | "consumed"; +}; +type RealState = + | Readonly<{ kind: "deferred" }> + | Readonly<{ kind: "immediate"; reservation: InputReservation }> + | Readonly<{ + kind: "activating"; + token: symbol; + reservation: InputReservation | null; + }> + | Readonly<{ + kind: "settled"; + reservation: InputReservation | null; + origin: InputOrigin | null; + automaticHistory: boolean; + }> + | Readonly<{ + kind: "failed"; + reservation: InputReservation | null; + failure: FailureFact | null; + }>; +export type Real = { + kind: "real"; + registration: symbol; + prompt: SessionPrompt["prompt"]; + metadata: SessionPrompt["metadata"]; + state: RealState; + observation: Observation | null; + reuse: { cancelled: number; hook: boolean } | null; +}; +type Alias = Readonly<{ + kind: "alias"; + source: Entry | undefined; + origin: InputOrigin | null; +}>; +export type Entry = + | Admission + | Real + | { kind: "observed"; observation: Observation } + | Alias + | Readonly<{ kind: "reserved"; reservation: InputReservation }> + | Readonly<{ kind: "failed-reservation"; failure: FailureFact }>; +type History = + | "retired-ordinary" + | "retired-automatic" + | "unknown-history" + | "rejected"; +export type SourceEvidence = + | Readonly<{ kind: "unregistered"; lineage: History }> + | Readonly<{ kind: "live"; origin: InputOrigin }> + | Readonly<{ kind: History }>; + +export function facts(entry: Entry | undefined): Readonly<{ + coverage: InputReservation | null; + failure: FailureFact | null; + history: History; +}> { + if (!entry || entry.kind === "observed") + return { coverage: null, failure: null, history: "unknown-history" }; + if (entry.kind === "alias") return facts(entry.source); + if (entry.kind === "failed-reservation") + return { coverage: null, failure: entry.failure, history: "rejected" }; + if (entry.kind === "reserved") + return { coverage: entry.reservation, failure: null, history: "rejected" }; + if (entry.kind === "private") { + const phase = entry.phase; + return { + coverage: entry.reservation, + failure: phase.state === "pending" ? null : phase.failure, + history: + phase.state !== "accepted" + ? "rejected" + : phase.origin.authority + ? "retired-automatic" + : "retired-ordinary", + }; + } + const state = entry.state; + return { + coverage: + state.kind === "deferred" || state.kind === "failed" + ? null + : state.reservation, + failure: state.kind === "failed" ? state.failure : null, + history: + state.kind === "failed" + ? "rejected" + : state.kind === "settled" && state.automaticHistory + ? "retired-automatic" + : "retired-ordinary", + }; +} + +type TerminalTransition = + | Readonly<{ kind: "unchanged" }> + | Readonly<{ kind: "reserved"; next: Entry }> + | Readonly<{ kind: "private"; phase: AdmissionPhase; reject: boolean }> + | Readonly<{ kind: "real"; state: RealState }>; +export function terminalize( + entry: Entry, + owner: + | Readonly<{ kind: "reservation"; reservation: InputReservation }> + | Readonly<{ kind: "claim"; token: symbol }>, +): TerminalTransition | null { + if (owner.kind === "claim") { + if ( + entry.kind !== "real" || + entry.state.kind !== "activating" || + entry.state.token !== owner.token + ) + return null; + const reservation = entry.state.reservation; + return { + kind: "real", + state: { + kind: "failed", + reservation, + failure: reservation ? { reservation, outcome: "real-claim" } : null, + }, + }; + } + const reservation = owner.reservation; + if (entry.kind === "reserved" && entry.reservation === reservation) + return { + kind: "reserved", + next: { + kind: "failed-reservation", + failure: { reservation, outcome: "before-registration" }, + }, + }; + if (entry.kind === "private" && entry.reservation === reservation) { + const phase = entry.phase; + if ( + phase.state === "rejected" || + (phase.state === "accepted" && phase.failure) + ) + return { kind: "unchanged" }; + return phase.state === "accepted" + ? { + kind: "private", + phase: { + ...phase, + failure: { reservation, outcome: "after-acceptance" }, + }, + reject: false, + } + : { + kind: "private", + phase: { + ...phase, + state: "rejected", + failure: { reservation, outcome: "private-rejected" }, + }, + reject: true, + }; + } + return facts(entry).failure?.reservation === reservation + ? { kind: "unchanged" } + : null; +} + +export function checkReceipt( + admission: Admission, + receipt: SessionInboxUser | SessionInboxSynthetic, +): void { + if ( + receipt.sessionID !== admission.reservation.host || + receipt.id !== admission.reservation.id || + receipt.type !== admission.type || + !matchesAdmissionPayload(admission.payload, receipt.payload) + ) + throw new Error( + "Flow admission receipt did not match its exact local payload.", + ); +} + +export function matchesReal( + entry: Real, + payload: SessionInboxUser["payload"] | EnqueuedUserPayload, +): boolean { + return ( + same(entry.metadata ?? {}, payload.metadata ?? {}) && + matchesAdmissionPayload(nativePromptPayload(entry.prompt), payload, false) + ); +} + +export function checkConsumed( + entry: Entry, + id: string, + native: readonly SessionMessageInfo[], + final: boolean, + classify: (origin: InputOrigin) => SourceEvidence, +): SourceEvidence { + if (entry.kind === "reserved" || entry.kind === "failed-reservation") { + if (!final) throw new Error("Flow execution epoch was revoked."); + return { kind: "rejected" }; + } + if (entry.kind === "alias") { + if (facts(entry).history === "rejected") return { kind: "rejected" }; + return entry.origin + ? classify(entry.origin) + : { kind: facts(entry).history }; + } + if (entry.kind === "observed") + return { kind: "unregistered", lineage: "unknown-history" }; + const message = native.find((message) => message.id === id); + if (entry.kind === "real") { + if (message?.type !== "user" || !matchesReal(entry, message)) + throw new Error( + "Flow context has an unknown, synthetic, or stale real input.", + ); + if ( + entry.state.kind === "activating" || + (entry.state.kind === "failed" && final) + ) + throw new Error("Flow execution epoch was revoked."); + return entry.state.kind === "settled" && entry.state.origin + ? classify(entry.state.origin) + : { kind: facts(entry).history }; + } + const phase = entry.phase; + if (final && phase.state === "rejected") return { kind: "rejected" }; + if ( + !message || + message.type !== entry.type || + (message.type !== "user" && message.type !== "synthetic") + ) + throw new Error( + "Flow native input type conflicts with its admitted dispatch.", + ); + if (phase.state !== "accepted" && !final) { + if (!matchesAdmissionPayload(entry.payload, message)) + throw new Error( + "Flow native input payload conflicts with its admitted dispatch.", + ); + return { kind: "rejected" }; + } + const { id: _id, time: _time, type: _type, ...payload } = message; + if (!same(payload, phase.acceptedPayload)) + throw new Error( + "Flow native input payload conflicts with its admitted dispatch.", + ); + if (phase.state !== "accepted") + throw new Error("Flow admitted input has no settled origin."); + return classify(phase.origin); +} +function matchesAdmissionPayload( + expected: Admission["payload"], + actual: + | SessionInboxUser["payload"] + | SessionInboxSynthetic["payload"] + | EnqueuedUserPayload, + checkMetadata = true, +): boolean { + if ( + expected.text !== actual.text || + (checkMetadata && !same(expected.metadata ?? {}, actual.metadata ?? {})) + ) + return false; + if ("description" in expected || "description" in actual) + return ( + ("description" in expected ? expected.description : undefined) === + ("description" in actual ? actual.description : undefined) + ); + const files = "files" in expected ? (expected.files ?? []) : []; + const nativeFiles = "files" in actual ? (actual.files ?? []) : []; + if ( + files.length !== nativeFiles.length || + files.some((file, i) => { + const native = nativeFiles[i]; + if ( + !native || + file.name !== native.name || + file.description !== native.description || + !same(file.mention, native.mention) + ) + return true; + if (native.source.type === "uri") return native.source.uri !== file.uri; + const match = /^data:([^;,]+);base64,(.*)$/s.exec(file.uri); + return !match || match[1] !== native.mime || match[2] !== native.data; + }) + ) + return false; + const agents = "agents" in expected ? (expected.agents ?? []) : []; + const nativeAgents = "agents" in actual ? (actual.agents ?? []) : []; + if (!same(agents, nativeAgents)) return false; + const skills = "skills" in expected ? (expected.skills ?? []) : []; + const nativeSkills = "skills" in actual ? (actual.skills ?? []) : []; + return ( + skills.length === nativeSkills.length && + skills.every( + (skill, i) => + skill.id === nativeSkills[i]?.id && + same(skill.mention, nativeSkills[i]?.mention), + ) + ); +} diff --git a/src/platform/opencode/lifecycle.ts b/src/platform/opencode/lifecycle.ts index dfdb4ccd..8dfe386d 100644 --- a/src/platform/opencode/lifecycle.ts +++ b/src/platform/opencode/lifecycle.ts @@ -204,16 +204,14 @@ export async function registerFlowLifecycle( const failReservation = ( reservation: Parameters[0], owner: CallbackOwner, + error: unknown, ) => { - try { - inputs.assertReservation(reservation); - } catch { - return; - } - failOwned(owner); + if (!inputs.finishReservation(reservation, error)) return; + if (owner.primary) attribution.end(owner.primary); + revokeEffects(owner.host); + autoDrive.deactivate(owner.host); + recovery.revoke(owner.host); }; - const makeOrigin = (...args: Parameters) => - inputs.makeOrigin(...args); const observeAccepted = async ( host: string, id: string, @@ -358,11 +356,11 @@ export async function registerFlowLifecycle( throw new Error("Flow synthetic dispatch was superseded."); }; const payload = { text, metadata: { [FLOW_AUTO_METADATA_KEY]: token } }; - const admission = inputs.register(reservation, { - type: "synthetic", - payload, - }); try { + const admission = inputs.register(reservation, { + type: "synthetic", + payload, + }); const pending = ctx.session.synthetic( { sessionID: host, id, ...payload, delivery: "queue", resume: true }, { signal: controller.signal }, @@ -381,12 +379,11 @@ export async function registerFlowLifecycle( intent, ); assertCurrent(); - return makeOrigin(host, id, delivery, true, expectedEpoch); + return inputs.makeOrigin(host, id, delivery, true, expectedEpoch); }); return { kind: "sent", source: id }; } catch (error) { - inputs.reject(admission, error); - failReservation(reservation, owner); + failReservation(reservation, owner, error); throw error; } }; @@ -408,11 +405,7 @@ export async function registerFlowLifecycle( ) => { attribution.assertExpected(host, expected); if (changing) - attribution.prepareReplacement( - input, - native, - expected, - true, + attribution.prepareReplacement(input, native, expected, true, () => compactions.prefixBridge( input, native, @@ -490,42 +483,41 @@ export async function registerFlowLifecycle( ); const assertCurrent = () => { inputs.assertReservation(reservation); - if (epoch(input.sessionID) !== expectedEpoch) - throw new Error("Flow command was superseded."); }; - const session = await ctx.session - .get({ sessionID: input.sessionID }, readOptions) - .catch((error: unknown) => { - failReservation(reservation, owner); - throw error; - }); - assertCurrent(); - if ( - session.parentID || - !session.agent || - specialists.has(session.agent) - ) - throw new Error( - "Flow commands require the native primary manager.", - ); - const delivery = nativeDelivery(session); - const invocationToken = - command.name === "flow-auto" && !command.stopping - ? autoDrive.createInvocationToken() - : undefined; - const payload = { - ...nativePromptPayload({ ...input.prompt, text: command.text }), - ...(invocationToken === undefined - ? {} - : { - metadata: { [FLOW_AUTO_METADATA_KEY]: invocationToken }, - }), - }; - const admission = inputs.register(reservation, { - type: "user", - payload, - }); try { + const session = await ctx.session.get( + { sessionID: input.sessionID }, + readOptions, + ); + assertCurrent(); + if ( + session.parentID || + !session.agent || + specialists.has(session.agent) + ) + throw new Error( + "Flow commands require the native primary manager.", + ); + const delivery = nativeDelivery(session); + const invocationToken = + command.name === "flow-auto" && !command.stopping + ? autoDrive.createInvocationToken() + : undefined; + const payload = { + ...nativePromptPayload({ + ...input.prompt, + text: command.text, + }), + ...(invocationToken === undefined + ? {} + : { + metadata: { [FLOW_AUTO_METADATA_KEY]: invocationToken }, + }), + }; + const admission = inputs.register(reservation, { + type: "user", + payload, + }); assertCurrent(); const receipt = await ctx.session.prompt( { @@ -556,7 +548,7 @@ export async function registerFlowLifecycle( assertCurrent, ); assertCurrent(); - return makeOrigin( + return inputs.makeOrigin( input.sessionID, id, delivery, @@ -592,8 +584,7 @@ export async function registerFlowLifecycle( ); } } catch (error) { - inputs.reject(admission, error); - failReservation(reservation, owner); + failReservation(reservation, owner, error); throw error; } }, @@ -642,15 +633,14 @@ export async function registerFlowLifecycle( native, prior, changing, - changing - ? compactions.prefixBridge( - input, - native, - prior?.participation.kind === "flow" - ? prior.participation.origin - : undefined, - ) - : undefined, + () => + compactions.prefixBridge( + input, + native, + prior?.participation.kind === "flow" + ? prior.participation.origin + : undefined, + ), ); completed = compactions.completed(input, native); }, diff --git a/tests/architecture-boundaries.test.ts b/tests/architecture-boundaries.test.ts index 7393acf7..52a3c76b 100644 --- a/tests/architecture-boundaries.test.ts +++ b/tests/architecture-boundaries.test.ts @@ -105,6 +105,32 @@ describe("v6 architecture boundaries", () => { expect(oversized).toEqual([]); }); + test("keeps input evidence subordinate to the admission ledger, including type imports and re-exports", async () => { + const violations: string[] = []; + const target = join(sourceRoot, "platform/opencode/input-evidence.ts"); + for (const directory of ["src", "tests", "scripts", "evals"]) { + for (const file of await sourceFiles(join(repositoryRoot, directory))) { + const source = await readFile(file, "utf8"); + const imports = [ + ...source.matchAll( + /(?:from\s+|import\s*(?:\(\s*)?)["']([^"']+)["']/g, + ), + ]; + for (const match of imports) { + const specifier = match[1]; + if (!specifier) continue; + if ( + resolve(dirname(file), specifier).replace(/\.js$/, ".ts") === + target && + repositoryPath(file) !== "src/platform/opencode/input-admission.ts" + ) + violations.push(`${repositoryPath(file)} imports ${specifier}`); + } + } + } + expect(violations).toEqual([]); + }); + test("keeps every src export referenced outside its declaring file", async () => { // `noUnusedLocals` cannot see an export that nothing imports, so dead // exported types and constants accumulate silently. Anything outside the diff --git a/tests/execution-attribution.test.ts b/tests/execution-attribution.test.ts index 1be6bc12..26986f67 100644 --- a/tests/execution-attribution.test.ts +++ b/tests/execution-attribution.test.ts @@ -1295,3 +1295,93 @@ for (const barrier of ["delete", "clear"] as const) kind: "retired", }); }); + +for (const path of [ + "direct-extension", + "nonchanging", + "expired-clock", + "native-only", +] as const) + test(`replacement bridge supplier stays lazy for ${path}`, () => { + let current = true, + calls = 0; + const attribution = new ExecutionAttribution(() => {}); + const primary = attribution.freeze( + context, + [user], + path === "native-only" + ? { kind: "native-only" } + : { kind: "flow", origin }, + { current: () => current, assertCurrent: () => {} }, + ); + attribution.end(primary); + if (path === "expired-clock") current = false; + const next: SessionContext = { + ...context, + messages: [ + ...(path === "direct-extension" ? context.messages : []), + { role: "user", id: "msg_bridge_next", content: [] }, + ], + }; + const native: SessionMessageInfo[] = [ + ...(path === "direct-extension" ? [user] : []), + { + type: "user", + id: "msg_bridge_next", + text: "Next source", + time: { created: 2 }, + }, + ]; + const prepare = () => + attribution.prepareReplacement( + next, + native, + primary, + path !== "nonchanging", + () => { + calls++; + throw new Error("unneeded bridge was evaluated"); + }, + ); + if (path === "native-only") + expect(prepare).toThrow("does not extend the frozen consumed prefix"); + else + expect(prepare().inputs).toEqual( + path === "direct-extension" + ? ["user-1", "msg_bridge_next"] + : ["msg_bridge_next"], + ); + expect(calls).toBe(0); + }); + +for (const outcome of ["throws", "undefined"] as const) + test(`replacement bridge supplier is required for a changing nonextension and ${outcome} denies it`, () => { + const f = fixture(); + f.attribution.end(f.primary); + let calls = 0; + const input: SessionContext = { + ...context, + messages: [{ role: "user", id: "msg_nonextension", content: [] }], + }; + const native: SessionMessageInfo[] = [ + { + type: "user", + id: "msg_nonextension", + text: "Different prefix", + time: { created: 2 }, + }, + ]; + expect(() => + f.attribution.prepareReplacement(input, native, f.primary, true, () => { + calls++; + if (outcome === "throws") throw new Error("bridge validation failed"); + return undefined; + }), + ).toThrow( + outcome === "throws" + ? "bridge validation failed" + : "does not extend the frozen consumed prefix", + ); + expect(calls).toBe(1); + expect(f.attribution.current(sid)).toBe(f.primary); + }); diff --git a/tests/input-admission.test.ts b/tests/input-admission.test.ts index b100f0bb..ddede582 100644 --- a/tests/input-admission.test.ts +++ b/tests/input-admission.test.ts @@ -76,7 +76,7 @@ test("matching receipt wakes only the exact admission scope", async () => { const f = fixture(); const scope = f.inputs.openSource(context("user-1")); const ready = scope.scope.ready(); - expect(f.admission.state).toBe("pending"); + expect(f.admission.phase.state).toBe("pending"); await f.inputs.accept(f.admission, f.receipt, async () => f.origin()); await ready; const resolved = await scope.resolve( @@ -203,7 +203,11 @@ test("a new frontier cancels old scope without rejecting a fresh exact ready", a f.close(); }); -import type { SessionInfo, SessionMessageInfo } from "@opencode/client"; +import type { + SessionInboxUser, + SessionInfo, + SessionMessageInfo, +} from "@opencode/client"; import { CompactionBoundary } from "../src/platform/opencode/auto-compaction.js"; async function compactionFixture() { @@ -501,7 +505,10 @@ for (const state of ["pending", "rejected", "accepted"] as const) if (state === "accepted") await f.inputs.accept(f.admission, f.receipt, async () => f.origin()); if (state === "rejected") - f.inputs.reject(f.admission, new Error("rejected receipt")); + f.inputs.finishReservation( + f.admission.reservation, + new Error("rejected receipt"), + ); const operation = f.inputs.openSource({ ...context("foreign"), messages: [ @@ -524,7 +531,9 @@ for (const state of ["pending", "rejected", "accepted"] as const) ), ).rejects.toThrow("native inputs differ"); operation.release(); - expect(f.admission.state).toBe(state === "pending" ? "pending" : state); + expect(f.admission.phase.state).toBe( + state === "pending" ? "pending" : state, + ); if (state === "accepted") { const old = f.inputs.openSource(context("user-1")); const resolved = await old.resolve( @@ -1188,11 +1197,10 @@ import { SessionMessage as TriageSessionMessage } from "@opencode/schema/session test("reservation triage rejected absent private identity cannot veto a new consumed public queue", async () => { const f = fixture(); try { - f.inputs.reject( - f.admission, + f.inputs.finishReservation( + f.admission.reservation, new Error("Private RPC failed before history."), ); - f.inputs.invalidate(host); await expect(f.admission.ready).rejects.toThrow( "Private RPC failed before history.", ); @@ -1243,8 +1251,10 @@ test("reservation triage rejected absent private identity cannot veto a new cons test("reservation triage exact rejected ledger identity remains negative without granting native-only", async () => { const f = fixture(); try { - f.inputs.reject(f.admission, new Error("Rejected private dispatch.")); - f.inputs.invalidate(host); + f.inputs.finishReservation( + f.admission.reservation, + new Error("Rejected private dispatch."), + ); const operation = f.inputs.openSource(context("user-1")); try { const result = await operation.resolve( @@ -1269,3 +1279,1261 @@ test("reservation triage exact rejected ledger identity remains negative without f.close(); } }); + +for (const replacement of ["same-epoch", "new-epoch"] as const) + for (const accepted of [false, true]) + test(`terminal ownership late private finish preserves ${replacement} ${accepted ? "accepted" : "pending"} replacement readiness`, async () => { + const f = fixture(); + try { + if (replacement === "new-epoch") f.inputs.invalidate(host); + const reservation = f.inputs.reserve(host, "msg_ready_successor"); + const next = f.inputs.register(reservation, { + type: "synthetic", + payload: { text: "Successor" }, + }); + const origin = f.inputs.makeOrigin( + host, + reservation.id, + delivery, + false, + reservation.epoch, + ); + if (accepted) + await f.inputs.accept( + next, + { + ...f.receipt, + id: reservation.id, + type: "synthetic", + payload: { text: "Successor" }, + }, + async () => origin, + ); + let settled = false; + void next.ready.then(() => { + settled = true; + }); + expect( + f.inputs.finishReservation( + f.reservation, + new Error("Late old private failure"), + ), + ).toBe(false); + expect( + f.inputs.finishReservation( + f.reservation, + new Error("Duplicate late failure"), + ), + ).toBe(false); + await expect(f.admission.ready).rejects.toThrow( + "Late old private failure", + ); + await Promise.resolve(); + expect(settled).toBe(accepted); + expect(next.phase.state).toBe(accepted ? "accepted" : "pending"); + expect(f.inputs.epoch(host)).toBe(reservation.epoch); + if (!accepted) + await f.inputs.accept( + next, + { + ...f.receipt, + id: reservation.id, + type: "synthetic", + payload: { text: "Successor" }, + }, + async () => origin, + ); + expect(await next.ready).toBe(origin); + expect(() => f.inputs.assertLive(origin)).not.toThrow(); + } finally { + f.close(); + } + }); + +test("terminal ownership finish settles once and cannot overwrite acceptance while activation is awaiting", async () => { + const f = fixture(), + entered = Promise.withResolvers(), + resume = Promise.withResolvers(); + try { + const origin = f.origin(); + const accepting = f.inputs + .accept(f.admission, f.receipt, async () => { + entered.resolve(); + await resume.promise; + return origin; + }) + .catch(String); + await entered.promise; + const old = f.inputs.captureBarrier(host), + epoch = f.inputs.epoch(host); + expect( + f.inputs.finishReservation( + { ...f.reservation }, + new Error("Forged reservation"), + ), + ).toBe(false); + expect(f.admission.phase.state).toBe("pending"); + expect( + f.inputs.finishReservation( + f.reservation, + new Error("Exact terminal failure"), + ), + ).toBe(true); + expect(f.inputs.epoch(host)).toBe(epoch + 1); + expect(old.current()).toBe(false); + const nextClock = f.inputs.captureBarrier(host); + expect( + f.inputs.finishReservation(f.reservation, new Error("Duplicate failure")), + ).toBe(false); + expect(nextClock.current()).toBe(true); + expect(f.inputs.epoch(host)).toBe(epoch + 1); + await expect(f.admission.ready).rejects.toThrow("Exact terminal failure"); + resume.resolve(); + expect(await accepting).toBe("Error: Flow execution epoch was revoked."); + expect(f.admission.phase.state).toBe("rejected"); + await expect(f.admission.ready).rejects.toThrow("Exact terminal failure"); + } finally { + resume.resolve(); + f.close(); + } +}); + +test("terminal ownership accepted parent keeps its exact receipt origin and resolved ready after child failure", async () => { + const f = fixture(); + try { + const origin = f.origin(); + await f.inputs.accept(f.admission, f.receipt, async () => origin); + expect( + f.inputs.finishReservation( + f.reservation, + new Error("Review child failed"), + ), + ).toBe(true); + expect(f.admission.phase.state).toBe("accepted"); + if (f.admission.phase.state !== "accepted") + throw new Error("Lost accepted phase"); + expect(f.admission.phase.origin).toBe(origin); + expect(f.admission.phase.acceptedPayload).toEqual({ + text: "Approved task", + }); + expect(f.admission.phase.failure?.outcome).toBe("after-acceptance"); + expect(await f.admission.ready).toBe(origin); + expect( + f.inputs.finishReservation( + f.reservation, + new Error("Duplicate child failure"), + ), + ).toBe(false); + expect(await f.admission.ready).toBe(origin); + } finally { + f.close(); + } +}); + +for (const terminal of ["deleted", "closed"] as const) + test(`terminal ownership finish cannot reopen a ${terminal} ledger host`, async () => { + const f = fixture(); + try { + const epoch = f.inputs.epoch(host), + old = f.inputs.captureBarrier(host); + if (terminal === "deleted") f.inputs.deleteHost(host); + else f.inputs.clear(); + expect( + f.inputs.finishReservation(f.reservation, new Error("Late failure")), + ).toBe(false); + expect(f.inputs.epoch(host)).toBe(epoch); + expect(old.current()).toBe(false); + expect(() => f.inputs.reserve(host, "msg_reopen")).toThrow("unavailable"); + const operation = f.inputs.openSource( + context("msg_unknown_after_delete"), + ); + await expect(operation.scope.ready()).rejects.toThrow( + "epoch was revoked", + ); + expect(operation.fail(() => true)).toBe(false); + operation.release(); + } finally { + f.close(); + } + }); + +for (const failedKind of ["private", "reserved"] as const) + test(`terminal identity exact failed ${failedKind} stays denied after a checked ordinary queue resets history`, async () => { + const f = publicLedger(); + let failedMessage: SessionMessageInfo; + try { + const reservation = f.inputs.reserve(host, "msg_negative"); + if (failedKind === "private") + f.inputs.register(reservation, { + type: "user", + payload: { text: "msg_negative" }, + }); + expect( + f.inputs.finishReservation(reservation, new Error("Failed identity")), + ).toBe(true); + failedMessage = f.message("msg_negative"); + f.prepare("msg_checked_reset"); + const reset = await f.consume(["msg_checked_reset"]); + expect(reset.participation?.kind).toBe("flow"); + expect( + reset.evidence.kind === "live" + ? reset.evidence.origin.authority + : "not-live", + ).toBeNull(); + f.inputs.invalidate(host); + const replay = f.inputs.openSource( + f.input(["msg_checked_reset", "msg_negative"]), + ); + try { + const result = await replay.resolve( + [f.message("msg_checked_reset"), failedMessage], + selected, + effects, + ); + expect(result.participation).toBeNull(); + expect(result.evidence.kind).toBe("rejected"); + } finally { + replay.release(); + } + } finally { + f.inputs.clear(); + } + }); + +for (const prefix of ["private", "reserved", "real"] as const) + test(`terminal identity aliased failed ${prefix} final cannot become an unknown native source`, async () => { + let rejectReal = false; + const f = publicLedger(async () => { + if (rejectReal) throw new Error("Real claim failed"); + }); + try { + let source: ReturnType; + if (prefix === "real") { + f.prepare("msg_alias_failed"); + source = f.inputs.openSource(f.input(["msg_alias_failed"])); + f.inputs.checkpoint(source.scope, "msg_failed_alias", null); + rejectReal = true; + await expect( + source.resolve([f.message("msg_alias_failed")], selected, effects), + ).rejects.toThrow("Real claim failed"); + expect(source.fail(() => true)).toBe(true); + } else { + const reservation = f.inputs.reserve(host, "msg_alias_failed"); + if (prefix === "private") + f.inputs.register(reservation, { + type: "user", + payload: { text: "msg_alias_failed" }, + }); + source = f.inputs.openSource(f.input(["msg_alias_failed"])); + f.inputs.checkpoint(source.scope, "msg_failed_alias", null); + expect( + f.inputs.finishReservation(reservation, new Error("Failed source")), + ).toBe(true); + } + source.release(); + const attempt = async () => { + const replay = f.inputs.openSource(f.input(["msg_failed_alias"])); + try { + const result = await replay.resolve( + [f.message("msg_failed_alias")], + selected, + effects, + ); + return result.participation; + } finally { + replay.release(); + } + }; + if (prefix === "real") + await expect(attempt()).rejects.toThrow("epoch was revoked"); + else expect(await attempt()).toBeNull(); + } finally { + f.inputs.clear(); + } + }); + +test("terminal identity a neverregistered failed prefix has no payload proof for a fresh queue", async () => { + const f = publicLedger(); + try { + const reservation = f.inputs.reserve(host, "msg_neverregistered"); + expect( + f.inputs.finishReservation( + reservation, + new Error("Selected read failed"), + ), + ).toBe(true); + expect(f.inputs.hasPrivate(host, reservation.id)).toBe(true); + f.reduce(f.event("session.inbox.enqueued", reservation.id, 1)); + f.reduce( + f.event("session.inbox.delivery.changed", reservation.id, 2, "steer"), + ); + expect(f.inputs.automaticBlock(host)).toBeNull(); + expect(() => f.prepare(reservation.id)).toThrow("unavailable"); + f.prepare("msg_after_unregistered"); + await expect( + f.consume([reservation.id, "msg_after_unregistered"]), + ).rejects.toThrow("epoch was revoked"); + expect(f.intents).toEqual([]); + expect( + (await f.consume(["msg_after_unregistered"])).participation?.kind, + ).toBe("flow"); + } finally { + f.inputs.clear(); + } +}); + +test("terminal identity a failed real final remains denied after checked queue reset and preserves original nonfinal payload", async () => { + let fail = true; + const f = publicLedger(async () => { + if (fail) throw new Error("Real activation failed"); + }); + try { + f.prepare("msg_failed_real"); + const failed = f.inputs.openSource(f.input(["msg_failed_real"])); + await expect( + failed.resolve([f.message("msg_failed_real")], selected, effects), + ).rejects.toThrow("Real activation failed"); + expect(failed.fail(() => true)).toBe(true); + failed.release(); + fail = false; + f.prepare("msg_real_reset"); + expect( + (await f.consume(["msg_failed_real", "msg_real_reset"])).participation + ?.kind, + ).toBe("flow"); + f.inputs.invalidate(host); + await expect( + f.consume(["msg_real_reset", "msg_failed_real"]), + ).rejects.toThrow("epoch was revoked"); + f.prepare("msg_later_queue"); + await expect( + f.consume( + ["msg_real_reset", "msg_failed_real", "msg_later_queue"], + [ + f.message("msg_real_reset"), + f.message("msg_failed_real", "Changed old payload"), + f.message("msg_later_queue"), + ], + ), + ).rejects.toThrow("unknown, synthetic, or stale real input"); + expect(f.intents).toEqual(["msg_failed_real", "msg_real_reset"]); + } finally { + f.inputs.clear(); + } +}); + +test("terminal ownership partial real activation keeps earlier settled members and fails only unfinished claim members", async () => { + let fail = true; + const f = publicLedger(async (id) => { + if (fail && id === "msg_partial_second") + throw new Error("Second activation failed"); + }); + try { + for (const id of [ + "msg_partial_first", + "msg_partial_second", + "msg_partial_final", + ]) + f.prepare(id); + const batch = [ + "msg_partial_first", + "msg_partial_second", + "msg_partial_final", + ], + operation = f.inputs.openSource(f.input(batch)); + await expect( + operation.resolve( + batch.map((id) => f.message(id)), + selected, + effects, + ), + ).rejects.toThrow("Second activation failed"); + const claimedEpoch = f.inputs.epoch(host); + expect(operation.fail(() => false)).toBe(false); + expect(f.inputs.epoch(host)).toBe(claimedEpoch); + expect(operation.fail(() => true)).toBe(true); + expect(operation.fail(() => true)).toBe(false); + expect(f.inputs.epoch(host)).toBe(claimedEpoch + 1); + operation.release(); + expect(f.intents).toEqual(["msg_partial_first", "msg_partial_second"]); + fail = false; + f.prepare("msg_after_partial"); + expect( + (await f.consume([...batch, "msg_after_partial"])).participation?.kind, + ).toBe("flow"); + expect(f.intents).toEqual([ + "msg_partial_first", + "msg_partial_second", + "msg_after_partial", + ]); + } finally { + f.inputs.clear(); + } +}); + +for (const replacement of ["same-epoch", "new-epoch"] as const) + for (const accepted of [false, true]) + test(`terminal ownership late real observer rejection preserves a ${replacement} ${accepted ? "accepted" : "pending"} private replacement`, async () => { + const entered = Promise.withResolvers(), + resume = Promise.withResolvers(); + const f = publicLedger(async () => { + entered.resolve(); + await resume.promise; + throw new Error("Late observer failure"); + }); + try { + f.prepare("msg_old_real"); + const operation = f.inputs.openSource(f.input(["msg_old_real"])); + const pending = operation + .resolve([f.message("msg_old_real")], selected, effects) + .catch(String); + await entered.promise; + if (replacement === "new-epoch") f.inputs.invalidate(host); + const reservation = f.inputs.reserve(host, "msg_private_replacement"); + const admission = f.inputs.register(reservation, { + type: "synthetic", + payload: { text: "Private replacement" }, + }); + const origin = f.inputs.makeOrigin( + host, + reservation.id, + delivery, + false, + reservation.epoch, + ); + const acceptReplacement = () => + f.inputs.accept( + admission, + { + id: reservation.id, + sessionID: host, + type: "synthetic", + payload: { text: "Private replacement" }, + time: { created: 3 }, + delivery: "queue", + }, + async () => origin, + ); + if (accepted) await acceptReplacement(); + resume.resolve(); + expect(await pending).toBe("Error: Flow execution epoch was revoked."); + expect(operation.fail(() => true)).toBe(false); + operation.release(); + expect(admission.phase.state).toBe(accepted ? "accepted" : "pending"); + expect(f.inputs.epoch(host)).toBe(reservation.epoch); + if (!accepted) await acceptReplacement(); + expect(await admission.ready).toBe(origin); + expect(() => f.inputs.assertLive(origin)).not.toThrow(); + expect(f.inputs.automaticBlock(host)).toBeNull(); + } finally { + resume.resolve(); + f.inputs.clear(); + } + }); + +test("terminal ownership a stale activation cannot overwrite a later accepted phase on the stable record", async () => { + const f = fixture(), + entered = Promise.withResolvers(), + resume = Promise.withResolvers(); + try { + const oldOrigin = f.origin(), + acceptedOrigin = f.origin(); + const old = f.inputs + .accept(f.admission, f.receipt, async () => { + entered.resolve(); + await resume.promise; + return oldOrigin; + }) + .catch(String); + await entered.promise; + await f.inputs.accept(f.admission, f.receipt, async () => acceptedOrigin); + expect(await f.admission.ready).toBe(acceptedOrigin); + resume.resolve(); + expect(await old).toBe("Error: Flow execution epoch was revoked."); + expect(f.admission.phase.state).toBe("accepted"); + if (f.admission.phase.state !== "accepted") + throw new Error("Lost accepted phase"); + expect(f.admission.phase.origin).toBe(acceptedOrigin); + expect(f.admission.phase.acceptedPayload).toEqual({ + text: "Approved task", + }); + expect(await f.admission.ready).toBe(acceptedOrigin); + } finally { + resume.resolve(); + f.close(); + } +}); + +test("terminal identity a captured unknown final cannot hide a subsequent real registration", async () => { + const f = publicLedger(); + try { + const failed = f.inputs.reserve(host, "msg_inspection_failed"); + expect( + f.inputs.finishReservation(failed, new Error("Pre-registration failed")), + ).toBe(true); + const operation = f.inputs.openSource(f.input(["msg_captured_unknown"])); + f.prepare("msg_captured_unknown"); + await expect( + operation.resolve([f.message("msg_captured_unknown")], selected, effects), + ).rejects.toThrow("epoch was revoked"); + expect(operation.fail(() => true)).toBe(false); + operation.release(); + expect(f.intents).toEqual([]); + expect( + (await f.consume(["msg_captured_unknown"])).participation?.kind, + ).toBe("flow"); + } finally { + f.inputs.clear(); + } +}); + +for (const changed of [ + "none", + "text", + "metadata", + "file-count", + "file-name", + "file-description", + "file-mention", + "file-uri", + "file-source", + "agent-count", + "agent-name", + "agent-mention", + "skill-count", + "skill-id", + "skill-mention", + "inline-mime", + "inline-data", +] as const) + test(`terminal payload rejected private prefix preserves ${changed} while consuming a checked queue`, async () => { + const f = publicLedger(); + try { + const inline = changed === "inline-mime" || changed === "inline-data"; + const mention = { start: 0, end: 4, text: "task" }; + const payload = { + text: "task", + metadata: { nested: { value: "original" } }, + files: [ + { + uri: inline + ? "data:text/plain;base64,eA==" + : "file:///tmp/flow-evidence.txt", + name: "evidence", + description: "Exact file", + mention, + }, + ], + agents: [{ name: "build", mention }], + skills: [{ id: Skill.ID.make("skill-proof"), mention }], + }; + const reservation = f.inputs.reserve(host, "msg_payload_failed"); + const admission = f.inputs.register(reservation, { + type: "user", + payload, + }); + expect( + f.inputs.finishReservation( + reservation, + new Error("Payload dispatch failed"), + ), + ).toBe(true); + await expect(admission.ready).rejects.toThrow("Payload dispatch failed"); + const nativePayload = { + text: "task", + metadata: { nested: { value: "original" } }, + files: [ + { + source: inline + ? { type: "inline" as const } + : { type: "uri" as const, uri: "file:///tmp/flow-evidence.txt" }, + data: "eA==", + mime: "text/plain", + name: "evidence", + description: "Exact file", + mention: { ...mention }, + }, + ], + agents: [{ name: "build", mention: { ...mention } }], + skills: [ + { + id: "skill-proof", + name: "proof", + text: "Native skill expansion", + mention: { ...mention }, + }, + ], + } satisfies SessionInboxUser["payload"]; + if (changed === "text") nativePayload.text = "tampered"; + if (changed === "metadata") + nativePayload.metadata.nested.value = "tampered"; + if (changed === "file-count") nativePayload.files = []; + if (changed === "file-name") + nativePayload.files = nativePayload.files.map((file) => ({ + ...file, + name: "tampered", + })); + if (changed === "file-description") + nativePayload.files = nativePayload.files.map((file) => ({ + ...file, + description: "tampered", + })); + if (changed === "file-mention") + nativePayload.files = nativePayload.files.map((file) => ({ + ...file, + mention: { ...mention, end: 3 }, + })); + if (changed === "file-uri") + nativePayload.files = nativePayload.files.map((file) => ({ + ...file, + source: { type: "uri", uri: "file:///tmp/changed" }, + })); + if (changed === "file-source") + nativePayload.files = nativePayload.files.map((file) => ({ + ...file, + source: { type: "inline" }, + })); + if (changed === "inline-mime") + nativePayload.files = nativePayload.files.map((file) => ({ + ...file, + mime: "text/html", + })); + if (changed === "inline-data") + nativePayload.files = nativePayload.files.map((file) => ({ + ...file, + data: "eQ==", + })); + if (changed === "agent-count") nativePayload.agents = []; + if (changed === "agent-name") + nativePayload.agents = nativePayload.agents.map((item) => ({ + ...item, + name: "other", + })); + if (changed === "agent-mention") + nativePayload.agents = nativePayload.agents.map((item) => ({ + ...item, + mention: { ...mention, text: "other" }, + })); + if (changed === "skill-count") nativePayload.skills = []; + if (changed === "skill-id") + nativePayload.skills = nativePayload.skills.map((item) => ({ + ...item, + id: "other", + })); + if (changed === "skill-mention") + nativePayload.skills = nativePayload.skills.map((item) => ({ + ...item, + mention: { ...mention, start: 1 }, + })); + f.prepare("msg_payload_queue"); + const operation = f.inputs.openSource( + f.input([reservation.id, "msg_payload_queue"]), + ); + const result = operation.resolve( + [ + { + ...nativePayload, + id: reservation.id, + type: "user", + time: { created: 0 }, + }, + f.message("msg_payload_queue"), + ], + selected, + effects, + ); + if (changed === "none") { + expect((await result).participation?.kind).toBe("flow"); + expect(f.intents).toEqual(["msg_payload_queue"]); + } else { + await expect(result).rejects.toThrow( + "payload conflicts with its admitted dispatch", + ); + expect(f.intents).toEqual([]); + expect(operation.fail(() => true)).toBe(false); + } + operation.release(); + } finally { + f.inputs.clear(); + } + }); + +for (const changed of ["none", "text", "description", "metadata"] as const) + test(`terminal payload rejected synthetic prefix preserves ${changed} before a registered successor`, async () => { + const f = publicLedger(); + try { + const reservation = f.inputs.reserve(host, "msg_synthetic_payload"); + const payload = { + text: "Notification", + description: "Exact description", + metadata: { key: "original" }, + }; + f.inputs.register(reservation, { type: "synthetic", payload }); + expect( + f.inputs.finishReservation( + reservation, + new Error("Synthetic dispatch failed"), + ), + ).toBe(true); + const actual = structuredClone(payload); + if (changed === "text") actual.text = "changed"; + if (changed === "description") actual.description = "changed"; + if (changed === "metadata") actual.metadata.key = "changed"; + f.prepare("msg_synthetic_successor"); + const promise = f.consume( + [reservation.id, "msg_synthetic_successor"], + [ + { + ...actual, + type: "synthetic", + id: reservation.id, + time: { created: 0 }, + }, + f.message("msg_synthetic_successor"), + ], + ); + if (changed === "none") + expect((await promise).participation?.kind).toBe("flow"); + else await expect(promise).rejects.toThrow("payload conflicts"); + } finally { + f.inputs.clear(); + } + }); + +for (const invalid of ["expired-origin", "delivery", "reason"] as const) + test(`terminal bridge completed checkpoint alone retains its ${invalid} guard`, async () => { + const f = await compactionFixture(); + try { + const source = await f.admission.ready; + const control = f.native[1]; + if (control?.type !== "compaction" || control.status !== "running") + throw new Error("Missing compaction control"); + expect((await f.boundary.admit(f.input)).kind).toBe("continuation"); + f.native.splice(0, f.native.length, { ...control, status: "completed" }); + const input = { ...f.input, ...context(control.id) }; + if (invalid === "expired-origin") f.inputs.invalidate(host); + if (invalid === "delivery") + input.model = { + ...model, + id: Model.ID.make("different-checkpoint-model"), + }; + if (invalid === "reason") + f.native.splice(0, 1, { + ...control, + status: "completed", + reason: "manual", + }); + expect(() => f.boundary.completed(input, f.native)).toThrow( + invalid === "expired-origin" + ? "epoch was revoked" + : invalid === "delivery" + ? "admitted agent or model differs" + : "control identity changed", + ); + if (invalid === "expired-origin") + expect(() => f.boundary.prefixBridge(input, f.native, source)).toThrow( + "epoch was revoked", + ); + } finally { + f.close(); + } + }); + +test("terminal payload failed real metadata remains exact nonfinal history before a fresh queue", async () => { + let fail = true; + const f = publicLedger(async () => { + if (fail) throw new Error("Real metadata activation failed"); + }); + try { + f.inputs.observePrompt( + { + sessionID: host, + messageID: SessionMessage.ID.make("msg_real_metadata_failed"), + prompt: { text: "Real payload" }, + metadata: { key: "original" }, + delivery: "queue", + }, + () => { + throw new Error("Unexpected immediate reservation"); + }, + ); + const native: SessionMessageInfo = { + type: "user", + id: "msg_real_metadata_failed", + text: "Real payload", + metadata: { key: "original" }, + time: { created: 0 }, + }; + const operation = f.inputs.openSource(f.input([native.id])); + await expect( + operation.resolve([native], selected, effects), + ).rejects.toThrow("Real metadata activation failed"); + expect(operation.fail(() => true)).toBe(true); + operation.release(); + fail = false; + f.prepare("msg_real_metadata_successor"); + await expect( + f.consume( + [native.id, "msg_real_metadata_successor"], + [ + { ...native, metadata: { key: "tampered" } }, + f.message("msg_real_metadata_successor"), + ], + ), + ).rejects.toThrow("unknown, synthetic, or stale real input"); + expect(f.intents).toEqual(["msg_real_metadata_failed"]); + expect( + ( + await f.consume( + [native.id, "msg_real_metadata_successor"], + [native, f.message("msg_real_metadata_successor")], + ) + ).participation?.kind, + ).toBe("flow"); + expect(f.intents).toEqual([ + "msg_real_metadata_failed", + "msg_real_metadata_successor", + ]); + } finally { + f.inputs.clear(); + } +}); + +import { CallID } from "@opencode/plugin/promise/tool"; +import { ExecutionAttribution } from "../src/platform/opencode/execution-attribution.js"; + +function terminalRegistrationFixture( + observed: boolean, + type: "user" | "synthetic", +) { + const f = publicLedger(), + source = "msg_terminal_registration", + text = "Native payload"; + const failed = f.inputs.reserve(host, "msg_registration_failed"); + expect( + f.inputs.finishReservation(failed, new Error("Local terminal failure")), + ).toBe(true); + if (observed) { + const event = f.event("session.inbox.enqueued", source, 1); + if (event.type !== "session.inbox.enqueued") + throw new Error("Expected enqueue observation"); + event.data.item = { type, payload: { text }, delivery: "queue" }; + f.reduce(event); + } + const input: SessionContext = { + ...context(source), + system: [], + options: {}, + tools: {}, + messages: [{ role: "user", id: source, content: [{ type: "text", text }] }], + }; + const native: SessionMessageInfo[] = [ + { type, id: source, text, time: { created: 1 } }, + ]; + const operation = f.inputs.openSource(input); + const attribution = new ExecutionAttribution((origin) => + f.inputs.assertLive(origin), + ); + const register = (samePayload: boolean, id = source, sessionID = host) => + f.inputs.observePrompt( + { + sessionID, + messageID: SessionMessage.ID.make(id), + prompt: { text: samePayload ? text : "Different registered payload" }, + delivery: "queue", + }, + () => { + throw new Error("Queue must remain deferred"); + }, + ); + const execution = { + sessionID: host, + agent, + messageID: SessionMessage.ID.make("msg_registration_assistant"), + id: CallID.make("call_registration_shell"), + }; + const addCall = () => + native.push({ + type: "assistant", + id: execution.messageID, + agent, + model, + time: { created: 2 }, + content: [ + { + type: "tool", + id: execution.id, + name: "shell", + state: { status: "running", input: {}, metadata: {} }, + time: { created: 2 }, + }, + ], + }); + return { + ...f, + source, + input, + native, + operation, + attribution, + register, + execution, + addCall, + }; +} + +for (const observed of [false, true]) + for (const type of ["user", "synthetic"] as const) + for (const samePayload of [false, true]) + for (const when of [ + "before-resolve", + "before-freeze", + "before-http", + "before-tool", + ] as const) + test(`captured registration clock refuses ${type} observed ${observed} matching ${samePayload} ${when}`, async () => { + const f = terminalRegistrationFixture(observed, type); + const barrier = f.inputs.captureBarrier(host), + epoch = f.inputs.epoch(host); + let httpEffects = 0, + toolEffects = 0; + try { + expect(f.operation.scope.current()).toBe(true); + if (when === "before-resolve") { + f.register(samePayload); + expect(f.operation.scope.current()).toBe(false); + expect(() => f.operation.scope.assertCurrent()).toThrow( + "epoch was revoked", + ); + await expect( + f.operation.resolve(f.native, selected, effects), + ).rejects.toThrow("epoch was revoked"); + } else { + const resolved = await f.operation.resolve( + f.native, + selected, + effects, + ); + expect(resolved.participation).toEqual({ kind: "native-only" }); + if (!resolved.participation) + throw new Error("Missing ordinary participation"); + if (when === "before-freeze") { + f.register(samePayload); + expect(resolved.scope.current()).toBe(false); + expect(() => + f.attribution.freeze( + f.input, + f.native, + resolved.participation ?? { kind: "native-only" }, + resolved.scope, + ), + ).toThrow("epoch was revoked"); + } else { + const primary = f.attribution.freeze( + f.input, + f.native, + resolved.participation, + resolved.scope, + ); + f.operation.release(); + expect(primary.participation).toEqual({ kind: "native-only" }); + if (when === "before-tool") + f.attribution.request({ + sessionID: host, + agent, + model, + kind: "primary", + request: new Request( + "http://localhost/registration-before-change", + ), + }); + f.addCall(); + f.register(samePayload); + expect(resolved.scope.current()).toBe(false); + expect(() => resolved.scope.assertCurrent()).toThrow( + "epoch was revoked", + ); + if (when === "before-http") + expect(() => { + f.attribution.request({ + sessionID: host, + agent, + model, + kind: "primary", + request: new Request( + "http://localhost/registration-denied", + ), + }); + httpEffects++; + }).toThrow("epoch was revoked"); + if (when === "before-tool") + expect(() => { + f.attribution.verify( + f.execution, + f.native, + selected, + process.cwd(), + "shell", + ); + toolEffects++; + }).toThrow("epoch was revoked"); + } + } + expect(f.inputs.epoch(host)).toBe(epoch); + expect(barrier.current()).toBe(true); + expect(f.operation.current()).toBe(false); + expect(f.operation.fail(() => true)).toBe(false); + expect(httpEffects).toBe(0); + expect(toolEffects).toBe(0); + expect(f.intents).toEqual([]); + } finally { + f.operation.release(); + f.inputs.clear(); + f.attribution.clear(); + } + }); + +for (const observed of [false, true]) + test(`captured registration cancels only the exact terminal scope held read, observed ${observed}`, async () => { + const f = terminalRegistrationFixture(observed, "user"), + hold = Promise.withResolvers(); + try { + const wait = f.operation.scope + .wait(hold.promise) + .then(() => "unexpected read", String); + f.register(false, "msg_unrelated_same_host"); + f.register( + false, + f.source, + Session.ID.make("session-other-registration"), + ); + expect(f.operation.scope.current()).toBe(true); + const other = f.inputs.openSource({ + ...f.input, + messages: [{ role: "user", id: "msg_held_other_source", content: [] }], + }); + const otherWait = other.scope + .wait(hold.promise) + .then(() => "other read completed", String); + f.register(false); + for (let turn = 0; turn < 20; turn++) await Promise.resolve(); + expect(await Promise.race([wait, Promise.resolve("still pending")])).toBe( + "Error: Flow execution epoch was revoked.", + ); + expect(other.scope.current()).toBe(true); + hold.resolve(); + expect(await otherWait).toBe("other read completed"); + other.release(); + } finally { + hold.resolve(); + f.operation.release(); + f.inputs.clear(); + f.attribution.clear(); + } + }); + +for (const queuedHost of ["same-host", "other-host"] as const) + test(`captured registration unrelated ${queuedHost} queue preserves resolved ordinary scope and tool`, async () => { + const f = terminalRegistrationFixture(false, "user"); + try { + const resolved = await f.operation.resolve(f.native, selected, effects); + if (!resolved.participation) + throw new Error("Missing ordinary participation"); + const primary = f.attribution.freeze( + f.input, + f.native, + resolved.participation, + resolved.scope, + ); + f.operation.release(); + f.attribution.request({ + sessionID: host, + agent, + model, + kind: "primary", + request: new Request("http://localhost/unrelated-registration"), + }); + f.addCall(); + f.register( + false, + "msg_unrelated_registration", + queuedHost === "same-host" + ? host + : Session.ID.make("session-other-registration"), + ); + expect(resolved.scope.current()).toBe(true); + expect(() => resolved.scope.assertCurrent()).not.toThrow(); + expect( + f.attribution.verify( + f.execution, + f.native, + selected, + process.cwd(), + "shell", + ).primary, + ).toBe(primary); + expect(primary.participation).toEqual({ kind: "native-only" }); + expect(f.intents).toEqual([]); + const target = + queuedHost === "same-host" + ? host + : Session.ID.make("session-other-registration"); + const queued = "msg_unrelated_registration", + text = "Different registered payload"; + const nextInput: SessionContext = { + ...f.input, + sessionID: target, + messages: [ + ...(queuedHost === "same-host" ? f.input.messages : []), + { role: "user", id: queued, content: [{ type: "text", text }] }, + ], + }; + const nextNative: SessionMessageInfo[] = [ + ...(queuedHost === "same-host" ? f.native : []), + { type: "user", id: queued, text, time: { created: 3 } }, + ]; + const next = f.inputs.openSource(nextInput); + const consumed = await next.resolve( + nextNative, + { ...selected, id: target }, + effects, + ); + expect(consumed.participation?.kind).toBe("flow"); + if (!consumed.participation) + throw new Error("Missing checked queue participation"); + f.attribution.end(primary); + const checked = f.attribution.freeze( + nextInput, + nextNative, + consumed.participation, + consumed.scope, + ); + next.release(); + f.attribution.request({ + sessionID: target, + agent, + model, + kind: "primary", + request: new Request("http://localhost/checked-unrelated-queue"), + }); + const execution = { + ...f.execution, + sessionID: target, + messageID: SessionMessage.ID.make("msg_unrelated_queued_assistant"), + id: CallID.make("call_unrelated_queued_shell"), + }; + nextNative.push({ + type: "assistant", + id: execution.messageID, + agent, + model, + time: { created: 4 }, + content: [ + { + type: "tool", + id: execution.id, + name: "shell", + state: { status: "running", input: {}, metadata: {} }, + time: { created: 4 }, + }, + ], + }); + expect( + f.attribution.verify( + execution, + nextNative, + { ...selected, id: target }, + process.cwd(), + "shell", + ).primary, + ).toBe(checked); + expect(f.intents).toEqual([queued]); + expect(resolved.scope.current()).toBe(queuedHost === "other-host"); + } finally { + f.operation.release(); + f.inputs.clear(); + f.attribution.clear(); + } + }); + +for (const mutation of ["observation", "checkpoint"] as const) + test(`captured registration exact ${mutation} entry replacement wakes its held read only`, async () => { + const f = terminalRegistrationFixture(false, "user"), + hold = Promise.withResolvers(); + try { + const other = f.inputs.openSource({ + ...f.input, + messages: [{ role: "user", id: "msg_other_held_entry", content: [] }], + }); + const waiting = f.operation.scope + .wait(hold.promise) + .then(() => "unexpected completion", String); + const otherWait = other.scope + .wait(hold.promise) + .then(() => "other completion", String); + const epoch = f.inputs.epoch(host); + if (mutation === "observation") + f.reduce(f.event("session.inbox.enqueued", f.source, 2)); + else f.inputs.checkpoint(f.operation.scope, f.source, null); + for (let turn = 0; turn < 20; turn++) await Promise.resolve(); + expect( + await Promise.race([waiting, Promise.resolve("still pending")]), + ).toBe("Error: Flow execution epoch was revoked."); + expect(f.operation.scope.current()).toBe(false); + expect(other.scope.current()).toBe(true); + expect(f.inputs.epoch(host)).toBe(epoch); + hold.resolve(); + expect(await otherWait).toBe("other completion"); + other.release(); + } finally { + hold.resolve(); + f.operation.release(); + f.inputs.clear(); + f.attribution.clear(); + } + }); + +test("captured registration retained real entry still invalidates on its exact registration generation change", async () => { + const f = publicLedger(), + hold = Promise.withResolvers(); + try { + const failed = f.inputs.reserve(host, "msg_registration_generation_failed"); + expect( + f.inputs.finishReservation(failed, new Error("Terminal predecessor")), + ).toBe(true); + f.prepare("msg_registration_generation"); + f.reduce( + f.event("session.inbox.enqueued", "msg_registration_generation", 1), + ); + const operation = f.inputs.openSource( + f.input(["msg_registration_generation"]), + ); + const waiting = operation.scope + .wait(hold.promise) + .then(() => "unexpected completion", String); + const epoch = f.inputs.epoch(host); + f.reduce( + f.event("session.inbox.cancelled", "msg_registration_generation", 2), + ); + expect(operation.scope.current()).toBe(true); + f.prepare("msg_registration_generation"); + expect(operation.scope.current()).toBe(false); + for (let turn = 0; turn < 20; turn++) await Promise.resolve(); + expect( + await Promise.race([waiting, Promise.resolve("still pending")]), + ).toBe("Error: Flow execution epoch was revoked."); + expect(f.inputs.epoch(host)).toBe(epoch); + expect(operation.fail(() => true)).toBe(false); + operation.release(); + f.reduce( + f.event("session.inbox.enqueued", "msg_registration_generation", 3), + ); + expect( + (await f.consume(["msg_registration_generation"])).participation?.kind, + ).toBe("flow"); + expect(f.intents).toEqual(["msg_registration_generation"]); + } finally { + hold.resolve(); + f.inputs.clear(); + } +}); diff --git a/tests/lifecycle.test.ts b/tests/lifecycle.test.ts index 68fc44e1..263f4814 100644 --- a/tests/lifecycle.test.ts +++ b/tests/lifecycle.test.ts @@ -4650,31 +4650,63 @@ for (const proof of ["before", "after"] as const) } }); -for (const settled of [false, true]) - test(`missing raw native input above ${settled ? "rejected" : "pending"} lineage refuses without authority`, async () => { +test("pending private receipt still refuses a distinct raw native suffix", async () => { + const f = await fixture(); + try { + f.blockAdmission(); + const command = f.command("flow-plan", "Pending plan source").catch(String); + await f.flush(); + f.native.push({ + type: "synthetic", + id: "msg_missing_above_unaccepted", + text: "External native", + time: { created: 1 }, + }); + await expect(f.emit("context", f.context())).rejects.toThrow( + "unknown, synthetic, or stale real input", + ); + expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); + expect(f.repository.saveCount).toBe(0); + f.releaseAdmission(); + await command; + } finally { + f.releaseAdmission(); + await f.lifecycle.dispose(); + } +}); + +for (const type of ["user", "synthetic"] as const) + test(`distinct ${type} after mismatched private receipt runs ordinary execution without summary authority`, async () => { const f = await fixture(); try { - if (settled) f.corruptReceipt(); - else f.blockAdmission(); - const command = f - .command("flow-plan", "Pending plan source") - .catch(String); - await f.flush(); - f.native.push({ - type: "synthetic", - id: "msg_missing_above_unaccepted", - text: "External native", - time: { created: 1 }, - }); - await expect(f.emit("context", f.context())).rejects.toThrow( - "unknown, synthetic, or stale real input", + f.corruptReceipt(); + await expect( + f.command("flow-plan", "Pending plan source"), + ).rejects.toThrow( + "Flow admission receipt did not match its exact local payload.", ); + const failed = f.native.find((message) => message.type === "user"); + if (failed?.type !== "user") + throw new Error("Missing rejected private prefix."); + expect(failed.text).toContain("Pending plan source"); + expect(f.native).toEqual([ + { + type: "user", + id: failed.id, + text: failed.text, + time: { created: 0 }, + }, + ]); + const id = "msg_receipt_distinct", + text = "External native"; + expect(failed.id).not.toBe(id); + f.native.push({ type, id, text, time: { created: 20 } }); + await exerciseUnregisteredNativeExecution(f, f.context().messages); + await refuseUnownedSummaries(f, "msg_receipt_summary"); expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); + expect(f.recovery.snapshot(sessionID)).toEqual({ mode: "off" }); expect(f.repository.saveCount).toBe(0); - f.releaseAdmission(); - await command; } finally { - f.releaseAdmission(); await f.lifecycle.dispose(); } }); @@ -6947,3 +6979,682 @@ test("parent consecutive public queues after a checked checkpoint need no stale await f.lifecycle.dispose(); } }); + +async function refuseUnownedSummaries(f: OwnershipFixture, id: string) { + for (const reason of ["manual", "auto"] as const) { + const input = f.context(), + control = { ...automaticControl(`${id}_${reason}`), reason }; + f.native.push(control); + try { + await expect(f.emit("compaction", input)).rejects.toThrow( + "epoch was revoked", + ); + } finally { + f.native.splice(f.native.indexOf(control), 1); + } + } + expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); + expect(f.recovery.snapshot(sessionID)).toMatchObject({ mode: "off" }); + expect(f.repository.saveCount).toBe(0); +} + +for (const materialized of [false, true]) + for (const type of ["user", "synthetic"] as const) + test(`terminal recovery ordinary ${type} executes before both summary denials with materialized prefix ${materialized}`, async () => { + const f = await fixture(); + try { + await triageRejectedCommand( + f, + materialized + ? "native-rpc-after-history" + : "native-rpc-before-history", + ); + f.native.push({ + type, + id: "msg_terminal_native", + text: "Distinct ordinary suffix", + time: { created: 30 }, + }); + await exerciseUnregisteredNativeExecution(f, f.context().messages); + await refuseUnownedSummaries(f, "msg_terminal_summary"); + } finally { + await f.lifecycle.dispose(); + } + }); + +for (const depth of [0, 2]) + for (const type of ["user", "synthetic"] as const) + test(`terminal recovery ordinary ${type} preserves automatic history through ${depth} checked aliases after execution`, async () => { + const f = await fixture(); + try { + await initialOwnershipWork(f); + for (let index = 0; index < depth; index++) { + await compactCurrentInput(f, `msg_negative_alias_${index}`); + await finishOwnedResponse(f, await startOwnedRequest(f)); + } + f.failNextSessionRead(); + await expect(f.emit("context", f.context())).rejects.toThrow( + "Native session read failed.", + ); + f.native.push({ + type, + id: "msg_after_automatic", + text: "Ordinary suffix after automatic history", + time: { created: 40 }, + }); + await exerciseRetiredAutomaticNativeExecution(f, f.context().messages); + await refuseUnownedSummaries(f, "msg_automatic_history_summary"); + } finally { + await f.lifecycle.dispose(); + } + }); + +test("terminal recovery exact consumed real failure cannot replay before a new source", async () => { + const f = await fixture(); + try { + await admitOrdinaryInput( + f, + "msg_failed_real_replay", + "Real failure cannot replay", + ); + const observe = spyOn(f.autoDrive, "observeMessage").mockRejectedValueOnce( + new Error("Claimed real activation failed."), + ); + try { + await expect(f.emit("context", f.context())).rejects.toThrow( + "Claimed real activation failed.", + ); + } finally { + observe.mockRestore(); + } + expect( + f.native.filter((item) => item.type === "user").map((item) => item.id), + ).toEqual(["msg_failed_real_replay"]); + await expect(f.emit("context", f.context())).rejects.toThrow( + "epoch was revoked", + ); + expect(f.repository.saveCount).toBe(0); + } finally { + await f.lifecycle.dispose(); + } +}); + +test("terminal recovery checked ordinary queue resets summary lineage while failed private replay stays denied", async () => { + const f = await fixture(); + try { + await initialOwnershipWork(f); + const { failedID } = await triageRejectedCommand( + f, + "native-rpc-before-history", + ); + if (!failedID) throw new Error("Missing rejected private identity."); + await triageFreshPublic(f, "queue"); + expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); + f.failNextSessionRead(); + await expect(f.emit("context", f.context())).rejects.toThrow( + "Native session read failed.", + ); + f.native.push({ + type: "user", + id: "msg_queue_reset_suffix", + text: "Unregistered ordinary suffix", + time: { created: 40 }, + }); + const input = f.context(), + control = { + ...automaticControl("msg_queue_reset_summary"), + reason: "manual" as const, + }; + f.native.push(control); + await f.emit("compaction", input); + expect(f.repository.saveCount).toBe(0); + f.native.splice(0, f.native.length, { ...control, status: "completed" }); + await expect(f.emit("context", f.context())).rejects.toThrow( + "unknown, synthetic, or stale", + ); + f.native.splice(0, f.native.length, { + type: "user", + id: failedID, + text: "Inspect failed reservation", + time: { created: 0 }, + }); + await expect(f.emit("context", f.context())).rejects.toThrow(); + } finally { + await f.lifecycle.dispose(); + } +}); + +test("terminal recovery expired checkpoint alone cannot execute after a failed command", async () => { + const f = await fixture(); + try { + await initialOwnershipWork(f); + await compactCurrentInput(f, "msg_expired_checkpoint"); + await finishOwnedResponse(f, await startOwnedRequest(f)); + await triageRejectedCommand(f, "native-rpc-before-history"); + expect( + f.native + .filter((item) => item.type === "compaction") + .map((item) => item.id), + ).toEqual(["msg_expired_checkpoint"]); + await expect(f.emit("context", f.context())).rejects.toThrow( + "epoch was revoked", + ); + expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); + } finally { + await f.lifecycle.dispose(); + } +}); + +test("terminal recovery queued command recovery works after an older checked checkpoint", async () => { + const f = await fixture(); + try { + await initialOwnershipWork(f); + await compactCurrentInput(f, "msg_old_recovery_checkpoint"); + await finishOwnedResponse(f, await startOwnedRequest(f)); + await admitOrdinaryInput( + f, + "msg_before_failed_command", + "Checked ordinary source after checkpoint", + ); + await finishOwnedResponse(f, await startOwnedRequest(f)); + await triageRejectedCommand(f, "native-rpc-before-history"); + const fresh = await triageFreshPublic(f, "queue"); + expect(fresh.effects).toEqual(["effect"]); + expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); + } finally { + await f.lifecycle.dispose(); + } +}); + +test("terminal recovery compaction consumes a changing queue after the previous checkpoint origin retired", async () => { + const f = await fixture(); + try { + await admitOrdinaryInput(f, "msg_compact_source", "Ordinary predecessor"); + await finishOwnedResponse(f, await startOwnedRequest(f)); + await compactCurrentInput(f, "msg_compact_old_checkpoint"); + await finishOwnedResponse(f, await startOwnedRequest(f)); + await admitOrdinaryInput(f, "msg_compact_first_queue", "First queue"); + await finishOwnedResponse(f, await startOwnedRequest(f)); + await admitOrdinaryInput(f, "msg_compact_second_queue", "Second queue"); + await compactCurrentInput(f, "msg_compact_new_checkpoint"); + expect(f.native.map((item) => item.id)).toEqual([ + "msg_compact_new_checkpoint", + ]); + const request = await startOwnedRequest(f); + const effects: string[] = [], + tool = await effectTool(f, effects); + expect(await tool.execute({}, ownershipNativeCall(f))).toEqual({ + content: [{ type: "text", text: "applied" }], + }); + expect(effects).toEqual(["effect"]); + await finishOwnedResponse(f, request); + } finally { + await f.lifecycle.dispose(); + } +}); + +async function exerciseRetiredAutomaticNativeExecution( + f: OwnershipFixture, + expectedMessages: SessionContext["messages"], +) { + expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); + expect(f.recovery.snapshot(sessionID)).toMatchObject({ + mode: "off", + automation: { active: false, bound: false }, + advice: { active: false }, + }); + expect(f.repository.saveCount).toBe(0); + const context = f.context(); + await f.emit("context", context); + expect(context.messages).toEqual(expectedMessages); + const request = new Request("http://localhost/unregistered-native", { + method: "POST", + body: "ordinary native request body", + }); + const outbound = { + sessionID, + agent, + model, + kind: "primary" as const, + request, + }; + await f.emit("http.request", outbound); + expect(await outbound.request.clone().text()).toBe( + "ordinary native request body", + ); + const inbound = { + ...outbound, + response: new Response("ordinary native response body"), + }; + await f.emit("http.response", inbound); + expect(await inbound.response.text()).toBe("ordinary native response body"); + const shell = ownershipNativeCall(f, "shell"); + let shellEffects = 0; + for (const callback of f.toolCallbacks["execute.before"]) + await callback({ + ...shell, + tool: "shell", + input: { command: "printf not-run" }, + }); + shellEffects++; + expect(shellEffects).toBe(1); + const assistant = f.native.find((message) => message.id === shell.messageID); + if (assistant?.type !== "assistant") + throw new Error("Missing native tool assistant."); + const flowCall = { ...shell, id: CallID.make("call_unregistered_flow") }; + assistant.content.push({ + type: "tool", + id: flowCall.id, + name: "flow_status", + time: { created: 4 }, + state: { + status: "running", + input: { request: { view: "compact" } }, + metadata: {}, + }, + }); + const effects: string[] = []; + const tool = await effectTool(f, effects); + await expect(tool.execute({}, flowCall)).rejects.toThrow(); + expect(effects).toEqual([]); + expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); + expect(f.recovery.snapshot(sessionID)).toMatchObject({ + mode: "off", + automation: { active: false, bound: false }, + advice: { active: false }, + }); + expect(f.repository.saveCount).toBe(0); + expect(f.repository.session).toBeNull(); +} + +test("terminal recovery an overlapping inspection cannot revoke the first checked queue before claim", async () => { + const f = await fixture(); + try { + await triageRejectedCommand(f, "native-rpc-before-history"); + await admitOrdinaryInput( + f, + "msg_inspection_queue", + "Checked queue after failure", + ); + f.blockNextContextRead(); + const pending = f.emit("context", f.context()); + await f.waitContextRead(); + await expect(f.emit("context", f.context())).rejects.toThrow( + "context callbacks overlap", + ); + f.releaseContextRead(); + await pending; + const request = new Request("http://localhost/inspection-first"); + await f.emit("http.request", { + sessionID, + agent, + model, + kind: "primary", + request, + }); + const effects: string[] = [], + tool = await effectTool(f, effects); + expect(await tool.execute({}, ownershipNativeCall(f))).toEqual({ + content: [{ type: "text", text: "applied" }], + }); + expect(effects).toEqual(["effect"]); + await finishOwnedResponse(f, request); + expect(f.repository.saveCount).toBe(0); + } finally { + f.releaseContextRead(); + await f.lifecycle.dispose(); + } +}); + +async function registrationNativeFixture( + observed: boolean, + type: "user" | "synthetic", +) { + const f = await fixture(); + await initialOwnershipWork(f); + const prior = await f.lifecycle.verifyTool(ownershipNativeCall(f), "flow"); + if (prior.kind !== "flow") + throw new Error("Missing captured automatic owner"); + await triageRejectedCommand(f, "native-rpc-before-history"); + expect(() => prior.assertCurrent()).toThrow(); + const id = "msg_registration_native", + text = "Original native payload"; + if (observed) + await f.sendEventAndWait({ + type: "session.inbox.enqueued", + id: "evt_registration_observed", + created: 20, + durable: { aggregateID: sessionID, seq: 200, version: 1 }, + data: { + sessionID, + inboxID: id, + item: { type, payload: { text }, delivery: "queue" }, + }, + }); + f.native.push({ type, id, text, time: { created: 30 } }); + return { f, id, text }; +} + +for (const observed of [false, true]) + for (const type of ["user", "synthetic"] as const) + for (const samePayload of [false, true]) + for (const when of [ + "before-resolve", + "await-freeze", + "before-http", + "before-tool", + ] as const) + test(`terminal registration lifecycle refuses ${type} observed ${observed} matching ${samePayload} ${when}`, async () => { + const { f, id, text } = await registrationNativeFixture( + observed, + type, + ); + const registeredText = samePayload + ? text + : "Different registered payload"; + const register = () => + f.emit("prompt", { + sessionID, + messageID: SessionMessage.ID.make(id), + prompt: { text: registeredText }, + delivery: "queue", + }); + let registration: Promise | undefined, + checks = 0, + httpEffects = 0, + shellEffects = 0; + const original = ExecutionAttribution.prototype.prepareReplacement; + const check = spyOn( + ExecutionAttribution.prototype, + "prepareReplacement", + ).mockImplementation(function (this: ExecutionAttribution, ...args) { + const result = original.apply(this, args); + if (++checks === 2 && when === "await-freeze") + queueMicrotask(() => { + registration = register(); + }); + return result; + }); + try { + if (when === "before-resolve") f.blockNextContextRead(); + const pending = f + .emit("context", f.context()) + .then(() => "accepted", String); + if (when === "before-resolve") { + await f.waitContextRead(); + await register(); + await f.flush(); + expect( + await Promise.race([pending, Promise.resolve("still pending")]), + ).toBe("Error: Flow execution epoch was revoked."); + f.releaseContextRead(); + } + const outcome = await pending; + await registration; + check.mockRestore(); + if (when === "before-resolve" || when === "await-freeze") { + expect(outcome).toBe("Error: Flow execution epoch was revoked."); + if (when === "await-freeze") { + expect(checks).toBe(2); + expect(registration).toBeDefined(); + } + } else { + expect(outcome).toBe("accepted"); + if (when === "before-tool") { + const request = new Request( + "http://localhost/registration-before-mutation", + ); + await f.emit("http.request", { + sessionID, + agent, + model, + kind: "primary", + request, + }); + await finishOwnedResponse(f, request); + } + await register(); + } + const request = new Request( + "http://localhost/registration-after-mutation", + ); + await expect( + (async () => { + await f.emit("http.request", { + sessionID, + agent, + model, + kind: "primary", + request, + }); + httpEffects++; + })(), + ).rejects.toThrow(); + const shell = ownershipNativeCall(f, "shell"); + await expect( + (async () => { + for (const callback of f.toolCallbacks["execute.before"]) + await callback({ + ...shell, + tool: "shell", + input: { command: "printf not-run" }, + }); + shellEffects++; + })(), + ).rejects.toThrow(); + await expect( + f.lifecycle.verifyTool(shell, "native", "shell"), + ).rejects.toThrow(); + const flowEffects: string[] = [], + tool = await effectTool(f, flowEffects); + await expect( + tool.execute({}, ownershipNativeCall(f)), + ).rejects.toThrow(); + expect(httpEffects).toBe(0); + expect(shellEffects).toBe(0); + expect(flowEffects).toEqual([]); + expect(f.repository.saveCount).toBe(0); + expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); + expect(f.recovery.snapshot(sessionID)).toMatchObject({ + mode: "off", + advice: { active: false }, + automation: { active: false, bound: false }, + }); + if (type === "user" && samePayload) { + await admitOrdinaryInput( + f, + "msg_registration_checked_successor", + "Fresh checked successor", + ); + const freshRequest = await startOwnedRequest(f); + const applied: string[] = [], + freshTool = await effectTool(f, applied); + expect( + await freshTool.execute({}, ownershipNativeCall(f)), + ).toEqual({ content: [{ type: "text", text: "applied" }] }); + expect(applied).toEqual(["effect"]); + await finishOwnedResponse(f, freshRequest); + expect(f.repository.saveCount).toBe(0); + } + } finally { + check.mockRestore(); + f.releaseContextRead(); + await f.lifecycle.dispose(); + } + }); + +for (const type of ["user", "synthetic"] as const) + test(`terminal registration earlier mismatched ${type} registration still refuses before capture`, async () => { + const { f, id } = await registrationNativeFixture(false, type); + try { + await f.emit("prompt", { + sessionID, + messageID: SessionMessage.ID.make(id), + prompt: { text: "Different registered payload" }, + delivery: "queue", + }); + await expect(f.emit("context", f.context())).rejects.toThrow( + "unknown, synthetic, or stale real input", + ); + expect(f.repository.saveCount).toBe(0); + expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); + expect(f.recovery.snapshot(sessionID)).toMatchObject({ + mode: "off", + advice: { active: false }, + automation: { active: false, bound: false }, + }); + } finally { + await f.lifecycle.dispose(); + } + }); + +for (const queuedHost of ["same-host", "other-host"] as const) + test(`terminal registration unrelated ${queuedHost} preparation preserves ordinary execution and a current Flow lease`, async () => { + const { f } = await registrationNativeFixture(false, "user"); + try { + const request = await startOwnedRequest(f); + const shell = ownershipNativeCall(f, "shell"); + const target = + queuedHost === "same-host" + ? sessionID + : Session.ID.make("session-registration-other"); + await f.emit("prompt", { + sessionID: target, + messageID: SessionMessage.ID.make("msg_positive_queued_one"), + prompt: { text: "msg_positive_queued_one" }, + delivery: "queue", + }); + for (const callback of f.toolCallbacks["execute.before"]) + await callback({ + ...shell, + tool: "shell", + input: { command: "printf not-run" }, + }); + expect(await f.lifecycle.verifyTool(shell, "native", "shell")).toEqual({ + kind: "native-only", + }); + await finishOwnedResponse(f, request); + await f.command( + "flow-auto", + "Current lease remains active during unrelated queue preparation", + ); + const primaryRequest = await startOwnedRequest(f); + const call = ownershipNativeCall(f), + guard = await f.lifecycle.verifyTool(call, "flow"); + if (guard.kind !== "flow") throw new Error("Missing Flow guard"); + const authority = f.autoDrive.captureAuthority(sessionID); + expect(authority?.token).toBe("local-token"); + await f.emit("prompt", { + sessionID: target, + messageID: SessionMessage.ID.make("msg_positive_queued_two"), + prompt: { text: "msg_positive_queued_two" }, + delivery: "queue", + }); + expect(() => guard.assertCurrent()).not.toThrow(); + expect(f.autoDrive.captureAuthority(sessionID)).toEqual(authority); + const applied: string[] = [], + tool = await effectTool(f, applied); + expect(await tool.execute({}, call)).toEqual({ + content: [{ type: "text", text: "applied" }], + }); + expect(applied).toEqual(["effect"]); + await finishOwnedResponse(f, primaryRequest); + if (queuedHost === "same-host") { + f.native.push( + { + type: "user", + id: "msg_positive_queued_one", + text: "msg_positive_queued_one", + time: { created: 40 }, + }, + { + type: "user", + id: "msg_positive_queued_two", + text: "msg_positive_queued_two", + time: { created: 41 }, + }, + ); + const queuedRequest = await startOwnedRequest(f); + const queuedEffects: string[] = [], + queuedTool = await effectTool(f, queuedEffects); + expect(await queuedTool.execute({}, ownershipNativeCall(f))).toEqual({ + content: [{ type: "text", text: "applied" }], + }); + expect(queuedEffects).toEqual(["effect"]); + await finishOwnedResponse(f, queuedRequest); + } + } finally { + await f.lifecycle.dispose(); + } + }); + +for (const scheduled of [false, true]) + test(`terminal registration original ownerless await-gap scheduled ${scheduled} refuses context and HTTP`, async () => { + const f = await fixture(); + let checks = 0, + registration: Promise | undefined; + const original = ExecutionAttribution.prototype.prepareReplacement; + const register = () => + f.emit("prompt", { + sessionID, + messageID: SessionMessage.ID.make("msg_ownerless_registration_gap"), + prompt: { text: "Different registered payload" }, + delivery: "queue", + }); + const check = spyOn( + ExecutionAttribution.prototype, + "prepareReplacement", + ).mockImplementation(function (this: ExecutionAttribution, ...args) { + const result = original.apply(this, args); + if (++checks === 2 && scheduled) + queueMicrotask(() => { + registration = register(); + }); + return result; + }); + try { + await triageRejectedCommand(f, "native-rpc-before-history"); + f.native.push({ + type: "user", + id: "msg_ownerless_registration_gap", + text: "Original native payload", + time: { created: 30 }, + }); + if (!scheduled) await register(); + await expect(f.emit("context", f.context())).rejects.toThrow( + scheduled + ? "epoch was revoked" + : "unknown, synthetic, or stale real input", + ); + await registration; + if (scheduled) { + expect(checks).toBe(2); + expect(registration).toBeDefined(); + } + check.mockRestore(); + let httpEffects = 0; + await expect( + (async () => { + await f.emit("http.request", { + sessionID, + agent, + model, + kind: "primary", + request: new Request("http://localhost/ownerless-gap-refusal"), + }); + httpEffects++; + })(), + ).rejects.toThrow(); + const effects: string[] = [], + tool = await effectTool(f, effects); + await expect(tool.execute({}, ownershipNativeCall(f))).rejects.toThrow(); + expect(httpEffects).toBe(0); + expect(effects).toEqual([]); + expect(f.repository.saveCount).toBe(0); + expect(f.autoDrive.captureAuthority(sessionID)).toBeNull(); + expect(f.recovery.snapshot(sessionID)).toEqual({ mode: "off" }); + } finally { + check.mockRestore(); + await f.lifecycle.dispose(); + } + }); From b7a9179e497c7ed8f31baad09567357006fdd424 Mon Sep 17 00:00:00 2001 From: Douwe de Vries Date: Sun, 25 Oct 2026 10:03:04 +0100 Subject: [PATCH 16/17] chore(release): qualify Flow 10.0.0 on OpenAI only --- .agents/plans/18-opencode-v2/README.md | 17 ++- .agents/plans/18-opencode-v2/decisions.tsv | 1 + .agents/plans/18-opencode-v2/execution.md | 13 +- .../qualification-dispatch-draft.json | 12 +- CHANGELOG.md | 5 +- docs/adr/0018-opencode-v2-only.md | 15 +- docs/development.md | 7 +- docs/positioning.md | 4 +- docs/release-qualification.md | 24 ++-- evals/README.md | 30 ++-- evals/release-policy.ts | 13 +- tests/canary-v2-native.test.ts | 14 +- tests/eval-canary.test.ts | 132 ++++++++++++++++++ tests/eval-release-sampling.test.ts | 75 ++++++++-- tests/qualification-cli.test.ts | 12 +- tests/release-1000.test.ts | 99 ++++++++++--- 16 files changed, 371 insertions(+), 102 deletions(-) diff --git a/.agents/plans/18-opencode-v2/README.md b/.agents/plans/18-opencode-v2/README.md index 93fa9027..572f2598 100644 --- a/.agents/plans/18-opencode-v2/README.md +++ b/.agents/plans/18-opencode-v2/README.md @@ -224,12 +224,19 @@ and 2 so tests exercise the final plugin rather than a placeholder. version selection. Do not mutate `RELEASE_HOST_POLICY` globally. Add regression checks that retained 9.x evidence still verifies offline. Add a prospective release profile that keeps all 17 current release cases, - including auto-continuation and delivery. Require two route providers unless - the operator approves a documented exception. Keep historical policies stable. + including auto-continuation and delivery. The operator's explicit exception + pins exact 10.0.0 to `openai/gpt-6.1-sol`, following 9.5.0 and 9.6.0. + It makes no cross-provider reliability claim. Later v2 releases retain two + route providers. Keep historical policies stable. - [ ] Generate the frozen sample and dispatch proposal from that profile. - With unchanged thresholds and two providers, 17 cases imply 144 primary cells - and 34 environmental reserves. Derive dispatch counts separately because - scenario followups and probes can consume additional dispatches. + Exact 10.0.0 keeps all thresholds and 17 cases with 72 primary cells and 17 + environmental reserves. Current steps derive 90 primary dispatches, 23 reserve + dispatches, and one route probe, giving 91 planned or 114 maximum top-level + dispatches. Internal turns and reviewer generations are separate; no dollar + cap follows. Refresh the draft at final candidate freeze. Paid qualification + stays HOLD until purpose, models, count, and expiry are approved under + [release operations](../05-release-simplification/README.md#authorize-paid-work). + Canary approval remains separate. Later generic profiles retain 144/34 cells. - [ ] Port release host permissions and model identity checks. Check recovery mode explicitly. Give the prospective version deliberate equivalents of `releaseReviewerModel`, `releaseHostPermissions`, and the recovery-key refusal. diff --git a/.agents/plans/18-opencode-v2/decisions.tsv b/.agents/plans/18-opencode-v2/decisions.tsv index f66b6b56..6d724a8c 100644 --- a/.agents/plans/18-opencode-v2/decisions.tsv +++ b/.agents/plans/18-opencode-v2/decisions.tsv @@ -79,3 +79,4 @@ ts phase decision why evidence result 2026-10-25T05:47:35Z failed-reservation-repair Retain exact terminal scope identity through freeze and effects Registration must not turn a captured unknown source into unchecked registered execution /tmp/flow-v2-failed-reservation-20261025/implementation-r1/review-r1-fixes/paired-source-test-proof.json; /tmp/flow-v2-failed-reservation-20261025/source-review-r2/report.md Fresh source and comment reviews pass; full check 5161 pass, 27 skip, zero fail; exact new package proof required 2026-10-25T05:47:35Z failed-reservation-scope Preserve the characterized ordinary-native nonterminal policy The same behavior exists in the original implementation and shows no terminal or Flow authority bypass /tmp/flow-v2-failed-reservation-20261025/source-review-r2/report.md; .agents/plans/18-opencode-v2/failed-reservation-review.md No broader admission guarantee or policy expansion; paid qualification remains on hold 2026-10-25T05:52:56Z failed-reservation-proof Add direct proof pointers for the repair checkpoint and exact corrected package Supersede row 79 evidence omissions without rewriting its historical result /tmp/flow-v2-failed-reservation-20261025/source-review-r2/report.md; /tmp/flow-v2-failed-reservation-20261025/comment-review-r2/report.md; /tmp/flow-v2-failed-reservation-20261025/parent/check-r2/check.log; /tmp/flow-v2-failed-reservation-20261025/parent-native/after-r2-recovered-verification.json; /tmp/flow-v2-failed-reservation-20261025/candidate-r2/smoke/command.json; /tmp/flow-v2-failed-reservation-20261025/trail-review-r1/review.md Source and comment reviews pass; full check 5161 pass, 27 skip, zero fail; af10177f native 23 checks and packed smoke 119 assertions pass; paid qualification on hold +2026-10-25T09:01:31Z qualification-model-scope Pin exact Flow 10.0.0 qualification to canonical OpenAI GPT-6.1 Sol The operator requested the prior OpenAI-only approach; preserve all 17 cases and per-case bars without claiming cross-provider coverage /tmp/flow-v2-openai-qualification-20261025/intent.md; /tmp/flow-v2-openai-qualification-20261025/source-review-r3/review.md; /tmp/flow-v2-openai-qualification-20261025/comment-review-r1/report.md; /tmp/flow-v2-openai-qualification-20261025/implementation-r1/historical-digest-proof.json; /tmp/flow-v2-openai-qualification-20261025/parent/check-r1/check.log; /Users/vriesd/projects/flow-opencode/.agents/plans/18-opencode-v2/qualification-dispatch-draft.json Scoped source and comment review pass; six historical policy and host digests unchanged; 72 primary cells and 17 reserves; draft 91 planned or 114 maximum top-level dispatches; paid qualification and canary remain on hold diff --git a/.agents/plans/18-opencode-v2/execution.md b/.agents/plans/18-opencode-v2/execution.md index f0352c54..17b57ce8 100644 --- a/.agents/plans/18-opencode-v2/execution.md +++ b/.agents/plans/18-opencode-v2/execution.md @@ -89,10 +89,15 @@ Flow session. Both were fixed and passed independent release review R2. Evaluato found separate credential-drain and ambient reviewer-variant defects. Their source fixes pass focused tests and typecheck. A fresh eval review remains due. -The draft qualification schedule has 180 primary manager dispatches, 46 reserve -dispatches, and two route probes. Its maximum is 228 top-level dispatches. -Internal generation is separate. This draft has no spending authorization and -is not a dollar cap. Canary authorization remains separate. +The current unapproved draft supersedes the earlier two-provider proposal for +exact 10.0.0 only. It pins `openai/gpt-6.1-sol` across all 17 cases, with 72 +primary cells and 17 reserve cells. Current steps derive 90 primary dispatches, +23 reserve dispatches, and one route probe, giving 91 planned or 114 maximum +top-level dispatches. Internal turns and reviewer generations are separate; +no dollar cap follows. Refresh the proposal at final candidate freeze. +Paid qualification stays HOLD until purpose, models, count, and expiry are +approved under [release operations](../05-release-simplification/README.md#authorize-paid-work). +Canary approval remains separate. Later v2 releases retain two-provider defaults. The user selected existing OpenCode 2 accounts as the authentication source. A synthetic source-owned OAuth resolver passed its bounded mechanism tests. diff --git a/.agents/plans/18-opencode-v2/qualification-dispatch-draft.json b/.agents/plans/18-opencode-v2/qualification-dispatch-draft.json index 781c0d67..5ba0c312 100644 --- a/.agents/plans/18-opencode-v2/qualification-dispatch-draft.json +++ b/.agents/plans/18-opencode-v2/qualification-dispatch-draft.json @@ -1,7 +1,8 @@ { "status": "draft-not-authorized", "packageVersion": "10.0.0", - "managerModels": 2, + "managerModels": 1, + "modelSet": ["openai/gpt-6.1-sol"], "cases": [ { "caseId": "delivery-summary-completed", @@ -123,9 +124,10 @@ "reserveDispatchesPerModel": 1 } ], - "primaryDispatches": 180, - "reserveDispatches": 46, - "routeProbes": 2, - "totalDispatchLimit": 228, + "primaryDispatches": 90, + "reserveDispatches": 23, + "routeProbes": 1, + "plannedDispatches": 91, + "totalDispatchLimit": 114, "semantics": "Top-level manager steps and route probes only; internal generation is separate; no dollar cap; canary requires separate approval." } diff --git a/CHANGELOG.md b/CHANGELOG.md index 000a15e7..6f8badb4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,8 +18,9 @@ One short entry per release, written for users deciding whether to upgrade. holding your Typesafe key. It defaults to `TYPESAFE_API_KEY`. A custom name never falls back to the default. - Session v5 schema remains unchanged. Historical qualification evidence retains its - original host policy. The new release profile requires all 17 cases on two - provider routes and a fresh exact-artifact canary. + original host policy. Exact 10.0.0 pins qualification to `openai/gpt-6.1-sol` + across all 17 cases and requires a fresh exact-artifact canary. This scope + makes no cross-provider reliability claim. - Zod is pinned to 4.6.7, including the fix for [oversized collection validation](https://github.com/advisories/GHSA-mrhx-c7xc-m3w5). diff --git a/docs/adr/0018-opencode-v2-only.md b/docs/adr/0018-opencode-v2-only.md index 0141831b..7d2a067b 100644 --- a/docs/adr/0018-opencode-v2-only.md +++ b/docs/adr/0018-opencode-v2-only.md @@ -46,12 +46,13 @@ variant. Reset restores its base. Native selection owns the coding model. ## Qualification -Qualification retains 17 cases on two provider routes, with the same approved -manager and reviewer model per cell and recovery off. Candidate, canary, and -publication bind the exact archive and unpacked manifest. +Flow 10.0.0 pins `openai/gpt-6.1-sol` across 17 cases with 72 primary and 17 +reserve cells. Later v2 releases retain two routes and 144/34 cells. Reviewers +use the manager model with recovery off. Candidate, canary, and publication +bind the exact archive and unpacked manifest. -Historical 9.x pins, digests, transcripts, and bundles remain verifiable without -an OpenCode 1 runtime. Publication approval gates remain. +Historical 9.x pins, digests, transcripts, and bundles remain verifiable offline. +Publication approval gates remain. -See [release qualification](../release-qualification.md) for the executable gates -and [automatic continuation](../automatic-continuation.md) for operator behavior. +See [qualification](../release-qualification.md) and +[automatic continuation](../automatic-continuation.md). diff --git a/docs/development.md b/docs/development.md index 34cc2ad7..39ac4a98 100644 --- a/docs/development.md +++ b/docs/development.md @@ -158,9 +158,10 @@ Follow the [frozen-candidate sequence](release-qualification.md#running-it): finish fixes and dependency updates, pass deterministic checks, approve paid evals. `bun run qualify -- --campaign-dir --canary ` seals the -policy grid, canary and grader evidence. Versions 9.1.0, 9.2.0, 9.3.0 and 9.4.0 use -GPT-6 Sol only under explicit exceptions. Other versions require two providers. Commit that -bundle before tagging; never substitute interrupted results for qualification. +policy grid, canary and grader evidence. Exact 10.0.0 pins `openai/gpt-6.1-sol`. +See [model policy](release-qualification.md#the-qualification-bar) for historical +exceptions and later defaults. Commit the bundle before tagging; +interrupted results cannot qualify. Release tags use `v`. Blocking release checks: the normal repository gate, package smoke, packed live OpenCode smoke, package integrity diff --git a/docs/positioning.md b/docs/positioning.md index b5b55870..aab1654f 100644 --- a/docs/positioning.md +++ b/docs/positioning.md @@ -45,8 +45,8 @@ only when a wrong change is expensive. For anything else it is overhead. Flow is pre-1.0 in the sense that matters: its reliability is measured, but not yet measured enough to promise. What that means concretely: -- Evals run against multiple providers on a schedule, and the numbers are published - with the release rather than described. Absent numbers mean unmeasured. +- Scheduled evals follow the [release policy](release-qualification.md). Numbers + ship with releases. Absent numbers mean unmeasured. - Session v5 is the only active state contract. There is no migration or rollback layer: finish or close active work before changing versions, in either direction. - The public surface is frozen while the guarantees are measured. See the release diff --git a/docs/release-qualification.md b/docs/release-qualification.md index 9211f09f..8a42b22b 100644 --- a/docs/release-qualification.md +++ b/docs/release-qualification.md @@ -9,7 +9,7 @@ This page owns release thresholds, candidate freezing, and publication order. | Threshold | Value | Why | | --- | --- | --- | -| Distinct providers | 1 for 9.1.0, 9.2.0, 9.3.0, 9.4.0, 9.5.0, and 9.6.0; otherwise ≥ 2 | 9.5.0 and 9.6.0 pin `openai/gpt-6.1-sol`; earlier exceptions pin GPT-6 Sol. OpenAI-only claims. | +| Distinct providers | 1 for 9.1.0, 9.2.0, 9.3.0, 9.4.0, 9.5.0, 9.6.0, and exact 10.0.0; otherwise 2 | 9.5.0, 9.6.0, and 10.0.0 pin `openai/gpt-6.1-sol`; earlier exceptions pin GPT-6 Sol. No cross-provider reliability claim. | | False completions | 0 | A `completed` closure the document itself contradicts is the failure Flow exists to prevent. | | Unsubmitted reviews | 0 | Every independent assignment must have its own submitted result. | | Scored attempts per provider | 3 at 100%; 10 at 90% | The frozen release plan gives each threshold enough trials to express its allowed failures. | @@ -54,9 +54,9 @@ Primary/reserve cells on GPT-6 Sol are 38/8 in 9.1.0 and 9.2.0, 48/9 in 9.3.0, and 57/12 in 9.4.0. The 9.4.0 profile adds three autonomous cases. Version 9.5.0 keeps those twelve cases on GPT-6.1 Sol, with 57/12 cells. Version 9.6.0 adds five delivery cases, collects them first, and uses 72/17 -cells. Flow 10.0.0 and later v2-targeting releases retain all 17 cases on two -provider routes, with 144 primary and 34 environmental reserve cells. Manager -and reviewer share the approved model within each cell. Recovery is off. +cells. Exact 10.0.0 retains all 17 cases on `openai/gpt-6.1-sol` with 72 primary +and 17 reserve cells. Later v2 releases retain two routes and 144/34 cells. +Manager and reviewer share the approved model per cell. Recovery is off. Other historical generic profiles use 96/18. Cell counts are not dispatch limits. Changed order requires a new freeze and approval. Existing campaigns stay immutable; narrowed or merged reports cannot qualify. @@ -115,14 +115,14 @@ Keep that ledger across retries. Budget-stopped campaigns cannot qualify. For native accounts, follow the [source setup](../evals/README.md#native-account-authentication). Prepare with `--native-auth`, launch with `--native-auth-source`, and record with the matching `--launch-receipt`. Live connection data remains private. -For 9.1.0 through 9.4.0, pin GPT-6 Sol. Versions 9.5.0 and 9.6.0 pin GPT-6.1 Sol. +For 9.1.0 through 9.4.0, pin GPT-6 Sol. Exact 9.5.0, 9.6.0, and 10.0.0 pin GPT-6.1 Sol. ```bash env -u TYPESAFE_API_KEY -u OPENCODE_FLOW_REVIEWER_MODEL \ -u OPENCODE_FLOW_REVIEWER_VARIANT -u OPENCODE_FLOW_REVIEWER_STEPS \ - bun run eval -- --release --model provider-a/model --model provider-b/model + bun run eval -- --release --model openai/gpt-6.1-sol bun run eval:canary -- prepare --report /report.json --out -bun scripts/canary-run.ts +bun scripts/canary-run.ts openai/gpt-6.1-sol bun run eval:canary -- record --prepared --operator \ --session --transcript bun run qualify -- --campaign-dir \ @@ -133,11 +133,11 @@ The canary launcher needs its separately approved ledger in `FLOW_EVAL_AUTHORIZA and provider access through its environment. It prints the native export path, collects it before stopping the isolated host, and charges failed launches. -The historical 9.6.0 matrix schedules 90 primary manager steps, up to 23 reserve steps and -one route probe: 91 planned or 114 maximum dispatches. Internal generation is -separate and these counts imply no dollar cap. Canary authorization is separate. For the v2 profile, derive the dispatch limit -from the frozen scenario steps, followups, and route probes before approval. -Do not copy the historical single-provider budget. +Current 10.0.0 steps derive 90 primary dispatches, 23 reserves, and one route +probe, giving 91 planned or 114 maximum top-level dispatches. Historical 9.6.0 +has the same counts. Internal turns and reviewer generations are separate; +no dollar cap follows. Canary approval is separate. Refresh the draft at final +freeze. Paid work stays HOLD until purpose, models, count, and expiry are approved. Use the [cheaper tiers](../evals/README.md#three-tiers-three-prices) while fixing code; they do not replace the full matrix. `bun run triage` identifies runs worth reading. diff --git a/evals/README.md b/evals/README.md index 25301dd7..0098c1e1 100644 --- a/evals/README.md +++ b/evals/README.md @@ -132,8 +132,10 @@ Ordinary runs use one sequential queue per model, with up to four queues in flig Release mode is strictly sequential (`--concurrency 1`). Versions 9.1.0 and 9.2.0 pin `openai/gpt-6-sol` with 38 primary targets and eight reserves. Version 9.3.0 pins the same model with 48 primary targets and nine reserves. Version -9.4.0 uses 57 primary targets and 12 reserves on the same route. Flow 10.0.0 -requires two providers across 17 cases, with 144 primary cells and 34 reserves. +9.4.0 uses 57 primary targets and 12 reserves on the same route. Exact 10.0.0 +pins `openai/gpt-6.1-sol` across all 17 cases with 72 primary cells and 17 reserves. +Later v2 releases retain two routes and 144/34 cells. The 10.0.0 exception makes +no cross-provider reliability claim. Cells are scheduling units; count scenario steps and probes separately for the dispatch authorization. Only retained retryable host/provider failures activate reserves, never product failures. Results are @@ -566,9 +568,10 @@ about the prompts; a single attempt of a stochastic scenario is not a rate. ## Multi-model matrix -Versions 9.1.0, 9.2.0, 9.3.0, and 9.4.0 require only `openai/gpt-6-sol` and make no -cross-provider claim. Other versions require two distinct providers. This is the -release sampling policy, not evidence that any candidate has qualified. +The [versioned policy](../docs/release-qualification.md#the-qualification-bar) +owns model requirements. Exact 10.0.0 requires only `openai/gpt-6.1-sol` and makes +no cross-provider reliability claim. Later v2 releases require two distinct +provider routes. This policy is not evidence that any candidate has qualified. `.github/workflows/evals.yml` runs the matrix weekly and on demand, outside contributor gates. @@ -726,13 +729,16 @@ distinction that matters: one pass in six and six in six are different findings. ## Cost -Versions 9.1.0 and 9.2.0 schedule 38 primary attempts and eight reserves on GPT-6 Sol. -Version 9.3.0 schedules 48 primary attempts and nine reserves on the same route. -Version 9.4.0 schedules 57 primary attempts and 12 reserves on that route. -These are policy targets, not completed qualification claims. Other versions schedule -96 primary attempts and 18 reserves across two providers. Ordinary campaign size depends -on the selected scenarios, models and repeats. Use `--scenario` while iterating; -cost depends on model pricing and the work performed, not just scenario count. +The [versioned qualification policy](../docs/release-qualification.md#the-qualification-bar) +owns model pins and cell counts, including historical exceptions. Exact 10.0.0 +has 72 primary cells and 17 reserves on `openai/gpt-6.1-sol`. Current scenario +steps derive 90 primary dispatches, 23 reserve dispatches, and one route probe, +for 91 planned or 114 maximum top-level dispatches. These remain an unapproved +proposal to refresh at final candidate freeze. Paid work is HOLD until purpose, +models, dispatch count, and expiry are approved. Canary approval is separate. +Internal turns and reviewer generations are separate, with no dollar cap. +Ordinary campaign size depends on scenarios, models, and repeats. Use `--scenario` +while iterating; cost depends on pricing and work, not just scenario count. Cost is the host-reported figure, which may come from model-price estimates. It is not a provider invoice. Historical OpenAI runs reported `cost: 0` on real diff --git a/evals/release-policy.ts b/evals/release-policy.ts index 9ed9cb7d..6af9ac45 100644 --- a/evals/release-policy.ts +++ b/evals/release-policy.ts @@ -205,6 +205,8 @@ const V2_RELEASE: ReleaseProfile = { }; export function releaseProfile(packageVersion: string): ReleaseProfile { + if (packageVersion === "10.0.0") + return openAiOnlyRelease(DELIVERY_RELEASE_CATALOG, "gpt-6.1-sol"); if (targetsOpenCodeV2(packageVersion)) return V2_RELEASE; if (packageVersion === "9.6.0") return openAiOnlyRelease(DELIVERY_RELEASE_CATALOG, "gpt-6.1-sol"); @@ -224,11 +226,12 @@ export function releaseReviewerModel( packageVersion: string, managerModel?: ModelIdentity, ): ModelIdentity | null { - if (targetsOpenCodeV2(packageVersion)) return managerModel ?? null; - if (packageVersion !== "9.6.0") return null; - const model = releaseProfile(packageVersion).requiredModels?.[0]; - if (!model) throw new Error("Pinned release reviewer model is absent."); - return model; + if (packageVersion === "9.6.0" || packageVersion === "10.0.0") { + const model = releaseProfile(packageVersion).requiredModels?.[0]; + if (!model) throw new Error("Pinned release reviewer model is absent."); + return model; + } + return targetsOpenCodeV2(packageVersion) ? (managerModel ?? null) : null; } export const RELEASE_ANALYSIS_SHA256 = canonicalSha256("flow-v2-analysis-v1", { diff --git a/tests/canary-v2-native.test.ts b/tests/canary-v2-native.test.ts index 77a14fae..eb69a06e 100644 --- a/tests/canary-v2-native.test.ts +++ b/tests/canary-v2-native.test.ts @@ -91,7 +91,7 @@ function sessionInfo( ...(parentID === undefined ? {} : { parentID }), projectID: "project", agent: parentID === undefined ? "build" : "flow-reviewer", - model: { providerID: "approved", id: "model" }, + model: { providerID: "openai", id: "gpt-6.1-sol" }, location: { directory }, cost: 0, tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } }, @@ -121,7 +121,7 @@ function native( id: "msg_manager", type: "assistant", agent: "build", - model: { providerID: "approved", id: "model" }, + model: { providerID: "openai", id: "gpt-6.1-sol" }, time: { created: 1, completed: 2 }, content: [ tool("flow_status", { @@ -167,7 +167,7 @@ function native( id: "msg_reviewer", type: "assistant", agent: "flow-reviewer", - model: { providerID: "approved", id: "model" }, + model: { providerID: "openai", id: "gpt-6.1-sol" }, time: { created: 1, completed: 2 }, content: session.runs .filter((run) => run.state === "completed") @@ -267,8 +267,8 @@ test("V2 canary uses native completed Flow tools and server session parentage", actor.sessionIds, ]), ).toEqual([ - ["manager", "approved", ["ses_manager"]], - ["reviewer", "approved", ["ses_reviewer"]], + ["manager", "openai", ["ses_manager"]], + ["reviewer", "openai", ["ses_reviewer"]], ]); }); @@ -418,7 +418,7 @@ for (const mutation of [ metadata: { parentSessionId: "ses_manager", sessionId: "ses_reviewer", - model: { providerID: "approved", id: "model" }, + model: { providerID: "openai", id: "gpt-6.1-sol" }, }, }, ], @@ -531,7 +531,7 @@ test("native collector refuses wrong host, active sessions, pagination loops and ses_manager: { type: "running", agent: "build", - model: { providerID: "approved", id: "model" }, + model: { providerID: "openai", id: "gpt-6.1-sol" }, messageID: "msg_manager", started: 1, }, diff --git a/tests/eval-canary.test.ts b/tests/eval-canary.test.ts index e64e87d7..6835aa9a 100644 --- a/tests/eval-canary.test.ts +++ b/tests/eval-canary.test.ts @@ -16,6 +16,7 @@ import { RELEASE_HOST_POLICY, releaseReviewerModel, } from "../evals/release-policy.js"; +import type { ModelIdentity } from "../evals/report.js"; import { artifactIdentitySha256, CANARY_CHECKLIST_SHA256, @@ -1264,3 +1265,134 @@ test("9.6 canary rejects every wrong-role model including a second reviewer", as ).toContain("pinned release route"); } }); + +test("10.0.0 canary rejects a self-consistent manager and reviewer on the wrong route", async () => { + const value = record({ + artifactValue: { ...artifact, packageVersion: "10.0.0" }, + }); + value.derivationVersion = "canary-evidence-v2"; + value.actors = ["manager", "reviewer"].map((role) => ({ + ...actor, + role: role === "manager" ? "manager" : "reviewer", + requestedModel: { + routeProvider: "other", + gateway: null, + family: "other", + model: "other", + revision: null, + }, + actualModel: { + kind: "observed", + value: { + routeProvider: "other", + gateway: null, + family: "other", + model: "other", + revision: null, + }, + }, + sessionIds: [""], + })); + const { recordSha256: _prior, ...base } = value; + const candidate = { ...base, recordSha256: canaryRecordSha256(base) }; + expect( + await canaryRecordIssue({ + version: "10.0.0", + record: candidate, + expectedArtifact: candidate.artifact, + directory: "/unused", + now: new Date("2026-08-25T00:00:01Z"), + }), + ).toBe("Canary manager model does not match the pinned release route."); +}); + +test("10.0.0 canary rejects missing and wrong-role identities before evidence access", async () => { + const model: ModelIdentity = { + routeProvider: "openai", + gateway: null, + family: "gpt-6.1-sol", + model: "gpt-6.1-sol", + revision: null, + }; + for (const variant of [ + "missing-manager", + "manager", + "reviewer", + "second-reviewer", + "observed-route", + "observed-model", + "family", + "gateway", + "revision", + ]) { + const value = record({ + artifactValue: { ...artifact, packageVersion: "10.0.0" }, + }); + value.derivationVersion = "canary-evidence-v2"; + value.actors = ["manager", "reviewer"].map((role) => ({ + ...actor, + role: role === "manager" ? "manager" : "reviewer", + requestedModel: model, + actualModel: { kind: "observed", value: model }, + sessionIds: [""], + })); + const reviewer = value.actors[1]; + if (!reviewer) throw new Error("Missing reviewer fixture."); + switch (variant) { + case "missing-manager": + value.actors = [reviewer]; + break; + case "manager": { + const manager = value.actors[0]; + if (!manager) throw new Error("Missing manager fixture."); + manager.requestedModel = { ...model, model: "other" }; + break; + } + case "reviewer": + reviewer.requestedModel = { ...model, model: "other" }; + break; + case "second-reviewer": + value.actors.push({ + ...reviewer, + requestedModel: { ...model, model: "other" }, + }); + break; + case "observed-route": + reviewer.actualModel = { + kind: "observed", + value: { ...model, routeProvider: "other" }, + }; + break; + case "observed-model": + reviewer.actualModel = { + kind: "observed", + value: { ...model, model: "other" }, + }; + break; + case "family": + reviewer.requestedModel = { ...model, family: "other" }; + break; + case "gateway": + reviewer.requestedModel = { ...model, gateway: "proxy" }; + break; + case "revision": + reviewer.requestedModel = { ...model, revision: "preview" }; + break; + } + const { recordSha256: _prior, ...base } = value; + const candidate = { ...base, recordSha256: canaryRecordSha256(base) }; + expect( + await canaryRecordIssue({ + version: "10.0.0", + record: candidate, + expectedArtifact: candidate.artifact, + directory: "/unused", + now: new Date("2026-08-25T00:00:01Z"), + }), + ).toBe( + variant === "missing-manager" + ? "Passed canaries require actors, session, and transcript evidence." + : `Canary ${variant === "manager" ? "manager" : "reviewer"} model does not match the pinned release route.`, + ); + } +}); diff --git a/tests/eval-release-sampling.test.ts b/tests/eval-release-sampling.test.ts index d6943b5b..0527094d 100644 --- a/tests/eval-release-sampling.test.ts +++ b/tests/eval-release-sampling.test.ts @@ -1,4 +1,7 @@ import { describe, expect, test } from "bun:test"; +import draft from "../.agents/plans/18-opencode-v2/qualification-dispatch-draft.json" with { + type: "json", +}; import { hostConfigSha256 } from "../evals/provenance.js"; import { assertExactReleaseCatalog, @@ -21,6 +24,7 @@ import { releaseScenarios, reserveJobsFor, } from "../evals/run.js"; +import { scenarioStepCatalog } from "../evals/scenario-steps.js"; import { SCENARIOS } from "../evals/scenarios.js"; import packageJson from "../package.json" with { type: "json" }; @@ -185,9 +189,9 @@ describe("release eval sampling", () => { test("checks configured CI release models before authorization", async () => { for (const [configured, expectedCode] of [ - ["openai/gpt-6.1-sol", 1], + ["openai/gpt-6.1-sol", 0], ["openai/gpt-6-sol", 1], - ["openai/gpt-6.1-sol,xai/grok-4.7", 0], + ["openai/gpt-6.1-sol,xai/grok-4.7", 1], ["openai/gpt-6.1-sol,openai/gpt-6-sol", 1], ["xai/grok-4.6", 1], ] as const) { @@ -206,7 +210,7 @@ describe("release eval sampling", () => { expect(code).toBe(expectedCode); if (expectedCode !== 0) expect(stderr).toContain( - "Release requires exactly 2 models on distinct route providers.", + "Release 10.0.0 requires exactly openai/gpt-6.1-sol with its canonical direct-route identity.", ); } }); @@ -373,24 +377,24 @@ describe("release eval sampling", () => { ).toThrow("openai/gpt-6.1-sol"); }); - test("schedules the current two-provider seventeen-case grid", () => { + test("schedules the current OpenAI-only seventeen-case grid", () => { const scenarios = releaseScenarios(packageJson.version); expect(scenarios).toHaveLength(17); const plan = campaignPlanFor({ - models: ["openai/gpt-6.1-sol", "xai/grok-4.7"], + models: ["openai/gpt-6.1-sol"], scenarios, sampling: { kind: "release", packageVersion: packageJson.version }, opencodeVersion: "2.1.4", }); - expect(plan.stoppingRule.count).toBe(144); - expect(plan.budget.maxAttempts).toBe(178); - expect(plan.abortPolicy.maxReplacementBlocks).toBe(34); + expect(plan.stoppingRule.count).toBe(72); + expect(plan.budget.maxAttempts).toBe(89); + expect(plan.abortPolicy.maxReplacementBlocks).toBe(17); expect( plan.cells.filter((cell) => cell.schedule === "primary"), - ).toHaveLength(144); + ).toHaveLength(72); expect( plan.cells.filter((cell) => cell.schedule === "environment-reserve"), - ).toHaveLength(34); + ).toHaveLength(17); }); test("keeps ordinary scenario catalogs report-only", () => { @@ -455,7 +459,7 @@ describe("release eval sampling", () => { } }); - test("rejects release grids without exactly two distinct route providers", async () => { + test("rejects current release grids outside the canonical OpenAI pin", async () => { for (const models of [ ["xai/a"], ["xai/a", "xai/b"], @@ -478,7 +482,7 @@ describe("release eval sampling", () => { ]); expect(exitCode).toBe(2); expect(stderr).toContain( - "Release requires exactly 2 models on distinct route providers.", + "Release 10.0.0 requires exactly openai/gpt-6.1-sol with its canonical direct-route identity.", ); } }); @@ -492,8 +496,6 @@ describe("release eval sampling", () => { "--model", "openai/gpt-6.1-sol", "--release", - "--model", - "xai/grok-4.7", "--concurrency", "2", ], @@ -520,3 +522,48 @@ describe("release eval sampling", () => { expect(stderr).toContain("--model requires a value"); }); }); + +test("current qualification draft counts actual scenario steps without authorizing a campaign", () => { + const policies = releaseCatalog("10.0.0"); + const scenarios = releaseScenarios("10.0.0"); + const cases = policies.map((policy) => { + const scenario = scenarios.find( + (scenario) => scenario.id === policy.caseId, + ); + if (!scenario) throw new Error(`Missing scenario ${policy.caseId}.`); + const steps = scenario.steps.map(scenarioStepCatalog); + return { + caseId: policy.caseId, + stepsPerAttempt: steps.length, + primaryAttemptsPerModel: policy.minScoredAttempts, + primaryDispatchesPerModel: policy.minScoredAttempts * steps.length, + reserveDispatchesPerModel: steps.length, + }; + }); + expect(draft.status).toBe("draft-not-authorized"); + expect(draft.packageVersion).toBe("10.0.0"); + expect(draft.modelSet).toEqual(["openai/gpt-6.1-sol"]); + expect(draft.managerModels).toBe(1); + expect(draft.cases).toEqual(cases); + expect(cases.reduce((sum, row) => sum + row.primaryAttemptsPerModel, 0)).toBe( + 72, + ); + expect(cases).toHaveLength(17); + const primary = cases.reduce( + (sum, row) => sum + row.primaryDispatchesPerModel, + 0, + ); + const reserves = cases.reduce( + (sum, row) => sum + row.reserveDispatchesPerModel, + 0, + ); + expect(primary).toBe(90); + expect(reserves).toBe(23); + expect(draft.primaryDispatches).toBe(primary); + expect(draft.reserveDispatches).toBe(reserves); + expect(draft.routeProbes).toBe(1); + expect(draft.plannedDispatches).toBe(91); + expect(draft.plannedDispatches).toBe(primary + draft.routeProbes); + expect(draft.totalDispatchLimit).toBe(114); + expect(draft.totalDispatchLimit).toBe(primary + reserves + draft.routeProbes); +}); diff --git a/tests/qualification-cli.test.ts b/tests/qualification-cli.test.ts index e9c4c013..60b4935b 100644 --- a/tests/qualification-cli.test.ts +++ b/tests/qualification-cli.test.ts @@ -55,29 +55,29 @@ import { assertQualificationBundle } from "../scripts/release-metadata.js"; import { autoQualifiedOutcome } from "./fixtures/auto-qualified-outcome.js"; -test("current release defaults retain 17 cases across two providers and three autonomous attempts", () => { +test("current release defaults retain 17 cases on OpenAI and three autonomous attempts", () => { expect(packageJson.version).toBe("10.0.0"); const scenarios = releaseScenarios(packageJson.version); const catalog = releaseCatalog(packageJson.version); expect(scenarios).toHaveLength(17); - expect([...new Set(catalog.map((row) => row.minProviders))]).toEqual([2]); + expect([...new Set(catalog.map((row) => row.minProviders))]).toEqual([1]); const plan = campaignPlanFor({ - models: ["openai/fixture-manager", "anthropic/fixture-manager"], + models: ["openai/gpt-6.1-sol"], scenarios, sampling: { kind: "release" }, opencodeVersion: releaseHostPolicy(packageJson.version).opencodeVersion, }); expect(plan.cells.filter((cell) => cell.schedule === "primary")).toHaveLength( - 144, + 72, ); expect( plan.cells.filter((cell) => cell.schedule === "environment-reserve"), - ).toHaveLength(34); + ).toHaveLength(17); expect( [ ...new Set(plan.cells.map((cell) => cell.managerModel?.routeProvider)), ].sort(), - ).toEqual(["anthropic", "openai"]); + ).toEqual(["openai"]); expect( attemptsForScenario("auto-two-features-evidence", { kind: "release" }), ).toBe(3); diff --git a/tests/release-1000.test.ts b/tests/release-1000.test.ts index 2d32d811..da51fa9f 100644 --- a/tests/release-1000.test.ts +++ b/tests/release-1000.test.ts @@ -15,6 +15,7 @@ import { releaseReviewerModel, } from "../evals/release-policy.js"; import type { ModelIdentity } from "../evals/report.js"; +import { ModelIdentitySchema } from "../evals/report-identities.js"; const manager: ModelIdentity = { routeProvider: "openai", @@ -31,8 +32,9 @@ const alternate: ModelIdentity = { revision: null, }; -test("OpenCode 2 release keeps every delivery and autonomous case on two providers", () => { +test("OpenAI-only 10.0.0 keeps every delivery and autonomous case", () => { const catalog = releaseCatalog("10.0.0"); + expect(catalog).toEqual(releaseCatalog("9.6.0")); expect(catalog.map((row) => row.caseId)).toEqual([ "delivery-summary-completed", "delivery-summary-deferred", @@ -52,25 +54,14 @@ test("OpenCode 2 release keeps every delivery and autonomous case on two provide "auto-prerequisite-repair", "auto-observe-with-required-pass", ]); - expect(catalog.map((row) => row.minProviders)).toEqual(Array(17).fill(2)); - expect(() => assertReleaseModels([manager], "10.0.0")).toThrow( - "exactly 2 models on distinct route providers", - ); - expect(() => - assertReleaseModels( - [manager, { ...alternate, routeProvider: "openai" }], - "10.0.0", - ), - ).toThrow("distinct route providers"); - assertReleaseModels([manager, alternate], "10.0.0"); - const cells = releaseCellsFor([manager, alternate], "10.0.0"); - expect(cells.filter((cell) => cell.schedule === "primary")).toHaveLength(144); + expect(catalog.map((row) => row.minProviders)).toEqual(Array(17).fill(1)); + assertReleaseModels([manager], "10.0.0"); + const cells = releaseCellsFor([manager], "10.0.0"); + expect(cells.filter((cell) => cell.schedule === "primary")).toHaveLength(72); expect( cells.filter((cell) => cell.schedule === "environment-reserve"), - ).toHaveLength(34); - for (const cell of cells) - expect(cell.reviewerModel).toEqual(cell.managerModel); - expect(releaseReviewerModel("10.0.0", alternate)).toEqual(alternate); + ).toHaveLength(17); + for (const cell of cells) expect(cell.reviewerModel).toEqual(manager); }); test("prospective host policy pins OpenCode 2 and refuses recovery or host overrides", () => { @@ -127,3 +118,75 @@ test("the release evaluator seals the ordinary control transport adapter", () => expect(files).toContain("evals/http-transport-plugin.ts"); expect(files).toContain("src/platform/opencode/http-transport.ts"); }); + +test("OpenAI-only 10.0.0 admits only the complete canonical direct identity", () => { + expect(() => assertReleaseModels([manager], "10.0.0")).not.toThrow(); + for (const forged of [ + alternate, + { ...manager, routeProvider: "other" }, + { ...manager, model: "gpt-6-sol" }, + { ...manager, family: "other-family" }, + { ...manager, gateway: "proxy" }, + { ...manager, revision: "preview" }, + { ...manager, variant: "fast" }, + { ...manager, unexpected: true }, + ]) { + expect(() => assertReleaseModels([forged], "10.0.0")).toThrow( + "canonical direct-route identity", + ); + } + for (const models of [[], [manager, alternate], [manager, manager]]) + expect(() => assertReleaseModels(models, "10.0.0")).toThrow( + "exactly openai/gpt-6.1-sol", + ); +}); + +test("OpenAI-only exception leaves later release defaults unchanged", () => { + for (const version of ["10.0.1", "10.1.0", "11.0.0"]) { + expect(releaseCatalog(version).map((row) => row.minProviders)).toEqual( + Array(17).fill(2), + ); + expect(() => assertReleaseModels([manager], version)).toThrow("exactly 2"); + expect(() => + assertReleaseModels( + [manager, { ...alternate, routeProvider: "openai" }], + version, + ), + ).toThrow("distinct route providers"); + assertReleaseModels([manager, alternate], version); + const cells = releaseCellsFor([manager, alternate], version); + expect(cells.filter((cell) => cell.schedule === "primary")).toHaveLength( + 144, + ); + expect( + cells.filter((cell) => cell.schedule === "environment-reserve"), + ).toHaveLength(34); + expect(releaseReviewerModel(version, alternate)).toEqual(alternate); + expect(releaseReviewerModel(version)).toBeNull(); + expect(releasePolicySha256(version)).toBe( + "sha256:86953cd5da1a0af6a541243abd1d8b92ec5d4c2e0aff26334a5be1d21187c3f6", + ); + } +}); + +test("OpenAI-only 10.0.0 resolves its reviewer pin before generic manager fallback", () => { + expect(releaseReviewerModel("10.0.0")).toEqual(manager); + expect(releaseReviewerModel("10.0.0", manager)).toEqual(manager); + expect(releaseReviewerModel("10.0.0", alternate)).toEqual(manager); +}); + +test("external release identities require every canonical field", () => { + const { routeProvider: _provider, ...missingProvider } = manager; + const { gateway: _gateway, ...missingGateway } = manager; + const { family: _family, ...missingFamily } = manager; + const { model: _model, ...missingModel } = manager; + const { revision: _revision, ...missingRevision } = manager; + for (const input of [ + missingProvider, + missingGateway, + missingFamily, + missingModel, + missingRevision, + ]) + expect(ModelIdentitySchema.safeParse(input).success).toBe(false); +}); From e026475a7b72cbfa0faaaa7faaed7fb773695974 Mon Sep 17 00:00:00 2001 From: Douwe de Vries Date: Sun, 25 Oct 2026 11:12:00 +0100 Subject: [PATCH 17/17] chore(ci): run release checks on Node 26 --- .agents/plans/18-opencode-v2/decisions.tsv | 2 ++ .github/workflows/ci.yml | 8 ++++---- .github/workflows/evals.yml | 2 +- .github/workflows/opencode-compatibility.yml | 2 +- .github/workflows/release.yml | 4 ++-- 5 files changed, 10 insertions(+), 8 deletions(-) diff --git a/.agents/plans/18-opencode-v2/decisions.tsv b/.agents/plans/18-opencode-v2/decisions.tsv index 6d724a8c..d481a8ac 100644 --- a/.agents/plans/18-opencode-v2/decisions.tsv +++ b/.agents/plans/18-opencode-v2/decisions.tsv @@ -80,3 +80,5 @@ ts phase decision why evidence result 2026-10-25T05:47:35Z failed-reservation-scope Preserve the characterized ordinary-native nonterminal policy The same behavior exists in the original implementation and shows no terminal or Flow authority bypass /tmp/flow-v2-failed-reservation-20261025/source-review-r2/report.md; .agents/plans/18-opencode-v2/failed-reservation-review.md No broader admission guarantee or policy expansion; paid qualification remains on hold 2026-10-25T05:52:56Z failed-reservation-proof Add direct proof pointers for the repair checkpoint and exact corrected package Supersede row 79 evidence omissions without rewriting its historical result /tmp/flow-v2-failed-reservation-20261025/source-review-r2/report.md; /tmp/flow-v2-failed-reservation-20261025/comment-review-r2/report.md; /tmp/flow-v2-failed-reservation-20261025/parent/check-r2/check.log; /tmp/flow-v2-failed-reservation-20261025/parent-native/after-r2-recovered-verification.json; /tmp/flow-v2-failed-reservation-20261025/candidate-r2/smoke/command.json; /tmp/flow-v2-failed-reservation-20261025/trail-review-r1/review.md Source and comment reviews pass; full check 5161 pass, 27 skip, zero fail; af10177f native 23 checks and packed smoke 119 assertions pass; paid qualification on hold 2026-10-25T09:01:31Z qualification-model-scope Pin exact Flow 10.0.0 qualification to canonical OpenAI GPT-6.1 Sol The operator requested the prior OpenAI-only approach; preserve all 17 cases and per-case bars without claiming cross-provider coverage /tmp/flow-v2-openai-qualification-20261025/intent.md; /tmp/flow-v2-openai-qualification-20261025/source-review-r3/review.md; /tmp/flow-v2-openai-qualification-20261025/comment-review-r1/report.md; /tmp/flow-v2-openai-qualification-20261025/implementation-r1/historical-digest-proof.json; /tmp/flow-v2-openai-qualification-20261025/parent/check-r1/check.log; /Users/vriesd/projects/flow-opencode/.agents/plans/18-opencode-v2/qualification-dispatch-draft.json Scoped source and comment review pass; six historical policy and host digests unchanged; 72 primary cells and 17 reserves; draft 91 planned or 114 maximum top-level dispatches; paid qualification and canary remain on hold +2026-10-25T10:11:29Z node26-release-runtime Use Node 26 for CI and the frozen Linux release runner The operator requested the newest Node 26 line; keep the existing Node >=24 support range and pinned Bun and OpenCode contracts https://nodejs.org/en/download; /tmp/flow-v2-release-followup-20261025/node26/review-r1/review.md; /tmp/flow-v2-release-followup-20261025/node26/image-runtime.json; /tmp/flow-v2-release-followup-20261025/linux-image-node26/Dockerfile Eight existing CI selectors become 26; frozen Linux Node26.12.1/Bun1.4.0/npm11.20.0 image built; fresh committed-head CI required before merge +2026-10-25T10:11:29Z picker-visual-readback Accept bounded visual readback of the unchanged terminal picker The autonomous release instruction authorizes the parent to inspect the concrete captured screens before merge /tmp/flow-v2-release-followup-20261025/parent/picker-visual-decision.json; /tmp/flow-v2-release-followup-20261025/picker-visual-r1/report.md Parent viewed all three contact sheets; no blocking layout at120x36; other sizes and physical font timing unproved; no human approval claim diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 214c81aa..28924175 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -69,7 +69,7 @@ jobs: - name: Set up Node uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 with: - node-version: 24 + node-version: 26 - name: Set up Bun uses: oven-sh/setup-bun@0c5077e51419868618aeaa5fe8019c62421857d6 # v2.2.0 @@ -145,7 +145,7 @@ jobs: - name: Set up Node uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 with: - node-version: 24 + node-version: 26 - name: Set up Bun uses: oven-sh/setup-bun@0c5077e51419868618aeaa5fe8019c62421857d6 # v2.2.0 @@ -167,7 +167,7 @@ jobs: - name: Set up Node uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 with: - node-version: 24 + node-version: 26 - name: Set up Bun uses: oven-sh/setup-bun@0c5077e51419868618aeaa5fe8019c62421857d6 # v2.2.0 @@ -191,7 +191,7 @@ jobs: - name: Set up Node uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 with: - node-version: 24 + node-version: 26 - name: Set up Bun uses: oven-sh/setup-bun@0c5077e51419868618aeaa5fe8019c62421857d6 # v2.2.0 with: diff --git a/.github/workflows/evals.yml b/.github/workflows/evals.yml index ba1cb72b..48a537f6 100644 --- a/.github/workflows/evals.yml +++ b/.github/workflows/evals.yml @@ -52,7 +52,7 @@ jobs: - name: Set up Node uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 with: - node-version: 24 + node-version: 26 - name: Set up Bun uses: oven-sh/setup-bun@0c5077e51419868618aeaa5fe8019c62421857d6 # v2.2.0 diff --git a/.github/workflows/opencode-compatibility.yml b/.github/workflows/opencode-compatibility.yml index d52e0cc0..835ffae9 100644 --- a/.github/workflows/opencode-compatibility.yml +++ b/.github/workflows/opencode-compatibility.yml @@ -18,7 +18,7 @@ jobs: - name: Set up Node uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 with: - node-version: 24 + node-version: 26 - name: Set up Bun uses: oven-sh/setup-bun@0c5077e51419868618aeaa5fe8019c62421857d6 # v2.2.0 diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 660f8970..ecfc7329 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -77,7 +77,7 @@ jobs: - name: Set up Node uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 with: - node-version: 24 + node-version: 26 package-manager-cache: false - name: Set up Bun @@ -176,7 +176,7 @@ jobs: - name: Set up Node uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 with: - node-version: 24 + node-version: 26 registry-url: 'https://registry.npmjs.org' package-manager-cache: false