|
| 1 | +"""Check the unsealed Apps plan handed to the Harness generation controller.""" |
| 2 | + |
| 3 | +from copy import deepcopy |
| 4 | +from hashlib import sha256 |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +from inferrsi.apps.api import CandidateProvider |
| 10 | +from inferrsi.contracts.json_v1 import decode, encode |
| 11 | +from inferrsi.harness.errors import PublicError |
| 12 | +from inferrsi.harness.execution_service import LocalCpuTarget, validate |
| 13 | + |
| 14 | +_FIXTURES = Path(__file__).resolve().parents[1] / "docs/contracts/fixtures/t108-v4" |
| 15 | +_ATTEMPT_FIELDS = ( |
| 16 | + "request_id", "run_id", "attempt_id", "owner", "target_request", "deadline_utc", |
| 17 | + "resources", "budget", "retry_of", "retry_reason", |
| 18 | +) |
| 19 | + |
| 20 | + |
| 21 | +def _artifact(name): |
| 22 | + body = name.encode() |
| 23 | + return { |
| 24 | + "location": f"stores/test/{name}", "sha256": sha256(body).hexdigest(), |
| 25 | + "media_type": "application/octet-stream", "size_bytes": len(body), |
| 26 | + } |
| 27 | + |
| 28 | + |
| 29 | +def _fixed_input(role, ref): |
| 30 | + return { |
| 31 | + "role": role, "source": ref["location"], "source_namespace": "artifact", |
| 32 | + "source_ref": ref, "snapshot_format": "file-v1", "expected_sha256": ref["sha256"], |
| 33 | + "materialized_path": f"inputs/{role}", |
| 34 | + } |
| 35 | + |
| 36 | + |
| 37 | +@pytest.fixture |
| 38 | +def generation(): |
| 39 | + seed = decode((_FIXTURES / "execution-spec.json").read_bytes()) |
| 40 | + prepared = decode((_FIXTURES / "prepared.json").read_bytes()) |
| 41 | + context_ref, material, feedback = map(_artifact, ("context", "task", "feedback")) |
| 42 | + candidate_contract = prepared["candidate_contract"] |
| 43 | + return { |
| 44 | + "context": { |
| 45 | + "campaign_id": "campaign-1", "generation_id": "generation-1", |
| 46 | + "candidate": None, "parent": None, "task_materials": [material], |
| 47 | + "delivered_context": { |
| 48 | + "artifact": context_ref, "items": [], "selection": {}, |
| 49 | + "omissions": [], "limitations": [], |
| 50 | + }, |
| 51 | + "allowed_feedback": [feedback], "output_contract": candidate_contract, |
| 52 | + }, |
| 53 | + "attempt": {field: seed[field] for field in _ATTEMPT_FIELDS}, |
| 54 | + "provider": {"name": "cpu-example", "revision": "1"}, |
| 55 | + "output_contract": { |
| 56 | + "manifest_role": "generation-manifest", "candidate_role": "candidate", |
| 57 | + "format_schema": candidate_contract["format_schema"], |
| 58 | + "required_input_roles": ["candidate"], "parser_revision": "1", |
| 59 | + }, |
| 60 | + "inputs": [ |
| 61 | + _fixed_input("knowledge", context_ref), |
| 62 | + _fixed_input("task", material), |
| 63 | + _fixed_input("feedback", feedback), |
| 64 | + ], |
| 65 | + "steps": [{ |
| 66 | + "step_id": "generate", "runtime_role": "local-python", |
| 67 | + "entrypoint_kind": "python_module", "entrypoint": "fixture_generation", |
| 68 | + "entrypoint_revision": "1", "expected_code_sha256": None, |
| 69 | + "args": ["inputs/knowledge", "output/candidate.txt"], "cwd": ".", |
| 70 | + "timeout_seconds": None, |
| 71 | + "outputs": [ |
| 72 | + {"role": "generation-manifest", "path": "output/manifest.json", "required": True, |
| 73 | + "media_type": "application/json", |
| 74 | + "format_schema": {"name": "fixture-generation-manifest", "revision": "1"}}, |
| 75 | + {"role": "candidate", "path": "output/candidate.txt", "required": True, |
| 76 | + "media_type": "text/plain", "format_schema": candidate_contract["format_schema"]}, |
| 77 | + ], |
| 78 | + }], |
| 79 | + "required_capabilities": ["local-process-tree"], |
| 80 | + } |
| 81 | + |
| 82 | + |
| 83 | +def _provider(generation, bind_inputs=None): |
| 84 | + return CandidateProvider( |
| 85 | + provider=generation["provider"], output_contract=generation["output_contract"], |
| 86 | + steps=generation["steps"], required_capabilities=generation["required_capabilities"], |
| 87 | + bind_inputs=bind_inputs or (lambda context: generation["inputs"]), |
| 88 | + output_root_for=lambda attempt_id: f"generation/{attempt_id}", |
| 89 | + ) |
| 90 | + |
| 91 | + |
| 92 | +def test_plan_preserves_attempt_and_keeps_manifest_separate(generation, tmp_path): |
| 93 | + original = deepcopy(generation) |
| 94 | + provider = _provider(generation) |
| 95 | + result = provider.plan_generation(generation["context"], generation["attempt"]) |
| 96 | + spec, manifest = result["spec"], result["manifest"] |
| 97 | + |
| 98 | + assert {key: spec[key] for key in _ATTEMPT_FIELDS} == generation["attempt"] |
| 99 | + assert all(spec[field] is None for field in ( |
| 100 | + "generation_record_ref", "prepared_ref", "protocol_digest", "operation", |
| 101 | + "validation_scope", "candidate", |
| 102 | + )) |
| 103 | + assert spec["inputs"] == generation["inputs"] |
| 104 | + assert spec["steps"] == generation["steps"] |
| 105 | + assert spec["output_root"] == f"generation/{generation['attempt']['attempt_id']}" |
| 106 | + assert manifest == { |
| 107 | + "schema_version": 1, "provider": generation["provider"], |
| 108 | + "output_contract": generation["output_contract"], |
| 109 | + "context_refs": [generation["context"]["delivered_context"]["artifact"]], |
| 110 | + "attempt_id": generation["attempt"]["attempt_id"], |
| 111 | + } |
| 112 | + target_request = spec["target_request"] |
| 113 | + validate(spec, LocalCpuTarget( |
| 114 | + target_request["target_id"], target_request["config_revision"], tmp_path, |
| 115 | + )) |
| 116 | + assert encode(provider.plan_generation(generation["context"], generation["attempt"])) == encode( |
| 117 | + result, |
| 118 | + ) |
| 119 | + spec["inputs"][0]["source_ref"]["location"] = "other" |
| 120 | + manifest["output_contract"]["required_input_roles"].clear() |
| 121 | + assert generation == original |
| 122 | + |
| 123 | + |
| 124 | +def test_provider_binds_each_context_with_the_same_worker_configuration(generation): |
| 125 | + def bind_context(context): |
| 126 | + return [ |
| 127 | + _fixed_input("knowledge", context["delivered_context"]["artifact"]), |
| 128 | + _fixed_input("task", context["task_materials"][0]), |
| 129 | + _fixed_input("feedback", context["allowed_feedback"][0]), |
| 130 | + ] |
| 131 | + |
| 132 | + context, attempt = generation["context"], generation["attempt"] |
| 133 | + provider = _provider(generation, bind_context) |
| 134 | + first = provider.plan_generation(context, attempt) |
| 135 | + next_context = deepcopy(context) |
| 136 | + next_context["delivered_context"]["artifact"] = _artifact("another-context") |
| 137 | + next_context["allowed_feedback"] = [_artifact("another-feedback")] |
| 138 | + next_attempt = {**attempt, "attempt_id": "next-attempt", "request_id": "next-attempt"} |
| 139 | + generation["steps"][0]["entrypoint"] = "altered-after-configuration" |
| 140 | + |
| 141 | + second = provider.plan_generation(next_context, next_attempt) |
| 142 | + |
| 143 | + assert second["spec"]["steps"] == first["spec"]["steps"] |
| 144 | + assert second["spec"]["output_root"] == "generation/next-attempt" |
| 145 | + assert second["manifest"]["attempt_id"] == "next-attempt" |
| 146 | + assert second["manifest"]["context_refs"] == [next_context["delivered_context"]["artifact"]] |
| 147 | + assert second["spec"]["inputs"] == bind_context(next_context) |
| 148 | + |
| 149 | + |
| 150 | +@pytest.mark.parametrize(("path", "value"), ( |
| 151 | + pytest.param(("context", "delivered_context"), {"content": b"unsealed"}, id="draft"), |
| 152 | + pytest.param(("inputs",), [], id="missing-delivery"), |
| 153 | + pytest.param(("inputs", 2), _fixed_input("feedback", _artifact("foreign")), |
| 154 | + id="foreign-feedback"), |
| 155 | + pytest.param(("inputs", 0, "role"), "generation_plan", id="reserved-role"), |
| 156 | + pytest.param(("steps", 0, "outputs"), [], id="missing-outputs"), |
| 157 | + pytest.param(("steps", 0, "outputs", 1, "required"), False, id="optional-candidate"), |
| 158 | + pytest.param(("steps", 0, "outputs", 1, "format_schema"), {"name": "other", "revision": "1"}, |
| 159 | + id="wrong-format"), |
| 160 | + pytest.param(("steps", 0, "expected_code_sha256"), "0" * 64, id="worker-digest"), |
| 161 | + pytest.param(("attempt", "request_id"), "different-attempt", id="new-request-id"), |
| 162 | +)) |
| 163 | +def test_plan_rejects_invalid_handoff(generation, path, value): |
| 164 | + target = generation |
| 165 | + for key in path[:-1]: |
| 166 | + target = target[key] |
| 167 | + target[path[-1]] = value |
| 168 | + |
| 169 | + with pytest.raises(PublicError) as caught: |
| 170 | + _provider(generation).plan_generation(generation["context"], generation["attempt"]) |
| 171 | + assert (caught.value.code, caught.value.stage) == ("InvalidExecutionSpec", "generation_plan") |
0 commit comments