diff --git a/src/inferrsi/harness/generation.py b/src/inferrsi/harness/generation.py new file mode 100644 index 00000000..9cbe7741 --- /dev/null +++ b/src/inferrsi/harness/generation.py @@ -0,0 +1,91 @@ +"""Seal the inputs of one generation attempt before execution registration.""" + +from copy import deepcopy +from hashlib import sha256 +from pathlib import PurePosixPath + +from inferrsi.contracts.json_v1 import encode +from inferrsi.harness import run_store +from inferrsi.harness.errors import PublicError +from inferrsi.harness.execution_service import LocalCpuTarget, validate + +_MANIFEST_FIELDS = {"schema_version", "provider", "output_contract", "context_refs", "attempt_id"} +_MANIFEST_PATH = PurePosixPath("inputs/generation-plan.json") + + +def prepare_generation( + store: run_store.RunStore, target: LocalCpuTarget, *, context_provider, candidate_provider, + context_request: dict, candidate_context: dict, attempt: dict, +) -> dict: + """Collect and seal the approved context, then return the final ordinary ExecutionSpec. + + The caller saves and replays this spec through Harness.execute. Preparation does not + register an execution or allocate a new attempt. + """ + errors = run_store._attempt_errors(attempt) + if errors: + raise PublicError( + "InvalidRequest", stage="generation-seal", message="illegal generation attempt", + field_errors=errors, + ) + attempt_id = attempt["attempt_id"] + try: + draft = context_provider.collect(deepcopy(context_request)) + content = draft.content + if ( + not isinstance(content, bytes) or sha256(content).hexdigest() != draft.sha256 + or len(content) > context_request["max_bytes"] + or sum(item["delivered_bytes"] for item in draft.items) != len(content) + ): + raise ValueError("invalid generation delivery bytes") + descriptor = { + "items": draft.items, "selection": draft.selection, "omissions": draft.omissions, + "limitations": draft.limitations, + } + ref = store._seal_generation_input( + attempt_id, "delivered-context", content, descriptor=descriptor, + ) + context = {**deepcopy(candidate_context), + "delivered_context": {**deepcopy(descriptor), "artifact": ref}} + plan = candidate_provider.plan_generation(context, deepcopy(attempt)) + spec = _bind_generation_plan(store, target, attempt, plan, ref) + except (AttributeError, KeyError, TypeError, ValueError) as exc: + raise PublicError( + "InvalidRequest", stage="generation-seal", attempt_id=attempt_id, started=False, + message="invalid delivery or candidate generation plan", + ) from exc + return spec + + +def _bind_generation_plan(store, target, attempt, plan, delivery_ref): + attempt_id = attempt["attempt_id"] + manifest = plan["manifest"] + spec = deepcopy(plan["spec"]) + if ( + manifest.keys() != _MANIFEST_FIELDS or type(manifest["schema_version"]) is not int + or manifest["schema_version"] != 1 or manifest["attempt_id"] != attempt_id + or manifest["context_refs"] != [delivery_ref] + or {key: spec[key] for key in attempt} != attempt + or any(item["role"] == "generation_plan" for item in spec["inputs"]) + or not any(item["source_ref"] == delivery_ref for item in spec["inputs"]) + ): + raise ValueError("generation plan does not match its sealed context and attempt") + paths = [item["materialized_path"] for item in spec["inputs"]] + paths.extend(output["path"] for step in spec["steps"] for output in step["outputs"]) + for source in paths: + path = PurePosixPath(source.casefold()) + if path.is_relative_to(_MANIFEST_PATH) or _MANIFEST_PATH.is_relative_to(path): + raise ValueError("a generation input or output occupies the reserved plan path") + data = encode(manifest) + # The expected address is internal until the commit succeeds. + ref = run_store._generation_ref( + store.namespace, run_store.attempt_hash(attempt_id), "generation-plan", data, + ) + spec["inputs"].append({ + "role": "generation_plan", "source": ref["location"], "source_namespace": "artifact", + "source_ref": ref, "snapshot_format": "file-v1", "expected_sha256": ref["sha256"], + "materialized_path": _MANIFEST_PATH.as_posix(), + }) + validate(spec, target) + store._seal_generation_input(attempt_id, "generation-plan", data) + return spec diff --git a/src/inferrsi/harness/run_store.py b/src/inferrsi/harness/run_store.py index 11c4eb32..61efb58f 100644 --- a/src/inferrsi/harness/run_store.py +++ b/src/inferrsi/harness/run_store.py @@ -61,6 +61,14 @@ _SHA256 = re.compile(r"[0-9a-f]{64}", re.ASCII) _RECORD_REST = re.compile(r"attempts/([0-9a-f]{64})/records/([1-9][0-9]{0,15})\.json", re.ASCII) _OBJECT_REST = re.compile(r"objects/([0-9a-f]{2})/([0-9a-f]{64})", re.ASCII) +_GENERATION_REST = re.compile( + r"generation/([0-9a-f]{64})/(delivered-context|generation-plan)/([0-9a-f]{64})", re.ASCII, +) +_GENERATION_MEDIA = { + "delivered-context": "application/octet-stream", + "generation-plan": "application/json", +} +_DELIVERY_FIELDS = frozenset({"items", "selection", "omissions", "limitations"}) # Errno values meaning "another handle holds the lock" for msvcrt.locking and flock. _CONTENDED = frozenset({errno.EACCES, errno.EAGAIN, errno.EWOULDBLOCK, errno.EDEADLK}) _CHUNK = 1024 * 1024 @@ -96,6 +104,13 @@ "AND NEW.latest_record_version <= OLD.latest_record_version) " "BEGIN SELECT RAISE(ABORT, 'record versions only move forward'); END", ) +_GENERATION_SCHEMA = ( + "CREATE TABLE IF NOT EXISTS generation_inputs (" + "attempt_hash TEXT NOT NULL, role TEXT NOT NULL, sha256 TEXT NOT NULL, data BLOB NOT NULL, " + "descriptor BLOB, PRIMARY KEY (attempt_hash, role))", + "CREATE TRIGGER IF NOT EXISTS generation_inputs_no_update BEFORE UPDATE ON generation_inputs " + "BEGIN SELECT RAISE(ABORT, 'generation inputs are immutable'); END", +) class _StorageFault(Exception): @@ -138,6 +153,12 @@ def artifact_ref(namespace: str, data: bytes, media_type: str) -> dict: } +def _generation_ref(namespace: str, key: str, role: str, data: bytes) -> dict: + ref = artifact_ref(namespace, data, _GENERATION_MEDIA[role]) + ref["location"] = f"stores/{namespace}/generation/{key}/{role}/{ref['sha256']}" + return ref + + def flatten(committed: CommittedRecord, observed_at: str) -> dict: """The flat ExecutionRecord: the fixed payload plus the three outer items only (V15).""" return { @@ -755,16 +776,19 @@ def open(self, ref: Mapping) -> io.BytesIO: rest = location[len(prefix):] if inside else "" record = _RECORD_REST.fullmatch(rest) stored = _OBJECT_REST.fullmatch(rest) + generation = _GENERATION_REST.fullmatch(rest) if stored is not None and stored[2][:2] != stored[1]: stored = None - if record is None and stored is None: + if record is None and stored is None and generation is None: raise PublicError( "UnsafeArtifactPath", stage="open", - message="location is not a record or object location of this store", + message="location is not a fixed artifact location of this store", ) try: if record is not None: data = self._record_bytes(record[1], int(record[2])) + elif generation is not None: + data = self._generation_bytes(generation[1], generation[2], generation[3], ref) else: data = self._object_bytes(stored[2]) except (sqlite3.Error, OSError) as exc: @@ -783,6 +807,54 @@ def open(self, ref: Mapping) -> io.BytesIO: ) return io.BytesIO(data) + def _generation_bytes(self, key: str, role: str, digest: str, ref: Mapping) -> bytes | None: + with self._connection() as conn: + row = conn.execute( + "SELECT data FROM generation_inputs " + "WHERE attempt_hash = ? AND role = ? AND sha256 = ?", + (key, role, digest), + ).fetchone() + if row is None: + return None + if ref["sha256"] != digest or ref["media_type"] != _GENERATION_MEDIA[role]: + raise PublicError( + "ArtifactIntegrityError", stage="open", message="generation input identity differs", + ) + return bytes(row["data"]) + + def read_delivered_context(self, attempt_id: str) -> dict: + """Read the immutable delivery metadata of a prepared generation attempt.""" + if not _is_id(attempt_id): + raise PublicError("InvalidRequest", stage="read_delivery", message="invalid attempt ID") + role = "delivered-context" + key = attempt_hash(attempt_id) + try: + with self._connection() as conn: + row = conn.execute( + "SELECT sha256, data, descriptor FROM generation_inputs " + "WHERE attempt_hash = ? AND role = ?", (key, role), + ).fetchone() + except (sqlite3.Error, OSError) as exc: + raise PublicError( + "ArtifactUnavailable", stage="read_delivery", message="delivery is not accessible", + ) from exc + if row is None: + raise PublicError( + "ArtifactUnavailable", stage="read_delivery", message="delivery is not committed", + ) + data = bytes(row["data"]) + ref = _generation_ref(self.namespace, key, role, data) + try: + descriptor = json_v1.decode(bytes(row["descriptor"])) + if ref["sha256"] != row["sha256"] or not _has_fields(descriptor, _DELIVERY_FIELDS): + raise ValueError("delivery failed integrity checks") + except (TypeError, ValueError) as exc: + raise PublicError( + "ArtifactIntegrityError", stage="read_delivery", + message="invalid committed delivery", + ) from exc + return {**descriptor, "artifact": ref} + def _record_bytes(self, hashed_attempt: str, version: int) -> bytes | None: with self._connection() as conn: row = conn.execute( @@ -828,6 +900,8 @@ def __init__( "INSERT INTO meta (key, value) VALUES (?, ?)", (("format", STORE_FORMAT), ("namespace", namespace)), ) + for statement in _GENERATION_SCHEMA: + conn.execute(statement) except (sqlite3.Error, OSError) as exc: raise PublicError( "ExecutionPersistenceError", stage="open", started=False, @@ -835,6 +909,47 @@ def __init__( ) from exc super().__init__(path, namespace, busy_timeout=busy_timeout) + def _seal_generation_input( + self, attempt_id: str, role: str, data: bytes, *, descriptor: dict | None = None, + ) -> dict: + """Commit one attempt input before registering the final execution request.""" + key = attempt_hash(attempt_id) + ref = _generation_ref(self.namespace, key, role, data) + descriptor_bytes = None if descriptor is None else json_v1.encode(descriptor) + stage = "delivered-context-retention" if role == "delivered-context" else "generation-seal" + try: + with self._connection() as conn, _transaction(conn): + values = (ref["sha256"], data, descriptor_bytes) + row = conn.execute( + "SELECT sha256, data, descriptor FROM generation_inputs " + "WHERE attempt_hash = ? AND role = ?", (key, role), + ).fetchone() + if row is None: + registered = conn.execute( + "SELECT 1 FROM attempts WHERE attempt_id = ?", (attempt_id,), + ).fetchone() + if registered is not None: + raise PublicError( + "RequestConflict", stage=stage, attempt_id=attempt_id, + message="cannot seal new generation inputs after registration", + ) + conn.execute( + "INSERT INTO generation_inputs " + "(attempt_hash, role, sha256, data, descriptor) " + "VALUES (?, ?, ?, ?, ?)", (key, role, *values), + ) + elif tuple(row) != values: + raise PublicError( + "RequestConflict", stage=stage, attempt_id=attempt_id, + message="the attempt already has a different committed generation input", + ) + except (sqlite3.Error, OSError) as exc: + raise PublicError( + "ExecutionPersistenceError", stage=stage, attempt_id=attempt_id, started=False, + message="generation input commit failed", + ) from exc + return ref + def claim(self, attempt: Mapping, request_digest: str) -> Claim | CommittedRecord: """Register an attempt atomically, or apply the D021 re-entry table to its registration. diff --git a/tests/test_harness_generation_seal.py b/tests/test_harness_generation_seal.py new file mode 100644 index 00000000..94c2850a --- /dev/null +++ b/tests/test_harness_generation_seal.py @@ -0,0 +1,209 @@ +"""T38 through the real Apps providers and the CPU Harness generation path.""" + +import os +import sqlite3 +import subprocess +import sys +from contextlib import contextmanager +from copy import deepcopy +from pathlib import Path + +import pytest + +from inferrsi.apps.context_provider import ContextProvider +from inferrsi.apps.knowledge import knowledge_reader +from inferrsi.contracts.json_v1 import decode, encode +from inferrsi.harness import run_store +from inferrsi.harness.api import Harness, LocalCpuTarget +from inferrsi.harness.errors import PublicError +from inferrsi.harness.generation import prepare_generation +from tests.test_apps_generation_plan import _fixed_input, _provider +from tests.test_apps_generation_plan import generation as generation + +_KNOWLEDGE = "package:inferrsi.common.skills.cgroup_resource_controls" + + +@pytest.fixture +def cpu_generation(generation, tmp_path): + store = run_store.RunStore(tmp_path / "store", "generation") + target_request = generation["attempt"]["target_request"] + target = LocalCpuTarget(target_request["target_id"], target_request["config_revision"], + tmp_path / "work") + harness = Harness(store, target, artifact_reader=store) + attempt = harness.allocate( + run_id="t38", owner="testing", target_request=target_request, + deadline_utc="2099-01-01T00:00:00Z", resources=generation["attempt"]["resources"], + budget=generation["attempt"]["budget"], + ) + reader = knowledge_reader() + revision = reader.read(_KNOWLEDGE).revision + request = { + "query": None, "ids": [_KNOWLEDGE], "pins": [{"name": _KNOWLEDGE, "revision": revision}], + "selection_limits": {}, "max_bytes": 65536, "round_id": "round-t38", + } + context_provider = ContextProvider(reader) + draft = context_provider.collect(request) + context = deepcopy(generation["context"]) + context.pop("delivered_context") + context.update(task_materials=[], allowed_feedback=[]) + metadata = {"candidate_id": "generated-t38", "kind": "text", "input_role": "candidate", + "parent_id": None, "parent_digest": None} + step = generation["steps"][0] + worker = ( + "from pathlib import Path\n" + "from hashlib import sha256\n" + "import json\n" + "context = Path('inputs/knowledge').read_bytes()\n" + "plan = json.loads(Path('inputs/generation-plan.json').read_bytes())\n" + "assert plan['context_refs'][0]['sha256'] == sha256(context).hexdigest()\n" + f"assert plan['attempt_id'] == {attempt['attempt_id']!r}\n" + "Path('output').mkdir()\n" + f"Path('output/manifest.json').write_bytes({encode(metadata)!r})\n" + "Path('output/candidate.txt').write_bytes(context)\n" + ) + step.update(entrypoint_kind="executable", entrypoint=sys.executable, args=["-c", worker]) + + def bind_inputs(candidate_context): + sealed = candidate_context["delivered_context"] + assert store.read_delivered_context(attempt["attempt_id"]) == sealed + with store.open(sealed["artifact"]) as stream: + assert stream.read() == draft.content + return [_fixed_input("knowledge", sealed["artifact"])] + + def parse_candidate(manifest, candidate_bytes): + assert candidate_bytes == draft.content + return decode(manifest), {"candidate": "candidate"} + + provider = _provider(generation, bind_inputs, parse_candidate=parse_candidate) + return store, target, harness, draft, { + "context_provider": context_provider, "candidate_provider": provider, + "context_request": request, "candidate_context": context, "attempt": attempt, + } + + +def test_seal_commits_context_and_plan_before_cpu_execution(cpu_generation): + store, target, harness, draft, inputs = cpu_generation + assert not hasattr(draft, "artifact") + attempt_id = inputs["attempt"]["attempt_id"] + spec = prepare_generation(store, target, **inputs) + with pytest.raises(PublicError, match="NoCommittedRecord"): + harness.read(attempt_id) + manifest_ref = spec["inputs"][-1]["source_ref"] + context = store.read_delivered_context(attempt_id) + assert context["items"] == draft.items + with store.open(manifest_ref) as stream: + assert decode(stream.read())["context_refs"] == [context["artifact"]] + assert prepare_generation(store, target, **inputs) == spec + + record = harness.execute(spec) + assert record["request_digest"] == run_store.request_digest(spec) + assert record["attempt_id"] == attempt_id + assert record["exit_code"] == 0 + proposal = inputs["candidate_provider"].collect_candidate(record, artifacts=store) + assert proposal["candidate"]["context_refs"] == [context["artifact"]] + assert proposal["generation_record_ref"] == record["record_ref"] + assert harness.execute(spec)["record_ref"] == record["record_ref"] + + child = """ +import sys +from inferrsi.harness.run_store import RunStoreReader +from inferrsi.contracts.json_v1 import encode +reader = RunStoreReader(sys.argv[1], 'generation') +delivery = reader.read_delivered_context(sys.argv[2]) +sys.stdout.buffer.write(encode(delivery) + b'\\n' + reader.open(delivery['artifact']).read()) +""" + source = str(Path(__file__).resolve().parents[1] / "src") + received = subprocess.check_output( + [sys.executable, "-c", child, str(store.root), attempt_id], + env={**os.environ, "PYTHONPATH": source}, timeout=30, + ) + descriptor, data = received.split(b"\n", 1) + assert decode(descriptor) == context + assert data == draft.content + + +@pytest.mark.parametrize("after_stage", [False, True]) +def test_retention_failure_exposes_neither_context_nor_plan( + cpu_generation, monkeypatch, after_stage, +): + store, target, harness, draft, inputs = cpu_generation + attempt_id = inputs["attempt"]["attempt_id"] + prospective_ref = run_store._generation_ref( + store.namespace, run_store.attempt_hash(attempt_id), "delivered-context", draft.content, + ) + reader = run_store.RunStoreReader(store.root, store.namespace) + transact = run_store._transaction + + @contextmanager + def fault(conn, *args): + if not after_stage: + raise sqlite3.OperationalError("stop before staging") + with transact(conn, *args): + yield + with pytest.raises(PublicError, match="ArtifactUnavailable"): + reader.open(prospective_ref) + raise sqlite3.OperationalError("stop before commit") + + with monkeypatch.context() as patch: + patch.setattr(run_store, "_transaction", fault) + patch.setattr(inputs["candidate_provider"], "plan_generation", + lambda *args: pytest.fail("planner called after failed delivery")) + with pytest.raises(PublicError, match="ExecutionPersistenceError") as caught: + prepare_generation(store, target, **inputs) + assert caught.value.stage == "delivered-context-retention" + with pytest.raises(PublicError, match="ArtifactUnavailable"): + reader.open(prospective_ref) + with pytest.raises(PublicError, match="ArtifactUnavailable"): + reader.read_delivered_context(attempt_id) + with store._connection() as conn: + assert conn.execute("SELECT count(*) FROM generation_inputs").fetchone()[0] == 0 + with pytest.raises(PublicError, match="NoCommittedRecord"): + harness.read(attempt_id) + + spec = prepare_generation(store, target, **inputs) + assert spec["attempt_id"] == attempt_id + assert reader.read_delivered_context(attempt_id)["artifact"] == prospective_ref + + +def test_same_attempt_cannot_replace_committed_delivery(cpu_generation): + store, target, _, _, inputs = cpu_generation + attempt_id = inputs["attempt"]["attempt_id"] + first = prepare_generation(store, target, **inputs) + committed = store.read_delivered_context(attempt_id) + changed = {**inputs["context_request"], "max_bytes": 0} + with pytest.raises(PublicError, match="RequestConflict"): + prepare_generation(store, target, **{**inputs, "context_request": changed}) + assert store.read_delivered_context(attempt_id) == committed + assert prepare_generation(store, target, **inputs) == first + + +def test_manifest_commit_failure_keeps_delivery_without_registering_execution( + cpu_generation, monkeypatch, +): + store, target, harness, draft, inputs = cpu_generation + attempt_id = inputs["attempt"]["attempt_id"] + transact = run_store._transaction + writes = 0 + + @contextmanager + def fail_manifest(conn, *args): + nonlocal writes + writes += 1 + with transact(conn, *args): + yield + if writes == 2: + raise sqlite3.OperationalError("stop before manifest commit") + + with monkeypatch.context() as patch: + patch.setattr(run_store, "_transaction", fail_manifest) + with pytest.raises(PublicError, match="ExecutionPersistenceError") as caught: + prepare_generation(store, target, **inputs) + assert caught.value.stage == "generation-seal" + delivery = store.read_delivered_context(attempt_id) + assert delivery["items"] == draft.items + with store._connection() as conn: + sealed_roles = [row[0] for row in conn.execute("SELECT role FROM generation_inputs")] + assert sealed_roles == ["delivered-context"] + assert conn.execute("SELECT count(*) FROM attempts").fetchone()[0] == 0 + spec = prepare_generation(store, target, **inputs) + assert harness.execute(spec)["attempt_id"] == attempt_id