diff --git a/cmd/duo/main.go b/cmd/duo/main.go index 7f46c42..8ea3d79 100644 --- a/cmd/duo/main.go +++ b/cmd/duo/main.go @@ -1,7 +1,6 @@ -// Command duo is the entrypoint for the duo CLI. It does nothing beyond -// constructing the root command, calling Execute, and mapping the result to -// an exit code — every other concern belongs to internal/cli and the verb -// packages it registers. +// Command duo is the entrypoint for the duo CLI. Apart from the explicitly +// configured portable-launcher command recorder, it only constructs the root +// command, calls Execute, and maps the result to an exit code. package main import ( @@ -9,6 +8,7 @@ import ( "github.com/procrastivity/duo/internal/buildinfo" "github.com/procrastivity/duo/internal/cli" + "github.com/procrastivity/duo/internal/conformance/portablelauncher" "github.com/procrastivity/duo/internal/iostreams" ) @@ -23,6 +23,10 @@ var ( ) func main() { + if recorded, exitCode := portablelauncher.RecordCommandIfConfigured(); recorded { + os.Exit(exitCode) + } + // Keep the ordinary execution path unchanged when recording is disabled. streams := iostreams.System() build := buildinfo.Info{Version: version, Commit: commit, Date: date} root := cli.NewRootCommand(streams, build) diff --git a/contracts/MANIFEST b/contracts/MANIFEST index 17e8838..cc8569a 100644 --- a/contracts/MANIFEST +++ b/contracts/MANIFEST @@ -41,10 +41,16 @@ aee7bfbbdc4aab9905b44e30790d040e23e727e77f054e3d3c26c961ff62dc5b fixtures/duo-e 2b80a74b759ca5fc7a32b1e68b95b34d983514faf31b64fbb13bd9fce5315ad1 fixtures/duo-external-v1/version-conflict.json 52e67bdca02355281e9a671865cbbf24d1a66938a44f214529214aa9528fcff4 fixtures/duo-external-v1/working-mode-effective.json b93d2aa95138b1896ddc9d56d63ea890f5ba1a72bc275d63a1530bfc19214674 fixtures/duo-external-v1/working-mode-selected.json +045edb8695b91fd54b3d792ef5cda267f0b812a1d44765ad7290091080ca67e1 fixtures/duo-portable-launcher-conformance-v1/blobs/045edb8695b91fd54b3d792ef5cda267f0b812a1d44765ad7290091080ca67e1.json +19ee7895766722228160ab602b9a937e9d630b5d0a66484478695b2cf67fc5b9 fixtures/duo-portable-launcher-conformance-v1/evidence-index.json +516d77e6520cb9c5ae8acf70c38707d10d101f14eddc14d75e233191f34e9804 fixtures/duo-portable-launcher-conformance-v1/result-rejected-fabricated-pass.json +bf23f142b457635dd864f369d5bce20d16cccbf50c4e64dc8d502016a8197fad fixtures/duo-portable-launcher-conformance-v1/result-structural-blocked.json +01e8e6bd80ceb441be6dc128aca023a1bede31b655e74b12ee3da0c2fc6cc204 fixtures/duo-portable-launcher-conformance-v1/scenario.json eb0894e6eb15b8972da2fbcb5608c07e60daf4508d43aa3bee770bcb432e1977 schemas/duo-config-v1.schema.json 2c6b98a2769c11e2876fe56275beeca1e0440d10555c037965be2c7dbc3e2c82 schemas/duo-config-v2.schema.json d1a2fd1be5339c5699a21cf65616a4d09ff932a5495111d02a422b7656086842 schemas/duo-config-v3.schema.json 8b055ff60f0ca5f3da46302671591aa0dae61185cd9f1f15fcceb36d841e7497 schemas/duo-external-v1.schema.json 0e580adec7e8ad7e75b8c103ab8eebbc27beff934f6747cc9bda983ab90cb73f schemas/duo-manifest-v1.schema.json +413fbe9c86614b80369814cb20a60f5e9513461294ef40acbceb5c951a1f2e4f schemas/duo-portable-launcher-conformance-result-v1.schema.json 388e3918e7b1421e341317c81f2c1754bd06d594b946d045a3a68e18adaf6acb schemas/duo-projection-conformance-v1.schema.json -238c56beae5fe97dfc774f9c54229ac9f26f964a1b9c42c59d0f66f37f8bdf91 schemas/duo-projection-stamp-v1.schema.json +0baeff3cf3510feb474539805502536367ce9511b79c73f0586af3054b6cc827 schemas/duo-projection-stamp-v1.schema.json diff --git a/contracts/fixtures/duo-portable-launcher-conformance-v1/blobs/045edb8695b91fd54b3d792ef5cda267f0b812a1d44765ad7290091080ca67e1.json b/contracts/fixtures/duo-portable-launcher-conformance-v1/blobs/045edb8695b91fd54b3d792ef5cda267f0b812a1d44765ad7290091080ca67e1.json new file mode 100644 index 0000000..c7c92a8 --- /dev/null +++ b/contracts/fixtures/duo-portable-launcher-conformance-v1/blobs/045edb8695b91fd54b3d792ef5cda267f0b812a1d44765ad7290091080ca67e1.json @@ -0,0 +1,1251 @@ +{ + "schema": "duo.portable-launcher-conformance-evidence/v1", + "scrub_status": "pass", + "observations": [ + { + "sequence": 1, + "assertion_id": "stage.completed_within_deadline", + "source": "controller", + "evidence": { + "deadline_ms": 30000, + "finished_ms": 1, + "kind": "controller.checkpoint", + "monotonic_ms": 0, + "started_ms": 0 + } + }, + { + "sequence": 1, + "assertion_id": "setup.fixture_isolated_and_pinned", + "source": "controller", + "evidence": { + "credential_provider": "openai-codex", + "credential_source_kind": "operator_copy", + "credentials_present": true, + "kind": "controller.checkpoint", + "measurements": { + "fixture_root": "$RUN", + "mcp_enabled": false, + "plugins_enabled": false, + "shared_state": false, + "terminal_input_used": false + }, + "monotonic_ms": 0, + "pi_no_extensions": true, + "pins_digest": "sha256:363ad70b0e3151ccc29df72fc7f876899eae2c31668ad2c8ff8b2cf115f66e1d" + } + }, + { + "sequence": 2, + "assertion_id": "stage.completed_within_deadline", + "source": "controller", + "evidence": { + "deadline_ms": 30000, + "finished_ms": 2, + "kind": "controller.checkpoint", + "monotonic_ms": 1, + "started_ms": 1 + } + }, + { + "sequence": 2, + "assertion_id": "discovery.skill_identity", + "source": "launcher_event", + "evidence": { + "arguments": [ + "fixture-launcher", + "{PORTABLE_LAUNCHER_TASK_BYTES}" + ], + "kind": "skill.discovery", + "skill": { + "content_digest": "sha256:6f8bc16b656f564a949c99f12d4cf0bcd04ec2c9b49a8b74fb65b2e355b5d182", + "format_version": "duo.skill/duo-delegation-loop/v1", + "name": "duo-delegation-loop" + }, + "task_digest": "sha256:5949809812a3ada98c95e77e9942af848e3080e4984df43a2d96fe5a24d5eab2" + } + }, + { + "sequence": 3, + "assertion_id": "stage.completed_within_deadline", + "source": "controller", + "evidence": { + "deadline_ms": 15000, + "finished_ms": 3, + "kind": "controller.checkpoint", + "monotonic_ms": 2, + "started_ms": 2 + } + }, + { + "sequence": 3, + "assertion_id": "preflight.ready", + "source": "controller", + "evidence": { + "kind": "controller.checkpoint", + "measurements": { + "all_required_pass": true, + "required_checks": 7, + "status": "ready" + }, + "monotonic_ms": 2 + } + }, + { + "sequence": 4, + "assertion_id": "stage.completed_within_deadline", + "source": "controller", + "evidence": { + "deadline_ms": 30000, + "finished_ms": 4, + "kind": "controller.checkpoint", + "monotonic_ms": 3, + "started_ms": 3 + } + }, + { + "sequence": 4, + "assertion_id": "launch.resolution", + "source": "duo_envelope", + "evidence": { + "argv_has_prompt": false, + "envelope": { + "features": [], + "operation": "session.launch", + "request_id": "req_fixture_4", + "result": { + "host": { + "host_source": "explicit-flag", + "instance_id": "hostinst_fixture", + "instance_label": "fixture", + "kind": "herdr", + "outranked_evidence": [], + "workspace_id": "workspace_fixture" + }, + "launch_resolution_id": "lrr_fixture", + "leaves": [ + { + "agent_runtime": "pi", + "declared_kind": "open", + "model_line": "gpt-5.6-luna", + "name": "main", + "outcome": "selected", + "relented_avoids": [] + } + ], + "selection": "ordered", + "session_id": "ses_fixture" + }, + "schema": "duo.external/v1", + "warnings": [] + } + } + }, + { + "sequence": 5, + "assertion_id": "stage.completed_within_deadline", + "source": "controller", + "evidence": { + "deadline_ms": 12000, + "finished_ms": 5, + "kind": "controller.checkpoint", + "monotonic_ms": 4, + "started_ms": 4 + } + }, + { + "sequence": 5, + "assertion_id": "bind.runtime_identity", + "source": "duo_envelope", + "evidence": { + "active_correlation_runtime_ids": [ + "run_fixture" + ], + "authority_instance_ids": [ + "run_fixture" + ], + "envelope": { + "features": [], + "operation": "session.inspect", + "request_id": "req_fixture_5", + "result": { + "attachments": [ + { + "attachment_id": "attachment_fixture", + "claim_held": true, + "container": "workspace-1:pane-1", + "epoch": { + "kind": "herdr.terminal_id", + "scope": "pane", + "value": "term_fixture" + }, + "integration_instance": "herdr:fixture", + "process_birth": { + "pid": 41210, + "started_at": "2026-09-17T00:00:00Z" + }, + "state": "attached" + } + ], + "lifecycle": "active", + "runtime_instance_id": "run_fixture", + "session_id": "ses_fixture" + }, + "schema": "duo.external/v1", + "warnings": [] + } + } + }, + { + "sequence": 6, + "assertion_id": "stage.completed_within_deadline", + "source": "controller", + "evidence": { + "deadline_ms": 30000, + "finished_ms": 6, + "kind": "controller.checkpoint", + "monotonic_ms": 5, + "started_ms": 5 + } + }, + { + "sequence": 6, + "assertion_id": "send.delivery", + "source": "duo_envelope", + "evidence": { + "envelope": { + "features": [], + "operation": "prompt.deliver", + "request_id": "req_fixture_6", + "result": { + "attempts": [ + { + "attempt_id": "attempt_fixture", + "realization": "native", + "recorded_result": "delivered", + "started_at": "2026-09-17T00:00:00Z" + } + ], + "command_id": "cmd_fixture", + "milestones": { + "accepted_at": "2026-09-17T00:00:00Z", + "acknowledged": false, + "activity_observed": false, + "delivered_at": "2026-09-17T00:00:01Z" + }, + "operation": "prompt.deliver", + "queue_policy": "queue_until_safe", + "responsibility_state": "delivered", + "retry": { + "action": "observe_existing_command", + "safe": false + }, + "revision": "4", + "target": { + "runtime_instance_id": "run_fixture", + "session_id": "ses_fixture" + } + }, + "schema": "duo.external/v1", + "warnings": [] + } + } + }, + { + "sequence": 7, + "assertion_id": "stage.completed_within_deadline", + "source": "controller", + "evidence": { + "deadline_ms": 20000, + "finished_ms": 7, + "kind": "controller.checkpoint", + "monotonic_ms": 6, + "started_ms": 6 + } + }, + { + "sequence": 7, + "assertion_id": "observe.assistant_text", + "source": "duo_envelope", + "evidence": { + "envelope": { + "features": [], + "operation": "conversation.list", + "request_id": "req_fixture_7", + "result": { + "items": [ + { + "author_role": "agent", + "blocks": [ + { + "content": { + "text": "DUO_PORTABLE_LAUNCHER_OK_V1" + }, + "position": "0", + "type": "text" + } + ], + "completion": "source_record_complete", + "received_at": "2026-09-17T00:00:00Z", + "record_id": "record_fixture", + "runtime_instance_id": "run_fixture", + "session_id": "ses_fixture" + } + ], + "session_id": "ses_fixture" + }, + "schema": "duo.external/v1", + "warnings": [] + } + } + }, + { + "sequence": 8, + "assertion_id": "stage.completed_within_deadline", + "source": "controller", + "evidence": { + "deadline_ms": 15000, + "finished_ms": 8, + "kind": "controller.checkpoint", + "monotonic_ms": 7, + "started_ms": 7 + } + }, + { + "sequence": 8, + "assertion_id": "command.delivery_integrity", + "source": "duo_envelope", + "evidence": { + "envelope": { + "features": [], + "operation": "command.inspect", + "request_id": "req_fixture_8", + "result": { + "attempts": [ + { + "attempt_id": "attempt_fixture", + "realization": "native", + "recorded_result": "delivered", + "started_at": "2026-09-17T00:00:00Z" + } + ], + "command_id": "cmd_fixture", + "milestones": { + "accepted_at": "2026-09-17T00:00:00Z", + "acknowledged": false, + "activity_observed": false, + "delivered_at": "2026-09-17T00:00:01Z" + }, + "operation": "prompt.deliver", + "queue_policy": "queue_until_safe", + "responsibility_state": "delivered", + "retry": { + "action": "observe_existing_command", + "safe": false + }, + "revision": "4", + "target": { + "runtime_instance_id": "run_fixture", + "session_id": "ses_fixture" + } + }, + "schema": "duo.external/v1", + "warnings": [] + }, + "unchanged": true + } + }, + { + "sequence": 9, + "assertion_id": "stage.completed_within_deadline", + "source": "controller", + "evidence": { + "deadline_ms": 15000, + "finished_ms": 9, + "kind": "controller.checkpoint", + "monotonic_ms": 8, + "started_ms": 8 + } + }, + { + "sequence": 9, + "assertion_id": "restart.authority_continuity", + "source": "controller", + "evidence": { + "kind": "controller.checkpoint", + "measurements": { + "attempts": 1, + "distinct_processes": true, + "outcome": "same_live", + "same_store": true + }, + "monotonic_ms": 8 + } + }, + { + "sequence": 10, + "assertion_id": "stage.completed_within_deadline", + "source": "controller", + "evidence": { + "deadline_ms": 12000, + "finished_ms": 10, + "kind": "controller.checkpoint", + "monotonic_ms": 9, + "started_ms": 9 + } + }, + { + "sequence": 10, + "assertion_id": "bind.runtime_identity", + "source": "duo_envelope", + "evidence": { + "active_correlation_runtime_ids": [ + "run_fixture" + ], + "authority_instance_ids": [ + "run_fixture" + ], + "envelope": { + "features": [], + "operation": "session.inspect", + "request_id": "req_fixture_10", + "result": { + "attachments": [ + { + "attachment_id": "attachment_fixture", + "claim_held": true, + "container": "workspace-1:pane-1", + "epoch": { + "kind": "herdr.terminal_id", + "scope": "pane", + "value": "term_fixture" + }, + "integration_instance": "herdr:fixture", + "process_birth": { + "pid": 41210, + "started_at": "2026-09-17T00:00:00Z" + }, + "state": "attached" + } + ], + "lifecycle": "active", + "runtime_instance_id": "run_fixture", + "session_id": "ses_fixture" + }, + "schema": "duo.external/v1", + "warnings": [] + } + } + }, + { + "sequence": 11, + "assertion_id": "stage.completed_within_deadline", + "source": "controller", + "evidence": { + "deadline_ms": 20000, + "finished_ms": 11, + "kind": "controller.checkpoint", + "monotonic_ms": 10, + "started_ms": 10 + } + }, + { + "sequence": 11, + "assertion_id": "observe.restart_text", + "source": "duo_envelope", + "evidence": { + "baseline_record_id": "record_fixture", + "envelope": { + "features": [], + "operation": "conversation.list", + "request_id": "req_fixture_11", + "result": { + "items": [ + { + "author_role": "agent", + "blocks": [ + { + "content": { + "text": "DUO_PORTABLE_LAUNCHER_OK_V1" + }, + "position": "0", + "type": "text" + } + ], + "completion": "source_record_complete", + "received_at": "2026-09-17T00:00:00Z", + "record_id": "record_fixture", + "runtime_instance_id": "run_fixture", + "session_id": "ses_fixture" + } + ], + "session_id": "ses_fixture" + }, + "schema": "duo.external/v1", + "warnings": [] + } + } + }, + { + "sequence": 12, + "assertion_id": "stage.completed_within_deadline", + "source": "controller", + "evidence": { + "deadline_ms": 15000, + "finished_ms": 12, + "kind": "controller.checkpoint", + "monotonic_ms": 11, + "started_ms": 11 + } + }, + { + "sequence": 12, + "assertion_id": "command.delivery_integrity", + "source": "duo_envelope", + "evidence": { + "envelope": { + "features": [], + "operation": "command.inspect", + "request_id": "req_fixture_12", + "result": { + "attempts": [ + { + "attempt_id": "attempt_fixture", + "realization": "native", + "recorded_result": "delivered", + "started_at": "2026-09-17T00:00:00Z" + } + ], + "command_id": "cmd_fixture", + "milestones": { + "accepted_at": "2026-09-17T00:00:00Z", + "acknowledged": false, + "activity_observed": false, + "delivered_at": "2026-09-17T00:00:01Z" + }, + "operation": "prompt.deliver", + "queue_policy": "queue_until_safe", + "responsibility_state": "delivered", + "retry": { + "action": "observe_existing_command", + "safe": false + }, + "revision": "4", + "target": { + "runtime_instance_id": "run_fixture", + "session_id": "ses_fixture" + } + }, + "schema": "duo.external/v1", + "warnings": [] + }, + "unchanged": true + } + }, + { + "sequence": 13, + "assertion_id": "stage.completed_within_deadline", + "source": "controller", + "evidence": { + "deadline_ms": 30000, + "finished_ms": 13, + "kind": "controller.checkpoint", + "monotonic_ms": 12, + "started_ms": 12 + } + }, + { + "sequence": 13, + "assertion_id": "send.idempotent_replay", + "source": "controller", + "evidence": { + "kind": "controller.checkpoint", + "measurements": { + "attempts": 1, + "new_turns": 0, + "same_command": true + }, + "monotonic_ms": 12 + } + }, + { + "sequence": 14, + "assertion_id": "stage.completed_within_deadline", + "source": "controller", + "evidence": { + "deadline_ms": 15000, + "finished_ms": 14, + "kind": "controller.checkpoint", + "monotonic_ms": 13, + "started_ms": 13 + } + }, + { + "sequence": 14, + "assertion_id": "command.idempotent_no_duplication", + "source": "controller", + "evidence": { + "kind": "controller.checkpoint", + "measurements": { + "attempts_delta": 0, + "commands_delta": 0, + "turns_delta": 0 + }, + "monotonic_ms": 13 + } + }, + { + "sequence": 15, + "assertion_id": "stage.completed_within_deadline", + "source": "controller", + "evidence": { + "deadline_ms": 30000, + "finished_ms": 15, + "kind": "controller.checkpoint", + "monotonic_ms": 14, + "started_ms": 14 + } + }, + { + "sequence": 15, + "assertion_id": "send.idempotency_conflict", + "source": "duo_envelope", + "evidence": { + "envelope": { + "error": { + "class": "conflict", + "code": "command.idempotency_conflict", + "details": { + "existing_digest": "sha256:b40d3a68eb33083b92199d734da2dece28395a6acc6d5e2cb9d7032dae4d11c7", + "idempotency_key": "portable-launcher-v1-primary", + "request_digest": "sha256:dbe904a42a9d9d9cdf7908508fa393ce43c9c53ea16a8cb8c9b82e7005f602bc" + }, + "effect": "no_effect", + "message": "fixture idempotency conflict", + "retry": { + "action": "use_new_idempotency_key", + "safe": false + }, + "target": { + "id": "cmd_fixture", + "kind": "prompt_command" + } + }, + "operation": "prompt.deliver", + "request_id": "req_fixture_15", + "schema": "duo.external/v1" + }, + "original_command_id": "cmd_fixture" + } + }, + { + "sequence": 16, + "assertion_id": "stage.completed_within_deadline", + "source": "controller", + "evidence": { + "deadline_ms": 15000, + "finished_ms": 16, + "kind": "controller.checkpoint", + "monotonic_ms": 15, + "started_ms": 15 + } + }, + { + "sequence": 16, + "assertion_id": "command.idempotency_no_drift", + "source": "controller", + "evidence": { + "kind": "controller.checkpoint", + "measurements": { + "attempts_delta": 0, + "commands_delta": 0, + "queue_delta": 0, + "turns_delta": 0 + }, + "monotonic_ms": 15 + } + }, + { + "sequence": 17, + "assertion_id": "blocked.induction_available", + "source": "controller", + "evidence": { + "kind": "controller.checkpoint", + "measurements": { + "available": false + }, + "monotonic_ms": 16 + } + }, + { + "sequence": 18, + "assertion_id": "prerequisite.not_reached", + "source": "controller", + "evidence": { + "kind": "controller.checkpoint", + "measurements": { + "reached": false + }, + "monotonic_ms": 17 + } + }, + { + "sequence": 19, + "assertion_id": "prerequisite.not_reached", + "source": "controller", + "evidence": { + "kind": "controller.checkpoint", + "measurements": { + "reached": false + }, + "monotonic_ms": 18 + } + }, + { + "sequence": 20, + "assertion_id": "prerequisite.not_reached", + "source": "controller", + "evidence": { + "kind": "controller.checkpoint", + "measurements": { + "reached": false + }, + "monotonic_ms": 19 + } + }, + { + "sequence": 21, + "assertion_id": "prerequisite.not_reached", + "source": "controller", + "evidence": { + "kind": "controller.checkpoint", + "measurements": { + "reached": false + }, + "monotonic_ms": 20 + } + }, + { + "sequence": 22, + "assertion_id": "stage.completed_within_deadline", + "source": "controller", + "evidence": { + "deadline_ms": 30000, + "finished_ms": 22, + "kind": "controller.checkpoint", + "monotonic_ms": 21, + "started_ms": 21 + } + }, + { + "sequence": 22, + "assertion_id": "launch.resolution", + "source": "duo_envelope", + "evidence": { + "argv_has_prompt": false, + "envelope": { + "features": [], + "operation": "session.launch", + "request_id": "req_fixture_22", + "result": { + "host": { + "host_source": "explicit-flag", + "instance_id": "hostinst_fixture", + "instance_label": "fixture", + "kind": "herdr", + "outranked_evidence": [], + "workspace_id": "workspace_fixture" + }, + "launch_resolution_id": "lrr_fixture", + "leaves": [ + { + "agent_runtime": "pi", + "declared_kind": "open", + "model_line": "gpt-5.6-luna", + "name": "main", + "outcome": "selected", + "relented_avoids": [] + } + ], + "selection": "ordered", + "session_id": "ses_fixture" + }, + "schema": "duo.external/v1", + "warnings": [] + } + } + }, + { + "sequence": 23, + "assertion_id": "stage.completed_within_deadline", + "source": "controller", + "evidence": { + "deadline_ms": 12000, + "finished_ms": 23, + "kind": "controller.checkpoint", + "monotonic_ms": 22, + "started_ms": 22 + } + }, + { + "sequence": 23, + "assertion_id": "bind.runtime_identity", + "source": "duo_envelope", + "evidence": { + "active_correlation_runtime_ids": [ + "run_fixture" + ], + "authority_instance_ids": [ + "run_fixture" + ], + "envelope": { + "features": [], + "operation": "session.inspect", + "request_id": "req_fixture_23", + "result": { + "attachments": [ + { + "attachment_id": "attachment_fixture", + "claim_held": true, + "container": "workspace-1:pane-1", + "epoch": { + "kind": "herdr.terminal_id", + "scope": "pane", + "value": "term_fixture" + }, + "integration_instance": "herdr:fixture", + "process_birth": { + "pid": 41210, + "started_at": "2026-09-17T00:00:00Z" + }, + "state": "attached" + } + ], + "lifecycle": "active", + "runtime_instance_id": "run_fixture", + "session_id": "ses_fixture" + }, + "schema": "duo.external/v1", + "warnings": [] + } + } + }, + { + "sequence": 24, + "assertion_id": "stage.completed_within_deadline", + "source": "controller", + "evidence": { + "deadline_ms": 30000, + "finished_ms": 24, + "kind": "controller.checkpoint", + "monotonic_ms": 23, + "started_ms": 23 + } + }, + { + "sequence": 24, + "assertion_id": "send.delivery", + "source": "duo_envelope", + "evidence": { + "envelope": { + "features": [], + "operation": "prompt.deliver", + "request_id": "req_fixture_24", + "result": { + "attempts": [ + { + "attempt_id": "attempt_fixture", + "realization": "native", + "recorded_result": "delivered", + "started_at": "2026-09-17T00:00:00Z" + } + ], + "command_id": "cmd_fixture", + "milestones": { + "accepted_at": "2026-09-17T00:00:00Z", + "acknowledged": false, + "activity_observed": false, + "delivered_at": "2026-09-17T00:00:01Z" + }, + "operation": "prompt.deliver", + "queue_policy": "queue_until_safe", + "responsibility_state": "delivered", + "retry": { + "action": "observe_existing_command", + "safe": false + }, + "revision": "4", + "target": { + "runtime_instance_id": "run_fixture", + "session_id": "ses_fixture" + } + }, + "schema": "duo.external/v1", + "warnings": [] + } + } + }, + { + "sequence": 25, + "assertion_id": "stage.completed_within_deadline", + "source": "controller", + "evidence": { + "deadline_ms": 20000, + "finished_ms": 25, + "kind": "controller.checkpoint", + "monotonic_ms": 24, + "started_ms": 24 + } + }, + { + "sequence": 25, + "assertion_id": "observe.exited", + "source": "controller", + "evidence": { + "kind": "controller.checkpoint", + "measurements": { + "attempts": 1, + "condition": "exited", + "process_success": true, + "same_instance": true + }, + "monotonic_ms": 24, + "target_verified": true + } + }, + { + "sequence": 26, + "assertion_id": "stage.completed_within_deadline", + "source": "controller", + "evidence": { + "deadline_ms": 15000, + "finished_ms": 26, + "kind": "controller.checkpoint", + "monotonic_ms": 25, + "started_ms": 25 + } + }, + { + "sequence": 26, + "assertion_id": "command.delivery_integrity", + "source": "duo_envelope", + "evidence": { + "envelope": { + "features": [], + "operation": "command.inspect", + "request_id": "req_fixture_26", + "result": { + "attempts": [ + { + "attempt_id": "attempt_fixture", + "realization": "native", + "recorded_result": "delivered", + "started_at": "2026-09-17T00:00:00Z" + } + ], + "command_id": "cmd_fixture", + "milestones": { + "accepted_at": "2026-09-17T00:00:00Z", + "acknowledged": false, + "activity_observed": false, + "delivered_at": "2026-09-17T00:00:01Z" + }, + "operation": "prompt.deliver", + "queue_policy": "queue_until_safe", + "responsibility_state": "delivered", + "retry": { + "action": "observe_existing_command", + "safe": false + }, + "revision": "4", + "target": { + "runtime_instance_id": "run_fixture", + "session_id": "ses_fixture" + } + }, + "schema": "duo.external/v1", + "warnings": [] + }, + "unchanged": true + } + }, + { + "sequence": 27, + "assertion_id": "stage.completed_within_deadline", + "source": "controller", + "evidence": { + "deadline_ms": 30000, + "finished_ms": 27, + "kind": "controller.checkpoint", + "monotonic_ms": 26, + "started_ms": 26 + } + }, + { + "sequence": 27, + "assertion_id": "launch.resolution", + "source": "duo_envelope", + "evidence": { + "argv_has_prompt": false, + "envelope": { + "features": [], + "operation": "session.launch", + "request_id": "req_fixture_27", + "result": { + "host": { + "host_source": "explicit-flag", + "instance_id": "hostinst_fixture", + "instance_label": "fixture", + "kind": "herdr", + "outranked_evidence": [], + "workspace_id": "workspace_fixture" + }, + "launch_resolution_id": "lrr_fixture", + "leaves": [ + { + "agent_runtime": "pi", + "declared_kind": "open", + "model_line": "gpt-5.6-luna", + "name": "main", + "outcome": "selected", + "relented_avoids": [] + } + ], + "selection": "ordered", + "session_id": "ses_fixture" + }, + "schema": "duo.external/v1", + "warnings": [] + } + } + }, + { + "sequence": 28, + "assertion_id": "stage.completed_within_deadline", + "source": "controller", + "evidence": { + "deadline_ms": 12000, + "finished_ms": 28, + "kind": "controller.checkpoint", + "monotonic_ms": 27, + "started_ms": 27 + } + }, + { + "sequence": 28, + "assertion_id": "bind.runtime_identity", + "source": "duo_envelope", + "evidence": { + "active_correlation_runtime_ids": [ + "run_fixture" + ], + "authority_instance_ids": [ + "run_fixture" + ], + "envelope": { + "features": [], + "operation": "session.inspect", + "request_id": "req_fixture_28", + "result": { + "attachments": [ + { + "attachment_id": "attachment_fixture", + "claim_held": true, + "container": "workspace-1:pane-1", + "epoch": { + "kind": "herdr.terminal_id", + "scope": "pane", + "value": "term_fixture" + }, + "integration_instance": "herdr:fixture", + "process_birth": { + "pid": 41210, + "started_at": "2026-09-17T00:00:00Z" + }, + "state": "attached" + } + ], + "lifecycle": "active", + "runtime_instance_id": "run_fixture", + "session_id": "ses_fixture" + }, + "schema": "duo.external/v1", + "warnings": [] + } + } + }, + { + "sequence": 29, + "assertion_id": "stage.completed_within_deadline", + "source": "controller", + "evidence": { + "deadline_ms": 30000, + "finished_ms": 29, + "kind": "controller.checkpoint", + "monotonic_ms": 28, + "started_ms": 28 + } + }, + { + "sequence": 29, + "assertion_id": "send.delivery", + "source": "duo_envelope", + "evidence": { + "envelope": { + "features": [], + "operation": "prompt.deliver", + "request_id": "req_fixture_29", + "result": { + "attempts": [ + { + "attempt_id": "attempt_fixture", + "realization": "native", + "recorded_result": "delivered", + "started_at": "2026-09-17T00:00:00Z" + } + ], + "command_id": "cmd_fixture", + "milestones": { + "accepted_at": "2026-09-17T00:00:00Z", + "acknowledged": false, + "activity_observed": false, + "delivered_at": "2026-09-17T00:00:01Z" + }, + "operation": "prompt.deliver", + "queue_policy": "queue_until_safe", + "responsibility_state": "delivered", + "retry": { + "action": "observe_existing_command", + "safe": false + }, + "revision": "4", + "target": { + "runtime_instance_id": "run_fixture", + "session_id": "ses_fixture" + } + }, + "schema": "duo.external/v1", + "warnings": [] + } + } + }, + { + "sequence": 30, + "assertion_id": "stage.completed_within_deadline", + "source": "controller", + "evidence": { + "deadline_ms": 22500, + "finished_ms": 20029, + "kind": "controller.checkpoint", + "monotonic_ms": 29, + "started_ms": 29 + } + }, + { + "sequence": 30, + "assertion_id": "observe.timeout", + "source": "controller", + "evidence": { + "kind": "controller.checkpoint", + "measurements": { + "assistant_blocks": 0, + "attempts": 1, + "delivered": true, + "elapsed_max_ms": 22500, + "elapsed_min_ms": 20000, + "elapsed_ms": 20000, + "outer_timed_out": false, + "process_success": true, + "stopped_verified": true + }, + "monotonic_ms": 29, + "target_verified": true, + "timeout_probe_supported": true + } + }, + { + "sequence": 31, + "assertion_id": "stage.completed_within_deadline", + "source": "controller", + "evidence": { + "deadline_ms": 15000, + "finished_ms": 20030, + "kind": "controller.checkpoint", + "monotonic_ms": 20029, + "started_ms": 20029 + } + }, + { + "sequence": 31, + "assertion_id": "command.delivery_integrity", + "source": "duo_envelope", + "evidence": { + "envelope": { + "features": [], + "operation": "command.inspect", + "request_id": "req_fixture_31", + "result": { + "attempts": [ + { + "attempt_id": "attempt_fixture", + "realization": "native", + "recorded_result": "delivered", + "started_at": "2026-09-17T00:00:00Z" + } + ], + "command_id": "cmd_fixture", + "milestones": { + "accepted_at": "2026-09-17T00:00:00Z", + "acknowledged": false, + "activity_observed": false, + "delivered_at": "2026-09-17T00:00:01Z" + }, + "operation": "prompt.deliver", + "queue_policy": "queue_until_safe", + "responsibility_state": "delivered", + "retry": { + "action": "observe_existing_command", + "safe": false + }, + "revision": "4", + "target": { + "runtime_instance_id": "run_fixture", + "session_id": "ses_fixture" + } + }, + "schema": "duo.external/v1", + "warnings": [] + }, + "unchanged": true + } + }, + { + "sequence": 32, + "assertion_id": "stage.completed_within_deadline", + "source": "controller", + "evidence": { + "deadline_ms": 30000, + "finished_ms": 20031, + "kind": "controller.checkpoint", + "monotonic_ms": 20030, + "started_ms": 20030 + } + }, + { + "sequence": 32, + "assertion_id": "cleanup.complete", + "source": "controller", + "evidence": { + "kind": "controller.checkpoint", + "measurements": { + "export_complete": true, + "panes_remaining": 0, + "pids_remaining": 0, + "root_removed": true, + "sockets_remaining": 0 + }, + "monotonic_ms": 20030 + } + } + ] +} diff --git a/contracts/fixtures/duo-portable-launcher-conformance-v1/evidence-index.json b/contracts/fixtures/duo-portable-launcher-conformance-v1/evidence-index.json new file mode 100644 index 0000000..4435d34 --- /dev/null +++ b/contracts/fixtures/duo-portable-launcher-conformance-v1/evidence-index.json @@ -0,0 +1,14 @@ +{ + "schema": "duo.portable-launcher-conformance-evidence-index/v1", + "policy": "portable-launcher-evidence/v1", + "blobs": [ + { + "ref": "blob:sha256:045edb8695b91fd54b3d792ef5cda267f0b812a1d44765ad7290091080ca67e1", + "media_type": "application/json", + "bytes": 35345, + "sha256": "sha256:045edb8695b91fd54b3d792ef5cda267f0b812a1d44765ad7290091080ca67e1", + "scrub_status": "pass", + "producer": "common_collector" + } + ] +} diff --git a/contracts/fixtures/duo-portable-launcher-conformance-v1/result-rejected-fabricated-pass.json b/contracts/fixtures/duo-portable-launcher-conformance-v1/result-rejected-fabricated-pass.json new file mode 100644 index 0000000..ba8a136 --- /dev/null +++ b/contracts/fixtures/duo-portable-launcher-conformance-v1/result-rejected-fabricated-pass.json @@ -0,0 +1,1473 @@ +{ + "schema": "duo.portable-launcher-conformance-result/v1", + "suite": { + "name": "portable-launcher-delegation", + "revision": 1, + "manifest_digest": "sha256:01e8e6bd80ceb441be6dc128aca023a1bede31b655e74b12ee3da0c2fc6cc204", + "oracle_digest": "sha256:a305744fc34af96b5bbd916b9de3aa1bc94f59ce4ea9316b62bb0a8dd5dd22c5" + }, + "run": { + "run_id": "fixture-structural-blocked", + "observed_at": "2026-09-17T00:00:00Z", + "host_os": "linux", + "host_arch": "x86_64", + "fixture_root": "$RUN", + "plugins_enabled": false, + "mcp_enabled": false, + "terminal_input_used": false + }, + "pins": { + "launcher": { + "name": "amp", + "version": "0.0.1789675234-g2899fe", + "executable_sha256": "sha256:f351217dac739614b4d9b3eacad728ce6d61647ab1f5d4a4de44d99aba6598ee" + }, + "duo": { + "version": "fixture", + "commit": "0123456789abcdef0123456789abcdef01234567", + "build_date": "2026-09-17T00:00:00Z", + "executable_sha256": "sha256:2af74f65db28670aa9a5439c4eb48137c63dea3db07f31b3143f672dffddaf11" + }, + "skill": { + "name": "duo-delegation-loop", + "format_version": "duo.skill/duo-delegation-loop/v1", + "content_digest": "sha256:6f8bc16b656f564a949c99f12d4cf0bcd04ec2c9b49a8b74fb65b2e355b5d182", + "installation_id": "fixture-installation" + }, + "config": { + "schema": "duo.config/v3", + "effective_digest": "sha256:dc3fbd7d29934bfcb08cf280ac04b77e6bde9a5da091e3a892062f757a71ced0" + }, + "authority": { + "schema_version": 1, + "store_digest_before": "sha256:b58d01a3400fbcf9af5b8f043c5e4bf2411631be0a51c7d8b7e466875289ab02", + "store_digest_after": "sha256:49a2b38653a56a91b6664cb633dd9918da8d6a47d0f79e75814bb8381c16e36b" + }, + "host": { + "name": "herdr", + "version": "0.8.2", + "protocol": "herdr-socket-api/20", + "schema_digest": "sha256:c48f1f54ee0150ca27e11fd44455fe94aeadb20fdf4e4a62393ed822a4e5b150", + "executable_sha256": "sha256:7db471d26dcc133939e1fc80074b7f1f90bf181eebaacf48bfbb2ad2962964da" + }, + "runtime": { + "name": "pi", + "version": "0.83.0", + "format": "pi-session-jsonl/v3", + "adapter_build": "stage1", + "conformance_record": "pi-0.83.0-2026-08-23", + "executable_sha256": "sha256:9b5e9b1aa2486835b24f7842b027d187d940986b020f584736af7a4927f47d4c", + "delivery_asset_sha256": "sha256:2708a3435821fb2e72302ee56d8a07fe62c265214ae8fd342e385c03ef245a23" + }, + "model": { + "provider": "openai-codex", + "model_line": "gpt-5.6-luna" + }, + "external_schema": { + "identity": "duo.external/v1", + "digest": "sha256:8b055ff60f0ca5f3da46302671591aa0dae61185cd9f1f15fcceb36d841e7497" + } + }, + "stages": [ + { + "sequence": 1, + "stage": "setup", + "case": "run", + "verdict": "pass", + "outcome": "success", + "started_offset_ms": 0, + "duration_ms": 1, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + }, + "actual": { + "within_deadline": true + }, + "matched": true + }, + { + "id": "setup.fixture_isolated_and_pinned", + "expected": { + "fixture_root": "$RUN", + "mcp_enabled": false, + "plugins_enabled": false, + "shared_state": false, + "terminal_input_used": false + }, + "actual": { + "fixture_root": "$RUN", + "mcp_enabled": false, + "plugins_enabled": false, + "shared_state": false, + "terminal_input_used": false + }, + "matched": true + } + ], + "evidence": [ + "blob:sha256:045edb8695b91fd54b3d792ef5cda267f0b812a1d44765ad7290091080ca67e1" + ], + "error": null + }, + { + "sequence": 2, + "stage": "discovery", + "case": "run", + "verdict": "pass", + "outcome": "success", + "started_offset_ms": 1, + "duration_ms": 1, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + }, + "actual": { + "within_deadline": true + }, + "matched": true + }, + { + "id": "discovery.skill_identity", + "expected": { + "content_digest": "sha256:6f8bc16b656f564a949c99f12d4cf0bcd04ec2c9b49a8b74fb65b2e355b5d182", + "format_version": "duo.skill/duo-delegation-loop/v1", + "name": "duo-delegation-loop" + }, + "actual": { + "content_digest": "sha256:6f8bc16b656f564a949c99f12d4cf0bcd04ec2c9b49a8b74fb65b2e355b5d182", + "format_version": "duo.skill/duo-delegation-loop/v1", + "name": "duo-delegation-loop" + }, + "matched": true + } + ], + "evidence": [ + "blob:sha256:045edb8695b91fd54b3d792ef5cda267f0b812a1d44765ad7290091080ca67e1" + ], + "error": null + }, + { + "sequence": 3, + "stage": "preflight", + "case": "run", + "verdict": "pass", + "outcome": "success", + "started_offset_ms": 2, + "duration_ms": 1, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + }, + "actual": { + "within_deadline": true + }, + "matched": true + }, + { + "id": "preflight.ready", + "expected": { + "all_required_pass": true, + "required_checks": 7, + "status": "ready" + }, + "actual": { + "all_required_pass": true, + "required_checks": 7, + "status": "ready" + }, + "matched": true + } + ], + "evidence": [ + "blob:sha256:045edb8695b91fd54b3d792ef5cda267f0b812a1d44765ad7290091080ca67e1" + ], + "error": null + }, + { + "sequence": 4, + "stage": "launch", + "case": "happy", + "verdict": "pass", + "outcome": "success", + "started_offset_ms": 3, + "duration_ms": 1, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + }, + "actual": { + "within_deadline": true + }, + "matched": true + }, + { + "id": "launch.resolution", + "expected": { + "host": "herdr", + "prelaunch_prompt": false, + "runtime": "pi", + "selected_leaves": 1 + }, + "actual": { + "host": "herdr", + "prelaunch_prompt": false, + "runtime": "pi", + "selected_leaves": 1 + }, + "matched": true + } + ], + "evidence": [ + "blob:sha256:045edb8695b91fd54b3d792ef5cda267f0b812a1d44765ad7290091080ca67e1" + ], + "error": null + }, + { + "sequence": 5, + "stage": "bind", + "case": "happy", + "verdict": "pass", + "outcome": "success", + "started_offset_ms": 4, + "duration_ms": 1, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + }, + "actual": { + "within_deadline": true + }, + "matched": true + }, + { + "id": "bind.runtime_identity", + "expected": { + "attachment_claimed": true, + "correlations": 1, + "instances": 1 + }, + "actual": { + "attachment_claimed": true, + "correlations": 1, + "instances": 1 + }, + "matched": true + } + ], + "evidence": [ + "blob:sha256:045edb8695b91fd54b3d792ef5cda267f0b812a1d44765ad7290091080ca67e1" + ], + "error": null + }, + { + "sequence": 6, + "stage": "send", + "case": "happy", + "verdict": "pass", + "outcome": "success", + "started_offset_ms": 5, + "duration_ms": 1, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + }, + "actual": { + "within_deadline": true + }, + "matched": true + }, + { + "id": "send.delivery", + "expected": { + "acknowledged": false, + "activity_observed": false, + "attempts": 1, + "queue_policy": "queue_until_safe", + "realization": "native", + "recorded_result": "delivered", + "retry_action": "observe_existing_command", + "retry_safe": false, + "state": "delivered" + }, + "actual": { + "acknowledged": false, + "activity_observed": false, + "attempts": 1, + "queue_policy": "queue_until_safe", + "realization": "native", + "recorded_result": "delivered", + "retry_action": "observe_existing_command", + "retry_safe": false, + "state": "delivered" + }, + "matched": true + } + ], + "evidence": [ + "blob:sha256:045edb8695b91fd54b3d792ef5cda267f0b812a1d44765ad7290091080ca67e1" + ], + "error": null + }, + { + "sequence": 7, + "stage": "observe", + "case": "happy", + "verdict": "pass", + "outcome": "success", + "started_offset_ms": 6, + "duration_ms": 1, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + }, + "actual": { + "within_deadline": true + }, + "matched": true + }, + { + "id": "observe.assistant_text", + "expected": { + "assistant_blocks": 1, + "source_complete": true, + "text_digest": "sha256:b3300f2f6e0338d2b672891be66827d95779a2eabf4a75dcae1b6f56c9a4dc98" + }, + "actual": { + "assistant_blocks": 1, + "source_complete": true, + "text_digest": "sha256:b3300f2f6e0338d2b672891be66827d95779a2eabf4a75dcae1b6f56c9a4dc98" + }, + "matched": true + } + ], + "evidence": [ + "blob:sha256:045edb8695b91fd54b3d792ef5cda267f0b812a1d44765ad7290091080ca67e1" + ], + "error": null + }, + { + "sequence": 8, + "stage": "command_inspection", + "case": "happy", + "verdict": "pass", + "outcome": "success", + "started_offset_ms": 7, + "duration_ms": 1, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + }, + "actual": { + "within_deadline": true + }, + "matched": true + }, + { + "id": "command.delivery_integrity", + "expected": { + "attempts": 1, + "queue_policy": "queue_until_safe", + "realization": "native", + "recorded_result": "delivered", + "retry_action": "observe_existing_command", + "retry_safe": false, + "state": "delivered", + "unchanged": true + }, + "actual": { + "attempts": 1, + "queue_policy": "queue_until_safe", + "realization": "native", + "recorded_result": "delivered", + "retry_action": "observe_existing_command", + "retry_safe": false, + "state": "delivered", + "unchanged": true + }, + "matched": true + } + ], + "evidence": [ + "blob:sha256:045edb8695b91fd54b3d792ef5cda267f0b812a1d44765ad7290091080ca67e1" + ], + "error": null + }, + { + "sequence": 9, + "stage": "restart", + "case": "restart", + "verdict": "pass", + "outcome": "success", + "started_offset_ms": 8, + "duration_ms": 1, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + }, + "actual": { + "within_deadline": true + }, + "matched": true + }, + { + "id": "restart.authority_continuity", + "expected": { + "attempts": 1, + "distinct_processes": true, + "outcome": "same_live", + "same_store": true + }, + "actual": { + "attempts": 1, + "distinct_processes": true, + "outcome": "same_live", + "same_store": true + }, + "matched": true + } + ], + "evidence": [ + "blob:sha256:045edb8695b91fd54b3d792ef5cda267f0b812a1d44765ad7290091080ca67e1" + ], + "error": null + }, + { + "sequence": 10, + "stage": "bind", + "case": "restart", + "verdict": "pass", + "outcome": "success", + "started_offset_ms": 9, + "duration_ms": 1, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + }, + "actual": { + "within_deadline": true + }, + "matched": true + }, + { + "id": "bind.runtime_identity", + "expected": { + "attachment_claimed": true, + "correlations": 1, + "instances": 1 + }, + "actual": { + "attachment_claimed": true, + "correlations": 1, + "instances": 1 + }, + "matched": true + } + ], + "evidence": [ + "blob:sha256:045edb8695b91fd54b3d792ef5cda267f0b812a1d44765ad7290091080ca67e1" + ], + "error": null + }, + { + "sequence": 11, + "stage": "observe", + "case": "restart", + "verdict": "pass", + "outcome": "success", + "started_offset_ms": 10, + "duration_ms": 1, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + }, + "actual": { + "within_deadline": true + }, + "matched": true + }, + { + "id": "observe.restart_text", + "expected": { + "same_record": true, + "text_digest": "sha256:b3300f2f6e0338d2b672891be66827d95779a2eabf4a75dcae1b6f56c9a4dc98" + }, + "actual": { + "same_record": true, + "text_digest": "sha256:b3300f2f6e0338d2b672891be66827d95779a2eabf4a75dcae1b6f56c9a4dc98" + }, + "matched": true + } + ], + "evidence": [ + "blob:sha256:045edb8695b91fd54b3d792ef5cda267f0b812a1d44765ad7290091080ca67e1" + ], + "error": null + }, + { + "sequence": 12, + "stage": "command_inspection", + "case": "restart", + "verdict": "pass", + "outcome": "success", + "started_offset_ms": 11, + "duration_ms": 1, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + }, + "actual": { + "within_deadline": true + }, + "matched": true + }, + { + "id": "command.delivery_integrity", + "expected": { + "attempts": 1, + "queue_policy": "queue_until_safe", + "realization": "native", + "recorded_result": "delivered", + "retry_action": "observe_existing_command", + "retry_safe": false, + "state": "delivered", + "unchanged": true + }, + "actual": { + "attempts": 1, + "queue_policy": "queue_until_safe", + "realization": "native", + "recorded_result": "delivered", + "retry_action": "observe_existing_command", + "retry_safe": false, + "state": "delivered", + "unchanged": true + }, + "matched": true + } + ], + "evidence": [ + "blob:sha256:045edb8695b91fd54b3d792ef5cda267f0b812a1d44765ad7290091080ca67e1" + ], + "error": null + }, + { + "sequence": 13, + "stage": "send", + "case": "same_key_same_text", + "verdict": "pass", + "outcome": "success", + "started_offset_ms": 12, + "duration_ms": 1, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + }, + "actual": { + "within_deadline": true + }, + "matched": true + }, + { + "id": "send.idempotent_replay", + "expected": { + "attempts": 1, + "new_turns": 0, + "same_command": true + }, + "actual": { + "attempts": 1, + "new_turns": 0, + "same_command": true + }, + "matched": true + } + ], + "evidence": [ + "blob:sha256:045edb8695b91fd54b3d792ef5cda267f0b812a1d44765ad7290091080ca67e1" + ], + "error": null + }, + { + "sequence": 14, + "stage": "command_inspection", + "case": "same_key_same_text", + "verdict": "pass", + "outcome": "success", + "started_offset_ms": 13, + "duration_ms": 1, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + }, + "actual": { + "within_deadline": true + }, + "matched": true + }, + { + "id": "command.idempotent_no_duplication", + "expected": { + "attempts_delta": 0, + "commands_delta": 0, + "turns_delta": 0 + }, + "actual": { + "attempts_delta": 0, + "commands_delta": 0, + "turns_delta": 0 + }, + "matched": true + } + ], + "evidence": [ + "blob:sha256:045edb8695b91fd54b3d792ef5cda267f0b812a1d44765ad7290091080ca67e1" + ], + "error": null + }, + { + "sequence": 15, + "stage": "send", + "case": "same_key_different_text", + "verdict": "pass", + "outcome": "success", + "started_offset_ms": 14, + "duration_ms": 1, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + }, + "actual": { + "within_deadline": true + }, + "matched": true + }, + { + "id": "send.idempotency_conflict", + "expected": { + "code": "command.idempotency_conflict", + "effect": "no_effect", + "existing_digest": "sha256:b40d3a68eb33083b92199d734da2dece28395a6acc6d5e2cb9d7032dae4d11c7", + "idempotency_key": "portable-launcher-v1-primary", + "request_digest": "sha256:dbe904a42a9d9d9cdf7908508fa393ce43c9c53ea16a8cb8c9b82e7005f602bc", + "retry_action": "use_new_idempotency_key", + "retry_safe": false, + "target_original": true + }, + "actual": { + "code": "command.idempotency_conflict", + "effect": "no_effect", + "existing_digest": "sha256:b40d3a68eb33083b92199d734da2dece28395a6acc6d5e2cb9d7032dae4d11c7", + "idempotency_key": "portable-launcher-v1-primary", + "request_digest": "sha256:dbe904a42a9d9d9cdf7908508fa393ce43c9c53ea16a8cb8c9b82e7005f602bc", + "retry_action": "use_new_idempotency_key", + "retry_safe": false, + "target_original": true + }, + "matched": true + } + ], + "evidence": [ + "blob:sha256:045edb8695b91fd54b3d792ef5cda267f0b812a1d44765ad7290091080ca67e1" + ], + "error": null + }, + { + "sequence": 16, + "stage": "command_inspection", + "case": "same_key_different_text", + "verdict": "pass", + "outcome": "success", + "started_offset_ms": 15, + "duration_ms": 1, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + }, + "actual": { + "within_deadline": true + }, + "matched": true + }, + { + "id": "command.idempotency_no_drift", + "expected": { + "attempts_delta": 0, + "commands_delta": 0, + "queue_delta": 0, + "turns_delta": 0 + }, + "actual": { + "attempts_delta": 0, + "commands_delta": 0, + "queue_delta": 0, + "turns_delta": 0 + }, + "matched": true + } + ], + "evidence": [ + "blob:sha256:045edb8695b91fd54b3d792ef5cda267f0b812a1d44765ad7290091080ca67e1" + ], + "error": null + }, + { + "sequence": 17, + "stage": "launch", + "case": "blocked", + "verdict": "pass", + "outcome": "success", + "started_offset_ms": 16, + "duration_ms": 1, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + }, + "actual": { + "within_deadline": true + }, + "matched": true + }, + { + "id": "launch.resolution", + "expected": { + "host": "herdr", + "prelaunch_prompt": false, + "runtime": "pi", + "selected_leaves": 1 + }, + "actual": { + "host": "herdr", + "prelaunch_prompt": false, + "runtime": "pi", + "selected_leaves": 1 + }, + "matched": true + } + ], + "evidence": [ + "blob:sha256:045edb8695b91fd54b3d792ef5cda267f0b812a1d44765ad7290091080ca67e1" + ], + "error": null + }, + { + "sequence": 18, + "stage": "bind", + "case": "blocked", + "verdict": "pass", + "outcome": "success", + "started_offset_ms": 17, + "duration_ms": 1, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + }, + "actual": { + "within_deadline": true + }, + "matched": true + }, + { + "id": "bind.runtime_identity", + "expected": { + "attachment_claimed": true, + "correlations": 1, + "instances": 1 + }, + "actual": { + "attachment_claimed": true, + "correlations": 1, + "instances": 1 + }, + "matched": true + } + ], + "evidence": [ + "blob:sha256:045edb8695b91fd54b3d792ef5cda267f0b812a1d44765ad7290091080ca67e1" + ], + "error": null + }, + { + "sequence": 19, + "stage": "send", + "case": "blocked", + "verdict": "pass", + "outcome": "success", + "started_offset_ms": 18, + "duration_ms": 1, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + }, + "actual": { + "within_deadline": true + }, + "matched": true + }, + { + "id": "send.delivery", + "expected": { + "acknowledged": false, + "activity_observed": false, + "attempts": 1, + "queue_policy": "queue_until_safe", + "realization": "native", + "recorded_result": "delivered", + "retry_action": "observe_existing_command", + "retry_safe": false, + "state": "delivered" + }, + "actual": { + "acknowledged": false, + "activity_observed": false, + "attempts": 1, + "queue_policy": "queue_until_safe", + "realization": "native", + "recorded_result": "delivered", + "retry_action": "observe_existing_command", + "retry_safe": false, + "state": "delivered" + }, + "matched": true + } + ], + "evidence": [ + "blob:sha256:045edb8695b91fd54b3d792ef5cda267f0b812a1d44765ad7290091080ca67e1" + ], + "error": null + }, + { + "sequence": 20, + "stage": "observe", + "case": "blocked", + "verdict": "pass", + "outcome": "blocked", + "started_offset_ms": 19, + "duration_ms": 1, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + }, + "actual": { + "within_deadline": true + }, + "matched": true + }, + { + "id": "observe.blocked", + "expected": { + "assistant_blocks": 0, + "attempts": 1, + "condition": "blocked", + "nonterminal": true + }, + "actual": { + "assistant_blocks": 0, + "attempts": 1, + "condition": "blocked", + "nonterminal": true + }, + "matched": true + } + ], + "evidence": [ + "blob:sha256:045edb8695b91fd54b3d792ef5cda267f0b812a1d44765ad7290091080ca67e1" + ], + "error": null + }, + { + "sequence": 21, + "stage": "command_inspection", + "case": "blocked", + "verdict": "pass", + "outcome": "success", + "started_offset_ms": 20, + "duration_ms": 1, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + }, + "actual": { + "within_deadline": true + }, + "matched": true + }, + { + "id": "command.delivery_integrity", + "expected": { + "attempts": 1, + "queue_policy": "queue_until_safe", + "realization": "native", + "recorded_result": "delivered", + "retry_action": "observe_existing_command", + "retry_safe": false, + "state": "delivered", + "unchanged": true + }, + "actual": { + "attempts": 1, + "queue_policy": "queue_until_safe", + "realization": "native", + "recorded_result": "delivered", + "retry_action": "observe_existing_command", + "retry_safe": false, + "state": "delivered", + "unchanged": true + }, + "matched": true + } + ], + "evidence": [ + "blob:sha256:045edb8695b91fd54b3d792ef5cda267f0b812a1d44765ad7290091080ca67e1" + ], + "error": null + }, + { + "sequence": 22, + "stage": "launch", + "case": "exited", + "verdict": "pass", + "outcome": "success", + "started_offset_ms": 21, + "duration_ms": 1, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + }, + "actual": { + "within_deadline": true + }, + "matched": true + }, + { + "id": "launch.resolution", + "expected": { + "host": "herdr", + "prelaunch_prompt": false, + "runtime": "pi", + "selected_leaves": 1 + }, + "actual": { + "host": "herdr", + "prelaunch_prompt": false, + "runtime": "pi", + "selected_leaves": 1 + }, + "matched": true + } + ], + "evidence": [ + "blob:sha256:045edb8695b91fd54b3d792ef5cda267f0b812a1d44765ad7290091080ca67e1" + ], + "error": null + }, + { + "sequence": 23, + "stage": "bind", + "case": "exited", + "verdict": "pass", + "outcome": "success", + "started_offset_ms": 22, + "duration_ms": 1, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + }, + "actual": { + "within_deadline": true + }, + "matched": true + }, + { + "id": "bind.runtime_identity", + "expected": { + "attachment_claimed": true, + "correlations": 1, + "instances": 1 + }, + "actual": { + "attachment_claimed": true, + "correlations": 1, + "instances": 1 + }, + "matched": true + } + ], + "evidence": [ + "blob:sha256:045edb8695b91fd54b3d792ef5cda267f0b812a1d44765ad7290091080ca67e1" + ], + "error": null + }, + { + "sequence": 24, + "stage": "send", + "case": "exited", + "verdict": "pass", + "outcome": "success", + "started_offset_ms": 23, + "duration_ms": 1, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + }, + "actual": { + "within_deadline": true + }, + "matched": true + }, + { + "id": "send.delivery", + "expected": { + "acknowledged": false, + "activity_observed": false, + "attempts": 1, + "queue_policy": "queue_until_safe", + "realization": "native", + "recorded_result": "delivered", + "retry_action": "observe_existing_command", + "retry_safe": false, + "state": "delivered" + }, + "actual": { + "acknowledged": false, + "activity_observed": false, + "attempts": 1, + "queue_policy": "queue_until_safe", + "realization": "native", + "recorded_result": "delivered", + "retry_action": "observe_existing_command", + "retry_safe": false, + "state": "delivered" + }, + "matched": true + } + ], + "evidence": [ + "blob:sha256:045edb8695b91fd54b3d792ef5cda267f0b812a1d44765ad7290091080ca67e1" + ], + "error": null + }, + { + "sequence": 25, + "stage": "observe", + "case": "exited", + "verdict": "pass", + "outcome": "exited", + "started_offset_ms": 24, + "duration_ms": 1, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + }, + "actual": { + "within_deadline": true + }, + "matched": true + }, + { + "id": "observe.exited", + "expected": { + "attempts": 1, + "condition": "exited", + "process_success": true, + "same_instance": true + }, + "actual": { + "attempts": 1, + "condition": "exited", + "process_success": true, + "same_instance": true + }, + "matched": true + } + ], + "evidence": [ + "blob:sha256:045edb8695b91fd54b3d792ef5cda267f0b812a1d44765ad7290091080ca67e1" + ], + "error": null + }, + { + "sequence": 26, + "stage": "command_inspection", + "case": "exited", + "verdict": "pass", + "outcome": "success", + "started_offset_ms": 25, + "duration_ms": 1, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + }, + "actual": { + "within_deadline": true + }, + "matched": true + }, + { + "id": "command.delivery_integrity", + "expected": { + "attempts": 1, + "queue_policy": "queue_until_safe", + "realization": "native", + "recorded_result": "delivered", + "retry_action": "observe_existing_command", + "retry_safe": false, + "state": "delivered", + "unchanged": true + }, + "actual": { + "attempts": 1, + "queue_policy": "queue_until_safe", + "realization": "native", + "recorded_result": "delivered", + "retry_action": "observe_existing_command", + "retry_safe": false, + "state": "delivered", + "unchanged": true + }, + "matched": true + } + ], + "evidence": [ + "blob:sha256:045edb8695b91fd54b3d792ef5cda267f0b812a1d44765ad7290091080ca67e1" + ], + "error": null + }, + { + "sequence": 27, + "stage": "launch", + "case": "timeout", + "verdict": "pass", + "outcome": "success", + "started_offset_ms": 26, + "duration_ms": 1, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + }, + "actual": { + "within_deadline": true + }, + "matched": true + }, + { + "id": "launch.resolution", + "expected": { + "host": "herdr", + "prelaunch_prompt": false, + "runtime": "pi", + "selected_leaves": 1 + }, + "actual": { + "host": "herdr", + "prelaunch_prompt": false, + "runtime": "pi", + "selected_leaves": 1 + }, + "matched": true + } + ], + "evidence": [ + "blob:sha256:045edb8695b91fd54b3d792ef5cda267f0b812a1d44765ad7290091080ca67e1" + ], + "error": null + }, + { + "sequence": 28, + "stage": "bind", + "case": "timeout", + "verdict": "pass", + "outcome": "success", + "started_offset_ms": 27, + "duration_ms": 1, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + }, + "actual": { + "within_deadline": true + }, + "matched": true + }, + { + "id": "bind.runtime_identity", + "expected": { + "attachment_claimed": true, + "correlations": 1, + "instances": 1 + }, + "actual": { + "attachment_claimed": true, + "correlations": 1, + "instances": 1 + }, + "matched": true + } + ], + "evidence": [ + "blob:sha256:045edb8695b91fd54b3d792ef5cda267f0b812a1d44765ad7290091080ca67e1" + ], + "error": null + }, + { + "sequence": 29, + "stage": "send", + "case": "timeout", + "verdict": "pass", + "outcome": "success", + "started_offset_ms": 28, + "duration_ms": 1, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + }, + "actual": { + "within_deadline": true + }, + "matched": true + }, + { + "id": "send.delivery", + "expected": { + "acknowledged": false, + "activity_observed": false, + "attempts": 1, + "queue_policy": "queue_until_safe", + "realization": "native", + "recorded_result": "delivered", + "retry_action": "observe_existing_command", + "retry_safe": false, + "state": "delivered" + }, + "actual": { + "acknowledged": false, + "activity_observed": false, + "attempts": 1, + "queue_policy": "queue_until_safe", + "realization": "native", + "recorded_result": "delivered", + "retry_action": "observe_existing_command", + "retry_safe": false, + "state": "delivered" + }, + "matched": true + } + ], + "evidence": [ + "blob:sha256:045edb8695b91fd54b3d792ef5cda267f0b812a1d44765ad7290091080ca67e1" + ], + "error": null + }, + { + "sequence": 30, + "stage": "observe", + "case": "timeout", + "verdict": "pass", + "outcome": "timeout", + "started_offset_ms": 29, + "duration_ms": 20000, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + }, + "actual": { + "within_deadline": true + }, + "matched": true + }, + { + "id": "observe.timeout", + "expected": { + "assistant_blocks": 0, + "attempts": 1, + "delivered": true, + "elapsed_max_ms": 22500, + "elapsed_min_ms": 20000, + "elapsed_ms": 20000, + "outer_timed_out": false, + "process_success": true, + "stopped_verified": true + }, + "actual": { + "assistant_blocks": 0, + "attempts": 1, + "delivered": true, + "elapsed_max_ms": 22500, + "elapsed_min_ms": 20000, + "elapsed_ms": 20000, + "outer_timed_out": false, + "process_success": true, + "stopped_verified": true + }, + "matched": true + } + ], + "evidence": [ + "blob:sha256:045edb8695b91fd54b3d792ef5cda267f0b812a1d44765ad7290091080ca67e1" + ], + "error": null + }, + { + "sequence": 31, + "stage": "command_inspection", + "case": "timeout", + "verdict": "pass", + "outcome": "success", + "started_offset_ms": 20029, + "duration_ms": 1, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + }, + "actual": { + "within_deadline": true + }, + "matched": true + }, + { + "id": "command.delivery_integrity", + "expected": { + "attempts": 1, + "queue_policy": "queue_until_safe", + "realization": "native", + "recorded_result": "delivered", + "retry_action": "observe_existing_command", + "retry_safe": false, + "state": "delivered", + "unchanged": true + }, + "actual": { + "attempts": 1, + "queue_policy": "queue_until_safe", + "realization": "native", + "recorded_result": "delivered", + "retry_action": "observe_existing_command", + "retry_safe": false, + "state": "delivered", + "unchanged": true + }, + "matched": true + } + ], + "evidence": [ + "blob:sha256:045edb8695b91fd54b3d792ef5cda267f0b812a1d44765ad7290091080ca67e1" + ], + "error": null + }, + { + "sequence": 32, + "stage": "cleanup", + "case": "run", + "verdict": "pass", + "outcome": "success", + "started_offset_ms": 20030, + "duration_ms": 1, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + }, + "actual": { + "within_deadline": true + }, + "matched": true + }, + { + "id": "cleanup.complete", + "expected": { + "export_complete": true, + "panes_remaining": 0, + "pids_remaining": 0, + "root_removed": true, + "sockets_remaining": 0 + }, + "actual": { + "export_complete": true, + "panes_remaining": 0, + "pids_remaining": 0, + "root_removed": true, + "sockets_remaining": 0 + }, + "matched": true + } + ], + "evidence": [ + "blob:sha256:045edb8695b91fd54b3d792ef5cda267f0b812a1d44765ad7290091080ca67e1" + ], + "error": null + } + ], + "summary": { + "verdict": "pass", + "first_failed_stage": null, + "first_failed_case": null + }, + "scrub": { + "status": "pass", + "policy": "portable-launcher-evidence/v1", + "findings": [] + } +} diff --git a/contracts/fixtures/duo-portable-launcher-conformance-v1/result-structural-blocked.json b/contracts/fixtures/duo-portable-launcher-conformance-v1/result-structural-blocked.json new file mode 100644 index 0000000..5e79e6a --- /dev/null +++ b/contracts/fixtures/duo-portable-launcher-conformance-v1/result-structural-blocked.json @@ -0,0 +1,1417 @@ +{ + "schema": "duo.portable-launcher-conformance-result/v1", + "suite": { + "name": "portable-launcher-delegation", + "revision": 1, + "manifest_digest": "sha256:01e8e6bd80ceb441be6dc128aca023a1bede31b655e74b12ee3da0c2fc6cc204", + "oracle_digest": "sha256:a305744fc34af96b5bbd916b9de3aa1bc94f59ce4ea9316b62bb0a8dd5dd22c5" + }, + "run": { + "run_id": "fixture-structural-blocked", + "observed_at": "2026-09-17T00:00:00Z", + "host_os": "linux", + "host_arch": "x86_64", + "fixture_root": "$RUN", + "plugins_enabled": false, + "mcp_enabled": false, + "terminal_input_used": false + }, + "pins": { + "launcher": { + "name": "amp", + "version": "0.0.1789675234-g2899fe", + "executable_sha256": "sha256:f351217dac739614b4d9b3eacad728ce6d61647ab1f5d4a4de44d99aba6598ee" + }, + "duo": { + "version": "fixture", + "commit": "0123456789abcdef0123456789abcdef01234567", + "build_date": "2026-09-17T00:00:00Z", + "executable_sha256": "sha256:2af74f65db28670aa9a5439c4eb48137c63dea3db07f31b3143f672dffddaf11" + }, + "skill": { + "name": "duo-delegation-loop", + "format_version": "duo.skill/duo-delegation-loop/v1", + "content_digest": "sha256:6f8bc16b656f564a949c99f12d4cf0bcd04ec2c9b49a8b74fb65b2e355b5d182", + "installation_id": "fixture-installation" + }, + "config": { + "schema": "duo.config/v3", + "effective_digest": "sha256:dc3fbd7d29934bfcb08cf280ac04b77e6bde9a5da091e3a892062f757a71ced0" + }, + "authority": { + "schema_version": 1, + "store_digest_before": "sha256:b58d01a3400fbcf9af5b8f043c5e4bf2411631be0a51c7d8b7e466875289ab02", + "store_digest_after": "sha256:49a2b38653a56a91b6664cb633dd9918da8d6a47d0f79e75814bb8381c16e36b" + }, + "host": { + "name": "herdr", + "version": "0.8.2", + "protocol": "herdr-socket-api/20", + "schema_digest": "sha256:c48f1f54ee0150ca27e11fd44455fe94aeadb20fdf4e4a62393ed822a4e5b150", + "executable_sha256": "sha256:7db471d26dcc133939e1fc80074b7f1f90bf181eebaacf48bfbb2ad2962964da" + }, + "runtime": { + "name": "pi", + "version": "0.83.0", + "format": "pi-session-jsonl/v3", + "adapter_build": "stage1", + "conformance_record": "pi-0.83.0-2026-08-23", + "executable_sha256": "sha256:9b5e9b1aa2486835b24f7842b027d187d940986b020f584736af7a4927f47d4c", + "delivery_asset_sha256": "sha256:2708a3435821fb2e72302ee56d8a07fe62c265214ae8fd342e385c03ef245a23" + }, + "model": { + "provider": "openai-codex", + "model_line": "gpt-5.6-luna" + }, + "external_schema": { + "identity": "duo.external/v1", + "digest": "sha256:8b055ff60f0ca5f3da46302671591aa0dae61185cd9f1f15fcceb36d841e7497" + } + }, + "stages": [ + { + "sequence": 1, + "stage": "setup", + "case": "run", + "verdict": "pass", + "outcome": "success", + "started_offset_ms": 0, + "duration_ms": 1, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + }, + "actual": { + "within_deadline": true + }, + "matched": true + }, + { + "id": "setup.fixture_isolated_and_pinned", + "expected": { + "fixture_root": "$RUN", + "mcp_enabled": false, + "plugins_enabled": false, + "shared_state": false, + "terminal_input_used": false + }, + "actual": { + "fixture_root": "$RUN", + "mcp_enabled": false, + "plugins_enabled": false, + "shared_state": false, + "terminal_input_used": false + }, + "matched": true + } + ], + "evidence": [ + "blob:sha256:045edb8695b91fd54b3d792ef5cda267f0b812a1d44765ad7290091080ca67e1" + ], + "error": null + }, + { + "sequence": 2, + "stage": "discovery", + "case": "run", + "verdict": "pass", + "outcome": "success", + "started_offset_ms": 1, + "duration_ms": 1, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + }, + "actual": { + "within_deadline": true + }, + "matched": true + }, + { + "id": "discovery.skill_identity", + "expected": { + "content_digest": "sha256:6f8bc16b656f564a949c99f12d4cf0bcd04ec2c9b49a8b74fb65b2e355b5d182", + "format_version": "duo.skill/duo-delegation-loop/v1", + "name": "duo-delegation-loop" + }, + "actual": { + "content_digest": "sha256:6f8bc16b656f564a949c99f12d4cf0bcd04ec2c9b49a8b74fb65b2e355b5d182", + "format_version": "duo.skill/duo-delegation-loop/v1", + "name": "duo-delegation-loop" + }, + "matched": true + } + ], + "evidence": [ + "blob:sha256:045edb8695b91fd54b3d792ef5cda267f0b812a1d44765ad7290091080ca67e1" + ], + "error": null + }, + { + "sequence": 3, + "stage": "preflight", + "case": "run", + "verdict": "pass", + "outcome": "success", + "started_offset_ms": 2, + "duration_ms": 1, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + }, + "actual": { + "within_deadline": true + }, + "matched": true + }, + { + "id": "preflight.ready", + "expected": { + "all_required_pass": true, + "required_checks": 7, + "status": "ready" + }, + "actual": { + "all_required_pass": true, + "required_checks": 7, + "status": "ready" + }, + "matched": true + } + ], + "evidence": [ + "blob:sha256:045edb8695b91fd54b3d792ef5cda267f0b812a1d44765ad7290091080ca67e1" + ], + "error": null + }, + { + "sequence": 4, + "stage": "launch", + "case": "happy", + "verdict": "pass", + "outcome": "success", + "started_offset_ms": 3, + "duration_ms": 1, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + }, + "actual": { + "within_deadline": true + }, + "matched": true + }, + { + "id": "launch.resolution", + "expected": { + "host": "herdr", + "prelaunch_prompt": false, + "runtime": "pi", + "selected_leaves": 1 + }, + "actual": { + "host": "herdr", + "prelaunch_prompt": false, + "runtime": "pi", + "selected_leaves": 1 + }, + "matched": true + } + ], + "evidence": [ + "blob:sha256:045edb8695b91fd54b3d792ef5cda267f0b812a1d44765ad7290091080ca67e1" + ], + "error": null + }, + { + "sequence": 5, + "stage": "bind", + "case": "happy", + "verdict": "pass", + "outcome": "success", + "started_offset_ms": 4, + "duration_ms": 1, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + }, + "actual": { + "within_deadline": true + }, + "matched": true + }, + { + "id": "bind.runtime_identity", + "expected": { + "attachment_claimed": true, + "correlations": 1, + "instances": 1 + }, + "actual": { + "attachment_claimed": true, + "correlations": 1, + "instances": 1 + }, + "matched": true + } + ], + "evidence": [ + "blob:sha256:045edb8695b91fd54b3d792ef5cda267f0b812a1d44765ad7290091080ca67e1" + ], + "error": null + }, + { + "sequence": 6, + "stage": "send", + "case": "happy", + "verdict": "pass", + "outcome": "success", + "started_offset_ms": 5, + "duration_ms": 1, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + }, + "actual": { + "within_deadline": true + }, + "matched": true + }, + { + "id": "send.delivery", + "expected": { + "acknowledged": false, + "activity_observed": false, + "attempts": 1, + "queue_policy": "queue_until_safe", + "realization": "native", + "recorded_result": "delivered", + "retry_action": "observe_existing_command", + "retry_safe": false, + "state": "delivered" + }, + "actual": { + "acknowledged": false, + "activity_observed": false, + "attempts": 1, + "queue_policy": "queue_until_safe", + "realization": "native", + "recorded_result": "delivered", + "retry_action": "observe_existing_command", + "retry_safe": false, + "state": "delivered" + }, + "matched": true + } + ], + "evidence": [ + "blob:sha256:045edb8695b91fd54b3d792ef5cda267f0b812a1d44765ad7290091080ca67e1" + ], + "error": null + }, + { + "sequence": 7, + "stage": "observe", + "case": "happy", + "verdict": "pass", + "outcome": "success", + "started_offset_ms": 6, + "duration_ms": 1, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + }, + "actual": { + "within_deadline": true + }, + "matched": true + }, + { + "id": "observe.assistant_text", + "expected": { + "assistant_blocks": 1, + "source_complete": true, + "text_digest": "sha256:b3300f2f6e0338d2b672891be66827d95779a2eabf4a75dcae1b6f56c9a4dc98" + }, + "actual": { + "assistant_blocks": 1, + "source_complete": true, + "text_digest": "sha256:b3300f2f6e0338d2b672891be66827d95779a2eabf4a75dcae1b6f56c9a4dc98" + }, + "matched": true + } + ], + "evidence": [ + "blob:sha256:045edb8695b91fd54b3d792ef5cda267f0b812a1d44765ad7290091080ca67e1" + ], + "error": null + }, + { + "sequence": 8, + "stage": "command_inspection", + "case": "happy", + "verdict": "pass", + "outcome": "success", + "started_offset_ms": 7, + "duration_ms": 1, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + }, + "actual": { + "within_deadline": true + }, + "matched": true + }, + { + "id": "command.delivery_integrity", + "expected": { + "attempts": 1, + "queue_policy": "queue_until_safe", + "realization": "native", + "recorded_result": "delivered", + "retry_action": "observe_existing_command", + "retry_safe": false, + "state": "delivered", + "unchanged": true + }, + "actual": { + "attempts": 1, + "queue_policy": "queue_until_safe", + "realization": "native", + "recorded_result": "delivered", + "retry_action": "observe_existing_command", + "retry_safe": false, + "state": "delivered", + "unchanged": true + }, + "matched": true + } + ], + "evidence": [ + "blob:sha256:045edb8695b91fd54b3d792ef5cda267f0b812a1d44765ad7290091080ca67e1" + ], + "error": null + }, + { + "sequence": 9, + "stage": "restart", + "case": "restart", + "verdict": "pass", + "outcome": "success", + "started_offset_ms": 8, + "duration_ms": 1, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + }, + "actual": { + "within_deadline": true + }, + "matched": true + }, + { + "id": "restart.authority_continuity", + "expected": { + "attempts": 1, + "distinct_processes": true, + "outcome": "same_live", + "same_store": true + }, + "actual": { + "attempts": 1, + "distinct_processes": true, + "outcome": "same_live", + "same_store": true + }, + "matched": true + } + ], + "evidence": [ + "blob:sha256:045edb8695b91fd54b3d792ef5cda267f0b812a1d44765ad7290091080ca67e1" + ], + "error": null + }, + { + "sequence": 10, + "stage": "bind", + "case": "restart", + "verdict": "pass", + "outcome": "success", + "started_offset_ms": 9, + "duration_ms": 1, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + }, + "actual": { + "within_deadline": true + }, + "matched": true + }, + { + "id": "bind.runtime_identity", + "expected": { + "attachment_claimed": true, + "correlations": 1, + "instances": 1 + }, + "actual": { + "attachment_claimed": true, + "correlations": 1, + "instances": 1 + }, + "matched": true + } + ], + "evidence": [ + "blob:sha256:045edb8695b91fd54b3d792ef5cda267f0b812a1d44765ad7290091080ca67e1" + ], + "error": null + }, + { + "sequence": 11, + "stage": "observe", + "case": "restart", + "verdict": "pass", + "outcome": "success", + "started_offset_ms": 10, + "duration_ms": 1, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + }, + "actual": { + "within_deadline": true + }, + "matched": true + }, + { + "id": "observe.restart_text", + "expected": { + "same_record": true, + "text_digest": "sha256:b3300f2f6e0338d2b672891be66827d95779a2eabf4a75dcae1b6f56c9a4dc98" + }, + "actual": { + "same_record": true, + "text_digest": "sha256:b3300f2f6e0338d2b672891be66827d95779a2eabf4a75dcae1b6f56c9a4dc98" + }, + "matched": true + } + ], + "evidence": [ + "blob:sha256:045edb8695b91fd54b3d792ef5cda267f0b812a1d44765ad7290091080ca67e1" + ], + "error": null + }, + { + "sequence": 12, + "stage": "command_inspection", + "case": "restart", + "verdict": "pass", + "outcome": "success", + "started_offset_ms": 11, + "duration_ms": 1, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + }, + "actual": { + "within_deadline": true + }, + "matched": true + }, + { + "id": "command.delivery_integrity", + "expected": { + "attempts": 1, + "queue_policy": "queue_until_safe", + "realization": "native", + "recorded_result": "delivered", + "retry_action": "observe_existing_command", + "retry_safe": false, + "state": "delivered", + "unchanged": true + }, + "actual": { + "attempts": 1, + "queue_policy": "queue_until_safe", + "realization": "native", + "recorded_result": "delivered", + "retry_action": "observe_existing_command", + "retry_safe": false, + "state": "delivered", + "unchanged": true + }, + "matched": true + } + ], + "evidence": [ + "blob:sha256:045edb8695b91fd54b3d792ef5cda267f0b812a1d44765ad7290091080ca67e1" + ], + "error": null + }, + { + "sequence": 13, + "stage": "send", + "case": "same_key_same_text", + "verdict": "pass", + "outcome": "success", + "started_offset_ms": 12, + "duration_ms": 1, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + }, + "actual": { + "within_deadline": true + }, + "matched": true + }, + { + "id": "send.idempotent_replay", + "expected": { + "attempts": 1, + "new_turns": 0, + "same_command": true + }, + "actual": { + "attempts": 1, + "new_turns": 0, + "same_command": true + }, + "matched": true + } + ], + "evidence": [ + "blob:sha256:045edb8695b91fd54b3d792ef5cda267f0b812a1d44765ad7290091080ca67e1" + ], + "error": null + }, + { + "sequence": 14, + "stage": "command_inspection", + "case": "same_key_same_text", + "verdict": "pass", + "outcome": "success", + "started_offset_ms": 13, + "duration_ms": 1, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + }, + "actual": { + "within_deadline": true + }, + "matched": true + }, + { + "id": "command.idempotent_no_duplication", + "expected": { + "attempts_delta": 0, + "commands_delta": 0, + "turns_delta": 0 + }, + "actual": { + "attempts_delta": 0, + "commands_delta": 0, + "turns_delta": 0 + }, + "matched": true + } + ], + "evidence": [ + "blob:sha256:045edb8695b91fd54b3d792ef5cda267f0b812a1d44765ad7290091080ca67e1" + ], + "error": null + }, + { + "sequence": 15, + "stage": "send", + "case": "same_key_different_text", + "verdict": "pass", + "outcome": "success", + "started_offset_ms": 14, + "duration_ms": 1, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + }, + "actual": { + "within_deadline": true + }, + "matched": true + }, + { + "id": "send.idempotency_conflict", + "expected": { + "code": "command.idempotency_conflict", + "effect": "no_effect", + "existing_digest": "sha256:b40d3a68eb33083b92199d734da2dece28395a6acc6d5e2cb9d7032dae4d11c7", + "idempotency_key": "portable-launcher-v1-primary", + "request_digest": "sha256:dbe904a42a9d9d9cdf7908508fa393ce43c9c53ea16a8cb8c9b82e7005f602bc", + "retry_action": "use_new_idempotency_key", + "retry_safe": false, + "target_original": true + }, + "actual": { + "code": "command.idempotency_conflict", + "effect": "no_effect", + "existing_digest": "sha256:b40d3a68eb33083b92199d734da2dece28395a6acc6d5e2cb9d7032dae4d11c7", + "idempotency_key": "portable-launcher-v1-primary", + "request_digest": "sha256:dbe904a42a9d9d9cdf7908508fa393ce43c9c53ea16a8cb8c9b82e7005f602bc", + "retry_action": "use_new_idempotency_key", + "retry_safe": false, + "target_original": true + }, + "matched": true + } + ], + "evidence": [ + "blob:sha256:045edb8695b91fd54b3d792ef5cda267f0b812a1d44765ad7290091080ca67e1" + ], + "error": null + }, + { + "sequence": 16, + "stage": "command_inspection", + "case": "same_key_different_text", + "verdict": "pass", + "outcome": "success", + "started_offset_ms": 15, + "duration_ms": 1, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + }, + "actual": { + "within_deadline": true + }, + "matched": true + }, + { + "id": "command.idempotency_no_drift", + "expected": { + "attempts_delta": 0, + "commands_delta": 0, + "queue_delta": 0, + "turns_delta": 0 + }, + "actual": { + "attempts_delta": 0, + "commands_delta": 0, + "queue_delta": 0, + "turns_delta": 0 + }, + "matched": true + } + ], + "evidence": [ + "blob:sha256:045edb8695b91fd54b3d792ef5cda267f0b812a1d44765ad7290091080ca67e1" + ], + "error": null + }, + { + "sequence": 17, + "stage": "launch", + "case": "blocked", + "verdict": "fail", + "outcome": "error", + "started_offset_ms": 16, + "duration_ms": 1, + "assertions": [ + { + "id": "blocked.induction_available", + "expected": { + "available": true + }, + "actual": { + "available": false + }, + "matched": false + } + ], + "evidence": [ + "blob:sha256:045edb8695b91fd54b3d792ef5cda267f0b812a1d44765ad7290091080ca67e1" + ], + "error": { + "code": "prerequisite.blocked_induction_unavailable", + "message": "supported admitted-then-blocked evidence is unavailable", + "effect": "no_effect", + "retry": { + "safe": false, + "action": "add_supported_blocked_evidence" + } + } + }, + { + "sequence": 18, + "stage": "bind", + "case": "blocked", + "verdict": "fail", + "outcome": "error", + "started_offset_ms": 17, + "duration_ms": 1, + "assertions": [ + { + "id": "prerequisite.not_reached", + "expected": { + "reached": true + }, + "actual": { + "reached": false + }, + "matched": false + } + ], + "evidence": [ + "blob:sha256:045edb8695b91fd54b3d792ef5cda267f0b812a1d44765ad7290091080ca67e1" + ], + "error": { + "code": "prerequisite.not_reached", + "message": "blocked induction prerequisite failed", + "effect": "no_effect", + "retry": { + "safe": false, + "action": "inspect_originating_failure" + } + } + }, + { + "sequence": 19, + "stage": "send", + "case": "blocked", + "verdict": "fail", + "outcome": "error", + "started_offset_ms": 18, + "duration_ms": 1, + "assertions": [ + { + "id": "prerequisite.not_reached", + "expected": { + "reached": true + }, + "actual": { + "reached": false + }, + "matched": false + } + ], + "evidence": [ + "blob:sha256:045edb8695b91fd54b3d792ef5cda267f0b812a1d44765ad7290091080ca67e1" + ], + "error": { + "code": "prerequisite.not_reached", + "message": "blocked induction prerequisite failed", + "effect": "no_effect", + "retry": { + "safe": false, + "action": "inspect_originating_failure" + } + } + }, + { + "sequence": 20, + "stage": "observe", + "case": "blocked", + "verdict": "fail", + "outcome": "error", + "started_offset_ms": 19, + "duration_ms": 1, + "assertions": [ + { + "id": "prerequisite.not_reached", + "expected": { + "reached": true + }, + "actual": { + "reached": false + }, + "matched": false + } + ], + "evidence": [ + "blob:sha256:045edb8695b91fd54b3d792ef5cda267f0b812a1d44765ad7290091080ca67e1" + ], + "error": { + "code": "prerequisite.not_reached", + "message": "blocked induction prerequisite failed", + "effect": "no_effect", + "retry": { + "safe": false, + "action": "inspect_originating_failure" + } + } + }, + { + "sequence": 21, + "stage": "command_inspection", + "case": "blocked", + "verdict": "fail", + "outcome": "error", + "started_offset_ms": 20, + "duration_ms": 1, + "assertions": [ + { + "id": "prerequisite.not_reached", + "expected": { + "reached": true + }, + "actual": { + "reached": false + }, + "matched": false + } + ], + "evidence": [ + "blob:sha256:045edb8695b91fd54b3d792ef5cda267f0b812a1d44765ad7290091080ca67e1" + ], + "error": { + "code": "prerequisite.not_reached", + "message": "blocked induction prerequisite failed", + "effect": "no_effect", + "retry": { + "safe": false, + "action": "inspect_originating_failure" + } + } + }, + { + "sequence": 22, + "stage": "launch", + "case": "exited", + "verdict": "pass", + "outcome": "success", + "started_offset_ms": 21, + "duration_ms": 1, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + }, + "actual": { + "within_deadline": true + }, + "matched": true + }, + { + "id": "launch.resolution", + "expected": { + "host": "herdr", + "prelaunch_prompt": false, + "runtime": "pi", + "selected_leaves": 1 + }, + "actual": { + "host": "herdr", + "prelaunch_prompt": false, + "runtime": "pi", + "selected_leaves": 1 + }, + "matched": true + } + ], + "evidence": [ + "blob:sha256:045edb8695b91fd54b3d792ef5cda267f0b812a1d44765ad7290091080ca67e1" + ], + "error": null + }, + { + "sequence": 23, + "stage": "bind", + "case": "exited", + "verdict": "pass", + "outcome": "success", + "started_offset_ms": 22, + "duration_ms": 1, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + }, + "actual": { + "within_deadline": true + }, + "matched": true + }, + { + "id": "bind.runtime_identity", + "expected": { + "attachment_claimed": true, + "correlations": 1, + "instances": 1 + }, + "actual": { + "attachment_claimed": true, + "correlations": 1, + "instances": 1 + }, + "matched": true + } + ], + "evidence": [ + "blob:sha256:045edb8695b91fd54b3d792ef5cda267f0b812a1d44765ad7290091080ca67e1" + ], + "error": null + }, + { + "sequence": 24, + "stage": "send", + "case": "exited", + "verdict": "pass", + "outcome": "success", + "started_offset_ms": 23, + "duration_ms": 1, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + }, + "actual": { + "within_deadline": true + }, + "matched": true + }, + { + "id": "send.delivery", + "expected": { + "acknowledged": false, + "activity_observed": false, + "attempts": 1, + "queue_policy": "queue_until_safe", + "realization": "native", + "recorded_result": "delivered", + "retry_action": "observe_existing_command", + "retry_safe": false, + "state": "delivered" + }, + "actual": { + "acknowledged": false, + "activity_observed": false, + "attempts": 1, + "queue_policy": "queue_until_safe", + "realization": "native", + "recorded_result": "delivered", + "retry_action": "observe_existing_command", + "retry_safe": false, + "state": "delivered" + }, + "matched": true + } + ], + "evidence": [ + "blob:sha256:045edb8695b91fd54b3d792ef5cda267f0b812a1d44765ad7290091080ca67e1" + ], + "error": null + }, + { + "sequence": 25, + "stage": "observe", + "case": "exited", + "verdict": "pass", + "outcome": "exited", + "started_offset_ms": 24, + "duration_ms": 1, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + }, + "actual": { + "within_deadline": true + }, + "matched": true + }, + { + "id": "observe.exited", + "expected": { + "attempts": 1, + "condition": "exited", + "process_success": true, + "same_instance": true + }, + "actual": { + "attempts": 1, + "condition": "exited", + "process_success": true, + "same_instance": true + }, + "matched": true + } + ], + "evidence": [ + "blob:sha256:045edb8695b91fd54b3d792ef5cda267f0b812a1d44765ad7290091080ca67e1" + ], + "error": null + }, + { + "sequence": 26, + "stage": "command_inspection", + "case": "exited", + "verdict": "pass", + "outcome": "success", + "started_offset_ms": 25, + "duration_ms": 1, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + }, + "actual": { + "within_deadline": true + }, + "matched": true + }, + { + "id": "command.delivery_integrity", + "expected": { + "attempts": 1, + "queue_policy": "queue_until_safe", + "realization": "native", + "recorded_result": "delivered", + "retry_action": "observe_existing_command", + "retry_safe": false, + "state": "delivered", + "unchanged": true + }, + "actual": { + "attempts": 1, + "queue_policy": "queue_until_safe", + "realization": "native", + "recorded_result": "delivered", + "retry_action": "observe_existing_command", + "retry_safe": false, + "state": "delivered", + "unchanged": true + }, + "matched": true + } + ], + "evidence": [ + "blob:sha256:045edb8695b91fd54b3d792ef5cda267f0b812a1d44765ad7290091080ca67e1" + ], + "error": null + }, + { + "sequence": 27, + "stage": "launch", + "case": "timeout", + "verdict": "pass", + "outcome": "success", + "started_offset_ms": 26, + "duration_ms": 1, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + }, + "actual": { + "within_deadline": true + }, + "matched": true + }, + { + "id": "launch.resolution", + "expected": { + "host": "herdr", + "prelaunch_prompt": false, + "runtime": "pi", + "selected_leaves": 1 + }, + "actual": { + "host": "herdr", + "prelaunch_prompt": false, + "runtime": "pi", + "selected_leaves": 1 + }, + "matched": true + } + ], + "evidence": [ + "blob:sha256:045edb8695b91fd54b3d792ef5cda267f0b812a1d44765ad7290091080ca67e1" + ], + "error": null + }, + { + "sequence": 28, + "stage": "bind", + "case": "timeout", + "verdict": "pass", + "outcome": "success", + "started_offset_ms": 27, + "duration_ms": 1, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + }, + "actual": { + "within_deadline": true + }, + "matched": true + }, + { + "id": "bind.runtime_identity", + "expected": { + "attachment_claimed": true, + "correlations": 1, + "instances": 1 + }, + "actual": { + "attachment_claimed": true, + "correlations": 1, + "instances": 1 + }, + "matched": true + } + ], + "evidence": [ + "blob:sha256:045edb8695b91fd54b3d792ef5cda267f0b812a1d44765ad7290091080ca67e1" + ], + "error": null + }, + { + "sequence": 29, + "stage": "send", + "case": "timeout", + "verdict": "pass", + "outcome": "success", + "started_offset_ms": 28, + "duration_ms": 1, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + }, + "actual": { + "within_deadline": true + }, + "matched": true + }, + { + "id": "send.delivery", + "expected": { + "acknowledged": false, + "activity_observed": false, + "attempts": 1, + "queue_policy": "queue_until_safe", + "realization": "native", + "recorded_result": "delivered", + "retry_action": "observe_existing_command", + "retry_safe": false, + "state": "delivered" + }, + "actual": { + "acknowledged": false, + "activity_observed": false, + "attempts": 1, + "queue_policy": "queue_until_safe", + "realization": "native", + "recorded_result": "delivered", + "retry_action": "observe_existing_command", + "retry_safe": false, + "state": "delivered" + }, + "matched": true + } + ], + "evidence": [ + "blob:sha256:045edb8695b91fd54b3d792ef5cda267f0b812a1d44765ad7290091080ca67e1" + ], + "error": null + }, + { + "sequence": 30, + "stage": "observe", + "case": "timeout", + "verdict": "pass", + "outcome": "timeout", + "started_offset_ms": 29, + "duration_ms": 20000, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + }, + "actual": { + "within_deadline": true + }, + "matched": true + }, + { + "id": "observe.timeout", + "expected": { + "assistant_blocks": 0, + "attempts": 1, + "delivered": true, + "elapsed_max_ms": 22500, + "elapsed_min_ms": 20000, + "elapsed_ms": 20000, + "outer_timed_out": false, + "process_success": true, + "stopped_verified": true + }, + "actual": { + "assistant_blocks": 0, + "attempts": 1, + "delivered": true, + "elapsed_max_ms": 22500, + "elapsed_min_ms": 20000, + "elapsed_ms": 20000, + "outer_timed_out": false, + "process_success": true, + "stopped_verified": true + }, + "matched": true + } + ], + "evidence": [ + "blob:sha256:045edb8695b91fd54b3d792ef5cda267f0b812a1d44765ad7290091080ca67e1" + ], + "error": null + }, + { + "sequence": 31, + "stage": "command_inspection", + "case": "timeout", + "verdict": "pass", + "outcome": "success", + "started_offset_ms": 20029, + "duration_ms": 1, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + }, + "actual": { + "within_deadline": true + }, + "matched": true + }, + { + "id": "command.delivery_integrity", + "expected": { + "attempts": 1, + "queue_policy": "queue_until_safe", + "realization": "native", + "recorded_result": "delivered", + "retry_action": "observe_existing_command", + "retry_safe": false, + "state": "delivered", + "unchanged": true + }, + "actual": { + "attempts": 1, + "queue_policy": "queue_until_safe", + "realization": "native", + "recorded_result": "delivered", + "retry_action": "observe_existing_command", + "retry_safe": false, + "state": "delivered", + "unchanged": true + }, + "matched": true + } + ], + "evidence": [ + "blob:sha256:045edb8695b91fd54b3d792ef5cda267f0b812a1d44765ad7290091080ca67e1" + ], + "error": null + }, + { + "sequence": 32, + "stage": "cleanup", + "case": "run", + "verdict": "pass", + "outcome": "success", + "started_offset_ms": 20030, + "duration_ms": 1, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + }, + "actual": { + "within_deadline": true + }, + "matched": true + }, + { + "id": "cleanup.complete", + "expected": { + "export_complete": true, + "panes_remaining": 0, + "pids_remaining": 0, + "root_removed": true, + "sockets_remaining": 0 + }, + "actual": { + "export_complete": true, + "panes_remaining": 0, + "pids_remaining": 0, + "root_removed": true, + "sockets_remaining": 0 + }, + "matched": true + } + ], + "evidence": [ + "blob:sha256:045edb8695b91fd54b3d792ef5cda267f0b812a1d44765ad7290091080ca67e1" + ], + "error": null + } + ], + "summary": { + "verdict": "fail", + "first_failed_stage": "launch", + "first_failed_case": "blocked" + }, + "scrub": { + "status": "pass", + "policy": "portable-launcher-evidence/v1", + "findings": [] + } +} diff --git a/contracts/fixtures/duo-portable-launcher-conformance-v1/scenario.json b/contracts/fixtures/duo-portable-launcher-conformance-v1/scenario.json new file mode 100644 index 0000000..14f4ce1 --- /dev/null +++ b/contracts/fixtures/duo-portable-launcher-conformance-v1/scenario.json @@ -0,0 +1,1417 @@ +{ + "schema": "duo.portable-launcher-conformance-scenario/v1", + "name": "portable-launcher-delegation", + "revision": 1, + "task_digest": "sha256:5949809812a3ada98c95e77e9942af848e3080e4984df43a2d96fe5a24d5eab2", + "fixture": { + "workspace": "$WORKSPACE", + "config_path": "$RUN/xdg/config/duo/duo.config.yaml", + "host": "herdr:$RUN/herdr/herdr.sock", + "preset": "builder", + "runtime": "pi", + "provider": "openai-codex", + "model_line": "gpt-5.6-luna", + "request_path": "$WORKSPACE/fixture/request.txt", + "prompt": "Read fixture/request.txt and reply with exactly the value of expected_reply, with no surrounding text.", + "expected_reply": "DUO_PORTABLE_LAUNCHER_OK_V1", + "conflict_text": "This text must conflict and must never be delivered.", + "request_digest": "sha256:b0a32f59630d6398da8d8913744dd7cd50a313d86bf7e833309aad0d47cc9c3b", + "reply_digest": "sha256:b3300f2f6e0338d2b672891be66827d95779a2eabf4a75dcae1b6f56c9a4dc98", + "prompt_digest": "sha256:b40d3a68eb33083b92199d734da2dece28395a6acc6d5e2cb9d7032dae4d11c7", + "conflict_digest": "sha256:dbe904a42a9d9d9cdf7908508fa393ce43c9c53ea16a8cb8c9b82e7005f602bc", + "idempotency_key": "portable-launcher-v1-primary" + }, + "prerequisites": [ + { + "name": "pinned_launcher_binary", + "status": "required", + "pre_setup": true + }, + { + "name": "pinned_duo_build", + "status": "required", + "pre_setup": true + }, + { + "name": "pinned_pi_0_83_0", + "status": "required", + "pre_setup": true + }, + { + "name": "pinned_herdr_0_8_2_protocol_20", + "status": "required", + "pre_setup": true + }, + { + "name": "isolated_inner_provider_credentials", + "status": "required", + "pre_setup": true + }, + { + "name": "timeout_induction_socket_probe", + "status": "required", + "pre_setup": false + }, + { + "name": "supported_admitted_then_blocked_producer", + "status": "unavailable", + "pre_setup": false + } + ], + "launchers": { + "amp": { + "version": "0.0.1789675234-g2899fe", + "executable_sha256": "sha256:f351217dac739614b4d9b3eacad728ce6d61647ab1f5d4a4de44d99aba6598ee" + }, + "codex": { + "version": "0.154.0", + "executable_sha256": "sha256:3188814c35471432d4123203e0eb38e5bddc60226e3d7ddf0e59e649ea140022" + }, + "opencode": { + "version": "1.18.31", + "executable_sha256": "sha256:f9dab32248695e9ebd56b16a1921798fd85112cf5a69c7dfd0cabc1e17be4a11" + } + }, + "deadlines_ms": { + "bind": 12000, + "cleanup": 30000, + "command_inspection": 15000, + "complete_run": 600000, + "discovery": 30000, + "launch": 30000, + "observe": 20000, + "preflight": 15000, + "restart": 15000, + "send": 30000, + "setup": 30000 + }, + "steps": [ + { + "sequence": 1, + "stage": "setup", + "case": "run", + "expected_outcome": "success", + "deadline_ms": 30000, + "action": { + "executor": "common_controller", + "operation": "fixture.prepare", + "arguments": [], + "auxiliary_operations": [], + "minimum_invocations": 1, + "maximum_invocations": 1, + "instruction": "Use the prepared isolated fixture and its exact per-run pins; do not use shared user state." + }, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + } + }, + { + "id": "setup.fixture_isolated_and_pinned", + "expected": { + "fixture_root": "$RUN", + "mcp_enabled": false, + "plugins_enabled": false, + "shared_state": false, + "terminal_input_used": false + } + } + ] + }, + { + "sequence": 2, + "stage": "discovery", + "case": "run", + "expected_outcome": "success", + "deadline_ms": 30000, + "action": { + "executor": "launcher", + "operation": "skill.discover", + "arguments": [ + "duo-delegation-loop" + ], + "auxiliary_operations": [], + "minimum_invocations": 1, + "maximum_invocations": 1, + "instruction": "Load the installed project skill and emit the launcher discovery event for its exact identity before invoking Duo." + }, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + } + }, + { + "id": "discovery.skill_identity", + "expected": { + "content_digest": "sha256:6f8bc16b656f564a949c99f12d4cf0bcd04ec2c9b49a8b74fb65b2e355b5d182", + "format_version": "duo.skill/duo-delegation-loop/v1", + "name": "duo-delegation-loop" + } + } + ] + }, + { + "sequence": 3, + "stage": "preflight", + "case": "run", + "expected_outcome": "success", + "deadline_ms": 15000, + "action": { + "executor": "duo", + "operation": "doctor.run", + "arguments": [ + "doctor", + "--workspace", + "$WORKSPACE", + "--output", + "json" + ], + "auxiliary_operations": [], + "minimum_invocations": 1, + "maximum_invocations": 1, + "instruction": "Run the read-only launcher preflight once and continue only when its seven required checks report ready." + }, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + } + }, + { + "id": "preflight.ready", + "expected": { + "all_required_pass": true, + "required_checks": 7, + "status": "ready" + } + } + ] + }, + { + "sequence": 4, + "stage": "launch", + "case": "happy", + "expected_outcome": "success", + "deadline_ms": 30000, + "action": { + "executor": "duo", + "operation": "session.launch", + "arguments": [ + "session", + "launch", + "builder", + "--require", + "agent_runtime=pi", + "--workspace", + "$WORKSPACE", + "--host", + "herdr:$RUN/herdr/herdr.sock", + "--output", + "json" + ], + "auxiliary_operations": [], + "minimum_invocations": 1, + "maximum_invocations": 1, + "instruction": "Launch one fresh canonical Pi session without a pre-launch prompt and retain its session identifier." + }, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + } + }, + { + "id": "launch.resolution", + "expected": { + "host": "herdr", + "prelaunch_prompt": false, + "runtime": "pi", + "selected_leaves": 1 + } + } + ] + }, + { + "sequence": 5, + "stage": "bind", + "case": "happy", + "expected_outcome": "success", + "deadline_ms": 12000, + "action": { + "executor": "duo", + "operation": "session.inspect", + "arguments": [ + "session", + "show", + "$CASE_SESSION_ID", + "--output", + "json" + ], + "auxiliary_operations": [], + "minimum_invocations": 1, + "maximum_invocations": 0, + "instruction": "Inspect the just-launched session until the one claimed Pi attachment and durable runtime identity are present; each poll is a separate command." + }, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + } + }, + { + "id": "bind.runtime_identity", + "expected": { + "attachment_claimed": true, + "correlations": 1, + "instances": 1 + } + } + ] + }, + { + "sequence": 6, + "stage": "send", + "case": "happy", + "expected_outcome": "success", + "deadline_ms": 30000, + "action": { + "executor": "duo", + "operation": "prompt.deliver", + "arguments": [ + "prompt", + "send", + "$CASE_SESSION_ID", + "--text", + "$CANONICAL_PROMPT", + "--idempotency-key", + "$PRIMARY_KEY", + "--expires-at", + "$EXPIRES_AT", + "--output", + "json" + ], + "auxiliary_operations": [], + "minimum_invocations": 1, + "maximum_invocations": 1, + "instruction": "Deliver the canonical prompt exactly once to this case's bound session with the primary idempotency key and finite expiry." + }, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + } + }, + { + "id": "send.delivery", + "expected": { + "acknowledged": false, + "activity_observed": false, + "attempts": 1, + "queue_policy": "queue_until_safe", + "realization": "native", + "recorded_result": "delivered", + "retry_action": "observe_existing_command", + "retry_safe": false, + "state": "delivered" + } + } + ] + }, + { + "sequence": 7, + "stage": "observe", + "case": "happy", + "expected_outcome": "success", + "deadline_ms": 20000, + "action": { + "executor": "duo", + "operation": "conversation.list", + "arguments": [ + "conversation", + "list", + "$CASE_SESSION_ID", + "--output", + "json" + ], + "auxiliary_operations": [ + "session.inspect" + ], + "minimum_invocations": 1, + "maximum_invocations": 0, + "instruction": "Poll session show and conversation list as distinct commands at approximately one-second intervals; stop on the declared outcome or the stage deadline and retain the final semantic conversation page." + }, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + } + }, + { + "id": "observe.assistant_text", + "expected": { + "assistant_blocks": 1, + "source_complete": true, + "text_digest": "sha256:b3300f2f6e0338d2b672891be66827d95779a2eabf4a75dcae1b6f56c9a4dc98" + } + } + ] + }, + { + "sequence": 8, + "stage": "command_inspection", + "case": "happy", + "expected_outcome": "success", + "deadline_ms": 15000, + "action": { + "executor": "duo", + "operation": "command.inspect", + "arguments": [ + "prompt", + "show", + "$CASE_COMMAND_ID", + "--output", + "json" + ], + "auxiliary_operations": [], + "minimum_invocations": 1, + "maximum_invocations": 1, + "instruction": "Inspect the case's original prompt command and retain its durable state and attempt list." + }, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + } + }, + { + "id": "command.delivery_integrity", + "expected": { + "attempts": 1, + "queue_policy": "queue_until_safe", + "realization": "native", + "recorded_result": "delivered", + "retry_action": "observe_existing_command", + "retry_safe": false, + "state": "delivered", + "unchanged": true + } + } + ] + }, + { + "sequence": 9, + "stage": "restart", + "case": "restart", + "expected_outcome": "success", + "deadline_ms": 15000, + "action": { + "executor": "duo", + "operation": "session.reconcile", + "arguments": [ + "session", + "reconcile", + "$HAPPY_SESSION_ID", + "--output", + "json" + ], + "auxiliary_operations": [], + "minimum_invocations": 1, + "maximum_invocations": 1, + "instruction": "Open the same authority store in a new Duo process, reconcile the still-live happy session, and retain same-live continuity." + }, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + } + }, + { + "id": "restart.authority_continuity", + "expected": { + "attempts": 1, + "distinct_processes": true, + "outcome": "same_live", + "same_store": true + } + } + ] + }, + { + "sequence": 10, + "stage": "bind", + "case": "restart", + "expected_outcome": "success", + "deadline_ms": 12000, + "action": { + "executor": "duo", + "operation": "session.inspect", + "arguments": [ + "session", + "show", + "$CASE_SESSION_ID", + "--output", + "json" + ], + "auxiliary_operations": [], + "minimum_invocations": 1, + "maximum_invocations": 0, + "instruction": "Inspect the just-launched session until the one claimed Pi attachment and durable runtime identity are present; each poll is a separate command." + }, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + } + }, + { + "id": "bind.runtime_identity", + "expected": { + "attachment_claimed": true, + "correlations": 1, + "instances": 1 + } + } + ] + }, + { + "sequence": 11, + "stage": "observe", + "case": "restart", + "expected_outcome": "success", + "deadline_ms": 20000, + "action": { + "executor": "duo", + "operation": "conversation.list", + "arguments": [ + "conversation", + "list", + "$CASE_SESSION_ID", + "--output", + "json" + ], + "auxiliary_operations": [ + "session.inspect" + ], + "minimum_invocations": 1, + "maximum_invocations": 0, + "instruction": "Poll session show and conversation list as distinct commands at approximately one-second intervals; stop on the declared outcome or the stage deadline and retain the final semantic conversation page." + }, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + } + }, + { + "id": "observe.restart_text", + "expected": { + "same_record": true, + "text_digest": "sha256:b3300f2f6e0338d2b672891be66827d95779a2eabf4a75dcae1b6f56c9a4dc98" + } + } + ] + }, + { + "sequence": 12, + "stage": "command_inspection", + "case": "restart", + "expected_outcome": "success", + "deadline_ms": 15000, + "action": { + "executor": "duo", + "operation": "command.inspect", + "arguments": [ + "prompt", + "show", + "$CASE_COMMAND_ID", + "--output", + "json" + ], + "auxiliary_operations": [], + "minimum_invocations": 1, + "maximum_invocations": 1, + "instruction": "Inspect the case's original prompt command and retain its durable state and attempt list." + }, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + } + }, + { + "id": "command.delivery_integrity", + "expected": { + "attempts": 1, + "queue_policy": "queue_until_safe", + "realization": "native", + "recorded_result": "delivered", + "retry_action": "observe_existing_command", + "retry_safe": false, + "state": "delivered", + "unchanged": true + } + } + ] + }, + { + "sequence": 13, + "stage": "send", + "case": "same_key_same_text", + "expected_outcome": "success", + "deadline_ms": 30000, + "action": { + "executor": "duo", + "operation": "prompt.deliver", + "arguments": [ + "prompt", + "send", + "$HAPPY_SESSION_ID", + "--text", + "$CANONICAL_PROMPT", + "--idempotency-key", + "$PRIMARY_KEY", + "--expires-at", + "$EXPIRES_AT", + "--output", + "json" + ], + "auxiliary_operations": [], + "minimum_invocations": 1, + "maximum_invocations": 1, + "instruction": "Resend the canonical text with the primary idempotency key and retain the original command identity." + }, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + } + }, + { + "id": "send.idempotent_replay", + "expected": { + "attempts": 1, + "new_turns": 0, + "same_command": true + } + } + ] + }, + { + "sequence": 14, + "stage": "command_inspection", + "case": "same_key_same_text", + "expected_outcome": "success", + "deadline_ms": 15000, + "action": { + "executor": "duo", + "operation": "command.inspect", + "arguments": [ + "prompt", + "show", + "$CASE_COMMAND_ID", + "--output", + "json" + ], + "auxiliary_operations": [], + "minimum_invocations": 1, + "maximum_invocations": 1, + "instruction": "Inspect the case's original prompt command and retain its durable state and attempt list." + }, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + } + }, + { + "id": "command.idempotent_no_duplication", + "expected": { + "attempts_delta": 0, + "commands_delta": 0, + "turns_delta": 0 + } + } + ] + }, + { + "sequence": 15, + "stage": "send", + "case": "same_key_different_text", + "expected_outcome": "success", + "deadline_ms": 30000, + "action": { + "executor": "duo", + "operation": "prompt.deliver", + "arguments": [ + "prompt", + "send", + "$HAPPY_SESSION_ID", + "--text", + "$CONFLICT_TEXT", + "--idempotency-key", + "$PRIMARY_KEY", + "--expires-at", + "$EXPIRES_AT", + "--output", + "json" + ], + "auxiliary_operations": [], + "minimum_invocations": 1, + "maximum_invocations": 1, + "instruction": "Send the conflict text with the primary idempotency key and retain the schema-valid conflict envelope; do not retry it." + }, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + } + }, + { + "id": "send.idempotency_conflict", + "expected": { + "code": "command.idempotency_conflict", + "effect": "no_effect", + "existing_digest": "sha256:b40d3a68eb33083b92199d734da2dece28395a6acc6d5e2cb9d7032dae4d11c7", + "idempotency_key": "portable-launcher-v1-primary", + "request_digest": "sha256:dbe904a42a9d9d9cdf7908508fa393ce43c9c53ea16a8cb8c9b82e7005f602bc", + "retry_action": "use_new_idempotency_key", + "retry_safe": false, + "target_original": true + } + } + ] + }, + { + "sequence": 16, + "stage": "command_inspection", + "case": "same_key_different_text", + "expected_outcome": "success", + "deadline_ms": 15000, + "action": { + "executor": "duo", + "operation": "command.inspect", + "arguments": [ + "prompt", + "show", + "$CASE_COMMAND_ID", + "--output", + "json" + ], + "auxiliary_operations": [], + "minimum_invocations": 1, + "maximum_invocations": 1, + "instruction": "Inspect the case's original prompt command and retain its durable state and attempt list." + }, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + } + }, + { + "id": "command.idempotency_no_drift", + "expected": { + "attempts_delta": 0, + "commands_delta": 0, + "queue_delta": 0, + "turns_delta": 0 + } + } + ] + }, + { + "sequence": 17, + "stage": "launch", + "case": "blocked", + "expected_outcome": "success", + "deadline_ms": 30000, + "action": { + "executor": "common_controller", + "operation": "prerequisite.require_admitted_then_blocked", + "arguments": [], + "auxiliary_operations": [], + "minimum_invocations": 1, + "maximum_invocations": 1, + "instruction": "Record prerequisite.blocked_induction_unavailable and do not launch or fabricate the admitted-then-blocked case while the canonical prerequisite is unavailable." + }, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + } + }, + { + "id": "launch.resolution", + "expected": { + "host": "herdr", + "prelaunch_prompt": false, + "runtime": "pi", + "selected_leaves": 1 + } + } + ] + }, + { + "sequence": 18, + "stage": "bind", + "case": "blocked", + "expected_outcome": "success", + "deadline_ms": 12000, + "action": { + "executor": "duo", + "operation": "session.inspect", + "arguments": [ + "session", + "show", + "$CASE_SESSION_ID", + "--output", + "json" + ], + "auxiliary_operations": [], + "minimum_invocations": 1, + "maximum_invocations": 0, + "instruction": "Inspect the just-launched session until the one claimed Pi attachment and durable runtime identity are present; each poll is a separate command." + }, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + } + }, + { + "id": "bind.runtime_identity", + "expected": { + "attachment_claimed": true, + "correlations": 1, + "instances": 1 + } + } + ] + }, + { + "sequence": 19, + "stage": "send", + "case": "blocked", + "expected_outcome": "success", + "deadline_ms": 30000, + "action": { + "executor": "duo", + "operation": "prompt.deliver", + "arguments": [ + "prompt", + "send", + "$CASE_SESSION_ID", + "--text", + "$CANONICAL_PROMPT", + "--idempotency-key", + "$PRIMARY_KEY", + "--expires-at", + "$EXPIRES_AT", + "--output", + "json" + ], + "auxiliary_operations": [], + "minimum_invocations": 1, + "maximum_invocations": 1, + "instruction": "Deliver the canonical prompt exactly once to this case's bound session with the primary idempotency key and finite expiry." + }, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + } + }, + { + "id": "send.delivery", + "expected": { + "acknowledged": false, + "activity_observed": false, + "attempts": 1, + "queue_policy": "queue_until_safe", + "realization": "native", + "recorded_result": "delivered", + "retry_action": "observe_existing_command", + "retry_safe": false, + "state": "delivered" + } + } + ] + }, + { + "sequence": 20, + "stage": "observe", + "case": "blocked", + "expected_outcome": "blocked", + "deadline_ms": 20000, + "action": { + "executor": "duo", + "operation": "conversation.list", + "arguments": [ + "conversation", + "list", + "$CASE_SESSION_ID", + "--output", + "json" + ], + "auxiliary_operations": [ + "session.inspect" + ], + "minimum_invocations": 1, + "maximum_invocations": 0, + "instruction": "Poll session show and conversation list as distinct commands at approximately one-second intervals; stop on the declared outcome or the stage deadline and retain the final semantic conversation page." + }, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + } + }, + { + "id": "observe.blocked", + "expected": { + "assistant_blocks": 0, + "attempts": 1, + "condition": "blocked", + "nonterminal": true + } + } + ] + }, + { + "sequence": 21, + "stage": "command_inspection", + "case": "blocked", + "expected_outcome": "success", + "deadline_ms": 15000, + "action": { + "executor": "duo", + "operation": "command.inspect", + "arguments": [ + "prompt", + "show", + "$CASE_COMMAND_ID", + "--output", + "json" + ], + "auxiliary_operations": [], + "minimum_invocations": 1, + "maximum_invocations": 1, + "instruction": "Inspect the case's original prompt command and retain its durable state and attempt list." + }, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + } + }, + { + "id": "command.delivery_integrity", + "expected": { + "attempts": 1, + "queue_policy": "queue_until_safe", + "realization": "native", + "recorded_result": "delivered", + "retry_action": "observe_existing_command", + "retry_safe": false, + "state": "delivered", + "unchanged": true + } + } + ] + }, + { + "sequence": 22, + "stage": "launch", + "case": "exited", + "expected_outcome": "success", + "deadline_ms": 30000, + "action": { + "executor": "duo", + "operation": "session.launch", + "arguments": [ + "session", + "launch", + "builder", + "--require", + "agent_runtime=pi", + "--workspace", + "$WORKSPACE", + "--host", + "herdr:$RUN/herdr/herdr.sock", + "--output", + "json" + ], + "auxiliary_operations": [], + "minimum_invocations": 1, + "maximum_invocations": 1, + "instruction": "Launch one fresh canonical Pi session without a pre-launch prompt and retain its session identifier." + }, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + } + }, + { + "id": "launch.resolution", + "expected": { + "host": "herdr", + "prelaunch_prompt": false, + "runtime": "pi", + "selected_leaves": 1 + } + } + ] + }, + { + "sequence": 23, + "stage": "bind", + "case": "exited", + "expected_outcome": "success", + "deadline_ms": 12000, + "action": { + "executor": "duo", + "operation": "session.inspect", + "arguments": [ + "session", + "show", + "$CASE_SESSION_ID", + "--output", + "json" + ], + "auxiliary_operations": [], + "minimum_invocations": 1, + "maximum_invocations": 0, + "instruction": "Inspect the just-launched session until the one claimed Pi attachment and durable runtime identity are present; each poll is a separate command. Emit the exact attachment-bound checkpoint so the common controller verifies and suspends that process before send." + }, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + } + }, + { + "id": "bind.runtime_identity", + "expected": { + "attachment_claimed": true, + "correlations": 1, + "instances": 1 + } + } + ] + }, + { + "sequence": 24, + "stage": "send", + "case": "exited", + "expected_outcome": "success", + "deadline_ms": 30000, + "action": { + "executor": "duo", + "operation": "prompt.deliver", + "arguments": [ + "prompt", + "send", + "$CASE_SESSION_ID", + "--text", + "$CANONICAL_PROMPT", + "--idempotency-key", + "$PRIMARY_KEY", + "--expires-at", + "$EXPIRES_AT", + "--output", + "json" + ], + "auxiliary_operations": [], + "minimum_invocations": 1, + "maximum_invocations": 1, + "instruction": "Deliver the canonical prompt exactly once to this case's bound session with the primary idempotency key and finite expiry. Emit the durable-delivery checkpoint for common controller verification." + }, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + } + }, + { + "id": "send.delivery", + "expected": { + "acknowledged": false, + "activity_observed": false, + "attempts": 1, + "queue_policy": "queue_until_safe", + "realization": "native", + "recorded_result": "delivered", + "retry_action": "observe_existing_command", + "retry_safe": false, + "state": "delivered" + } + } + ] + }, + { + "sequence": 25, + "stage": "observe", + "case": "exited", + "expected_outcome": "exited", + "deadline_ms": 20000, + "action": { + "executor": "duo", + "operation": "session.reconcile", + "arguments": [ + "session", + "reconcile", + "$CASE_SESSION_ID", + "--output", + "json" + ], + "auxiliary_operations": [], + "minimum_invocations": 1, + "maximum_invocations": 1, + "instruction": "After the common controller closes the exact suspended pane, reconcile this session once and retain the exited observation." + }, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + } + }, + { + "id": "observe.exited", + "expected": { + "attempts": 1, + "condition": "exited", + "process_success": true, + "same_instance": true + } + } + ] + }, + { + "sequence": 26, + "stage": "command_inspection", + "case": "exited", + "expected_outcome": "success", + "deadline_ms": 15000, + "action": { + "executor": "duo", + "operation": "command.inspect", + "arguments": [ + "prompt", + "show", + "$CASE_COMMAND_ID", + "--output", + "json" + ], + "auxiliary_operations": [], + "minimum_invocations": 1, + "maximum_invocations": 1, + "instruction": "Inspect the case's original prompt command and retain its durable state and attempt list." + }, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + } + }, + { + "id": "command.delivery_integrity", + "expected": { + "attempts": 1, + "queue_policy": "queue_until_safe", + "realization": "native", + "recorded_result": "delivered", + "retry_action": "observe_existing_command", + "retry_safe": false, + "state": "delivered", + "unchanged": true + } + } + ] + }, + { + "sequence": 27, + "stage": "launch", + "case": "timeout", + "expected_outcome": "success", + "deadline_ms": 30000, + "action": { + "executor": "duo", + "operation": "session.launch", + "arguments": [ + "session", + "launch", + "builder", + "--require", + "agent_runtime=pi", + "--workspace", + "$WORKSPACE", + "--host", + "herdr:$RUN/herdr/herdr.sock", + "--output", + "json" + ], + "auxiliary_operations": [], + "minimum_invocations": 1, + "maximum_invocations": 1, + "instruction": "Launch one fresh canonical Pi session without a pre-launch prompt and retain its session identifier." + }, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + } + }, + { + "id": "launch.resolution", + "expected": { + "host": "herdr", + "prelaunch_prompt": false, + "runtime": "pi", + "selected_leaves": 1 + } + } + ] + }, + { + "sequence": 28, + "stage": "bind", + "case": "timeout", + "expected_outcome": "success", + "deadline_ms": 12000, + "action": { + "executor": "duo", + "operation": "session.inspect", + "arguments": [ + "session", + "show", + "$CASE_SESSION_ID", + "--output", + "json" + ], + "auxiliary_operations": [], + "minimum_invocations": 1, + "maximum_invocations": 0, + "instruction": "Inspect the just-launched session until the one claimed Pi attachment and durable runtime identity are present; each poll is a separate command. Emit the exact attachment-bound checkpoint so the common controller verifies and suspends that process before send." + }, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + } + }, + { + "id": "bind.runtime_identity", + "expected": { + "attachment_claimed": true, + "correlations": 1, + "instances": 1 + } + } + ] + }, + { + "sequence": 29, + "stage": "send", + "case": "timeout", + "expected_outcome": "success", + "deadline_ms": 30000, + "action": { + "executor": "duo", + "operation": "prompt.deliver", + "arguments": [ + "prompt", + "send", + "$CASE_SESSION_ID", + "--text", + "$CANONICAL_PROMPT", + "--idempotency-key", + "$PRIMARY_KEY", + "--expires-at", + "$EXPIRES_AT", + "--output", + "json" + ], + "auxiliary_operations": [], + "minimum_invocations": 1, + "maximum_invocations": 1, + "instruction": "Deliver the canonical prompt exactly once to this case's bound session with the primary idempotency key and finite expiry. Emit the durable-delivery checkpoint for common controller verification." + }, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + } + }, + { + "id": "send.delivery", + "expected": { + "acknowledged": false, + "activity_observed": false, + "attempts": 1, + "queue_policy": "queue_until_safe", + "realization": "native", + "recorded_result": "delivered", + "retry_action": "observe_existing_command", + "retry_safe": false, + "state": "delivered" + } + } + ] + }, + { + "sequence": 30, + "stage": "observe", + "case": "timeout", + "expected_outcome": "timeout", + "deadline_ms": 20000, + "action": { + "executor": "duo", + "operation": "conversation.list", + "arguments": [ + "conversation", + "list", + "$CASE_SESSION_ID", + "--output", + "json" + ], + "auxiliary_operations": [ + "session.inspect" + ], + "minimum_invocations": 1, + "maximum_invocations": 0, + "instruction": "Poll session show and conversation list as distinct commands at approximately one-second intervals; stop on the declared outcome or the stage deadline and retain the final semantic conversation page." + }, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + } + }, + { + "id": "observe.timeout", + "expected": { + "assistant_blocks": 0, + "attempts": 1, + "delivered": true, + "elapsed_max_ms": 22500, + "elapsed_min_ms": 20000, + "elapsed_ms": 20000, + "outer_timed_out": false, + "process_success": true, + "stopped_verified": true + } + } + ] + }, + { + "sequence": 31, + "stage": "command_inspection", + "case": "timeout", + "expected_outcome": "success", + "deadline_ms": 15000, + "action": { + "executor": "duo", + "operation": "command.inspect", + "arguments": [ + "prompt", + "show", + "$CASE_COMMAND_ID", + "--output", + "json" + ], + "auxiliary_operations": [], + "minimum_invocations": 1, + "maximum_invocations": 1, + "instruction": "Inspect the case's original prompt command and retain its durable state and attempt list." + }, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + } + }, + { + "id": "command.delivery_integrity", + "expected": { + "attempts": 1, + "queue_policy": "queue_until_safe", + "realization": "native", + "recorded_result": "delivered", + "retry_action": "observe_existing_command", + "retry_safe": false, + "state": "delivered", + "unchanged": true + } + } + ] + }, + { + "sequence": 32, + "stage": "cleanup", + "case": "run", + "expected_outcome": "success", + "deadline_ms": 30000, + "action": { + "executor": "common_controller", + "operation": "fixture.cleanup", + "arguments": [], + "auxiliary_operations": [], + "minimum_invocations": 1, + "maximum_invocations": 1, + "instruction": "Run common controller cleanup, independently inspect/export the fixture, and remove only the run root; cleanup is always last." + }, + "assertions": [ + { + "id": "stage.completed_within_deadline", + "expected": { + "within_deadline": true + } + }, + { + "id": "cleanup.complete", + "expected": { + "export_complete": true, + "panes_remaining": 0, + "pids_remaining": 0, + "root_removed": true, + "sockets_remaining": 0 + } + } + ] + } + ] +} diff --git a/contracts/schemas/duo-portable-launcher-conformance-result-v1.schema.json b/contracts/schemas/duo-portable-launcher-conformance-result-v1.schema.json new file mode 100644 index 0000000..54aa880 --- /dev/null +++ b/contracts/schemas/duo-portable-launcher-conformance-result-v1.schema.json @@ -0,0 +1,217 @@ +{ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "$id": "urn:duo:schema:portable-launcher-conformance-result:v1", + "title": "Duo portable launcher conformance result v1", + "type": "object", + "required": ["schema", "suite", "run", "pins", "stages", "summary", "scrub"], + "properties": { + "schema": {"const": "duo.portable-launcher-conformance-result/v1"}, + "suite": {"$ref": "#/$defs/suite"}, + "run": {"$ref": "#/$defs/run"}, + "pins": {"$ref": "#/$defs/pins"}, + "stages": {"type": "array", "minItems": 32, "maxItems": 32, "items": {"$ref": "#/$defs/stage"}}, + "summary": {"$ref": "#/$defs/summary"}, + "scrub": {"$ref": "#/$defs/scrub"} + }, + "additionalProperties": false, + "$defs": { + "digest": {"type": "string", "pattern": "^sha256:[0-9a-f]{64}$"}, + "blob": {"type": "string", "pattern": "^blob:sha256:[0-9a-f]{64}$"}, + "suite": { + "type": "object", + "required": ["name", "revision", "manifest_digest", "oracle_digest"], + "properties": { + "name": {"const": "portable-launcher-delegation"}, + "revision": {"const": 1}, + "manifest_digest": {"$ref": "#/$defs/digest"}, + "oracle_digest": {"$ref": "#/$defs/digest"} + }, + "additionalProperties": false + }, + "run": { + "type": "object", + "required": ["run_id", "observed_at", "host_os", "host_arch", "fixture_root", "plugins_enabled", "mcp_enabled", "terminal_input_used"], + "properties": { + "run_id": {"type": "string", "minLength": 1}, + "observed_at": {"type": "string", "format": "date-time"}, + "host_os": {"const": "linux"}, + "host_arch": {"const": "x86_64"}, + "fixture_root": {"const": "$RUN"}, + "plugins_enabled": {"const": false}, + "mcp_enabled": {"const": false}, + "terminal_input_used": {"const": false} + }, + "additionalProperties": false + }, + "binary": { + "type": "object", + "required": ["name", "version", "executable_sha256"], + "properties": { + "name": {"type": "string", "minLength": 1}, + "version": {"type": "string", "minLength": 1}, + "executable_sha256": {"$ref": "#/$defs/digest"} + }, + "additionalProperties": false + }, + "pins": { + "type": "object", + "required": ["launcher", "duo", "skill", "config", "authority", "host", "runtime", "model", "external_schema"], + "properties": { + "launcher": { + "allOf": [ + {"$ref": "#/$defs/binary"}, + {"properties": {"name": {"enum": ["amp", "opencode", "codex"]}}} + ] + }, + "duo": { + "type": "object", + "required": ["version", "commit", "build_date", "executable_sha256"], + "properties": { + "version": {"type": "string", "minLength": 1}, + "commit": {"type": "string", "minLength": 1}, + "build_date": {"type": "string", "minLength": 1}, + "executable_sha256": {"$ref": "#/$defs/digest"} + }, + "additionalProperties": false + }, + "skill": { + "type": "object", + "required": ["name", "format_version", "content_digest", "installation_id"], + "properties": { + "name": {"const": "duo-delegation-loop"}, + "format_version": {"const": "duo.skill/duo-delegation-loop/v1"}, + "content_digest": {"$ref": "#/$defs/digest"}, + "installation_id": {"type": "string", "minLength": 1} + }, + "additionalProperties": false + }, + "config": { + "type": "object", + "required": ["schema", "effective_digest"], + "properties": {"schema": {"const": "duo.config/v3"}, "effective_digest": {"$ref": "#/$defs/digest"}}, + "additionalProperties": false + }, + "authority": { + "type": "object", + "required": ["schema_version", "store_digest_before", "store_digest_after"], + "properties": {"schema_version": {"const": 1}, "store_digest_before": {"$ref": "#/$defs/digest"}, "store_digest_after": {"$ref": "#/$defs/digest"}}, + "additionalProperties": false + }, + "host": { + "type": "object", + "required": ["name", "version", "protocol", "schema_digest", "executable_sha256"], + "properties": { + "name": {"const": "herdr"}, + "version": {"const": "0.8.2"}, + "protocol": {"const": "herdr-socket-api/20"}, + "schema_digest": {"const": "sha256:c48f1f54ee0150ca27e11fd44455fe94aeadb20fdf4e4a62393ed822a4e5b150"}, + "executable_sha256": {"$ref": "#/$defs/digest"} + }, + "additionalProperties": false + }, + "runtime": { + "type": "object", + "required": ["name", "version", "format", "adapter_build", "conformance_record", "executable_sha256", "delivery_asset_sha256"], + "properties": { + "name": {"const": "pi"}, + "version": {"const": "0.83.0"}, + "format": {"const": "pi-session-jsonl/v3"}, + "adapter_build": {"const": "stage1"}, + "conformance_record": {"const": "pi-0.83.0-2026-08-23"}, + "executable_sha256": {"$ref": "#/$defs/digest"}, + "delivery_asset_sha256": {"const": "sha256:2708a3435821fb2e72302ee56d8a07fe62c265214ae8fd342e385c03ef245a23"} + }, + "additionalProperties": false + }, + "model": { + "type": "object", + "required": ["provider", "model_line"], + "properties": {"provider": {"const": "openai-codex"}, "model_line": {"const": "gpt-5.6-luna"}}, + "additionalProperties": false + }, + "external_schema": { + "type": "object", + "required": ["identity", "digest"], + "properties": {"identity": {"const": "duo.external/v1"}, "digest": {"const": "sha256:8b055ff60f0ca5f3da46302671591aa0dae61185cd9f1f15fcceb36d841e7497"}}, + "additionalProperties": false + } + }, + "additionalProperties": false + }, + "assertion": { + "type": "object", + "required": ["id", "expected", "actual", "matched"], + "properties": { + "id": { + "enum": [ + "bind.runtime_identity", "blocked.induction_available", "cleanup.complete", + "command.delivery_integrity", "command.idempotency_no_drift", "command.idempotent_no_duplication", + "discovery.skill_identity", "launch.resolution", "observe.assistant_text", "observe.blocked", + "observe.exited", "observe.restart_text", "observe.timeout", "preflight.ready", + "prerequisite.not_reached", "restart.authority_continuity", "send.delivery", + "send.idempotency_conflict", "send.idempotent_replay", "setup.fixture_isolated_and_pinned", + "stage.completed_within_deadline", "stage.failure" + ] + }, + "expected": {"type": "object"}, + "actual": {"type": "object"}, + "matched": {"type": "boolean"} + }, + "additionalProperties": false + }, + "retry": { + "type": "object", + "required": ["safe", "action"], + "properties": {"safe": {"type": "boolean"}, "action": {"type": "string", "minLength": 1}}, + "additionalProperties": false + }, + "error": { + "type": "object", + "required": ["code", "message", "effect", "retry"], + "properties": { + "code": {"type": "string", "minLength": 1}, + "message": {"type": "string", "minLength": 1}, + "effect": {"enum": ["no_effect", "possible_effect", "unknown_effect"]}, + "retry": {"$ref": "#/$defs/retry"} + }, + "additionalProperties": false + }, + "stage": { + "type": "object", + "required": ["sequence", "stage", "case", "verdict", "outcome", "started_offset_ms", "duration_ms", "assertions", "evidence", "error"], + "properties": { + "sequence": {"type": "integer", "minimum": 1}, + "stage": {"enum": ["setup", "discovery", "preflight", "launch", "bind", "send", "observe", "command_inspection", "restart", "cleanup"]}, + "case": {"enum": ["run", "happy", "blocked", "exited", "timeout", "restart", "same_key_same_text", "same_key_different_text"]}, + "verdict": {"enum": ["pass", "fail"]}, + "outcome": {"enum": ["success", "blocked", "exited", "timeout", "error"]}, + "started_offset_ms": {"type": "integer", "minimum": 0}, + "duration_ms": {"type": "integer", "minimum": 0}, + "assertions": {"type": "array", "minItems": 1, "items": {"$ref": "#/$defs/assertion"}}, + "evidence": {"type": "array", "minItems": 1, "items": {"$ref": "#/$defs/blob"}}, + "error": {"oneOf": [{"type": "null"}, {"$ref": "#/$defs/error"}]} + }, + "additionalProperties": false + }, + "summary": { + "type": "object", + "required": ["verdict", "first_failed_stage", "first_failed_case"], + "properties": { + "verdict": {"enum": ["pass", "fail"]}, + "first_failed_stage": {"enum": [null, "setup", "discovery", "preflight", "launch", "bind", "send", "observe", "command_inspection", "restart", "cleanup"]}, + "first_failed_case": {"enum": [null, "run", "happy", "blocked", "exited", "timeout", "restart", "same_key_same_text", "same_key_different_text"]} + }, + "additionalProperties": false + }, + "scrub": { + "type": "object", + "required": ["status", "policy", "findings"], + "properties": { + "status": {"enum": ["pass", "fail"]}, + "policy": {"const": "portable-launcher-evidence/v1"}, + "findings": {"type": "array", "items": {"type": "string"}} + }, + "additionalProperties": false + } + } +} diff --git a/contracts/schemas/duo-projection-stamp-v1.schema.json b/contracts/schemas/duo-projection-stamp-v1.schema.json index 1e554e5..2233857 100644 --- a/contracts/schemas/duo-projection-stamp-v1.schema.json +++ b/contracts/schemas/duo-projection-stamp-v1.schema.json @@ -13,18 +13,66 @@ "type": "object", "required": ["harness", "tested_version_range"], "properties": { - "harness": {"enum": ["claude_code", "codex", "devin", "opencode", "pi"]}, - "tested_version_range": {"type": "string", "minLength": 1} + "harness": {"enum": ["claude_code", "codex", "devin", "opencode", "pi", "portable_launchers"]}, + "tested_version_range": {"type": "string", "minLength": 1}, + "launchers": { + "type": "array", + "items": {"$ref": "#/$defs/launcher"}, + "minItems": 1 + } }, "additionalProperties": true }, + "components": { + "type": "array", + "items": {"type": "string", "minLength": 1}, + "minItems": 1, + "uniqueItems": true + }, "installation_id": {"type": "string", "minLength": 1}, "generated_at": {"type": "string", "format": "date-time"}, "source_assets": {"$ref": "#/$defs/digested_paths"}, "files": {"$ref": "#/$defs/digested_paths"} }, "additionalProperties": false, + "allOf": [ + { + "if": { + "properties": { + "target": { + "properties": {"harness": {"const": "portable_launchers"}}, + "required": ["harness"] + } + }, + "required": ["target"] + }, + "then": { + "required": ["components"], + "properties": { + "target": {"required": ["launchers"]}, + "components": { + "items": {"const": "filesystem_skill"}, + "maxItems": 1 + } + } + } + } + ], "$defs": { + "launcher": { + "type": "object", + "required": ["name", "tested_versions"], + "properties": { + "name": {"enum": ["amp", "opencode", "codex"]}, + "tested_versions": { + "type": "array", + "items": {"type": "string", "minLength": 1}, + "minItems": 1, + "uniqueItems": true + } + }, + "additionalProperties": false + }, "digested_paths": { "type": "array", "items": { diff --git a/contrib/gen-contracts-manifest b/contrib/gen-contracts-manifest index 294244b..a62eb0c 100755 --- a/contrib/gen-contracts-manifest +++ b/contrib/gen-contracts-manifest @@ -3,8 +3,8 @@ # repo and write contracts/MANIFEST. # # Hashes: -# contracts/schemas/ -> schemas/... -# contracts/fixtures/duo-external-v1/ -> fixtures/duo-external-v1/... +# contracts/schemas/ -> schemas/... +# contracts/fixtures/ -> fixtures/... # # and writes contracts/MANIFEST: one "sha256 path" line per file (paths # relative to contracts/), sorted, so the output is deterministic and a @@ -20,7 +20,7 @@ dst="$repo_root/contracts" echo "# Regenerate with contrib/gen-contracts-manifest; do not edit by hand." ( cd "$dst" - find schemas fixtures/duo-external-v1 -type f -print0 | LC_ALL=C sort -z | xargs -0 sha256sum + find schemas fixtures -type f -print0 | LC_ALL=C sort -z | xargs -0 sha256sum ) } > "$dst/MANIFEST" diff --git a/contrib/portable-launcher-conformance/amp/main.go b/contrib/portable-launcher-conformance/amp/main.go new file mode 100644 index 0000000..661e5d7 --- /dev/null +++ b/contrib/portable-launcher-conformance/amp/main.go @@ -0,0 +1,28 @@ +// Package main provides the thin Amp adapter for the portable-launcher suite. +package main + +import ( + "context" + "errors" + "fmt" + "os" + "path/filepath" + + "github.com/procrastivity/duo/internal/conformance/portablelauncher" +) + +func main() { + root := os.Getenv("DUO_CONFORMANCE_RUN_ROOT") + runOrigin := os.Getenv(portablelauncher.CommandRunOriginEnv) + spec := portablelauncher.DriverSpec{ + Name: "amp", Executable: filepath.Join(root, "bin", "amp"), + Arguments: []string{"-x", portablelauncher.TaskArgument, "--stream-json", "--no-ide", "--settings-file", filepath.Join(root, "xdg", "config", "amp", "settings.json")}, + } + capture, runErr := portablelauncher.RunDriver(context.Background(), spec, portablelauncher.RequestFromRunRoot(root, runOrigin)) + persistErr := portablelauncher.PersistDriverCapture(root, capture) + err := errors.Join(runErr, persistErr) + if err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } +} diff --git a/contrib/portable-launcher-conformance/codex/main.go b/contrib/portable-launcher-conformance/codex/main.go new file mode 100644 index 0000000..330f530 --- /dev/null +++ b/contrib/portable-launcher-conformance/codex/main.go @@ -0,0 +1,28 @@ +// Package main provides the thin Codex adapter for the portable-launcher suite. +package main + +import ( + "context" + "errors" + "fmt" + "os" + "path/filepath" + + "github.com/procrastivity/duo/internal/conformance/portablelauncher" +) + +func main() { + root := os.Getenv("DUO_CONFORMANCE_RUN_ROOT") + runOrigin := os.Getenv(portablelauncher.CommandRunOriginEnv) + spec := portablelauncher.DriverSpec{ + Name: "codex", Executable: filepath.Join(root, "bin", "codex"), + Arguments: []string{"--ask-for-approval", "never", "--sandbox", "workspace-write", "exec", "--ephemeral", "--ignore-user-config", "--skip-git-repo-check", "--json", portablelauncher.TaskArgument}, + } + capture, runErr := portablelauncher.RunDriver(context.Background(), spec, portablelauncher.RequestFromRunRoot(root, runOrigin)) + persistErr := portablelauncher.PersistDriverCapture(root, capture) + err := errors.Join(runErr, persistErr) + if err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } +} diff --git a/contrib/portable-launcher-conformance/opencode/main.go b/contrib/portable-launcher-conformance/opencode/main.go new file mode 100644 index 0000000..07b1736 --- /dev/null +++ b/contrib/portable-launcher-conformance/opencode/main.go @@ -0,0 +1,30 @@ +// Package main provides the thin OpenCode adapter for the portable-launcher suite. +package main + +import ( + "context" + "errors" + "fmt" + "os" + "path/filepath" + + "github.com/procrastivity/duo/internal/conformance/portablelauncher" +) + +func main() { + root := os.Getenv("DUO_CONFORMANCE_RUN_ROOT") + runOrigin := os.Getenv(portablelauncher.CommandRunOriginEnv) + spec := portablelauncher.DriverSpec{ + Name: "opencode", Executable: filepath.Join(root, "bin", "opencode"), + Arguments: []string{"run", "--pure", "--format", "json", portablelauncher.TaskArgument}, + } + request := portablelauncher.RequestFromRunRoot(root, runOrigin) + request.Environment = map[string]string{"OPENCODE_DISABLE_MODELS_FETCH": "1"} + capture, runErr := portablelauncher.RunDriver(context.Background(), spec, request) + persistErr := portablelauncher.PersistDriverCapture(root, capture) + err := errors.Join(runErr, persistErr) + if err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } +} diff --git a/docs/vnext/duo-portable-launcher-conformance-contract.md b/docs/vnext/duo-portable-launcher-conformance-contract.md new file mode 100644 index 0000000..1faf8d2 --- /dev/null +++ b/docs/vnext/duo-portable-launcher-conformance-contract.md @@ -0,0 +1,612 @@ +# Portable launcher skill installation and authority preflight + +> Status: **Stage 2 shared projection and launcher preflight implemented.** +> Live Amp, OpenCode, and Codex conformance evidence remains for Stages 3–5. + +This contract specializes the general projection rules in +[`duo-vnext-installation-contract.md`](./duo-vnext-installation-contract.md) +for one shared filesystem skill. Amp, OpenCode, and Codex are outer drivers of +the existing Duo CLI. This contract adds no runtime adapter, plugin, hook, MCP +surface, remote authority, or terminal-input fallback. + +## 1. Observed baseline versus the contract + +The distinction is important because the shared product seams now exist, but +the three launcher support claims still require live evidence. + +### 1.1 Existing behavior in this checkout + +- [`../../skills/duo-delegation-loop/SKILL.md`](../../skills/duo-delegation-loop/SKILL.md) + is the normative authored skill. The implemented projection is 6,284 bytes + with SHA-256 + `6f8bc16b656f564a949c99f12d4cf0bcd04ec2c9b49a8b74fb65b2e355b5d182`. +- The 2026-09-17 live probe in + [`../../evidence/portable-launcher-conformance/step-01/report.md`](../../evidence/portable-launcher-conformance/step-01/report.md) + proved project discovery at + `.agents/skills/duo-delegation-loop/SKILL.md` for Amp + `0.0.1789675234-g2899fe`, OpenCode `1.18.31`, and Codex CLI `0.154.0`. + Both a copied directory and a directory symlink worked on the probed Linux + host. The repository-relative symlink target + `../../skills/duo-delegation-loop` was also live-confirmed. The checkout did + not itself contain `.agents/`. +- `internal/asset` resolves shipped or embedded assets. `internal/manifest` + declares and installs the `portable_launchers` target with the normative + `duo.projection-stamp/v1` ownership record. Its older `Stamp` and `Drift` + primitives still use `.duo-manifest-stamp.json` and do not confer ownership + on the portable target. +- `contracts/schemas/duo-projection-stamp-v1.schema.json` and the installation + contract define `.duo-generated.json`. `internal/runtime/devin` has a + target-local implementation of that pattern. It is useful precedent, not + the owner of this launcher-neutral skill. +- `internal/doctor` owns authority-store diagnostics. `internal/cli/doctor.go` + composes those diagnostics with effective-path config inspection, selected + workspace and host deduction, bounded Herdr reachability, portable skill + inspection, and other runtime-specific sections. Its launcher preflight + uses a physical read-only store handle, inspects writer leases without + acquiring one, and reports harness orphans without reaping them. +- `duo install portable-launchers` owns explicit install and repair. Diagnosis + never invokes it or writes projection state. + +### 1.2 Contract introduced by this document + +The installed product projects one stamped **copy** of the canonical bytes +into a selected workspace. A source checkout may separately use the proven +relative symlink as a development convenience, but that symlink is not a +product installation and conveys no Duo ownership. + +The product target is named `portable_launchers` in JSON and +`portable-launchers` on the CLI. Its only payload is the filesystem skill: + +```text +/.agents/skills/duo-delegation-loop/ +├── SKILL.md +└── .duo-generated.json +``` + +There is no per-launcher copy. The project target is normative; the shared +user root `~/.agents/skills` remains observed fallback behavior, not an +installation target in this Matter. + +## 2. Canonical artifact and public identity + +The public identity uses **both** an authored format version and a digest: + +| Field | Value and rule | +|---|---| +| `name` | `duo-delegation-loop` | +| `format_version` | `duo.skill/duo-delegation-loop/v1` | +| `content_digest` | `sha256:` followed by the lowercase SHA-256 of the exact installed `SKILL.md` bytes | +| `media_type` | `text/markdown` | + +The baseline identity is therefore +`duo.skill/duo-delegation-loop/v1@sha256:6f8bc16b656f564a949c99f12d4cf0bcd04ec2c9b49a8b74fb65b2e355b5d182`. +That digest is a baseline pin, not a promise that Stage 2 will leave the +manual-install prose unchanged. If Stage 2 changes any byte, the manifest, +stamp, doctor, and evidence must report the new digest. + +`format_version` changes only for an incompatible change to the skill's +consumer contract or installed shape. Editorial or command-guidance changes +within the same shape retain `v1` and necessarily change `content_digest`. +The digest is over bytes as packaged, with no line-ending normalization and +no trailing-newline insertion. + +The authored source remains +`skills/duo-delegation-loop/SKILL.md`. Product packaging must expose those +same bytes as the logical shipped asset +`skills/duo-delegation-loop/SKILL.md` through `internal/asset.ReadDefault` +(not the override-first `Resolve` path); a separately edited copy under +`assets/` is forbidden. The installed product, including the embedded fallback +used by a bare binary, must therefore work without a source checkout. How the +build embeds that source is a packaging detail, but the byte-equality test +between authored source, resolved shipped asset, and rendered `SKILL.md` is +mandatory. + +Rendering is the identity transform: no path, version, launcher, prompt, or +configuration value is substituted into `SKILL.md`. The projection stamp is +the generated marker. No comment is prepended to `SKILL.md`, because skill +YAML frontmatter must remain at byte zero. + +`duo doctor --output json` and every conformance capture report +`name`, `format_version`, and prefixed `content_digest`. Captures additionally +record the Duo version/commit, manifest digest, launcher executable identity +and version, host/Herdr pin, and observation time. A bare digest or a format +version alone is not a complete skill identity. + +## 3. Ownership boundaries + +Stage 2 extends existing seams rather than creating a launcher subsystem. + +| Concern | Owner | +|---|---| +| Authored canonical bytes | `skills/duo-delegation-loop/SKILL.md` | +| Installed/embedded byte resolution | `internal/asset`; user overrides are not allowed for this normative skill | +| Identity rendering, normative stamp parsing, path validation, drift classification, and non-destructive placement | `internal/manifest`, replacing or widening its bootstrap stamp helpers rather than adding a parallel projection package | +| Static asset digest and the single shared target declaration | `internal/manifest.Build` and `duo.manifest/v1` | +| `duo install portable-launchers` command wiring | `internal/cli`; it delegates all rendering and mutation policy to `internal/manifest` | +| Read-only projection and authority diagnosis | `internal/doctor`, composed and rendered by `internal/cli/doctor.go` | +| Herdr selection and non-mutating reachability probe | existing `internal/launch/materialize` deduction plus `internal/host/herdr.Factory.Probe` | +| Executable registration | `internal/cli.NewRootCommand`; `cmd/duo/main.go` remains only the entry point | + +`internal/runtime/devin` stays the owner of the Devin launch-time projection. +Its private stamp structs and status constants must not become the portable +skill API, and the portable installer must not import a runtime package. + +The projection root is the skill directory, not `.agents` or +`.agents/skills`. The stamp owns only `SKILL.md`; unrelated siblings and +unlisted files are never removed. A first successful install mints a +non-empty opaque `installation_id`. Repair preserves that ID. `generated_at` +changes only after a successful placement. + +The stamp is `duo.projection-stamp/v1` with this specialization: + +```json +{ + "schema": "duo.projection-stamp/v1", + "product_version": "", + "manifest_digest": "sha256:<64 lowercase hex>", + "projection_format": "duo.skill/duo-delegation-loop/v1", + "target": { + "harness": "portable_launchers", + "tested_version_range": "amp=0.0.1789675234-g2899fe;opencode=1.18.31;codex=0.154.0", + "launchers": [ + {"name": "amp", "tested_versions": ["0.0.1789675234-g2899fe"]}, + {"name": "opencode", "tested_versions": ["1.18.31"]}, + {"name": "codex", "tested_versions": ["0.154.0"]} + ] + }, + "components": ["filesystem_skill"], + "installation_id": "", + "generated_at": "", + "source_assets": [ + { + "path": "skills/duo-delegation-loop/SKILL.md", + "digest": "sha256:" + } + ], + "files": [ + {"path": "SKILL.md", "digest": "sha256:"} + ] +} +``` + +The `launchers` array is ordered `amp`, `opencode`, `codex`; `tested_versions` +contains exact live pins, not inferred ranges. Stage 2 widens the existing +stamp schema's `target.harness` enum to include `portable_launchers`, schemas +the launcher rows, and adds the required closed `components` array. For this +target it contains only `filesystem_skill`. Slash-separated stamp paths are +relative to the projection root and must be clean, non-empty, non-absolute, +and contain no `..`. An implementation must reject duplicate paths and any +stamp that tries to claim the stamp itself or a path outside the root. + +## 4. States, precedence, and non-destructive transitions + +The closed projection state enum is: + +```text +current | missing | stale | modified | incompatible | unowned_conflict +``` + +Inspection uses `lstat` and never follows a destination symlink when deciding +ownership. `SKILL.md` must be a regular file. The deterministic precedence +below prevents a mixed tree from receiving an optimistic state. + +1. **`incompatible`** — a stamp is present but malformed, has an unknown + schema or newer/unknown projection format, names another target, contains + unsafe/duplicate paths, or the target has an unsupported filesystem shape. + This includes a stamped symlink where the product requires a copied regular + file. No write is allowed. This specializes the general installation + contract for a shared target with no singular harness executable: outer + launcher-version compatibility is evaluated separately against the + manifest pins and does not change ownership or byte drift. +2. **`modified`** — a valid recognized stamp claims an existing regular file, + but its bytes do not match the digest in that stamp. No write is allowed. +3. **`unowned_conflict`** — an expected destination exists but no valid stamp + claims it. A directory symlink, including the proven repository-relative + development symlink, is unowned for product-install purposes. No write is + allowed. The stable conflict code is + `projection.user_file_conflict`. +4. **`missing`** — no expected destination blocks placement, or a valid stamp + owns an expected path that is absent. An absent target tree and an empty + pre-existing target directory are both `missing`. +5. **`stale`** — all existing files still match a valid recognized prior + stamp, but that stamp's Duo manifest, product version, source-asset digest, + file set, or known older projection format differs from what the current + binary would produce. +6. **`current`** — the valid stamp, current manifest and format, canonical + source digest, complete expected file set, and installed file bytes all + agree. + +An outer launcher version outside the manifest's exact tested pins is an +unsupported conformance claim, but it does not mutate otherwise-current +files. Captures must reject such a run as launcher-incompatible. Doctor may +report that version when it is supplied or detected, but it must not guess +which outer launcher invoked it. + +### 4.1 Install and repair + +The public write surface is: + +```text +duo install portable-launchers [--workspace PATH] [--repair] [--output text|json] +``` + +`--workspace` resolves exactly as the launch command does: the flag, else the +current directory, made absolute and cleaned. Automated tests set all roots to +temporary directories; they never write a real home skill tree. + +- Plain install writes only a fresh `missing` projection for which neither + expected destination exists. `current` is an idempotent no-op. If a stamp or + partial owned projection already exists, plain install makes no change and + tells the operator to inspect or use `--repair`. +- `--repair` is an idempotent no-op for `current`. It may create a fresh + `missing` projection, restore a missing file that a valid stamp owns, or + replace `stale` files only after every existing file claimed by the prior + stamp still matches that stamp. It may add a newly expected file only when + its destination is absent. +- Neither form writes in `modified`, `incompatible`, or `unowned_conflict`. + There is no force flag. `modified` raises `projection.modified`; + `unowned_conflict` raises `projection.user_file_conflict`; both retain + `effect: no_effect`. Incompatible input raises the new stable + `projection.incompatible` code (class `unsupported`, effect `no_effect`) and + tells the operator to upgrade Duo or move the unknown projection for manual + review. A partial owned projection passed without `--repair` raises + `invalid.precondition` with no write. +- Repair may remove an obsolete path only when the prior valid stamp claims + it and its current digest still matches. It never removes an unlisted file + or any parent directory it did not create and find empty. +- Rendering occurs in a private sibling staging directory. Payload files are + fsynced and placed by same-filesystem rename; the stamp is the final commit + marker. A crash before the stamp commit can only yield a safe non-current + state on the next inspection, never permission to overwrite user bytes. + +The installer validates the selected workspace and every ancestor before +writing. It refuses symlink traversal through the managed destination and +does not resolve a target outside the selected workspace. + +Successful creation, repair, and the `current` no-op exit `0`; the three +non-destructive conflicts and `invalid.precondition` exit `1`; malformed CLI +usage exits `2`; an unexpected staging, fsync, or placement failure exits `4`. +JSON success reports `target`, `state`, `changed` (boolean), `root`, +`installation_id`, `format_version`, and `content_digest`. A failure uses the +existing structured error envelope and writes no success result. + +### 4.2 Development projection versus installed projection + +For repository development on the probed Linux host, this remains valid: + +```text +.agents/skills/duo-delegation-loop -> ../../skills/duo-delegation-loop +``` + +It keeps edits live and is suitable for deterministic discovery tests. It is +not portable product state: it depends on source-tree layout, cannot carry a +stamp inside the symlink without writing into the authored source, and has no +safe uninstall/repair ownership. Product installation therefore always uses +the stamped copied form. The installer reports a development link at the +product destination as `unowned_conflict` and asks the operator to keep it, +move it, or remove it explicitly; it never converts the link in place. + +## 5. Manifest contract + +`duo manifest --output json` declares exactly one shared target in +`harness_targets`. Stage 2 extends `internal/manifest.HarnessTarget` to these +stable fields: + +```json +{ + "name": "portable_launchers", + "status": "unverified", + "scope": "workspace", + "discovery_root": ".agents/skills", + "projection_root": ".agents/skills/duo-delegation-loop", + "stamp_file": ".duo-generated.json", + "projection_format": "duo.skill/duo-delegation-loop/v1", + "components": ["filesystem_skill"], + "artifact": { + "name": "duo-delegation-loop", + "media_type": "text/markdown", + "source_asset": "skills/duo-delegation-loop/SKILL.md", + "output_path": "SKILL.md", + "content_digest": "sha256:<64 lowercase hex>" + }, + "launchers": [ + {"name": "amp", "tested_versions": ["0.0.1789675234-g2899fe"]}, + {"name": "opencode", "tested_versions": ["1.18.31"]}, + {"name": "codex", "tested_versions": ["0.154.0"]} + ] +} +``` + +The array orders shown above are stable. `status` is the closed enum +`supported | unverified`; `scope` is `workspace`; `components` contains only +`filesystem_skill` in this Matter. The canonical asset also appears in the +manifest's existing `assets` array. The target's prefixed +`artifact.content_digest` is the public artifact identity; the legacy +`assets[].sha256` field may retain its existing unprefixed 64-hex encoding for +wire compatibility. + +Stage 2 emits `unverified`: discovery is pinned and the filesystem component +exists, but the full launch/send/observe suite has not passed. Stage 6 may +change it to `supported` only after all three pinned launchers pass the common +suite and the release matrix is sealed. Manifest status therefore cannot be +mistaken for an end-to-end support claim. + +The manifest declares tested launcher pins, not installed launcher presence +or live authority readiness. No hook, plugin, MCP component, terminal paste, +or launcher-specific operation appears in this target. + +## 6. Reachable local-authority preflight + +The common skill adds a phase zero before `session launch`: invoke + +```text +duo doctor --workspace [--config PATH] [--host KIND[:INSTANCE]] --output json +``` + +using the same workspace, config, and host override intended for launch. The +new optional `--config` and `--host` flags on doctor must share the launch +command's parsers and precedence; they are not a second selection algorithm. +If the caller omits them, doctor uses the same defaults as launch. + +Doctor evaluates independent checks where possible and marks a dependent +check `not_checked` when its prerequisite failed. It does not stop at the +first finding. A launcher proceeds to `duo session launch` only when +`launcher_preflight.status` is `ready`. + +### 6.1 Required checks + +| Ordered check ID | What must be proved | Failure action | +|---|---|---| +| `duo_executable` | The running executable resolves to a local path and reports non-empty version, commit, and build-date identity. `dev` is allowed when the commit is reported. | Run the intended installed Duo binary and record its identity. | +| `effective_config` | The exact config path launch would use loads and validates as `duo.config/v3`; report its deterministic effective digest and available preset names. | Install/fix v3 config or run the documented v2 migration; never edit it during doctor. | +| `authority_store` | The XDG-selected store path is locally addressable. A missing store is `initializable` and passes because the first real write owns creation. A present store must be readable, schema-compatible, replayable, and not held by another unexpired writer lease. | Fix path/schema/corruption, or wait for/stop the named writer through its normal lifecycle. | +| `workspace` | The selected path is absolute, exists, is a directory, and is the same root used for launch and skill lookup. | Pass the actual project root with `--workspace`; do not silently substitute another checkout. | +| `host_selection` | Existing M1 materialization deduces exactly one enabled host using explicit flag > workspace correlation > cwd correlation > ambient environment > policy default. | Supply `--host`, repair/rebind the workspace correlation, or correct host policy. | +| `host_reachability` | The selected host is reachable through a bounded, non-mutating adapter probe. For Herdr this is the existing ping path at the selected Unix socket. | Start/select the named Herdr server or make its socket reachable in this execution environment. | +| `skill_projection` | The selected workspace's copied projection is `current` for the manifest identity in section 2. | Run the installer/repair when allowed; move modified or unowned content before retrying. | + +Host compatibility evidence is reported separately from reachability. A +reachable Herdr with a pin/schema mismatch is `warning` information in this +preflight because the existing adapter admits `unverified`; a conformance +capture still cannot claim the pinned supported matrix unless Herdr version, +protocol, and schema digest match the accepted evidence. Provider standing +facts, scrub-gate survivors, active/recovering counts, launcher executable +pins, and unrelated runtime projections are informational unless another +contract makes them a launch refusal. + +### 6.2 Stable doctor JSON + +`doctor.run` remains the public operation. Its JSON report grows by one +top-level sibling; existing fields are not renamed: + +```json +{ + "launcher_preflight": { + "schema": "duo.launcher-preflight/v1", + "status": "ready", + "authority_scope": "local", + "checks": [ + { + "id": "duo_executable", + "stage": "executable", + "required": true, + "status": "pass", + "code": "ok", + "summary": "running Duo executable has a reportable build identity", + "action": "" + } + ], + "duo": { + "executable_path": "/absolute/path/to/duo", + "version": "", + "commit": "", + "build_date": "" + }, + "config": { + "path": "/absolute/path/to/duo.config.yaml", + "schema": "duo.config/v3", + "effective_digest": "sha256:<64 lowercase hex>", + "valid": true, + "presets": ["builder"] + }, + "authority": { + "path": "/absolute/path/to/duo.db", + "state": "ready", + "present": true, + "healthy": true, + "schema_version": 1, + "writer_active": false + }, + "workspace": { + "requested_path": "", + "selected_path": "/absolute/clean/path", + "source": "flag", + "exists": true, + "directory": true + }, + "host": { + "selected": true, + "kind": "herdr", + "instance_id": "herdr:", + "instance_label": "/path/to/herdr.sock", + "host_source": "workspace-correlation", + "reachable": true, + "detected_version": "0.8.2", + "protocol_identity": "herdr-socket-api/20", + "compatibility": "supported" + }, + "skill_projection": { + "target": "portable_launchers", + "name": "duo-delegation-loop", + "format_version": "duo.skill/duo-delegation-loop/v1", + "content_digest": "sha256:<64 lowercase hex>", + "root": "/workspace/.agents/skills/duo-delegation-loop", + "file": "/workspace/.agents/skills/duo-delegation-loop/SKILL.md", + "stamp": "/workspace/.agents/skills/duo-delegation-loop/.duo-generated.json", + "state": "current", + "installation_id": "" + } + } +} +``` + +All displayed fields are always present, using empty strings, `false`, zero, +or empty arrays when evidence is unavailable; consumers must not infer state +from omission. `checks` always uses the required-check order in section 6.1. + +Closed enums are: + +- overall `status`: `ready | not_ready`; +- `authority_scope`: `local | unavailable`; +- check `status`: `pass | fail | warning | not_checked`; +- check `id`: the seven IDs in section 6.1 followed by exactly one + informational check, `host_compatibility`; +- `stage`: `executable | config | authority | workspace | host_selection | + host_reachability | skill_projection`; +- authority `state`: `ready | initializable | writer_active | incompatible | + unhealthy | unavailable`; +- workspace `source`: `flag | cwd`; +- host `compatibility`: `supported | unverified | incompatible | unavailable | + unknown`; +- skill projection `state`: the six values in section 4. + +`checks` therefore contains exactly eight rows. `host_compatibility` is last, +has `stage: host_reachability` and `required: false`, and is `pass` only for +the pinned supported evidence, `warning` for a reachable unverified or +incompatible version, and `not_checked` when no host answered. Adding another +check ID requires a preflight schema-version change rather than silently +growing this closed list. + +`code` is `ok` on pass. Failures use stable dotted findings, including +`config.missing`, `config.invalid`, `authority.writer_active`, +`authority.unavailable`, `workspace.invalid`, `launch.host_unresolved`, +`host.unreachable`, `projection.missing`, `projection.stale`, +`projection.modified`, `projection.incompatible`, and +`projection.user_file_conflict`. `summary` and `action` are safe human text, +not parser inputs. An action names the failing stage, observed local resource, +and one concrete next step; it never includes credentials or raw environment +values. + +The effective config digest is SHA-256 over the deterministic JSON encoding +of the validated configuration value launch consumes, with a `sha256:` +prefix. Preset names are sorted. This is an observation of effective intent, +not a copy of config secrets. + +### 6.3 Human output + +Human mode contains a `launcher preflight: ready` or +`launcher preflight: not ready` heading, followed by the seven checks in the +same order. Each line begins `[pass]`, `[fail]`, `[warning]`, or +`[not checked]`, then the check ID and summary. Every non-pass required check +has an indented `action:` line. The projection line prints the state and full +artifact identity. JSON fields and enums are the stable interface; sentence +wording may improve without a schema change. + +### 6.4 Exit and refusal semantics + +`duo doctor` preserves its diagnostic command semantics: exit `0` means a +complete report was produced, not that every check passed. Usage remains `2`, +and an internal failure that prevents a trustworthy report remains `4`. +Callers gate on `launcher_preflight.status`, not process exit alone. + +When status is `not_ready`, the common skill refuses before launch and reports +the failed stage(s) and actions. It does not invoke `session launch`, create an +authority store, install/repair the skill, switch workspace, select a remote +authority, enable a plugin or MCP route, or paste into a terminal. + +“Local” is established by reachable resources, not by vendor-environment +heuristics. Duo does not attempt to recognize every orb/container product by +environment variable. An orb or remote execution context without the selected +workspace, effective config/store paths, current copied projection, and +reachable selected host reports `authority_scope: unavailable` and +`not_ready`. An isolated environment in which those exact resources are +deliberately mounted and the local Herdr socket answers is operationally local +and may pass. There is no network fallback to an authority on another host. + +### 6.5 No-write diagnosis + +Doctor and every helper it calls for this preflight have zero persistent +mutation. In particular they must not: + +- create or migrate the authority database; +- acquire even a transient authority-writer lease; +- enroll or bind a workspace; +- reap harness directories; +- create a target directory, stamp, or skill file; +- repair config or projection drift; or +- change Herdr or launcher state. + +Stage 2 must use a genuinely read-only store/schema/lease inspection path and +replace the current doctor-time harness sweep with reporting only. A bounded +Herdr ping and schema/version query are non-mutating protocol reads and are +allowed. Install and cleanup remain explicit write operations. + +## 7. Compatibility and migration rules + +1. **Current manual installs are unowned.** An unstamped copied `SKILL.md` or + source-tree symlink is `unowned_conflict`, even if its bytes match the + canonical digest. Duo never silently adopts it. The operator moves/removes + it and then runs `duo install portable-launchers`. +2. **Bootstrap stamps do not transfer ownership.** A + `.duo-manifest-stamp.json` from `internal/manifest.Stamp` is not a + `duo.projection-stamp/v1` record and cannot authorize overwrite. At the + portable target it is unrelated or conflicting user content. +3. **Known older output migrates by repair.** A valid, recognized older + projection stamp whose claimed files still match is `stale` and may be + regenerated with `--repair`. The installer preserves `installation_id` and + writes the new stamp last. +4. **Unknown/newer formats never downgrade.** An old Duo binary seeing an + unknown projection format reports `incompatible` and writes nothing. +5. **Product upgrades are digest-driven.** A changed product/manifest alone + makes an intact owned projection `stale`; a changed canonical skill digest + also makes it `stale`. Repair never needs a launcher-specific migration. +6. **Launcher support is evidence-pinned.** The three exact versions in this + document are the initial supported matrix. A newer launcher is not assumed + compatible merely because it still finds `.agents/skills`; re-run live + discovery and the launcher-neutral suite, then append the exact version to + `tested_versions` without changing the skill format unless its consumed + contract changed. +7. **The manual-install prose must converge.** When Stage 2 lands the + installer, the normative skill and operator guidance must stop claiming + that no renderer exists. Hand-managed Claude Code/Cursor instructions are + not silently converted and remain outside this three-launcher target. +8. **No Windows support is inferred.** Copy installation avoids dependence on + symlink support, but the accepted discovery evidence and relative-link + confirmation are Linux-only. A Windows support claim needs its own live + evidence; it does not change the safe copy/stamp rules. + +## 8. Stage 2 acceptance consequences + +Implementation is conformant only if isolated-root tests cover all six +projection states, state precedence, fresh install, idempotent current +install, allowed missing/stale repair, refusal without byte changes for +modified/incompatible/unowned conflicts, unsafe stamp paths, symlink +destinations, and crash-safe stamp-last placement. Manifest tests pin the +single target, its initial `unverified` status, and artifact identity. Doctor +tests pin every JSON field and enum, success and failure human sections, a +negative Herdr reachability case, and snapshots proving diagnosis creates, +modifies, and removes no file. + +The live evidence for each launcher must cite the manifest target identity, +doctor-reported skill identity, exact launcher binary/version, Duo build, +Herdr compatibility evidence, selected workspace, and a `ready` preflight. +Discovery alone is not authority readiness, and authority readiness alone is +not end-to-end launcher conformance. + +## 9. Conservative assumptions and open evidence + +- Exact launcher versions are deliberately pins rather than semantic ranges; + there is not enough live evidence to claim wider ranges. +- The current skill digest is retained as a baseline, while allowing Stage 2 + to update obsolete hand-install wording. The post-change digest becomes the + identity everywhere in one commit. +- Missing authority state is treated as initializable because Duo currently + has no separate initialization verb. This does not promise that a later + write cannot fail; it says diagnosis observed no existing corrupt or locked + authority. +- Duplicate skill-name precedence, launcher hot reload, Windows discovery, + and user-global installation remain unproved and outside this contract. +- No cross-repo assumption is invalidated. This contract consumes and narrows + the 2026-09-17 duo-lab assessment: it turns the confirmed shared discovery + path into a product-owned copied projection and leaves runtime research in + `duo-lab`. diff --git a/docs/vnext/duo-portable-launcher-conformance-suite.md b/docs/vnext/duo-portable-launcher-conformance-suite.md new file mode 100644 index 0000000..dd057ee --- /dev/null +++ b/docs/vnext/duo-portable-launcher-conformance-suite.md @@ -0,0 +1,797 @@ +# Portable launcher conformance suite and supported builder fixture + +> Status: **Stage 2 offline suite implemented; live launcher evidence remains +> for Stages 3–5.** The blocked case has an explicit prerequisite in section 8 +> and cannot be reported as passing today. + +This suite tests Amp, OpenCode, and Codex as **outer launchers** of the same +Duo CLI workflow. It does not use any of them as the launched agent runtime. +All three receive the same task, use the same projected +`duo-delegation-loop` skill, and operate one identically constructed local +fixture. A launcher-specific driver may start its launcher and preserve its +events; it may not know how Duo launches, binds, sends, observes, retries, or +recovers. + +The selected inner pair is the supported **Pi 0.83.0 runtime adapter on Herdr +0.8.2**. OpenCode is not selected: its adapter is intentionally not available +through the production CLI lookup and its current work is read-only. Amp is +not selected: its runtime adapter does not provide conversation or condition +observation. Devin remains a candidate runtime. Claude Code is a supported +alternative, but Pi has the stronger fixture boundary for this suite: an +exact version probe, a versioned transcript format, a Duo-materialized native +prompt socket, and `RuntimeReadyProvider`. + +## 1. Existing product support and exact pins + +### 1.1 Accepted fixture matrix + +The following values are exact, not minimum versions or compatible ranges. +A live capture with any other value is ineligible, even if an adapter can run +it as `unverified`. + +| Component | Required identity | Source in this checkout | +|---|---|---| +| Duo source/build | one per-run exact 40-character lowercase commit, exact version/build date, and executable SHA-256; setup artifact, recorder executable, setup evidence, and result pin must all agree | captured checkout, `internal/buildinfo`, and the validated command journals | +| Public Duo wire | `duo.external/v1`; schema SHA-256 `8b055ff60f0ca5f3da46302671591aa0dae61185cd9f1f15fcceb36d841e7497` | `contracts/schemas/duo-external-v1.schema.json` | +| Effective config | `duo.config/v3`; schema SHA-256 `d1a2fd1be5339c5699a21cf65616a4d09ff932a5495111d02a422b7656086842` | `contracts/schemas/duo-config-v3.schema.json` | +| Authority store | schema version `1` | `internal/store/migrations.go` and `internal/store/store.go` | +| Runtime | Pi `0.83.0`; adapter `pi`, build `stage1`, conformance record `pi-0.83.0-2026-08-23` | `internal/runtime/pi/pi.go` | +| Runtime semantic format | `pi-session-jsonl/v3` (header `version: 3`) | `internal/runtime/pi/pi.go` and `internal/runtime/pi/transcript.go` | +| Runtime delivery asset | `duo-inject.ts`, SHA-256 `2708a3435821fb2e72302ee56d8a07fe62c265214ae8fd342e385c03ef245a23` | `internal/runtime/pi/inject/duo-inject.ts` and `internal/runtime/pi/inject.go` | +| Host | Herdr `0.8.2`, socket protocol `20`, API-schema SHA-256 `c48f1f54ee0150ca27e11fd44455fe94aeadb20fdf4e4a62393ed822a4e5b150` | `internal/host/herdr/factory.go` | +| Host adapter | adapter `herdr`, build `stage1`, conformance record `notes/19-herdr-probes.md@2026-08-23` | `internal/host/herdr/factory.go` | +| Skill baseline | `duo.skill/duo-delegation-loop/v1@sha256:6f8bc16b656f564a949c99f12d4cf0bcd04ec2c9b49a8b74fb65b2e355b5d182` | `skills/duo-delegation-loop/SKILL.md` and the companion installation contract | +| Inner model selection | provider `openai-codex`, model line `gpt-5.6-luna`; this is captured as fixture input, not represented as an adapter compatibility claim | existing v3 dogfood configuration in `evidence/dogfood/2026-08-24/duo.config.yaml` | +| Outer launchers | Amp `0.0.1789675234-g2899fe`; OpenCode `1.18.31`; Codex CLI `0.154.0`, with the executable digests from Step 01 | `evidence/portable-launcher-conformance/step-01/probe-summary.json` | + +The skill digest is a baseline. If Stage 2 changes its obsolete hand-install +wording, the installed projection, manifest, doctor output, scenario +manifest, and capture must all use the new digest as required by +`duo-portable-launcher-conformance-contract.md`. A capture must never silently +substitute the baseline digest. + +The runtime is a supported product fixture rather than research-only +behavior: + +- `stage1Support.Supported` in `internal/cli/session_launch.go` accepts the + `(herdr, 0.8.2, pi)` tuple. +- `internal/runtime/pi.Runtime` implements `RuntimeCorrelator`, + `ConversationProvider`, `ConditionProvider`, `RuntimePromptProvider`, and + `RuntimeReadyProvider`. +- every Pi launch goes through `stage1LeafAugmenter`, which materializes the + embedded `duo-inject.ts` and passes it with `-e`; prompt delivery is a + native Unix-socket user turn, not terminal paste. +- the parser refuses a transcript whose header is not schema version 3, and + the repository contains scrubbed, captured Pi 0.83.0 transcripts under + `internal/runtime/pi/testdata/`. +- the Herdr adapter implements launch, attachment validation, lifecycle, + binding identity, and prompt fallback against its pinned protocol. The live + adapter test requires a disposable server and closes the pane it creates + (`internal/host/herdr/live_test.go`). + +The permanent fake adapters are valuable offline test infrastructure, but +they are not this live fixture. `internal/host/fake` and +`internal/runtime/fake` are registered in memory by tests and are not the +supported production launch tuple. A capture obtained with either fake must +be rejected. + +### 1.2 Current-host mismatch is a prerequisite, not permission to widen support + +The local observations made while authoring this document on 2026-09-17 are: + +| Component | Installed observation | Fixture verdict | +|---|---|---| +| Pi | `0.84.4` | unverified; not the required `0.83.0` | +| Herdr | `0.9.0`, protocol `22`, API-schema SHA-256 `226d4ecbd128d2e6bc84e4c8ddcec21ba9c7e51a0aafffcf087111ead3f1fa9a` | unverified; not the required `0.8.2`/`20`/`c48f…b150` | +| Claude Code | `2.1.274` | outside its adapter's literal `2.1.240`, `2.1.241` set; not a fallback fixture | +| checked-out Duo | must be captured as an exact full commit for the run | eligible only when it matches the built and recorded executable identity | +| `duo` on PATH | not implicitly trusted | eligible only when copied into the fixture and bound to the same per-run version, commit, build date, and digest | + +Stage 2 therefore needs locally installed, digest-pinned Pi 0.83.0 and Herdr +0.8.2 artifacts and a Duo binary built from the captured source commit. It +must not relabel the installed newer binaries as supported, download an +unpinned latest version, or fall back to Claude, fake, Amp, Devin, or +OpenCode. The artifact source and SHA-256 become additional capture pins; +this document cannot truthfully invent those binary digests before the exact +artifacts are provisioned. + +## 2. Disposable local fixture + +### 2.1 Roots and deterministic content + +The common fixture controller creates a mode-`0700` run root with a random +unpredictable pathname. Paths in evidence are rewritten to the stable tokens +below; the random host pathname is never an oracle input. + +```text +$RUN/ +├── home/ # HOME +├── xdg/ +│ ├── config/ # XDG_CONFIG_HOME +│ ├── data/ # XDG_DATA_HOME; Duo store + harnesses +│ ├── state/ # XDG_STATE_HOME +│ ├── cache/ # XDG_CACHE_HOME +│ └── runtime/ # XDG_RUNTIME_DIR; Pi inject sockets +├── bin/ # pinned duo, herdr, pi and an allowlisted PATH +├── secrets/ # 0700, omitted from evidence +├── herdr/ # config, session state, socket and logs +├── workspace/ # local Git worktree and projected skill +├── capture/ # raw local events before scrub +└── result/ # scrubbed export staging before atomic copy-out +``` + +`PI_CODING_AGENT_DIR`, `PI_CODING_AGENT_SESSION_DIR`, `HERDR_CONFIG_PATH`, +and every Duo/config/store/harness path resolve below `$RUN`. The fixture +passes the explicit workspace and explicit Herdr socket to preflight and +launch; it does not allow ambient host deduction to select a user's server. +`PATH` resolves the pinned fixture binaries before a small immutable system +allowlist. The controller fails setup if any resolved executable escapes +`$RUN/bin` (apart from that declared system allowlist). + +The workspace is an initialized local Git repository with no remote. Its +fixture-owned files are byte-for-byte deterministic: + +```text +fixture/request.txt: +fixture_id=portable-launcher-v1 +expected_reply=DUO_PORTABLE_LAUNCHER_OK_V1 +``` + +The file, including its final newline, has SHA-256 +`b0a32f59630d6398da8d8913744dd7cd50a313d86bf7e833309aad0d47cc9c3b`. +The expected assistant text is exactly `DUO_PORTABLE_LAUNCHER_OK_V1` (SHA-256 +`b3300f2f6e0338d2b672891be66827d95779a2eabf4a75dcae1b6f56c9a4dc98`). + +The fixture config has one enabled host kind, one runtime, one variant, and +one `builder` preset with one `main` leaf. It selects Pi, the provider/model +row in section 1.1, close-on-exit, and an explicit Herdr host. The canonical +prompt is the exact UTF-8 string: + +```text +Read fixture/request.txt and reply with exactly the value of expected_reply, with no surrounding text. +``` + +Its prompt-only digest, matching `promptCanonicalDigest` in +`internal/cli/prompt.go`, is +`sha256:b40d3a68eb33083b92199d734da2dece28395a6acc6d5e2cb9d7032dae4d11c7`. +The fixed idempotency key is `portable-launcher-v1-primary`. Because every +run has a fresh authority store, fixed keys cannot collide across runs. +Product-minted IDs (`ses_…`, runtime instance, command, attempt, attachment, +and launch-resolution IDs) remain opaque and nondeterministic; the oracle +compares their relationships, never hard-codes their values. + +The conflict text is exactly `This text must conflict and must never be +delivered.` Its digest is +`sha256:dbe904a42a9d9d9cdf7908508fa393ce43c9c53ea16a8cb8c9b82e7005f602bc`. + +### 2.2 Credentials and extension boundary + +The suite does not own credentials. Before setup, an operator or CI secret +provider stages only the chosen inner provider's minimum credential material +into `$RUN/secrets` or supplies the corresponding secret environment value. +The controller copies it mechanically into the isolated Pi config with +directory mode `0700` and file mode `0600`. It records only +`credentials_present: true`, the provider name, and a non-secret source kind +(`ci_secret`, `operator_copy`, or `environment`); it never records a value, +token fingerprint, source pathname, or auth-file bytes. Missing credentials +fail `preflight`; they do not select another provider. + +Pi starts with `--no-extensions`; the pinned 0.83.0 semantics for that flag +and the explicit Duo-owned `-e ` exception are recorded in the +embedded inject asset itself. Its isolated roots contain no other skills, +prompt templates, context files, themes, or project packages. Stage 2 may add +the corresponding disable flags only after the pinned binary's captured help +confirms them. No other Pi extension is allowed. The outer launchers use their +isolated, plugin-free options from the Step 01 pattern; their temporary auth +files are treated by the same secret rule. No MCP server is configured or +reachable. A capture records `plugins_enabled: false`, `mcp_enabled: false`, +and the exact outer arguments. + +### 2.3 Host lifecycle + +The controller, not an outer launcher, starts one clean-environment Herdr +0.8.2 server whose socket and persistence are below `$RUN/herdr`. It verifies +ping version `0.8.2`, protocol `20`, and a fresh `herdr api schema --json` +digest before making the server available. The server environment is built +from an allowlist and is checked by the existing scrub policy so outer-agent +markers such as `CLAUDECODE`, `CLAUDE_CODE_CHILD_SESSION`, `CLAUDE_CODE_ENTRYPOINT`, +and `AI_AGENT` cannot leak into launched panes. This is required because +Herdr panes inherit the server environment (`internal/host/herdr/doc.go` and +`internal/scrub`). + +Startup is complete only after the socket answers the pinned probe and its +snapshot is empty. Cleanup addresses objects by IDs captured from Duo/Herdr, +closes every fixture pane, stops only this Herdr server through its normal +local control surface, waits for its PID and socket to disappear, and then +removes `$RUN`. It never runs a broad `pkill`, follows a path outside +`$RUN`, contacts a user Herdr socket, or deletes an unrecognized object. + +Before deletion the controller checks that no fixture PID remains, no fixture +socket remains, the Herdr snapshot has no fixture pane, and all files are +under `$RUN`. It atomically copies the scrubbed result and referenced blobs to +the caller-selected evidence directory outside `$RUN`, then removes `$RUN`. +Cleanup failure is retained as a failed `cleanup` stage; it is not hidden by a +shell trap's exit status. A best-effort emergency cleanup may run after +recording that failure, but may not rewrite it as success. + +## 3. Ownership: one scenario and thin launcher drivers + +The canonical scenario is `portable-launcher-delegation/v1`. Its task text, +fixture manifest, state machine, executable action instructions, deadlines, +assertions, and oracle live in the common suite. Every launcher receives the +same bytes. Each stage names its executor, operation, argv template, allowed +auxiliary polling operations, and invocation cardinality. The task tells the +outer agent only to load the shared `duo-delegation-loop` skill and execute +the canonical scenario manifest in order. It does not ask the launcher to +author a result or verdict; the common collector is the sole final-result +authority. + +A per-launcher driver may do exactly four things: + +1. start the pinned outer launcher in `$RUN/workspace` with its isolated + HOME/config and plugin/MCP-disabled flags; +2. make the already installed canonical project skill visible through + `.agents/skills/duo-delegation-loop` (the common installer owns its bytes); +3. submit the exact common task bytes once; and +4. preserve the launcher's raw event stream and stderr. + +It may not construct Duo commands, parse Duo IDs for the launcher, poll, +retry, induce fixture states, alter deadlines, add hints after a failure, +paste into a terminal, call Herdr, author or edit a result, or translate a +launcher-specific success into a suite pass. Launcher-native event envelopes +may differ, but all semantic evidence and the authoritative result are +assembled by the common collector. + +The fixture controller is separate from every driver. It may perform declared +fault induction (sections 7–8) only after a durable checkpoint is observed. +It cannot answer the model, issue a replacement Duo command, or repair a +missed step. The oracle reads captured Duo envelopes, the read-only authority +projection, process/host evidence, and controller checkpoints. The launcher's +self-reported `pass` field is never trusted. + +## 4. Versioned result contract + +Stage 2 must add a JSON Schema whose identity is +`duo.portable-launcher-conformance-result/v1`. The following is the normative +shape; prose fields may be added only by a v2 schema, not opportunistically. + +```json +{ + "schema": "duo.portable-launcher-conformance-result/v1", + "suite": { + "name": "portable-launcher-delegation", + "revision": 1, + "manifest_digest": "sha256:<64 lowercase hex>", + "oracle_digest": "sha256:<64 lowercase hex>" + }, + "run": { + "run_id": "", + "observed_at": "", + "host_os": "linux", + "host_arch": "x86_64", + "fixture_root": "$RUN", + "plugins_enabled": false, + "mcp_enabled": false, + "terminal_input_used": false + }, + "pins": { + "launcher": {"name": "amp|opencode|codex", "version": "", "executable_sha256": "sha256:"}, + "duo": {"version": "", "commit": "", "build_date": "", "executable_sha256": "sha256:"}, + "skill": {"name": "duo-delegation-loop", "format_version": "duo.skill/duo-delegation-loop/v1", "content_digest": "sha256:", "installation_id": ""}, + "config": {"schema": "duo.config/v3", "effective_digest": "sha256:"}, + "authority": {"schema_version": 1, "store_digest_before": "sha256:", "store_digest_after": "sha256:"}, + "host": {"name": "herdr", "version": "0.8.2", "protocol": "herdr-socket-api/20", "schema_digest": "sha256:c48f1f54ee0150ca27e11fd44455fe94aeadb20fdf4e4a62393ed822a4e5b150", "executable_sha256": "sha256:"}, + "runtime": {"name": "pi", "version": "0.83.0", "format": "pi-session-jsonl/v3", "adapter_build": "stage1", "conformance_record": "pi-0.83.0-2026-08-23", "executable_sha256": "sha256:", "delivery_asset_sha256": "sha256:2708a3435821fb2e72302ee56d8a07fe62c265214ae8fd342e385c03ef245a23"}, + "model": {"provider": "openai-codex", "model_line": "gpt-5.6-luna"}, + "external_schema": {"identity": "duo.external/v1", "digest": "sha256:8b055ff60f0ca5f3da46302671591aa0dae61185cd9f1f15fcceb36d841e7497"} + }, + "stages": [ + { + "sequence": 1, + "stage": "setup", + "case": "run", + "verdict": "pass", + "outcome": "success", + "started_offset_ms": 0, + "duration_ms": 1, + "assertions": [{"id": "", "expected": {}, "actual": {}, "matched": true}], + "evidence": ["blob:sha256:"], + "error": null + } + ], + "summary": {"verdict": "pass", "first_failed_stage": null, "first_failed_case": null}, + "scrub": {"status": "pass", "policy": "portable-launcher-evidence/v1", "findings": []} +} +``` + +The closed stage enum is: + +```text +setup | discovery | preflight | launch | bind | send | observe | +command_inspection | restart | cleanup +``` + +The closed case enum is: + +```text +run | happy | blocked | exited | timeout | restart | +same_key_same_text | same_key_different_text +``` + +The required stage/case matrix is also closed: + +| Case | Required stages in scenario order | +|---|---| +| `run` | `setup`, `discovery`, `preflight`, `cleanup` | +| `happy` | `launch`, `bind`, `send`, `observe`, `command_inspection` | +| `restart` | `restart`, `bind`, `observe`, `command_inspection` | +| `same_key_same_text` | `send`, `command_inspection` | +| `same_key_different_text` | `send`, `command_inspection` | +| `blocked` | `launch`, `bind`, `send`, `observe`, `command_inspection` | +| `exited` | `launch`, `bind`, `send`, `observe`, `command_inspection` | +| `timeout` | `launch`, `bind`, `send`, `observe`, `command_inspection` | + +The manifest fixes the global interleaving shown in section 5. If an earlier +failure prevents execution, the common collector still emits each remaining +required record as `verdict: fail`, `outcome: error` with +`prerequisite.not_reached`. Such a record references the originating failure +checkpoint and has no fabricated operation assertion. This makes a failed +capture complete and stage-addressable without adding a `skipped` escape +hatch. + +`verdict` is `pass | fail`. `outcome` is +`success | blocked | exited | timeout | error`. An expected blocked, exited, +or timeout observation has `verdict: pass` only when the oracle independently +confirms that exact outcome. It is not encoded as a failed process. Every +failed stage requires `error` with `code`, safe `message`, `effect`, and +`retry`; every passing stage has `error: null`. Overall pass requires all +required stage/case pairs, so there is no `skipped`, `unsupported`, or +`assumed_pass` value. + +`assertions[].id` is a closed list owned by the oracle revision. `expected` +comes from the signed scenario manifest; `actual` comes from evidence. A +launcher-authored expected value is invalid. Opaque IDs may appear in +`actual`, but assertions compare referential equality across records. + +Each referenced blob is content-addressed and listed in a sidecar index with +media type, byte count, SHA-256, scrub status, and producer. The capture keeps +normalized Duo envelopes, selected fixture-owned conversation records, +launcher tool events, controller checkpoints, and process identities. It +does not keep raw vendor transcripts, reasoning, auth files, environment +dumps, Herdr screen contents, or arbitrary launcher state. + +Offline validation requires all pin fields above, the exact outer executable +identity from Step 01, the post-Stage-2 skill and manifest identities, every +required stage, monotonically increasing `sequence`, valid timing fields, +blob hashes, schema-valid `duo.external/v1` envelopes, and a passing scrub +record. Absolute fixture paths in blobs are rewritten to `$RUN`, `$HOME`, +and `$WORKSPACE`; the rewrite map itself is not retained. + +## 5. Canonical scenario body + +The common scenario executes this order. Drivers cannot omit or reorder it. + +1. **Setup:** create the isolated roots, deterministic workspace/config, + copied stamped skill, empty authority, pinned binary PATH, credential + boundary, and clean Herdr server. +2. **Discovery:** prove the launcher reports the projected skill's name, + source, format, and digest through launcher evidence, not merely filesystem + existence. +3. **Preflight:** run the Stage 2 `duo doctor ... --output json` contract with + the exact workspace/config/host and require `launcher_preflight.status == + ready`, all seven required checks passing, and host compatibility + supported. +4. **Happy launch/bind:** run `duo session launch builder --require + agent_runtime=pi --workspace --host herdr: + --output json`; capture the session ID, then show/list until bind evidence + is present within the bind boundary. +5. **Happy send/observe/inspect:** send the canonical prompt and key, observe + the exact assistant text, and inspect the command. +6. **Authority restart:** end the process that executed the successful send, + open the same store in a new Duo process, explicitly reconcile the still + live attachment, and re-read session, conversation, and command state. +7. **Idempotency pair:** in the new Duo process, resend the same key/text, + then the same key with the conflict text; inspect after each. +8. **Blocked case:** run the blocked induction and assertions in section 8.1. + Today this deterministically fails at its prerequisite checkpoint; no + launcher can pass the complete suite until that prerequisite exists. +9. **Exited case:** launch a fresh session, suspend its exact process before + send, send a fixture prompt, induce host-proved exit after the durable + delivery checkpoint, reconcile, and observe exited. +10. **Timeout case:** launch/bind a fresh session, suspend the exact Pi process + under controller verification, durably send, and prove the 20-second + assistant observation timeout without changing command delivery. +11. **Cleanup:** close all fixture panes and processes, stop the fixture + server, scrub/export evidence, and remove the run root. + +The outer agent invokes each Duo poll as a distinct command, approximately +once per second, exactly as the normative skill requires. The capture rejects +`duo wait`, `--block-for`, `seq`/shell polling loops, hidden stderr, terminal +paste, stop/interrupt, or a `duo command` family. `command.inspect` must be +reached through `duo prompt show`. + +## 6. Independently derived assertions + +The oracle below is generated from fixture constants and product contracts +before the outer launcher starts. Neither launcher instructions nor model +output can redefine an expectation. + +### 6.1 Launch and bind + +For each launched case: + +- the launch envelope has `schema: duo.external/v1`, operation + `session.launch`, a non-empty new `session_id` and `launch_resolution_id`, + ordered selection, exactly one selected `main` leaf, `agent_runtime: pi`, + the pinned model line, host kind `herdr`, the fixture socket identity, and + no warning that changes support; +- no pre-launch prompt exists and the captured argv has no `--prompt`; +- the session and launch-resolution facts precede the host spawn, and the + attachment refers to the one fixture pane; +- bind produces exactly one current runtime instance in state `live`, one + active `agent.session` correlation for Pi, a transcript locator below the + isolated Pi sessions root, and one claimed attachment with non-zero PID and + start time; +- `session.inspect` names the same session/runtime IDs, lifecycle `active`, + exposes `prompt.deliver` and `conversation.list` as `available`, and never + claims blocked or exited before their induction. A fresh CLI process may + show the derived view `recovering` until `session.reconcile`; the oracle + therefore requires the explicit same-live reconcile rather than falsely + equating `view` with durable instance state. + +Failure is attributed to `launch` until a successful `session.launch` +envelope exists, then to `bind` until all bind assertions hold. Filesystem +discovery cannot satisfy either stage. + +### 6.2 Durable send + +The first send must return operation `prompt.deliver` with: + +- one non-empty `command_id`, target session/runtime equal to the bound IDs; +- `responsibility_state: delivered`, `queue_policy: queue_until_safe`; +- `activity_observed: false`, `acknowledged: false`; +- `retry: {safe:false, action:"observe_existing_command"}`; +- no `effect` field on the success envelope. Current `duo.external/v1` + defines effect certainty for unsuccessful attempts; inventing + `effect: delivered` is invalid. + +The durable authority record must have the independently calculated prompt +digest, fixed key, finite expiry, and exactly one attempt with path kind +`runtime`. The Pi socket path and captured extension digest must match the +bound session. `command.inspect` must report `delivered`, revision at least +`3`, one attempt with `realization: native` and `recorded_result: delivered`, +non-empty accepted/delivered times, false activity/acknowledgment, and the +same retry advice. This is the effect assertion: one native complete-turn +delivery attempt, not merely “send did not crash.” + +### 6.3 Assistant text observation + +Starting from the pre-send conversation baseline, exactly one newly observed +assistant text block must equal `DUO_PORTABLE_LAUNCHER_OK_V1`. User or `peer` +records cannot satisfy the assertion. Its record names the same current +runtime instance and is source-complete. Extra assistant prose, a terminal +snapshot, launcher final text, or a copied expected value outside +`conversation.list` fails `observe`. + +The runtime may write internal thinking/tool records, but only semantic +`conversation.list` assistant text is evidence. Raw transcript content is +not exported. + +### 6.4 Blocked + +Once the prerequisite in section 8.1 exists, the blocked case requires: + +- the same bound runtime instance remains nonterminal and its condition is + exactly `blocked` with non-empty determining evidence and fresh or + explicitly bounded freshness; +- polling stops on blocked before the 20-second timeout and no expected + assistant text appears after the case baseline; +- if the case prompt was already admitted, its command remains `delivered` + with exactly one native attempt and retry + `{safe:false, action:"observe_existing_command"}`. Blocked does not rewrite + transport delivery or fabricate `no_effect`; +- if induction is proved before any send, no command may exist. The scenario + manifest must select one of these two shapes; it may not accept either at + runtime. + +For v1 the selected shape is **admitted-then-blocked**. Until supported +evidence can force that edge, the stage is `verdict: fail`, `outcome: error`, +code `prerequisite.blocked_induction_unavailable`, effect `no_effect`, retry +`{safe:false, action:"add_supported_blocked_evidence"}`. + +### 6.5 Exited + +The controller first suspends the exact process-birth PID and verifies the +stopped state, preventing a model response from racing the lifecycle edge. +After it sees the exited-case command durably `delivered`, it closes that +case's exact pane through Herdr's `pane.close` API and invokes `duo session +reconcile --output json`. Assertions are: + +- controller target terminal ID/PID/start time equals the session attachment; +- reconcile reports `exited` for the same runtime-instance ID; +- subsequent inspect keeps lifecycle `active`, has derived view `inactive`, + runtime instance state `exited`, and condition `exited` with reported/fresh + evidence; +- the already delivered command remains delivered, one attempt, native, + `recorded_result: delivered`, with retry + `{safe:false, action:"observe_existing_command"}`; exit cannot erase or + replay its possible effect; +- no assistant success is required after the case baseline, and polling + stops on exited rather than timing out. + +The controller's Herdr call is declared fault induction, not a driver repair. +No keys, text, shell command, stop/interrupt Duo verb, or terminal bytes are +sent to the runtime. + +### 6.6 Timeout + +Timeout is the skill's **assistant observation timeout**, not process timeout +or command expiry. On a fresh bound Pi session, the controller sends `SIGSTOP` +to the exact process-birth PID and verifies Linux reports it stopped before +the outer agent sends. The already listening Unix socket remains the supported +Pi native delivery boundary: the command must still become delivered with one +native attempt and the standard observe-existing retry advice. The frozen +process cannot create an assistant turn. + +The observer must run for at least 20,000 ms and no more than 22,500 ms on a +monotonic clock, with roughly one show/list pair per second. It passes with +`outcome: timeout` only when no new assistant text exists, session/runtime IDs +remain unchanged, condition is not falsely reported blocked/exited, and +command inspection remains delivered/one-attempt. The launcher process must +remain successful; an outer timeout, killed shell, command expiry, or CLI +nonzero exit is not this outcome. + +The signal is sent by the common fixture controller, never by a launcher or +inside the pane. It changes no adapter, transcript, socket protocol, or +runtime bytes. Cleanup closes the disposable pane while the process is +stopped (and escalates only to that exact captured PID if the normal pane +close cannot finish). It must not resume the queued prompt into a shared or +production runtime. + +### 6.7 Authority restart + +“Restart” in this suite means an **authority-process restart**, not an outer +launcher restart, Herdr restart, or Pi restart. Duo is a command-per-process +CLI: the process that executed the first successful `prompt send` exits and +releases its store lease; a separately exec'd Duo process opens the same +SQLite file and runs `session reconcile`, then read operations. The capture +proves distinct OS PIDs and one unchanged store inode/path token. + +The durable Duo session ID, runtime-instance ID (after same-live host +validation), command ID, idempotency key/digest record, attachment claim, +conversation correlation, and one delivery attempt survive. Authority +incarnation and process PID do not. Required recovered values are: + +- reconcile outcome `same_live` for the original runtime instance; +- the same assistant record/text remains readable; +- `prompt show` returns the same delivered command, revision, target, attempt + ID/count/result, milestones, and retry advice; +- no adapter call and no new user or assistant turn occurs merely because the + new authority opened the store. + +The forbidden replay is any second delivery attempt or second inner user turn +for the primary prompt. Restart must not create a replacement runtime +instance, because Herdr proves the original process birth is still live. + +### 6.8 Same key and same text + +After restart, resending the fixed key with the canonical text must return +the original command ID, delivered state, unchanged target and attempt list, +and retry `{safe:false, action:"observe_existing_command"}`. The success +envelope again has no `effect` member. The oracle compares pre/post authority +facts and semantic turns: command count, attempt count, primary user-turn +count, and matching assistant-turn count must not increase. + +### 6.9 Same key and different text + +Sending the fixed key with the conflict text must exit as a schema-valid +`prompt.deliver` failure: + +```text +class: conflict +code: command.idempotency_conflict +effect: no_effect +retry: {safe:false, action:"use_new_idempotency_key"} +target: {kind:"prompt_command", id:} +``` + +`details.idempotency_key` equals the fixed key; `existing_digest` equals +`sha256:b40d3a68eb33083b92199d734da2dece28395a6acc6d5e2cb9d7032dae4d11c7`; +`request_digest` equals +`sha256:dbe904a42a9d9d9cdf7908508fa393ce43c9c53ea16a8cb8c9b82e7005f602bc`. +The original command remains delivered with one attempt. No new command, +attempt, queue entry, inner user turn, or assistant turn may appear. + +## 7. State induction rules + +Only the common fixture controller may induce a state, and only against IDs +derived from the fixture's durable evidence. Its control log is part of the +capture. The allowed v1 controls are: + +| State | Induction | Why it preserves the support claim | +|---|---|---| +| exited | Verified `SIGSTOP`, then Herdr 0.8.2 `pane.close` for the exact disposable attachment after durable delivery, followed by public `session.reconcile` | Suspension removes the response race; host lifecycle and continuity validation are implemented supported surfaces, and no runtime input is synthesized. | +| timeout | Linux `SIGSTOP` for the exact Pi process birth, verified before send; observe for 20 seconds; close the pane during cleanup | The pinned Pi binary, transcript parser, native delivery socket, and Herdr attachment remain unchanged. The fault is scheduler suspension, not a fake adapter or random model timing. | +| blocked | unavailable today; section 8.1 | Pi 0.83.0 has no permission system or blocked producer, so a fabricated event would weaken the claim. | + +No induction may use PTY writes, `pane.send_text`, `pane.send_keys`, shell +paste, slash commands, model persuasion, sleep-based races, altered +transcripts, fake sockets, proxy responses, plugins, MCP, an outer-launcher +API, or another runtime adapter. A checkpoint is event-driven (durable state +or verified process state), never “sleep N and hope.” + +## 8. Known prerequisites and conservative failures + +### 8.1 Blocked is not inducible on the selected supported runtime + +`internal/runtime/pi/doc.go` explicitly records that Pi 0.83.0 has no +permission system and no blocked-family producer. Its only known convention, +`herdr:blocked`, had one listener and zero emitters in the pinned sweep. +`internal/runtime/pi/condition.go` consequently emits idle, working, or +unknown, never blocked. Although `duo.external/v1` and +`runtime.ConditionBlocked` include the value, vocabulary is not evidence. +Herdr's `agent_blocked` prompt refusal is an adapter no-effect mapping, not a +Duo session-condition observation, and manufacturing that protocol reply in a +fake server would not prove the Pi × Herdr product tuple. + +The smallest honest prerequisite is a version-pinned, supported production +condition source that can deterministically place the selected live runtime +in blocked **after prompt admission**, plus a non-terminal control that +induces and clears it without PTY input. The adapter/composer must project +that source as fresh `condition.value: blocked` for the exact runtime instance +and carry conformance evidence for its external version. If Pi cannot supply +that source, selecting a different inner runtime requires reopening this +Stage 1 decision and rerunning every scenario for all launchers; Stage 2 may +not silently switch only the blocked case. + +This prerequisite belongs to runtime/host capability work, not to a thin +launcher driver. Until it lands, the common suite is useful for development +but **cannot issue an overall pass**, and Stage 6 cannot seal portable +launcher support. + +### 8.2 Pinned artifacts and credentials + +The exact Pi 0.83.0 and Herdr 0.8.2 binaries are not currently available on +PATH, and their executable SHA-256 values are not established by this +checkout. Stage 2 must provision them locally and record provenance/digests +before live runs. The selected provider also needs a disposable isolated +credential. Absence fails setup/preflight; it does not weaken pins or choose +another runtime/model. + +### 8.3 Timeout fault-control proof + +Current code proves the Pi adapter's delivery boundary (successful full-frame +write to a live peer), socket naming, and native attempt semantics, but this +checkout has no live test that freezes a pinned Pi process between bind and +send. Before relying on section 6.6, Stage 2 must run that exact disposable +probe and retain evidence that the stopped listener yields one delivered +attempt and no assistant turn. If the pinned socket does not admit the frame +while stopped, `timeout` fails with +`prerequisite.timeout_induction_unavailable`; the suite may not replace it +with model persuasion, a timing race, command expiry, or a fake listener. + +## 9. Deadlines and cleanup are oracle results + +All bounds use monotonic deadlines in the common runner, not shell polling +loops: + +| Boundary | Limit | Stage result | +|---|---:|---| +| fixture setup and host probe | 30 s | `setup/error` | +| launcher discovery | 30 s | `discovery/error` | +| Duo preflight | 15 s | `preflight/error` | +| each launch process | 30 s | `launch/error` | +| post-launch bind | 12 s (covers the product's 8 s identity bind) | `bind/error` | +| each send process except the intentional observation wait | 30 s | `send/error` | +| ordinary assistant observation | 20 s | `observe/timeout` and overall failure when success was expected | +| intentional timeout observation | 20.0–22.5 s | `observe/timeout` and case pass | +| command inspection/restart reconcile | 15 s each | `command_inspection/error` or `restart/error` | +| cleanup | 30 s | `cleanup/error` | +| complete outer run | 10 min | failure at the last common checkpoint, never an unclassified driver timeout | + +Cleanup is mandatory after every prior outcome and appears last exactly once. +An earlier failure does not authorize omission: unrun semantic cases are +recorded by the common collector as failures at their first required stage, +then cleanup runs. A capture with no cleanup stage is invalid rather than +“failed but acceptable.” + +## 10. Rejection rules + +The offline validator rejects the entire capture if any of these is true: + +1. **Terminal evidence:** any PTY/terminal input, paste, send-keys, terminal + snapshot used as semantic evidence, or fallback after a Duo failure. +2. **Missing stages:** a required run/case stage, cleanup, assertion, error + stage, or evidence blob is absent, duplicated contrary to the scenario + manifest, out of order, or outside the closed enum. +3. **Launcher semantics:** a driver contains Duo command names, stage logic, + expected text/digests, polling, retries, state induction, or result repair; + task bytes or scenario/oracle digests differ between launchers. +4. **Unpinned inputs:** exact launcher, Duo, skill, config, host triple, + runtime version/format/asset, model selection, external schema, or binary + digest is absent or differs. `unverified` compatibility is never pass. +5. **Forbidden integrations:** plugins or MCP are enabled, a fake/unfinished + adapter appears, a user/production socket/store/workspace is touched, or + credentials are missing and a fallback provider was selected. +6. **Unsanitized content:** a credential value/fingerprint/path, private raw + transcript, reasoning, absolute user/run path, environment dump, vendor + state, or non-fixture conversation appears in the result or blobs. +7. **Fabricated pass:** a launcher verdict lacks matching raw Duo envelopes, + process/controller checkpoints, authority relationships, or independently + recomputed assertions; a nonzero/missing command was converted to pass; an + expected non-happy outcome was represented by killing the outer command. +8. **Idempotency drift:** a replay changes command ID, target, revision, + attempt list, or turn counts; a conflict lacks exact no-effect/retry/digest + fields. +9. **Timeout drift:** timeout is inferred from a shell exit, wall-clock sleep, + command expiry, or model timing instead of the bounded monotonic observer + and verified stopped process. +10. **Cleanup drift:** fixture resources remain, cleanup exceeds its deadline, + or emergency cleanup hides the failed cleanup stage. + +## 11. Stage 2 implementation boundaries + +The implemented offline slice keeps ownership as follows: + +`assembly.go` and `orchestrate.go` are Linux-only because their trusted +command journal and process-control sources are Linux-only. The scenario, +schemas, collector, oracle, and offline validator remain platform-neutral. + +```text +internal/conformance/portablelauncher/ +├── scenario.go # one manifest/state machine and task bytes +├── oracle.go # assertions; no launcher imports +├── fixture.go # roots, pinned host/runtime, controller, cleanup +├── capture.go # scrubbed content-addressed evidence +├── assembly.go # typed raw facts -> canonical observations/stages +├── orchestrate.go # prepare/run/load/assemble/validate/atomic-write API +└── validate.go # offline schema/pin/rejection validation + +contracts/schemas/ +└── duo-portable-launcher-conformance-result-v1.schema.json + +contracts/fixtures/duo-portable-launcher-conformance-v1/ +├── scenario.json # deterministic inputs and required stage/case matrix +└── result-*.json # passing structural/non-happy and rejected examples + +contrib/portable-launcher-conformance/ +├── amp # thin process/event adapter only +├── opencode # thin process/event adapter only +└── codex # thin process/event adapter only +``` + +No helper binary is added until the live normalization/provisioning inputs +exist; exporting an entry point that invents those inputs would be misleading. +Any future helper imports the common package and must not become a second Duo +protocol or a launcher-specific scenario owner. Runtime fixture +bytes remain owned by `internal/runtime/pi`; Herdr behavior remains owned by +`internal/host/herdr`; operation names remain owned by +`internal/registry/table.go`; public operation envelopes remain +`duo.external/v1`; skill installation/preflight remain owned by the companion +contract's `internal/manifest`, `internal/doctor`, and `internal/cli` seams. + +Stage 2 offline tests cover schema closure, every expected non-happy +outcome, stage attribution for every deadline, cleanup after each failure +point, scrub rejection, binary/version drift, missing evidence, terminal +paste, launcher-semantic contamination, duplicate effects, and fabricated +passes. Live launcher evidence remains for Stages 3–5; Stage 2 must not embed +launcher inference in ordinary unit tests. + +## 12. Decision summary + +- One inner fixture: supported Pi 0.83.0 on supported Herdr 0.8.2/protocol + 20/schema `c48f…b150`, with Pi transcript format v3 and Duo's native inject + asset pinned. +- One isolated local authority/workspace/host/runtime per run; no shared user + state, plugins, MCP, terminal input, or credential capture. +- One scenario/oracle/result schema for all outer launchers; drivers only + launch, submit, and capture raw events/stderr. The common collector alone + authors the final result. +- Exact delivery, semantic observation, exit, timeout, authority-restart, and + asymmetric idempotency assertions are grounded in current operations and + fixtures, including effect and retry semantics. +- Exited is induced by exact-pane host lifecycle; timeout by verified process + suspension around the native socket. Both are event-driven and disposable. +- Blocked cannot honestly be induced by the selected supported runtime today. + It is a named prerequisite and an overall-suite blocker, never a fabricated + pass. diff --git a/evidence/portable-launcher-conformance/step-01/probe-summary.json b/evidence/portable-launcher-conformance/step-01/probe-summary.json new file mode 100644 index 0000000..c3215b9 --- /dev/null +++ b/evidence/portable-launcher-conformance/step-01/probe-summary.json @@ -0,0 +1,83 @@ +{ + "schema_version": 1, + "observed_at_utc": "2026-09-17T20:56:15Z", + "host": { + "kernel": "Linux 6.8.0-136-generic", + "architecture": "x86_64" + }, + "duo_checkout": { + "path": "/home/dev/Code/duo", + "commit": "1437f2f69e71d164b4248ed121803e53e3e1a2ba", + "project_agents_directory_present": false + }, + "prior_assessment": { + "repository": "duo-lab", + "commit": "caf02f7eb325b1c935d0cf793bc53fbb3a3f1a65", + "date": "2026-09-17" + }, + "skill": { + "name": "duo-delegation-loop", + "canonical_path": "skills/duo-delegation-loop/SKILL.md", + "size_bytes": 6002, + "sha256": "4c84381b7b4a3454abd58a192bd158b3ae14cd7e1bcd7dba4c87d5d5ae9be30e" + }, + "launchers": { + "amp": { + "version": "0.0.1789675234-g2899fe", + "release_time": "2026-09-17T20:00:34.000Z", + "command_path": "/home/dev/.local/bin/amp", + "resolved_path": "/home/dev/.amp/bin/amp", + "sha256": "f351217dac739614b4d9b3eacad728ce6d61647ab1f5d4a4de44d99aba6598ee", + "project_copy": true, + "project_symlink": true, + "nested_cwd": true, + "user_home_agents": true, + "user_config_agents": true, + "native_user_root": "~/.config/amp/skills", + "behavioral_invocation": true, + "invocation_evidence": "stream JSON skill tool_use followed by correct sequence and 20s timeout" + }, + "opencode": { + "version": "1.18.31", + "command_path": "/home/dev/.opencode/bin/opencode", + "resolved_path": "/home/dev/.opencode/bin/opencode", + "sha256": "f9dab32248695e9ebd56b16a1921798fd85112cf5a69c7dfd0cabc1e17be4a11", + "project_copy": true, + "project_symlink": true, + "nested_cwd": true, + "user_home_agents": true, + "user_config_agents": false, + "native_project_root": ".opencode/skills", + "native_user_root": "~/.config/opencode/skills", + "behavioral_invocation": true, + "invocation_evidence": "completed skill tool_use with exact untruncated content followed by correct sequence and 20s timeout" + }, + "codex": { + "version": "0.154.0", + "command_path": "/home/dev/.local/bin/codex", + "javascript_entrypoint": "/home/dev/.npm-global/lib/node_modules/@openai/codex/bin/codex.js", + "javascript_entrypoint_sha256": "61b0194f3bb6534439c8d26a3ed57d0805f84b884588b761795323eeb92fcf70", + "native_package_version": "0.154.0-linux-x64", + "native_binary_sha256": "3188814c35471432d4123203e0eb38e5bddc60226e3d7ddf0e59e649ea140022", + "project_copy": true, + "project_symlink": true, + "nested_cwd": true, + "user_home_agents": true, + "user_config_agents": false, + "native_user_root": "$CODEX_HOME/skills", + "behavioral_invocation": true, + "invocation_evidence": "model-visible skill entry, successful SKILL.md read, correct sequence and 20s timeout" + } + }, + "shared_contract": { + "project_root": ".agents/skills", + "user_root": "~/.agents/skills", + "copied_directory_supported": true, + "absolute_directory_symlink_supported": true, + "relative_directory_symlink_tested": true, + "relative_directory_symlink_supported": true, + "relative_directory_symlink_target": "../../skills/duo-delegation-loop" + }, + "end_to_end_duo_commands_executed": false, + "secrets_in_artifact": false +} diff --git a/evidence/portable-launcher-conformance/step-01/report.md b/evidence/portable-launcher-conformance/step-01/report.md new file mode 100644 index 0000000..e0ab024 --- /dev/null +++ b/evidence/portable-launcher-conformance/step-01/report.md @@ -0,0 +1,222 @@ +# Step 01 live launcher discovery evidence + +- **Probe window:** 2026-09-17, completed 20:56:15Z +- **Host:** Linux 6.8.0-136-generic x86_64 +- **Duo checkout:** `/home/dev/Code/duo` at `1437f2f69e71d164b4248ed121803e53e3e1a2ba` +- **Prior assessment pin:** duo-lab `caf02f7eb325b1c935d0cf793bc53fbb3a3f1a65`, dated 2026-09-17 +- **Canonical skill tested:** `skills/duo-delegation-loop/SKILL.md`, 6,002 bytes, SHA-256 `4c84381b7b4a3454abd58a192bd158b3ae14cd7e1bcd7dba4c87d5d5ae9be30e` + +## Result + +The prior assessment's central claim is live-confirmed at the versions below: +**Amp, OpenCode, and Codex all discover a project skill at +`/.agents/skills/duo-delegation-loop/SKILL.md`**, including when the +skill directory is a symlink. All three also discover the same skill from the +shared user root `~/.agents/skills/`. + +The current Duo checkout itself does **not** yet contain `.agents/`; its +canonical skill exists only under `skills/duo-delegation-loop/`. Thus the +isolated probes prove that the proposed projection works, not that this +checkout already exposes the project skill. + +Evidence strength: + +- **Direct/deterministic:** executable identity, version output, CLI discovery + output, Codex model-visible prompt input, copy/symlink behavior, and parent + worktree traversal. +- **Strong behavioral invocation:** a minimal authenticated inference for each + launcher explicitly loaded the named skill and returned its three phases and + 20-second timeout without running Duo. Amp and OpenCode emitted a `skill` + tool event. Codex advertised the skill in model input, read `SKILL.md` through + the projected symlink, and then followed it. +- **Not proved here:** a real `duo session launch` / `duo prompt send` cycle, + hot reload after changing a skill in an already-running launcher, precedence + when duplicate skill names exist, or Windows behavior. + +## Installed launchers and executable identity + +| Launcher | Live version output | Resolved executable identity | SHA-256 | +|---|---|---|---| +| Amp | `0.0.1789675234-g2899fe (released 2026-09-17T20:00:34.000Z, 45m ago)` | `command -v`: `/home/dev/.local/bin/amp`; symlink to `/home/dev/.amp/bin/amp`; ELF x86-64, 100,750,816 bytes | `f351217dac739614b4d9b3eacad728ce6d61647ab1f5d4a4de44d99aba6598ee` | +| OpenCode | `1.18.31` | `/home/dev/.opencode/bin/opencode`; ELF x86-64, 185,030,784 bytes | `f9dab32248695e9ebd56b16a1921798fd85112cf5a69c7dfd0cabc1e17be4a11` | +| Codex | `codex-cli 0.154.0` | `/home/dev/.local/bin/codex` → npm `bin/codex.js` (SHA-256 `61b0194f…`), which launches package `@openai/codex-linux-x64` `0.154.0-linux-x64`; native ELF at `.../vendor/x86_64-unknown-linux-musl/bin/codex`, 262,858,016 bytes | native: `3188814c35471432d4123203e0eb38e5bddc60226e3d7ddf0e59e649ea140022` | + +`amp --version` and `amp version` agreed. `opencode --version` was authoritative; +`opencode version` is not a version command and attempted to open a directory +named `version`. `codex --version` was authoritative; `codex version` attempted +to start the TUI and failed noninteractively. Those latter two failures are CLI +shape observations, not version uncertainty. + +## Isolated probe layout + +All probe projects and homes were under +`/tmp/duo-launcher-probe-step01` and were removed after evidence was distilled. +The meaningful layout was: + +```text +source/duo-delegation-loop/SKILL.md # byte-identical canonical copy +project-copy/.agents/skills/duo-delegation-loop/ # copied directory +project-link/.agents/skills/duo-delegation-loop -> /tmp/.../source/duo-delegation-loop +project-relative/.agents/skills/duo-delegation-loop -> ../../skills/duo-delegation-loop +project-empty/ # user-root probes +home/ # isolated HOME/XDG/CODEX_HOME +``` + +Each project was an independent initialized Git working tree. Discovery was +also rerun from `project-link/nested/a`; every launcher resolved the skill from +the worktree root, rather than requiring the process cwd to contain `.agents`. + +No real user-global skill tree was changed. Authentication files needed for +the Amp and OpenCode inference/user-root probes were mechanically copied into +mode-0700/0600 temporary paths, never printed, and deleted immediately after +use. Codex used `exec --ephemeral`. OpenCode used `--pure` and isolated XDG +state; Amp used an empty isolated settings file and `--no-ide`; Codex used +`--ignore-user-config`. No plugin or MCP supplied discovery evidence. + +## Discovery matrix + +`yes` means the launcher itself returned the exact skill name, description, +and/or source path. `no` means the same isolated probe returned no matching +skill. Blank native roots were not tested because they are irrelevant to the +shared projection. + +| Root/form | Amp | OpenCode | Codex | +|---|---:|---:|---:| +| project `.agents/skills`, copied directory | yes (`amp skill info`) | yes (`opencode debug skill`) | yes (`codex debug prompt-input`) | +| project `.agents/skills`, absolute directory symlink | yes | yes | yes | +| project `.agents/skills`, invoked from nested cwd | yes | yes | yes | +| user `~/.agents/skills`, copied directory | yes | yes | yes | +| user `~/.config/agents/skills`, copied directory | yes | no | no | +| user `~/.config/amp/skills`, copied directory | yes | — | — | +| project `.opencode/skills`, copied directory | — | yes | — | +| user `~/.config/opencode/skills`, copied directory | — | yes | — | +| user `$CODEX_HOME/skills`, copied directory | — | — | yes | + +Amp's live help additionally identifies `.claude/skills` roots and custom +`amp.skills.path`; OpenCode's built-in customization skill identifies singular +and plural `.opencode/skill(s)` plus `~/.claude/skills`; those extra roots were +not needed to establish the common contract. + +### Symlink versus copy + +The copied and symlinked project forms exposed the same frontmatter and body. +OpenCode reported `body_has_launch=true` for both. Codex included the skill in +the model-visible skills block for both. Amp resolved both with `skill info`. +The authenticated invocation used the symlinked form for all three launchers. +Therefore a directory symlink from +`.agents/skills/duo-delegation-loop` to +`skills/duo-delegation-loop` is sufficient on this Linux host; copying also +works but can drift from the canonical source. The original probes used an +absolute target. An independent deterministic check then confirmed Amp, +OpenCode, and Codex all recognize the intended repository-relative target +`../../skills/duo-delegation-loop`. + +## Specific skill recognition and invocation + +The prompt for each live inference was narrowly constrained: use +`duo-delegation-loop`, execute no Duo command, and return the three phases plus +the timeout. + +- **Amp:** stream JSON contained a `tool_use` named `skill` with input + `{"name":"duo-delegation-loop"}`. Final output was + `session launch builder → prompt send → session show/conversation list/prompt show; polling timeout=20s`. + Amp necessarily created one remote execute thread; it was permanently + deleted immediately after the probe. +- **OpenCode:** JSON contained completed `tool_use` `skill` with + `name=duo-delegation-loop`, exact content, source directory under the + symlinked projection, and `truncated=false`. Final output reproduced the + launch/send/observe sequence and `20s` timeout. +- **Codex:** `debug prompt-input` directly showed the `.agents/skills` root and + named skill. The live ephemeral exec then ran only + `cat .../.agents/skills/duo-delegation-loop/SKILL.md` (exit 0) and returned + the launch/deliver/observe sequence and `20s` timeout. This is strong + behavioral evidence, although Codex represented loading as a read command + rather than a dedicated `skill` event. + +No inference executed `duo`, edited a project, or exposed credentials. The +responses establish recognition and instruction loading, not end-to-end Duo +launcher conformance. + +## Repeatable commands + +Paths below use `$P=/tmp/duo-launcher-probe-step01`, `$H=$P/home`, and a +byte-identical copy of the canonical skill. Credential setup is intentionally +omitted; use an already authenticated, disposable context and never print an +auth file. + +```sh +# Versions and executable identity +command -v amp opencode codex +amp --version +opencode --version +codex --version +readlink -f "$(command -v amp)" "$(command -v opencode)" "$(command -v codex)" +file -L "$(command -v amp)" "$(command -v opencode)" "$(command -v codex)" +sha256sum "$(command -v amp)" "$(command -v opencode)" "$(command -v codex)" + +# Amp deterministic project discovery (run in project-copy/project-link) +amp skill info duo-delegation-loop --json + +# OpenCode deterministic project discovery with isolated roots and no plugins +HOME="$H" XDG_CONFIG_HOME="$H/.config" XDG_DATA_HOME="$H/.local/share" \ +XDG_CACHE_HOME="$H/.cache" XDG_STATE_HOME="$H/.local/state" \ +OPENCODE_DISABLE_MODELS_FETCH=1 opencode debug skill --pure + +# Codex deterministic model-visible discovery +HOME="$H" CODEX_HOME="$H/.codex" codex debug prompt-input probe + +# Behavioral probes (semantically equivalent prompts; do not run Duo) +PROMPT='Use the duo-delegation-loop skill. Do not execute duo or any shell command. Return exactly one compact line beginning SEQUENCE= and list the three command-family phases and polling timeout from the skill.' +CODEX_PROMPT='Use the duo-delegation-loop skill. Do not execute duo or any other shell command except what is strictly required to load that skill. Return exactly one compact line beginning SEQUENCE= and list the three command-family phases and the polling timeout from the skill.' +amp -x "$PROMPT" --stream-json --no-ide --settings-file "$H/.config/amp/settings.json" +opencode run --pure --format json "$PROMPT" +codex --ask-for-approval never --sandbox read-only exec --ephemeral \ + --ignore-user-config --skip-git-repo-check --json "$CODEX_PROMPT" +``` + +User-root results were obtained by placing the same directory, one root at a +time, under the paths in the matrix and rerunning the deterministic command +from `project-empty`. Each temporary placement was removed before the next. + +## Limitations, assumptions, and disagreement check + +- Direct observations apply to the exact binaries, host, filesystem, and date + above. Continued compatibility is an inference and should be rechecked when + launcher versions change. +- The symlink targets stayed on the same local filesystem and remained + readable. Dangling, cross-device, Windows, and sandbox-denied symlinks were + not tested. +- A model correctly summarizing the loaded skill is not proof that it will + complete a real delegation loop under every permission/auth/runtime state. +- OpenCode 1.18.31's built-in customization text documents project + `.opencode/skill(s)` and user `~/.agents/skills`, but its live + `debug skill` also recognized project `.agents/skills`. The live behavior is + the stronger evidence for this pin. +- **No disagreement with duo-lab `caf02f7` on the central claim.** The material + qualification is that the current Duo checkout has not yet created the + `.agents/skills/duo-delegation-loop` projection, so “ready to conform” remains + accurate; “already installed in this checkout” would not be. + +## Recommended WIP findings + +1. **Live pin:** On 2026-09-17, Amp + `0.0.1789675234-g2899fe`, OpenCode `1.18.31`, and Codex CLI `0.154.0` + all directly discovered and behaviorally loaded the canonical + `duo-delegation-loop` skill from project `.agents/skills`. +2. **Projection contract:** Both a copied directory and an absolute directory + symlink worked for all three. A follow-up deterministic probe also confirmed + the intended relative symlink for all three; prefer + `.agents/skills/duo-delegation-loop -> ../../skills/duo-delegation-loop` + to avoid copy drift. +3. **User fallback:** `~/.agents/skills/duo-delegation-loop` was recognized by + all three. `~/.config/agents/skills` was Amp-only in this probe and must not + be treated as the shared user root. +4. **Current gap:** `/home/dev/Code/duo` at + `1437f2f69e71d164b4248ed121803e53e3e1a2ba` had no `.agents` directory; this + step validates the design but does not land the projection. +5. **Boundary:** Invocation proof covered skill loading and behavioral + adherence only. Real launch/send/observe, failure states, duplicate-name + precedence, and reload behavior remain for launcher conformance work. +6. **Cross-repo impact:** this confirms, rather than invalidates, the pinned + duo-lab `caf02f7` assumption. It unblocks product-side shared projection and + conformance design; no duo-lab correction is required. diff --git a/evidence/portable-launcher-conformance/step-07/probe-summary.json b/evidence/portable-launcher-conformance/step-07/probe-summary.json new file mode 100644 index 0000000..1a251e9 --- /dev/null +++ b/evidence/portable-launcher-conformance/step-07/probe-summary.json @@ -0,0 +1,198 @@ +{ + "schema": "duo.portable-launcher-step07-evidence/v1", + "observed_at_utc": { + "start": "2026-09-18T07:45:21Z", + "finish": "2026-09-18T07:51:13Z" + }, + "host": { + "kernel": "Linux 6.8.0-136-generic", + "architecture": "x86_64" + }, + "checkout": { + "path": "$CHECKOUT", + "branch": "feat/portable-launcher-conformance", + "commit": "e38ff77744ad453912ba4dd3a9bc7372c95e1130", + "describe": "e38ff77-dirty", + "preexisting_out_of_scope_changes": true + }, + "amp": { + "version": "0.0.1789715829-g82b7fb", + "release_time": "2026-09-18T07:17:09.000Z", + "command_path": "$AMP_COMMAND", + "resolved_path": "$AMP_RESOLVED", + "executable_bytes": 100787680, + "executable_sha256": "sha256:36b6da094db6e984ede80f65027674bda3fd1c126828b1fb4c254c003b6cd568", + "step01_version": "0.0.1789675234-g2899fe", + "step01_executable_sha256": "sha256:f351217dac739614b4d9b3eacad728ce6d61647ab1f5d4a4de44d99aba6598ee", + "matches_step01_pin": false, + "manifest_tested_version": "0.0.1789675234-g2899fe", + "matches_manifest_tested_version": false + }, + "duo": { + "version": "e38ff77-dirty", + "commit": "e38ff77744ad453912ba4dd3a9bc7372c95e1130", + "build_date": "2026-09-18T07:45:21Z", + "executable_path": "$RUN/bin/duo", + "executable_bytes": 14407182, + "executable_sha256": "sha256:871361f7d066e67b72cd6a95ba07c2d73ed21c6db14cd614795a7f6569028cf8", + "manifest_digest": "sha256:f508953d954b9d1ad2f37f18753a1d77a3b71ef847c280572bfb6d196d0f528f" + }, + "skill": { + "name": "duo-delegation-loop", + "format_version": "duo.skill/duo-delegation-loop/v1", + "content_digest": "sha256:6f8bc16b656f564a949c99f12d4cf0bcd04ec2c9b49a8b74fb65b2e355b5d182", + "size_bytes": 6284, + "installation_id": "99c41f9c084e349f940813980c9dcd22", + "installed_root": "$RUN/workspace/.agents/skills/duo-delegation-loop", + "installed_file": "$RUN/workspace/.agents/skills/duo-delegation-loop/SKILL.md", + "installed_stamp": "$RUN/workspace/.agents/skills/duo-delegation-loop/.duo-generated.json", + "stamp_sha256": "sha256:425ac11074b0822454d4d1bbbd83b07db9ddd632d1688e57b1f669af31c04c14" + }, + "config": { + "schema": "duo.config/v3", + "path": "$RUN/xdg/config/duo/duo.config.yaml", + "source_bytes_sha256": "sha256:b208078bb16c4d218e123b60cbe7e0173436d39d0abf6165f398bfa9f4615cd8", + "effective_digest": "sha256:bb5b986318036d0c25d6ed9d5f586ffde5a0973ab2f3a38daf98fe29ab4ae674", + "presets": [ + "builder" + ] + }, + "authority": { + "path": "$RUN/xdg/data/duo/duo.db", + "state": "initializable", + "present": false, + "healthy": true, + "schema_version": 0, + "writer_active": false + }, + "herdr": { + "version": "0.9.0", + "command_path": "$HERDR_COMMAND", + "resolved_path": "$HERDR_RESOLVED", + "executable_bytes": 39728936, + "executable_sha256": "sha256:5ef212a3f142f902b1a4eb3f7a76827ded9f313804c580c48297c060ac1fcac0", + "protocol_identity": "herdr-socket-api/22", + "schema_export_sha256": "sha256:226d4ecbd128d2e6bc84e4c8ddcec21ba9c7e51a0aafffcf087111ead3f1fa9a", + "compatibility": "unverified", + "required_suite_pin_available": false, + "required_suite_pin": { + "version": "0.8.2", + "protocol_identity": "herdr-socket-api/20", + "schema_digest": "sha256:c48f1f54ee0150ca27e11fd44455fe94aeadb20fdf4e4a62393ed822a4e5b150" + } + }, + "installation": { + "command": "$RUN/bin/duo install portable-launchers --workspace $RUN/workspace --output json", + "exit_code": 0, + "schema": "duo.external/v1", + "operation": "projection.install", + "target": "portable_launchers", + "state": "current", + "changed": true, + "warnings": [], + "features": [] + }, + "amp_discovery": { + "commands": [ + "amp skill list --json --settings-file $RUN/noauth/config/amp/settings.json", + "amp skill info duo-delegation-loop --json --settings-file $RUN/noauth/config/amp/settings.json" + ], + "exit_codes": [ + 0, + 0 + ], + "name": "duo-delegation-loop", + "source": "workspace-agents", + "base_dir": "file://$RUN/workspace/.agents/skills/duo-delegation-loop", + "info_path": "file://$RUN/workspace/.agents/skills/duo-delegation-loop", + "description_sha256": "sha256:47c044a8e999289d69dd7912f007af0d7f11b6e10a8f5ffe28e730c7139cb85e", + "skill_errors": [], + "loader_invoked": true, + "model_turn_invoked": false, + "threads_created": false + }, + "isolation": { + "run_root": "$RUN", + "home": "$RUN/noauth/home", + "xdg_config_home": "$RUN/noauth/config", + "xdg_data_home": "$RUN/noauth/data", + "xdg_state_home": "$RUN/noauth/state", + "xdg_cache_home": "$RUN/noauth/cache", + "workspace": "$RUN/workspace", + "real_user_skill_tree_written": false, + "account_credentials_used_by_canonical_amp_probe": false, + "global_plugin_lookup_deliberately_denied": true, + "global_plugin_load_failures": 2, + "plugin_start_events": 0, + "project_plugin_root_present": false, + "system_plugin_root_present": false, + "mcp_initializations_with_empty_ready_and_disabled_sets": 2, + "mcp_configured": false, + "terminal_input_used": false + }, + "positive_preflight": { + "command": "$RUN/bin/duo doctor --workspace $RUN/workspace --config $RUN/xdg/config/duo/duo.config.yaml --host herdr:$RUN/xdg/config/herdr/sessions/duo-step07/herdr.sock --output json", + "exit_code": 0, + "schema": "duo.launcher-preflight/v1", + "status": "ready", + "authority_scope": "local", + "checks": [ + {"id": "duo_executable", "stage": "executable", "required": true, "status": "pass", "code": "ok"}, + {"id": "effective_config", "stage": "config", "required": true, "status": "pass", "code": "ok"}, + {"id": "authority_store", "stage": "authority", "required": true, "status": "pass", "code": "ok"}, + {"id": "workspace", "stage": "workspace", "required": true, "status": "pass", "code": "ok"}, + {"id": "host_selection", "stage": "host_selection", "required": true, "status": "pass", "code": "ok"}, + {"id": "host_reachability", "stage": "host_reachability", "required": true, "status": "pass", "code": "ok"}, + {"id": "skill_projection", "stage": "skill_projection", "required": true, "status": "pass", "code": "ok"}, + {"id": "host_compatibility", "stage": "host_reachability", "required": false, "status": "warning", "code": "host.compatibility_unverified"} + ], + "skill_projection_state": "current", + "host_reachable": true, + "host_compatibility_required_for_readiness": false + }, + "negative_preflight": { + "command": "$RUN/bin/duo doctor --workspace $RUN/workspace --config $RUN/xdg/config/duo/duo.config.yaml --host herdr:$RUN/xdg/runtime/no-server/herdr.sock --output json", + "exit_code": 0, + "schema": "duo.launcher-preflight/v1", + "status": "not_ready", + "authority_scope": "unavailable", + "failed_check": { + "id": "host_reachability", + "stage": "host_reachability", + "required": true, + "status": "fail", + "code": "host.unreachable", + "summary": "selected Herdr host did not answer the bounded ping", + "action": "Host reachability stage: start or select the Herdr server at $RUN/xdg/runtime/no-server/herdr.sock and make its socket reachable." + }, + "compatibility_check": { + "status": "not_checked", + "code": "prerequisite.not_reached" + }, + "duo_authority_entries_before": 0, + "duo_authority_entries_after": 0, + "workspace_payload_manifest_sha256_before": "sha256:f900a9968fff9683031b6e87016860c75f80508657f4c30ddab8a5561bcf61ea", + "workspace_payload_manifest_sha256_after": "sha256:f900a9968fff9683031b6e87016860c75f80508657f4c30ddab8a5561bcf61ea", + "launch_command_invoked": false, + "authority_store_created": false, + "workspace_changed": false + }, + "scope_result": { + "step07": "blocked", + "blocker": "The accepted Amp 0.0.1789675234-g2899fe executable was not available for this run; the observed Amp 0.0.1789715829-g82b7fb executable is ineligible under the exact suite pin.", + "full_amp_suite_claimed": false, + "launcher_support_claimed": false, + "compatibility_warnings": [ + "Live Amp differs from the Step 01 and manifest tested pin.", + "Reachable Herdr 0.9.0/protocol 22 is unverified, not the later suite's Herdr 0.8.2/protocol 20 pin." + ] + }, + "scrub": { + "status": "pass", + "run_paths_replaced": true, + "credentials_present": false, + "private_transcripts_present": false, + "raw_vendor_state_present": false, + "unsanitized_environment_values_present": false + } +} diff --git a/evidence/portable-launcher-conformance/step-07/report.md b/evidence/portable-launcher-conformance/step-07/report.md new file mode 100644 index 0000000..37ff303 --- /dev/null +++ b/evidence/portable-launcher-conformance/step-07/report.md @@ -0,0 +1,110 @@ +# Step 07 — Amp discovery and local-authority preflight + +- **Evidence window:** 2026-09-18 07:45:21Z–07:51:13Z +- **Checkout:** `$CHECKOUT`, branch `feat/portable-launcher-conformance`, commit `e38ff77744ad453912ba4dd3a9bc7372c95e1130` +- **Outcome:** **BLOCKED — the accepted Amp executable was unavailable** +- **Not claimed:** the complete Amp launcher suite or launcher support. The live Amp and Herdr versions do not match the repository's later full-suite pins. + +## Live pins + +| Component | Observed identity | +|---|---| +| Amp | `0.0.1789715829-g82b7fb`, released `2026-09-18T07:17:09.000Z`; 100,787,680-byte resolved executable; SHA-256 `36b6da094db6e984ede80f65027674bda3fd1c126828b1fb4c254c003b6cd568` | +| Duo | `e38ff77-dirty`; full commit `e38ff77744ad453912ba4dd3a9bc7372c95e1130`; built `2026-09-18T07:45:21Z`; SHA-256 `871361f7d066e67b72cd6a95ba07c2d73ed21c6db14cd614795a7f6569028cf8` | +| Skill | `duo.skill/duo-delegation-loop/v1@sha256:6f8bc16b656f564a949c99f12d4cf0bcd04ec2c9b49a8b74fb65b2e355b5d182`; 6,284 bytes; installation ID `99c41f9c084e349f940813980c9dcd22` | +| Config | `duo.config/v3`; effective digest `sha256:bb5b986318036d0c25d6ed9d5f586ffde5a0973ab2f3a38daf98fe29ab4ae674`; preset `builder` | +| Authority | `$RUN/xdg/data/duo/duo.db`; absent, healthy, locally `initializable`, no writer | +| Herdr | `0.9.0`; protocol `herdr-socket-api/22`; 39,728,936-byte executable; SHA-256 `5ef212a3f142f902b1a4eb3f7a76827ded9f313804c580c48297c060ac1fcac0`; schema-export SHA-256 `226d4ecbd128d2e6bc84e4c8ddcec21ba9c7e51a0aafffcf087111ead3f1fa9a` | +| Host | Linux `6.8.0-136-generic`, `x86_64` | + +The live Amp pin changed since Step 01 (`0.0.1789675234-g2899fe`, digest `f351217d…`). It also differs from the manifest's current exact tested version. This run therefore re-probed the live binary instead of inheriting Step 01's result. The new pin is discovery evidence only; the manifest and full-suite accepted pin remain unchanged. Because the workplan requires the pinned launcher and the suite admits exact identities, this capture does not meet Step 07's seal condition. + +The checkout was already dirty before this task because of out-of-scope user work. The Duo build records that honestly as `e38ff77-dirty`; its full commit, build time, manifest digest, byte size, and executable digest disambiguate the tested binary. No product source was changed. + +## Isolated setup and projection + +The run used one mode-0700 disposable root with separate home, XDG config/data/state/cache/runtime, workspace, binaries, and Herdr session. The canonical probe did not use account credentials. A known-invalid local sentinel prevented account-global plugin retrieval without exposing or copying a credential. The local skill commands still completed with exit 0. + +The checkout-built Duo command was: + +```text +$RUN/bin/duo install portable-launchers --workspace $RUN/workspace --output json +``` + +It returned `projection.install`, `state: current`, `changed: true`, and the identity above. The installed `SKILL.md` was a regular copied file with the exact canonical digest. Its Duo ownership stamp had manifest digest `sha256:f508953d954b9d1ad2f37f18753a1d77a3b71ef847c280572bfb6d196d0f528f`. + +No real user skill tree was written. The disposable roots were removed after validation. + +## Amp recognition and loader invocation + +From the isolated workspace, with the isolated settings and roots: + +```text +amp skill list --json --settings-file $RUN/noauth/config/amp/settings.json +amp skill info duo-delegation-loop --json --settings-file $RUN/noauth/config/amp/settings.json +``` + +Both commands exited 0. `skill list` returned exactly one `duo-delegation-loop` entry with `source: workspace-agents` and base directory `file://$RUN/workspace/.agents/skills/duo-delegation-loop`; `skill info` resolved the same path. The returned descriptions were byte-identical (SHA-256 `47c044a8e999289d69dd7912f007af0d7f11b6e10a8f5ffe28e730c7139cb85e`). Direct hashing tied that path to the installed skill digest. + +This is a direct invocation of Amp's local skill loader and metadata parser. It is deliberately not a model-turn behavioral invocation: `amp -x` would create a thread, which this task prohibited and Step 08 owns. No thread or terminal-input path was used. + +The canonical Amp log showed zero plugin-start events. Account-global plugin lookup was deliberately denied, project/system plugin roots were absent, and both MCP initialization records had empty `ready` and `disabled` sets. The skill result had no errors. Thus neither a plugin nor MCP supplied the recognition evidence. + +## Positive local-authority preflight + +A disposable Herdr 0.9.0 server answered at the explicit isolated Unix socket. The exact command was: + +```text +$RUN/bin/duo doctor --workspace $RUN/workspace \ + --config $RUN/xdg/config/duo/duo.config.yaml \ + --host herdr:$RUN/xdg/config/herdr/sessions/duo-step07/herdr.sock \ + --output json +``` + +Doctor exited 0 and reported `duo.launcher-preflight/v1`, `status: ready`, and `authority_scope: local`. All seven required checks passed in contract order: executable, config, authority, workspace, host selection, host reachability, and skill projection. The selected host answered the bounded ping, and the projected skill was `current` with the same installation ID and digest Amp resolved. + +The eighth informational check was separately `warning / host.compatibility_unverified`: installed Herdr is 0.9.0/protocol 22, not the full-suite pin 0.8.2/protocol 20/schema `c48f1f54…`. Under contract §6, that warning does not make otherwise reachable local authority not-ready. This evidence does **not** claim the later suite pin. + +## Negative reachability and refusal before launch + +The same binary, config, workspace, and installed skill were checked against an absent socket: + +```text +$RUN/bin/duo doctor --workspace $RUN/workspace \ + --config $RUN/xdg/config/duo/duo.config.yaml \ + --host herdr:$RUN/xdg/runtime/no-server/herdr.sock \ + --output json +``` + +Doctor still exited 0, as required for a complete diagnostic report, but preflight was `not_ready` with `authority_scope: unavailable`. The exact required failure was: + +- stage/check: `host_reachability` +- status/code: `fail / host.unreachable` +- summary: `selected Herdr host did not answer the bounded ping` +- action: `Host reachability stage: start or select the Herdr server at $RUN/xdg/runtime/no-server/herdr.sock and make its socket reachable.` + +`host_compatibility` was `not_checked / prerequisite.not_reached`. The Duo executable, valid config, initializable authority, workspace, host selection, and current projection still passed, proving readiness did not collapse to binary presence. + +No launch command was invoked. The Duo authority tree had zero entries before and after, so doctor did not create a store or session record. The workspace payload manifest was identical before and after (`sha256:f900a9968fff9683031b6e87016860c75f80508657f4c30ddab8a5561bcf61ea`). + +## Conservative assumptions and remaining boundary + +1. “Invokes the skill” is interpreted at this Step 07 boundary as Amp's deterministic local loader invocation (`skill info`), not an agent/model turn. That is the strongest proof compatible with the explicit no-thread rule; behavioral scenario execution remains Step 08. +2. A missing authority store is accepted as `initializable`, exactly as contract §6.1 specifies. Doctor remained read-only and did not create it. +3. Herdr 0.9.0 can honestly prove reachability/readiness under the informational compatibility rule, but cannot prove the later exact Herdr suite pin. +4. Live Amp discovery is proved for the newly observed exact executable. Because it differs from the manifest's tested pin, no broader launcher-support or full-suite compatibility claim is made. +5. The canonical Amp probe intentionally had no account-global state. Its expected unauthenticated global-plugin warning is not a skill error and does not weaken the direct workspace source/path evidence. + +## Blocker and resume condition + +The accepted `0.0.1789675234-g2899fe` executable with SHA-256 `f351217d…` was no longer present under the local Amp installation or in the searched local caches. The installed self-updating binary cannot substitute for it: the suite explicitly rejects unpinned latest versions even when their observed behavior succeeds. + +Resume this step by provisioning that exact accepted executable and rerunning discovery plus both preflight cases. Alternatively, revise the accepted Amp pin through a deliberate contract change and rerun every Stage 1 observation and Stage 2 artifact whose identity embeds the old version. This evidence alone is insufficient to authorize that wider change. + +## Verification + +- `jq` parsed and asserted the raw installer and both raw doctor captures before scrubbing. +- `validate.sh` parses `probe-summary.json`, checks cross-field identities and positive/negative verdicts, confirms plugin/MCP/thread boundaries, and rejects raw run paths or common credential material. +- `git diff --check -- evidence/portable-launcher-conformance/step-07` passed. + +Cross-repo impact: **the live Amp observation invalidates the freshness assumption for the accepted Amp pin in `duo-lab`'s 2026-09-17 assessment at `caf02f7eb325b1c935d0cf793bc53fbb3a3f1a65`**. It does not contradict that assessment's historical discovery result or alter the Herdr 0.8.2/protocol 20 runtime-fixture requirement. Offer a finding on the affected `duo-lab` matter before authoring it there. diff --git a/evidence/portable-launcher-conformance/step-07/validate.sh b/evidence/portable-launcher-conformance/step-07/validate.sh new file mode 100755 index 0000000..3bfd57b --- /dev/null +++ b/evidence/portable-launcher-conformance/step-07/validate.sh @@ -0,0 +1,55 @@ +#!/bin/sh +set -eu + +root=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd) +summary="$root/probe-summary.json" + +jq -e ' + .schema == "duo.portable-launcher-step07-evidence/v1" and + .observed_at_utc.start[0:10] == "2026-09-18" and + .observed_at_utc.finish[0:10] == "2026-09-18" and + .amp.version == "0.0.1789715829-g82b7fb" and + .amp.executable_sha256 == "sha256:36b6da094db6e984ede80f65027674bda3fd1c126828b1fb4c254c003b6cd568" and + .amp.matches_step01_pin == false and + .amp.matches_manifest_tested_version == false and + .scope_result.step07 == "blocked" and + (.scope_result.blocker | contains("accepted Amp 0.0.1789675234-g2899fe executable was not available")) and + .scope_result.full_amp_suite_claimed == false and + .scope_result.launcher_support_claimed == false and + .duo.commit == .checkout.commit and + .skill.name == .amp_discovery.name and + .skill.installed_root == ($__step07_run + "/workspace/.agents/skills/duo-delegation-loop") and + .amp_discovery.source == "workspace-agents" and + .amp_discovery.loader_invoked == true and + .amp_discovery.model_turn_invoked == false and + .amp_discovery.threads_created == false and + .installation.state == "current" and + .installation.target == "portable_launchers" and + .positive_preflight.schema == "duo.launcher-preflight/v1" and + .positive_preflight.status == "ready" and + .positive_preflight.authority_scope == "local" and + ([.positive_preflight.checks[] | select(.required == true and .status == "pass" and .code == "ok")] | length) == 7 and + ([.positive_preflight.checks[] | select(.id == "host_compatibility" and .required == false and .status == "warning" and .code == "host.compatibility_unverified")] | length) == 1 and + .negative_preflight.status == "not_ready" and + .negative_preflight.authority_scope == "unavailable" and + .negative_preflight.failed_check.stage == "host_reachability" and + .negative_preflight.failed_check.code == "host.unreachable" and + (.negative_preflight.failed_check.action | contains("$RUN/xdg/runtime/no-server/herdr.sock")) and + .negative_preflight.duo_authority_entries_before == 0 and + .negative_preflight.duo_authority_entries_after == 0 and + .negative_preflight.workspace_payload_manifest_sha256_before == .negative_preflight.workspace_payload_manifest_sha256_after and + .negative_preflight.launch_command_invoked == false and + .negative_preflight.authority_store_created == false and + .negative_preflight.workspace_changed == false and + .isolation.plugin_start_events == 0 and + .isolation.mcp_configured == false and + .isolation.terminal_input_used == false and + .scrub.status == "pass" +' --arg __step07_run '$RUN' "$summary" >/dev/null + +if grep -En '/tmp/duo-step07|sgamp_|Bearer[[:space:]]|user_code|"access_token"|"refresh_token"' "$summary" "$root/report.md"; then + echo "step-07 evidence scrub check failed" >&2 + exit 1 +fi + +printf '%s\n' "step-07 evidence validation: pass" diff --git a/evidence/portable-launcher-conformance/step-09/probe-summary.json b/evidence/portable-launcher-conformance/step-09/probe-summary.json new file mode 100644 index 0000000..643465a --- /dev/null +++ b/evidence/portable-launcher-conformance/step-09/probe-summary.json @@ -0,0 +1,225 @@ +{ + "schema": "duo.portable-launcher-step09-evidence/v1", + "observed_at_utc": { + "start": "2026-09-18T08:04:57Z", + "finish": "2026-09-18T08:09:32Z" + }, + "host": { + "kernel": "Linux 6.8.0-136-generic", + "architecture": "x86_64" + }, + "checkout": { + "path": "$CHECKOUT", + "branch": "feat/portable-launcher-conformance", + "commit": "75ad27a58491a00d4243564f86dc213c4688aa35", + "describe": "75ad27a-dirty", + "preexisting_out_of_scope_changes": true + }, + "opencode": { + "version": "1.18.31", + "command_path": "$OPENCODE_COMMAND", + "resolved_path": "$OPENCODE_RESOLVED", + "executable_bytes": 185030784, + "executable_sha256": "sha256:f9dab32248695e9ebd56b16a1921798fd85112cf5a69c7dfd0cabc1e17be4a11", + "accepted_version": "1.18.31", + "accepted_executable_sha256": "sha256:f9dab32248695e9ebd56b16a1921798fd85112cf5a69c7dfd0cabc1e17be4a11", + "matches_accepted_pin": true, + "matches_manifest_tested_version": true + }, + "duo": { + "version": "75ad27a-dirty", + "commit": "75ad27a58491a00d4243564f86dc213c4688aa35", + "build_date": "2026-09-18T08:04:57Z", + "build_command": "CGO_ENABLED=0 go build -trimpath -ldflags -o $RUN/bin/duo ./cmd/duo", + "executable_path": "$RUN/bin/duo", + "executable_bytes": 14369268, + "executable_sha256": "sha256:f596857ba690ac1722d8c7118875aa2cfa4b5a76434406ead278bd350b83f6b5", + "manifest_digest": "sha256:35fe1c26e9b619759c9c7efc3b50074af8e61fb7d0ee595ad9e98278b9e02a84" + }, + "skill": { + "name": "duo-delegation-loop", + "format_version": "duo.skill/duo-delegation-loop/v1", + "content_digest": "sha256:6f8bc16b656f564a949c99f12d4cf0bcd04ec2c9b49a8b74fb65b2e355b5d182", + "size_bytes": 6284, + "canonical_path": "$CHECKOUT/skills/duo-delegation-loop/SKILL.md", + "installation_id": "760f86d8e7c1d2dcb31be8ebdf4b7f3d", + "installed_root": "$RUN/workspace/.agents/skills/duo-delegation-loop", + "installed_file": "$RUN/workspace/.agents/skills/duo-delegation-loop/SKILL.md", + "installed_stamp": "$RUN/workspace/.agents/skills/duo-delegation-loop/.duo-generated.json", + "stamp_schema": "duo.projection-stamp/v1", + "stamp_target": "portable_launchers", + "stamp_tested_version_range": "amp=0.0.1789675234-g2899fe;opencode=1.18.31;codex=0.154.0", + "stamp_sha256": "sha256:2a1c43f5a231905844f53d0114bb449513d31d13c1d4de81866c9e5770f9a628", + "installed_bytes_match_canonical": true + }, + "config": { + "schema": "duo.config/v3", + "path": "$RUN/xdg/config/duo/duo.config.yaml", + "source_bytes_sha256": "sha256:1b4cb5fafef7a324beb46b8d8529ee76d4cc1218c41d5da24251b0872b293840", + "effective_digest": "sha256:92db1583823b8d8ce7384449896cea6c641b3b4c7df5d50ce56d43a0092826ae", + "presets": [ + "builder" + ] + }, + "authority": { + "path": "$RUN/xdg/data/duo/duo.db", + "state": "initializable", + "present": false, + "healthy": true, + "schema_version": 0, + "writer_active": false + }, + "herdr": { + "version": "0.9.0", + "command_path": "$HERDR_COMMAND", + "resolved_path": "$HERDR_RESOLVED", + "executable_bytes": 39728936, + "executable_sha256": "sha256:5ef212a3f142f902b1a4eb3f7a76827ded9f313804c580c48297c060ac1fcac0", + "protocol_identity": "herdr-socket-api/22", + "schema_export_sha256": "sha256:226d4ecbd128d2e6bc84e4c8ddcec21ba9c7e51a0aafffcf087111ead3f1fa9a", + "compatibility": "unverified", + "required_full_suite_pin_available": false, + "required_full_suite_pin": { + "version": "0.8.2", + "protocol_identity": "herdr-socket-api/20", + "schema_digest": "sha256:c48f1f54ee0150ca27e11fd44455fe94aeadb20fdf4e4a62393ed822a4e5b150" + } + }, + "installation": { + "command": "$RUN/bin/duo install portable-launchers --workspace $RUN/workspace --output json", + "exit_code": 0, + "schema": "duo.external/v1", + "operation": "projection.install", + "target": "portable_launchers", + "state": "current", + "changed": true, + "warnings": [], + "features": [] + }, + "opencode_discovery": { + "commands": [ + "opencode debug skill --pure", + "opencode debug config --pure" + ], + "exit_codes": [ + 0, + 0 + ], + "matching_skill_records": 1, + "name": "duo-delegation-loop", + "location": "$RUN/workspace/.agents/skills/duo-delegation-loop/SKILL.md", + "description_sha256": "sha256:47c044a8e999289d69dd7912f007af0d7f11b6e10a8f5ffe28e730c7139cb85e", + "loaded_body_sha256": "sha256:f957e32d73807d68fab29682eff0abc5bc98da5fb5a244c841c48eb6867c6421", + "canonical_body_sha256": "sha256:f957e32d73807d68fab29682eff0abc5bc98da5fb5a244c841c48eb6867c6421", + "loaded_body_matches_canonical": true, + "body_has_launch_sequence": true, + "body_has_install_guidance": true, + "loader_invoked": true, + "invocation_boundary": "OpenCode's deterministic debug skill command discovered the project skill and invoked its local loader/frontmatter parser, returning the canonical parsed body; no model turn was run.", + "model_turn_invoked": false, + "behavioral_model_adherence_claimed": false, + "sessions_created": 0, + "accounts_present": 0, + "credentials_present": 0 + }, + "isolation": { + "run_root": "$RUN", + "home": "$RUN/noauth/home", + "xdg_config_home": "$RUN/noauth/config", + "xdg_data_home": "$RUN/noauth/data", + "xdg_state_home": "$RUN/noauth/state", + "xdg_cache_home": "$RUN/noauth/cache", + "workspace": "$RUN/workspace", + "clean_environment_used": true, + "real_user_skill_tree_written": false, + "account_credentials_used": false, + "external_plugins_disabled_by_pure": true, + "resolved_plugins": [], + "mcp_configured": false, + "terminal_input_used": false, + "threads_created": false + }, + "positive_preflight": { + "command": "$RUN/bin/duo doctor --workspace $RUN/workspace --config $RUN/xdg/config/duo/duo.config.yaml --host herdr:$RUN/xdg/config/herdr/sessions/duo-step09/herdr.sock --output json", + "exit_code": 0, + "schema": "duo.launcher-preflight/v1", + "status": "ready", + "authority_scope": "local", + "host_source": "explicit-flag", + "checks": [ + {"id": "duo_executable", "stage": "executable", "required": true, "status": "pass", "code": "ok"}, + {"id": "effective_config", "stage": "config", "required": true, "status": "pass", "code": "ok"}, + {"id": "authority_store", "stage": "authority", "required": true, "status": "pass", "code": "ok"}, + {"id": "workspace", "stage": "workspace", "required": true, "status": "pass", "code": "ok"}, + {"id": "host_selection", "stage": "host_selection", "required": true, "status": "pass", "code": "ok"}, + {"id": "host_reachability", "stage": "host_reachability", "required": true, "status": "pass", "code": "ok"}, + {"id": "skill_projection", "stage": "skill_projection", "required": true, "status": "pass", "code": "ok"}, + {"id": "host_compatibility", "stage": "host_reachability", "required": false, "status": "warning", "code": "host.compatibility_unverified"} + ], + "skill_projection_state": "current", + "host_reachable": true, + "host_compatibility_required_for_readiness": false, + "duo_authority_entries_before": 0, + "duo_authority_entries_after": 0, + "workspace_payload_manifest_sha256_before": "sha256:eb06b3f3ecbb9004943a86728172b86e428f811ea84f377842c11cc0c7f78da1", + "workspace_payload_manifest_sha256_after": "sha256:eb06b3f3ecbb9004943a86728172b86e428f811ea84f377842c11cc0c7f78da1", + "authority_store_created": false, + "workspace_changed": false + }, + "negative_preflight": { + "command": "$RUN/bin/duo doctor --workspace $RUN/workspace --config $RUN/xdg/config/duo/duo.config.yaml --host herdr:$RUN/xdg/runtime/no-server/herdr.sock --output json", + "exit_code": 0, + "schema": "duo.launcher-preflight/v1", + "status": "not_ready", + "authority_scope": "unavailable", + "failed_check": { + "id": "host_reachability", + "stage": "host_reachability", + "required": true, + "status": "fail", + "code": "host.unreachable", + "summary": "selected Herdr host did not answer the bounded ping", + "action": "Host reachability stage: start or select the Herdr server at $RUN/xdg/runtime/no-server/herdr.sock and make its socket reachable." + }, + "compatibility_check": { + "status": "not_checked", + "code": "prerequisite.not_reached" + }, + "duo_authority_entries_before": 0, + "duo_authority_entries_after": 0, + "workspace_payload_manifest_sha256_before": "sha256:eb06b3f3ecbb9004943a86728172b86e428f811ea84f377842c11cc0c7f78da1", + "workspace_payload_manifest_sha256_after": "sha256:eb06b3f3ecbb9004943a86728172b86e428f811ea84f377842c11cc0c7f78da1", + "launch_command_invoked": false, + "authority_store_created": false, + "workspace_changed": false + }, + "host_state": { + "fixture_objects_before": {"workspaces": 0, "tabs": 0, "panes": 0, "agents": 0}, + "fixture_objects_after": {"workspaces": 0, "tabs": 0, "panes": 0, "agents": 0}, + "launch_observed": false + }, + "scope_result": { + "step09": "pass", + "seal_condition_met": true, + "exact_accepted_launcher_used": true, + "full_opencode_suite_claimed": false, + "launcher_support_claimed": false, + "compatibility_warnings": [ + "Reachable Herdr 0.9.0/protocol 22 is unverified, not the later full-suite Herdr 0.8.2/protocol 20 pin." + ] + }, + "cleanup": { + "server_stopped_via_local_control": true, + "server_process_exited": true, + "socket_removed": true, + "run_root_removed": true + }, + "scrub": { + "status": "pass", + "run_paths_replaced": true, + "credentials_present": false, + "private_transcripts_present": false, + "raw_vendor_state_present": false, + "unsanitized_environment_values_present": false + } +} diff --git a/evidence/portable-launcher-conformance/step-09/report.md b/evidence/portable-launcher-conformance/step-09/report.md new file mode 100644 index 0000000..64a1017 --- /dev/null +++ b/evidence/portable-launcher-conformance/step-09/report.md @@ -0,0 +1,106 @@ +# Step 09 — OpenCode discovery and local-authority preflight + +- **Evidence window:** 2026-09-18 08:04:57Z–08:09:32Z +- **Checkout:** `$CHECKOUT`, branch `feat/portable-launcher-conformance`, commit `75ad27a58491a00d4243564f86dc213c4688aa35` +- **Outcome:** **PASS — the exact accepted OpenCode launcher passed discovery and both preflight cases** +- **Step 09 seal condition:** **met** +- **Not claimed:** behavioral model adherence, the complete OpenCode launcher suite, or overall launcher support. Step 10 owns the behavioral suite, and the installed Herdr does not match that suite's host pin. + +## Exact observed pins + +| Component | Observed identity | +|---|---| +| OpenCode | `1.18.31`; 185,030,784-byte ELF x86-64 executable; SHA-256 `f9dab32248695e9ebd56b16a1921798fd85112cf5a69c7dfd0cabc1e17be4a11` | +| Duo | `75ad27a-dirty`; full commit `75ad27a58491a00d4243564f86dc213c4688aa35`; built `2026-09-18T08:04:57Z`; 14,369,268 bytes; SHA-256 `f596857ba690ac1722d8c7118875aa2cfa4b5a76434406ead278bd350b83f6b5` | +| Duo manifest | semantic digest `sha256:35fe1c26e9b619759c9c7efc3b50074af8e61fb7d0ee595ad9e98278b9e02a84` | +| Skill | `duo.skill/duo-delegation-loop/v1@sha256:6f8bc16b656f564a949c99f12d4cf0bcd04ec2c9b49a8b74fb65b2e355b5d182`; 6,284 bytes; installation ID `760f86d8e7c1d2dcb31be8ebdf4b7f3d` | +| Config | `duo.config/v3`; source SHA-256 `1b4cb5fafef7a324beb46b8d8529ee76d4cc1218c41d5da24251b0872b293840`; effective digest `sha256:92db1583823b8d8ce7384449896cea6c641b3b4c7df5d50ce56d43a0092826ae`; preset `builder` | +| Authority | `$RUN/xdg/data/duo/duo.db`; absent, healthy, locally `initializable`, schema version `0`, no writer | +| Herdr | `0.9.0`; protocol `herdr-socket-api/22`; 39,728,936 bytes; SHA-256 `5ef212a3f142f902b1a4eb3f7a76827ded9f313804c580c48297c060ac1fcac0`; schema-export SHA-256 `226d4ecbd128d2e6bc84e4c8ddcec21ba9c7e51a0aafffcf087111ead3f1fa9a` | +| Host | Linux `6.8.0-136-generic`, `x86_64` | + +The OpenCode version and executable digest exactly match the accepted Step 01 and product-suite pin. No latest or substitute executable was used. The checkout was already dirty because of out-of-scope user residue; the Duo build records that honestly in its version while its full commit, build time, byte count, digest, and semantic manifest digest disambiguate the executable. + +## Isolated projection and ownership + +The run used a mode-0700 disposable root with separate OpenCode home, XDG config/data/state/cache, Duo config/data/runtime, workspace, binaries, and Herdr state. The current checkout built Duo directly into `$RUN/bin/duo`. The installer command was: + +```text +$RUN/bin/duo install portable-launchers --workspace $RUN/workspace --output json +``` + +It exited 0 with `projection.install`, `state: current`, and `changed: true`. The installed `SKILL.md` was a regular copied file and byte-matched the product-owned source at `$CHECKOUT/skills/duo-delegation-loop/SKILL.md`. Its ownership record was `duo.projection-stamp/v1`, target `portable_launchers`, format `duo.skill/duo-delegation-loop/v1`, semantic manifest digest `sha256:35fe1c26…02a84`, and exact tested range `amp=0.0.1789675234-g2899fe;opencode=1.18.31;codex=0.154.0`. The stamp SHA-256 was `2a1c43f5a231905844f53d0114bb449513d31d13c1d4de81866c9e5770f9a628`. + +No real user skill tree, launcher config, account, or credential store was read or written. The disposable roots were removed after evidence validation. + +## OpenCode recognition and deterministic loader invocation + +From the isolated workspace, under a clean allowlisted environment: + +```text +opencode debug skill --pure +opencode debug config --pure +``` + +Both commands exited 0. `debug skill` returned exactly one `duo-delegation-loop` record at `$RUN/workspace/.agents/skills/duo-delegation-loop/SKILL.md`. It parsed the canonical frontmatter description (SHA-256 `47c044a8…9cb85e`) and returned the complete body. The loaded-body digest and the independently extracted canonical-body digest were identical: `sha256:f957e32d73807d68fab29682eff0abc5bc98da5fb5a244c841c48eb6867c6421`. The body included the launch sequence and current portable-launcher installation guidance. Directly hashing the reported installed file tied it to the full canonical skill digest above. + +At this Step 09 boundary, **“invokes” means OpenCode's deterministic `debug skill` facility invoked its project skill discovery, frontmatter parser, and body loader and returned the installed canonical skill**. It does not mean a model consumed or followed the instructions. No model turn was run, no OpenCode session was created, and no behavioral adherence is claimed; that belongs to Step 10. This is the strongest invocation proof compatible with the explicit prohibition on creating threads. + +`--pure` disabled external plugins. The resolved isolated config had `plugin: []` and no MCP configuration. Aggregate database checks found zero sessions, accounts, and credentials. No terminal input was used. + +## Positive reachable local-authority preflight + +A disposable Herdr server answered at the explicitly selected isolated Unix socket. The exact command was: + +```text +$RUN/bin/duo doctor --workspace $RUN/workspace \ + --config $RUN/xdg/config/duo/duo.config.yaml \ + --host herdr:$RUN/xdg/config/herdr/sessions/duo-step09/herdr.sock \ + --output json +``` + +Doctor exited 0 and reported `duo.launcher-preflight/v1`, `status: ready`, `authority_scope: local`, and `host_source: explicit-flag`. All seven required checks passed in contract order: executable, effective config, authority store, workspace, host selection, host reachability, and skill projection. The projected skill was `current` with the same identity and installation ID OpenCode resolved. + +The eighth informational check was `warning / host.compatibility_unverified`: the reachable host is Herdr 0.9.0/protocol 22, not the later full-suite pin Herdr 0.8.2/protocol 20/schema `c48f1f54…b150`. Contract §6 makes compatibility informational for this preflight, so this does not weaken the Step 09 readiness result. It does prevent a complete-suite claim. + +Positive diagnosis was read-only: the workspace payload manifest stayed `sha256:eb06b3f…8da1`, the Duo authority tree stayed at zero entries, and no authority database was created. + +## Negative unreachable-host refusal + +The same Duo binary, config, workspace, and installed skill were checked against an absent explicitly selected socket: + +```text +$RUN/bin/duo doctor --workspace $RUN/workspace \ + --config $RUN/xdg/config/duo/duo.config.yaml \ + --host herdr:$RUN/xdg/runtime/no-server/herdr.sock \ + --output json +``` + +Doctor correctly exited 0 because it produced a complete diagnostic report, but preflight was `not_ready` with `authority_scope: unavailable`. The stage-specific required failure was: + +- check/stage: `host_reachability` +- status/code: `fail / host.unreachable` +- summary: `selected Herdr host did not answer the bounded ping` +- action: start or select the Herdr server at the scrubbed `$RUN/xdg/runtime/no-server/herdr.sock` path and make it reachable + +`host_compatibility` was `not_checked / prerequisite.not_reached`; the other six required checks retained their independent results, including a current skill projection. + +No `session launch` command was invoked. The workspace payload manifest remained byte-for-byte identical, the authority tree remained at zero entries, no store was created, and the isolated reachable Herdr server retained zero workspaces, tabs, panes, and agents before and after the probes. Thus the negative case proves refusal at host reachability rather than treating executable presence as readiness, and it proves no Duo authority/workspace/host launch mutation. + +## Assumptions and remaining boundary + +1. A missing local authority store is `initializable` and passes, as the contract explicitly specifies. Both preflights proved doctor did not create it. +2. Reachability is established by the explicitly selected local Unix socket, not ambient vendor markers. The host process and every selected path were under `$RUN`. +3. OpenCode invocation is loader invocation only. Model consumption, launch/send/observe behavior, and instruction adherence are deliberately not inferred. +4. Herdr 0.9.0 can prove local reachability for preflight, but cannot substitute for the complete suite's exact Herdr 0.8.2 pin. +5. The existing out-of-scope dirty checkout state affects the human-readable Duo version suffix, not the captured full source commit or executable identity. + +## Verification and scope result + +- `validate.sh` asserts a Step 09 pass only when the launcher is exactly OpenCode `1.18.31` with accepted executable SHA-256 `f9dab322…e4a11`; it cannot bless an ineligible version. +- The validator checks canonical loader identity, plugin/MCP/session boundaries, all positive required checks, the negative stage-specific failure, and no-write/no-launch assertions. +- `git diff --check -- evidence/portable-launcher-conformance/step-09` passed. + +**This evidence meets Step 09's seal condition.** Step 10 remains open and must use the full suite's exact host/runtime fixture before any OpenCode launcher-support claim. + +Cross-repo impact: **unblocks work in `duo-lab`** by confirming the accepted OpenCode pin (`1.18.31`, executable SHA-256 `f9dab32248695e9ebd56b16a1921798fd85112cf5a69c7dfd0cabc1e17be4a11`) on 2026-09-18 for deterministic installed-skill discovery/loader invocation and local preflight. It does not invalidate the 2026-09-17 duo-lab assessment at `caf02f7eb325b1c935d0cf793bc53fbb3a3f1a65`. Proposed handoff: “Capture OpenCode 1.18.31 full-suite runtime evidence,” body: consume Duo Step 09's exact launcher/skill/preflight pins, re-verify the live launcher and provision exact Pi 0.83.0 plus Herdr 0.8.2/protocol 20/schema `c48f…b150`; seal when the complete launcher-neutral scenario passes; origin: `duo`, portable-launcher-conformance Step 09, observed 2026-09-18. Author it in `duo-lab` only after user approval. diff --git a/evidence/portable-launcher-conformance/step-09/validate.sh b/evidence/portable-launcher-conformance/step-09/validate.sh new file mode 100755 index 0000000..1ab5f70 --- /dev/null +++ b/evidence/portable-launcher-conformance/step-09/validate.sh @@ -0,0 +1,97 @@ +#!/bin/sh +set -eu + +root=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd) +summary="$root/probe-summary.json" + +jq -e ' + .schema == "duo.portable-launcher-step09-evidence/v1" and + .observed_at_utc.start[0:10] == "2026-09-18" and + .observed_at_utc.finish[0:10] == "2026-09-18" and + .checkout.commit == .duo.commit and + .opencode.version == "1.18.31" and + .opencode.accepted_version == "1.18.31" and + .opencode.executable_sha256 == "sha256:f9dab32248695e9ebd56b16a1921798fd85112cf5a69c7dfd0cabc1e17be4a11" and + .opencode.accepted_executable_sha256 == .opencode.executable_sha256 and + .opencode.matches_accepted_pin == true and + .opencode.matches_manifest_tested_version == true and + .scope_result.step09 == "pass" and + .scope_result.seal_condition_met == true and + .scope_result.exact_accepted_launcher_used == true and + .scope_result.full_opencode_suite_claimed == false and + .scope_result.launcher_support_claimed == false and + .installation.schema == "duo.external/v1" and + .installation.operation == "projection.install" and + .installation.target == "portable_launchers" and + .installation.state == "current" and + .skill.name == .opencode_discovery.name and + .skill.format_version == "duo.skill/duo-delegation-loop/v1" and + .skill.content_digest == "sha256:6f8bc16b656f564a949c99f12d4cf0bcd04ec2c9b49a8b74fb65b2e355b5d182" and + .skill.installed_file == ($__run + "/workspace/.agents/skills/duo-delegation-loop/SKILL.md") and + .skill.stamp_schema == "duo.projection-stamp/v1" and + .skill.stamp_target == "portable_launchers" and + .skill.installed_bytes_match_canonical == true and + .opencode_discovery.matching_skill_records == 1 and + .opencode_discovery.location == .skill.installed_file and + .opencode_discovery.loaded_body_sha256 == .opencode_discovery.canonical_body_sha256 and + .opencode_discovery.loaded_body_matches_canonical == true and + .opencode_discovery.body_has_launch_sequence == true and + .opencode_discovery.body_has_install_guidance == true and + .opencode_discovery.loader_invoked == true and + .opencode_discovery.model_turn_invoked == false and + .opencode_discovery.behavioral_model_adherence_claimed == false and + .opencode_discovery.sessions_created == 0 and + .opencode_discovery.accounts_present == 0 and + .opencode_discovery.credentials_present == 0 and + .isolation.external_plugins_disabled_by_pure == true and + (.isolation.resolved_plugins | length) == 0 and + .isolation.mcp_configured == false and + .isolation.terminal_input_used == false and + .isolation.threads_created == false and + .positive_preflight.schema == "duo.launcher-preflight/v1" and + .positive_preflight.status == "ready" and + .positive_preflight.authority_scope == "local" and + .positive_preflight.host_source == "explicit-flag" and + ([.positive_preflight.checks[] | select(.required == true and .status == "pass" and .code == "ok")] | length) == 7 and + ([.positive_preflight.checks[] | select(.id == "host_compatibility" and .required == false and .status == "warning" and .code == "host.compatibility_unverified")] | length) == 1 and + .positive_preflight.skill_projection_state == "current" and + .positive_preflight.host_reachable == true and + .positive_preflight.duo_authority_entries_before == 0 and + .positive_preflight.duo_authority_entries_after == 0 and + .positive_preflight.workspace_payload_manifest_sha256_before == .positive_preflight.workspace_payload_manifest_sha256_after and + .positive_preflight.authority_store_created == false and + .positive_preflight.workspace_changed == false and + .negative_preflight.schema == "duo.launcher-preflight/v1" and + .negative_preflight.status == "not_ready" and + .negative_preflight.authority_scope == "unavailable" and + .negative_preflight.failed_check.id == "host_reachability" and + .negative_preflight.failed_check.stage == "host_reachability" and + .negative_preflight.failed_check.status == "fail" and + .negative_preflight.failed_check.code == "host.unreachable" and + (.negative_preflight.failed_check.action | contains("$RUN/xdg/runtime/no-server/herdr.sock")) and + .negative_preflight.compatibility_check.status == "not_checked" and + .negative_preflight.compatibility_check.code == "prerequisite.not_reached" and + .negative_preflight.duo_authority_entries_before == 0 and + .negative_preflight.duo_authority_entries_after == 0 and + .negative_preflight.workspace_payload_manifest_sha256_before == .negative_preflight.workspace_payload_manifest_sha256_after and + .negative_preflight.launch_command_invoked == false and + .negative_preflight.authority_store_created == false and + .negative_preflight.workspace_changed == false and + .host_state.fixture_objects_before == .host_state.fixture_objects_after and + .host_state.fixture_objects_after == {"workspaces":0,"tabs":0,"panes":0,"agents":0} and + .host_state.launch_observed == false and + .herdr.compatibility == "unverified" and + .herdr.required_full_suite_pin_available == false and + .cleanup.server_stopped_via_local_control == true and + .cleanup.server_process_exited == true and + .cleanup.socket_removed == true and + .cleanup.run_root_removed == true and + .scrub.status == "pass" +' --arg __run '$RUN' "$summary" >/dev/null + +if grep -En '/tmp/ds09|/home/dev|Bearer[[:space:]]|user_code|"access_token"|"refresh_token"|api[_-]?key|client[_-]?secret' "$summary" "$root/report.md"; then + echo "step-09 evidence scrub check failed" >&2 + exit 1 +fi + +printf '%s\n' "step-09 evidence validation: pass" diff --git a/evidence/portable-launcher-conformance/step-10/prerequisite-summary.json b/evidence/portable-launcher-conformance/step-10/prerequisite-summary.json new file mode 100644 index 0000000..5821456 --- /dev/null +++ b/evidence/portable-launcher-conformance/step-10/prerequisite-summary.json @@ -0,0 +1,209 @@ +{ + "schema": "duo.portable-launcher-step10-prerequisite-evidence/v1", + "observed_at_utc": "2026-09-18T08:19:25Z", + "host": { + "kernel": "Linux 6.8.0-136-generic", + "architecture": "x86_64" + }, + "checkout": { + "path": "$CHECKOUT", + "branch": "feat/portable-launcher-conformance", + "commit": "99d48b8d2c5fd01db970566bf004185448b9f311", + "preexisting_out_of_scope_changes": true + }, + "launcher": { + "name": "opencode", + "required": { + "version": "1.18.31", + "executable_sha256": "sha256:f9dab32248695e9ebd56b16a1921798fd85112cf5a69c7dfd0cabc1e17be4a11" + }, + "observed": { + "version": "1.18.31", + "executable_sha256": "sha256:f9dab32248695e9ebd56b16a1921798fd85112cf5a69c7dfd0cabc1e17be4a11", + "executable_bytes": 185030784, + "location": "$HOME/.opencode/bin/opencode" + }, + "matches_exact_accepted_pin": true + }, + "duo": { + "source_commit": "99d48b8d2c5fd01db970566bf004185448b9f311", + "build": { + "version": "99d48b8-dirty", + "commit": "99d48b8d2c5fd01db970566bf004185448b9f311", + "build_date": "2026-09-18T08:18:59Z", + "command": "CGO_ENABLED=0 go build -trimpath -ldflags -o $EVIDENCE/.duo-prereq ./cmd/duo", + "executable_sha256": "sha256:2cd183e9f99855a13c8c66864d1926947d922f9b39f1e889f66c5d3c4cd48ca6", + "executable_bytes": 14369268, + "identity_verified_by_version_command": true, + "artifact_retained": false + }, + "manifest": { + "schema": "duo.manifest/v1", + "manifest_digest": "sha256:aeafb5502724a9d68d33a1ea43ee67f65f3f76477a4e115a9babd35ca817a003", + "portable_target": "portable_launchers", + "portable_target_status": "unverified" + } + }, + "skill": { + "name": "duo-delegation-loop", + "format_version": "duo.skill/duo-delegation-loop/v1", + "content_digest": "sha256:6f8bc16b656f564a949c99f12d4cf0bcd04ec2c9b49a8b74fb65b2e355b5d182", + "bytes": 6284, + "matches_manifest": true + }, + "suite": { + "schema": "duo.portable-launcher-conformance-scenario/v1", + "name": "portable-launcher-delegation", + "revision": 1, + "scenario_bytes": 40118, + "manifest_digest": "sha256:01e8e6bd80ceb441be6dc128aca023a1bede31b655e74b12ee3da0c2fc6cc204", + "oracle_digest": "sha256:a305744fc34af96b5bbd916b9de3aa1bc94f59ce4ea9316b62bb0a8dd5dd22c5", + "task_digest": "sha256:5949809812a3ada98c95e77e9942af848e3080e4984df43a2d96fe5a24d5eab2", + "fixture": { + "request_digest": "sha256:b0a32f59630d6398da8d8913744dd7cd50a313d86bf7e833309aad0d47cc9c3b", + "reply_digest": "sha256:b3300f2f6e0338d2b672891be66827d95779a2eabf4a75dcae1b6f56c9a4dc98", + "prompt_digest": "sha256:b40d3a68eb33083b92199d734da2dece28395a6acc6d5e2cb9d7032dae4d11c7", + "conflict_digest": "sha256:dbe904a42a9d9d9cdf7908508fa393ce43c9c53ea16a8cb8c9b82e7005f602bc", + "provider": "openai-codex", + "model_line": "gpt-5.6-luna" + }, + "source_and_fixture_sync_tests_passed": true + }, + "runtime": { + "required": { + "name": "pi", + "version": "0.83.0", + "semantic_format": "pi-session-jsonl/v3", + "adapter_build": "stage1", + "conformance_record": "pi-0.83.0-2026-08-23", + "delivery_asset_sha256": "sha256:2708a3435821fb2e72302ee56d8a07fe62c265214ae8fd342e385c03ef245a23" + }, + "installed": { + "version": "0.84.4", + "executable_sha256": "sha256:5406c369954516fb56879d685e082ff9095cd6e06e41af406f394942377fd4bf", + "executable_bytes": 629, + "location": "$HOME/.npm-global/lib/node_modules/@earendil-works/pi-coding-agent/dist/bundle/cli.js", + "matches_required_version": false + }, + "local_cache": { + "exact_package_archive_present": true, + "package_version": "0.83.0", + "package_archive_sha256": "sha256:7097fe4b38762dda7ec78001e7b90430c849fbaf717325bfe8109744e32255e6", + "package_archive_bytes": 4992066, + "package_integrity": "sha512-uYhF+FsZxogoSX/AxBcUdiY+ZklubwaXyAoEGA2eQwsHcyEAhUYIKh/WLXe/a8+k8eTCmxb+ZN2Zo9mzQtzbWw==", + "cached_launcher_path": "package/dist/cli.js", + "cached_launcher_sha256": "sha256:af302f231437eaf6f37691bce4b34234fcb626bcb5eb3910d4fc3f6519bf78ca", + "cached_launcher_bytes": 681, + "dependency_tree_provisioned": false, + "runnable_exact_executable_available": false + }, + "exact_required_runtime_available": false, + "reason": "The exact package archive is cached, but no runnable Pi 0.83.0 installation with a verified dependency tree and executable identity is provisioned; the only installed Pi is 0.84.4." + }, + "host_adapter": { + "required": { + "name": "herdr", + "version": "0.8.2", + "protocol": "herdr-socket-api/20", + "api_schema_sha256": "sha256:c48f1f54ee0150ca27e11fd44455fe94aeadb20fdf4e4a62393ed822a4e5b150", + "adapter_build": "stage1", + "conformance_record": "notes/19-herdr-probes.md@2026-08-23" + }, + "installed_on_path": { + "version": "0.9.0", + "executable_sha256": "sha256:5ef212a3f142f902b1a4eb3f7a76827ded9f313804c580c48297c060ac1fcac0", + "executable_bytes": 39728936, + "api_schema_sha256": "sha256:226d4ecbd128d2e6bc84e4c8ddcec21ba9c7e51a0aafffcf087111ead3f1fa9a", + "location": "$HERDR_CHECKOUT/target/release/herdr", + "matches_required_identity": false + }, + "local_exact_version_candidates": [ + { + "location": "$HERDR_CHECKOUT/target/debug/herdr", + "version": "0.8.2", + "executable_sha256": "sha256:7e1a54d98765f91eca532752369d7c01f91b971883599b393a1f014d332bb85e", + "executable_bytes": 228542880, + "api_schema_sha256": "sha256:24e62c2d8be455ba7872c0e66bfbbf77e8403c29c3f5b1ff10c8ff3c112f4e63", + "matches_required_schema": false + }, + { + "location": "$HERDR_CHECKOUT/target/release/deps/herdr-dacc253a0a16c2ca", + "version": "0.8.2", + "executable_sha256": "sha256:c1cb6b1338d0f9e70cd8aa35828cec0635d64865aa90535d4350168750cf4ccf", + "executable_bytes": 32012656, + "api_schema_sha256": "sha256:24e62c2d8be455ba7872c0e66bfbbf77e8403c29c3f5b1ff10c8ff3c112f4e63", + "matches_required_schema": false + } + ], + "protocol_probe_performed": false, + "protocol_probe_reason": "No fixture server was started after exact prerequisites failed.", + "exact_required_host_available": false, + "reason": "The locally found Herdr 0.8.2 executables export schema 24e62c2d... rather than the accepted c48f1f54... identity; version text alone is ineligible." + }, + "prerequisites": [ + {"name": "pinned_launcher_binary", "required": true, "status": "available"}, + {"name": "pinned_duo_build_identity", "required": true, "status": "available_for_inspection"}, + {"name": "current_canonical_skill_identity", "required": true, "status": "available"}, + {"name": "launcher_neutral_suite_revision", "required": true, "status": "available"}, + {"name": "pinned_pi_0_83_0_runnable_artifact", "required": true, "status": "unavailable"}, + {"name": "pinned_herdr_0_8_2_protocol_20_schema", "required": true, "status": "unavailable"}, + {"name": "isolated_inner_provider_credentials", "required": true, "status": "not_checked"}, + {"name": "timeout_induction_socket_probe", "required": true, "status": "not_checked"}, + {"name": "supported_admitted_then_blocked_producer", "required": true, "status": "unavailable"} + ], + "canonical_blocked_prerequisite": { + "name": "supported_admitted_then_blocked_producer", + "scenario_status": "unavailable", + "validator_rejects_blocked_case_pass": true, + "available": false + }, + "eligibility": { + "all_exact_prerequisites_available": false, + "result": "blocked", + "first_identity_blockers": [ + "pinned_pi_0_83_0_runnable_artifact", + "pinned_herdr_0_8_2_protocol_20_schema" + ], + "complete_suite_can_pass_current_revision": false, + "step10_seal_condition_met": false, + "matter_seal_condition_met": false + }, + "actions": { + "fixture_setup_invoked": false, + "fixture_root_created": false, + "fixture_workspace_created_or_mutated": false, + "fixture_authority_created_or_mutated": false, + "herdr_server_started": false, + "launcher_version_probe_invoked": true, + "launcher_session_invoked": false, + "launcher_thread_created": false, + "model_turn_invoked": false, + "duo_version_or_manifest_probe_invoked": true, + "duo_session_launch_invoked": false, + "account_billed_action_invoked": false, + "credentials_inspected": false, + "production_or_shared_runtime_state_accessed": false, + "binary_downloaded": false + }, + "search_scope": { + "pi": ["$HOME/.local/bin", "$HOME/.npm-global", "$HOME/.npm", "$HOME/.cache", "$HOME/.local/share/pnpm"], + "herdr": ["$HOME/.local/bin", "$HOME/.cargo/bin", "$HERDR_CHECKOUT/target", "$HOME/.cache"] + }, + "resume_condition": { + "required": [ + "Provision a runnable Pi 0.83.0 artifact and complete dependency tree locally, then record its provenance and executable SHA-256 without widening the pin.", + "Provision a Herdr executable that is simultaneously version 0.8.2, protocol 20, and API-schema SHA-256 c48f1f54ee0150ca27e11fd44455fe94aeadb20fdf4e4a62393ed822a4e5b150; reject the current 24e62c2d... candidates.", + "Add supported, version-pinned production evidence for deterministic admitted-then-blocked Pi behavior and update the canonical suite only through the owning runtime/host capability work.", + "Rebuild Duo from the then-current exact checkout commit and retain the per-run build identity; reverify the OpenCode and skill/suite pins.", + "Confirm isolated openai-codex credentials without recording secret material and complete the timeout socket probe in the disposable fixture.", + "Before any model turn or launcher session, obtain a parent decision reconciling the no-thread constraint with Step 10's model-turn requirement." + ] + }, + "scrub": { + "status": "pass", + "absolute_checkout_paths_present": false, + "credentials_present": false, + "private_transcripts_present": false, + "raw_vendor_state_present": false + } +} diff --git a/evidence/portable-launcher-conformance/step-10/report.md b/evidence/portable-launcher-conformance/step-10/report.md new file mode 100644 index 0000000..60e6a62 --- /dev/null +++ b/evidence/portable-launcher-conformance/step-10/report.md @@ -0,0 +1,71 @@ +# Step 10 — Complete OpenCode launcher suite prerequisite gate + +- **Observed:** 2026-09-18T08:19:25Z +- **Checkout:** `$CHECKOUT`, branch `feat/portable-launcher-conformance`, commit `99d48b8d2c5fd01db970566bf004185448b9f311` +- **Outcome:** **BLOCKED before fixture setup, launcher session, model turn, or Duo launch** +- **Step 10 seal condition:** **not met** +- **Matter seal condition:** **not met** + +The complete launcher-neutral OpenCode suite cannot run honestly under the repository's exact accepted pins in this checkout. OpenCode itself is exact, but no eligible runnable Pi 0.83.0 installation was found, the local Herdr 0.8.2 executables fail the accepted API-schema identity, and the canonical suite still declares its admitted-then-blocked producer unavailable and rejects any blocked-case pass. A newer installed runtime or a same-version/different-schema host was not relabeled as accepted. + +## Exact required and observed identities + +| Component | Required | Observed | Verdict | +|---|---|---|---| +| Outer OpenCode | `1.18.31`, executable SHA-256 `f9dab32248695e9ebd56b16a1921798fd85112cf5a69c7dfd0cabc1e17be4a11` | Exact version and digest; 185,030,784 bytes | eligible | +| Runtime Pi | `0.83.0`; `pi-session-jsonl/v3`; adapter `pi` build `stage1`; record `pi-0.83.0-2026-08-23`; inject SHA-256 `2708a3435821fb2e72302ee56d8a07fe62c265214ae8fd342e385c03ef245a23` | Installed executable is `0.84.4`, SHA-256 `5406c369954516fb56879d685e082ff9095cd6e06e41af406f394942377fd4bf` | **ineligible** | +| Local Pi cache | runnable exact artifact and dependency tree | Exact `0.83.0` package archive is cached (4,992,066 bytes; SHA-256 `7097fe4b38762dda7ec78001e7b90430c849fbaf717325bfe8109744e32255e6`), but it is not an installed runnable dependency-complete executable | **insufficient** | +| Host Herdr | `0.8.2`; `herdr-socket-api/20`; API-schema SHA-256 `c48f1f54ee0150ca27e11fd44455fe94aeadb20fdf4e4a62393ed822a4e5b150`; adapter `herdr` build `stage1`; record `notes/19-herdr-probes.md@2026-08-23` | PATH is `0.9.0`/schema `226d4ecb…`; two local `0.8.2` executables both export schema `24e62c2d8be455ba7872c0e66bfbbf77e8403c29c3f5b1ff10c8ff3c112f4e63` | **ineligible** | +| Skill | `duo.skill/duo-delegation-loop/v1` plus exact current digest | `sha256:6f8bc16b656f564a949c99f12d4cf0bcd04ec2c9b49a8b74fb65b2e355b5d182`, 6,284 bytes; manifest agrees | eligible | +| Suite | `portable-launcher-delegation`, revision 1 | scenario SHA-256 `01e8e6bd80ceb441be6dc128aca023a1bede31b655e74b12ee3da0c2fc6cc204`; oracle `a305744f…`; task `59498098…`; fixture/source tests passed | exact, but cannot pass while blocked producer is unavailable | + +The local Herdr candidates were rejected on the accepted three-part identity, not on filename or version text. Their executable SHA-256 values are `7e1a54d98765f91eca532752369d7c01f91b971883599b393a1f014d332bb85e` (debug) and `c1cb6b1338d0f9e70cd8aa35828cec0635d64865aa90535d4350168750cf4ccf` (release dependency artifact). Both independently exported schema `24e62c2d…`, so neither can stand in for `c48f1f54…`. No Herdr server was started merely to probe protocol after this mismatch was established. + +## Checkout-built Duo identity + +A prerequisite-only build was made inside this evidence directory, queried with the no-store `version` and `manifest` commands, hashed, and removed. It did not prepare a fixture or invoke a session. + +- version: `99d48b8-dirty` +- full commit: `99d48b8d2c5fd01db970566bf004185448b9f311` +- build date: `2026-09-18T08:18:59Z` +- executable: 14,369,268 bytes, SHA-256 `2cd183e9f99855a13c8c66864d1926947d922f9b39f1e889f66c5d3c4cd48ca6` +- manifest: `duo.manifest/v1`, semantic digest `sha256:aeafb5502724a9d68d33a1ea43ee67f65f3f76477a4e115a9babd35ca817a003` +- portable target: `portable_launchers`, status `unverified`, with the exact skill identity above + +The temporary executable was deliberately not retained. A resumed live run must build and retain a new exact per-run identity from the then-current checkout rather than reuse this observation as an artifact. + +## Canonical suite blocker independent of local binaries + +Revision 1 declares `supported_admitted_then_blocked_producer` as `unavailable`. The offline validator rejects any blocked-case stage marked pass, and the runner has no supported blocked control. Consequently, even provisioning both exact binaries would not permit every named scenario to pass today. The honest prerequisite is the contract's supported, version-pinned production condition source that deterministically produces `blocked` after prompt admission, with a non-terminal controller and exact runtime-instance evidence. A fake event, different runtime for one case, PTY input, model persuasion, or launcher-driver patch would be ineligible. + +## Stop boundary and assumptions + +No fixture root, fixture workspace, authority store, Herdr server, OpenCode session/thread, model turn, Duo session launch, billed action, or credential inspection occurred. No production/account/shared runtime state was accessed or mutated, no binary was downloaded, and no fixture authority/workspace was created or changed. Only the expressly allowed local installation/cache reads, local version/hash/schema inspection, focused offline Go tests, and the prerequisite-only Duo build were performed. + +Assumptions: + +1. A cached npm package archive is not treated as a runnable Pi artifact. Its exact runtime dependencies were not provisioned or verified, and the installed command remains 0.84.4. +2. A Herdr binary saying `0.8.2` is insufficient when its exported schema digest differs from the accepted pin. Protocol was intentionally not inferred or live-probed after that decisive mismatch. +3. Credentials were left uninspected because earlier exact-identity prerequisites already forced a pre-setup stop. +4. The Duo `-dirty` suffix honestly reflects known pre-existing out-of-scope residue. Its contents were not inspected or incorporated; full commit, date, size, digest, and manifest identify the prerequisite build. + +## Exact resume condition + +Resume only after all of the following are true: + +1. A runnable, dependency-complete Pi 0.83.0 artifact is locally provisioned and its provenance and executable SHA-256 are captured without widening the version. +2. A Herdr artifact simultaneously proves version 0.8.2, protocol 20, and API-schema SHA-256 `c48f1f54…b150`; the current `24e62c2d…` candidates remain rejected. +3. Owning runtime/host capability work supplies supported, version-pinned admitted-then-blocked evidence and the canonical suite no longer marks that prerequisite unavailable. +4. The current checkout's Duo is rebuilt and retained for the run; OpenCode, skill, scenario, oracle, and fixture identities are reverified. +5. Isolated `openai-codex` credentials are confirmed without recording secret material, and the timeout socket probe succeeds in the disposable fixture. +6. Before any model turn or launcher session, the parent explicitly reconciles the user's no-thread constraint with Step 10's model-turn requirement. + +Until then, neither Step 10 nor the Matter seal condition is met. + +## Verification + +- `go test ./internal/conformance/portablelauncher -run 'TestCanonicalScenario|TestCollectorMandatoryBlockedPrerequisitePreventsPass' -count=1` — passed. +- `./evidence/portable-launcher-conformance/step-10/validate.sh` — asserts the exact accepted identities, rejects the installed/newer and same-version/wrong-schema substitutes, and requires the blocked/no-action result. +- `git diff --check -- evidence/portable-launcher-conformance/step-10` — required final check. + +Cross-repo impact: **no cross-repo impact**. This capture confirms, rather than changes, the existing `duo-lab` prerequisite boundary for Pi `0.83.0` and Herdr `0.8.2`/protocol `20`/schema `c48f1f54…b150` as of 2026-09-18; it neither unblocks work there nor invalidates its accepted pin. No handoff is proposed. diff --git a/evidence/portable-launcher-conformance/step-10/validate.sh b/evidence/portable-launcher-conformance/step-10/validate.sh new file mode 100755 index 0000000..c2ef7ef --- /dev/null +++ b/evidence/portable-launcher-conformance/step-10/validate.sh @@ -0,0 +1,114 @@ +#!/bin/sh +set -eu + +root=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd) +summary="$root/prerequisite-summary.json" + +jq -e ' + . as $evidence | + .schema == "duo.portable-launcher-step10-prerequisite-evidence/v1" and + .observed_at_utc[0:10] == "2026-09-18" and + .checkout.branch == "feat/portable-launcher-conformance" and + (.checkout.commit | test("^[0-9a-f]{40}$")) and + + .launcher.name == "opencode" and + .launcher.required.version == "1.18.31" and + .launcher.required.executable_sha256 == "sha256:f9dab32248695e9ebd56b16a1921798fd85112cf5a69c7dfd0cabc1e17be4a11" and + .launcher.observed.version == .launcher.required.version and + .launcher.observed.executable_sha256 == .launcher.required.executable_sha256 and + .launcher.matches_exact_accepted_pin == true and + + .duo.source_commit == .checkout.commit and + .duo.build.commit == .checkout.commit and + .duo.build.version == "99d48b8-dirty" and + .duo.build.executable_sha256 == "sha256:2cd183e9f99855a13c8c66864d1926947d922f9b39f1e889f66c5d3c4cd48ca6" and + .duo.build.identity_verified_by_version_command == true and + .duo.build.artifact_retained == false and + .duo.manifest.schema == "duo.manifest/v1" and + .duo.manifest.portable_target == "portable_launchers" and + .duo.manifest.portable_target_status == "unverified" and + + .skill.name == "duo-delegation-loop" and + .skill.format_version == "duo.skill/duo-delegation-loop/v1" and + .skill.content_digest == "sha256:6f8bc16b656f564a949c99f12d4cf0bcd04ec2c9b49a8b74fb65b2e355b5d182" and + .skill.matches_manifest == true and + + .suite.schema == "duo.portable-launcher-conformance-scenario/v1" and + .suite.name == "portable-launcher-delegation" and + .suite.revision == 1 and + .suite.manifest_digest == "sha256:01e8e6bd80ceb441be6dc128aca023a1bede31b655e74b12ee3da0c2fc6cc204" and + .suite.oracle_digest == "sha256:a305744fc34af96b5bbd916b9de3aa1bc94f59ce4ea9316b62bb0a8dd5dd22c5" and + .suite.task_digest == "sha256:5949809812a3ada98c95e77e9942af848e3080e4984df43a2d96fe5a24d5eab2" and + .suite.source_and_fixture_sync_tests_passed == true and + + .runtime.required.version == "0.83.0" and + .runtime.required.semantic_format == "pi-session-jsonl/v3" and + .runtime.required.adapter_build == "stage1" and + .runtime.required.conformance_record == "pi-0.83.0-2026-08-23" and + .runtime.required.delivery_asset_sha256 == "sha256:2708a3435821fb2e72302ee56d8a07fe62c265214ae8fd342e385c03ef245a23" and + .runtime.installed.version == "0.84.4" and + .runtime.installed.version != .runtime.required.version and + .runtime.installed.matches_required_version == false and + .runtime.local_cache.exact_package_archive_present == true and + .runtime.local_cache.package_version == .runtime.required.version and + .runtime.local_cache.package_archive_sha256 == "sha256:7097fe4b38762dda7ec78001e7b90430c849fbaf717325bfe8109744e32255e6" and + .runtime.local_cache.dependency_tree_provisioned == false and + .runtime.local_cache.runnable_exact_executable_available == false and + .runtime.exact_required_runtime_available == false and + + .host_adapter.required.version == "0.8.2" and + .host_adapter.required.protocol == "herdr-socket-api/20" and + .host_adapter.required.api_schema_sha256 == "sha256:c48f1f54ee0150ca27e11fd44455fe94aeadb20fdf4e4a62393ed822a4e5b150" and + .host_adapter.required.adapter_build == "stage1" and + .host_adapter.required.conformance_record == "notes/19-herdr-probes.md@2026-08-23" and + .host_adapter.installed_on_path.version == "0.9.0" and + .host_adapter.installed_on_path.matches_required_identity == false and + (.host_adapter.local_exact_version_candidates | length) == 2 and + ([.host_adapter.local_exact_version_candidates[] | + select(.version == "0.8.2" and + .api_schema_sha256 == "sha256:24e62c2d8be455ba7872c0e66bfbbf77e8403c29c3f5b1ff10c8ff3c112f4e63" and + .matches_required_schema == false)] | length) == 2 and + ([.host_adapter.local_exact_version_candidates[] | + select(.api_schema_sha256 == $evidence.host_adapter.required.api_schema_sha256)] | length) == 0 and + .host_adapter.protocol_probe_performed == false and + .host_adapter.exact_required_host_available == false and + + .canonical_blocked_prerequisite.name == "supported_admitted_then_blocked_producer" and + .canonical_blocked_prerequisite.scenario_status == "unavailable" and + .canonical_blocked_prerequisite.validator_rejects_blocked_case_pass == true and + .canonical_blocked_prerequisite.available == false and + + ([.prerequisites[] | select(.required == true and .status == "unavailable")] | length) >= 3 and + ([.prerequisites[] | select(.name == "pinned_pi_0_83_0_runnable_artifact" and .status == "unavailable")] | length) == 1 and + ([.prerequisites[] | select(.name == "pinned_herdr_0_8_2_protocol_20_schema" and .status == "unavailable")] | length) == 1 and + ([.prerequisites[] | select(.name == "supported_admitted_then_blocked_producer" and .status == "unavailable")] | length) == 1 and + .eligibility.all_exact_prerequisites_available == false and + .eligibility.result == "blocked" and + .eligibility.complete_suite_can_pass_current_revision == false and + .eligibility.step10_seal_condition_met == false and + .eligibility.matter_seal_condition_met == false and + + .actions.fixture_setup_invoked == false and + .actions.fixture_root_created == false and + .actions.fixture_workspace_created_or_mutated == false and + .actions.fixture_authority_created_or_mutated == false and + .actions.herdr_server_started == false and + .actions.launcher_session_invoked == false and + .actions.launcher_thread_created == false and + .actions.model_turn_invoked == false and + .actions.duo_session_launch_invoked == false and + .actions.account_billed_action_invoked == false and + .actions.credentials_inspected == false and + .actions.production_or_shared_runtime_state_accessed == false and + .actions.binary_downloaded == false and + .scrub.status == "pass" +' "$summary" >/dev/null + +# The evidence must remain path- and secret-scrubbed. Tokens such as $HOME, +# $CHECKOUT, and $HERDR_CHECKOUT are intentional stable identities. +if grep -En '/home/dev|/tmp/|Bearer[[:space:]]|user_code|"access_token"|"refresh_token"|api[_-]?key|client[_-]?secret' "$summary" "$root/report.md"; then + echo "step-10 evidence scrub check failed" >&2 + exit 1 +fi + +printf '%s\n' "step-10 prerequisite evidence validation: blocked (pass)" diff --git a/evidence/portable-launcher-conformance/step-11/probe-summary.json b/evidence/portable-launcher-conformance/step-11/probe-summary.json new file mode 100644 index 0000000..cf38492 --- /dev/null +++ b/evidence/portable-launcher-conformance/step-11/probe-summary.json @@ -0,0 +1,203 @@ +{ + "schema": "duo.portable-launcher-step11-evidence/v1", + "observed_at_utc": {"start": "2026-09-18T08:28:56Z", "finish": "2026-09-18T08:33:22Z"}, + "host": {"kernel": "Linux 6.8.0-136-generic", "architecture": "x86_64"}, + "checkout": { + "path": "$CHECKOUT", + "branch": "feat/portable-launcher-conformance", + "commit": "40a2d24652c2543b4cde3bc1ebbd164c7b8952c9", + "describe": "40a2d24-dirty", + "preexisting_out_of_scope_changes": true + }, + "codex": { + "version": "0.154.0", + "version_output": "codex-cli 0.154.0", + "command_path": "$CODEX_COMMAND", + "javascript_entrypoint": "$CODEX_JS_ENTRYPOINT", + "javascript_entrypoint_bytes": 8790, + "javascript_entrypoint_sha256": "sha256:61b0194f3bb6534439c8d26a3ed57d0805f84b884588b761795323eeb92fcf70", + "native_package_name": "@openai/codex", + "native_package_version": "0.154.0-linux-x64", + "native_executable_path": "$CODEX_NATIVE", + "native_executable_bytes": 262858016, + "native_executable_sha256": "sha256:3188814c35471432d4123203e0eb38e5bddc60226e3d7ddf0e59e649ea140022", + "accepted_version": "0.154.0", + "accepted_native_executable_sha256": "sha256:3188814c35471432d4123203e0eb38e5bddc60226e3d7ddf0e59e649ea140022", + "matches_accepted_pin": true, + "matches_manifest_tested_version": true, + "diagnostic_executable": "$CODEX_NATIVE" + }, + "duo": { + "version": "40a2d24-dirty", + "commit": "40a2d24652c2543b4cde3bc1ebbd164c7b8952c9", + "build_date": "2026-09-18T08:28:56Z", + "build_command": "CGO_ENABLED=0 go build -trimpath -ldflags -o $RUN/bin/duo ./cmd/duo", + "executable_path": "$RUN/bin/duo", + "executable_bytes": 14369268, + "executable_sha256": "sha256:546be61fc15d0744d9d0758e6e2629cf154dfbdc2df1eebf72aa5884787de2c7", + "manifest_digest": "sha256:5d247c2033264777e20566c0bbc53842ebed5078f4135ca6a899ecfea8bf7404" + }, + "skill": { + "name": "duo-delegation-loop", + "format_version": "duo.skill/duo-delegation-loop/v1", + "content_digest": "sha256:6f8bc16b656f564a949c99f12d4cf0bcd04ec2c9b49a8b74fb65b2e355b5d182", + "size_bytes": 6284, + "canonical_path": "$CHECKOUT/skills/duo-delegation-loop/SKILL.md", + "installation_id": "7e04a954a123f6fc2e1129d5f8e9e752", + "installed_root": "$RUN/workspace/.agents/skills/duo-delegation-loop", + "installed_file": "$RUN/workspace/.agents/skills/duo-delegation-loop/SKILL.md", + "installed_stamp": "$RUN/workspace/.agents/skills/duo-delegation-loop/.duo-generated.json", + "stamp_schema": "duo.projection-stamp/v1", + "stamp_target": "portable_launchers", + "stamp_tested_version_range": "amp=0.0.1789675234-g2899fe;opencode=1.18.31;codex=0.154.0", + "stamp_sha256": "sha256:2a3e5e88d714cfa737e5d8d50a96d1dfb0bf1ecee7edcc02dc3ea59af236fb9d", + "installed_bytes_match_canonical": true + }, + "config": { + "schema": "duo.config/v3", + "path": "$RUN/xdg/config/duo/duo.config.yaml", + "source_bytes_sha256": "sha256:1b4cb5fafef7a324beb46b8d8529ee76d4cc1218c41d5da24251b0872b293840", + "effective_digest": "sha256:92db1583823b8d8ce7384449896cea6c641b3b4c7df5d50ce56d43a0092826ae", + "presets": ["builder"] + }, + "authority": { + "path": "$RUN/xdg/data/duo/duo.db", + "state": "initializable", + "present": false, + "healthy": true, + "schema_version": 0, + "writer_active": false + }, + "herdr": { + "version": "0.9.0", + "command_path": "$HERDR_COMMAND", + "resolved_path": "$HERDR_RESOLVED", + "executable_bytes": 39728936, + "executable_sha256": "sha256:5ef212a3f142f902b1a4eb3f7a76827ded9f313804c580c48297c060ac1fcac0", + "protocol_identity": "herdr-socket-api/22", + "schema_export_sha256": "sha256:226d4ecbd128d2e6bc84e4c8ddcec21ba9c7e51a0aafffcf087111ead3f1fa9a", + "compatibility": "unverified", + "required_full_suite_pin_available": false, + "required_full_suite_pin": { + "version": "0.8.2", + "protocol_identity": "herdr-socket-api/20", + "schema_digest": "sha256:c48f1f54ee0150ca27e11fd44455fe94aeadb20fdf4e4a62393ed822a4e5b150" + } + }, + "installation": { + "exit_code": 0, + "schema": "duo.external/v1", + "operation": "projection.install", + "target": "portable_launchers", + "state": "current", + "changed": true + }, + "codex_discovery": { + "command": "$CODEX_NATIVE debug prompt-input Use-the-duo-delegation-loop-skill", + "exit_code": 0, + "matching_skill_records": 1, + "name": "duo-delegation-loop", + "skill_root": "$RUN/workspace/.agents/skills", + "location": "$RUN/workspace/.agents/skills/duo-delegation-loop/SKILL.md", + "description_sha256": "sha256:47c044a8e999289d69dd7912f007af0d7f11b6e10a8f5ffe28e730c7139cb85e", + "canonical_file_digest_matches": true, + "loader_invoked": true, + "model_visible_entry_injected": true, + "invocation_boundary": "Codex debug prompt-input invoked deterministic project skill discovery and frontmatter parsing, then injected the canonical skill name, description, and installed SKILL.md locator into model-visible prompt input; it did not run a model or load behavioral adherence evidence.", + "model_turn_invoked": false, + "behavioral_model_adherence_claimed": false, + "threads_created": 0, + "sessions_created": 0, + "credentials_used": false + }, + "isolation": { + "run_root": "$RUN", + "home": "$RUN/noauth/home", + "codex_home": "$RUN/noauth/codex", + "workspace": "$RUN/workspace", + "clean_environment_used": true, + "real_user_skill_tree_written": false, + "account_credentials_used": false, + "installed_plugins": [], + "configured_mcp_servers": [], + "plugins_enabled": false, + "mcp_enabled": false, + "terminal_input_used": false, + "system_skills_bootstrapped_in_disposable_codex_home": true + }, + "positive_preflight": { + "exit_code": 0, + "schema": "duo.launcher-preflight/v1", + "status": "ready", + "authority_scope": "local", + "host_source": "explicit-flag", + "checks": [ + {"id": "duo_executable", "stage": "executable", "required": true, "status": "pass", "code": "ok"}, + {"id": "effective_config", "stage": "config", "required": true, "status": "pass", "code": "ok"}, + {"id": "authority_store", "stage": "authority", "required": true, "status": "pass", "code": "ok"}, + {"id": "workspace", "stage": "workspace", "required": true, "status": "pass", "code": "ok"}, + {"id": "host_selection", "stage": "host_selection", "required": true, "status": "pass", "code": "ok"}, + {"id": "host_reachability", "stage": "host_reachability", "required": true, "status": "pass", "code": "ok"}, + {"id": "skill_projection", "stage": "skill_projection", "required": true, "status": "pass", "code": "ok"}, + {"id": "host_compatibility", "stage": "host_reachability", "required": false, "status": "warning", "code": "host.compatibility_unverified"} + ], + "skill_projection_state": "current", + "host_reachable": true, + "duo_authority_entries_before": 0, + "duo_authority_entries_after": 0, + "workspace_payload_manifest_sha256_before": "sha256:bbf0034693c602bd9ecebe65a6f21caf0e8c1556cac49c9cbb46628cb385efc2", + "workspace_payload_manifest_sha256_after": "sha256:bbf0034693c602bd9ecebe65a6f21caf0e8c1556cac49c9cbb46628cb385efc2", + "authority_store_created": false, + "workspace_changed": false + }, + "negative_preflight": { + "exit_code": 0, + "schema": "duo.launcher-preflight/v1", + "status": "not_ready", + "authority_scope": "unavailable", + "failed_check": { + "id": "host_reachability", + "stage": "host_reachability", + "required": true, + "status": "fail", + "code": "host.unreachable", + "summary": "selected Herdr host did not answer the bounded ping", + "action": "Host reachability stage: start or select the Herdr server at $RUN/xdg/runtime/no-server/herdr.sock and make its socket reachable." + }, + "compatibility_check": {"status": "not_checked", "code": "prerequisite.not_reached"}, + "duo_authority_entries_before": 0, + "duo_authority_entries_after": 0, + "workspace_payload_manifest_sha256_before": "sha256:bbf0034693c602bd9ecebe65a6f21caf0e8c1556cac49c9cbb46628cb385efc2", + "workspace_payload_manifest_sha256_after": "sha256:bbf0034693c602bd9ecebe65a6f21caf0e8c1556cac49c9cbb46628cb385efc2", + "launch_command_invoked": false, + "authority_store_created": false, + "workspace_changed": false + }, + "host_state": { + "fixture_objects_before": {"workspaces": 0, "tabs": 0, "panes": 0, "agents": 0}, + "fixture_objects_after": {"workspaces": 0, "tabs": 0, "panes": 0, "agents": 0}, + "launch_observed": false + }, + "scope_result": { + "step11": "pass", + "seal_condition_met": true, + "exact_accepted_native_launcher_used": true, + "full_codex_suite_claimed": false, + "launcher_support_claimed": false, + "compatibility_warnings": ["Reachable Herdr 0.9.0/protocol 22 is unverified, not the later full-suite Herdr 0.8.2/protocol 20 pin."] + }, + "cleanup": { + "server_stopped_via_local_control": true, + "server_process_exited": true, + "socket_removed": true, + "run_root_removed": true + }, + "scrub": { + "status": "pass", + "run_paths_replaced": true, + "credentials_present": false, + "private_transcripts_present": false, + "raw_vendor_state_present": false, + "unsanitized_environment_values_present": false + } +} diff --git a/evidence/portable-launcher-conformance/step-11/report.md b/evidence/portable-launcher-conformance/step-11/report.md new file mode 100644 index 0000000..9ab7273 --- /dev/null +++ b/evidence/portable-launcher-conformance/step-11/report.md @@ -0,0 +1,90 @@ +# Step 11 — Codex discovery and local-authority preflight + +- **Evidence window:** 2026-09-18 08:28:56Z–08:33:22Z +- **Checkout:** `$CHECKOUT`, branch `feat/portable-launcher-conformance`, commit `40a2d24652c2543b4cde3bc1ebbd164c7b8952c9` +- **Outcome:** **PASS — the exact accepted Codex native launcher passed deterministic discovery and both preflight cases** +- **Step 11 seal condition:** **met** +- **Not claimed:** behavioral model adherence, the complete Codex launcher suite, or overall launcher support. + +## Exact observed pins + +| Component | Observed identity | +|---|---| +| Codex wrapper | CLI `0.154.0`; 8,790-byte JavaScript entrypoint; SHA-256 `61b0194f3bb6534439c8d26a3ed57d0805f84b884588b761795323eeb92fcf70` | +| Codex accepted native | package `@openai/codex` `0.154.0-linux-x64`; 262,858,016-byte ELF x86-64; SHA-256 `3188814c35471432d4123203e0eb38e5bddc60226e3d7ddf0e59e649ea140022` | +| Duo | `40a2d24-dirty`; full commit `40a2d24652c2543b4cde3bc1ebbd164c7b8952c9`; built `2026-09-18T08:28:56Z`; 14,369,268 bytes; SHA-256 `546be61fc15d0744d9d0758e6e2629cf154dfbdc2df1eebf72aa5884787de2c7` | +| Duo manifest | semantic digest `sha256:5d247c2033264777e20566c0bbc53842ebed5078f4135ca6a899ecfea8bf7404` | +| Skill | `duo.skill/duo-delegation-loop/v1@sha256:6f8bc16b656f564a949c99f12d4cf0bcd04ec2c9b49a8b74fb65b2e355b5d182`; 6,284 bytes; installation ID `7e04a954a123f6fc2e1129d5f8e9e752` | +| Config | `duo.config/v3`; source SHA-256 `1b4cb5fafef7a324beb46b8d8529ee76d4cc1218c41d5da24251b0872b293840`; effective digest `sha256:92db1583823b8d8ce7384449896cea6c641b3b4c7df5d50ce56d43a0092826ae` | +| Authority | `$RUN/xdg/data/duo/duo.db`; absent, healthy, locally `initializable`, schema version `0`, no writer | +| Herdr | `0.9.0`; protocol `herdr-socket-api/22`; SHA-256 `5ef212a3f142f902b1a4eb3f7a76827ded9f313804c580c48297c060ac1fcac0`; schema SHA-256 `226d4ecbd128d2e6bc84e4c8ddcec21ba9c7e51a0aafffcf087111ead3f1fa9a` | +| Host | Linux `6.8.0-136-generic`, `x86_64` | + +The accepted Codex pin is the native executable, not the npm JavaScript entrypoint. The deterministic diagnostic was run directly with `$CODEX_NATIVE`; its version, native package version, byte count, and digest exactly match the accepted pin. The wrapper identity is retained separately so its smaller JavaScript digest cannot accidentally satisfy validation. + +## Isolated projection and deterministic invocation boundary + +The run used a mode-0700 disposable root with clean `HOME`, `CODEX_HOME`, XDG roots, workspace, Duo binary, and Herdr state. Checkout-built Duo installed a regular copied projection: + +```text +$RUN/bin/duo install portable-launchers --workspace $RUN/workspace --output json +``` + +Installation returned `projection.install`, `state: current`, and `changed: true`. Installed `SKILL.md` byte-matched the current product source. Its ownership stamp was `duo.projection-stamp/v1`, target `portable_launchers`, with the current manifest and canonical skill digests and tested range `amp=0.0.1789675234-g2899fe;opencode=1.18.31;codex=0.154.0`. + +From the isolated workspace, the accepted native executable ran only Codex's deterministic diagnostic: + +```text +$CODEX_NATIVE debug prompt-input 'Use the duo-delegation-loop skill.' +``` + +It exited 0 and injected exactly one project entry named `duo-delegation-loop` from `$RUN/workspace/.agents/skills/duo-delegation-loop/SKILL.md` into model-visible prompt input. The parsed description digest was `sha256:47c044a8e999289d69dd7912f007af0d7f11b6e10a8f5ffe28e730c7139cb85e`; direct file hashing tied the locator to the canonical full-file digest above. + +At this boundary, **“invokes” means the diagnostic invoked Codex's project skill discovery and frontmatter parser, then injected the skill name, canonical description, and installed locator into the model-visible input list**. It does not mean a model read or followed the body. No model turn, Codex thread/session, or behavioral adherence claim occurred. This is the strongest invocation proof allowed by the explicit no-thread constraint. + +`codex plugin list --json` reported no installed plugins and `codex mcp list --json` reported no configured servers under the isolated roots. The diagnostic bootstrapped Codex's built-in system skills only inside disposable `CODEX_HOME`; those are not plugins and were deleted with the run root. No credentials, terminal input, user-global skill tree, or raw vendor state entered the evidence. + +## Reachable local-authority preflight + +A disposable real Herdr server answered at the explicitly selected isolated Unix socket: + +```text +$RUN/bin/duo doctor --workspace $RUN/workspace \ + --config $RUN/xdg/config/duo/duo.config.yaml \ + --host herdr:$RUN/xdg/config/herdr/sessions/duo-step11/herdr.sock \ + --output json +``` + +Doctor exited 0 with `duo.launcher-preflight/v1`, `status: ready`, `authority_scope: local`, and `host_source: explicit-flag`. All seven required checks passed in contract order. The projected skill was `current` with the same identity and installation ID Codex exposed. + +The informational compatibility check was `warning / host.compatibility_unverified`. Installed Herdr `0.9.0`/protocol `22`/schema `226d…1fa9a` does not match the later complete-suite pin `0.8.2`/protocol `20`/schema `c48f…b150`. Contract §6 makes this informational for preflight, so Step 11 can pass, but this run cannot claim the complete launcher suite. + +## Unreachable local-authority refusal and no mutation + +The same binary, config, workspace, and projection were checked against an absent explicitly selected socket: + +```text +$RUN/bin/duo doctor --workspace $RUN/workspace \ + --config $RUN/xdg/config/duo/duo.config.yaml \ + --host herdr:$RUN/xdg/runtime/no-server/herdr.sock \ + --output json +``` + +Doctor correctly exited 0 after producing a complete report, while preflight was `not_ready` with `authority_scope: unavailable`. The stage-specific failure was `host_reachability`, `fail / host.unreachable`; `host_compatibility` was `not_checked / prerequisite.not_reached`. + +No launch command was invoked. Across both probes, the Duo authority tree remained at zero entries, no authority database was created, and the workspace payload manifest remained `sha256:bbf0034693c602bd9ecebe65a6f21caf0e8c1556cac49c9cbb46628cb385efc2`. The fixture Herdr snapshot stayed at zero workspaces, tabs, panes, and agents. The server was stopped through local control, its process and socket disappeared, and the disposable root was removed. + +## Assumptions, unresolved boundary, and verification + +1. A missing local authority store is `initializable`, as the contract specifies; the no-write snapshots prove doctor did not create it. +2. Locality is the explicitly selected reachable Unix socket and isolated local paths, not ambient vendor markers. +3. The dirty version suffix reflects pre-existing out-of-scope checkout residue. The full source commit, build date, executable digest, and manifest digest disambiguate the tested Duo binary; the residue was not inspected or incorporated. +4. Deterministic loader invocation is not behavioral model adherence. Full launch/send/observe behavior belongs to Step 12. +5. Step 11 has no remaining blocker. Step 12 still requires the exact Pi `0.83.0` and Herdr `0.8.2` fixture and remains subject to the suite's documented blocked-condition prerequisite. + +Verification passed: + +- `evidence/portable-launcher-conformance/step-11/validate.sh` +- `git diff --check -- evidence/portable-launcher-conformance/step-11` + +Cross-repo impact: **unblocks work in `duo-lab`** by confirming on 2026-09-18 that Codex CLI `0.154.0`, native package `0.154.0-linux-x64`, native SHA-256 `3188814c35471432d4123203e0eb38e5bddc60226e3d7ddf0e59e649ea140022` discovers the installed canonical skill and passes reachable/unreachable local preflight. It does not invalidate the 2026-09-17 assessment at `caf02f7eb325b1c935d0cf793bc53fbb3a3f1a65`. Proposed handoff (not authored without approval): title “Capture Codex 0.154.0 full-suite runtime evidence”; body: consume Duo Step 11's exact launcher/skill/preflight pins, re-verify the live pin, and provision exact Pi `0.83.0` plus Herdr `0.8.2`/protocol `20`/schema `c48f…b150`; seal when the complete launcher-neutral scenario passes or records its named prerequisite honestly; origin: `duo`, portable-launcher-conformance Step 11, commit `40a2d24652c2543b4cde3bc1ebbd164c7b8952c9`, observed 2026-09-18. diff --git a/evidence/portable-launcher-conformance/step-11/validate.sh b/evidence/portable-launcher-conformance/step-11/validate.sh new file mode 100755 index 0000000..9ca1197 --- /dev/null +++ b/evidence/portable-launcher-conformance/step-11/validate.sh @@ -0,0 +1,124 @@ +#!/bin/sh +set -eu + +root=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd) +summary="$root/probe-summary.json" + +jq -e ' + .schema == "duo.portable-launcher-step11-evidence/v1" and + .observed_at_utc.start[0:10] == "2026-09-18" and + .observed_at_utc.finish[0:10] == "2026-09-18" and + .checkout.branch == "feat/portable-launcher-conformance" and + .checkout.commit == .duo.commit and + + # A passing Step 11 must use the accepted native executable. The npm + # JavaScript entrypoint is recorded separately and can never satisfy it. + .scope_result.step11 == "pass" and + .scope_result.seal_condition_met == true and + .scope_result.exact_accepted_native_launcher_used == true and + .codex.version == "0.154.0" and + .codex.accepted_version == "0.154.0" and + .codex.native_package_version == "0.154.0-linux-x64" and + .codex.native_executable_sha256 == "sha256:3188814c35471432d4123203e0eb38e5bddc60226e3d7ddf0e59e649ea140022" and + .codex.accepted_native_executable_sha256 == .codex.native_executable_sha256 and + .codex.javascript_entrypoint_sha256 == "sha256:61b0194f3bb6534439c8d26a3ed57d0805f84b884588b761795323eeb92fcf70" and + .codex.javascript_entrypoint_sha256 != .codex.native_executable_sha256 and + .codex.diagnostic_executable == "$CODEX_NATIVE" and + .codex.matches_accepted_pin == true and + .codex.matches_manifest_tested_version == true and + + .installation.schema == "duo.external/v1" and + .installation.operation == "projection.install" and + .installation.target == "portable_launchers" and + .installation.state == "current" and + .skill.name == .codex_discovery.name and + .skill.format_version == "duo.skill/duo-delegation-loop/v1" and + .skill.content_digest == "sha256:6f8bc16b656f564a949c99f12d4cf0bcd04ec2c9b49a8b74fb65b2e355b5d182" and + .skill.installed_file == "$RUN/workspace/.agents/skills/duo-delegation-loop/SKILL.md" and + .skill.stamp_schema == "duo.projection-stamp/v1" and + .skill.stamp_target == "portable_launchers" and + .skill.installed_bytes_match_canonical == true and + + .codex_discovery.exit_code == 0 and + .codex_discovery.matching_skill_records == 1 and + .codex_discovery.location == .skill.installed_file and + .codex_discovery.description_sha256 == "sha256:47c044a8e999289d69dd7912f007af0d7f11b6e10a8f5ffe28e730c7139cb85e" and + .codex_discovery.canonical_file_digest_matches == true and + .codex_discovery.loader_invoked == true and + .codex_discovery.model_visible_entry_injected == true and + .codex_discovery.model_turn_invoked == false and + .codex_discovery.behavioral_model_adherence_claimed == false and + .codex_discovery.threads_created == 0 and + .codex_discovery.sessions_created == 0 and + .codex_discovery.credentials_used == false and + .isolation.clean_environment_used == true and + .isolation.real_user_skill_tree_written == false and + .isolation.account_credentials_used == false and + (.isolation.installed_plugins | length) == 0 and + (.isolation.configured_mcp_servers | length) == 0 and + .isolation.plugins_enabled == false and + .isolation.mcp_enabled == false and + .isolation.terminal_input_used == false and + + .positive_preflight.schema == "duo.launcher-preflight/v1" and + .positive_preflight.status == "ready" and + .positive_preflight.authority_scope == "local" and + .positive_preflight.host_source == "explicit-flag" and + (.positive_preflight.checks | length) == 8 and + [.positive_preflight.checks[].id] == [ + "duo_executable", "effective_config", "authority_store", "workspace", + "host_selection", "host_reachability", "skill_projection", "host_compatibility" + ] and + ([.positive_preflight.checks[] | select(.required == true and .status == "pass" and .code == "ok")] | length) == 7 and + ([.positive_preflight.checks[] | select(.id == "host_compatibility" and .required == false and .status == "warning" and .code == "host.compatibility_unverified")] | length) == 1 and + .positive_preflight.skill_projection_state == "current" and + .positive_preflight.host_reachable == true and + .positive_preflight.duo_authority_entries_before == 0 and + .positive_preflight.duo_authority_entries_after == 0 and + .positive_preflight.workspace_payload_manifest_sha256_before == .positive_preflight.workspace_payload_manifest_sha256_after and + .positive_preflight.authority_store_created == false and + .positive_preflight.workspace_changed == false and + + .negative_preflight.schema == "duo.launcher-preflight/v1" and + .negative_preflight.status == "not_ready" and + .negative_preflight.authority_scope == "unavailable" and + .negative_preflight.failed_check.id == "host_reachability" and + .negative_preflight.failed_check.stage == "host_reachability" and + .negative_preflight.failed_check.status == "fail" and + .negative_preflight.failed_check.code == "host.unreachable" and + (.negative_preflight.failed_check.action | contains("$RUN/xdg/runtime/no-server/herdr.sock")) and + .negative_preflight.compatibility_check.status == "not_checked" and + .negative_preflight.compatibility_check.code == "prerequisite.not_reached" and + .negative_preflight.duo_authority_entries_before == 0 and + .negative_preflight.duo_authority_entries_after == 0 and + .negative_preflight.workspace_payload_manifest_sha256_before == .negative_preflight.workspace_payload_manifest_sha256_after and + .negative_preflight.launch_command_invoked == false and + .negative_preflight.authority_store_created == false and + .negative_preflight.workspace_changed == false and + + .host_state.fixture_objects_before == .host_state.fixture_objects_after and + .host_state.fixture_objects_after == {"workspaces":0,"tabs":0,"panes":0,"agents":0} and + .host_state.launch_observed == false and + .herdr.version == "0.9.0" and + .herdr.protocol_identity == "herdr-socket-api/22" and + .herdr.compatibility == "unverified" and + .herdr.required_full_suite_pin_available == false and + .scope_result.full_codex_suite_claimed == false and + .scope_result.launcher_support_claimed == false and + .cleanup.server_stopped_via_local_control == true and + .cleanup.server_process_exited == true and + .cleanup.socket_removed == true and + .cleanup.run_root_removed == true and + .scrub.status == "pass" and + .scrub.credentials_present == false and + .scrub.private_transcripts_present == false and + .scrub.raw_vendor_state_present == false and + .scrub.unsanitized_environment_values_present == false +' "$summary" >/dev/null + +if grep -En '/tmp/|/home/|Bearer[[:space:]]|user_code|"access_token"|"refresh_token"|api[_-]?key|client[_-]?secret' "$summary" "$root/report.md"; then + echo "step-11 evidence scrub check failed" >&2 + exit 1 +fi + +printf '%s\n' "step-11 evidence validation: pass" diff --git a/evidence/portable-launcher-conformance/step-12/prerequisite-summary.json b/evidence/portable-launcher-conformance/step-12/prerequisite-summary.json new file mode 100644 index 0000000..ab6d01d --- /dev/null +++ b/evidence/portable-launcher-conformance/step-12/prerequisite-summary.json @@ -0,0 +1,227 @@ +{ + "schema": "duo.portable-launcher-step12-prerequisite-evidence/v1", + "observed_at_utc": { + "start": "2026-09-18T08:40:51Z", + "finish": "2026-09-18T08:43:04Z" + }, + "host": { + "kernel": "Linux 6.8.0-136-generic", + "architecture": "x86_64" + }, + "checkout": { + "path": "$CHECKOUT", + "branch": "feat/portable-launcher-conformance", + "commit": "4c47e63c400184a091ecc6f09aab31fde6fe9937", + "preexisting_out_of_scope_changes": true + }, + "launcher": { + "name": "codex", + "required": { + "cli_version": "0.154.0", + "native_package_version": "0.154.0-linux-x64", + "native_executable_sha256": "sha256:3188814c35471432d4123203e0eb38e5bddc60226e3d7ddf0e59e649ea140022" + }, + "observed": { + "cli_version": "0.154.0", + "version_output": "codex-cli 0.154.0", + "command_location": "$HOME/.local/bin/codex", + "javascript_entrypoint_location": "$HOME/.npm-global/lib/node_modules/@openai/codex/bin/codex.js", + "javascript_entrypoint_bytes": 8790, + "javascript_entrypoint_sha256": "sha256:61b0194f3bb6534439c8d26a3ed57d0805f84b884588b761795323eeb92fcf70", + "native_package_name": "@openai/codex", + "native_package_version": "0.154.0-linux-x64", + "native_executable_location": "$HOME/.npm-global/lib/node_modules/@openai/codex/node_modules/@openai/codex-linux-x64/vendor/x86_64-unknown-linux-musl/bin/codex", + "native_executable_kind": "ELF 64-bit x86-64 static-pie", + "native_executable_bytes": 262858016, + "native_executable_sha256": "sha256:3188814c35471432d4123203e0eb38e5bddc60226e3d7ddf0e59e649ea140022" + }, + "accepted_identity_is_native_not_wrapper": true, + "matches_exact_accepted_pin": true + }, + "duo": { + "source_commit": "4c47e63c400184a091ecc6f09aab31fde6fe9937", + "build": { + "version": "4c47e63-dirty", + "commit": "4c47e63c400184a091ecc6f09aab31fde6fe9937", + "build_date": "2026-09-18T08:42:27Z", + "command": "CGO_ENABLED=0 go build -trimpath -ldflags -o $EVIDENCE/.duo-prereq ./cmd/duo", + "executable_sha256": "sha256:c89484c7ef0bbd1c7c1b282961e8ca7686ac7064a1ea052cf99c834753affa82", + "executable_bytes": 14369268, + "identity_verified_by_version_command": true, + "artifact_retained": false + }, + "manifest": { + "schema": "duo.manifest/v1", + "manifest_digest": "sha256:2a3f73835d2d2a03bed9af33d9e63880fbe64430692181dd26ba771a318413dd", + "portable_target": "portable_launchers", + "portable_target_status": "unverified" + } + }, + "skill": { + "name": "duo-delegation-loop", + "format_version": "duo.skill/duo-delegation-loop/v1", + "content_digest": "sha256:6f8bc16b656f564a949c99f12d4cf0bcd04ec2c9b49a8b74fb65b2e355b5d182", + "bytes": 6284, + "matches_manifest": true + }, + "suite": { + "schema": "duo.portable-launcher-conformance-scenario/v1", + "name": "portable-launcher-delegation", + "revision": 1, + "scenario_bytes": 40118, + "manifest_digest": "sha256:01e8e6bd80ceb441be6dc128aca023a1bede31b655e74b12ee3da0c2fc6cc204", + "oracle_digest": "sha256:a305744fc34af96b5bbd916b9de3aa1bc94f59ce4ea9316b62bb0a8dd5dd22c5", + "task_digest": "sha256:5949809812a3ada98c95e77e9942af848e3080e4984df43a2d96fe5a24d5eab2", + "fixture": { + "request_digest": "sha256:b0a32f59630d6398da8d8913744dd7cd50a313d86bf7e833309aad0d47cc9c3b", + "reply_digest": "sha256:b3300f2f6e0338d2b672891be66827d95779a2eabf4a75dcae1b6f56c9a4dc98", + "prompt_digest": "sha256:b40d3a68eb33083b92199d734da2dece28395a6acc6d5e2cb9d7032dae4d11c7", + "conflict_digest": "sha256:dbe904a42a9d9d9cdf7908508fa393ce43c9c53ea16a8cb8c9b82e7005f602bc", + "provider": "openai-codex", + "model_line": "gpt-5.6-luna" + }, + "source_and_fixture_sync_tests_passed": true + }, + "runtime": { + "required": { + "name": "pi", + "version": "0.83.0", + "semantic_format": "pi-session-jsonl/v3", + "adapter_build": "stage1", + "conformance_record": "pi-0.83.0-2026-08-23", + "delivery_asset_sha256": "sha256:2708a3435821fb2e72302ee56d8a07fe62c265214ae8fd342e385c03ef245a23" + }, + "installed": { + "version": "0.84.4", + "executable_sha256": "sha256:5406c369954516fb56879d685e082ff9095cd6e06e41af406f394942377fd4bf", + "executable_bytes": 629, + "location": "$HOME/.npm-global/lib/node_modules/@earendil-works/pi-coding-agent/dist/bundle/cli.js", + "matches_required_version": false + }, + "local_cache": { + "exact_package_archive_present": true, + "package_version": "0.83.0", + "package_archive_sha256": "sha256:7097fe4b38762dda7ec78001e7b90430c849fbaf717325bfe8109744e32255e6", + "package_archive_bytes": 4992066, + "package_integrity": "sha512-uYhF+FsZxogoSX/AxBcUdiY+ZklubwaXyAoEGA2eQwsHcyEAhUYIKh/WLXe/a8+k8eTCmxb+ZN2Zo9mzQtzbWw==", + "cached_launcher_path": "package/dist/cli.js", + "cached_launcher_sha256": "sha256:af302f231437eaf6f37691bce4b34234fcb626bcb5eb3910d4fc3f6519bf78ca", + "cached_launcher_bytes": 681, + "dependency_tree_provisioned": false, + "runnable_exact_executable_available": false + }, + "exact_required_runtime_available": false, + "reason": "The exact package archive is cached, but no runnable Pi 0.83.0 installation with a verified dependency tree and executable identity is provisioned; the only installed Pi is 0.84.4." + }, + "host_adapter": { + "required": { + "name": "herdr", + "version": "0.8.2", + "protocol": "herdr-socket-api/20", + "api_schema_sha256": "sha256:c48f1f54ee0150ca27e11fd44455fe94aeadb20fdf4e4a62393ed822a4e5b150", + "adapter_build": "stage1", + "conformance_record": "notes/19-herdr-probes.md@2026-08-23" + }, + "installed_on_path": { + "version": "0.9.0", + "executable_sha256": "sha256:5ef212a3f142f902b1a4eb3f7a76827ded9f313804c580c48297c060ac1fcac0", + "executable_bytes": 39728936, + "api_schema_sha256": "sha256:226d4ecbd128d2e6bc84e4c8ddcec21ba9c7e51a0aafffcf087111ead3f1fa9a", + "location": "$HERDR_CHECKOUT/target/release/herdr", + "matches_required_identity": false + }, + "local_exact_version_candidates": [ + { + "location": "$HERDR_CHECKOUT/target/debug/herdr", + "version": "0.8.2", + "executable_sha256": "sha256:7e1a54d98765f91eca532752369d7c01f91b971883599b393a1f014d332bb85e", + "executable_bytes": 228542880, + "api_schema_sha256": "sha256:24e62c2d8be455ba7872c0e66bfbbf77e8403c29c3f5b1ff10c8ff3c112f4e63", + "matches_required_schema": false + }, + { + "location": "$HERDR_CHECKOUT/target/release/deps/herdr-dacc253a0a16c2ca", + "version": "0.8.2", + "executable_sha256": "sha256:c1cb6b1338d0f9e70cd8aa35828cec0635d64865aa90535d4350168750cf4ccf", + "executable_bytes": 32012656, + "api_schema_sha256": "sha256:24e62c2d8be455ba7872c0e66bfbbf77e8403c29c3f5b1ff10c8ff3c112f4e63", + "matches_required_schema": false + } + ], + "protocol_probe_performed": false, + "protocol_probe_reason": "No fixture server was started after the independently exported API schema decisively rejected the local 0.8.2 candidates.", + "exact_required_host_available": false, + "reason": "The locally found Herdr 0.8.2 executables export schema 24e62c2d... rather than the accepted c48f1f54... identity; version text alone is ineligible." + }, + "prerequisites": [ + {"name": "pinned_codex_native_binary", "required": true, "status": "available"}, + {"name": "pinned_duo_build_identity", "required": true, "status": "available_for_inspection"}, + {"name": "current_canonical_skill_identity", "required": true, "status": "available"}, + {"name": "launcher_neutral_suite_revision", "required": true, "status": "available"}, + {"name": "pinned_pi_0_83_0_runnable_artifact", "required": true, "status": "unavailable"}, + {"name": "pinned_herdr_0_8_2_protocol_20_schema", "required": true, "status": "unavailable"}, + {"name": "isolated_inner_provider_credentials", "required": true, "status": "not_checked"}, + {"name": "timeout_induction_socket_probe", "required": true, "status": "not_checked"}, + {"name": "supported_admitted_then_blocked_producer", "required": true, "status": "unavailable"} + ], + "canonical_blocked_prerequisite": { + "name": "supported_admitted_then_blocked_producer", + "scenario_status": "unavailable", + "validator_rejects_overall_pass": true, + "validator_rejects_blocked_case_pass": true, + "available": false + }, + "eligibility": { + "all_exact_prerequisites_available": false, + "result": "blocked", + "first_identity_blockers": [ + "pinned_pi_0_83_0_runnable_artifact", + "pinned_herdr_0_8_2_protocol_20_schema" + ], + "independent_suite_blocker": "supported_admitted_then_blocked_producer", + "complete_suite_can_pass_current_revision": false, + "step12_seal_condition_met": false, + "matter_seal_condition_met": false + }, + "actions": { + "fixture_setup_invoked": false, + "fixture_root_created": false, + "fixture_workspace_created_or_mutated": false, + "fixture_authority_created_or_mutated": false, + "herdr_server_started": false, + "launcher_version_probe_invoked": true, + "launcher_session_invoked": false, + "launcher_thread_created": false, + "model_turn_invoked": false, + "duo_version_or_manifest_probe_invoked": true, + "duo_session_launch_invoked": false, + "account_billed_action_invoked": false, + "credentials_inspected": false, + "production_or_shared_runtime_state_accessed": false, + "binary_downloaded": false, + "external_network_action_invoked": false, + "wip_lifecycle_state_changed": false + }, + "search_scope": { + "codex": ["$HOME/.local/bin", "$HOME/.npm-global/lib/node_modules/@openai"], + "pi": ["$HOME/.local/bin", "$HOME/.npm-global", "$HOME/.npm", "$HOME/.cache", "$HOME/.local/share/pnpm"], + "herdr": ["$HOME/.local/bin", "$HOME/.cargo/bin", "$HERDR_CHECKOUT/target", "$HOME/.cache"] + }, + "resume_condition": { + "required": [ + "Provision a runnable Pi 0.83.0 artifact and complete dependency tree locally, then record its provenance and executable SHA-256 without widening the pin.", + "Provision a Herdr executable that is simultaneously version 0.8.2, protocol 20, and API-schema SHA-256 c48f1f54ee0150ca27e11fd44455fe94aeadb20fdf4e4a62393ed822a4e5b150; reject the current 24e62c2d... candidates.", + "Add supported, version-pinned production evidence for deterministic admitted-then-blocked Pi behavior and update the canonical suite only through the owning runtime/host capability work.", + "Rebuild and retain Duo from the then-current exact checkout commit and reverify the Codex native, skill, scenario, oracle, and fixture identities.", + "Confirm isolated openai-codex credentials without recording secret material and complete the timeout socket probe in the disposable fixture.", + "Before any model turn or Codex session/thread, obtain a parent decision reconciling the user's no-thread constraint with Step 12's model-turn requirement." + ] + }, + "scrub": { + "status": "pass", + "absolute_checkout_paths_present": false, + "credentials_present": false, + "private_transcripts_present": false, + "raw_vendor_state_present": false + } +} diff --git a/evidence/portable-launcher-conformance/step-12/report.md b/evidence/portable-launcher-conformance/step-12/report.md new file mode 100644 index 0000000..0d78b8b --- /dev/null +++ b/evidence/portable-launcher-conformance/step-12/report.md @@ -0,0 +1,75 @@ +# Step 12 — Complete Codex launcher suite prerequisite gate + +- **Observed:** 2026-09-18T08:40:51Z–08:43:04Z +- **Checkout:** `$CHECKOUT`, branch `feat/portable-launcher-conformance`, commit `4c47e63c400184a091ecc6f09aab31fde6fe9937` +- **Outcome:** **BLOCKED before fixture setup, Codex session/thread, model turn, or Duo launch** +- **Step 12 seal condition:** **not met** +- **Matter seal condition:** **not met** + +The complete launcher-neutral Codex suite cannot run honestly under the repository's exact accepted pins in this checkout. The accepted Codex native executable is exact, but no eligible runnable Pi 0.83.0 installation was found, the local Herdr 0.8.2 executables fail the accepted API-schema identity, and suite revision 1 still declares its admitted-then-blocked producer unavailable and rejects an overall or blocked-case pass. The newer installed Pi/Herdr artifacts and the same-version/wrong-schema Herdr artifacts were not relabeled as accepted. + +## Exact required and observed identities + +| Component | Required | Observed | Verdict | +|---|---|---|---| +| Outer Codex | CLI `0.154.0`; native package `0.154.0-linux-x64`; native SHA-256 `3188814c35471432d4123203e0eb38e5bddc60226e3d7ddf0e59e649ea140022` | Exact native ELF, 262,858,016 bytes, exact digest; wrapper is separately SHA-256 `61b0194f3bb6534439c8d26a3ed57d0805f84b884588b761795323eeb92fcf70` | eligible | +| Runtime Pi | `0.83.0`; `pi-session-jsonl/v3`; adapter `pi` build `stage1`; record `pi-0.83.0-2026-08-23`; inject SHA-256 `2708a3435821fb2e72302ee56d8a07fe62c265214ae8fd342e385c03ef245a23` | Installed executable is `0.84.4`, SHA-256 `5406c369954516fb56879d685e082ff9095cd6e06e41af406f394942377fd4bf` | **ineligible** | +| Local Pi cache | Runnable exact artifact with a verified dependency tree | Exact `0.83.0` package archive is cached (4,992,066 bytes; SHA-256 `7097fe4b38762dda7ec78001e7b90430c849fbaf717325bfe8109744e32255e6`), but it is not a provisioned dependency-complete executable | **insufficient** | +| Host Herdr | `0.8.2`; `herdr-socket-api/20`; API-schema SHA-256 `c48f1f54ee0150ca27e11fd44455fe94aeadb20fdf4e4a62393ed822a4e5b150`; adapter `herdr` build `stage1`; record `notes/19-herdr-probes.md@2026-08-23` | PATH is `0.9.0`/schema `226d4ecb…`; two local `0.8.2` executables both export schema `24e62c2d8be455ba7872c0e66bfbbf77e8403c29c3f5b1ff10c8ff3c112f4e63` | **ineligible** | +| Skill | Current `duo.skill/duo-delegation-loop/v1` and exact current digest | SHA-256 `6f8bc16b656f564a949c99f12d4cf0bcd04ec2c9b49a8b74fb65b2e355b5d182`, 6,284 bytes; manifest agrees | eligible | +| Suite | `portable-launcher-delegation`, revision 1 | Scenario SHA-256 `01e8e6bd80ceb441be6dc128aca023a1bede31b655e74b12ee3da0c2fc6cc204`; oracle `a305744fc34af96b5bbd916b9de3aa1bc94f59ce4ea9316b62bb0a8dd5dd22c5`; task `5949809812a3ada98c95e77e9942af848e3080e4984df43a2d96fe5a24d5eab2`; source/fixture tests passed | exact, but incapable of an overall pass while the blocked producer is unavailable | + +The accepted Codex identity is the native ELF, not the npm JavaScript entrypoint. Both were independently resolved and hashed. The native package metadata reports `0.154.0-linux-x64`, the native executable itself reports `codex-cli 0.154.0`, and only its `3188814c…0022` digest satisfies the suite pin. + +The Herdr candidates were rejected on the complete accepted identity rather than filename or version text. Their executable SHA-256 values are `7e1a54d98765f91eca532752369d7c01f91b971883599b393a1f014d332bb85e` (debug) and `c1cb6b1338d0f9e70cd8aa35828cec0635d64865aa90535d4350168750cf4ccf` (release dependency artifact). Both independently exported schema `24e62c2d…f4e63`, so neither can stand in for `c48f1f54…b150`. No Herdr server was started to probe protocol after that decisive mismatch. + +## Exact checkout-built Duo identity + +A prerequisite-only Duo binary was built inside this evidence directory, queried only with `version` and `manifest`, hashed, and removed. It did not prepare a fixture or invoke a session. + +- version: `4c47e63-dirty` +- full commit: `4c47e63c400184a091ecc6f09aab31fde6fe9937` +- build date: `2026-09-18T08:42:27Z` +- executable: 14,369,268 bytes, SHA-256 `c89484c7ef0bbd1c7c1b282961e8ca7686ac7064a1ea052cf99c834753affa82` +- manifest: `duo.manifest/v1`, semantic digest `sha256:2a3f73835d2d2a03bed9af33d9e63880fbe64430692181dd26ba771a318413dd` +- portable target: `portable_launchers`, status `unverified`, carrying the exact skill and Codex pins above + +The artifact was deliberately not retained. Any resumed live run must rebuild and retain a new exact per-run identity from the then-current checkout. + +## Complete-pass blocker independent of local artifacts + +Revision 1 declares `supported_admitted_then_blocked_producer` as `unavailable`. Pi 0.83.0 has no permission system or blocked-family producer, and its condition adapter never emits blocked. The suite validator forbids both an overall pass and any blocked-case stage marked pass. Consequently, provisioning both exact binaries would still not make the current complete suite pass. The honest prerequisite is a supported, version-pinned production condition source that deterministically produces `blocked` after prompt admission, with a non-terminal controller and exact runtime-instance evidence. Fake events, PTY input, model persuasion, a different runtime for one case, or launcher-driver patches remain ineligible. + +## Stop boundary and assumptions + +No fixture root, fixture workspace, authority store, Herdr server, Codex session/thread, model turn, Duo session launch, billed action, credential inspection, network download, production/shared runtime access, or WIP lifecycle transition occurred. Only local installation/cache reads, version/hash/schema inspection, focused offline tests, and the prerequisite-only Duo build occurred. No external or mutable product/runtime action occurred. + +Assumptions: + +1. A cached npm package archive is not a runnable Pi artifact; its runtime dependencies were neither provisioned nor verified, and the installed command remains 0.84.4. +2. A Herdr binary reporting `0.8.2` is insufficient when its exported schema digest differs from the accepted pin. Protocol was intentionally not inferred or live-probed after that decisive failure. +3. Credentials and timeout induction were left uninspected because exact identity and canonical-suite prerequisites already forced a pre-setup stop. +4. The `-dirty` suffix honestly records the user-declared, pre-existing out-of-scope checkout residue. That residue was not inspected, staged, edited, removed, or incorporated as evidence. +5. If every prerequisite later becomes available, the run must still stop before a model turn or Codex session/thread until the parent reconciles the user's no-thread constraint. + +## Exact resume condition + +Resume only after all of the following are true: + +1. A runnable, dependency-complete Pi 0.83.0 artifact is locally provisioned and its provenance and executable SHA-256 are captured without widening the pin. +2. A Herdr artifact simultaneously proves version 0.8.2, protocol 20, and API-schema SHA-256 `c48f1f54…b150`; the current `24e62c2d…` candidates remain rejected. +3. Owning runtime/host capability work supplies supported, version-pinned admitted-then-blocked evidence and the canonical suite no longer marks that prerequisite unavailable. +4. Duo is rebuilt and retained from the then-current exact checkout; Codex native, skill, scenario, oracle, and fixture identities are independently reverified. +5. Isolated `openai-codex` credentials are confirmed without recording secret material, and the timeout socket probe succeeds in the disposable fixture. +6. Before any model turn or Codex session/thread, the parent explicitly reconciles the user's no-thread constraint with Step 12's model-turn requirement. + +Until then, neither Step 12 nor the Matter seal condition is met. + +## Verification + +- `go test ./internal/conformance/portablelauncher -run 'TestCanonicalScenario|TestCollectorMandatoryBlockedPrerequisitePreventsPass' -count=1` — passed. +- `evidence/portable-launcher-conformance/step-12/validate.sh` — asserts the blocked result, exact accepted Codex native identity, rejected runtime/host substitutes, unavailable blocked producer, and no-action boundary. +- `sh -n evidence/portable-launcher-conformance/step-12/validate.sh` — required final check. +- `git diff --check -- evidence/portable-launcher-conformance/step-12` — required final check. + +Cross-repo impact: **no cross-repo impact**. This evidence independently confirms on 2026-09-18 the existing `duo-lab` prerequisite boundary at Codex `0.154.0` native SHA-256 `3188814c…0022`, Pi `0.83.0`, and Herdr `0.8.2`/protocol `20`/schema `c48f1f54…b150`; it neither unblocks work there nor invalidates an accepted assumption. No handoff is proposed. diff --git a/evidence/portable-launcher-conformance/step-12/validate.sh b/evidence/portable-launcher-conformance/step-12/validate.sh new file mode 100755 index 0000000..b623c22 --- /dev/null +++ b/evidence/portable-launcher-conformance/step-12/validate.sh @@ -0,0 +1,127 @@ +#!/bin/sh +set -eu + +root=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd) +summary="$root/prerequisite-summary.json" + +jq -e ' + . as $evidence | + .schema == "duo.portable-launcher-step12-prerequisite-evidence/v1" and + .observed_at_utc.start[0:10] == "2026-09-18" and + .observed_at_utc.finish[0:10] == "2026-09-18" and + .checkout.branch == "feat/portable-launcher-conformance" and + (.checkout.commit | test("^[0-9a-f]{40}$")) and + + # Bind the result to the accepted native Codex ELF. The JavaScript wrapper + # is separately identified and cannot satisfy the launcher prerequisite. + .launcher.name == "codex" and + .launcher.required.cli_version == "0.154.0" and + .launcher.required.native_package_version == "0.154.0-linux-x64" and + .launcher.required.native_executable_sha256 == "sha256:3188814c35471432d4123203e0eb38e5bddc60226e3d7ddf0e59e649ea140022" and + .launcher.observed.cli_version == .launcher.required.cli_version and + .launcher.observed.native_package_version == .launcher.required.native_package_version and + .launcher.observed.native_executable_sha256 == .launcher.required.native_executable_sha256 and + .launcher.observed.native_executable_kind == "ELF 64-bit x86-64 static-pie" and + .launcher.observed.javascript_entrypoint_sha256 == "sha256:61b0194f3bb6534439c8d26a3ed57d0805f84b884588b761795323eeb92fcf70" and + .launcher.observed.javascript_entrypoint_sha256 != .launcher.observed.native_executable_sha256 and + .launcher.accepted_identity_is_native_not_wrapper == true and + .launcher.matches_exact_accepted_pin == true and + + .duo.source_commit == .checkout.commit and + .duo.build.commit == .checkout.commit and + .duo.build.version == "4c47e63-dirty" and + .duo.build.executable_sha256 == "sha256:c89484c7ef0bbd1c7c1b282961e8ca7686ac7064a1ea052cf99c834753affa82" and + .duo.build.identity_verified_by_version_command == true and + .duo.build.artifact_retained == false and + .duo.manifest.schema == "duo.manifest/v1" and + .duo.manifest.portable_target == "portable_launchers" and + .duo.manifest.portable_target_status == "unverified" and + + .skill.name == "duo-delegation-loop" and + .skill.format_version == "duo.skill/duo-delegation-loop/v1" and + .skill.content_digest == "sha256:6f8bc16b656f564a949c99f12d4cf0bcd04ec2c9b49a8b74fb65b2e355b5d182" and + .skill.matches_manifest == true and + + .suite.schema == "duo.portable-launcher-conformance-scenario/v1" and + .suite.name == "portable-launcher-delegation" and + .suite.revision == 1 and + .suite.manifest_digest == "sha256:01e8e6bd80ceb441be6dc128aca023a1bede31b655e74b12ee3da0c2fc6cc204" and + .suite.oracle_digest == "sha256:a305744fc34af96b5bbd916b9de3aa1bc94f59ce4ea9316b62bb0a8dd5dd22c5" and + .suite.task_digest == "sha256:5949809812a3ada98c95e77e9942af848e3080e4984df43a2d96fe5a24d5eab2" and + .suite.source_and_fixture_sync_tests_passed == true and + + .runtime.required.version == "0.83.0" and + .runtime.required.semantic_format == "pi-session-jsonl/v3" and + .runtime.required.adapter_build == "stage1" and + .runtime.required.conformance_record == "pi-0.83.0-2026-08-23" and + .runtime.required.delivery_asset_sha256 == "sha256:2708a3435821fb2e72302ee56d8a07fe62c265214ae8fd342e385c03ef245a23" and + .runtime.installed.version == "0.84.4" and + .runtime.installed.version != .runtime.required.version and + .runtime.installed.matches_required_version == false and + .runtime.local_cache.exact_package_archive_present == true and + .runtime.local_cache.package_version == .runtime.required.version and + .runtime.local_cache.package_archive_sha256 == "sha256:7097fe4b38762dda7ec78001e7b90430c849fbaf717325bfe8109744e32255e6" and + .runtime.local_cache.dependency_tree_provisioned == false and + .runtime.local_cache.runnable_exact_executable_available == false and + .runtime.exact_required_runtime_available == false and + + .host_adapter.required.version == "0.8.2" and + .host_adapter.required.protocol == "herdr-socket-api/20" and + .host_adapter.required.api_schema_sha256 == "sha256:c48f1f54ee0150ca27e11fd44455fe94aeadb20fdf4e4a62393ed822a4e5b150" and + .host_adapter.required.adapter_build == "stage1" and + .host_adapter.required.conformance_record == "notes/19-herdr-probes.md@2026-08-23" and + .host_adapter.installed_on_path.version == "0.9.0" and + .host_adapter.installed_on_path.matches_required_identity == false and + (.host_adapter.local_exact_version_candidates | length) == 2 and + ([.host_adapter.local_exact_version_candidates[] | + select(.version == "0.8.2" and + .api_schema_sha256 == "sha256:24e62c2d8be455ba7872c0e66bfbbf77e8403c29c3f5b1ff10c8ff3c112f4e63" and + .matches_required_schema == false)] | length) == 2 and + ([.host_adapter.local_exact_version_candidates[] | + select(.api_schema_sha256 == $evidence.host_adapter.required.api_schema_sha256)] | length) == 0 and + .host_adapter.protocol_probe_performed == false and + .host_adapter.exact_required_host_available == false and + + .canonical_blocked_prerequisite.name == "supported_admitted_then_blocked_producer" and + .canonical_blocked_prerequisite.scenario_status == "unavailable" and + .canonical_blocked_prerequisite.validator_rejects_overall_pass == true and + .canonical_blocked_prerequisite.validator_rejects_blocked_case_pass == true and + .canonical_blocked_prerequisite.available == false and + + ([.prerequisites[] | select(.required == true and .status == "unavailable")] | length) >= 3 and + ([.prerequisites[] | select(.name == "pinned_codex_native_binary" and .status == "available")] | length) == 1 and + ([.prerequisites[] | select(.name == "pinned_pi_0_83_0_runnable_artifact" and .status == "unavailable")] | length) == 1 and + ([.prerequisites[] | select(.name == "pinned_herdr_0_8_2_protocol_20_schema" and .status == "unavailable")] | length) == 1 and + ([.prerequisites[] | select(.name == "supported_admitted_then_blocked_producer" and .status == "unavailable")] | length) == 1 and + .eligibility.all_exact_prerequisites_available == false and + .eligibility.result == "blocked" and + .eligibility.complete_suite_can_pass_current_revision == false and + .eligibility.step12_seal_condition_met == false and + .eligibility.matter_seal_condition_met == false and + + .actions.fixture_setup_invoked == false and + .actions.fixture_root_created == false and + .actions.fixture_workspace_created_or_mutated == false and + .actions.fixture_authority_created_or_mutated == false and + .actions.herdr_server_started == false and + .actions.launcher_session_invoked == false and + .actions.launcher_thread_created == false and + .actions.model_turn_invoked == false and + .actions.duo_session_launch_invoked == false and + .actions.account_billed_action_invoked == false and + .actions.credentials_inspected == false and + .actions.production_or_shared_runtime_state_accessed == false and + .actions.binary_downloaded == false and + .actions.external_network_action_invoked == false and + .actions.wip_lifecycle_state_changed == false and + .scrub.status == "pass" +' "$summary" >/dev/null + +# The evidence must remain path- and secret-scrubbed. Stable tokens such as +# $HOME, $CHECKOUT, $EVIDENCE, and $HERDR_CHECKOUT are intentional. +if grep -En '/home/dev|/tmp/|Bearer[[:space:]]|user_code|"access_token"|"refresh_token"|api[_-]?key|client[_-]?secret' "$summary" "$root/report.md"; then + echo "step-12 evidence scrub check failed" >&2 + exit 1 +fi + +printf '%s\n' "step-12 prerequisite evidence validation: blocked (pass)" diff --git a/flake.nix b/flake.nix index a2d190f..fcfa7d1 100644 --- a/flake.nix +++ b/flake.nix @@ -43,6 +43,9 @@ postInstall = '' mkdir -p $out/share/duo cp -r assets $out/share/duo/assets + mkdir -p $out/share/duo/assets/skills/duo-delegation-loop + cp skills/duo-delegation-loop/SKILL.md \ + $out/share/duo/assets/skills/duo-delegation-loop/SKILL.md # assets.go only exists so `assets/` can embed itself as the # binary's last-resort fallback; it is source, not a shipped # asset, and must not appear in the installed share tree. diff --git a/go.mod b/go.mod index f9d5d56..9537486 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,7 @@ toolchain go1.23.4 require ( github.com/spf13/cobra v1.10.2 github.com/spf13/pflag v1.0.9 + golang.org/x/sys v0.22.0 gopkg.in/yaml.v3 v3.0.1 modernc.org/sqlite v1.34.5 ) @@ -18,7 +19,6 @@ require ( github.com/mattn/go-isatty v0.0.20 // indirect github.com/ncruces/go-strftime v0.1.9 // indirect github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect - golang.org/x/sys v0.22.0 // indirect modernc.org/libc v1.55.3 // indirect modernc.org/mathutil v1.6.0 // indirect modernc.org/memory v1.8.0 // indirect diff --git a/internal/asset/asset.go b/internal/asset/asset.go index fa941b0..bd99978 100644 --- a/internal/asset/asset.go +++ b/internal/asset/asset.go @@ -9,8 +9,10 @@ import ( "fmt" "os" "path/filepath" + "strings" rootassets "github.com/procrastivity/duo/assets" + rootskills "github.com/procrastivity/duo/skills" ) // appName names both the XDG config subdirectory and the installed share @@ -129,6 +131,11 @@ func ReadDefault(relativeName string) (Resolved, error) { if data, err := rootassets.FS.ReadFile(relativeName); err == nil { return Resolved{Source: SourceEmbedded, data: data}, nil } + if skillName, ok := strings.CutPrefix(filepath.ToSlash(relativeName), "skills/"); ok { + if data, err := rootskills.FS.ReadFile(skillName); err == nil { + return Resolved{Source: SourceEmbedded, data: data}, nil + } + } return Resolved{}, fmt.Errorf("asset: %q not found in default directory or embedded fallback", relativeName) } diff --git a/internal/asset/portable_skill_test.go b/internal/asset/portable_skill_test.go new file mode 100644 index 0000000..6e44983 --- /dev/null +++ b/internal/asset/portable_skill_test.go @@ -0,0 +1,58 @@ +package asset + +import ( + "bytes" + "os" + "path/filepath" + "testing" +) + +func TestPortableSkillEmbeddedFallbackMatchesAuthoredSource(t *testing.T) { + authored, err := os.ReadFile(filepath.Join("..", "..", "skills", "duo-delegation-loop", "SKILL.md")) + if err != nil { + t.Fatal(err) + } + oldExecutable := executable + executable = func() (string, error) { return filepath.Join(t.TempDir(), "bin", "duo"), nil } + t.Cleanup(func() { executable = oldExecutable }) + + resolved, err := ReadDefault("skills/duo-delegation-loop/SKILL.md") + if err != nil { + t.Fatal(err) + } + if resolved.Source != SourceEmbedded { + t.Fatalf("source = %s, want embedded", resolved.Source) + } + if !bytes.Equal(resolved.Bytes(), authored) { + t.Fatal("embedded portable skill differs from authored source bytes") + } +} + +func TestPortableSkillInstalledShareTreeMatchesAuthoredSource(t *testing.T) { + authored, err := os.ReadFile(filepath.Join("..", "..", "skills", "duo-delegation-loop", "SKILL.md")) + if err != nil { + t.Fatal(err) + } + prefix := t.TempDir() + shipped := filepath.Join(prefix, "share", "duo", "assets", "skills", "duo-delegation-loop", "SKILL.md") + if err := os.MkdirAll(filepath.Dir(shipped), 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(shipped, authored, 0o644); err != nil { + t.Fatal(err) + } + oldExecutable := executable + executable = func() (string, error) { return filepath.Join(prefix, "bin", "duo"), nil } + t.Cleanup(func() { executable = oldExecutable }) + + resolved, err := ReadDefault("skills/duo-delegation-loop/SKILL.md") + if err != nil { + t.Fatal(err) + } + if resolved.Source != SourceDefault || resolved.Path != shipped { + t.Fatalf("resolution = source %s path %q, want default %q", resolved.Source, resolved.Path, shipped) + } + if !bytes.Equal(resolved.Bytes(), authored) { + t.Fatal("installed-share portable skill differs from authored source bytes") + } +} diff --git a/internal/cli/doctor.go b/internal/cli/doctor.go index 0b4e666..4812119 100644 --- a/internal/cli/doctor.go +++ b/internal/cli/doctor.go @@ -5,27 +5,34 @@ import ( "encoding/json" "errors" "fmt" + "io" "os" + "path/filepath" "sort" "strings" + "time" "github.com/spf13/cobra" "github.com/procrastivity/duo/internal/adapter" + "github.com/procrastivity/duo/internal/buildinfo" "github.com/procrastivity/duo/internal/cliflags" "github.com/procrastivity/duo/internal/config" "github.com/procrastivity/duo/internal/doctor" "github.com/procrastivity/duo/internal/domain" + "github.com/procrastivity/duo/internal/domain/storerepo" "github.com/procrastivity/duo/internal/duoerr" hostfake "github.com/procrastivity/duo/internal/host/fake" "github.com/procrastivity/duo/internal/host/herdr" "github.com/procrastivity/duo/internal/iostreams" "github.com/procrastivity/duo/internal/launch" "github.com/procrastivity/duo/internal/launch/materialize" + "github.com/procrastivity/duo/internal/manifest" runtimedevin "github.com/procrastivity/duo/internal/runtime/devin" runtimefake "github.com/procrastivity/duo/internal/runtime/fake" "github.com/procrastivity/duo/internal/scrub" + "github.com/procrastivity/duo/internal/store" "github.com/procrastivity/duo/internal/surface" ) @@ -60,15 +67,14 @@ func registeredAdapters(cmd *cobra.Command) []doctor.Adapter { } // doctorCommand constructs the `duo doctor` verb: internal/registry's -// "doctor.run" operation, CLI path {"doctor"}. Step 10 wired the core -// checks — authority-store health and the registered-adapters section; -// docs/doctor/decisions.md records what a later step still owes -// (generated-artifact drift, harness trust, live socket checks). +// "doctor.run" operation, CLI path {"doctor"}. It preserves the original +// authority, adapter, and visibility sections and adds the portable-launcher +// readiness contract as a stable top-level sibling. // -// The visibility rail is a diagnostic read. The harness-directory sweep -// (notes/51 9a) is the one filesystem write: it deletes orphan close-on-exit -// dirs whose launch-resolution id is not live. It does not write the -// authority store and does not dial a socket (I-3). +// The visibility rail and launcher preflight are diagnostic reads. Harness +// directories are reported but never reaped; the authority opens through +// SQLite mode=ro; projection inspection never repairs; the only live request +// is a bounded, non-mutating ping to the selected host. // // Step 15 (config-v3) adds the visibility rail: the cwd workspace's (or // --workspace's) current host correlation, what M1 would deduce right now @@ -87,8 +93,12 @@ func registeredAdapters(cmd *cobra.Command) []doctor.Adapter { // --store-path flag, which no spec for this step asks for. The launch // config path resolves the same way session.launch's own default does // (defaultLaunchConfigPath, session_launch.go). -func doctorCommand(streams *iostreams.Streams) *cobra.Command { - var workspace string +func doctorCommand(streams *iostreams.Streams, build buildinfo.Info) *cobra.Command { + var ( + workspace string + configPath string + hostFlag string + ) cmd := &cobra.Command{ Use: "doctor", @@ -109,39 +119,69 @@ func doctorCommand(streams *iostreams.Streams) *cobra.Command { base := doctor.Run(storePath, registeredAdapters(cmd)) - root, err := workspaceRoot(workspace) - if err != nil { - return err - } + root, workspaceStatus, workspaceErr := doctorWorkspace(workspace) - // openReadAuthority never creates a store that was not already - // there (session.go), the same "a missing store is not an - // error" discipline doctor.Run's own probeStore applies — a - // fresh installation reports an unbound workspace and no - // standing provider facts, not a freshly-created database. - a, closer, err := openReadAuthority(cmd.Context()) - if err != nil { - return err + // Doctor has a stricter read seam than ordinary legacy read + // commands: SQLite mode=ro, no migration, and no lease. A replay + // failure becomes an authority finding while the remaining + // independent checks continue against an empty read model. + a, closer, replayErr := openDoctorAuthority(cmd.Context(), storePath) + if replayErr != nil { + base.Store.Healthy = false + if base.Store.Error == "" { + base.Store.Error = replayErr.Error() + } + a, err = emptyDoctorAuthority(cmd.Context()) + if err != nil { + return duoerr.New("internal.authority_open_failed", fmt.Sprintf("opening fallback diagnostic authority: %v", err)) + } + closer = nopCloser{} } defer func() { _ = closer.Close() }() - configPath, err := defaultLaunchConfigPath() + selectedConfigPath := configPath + if selectedConfigPath == "" { + selectedConfigPath, err = defaultLaunchConfigPath() + if err != nil { + return duoerr.New("internal.config_path_unresolved", fmt.Sprintf("resolving the default duo.config path: %v", err)) + } + } + selectedConfigPath, err = filepath.Abs(selectedConfigPath) if err != nil { - return duoerr.New("internal.config_path_unresolved", fmt.Sprintf("resolving the default duo.config path: %v", err)) + return duoerr.New("internal.config_path_unresolved", fmt.Sprintf("resolving the selected duo.config path: %v", err)) } - configSection, policy := doctorConfigStatus(configPath) + selectedConfigPath = filepath.Clean(selectedConfigPath) + configSection, policy, configDoc := doctorConfigStatus(selectedConfigPath) - deduction := doctorHostDeduction(cmd.Context(), a, root, policy) + deduction := doctorHostDeduction(cmd.Context(), a, root, hostFlag, policy) harnessRoot, err := doctor.DefaultHarnessRoot() if err != nil { return duoerr.New("internal.doctor_harness_path_unresolved", fmt.Sprintf("resolving the harness directory: %v", err)) } - sweep, err := doctor.SweepHarnessDirs(harnessRoot, keepLiveHarness(a)) + sweep, err := doctor.InspectHarnessDirs(harnessRoot, keepLiveHarness(a)) + if err != nil { + sweep = doctor.HarnessSweep{IDs: []string{}, ReadOnly: true, Error: err.Error()} + } + + m, err := manifest.Build(cmd.Root(), build) if err != nil { - return duoerr.New("internal.doctor_harness_sweep_failed", fmt.Sprintf("reaping orphan harness directories: %v", err)) + return duoerr.New("internal.manifest_build_failed", fmt.Sprintf("building the diagnostic manifest: %v", err)) + } + preflight := doctorLauncherPreflight(cmd.Context(), doctorPreflightInput{ + Build: build, Store: base.Store, ReplayError: replayErr, + ConfigPath: selectedConfigPath, ConfigSection: configSection, Config: configDoc, + Workspace: workspaceStatus, WorkspaceError: workspaceErr, + Deduction: deduction, Manifest: m, + }) + if workspaceErr != nil { + // Legacy sections remain useful and shape-compatible. They use + // the selected (possibly invalid) absolute path but do not turn + // workspace invalidity into a command-level failure. + deduction.Detail = workspaceErr.Error() } report := doctorReport{ Report: base, + LauncherPreflight: preflight, HostBinding: doctorHostBinding(a, root), HostDeduction: deduction, Providers: doctorProviders(a), @@ -166,6 +206,8 @@ func doctorCommand(streams *iostreams.Streams) *cobra.Command { }, } cmd.Flags().StringVar(&workspace, "workspace", "", "workspace root path (defaults to the current directory)") + cmd.Flags().StringVar(&configPath, "config", "", "path to the duo.config/v3 document (defaults to $XDG_CONFIG_HOME/duo/duo.config.yaml)") + cmd.Flags().StringVar(&hostFlag, "host", "", `the session host to diagnose, "" or ":", using launch precedence`) surface.Annotate(cmd, surface.Plumbing) return cmd } @@ -177,6 +219,7 @@ func humanReport(report doctorReport) string { var b strings.Builder b.WriteString("duo doctor\n") + writeLauncherPreflightSection(&b, report.LauncherPreflight) fmt.Fprintf(&b, " store: %s\n", report.Store.Path) switch { case !report.Store.Present: @@ -228,12 +271,15 @@ func humanReport(report doctorReport) string { report.RecoveringInstances, noun) } - if report.HarnessSweep.Reaped > 0 { + if report.HarnessSweep.Orphaned > 0 { noun := "directories" - if report.HarnessSweep.Reaped == 1 { + if report.HarnessSweep.Orphaned == 1 { noun = "directory" } - fmt.Fprintf(&b, " harness: reaped %d orphan %s\n", report.HarnessSweep.Reaped, noun) + fmt.Fprintf(&b, " harness: found %d orphan %s (read-only; nothing reaped)\n", report.HarnessSweep.Orphaned, noun) + } + if report.HarnessSweep.Error != "" { + fmt.Fprintf(&b, " harness: inspection unavailable (%s)\n", report.HarnessSweep.Error) } return b.String() @@ -267,6 +313,7 @@ func humanReport(report doctorReport) string { // an additive top-level key, never a rename. type doctorReport struct { doctor.Report + LauncherPreflight doctor.LauncherPreflight `json:"launcher_preflight"` HostBinding workspaceHostShowResult `json:"host_binding"` HostDeduction doctorHostDeductionSection `json:"host_deduction"` Providers []doctorProviderStanding `json:"providers"` @@ -277,10 +324,8 @@ type doctorReport struct { // no host was deduced / the listener environ could not be observed. // Doctor warns; launch still refuses. ScrubGate *doctorScrubGateWarning `json:"scrub_gate,omitempty"` - // HarnessSweep is the notes/51 9a reaper: directories under - // $XDG_DATA_HOME/duo/harness// whose launch-resolution id has no - // matching live (non-terminal) runtime instance, including dirs whose - // launch never committed a record. Filesystem-only; no socket dial. + // HarnessSweep preserves the legacy key while now carrying a read-only + // inspection: orphan directories are named but Reaped remains zero. HarnessSweep doctor.HarnessSweep `json:"harness_sweep"` // DevinProjection is the launch-workspace hook projection and its // session-start drift status. It is additive so existing doctor readers @@ -288,6 +333,24 @@ type doctorReport struct { DevinProjection runtimedevin.ProjectionInspection `json:"devin_projection"` } +func writeLauncherPreflightSection(b *strings.Builder, p doctor.LauncherPreflight) { + status := strings.ReplaceAll(p.Status, "_", " ") + fmt.Fprintf(b, "launcher preflight: %s\n", status) + for _, check := range p.Checks { + label := strings.ReplaceAll(check.Status, "_", " ") + fmt.Fprintf(b, "[%s] %s: %s", label, check.ID, check.Summary) + if check.ID == doctor.CheckSkillProjection { + fmt.Fprintf(b, " (state=%s, identity=%s@%s, file=%s)", + p.SkillProjection.State, p.SkillProjection.FormatVersion, + p.SkillProjection.ContentDigest, p.SkillProjection.File) + } + b.WriteByte('\n') + if check.Required && check.Status != "pass" && check.Action != "" { + fmt.Fprintf(b, " action: %s\n", check.Action) + } + } +} + // doctorHostDeductionSection is what M1 would deduce right now for the // report's workspace. type doctorHostDeductionSection struct { @@ -555,7 +618,7 @@ func doctorDevinProjection(a *domain.Authority, root string) runtimedevin.Projec // launch would deduce, and a doctor that deduced from a smaller set of // inputs than the launcher would report a different answer than the one // the operator is about to get. -func doctorHostDeduction(ctx context.Context, a *domain.Authority, root string, policy config.SessionHostPolicy) doctorHostDeductionSection { +func doctorHostDeduction(ctx context.Context, a *domain.Authority, root, hostFlag string, policy config.SessionHostPolicy) doctorHostDeductionSection { section := doctorHostDeductionSection{ OutrankedEvidence: []doctorOutrankedEvidence{}, Ranking: doctorRanking(), @@ -563,6 +626,7 @@ func doctorHostDeduction(ctx context.Context, a *domain.Authority, root string, result, mErr := materialize.Materialize(ctx, materialize.Options{ WorkspaceFlag: root, + HostFlag: hostFlag, Policy: policy, Correlations: a, Providers: a, @@ -663,10 +727,11 @@ func doctorScrubGate(d doctorHostDeductionSection) *doctorScrubGateWarning { } } -// keepLiveHarness is the doctor sweep's keep predicate: a harness directory +// keepLiveHarness is the doctor inspection's keep predicate: a harness directory // named for a launch-resolution id stays only when that id still has a // committed record whose minted runtime instance is not terminal. No record -// (a refused launch, or any dir whose launch never committed) is reaped. +// (a refused launch, or any dir whose launch never committed) is reported +// orphaned, never reaped. // Terminal is InstanceState.Terminal — exited — not the recovering view // Open() derives on every load, which would otherwise reap every live // session the moment doctor opened the store. Session.Current is not @@ -708,30 +773,30 @@ func doctorProviders(a *domain.Authority) []doctorProviderStanding { // treats an empty SessionHostPolicy as "no enabled kind" at the // policy-default rung, which is the honest answer when there is no policy // to read. -func doctorConfigStatus(path string) (doctorConfigSection, config.SessionHostPolicy) { +func doctorConfigStatus(path string) (doctorConfigSection, config.SessionHostPolicy, config.DocumentV3) { section := doctorConfigSection{Path: path} if _, statErr := os.Stat(path); errors.Is(statErr, os.ErrNotExist) { section.Schema = "missing" section.Detail = fmt.Sprintf("no config file at %s", path) - return section, config.SessionHostPolicy{} + return section, config.SessionHostPolicy{}, config.DocumentV3{} } else if statErr != nil { section.Schema = "unreadable" section.Detail = statErr.Error() - return section, config.SessionHostPolicy{} + return section, config.SessionHostPolicy{}, config.DocumentV3{} } doc, err := config.LoadV3(path) if err == nil { section.Schema = config.SchemaV3 - return section, doc.SessionHosts + return section, doc.SessionHosts, doc } de, ok := err.(*duoerr.Error) if !ok { section.Schema = "unreadable" section.Detail = err.Error() - return section, config.SessionHostPolicy{} + return section, config.SessionHostPolicy{}, config.DocumentV3{} } switch de.Code { @@ -755,5 +820,318 @@ func doctorConfigStatus(path string) (doctorConfigSection, config.SessionHostPol section.Schema = "unreadable" section.Detail = de.Message } - return section, config.SessionHostPolicy{} + return section, config.SessionHostPolicy{}, config.DocumentV3{} +} + +// doctorWorkspace applies launch's --workspace > cwd precedence, then makes +// the selected path absolute and clean for the preflight wire report. +func doctorWorkspace(requested string) (string, doctor.WorkspacePreflight, error) { + status := doctor.WorkspacePreflight{RequestedPath: requested, Source: "cwd"} + if requested != "" { + status.Source = "flag" + } + selected, err := workspaceRoot(requested) + if err != nil { + return "", status, err + } + selected, err = filepath.Abs(selected) + if err != nil { + return "", status, fmt.Errorf("resolving selected workspace %q: %w", selected, err) + } + selected = filepath.Clean(selected) + status.SelectedPath = selected + info, err := os.Stat(selected) + if err != nil { + return selected, status, fmt.Errorf("workspace %q is not locally accessible: %w", selected, err) + } + status.Exists = true + status.Directory = info.IsDir() + if !status.Directory { + return selected, status, fmt.Errorf("workspace %q is not a directory", selected) + } + return selected, status, nil +} + +// openDoctorAuthority is diagnosis's zero-write authority seam. Unlike the +// older general read helper it cannot create/migrate a database because the +// SQLite handle itself is mode=ro. +func openDoctorAuthority(ctx context.Context, path string) (*domain.Authority, io.Closer, error) { + if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) { + a, openErr := emptyDoctorAuthority(ctx) + return a, nopCloser{}, openErr + } else if err != nil { + return nil, nil, err + } + s, err := store.OpenReadOnly(path) + if err != nil { + return nil, nil, err + } + a, err := domain.Open(ctx, storerepo.New(s)) + if err != nil { + _ = s.Close() + return nil, nil, fmt.Errorf("replaying authority store: %w", err) + } + return a, s, nil +} + +func emptyDoctorAuthority(ctx context.Context) (*domain.Authority, error) { + return domain.Open(ctx, emptyRepository{}) +} + +type doctorPreflightInput struct { + Build buildinfo.Info + Store doctor.StoreStatus + ReplayError error + ConfigPath string + ConfigSection doctorConfigSection + Config config.DocumentV3 + Workspace doctor.WorkspacePreflight + WorkspaceError error + Deduction doctorHostDeductionSection + Manifest manifest.Manifest +} + +func doctorLauncherPreflight(ctx context.Context, in doctorPreflightInput) doctor.LauncherPreflight { + p := doctor.NewLauncherPreflight() + doctorExecutableCheck(&p, in.Build) + doctorEffectiveConfigCheck(&p, in) + doctorAuthorityCheck(&p, in.Store, in.ReplayError) + doctorWorkspaceCheck(&p, in.Workspace, in.WorkspaceError) + doctorHostChecks(ctx, &p, in) + doctorProjectionCheck(&p, in) + p.Finalize() + return p +} + +func doctorExecutableCheck(p *doctor.LauncherPreflight, build buildinfo.Info) { + p.Duo.Version, p.Duo.Commit, p.Duo.BuildDate = build.Version, build.Commit, build.Date + executable, err := os.Executable() + if err == nil { + executable, err = filepath.Abs(executable) + } + if err == nil { + executable, err = filepath.EvalSymlinks(executable) + } + if err == nil { + var info os.FileInfo + info, err = os.Stat(executable) + if err == nil && info.IsDir() { + err = fmt.Errorf("resolved executable is a directory") + } + } + if err == nil { + p.Duo.ExecutablePath = filepath.Clean(executable) + } + if err != nil || build.Version == "" || build.Commit == "" || build.Date == "" { + summary := "running Duo executable does not have a complete local build identity" + action := "Executable stage: run the intended installed Duo binary and record its version, commit, and build date." + p.SetCheck(doctor.CheckDuoExecutable, "fail", "executable.identity_unavailable", summary, action) + return + } + p.SetCheck(doctor.CheckDuoExecutable, "pass", "ok", "running Duo executable has a reportable build identity", "") +} + +func doctorEffectiveConfigCheck(p *doctor.LauncherPreflight, in doctorPreflightInput) { + p.Config.Path = in.ConfigPath + p.Config.Schema = in.ConfigSection.Schema + if in.ConfigSection.Schema != config.SchemaV3 { + code := "config.invalid" + if in.ConfigSection.Schema == "missing" { + code = "config.missing" + } + p.SetCheck(doctor.CheckEffectiveConfig, "fail", code, + "effective launch config is not a valid duo.config/v3 document", + fmt.Sprintf("Config stage: install or fix duo.config/v3 at %s, or migrate a v2 document with duo config migrate --to duo.config/v3.", in.ConfigPath)) + return + } + digest, err := launch.ConfigurationDigest(in.Config) + if err != nil { + p.SetCheck(doctor.CheckEffectiveConfig, "fail", "config.invalid", "effective launch config could not be identified", + fmt.Sprintf("Config stage: fix the validated configuration at %s and rerun doctor.", in.ConfigPath)) + return + } + presets := make([]string, 0, len(in.Config.Presets)) + for name := range in.Config.Presets { + presets = append(presets, name) + } + sort.Strings(presets) + p.Config.Valid = true + p.Config.EffectiveDigest = digest + p.Config.Presets = presets + p.SetCheck(doctor.CheckEffectiveConfig, "pass", "ok", "effective duo.config/v3 intent is valid and deterministically identified", "") +} + +func doctorAuthorityCheck(p *doctor.LauncherPreflight, status doctor.StoreStatus, replayErr error) { + p.Authority = doctor.AuthorityPreflight{ + Path: status.Path, State: "unavailable", Present: status.Present, + Healthy: status.Healthy, SchemaVersion: status.SchemaVersion, + } + if status.Writer != nil { + p.Authority.WriterActive = status.Writer.Active + } + switch { + case !status.Present && status.Error == "" && replayErr == nil: + p.Authority.State = "initializable" + p.Authority.Healthy = true + p.SetCheck(doctor.CheckAuthorityStore, "pass", "ok", "authority store is absent and locally initializable by the first real write", "") + case status.Writer != nil && status.Writer.Active: + p.Authority.State = "writer_active" + p.Authority.Healthy = true + p.SetCheck(doctor.CheckAuthorityStore, "fail", "authority.writer_active", "authority store is held by an unexpired writer lease", + fmt.Sprintf("Authority stage: wait for or normally stop writer pid %d on %s for %s, then rerun doctor.", status.Writer.PID, status.Writer.Hostname, status.Path)) + case strings.Contains(status.Error, "unsupported schema"): + p.Authority.State = "incompatible" + p.SetCheck(doctor.CheckAuthorityStore, "fail", "authority.incompatible", "authority store schema is incompatible with this Duo build", + fmt.Sprintf("Authority stage: use a compatible Duo build for %s; doctor will not migrate it.", status.Path)) + case !status.Present && status.Error != "": + p.Authority.State = "unavailable" + p.SetCheck(doctor.CheckAuthorityStore, "fail", "authority.unavailable", "authority store path is not locally addressable", + fmt.Sprintf("Authority stage: make the XDG-selected path %s locally accessible, then rerun doctor.", status.Path)) + case replayErr != nil || (status.Present && status.Error != ""): + p.Authority.State = "unhealthy" + p.SetCheck(doctor.CheckAuthorityStore, "fail", "authority.unhealthy", "authority store is not readable and replayable", + fmt.Sprintf("Authority stage: inspect and repair or restore the local store at %s, then rerun doctor.", status.Path)) + default: + p.Authority.State = "ready" + p.SetCheck(doctor.CheckAuthorityStore, "pass", "ok", "authority store is readable, compatible, replayable, and has no active writer", "") + } +} + +func doctorWorkspaceCheck(p *doctor.LauncherPreflight, status doctor.WorkspacePreflight, err error) { + p.Workspace = status + if err != nil || status.SelectedPath == "" || !filepath.IsAbs(status.SelectedPath) || !status.Exists || !status.Directory { + p.SetCheck(doctor.CheckWorkspace, "fail", "workspace.invalid", "selected workspace is not an absolute accessible directory", + fmt.Sprintf("Workspace stage: pass the actual existing project root with --workspace; selected resource was %q.", status.SelectedPath)) + return + } + p.SetCheck(doctor.CheckWorkspace, "pass", "ok", "selected workspace is the absolute project root used for launch and skill lookup", "") +} + +func checkPassed(p *doctor.LauncherPreflight, id string) bool { + for _, check := range p.Checks { + if check.ID == id { + return check.Status == "pass" + } + } + return false +} + +// doctorProbeHerdr is injectable so CLI tests can pin the compatibility +// boundary without relying on an installed herdr schema-export binary. +var doctorProbeHerdr = func(ctx context.Context, cfg herdr.Config) (adapter.Probe, error) { + return (herdr.Factory{Config: cfg}).Probe(ctx) +} + +func doctorHostChecks(ctx context.Context, p *doctor.LauncherPreflight, in doctorPreflightInput) { + prerequisites := checkPassed(p, doctor.CheckEffectiveConfig) && checkPassed(p, doctor.CheckWorkspace) + if !prerequisites { + p.SetCheck(doctor.CheckHostSelection, "not_checked", "prerequisite.not_reached", "host selection was not evaluated because config or workspace failed", + "Host selection stage: fix the config and workspace findings, then rerun doctor with the intended --host value.") + p.SetCheck(doctor.CheckHostReachability, "not_checked", "prerequisite.not_reached", "host reachability was not checked because no trustworthy host was selected", + "Host reachability stage: resolve the prerequisite findings, then start or select the intended host.") + p.SetCheck(doctor.CheckHostCompatibility, "not_checked", "prerequisite.not_reached", "host compatibility was not checked because no host answered", "") + return + } + if in.Deduction.Host == nil { + p.SetCheck(doctor.CheckHostSelection, "fail", "launch.host_unresolved", "launch materialization did not deduce exactly one enabled host", + fmt.Sprintf("Host selection stage: supply --host, repair the workspace correlation, or correct host policy for %s.", in.Workspace.SelectedPath)) + p.SetCheck(doctor.CheckHostReachability, "not_checked", "prerequisite.not_reached", "host reachability was not checked because host selection failed", + "Host reachability stage: resolve host selection and rerun doctor.") + p.SetCheck(doctor.CheckHostCompatibility, "not_checked", "prerequisite.not_reached", "host compatibility was not checked because no host answered", "") + return + } + host := in.Deduction.Host + p.Host.Selected = true + p.Host.Kind = host.Kind + p.Host.InstanceID = host.InstanceID + p.Host.InstanceLabel = host.InstanceLabel + p.Host.HostSource = host.HostSource + p.SetCheck(doctor.CheckHostSelection, "pass", "ok", "launch materialization selected exactly one enabled host", "") + + if host.Kind != herdr.AdapterID { + p.Host.Compatibility = "unknown" + p.SetCheck(doctor.CheckHostReachability, "fail", "host.unreachable", "selected host has no bounded reachability probe in this build", + fmt.Sprintf("Host reachability stage: select a supported Herdr host for %s and rerun doctor.", host.InstanceLabel)) + p.SetCheck(doctor.CheckHostCompatibility, "not_checked", "prerequisite.not_reached", "host compatibility was not checked because no host answered", "") + return + } + probeID := host.InstanceID + if probeID == "" { + probeID = herdr.AdapterID + ":selected" + } + probeCtx, cancel := context.WithTimeout(ctx, 750*time.Millisecond) + defer cancel() + probe, err := doctorProbeHerdr(probeCtx, herdr.Config{ + IntegrationInstanceID: probeID, + SocketPath: host.InstanceLabel, + CallTimeout: 500 * time.Millisecond, + }) + if err != nil || probe.ConnectionState != "connected" { + p.Host.Compatibility = "unavailable" + p.SetCheck(doctor.CheckHostReachability, "fail", "host.unreachable", "selected Herdr host did not answer the bounded ping", + fmt.Sprintf("Host reachability stage: start or select the Herdr server at %s and make its socket reachable.", host.InstanceLabel)) + p.SetCheck(doctor.CheckHostCompatibility, "not_checked", "prerequisite.not_reached", "host compatibility was not checked because no host answered", "") + return + } + p.Host.Reachable = true + p.Host.DetectedVersion = probe.DetectedVersion + p.Host.ProtocolIdentity = probe.ProtocolOrFormatIdentity + p.Host.Compatibility = string(probe.Compatibility) + if p.Host.Compatibility == "" { + p.Host.Compatibility = "unknown" + } + p.SetCheck(doctor.CheckHostReachability, "pass", "ok", "selected Herdr host answered the bounded non-mutating ping", "") + if probe.Compatibility == adapter.CompatibilitySupported { + p.SetCheck(doctor.CheckHostCompatibility, "pass", "ok", "reachable host matches the pinned version, protocol, and schema evidence", "") + return + } + code := "host.compatibility_unverified" + if probe.Compatibility == adapter.CompatibilityIncompatible { + code = "host.compatibility_incompatible" + } + p.SetCheck(doctor.CheckHostCompatibility, "warning", code, "reachable host does not match the complete pinned compatibility evidence", "") +} + +func doctorProjectionCheck(p *doctor.LauncherPreflight, in doctorPreflightInput) { + target := in.Manifest.HarnessTargets[0] + root := filepath.Join(in.Workspace.SelectedPath, filepath.FromSlash(target.ProjectionRoot)) + p.SkillProjection = doctor.SkillProjection{ + Target: target.Name, Name: target.Artifact.Name, FormatVersion: target.ProjectionFormat, + ContentDigest: target.Artifact.ContentDigest, Root: root, + File: filepath.Join(root, filepath.FromSlash(target.Artifact.OutputPath)), + Stamp: filepath.Join(root, filepath.FromSlash(target.StampFile)), State: "incompatible", + } + if !checkPassed(p, doctor.CheckWorkspace) { + p.SetCheck(doctor.CheckSkillProjection, "not_checked", "prerequisite.not_reached", "portable skill projection was not inspected because workspace selection failed", + "Skill projection stage: pass an accessible project root with --workspace, then rerun doctor.") + return + } + inspection, err := manifest.InspectPortableLaunchers(in.Workspace.SelectedPath, in.Manifest) + if err != nil { + p.SetCheck(doctor.CheckSkillProjection, "fail", "projection.incompatible", "portable skill projection could not be safely inspected", + fmt.Sprintf("Skill projection stage: inspect or move the projection at %s, then run the installer when allowed.", root)) + return + } + p.SkillProjection.State = string(inspection.State) + p.SkillProjection.Root = inspection.Root + p.SkillProjection.File = filepath.Join(inspection.Root, manifest.PortableSkillFile) + p.SkillProjection.Stamp = filepath.Join(inspection.Root, manifest.ProjectionStampFile) + p.SkillProjection.InstallationID = inspection.InstallationID + if inspection.State == manifest.StateCurrent { + p.SetCheck(doctor.CheckSkillProjection, "pass", "ok", "portable skill projection is current for this Duo manifest", "") + return + } + code := map[manifest.ProjectionState]string{ + manifest.StateMissing: "projection.missing", + manifest.StateStale: "projection.stale", + manifest.StateModified: "projection.modified", + manifest.StateIncompatible: "projection.incompatible", + manifest.StateUnownedConflict: "projection.user_file_conflict", + }[inspection.State] + if code == "" { + code = "projection.incompatible" + } + p.SetCheck(doctor.CheckSkillProjection, "fail", code, + fmt.Sprintf("portable skill projection is %s for this Duo manifest", inspection.State), + fmt.Sprintf("Skill projection stage: inspect %s and run duo install portable-launchers --workspace %s --repair when allowed; move modified or unowned content first.", inspection.Root, in.Workspace.SelectedPath)) } diff --git a/internal/cli/doctor_harness_test.go b/internal/cli/doctor_harness_test.go index 28e9173..3a495b4 100644 --- a/internal/cli/doctor_harness_test.go +++ b/internal/cli/doctor_harness_test.go @@ -26,7 +26,7 @@ func plantHarnessDir(t *testing.T, lrr, leaf string) string { return dir } -func doctorHarnessSweepJSON(t *testing.T, extraArgs ...string) (reaped int, out string) { +func doctorHarnessInspectionJSON(t *testing.T, extraArgs ...string) (orphaned int, out string) { t.Helper() args := append([]string{"doctor", "--output", "json"}, extraArgs...) code, stdout, errOut := runSession(t, args...) @@ -35,21 +35,25 @@ func doctorHarnessSweepJSON(t *testing.T, extraArgs ...string) (reaped int, out } var report struct { HarnessSweep struct { - Reaped int `json:"reaped"` - Kept int `json:"kept"` - IDs []string `json:"ids"` + Reaped int `json:"reaped"` + Kept int `json:"kept"` + Orphaned int `json:"orphaned"` + ReadOnly bool `json:"read_only"` + IDs []string `json:"ids"` } `json:"harness_sweep"` } if err := json.Unmarshal([]byte(stdout), &report); err != nil { t.Fatalf("decoding doctor JSON: %v\n%s", err, stdout) } - return report.HarnessSweep.Reaped, stdout + if !report.HarnessSweep.ReadOnly || report.HarnessSweep.Reaped != 0 { + t.Fatalf("doctor mutated harness state: %+v", report.HarnessSweep) + } + return report.HarnessSweep.Orphaned, stdout } -// TestDoctorReapsRefusedLaunchHarnessDir is notes/51 9a: a harness directory -// whose launch-resolution id has no durable record (a refused launch, or -// any materialize that never committed) is gone after doctor. -func TestDoctorReapsRefusedLaunchHarnessDir(t *testing.T) { +// TestDoctorReportsRefusedLaunchHarnessDirReadOnly proves diagnosis reports +// an orphan but leaves cleanup to an explicit write operation. +func TestDoctorReportsRefusedLaunchHarnessDirReadOnly(t *testing.T) { t.Setenv("XDG_DATA_HOME", t.TempDir()) t.Setenv("XDG_CONFIG_HOME", t.TempDir()) clearAmbientHerdrEnv(t) @@ -59,12 +63,12 @@ func TestDoctorReapsRefusedLaunchHarnessDir(t *testing.T) { t.Fatalf("planted harness dir missing before doctor: %v", err) } - reaped, stdout := doctorHarnessSweepJSON(t) - if reaped != 1 { - t.Errorf("harness_sweep.reaped = %d, want 1\n%s", reaped, stdout) + orphaned, stdout := doctorHarnessInspectionJSON(t) + if orphaned != 1 { + t.Errorf("harness_sweep.orphaned = %d, want 1\n%s", orphaned, stdout) } - if _, err := os.Stat(dir); !os.IsNotExist(err) { - t.Errorf("refused launch harness dir still present after doctor: %v", err) + if _, err := os.Stat(dir); err != nil { + t.Errorf("doctor removed refused-launch harness dir: %v", err) } } @@ -90,19 +94,18 @@ func TestDoctorKeepsLiveSessionHarnessDir(t *testing.T) { root := h.root h.close() - reaped, _ := doctorHarnessSweepJSON(t, "--workspace", root) - if reaped != 0 { - t.Errorf("harness_sweep.reaped = %d, want 0 for a live session", reaped) + orphaned, _ := doctorHarnessInspectionJSON(t, "--workspace", root) + if orphaned != 0 { + t.Errorf("harness_sweep.orphaned = %d, want 0 for a live session", orphaned) } if _, err := os.Stat(dir); err != nil { t.Errorf("live session harness dir was reaped: %v", err) } } -// TestDoctorReapsTerminalSessionHarnessDir: once the runtime instance is -// exited, doctor removes the harness directory. Close-on-exit no longer -// needs the files. -func TestDoctorReapsTerminalSessionHarnessDir(t *testing.T) { +// TestDoctorReportsTerminalSessionHarnessDirReadOnly: a terminal runtime is +// reported as orphaned, but doctor does not perform cleanup. +func TestDoctorReportsTerminalSessionHarnessDirReadOnly(t *testing.T) { h := newBindHarness(t, nil) clearAmbientHerdrEnv(t) @@ -125,15 +128,15 @@ func TestDoctorReapsTerminalSessionHarnessDir(t *testing.T) { root := h.root h.close() - reaped, stdout := doctorHarnessSweepJSON(t, "--workspace", root) - if reaped != 1 { - t.Errorf("harness_sweep.reaped = %d, want 1 for a terminal session\n%s", reaped, stdout) + orphaned, stdout := doctorHarnessInspectionJSON(t, "--workspace", root) + if orphaned != 1 { + t.Errorf("harness_sweep.orphaned = %d, want 1 for a terminal session\n%s", orphaned, stdout) } - if _, err := os.Stat(dir); !os.IsNotExist(err) { - t.Errorf("terminal session harness dir still present after doctor: %v", err) + if _, err := os.Stat(dir); err != nil { + t.Errorf("doctor removed terminal-session harness dir: %v", err) } parent := filepath.Dir(dir) - if _, err := os.Stat(parent); !os.IsNotExist(err) { - t.Errorf("terminal session lrr directory still present after doctor: %v", err) + if _, err := os.Stat(parent); err != nil { + t.Errorf("doctor removed terminal-session lrr directory: %v", err) } } diff --git a/internal/cli/doctor_preflight_test.go b/internal/cli/doctor_preflight_test.go new file mode 100644 index 0000000..4cf37c7 --- /dev/null +++ b/internal/cli/doctor_preflight_test.go @@ -0,0 +1,385 @@ +package cli + +import ( + "bytes" + "context" + "encoding/json" + "os" + "path/filepath" + "strings" + "testing" + + "github.com/procrastivity/duo/internal/adapter" + "github.com/procrastivity/duo/internal/config" + "github.com/procrastivity/duo/internal/doctor" + "github.com/procrastivity/duo/internal/exitcode" + "github.com/procrastivity/duo/internal/host/herdr" + "github.com/procrastivity/duo/internal/launch" + "github.com/procrastivity/duo/internal/manifest" + "github.com/procrastivity/duo/internal/store" +) + +const doctorPreflightConfig = ` +schema: duo.config/v3 +session_hosts: + prefer: [herdr] +agent_runtimes: + pi_default: + kind: pi + executable: pi +launch_variants: + builder: + agent_runtime: pi_default + model_line: openai/gpt-5.3-codex + model_family: gpt +presets: + zeta: + leaves: + main: + candidates: [{variant: builder}] + builder: + leaves: + main: + candidates: [{variant: builder}] +` + +func prepareDoctorPreflight(t *testing.T) (workspace, configPath, socket string) { + t.Helper() + t.Setenv("XDG_DATA_HOME", filepath.Join(t.TempDir(), "data")) + t.Setenv("XDG_CONFIG_HOME", filepath.Join(t.TempDir(), "config")) + clearAmbientHerdrEnv(t) + workspace = t.TempDir() + configPath = filepath.Join(t.TempDir(), "duo.config.yaml") + if err := os.WriteFile(configPath, []byte(doctorPreflightConfig), 0o600); err != nil { + t.Fatalf("WriteFile config: %v", err) + } + code, _, stderr := runSession(t, "install", "portable-launchers", "--workspace", workspace) + if code != exitcode.Success { + t.Fatalf("install portable projection: exit %d: %s", code, stderr) + } + socket = filepath.Join(t.TempDir(), "herdr.sock") + return workspace, configPath, socket +} + +func useDoctorProbe(t *testing.T, probe adapter.Probe, err error) { + t.Helper() + previous := doctorProbeHerdr + doctorProbeHerdr = func(context.Context, herdr.Config) (adapter.Probe, error) { return probe, err } + t.Cleanup(func() { doctorProbeHerdr = previous }) +} + +func runLauncherPreflight(t *testing.T, args ...string) (doctor.LauncherPreflight, string) { + t.Helper() + code, stdout, stderr := runSession(t, append([]string{"doctor", "--output", "json"}, args...)...) + if code != exitcode.Success { + t.Fatalf("doctor exit = %d, want 0 (stderr: %s)", code, stderr) + } + var report struct { + Preflight doctor.LauncherPreflight `json:"launcher_preflight"` + } + if err := json.Unmarshal([]byte(stdout), &report); err != nil { + t.Fatalf("decode doctor JSON: %v\n%s", err, stdout) + } + return report.Preflight, stdout +} + +func preflightCheck(t *testing.T, p doctor.LauncherPreflight, id string) doctor.Check { + t.Helper() + for _, check := range p.Checks { + if check.ID == id { + return check + } + } + t.Fatalf("preflight has no %s check: %+v", id, p.Checks) + return doctor.Check{} +} + +func TestDoctorLauncherPreflightReadyIsClosedDeterministicAndReadOnly(t *testing.T) { + workspace, configPath, socket := prepareDoctorPreflight(t) + useDoctorProbe(t, adapter.Probe{ + DetectedVersion: herdr.PinnedVersion, + ProtocolOrFormatIdentity: "herdr-socket-api/20", + FixtureOrSchemaDigest: herdr.PinnedSchemaDigest, + ConnectionState: "connected", + Compatibility: adapter.CompatibilitySupported, + }, nil) + before := snapshotTree(t, workspace) + dataHome := os.Getenv("XDG_DATA_HOME") + + p, _ := runLauncherPreflight(t, "--workspace", workspace, "--config", configPath, "--host", "herdr:"+socket) + + if p.Schema != doctor.LauncherPreflightSchema || p.Status != "ready" || p.AuthorityScope != "local" { + t.Fatalf("preflight identity/status = %q/%q/%q, want schema/ready/local", p.Schema, p.Status, p.AuthorityScope) + } + wantIDs := []string{ + doctor.CheckDuoExecutable, doctor.CheckEffectiveConfig, doctor.CheckAuthorityStore, + doctor.CheckWorkspace, doctor.CheckHostSelection, doctor.CheckHostReachability, + doctor.CheckSkillProjection, doctor.CheckHostCompatibility, + } + if len(p.Checks) != len(wantIDs) { + t.Fatalf("checks = %d, want exactly 8", len(p.Checks)) + } + for i, id := range wantIDs { + if p.Checks[i].ID != id { + t.Errorf("checks[%d].id = %q, want %q", i, p.Checks[i].ID, id) + } + if p.Checks[i].Status != "pass" || p.Checks[i].Code != "ok" { + t.Errorf("check %s = %s/%s, want pass/ok", id, p.Checks[i].Status, p.Checks[i].Code) + } + } + if p.Duo.Version != "v0.1.0-test" || p.Duo.Commit != "abcdef0" || p.Duo.BuildDate != "2026-08-23T00:00:00Z" || !filepath.IsAbs(p.Duo.ExecutablePath) { + t.Errorf("duo identity = %+v", p.Duo) + } + doc, err := config.LoadV3(configPath) + if err != nil { + t.Fatalf("LoadV3: %v", err) + } + wantConfigDigest, err := launch.ConfigurationDigest(doc) + if err != nil { + t.Fatalf("ConfigurationDigest: %v", err) + } + if p.Config.Path != configPath || p.Config.Schema != "duo.config/v3" || !p.Config.Valid || + p.Config.EffectiveDigest != wantConfigDigest || strings.Join(p.Config.Presets, ",") != "builder,zeta" { + t.Errorf("config identity = %+v", p.Config) + } + if p.Authority.State != "initializable" || p.Authority.Present { + t.Errorf("authority = %+v, want absent initializable", p.Authority) + } + if p.Workspace.SelectedPath != workspace || p.Workspace.Source != "flag" || !p.Workspace.Exists || !p.Workspace.Directory { + t.Errorf("workspace = %+v", p.Workspace) + } + if !p.Host.Selected || !p.Host.Reachable || p.Host.InstanceLabel != socket || p.Host.HostSource != "explicit-flag" || p.Host.Compatibility != "supported" { + t.Errorf("host = %+v", p.Host) + } + if p.SkillProjection.State != "current" || p.SkillProjection.Name != "duo-delegation-loop" || + p.SkillProjection.File != filepath.Join(workspace, filepath.FromSlash(manifest.PortableProjectionRoot), manifest.PortableSkillFile) { + t.Errorf("skill projection = %+v", p.SkillProjection) + } + if after := snapshotTree(t, workspace); !bytes.Equal(after, before) { + t.Fatalf("doctor changed workspace\nbefore: %s\nafter: %s", before, after) + } + if _, err := os.Stat(dataHome); !os.IsNotExist(err) { + t.Fatalf("doctor created the missing XDG data root: %v", err) + } +} + +func TestDoctorLauncherPreflightUnreachableAndModifiedAreNotReadyButExitZero(t *testing.T) { + workspace, configPath, socket := prepareDoctorPreflight(t) + useDoctorProbe(t, adapter.Probe{ + ConnectionState: "unreachable", Compatibility: adapter.CompatibilityUnavailable, + }, nil) + + p, _ := runLauncherPreflight(t, "--workspace", workspace, "--config", configPath, "--host", "herdr:"+socket) + if p.Status != "not_ready" || p.AuthorityScope != "unavailable" { + t.Fatalf("status/scope = %s/%s, want not_ready/unavailable", p.Status, p.AuthorityScope) + } + if check := preflightCheck(t, p, doctor.CheckHostReachability); check.Status != "fail" || check.Code != "host.unreachable" || check.Action == "" { + t.Errorf("host reachability check = %+v", check) + } + if check := preflightCheck(t, p, doctor.CheckHostCompatibility); check.Status != "not_checked" { + t.Errorf("host compatibility check = %+v", check) + } + + skill := filepath.Join(workspace, filepath.FromSlash(manifest.PortableProjectionRoot), manifest.PortableSkillFile) + if err := os.WriteFile(skill, []byte("locally modified\n"), 0o644); err != nil { + t.Fatalf("modify skill: %v", err) + } + before := snapshotTree(t, workspace) + p, _ = runLauncherPreflight(t, "--workspace", workspace, "--config", configPath, "--host", "herdr:"+socket) + if p.SkillProjection.State != "modified" { + t.Fatalf("projection state = %q, want modified", p.SkillProjection.State) + } + if check := preflightCheck(t, p, doctor.CheckSkillProjection); check.Status != "fail" || check.Code != "projection.modified" { + t.Errorf("projection check = %+v", check) + } + if after := snapshotTree(t, workspace); !bytes.Equal(after, before) { + t.Fatalf("doctor repaired modified projection\nbefore: %s\nafter: %s", before, after) + } +} + +func TestDoctorLauncherPreflightFreshMissingProjectionIsNotReady(t *testing.T) { + workspace, configPath, socket := prepareDoctorPreflight(t) + projectionRoot := filepath.Join(workspace, filepath.FromSlash(manifest.PortableProjectionRoot)) + if err := os.RemoveAll(projectionRoot); err != nil { + t.Fatalf("remove installed projection for fresh-workspace case: %v", err) + } + useDoctorProbe(t, adapter.Probe{ + DetectedVersion: herdr.PinnedVersion, + ProtocolOrFormatIdentity: "herdr-socket-api/20", + ConnectionState: "connected", + Compatibility: adapter.CompatibilitySupported, + }, nil) + + p, _ := runLauncherPreflight(t, "--workspace", workspace, "--config", configPath, "--host", "herdr:"+socket) + if p.Status != "not_ready" || p.Authority.State != "initializable" || p.SkillProjection.State != "missing" { + t.Fatalf("status/authority/projection = %s/%s/%s", p.Status, p.Authority.State, p.SkillProjection.State) + } + if check := preflightCheck(t, p, doctor.CheckSkillProjection); check.Status != "fail" || check.Code != "projection.missing" { + t.Errorf("projection check = %+v", check) + } + if _, err := os.Stat(projectionRoot); !os.IsNotExist(err) { + t.Fatalf("doctor created missing projection: %v", err) + } +} + +func TestDoctorLauncherPreflightUnverifiedCompatibilityWarnsButRemainsReady(t *testing.T) { + workspace, configPath, socket := prepareDoctorPreflight(t) + useDoctorProbe(t, adapter.Probe{ + DetectedVersion: "0.9.0", + ProtocolOrFormatIdentity: "herdr-socket-api/21", + ConnectionState: "connected", + Compatibility: adapter.CompatibilityUnverified, + }, nil) + + p, _ := runLauncherPreflight(t, "--workspace", workspace, "--config", configPath, "--host", "herdr:"+socket) + if p.Status != "ready" || p.AuthorityScope != "local" { + t.Fatalf("status/scope = %s/%s, want ready/local", p.Status, p.AuthorityScope) + } + if check := preflightCheck(t, p, doctor.CheckHostCompatibility); check.Status != "warning" || check.Code != "host.compatibility_unverified" || check.Required { + t.Errorf("host compatibility = %+v, want informational warning", check) + } +} + +func TestDoctorLauncherPreflightUnavailableResourcesUseDependentNotChecked(t *testing.T) { + dataHome := filepath.Join(t.TempDir(), "absent-data") + t.Setenv("XDG_DATA_HOME", dataHome) + t.Setenv("XDG_CONFIG_HOME", filepath.Join(t.TempDir(), "absent-config")) + clearAmbientHerdrEnv(t) + missingWorkspace := filepath.Join(t.TempDir(), "missing-workspace") + missingConfig := filepath.Join(t.TempDir(), "missing-config.yaml") + + p, _ := runLauncherPreflight(t, "--workspace", missingWorkspace, "--config", missingConfig, "--host", "herdr:/missing/herdr.sock") + if p.Status != "not_ready" || p.AuthorityScope != "unavailable" { + t.Fatalf("status/scope = %s/%s", p.Status, p.AuthorityScope) + } + if check := preflightCheck(t, p, doctor.CheckEffectiveConfig); check.Code != "config.missing" || check.Action == "" { + t.Errorf("config check = %+v", check) + } + if check := preflightCheck(t, p, doctor.CheckAuthorityStore); check.Status != "pass" || p.Authority.State != "initializable" { + t.Errorf("authority check/state = %+v/%s", check, p.Authority.State) + } + if check := preflightCheck(t, p, doctor.CheckWorkspace); check.Code != "workspace.invalid" || check.Action == "" { + t.Errorf("workspace check = %+v", check) + } + for _, id := range []string{doctor.CheckHostSelection, doctor.CheckHostReachability, doctor.CheckSkillProjection, doctor.CheckHostCompatibility} { + if check := preflightCheck(t, p, id); check.Status != "not_checked" { + t.Errorf("%s = %+v, want not_checked", id, check) + } + } + if _, err := os.Stat(dataHome); !os.IsNotExist(err) { + t.Fatalf("doctor created unavailable local authority root: %v", err) + } +} + +func TestDoctorLauncherPreflightReportsWriterWithoutChangingLease(t *testing.T) { + workspace, configPath, socket := prepareDoctorPreflight(t) + useDoctorProbe(t, adapter.Probe{ConnectionState: "connected", Compatibility: adapter.CompatibilitySupported}, nil) + storePath, err := doctor.DefaultStorePath() + if err != nil { + t.Fatalf("DefaultStorePath: %v", err) + } + writer, err := store.OpenAuthority(storePath) + if err != nil { + t.Fatalf("OpenAuthority: %v", err) + } + t.Cleanup(func() { _ = writer.Close() }) + incarnation := writer.Incarnation() + + p, _ := runLauncherPreflight(t, "--workspace", workspace, "--config", configPath, "--host", "herdr:"+socket) + if p.Authority.State != "writer_active" || !p.Authority.WriterActive { + t.Fatalf("authority = %+v, want writer_active", p.Authority) + } + if p.Status != "not_ready" || p.AuthorityScope != "local" { + t.Errorf("status/scope = %s/%s, want not_ready/local for a reachable local store with an active writer", p.Status, p.AuthorityScope) + } + if check := preflightCheck(t, p, doctor.CheckAuthorityStore); check.Code != "authority.writer_active" || check.Status != "fail" { + t.Errorf("authority check = %+v", check) + } + if writer.Incarnation() != incarnation { + t.Errorf("writer incarnation changed: %q != %q", writer.Incarnation(), incarnation) + } + if err := writer.RenewLease(context.Background()); err != nil { + t.Fatalf("doctor disrupted active writer lease: %v", err) + } +} + +func TestDoctorLauncherPreflightStillChecksExplicitHostWhenAuthorityUnavailable(t *testing.T) { + workspace, configPath, socket := prepareDoctorPreflight(t) + dataHome := os.Getenv("XDG_DATA_HOME") + if err := os.WriteFile(dataHome, []byte("not a directory\n"), 0o600); err != nil { + t.Fatalf("block authority data root: %v", err) + } + useDoctorProbe(t, adapter.Probe{ + DetectedVersion: herdr.PinnedVersion, + ProtocolOrFormatIdentity: "herdr-socket-api/20", + FixtureOrSchemaDigest: herdr.PinnedSchemaDigest, + ConnectionState: "connected", + Compatibility: adapter.CompatibilitySupported, + }, nil) + + p, _ := runLauncherPreflight(t, "--workspace", workspace, "--config", configPath, "--host", "herdr:"+socket) + if p.Status != "not_ready" || p.AuthorityScope != "unavailable" || p.Authority.State != "unavailable" { + t.Fatalf("status/scope/authority = %s/%s/%s", p.Status, p.AuthorityScope, p.Authority.State) + } + for _, id := range []string{doctor.CheckHostSelection, doctor.CheckHostReachability, doctor.CheckHostCompatibility, doctor.CheckSkillProjection} { + if check := preflightCheck(t, p, id); check.Status != "pass" { + t.Errorf("independent check %s = %+v, want pass", id, check) + } + } +} + +func TestDoctorLauncherPreflightCorruptAuthorityIsTrustworthyNotReadyAndUnchanged(t *testing.T) { + t.Setenv("XDG_DATA_HOME", filepath.Join(t.TempDir(), "data")) + t.Setenv("XDG_CONFIG_HOME", filepath.Join(t.TempDir(), "config")) + clearAmbientHerdrEnv(t) + storePath, err := doctor.DefaultStorePath() + if err != nil { + t.Fatalf("DefaultStorePath: %v", err) + } + if err := os.MkdirAll(filepath.Dir(storePath), 0o700); err != nil { + t.Fatalf("MkdirAll store parent: %v", err) + } + before := []byte("not a sqlite authority database\n") + if err := os.WriteFile(storePath, before, 0o600); err != nil { + t.Fatalf("WriteFile corrupt store: %v", err) + } + + p, _ := runLauncherPreflight(t, "--workspace", t.TempDir(), "--config", filepath.Join(t.TempDir(), "missing.yaml")) + if p.Status != "not_ready" || p.Authority.State != "unhealthy" { + t.Fatalf("status/authority = %s/%s, want not_ready/unhealthy", p.Status, p.Authority.State) + } + if check := preflightCheck(t, p, doctor.CheckAuthorityStore); check.Status != "fail" || check.Code != "authority.unhealthy" { + t.Errorf("authority check = %+v", check) + } + after, err := os.ReadFile(storePath) + if err != nil { + t.Fatalf("ReadFile corrupt store after doctor: %v", err) + } + if !bytes.Equal(after, before) { + t.Fatalf("doctor changed corrupt authority bytes: %q", after) + } +} + +func TestDoctorLauncherPreflightHumanRendersHeadingChecksAndActions(t *testing.T) { + t.Setenv("XDG_DATA_HOME", filepath.Join(t.TempDir(), "data")) + t.Setenv("XDG_CONFIG_HOME", filepath.Join(t.TempDir(), "config")) + clearAmbientHerdrEnv(t) + workspace := t.TempDir() + code, stdout, stderr := runSession(t, "doctor", "--workspace", workspace) + if code != exitcode.Success { + t.Fatalf("doctor exit = %d: %s", code, stderr) + } + for _, want := range []string{ + "launcher preflight: not ready", + "[pass] duo_executable:", + "[fail] effective_config:", + "[not checked] host_selection:", + "[fail] skill_projection:", + "[not checked] host_compatibility:", + "action: Config stage:", + } { + if !strings.Contains(stdout, want) { + t.Errorf("human output missing %q:\n%s", want, stdout) + } + } +} diff --git a/internal/cli/doctor_visibility_test.go b/internal/cli/doctor_visibility_test.go index 6bb2b02..57cd6a1 100644 --- a/internal/cli/doctor_visibility_test.go +++ b/internal/cli/doctor_visibility_test.go @@ -358,10 +358,8 @@ func TestDoctorVisibility_DisabledProvider(t *testing.T) { } // TestDoctorVisibility_ConfigMissing covers the plainly-stated "missing" -// config case: no --config flag exists on `duo doctor`, and this test's -// isolated XDG_CONFIG_HOME never has a duo.config document written into -// it, so the default launch-config path resolves to a file that is not -// there. +// default config case: this test's isolated XDG_CONFIG_HOME never has a +// duo.config document written into it. func TestDoctorVisibility_ConfigMissing(t *testing.T) { t.Setenv("XDG_DATA_HOME", t.TempDir()) t.Setenv("XDG_CONFIG_HOME", t.TempDir()) diff --git a/internal/cli/install.go b/internal/cli/install.go new file mode 100644 index 0000000..4da45e4 --- /dev/null +++ b/internal/cli/install.go @@ -0,0 +1,121 @@ +package cli + +import ( + "encoding/json" + "errors" + "fmt" + + "github.com/spf13/cobra" + + "github.com/procrastivity/duo/internal/buildinfo" + "github.com/procrastivity/duo/internal/cliflags" + "github.com/procrastivity/duo/internal/duoerr" + "github.com/procrastivity/duo/internal/iostreams" + "github.com/procrastivity/duo/internal/manifest" + "github.com/procrastivity/duo/internal/registry" + "github.com/procrastivity/duo/internal/surface" +) + +const portableInstallOperation = "projection.install" + +func installCommand(streams *iostreams.Streams, build buildinfo.Info, root *cobra.Command) *cobra.Command { + cmd := &cobra.Command{ + Use: "install", + Short: "install product-owned workspace projections", + } + cmd.AddCommand(portableLaunchersInstallCommand(streams, build, root)) + return cmd +} + +func portableLaunchersInstallCommand(streams *iostreams.Streams, build buildinfo.Info, root *cobra.Command) *cobra.Command { + var workspace string + var repair bool + cmd := &cobra.Command{ + Use: "portable-launchers", + Short: "install the shared Amp, OpenCode, and Codex filesystem skill", + Args: cobra.NoArgs, + RunE: func(cmd *cobra.Command, _ []string) error { + m, err := manifest.Build(root, build) + if err != nil { + return duoerr.New("internal.manifest_build_failed", fmt.Sprintf("building the installation manifest: %v", err)) + } + result, err := manifest.InstallPortableLaunchers(workspace, repair, m) + if err != nil { + var projectionErr *manifest.ProjectionError + if errors.As(err, &projectionErr) { + return writePortableInstallFailure(streams, cliflags.FromContext(cmd.Context()).Output, projectionErr) + } + return duoerr.New("internal.projection_install_failed", err.Error()) + } + + if cliflags.FromContext(cmd.Context()).JSON() { + b, err := json.Marshal(newEnvelope(portableInstallOperation, result)) + if err != nil { + return duoerr.New("internal.projection_install_failed", fmt.Sprintf("encoding installation result: %v", err)) + } + _, err = fmt.Fprintln(streams.Out, string(b)) + return err + } + _, err = fmt.Fprintf(streams.Out, "portable-launchers: %s (changed: %t)\nroot: %s\nidentity: %s@%s\ninstallation: %s\n", + result.State, result.Changed, result.Root, result.FormatVersion, result.ContentDigest, result.InstallationID) + return err + }, + } + cmd.Flags().StringVar(&workspace, "workspace", "", "workspace root (default: current directory)") + cmd.Flags().BoolVar(&repair, "repair", false, "repair an owned missing or stale projection") + surface.Annotate(cmd, surface.Plumbing) + return cmd +} + +type installRetryAdvice struct { + Safe bool `json:"safe"` + Action string `json:"action"` +} + +func writePortableInstallFailure(streams *iostreams.Streams, mode string, failure *manifest.ProjectionError) error { + if mode != cliflags.OutputJSON { + return duoerr.New(failure.Code, failure.Message) + } + action := map[string]string{ + "invalid.precondition": "inspect_and_retry_with_repair", + "projection.modified": "review_modified_projection", + "projection.incompatible": "upgrade_or_move_projection", + "projection.user_file_conflict": "move_unowned_content", + }[failure.Code] + if action == "" { + action = "inspect_projection" + } + class := "internal" + if registered, ok := registry.StableErrorCodes()[failure.Code]; ok { + class = string(registered) + } + envelope := struct { + Schema string `json:"schema"` + RequestID string `json:"request_id"` + Operation string `json:"operation"` + Error struct { + Class string `json:"class"` + Code string `json:"code"` + Message string `json:"message"` + Retry installRetryAdvice `json:"retry"` + Effect string `json:"effect"` + } `json:"error"` + }{ + Schema: "duo.external/v1", + RequestID: requestID(), + Operation: portableInstallOperation, + } + envelope.Error.Class = class + envelope.Error.Code = failure.Code + envelope.Error.Message = failure.Message + envelope.Error.Retry = installRetryAdvice{Safe: false, Action: action} + envelope.Error.Effect = "no_effect" + b, err := json.Marshal(envelope) + if err != nil { + return duoerr.New("internal.projection_install_failed", fmt.Sprintf("encoding installation failure: %v", err)) + } + if _, err := fmt.Fprintln(streams.Err, string(b)); err != nil { + return err + } + return &failureWrittenError{code: failure.Code} +} diff --git a/internal/cli/install_test.go b/internal/cli/install_test.go new file mode 100644 index 0000000..b8137a4 --- /dev/null +++ b/internal/cli/install_test.go @@ -0,0 +1,152 @@ +package cli + +import ( + "bytes" + "encoding/json" + "os" + "path/filepath" + "testing" + + "github.com/procrastivity/duo/internal/buildinfo" + "github.com/procrastivity/duo/internal/exitcode" + "github.com/procrastivity/duo/internal/iostreams" + "github.com/procrastivity/duo/internal/manifest" +) + +var installTestBuild = buildinfo.Info{Version: "v-test", Commit: "abc", Date: "2026-09-17T00:00:00Z"} + +func runInstallCLI(t *testing.T, args ...string) (int, string, string) { + t.Helper() + out, errOut := &bytes.Buffer{}, &bytes.Buffer{} + streams := &iostreams.Streams{Out: out, Err: errOut} + root := NewRootCommand(streams, installTestBuild) + root.SetArgs(args) + return Execute(root, streams), out.String(), errOut.String() +} + +func cliProjectionRoot(workspace string) string { + return filepath.Join(workspace, filepath.FromSlash(manifest.PortableProjectionRoot)) +} + +func TestInstallPortableLaunchersTextAndJSONSuccess(t *testing.T) { + textWorkspace := t.TempDir() + code, out, errOut := runInstallCLI(t, "install", "portable-launchers", "--workspace", textWorkspace) + if code != exitcode.Success { + t.Fatalf("text exit = %d, stderr: %s", code, errOut) + } + if !bytes.Contains([]byte(out), []byte("portable-launchers: current (changed: true)")) { + t.Fatalf("text output = %q", out) + } + + jsonWorkspace := t.TempDir() + code, out, errOut = runInstallCLI(t, "install", "portable-launchers", "--workspace", jsonWorkspace, "--output", "json") + if code != exitcode.Success { + t.Fatalf("json exit = %d, stderr: %s", code, errOut) + } + assertValidExternalV1(t, []byte(out)) + var envelope struct { + Operation string `json:"operation"` + Result manifest.InstallResult `json:"result"` + } + if err := json.Unmarshal([]byte(out), &envelope); err != nil { + t.Fatalf("invalid JSON: %v: %s", err, out) + } + if envelope.Operation != "projection.install" || envelope.Result.State != manifest.StateCurrent || !envelope.Result.Changed || envelope.Result.InstallationID == "" { + t.Fatalf("JSON result = %#v", envelope) + } +} + +func TestInstallPortableLaunchersConflictClassesDoNotWrite(t *testing.T) { + tests := []struct { + name string + code string + seed func(t *testing.T, workspace string) + }{ + { + name: "modified", code: "projection.modified", + seed: func(t *testing.T, workspace string) { + if code, _, stderr := runInstallCLI(t, "install", "portable-launchers", "--workspace", workspace); code != 0 { + t.Fatalf("seed failed: %s", stderr) + } + _ = os.WriteFile(filepath.Join(cliProjectionRoot(workspace), manifest.PortableSkillFile), []byte("edited\n"), 0o644) + }, + }, + { + name: "incompatible", code: "projection.incompatible", + seed: func(_ *testing.T, workspace string) { + root := cliProjectionRoot(workspace) + _ = os.MkdirAll(root, 0o755) + _ = os.WriteFile(filepath.Join(root, manifest.ProjectionStampFile), []byte("{bad\n"), 0o644) + }, + }, + { + name: "unowned", code: "projection.user_file_conflict", + seed: func(_ *testing.T, workspace string) { + root := cliProjectionRoot(workspace) + _ = os.MkdirAll(root, 0o755) + _ = os.WriteFile(filepath.Join(root, manifest.PortableSkillFile), []byte("mine\n"), 0o644) + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + workspace := t.TempDir() + tt.seed(t, workspace) + before := snapshotTree(t, workspace) + code, out, errOut := runInstallCLI(t, "install", "portable-launchers", "--workspace", workspace, "--repair", "--output", "json") + if code != exitcode.UserFail || out != "" { + t.Fatalf("exit=%d stdout=%q stderr=%q", code, out, errOut) + } + assertValidExternalV1(t, []byte(errOut)) + var envelope struct { + Schema string `json:"schema"` + Operation string `json:"operation"` + Error struct { + Class string `json:"class"` + Code string `json:"code"` + Effect string `json:"effect"` + Retry struct { + Safe bool `json:"safe"` + Action string `json:"action"` + } `json:"retry"` + } `json:"error"` + } + if err := json.Unmarshal([]byte(errOut), &envelope); err != nil || envelope.Schema != "duo.external/v1" || + envelope.Operation != portableInstallOperation || envelope.Error.Code != tt.code || + envelope.Error.Class == "" || envelope.Error.Effect != "no_effect" || + envelope.Error.Retry.Safe || envelope.Error.Retry.Action == "" { + t.Fatalf("error envelope = %q (%v), want %s", errOut, err, tt.code) + } + after := snapshotTree(t, workspace) + if !bytes.Equal(before, after) { + t.Fatalf("conflict changed workspace\nbefore: %s\nafter: %s", before, after) + } + }) + } +} + +func snapshotTree(t *testing.T, root string) []byte { + t.Helper() + var out bytes.Buffer + err := filepath.WalkDir(root, func(path string, d os.DirEntry, err error) error { + if err != nil { + return err + } + rel, _ := filepath.Rel(root, path) + out.WriteString(rel) + out.WriteByte('\n') + if !d.IsDir() { + data, readErr := os.ReadFile(path) + if readErr != nil { + return readErr + } + out.Write(data) + out.WriteByte('\n') + } + return nil + }) + if err != nil { + t.Fatal(err) + } + return out.Bytes() +} diff --git a/internal/cli/root.go b/internal/cli/root.go index cd95d54..1975746 100644 --- a/internal/cli/root.go +++ b/internal/cli/root.go @@ -55,7 +55,8 @@ func NewRootCommand(streams *iostreams.Streams, build buildinfo.Info) *cobra.Com root.AddCommand(versionCommand(streams, build)) root.AddCommand(manifestCommand(streams, build, root)) - root.AddCommand(doctorCommand(streams)) + root.AddCommand(installCommand(streams, build, root)) + root.AddCommand(doctorCommand(streams, build)) root.AddCommand(sessionCommand(streams)) root.AddCommand(conversationCommand(streams)) root.AddCommand(promptCommand(streams)) diff --git a/internal/conformance/portablelauncher/assembly.go b/internal/conformance/portablelauncher/assembly.go new file mode 100644 index 0000000..18d6c87 --- /dev/null +++ b/internal/conformance/portablelauncher/assembly.go @@ -0,0 +1,760 @@ +//go:build linux + +package portablelauncher + +import ( + "bytes" + "encoding/json" + "errors" + "fmt" + "io" + "strings" + "time" + + "github.com/procrastivity/duo/internal/registry" +) + +// StageTimingFact is a common monotonic stage boundary. Sequence is checked +// against the canonical scenario; it does not select scenario semantics. +type StageTimingFact struct { + Sequence int + StartedOffsetMS int64 + DurationMS int64 +} + +// SetupFact contains only facts observed while preparing the isolated fixture. +type SetupFact struct { + Isolated bool + CredentialPresent bool + CredentialProvider string + CredentialSourceKind string + PiNoExtensions bool +} + +// LauncherEventFact is a normalized launcher event. Normalization remains a +// launcher integration responsibility; this common boundary accepts no +// verdict, expected value, terminal bytes, or free-form observation. +type LauncherEventFact struct { + Kind string + InputSource string + TaskDigest string + Arguments []string + Skill SkillPin +} + +// PreflightFact is the already-produced Step 05 diagnostic summary. The +// conformance suite records it but does not duplicate doctor checks. +type PreflightFact struct { + Status string + RequiredChecks int + AllRequiredPassed bool +} + +// BindFact links one inspected Duo envelope to independently projected +// authority relationships for the named canonical case. +type BindFact struct { + Case string + AuthorityInstances []string + ActiveCorrelations []string +} + +// RestartFact records authority-process continuity and the conversation +// baseline used after reopening the same store. +type RestartFact struct { + Outcome string + DistinctProcesses bool + SameStore bool + Attempts int + BaselineRecordID string +} + +// IdempotencyFact records independently counted effects around the two +// idempotency actions. +type IdempotencyFact struct { + SameCommand bool + Attempts int + NewTurns int + OriginalCommandID string +} + +// CommandDeltaFact is an authority/controller before/after count for a +// canonical idempotency case. +type CommandDeltaFact struct { + Case string + CommandsDelta int + AttemptsDelta int + QueueDelta int + TurnsDelta int +} + +// CommandIntegrityFact records whether a case's command projection remained +// unchanged across the associated non-happy observation. +type CommandIntegrityFact struct { + Case string + Unchanged bool +} + +// ExitedFact is the exact lifecycle observation made after controller close. +type ExitedFact struct { + Condition string + SameInstance bool + ProcessSucceeded bool + Attempts int +} + +// CleanupFact records independently inspected resources and export state. +type CleanupFact struct { + PIDsRemaining int + SocketsRemaining int + PanesRemaining int + RootRemoved bool + ExportComplete bool +} + +// AssemblyInput contains validated raw facts available at the common suite +// boundary. AssembleCommonStages is the only production constructor of +// Observation values. +type AssemblyInput struct { + Run RunIdentity + Pins Pins + Record RunRecord + Timings []StageTimingFact + Commands []RecordedCommand + LauncherEvents []LauncherEventFact + Setup SetupFact + Preflight PreflightFact + Binds []BindFact + Restart RestartFact + Idempotency IdempotencyFact + CommandDeltas []CommandDeltaFact + CommandIntegrity []CommandIntegrityFact + Exited ExitedFact + Timeout TimeoutEvidence + TimeoutProbeSupported bool + Cleanup CleanupFact +} + +// AssembleCommonStages derives canonical stage captures from typed raw facts. +// It returns complete captures even on rejection so tests and callers can +// inspect first-stage attribution; a non-nil error means the captures must not +// be published as a successful materialization. +func AssembleCommonStages(input AssemblyInput) ([]CommonStageCapture, error) { + scenario := CanonicalScenario() + stages := make([]CommonStageCapture, len(scenario.Steps)) + var firstErr error + firstFailureIndex := -1 + firstFailureRejected := false + setFailure := func(index int, code, message string, reject bool) { + if reject && firstErr == nil { + firstErr = fmt.Errorf("%s: %s", code, message) + } + if firstFailureIndex >= 0 && index > firstFailureIndex { + return + } + if firstFailureIndex == index && firstFailureRejected { + return + } + if firstFailureIndex >= 0 && index < firstFailureIndex { + stages[firstFailureIndex].Failure = nil + } + firstFailureIndex = index + firstFailureRejected = reject + stages[index].Failure = &StageError{ + Code: code, Message: message, Effect: "unknown_effect", + Retry: Retry{Safe: false, Action: "inspect_originating_failure"}, + } + } + reject := func(index int, code, message string) { setFailure(index, code, message, true) } + failRun := func(index int, code, message string) { setFailure(index, code, message, false) } + + var priorEnd int64 + for i, step := range scenario.Steps { + stages[i].Sequence = step.Sequence + if i >= len(input.Timings) { + stages[i].StartedOffsetMS = priorEnd + reject(i, "assembly.timing_missing", "canonical stage timing is missing") + continue + } + timing := input.Timings[i] + if timing.Sequence != step.Sequence { + stages[i].StartedOffsetMS = priorEnd + reject(i, "assembly.timing_reordered", fmt.Sprintf("timing sequence %d does not match canonical sequence %d", timing.Sequence, step.Sequence)) + continue + } + stages[i].StartedOffsetMS = timing.StartedOffsetMS + stages[i].DurationMS = timing.DurationMS + if timing.StartedOffsetMS < priorEnd || timing.DurationMS < 0 { + stages[i].StartedOffsetMS, stages[i].DurationMS = priorEnd, 0 + reject(i, "assembly.timing_invalid", "stage timing is negative or non-monotonic") + } + priorEnd = stages[i].StartedOffsetMS + stages[i].DurationMS + } + if len(input.Timings) > len(stages) { + reject(len(stages)-1, "assembly.timing_extra", "timing exists outside the canonical stage sequence") + } + + for i, step := range scenario.Steps { + deadline := step.DeadlineMS + if step.Case == "timeout" && step.Stage == "observe" { + deadline = CanonicalOracle().TimeoutMaximumMS + } + stages[i].Observations = append(stages[i].Observations, Observation{ + Sequence: step.Sequence, AssertionID: "stage.completed_within_deadline", Source: "controller", + Evidence: map[string]any{ + "kind": "controller.checkpoint", "monotonic_ms": float64(stages[i].StartedOffsetMS), + "started_ms": float64(stages[i].StartedOffsetMS), "finished_ms": float64(stages[i].StartedOffsetMS + stages[i].DurationMS), + "deadline_ms": float64(deadline), + }, + }) + if stages[i].DurationMS > deadline { + failRun(i, "stage.deadline_exceeded", fmt.Sprintf("%s/%s exceeded its %dms deadline", step.Case, step.Stage, deadline)) + } + } + + commandsByStage, commandErr := assembleCommandSequence(scenario, input.Timings, input.Commands) + if commandErr != nil { + if hasRecordedRunFailure(input.Record) { + code := "run.incomplete" + message := "the recorded run ended before the next canonical command completed" + if input.Record.CompleteRunTimedOut { + code = "run.deadline_exceeded" + message = recordedRunFailureMessage(input.Record) + } + failRun(commandErr.index, code, message) + } else { + reject(commandErr.index, commandErr.code, commandErr.message) + } + } else if hasRecordedRunFailure(input.Record) { + failRun(lastReachedStage(commandsByStage), recordedRunFailureCode(input.Record), recordedRunFailureMessage(input.Record)) + } + + if index, err := validateLauncherEvents(input.LauncherEvents); err != nil { + reject(index, "assembly.launcher_event_rejected", err.Error()) + } + if err := rejectRawLauncherInputEvidence(input.Record.Capture.Events); err != nil { + reject(collectorStageIndexForAssembly("run", "discovery"), "assembly.launcher_event_rejected", err.Error()) + } + + // Derive semantic observations only until the first bad source. Cleanup is + // always derived independently below, even when an earlier stage failed. + for i, step := range scenario.Steps { + if step.Stage == "cleanup" || (step.Case == "blocked" && blockedPrerequisiteUnavailable()) { + continue + } + if firstErr != nil || (firstFailureIndex >= 0 && i >= firstFailureIndex) { + continue + } + observation, err := assembleSemanticObservation(input, step, commandsByStage[i]) + if err != nil { + reject(i, "assembly.source_invalid", err.Error()) + continue + } + stages[i].Observations = append(stages[i].Observations, observation) + } + + cleanupIndex := len(stages) - 1 + cleanupStep := scenario.Steps[cleanupIndex] + stages[cleanupIndex].Observations = append(stages[cleanupIndex].Observations, controllerObservation(cleanupStep, map[string]any{ + "pids_remaining": float64(input.Cleanup.PIDsRemaining), "sockets_remaining": float64(input.Cleanup.SocketsRemaining), + "panes_remaining": float64(input.Cleanup.PanesRemaining), "root_removed": input.Cleanup.RootRemoved, + "export_complete": input.Cleanup.ExportComplete, + }, nil)) + return stages, firstErr +} + +type commandAssemblyError struct { + index int + code string + message string +} + +func assembleCommandSequence(scenario Scenario, timings []StageTimingFact, commands []RecordedCommand) (map[int][]RecordedCommand, *commandAssemblyError) { + byStage := make(map[int][]RecordedCommand) + if len(timings) < len(scenario.Steps) { + return byStage, &commandAssemblyError{len(timings), "assembly.timing_missing", "command attribution requires every canonical stage timing"} + } + var priorEndNS int64 + for position, command := range commands { + if command.StartOffsetNS < priorEndNS || command.EndOffsetNS < command.StartOffsetNS { + return byStage, &commandAssemblyError{stageIndexForCommandOffset(timings, command.StartOffsetNS), "assembly.command_order", fmt.Sprintf("command %d overlaps or regresses", position+1)} + } + priorEndNS = command.EndOffsetNS + stageIndex := stageIndexForCommandInterval(timings, command.StartOffsetNS, command.EndOffsetNS) + if stageIndex < 0 || stageIndex >= len(scenario.Steps) { + return byStage, &commandAssemblyError{stageIndexForCommandOffset(timings, command.StartOffsetNS), "assembly.command_extra", fmt.Sprintf("command %d is outside every canonical stage boundary", position+1)} + } + step := scenario.Steps[stageIndex] + if step.Action.Executor != "duo" || (step.Case == "blocked" && blockedPrerequisiteUnavailable()) { + return byStage, &commandAssemblyError{stageIndex, "assembly.command_extra", fmt.Sprintf("command %d occurred during non-Duo stage %s/%s", position+1, step.Case, step.Stage)} + } + byStage[stageIndex] = append(byStage[stageIndex], command) + } + + for i, step := range scenario.Steps { + if step.Action.Executor != "duo" || (step.Case == "blocked" && blockedPrerequisiteUnavailable()) { + continue + } + stageCommands := byStage[i] + primary := 0 + auxiliary := make(map[string]int, len(step.Action.AuxiliaryOperations)) + for position, command := range stageCommands { + operation, ok := recordedCommandOperation(command.Arguments) + if !ok { + return byStage, &commandAssemblyError{i, "assembly.command_unknown", fmt.Sprintf("command %d in %s/%s has no registered operation", position+1, step.Case, step.Stage)} + } + switch { + case operation == step.Action.Operation: + if err := validateCanonicalArguments(step.Action, command.Arguments); err != nil { + return byStage, &commandAssemblyError{i, "assembly.command_arguments", err.Error()} + } + primary++ + case containsString(step.Action.AuxiliaryOperations, operation): + if err := validateAuxiliaryArguments(step.Action, operation, command.Arguments); err != nil { + return byStage, &commandAssemblyError{i, "assembly.command_arguments", err.Error()} + } + auxiliary[operation]++ + default: + return byStage, &commandAssemblyError{i, "assembly.command_reordered", fmt.Sprintf("%s is not allowed during %s/%s", operation, step.Case, step.Stage)} + } + if err := validateRecordedCommandStatus(step, operation, command); err != nil { + return byStage, &commandAssemblyError{i, "assembly.command_status", err.Error()} + } + } + if primary < step.Action.MinimumInvocations { + return byStage, &commandAssemblyError{i, "assembly.command_missing_or_reordered", fmt.Sprintf("%s/%s requires at least %d %s invocation(s)", step.Case, step.Stage, step.Action.MinimumInvocations, step.Action.Operation)} + } + if step.Action.MaximumInvocations > 0 && primary > step.Action.MaximumInvocations { + return byStage, &commandAssemblyError{i, "assembly.command_extra", fmt.Sprintf("%s has more than %d canonical invocations", step.Action.Operation, step.Action.MaximumInvocations)} + } + for _, operation := range step.Action.AuxiliaryOperations { + if auxiliary[operation] < step.Action.MinimumInvocations { + return byStage, &commandAssemblyError{i, "assembly.command_missing_or_reordered", fmt.Sprintf("%s/%s requires %s alongside %s", step.Case, step.Stage, operation, step.Action.Operation)} + } + } + if len(step.Action.AuxiliaryOperations) == 1 { + if err := validatePollingPairs(step, stageCommands, step.Action.AuxiliaryOperations[0]); err != nil { + return byStage, &commandAssemblyError{i, "assembly.command_reordered", err.Error()} + } + } + } + return byStage, nil +} + +func stageIndexForCommandInterval(timings []StageTimingFact, startNS, endNS int64) int { + matched := -1 + for i, timing := range timings { + stageStart := timing.StartedOffsetMS * int64(time.Millisecond) + stageEnd := (timing.StartedOffsetMS + timing.DurationMS) * int64(time.Millisecond) + if startNS < stageStart || endNS > stageEnd { + continue + } + if matched >= 0 { + return -1 + } + matched = i + } + return matched +} + +func stageIndexForCommandOffset(timings []StageTimingFact, offsetNS int64) int { + for i, timing := range timings { + if offsetNS < (timing.StartedOffsetMS+timing.DurationMS)*int64(time.Millisecond) { + return i + } + } + if len(timings) == 0 { + return 0 + } + return len(timings) - 1 +} + +func validatePollingPairs(step StepSpec, commands []RecordedCommand, auxiliary string) error { + if len(commands)%2 != 0 { + return fmt.Errorf("%s/%s polling must contain complete auxiliary/primary pairs", step.Case, step.Stage) + } + for i, command := range commands { + operation, _ := recordedCommandOperation(command.Arguments) + want := auxiliary + if i%2 == 1 { + want = step.Action.Operation + } + if operation != want { + return fmt.Errorf("%s/%s polling command %d is %s, want %s", step.Case, step.Stage, i+1, operation, want) + } + } + return nil +} + +func validateRecordedCommandStatus(step StepSpec, operation string, command RecordedCommand) error { + expectedFailure := step.Case == "same_key_different_text" && step.Stage == "send" && operation == step.Action.Operation + if command.Signal != 0 || command.SignalName != "" { + return fmt.Errorf("%s/%s command was terminated by a signal", step.Case, step.Stage) + } + if expectedFailure && command.ExitCode == 0 { + return fmt.Errorf("%s/%s expected a nonzero schema-valid conflict command", step.Case, step.Stage) + } + if !expectedFailure && command.ExitCode != 0 { + return fmt.Errorf("%s/%s command exited nonzero", step.Case, step.Stage) + } + return nil +} + +func hasRecordedRunFailure(record RunRecord) bool { + return record.RunnerError != nil || record.LauncherError != nil || record.CheckpointSourceError != nil || record.CompleteRunTimedOut +} + +func recordedRunFailureMessage(record RunRecord) string { + switch { + case record.CompleteRunTimedOut: + return "the complete outer run deadline expired at the last common checkpoint" + case record.LauncherError != nil: + return "the outer launcher failed after the last completed canonical command" + case record.CheckpointSourceError != nil: + return "the trusted checkpoint source failed after the last completed canonical command" + default: + return "the common runner failed after the last completed canonical command" + } +} + +func recordedRunFailureCode(record RunRecord) string { + if record.CompleteRunTimedOut { + return "run.deadline_exceeded" + } + return "run.failed" +} + +func lastReachedStage(commandsByStage map[int][]RecordedCommand) int { + last := collectorStageIndexForAssembly("run", "discovery") + for index, commands := range commandsByStage { + if len(commands) != 0 && index > last { + last = index + } + } + return last +} + +func validateAuxiliaryArguments(primary ActionSpec, operation string, arguments []string) error { + if operation != registeredOperationName("session", "show") || len(primary.Arguments) < 3 { + return fmt.Errorf("%s is not a canonical auxiliary operation for %s", operation, primary.Operation) + } + action := ActionSpec{ + Operation: operation, + Arguments: []string{"session", "show", primary.Arguments[2], "--output", "json"}, + } + return validateCanonicalArguments(action, arguments) +} + +func recordedCommandOperation(arguments []string) (string, bool) { + bestLength := 0 + operation := "" + for _, descriptor := range registry.All() { + if len(descriptor.CLI) > len(arguments) || len(descriptor.CLI) <= bestLength { + continue + } + match := true + for i := range descriptor.CLI { + match = match && arguments[i] == descriptor.CLI[i] + } + if match { + bestLength, operation = len(descriptor.CLI), descriptor.Name + } + } + return operation, operation != "" +} + +func validateCanonicalArguments(action ActionSpec, arguments []string) error { + if len(arguments) != len(action.Arguments) { + return fmt.Errorf("%s argv length %d does not match canonical length %d", action.Operation, len(arguments), len(action.Arguments)) + } + for i, want := range action.Arguments { + got := arguments[i] + switch want { + case "$CANONICAL_PROMPT": + want = string(PromptBytes) + case "$CONFLICT_TEXT": + want = string(ConflictText) + case "$PRIMARY_KEY": + want = CanonicalScenario().Fixture.IdempotencyKey + case "$EXPIRES_AT": + if parsed, err := time.Parse(time.RFC3339, got); err != nil || parsed.IsZero() { + return fmt.Errorf("%s argv[%d] is not a finite RFC3339 expiry", action.Operation, i) + } + continue + case "$WORKSPACE": + if got != "$WORKSPACE" && !strings.HasSuffix(got, "/workspace") { + return fmt.Errorf("%s argv[%d] is not the fixture workspace", action.Operation, i) + } + continue + case "herdr:$RUN/herdr/herdr.sock": + if got != want && !strings.HasSuffix(got, "/herdr/herdr.sock") { + return fmt.Errorf("%s argv[%d] is not the fixture Herdr socket", action.Operation, i) + } + continue + default: + if strings.HasPrefix(want, "$") { + if got == "" { + return fmt.Errorf("%s argv[%d] has an empty canonical binding", action.Operation, i) + } + continue + } + } + if got != want { + return fmt.Errorf("%s argv[%d] = %q, want canonical %q", action.Operation, i, got, want) + } + } + return nil +} + +func validateLauncherEvents(events []LauncherEventFact) (int, error) { + discoveryIndex := collectorStageIndexForAssembly("run", "discovery") + discoveryCount := 0 + for _, event := range events { + input := strings.ToLower(event.InputSource) + kind := strings.ToLower(event.Kind) + if input == "terminal" || input == "terminal_paste" || input == "paste" || input == "tool_input" || strings.Contains(kind, "terminal") || strings.Contains(kind, "paste") || kind == "tool.input" { + return discoveryIndex, fmt.Errorf("launcher terminal/paste/tool-input event is forbidden") + } + if event.Kind == "skill.discovery" { + discoveryCount++ + } + } + if discoveryCount != 1 { + return discoveryIndex, fmt.Errorf("launcher emitted %d skill discovery events, want exactly one", discoveryCount) + } + return discoveryIndex, nil +} + +func rejectRawLauncherInputEvidence(events []byte) error { + lower := strings.ToLower(string(events)) + for _, marker := range []string{ + "terminal_snapshot", "terminal-paste", "terminal_paste", "send_keys", "pane.send_text", "pane.send_keys", + `"kind":"tool.input"`, `"kind": "tool.input"`, `"input_source":"terminal"`, `"input_source": "terminal"`, + `"input_source":"paste"`, `"input_source": "paste"`, `"input_source":"tool_input"`, `"input_source": "tool_input"`, + } { + if strings.Contains(lower, marker) { + return fmt.Errorf("raw launcher stream contains forbidden terminal/paste/tool-input evidence") + } + } + return nil +} + +func assembleSemanticObservation(input AssemblyInput, step StepSpec, commands []RecordedCommand) (Observation, error) { + id := step.Assertions[1].ID + switch id { + case "setup.fixture_isolated_and_pinned": + pinsJSON, err := json.Marshal(input.Pins) + if err != nil { + return Observation{}, err + } + measurements := map[string]any{ + "fixture_root": input.Run.FixtureRoot, "shared_state": !input.Setup.Isolated, + "plugins_enabled": input.Run.PluginsEnabled, "mcp_enabled": input.Run.MCPEnabled, + "terminal_input_used": input.Run.TerminalInputUsed, + } + extra := map[string]any{ + "pins_digest": Digest(pinsJSON), "credentials_present": input.Setup.CredentialPresent, + "credential_provider": input.Setup.CredentialProvider, "credential_source_kind": input.Setup.CredentialSourceKind, + "pi_no_extensions": input.Setup.PiNoExtensions, + } + return controllerObservation(step, measurements, extra), nil + case "discovery.skill_identity": + for _, event := range input.LauncherEvents { + if event.Kind != "skill.discovery" { + continue + } + return Observation{Sequence: step.Sequence, AssertionID: id, Source: "launcher_event", Evidence: map[string]any{ + "kind": event.Kind, "task_digest": event.TaskDigest, "arguments": stringsToAny(event.Arguments), + "skill": map[string]any{"name": event.Skill.Name, "format_version": event.Skill.FormatVersion, "content_digest": event.Skill.ContentDigest}, + }}, nil + } + return Observation{}, fmt.Errorf("skill discovery event is missing") + case "preflight.ready": + return controllerObservation(step, map[string]any{ + "status": input.Preflight.Status, "required_checks": float64(input.Preflight.RequiredChecks), "all_required_pass": input.Preflight.AllRequiredPassed, + }, nil), nil + case "restart.authority_continuity": + return controllerObservation(step, map[string]any{ + "outcome": input.Restart.Outcome, "distinct_processes": input.Restart.DistinctProcesses, + "same_store": input.Restart.SameStore, "attempts": float64(input.Restart.Attempts), + }, nil), nil + case "send.idempotent_replay": + return controllerObservation(step, map[string]any{ + "same_command": input.Idempotency.SameCommand, "attempts": float64(input.Idempotency.Attempts), "new_turns": float64(input.Idempotency.NewTurns), + }, nil), nil + case "command.idempotent_no_duplication", "command.idempotency_no_drift": + delta, ok := commandDelta(input.CommandDeltas, step.Case) + if !ok { + return Observation{}, fmt.Errorf("authority command delta for %s is missing", step.Case) + } + measurements := map[string]any{"commands_delta": float64(delta.CommandsDelta), "attempts_delta": float64(delta.AttemptsDelta), "turns_delta": float64(delta.TurnsDelta)} + if id == "command.idempotency_no_drift" { + measurements["queue_delta"] = float64(delta.QueueDelta) + } + return controllerObservation(step, measurements, nil), nil + case "observe.exited": + targetVerified := verifiedControls(input.Record, RunnerCaseExited, ControlSuspendExactProcess, ControlCloseExactPane) + if !targetVerified { + return Observation{}, fmt.Errorf("exited assertion lacks the ordered verified controller checkpoints") + } + measurements := map[string]any{ + "condition": input.Exited.Condition, "same_instance": input.Exited.SameInstance, + "process_success": input.Exited.ProcessSucceeded, "attempts": float64(input.Exited.Attempts), + } + return controllerObservation(step, measurements, map[string]any{"target_verified": targetVerified}), nil + case "observe.timeout": + targetVerified := verifiedControls(input.Record, RunnerCaseTimeout, ControlSuspendExactProcess) + if !targetVerified { + return Observation{}, fmt.Errorf("timeout assertion lacks the verified suspension checkpoint") + } + measurements := map[string]any{ + "elapsed_min_ms": float64(CanonicalOracle().TimeoutMinimumMS), "elapsed_max_ms": float64(CanonicalOracle().TimeoutMaximumMS), + "elapsed_ms": float64(input.Timeout.ElapsedMS), "process_success": input.Timeout.ProcessSucceeded, + "outer_timed_out": input.Timeout.OuterTimedOut, "stopped_verified": targetVerified, + "delivered": input.Timeout.Delivered, "attempts": float64(input.Timeout.Attempts), "assistant_blocks": float64(input.Timeout.AssistantBlocks), + } + return controllerObservation(step, measurements, map[string]any{"target_verified": targetVerified, "timeout_probe_supported": input.TimeoutProbeSupported}), nil + } + + command, err := finalPrimaryCommand(step.Action, commands) + if err != nil { + return Observation{}, err + } + envelope, err := decodeCommandEnvelope(command, step.Action.Operation) + if err != nil { + return Observation{}, err + } + wrapper := map[string]any{"envelope": envelope} + switch id { + case "launch.resolution": + wrapper["argv_has_prompt"] = containsString(command.Arguments, "--prompt") + case "bind.runtime_identity": + bind, ok := bindFact(input.Binds, step.Case) + if !ok { + return Observation{}, fmt.Errorf("authority bind fact for %s is missing", step.Case) + } + wrapper["authority_instance_ids"] = stringsToAny(bind.AuthorityInstances) + wrapper["active_correlation_runtime_ids"] = stringsToAny(bind.ActiveCorrelations) + case "observe.restart_text": + wrapper["baseline_record_id"] = input.Restart.BaselineRecordID + case "send.idempotency_conflict": + wrapper["original_command_id"] = input.Idempotency.OriginalCommandID + case "command.delivery_integrity": + fact, ok := integrityFact(input.CommandIntegrity, step.Case) + if !ok { + return Observation{}, fmt.Errorf("command integrity fact for %s is missing", step.Case) + } + wrapper["unchanged"] = fact.Unchanged + } + return Observation{Sequence: step.Sequence, AssertionID: id, Source: "duo_envelope", Evidence: wrapper}, nil +} + +func verifiedControls(record RunRecord, caseName RunnerCase, actions ...ControlAction) bool { + position := 0 + for _, control := range record.Controls { + if control.Case != caseName { + continue + } + if position >= len(actions) || control.Action != actions[position] || control.Error != nil || control.Requested != control.Result.Target || !control.Result.StoppedVerified { + return false + } + position++ + } + return position == len(actions) +} + +func controllerObservation(step StepSpec, measurements, extra map[string]any) Observation { + evidence := map[string]any{"kind": "controller.checkpoint", "monotonic_ms": float64(0), "measurements": measurements} + for key, value := range extra { + evidence[key] = value + } + return Observation{Sequence: step.Sequence, AssertionID: step.Assertions[1].ID, Source: "controller", Evidence: evidence} +} + +func finalPrimaryCommand(action ActionSpec, commands []RecordedCommand) (RecordedCommand, error) { + for i := len(commands) - 1; i >= 0; i-- { + operation, _ := recordedCommandOperation(commands[i].Arguments) + if operation == action.Operation { + return commands[i], nil + } + } + return RecordedCommand{}, fmt.Errorf("no %s command was selected for canonical evidence", action.Operation) +} + +func decodeCommandEnvelope(command RecordedCommand, operation string) (map[string]any, error) { + decoder := json.NewDecoder(bytes.NewReader(command.Stdout)) + decoder.UseNumber() + var raw any + if err := decoder.Decode(&raw); err != nil { + return nil, fmt.Errorf("%s stdout is not a JSON envelope: %w", operation, err) + } + if err := decoder.Decode(&struct{}{}); err == nil { + return nil, fmt.Errorf("%s stdout contains multiple JSON values", operation) + } else if !errors.Is(err, io.EOF) { + return nil, fmt.Errorf("%s stdout has invalid trailing data: %w", operation, err) + } + canonical, err := json.Marshal(raw) + if err != nil { + return nil, err + } + var envelope map[string]any + if err := json.Unmarshal(canonical, &envelope); err != nil { + return nil, err + } + if envelope["operation"] != operation { + return nil, fmt.Errorf("command envelope operation %q does not match canonical %q", envelope["operation"], operation) + } + if err := ValidateExternalEnvelope(envelope); err != nil { + return nil, fmt.Errorf("command envelope failed duo.external/v1 validation: %w", err) + } + return envelope, nil +} + +func bindFact(facts []BindFact, caseName string) (BindFact, bool) { + var out BindFact + count := 0 + for _, fact := range facts { + if fact.Case == caseName { + out, count = fact, count+1 + } + } + return out, count == 1 +} + +func commandDelta(facts []CommandDeltaFact, caseName string) (CommandDeltaFact, bool) { + var out CommandDeltaFact + count := 0 + for _, fact := range facts { + if fact.Case == caseName { + out, count = fact, count+1 + } + } + return out, count == 1 +} + +func integrityFact(facts []CommandIntegrityFact, caseName string) (CommandIntegrityFact, bool) { + var out CommandIntegrityFact + count := 0 + for _, fact := range facts { + if fact.Case == caseName { + out, count = fact, count+1 + } + } + return out, count == 1 +} + +func stringsToAny(values []string) []any { + out := make([]any, len(values)) + for i := range values { + out[i] = values[i] + } + return out +} + +func collectorStageIndexForAssembly(caseName, stageName string) int { + for i, step := range CanonicalScenario().Steps { + if step.Case == caseName && step.Stage == stageName { + return i + } + } + return 0 +} diff --git a/internal/conformance/portablelauncher/assembly_test.go b/internal/conformance/portablelauncher/assembly_test.go new file mode 100644 index 0000000..bfbae23 --- /dev/null +++ b/internal/conformance/portablelauncher/assembly_test.go @@ -0,0 +1,377 @@ +//go:build linux + +package portablelauncher + +import ( + "encoding/json" + "fmt" + "reflect" + "strings" + "testing" + "time" +) + +func TestTrustedAssemblyMaterializesAuthoritativeStructuralBlockedBundle(t *testing.T) { + input := assemblyTestInput(t) + stages, err := AssembleCommonStages(input) + if err != nil { + t.Fatalf("AssembleCommonStages: %v", err) + } + result, capture := CollectResult(CollectorInput{Run: input.Run, Pins: input.Pins, Record: input.Record, Stages: stages}) + if result.Summary.Verdict != "fail" || result.Summary.FirstFailedCase == nil || *result.Summary.FirstFailedCase != "blocked" { + t.Fatalf("summary = %#v", result.Summary) + } + if err := validateCollectedCapture(result, capture); err != nil { + t.Fatalf("trusted assembled structural bundle did not validate: %v", err) + } + blocked := result.Stages[collectorStageIndex("blocked", "launch")] + if blocked.Error == nil || blocked.Error.Code != "prerequisite.blocked_induction_unavailable" { + t.Fatalf("blocked stage = %#v", blocked) + } +} + +func TestCanonicalScenarioOwnsEveryOrderedActionWithoutResultAuthorship(t *testing.T) { + scenario := CanonicalScenario() + for i, step := range scenario.Steps { + if step.Sequence != i+1 || step.Action.Executor == "" || step.Action.Operation == "" || step.Action.Instruction == "" || step.Action.MinimumInvocations < 1 { + t.Fatalf("step %d has incomplete canonical action: %#v", i, step) + } + if step.Action.Executor == "duo" && len(step.Action.Arguments) == 0 { + t.Fatalf("step %d Duo action has no argv template", i) + } + lower := strings.ToLower(step.Action.Instruction + " " + step.Action.Operation) + if strings.Contains(lower, "author result") || strings.Contains(lower, "write result") || strings.Contains(lower, "verdict") { + t.Fatalf("step %d delegates result authorship: %#v", i, step.Action) + } + } + blocked := scenario.Steps[collectorStageIndex("blocked", "launch")] + if blocked.Action.Executor != "common_controller" || blocked.Action.Operation != "prerequisite.require_admitted_then_blocked" { + t.Fatalf("blocked action = %#v", blocked.Action) + } +} + +func TestTrustedAssemblyRejectsCommandAndStageSequenceDrift(t *testing.T) { + base := assemblyTestInput(t) + tests := []struct { + name string + mutate func(*AssemblyInput) + wantCode string + wantStage string + wantCase string + }{ + {name: "missing command", mutate: func(in *AssemblyInput) { in.Commands = in.Commands[1:] }, wantCode: "assembly.command_missing_or_reordered", wantStage: "preflight", wantCase: "run"}, + {name: "reordered command", mutate: func(in *AssemblyInput) { + in.Commands[0].Arguments, in.Commands[1].Arguments = in.Commands[1].Arguments, in.Commands[0].Arguments + }, wantCode: "assembly.command_reordered", wantStage: "preflight", wantCase: "run"}, + {name: "extra command", mutate: func(in *AssemblyInput) { + command := in.Commands[len(in.Commands)-1] + cleanup := in.Timings[len(in.Timings)-1] + command.StartOffsetNS = cleanup.StartedOffsetMS*int64(time.Millisecond) + int64(100*time.Microsecond) + command.EndOffsetNS = command.StartOffsetNS + int64(100*time.Microsecond) + in.Commands = append(in.Commands, command) + }, wantCode: "assembly.command_extra", wantStage: "cleanup", wantCase: "run"}, + {name: "missing timing", mutate: func(in *AssemblyInput) { in.Timings = in.Timings[:len(in.Timings)-1] }, wantCode: "assembly.timing_missing", wantStage: "cleanup", wantCase: "run"}, + {name: "reordered timing", mutate: func(in *AssemblyInput) { in.Timings[3], in.Timings[4] = in.Timings[4], in.Timings[3] }, wantCode: "assembly.timing_reordered", wantStage: "launch", wantCase: "happy"}, + {name: "extra timing", mutate: func(in *AssemblyInput) { in.Timings = append(in.Timings, StageTimingFact{Sequence: 33}) }, wantCode: "assembly.timing_extra", wantStage: "cleanup", wantCase: "run"}, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + input := cloneAssemblyInput(t, base) + test.mutate(&input) + stages, err := AssembleCommonStages(input) + if err == nil || !strings.Contains(err.Error(), test.wantCode) { + t.Fatalf("error = %v, want %s", err, test.wantCode) + } + index := collectorStageIndex(test.wantCase, test.wantStage) + if stages[index].Failure == nil || stages[index].Failure.Code != test.wantCode { + t.Fatalf("attributed stage %s/%s = %#v", test.wantCase, test.wantStage, stages[index]) + } + }) + } +} + +func TestTrustedAssemblyRejectsCommandStatusAndPollingDrift(t *testing.T) { + tests := []struct { + name string + mutate func(*AssemblyInput) + wantCode string + wantStage string + wantCase string + }{ + {name: "successful stage exits nonzero", mutate: func(in *AssemblyInput) { in.Commands[0].ExitCode = 1 }, wantCode: "assembly.command_status", wantStage: "preflight", wantCase: "run"}, + {name: "conflict stage exits zero", mutate: func(in *AssemblyInput) { + for i := range in.Commands { + if operation, _ := recordedCommandOperation(in.Commands[i].Arguments); operation == registeredOperationName("prompt", "send") && containsString(in.Commands[i].Arguments, string(ConflictText)) { + in.Commands[i].ExitCode = 0 + return + } + } + }, wantCode: "assembly.command_status", wantStage: "send", wantCase: "same_key_different_text"}, + {name: "poll misses auxiliary", mutate: func(in *AssemblyInput) { + observe := collectorStageIndex("happy", "observe") + indexes := commandIndexesForStage(*in, observe) + in.Commands = append(in.Commands[:indexes[0]], in.Commands[indexes[0]+1:]...) + }, wantCode: "assembly.command_missing_or_reordered", wantStage: "observe", wantCase: "happy"}, + {name: "poll pair reordered", mutate: func(in *AssemblyInput) { + observe := collectorStageIndex("happy", "observe") + indexes := commandIndexesForStage(*in, observe) + first, second := indexes[0], indexes[1] + in.Commands[first].Arguments, in.Commands[second].Arguments = in.Commands[second].Arguments, in.Commands[first].Arguments + }, wantCode: "assembly.command_reordered", wantStage: "observe", wantCase: "happy"}, + {name: "command crosses stage boundary", mutate: func(in *AssemblyInput) { + preflight := collectorStageIndex("run", "preflight") + in.Commands[0].EndOffsetNS = (in.Timings[preflight].StartedOffsetMS+in.Timings[preflight].DurationMS)*int64(time.Millisecond) + 1 + }, wantCode: "assembly.command_extra", wantStage: "preflight", wantCase: "run"}, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + input := assemblyTestInput(t) + test.mutate(&input) + stages, err := AssembleCommonStages(input) + if err == nil || !strings.Contains(err.Error(), test.wantCode) { + t.Fatalf("error = %v, want %s", err, test.wantCode) + } + index := collectorStageIndex(test.wantCase, test.wantStage) + if stages[index].Failure == nil || stages[index].Failure.Code != test.wantCode { + t.Fatalf("attributed stage %s/%s = %#v", test.wantCase, test.wantStage, stages[index]) + } + }) + } +} + +func TestTrustedAssemblyRejectsLauncherInputChannels(t *testing.T) { + for _, source := range []string{"terminal", "terminal_paste", "paste", "tool_input"} { + t.Run(source, func(t *testing.T) { + input := assemblyTestInput(t) + input.LauncherEvents = append(input.LauncherEvents, LauncherEventFact{Kind: "tool.input", InputSource: source}) + stages, err := AssembleCommonStages(input) + if err == nil || !strings.Contains(err.Error(), "terminal/paste/tool-input") { + t.Fatalf("error = %v", err) + } + index := collectorStageIndex("run", "discovery") + if stages[index].Failure == nil || stages[index].Failure.Code != "assembly.launcher_event_rejected" { + t.Fatalf("discovery attribution = %#v", stages[index]) + } + }) + } + t.Run("raw launcher stream", func(t *testing.T) { + input := assemblyTestInput(t) + input.Record.Capture.Events = []byte(`{"kind":"tool.input","input_source":"terminal"}`) + stages, err := AssembleCommonStages(input) + if err == nil || !strings.Contains(err.Error(), "terminal/paste/tool-input") { + t.Fatalf("error = %v", err) + } + index := collectorStageIndex("run", "discovery") + if stages[index].Failure == nil || stages[index].Failure.Code != "assembly.launcher_event_rejected" { + t.Fatalf("discovery attribution = %#v", stages[index]) + } + }) +} + +func TestTrustedAssemblyAttributesEveryStageDeadline(t *testing.T) { + for i, step := range CanonicalScenario().Steps { + t.Run(fmt.Sprintf("%02d-%s-%s", step.Sequence, step.Case, step.Stage), func(t *testing.T) { + input := assemblyTestInput(t) + limit := step.DeadlineMS + if step.Case == "timeout" && step.Stage == "observe" { + limit = CanonicalOracle().TimeoutMaximumMS + } + oldEnd := input.Timings[i].StartedOffsetMS + input.Timings[i].DurationMS + delta := limit + 1 - input.Timings[i].DurationMS + input.Timings[i].DurationMS = limit + 1 + for j := i + 1; j < len(input.Timings); j++ { + input.Timings[j].StartedOffsetMS += delta + } + for j := range input.Commands { + if input.Commands[j].StartOffsetNS >= oldEnd*int64(time.Millisecond) { + input.Commands[j].StartOffsetNS += delta * int64(time.Millisecond) + input.Commands[j].EndOffsetNS += delta * int64(time.Millisecond) + } + } + stages, err := AssembleCommonStages(input) + if err != nil { + t.Fatalf("deadline failure should materialize, got rejection: %v", err) + } + if stages[i].Failure == nil || stages[i].Failure.Code != "stage.deadline_exceeded" { + t.Fatalf("stage %d attribution = %#v", i, stages[i]) + } + }) + } +} + +func TestTrustedAssemblyMaterializesCompleteRunDeadline(t *testing.T) { + input := assemblyTestInput(t) + preflight := collectorStageIndex("run", "preflight") + oldDuration := input.Timings[preflight].DurationMS + input.Timings[preflight].DurationMS = CanonicalScenario().DeadlinesMS["complete_run"] + 1 + delta := input.Timings[preflight].DurationMS - oldDuration + for i := preflight + 1; i < len(input.Timings); i++ { + input.Timings[i].StartedOffsetMS += delta + } + input.Commands = nil + input.Record.CompleteRunTimedOut = true + input.Record.RunnerError = fmt.Errorf("complete-run deadline exceeded") + stages, err := AssembleCommonStages(input) + if err != nil { + t.Fatalf("complete-run deadline should materialize, got rejection: %v", err) + } + if stages[preflight].Failure == nil || stages[preflight].Failure.Code != "run.deadline_exceeded" { + t.Fatalf("preflight deadline attribution = %#v", stages[preflight]) + } + result, capture := CollectResult(CollectorInput{Run: input.Run, Pins: input.Pins, Record: input.Record, Stages: stages}) + if err := validateCollectedCapture(result, capture); err != nil { + t.Fatalf("materialized complete-run deadline bundle did not validate: %v", err) + } +} + +func TestDynamicDuoCommitPinValidation(t *testing.T) { + valid := fixturePins().Duo + for _, commit := range []string{ + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + "0123456789abcdef0123456789abcdef01234567", + } { + pin := valid + pin.Commit = commit + if !validDuoPin(pin) { + t.Fatalf("valid dynamic commit %q rejected", commit) + } + } + for _, commit := range []string{"", "too-short", strings.Repeat("A", 40), strings.Repeat("g", 40), strings.Repeat("0", 39), strings.Repeat("0", 40)} { + pin := valid + pin.Commit = commit + if validDuoPin(pin) { + t.Fatalf("invalid exact commit %q accepted", commit) + } + } +} + +func assemblyTestInput(t *testing.T) AssemblyInput { + t.Helper() + pins := fixturePins() + input := AssemblyInput{ + Run: RunIdentity{RunID: "assembly-test", ObservedAt: "2026-09-18T00:00:00Z", HostOS: "linux", HostArch: "x86_64", FixtureRoot: "$RUN"}, + Pins: pins, + Setup: SetupFact{Isolated: true, CredentialPresent: true, CredentialProvider: "openai-codex", CredentialSourceKind: "operator_copy", PiNoExtensions: true}, + LauncherEvents: []LauncherEventFact{{ + Kind: "skill.discovery", InputSource: "task_argument", TaskDigest: Digest(CanonicalTaskBytes), Arguments: []string{"fixture-launcher", TaskArgument}, Skill: pins.Skill, + }}, + Preflight: PreflightFact{Status: "ready", RequiredChecks: 7, AllRequiredPassed: true}, + Restart: RestartFact{Outcome: "same_live", DistinctProcesses: true, SameStore: true, Attempts: 1, BaselineRecordID: "record_fixture"}, + Idempotency: IdempotencyFact{SameCommand: true, Attempts: 1, NewTurns: 0, OriginalCommandID: "cmd_fixture"}, + CommandDeltas: []CommandDeltaFact{ + {Case: "same_key_same_text"}, {Case: "same_key_different_text"}, + }, + Exited: ExitedFact{Condition: "exited", SameInstance: true, ProcessSucceeded: true, Attempts: 1}, + Timeout: TimeoutEvidence{ElapsedMS: 20_000, ProcessSucceeded: true, StoppedVerified: true, Delivered: true, Attempts: 1}, + TimeoutProbeSupported: true, + Cleanup: CleanupFact{RootRemoved: true, ExportComplete: true}, + } + input.Record.Controls = []RecordedControl{ + {Action: ControlSuspendExactProcess, Case: RunnerCaseExited, Requested: exitedIdentity, Result: ControlCheckpoint{Target: exitedIdentity, StoppedVerified: true, MonotonicMS: 1}}, + {Action: ControlCloseExactPane, Case: RunnerCaseExited, Requested: exitedIdentity, Result: ControlCheckpoint{Target: exitedIdentity, StoppedVerified: true, MonotonicMS: 2}}, + {Action: ControlSuspendExactProcess, Case: RunnerCaseTimeout, Requested: timeoutIdentity, Result: ControlCheckpoint{Target: timeoutIdentity, StoppedVerified: true, MonotonicMS: 3}}, + } + for _, caseName := range []string{"happy", "restart", "exited", "timeout"} { + input.Binds = append(input.Binds, BindFact{Case: caseName, AuthorityInstances: []string{"run_fixture"}, ActiveCorrelations: []string{"run_fixture"}}) + input.CommandIntegrity = append(input.CommandIntegrity, CommandIntegrityFact{Case: caseName, Unchanged: true}) + } + + var offset int64 + for _, step := range CanonicalScenario().Steps { + duration := int64(1) + if step.Case == "timeout" && step.Stage == "observe" { + duration = 20_000 + } + input.Timings = append(input.Timings, StageTimingFact{Sequence: step.Sequence, StartedOffsetMS: offset, DurationMS: duration}) + stageStart := offset * int64(time.Millisecond) + offset += duration + if step.Action.Executor != "duo" || (step.Case == "blocked" && blockedPrerequisiteUnavailable()) { + continue + } + arguments := resolveAssemblyArguments(step.Action.Arguments) + stdout := []byte("{}\n") + id := step.Assertions[1].ID + if evidenceSource(id) == "duo_envelope" { + actual := step.Assertions[1].Expected + wrapper := duoEvidenceDocument(t, id, step.Sequence, actual) + envelope := wrapper["envelope"] + var err error + stdout, err = json.Marshal(envelope) + if err != nil { + t.Fatal(err) + } + stdout = append(stdout, '\n') + } + commandOffset := stageStart + int64(100*time.Microsecond) + if len(step.Action.AuxiliaryOperations) != 0 { + auxiliary := []string{"session", "show", arguments[2], "--output", "json"} + input.Commands = append(input.Commands, RecordedCommand{ + Arguments: auxiliary, Stdout: []byte("{}\n"), StartOffsetNS: commandOffset, EndOffsetNS: commandOffset + int64(100*time.Microsecond), + }) + commandOffset += int64(200 * time.Microsecond) + } + exitCode := 0 + if step.Case == "same_key_different_text" && step.Stage == "send" { + exitCode = 1 + } + input.Commands = append(input.Commands, RecordedCommand{ + Arguments: arguments, Stdout: stdout, ExitCode: exitCode, + StartOffsetNS: commandOffset, EndOffsetNS: commandOffset + int64(100*time.Microsecond), + }) + } + return input +} + +func commandIndexesForStage(input AssemblyInput, stage int) []int { + timing := input.Timings[stage] + start := timing.StartedOffsetMS * int64(time.Millisecond) + end := (timing.StartedOffsetMS + timing.DurationMS) * int64(time.Millisecond) + var indexes []int + for i, command := range input.Commands { + if command.StartOffsetNS >= start && command.EndOffsetNS <= end { + indexes = append(indexes, i) + } + } + return indexes +} + +func resolveAssemblyArguments(arguments []string) []string { + out := append([]string(nil), arguments...) + for i, value := range out { + switch value { + case "$WORKSPACE": + out[i] = "$WORKSPACE" + case "herdr:$RUN/herdr/herdr.sock": + out[i] = value + case "$CANONICAL_PROMPT": + out[i] = string(PromptBytes) + case "$CONFLICT_TEXT": + out[i] = string(ConflictText) + case "$PRIMARY_KEY": + out[i] = CanonicalScenario().Fixture.IdempotencyKey + case "$EXPIRES_AT": + out[i] = "2026-09-18T00:10:00Z" + default: + if strings.HasPrefix(value, "$") { + out[i] = "fixture-binding" + } + } + } + return out +} + +func cloneAssemblyInput(t *testing.T, input AssemblyInput) AssemblyInput { + t.Helper() + b, err := json.Marshal(input) + if err != nil { + t.Fatal(err) + } + var out AssemblyInput + if err := json.Unmarshal(b, &out); err != nil { + t.Fatal(err) + } + if !reflect.DeepEqual(input, out) { + t.Fatal("assembly fixture clone changed value") + } + return out +} diff --git a/internal/conformance/portablelauncher/bundle.go b/internal/conformance/portablelauncher/bundle.go new file mode 100644 index 0000000..375a6af --- /dev/null +++ b/internal/conformance/portablelauncher/bundle.go @@ -0,0 +1,143 @@ +package portablelauncher + +import ( + "encoding/json" + "errors" + "fmt" + "os" + "path/filepath" + "strings" +) + +const bundleStagingMarker = ".staging-" + +type bundleFileWriter func(string, []byte) error + +// WriteBundle validates and atomically publishes one scrubbed conformance +// bundle. Raw launcher capture is intentionally not part of the bundle. +func WriteBundle(destination string, result Result, capture *Capture) error { + return writeBundle(destination, result, capture, writePrivateBundleFile) +} + +func writeBundle(destination string, result Result, capture *Capture, writeFile bundleFileWriter) error { + if capture == nil { + return fmt.Errorf("write conformance bundle: capture is nil") + } + if writeFile == nil { + return fmt.Errorf("write conformance bundle: file writer is nil") + } + + resultJSON, err := marshalBundleJSON(result) + if err != nil { + return fmt.Errorf("write conformance bundle: marshal result: %w", err) + } + index := capture.Index() + indexJSON, err := marshalBundleJSON(index) + if err != nil { + return fmt.Errorf("write conformance bundle: marshal evidence index: %w", err) + } + blobs := make(map[string][]byte, len(index.Blobs)) + for _, entry := range index.Blobs { + if blob, ok := capture.Blob(entry.Reference); ok { + blobs[entry.Reference] = blob + } + } + readBlob := func(reference string) ([]byte, error) { + blob, ok := blobs[reference] + if !ok { + return nil, fmt.Errorf("blob %s not found", reference) + } + return append([]byte(nil), blob...), nil + } + if _, err := Validate(ValidationInput{ResultJSON: resultJSON, IndexJSON: indexJSON, ReadBlob: readBlob}); err != nil { + return fmt.Errorf("write conformance bundle: validate: %w", err) + } + + destination = filepath.Clean(destination) + if destination == "." || destination == string(filepath.Separator) { + return fmt.Errorf("write conformance bundle: destination must name a new directory") + } + if _, err := os.Lstat(destination); err == nil { + return fmt.Errorf("write conformance bundle: destination already exists") + } else if !errors.Is(err, os.ErrNotExist) { + return fmt.Errorf("write conformance bundle: inspect destination: %w", err) + } + parent := filepath.Dir(destination) + if info, err := os.Stat(parent); err != nil { + return fmt.Errorf("write conformance bundle: inspect destination parent: %w", err) + } else if !info.IsDir() { + return fmt.Errorf("write conformance bundle: destination parent is not a directory") + } + + stage, err := os.MkdirTemp(parent, "."+filepath.Base(destination)+bundleStagingMarker) + if err != nil { + return fmt.Errorf("write conformance bundle: create staging directory: %w", err) + } + defer func() { + if stage != "" { + _ = os.RemoveAll(stage) + } + }() + if err := os.Chmod(stage, 0o700); err != nil { + return fmt.Errorf("write conformance bundle: protect staging directory: %w", err) + } + blobDir := filepath.Join(stage, "blobs") + if err := os.Mkdir(blobDir, 0o700); err != nil { + return fmt.Errorf("write conformance bundle: create blob directory: %w", err) + } + + files := []struct { + path string + data []byte + }{ + {path: filepath.Join(stage, "result.json"), data: resultJSON}, + {path: filepath.Join(stage, "evidence-index.json"), data: indexJSON}, + } + for _, entry := range index.Blobs { + digest := strings.TrimPrefix(entry.Reference, "blob:sha256:") + files = append(files, struct { + path string + data []byte + }{path: filepath.Join(blobDir, digest+".json"), data: blobs[entry.Reference]}) + } + for _, file := range files { + if err := writeFile(file.path, file.data); err != nil { + return fmt.Errorf("write conformance bundle: write %s: %w", filepath.Base(file.path), err) + } + } + + if err := publishBundle(stage, destination); err != nil { + return fmt.Errorf("write conformance bundle: publish: %w", err) + } + stage = "" + return nil +} + +func marshalBundleJSON(value any) ([]byte, error) { + data, err := json.MarshalIndent(value, "", " ") + if err != nil { + return nil, err + } + return append(data, '\n'), nil +} + +func writePrivateBundleFile(path string, data []byte) error { + file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o600) + if err != nil { + return err + } + remaining := data + for len(remaining) != 0 { + written, writeErr := file.Write(remaining) + if writeErr != nil { + _ = file.Close() + return writeErr + } + if written == 0 { + _ = file.Close() + return errors.New("short write") + } + remaining = remaining[written:] + } + return file.Close() +} diff --git a/internal/conformance/portablelauncher/bundle_publish_linux.go b/internal/conformance/portablelauncher/bundle_publish_linux.go new file mode 100644 index 0000000..af0d149 --- /dev/null +++ b/internal/conformance/portablelauncher/bundle_publish_linux.go @@ -0,0 +1,11 @@ +//go:build linux + +package portablelauncher + +import "golang.org/x/sys/unix" + +func publishBundle(stage, destination string) error { + // RENAME_NOREPLACE is the publication boundary. Unlike os.Rename for + // directories, it cannot replace an empty destination created by a racer. + return unix.Renameat2(unix.AT_FDCWD, stage, unix.AT_FDCWD, destination, unix.RENAME_NOREPLACE) +} diff --git a/internal/conformance/portablelauncher/bundle_publish_unsupported.go b/internal/conformance/portablelauncher/bundle_publish_unsupported.go new file mode 100644 index 0000000..948bd37 --- /dev/null +++ b/internal/conformance/portablelauncher/bundle_publish_unsupported.go @@ -0,0 +1,9 @@ +//go:build !linux + +package portablelauncher + +import "fmt" + +func publishBundle(_, _ string) error { + return fmt.Errorf("portable launcher conformance bundle publication requires Linux renameat2") +} diff --git a/internal/conformance/portablelauncher/bundle_test.go b/internal/conformance/portablelauncher/bundle_test.go new file mode 100644 index 0000000..53462d8 --- /dev/null +++ b/internal/conformance/portablelauncher/bundle_test.go @@ -0,0 +1,348 @@ +package portablelauncher + +import ( + "bytes" + "errors" + "os" + "path/filepath" + "reflect" + "strings" + "testing" +) + +func TestWriteBundleDeterministicLayoutAndRevalidation(t *testing.T) { + result, capture := validBundleInput(t) + parent := t.TempDir() + first := filepath.Join(parent, "first") + second := filepath.Join(parent, "second") + if err := WriteBundle(first, result, capture); err != nil { + t.Fatal(err) + } + if err := WriteBundle(second, result, capture); err != nil { + t.Fatal(err) + } + + index := capture.Index() + wantFiles := map[string][]byte{ + "result.json": mustBundleJSON(t, result), + "evidence-index.json": mustBundleJSON(t, index), + } + for _, entry := range index.Blobs { + blob, ok := capture.Blob(entry.Reference) + if !ok { + t.Fatalf("valid capture lost %s", entry.Reference) + } + digest := strings.TrimPrefix(entry.Reference, "blob:sha256:") + wantFiles[filepath.Join("blobs", digest+".json")] = blob + } + + for _, root := range []string{first, second} { + assertBundleTree(t, root, wantFiles) + } + for relative := range wantFiles { + one, err := os.ReadFile(filepath.Join(first, relative)) + if err != nil { + t.Fatal(err) + } + two, err := os.ReadFile(filepath.Join(second, relative)) + if err != nil { + t.Fatal(err) + } + if !bytes.Equal(one, two) { + t.Errorf("%s differs between identical exports", relative) + } + } + + resultJSON, err := os.ReadFile(filepath.Join(first, "result.json")) + if err != nil { + t.Fatal(err) + } + indexJSON, err := os.ReadFile(filepath.Join(first, "evidence-index.json")) + if err != nil { + t.Fatal(err) + } + readBlob := func(reference string) ([]byte, error) { + digest := strings.TrimPrefix(reference, "blob:sha256:") + return os.ReadFile(filepath.Join(first, "blobs", digest+".json")) + } + if _, err := Validate(ValidationInput{ResultJSON: resultJSON, IndexJSON: indexJSON, ReadBlob: readBlob}); err != nil { + t.Fatalf("published bundle did not revalidate: %v", err) + } +} + +func TestWriteBundleAcceptsAuthoritativeCollectedStructuralFailure(t *testing.T) { + result, capture := CollectResult(collectorTestInput(t)) + destination := filepath.Join(t.TempDir(), "collected") + if err := WriteBundle(destination, result, capture); err != nil { + t.Fatalf("WriteBundle collected result: %v", err) + } + + resultJSON, err := os.ReadFile(filepath.Join(destination, "result.json")) + if err != nil { + t.Fatal(err) + } + var published Result + if err := decodeStrict(resultJSON, &published); err != nil { + t.Fatal(err) + } + if published.Summary.Verdict != "fail" || published.Summary.FirstFailedCase == nil || *published.Summary.FirstFailedCase != "blocked" { + t.Fatalf("published summary = %#v", published.Summary) + } + for _, caseName := range []string{"exited", "timeout"} { + for i, step := range CanonicalScenario().Steps { + if step.Case == caseName && published.Stages[i].Verdict != "pass" { + t.Fatalf("published %s stage %d = %#v", caseName, i, published.Stages[i]) + } + } + } +} + +func TestWriteBundleNeverOverwritesDestination(t *testing.T) { + t.Run("already present", func(t *testing.T) { + result, capture := validBundleInput(t) + parent := t.TempDir() + destination := filepath.Join(parent, "bundle") + if err := os.Mkdir(destination, 0o711); err != nil { + t.Fatal(err) + } + before, err := os.Stat(destination) + if err != nil { + t.Fatal(err) + } + + err = WriteBundle(destination, result, capture) + if err == nil || !strings.Contains(err.Error(), "already exists") { + t.Fatalf("WriteBundle error = %v, want existing-destination refusal", err) + } + after, err := os.Stat(destination) + if err != nil { + t.Fatal(err) + } + if !os.SameFile(before, after) || after.Mode().Perm() != 0o711 { + t.Fatalf("existing destination changed: before=%v after=%v", before.Mode(), after.Mode()) + } + entries, err := os.ReadDir(destination) + if err != nil { + t.Fatal(err) + } + if len(entries) != 0 { + t.Fatalf("existing destination gained files: %v", entries) + } + assertNoBundleStaging(t, parent, filepath.Base(destination)) + }) + + t.Run("created during publication", func(t *testing.T) { + result, capture := validBundleInput(t) + parent := t.TempDir() + destination := filepath.Join(parent, "bundle") + createdDestination := false + err := writeBundle(destination, result, capture, func(path string, data []byte) error { + if err := writePrivateBundleFile(path, data); err != nil { + return err + } + if createdDestination { + return nil + } + if filepath.Base(filepath.Dir(path)) == "blobs" { + if err := os.Mkdir(destination, 0o700); err != nil { + return err + } + createdDestination = true + } + return nil + }) + if err == nil || !strings.Contains(err.Error(), "publish") { + t.Fatalf("writeBundle error = %v, want no-replace publication refusal", err) + } + if !createdDestination { + t.Fatal("test did not create the competing destination") + } + entries, err := os.ReadDir(destination) + if err != nil { + t.Fatal(err) + } + if len(entries) != 0 { + t.Fatalf("competing destination was overwritten: %v", entries) + } + assertNoBundleStaging(t, parent, filepath.Base(destination)) + }) +} + +func TestWriteBundleRejectsInvalidResultAndCaptureBeforePublication(t *testing.T) { + tests := []struct { + name string + want string + edit func(*Result, *Capture) + }{ + { + name: "invalid result", + want: "result scrub failed", + edit: func(result *Result, _ *Capture) { + result.Run.RunID = "Authorization: Bearer abcdefghijklmnop" + }, + }, + { + name: "missing blob", + want: "missing blob", + edit: func(_ *Result, capture *Capture) { + delete(capture.blobs, capture.entries[0].Reference) + }, + }, + { + name: "unreferenced blob", + want: "unreferenced blob", + edit: func(_ *Result, capture *Capture) { + if _, err := capture.AddJSON("common_collector", Evidence{Schema: EvidenceSchema, ScrubStatus: "pass", Observations: []Observation{}}); err != nil { + t.Fatalf("add unreferenced evidence: %v", err) + } + }, + }, + { + name: "mismatched blob", + want: "digest mismatch", + edit: func(_ *Result, capture *Capture) { + ref := capture.entries[0].Reference + capture.blobs[ref] = append(capture.blobs[ref], ' ') + }, + }, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + result, capture := validBundleInput(t) + test.edit(&result, capture) + parent := t.TempDir() + destination := filepath.Join(parent, "bundle") + err := WriteBundle(destination, result, capture) + if err == nil || !strings.Contains(err.Error(), test.want) { + t.Fatalf("WriteBundle error = %v, want %q", err, test.want) + } + if _, err := os.Lstat(destination); !errors.Is(err, os.ErrNotExist) { + t.Fatalf("invalid bundle published a final directory: %v", err) + } + assertNoBundleStaging(t, parent, filepath.Base(destination)) + }) + } +} + +func TestWriteBundleFailureRemovesOnlyItsStagingDirectory(t *testing.T) { + result, capture := validBundleInput(t) + parent := t.TempDir() + destination := filepath.Join(parent, "bundle") + unrelated := filepath.Join(parent, ".bundle"+bundleStagingMarker+"keep") + if err := os.Mkdir(unrelated, 0o700); err != nil { + t.Fatal(err) + } + marker := filepath.Join(unrelated, "marker") + if err := os.WriteFile(marker, []byte("keep\n"), 0o600); err != nil { + t.Fatal(err) + } + + injected := errors.New("injected write failure") + err := writeBundle(destination, result, capture, func(path string, data []byte) error { + if filepath.Base(path) == "evidence-index.json" { + return injected + } + return writePrivateBundleFile(path, data) + }) + if !errors.Is(err, injected) { + t.Fatalf("writeBundle error = %v, want injected failure", err) + } + if _, err := os.Lstat(destination); !errors.Is(err, os.ErrNotExist) { + t.Fatalf("failed write left a final directory: %v", err) + } + if data, err := os.ReadFile(marker); err != nil || string(data) != "keep\n" { + t.Fatalf("failure cleanup touched unrelated staging directory: data=%q err=%v", data, err) + } + entries, err := os.ReadDir(parent) + if err != nil { + t.Fatal(err) + } + if len(entries) != 1 || entries[0].Name() != filepath.Base(unrelated) { + t.Fatalf("write failure left unexpected paths: %v", entries) + } +} + +func validBundleInput(t *testing.T) (Result, *Capture) { + t.Helper() + built := buildContractFixture(t) + capture := &Capture{ + entries: append([]BlobEntry(nil), built.Index.Blobs...), + blobs: map[string][]byte{built.BlobRef: append([]byte(nil), built.Blob...)}, + } + return built.Result, capture +} + +func mustBundleJSON(t *testing.T, value any) []byte { + t.Helper() + data, err := marshalBundleJSON(value) + if err != nil { + t.Fatal(err) + } + return data +} + +func assertBundleTree(t *testing.T, root string, wantFiles map[string][]byte) { + t.Helper() + wantPaths := []string{"blobs", "evidence-index.json", "result.json"} + entries, err := os.ReadDir(root) + if err != nil { + t.Fatal(err) + } + var gotPaths []string + for _, entry := range entries { + gotPaths = append(gotPaths, entry.Name()) + } + if !reflect.DeepEqual(gotPaths, wantPaths) { + t.Fatalf("bundle root entries = %v, want %v", gotPaths, wantPaths) + } + for _, directory := range []string{root, filepath.Join(root, "blobs")} { + info, err := os.Stat(directory) + if err != nil { + t.Fatal(err) + } + if !info.IsDir() || info.Mode().Perm() != 0o700 { + t.Errorf("directory %s mode = %v, want 0700", directory, info.Mode()) + } + } + for relative, want := range wantFiles { + path := filepath.Join(root, relative) + got, err := os.ReadFile(path) + if err != nil { + t.Fatal(err) + } + if !bytes.Equal(got, want) { + t.Errorf("%s content differs from deterministic bytes", relative) + } + if !bytes.HasSuffix(got, []byte("\n")) || bytes.HasSuffix(got, []byte("\n\n")) { + t.Errorf("%s does not have exactly one final newline", relative) + } + info, err := os.Stat(path) + if err != nil { + t.Fatal(err) + } + if !info.Mode().IsRegular() || info.Mode().Perm() != 0o600 { + t.Errorf("file %s mode = %v, want 0600", relative, info.Mode()) + } + } + blobs, err := os.ReadDir(filepath.Join(root, "blobs")) + if err != nil { + t.Fatal(err) + } + if len(blobs) != len(wantFiles)-2 { + t.Errorf("blob count = %d, want %d", len(blobs), len(wantFiles)-2) + } +} + +func assertNoBundleStaging(t *testing.T, parent, destinationBase string) { + t.Helper() + entries, err := os.ReadDir(parent) + if err != nil { + t.Fatal(err) + } + prefix := "." + destinationBase + bundleStagingMarker + for _, entry := range entries { + if strings.HasPrefix(entry.Name(), prefix) { + t.Errorf("staging directory remains after failure: %s", entry.Name()) + } + } +} diff --git a/internal/conformance/portablelauncher/capture.go b/internal/conformance/portablelauncher/capture.go new file mode 100644 index 0000000..ba5af12 --- /dev/null +++ b/internal/conformance/portablelauncher/capture.go @@ -0,0 +1,98 @@ +package portablelauncher + +import ( + "bytes" + "encoding/json" + "fmt" + "regexp" + "sort" + "strings" +) + +var secretPatterns = []*regexp.Regexp{ + regexp.MustCompile(`(?i)authorization["']?\s*[:=]\s*["']?\s*(bearer|basic)\s+[a-z0-9._~+/=-]{8,}`), + regexp.MustCompile(`(?i)bearer\s+[a-z0-9._~+/=-]{12,}`), + regexp.MustCompile(`(?i)(api[_-]?key|access[_-]?token|refresh[_-]?token|client[_-]?secret)["']?\s*[:=]`), + regexp.MustCompile(`\bsk-[A-Za-z0-9_-]{12,}\b`), + regexp.MustCompile(`-----BEGIN [A-Z ]+PRIVATE KEY-----`), +} + +var forbiddenEvidenceTerms = []string{ + "terminal_snapshot", "terminal-paste", "terminal_paste", "send_keys", "pane.send_text", "pane.send_keys", + "raw_transcript", "chain_of_thought", "environment_dump", +} + +// Scrub checks bytes before they can enter a content-addressed capture. Paths +// must already have been rewritten to stable tokens. +func Scrub(data []byte) []string { + var findings []string + for _, pattern := range secretPatterns { + if pattern.Match(data) { + findings = append(findings, "secret-shaped content") + break + } + } + lower := strings.ToLower(string(data)) + for _, term := range forbiddenEvidenceTerms { + if strings.Contains(lower, term) { + findings = append(findings, "forbidden evidence term: "+term) + } + } + for _, marker := range []string{"/home/", "/users/", "/tmp/", "/var/tmp/", "/private/var/", `c:\\users\\`} { + if strings.Contains(lower, marker) { + findings = append(findings, "absolute user/run path was not rewritten") + break + } + } + sort.Strings(findings) + return findings +} + +// Capture stores scrubbed, content-addressed JSON evidence for one suite run. +type Capture struct { + entries []BlobEntry + blobs map[string][]byte +} + +// NewCapture creates an empty evidence capture. +func NewCapture() *Capture { return &Capture{blobs: map[string][]byte{}} } + +// AddJSON scrubs and adds one JSON evidence value, returning its blob reference. +func (c *Capture) AddJSON(producer string, value any) (string, error) { + if !oneOf(producer, "common_collector", "controller", "duo", "launcher") { + return "", fmt.Errorf("unknown evidence producer %q", producer) + } + b, err := json.MarshalIndent(value, "", " ") + if err != nil { + return "", fmt.Errorf("marshal evidence: %w", err) + } + b = append(b, '\n') + if findings := Scrub(b); len(findings) != 0 { + return "", fmt.Errorf("evidence scrub failed: %s", strings.Join(findings, "; ")) + } + ref := "blob:" + Digest(b) + if old, ok := c.blobs[ref]; ok && !bytes.Equal(old, b) { + return "", fmt.Errorf("content-address collision at %s", ref) + } + if _, exists := c.blobs[ref]; !exists { + c.blobs[ref] = append([]byte(nil), b...) + c.entries = append(c.entries, BlobEntry{ + Reference: ref, MediaType: "application/json", Bytes: int64(len(b)), + SHA256: strings.TrimPrefix(ref, "blob:"), ScrubStatus: "pass", Producer: producer, + }) + } + return ref, nil +} + +// Index returns a deterministic evidence index for the captured blobs. +func (c *Capture) Index() EvidenceIndex { + entries := append([]BlobEntry(nil), c.entries...) + sort.Slice(entries, func(i, j int) bool { return entries[i].Reference < entries[j].Reference }) + return EvidenceIndex{Schema: IndexSchema, Policy: ScrubPolicy, Blobs: entries} +} + +// Blob returns a copy of the content-addressed evidence bytes. +func (c *Capture) Blob(ref string) ([]byte, bool) { + b, ok := c.blobs[ref] + return append([]byte(nil), b...), ok +} diff --git a/internal/conformance/portablelauncher/checkpoints.go b/internal/conformance/portablelauncher/checkpoints.go new file mode 100644 index 0000000..9b90b89 --- /dev/null +++ b/internal/conformance/portablelauncher/checkpoints.go @@ -0,0 +1,490 @@ +package portablelauncher + +import ( + "context" + "errors" + "fmt" + "os" + "time" + + "github.com/procrastivity/duo/internal/domain" + "github.com/procrastivity/duo/internal/domain/storerepo" + "github.com/procrastivity/duo/internal/store" +) + +// AuthoritySnapshotLoader returns the complete durable fact log in commit +// order. An injected loader keeps observer tests independent of SQLite and of +// every live host or runtime process. +type AuthoritySnapshotLoader func(context.Context) ([]domain.Fact, error) + +// MonotonicClock returns an offset from the beginning of the run. It must be +// backed by a monotonic clock; authority fact wall timestamps are never used +// for controller ordering. +type MonotonicClock func() time.Duration + +// CheckpointObserverConfig is the complete trusted input to the authority +// observer. Integration values are exact configured instances, not kinds. +type CheckpointObserverConfig struct { + Load AuthoritySnapshotLoader + Monotonic MonotonicClock + PollInterval time.Duration + FixtureWorkspace string + HerdrIntegration string + PiIntegration string +} + +// CheckpointObserver projects durable authority facts into the two raw facts +// RunCommon understands. It owns no controller actions and no verdicts. +type CheckpointObserver struct { + config CheckpointObserverConfig + phase int + targets map[RunnerCase]ProcessIdentity +} + +// NewCheckpointObserver validates and constructs an authority-fact observer. +func NewCheckpointObserver(config CheckpointObserverConfig) (*CheckpointObserver, error) { + switch { + case config.Load == nil: + return nil, fmt.Errorf("checkpoint observer requires a snapshot loader") + case config.Monotonic == nil: + return nil, fmt.Errorf("checkpoint observer requires a monotonic clock") + case config.PollInterval <= 0: + return nil, fmt.Errorf("checkpoint observer requires a positive poll interval") + case config.FixtureWorkspace == "": + return nil, fmt.Errorf("checkpoint observer requires the fixture workspace") + case config.HerdrIntegration == "": + return nil, fmt.Errorf("checkpoint observer requires the exact Herdr integration identity") + case config.PiIntegration == "": + return nil, fmt.Errorf("checkpoint observer requires the exact Pi integration identity") + } + return &CheckpointObserver{config: config, targets: make(map[RunnerCase]ProcessIdentity, 2)}, nil +} + +// NewAuthoritySnapshotLoader builds the production loader. Every snapshot is +// opened mode=ro/query_only, replayed, and closed; it never acquires the +// authority lease, invokes a domain mutator, or shells out to a CLI. +func NewAuthoritySnapshotLoader(path string) AuthoritySnapshotLoader { + return func(ctx context.Context) ([]domain.Fact, error) { + if _, err := os.Stat(path); err != nil { + if errors.Is(err, os.ErrNotExist) { + return nil, nil + } + return nil, fmt.Errorf("checkpoint observer inspecting authority store: %w", err) + } + s, err := store.OpenReadOnly(path) + if err != nil { + return nil, err + } + defer func() { _ = s.Close() }() + return storerepo.New(s).Load(ctx) + } +} + +// Observe polls until all four raw checkpoints have been emitted or ctx is +// canceled. It emits at most one checkpoint from any one snapshot, which +// gives RunCommon a control boundary before the next authority observation. +// No goroutine is created here; cancellation stops the ticker and every send. +func (o *CheckpointObserver) Observe(ctx context.Context, output chan<- RunnerCheckpoint) error { + ticker := time.NewTicker(o.config.PollInterval) + defer ticker.Stop() + + for { + facts, err := o.config.Load(ctx) + if err != nil { + return fmt.Errorf("checkpoint observer loading authority snapshot: %w", err) + } + checkpoint, err := o.next(facts) + if err != nil { + return err + } + if checkpoint != nil { + select { + case output <- *checkpoint: + o.phase++ + if o.phase == len(orderedControlCheckpoints) { + return nil + } + case <-ctx.Done(): + return ctx.Err() + } + } + + select { + case <-ticker.C: + case <-ctx.Done(): + return ctx.Err() + } + } +} + +func (o *CheckpointObserver) next(facts []domain.Fact) (*RunnerCheckpoint, error) { + if o.phase >= len(orderedControlCheckpoints) { + return nil, nil + } + projection, err := projectSnapshot(facts) + if err != nil { + return nil, fmt.Errorf("checkpoint observer projecting authority snapshot: %w", err) + } + caseName := orderedControlCheckpoints[o.phase].caseName + session, ready, err := o.sessionForCase(projection, caseName) + if err != nil || !ready { + return nil, err + } + target, ready, err := o.boundTarget(projection, session) + if err != nil || !ready { + return nil, err + } + + want := orderedControlCheckpoints[o.phase] + if want.kind == CheckpointCommandDelivered { + if target != o.targets[caseName] { + return nil, fmt.Errorf("checkpoint observer: %s process identity changed after bind", caseName) + } + delivered, err := commandDelivered(projection, session, PromptDigest, CanonicalScenario().Fixture.IdempotencyKey) + if err != nil || !delivered { + return nil, err + } + } else { + o.targets[caseName] = target + } + + offset := o.config.Monotonic() + if offset < 0 { + return nil, fmt.Errorf("checkpoint observer monotonic offset is negative") + } + return &RunnerCheckpoint{ + Kind: want.kind, Case: caseName, Target: target, MonotonicMS: offset.Milliseconds(), + }, nil +} + +type snapshotProjection struct { + facts []domain.Fact + authority *domain.Authority +} + +func projectSnapshot(facts []domain.Fact) (snapshotProjection, error) { + repo := snapshotRepository{facts: facts} + authority, err := domain.Open(context.Background(), repo) + if err != nil { + return snapshotProjection{}, err + } + return snapshotProjection{facts: facts, authority: authority}, nil +} + +type snapshotRepository struct { + facts []domain.Fact +} + +func (r snapshotRepository) Load(context.Context) ([]domain.Fact, error) { + return r.facts, nil +} + +func (snapshotRepository) CommitIdentity(context.Context, domain.Change) error { + return errors.New("checkpoint snapshot repository is read-only") +} + +func (snapshotRepository) CommitLaunchResolution(context.Context, domain.Change) error { + return errors.New("checkpoint snapshot repository is read-only") +} + +func (snapshotRepository) CommitCommandAcceptance(context.Context, domain.Change) error { + return errors.New("checkpoint snapshot repository is read-only") +} + +func (snapshotRepository) CommitCommandTransition(context.Context, domain.Change) error { + return errors.New("checkpoint snapshot repository is read-only") +} + +func (snapshotRepository) CommitObservation(context.Context, domain.Change) error { + return errors.New("checkpoint snapshot repository is read-only") +} + +type launchFact struct { + session domain.SessionID + instance domain.InstanceID + index int +} + +func (o *CheckpointObserver) sessionForCase(projection snapshotProjection, caseName RunnerCase) (domain.SessionID, bool, error) { + ordinal, ok := canonicalLaunchOrdinal(caseName) + if !ok { + return "", false, fmt.Errorf("checkpoint observer has no canonical launch ordinal for %s", caseName) + } + launches, complete, err := o.orderedFixtureLaunches(projection) + if err != nil { + return "", false, err + } + if !complete || len(launches) <= ordinal { + return "", false, nil + } + return launches[ordinal].session, true, nil +} + +// canonicalLaunchOrdinal is the single intentional ordinal bridge between +// durable sessions and scenario cases. The authority has no durable case +// field, so opaque IDs must never be sorted or interpreted: the nth valid +// fixture launch is mapped to the nth canonical runnable launch. A case whose +// prerequisite is explicitly unavailable cannot create a session and does +// not consume an ordinal. A future durable case field should replace only +// this function and its caller. +func canonicalLaunchOrdinal(caseName RunnerCase) (int, bool) { + ordinal := 0 + for _, group := range caseMatrix { + hasLaunch := false + for _, stage := range group.Stages { + if stage == "launch" { + hasLaunch = true + break + } + } + if !hasLaunch { + continue + } + if group.Case == string(RunnerCaseBlocked) && blockedPrerequisiteUnavailable() { + continue + } + if group.Case == string(caseName) { + return ordinal, true + } + ordinal++ + } + return 0, false +} + +func canonicalLaunchCount() int { + count := 0 + for _, group := range caseMatrix { + if group.Case == string(RunnerCaseBlocked) && blockedPrerequisiteUnavailable() { + continue + } + for _, stage := range group.Stages { + if stage == "launch" { + count++ + break + } + } + } + return count +} + +func blockedPrerequisiteUnavailable() bool { + for _, prerequisite := range CanonicalScenario().Prerequisites { + if prerequisite.Name == CanonicalOracle().BlockedPrerequisite { + return prerequisite.Status == "unavailable" + } + } + return false +} + +func (o *CheckpointObserver) orderedFixtureLaunches(projection snapshotProjection) ([]launchFact, bool, error) { + seen := make(map[domain.SessionID]struct{}) + workspaceIDs := make(map[domain.WorkspaceID]struct{}) + var launches []launchFact + for index, fact := range projection.facts { + if fact.Kind != domain.FactSessionCreated || fact.Session == nil { + continue + } + id := fact.Session.ID + if _, duplicate := seen[id]; duplicate { + return nil, false, fmt.Errorf("checkpoint observer: duplicate session.created for %s", id) + } + seen[id] = struct{}{} + workspace, ok := projection.authority.Workspace(fact.Session.Workspace) + if !ok || workspace.RootPath != o.config.FixtureWorkspace { + continue + } + if fact.Reason != "launch" || fact.Session.Current == "" { + return nil, false, fmt.Errorf("checkpoint observer: ambiguous non-launch session %s in fixture workspace", id) + } + workspaceIDs[fact.Session.Workspace] = struct{}{} + launches = append(launches, launchFact{session: id, instance: fact.Session.Current, index: index}) + } + if len(workspaceIDs) > 1 { + return nil, false, fmt.Errorf("checkpoint observer: fixture workspace resolves to multiple durable workspace IDs") + } + if len(launches) > canonicalLaunchCount() { + return nil, false, fmt.Errorf("checkpoint observer: ambiguous extra fixture launch") + } + for _, launch := range launches { + instanceAt, launchedAt := -1, -1 + instanceCount, launchedCount := 0, 0 + for index, fact := range projection.facts { + switch { + case fact.Kind == domain.FactInstanceStarted && fact.Instance != nil && fact.Instance.ID == launch.instance: + if fact.Instance.Session != launch.session || fact.SessionID != launch.session { + return nil, false, fmt.Errorf("checkpoint observer: launch instance %s has inconsistent session", launch.instance) + } + instanceAt, instanceCount = index, instanceCount+1 + case fact.Kind == domain.FactSessionLaunched && fact.SessionID == launch.session: + if fact.InstanceID != launch.instance { + return nil, false, fmt.Errorf("checkpoint observer: session %s launch names the wrong instance", launch.session) + } + launchedAt, launchedCount = index, launchedCount+1 + } + } + if instanceCount == 0 || launchedCount == 0 { + return launches, false, nil + } + if instanceCount != 1 || launchedCount != 1 { + return nil, false, fmt.Errorf("checkpoint observer: duplicated launch prerequisites for %s", launch.session) + } + if launch.index >= instanceAt || instanceAt >= launchedAt { + return nil, false, fmt.Errorf("checkpoint observer: reordered launch prerequisites for %s", launch.session) + } + } + return launches, true, nil +} + +func (o *CheckpointObserver) boundTarget(projection snapshotProjection, sessionID domain.SessionID) (ProcessIdentity, bool, error) { + session, ok := projection.authority.Session(sessionID) + if !ok || session.Current == "" { + return ProcessIdentity{}, false, nil + } + live := 0 + seenInstances := make(map[domain.InstanceID]struct{}) + for _, fact := range projection.facts { + if fact.Kind != domain.FactInstanceStarted || fact.Instance == nil || fact.Instance.Session != sessionID { + continue + } + if _, duplicate := seenInstances[fact.Instance.ID]; duplicate { + return ProcessIdentity{}, false, fmt.Errorf("checkpoint observer: duplicate instance.started for %s", fact.Instance.ID) + } + seenInstances[fact.Instance.ID] = struct{}{} + instance, exists := projection.authority.Instance(fact.Instance.ID) + if exists && instance.State == domain.InstanceLive { + live++ + if instance.ID != session.Current { + return ProcessIdentity{}, false, nil + } + } + } + current, ok := projection.authority.Instance(session.Current) + if !ok || current.Session != sessionID || current.State != domain.InstanceLive || live != 1 { + return ProcessIdentity{}, false, nil + } + + attachments := projection.authority.Attachments(sessionID) + if len(attachments) != 1 { + return ProcessIdentity{}, false, nil + } + attachment := attachments[0] + if session.Attachment != attachment.ID || attachment.State != domain.Attached || + attachment.Continuity != domain.ContinuityVerified || + attachment.IntegrationInstance != o.config.HerdrIntegration || + attachment.Epoch.Kind != "herdr.terminal_id" || attachment.Epoch.Scope != domain.EpochScopePane || + attachment.Epoch.Value == "" || attachment.Container == "" || !attachment.Process.Present() { + return ProcessIdentity{}, false, nil + } + hostFingerprint := domain.Fingerprint{ + IntegrationInstance: attachment.IntegrationInstance, + Epoch: attachment.Epoch, + Container: attachment.Container, + Process: attachment.Process, + } + hostClaim, held := projection.authority.ActiveClaim(hostFingerprint.ClaimRef()) + if !held || hostClaim.Session != sessionID || hostClaim.Instance != current.ID { + return ProcessIdentity{}, false, nil + } + + var agent *domain.Correlation + for _, correlation := range projection.authority.Correlations(domain.TargetInstance, string(current.ID)) { + if correlation.ExternalKind != "agent.session" || correlation.Status != domain.CorrelationActive { + continue + } + if agent != nil { + return ProcessIdentity{}, false, nil + } + correlationCopy := correlation + agent = &correlationCopy + } + if agent == nil || agent.Scope != o.config.PiIntegration || agent.ExternalValue == "" { + return ProcessIdentity{}, false, nil + } + agentRef := domain.AgentSessionRef{IntegrationInstance: agent.Scope, SessionID: agent.ExternalValue} + agentClaim, held := projection.authority.ActiveClaim(agentRef.ClaimRef()) + if !held || agentClaim.Session != sessionID || agentClaim.Instance != current.ID { + return ProcessIdentity{}, false, nil + } + + return ProcessIdentity{ + PID: attachment.Process.PID, StartTime: attachment.Process.StartedAt, + TerminalID: attachment.Epoch.Value, AttachmentID: string(attachment.ID), + }, true, nil +} + +func commandDelivered( + projection snapshotProjection, + sessionID domain.SessionID, + digest, key string, +) (bool, error) { + var matches []domain.PromptCommand + seen := make(map[domain.CommandID]struct{}) + for _, fact := range projection.facts { + if fact.Kind != domain.FactCommandAccepted || fact.Command == nil { + continue + } + id := fact.Command.ID + if _, duplicate := seen[id]; duplicate { + return false, fmt.Errorf("checkpoint observer: duplicate command.accepted for %s", id) + } + seen[id] = struct{}{} + command, ok := projection.authority.Command(id) + if !ok || command.Session != sessionID || command.Instance != fact.Command.Instance { + continue + } + if command.Operation == domain.PromptDeliverOperation && command.IdempotencyKey == key && command.CanonicalDigest == digest { + matches = append(matches, command) + } + } + if len(matches) == 0 { + return false, nil + } + if len(matches) != 1 { + return false, fmt.Errorf("checkpoint observer: ambiguous canonical commands for session %s", sessionID) + } + command := matches[0] + session, ok := projection.authority.Session(sessionID) + if !ok || command.Instance != session.Current || command.State != domain.ResponsibilityDelivered || + len(command.Attempts) != 1 || command.Attempts[0].PathKind != domain.PromptPathRuntime || + command.Attempts[0].RecordedResult != string(domain.ResponsibilityDelivered) { + return false, nil + } + acceptedAt, attemptAt, deliveredAt := -1, -1, -1 + acceptedCount, attemptCount, deliveredCount := 0, 0, 0 + for index, fact := range projection.facts { + if fact.Command == nil || fact.Command.ID != command.ID { + continue + } + if fact.SessionID != sessionID || fact.InstanceID != command.Instance { + return false, fmt.Errorf("checkpoint observer: command %s fact target changed", command.ID) + } + switch fact.Kind { + case domain.FactCommandAccepted: + acceptedAt, acceptedCount = index, acceptedCount+1 + case domain.FactCommandAttemptCreated: + if fact.Command.Attempt == nil || fact.Command.Attempt.ID != command.Attempts[0].ID || + fact.Command.Attempt.PathKind != domain.PromptPathRuntime { + return false, nil + } + attemptAt, attemptCount = index, attemptCount+1 + case domain.FactCommandDelivered: + if fact.Command.State != domain.ResponsibilityDelivered || fact.Command.Attempt == nil || + fact.Command.Attempt.ID != command.Attempts[0].ID || + fact.Command.Attempt.PathKind != domain.PromptPathRuntime || + fact.Command.Attempt.RecordedResult != string(domain.ResponsibilityDelivered) { + return false, nil + } + deliveredAt, deliveredCount = index, deliveredCount+1 + } + } + if acceptedCount != 1 || attemptCount != 1 || deliveredCount != 1 { + if acceptedCount > 1 || attemptCount > 1 || deliveredCount > 1 { + return false, fmt.Errorf("checkpoint observer: duplicated command lifecycle for %s", command.ID) + } + return false, nil + } + if acceptedAt >= attemptAt || attemptAt >= deliveredAt { + return false, fmt.Errorf("checkpoint observer: reordered command lifecycle for %s", command.ID) + } + return true, nil +} diff --git a/internal/conformance/portablelauncher/checkpoints_test.go b/internal/conformance/portablelauncher/checkpoints_test.go new file mode 100644 index 0000000..f63ba47 --- /dev/null +++ b/internal/conformance/portablelauncher/checkpoints_test.go @@ -0,0 +1,483 @@ +package portablelauncher + +import ( + "context" + "errors" + "os" + "path/filepath" + "strings" + "testing" + "time" + + "github.com/procrastivity/duo/internal/domain" +) + +const ( + checkpointWorkspace = "/fixture/workspace" + checkpointHerdr = "herdr@/fixture/herdr.sock" + checkpointPi = "pi@fixture" +) + +var checkpointLaunches = []struct { + session domain.SessionID + instance domain.InstanceID +}{ + // IDs are deliberately reverse-lexical. The observer must use durable + // creation order, never opaque-ID order. + {session: "ses_z_happy", instance: "ins_z_happy"}, + {session: "ses_b_exited", instance: "ins_b_exited"}, + {session: "ses_a_timeout", instance: "ins_a_timeout"}, +} + +func TestCheckpointObserverUsesFactOrderAndMonotonicOffsets(t *testing.T) { + clock := &offsetClock{offsets: []time.Duration{ + 11*time.Millisecond + 900*time.Microsecond, + 22 * time.Millisecond, + 33 * time.Millisecond, + 44 * time.Millisecond, + }} + observer := newTestObserver(t, checkpointWorkspace, clock.Now) + + exitedFacts := fixtureLaunchFacts(2) + exitedTarget := addCompleteBind(&exitedFacts, 1, bindOptions{}) + checkpoint := requireNext(t, observer, exitedFacts) + assertCheckpoint(t, checkpoint, CheckpointAttachmentBound, RunnerCaseExited, exitedTarget, 11) + observer.phase++ + + // The same snapshot cannot repeat the bind checkpoint, and it has no + // delivered command with which to advance. + if got := nextCheckpoint(t, observer, exitedFacts); got != nil { + t.Fatalf("duplicate snapshot emitted %+v", *got) + } + addCommand(&exitedFacts, 1, commandOptions{}) + checkpoint = requireNext(t, observer, exitedFacts) + assertCheckpoint(t, checkpoint, CheckpointCommandDelivered, RunnerCaseExited, exitedTarget, 22) + observer.phase++ + if got := nextCheckpoint(t, observer, exitedFacts); got != nil { + t.Fatalf("repeated delivered snapshot emitted %+v", *got) + } + + timeoutFacts := fixtureLaunchFacts(3) + addCompleteBind(&timeoutFacts, 1, bindOptions{}) + addCommand(&timeoutFacts, 1, commandOptions{}) + timeoutTarget := addCompleteBind(&timeoutFacts, 2, bindOptions{}) + checkpoint = requireNext(t, observer, timeoutFacts) + assertCheckpoint(t, checkpoint, CheckpointAttachmentBound, RunnerCaseTimeout, timeoutTarget, 33) + observer.phase++ + addCommand(&timeoutFacts, 2, commandOptions{}) + checkpoint = requireNext(t, observer, timeoutFacts) + assertCheckpoint(t, checkpoint, CheckpointCommandDelivered, RunnerCaseTimeout, timeoutTarget, 44) +} + +func TestCheckpointObserverRefusesIncompleteOrMismatchedBind(t *testing.T) { + tests := []struct { + name string + workspace string + options bindOptions + mutate func(*[]domain.Fact) + }{ + {name: "incomplete process", workspace: checkpointWorkspace, options: bindOptions{missingStart: true}}, + {name: "wrong workspace", workspace: "/other/workspace"}, + {name: "wrong Herdr integration", workspace: checkpointWorkspace, options: bindOptions{herdr: "herdr@other"}}, + {name: "wrong Pi correlation", workspace: checkpointWorkspace, options: bindOptions{pi: "pi@other"}}, + {name: "inactive Pi correlation", workspace: checkpointWorkspace, options: bindOptions{correlationStatus: domain.CorrelationStale}}, + {name: "host claim missing", workspace: checkpointWorkspace, options: bindOptions{omitHostClaim: true}}, + {name: "agent claim missing", workspace: checkpointWorkspace, options: bindOptions{omitAgentClaim: true}}, + {name: "multiple attachments", workspace: checkpointWorkspace, mutate: addSecondAttachment}, + {name: "multiple live instances", workspace: checkpointWorkspace, mutate: addSecondLiveInstance}, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + facts := fixtureLaunchFacts(2) + addCompleteBind(&facts, 1, test.options) + if test.mutate != nil { + test.mutate(&facts) + } + observer := newTestObserver(t, test.workspace, func() time.Duration { return time.Second }) + if got := nextCheckpoint(t, observer, facts); got != nil { + t.Fatalf("invalid bind emitted %+v", *got) + } + }) + } +} + +func TestCheckpointObserverRequiresExactDurableDelivery(t *testing.T) { + tests := []struct { + name string + options commandOptions + }{ + {name: "accepted only", options: commandOptions{acceptedOnly: true}}, + {name: "attempted only", options: commandOptions{attemptedOnly: true}}, + {name: "wrong key", options: commandOptions{key: "other-key"}}, + {name: "wrong digest", options: commandOptions{digest: ConflictDigest}}, + {name: "wrong instance", options: commandOptions{instance: checkpointLaunches[0].instance}}, + {name: "host path", options: commandOptions{path: domain.PromptPathHost}}, + {name: "multiple attempts", options: commandOptions{secondAttempt: true}}, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + facts := fixtureLaunchFacts(2) + target := addCompleteBind(&facts, 1, bindOptions{}) + observer := newTestObserver(t, checkpointWorkspace, func() time.Duration { return 7 * time.Millisecond }) + bind := requireNext(t, observer, facts) + if bind.Target != target { + t.Fatalf("bind target = %+v, want %+v", bind.Target, target) + } + observer.phase++ + addCommand(&facts, 1, test.options) + if got := nextCheckpoint(t, observer, facts); got != nil { + t.Fatalf("invalid command emitted %+v", *got) + } + }) + } +} + +func TestCheckpointObserverFailsClosedOnLaunchPrerequisiteDrift(t *testing.T) { + t.Run("missing prior launch", func(t *testing.T) { + facts := fixtureLaunchFacts(1) + addCompleteBind(&facts, 0, bindOptions{}) + observer := newTestObserver(t, checkpointWorkspace, func() time.Duration { return 0 }) + if got := nextCheckpoint(t, observer, facts); got != nil { + t.Fatalf("missing exited ordinal emitted %+v", *got) + } + }) + + t.Run("reordered launch facts", func(t *testing.T) { + facts := fixtureLaunchFacts(2) + created := factIndex(facts, domain.FactSessionCreated, checkpointLaunches[1].session) + // The generated trio is session.created, instance.started, + // session.launched. Swap the latter two. + facts[created+1], facts[created+2] = facts[created+2], facts[created+1] + observer := newTestObserver(t, checkpointWorkspace, func() time.Duration { return 0 }) + if _, err := observer.next(facts); err == nil || !strings.Contains(err.Error(), "reordered launch prerequisites") { + t.Fatalf("reordered facts error = %v", err) + } + }) + + t.Run("duplicate creation", func(t *testing.T) { + facts := fixtureLaunchFacts(2) + index := factIndex(facts, domain.FactSessionCreated, checkpointLaunches[1].session) + facts = append(facts, facts[index]) + observer := newTestObserver(t, checkpointWorkspace, func() time.Duration { return 0 }) + if _, err := observer.next(facts); err == nil || !strings.Contains(err.Error(), "duplicate session.created") { + t.Fatalf("duplicate facts error = %v", err) + } + }) + + t.Run("ambiguous extra launch", func(t *testing.T) { + facts := fixtureLaunchFacts(3) + appendLaunch(&facts, "ses_extra", "ins_extra") + observer := newTestObserver(t, checkpointWorkspace, func() time.Duration { return 0 }) + if _, err := observer.next(facts); err == nil || !strings.Contains(err.Error(), "ambiguous extra fixture launch") { + t.Fatalf("extra launch error = %v", err) + } + }) +} + +func TestCheckpointObserverCancellationDoesNotBlock(t *testing.T) { + observer := newTestObserver(t, checkpointWorkspace, func() time.Duration { return 0 }) + ctx, cancel := context.WithCancel(context.Background()) + done := make(chan error, 1) + go func() { + done <- observer.Observe(ctx, make(chan RunnerCheckpoint)) + }() + cancel() + select { + case err := <-done: + if !errors.Is(err, context.Canceled) { + t.Fatalf("Observe cancellation error = %v", err) + } + case <-time.After(time.Second): + t.Fatal("Observe did not return after cancellation") + } +} + +func TestAuthoritySnapshotLoaderTreatsMissingStoreAsNoFacts(t *testing.T) { + path := filepath.Join(t.TempDir(), "missing", "duo.db") + facts, err := NewAuthoritySnapshotLoader(path)(context.Background()) + if err != nil { + t.Fatalf("missing authority store: %v", err) + } + if facts != nil { + t.Fatalf("missing authority facts = %v, want nil", facts) + } + if _, err := os.Stat(filepath.Dir(path)); !errors.Is(err, os.ErrNotExist) { + t.Fatalf("snapshot loader created missing parent: %v", err) + } +} + +type offsetClock struct { + offsets []time.Duration + next int +} + +func (c *offsetClock) Now() time.Duration { + value := c.offsets[c.next] + c.next++ + return value +} + +func newTestObserver(t *testing.T, workspace string, clock MonotonicClock) *CheckpointObserver { + t.Helper() + observer, err := NewCheckpointObserver(CheckpointObserverConfig{ + Load: func(context.Context) ([]domain.Fact, error) { + return nil, nil + }, + Monotonic: clock, + PollInterval: time.Hour, + FixtureWorkspace: workspace, + HerdrIntegration: checkpointHerdr, + PiIntegration: checkpointPi, + }) + if err != nil { + t.Fatalf("NewCheckpointObserver: %v", err) + } + return observer +} + +func nextCheckpoint(t *testing.T, observer *CheckpointObserver, facts []domain.Fact) *RunnerCheckpoint { + t.Helper() + checkpoint, err := observer.next(facts) + if err != nil { + t.Fatalf("next: %v", err) + } + return checkpoint +} + +func requireNext(t *testing.T, observer *CheckpointObserver, facts []domain.Fact) *RunnerCheckpoint { + t.Helper() + checkpoint := nextCheckpoint(t, observer, facts) + if checkpoint == nil { + t.Fatal("next returned no checkpoint") + } + return checkpoint +} + +func assertCheckpoint( + t *testing.T, + got *RunnerCheckpoint, + kind RunnerCheckpointKind, + caseName RunnerCase, + target ProcessIdentity, + offset int64, +) { + t.Helper() + if got.Kind != kind || got.Case != caseName || got.Target != target || got.MonotonicMS != offset { + t.Fatalf("checkpoint = %+v, want kind=%s case=%s target=%+v offset=%d", got, kind, caseName, target, offset) + } +} + +func fixtureLaunchFacts(count int) []domain.Fact { + workspace := domain.Workspace{ID: "ws_fixture", RootPath: checkpointWorkspace, CreatedAt: "2099-01-01T00:00:00.000Z"} + facts := []domain.Fact{{ + ID: "fact_workspace", Kind: domain.FactWorkspaceCreated, At: workspace.CreatedAt, Workspace: &workspace, + }} + for index := 0; index < count; index++ { + appendLaunch(&facts, checkpointLaunches[index].session, checkpointLaunches[index].instance) + } + return facts +} + +func appendLaunch(facts *[]domain.Fact, sessionID domain.SessionID, instanceID domain.InstanceID) { + session := domain.Session{ + ID: sessionID, Workspace: "ws_fixture", State: domain.SessionActive, + Current: instanceID, CreatedAt: "2099-01-01T00:00:00.000Z", + } + instance := domain.RuntimeInstance{ + ID: instanceID, Session: sessionID, State: domain.InstanceStarting, + StartedAt: "2099-01-01T00:00:00.000Z", + } + *facts = append(*facts, + domain.Fact{ID: domain.FactID("fact_create_" + sessionID), Kind: domain.FactSessionCreated, At: session.CreatedAt, Reason: "launch", Session: &session}, + domain.Fact{ID: domain.FactID("fact_start_" + instanceID), Kind: domain.FactInstanceStarted, At: instance.StartedAt, SessionID: sessionID, Instance: &instance}, + domain.Fact{ID: domain.FactID("fact_launch_" + sessionID), Kind: domain.FactSessionLaunched, At: instance.StartedAt, SessionID: sessionID, InstanceID: instanceID}, + domain.Fact{ID: domain.FactID("fact_live_" + instanceID), Kind: domain.FactInstanceState, At: instance.StartedAt, InstanceID: instanceID, State: string(domain.InstanceLive)}, + ) +} + +type bindOptions struct { + missingStart bool + herdr string + pi string + correlationStatus domain.CorrelationStatus + omitHostClaim bool + omitAgentClaim bool +} + +func addCompleteBind(facts *[]domain.Fact, launch int, options bindOptions) ProcessIdentity { + sessionID := checkpointLaunches[launch].session + instanceID := checkpointLaunches[launch].instance + herdr := options.herdr + if herdr == "" { + herdr = checkpointHerdr + } + pi := options.pi + if pi == "" { + pi = checkpointPi + } + status := options.correlationStatus + if status == "" { + status = domain.CorrelationActive + } + started := "birth-" + string(sessionID) + if options.missingStart { + started = "" + } + attachment := domain.HostAttachment{ + ID: domain.AttachmentID("att_" + sessionID), Session: sessionID, + IntegrationInstance: herdr, + Epoch: domain.HostEpoch{ + Kind: "herdr.terminal_id", Value: "terminal-" + string(sessionID), Scope: domain.EpochScopePane, + }, + Container: "pane-" + string(sessionID), + Process: domain.ProcessBirth{ + PID: 1000 + launch, StartedAt: started, Executable: "/fixture/bin/pi", + }, + State: domain.Attached, + } + *facts = append(*facts, domain.Fact{ + ID: domain.FactID("fact_attachment_" + sessionID), Kind: domain.FactAttachmentCreated, + SessionID: sessionID, Attachment: &attachment, + }) + fingerprint := domain.Fingerprint{ + IntegrationInstance: attachment.IntegrationInstance, + Epoch: attachment.Epoch, + Container: attachment.Container, + Process: attachment.Process, + } + if !options.omitHostClaim { + claim := domain.Claim{Ref: fingerprint.ClaimRef(), Session: sessionID, Instance: instanceID} + *facts = append(*facts, domain.Fact{Kind: domain.FactClaimHeld, Claim: &claim}) + } + correlation := domain.Correlation{ + ID: "cor_agent_" + domain.CorrelationID(sessionID), + TargetKind: domain.TargetInstance, TargetID: string(instanceID), + ExternalKind: "agent.session", ExternalValue: "pi-session-" + string(sessionID), + Scope: pi, Status: status, + } + *facts = append(*facts, domain.Fact{Kind: domain.FactCorrelationClaimed, Correlation: &correlation}) + if !options.omitAgentClaim { + agentRef := domain.AgentSessionRef{IntegrationInstance: pi, SessionID: correlation.ExternalValue} + claim := domain.Claim{Ref: agentRef.ClaimRef(), Session: sessionID, Instance: instanceID} + *facts = append(*facts, domain.Fact{Kind: domain.FactClaimHeld, Claim: &claim}) + } + return ProcessIdentity{ + PID: attachment.Process.PID, StartTime: attachment.Process.StartedAt, + TerminalID: attachment.Epoch.Value, AttachmentID: string(attachment.ID), + } +} + +func addSecondAttachment(facts *[]domain.Fact) { + sessionID := checkpointLaunches[1].session + attachment := domain.HostAttachment{ + ID: "att_second", + Session: sessionID, + IntegrationInstance: checkpointHerdr, + Epoch: domain.HostEpoch{ + Kind: "herdr.terminal_id", Value: "terminal-second", Scope: domain.EpochScopePane, + }, + Container: "pane-second", + Process: domain.ProcessBirth{ + PID: 2002, StartedAt: "birth-second", Executable: "/fixture/bin/pi", + }, + State: domain.Attached, + } + *facts = append(*facts, domain.Fact{ + Kind: domain.FactAttachmentCreated, SessionID: sessionID, Attachment: &attachment, + }) +} + +func addSecondLiveInstance(facts *[]domain.Fact) { + sessionID := checkpointLaunches[1].session + instanceID := domain.InstanceID("ins_second_live") + instance := domain.RuntimeInstance{ID: instanceID, Session: sessionID, State: domain.InstanceLive} + *facts = append(*facts, domain.Fact{ + Kind: domain.FactInstanceStarted, SessionID: sessionID, Instance: &instance, + }) +} + +type commandOptions struct { + acceptedOnly bool + attemptedOnly bool + key string + digest string + instance domain.InstanceID + path domain.PromptPathKind + secondAttempt bool +} + +func addCommand(facts *[]domain.Fact, launch int, options commandOptions) { + sessionID := checkpointLaunches[launch].session + instanceID := options.instance + if instanceID == "" { + instanceID = checkpointLaunches[launch].instance + } + key := options.key + if key == "" { + key = CanonicalScenario().Fixture.IdempotencyKey + } + digest := options.digest + if digest == "" { + digest = PromptDigest + } + path := options.path + if path == "" { + path = domain.PromptPathRuntime + } + commandID := domain.CommandID("cmd_" + sessionID) + attemptID := domain.AttemptID("try_" + sessionID) + accepted := domain.CommandFact{ + ID: commandID, Revision: 1, Operation: domain.PromptDeliverOperation, + Session: sessionID, Instance: instanceID, Caller: "outer-agent", + IdempotencyKey: key, CanonicalDigest: digest, + QueuePolicy: domain.QueueUntilSafe, ExpiresAt: "2099-01-01T00:15:00.000Z", + State: domain.ResponsibilityQueued, AcceptedAt: "2099-01-01T00:00:00.000Z", + } + *facts = append(*facts, domain.Fact{ + Kind: domain.FactCommandAccepted, SessionID: sessionID, InstanceID: instanceID, Command: &accepted, + }) + if options.acceptedOnly { + return + } + attempt := domain.CommandAttempt{ID: attemptID, PathKind: path, StartedAt: "2099-01-01T00:00:01.000Z"} + attempted := accepted + attempted.Revision = 2 + attempted.State = domain.ResponsibilityAttempting + attempted.PathKind = path + attempted.Attempt = &attempt + *facts = append(*facts, domain.Fact{ + Kind: domain.FactCommandAttemptCreated, SessionID: sessionID, InstanceID: instanceID, Command: &attempted, + }) + if options.secondAttempt { + second := attempt + second.ID = "try_second" + secondFact := attempted + secondFact.Revision = 3 + secondFact.Attempt = &second + *facts = append(*facts, domain.Fact{ + Kind: domain.FactCommandAttemptCreated, SessionID: sessionID, InstanceID: instanceID, Command: &secondFact, + }) + } + if options.attemptedOnly { + return + } + deliveredAttempt := attempt + deliveredAttempt.RecordedResult = string(domain.ResponsibilityDelivered) + delivered := accepted + delivered.Revision = 3 + delivered.State = domain.ResponsibilityDelivered + delivered.TerminalAt = "2099-01-01T00:00:02.000Z" + delivered.Attempt = &deliveredAttempt + *facts = append(*facts, domain.Fact{ + Kind: domain.FactCommandDelivered, SessionID: sessionID, InstanceID: instanceID, Command: &delivered, + }) +} + +func factIndex(facts []domain.Fact, kind domain.FactKind, session domain.SessionID) int { + for index, fact := range facts { + if fact.Kind == kind && fact.Session != nil && fact.Session.ID == session { + return index + } + } + return -1 +} diff --git a/internal/conformance/portablelauncher/collector.go b/internal/conformance/portablelauncher/collector.go new file mode 100644 index 0000000..bd10aa7 --- /dev/null +++ b/internal/conformance/portablelauncher/collector.go @@ -0,0 +1,351 @@ +package portablelauncher + +import ( + "encoding/json" + "fmt" + "reflect" +) + +// CommonStageCapture is the launcher-neutral input for one stage. Production +// callers obtain it from AssembleCommonStages; direct construction exists for +// focused collector tests only. Observations cannot carry expectations or a +// verdict. Sequence selects the corresponding canonical scenario step. +type CommonStageCapture struct { + Sequence int + StartedOffsetMS int64 + DurationMS int64 + Observations []Observation + Failure *StageError +} + +// CollectorInput contains common-owned run metadata and stage captures. The +// RunRecord is retained so orchestration and cleanup failures cannot be hidden. +// Raw launcher events and stderr are diagnostic capture, never result input. +type CollectorInput struct { + Run RunIdentity + Pins Pins + Record RunRecord + Stages []CommonStageCapture +} + +// CollectResult constructs the authoritative result and its in-memory, +// content-addressed evidence. It always returns the complete canonical stage +// list. The production entry point is Orchestrate, which supplies only trusted +// assembled captures. The caller may persist the returned Capture in a later +// bundle-writing step. +func CollectResult(input CollectorInput) (Result, *Capture) { + scenario := CanonicalScenario() + scenarioJSON, err := ScenarioJSON(scenario) + if err != nil { + panic(fmt.Sprintf("portable launcher collector: canonical scenario: %v", err)) + } + + result := Result{ + Schema: ResultSchema, + Suite: SuiteIdentity{ + Name: ScenarioName, Revision: scenario.Revision, + ManifestDigest: Digest(scenarioJSON), OracleDigest: OracleDigest(), + }, + Run: input.Run, Pins: input.Pins, + Stages: make([]StageResult, len(scenario.Steps)), + Scrub: ScrubRecord{Status: "pass", Policy: ScrubPolicy, Findings: []string{}}, + } + + // Build identities and timings before deriving observations: the oracle's + // deadline derivation checks these authoritative stage fields. + var priorEnd int64 + for i, step := range scenario.Steps { + started, duration := priorEnd, int64(0) + if i < len(input.Stages) && input.Stages[i].Sequence == step.Sequence { + started = input.Stages[i].StartedOffsetMS + duration = input.Stages[i].DurationMS + } + if started < priorEnd || duration < 0 { + started, duration = priorEnd, 0 + } + result.Stages[i] = StageResult{ + Sequence: step.Sequence, Stage: step.Stage, Case: step.Case, + StartedOffsetMS: started, DurationMS: duration, + } + priorEnd = started + duration + } + + capture := NewCapture() + var firstFailure *failureOrigin + var origin *failureOrigin + var originBeforeBlocked *failureOrigin + rememberFailure := func(stage StageResult) { + origin = newFailureOrigin(stage) + if firstFailure == nil { + firstFailure = origin + } + } + for i, step := range scenario.Steps { + stage := &result.Stages[i] + if origin != nil && origin.Case == "blocked" && step.Case != "blocked" { + origin = originBeforeBlocked + originBeforeBlocked = nil + } + // The known structural blocked prerequisite is canonical and + // case-local. Preserve it even when an earlier run failure made other + // semantic stages unreachable; otherwise the capture could erase the + // suite's mandatory blocker. + if blockedPrerequisiteUnavailable() && step.Case == "blocked" && step.Stage == "launch" { + if origin != nil && origin.Case != "blocked" { + originBeforeBlocked = origin + } + setBlockedUnavailable(stage, capture, &result.Scrub) + rememberFailure(*stage) + continue + } + if origin != nil && step.Stage != "cleanup" { + stage.StartedOffsetMS = result.Stages[i-1].StartedOffsetMS + result.Stages[i-1].DurationMS + stage.DurationMS = 0 + setNotReached(stage, *origin, capture, &result.Scrub) + continue + } + + if err := recordFailureAt(input.Record, i, len(scenario.Steps)); err != nil { + setFailed(stage, collectorError("collector.run_failed", err.Error()), capture, &result.Scrub) + rememberFailure(*stage) + continue + } + + if i >= len(input.Stages) { + setFailed(stage, collectorError("collector.stage_missing", "required common stage capture is missing"), capture, &result.Scrub) + rememberFailure(*stage) + continue + } + common := input.Stages[i] + if common.Sequence != step.Sequence { + message := fmt.Sprintf("common stage capture sequence %d does not match required sequence %d", common.Sequence, step.Sequence) + setFailed(stage, collectorError("collector.stage_invalid", message), capture, &result.Scrub) + rememberFailure(*stage) + continue + } + + ref, evidenceErr := addStageEvidence(capture, common.Observations) + if evidenceErr != nil { + result.Scrub.Status = "fail" + result.Scrub.Findings = append(result.Scrub.Findings, evidenceErr.Error()) + setFailed(stage, collectorError("collector.evidence_rejected", "independent stage evidence failed capture policy"), capture, &result.Scrub) + rememberFailure(*stage) + continue + } + stage.Evidence = []string{ref} + + if common.Failure != nil { + stage.Verdict, stage.Outcome = "fail", "error" + stage.Assertions, _ = collectAssertions(result, step, common.Observations) + stage.Error = cloneStageError(common.Failure) + if stage.Error.Code == "" || stage.Error.Message == "" || stage.Error.Effect == "" || stage.Error.Retry.Action == "" { + stage.Error = collectorError("collector.stage_failed", "common stage reported an incomplete failure") + } + setFailureAssertion(stage, capture, &result.Scrub) + rememberFailure(*stage) + continue + } + + assertions, assertionErr := collectAssertions(result, step, common.Observations) + stage.Assertions = assertions + if assertionErr != nil { + stage.Verdict, stage.Outcome = "fail", "error" + stage.Error = collectorError("collector.invalid_stage_evidence", assertionErr.Error()) + if len(stage.Assertions) == 0 { + setFailureAssertion(stage, capture, &result.Scrub) + } + rememberFailure(*stage) + continue + } + stage.Verdict, stage.Outcome = "pass", step.Outcome + } + + // An extra capture has no canonical identity. Fail the final canonical + // checkpoint rather than append a launcher-defined stage. + if len(input.Stages) > len(scenario.Steps) { + last := &result.Stages[len(result.Stages)-1] + setFailed(last, collectorError("collector.unexpected_stage", "common capture contains a stage outside the canonical scenario"), capture, &result.Scrub) + rememberFailure(*last) + } + + if firstFailure == nil { + result.Summary = Summary{Verdict: "pass"} + } else { + result.Summary = Summary{ + Verdict: "fail", FirstFailedStage: stringPointerValue(firstFailure.Stage), FirstFailedCase: stringPointerValue(firstFailure.Case), + } + } + return result, capture +} + +type failureOrigin struct { + Sequence int + Stage string + Case string + Evidence []string +} + +func newFailureOrigin(stage StageResult) *failureOrigin { + return &failureOrigin{Sequence: stage.Sequence, Stage: stage.Stage, Case: stage.Case, Evidence: append([]string(nil), stage.Evidence...)} +} + +func collectAssertions(result Result, step StepSpec, observations []Observation) ([]Assertion, error) { + byID := make(map[string][]Observation, len(observations)) + for _, observation := range observations { + if observation.Sequence != step.Sequence { + return nil, fmt.Errorf("observation sequence %d does not match stage sequence %d", observation.Sequence, step.Sequence) + } + byID[observation.AssertionID] = append(byID[observation.AssertionID], observation) + } + + assertions := make([]Assertion, 0, len(step.Assertions)) + for _, expected := range step.Assertions { + matches := byID[expected.ID] + if len(matches) != 1 { + return assertions, fmt.Errorf("assertion %q has %d independent observations, want 1", expected.ID, len(matches)) + } + observation := matches[0] + if observation.Source == "launcher_result" || observation.Source == "launcher_final_text" || observation.Source == "terminal" { + return assertions, fmt.Errorf("assertion %q uses untrusted source %q", expected.ID, observation.Source) + } + var problems Problems + actual := deriveObservationActual(result, observation, "collector."+expected.ID, &problems) + if err := problems.Err(); err != nil { + return assertions, err + } + assertion := Assertion{ID: expected.ID, Expected: cloneValueMap(expected.Expected), Actual: cloneValueMap(actual)} + assertion.Matched = reflect.DeepEqual(assertion.Expected, assertion.Actual) + assertions = append(assertions, assertion) + if !assertion.Matched { + return assertions, fmt.Errorf("assertion %q did not match canonical expectation", expected.ID) + } + delete(byID, expected.ID) + } + if len(byID) != 0 { + return assertions, fmt.Errorf("stage contains observations outside its canonical assertion set") + } + return assertions, nil +} + +func addStageEvidence(capture *Capture, observations []Observation) (string, error) { + evidence := Evidence{Schema: EvidenceSchema, ScrubStatus: "pass", Observations: cloneObservations(observations)} + return capture.AddJSON("common_collector", evidence) +} + +func setBlockedUnavailable(stage *StageResult, capture *Capture, scrub *ScrubRecord) { + stage.Verdict, stage.Outcome = "fail", "error" + stage.Error = &StageError{ + Code: "prerequisite.blocked_induction_unavailable", Message: "supported admitted-then-blocked evidence is unavailable", + Effect: "no_effect", Retry: Retry{Safe: false, Action: "add_supported_blocked_evidence"}, + } + stage.Assertions = []Assertion{{ + ID: "blocked.induction_available", Expected: map[string]any{"available": true}, + Actual: map[string]any{"available": false}, Matched: false, + }} + setGeneratedEvidence(stage, stage.Assertions[0], nil, capture, scrub) +} + +func setFailed(stage *StageResult, stageError *StageError, capture *Capture, scrub *ScrubRecord) { + stage.Verdict, stage.Outcome, stage.Error = "fail", "error", stageError + setFailureAssertion(stage, capture, scrub) +} + +func setFailureAssertion(stage *StageResult, capture *Capture, scrub *ScrubRecord) { + assertion := Assertion{ID: "stage.failure", Expected: map[string]any{"success": true}, Actual: map[string]any{"success": false}, Matched: false} + stage.Assertions = append(stage.Assertions, assertion) + setGeneratedEvidence(stage, assertion, nil, capture, scrub) +} + +func setNotReached(stage *StageResult, origin failureOrigin, capture *Capture, scrub *ScrubRecord) { + actual := map[string]any{ + "reached": false, "origin_sequence": float64(origin.Sequence), "origin_stage": origin.Stage, "origin_case": origin.Case, + } + assertion := Assertion{ID: "prerequisite.not_reached", Expected: map[string]any{"reached": true}, Actual: actual, Matched: false} + stage.Verdict, stage.Outcome = "fail", "error" + stage.Error = &StageError{ + Code: "prerequisite.not_reached", + Message: fmt.Sprintf("required stage was not reached after %s/%s checkpoint failed", origin.Case, origin.Stage), + Effect: "no_effect", Retry: Retry{Safe: false, Action: "inspect_originating_failure"}, + } + stage.Assertions = []Assertion{assertion} + setGeneratedEvidence(stage, assertion, &origin, capture, scrub) +} + +func setGeneratedEvidence(stage *StageResult, assertion Assertion, origin *failureOrigin, capture *Capture, scrub *ScrubRecord) { + checkpoint := map[string]any{ + "kind": "controller.checkpoint", "monotonic_ms": float64(stage.StartedOffsetMS), + "measurements": cloneValueMap(assertion.Actual), + } + if origin != nil { + checkpoint["origin_sequence"] = float64(origin.Sequence) + checkpoint["origin_stage"] = origin.Stage + checkpoint["origin_case"] = origin.Case + checkpoint["origin_evidence"] = append([]string(nil), origin.Evidence...) + } + ref, err := addStageEvidence(capture, []Observation{{ + Sequence: stage.Sequence, AssertionID: assertion.ID, Source: "controller", Evidence: checkpoint, + }}) + if err != nil { + scrub.Status = "fail" + scrub.Findings = append(scrub.Findings, err.Error()) + if len(stage.Evidence) == 0 { + stage.Evidence = append([]string(nil), originEvidence(origin)...) + } + return + } + stage.Evidence = append(stage.Evidence, ref) +} + +func originEvidence(origin *failureOrigin) []string { + if origin == nil { + return nil + } + return origin.Evidence +} + +func recordFailureAt(record RunRecord, index, stageCount int) error { + if index == stageCount-1 { + if record.CleanupError != nil { + return fmt.Errorf("common cleanup reported a failure") + } + } + return nil +} + +func collectorError(code, message string) *StageError { + return &StageError{Code: code, Message: message, Effect: "unknown_effect", Retry: Retry{Safe: false, Action: "inspect_originating_failure"}} +} + +func cloneStageError(in *StageError) *StageError { + if in == nil { + return nil + } + out := *in + return &out +} + +func cloneObservations(in []Observation) []Observation { + out := make([]Observation, len(in)) + for i, observation := range in { + out[i] = observation + out[i].Evidence = cloneValueMap(observation.Evidence) + } + return out +} + +func cloneValueMap(in map[string]any) map[string]any { + if in == nil { + return nil + } + b, err := json.Marshal(in) + if err != nil { + return nil + } + var out map[string]any + if err := json.Unmarshal(b, &out); err != nil { + return nil + } + return out +} + +func stringPointerValue(value string) *string { return &value } diff --git a/internal/conformance/portablelauncher/collector_test.go b/internal/conformance/portablelauncher/collector_test.go new file mode 100644 index 0000000..f8f988e --- /dev/null +++ b/internal/conformance/portablelauncher/collector_test.go @@ -0,0 +1,292 @@ +package portablelauncher + +import ( + "encoding/json" + "reflect" + "testing" +) + +func TestCollectorBuildsCanonicalCompleteOrderAndCleanupLast(t *testing.T) { + scenario := CanonicalScenario() + result, _ := CollectResult(collectorTestInput(t)) + + if len(result.Stages) != len(scenario.Steps) { + t.Fatalf("stage count = %d, want %d", len(result.Stages), len(scenario.Steps)) + } + cleanupCount := 0 + for i, step := range scenario.Steps { + got := result.Stages[i] + if got.Sequence != step.Sequence || got.Stage != step.Stage || got.Case != step.Case { + t.Fatalf("stage %d = %d/%s/%s, want %d/%s/%s", i, got.Sequence, got.Case, got.Stage, step.Sequence, step.Case, step.Stage) + } + if got.Stage == "cleanup" { + cleanupCount++ + } + } + last := result.Stages[len(result.Stages)-1] + if cleanupCount != 1 || last.Stage != "cleanup" || last.Case != "run" { + t.Fatalf("cleanup count/last = %d, %s/%s", cleanupCount, last.Case, last.Stage) + } +} + +func TestCollectorPropagatesFirstFailureWithoutSemanticSuccesses(t *testing.T) { + input := collectorTestInput(t) + failureIndex := 4 // happy/bind, independently selected from the manifest order. + input.Stages[failureIndex].Failure = &StageError{ + Code: "fixture.bind_failed", Message: "bind checkpoint failed", Effect: "no_effect", + Retry: Retry{Safe: false, Action: "inspect_bind"}, + } + + result, captured := CollectResult(input) + origin := result.Stages[failureIndex] + if origin.Verdict != "fail" || origin.Error == nil || origin.Error.Code != "fixture.bind_failed" { + t.Fatalf("origin = %#v", origin) + } + if len(origin.Evidence) != 2 { + t.Fatalf("origin evidence = %#v, want captured observations plus failure checkpoint", origin.Evidence) + } + assertCollectorObservation(t, captured, origin.Evidence[0], origin.Sequence, CanonicalScenario().Steps[failureIndex].Assertions[0].ID) + for i := failureIndex + 1; i < len(result.Stages); i++ { + stage := result.Stages[i] + if stage.Stage == "cleanup" { + continue + } + if stage.Case == "blocked" && stage.Stage == "launch" { + if stage.Error == nil || stage.Error.Code != "prerequisite.blocked_induction_unavailable" { + t.Fatalf("canonical blocked failure was not preserved: %#v", stage) + } + continue + } + if stage.Verdict != "fail" || stage.Outcome != "error" || stage.Error == nil || stage.Error.Code != "prerequisite.not_reached" { + t.Fatalf("stage %d did not fail closed: %#v", i, stage) + } + if len(stage.Assertions) != 1 || stage.Assertions[0].ID != "prerequisite.not_reached" || stage.Assertions[0].Matched { + t.Fatalf("stage %d fabricated or retained semantic assertions: %#v", i, stage.Assertions) + } + actual := stage.Assertions[0].Actual + wantOrigin := origin + if stage.Case == "blocked" { + blockedOrigin := result.Stages[collectorStageIndex("blocked", "launch")] + wantOrigin = blockedOrigin + } + if actual["origin_sequence"] != float64(wantOrigin.Sequence) || actual["origin_stage"] != wantOrigin.Stage || actual["origin_case"] != wantOrigin.Case { + t.Fatalf("stage %d origin = %#v", i, actual) + } + assertCollectorEvidence(t, captured, stage, "prerequisite.not_reached", actual) + } + last := result.Stages[len(result.Stages)-1] + if last.Stage != "cleanup" || last.Verdict != "pass" || last.Error != nil { + t.Fatalf("independently executed cleanup did not pass last: %#v", last) + } +} + +func TestCollectorMandatoryBlockedPrerequisitePreventsPass(t *testing.T) { + result, captured := CollectResult(collectorTestInput(t)) + index := collectorStageIndex("blocked", "launch") + stage := result.Stages[index] + + wantError := StageError{ + Code: "prerequisite.blocked_induction_unavailable", Message: "supported admitted-then-blocked evidence is unavailable", + Effect: "no_effect", Retry: Retry{Safe: false, Action: "add_supported_blocked_evidence"}, + } + if stage.Verdict != "fail" || stage.Outcome != "error" || stage.Error == nil || !reflect.DeepEqual(*stage.Error, wantError) { + t.Fatalf("blocked stage = %#v, want stable unavailable metadata %#v", stage, wantError) + } + if len(stage.Assertions) != 1 || stage.Assertions[0].ID != "blocked.induction_available" || stage.Assertions[0].Actual["available"] != false { + t.Fatalf("blocked assertion = %#v", stage.Assertions) + } + assertCollectorEvidence(t, captured, stage, "blocked.induction_available", map[string]any{"available": false}) + if result.Summary.Verdict != "fail" || result.Summary.FirstFailedStage == nil || *result.Summary.FirstFailedStage != "launch" || result.Summary.FirstFailedCase == nil || *result.Summary.FirstFailedCase != "blocked" { + t.Fatalf("summary = %#v", result.Summary) + } + for _, caseName := range []string{"exited", "timeout"} { + for i, step := range CanonicalScenario().Steps { + if step.Case == caseName && result.Stages[i].Verdict != "pass" { + t.Fatalf("case-local blocked prerequisite suppressed %s stage %d: %#v", caseName, i, result.Stages[i]) + } + } + } +} + +func TestCollectorRawLauncherEventsCannotOverrideAuthoritativeEvidence(t *testing.T) { + input := collectorTestInput(t) + input.Record.Capture.Events = []byte(`{"summary":{"verdict":"pass"},"stages":[{"verdict":"pass","outcome":"success"}]}`) + + // The independently captured setup measurement disagrees with the signed + // scenario. The expected value below comes from CanonicalScenario, not from + // collector output or the raw launcher event bytes above. + semantic := &input.Stages[0].Observations[1] + semantic.Evidence["measurements"].(map[string]any)["shared_state"] = true + wantExpected := CanonicalScenario().Steps[0].Assertions[1].Expected + + result, captured := CollectResult(input) + stage := result.Stages[0] + if stage.Verdict != "fail" || result.Summary.Verdict != "fail" { + t.Fatalf("launcher raw pass overrode common evidence: stage=%#v summary=%#v", stage, result.Summary) + } + if len(stage.Assertions) != 2 { + t.Fatalf("setup assertions = %#v", stage.Assertions) + } + got := stage.Assertions[1] + if !reflect.DeepEqual(got.Expected, wantExpected) || got.Actual["shared_state"] != true || got.Matched { + t.Fatalf("authoritative assertion = %#v, canonical expected = %#v", got, wantExpected) + } + assertCollectorEvidence(t, captured, stage, got.ID, got.Actual) + + for i := 1; i < len(result.Stages); i++ { + if result.Stages[i].Stage == "cleanup" { + continue + } + if result.Stages[i].Case == "blocked" && result.Stages[i].Stage == "launch" { + if result.Stages[i].Error == nil || result.Stages[i].Error.Code != "prerequisite.blocked_induction_unavailable" { + t.Fatalf("canonical blocked failure was not preserved: %#v", result.Stages[i]) + } + continue + } + if result.Stages[i].Verdict != "fail" || result.Stages[i].Assertions[0].ID != "prerequisite.not_reached" { + t.Fatalf("stage %d was turned into pass by raw launcher events: %#v", i, result.Stages[i]) + } + } +} + +func TestCollectorMissingCaptureFailsClosedWithCompleteSummary(t *testing.T) { + input := collectorTestInput(t) + input.Stages = input.Stages[:3] + input.Record.Capture.Events = []byte(`{"summary":{"verdict":"pass"}}`) + result, _ := CollectResult(input) + + want := CanonicalScenario().Steps[3] + first := result.Stages[3] + if first.Stage != want.Stage || first.Case != want.Case || first.Error == nil || first.Error.Code != "collector.stage_missing" { + t.Fatalf("first missing stage = %#v, want %s/%s", first, want.Case, want.Stage) + } + if result.Summary.FirstFailedStage == nil || *result.Summary.FirstFailedStage != want.Stage || result.Summary.FirstFailedCase == nil || *result.Summary.FirstFailedCase != want.Case { + t.Fatalf("summary = %#v", result.Summary) + } + if len(result.Stages) != len(CanonicalScenario().Steps) { + t.Fatalf("incomplete result has %d stages", len(result.Stages)) + } +} + +func collectorTestInput(t *testing.T) CollectorInput { + t.Helper() + scenario := CanonicalScenario() + pins := fixturePins() + input := CollectorInput{ + Run: RunIdentity{RunID: "collector-test", ObservedAt: "2026-09-18T00:00:00Z", HostOS: "linux", HostArch: "x86_64", FixtureRoot: "$RUN"}, + Pins: pins, + } + var offset int64 + for _, step := range scenario.Steps { + duration := int64(1) + if step.Case == "timeout" && step.Stage == "observe" { + duration = 20_000 + } + capture := CommonStageCapture{Sequence: step.Sequence, StartedOffsetMS: offset, DurationMS: duration} + for _, expected := range step.Assertions { + actual := collectorCloneMap(t, expected.Expected) + evidence := map[string]any{ + "kind": "controller.checkpoint", "monotonic_ms": float64(offset), "measurements": actual, + } + if expected.ID == "stage.completed_within_deadline" { + delete(evidence, "measurements") + deadline := step.DeadlineMS + if step.Case == "timeout" && step.Stage == "observe" { + deadline = CanonicalOracle().TimeoutMaximumMS + } + evidence["started_ms"] = float64(offset) + evidence["finished_ms"] = float64(offset + duration) + evidence["deadline_ms"] = float64(deadline) + } + if expected.ID == "setup.fixture_isolated_and_pinned" { + pinBytes, err := json.Marshal(pins) + if err != nil { + t.Fatal(err) + } + evidence["pins_digest"] = Digest(pinBytes) + evidence["credentials_present"] = true + evidence["credential_provider"] = "openai-codex" + evidence["credential_source_kind"] = "operator_copy" + evidence["pi_no_extensions"] = true + } + if expected.ID == "observe.exited" || expected.ID == "observe.timeout" { + evidence["target_verified"] = true + } + if expected.ID == "observe.timeout" { + evidence["timeout_probe_supported"] = true + } + capture.Observations = append(capture.Observations, Observation{ + Sequence: step.Sequence, AssertionID: expected.ID, Source: "controller", Evidence: evidence, + }) + } + input.Stages = append(input.Stages, capture) + offset += duration + } + return input +} + +func collectorStageIndex(caseName, stageName string) int { + for i, step := range CanonicalScenario().Steps { + if step.Case == caseName && step.Stage == stageName { + return i + } + } + return -1 +} + +func assertCollectorEvidence(t *testing.T, capture *Capture, stage StageResult, assertionID string, actual map[string]any) { + t.Helper() + if len(stage.Evidence) != 1 { + t.Fatalf("evidence refs = %#v", stage.Evidence) + } + b, ok := capture.Blob(stage.Evidence[0]) + if !ok { + t.Fatalf("missing captured blob %q", stage.Evidence[0]) + } + var evidence Evidence + if err := json.Unmarshal(b, &evidence); err != nil { + t.Fatal(err) + } + for _, observation := range evidence.Observations { + if observation.Sequence != stage.Sequence || observation.AssertionID != assertionID { + continue + } + measurements, _ := observation.Evidence["measurements"].(map[string]any) + if !reflect.DeepEqual(measurements, actual) { + t.Fatalf("captured measurements = %#v, want %#v", measurements, actual) + } + return + } + t.Fatalf("no observation for sequence %d assertion %q", stage.Sequence, assertionID) +} + +func assertCollectorObservation(t *testing.T, capture *Capture, reference string, sequence int, assertionID string) { + t.Helper() + b, ok := capture.Blob(reference) + if !ok { + t.Fatalf("missing captured blob %q", reference) + } + var evidence Evidence + if err := json.Unmarshal(b, &evidence); err != nil { + t.Fatal(err) + } + for _, observation := range evidence.Observations { + if observation.Sequence == sequence && observation.AssertionID == assertionID { + return + } + } + t.Fatalf("no preserved observation for sequence %d assertion %q", sequence, assertionID) +} + +func collectorCloneMap(t *testing.T, in map[string]any) map[string]any { + t.Helper() + b, err := json.Marshal(in) + if err != nil { + t.Fatal(err) + } + var out map[string]any + if err := json.Unmarshal(b, &out); err != nil { + t.Fatal(err) + } + return out +} diff --git a/internal/conformance/portablelauncher/command_capture_linux.go b/internal/conformance/portablelauncher/command_capture_linux.go new file mode 100644 index 0000000..dd62e3b --- /dev/null +++ b/internal/conformance/portablelauncher/command_capture_linux.go @@ -0,0 +1,499 @@ +//go:build linux + +package portablelauncher + +import ( + "bytes" + "crypto/sha256" + "encoding/hex" + "encoding/json" + "errors" + "fmt" + "io" + "os" + "path" + "sort" + "strconv" + "strings" + "syscall" + + "golang.org/x/sys/unix" +) + +const commandClock = "CLOCK_BOOTTIME" + +var commandCaptureFiles = []string{ + "argv.json", + "complete.json", + "metadata.json", + "started.json", + "stderr.raw", + "stdout.raw", +} + +// RecordedExecutable identifies the exact Duo executable used by a recorded +// invocation. SHA256 is the unprefixed, lowercase digest recorded by the +// command journal. +type RecordedExecutable struct { + Device uint64 + Inode uint64 + SHA256 string +} + +func recordedExecutableForPath(path string) (RecordedExecutable, error) { + executable, err := identifyCommandExecutable(path) + if err != nil { + return RecordedExecutable{}, err + } + return RecordedExecutable(executable), nil +} + +// RecordedCommand is one validated raw command fact. It deliberately carries +// no scenario, stage, case, assertion, verdict, environment, or host path. +type RecordedCommand struct { + InvocationID string + Arguments []string + Stdout []byte + Stderr []byte + WorkingDirectory string + StartOffsetNS int64 + EndOffsetNS int64 + RecorderPID int + ChildPID int + ChildProcStartTicks uint64 + Executable RecordedExecutable + ExitCode int + Signal int + SignalName string +} + +// LoadRecordedCommands validates and loads completed command journals from +// the exact capture directory. expectedExecutable must contain the pinned +// device, inode, and unprefixed lowercase SHA-256 identity. Results are sorted +// by start offset, then invocation identity; overlapping intervals are kept. +func LoadRecordedCommands(captureDirectory string, expectedRunOriginBootTimeNS int64, expectedExecutable RecordedExecutable) ([]RecordedCommand, error) { + if expectedRunOriginBootTimeNS <= 0 { + return nil, fmt.Errorf("expected command run origin must be positive") + } + if expectedExecutable.Device == 0 || expectedExecutable.Inode == 0 || !validCommandDigest(expectedExecutable.SHA256) { + return nil, fmt.Errorf("expected Duo executable identity is invalid") + } + captureDirectory, _, err := validateCommandCaptureDirectory(captureDirectory) + if err != nil { + return nil, err + } + + capture, err := openCommandCaptureDirectory(captureDirectory) + if err != nil { + return nil, err + } + defer func() { _ = capture.Close() }() + commands, missing, err := openCommandDirectoryAt(capture, "duo-commands") + if err != nil { + return nil, err + } + if missing { + return []RecordedCommand{}, nil + } + defer func() { _ = commands.Close() }() + if err := requireCommandDirectoryMode(commands, "command records directory"); err != nil { + return nil, err + } + + entries, err := commands.ReadDir(-1) + if err != nil { + return nil, fmt.Errorf("enumerate command records: %w", err) + } + records := make([]RecordedCommand, 0, len(entries)) + seen := make(map[string]struct{}, len(entries)) + for _, entry := range entries { + name := entry.Name() + if strings.HasSuffix(name, ".inflight") { + if entry.Type()&os.ModeSymlink != 0 { + return nil, fmt.Errorf("command records contain a symlink") + } + inflight, _, openErr := openCommandDirectoryAt(commands, name) + if openErr == nil { + _ = inflight.Close() + return nil, fmt.Errorf("command capture is incomplete") + } + return nil, fmt.Errorf("inflight command entry is not a safe directory") + } + invocationID, recorderPID, ok := parseCompletedInvocationName(name) + if !ok { + return nil, fmt.Errorf("unknown or malformed command record entry %q", name) + } + if _, duplicate := seen[invocationID]; duplicate { + return nil, fmt.Errorf("duplicate command invocation identity %q", invocationID) + } + seen[invocationID] = struct{}{} + invocation, missing, openErr := openCommandDirectoryAt(commands, name) + if openErr != nil || missing { + return nil, fmt.Errorf("completed command entry is not a safe directory") + } + if modeErr := requireCommandDirectoryMode(invocation, "completed command directory"); modeErr != nil { + _ = invocation.Close() + return nil, modeErr + } + record, loadErr := loadRecordedCommand(invocation, invocationID, recorderPID, expectedRunOriginBootTimeNS, expectedExecutable) + closeErr := invocation.Close() + if loadErr != nil { + return nil, fmt.Errorf("command %q: %w", invocationID, loadErr) + } + if closeErr != nil { + return nil, fmt.Errorf("close completed command directory: %w", closeErr) + } + records = append(records, record) + } + sort.Slice(records, func(i, j int) bool { + if records[i].StartOffsetNS != records[j].StartOffsetNS { + return records[i].StartOffsetNS < records[j].StartOffsetNS + } + return records[i].InvocationID < records[j].InvocationID + }) + return records, nil +} + +func openCommandCaptureDirectory(captureDirectory string) (*os.File, error) { + fd, err := unix.Open(captureDirectory, unix.O_RDONLY|unix.O_CLOEXEC|unix.O_DIRECTORY|unix.O_NOFOLLOW, 0) + if err != nil { + return nil, fmt.Errorf("open validated command capture directory: %w", err) + } + return os.NewFile(uintptr(fd), "command-capture"), nil +} + +func openCommandDirectoryAt(parent *os.File, name string) (*os.File, bool, error) { + fd, err := unix.Openat(int(parent.Fd()), name, unix.O_RDONLY|unix.O_CLOEXEC|unix.O_DIRECTORY|unix.O_NOFOLLOW, 0) + if errors.Is(err, unix.ENOENT) { + return nil, true, nil + } + if err != nil { + return nil, false, err + } + return os.NewFile(uintptr(fd), name), false, nil +} + +func requireCommandDirectoryMode(directory *os.File, label string) error { + info, err := directory.Stat() + if err != nil || !info.IsDir() || info.Mode().Perm() != 0o700 { + return fmt.Errorf("%s must be a mode-0700 directory", label) + } + return nil +} + +func parseCompletedInvocationName(name string) (string, int, bool) { + if !strings.HasSuffix(name, ".complete") { + return "", 0, false + } + identity := strings.TrimSuffix(name, ".complete") + parts := strings.Split(identity, "-") + if len(parts) != 3 || parts[0] != "invocation" || parts[1] == "" || parts[2] == "" { + return "", 0, false + } + pid, err := strconv.Atoi(parts[1]) + if err != nil || pid <= 0 || strconv.Itoa(pid) != parts[1] || len(parts[2]) != 32 || !lowerHex(parts[2]) { + return "", 0, false + } + return identity, pid, true +} + +func loadRecordedCommand(invocation *os.File, invocationID string, nameRecorderPID int, expectedOrigin int64, expectedExecutable RecordedExecutable) (RecordedCommand, error) { + entries, err := invocation.ReadDir(-1) + if err != nil { + return RecordedCommand{}, fmt.Errorf("enumerate invocation files: %w", err) + } + seen := make(map[string]struct{}, len(entries)) + for _, entry := range entries { + name := entry.Name() + if !containsCommandCaptureFile(name) { + return RecordedCommand{}, fmt.Errorf("unexpected invocation file %q", name) + } + if _, duplicate := seen[name]; duplicate { + return RecordedCommand{}, fmt.Errorf("duplicate invocation file %q", name) + } + seen[name] = struct{}{} + } + for _, name := range commandCaptureFiles { + if _, ok := seen[name]; !ok { + return RecordedCommand{}, fmt.Errorf("missing invocation file %q", name) + } + } + + metadataBytes, err := readCommandFileAt(invocation, "metadata.json") + if err != nil { + return RecordedCommand{}, err + } + argvBytes, err := readCommandFileAt(invocation, "argv.json") + if err != nil { + return RecordedCommand{}, err + } + startedBytes, err := readCommandFileAt(invocation, "started.json") + if err != nil { + return RecordedCommand{}, err + } + completeBytes, err := readCommandFileAt(invocation, "complete.json") + if err != nil { + return RecordedCommand{}, err + } + stdout, err := readCommandFileAt(invocation, "stdout.raw") + if err != nil { + return RecordedCommand{}, err + } + stderr, err := readCommandFileAt(invocation, "stderr.raw") + if err != nil { + return RecordedCommand{}, err + } + + var metadata commandMetadata + if err := decodeCommandJSON(metadataBytes, &metadata, + []string{"schema", "clock", "run_origin_boottime_ns", "start_offset_ns", "working_directory", "recorder_pid", "executable"}, + map[string][]string{"executable": {"device", "inode", "sha256"}}); err != nil { + return RecordedCommand{}, fmt.Errorf("decode metadata.json: %w", err) + } + var argv commandArgv + if err := decodeCommandJSON(argvBytes, &argv, + []string{"schema", "excludes_argv0", "arguments"}, nil); err != nil { + return RecordedCommand{}, fmt.Errorf("decode argv.json: %w", err) + } + var started commandStarted + if err := decodeCommandJSON(startedBytes, &started, + []string{"schema", "child_pid", "child_proc_start_ticks"}, nil); err != nil { + return RecordedCommand{}, fmt.Errorf("decode started.json: %w", err) + } + var complete commandComplete + if err := decodeCommandJSON(completeBytes, &complete, + []string{"schema", "clock", "run_origin_boottime_ns", "start_offset_ns", "end_offset_ns", "working_directory", "recorder_pid", "child_pid", "child_proc_start_ticks", "executable", "exit_code", "signal", "signal_name", "stdout", "stderr"}, + map[string][]string{ + "executable": {"device", "inode", "sha256"}, + "stdout": {"bytes", "sha256"}, + "stderr": {"bytes", "sha256"}, + }); err != nil { + return RecordedCommand{}, fmt.Errorf("decode complete.json: %w", err) + } + + if metadata.Schema != commandSchema || argv.Schema != commandSchema || started.Schema != commandSchema || complete.Schema != commandSchema { + return RecordedCommand{}, fmt.Errorf("command journal schema mismatch") + } + if metadata.Clock != commandClock || complete.Clock != commandClock { + return RecordedCommand{}, fmt.Errorf("command journal clock mismatch") + } + if metadata.RunOriginBootTimeNS != expectedOrigin || complete.RunOriginBootTimeNS != expectedOrigin { + return RecordedCommand{}, fmt.Errorf("command journal run origin mismatch") + } + if metadata.StartOffsetNS < 0 || complete.StartOffsetNS < 0 || complete.EndOffsetNS < 0 || complete.EndOffsetNS < metadata.StartOffsetNS { + return RecordedCommand{}, fmt.Errorf("command journal monotonic offsets are invalid") + } + if metadata.StartOffsetNS != complete.StartOffsetNS || metadata.WorkingDirectory != complete.WorkingDirectory || metadata.RecorderPID != complete.RecorderPID || metadata.Executable != complete.Executable { + return RecordedCommand{}, fmt.Errorf("metadata and completion facts disagree") + } + if !validCommandWorkingDirectory(metadata.WorkingDirectory) { + return RecordedCommand{}, fmt.Errorf("working directory is not a safe run token") + } + if metadata.RecorderPID <= 0 || metadata.RecorderPID != nameRecorderPID { + return RecordedCommand{}, fmt.Errorf("recorder process identity mismatch") + } + if started.ChildPID <= 0 || started.ChildPID == metadata.RecorderPID || started.ChildProcStartTicks == 0 || complete.ChildPID != started.ChildPID || complete.ChildProcStartTicks != started.ChildProcStartTicks { + return RecordedCommand{}, fmt.Errorf("child process identity mismatch") + } + journalExecutable := RecordedExecutable{Device: metadata.Executable.Device, Inode: metadata.Executable.Inode, SHA256: metadata.Executable.SHA256} + if journalExecutable.Device == 0 || journalExecutable.Inode == 0 || !validCommandDigest(journalExecutable.SHA256) || journalExecutable != expectedExecutable { + return RecordedCommand{}, fmt.Errorf("duo executable identity mismatch") + } + if !argv.ExcludesArgv0 { + return RecordedCommand{}, fmt.Errorf("argv record must exclude argv[0]") + } + if err := validateCommandExitStatus(complete); err != nil { + return RecordedCommand{}, err + } + if err := validateCommandStream("stdout", stdout, complete.Stdout); err != nil { + return RecordedCommand{}, err + } + if err := validateCommandStream("stderr", stderr, complete.Stderr); err != nil { + return RecordedCommand{}, err + } + + return RecordedCommand{ + InvocationID: invocationID, Arguments: append([]string(nil), argv.Arguments...), + Stdout: append([]byte(nil), stdout...), Stderr: append([]byte(nil), stderr...), + WorkingDirectory: metadata.WorkingDirectory, + StartOffsetNS: metadata.StartOffsetNS, EndOffsetNS: complete.EndOffsetNS, + RecorderPID: metadata.RecorderPID, ChildPID: started.ChildPID, ChildProcStartTicks: started.ChildProcStartTicks, + Executable: journalExecutable, ExitCode: complete.ExitCode, Signal: complete.Signal, SignalName: complete.SignalName, + }, nil +} + +func containsCommandCaptureFile(name string) bool { + index := sort.SearchStrings(commandCaptureFiles, name) + return index < len(commandCaptureFiles) && commandCaptureFiles[index] == name +} + +func readCommandFileAt(directory *os.File, name string) ([]byte, error) { + fd, err := unix.Openat(int(directory.Fd()), name, unix.O_RDONLY|unix.O_CLOEXEC|unix.O_NOFOLLOW, 0) + if err != nil { + return nil, fmt.Errorf("open invocation file %q: %w", name, err) + } + file := os.NewFile(uintptr(fd), name) + info, statErr := file.Stat() + if statErr != nil || !info.Mode().IsRegular() || info.Mode().Perm() != 0o600 { + _ = file.Close() + return nil, fmt.Errorf("invocation file %q must be a mode-0600 regular file", name) + } + data, readErr := io.ReadAll(file) + closeErr := file.Close() + if err := errors.Join(readErr, closeErr); err != nil { + return nil, fmt.Errorf("read invocation file %q: %w", name, err) + } + return data, nil +} + +func decodeCommandJSON(data []byte, target any, required []string, nested map[string][]string) error { + if err := rejectDuplicateJSONFields(data); err != nil { + return err + } + decoder := json.NewDecoder(bytes.NewReader(data)) + decoder.DisallowUnknownFields() + if err := decoder.Decode(target); err != nil { + return err + } + var trailing any + if err := decoder.Decode(&trailing); !errors.Is(err, io.EOF) { + if err == nil { + return fmt.Errorf("trailing JSON value") + } + return fmt.Errorf("trailing JSON: %w", err) + } + var object map[string]json.RawMessage + if err := json.Unmarshal(data, &object); err != nil || object == nil { + return fmt.Errorf("JSON record must be an object") //nolint:staticcheck // JSON is the proper initialism. + } + if err := requireCommandJSONFields(object, required); err != nil { + return err + } + for field, fields := range nested { + var child map[string]json.RawMessage + if err := json.Unmarshal(object[field], &child); err != nil || child == nil { + return fmt.Errorf("field %q must be an object", field) + } + if err := requireCommandJSONFields(child, fields); err != nil { + return fmt.Errorf("field %q: %w", field, err) + } + } + return nil +} + +func requireCommandJSONFields(object map[string]json.RawMessage, required []string) error { + for _, field := range required { + if _, ok := object[field]; !ok { + return fmt.Errorf("missing required field %q", field) + } + } + return nil +} + +func rejectDuplicateJSONFields(data []byte) error { + decoder := json.NewDecoder(bytes.NewReader(data)) + decoder.UseNumber() + if err := walkCommandJSONValue(decoder); err != nil { + return err + } + var trailing any + if err := decoder.Decode(&trailing); !errors.Is(err, io.EOF) { + return fmt.Errorf("trailing JSON") + } + return nil +} + +func walkCommandJSONValue(decoder *json.Decoder) error { + token, err := decoder.Token() + if err != nil { + return err + } + delimiter, ok := token.(json.Delim) + if !ok { + return nil + } + switch delimiter { + case '{': + seen := map[string]struct{}{} + for decoder.More() { + keyToken, err := decoder.Token() + if err != nil { + return err + } + key, ok := keyToken.(string) + if !ok { + return fmt.Errorf("JSON object key is not a string") //nolint:staticcheck // JSON is the proper initialism. + } + if _, duplicate := seen[key]; duplicate { + return fmt.Errorf("duplicate JSON field %q", key) + } + seen[key] = struct{}{} + if err := walkCommandJSONValue(decoder); err != nil { + return err + } + } + _, err = decoder.Token() + return err + case '[': + for decoder.More() { + if err := walkCommandJSONValue(decoder); err != nil { + return err + } + } + _, err = decoder.Token() + return err + default: + return fmt.Errorf("unexpected JSON delimiter") + } +} + +func validCommandWorkingDirectory(value string) bool { + if value == "$RUN" { + return true + } + if !strings.HasPrefix(value, "$RUN/") || strings.Contains(value, "\\") || strings.ContainsRune(value, '\x00') { + return false + } + relative := strings.TrimPrefix(value, "$RUN/") + return relative != "" && path.Clean(relative) == relative && !path.IsAbs(relative) && relative != ".." && !strings.HasPrefix(relative, "../") +} + +func validateCommandExitStatus(complete commandComplete) error { + if complete.ExitCode < 0 || complete.ExitCode > 255 { + return fmt.Errorf("command exit code is invalid") + } + if complete.Signal == 0 { + if complete.SignalName != "" { + return fmt.Errorf("unsignaled command has a signal name") + } + return nil + } + if complete.Signal < 1 || complete.Signal > 64 || complete.ExitCode != 128+complete.Signal || complete.SignalName != syscall.Signal(complete.Signal).String() { + return fmt.Errorf("command signal status is inconsistent") + } + return nil +} + +func validateCommandStream(name string, data []byte, result commandStreamResult) error { + if result.Bytes < 0 || result.Bytes != int64(len(data)) || !validCommandDigest(result.SHA256) { + return fmt.Errorf("%s byte count or digest is invalid", name) + } + sum := sha256.Sum256(data) + if result.SHA256 != hex.EncodeToString(sum[:]) { + return fmt.Errorf("%s digest mismatch", name) + } + return nil +} + +func validCommandDigest(value string) bool { + return len(value) == sha256.Size*2 && lowerHex(value) +} + +func lowerHex(value string) bool { + for _, char := range value { + if (char < '0' || char > '9') && (char < 'a' || char > 'f') { + return false + } + } + return true +} diff --git a/internal/conformance/portablelauncher/command_capture_linux_test.go b/internal/conformance/portablelauncher/command_capture_linux_test.go new file mode 100644 index 0000000..56f45d5 --- /dev/null +++ b/internal/conformance/portablelauncher/command_capture_linux_test.go @@ -0,0 +1,396 @@ +//go:build linux + +package portablelauncher + +import ( + "bytes" + "encoding/json" + "os" + "path/filepath" + "reflect" + "strings" + "testing" +) + +type commandCaptureFixture struct { + capture string + origin int64 + executable RecordedExecutable + directory string + metadata commandMetadata + argv commandArgv + started commandStarted + complete commandComplete + stdout []byte + stderr []byte +} + +func TestLoadRecordedCommandsValidExactAndDefensive(t *testing.T) { + fixture := newCommandCaptureFixture(t, "invocation-41-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 50, 90) + got, err := LoadRecordedCommands(fixture.capture, fixture.origin, fixture.executable) + if err != nil { + t.Fatal(err) + } + if len(got) != 1 { + t.Fatalf("records = %d, want 1", len(got)) + } + want := RecordedCommand{ + InvocationID: "invocation-41-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + Arguments: []string{"session", "launch", "arg with spaces"}, + Stdout: []byte("stdout\x00\xff"), Stderr: []byte("stderr\n"), + WorkingDirectory: "$RUN/workspace", StartOffsetNS: 50, EndOffsetNS: 90, + RecorderPID: 41, ChildPID: 52, ChildProcStartTicks: 700, + Executable: fixture.executable, ExitCode: 17, + } + if !reflect.DeepEqual(got[0], want) { + t.Fatalf("record = %#v, want %#v", got[0], want) + } + + got[0].Arguments[0] = "changed" + got[0].Stdout[0] = 'X' + got[0].Stderr[0] = 'Y' + again, err := LoadRecordedCommands(fixture.capture, fixture.origin, fixture.executable) + if err != nil { + t.Fatal(err) + } + if !reflect.DeepEqual(again[0].Arguments, want.Arguments) || !bytes.Equal(again[0].Stdout, want.Stdout) || !bytes.Equal(again[0].Stderr, want.Stderr) { + t.Fatalf("returned slices alias loader state: %#v", again[0]) + } +} + +func TestLoadRecordedCommandsOrdersOverlappingFactsByStartThenInvocation(t *testing.T) { + first := newCommandCaptureFixture(t, "invocation-41-cccccccccccccccccccccccccccccccc", 30, 100) + writeCommandCaptureFixture(t, first) + second := first + second.directory = filepath.Join(first.capture, "duo-commands", "invocation-42-bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb.complete") + second.metadata.RecorderPID, second.complete.RecorderPID = 42, 42 + second.metadata.StartOffsetNS, second.complete.StartOffsetNS, second.complete.EndOffsetNS = 10, 10, 80 + second.started.ChildPID, second.complete.ChildPID = 53, 53 + writeCommandCaptureFixture(t, second) + third := first + third.directory = filepath.Join(first.capture, "duo-commands", "invocation-43-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.complete") + third.metadata.RecorderPID, third.complete.RecorderPID = 43, 43 + third.metadata.StartOffsetNS, third.complete.StartOffsetNS, third.complete.EndOffsetNS = 10, 10, 70 + third.started.ChildPID, third.complete.ChildPID = 54, 54 + writeCommandCaptureFixture(t, third) + + got, err := LoadRecordedCommands(first.capture, first.origin, first.executable) + if err != nil { + t.Fatal(err) + } + want := []string{ + "invocation-42-bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", + "invocation-43-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + "invocation-41-cccccccccccccccccccccccccccccccc", + } + identities := make([]string, len(got)) + for i := range got { + identities[i] = got[i].InvocationID + } + if !reflect.DeepEqual(identities, want) || got[0].EndOffsetNS <= got[1].StartOffsetNS || got[1].EndOffsetNS <= got[2].StartOffsetNS { + t.Fatalf("ordered overlapping records = %v", identities) + } +} + +func TestLoadRecordedCommandsMissingAndEmptyJournal(t *testing.T) { + _, capture, _, origin := newCommandRun(t) + executable := RecordedExecutable{Device: 1, Inode: 2, SHA256: strings.Repeat("a", 64)} + for _, test := range []struct { + name string + setup func() + }{ + {name: "missing"}, + {name: "empty", setup: func() { + mustMkdirCommandCapture(t, filepath.Join(capture, "duo-commands"), 0o700) + }}, + } { + t.Run(test.name, func(t *testing.T) { + if test.setup != nil { + test.setup() + } + got, err := LoadRecordedCommands(capture, origin, executable) + if err != nil || got == nil || len(got) != 0 { + t.Fatalf("empty load = %#v, %v", got, err) + } + }) + } +} + +func TestLoadRecordedCommandsRejectsUnsafeDirectoryEntries(t *testing.T) { + tests := []struct { + name string + mutate func(*testing.T, commandCaptureFixture) + }{ + {name: "inflight", mutate: func(t *testing.T, f commandCaptureFixture) { + mustMkdirCommandCapture(t, filepath.Join(f.capture, "duo-commands", "invocation-99-dddddddddddddddddddddddddddddddd.inflight"), 0o700) + }}, + {name: "journal symlink", mutate: func(t *testing.T, f commandCaptureFixture) { + if err := os.Symlink(f.directory, filepath.Join(f.capture, "duo-commands", "invocation-99-dddddddddddddddddddddddddddddddd.complete")); err != nil { + t.Fatal(err) + } + }}, + {name: "unknown", mutate: func(t *testing.T, f commandCaptureFixture) { + mustWriteCommandCapture(t, filepath.Join(f.capture, "duo-commands", "README"), []byte("no"), 0o600) + }}, + {name: "non-directory", mutate: func(t *testing.T, f commandCaptureFixture) { + mustWriteCommandCapture(t, filepath.Join(f.capture, "duo-commands", "invocation-99-dddddddddddddddddddddddddddddddd.complete"), []byte("no"), 0o600) + }}, + {name: "malformed name", mutate: func(t *testing.T, f commandCaptureFixture) { + mustMkdirCommandCapture(t, filepath.Join(f.capture, "duo-commands", "invocation-099-dddddddddddddddddddddddddddddddd.complete"), 0o700) + }}, + {name: "missing file", mutate: func(t *testing.T, f commandCaptureFixture) { + if err := os.Remove(filepath.Join(f.directory, "started.json")); err != nil { + t.Fatal(err) + } + }}, + {name: "extra file", mutate: func(t *testing.T, f commandCaptureFixture) { + mustWriteCommandCapture(t, filepath.Join(f.directory, "environment.json"), []byte("{}\n"), 0o600) + }}, + {name: "duplicate file copy", mutate: func(t *testing.T, f commandCaptureFixture) { + data, err := os.ReadFile(filepath.Join(f.directory, "metadata.json")) + if err != nil { + t.Fatal(err) + } + mustWriteCommandCapture(t, filepath.Join(f.directory, "metadata-copy.json"), data, 0o600) + }}, + {name: "file symlink", mutate: func(t *testing.T, f commandCaptureFixture) { + if err := os.Remove(filepath.Join(f.directory, "argv.json")); err != nil { + t.Fatal(err) + } + if err := os.Symlink("metadata.json", filepath.Join(f.directory, "argv.json")); err != nil { + t.Fatal(err) + } + }}, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + fixture := newCommandCaptureFixture(t, "invocation-41-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 50, 90) + test.mutate(t, fixture) + assertCommandCaptureLoadFails(t, fixture) + }) + } +} + +func TestLoadRecordedCommandsRejectsStrictJSONViolations(t *testing.T) { + for _, test := range []struct { + name string + data []byte + }{ + {name: "unknown", data: []byte(`{"schema":"duo-portable-launcher-command-v1","child_pid":52,"child_proc_start_ticks":700,"unknown":true}`)}, + {name: "trailing", data: []byte(`{"schema":"duo-portable-launcher-command-v1","child_pid":52,"child_proc_start_ticks":700} {}`)}, + {name: "missing field", data: []byte(`{"schema":"duo-portable-launcher-command-v1","child_pid":52}`)}, + {name: "duplicate field", data: []byte(`{"schema":"duo-portable-launcher-command-v1","child_pid":52,"child_pid":52,"child_proc_start_ticks":700}`)}, + } { + t.Run(test.name, func(t *testing.T) { + fixture := newCommandCaptureFixture(t, "invocation-41-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 50, 90) + data := bytes.ReplaceAll(test.data, []byte{'\\', '"'}, []byte{'"'}) + mustWriteCommandCapture(t, filepath.Join(fixture.directory, "started.json"), data, 0o600) + assertCommandCaptureLoadFails(t, fixture) + }) + } +} + +func TestLoadRecordedCommandsRejectsFactMismatches(t *testing.T) { + tests := []struct { + name string + mutate func(*commandCaptureFixture) + }{ + {name: "metadata schema", mutate: func(f *commandCaptureFixture) { f.metadata.Schema = "other" }}, + {name: "argv schema", mutate: func(f *commandCaptureFixture) { f.argv.Schema = "other" }}, + {name: "started schema", mutate: func(f *commandCaptureFixture) { f.started.Schema = "other" }}, + {name: "complete schema", mutate: func(f *commandCaptureFixture) { f.complete.Schema = "other" }}, + {name: "clock", mutate: func(f *commandCaptureFixture) { f.complete.Clock = "CLOCK_MONOTONIC" }}, + {name: "origin", mutate: func(f *commandCaptureFixture) { f.metadata.RunOriginBootTimeNS++ }}, + {name: "negative start", mutate: func(f *commandCaptureFixture) { f.metadata.StartOffsetNS, f.complete.StartOffsetNS = -1, -1 }}, + {name: "end before start", mutate: func(f *commandCaptureFixture) { f.complete.EndOffsetNS = f.complete.StartOffsetNS - 1 }}, + {name: "metadata cwd", mutate: func(f *commandCaptureFixture) { f.complete.WorkingDirectory = "$RUN/other" }}, + {name: "unsafe cwd", mutate: func(f *commandCaptureFixture) { + f.metadata.WorkingDirectory, f.complete.WorkingDirectory = "/host/private", "/host/private" + }}, + {name: "recorder pid", mutate: func(f *commandCaptureFixture) { f.metadata.RecorderPID, f.complete.RecorderPID = 42, 42 }}, + {name: "child pid", mutate: func(f *commandCaptureFixture) { f.complete.ChildPID++ }}, + {name: "child ticks", mutate: func(f *commandCaptureFixture) { f.started.ChildProcStartTicks = 0 }}, + {name: "executable metadata", mutate: func(f *commandCaptureFixture) { f.complete.Executable.Inode++ }}, + {name: "executable device absent", mutate: func(f *commandCaptureFixture) { + f.metadata.Executable.Device, f.complete.Executable.Device, f.executable.Device = 0, 0, 0 + }}, + {name: "executable pin", mutate: func(f *commandCaptureFixture) { f.executable.Inode++ }}, + {name: "uppercase digest", mutate: func(f *commandCaptureFixture) { + f.metadata.Executable.SHA256 = strings.ToUpper(f.metadata.Executable.SHA256) + f.complete.Executable.SHA256 = f.metadata.Executable.SHA256 + }}, + {name: "argv includes zero", mutate: func(f *commandCaptureFixture) { f.argv.ExcludesArgv0 = false }}, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + fixture := newCommandCaptureFixture(t, "invocation-41-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 50, 90) + test.mutate(&fixture) + writeCommandCaptureFixture(t, fixture) + assertCommandCaptureLoadFails(t, fixture) + }) + } +} + +func TestLoadRecordedCommandsRejectsExitAndSignalInconsistency(t *testing.T) { + for _, test := range []struct { + name string + mutate func(*commandComplete) + }{ + {name: "negative exit", mutate: func(c *commandComplete) { c.ExitCode = -1 }}, + {name: "unsignaled name", mutate: func(c *commandComplete) { c.SignalName = "terminated" }}, + {name: "signal exit", mutate: func(c *commandComplete) { c.Signal, c.SignalName = 15, "terminated" }}, + {name: "signal name", mutate: func(c *commandComplete) { c.ExitCode, c.Signal, c.SignalName = 143, 15, "SIGTERM" }}, + {name: "signal range", mutate: func(c *commandComplete) { c.ExitCode, c.Signal, c.SignalName = 200, 72, "signal 72" }}, + } { + t.Run(test.name, func(t *testing.T) { + fixture := newCommandCaptureFixture(t, "invocation-41-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 50, 90) + test.mutate(&fixture.complete) + writeCommandCaptureFixture(t, fixture) + assertCommandCaptureLoadFails(t, fixture) + }) + } +} + +func TestLoadRecordedCommandsRejectsStreamTampering(t *testing.T) { + for _, test := range []struct { + name string + mutate func(*commandCaptureFixture) + }{ + {name: "stdout bytes", mutate: func(f *commandCaptureFixture) { f.complete.Stdout.Bytes++ }}, + {name: "stdout digest", mutate: func(f *commandCaptureFixture) { f.complete.Stdout.SHA256 = strings.Repeat("0", 64) }}, + {name: "stdout raw", mutate: func(f *commandCaptureFixture) { f.stdout = append(f.stdout, '!') }}, + {name: "stderr bytes", mutate: func(f *commandCaptureFixture) { f.complete.Stderr.Bytes-- }}, + {name: "stderr digest", mutate: func(f *commandCaptureFixture) { f.complete.Stderr.SHA256 = strings.Repeat("A", 64) }}, + {name: "stderr raw", mutate: func(f *commandCaptureFixture) { f.stderr[0] = 'X' }}, + } { + t.Run(test.name, func(t *testing.T) { + fixture := newCommandCaptureFixture(t, "invocation-41-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 50, 90) + test.mutate(&fixture) + writeCommandCaptureFixture(t, fixture) + assertCommandCaptureLoadFails(t, fixture) + }) + } +} + +func TestLoadRecordedCommandsRejectsUnsafeModes(t *testing.T) { + for _, test := range []struct { + name string + mutate func(*testing.T, commandCaptureFixture) + }{ + {name: "commands directory", mutate: func(t *testing.T, f commandCaptureFixture) { + mustChmodCommandCapture(t, filepath.Dir(f.directory), 0o755) + }}, + {name: "invocation directory", mutate: func(t *testing.T, f commandCaptureFixture) { mustChmodCommandCapture(t, f.directory, 0o750) }}, + {name: "record file", mutate: func(t *testing.T, f commandCaptureFixture) { + mustChmodCommandCapture(t, filepath.Join(f.directory, "metadata.json"), 0o644) + }}, + } { + t.Run(test.name, func(t *testing.T) { + fixture := newCommandCaptureFixture(t, "invocation-41-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 50, 90) + test.mutate(t, fixture) + assertCommandCaptureLoadFails(t, fixture) + }) + } +} + +func TestRecordedCommandExposesNoHostAbsolutePath(t *testing.T) { + fixture := newCommandCaptureFixture(t, "invocation-41-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 50, 90) + got, err := LoadRecordedCommands(fixture.capture, fixture.origin, fixture.executable) + if err != nil { + t.Fatal(err) + } + encoded, err := json.Marshal(got) + if err != nil { + t.Fatal(err) + } + root := filepath.Dir(fixture.capture) + if bytes.Contains(encoded, []byte(root)) || filepath.IsAbs(got[0].WorkingDirectory) { + t.Fatalf("record exposes host path: %s", encoded) + } +} + +func newCommandCaptureFixture(t *testing.T, identity string, start, end int64) commandCaptureFixture { + t.Helper() + _, capture, _, origin := newCommandRun(t) + commands := filepath.Join(capture, "duo-commands") + mustMkdirCommandCapture(t, commands, 0o700) + executable := RecordedExecutable{Device: 11, Inode: 12, SHA256: strings.Repeat("a", 64)} + stdout := []byte("stdout\x00\xff") + stderr := []byte("stderr\n") + fixture := commandCaptureFixture{ + capture: capture, origin: origin, executable: executable, + directory: filepath.Join(commands, identity+".complete"), + metadata: commandMetadata{ + Schema: commandSchema, Clock: commandClock, RunOriginBootTimeNS: origin, + StartOffsetNS: start, WorkingDirectory: "$RUN/workspace", RecorderPID: 41, + Executable: commandExecutable(executable), + }, + argv: commandArgv{Schema: commandSchema, ExcludesArgv0: true, Arguments: []string{"session", "launch", "arg with spaces"}}, + started: commandStarted{Schema: commandSchema, ChildPID: 52, ChildProcStartTicks: 700}, + stdout: stdout, stderr: stderr, + } + fixture.complete = commandComplete{ + Schema: commandSchema, Clock: commandClock, RunOriginBootTimeNS: origin, + StartOffsetNS: start, EndOffsetNS: end, WorkingDirectory: fixture.metadata.WorkingDirectory, + RecorderPID: 41, ChildPID: fixture.started.ChildPID, ChildProcStartTicks: fixture.started.ChildProcStartTicks, + Executable: fixture.metadata.Executable, ExitCode: 17, + Stdout: commandStreamResult{Bytes: int64(len(stdout)), SHA256: Digest(stdout)[len("sha256:"):]}, + Stderr: commandStreamResult{Bytes: int64(len(stderr)), SHA256: Digest(stderr)[len("sha256:"):]}, + } + writeCommandCaptureFixture(t, fixture) + return fixture +} + +func writeCommandCaptureFixture(t *testing.T, fixture commandCaptureFixture) { + t.Helper() + if _, err := os.Stat(fixture.directory); err == nil { + if err := os.RemoveAll(fixture.directory); err != nil { + t.Fatal(err) + } + } + mustMkdirCommandCapture(t, fixture.directory, 0o700) + for name, value := range map[string]any{ + "metadata.json": fixture.metadata, + "argv.json": fixture.argv, + "started.json": fixture.started, + "complete.json": fixture.complete, + } { + data, err := json.Marshal(value) + if err != nil { + t.Fatal(err) + } + mustWriteCommandCapture(t, filepath.Join(fixture.directory, name), append(data, '\n'), 0o600) + } + mustWriteCommandCapture(t, filepath.Join(fixture.directory, "stdout.raw"), fixture.stdout, 0o600) + mustWriteCommandCapture(t, filepath.Join(fixture.directory, "stderr.raw"), fixture.stderr, 0o600) +} + +func assertCommandCaptureLoadFails(t *testing.T, fixture commandCaptureFixture) { + t.Helper() + if got, err := LoadRecordedCommands(fixture.capture, fixture.origin, fixture.executable); err == nil { + t.Fatalf("LoadRecordedCommands unexpectedly succeeded: %#v", got) + } +} + +func mustMkdirCommandCapture(t *testing.T, path string, mode os.FileMode) { + t.Helper() + if err := os.Mkdir(path, mode); err != nil { + t.Fatal(err) + } + mustChmodCommandCapture(t, path, mode) +} + +func mustWriteCommandCapture(t *testing.T, path string, data []byte, mode os.FileMode) { + t.Helper() + if err := os.WriteFile(path, data, mode); err != nil { + t.Fatal(err) + } + mustChmodCommandCapture(t, path, mode) +} + +func mustChmodCommandCapture(t *testing.T, path string, mode os.FileMode) { + t.Helper() + if err := os.Chmod(path, mode); err != nil { + t.Fatal(err) + } +} diff --git a/internal/conformance/portablelauncher/command_record_linux.go b/internal/conformance/portablelauncher/command_record_linux.go new file mode 100644 index 0000000..96e5161 --- /dev/null +++ b/internal/conformance/portablelauncher/command_record_linux.go @@ -0,0 +1,713 @@ +//go:build linux + +package portablelauncher + +import ( + "crypto/rand" + "crypto/sha256" + "encoding/binary" + "encoding/hex" + "encoding/json" + "errors" + "fmt" + "io" + "os" + "os/exec" + "os/signal" + "path/filepath" + "strconv" + "strings" + "sync" + "syscall" + + "golang.org/x/sys/unix" +) + +const ( + // CommandCaptureDirectoryEnv explicitly enables process-level Duo command + // capture. Its value must be the capture directory of a private portable + // launcher run root. + CommandCaptureDirectoryEnv = "DUO_CONFORMANCE_COMMAND_CAPTURE_DIR" + // CommandRunOriginEnv is the suite controller's CLOCK_BOOTTIME origin in + // nanoseconds. All command offsets are measured from this common origin. + CommandRunOriginEnv = "DUO_CONFORMANCE_RUN_ORIGIN_BOOTTIME_NS" + + commandBypassEnv = "DUO_CONFORMANCE_COMMAND_BYPASS" + commandRecorderFail = 125 + commandSchema = "duo-portable-launcher-command-v1" + bypassFD = 3 + bypassMagic = "DUO-COMMAND-BYPASS-V1\x00" +) + +// commandMetadata and the related records are deliberately concrete and +// versioned. A later assembler can decode them without interpreting CLI text. +// Arguments exclude argv[0], which is documented explicitly in commandArgv. +type commandMetadata struct { + Schema string `json:"schema"` + Clock string `json:"clock"` + RunOriginBootTimeNS int64 `json:"run_origin_boottime_ns"` + StartOffsetNS int64 `json:"start_offset_ns"` + WorkingDirectory string `json:"working_directory"` + RecorderPID int `json:"recorder_pid"` + Executable commandExecutable `json:"executable"` +} + +type commandExecutable struct { + Device uint64 `json:"device"` + Inode uint64 `json:"inode"` + SHA256 string `json:"sha256"` +} + +type commandArgv struct { + Schema string `json:"schema"` + ExcludesArgv0 bool `json:"excludes_argv0"` + Arguments []string `json:"arguments"` +} + +type commandStarted struct { + Schema string `json:"schema"` + ChildPID int `json:"child_pid"` + ChildProcStartTicks uint64 `json:"child_proc_start_ticks"` +} + +type commandStreamResult struct { + Bytes int64 `json:"bytes"` + SHA256 string `json:"sha256"` +} + +type commandComplete struct { + Schema string `json:"schema"` + Clock string `json:"clock"` + RunOriginBootTimeNS int64 `json:"run_origin_boottime_ns"` + StartOffsetNS int64 `json:"start_offset_ns"` + EndOffsetNS int64 `json:"end_offset_ns"` + WorkingDirectory string `json:"working_directory"` + RecorderPID int `json:"recorder_pid"` + ChildPID int `json:"child_pid"` + ChildProcStartTicks uint64 `json:"child_proc_start_ticks"` + Executable commandExecutable `json:"executable"` + ExitCode int `json:"exit_code"` + Signal int `json:"signal"` + SignalName string `json:"signal_name"` + Stdout commandStreamResult `json:"stdout"` + Stderr commandStreamResult `json:"stderr"` +} + +type commandRecorderInvocation struct { + args []string + env []string + cwd string + stdin io.Reader + stdout io.Writer + stderr io.Writer + exe string + pid int +} + +type commandRecorderConfig struct { + runRoot string + captureDir string + originNS int64 + startNS int64 + cwdToken string + executable commandExecutable +} + +// RecordCommandIfConfigured is the Linux CLI entry seam. With neither +// recorder variable configured it returns immediately, leaving main's normal +// iostreams.System -> cli.NewRootCommand -> cli.Execute path untouched. +func RecordCommandIfConfigured() (recorded bool, exitCode int) { + environment := os.Environ() + if !recorderEnvironmentPresent(environment) { + return false, 0 + } + cwd, err := os.Getwd() + if err != nil { + _, _ = fmt.Fprintf(os.Stderr, "duo command recorder: determine cwd: %v\n", err) + return true, commandRecorderFail + } + recorded, exitCode = recordCommand(commandRecorderInvocation{ + args: os.Args, env: environment, cwd: cwd, + stdin: os.Stdin, stdout: os.Stdout, stderr: os.Stderr, + exe: "/proc/self/exe", pid: os.Getpid(), + }) + if !recorded { + _ = os.Unsetenv(commandBypassEnv) + } + return recorded, exitCode +} + +func recordCommand(in commandRecorderInvocation) (bool, int) { + if consumeCommandBypass(in.env, in.exe) { + return false, 0 + } + if !recorderEnvironmentPresent(in.env) { + return false, 0 + } + fail := func(err error) (bool, int) { + _, _ = fmt.Fprintf(in.stderr, "duo command recorder: %v\n", err) + return true, commandRecorderFail + } + config, err := loadCommandRecorderConfig(in) + if err != nil { + return fail(err) + } + code, err := executeRecordedCommand(in, config) + if err != nil { + return fail(err) + } + return true, code +} + +func recorderEnvironmentPresent(env []string) bool { + return envValue(env, CommandCaptureDirectoryEnv) != "" || envValue(env, CommandRunOriginEnv) != "" +} + +func loadCommandRecorderConfig(in commandRecorderInvocation) (commandRecorderConfig, error) { + capture := envValue(in.env, CommandCaptureDirectoryEnv) + originValue := envValue(in.env, CommandRunOriginEnv) + if capture == "" || originValue == "" { + return commandRecorderConfig{}, fmt.Errorf("capture directory and common run origin must both be configured") + } + origin, err := strconv.ParseInt(originValue, 10, 64) + if err != nil || origin <= 0 { + return commandRecorderConfig{}, fmt.Errorf("common run origin must be a positive CLOCK_BOOTTIME nanosecond value") + } + now, err := bootTimeNanoseconds() + if err != nil { + return commandRecorderConfig{}, fmt.Errorf("read CLOCK_BOOTTIME: %w", err) + } + if origin > now { + return commandRecorderConfig{}, fmt.Errorf("common run origin is after recorder start") + } + + capture, runRoot, err := validateCommandCaptureDirectory(capture) + if err != nil { + return commandRecorderConfig{}, err + } + cwd, err := filepath.Abs(in.cwd) + if err != nil { + return commandRecorderConfig{}, fmt.Errorf("resolve working directory: %w", err) + } + resolvedCWD, err := filepath.EvalSymlinks(cwd) + if err != nil || filepath.Clean(resolvedCWD) != filepath.Clean(cwd) || !within(runRoot, resolvedCWD) { + return commandRecorderConfig{}, fmt.Errorf("working directory must resolve without symlinks below the run root") + } + rel, err := filepath.Rel(runRoot, resolvedCWD) + if err != nil || rel == ".." || strings.HasPrefix(rel, ".."+string(filepath.Separator)) { + return commandRecorderConfig{}, fmt.Errorf("tokenize working directory below run root") + } + cwdToken := "$RUN" + if rel != "." { + cwdToken += "/" + filepath.ToSlash(rel) + } + executable, err := identifyCommandExecutable(in.exe) + if err != nil { + return commandRecorderConfig{}, err + } + return commandRecorderConfig{ + runRoot: runRoot, captureDir: capture, originNS: origin, + startNS: now - origin, cwdToken: cwdToken, executable: executable, + }, nil +} + +func validateCommandCaptureDirectory(path string) (string, string, error) { + abs, err := filepath.Abs(path) + if err != nil { + return "", "", fmt.Errorf("resolve command capture directory: %w", err) + } + if filepath.Clean(abs) != abs { + return "", "", fmt.Errorf("command capture directory must be a clean absolute path") + } + info, err := os.Lstat(abs) + if err != nil || !info.IsDir() || info.Mode()&os.ModeSymlink != 0 || info.Mode().Perm()&0o077 != 0 { + return "", "", fmt.Errorf("command capture directory must exist as a private non-symlink directory") + } + resolved, err := filepath.EvalSymlinks(abs) + if err != nil || filepath.Clean(resolved) != abs { + return "", "", fmt.Errorf("command capture directory contains a symlink component") + } + + for candidate := filepath.Dir(abs); ; candidate = filepath.Dir(candidate) { + if strings.HasPrefix(filepath.Base(candidate), "duo-portable-launcher-") { + root, rootErr := validatePrivateTempDir(candidate) + if rootErr != nil || !within(root, abs) { + return "", "", fmt.Errorf("command capture directory is not below a validated private run root") + } + if abs != filepath.Join(root, "capture") { + return "", "", fmt.Errorf("command capture directory must be the run root capture directory") + } + return abs, root, nil + } + parent := filepath.Dir(candidate) + if parent == candidate { + break + } + } + return "", "", fmt.Errorf("command capture directory has no duo-portable-launcher run root") +} + +func identifyCommandExecutable(path string) (commandExecutable, error) { + f, err := os.Open(path) + if err != nil { + return commandExecutable{}, fmt.Errorf("open recorder executable: %w", err) + } + defer func() { _ = f.Close() }() + info, err := f.Stat() + if err != nil || !info.Mode().IsRegular() { + return commandExecutable{}, fmt.Errorf("recorder executable is not a regular file") + } + stat, ok := info.Sys().(*syscall.Stat_t) + if !ok { + return commandExecutable{}, fmt.Errorf("recorder executable has no Linux stat identity") + } + hash := sha256.New() + if _, err := io.Copy(hash, f); err != nil { + return commandExecutable{}, fmt.Errorf("hash recorder executable: %w", err) + } + return commandExecutable{Device: uint64(stat.Dev), Inode: stat.Ino, SHA256: hex.EncodeToString(hash.Sum(nil))}, nil +} + +func executeRecordedCommand(in commandRecorderInvocation, config commandRecorderConfig) (exitCode int, resultErr error) { + commandsDir := filepath.Join(config.captureDir, "duo-commands") + if err := ensurePrivateCommandDirectory(commandsDir); err != nil { + return 0, err + } + name, err := randomCommandInvocationName(in.pid) + if err != nil { + return 0, err + } + inflight := filepath.Join(commandsDir, name+".inflight") + complete := filepath.Join(commandsDir, name+".complete") + if err := os.Mkdir(inflight, 0o700); err != nil { + return 0, fmt.Errorf("create invocation directory: %w", err) + } + if err := os.Chmod(inflight, 0o700); err != nil { + _ = os.Remove(inflight) + return 0, fmt.Errorf("set invocation directory mode: %w", err) + } + removeBeforeStart := true + defer func() { + if resultErr != nil && removeBeforeStart { + resultErr = errors.Join(resultErr, os.RemoveAll(inflight)) + } + }() + + metadata := commandMetadata{ + Schema: commandSchema, Clock: "CLOCK_BOOTTIME", + RunOriginBootTimeNS: config.originNS, StartOffsetNS: config.startNS, + WorkingDirectory: config.cwdToken, RecorderPID: in.pid, Executable: config.executable, + } + argv := commandArgv{Schema: commandSchema, ExcludesArgv0: true, Arguments: append([]string(nil), in.args[1:]...)} + if err := writeCommandJSON(filepath.Join(inflight, "metadata.json"), metadata); err != nil { + return 0, err + } + if err := writeCommandJSON(filepath.Join(inflight, "argv.json"), argv); err != nil { + return 0, err + } + stdoutFile, err := openCommandFile(filepath.Join(inflight, "stdout.raw")) + if err != nil { + return 0, err + } + defer func() { _ = stdoutFile.Close() }() + stderrFile, err := openCommandFile(filepath.Join(inflight, "stderr.raw")) + if err != nil { + return 0, err + } + defer func() { _ = stderrFile.Close() }() + + capRead, capWrite, marker, err := newCommandBypassCapability(in.pid) + if err != nil { + return 0, err + } + defer func() { _ = capRead.Close() }() + childEnv := setEnv(in.env, commandBypassEnv, marker) + cmd := exec.Command(in.exe, in.args[1:]...) + cmd.Args[0] = in.args[0] + cmd.Dir = in.cwd + cmd.Env = childEnv + cmd.Stdin = in.stdin + cmd.ExtraFiles = []*os.File{capRead} + cmd.SysProcAttr = &syscall.SysProcAttr{Pdeathsig: syscall.SIGKILL} + + var killOnce sync.Once + var forwardingErrMu sync.Mutex + var forwardingErr error + killOnError := func(err error) { + forwardingErrMu.Lock() + forwardingErr = errors.Join(forwardingErr, err) + forwardingErrMu.Unlock() + killOnce.Do(func() { + if cmd.Process != nil { + _ = cmd.Process.Kill() + } + }) + } + stdoutCapture := newCommandStreamWriter(stdoutFile, in.stdout, killOnError) + stderrCapture := newCommandStreamWriter(stderrFile, in.stderr, killOnError) + cmd.Stdout = stdoutCapture + cmd.Stderr = stderrCapture + if err := cmd.Start(); err != nil { + _ = capWrite.Close() + return 0, fmt.Errorf("start inner Duo CLI: %w", err) + } + if err := capWrite.Close(); err != nil { + _ = cmd.Process.Kill() + _ = cmd.Wait() + return 0, fmt.Errorf("close bypass capability: %w", err) + } + _ = capRead.Close() + childTicks, err := readCommandProcStartTicks(cmd.Process.Pid) + if err != nil { + _ = cmd.Process.Kill() + _ = cmd.Wait() + return 0, err + } + started := commandStarted{Schema: commandSchema, ChildPID: cmd.Process.Pid, ChildProcStartTicks: childTicks} + if err := writeCommandJSON(filepath.Join(inflight, "started.json"), started); err != nil { + _ = cmd.Process.Kill() + _ = cmd.Wait() + return 0, err + } + removeBeforeStart = false + + forwardSignals := make(chan os.Signal, 8) + signal.Notify(forwardSignals, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGQUIT) + signalDone := make(chan struct{}) + go func() { + defer close(signalDone) + for sig := range forwardSignals { + if cmd.Process != nil { + _ = cmd.Process.Signal(sig) + } + } + }() + waitErr := cmd.Wait() + signal.Stop(forwardSignals) + close(forwardSignals) + <-signalDone + + forwardingErrMu.Lock() + streamErr := forwardingErr + forwardingErrMu.Unlock() + if streamErr != nil { + return 0, fmt.Errorf("capture and forward child streams: %w", streamErr) + } + exitCode, signalNumber, signalName, err := commandExitStatus(cmd.ProcessState, waitErr) + if err != nil { + return 0, err + } + end, err := bootTimeNanoseconds() + if err != nil || end < config.originNS+config.startNS { + return 0, fmt.Errorf("read monotonic command completion") + } + if err := stdoutFile.Sync(); err != nil { + return 0, fmt.Errorf("sync stdout capture: %w", err) + } + if err := stderrFile.Sync(); err != nil { + return 0, fmt.Errorf("sync stderr capture: %w", err) + } + stdoutResult, err := stdoutCapture.result() + if err != nil { + return 0, fmt.Errorf("finalize stdout capture: %w", err) + } + stderrResult, err := stderrCapture.result() + if err != nil { + return 0, fmt.Errorf("finalize stderr capture: %w", err) + } + if err := stdoutFile.Close(); err != nil { + return 0, fmt.Errorf("close stdout capture: %w", err) + } + if err := stderrFile.Close(); err != nil { + return 0, fmt.Errorf("close stderr capture: %w", err) + } + completion := commandComplete{ + Schema: commandSchema, Clock: "CLOCK_BOOTTIME", + RunOriginBootTimeNS: config.originNS, StartOffsetNS: config.startNS, EndOffsetNS: end - config.originNS, + WorkingDirectory: config.cwdToken, RecorderPID: in.pid, + ChildPID: cmd.Process.Pid, ChildProcStartTicks: childTicks, Executable: config.executable, + ExitCode: exitCode, Signal: signalNumber, SignalName: signalName, + Stdout: stdoutResult, Stderr: stderrResult, + } + if err := writeCommandJSON(filepath.Join(inflight, "complete.json"), completion); err != nil { + return 0, err + } + if err := syncCommandDirectory(inflight); err != nil { + return 0, err + } + if err := unix.Renameat2(unix.AT_FDCWD, inflight, unix.AT_FDCWD, complete, unix.RENAME_NOREPLACE); err != nil { + return 0, fmt.Errorf("publish completed invocation: %w", err) + } + if err := syncCommandDirectory(commandsDir); err != nil { + return 0, err + } + return exitCode, nil +} + +func ensurePrivateCommandDirectory(path string) error { + err := os.Mkdir(path, 0o700) + if err != nil && !errors.Is(err, os.ErrExist) { + return fmt.Errorf("create command records directory: %w", err) + } + if err == nil { + if chmodErr := os.Chmod(path, 0o700); chmodErr != nil { + return fmt.Errorf("set command records directory mode: %w", chmodErr) + } + } + info, err := os.Lstat(path) + if err != nil || !info.IsDir() || info.Mode()&os.ModeSymlink != 0 || info.Mode().Perm() != 0o700 { + return fmt.Errorf("command records directory must be a mode-0700 non-symlink directory") + } + return nil +} + +func randomCommandInvocationName(pid int) (string, error) { + random := make([]byte, 16) + if _, err := io.ReadFull(rand.Reader, random); err != nil { + return "", fmt.Errorf("generate invocation identity: %w", err) + } + return fmt.Sprintf("invocation-%d-%s", pid, hex.EncodeToString(random)), nil +} + +func openCommandFile(path string) (*os.File, error) { + f, err := os.OpenFile(path, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0o600) + if err != nil { + return nil, fmt.Errorf("create command record %s: %w", filepath.Base(path), err) + } + if err := f.Chmod(0o600); err != nil { + _ = f.Close() + return nil, err + } + return f, nil +} + +func writeCommandJSON(path string, value any) error { + f, err := openCommandFile(path) + if err != nil { + return err + } + encoder := json.NewEncoder(f) + encoder.SetEscapeHTML(false) + writeErr := encoder.Encode(value) + syncErr := f.Sync() + closeErr := f.Close() + if err := errors.Join(writeErr, syncErr, closeErr); err != nil { + return fmt.Errorf("write command record %s: %w", filepath.Base(path), err) + } + return nil +} + +func syncCommandDirectory(path string) error { + dir, err := os.Open(path) + if err != nil { + return err + } + return errors.Join(dir.Sync(), dir.Close()) +} + +type commandStreamWriter struct { + capture *os.File + forward io.Writer + bytes int64 + onError func(error) + mu sync.Mutex +} + +func newCommandStreamWriter(capture *os.File, forward io.Writer, onError func(error)) *commandStreamWriter { + return &commandStreamWriter{capture: capture, forward: forward, onError: onError} +} + +func (w *commandStreamWriter) Write(p []byte) (int, error) { + w.mu.Lock() + defer w.mu.Unlock() + n, err := w.capture.Write(p) + if err == nil && n != len(p) { + err = io.ErrShortWrite + } + if err != nil { + w.onError(fmt.Errorf("write raw capture: %w", err)) + return n, err + } + w.bytes += int64(len(p)) + // Re-hashing the complete file at result time avoids mutable hash state in + // metadata and keeps the raw stream itself authoritative. + n, err = w.forward.Write(p) + if err == nil && n != len(p) { + err = io.ErrShortWrite + } + if err != nil { + w.onError(fmt.Errorf("forward raw stream: %w", err)) + return n, err + } + return len(p), nil +} + +func (w *commandStreamWriter) result() (commandStreamResult, error) { + w.mu.Lock() + defer w.mu.Unlock() + // Capture files are write-only, so compute the stream digest from a second + // descriptor after all writes have completed. + b, err := os.ReadFile(w.capture.Name()) + if err != nil { + return commandStreamResult{}, fmt.Errorf("read completed stream capture: %w", err) + } + if int64(len(b)) != w.bytes { + return commandStreamResult{}, fmt.Errorf("stream capture byte count changed") + } + sum := sha256.Sum256(b) + return commandStreamResult{Bytes: w.bytes, SHA256: hex.EncodeToString(sum[:])}, nil +} + +func commandExitStatus(state *os.ProcessState, waitErr error) (int, int, string, error) { + if state == nil { + return 0, 0, "", fmt.Errorf("inner Duo CLI has no process state") + } + status, ok := state.Sys().(syscall.WaitStatus) + if !ok { + return 0, 0, "", fmt.Errorf("inner Duo CLI has no Linux wait status") + } + if status.Signaled() { + sig := status.Signal() + return 128 + int(sig), int(sig), sig.String(), nil + } + if status.Exited() { + code := status.ExitStatus() + if waitErr == nil || isExitError(waitErr) { + return code, 0, "", nil + } + } + return 0, 0, "", fmt.Errorf("wait for inner Duo CLI: %w", waitErr) +} + +func isExitError(err error) bool { + var exitErr *exec.ExitError + return errors.As(err, &exitErr) +} + +func readCommandProcStartTicks(pid int) (uint64, error) { + raw, err := os.ReadFile("/proc/" + strconv.Itoa(pid) + "/stat") + if err != nil { + return 0, fmt.Errorf("read child process start ticks: %w", err) + } + end := strings.LastIndexByte(string(raw), ')') + if end < 0 || end+2 >= len(raw) { + return 0, fmt.Errorf("child process stat is malformed") + } + fields := strings.Fields(string(raw[end+2:])) + const startTimeIndex = 19 + if len(fields) <= startTimeIndex { + return 0, fmt.Errorf("child process stat is incomplete") + } + ticks, err := strconv.ParseUint(fields[startTimeIndex], 10, 64) + if err != nil || ticks == 0 { + return 0, fmt.Errorf("child process start ticks are invalid") + } + return ticks, nil +} + +func bootTimeNanoseconds() (int64, error) { + var value unix.Timespec + if err := unix.ClockGettime(unix.CLOCK_BOOTTIME, &value); err != nil { + return 0, err + } + return value.Nano(), nil +} + +func newCommandBypassCapability(parentPID int) (*os.File, *os.File, string, error) { + read, write, err := os.Pipe() + if err != nil { + return nil, nil, "", fmt.Errorf("create bypass capability: %w", err) + } + token := make([]byte, 32) + if _, err := io.ReadFull(rand.Reader, token); err != nil { + _ = read.Close() + _ = write.Close() + return nil, nil, "", err + } + payload := make([]byte, len(bypassMagic)+8+len(token)) + copy(payload, bypassMagic) + binary.BigEndian.PutUint64(payload[len(bypassMagic):], uint64(parentPID)) + copy(payload[len(bypassMagic)+8:], token) + if _, err := write.Write(payload); err != nil { + _ = read.Close() + _ = write.Close() + return nil, nil, "", fmt.Errorf("write bypass capability: %w", err) + } + digest := sha256.Sum256(payload) + marker := strconv.Itoa(bypassFD) + ":" + hex.EncodeToString(digest[:]) + return read, write, marker, nil +} + +func consumeCommandBypass(env []string, selfExe string) bool { + marker := envValue(env, commandBypassEnv) + parts := strings.Split(marker, ":") + if len(parts) != 2 || parts[0] != strconv.Itoa(bypassFD) { + return false + } + wantDigest, err := hex.DecodeString(parts[1]) + if err != nil || len(wantDigest) != sha256.Size { + return false + } + f := os.NewFile(uintptr(bypassFD), "duo-command-bypass") + if f == nil { + return false + } + defer func() { _ = f.Close() }() + info, err := f.Stat() + if err != nil || info.Mode()&os.ModeNamedPipe == 0 { + return false + } + payload, err := io.ReadAll(io.LimitReader(f, int64(len(bypassMagic)+8+32+1))) + if err != nil || len(payload) != len(bypassMagic)+8+32 || string(payload[:len(bypassMagic)]) != bypassMagic { + return false + } + digest := sha256.Sum256(payload) + if !equalBytes(digest[:], wantDigest) { + return false + } + parentPID := int(binary.BigEndian.Uint64(payload[len(bypassMagic):])) + if parentPID <= 0 || parentPID != os.Getppid() { + return false + } + self, err := os.Stat(selfExe) + if err != nil { + return false + } + parent, err := os.Stat("/proc/" + strconv.Itoa(parentPID) + "/exe") + if err != nil || !os.SameFile(self, parent) { + return false + } + return true +} + +func equalBytes(a, b []byte) bool { + if len(a) != len(b) { + return false + } + var different byte + for i := range a { + different |= a[i] ^ b[i] + } + return different == 0 +} + +func envValue(env []string, key string) string { + prefix := key + "=" + for i := len(env) - 1; i >= 0; i-- { + if strings.HasPrefix(env[i], prefix) { + return strings.TrimPrefix(env[i], prefix) + } + } + return "" +} + +func setEnv(env []string, key, value string) []string { + prefix := key + "=" + out := make([]string, 0, len(env)+1) + for _, entry := range env { + if !strings.HasPrefix(entry, prefix) { + out = append(out, entry) + } + } + return append(out, prefix+value) +} diff --git a/internal/conformance/portablelauncher/command_record_linux_test.go b/internal/conformance/portablelauncher/command_record_linux_test.go new file mode 100644 index 0000000..b976dbe --- /dev/null +++ b/internal/conformance/portablelauncher/command_record_linux_test.go @@ -0,0 +1,451 @@ +//go:build linux + +package portablelauncher + +import ( + "bytes" + "crypto/sha256" + "encoding/hex" + "encoding/json" + "errors" + "io" + "os" + "path/filepath" + "reflect" + "sort" + "strconv" + "strings" + "sync" + "syscall" + "testing" +) + +const commandRecorderHelperEnv = "DUO_COMMAND_RECORDER_TEST_HELPER" + +func TestCommandRecorderHelper(_ *testing.T) { + if os.Getenv(commandRecorderHelperEnv) != "1" { + return + } + cwd, err := os.Getwd() + if err != nil { + os.Exit(119) + } + handled, code := recordCommand(commandRecorderInvocation{ + args: os.Args, env: os.Environ(), cwd: cwd, + stdin: os.Stdin, stdout: os.Stdout, stderr: os.Stderr, + exe: "/proc/self/exe", pid: os.Getpid(), + }) + if handled { + os.Exit(code) + } + separator := -1 + for i, arg := range os.Args { + if arg == "--" { + separator = i + break + } + } + if separator < 0 || separator+1 >= len(os.Args) { + os.Exit(118) + } + switch os.Args[separator+1] { + case "streams": + input, readErr := io.ReadAll(os.Stdin) + if readErr != nil { + os.Exit(117) + } + _, _ = os.Stdout.Write(append([]byte("stdout:"), input...)) + _, _ = os.Stderr.Write([]byte("stderr:\x00\xff\n")) + os.Exit(0) + case "exit": + code, _ := strconv.Atoi(os.Args[separator+2]) + os.Exit(code) + case "signal": + _ = syscall.Kill(os.Getpid(), syscall.SIGTERM) + select {} + case "quiet": + os.Exit(0) + default: + os.Exit(116) + } +} + +func TestCommandRecorderDisabledDoesNotHandleExecution(t *testing.T) { + in := commandTestInvocation(t, nil, "quiet") + in.env = removeCommandRecorderEnv(os.Environ()) + handled, code := recordCommand(in) + if handled || code != 0 { + t.Fatalf("recordCommand disabled = (%v, %d), want (false, 0)", handled, code) + } + if _, err := os.Stat(filepath.Join(in.cwd, "..", "capture", "duo-commands")); !errors.Is(err, os.ErrNotExist) { + t.Fatalf("disabled recorder touched capture: %v", err) + } +} + +func TestRecordedCommandForwardsAndCapturesIndependentStreamsAndStdin(t *testing.T) { + input := []byte("input\x00bytes\n") + in := commandTestInvocation(t, input, "streams", "arg with spaces", "literal-byte-marker") + var stdout, stderr bytes.Buffer + in.stdout, in.stderr = &stdout, &stderr + handled, code := recordCommand(in) + if !handled || code != 0 { + t.Fatalf("recordCommand = (%v, %d), stderr %q", handled, code, stderr.String()) + } + wantStdout := append([]byte("stdout:"), input...) + wantStderr := []byte("stderr:\x00\xff\n") + if !bytes.Equal(stdout.Bytes(), wantStdout) || !bytes.Equal(stderr.Bytes(), wantStderr) { + t.Fatalf("forwarded streams stdout=%q stderr=%q", stdout.Bytes(), stderr.Bytes()) + } + + dir := onlyCompletedCommand(t, captureForInvocation(t, in)) + assertFileBytes(t, filepath.Join(dir, "stdout.raw"), wantStdout) + assertFileBytes(t, filepath.Join(dir, "stderr.raw"), wantStderr) + var argv commandArgv + readCommandJSON(t, filepath.Join(dir, "argv.json"), &argv) + if !argv.ExcludesArgv0 || !reflect.DeepEqual(argv.Arguments, in.args[1:]) { + t.Fatalf("argv record = %#v, want %#v", argv, in.args[1:]) + } + var complete commandComplete + readCommandJSON(t, filepath.Join(dir, "complete.json"), &complete) + assertCommandStreamResult(t, complete.Stdout, wantStdout) + assertCommandStreamResult(t, complete.Stderr, wantStderr) +} + +func TestRecordedCommandMetadataAndNoEnvironmentValues(t *testing.T) { + in := commandTestInvocation(t, nil, "quiet", "exact-argument") + secret := "environment-value-must-not-appear-9f7ce8" + in.env = append(in.env, "RECORDER_SENTINEL="+secret) + before, err := bootTimeNanoseconds() + if err != nil { + t.Fatal(err) + } + handled, code := recordCommand(in) + after, afterErr := bootTimeNanoseconds() + if afterErr != nil { + t.Fatal(afterErr) + } + if !handled || code != 0 { + t.Fatalf("recordCommand = (%v, %d)", handled, code) + } + dir := onlyCompletedCommand(t, captureForInvocation(t, in)) + var metadata commandMetadata + readCommandJSON(t, filepath.Join(dir, "metadata.json"), &metadata) + var started commandStarted + readCommandJSON(t, filepath.Join(dir, "started.json"), &started) + var complete commandComplete + readCommandJSON(t, filepath.Join(dir, "complete.json"), &complete) + origin, _ := strconv.ParseInt(envValue(in.env, CommandRunOriginEnv), 10, 64) + if metadata.Schema != commandSchema || metadata.Clock != "CLOCK_BOOTTIME" || metadata.RunOriginBootTimeNS != origin { + t.Fatalf("metadata identity = %#v", metadata) + } + if metadata.WorkingDirectory != "$RUN/workspace" || metadata.RecorderPID != in.pid { + t.Fatalf("metadata process/path = %#v", metadata) + } + if metadata.Executable.Device == 0 || metadata.Executable.Inode == 0 || len(metadata.Executable.SHA256) != 64 { + t.Fatalf("metadata executable = %#v", metadata.Executable) + } + if started.ChildPID <= 0 || started.ChildProcStartTicks == 0 || complete.ChildPID != started.ChildPID || complete.ChildProcStartTicks != started.ChildProcStartTicks { + t.Fatalf("start/complete process identity = %#v / %#v", started, complete) + } + if metadata.StartOffsetNS < before-origin || metadata.StartOffsetNS > after-origin || complete.EndOffsetNS < metadata.StartOffsetNS || complete.EndOffsetNS > after-origin { + t.Fatalf("monotonic offsets start=%d end=%d bounds=[%d,%d]", metadata.StartOffsetNS, complete.EndOffsetNS, before-origin, after-origin) + } + for _, name := range []string{"metadata.json", "argv.json", "started.json", "complete.json"} { + data, readErr := os.ReadFile(filepath.Join(dir, name)) + if readErr != nil { + t.Fatal(readErr) + } + if bytes.Contains(data, []byte(secret)) || bytes.Contains(data, []byte("RECORDER_SENTINEL")) { + t.Fatalf("%s recorded environment content", name) + } + } + assertPrivateCommandModes(t, dir) +} + +func TestRecordedCommandReturnsNonzeroAndSignalExit(t *testing.T) { + t.Run("nonzero", func(t *testing.T) { + in := commandTestInvocation(t, nil, "exit", "37") + handled, code := recordCommand(in) + if !handled || code != 37 { + t.Fatalf("recordCommand = (%v, %d), want exit 37", handled, code) + } + var complete commandComplete + readCommandJSON(t, filepath.Join(onlyCompletedCommand(t, captureForInvocation(t, in)), "complete.json"), &complete) + if complete.ExitCode != 37 || complete.Signal != 0 || complete.SignalName != "" { + t.Fatalf("completion status = %#v", complete) + } + }) + t.Run("signal", func(t *testing.T) { + in := commandTestInvocation(t, nil, "signal") + handled, code := recordCommand(in) + if !handled || code != 128+int(syscall.SIGTERM) { + t.Fatalf("recordCommand = (%v, %d), want signal exit", handled, code) + } + var complete commandComplete + readCommandJSON(t, filepath.Join(onlyCompletedCommand(t, captureForInvocation(t, in)), "complete.json"), &complete) + if complete.Signal != int(syscall.SIGTERM) || complete.SignalName != syscall.SIGTERM.String() { + t.Fatalf("completion signal = %#v", complete) + } + }) +} + +func TestCommandRecorderConcurrentInvocationsUseUniqueRecords(t *testing.T) { + _, capture, workspace, origin := newCommandRun(t) + const count = 8 + var wg sync.WaitGroup + errorsCh := make(chan string, count) + for i := 0; i < count; i++ { + wg.Add(1) + go func(i int) { + defer wg.Done() + in := newCommandTestInvocation(capture, workspace, origin, nil, "exit", strconv.Itoa(i%3)) + handled, code := recordCommand(in) + if !handled || code != i%3 { + errorsCh <- "unexpected concurrent result" + } + }(i) + } + wg.Wait() + close(errorsCh) + for message := range errorsCh { + t.Error(message) + } + entries := commandRecordEntries(t, capture) + if len(entries) != count { + t.Fatalf("record count = %d, want %d (%v)", len(entries), count, entries) + } + seen := make(map[string]bool, count) + for _, entry := range entries { + if filepath.Ext(entry) != ".complete" || seen[entry] { + t.Fatalf("non-unique or incomplete concurrent record %q", entry) + } + seen[entry] = true + } +} + +func TestCommandRecorderMarkerWithoutCapabilityCannotBypass(t *testing.T) { + in := commandTestInvocation(t, nil, "quiet") + in.env = setEnv(in.env, commandBypassEnv, "spoofed-marker") + handled, code := recordCommand(in) + if !handled || code != 0 { + t.Fatalf("spoofed marker result = (%v, %d)", handled, code) + } + if len(commandRecordEntries(t, captureForInvocation(t, in))) != 1 { + t.Fatal("spoofed marker bypassed command recording") + } +} + +func TestCommandRecorderPreservesStartedIncompleteRecord(t *testing.T) { + in := commandTestInvocation(t, nil, "streams") + in.stdout = failingCommandWriter{} + var stderr bytes.Buffer + in.stderr = &stderr + handled, code := recordCommand(in) + if !handled || code != commandRecorderFail { + t.Fatalf("stream failure result = (%v, %d), stderr=%q", handled, code, stderr.String()) + } + entries := commandRecordEntries(t, captureForInvocation(t, in)) + if len(entries) != 1 || filepath.Ext(entries[0]) != ".inflight" { + t.Fatalf("incomplete entries = %v", entries) + } + dir := filepath.Join(captureForInvocation(t, in), "duo-commands", entries[0]) + if _, err := os.Stat(filepath.Join(dir, "started.json")); err != nil { + t.Fatalf("incomplete record has no usable start record: %v", err) + } + if _, err := os.Stat(filepath.Join(dir, "complete.json")); !errors.Is(err, os.ErrNotExist) { + t.Fatalf("incomplete record has completion: %v", err) + } +} + +func TestCommandRecorderRefusesSymlinkAndPathEscape(t *testing.T) { + _, capture, workspace, origin := newCommandRun(t) + outside := t.TempDir() + link := filepath.Join(filepath.Dir(capture), "capture-link") + if err := os.Symlink(outside, link); err != nil { + t.Fatal(err) + } + tests := []struct { + name string + capture string + cwd string + }{ + {name: "capture symlink", capture: link, cwd: workspace}, + {name: "cwd escape", capture: capture, cwd: outside}, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + in := newCommandTestInvocation(test.capture, test.cwd, origin, nil, "quiet") + var stderr bytes.Buffer + in.stderr = &stderr + handled, code := recordCommand(in) + if !handled || code != commandRecorderFail || stderr.Len() == 0 { + t.Fatalf("unsafe path result = (%v, %d), stderr=%q", handled, code, stderr.String()) + } + }) + } + if entries := commandRecordEntries(t, capture); len(entries) != 0 { + t.Fatalf("unsafe path ran child or wrote record: %v", entries) + } +} + +type failingCommandWriter struct{} + +func (failingCommandWriter) Write([]byte) (int, error) { + return 0, errors.New("injected forwarding failure") +} + +func commandTestInvocation(t *testing.T, input []byte, helperArgs ...string) commandRecorderInvocation { + t.Helper() + _, capture, workspace, origin := newCommandRun(t) + return newCommandTestInvocation(capture, workspace, origin, input, helperArgs...) +} + +func newCommandTestInvocation(capture, workspace string, origin int64, input []byte, helperArgs ...string) commandRecorderInvocation { + args := []string{"duo-test", "-test.run=^TestCommandRecorderHelper$", "--"} + args = append(args, helperArgs...) + env := removeCommandRecorderEnv(os.Environ()) + env = append(env, + commandRecorderHelperEnv+"=1", + CommandCaptureDirectoryEnv+"="+capture, + CommandRunOriginEnv+"="+strconv.FormatInt(origin, 10), + ) + return commandRecorderInvocation{ + args: args, env: env, cwd: workspace, stdin: bytes.NewReader(input), + stdout: io.Discard, stderr: io.Discard, exe: "/proc/self/exe", pid: os.Getpid(), + } +} + +func newCommandRun(t *testing.T) (root, capture, workspace string, origin int64) { + t.Helper() + base := t.TempDir() + root, err := os.MkdirTemp(base, "duo-portable-launcher-") + if err != nil { + t.Fatal(err) + } + if err := os.Chmod(root, 0o700); err != nil { + t.Fatal(err) + } + capture = filepath.Join(root, "capture") + workspace = filepath.Join(root, "workspace") + for _, dir := range []string{capture, workspace} { + if err := os.Mkdir(dir, 0o700); err != nil { + t.Fatal(err) + } + } + now, err := bootTimeNanoseconds() + if err != nil { + t.Fatal(err) + } + return root, capture, workspace, now - 1_000_000 +} + +func removeCommandRecorderEnv(env []string) []string { + keys := []string{CommandCaptureDirectoryEnv + "=", CommandRunOriginEnv + "=", commandBypassEnv + "=", commandRecorderHelperEnv + "="} + out := make([]string, 0, len(env)) + for _, entry := range env { + keep := true + for _, key := range keys { + if strings.HasPrefix(entry, key) { + keep = false + } + } + if keep { + out = append(out, entry) + } + } + return out +} + +func captureForInvocation(t *testing.T, in commandRecorderInvocation) string { + t.Helper() + return envValue(in.env, CommandCaptureDirectoryEnv) +} + +func commandRecordEntries(t *testing.T, capture string) []string { + t.Helper() + entries, err := os.ReadDir(filepath.Join(capture, "duo-commands")) + if errors.Is(err, os.ErrNotExist) { + return nil + } + if err != nil { + t.Fatal(err) + } + names := make([]string, 0, len(entries)) + for _, entry := range entries { + names = append(names, entry.Name()) + } + sort.Strings(names) + return names +} + +func onlyCompletedCommand(t *testing.T, capture string) string { + t.Helper() + entries := commandRecordEntries(t, capture) + if len(entries) != 1 || filepath.Ext(entries[0]) != ".complete" { + t.Fatalf("command entries = %v, want one complete record", entries) + } + return filepath.Join(capture, "duo-commands", entries[0]) +} + +func readCommandJSON(t *testing.T, path string, target any) { + t.Helper() + data, err := os.ReadFile(path) + if err != nil { + t.Fatal(err) + } + decoder := json.NewDecoder(bytes.NewReader(data)) + decoder.DisallowUnknownFields() + if err := decoder.Decode(target); err != nil { + t.Fatalf("decode %s: %v", path, err) + } + var trailing any + if err := decoder.Decode(&trailing); !errors.Is(err, io.EOF) { + t.Fatalf("trailing JSON in %s: %v", path, err) + } +} + +func assertFileBytes(t *testing.T, path string, want []byte) { + t.Helper() + got, err := os.ReadFile(path) + if err != nil { + t.Fatal(err) + } + if !bytes.Equal(got, want) { + t.Fatalf("%s = %q, want %q", path, got, want) + } +} + +func assertCommandStreamResult(t *testing.T, got commandStreamResult, data []byte) { + t.Helper() + sum := sha256.Sum256(data) + if got.Bytes != int64(len(data)) || got.SHA256 != hex.EncodeToString(sum[:]) { + t.Fatalf("stream result = %#v for %d bytes", got, len(data)) + } +} + +func assertPrivateCommandModes(t *testing.T, invocation string) { + t.Helper() + dirs := []string{filepath.Dir(invocation), invocation} + for _, dir := range dirs { + info, err := os.Stat(dir) + if err != nil { + t.Fatalf("stat directory %s: %v", dir, err) + } + if info.Mode().Perm() != 0o700 { + t.Fatalf("directory %s mode = %v", dir, info.Mode().Perm()) + } + } + entries, err := os.ReadDir(invocation) + if err != nil { + t.Fatal(err) + } + for _, entry := range entries { + info, infoErr := entry.Info() + if infoErr != nil { + t.Fatalf("stat file %s: %v", entry.Name(), infoErr) + } + if info.Mode().Perm() != 0o600 { + t.Fatalf("file %s mode = %v", entry.Name(), info.Mode().Perm()) + } + } +} diff --git a/internal/conformance/portablelauncher/command_record_unsupported.go b/internal/conformance/portablelauncher/command_record_unsupported.go new file mode 100644 index 0000000..76c413d --- /dev/null +++ b/internal/conformance/portablelauncher/command_record_unsupported.go @@ -0,0 +1,25 @@ +//go:build !linux + +package portablelauncher + +import ( + "fmt" + "os" +) + +const ( + CommandCaptureDirectoryEnv = "DUO_CONFORMANCE_COMMAND_CAPTURE_DIR" + CommandRunOriginEnv = "DUO_CONFORMANCE_RUN_ORIGIN_BOOTTIME_NS" + commandRecorderFail = 125 +) + +// RecordCommandIfConfigured leaves ordinary non-Linux Duo execution +// unchanged. A conformance capture request fails closed because the live +// portable-launcher suite requires Linux process and clock evidence. +func RecordCommandIfConfigured() (recorded bool, exitCode int) { + if os.Getenv(CommandCaptureDirectoryEnv) == "" && os.Getenv(CommandRunOriginEnv) == "" { + return false, 0 + } + _, _ = fmt.Fprintln(os.Stderr, "duo command recorder: portable launcher conformance recording requires Linux") + return true, commandRecorderFail +} diff --git a/internal/conformance/portablelauncher/controller.go b/internal/conformance/portablelauncher/controller.go new file mode 100644 index 0000000..4148afa --- /dev/null +++ b/internal/conformance/portablelauncher/controller.go @@ -0,0 +1,127 @@ +package portablelauncher + +import ( + "context" + "fmt" + "sync" +) + +// ExactControllerOperation performs one controller action against the full +// process-and-attachment identity. Implementations must fail rather than act +// on a PID, terminal, or attachment match alone. +type ExactControllerOperation func(context.Context, ProcessIdentity) error + +// ExactFaultControllerConfig supplies every operation needed for verified +// fault induction. Keeping all five operations mandatory leaves no concrete +// controller path that can fall back to PID-only signaling or an unverified +// pane close. +type ExactFaultControllerConfig struct { + VerifyLiveTarget ExactControllerOperation + StopProcess ExactControllerOperation + VerifyStopped ExactControllerOperation + ClosePane ExactControllerOperation + VerifyPaneAbsent ExactControllerOperation + Monotonic MonotonicClock +} + +// ExactFaultController applies launcher-neutral exact process and pane +// controls. It serializes complete control sequences so verification and the +// action it guards cannot be interleaved by another caller. +type ExactFaultController struct { + config ExactFaultControllerConfig + + mu sync.Mutex + suspended map[ProcessIdentity]struct{} + lastCheckpointMS int64 +} + +var _ FaultController = (*ExactFaultController)(nil) + +// NewExactFaultController validates and constructs an exact fault controller. +func NewExactFaultController(config ExactFaultControllerConfig) (*ExactFaultController, error) { + switch { + case config.VerifyLiveTarget == nil: + return nil, fmt.Errorf("exact fault controller requires live-target verification") + case config.StopProcess == nil: + return nil, fmt.Errorf("exact fault controller requires an exact stop operation") + case config.VerifyStopped == nil: + return nil, fmt.Errorf("exact fault controller requires stopped-state verification") + case config.ClosePane == nil: + return nil, fmt.Errorf("exact fault controller requires an exact pane-close operation") + case config.VerifyPaneAbsent == nil: + return nil, fmt.Errorf("exact fault controller requires pane-absence verification") + case config.Monotonic == nil: + return nil, fmt.Errorf("exact fault controller requires a monotonic clock shared with the checkpoint observer") + } + return &ExactFaultController{ + config: config, + suspended: make(map[ProcessIdentity]struct{}), + }, nil +} + +// SuspendExactProcess verifies the full live identity, stops that identity, +// and independently verifies its stopped state. A failed sequence is never +// recorded as an eligible suspension. +func (c *ExactFaultController) SuspendExactProcess(ctx context.Context, target ProcessIdentity) (ControlCheckpoint, error) { + if !validProcessIdentity(target) { + return ControlCheckpoint{}, fmt.Errorf("exact process suspension requires a complete process identity") + } + + c.mu.Lock() + defer c.mu.Unlock() + + if err := c.config.VerifyLiveTarget(ctx, target); err != nil { + return ControlCheckpoint{}, fmt.Errorf("verify exact live target: %w", err) + } + if err := c.config.StopProcess(ctx, target); err != nil { + return ControlCheckpoint{}, fmt.Errorf("stop exact process: %w", err) + } + if err := c.config.VerifyStopped(ctx, target); err != nil { + return ControlCheckpoint{}, fmt.Errorf("verify exact process stopped: %w", err) + } + + checkpoint := c.checkpoint(target) + c.suspended[target] = struct{}{} + return checkpoint, nil +} + +// CloseExactPane closes only an exact identity successfully suspended by this +// controller. Failures retain the suspension record so callers can retry or +// conservatively clean up; only an independently verified absence consumes it. +func (c *ExactFaultController) CloseExactPane(ctx context.Context, target ProcessIdentity) (ControlCheckpoint, error) { + if !validProcessIdentity(target) { + return ControlCheckpoint{}, fmt.Errorf("exact pane close requires a complete process identity") + } + + c.mu.Lock() + defer c.mu.Unlock() + + if _, ok := c.suspended[target]; !ok { + return ControlCheckpoint{}, fmt.Errorf("exact pane close requires a prior verified suspension for the requested identity") + } + if err := c.config.VerifyStopped(ctx, target); err != nil { + return ControlCheckpoint{}, fmt.Errorf("reverify exact process stopped: %w", err) + } + if err := c.config.ClosePane(ctx, target); err != nil { + return ControlCheckpoint{}, fmt.Errorf("close exact pane: %w", err) + } + if err := c.config.VerifyPaneAbsent(ctx, target); err != nil { + return ControlCheckpoint{}, fmt.Errorf("verify exact pane absent: %w", err) + } + + checkpoint := c.checkpoint(target) + delete(c.suspended, target) + return checkpoint, nil +} + +func (c *ExactFaultController) checkpoint(target ProcessIdentity) ControlCheckpoint { + milliseconds := c.config.Monotonic().Milliseconds() + if milliseconds < 0 { + milliseconds = 0 + } + if milliseconds < c.lastCheckpointMS { + milliseconds = c.lastCheckpointMS + } + c.lastCheckpointMS = milliseconds + return ControlCheckpoint{Target: target, StoppedVerified: true, MonotonicMS: milliseconds} +} diff --git a/internal/conformance/portablelauncher/controller_test.go b/internal/conformance/portablelauncher/controller_test.go new file mode 100644 index 0000000..63390a4 --- /dev/null +++ b/internal/conformance/portablelauncher/controller_test.go @@ -0,0 +1,347 @@ +package portablelauncher + +import ( + "context" + "errors" + "reflect" + "strings" + "sync" + "testing" + "time" +) + +var ( + controllerExitedIdentity = ProcessIdentity{ + PID: 701, StartTime: "birth-alpha", TerminalID: "terminal-zeta", AttachmentID: "attachment-beta", + } + controllerTimeoutIdentity = ProcessIdentity{ + PID: 907, StartTime: "birth-gamma", TerminalID: "terminal-alpha", AttachmentID: "attachment-omega", + } +) + +const ( + operationVerifyLive = "verify-live" + operationStop = "stop" + operationVerifyStop = "verify-stopped" + operationClosePane = "close-pane" + operationVerifyAbsent = "verify-absent" +) + +type controllerOperationCall struct { + name string + target ProcessIdentity +} + +type controllerOperationSpy struct { + mu sync.Mutex + calls []controllerOperationCall + failures map[string][]error +} + +func (s *controllerOperationSpy) operation(name string) ExactControllerOperation { + return func(_ context.Context, target ProcessIdentity) error { + s.mu.Lock() + defer s.mu.Unlock() + s.calls = append(s.calls, controllerOperationCall{name: name, target: target}) + if len(s.failures[name]) == 0 { + return nil + } + err := s.failures[name][0] + s.failures[name] = s.failures[name][1:] + return err + } +} + +func (s *controllerOperationSpy) config(clock MonotonicClock) ExactFaultControllerConfig { + return ExactFaultControllerConfig{ + VerifyLiveTarget: s.operation(operationVerifyLive), + StopProcess: s.operation(operationStop), + VerifyStopped: s.operation(operationVerifyStop), + ClosePane: s.operation(operationClosePane), + VerifyPaneAbsent: s.operation(operationVerifyAbsent), + Monotonic: clock, + } +} + +func (s *controllerOperationSpy) recorded() []controllerOperationCall { + s.mu.Lock() + defer s.mu.Unlock() + return append([]controllerOperationCall(nil), s.calls...) +} + +func (s *controllerOperationSpy) reset() { + s.mu.Lock() + defer s.mu.Unlock() + s.calls = nil +} + +func TestExactFaultControllerRunnerSequenceUsesExactOrderedOperations(t *testing.T) { + spy := &controllerOperationSpy{} + clock := &offsetClock{offsets: []time.Duration{11 * time.Millisecond, 22 * time.Millisecond, 33 * time.Millisecond}} + controller := newExactControllerForTest(t, spy, clock.Now) + + exitedSuspend, err := controller.SuspendExactProcess(context.Background(), controllerExitedIdentity) + if err != nil { + t.Fatalf("SuspendExactProcess(exited): %v", err) + } + exitedClose, err := controller.CloseExactPane(context.Background(), controllerExitedIdentity) + if err != nil { + t.Fatalf("CloseExactPane(exited): %v", err) + } + timeoutSuspend, err := controller.SuspendExactProcess(context.Background(), controllerTimeoutIdentity) + if err != nil { + t.Fatalf("SuspendExactProcess(timeout): %v", err) + } + + assertControlCheckpoint(t, exitedSuspend, controllerExitedIdentity, 11) + assertControlCheckpoint(t, exitedClose, controllerExitedIdentity, 22) + assertControlCheckpoint(t, timeoutSuspend, controllerTimeoutIdentity, 33) + want := []controllerOperationCall{ + {name: operationVerifyLive, target: controllerExitedIdentity}, + {name: operationStop, target: controllerExitedIdentity}, + {name: operationVerifyStop, target: controllerExitedIdentity}, + {name: operationVerifyStop, target: controllerExitedIdentity}, + {name: operationClosePane, target: controllerExitedIdentity}, + {name: operationVerifyAbsent, target: controllerExitedIdentity}, + {name: operationVerifyLive, target: controllerTimeoutIdentity}, + {name: operationStop, target: controllerTimeoutIdentity}, + {name: operationVerifyStop, target: controllerTimeoutIdentity}, + } + if got := spy.recorded(); !reflect.DeepEqual(got, want) { + t.Fatalf("operation calls = %+v, want %+v", got, want) + } +} + +func TestExactFaultControllerSuspendFailsClosedAtEveryBoundary(t *testing.T) { + wantFailure := errors.New("injected suspension failure") + tests := []struct { + name string + operation string + wantCalls []string + }{ + {name: "live target verification", operation: operationVerifyLive, wantCalls: []string{operationVerifyLive}}, + {name: "exact stop", operation: operationStop, wantCalls: []string{operationVerifyLive, operationStop}}, + {name: "stopped state verification", operation: operationVerifyStop, wantCalls: []string{operationVerifyLive, operationStop, operationVerifyStop}}, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + spy := &controllerOperationSpy{failures: map[string][]error{test.operation: {wantFailure}}} + controller := newExactControllerForTest(t, spy, func() time.Duration { return time.Millisecond }) + + if _, err := controller.SuspendExactProcess(context.Background(), controllerExitedIdentity); !errors.Is(err, wantFailure) { + t.Fatalf("SuspendExactProcess error = %v, want injected failure", err) + } + assertOperationNames(t, spy.recorded(), test.wantCalls) + + beforeClose := len(spy.recorded()) + if _, err := controller.CloseExactPane(context.Background(), controllerExitedIdentity); err == nil || !strings.Contains(err.Error(), "prior verified suspension") { + t.Fatalf("CloseExactPane after failed suspension error = %v", err) + } + if got := len(spy.recorded()); got != beforeClose { + t.Fatalf("close after failed suspension invoked %d operations, want none", got-beforeClose) + } + }) + } +} + +func TestExactFaultControllerCloseFailsClosedAndRemainsRetryable(t *testing.T) { + wantFailure := errors.New("injected close failure") + tests := []struct { + name string + operation string + wantCalls []string + }{ + {name: "stopped state reverification", operation: operationVerifyStop, wantCalls: []string{operationVerifyStop}}, + {name: "exact pane close", operation: operationClosePane, wantCalls: []string{operationVerifyStop, operationClosePane}}, + {name: "pane absence verification", operation: operationVerifyAbsent, wantCalls: []string{operationVerifyStop, operationClosePane, operationVerifyAbsent}}, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + spy := &controllerOperationSpy{} + controller := newExactControllerForTest(t, spy, func() time.Duration { return 9 * time.Millisecond }) + if _, err := controller.SuspendExactProcess(context.Background(), controllerExitedIdentity); err != nil { + t.Fatalf("SuspendExactProcess: %v", err) + } + spy.reset() + spy.failures = map[string][]error{test.operation: {wantFailure}} + + if _, err := controller.CloseExactPane(context.Background(), controllerExitedIdentity); !errors.Is(err, wantFailure) { + t.Fatalf("CloseExactPane error = %v, want injected failure", err) + } + assertOperationNames(t, spy.recorded(), test.wantCalls) + + spy.reset() + checkpoint, err := controller.CloseExactPane(context.Background(), controllerExitedIdentity) + if err != nil { + t.Fatalf("CloseExactPane retry: %v", err) + } + assertControlCheckpoint(t, checkpoint, controllerExitedIdentity, 9) + assertOperationNames(t, spy.recorded(), []string{operationVerifyStop, operationClosePane, operationVerifyAbsent}) + }) + } +} + +func TestExactFaultControllerRejectsIncompleteIdentitiesWithoutOperations(t *testing.T) { + tests := []struct { + name string + target ProcessIdentity + }{ + {name: "zero value", target: ProcessIdentity{}}, + {name: "nonpositive PID", target: ProcessIdentity{PID: -1, StartTime: "birth", TerminalID: "terminal", AttachmentID: "attachment"}}, + {name: "missing start time", target: ProcessIdentity{PID: 1, TerminalID: "terminal", AttachmentID: "attachment"}}, + {name: "missing terminal", target: ProcessIdentity{PID: 1, StartTime: "birth", AttachmentID: "attachment"}}, + {name: "missing attachment", target: ProcessIdentity{PID: 1, StartTime: "birth", TerminalID: "terminal"}}, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + spy := &controllerOperationSpy{} + controller := newExactControllerForTest(t, spy, func() time.Duration { return 0 }) + if _, err := controller.SuspendExactProcess(context.Background(), test.target); err == nil || !strings.Contains(err.Error(), "complete process identity") { + t.Fatalf("SuspendExactProcess error = %v", err) + } + if _, err := controller.CloseExactPane(context.Background(), test.target); err == nil || !strings.Contains(err.Error(), "complete process identity") { + t.Fatalf("CloseExactPane error = %v", err) + } + if calls := spy.recorded(); len(calls) != 0 { + t.Fatalf("incomplete identity operations = %+v, want none", calls) + } + }) + } +} + +func TestExactFaultControllerRejectsCloseBeforeSuspension(t *testing.T) { + spy := &controllerOperationSpy{} + controller := newExactControllerForTest(t, spy, func() time.Duration { return 0 }) + if _, err := controller.CloseExactPane(context.Background(), controllerExitedIdentity); err == nil || !strings.Contains(err.Error(), "prior verified suspension") { + t.Fatalf("CloseExactPane error = %v", err) + } + if calls := spy.recorded(); len(calls) != 0 { + t.Fatalf("close-before-suspend operations = %+v, want none", calls) + } +} + +func TestExactFaultControllerDoesNotReuseSuspensionAcrossIdentities(t *testing.T) { + spy := &controllerOperationSpy{} + controller := newExactControllerForTest(t, spy, func() time.Duration { return 0 }) + if _, err := controller.SuspendExactProcess(context.Background(), controllerExitedIdentity); err != nil { + t.Fatalf("SuspendExactProcess: %v", err) + } + spy.reset() + + mismatches := []ProcessIdentity{ + {PID: controllerExitedIdentity.PID, StartTime: controllerTimeoutIdentity.StartTime, TerminalID: controllerExitedIdentity.TerminalID, AttachmentID: controllerExitedIdentity.AttachmentID}, + {PID: controllerExitedIdentity.PID, StartTime: controllerExitedIdentity.StartTime, TerminalID: controllerTimeoutIdentity.TerminalID, AttachmentID: controllerExitedIdentity.AttachmentID}, + {PID: controllerExitedIdentity.PID, StartTime: controllerExitedIdentity.StartTime, TerminalID: controllerExitedIdentity.TerminalID, AttachmentID: controllerTimeoutIdentity.AttachmentID}, + controllerTimeoutIdentity, + } + for _, target := range mismatches { + if _, err := controller.CloseExactPane(context.Background(), target); err == nil || !strings.Contains(err.Error(), "prior verified suspension") { + t.Fatalf("CloseExactPane(%+v) error = %v", target, err) + } + } + if calls := spy.recorded(); len(calls) != 0 { + t.Fatalf("mismatched identity operations = %+v, want none", calls) + } + + if _, err := controller.CloseExactPane(context.Background(), controllerExitedIdentity); err != nil { + t.Fatalf("CloseExactPane(original): %v", err) + } +} + +func TestExactFaultControllerCheckpointMillisecondsNeverRegress(t *testing.T) { + spy := &controllerOperationSpy{} + clock := &offsetClock{offsets: []time.Duration{ + 19*time.Millisecond + 900*time.Microsecond, + 7 * time.Millisecond, + -3 * time.Millisecond, + }} + controller := newExactControllerForTest(t, spy, clock.Now) + + first, err := controller.SuspendExactProcess(context.Background(), controllerExitedIdentity) + if err != nil { + t.Fatalf("first suspension: %v", err) + } + second, err := controller.CloseExactPane(context.Background(), controllerExitedIdentity) + if err != nil { + t.Fatalf("close: %v", err) + } + third, err := controller.SuspendExactProcess(context.Background(), controllerTimeoutIdentity) + if err != nil { + t.Fatalf("second suspension: %v", err) + } + if got, want := []int64{first.MonotonicMS, second.MonotonicMS, third.MonotonicMS}, []int64{19, 19, 19}; !reflect.DeepEqual(got, want) { + t.Fatalf("checkpoint milliseconds = %v, want %v", got, want) + } +} + +func TestNewExactFaultControllerRequiresCompleteConfig(t *testing.T) { + noop := func(context.Context, ProcessIdentity) error { return nil } + complete := ExactFaultControllerConfig{ + VerifyLiveTarget: noop, + StopProcess: noop, + VerifyStopped: noop, + ClosePane: noop, + VerifyPaneAbsent: noop, + Monotonic: func() time.Duration { return 0 }, + } + tests := []struct { + name string + mutate func(*ExactFaultControllerConfig) + wantError string + }{ + {name: "live target verifier", mutate: func(c *ExactFaultControllerConfig) { c.VerifyLiveTarget = nil }}, + {name: "exact stopper", mutate: func(c *ExactFaultControllerConfig) { c.StopProcess = nil }}, + {name: "stopped verifier", mutate: func(c *ExactFaultControllerConfig) { c.VerifyStopped = nil }}, + {name: "pane closer", mutate: func(c *ExactFaultControllerConfig) { c.ClosePane = nil }}, + {name: "pane absence verifier", mutate: func(c *ExactFaultControllerConfig) { c.VerifyPaneAbsent = nil }}, + { + name: "monotonic clock", + mutate: func(c *ExactFaultControllerConfig) { c.Monotonic = nil }, + wantError: "requires a monotonic clock shared with the checkpoint observer", + }, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + config := complete + test.mutate(&config) + _, err := NewExactFaultController(config) + if err == nil { + t.Fatal("NewExactFaultController accepted an incomplete config") + } + if test.wantError != "" && !strings.Contains(err.Error(), test.wantError) { + t.Fatalf("NewExactFaultController error = %q, want it to contain %q", err, test.wantError) + } + }) + } +} + +func newExactControllerForTest(t *testing.T, spy *controllerOperationSpy, clock MonotonicClock) *ExactFaultController { + t.Helper() + controller, err := NewExactFaultController(spy.config(clock)) + if err != nil { + t.Fatalf("NewExactFaultController: %v", err) + } + return controller +} + +func assertControlCheckpoint(t *testing.T, got ControlCheckpoint, target ProcessIdentity, milliseconds int64) { + t.Helper() + want := ControlCheckpoint{Target: target, StoppedVerified: true, MonotonicMS: milliseconds} + if got != want { + t.Fatalf("checkpoint = %+v, want %+v", got, want) + } +} + +func assertOperationNames(t *testing.T, calls []controllerOperationCall, want []string) { + t.Helper() + got := make([]string, len(calls)) + for index, call := range calls { + got[index] = call.name + if call.target != controllerExitedIdentity { + t.Fatalf("operation %d target = %+v, want %+v", index, call.target, controllerExitedIdentity) + } + } + if !reflect.DeepEqual(got, want) { + t.Fatalf("operation names = %v, want %v", got, want) + } +} diff --git a/internal/conformance/portablelauncher/driver.go b/internal/conformance/portablelauncher/driver.go new file mode 100644 index 0000000..52a5fa2 --- /dev/null +++ b/internal/conformance/portablelauncher/driver.go @@ -0,0 +1,253 @@ +package portablelauncher + +import ( + "bytes" + "context" + "errors" + "fmt" + "os" + "os/exec" + "path/filepath" + "regexp" + "strconv" + "strings" + "time" +) + +// TaskArgument is the placeholder a driver replaces with the canonical task. +const ( + TaskArgument = "{PORTABLE_LAUNCHER_TASK_BYTES}" + completeRunTimeout = 10 * time.Minute +) + +// DriverSpec defines one launcher's thin outer-process invocation. +type DriverSpec struct { + Name string + Executable string + Arguments []string +} + +// DriverRequest supplies the isolated paths and allowed environment additions +// for one launcher invocation. +type DriverRequest struct { + RunRoot string + Workspace string + ScenarioPath string + RunOriginBootTimeNS string + Environment map[string]string +} + +// DriverCapture contains the raw process streams collected from a launcher. +type DriverCapture struct { + Events []byte + Stderr []byte +} + +// PersistDriverCapture writes the launcher's raw process streams below the +// isolated capture directory without interpreting or repairing either one. +func PersistDriverCapture(runRoot string, capture DriverCapture) error { + root, err := validatePrivateTempDir(runRoot) + if err != nil || !strings.HasPrefix(filepath.Base(root), "duo-portable-launcher-") { + return fmt.Errorf("persist driver capture: unsafe run root") + } + dir := filepath.Join(root, "capture") + resolved, err := filepath.EvalSymlinks(dir) + if err != nil || !within(root, resolved) { + return fmt.Errorf("persist driver capture: unsafe capture directory") + } + files := []struct { + name string + data []byte + }{ + {name: "launcher-events.jsonl", data: capture.Events}, + {name: "launcher-stderr.log", data: capture.Stderr}, + } + for _, file := range files { + path := filepath.Join(dir, file.name) + out, openErr := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o600) + if openErr != nil { + return fmt.Errorf("persist driver capture %s: %w", file.name, openErr) + } + _, writeErr := out.Write(file.data) + closeErr := out.Close() + if writeErr != nil { + return fmt.Errorf("persist driver capture %s: %w", file.name, writeErr) + } + if closeErr != nil { + return fmt.Errorf("persist driver capture %s: %w", file.name, closeErr) + } + } + return nil +} + +// RunDriver only constructs and executes the outer process invocation. It +// submits the common task once and returns the raw launcher process streams. +func RunDriver(ctx context.Context, spec DriverSpec, request DriverRequest) (DriverCapture, error) { + if err := validateDriverRequest(spec, request); err != nil { + return DriverCapture{}, err + } + ctx, cancel := context.WithTimeout(ctx, completeRunTimeout) + defer cancel() + args := append([]string(nil), spec.Arguments...) + taskCount := 0 + for i := range args { + if args[i] == TaskArgument { + args[i] = string(CanonicalTaskBytes) + taskCount++ + } + } + if taskCount != 1 { + return DriverCapture{}, fmt.Errorf("driver must submit the canonical task exactly once") + } + cmd := exec.CommandContext(ctx, spec.Executable, args...) + cmd.Dir = request.Workspace + cmd.Stdin = nil + cmd.Env = isolatedEnvironment(request) + var stdout, stderr bytes.Buffer + cmd.Stdout = &stdout + cmd.Stderr = &stderr + runErr := cmd.Run() + capture := DriverCapture{Events: stdout.Bytes(), Stderr: stderr.Bytes()} + if runErr != nil { + if errors.Is(ctx.Err(), context.DeadlineExceeded) { + return capture, fmt.Errorf("launcher complete-run deadline exceeded: %w", ctx.Err()) + } + return capture, fmt.Errorf("launcher process failed: %w", runErr) + } + return capture, nil +} + +func validateDriverRequest(spec DriverSpec, r DriverRequest) error { + if err := validateRunOrigin(r.RunOriginBootTimeNS); err != nil { + return err + } + if spec.Name == "" || spec.Executable == "" || !filepath.IsAbs(spec.Executable) { + return fmt.Errorf("driver executable must be named and absolute") + } + if !filepath.IsAbs(r.RunRoot) || !strings.HasPrefix(filepath.Base(r.RunRoot), "duo-portable-launcher-") { + return fmt.Errorf("driver run root must be an isolated temporary child") + } + root, err := validatePrivateTempDir(r.RunRoot) + if err != nil { + return fmt.Errorf("driver run root must exist as a private directory") + } + want, ok := AcceptedLauncherPin(spec.Name) + if !ok { + return fmt.Errorf("driver names an unsupported launcher") + } + for label, path := range map[string]string{"executable": spec.Executable, "workspace": r.Workspace, "scenario": r.ScenarioPath} { + resolved, resolveErr := filepath.EvalSymlinks(path) + if resolveErr != nil || !within(root, resolved) { + return fmt.Errorf("driver %s path escapes run root", label) + } + } + resolvedExecutable, _ := filepath.EvalSymlinks(spec.Executable) + if filepath.Dir(resolvedExecutable) != filepath.Join(root, "bin") { + return fmt.Errorf("driver executable must resolve from the pinned run bin") + } + if info, err := os.Lstat(spec.Executable); err != nil || !info.Mode().IsRegular() { + return fmt.Errorf("driver executable is missing or not a regular file") + } + executable, err := os.ReadFile(spec.Executable) + if err != nil || Digest(executable) != want.Digest { + return fmt.Errorf("driver executable does not match the pinned digest") + } + scenario, err := os.ReadFile(r.ScenarioPath) + if err != nil { + return fmt.Errorf("driver scenario is unavailable: %w", err) + } + wantScenario, err := ScenarioJSON(CanonicalScenario()) + if err != nil || !bytes.Equal(scenario, wantScenario) { + return fmt.Errorf("driver scenario does not match canonical bytes") + } + for key := range r.Environment { + upper := strings.ToUpper(key) + if key == "" || strings.Contains(key, "=") || upper == "PATH" || upper == "HOME" || strings.HasPrefix(upper, "XDG_") || strings.HasPrefix(upper, "DUO_CONFORMANCE_") || strings.Contains(upper, "MCP") || strings.Contains(upper, "PLUGIN") { + return fmt.Errorf("driver environment key %q is forbidden", key) + } + } + return nil +} + +func validateRunOrigin(value string) error { + if value == "" { + return fmt.Errorf("driver common run origin must be a positive decimal int64") + } + for i := range value { + if value[i] < '0' || value[i] > '9' { + return fmt.Errorf("driver common run origin must be a positive decimal int64") + } + } + origin, err := strconv.ParseInt(value, 10, 64) + if err != nil || origin <= 0 { + return fmt.Errorf("driver common run origin must be a positive decimal int64") + } + return nil +} + +// RequestFromRunRoot constructs the common isolated paths used by every thin +// adapter. It does not inspect or alter scenario semantics. +func RequestFromRunRoot(root, runOriginBootTimeNS string) DriverRequest { + return DriverRequest{ + RunRoot: root, Workspace: filepath.Join(root, "workspace"), RunOriginBootTimeNS: runOriginBootTimeNS, + ScenarioPath: filepath.Join(root, "workspace", ".duo-conformance", "scenario.json"), + } +} + +func isolatedEnvironment(r DriverRequest) []string { + env := map[string]string{ + "CODEX_HOME": filepath.Join(r.RunRoot, "xdg", "config", "codex"), + "HOME": filepath.Join(r.RunRoot, "home"), + "PATH": filepath.Join(r.RunRoot, "bin"), + "XDG_CONFIG_HOME": filepath.Join(r.RunRoot, "xdg", "config"), + "XDG_DATA_HOME": filepath.Join(r.RunRoot, "xdg", "data"), + "XDG_STATE_HOME": filepath.Join(r.RunRoot, "xdg", "state"), + "XDG_CACHE_HOME": filepath.Join(r.RunRoot, "xdg", "cache"), + "XDG_RUNTIME_DIR": filepath.Join(r.RunRoot, "xdg", "runtime"), + "PI_CODING_AGENT_DIR": filepath.Join(r.RunRoot, "xdg", "config", "pi"), + "PI_CODING_AGENT_SESSION_DIR": filepath.Join(r.RunRoot, "xdg", "data", "pi", "sessions"), + "HERDR_CONFIG_PATH": filepath.Join(r.RunRoot, "herdr", "config.yaml"), + "DUO_CONFORMANCE_SCENARIO_PATH": r.ScenarioPath, + CommandCaptureDirectoryEnv: filepath.Join(r.RunRoot, "capture"), + CommandRunOriginEnv: r.RunOriginBootTimeNS, + } + for key, value := range r.Environment { + env[key] = value + } + out := make([]string, 0, len(env)) + for key, value := range env { + out = append(out, key+"="+value) + } + return out +} + +var forbiddenDriverSemantics = []*regexp.Regexp{ + regexp.MustCompile(`\bduo\s+(doctor|session|prompt|command|wait)\b`), + regexp.MustCompile(`DUO_PORTABLE_LAUNCHER_OK_V1|b40d3a68|dbe904a4`), + regexp.MustCompile(`(?i)\b(poll|retry|sigstop|pane\.close|send_keys|send_text|repair_result)\b`), +} + +// ValidateDriverSource is a static guard for thin adapter sources. +func ValidateDriverSource(source []byte) error { + for _, pattern := range forbiddenDriverSemantics { + if pattern.Match(source) { + return fmt.Errorf("launcher driver contains scenario/oracle semantics matching %s", pattern) + } + } + return nil +} + +// ValidateSemanticSource rejects launcher-specific branching/imports in the +// common scenario, oracle, and validator sources. +func ValidateSemanticSource(source []byte) error { + lower := strings.ToLower(string(source)) + for _, token := range []string{"internal/runtime/amp", "internal/runtime/opencode", "internal/runtime/codex"} { + if strings.Contains(lower, token) { + return fmt.Errorf("semantic source imports launcher package %q", token) + } + } + if regexp.MustCompile(`switch\s+(launcher|driver)\b|if\s+(launcher|driver)\b`).MatchString(lower) { + return fmt.Errorf("semantic source branches on launcher identity") + } + return nil +} diff --git a/internal/conformance/portablelauncher/external_schema.go b/internal/conformance/portablelauncher/external_schema.go new file mode 100644 index 0000000..79ab9c3 --- /dev/null +++ b/internal/conformance/portablelauncher/external_schema.go @@ -0,0 +1,203 @@ +package portablelauncher + +import ( + "encoding/json" + "fmt" + "math" + "reflect" + "regexp" + "strings" + "sync" + "time" + + "github.com/procrastivity/duo/contracts" +) + +var externalSchemaOnce = sync.OnceValues(func() (map[string]any, error) { + data, err := contracts.FS.ReadFile("schemas/duo-external-v1.schema.json") + if err != nil { + return nil, err + } + var schema map[string]any + if err := json.Unmarshal(data, &schema); err != nil { + return nil, err + } + return schema, nil +}) + +// ValidateExternalEnvelope validates normalized captured Duo evidence against +// the pinned public schema using the standard-library JSON value model. +func ValidateExternalEnvelope(envelope map[string]any) error { + schema, err := externalSchemaOnce() + if err != nil { + return fmt.Errorf("load duo.external/v1 schema: %w", err) + } + var problems Problems + validateSchemaNode(schema, schema, envelope, "$", &problems) + return problems.Err() +} + +func validateSchemaNode(root, schema map[string]any, value any, path string, problems *Problems) { + if ref, ok := schema["$ref"].(string); ok { + name := strings.TrimPrefix(ref, "#/$defs/") + defs, _ := root["$defs"].(map[string]any) + target, ok := defs[name].(map[string]any) + if !ok { + problems.Add(path + ": unresolved schema reference " + ref) + return + } + validateSchemaNode(root, target, value, path, problems) + return + } + if alternatives, ok := schema["oneOf"].([]any); ok { + matches := 0 + for _, raw := range alternatives { + candidate, _ := raw.(map[string]any) + var candidateProblems Problems + validateSchemaNode(root, candidate, value, path, &candidateProblems) + if len(candidateProblems.Items) == 0 { + matches++ + } + } + if matches != 1 { + problems.Add(fmt.Sprintf("%s: matches %d oneOf alternatives, want exactly one", path, matches)) + } + } + if clauses, ok := schema["allOf"].([]any); ok { + for _, raw := range clauses { + clause, _ := raw.(map[string]any) + if condition, ok := clause["if"].(map[string]any); ok { + var conditionProblems Problems + validateSchemaNode(root, condition, value, path, &conditionProblems) + if len(conditionProblems.Items) == 0 { + if thenSchema, ok := clause["then"].(map[string]any); ok { + validateSchemaNode(root, thenSchema, value, path, problems) + } + } else if elseSchema, ok := clause["else"].(map[string]any); ok { + validateSchemaNode(root, elseSchema, value, path, problems) + } + continue + } + validateSchemaNode(root, clause, value, path, problems) + } + } + if constValue, ok := schema["const"]; ok && !reflect.DeepEqual(constValue, value) { + problems.Add(fmt.Sprintf("%s: value does not match const", path)) + } + if values, ok := schema["enum"].([]any); ok { + matched := false + for _, candidate := range values { + matched = matched || reflect.DeepEqual(candidate, value) + } + if !matched { + problems.Add(fmt.Sprintf("%s: value is outside enum", path)) + } + } + if rawType, ok := schema["type"]; ok && !schemaTypeMatches(rawType, value) { + problems.Add(fmt.Sprintf("%s: type mismatch", path)) + return + } + + switch typed := value.(type) { + case map[string]any: + if minimum, ok := schema["minProperties"].(float64); ok && len(typed) < int(minimum) { + problems.Add(fmt.Sprintf("%s: too few properties", path)) + } + if required, ok := schema["required"].([]any); ok { + for _, raw := range required { + key, _ := raw.(string) + if _, present := typed[key]; !present { + problems.Add(fmt.Sprintf("%s: missing required property %s", path, key)) + } + } + } + properties, _ := schema["properties"].(map[string]any) + additional, hasAdditional := schema["additionalProperties"] + for key, child := range typed { + if raw, ok := properties[key]; ok { + childSchema, _ := raw.(map[string]any) + validateSchemaNode(root, childSchema, child, path+"."+key, problems) + continue + } + if allowed, ok := additional.(bool); hasAdditional && ok && !allowed { + problems.Add(fmt.Sprintf("%s: additional property %s", path, key)) + } else if childSchema, ok := additional.(map[string]any); hasAdditional && ok { + validateSchemaNode(root, childSchema, child, path+"."+key, problems) + } + } + case []any: + if minimum, ok := schema["minItems"].(float64); ok && len(typed) < int(minimum) { + problems.Add(fmt.Sprintf("%s: too few items", path)) + } + if unique, _ := schema["uniqueItems"].(bool); unique { + seen := map[string]bool{} + for _, item := range typed { + encoded, _ := json.Marshal(item) + if seen[string(encoded)] { + problems.Add(fmt.Sprintf("%s: duplicate array item", path)) + } + seen[string(encoded)] = true + } + } + if itemSchema, ok := schema["items"].(map[string]any); ok { + for i, item := range typed { + validateSchemaNode(root, itemSchema, item, fmt.Sprintf("%s[%d]", path, i), problems) + } + } + case string: + if minimum, ok := schema["minLength"].(float64); ok && len(typed) < int(minimum) { + problems.Add(fmt.Sprintf("%s: string is too short", path)) + } + if pattern, ok := schema["pattern"].(string); ok { + compiled, err := regexp.Compile(pattern) + if err != nil || !compiled.MatchString(typed) { + problems.Add(fmt.Sprintf("%s: string does not match pattern", path)) + } + } + if format, _ := schema["format"].(string); format == "date-time" { + if _, err := time.Parse(time.RFC3339Nano, typed); err != nil { + problems.Add(fmt.Sprintf("%s: invalid date-time", path)) + } + } + case float64: + if minimum, ok := schema["minimum"].(float64); ok && typed < minimum { + problems.Add(fmt.Sprintf("%s: number is below minimum", path)) + } + } +} + +func schemaTypeMatches(rawType, value any) bool { + if alternatives, ok := rawType.([]any); ok { + for _, alternative := range alternatives { + if schemaTypeMatches(alternative, value) { + return true + } + } + return false + } + typeName, _ := rawType.(string) + switch typeName { + case "object": + _, ok := value.(map[string]any) + return ok + case "array": + _, ok := value.([]any) + return ok + case "string": + _, ok := value.(string) + return ok + case "boolean": + _, ok := value.(bool) + return ok + case "number": + _, ok := value.(float64) + return ok + case "integer": + number, ok := value.(float64) + return ok && number == math.Trunc(number) + case "null": + return value == nil + default: + return true + } +} diff --git a/internal/conformance/portablelauncher/fixture.go b/internal/conformance/portablelauncher/fixture.go new file mode 100644 index 0000000..77d4d2f --- /dev/null +++ b/internal/conformance/portablelauncher/fixture.go @@ -0,0 +1,338 @@ +package portablelauncher + +import ( + "context" + "errors" + "fmt" + "io" + "os" + "path/filepath" + "strings" + "time" + + "github.com/procrastivity/duo/internal/manifest" +) + +// Artifact identifies one immutable executable used by a suite run. +type Artifact struct { + Name string + Path string + Version string + Digest string + Commit string + BuildDate string +} + +// Credential carries isolated provider credentials into a fixture. +type Credential struct { + Present bool + SourceKind string + Bytes []byte +} + +// SetupInput contains the pinned inputs required to prepare an isolated run. +type SetupInput struct { + BaseDir string + Artifacts map[string]Artifact + HostProtocol string + HostSchemaDigest string + ProductManifest manifest.Manifest + ConfigBytes []byte + EffectiveConfigDigest string + Credential Credential + // Duo identifies the exact per-run build expected from Artifacts["duo"]. + // It is supplied by the run, not locked to the commit that designed the + // suite, and is copied unchanged into the authoritative result pins. + Duo DuoPin +} + +// Fixture identifies the isolated filesystem paths for one suite run. +type Fixture struct { + Root string + Workspace string + Capture string + Result string + InstallationID string + cleaned bool +} + +// CheckSetupPrerequisites is deliberately pure: callers must run it before +// creating a run root or touching shared state. +func CheckSetupPrerequisites(in SetupInput) error { + var p Problems + if _, err := validatePrivateTempDir(in.BaseDir); err != nil { + p.Add("setup base must be a private child of the system temporary directory: " + err.Error()) + } + + required := []string{"launcher", "duo", "host", "runtime"} + for _, key := range required { + artifact, ok := in.Artifacts[key] + if !ok { + p.Add("missing pinned artifact: " + key) + continue + } + if err := validateArtifact(artifact); err != nil { + p.Add(key + ": " + err.Error()) + } + } + if launcher, ok := in.Artifacts["launcher"]; ok { + want, accepted := AcceptedLauncherPin(launcher.Name) + if !accepted || launcher.Version != want.Version || launcher.Digest != want.Digest { + p.Add("launcher: exact accepted version and executable digest required") + } + } + if duo, ok := in.Artifacts["duo"]; ok { + if duo.Name != "duo" || !validDuoPin(in.Duo) || duo.Version != in.Duo.Version || duo.Commit != in.Duo.Commit || duo.BuildDate != in.Duo.BuildDate || duo.Digest != in.Duo.ExecutableSHA256 { + p.Add("duo: artifact must exactly match the per-run version, commit, build date, and executable pin") + } + } + tool := in.ProductManifest.Tool + if tool.Name != "duo" || tool.Version != in.Duo.Version || tool.Commit != in.Duo.Commit || tool.Date != in.Duo.BuildDate || in.ProductManifest.Product.Name != "duo" || in.ProductManifest.Product.Version != in.Duo.Version { + p.Add("duo: product manifest build identity must exactly match the per-run pin") + } + if host, ok := in.Artifacts["host"]; ok { + if host.Name != "herdr" || host.Version != "0.8.2" || in.HostProtocol != "herdr-socket-api/20" || in.HostSchemaDigest != HostSchemaDigest { + p.Add("host: exact Herdr 0.8.2/protocol 20/schema prerequisite absent") + } + } + if runtime, ok := in.Artifacts["runtime"]; ok && (runtime.Name != "pi" || runtime.Version != "0.83.0") { + p.Add("runtime: exact Pi 0.83.0 prerequisite absent") + } + if len(in.ProductManifest.HarnessTargets) != 1 || in.ProductManifest.HarnessTargets[0].Name != manifest.PortableTargetName || in.ProductManifest.HarnessTargets[0].Artifact.ContentDigest != SkillContentDigest { + p.Add("skill: product manifest does not carry the locked projection identity") + } + if len(in.ConfigBytes) == 0 || !digestPattern.MatchString(in.EffectiveConfigDigest) || Digest(in.ConfigBytes) != in.EffectiveConfigDigest { + p.Add("config: deterministic bytes must match the effective digest") + } + if !in.Credential.Present || len(in.Credential.Bytes) == 0 || !oneOf(in.Credential.SourceKind, "ci_secret", "operator_copy", "environment") { + p.Add("credential: isolated selected-provider credential prerequisite absent") + } + return p.Err() +} + +func validateArtifact(a Artifact) error { + if a.Name == "" || a.Version == "" || !digestPattern.MatchString(a.Digest) || a.Path == "" { + return fmt.Errorf("identity is unpinned or incomplete") + } + info, err := os.Lstat(a.Path) + if err != nil { + return fmt.Errorf("artifact unavailable: %w", err) + } + if !info.Mode().IsRegular() { + return fmt.Errorf("artifact must be a regular file") + } + b, err := os.ReadFile(a.Path) + if err != nil { + return fmt.Errorf("read artifact: %w", err) + } + if Digest(b) != a.Digest { + return fmt.Errorf("artifact digest mismatch") + } + return nil +} + +// PrepareFixture validates all prerequisites and materializes an isolated run +// without modifying shared launcher or user state. +func PrepareFixture(in SetupInput) (*Fixture, error) { + if err := CheckSetupPrerequisites(in); err != nil { + return nil, err + } + root, err := os.MkdirTemp(in.BaseDir, "duo-portable-launcher-") + if err != nil { + return nil, fmt.Errorf("create run root: %w", err) + } + if err := os.Chmod(root, 0o700); err != nil { + _ = os.RemoveAll(root) + return nil, err + } + f := &Fixture{Root: root, Workspace: filepath.Join(root, "workspace"), Capture: filepath.Join(root, "capture"), Result: filepath.Join(root, "result")} + if err := f.materialize(in); err != nil { + _ = os.RemoveAll(root) + return nil, err + } + return f, nil +} + +func (f *Fixture) materialize(in SetupInput) error { + dirs := []string{ + "home", "xdg/config", "xdg/config/amp", "xdg/config/codex", "xdg/config/duo", "xdg/config/opencode", "xdg/config/pi", "xdg/data", "xdg/data/pi/sessions", "xdg/state", "xdg/cache", "xdg/runtime", + "bin", "secrets", "herdr", "workspace", "workspace/fixture", "workspace/.git/refs/heads", + "workspace/.duo-conformance", "capture", "result", + } + for _, rel := range dirs { + path, err := f.Path(rel) + if err != nil { + return err + } + if err := os.MkdirAll(path, 0o700); err != nil { + return fmt.Errorf("create %s: %w", rel, err) + } + } + files := []struct { + rel string + data []byte + mode os.FileMode + }{ + {"workspace/fixture/request.txt", RequestBytes, 0o600}, + {"workspace/.git/HEAD", []byte("ref: refs/heads/main\n"), 0o600}, + {"workspace/.git/config", []byte("[core]\n\trepositoryformatversion = 0\n\tbare = false\n"), 0o600}, + {"xdg/config/duo/duo.config.yaml", in.ConfigBytes, 0o600}, + {"xdg/config/amp/settings.json", []byte("{}\n"), 0o600}, + {"secrets/provider", in.Credential.Bytes, 0o600}, + {"xdg/config/pi/auth.json", in.Credential.Bytes, 0o600}, + } + scenario, err := ScenarioJSON(CanonicalScenario()) + if err != nil { + return err + } + files = append(files, struct { + rel string + data []byte + mode os.FileMode + }{"workspace/.duo-conformance/scenario.json", scenario, 0o600}) + for _, file := range files { + path, err := f.Path(file.rel) + if err != nil { + return err + } + if err := os.WriteFile(path, file.data, file.mode); err != nil { + return fmt.Errorf("write %s: %w", file.rel, err) + } + } + installed, err := manifest.InstallPortableLaunchers(f.Workspace, false, in.ProductManifest) + if err != nil { + return fmt.Errorf("install portable launcher skill: %w", err) + } + if installed.State != manifest.StateCurrent || installed.ContentDigest != SkillContentDigest || installed.InstallationID == "" { + return fmt.Errorf("portable launcher skill installation did not produce the locked current projection") + } + f.InstallationID = installed.InstallationID + for key, artifact := range in.Artifacts { + if err := copyFile(artifact.Path, filepath.Join(f.Root, "bin", artifact.Name), 0o700); err != nil { + return fmt.Errorf("copy %s: %w", key, err) + } + } + return nil +} + +func validDuoPin(pin DuoPin) bool { + if pin.Version == "" || !exactCommitPattern.MatchString(pin.Commit) || strings.Trim(pin.Commit, "0") == "" || !digestPattern.MatchString(pin.ExecutableSHA256) { + return false + } + when, err := time.Parse(time.RFC3339, pin.BuildDate) + return err == nil && when.Location() == time.UTC +} + +// Path resolves a clean relative path below the fixture root. +func (f *Fixture) Path(relative string) (string, error) { + if relative == "" || filepath.IsAbs(relative) || filepath.Clean(relative) != relative || relative == ".." || strings.HasPrefix(relative, ".."+string(filepath.Separator)) { + return "", fmt.Errorf("unsafe fixture-relative path %q", relative) + } + path := filepath.Join(f.Root, relative) + if !within(f.Root, path) { + return "", fmt.Errorf("fixture path escapes run root: %q", relative) + } + return path, nil +} + +func within(root, path string) bool { + rel, err := filepath.Rel(filepath.Clean(root), filepath.Clean(path)) + return err == nil && rel != ".." && !strings.HasPrefix(rel, ".."+string(filepath.Separator)) +} + +func validatePrivateTempDir(path string) (string, error) { + abs, err := filepath.Abs(path) + if err != nil { + return "", err + } + info, err := os.Lstat(abs) + if err != nil || !info.IsDir() || info.Mode()&os.ModeSymlink != 0 || info.Mode().Perm()&0o077 != 0 { + return "", fmt.Errorf("path must exist as a mode-private, non-symlink directory") + } + resolved, err := filepath.EvalSymlinks(abs) + if err != nil { + return "", err + } + temp, err := filepath.EvalSymlinks(filepath.Clean(os.TempDir())) + if err != nil { + return "", err + } + if filepath.Clean(resolved) != filepath.Clean(abs) { + return "", fmt.Errorf("path contains a symlink component") + } + if resolved == temp || !within(temp, resolved) { + return "", fmt.Errorf("path is outside the system temporary directory") + } + return resolved, nil +} + +func copyFile(src, dst string, mode os.FileMode) error { + in, err := os.Open(src) + if err != nil { + return err + } + out, err := os.OpenFile(dst, os.O_CREATE|os.O_EXCL|os.O_WRONLY, mode) + if err != nil { + return errors.Join(err, in.Close()) + } + _, copyErr := io.Copy(out, in) + return errors.Join(copyErr, out.Close(), in.Close()) +} + +// ResourceInspector verifies that no run-owned resources remain active before +// a fixture is removed. +type ResourceInspector func(context.Context, *Fixture) error + +// EvidenceExporter preserves the capture needed for offline validation before +// a fixture is removed. +type EvidenceExporter func(context.Context, *Fixture) error + +// Cleanup records failures by returning them even though it performs a +// best-effort exact-root removal afterwards. It never broad-kills processes. +func (f *Fixture) Cleanup(ctx context.Context, inspect ResourceInspector, export EvidenceExporter) error { + if f == nil || f.Root == "" || !strings.HasPrefix(filepath.Base(f.Root), "duo-portable-launcher-") { + return fmt.Errorf("refusing cleanup of unsafe root") + } + if _, err := validatePrivateTempDir(f.Root); err != nil { + return fmt.Errorf("refusing cleanup of missing or symlinked root") + } + var p Problems + if inspect == nil { + p.Add("cleanup resource inspection omitted") + } else if err := inspect(ctx, f); err != nil { + p.Add("cleanup resource inspection failed: " + err.Error()) + } + if export == nil { + p.Add("cleanup evidence export omitted") + } else if err := export(ctx, f); err != nil { + p.Add("cleanup evidence export failed: " + err.Error()) + } + if err := os.RemoveAll(f.Root); err != nil { + p.Add("cleanup root removal failed: " + err.Error()) + } else { + f.cleaned = true + } + return p.Err() +} + +// ProcessIdentity identifies one exact run-owned process and attachment. +type ProcessIdentity struct { + PID int + StartTime string + TerminalID string + AttachmentID string +} + +// ControlCheckpoint records an independently verified fault-controller action. +type ControlCheckpoint struct { + Target ProcessIdentity + StoppedVerified bool + MonotonicMS int64 +} + +// FaultController is the boundary later live stages implement. The common +// suite owns when it is called; a launcher driver never receives this seam. +type FaultController interface { + SuspendExactProcess(context.Context, ProcessIdentity) (ControlCheckpoint, error) + CloseExactPane(context.Context, ProcessIdentity) (ControlCheckpoint, error) +} diff --git a/internal/conformance/portablelauncher/live_controller_linux.go b/internal/conformance/portablelauncher/live_controller_linux.go new file mode 100644 index 0000000..7900a85 --- /dev/null +++ b/internal/conformance/portablelauncher/live_controller_linux.go @@ -0,0 +1,408 @@ +//go:build linux + +package portablelauncher + +import ( + "context" + "errors" + "fmt" + "sync" + "time" + + "github.com/procrastivity/duo/internal/domain" + "github.com/procrastivity/duo/internal/host" + "github.com/procrastivity/duo/internal/host/herdr" + "github.com/procrastivity/duo/internal/launch/materialize" +) + +const herdrTerminalEpochKind = "herdr.terminal_id" + +// AuthorityAttachmentClaimResolver resolves an exact controller target from +// the durable authority log. Resolution is by AttachmentID only; it never +// infers a session from launch order, a PID, or mutable host coordinates. +type AuthorityAttachmentClaimResolver struct { + load AuthoritySnapshotLoader + herdrIntegration string +} + +// NewAuthorityAttachmentClaimResolver constructs a read-only durable claim +// resolver for one exact Herdr integration instance. +func NewAuthorityAttachmentClaimResolver(load AuthoritySnapshotLoader, herdrIntegration string) (*AuthorityAttachmentClaimResolver, error) { + switch { + case load == nil: + return nil, fmt.Errorf("authority attachment claim resolver requires a snapshot loader") + case herdrIntegration == "": + return nil, fmt.Errorf("authority attachment claim resolver requires the exact Herdr integration identity") + } + return &AuthorityAttachmentClaimResolver{load: load, herdrIntegration: herdrIntegration}, nil +} + +// Resolve replays one read-only snapshot and returns the exact durable claim +// selected by target.AttachmentID. Every relationship needed to authorize +// live process control is checked again on each call. +func (r *AuthorityAttachmentClaimResolver) Resolve(ctx context.Context, target ProcessIdentity) (host.HostAttachmentClaim, error) { + if r == nil || r.load == nil || r.herdrIntegration == "" { + return host.HostAttachmentClaim{}, fmt.Errorf("authority attachment claim resolver is not configured") + } + if !validProcessIdentity(target) { + return host.HostAttachmentClaim{}, fmt.Errorf("authority attachment claim resolution requires a complete process identity") + } + facts, err := r.load(ctx) + if err != nil { + return host.HostAttachmentClaim{}, fmt.Errorf("load authority attachment snapshot: %w", err) + } + if err := ctx.Err(); err != nil { + return host.HostAttachmentClaim{}, err + } + projection, err := projectSnapshot(facts) + if err != nil { + return host.HostAttachmentClaim{}, fmt.Errorf("project authority attachment snapshot: %w", err) + } + + attachment, ok := projection.authority.Attachment(domain.AttachmentID(target.AttachmentID)) + if !ok || string(attachment.ID) != target.AttachmentID { + return host.HostAttachmentClaim{}, fmt.Errorf("durable attachment %q does not exist", target.AttachmentID) + } + if attachment.State != domain.Attached || attachment.Continuity != domain.ContinuityVerified { + return host.HostAttachmentClaim{}, fmt.Errorf("durable attachment %q is not attached with verified continuity", target.AttachmentID) + } + if attachment.IntegrationInstance != r.herdrIntegration { + return host.HostAttachmentClaim{}, fmt.Errorf("durable attachment %q belongs to a different Herdr integration", target.AttachmentID) + } + if attachment.Epoch.Kind != herdrTerminalEpochKind || attachment.Epoch.Scope != domain.EpochScopePane || + attachment.Epoch.Value == "" || attachment.Epoch.Value != target.TerminalID || attachment.Container == "" { + return host.HostAttachmentClaim{}, fmt.Errorf("durable attachment %q has mismatched Herdr terminal or pane identity", target.AttachmentID) + } + if !attachment.Process.Present() || attachment.Process.PID != target.PID || attachment.Process.StartedAt != target.StartTime { + return host.HostAttachmentClaim{}, fmt.Errorf("durable attachment %q has mismatched process birth", target.AttachmentID) + } + started, err := time.Parse(materialize.CaptureTimeLayout, attachment.Process.StartedAt) + if err != nil || started.UTC().Format(materialize.CaptureTimeLayout) != attachment.Process.StartedAt || started.UnixMilli() < 0 { + return host.HostAttachmentClaim{}, fmt.Errorf("durable attachment %q has a non-canonical process start time", target.AttachmentID) + } + + session, ok := projection.authority.Session(attachment.Session) + if !ok || session.ID != attachment.Session || session.State != domain.SessionActive || + session.Attachment != attachment.ID || session.Current == "" { + return host.HostAttachmentClaim{}, fmt.Errorf("durable attachment %q is not the owning active session attachment", target.AttachmentID) + } + instance, ok := projection.authority.Instance(session.Current) + if !ok || instance.ID != session.Current || instance.Session != session.ID || instance.State != domain.InstanceLive { + return host.HostAttachmentClaim{}, fmt.Errorf("durable attachment %q has no consistent current live instance", target.AttachmentID) + } + fingerprint := domain.Fingerprint{ + IntegrationInstance: attachment.IntegrationInstance, + Epoch: attachment.Epoch, + Container: attachment.Container, + Process: attachment.Process, + } + claimRef := fingerprint.ClaimRef() + active, held := projection.authority.ActiveClaim(claimRef) + if !held || active.Ref != claimRef || active.Session != session.ID || active.Instance != instance.ID || active.Degraded { + return host.HostAttachmentClaim{}, fmt.Errorf("durable attachment %q has no consistent active host claim", target.AttachmentID) + } + + return host.HostAttachmentClaim{ + Attachment: host.Attachment{ + IntegrationInstanceID: attachment.IntegrationInstance, + HostServerEpoch: herdr.NoServerEpoch, + HostContainerID: attachment.Epoch.Value, + PaneID: attachment.Container, + }, + LastKnownProcessBirth: host.ProcessBirthEvidence{ + PID: attachment.Process.PID, + StartTime: started, + StartTimeSource: herdr.StartTimeSourceProcfs, + }, + }, nil +} + +// ExactAttachmentHost is the complete Herdr seam needed by live fault +// induction: exact continuity validation and exact pane closure. +type ExactAttachmentHost interface { + host.HostAttachmentValidator + CloseExactAttachment(context.Context, host.HostAttachmentClaim) error +} + +var _ ExactAttachmentHost = (*herdr.Host)(nil) + +type attachmentClaimResolver interface { + Resolve(context.Context, ProcessIdentity) (host.HostAttachmentClaim, error) +} + +type exactProcessControl interface { + AcquireExactTarget(context.Context, ProcessIdentity) error + SendSIGSTOP(context.Context, ProcessIdentity) error + VerifyStopped(context.Context, ProcessIdentity) error + SendSIGKILL(context.Context, ProcessIdentity) error + Release(ProcessIdentity) error +} + +var _ exactProcessControl = (*LinuxProcessControl)(nil) + +type acquiredLiveTarget struct { + claim host.HostAttachmentClaim +} + +// LinuxHerdrLiveFaultController composes durable authority resolution, Herdr +// continuity/close operations, and Linux pidfds behind the launcher-neutral +// FaultController contract. +type LinuxHerdrLiveFaultController struct { + resolver attachmentClaimResolver + host ExactAttachmentHost + process exactProcessControl + exact *ExactFaultController + + mu sync.Mutex + acquired map[ProcessIdentity]acquiredLiveTarget +} + +var _ FaultController = (*LinuxHerdrLiveFaultController)(nil) + +// NewLinuxHerdrLiveFaultController constructs the production live controller. +// monotonic must be the same run-origin clock used by the checkpoint observer. +func NewLinuxHerdrLiveFaultController( + resolver *AuthorityAttachmentClaimResolver, + exactHost ExactAttachmentHost, + monotonic MonotonicClock, +) (*LinuxHerdrLiveFaultController, error) { + if resolver == nil { + return nil, fmt.Errorf("linux Herdr live fault controller requires an authority claim resolver") + } + return newLinuxHerdrLiveFaultController(resolver, exactHost, monotonic, NewLinuxProcessControl()) +} + +// newLinuxHerdrLiveFaultController is the test seam for the pidfd primitive. +// Production callers cannot inject an arbitrary signaling implementation. +func newLinuxHerdrLiveFaultController( + resolver attachmentClaimResolver, + exactHost ExactAttachmentHost, + monotonic MonotonicClock, + process exactProcessControl, +) (*LinuxHerdrLiveFaultController, error) { + switch { + case resolver == nil: + return nil, fmt.Errorf("linux Herdr live fault controller requires an authority claim resolver") + case exactHost == nil: + return nil, fmt.Errorf("linux Herdr live fault controller requires an exact attachment host") + case monotonic == nil: + return nil, fmt.Errorf("linux Herdr live fault controller requires the shared run-origin monotonic clock") + case process == nil: + return nil, fmt.Errorf("linux Herdr live fault controller requires Linux process control") + } + + c := &LinuxHerdrLiveFaultController{ + resolver: resolver, + host: exactHost, + process: process, + acquired: make(map[ProcessIdentity]acquiredLiveTarget), + } + exact, err := NewExactFaultController(ExactFaultControllerConfig{ + VerifyLiveTarget: c.verifyAndAcquireLiveTarget, + StopProcess: c.process.SendSIGSTOP, + VerifyStopped: c.process.VerifyStopped, + ClosePane: c.closeStablePane, + VerifyPaneAbsent: c.verifyPaneAbsentAndRelease, + Monotonic: monotonic, + }) + if err != nil { + return nil, err + } + c.exact = exact + return c, nil +} + +// SuspendExactProcess implements FaultController. +func (c *LinuxHerdrLiveFaultController) SuspendExactProcess(ctx context.Context, target ProcessIdentity) (ControlCheckpoint, error) { + c.mu.Lock() + defer c.mu.Unlock() + return c.exact.SuspendExactProcess(ctx, target) +} + +// CloseExactPane implements FaultController. +func (c *LinuxHerdrLiveFaultController) CloseExactPane(ctx context.Context, target ProcessIdentity) (ControlCheckpoint, error) { + c.mu.Lock() + defer c.mu.Unlock() + return c.exact.CloseExactPane(ctx, target) +} + +func (c *LinuxHerdrLiveFaultController) verifyAndAcquireLiveTarget(ctx context.Context, target ProcessIdentity) error { + claim, err := c.resolver.Resolve(ctx, target) + if err != nil { + return err + } + if err := requireClaimMatchesTarget(claim, target); err != nil { + return err + } + if _, exists := c.acquired[target]; exists { + return fmt.Errorf("exact live target is already acquired") + } + if err := c.requireSameLive(ctx, claim); err != nil { + return fmt.Errorf("validate exact Herdr continuity before pidfd acquisition: %w", err) + } + if err := c.process.AcquireExactTarget(ctx, target); err != nil { + return err + } + if err := c.requireSameLive(ctx, claim); err != nil { + releaseErr := c.process.Release(target) + return errors.Join( + fmt.Errorf("revalidate exact Herdr continuity after pidfd acquisition: %w", err), + wrapIfError("release pidfd after failed post-acquire validation", releaseErr), + ) + } + c.acquired[target] = acquiredLiveTarget{claim: claim} + return nil +} + +func (c *LinuxHerdrLiveFaultController) closeStablePane(ctx context.Context, target ProcessIdentity) error { + state, ok := c.acquired[target] + if !ok { + return fmt.Errorf("exact target has no acquired durable claim") + } + claim, err := c.resolver.Resolve(ctx, target) + if err != nil { + return fmt.Errorf("re-resolve durable claim before pane close: %w", err) + } + if err := requireClaimMatchesTarget(claim, target); err != nil { + return fmt.Errorf("revalidate target before pane close: %w", err) + } + if !sameAttachmentClaim(claim, state.claim) { + return fmt.Errorf("durable attachment claim changed after process suspension") + } + return c.host.CloseExactAttachment(ctx, claim) +} + +func (c *LinuxHerdrLiveFaultController) verifyPaneAbsentAndRelease(ctx context.Context, target ProcessIdentity) error { + state, ok := c.acquired[target] + if !ok { + return fmt.Errorf("exact target has no acquired durable claim") + } + continuity, err := c.host.ValidateAttachment(ctx, state.claim) + if err != nil { + return err + } + if continuity.Class != host.ContinuityPaneAbsent { + return fmt.Errorf("exact pane absence requires %s continuity, got %s", host.ContinuityPaneAbsent, continuity.Class) + } + if err := c.process.Release(target); err != nil { + return fmt.Errorf("release exact target after pane absence: %w", err) + } + delete(c.acquired, target) + return nil +} + +func (c *LinuxHerdrLiveFaultController) requireSameLive(ctx context.Context, claim host.HostAttachmentClaim) error { + continuity, err := c.host.ValidateAttachment(ctx, claim) + if err != nil { + return err + } + if continuity.Class != host.ContinuitySameLive || !continuity.SameProcess { + return fmt.Errorf("exact live target requires %s continuity, got %s", host.ContinuitySameLive, continuity.Class) + } + if !evidenceMatchesClaim(continuity.Evidence, claim) { + return fmt.Errorf("same-live continuity evidence does not exactly match the durable claim") + } + return nil +} + +// Cleanup conservatively releases every still-owned pidfd. It attempts normal +// exact pane closure only while durable identity and same-live Herdr evidence +// remain exact. SIGKILL is an escalation only for a failed normal exact close; +// it is sent through the already-owned pidfd and never stands in for proof of +// pane absence. +func (c *LinuxHerdrLiveFaultController) Cleanup(ctx context.Context) error { + c.mu.Lock() + defer c.mu.Unlock() + + var cleanupErrors []error + for target, state := range c.acquired { + var targetErrors []error + claim, err := c.resolver.Resolve(ctx, target) + if err != nil { + targetErrors = append(targetErrors, fmt.Errorf("re-resolve durable claim: %w", err)) + } else if err := requireClaimMatchesTarget(claim, target); err != nil { + targetErrors = append(targetErrors, fmt.Errorf("revalidate cleanup target: %w", err)) + } else if !sameAttachmentClaim(claim, state.claim) { + targetErrors = append(targetErrors, fmt.Errorf("durable attachment claim changed before cleanup")) + } else { + continuity, validateErr := c.host.ValidateAttachment(ctx, claim) + switch { + case validateErr != nil: + targetErrors = append(targetErrors, fmt.Errorf("validate exact Herdr continuity for cleanup: %w", validateErr)) + case continuity.Class == host.ContinuityPaneAbsent: + // Absence is independently proven; only descriptor release remains. + case continuity.Class != host.ContinuitySameLive || !continuity.SameProcess || !evidenceMatchesClaim(continuity.Evidence, claim): + targetErrors = append(targetErrors, fmt.Errorf("cleanup refused non-exact host continuity %s", continuity.Class)) + default: + closeErr := c.host.CloseExactAttachment(ctx, claim) + if closeErr != nil { + targetErrors = append(targetErrors, fmt.Errorf("normal exact pane close failed: %w", closeErr)) + } + absence, absenceErr := c.host.ValidateAttachment(ctx, claim) + // A failed exact close can itself report a continuity race. Recheck + // before escalation so a replaced or unproven host identity is + // never killed based only on the earlier same-live observation. + if closeErr != nil && absenceErr == nil && + absence.Class == host.ContinuitySameLive && absence.SameProcess && + evidenceMatchesClaim(absence.Evidence, claim) { + if killErr := c.process.SendSIGKILL(ctx, target); killErr != nil { + targetErrors = append(targetErrors, fmt.Errorf("exact pidfd SIGKILL escalation failed: %w", killErr)) + } + absence, absenceErr = c.host.ValidateAttachment(ctx, claim) + } + if absenceErr != nil { + targetErrors = append(targetErrors, fmt.Errorf("verify pane absence after cleanup close: %w", absenceErr)) + } else if absence.Class != host.ContinuityPaneAbsent { + targetErrors = append(targetErrors, fmt.Errorf("pane cleanup not proven absent: got %s", absence.Class)) + } + } + } + + if releaseErr := c.process.Release(target); releaseErr != nil { + targetErrors = append(targetErrors, fmt.Errorf("release exact target pidfd: %w", releaseErr)) + } + delete(c.acquired, target) + if err := errors.Join(targetErrors...); err != nil { + cleanupErrors = append(cleanupErrors, fmt.Errorf("cleanup exact target attachment %s: %w", target.AttachmentID, err)) + } + } + return errors.Join(cleanupErrors...) +} + +func requireClaimMatchesTarget(claim host.HostAttachmentClaim, target ProcessIdentity) error { + attachment := claim.Attachment + birth := claim.LastKnownProcessBirth + if attachment.IntegrationInstanceID == "" || attachment.HostServerEpoch != herdr.NoServerEpoch || + attachment.HostContainerID != target.TerminalID || attachment.PaneID == "" || + birth.PID != target.PID || birth.StartTimeSource != herdr.StartTimeSourceProcfs || + birth.StartTime.IsZero() || birth.StartTime.UTC().Format(materialize.CaptureTimeLayout) != target.StartTime { + return fmt.Errorf("durable attachment claim does not exactly match the requested process identity") + } + return nil +} + +func sameAttachmentClaim(left, right host.HostAttachmentClaim) bool { + return left.Attachment == right.Attachment && + left.LastKnownProcessBirth.PID == right.LastKnownProcessBirth.PID && + left.LastKnownProcessBirth.StartTimeSource == right.LastKnownProcessBirth.StartTimeSource && + left.LastKnownProcessBirth.StartTime.Equal(right.LastKnownProcessBirth.StartTime) +} + +func evidenceMatchesClaim(evidence host.Evidence, claim host.HostAttachmentClaim) bool { + attachment := claim.Attachment + birth := claim.LastKnownProcessBirth + return evidence.IntegrationInstanceID == attachment.IntegrationInstanceID && + evidence.HostServerEpoch == attachment.HostServerEpoch && + evidence.HostContainerID == attachment.HostContainerID && + evidence.PaneID == attachment.PaneID && + evidence.ProcessBirth.PID == birth.PID && + evidence.ProcessBirth.StartTimeSource == birth.StartTimeSource && + evidence.ProcessBirth.StartTime.Equal(birth.StartTime) +} + +func wrapIfError(operation string, err error) error { + if err == nil { + return nil + } + return fmt.Errorf("%s: %w", operation, err) +} diff --git a/internal/conformance/portablelauncher/live_controller_linux_test.go b/internal/conformance/portablelauncher/live_controller_linux_test.go new file mode 100644 index 0000000..2e49bfb --- /dev/null +++ b/internal/conformance/portablelauncher/live_controller_linux_test.go @@ -0,0 +1,650 @@ +//go:build linux + +package portablelauncher + +import ( + "context" + "errors" + "reflect" + "strings" + "testing" + "time" + + "github.com/procrastivity/duo/internal/domain" + "github.com/procrastivity/duo/internal/host" + "github.com/procrastivity/duo/internal/host/herdr" +) + +const ( + liveTestIntegration = "herdr:fixture-zeta" + liveTestStartedAt = "2026-09-18T14:23:41.370Z" +) + +var liveTestTarget = ProcessIdentity{ + PID: 4317, + StartTime: liveTestStartedAt, + TerminalID: "terminal-epoch-alpha", + AttachmentID: "attachment-durable-omega", +} + +func TestAuthorityAttachmentClaimResolverReturnsExactDurableClaim(t *testing.T) { + facts := liveAuthorityFacts() + resolver := newLiveAuthorityResolver(t, func(context.Context) ([]domain.Fact, error) { + return facts, nil + }) + + claim, err := resolver.Resolve(context.Background(), liveTestTarget) + if err != nil { + t.Fatalf("Resolve: %v", err) + } + started, err := time.Parse(testProcessBirthLayout, liveTestStartedAt) + if err != nil { + t.Fatalf("parse expected start time: %v", err) + } + want := host.HostAttachmentClaim{ + Attachment: host.Attachment{ + IntegrationInstanceID: liveTestIntegration, + HostServerEpoch: herdr.NoServerEpoch, + HostContainerID: liveTestTarget.TerminalID, + PaneID: "pane-container-zeta", + }, + LastKnownProcessBirth: host.ProcessBirthEvidence{ + PID: liveTestTarget.PID, + StartTime: started, + StartTimeSource: herdr.StartTimeSourceProcfs, + }, + } + if !sameAttachmentClaim(claim, want) { + t.Fatalf("claim = %+v, want %+v", claim, want) + } +} + +func TestAuthorityAttachmentClaimResolverRejectsStaleAndMismatchedFacts(t *testing.T) { + tests := []struct { + name string + mutate func([]domain.Fact, *ProcessIdentity) []domain.Fact + }{ + {name: "attachment ID is exact", mutate: func(f []domain.Fact, target *ProcessIdentity) []domain.Fact { + target.AttachmentID = "attachment-durable-alpha" + return f + }}, + {name: "attachment detached", mutate: func(f []domain.Fact, _ *ProcessIdentity) []domain.Fact { + liveAttachmentFact(f).Attachment.State = domain.Detached + return f + }}, + {name: "continuity unverified", mutate: func(f []domain.Fact, _ *ProcessIdentity) []domain.Fact { + liveAttachmentFact(f).Attachment.Continuity = domain.ContinuityUnverified + return f + }}, + {name: "wrong integration", mutate: func(f []domain.Fact, _ *ProcessIdentity) []domain.Fact { + liveAttachmentFact(f).Attachment.IntegrationInstance = "herdr:fixture-alpha" + return f + }}, + {name: "wrong epoch kind", mutate: func(f []domain.Fact, _ *ProcessIdentity) []domain.Fact { + liveAttachmentFact(f).Attachment.Epoch.Kind = "herdr.server" + return f + }}, + {name: "wrong epoch scope", mutate: func(f []domain.Fact, _ *ProcessIdentity) []domain.Fact { + liveAttachmentFact(f).Attachment.Epoch.Scope = domain.EpochScopeServer + return f + }}, + {name: "wrong terminal", mutate: func(f []domain.Fact, _ *ProcessIdentity) []domain.Fact { + liveAttachmentFact(f).Attachment.Epoch.Value = "terminal-epoch-beta" + return f + }}, + {name: "missing pane", mutate: func(f []domain.Fact, _ *ProcessIdentity) []domain.Fact { + liveAttachmentFact(f).Attachment.Container = "" + return f + }}, + {name: "wrong PID", mutate: func(f []domain.Fact, target *ProcessIdentity) []domain.Fact { + target.PID++ + return f + }}, + {name: "wrong start", mutate: func(f []domain.Fact, target *ProcessIdentity) []domain.Fact { + target.StartTime = "2026-09-18T14:23:41.371Z" + return f + }}, + {name: "noncanonical stored start", mutate: func(f []domain.Fact, target *ProcessIdentity) []domain.Fact { + liveAttachmentFact(f).Attachment.Process.StartedAt = "2026-09-18T14:23:41Z" + target.StartTime = "2026-09-18T14:23:41Z" + return f + }}, + {name: "inactive owning session", mutate: func(f []domain.Fact, _ *ProcessIdentity) []domain.Fact { + liveSessionFact(f).Session.State = domain.SessionInactive + return f + }}, + {name: "different current attachment", mutate: func(f []domain.Fact, _ *ProcessIdentity) []domain.Fact { + other := *liveAttachmentFact(f).Attachment + other.ID = "attachment-other" + return append(f, domain.Fact{Kind: domain.FactAttachmentCreated, SessionID: other.Session, Attachment: &other}) + }}, + {name: "current instance exited", mutate: func(f []domain.Fact, _ *ProcessIdentity) []domain.Fact { + return append(f, domain.Fact{Kind: domain.FactInstanceState, InstanceID: "instance-current-beta", State: string(domain.InstanceExited)}) + }}, + {name: "host claim missing", mutate: func(f []domain.Fact, _ *ProcessIdentity) []domain.Fact { + return removeLiveFactKind(f, domain.FactClaimHeld) + }}, + {name: "host claim wrong owner", mutate: func(f []domain.Fact, _ *ProcessIdentity) []domain.Fact { + liveClaimFact(f).Claim.Instance = "instance-other" + return f + }}, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + facts := liveAuthorityFacts() + target := liveTestTarget + facts = test.mutate(facts, &target) + resolver := newLiveAuthorityResolver(t, func(context.Context) ([]domain.Fact, error) { + return facts, nil + }) + if claim, err := resolver.Resolve(context.Background(), target); err == nil { + t.Fatalf("Resolve accepted stale or mismatched facts: %+v", claim) + } + }) + } +} + +func TestLinuxHerdrLiveControllerOrdersAuthorityHostAndPidfdOperations(t *testing.T) { + events := &liveEventLog{} + claim := liveTestClaim(t) + resolver := &fakeLiveClaimResolver{events: events, claim: claim} + exactHost := &fakeExactAttachmentHost{ + events: events, + validations: []fakeValidation{ + {evidence: sameLiveEvidence(claim)}, + {evidence: sameLiveEvidence(claim)}, + {evidence: host.ContinuityEvidence(host.ContinuityPaneAbsent, host.Evidence{})}, + }, + } + process := newFakeExactProcessControl(events) + controller := newLiveControllerForTest(t, resolver, exactHost, process) + + if _, err := controller.SuspendExactProcess(context.Background(), liveTestTarget); err != nil { + t.Fatalf("SuspendExactProcess: %v", err) + } + wantSuspend := []string{"resolve", "host.validate", "process.acquire", "host.validate", "process.stop", "process.verify-stopped"} + if got := events.copy(); !reflect.DeepEqual(got, wantSuspend) { + t.Fatalf("suspend operations = %v, want %v", got, wantSuspend) + } + + events.reset() + if _, err := controller.CloseExactPane(context.Background(), liveTestTarget); err != nil { + t.Fatalf("CloseExactPane: %v", err) + } + wantClose := []string{"process.verify-stopped", "resolve", "host.close", "host.validate", "process.release"} + if got := events.copy(); !reflect.DeepEqual(got, wantClose) { + t.Fatalf("close operations = %v, want %v", got, wantClose) + } +} + +func TestLinuxHerdrLiveControllerPostAcquireFailureReleasesPidfd(t *testing.T) { + events := &liveEventLog{} + claim := liveTestClaim(t) + resolver := &fakeLiveClaimResolver{events: events, claim: claim} + exactHost := &fakeExactAttachmentHost{ + events: events, + validations: []fakeValidation{ + {evidence: sameLiveEvidence(claim)}, + {evidence: host.ContinuityEvidence(host.ContinuityProcessReplaced, evidenceForClaim(claim))}, + }, + } + process := newFakeExactProcessControl(events) + controller := newLiveControllerForTest(t, resolver, exactHost, process) + + if _, err := controller.SuspendExactProcess(context.Background(), liveTestTarget); err == nil { + t.Fatal("SuspendExactProcess accepted failed post-acquire continuity") + } + want := []string{"resolve", "host.validate", "process.acquire", "host.validate", "process.release"} + if got := events.copy(); !reflect.DeepEqual(got, want) { + t.Fatalf("operations = %v, want %v", got, want) + } + if len(process.acquired) != 0 || len(controller.acquired) != 0 { + t.Fatalf("post-acquire failure retained process=%d controller=%d targets", len(process.acquired), len(controller.acquired)) + } +} + +func TestLinuxHerdrLiveControllerCloseRequiresStableDurableIdentity(t *testing.T) { + events := &liveEventLog{} + claim := liveTestClaim(t) + resolver := &fakeLiveClaimResolver{events: events, claim: claim} + exactHost := &fakeExactAttachmentHost{events: events, validations: []fakeValidation{ + {evidence: sameLiveEvidence(claim)}, {evidence: sameLiveEvidence(claim)}, + }} + process := newFakeExactProcessControl(events) + controller := newLiveControllerForTest(t, resolver, exactHost, process) + if _, err := controller.SuspendExactProcess(context.Background(), liveTestTarget); err != nil { + t.Fatalf("SuspendExactProcess: %v", err) + } + + resolver.claim.Attachment.PaneID = "pane-container-replaced" + events.reset() + if _, err := controller.CloseExactPane(context.Background(), liveTestTarget); err == nil || !strings.Contains(err.Error(), "claim changed") { + t.Fatalf("CloseExactPane error = %v, want changed durable claim", err) + } + want := []string{"process.verify-stopped", "resolve"} + if got := events.copy(); !reflect.DeepEqual(got, want) { + t.Fatalf("operations = %v, want %v", got, want) + } + if len(process.acquired) != 1 { + t.Fatalf("unstable close released pidfd; acquired = %d", len(process.acquired)) + } +} + +func TestLinuxHerdrLiveControllerRequiresPaneAbsenceBeforeRelease(t *testing.T) { + events := &liveEventLog{} + claim := liveTestClaim(t) + resolver := &fakeLiveClaimResolver{events: events, claim: claim} + exactHost := &fakeExactAttachmentHost{events: events, validations: []fakeValidation{ + {evidence: sameLiveEvidence(claim)}, + {evidence: sameLiveEvidence(claim)}, + {evidence: host.ContinuityEvidence(host.ContinuityUnproven, evidenceForClaim(claim))}, + }} + process := newFakeExactProcessControl(events) + controller := newLiveControllerForTest(t, resolver, exactHost, process) + if _, err := controller.SuspendExactProcess(context.Background(), liveTestTarget); err != nil { + t.Fatalf("SuspendExactProcess: %v", err) + } + + events.reset() + if _, err := controller.CloseExactPane(context.Background(), liveTestTarget); err == nil || !strings.Contains(err.Error(), "pane absence") { + t.Fatalf("CloseExactPane error = %v, want pane-absence refusal", err) + } + want := []string{"process.verify-stopped", "resolve", "host.close", "host.validate"} + if got := events.copy(); !reflect.DeepEqual(got, want) { + t.Fatalf("operations = %v, want %v", got, want) + } + if len(process.acquired) != 1 { + t.Fatalf("unproven absence released pidfd; acquired = %d", len(process.acquired)) + } +} + +func TestLinuxHerdrLiveControllerCleanupClosesThenReleasesWithoutKill(t *testing.T) { + controller, resolver, exactHost, process, events, claim := suspendedLiveController(t) + exactHost.validations = append(exactHost.validations, + fakeValidation{evidence: sameLiveEvidence(claim)}, + fakeValidation{evidence: host.ContinuityEvidence(host.ContinuityPaneAbsent, host.Evidence{})}, + ) + events.reset() + + if err := controller.Cleanup(context.Background()); err != nil { + t.Fatalf("Cleanup: %v", err) + } + want := []string{"resolve", "host.validate", "host.close", "host.validate", "process.release"} + if got := events.copy(); !reflect.DeepEqual(got, want) { + t.Fatalf("cleanup operations = %v, want %v", got, want) + } + if resolver.calls < 2 || len(process.acquired) != 0 || len(controller.acquired) != 0 { + t.Fatalf("cleanup retained target: resolver calls=%d process=%d controller=%d", resolver.calls, len(process.acquired), len(controller.acquired)) + } +} + +func TestLinuxHerdrLiveControllerCleanupCloseFailureUsesOnlyExactSIGKILLAndReportsFailure(t *testing.T) { + controller, _, exactHost, process, events, claim := suspendedLiveController(t) + closeFailure := errors.New("injected exact close failure") + exactHost.closeErr = closeFailure + exactHost.validations = append(exactHost.validations, + fakeValidation{evidence: sameLiveEvidence(claim)}, + fakeValidation{evidence: sameLiveEvidence(claim)}, + fakeValidation{evidence: sameLiveEvidence(claim)}, + ) + events.reset() + + err := controller.Cleanup(context.Background()) + if !errors.Is(err, closeFailure) || !strings.Contains(err.Error(), "pane cleanup not proven absent") { + t.Fatalf("Cleanup error = %v, want close and pane-cleanup failures", err) + } + want := []string{"resolve", "host.validate", "host.close", "host.validate", "process.kill", "host.validate", "process.release"} + if got := events.copy(); !reflect.DeepEqual(got, want) { + t.Fatalf("cleanup operations = %v, want %v", got, want) + } + if process.killCalls != 1 || len(process.acquired) != 0 { + t.Fatalf("cleanup kill calls=%d acquired=%d, want one exact kill and release", process.killCalls, len(process.acquired)) + } +} + +func TestLinuxHerdrLiveControllerCleanupNeverKillsWithoutExactSameLiveIdentity(t *testing.T) { + tests := []struct { + name string + mutate func(*fakeLiveClaimResolver, *fakeExactAttachmentHost, host.HostAttachmentClaim) + }{ + {name: "process replaced", mutate: func(_ *fakeLiveClaimResolver, h *fakeExactAttachmentHost, claim host.HostAttachmentClaim) { + h.validations = append(h.validations, fakeValidation{evidence: host.ContinuityEvidence(host.ContinuityProcessReplaced, evidenceForClaim(claim))}) + }}, + {name: "continuity unproven", mutate: func(_ *fakeLiveClaimResolver, h *fakeExactAttachmentHost, claim host.HostAttachmentClaim) { + h.validations = append(h.validations, fakeValidation{evidence: host.ContinuityEvidence(host.ContinuityUnproven, evidenceForClaim(claim))}) + }}, + {name: "same live with mismatched evidence", mutate: func(_ *fakeLiveClaimResolver, h *fakeExactAttachmentHost, claim host.HostAttachmentClaim) { + evidence := evidenceForClaim(claim) + evidence.PaneID = "pane-other" + h.validations = append(h.validations, fakeValidation{evidence: host.ContinuityEvidence(host.ContinuitySameLive, evidence)}) + }}, + {name: "durable claim changed", mutate: func(r *fakeLiveClaimResolver, _ *fakeExactAttachmentHost, _ host.HostAttachmentClaim) { + r.claim.Attachment.PaneID = "pane-other" + }}, + {name: "process replaced after close failure", mutate: func(_ *fakeLiveClaimResolver, h *fakeExactAttachmentHost, claim host.HostAttachmentClaim) { + h.closeErr = errors.New("close raced with replacement") + h.validations = append(h.validations, + fakeValidation{evidence: sameLiveEvidence(claim)}, + fakeValidation{evidence: host.ContinuityEvidence(host.ContinuityProcessReplaced, evidenceForClaim(claim))}, + ) + }}, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + controller, resolver, exactHost, process, events, claim := suspendedLiveController(t) + test.mutate(resolver, exactHost, claim) + events.reset() + + if err := controller.Cleanup(context.Background()); err == nil { + t.Fatal("Cleanup accepted missing or mismatched exact continuity") + } + if process.killCalls != 0 { + t.Fatalf("Cleanup sent %d kill signals without exact same-live identity", process.killCalls) + } + if got := events.copy(); len(got) == 0 || got[len(got)-1] != "process.release" { + t.Fatalf("cleanup operations = %v, want pidfd release on refusal", got) + } + }) + } +} + +func TestLinuxHerdrLiveControllerCleanupIsIdempotent(t *testing.T) { + controller, _, exactHost, _, events, claim := suspendedLiveController(t) + exactHost.validations = append(exactHost.validations, + fakeValidation{evidence: sameLiveEvidence(claim)}, + fakeValidation{evidence: host.ContinuityEvidence(host.ContinuityPaneAbsent, host.Evidence{})}, + ) + if err := controller.Cleanup(context.Background()); err != nil { + t.Fatalf("first Cleanup: %v", err) + } + events.reset() + if err := controller.Cleanup(context.Background()); err != nil { + t.Fatalf("second Cleanup: %v", err) + } + if got := events.copy(); len(got) != 0 { + t.Fatalf("second Cleanup operations = %v, want none", got) + } +} + +func TestLinuxHerdrLiveControllerConstructorsRejectMissingDependencies(t *testing.T) { + if _, err := NewAuthorityAttachmentClaimResolver(nil, liveTestIntegration); err == nil { + t.Fatal("resolver constructor accepted nil loader") + } + if _, err := NewAuthorityAttachmentClaimResolver(func(context.Context) ([]domain.Fact, error) { return nil, nil }, ""); err == nil { + t.Fatal("resolver constructor accepted empty integration") + } + if _, err := NewLinuxHerdrLiveFaultController(nil, &fakeExactAttachmentHost{}, func() time.Duration { return 0 }); err == nil { + t.Fatal("public controller constructor accepted nil resolver") + } + + resolver := &fakeLiveClaimResolver{} + exactHost := &fakeExactAttachmentHost{} + process := newFakeExactProcessControl(&liveEventLog{}) + tests := []struct { + name string + resolver attachmentClaimResolver + host ExactAttachmentHost + clock MonotonicClock + process exactProcessControl + }{ + {name: "resolver", host: exactHost, clock: func() time.Duration { return 0 }, process: process}, + {name: "host", resolver: resolver, clock: func() time.Duration { return 0 }, process: process}, + {name: "clock", resolver: resolver, host: exactHost, process: process}, + {name: "process", resolver: resolver, host: exactHost, clock: func() time.Duration { return 0 }}, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + if _, err := newLinuxHerdrLiveFaultController(test.resolver, test.host, test.clock, test.process); err == nil { + t.Fatalf("constructor accepted missing %s", test.name) + } + }) + } +} + +func liveAuthorityFacts() []domain.Fact { + const ( + sessionID = domain.SessionID("session-owner-alpha") + instanceID = domain.InstanceID("instance-current-beta") + ) + session := domain.Session{ + ID: sessionID, Workspace: "workspace-fixture", State: domain.SessionActive, + Current: instanceID, CreatedAt: "2026-09-18T14:23:40.000Z", + } + instance := domain.RuntimeInstance{ + ID: instanceID, Session: sessionID, State: domain.InstanceLive, + StartedAt: "2026-09-18T14:23:40.100Z", + } + attachment := domain.HostAttachment{ + ID: domain.AttachmentID(liveTestTarget.AttachmentID), Session: sessionID, + IntegrationInstance: liveTestIntegration, + Epoch: domain.HostEpoch{ + Kind: herdrTerminalEpochKind, Scope: domain.EpochScopePane, Value: liveTestTarget.TerminalID, + }, + Container: "pane-container-zeta", + Process: domain.ProcessBirth{ + Host: "fixture-host", PID: liveTestTarget.PID, StartedAt: liveTestTarget.StartTime, + Executable: "/fixture/bin/pi", + }, + State: domain.Attached, Continuity: domain.ContinuityVerified, + } + fingerprint := domain.Fingerprint{ + IntegrationInstance: attachment.IntegrationInstance, + Epoch: attachment.Epoch, + Container: attachment.Container, + Process: attachment.Process, + } + claim := domain.Claim{Ref: fingerprint.ClaimRef(), Session: sessionID, Instance: instanceID} + return []domain.Fact{ + {ID: "fact-session", Kind: domain.FactSessionCreated, Session: &session}, + {ID: "fact-instance", Kind: domain.FactInstanceStarted, SessionID: sessionID, Instance: &instance}, + {ID: "fact-attachment", Kind: domain.FactAttachmentCreated, SessionID: sessionID, Attachment: &attachment}, + {ID: "fact-claim", Kind: domain.FactClaimHeld, Claim: &claim}, + } +} + +func liveAttachmentFact(facts []domain.Fact) *domain.Fact { + for i := range facts { + if facts[i].Kind == domain.FactAttachmentCreated { + return &facts[i] + } + } + panic("test facts have no attachment") +} + +func liveSessionFact(facts []domain.Fact) *domain.Fact { + for i := range facts { + if facts[i].Kind == domain.FactSessionCreated { + return &facts[i] + } + } + panic("test facts have no session") +} + +func liveClaimFact(facts []domain.Fact) *domain.Fact { + for i := range facts { + if facts[i].Kind == domain.FactClaimHeld { + return &facts[i] + } + } + panic("test facts have no claim") +} + +func removeLiveFactKind(facts []domain.Fact, kind domain.FactKind) []domain.Fact { + out := make([]domain.Fact, 0, len(facts)) + for _, fact := range facts { + if fact.Kind != kind { + out = append(out, fact) + } + } + return out +} + +func newLiveAuthorityResolver(t *testing.T, load AuthoritySnapshotLoader) *AuthorityAttachmentClaimResolver { + t.Helper() + resolver, err := NewAuthorityAttachmentClaimResolver(load, liveTestIntegration) + if err != nil { + t.Fatalf("NewAuthorityAttachmentClaimResolver: %v", err) + } + return resolver +} + +func liveTestClaim(t *testing.T) host.HostAttachmentClaim { + t.Helper() + resolver := newLiveAuthorityResolver(t, func(context.Context) ([]domain.Fact, error) { + return liveAuthorityFacts(), nil + }) + claim, err := resolver.Resolve(context.Background(), liveTestTarget) + if err != nil { + t.Fatalf("Resolve test claim: %v", err) + } + return claim +} + +type liveEventLog struct { + events []string +} + +func (l *liveEventLog) add(event string) { l.events = append(l.events, event) } +func (l *liveEventLog) copy() []string { return append([]string(nil), l.events...) } +func (l *liveEventLog) reset() { l.events = nil } + +type fakeLiveClaimResolver struct { + events *liveEventLog + claim host.HostAttachmentClaim + err error + calls int +} + +func (r *fakeLiveClaimResolver) Resolve(context.Context, ProcessIdentity) (host.HostAttachmentClaim, error) { + if r.events != nil { + r.events.add("resolve") + } + r.calls++ + return r.claim, r.err +} + +type fakeValidation struct { + evidence host.HostContinuityEvidence + err error +} + +type fakeExactAttachmentHost struct { + events *liveEventLog + validations []fakeValidation + closeErr error +} + +func (h *fakeExactAttachmentHost) ValidateAttachment(context.Context, host.HostAttachmentClaim) (host.HostContinuityEvidence, error) { + if h.events != nil { + h.events.add("host.validate") + } + if len(h.validations) == 0 { + return host.HostContinuityEvidence{}, errors.New("unexpected host validation") + } + result := h.validations[0] + h.validations = h.validations[1:] + return result.evidence, result.err +} + +func (h *fakeExactAttachmentHost) CloseExactAttachment(context.Context, host.HostAttachmentClaim) error { + if h.events != nil { + h.events.add("host.close") + } + return h.closeErr +} + +type fakeExactProcessControl struct { + events *liveEventLog + acquired map[ProcessIdentity]bool + failures map[string]error + killCalls int +} + +func newFakeExactProcessControl(events *liveEventLog) *fakeExactProcessControl { + return &fakeExactProcessControl{events: events, acquired: make(map[ProcessIdentity]bool), failures: make(map[string]error)} +} + +func (p *fakeExactProcessControl) operation(name string, _ ProcessIdentity) error { + p.events.add("process." + name) + return p.failures[name] +} + +func (p *fakeExactProcessControl) AcquireExactTarget(_ context.Context, target ProcessIdentity) error { + if err := p.operation("acquire", target); err != nil { + return err + } + p.acquired[target] = true + return nil +} + +func (p *fakeExactProcessControl) SendSIGSTOP(_ context.Context, target ProcessIdentity) error { + return p.operation("stop", target) +} + +func (p *fakeExactProcessControl) VerifyStopped(_ context.Context, target ProcessIdentity) error { + return p.operation("verify-stopped", target) +} + +func (p *fakeExactProcessControl) SendSIGKILL(_ context.Context, target ProcessIdentity) error { + p.killCalls++ + return p.operation("kill", target) +} + +func (p *fakeExactProcessControl) Release(target ProcessIdentity) error { + err := p.operation("release", target) + delete(p.acquired, target) + return err +} + +func evidenceForClaim(claim host.HostAttachmentClaim) host.Evidence { + return host.Evidence{ + IntegrationInstanceID: claim.Attachment.IntegrationInstanceID, + HostServerEpoch: claim.Attachment.HostServerEpoch, + HostContainerID: claim.Attachment.HostContainerID, + PaneID: claim.Attachment.PaneID, + ProcessBirth: claim.LastKnownProcessBirth, + } +} + +func sameLiveEvidence(claim host.HostAttachmentClaim) host.HostContinuityEvidence { + return host.ContinuityEvidence(host.ContinuitySameLive, evidenceForClaim(claim)) +} + +func newLiveControllerForTest( + t *testing.T, + resolver attachmentClaimResolver, + exactHost ExactAttachmentHost, + process exactProcessControl, +) *LinuxHerdrLiveFaultController { + t.Helper() + controller, err := newLinuxHerdrLiveFaultController(resolver, exactHost, func() time.Duration { return 17 * time.Millisecond }, process) + if err != nil { + t.Fatalf("newLinuxHerdrLiveFaultController: %v", err) + } + return controller +} + +func suspendedLiveController(t *testing.T) ( + *LinuxHerdrLiveFaultController, + *fakeLiveClaimResolver, + *fakeExactAttachmentHost, + *fakeExactProcessControl, + *liveEventLog, + host.HostAttachmentClaim, +) { + t.Helper() + events := &liveEventLog{} + claim := liveTestClaim(t) + resolver := &fakeLiveClaimResolver{events: events, claim: claim} + exactHost := &fakeExactAttachmentHost{events: events, validations: []fakeValidation{ + {evidence: sameLiveEvidence(claim)}, {evidence: sameLiveEvidence(claim)}, + }} + process := newFakeExactProcessControl(events) + controller := newLiveControllerForTest(t, resolver, exactHost, process) + if _, err := controller.SuspendExactProcess(context.Background(), liveTestTarget); err != nil { + t.Fatalf("SuspendExactProcess: %v", err) + } + return controller, resolver, exactHost, process, events, claim +} diff --git a/internal/conformance/portablelauncher/model.go b/internal/conformance/portablelauncher/model.go new file mode 100644 index 0000000..2f24bc0 --- /dev/null +++ b/internal/conformance/portablelauncher/model.go @@ -0,0 +1,214 @@ +// Package portablelauncher owns the launcher-neutral portable launcher +// conformance scenario, capture model, oracle, and offline validator. +package portablelauncher + +import "encoding/json" + +// Public schema and identity constants for the portable-launcher suite. +const ( + ResultSchema = "duo.portable-launcher-conformance-result/v1" + ScenarioSchema = "duo.portable-launcher-conformance-scenario/v1" + EvidenceSchema = "duo.portable-launcher-conformance-evidence/v1" + IndexSchema = "duo.portable-launcher-conformance-evidence-index/v1" + ScenarioName = "portable-launcher-delegation" + ScenarioID = "portable-launcher-delegation/v1" + ScrubPolicy = "portable-launcher-evidence/v1" +) + +// Result is one complete portable-launcher conformance result. +type Result struct { + Schema string `json:"schema"` + Suite SuiteIdentity `json:"suite"` + Run RunIdentity `json:"run"` + Pins Pins `json:"pins"` + Stages []StageResult `json:"stages"` + Summary Summary `json:"summary"` + Scrub ScrubRecord `json:"scrub"` +} + +// SuiteIdentity pins the scenario and oracle used to interpret a result. +type SuiteIdentity struct { + Name string `json:"name"` + Revision int `json:"revision"` + ManifestDigest string `json:"manifest_digest"` + OracleDigest string `json:"oracle_digest"` +} + +// RunIdentity records the isolated host and invocation properties of a run. +type RunIdentity struct { + RunID string `json:"run_id"` + ObservedAt string `json:"observed_at"` + HostOS string `json:"host_os"` + HostArch string `json:"host_arch"` + FixtureRoot string `json:"fixture_root"` + PluginsEnabled bool `json:"plugins_enabled"` + MCPEnabled bool `json:"mcp_enabled"` + TerminalInputUsed bool `json:"terminal_input_used"` +} + +// Pins records every product and runtime identity required to reproduce a run. +type Pins struct { + Launcher LauncherPin `json:"launcher"` + Duo DuoPin `json:"duo"` + Skill SkillPin `json:"skill"` + Config ConfigPin `json:"config"` + Authority AuthorityPin `json:"authority"` + Host HostPin `json:"host"` + Runtime RuntimePin `json:"runtime"` + Model ModelPin `json:"model"` + ExternalSchema ExternalSchemaPin `json:"external_schema"` +} + +// LauncherPin identifies the tested outer launcher executable. +type LauncherPin struct { + Name string `json:"name"` + Version string `json:"version"` + ExecutableSHA256 string `json:"executable_sha256"` +} + +// DuoPin identifies the Duo build under test. +type DuoPin struct { + Version string `json:"version"` + Commit string `json:"commit"` + BuildDate string `json:"build_date"` + ExecutableSHA256 string `json:"executable_sha256"` +} + +// SkillPin identifies the projected portable-launcher skill installation. +type SkillPin struct { + Name string `json:"name"` + FormatVersion string `json:"format_version"` + ContentDigest string `json:"content_digest"` + InstallationID string `json:"installation_id"` +} + +// ConfigPin identifies the effective Duo configuration. +type ConfigPin struct { + Schema string `json:"schema"` + EffectiveDigest string `json:"effective_digest"` +} + +// AuthorityPin records the authority-store state around a run. +type AuthorityPin struct { + SchemaVersion int `json:"schema_version"` + StoreDigestBefore string `json:"store_digest_before"` + StoreDigestAfter string `json:"store_digest_after"` +} + +// HostPin identifies the host executable and protocol. +type HostPin struct { + Name string `json:"name"` + Version string `json:"version"` + Protocol string `json:"protocol"` + SchemaDigest string `json:"schema_digest"` + ExecutableSHA256 string `json:"executable_sha256"` +} + +// RuntimePin identifies the admitted runtime and delivery adapter evidence. +type RuntimePin struct { + Name string `json:"name"` + Version string `json:"version"` + Format string `json:"format"` + AdapterBuild string `json:"adapter_build"` + ConformanceRecord string `json:"conformance_record"` + ExecutableSHA256 string `json:"executable_sha256"` + DeliveryAssetSHA256 string `json:"delivery_asset_sha256"` +} + +// ModelPin identifies the selected provider and model line. +type ModelPin struct { + Provider string `json:"provider"` + ModelLine string `json:"model_line"` +} + +// ExternalSchemaPin identifies the public Duo envelope schema. +type ExternalSchemaPin struct { + Identity string `json:"identity"` + Digest string `json:"digest"` +} + +// StageResult records one ordered scenario stage and its evidence. +type StageResult struct { + Sequence int `json:"sequence"` + Stage string `json:"stage"` + Case string `json:"case"` + Verdict string `json:"verdict"` + Outcome string `json:"outcome"` + StartedOffsetMS int64 `json:"started_offset_ms"` + DurationMS int64 `json:"duration_ms"` + Assertions []Assertion `json:"assertions"` + Evidence []string `json:"evidence"` + Error *StageError `json:"error"` +} + +// Assertion records an expected value and the independently derived actual. +type Assertion struct { + ID string `json:"id"` + Expected map[string]any `json:"expected"` + Actual map[string]any `json:"actual"` + Matched bool `json:"matched"` +} + +// StageError records a failed stage's stable error metadata. +type StageError struct { + Code string `json:"code"` + Message string `json:"message"` + Effect string `json:"effect"` + Retry Retry `json:"retry"` +} + +// Retry records whether and how a caller may retry after a stage error. +type Retry struct { + Safe bool `json:"safe"` + Action string `json:"action"` +} + +// Summary records the whole-run verdict and first failure. +type Summary struct { + Verdict string `json:"verdict"` + FirstFailedStage *string `json:"first_failed_stage"` + FirstFailedCase *string `json:"first_failed_case"` +} + +// ScrubRecord records the evidence-scrubbing policy outcome. +type ScrubRecord struct { + Status string `json:"status"` + Policy string `json:"policy"` + Findings []string `json:"findings"` +} + +// EvidenceIndex lists the content-addressed blobs supporting a result. +type EvidenceIndex struct { + Schema string `json:"schema"` + Policy string `json:"policy"` + Blobs []BlobEntry `json:"blobs"` +} + +// BlobEntry identifies one scrubbed evidence blob and its producer. +type BlobEntry struct { + Reference string `json:"ref"` + MediaType string `json:"media_type"` + Bytes int64 `json:"bytes"` + SHA256 string `json:"sha256"` + ScrubStatus string `json:"scrub_status"` + Producer string `json:"producer"` +} + +// Evidence contains independently captured observations. +type Evidence struct { + Schema string `json:"schema"` + ScrubStatus string `json:"scrub_status"` + Observations []Observation `json:"observations"` +} + +// Observation binds captured evidence to one stage assertion. +type Observation struct { + Sequence int `json:"sequence"` + AssertionID string `json:"assertion_id"` + Source string `json:"source"` + Evidence map[string]any `json:"evidence"` +} + +// RawResult is the exact, untrusted result object collected from a launcher. +// It is intentionally not interpreted by a driver. +type RawResult = json.RawMessage diff --git a/internal/conformance/portablelauncher/oracle.go b/internal/conformance/portablelauncher/oracle.go new file mode 100644 index 0000000..83c9539 --- /dev/null +++ b/internal/conformance/portablelauncher/oracle.go @@ -0,0 +1,445 @@ +package portablelauncher + +import ( + "encoding/json" + "fmt" + "reflect" + "slices" + "sort" + + "github.com/procrastivity/duo/internal/registry" +) + +// OracleSpec identifies the trusted derivation rules and assertion inventory +// used by the offline validator. +type OracleSpec struct { + Revision int `json:"revision"` + Derivation string `json:"derivation"` + AssertionIDs []string `json:"assertion_ids"` + DuoOperations map[string]string `json:"duo_operations"` + BlockedPrerequisite string `json:"blocked_prerequisite"` + TimeoutMinimumMS int64 `json:"timeout_minimum_ms"` + TimeoutMaximumMS int64 `json:"timeout_maximum_ms"` + CompleteRunMaximumMS int64 `json:"complete_run_maximum_ms"` + IndependentEvidenceOnly bool `json:"independent_evidence_only"` + LauncherVerdictUntrusted bool `json:"launcher_verdict_untrusted"` +} + +// CanonicalOracle returns the trusted oracle definition for the suite. +func CanonicalOracle() OracleSpec { + ids := map[string]bool{ + "stage.completed_within_deadline": true, + "stage.failure": true, + "prerequisite.not_reached": true, + "blocked.induction_available": true, + } + for _, step := range CanonicalScenario().Steps { + for _, assertion := range step.Assertions { + ids[assertion.ID] = true + } + } + ordered := make([]string, 0, len(ids)) + for id := range ids { + ordered = append(ordered, id) + } + sort.Strings(ordered) + return OracleSpec{ + Revision: 1, Derivation: "portable-launcher-evidence-derivation/v1", AssertionIDs: ordered, + DuoOperations: portableOperationNames(), + BlockedPrerequisite: "supported_admitted_then_blocked_producer", + TimeoutMinimumMS: 20_000, TimeoutMaximumMS: 22_500, + CompleteRunMaximumMS: 600_000, + IndependentEvidenceOnly: true, LauncherVerdictUntrusted: true, + } +} + +func portableOperationNames() map[string]string { + assertionCLI := map[string][]string{ + "bind.runtime_identity": {"session", "show"}, + "command.delivery_integrity": {"prompt", "show"}, + "launch.resolution": {"session", "launch"}, + "observe.assistant_text": {"conversation", "list"}, + "observe.restart_text": {"conversation", "list"}, + "send.delivery": {"prompt", "send"}, + "send.idempotency_conflict": {"prompt", "send"}, + } + operations := make(map[string]string, len(assertionCLI)) + for assertionID, cli := range assertionCLI { + for _, descriptor := range registry.All() { + if slices.Equal(descriptor.CLI, cli) { + operations[assertionID] = descriptor.Name + break + } + } + if operations[assertionID] == "" { + panic(fmt.Sprintf("portable launcher oracle: no registry operation for CLI path %v", cli)) + } + } + return operations +} + +// OracleDigest returns the digest of the canonical oracle bytes. +func OracleDigest() string { + b, err := json.Marshal(CanonicalOracle()) + if err != nil { + panic(fmt.Sprintf("portable launcher oracle: %v", err)) + } + return Digest(b) +} + +type capturedObservation struct { + Observation + Reference string +} + +func validateAssertions(result Result, scenario Scenario, observations map[observationKey]capturedObservation, problems *Problems) { + allowed := map[string]bool{} + for _, id := range CanonicalOracle().AssertionIDs { + allowed[id] = true + } + + limit := len(result.Stages) + if len(scenario.Steps) < limit { + limit = len(scenario.Steps) + } + for i := 0; i < limit; i++ { + stage := result.Stages[i] + step := scenario.Steps[i] + seen := map[string]bool{} + for _, assertion := range stage.Assertions { + path := fmt.Sprintf("stages[%d].assertions[%s]", i, assertion.ID) + if !allowed[assertion.ID] { + problems.Add(path + ": unknown oracle assertion") + } + if seen[assertion.ID] { + problems.Add(path + ": duplicate oracle assertion") + } + seen[assertion.ID] = true + obs, ok := observations[observationKey{Sequence: stage.Sequence, AssertionID: assertion.ID}] + if !ok { + problems.Add(path + ": no independent evidence observation") + continue + } + if !containsString(stage.Evidence, obs.Reference) { + problems.Add(path + ": observation is not in evidence referenced by this stage") + } + if obs.Source == "launcher_result" || obs.Source == "launcher_final_text" || obs.Source == "terminal" { + problems.Add(path + ": untrusted source cannot satisfy an oracle assertion") + } + actual := deriveObservationActual(result, obs.Observation, path, problems) + if !reflect.DeepEqual(assertion.Actual, actual) { + problems.Add(path + ": actual does not match independent evidence") + } + wantMatched := reflect.DeepEqual(assertion.Expected, assertion.Actual) + if assertion.Matched != wantMatched { + problems.Add(path + ": matched is inconsistent with expected and actual") + } + } + + switch stage.Verdict { + case "pass": + if len(stage.Assertions) != len(step.Assertions) { + problems.Add(fmt.Sprintf("stages[%d]: passing stage has %d assertions, want %d", i, len(stage.Assertions), len(step.Assertions))) + } + for _, expected := range step.Assertions { + var found *Assertion + for j := range stage.Assertions { + if stage.Assertions[j].ID == expected.ID { + found = &stage.Assertions[j] + break + } + } + if found == nil { + problems.Add(fmt.Sprintf("stages[%d]: missing required assertion %q", i, expected.ID)) + continue + } + if !reflect.DeepEqual(found.Expected, expected.Expected) { + problems.Add(fmt.Sprintf("stages[%d].assertions[%s]: expectation is not the trusted scenario value", i, expected.ID)) + } + if !found.Matched { + problems.Add(fmt.Sprintf("stages[%d].assertions[%s]: passing stage has unmatched assertion", i, expected.ID)) + } + } + case "fail": + if len(stage.Assertions) == 0 { + problems.Add(fmt.Sprintf("stages[%d]: failed stage has no independently checkable assertion", i)) + } + allMatched := true + for _, assertion := range stage.Assertions { + allMatched = allMatched && assertion.Matched + } + if allMatched { + problems.Add(fmt.Sprintf("stages[%d]: failed stage has no failed assertion", i)) + } + } + } +} + +func deriveObservationActual(result Result, obs Observation, path string, problems *Problems) map[string]any { + if _, present := obs.Evidence["oracle_actual"]; present { + problems.Add(path + ": evidence contains forbidden oracle_actual shortcut") + } + switch obs.Source { + case "duo_envelope": + envelope, ok := obs.Evidence["envelope"].(map[string]any) + if !ok || envelope["schema"] != "duo.external/v1" || stringValue(envelope["request_id"]) == "" || !operationPattern.MatchString(stringValue(envelope["operation"])) { + problems.Add(path + ": malformed duo.external/v1 evidence envelope") + return nil + } + if err := ValidateExternalEnvelope(envelope); err != nil { + problems.Add(path + ": duo.external/v1 schema violation: " + err.Error()) + } + wantOperation, known := CanonicalOracle().DuoOperations[obs.AssertionID] + if !known || envelope["operation"] != wantOperation { + problems.Add(path + ": Duo operation does not match oracle assertion") + } + _, hasResult := envelope["result"] + _, hasError := envelope["error"] + if hasResult == hasError { + problems.Add(path + ": Duo evidence must carry exactly one of result or error") + } + return deriveDuoActual(obs.AssertionID, envelope, obs.Evidence, path, problems) + case "controller": + monotonic, hasMonotonic := obs.Evidence["monotonic_ms"] + if obs.Evidence["kind"] != "controller.checkpoint" || !hasMonotonic || !isNumber(monotonic) || int64Number(monotonic) < 0 { + problems.Add(path + ": malformed controller checkpoint") + } + if (obs.AssertionID == "observe.exited" || obs.AssertionID == "observe.timeout") && obs.Evidence["target_verified"] != true { + problems.Add(path + ": exact process/attachment target was not verified") + } + if obs.AssertionID == "observe.timeout" && obs.Evidence["timeout_probe_supported"] != true { + problems.Add(path + ": prerequisite.timeout_induction_unavailable") + } + if obs.AssertionID == "stage.completed_within_deadline" { + started, startOK := numberValue(obs.Evidence["started_ms"]) + finished, finishOK := numberValue(obs.Evidence["finished_ms"]) + deadline, deadlineOK := numberValue(obs.Evidence["deadline_ms"]) + if !startOK || !finishOK || !deadlineOK || finished < started || deadline < 0 { + problems.Add(path + ": malformed monotonic deadline checkpoint") + return nil + } + if obs.Sequence < 1 || obs.Sequence > len(result.Stages) || obs.Sequence > len(CanonicalScenario().Steps) { + problems.Add(path + ": deadline checkpoint sequence is outside the scenario") + return nil + } + stage := result.Stages[obs.Sequence-1] + step := CanonicalScenario().Steps[obs.Sequence-1] + wantDeadline := float64(step.DeadlineMS) + if step.Case == "timeout" && step.Stage == "observe" { + wantDeadline = float64(CanonicalOracle().TimeoutMaximumMS) + } + if started != float64(stage.StartedOffsetMS) || finished-started != float64(stage.DurationMS) || deadline != wantDeadline { + problems.Add(path + ": deadline checkpoint does not match the stage timing contract") + } + return map[string]any{"within_deadline": finished-started <= deadline} + } + measurements, ok := obs.Evidence["measurements"].(map[string]any) + if !ok { + problems.Add(path + ": controller checkpoint has no measurements") + return nil + } + if _, forbidden := measurements["oracle_actual"]; forbidden { + problems.Add(path + ": controller measurements contain forbidden oracle_actual shortcut") + } + if obs.AssertionID == "setup.fixture_isolated_and_pinned" { + pinsJSON, err := json.Marshal(result.Pins) + if err != nil || obs.Evidence["pins_digest"] != Digest(pinsJSON) { + problems.Add(path + ": controller pin evidence does not match result pins") + } + if obs.Evidence["credentials_present"] != true || obs.Evidence["credential_provider"] != "openai-codex" || !oneOf(stringValue(obs.Evidence["credential_source_kind"]), "ci_secret", "operator_copy", "environment") { + problems.Add(path + ": isolated credential metadata is missing or invalid") + } + if obs.Evidence["pi_no_extensions"] != true { + problems.Add(path + ": Pi extension boundary was not proved") + } + } + return measurements + case "launcher_event": + arguments, argumentsOK := obs.Evidence["arguments"].([]any) + if obs.Evidence["kind"] != "skill.discovery" || obs.Evidence["task_digest"] != Digest(CanonicalTaskBytes) || !argumentsOK || len(arguments) == 0 { + problems.Add(path + ": malformed launcher discovery event") + } + skill, ok := obs.Evidence["skill"].(map[string]any) + if !ok { + problems.Add(path + ": launcher discovery event has no skill identity") + return nil + } + return skill + default: + problems.Add(path + ": unknown evidence source") + return nil + } +} + +func deriveDuoActual(assertionID string, envelope, wrapper map[string]any, path string, problems *Problems) map[string]any { + result, _ := envelope["result"].(map[string]any) + switch assertionID { + case "launch.resolution": + argvHasPrompt, argvRecorded := wrapper["argv_has_prompt"].(bool) + if !argvRecorded { + problems.Add(path + ": launch evidence does not record prompt argv presence") + } + leaves, _ := result["leaves"].([]any) + selected := 0 + runtime := "" + for _, raw := range leaves { + leaf, _ := raw.(map[string]any) + if leaf["outcome"] == "selected" { + selected++ + runtime = stringValue(leaf["agent_runtime"]) + } + } + host, _ := result["host"].(map[string]any) + return map[string]any{"runtime": runtime, "host": stringValue(host["kind"]), "selected_leaves": float64(selected), "prelaunch_prompt": argvHasPrompt} + case "bind.runtime_identity": + instances, instancesOK := wrapper["authority_instance_ids"].([]any) + correlations, correlationsOK := wrapper["active_correlation_runtime_ids"].([]any) + attachments, _ := result["attachments"].([]any) + runtimeInstanceID := stringValue(result["runtime_instance_id"]) + claimed := len(attachments) == 1 + if claimed { + attachment, _ := attachments[0].(map[string]any) + birth, _ := attachment["process_birth"].(map[string]any) + claimed = attachment["state"] == "attached" && attachment["claim_held"] == true && int64Number(birth["pid"]) > 0 && stringValue(birth["started_at"]) != "" + } + if !instancesOK || !correlationsOK { + problems.Add(path + ": bind evidence lacks authority relationships") + } + if runtimeInstanceID == "" || len(instances) != 1 || len(correlations) != 1 || stringValue(instances[0]) != runtimeInstanceID || stringValue(correlations[0]) != runtimeInstanceID { + problems.Add(path + ": bind authority relationships do not name the inspected runtime") + } + return map[string]any{"instances": float64(len(instances)), "correlations": float64(len(correlations)), "attachment_claimed": claimed} + case "send.delivery": + attempts, _ := result["attempts"].([]any) + realization, recordedResult := "", "" + if len(attempts) == 1 { + attempt, _ := attempts[0].(map[string]any) + realization = stringValue(attempt["realization"]) + recordedResult = stringValue(attempt["recorded_result"]) + } + retry, _ := result["retry"].(map[string]any) + milestones, _ := result["milestones"].(map[string]any) + return map[string]any{ + "state": result["responsibility_state"], "queue_policy": result["queue_policy"], + "attempts": float64(len(attempts)), "realization": realization, "recorded_result": recordedResult, + "activity_observed": milestones["activity_observed"], "acknowledged": milestones["acknowledged"], + "retry_safe": retry["safe"], "retry_action": retry["action"], + } + case "observe.assistant_text", "observe.restart_text": + items, _ := result["items"].([]any) + blocks := 0 + complete := true + text := "" + recordID := "" + for _, rawItem := range items { + item, _ := rawItem.(map[string]any) + if item["author_role"] != "agent" { + continue + } + for _, rawBlock := range sliceValue(item["blocks"]) { + block, _ := rawBlock.(map[string]any) + if block["type"] != "text" { + continue + } + content, _ := block["content"].(map[string]any) + blocks++ + text += stringValue(content["text"]) + } + complete = complete && item["completion"] == "source_record_complete" + recordID = stringValue(item["record_id"]) + } + if assertionID == "observe.restart_text" { + return map[string]any{"text_digest": Digest([]byte(text)), "same_record": recordID != "" && recordID == stringValue(wrapper["baseline_record_id"])} + } + return map[string]any{"assistant_blocks": float64(blocks), "text_digest": Digest([]byte(text)), "source_complete": complete} + case "send.idempotency_conflict": + errorObject, _ := envelope["error"].(map[string]any) + retry, _ := errorObject["retry"].(map[string]any) + details, _ := errorObject["details"].(map[string]any) + target, _ := errorObject["target"].(map[string]any) + return map[string]any{ + "code": errorObject["code"], "effect": errorObject["effect"], "retry_safe": retry["safe"], + "retry_action": retry["action"], "idempotency_key": details["idempotency_key"], + "target_original": stringValue(target["id"]) != "" && target["id"] == wrapper["original_command_id"], + "existing_digest": details["existing_digest"], "request_digest": details["request_digest"], + } + case "command.delivery_integrity": + attempts, _ := result["attempts"].([]any) + realization, recordedResult := "", "" + if len(attempts) == 1 { + attempt, _ := attempts[0].(map[string]any) + realization = stringValue(attempt["realization"]) + recordedResult = stringValue(attempt["recorded_result"]) + } + retry, _ := result["retry"].(map[string]any) + return map[string]any{ + "state": result["responsibility_state"], "queue_policy": result["queue_policy"], + "attempts": float64(len(attempts)), "realization": realization, "recorded_result": recordedResult, + "retry_safe": retry["safe"], "retry_action": retry["action"], "unchanged": wrapper["unchanged"] == true, + } + default: + problems.Add(path + ": assertion has no Duo evidence derivation") + return nil + } +} + +func containsString(values []string, want string) bool { + for _, value := range values { + if value == want { + return true + } + } + return false +} + +func numberValue(value any) (float64, bool) { + switch n := value.(type) { + case float64: + return n, true + case int: + return float64(n), true + case int64: + return float64(n), true + default: + return 0, false + } +} + +func sliceValue(value any) []any { + values, _ := value.([]any) + return values +} + +// TimeoutEvidence records the independent measurements used to validate the +// suite's intentional observation timeout. +type TimeoutEvidence struct { + ElapsedMS int64 + ProcessSucceeded bool + StoppedVerified bool + Delivered bool + Attempts int + AssistantBlocks int + OuterTimedOut bool +} + +// ValidateIntentionalTimeout is the controller/oracle boundary for the +// intentional observation timeout. It performs no live signal or host probe. +func ValidateIntentionalTimeout(e TimeoutEvidence) error { + var p Problems + if e.ElapsedMS < 20_000 || e.ElapsedMS > 22_500 { + p.Add(fmt.Sprintf("timeout observation elapsed_ms=%d outside 20000..22500", e.ElapsedMS)) + } + if !e.ProcessSucceeded || e.OuterTimedOut { + p.Add("timeout outcome came from launcher/process failure") + } + if !e.StoppedVerified { + p.Add("timeout process suspension was not independently verified") + } + if !e.Delivered || e.Attempts != 1 { + p.Add("timeout command was not delivered exactly once") + } + if e.AssistantBlocks != 0 { + p.Add("timeout observation contains an assistant response") + } + return p.Err() +} diff --git a/internal/conformance/portablelauncher/orchestrate.go b/internal/conformance/portablelauncher/orchestrate.go new file mode 100644 index 0000000..247c997 --- /dev/null +++ b/internal/conformance/portablelauncher/orchestrate.go @@ -0,0 +1,205 @@ +//go:build linux + +package portablelauncher + +import ( + "context" + "errors" + "fmt" + "path/filepath" +) + +// OrchestrationInput is the reusable launcher-neutral live-suite entry point. +// Facts contains normalized raw facts and timings only; its Run, Pins, Record, +// Commands, and Cleanup.RootRemoved fields are overwritten by orchestration. +type OrchestrationInput struct { + Setup SetupInput + Runner RunnerInput + Run RunIdentity + Pins Pins + Facts AssemblyInput + RunOriginBootTimeNS int64 + Destination string +} + +// OrchestrationSeams make the complete pipeline deterministic in offline +// tests. Zero-valued fields use the production implementations. +type OrchestrationSeams struct { + Prepare func(SetupInput) (*Fixture, error) + Run func(context.Context, RunnerInput) RunRecord + Load func(string, int64, RecordedExecutable) ([]RecordedCommand, error) + Assemble func(AssemblyInput) ([]CommonStageCapture, error) + Collect func(CollectorInput) (Result, *Capture) + Validate func(Result, *Capture) error + Write func(string, Result, *Capture) error +} + +// Orchestrate prepares one fixture, runs the common controller, loads the +// command journals before fixture removal, assembles trusted stages, collects +// and offline-validates the result, then atomically writes the bundle. Fixture +// cleanup is always completed before assembly and remains an independent final +// canonical stage. +func Orchestrate(ctx context.Context, input OrchestrationInput) (Result, error) { + return orchestrate(ctx, input, OrchestrationSeams{}) +} + +func orchestrate(ctx context.Context, input OrchestrationInput, seams OrchestrationSeams) (Result, error) { + seams = defaultOrchestrationSeams(seams) + if input.Destination == "" { + return Result{}, fmt.Errorf("portable launcher orchestration requires a bundle destination") + } + if input.RunOriginBootTimeNS <= 0 { + return Result{}, fmt.Errorf("portable launcher orchestration requires a positive command run origin") + } + if input.Setup.Duo != input.Pins.Duo { + return Result{}, fmt.Errorf("portable launcher orchestration: setup Duo identity does not match result pins") + } + + fixture, err := seams.Prepare(input.Setup) + if err != nil { + if fixture != nil { + _ = cleanupFixture(ctx, fixture, input.Runner.InspectResources, input.Runner.ExportEvidence) + } + return Result{}, fmt.Errorf("portable launcher orchestration: prepare fixture: %w", err) + } + if fixture == nil { + return Result{}, fmt.Errorf("portable launcher orchestration: prepare fixture returned nil") + } + + if input.Pins.Skill.InstallationID == "" { + input.Pins.Skill.InstallationID = fixture.InstallationID + } else if input.Pins.Skill.InstallationID != fixture.InstallationID { + cleanupErr := cleanupFixture(ctx, fixture, input.Runner.InspectResources, input.Runner.ExportEvidence) + return Result{}, errors.Join(fmt.Errorf("portable launcher orchestration: installed skill identity does not match result pins"), cleanupErr) + } + if err := setupMatchesResultPins(input.Setup, input.Pins); err != nil { + cleanupErr := cleanupFixture(ctx, fixture, input.Runner.InspectResources, input.Runner.ExportEvidence) + return Result{}, errors.Join(err, cleanupErr) + } + + identity, err := recordedExecutableForPath(filepath.Join(fixture.Root, "bin", "duo")) + if err != nil { + cleanupErr := cleanupFixture(ctx, fixture, input.Runner.InspectResources, input.Runner.ExportEvidence) + return Result{}, errors.Join(fmt.Errorf("portable launcher orchestration: identify copied Duo executable: %w", err), cleanupErr) + } + + var commands []RecordedCommand + var loadErr error + loaded := false + callerExport := input.Runner.ExportEvidence + input.Runner.ExportEvidence = func(exportCtx context.Context, f *Fixture) error { + if !loaded { + commands, loadErr = seams.Load(f.Capture, input.RunOriginBootTimeNS, identity) + loaded = true + } + var exportErr error + if callerExport == nil { + exportErr = fmt.Errorf("cleanup evidence export omitted") + } else { + exportErr = callerExport(exportCtx, f) + } + return errors.Join(loadErr, exportErr) + } + input.Runner.Fixture = fixture + record := seams.Run(ctx, input.Runner) + if !fixture.cleaned { + cleanupErr := cleanupFixture(ctx, fixture, input.Runner.InspectResources, input.Runner.ExportEvidence) + record.CleanupError = errors.Join(record.CleanupError, cleanupErr) + } + + facts := input.Facts + facts.Run, facts.Pins, facts.Record, facts.Commands = input.Run, input.Pins, record, commands + facts.Setup.Isolated = true + facts.Setup.CredentialPresent = input.Setup.Credential.Present + facts.Setup.CredentialProvider = CanonicalScenario().Fixture.Provider + facts.Setup.CredentialSourceKind = input.Setup.Credential.SourceKind + facts.Cleanup.RootRemoved = fixture.cleaned + stages, assemblyErr := seams.Assemble(facts) + result, capture := seams.Collect(CollectorInput{Run: input.Run, Pins: input.Pins, Record: record, Stages: stages}) + if loadErr != nil || assemblyErr != nil { + return result, errors.Join(fmt.Errorf("portable launcher orchestration: trusted assembly rejected raw facts"), loadErr, assemblyErr) + } + if err := seams.Validate(result, capture); err != nil { + return result, fmt.Errorf("portable launcher orchestration: offline validation: %w", err) + } + if err := seams.Write(input.Destination, result, capture); err != nil { + return result, fmt.Errorf("portable launcher orchestration: write bundle: %w", err) + } + return result, nil +} + +func cleanupFixture(ctx context.Context, fixture *Fixture, inspect ResourceInspector, export EvidenceExporter) error { + cleanupContext, cancel := context.WithTimeout(context.WithoutCancel(ctx), cleanupTimeout) + defer cancel() + return fixture.Cleanup(cleanupContext, inspect, export) +} + +func defaultOrchestrationSeams(seams OrchestrationSeams) OrchestrationSeams { + if seams.Prepare == nil { + seams.Prepare = PrepareFixture + } + if seams.Run == nil { + seams.Run = RunCommon + } + if seams.Load == nil { + seams.Load = LoadRecordedCommands + } + if seams.Assemble == nil { + seams.Assemble = AssembleCommonStages + } + if seams.Collect == nil { + seams.Collect = CollectResult + } + if seams.Validate == nil { + seams.Validate = validateCollectedCapture + } + if seams.Write == nil { + seams.Write = WriteBundle + } + return seams +} + +func validateCollectedCapture(result Result, capture *Capture) error { + if capture == nil { + return fmt.Errorf("capture is nil") + } + resultJSON, err := marshalBundleJSON(result) + if err != nil { + return err + } + indexJSON, err := marshalBundleJSON(capture.Index()) + if err != nil { + return err + } + _, err = Validate(ValidationInput{ResultJSON: resultJSON, IndexJSON: indexJSON, ReadBlob: func(reference string) ([]byte, error) { + blob, ok := capture.Blob(reference) + if !ok { + return nil, fmt.Errorf("blob %s not found", reference) + } + return blob, nil + }}) + return err +} + +func setupMatchesResultPins(setup SetupInput, pins Pins) error { + launcher := setup.Artifacts["launcher"] + duo := setup.Artifacts["duo"] + host := setup.Artifacts["host"] + runtime := setup.Artifacts["runtime"] + if launcher.Name != pins.Launcher.Name || launcher.Version != pins.Launcher.Version || launcher.Digest != pins.Launcher.ExecutableSHA256 { + return fmt.Errorf("portable launcher orchestration: setup launcher identity does not match result pins") + } + if duo.Version != pins.Duo.Version || duo.Commit != pins.Duo.Commit || duo.BuildDate != pins.Duo.BuildDate || duo.Digest != pins.Duo.ExecutableSHA256 { + return fmt.Errorf("portable launcher orchestration: setup Duo identity does not match result pins") + } + if host.Version != pins.Host.Version || host.Digest != pins.Host.ExecutableSHA256 || setup.HostProtocol != pins.Host.Protocol || setup.HostSchemaDigest != pins.Host.SchemaDigest { + return fmt.Errorf("portable launcher orchestration: setup host identity does not match result pins") + } + if runtime.Version != pins.Runtime.Version || runtime.Digest != pins.Runtime.ExecutableSHA256 { + return fmt.Errorf("portable launcher orchestration: setup runtime identity does not match result pins") + } + if setup.EffectiveConfigDigest != pins.Config.EffectiveDigest { + return fmt.Errorf("portable launcher orchestration: setup config identity does not match result pins") + } + return nil +} diff --git a/internal/conformance/portablelauncher/orchestrate_test.go b/internal/conformance/portablelauncher/orchestrate_test.go new file mode 100644 index 0000000..4196f22 --- /dev/null +++ b/internal/conformance/portablelauncher/orchestrate_test.go @@ -0,0 +1,266 @@ +//go:build linux + +package portablelauncher + +import ( + "context" + "errors" + "os" + "path/filepath" + "strings" + "testing" + "time" + + "github.com/spf13/cobra" + + "github.com/procrastivity/duo/internal/buildinfo" + "github.com/procrastivity/duo/internal/manifest" + "github.com/procrastivity/duo/internal/surface" +) + +func TestPrepareFixtureMaterializesCanonicalScenarioAndDynamicDuoBuild(t *testing.T) { + base, err := os.MkdirTemp(os.TempDir(), "duo-portable-setup-test-") + if err != nil { + t.Fatal(err) + } + if err := os.Chmod(base, 0o700); err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = os.RemoveAll(base) }) + artifacts := make(map[string]Artifact) + writeArtifact := func(key, name, version string, data []byte) { + path := filepath.Join(base, key) + if err := os.WriteFile(path, data, 0o700); err != nil { + t.Fatal(err) + } + artifacts[key] = Artifact{Name: name, Path: path, Version: version, Digest: Digest(data)} + } + writeArtifact("launcher", "amp", "fixture-launcher", []byte("launcher\n")) + writeArtifact("duo", "duo", "fixture-duo", []byte("duo\n")) + writeArtifact("host", "herdr", "0.8.2", []byte("herdr\n")) + writeArtifact("runtime", "pi", "0.83.0", []byte("pi\n")) + duoPin := DuoPin{Version: "fixture-duo", Commit: "0123456789abcdef0123456789abcdef01234567", BuildDate: "2026-09-18T00:00:00Z", ExecutableSHA256: artifacts["duo"].Digest} + duoArtifact := artifacts["duo"] + duoArtifact.Commit, duoArtifact.BuildDate = duoPin.Commit, duoPin.BuildDate + artifacts["duo"] = duoArtifact + + oldLauncher := acceptedLaunchers["amp"] + acceptedLaunchers["amp"] = AcceptedLauncher{Version: artifacts["launcher"].Version, Digest: artifacts["launcher"].Digest} + t.Cleanup(func() { acceptedLaunchers["amp"] = oldLauncher }) + root := &cobra.Command{Use: "duo"} + verb := &cobra.Command{Use: "fixture", RunE: func(*cobra.Command, []string) error { return nil }} + surface.Annotate(verb, surface.Plumbing) + root.AddCommand(verb) + productManifest, err := manifest.Build(root, buildinfo.Info{Version: duoPin.Version, Commit: duoPin.Commit, Date: duoPin.BuildDate}) + if err != nil { + t.Fatal(err) + } + config := []byte("schema: duo.config/v3\n") + setupInput := SetupInput{ + BaseDir: base, Artifacts: artifacts, HostProtocol: "herdr-socket-api/20", HostSchemaDigest: HostSchemaDigest, + ProductManifest: productManifest, ConfigBytes: config, EffectiveConfigDigest: Digest(config), + Credential: Credential{Present: true, SourceKind: "operator_copy", Bytes: []byte("fixture credential\n")}, Duo: duoPin, + } + mismatch := setupInput + mismatch.ProductManifest.Tool.Commit = strings.Repeat("b", 40) + if err := CheckSetupPrerequisites(mismatch); err == nil || !strings.Contains(err.Error(), "product manifest build identity") { + t.Fatalf("product manifest with a different Duo pin was accepted: %v", err) + } + fixture, err := PrepareFixture(setupInput) + if err != nil { + t.Fatalf("PrepareFixture: %v", err) + } + if fixture.InstallationID == "" { + t.Fatal("materialized fixture has no projected skill identity") + } + wantScenario, _ := ScenarioJSON(CanonicalScenario()) + gotScenario, err := os.ReadFile(filepath.Join(fixture.Workspace, ".duo-conformance", "scenario.json")) + if err != nil || string(gotScenario) != string(wantScenario) { + t.Fatalf("materialized scenario mismatch: err=%v", err) + } + if err := fixture.Cleanup(context.Background(), func(context.Context, *Fixture) error { return nil }, func(context.Context, *Fixture) error { return nil }); err != nil { + t.Fatalf("cleanup materialized fixture: %v", err) + } +} + +func TestOrchestrateRunsOfflinePipelineAndPublishesStructuralBlockedBundle(t *testing.T) { + input, seams, root, order := orchestrationTestFixture(t) + result, err := orchestrate(context.Background(), input, seams) + if err != nil { + t.Fatalf("orchestrate: %v", err) + } + if result.Summary.Verdict != "fail" || result.Summary.FirstFailedCase == nil || *result.Summary.FirstFailedCase != "blocked" { + t.Fatalf("summary = %#v", result.Summary) + } + if _, err := os.Stat(filepath.Join(input.Destination, "result.json")); err != nil { + t.Fatalf("bundle was not published: %v", err) + } + if _, err := os.Stat(root); !errors.Is(err, os.ErrNotExist) { + t.Fatalf("fixture root remains after orchestration: %v", err) + } + want := []string{"run", "inspect", "load", "export", "assemble", "validate", "write"} + if strings.Join(*order, ",") != strings.Join(want, ",") { + t.Fatalf("pipeline order = %v, want %v", *order, want) + } + last := result.Stages[len(result.Stages)-1] + if last.Stage != "cleanup" || last.Verdict != "pass" { + t.Fatalf("independent final cleanup = %#v", last) + } +} + +func TestOrchestrateCleansAfterEveryMajorFailureBoundary(t *testing.T) { + tests := []struct { + name string + edit func(*OrchestrationSeams) + }{ + {name: "load", edit: func(s *OrchestrationSeams) { + s.Load = func(string, int64, RecordedExecutable) ([]RecordedCommand, error) { + return nil, errors.New("load failed") + } + }}, + {name: "assemble", edit: func(s *OrchestrationSeams) { + base := s.Assemble + s.Assemble = func(input AssemblyInput) ([]CommonStageCapture, error) { + stages, _ := base(input) + return stages, errors.New("assembly failed") + } + }}, + {name: "validate", edit: func(s *OrchestrationSeams) { + s.Validate = func(Result, *Capture) error { return errors.New("validation failed") } + }}, + {name: "write", edit: func(s *OrchestrationSeams) { + s.Write = func(string, Result, *Capture) error { return errors.New("write failed") } + }}, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + input, seams, root, _ := orchestrationTestFixture(t) + test.edit(&seams) + if _, err := orchestrate(context.Background(), input, seams); err == nil { + t.Fatal("injected pipeline failure was hidden") + } + if _, err := os.Stat(root); !errors.Is(err, os.ErrNotExist) { + t.Fatalf("fixture root remains after %s failure: %v", test.name, err) + } + }) + } +} + +func TestOrchestrateCleansAndMaterializesCommonRunFailure(t *testing.T) { + input, seams, root, _ := orchestrationTestFixture(t) + seams.Run = func(context.Context, RunnerInput) RunRecord { + record := baseRunRecordForOrchestration(input.Facts) + record.RunnerError = errors.New("injected common run failure") + return record // orchestration must perform the missed cleanup itself. + } + result, err := orchestrate(context.Background(), input, seams) + if err != nil { + t.Fatalf("authoritative failed run was not materialized: %v", err) + } + if _, err := os.Stat(root); !errors.Is(err, os.ErrNotExist) { + t.Fatalf("fixture root remains after common run failure: %v", err) + } + if result.Summary.FirstFailedCase == nil || *result.Summary.FirstFailedCase != "blocked" { + t.Fatalf("structural blocker was not preserved as the first failure: %#v", result.Summary) + } + lastReached := result.Stages[collectorStageIndex("timeout", "command_inspection")] + if lastReached.Error == nil || lastReached.Error.Code != "run.failed" { + t.Fatalf("run failure attribution = %#v", lastReached) + } +} + +func TestOrchestrateBoundsCleanupAfterCallerCancellation(t *testing.T) { + input, seams, _, _ := orchestrationTestFixture(t) + input.Pins.Skill.InstallationID = "different-installation" + deadlineSeen := false + input.Runner.InspectResources = func(ctx context.Context, _ *Fixture) error { + deadline, ok := ctx.Deadline() + deadlineSeen = ok && ctx.Err() == nil && time.Until(deadline) > 0 && time.Until(deadline) <= cleanupTimeout + return nil + } + input.Runner.ExportEvidence = func(context.Context, *Fixture) error { return nil } + ctx, cancel := context.WithCancel(context.Background()) + cancel() + if _, err := orchestrate(ctx, input, seams); err == nil || !strings.Contains(err.Error(), "installed skill identity") { + t.Fatalf("pin mismatch = %v", err) + } + if !deadlineSeen { + t.Fatal("cleanup did not receive a fresh bounded context after caller cancellation") + } +} + +func baseRunRecordForOrchestration(facts AssemblyInput) RunRecord { + return RunRecord{Controls: append([]RecordedControl(nil), facts.Record.Controls...)} +} + +func orchestrationTestFixture(t *testing.T) (OrchestrationInput, OrchestrationSeams, string, *[]string) { + t.Helper() + root, err := os.MkdirTemp(t.TempDir(), "duo-portable-launcher-") + if err != nil { + t.Fatal(err) + } + for _, name := range []string{"bin", "capture", "workspace"} { + if err := os.MkdirAll(filepath.Join(root, name), 0o700); err != nil { + t.Fatal(err) + } + } + duoBytes := []byte("fixture duo executable\n") + if err := os.WriteFile(filepath.Join(root, "bin", "duo"), duoBytes, 0o700); err != nil { + t.Fatal(err) + } + pins := fixturePins() + facts := assemblyTestInput(t) + facts.Pins = pins + setup := SetupInput{ + Artifacts: map[string]Artifact{ + "launcher": {Name: pins.Launcher.Name, Version: pins.Launcher.Version, Digest: pins.Launcher.ExecutableSHA256}, + "duo": {Name: "duo", Version: pins.Duo.Version, Commit: pins.Duo.Commit, BuildDate: pins.Duo.BuildDate, Digest: pins.Duo.ExecutableSHA256}, + "host": {Name: "herdr", Version: pins.Host.Version, Digest: pins.Host.ExecutableSHA256}, + "runtime": {Name: "pi", Version: pins.Runtime.Version, Digest: pins.Runtime.ExecutableSHA256}, + }, + HostProtocol: pins.Host.Protocol, HostSchemaDigest: pins.Host.SchemaDigest, + EffectiveConfigDigest: pins.Config.EffectiveDigest, + Credential: Credential{Present: true, SourceKind: "operator_copy", Bytes: []byte("fixture credential")}, + Duo: pins.Duo, + } + fixture := &Fixture{ + Root: root, Workspace: filepath.Join(root, "workspace"), Capture: filepath.Join(root, "capture"), + Result: filepath.Join(root, "result"), InstallationID: pins.Skill.InstallationID, + } + order := &[]string{} + runner := RunnerInput{ + InspectResources: func(context.Context, *Fixture) error { *order = append(*order, "inspect"); return nil }, + ExportEvidence: func(context.Context, *Fixture) error { *order = append(*order, "export"); return nil }, + } + input := OrchestrationInput{ + Setup: setup, Runner: runner, Run: facts.Run, Pins: pins, Facts: facts, + RunOriginBootTimeNS: 1, Destination: filepath.Join(t.TempDir(), "bundle"), + } + seams := OrchestrationSeams{ + Prepare: func(SetupInput) (*Fixture, error) { return fixture, nil }, + Run: func(ctx context.Context, in RunnerInput) RunRecord { + *order = append(*order, "run") + record := facts.Record + record.CleanupError = in.Fixture.Cleanup(ctx, in.InspectResources, in.ExportEvidence) + return record + }, + Load: func(string, int64, RecordedExecutable) ([]RecordedCommand, error) { + *order = append(*order, "load") + return facts.Commands, nil + }, + Assemble: func(input AssemblyInput) ([]CommonStageCapture, error) { + *order = append(*order, "assemble") + return AssembleCommonStages(input) + }, + Collect: CollectResult, + Validate: func(result Result, capture *Capture) error { + *order = append(*order, "validate") + return validateCollectedCapture(result, capture) + }, + Write: func(destination string, result Result, capture *Capture) error { + *order = append(*order, "write") + return WriteBundle(destination, result, capture) + }, + } + return input, seams, root, order +} diff --git a/internal/conformance/portablelauncher/pins.go b/internal/conformance/portablelauncher/pins.go new file mode 100644 index 0000000..17289a9 --- /dev/null +++ b/internal/conformance/portablelauncher/pins.go @@ -0,0 +1,39 @@ +package portablelauncher + +import "regexp" + +// Pinned suite identities and canonical payload digests. +const ( + SkillName = "duo-delegation-loop" + SkillFormat = "duo.skill/duo-delegation-loop/v1" + SkillContentDigest = "sha256:6f8bc16b656f564a949c99f12d4cf0bcd04ec2c9b49a8b74fb65b2e355b5d182" + ExternalSchemaDigest = "sha256:8b055ff60f0ca5f3da46302671591aa0dae61185cd9f1f15fcceb36d841e7497" + ConfigSchemaDigest = "sha256:d1a2fd1be5339c5699a21cf65616a4d09ff932a5495111d02a422b7656086842" + HostSchemaDigest = "sha256:c48f1f54ee0150ca27e11fd44455fe94aeadb20fdf4e4a62393ed822a4e5b150" + DeliveryAssetDigest = "sha256:2708a3435821fb2e72302ee56d8a07fe62c265214ae8fd342e385c03ef245a23" + RequestDigest = "sha256:b0a32f59630d6398da8d8913744dd7cd50a313d86bf7e833309aad0d47cc9c3b" + ReplyDigest = "sha256:b3300f2f6e0338d2b672891be66827d95779a2eabf4a75dcae1b6f56c9a4dc98" + PromptDigest = "sha256:b40d3a68eb33083b92199d734da2dece28395a6acc6d5e2cb9d7032dae4d11c7" + ConflictDigest = "sha256:dbe904a42a9d9d9cdf7908508fa393ce43c9c53ea16a8cb8c9b82e7005f602bc" +) + +var exactCommitPattern = regexp.MustCompile(`^[0-9a-f]{40}$`) + +// AcceptedLauncher is an exact launcher version and executable digest admitted +// by the suite. +type AcceptedLauncher struct { + Version string `json:"version"` + Digest string `json:"executable_sha256"` +} + +var acceptedLaunchers = map[string]AcceptedLauncher{ + "amp": {Version: "0.0.1789675234-g2899fe", Digest: "sha256:f351217dac739614b4d9b3eacad728ce6d61647ab1f5d4a4de44d99aba6598ee"}, + "opencode": {Version: "1.18.31", Digest: "sha256:f9dab32248695e9ebd56b16a1921798fd85112cf5a69c7dfd0cabc1e17be4a11"}, + "codex": {Version: "0.154.0", Digest: "sha256:3188814c35471432d4123203e0eb38e5bddc60226e3d7ddf0e59e649ea140022"}, +} + +// AcceptedLauncherPin returns the exact admitted pin for a launcher name. +func AcceptedLauncherPin(name string) (AcceptedLauncher, bool) { + p, ok := acceptedLaunchers[name] + return p, ok +} diff --git a/internal/conformance/portablelauncher/portablelauncher_test.go b/internal/conformance/portablelauncher/portablelauncher_test.go new file mode 100644 index 0000000..44014fa --- /dev/null +++ b/internal/conformance/portablelauncher/portablelauncher_test.go @@ -0,0 +1,1034 @@ +package portablelauncher + +import ( + "context" + "encoding/json" + "errors" + "fmt" + "os" + "path/filepath" + "reflect" + "sort" + "strings" + "testing" +) + +const fixtureDir = "../../../contracts/fixtures/duo-portable-launcher-conformance-v1" + +const fixtureDuoCommit = "0123456789abcdef0123456789abcdef01234567" + +type builtFixture struct { + Scenario []byte + Result Result + Rejected Result + Index EvidenceIndex + BlobRef string + Blob []byte +} + +func buildContractFixture(t *testing.T) builtFixture { + t.Helper() + scenario := CanonicalScenario() + scenarioJSON, err := ScenarioJSON(scenario) + if err != nil { + t.Fatal(err) + } + result := Result{ + Schema: ResultSchema, + Suite: SuiteIdentity{Name: ScenarioName, Revision: 1, ManifestDigest: Digest(scenarioJSON), OracleDigest: OracleDigest()}, + Run: RunIdentity{RunID: "fixture-structural-blocked", ObservedAt: "2026-09-17T00:00:00Z", HostOS: "linux", HostArch: "x86_64", FixtureRoot: "$RUN"}, + Pins: fixturePins(), + Summary: Summary{Verdict: "fail", FirstFailedStage: stringPointer("launch"), FirstFailedCase: stringPointer("blocked")}, + Scrub: ScrubRecord{Status: "pass", Policy: ScrubPolicy, Findings: []string{}}, + } + + var observations []Observation + var offset int64 + blockedFailed := false + for _, step := range scenario.Steps { + stage := StageResult{ + Sequence: step.Sequence, Stage: step.Stage, Case: step.Case, + Verdict: "pass", Outcome: step.Outcome, StartedOffsetMS: offset, DurationMS: 1, + } + if step.Case == "timeout" && step.Stage == "observe" { + stage.DurationMS = 20_000 + } + switch { + case step.Case == "blocked" && step.Stage == "launch": + blockedFailed = true + stage.Verdict, stage.Outcome = "fail", "error" + stage.Error = &StageError{ + Code: "prerequisite.blocked_induction_unavailable", Message: "supported admitted-then-blocked evidence is unavailable", + Effect: "no_effect", Retry: Retry{Safe: false, Action: "add_supported_blocked_evidence"}, + } + stage.Assertions = []Assertion{{ + ID: "blocked.induction_available", Expected: map[string]any{"available": true}, + Actual: map[string]any{"available": false}, Matched: false, + }} + case step.Case == "blocked" && blockedFailed: + stage.Verdict, stage.Outcome = "fail", "error" + stage.Error = &StageError{ + Code: "prerequisite.not_reached", Message: "blocked induction prerequisite failed", + Effect: "no_effect", Retry: Retry{Safe: false, Action: "inspect_originating_failure"}, + } + stage.Assertions = []Assertion{{ + ID: "prerequisite.not_reached", Expected: map[string]any{"reached": true}, + Actual: map[string]any{"reached": false}, Matched: false, + }} + default: + for _, expected := range step.Assertions { + actual := cloneMap(t, expected.Expected) + stage.Assertions = append(stage.Assertions, Assertion{ID: expected.ID, Expected: cloneMap(t, expected.Expected), Actual: actual, Matched: true}) + } + } + for _, assertion := range stage.Assertions { + observations = append(observations, Observation{ + Sequence: stage.Sequence, AssertionID: assertion.ID, + Source: evidenceSource(assertion.ID), + Evidence: evidenceDocument(t, assertion.ID, stage, result.Pins, assertion.Actual), + }) + } + result.Stages = append(result.Stages, stage) + offset += stage.DurationMS + } + + evidence := Evidence{Schema: EvidenceSchema, ScrubStatus: "pass", Observations: observations} + capture := NewCapture() + ref, err := capture.AddJSON("common_collector", evidence) + if err != nil { + t.Fatal(err) + } + for i := range result.Stages { + result.Stages[i].Evidence = []string{ref} + } + blob, _ := capture.Blob(ref) + + rejected := cloneResult(t, result) + for i, step := range scenario.Steps { + if rejected.Stages[i].Case != "blocked" { + continue + } + rejected.Stages[i].Verdict = "pass" + rejected.Stages[i].Outcome = step.Outcome + rejected.Stages[i].Error = nil + rejected.Stages[i].Assertions = nil + for _, expected := range step.Assertions { + rejected.Stages[i].Assertions = append(rejected.Stages[i].Assertions, Assertion{ID: expected.ID, Expected: cloneMap(t, expected.Expected), Actual: cloneMap(t, expected.Expected), Matched: true}) + } + } + rejected.Summary = Summary{Verdict: "pass"} + return builtFixture{Scenario: scenarioJSON, Result: result, Rejected: rejected, Index: capture.Index(), BlobRef: ref, Blob: blob} +} + +func evidenceDocument(t *testing.T, id string, stage StageResult, pins Pins, actual map[string]any) map[string]any { + t.Helper() + sequence := stage.Sequence + source := evidenceSource(id) + switch source { + case "launcher_event": + return map[string]any{"kind": "skill.discovery", "task_digest": Digest(CanonicalTaskBytes), "arguments": []any{"fixture-launcher", TaskArgument}, "skill": cloneMap(t, actual)} + case "controller": + doc := map[string]any{"kind": "controller.checkpoint", "monotonic_ms": float64(stage.StartedOffsetMS), "measurements": cloneMap(t, actual)} + if id == "stage.completed_within_deadline" { + delete(doc, "measurements") + doc["started_ms"] = float64(stage.StartedOffsetMS) + doc["finished_ms"] = float64(stage.StartedOffsetMS + stage.DurationMS) + deadline := CanonicalScenario().Steps[sequence-1].DeadlineMS + if stage.Case == "timeout" && stage.Stage == "observe" { + deadline = CanonicalOracle().TimeoutMaximumMS + } + doc["deadline_ms"] = float64(deadline) + } + if id == "observe.exited" || id == "observe.timeout" { + doc["target_verified"] = true + } + if id == "observe.timeout" { + doc["timeout_probe_supported"] = true + } + if id == "setup.fixture_isolated_and_pinned" { + pinBytes, err := json.Marshal(pins) + if err != nil { + t.Fatal(err) + } + doc["pins_digest"] = Digest(pinBytes) + doc["credentials_present"] = true + doc["credential_provider"] = "openai-codex" + doc["credential_source_kind"] = "operator_copy" + doc["pi_no_extensions"] = true + } + return doc + default: + return duoEvidenceDocument(t, id, sequence, actual) + } +} + +func duoEvidenceDocument(t *testing.T, id string, sequence int, actual map[string]any) map[string]any { + t.Helper() + envelope := map[string]any{ + "schema": "duo.external/v1", "request_id": fmt.Sprintf("req_fixture_%d", sequence), + "operation": CanonicalOracle().DuoOperations[id], "warnings": []any{}, "features": []any{}, + } + wrapper := map[string]any{"envelope": envelope} + switch id { + case "launch.resolution": + envelope["result"] = map[string]any{ + "session_id": "ses_fixture", "launch_resolution_id": "lrr_fixture", "selection": "ordered", + "leaves": []any{map[string]any{ + "name": "main", "agent_runtime": actual["runtime"], "model_line": "gpt-5.6-luna", + "declared_kind": "open", "outcome": "selected", "relented_avoids": []any{}, + }}, + "host": map[string]any{"kind": actual["host"], "instance_id": "hostinst_fixture", "instance_label": "fixture", "host_source": "explicit-flag", "workspace_id": "workspace_fixture", "outranked_evidence": []any{}}, + } + wrapper["argv_has_prompt"] = actual["prelaunch_prompt"] + case "bind.runtime_identity": + envelope["result"] = map[string]any{ + "session_id": "ses_fixture", "lifecycle": "active", "runtime_instance_id": "run_fixture", + "attachments": []any{map[string]any{ + "attachment_id": "attachment_fixture", "state": "attached", "integration_instance": "herdr:fixture", + "epoch": map[string]any{"kind": "herdr.terminal_id", "value": "term_fixture", "scope": "pane"}, + "container": "workspace-1:pane-1", + "process_birth": map[string]any{"pid": float64(41210), "started_at": "2026-09-17T00:00:00Z"}, + "claim_held": true, + }}, + } + wrapper["authority_instance_ids"] = []any{"run_fixture"} + wrapper["active_correlation_runtime_ids"] = []any{"run_fixture"} + case "send.delivery", "command.delivery_integrity": + envelope["result"] = map[string]any{ + "command_id": "cmd_fixture", "revision": "4", "operation": "prompt.deliver", + "target": map[string]any{"session_id": "ses_fixture", "runtime_instance_id": "run_fixture"}, + "responsibility_state": actual["state"], "queue_policy": "queue_until_safe", + "attempts": []any{map[string]any{"attempt_id": "attempt_fixture", "realization": "native", "started_at": "2026-09-17T00:00:00Z", "recorded_result": "delivered"}}, + "milestones": map[string]any{"accepted_at": "2026-09-17T00:00:00Z", "delivered_at": "2026-09-17T00:00:01Z", "activity_observed": false, "acknowledged": false}, + "retry": map[string]any{"safe": false, "action": "observe_existing_command"}, + } + if id == "command.delivery_integrity" { + wrapper["unchanged"] = actual["unchanged"] + } + case "observe.assistant_text", "observe.restart_text": + envelope["result"] = map[string]any{ + "session_id": "ses_fixture", + "items": []any{map[string]any{ + "record_id": "record_fixture", "session_id": "ses_fixture", "runtime_instance_id": "run_fixture", + "author_role": "agent", "blocks": []any{map[string]any{"position": "0", "type": "text", "content": map[string]any{"text": "DUO_PORTABLE_LAUNCHER_OK_V1"}}}, + "completion": "source_record_complete", "received_at": "2026-09-17T00:00:00Z", + }}, + } + if id == "observe.restart_text" { + wrapper["baseline_record_id"] = "record_fixture" + } + case "send.idempotency_conflict": + envelope["error"] = map[string]any{ + "class": "conflict", "code": actual["code"], "message": "fixture idempotency conflict", + "target": map[string]any{"kind": "prompt_command", "id": "cmd_fixture"}, + "retry": map[string]any{"safe": false, "action": actual["retry_action"]}, "effect": actual["effect"], + "details": map[string]any{"idempotency_key": "portable-launcher-v1-primary", "existing_digest": actual["existing_digest"], "request_digest": actual["request_digest"]}, + } + wrapper["original_command_id"] = "cmd_fixture" + delete(envelope, "warnings") + delete(envelope, "features") + default: + t.Fatalf("no fixture Duo evidence builder for %s", id) + } + return wrapper +} + +func fixturePins() Pins { + launcher, _ := AcceptedLauncherPin("amp") + return Pins{ + Launcher: LauncherPin{Name: "amp", Version: launcher.Version, ExecutableSHA256: launcher.Digest}, + Duo: DuoPin{Version: "fixture", Commit: fixtureDuoCommit, BuildDate: "2026-09-17T00:00:00Z", ExecutableSHA256: Digest([]byte("fixture duo executable\n"))}, + Skill: SkillPin{Name: SkillName, FormatVersion: SkillFormat, ContentDigest: SkillContentDigest, InstallationID: "fixture-installation"}, + Config: ConfigPin{Schema: "duo.config/v3", EffectiveDigest: Digest([]byte("fixture effective config\n"))}, + Authority: AuthorityPin{SchemaVersion: 1, StoreDigestBefore: Digest([]byte("fixture empty authority\n")), StoreDigestAfter: Digest([]byte("fixture populated authority\n"))}, + Host: HostPin{Name: "herdr", Version: "0.8.2", Protocol: "herdr-socket-api/20", SchemaDigest: HostSchemaDigest, ExecutableSHA256: Digest([]byte("fixture herdr executable\n"))}, + Runtime: RuntimePin{Name: "pi", Version: "0.83.0", Format: "pi-session-jsonl/v3", AdapterBuild: "stage1", ConformanceRecord: "pi-0.83.0-2026-08-23", ExecutableSHA256: Digest([]byte("fixture pi executable\n")), DeliveryAssetSHA256: DeliveryAssetDigest}, + Model: ModelPin{Provider: "openai-codex", ModelLine: "gpt-5.6-luna"}, + ExternalSchema: ExternalSchemaPin{Identity: "duo.external/v1", Digest: ExternalSchemaDigest}, + } +} + +func evidenceSource(id string) string { + switch { + case strings.HasPrefix(id, "discovery."): + return "launcher_event" + case strings.HasPrefix(id, "setup."), strings.HasPrefix(id, "cleanup."), strings.HasPrefix(id, "blocked."), strings.HasPrefix(id, "prerequisite."), strings.HasPrefix(id, "preflight."), strings.HasPrefix(id, "restart."), strings.HasPrefix(id, "command.idempot"), id == "send.idempotent_replay", id == "observe.timeout", id == "observe.exited", id == "stage.completed_within_deadline": + return "controller" + default: + return "duo_envelope" + } +} + +func TestContractFixtures(t *testing.T) { + built := buildContractFixture(t) + files := map[string][]byte{ + "scenario.json": built.Scenario, + "result-structural-blocked.json": marshalFixture(t, built.Result), + "result-rejected-fabricated-pass.json": marshalFixture(t, built.Rejected), + "evidence-index.json": marshalFixture(t, built.Index), + filepath.Join("blobs", strings.TrimPrefix(built.BlobRef, "blob:sha256:")+".json"): built.Blob, + } + if os.Getenv("UPDATE_PORTABLE_LAUNCHER_FIXTURES") == "1" { + if err := os.RemoveAll(fixtureDir); err != nil { + t.Fatal(err) + } + for name, data := range files { + path := filepath.Join(fixtureDir, name) + if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(path, data, 0o644); err != nil { + t.Fatal(err) + } + } + } + for name, want := range files { + got, err := os.ReadFile(filepath.Join(fixtureDir, name)) + if err != nil { + t.Fatalf("read %s: %v", name, err) + } + if !reflect.DeepEqual(got, want) { + t.Errorf("%s is stale; run UPDATE_PORTABLE_LAUNCHER_FIXTURES=1 go test ./internal/conformance/portablelauncher", name) + } + } + if _, err := validateFixture(built); err != nil { + t.Fatalf("structural blocked fixture must validate: %v", err) + } + rejected := built + rejected.Result = built.Rejected + if _, err := validateFixture(rejected); err == nil || !strings.Contains(err.Error(), "blocked") { + t.Fatalf("fabricated-pass fixture must be rejected for blocked prerequisite: %v", err) + } +} + +func TestDeterministicConstantsAndScenarioMatrix(t *testing.T) { + assertDigest(t, RequestBytes, RequestDigest) + assertDigest(t, []byte("DUO_PORTABLE_LAUNCHER_OK_V1"), ReplyDigest) + assertDigest(t, PromptBytes, PromptDigest) + assertDigest(t, ConflictText, ConflictDigest) + for path, want := range map[string]string{ + "../../../skills/duo-delegation-loop/SKILL.md": SkillContentDigest, + "../../runtime/pi/inject/duo-inject.ts": DeliveryAssetDigest, + "../../../contracts/schemas/duo-external-v1.schema.json": ExternalSchemaDigest, + "../../../contracts/schemas/duo-config-v3.schema.json": ConfigSchemaDigest, + } { + data, err := os.ReadFile(path) + if err != nil { + t.Fatal(err) + } + assertDigest(t, data, want) + } + s := CanonicalScenario() + if len(s.Steps) != 32 { + t.Fatalf("stage/case rows = %d, want 32", len(s.Steps)) + } + for i, step := range s.Steps { + if step.Sequence != i+1 || len(step.Assertions) != 2 { + t.Fatalf("step %d is not complete and ordered: %+v", i, step) + } + } + for _, prereq := range s.Prerequisites { + if prereq.Name == "supported_admitted_then_blocked_producer" && prereq.Status != "unavailable" { + t.Fatal("blocked producer must remain an explicit unavailable prerequisite") + } + } + task := strings.ToLower(string(CanonicalTaskBytes)) + if strings.Contains(task, "result") || strings.Contains(task, "verdict") || strings.Contains(task, "duo_conformance_result_path") { + t.Fatalf("canonical task assigns final-result ownership to the launcher: %q", CanonicalTaskBytes) + } +} + +func TestOfflineValidatorRejectsContractDrift(t *testing.T) { + tests := []struct { + name string + want string + edit func(*builtFixture) + }{ + {"missing stage", "got 31 records", func(f *builtFixture) { f.Result.Stages = f.Result.Stages[:31] }}, + {"duplicate stage", "got 33 records", func(f *builtFixture) { f.Result.Stages = append(f.Result.Stages, f.Result.Stages[31]) }}, + {"reordered stage", "want run/setup", func(f *builtFixture) { f.Result.Stages[0], f.Result.Stages[1] = f.Result.Stages[1], f.Result.Stages[0] }}, + {"unknown stage", "want run/setup", func(f *builtFixture) { f.Result.Stages[0].Stage = "mystery" }}, + {"unknown case", "want run/setup", func(f *builtFixture) { f.Result.Stages[0].Case = "mystery" }}, + {"nonmonotonic timing", "timing is not monotonic", func(f *builtFixture) { f.Result.Stages[2].StartedOffsetMS = 0 }}, + {"setup offset", "offset zero", func(f *builtFixture) { f.Result.Stages[0].StartedOffsetMS = 1 }}, + {"deadline exceeded", "stage deadline exceeded", func(f *builtFixture) { f.Result.Stages[0].DurationMS = 30_001 }}, + {"cleanup omission", "got 31 records", func(f *builtFixture) { f.Result.Stages = f.Result.Stages[:31] }}, + {"cleanup failure", "failed or omitted cleanup", func(f *builtFixture) { failStage(&f.Result.Stages[31], "cleanup.failed") }}, + {"terminal paste", "terminal input", func(f *builtFixture) { f.Result.Run.TerminalInputUsed = true }}, + {"plugin enabled", "plugins, MCP", func(f *builtFixture) { f.Result.Run.PluginsEnabled = true }}, + {"MCP enabled", "plugins, MCP", func(f *builtFixture) { f.Result.Run.MCPEnabled = true }}, + {"unpinned identity", "pins.runtime", func(f *builtFixture) { f.Result.Pins.Runtime.ExecutableSHA256 = "" }}, + {"wrong current runtime", "Pi 0.83.0", func(f *builtFixture) { f.Result.Pins.Runtime.Version = "0.84.4" }}, + {"wrong current host", "Herdr 0.8.2", func(f *builtFixture) { f.Result.Pins.Host.Version = "0.9.0" }}, + {"missing assertions", "passing stage has 0 assertions", func(f *builtFixture) { f.Result.Stages[0].Assertions = nil }}, + {"fabricated pass", "overall pass forbidden", func(f *builtFixture) { + for i := range f.Result.Stages { + f.Result.Stages[i].Verdict = "pass" + f.Result.Stages[i].Outcome = CanonicalScenario().Steps[i].Outcome + f.Result.Stages[i].Error = nil + } + f.Result.Summary = Summary{Verdict: "pass"} + }}, + {"idempotency duplication", "expectation is not the trusted scenario", func(f *builtFixture) { + mutateAssertion(f, "command.idempotent_no_duplication", "attempts_delta", float64(1)) + }}, + {"conflict drift", "expectation is not the trusted scenario", func(f *builtFixture) { mutateAssertion(f, "send.idempotency_conflict", "effect", "possible_effect") }}, + {"blocked pass", "blocked_producer is unavailable", func(f *builtFixture) { + i := stageIndex(f.Result, "blocked", "launch") + f.Result.Stages[i].Verdict = "pass" + f.Result.Stages[i].Outcome = "success" + f.Result.Stages[i].Error = nil + }}, + {"secret-shaped content", "result scrub failed", func(f *builtFixture) { f.Result.Run.RunID = "sk-0123456789abcdef" }}, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + f := buildContractFixture(t) + tc.edit(&f) + _, err := validateFixture(f) + if err == nil || !strings.Contains(err.Error(), tc.want) { + t.Fatalf("error = %v, want substring %q", err, tc.want) + } + }) + } +} + +func TestOfflineValidatorRejectsEvidenceFailures(t *testing.T) { + t.Run("terminal evidence", func(t *testing.T) { + findings := Scrub([]byte(`{"kind":"terminal_snapshot","terminal_paste":true}`)) + if len(findings) == 0 { + t.Fatal("terminal-paste evidence passed scrub") + } + }) + t.Run("public authorization object is not a secret", func(t *testing.T) { + if findings := Scrub([]byte(`{"authorization":{"allowed":true,"policy_version":"12"}}`)); len(findings) != 0 { + t.Fatalf("public authorization metadata failed scrub: %v", findings) + } + if findings := Scrub([]byte(`{"authorization":"Bearer abcdefghijklmnop"}`)); len(findings) == 0 { + t.Fatal("authorization header secret passed scrub") + } + }) + t.Run("missing blob", func(t *testing.T) { + f := buildContractFixture(t) + _, err := Validate(ValidationInput{ResultJSON: marshalFixture(t, f.Result), IndexJSON: marshalFixture(t, f.Index), ReadBlob: func(string) ([]byte, error) { return nil, errors.New("gone") }}) + if err == nil || !strings.Contains(err.Error(), "missing blob") { + t.Fatalf("error = %v", err) + } + }) + t.Run("mismatched blob", func(t *testing.T) { + f := buildContractFixture(t) + f.Blob = append(f.Blob, ' ') + if _, err := validateFixture(f); err == nil || !strings.Contains(err.Error(), "digest mismatch") { + t.Fatalf("error = %v", err) + } + }) + t.Run("malformed Duo envelope", func(t *testing.T) { + f := buildContractFixture(t) + mutateEvidence(t, &f, func(e *Evidence) { + for i := range e.Observations { + if e.Observations[i].Source == "duo_envelope" { + envelope := e.Observations[i].Evidence["envelope"].(map[string]any) + envelope["schema"] = "vendor.result/v1" + return + } + } + }) + if _, err := validateFixture(f); err == nil || !strings.Contains(err.Error(), "malformed duo.external/v1") { + t.Fatalf("error = %v", err) + } + }) + t.Run("wrong Duo operation", func(t *testing.T) { + f := buildContractFixture(t) + mutateEvidence(t, &f, func(e *Evidence) { + for i := range e.Observations { + if e.Observations[i].Source == "duo_envelope" { + envelope := e.Observations[i].Evidence["envelope"].(map[string]any) + envelope["operation"] = "fixture.observe" + return + } + } + }) + if _, err := validateFixture(f); err == nil || !strings.Contains(err.Error(), "operation does not match") { + t.Fatalf("error = %v", err) + } + }) + t.Run("oracle actual shortcut", func(t *testing.T) { + f := buildContractFixture(t) + mutateEvidence(t, &f, func(e *Evidence) { + e.Observations[0].Evidence["oracle_actual"] = map[string]any{"within_deadline": true} + }) + if _, err := validateFixture(f); err == nil || !strings.Contains(err.Error(), "forbidden oracle_actual") { + t.Fatalf("error = %v", err) + } + }) + t.Run("raw evidence disagrees with assertion", func(t *testing.T) { + f := buildContractFixture(t) + mutateEvidence(t, &f, func(e *Evidence) { + for i := range e.Observations { + if e.Observations[i].AssertionID == "send.delivery" { + envelope := e.Observations[i].Evidence["envelope"].(map[string]any) + result := envelope["result"].(map[string]any) + result["attempts"] = []any{} + return + } + } + }) + if _, err := validateFixture(f); err == nil || !strings.Contains(err.Error(), "actual does not match independent evidence") { + t.Fatalf("error = %v", err) + } + }) + t.Run("pin evidence mismatch", func(t *testing.T) { + f := buildContractFixture(t) + f.Result.Pins.Duo.ExecutableSHA256 = "sha256:" + strings.Repeat("2", 64) + if _, err := validateFixture(f); err == nil || !strings.Contains(err.Error(), "pin evidence") { + t.Fatalf("error = %v", err) + } + }) + t.Run("timeout probe unavailable", func(t *testing.T) { + f := buildContractFixture(t) + mutateEvidence(t, &f, func(e *Evidence) { + for i := range e.Observations { + if e.Observations[i].AssertionID == "observe.timeout" { + e.Observations[i].Evidence["timeout_probe_supported"] = false + } + } + }) + if _, err := validateFixture(f); err == nil || !strings.Contains(err.Error(), "prerequisite.timeout_induction_unavailable") { + t.Fatalf("error = %v", err) + } + }) + t.Run("scrubbed blob", func(t *testing.T) { + f := buildContractFixture(t) + f.Blob = []byte(`{"authorization":"Bearer abcdefghijklmnop"}`) + f.Index.Blobs[0].Bytes = int64(len(f.Blob)) + f.Index.Blobs[0].SHA256 = Digest(f.Blob) + f.Index.Blobs[0].Reference = "blob:" + Digest(f.Blob) + for i := range f.Result.Stages { + f.Result.Stages[i].Evidence = []string{f.Index.Blobs[0].Reference} + } + f.BlobRef = f.Index.Blobs[0].Reference + if _, err := validateFixture(f); err == nil || !strings.Contains(err.Error(), "scrub failed") { + t.Fatalf("error = %v", err) + } + }) +} + +func TestEveryStageDeadlineIsEnforced(t *testing.T) { + for i, step := range CanonicalScenario().Steps { + t.Run(fmt.Sprintf("%02d-%s-%s", step.Sequence, step.Case, step.Stage), func(t *testing.T) { + f := buildContractFixture(t) + limit := step.DeadlineMS + if step.Case == "timeout" && step.Stage == "observe" { + limit = CanonicalOracle().TimeoutMaximumMS + } + f.Result.Stages[i].DurationMS = limit + 1 + _, err := validateFixture(f) + want := fmt.Sprintf("stages[%d]: stage deadline exceeded", i) + if err == nil || !strings.Contains(err.Error(), want) { + t.Fatalf("error = %v, want %q", err, want) + } + }) + } +} + +func TestTimeoutBoundaryRejectsProcessFailureAndEdges(t *testing.T) { + valid := TimeoutEvidence{ElapsedMS: 20_000, ProcessSucceeded: true, StoppedVerified: true, Delivered: true, Attempts: 1} + if err := ValidateIntentionalTimeout(valid); err != nil { + t.Fatal(err) + } + for name, edit := range map[string]func(*TimeoutEvidence){ + "below": func(e *TimeoutEvidence) { e.ElapsedMS = 19_999 }, + "above": func(e *TimeoutEvidence) { e.ElapsedMS = 22_501 }, + "process failure": func(e *TimeoutEvidence) { e.ProcessSucceeded = false }, + "outer timeout": func(e *TimeoutEvidence) { e.OuterTimedOut = true }, + "not stopped": func(e *TimeoutEvidence) { e.StoppedVerified = false }, + "duplicate delivery": func(e *TimeoutEvidence) { e.Attempts = 2 }, + } { + t.Run(name, func(t *testing.T) { + e := valid + edit(&e) + if err := ValidateIntentionalTimeout(e); err == nil { + t.Fatal("invalid timeout evidence accepted") + } + }) + } +} + +func TestFixtureFailsClosedAndCleanupSurfacesFailures(t *testing.T) { + base := t.TempDir() + artifact := filepath.Join(base, "artifact") + if err := os.WriteFile(artifact, []byte("not a pinned binary"), 0o600); err != nil { + t.Fatal(err) + } + in := SetupInput{ + BaseDir: base, + Artifacts: map[string]Artifact{ + "launcher": {Name: "amp", Path: artifact, Version: "0.0.1789675234-g2899fe", Digest: Digest([]byte("not a pinned binary"))}, + "duo": {Name: "duo", Path: artifact, Version: "fixture", Commit: fixtureDuoCommit, BuildDate: "2026-09-17T00:00:00Z", Digest: Digest([]byte("not a pinned binary"))}, + "host": {Name: "herdr", Path: artifact, Version: "0.9.0", Digest: Digest([]byte("not a pinned binary"))}, + "runtime": {Name: "pi", Path: artifact, Version: "0.84.4", Digest: Digest([]byte("not a pinned binary"))}, + }, + HostProtocol: "herdr-socket-api/22", HostSchemaDigest: "sha256:" + strings.Repeat("2", 64), + ConfigBytes: []byte("schema: duo.config/v3\n"), EffectiveConfigDigest: "sha256:" + strings.Repeat("3", 64), + Duo: DuoPin{Version: "fixture", Commit: fixtureDuoCommit, BuildDate: "2026-09-17T00:00:00Z", ExecutableSHA256: Digest([]byte("not a pinned binary"))}, + } + before := directoryNames(t, base) + if _, err := PrepareFixture(in); err == nil || !strings.Contains(err.Error(), "Pi 0.83.0") || !strings.Contains(err.Error(), "Herdr 0.8.2") { + t.Fatalf("wrong current binaries were not rejected explicitly: %v", err) + } + after := directoryNames(t, base) + if !reflect.DeepEqual(before, after) { + t.Fatalf("failed setup touched run state: before=%v after=%v", before, after) + } + shared := in + shared.BaseDir = filepath.Clean(os.TempDir()) + if err := CheckSetupPrerequisites(shared); err == nil || !strings.Contains(err.Error(), "private child") { + t.Fatalf("shared root accepted: %v", err) + } + linkedBase := filepath.Join(filepath.Dir(base), "linked-base") + if err := os.Symlink(base, linkedBase); err != nil { + t.Fatal(err) + } + shared.BaseDir = linkedBase + if err := CheckSetupPrerequisites(shared); err == nil || !strings.Contains(err.Error(), "non-symlink directory") { + t.Fatalf("symlinked setup root accepted: %v", err) + } + realParent := t.TempDir() + child := filepath.Join(realParent, "child") + if err := os.Mkdir(child, 0o700); err != nil { + t.Fatal(err) + } + linkedParent := filepath.Join(filepath.Dir(realParent), "linked-parent-"+filepath.Base(realParent)) + if err := os.Symlink(realParent, linkedParent); err != nil { + t.Fatal(err) + } + shared.BaseDir = filepath.Join(linkedParent, "child") + if err := CheckSetupPrerequisites(shared); err == nil || !strings.Contains(err.Error(), "symlink component") { + t.Fatalf("setup root beneath a symlink accepted: %v", err) + } + + root, err := os.MkdirTemp(t.TempDir(), "duo-portable-launcher-") + if err != nil { + t.Fatal(err) + } + f := &Fixture{Root: root} + if _, err := f.Path("../escape"); err == nil { + t.Fatal("fixture path escape accepted") + } + err = f.Cleanup(context.Background(), func(context.Context, *Fixture) error { return errors.New("pane remains") }, nil) + if err == nil || !strings.Contains(err.Error(), "pane remains") || !strings.Contains(err.Error(), "export omitted") { + t.Fatalf("cleanup failure/omission hidden: %v", err) + } + if _, statErr := os.Stat(root); !os.IsNotExist(statErr) { + t.Fatalf("best-effort exact-root cleanup did not run: %v", statErr) + } +} + +func TestPersistDriverCapturePreservesRawStreams(t *testing.T) { + root, err := os.MkdirTemp(t.TempDir(), "duo-portable-launcher-") + if err != nil { + t.Fatal(err) + } + if err := os.Mkdir(filepath.Join(root, "capture"), 0o700); err != nil { + t.Fatal(err) + } + capture := DriverCapture{Events: []byte("event\n"), Stderr: []byte("stderr\n")} + if err := PersistDriverCapture(root, capture); err != nil { + t.Fatal(err) + } + for name, want := range map[string][]byte{ + "launcher-events.jsonl": capture.Events, + "launcher-stderr.log": capture.Stderr, + } { + got, err := os.ReadFile(filepath.Join(root, "capture", name)) + if err != nil { + t.Fatal(err) + } + if !reflect.DeepEqual(got, want) { + t.Errorf("%s = %q, want %q", name, got, want) + } + } + entries, err := os.ReadDir(filepath.Join(root, "capture")) + if err != nil { + t.Fatal(err) + } + if len(entries) != 2 { + t.Fatalf("persisted capture files = %v, want only raw events and stderr", entries) + } + if err := PersistDriverCapture(root, capture); err == nil { + t.Fatal("capture persistence overwrote an existing raw stream") + } +} + +func prepareDriverFixture(t *testing.T, launcher string, script []byte) (string, DriverSpec) { + t.Helper() + root, err := os.MkdirTemp(t.TempDir(), "duo-portable-launcher-") + if err != nil { + t.Fatal(err) + } + for _, name := range []string{"bin", "workspace", "capture", "workspace/.duo-conformance"} { + if err := os.MkdirAll(filepath.Join(root, name), 0o700); err != nil { + t.Fatal(err) + } + } + executable := filepath.Join(root, "bin", "fixture-launcher") + if err := os.WriteFile(executable, script, 0o700); err != nil { + t.Fatal(err) + } + acceptedLaunchers[launcher] = AcceptedLauncher{Version: "fixture", Digest: Digest(script)} + t.Cleanup(func() { delete(acceptedLaunchers, launcher) }) + scenario, err := ScenarioJSON(CanonicalScenario()) + if err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(root, "workspace", ".duo-conformance", "scenario.json"), scenario, 0o600); err != nil { + t.Fatal(err) + } + return root, DriverSpec{Name: launcher, Executable: executable, Arguments: []string{TaskArgument}} +} + +func TestRunOriginValidationRejectsBeforeLauncher(t *testing.T) { + script := []byte("#!/bin/sh\nprintf launched > launched\n") + root, spec := prepareDriverFixture(t, "fixture-invalid-origin", script) + t.Setenv(CommandRunOriginEnv, "987654321") + for _, test := range []struct { + name string + origin string + }{ + {name: "missing", origin: ""}, + {name: "zero", origin: "0"}, + {name: "nondecimal", origin: "12x"}, + {name: "overflow", origin: "9223372036854775808"}, + } { + t.Run(test.name, func(t *testing.T) { + _, err := RunDriver(context.Background(), spec, RequestFromRunRoot(root, test.origin)) + if err == nil || !strings.Contains(err.Error(), "positive decimal int64") { + t.Fatalf("RunDriver origin %q error = %v, want positive decimal int64 rejection", test.origin, err) + } + if _, err := os.Stat(filepath.Join(root, "workspace", "launched")); !os.IsNotExist(err) { + t.Fatalf("launcher ran for rejected origin %q: %v", test.origin, err) + } + }) + } +} + +func TestRecorderEnvironmentUsesExactRunConfiguration(t *testing.T) { + const origin = "123456789" + root := filepath.Join(t.TempDir(), "duo-portable-launcher-environment") + environment := make(map[string]string) + for _, entry := range isolatedEnvironment(RequestFromRunRoot(root, origin)) { + key, value, ok := strings.Cut(entry, "=") + if !ok { + t.Fatalf("malformed environment entry %q", entry) + } + environment[key] = value + } + if got, want := environment[CommandCaptureDirectoryEnv], filepath.Join(root, "capture"); got != want { + t.Errorf("%s = %q, want %q", CommandCaptureDirectoryEnv, got, want) + } + if got := environment[CommandRunOriginEnv]; got != origin { + t.Errorf("%s = %q, want %q", CommandRunOriginEnv, got, origin) + } + if _, ok := environment["DUO_CONFORMANCE_RESULT_PATH"]; ok { + t.Fatal("isolated launcher environment contains result path") + } +} + +func TestDriverEnvironmentCannotOverrideRecorderConfiguration(t *testing.T) { + script := []byte("#!/bin/sh\nprintf launched > launched\n") + root, spec := prepareDriverFixture(t, "fixture-recorder-override", script) + for _, key := range []string{CommandCaptureDirectoryEnv, CommandRunOriginEnv} { + t.Run(key, func(t *testing.T) { + request := RequestFromRunRoot(root, "123456789") + request.Environment = map[string]string{key: "override"} + _, err := RunDriver(context.Background(), spec, request) + if err == nil || !strings.Contains(err.Error(), "environment key") { + t.Fatalf("RunDriver override of %s error = %v, want forbidden environment key", key, err) + } + if _, err := os.Stat(filepath.Join(root, "workspace", "launched")); !os.IsNotExist(err) { + t.Fatalf("launcher ran for forbidden %s override: %v", key, err) + } + }) + } +} + +func TestRunDriverSucceedsWithoutResultFileOrResultPathEnvironment(t *testing.T) { + root, err := os.MkdirTemp(t.TempDir(), "duo-portable-launcher-") + if err != nil { + t.Fatal(err) + } + for _, name := range []string{"bin", "workspace", "capture", "workspace/.duo-conformance"} { + if err := os.MkdirAll(filepath.Join(root, name), 0o700); err != nil { + t.Fatal(err) + } + } + executable := filepath.Join(root, "bin", "fixture-launcher") + script := []byte("#!/bin/sh\nif [ \"${DUO_CONFORMANCE_RESULT_PATH+x}\" = x ]; then\n printf 'unexpected result path\\n' >&2\n exit 9\nfi\n[ \"$#\" -eq 1 ] || exit 8\nprintf '%s\\n%s\\n%s' \"$DUO_CONFORMANCE_COMMAND_CAPTURE_DIR\" \"$DUO_CONFORMANCE_RUN_ORIGIN_BOOTTIME_NS\" \"$1\"\n") + if err := os.WriteFile(executable, script, 0o700); err != nil { + t.Fatal(err) + } + const launcher = "fixture-success" + acceptedLaunchers[launcher] = AcceptedLauncher{Version: "fixture", Digest: Digest(script)} + t.Cleanup(func() { delete(acceptedLaunchers, launcher) }) + scenario, err := ScenarioJSON(CanonicalScenario()) + if err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(root, "workspace", ".duo-conformance", "scenario.json"), scenario, 0o600); err != nil { + t.Fatal(err) + } + + const origin = "123456789" + request := RequestFromRunRoot(root, origin) + for _, entry := range isolatedEnvironment(request) { + if strings.HasPrefix(entry, "DUO_CONFORMANCE_RESULT_PATH=") { + t.Fatalf("isolated launcher environment contains result path: %q", entry) + } + } + capture, err := RunDriver(context.Background(), DriverSpec{ + Name: launcher, Executable: executable, Arguments: []string{TaskArgument}, + }, request) + if err != nil { + t.Fatalf("RunDriver without a result file failed: %v", err) + } + wantEvents := []byte(filepath.Join(root, "capture") + "\n" + origin + "\n" + string(CanonicalTaskBytes)) + if !reflect.DeepEqual(capture.Events, wantEvents) || len(capture.Stderr) != 0 { + t.Fatalf("successful launcher streams = events %q stderr %q", capture.Events, capture.Stderr) + } +} + +func TestRunDriverPreservesStreamsFromFailedLauncher(t *testing.T) { + root, err := os.MkdirTemp(t.TempDir(), "duo-portable-launcher-") + if err != nil { + t.Fatal(err) + } + for _, name := range []string{"bin", "workspace", "capture", "workspace/.duo-conformance"} { + if err := os.MkdirAll(filepath.Join(root, name), 0o700); err != nil { + t.Fatal(err) + } + } + executable := filepath.Join(root, "bin", "fixture-launcher") + script := []byte("#!/bin/sh\nprintf 'event\\n'\nprintf 'stderr\\n' >&2\nexit 7\n") + if err := os.WriteFile(executable, script, 0o700); err != nil { + t.Fatal(err) + } + const launcher = "fixture-failure" + acceptedLaunchers[launcher] = AcceptedLauncher{Version: "fixture", Digest: Digest(script)} + t.Cleanup(func() { delete(acceptedLaunchers, launcher) }) + scenario, err := ScenarioJSON(CanonicalScenario()) + if err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(root, "workspace", ".duo-conformance", "scenario.json"), scenario, 0o600); err != nil { + t.Fatal(err) + } + + capture, err := RunDriver(context.Background(), DriverSpec{ + Name: launcher, Executable: executable, Arguments: []string{TaskArgument}, + }, RequestFromRunRoot(root, "123456789")) + if err == nil || !strings.Contains(err.Error(), "launcher process failed") { + t.Fatalf("RunDriver error = %v, want launcher failure", err) + } + if string(capture.Events) != "event\n" || string(capture.Stderr) != "stderr\n" { + t.Fatalf("failed launcher streams not preserved: events=%q stderr=%q", capture.Events, capture.Stderr) + } +} + +func TestNeutralityAndThinDriverGuards(t *testing.T) { + for _, name := range []string{"scenario.go", "oracle.go", "validate.go"} { + data, err := os.ReadFile(name) + if err != nil { + t.Fatal(err) + } + if err := ValidateSemanticSource(data); err != nil { + t.Errorf("%s: %v", name, err) + } + } + for _, name := range []string{"amp", "opencode", "codex"} { + data, err := os.ReadFile(filepath.Join("../../../contrib/portable-launcher-conformance", name, "main.go")) + if err != nil { + t.Fatal(err) + } + if err := ValidateDriverSource(data); err != nil { + t.Errorf("%s driver: %v", name, err) + } + if strings.Count(string(data), "TaskArgument") != 1 { + t.Errorf("%s driver does not submit exactly one common task marker", name) + } + } + if ValidateSemanticSource([]byte(`switch launcher { case "x": }`)) == nil { + t.Fatal("launcher-specific semantic switch accepted") + } + if ValidateDriverSource([]byte(`duo session launch; retry; DUO_PORTABLE_LAUNCHER_OK_V1`)) == nil { + t.Fatal("semantic contamination in driver accepted") + } +} + +func TestPublicSchemaParsesAndIsClosed(t *testing.T) { + data, err := os.ReadFile("../../../contracts/schemas/duo-portable-launcher-conformance-result-v1.schema.json") + if err != nil { + t.Fatal(err) + } + var schema map[string]any + if err := json.Unmarshal(data, &schema); err != nil { + t.Fatal(err) + } + if schema["additionalProperties"] != false { + t.Fatal("result schema is not closed") + } + stages := schema["properties"].(map[string]any)["stages"].(map[string]any) + if stages["minItems"] != float64(32) || stages["maxItems"] != float64(32) { + t.Fatal("result schema does not close the 32-stage matrix") + } + defs, ok := schema["$defs"].(map[string]any) + if !ok { + t.Fatal("result schema has no definitions") + } + for _, name := range []string{"pins", "stage", "assertion", "error", "summary", "scrub"} { + def, ok := defs[name].(map[string]any) + if !ok || def["additionalProperties"] != false { + t.Errorf("schema definition %s is absent or open", name) + } + } + assertion := defs["assertion"].(map[string]any) + properties := assertion["properties"].(map[string]any) + idSchema := properties["id"].(map[string]any) + var gotIDs []string + for _, value := range idSchema["enum"].([]any) { + gotIDs = append(gotIDs, value.(string)) + } + wantIDs := append([]string(nil), CanonicalOracle().AssertionIDs...) + sort.Strings(gotIDs) + sort.Strings(wantIDs) + if !reflect.DeepEqual(gotIDs, wantIDs) { + t.Errorf("schema assertion enum = %v, oracle = %v", gotIDs, wantIDs) + } + for _, name := range []string{"result-structural-blocked.json", "result-rejected-fabricated-pass.json"} { + data, err := os.ReadFile(filepath.Join(fixtureDir, name)) + if err != nil { + t.Fatal(err) + } + var document any + if err := json.Unmarshal(data, &document); err != nil { + t.Fatal(err) + } + var problems Problems + validateSchemaNode(schema, schema, document, "$", &problems) + if err := problems.Err(); err != nil { + t.Errorf("%s does not satisfy the public schema: %v", name, err) + } + } +} + +func validateFixture(f builtFixture) (*Result, error) { + return Validate(ValidationInput{ + ResultJSON: marshalJSON(f.Result), IndexJSON: marshalJSON(f.Index), + ReadBlob: func(ref string) ([]byte, error) { + if ref != f.BlobRef { + return nil, fmt.Errorf("unknown reference %s", ref) + } + return append([]byte(nil), f.Blob...), nil + }, + }) +} + +func mutateAssertion(f *builtFixture, id, key string, value any) { + for i := range f.Result.Stages { + for j := range f.Result.Stages[i].Assertions { + if f.Result.Stages[i].Assertions[j].ID != id { + continue + } + f.Result.Stages[i].Assertions[j].Expected[key] = value + f.Result.Stages[i].Assertions[j].Actual[key] = value + f.Result.Stages[i].Assertions[j].Matched = true + } + } +} + +func mutateEvidence(t *testing.T, f *builtFixture, mutate func(*Evidence)) { + t.Helper() + var evidence Evidence + if err := json.Unmarshal(f.Blob, &evidence); err != nil { + t.Fatal(err) + } + mutate(&evidence) + capture := NewCapture() + ref, err := capture.AddJSON("common_collector", evidence) + if err != nil { + t.Fatal(err) + } + f.BlobRef = ref + f.Blob, _ = capture.Blob(ref) + f.Index = capture.Index() + for i := range f.Result.Stages { + f.Result.Stages[i].Evidence = []string{ref} + } +} + +func failStage(stage *StageResult, code string) { + stage.Verdict, stage.Outcome = "fail", "error" + stage.Error = &StageError{Code: code, Message: "fixture failure", Effect: "no_effect", Retry: Retry{Action: "inspect_failure"}} + stage.Assertions = []Assertion{{ID: "stage.failure", Expected: map[string]any{"success": true}, Actual: map[string]any{"success": false}, Matched: false}} +} + +func stageIndex(r Result, caseName, stageName string) int { + for i, stage := range r.Stages { + if stage.Case == caseName && stage.Stage == stageName { + return i + } + } + return -1 +} + +func cloneMap(t *testing.T, in map[string]any) map[string]any { + t.Helper() + b, _ := json.Marshal(in) + var out map[string]any + if err := json.Unmarshal(b, &out); err != nil { + t.Fatal(err) + } + return out +} + +func cloneResult(t *testing.T, in Result) Result { + t.Helper() + b, _ := json.Marshal(in) + var out Result + if err := json.Unmarshal(b, &out); err != nil { + t.Fatal(err) + } + return out +} + +func marshalFixture(t *testing.T, value any) []byte { + t.Helper() + b, err := json.MarshalIndent(value, "", " ") + if err != nil { + t.Fatal(err) + } + return append(b, '\n') +} + +func marshalJSON(value any) []byte { b, _ := json.Marshal(value); return b } +func stringPointer(value string) *string { return &value } + +func assertDigest(t *testing.T, data []byte, want string) { + t.Helper() + if got := Digest(data); got != want { + t.Fatalf("digest = %s, want %s", got, want) + } +} + +func directoryNames(t *testing.T, dir string) []string { + t.Helper() + entries, err := os.ReadDir(dir) + if err != nil { + t.Fatal(err) + } + out := make([]string, 0, len(entries)) + for _, entry := range entries { + out = append(out, entry.Name()) + } + sort.Strings(out) + return out +} diff --git a/internal/conformance/portablelauncher/process_control_linux.go b/internal/conformance/portablelauncher/process_control_linux.go new file mode 100644 index 0000000..c87a3c8 --- /dev/null +++ b/internal/conformance/portablelauncher/process_control_linux.go @@ -0,0 +1,372 @@ +package portablelauncher + +import ( + "bytes" + "context" + "errors" + "fmt" + "math" + "os" + "strconv" + "strings" + "sync" + "time" + + "golang.org/x/sys/unix" +) + +const ( + // linuxProcessBirthLayout is the millisecond UTC representation carried by + // ProcessIdentity.StartTime and by launch/attachment evidence. + linuxProcessBirthLayout = "2006-01-02T15:04:05.000Z" + + // Linux stop delivery is asynchronous. Two seconds is deliberately much + // longer than an ordinary scheduler transition while still bounding a + // failed induction well below the suite's observation deadlines. + linuxStopObservationTimeout = 2 * time.Second + linuxStopPollInterval = 20 * time.Millisecond + + // Linux exposes process start time in USER_HZ units. USER_HZ is 100 on + // every Linux architecture supported by Go, matching the Herdr adapter's + // documented procfs birth-evidence assumption. + linuxClockTicksPerSecond = int64(100) +) + +// LinuxProcessControl owns pidfds for exact conformance-fixture process +// identities. It never degrades to PID-only signaling. +type LinuxProcessControl struct { + mu sync.RWMutex + + pidfds map[ProcessIdentity]int + closed bool + stopTimeout time.Duration + pollInterval time.Duration +} + +// NewLinuxProcessControl constructs an empty pidfd-backed process control. +func NewLinuxProcessControl() *LinuxProcessControl { + return newLinuxProcessControl(linuxStopObservationTimeout, linuxStopPollInterval) +} + +func newLinuxProcessControl(stopTimeout, pollInterval time.Duration) *LinuxProcessControl { + return &LinuxProcessControl{ + pidfds: make(map[ProcessIdentity]int), + stopTimeout: stopTimeout, + pollInterval: pollInterval, + } +} + +// AcquireExactTarget opens a pidfd before re-reading procfs birth evidence. +// That order closes the PID-reuse window between evidence capture and pidfd +// acquisition. Every failure after PidfdOpen closes the new descriptor. +func (c *LinuxProcessControl) AcquireExactTarget(ctx context.Context, target ProcessIdentity) error { + if err := validateLinuxProcessIdentity(target); err != nil { + return err + } + if err := ctx.Err(); err != nil { + return err + } + + fd, err := unix.PidfdOpen(target.PID, 0) + if err != nil { + return fmt.Errorf("pidfd_open exact target %d (pidfd support is required): %w", target.PID, err) + } + owned := false + defer func() { + if !owned { + _ = unix.Close(fd) + } + }() + + observation, err := readLinuxProcessObservation(target.PID) + if err != nil { + return fmt.Errorf("verify exact target after pidfd_open: %w", err) + } + if err := compareLinuxProcessBirth(target, observation); err != nil { + return fmt.Errorf("verify exact target after pidfd_open: %w", err) + } + if !linuxProcessStateLive(observation.state) { + return fmt.Errorf("verify exact target after pidfd_open: process state %q is not live", observation.state) + } + if err := ctx.Err(); err != nil { + return err + } + + c.mu.Lock() + defer c.mu.Unlock() + if c.closed { + return fmt.Errorf("linux process control is closed") + } + if _, exists := c.pidfds[target]; exists { + return fmt.Errorf("exact target is already acquired") + } + c.pidfds[target] = fd + owned = true + return nil +} + +// VerifyExactTarget independently re-reads procfs and requires the complete +// identity to still select the process represented by an owned pidfd. +func (c *LinuxProcessControl) VerifyExactTarget(ctx context.Context, target ProcessIdentity) error { + if err := validateLinuxProcessIdentity(target); err != nil { + return err + } + if err := ctx.Err(); err != nil { + return err + } + + c.mu.RLock() + defer c.mu.RUnlock() + observation, err := c.verifyExactTargetLocked(target) + if err != nil { + return err + } + if !linuxProcessStateLive(observation.state) { + return fmt.Errorf("exact target process state %q is not live", observation.state) + } + return ctx.Err() +} + +// SendSIGSTOP sends SIGSTOP through the acquired pidfd. There is intentionally +// no kill(2), shell-command, terminal, or other PID-only fallback. +func (c *LinuxProcessControl) SendSIGSTOP(ctx context.Context, target ProcessIdentity) error { + return c.sendOwnedPidfdSignal(ctx, target, unix.SIGSTOP, "SIGSTOP") +} + +// SendSIGKILL sends SIGKILL through the acquired pidfd for exact cleanup +// escalation. It retains the pidfd so the caller continues to own observation +// and must release the target explicitly. +func (c *LinuxProcessControl) SendSIGKILL(ctx context.Context, target ProcessIdentity) error { + return c.sendOwnedPidfdSignal(ctx, target, unix.SIGKILL, "SIGKILL") +} + +func (c *LinuxProcessControl) sendOwnedPidfdSignal(ctx context.Context, target ProcessIdentity, signal unix.Signal, signalName string) error { + if err := validateLinuxProcessIdentity(target); err != nil { + return err + } + if err := ctx.Err(); err != nil { + return err + } + + c.mu.RLock() + defer c.mu.RUnlock() + fd, ok := c.pidfds[target] + if !ok { + return fmt.Errorf("exact target is not acquired") + } + if err := unix.PidfdSendSignal(fd, signal, nil, 0); err != nil { + return fmt.Errorf("pidfd_send_signal %s: %w", signalName, err) + } + return nil +} + +// VerifyStopped waits for Linux to report a stopped or tracing-stop state, +// rechecking exact process birth on every observation. It returns early for a +// missing, replaced, zombie, or dead process and honors caller cancellation. +func (c *LinuxProcessControl) VerifyStopped(ctx context.Context, target ProcessIdentity) error { + if err := validateLinuxProcessIdentity(target); err != nil { + return err + } + if err := ctx.Err(); err != nil { + return err + } + if c.stopTimeout <= 0 || c.pollInterval <= 0 { + return fmt.Errorf("linux process control has invalid stop observation bounds") + } + + stopContext, cancel := context.WithTimeout(ctx, c.stopTimeout) + defer cancel() + for { + c.mu.RLock() + observation, err := c.verifyExactTargetLocked(target) + c.mu.RUnlock() + if err != nil { + return err + } + switch observation.state { + case 'T', 't': + return nil + case 'Z', 'X', 'x': + return fmt.Errorf("exact target exited before stopped-state verification (state %q)", observation.state) + } + + timer := time.NewTimer(c.pollInterval) + select { + case <-stopContext.Done(): + if !timer.Stop() { + select { + case <-timer.C: + default: + } + } + if err := ctx.Err(); err != nil { + return err + } + return fmt.Errorf("exact target did not enter a stopped state within %s: %w", c.stopTimeout, stopContext.Err()) + case <-timer.C: + } + } +} + +// Release closes and forgets the pidfd owned for exactly target. +func (c *LinuxProcessControl) Release(target ProcessIdentity) error { + if err := validateLinuxProcessIdentity(target); err != nil { + return err + } + c.mu.Lock() + defer c.mu.Unlock() + fd, ok := c.pidfds[target] + if !ok { + return fmt.Errorf("exact target is not acquired") + } + delete(c.pidfds, target) + if err := unix.Close(fd); err != nil { + return fmt.Errorf("close exact target pidfd: %w", err) + } + return nil +} + +// Close closes every owned pidfd. It is safe to call more than once; after it +// returns, the control cannot acquire new targets. +func (c *LinuxProcessControl) Close() error { + c.mu.Lock() + defer c.mu.Unlock() + if c.closed { + return nil + } + c.closed = true + var closeErrors []error + for target, fd := range c.pidfds { + delete(c.pidfds, target) + if err := unix.Close(fd); err != nil { + closeErrors = append(closeErrors, fmt.Errorf("close pidfd for pid %d: %w", target.PID, err)) + } + } + return errors.Join(closeErrors...) +} + +func (c *LinuxProcessControl) verifyExactTargetLocked(target ProcessIdentity) (linuxProcessObservation, error) { + if _, ok := c.pidfds[target]; !ok { + return linuxProcessObservation{}, fmt.Errorf("exact target is not acquired") + } + observation, err := readLinuxProcessObservation(target.PID) + if err != nil { + return linuxProcessObservation{}, fmt.Errorf("read exact target: %w", err) + } + if err := compareLinuxProcessBirth(target, observation); err != nil { + return linuxProcessObservation{}, err + } + return observation, nil +} + +func validateLinuxProcessIdentity(target ProcessIdentity) error { + if target.PID <= 0 { + return fmt.Errorf("exact target requires a positive PID") + } + if strings.TrimSpace(target.TerminalID) == "" || strings.TrimSpace(target.AttachmentID) == "" { + return fmt.Errorf("exact target requires a complete attachment identity") + } + parsed, err := time.Parse(linuxProcessBirthLayout, target.StartTime) + if err != nil || parsed.UTC().Format(linuxProcessBirthLayout) != target.StartTime || parsed.UnixMilli() < 0 { + return fmt.Errorf("exact target requires a canonical non-negative millisecond UTC start time") + } + return nil +} + +type linuxProcessObservation struct { + state byte + startTime string +} + +func readLinuxProcessObservation(pid int) (linuxProcessObservation, error) { + raw, err := os.ReadFile("/proc/" + strconv.Itoa(pid) + "/stat") + if err != nil { + return linuxProcessObservation{}, fmt.Errorf("read /proc/%d/stat: %w", pid, err) + } + space := bytes.IndexByte(raw, ' ') + if space <= 0 { + return linuxProcessObservation{}, fmt.Errorf("/proc/%d/stat has no PID field", pid) + } + reportedPID, err := strconv.Atoi(string(raw[:space])) + if err != nil || reportedPID != pid { + return linuxProcessObservation{}, fmt.Errorf("/proc/%d/stat PID field does not match", pid) + } + // comm is parenthesized and can itself contain spaces or ')'. The final + // ')' is therefore the only safe place from which to count later fields. + end := bytes.LastIndexByte(raw, ')') + if end < space+2 || end+2 >= len(raw) || raw[end+1] != ' ' { + return linuxProcessObservation{}, fmt.Errorf("/proc/%d/stat has no complete comm field", pid) + } + fields := bytes.Fields(raw[end+2:]) + // After comm, field 3 (state) is index 0 and field 22 (starttime) is 19. + const startTimeIndex = 19 + if len(fields) <= startTimeIndex || len(fields[0]) != 1 { + return linuxProcessObservation{}, fmt.Errorf("/proc/%d/stat is incomplete", pid) + } + if !linuxProcessStateValid(fields[0][0]) { + return linuxProcessObservation{}, fmt.Errorf("/proc/%d/stat has invalid process state %q", pid, fields[0][0]) + } + ticks, err := strconv.ParseInt(string(fields[startTimeIndex]), 10, 64) + if err != nil || ticks < 0 { + return linuxProcessObservation{}, fmt.Errorf("/proc/%d/stat has invalid starttime", pid) + } + + bootSeconds, err := readLinuxBootTime() + if err != nil { + return linuxProcessObservation{}, err + } + wholeSeconds := ticks / linuxClockTicksPerSecond + if wholeSeconds > math.MaxInt64-bootSeconds { + return linuxProcessObservation{}, fmt.Errorf("/proc/%d/stat starttime overflows wall clock", pid) + } + nanos := (ticks % linuxClockTicksPerSecond) * (int64(time.Second) / linuxClockTicksPerSecond) + started := time.Unix(bootSeconds+wholeSeconds, nanos).UTC() + return linuxProcessObservation{state: fields[0][0], startTime: started.Format(linuxProcessBirthLayout)}, nil +} + +func readLinuxBootTime() (int64, error) { + raw, err := os.ReadFile("/proc/stat") + if err != nil { + return 0, fmt.Errorf("read /proc/stat: %w", err) + } + for _, line := range bytes.Split(raw, []byte("\n")) { + fields := bytes.Fields(line) + if len(fields) == 0 || !bytes.Equal(fields[0], []byte("btime")) { + continue + } + if len(fields) != 2 { + return 0, fmt.Errorf("/proc/stat has malformed btime") + } + seconds, err := strconv.ParseInt(string(fields[1]), 10, 64) + if err != nil || seconds < 0 { + return 0, fmt.Errorf("/proc/stat has invalid btime") + } + return seconds, nil + } + return 0, fmt.Errorf("/proc/stat has no btime") +} + +func compareLinuxProcessBirth(target ProcessIdentity, observation linuxProcessObservation) error { + if observation.startTime != target.StartTime { + return fmt.Errorf("exact target process birth mismatch: observed %s, requested %s", observation.startTime, target.StartTime) + } + return nil +} + +func linuxProcessStateLive(state byte) bool { + switch state { + case 'Z', 'X', 'x': + return false + default: + return true + } +} + +func linuxProcessStateValid(state byte) bool { + switch state { + case 'R', 'S', 'D', 'Z', 'T', 't', 'X', 'x', 'K', 'W', 'P', 'I': + return true + default: + return false + } +} diff --git a/internal/conformance/portablelauncher/process_control_linux_test.go b/internal/conformance/portablelauncher/process_control_linux_test.go new file mode 100644 index 0000000..53873cb --- /dev/null +++ b/internal/conformance/portablelauncher/process_control_linux_test.go @@ -0,0 +1,500 @@ +package portablelauncher + +import ( + "bytes" + "context" + "errors" + "os" + "os/exec" + "path/filepath" + "strconv" + "strings" + "sync" + "testing" + "time" + + "golang.org/x/sys/unix" +) + +const processControlHelperEnvironment = "DUO_PROCESS_CONTROL_HELPER=1" + +func TestProcessControlHelperProcess(_ *testing.T) { + if os.Getenv("DUO_PROCESS_CONTROL_HELPER") != "1" { + return + } + for { + time.Sleep(time.Hour) + } +} + +func TestLinuxProcessControlAcquireStopVerifyRelease(t *testing.T) { + child := startProcessControlChild(t) + control := NewLinuxProcessControl() + t.Cleanup(func() { _ = control.Close() }) + target := child.identity(t, "terminal-success", "attachment-success") + + if err := control.AcquireExactTarget(context.Background(), target); err != nil { + t.Fatalf("AcquireExactTarget: %v", err) + } + if err := control.VerifyExactTarget(context.Background(), target); err != nil { + t.Fatalf("VerifyExactTarget: %v", err) + } + if err := control.SendSIGSTOP(context.Background(), target); err != nil { + t.Fatalf("SendSIGSTOP: %v", err) + } + if err := control.VerifyStopped(context.Background(), target); err != nil { + t.Fatalf("VerifyStopped: %v", err) + } + if state := testProcessState(t, child.pid); state != 'T' && state != 't' { + t.Fatalf("child state = %q, want stopped or tracing-stop", state) + } + if err := control.Release(target); err != nil { + t.Fatalf("Release: %v", err) + } + if err := control.VerifyExactTarget(context.Background(), target); err == nil || !strings.Contains(err.Error(), "not acquired") { + t.Fatalf("VerifyExactTarget after Release error = %v, want not acquired", err) + } +} + +func TestLinuxProcessControlSendSIGKILLTerminatesOnlyExactAcquiredChild(t *testing.T) { + targetChild := startProcessControlChild(t) + bystander := startProcessControlChild(t) + control := NewLinuxProcessControl() + t.Cleanup(func() { _ = control.Close() }) + target := targetChild.identity(t, "terminal-kill", "attachment-kill") + + if err := control.AcquireExactTarget(context.Background(), target); err != nil { + t.Fatalf("AcquireExactTarget: %v", err) + } + ownedFD := control.pidfds[target] + if err := control.SendSIGKILL(context.Background(), target); err != nil { + t.Fatalf("SendSIGKILL: %v", err) + } + targetChild.waitForExit(t) + + if got := control.pidfds[target]; got != ownedFD { + t.Fatalf("owned pidfd after SIGKILL = %d, want retained fd %d", got, ownedFD) + } + if _, err := unix.FcntlInt(uintptr(ownedFD), unix.F_GETFD, 0); err != nil { + t.Fatalf("owned pidfd after SIGKILL is not open: %v", err) + } + assertProcessControlChildLive(t, bystander) + if err := control.Release(target); err != nil { + t.Fatalf("Release: %v", err) + } + if _, err := unix.FcntlInt(uintptr(ownedFD), unix.F_GETFD, 0); !errors.Is(err, unix.EBADF) { + t.Fatalf("F_GETFD(%d) after Release error = %v, want EBADF", ownedFD, err) + } +} + +func TestLinuxProcessControlWrongBirthFailsAcquireAndLeavesChildRunning(t *testing.T) { + child := startProcessControlChild(t) + control := NewLinuxProcessControl() + t.Cleanup(func() { _ = control.Close() }) + target := child.identity(t, "terminal-wrong-birth", "attachment-wrong-birth") + started, err := time.Parse(testProcessBirthLayout, target.StartTime) + if err != nil { + t.Fatalf("parse independently derived start time: %v", err) + } + target.StartTime = started.Add(time.Millisecond).Format(testProcessBirthLayout) + before := countOpenTestPidfds(t) + + if err := control.AcquireExactTarget(context.Background(), target); err == nil || !strings.Contains(err.Error(), "birth mismatch") { + t.Fatalf("AcquireExactTarget error = %v, want birth mismatch", err) + } + if state := testProcessState(t, child.pid); state == 'T' || state == 't' || state == 'Z' || state == 'X' || state == 'x' { + t.Fatalf("child state after failed acquire = %q, want running live process", state) + } + if got := len(control.pidfds); got != 0 { + t.Fatalf("owned pidfds after failed acquire = %d, want 0", got) + } + if got := countOpenTestPidfds(t); got != before { + t.Fatalf("pidfd count after failed acquire = %d, want %d", got, before) + } +} + +func TestLinuxProcessControlOperationsWithoutAcquireFail(t *testing.T) { + child := startProcessControlChild(t) + control := NewLinuxProcessControl() + t.Cleanup(func() { _ = control.Close() }) + target := child.identity(t, "terminal-unacquired", "attachment-unacquired") + + operations := []struct { + name string + run func() error + }{ + {name: "verify", run: func() error { return control.VerifyExactTarget(context.Background(), target) }}, + {name: "stop", run: func() error { return control.SendSIGSTOP(context.Background(), target) }}, + {name: "kill", run: func() error { return control.SendSIGKILL(context.Background(), target) }}, + {name: "verify stopped", run: func() error { return control.VerifyStopped(context.Background(), target) }}, + {name: "release", run: func() error { return control.Release(target) }}, + } + for _, operation := range operations { + if err := operation.run(); err == nil || !strings.Contains(err.Error(), "not acquired") { + t.Errorf("%s error = %v, want not acquired", operation.name, err) + } + } + assertProcessControlChildLive(t, child) +} + +func TestLinuxProcessControlDuplicateAcquirePreservesExistingHandle(t *testing.T) { + child := startProcessControlChild(t) + control := NewLinuxProcessControl() + t.Cleanup(func() { _ = control.Close() }) + target := child.identity(t, "terminal-duplicate", "attachment-duplicate") + before := countOpenTestPidfds(t) + + if err := control.AcquireExactTarget(context.Background(), target); err != nil { + t.Fatalf("first AcquireExactTarget: %v", err) + } + fd := control.pidfds[target] + if got := countOpenTestPidfds(t); got != before+1 { + t.Fatalf("pidfd count after first acquire = %d, want %d", got, before+1) + } + if err := control.AcquireExactTarget(context.Background(), target); err == nil || !strings.Contains(err.Error(), "already acquired") { + t.Fatalf("duplicate AcquireExactTarget error = %v, want already acquired", err) + } + if got := control.pidfds[target]; got != fd { + t.Fatalf("pidfd after duplicate acquire = %d, want original %d", got, fd) + } + if got := countOpenTestPidfds(t); got != before+1 { + t.Fatalf("pidfd count after duplicate acquire = %d, want %d", got, before+1) + } + if err := control.SendSIGSTOP(context.Background(), target); err != nil { + t.Fatalf("SendSIGSTOP through preserved handle: %v", err) + } + if err := control.VerifyStopped(context.Background(), target); err != nil { + t.Fatalf("VerifyStopped through preserved handle: %v", err) + } + if err := control.Release(target); err != nil { + t.Fatalf("Release: %v", err) + } + if got := countOpenTestPidfds(t); got != before { + t.Fatalf("pidfd count after release = %d, want %d", got, before) + } +} + +func TestLinuxProcessControlVerifyStoppedHonorsCancellationAndTimeout(t *testing.T) { + child := startProcessControlChild(t) + target := child.identity(t, "terminal-bounds", "attachment-bounds") + + t.Run("caller cancellation", func(t *testing.T) { + control := NewLinuxProcessControl() + t.Cleanup(func() { _ = control.Close() }) + if err := control.AcquireExactTarget(context.Background(), target); err != nil { + t.Fatalf("AcquireExactTarget: %v", err) + } + ctx, cancel := context.WithCancel(context.Background()) + cancel() + err := control.VerifyStopped(ctx, target) + if !errors.Is(err, context.Canceled) { + t.Fatalf("VerifyStopped error = %v, want context canceled", err) + } + }) + + t.Run("bounded timeout", func(t *testing.T) { + control := newLinuxProcessControl(45*time.Millisecond, 10*time.Millisecond) + t.Cleanup(func() { _ = control.Close() }) + if err := control.AcquireExactTarget(context.Background(), target); err != nil { + t.Fatalf("AcquireExactTarget: %v", err) + } + err := control.VerifyStopped(context.Background(), target) + if !errors.Is(err, context.DeadlineExceeded) { + t.Fatalf("VerifyStopped error = %v, want bounded deadline", err) + } + }) +} + +func TestLinuxProcessControlCloseAllIsIdempotent(t *testing.T) { + first := startProcessControlChild(t) + second := startProcessControlChild(t) + control := NewLinuxProcessControl() + firstTarget := first.identity(t, "terminal-close-first", "attachment-close-first") + secondTarget := second.identity(t, "terminal-close-second", "attachment-close-second") + for _, target := range []ProcessIdentity{firstTarget, secondTarget} { + if err := control.AcquireExactTarget(context.Background(), target); err != nil { + t.Fatalf("AcquireExactTarget(%d): %v", target.PID, err) + } + } + fds := []int{control.pidfds[firstTarget], control.pidfds[secondTarget]} + + if err := control.Close(); err != nil { + t.Fatalf("first Close: %v", err) + } + if err := control.Close(); err != nil { + t.Fatalf("second Close: %v", err) + } + if got := len(control.pidfds); got != 0 { + t.Fatalf("owned pidfds after Close = %d, want 0", got) + } + for _, fd := range fds { + if _, err := unix.FcntlInt(uintptr(fd), unix.F_GETFD, 0); !errors.Is(err, unix.EBADF) { + t.Fatalf("F_GETFD(%d) error = %v, want EBADF", fd, err) + } + } + if err := control.AcquireExactTarget(context.Background(), firstTarget); err == nil || !strings.Contains(err.Error(), "closed") { + t.Fatalf("AcquireExactTarget after Close error = %v, want closed", err) + } +} + +func TestLinuxProcessControlDoesNotReuseStateAcrossFullIdentities(t *testing.T) { + child := startProcessControlChild(t) + control := NewLinuxProcessControl() + t.Cleanup(func() { _ = control.Close() }) + acquired := child.identity(t, "terminal-original", "attachment-original") + if err := control.AcquireExactTarget(context.Background(), acquired); err != nil { + t.Fatalf("AcquireExactTarget: %v", err) + } + + mismatches := []ProcessIdentity{ + {PID: acquired.PID, StartTime: acquired.StartTime, TerminalID: "terminal-other", AttachmentID: acquired.AttachmentID}, + {PID: acquired.PID, StartTime: acquired.StartTime, TerminalID: acquired.TerminalID, AttachmentID: "attachment-other"}, + } + for _, mismatch := range mismatches { + if err := control.VerifyExactTarget(context.Background(), mismatch); err == nil || !strings.Contains(err.Error(), "not acquired") { + t.Errorf("VerifyExactTarget(%+v) error = %v, want not acquired", mismatch, err) + } + if err := control.SendSIGSTOP(context.Background(), mismatch); err == nil || !strings.Contains(err.Error(), "not acquired") { + t.Errorf("SendSIGSTOP(%+v) error = %v, want not acquired", mismatch, err) + } + if err := control.SendSIGKILL(context.Background(), mismatch); err == nil || !strings.Contains(err.Error(), "not acquired") { + t.Errorf("SendSIGKILL(%+v) error = %v, want not acquired", mismatch, err) + } + } + assertProcessControlChildLive(t, child) + if err := control.VerifyExactTarget(context.Background(), acquired); err != nil { + t.Fatalf("VerifyExactTarget(original): %v", err) + } +} + +func TestLinuxProcessControlSendSIGKILLValidatesIdentityAndContext(t *testing.T) { + child := startProcessControlChild(t) + control := NewLinuxProcessControl() + t.Cleanup(func() { _ = control.Close() }) + target := child.identity(t, "terminal-kill-validation", "attachment-kill-validation") + if err := control.AcquireExactTarget(context.Background(), target); err != nil { + t.Fatalf("AcquireExactTarget: %v", err) + } + + canceled, cancel := context.WithCancel(context.Background()) + cancel() + if err := control.SendSIGKILL(canceled, target); !errors.Is(err, context.Canceled) { + t.Fatalf("SendSIGKILL with canceled context error = %v, want context canceled", err) + } + malformed := target + malformed.AttachmentID = "" + if err := control.SendSIGKILL(context.Background(), malformed); err == nil || !strings.Contains(err.Error(), "complete attachment identity") { + t.Fatalf("SendSIGKILL with incomplete identity error = %v, want identity refusal", err) + } + assertProcessControlChildLive(t, child) +} + +func TestLinuxProcessControlRejectsMalformedIdentityBeforePidfdOpen(t *testing.T) { + validTime := time.Unix(1_700_000_000, 0).UTC().Format(testProcessBirthLayout) + tests := []ProcessIdentity{ + {}, + {PID: -1, StartTime: validTime, TerminalID: "terminal", AttachmentID: "attachment"}, + {PID: 1, StartTime: "not-a-time", TerminalID: "terminal", AttachmentID: "attachment"}, + {PID: 1, StartTime: "1960-01-01T00:00:00.000Z", TerminalID: "terminal", AttachmentID: "attachment"}, + {PID: 1, StartTime: validTime, TerminalID: "", AttachmentID: "attachment"}, + {PID: 1, StartTime: validTime, TerminalID: "terminal", AttachmentID: ""}, + } + for _, target := range tests { + control := NewLinuxProcessControl() + if err := control.AcquireExactTarget(context.Background(), target); err == nil { + t.Errorf("AcquireExactTarget(%+v) succeeded", target) + } + if got := len(control.pidfds); got != 0 { + t.Errorf("AcquireExactTarget(%+v) owned %d pidfds, want 0", target, got) + } + _ = control.Close() + } +} + +func TestLinuxProcessControlSourceHasNoPIDSignalFallback(t *testing.T) { + source, err := os.ReadFile("process_control_linux.go") + if err != nil { + t.Fatalf("read implementation source: %v", err) + } + if !bytes.Contains(source, []byte("unix.PidfdSendSignal(")) { + t.Fatal("implementation does not contain pidfd signal delivery") + } + for _, forbidden := range []string{ + "unix.Kill(", "syscall.Kill(", "exec.Command(", "os.Process.Signal(", + "os.Process.Kill(", "pkill", "/bin/kill", "pane.send_text", "pane.send_keys", + } { + if bytes.Contains(source, []byte(forbidden)) { + t.Errorf("implementation contains forbidden PID/command fallback %q", forbidden) + } + } +} + +type processControlChild struct { + pid int + cleanupFD int + command *exec.Cmd + once sync.Once +} + +func startProcessControlChild(t *testing.T) *processControlChild { + t.Helper() + command := exec.Command(os.Args[0], "-test.run=^TestProcessControlHelperProcess$") + command.Env = append(os.Environ(), processControlHelperEnvironment) + if err := command.Start(); err != nil { + t.Fatalf("start disposable child: %v", err) + } + cleanupFD, err := unix.PidfdOpen(command.Process.Pid, 0) + if err != nil { + _ = command.Process.Kill() + _ = command.Wait() + t.Fatalf("open test cleanup pidfd: %v", err) + } + child := &processControlChild{pid: command.Process.Pid, cleanupFD: cleanupFD, command: command} + t.Cleanup(func() { child.cleanup(t) }) + return child +} + +func (c *processControlChild) cleanup(t *testing.T) { + t.Helper() + c.once.Do(func() { + if err := unix.PidfdSendSignal(c.cleanupFD, unix.SIGCONT, nil, 0); err != nil && !errors.Is(err, unix.ESRCH) { + t.Errorf("resume disposable child: %v", err) + } + if err := unix.PidfdSendSignal(c.cleanupFD, unix.SIGKILL, nil, 0); err != nil && !errors.Is(err, unix.ESRCH) { + t.Errorf("kill disposable child: %v", err) + } + c.waitAndClose(t) + }) +} + +func (c *processControlChild) waitForExit(t *testing.T) { + t.Helper() + c.once.Do(func() { c.waitAndClose(t) }) +} + +func (c *processControlChild) waitAndClose(t *testing.T) { + t.Helper() + pollFDs := []unix.PollFd{{Fd: int32(c.cleanupFD), Events: unix.POLLIN}} + ready, err := unix.Poll(pollFDs, 2_000) + if err != nil || ready != 1 || pollFDs[0].Revents&unix.POLLIN == 0 { + t.Errorf("disposable child pidfd did not report exit: ready=%d revents=%#x err=%v", ready, pollFDs[0].Revents, err) + if killErr := unix.PidfdSendSignal(c.cleanupFD, unix.SIGKILL, nil, 0); killErr != nil && !errors.Is(killErr, unix.ESRCH) { + t.Errorf("fallback kill disposable child: %v", killErr) + } + pollFDs[0].Revents = 0 + if cleanupReady, cleanupErr := unix.Poll(pollFDs, 2_000); cleanupErr != nil || cleanupReady != 1 || pollFDs[0].Revents&unix.POLLIN == 0 { + t.Errorf("disposable child pidfd did not report exit after fallback cleanup: ready=%d revents=%#x err=%v", cleanupReady, pollFDs[0].Revents, cleanupErr) + } + } + if err := c.command.Wait(); err != nil { + var exitError *exec.ExitError + if !errors.As(err, &exitError) { + t.Errorf("wait for disposable child: %v", err) + } + } + if err := unix.Close(c.cleanupFD); err != nil { + t.Errorf("close test cleanup pidfd: %v", err) + } +} + +func assertProcessControlChildLive(t *testing.T, child *processControlChild) { + t.Helper() + if err := unix.PidfdSendSignal(child.cleanupFD, 0, nil, 0); err != nil { + t.Fatalf("disposable child is not live: %v", err) + } + if state := testProcessState(t, child.pid); state == 'Z' || state == 'X' || state == 'x' { + t.Fatalf("disposable child state = %q, want live", state) + } +} + +const ( + testProcessBirthLayout = "2006-01-02T15:04:05.000Z" + testLinuxClockTicks = int64(100) +) + +func (c *processControlChild) identity(t *testing.T, terminalID, attachmentID string) ProcessIdentity { + t.Helper() + return ProcessIdentity{ + PID: c.pid, + StartTime: independentlyDerivedProcessBirth(t, c.pid), + TerminalID: terminalID, + AttachmentID: attachmentID, + } +} + +func independentlyDerivedProcessBirth(t *testing.T, pid int) string { + t.Helper() + raw := readTestProcStat(t, pid) + end := bytes.LastIndexByte(raw, ')') + if end < 0 || end+2 >= len(raw) { + t.Fatalf("child /proc stat has no comm terminator: %q", raw) + } + fields := bytes.Fields(raw[end+2:]) + if len(fields) <= 19 { + t.Fatalf("child /proc stat has %d post-comm fields", len(fields)) + } + ticks, err := strconv.ParseInt(string(fields[19]), 10, 64) + if err != nil { + t.Fatalf("parse child start ticks: %v", err) + } + procStat, err := os.ReadFile("/proc/stat") + if err != nil { + t.Fatalf("read /proc/stat: %v", err) + } + var bootSeconds int64 = -1 + for _, line := range bytes.Split(procStat, []byte("\n")) { + fields := bytes.Fields(line) + if len(fields) == 2 && string(fields[0]) == "btime" { + bootSeconds, err = strconv.ParseInt(string(fields[1]), 10, 64) + if err != nil { + t.Fatalf("parse test btime: %v", err) + } + break + } + } + if bootSeconds < 0 { + t.Fatal("/proc/stat has no valid btime") + } + seconds := ticks / testLinuxClockTicks + nanos := (ticks % testLinuxClockTicks) * int64(time.Second) / testLinuxClockTicks + return time.Unix(bootSeconds+seconds, nanos).UTC().Format(testProcessBirthLayout) +} + +func testProcessState(t *testing.T, pid int) byte { + t.Helper() + raw := readTestProcStat(t, pid) + end := bytes.LastIndexByte(raw, ')') + if end < 0 || end+2 >= len(raw) { + t.Fatalf("child /proc stat has no state: %q", raw) + } + fields := bytes.Fields(raw[end+2:]) + if len(fields) == 0 || len(fields[0]) != 1 { + t.Fatalf("child /proc stat has malformed state: %q", raw) + } + return fields[0][0] +} + +func readTestProcStat(t *testing.T, pid int) []byte { + t.Helper() + raw, err := os.ReadFile(filepath.Join("/proc", strconv.Itoa(pid), "stat")) + if err != nil { + t.Fatalf("read child /proc stat: %v", err) + } + return raw +} + +func countOpenTestPidfds(t *testing.T) int { + t.Helper() + entries, err := os.ReadDir("/proc/self/fd") + if err != nil { + t.Fatalf("read /proc/self/fd: %v", err) + } + count := 0 + for _, entry := range entries { + target, err := os.Readlink(filepath.Join("/proc/self/fd", entry.Name())) + if err == nil && target == "anon_inode:[pidfd]" { + count++ + } + } + return count +} diff --git a/internal/conformance/portablelauncher/runner.go b/internal/conformance/portablelauncher/runner.go new file mode 100644 index 0000000..0202665 --- /dev/null +++ b/internal/conformance/portablelauncher/runner.go @@ -0,0 +1,375 @@ +package portablelauncher + +import ( + "context" + "errors" + "fmt" + "io" + "time" +) + +const cleanupTimeout = 30 * time.Second + +// RunnerCheckpointKind identifies a raw fact observed by the checkpoint +// source. Checkpoints intentionally cannot carry a verdict, assertion, or +// expected value. +type RunnerCheckpointKind string + +const ( + // CheckpointAttachmentBound reports an exact, durable attachment identity. + CheckpointAttachmentBound RunnerCheckpointKind = "attachment_bound" + // CheckpointCommandDelivered reports durable delivery for that identity. + CheckpointCommandDelivered RunnerCheckpointKind = "command_delivered" +) + +// RunnerCase identifies the state-induction case to which a raw checkpoint +// belongs. +type RunnerCase string + +const ( + // RunnerCaseBlocked names the unsupported admitted-then-blocked case. + RunnerCaseBlocked RunnerCase = "blocked" + // RunnerCaseExited names the exact-pane lifecycle case. + RunnerCaseExited RunnerCase = "exited" + // RunnerCaseTimeout names the suspended-process observation case. + RunnerCaseTimeout RunnerCase = "timeout" +) + +// RunnerCheckpoint is launcher-neutral evidence emitted by an authority or +// host observer. The source reports facts only; RunCommon owns their meaning +// and every resulting controller action. +type RunnerCheckpoint struct { + Kind RunnerCheckpointKind + Case RunnerCase + Target ProcessIdentity + MonotonicMS int64 +} + +// LauncherFunc runs one thin launcher and returns its uninterpreted capture. +// It must honor context cancellation. +type LauncherFunc func(context.Context) (DriverCapture, error) + +// CheckpointSource emits raw checkpoints until the semantic run is complete. +// It must honor context cancellation and must not close the supplied channel. +type CheckpointSource func(context.Context, chan<- RunnerCheckpoint) error + +// ControlAction identifies an action selected by common runner policy. +type ControlAction string + +const ( + // ControlSuspendExactProcess is verified exact-process suspension. + ControlSuspendExactProcess ControlAction = "suspend_exact_process" + // ControlCloseExactPane is exact-pane closure after durable delivery. + ControlCloseExactPane ControlAction = "close_exact_pane" +) + +// RecordedCheckpoint retains every raw checkpoint and any reason it was +// rejected. It is input for a later collector, not a suite verdict. +type RecordedCheckpoint struct { + Checkpoint RunnerCheckpoint + Error error +} + +// RecordedControl retains the exact request, result, and error for one common +// runner controller action. +type RecordedControl struct { + Action ControlAction + Case RunnerCase + Requested ProcessIdentity + Result ControlCheckpoint + Error error +} + +// RunRecord is the raw outcome of launcher orchestration. It deliberately +// does not contain or imply a conformance verdict. +type RunRecord struct { + Capture DriverCapture + LauncherError error + CheckpointSourceError error + RunnerError error + Checkpoints []RecordedCheckpoint + Controls []RecordedControl + CompleteRunTimedOut bool + CleanupError error +} + +// RunnerInput supplies only the launcher-neutral seams needed by the common +// runner. Fixture is already prepared, so cleanup is mandatory on every +// return from RunCommon. +type RunnerInput struct { + Fixture *Fixture + Launch LauncherFunc + Checkpoints CheckpointSource + Controller FaultController + InspectResources ResourceInspector + ExportEvidence EvidenceExporter +} + +type controllerCleaner interface { + Cleanup(context.Context) error +} + +type runnerConfig struct { + completeTimeout time.Duration +} + +// RunCommon runs the launcher and checkpoint observer concurrently under the +// canonical complete-run deadline, applies the common fault-induction order, +// and cleans the prepared fixture exactly once. Concrete authority polling, +// host inspection, and final result assembly are intentionally outside this +// boundary. +func RunCommon(ctx context.Context, input RunnerInput) (record RunRecord) { + return runCommon(ctx, input, runnerConfig{completeTimeout: completeRunTimeout}) +} + +func runCommon(ctx context.Context, input RunnerInput, config runnerConfig) (record RunRecord) { + if input.Fixture == nil { + record.RunnerError = fmt.Errorf("common runner requires a prepared fixture") + return record + } + defer func() { + cleanupContext, cancel := context.WithTimeout(context.WithoutCancel(ctx), cleanupTimeout) + defer cancel() + + var cleanupErrors []error + if cleaner, ok := input.Controller.(controllerCleaner); ok { + if err := cleaner.Cleanup(cleanupContext); err != nil { + cleanupErrors = append(cleanupErrors, fmt.Errorf("fault-controller cleanup failed: %w", err)) + } + } + if err := input.Fixture.Cleanup(cleanupContext, input.InspectResources, input.ExportEvidence); err != nil { + cleanupErrors = append(cleanupErrors, fmt.Errorf("fixture cleanup failed: %w", err)) + } + record.CleanupError = errors.Join(record.CleanupError, errors.Join(cleanupErrors...)) + }() + + if input.Launch == nil { + record.RunnerError = fmt.Errorf("common runner requires a launcher function") + return record + } + if input.Checkpoints == nil { + record.RunnerError = fmt.Errorf("common runner requires a checkpoint source") + return record + } + if input.Controller == nil { + record.RunnerError = fmt.Errorf("common runner requires a fault controller") + return record + } + if config.completeTimeout <= 0 { + record.RunnerError = fmt.Errorf("common runner requires a positive complete-run deadline") + return record + } + + completeContext, completeCancel := context.WithTimeout(ctx, config.completeTimeout) + defer completeCancel() + workContext, workCancel := context.WithCancel(completeContext) + defer workCancel() + + type launcherResult struct { + capture DriverCapture + err error + } + launcherDone := make(chan launcherResult, 1) + go func() { + capture, err := input.Launch(workContext) + launcherDone <- launcherResult{capture: capture, err: err} + }() + + checkpointEvents := make(chan RunnerCheckpoint) + checkpointSourceDone := make(chan error, 1) + go func() { + checkpointSourceDone <- input.Checkpoints(workContext, checkpointEvents) + }() + + state := runnerControlState{controller: input.Controller} + launcherPending, sourcePending := true, true + deadline := completeContext.Done() + for launcherPending || sourcePending { + select { + case checkpoint, open := <-checkpointEvents: + if !open { + checkpointEvents = nil + if record.RunnerError == nil { + record.RunnerError = fmt.Errorf("checkpoint source closed the runner-owned event channel") + workCancel() + } + continue + } + entry := RecordedCheckpoint{Checkpoint: checkpoint} + if record.RunnerError != nil { + entry.Error = fmt.Errorf("checkpoint received after runner failure") + } else if !launcherPending { + entry.Error = fmt.Errorf("checkpoint received after launcher completion") + } else { + entry.Error = state.accept(workContext, checkpoint, &record.Controls) + } + record.Checkpoints = append(record.Checkpoints, entry) + if entry.Error != nil && record.RunnerError == nil { + record.RunnerError = entry.Error + workCancel() + } + case result := <-launcherDone: + launcherPending = false + record.Capture = result.capture + record.LauncherError = result.err + if result.err != nil && record.RunnerError == nil { + record.RunnerError = fmt.Errorf("launcher failed: %w", result.err) + workCancel() + } + case err := <-checkpointSourceDone: + sourcePending = false + record.CheckpointSourceError = err + if err != nil && !errors.Is(err, context.Canceled) && !errors.Is(err, context.DeadlineExceeded) && !errors.Is(err, io.EOF) && record.RunnerError == nil { + record.RunnerError = fmt.Errorf("checkpoint source failed: %w", err) + workCancel() + } + case <-deadline: + deadline = nil + record.CompleteRunTimedOut = errors.Is(completeContext.Err(), context.DeadlineExceeded) + if record.RunnerError == nil { + record.RunnerError = fmt.Errorf("complete-run deadline exceeded: %w", completeContext.Err()) + } + workCancel() + } + } + if errors.Is(completeContext.Err(), context.DeadlineExceeded) { + record.CompleteRunTimedOut = true + if record.RunnerError == nil || errors.Is(record.LauncherError, context.DeadlineExceeded) { + record.RunnerError = fmt.Errorf("complete-run deadline exceeded: %w", completeContext.Err()) + } + } + + if record.RunnerError == nil { + if err := state.complete(); err != nil { + record.RunnerError = err + } + } + return record +} + +type runnerControlState struct { + controller FaultController + phase int + lastCheckpoint int64 + lastRaw RunnerCheckpoint + exitedTarget ProcessIdentity + timeoutTarget ProcessIdentity + lastControl ControlCheckpoint +} + +var orderedControlCheckpoints = [...]struct { + kind RunnerCheckpointKind + caseName RunnerCase +}{ + {kind: CheckpointAttachmentBound, caseName: RunnerCaseExited}, + {kind: CheckpointCommandDelivered, caseName: RunnerCaseExited}, + {kind: CheckpointAttachmentBound, caseName: RunnerCaseTimeout}, + {kind: CheckpointCommandDelivered, caseName: RunnerCaseTimeout}, +} + +func (s *runnerControlState) accept(ctx context.Context, checkpoint RunnerCheckpoint, controls *[]RecordedControl) error { + if checkpoint.Case == RunnerCaseBlocked { + return fmt.Errorf("prerequisite.blocked_induction_unavailable: common runner has no supported blocked control") + } + if !validProcessIdentity(checkpoint.Target) { + return fmt.Errorf("checkpoint has incomplete process identity") + } + if checkpoint.MonotonicMS < 0 || (s.phase > 0 && checkpoint.MonotonicMS < s.lastCheckpoint) { + return fmt.Errorf("checkpoint monotonic order regressed") + } + if s.phase > 0 && checkpoint.Kind == s.lastRaw.Kind && checkpoint.Case == s.lastRaw.Case && checkpoint.Target == s.lastRaw.Target { + return fmt.Errorf("duplicate checkpoint/control for %s/%s", checkpoint.Case, checkpoint.Kind) + } + if s.phase >= len(orderedControlCheckpoints) { + return fmt.Errorf("duplicate checkpoint/control after induction sequence completed") + } + want := orderedControlCheckpoints[s.phase] + if checkpoint.Kind != want.kind || checkpoint.Case != want.caseName { + return fmt.Errorf("wrong checkpoint case/order: got %s/%s, want %s/%s", checkpoint.Case, checkpoint.Kind, want.caseName, want.kind) + } + + switch s.phase { + case 0: + s.exitedTarget = checkpoint.Target + control, err := s.suspend(ctx, RunnerCaseExited, checkpoint, controls) + if err != nil { + return err + } + s.lastControl = control + case 1: + if checkpoint.Target != s.exitedTarget { + return fmt.Errorf("exited checkpoint process identity changed") + } + if checkpoint.MonotonicMS < s.lastControl.MonotonicMS { + return fmt.Errorf("exited delivery preceded verified suspension") + } + control, err := s.close(ctx, checkpoint, controls) + if err != nil { + return err + } + s.lastControl = control + case 2: + if checkpoint.Target == s.exitedTarget { + return fmt.Errorf("timeout case reused exited process identity") + } + s.timeoutTarget = checkpoint.Target + control, err := s.suspend(ctx, RunnerCaseTimeout, checkpoint, controls) + if err != nil { + return err + } + s.lastControl = control + case 3: + if checkpoint.Target != s.timeoutTarget { + return fmt.Errorf("timeout checkpoint process identity changed") + } + if checkpoint.MonotonicMS < s.lastControl.MonotonicMS { + return fmt.Errorf("timeout delivery preceded verified suspension") + } + } + + s.lastCheckpoint = checkpoint.MonotonicMS + s.lastRaw = checkpoint + s.phase++ + return nil +} + +func (s *runnerControlState) suspend(ctx context.Context, caseName RunnerCase, checkpoint RunnerCheckpoint, controls *[]RecordedControl) (ControlCheckpoint, error) { + result, err := s.controller.SuspendExactProcess(ctx, checkpoint.Target) + if err != nil { + err = fmt.Errorf("%s exact process suspension failed: %w", caseName, err) + } else if result.Target != checkpoint.Target { + err = fmt.Errorf("%s suspension controller changed process identity", caseName) + } else if !result.StoppedVerified { + err = fmt.Errorf("%s exact process suspension was not verified", caseName) + } else if result.MonotonicMS < checkpoint.MonotonicMS { + err = fmt.Errorf("%s suspension control predates its checkpoint", caseName) + } + *controls = append(*controls, RecordedControl{Action: ControlSuspendExactProcess, Case: caseName, Requested: checkpoint.Target, Result: result, Error: err}) + return result, err +} + +func (s *runnerControlState) close(ctx context.Context, checkpoint RunnerCheckpoint, controls *[]RecordedControl) (ControlCheckpoint, error) { + result, err := s.controller.CloseExactPane(ctx, checkpoint.Target) + if err != nil { + err = fmt.Errorf("exited exact pane close failed: %w", err) + } else if result.Target != checkpoint.Target { + err = fmt.Errorf("exited pane-close controller changed process identity") + } else if !result.StoppedVerified { + err = fmt.Errorf("exited pane close did not retain verified suspension") + } else if result.MonotonicMS < checkpoint.MonotonicMS { + err = fmt.Errorf("exited pane close preceded durable delivery") + } + *controls = append(*controls, RecordedControl{Action: ControlCloseExactPane, Case: RunnerCaseExited, Requested: checkpoint.Target, Result: result, Error: err}) + return result, err +} + +func (s *runnerControlState) complete() error { + if s.phase != len(orderedControlCheckpoints) { + return fmt.Errorf("controller checkpoint sequence ended incomplete after %d of %d checkpoints", s.phase, len(orderedControlCheckpoints)) + } + return nil +} + +func validProcessIdentity(identity ProcessIdentity) bool { + return identity.PID > 0 && identity.StartTime != "" && identity.TerminalID != "" && identity.AttachmentID != "" +} diff --git a/internal/conformance/portablelauncher/runner_cleanup_test.go b/internal/conformance/portablelauncher/runner_cleanup_test.go new file mode 100644 index 0000000..b52b41d --- /dev/null +++ b/internal/conformance/portablelauncher/runner_cleanup_test.go @@ -0,0 +1,198 @@ +package portablelauncher + +import ( + "context" + "errors" + "os" + "reflect" + "strings" + "sync" + "testing" + "time" +) + +type cleanupTestController struct { + *fakeFaultController + + mu sync.Mutex + cleanupCalls int + cleanupContext context.Context + cleanupErr error + onCleanup func(context.Context) +} + +func (c *cleanupTestController) Cleanup(ctx context.Context) error { + c.mu.Lock() + c.cleanupCalls++ + c.cleanupContext = ctx + onCleanup := c.onCleanup + err := c.cleanupErr + c.mu.Unlock() + + if onCleanup != nil { + onCleanup(ctx) + } + return err +} + +func (c *cleanupTestController) cleanupState() (int, context.Context) { + c.mu.Lock() + defer c.mu.Unlock() + return c.cleanupCalls, c.cleanupContext +} + +type cleanupEventLog struct { + mu sync.Mutex + events []string +} + +func (l *cleanupEventLog) append(event string) { + l.mu.Lock() + defer l.mu.Unlock() + l.events = append(l.events, event) +} + +func (l *cleanupEventLog) snapshot() []string { + l.mu.Lock() + defer l.mu.Unlock() + return append([]string(nil), l.events...) +} + +func TestRunCommonCleansControllerBeforeFixtureAndRetainsBothErrors(t *testing.T) { + controllerErr := errors.New("controller cleanup refused") + fixtureErr := errors.New("fixture resource remains") + events := &cleanupEventLog{} + rootPresentDuringControllerCleanup := false + controller := &cleanupTestController{ + fakeFaultController: &fakeFaultController{}, + cleanupErr: controllerErr, + } + input, _ := runnerTestInput(t, controller, happyRunnerCheckpoints()) + root := input.Fixture.Root + controller.onCleanup = func(context.Context) { + events.append("controller") + _, err := os.Stat(root) + rootPresentDuringControllerCleanup = err == nil + } + input.InspectResources = func(context.Context, *Fixture) error { + events.append("inspect") + return fixtureErr + } + input.ExportEvidence = func(context.Context, *Fixture) error { + events.append("export") + return nil + } + + record := runCommon(context.Background(), input, runnerConfig{completeTimeout: time.Second}) + + if record.RunnerError != nil { + t.Fatalf("runner error = %v, want nil", record.RunnerError) + } + if got, want := events.snapshot(), []string{"controller", "inspect", "export"}; !reflect.DeepEqual(got, want) { + t.Fatalf("cleanup order = %v, want %v", got, want) + } + if calls, _ := controller.cleanupState(); calls != 1 { + t.Fatalf("controller cleanup calls = %d, want 1", calls) + } + if !rootPresentDuringControllerCleanup { + t.Fatal("fixture root was absent before controller cleanup") + } + if _, err := os.Stat(root); !os.IsNotExist(err) { + t.Fatalf("fixture root still exists after cleanup: %v", err) + } + if record.CleanupError == nil { + t.Fatal("cleanup error = nil, want controller and fixture failures") + } + if !errors.Is(record.CleanupError, controllerErr) { + t.Fatalf("cleanup error does not retain controller failure: %v", record.CleanupError) + } + var fixtureProblems *Problems + if !errors.As(record.CleanupError, &fixtureProblems) || len(fixtureProblems.Items) != 1 || fixtureProblems.Items[0] != "cleanup resource inspection failed: fixture resource remains" { + t.Fatalf("cleanup error does not retain fixture failure: %v", record.CleanupError) + } + for _, want := range []string{ + "fault-controller cleanup failed: controller cleanup refused", + "fixture cleanup failed: cleanup resource inspection failed: fixture resource remains", + } { + if !strings.Contains(record.CleanupError.Error(), want) { + t.Fatalf("cleanup error = %v, want it to contain %q", record.CleanupError, want) + } + } +} + +func TestRunCommonCleanupContextOutlivesCanceledRunContext(t *testing.T) { + controller := &cleanupTestController{fakeFaultController: &fakeFaultController{}} + input, _ := runnerTestInput(t, controller, nil) + type contextObservation struct { + ctx context.Context + err error + deadline time.Time + hasDeadline bool + } + var cleanupContexts []contextObservation + observe := func(ctx context.Context) { + deadline, hasDeadline := ctx.Deadline() + cleanupContexts = append(cleanupContexts, contextObservation{ + ctx: ctx, err: ctx.Err(), deadline: deadline, hasDeadline: hasDeadline, + }) + } + controller.onCleanup = func(ctx context.Context) { + observe(ctx) + } + input.InspectResources = func(ctx context.Context, _ *Fixture) error { + observe(ctx) + return nil + } + input.ExportEvidence = func(ctx context.Context, _ *Fixture) error { + observe(ctx) + return nil + } + runContext, cancel := context.WithCancel(context.Background()) + cancel() + + record := runCommon(runContext, input, runnerConfig{completeTimeout: time.Second}) + + if record.CleanupError != nil { + t.Fatalf("cleanup error = %v, want nil", record.CleanupError) + } + if len(cleanupContexts) != 3 { + t.Fatalf("cleanup context observations = %d, want 3", len(cleanupContexts)) + } + for i, observation := range cleanupContexts { + if observation.err != nil { + t.Fatalf("cleanup context %d inherited run cancellation: %v", i, observation.err) + } + if !observation.hasDeadline || time.Until(observation.deadline) <= 0 || time.Until(observation.deadline) > cleanupTimeout { + t.Fatalf("cleanup context %d deadline = %v/%v, want active cleanup deadline", i, observation.deadline, observation.hasDeadline) + } + if observation.ctx != cleanupContexts[0].ctx { + t.Fatalf("cleanup context %d differs from controller cleanup context", i) + } + } + if calls, gotContext := controller.cleanupState(); calls != 1 || gotContext != cleanupContexts[0].ctx { + t.Fatalf("controller cleanup state = calls:%d context:%v, want one call with shared context", calls, gotContext) + } +} + +func TestRunCommonNilFixtureReturnsRunnerErrorWithoutCleanup(t *testing.T) { + record := RunCommon(context.Background(), RunnerInput{}) + + if record.RunnerError == nil || record.RunnerError.Error() != "common runner requires a prepared fixture" { + t.Fatalf("runner error = %v, want missing prepared fixture", record.RunnerError) + } + if record.CleanupError != nil { + t.Fatalf("cleanup error = %v, want nil for an unprepared fixture", record.CleanupError) + } +} + +func TestRunCommonControllerWithoutCleanupPreservesFixtureCleanup(t *testing.T) { + controller := &fakeFaultController{} + input, cleanup := runnerTestInput(t, controller, happyRunnerCheckpoints()) + + record := runCommon(context.Background(), input, runnerConfig{completeTimeout: time.Second}) + + cleanup.assertOnce(t) + if record.RunnerError != nil || record.CleanupError != nil { + t.Fatalf("errors = runner:%v cleanup:%v, want nil", record.RunnerError, record.CleanupError) + } +} diff --git a/internal/conformance/portablelauncher/runner_test.go b/internal/conformance/portablelauncher/runner_test.go new file mode 100644 index 0000000..11805ed --- /dev/null +++ b/internal/conformance/portablelauncher/runner_test.go @@ -0,0 +1,318 @@ +package portablelauncher + +import ( + "context" + "errors" + "os" + "path/filepath" + "reflect" + "strings" + "sync" + "testing" + "time" +) + +var ( + exitedIdentity = ProcessIdentity{PID: 101, StartTime: "birth-exited", TerminalID: "terminal-exited", AttachmentID: "attachment-exited"} + timeoutIdentity = ProcessIdentity{PID: 202, StartTime: "birth-timeout", TerminalID: "terminal-timeout", AttachmentID: "attachment-timeout"} +) + +type fakeFaultController struct { + mu sync.Mutex + actions []ControlAction + responses []ControlCheckpoint + errors []error +} + +func (f *fakeFaultController) SuspendExactProcess(_ context.Context, target ProcessIdentity) (ControlCheckpoint, error) { + return f.control(ControlSuspendExactProcess, target) +} + +func (f *fakeFaultController) CloseExactPane(_ context.Context, target ProcessIdentity) (ControlCheckpoint, error) { + return f.control(ControlCloseExactPane, target) +} + +func (f *fakeFaultController) control(action ControlAction, target ProcessIdentity) (ControlCheckpoint, error) { + f.mu.Lock() + defer f.mu.Unlock() + index := len(f.actions) + f.actions = append(f.actions, action) + if index < len(f.responses) { + var err error + if index < len(f.errors) { + err = f.errors[index] + } + return f.responses[index], err + } + monotonic := []int64{15, 25, 35} + return ControlCheckpoint{Target: target, StoppedVerified: true, MonotonicMS: monotonic[index]}, nil +} + +func (f *fakeFaultController) recordedActions() []ControlAction { + f.mu.Lock() + defer f.mu.Unlock() + return append([]ControlAction(nil), f.actions...) +} + +func TestRunCommonOrdersControlsAndRunsLauncherAsynchronously(t *testing.T) { + controller := &fakeFaultController{} + checkpoints := happyRunnerCheckpoints() + input, cleanup := runnerTestInput(t, controller, checkpoints) + record := runCommon(context.Background(), input, runnerConfig{completeTimeout: time.Second}) + cleanup.assertOnce(t) + + if record.RunnerError != nil || record.LauncherError != nil || record.CheckpointSourceError != nil || record.CleanupError != nil { + t.Fatalf("unexpected errors: runner=%v launcher=%v source=%v cleanup=%v", record.RunnerError, record.LauncherError, record.CheckpointSourceError, record.CleanupError) + } + wantActions := []ControlAction{ControlSuspendExactProcess, ControlCloseExactPane, ControlSuspendExactProcess} + if got := controller.recordedActions(); !reflect.DeepEqual(got, wantActions) { + t.Fatalf("controller actions = %v, want %v", got, wantActions) + } + if len(record.Checkpoints) != 4 || len(record.Controls) != 3 { + t.Fatalf("recorded checkpoints/controls = %d/%d, want 4/3", len(record.Checkpoints), len(record.Controls)) + } + if string(record.Capture.Events) != "raw event\n" { + t.Fatalf("launcher capture was changed: %+v", record.Capture) + } + for i, control := range record.Controls { + if control.Error != nil || control.Requested != control.Result.Target { + t.Fatalf("control %d did not retain an exact successful result: %+v", i, control) + } + } +} + +func TestRunCommonFailsClosedOnCheckpointAndControllerDrift(t *testing.T) { + changedIdentity := exitedIdentity + changedIdentity.StartTime = "different-birth" + tests := []struct { + name string + checkpoints []RunnerCheckpoint + controller *fakeFaultController + wantError string + wantActions []ControlAction + }{ + { + name: "delivery before suspension", + checkpoints: []RunnerCheckpoint{ + {Kind: CheckpointCommandDelivered, Case: RunnerCaseExited, Target: exitedIdentity, MonotonicMS: 10}, + }, + controller: &fakeFaultController{}, wantError: "wrong checkpoint case/order", + }, + { + name: "delivery monotonic time before suspension", + checkpoints: []RunnerCheckpoint{ + {Kind: CheckpointAttachmentBound, Case: RunnerCaseExited, Target: exitedIdentity, MonotonicMS: 10}, + {Kind: CheckpointCommandDelivered, Case: RunnerCaseExited, Target: exitedIdentity, MonotonicMS: 11}, + }, + controller: &fakeFaultController{}, wantError: "delivery preceded verified suspension", + wantActions: []ControlAction{ControlSuspendExactProcess}, + }, + { + name: "identity changes", + checkpoints: []RunnerCheckpoint{ + {Kind: CheckpointAttachmentBound, Case: RunnerCaseExited, Target: exitedIdentity, MonotonicMS: 10}, + {Kind: CheckpointCommandDelivered, Case: RunnerCaseExited, Target: changedIdentity, MonotonicMS: 20}, + }, + controller: &fakeFaultController{}, wantError: "process identity changed", + wantActions: []ControlAction{ControlSuspendExactProcess}, + }, + { + name: "duplicate checkpoint and control", + checkpoints: []RunnerCheckpoint{ + {Kind: CheckpointAttachmentBound, Case: RunnerCaseExited, Target: exitedIdentity, MonotonicMS: 10}, + {Kind: CheckpointAttachmentBound, Case: RunnerCaseExited, Target: exitedIdentity, MonotonicMS: 20}, + }, + controller: &fakeFaultController{}, wantError: "duplicate checkpoint/control", + wantActions: []ControlAction{ControlSuspendExactProcess}, + }, + { + name: "wrong case order", + checkpoints: []RunnerCheckpoint{ + {Kind: CheckpointAttachmentBound, Case: RunnerCaseTimeout, Target: timeoutIdentity, MonotonicMS: 10}, + }, + controller: &fakeFaultController{}, wantError: "wrong checkpoint case/order", + }, + { + name: "blocked remains unsupported", + checkpoints: []RunnerCheckpoint{ + {Kind: CheckpointAttachmentBound, Case: RunnerCaseBlocked, Target: exitedIdentity, MonotonicMS: 10}, + }, + controller: &fakeFaultController{}, wantError: "prerequisite.blocked_induction_unavailable", + }, + { + name: "controller does not verify suspension", + checkpoints: []RunnerCheckpoint{ + {Kind: CheckpointAttachmentBound, Case: RunnerCaseExited, Target: exitedIdentity, MonotonicMS: 10}, + }, + controller: &fakeFaultController{responses: []ControlCheckpoint{{Target: exitedIdentity, MonotonicMS: 15}}}, + wantError: "suspension was not verified", wantActions: []ControlAction{ControlSuspendExactProcess}, + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + input, cleanup := runnerTestInput(t, tc.controller, tc.checkpoints) + record := runCommon(context.Background(), input, runnerConfig{completeTimeout: time.Second}) + cleanup.assertOnce(t) + if record.RunnerError == nil || !strings.Contains(record.RunnerError.Error(), tc.wantError) { + t.Fatalf("runner error = %v, want %q", record.RunnerError, tc.wantError) + } + if got := tc.controller.recordedActions(); !reflect.DeepEqual(got, tc.wantActions) { + t.Fatalf("controller actions = %v, want %v", got, tc.wantActions) + } + if len(record.Checkpoints) == 0 || record.Checkpoints[len(record.Checkpoints)-1].Error == nil { + t.Fatal("rejected raw checkpoint and error were not retained") + } + }) + } +} + +func TestRunCommonPreservesFailedLauncherCapture(t *testing.T) { + controller := &fakeFaultController{} + input, cleanup := runnerTestInput(t, controller, nil) + wantError := errors.New("launcher exited 7") + input.Launch = func(context.Context) (DriverCapture, error) { + return DriverCapture{ + Events: []byte("partial event\n"), Stderr: []byte("raw stderr\n"), + }, wantError + } + record := runCommon(context.Background(), input, runnerConfig{completeTimeout: time.Second}) + cleanup.assertOnce(t) + + if !errors.Is(record.LauncherError, wantError) || record.RunnerError == nil || !strings.Contains(record.RunnerError.Error(), "launcher failed") { + t.Fatalf("launcher failure was not retained: launcher=%v runner=%v", record.LauncherError, record.RunnerError) + } + if string(record.Capture.Events) != "partial event\n" || string(record.Capture.Stderr) != "raw stderr\n" { + t.Fatalf("failed launcher capture was repaired or lost: %+v", record.Capture) + } +} + +func TestRunCommonCompleteTimeoutStillCleansExactlyOnce(t *testing.T) { + controller := &fakeFaultController{} + input, cleanup := runnerTestInput(t, controller, nil) + input.Launch = func(ctx context.Context) (DriverCapture, error) { + <-ctx.Done() + return DriverCapture{Events: []byte("before timeout\n")}, ctx.Err() + } + input.Checkpoints = func(ctx context.Context, _ chan<- RunnerCheckpoint) error { + <-ctx.Done() + return ctx.Err() + } + record := runCommon(context.Background(), input, runnerConfig{completeTimeout: 10 * time.Millisecond}) + cleanup.assertOnce(t) + + if !record.CompleteRunTimedOut || record.RunnerError == nil || !strings.Contains(record.RunnerError.Error(), "complete-run deadline exceeded") { + t.Fatalf("timeout record = timedOut:%v runner:%v", record.CompleteRunTimedOut, record.RunnerError) + } + if string(record.Capture.Events) != "before timeout\n" || !errors.Is(record.LauncherError, context.DeadlineExceeded) { + t.Fatalf("timeout capture/error not preserved: capture=%+v error=%v", record.Capture, record.LauncherError) + } +} + +func TestRunCommonRecordsControllerFailureAndCleanupFailure(t *testing.T) { + controllerError := errors.New("signal refused") + controller := &fakeFaultController{ + responses: []ControlCheckpoint{{Target: exitedIdentity, MonotonicMS: 15}}, + errors: []error{controllerError}, + } + input, cleanup := runnerTestInput(t, controller, []RunnerCheckpoint{ + {Kind: CheckpointAttachmentBound, Case: RunnerCaseExited, Target: exitedIdentity, MonotonicMS: 10}, + }) + cleanup.inspectError = errors.New("pane remains") + record := runCommon(context.Background(), input, runnerConfig{completeTimeout: time.Second}) + cleanup.assertOnce(t) + + if record.RunnerError == nil || !errors.Is(record.Controls[0].Error, controllerError) { + t.Fatalf("controller failure not retained: runner=%v controls=%+v", record.RunnerError, record.Controls) + } + if record.CleanupError == nil || !strings.Contains(record.CleanupError.Error(), "pane remains") { + t.Fatalf("cleanup failure was hidden: %v", record.CleanupError) + } +} + +func happyRunnerCheckpoints() []RunnerCheckpoint { + return []RunnerCheckpoint{ + {Kind: CheckpointAttachmentBound, Case: RunnerCaseExited, Target: exitedIdentity, MonotonicMS: 10}, + {Kind: CheckpointCommandDelivered, Case: RunnerCaseExited, Target: exitedIdentity, MonotonicMS: 20}, + {Kind: CheckpointAttachmentBound, Case: RunnerCaseTimeout, Target: timeoutIdentity, MonotonicMS: 30}, + {Kind: CheckpointCommandDelivered, Case: RunnerCaseTimeout, Target: timeoutIdentity, MonotonicMS: 40}, + } +} + +type cleanupSpy struct { + mu sync.Mutex + inspectCalls int + exportCalls int + deadlineSeen bool + inspectError error +} + +func (s *cleanupSpy) inspect(ctx context.Context, _ *Fixture) error { + s.mu.Lock() + defer s.mu.Unlock() + s.inspectCalls++ + deadline, ok := ctx.Deadline() + s.deadlineSeen = ok && time.Until(deadline) > 29*time.Second && time.Until(deadline) <= cleanupTimeout + return s.inspectError +} + +func (s *cleanupSpy) export(context.Context, *Fixture) error { + s.mu.Lock() + defer s.mu.Unlock() + s.exportCalls++ + return nil +} + +func (s *cleanupSpy) assertOnce(t *testing.T) { + t.Helper() + s.mu.Lock() + defer s.mu.Unlock() + if s.inspectCalls != 1 || s.exportCalls != 1 || !s.deadlineSeen { + t.Fatalf("cleanup calls inspect/export/deadline = %d/%d/%v, want 1/1/true", s.inspectCalls, s.exportCalls, s.deadlineSeen) + } +} + +func runnerTestInput(t *testing.T, controller FaultController, checkpoints []RunnerCheckpoint) (RunnerInput, *cleanupSpy) { + t.Helper() + root, err := os.MkdirTemp(t.TempDir(), "duo-portable-launcher-") + if err != nil { + t.Fatal(err) + } + if err := os.Mkdir(filepath.Join(root, "capture"), 0o700); err != nil { + t.Fatal(err) + } + + started := make(chan struct{}) + sourceFinished := make(chan struct{}) + cleanup := &cleanupSpy{} + input := RunnerInput{ + Fixture: &Fixture{Root: root, Capture: filepath.Join(root, "capture")}, + Launch: func(ctx context.Context) (DriverCapture, error) { + close(started) + select { + case <-sourceFinished: + return DriverCapture{Events: []byte("raw event\n")}, nil + case <-ctx.Done(): + return DriverCapture{}, ctx.Err() + } + }, + Checkpoints: func(ctx context.Context, output chan<- RunnerCheckpoint) error { + defer close(sourceFinished) + select { + case <-started: + case <-ctx.Done(): + return ctx.Err() + } + for _, checkpoint := range checkpoints { + select { + case output <- checkpoint: + case <-ctx.Done(): + return ctx.Err() + } + } + return nil + }, + Controller: controller, InspectResources: cleanup.inspect, ExportEvidence: cleanup.export, + } + return input, cleanup +} diff --git a/internal/conformance/portablelauncher/scenario.go b/internal/conformance/portablelauncher/scenario.go new file mode 100644 index 0000000..7e2b19a --- /dev/null +++ b/internal/conformance/portablelauncher/scenario.go @@ -0,0 +1,366 @@ +package portablelauncher + +import ( + "crypto/sha256" + "encoding/hex" + "encoding/json" + "fmt" + "slices" + "time" + + "github.com/procrastivity/duo/internal/registry" +) + +// Canonical request, prompt, conflict, and outer-task payloads. +var ( + RequestBytes = []byte("fixture_id=portable-launcher-v1\nexpected_reply=DUO_PORTABLE_LAUNCHER_OK_V1\n") + PromptBytes = []byte("Read fixture/request.txt and reply with exactly the value of expected_reply, with no surrounding text.") + ConflictText = []byte("This text must conflict and must never be delivered.") + + // CanonicalTaskBytes are submitted byte-for-byte to every outer launcher. + // Expectations remain in the signed scenario and oracle, not in this task. + CanonicalTaskBytes = []byte("Load the shared duo-delegation-loop skill and execute the canonical scenario manifest at $DUO_CONFORMANCE_SCENARIO_PATH in order.\n") +) + +// Scenario is the launcher-neutral ordered conformance contract. +type Scenario struct { + Schema string `json:"schema"` + Name string `json:"name"` + Revision int `json:"revision"` + TaskDigest string `json:"task_digest"` + Fixture ScenarioFixture `json:"fixture"` + Prerequisites []Prerequisite `json:"prerequisites"` + Launchers map[string]AcceptedLauncher `json:"launchers"` + DeadlinesMS map[string]int64 `json:"deadlines_ms"` + Steps []StepSpec `json:"steps"` +} + +// ScenarioFixture describes the deterministic workspace and runtime inputs. +type ScenarioFixture struct { + Workspace string `json:"workspace"` + ConfigPath string `json:"config_path"` + Host string `json:"host"` + Preset string `json:"preset"` + Runtime string `json:"runtime"` + Provider string `json:"provider"` + ModelLine string `json:"model_line"` + RequestPath string `json:"request_path"` + Prompt string `json:"prompt"` + ExpectedReply string `json:"expected_reply"` + ConflictText string `json:"conflict_text"` + RequestDigest string `json:"request_digest"` + ReplyDigest string `json:"reply_digest"` + PromptDigest string `json:"prompt_digest"` + ConflictDigest string `json:"conflict_digest"` + IdempotencyKey string `json:"idempotency_key"` +} + +// Prerequisite identifies one required capability and when it must be checked. +type Prerequisite struct { + Name string `json:"name"` + Status string `json:"status"` + PreSetup bool `json:"pre_setup"` +} + +// StepSpec defines one ordered stage and its trusted expectations. +type StepSpec struct { + Sequence int `json:"sequence"` + Stage string `json:"stage"` + Case string `json:"case"` + Outcome string `json:"expected_outcome"` + DeadlineMS int64 `json:"deadline_ms"` + Action ActionSpec `json:"action"` + Assertions []ExpectedAssertion `json:"assertions"` +} + +// ActionSpec is the launcher-neutral instruction for one canonical stage. +// Operation is either a registered Duo operation or a common-suite action. +// Arguments are executable argv templates; values beginning with '$' are +// bound from prior canonical outputs. MaximumInvocations zero means bounded +// by the stage deadline rather than by count. Drivers never construct these +// values. +type ActionSpec struct { + Executor string `json:"executor"` + Operation string `json:"operation"` + Arguments []string `json:"arguments"` + AuxiliaryOperations []string `json:"auxiliary_operations"` + MinimumInvocations int `json:"minimum_invocations"` + MaximumInvocations int `json:"maximum_invocations"` + Instruction string `json:"instruction"` +} + +// ExpectedAssertion is an assertion ID and its canonical expected value. +type ExpectedAssertion struct { + ID string `json:"id"` + Expected map[string]any `json:"expected"` +} + +var caseMatrix = []struct { + Case string + Stages []string +}{ + {Case: "run", Stages: []string{"setup", "discovery", "preflight"}}, + {Case: "happy", Stages: []string{"launch", "bind", "send", "observe", "command_inspection"}}, + {Case: "restart", Stages: []string{"restart", "bind", "observe", "command_inspection"}}, + {Case: "same_key_same_text", Stages: []string{"send", "command_inspection"}}, + {Case: "same_key_different_text", Stages: []string{"send", "command_inspection"}}, + {Case: "blocked", Stages: []string{"launch", "bind", "send", "observe", "command_inspection"}}, + {Case: "exited", Stages: []string{"launch", "bind", "send", "observe", "command_inspection"}}, + {Case: "timeout", Stages: []string{"launch", "bind", "send", "observe", "command_inspection"}}, + {Case: "run", Stages: []string{"cleanup"}}, +} + +var deadlineByStage = map[string]int64{ + "setup": 30_000, "discovery": 30_000, "preflight": 15_000, + "launch": 30_000, "bind": 12_000, "send": 30_000, + "observe": 20_000, "command_inspection": 15_000, "restart": 15_000, + "cleanup": 30_000, +} + +// CanonicalScenario returns the complete portable-launcher scenario. +func CanonicalScenario() Scenario { + s := Scenario{ + Schema: ScenarioSchema, + Name: ScenarioName, + Revision: 1, + TaskDigest: Digest(CanonicalTaskBytes), + Fixture: ScenarioFixture{ + Workspace: "$WORKSPACE", ConfigPath: "$RUN/xdg/config/duo/duo.config.yaml", + Host: "herdr:$RUN/herdr/herdr.sock", Preset: "builder", Runtime: "pi", + Provider: "openai-codex", ModelLine: "gpt-5.6-luna", + RequestPath: "$WORKSPACE/fixture/request.txt", Prompt: string(PromptBytes), + ExpectedReply: "DUO_PORTABLE_LAUNCHER_OK_V1", ConflictText: string(ConflictText), + RequestDigest: RequestDigest, ReplyDigest: ReplyDigest, + PromptDigest: PromptDigest, ConflictDigest: ConflictDigest, + IdempotencyKey: "portable-launcher-v1-primary", + }, + Prerequisites: []Prerequisite{ + {Name: "pinned_launcher_binary", Status: "required", PreSetup: true}, + {Name: "pinned_duo_build", Status: "required", PreSetup: true}, + {Name: "pinned_pi_0_83_0", Status: "required", PreSetup: true}, + {Name: "pinned_herdr_0_8_2_protocol_20", Status: "required", PreSetup: true}, + {Name: "isolated_inner_provider_credentials", Status: "required", PreSetup: true}, + {Name: "timeout_induction_socket_probe", Status: "required", PreSetup: false}, + {Name: "supported_admitted_then_blocked_producer", Status: "unavailable", PreSetup: false}, + }, + Launchers: cloneLauncherPins(), + DeadlinesMS: cloneDeadlines(), + } + sequence := 0 + for _, group := range caseMatrix { + for _, stage := range group.Stages { + sequence++ + outcome := "success" + if stage == "observe" { + switch group.Case { + case "blocked", "exited", "timeout": + outcome = group.Case + } + } + s.Steps = append(s.Steps, StepSpec{ + Sequence: sequence, Stage: stage, Case: group.Case, Outcome: outcome, + DeadlineMS: deadlineByStage[stage], Action: canonicalAction(stage, group.Case), + Assertions: expectedAssertions(stage, group.Case), + }) + } + } + return s +} + +func canonicalAction(stage, caseName string) ActionSpec { + jsonOutput := []string{"--output", "json"} + duo := func(operation, instruction string, arguments ...string) ActionSpec { + return ActionSpec{Executor: "duo", Operation: operation, Arguments: append(arguments, jsonOutput...), AuxiliaryOperations: []string{}, MinimumInvocations: 1, MaximumInvocations: 1, Instruction: instruction} + } + controller := func(operation, instruction string) ActionSpec { + return ActionSpec{Executor: "common_controller", Operation: operation, Arguments: []string{}, AuxiliaryOperations: []string{}, MinimumInvocations: 1, MaximumInvocations: 1, Instruction: instruction} + } + + switch { + case stage == "setup": + return controller("fixture.prepare", "Use the prepared isolated fixture and its exact per-run pins; do not use shared user state.") + case stage == "discovery": + return ActionSpec{Executor: "launcher", Operation: "skill.discover", Arguments: []string{SkillName}, AuxiliaryOperations: []string{}, MinimumInvocations: 1, MaximumInvocations: 1, Instruction: "Load the installed project skill and emit the launcher discovery event for its exact identity before invoking Duo."} + case stage == "preflight": + return duo(registeredOperationName("doctor"), "Run the read-only launcher preflight once and continue only when its seven required checks report ready.", "doctor", "--workspace", "$WORKSPACE") + case stage == "launch": + if caseName == "blocked" { + return controller("prerequisite.require_admitted_then_blocked", "Record prerequisite.blocked_induction_unavailable and do not launch or fabricate the admitted-then-blocked case while the canonical prerequisite is unavailable.") + } + return duo(registeredOperationName("session", "launch"), "Launch one fresh canonical Pi session without a pre-launch prompt and retain its session identifier.", "session", "launch", "builder", "--require", "agent_runtime=pi", "--workspace", "$WORKSPACE", "--host", "herdr:$RUN/herdr/herdr.sock") + case stage == "bind": + instruction := "Inspect the just-launched session until the one claimed Pi attachment and durable runtime identity are present; each poll is a separate command." + if caseName == "exited" || caseName == "timeout" { + instruction += " Emit the exact attachment-bound checkpoint so the common controller verifies and suspends that process before send." + } + action := duo(registeredOperationName("session", "show"), instruction, "session", "show", "$CASE_SESSION_ID") + action.MaximumInvocations = 0 + return action + case stage == "send" && caseName == "same_key_same_text": + return duo(registeredOperationName("prompt", "send"), "Resend the canonical text with the primary idempotency key and retain the original command identity.", "prompt", "send", "$HAPPY_SESSION_ID", "--text", "$CANONICAL_PROMPT", "--idempotency-key", "$PRIMARY_KEY", "--expires-at", "$EXPIRES_AT") + case stage == "send" && caseName == "same_key_different_text": + return duo(registeredOperationName("prompt", "send"), "Send the conflict text with the primary idempotency key and retain the schema-valid conflict envelope; do not retry it.", "prompt", "send", "$HAPPY_SESSION_ID", "--text", "$CONFLICT_TEXT", "--idempotency-key", "$PRIMARY_KEY", "--expires-at", "$EXPIRES_AT") + case stage == "send": + instruction := "Deliver the canonical prompt exactly once to this case's bound session with the primary idempotency key and finite expiry." + if caseName == "exited" || caseName == "timeout" { + instruction += " Emit the durable-delivery checkpoint for common controller verification." + } + return duo(registeredOperationName("prompt", "send"), instruction, "prompt", "send", "$CASE_SESSION_ID", "--text", "$CANONICAL_PROMPT", "--idempotency-key", "$PRIMARY_KEY", "--expires-at", "$EXPIRES_AT") + case stage == "observe" && caseName == "exited": + return duo(registeredOperationName("session", "reconcile"), "After the common controller closes the exact suspended pane, reconcile this session once and retain the exited observation.", "session", "reconcile", "$CASE_SESSION_ID") + case stage == "observe": + action := duo(registeredOperationName("conversation", "list"), "Poll session show and conversation list as distinct commands at approximately one-second intervals; stop on the declared outcome or the stage deadline and retain the final semantic conversation page.", "conversation", "list", "$CASE_SESSION_ID") + action.AuxiliaryOperations = []string{registeredOperationName("session", "show")} + action.MaximumInvocations = 0 + return action + case stage == "command_inspection": + return duo(registeredOperationName("prompt", "show"), "Inspect the case's original prompt command and retain its durable state and attempt list.", "prompt", "show", "$CASE_COMMAND_ID") + case stage == "restart": + return duo(registeredOperationName("session", "reconcile"), "Open the same authority store in a new Duo process, reconcile the still-live happy session, and retain same-live continuity.", "session", "reconcile", "$HAPPY_SESSION_ID") + case stage == "cleanup": + return controller("fixture.cleanup", "Run common controller cleanup, independently inspect/export the fixture, and remove only the run root; cleanup is always last.") + default: + panic(fmt.Sprintf("portable launcher scenario: no action for %s/%s", caseName, stage)) + } +} + +func registeredOperationName(cli ...string) string { + for _, descriptor := range registry.All() { + if slices.Equal(descriptor.CLI, cli) { + return descriptor.Name + } + } + panic(fmt.Sprintf("portable launcher scenario: no registered operation for CLI path %v", cli)) +} + +func cloneLauncherPins() map[string]AcceptedLauncher { + out := make(map[string]AcceptedLauncher, len(acceptedLaunchers)) + for name, pin := range acceptedLaunchers { + out[name] = pin + } + return out +} + +func cloneDeadlines() map[string]int64 { + out := make(map[string]int64, len(deadlineByStage)+1) + for k, v := range deadlineByStage { + out[k] = v + } + out["complete_run"] = int64((10 * time.Minute) / time.Millisecond) + return out +} + +func expectedAssertions(stage, caseName string) []ExpectedAssertion { + timing := ExpectedAssertion{ID: "stage.completed_within_deadline", Expected: map[string]any{"within_deadline": true}} + semantic := ExpectedAssertion{ID: stageAssertionID(stage, caseName), Expected: assertionExpectation(stage, caseName)} + return []ExpectedAssertion{timing, semantic} +} + +func stageAssertionID(stage, caseName string) string { + switch { + case stage == "setup": + return "setup.fixture_isolated_and_pinned" + case stage == "discovery": + return "discovery.skill_identity" + case stage == "preflight": + return "preflight.ready" + case stage == "launch": + return "launch.resolution" + case stage == "bind": + return "bind.runtime_identity" + case stage == "restart": + return "restart.authority_continuity" + case stage == "cleanup": + return "cleanup.complete" + case stage == "observe" && caseName == "happy": + return "observe.assistant_text" + case stage == "observe" && caseName == "restart": + return "observe.restart_text" + case stage == "observe": + return "observe." + caseName + case stage == "send" && caseName == "same_key_same_text": + return "send.idempotent_replay" + case stage == "send" && caseName == "same_key_different_text": + return "send.idempotency_conflict" + case stage == "send": + return "send.delivery" + case stage == "command_inspection" && caseName == "same_key_same_text": + return "command.idempotent_no_duplication" + case stage == "command_inspection" && caseName == "same_key_different_text": + return "command.idempotency_no_drift" + default: + return "command.delivery_integrity" + } +} + +func assertionExpectation(stage, caseName string) map[string]any { + switch stageAssertionID(stage, caseName) { + case "setup.fixture_isolated_and_pinned": + return map[string]any{"fixture_root": "$RUN", "shared_state": false, "plugins_enabled": false, "mcp_enabled": false, "terminal_input_used": false} + case "discovery.skill_identity": + return map[string]any{"name": SkillName, "format_version": SkillFormat, "content_digest": SkillContentDigest} + case "preflight.ready": + return map[string]any{"status": "ready", "required_checks": float64(7), "all_required_pass": true} + case "launch.resolution": + return map[string]any{"runtime": "pi", "host": "herdr", "selected_leaves": float64(1), "prelaunch_prompt": false} + case "bind.runtime_identity": + return map[string]any{"instances": float64(1), "correlations": float64(1), "attachment_claimed": true} + case "send.delivery": + return map[string]any{ + "state": "delivered", "queue_policy": "queue_until_safe", "attempts": float64(1), + "realization": "native", "recorded_result": "delivered", "activity_observed": false, + "acknowledged": false, "retry_safe": false, "retry_action": "observe_existing_command", + } + case "observe.assistant_text": + return map[string]any{"assistant_blocks": float64(1), "text_digest": ReplyDigest, "source_complete": true} + case "restart.authority_continuity": + return map[string]any{"outcome": "same_live", "distinct_processes": true, "same_store": true, "attempts": float64(1)} + case "observe.restart_text": + return map[string]any{"text_digest": ReplyDigest, "same_record": true} + case "send.idempotent_replay": + return map[string]any{"same_command": true, "attempts": float64(1), "new_turns": float64(0)} + case "command.idempotent_no_duplication": + return map[string]any{"commands_delta": float64(0), "attempts_delta": float64(0), "turns_delta": float64(0)} + case "send.idempotency_conflict": + return map[string]any{ + "code": "command.idempotency_conflict", "effect": "no_effect", "retry_safe": false, + "retry_action": "use_new_idempotency_key", "idempotency_key": "portable-launcher-v1-primary", + "target_original": true, "existing_digest": PromptDigest, "request_digest": ConflictDigest, + } + case "command.idempotency_no_drift": + return map[string]any{"commands_delta": float64(0), "attempts_delta": float64(0), "queue_delta": float64(0), "turns_delta": float64(0)} + case "observe.blocked": + return map[string]any{"condition": "blocked", "nonterminal": true, "attempts": float64(1), "assistant_blocks": float64(0)} + case "observe.exited": + return map[string]any{"condition": "exited", "same_instance": true, "process_success": true, "attempts": float64(1)} + case "observe.timeout": + return map[string]any{ + "elapsed_min_ms": float64(20_000), "elapsed_max_ms": float64(22_500), "elapsed_ms": float64(20_000), + "process_success": true, "outer_timed_out": false, "stopped_verified": true, + "delivered": true, "attempts": float64(1), "assistant_blocks": float64(0), + } + case "cleanup.complete": + return map[string]any{"pids_remaining": float64(0), "sockets_remaining": float64(0), "panes_remaining": float64(0), "root_removed": true, "export_complete": true} + default: + return map[string]any{ + "state": "delivered", "queue_policy": "queue_until_safe", "attempts": float64(1), + "realization": "native", "recorded_result": "delivered", "retry_safe": false, + "retry_action": "observe_existing_command", "unchanged": true, + } + } +} + +// ScenarioJSON encodes a scenario using the canonical fixture formatting. +func ScenarioJSON(s Scenario) ([]byte, error) { + b, err := json.MarshalIndent(s, "", " ") + if err != nil { + return nil, fmt.Errorf("marshal scenario: %w", err) + } + return append(b, '\n'), nil +} + +// Digest returns a sha256-prefixed lowercase digest of data. +func Digest(data []byte) string { + sum := sha256.Sum256(data) + return "sha256:" + hex.EncodeToString(sum[:]) +} diff --git a/internal/conformance/portablelauncher/validate.go b/internal/conformance/portablelauncher/validate.go new file mode 100644 index 0000000..46e3c88 --- /dev/null +++ b/internal/conformance/portablelauncher/validate.go @@ -0,0 +1,425 @@ +package portablelauncher + +import ( + "bytes" + "encoding/json" + "errors" + "fmt" + "io" + "regexp" + "sort" + "strings" + "time" +) + +var ( + digestPattern = regexp.MustCompile(`^sha256:[0-9a-f]{64}$`) + blobPattern = regexp.MustCompile(`^blob:sha256:[0-9a-f]{64}$`) + operationPattern = regexp.MustCompile(`^[a-z][a-z0-9_]*(\.[a-z][a-z0-9_]*)+$`) +) + +// Problems accumulates deterministic validation failures. +type Problems struct{ Items []string } + +// Add records one validation failure. +func (p *Problems) Add(problem string) { p.Items = append(p.Items, problem) } + +// Err sorts the recorded failures and returns nil when none exist. +func (p *Problems) Err() error { + if len(p.Items) == 0 { + return nil + } + sort.Strings(p.Items) + return p +} +func (p *Problems) Error() string { return strings.Join(p.Items, "\n") } + +// BlobReader resolves one content-addressed evidence reference. +type BlobReader func(reference string) ([]byte, error) + +// ValidationInput contains a result, evidence index, and blob resolver. +type ValidationInput struct { + ResultJSON []byte + IndexJSON []byte + ReadBlob BlobReader +} + +type observationKey struct { + Sequence int + AssertionID string +} + +// Validate performs complete offline validation. The canonical scenario and +// oracle are compiled into this package; launcher output never supplies an +// expectation. +func Validate(input ValidationInput) (*Result, error) { + var problems Problems + if findings := Scrub(input.ResultJSON); len(findings) != 0 { + problems.Add("result scrub failed: " + strings.Join(findings, "; ")) + } + if findings := Scrub(input.IndexJSON); len(findings) != 0 { + problems.Add("index scrub failed: " + strings.Join(findings, "; ")) + } + + var result Result + if err := decodeStrict(input.ResultJSON, &result); err != nil { + return nil, fmt.Errorf("decode result: %w", err) + } + var index EvidenceIndex + if err := decodeStrict(input.IndexJSON, &index); err != nil { + return nil, fmt.Errorf("decode evidence index: %w", err) + } + scenario := CanonicalScenario() + scenarioJSON, err := ScenarioJSON(scenario) + if err != nil { + return nil, err + } + + validateHeader(result, scenarioJSON, &problems) + validatePins(result.Pins, &problems) + validateStages(result, scenario, &problems) + observations := validateEvidence(result, index, input.ReadBlob, &problems) + validateAssertions(result, scenario, observations, &problems) + validateSummary(result, &problems) + validateSpecialOutcomes(result, &problems) + if err := problems.Err(); err != nil { + return &result, err + } + return &result, nil +} + +func decodeStrict(data []byte, dst any) error { + dec := json.NewDecoder(bytes.NewReader(data)) + dec.DisallowUnknownFields() + if err := dec.Decode(dst); err != nil { + return err + } + if err := dec.Decode(&struct{}{}); !errors.Is(err, io.EOF) { + if err == nil { + return fmt.Errorf("multiple JSON values") + } + return err + } + return nil +} + +func validateHeader(r Result, scenarioJSON []byte, p *Problems) { + if r.Schema != ResultSchema { + p.Add("schema: wrong result identity") + } + if r.Suite.Name != ScenarioName || r.Suite.Revision != 1 { + p.Add("suite: wrong name or revision") + } + if r.Suite.ManifestDigest != Digest(scenarioJSON) { + p.Add("suite.manifest_digest: does not match canonical scenario bytes") + } + if r.Suite.OracleDigest != OracleDigest() { + p.Add("suite.oracle_digest: does not match canonical oracle") + } + if r.Run.RunID == "" { + p.Add("run.run_id: missing") + } + if parsed, err := time.Parse(time.RFC3339, r.Run.ObservedAt); err != nil || parsed.Location() != time.UTC { + p.Add("run.observed_at: must be RFC3339 UTC") + } + if r.Run.HostOS != "linux" || r.Run.HostArch != "x86_64" || r.Run.FixtureRoot != "$RUN" { + p.Add("run: unsupported host or unrewritten fixture root") + } + if r.Run.PluginsEnabled || r.Run.MCPEnabled || r.Run.TerminalInputUsed { + p.Add("run: plugins, MCP, and terminal input must all be disabled") + } + if r.Scrub.Status != "pass" || r.Scrub.Policy != ScrubPolicy || len(r.Scrub.Findings) != 0 { + p.Add("scrub: capture did not pass the required policy") + } +} + +func validatePins(v Pins, p *Problems) { + wantLauncher, ok := AcceptedLauncherPin(v.Launcher.Name) + if !ok || v.Launcher.Version != wantLauncher.Version || v.Launcher.ExecutableSHA256 != wantLauncher.Digest { + p.Add("pins.launcher: identity is absent or not an exact accepted pin") + } + if !validDuoPin(v.Duo) { + p.Add("pins.duo: incomplete or mismatched build identity") + } + if v.Skill.Name != SkillName || v.Skill.FormatVersion != SkillFormat || v.Skill.ContentDigest != SkillContentDigest || v.Skill.InstallationID == "" { + p.Add("pins.skill: incomplete or mismatched projected skill identity") + } + if v.Config.Schema != "duo.config/v3" || !digestPattern.MatchString(v.Config.EffectiveDigest) { + p.Add("pins.config: incomplete or mismatched config identity") + } + if v.Authority.SchemaVersion != 1 || !digestPattern.MatchString(v.Authority.StoreDigestBefore) || !digestPattern.MatchString(v.Authority.StoreDigestAfter) { + p.Add("pins.authority: incomplete authority identity") + } + if v.Host.Name != "herdr" || v.Host.Version != "0.8.2" || v.Host.Protocol != "herdr-socket-api/20" || v.Host.SchemaDigest != HostSchemaDigest || !digestPattern.MatchString(v.Host.ExecutableSHA256) { + p.Add("pins.host: exact Herdr 0.8.2/protocol 20/schema pin required") + } + if v.Runtime.Name != "pi" || v.Runtime.Version != "0.83.0" || v.Runtime.Format != "pi-session-jsonl/v3" || v.Runtime.AdapterBuild != "stage1" || v.Runtime.ConformanceRecord != "pi-0.83.0-2026-08-23" || v.Runtime.DeliveryAssetSHA256 != DeliveryAssetDigest || !digestPattern.MatchString(v.Runtime.ExecutableSHA256) { + p.Add("pins.runtime: exact Pi 0.83.0 identity required") + } + if v.Model.Provider != "openai-codex" || v.Model.ModelLine != "gpt-5.6-luna" { + p.Add("pins.model: selected provider/model does not match the fixture") + } + if v.ExternalSchema.Identity != "duo.external/v1" || v.ExternalSchema.Digest != ExternalSchemaDigest { + p.Add("pins.external_schema: exact public wire identity required") + } +} + +func validateStages(r Result, scenario Scenario, p *Problems) { + if len(r.Stages) != len(scenario.Steps) { + p.Add(fmt.Sprintf("stages: got %d records, want %d", len(r.Stages), len(scenario.Steps))) + } + limit := len(r.Stages) + if len(scenario.Steps) < limit { + limit = len(scenario.Steps) + } + var priorEnd int64 + completeDeadlineFailed := false + for i := 0; i < limit; i++ { + got, want := r.Stages[i], scenario.Steps[i] + if got.Sequence != i+1 || got.Sequence != want.Sequence { + p.Add(fmt.Sprintf("stages[%d].sequence: not exact and monotonic", i)) + } + if got.Stage != want.Stage || got.Case != want.Case { + p.Add(fmt.Sprintf("stages[%d]: got %s/%s, want %s/%s", i, got.Case, got.Stage, want.Case, want.Stage)) + } + if got.StartedOffsetMS < priorEnd || got.DurationMS < 0 { + p.Add(fmt.Sprintf("stages[%d]: timing is not monotonic", i)) + } + if i == 0 && got.StartedOffsetMS != 0 { + p.Add("stages[0]: setup must start at monotonic offset zero") + } + stageMaximum := want.DeadlineMS + if got.Case == "timeout" && got.Stage == "observe" { + stageMaximum = CanonicalOracle().TimeoutMaximumMS + } + deadlineFailure := got.Verdict == "fail" && got.Error != nil && (got.Error.Code == "stage.deadline_exceeded" || got.Error.Code == "run.deadline_exceeded") + if got.DurationMS > stageMaximum && !deadlineFailure { + p.Add(fmt.Sprintf("stages[%d]: stage deadline exceeded", i)) + } + if got.Verdict == "fail" && got.Error != nil && got.Error.Code == "run.deadline_exceeded" { + completeDeadlineFailed = true + } + if got.StartedOffsetMS+got.DurationMS > scenario.DeadlinesMS["complete_run"] && !completeDeadlineFailed { + p.Add(fmt.Sprintf("stages[%d]: complete run deadline exceeded", i)) + } + priorEnd = got.StartedOffsetMS + got.DurationMS + if got.Verdict != "pass" && got.Verdict != "fail" { + p.Add(fmt.Sprintf("stages[%d].verdict: unknown value", i)) + } + if !oneOf(got.Outcome, "success", "blocked", "exited", "timeout", "error") { + p.Add(fmt.Sprintf("stages[%d].outcome: unknown value", i)) + } + if got.Verdict == "pass" { + if got.Error != nil { + p.Add(fmt.Sprintf("stages[%d]: passing stage has an error", i)) + } + if got.Outcome != want.Outcome { + p.Add(fmt.Sprintf("stages[%d]: passing outcome %q, want %q", i, got.Outcome, want.Outcome)) + } + } else { + if got.Outcome != "error" || got.Error == nil || got.Error.Code == "" || got.Error.Message == "" || got.Error.Effect == "" || got.Error.Retry.Action == "" { + p.Add(fmt.Sprintf("stages[%d]: failed stage lacks complete error metadata", i)) + } + } + if len(got.Evidence) == 0 { + p.Add(fmt.Sprintf("stages[%d]: no evidence references", i)) + } + seenEvidence := map[string]bool{} + for _, ref := range got.Evidence { + if !blobPattern.MatchString(ref) { + p.Add(fmt.Sprintf("stages[%d]: malformed evidence reference %q", i, ref)) + } + if seenEvidence[ref] { + p.Add(fmt.Sprintf("stages[%d]: duplicate evidence reference", i)) + } + seenEvidence[ref] = true + } + } + if len(r.Stages) > 0 { + last := r.Stages[len(r.Stages)-1] + if last.Stage != "cleanup" || last.Case != "run" { + p.Add("stages: cleanup must be last exactly once") + } + cleanupCount := 0 + for _, stage := range r.Stages { + if stage.Stage == "cleanup" { + cleanupCount++ + } + } + if cleanupCount != 1 { + p.Add("stages: cleanup must appear exactly once") + } + if last.Verdict != "pass" { + p.Add("cleanup: failed or omitted cleanup invalidates the capture") + } + } +} + +func validateEvidence(r Result, index EvidenceIndex, read BlobReader, p *Problems) map[observationKey]capturedObservation { + observations := map[observationKey]capturedObservation{} + if index.Schema != IndexSchema || index.Policy != ScrubPolicy { + p.Add("evidence index: wrong schema or scrub policy") + } + referenced := map[string]bool{} + for _, stage := range r.Stages { + for _, ref := range stage.Evidence { + referenced[ref] = true + } + } + indexed := map[string]bool{} + for i, entry := range index.Blobs { + path := fmt.Sprintf("evidence_index.blobs[%d]", i) + if indexed[entry.Reference] { + p.Add(path + ": duplicate blob") + } + indexed[entry.Reference] = true + if !referenced[entry.Reference] { + p.Add(path + ": unreferenced blob") + } + if entry.MediaType != "application/json" || entry.ScrubStatus != "pass" || !oneOf(entry.Producer, "common_collector", "controller", "duo", "launcher") { + p.Add(path + ": incomplete media/scrub/producer metadata") + } + if read == nil { + p.Add(path + ": no blob reader") + continue + } + data, err := read(entry.Reference) + if err != nil { + p.Add(path + ": missing blob: " + err.Error()) + continue + } + if int64(len(data)) != entry.Bytes || Digest(data) != entry.SHA256 || "blob:"+Digest(data) != entry.Reference { + p.Add(path + ": byte count or digest mismatch") + } + if findings := Scrub(data); len(findings) != 0 { + p.Add(path + ": scrub failed: " + strings.Join(findings, "; ")) + } + var evidence Evidence + if err := decodeStrict(data, &evidence); err != nil { + p.Add(path + ": malformed evidence: " + err.Error()) + continue + } + if evidence.Schema != EvidenceSchema || evidence.ScrubStatus != "pass" { + p.Add(path + ": wrong evidence schema or scrub status") + } + for _, obs := range evidence.Observations { + key := observationKey{Sequence: obs.Sequence, AssertionID: obs.AssertionID} + if _, exists := observations[key]; exists { + p.Add(fmt.Sprintf("evidence: duplicate observation for sequence %d assertion %s", obs.Sequence, obs.AssertionID)) + } + observations[key] = capturedObservation{Observation: obs, Reference: entry.Reference} + } + } + for ref := range referenced { + if !indexed[ref] { + p.Add("evidence: referenced blob missing from index: " + ref) + } + } + asserted := map[observationKey]bool{} + for _, stage := range r.Stages { + for _, assertion := range stage.Assertions { + asserted[observationKey{Sequence: stage.Sequence, AssertionID: assertion.ID}] = true + } + } + for key := range observations { + if !asserted[key] { + p.Add(fmt.Sprintf("evidence: unclaimed observation for sequence %d assertion %s", key.Sequence, key.AssertionID)) + } + } + return observations +} + +func validateSummary(r Result, p *Problems) { + var first *StageResult + allPass := true + for i := range r.Stages { + if r.Stages[i].Verdict == "fail" { + allPass = false + if first == nil { + first = &r.Stages[i] + } + } + } + if allPass { + if r.Summary.Verdict != "pass" || r.Summary.FirstFailedStage != nil || r.Summary.FirstFailedCase != nil { + p.Add("summary: inconsistent all-pass summary") + } + p.Add("summary: overall pass forbidden while supported blocked evidence is unavailable") + return + } + if r.Summary.Verdict != "fail" || first == nil || r.Summary.FirstFailedStage == nil || r.Summary.FirstFailedCase == nil || *r.Summary.FirstFailedStage != first.Stage || *r.Summary.FirstFailedCase != first.Case { + p.Add("summary: inconsistent first failure") + } +} + +func validateSpecialOutcomes(r Result, p *Problems) { + for i, stage := range r.Stages { + if stage.Case == "blocked" && stage.Verdict == "pass" { + p.Add(fmt.Sprintf("stages[%d]: supported_admitted_then_blocked_producer is unavailable", i)) + } + if stage.Case == "blocked" && stage.Stage == "launch" && stage.Verdict == "fail" { + if stage.Error == nil || stage.Error.Code != "prerequisite.blocked_induction_unavailable" || stage.Error.Effect != "no_effect" || stage.Error.Retry.Safe || stage.Error.Retry.Action != "add_supported_blocked_evidence" { + p.Add(fmt.Sprintf("stages[%d]: blocked prerequisite failure metadata is not exact", i)) + } + } + if stage.Case == "timeout" && stage.Stage == "observe" && stage.Verdict == "pass" { + actual := assertionActual(stage, "observe.timeout") + e := TimeoutEvidence{ + ElapsedMS: int64Number(actual["elapsed_ms"]), ProcessSucceeded: boolValue(actual["process_success"]), + StoppedVerified: boolValue(actual["stopped_verified"]), Delivered: boolValue(actual["delivered"]), + Attempts: int(int64Number(actual["attempts"])), AssistantBlocks: int(int64Number(actual["assistant_blocks"])), + OuterTimedOut: boolValue(actual["outer_timed_out"]), + } + if err := ValidateIntentionalTimeout(e); err != nil { + p.Add(fmt.Sprintf("stages[%d]: %v", i, err)) + } + if stage.DurationMS < 20_000 || stage.DurationMS > 22_500 { + p.Add(fmt.Sprintf("stages[%d]: timeout duration outside 20000..22500", i)) + } + } + } +} + +func assertionActual(stage StageResult, id string) map[string]any { + for _, a := range stage.Assertions { + if a.ID == id { + return a.Actual + } + } + return nil +} + +func int64Number(v any) int64 { + switch n := v.(type) { + case float64: + return int64(n) + case int64: + return n + case int: + return int64(n) + default: + return 0 + } +} + +func boolValue(v any) bool { b, _ := v.(bool); return b } + +func stringValue(v any) string { s, _ := v.(string); return s } + +func isNumber(v any) bool { + switch v.(type) { + case float64, int64, int: + return true + default: + return false + } +} + +func oneOf(value string, allowed ...string) bool { + for _, candidate := range allowed { + if value == candidate { + return true + } + } + return false +} diff --git a/internal/doctor/doctor.go b/internal/doctor/doctor.go index 00bb45c..b9e1a54 100644 --- a/internal/doctor/doctor.go +++ b/internal/doctor/doctor.go @@ -1,23 +1,19 @@ -// Package doctor implements `duo doctor`'s core diagnostics: authority-store -// health (schema version, writer-lease state) and the registered-adapters -// inventory. It is deliberately narrow — Step 10's "core" scope — and does -// not yet cover generated-artifact drift, harness trust, or live socket -// checks (duo-vnext-installation-contract.md and duo-vnext-external- -// surfaces.md describe the full eventual `duo doctor`; docs/doctor/ -// decisions.md records what this package intentionally leaves out and why). +// Package doctor implements `duo doctor` diagnostics: authority-store health, +// registered adapters, read-only harness inspection, and the stable portable +// launcher-preflight report shape. // -// This package reads internal/store's public API only (Open, OpenAuthority, -// the lease error types) and never touches internal/host or -// internal/runtime. Adapter rows arrive from the caller — the composition -// root owns which adapter factories exist, and hands doctor their §5.1 -// descriptors through FromDescriptor; doctor itself stays neutral. +// Store diagnosis uses OpenReadOnly and direct lease inspection; it never +// creates/migrates a database or acquires a writer lease. Adapter rows and +// host/projection evidence arrive from the composition root, keeping this +// package neutral about concrete host and runtime adapters. package doctor import ( - "errors" + "context" "fmt" "os" "path/filepath" + "time" "github.com/procrastivity/duo/internal/adapter" "github.com/procrastivity/duo/internal/store" @@ -158,10 +154,9 @@ func Run(path string, adapters []Adapter) Report { } } -// probeStore reports path's health without ever creating a store file that -// was not already there. See docs/doctor/decisions.md for why the writer- -// lease probe (a transient OpenAuthority + immediate Close when no writer -// holds it) still counts as a read-only check. +// probeStore reports path's health through SQLite's physical read-only mode. +// It never creates/migrates a database and observes the writer lease without +// acquiring even a transient lease. func probeStore(path string) StoreStatus { status := StoreStatus{Path: path} @@ -179,17 +174,24 @@ func probeStore(path string) StoreStatus { } status.Present = true - ro, err := store.Open(path) + ro, err := store.OpenReadOnly(path) if err != nil { status.Error = err.Error() return status } status.SchemaVersion = ro.Version() + lease, writerErr := ro.InspectWriterLease(context.Background(), time.Now()) + if writerErr == nil { + status.Writer = &WriterStatus{ + Active: lease.Active, + Incarnation: lease.Incarnation, + PID: lease.PID, + Hostname: lease.Hostname, + ExpiresAt: lease.ExpiresAt, + } + } closeErr := ro.Close() - writer, writerErr := probeWriter(path) - status.Writer = writer - switch { case closeErr != nil: status.Error = closeErr.Error() @@ -200,29 +202,3 @@ func probeStore(path string) StoreStatus { } return status } - -// probeWriter reports whether another process holds the authority-writer -// lease. When nothing holds it, OpenAuthority itself briefly becomes the -// writer to observe that fact — this handle is closed immediately, which -// deletes the just-minted lease row again (Store.Close's documented -// behavior), leaving the lease exactly as unclaimed as it was found. -func probeWriter(path string) (*WriterStatus, error) { - authority, err := store.OpenAuthority(path) - - var active *store.WriterActiveError - if errors.As(err, &active) { - return &WriterStatus{ - Active: true, - Incarnation: active.Incarnation, - PID: active.PID, - Hostname: active.Hostname, - ExpiresAt: active.ExpiresAt, - }, nil - } - if err != nil { - return nil, err - } - - closeErr := authority.Close() - return &WriterStatus{Active: false}, closeErr -} diff --git a/internal/doctor/doctor_test.go b/internal/doctor/doctor_test.go index 2275af7..9d7add6 100644 --- a/internal/doctor/doctor_test.go +++ b/internal/doctor/doctor_test.go @@ -1,6 +1,8 @@ package doctor import ( + "bytes" + "os" "path/filepath" "testing" @@ -43,6 +45,14 @@ func TestRun_PresentNoWriter(t *testing.T) { if err := s.Close(); err != nil { t.Fatalf("Close (setup): %v", err) } + before, err := os.ReadFile(path) + if err != nil { + t.Fatalf("ReadFile before doctor: %v", err) + } + beforeInfo, err := os.Stat(path) + if err != nil { + t.Fatalf("Stat before doctor: %v", err) + } report := Run(path, nil) @@ -61,6 +71,17 @@ func TestRun_PresentNoWriter(t *testing.T) { if report.Store.Writer.Active { t.Errorf("Writer.Active = true, want false: %+v", report.Store.Writer) } + after, err := os.ReadFile(path) + if err != nil { + t.Fatalf("ReadFile after doctor: %v", err) + } + afterInfo, err := os.Stat(path) + if err != nil { + t.Fatalf("Stat after doctor: %v", err) + } + if !bytes.Equal(before, after) || !beforeInfo.ModTime().Equal(afterInfo.ModTime()) || beforeInfo.Size() != afterInfo.Size() { + t.Fatal("doctor store probe changed database bytes or metadata") + } // The probe must not have left a lingering lease: a second probe must // see the same "no writer" result, not "active" because a lease row diff --git a/internal/doctor/harness.go b/internal/doctor/harness.go index bfdda52..3354c64 100644 --- a/internal/doctor/harness.go +++ b/internal/doctor/harness.go @@ -33,8 +33,48 @@ type HarnessSweep struct { // Kept is the number of launch-resolution directories left in place // because KeepHarnessDir returned true. Kept int `json:"kept"` - // IDs are the reaped launch-resolution directory names, sorted. + // IDs are affected launch-resolution directory names, sorted: reaped + // for SweepHarnessDirs and reported orphaned for InspectHarnessDirs. IDs []string `json:"ids,omitempty"` + // Orphaned is the number of launch-resolution directories diagnosis + // found without a live owner. Doctor reports these but never reaps them. + Orphaned int `json:"orphaned"` + // ReadOnly distinguishes a diagnostic inspection from the explicit + // cleanup helper retained below for non-doctor callers. + ReadOnly bool `json:"read_only"` + // Error is a safe read failure. Harness state is informational and does + // not prevent the required launcher checks from producing a report. + Error string `json:"error,omitempty"` +} + +// InspectHarnessDirs reports immediate launch-resolution directories without +// deleting them. IDs are orphan names in sorted order; Reaped is always zero. +func InspectHarnessDirs(root string, keep KeepHarnessDir) (HarnessSweep, error) { + out := HarnessSweep{IDs: []string{}, ReadOnly: true} + if root == "" { + return out, fmt.Errorf("doctor: harness inspection needs a root") + } + entries, err := os.ReadDir(root) + if err != nil { + if os.IsNotExist(err) { + return out, nil + } + return out, fmt.Errorf("doctor: reading harness directory %s: %w", root, err) + } + for _, entry := range entries { + name := entry.Name() + if name == "" || name == "." || name == ".." || !entry.IsDir() || filepath.Base(name) != name { + continue + } + if keep != nil && keep(name) { + out.Kept++ + continue + } + out.Orphaned++ + out.IDs = append(out.IDs, name) + } + sort.Strings(out.IDs) + return out, nil } // SweepHarnessDirs deletes immediate subdirectories of root whose diff --git a/internal/doctor/harness_test.go b/internal/doctor/harness_test.go index 33c8d7f..85e2388 100644 --- a/internal/doctor/harness_test.go +++ b/internal/doctor/harness_test.go @@ -84,6 +84,28 @@ func TestSweepHarnessDirsNilKeepReapsAll(t *testing.T) { } } +func TestInspectHarnessDirsReportsWithoutDeleting(t *testing.T) { + root := t.TempDir() + mustHarnessDir(t, root, "lrr_live", "primary") + mustHarnessDir(t, root, "lrr_orphan", "main") + + report, err := InspectHarnessDirs(root, func(id string) bool { return id == "lrr_live" }) + if err != nil { + t.Fatalf("InspectHarnessDirs: %v", err) + } + if !report.ReadOnly || report.Reaped != 0 || report.Kept != 1 || report.Orphaned != 1 { + t.Fatalf("inspection = %+v, want read-only, kept 1, orphaned 1, reaped 0", report) + } + if len(report.IDs) != 1 || report.IDs[0] != "lrr_orphan" { + t.Fatalf("IDs = %v, want [lrr_orphan]", report.IDs) + } + for _, id := range []string{"lrr_live", "lrr_orphan"} { + if _, err := os.Stat(filepath.Join(root, id)); err != nil { + t.Errorf("inspection removed %s: %v", id, err) + } + } +} + func mustHarnessDir(t *testing.T, root, lrr, leaf string) { t.Helper() dir := filepath.Join(root, lrr, leaf) diff --git a/internal/doctor/preflight.go b/internal/doctor/preflight.go new file mode 100644 index 0000000..effb24a --- /dev/null +++ b/internal/doctor/preflight.go @@ -0,0 +1,177 @@ +package doctor + +// LauncherPreflightSchema is the stable launcher-readiness report version. +const LauncherPreflightSchema = "duo.launcher-preflight/v1" + +// Stable launcher-preflight check IDs in required report order. +const ( + CheckDuoExecutable = "duo_executable" + CheckEffectiveConfig = "effective_config" + CheckAuthorityStore = "authority_store" + CheckWorkspace = "workspace" + CheckHostSelection = "host_selection" + CheckHostReachability = "host_reachability" + CheckSkillProjection = "skill_projection" + CheckHostCompatibility = "host_compatibility" +) + +// Check is one closed launcher-preflight row. Action is empty only for a +// passing check or an informational check that was not reached. +type Check struct { + ID string `json:"id"` + Stage string `json:"stage"` + Required bool `json:"required"` + Status string `json:"status"` + Code string `json:"code"` + Summary string `json:"summary"` + Action string `json:"action"` +} + +// DuoIdentity is the exact running executable and compiled build triple. +type DuoIdentity struct { + ExecutablePath string `json:"executable_path"` + Version string `json:"version"` + Commit string `json:"commit"` + BuildDate string `json:"build_date"` +} + +// EffectiveConfig is the selected validated config's non-secret identity. +type EffectiveConfig struct { + Path string `json:"path"` + Schema string `json:"schema"` + EffectiveDigest string `json:"effective_digest"` + Valid bool `json:"valid"` + Presets []string `json:"presets"` +} + +// AuthorityPreflight is the read-only local authority-store diagnosis. +type AuthorityPreflight struct { + Path string `json:"path"` + State string `json:"state"` + Present bool `json:"present"` + Healthy bool `json:"healthy"` + SchemaVersion int `json:"schema_version"` + WriterActive bool `json:"writer_active"` +} + +// WorkspacePreflight is launch's selected workspace and its local shape. +type WorkspacePreflight struct { + RequestedPath string `json:"requested_path"` + SelectedPath string `json:"selected_path"` + Source string `json:"source"` + Exists bool `json:"exists"` + Directory bool `json:"directory"` +} + +// HostPreflight is M1's selected host plus bounded probe evidence. +type HostPreflight struct { + Selected bool `json:"selected"` + Kind string `json:"kind"` + InstanceID string `json:"instance_id"` + InstanceLabel string `json:"instance_label"` + HostSource string `json:"host_source"` + Reachable bool `json:"reachable"` + DetectedVersion string `json:"detected_version"` + ProtocolIdentity string `json:"protocol_identity"` + Compatibility string `json:"compatibility"` +} + +// SkillProjection is the copied portable launcher's full artifact identity. +type SkillProjection struct { + Target string `json:"target"` + Name string `json:"name"` + FormatVersion string `json:"format_version"` + ContentDigest string `json:"content_digest"` + Root string `json:"root"` + File string `json:"file"` + Stamp string `json:"stamp"` + State string `json:"state"` + InstallationID string `json:"installation_id"` +} + +// LauncherPreflight is the stable top-level launcher readiness object. +// Every field is deliberately non-omitempty: unavailable evidence is +// represented by the field's zero value, never by changing the shape. +type LauncherPreflight struct { + Schema string `json:"schema"` + Status string `json:"status"` + AuthorityScope string `json:"authority_scope"` + Checks []Check `json:"checks"` + Duo DuoIdentity `json:"duo"` + Config EffectiveConfig `json:"config"` + Authority AuthorityPreflight `json:"authority"` + Workspace WorkspacePreflight `json:"workspace"` + Host HostPreflight `json:"host"` + SkillProjection SkillProjection `json:"skill_projection"` +} + +// NewLauncherPreflight returns the closed eight-row check list in contract +// order. Callers update rows with SetCheck and then call Finalize. +func NewLauncherPreflight() LauncherPreflight { + return LauncherPreflight{ + Schema: LauncherPreflightSchema, + Status: "not_ready", + AuthorityScope: "unavailable", + Checks: []Check{ + {ID: CheckDuoExecutable, Stage: "executable", Required: true, Status: "not_checked", Code: "prerequisite.not_reached"}, + {ID: CheckEffectiveConfig, Stage: "config", Required: true, Status: "not_checked", Code: "prerequisite.not_reached"}, + {ID: CheckAuthorityStore, Stage: "authority", Required: true, Status: "not_checked", Code: "prerequisite.not_reached"}, + {ID: CheckWorkspace, Stage: "workspace", Required: true, Status: "not_checked", Code: "prerequisite.not_reached"}, + {ID: CheckHostSelection, Stage: "host_selection", Required: true, Status: "not_checked", Code: "prerequisite.not_reached"}, + {ID: CheckHostReachability, Stage: "host_reachability", Required: true, Status: "not_checked", Code: "prerequisite.not_reached"}, + {ID: CheckSkillProjection, Stage: "skill_projection", Required: true, Status: "not_checked", Code: "prerequisite.not_reached"}, + {ID: CheckHostCompatibility, Stage: "host_reachability", Required: false, Status: "not_checked", Code: "prerequisite.not_reached"}, + }, + Config: EffectiveConfig{Presets: []string{}}, + Host: HostPreflight{Compatibility: "unknown"}, + } +} + +// SetCheck replaces one existing row without changing order or allowing the +// closed list to grow. It returns false only for a programmer error (unknown +// ID), which lets callers turn that into an internal command failure. +func (p *LauncherPreflight) SetCheck(id, status, code, summary, action string) bool { + for i := range p.Checks { + if p.Checks[i].ID != id { + continue + } + p.Checks[i].Status = status + p.Checks[i].Code = code + p.Checks[i].Summary = summary + p.Checks[i].Action = action + return true + } + return false +} + +// Finalize derives readiness only from the seven required checks. Host +// compatibility is informational and may warn without refusing launch. +// authority_scope is independent of executable identity and an active writer: +// it is local when the exact config/store/workspace/projection resources are +// usable and the selected host answers. An active writer still makes the +// launch not ready, but does not make those resources remote. +func (p *LauncherPreflight) Finalize() { + p.Status = "ready" + for _, check := range p.Checks { + if check.Required && check.Status != "pass" { + p.Status = "not_ready" + break + } + } + p.AuthorityScope = "unavailable" + authorityLocal := checkStatus(p.Checks, CheckAuthorityStore) == "pass" || p.Authority.State == "writer_active" + if checkStatus(p.Checks, CheckEffectiveConfig) == "pass" && authorityLocal && + checkStatus(p.Checks, CheckWorkspace) == "pass" && checkStatus(p.Checks, CheckHostSelection) == "pass" && + checkStatus(p.Checks, CheckHostReachability) == "pass" && checkStatus(p.Checks, CheckSkillProjection) == "pass" { + p.AuthorityScope = "local" + } +} + +func checkStatus(checks []Check, id string) string { + for _, check := range checks { + if check.ID == id { + return check.Status + } + } + return "" +} diff --git a/internal/host/herdr/control.go b/internal/host/herdr/control.go new file mode 100644 index 0000000..cdf3131 --- /dev/null +++ b/internal/host/herdr/control.go @@ -0,0 +1,70 @@ +package herdr + +import ( + "context" + "errors" + "fmt" + + "github.com/procrastivity/duo/internal/host" +) + +// CloseExactAttachment is the narrow pane-control seam used by portable +// launcher conformance fault induction. It refuses to act unless the claim +// completely identifies this integration instance, the pane's terminal +// incarnation, and a proven process birth, then revalidates all of that +// continuity immediately before closing the pane. +// +// Herdr 0.8.2's pane.close accepts only pane_id. Consequently, the full +// continuity check and the mutating request are separate server calls: this +// method cannot provide server-side compare-and-close atomicity, and the pane +// can change between validation and close. It makes no stronger guarantee. +func (h *Host) CloseExactAttachment(ctx context.Context, claim host.HostAttachmentClaim) error { + if err := h.validateExactCloseClaim(claim); err != nil { + return err + } + + continuity, err := h.ValidateAttachment(ctx, claim) + if err != nil { + return err + } + if continuity.Class != host.ContinuitySameLive { + return fmt.Errorf("herdr: exact attachment close requires %s continuity, got %s", + host.ContinuitySameLive, continuity.Class) + } + if !evidenceMatchesCloseClaim(continuity.Evidence, claim) { + return errors.New("herdr: exact attachment close continuity evidence does not match the claim") + } + + // Do not reinterpret pane_not_found here. After successful validation it + // means the separate close request did not prove the requested induction. + return h.client.call(ctx, "pane.close", paneTargetParams{PaneID: claim.Attachment.PaneID}, nil) +} + +func (h *Host) validateExactCloseClaim(claim host.HostAttachmentClaim) error { + attachment := claim.Attachment + if attachment.IntegrationInstanceID == "" { + return errors.New("herdr: exact attachment close requires an integration instance ID") + } + if err := h.requireInstance(attachment.IntegrationInstanceID); err != nil { + return err + } + if attachment.PaneID == "" { + return errors.New("herdr: exact attachment close requires a pane ID") + } + if attachment.HostContainerID == "" { + return errors.New("herdr: exact attachment close requires a terminal container ID") + } + if !birthProven(claim.LastKnownProcessBirth) { + return errors.New("herdr: exact attachment close requires proven process birth") + } + return nil +} + +func evidenceMatchesCloseClaim(evidence host.Evidence, claim host.HostAttachmentClaim) bool { + attachment := claim.Attachment + return evidence.IntegrationInstanceID == attachment.IntegrationInstanceID && + evidence.HostServerEpoch == attachment.HostServerEpoch && + evidence.HostContainerID == attachment.HostContainerID && + evidence.PaneID == attachment.PaneID && + sameBirth(evidence.ProcessBirth, claim.LastKnownProcessBirth) +} diff --git a/internal/host/herdr/control_test.go b/internal/host/herdr/control_test.go new file mode 100644 index 0000000..951a705 --- /dev/null +++ b/internal/host/herdr/control_test.go @@ -0,0 +1,181 @@ +package herdr + +import ( + "context" + "testing" + + "github.com/procrastivity/duo/internal/host" +) + +func TestCloseExactAttachmentClosesOnlyTheClaimedPane(t *testing.T) { + f := newFakeHerdr(t) + target := f.addPane("w1") + survivor := f.addPane("w1") + h := testHost(t, f) + + if err := h.CloseExactAttachment(context.Background(), exactControlClaim(target)); err != nil { + t.Fatalf("CloseExactAttachment: %v", err) + } + + if got := f.callCount("pane.close"); got != 1 { + t.Fatalf("pane.close calls = %d, want 1", got) + } + if got := f.paneCount(); got != 1 { + t.Fatalf("remaining panes = %d, want 1", got) + } + f.mu.Lock() + _, targetFound := f.findLocked(target.paneID) + _, survivorFound := f.findLocked(survivor.paneID) + f.mu.Unlock() + if targetFound || !survivorFound { + t.Fatalf("pane state after close: target found = %t, survivor found = %t", targetFound, survivorFound) + } +} + +func TestCloseExactAttachmentRejectsIncompleteClaimsBeforeClose(t *testing.T) { + tests := []struct { + name string + mutate func(*host.HostAttachmentClaim) + }{ + { + name: "integration instance", + mutate: func(claim *host.HostAttachmentClaim) { + claim.Attachment.IntegrationInstanceID = "" + }, + }, + { + name: "pane ID", + mutate: func(claim *host.HostAttachmentClaim) { + claim.Attachment.PaneID = "" + }, + }, + { + name: "terminal container ID", + mutate: func(claim *host.HostAttachmentClaim) { + claim.Attachment.HostContainerID = "" + }, + }, + { + name: "process birth", + mutate: func(claim *host.HostAttachmentClaim) { + claim.LastKnownProcessBirth = host.ProcessBirthEvidence{} + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + f := newFakeHerdr(t) + pane := f.addPane("w1") + h := testHost(t, f) + claim := exactControlClaim(pane) + tt.mutate(&claim) + + if err := h.CloseExactAttachment(context.Background(), claim); err == nil { + t.Fatal("CloseExactAttachment accepted an incomplete claim") + } + if got := f.callCount("pane.close"); got != 0 { + t.Fatalf("pane.close calls = %d, want 0", got) + } + }) + } +} + +func TestCloseExactAttachmentRefusesAnythingButExactSameLiveContinuity(t *testing.T) { + t.Run("wrong integration", func(t *testing.T) { + f := newFakeHerdr(t) + pane := f.addPane("w1") + h := testHost(t, f) + claim := exactControlClaim(pane) + claim.Attachment.IntegrationInstanceID = "herdr:other" + + assertExactCloseRefused(t, h, f, claim) + if got := f.callCount("pane.get"); got != 0 { + t.Fatalf("pane.get calls = %d, want 0 for the wrong integration", got) + } + }) + + t.Run("terminal replacement", func(t *testing.T) { + f := newFakeHerdr(t) + pane := f.addPane("w1") + h := testHost(t, f) + claim := exactControlClaim(pane) + f.mutatePane(pane.paneID, func(p *fakePaneState) { p.terminalID = "term_replaced" }) + + assertExactCloseRefused(t, h, f, claim) + }) + + t.Run("process replacement", func(t *testing.T) { + f := newFakeHerdr(t) + pane := f.addPane("w1") + h := testHost(t, f) + claim := exactControlClaim(pane) + f.mutatePane(pane.paneID, func(p *fakePaneState) { p.fgPID++ }) + + assertExactCloseRefused(t, h, f, claim) + }) + + t.Run("unproven live birth", func(t *testing.T) { + f := newFakeHerdr(t) + pane := f.addPane("w1") + h := testHost(t, f, func(c *Config) { c.ResolveProcessBirth = unprovenBirth }) + + assertExactCloseRefused(t, h, f, exactControlClaim(pane)) + }) + + t.Run("already absent pane", func(t *testing.T) { + f := newFakeHerdr(t) + pane := f.addPane("w1") + h := testHost(t, f) + claim := exactControlClaim(pane) + f.removePane(pane.paneID) + + assertExactCloseRefused(t, h, f, claim) + }) + + t.Run("nonmatching server epoch", func(t *testing.T) { + f := newFakeHerdr(t) + pane := f.addPane("w1") + h := testHost(t, f) + claim := exactControlClaim(pane) + claim.Attachment.HostServerEpoch = "epoch-herdr-does-not-have" + + assertExactCloseRefused(t, h, f, claim) + }) +} + +func TestCloseExactAttachmentReturnsCloseProtocolError(t *testing.T) { + f := newFakeHerdr(t) + pane := f.addPane("w1") + h := testHost(t, f) + f.setPaneCloseError(CodePaneNotFound, "pane disappeared after validation") + + err := h.CloseExactAttachment(context.Background(), exactControlClaim(pane)) + if err == nil { + t.Fatal("CloseExactAttachment treated pane_not_found as success") + } + if got := ErrorCode(err); got != CodePaneNotFound { + t.Fatalf("ErrorCode = %q, want %q (error: %v)", got, CodePaneNotFound, err) + } + if got := f.callCount("pane.close"); got != 1 { + t.Fatalf("pane.close calls = %d, want 1", got) + } +} + +func assertExactCloseRefused(t *testing.T, h *Host, f *fakeHerdr, claim host.HostAttachmentClaim) { + t.Helper() + if err := h.CloseExactAttachment(context.Background(), claim); err == nil { + t.Fatal("CloseExactAttachment accepted a claim without exact same-live continuity") + } + if got := f.callCount("pane.close"); got != 0 { + t.Fatalf("pane.close calls = %d, want 0", got) + } +} + +func exactControlClaim(pane fakePaneState) host.HostAttachmentClaim { + info := processInfo{ShellPID: pane.shellPID} + if pane.fgPID > 0 { + info.ForegroundProcesses = []processEntry{{PID: pane.fgPID}} + } + return claimFor(pane, fakeBirth(context.Background(), info)) +} diff --git a/internal/launch/placement_test.go b/internal/launch/placement_test.go index 4efb62c..943b0ca 100644 --- a/internal/launch/placement_test.go +++ b/internal/launch/placement_test.go @@ -111,18 +111,18 @@ func TestConfigurationDigestPinsLaunchTarget(t *testing.T) { }, } - baseDigest, err := configurationDigest(base) + baseDigest, err := ConfigurationDigest(base) if err != nil { t.Fatalf("configurationDigest(base): %v", err) } - withDigest, err := configurationDigest(withTarget) + withDigest, err := ConfigurationDigest(withTarget) if err != nil { t.Fatalf("configurationDigest(withTarget): %v", err) } if baseDigest == withDigest { t.Fatalf("launch_target did not change configurationDigest: both %q", baseDigest) } - again, err := configurationDigest(withTarget) + again, err := ConfigurationDigest(withTarget) if err != nil { t.Fatalf("configurationDigest(withTarget) again: %v", err) } @@ -238,18 +238,18 @@ func TestConfigurationDigestPinsCloseOnExit(t *testing.T) { }, } - baseDigest, err := configurationDigest(base) + baseDigest, err := ConfigurationDigest(base) if err != nil { t.Fatalf("configurationDigest(base): %v", err) } - withDigest, err := configurationDigest(withClose) + withDigest, err := ConfigurationDigest(withClose) if err != nil { t.Fatalf("configurationDigest(withClose): %v", err) } if baseDigest == withDigest { t.Fatalf("close_on_exit did not change configurationDigest: both %q", baseDigest) } - again, err := configurationDigest(withClose) + again, err := ConfigurationDigest(withClose) if err != nil { t.Fatalf("configurationDigest(withClose) again: %v", err) } diff --git a/internal/launch/record.go b/internal/launch/record.go index cda5304..ca14e45 100644 --- a/internal/launch/record.go +++ b/internal/launch/record.go @@ -387,7 +387,7 @@ func mintRecordID() (string, error) { return recordPrefix + "_" + hex.EncodeToString(b[:]), nil } -// configurationDigest hashes the launch-relevant subset of a resolved +// ConfigurationDigest hashes the launch-relevant subset of a resolved // configuration document: the declaration families resolution reads. // // §7.1 calls this "the resolved launch-configuration digest — the merged @@ -403,7 +403,7 @@ func mintRecordID() (string, error) { // evidence bundle the record cites separately. Two launches of the same // document against two different sockets share a configuration digest, // which is the honest answer: the configuration did not change. -func configurationDigest(doc config.DocumentV3) (string, error) { +func ConfigurationDigest(doc config.DocumentV3) (string, error) { // encoding/json sorts map keys, so the same document always produces // the same bytes regardless of decode order. subset := struct { diff --git a/internal/launch/resolve.go b/internal/launch/resolve.go index 0d32dd2..2988f31 100644 --- a/internal/launch/resolve.go +++ b/internal/launch/resolve.go @@ -113,7 +113,7 @@ func NewResolver(doc config.DocumentV3, mat materialize.Result, opts Options) (* opts.NewID = mintRecordID } - digest, err := configurationDigest(doc) + digest, err := ConfigurationDigest(doc) if err != nil { return nil, err } diff --git a/internal/manifest/assets.go b/internal/manifest/assets.go index e823adb..5b1faa0 100644 --- a/internal/manifest/assets.go +++ b/internal/manifest/assets.go @@ -13,6 +13,7 @@ import ( "github.com/procrastivity/duo/internal/asset" rootassets "github.com/procrastivity/duo/assets" + rootskills "github.com/procrastivity/duo/skills" ) // walkAssets lists every file under the shipped assets/ tree with a sha256 @@ -66,23 +67,28 @@ func walkDiskDir(dir string) ([]Asset, bool, error) { func walkEmbedded() ([]Asset, error) { var out []Asset - err := fs.WalkDir(rootassets.FS, ".", func(path string, d fs.DirEntry, err error) error { - if err != nil { - return err - } - if d.IsDir() || !isShippedAsset(path) { + walk := func(fsys fs.FS, prefix string) error { + return fs.WalkDir(fsys, ".", func(path string, d fs.DirEntry, err error) error { + if err != nil { + return err + } + if d.IsDir() || !isShippedAsset(path) { + return nil + } + data, err := fs.ReadFile(fsys, path) + if err != nil { + return err + } + out = append(out, Asset{Path: prefix + path, SHA256: Checksum(data)}) return nil - } - data, err := rootassets.FS.ReadFile(path) - if err != nil { - return err - } - out = append(out, Asset{Path: path, SHA256: Checksum(data)}) - return nil - }) - if err != nil { + }) + } + if err := walk(rootassets.FS, ""); err != nil { return nil, fmt.Errorf("manifest: walking embedded asset fallback: %w", err) } + if err := walk(rootskills.FS, "skills/"); err != nil { + return nil, fmt.Errorf("manifest: walking embedded skill fallback: %w", err) + } sort.Slice(out, func(i, j int) bool { return out[i].Path < out[j].Path }) return out, nil diff --git a/internal/manifest/manifest.go b/internal/manifest/manifest.go index ee0fbcc..12764a9 100644 --- a/internal/manifest/manifest.go +++ b/internal/manifest/manifest.go @@ -57,12 +57,55 @@ type Adapter struct { } // HarnessTarget describes one generated-integration harness target — the -// duo.manifest/v1 contract's "harness_targets" array. No harness renderer -// exists yet, so Build always emits an empty slice; the shape exists ready -// for that later work (duo-vnext-installation-contract.md §2). +// duo.manifest/v1 contract's "harness_targets" array. Build currently emits +// the one launcher-neutral portable skill target. type HarnessTarget struct { - Name string `json:"name"` - Status string `json:"status"` + Name string `json:"name"` + Status string `json:"status"` + Scope string `json:"scope"` + DiscoveryRoot string `json:"discovery_root"` + ProjectionRoot string `json:"projection_root"` + StampFile string `json:"stamp_file"` + ProjectionFormat string `json:"projection_format"` + Components []string `json:"components"` + Artifact Artifact `json:"artifact"` + Launchers []Launcher `json:"launchers"` +} + +// Artifact is the public identity and placement of a target payload. +type Artifact struct { + Name string `json:"name"` + MediaType string `json:"media_type"` + SourceAsset string `json:"source_asset"` + OutputPath string `json:"output_path"` + ContentDigest string `json:"content_digest"` +} + +// Launcher records one exact launcher-version evidence set. +type Launcher struct { + Name string `json:"name"` + TestedVersions []string `json:"tested_versions"` +} + +// UnmarshalJSON keeps launcher rows closed even though a projection target's +// outer object intentionally remains extensible for existing consumers. +func (l *Launcher) UnmarshalJSON(data []byte) error { + var fields map[string]json.RawMessage + if err := json.Unmarshal(data, &fields); err != nil { + return err + } + for key := range fields { + if key != "name" && key != "tested_versions" { + return fmt.Errorf("manifest: unknown launcher field %q", key) + } + } + type launcherAlias Launcher + var decoded launcherAlias + if err := json.Unmarshal(data, &decoded); err != nil { + return err + } + *l = Launcher(decoded) + return nil } // Arg describes one flag a verb declares on itself (its LocalFlags — the @@ -139,6 +182,10 @@ func Build(root *cobra.Command, build buildinfo.Info) (Manifest, error) { if assets == nil { assets = []Asset{} } + portableTarget, err := portableLauncherTarget(assets) + if err != nil { + return Manifest{}, err + } contractDigests, err := loadContracts() if err != nil { @@ -154,7 +201,7 @@ func Build(root *cobra.Command, build buildinfo.Info) (Manifest, error) { Operations: registry.ManifestOperations(), Adapters: []Adapter{}, Assets: assets, - HarnessTargets: []HarnessTarget{}, + HarnessTargets: []HarnessTarget{portableTarget}, Tool: Tool{ Name: root.Name(), @@ -176,6 +223,46 @@ func Build(root *cobra.Command, build buildinfo.Info) (Manifest, error) { return m, nil } +func portableLauncherTarget(assets []Asset) (HarnessTarget, error) { + digest := "" + for _, a := range assets { + if a.Path == PortableSkillAsset { + digest = "sha256:" + a.SHA256 + break + } + } + if digest == "" { + return HarnessTarget{}, fmt.Errorf("manifest: required shipped asset %q is absent", PortableSkillAsset) + } + return HarnessTarget{ + Name: PortableTargetName, + Status: "unverified", + Scope: "workspace", + DiscoveryRoot: ".agents/skills", + ProjectionRoot: PortableProjectionRoot, + StampFile: ProjectionStampFile, + ProjectionFormat: PortableProjectionFormat, + Components: []string{"filesystem_skill"}, + Artifact: Artifact{ + Name: "duo-delegation-loop", + MediaType: "text/markdown", + SourceAsset: PortableSkillAsset, + OutputPath: PortableSkillFile, + ContentDigest: digest, + }, + Launchers: cloneLaunchers(portableLaunchers), + }, nil +} + +func cloneLaunchers(in []Launcher) []Launcher { + out := make([]Launcher, len(in)) + for i, launcher := range in { + out[i] = launcher + out[i].TestedVersions = cloneStrings(launcher.TestedVersions) + } + return out +} + func cloneStrings(s []string) []string { out := make([]string, len(s)) copy(out, s) diff --git a/internal/manifest/portable.go b/internal/manifest/portable.go new file mode 100644 index 0000000..ba55345 --- /dev/null +++ b/internal/manifest/portable.go @@ -0,0 +1,645 @@ +package manifest + +import ( + "bytes" + "crypto/rand" + "encoding/hex" + "encoding/json" + "errors" + "fmt" + "io" + "os" + "path" + "path/filepath" + "slices" + "strings" + "time" + + "github.com/procrastivity/duo/internal/asset" +) + +const ( + // PortableTargetName is the manifest and stamp identity for the shared + // Amp, OpenCode, and Codex filesystem-skill projection. + PortableTargetName = "portable_launchers" + // PortableProjectionRoot is the workspace-relative managed directory. + PortableProjectionRoot = ".agents/skills/duo-delegation-loop" + // PortableProjectionFormat versions the installed skill contract. + PortableProjectionFormat = "duo.skill/duo-delegation-loop/v1" + // PortableSkillAsset is the canonical logical shipped-asset path. + PortableSkillAsset = "skills/duo-delegation-loop/SKILL.md" + // PortableSkillFile is the payload path relative to the projection root. + PortableSkillFile = "SKILL.md" + // ProjectionStampFile is the ownership record beside the projected skill. + ProjectionStampFile = ".duo-generated.json" + projectionStampSchema = "duo.projection-stamp/v1" + portableVersionRange = "amp=0.0.1789675234-g2899fe;opencode=1.18.31;codex=0.154.0" +) + +var portableLaunchers = []Launcher{ + {Name: "amp", TestedVersions: []string{"0.0.1789675234-g2899fe"}}, + {Name: "opencode", TestedVersions: []string{"1.18.31"}}, + {Name: "codex", TestedVersions: []string{"0.154.0"}}, +} + +// ProjectionState is the closed portable projection inspection result. +type ProjectionState string + +const ( + // StateCurrent means the installed bytes and ownership stamp match the + // current binary's portable projection. + StateCurrent ProjectionState = "current" + // StateMissing means an expected projection or owned file is absent. + StateMissing ProjectionState = "missing" + // StateStale means intact owned output differs from the current manifest. + StateStale ProjectionState = "stale" + // StateModified means an owned file no longer matches its stamped digest. + StateModified ProjectionState = "modified" + // StateIncompatible means the ownership record or filesystem shape is unsupported. + StateIncompatible ProjectionState = "incompatible" + // StateUnownedConflict means an expected destination exists without valid ownership. + StateUnownedConflict ProjectionState = "unowned_conflict" +) + +// DigestedPath is one slash-separated path owned or consumed by a stamp. +type DigestedPath struct { + Path string `json:"path"` + Digest string `json:"digest"` +} + +// ProjectionTarget identifies the portable target and its evidence pins. +type ProjectionTarget struct { + Harness string `json:"harness"` + TestedVersionRange string `json:"tested_version_range"` + Launchers []Launcher `json:"launchers"` +} + +// UnmarshalJSON permits the target-level extension fields allowed by the v1 +// schema while Launcher itself keeps each launcher row closed. +func (t *ProjectionTarget) UnmarshalJSON(data []byte) error { + type targetAlias ProjectionTarget + var decoded targetAlias + if err := json.Unmarshal(data, &decoded); err != nil { + return err + } + *t = ProjectionTarget(decoded) + return nil +} + +// ProjectionStamp is the normative duo.projection-stamp/v1 ownership record. +type ProjectionStamp struct { + Schema string `json:"schema"` + ProductVersion string `json:"product_version"` + ManifestDigest string `json:"manifest_digest"` + ProjectionFormat string `json:"projection_format"` + Target ProjectionTarget `json:"target"` + Components []string `json:"components"` + InstallationID string `json:"installation_id"` + GeneratedAt string `json:"generated_at"` + SourceAssets []DigestedPath `json:"source_assets"` + Files []DigestedPath `json:"files"` +} + +// PortableInspection is a read-only projection diagnosis. +type PortableInspection struct { + Target string `json:"target"` + State ProjectionState `json:"state"` + Root string `json:"root"` + InstallationID string `json:"installation_id"` + FormatVersion string `json:"format_version"` + ContentDigest string `json:"content_digest"` + stamp *ProjectionStamp + rootPresent bool + stampPresent bool +} + +// InstallResult is the stable successful install result. +type InstallResult struct { + Target string `json:"target"` + State ProjectionState `json:"state"` + Changed bool `json:"changed"` + Root string `json:"root"` + InstallationID string `json:"installation_id"` + FormatVersion string `json:"format_version"` + ContentDigest string `json:"content_digest"` +} + +// ProjectionError carries the stable public code for an expected refusal. +type ProjectionError struct { + Code string + Message string +} + +func (e *ProjectionError) Error() string { return e.Message } + +type portableSpec struct { + manifest Manifest + target HarnessTarget + payload []byte +} + +func portableSpecFor(m Manifest) (portableSpec, error) { + if len(m.HarnessTargets) != 1 || m.HarnessTargets[0].Name != PortableTargetName { + return portableSpec{}, fmt.Errorf("manifest: portable launcher target is unavailable") + } + r, err := asset.ReadDefault(PortableSkillAsset) + if err != nil { + return portableSpec{}, err + } + t := m.HarnessTargets[0] + if got := "sha256:" + Checksum(r.Bytes()); got != t.Artifact.ContentDigest { + return portableSpec{}, fmt.Errorf("manifest: portable skill digest %s does not match target %s", got, t.Artifact.ContentDigest) + } + return portableSpec{manifest: m, target: t, payload: r.Bytes()}, nil +} + +// InspectPortableLaunchers classifies the selected workspace without writing. +func InspectPortableLaunchers(workspace string, m Manifest) (PortableInspection, error) { + spec, err := portableSpecFor(m) + if err != nil { + return PortableInspection{}, err + } + workspace, err = validateWorkspace(workspace) + if err != nil { + return PortableInspection{}, err + } + root := filepath.Join(workspace, filepath.FromSlash(PortableProjectionRoot)) + base := PortableInspection{ + Target: PortableTargetName, + State: StateMissing, + Root: root, + FormatVersion: PortableProjectionFormat, + ContentDigest: spec.target.Artifact.ContentDigest, + } + if conflict, err := managedParentConflict(workspace, filepath.Dir(root)); err != nil { + return PortableInspection{}, err + } else if conflict { + base.State = StateUnownedConflict + return base, nil + } + + rootInfo, err := os.Lstat(root) + if errors.Is(err, os.ErrNotExist) { + return base, nil + } + if err != nil { + return PortableInspection{}, fmt.Errorf("manifest: inspecting projection root: %w", err) + } + base.rootPresent = true + if rootInfo.Mode()&os.ModeSymlink != 0 || !rootInfo.IsDir() { + base.State = StateUnownedConflict + return base, nil + } + + stampPath := filepath.Join(root, ProjectionStampFile) + stampInfo, err := os.Lstat(stampPath) + if errors.Is(err, os.ErrNotExist) { + if exists, err := pathExistsNoFollow(filepath.Join(root, PortableSkillFile)); err != nil { + return PortableInspection{}, err + } else if exists { + base.State = StateUnownedConflict + } + return base, nil + } + if err != nil { + return PortableInspection{}, fmt.Errorf("manifest: inspecting projection stamp: %w", err) + } + base.stampPresent = true + if !stampInfo.Mode().IsRegular() { + base.State = StateIncompatible + return base, nil + } + + stamp, err := readProjectionStamp(stampPath) + if err != nil || validatePortableStamp(stamp) != nil { + base.State = StateIncompatible + return base, nil + } + base.stamp = &stamp + base.InstallationID = stamp.InstallationID + + claimed := make(map[string]bool, len(stamp.Files)) + missing := false + for _, file := range stamp.Files { + claimed[file.Path] = true + filePath := filepath.Join(root, filepath.FromSlash(file.Path)) + info, err := os.Lstat(filePath) + if errors.Is(err, os.ErrNotExist) { + missing = true + continue + } + if err != nil { + return PortableInspection{}, fmt.Errorf("manifest: inspecting owned file %q: %w", file.Path, err) + } + if !info.Mode().IsRegular() { + base.State = StateIncompatible + return base, nil + } + data, err := os.ReadFile(filePath) + if err != nil { + return PortableInspection{}, fmt.Errorf("manifest: reading owned file %q: %w", file.Path, err) + } + if "sha256:"+Checksum(data) != file.Digest { + base.State = StateModified + return base, nil + } + } + + expectedPath := filepath.Join(root, PortableSkillFile) + if !claimed[PortableSkillFile] { + if exists, err := pathExistsNoFollow(expectedPath); err != nil { + return PortableInspection{}, err + } else if exists { + base.State = StateUnownedConflict + return base, nil + } + missing = true + } + if missing { + base.State = StateMissing + return base, nil + } + + want := expectedStamp(spec, stamp.InstallationID, stamp.GeneratedAt) + if !stampCurrent(stamp, want) { + base.State = StateStale + return base, nil + } + base.State = StateCurrent + return base, nil +} + +// InstallPortableLaunchers performs the contract's non-destructive install or repair. +func InstallPortableLaunchers(workspace string, repair bool, m Manifest) (InstallResult, error) { + spec, err := portableSpecFor(m) + if err != nil { + return InstallResult{}, err + } + inspection, err := InspectPortableLaunchers(workspace, m) + if err != nil { + return InstallResult{}, err + } + if inspection.State == StateCurrent { + return resultFromInspection(inspection, false), nil + } + switch inspection.State { + case StateModified: + return InstallResult{}, &ProjectionError{Code: "projection.modified", Message: "The owned portable launcher skill was modified; move or restore it before retrying."} + case StateIncompatible: + return InstallResult{}, &ProjectionError{Code: "projection.incompatible", Message: "The portable launcher projection has an unknown or malformed ownership record; upgrade Duo or move it for manual review."} + case StateUnownedConflict: + return InstallResult{}, &ProjectionError{Code: "projection.user_file_conflict", Message: "Unowned content blocks the portable launcher projection; move or remove it explicitly before installing."} + case StateMissing: + if (inspection.stampPresent || (inspection.stamp != nil && inspection.rootPresent)) && !repair { + return InstallResult{}, &ProjectionError{Code: "invalid.precondition", Message: "The owned portable launcher projection is incomplete; inspect it and rerun with --repair."} + } + case StateStale: + if !repair { + return InstallResult{}, &ProjectionError{Code: "invalid.precondition", Message: "The owned portable launcher projection is stale; inspect it and rerun with --repair."} + } + } + + workspace, err = validateWorkspace(workspace) + if err != nil { + return InstallResult{}, err + } + root := inspection.Root + if err := ensureManagedParents(workspace, filepath.Dir(root)); err != nil { + return InstallResult{}, err + } + if !inspection.rootPresent { + if err := os.Mkdir(root, 0o755); err != nil { + return InstallResult{}, fmt.Errorf("manifest: creating projection root: %w", err) + } + } + + installationID := inspection.InstallationID + if installationID == "" { + installationID, err = newInstallationID() + if err != nil { + return InstallResult{}, err + } + } + stamp := expectedStamp(spec, installationID, time.Now().UTC().Format(time.RFC3339Nano)) + stampBytes, err := json.MarshalIndent(stamp, "", " ") + if err != nil { + return InstallResult{}, fmt.Errorf("manifest: encoding projection stamp: %w", err) + } + stampBytes = append(stampBytes, '\n') + + stage, err := os.MkdirTemp(filepath.Dir(root), ".duo-portable-launchers-") + if err != nil { + return InstallResult{}, fmt.Errorf("manifest: creating projection staging directory: %w", err) + } + defer func() { _ = os.RemoveAll(stage) }() + if err := writeSynced(filepath.Join(stage, PortableSkillFile), spec.payload); err != nil { + return InstallResult{}, err + } + if err := writeSynced(filepath.Join(stage, ProjectionStampFile), stampBytes); err != nil { + return InstallResult{}, err + } + + // Reinspect immediately before placement so a concurrent user edit cannot + // turn the earlier ownership proof into overwrite permission. + again, err := InspectPortableLaunchers(workspace, m) + if err != nil { + return InstallResult{}, err + } + if again.State != inspection.State || again.InstallationID != inspection.InstallationID { + return InstallResult{}, &ProjectionError{Code: "invalid.precondition", Message: "The portable launcher projection changed during installation; inspect it and retry."} + } + if err := os.Rename(filepath.Join(stage, PortableSkillFile), filepath.Join(root, PortableSkillFile)); err != nil { + return InstallResult{}, fmt.Errorf("manifest: placing portable skill: %w", err) + } + if inspection.stamp != nil { + for _, old := range inspection.stamp.Files { + if old.Path == PortableSkillFile { + continue + } + if err := os.Remove(filepath.Join(root, filepath.FromSlash(old.Path))); err != nil && !errors.Is(err, os.ErrNotExist) { + return InstallResult{}, fmt.Errorf("manifest: removing obsolete owned file %q: %w", old.Path, err) + } + } + } + // The ownership stamp is deliberately the final rename/commit marker. + if err := os.Rename(filepath.Join(stage, ProjectionStampFile), filepath.Join(root, ProjectionStampFile)); err != nil { + return InstallResult{}, fmt.Errorf("manifest: committing projection stamp: %w", err) + } + if err := syncDir(root); err != nil { + return InstallResult{}, err + } + return InstallResult{ + Target: PortableTargetName, State: StateCurrent, Changed: true, Root: root, + InstallationID: installationID, FormatVersion: PortableProjectionFormat, + ContentDigest: spec.target.Artifact.ContentDigest, + }, nil +} + +func resultFromInspection(i PortableInspection, changed bool) InstallResult { + return InstallResult{Target: i.Target, State: i.State, Changed: changed, Root: i.Root, InstallationID: i.InstallationID, FormatVersion: i.FormatVersion, ContentDigest: i.ContentDigest} +} + +func expectedStamp(spec portableSpec, installationID, generatedAt string) ProjectionStamp { + return ProjectionStamp{ + Schema: projectionStampSchema, ProductVersion: spec.manifest.Product.Version, + ManifestDigest: spec.manifest.ManifestDigest, ProjectionFormat: PortableProjectionFormat, + Target: ProjectionTarget{Harness: PortableTargetName, TestedVersionRange: portableVersionRange, Launchers: cloneLaunchers(portableLaunchers)}, + Components: []string{"filesystem_skill"}, InstallationID: installationID, GeneratedAt: generatedAt, + SourceAssets: []DigestedPath{{Path: PortableSkillAsset, Digest: spec.target.Artifact.ContentDigest}}, + Files: []DigestedPath{{Path: PortableSkillFile, Digest: spec.target.Artifact.ContentDigest}}, + } +} + +func stampCurrent(got, want ProjectionStamp) bool { + got.GeneratedAt, want.GeneratedAt = "", "" + return got.Schema == want.Schema && got.ProductVersion == want.ProductVersion && + got.ManifestDigest == want.ManifestDigest && got.ProjectionFormat == want.ProjectionFormat && + got.InstallationID == want.InstallationID && got.Target.Harness == want.Target.Harness && + got.Target.TestedVersionRange == want.Target.TestedVersionRange && + launchersEqual(got.Target.Launchers, want.Target.Launchers) && + slices.Equal(got.Components, want.Components) && slices.Equal(got.SourceAssets, want.SourceAssets) && + slices.Equal(got.Files, want.Files) +} + +func launchersEqual(a, b []Launcher) bool { + if len(a) != len(b) { + return false + } + for i := range a { + if a[i].Name != b[i].Name || !slices.Equal(a[i].TestedVersions, b[i].TestedVersions) { + return false + } + } + return true +} + +func readProjectionStamp(filename string) (ProjectionStamp, error) { + f, err := os.Open(filename) + if err != nil { + return ProjectionStamp{}, err + } + defer func() { _ = f.Close() }() + dec := json.NewDecoder(io.LimitReader(f, 1<<20)) + dec.DisallowUnknownFields() + var stamp ProjectionStamp + if err := dec.Decode(&stamp); err != nil { + return ProjectionStamp{}, err + } + if dec.Decode(&struct{}{}) != io.EOF { + return ProjectionStamp{}, fmt.Errorf("trailing JSON content") + } + return stamp, nil +} + +func validatePortableStamp(stamp ProjectionStamp) error { + if stamp.Schema != projectionStampSchema || stamp.ProjectionFormat != PortableProjectionFormat || + stamp.Target.Harness != PortableTargetName || stamp.ProductVersion == "" || stamp.InstallationID == "" || + stamp.GeneratedAt == "" || stamp.Target.TestedVersionRange == "" { + return fmt.Errorf("unsupported stamp identity") + } + if _, err := time.Parse(time.RFC3339, stamp.GeneratedAt); err != nil { + return fmt.Errorf("invalid generated_at") + } + if !validDigest(stamp.ManifestDigest) || !slices.Equal(stamp.Components, []string{"filesystem_skill"}) || len(stamp.Target.Launchers) == 0 { + return fmt.Errorf("invalid stamp metadata") + } + launcherNames := map[string]bool{} + for _, launcher := range stamp.Target.Launchers { + if launcher.Name == "" || launcherNames[launcher.Name] || len(launcher.TestedVersions) == 0 { + return fmt.Errorf("invalid launcher row") + } + launcherNames[launcher.Name] = true + versions := map[string]bool{} + for _, version := range launcher.TestedVersions { + if version == "" || versions[version] { + return fmt.Errorf("invalid launcher version") + } + versions[version] = true + } + } + if len(stamp.SourceAssets) == 0 || len(stamp.Files) == 0 { + return fmt.Errorf("empty stamp paths") + } + if err := validateDigestedPaths(stamp.SourceAssets, true); err != nil { + return err + } + if err := validateDigestedPaths(stamp.Files, true); err != nil { + return err + } + seen := map[string]bool{} + for _, item := range append(slices.Clone(stamp.SourceAssets), stamp.Files...) { + if seen[item.Path] { + return fmt.Errorf("duplicate stamp path %q", item.Path) + } + seen[item.Path] = true + } + return nil +} + +func validateDigestedPaths(paths []DigestedPath, rejectStamp bool) error { + seen := map[string]bool{} + for _, item := range paths { + if !safeSlashPath(item.Path) || !validDigest(item.Digest) || seen[item.Path] || (rejectStamp && item.Path == ProjectionStampFile) { + return fmt.Errorf("unsafe or duplicate stamp path %q", item.Path) + } + seen[item.Path] = true + } + return nil +} + +func safeSlashPath(p string) bool { + return p != "" && p == path.Clean(p) && !path.IsAbs(p) && p != "." && !strings.Contains(p, "\\") && + p != ".." && !strings.HasPrefix(p, "../") +} + +func validDigest(d string) bool { + if len(d) != len("sha256:")+64 || !strings.HasPrefix(d, "sha256:") { + return false + } + _, err := hex.DecodeString(strings.TrimPrefix(d, "sha256:")) + return err == nil && d == strings.ToLower(d) +} + +func validateWorkspace(workspace string) (string, error) { + if workspace == "" { + workspace = "." + } + abs, err := filepath.Abs(workspace) + if err != nil { + return "", fmt.Errorf("manifest: resolving workspace: %w", err) + } + abs = filepath.Clean(abs) + info, err := os.Lstat(abs) + if err != nil { + return "", &ProjectionError{Code: "invalid.precondition", Message: fmt.Sprintf("Workspace %q must be an existing non-symlink directory.", abs)} + } + if info.Mode()&os.ModeSymlink != 0 || !info.IsDir() { + return "", &ProjectionError{Code: "invalid.precondition", Message: fmt.Sprintf("Workspace %q must be an existing non-symlink directory.", abs)} + } + if err := rejectSymlinkAncestors(abs); err != nil { + return "", err + } + return abs, nil +} + +func rejectSymlinkAncestors(filename string) error { + volume := filepath.VolumeName(filename) + rest := strings.TrimPrefix(filename, volume) + current := volume + string(filepath.Separator) + for _, component := range strings.Split(strings.TrimPrefix(rest, string(filepath.Separator)), string(filepath.Separator)) { + if component == "" { + continue + } + current = filepath.Join(current, component) + info, err := os.Lstat(current) + if err != nil { + return fmt.Errorf("manifest: inspecting workspace ancestor %q: %w", current, err) + } + if info.Mode()&os.ModeSymlink != 0 { + return &ProjectionError{Code: "invalid.precondition", Message: fmt.Sprintf("Workspace ancestor %q must not be a symlink.", current)} + } + } + return nil +} + +func managedParentConflict(workspace, parent string) (bool, error) { + rel, err := filepath.Rel(workspace, parent) + if err != nil || rel == ".." || strings.HasPrefix(rel, ".."+string(filepath.Separator)) { + return false, &ProjectionError{Code: "invalid.precondition", Message: "The managed projection path escapes the selected workspace."} + } + current := workspace + for _, component := range strings.Split(rel, string(filepath.Separator)) { + if component == "." || component == "" { + continue + } + current = filepath.Join(current, component) + info, err := os.Lstat(current) + if errors.Is(err, os.ErrNotExist) { + return false, nil + } + if err != nil { + return false, fmt.Errorf("manifest: inspecting managed parent %q: %w", current, err) + } + if info.Mode()&os.ModeSymlink != 0 || !info.IsDir() { + return true, nil + } + } + return false, nil +} + +func ensureManagedParents(workspace, parent string) error { + rel, err := filepath.Rel(workspace, parent) + if err != nil || rel == ".." || strings.HasPrefix(rel, ".."+string(filepath.Separator)) { + return &ProjectionError{Code: "invalid.precondition", Message: "The managed projection path escapes the selected workspace."} + } + current := workspace + for _, component := range strings.Split(rel, string(filepath.Separator)) { + if component == "." || component == "" { + continue + } + current = filepath.Join(current, component) + info, err := os.Lstat(current) + if errors.Is(err, os.ErrNotExist) { + if err := os.Mkdir(current, 0o755); err != nil { + return fmt.Errorf("manifest: creating managed parent %q: %w", current, err) + } + continue + } + if err != nil { + return fmt.Errorf("manifest: inspecting managed parent %q: %w", current, err) + } + if info.Mode()&os.ModeSymlink != 0 || !info.IsDir() { + return &ProjectionError{Code: "projection.user_file_conflict", Message: fmt.Sprintf("Managed destination ancestor %q is not a real directory.", current)} + } + } + return nil +} + +func pathExistsNoFollow(filename string) (bool, error) { + _, err := os.Lstat(filename) + if errors.Is(err, os.ErrNotExist) { + return false, nil + } + if err != nil { + return false, fmt.Errorf("manifest: inspecting %q: %w", filename, err) + } + return true, nil +} + +func newInstallationID() (string, error) { + b := make([]byte, 16) + if _, err := rand.Read(b); err != nil { + return "", fmt.Errorf("manifest: minting installation id: %w", err) + } + return hex.EncodeToString(b), nil +} + +func writeSynced(filename string, data []byte) error { + f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o644) + if err != nil { + return fmt.Errorf("manifest: staging %q: %w", filepath.Base(filename), err) + } + if _, err = io.Copy(f, bytes.NewReader(data)); err == nil { + err = f.Sync() + } + closeErr := f.Close() + if err != nil { + return fmt.Errorf("manifest: staging %q: %w", filepath.Base(filename), err) + } + if closeErr != nil { + return fmt.Errorf("manifest: closing staged %q: %w", filepath.Base(filename), closeErr) + } + return nil +} + +func syncDir(dir string) error { + f, err := os.Open(dir) + if err != nil { + return fmt.Errorf("manifest: opening projection directory for sync: %w", err) + } + defer func() { _ = f.Close() }() + if err := f.Sync(); err != nil { + return fmt.Errorf("manifest: syncing projection directory: %w", err) + } + return nil +} diff --git a/internal/manifest/portable_test.go b/internal/manifest/portable_test.go new file mode 100644 index 0000000..79a6f97 --- /dev/null +++ b/internal/manifest/portable_test.go @@ -0,0 +1,449 @@ +package manifest + +import ( + "bytes" + "crypto/sha256" + "encoding/hex" + "encoding/json" + "errors" + "os" + "path/filepath" + "reflect" + "testing" + + "github.com/spf13/cobra" + + "github.com/procrastivity/duo/internal/buildinfo" + "github.com/procrastivity/duo/internal/surface" +) + +func portableTestManifest(t *testing.T) Manifest { + t.Helper() + root := &cobra.Command{Use: "duo"} + verb := &cobra.Command{Use: "noop", RunE: func(*cobra.Command, []string) error { return nil }} + surface.Annotate(verb, surface.Plumbing) + root.AddCommand(verb) + m, err := Build(root, buildinfo.Info{Version: "v-test", Commit: "abc", Date: "2026-09-17T00:00:00Z"}) + if err != nil { + t.Fatal(err) + } + return m +} + +func portableRoot(workspace string) string { + return filepath.Join(workspace, filepath.FromSlash(PortableProjectionRoot)) +} + +func loadStamp(t *testing.T, workspace string) ProjectionStamp { + t.Helper() + b, err := os.ReadFile(filepath.Join(portableRoot(workspace), ProjectionStampFile)) + if err != nil { + t.Fatal(err) + } + var stamp ProjectionStamp + if err := json.Unmarshal(b, &stamp); err != nil { + t.Fatal(err) + } + return stamp +} + +func saveStamp(t *testing.T, workspace string, stamp ProjectionStamp) { + t.Helper() + b, err := json.MarshalIndent(stamp, "", " ") + if err != nil { + t.Fatal(err) + } + b = append(b, '\n') + if err := os.WriteFile(filepath.Join(portableRoot(workspace), ProjectionStampFile), b, 0o644); err != nil { + t.Fatal(err) + } +} + +func inspectState(t *testing.T, workspace string, m Manifest) ProjectionState { + t.Helper() + i, err := InspectPortableLaunchers(workspace, m) + if err != nil { + t.Fatal(err) + } + return i.State +} + +func projectionErrorCode(t *testing.T, err error) string { + t.Helper() + var pe *ProjectionError + if !errors.As(err, &pe) { + t.Fatalf("error = %v, want ProjectionError", err) + } + return pe.Code +} + +func TestPortableManifestTargetAndAssetIdentity(t *testing.T) { + m := portableTestManifest(t) + if len(m.HarnessTargets) != 1 { + t.Fatalf("targets = %d, want 1", len(m.HarnessTargets)) + } + target := m.HarnessTargets[0] + if target.Name != "portable_launchers" || target.Status != "unverified" || target.Scope != "workspace" { + t.Fatalf("target identity = %#v", target) + } + if target.DiscoveryRoot != ".agents/skills" || target.ProjectionRoot != PortableProjectionRoot || target.StampFile != ProjectionStampFile { + t.Fatalf("target paths = %#v", target) + } + if !reflect.DeepEqual(target.Components, []string{"filesystem_skill"}) { + t.Fatalf("components = %v", target.Components) + } + wantLaunchers := []Launcher{ + {Name: "amp", TestedVersions: []string{"0.0.1789675234-g2899fe"}}, + {Name: "opencode", TestedVersions: []string{"1.18.31"}}, + {Name: "codex", TestedVersions: []string{"0.154.0"}}, + } + if !reflect.DeepEqual(target.Launchers, wantLaunchers) { + t.Fatalf("launchers = %#v", target.Launchers) + } + authored, err := os.ReadFile(filepath.Join("..", "..", "skills", "duo-delegation-loop", "SKILL.md")) + if err != nil { + t.Fatal(err) + } + sum := sha256.Sum256(authored) + wantDigest := "sha256:" + hex.EncodeToString(sum[:]) + if target.Artifact != (Artifact{Name: "duo-delegation-loop", MediaType: "text/markdown", SourceAsset: PortableSkillAsset, OutputPath: PortableSkillFile, ContentDigest: wantDigest}) { + t.Fatalf("artifact = %#v", target.Artifact) + } + found := false + for _, a := range m.Assets { + if a.Path == PortableSkillAsset { + found = true + if a.SHA256 != hex.EncodeToString(sum[:]) { + t.Fatalf("asset digest = %s, want %x", a.SHA256, sum) + } + } + } + if !found { + t.Fatal("canonical skill is absent from assets inventory") + } + second := portableTestManifest(t) + if second.ManifestDigest != m.ManifestDigest { + t.Fatalf("manifest digest changed: %s != %s", second.ManifestDigest, m.ManifestDigest) + } + withoutDigest := m + withoutDigest.ManifestDigest = "" + encoded, err := json.Marshal(withoutDigest) + if err != nil { + t.Fatal(err) + } + manifestSum := sha256.Sum256(encoded) + if want := "sha256:" + hex.EncodeToString(manifestSum[:]); m.ManifestDigest != want { + t.Fatalf("manifest digest = %s, independent digest = %s", m.ManifestDigest, want) + } +} + +func TestPortableFreshCurrentIdempotentAndByteIdentical(t *testing.T) { + workspace := t.TempDir() + overrideRoot := t.TempDir() + t.Setenv("XDG_CONFIG_HOME", overrideRoot) + override := filepath.Join(overrideRoot, "duo", filepath.FromSlash(PortableSkillAsset)) + if err := os.MkdirAll(filepath.Dir(override), 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(override, []byte("must not be installed\n"), 0o644); err != nil { + t.Fatal(err) + } + m := portableTestManifest(t) + if got := inspectState(t, workspace, m); got != StateMissing { + t.Fatalf("fresh state = %s", got) + } + first, err := InstallPortableLaunchers(workspace, false, m) + if err != nil { + t.Fatal(err) + } + if !first.Changed || first.State != StateCurrent || first.InstallationID == "" { + t.Fatalf("first result = %#v", first) + } + authored, _ := os.ReadFile(filepath.Join("..", "..", "skills", "duo-delegation-loop", "SKILL.md")) + rendered, _ := os.ReadFile(filepath.Join(portableRoot(workspace), PortableSkillFile)) + if !bytes.Equal(authored, rendered) { + t.Fatal("rendered skill differs from authored bytes") + } + stampBefore, _ := os.ReadFile(filepath.Join(portableRoot(workspace), ProjectionStampFile)) + skillInfo, _ := os.Stat(filepath.Join(portableRoot(workspace), PortableSkillFile)) + stampInfo, _ := os.Stat(filepath.Join(portableRoot(workspace), ProjectionStampFile)) + if stampInfo.ModTime().Before(skillInfo.ModTime()) { + t.Fatalf("stamp mtime %s precedes payload mtime %s; stamp was not staged last", stampInfo.ModTime(), skillInfo.ModTime()) + } + second, err := InstallPortableLaunchers(workspace, false, m) + if err != nil { + t.Fatal(err) + } + stampAfter, _ := os.ReadFile(filepath.Join(portableRoot(workspace), ProjectionStampFile)) + if second.Changed || second.InstallationID != first.InstallationID || !bytes.Equal(stampBefore, stampAfter) { + t.Fatalf("current install was not an exact no-op: %#v", second) + } +} + +func TestPortableStatesPrecedenceAndNoWriteConflicts(t *testing.T) { + m := portableTestManifest(t) + + t.Run("modified", func(t *testing.T) { + workspace := t.TempDir() + _, _ = InstallPortableLaunchers(workspace, false, m) + file := filepath.Join(portableRoot(workspace), PortableSkillFile) + if err := os.WriteFile(file, []byte("user edit\n"), 0o644); err != nil { + t.Fatal(err) + } + before, _ := os.ReadFile(file) + if got := inspectState(t, workspace, m); got != StateModified { + t.Fatalf("state = %s", got) + } + _, err := InstallPortableLaunchers(workspace, true, m) + if code := projectionErrorCode(t, err); code != "projection.modified" { + t.Fatalf("code = %s", code) + } + after, _ := os.ReadFile(file) + if !bytes.Equal(before, after) { + t.Fatal("modified file was overwritten") + } + }) + + t.Run("incompatible_precedes_modified", func(t *testing.T) { + workspace := t.TempDir() + _, _ = InstallPortableLaunchers(workspace, false, m) + file := filepath.Join(portableRoot(workspace), PortableSkillFile) + _ = os.WriteFile(file, []byte("user edit\n"), 0o644) + stampPath := filepath.Join(portableRoot(workspace), ProjectionStampFile) + _ = os.WriteFile(stampPath, []byte("not-json\n"), 0o644) + if got := inspectState(t, workspace, m); got != StateIncompatible { + t.Fatalf("state = %s", got) + } + before, _ := os.ReadFile(file) + _, err := InstallPortableLaunchers(workspace, true, m) + if code := projectionErrorCode(t, err); code != "projection.incompatible" { + t.Fatalf("code = %s", code) + } + after, _ := os.ReadFile(file) + if !bytes.Equal(before, after) { + t.Fatal("incompatible projection was changed") + } + }) + + t.Run("unowned_file", func(t *testing.T) { + workspace := t.TempDir() + root := portableRoot(workspace) + _ = os.MkdirAll(root, 0o755) + file := filepath.Join(root, PortableSkillFile) + _ = os.WriteFile(file, []byte("user file\n"), 0o644) + if got := inspectState(t, workspace, m); got != StateUnownedConflict { + t.Fatalf("state = %s", got) + } + _, err := InstallPortableLaunchers(workspace, false, m) + if code := projectionErrorCode(t, err); code != "projection.user_file_conflict" { + t.Fatalf("code = %s", code) + } + got, _ := os.ReadFile(file) + if string(got) != "user file\n" { + t.Fatal("unowned file was changed") + } + }) + + t.Run("destination_directory_symlink", func(t *testing.T) { + workspace := t.TempDir() + outside := t.TempDir() + parent := filepath.Dir(portableRoot(workspace)) + _ = os.MkdirAll(parent, 0o755) + if err := os.Symlink(outside, portableRoot(workspace)); err != nil { + t.Fatal(err) + } + if got := inspectState(t, workspace, m); got != StateUnownedConflict { + t.Fatalf("state = %s", got) + } + _, err := InstallPortableLaunchers(workspace, true, m) + if code := projectionErrorCode(t, err); code != "projection.user_file_conflict" { + t.Fatalf("code = %s", code) + } + entries, _ := os.ReadDir(outside) + if len(entries) != 0 { + t.Fatal("installer followed destination symlink") + } + }) + + t.Run("destination_ancestor_symlink", func(t *testing.T) { + workspace := t.TempDir() + outside := t.TempDir() + if err := os.Symlink(outside, filepath.Join(workspace, ".agents")); err != nil { + t.Fatal(err) + } + if got := inspectState(t, workspace, m); got != StateUnownedConflict { + t.Fatalf("state = %s", got) + } + _, err := InstallPortableLaunchers(workspace, true, m) + if code := projectionErrorCode(t, err); code != "projection.user_file_conflict" { + t.Fatalf("code = %s", code) + } + entries, _ := os.ReadDir(outside) + if len(entries) != 0 { + t.Fatal("installer followed managed ancestor symlink") + } + }) +} + +func TestPortableStatePrecedence(t *testing.T) { + m := portableTestManifest(t) + + t.Run("modified_before_unowned", func(t *testing.T) { + workspace := t.TempDir() + _, _ = InstallPortableLaunchers(workspace, false, m) + stamp := loadStamp(t, workspace) + stamp.Files = []DigestedPath{{Path: "OLD.txt", Digest: "sha256:" + string(bytes.Repeat([]byte("a"), 64))}} + saveStamp(t, workspace, stamp) + if err := os.WriteFile(filepath.Join(portableRoot(workspace), "OLD.txt"), []byte("modified\n"), 0o644); err != nil { + t.Fatal(err) + } + if got := inspectState(t, workspace, m); got != StateModified { + t.Fatalf("state = %s, want modified before unowned_conflict", got) + } + }) + + t.Run("unowned_before_missing", func(t *testing.T) { + workspace := t.TempDir() + _, _ = InstallPortableLaunchers(workspace, false, m) + stamp := loadStamp(t, workspace) + stamp.Files = []DigestedPath{{Path: "ABSENT.txt", Digest: "sha256:" + string(bytes.Repeat([]byte("a"), 64))}} + saveStamp(t, workspace, stamp) + if got := inspectState(t, workspace, m); got != StateUnownedConflict { + t.Fatalf("state = %s, want unowned_conflict before missing", got) + } + }) + + t.Run("missing_before_stale", func(t *testing.T) { + workspace := t.TempDir() + _, _ = InstallPortableLaunchers(workspace, false, m) + stamp := loadStamp(t, workspace) + stamp.ManifestDigest = "sha256:" + string(bytes.Repeat([]byte("a"), 64)) + saveStamp(t, workspace, stamp) + if err := os.Remove(filepath.Join(portableRoot(workspace), PortableSkillFile)); err != nil { + t.Fatal(err) + } + if got := inspectState(t, workspace, m); got != StateMissing { + t.Fatalf("state = %s, want missing before stale", got) + } + }) +} + +func TestPortablePlainVersusRepairAndPreservation(t *testing.T) { + workspace := t.TempDir() + m := portableTestManifest(t) + first, err := InstallPortableLaunchers(workspace, false, m) + if err != nil { + t.Fatal(err) + } + root := portableRoot(workspace) + unlisted := filepath.Join(root, "NOTES.txt") + if err := os.WriteFile(unlisted, []byte("keep me\n"), 0o644); err != nil { + t.Fatal(err) + } + if err := os.Remove(filepath.Join(root, PortableSkillFile)); err != nil { + t.Fatal(err) + } + stampBefore, _ := os.ReadFile(filepath.Join(root, ProjectionStampFile)) + if got := inspectState(t, workspace, m); got != StateMissing { + t.Fatalf("missing-owned state = %s", got) + } + _, err = InstallPortableLaunchers(workspace, false, m) + if code := projectionErrorCode(t, err); code != "invalid.precondition" { + t.Fatalf("plain missing code = %s", code) + } + stampAfterPlain, _ := os.ReadFile(filepath.Join(root, ProjectionStampFile)) + if !bytes.Equal(stampBefore, stampAfterPlain) { + t.Fatal("plain install changed partial owned output") + } + repaired, err := InstallPortableLaunchers(workspace, true, m) + if err != nil { + t.Fatal(err) + } + if repaired.InstallationID != first.InstallationID || !repaired.Changed { + t.Fatalf("missing repair result = %#v", repaired) + } + if got, _ := os.ReadFile(unlisted); string(got) != "keep me\n" { + t.Fatal("repair removed or changed an unlisted file") + } + + stamp := loadStamp(t, workspace) + obsoleteData := []byte("old generated file\n") + obsoleteSum := sha256.Sum256(obsoleteData) + obsolete := filepath.Join(root, "OLD.txt") + if err := os.WriteFile(obsolete, obsoleteData, 0o644); err != nil { + t.Fatal(err) + } + stamp.Files = append(stamp.Files, DigestedPath{Path: "OLD.txt", Digest: "sha256:" + hex.EncodeToString(obsoleteSum[:])}) + saveStamp(t, workspace, stamp) + if got := inspectState(t, workspace, m); got != StateStale { + t.Fatalf("obsolete-file state = %s", got) + } + if _, err := InstallPortableLaunchers(workspace, true, m); err != nil { + t.Fatal(err) + } + if _, err := os.Lstat(obsolete); !errors.Is(err, os.ErrNotExist) { + t.Fatalf("obsolete owned file remains: %v", err) + } + if got, _ := os.ReadFile(unlisted); string(got) != "keep me\n" { + t.Fatal("obsolete-file repair removed an unlisted file") + } + + stamp = loadStamp(t, workspace) + stamp.ManifestDigest = "sha256:" + string(bytes.Repeat([]byte("a"), 64)) + saveStamp(t, workspace, stamp) + if got := inspectState(t, workspace, m); got != StateStale { + t.Fatalf("stale state = %s", got) + } + staleStamp := loadStamp(t, workspace) + _, err = InstallPortableLaunchers(workspace, false, m) + if code := projectionErrorCode(t, err); code != "invalid.precondition" { + t.Fatalf("plain stale code = %s", code) + } + repaired, err = InstallPortableLaunchers(workspace, true, m) + if err != nil { + t.Fatal(err) + } + if repaired.InstallationID != staleStamp.InstallationID || inspectState(t, workspace, m) != StateCurrent { + t.Fatalf("stale repair did not preserve installation id: %#v", repaired) + } +} + +func TestPortableRejectsUnsafeDuplicateAndSymlinkStampedPaths(t *testing.T) { + m := portableTestManifest(t) + tests := []struct { + name string + mutate func(*ProjectionStamp) + }{ + {name: "unsafe", mutate: func(s *ProjectionStamp) { s.Files[0].Path = "../escape" }}, + {name: "duplicate", mutate: func(s *ProjectionStamp) { s.Files = append(s.Files, s.Files[0]) }}, + {name: "stamp_claim", mutate: func(s *ProjectionStamp) { s.Files[0].Path = ProjectionStampFile }}, + {name: "source_stamp_claim", mutate: func(s *ProjectionStamp) { s.SourceAssets[0].Path = ProjectionStampFile }}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + workspace := t.TempDir() + _, _ = InstallPortableLaunchers(workspace, false, m) + stamp := loadStamp(t, workspace) + tt.mutate(&stamp) + saveStamp(t, workspace, stamp) + if got := inspectState(t, workspace, m); got != StateIncompatible { + t.Fatalf("state = %s", got) + } + }) + } + + t.Run("stamped_payload_symlink", func(t *testing.T) { + workspace := t.TempDir() + _, _ = InstallPortableLaunchers(workspace, false, m) + file := filepath.Join(portableRoot(workspace), PortableSkillFile) + if err := os.Remove(file); err != nil { + t.Fatal(err) + } + if err := os.Symlink(filepath.Join("..", "elsewhere"), file); err != nil { + t.Fatal(err) + } + if got := inspectState(t, workspace, m); got != StateIncompatible { + t.Fatalf("state = %s", got) + } + }) +} diff --git a/internal/registry/portable_install_test.go b/internal/registry/portable_install_test.go new file mode 100644 index 0000000..2deafc8 --- /dev/null +++ b/internal/registry/portable_install_test.go @@ -0,0 +1,29 @@ +package registry_test + +import ( + "slices" + "testing" + + "github.com/procrastivity/duo/internal/registry" +) + +func TestPortableInstallDescriptor(t *testing.T) { + d, ok := registry.Lookup("projection.install") + if !ok { + t.Fatal("projection.install is not registered") + } + if d.Projectability != registry.LocalAdmin || !slices.Equal(d.CLI, []string{"install", "portable-launchers"}) { + t.Fatalf("descriptor = %#v", d) + } + if !slices.Equal(d.Permissions, []string{"harness.manage"}) || d.MCPTool != "" || d.Route != nil { + t.Fatalf("descriptor projections = %#v", d) + } + if d.Milestone { + t.Fatal("projection.install must not expand the sealed dogfood-milestone operation set") + } + for _, code := range []string{"invalid.precondition", "projection.modified", "projection.incompatible", "projection.user_file_conflict"} { + if !slices.Contains(d.ErrorCodes, code) { + t.Errorf("descriptor missing %s", code) + } + } +} diff --git a/internal/registry/table.go b/internal/registry/table.go index c6dee7d..9e5747d 100644 --- a/internal/registry/table.go +++ b/internal/registry/table.go @@ -162,6 +162,24 @@ var table = []Descriptor{ Route: nil, Milestone: true, }, + { + Name: "projection.install", + Projectability: LocalAdmin, + RequestSchema: externalV1, + ResultSchema: externalV1, + Permissions: []string{"harness.manage"}, + Idempotency: IdempotencyNotApplicable, + Audit: AuditNone, + CLI: []string{"install", "portable-launchers"}, + MCPTool: "", + Route: nil, + ErrorCodes: []string{ + "invalid.precondition", + "projection.modified", + "projection.incompatible", + "projection.user_file_conflict", + }, + }, { // "config.migrate" is an authored operation name for the // duo-vnext-installation-contract.md §1.3 `duo config migrate` @@ -498,6 +516,7 @@ var stableErrorCodes = map[string]ErrorClass{ "operation.unsupported": "unsupported", "operation.realization_unsupported": "unsupported", "projection.component_unverified": "unsupported", + "projection.incompatible": "unsupported", "lease.unsupported_topology": "unsupported", "operation.temporarily_unavailable": "unavailable", diff --git a/internal/store/lease.go b/internal/store/lease.go index f665f63..a4bffe1 100644 --- a/internal/store/lease.go +++ b/internal/store/lease.go @@ -42,6 +42,38 @@ func (e *WriterActiveError) Error() string { e.Incarnation, e.PID, e.Hostname, e.ExpiresAt) } +// WriterLease is a read-only snapshot of the singleton authority-writer +// lease. Active means the row exists and its lease has not expired at the +// supplied observation time. InspectWriterLease never performs takeover or +// process-liveness probing: diagnosis observes the durable lease exactly as +// written and leaves normal writer acquisition to decide whether a local +// holder is provably dead. +type WriterLease struct { + Active bool + Incarnation string + PID int + Hostname string + ExpiresAt string +} + +// InspectWriterLease reads the current writer lease through this handle. +// It is safe on an OpenReadOnly handle and performs no writes, including no +// transient lease acquisition. +func (s *Store) InspectWriterLease(ctx context.Context, now time.Time) (WriterLease, error) { + var lease WriterLease + err := s.db.QueryRowContext(ctx, + `SELECT incarnation, pid, hostname, expires_at FROM writer_lease WHERE id = 1`, + ).Scan(&lease.Incarnation, &lease.PID, &lease.Hostname, &lease.ExpiresAt) + switch { + case errors.Is(err, sql.ErrNoRows): + return WriterLease{}, nil + case err != nil: + return WriterLease{}, fmt.Errorf("store: inspecting writer lease: %w", err) + } + lease.Active = lease.ExpiresAt > timestamp(now) + return lease, nil +} + // OpenAuthority opens the store at path and acquires the exclusive // authority-writer lease, minting a fresh incarnation ID. When a live writer // already holds the lease it fails with *WriterActiveError. See diff --git a/internal/store/readonly.go b/internal/store/readonly.go new file mode 100644 index 0000000..379e001 --- /dev/null +++ b/internal/store/readonly.go @@ -0,0 +1,55 @@ +package store + +import ( + "context" + "database/sql" + "fmt" + "net/url" + "path/filepath" +) + +// OpenReadOnly opens an existing authority database without acquiring the +// writer lease or changing the database or its filesystem. The SQLite +// mode=ro URI is the physical enforcement boundary; query_only is a second +// guard against accidental writes through this connection. immutable is +// deliberately not used, because this reader must observe commits made by a +// concurrent WAL writer. +func OpenReadOnly(path string) (*Store, error) { + abs, err := filepath.Abs(path) + if err != nil { + return nil, fmt.Errorf("store: resolving read-only path %s: %w", path, err) + } + u := url.URL{Scheme: "file", Path: abs} + query := u.Query() + query.Set("mode", "ro") + query.Add("_pragma", "query_only(1)") + u.RawQuery = query.Encode() + + db, err := sql.Open("sqlite", u.String()) + if err != nil { + return nil, fmt.Errorf("store: open read-only %s: %w", path, err) + } + db.SetMaxOpenConns(1) + closeOnError := func(err error) (*Store, error) { + _ = db.Close() + return nil, err + } + + ctx := context.Background() + if err := db.PingContext(ctx); err != nil { + return closeOnError(fmt.Errorf("store: connect read-only %s: %w", path, err)) + } + version, err := schemaVersion(ctx, db) + if err != nil { + return closeOnError(err) + } + want := latestVersion(register) + if version != want { + return closeOnError(fmt.Errorf( + "store: database is at unsupported schema v%d; this duo read-only projection requires v%d", + version, want, + )) + } + + return &Store{db: db, path: path, version: version}, nil +} diff --git a/internal/store/readonly_test.go b/internal/store/readonly_test.go new file mode 100644 index 0000000..99ff92a --- /dev/null +++ b/internal/store/readonly_test.go @@ -0,0 +1,183 @@ +package store + +import ( + "context" + "crypto/sha256" + "errors" + "os" + "path/filepath" + "testing" + "time" +) + +func TestOpenReadOnlyDoesNotCreateMissingDatabaseOrParent(t *testing.T) { + root := t.TempDir() + parent := filepath.Join(root, "missing") + path := filepath.Join(parent, "duo.db") + + if _, err := OpenReadOnly(path); err == nil { + t.Fatal("OpenReadOnly(missing) succeeded") + } + if _, err := os.Stat(parent); !errors.Is(err, os.ErrNotExist) { + t.Fatalf("missing parent changed: stat error = %v", err) + } + entries, err := os.ReadDir(root) + if err != nil { + t.Fatalf("ReadDir: %v", err) + } + if len(entries) != 0 { + t.Fatalf("OpenReadOnly created filesystem entries: %v", entryNames(entries)) + } +} + +func TestOpenReadOnlyIsQueryOnlyAndDoesNotMutateDatabase(t *testing.T) { + dir := t.TempDir() + path := filepath.Join(dir, "duo.db") + writable, err := Open(path) + if err != nil { + t.Fatalf("Open: %v", err) + } + if err := writable.Close(); err != nil { + t.Fatalf("Close writable: %v", err) + } + before := databaseSnapshot(t, path) + + reader, err := OpenReadOnly(path) + if err != nil { + t.Fatalf("OpenReadOnly: %v", err) + } + var queryOnly int + if err := reader.db.QueryRowContext(context.Background(), `PRAGMA query_only`).Scan(&queryOnly); err != nil { + t.Fatalf("PRAGMA query_only: %v", err) + } + if queryOnly != 1 { + t.Fatalf("PRAGMA query_only = %d, want 1", queryOnly) + } + if _, err := reader.db.ExecContext(context.Background(), `CREATE TABLE forbidden (id INTEGER)`); err == nil { + t.Fatal("write through read-only database handle succeeded") + } + if err := reader.Close(); err != nil { + t.Fatalf("Close reader: %v", err) + } + after := databaseSnapshot(t, path) + if after != before { + t.Fatalf("read-only open mutated database:\nbefore: %+v\nafter: %+v", before, after) + } +} + +func TestOpenReadOnlySeesConcurrentWALCommits(t *testing.T) { + path := filepath.Join(t.TempDir(), "duo.db") + writer, err := Open(path) + if err != nil { + t.Fatalf("Open writer: %v", err) + } + defer func() { _ = writer.Close() }() + reader, err := OpenReadOnly(path) + if err != nil { + t.Fatalf("OpenReadOnly: %v", err) + } + defer func() { _ = reader.Close() }() + + ctx := context.Background() + items, err := reader.ReadStream(ctx, "concurrent", 0, 10) + if err != nil { + t.Fatalf("initial ReadStream: %v", err) + } + if len(items) != 0 { + t.Fatalf("initial stream has %d items", len(items)) + } + if _, err := writer.db.ExecContext(ctx, ` + INSERT INTO stream_log (stream, seq, item_id, payload, recorded_at) + VALUES ('concurrent', 1, 'item-1', 'committed', '2026-09-18T00:00:00.000Z')`); err != nil { + t.Fatalf("concurrent writer commit: %v", err) + } + items, err = reader.ReadStream(ctx, "concurrent", 0, 10) + if err != nil { + t.Fatalf("ReadStream after commit: %v", err) + } + if len(items) != 1 || items[0].Payload != "committed" { + t.Fatalf("reader did not see committed WAL item: %+v", items) + } +} + +func TestOpenReadOnlyRejectsUnsupportedSchema(t *testing.T) { + path := filepath.Join(t.TempDir(), "duo.db") + writable, err := Open(path) + if err != nil { + t.Fatalf("Open: %v", err) + } + if _, err := writable.db.ExecContext(context.Background(), ` + INSERT INTO schema_migrations (version, name, applied_at) + VALUES (?, 'future', '2026-09-18T00:00:00.000Z')`, latestVersion(register)+1); err != nil { + t.Fatalf("install future schema marker: %v", err) + } + if err := writable.Close(); err != nil { + t.Fatalf("Close: %v", err) + } + + if _, err := OpenReadOnly(path); err == nil { + t.Fatal("OpenReadOnly accepted a future schema") + } +} + +func TestOpenReadOnlyInspectsWriterLeaseWithoutTakingOne(t *testing.T) { + path := filepath.Join(t.TempDir(), "duo.db") + writable, err := Open(path) + if err != nil { + t.Fatalf("Open: %v", err) + } + if err := writable.Close(); err != nil { + t.Fatalf("Close writable: %v", err) + } + before := databaseSnapshot(t, path) + + reader, err := OpenReadOnly(path) + if err != nil { + t.Fatalf("OpenReadOnly: %v", err) + } + lease, err := reader.InspectWriterLease(context.Background(), time.Now()) + if err != nil { + t.Fatalf("InspectWriterLease: %v", err) + } + if lease.Active || lease.Incarnation != "" { + t.Fatalf("lease = %+v, want no active or transient writer", lease) + } + if err := reader.Close(); err != nil { + t.Fatalf("Close reader: %v", err) + } + after := databaseSnapshot(t, path) + if after != before { + t.Fatalf("lease inspection mutated database:\nbefore: %+v\nafter: %+v", before, after) + } +} + +type fileState struct { + Mode os.FileMode + Size int64 + ModTime int64 + Digest [sha256.Size]byte +} + +func databaseSnapshot(t *testing.T, path string) fileState { + t.Helper() + info, err := os.Stat(path) + if err != nil { + t.Fatalf("Stat(%s): %v", path, err) + } + contents, err := os.ReadFile(path) + if err != nil { + t.Fatalf("ReadFile(%s): %v", path, err) + } + return fileState{ + Mode: info.Mode(), Size: info.Size(), ModTime: info.ModTime().UnixNano(), + Digest: sha256.Sum256(contents), + } +} + +func entryNames(entries []os.DirEntry) []string { + names := make([]string, len(entries)) + for i, entry := range entries { + names[i] = entry.Name() + } + return names +} diff --git a/skills/duo-delegation-loop/SKILL.md b/skills/duo-delegation-loop/SKILL.md index e3a9fd0..ba28dc9 100644 --- a/skills/duo-delegation-loop/SKILL.md +++ b/skills/duo-delegation-loop/SKILL.md @@ -85,7 +85,7 @@ Extract condition and assistant text with: ``` jq -r '.result.condition.value // "absent"' -jq -r '.result.items[]? | select(.author_role=="assistant") | .blocks[0].content.text' +jq -r '.result.items[]? | select(.author_role=="agent") | .blocks[0].content.text' ``` Do not print `result["condition"]` — that is an object; a `case` on it never matches `idle`. @@ -116,14 +116,20 @@ Optional `--after` pages `duo conversation list` from `next_page`. Optional `--l - Stop, interrupt, or terminal paste - Composer-lease verbs - MCP tools and presentation routes -- Stage 5 generated projection +- Launcher-specific hooks, plugins, and runtime adapters - `duo wait`, `--block-for` (parked; notes/54) -## Install by hand +## Installation -This file is the normative source. Install it by hand into the orchestrating harness. There is no Stage 5 renderer in this milestone. +This file is the normative source. For Amp, OpenCode, and Codex project discovery, install the product-owned copied projection with: -Do not run these installs as part of authoring this skill. +``` +duo install portable-launchers --workspace +``` + +Use `--repair` only when Duo reports an owned missing or stale projection. Duo does not overwrite modified or unowned content. + +The hand-managed integrations below remain outside that shared product target. Do not run them as part of authoring this skill. ### Claude Code (first) diff --git a/skills/embed.go b/skills/embed.go new file mode 100644 index 0000000..0c81ad1 --- /dev/null +++ b/skills/embed.go @@ -0,0 +1,11 @@ +// Package skills embeds the canonical authored skill tree for bare-binary +// installs. The embedded bytes come directly from the normative source; there +// is no generated or separately edited copy under assets/. +package skills + +import "embed" + +// FS contains the canonical skills shipped by Duo. +// +//go:embed duo-delegation-loop/SKILL.md +var FS embed.FS