From bfa4bc23a4cd5f5ec031eb77b6b8989d10273987 Mon Sep 17 00:00:00 2001 From: zengchuang Date: Sat, 21 Nov 2026 19:29:15 +0800 Subject: [PATCH 1/3] feat(harness): validate prepared bindings before registration Adapt the authorized pool draft and freeze the null-timeout binding vector. Agent-assisted-by: Codex (author) Signed-off-by: zengchuang --- docs/apps-module-migrations.json | 1 + src/inferrsi/harness/execution_service.py | 84 +++++- src/inferrsi/harness/prepared_binding.py | 269 ++++++++++++++++++ .../harness-prepared-binding/README.md | 14 + .../d023-null-timeout.json | 1 + tests/test_harness_execute.py | 4 +- tests/test_harness_prepared_binding.py | 116 ++++++++ 7 files changed, 480 insertions(+), 9 deletions(-) create mode 100644 src/inferrsi/harness/prepared_binding.py create mode 100644 tests/fixtures/harness-prepared-binding/README.md create mode 100644 tests/fixtures/harness-prepared-binding/d023-null-timeout.json create mode 100644 tests/test_harness_prepared_binding.py diff --git a/docs/apps-module-migrations.json b/docs/apps-module-migrations.json index ebaf1b65c..a74372bf8 100644 --- a/docs/apps-module-migrations.json +++ b/docs/apps-module-migrations.json @@ -127,6 +127,7 @@ ], "consumers": [ "docs/apps-output.md", + "tests/test_harness_prepared_binding.py", "docs/architecture.md", "docs/module-layout.md", "docs/operations.md", diff --git a/src/inferrsi/harness/execution_service.py b/src/inferrsi/harness/execution_service.py index edc40a3fa..66995746e 100644 --- a/src/inferrsi/harness/execution_service.py +++ b/src/inferrsi/harness/execution_service.py @@ -12,11 +12,12 @@ from pathlib import Path from inferrsi.contracts import json_v1 -from inferrsi.harness import input_binding, local_process, run_store +from inferrsi.harness import input_binding, local_process, prepared_binding, run_store from inferrsi.harness.errors import PublicError -# H02 single local-CPU execute in ordinary command mode (V05 null set). Evaluation mode, -# entry-code digests (T37) and generation chaining are later slices and are refused here. +# H02 single local-CPU execute in ordinary command mode (V05 null set). Evaluation-mode +# specs are checked before registration (V05/V28/V32 and their fixed preparation) and then +# refused: the T37 entry-code check and generation chaining are later slices. ATTEMPT_FIELDS = ( "request_id", "run_id", "attempt_id", "owner", "target_request", "deadline_utc", "resources", "budget", "retry_of", "retry_reason", @@ -42,6 +43,10 @@ ) _OUTPUT_FIELDS = frozenset({"role", "path", "required", "media_type", "format_schema"}) _REF_FIELDS = frozenset({"location", "sha256", "size_bytes", "media_type"}) +_PREPARED_REF_FIELDS = frozenset({"prepared_id", "schema_version", "artifact"}) +_CANDIDATE_FIELDS = frozenset( + {"candidate_id", "kind", "input_role", "parent_id", "parent_digest", "context_refs"} +) _DIMENSION_FIELDS = frozenset({"dimension", "unit", "limit", "reserve_estimate"}) _DEFINITION_FIELDS = frozenset({"name", "revision"}) _DECIMAL = re.compile(r"(?:0|-?[1-9][0-9]*|-?(?:0|[1-9][0-9]*)\.[0-9]*[1-9])", re.ASCII) @@ -222,6 +227,9 @@ def _step_errors(steps: object, ordinary: bool) -> list[str]: elif ordinary and digest is not None: # V32-N1: ordinary command mode carries no expected entry digest. errors.append(f"{where}.expected_code_sha256: must be null in ordinary command mode") + elif not ordinary and digest is None: + # V32-N2: null never opts an evaluation step out of the frozen code digest. + errors.append(f"{where}.expected_code_sha256: must be set in evaluation mode (V32)") if not (isinstance(step["args"], list) and all(isinstance(a, str) for a in step["args"])): errors.append(f"{where}.args: not a list of Text") if step["cwd"] != "." and not _is_path(step["cwd"]): @@ -265,6 +273,53 @@ def _output_errors(outputs: object, where: str) -> list[str]: return errors +def _evaluation_errors(spec: Mapping) -> list[str]: + """Static shape of the evaluation fields; the fixed preparation is read separately.""" + errors: list[str] = [] + ref = spec["prepared_ref"] + if not run_store._has_fields(ref, _PREPARED_REF_FIELDS): + errors.append("prepared_ref: fields must be exactly the PreparedRef fields") + else: + if not prepared_binding._is_semantic_id(ref["prepared_id"]): + errors.append("prepared_ref.prepared_id: not a SemanticId") + if type(ref["schema_version"]) is not int or ref["schema_version"] != 1: + errors.append("prepared_ref.schema_version: must be the integer 1") + if not _is_artifact_ref(ref["artifact"]): + errors.append("prepared_ref.artifact: not an ArtifactRef") + elif ref["artifact"]["media_type"] != prepared_binding.PREPARED_MEDIA_TYPE: + errors.append("prepared_ref.artifact.media_type: must be application/json") + if not prepared_binding._is_semantic_id(spec["protocol_digest"]): + errors.append("protocol_digest: not a SemanticId") + for name in ("operation", "validation_scope"): + if not run_store._is_id(spec[name]): + errors.append(f"{name}: not an Id") + candidate = spec["candidate"] + if not run_store._has_fields(candidate, _CANDIDATE_FIELDS): + errors.append("candidate: fields must be exactly the CandidateSubmission fields") + return errors + for name in ("candidate_id", "kind", "input_role"): + if not run_store._is_id(candidate[name]): + errors.append(f"candidate.{name}: not an Id") + # V02: parent id and digest are both null or both set. + if (candidate["parent_id"] is None) != (candidate["parent_digest"] is None): + errors.append("candidate.parent_id: parent_id and parent_digest must be paired") + elif candidate["parent_id"] is not None and not ( + run_store._is_id(candidate["parent_id"]) and prepared_binding._is_sha256( + candidate["parent_digest"] + ) + ): + errors.append("candidate.parent_id: not an Id with a Sha256 parent_digest") + refs = candidate["context_refs"] + if not isinstance(refs, list) or not all(_is_artifact_ref(item) for item in refs): + errors.append("candidate.context_refs: not a list of ArtifactRef") + # V03: the candidate role names exactly one InputSpec. + inputs = spec["inputs"] if isinstance(spec["inputs"], list) else [] + roles = [item["role"] for item in inputs if isinstance(item, Mapping) and "role" in item] + if roles.count(candidate["input_role"]) != 1: + errors.append("candidate.input_role: must name exactly one input role (V03)") + return errors + + def _request_errors(spec: Mapping) -> list[str]: """Shape and resource/budget field errors beyond the AttemptContext rules.""" errors: list[str] = [] @@ -304,8 +359,12 @@ def _request_errors(spec: Mapping) -> list[str]: return errors -def validate(spec: object, target: LocalCpuTarget) -> None: - """Refuse a request before registration (D021 priority: version, fields, support).""" +def validate(spec: object, target: LocalCpuTarget, *, artifact_reader=None) -> None: + """Refuse a request before registration (D021 priority: version, fields, support). + + An evaluation-mode spec is also checked against its fixed preparation, read through + ``artifact_reader`` (V05, V28, V32). Nothing here registers an attempt or writes a record. + """ if isinstance(spec, Mapping): version = spec.get("schema_version") if type(version) is int and version != 1: @@ -323,8 +382,11 @@ def validate(spec: object, target: LocalCpuTarget) -> None: errors += run_store._attempt_errors({name: spec[name] for name in ATTEMPT_FIELDS}) errors += _request_errors(spec) evaluation = [name for name in _EVALUATION_FIELDS if spec[name] is not None] - if evaluation and len(evaluation) != len(_EVALUATION_FIELDS): + complete = len(evaluation) == len(_EVALUATION_FIELDS) + if evaluation and not complete: errors.append("prepared_ref: evaluation fields must be all null or all set (V05)") + elif complete: + errors += _evaluation_errors(spec) errors += _input_errors(spec["inputs"]) errors += _step_errors(spec["steps"], ordinary=not evaluation) try: @@ -336,9 +398,15 @@ def validate(spec: object, target: LocalCpuTarget) -> None: "InvalidExecutionSpec", stage="validate", message="illegal ExecutionSpec", field_errors=errors, ) + if evaluation: + # Malformed or inconsistent bindings are refused here, before the RunStore sees the + # request. The reader is the read-only port; H does not write or register anything. + prepared_binding.verify(spec, artifact_reader) unsupported = [] if evaluation: - unsupported.append("evaluation mode is not implemented in this slice") + # The T37 pre-start entry-code check runs after registration and is a later slice. + # Until it exists, no evaluation-mode candidate may start. + unsupported.append("evaluation-mode entry verification is not implemented in this slice") if spec["generation_record_ref"] is not None: unsupported.append("generation chaining is not implemented in this slice") request = spec["target_request"] @@ -920,7 +988,7 @@ def execute( artifact_reader=None, ) -> dict: """H ``execute``: register, admit, bind, run and commit one ordinary command request.""" - validate(spec, target) + validate(spec, target, artifact_reader=artifact_reader) digest = run_store.request_digest(spec) attempt = {name: copy.deepcopy(spec[name]) for name in ATTEMPT_FIELDS} # Re-entry is decided by the store before any binding or start (V14 / D021 section 3). diff --git a/src/inferrsi/harness/prepared_binding.py b/src/inferrsi/harness/prepared_binding.py new file mode 100644 index 000000000..a3ab05ad5 --- /dev/null +++ b/src/inferrsi/harness/prepared_binding.py @@ -0,0 +1,269 @@ +"""H's read-only binding projection of a fixed PreparedEvaluation (D021 section 1, V28, V32). + +An evaluation-mode ExecutionSpec names its preparation by PreparedRef. Before any +registration, H reads the fixed bytes through the read-only artifact port, verifies them +against the Ref and compares the fields it needs with the spec: ids, digests, operation and +scope, the candidate declaration and the frozen worker code digest. The projection is defined +here. It imports nothing from Benchmark or Apps, and it never interprets task rules, formats +or scores. + +Nothing in this module registers an attempt, writes a record or starts a process. Every +refusal is raised before the RunStore sees the request. Verifying the actual entry bytes +against the frozen digest (T37) happens after registration and is not part of this module. +""" + +from __future__ import annotations + +import hashlib +import re +from collections.abc import Mapping +from dataclasses import dataclass + +from inferrsi.contracts import json_v1 +from inferrsi.harness import run_store +from inferrsi.harness.errors import PublicError + +STAGE = "prepared_binding" +PREPARED_MEDIA_TYPE = "application/json" +# The candidate identity rules this H supports (V28). Any other is refused, never downgraded. +CAPTURED_SHA256 = {"name": "captured-sha256", "revision": "1"} +EXPECTED_SHA256 = {"name": "expected-sha256", "revision": "1"} + +# Exact v1 field sets of the objects the projection reads. An unknown field is refused and +# never dropped (V01). +_PREPARED_FIELDS = frozenset( + { + "schema_version", "prepared_id", "task", "grader", "operation", "validation_scope", + "protocol", "protocol_digest", "candidate_contract", "expected_candidate", + "required_evidence", "recipe", "resources", "created_at", "display_notes", + } +) +_CONTRACT_FIELDS = frozenset( + { + "kind", "input_role", "format_schema", "entrypoint", "required_paths", + "allowed_content", "identity_rule", "modification_receipt_required", + } +) +_RECIPE_FIELDS = frozenset( + { + "recipe_revision", "runtime_role", "entrypoint_kind", "entrypoint", + "entrypoint_revision", "code_sha256", "args", "input_roles", "outputs", + "required_capabilities", + } +) +_EXPECTED_FIELDS = frozenset({"candidate_id", "sha256", "format_schema"}) +_DEFINITION_FIELDS = frozenset({"name", "revision"}) +_SEMANTIC_ID = re.compile(r"sha256:[0-9a-f]{64}", re.ASCII) +_SHA256 = re.compile(r"[0-9a-f]{64}", re.ASCII) +_READ_CHUNK = 1024 * 1024 + + +@dataclass(frozen=True) +class FixedPreparation: + """The identity fields H reads from a fixed preparation; nothing else is retained.""" + + prepared_id: str + operation: str + validation_scope: str + protocol_digest: str + candidate_kind: str + candidate_input_role: str + candidate_format: dict + identity_rule: dict + expected_candidate: dict | None + code_sha256: str + + +def _refuse(code: str, message: str, field_errors: list[str] | None = None) -> PublicError: + return PublicError(code, stage=STAGE, message=message, field_errors=field_errors or []) + + +def _is_definition(value: object) -> bool: + return run_store._has_fields(value, _DEFINITION_FIELDS) and all( + run_store._is_id(value[name]) for name in _DEFINITION_FIELDS + ) + + +def _is_sha256(value: object) -> bool: + return isinstance(value, str) and _SHA256.fullmatch(value) is not None + + +def _is_semantic_id(value: object) -> bool: + return isinstance(value, str) and _SEMANTIC_ID.fullmatch(value) is not None + + +def read(ref: Mapping, reader) -> bytes: + """The fixed bytes of ``ref`` from the read-only port, verified against the Ref. + + The port's own verification is not trusted: size and hash are checked here again + (V06). At most ``size_bytes + 1`` bytes are read, so a wrong Ref cannot make this + allocate more than the stream actually holds. + """ + if reader is None: + raise _refuse( + "ArtifactUnavailable", "no read-only artifact reader is configured for the " + "fixed preparation", + ) + size = ref["size_bytes"] + digest = hashlib.sha256() + parts: list[bytes] = [] + total = 0 + try: + stream = reader.open(ref) + with stream: + while total <= size: + chunk = stream.read(min(_READ_CHUNK, size + 1 - total)) + if not chunk: + break + digest.update(chunk) + parts.append(chunk) + total += len(chunk) + except PublicError: + raise + except OSError as exc: + raise _refuse( + "ArtifactUnavailable", + f"the fixed preparation could not be read: {run_store._describe(exc)}", + ) from exc + if total != size or digest.hexdigest() != ref["sha256"]: + raise _refuse( + "ArtifactIntegrityError", "the fixed preparation bytes differ from their ArtifactRef" + ) + return b"".join(parts) + + +def project(data: bytes) -> FixedPreparation: + """Parse the identity fields of canonical PreparedEvaluation bytes.""" + try: + prepared = json_v1.decode(data) + except json_v1.CanonicalError as exc: + raise _refuse( + "ArtifactIntegrityError", + f"the fixed preparation is not canonical inferrsi-json-v1 ({exc.stage})", + ) from exc + version = prepared.get("schema_version") if isinstance(prepared, dict) else None + if type(version) is int and version != 1: + raise _refuse("UnsupportedSchema", "only PreparedEvaluation v1 is supported") + errors: list[str] = [] + if not run_store._has_fields(prepared, _PREPARED_FIELDS): + raise _refuse( + "InvalidExecutionSpec", "illegal fixed preparation", + ["prepared: fields must be exactly the PreparedEvaluation fields"], + ) + if type(version) is not int: + errors.append("prepared.schema_version: must be the integer 1") + if not _is_semantic_id(prepared["prepared_id"]): + errors.append("prepared.prepared_id: not a SemanticId") + if not _is_semantic_id(prepared["protocol_digest"]): + errors.append("prepared.protocol_digest: not a SemanticId") + for name in ("operation", "validation_scope"): + if not run_store._is_id(prepared[name]): + errors.append(f"prepared.{name}: not an Id") + contract, recipe = prepared["candidate_contract"], prepared["recipe"] + expected = prepared["expected_candidate"] + if not run_store._has_fields(contract, _CONTRACT_FIELDS): + errors.append("prepared.candidate_contract: not the CandidateContract fields") + else: + for name in ("kind", "input_role"): + if not run_store._is_id(contract[name]): + errors.append(f"prepared.candidate_contract.{name}: not an Id") + for name in ("format_schema", "identity_rule"): + if not _is_definition(contract[name]): + errors.append(f"prepared.candidate_contract.{name}: not a DefinitionRef") + if not run_store._has_fields(recipe, _RECIPE_FIELDS): + errors.append("prepared.recipe: not the OperationRecipe fields") + elif not _is_sha256(recipe["code_sha256"]): + errors.append("prepared.recipe.code_sha256: not a Sha256") + if expected is not None: + if not run_store._has_fields(expected, _EXPECTED_FIELDS): + errors.append( + "prepared.expected_candidate: not null or the CandidateExpectation fields" + ) + else: + if not run_store._is_id(expected["candidate_id"]): + errors.append("prepared.expected_candidate.candidate_id: not an Id") + if not _is_sha256(expected["sha256"]): + errors.append("prepared.expected_candidate.sha256: not a Sha256") + if not _is_definition(expected["format_schema"]): + errors.append("prepared.expected_candidate.format_schema: not a DefinitionRef") + if errors: + raise _refuse("InvalidExecutionSpec", "illegal fixed preparation", errors) + return FixedPreparation( + prepared_id=prepared["prepared_id"], + operation=prepared["operation"], + validation_scope=prepared["validation_scope"], + protocol_digest=prepared["protocol_digest"], + candidate_kind=contract["kind"], + candidate_input_role=contract["input_role"], + candidate_format=contract["format_schema"], + identity_rule=contract["identity_rule"], + expected_candidate=expected, + code_sha256=recipe["code_sha256"], + ) + + +def _check_identity(spec: Mapping, fixed: FixedPreparation) -> None: + """V17: top-level identity fields must agree before any declaration is compared.""" + pairs = ( + ("prepared_ref.prepared_id", spec["prepared_ref"]["prepared_id"], fixed.prepared_id), + ("protocol_digest", spec["protocol_digest"], fixed.protocol_digest), + ("operation", spec["operation"], fixed.operation), + ("validation_scope", spec["validation_scope"], fixed.validation_scope), + ) + differing = [f"{name}: differs from the fixed preparation" for name, a, b in pairs if a != b] + if differing: + raise _refuse( + "IdentityMismatch", "the execution spec names a different preparation", differing + ) + + +def _check_declarations(spec: Mapping, fixed: FixedPreparation) -> None: + """V28 and V32: declaration conflicts are refused before registration, never recorded.""" + errors = [ + f"steps[{index}].expected_code_sha256: must equal the frozen recipe code_sha256 (V32)" + for index, step in enumerate(spec["steps"]) + if step["expected_code_sha256"] != fixed.code_sha256 + ] + candidate = spec["candidate"] + if candidate["input_role"] != fixed.candidate_input_role: + errors.append("candidate.input_role: differs from the frozen candidate contract") + if candidate["kind"] != fixed.candidate_kind: + errors.append("candidate.kind: differs from the frozen candidate contract") + declared = next( + (item["expected_sha256"] for item in spec["inputs"] + if item["role"] == candidate["input_role"]), + None, + ) + if fixed.identity_rule == EXPECTED_SHA256 and declared is None: + errors.append( + "inputs: the candidate input needs expected_sha256 under expected-sha256@1 (V28)" + ) + frozen = fixed.expected_candidate + if frozen is not None: + if candidate["candidate_id"] != frozen["candidate_id"]: + errors.append("candidate.candidate_id: differs from the frozen expected candidate") + if declared != frozen["sha256"]: + errors.append("inputs: the candidate expected_sha256 differs from the frozen one") + if fixed.candidate_format != frozen["format_schema"]: + errors.append("prepared: expected candidate format differs from the candidate contract") + if errors: + raise _refuse( + "InvalidExecutionSpec", "the execution spec conflicts with its fixed preparation", + errors, + ) + + +def verify(spec: Mapping, reader) -> FixedPreparation: + """Check an evaluation-mode spec against its fixed preparation before registration. + + ``spec`` has passed the static ExecutionSpec checks, so its evaluation fields are all + set and well formed. Raises a PublicError for any malformed or inconsistent binding. + """ + fixed = project(read(spec["prepared_ref"]["artifact"], reader)) + if fixed.identity_rule not in (CAPTURED_SHA256, EXPECTED_SHA256): + raise _refuse( + "UnsupportedRuleVersion", "the frozen candidate identity rule is unsupported" + ) + _check_identity(spec, fixed) + _check_declarations(spec, fixed) + return fixed diff --git a/tests/fixtures/harness-prepared-binding/README.md b/tests/fixtures/harness-prepared-binding/README.md new file mode 100644 index 000000000..e235960c0 --- /dev/null +++ b/tests/fixtures/harness-prepared-binding/README.md @@ -0,0 +1,14 @@ +# D023 null-timeout binding vector + +`d023-null-timeout.json` is derived from the unchanged canonical +`docs/contracts/fixtures/t108-v4/execution-spec.json`: add the frozen preparation's +`recipe.code_sha256` as `steps[0].expected_code_sha256`, and set +`steps[0].timeout_seconds` to null. The accepted #429 PR-1 vector is 2069 bytes, +with request digest +`sha256:62ce11e6d458b5c581245d0988189e1bd834f2ed1e1880d15242faf70c5b6fe9`. + +This is immutable contract-conformance input, not observed execution evidence or +an available worker implementation. Tests compare the canonical Apps binder, +Harness digest and read-only prepared projection. No process is started. T37 +entry verification and production T40 replay remain separate PR-2/PR-3 work. +The original documentary fixtures are not rewritten. diff --git a/tests/fixtures/harness-prepared-binding/d023-null-timeout.json b/tests/fixtures/harness-prepared-binding/d023-null-timeout.json new file mode 100644 index 000000000..da758ae6b --- /dev/null +++ b/tests/fixtures/harness-prepared-binding/d023-null-timeout.json @@ -0,0 +1 @@ +{"attempt_id":"att-1","budget":{"declared_by":"fixture","dimensions":[],"policy_ref":null,"required_enforcement":[],"scope_id":"campaign-1"},"candidate":{"candidate_id":"cand-1","context_refs":[],"input_role":"candidate","kind":"text","parent_digest":null,"parent_id":null},"deadline_utc":"2099-01-01T00:00:00Z","generation_record_ref":null,"inputs":[{"expected_sha256":"e39494a6eed78161414f7afdef602f86ff19de6efa9ca9053407d548981fe01f","materialized_path":"inputs/candidate.txt","role":"candidate","snapshot_format":"file-v1","source":"fixtures/candidate.txt","source_namespace":"artifact","source_ref":{"location":"fixtures/candidate.txt","media_type":"text/plain","sha256":"e39494a6eed78161414f7afdef602f86ff19de6efa9ca9053407d548981fe01f","size_bytes":18}}],"operation":"evaluate","output_root":"attempts/att-1","owner":"fixture","prepared_ref":{"artifact":{"location":"fixtures/prepared.json","media_type":"application/json","sha256":"8450cf590172a785282fed31480eb82bb192f4e9a72c73dd70d0b5ea9a3d30ff","size_bytes":2690},"prepared_id":"sha256:7f481e7e61c98343015118d7dcd98806abe863398b2c16dacac125d2d00ed486","schema_version":1},"protocol_digest":"sha256:497290081c4f3d3cab0c50c42a0be1c467e1a840dcf680eaace602311c46a80c","request_id":"att-1","required_capabilities":["local-process-tree"],"resources":{"cpu_count":1,"memory_bytes":null,"required_capabilities":["local-process-tree"]},"retry_of":null,"retry_reason":null,"run_id":"run-1","schema_version":1,"steps":[{"args":["inputs/candidate.txt","outputs/grader.json"],"cwd":".","entrypoint":"fixture_worker","entrypoint_kind":"python_module","entrypoint_revision":"1","expected_code_sha256":"ba351e8f96c162ed9c32309f24b1d2d75d11a7369168571ebbb0a79d13315332","outputs":[{"format_schema":{"name":"fixture-grader","revision":"1"},"media_type":"application/json","path":"outputs/grader.json","required":true,"role":"grader"}],"runtime_role":"local-python","step_id":"main","timeout_seconds":null}],"target_request":{"config_revision":"1","executor_kind":"local_cpu","target_id":"fixture-cpu"},"validation_scope":"p1"} \ No newline at end of file diff --git a/tests/test_harness_execute.py b/tests/test_harness_execute.py index efd054515..0778e0d05 100644 --- a/tests/test_harness_execute.py +++ b/tests/test_harness_execute.py @@ -37,6 +37,7 @@ NEW_H_CLOSURE = { "inferrsi.harness.api", "inferrsi.harness.execution_service", "inferrsi.harness.input_binding", "inferrsi.harness.local_process", + "inferrsi.harness.prepared_binding", "inferrsi.harness.atomic_json", "inferrsi.harness.run_store", "inferrsi.harness.errors", "inferrsi.harness.execution_spec", "inferrsi.contracts.json_v1", @@ -436,7 +437,8 @@ def test_spec_time_refusals_register_nothing(h: Harness) -> None: candidate={"candidate_id": "cand", "context_refs": [], "input_role": "candidate", "kind": "text", "parent_digest": None, "parent_id": None}, ) - assert error_of(h.execute, evaluation).code == "UnsupportedOperation" + # The malformed binding is rejected before support/entry checks (PR-1 / V32-N2). + assert error_of(h.execute, evaluation).code == "InvalidExecutionSpec" assert error_of(h.execute, dict(spec, schema_version=2)).code == "UnsupportedSchema" float_timeout = dict(spec, steps=[dict(spec["steps"][0], timeout_seconds=1.5)]) assert error_of(h.execute, float_timeout).code == "InvalidExecutionSpec" diff --git a/tests/test_harness_prepared_binding.py b/tests/test_harness_prepared_binding.py new file mode 100644 index 000000000..4601ebac3 --- /dev/null +++ b/tests/test_harness_prepared_binding.py @@ -0,0 +1,116 @@ +"""PR-1: bind frozen preparation before registration; never perform T37 entry checks.""" + +import sqlite3 +from copy import deepcopy +from hashlib import sha256 +from io import BytesIO +from pathlib import Path + +import pytest + +from inferrsi.apps.handoff.bridge import bind_execution, verify_protocol +from inferrsi.contracts.json_v1 import decode, encode +from inferrsi.harness import execution_service, prepared_binding, run_store +from inferrsi.harness.api import Harness, LocalCpuTarget +from inferrsi.harness.errors import PublicError + +ROOT = Path(__file__).resolve().parents[1] +GOLDEN = ROOT / "docs/contracts/fixtures/t108-v4" +VECTOR = ROOT / "tests/fixtures/harness-prepared-binding/d023-null-timeout.json" +DIGEST = "sha256:62ce11e6d458b5c581245d0988189e1bd834f2ed1e1880d15242faf70c5b6fe9" + + +def test_d023_null_timeout_bytes_match_apps_and_harness(): + data = VECTOR.read_bytes() + spec = decode(data) + prepared = decode((GOLDEN / "prepared.json").read_bytes()) + verify_protocol(prepared, (GOLDEN / "protocol.json").read_bytes()) + request = { + "schema_version": 1, "prepared_ref": spec["prepared_ref"], + "attempt": {key: spec[key] for key in execution_service.ATTEMPT_FIELDS}, + "proposal": {key: spec[key] for key in ("candidate", "inputs", "generation_record_ref")}, + "output_root": spec["output_root"], + } + assert len(data) == 2069 + assert "sha256:" + sha256(data).hexdigest() == DIGEST + assert run_store.request_digest(spec) == DIGEST + assert encode(bind_execution(request, prepared)) == data + assert spec["steps"][0]["timeout_seconds"] is None + + +@pytest.fixture +def binding(tmp_path, monkeypatch): + spec = decode(VECTOR.read_bytes()) + prepared = (GOLDEN / "prepared.json").read_bytes() + + class Reader: + def __init__(self): + self.opens = [] + self.data = prepared + + def open(self, ref): + assert ref == spec["prepared_ref"]["artifact"] + self.opens.append(deepcopy(ref)) + return BytesIO(self.data) + + reader = Reader() + store = run_store.RunStore(tmp_path / "store", "binding-tests") + target = LocalCpuTarget("fixture-cpu", "1", work_root=tmp_path / "work") + harness = Harness(store, target, artifact_reader=reader) + + def forbidden(*args, **kwargs): + pytest.fail("a PR-1 refusal reached registration or process execution") + + monkeypatch.setattr(store, "claim", forbidden) + monkeypatch.setattr(execution_service.local_process, "execute_command", forbidden) + return spec, reader, store, harness + + +def assert_empty(store): + with sqlite3.connect(store.root / run_store.INDEX_NAME) as connection: + assert connection.execute("SELECT count(*) FROM attempts").fetchone()[0] == 0 + assert connection.execute("SELECT count(*) FROM records").fetchone()[0] == 0 + + +def test_projection_reads_the_fixed_preparation_without_resolving_an_entry(binding): + spec, reader, store, harness = binding + fixed = prepared_binding.verify(spec, reader) + assert fixed.prepared_id == spec["prepared_ref"]["prepared_id"] + assert fixed.code_sha256 == spec["steps"][0]["expected_code_sha256"] + assert len(reader.opens) == 1 + # T37 is a later delivery: valid evaluation bindings still cannot start. + with pytest.raises(PublicError) as caught: + harness.execute(spec) + assert caught.value.code == "UnsupportedOperation" + assert_empty(store) + + +@pytest.mark.parametrize("failure", ["null-code", "wrong-code", "protocol", "candidate", "input"]) +def test_malformed_or_inconsistent_binding_registers_no_attempt(binding, failure): + spec, reader, store, harness = binding + if failure == "null-code": + spec["steps"][0]["expected_code_sha256"] = None + elif failure == "wrong-code": + spec["steps"][0]["expected_code_sha256"] = "0" * 64 + elif failure == "protocol": + spec["protocol_digest"] = "sha256:" + "0" * 64 + elif failure == "candidate": + spec["candidate"]["candidate_id"] = "different-candidate" + else: + spec["inputs"][0]["expected_sha256"] = "0" * 64 + spec["inputs"][0]["source_ref"]["sha256"] = "0" * 64 + with pytest.raises(PublicError) as caught: + harness.execute(spec) + assert caught.value.code == ("IdentityMismatch" if failure == "protocol" + else "InvalidExecutionSpec") + assert_empty(store) + assert len(reader.opens) == (0 if failure == "null-code" else 1) + + +def test_corrupt_preparation_bytes_cannot_reach_registration(binding): + spec, reader, store, harness = binding + reader.data = b"corrupt retained preparation" + with pytest.raises(PublicError) as caught: + harness.execute(spec) + assert caught.value.code == "ArtifactIntegrityError" + assert_empty(store) From b8d8685c74fe4c4a116e06b178e9bc32060605e3 Mon Sep 17 00:00:00 2001 From: zengchuang Date: Sat, 21 Nov 2026 19:45:39 +0800 Subject: [PATCH 2/3] feat(harness): bind frozen worker declarations before registration Verify candidate and worker declarations against fixed preparation bytes. Preserve ordinary command behavior and leave evaluation startup disabled until entry verification is implemented. Agent-assisted-by: Codex (author) Agent-assisted-by: Unknown PR Pool agent (draft contributor) Signed-off-by: zengchuang --- docs/contracts/harness-prepared-binding.md | 52 +++++ docs/operations.md | 1 + src/inferrsi/harness/prepared_binding.py | 112 +++++++++- tests/check_installed.py | 9 + tests/test_harness_prepared_binding.py | 240 +++++++++++++++++++++ 5 files changed, 405 insertions(+), 9 deletions(-) create mode 100644 docs/contracts/harness-prepared-binding.md diff --git a/docs/contracts/harness-prepared-binding.md b/docs/contracts/harness-prepared-binding.md new file mode 100644 index 000000000..15db9301e --- /dev/null +++ b/docs/contracts/harness-prepared-binding.md @@ -0,0 +1,52 @@ +# Harness preparation binding before registration + +The Harness evaluation path checks declarations before registering an attempt. +`inferrsi.harness.prepared_binding.verify(spec, artifact_reader)` returns a +`FixedPreparation` projection or raises a Harness `PublicError`. The spec must +first pass `execution_service.validate`'s static shape checks; `Harness.execute` +performs both checks in that order. Ordinary command mode reads no preparation. + +The projection's `read(ref, reader)` port obtains a binary stream through +`reader.open(ArtifactRef)`, closes it, bounds reads by the declared size plus one +byte, and independently checks size and SHA-256. Missing bytes or a missing +reader produce `ArtifactUnavailable`; changed bytes or noncanonical JSON +produce `ArtifactIntegrityError`. Unknown host programming errors propagate. +`project(bytes)` is pure: it parses the canonical v1 envelope and validates the +fields used by Harness. It does not read a protocol, resolve an entry or start a +process. A schema version other than v1 is explicitly unsupported. + +Binding compares the semantic prepared ID, protocol digest, operation and scope +with the fixed preparation. Disagreement is `IdentityMismatch`. It also checks: + +- Candidate kind and role, a frozen expected candidate ID/hash/format when + present, and the supported `captured-sha256@1` or `expected-sha256@1` identity + rule. An unknown rule/revision is `UnsupportedRuleVersion`. The expected rule + requires a declared expected input hash even without a frozen candidate. +- A non-null expected code digest on every evaluation step, matching the frozen + recipe. This is an expected declaration, not an observed entry-code hash. +- One worker step preserving runtime role, entry kind/name/revision, all output + declarations, and arguments with whole-argument `{input:role}` / `{output:role}` + expansion. Inputs may use caller-chosen materialized paths; arguments must + match those paths. Paths are relative to the attempt directory (`cwd="."`), + and input/output paths must not overlap. Required frozen capabilities cannot + be dropped; additional declared capabilities remain subject to admission. + +Malformed or conflicting declarations are `InvalidExecutionSpec`. All of these +refusals precede `RunStore.claim`: neither attempt nor record is registered. +The public projection does not interpret candidate formats, grading rules, +evidence requirements or scores, and does not recompute Benchmark's semantic +prepared ID. The fixed ArtifactRef authenticates the preparation bytes; Apps +performs the separate archived-protocol binding through its canonical +`inferrsi.apps.handoff.bridge.verify_protocol` port. + +The D023 conformance vector is kept separately under +`tests/fixtures/harness-prepared-binding/`. It adds the frozen code declaration +and a null step timeout without rewriting historical fixtures. Its canonical +bytes must agree between the Apps binder and Harness `request_digest`. +The vector is documentary; its historical code pin is not an available worker. + +This slice keeps valid evaluation-mode execution explicitly unsupported. +Actual entry resolution and code-byte verification after registration (T37), +attempt deadlines/budget enforcement with a null step timeout, and evaluation +replay (T40) are later delivery slices. It does not establish production CPU +acceptance or completion of the governing Harness or Apps Issues. diff --git a/docs/operations.md b/docs/operations.md index 9115ca36b..4e4531453 100644 --- a/docs/operations.md +++ b/docs/operations.md @@ -52,6 +52,7 @@ Effect classes used below: | Rebuild scoped legacy evidence views | `python tools/rebuild_model_evidence.py --archive --output ` | CPU-only; creates a new output directory | Authenticates original bytes and pinned projection/code; no extraction, network, archived-code execution or campaign qualification | | Inspect evidence inventory / generate budget input | `python tools/validate_evidence.py [--budget-assets]` | Read-only | Offline layout/hash/schema checks; optional disposable #328 metadata view derived from campaign authority; no downloads | | Execute one ordinary local CPU command with a fixed record | Python `inferrsi.harness.api.Harness.allocate` / `execute` / `read` | Experiment-executing / record-writing | One explicit RunStore root and one trusted local CPU target; ordinary command mode only; a record holds execution facts, so exit 0 is not a verdict; no device, model or network fallback | +| Verify a frozen preparation binding before registration | Python `inferrsi.harness.prepared_binding.verify` / `read` / `project` | Read-only / pure projection | Fixed ArtifactRef and read-only stream capability; no registration, entry resolution or execution; [binding contract](contracts/harness-prepared-binding.md) | | Freeze a configured P1 preparation | Python `benchmark.preparation.PreparationService.prepare`; [production prepare](contracts/production-prepare.md) | Read-only resource access / pure encoding | Verifies supplied frozen worker/resource bytes and returns a preparation, protocol and role-keyed closure; Apps separately archives them before publishing a Ref | | Visualize a recorded run or campaign | CLI `dashboard`; [dashboard](dashboard.md) | Report-generating / local read-only serving | Writes an offline HTML snapshot or serves an optional live view on loopback; does not run an agent or validate a claim | | Capture a knowledge context | CLI `campaign capture-context`; [offline dashboard](dashboard.md#record-the-optimization-story-campaign-trace) | Local evidence-writing | Saves assembled context and resource hashes in a new `knowledge/.json`. Supports ordinary questions and explicit IDs. The only accepted explicit `--relevance-method` value is `find-lexical-v1` | diff --git a/src/inferrsi/harness/prepared_binding.py b/src/inferrsi/harness/prepared_binding.py index a3ab05ad5..dbeb6709e 100644 --- a/src/inferrsi/harness/prepared_binding.py +++ b/src/inferrsi/harness/prepared_binding.py @@ -3,7 +3,7 @@ An evaluation-mode ExecutionSpec names its preparation by PreparedRef. Before any registration, H reads the fixed bytes through the read-only artifact port, verifies them against the Ref and compares the fields it needs with the spec: ids, digests, operation and -scope, the candidate declaration and the frozen worker code digest. The projection is defined +scope, the candidate declaration and the frozen worker declarations. The projection is defined here. It imports nothing from Benchmark or Apps, and it never interprets task rules, formats or scores. @@ -20,7 +20,7 @@ from dataclasses import dataclass from inferrsi.contracts import json_v1 -from inferrsi.harness import run_store +from inferrsi.harness import input_binding, run_store from inferrsi.harness.errors import PublicError STAGE = "prepared_binding" @@ -53,6 +53,9 @@ ) _EXPECTED_FIELDS = frozenset({"candidate_id", "sha256", "format_schema"}) _DEFINITION_FIELDS = frozenset({"name", "revision"}) +_OUTPUT_FIELDS = frozenset({"role", "path", "required", "media_type", "format_schema"}) +_ARTIFACT_FIELDS = frozenset({"location", "sha256", "size_bytes", "media_type"}) +_PLACEHOLDER = re.compile(r"\{(input|output):([^{}]+)\}") _SEMANTIC_ID = re.compile(r"sha256:[0-9a-f]{64}", re.ASCII) _SHA256 = re.compile(r"[0-9a-f]{64}", re.ASCII) _READ_CHUNK = 1024 * 1024 @@ -60,7 +63,7 @@ @dataclass(frozen=True) class FixedPreparation: - """The identity fields H reads from a fixed preparation; nothing else is retained.""" + """H's mechanical identity/binding projection; task rules and scores are not retained.""" prepared_id: str operation: str @@ -72,6 +75,7 @@ class FixedPreparation: identity_rule: dict expected_candidate: dict | None code_sha256: str + recipe: dict def _refuse(code: str, message: str, field_errors: list[str] | None = None) -> PublicError: @@ -92,6 +96,60 @@ def _is_semantic_id(value: object) -> bool: return isinstance(value, str) and _SEMANTIC_ID.fullmatch(value) is not None +def _is_ids(value: object) -> bool: + return (isinstance(value, list) and all(run_store._is_id(item) for item in value) + and len(set(value)) == len(value)) + + +def _is_path(value: object) -> bool: + return isinstance(value, str) and input_binding.RELATIVE_PATH.fullmatch(value) is not None + + +def _recipe_errors(recipe: object) -> list[str]: + """Validate only mechanical worker declarations, without interpreting formats or rules.""" + if not run_store._has_fields(recipe, _RECIPE_FIELDS): + return ["prepared.recipe: not the OperationRecipe fields"] + errors = [] + for name in ("recipe_revision", "runtime_role", "entrypoint_revision"): + if not run_store._is_id(recipe[name]): + errors.append(f"prepared.recipe.{name}: not an Id") + if recipe["entrypoint_kind"] not in ("python_module", "executable"): + errors.append("prepared.recipe.entrypoint_kind: not a supported entry kind") + if not isinstance(recipe["entrypoint"], str) or not recipe["entrypoint"]: + errors.append("prepared.recipe.entrypoint: not a non-empty Text") + if not _is_sha256(recipe["code_sha256"]): + errors.append("prepared.recipe.code_sha256: not a Sha256") + for name in ("input_roles", "required_capabilities"): + if not _is_ids(recipe[name]): + errors.append(f"prepared.recipe.{name}: not a list of unique Ids") + outputs = recipe["outputs"] + if not isinstance(outputs, list) or not all( + run_store._has_fields(item, _OUTPUT_FIELDS) + and run_store._is_id(item["role"]) and _is_path(item["path"]) + and type(item["required"]) is bool + and isinstance(item["media_type"], str) and bool(item["media_type"]) + and _is_definition(item["format_schema"]) for item in outputs + ): + errors.append("prepared.recipe.outputs: not a list of OutputSpec") + elif (len({item["role"] for item in outputs}) != len(outputs) + or len({item["path"] for item in outputs}) != len(outputs)): + errors.append("prepared.recipe.outputs: roles and paths must be unique") + args = recipe["args"] + if not isinstance(args, list) or not all(isinstance(arg, str) for arg in args): + errors.append("prepared.recipe.args: not a list of Text") + elif not errors: + roles = {"input": set(recipe["input_roles"]), + "output": {item["role"] for item in outputs}} + for arg in args: + match = _PLACEHOLDER.fullmatch(arg) + if match: + if match[2] not in roles[match[1]]: + errors.append("prepared.recipe.args: unknown placeholder role") + elif "{input:" in arg or "{output:" in arg: + errors.append("prepared.recipe.args: placeholders must occupy a whole argument") + return errors + + def read(ref: Mapping, reader) -> bytes: """The fixed bytes of ``ref`` from the read-only port, verified against the Ref. @@ -99,17 +157,23 @@ def read(ref: Mapping, reader) -> bytes: (V06). At most ``size_bytes + 1`` bytes are read, so a wrong Ref cannot make this allocate more than the stream actually holds. """ + if not (run_store._has_fields(ref, _ARTIFACT_FIELDS) and _is_path(ref["location"]) + and _is_sha256(ref["sha256"]) and type(ref["size_bytes"]) is int + and 0 <= ref["size_bytes"] <= json_v1.MAX_SAFE_INTEGER + and ref["media_type"] == PREPARED_MEDIA_TYPE): + raise _refuse("InvalidExecutionSpec", "illegal fixed preparation ArtifactRef") if reader is None: raise _refuse( "ArtifactUnavailable", "no read-only artifact reader is configured for the " "fixed preparation", ) size = ref["size_bytes"] + expected_sha256 = ref["sha256"] digest = hashlib.sha256() parts: list[bytes] = [] total = 0 try: - stream = reader.open(ref) + stream = reader.open(dict(ref)) with stream: while total <= size: chunk = stream.read(min(_READ_CHUNK, size + 1 - total)) @@ -125,7 +189,7 @@ def read(ref: Mapping, reader) -> bytes: "ArtifactUnavailable", f"the fixed preparation could not be read: {run_store._describe(exc)}", ) from exc - if total != size or digest.hexdigest() != ref["sha256"]: + if total != size or digest.hexdigest() != expected_sha256: raise _refuse( "ArtifactIntegrityError", "the fixed preparation bytes differ from their ArtifactRef" ) @@ -170,10 +234,7 @@ def project(data: bytes) -> FixedPreparation: for name in ("format_schema", "identity_rule"): if not _is_definition(contract[name]): errors.append(f"prepared.candidate_contract.{name}: not a DefinitionRef") - if not run_store._has_fields(recipe, _RECIPE_FIELDS): - errors.append("prepared.recipe: not the OperationRecipe fields") - elif not _is_sha256(recipe["code_sha256"]): - errors.append("prepared.recipe.code_sha256: not a Sha256") + errors.extend(_recipe_errors(recipe)) if expected is not None: if not run_store._has_fields(expected, _EXPECTED_FIELDS): errors.append( @@ -199,6 +260,7 @@ def project(data: bytes) -> FixedPreparation: identity_rule=contract["identity_rule"], expected_candidate=expected, code_sha256=recipe["code_sha256"], + recipe=recipe, ) @@ -224,6 +286,7 @@ def _check_declarations(spec: Mapping, fixed: FixedPreparation) -> None: for index, step in enumerate(spec["steps"]) if step["expected_code_sha256"] != fixed.code_sha256 ] + errors.extend(_worker_errors(spec, fixed.recipe)) candidate = spec["candidate"] if candidate["input_role"] != fixed.candidate_input_role: errors.append("candidate.input_role: differs from the frozen candidate contract") @@ -253,6 +316,37 @@ def _check_declarations(spec: Mapping, fixed: FixedPreparation) -> None: ) +def _worker_errors(spec: Mapping, recipe: dict) -> list[str]: + """Preserve the frozen worker's entry, paths and placeholder-expanded arguments.""" + errors = [] + if len(spec["steps"]) != 1: + return ["steps: the frozen recipe binds exactly one worker step"] + step = spec["steps"][0] + for name in ("runtime_role", "entrypoint_kind", "entrypoint", "entrypoint_revision", "outputs"): + if step[name] != recipe[name]: + errors.append(f"steps[0].{name}: differs from the frozen worker declaration") + if step["cwd"] != ".": + errors.append("steps[0].cwd: frozen recipe paths are relative to the attempt directory") + if not set(recipe["required_capabilities"]) <= set(spec["required_capabilities"]): + errors.append("required_capabilities: missing a frozen worker requirement") + inputs = {item["role"]: item["materialized_path"] for item in spec["inputs"]} + if not set(recipe["input_roles"]) <= inputs.keys(): + errors.append("inputs: missing a frozen worker input role") + else: + outputs = {item["role"]: item["path"] for item in recipe["outputs"]} + paths = {"input": inputs, "output": outputs} + args = [] + for arg in recipe["args"]: + match = _PLACEHOLDER.fullmatch(arg) + args.append(paths[match[1]][match[2]] if match else arg) + if step["args"] != args: + errors.append("steps[0].args: differs from the frozen recipe binding") + ordered = sorted(tuple(path.split("/")) for path in [*inputs.values(), *outputs.values()]) + if any(b[:len(a)] == a for a, b in zip(ordered, ordered[1:])): + errors.append("inputs/outputs: materialized paths must not overlap") + return errors + + def verify(spec: Mapping, reader) -> FixedPreparation: """Check an evaluation-mode spec against its fixed preparation before registration. diff --git a/tests/check_installed.py b/tests/check_installed.py index 5b02b0d9f..2279c1e1b 100644 --- a/tests/check_installed.py +++ b/tests/check_installed.py @@ -27,6 +27,15 @@ from inferrsi.resources import repository_packs from inferrsi.resources.repository_packs import REPO_TYPES, repository_pack +# The new Harness binding port ships independently of Apps and Benchmark. +subprocess.run([ + sys.executable, "-I", "-B", "-c", + "import sys; from inferrsi.harness import prepared_binding as p; " + "assert all(callable(getattr(p, n)) for n in ('read', 'project', 'verify')); " + "assert not any(n == root or n.startswith(root + '.') for n in sys.modules " + "for root in ('inferrsi.apps', 'inferrsi.benchmark', 'inferrsi.knowledge'))", +], check=True, capture_output=True, text=True) + assert Path(inferrsi.__file__).resolve().parent.parent.name != "src", ( "This check requires an installed package" ) diff --git a/tests/test_harness_prepared_binding.py b/tests/test_harness_prepared_binding.py index 4601ebac3..fb16b9e9a 100644 --- a/tests/test_harness_prepared_binding.py +++ b/tests/test_harness_prepared_binding.py @@ -63,6 +63,7 @@ def forbidden(*args, **kwargs): monkeypatch.setattr(store, "claim", forbidden) monkeypatch.setattr(execution_service.local_process, "execute_command", forbidden) + monkeypatch.setattr(execution_service, "_argv", forbidden) return spec, reader, store, harness @@ -114,3 +115,242 @@ def test_corrupt_preparation_bytes_cannot_reach_registration(binding): harness.execute(spec) assert caught.value.code == "ArtifactIntegrityError" assert_empty(store) + + +def replace_prepared(binding, path, value): + spec, reader, _, _ = binding + prepared = decode(reader.data) + parent = prepared + for key in path[:-1]: + parent = parent[key] + parent[path[-1]] = value + reader.data = encode(prepared) + spec["prepared_ref"]["artifact"].update( + sha256=sha256(reader.data).hexdigest(), size_bytes=len(reader.data), + ) + + +@pytest.mark.parametrize("path,value", [ + (("prepared_ref", "prepared_id"), "sha256:" + "0" * 64), + (("operation",), "different"), (("validation_scope",), "different"), +]) +def test_all_preparation_identity_disagreements_precede_registration(binding, path, value): + spec, reader, store, harness = binding + parent = spec + for key in path[:-1]: + parent = parent[key] + parent[path[-1]] = value + with pytest.raises(PublicError) as caught: + harness.execute(spec) + assert caught.value.code == "IdentityMismatch" + assert len(reader.opens) == 1 + assert_empty(store) + + +@pytest.mark.parametrize("field,value", [ + ("runtime_role", "another-python"), ("entrypoint_kind", "executable"), + ("entrypoint", "another_worker"), ("entrypoint_revision", "2"), + ("args", ["inputs/candidate.txt", "outputs/other.json"]), ("cwd", "elsewhere"), + ("outputs", []), +]) +def test_worker_declarations_must_preserve_the_frozen_recipe(binding, field, value): + spec, reader, store, harness = binding + spec["steps"][0][field] = value + with pytest.raises(PublicError) as caught: + harness.execute(spec) + assert caught.value.code == "InvalidExecutionSpec" + assert caught.value.stage == "prepared_binding" + assert len(reader.opens) == 1 + assert_empty(store) + + +@pytest.mark.parametrize("field,value", [ + ("path", "inputs/candidate.txt"), ("required", False), ("role", "other"), + ("media_type", "text/plain"), ("format_schema", {"name": "fixture-grader", "revision": "2"}), +]) +def test_output_relabeling_cannot_change_the_frozen_worker_contract(binding, field, value): + spec, _, store, harness = binding + spec["steps"][0]["outputs"][0][field] = value + with pytest.raises(PublicError) as caught: + harness.execute(spec) + assert caught.value.code == "InvalidExecutionSpec" + assert_empty(store) + + +@pytest.mark.parametrize("failure", ["capability", "missing-role", "overlap", "extra-step"]) +def test_incomplete_or_ambiguous_worker_binding_is_refused(binding, failure): + spec, _, store, harness = binding + if failure == "capability": + spec["required_capabilities"] = [] + elif failure == "missing-role": + replace_prepared(binding, ("recipe", "input_roles"), ["candidate", "config"]) + elif failure == "extra-step": + extra = deepcopy(spec["steps"][0]) + extra["step_id"] = "extra" + spec["steps"].append(extra) + else: + # Keep argument binding internally consistent while overlapping an output. + spec["inputs"][0]["materialized_path"] = "outputs" + spec["steps"][0]["args"][0] = "outputs" + with pytest.raises(PublicError) as caught: + harness.execute(spec) + assert caught.value.code == "InvalidExecutionSpec" + assert_empty(store) + + +@pytest.mark.parametrize("path,value", [ + (("schema_version",), True), (("schema_version",), 2), + (("candidate_contract",), None), (("candidate_contract", "identity_rule"), {}), + (("expected_candidate",), {}), (("expected_candidate", "sha256"), None), + (("recipe",), None), (("recipe", "code_sha256"), None), + (("recipe", "runtime_role"), None), (("recipe", "entrypoint_kind"), "unknown"), + (("recipe", "entrypoint"), None), (("recipe", "entrypoint_revision"), ""), + (("recipe", "args"), [None]), (("recipe", "args"), ["{input:unknown}"]), + (("recipe", "args"), ["prefix{input:candidate}"]), + (("recipe", "input_roles"), ["candidate", "candidate"]), + (("recipe", "required_capabilities"), [None]), + (("recipe", "outputs"), [{}]), (("recipe", "outputs", 0, "path"), "../outside"), + (("recipe", "outputs", 0, "format_schema"), {}), +]) +def test_malformed_fixed_projection_is_refused_with_a_declared_error(binding, path, value): + _, _, store, harness = binding + replace_prepared(binding, path, value) + with pytest.raises(PublicError) as caught: + harness.execute(binding[0]) + assert caught.value.code == ("UnsupportedSchema" if value == 2 else "InvalidExecutionSpec") + assert_empty(store) + + +@pytest.mark.parametrize("revision", ["2", "unknown"]) +def test_unknown_candidate_identity_revision_is_never_downgraded(binding, revision): + spec, _, store, harness = binding + replace_prepared(binding, ("candidate_contract", "identity_rule", "revision"), revision) + with pytest.raises(PublicError) as caught: + harness.execute(spec) + assert caught.value.code == "UnsupportedRuleVersion" + assert_empty(store) + + +@pytest.mark.parametrize("rule", ["captured-sha256", "expected-sha256"]) +def test_expected_hash_requirement_depends_on_the_frozen_identity_rule(binding, rule): + spec, _, store, harness = binding + replace_prepared(binding, ("candidate_contract", "identity_rule", "name"), rule) + replace_prepared(binding, ("expected_candidate",), None) + spec["inputs"][0].update( + expected_sha256=None, source_namespace="controller", source_ref=None, + source="candidate.txt", + ) + with pytest.raises(PublicError) as caught: + harness.execute(spec) + assert caught.value.code == ("InvalidExecutionSpec" if rule == "expected-sha256" + else "UnsupportedOperation") + assert_empty(store) + + +@pytest.mark.parametrize("mode", ["noncanonical", "duplicate-key", "array", "extra-field"]) +def test_unparseable_or_unknown_preparation_fields_never_register(binding, mode): + spec, reader, store, harness = binding + if mode == "noncanonical": + reader.data += b"\n" + elif mode == "duplicate-key": + reader.data = reader.data[:-1] + b',"schema_version":1}' + elif mode == "array": + reader.data = b"[]" + else: + replace_prepared(binding, ("unknown",), True) + spec["prepared_ref"]["artifact"].update( + sha256=sha256(reader.data).hexdigest(), size_bytes=len(reader.data), + ) + with pytest.raises(PublicError) as caught: + harness.execute(spec) + expected = ("ArtifactIntegrityError" if mode in {"noncanonical", "duplicate-key"} + else "InvalidExecutionSpec") + assert caught.value.code == expected + assert_empty(store) + + +@pytest.mark.parametrize("path,value", [ + (("schema_version",), True), (("schema_version",), 2), (("unknown",), None), + (("prepared_ref",), None), (("prepared_ref", "schema_version"), 2), + (("prepared_ref", "artifact", "location"), "../outside"), + (("prepared_ref", "artifact", "size_bytes"), True), + (("prepared_ref", "artifact", "sha256"), "A" * 64), + (("prepared_ref", "artifact", "media_type"), "text/plain"), + (("protocol_digest",), "invalid"), (("candidate",), {}), + (("candidate", "kind"), None), (("candidate", "parent_id"), "parent"), + (("candidate", "context_refs"), [None]), (("candidate", "input_role"), "absent"), + (("steps", 0, "expected_code_sha256"), "invalid"), +]) +def test_static_shape_errors_never_open_the_preparation(binding, path, value): + spec, reader, store, harness = binding + parent = spec + for key in path[:-1]: + parent = parent[key] + parent[path[-1]] = value + with pytest.raises(PublicError) as caught: + harness.execute(spec) + assert caught.value.code == ("UnsupportedSchema" if path == ("schema_version",) and value == 2 + else "InvalidExecutionSpec") + assert reader.opens == [] + assert_empty(store) + + +@pytest.mark.parametrize("mode", ["undersize", "oversize", "wrong-hash", "missing", "os-error"]) +def test_fixed_byte_reader_closes_streams_and_bounds_reads(binding, mode): + spec, reader, store, harness = binding + original = spec["prepared_ref"]["artifact"] + sizes = [] + + class Tracking(BytesIO): + def read(self, size=-1): + sizes.append(size) + return super().read(size) + + stream = Tracking(reader.data + (b"x" if mode == "oversize" else b"")) + + class Tracked: + def open(self, ref): + if mode == "os-error": + raise FileNotFoundError("fixture missing") + return stream + + if mode == "undersize": + original["size_bytes"] += 1 + elif mode == "wrong-hash": + original["sha256"] = "0" * 64 + with pytest.raises(PublicError) as caught: + prepared_binding.verify(spec, None if mode == "missing" else Tracked()) + assert caught.value.code == ("ArtifactUnavailable" if mode in {"missing", "os-error"} + else "ArtifactIntegrityError") + if mode not in {"missing", "os-error"}: + assert stream.closed + assert max(sizes) <= original["size_bytes"] + 1 + assert_empty(store) + + +def test_reader_cannot_rewrite_the_ref_it_is_meant_to_verify(binding): + spec, _, store, _ = binding + original = deepcopy(spec["prepared_ref"]["artifact"]) + + class Mutating: + def open(self, ref): + ref.update(sha256=sha256(b"{}").hexdigest(), size_bytes=2) + return BytesIO(b"{}") + + with pytest.raises(PublicError) as caught: + prepared_binding.verify(spec, Mutating()) + assert caught.value.code == "ArtifactIntegrityError" + assert spec["prepared_ref"]["artifact"] == original + assert_empty(store) + + +def test_reader_programming_errors_propagate_without_registration(binding): + spec, _, store, _ = binding + + class Broken: + def open(self, ref): + raise TypeError("broken host reader") + + with pytest.raises(TypeError, match="broken host reader"): + prepared_binding.verify(spec, Broken()) + assert_empty(store) From 66fef981f6893038f3128595ffcdfd12b65bfba2 Mon Sep 17 00:00:00 2001 From: zengchuang Date: Sat, 21 Nov 2026 21:06:02 +0800 Subject: [PATCH 3/3] test(harness): guard the actual supervisor before registration Intercept the supervisor used by Harness rather than the legacy execution wrapper in zero-start binding tests. Agent-assisted-by: Codex (author) Signed-off-by: zengchuang --- tests/test_harness_prepared_binding.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_harness_prepared_binding.py b/tests/test_harness_prepared_binding.py index fb16b9e9a..61027fac3 100644 --- a/tests/test_harness_prepared_binding.py +++ b/tests/test_harness_prepared_binding.py @@ -62,7 +62,7 @@ def forbidden(*args, **kwargs): pytest.fail("a PR-1 refusal reached registration or process execution") monkeypatch.setattr(store, "claim", forbidden) - monkeypatch.setattr(execution_service.local_process, "execute_command", forbidden) + monkeypatch.setattr(execution_service.local_process, "_supervise", forbidden) monkeypatch.setattr(execution_service, "_argv", forbidden) return spec, reader, store, harness