Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions src/inferrsi/harness/generation.py
Original file line number Diff line number Diff line change
@@ -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
119 changes: 117 additions & 2 deletions src/inferrsi/harness/run_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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:
Expand All @@ -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(
Expand Down Expand Up @@ -828,13 +900,56 @@ 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,
message=f"cannot initialize the RunStore: {_describe(exc)}",
) 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.

Expand Down
Loading
Loading