From fec204eb3d3c174c62b75473c95a49d22439cf4d Mon Sep 17 00:00:00 2001 From: amy-why-3459 Date: Wed, 18 Nov 2026 12:52:47 +0800 Subject: [PATCH 01/16] Add offline candidate trees and verification completion menus Separate editable lineage from candidate-specific verification evidence. Retain scoped attempts and dependency epochs, select complete development candidates, and enumerate bounded completion plans with shared preparation and cost caps. Cover failed-parent repair, stale evidence, deterministic selection and planner feasibility with CPU controls; document the shared execution integration boundary. Agent-assisted-by: Codex (implementation, tests and documentation) Signed-off-by: amy-why-3459 --- docs/architecture.md | 5 + docs/operations.md | 1 + docs/speculative-search.md | 140 ++++++++ src/inferrsi/harness/search_tree.py | 361 +++++++++++++++++++++ src/inferrsi/harness/verification_plans.py | 145 +++++++++ tests/fixtures/speculative_search.py | 67 ++++ tests/test_speculative_search.py | 263 +++++++++++++++ tests/test_verification_plans.py | 151 +++++++++ 8 files changed, 1133 insertions(+) create mode 100644 docs/speculative-search.md create mode 100644 src/inferrsi/harness/search_tree.py create mode 100644 src/inferrsi/harness/verification_plans.py create mode 100644 tests/fixtures/speculative_search.py create mode 100644 tests/test_speculative_search.py create mode 100644 tests/test_verification_plans.py diff --git a/docs/architecture.md b/docs/architecture.md index 6b87670a1..722d7fa87 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -32,6 +32,11 @@ The optional [service-reuse protocol](service-reuse-campaigns.md) adds project assessment and foreground service ownership within this same controller. Its protocol and result rules live in `harness.project_service`, with process lifetime managed by `local_process`. +The [offline speculative-search primitives](speculative-search.md) add immutable +candidate trees, scoped evidence transitions and bounded completion menus in +Harness. They expose pure control operations for future VATS assembly; the +existing session controller continues to own execution. Production adapter +integration, learned role selection and GVO remain separate delivery work. The [native host handoff](native-agent-sessions.md) lets the initiating Agent resolve the task request before any campaign exists, use its host's new-session tool and retain the native identity and returned artifacts. The child owns the diff --git a/docs/operations.md b/docs/operations.md index 8a985d9be..f07bcf930 100644 --- a/docs/operations.md +++ b/docs/operations.md @@ -70,6 +70,7 @@ Effect classes used below: | Qualify an artifact verifier host | [Manual reviewer qualification](skill-evaluation/skill-eval-reviewer-qualification.md), `python -m tools.skill_eval_qualify` | Trusted-control execution / report-generating | Manual main-only workflow; fixed software controls, no model or external artifact input; unavailable isolation fails rather than skips | | Replay a cloud-generated parser pair | [Isolated cloud replay](skill-evaluation/skill-eval-cloud-replay.md), `python -m tools.skill_eval_cloud_replay` | Untrusted-artifact execution / report-generating | Manual pinned data ingress; same-host controls before bubblewrap-only replay; no models or credentials | | Validate one harness-improvement step | [Offline improvement](harness-improvement.md), Python `tools.skill_eval_improve.prepare` / `run_offline` | Snapshot-writing / offline experiment-executing | Supplied declarative INV/DOM edits, shared total limits, rerun and one holdout gate; trusted fixed-output clients only; no real-model or Store execution | +| Build an offline candidate tree and completion menu | Python `harness.search_tree` / `harness.verification_plans.completion_menu`; [control contract](speculative-search.md) | Read-only / in-memory transitions | Immutable candidates, scoped caller-supplied receipts and bounded exact plans; execution, durable state, accounting, learned roles and final acceptance require separate adapters | | Plan and report skill PR evaluations | [Skill PR integration](skill-eval-pr.md), `python -m tools.skill_eval_pr` | Read-only planning / authorized experiment-executing / PR-publishing | Trusted mapping, exact operator approval, consumed execution identity and credential-separated publication from retained evidence; no automatic paid trials | | Recover goal progress and the next proposal | [Offline goal checkpoints](goal-checkpoints.md), `python -m tools.goal_checkpoint` | Read-only | Explicit scope/criteria, pinned owner decisions and existing comparison receipts; a disposable view, never execution or acceptance authority | | Summarize campaign gains | `tools/campaign_summary_chart.py`; `report`/`run` figure | Figure-generating | Renders recorded values into one gain figure plus a matching table; validates the specification and computes nothing new | diff --git a/docs/speculative-search.md b/docs/speculative-search.md new file mode 100644 index 000000000..96d6e38a5 --- /dev/null +++ b/docs/speculative-search.md @@ -0,0 +1,140 @@ +# Offline candidate trees and verification plans + +This is the first implementation slice of +[the SpecRSI proposal](https://github.com/InferMatrixLab/InferRSI/issues/363). +`harness/search_tree.py` provides immutable candidate and evidence transitions; +`harness/verification_plans.py` constructs bounded completion menus. Both are +pure Python control primitives with no execution, storage or model access. +The existing project and benchmark session commands retain their current behavior. + +The design follows [SpecRSI v2, Section 3](https://arxiv.org/html/2610.16491v2#S3): +editing an incomplete or unsuccessful parent is allowed, while adopting a version +requires complete admissible evidence for that version. Completion bundles keep +complementary checks together. This slice supplies building blocks for the inner +search; a learned Scorer/Policy, automatic Drafter and GVO updates remain future +integration work. + +## State and evidence + +`Contract` freezes the search identity, verification context, required checks, +objective, direction, improver snapshot and candidate/depth/check-attempt caps. +The context identifies the plan, validator, environment, workload, original +baseline and quality reference. Checks may encode separately required repetitions. +The current selector supports a scalar objective plus hard PASS requirements; +the Skill lane's quality-first lexicographic selector needs a separate adapter. + +`initialize` requires one complete, passing receipt per root check. Root receipts +count against the attempt cap. Their acquisition/reuse cost must already be in +the caller's authoritative accounting. A passing objective check carries a finite +value; other checks and rejected/inconclusive outcomes cannot supply a value. + +`add_candidate` records an immutable artifact digest and edit parent, with the +frozen producer snapshot, producer revision, context/history digests and actual +generation dependencies. Callers retain and verify artifacts with the existing +`harness.candidates.retain` / `identity` helpers before admission. This core does +not authenticate digests or decide whether an edit was permitted. Parent quality +failures do not invalidate the edit lineage. Real generation-source dependencies +are inherited and must still be current; a child cannot drop them to escape an +invalidation. Rebase or composition is a new candidate with its own evidence. +This offline core predeclares dependency keys at initialization; introducing new +source authorities dynamically requires later controller integration. + +`register_attempt` binds a caller-owned attempt/execution key, reservation and +lease to one candidate, check and complete verification context. Registering an +attempt grants no execution permission. Unknown work on the same check cannot be +replaced, even after dependency invalidation. Valid outcomes cannot be resampled +until they pass; an INCONCLUSIVE outcome may be retried with a fresh identity +under the caller's frozen retry rules and a new reservation. + +`record_receipt` requires the entire registered binding. Identical delivery is +idempotent and conflicting delivery is rejected. History retains late results; +`evidence` admits only the latest attempt with current dependency epochs and +valid candidate provenance. FAIL is an observed outcome, whereas INCONCLUSIVE +leaves a check missing. Identical candidate bytes do not transfer evidence. + +`accept` selects only fully passing candidates, preferring the root on an +objective tie and otherwise using lexicographic candidate ID order. The result is +a **development selection**. Independent final/holdout acceptance and production +promotion remain caller-owned. `verified_gain` measures this selection relative +to the root. Invalidation recomputes eligibility; invalid root evidence blocks +acceptance and further drafting/check registration, while late archival remains +available. + +## Completion planning + +`completion_menu(state, jobs, available_budget=..., available_jobs=..., +max_targets=..., max_jobs=...)` takes a supplied integer-cost budget view and +returns immutable `Plan` values. The caller has already accounted for planning, +generation, final reserve, in-flight obligations and unknown spend when deriving +that view. Costs must be known positive conservative bounds in the same unit. + +Each `Job` names either a candidate/check pair or a shared preparation step. +Prerequisites form an acyclic graph. The solver takes each candidate's complete +missing-check set, closes its prerequisites, and enumerates unions of at most +`max_targets` candidate seeds. Every feasible singleton completion remains in +the menu. Shared jobs are charged once per plan, conflicts exclude combinations, +and available job slots and remaining check attempts bound each proposal. +`max_jobs` bounds the input graph; enumeration is intended for small offline +controls, not an unbounded production optimizer. No Scorer prediction is used +as evidence or as a substitute for a completion seed. + +Plans bind the search ID and state revision and carry a deterministic topological +job order. The caller retains the full job definitions and costs with its frozen +plan; job IDs alone do not authenticate them. The adapter rebuilds job +relations from current observations at each synchronous decision boundary; +already observed checks are not silently scheduled again. An in-flight attempt, +including an invalidated one, blocks replanning until its execution is settled. +An empty menu is a valid outcome under a tight budget; the root remains available +when its evidence is valid. Feasibility says nothing about whether checks pass. + +## Shared execution and persistence boundary + +These modules introduce no second runner, budget ledger, holdout tracker or +storage backend. A future application adapter must: + +1. Read current authoritative state and the frozen contract, settle the previous + work block, and supply a conservative available-budget view. +2. Generate and retain permitted edits, or select an affordable check plan using + the frozen roles. Account for generation, planning and failed work. +3. Use shared execution controls for atomic claim/reservation, absolute deadline, + resource isolation and actual execution. Bind their identities to the tree. +4. Authenticate and assess receipts, apply a transition against the expected + search revision, and persist it using the authoritative storage CAS/lock. + A pure function's revision check is **not a durable or cross-worker CAS**. + On conflict, reread and recompute; an ambiguous claim never grants a retry. +5. Persist real dependency invalidation before cancellation. Reconcile costs and + retain leases until process cleanup is confirmed. Receipt archival itself + neither charges again nor releases a resource. +6. Freeze the selected version for independent final acceptance, with holdout + access and interruptions tracked separately from development feedback. + +There is no production adapter in this slice. In particular, local immutable +values and revision checks do not establish crash recovery, external exactly-once, +multi-worker resource isolation, budget transactions or holdout non-reuse. + +## Verification and remaining delivery + +Run the CPU controls with: + +```bash +python -m pytest tests/test_speculative_search.py tests/test_verification_plans.py +``` + +Tests cover retained file/directory artifacts for synthetic Engine/Skill shapes, +failed-parent repair, incomplete ancestors, failure versus missing evidence, +dependency/result order, identical bytes with separate attempts, stale revision +rejection, immutable role snapshots, deterministic selection, invalid roots and +bounded search. The planner is compared with an independent exhaustive job-subset +oracle across budgets and target limits, including shared preparation and conflicts. + +| Issue acceptance area | This slice | Remaining evidence | +| --- | --- | --- | +| AC-01/02 | Frozen scalar contract and offline candidate tree | Production support matrix and role/execution adapters | +| AC-03/05 | Scoped evidence, epoch invalidation, development selection and revision guards | Authoritative durable state integration and restart/race tests | +| AC-04 | Bounded exact completion menu and exhaustive controls | Scorer refinement, Policy selection and scalable solver integration | +| AC-06 | Frozen snapshot identity only | GVO data collection, objectives, parameter updates and new searches | +| AC-07/08 | References to caller-owned execution identities | Actual budget/resource transactions, cancellation and independent final/holdout | +| AC-09/10 | Synthetic control fixtures | Both real adapters, model generation, training, matched experiments and lifecycle costs | + +These tests do not establish model quality, engine speedup or SpecRSI efficiency. +The parent issue remains open for its full dual-loop delivery. diff --git a/src/inferrsi/harness/search_tree.py b/src/inferrsi/harness/search_tree.py new file mode 100644 index 000000000..d6623835f --- /dev/null +++ b/src/inferrsi/harness/search_tree.py @@ -0,0 +1,361 @@ +"""Pure VATS candidate/evidence transitions; execution and durable CAS belong to the caller. + +Inputs are trusted controller records, not authenticated executor messages. This module +neither starts work nor grants a claim, releases a lease, spends budget or promotes a +production version. See docs/speculative-search.md for the adapter boundary. +""" + +from dataclasses import dataclass, fields, replace +from math import isfinite + +from inferrsi.contracts.claim import Verdict + + +def _text(value): + if not isinstance(value, str) or not value.strip(): + raise ValueError("Identity must be a nonempty string") + + +def _integer(value, minimum=0): + if type(value) is not int or value < minimum: + raise ValueError("Expected a bounded nonnegative integer") + + +def _sha(value): + if not isinstance(value, str) or len(value) != 64 or set(value) - set("0123456789abcdef"): + raise ValueError("Expected a lowercase SHA-256 digest") + + +@dataclass(frozen=True) +class Context: + contract: str + plan: str + validator: str + environment: str + workload: str + baseline: str + quality_reference: str + + def __post_init__(self): + for field in fields(self): + _text(getattr(self, field.name)) + + +@dataclass(frozen=True, order=True) +class Dependency: + key: str + epoch: int + + def __post_init__(self): + _text(self.key) + _integer(self.epoch) + + +def _dependencies(values): + values = tuple(values) + if any(not isinstance(value, Dependency) for value in values): + raise ValueError("Expected dependency identities") + if len({value.key for value in values}) != len(values): + raise ValueError("Duplicate dependency key") + return tuple(sorted(values)) + + +@dataclass(frozen=True) +class Producer: + snapshot: str + revision: str + context_digest: str + history_digest: str + + def __post_init__(self): + _text(self.snapshot) + _text(self.revision) + _sha(self.context_digest) + _sha(self.history_digest) + + +@dataclass(frozen=True) +class Contract: + search_id: str + context: Context + improver_snapshot: str + checks: tuple[str, ...] + objective_check: str + direction: str + max_candidates: int + max_depth: int + max_attempts: int + + def __post_init__(self): + _text(self.search_id) + _text(self.improver_snapshot) + if not isinstance(self.context, Context): + raise ValueError("Expected verification context") + if not isinstance(self.checks, (tuple, list)): + raise ValueError("Required checks must be an ordered sequence") + checks = tuple(self.checks) + for check in checks: + _text(check) + if not checks or len(set(checks)) != len(checks) or self.objective_check not in checks: + raise ValueError("Required checks must be unique and include the objective") + object.__setattr__(self, "checks", checks) + if self.direction not in ("maximize", "minimize"): + raise ValueError("Unknown objective direction") + for value in (self.max_candidates, self.max_depth, self.max_attempts): + _integer(value, 1) + + +@dataclass(frozen=True) +class Candidate: + candidate_id: str + artifact_digest: str + parent_id: str | None + producer: Producer + dependencies: tuple[Dependency, ...] = () + + def __post_init__(self): + _text(self.candidate_id) + _sha(self.artifact_digest) + if self.parent_id is not None: + _text(self.parent_id) + if not isinstance(self.producer, Producer): + raise ValueError("Expected producer identity") + object.__setattr__(self, "dependencies", _dependencies(self.dependencies)) + + +@dataclass(frozen=True) +class Attempt: + attempt_id: str + execution_key: str + reservation_id: str + lease_id: str + search_id: str + candidate_id: str + artifact_digest: str + check_id: str + context: Context + dependencies: tuple[Dependency, ...] = () + + def __post_init__(self): + for name in ("attempt_id", "execution_key", "reservation_id", "lease_id", "search_id", + "candidate_id", "check_id"): + _text(getattr(self, name)) + _sha(self.artifact_digest) + if not isinstance(self.context, Context): + raise ValueError("Expected verification context") + object.__setattr__(self, "dependencies", _dependencies(self.dependencies)) + + +@dataclass(frozen=True) +class Receipt: + attempt: Attempt + verdict: Verdict + evidence_digest: str + objective: float | None = None + + def __post_init__(self): + if not isinstance(self.attempt, Attempt) or not isinstance(self.verdict, Verdict): + raise ValueError("Expected bound attempt and verdict") + _sha(self.evidence_digest) + if self.objective is not None and ( + type(self.objective) not in (int, float) or not isfinite(self.objective) + ): + raise ValueError("Objective must be a finite number, not missing-value imputation") + if self.verdict != Verdict.PASS and self.objective is not None: + raise ValueError("Rejected or inconclusive evidence cannot supply an objective") + + +@dataclass(frozen=True) +class SearchState: + contract: Contract + root_id: str + candidates: tuple[Candidate, ...] + attempts: tuple[Attempt, ...] + receipts: tuple[Receipt, ...] + dependencies: tuple[Dependency, ...] + revision: int = 0 + + +def candidate(state, candidate_id): + for value in state.candidates: + if value.candidate_id == candidate_id: + return value + raise ValueError("Unknown candidate") + + +def _current(state, dependencies): + epochs = {item.key: item.epoch for item in state.dependencies} + return all(epochs.get(item.key) == item.epoch for item in dependencies) + + +def candidate_valid(state, candidate_id): + return _current(state, candidate(state, candidate_id).dependencies) + + +def _binding(state, attempt): + node = candidate(state, attempt.candidate_id) + if (attempt.search_id != state.contract.search_id + or attempt.artifact_digest != node.artifact_digest + or attempt.context != state.contract.context + or attempt.check_id not in state.contract.checks + or not set(node.dependencies).issubset(attempt.dependencies)): + raise ValueError("Attempt does not bind this candidate and verification contract") + + +def latest_attempt(state, candidate_id, check_id): + return next((item for item in reversed(state.attempts) + if (item.candidate_id, item.check_id) == (candidate_id, check_id)), None) + + +def evidence(state, candidate_id): + """Current, scoped outcomes, including FAIL; INCONCLUSIVE remains missing.""" + if not candidate_valid(state, candidate_id): + return {} + receipts = {item.attempt.attempt_id: item for item in state.receipts} + result = {} + for check in state.contract.checks: + attempt = latest_attempt(state, candidate_id, check) + if attempt is None or not _current(state, attempt.dependencies): + continue + receipt = receipts.get(attempt.attempt_id) + if receipt is not None and receipt.verdict != Verdict.INCONCLUSIVE: + result[check] = receipt + return result + + +def missing_checks(state, candidate_id): + observed = evidence(state, candidate_id) + return tuple(check for check in state.contract.checks if check not in observed) + + +def complete_candidates(state): + result = [] + for node in state.candidates: + records = evidence(state, node.candidate_id) + if len(records) == len(state.contract.checks) and all( + record.verdict == Verdict.PASS for record in records.values() + ): + result.append(node) + return tuple(result) + + +def accept(state): + """Select a development candidate only; the root wins ties, then stable ID order.""" + complete = complete_candidates(state) + if state.root_id not in {item.candidate_id for item in complete}: + raise ValueError("Search root no longer has complete admissible evidence") + sign = -1 if state.contract.direction == "maximize" else 1 + return min(complete, key=lambda item: ( + sign * evidence(state, item.candidate_id)[state.contract.objective_check].objective, + item.candidate_id != state.root_id, item.candidate_id, + )) + + +def verified_gain(state): + selected = accept(state) + check = state.contract.objective_check + gain = (evidence(state, selected.candidate_id)[check].objective + - evidence(state, state.root_id)[check].objective) + return gain if state.contract.direction == "maximize" else -gain + + +def _receipt_shape(state, receipt): + objective = receipt.attempt.check_id == state.contract.objective_check + if (receipt.verdict == Verdict.PASS and objective) != (receipt.objective is not None): + raise ValueError("Only a passing objective check must supply an objective") + + +def initialize(contract, root, receipts, *, dependencies=()): + """Import previously acquired root evidence; its execution/cost remains caller-owned.""" + receipts = tuple(receipts) + if root.parent_id is not None: + raise ValueError("Search root cannot have an edit parent in this search") + state = SearchState(contract, root.candidate_id, (root,), + tuple(item.attempt for item in receipts), receipts, + _dependencies(dependencies)) + if len(receipts) != len(contract.checks) or len(receipts) > contract.max_attempts: + raise ValueError("Root requires exactly one receipt per required check within the cap") + if len({item.attempt.check_id for item in receipts}) != len(receipts): + raise ValueError("Duplicate root check") + for field in ("attempt_id", "execution_key"): + if len({getattr(item.attempt, field) for item in receipts}) != len(receipts): + raise ValueError("Root execution identities must be unique") + for receipt in receipts: + _binding(state, receipt.attempt) + _receipt_shape(state, receipt) + accept(state) + return state + + +def _guard(state, expected_revision, *, require_root=True): + _integer(expected_revision) + if state.revision != expected_revision: + raise ValueError("Stale search revision; reread and recompute before storage CAS") + if require_root: + accept(state) + + +def add_candidate(state, node, *, expected_revision): + _guard(state, expected_revision) + if len(state.candidates) >= state.contract.max_candidates: + raise ValueError("Candidate limit exhausted") + if any(item.candidate_id == node.candidate_id for item in state.candidates): + raise ValueError("Candidate identity is immutable") + parent = candidate(state, node.parent_id) + if (node.producer.snapshot != state.contract.improver_snapshot + or not _current(state, node.dependencies) + or not set(parent.dependencies).issubset(node.dependencies)): + raise ValueError("Invalid generation dependencies or changed improver snapshot") + depth = 1 + while parent.parent_id is not None: + parent = candidate(state, parent.parent_id) + depth += 1 + if depth > state.contract.max_depth: + raise ValueError("Draft depth limit exhausted") + return replace(state, candidates=(*state.candidates, node), revision=state.revision + 1) + + +def register_attempt(state, attempt, *, expected_revision): + """Record a caller-owned claim. A return value is NOT permission to execute.""" + _guard(state, expected_revision) + _binding(state, attempt) + if not _current(state, attempt.dependencies): + raise ValueError("Attempt depends on invalidated evidence or generation sources") + if len(state.attempts) >= state.contract.max_attempts: + raise ValueError("Check attempt limit exhausted") + if any(item.attempt_id == attempt.attempt_id or item.execution_key == attempt.execution_key + for item in state.attempts): + raise ValueError("Attempt and execution identities cannot be reused") + previous = latest_attempt(state, attempt.candidate_id, attempt.check_id) + if previous is not None: + receipt = next((item for item in state.receipts if item.attempt == previous), None) + if receipt is None: + raise ValueError("Unsettled attempt cannot be replaced or implicitly replayed") + if _current(state, previous.dependencies) and receipt.verdict != Verdict.INCONCLUSIVE: + raise ValueError("A valid outcome cannot be retried until it passes") + return replace(state, attempts=(*state.attempts, attempt), revision=state.revision + 1) + + +def record_receipt(state, receipt, *, expected_revision): + """Retain late receipts as history; evidence() separately decides current admission.""" + _guard(state, expected_revision, require_root=False) + if receipt.attempt not in state.attempts: + raise ValueError("Receipt does not match a registered attempt") + _receipt_shape(state, receipt) + previous = next((item for item in state.receipts + if item.attempt.attempt_id == receipt.attempt.attempt_id), None) + if previous is not None: + if previous != receipt: + raise ValueError("Conflicting receipt for an immutable attempt") + return state + return replace(state, receipts=(*state.receipts, receipt), revision=state.revision + 1) + + +def invalidate(state, dependency_key, *, expected_revision): + """Invalidate a real source, not a parent quality outcome; no cancel/release is issued.""" + _guard(state, expected_revision, require_root=False) + if dependency_key not in {item.key for item in state.dependencies}: + raise ValueError("Unknown dependency") + dependencies = tuple(replace(item, epoch=item.epoch + 1) if item.key == dependency_key + else item for item in state.dependencies) + return replace(state, dependencies=dependencies, revision=state.revision + 1) diff --git a/src/inferrsi/harness/verification_plans.py b/src/inferrsi/harness/verification_plans.py new file mode 100644 index 000000000..81d39f862 --- /dev/null +++ b/src/inferrsi/harness/verification_plans.py @@ -0,0 +1,145 @@ +"""Bounded exact completion-menu construction for offline VATS controls. + +Costs are caller-supplied conservative integer bounds in one declared unit. A plan +is a proposal against a budget view, never a resource reservation or execution grant. +""" + +from dataclasses import dataclass +from itertools import combinations + +from inferrsi.contracts.claim import Verdict + +from .search_tree import accept, candidate_valid, evidence, missing_checks + + +@dataclass(frozen=True) +class Job: + job_id: str + cost: int + candidate_id: str | None = None + check_id: str | None = None + prerequisites: tuple[str, ...] = () + conflicts: tuple[str, ...] = () + + def __post_init__(self): + if not isinstance(self.job_id, str) or not self.job_id.strip(): + raise ValueError("Job identity must be nonempty") + if type(self.cost) is not int or self.cost <= 0: + raise ValueError("Job cost must be a known positive integer upper bound") + if (self.candidate_id is None) != (self.check_id is None): + raise ValueError("A check job needs both candidate and check identities") + for value in (self.candidate_id, self.check_id): + if value is not None and (not isinstance(value, str) or not value.strip()): + raise ValueError("Check identity must be nonempty") + for name in ("prerequisites", "conflicts"): + if not isinstance(getattr(self, name), (tuple, list)): + raise ValueError("Job relations must be ordered sequences") + values = tuple(getattr(self, name)) + if (any(not isinstance(value, str) or not value.strip() for value in values) + or len(set(values)) != len(values) or self.job_id in values): + raise ValueError("Job relations must be unique, nonempty, non-self identities") + object.__setattr__(self, name, values) + + +@dataclass(frozen=True) +class Plan: + candidate_ids: tuple[str, ...] + job_ids: tuple[str, ...] + cost: int + search_id: str + state_revision: int + + +def completion_menu(state, jobs, *, available_budget, available_jobs, max_targets, max_jobs): + """Enumerate unions of up to max_targets completion seeds, retaining every feasible seed. + +The caller must settle a synchronous work block first. Preparation jobs have no +candidate/check pair. Shared prerequisites are charged once per proposed plan; +conflicts prohibit co-scheduling even when only one side declares the relation. +""" + for value, minimum in ((available_budget, 0), (available_jobs, 0), + (max_targets, 1), (max_jobs, 1)): + if type(value) is not int or value < minimum: + raise ValueError("Menu limits and available budget must be known integers") + accept(state) + receipts = {item.attempt.attempt_id for item in state.receipts} + # An invalidated in-flight job is still real work; only its owner can settle it. + if any(item.attempt_id not in receipts for item in state.attempts): + raise ValueError("Settle the current work block before constructing another menu") + jobs = tuple(jobs) + if len(jobs) > max_jobs: + raise ValueError("Job menu exceeds the declared enumeration bound") + by_id = {job.job_id: job for job in jobs} + if len(by_id) != len(jobs): + raise ValueError("Duplicate job identity") + checks = {} + for job in jobs: + if (set(job.prerequisites) | set(job.conflicts)) - by_id.keys(): + raise ValueError("Unknown job relation") + if job.candidate_id is not None: + candidate_valid(state, job.candidate_id) # Reject unknown candidate IDs. + if job.check_id not in state.contract.checks: + raise ValueError("Unknown required check") + pair = (job.candidate_id, job.check_id) + if pair in checks: + raise ValueError("Ambiguous jobs for one candidate check") + checks[pair] = job.job_id + + closures = {} + + def closure(job_id, visiting=frozenset()): + if job_id in visiting: + raise ValueError("Cyclic job prerequisites") + if job_id not in closures: + result = {job_id} + for parent in by_id[job_id].prerequisites: + result.update(closure(parent, visiting | {job_id})) + closures[job_id] = frozenset(result) + return closures[job_id] + + for job in jobs: + closure(job.job_id) + + seeds = {} + for node in state.candidates: + cid = node.candidate_id + if not candidate_valid(state, cid) or any( + item.verdict == Verdict.FAIL for item in evidence(state, cid).values() + ): + continue + missing = missing_checks(state, cid) + if not missing or any((cid, check) not in checks for check in missing): + continue + seeds[cid] = frozenset().union(*(closure(checks[(cid, check)]) for check in missing)) + + plans = {} + remaining_attempts = state.contract.max_attempts - len(state.attempts) + for size in range(1, min(max_targets, len(seeds)) + 1): + for targets in combinations(sorted(seeds), size): + union = frozenset().union(*(seeds[cid] for cid in targets)) + if len(union) > available_jobs: + continue + check_jobs = [by_id[jid] for jid in union if by_id[jid].candidate_id is not None] + if len(check_jobs) > remaining_attempts: + continue + # A cross-candidate prerequisite must itself be a legal outstanding check. + if any(job.check_id not in missing_checks(state, job.candidate_id) + or not candidate_valid(state, job.candidate_id) for job in check_jobs): + continue + if any(set(by_id[jid].conflicts) & union for jid in union): + continue + cost = sum(by_id[jid].cost for jid in union) + if cost > available_budget: + continue + # Stable topological order, so an adapter never receives a child before setup. + ordered = [] + remaining = set(union) + while remaining: + ready = sorted(jid for jid in remaining + if not (set(by_id[jid].prerequisites) & remaining)) + ordered.extend(ready) + remaining.difference_update(ready) + completed = tuple(sorted(cid for cid, seed in seeds.items() if seed <= union)) + plan = Plan(completed, tuple(ordered), cost, state.contract.search_id, state.revision) + plans[plan.job_ids] = plan + return tuple(sorted(plans.values(), key=lambda plan: (plan.cost, plan.job_ids))) diff --git a/tests/fixtures/speculative_search.py b/tests/fixtures/speculative_search.py new file mode 100644 index 000000000..104938206 --- /dev/null +++ b/tests/fixtures/speculative_search.py @@ -0,0 +1,67 @@ +"""Synthetic observations and identities; no model, hardware or billing evidence.""" + +from dataclasses import replace +from hashlib import sha256 + +from inferrsi.contracts.claim import Verdict +from inferrsi.harness import search_tree as tree + + +def digest(text): + return sha256(text.encode()).hexdigest() + + +def contract(**changes): + return replace(tree.Contract( + "search", tree.Context("contract", "plan", "grader", "cpu-fixture", "development", + "original-baseline", "quality-reference"), + "improver-0", ("quality", "objective"), "objective", "maximize", 20, 5, 40, + ), **changes) + + +def producer(snapshot="improver-0"): + return tree.Producer(snapshot, "synthetic-producer-1", digest("context"), digest("history")) + + +def attempt(state, cid, check, *, dependencies=(), tag=None): + node = tree.candidate(state, cid) + tag = tag or str(len(state.attempts)) + return tree.Attempt( + f"attempt-{tag}", f"execution-{tag}", f"reservation-{tag}", f"lease-{tag}", + state.contract.search_id, cid, node.artifact_digest, check, state.contract.context, + tuple(sorted(set(node.dependencies) | set(dependencies))), + ) + + +def receipt(binding, verdict=Verdict.PASS, objective=None): + return tree.Receipt(binding, verdict, digest(f"receipt:{binding.attempt_id}"), objective) + + +def start(*, configuration=None, dependencies=(), root_dependencies=(), score=1): + configuration = configuration or contract() + root = tree.Candidate("root", digest("root"), None, producer(), root_dependencies) + seed = tree.SearchState(configuration, "root", (root,), (), (), tuple(dependencies)) + receipts = tuple(receipt(attempt(seed, "root", check, tag=f"root-{check}"), + objective=score if check == configuration.objective_check else None) + for check in configuration.checks) + return tree.initialize(configuration, root, receipts, dependencies=dependencies) + + +def add(state, cid, parent="root", *, dependencies=None, artifact_digest=None, snapshot=None): + node = tree.Candidate(cid, artifact_digest or digest(cid), parent, + producer(snapshot or state.contract.improver_snapshot), + tree.candidate(state, parent).dependencies if dependencies is None + else dependencies) + return tree.add_candidate(state, node, expected_revision=state.revision) + + +def observe(state, cid, check, verdict=Verdict.PASS, objective=None, *, dependencies=()): + binding = attempt(state, cid, check, dependencies=dependencies) + state = tree.register_attempt(state, binding, expected_revision=state.revision) + return tree.record_receipt(state, receipt(binding, verdict, objective), + expected_revision=state.revision) + + +def complete(state, cid, score): + state = observe(state, cid, "quality") + return observe(state, cid, "objective", objective=score) diff --git a/tests/test_speculative_search.py b/tests/test_speculative_search.py new file mode 100644 index 000000000..997e95f5b --- /dev/null +++ b/tests/test_speculative_search.py @@ -0,0 +1,263 @@ +from dataclasses import FrozenInstanceError, fields, replace + +import pytest + +from inferrsi.contracts.claim import Verdict +from inferrsi.harness import candidates +from inferrsi.harness import search_tree as tree +from tests.fixtures.speculative_search import ( + add, + attempt, + complete, + contract, + digest, + observe, + producer, + receipt, + start, +) + + +@pytest.mark.parametrize("lane,filename", [("engine", "kernel.py"), ("skill", "SKILL.md")]) +def test_failed_parent_can_be_repaired_with_its_own_snapshot_and_complete_evidence( + tmp_path, lane, filename, +): + # File retention is real; grades are synthetic controls, not either production adapter. + work = tmp_path / "work" + work.mkdir() + source = work / filename + state = start(configuration=contract(search_id=lane)) + for cid, value, parent in (("broken", -1, "root"), ("repaired", 3, "broken")): + source.write_text(str(value)) + retained = tmp_path / cid + identity = candidates.retain(work, retained) + state = add(state, cid, parent, artifact_digest=identity) + actual = int((retained / filename).read_text()) + state = observe(state, cid, "quality", Verdict.PASS if actual > 0 else Verdict.FAIL) + if actual > 0: + # A high objective alone never compensates for missing or failed quality. + state = observe(state, cid, "objective", objective=actual) + source.write_text("999") + assert candidates.identity(tmp_path / "repaired") == tree.accept(state).artifact_digest + assert tree.accept(state).candidate_id == "repaired" + assert tree.verified_gain(state) == 2 + assert tree.missing_checks(state, "broken") == ("objective",) + assert tree.evidence(state, "broken")["quality"].verdict == Verdict.FAIL + assert tree.candidate_valid(state, "broken") + + +def test_incomplete_parent_can_be_extended_without_fabricating_parent_results(): + state = add(add(start(), "parent"), "child", "parent") + state = complete(state, "child", 4) + assert tree.accept(state).candidate_id == "child" + assert tree.evidence(state, "parent") == {} + + +def test_objective_prediction_partial_results_and_failed_quality_cannot_win(): + state = add(start(), "candidate") + state = observe(state, "candidate", "objective", objective=1000) + assert tree.accept(state).candidate_id == "root" + state = observe(state, "candidate", "quality", Verdict.FAIL) + assert tree.missing_checks(state, "candidate") == () # Known failure is not missing. + assert tree.accept(state).candidate_id == "root" + assert tree.verified_gain(state) == 0 + + +@pytest.mark.parametrize("direction", ["maximize", "minimize"]) +@pytest.mark.parametrize("order", [("z", "a"), ("a", "z")]) +def test_selection_is_independent_of_receipt_order_and_root_wins_ties(direction, order): + state = start(configuration=contract(direction=direction), score=10) + for cid in order: + state = complete(add(state, cid), cid, 11 if direction == "maximize" else 9) + assert tree.accept(state).candidate_id == "a" + assert tree.verified_gain(state) == 1 + tied = complete(add(start(score=10), "a"), "a", 10) + assert tree.accept(tied).candidate_id == "root" + + +@pytest.mark.parametrize("late", [True, False]) +def test_real_dependency_invalidation_and_success_race_cannot_leave_stale_incumbent(late): + dependency = tree.Dependency("measurement-source", 0) + state = add(start(dependencies=(dependency,)), "candidate") + state = observe(state, "candidate", "quality") + binding = attempt(state, "candidate", "objective", dependencies=(dependency,)) + state = tree.register_attempt(state, binding, expected_revision=state.revision) + result = receipt(binding, objective=5) + if not late: + state = tree.record_receipt(state, result, expected_revision=state.revision) + assert tree.accept(state).candidate_id == "candidate" + state = tree.invalidate(state, dependency.key, expected_revision=state.revision) + if late: + state = tree.record_receipt(state, result, expected_revision=state.revision) + assert result in state.receipts # Archive is distinct from evidence admission. + assert tree.accept(state).candidate_id == "root" + assert tree.missing_checks(state, "candidate") == ("objective",) + + +def test_old_receipt_after_new_attempt_cannot_override_current_outcome_or_be_reused(): + dependency = tree.Dependency("measurement-source", 0) + state = add(start(dependencies=(dependency,)), "candidate") + state = observe(state, "candidate", "quality") + state = observe(state, "candidate", "objective", objective=9, dependencies=(dependency,)) + old = state.receipts[-1] + state = tree.invalidate(state, dependency.key, expected_revision=state.revision) + new_dependency = state.dependencies[0] + binding = attempt(state, "candidate", "objective", dependencies=(new_dependency,)) + state = tree.register_attempt(state, binding, expected_revision=state.revision) + state = tree.record_receipt(state, old, expected_revision=state.revision) + assert tree.accept(state).candidate_id == "root" + state = tree.record_receipt(state, receipt(binding, Verdict.FAIL), + expected_revision=state.revision) + before = state + state = tree.record_receipt(state, old, expected_revision=state.revision) + assert state is before + assert tree.evidence(state, "candidate")["objective"].verdict == Verdict.FAIL + assert tree.accept(state).candidate_id == "root" + + +def test_generation_invalidation_affects_actual_descendants_but_not_other_branch(): + dependency = tree.Dependency("retrieved-context", 0) + state = start(dependencies=(dependency,)) + state = add(state, "parent", dependencies=(dependency,)) + state = complete(add(state, "child", "parent"), "child", 10) + state = complete(add(state, "other"), "other", 3) + state = tree.invalidate(state, dependency.key, expected_revision=state.revision) + assert not tree.candidate_valid(state, "child") + assert tree.accept(state).candidate_id == "other" + with pytest.raises(ValueError, match="dependencies"): + add(state, "grandchild", "child", dependencies=()) + + +def test_invalid_root_blocks_search_and_acceptance_but_allows_late_archival(): + dependency = tree.Dependency("root-source", 0) + state = add(start(dependencies=(dependency,), root_dependencies=(dependency,)), "candidate") + binding = attempt(state, "candidate", "quality") + state = tree.register_attempt(state, binding, expected_revision=state.revision) + state = tree.invalidate(state, dependency.key, expected_revision=state.revision) + state = tree.record_receipt(state, receipt(binding), expected_revision=state.revision) + with pytest.raises(ValueError, match="root"): + tree.accept(state) + with pytest.raises(ValueError, match="root"): + add(state, "another") + + +def test_inconclusive_is_missing_and_may_be_retried_with_fresh_identity(): + state = add(start(), "candidate") + state = observe(state, "candidate", "quality", Verdict.INCONCLUSIVE) + assert tree.missing_checks(state, "candidate") == ("quality", "objective") + state = complete(state, "candidate", 2) + assert tree.accept(state).candidate_id == "candidate" + assert len([item for item in state.attempts if item.check_id == "quality"]) == 3 + + +@pytest.mark.parametrize("verdict", [Verdict.PASS, Verdict.FAIL]) +def test_valid_outcomes_cannot_be_retried_until_they_pass(verdict): + state = observe(add(start(), "candidate"), "candidate", "quality", verdict) + with pytest.raises(ValueError, match="retried"): + observe(state, "candidate", "quality") + + +def test_unsettled_attempt_blocks_replacement_even_after_invalidation(): + dependency = tree.Dependency("measurement-source", 0) + state = add(start(dependencies=(dependency,)), "candidate") + binding = attempt(state, "candidate", "quality", dependencies=(dependency,)) + state = tree.register_attempt(state, binding, expected_revision=state.revision) + state = tree.invalidate(state, dependency.key, expected_revision=state.revision) + replacement = attempt(state, "candidate", "quality", dependencies=state.dependencies) + with pytest.raises(ValueError, match="Unsettled"): + tree.register_attempt(state, replacement, expected_revision=state.revision) + + +@pytest.mark.parametrize("field", [field.name for field in fields(tree.Context)]) +def test_receipts_from_changed_contract_plan_grader_environment_or_data_are_rejected(field): + state = add(start(), "candidate") + binding = attempt(state, "candidate", "quality") + state = tree.register_attempt(state, binding, expected_revision=state.revision) + wrong = replace(binding, context=replace(binding.context, **{field: "different"})) + with pytest.raises(ValueError, match="registered"): + tree.record_receipt(state, receipt(wrong), expected_revision=state.revision) + + +@pytest.mark.parametrize("field,value", [ + ("search_id", "other-search"), ("candidate_id", "root"), + ("artifact_digest", digest("different")), ("check_id", "objective"), + ("attempt_id", "other-attempt"), ("execution_key", "other-execution"), + ("reservation_id", "other-reservation"), ("lease_id", "other-lease"), +]) +def test_receipt_must_match_the_entire_registered_binding(field, value): + state = add(start(), "candidate") + binding = attempt(state, "candidate", "quality") + state = tree.register_attempt(state, binding, expected_revision=state.revision) + with pytest.raises(ValueError, match="registered"): + tree.record_receipt(state, receipt(replace(binding, **{field: value})), + expected_revision=state.revision) + + +def test_same_bytes_do_not_transfer_evidence_and_conflicting_receipts_are_rejected(): + state = complete(add(start(), "first", artifact_digest=digest("same")), "first", 5) + old = state.receipts[-1] + state = add(state, "second", "first", artifact_digest=digest("same")) + assert tree.evidence(state, "second") == {} + with pytest.raises(ValueError, match="Conflicting"): + tree.record_receipt(state, replace(old, objective=6), expected_revision=state.revision) + with pytest.raises(ValueError, match="reused"): + tree.register_attempt(state, replace(old.attempt, candidate_id="second"), + expected_revision=state.revision) + + +def test_generation_snapshot_candidates_and_history_are_immutable(): + state = start() + with pytest.raises(ValueError, match="snapshot"): + add(state, "candidate", snapshot="updated-mid-search") + advanced = add(state, "candidate") + assert len(state.candidates) == 1 + with pytest.raises(FrozenInstanceError): + advanced.candidates[1].artifact_digest = digest("mutated") + with pytest.raises(ValueError, match="immutable"): + add(advanced, "candidate", artifact_digest=digest("changed")) + with pytest.raises(ValueError, match="Stale"): + tree.add_candidate(advanced, tree.Candidate("other", digest("other"), "root", producer()), + expected_revision=state.revision) + + +@pytest.mark.parametrize("limit", ["max_candidates", "max_depth", "max_attempts"]) +def test_bounded_search_still_returns_the_qualified_root(limit): + config = contract(**{limit: 2 if limit == "max_attempts" else 1}) + state = start(configuration=config) + with pytest.raises(ValueError, match="limit"): + if limit == "max_candidates": + add(state, "first") + elif limit == "max_depth": + add(add(state, "first"), "second", "first") + else: + observe(add(state, "first"), "first", "quality") + assert tree.accept(state).candidate_id == "root" + + +@pytest.mark.parametrize("value", [float("nan"), float("inf"), True, "10"]) +def test_invalid_objective_never_becomes_evidence(value): + state = add(start(), "candidate") + with pytest.raises(ValueError, match="finite"): + receipt(attempt(state, "candidate", "objective"), objective=value) + + +def test_root_bootstrap_requires_complete_scoped_passing_evidence(): + state = start() + for receipts in (state.receipts[:1], (*state.receipts, state.receipts[0]), + (replace(state.receipts[0], verdict=Verdict.FAIL), state.receipts[1])): + with pytest.raises(ValueError): + tree.initialize(state.contract, state.candidates[0], receipts) + with pytest.raises(ValueError, match="objective"): + tree.initialize(state.contract, state.candidates[0], + (state.receipts[0], replace(state.receipts[1], objective=None))) + + +def test_contract_copies_ordered_checks_and_rejects_strings_and_unknown_dependencies(): + checks = ["quality", "objective"] + configuration = contract(checks=checks) + checks.clear() + assert configuration.checks == ("quality", "objective") + with pytest.raises(ValueError, match="ordered"): + contract(checks="quality") + with pytest.raises(ValueError, match="dependencies"): + add(start(), "candidate", dependencies=(tree.Dependency("undeclared", 0),)) diff --git a/tests/test_verification_plans.py b/tests/test_verification_plans.py new file mode 100644 index 000000000..d87028f4d --- /dev/null +++ b/tests/test_verification_plans.py @@ -0,0 +1,151 @@ +from dataclasses import replace +from itertools import combinations + +import pytest + +from inferrsi.contracts.claim import Verdict +from inferrsi.harness import search_tree as tree +from inferrsi.harness.verification_plans import Job, completion_menu +from tests.fixtures.speculative_search import add, attempt, contract, observe, start + + +def menu(state, jobs, **changes): + options = dict(available_budget=100, available_jobs=20, max_targets=3, max_jobs=20) + return completion_menu(state, jobs, **(options | changes)) + + +def fixture(): + state = start() + jobs = [Job("setup", 3)] + for cid in ("a", "b", "c"): + state = add(state, cid) + jobs.extend((Job(f"{cid}-quality", 2, cid, "quality", ("setup",)), + Job(f"{cid}-objective", 4, cid, "objective", (f"{cid}-quality",)))) + return state, tuple(jobs) + + +def test_low_standalone_value_checks_are_preserved_as_completion_bundles(): + state, jobs = fixture() + plans = menu(state, jobs, available_budget=9) + assert len(plans) == 3 + assert {plan.candidate_ids for plan in plans} == {("a",), ("b",), ("c",)} + assert all(len(plan.job_ids) == 3 and plan.cost == 9 for plan in plans) + assert all(plan.job_ids[0] == "setup" for plan in plans) + # No partial check subset can be presented as a completed candidate. + assert menu(state, jobs, available_budget=8) == () + + +def test_cross_branch_completion_counts_shared_preparation_once(): + state, jobs = fixture() + plans = menu(state, jobs, available_budget=15, max_targets=2) + pairs = [plan for plan in plans if len(plan.candidate_ids) == 2] + assert len(pairs) == 3 + assert all(plan.cost == 15 and plan.job_ids.count("setup") == 1 for plan in pairs) + assert all(len(plan.job_ids) == 5 for plan in pairs) + + +def exhaustive_oracle(jobs, budget, job_limit, target_limit): + # Independent oracle enumerates all job subsets, not candidate-completion unions. + result = {} + for size in range(len(jobs) + 1): + for subset in combinations(jobs, size): + ids = {job.job_id for job in subset} + if (len(ids) > job_limit or sum(job.cost for job in subset) > budget + or any(set(job.prerequisites) - ids or set(job.conflicts) & ids + for job in subset)): + continue + targets = tuple(cid for cid in ("a", "b", "c") + if {f"{cid}-quality", f"{cid}-objective"} <= ids) + if not 0 < len(targets) <= target_limit: + continue + cost = sum(job.cost for job in subset) + result[targets] = min(cost, result.get(targets, cost)) + return result + + +@pytest.mark.parametrize("budget", [0, 8, 9, 14, 15, 20, 21]) +@pytest.mark.parametrize("target_limit", [1, 2, 3]) +def test_exact_menu_matches_independent_exhaustive_job_subset_oracle(budget, target_limit): + state, jobs = fixture() + expected = exhaustive_oracle(jobs, budget, 7, target_limit) + actual = {plan.candidate_ids: plan.cost for plan in menu( + state, jobs, available_budget=budget, available_jobs=7, max_targets=target_limit, + )} + assert actual == expected + + +def test_conflicts_job_caps_and_attempt_caps_cannot_be_overridden_by_budget(): + state, jobs = fixture() + jobs = tuple(replace(job, conflicts=("b-objective",)) if job.job_id == "a-objective" + else job for job in jobs) + assert all(not {"a", "b"} <= set(plan.candidate_ids) for plan in menu(state, jobs)) + assert menu(state, jobs, available_jobs=2) == () + limited = replace(state, contract=replace(state.contract, max_attempts=3)) + assert menu(limited, jobs) == () # Root used two; completing another needs two. + with pytest.raises(ValueError, match="enumeration"): + menu(state, jobs, max_jobs=6) + + +def test_partial_evidence_reduces_work_but_failure_is_not_a_completion_target(): + state, jobs = fixture() + state = observe(state, "a", "quality") + state = observe(state, "b", "quality", Verdict.FAIL) + # The adapter rebuilds the menu from remaining work and scopes prerequisite reuse. + jobs = tuple(replace(job, prerequisites=("setup",)) if job.job_id == "a-objective" + else job for job in jobs if job.job_id != "a-quality") + plans = menu(state, jobs, available_budget=7) + assert [(plan.candidate_ids, plan.cost) for plan in plans] == [(("a",), 7)] + + +def test_unknown_cost_and_invalid_dependency_graphs_are_rejected(): + state, jobs = fixture() + for cost in (0, -1, True, None, float("nan")): + with pytest.raises(ValueError, match="cost"): + Job("unknown", cost) + with pytest.raises(ValueError, match="Unknown job relation"): + menu(state, (*jobs, Job("orphan", 1, prerequisites=("absent",)))) + with pytest.raises(ValueError, match="Cyclic"): + menu(state, (*jobs, Job("x", 1, prerequisites=("y",)), + Job("y", 1, prerequisites=("x",)))) + with pytest.raises(ValueError, match="Ambiguous"): + menu(state, (*jobs, Job("duplicate", 1, "a", "quality"))) + with pytest.raises(ValueError, match="ordered"): + Job("x", 1, prerequisites="setup") + + +def test_inflight_work_prevents_replanning_even_if_source_is_invalidated(): + dependency = tree.Dependency("source", 0) + state = add(start(dependencies=(dependency,)), "a") + binding = attempt(state, "a", "quality", dependencies=(dependency,)) + state = tree.register_attempt(state, binding, expected_revision=state.revision) + state = tree.invalidate(state, dependency.key, expected_revision=state.revision) + with pytest.raises(ValueError, match="Settle"): + menu(state, []) + + +def test_inconclusive_outcome_remains_a_completion_job(): + state, jobs = fixture() + state = observe(state, "a", "quality", Verdict.INCONCLUSIVE) + assert ("a",) in {plan.candidate_ids for plan in menu(state, jobs, available_budget=9)} + + +def test_planning_never_changes_evidence_or_spends_the_supplied_budget(): + state, jobs = fixture() + before = state + plans = menu(state, jobs) + assert plans + assert all((plan.search_id, plan.state_revision) == (state.contract.search_id, state.revision) + for plan in plans) + assert state is before + assert tree.accept(state).candidate_id == "root" + assert tree.verified_gain(state) == 0 + with pytest.raises(ValueError, match="known integers"): + menu(state, jobs, available_budget=None) + + +def test_all_jobs_must_name_known_candidates_and_frozen_required_checks(): + state = start(configuration=contract()) + with pytest.raises(ValueError, match="Unknown candidate"): + menu(state, [Job("q", 1, "absent", "quality")]) + with pytest.raises(ValueError, match="Unknown required check"): + menu(state, [Job("q", 1, "root", "unregistered-check")]) From d41f52093886c6767e3e573b107ba9fd179e8ca4 Mon Sep 17 00:00:00 2001 From: amy-why-3459 Date: Wed, 18 Nov 2026 21:12:59 +0800 Subject: [PATCH 02/16] harness: assemble bounded VATS and GVO with durable work control Signed-off-by: amy-why-3459 --- docs/architecture.md | 11 +- docs/operations.md | 3 + docs/speculative-search.md | 356 ++++++---- src/inferrsi/apps/cli/__init__.py | 5 + src/inferrsi/apps/cli/arguments.py | 3 + src/inferrsi/apps/search_benchmarks.py | 159 +++++ src/inferrsi/apps/speculative_search.py | 218 ++++++ src/inferrsi/harness/gvo.py | 213 ++++++ src/inferrsi/harness/local_process.py | 7 +- src/inferrsi/harness/search_adapter.py | 283 ++++++++ src/inferrsi/harness/search_campaign.py | 341 ++++++++++ src/inferrsi/harness/search_continuations.py | 155 +++++ src/inferrsi/harness/search_records.py | 60 ++ src/inferrsi/harness/search_tree.py | 2 + src/inferrsi/harness/trials/external.py | 3 + src/inferrsi/harness/vats.py | 659 +++++++++++++++++++ src/inferrsi/harness/work_control.py | 278 ++++++++ src/inferrsi/model/search_roles.py | 215 ++++++ tests/test_search_benchmarks.py | 115 ++++ tests/test_search_roles.py | 71 ++ tests/test_speculative_search.py | 8 + tests/test_vats_runtime.py | 310 +++++++++ tests/test_work_control.py | 152 +++++ tools/speculative_skill.py | 165 +++++ 24 files changed, 3652 insertions(+), 140 deletions(-) create mode 100644 src/inferrsi/apps/search_benchmarks.py create mode 100644 src/inferrsi/apps/speculative_search.py create mode 100644 src/inferrsi/harness/gvo.py create mode 100644 src/inferrsi/harness/search_adapter.py create mode 100644 src/inferrsi/harness/search_campaign.py create mode 100644 src/inferrsi/harness/search_continuations.py create mode 100644 src/inferrsi/harness/search_records.py create mode 100644 src/inferrsi/harness/vats.py create mode 100644 src/inferrsi/harness/work_control.py create mode 100644 src/inferrsi/model/search_roles.py create mode 100644 tests/test_search_benchmarks.py create mode 100644 tests/test_search_roles.py create mode 100644 tests/test_vats_runtime.py create mode 100644 tests/test_work_control.py create mode 100644 tools/speculative_skill.py diff --git a/docs/architecture.md b/docs/architecture.md index 722d7fa87..d0e5fe69d 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -32,11 +32,12 @@ The optional [service-reuse protocol](service-reuse-campaigns.md) adds project assessment and foreground service ownership within this same controller. Its protocol and result rules live in `harness.project_service`, with process lifetime managed by `local_process`. -The [offline speculative-search primitives](speculative-search.md) add immutable -candidate trees, scoped evidence transitions and bounded completion menus in -Harness. They expose pure control operations for future VATS assembly; the -existing session controller continues to own execution. Production adapter -integration, learned role selection and GVO remain separate delivery work. +The [experimental speculative search](speculative-search.md) composes immutable +candidate trees, bounded completion menus, synchronous VATS and CPU GVO roles. +Its work controller uses shared RunStore transactions and the existing local +process executor. Apps supplies Engine/Skill grading; the support matrix identifies +remaining production accounting and pilot evidence. Serial session commands keep +their existing controller and behavior. The [native host handoff](native-agent-sessions.md) lets the initiating Agent resolve the task request before any campaign exists, use its host's new-session tool and retain the native identity and returned artifacts. The child owns the diff --git a/docs/operations.md b/docs/operations.md index f07bcf930..13fc39135 100644 --- a/docs/operations.md +++ b/docs/operations.md @@ -71,6 +71,9 @@ Effect classes used below: | Replay a cloud-generated parser pair | [Isolated cloud replay](skill-evaluation/skill-eval-cloud-replay.md), `python -m tools.skill_eval_cloud_replay` | Untrusted-artifact execution / report-generating | Manual pinned data ingress; same-host controls before bubblewrap-only replay; no models or credentials | | Validate one harness-improvement step | [Offline improvement](harness-improvement.md), Python `tools.skill_eval_improve.prepare` / `run_offline` | Snapshot-writing / offline experiment-executing | Supplied declarative INV/DOM edits, shared total limits, rerun and one holdout gate; trusted fixed-output clients only; no real-model or Store execution | | Build an offline candidate tree and completion menu | Python `harness.search_tree` / `harness.verification_plans.completion_menu`; [control contract](speculative-search.md) | Read-only / in-memory transitions | Immutable candidates, scoped caller-supplied receipts and bounded exact plans; execution, durable state, accounting, learned roles and final acceptance require separate adapters | +| Run bounded speculative Engine search and role updates | `inferrsi search engine`; [support and configuration](speculative-search.md) | Execution / candidate writes / CPU model updates | Explicit shared RunStore, permissions, checkpoint, budgets, domain and final gate; actual device allocation is caller-owned | +| Inspect/export speculative search | `inferrsi search status` / `search export` | Read / output artifact | Reads the existing store; never restarts search, training or final evaluation | +| Compose Skill speculative search | Python `apps.search_benchmarks.SkillEvaluator` and checkout `tools.speculative_skill.Comparator` | Explicit provider execution through existing comparator | Frozen original baseline, split descriptors, independent grader and bounded provider; see campaign accounting limits in the support matrix | | Plan and report skill PR evaluations | [Skill PR integration](skill-eval-pr.md), `python -m tools.skill_eval_pr` | Read-only planning / authorized experiment-executing / PR-publishing | Trusted mapping, exact operator approval, consumed execution identity and credential-separated publication from retained evidence; no automatic paid trials | | Recover goal progress and the next proposal | [Offline goal checkpoints](goal-checkpoints.md), `python -m tools.goal_checkpoint` | Read-only | Explicit scope/criteria, pinned owner decisions and existing comparison receipts; a disposable view, never execution or acceptance authority | | Summarize campaign gains | `tools/campaign_summary_chart.py`; `report`/`run` figure | Figure-generating | Renders recorded values into one gain figure plus a matching table; validates the specification and computes nothing new | diff --git a/docs/speculative-search.md b/docs/speculative-search.md index 96d6e38a5..d7639bb55 100644 --- a/docs/speculative-search.md +++ b/docs/speculative-search.md @@ -1,140 +1,228 @@ -# Offline candidate trees and verification plans - -This is the first implementation slice of -[the SpecRSI proposal](https://github.com/InferMatrixLab/InferRSI/issues/363). -`harness/search_tree.py` provides immutable candidate and evidence transitions; -`harness/verification_plans.py` constructs bounded completion menus. Both are -pure Python control primitives with no execution, storage or model access. -The existing project and benchmark session commands retain their current behavior. - -The design follows [SpecRSI v2, Section 3](https://arxiv.org/html/2610.16491v2#S3): -editing an incomplete or unsuccessful parent is allowed, while adopting a version -requires complete admissible evidence for that version. Completion bundles keep -complementary checks together. This slice supplies building blocks for the inner -search; a learned Scorer/Policy, automatic Drafter and GVO updates remain future -integration work. - -## State and evidence - -`Contract` freezes the search identity, verification context, required checks, -objective, direction, improver snapshot and candidate/depth/check-attempt caps. -The context identifies the plan, validator, environment, workload, original -baseline and quality reference. Checks may encode separately required repetitions. -The current selector supports a scalar objective plus hard PASS requirements; -the Skill lane's quality-first lexicographic selector needs a separate adapter. - -`initialize` requires one complete, passing receipt per root check. Root receipts -count against the attempt cap. Their acquisition/reuse cost must already be in -the caller's authoritative accounting. A passing objective check carries a finite -value; other checks and rejected/inconclusive outcomes cannot supply a value. - -`add_candidate` records an immutable artifact digest and edit parent, with the -frozen producer snapshot, producer revision, context/history digests and actual -generation dependencies. Callers retain and verify artifacts with the existing -`harness.candidates.retain` / `identity` helpers before admission. This core does -not authenticate digests or decide whether an edit was permitted. Parent quality -failures do not invalidate the edit lineage. Real generation-source dependencies -are inherited and must still be current; a child cannot drop them to escape an -invalidation. Rebase or composition is a new candidate with its own evidence. -This offline core predeclares dependency keys at initialization; introducing new -source authorities dynamically requires later controller integration. - -`register_attempt` binds a caller-owned attempt/execution key, reservation and -lease to one candidate, check and complete verification context. Registering an -attempt grants no execution permission. Unknown work on the same check cannot be -replaced, even after dependency invalidation. Valid outcomes cannot be resampled -until they pass; an INCONCLUSIVE outcome may be retried with a fresh identity -under the caller's frozen retry rules and a new reservation. - -`record_receipt` requires the entire registered binding. Identical delivery is -idempotent and conflicting delivery is rejected. History retains late results; -`evidence` admits only the latest attempt with current dependency epochs and -valid candidate provenance. FAIL is an observed outcome, whereas INCONCLUSIVE -leaves a check missing. Identical candidate bytes do not transfer evidence. - -`accept` selects only fully passing candidates, preferring the root on an -objective tie and otherwise using lexicographic candidate ID order. The result is -a **development selection**. Independent final/holdout acceptance and production -promotion remain caller-owned. `verified_gain` measures this selection relative -to the root. Invalidation recomputes eligibility; invalid root evidence blocks -acceptance and further drafting/check registration, while late archival remains -available. - -## Completion planning - -`completion_menu(state, jobs, available_budget=..., available_jobs=..., -max_targets=..., max_jobs=...)` takes a supplied integer-cost budget view and -returns immutable `Plan` values. The caller has already accounted for planning, -generation, final reserve, in-flight obligations and unknown spend when deriving -that view. Costs must be known positive conservative bounds in the same unit. - -Each `Job` names either a candidate/check pair or a shared preparation step. -Prerequisites form an acyclic graph. The solver takes each candidate's complete -missing-check set, closes its prerequisites, and enumerates unions of at most -`max_targets` candidate seeds. Every feasible singleton completion remains in -the menu. Shared jobs are charged once per plan, conflicts exclude combinations, -and available job slots and remaining check attempts bound each proposal. -`max_jobs` bounds the input graph; enumeration is intended for small offline -controls, not an unbounded production optimizer. No Scorer prediction is used -as evidence or as a substitute for a completion seed. - -Plans bind the search ID and state revision and carry a deterministic topological -job order. The caller retains the full job definitions and costs with its frozen -plan; job IDs alone do not authenticate them. The adapter rebuilds job -relations from current observations at each synchronous decision boundary; -already observed checks are not silently scheduled again. An in-flight attempt, -including an invalidated one, blocks replanning until its execution is settled. -An empty menu is a valid outcome under a tight budget; the root remains available -when its evidence is valid. Feasibility says nothing about whether checks pass. - -## Shared execution and persistence boundary - -These modules introduce no second runner, budget ledger, holdout tracker or -storage backend. A future application adapter must: - -1. Read current authoritative state and the frozen contract, settle the previous - work block, and supply a conservative available-budget view. -2. Generate and retain permitted edits, or select an affordable check plan using - the frozen roles. Account for generation, planning and failed work. -3. Use shared execution controls for atomic claim/reservation, absolute deadline, - resource isolation and actual execution. Bind their identities to the tree. -4. Authenticate and assess receipts, apply a transition against the expected - search revision, and persist it using the authoritative storage CAS/lock. - A pure function's revision check is **not a durable or cross-worker CAS**. - On conflict, reread and recompute; an ambiguous claim never grants a retry. -5. Persist real dependency invalidation before cancellation. Reconcile costs and - retain leases until process cleanup is confirmed. Receipt archival itself - neither charges again nor releases a resource. -6. Freeze the selected version for independent final acceptance, with holdout - access and interruptions tracked separately from development feedback. - -There is no production adapter in this slice. In particular, local immutable -values and revision checks do not establish crash recovery, external exactly-once, -multi-worker resource isolation, budget transactions or holdout non-reuse. - -## Verification and remaining delivery - -Run the CPU controls with: +# Speculative candidate search and group learning + +The experimental implementation of [issue #363](https://github.com/InferMatrixLab/InferRSI/issues/363) +composes bounded VATS, trainable CPU roles, GVO, durable work accounting and +independent final selection. It follows the two loops in +[SpecRSI v2 §3](https://arxiv.org/html/2610.16491v2#S3). It does not reproduce the +paper's language-model training or establish its reported gains. Existing +`campaign` and `benchmarks session` commands keep their serial lifecycle. + +## Supported profile and authority + +| Component | Implementation | Boundary | +| --- | --- | --- | +| Candidate/evidence model | `harness.search_tree`, `search_records` | Immutable artifacts, actual dependency epochs and complete-candidate selection | +| Solver | `harness.verification_plans`, `vats.menu` | Bounded exhaustive completion plans; conservative integer millisecond caps | +| Drafter | `model.search_roles`, `harness.search_adapter` | Trainable softmax over supplied exact text edits; actual retained changes, not free-form LLM generation | +| Policy | `model.search_roles` | Independent eight-feature linear softmax, recorded behavior probabilities, deterministic deployment actions | +| Scorer | `model.search_roles` | Separate linear model; actual suffix MSE and optional paid continuation differences | +| Work/state authority | `harness.work_control` | Transactions in the shared `RunStore` SQLite database | +| Execution | Existing `scenarios.external` and `local_process` | Absolute deadline and process-domain cleanup; no new subprocess runner | +| Engine | `apps.search_benchmarks.EngineEvaluator` | Existing packaged `optimization.py` as one whole check; currently the Qwen denoiser NPU task | +| Skill | `apps.search_benchmarks.SkillEvaluator`, checkout `tools.speculative_skill.Comparator` | Explicit provider configuration, original baseline, independent grader, separate development/holdout descriptors | +| Final | `harness.search_campaign.finalize` | One-time scope claim before access; receipt-only recovery/export; task promotion only | + +This is a trusted local host controller. Resource domains exclude **cooperating +controllers sharing one RunStore**. They are not scheduler/GPU reservations, +filesystem sandboxes, distributed leases, provider cancellation guarantees or +cryptographic attestations. The caller supplies authorized resources and frozen +edit/check permissions. Candidate code cannot enforce its own isolation. + +Execution uses one worker. The offline planner represents shared setup and +conflicts; supplied runtime adapters package setup into each whole check. They +do not execute arbitrary standalone shared-setup jobs. Multi-worker performance +qualification remains outstanding. + +## Candidate and evidence semantics + +`Contract` freezes search ID, context, roles, checks, objective/direction and +candidate/depth/attempt caps. Context includes plan, validator, workload, +environment, original baseline and reference. Objectives must be finite; +overflowing the difference between two finite scores is also rejected. Quality +checks are hard requirements, never compensable by a better score. + +`qualify_root` obtains root evidence through claimed real checks. Unknown root +acquisition can only recover its original receipt. `import_qualified_root` +explicitly re-scopes root receipts under identical artifact/check/context +conditions, retaining physical execution and reservation identities. Root check +attempts count against each search's cap; actual acquisition is charged once to +the campaign, separately from independent search returns. + +In `A → B → C`, B may be unchecked or fail quality while C is edited and checked. +C needs every required receipt for **C's own artifact** to win. Task failure does +not invalidate descendants. Genuine environment/source dependency invalidation +does invalidate dependent evidence. Dependencies are predeclared; dynamic +introduction of new authorities is unsupported. + +`record_receipt` archives registered late results; `evidence` admits only current, +matching receipts. Equal bytes alone do not authorize reuse. Duplicate delivery +is idempotent; conflicts fail. FAIL is an observation; INCONCLUSIVE leaves a +check missing. A valid outcome cannot be repeatedly sampled until it passes. +Permitted retries need new identities and paid reservations. + +`Search.invalidate` persists the epoch change and incumbent recomputation +atomically. An invalid root blocks search; late evidence can still be archived +and charged. `ACCEPT` prefers the root on ties, then candidate ID order. It +selects a development result without changing the stable version. + +## VATS execution + +`Search.start` retains a qualified root and frozen role/settings/adapter hashes. +Each decision claims and meters Solver/Scorer/Policy preparation, constructs +bounded DRAFT/VERIFY/ACCEPT actions and records the behavior law and paid state. +Completion bundles remain in the menu; Scorer predictions never become receipts. +Optional refinement adds unions without removing base completions. An undersized +menu cap fails explicitly. Collection samples Policy; deployment uses argmax. +Drafter edits remain sampled from the supplied permitted vocabulary. + +The chosen work block is persisted before execution. Each edit waits for its +parent artifact, without waiting for parent quality. Each check binds candidate, +contract, attempt, execution, reservation and domain. Settlement commits elapsed +time, cleanup facts and evidence/incumbent changes in one transaction. Recovery +continues the retained block cursor before making another Policy decision. + +ACCEPT and limits return only complete admissible versions. Unstarted work stops +when its conservative cap no longer fits. Unknown launched operations block +continuation until reconciled. Candidate, depth, step, menu and attempt limits +bound the search independently of its time budget. + +The planner closes prerequisites, counts shared costs once and checks conflicts. +`max_targets` limits completion **seeds**, not candidates incidentally completed +by cross-candidate prerequisites. `max_jobs` bounds the whole supplied job graph. +The exact algorithm targets small menus. `draft_ahead=False` and `bundled=False` +are explicit ablation switches; they are not claims of comparative efficiency. + +## GVO and paid continuations + +`collect_group` requires at least two independent complete searches with the same +root, contract, budget, snapshot and adapter. Each owns a tree and seed. The +campaign must be able to provide equal declared budgets before collection. +Leaves from one tree are not independent group members. + +`gvo.examples` computes whole-search returns and leave-one-out advantages. +Natural sampled Drafter/Policy choices supply the clipped PPO-style objective +with KL regularization against a frozen reference. Scorer targets use actual +remaining gains and future costs from each paid decision boundary. Current +preparation is sunk for that suffix but stays in total campaign cost. Missing +labels/cost observations are never zero-filled. Penalties and training settings +are explicit frozen conditions. + +`train_group` checks retained settled records and replayed behavior probabilities, +updates separate actual parameter arrays and commits checkpoint plus training +receipt. `learn` supports Reset/Carry and reuses completed updates on resume; +unknown training cannot be relaunched. An initial checkpoint with explicit +provenance is required. This profile provides neither a pretrained model nor a +supervised warmup dataset. + +`search_continuations.continue_from` copies a retained paid state, charges the +copy, forces one legal first action and continues the frozen policy. The entire +suffix is Scorer-only. `paired_targets` requires the same source state, settings, +snapshot, adapter and continuation seed with different first actions. Both +absolute suffix targets and observed relative differences train the Scorer. +Caller-authored unreceipted pair targets are rejected. + +These are real updates to a constrained CPU operator model. They are not LLM +finetuning, prompt summarization or evidence of pretrained-model improvement. + +## Accounting, recovery and final + +`WorkControl` extends the existing RunStore database with campaign, domain and +holdout tables. It is the authority for this experimental runtime, not a mirror +of serial-session manifests. One transaction reserves work/domains and registers +the pending tree attempt. Controllers using the same resource or holdout must +use the **same shared store**. + +Launch intent is durable before dispatch; an ambiguous crash never authorizes a +second launch. Known elapsed time and cleanup release unused reservation. +Unknown usage/cleanup retains the entire reservation and domain. Over-cap work +is charged and blocks promotion. Absolute deadlines include queueing and cleanup. +In-process bounded planning, editing and training are checked after return; +they are not forcibly preempted like subprocesses. Preflight freezing and initial +setup are outside this campaign meter, a remaining lifecycle-cost limitation. + +Receipts bind candidate/adapter identities and content hashes. They detect +accidental mutation under a trusted host, not malicious-host forgery. Recovery +checks retained candidate bytes and never launches new work. Lost preparation, +training or copy receipts remain unresolved; no guessed-cost retry is available. +External process-liveness reconciliation needs a qualified authority. + +`finalize` consumes a unique scope in the shared store before final data access, +freezes selection and closes search/training. Engine compares again with the +original baseline. For Skill, supply a **holdout-bound adapter** and the declared +holdout scope, never the development adapter. Exact bounded radix utility keeps +quality before token use; reduced tokens cannot offset a quality regression. + +After interruption, final consumes existing receipts only. Even an unstarted +remaining check cannot be newly dispatched by recovery. Existing final results +are immutable; export never reruns them. Only complete accepted final evidence, +known cleanup and valid budget can update `stable`. Role checkpoints are never +automatically promoted to a production/default improver. Such promotion needs +separate independent data and acceptance authority. + +Enforced spend is in milliseconds. Skill reports retain provider token/currency +facts, and the existing comparator enforces its per-comparison limits. A +**campaign-wide provider token/currency reservation bridge is not implemented**. +Elapsed-time caps must not be presented as that guarantee. + +## Entry points + +`inferrsi search engine --help` lists explicit Engine bounds and permissions. +Supply a fresh `--session`, shared `--store`, unique `--campaign`, packaged task, +initial checkpoint, edit vocabulary, authorized resource domain, time/search/ +training bounds, independent group count, rounds, final scope and final threshold. +Task protocol flags follow `--`, for example `--pairs 1 --samples 1 --warmups 0` +when the campaign owner chooses one measurement per arm. No gate is inferred. +A domain name does not allocate a device. + +Checkpoint JSON contains numeric arrays `drafter`, `policy`, `scorer` and nonempty +`origin`. Drafter dimensions match edits; Policy/Scorer each have eight dimensions. +Freeze initial parameters and provenance in the campaign plan. +`--edit TOKEN FILE BEFORE AFTER` permits one exact replacement in a candidate +relative file; candidates cannot change the evaluator or roles. + +Read/export without execution: + +```bash +inferrsi search status --session +inferrsi search export --session --output +``` + +Python composition uses `qualify_root`, `collect_group`, `learn`, `continue_from` +and `finalize`. Recovery requires the original frozen adapters. The CLI has no +blind whole-campaign restart command. Skill's checkout `Comparator` takes the +existing comparator configuration with an explicit bounded provider, original +Git baseline, editable bundle paths and two split descriptors (`path`, `sha256`, +`scope`, `task_count`). It uses the existing private Git-index materializer, +executes the existing comparator under the shared process executor and revalidates +its independent retained report. Construction does not load holdout bytes. +Provider readiness/credentials/authorization remain existing explicit requirements. + +## Validation and issue completion ```bash -python -m pytest tests/test_speculative_search.py tests/test_verification_plans.py +python -m pytest tests/test_speculative_search.py tests/test_verification_plans.py \ + tests/test_work_control.py tests/test_search_roles.py tests/test_vats_runtime.py \ + tests/test_search_benchmarks.py ``` -Tests cover retained file/directory artifacts for synthetic Engine/Skill shapes, -failed-parent repair, incomplete ancestors, failure versus missing evidence, -dependency/result order, identical bytes with separate attempts, stale revision -rejection, immutable role snapshots, deterministic selection, invalid roots and -bounded search. The planner is compared with an independent exhaustive job-subset -oracle across budgets and target limits, including shared preparation and conflicts. +Controls cover exhaustive planning, failed-parent repair, finite-score overflow, +concurrent domain claims, unknown reservations, atomic evidence/accounting, +invalid roots, process-backed CPU searches, numerical actor-gradient checks, +parameter updates, fresh searches, paid continuations, recovery and final export. +The numeric process grader and Skill report fixtures test software behavior; +they are not real task/provider efficacy evidence. -| Issue acceptance area | This slice | Remaining evidence | +| Issue area | Delivered software | Still required for full acceptance | | --- | --- | --- | -| AC-01/02 | Frozen scalar contract and offline candidate tree | Production support matrix and role/execution adapters | -| AC-03/05 | Scoped evidence, epoch invalidation, development selection and revision guards | Authoritative durable state integration and restart/race tests | -| AC-04 | Bounded exact completion menu and exhaustive controls | Scorer refinement, Policy selection and scalable solver integration | -| AC-06 | Frozen snapshot identity only | GVO data collection, objectives, parameter updates and new searches | -| AC-07/08 | References to caller-owned execution identities | Actual budget/resource transactions, cancellation and independent final/holdout | -| AC-09/10 | Synthetic control fixtures | Both real adapters, model generation, training, matched experiments and lifecycle costs | - -These tests do not establish model quality, engine speedup or SpecRSI efficiency. -The parent issue remains open for its full dual-loop delivery. +| AC-01/02 | Support/permission boundaries and immutable trees | Production role/environment qualification | +| AC-03/05 | Durable transitions, late receipts, invalidation, bounded synchronous VATS | Production adapter fault/race evidence | +| AC-04 | Exact planner, independent oracle and Scorer refinement | Scaling and real complementary-check evidence | +| AC-06 | Independent groups, actor/value updates, paid continuations, checkpoints, Reset/Carry | Actual task training results and role qualification | +| AC-07 | Shared local claims, elapsed-time caps and cleanup-aware recovery | Host callback preemption, provider/device total accounting and production resource integration | +| AC-08 | One-time final scope and task promotion gate | Real independent final evidence and separate default-improver qualification | +| AC-09 | Packaged Engine evaluator and explicit Skill comparator bridge | Both complete real VATS/GVO/final pilots | +| AC-10 | Retained trajectories/costs and ablation switches | Serial/VATS, bundle, learning, Reset/Carry and worker experiments with full lifecycle costs | + +The parent issue remains open. Software tests, CPU mathematical checks or an +available adapter do not establish full acceptance, NPU speedup, provider quality, +search efficiency or universal SpecRSI gains. diff --git a/src/inferrsi/apps/cli/__init__.py b/src/inferrsi/apps/cli/__init__.py index 839454cce..d34797adc 100644 --- a/src/inferrsi/apps/cli/__init__.py +++ b/src/inferrsi/apps/cli/__init__.py @@ -499,6 +499,11 @@ def main(argv: list[str] | None = None) -> int: result = getattr(agent_sessions, args.agent_action)(args.job) _print(result) return 0 + if args.command == "search": + from ..speculative_search import command as search_command + + _print(search_command(args)) + return 0 if args.command == "benchmarks": if args.benchmark_action == "compare": from ..workflows.benchmark_compare import compare_command diff --git a/src/inferrsi/apps/cli/arguments.py b/src/inferrsi/apps/cli/arguments.py index d532b0ca5..3bdeb6aed 100644 --- a/src/inferrsi/apps/cli/arguments.py +++ b/src/inferrsi/apps/cli/arguments.py @@ -243,6 +243,9 @@ def parser(*, parser_class=argparse.ArgumentParser) -> argparse.ArgumentParser: from .evaluate import add_parser as add_evaluate_parser add_evaluate_parser(commands) + from ..speculative_search import add_arguments as search_arguments + + search_arguments(commands) mindie = commands.add_parser("mindie", help="Use project-approved, local MindIE knowledge") mindie_actions = mindie.add_subparsers(dest="mindie_action", required=True) enable = mindie_actions.add_parser("enable", help="Approve a MindIE domain for this checkout") diff --git a/src/inferrsi/apps/search_benchmarks.py b/src/inferrsi/apps/search_benchmarks.py new file mode 100644 index 000000000..eb7a11720 --- /dev/null +++ b/src/inferrsi/apps/search_benchmarks.py @@ -0,0 +1,159 @@ +"""Benchmark-owned evaluators composed with Harness speculative search. + +The engine protocol remains one indivisible check; no CPU or partial-case run +can impersonate its NPU performance qualification. Skill grading stays in the +existing independent comparator and quality-first rules. +""" + +import math +import sys +import time +from pathlib import Path + +from inferrsi.apps.improvement import assessment +from inferrsi.benchmark.improvement import SelectionPolicy, holdout_accepts +from inferrsi.benchmark.registry import task_resources +from inferrsi.harness import candidates, task_protocol +from inferrsi.harness.artifact_primitives import digest, sha256 +from inferrsi.harness.atomic_json import atomic_write_json +from inferrsi.harness.optimization_session import runtime_environment +from inferrsi.harness.scenarios.external import execute_trial + + +class EngineEvaluator: + def __init__(self, package, frozen, *, arguments, python=sys.executable, prefix=()): + self.frozen = Path(frozen).resolve() + self.plan = task_protocol.prepare( + Path(str(task_resources(package))), self.frozen, arguments + ) + self.python, self.prefix = python, tuple(prefix) + self.identity = digest( + { + "lane": "engine", + "task": package, + "plan": self.plan, + "python": python, + "prefix": self.prefix, + } + ) + + def evaluate(self, candidate, check, output, deadline): + if check != "evaluation": + raise ValueError("Packaged engine protocol requires one full evaluation job") + actual = { + p.relative_to(self.frozen).as_posix(): sha256(p) + for p in self.frozen.rglob("*") + if p.is_file() + } + if actual != self.plan["frozen_files"]: + raise ValueError("Frozen benchmark resources changed") + output.mkdir(parents=True) + candidates.retain(candidate / "candidate.py", output / "candidate.py") + protocol = task_protocol.protocol(self.frozen) + args = protocol.arguments( + output / "candidate.py", output / "measurement", self.plan["evaluation_settings"] + ) + from inferrsi.harness import optimization_session + + command = ( + *self.prefix, + self.python, + str(Path(optimization_session.__file__).resolve()), + str(self.frozen / "benchmark.py"), + *args, + ) + environment = runtime_environment() + environment["PYTHONDONTWRITEBYTECODE"] = "1" + execution = execute_trial( + command, + output, + output / "execution", + time.monotonic() + max(0, deadline - time.time()), + None, + environment=environment, + include_cleanup=True, + ) + assessed = protocol.assess(output, self.plan["evaluation_settings"], execution) + if execution["cleanup_verified"] is not True: + assessed = {"verdict": "INCONCLUSIVE", "score": None} + atomic_write_json(output / "assessment.json", assessed) + return { + "verdict": assessed["verdict"], + "objective": assessed.get("score"), + "cleanup_verified": execution["cleanup_verified"], + "assessment_sha256": sha256(output / "assessment.json"), + "execution_sha256": sha256(output / "execution/execution.json"), + } + + +class SkillEvaluator: + """Use an explicitly bound independent comparator, with an exact lexicographic utility. + + The comparator's run method returns a verified report and cleanup facts; it + must run the existing Skill comparison for the supplied candidate and split. + No generic model client or fixed-output fixture is silently selected here. + """ + + def __init__(self, comparator, *, split, policy, token_cap, task_count): + if not isinstance(policy, SelectionPolicy) or split not in ("development", "holdout"): + raise ValueError("A frozen Skill selection policy and explicit split are required") + if ( + type(token_cap) is not int + or token_cap < 1 + or type(task_count) is not int + or task_count < 1 + or task_count * (token_cap + 1) > 2**53 - 1 + ): + raise ValueError("Skill utility must be exactly representable within declared bounds") + self.comparator, self.split, self.policy = comparator, split, policy + self.holdout_scope = ( + getattr(comparator, "holdout_scope", None) if split == "holdout" else None + ) + self.token_cap, self.task_count = token_cap, task_count + self.identity = digest( + { + "lane": "skill", + "comparator": comparator.identity, + "split": split, + "min_success_rate": policy.min_success_rate, + "max_regressions": policy.max_regressions, + "token_cap": token_cap, + "task_count": task_count, + "utility": "successes*(token_cap+1)-tokens", + } + ) + + def evaluate(self, candidate, check, output, deadline): + if check != "evaluation": + raise ValueError("Skill comparison is an indivisible evaluation job") + report, cleanup = self.comparator.run(candidate, output, deadline, split=self.split) + score = assessment(report, self.policy) + tokens = score["usage"]["candidate"]["total_tokens"] + if ( + cleanup is not True + or not score["complete"] + or score["reason"] == "unknown_usage" + or tokens is None + or type(tokens) is not int + or not 0 <= tokens <= self.token_cap + or score["trials"] != self.task_count + ): + verdict, objective = "INCONCLUSIVE", None + elif score["reason"] not in ("eligible", "no_improvement"): + verdict, objective = "FAIL", None + else: + # One extra success dominates the entire possible token range. + # This exact radix preserves the existing quality-then-token order. + verdict = "PASS" + objective = score["successes"] * (self.token_cap + 1) - tokens + if not math.isfinite(objective): + raise ValueError("Invalid Skill utility") + if self.split == "holdout" and not holdout_accepts(score): + verdict, objective = "FAIL", None + return { + "verdict": verdict, + "objective": objective, + "cleanup_verified": cleanup, + "report_digest": digest(report), + "assessment": score, + } diff --git a/src/inferrsi/apps/speculative_search.py b/src/inferrsi/apps/speculative_search.py new file mode 100644 index 000000000..318939f00 --- /dev/null +++ b/src/inferrsi/apps/speculative_search.py @@ -0,0 +1,218 @@ +"""CLI composition for the supported packaged Engine VATS/GVO profile.""" + +import json +import sys +import time +from dataclasses import asdict, replace +from pathlib import Path + +from inferrsi.harness import candidates, search_tree +from inferrsi.harness.artifact_primitives import digest +from inferrsi.harness.atomic_json import atomic_write_json +from inferrsi.harness.run_store import RunStore, RunStoreReader +from inferrsi.harness.search_adapter import ArtifactAdapter, Edit +from inferrsi.harness.search_campaign import collect_group, finalize, learn, qualify_root +from inferrsi.harness.search_records import decode +from inferrsi.harness.vats import Settings +from inferrsi.harness.work_control import WorkControl +from inferrsi.model.search_roles import Snapshot + +from .search_benchmarks import EngineEvaluator + + +def add_arguments(commands): + search = commands.add_parser("search", help="Bounded SpecRSI search and role training") + actions = search.add_subparsers(dest="search_action", required=True) + run = actions.add_parser("engine", help="Run a supplied packaged Engine protocol") + run.add_argument("--session", type=Path, required=True) + run.add_argument("--store", type=Path, required=True, help="Shared resource/holdout RunStore") + run.add_argument("--campaign", required=True, help="Unique campaign ID in the shared store") + run.add_argument("--task", required=True) + run.add_argument( + "--edit", + nargs=4, + action="append", + required=True, + metavar=("TOKEN", "FILE", "BEFORE", "AFTER"), + ) + run.add_argument( + "--checkpoint", + type=Path, + required=True, + help="Initial trainable finite-edit role snapshot with provenance", + ) + run.add_argument("--domain", required=True, help="Caller-owned exclusive measurement domain") + run.add_argument("--max-seconds", type=int, required=True) + run.add_argument("--evaluation-seconds", type=int, required=True) + run.add_argument("--final-reserve-seconds", type=int, required=True) + run.add_argument("--search-seconds", type=int, required=True) + run.add_argument("--preparation-seconds", type=int, required=True) + run.add_argument("--draft-seconds", type=int, required=True) + run.add_argument("--training-seconds", type=int, required=True) + run.add_argument("--candidates", type=int, required=True) + run.add_argument("--depth", type=int, required=True) + run.add_argument("--attempts", type=int, required=True) + run.add_argument("--steps", type=int, required=True) + run.add_argument("--plans", type=int, required=True) + run.add_argument("--targets", type=int, required=True) + run.add_argument("--groups", type=int, required=True, help="Independent searches per round") + run.add_argument("--rounds", type=int, required=True) + run.add_argument("--learning-rate", type=float, required=True) + run.add_argument("--seed", type=int, default=0) + run.add_argument("--mode", choices=("reset", "carry"), default="reset") + run.add_argument("--final-scope", required=True) + run.add_argument("--minimum-final-score", type=float, required=True) + run.add_argument("--python", default=sys.executable) + run.add_argument("arguments", nargs="*", help="Task protocol arguments after --") + for action in ("status", "export"): + read = actions.add_parser(action) + read.add_argument("--session", type=Path, required=True) + if action == "export": + read.add_argument("--output", type=Path, required=True) + + +def command(args): + root = args.session.resolve() + if args.search_action != "engine": + location = json.loads((root / "control.json").read_text(encoding="utf-8")) + control = WorkControl(RunStoreReader(location["store"], "speculative")) + result = control.read(location["campaign"]) + if args.search_action == "export": + atomic_write_json(args.output, result) + return result + if root.exists(): + raise ValueError("Use a new session directory; status/export never restart work") + if args.groups < 2 or args.rounds < 1: + raise ValueError("GVO needs at least two independent searches and one training round") + if args.final_reserve_seconds < args.evaluation_seconds: + raise ValueError("Final reserve must cover the full evaluation cap") + required = ( + args.evaluation_seconds + + args.final_reserve_seconds + + (args.groups * args.rounds + 2) * args.search_seconds + + args.rounds * args.training_seconds + ) + if args.max_seconds < required: + raise ValueError( + "Campaign cap must fund qualification, all search groups, training and final" + ) + snapshot = Snapshot(**json.loads(args.checkpoint.read_text(encoding="utf-8"))) + if ( + len(snapshot.drafter) != len(args.edit) + or len(snapshot.policy) != 8 + or len(snapshot.scorer) != 8 + ): + raise ValueError( + "Checkpoint needs one Drafter weight per edit and eight Policy/Scorer weights" + ) + settings = Settings( + args.steps, + 1, + args.depth, + args.targets, + args.candidates, + args.plans, + args.preparation_seconds * 1000, + args.draft_seconds * 1000, + args.search_seconds * 1000, + ) + root.mkdir(parents=True) + evaluator = EngineEvaluator( + args.task, root / "frozen", arguments=args.arguments, python=args.python + ) + adapter = ArtifactAdapter( + root / "artifacts", + (Edit(*raw) for raw in args.edit), + {"evaluation": args.evaluation_seconds * 1000}, + evaluator, + domain=args.domain, + ) + source = root / "initial" + source.mkdir() + candidates.retain(root / "frozen/baseline.py", source / "candidate.py") + node = adapter.import_root(source, snapshot=digest(snapshot.document()), context={}) + control = WorkControl(RunStore(args.store.resolve(), "speculative")) + campaign = args.campaign + atomic_write_json( + root / "control.json", {"store": str(args.store.resolve()), "campaign": campaign} + ) + specification = { + "adapter": adapter.identity, + "settings": asdict(settings), + "groups": args.groups, + "rounds": args.rounds, + "mode": args.mode, + "learning_rate": args.learning_rate, + "minimum_final_score": args.minimum_final_score, + "initial_snapshot": snapshot.document(), + } + contract = search_tree.Contract( + "qualification", + search_tree.Context( + digest(specification), + digest(specification), + evaluator.identity, + args.domain, + args.task, + node.artifact_digest, + evaluator.identity, + ), + digest(snapshot.document()), + ("evaluation",), + "evaluation", + "maximize", + args.candidates, + args.depth, + args.attempts, + ) + control.create( + campaign, + specification, + {"searches": {}, "checkpoint": snapshot.document(), "stable": asdict(node)}, + cap_ms=args.max_seconds * 1000, + final_reserve_ms=args.final_reserve_seconds * 1000, + deadline=time.time() + args.max_seconds, + ) + initial = qualify_root(control, campaign, adapter, node, contract) + trained = learn( + control, + campaign, + adapter, + initial, + snapshot, + settings, + rounds=args.rounds, + group_size=args.groups, + training_cap_ms=args.training_seconds * 1000, + learning_rate=args.learning_rate, + seed=args.seed, + mode=args.mode, + ) + # A fresh post-update search demonstrates the new frozen checkpoint. Selection + # is development-only, and never uses final results to choose another version. + records = collect_group( + control, + campaign, + adapter, + initial, + trained, + settings, + group_id="deployment", + size=2, + seed=args.seed, + collect=False, + ) + states = [decode(record["tree"]) for record in records] + selected_state = max(states, key=search_tree.verified_gain) + selected = replace(search_tree.accept(selected_state), parent_id=None) + finalize( + control, + campaign, + adapter, + selected, + holdout_scope=args.final_scope, + accept_result=lambda values: all( + v["objective"] >= args.minimum_final_score for v in values + ), + ) + return control.read(campaign) diff --git a/src/inferrsi/harness/gvo.py b/src/inferrsi/harness/gvo.py new file mode 100644 index 000000000..10e950960 --- /dev/null +++ b/src/inferrsi/harness/gvo.py @@ -0,0 +1,213 @@ +"""GVO data separation and metered group updates of frozen search roles.""" + +import math +import time +import uuid +from dataclasses import replace + +from inferrsi.model.search_roles import Choice, finite, update + +from .artifact_primitives import digest +from .search_records import decode + + +def choice_from_document(raw): + return Choice( + **( + raw + | { + "options": tuple(raw["options"]), + "features": tuple(tuple(row) for row in raw["features"]), + "probabilities": tuple(raw["probabilities"]), + } + ) + ) + + +def examples(records, *, expense_weight=0.0, elapsed_weight=0.0): + records = tuple(records) + if len(records) < 2: + raise ValueError("GVO requires at least two independent complete searches") + if finite(expense_weight) < 0 or finite(elapsed_weight) < 0: + raise ValueError("Return penalties cannot be negative") + identities, roots, contracts, snapshots, settings, adapters = ( + [], + set(), + set(), + set(), + set(), + set(), + ) + returns = [] + for record in records: + if record["status"] != "complete" or record["pending"] is not None: + raise ValueError("Incomplete searches cannot supply GVO returns") + state = decode(record["tree"]) + identities.append(state.contract.search_id) + roots.add( + next(n.artifact_digest for n in state.candidates if n.candidate_id == state.root_id) + ) + frozen = record["tree"]["contract"].copy() + frozen.pop("search_id") + contracts.add(digest(frozen)) + snapshots.add(record["snapshot"]) + settings.add(record["settings"]) + adapters.add(record["adapter"]) + returns.append( + finite( + record["gain"] + - record["initial_gain"] + - expense_weight * finite(record["spent_ms"]) / 1000 + - elapsed_weight * finite(record["elapsed_ms"]) / 1000 + ) + ) + if len(set(identities)) != len(identities) or any( + len(values) != 1 for values in (roots, contracts, snapshots, settings, adapters) + ): + raise ValueError("Search group must have distinct IDs and identical starting conditions") + if any(record["forced"] for record in records): + raise ValueError("Forced continuations are Scorer-only, never actor group members") + advantages = [ + finite(value - math.fsum(v for j, v in enumerate(returns) if j != i) / (len(returns) - 1)) + for i, value in enumerate(returns) + ] + actors, values = [], [] + for record, advantage in zip(records, advantages): + values.extend( + suffix_examples(record, expense_weight=expense_weight, elapsed_weight=elapsed_weight) + ) + if not record["collect"]: + raise ValueError("Deployment searches cannot supply on-policy actor data") + for step in record["trace"]: + if step["forced"]: + raise ValueError("Forced action leaked into a natural trajectory") + for raw in (step["choice"], *step["draft_choices"]): + choice = choice_from_document(raw) + if choice.sampled: + actors.append((choice, advantage)) + return actors, values, {"search_ids": identities, "returns": returns, "advantages": advantages} + + +def suffix_examples(record, *, expense_weight, elapsed_weight): + if record["status"] != "complete" or record["pending"] is not None: + raise ValueError("Suffix target requires a completed and settled search") + end = record["started_at"] + record["elapsed_ms"] / 1000 + result = [] + for step in record["trace"]: + future_cost = record["spent_ms"] - step["spent_before"] + if future_cost < 0 or end < step["at"]: + raise ValueError("Suffix accounting precedes its paid decision state") + target = finite( + record["gain"] + - step["gain_before"] + - expense_weight * future_cost / 1000 + - elapsed_weight * (end - step["at"]) + ) + result.append((tuple(step["features"]), target)) + return result + + +def train_group( + control, + campaign, + records, + snapshot, + reference, + *, + cap_ms, + learning_rate, + expense_weight=0.0, + elapsed_weight=0.0, + scorer_continuations=(), + scorer_pairs=(), + pair_examples=(), + **training, +): + """One actual parameter update, durably checkpointed inside the campaign budget.""" + records = tuple(records) + if any(record["snapshot"] != digest(snapshot.document()) for record in records): + raise ValueError("Training checkpoint differs from collection checkpoint") + doc = control.read(campaign) + if doc["final_claim"] is not None: + raise ValueError("Training is closed before final evaluation") + if digest(doc["state"]["checkpoint"]) != digest(snapshot.document()): + raise ValueError("Training checkpoint is no longer current") + if any( + op["category"] == "training" and op["status"] != "settled" + for op in doc["operations"].values() + ): + raise ValueError("Unresolved training execution cannot be relaunched") + for record in (*records, *scorer_continuations, *(r for pair in scorer_pairs for r in pair)): + search_id = decode(record["tree"]).contract.search_id + retained = doc["state"]["searches"].get(search_id) + if retained is None or digest(retained) != digest(record): + raise ValueError("Training requires the retained, settled search record") + if pair_examples: + raise ValueError("Unreceipted pair targets are unsupported; use paid suffix records") + from .search_continuations import paired_targets + + pair_examples = tuple( + paired_targets(pair, expense_weight=expense_weight, elapsed_weight=elapsed_weight) + for pair in scorer_pairs + ) + operation = "gvo-" + uuid.uuid4().hex + control.claim( + campaign, + doc["revision"], + operation, + category="training", + cap_ms=cap_ms, + binding={ + "snapshot": digest(snapshot.document()), + "reference": digest(reference.document()), + "training": { + "learning_rate": learning_rate, + "expense_weight": expense_weight, + "elapsed_weight": elapsed_weight, + **training, + }, + "searches": [decode(r["tree"]).contract.search_id for r in records], + }, + ) + claim = control.launch(campaign, operation) + started = time.monotonic() + actors, values, summary = examples( + records, expense_weight=expense_weight, elapsed_weight=elapsed_weight + ) + for record in scorer_continuations: + if not record["forced"]: + raise ValueError("Additional Scorer trajectories must be explicitly marked forced") + values.extend( + suffix_examples(record, expense_weight=expense_weight, elapsed_weight=elapsed_weight) + ) + trained = update( + snapshot, + reference, + actors, + values, + pair_examples, + learning_rate=learning_rate, + groups=len(records), + **training, + ) + trained = replace(trained, origin=digest({"parent": snapshot.document(), "group": summary})) + elapsed = max(1, math.ceil((time.monotonic() - started) * 1000)) + + def checkpoint(state): + state["checkpoint"] = trained.document() + state.setdefault("training", []).append( + {**summary, "operation": operation, "checkpoint": digest(trained.document())} + ) + return state + + control.settle( + campaign, + operation, + {**summary, "checkpoint": trained.document()}, + actual_ms=elapsed, + cleanup_verified=True, + transition=checkpoint, + ) + if elapsed > cap_ms or time.time() >= claim["deadline"]: + raise ValueError("Training exceeded its frozen budget") + return trained diff --git a/src/inferrsi/harness/local_process.py b/src/inferrsi/harness/local_process.py index 7d98a6210..a92cfb5b8 100644 --- a/src/inferrsi/harness/local_process.py +++ b/src/inferrsi/harness/local_process.py @@ -379,7 +379,7 @@ def _supervise( def execute_command(command: tuple[str, ...], cwd: Path, trial_dir: Path, deadline: float, *, - environment: dict | None = None) -> dict: + environment: dict | None = None, include_cleanup: bool = False) -> dict: """Legacy trial record: execute argv under a whole-run deadline in trial_dir.""" trial_dir.mkdir(parents=True) result_path = trial_dir / "result.json" @@ -408,4 +408,9 @@ def execute_command(command: tuple[str, ...], cwd: Path, trial_dir: Path, deadli if run["error"] is not None: record["error"] = run["error"] record["elapsed_seconds"] = run["elapsed_seconds"] + if include_cleanup: + record.update(cleanup_verified=(run["domain_empty"] if run["pid"] is not None else True), + termination_requested=run["termination_requested"], + process_exited=run["returncode"] is not None, + process_started_at=run["started_at"], process_ended_at=run["ended_at"]) return record diff --git a/src/inferrsi/harness/search_adapter.py b/src/inferrsi/harness/search_adapter.py new file mode 100644 index 000000000..4896f8a07 --- /dev/null +++ b/src/inferrsi/harness/search_adapter.py @@ -0,0 +1,283 @@ +"""Artifact-backed finite-edit generation and authenticated command evaluations. + +Commands and graders are trusted host inputs frozen before search. Candidates +must run in a sandbox supplied by the application when their code is untrusted; +the existing local process domain supplies cleanup, not filesystem isolation. +""" + +import json +import math +import time +from dataclasses import asdict, dataclass +from pathlib import Path + +from inferrsi.contracts.claim import Verdict +from inferrsi.model.search_roles import choose + +from . import candidates +from . import search_tree as tree +from .artifact_primitives import digest, sha256 +from .atomic_json import atomic_write_json +from .optimization_session import runtime_environment +from .scenarios.external import execute_trial +from .verification_plans import Job + + +@dataclass(frozen=True) +class Edit: + token: str + path: str + before: str + after: str + + def __post_init__(self): + path = Path(self.path) + if ( + not self.token + or not self.before + or self.before == self.after + or path.is_absolute() + or ".." in path.parts + or not path.parts + ): + raise ValueError("Edit must name one permitted relative file and a nonempty change") + + +class ArtifactAdapter: + def __init__(self, root, edits, checks, evaluator, *, domain): + self.root = Path(root).resolve() + self.root.mkdir(parents=True, exist_ok=True) + self.edits, self.checks = tuple(edits), dict(checks) + if ( + not self.edits + or len({e.token for e in self.edits}) != len(self.edits) + or not self.checks + or any(type(v) is not int or v <= 0 for v in self.checks.values()) + or not domain + ): + raise ValueError("Frozen edit vocabulary, check caps and domain are required") + self.evaluator, self.domain = evaluator, domain + self.identity = digest( + { + "edits": [asdict(e) for e in self.edits], + "checks": self.checks, + "evaluator": evaluator.identity, + "domain": domain, + "generation": "finite-edit-softmax-v1", + } + ) + + def path(self, node): + path = self.root / "candidates" / node.candidate_id + if path.parent != self.root / "candidates" or path.is_symlink(): + raise ValueError("Candidate ID must name one retained snapshot") + if candidates.identity(path) != node.artifact_digest: + raise ValueError("Retained candidate artifact changed") + return path + + def import_root(self, source, *, snapshot, context): + destination = self.root / "candidates" / "root" + destination.parent.mkdir(exist_ok=True) + fingerprint = candidates.retain(Path(source), destination) + return tree.Candidate( + "root", + fingerprint, + None, + tree.Producer(snapshot, self.identity, digest(context), digest([])), + ) + + def jobs(self, state): + return tuple( + Job(f"{node.candidate_id}:{check}", self.checks[check], node.candidate_id, check) + for node in state.candidates + if tree.candidate_valid(state, node.candidate_id) + and not any( + r.verdict == Verdict.FAIL for r in tree.evidence(state, node.candidate_id).values() + ) + for check in tree.missing_checks(state, node.candidate_id) + ) + + def domains(self, kind, node, check): + return (self.domain,) if kind == "verify" else () + + def _save(self, operation, binding, result): + if Path(operation).name != operation or operation in (".", ".."): + raise ValueError("Operation must name one retained execution") + binding = binding | {"adapter": self.identity} + path = self.root / "receipts" / (operation + ".json") + path.parent.mkdir(exist_ok=True) + atomic_write_json( + path, + { + "binding": binding, + "result": result, + "digest": digest({"binding": binding, "result": result}), + }, + ) + return result + + def draft(self, parent, state, snapshot, rng, operation, deadline): + started = time.monotonic() + source = self.path(parent) + files = candidates.members(source) + legal = [ + (index, edit) + for index, edit in enumerate(self.edits) + if edit.path in files + and files[edit.path].read_text(encoding="utf-8").count(edit.before) == 1 + ] + choice, node = None, None + if legal and time.time() < deadline: + vectors = [ + tuple(float(i == index) for i in range(len(self.edits))) for index, _ in legal + ] + choice = choose( + snapshot, + "drafter", + [edit.token for _, edit in legal], + vectors, + rng=rng, + context=digest(asdict(parent)), + collect=True, + ) + edit = legal[choice.selected][1] + destination = self.root / "candidates" / operation + candidates.retain(source, destination) + path = destination / edit.path + text = path.read_text(encoding="utf-8") + path.write_text(text.replace(edit.before, edit.after, 1), encoding="utf-8") + node = tree.Candidate( + operation, + candidates.identity(destination), + parent.candidate_id, + tree.Producer( + digest(snapshot.document()), + self.identity, + digest(asdict(parent)), + digest([asdict(r) for r in state.receipts]), + ), + parent.dependencies, + ) + result = { + "candidate": asdict(node) if node else None, + "choice": asdict(choice) if choice else None, + "cleanup_verified": True, + "elapsed_ms": max(1, math.ceil((time.monotonic() - started) * 1000)), + } + if time.time() >= deadline: + result["candidate"] = None + result["error"] = "draft_deadline" + return self._save(operation, {"candidate": asdict(parent), "kind": "draft"}, result) + + def verify(self, node, check, operation, deadline): + started = time.monotonic() + source = self.path(node) + output = self.root / "executions" / operation + result = self.evaluator.evaluate(source, check, output, deadline) + if candidates.identity(source) != node.artifact_digest: + raise ValueError("Evaluation changed the retained candidate") + verdict = Verdict(result["verdict"]) + objective = result.get("objective") + if objective is not None and ( + type(objective) not in (int, float) or not math.isfinite(objective) + ): + raise ValueError("Nonfinite benchmark score") + result = { + **result, + "verdict": verdict.value, + "elapsed_ms": max(1, math.ceil((time.monotonic() - started) * 1000)), + "evidence_digest": digest(result), + "objective": objective, + } + return self._save( + operation, {"candidate": asdict(node), "check": check, "kind": "verify"}, result + ) + + def recover(self, operation, pending): + if Path(operation).name != operation or operation in (".", ".."): + raise ValueError("Operation must name one retained execution") + path = self.root / "receipts" / (operation + ".json") + if not path.is_file(): + return None + receipt = json.loads(path.read_text(encoding="utf-8")) + if ( + digest({"binding": receipt["binding"], "result": receipt["result"]}) + != receipt["digest"] + ): + raise ValueError("Recovery receipt changed") + expected = { + "candidate": pending["binding"]["candidate"], + "kind": pending["kind"], + "adapter": self.identity, + } + if pending["kind"] == "verify": + expected["check"] = pending["binding"]["check"] + if digest(expected) != digest(receipt["binding"]): + raise ValueError("Recovery receipt belongs to a different operation") + for raw in (expected["candidate"], receipt["result"].get("candidate")): + if raw is not None: + self.path( + tree.Candidate( + **( + raw + | { + "producer": tree.Producer(**raw["producer"]), + "dependencies": tuple( + tree.Dependency(**d) for d in raw["dependencies"] + ), + } + ) + ) + ) + return receipt["result"] + + +class CommandEvaluator: + """Frozen independent evaluator command with explicit JSON result protocol. + + The grader writes {verdict, objective?} to {result}; the shared executor + independently supplies process/cleanup facts. Useful for project protocols + and CPU integration controls, not an implicit production qualification. + """ + + def __init__(self, commands, resources): + self.commands = {key: tuple(value) for key, value in commands.items()} + self.resources = {Path(path).resolve(): sha256(Path(path)) for path in resources} + self.identity = digest( + {"commands": self.commands, "resources": {str(k): v for k, v in self.resources.items()}} + ) + + def evaluate(self, candidate, check, output, deadline): + if any(sha256(path) != expected for path, expected in self.resources.items()): + raise ValueError("Frozen evaluator resources changed") + command = tuple(arg.replace("{candidate}", str(candidate)) for arg in self.commands[check]) + output.parent.mkdir(parents=True, exist_ok=True) + record = execute_trial( + command, + candidate, + output, + time.monotonic() + max(0, deadline - time.time()), + None, + environment=runtime_environment(), + include_cleanup=True, + ) + result = { + "verdict": "INCONCLUSIVE", + "objective": None, + "cleanup_verified": record["cleanup_verified"], + "execution_sha256": sha256(output / "execution.json"), + } + if ( + record["status"] == "completed" + and record["returncode"] == 0 + and record["cleanup_verified"] is True + ): + document = json.loads(Path(record["result_path"]).read_text(encoding="utf-8")) + result.update( + verdict=Verdict(document["verdict"]).value, + objective=document.get("objective"), + result_sha256=sha256(Path(record["result_path"])), + ) + if any(sha256(path) != expected for path, expected in self.resources.items()): + raise ValueError("Evaluator resources changed during execution") + return result diff --git a/src/inferrsi/harness/search_campaign.py b/src/inferrsi/harness/search_campaign.py new file mode 100644 index 000000000..fea8bce99 --- /dev/null +++ b/src/inferrsi/harness/search_campaign.py @@ -0,0 +1,341 @@ +"""Campaign assembly: real root acquisition, independent searches, GVO and final gating.""" + +import time +from dataclasses import asdict, replace + +from inferrsi.contracts.claim import Verdict +from inferrsi.model.search_roles import Snapshot + +from . import search_tree as tree +from .artifact_primitives import digest +from .gvo import train_group +from .search_records import decode, encode +from .vats import Search + + +def qualify_root(control, campaign, adapter, root, contract): + """Acquire root evidence using real checks and the same authoritative accounting.""" + retained = control.read(campaign)["state"].get("qualified_root") + if retained is not None: + state = decode(retained) + if state.contract != contract or tree.candidate(state, state.root_id) != root: + raise ValueError("Root qualification is bound to another frozen context") + tree.accept(state) + return state + receipts = [] + for check in contract.checks: + doc = control.read(campaign) + binding = { + "candidate": asdict(root), + "context": asdict(contract.context), + "check": check, + "adapter": adapter.identity, + } + operation = "root-" + digest(binding) + previous = doc["operations"].get(operation) + if previous is None: + control.claim( + campaign, + doc["revision"], + operation, + category="root", + cap_ms=adapter.checks[check], + domains=(adapter.domain,), + binding=binding, + ) + claim = control.launch(campaign, operation) + result = adapter.verify(root, check, operation, claim["deadline"]) + elif previous["status"] == "settled": + result = previous["receipt"] + else: + result = adapter.recover(operation, {"kind": "verify", "binding": binding}) + if result is None: + raise ValueError("Root execution unknown; recover its receipt without relaunch") + control.settle( + campaign, + operation, + result, + actual_ms=result.get("elapsed_ms"), + cleanup_verified=result.get("cleanup_verified"), + ) + if ( + result["cleanup_verified"] is not True + or result["verdict"] != "PASS" + or control.read(campaign)["blocked"] + ): + raise ValueError("Root lacks complete admissible real evidence") + attempt = tree.Attempt( + operation, + operation, + operation, + operation, + contract.search_id, + root.candidate_id, + root.artifact_digest, + check, + contract.context, + root.dependencies, + ) + receipts.append( + tree.Receipt(attempt, Verdict.PASS, result["evidence_digest"], result.get("objective")) + ) + state = tree.initialize(contract, root, receipts, dependencies=root.dependencies) + doc = control.read(campaign) + control.update(campaign, doc["revision"], lambda body: body | {"qualified_root": encode(state)}) + return state + + +def import_qualified_root(source, contract): + """Explicit root-only evidence import under identical artifact/check/context scope. + + No new physical execution is asserted: execution/reservation/lease keys and + evidence digests retain their original identity. The receiving attempt is a + scoped import recorded in the campaign; arbitrary child evidence is not reused. + """ + if ( + source.contract.context != contract.context + or source.contract.checks != contract.checks + or source.contract.objective_check != contract.objective_check + or source.contract.direction != contract.direction + ): + raise ValueError("Root evidence reuse requires an identical verification contract") + root = tree.candidate(source, source.root_id) + observed = tree.evidence(source, source.root_id) + if len(observed) != len(contract.checks) or any( + r.verdict != Verdict.PASS for r in observed.values() + ): + raise ValueError("Cannot import invalid root evidence") + receipts = [ + replace( + record, + attempt=replace( + record.attempt, + search_id=contract.search_id, + attempt_id=f"{contract.search_id}-import-{check}", + ), + ) + for check, record in observed.items() + ] + return tree.initialize(contract, root, receipts, dependencies=source.dependencies) + + +def collect_group( + control, campaign, adapter, root, snapshot, settings, *, group_id, size, seed=0, collect=True +): + if type(size) is not int or size < 2: + raise ValueError("A group needs two or more independent searches") + doc = control.read(campaign) + outstanding = sum( + max( + 0, + settings.search_ms + - doc["state"]["searches"].get(f"{group_id}-{i}", {}).get("spent_ms", 0), + ) + for i in range(size) + if doc["state"]["searches"].get(f"{group_id}-{i}", {}).get("status") != "complete" + ) + if outstanding > min(control.available(doc), (doc["deadline"] - time.time()) * 1000): + raise ValueError("Group cannot provide identical search budgets within the campaign cap") + result = [] + for index in range(size): + search_id = f"{group_id}-{index}" + contract = replace( + root.contract, search_id=search_id, improver_snapshot=digest(snapshot.document()) + ) + initial = import_qualified_root(root, contract) + search = Search(control, campaign, search_id, snapshot, settings, adapter) + doc = control.read(campaign) + if search_id not in doc["state"]["searches"]: + search.start(initial, seed=seed + index, collect=collect) + else: + _, retained, retained_tree = search.read() + if ( + retained["seed"] != seed + index + or retained["collect"] != collect + or retained_tree.contract != contract + or tree.candidate(retained_tree, retained_tree.root_id) + != tree.candidate(initial, initial.root_id) + ): + raise ValueError("Resumed group has different frozen starting conditions") + result.append(search.run()) + return tuple(result) + + +def learn( + control, + campaign, + adapter, + root, + snapshot, + settings, + *, + rounds, + group_size, + training_cap_ms, + learning_rate, + seed=0, + mode="reset", + **training, +): + if type(rounds) is not int or rounds < 1 or mode not in ("reset", "carry"): + raise ValueError("Frozen positive outer rounds and Reset/Carry mode are required") + reference = snapshot + for index in range(rounds): + records = collect_group( + control, + campaign, + adapter, + root, + snapshot, + settings, + group_id=f"round-{index}", + size=group_size, + seed=seed + index * group_size, + ) + ids = [decode(r["tree"]).contract.search_id for r in records] + doc = control.read(campaign) + retained = [ + op + for op in doc["operations"].values() + if op["category"] == "training" and op["binding"]["searches"] == ids + ] + if retained: + if len(retained) != 1 or retained[0]["status"] != "settled": + raise ValueError("Unknown training outcome; no second update is permitted") + op = retained[0] + expected = { + "learning_rate": learning_rate, + "expense_weight": 0.0, + "elapsed_weight": 0.0, + **training, + } + if ( + op["binding"]["snapshot"] != digest(snapshot.document()) + or op["binding"]["reference"] != digest(reference.document()) + or digest(op["binding"]["training"]) != digest(expected) + ): + raise ValueError("Frozen outer-loop training configuration changed") + snapshot = Snapshot(**op["receipt"]["checkpoint"]) + else: + snapshot = train_group( + control, + campaign, + records, + snapshot, + reference, + cap_ms=training_cap_ms, + learning_rate=learning_rate, + **training, + ) + if mode == "carry": + best = min( + (decode(record["tree"]) for record in records), + key=lambda state: (-tree.verified_gain(state), state.contract.search_id), + ) + selected = tree.accept(best) + # Carry the complete selected artifact, retaining its actual receipts. + selected = replace(selected, parent_id=None) + root = tree.initialize( + best.contract, + selected, + tree.evidence(best, selected.candidate_id).values(), + dependencies=best.dependencies, + ) + return snapshot + + +def finalize(control, campaign, adapter, selected, *, holdout_scope, accept_result): + """Freeze once; a failed/unknown holdout never reopens search or changes selection. + + accept_result is a fixed independent final gate. Only this function records + stable promotion; search acceptance and trained checkpoints do not promote. + """ + doc = control.read(campaign) + if doc["final_result"] is not None: + return doc["final_result"] + required_scope = getattr(adapter.evaluator, "holdout_scope", None) + if required_scope is not None and holdout_scope != required_scope: + raise ValueError("Final scope must match the evaluator's frozen holdout data identity") + binding = { + "candidate": asdict(selected), + "adapter": adapter.identity, + "checks": adapter.checks, + "scope": holdout_scope, + } + recovering = doc["final_claim"] is not None + if recovering: + if digest(doc["final_claim"]["binding"]) != digest(binding): + raise ValueError("Cannot change the final candidate or validation scope") + else: + control.claim_final(campaign, doc["revision"], holdout_scope, binding) + results = [] + for check, cap in adapter.checks.items(): + doc = control.read(campaign) + operation = "final-" + digest({"campaign": campaign, "binding": binding, "check": check}) + if recovering: + op = doc["operations"].get(operation) + if op is None: + raise ValueError("Interrupted holdout may be consumed; no new execution") + if op["status"] == "settled": + result = op["receipt"] + else: + result = adapter.recover( + operation, + {"kind": "verify", "binding": {"candidate": asdict(selected), "check": check}}, + ) + if result is None: + raise ValueError("Unknown final execution; recover its receipt without rerun") + control.settle( + campaign, + operation, + result, + actual_ms=result.get("elapsed_ms"), + cleanup_verified=result.get("cleanup_verified"), + ) + else: + control.claim( + campaign, + doc["revision"], + operation, + category="final", + cap_ms=cap, + domains=(adapter.domain,), + binding=binding | {"check": check}, + final=True, + ) + claim = control.launch(campaign, operation) + result = adapter.verify(selected, check, operation, claim["deadline"]) + control.settle( + campaign, + operation, + result, + actual_ms=result.get("elapsed_ms"), + cleanup_verified=result.get("cleanup_verified"), + ) + results.append(result) + doc = control.read(campaign) + unresolved = doc["blocked"] or any( + op["status"] != "settled" for op in doc["operations"].values() + ) + passed = ( + not unresolved + and all(r["verdict"] == "PASS" and r["cleanup_verified"] is True for r in results) + and accept_result(results) + ) + result = { + "candidate": selected.candidate_id, + "verdict": "PASS" if passed else "FAIL", + "results": results, + "promoted": passed, + } + if unresolved or any( + r["verdict"] == "INCONCLUSIVE" or r["cleanup_verified"] is not True for r in results + ): + result.update(verdict="INCONCLUSIVE", promoted=False) + control.finish_final( + campaign, + binding, + result, + transition=(lambda body: body | {"stable": asdict(selected)}) if passed else None, + ) + return result diff --git a/src/inferrsi/harness/search_continuations.py b/src/inferrsi/harness/search_continuations.py new file mode 100644 index 000000000..665243f7c --- /dev/null +++ b/src/inferrsi/harness/search_continuations.py @@ -0,0 +1,155 @@ +"""Paid-state continuation sampling, isolated from natural GVO actor groups.""" + +import copy +import math +import time +from dataclasses import asdict, replace + +from .artifact_primitives import digest +from .search_records import decode, encode +from .vats import Action, Search + + +def continue_from(search, step_index, action_key, search_id, *, copy_cap_ms, seed): + """Clone a retained paid decision state and force exactly its first action. + + Original observations are explicit imports with their physical execution IDs. + Copying is charged; all suffix edits/checks use ordinary WorkControl claims. + No holdout or unobserved outcome is used. The entire suffix is Scorer-only. + """ + doc, source, _ = search.read() + if doc["final_claim"] is not None or source["status"] != "complete": + raise ValueError("Continuation source must be a completed development search") + if ( + source["forced"] + or type(step_index) is not int + or not 0 <= step_index < len(source["trace"]) + ): + raise ValueError("Select a retained natural paid decision state") + step = source["trace"][step_index] + actions = [Action(**raw) for raw in step["menu"]] + selected = next((i for i, action in enumerate(actions) if action.key == action_key), None) + if selected is None: + raise ValueError("Forced action is outside the original paid menu") + action = actions[selected] + fork = Search( + search.control, search.campaign, search_id, search.snapshot, search.settings, search.adapter + ) + if search_id in doc["state"]["searches"]: + _, retained, _ = fork.read() + if retained.get("continuation") != { + "source": search.search_id, + "step": step_index, + "action": action_key, + "paid_state": digest(step["paid_state"]), + "seed": seed, + }: + raise ValueError("Continuation identity already belongs to another paid state") + return fork.run() + operation = "copy-" + search_id + search.control.claim( + search.campaign, + doc["revision"], + operation, + category="continuation_copy", + cap_ms=copy_cap_ms, + binding={"search": search_id, "source": search.search_id, "step": step_index}, + ) + search.control.launch(search.campaign, operation) + started = time.monotonic() + current = decode(copy.deepcopy(step["paid_state"])) + # Change the receiving scope, retaining immutable physical source identities. + imports = { + attempt.attempt_id: replace( + attempt, search_id=search_id, attempt_id=f"{search_id}-import-{attempt.attempt_id}" + ) + for attempt in current.attempts + } + current = replace( + current, + contract=replace(current.contract, search_id=search_id), + attempts=tuple(imports[a.attempt_id] for a in current.attempts), + receipts=tuple(replace(r, attempt=imports[r.attempt.attempt_id]) for r in current.receipts), + ) + record = copy.deepcopy(source) + forced_step = copy.deepcopy(step) + now = time.time() + forced_step.update( + action=asdict(action), + features=step["vectors"][selected], + forced=True, + draft_choices=[], + at=now, + ) + record.update( + tree=encode(current), + seed=seed, + forced=True, + trace=[forced_step], + status="active", + pending=None, + spent_ms=step["spent_before"], + started_at=now, + elapsed_ms=0, + draft_attempts=step["draft_attempts"], + initial_gain=step["gain_before"], + initial_spent_ms=step["spent_before"], + continuation={ + "source": search.search_id, + "step": step_index, + "action": action_key, + "paid_state": digest(step["paid_state"]), + "seed": seed, + }, + active_action=None + if action.kind == "accept" + else { + "action": asdict(action), + "cursor": 0, + "last_child": None, + "jobs": step["jobs"], + }, + ) + record.pop("gain", None) + record.pop("stop_reason", None) + elapsed = max(1, math.ceil((time.monotonic() - started) * 1000)) + + def commit(state): + if search_id in state["searches"]: + raise ValueError("Continuation search was concurrently created") + state["searches"][search_id] = record + return state + + search.control.settle( + search.campaign, + operation, + {"record": record}, + actual_ms=elapsed, + cleanup_verified=True, + transition=commit, + ) + if action.kind == "accept": + return fork.finish("forced_accept") + return fork.run() + + +def paired_targets(records, *, expense_weight, elapsed_weight): + """Produce differences only for two settled continuations of the same paid state.""" + from .gvo import suffix_examples + + if len(records) != 2: + raise ValueError("A matched comparison needs exactly two continuations") + left, right = records + keys = ("source", "step", "paid_state", "seed") + if ( + not all(r["forced"] and r.get("continuation") for r in records) + or any(left["continuation"][k] != right["continuation"][k] for k in keys) + or any(left[k] != right[k] for k in ("snapshot", "settings", "adapter")) + or left["continuation"]["action"] == right["continuation"]["action"] + ): + raise ValueError("Scorer pair must share a paid state and frozen continuation policy") + first = [ + suffix_examples(r, expense_weight=expense_weight, elapsed_weight=elapsed_weight)[0] + for r in records + ] + return (first[0][0], first[1][0], first[0][1] - first[1][1]) diff --git a/src/inferrsi/harness/search_records.py b/src/inferrsi/harness/search_records.py new file mode 100644 index 000000000..8377a3587 --- /dev/null +++ b/src/inferrsi/harness/search_records.py @@ -0,0 +1,60 @@ +"""Explicit JSON encoding of immutable search controls, without executable payloads.""" + +from dataclasses import asdict + +from inferrsi.contracts.claim import Verdict + +from . import search_tree as tree + + +def encode(state): + return asdict(state) + + +def decode(raw): + def dependencies(values): + return tuple(tree.Dependency(**value) for value in values) + + def attempt(value): + return tree.Attempt( + **( + value + | { + "context": tree.Context(**value["context"]), + "dependencies": dependencies(value["dependencies"]), + } + ) + ) + + contract = tree.Contract( + **(raw["contract"] | {"context": tree.Context(**raw["contract"]["context"])}) + ) + nodes = tuple( + tree.Candidate( + **( + value + | { + "producer": tree.Producer(**value["producer"]), + "dependencies": dependencies(value["dependencies"]), + } + ) + ) + for value in raw["candidates"] + ) + return tree.SearchState( + contract, + raw["root_id"], + nodes, + tuple(attempt(value) for value in raw["attempts"]), + tuple( + tree.Receipt( + attempt(value["attempt"]), + Verdict(value["verdict"]), + value["evidence_digest"], + value["objective"], + ) + for value in raw["receipts"] + ), + dependencies(raw["dependencies"]), + raw["revision"], + ) diff --git a/src/inferrsi/harness/search_tree.py b/src/inferrsi/harness/search_tree.py index d6623835f..e2fa8fe42 100644 --- a/src/inferrsi/harness/search_tree.py +++ b/src/inferrsi/harness/search_tree.py @@ -256,6 +256,8 @@ def verified_gain(state): check = state.contract.objective_check gain = (evidence(state, selected.candidate_id)[check].objective - evidence(state, state.root_id)[check].objective) + if not isfinite(gain): + raise ValueError("Verified gain exceeds the finite objective domain") return gain if state.contract.direction == "maximize" else -gain diff --git a/src/inferrsi/harness/trials/external.py b/src/inferrsi/harness/trials/external.py index ba532614c..b98826ad6 100644 --- a/src/inferrsi/harness/trials/external.py +++ b/src/inferrsi/harness/trials/external.py @@ -17,6 +17,7 @@ def execute_trial( *, metric: Callable[[dict, str], float] | None = None, environment: dict | None = None, + include_cleanup: bool = False, ) -> dict: """Run one command and retain its record. @@ -27,6 +28,8 @@ def execute_trial( if metric_key is not None and metric is None: raise TypeError("execute_trial needs the caller's metric validation for metric_key") options = {"environment": environment} if environment is not None else {} + if include_cleanup: + options["include_cleanup"] = True record = execute_command(command, cwd, trial_dir, deadline, **options) result_path = Path(record["result_path"]) if metric_key is not None: diff --git a/src/inferrsi/harness/vats.py b/src/inferrsi/harness/vats.py new file mode 100644 index 000000000..bf79022fb --- /dev/null +++ b/src/inferrsi/harness/vats.py @@ -0,0 +1,659 @@ +"""Bounded synchronous VATS over immutable candidates and injected real operations. + +The controller owns search transitions; WorkControl owns persistent claims and +accounting; the adapter owns permitted edits and authenticated real observations. +No value prediction is admitted as evidence. There is no background re-dispatch. +""" + +import math +import random +import time +import uuid +from dataclasses import asdict, dataclass, replace +from itertools import combinations + +from inferrsi.model.search_roles import choose, dot + +from . import search_tree as tree +from .artifact_primitives import digest +from .search_records import decode, encode +from .verification_plans import completion_menu + + +@dataclass(frozen=True) +class Settings: + max_steps: int + max_parents: int + draft_depth: int + max_targets: int + max_jobs: int + max_plans: int + preparation_ms: int + draft_ms: int + search_ms: int + refine_queries: int = 0 + temperature: float = 1.0 + exploration: float = 0.1 + draft_ahead: bool = True + bundled: bool = True + + def __post_init__(self): + for key in ( + "max_steps", + "max_parents", + "draft_depth", + "max_targets", + "max_jobs", + "max_plans", + "preparation_ms", + "draft_ms", + "search_ms", + ): + if type(getattr(self, key)) is not int or getattr(self, key) <= 0: + raise ValueError("VATS bounds must be positive integers") + if type(self.refine_queries) is not int or self.refine_queries < 0: + raise ValueError("Invalid Scorer query cap") + if not math.isfinite(self.temperature) or self.temperature <= 0: + raise ValueError("Invalid policy temperature") + if not 0 <= self.exploration < 1: + raise ValueError("Invalid policy exploration") + + +@dataclass(frozen=True) +class Action: + kind: str + parents: tuple[str, ...] = () + depth: int = 0 + jobs: tuple[str, ...] = () + cost_ms: int = 0 + + @property + def key(self): + return digest(asdict(self)) + + +def depth(state, node): + result = 0 + while node.parent_id is not None: + result += 1 + node = tree.candidate(state, node.parent_id) + return result + + +def features(state, action, available, settings): + """Fixed public features; ACCEPT has a fixed zero Policy logit.""" + if action.kind == "accept": + return (0.0,) * 8 + return ( + 1.0, + float(action.kind == "draft"), + float(action.kind == "verify"), + action.cost_ms / settings.search_ms, + len(state.candidates) / state.contract.max_candidates, + available / settings.search_ms, + action.depth / state.contract.max_depth, + len(action.jobs) / settings.max_jobs, + ) + + +def menu(state, jobs, available, settings, snapshot): + """Preserve singleton completion seeds before optionally refining their unions.""" + available = min(available, settings.search_ms) + plans = completion_menu( + state, + jobs, + available_budget=max(0, available), + available_jobs=settings.max_jobs, + max_targets=settings.max_targets, + max_jobs=settings.max_jobs, + ) + singleton_plans = completion_menu( + state, + jobs, + available_budget=max(0, available), + available_jobs=settings.max_jobs, + max_targets=1, + max_jobs=settings.max_jobs, + ) + seeds = [Action("verify", jobs=plan.job_ids, cost_ms=plan.cost) for plan in singleton_plans] + unions = [Action("verify", jobs=plan.job_ids, cost_ms=plan.cost) for plan in plans] + allowed = {frozenset(a.jobs): a for a in unions} + # Refine priority among feasible bounded unions without evicting completion + # seeds or bypassing Solver's frozen target/job/cost/attempt constraints. + by_job = {job.job_id: job for job in jobs} + queries = 0 + extra = [] + for left, right in combinations(seeds, 2): + if queries + 2 > settings.refine_queries: + break + union = tuple(dict.fromkeys((*left.jobs, *right.jobs))) + if frozenset(union) not in allowed: + continue + cost = sum(by_job[jid].cost for jid in union) + attempts = sum(by_job[jid].candidate_id is not None for jid in union) + if ( + cost > available + or len(union) > settings.max_jobs + or attempts + len(state.attempts) > state.contract.max_attempts + or any(set(by_job[jid].conflicts) & set(union) for jid in union) + ): + continue + trial = allowed[frozenset(union)] + queries += 2 + if dot(snapshot.scorer, features(state, trial, available, settings)) > dot( + snapshot.scorer, features(state, left, available, settings) + ): + extra.append(trial) + if not settings.bundled: + # Ablation: individual executable jobs, explicitly retaining incomplete evidence. + seeds = [ + Action("verify", jobs=(job.job_id,), cost_ms=job.cost) + for job in jobs + if not job.prerequisites + and job.cost <= available + and job.candidate_id is not None + and job.check_id in tree.missing_checks(state, job.candidate_id) + ] + extra = [] + unions = [] + parents = [ + node + for node in state.candidates + if tree.candidate_valid(state, node.candidate_id) + and (settings.draft_ahead or node in tree.complete_candidates(state)) + ] + drafts = [] + slots = state.contract.max_candidates - len(state.candidates) + for count in range(1, min(len(parents), settings.max_parents) + 1): + for selected in combinations(parents, count): + for height in range(1, settings.draft_depth + 1): + cost = count * height * settings.draft_ms + if ( + cost <= available + and count * height <= slots + and all( + depth(state, node) + height <= state.contract.max_depth for node in selected + ) + ): + drafts.append( + Action( + "draft", + tuple(node.candidate_id for node in selected), + height, + cost_ms=cost, + ) + ) + if len(drafts) >= settings.max_plans: + break + if len(drafts) >= settings.max_plans: + break + if len(drafts) >= settings.max_plans: + break + required = [Action("accept"), *seeds] + if len(required) > settings.max_plans: + raise ValueError("Menu cap cannot preserve all feasible completion seeds") + unique = {action.key: action for action in required} + for action in (*extra, *drafts, *unions): + if len(unique) >= settings.max_plans: + break + unique[action.key] = action + return tuple(unique.values()) + + +class Search: + """A resumable search bound to one immutable role snapshot and adapter contract. + + Adapter methods: jobs(tree), draft(parent, choice_context, snapshot, rng, + destination, deadline), verify(node, check, destination, deadline), recover(...). + Draft returns (Candidate, Choice|None, receipt); verify returns Receipt facts. + Both return authenticated elapsed_ms and cleanup_verified in their receipt. + """ + + def __init__(self, control, campaign, search_id, snapshot, settings, adapter): + self.control, self.campaign, self.search_id = control, campaign, search_id + self.snapshot, self.settings, self.adapter = snapshot, settings, adapter + self.snapshot_id = digest(snapshot.document()) + self.settings_id = digest(asdict(settings)) + + def read(self): + document = self.control.read(self.campaign) + search = document["state"]["searches"][self.search_id] + if ( + search["snapshot"] != self.snapshot_id + or search["settings"] != self.settings_id + or search["adapter"] != self.adapter.identity + ): + raise ValueError("Frozen search roles, menu or adapter changed") + return document, search, decode(search["tree"]) + + def start(self, initial, *, seed, collect=True, forced=False): + if ( + initial.contract.search_id != self.search_id + or initial.contract.improver_snapshot != self.snapshot_id + ): + raise ValueError("Search root must bind the frozen search and improver") + tree.accept(initial) + if not forced and len(initial.candidates) != 1: + raise ValueError("Independent natural searches must start from one qualified root") + doc = self.control.read(self.campaign) + + def transition(state): + if self.search_id in state["searches"]: + raise ValueError("Search already exists; resume it") + state["searches"][self.search_id] = { + "tree": encode(initial), + "snapshot": self.snapshot_id, + "settings": self.settings_id, + "adapter": self.adapter.identity, + "seed": seed, + "collect": collect, + "forced": forced, + "trace": [], + "status": "active", + "pending": None, + "spent_ms": 0, + "started_at": time.time(), + "elapsed_ms": 0, + "draft_attempts": 0, + "active_action": None, + "initial_gain": tree.verified_gain(initial), + "selected": initial.root_id, + } + return state + + self.control.update(self.campaign, doc["revision"], transition) + + def _change(self, state, operation, result): + search = state["searches"][self.search_id] + pending = search["pending"] + if pending is None or pending["operation"] != operation: + raise ValueError("Late result does not match the active work identity") + current = decode(search["tree"]) + if pending["kind"] == "draft": + search["draft_attempts"] += 1 + if result.get("candidate") is not None: + raw = result["candidate"] + node = tree.Candidate( + **( + raw + | { + "producer": tree.Producer(**raw["producer"]), + "dependencies": tuple( + tree.Dependency(**d) for d in raw["dependencies"] + ), + } + ) + ) + try: + tree.accept(current) + root_valid = True + except ValueError: + root_valid = False + if root_valid and tree.candidate_valid(current, node.parent_id): + current = tree.add_candidate(current, node, expected_revision=current.revision) + else: + search.setdefault("rejected_drafts", []).append(operation) + else: + bound = next(a for a in current.attempts if a.attempt_id == operation) + from inferrsi.contracts.claim import Verdict + + receipt = tree.Receipt( + bound, + Verdict(result["verdict"]), + result["evidence_digest"], + result.get("objective"), + ) + current = tree.record_receipt(current, receipt, expected_revision=current.revision) + search["tree"] = encode(current) + if tree.complete_candidates(current) and tree.evidence(current, current.root_id): + try: + search["selected"] = tree.accept(current).candidate_id + except ValueError: + search.update(status="blocked", selected=None) + else: + search.update(status="blocked", selected=None) + search["spent_ms"] += result["elapsed_ms"] + search["pending"] = None + active = search["active_action"] + if active is not None: + active["cursor"] += 1 + if pending["kind"] == "draft": + active["last_child"] = ( + result["candidate"]["candidate_id"] + if result.get("candidate") + and any( + n.candidate_id == result["candidate"]["candidate_id"] + for n in current.candidates + ) + else None + ) + if result.get("choice") is not None: + search["trace"][-1]["draft_choices"].append(result["choice"]) + return state + + def invalidate(self, dependency): + """Persist invalidation and incumbent replacement before any cancellation. + + In-flight work remains charged to its original claim; late receipts can + still be archived by recover(), but can never revive invalid evidence. + """ + doc, _, _ = self.read() + + def transition(state): + record = state["searches"][self.search_id] + current = decode(record["tree"]) + current = tree.invalidate(current, dependency, expected_revision=current.revision) + record["tree"] = encode(current) + try: + record["selected"] = tree.accept(current).candidate_id + except ValueError: + record.update(status="blocked", selected=None) + record.setdefault("invalidations", []).append(dependency) + return state + + self.control.update(self.campaign, doc["revision"], transition) + + def recover(self): + _, search, _ = self.read() + pending = search["pending"] + if pending is None: + return + # Receipt-only recovery. Never call draft/verify from this path. + result = self.adapter.recover(pending["operation"], pending) + if result is None: + raise ValueError("Unknown execution: retain reservation and do not relaunch") + self.control.settle( + self.campaign, + pending["operation"], + result, + actual_ms=result.get("elapsed_ms"), + cleanup_verified=result.get("cleanup_verified"), + transition=lambda state: self._change(state, pending["operation"], result), + ) + + def _execute(self, kind, node, check=None): + doc, search, current = self.read() + operation = self.search_id + "-" + uuid.uuid4().hex + cap = self.settings.draft_ms if kind == "draft" else check.cost + binding = { + "search": self.search_id, + "candidate": asdict(node), + "check": check.check_id if check else None, + "snapshot": self.snapshot_id, + "adapter": self.adapter.identity, + } + + def pending(state): + search = state["searches"][self.search_id] + current = decode(search["tree"]) + if search["pending"] is not None: + raise ValueError("An operation is already pending") + if kind == "verify": + attempt = tree.Attempt( + operation, + operation, + operation, + operation, + self.search_id, + node.candidate_id, + node.artifact_digest, + check.check_id, + current.contract.context, + node.dependencies, + ) + current = tree.register_attempt( + current, attempt, expected_revision=current.revision + ) + search["tree"] = encode(current) + search["pending"] = {"operation": operation, "kind": kind, "binding": binding} + return state + + self.control.claim( + self.campaign, + doc["revision"], + operation, + category=kind, + cap_ms=cap, + binding=binding, + domains=self.adapter.domains(kind, node, check), + transition=pending, + ) + claim = self.control.launch(self.campaign, operation) + # Any exception retains the claim. The adapter's authenticated receipt is + # the only recovery route, including when writing the state below fails. + if kind == "draft": + rng = random.Random( + f"{search['seed']}:{len(current.candidates)}:{len(search['trace'])}" + ) + result = self.adapter.draft( + node, current, self.snapshot, rng, operation, claim["deadline"] + ) + else: + result = self.adapter.verify(node, check.check_id, operation, claim["deadline"]) + self.control.settle( + self.campaign, + operation, + result, + actual_ms=result.get("elapsed_ms"), + cleanup_verified=result.get("cleanup_verified"), + transition=lambda state: self._change(state, operation, result), + ) + _, updated, _ = self.read() + if updated["pending"] is not None: + raise ValueError("Execution or cleanup remains unknown") + return result + + def run(self, *, forced_action=None): + self.recover() + while True: + doc, search, current = self.read() + if search["status"] == "complete": + return search + if search["status"] == "blocked": + raise ValueError("Search root evidence is invalid; search remains blocked") + if doc["blocked"] or doc["final_claim"] is not None: + raise ValueError("Campaign blocked or final selection frozen") + if any( + op["status"] != "settled" and op["binding"].get("search") == self.search_id + for op in doc["operations"].values() + ): + raise ValueError("Unresolved claim: recover without relaunch") + if search["active_action"] is not None: + self._continue_action() + continue + remaining = min( + self.control.available(doc), self.settings.search_ms - search["spent_ms"] + ) + attempts_left = ( + current.contract.max_attempts - len(current.attempts) - search["draft_attempts"] + ) + if ( + len(search["trace"]) >= self.settings.max_steps + or attempts_left <= 0 + or remaining <= self.settings.preparation_ms + or time.time() >= doc["deadline"] + ): + return self.finish("limit") + op = self.search_id + "-prepare-" + uuid.uuid4().hex + self.control.claim( + self.campaign, + doc["revision"], + op, + category="preparation", + cap_ms=self.settings.preparation_ms, + binding={"search": self.search_id}, + ) + claim = self.control.launch(self.campaign, op) + started = time.monotonic() + jobs = self.adapter.jobs(current) + planning_state = replace( + current, + contract=replace( + current.contract, + max_attempts=current.contract.max_attempts - search["draft_attempts"], + ), + ) + actions = menu( + planning_state, + jobs, + remaining - self.settings.preparation_ms, + self.settings, + self.snapshot, + ) + actions = tuple( + a for a in actions if a.kind != "draft" or len(a.parents) * a.depth <= attempts_left + ) + vectors = tuple( + features(current, action, remaining, self.settings) for action in actions + ) + rng = random.Random(f"{search['seed']}:policy:{len(search['trace'])}") + choice = choose( + self.snapshot, + "policy", + [action.key for action in actions], + vectors, + rng=rng, + context=digest(encode(current)), + collect=search["collect"], + temperature=self.settings.temperature, + exploration=self.settings.exploration if search["collect"] else 0, + ) + selected = choice.selected + forced = forced_action is not None and not search["trace"] + if forced: + if not search["forced"]: + raise ValueError("Forced continuation must be isolated from actor training") + selected = next( + i for i, action in enumerate(actions) if action.key == forced_action + ) + action = actions[selected] + elapsed = max(1, math.ceil((time.monotonic() - started) * 1000)) + + def prepared(state): + record = state["searches"][self.search_id] + record["spent_ms"] += elapsed + record["trace"].append( + { + "action": asdict(action), + "choice": asdict(choice), + "forced": forced, + "features": vectors[selected], + "draft_choices": [], + "gain_before": tree.verified_gain(current), + "spent_before": record["spent_ms"], + "at": time.time(), + "paid_state": encode(current), + "menu": [asdict(a) for a in actions], + "vectors": vectors, + "jobs": [asdict(job) for job in jobs], + "draft_attempts": record["draft_attempts"], + } + ) + if action.kind != "accept": + record["active_action"] = { + "action": asdict(action), + "cursor": 0, + "last_child": None, + "jobs": [asdict(job) for job in jobs], + } + return state + + self.control.settle( + self.campaign, + op, + {"menu": [asdict(a) for a in actions], "elapsed_ms": elapsed}, + actual_ms=elapsed, + cleanup_verified=True, + transition=prepared, + ) + if time.time() >= claim["deadline"]: + raise ValueError("Preparation deadline exceeded") + if action.kind == "accept": + return self.finish("accept") + + def _continue_action(self): + from .verification_plans import Job + + while True: + doc, search, current = self.read() + active = search["active_action"] + action = Action(**active["action"]) + cursor = active["cursor"] + limit = ( + len(action.parents) * action.depth if action.kind == "draft" else len(action.jobs) + ) + if cursor >= limit: + + def done(state): + state["searches"][self.search_id]["active_action"] = None + return state + + self.control.update(self.campaign, doc["revision"], done) + return + next_cap = ( + self.settings.draft_ms + if action.kind == "draft" + else next( + job["cost"] for job in active["jobs"] if job["job_id"] == action.jobs[cursor] + ) + ) + if ( + time.time() >= doc["deadline"] + or next_cap > self.control.available(doc) + or next_cap > self.settings.search_ms - search["spent_ms"] + ): + + def exhausted(state): + record = state["searches"][self.search_id] + record["active_action"] = None + record["block_stop_reason"] = "budget_or_deadline" + return state + + self.control.update(self.campaign, doc["revision"], exhausted) + return + if action.kind == "draft": + branch, level = divmod(cursor, action.depth) + if level and active["last_child"] is None: + + def skip(state): + state["searches"][self.search_id]["active_action"]["cursor"] = ( + branch + 1 + ) * action.depth + return state + + self.control.update(self.campaign, doc["revision"], skip) + continue + parent = active["last_child"] if level else action.parents[branch] + self._execute("draft", tree.candidate(current, parent)) + else: + by_id = {raw["job_id"]: Job(**raw) for raw in active["jobs"]} + job = by_id[action.jobs[cursor]] + if job.candidate_id is None: + raise ValueError("Adapter must package shared setup into its real check") + self._execute("verify", tree.candidate(current, job.candidate_id), job) + + def finish(self, reason): + doc, search, current = self.read() + if search["pending"] is not None: + raise ValueError("Cannot accept unsettled work") + if search["active_action"] is not None: + raise ValueError("Cannot accept an unfinished bounded work block") + if any( + op["status"] != "settled" and op["binding"].get("search") == self.search_id + for op in doc["operations"].values() + ): + raise ValueError("Cannot accept an unresolved claim") + + def transition(state): + record = state["searches"][self.search_id] + record.update( + status="complete", + stop_reason=reason, + selected=tree.accept(current).candidate_id, + gain=tree.verified_gain(current), + elapsed_ms=max(1, math.ceil((time.time() - record["started_at"]) * 1000)), + ) + return state + + self.control.update(self.campaign, doc["revision"], transition) + return self.read()[1] diff --git a/src/inferrsi/harness/work_control.py b/src/inferrsi/harness/work_control.py new file mode 100644 index 000000000..3a0993c42 --- /dev/null +++ b/src/inferrsi/harness/work_control.py @@ -0,0 +1,278 @@ +"""Campaign accounting and CAS in the shared RunStore database. + +This is a controller for trusted host operations, not an executor or sandbox. +All cooperating controllers must share the same RunStore for resource domains +and holdout scopes to be exclusive. Unknown work retains its entire reservation. +""" + +import json +import math +import time +from contextlib import contextmanager + +from .artifact_primitives import digest +from .run_store import _transaction + + +def _json(value): + return json.dumps(value, sort_keys=True, separators=(",", ":"), allow_nan=False) + + +def _positive(value): + if type(value) is not int or value <= 0: + raise ValueError("Work limits must be positive integer milliseconds") + + +class WorkControl: + def __init__(self, store): + self.store = store + if store._read_only: + return + with store._connection() as db, _transaction(db): + db.execute( + "CREATE TABLE IF NOT EXISTS work_campaigns " + "(id TEXT PRIMARY KEY, revision INTEGER NOT NULL, body TEXT NOT NULL)" + ) + db.execute( + "CREATE TABLE IF NOT EXISTS work_domains " + "(id TEXT PRIMARY KEY, campaign TEXT NOT NULL, operation TEXT NOT NULL)" + ) + db.execute( + "CREATE TABLE IF NOT EXISTS work_holdouts " + "(scope TEXT PRIMARY KEY, campaign TEXT NOT NULL, binding TEXT NOT NULL)" + ) + + @contextmanager + def _transaction(self, campaign, revision=None): + with self.store._connection() as db, _transaction(db): + row = db.execute( + "SELECT revision, body FROM work_campaigns WHERE id=?", (campaign,) + ).fetchone() + if row is None: + raise ValueError("Unknown campaign") + if revision is not None and row[0] != revision: + raise ValueError("Stale campaign revision") + body = json.loads(row[1]) + yield db, body + db.execute( + "UPDATE work_campaigns SET revision=?, body=? WHERE id=?", + (row[0] + 1, _json(body), campaign), + ) + + def create(self, campaign, contract, state, *, cap_ms, final_reserve_ms, deadline): + _positive(cap_ms) + _positive(final_reserve_ms) + if ( + not campaign + or final_reserve_ms >= cap_ms + or not math.isfinite(deadline) + or deadline <= time.time() + ): + raise ValueError("Invalid campaign budget or deadline") + body = { + "contract": contract, + "contract_digest": digest(contract), + "state": state, + "cap_ms": cap_ms, + "final_reserve_ms": final_reserve_ms, + "deadline": deadline, + "spent_ms": 0, + "operations": {}, + "events": [], + "final_claim": None, + "final_result": None, + "blocked": False, + } + with self.store._connection() as db, _transaction(db): + db.execute("INSERT INTO work_campaigns VALUES (?, 0, ?)", (campaign, _json(body))) + return self.read(campaign) + + def read(self, campaign): + with self.store._connection() as db: + row = db.execute( + "SELECT revision, body FROM work_campaigns WHERE id=?", (campaign,) + ).fetchone() + if row is None: + raise ValueError("Unknown campaign") + body = json.loads(row[1]) + if digest(body["contract"]) != body["contract_digest"]: + raise ValueError("Frozen campaign contract changed") + return {"revision": row[0], **body} + + @staticmethod + def available(body, *, final=False): + held = sum(op["cap_ms"] for op in body["operations"].values() if op["status"] != "settled") + return body["cap_ms"] - body["spent_ms"] - held - (0 if final else body["final_reserve_ms"]) + + def update(self, campaign, revision, transition): + """Persist a pure state transition and incumbent recomputation in one CAS.""" + with self._transaction(campaign, revision) as (_, body): + if body["blocked"] or body["final_claim"] is not None: + raise ValueError("Campaign transitions are blocked or final selection is frozen") + body["state"] = transition(body["state"]) + return self.read(campaign) + + def claim( + self, + campaign, + revision, + operation, + *, + category, + cap_ms, + domains=(), + binding, + final=False, + transition=None, + ): + _positive(cap_ms) + domains = tuple(sorted(set(domains))) + if not operation or not category or any(not item for item in domains): + raise ValueError("Missing operation or resource identity") + if final != (category == "final"): + raise ValueError("Final reservations require the final work category") + with self._transaction(campaign, revision) as (db, body): + if operation in body["operations"]: + raise ValueError("Operation already claimed; recover without relaunch") + if body["blocked"] or (body["final_claim"] is not None and not final): + raise ValueError("Campaign is blocked or final selection is frozen") + if final and body["final_claim"] is None: + raise ValueError("Final work requires a durable one-time claim") + if time.time() >= body["deadline"] or cap_ms > self.available(body, final=final): + raise ValueError("Work exceeds remaining budget or deadline") + for domain in domains: + if db.execute("SELECT 1 FROM work_domains WHERE id=?", (domain,)).fetchone(): + raise ValueError("Measurement domain is already leased") + db.execute( + "INSERT INTO work_domains VALUES (?, ?, ?)", (domain, campaign, operation) + ) + body["operations"][operation] = { + "category": category, + "binding": binding, + "cap_ms": cap_ms, + "domains": domains, + "status": "claimed", + "receipt": None, + "deadline": min(body["deadline"], time.time() + cap_ms / 1000), + "execution_key": operation, + } + body["events"].append({"kind": "claimed", "operation": operation, "at": time.time()}) + if transition is not None: + body["state"] = transition(body["state"]) + return self.read(campaign) + + def launch(self, campaign, operation): + """Persist intent before external launch. A crash here must never relaunch.""" + expired = False + with self._transaction(campaign) as (db, body): + op = body["operations"][operation] + if op["status"] != "claimed": + raise ValueError("Execution intent already recorded") + expired = time.time() >= op["deadline"] + if expired: + op.update(status="settled", actual_ms=0, receipt={"not_started": "deadline"}) + db.execute( + "DELETE FROM work_domains WHERE campaign=? AND operation=?", + (campaign, operation), + ) + else: + op.update(status="launched", started_at=time.time()) + body["events"].append( + { + "kind": "not_started" if expired else "launch_intent", + "operation": operation, + "at": time.time(), + } + ) + if expired: + raise ValueError("Deadline expired before launch") + return self.read(campaign)["operations"][operation] + + def settle(self, campaign, operation, receipt, *, actual_ms, cleanup_verified, transition=None): + """Only authenticated, owner-scoped receipts may be passed by the adapter. + + Receipt replay is idempotent. Evidence and accounting commit together. + Unknown usage/cleanup does not release the domain or budget reservation. + """ + if actual_ms is not None and (type(actual_ms) is not int or actual_ms < 0): + raise ValueError("Usage must be nonnegative integer milliseconds or unknown") + with self._transaction(campaign) as (db, body): + op = body["operations"][operation] + settlement = { + "receipt": receipt, + "actual_ms": actual_ms, + "cleanup_verified": cleanup_verified, + } + if op["status"] == "settled": + if _json(op.get("settlement")) != _json(settlement): + raise ValueError("Conflicting duplicate settlement") + return + if op["status"] not in ("launched", "unknown"): + raise ValueError("Cannot settle an unlaunched operation") + op["settlement"] = settlement + if actual_ms is None or cleanup_verified is not True: + op["status"] = "unknown" + body["events"].append( + {"kind": "unknown", "operation": operation, "at": time.time()} + ) + else: + op.update(status="settled", actual_ms=actual_ms, receipt=receipt) + body["spent_ms"] += actual_ms + executable_ms = min(op["cap_ms"], (op["deadline"] - op["started_at"]) * 1000) + if actual_ms > executable_ms or body["spent_ms"] > body["cap_ms"]: + body["blocked"] = True + if transition is not None and not body["blocked"]: + body["state"] = transition(body["state"]) + db.execute( + "DELETE FROM work_domains WHERE campaign=? AND operation=?", + (campaign, operation), + ) + body["events"].extend( + [ + {"kind": "cleanup_confirmed", "operation": operation, "at": time.time()}, + {"kind": "released", "operation": operation, "at": time.time()}, + ] + ) + return self.read(campaign) + + def claim_final(self, campaign, revision, scope, binding): + """Consume a shared holdout scope before any access, including interrupted access.""" + if not isinstance(scope, str) or not scope.strip(): + raise ValueError("Holdout scope must have a nonempty identity") + with self._transaction(campaign, revision) as (db, body): + if ( + body["final_claim"] is not None + or body["blocked"] + or any(op["status"] != "settled" for op in body["operations"].values()) + ): + raise ValueError("Final is already claimed or work remains unresolved") + if time.time() >= body["deadline"]: + raise ValueError("Final deadline exhausted") + if db.execute("SELECT 1 FROM work_holdouts WHERE scope=?", (scope,)).fetchone(): + raise ValueError("Holdout scope already consumed in this RunStore") + db.execute( + "INSERT INTO work_holdouts VALUES (?, ?, ?)", (scope, campaign, _json(binding)) + ) + body["final_claim"] = {"scope": scope, "binding": binding} + return self.read(campaign) + + def finish_final(self, campaign, binding, result, *, transition=None): + with self._transaction(campaign) as (_, body): + if body["final_claim"] is None or _json(body["final_claim"]["binding"]) != _json( + binding + ): + raise ValueError("Final result does not match the frozen selection") + if body["final_result"] is not None: + if _json(body["final_result"]) != _json(result): + raise ValueError("Final result is immutable; export the existing result") + return + if transition is not None and ( + body["blocked"] + or any(op["status"] != "settled" for op in body["operations"].values()) + or result.get("verdict") != "PASS" + ): + raise ValueError("Unresolved or over-budget work cannot promote a candidate") + body["final_result"] = result + if transition is not None: + body["state"] = transition(body["state"]) + return self.read(campaign) diff --git a/src/inferrsi/model/search_roles.py b/src/inferrsi/model/search_roles.py new file mode 100644 index 000000000..3e5df28cf --- /dev/null +++ b/src/inferrsi/model/search_roles.py @@ -0,0 +1,215 @@ +"""Trainable finite-edit Drafter, independent Policy and linear continuation Scorer. + +The reference CPU model samples tokens from a frozen vocabulary of permitted edit +operators. It is not a language-model fine-tuner. Hosts may supply another role +implementation, but must preserve observed contexts, masks and behavior laws. +""" + +from dataclasses import asdict, dataclass, replace +from math import exp, fsum, isfinite, log + + +def finite(value): + if type(value) not in (float, int) or not isfinite(value): + raise ValueError("Expected a finite numeric observation") + return value + + +def dot(left, right): + if len(left) != len(right) or not left: + raise ValueError("Role feature dimension mismatch") + return finite(fsum(finite(a) * finite(b) for a, b in zip(left, right))) + + +def distribution(weights, features, temperature=1.0, exploration=0.0): + if finite(temperature) <= 0 or not 0 <= finite(exploration) < 1 or not features: + raise ValueError("Invalid sampling law") + logits = [dot(weights, row) / temperature for row in features] + scale = max(logits) + raw = [exp(value - scale) for value in logits] + total = fsum(raw) + soft = tuple(value / total for value in raw) + probabilities = tuple((1 - exploration) * p + exploration / len(soft) for p in soft) + if any(p <= 0 for p in probabilities): + raise ValueError("Sampling law underflows; rescale model or increase exploration") + return probabilities, soft + + +@dataclass(frozen=True) +class Snapshot: + drafter: tuple[float, ...] + policy: tuple[float, ...] + scorer: tuple[float, ...] + origin: str + + def __post_init__(self): + for name in ("drafter", "policy", "scorer"): + values = tuple(getattr(self, name)) + if not values: + raise ValueError("Empty role parameters") + for value in values: + finite(value) + object.__setattr__(self, name, values) + if not self.origin: + raise ValueError("Checkpoint provenance is required") + + def document(self): + return { + **asdict(self), + "drafter": list(self.drafter), + "policy": list(self.policy), + "scorer": list(self.scorer), + } + + +@dataclass(frozen=True) +class Choice: + role: str + options: tuple[str, ...] + features: tuple[tuple[float, ...], ...] + selected: int + probabilities: tuple[float, ...] + temperature: float + exploration: float + context: str + sampled: bool = True + + def __post_init__(self): + if ( + self.role not in ("drafter", "policy") + or not self.context + or len(set(self.options)) != len(self.options) + or not 0 <= self.selected < len(self.options) + or len(self.options) != len(self.features) + or len(self.options) != len(self.probabilities) + or abs(fsum(self.probabilities) - 1) > 1e-10 + or any(not 0 < finite(p) <= 1 for p in self.probabilities) + ): + raise ValueError("Invalid recorded role choice") + + +def choose( + snapshot, role, options, features, *, rng, context, collect, temperature=1.0, exploration=0.0 +): + probabilities, _ = distribution(getattr(snapshot, role), features, temperature, exploration) + if collect or role == "drafter": + selected = rng.choices(range(len(options)), weights=probabilities, k=1)[0] + else: + selected = max(range(len(options)), key=lambda index: probabilities[index]) + return Choice( + role, + tuple(options), + tuple(tuple(row) for row in features), + selected, + probabilities, + temperature, + exploration, + context, + sampled=collect or role == "drafter", + ) + + +def actor_gradient(weights, reference, choice, advantage, clip, kl_weight): + """Gradient ascent for the clipped surrogate minus KL to a frozen reference.""" + probabilities, soft = distribution( + weights, choice.features, choice.temperature, choice.exploration + ) + refs, _ = distribution(reference, choice.features, choice.temperature, choice.exploration) + ratio = finite(probabilities[choice.selected] / choice.probabilities[choice.selected]) + active = not ((advantage > 0 and ratio > 1 + clip) or (advantage < 0 and ratio < 1 - clip)) + means = [fsum(p * row[d] for p, row in zip(soft, choice.features)) for d in range(len(weights))] + jacobian = [ + tuple( + (1 - choice.exploration) * p * (row[d] - means[d]) / choice.temperature + for d in range(len(weights)) + ) + for p, row in zip(soft, choice.features) + ] + return tuple( + finite( + ( + advantage / choice.probabilities[choice.selected] * jacobian[choice.selected][d] + if active + else 0 + ) + - kl_weight + * fsum( + jacobian[i][d] * (log(probabilities[i] / refs[i]) + 1) + for i in range(len(probabilities)) + ) + ) + for d in range(len(weights)) + ) + + +def update( + snapshot, + reference, + actor_examples, + value_examples, + pair_examples=(), + *, + learning_rate, + clip=0.2, + kl_weight=0.01, + pair_weight=1.0, + epochs=1, + groups=1, +): + """Update actual parameters; forced trajectories belong only in value/pair examples. + + Actor examples are (Choice, whole-search advantage), value examples are + (features, realized suffix return), pairs are (left features, right features, + paid observed return difference). Missing targets are rejected, never imputed. + """ + if ( + finite(learning_rate) <= 0 + or not 0 < finite(clip) < 1 + or finite(kl_weight) < 0 + or finite(pair_weight) < 0 + or type(epochs) is not int + or epochs < 1 + or type(groups) is not int + or groups < 1 + ): + raise ValueError("Invalid frozen training settings") + actors, values, pairs = tuple(actor_examples), tuple(value_examples), tuple(pair_examples) + for choice, advantage in actors: + finite(advantage) + if not choice.sampled: + raise ValueError("Deterministic actions cannot enter the actor objective") + replay, _ = distribution( + getattr(snapshot, choice.role), choice.features, choice.temperature, choice.exploration + ) + if any(abs(a - b) > 1e-10 for a, b in zip(replay, choice.probabilities)): + raise ValueError("Behavior probabilities do not match the frozen checkpoint") + current = snapshot + for _ in range(epochs): + changed = {} + for role in ("drafter", "policy"): + weights = getattr(current, role) + gradients = [ + actor_gradient( + weights, getattr(reference, role), choice, advantage, clip, kl_weight + ) + for choice, advantage in actors + if choice.role == role + ] + changed[role] = tuple( + finite(w + learning_rate * fsum(g[d] for g in gradients) / groups) + for d, w in enumerate(weights) + ) + weights = current.scorer + gradient = [0.0] * len(weights) + for features, target in values: + error = dot(weights, features) - finite(target) + for d in range(len(weights)): + gradient[d] += 2 * error * features[d] / len(values) + for left, right, target in pairs: + delta = tuple(a - b for a, b in zip(left, right)) + error = dot(weights, delta) - finite(target) + for d in range(len(weights)): + gradient[d] += pair_weight * 2 * error * delta[d] / len(pairs) + changed["scorer"] = tuple(finite(w - learning_rate * g) for w, g in zip(weights, gradient)) + current = replace(current, **changed) + return current diff --git a/tests/test_search_benchmarks.py b/tests/test_search_benchmarks.py new file mode 100644 index 000000000..5cc1ba6b3 --- /dev/null +++ b/tests/test_search_benchmarks.py @@ -0,0 +1,115 @@ +import json +import time +from types import SimpleNamespace + +import pytest + +from inferrsi.apps.cli.arguments import parser +from inferrsi.apps.search_benchmarks import EngineEvaluator, SkillEvaluator +from inferrsi.apps.speculative_search import command +from inferrsi.benchmark.improvement import SelectionPolicy + + +def skill_report(base_tokens=100, tokens=80, *, verdict="PASS", outcome="unchanged"): + def trial(count, result): + return { + "verdict": result, + "elapsed_seconds": 1, + "observations": { + "usage": None + if count is None + else {"input_tokens": count, "output_tokens": 0, "cached_input_tokens": 0}, + "tool_calls": 0, + }, + } + + return { + "status": {"execution_state": "completed"}, + "pairs": [ + { + "baseline": trial(base_tokens, "PASS"), + "candidate": trial(tokens, verdict), + "outcome": outcome, + } + ], + } + + +@pytest.mark.parametrize( + "report,split,verdict", + [ + (skill_report(), "development", "PASS"), + (skill_report(tokens=1, verdict="FAIL", outcome="regression"), "development", "FAIL"), + (skill_report(base_tokens=None), "development", "INCONCLUSIVE"), + (skill_report(tokens=None), "development", "INCONCLUSIVE"), + (skill_report(tokens=101), "holdout", "FAIL"), + (skill_report(tokens=100), "holdout", "PASS"), + ], +) +def test_skill_quality_unknown_usage_and_independent_holdout_gates(report, split, verdict): + comparator = SimpleNamespace(identity="explicit-comparator", run=lambda *a, **k: (report, True)) + evaluator = SkillEvaluator( + comparator, split=split, policy=SelectionPolicy(1, 0), token_cap=1000, task_count=1 + ) + result = evaluator.evaluate(None, "evaluation", None, time.time() + 1) + assert result["verdict"] == verdict + assert (result["objective"] is not None) == (verdict == "PASS") + + +def test_engine_uses_frozen_complete_task_protocol_and_rejects_mutation(tmp_path): + evaluator = EngineEvaluator( + "qwen21-denoise-npu", + tmp_path / "frozen", + arguments=["--pairs", "1", "--samples", "1", "--warmups", "0"], + ) + assert evaluator.plan["evaluation_settings"] == {"pairs": 1, "samples": 1, "warmups": 0} + (evaluator.frozen / "reference.py").write_text("changed reference") + with pytest.raises(ValueError, match="resources changed"): + evaluator.evaluate(tmp_path, "evaluation", tmp_path / "output", time.time() + 1) + assert not (tmp_path / "output").exists() + + +def test_cli_requires_explicit_shared_store_and_read_commands_do_not_create_one(tmp_path): + args = parser().parse_args(["search", "status", "--session", str(tmp_path / "absent")]) + with pytest.raises(FileNotFoundError): + command(args) + assert not (tmp_path / "absent").exists() + with pytest.raises(SystemExit): + parser().parse_args(["search", "engine", "--session", str(tmp_path / "run")]) + + +def test_skill_comparator_preparation_does_not_access_holdout(tmp_path): + from tests.fixtures.harness_improvement_demo import configuration + from tools.speculative_skill import Comparator + + config, repo = configuration(tmp_path) + comparison = {k: config[k] for k in ("skill", "skill_root", "model", "limits")} + comparison.update( + mode="update", + client={"explicit": "preparation-only-test"}, + snapshots={"baseline": {"git_revision": config["baseline"]}}, + ) + def descriptor(split): + return { + "path": str(tmp_path / (split + ".json")), + "sha256": split, + "scope": split, + "task_count": 3, + } + bound = Comparator( + repo, + { + "comparison": comparison, + "allowed_paths": config["allowed_paths"], + "splits": {s: descriptor(s) for s in ("development", "holdout")}, + }, + tmp_path / "baseline-snapshot", + ) + exported = bound.export_root(tmp_path / "candidate") + assert (exported / "selected/SKILL.md").is_file() + assert not (tmp_path / "holdout.json").exists() + assert not (tmp_path / "development.json").exists() + # The prepared adapter still fails before provider access if its split is missing. + with pytest.raises(FileNotFoundError): + bound.run(exported, tmp_path / "run", time.time() + 1, split="development") + json.dumps(bound.config) diff --git a/tests/test_search_roles.py b/tests/test_search_roles.py new file mode 100644 index 000000000..3378e7bee --- /dev/null +++ b/tests/test_search_roles.py @@ -0,0 +1,71 @@ +import random + +import pytest + +from inferrsi.model.search_roles import Snapshot, actor_gradient, choose, distribution, update + + +def test_actor_gradient_matches_finite_difference_with_exploration_and_kl(): + model = Snapshot((0.2, -0.1), (0.3, -0.2), (0.0, 0.0), "initial") + choice = choose( + model, + "policy", + ("a", "b"), + ((1.0, 0.0), (0.0, 1.0)), + rng=random.Random(2), + context="observed", + collect=True, + exploration=0.1, + ) + reference = (0.0, 0.0) + gradient = actor_gradient(model.policy, reference, choice, 2.0, 0.2, 0.1) + from math import log + + def objective(weights): + p, _ = distribution(weights, choice.features, exploration=0.1) + q, _ = distribution(reference, choice.features, exploration=0.1) + return 2 * p[choice.selected] / choice.probabilities[choice.selected] - 0.1 * sum( + a * log(a / b) for a, b in zip(p, q) + ) + + for i in range(2): + plus, minus = list(model.policy), list(model.policy) + plus[i] += 1e-6 + minus[i] -= 1e-6 + assert gradient[i] == pytest.approx((objective(plus) - objective(minus)) / 2e-6) + + +def test_actual_updates_keep_policy_scorer_independent_and_reject_false_behavior(): + model = Snapshot((0.0, 0.0), (0.0, 0.0), (0.0, 0.0), "initial") + choice = choose( + model, + "drafter", + ("a", "b"), + ((1.0, 0.0), (0.0, 1.0)), + rng=random.Random(1), + context="observed", + collect=True, + ) + trained = update(model, model, [(choice, 2.0)], [((1.0, 0.0), 4.0)], learning_rate=0.1) + assert trained.drafter != model.drafter + assert trained.scorer != model.scorer + assert trained.policy == model.policy + with pytest.raises(ValueError, match="Behavior"): + update(trained, model, [(choice, 2.0)], [], learning_rate=0.1) + with pytest.raises(ValueError, match="finite"): + update(model, model, [], [((1.0, 0.0), None)], learning_rate=0.1) + + +def test_clipped_actor_does_not_reinforce_outside_positive_ratio_bound(): + model = Snapshot((0.0, 0.0), (0.0, 0.0), (0.0, 0.0), "initial") + choice = choose( + model, + "policy", + ("a", "b"), + ((1.0, 0.0), (0.0, 1.0)), + rng=random.Random(1), + context="observed", + collect=True, + ) + weights = (10.0, -10.0) if choice.selected == 0 else (-10.0, 10.0) + assert actor_gradient(weights, model.policy, choice, 1.0, 0.2, 0.0) == (0.0, 0.0) diff --git a/tests/test_speculative_search.py b/tests/test_speculative_search.py index 997e95f5b..ad686fbf0 100644 --- a/tests/test_speculative_search.py +++ b/tests/test_speculative_search.py @@ -63,6 +63,14 @@ def test_objective_prediction_partial_results_and_failed_quality_cannot_win(): assert tree.verified_gain(state) == 0 +@pytest.mark.parametrize("direction,sign", [("maximize", 1), ("minimize", -1)]) +def test_finite_objectives_cannot_publish_an_infinite_gain(direction, sign): + state = start(configuration=contract(direction=direction), score=-sign * 1e308) + state = complete(add(state, "candidate"), "candidate", sign * 1e308) + with pytest.raises(ValueError, match="finite objective"): + tree.verified_gain(state) + + @pytest.mark.parametrize("direction", ["maximize", "minimize"]) @pytest.mark.parametrize("order", [("z", "a"), ("a", "z")]) def test_selection_is_independent_of_receipt_order_and_root_wins_ties(direction, order): diff --git a/tests/test_vats_runtime.py b/tests/test_vats_runtime.py new file mode 100644 index 000000000..bd3a793ff --- /dev/null +++ b/tests/test_vats_runtime.py @@ -0,0 +1,310 @@ +import json +import sys +import time +from dataclasses import asdict, replace + +import pytest + +from inferrsi.harness import search_tree as tree +from inferrsi.harness.artifact_primitives import digest +from inferrsi.harness.gvo import examples, train_group +from inferrsi.harness.run_store import RunStore +from inferrsi.harness.search_adapter import ArtifactAdapter, CommandEvaluator, Edit +from inferrsi.harness.search_campaign import collect_group, finalize, qualify_root +from inferrsi.harness.search_records import decode +from inferrsi.harness.vats import Search, Settings +from inferrsi.harness.work_control import WorkControl +from inferrsi.model.search_roles import Snapshot +from tests.fixtures.speculative_search import contract + + +@pytest.fixture +def runtime(tmp_path): + grader = tmp_path / "grader.py" + grader.write_text("""import json, pathlib, sys +value = int((pathlib.Path(sys.argv[1]) / "candidate.txt").read_text()) +check = sys.argv[2] +out = {"verdict": "PASS" if value > 0 else "FAIL"} +if value > 0 and check == "objective": + out["objective"] = value +pathlib.Path(sys.argv[3]).write_text(json.dumps(out)) +""") + source = tmp_path / "baseline" + source.mkdir() + (source / "candidate.txt").write_text("1") + evaluator = CommandEvaluator( + { + check: (sys.executable, str(grader), "{candidate}", check, "{result}") + for check in ("quality", "objective") + }, + [grader], + ) + adapter = ArtifactAdapter( + tmp_path / "artifacts", + ( + Edit("improve", "candidate.txt", "1", "3"), + Edit("break", "candidate.txt", "1", "-1"), + Edit("repair", "candidate.txt", "-1", "4"), + ), + {"quality": 3000, "objective": 3000}, + evaluator, + domain="cpu-tests", + ) + snapshot = Snapshot( + (1.0, 0.0, 0.0), + (-1.0, 3.0, 6.0, 0.0, 0.0, 0.0, 0.0, 0.0), + (0.0,) * 8, + "test-initial-checkpoint", + ) + settings = Settings(5, 1, 2, 2, 40, 50, 3000, 3000, 40000) + control = WorkControl(RunStore(tmp_path / "store", "search-test")) + control.create( + "campaign", + {"adapter": adapter.identity, "settings": asdict(settings)}, + {"searches": {}, "checkpoint": snapshot.document()}, + cap_ms=120000, + final_reserve_ms=12000, + deadline=time.time() + 120, + ) + root = adapter.import_root(source, snapshot=digest(snapshot.document()), context={}) + initial = qualify_root( + control, "campaign", adapter, root, contract(improver_snapshot=digest(snapshot.document())) + ) + return control, adapter, snapshot, settings, initial + + +def test_real_process_search_group_training_checkpoint_and_fresh_search(runtime): + control, adapter, snapshot, settings, root = runtime + records = collect_group( + control, "campaign", adapter, root, snapshot, settings, group_id="first", size=2, seed=12 + ) + assert all(record["status"] == "complete" for record in records) + assert all(tree.accept(decode(record["tree"])) for record in records) + assert any(len(record["tree"]["candidates"]) > 1 for record in records) + actors, values, group = examples(records) + assert actors and values and len(group["advantages"]) == 2 + trained = train_group( + control, "campaign", records, snapshot, snapshot, cap_ms=5000, learning_rate=0.01 + ) + assert trained.document() == control.read("campaign")["state"]["checkpoint"] + assert trained.scorer != snapshot.scorer + next_records = collect_group( + control, "campaign", adapter, root, trained, settings, group_id="next", size=2, seed=42 + ) + assert all(record["snapshot"] == digest(trained.document()) for record in next_records) + assert control.read("campaign")["spent_ms"] > 0 + + +def test_final_access_is_frozen_and_export_retry_does_not_execute(runtime): + control, adapter, snapshot, settings, root = runtime + records = collect_group( + control, "campaign", adapter, root, snapshot, settings, group_id="first", size=2, seed=4 + ) + selected = tree.accept(decode(records[0]["tree"])) + result = finalize( + control, + "campaign", + adapter, + selected, + holdout_scope="test-final", + accept_result=lambda results: all(r["verdict"] == "PASS" for r in results), + ) + before = len(list((adapter.root / "executions").iterdir())) + assert ( + finalize( + control, + "campaign", + adapter, + selected, + holdout_scope="test-final", + accept_result=lambda results: False, + ) + == result + ) + assert len(list((adapter.root / "executions").iterdir())) == before + assert control.read("campaign")["state"]["stable"]["candidate_id"] == selected.candidate_id + + +def test_resume_after_receipt_before_state_commit_never_executes_twice(runtime, monkeypatch): + control, adapter, snapshot, settings, root = runtime + search = Search(control, "campaign", root.contract.search_id, snapshot, settings, adapter) + search.start(root, seed=2, collect=False) + original = control.settle + failed = False + + def crash(campaign, operation, receipt, **kwargs): + nonlocal failed + if receipt.get("candidate") and not failed: + failed = True + raise RuntimeError("crash before commit") + return original(campaign, operation, receipt, **kwargs) + + monkeypatch.setattr(control, "settle", crash) + with pytest.raises(RuntimeError, match="crash"): + search.run() + assert search.read()[1]["pending"] is not None + monkeypatch.setattr(control, "settle", original) + before = len(list((adapter.root / "candidates").iterdir())) + search.recover() + assert search.read()[1]["pending"] is None + assert len(list((adapter.root / "candidates").iterdir())) == before + + +def test_gvo_rejects_shared_tree_leaves_forced_and_changed_start_conditions(runtime): + control, adapter, snapshot, settings, root = runtime + records = collect_group( + control, "campaign", adapter, root, snapshot, settings, group_id="group", size=2, seed=2 + ) + with pytest.raises(ValueError, match="distinct"): + examples((records[0], records[0])) + with pytest.raises(ValueError, match="Forced"): + examples((records[0] | {"forced": True}, records[1])) + with pytest.raises(ValueError, match="identical"): + examples((records[0] | {"settings": "other"}, records[1])) + + +def test_snapshot_change_on_resume_is_rejected(runtime): + control, adapter, snapshot, settings, root = runtime + Search(control, "campaign", "search", snapshot, settings, adapter).start(root, seed=0) + changed = replace(snapshot, policy=(2.0,) * 8) + with pytest.raises(ValueError, match="Frozen"): + Search(control, "campaign", "search", changed, settings, adapter).run() + + +def test_persisted_search_has_only_real_check_results(runtime): + control, adapter, snapshot, settings, root = runtime + records = collect_group( + control, "campaign", adapter, root, snapshot, settings, group_id="group", size=2, seed=5 + ) + serialized = json.dumps(control.read("campaign"), allow_nan=False) + assert serialized + for record in records: + state = decode(record["tree"]) + for node in tree.complete_candidates(state): + assert all( + r.verdict.value == "PASS" for r in tree.evidence(state, node.candidate_id).values() + ) + + +def test_paid_continuations_are_metered_matched_and_excluded_from_actors(runtime): + from inferrsi.harness.search_continuations import continue_from, paired_targets + from inferrsi.harness.vats import Action + + control, adapter, snapshot, settings, root = runtime + records = collect_group( + control, "campaign", adapter, root, snapshot, settings, group_id="natural", size=2, seed=12 + ) + source = Search(control, "campaign", "natural-0", snapshot, settings, adapter) + menu = [Action(**a) for a in records[0]["trace"][0]["menu"]] + accept = next(a for a in menu if a.kind == "accept") + draft = next(a for a in menu if a.kind == "draft") + before = control.read("campaign")["spent_ms"] + suffixes = [ + continue_from(source, 0, a.key, f"suffix-{i}", copy_cap_ms=3000, seed=91) + for i, a in enumerate((accept, draft)) + ] + assert control.read("campaign")["spent_ms"] > before + assert all(s["forced"] and s["status"] == "complete" for s in suffixes) + pair = paired_targets(suffixes, expense_weight=0.1, elapsed_weight=0) + assert len(pair) == 3 + assert any( + op["category"] == "continuation_copy" + for op in control.read("campaign")["operations"].values() + ) + with pytest.raises(ValueError, match="Forced"): + examples(suffixes) + trained = train_group( + control, + "campaign", + records, + snapshot, + snapshot, + cap_ms=5000, + learning_rate=0.01, + scorer_continuations=suffixes, + scorer_pairs=(suffixes,), + ) + assert trained.scorer != snapshot.scorer + + +def test_root_invalidation_is_persisted_and_cannot_return_an_invalid_root(runtime): + control, adapter, snapshot, settings, root = runtime + dependency = tree.Dependency("environment", 0) + nodes = tuple(replace(n, dependencies=(dependency,)) for n in root.candidates) + attempts = tuple(replace(a, dependencies=(dependency,)) for a in root.attempts) + by_id = {a.attempt_id: a for a in attempts} + receipts = tuple(replace(r, attempt=by_id[r.attempt.attempt_id]) for r in root.receipts) + initial = replace( + root, candidates=nodes, attempts=attempts, receipts=receipts, dependencies=(dependency,) + ) + search = Search(control, "campaign", "search", snapshot, settings, adapter) + search.start(initial, seed=1) + search.invalidate("environment") + assert search.read()[1]["selected"] is None + assert search.read()[1]["status"] == "blocked" + with pytest.raises(ValueError, match="root evidence"): + search.run() + + +def test_late_draft_after_root_receipt_invalidation_is_charged_without_admission( + runtime, monkeypatch +): + control, adapter, snapshot, settings, root = runtime + dependency = tree.Dependency("root-proof", 0) + attempts = tuple(replace(a, dependencies=(dependency,)) for a in root.attempts) + by_id = {a.attempt_id: a for a in attempts} + initial = replace( + root, + attempts=attempts, + dependencies=(dependency,), + receipts=tuple(replace(r, attempt=by_id[r.attempt.attempt_id]) for r in root.receipts), + ) + search = Search(control, "campaign", "search", snapshot, settings, adapter) + search.start(initial, seed=2, collect=False) + original = control.settle + injected = False + + def invalidate_first(campaign, operation, receipt, **kwargs): + nonlocal injected + if receipt.get("candidate") and not injected: + injected = True + search.invalidate("root-proof") + return original(campaign, operation, receipt, **kwargs) + + monkeypatch.setattr(control, "settle", invalidate_first) + with pytest.raises(ValueError, match="root evidence"): + search.run() + assert injected + doc, record, state = search.read() + assert record["pending"] is None and record["selected"] is None + assert len(state.candidates) == 1 + assert all(op["status"] == "settled" for op in doc["operations"].values()) + assert record["rejected_drafts"] and record["spent_ms"] > 0 + + +def test_training_rejects_unretained_search_labels(runtime): + control, adapter, snapshot, settings, root = runtime + records = collect_group( + control, "campaign", adapter, root, snapshot, settings, group_id="natural", size=2, seed=12 + ) + altered = (records[0] | {"gain": 999}, records[1]) + with pytest.raises(ValueError, match="retained"): + train_group( + control, "campaign", altered, snapshot, snapshot, cap_ms=5000, learning_rate=0.01 + ) + + +@pytest.mark.parametrize("mode", ["reset", "carry"]) +def test_outer_loop_resume_reuses_trained_checkpoints_and_frozen_roots(runtime, mode): + from inferrsi.harness.search_campaign import learn + + control, adapter, snapshot, settings, root = runtime + settings = replace(settings, search_ms=10000, max_steps=3) + args = dict(rounds=2, group_size=2, training_cap_ms=5000, learning_rate=0.01, seed=8, mode=mode) + trained = learn(control, "campaign", adapter, root, snapshot, settings, **args) + before = control.read("campaign") + resumed = learn(control, "campaign", adapter, root, snapshot, settings, **args) + assert resumed == trained + assert control.read("campaign") == before + with pytest.raises(ValueError, match="starting conditions"): + learn(control, "campaign", adapter, root, snapshot, settings, **(args | {"seed": 9})) diff --git a/tests/test_work_control.py b/tests/test_work_control.py new file mode 100644 index 000000000..4e7684230 --- /dev/null +++ b/tests/test_work_control.py @@ -0,0 +1,152 @@ +import time +from concurrent.futures import ThreadPoolExecutor + +import pytest + +from inferrsi.harness.run_store import RunStore +from inferrsi.harness.work_control import WorkControl + + +@pytest.fixture +def control(tmp_path): + result = WorkControl(RunStore(tmp_path / "store", "controls")) + result.create( + "campaign", + {"fixed": "contract"}, + {"incumbent": "root"}, + cap_ms=1000, + final_reserve_ms=100, + deadline=time.time() + 60, + ) + return result + + +def claim(control, op="work", **changes): + args = dict(category="verify", cap_ms=200, domains=("device",), binding={"candidate": "c"}) + return control.claim("campaign", control.read("campaign")["revision"], op, **(args | changes)) + + +def test_atomic_resource_and_budget_reservation_across_controllers(control): + other = WorkControl(control.store) + other.create("other", {}, {}, cap_ms=1000, final_reserve_ms=100, deadline=time.time() + 60) + with ThreadPoolExecutor(2) as pool: + + def reserve(name): + try: + control.claim( + name, 0, name, category="verify", cap_ms=200, domains=("device",), binding={} + ) + return True + except ValueError: + return False + + assert sum(pool.map(reserve, ("campaign", "other"))) == 1 + + +def test_unknown_cleanup_or_usage_retains_reservation_and_never_relaunches(control): + claim(control) + control.launch("campaign", "work") + control.settle( + "campaign", "work", {"result": "unknown"}, actual_ms=None, cleanup_verified=False + ) + assert control.available(control.read("campaign")) == 700 + with pytest.raises(ValueError, match="already claimed"): + claim(control) + with pytest.raises(ValueError, match="leased"): + claim(control, "another") + reopened = WorkControl(RunStore(control.store.root, control.store.namespace)) + assert reopened.read("campaign")["operations"]["work"]["status"] == "unknown" + + +def test_receipt_settlement_and_incumbent_are_atomic_and_idempotent(control): + claim(control) + control.launch("campaign", "work") + + def update(state): + return state | {"incumbent": "child"} + + result = control.settle( + "campaign", + "work", + {"proof": "bound"}, + actual_ms=50, + cleanup_verified=True, + transition=update, + ) + assert result["state"]["incumbent"] == "child" + control.settle("campaign", "work", {"proof": "bound"}, actual_ms=50, cleanup_verified=True) + assert control.read("campaign")["spent_ms"] == 50 + with pytest.raises(ValueError, match="Conflicting"): + control.settle("campaign", "work", {}, actual_ms=50, cleanup_verified=True) + claim(control, "next") + + +def test_expired_claim_cannot_start_and_does_not_charge_unstarted_work(control, monkeypatch): + claim(control) + monkeypatch.setattr("inferrsi.harness.work_control.time.time", lambda: 1e12) + with pytest.raises(ValueError, match="expired"): + control.launch("campaign", "work") + result = control.read("campaign") + assert result["operations"]["work"]["status"] == "settled" + assert result["spent_ms"] == 0 + + +def test_cas_and_failed_transition_roll_back_accounting(control): + before = control.read("campaign") + control.update("campaign", before["revision"], lambda state: state | {"x": 1}) + with pytest.raises(ValueError, match="Stale"): + control.update("campaign", before["revision"], lambda state: {}) + claim(control) + control.launch("campaign", "work") + + def fail(state): + raise ValueError("invalid receipt") + + with pytest.raises(ValueError, match="invalid receipt"): + control.settle("campaign", "work", {}, actual_ms=20, cleanup_verified=True, transition=fail) + assert control.read("campaign")["spent_ms"] == 0 + + +def test_holdout_is_once_across_campaigns_even_after_interruption(control): + before = control.read("campaign") + control.claim_final("campaign", before["revision"], "heldout-data-v1", {"candidate": "c"}) + control.create("second", {}, {}, cap_ms=1000, final_reserve_ms=100, deadline=time.time() + 60) + with pytest.raises(ValueError, match="consumed"): + control.claim_final("second", 0, "heldout-data-v1", {"candidate": "other"}) + with pytest.raises(ValueError, match="frozen"): + claim(control) + control.finish_final("campaign", {"candidate": "c"}, {"verdict": "FAIL"}) + control.finish_final("campaign", {"candidate": "c"}, {"verdict": "FAIL"}) + with pytest.raises(ValueError, match="immutable"): + control.finish_final("campaign", {"candidate": "c"}, {"verdict": "PASS"}) + + +def test_final_reserve_and_unknown_work_cannot_be_spent(control): + with pytest.raises(ValueError, match="budget"): + claim(control, cap_ms=901) + claim(control) + with pytest.raises(ValueError, match="unresolved"): + control.claim_final("campaign", control.read("campaign")["revision"], "holdout", {}) + + +def test_over_budget_final_is_accounted_but_never_promoted(control): + control.claim_final("campaign", 0, "holdout", {"candidate": "c"}) + claim(control, final=True, category="final") + control.launch("campaign", "work") + control.settle("campaign", "work", {"verdict": "PASS"}, actual_ms=201, cleanup_verified=True) + assert control.read("campaign")["spent_ms"] == 201 + assert control.read("campaign")["blocked"] + with pytest.raises(ValueError, match="cannot promote"): + control.finish_final( + "campaign", + {"candidate": "c"}, + {"verdict": "PASS"}, + transition=lambda state: state | {"incumbent": "c"}, + ) + + +def test_read_only_control_does_not_create_or_change_tables(control): + from inferrsi.harness.run_store import RunStoreReader + + reader = WorkControl(RunStoreReader(control.store.root, control.store.namespace)) + assert reader.read("campaign") == control.read("campaign") diff --git a/tools/speculative_skill.py b/tools/speculative_skill.py new file mode 100644 index 000000000..548d55737 --- /dev/null +++ b/tools/speculative_skill.py @@ -0,0 +1,165 @@ +"""Explicit bounded-provider Skill comparator for the speculative-search Python API. + +This checkout tool composes the existing comparison authority. It never selects +a model/client implicitly, and does not turn fixed-output fixtures into live runs. +""" + +import argparse +import copy +import json +import os +import sys +import time +from pathlib import Path + +from inferrsi.harness import candidates +from inferrsi.harness.artifact_primitives import digest, sha256 +from inferrsi.harness.atomic_json import atomic_write_json +from inferrsi.harness.optimization_session import runtime_environment +from inferrsi.harness.scenarios.external import execute_trial +from inferrsi.knowledge.knowledge_operations import KnowledgeReader +from tools import skill_eval_compare as compare +from tools.skill_eval_improve import materialize + + +class Comparator: + """Bind a repo, original baseline and two disjoint externally declared splits. + + config contains comparison (the existing comparator config, including client), + allowed_paths, and splits. Each split has path, sha256, scope and task_count. + Holdout bytes are only opened by run(split='holdout'), after the caller's + WorkControl final claim. The controller must use that declared scope. + """ + + def __init__(self, repo, config, baseline_source): + self.repo = Path(repo).resolve() + self.config = copy.deepcopy(config) + if set(config) != {"comparison", "allowed_paths", "splits"}: + raise ValueError("Unknown or missing Skill comparator configuration") + comparison = config["comparison"] + if "client" not in comparison or comparison["mode"] != "update": + raise ValueError("Skill search requires an explicit bounded provider update config") + if set(config["splits"]) != {"development", "holdout"}: + raise ValueError("Separate development and holdout descriptors are required") + dev, final = (config["splits"][key] for key in ("development", "holdout")) + if dev["scope"] == final["scope"] or dev["sha256"] == final["sha256"]: + raise ValueError("Development and holdout must have distinct declared identities") + for split in (dev, final): + if ( + set(split) != {"path", "sha256", "scope", "task_count"} + or type(split["task_count"]) is not int + or split["task_count"] < 1 + or not Path(split["path"]).is_absolute() + ): + raise ValueError("Split requires an absolute source and frozen identity/count") + self.baseline = comparison["snapshots"]["baseline"]["git_revision"] + root = Path(baseline_source) + library = compare.snapshot(self.repo, self.baseline, comparison["skill_root"], root) + bundle = KnowledgeReader(skill_root=library).read("checkout:" + comparison["skill"]) + self.original = dict(bundle.content) + if set(config["allowed_paths"]) - self.original.keys(): + raise ValueError("Allowed edits must belong to the original Skill bundle") + self.identity = digest( + { + "config": config, + "baseline": self.baseline, + "resources": {k: digest(v.hex()) for k, v in self.original.items()}, + "controllers": {name: sha256(compare.ROOT / name) for name in compare.CONTROLLERS}, + } + ) + self._config_digest = digest(self.config) + self._controllers = {name: sha256(compare.ROOT / name) for name in compare.CONTROLLERS} + self.holdout_scope = "skill-data:" + final["sha256"] + + def export_root(self, destination): + destination = Path(destination) + destination.mkdir(parents=True, exist_ok=False) + for name, value in self.original.items(): + path = destination / name + path.parent.mkdir(parents=True, exist_ok=True) + path.write_bytes(value) + return destination + + def run(self, candidate, output, deadline, *, split): + if digest(self.config) != self._config_digest or any( + sha256(compare.ROOT / name) != expected for name, expected in self._controllers.items() + ): + raise ValueError("Frozen Skill configuration or controller changed") + files = candidates.members(candidate) + if set(files) != self.original.keys(): + raise ValueError("Skill candidate changed the frozen bundle membership") + edits = { + name: path.read_text(encoding="utf-8") + for name, path in files.items() + if path.read_bytes() != self.original[name] + } + if set(edits) - set(self.config["allowed_paths"]): + raise ValueError("Skill candidate changed a protected resource") + comparison = copy.deepcopy(self.config["comparison"]) + revisions, _ = materialize( + self.repo, + self.baseline, + comparison["skill_root"], + comparison["skill"], + {"inv": edits} if edits else {}, + self.config["allowed_paths"], + ) + descriptor = self.config["splits"][split] + tasks = Path(descriptor["path"]) + if sha256(tasks) != descriptor["sha256"]: + raise ValueError("Frozen Skill split changed") + cases = json.loads(tasks.read_text(encoding="utf-8")) + if len(cases) * comparison["limits"]["repeats"] != descriptor["task_count"]: + raise ValueError("Skill trial count differs from the frozen split") + if any(case.get("split") != split for case in cases): + raise ValueError("Skill task belongs to another declared split") + output.mkdir(parents=True, exist_ok=False) + comparison.update( + cases="tasks.json", + snapshots={ + "baseline": {"git_revision": self.baseline}, + "candidate": {"git_revision": revisions.get("inv", self.baseline)}, + }, + ) + atomic_write_json(output / "tasks.json", cases) + atomic_write_json(output / "config.json", comparison) + prepared = output / "comparison" + authorization = compare.prepare(output / "config.json", self.repo, prepared) + environment = runtime_environment() + environment["PYTHONPATH"] = str(compare.ROOT) + os.pathsep + environment["PYTHONPATH"] + command = ( + sys.executable, + "-m", + "tools.speculative_skill", + "--output", + str(prepared), + "--authorization", + authorization, + ) + record = execute_trial( + command, + compare.ROOT, + output / "execution", + time.monotonic() + max(0, deadline - time.time()), + None, + environment=environment, + include_cleanup=True, + ) + if sha256(prepared / "plan/freeze.json") != authorization: + raise ValueError("Skill comparison plan changed during execution") + report = compare.read_report(prepared) + atomic_write_json(output / "report.json", report) + return report, record["cleanup_verified"] + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--output", type=Path, required=True) + parser.add_argument("--authorization", required=True) + args = parser.parse_args() + compare.execute(args.output, args.authorization, require_request_authorization=True) + compare.report(args.output) + + +if __name__ == "__main__": + main() From fffb3fccfe76d379cc46f7677e53fbebc9b85e01 Mon Sep 17 00:00:00 2001 From: amy-why-3459 Date: Wed, 18 Nov 2026 21:15:53 +0800 Subject: [PATCH 03/16] harness: retain evaluator identity across search execution Signed-off-by: amy-why-3459 --- src/inferrsi/apps/search_benchmarks.py | 6 ++++++ tests/test_search_benchmarks.py | 21 +++++++++++++++++++++ tools/speculative_skill.py | 11 +++++++++-- 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/inferrsi/apps/search_benchmarks.py b/src/inferrsi/apps/search_benchmarks.py index eb7a11720..95b779313 100644 --- a/src/inferrsi/apps/search_benchmarks.py +++ b/src/inferrsi/apps/search_benchmarks.py @@ -73,6 +73,12 @@ def evaluate(self, candidate, check, output, deadline): environment=environment, include_cleanup=True, ) + if { + p.relative_to(self.frozen).as_posix(): sha256(p) + for p in self.frozen.rglob("*") + if p.is_file() + } != self.plan["frozen_files"]: + raise ValueError("Frozen benchmark resources changed during execution") assessed = protocol.assess(output, self.plan["evaluation_settings"], execution) if execution["cleanup_verified"] is not True: assessed = {"verdict": "INCONCLUSIVE", "score": None} diff --git a/tests/test_search_benchmarks.py b/tests/test_search_benchmarks.py index 5cc1ba6b3..b2045f5db 100644 --- a/tests/test_search_benchmarks.py +++ b/tests/test_search_benchmarks.py @@ -78,6 +78,25 @@ def test_cli_requires_explicit_shared_store_and_read_commands_do_not_create_one( parser().parse_args(["search", "engine", "--session", str(tmp_path / "run")]) +def test_engine_rejects_frozen_resource_mutation_during_evaluation(tmp_path, monkeypatch): + evaluator = EngineEvaluator( + "qwen21-denoise-npu", + tmp_path / "frozen", + arguments=["--pairs", "1", "--samples", "1", "--warmups", "0"], + ) + source = tmp_path / "candidate" + source.mkdir() + (source / "candidate.py").write_text("unchanged candidate") + + def mutated_execution(*args, **kwargs): + (evaluator.frozen / "reference.py").write_text("changed reference") + return {"status": "completed", "returncode": 0, "cleanup_verified": True} + + monkeypatch.setattr("inferrsi.apps.search_benchmarks.execute_trial", mutated_execution) + with pytest.raises(ValueError, match="resources changed during"): + evaluator.evaluate(source, "evaluation", tmp_path / "output", time.time() + 1) + + def test_skill_comparator_preparation_does_not_access_holdout(tmp_path): from tests.fixtures.harness_improvement_demo import configuration from tools.speculative_skill import Comparator @@ -89,6 +108,7 @@ def test_skill_comparator_preparation_does_not_access_holdout(tmp_path): client={"explicit": "preparation-only-test"}, snapshots={"baseline": {"git_revision": config["baseline"]}}, ) + def descriptor(split): return { "path": str(tmp_path / (split + ".json")), @@ -96,6 +116,7 @@ def descriptor(split): "scope": split, "task_count": 3, } + bound = Comparator( repo, { diff --git a/tools/speculative_skill.py b/tools/speculative_skill.py index 548d55737..09cb18ed1 100644 --- a/tools/speculative_skill.py +++ b/tools/speculative_skill.py @@ -59,16 +59,23 @@ def __init__(self, repo, config, baseline_source): self.original = dict(bundle.content) if set(config["allowed_paths"]) - self.original.keys(): raise ValueError("Allowed edits must belong to the original Skill bundle") + self._controllers = { + name: sha256(compare.ROOT / name) + for name in ( + *compare.CONTROLLERS, + "tools/speculative_skill.py", + "tools/skill_eval_improve.py", + ) + } self.identity = digest( { "config": config, "baseline": self.baseline, "resources": {k: digest(v.hex()) for k, v in self.original.items()}, - "controllers": {name: sha256(compare.ROOT / name) for name in compare.CONTROLLERS}, + "controllers": self._controllers, } ) self._config_digest = digest(self.config) - self._controllers = {name: sha256(compare.ROOT / name) for name in compare.CONTROLLERS} self.holdout_scope = "skill-data:" + final["sha256"] def export_root(self, destination): From db0de8c5dc6e5ea513eef0ff8c1f35d2899dd023 Mon Sep 17 00:00:00 2001 From: amy-why-3459 Date: Wed, 18 Nov 2026 21:21:19 +0800 Subject: [PATCH 04/16] harness: bind GVO groups to identical initial observations Signed-off-by: amy-why-3459 --- docs/speculative-search.md | 5 +++-- src/inferrsi/harness/gvo.py | 5 ++++- src/inferrsi/harness/vats.py | 19 +++++++++++++++++++ tests/test_vats_runtime.py | 2 ++ 4 files changed, 28 insertions(+), 3 deletions(-) diff --git a/docs/speculative-search.md b/docs/speculative-search.md index d7639bb55..b99e2c977 100644 --- a/docs/speculative-search.md +++ b/docs/speculative-search.md @@ -148,8 +148,9 @@ External process-liveness reconciliation needs a qualified authority. `finalize` consumes a unique scope in the shared store before final data access, freezes selection and closes search/training. Engine compares again with the -original baseline. For Skill, supply a **holdout-bound adapter** and the declared -holdout scope, never the development adapter. Exact bounded radix utility keeps +original baseline. For Skill, supply a **holdout-bound adapter** and +`Comparator.holdout_scope` (derived from the frozen data digest), never the +development adapter or a replacement label. Exact bounded radix utility keeps quality before token use; reduced tokens cannot offset a quality regression. After interruption, final consumes existing receipts only. Even an unstarted diff --git a/src/inferrsi/harness/gvo.py b/src/inferrsi/harness/gvo.py index 10e950960..f485040d3 100644 --- a/src/inferrsi/harness/gvo.py +++ b/src/inferrsi/harness/gvo.py @@ -39,6 +39,7 @@ def examples(records, *, expense_weight=0.0, elapsed_weight=0.0): set(), ) returns = [] + initial_conditions = set() for record in records: if record["status"] != "complete" or record["pending"] is not None: raise ValueError("Incomplete searches cannot supply GVO returns") @@ -53,6 +54,7 @@ def examples(records, *, expense_weight=0.0, elapsed_weight=0.0): snapshots.add(record["snapshot"]) settings.add(record["settings"]) adapters.add(record["adapter"]) + initial_conditions.add(record["initial_conditions"]) returns.append( finite( record["gain"] @@ -62,7 +64,8 @@ def examples(records, *, expense_weight=0.0, elapsed_weight=0.0): ) ) if len(set(identities)) != len(identities) or any( - len(values) != 1 for values in (roots, contracts, snapshots, settings, adapters) + len(values) != 1 + for values in (roots, contracts, snapshots, settings, adapters, initial_conditions) ): raise ValueError("Search group must have distinct IDs and identical starting conditions") if any(record["forced"] for record in records): diff --git a/src/inferrsi/harness/vats.py b/src/inferrsi/harness/vats.py index bf79022fb..6ed33bf5b 100644 --- a/src/inferrsi/harness/vats.py +++ b/src/inferrsi/harness/vats.py @@ -235,6 +235,24 @@ def start(self, initial, *, seed, collect=True, forced=False): tree.accept(initial) if not forced and len(initial.candidates) != 1: raise ValueError("Independent natural searches must start from one qualified root") + + def observation(attempt): + return { + key: value + for key, value in asdict(attempt).items() + if key not in ("attempt_id", "search_id") + } + + initial_conditions = digest( + { + "candidates": [asdict(node) for node in initial.candidates], + "dependencies": [asdict(item) for item in initial.dependencies], + "attempts": [observation(a) for a in initial.attempts], + "receipts": [ + {**asdict(r), "attempt": observation(r.attempt)} for r in initial.receipts + ], + } + ) doc = self.control.read(self.campaign) def transition(state): @@ -257,6 +275,7 @@ def transition(state): "draft_attempts": 0, "active_action": None, "initial_gain": tree.verified_gain(initial), + "initial_conditions": initial_conditions, "selected": initial.root_id, } return state diff --git a/tests/test_vats_runtime.py b/tests/test_vats_runtime.py index bd3a793ff..1873789ab 100644 --- a/tests/test_vats_runtime.py +++ b/tests/test_vats_runtime.py @@ -161,6 +161,8 @@ def test_gvo_rejects_shared_tree_leaves_forced_and_changed_start_conditions(runt examples((records[0] | {"forced": True}, records[1])) with pytest.raises(ValueError, match="identical"): examples((records[0] | {"settings": "other"}, records[1])) + with pytest.raises(ValueError, match="identical"): + examples((records[0] | {"initial_conditions": "different-root-proof"}, records[1])) def test_snapshot_change_on_resume_is_rejected(runtime): From 79d4c6de4e6629d128daaaa70785a50a24bb0f5c Mon Sep 17 00:00:00 2001 From: amy-why-3459 Date: Wed, 18 Nov 2026 21:25:13 +0800 Subject: [PATCH 05/16] harness: inject search role operations across the model boundary Signed-off-by: amy-why-3459 --- docs/speculative-search.md | 3 +++ src/inferrsi/harness/gvo.py | 25 +++++++----------------- src/inferrsi/harness/search_adapter.py | 4 +--- src/inferrsi/harness/search_campaign.py | 3 +-- src/inferrsi/harness/vats.py | 9 +++------ src/inferrsi/model/search_roles.py | 26 +++++++++++++++++++++++++ 6 files changed, 41 insertions(+), 29 deletions(-) diff --git a/docs/speculative-search.md b/docs/speculative-search.md index b99e2c977..3c3e792bb 100644 --- a/docs/speculative-search.md +++ b/docs/speculative-search.md @@ -123,6 +123,9 @@ Caller-authored unreceipted pair targets are rejected. These are real updates to a constrained CPU operator model. They are not LLM finetuning, prompt summarization or evidence of pretrained-model improvement. +Apps injects the role snapshot's `choose`, `predict`, `train`, `restore` and +`document` operations. Harness retains traces and controls when they may run; +it has no import dependency on Model. ## Accounting, recovery and final diff --git a/src/inferrsi/harness/gvo.py b/src/inferrsi/harness/gvo.py index f485040d3..71490cc0f 100644 --- a/src/inferrsi/harness/gvo.py +++ b/src/inferrsi/harness/gvo.py @@ -5,23 +5,14 @@ import uuid from dataclasses import replace -from inferrsi.model.search_roles import Choice, finite, update - from .artifact_primitives import digest from .search_records import decode -def choice_from_document(raw): - return Choice( - **( - raw - | { - "options": tuple(raw["options"]), - "features": tuple(tuple(row) for row in raw["features"]), - "probabilities": tuple(raw["probabilities"]), - } - ) - ) +def finite(value): + if type(value) not in (int, float) or not math.isfinite(value): + raise ValueError("Search returns require finite recorded observations") + return value def examples(records, *, expense_weight=0.0, elapsed_weight=0.0): @@ -85,9 +76,8 @@ def examples(records, *, expense_weight=0.0, elapsed_weight=0.0): if step["forced"]: raise ValueError("Forced action leaked into a natural trajectory") for raw in (step["choice"], *step["draft_choices"]): - choice = choice_from_document(raw) - if choice.sampled: - actors.append((choice, advantage)) + if raw["sampled"]: + actors.append((raw, advantage)) return actors, values, {"search_ids": identities, "returns": returns, "advantages": advantages} @@ -183,8 +173,7 @@ def train_group( values.extend( suffix_examples(record, expense_weight=expense_weight, elapsed_weight=elapsed_weight) ) - trained = update( - snapshot, + trained = snapshot.train( reference, actors, values, diff --git a/src/inferrsi/harness/search_adapter.py b/src/inferrsi/harness/search_adapter.py index 4896f8a07..8375337fe 100644 --- a/src/inferrsi/harness/search_adapter.py +++ b/src/inferrsi/harness/search_adapter.py @@ -12,7 +12,6 @@ from pathlib import Path from inferrsi.contracts.claim import Verdict -from inferrsi.model.search_roles import choose from . import candidates from . import search_tree as tree @@ -131,8 +130,7 @@ def draft(self, parent, state, snapshot, rng, operation, deadline): vectors = [ tuple(float(i == index) for i in range(len(self.edits))) for index, _ in legal ] - choice = choose( - snapshot, + choice = snapshot.choose( "drafter", [edit.token for _, edit in legal], vectors, diff --git a/src/inferrsi/harness/search_campaign.py b/src/inferrsi/harness/search_campaign.py index fea8bce99..96897c510 100644 --- a/src/inferrsi/harness/search_campaign.py +++ b/src/inferrsi/harness/search_campaign.py @@ -4,7 +4,6 @@ from dataclasses import asdict, replace from inferrsi.contracts.claim import Verdict -from inferrsi.model.search_roles import Snapshot from . import search_tree as tree from .artifact_primitives import digest @@ -215,7 +214,7 @@ def learn( or digest(op["binding"]["training"]) != digest(expected) ): raise ValueError("Frozen outer-loop training configuration changed") - snapshot = Snapshot(**op["receipt"]["checkpoint"]) + snapshot = snapshot.restore(op["receipt"]["checkpoint"]) else: snapshot = train_group( control, diff --git a/src/inferrsi/harness/vats.py b/src/inferrsi/harness/vats.py index 6ed33bf5b..914ca4159 100644 --- a/src/inferrsi/harness/vats.py +++ b/src/inferrsi/harness/vats.py @@ -12,8 +12,6 @@ from dataclasses import asdict, dataclass, replace from itertools import combinations -from inferrsi.model.search_roles import choose, dot - from . import search_tree as tree from .artifact_primitives import digest from .search_records import decode, encode @@ -140,8 +138,8 @@ def menu(state, jobs, available, settings, snapshot): continue trial = allowed[frozenset(union)] queries += 2 - if dot(snapshot.scorer, features(state, trial, available, settings)) > dot( - snapshot.scorer, features(state, left, available, settings) + if snapshot.predict(features(state, trial, available, settings)) > snapshot.predict( + features(state, left, available, settings) ): extra.append(trial) if not settings.bundled: @@ -526,8 +524,7 @@ def run(self, *, forced_action=None): features(current, action, remaining, self.settings) for action in actions ) rng = random.Random(f"{search['seed']}:policy:{len(search['trace'])}") - choice = choose( - self.snapshot, + choice = self.snapshot.choose( "policy", [action.key for action in actions], vectors, diff --git a/src/inferrsi/model/search_roles.py b/src/inferrsi/model/search_roles.py index 3e5df28cf..e0a8e9501 100644 --- a/src/inferrsi/model/search_roles.py +++ b/src/inferrsi/model/search_roles.py @@ -61,6 +61,32 @@ def document(self): "scorer": list(self.scorer), } + def choose(self, *args, **kwargs): + """Injected role operation; Harness does not import this Model module.""" + return choose(self, *args, **kwargs) + + def predict(self, features): + return dot(self.scorer, features) + + def restore(self, document): + return type(self)(**document) + + def train(self, reference, actors, values, pairs, **settings): + restored = [] + for raw, advantage in actors: + choice = Choice( + **( + raw + | { + "options": tuple(raw["options"]), + "features": tuple(tuple(row) for row in raw["features"]), + "probabilities": tuple(raw["probabilities"]), + } + ) + ) + restored.append((choice, advantage)) + return update(self, reference, restored, values, pairs, **settings) + @dataclass(frozen=True) class Choice: From 94ba88d4fae955f23b9ab6ceca3a56224d8eddd6 Mon Sep 17 00:00:00 2001 From: amy-why-3459 Date: Wed, 18 Nov 2026 21:42:11 +0800 Subject: [PATCH 06/16] apps: isolate benchmark loading and use bounded protocol processes Signed-off-by: amy-why-3459 --- src/inferrsi/apps/search_benchmarks.py | 21 +++++++++++++++++---- src/inferrsi/apps/speculative_search.py | 4 ++-- src/inferrsi/harness/task_protocol.py | 18 ++++++++++++++---- tests/harness/test_task_protocol_child.py | 16 ++++++++++++++++ 4 files changed, 49 insertions(+), 10 deletions(-) diff --git a/src/inferrsi/apps/search_benchmarks.py b/src/inferrsi/apps/search_benchmarks.py index 95b779313..b89442d19 100644 --- a/src/inferrsi/apps/search_benchmarks.py +++ b/src/inferrsi/apps/search_benchmarks.py @@ -49,9 +49,14 @@ def evaluate(self, candidate, check, output, deadline): raise ValueError("Frozen benchmark resources changed") output.mkdir(parents=True) candidates.retain(candidate / "candidate.py", output / "candidate.py") - protocol = task_protocol.protocol(self.frozen) - args = protocol.arguments( - output / "candidate.py", output / "measurement", self.plan["evaluation_settings"] + args = task_protocol.run( + self.frozen, + self.plan["frozen_files"]["optimization.py"], + "arguments", + deadline=time.monotonic() + max(0, deadline - time.time()), + candidate=output / "candidate.py", + output=output / "measurement", + settings=self.plan["evaluation_settings"], ) from inferrsi.harness import optimization_session @@ -79,7 +84,15 @@ def evaluate(self, candidate, check, output, deadline): if p.is_file() } != self.plan["frozen_files"]: raise ValueError("Frozen benchmark resources changed during execution") - assessed = protocol.assess(output, self.plan["evaluation_settings"], execution) + assessed = task_protocol.run( + self.frozen, + self.plan["frozen_files"]["optimization.py"], + "assess", + deadline=time.monotonic() + max(0, deadline - time.time()), + directory=output, + settings=self.plan["evaluation_settings"], + execution=execution, + ) if execution["cleanup_verified"] is not True: assessed = {"verdict": "INCONCLUSIVE", "score": None} atomic_write_json(output / "assessment.json", assessed) diff --git a/src/inferrsi/apps/speculative_search.py b/src/inferrsi/apps/speculative_search.py index 318939f00..01deb8b3f 100644 --- a/src/inferrsi/apps/speculative_search.py +++ b/src/inferrsi/apps/speculative_search.py @@ -17,8 +17,6 @@ from inferrsi.harness.work_control import WorkControl from inferrsi.model.search_roles import Snapshot -from .search_benchmarks import EngineEvaluator - def add_arguments(commands): search = commands.add_parser("search", help="Bounded SpecRSI search and role training") @@ -80,6 +78,8 @@ def command(args): if args.search_action == "export": atomic_write_json(args.output, result) return result + from .search_benchmarks import EngineEvaluator + if root.exists(): raise ValueError("Use a new session directory; status/export never restart work") if args.groups < 2 or args.rounds < 1: diff --git a/src/inferrsi/harness/task_protocol.py b/src/inferrsi/harness/task_protocol.py index 340f12181..792b5564e 100644 --- a/src/inferrsi/harness/task_protocol.py +++ b/src/inferrsi/harness/task_protocol.py @@ -7,6 +7,7 @@ Windows Job, and returns one strict JSON envelope (#412 R3). """ +import math import os import shutil import sys @@ -85,15 +86,23 @@ def _response_problem(response, operation: str) -> str | None: return None -def run(root: Path, protocol_sha256: str, operation: str, **arguments): +def run(root: Path, protocol_sha256: str, operation: str, *, deadline=None, **arguments): """Run one operation of ``root/optimization.py`` whose bytes have ``protocol_sha256``. Path arguments must be absolute. The child's stderr tail is copied to this process's stderr after every operation. A protocol ``SystemExit`` is raised again with its exit code; every other failure raises ``TaskProtocolError``. Descendants that the - protocol leaves running are stopped with the child. + protocol leaves running are stopped with the child. An optional absolute + monotonic deadline further bounds the shared protocol timeout. """ path = Path(root).resolve() / "optimization.py" + timeout = PROTOCOL_TIMEOUT_SECONDS + if deadline is not None: + if type(deadline) not in (int, float) or not math.isfinite(deadline): + raise TaskProtocolError("request", "deadline must be finite monotonic time") + timeout = min(timeout, deadline - time.monotonic()) + if timeout <= 0: + raise TaskProtocolError("timeout", "deadline expired before protocol execution") if sha256(path) != protocol_sha256: raise TaskProtocolError("identity", "protocol bytes differ from the frozen digest") request = {"schema_version": protocol_child.SCHEMA_VERSION, "operation": operation, @@ -113,7 +122,8 @@ def run(root: Path, protocol_sha256: str, operation: str, **arguments): # cannot land among the frozen resources. record = local_process._supervise( argv, work, dict(os.environ), work / "stdout", work / "stderr", - time.monotonic() + PROTOCOL_TIMEOUT_SECONDS) + min(time.monotonic() + timeout, deadline) if deadline is not None + else time.monotonic() + timeout) stderr = _tail(work / "stderr") if (work / "stderr").is_file() else "" # Diagnostics contract: after every operation, whatever its result, the last # STDERR_TAIL_BYTES of the child's stderr (protocol prints included) are copied to @@ -125,7 +135,7 @@ def run(root: Path, protocol_sha256: str, operation: str, **arguments): raise KeyboardInterrupt if record["status"] == "timeout": raise TaskProtocolError( - "timeout", f"{operation} exceeded {PROTOCOL_TIMEOUT_SECONDS:g} s; {stderr}") + "timeout", f"{operation} exceeded {timeout:g} s; {stderr}") if record["status"] not in ("completed", "failed"): raise TaskProtocolError("launch", f"{record['status']}: {record['error']}") if record["returncode"] != 0: diff --git a/tests/harness/test_task_protocol_child.py b/tests/harness/test_task_protocol_child.py index 0446015c8..973d11910 100644 --- a/tests/harness/test_task_protocol_child.py +++ b/tests/harness/test_task_protocol_child.py @@ -219,6 +219,22 @@ def test_deadline_stops_the_child_and_its_descendants(tmp_path, monkeypatch, cap assert not marker.exists() +def test_campaign_deadline_bounds_the_protocol_timeout(tmp_path): + digest = write_protocol( + tmp_path, "import time\ndef validate_arguments(a):\n time.sleep(60)\n") + started = time.monotonic() + error = failure(tmp_path, digest, deadline=started + 1.0, arguments=[]) + assert error.kind == "timeout" + assert time.monotonic() - started < 10 + + +def test_expired_campaign_deadline_never_starts_protocol(tmp_path, monkeypatch): + digest = write_protocol(tmp_path, "def validate_arguments(a):\n return {}\n") + monkeypatch.setattr(task_protocol.local_process, "_supervise", + lambda *a, **k: pytest.fail("expired protocol must not start")) + assert failure(tmp_path, digest, deadline=0, arguments=[]).kind == "timeout" + + @pytest.mark.parametrize("body, raises", [ (" return {}\n", False), (" raise ValueError('rejected')\n", True), From b8432339dcec25822924ab4573ef9d76d96bae89 Mon Sep 17 00:00:00 2001 From: amy-why-3459 Date: Wed, 18 Nov 2026 22:02:39 +0800 Subject: [PATCH 07/16] harness: reconcile unstarted search work and retain known overruns Recover conclusively unstarted work without adapter execution, keep unknown launches blocked, and reserve known usage until cleanup is reconciled. Reject legacy searches without retained initial evidence and cover the reviewed planner, role, identity and deadline boundaries. Agent-assisted-by: Codex (implementation) Signed-off-by: amy-why-3459 --- docs/speculative-search.md | 18 ++- src/inferrsi/harness/gvo.py | 8 +- src/inferrsi/harness/search_records.py | 8 ++ src/inferrsi/harness/vats.py | 41 ++++++- src/inferrsi/harness/work_control.py | 22 +++- tests/harness/test_task_protocol_child.py | 8 ++ tests/test_search_benchmarks.py | 35 ++++++ tests/test_search_roles.py | 19 +++ tests/test_vats_runtime.py | 135 ++++++++++++++++++++++ tests/test_verification_plans.py | 14 +++ tests/test_work_control.py | 33 ++++++ 11 files changed, 329 insertions(+), 12 deletions(-) diff --git a/docs/speculative-search.md b/docs/speculative-search.md index 3c3e792bb..bf7020c8b 100644 --- a/docs/speculative-search.md +++ b/docs/speculative-search.md @@ -75,6 +75,12 @@ Optional refinement adds unions without removing base completions. An undersized menu cap fails explicitly. Collection samples Policy; deployment uses argmax. Drafter edits remain sampled from the supplied permitted vocabulary. +Experimental records predating `initial_conditions` remain readable/exportable +through WorkControl, but cannot resume or enter GVO training. Start fresh searches +from qualified evidence; there is no migration that guesses a historical starting +tree from a later tree. Receiving search/attempt IDs may differ after an explicit +root import; actual execution identities, evidence and dependency epochs must agree. + The chosen work block is persisted before execution. Each edit waits for its parent artifact, without waiting for parent quality. Each check binds candidate, contract, attempt, execution, reservation and domain. Settlement commits elapsed @@ -138,7 +144,10 @@ use the **same shared store**. Launch intent is durable before dispatch; an ambiguous crash never authorizes a second launch. Known elapsed time and cleanup release unused reservation. Unknown usage/cleanup retains the entire reservation and domain. Over-cap work -is charged and blocks promotion. Absolute deadlines include queueing and cleanup. +blocks further claims immediately, even with unknown cleanup; the retained hold +is at least the largest known usage. Later reconciliation releases the domain +and charges the final usage once, never below that known lower bound. Absolute +deadlines include queueing and cleanup. In-process bounded planning, editing and training are checked after return; they are not forcibly preempted like subprocesses. Preflight freezing and initial setup are outside this campaign meter, a remaining lifecycle-cost limitation. @@ -149,6 +158,13 @@ checks retained candidate bytes and never launches new work. Lost preparation, training or copy receipts remain unresolved; no guessed-cost retry is available. External process-liveness reconciliation needs a qualified authority. +An expired launch recorded as `settled/not_started` is conclusive control evidence +that dispatch never occurred. Search recovery clears its pending work and ends +the current block; a registered verification attempt gets a control-only +`INCONCLUSIVE` receipt, with no objective or evaluator PASS. No execution is +charged. A search whose limit has expired can then return its qualified incumbent. +This exception does not apply to ambiguous launch-intent crashes. + `finalize` consumes a unique scope in the shared store before final data access, freezes selection and closes search/training. Engine compares again with the original baseline. For Skill, supply a **holdout-bound adapter** and diff --git a/src/inferrsi/harness/gvo.py b/src/inferrsi/harness/gvo.py index 71490cc0f..1fa865037 100644 --- a/src/inferrsi/harness/gvo.py +++ b/src/inferrsi/harness/gvo.py @@ -6,7 +6,7 @@ from dataclasses import replace from .artifact_primitives import digest -from .search_records import decode +from .search_records import decode, initial_conditions def finite(value): @@ -30,7 +30,7 @@ def examples(records, *, expense_weight=0.0, elapsed_weight=0.0): set(), ) returns = [] - initial_conditions = set() + starts = set() for record in records: if record["status"] != "complete" or record["pending"] is not None: raise ValueError("Incomplete searches cannot supply GVO returns") @@ -45,7 +45,7 @@ def examples(records, *, expense_weight=0.0, elapsed_weight=0.0): snapshots.add(record["snapshot"]) settings.add(record["settings"]) adapters.add(record["adapter"]) - initial_conditions.add(record["initial_conditions"]) + starts.add(initial_conditions(record)) returns.append( finite( record["gain"] @@ -56,7 +56,7 @@ def examples(records, *, expense_weight=0.0, elapsed_weight=0.0): ) if len(set(identities)) != len(identities) or any( len(values) != 1 - for values in (roots, contracts, snapshots, settings, adapters, initial_conditions) + for values in (roots, contracts, snapshots, settings, adapters, starts) ): raise ValueError("Search group must have distinct IDs and identical starting conditions") if any(record["forced"] for record in records): diff --git a/src/inferrsi/harness/search_records.py b/src/inferrsi/harness/search_records.py index 8377a3587..343ad49d9 100644 --- a/src/inferrsi/harness/search_records.py +++ b/src/inferrsi/harness/search_records.py @@ -11,6 +11,14 @@ def encode(state): return asdict(state) +def initial_conditions(record): + """Reject legacy records whose actual starting evidence was never retained.""" + value = record.get("initial_conditions") + if not isinstance(value, str) or not value: + raise ValueError("Legacy search lacks initial evidence identity; start a fresh search") + return value + + def decode(raw): def dependencies(values): return tuple(tree.Dependency(**value) for value in values) diff --git a/src/inferrsi/harness/vats.py b/src/inferrsi/harness/vats.py index 914ca4159..ab8b67ac1 100644 --- a/src/inferrsi/harness/vats.py +++ b/src/inferrsi/harness/vats.py @@ -14,7 +14,7 @@ from . import search_tree as tree from .artifact_primitives import digest -from .search_records import decode, encode +from .search_records import decode, encode, initial_conditions from .verification_plans import completion_menu @@ -216,6 +216,7 @@ def __init__(self, control, campaign, search_id, snapshot, settings, adapter): def read(self): document = self.control.read(self.campaign) search = document["state"]["searches"][self.search_id] + initial_conditions(search) if ( search["snapshot"] != self.snapshot_id or search["settings"] != self.settings_id @@ -371,11 +372,45 @@ def transition(state): self.control.update(self.campaign, doc["revision"], transition) def recover(self): - _, search, _ = self.read() + doc, search, _ = self.read() pending = search["pending"] if pending is None: return - # Receipt-only recovery. Never call draft/verify from this path. + operation = doc["operations"][pending["operation"]] + if operation["status"] == "settled" and operation["receipt"] == {"not_started": "deadline"}: + # launch() conclusively refused dispatch. There can be no adapter + # receipt: settle the Attempt as control-only INCONCLUSIVE, and + # abandon the remaining block without inventing check evidence. + def not_started(state): + from inferrsi.contracts.claim import Verdict + + record = state["searches"][self.search_id] + current = decode(record["tree"]) + if pending["kind"] == "verify": + bound = next( + a for a in current.attempts if a.attempt_id == pending["operation"] + ) + current = tree.record_receipt( + current, + tree.Receipt(bound, Verdict.INCONCLUSIVE, digest(operation)), + expected_revision=current.revision, + ) + record.update( + tree=encode(current), + pending=None, + active_action=None, + block_stop_reason="launch_deadline", + ) + try: + record["selected"] = tree.accept(current).candidate_id + except ValueError: + record.update(status="blocked", selected=None) + return state + + self.control.update(self.campaign, doc["revision"], not_started) + return + # All uncertain launches still require an adapter receipt. Never call + # draft/verify from recovery, including after launch-intent crashes. result = self.adapter.recover(pending["operation"], pending) if result is None: raise ValueError("Unknown execution: retain reservation and do not relaunch") diff --git a/src/inferrsi/harness/work_control.py b/src/inferrsi/harness/work_control.py index 3a0993c42..97ace875a 100644 --- a/src/inferrsi/harness/work_control.py +++ b/src/inferrsi/harness/work_control.py @@ -2,7 +2,8 @@ This is a controller for trusted host operations, not an executor or sandbox. All cooperating controllers must share the same RunStore for resource domains -and holdout scopes to be exclusive. Unknown work retains its entire reservation. +and holdout scopes to be exclusive. Unknown work retains at least its entire +reservation and any larger known usage lower bound. """ import json @@ -101,7 +102,11 @@ def read(self, campaign): @staticmethod def available(body, *, final=False): - held = sum(op["cap_ms"] for op in body["operations"].values() if op["status"] != "settled") + held = sum( + max(op["cap_ms"], op.get("known_ms", 0)) + for op in body["operations"].values() + if op["status"] != "settled" + ) return body["cap_ms"] - body["spent_ms"] - held - (0 if final else body["final_reserve_ms"]) def update(self, campaign, revision, transition): @@ -209,6 +214,16 @@ def settle(self, campaign, operation, receipt, *, actual_ms, cleanup_verified, t return if op["status"] not in ("launched", "unknown"): raise ValueError("Cannot settle an unlaunched operation") + known_ms = max(op.get("known_ms", 0), actual_ms or 0) + if cleanup_verified is True and actual_ms is not None and actual_ms < known_ms: + raise ValueError("Final usage cannot be below previously known usage") + op["known_ms"] = known_ms + executable_ms = min(op["cap_ms"], (op["deadline"] - op["started_at"]) * 1000) + # Cleanup uncertainty cannot erase an observed overrun. Keep the + # domain and conservative hold until reconciliation, blocking any + # further work immediately, including work on other domains. + if known_ms > executable_ms: + body["blocked"] = True op["settlement"] = settlement if actual_ms is None or cleanup_verified is not True: op["status"] = "unknown" @@ -218,8 +233,7 @@ def settle(self, campaign, operation, receipt, *, actual_ms, cleanup_verified, t else: op.update(status="settled", actual_ms=actual_ms, receipt=receipt) body["spent_ms"] += actual_ms - executable_ms = min(op["cap_ms"], (op["deadline"] - op["started_at"]) * 1000) - if actual_ms > executable_ms or body["spent_ms"] > body["cap_ms"]: + if body["spent_ms"] > body["cap_ms"]: body["blocked"] = True if transition is not None and not body["blocked"]: body["state"] = transition(body["state"]) diff --git a/tests/harness/test_task_protocol_child.py b/tests/harness/test_task_protocol_child.py index 973d11910..c76898035 100644 --- a/tests/harness/test_task_protocol_child.py +++ b/tests/harness/test_task_protocol_child.py @@ -235,6 +235,14 @@ def test_expired_campaign_deadline_never_starts_protocol(tmp_path, monkeypatch): assert failure(tmp_path, digest, deadline=0, arguments=[]).kind == "timeout" +@pytest.mark.parametrize("deadline", [float("nan"), float("inf"), -float("inf"), True, "soon"]) +def test_invalid_campaign_deadline_never_starts_protocol(tmp_path, monkeypatch, deadline): + digest = write_protocol(tmp_path, "def validate_arguments(a):\n return {}\n") + monkeypatch.setattr(task_protocol.local_process, "_supervise", + lambda *a, **k: pytest.fail("invalid deadline must not start")) + assert failure(tmp_path, digest, deadline=deadline, arguments=[]).kind == "request" + + @pytest.mark.parametrize("body, raises", [ (" return {}\n", False), (" raise ValueError('rejected')\n", True), diff --git a/tests/test_search_benchmarks.py b/tests/test_search_benchmarks.py index b2045f5db..bf957bb1f 100644 --- a/tests/test_search_benchmarks.py +++ b/tests/test_search_benchmarks.py @@ -97,6 +97,41 @@ def mutated_execution(*args, **kwargs): evaluator.evaluate(source, "evaluation", tmp_path / "output", time.time() + 1) +def test_engine_protocol_stages_share_the_original_deadline(tmp_path, monkeypatch): + evaluator = EngineEvaluator( + "qwen21-denoise-npu", tmp_path / "frozen", + arguments=["--pairs", "1", "--samples", "1", "--warmups", "0"], + ) + source = tmp_path / "candidate" + source.mkdir() + (source / "candidate.py").write_text("candidate") + clock = [100.0] + observed = [] + monkeypatch.setattr("inferrsi.apps.search_benchmarks.time.time", lambda: clock[0]) + monkeypatch.setattr("inferrsi.apps.search_benchmarks.time.monotonic", lambda: clock[0] + 1000) + + def protocol(root, digest, operation, *, deadline, **kwargs): + observed.append((operation, deadline, deadline - (clock[0] + 1000))) + if operation == "arguments": + clock[0] += 2 + return [] + clock[0] += 1 + return {"verdict": "INCONCLUSIVE", "score": None} + + def execute(command, cwd, output, deadline, *args, **kwargs): + observed.append(("execution", deadline, deadline - (clock[0] + 1000))) + output.mkdir(parents=True) + (output / "execution.json").write_text("{}") + clock[0] += 3 + return {"status": "completed", "returncode": 0, "cleanup_verified": True} + + monkeypatch.setattr("inferrsi.apps.search_benchmarks.task_protocol.run", protocol) + monkeypatch.setattr("inferrsi.apps.search_benchmarks.execute_trial", execute) + evaluator.evaluate(source, "evaluation", tmp_path / "output", 110.0) + assert observed == [("arguments", 1110.0, 10.0), ("execution", 1110.0, 8.0), + ("assess", 1110.0, 5.0)] + + def test_skill_comparator_preparation_does_not_access_holdout(tmp_path): from tests.fixtures.harness_improvement_demo import configuration from tools.speculative_skill import Comparator diff --git a/tests/test_search_roles.py b/tests/test_search_roles.py index 3378e7bee..1ff9219a5 100644 --- a/tests/test_search_roles.py +++ b/tests/test_search_roles.py @@ -1,4 +1,6 @@ +import json import random +from dataclasses import asdict import pytest @@ -69,3 +71,20 @@ def test_clipped_actor_does_not_reinforce_outside_positive_ratio_bound(): ) weights = (10.0, -10.0) if choice.selected == 0 else (-10.0, 10.0) assert actor_gradient(weights, model.policy, choice, 1.0, 0.2, 0.0) == (0.0, 0.0) + + +def test_injected_roles_preserve_serialized_behavior_and_checkpoint_operations(): + model = Snapshot((0.2, -0.1), (0.3, -0.2), (0.5, -0.25), "initial") + vectors = ((1.0, 0.0), (0.0, 1.0)) + options = dict(context="observed", collect=True, temperature=0.8, exploration=0.1) + choice = model.choose("policy", ("a", "b"), vectors, rng=random.Random(7), **options) + assert choice == choose(model, "policy", ("a", "b"), vectors, + rng=random.Random(7), **options) + actors = [(json.loads(json.dumps(asdict(choice))), 2.0)] + values = [(vectors[0], 4.0)] + trained = model.train(model, actors, values, (), learning_rate=0.1) + assert trained == update(model, model, [(choice, 2.0)], values, learning_rate=0.1) + restored = model.restore(json.loads(json.dumps(trained.document()))) + assert restored == trained and restored != model + assert model.predict((2.0, 4.0)) == 0.0 + assert restored.predict(vectors[0]) == trained.scorer[0] diff --git a/tests/test_vats_runtime.py b/tests/test_vats_runtime.py index 1873789ab..522667cf9 100644 --- a/tests/test_vats_runtime.py +++ b/tests/test_vats_runtime.py @@ -150,6 +150,83 @@ def crash(campaign, operation, receipt, **kwargs): assert len(list((adapter.root / "candidates").iterdir())) == before +@pytest.mark.parametrize("kind", ["draft", "verify"]) +def test_expired_launch_recovers_unstarted_block_and_finishes_with_root(runtime, monkeypatch, kind): + control, adapter, snapshot, settings, root = runtime + search = Search(control, "campaign", root.contract.search_id, snapshot, settings, adapter) + search.start(root, seed=2, collect=False) + launch = control.launch + interrupted = {} + + def expire(campaign, operation): + doc = control.read(campaign) + if doc["operations"][operation]["category"] == kind: + interrupted.update(operation=operation, spent_ms=doc["spent_ms"]) + monkeypatch.setattr( + "inferrsi.harness.work_control.time.time", lambda: doc["deadline"] + 1 + ) + return launch(campaign, operation) + + monkeypatch.setattr(control, "launch", expire) + with pytest.raises(ValueError, match="expired before launch"): + search.run() + assert interrupted + operation = interrupted["operation"] + op = control.read("campaign")["operations"][operation] + assert op["receipt"] == {"not_started": "deadline"} + assert not (adapter.root / "executions" / operation).exists() + + def no_adapter_recovery(*args): + pytest.fail("Conclusive no-launch recovery must use the durable control record") + + monkeypatch.setattr(adapter, "recover", no_adapter_recovery) + reopened = WorkControl(RunStore(control.store.root, control.store.namespace)) + resumed = Search(reopened, "campaign", root.contract.search_id, snapshot, settings, adapter) + resumed.recover() + after = reopened.read("campaign") + resumed.recover() + assert reopened.read("campaign") == after + record = resumed.run() + assert record["status"] == "complete" and record["stop_reason"] == "limit" + assert record["selected"] == root.root_id + assert record["pending"] is None and record["active_action"] is None + assert record["block_stop_reason"] == "launch_deadline" + assert reopened.read("campaign")["spent_ms"] == interrupted["spent_ms"] + if kind == "verify": + state = decode(record["tree"]) + receipt = next(r for r in state.receipts if r.attempt.attempt_id == operation) + assert receipt.verdict.value == "INCONCLUSIVE" and receipt.objective is None + assert receipt.attempt.check_id in tree.missing_checks(state, receipt.attempt.candidate_id) + with reopened.store._connection() as db: + lease = db.execute("SELECT 1 FROM work_domains WHERE operation=?", (operation,)).fetchone() + assert lease is None + + +@pytest.mark.parametrize("kind", ["draft", "verify"]) +def test_unknown_launch_is_not_treated_as_conclusively_unstarted(runtime, monkeypatch, kind): + control, adapter, snapshot, settings, root = runtime + search = Search(control, "campaign", root.contract.search_id, snapshot, settings, adapter) + search.start(root, seed=2, collect=False) + launch = control.launch + + def crash_after_intent(campaign, operation): + result = launch(campaign, operation) + if result["category"] == kind: + raise RuntimeError("lost after launch intent") + return result + + monkeypatch.setattr(control, "launch", crash_after_intent) + with pytest.raises(RuntimeError, match="lost after launch intent"): + search.run() + before = control.read("campaign") + with pytest.raises(ValueError, match="Unknown execution"): + search.recover() + assert control.read("campaign") == before + assert search.read()[1]["pending"] is not None + with pytest.raises(ValueError, match="unsettled"): + search.finish("limit") + + def test_gvo_rejects_shared_tree_leaves_forced_and_changed_start_conditions(runtime): control, adapter, snapshot, settings, root = runtime records = collect_group( @@ -163,6 +240,64 @@ def test_gvo_rejects_shared_tree_leaves_forced_and_changed_start_conditions(runt examples((records[0] | {"settings": "other"}, records[1])) with pytest.raises(ValueError, match="identical"): examples((records[0] | {"initial_conditions": "different-root-proof"}, records[1])) + legacy = {k: v for k, v in records[0].items() if k != "initial_conditions"} + with pytest.raises(ValueError, match="Legacy search"): + examples((legacy, records[1])) + + +def test_legacy_search_can_be_exported_but_cannot_resume_without_initial_evidence(runtime): + control, adapter, snapshot, settings, root = runtime + search = Search(control, "campaign", root.contract.search_id, snapshot, settings, adapter) + search.start(root, seed=0) + + def legacy(state): + state["searches"][search.search_id].pop("initial_conditions") + return state + + before = control.update("campaign", control.read("campaign")["revision"], legacy) + with pytest.raises(ValueError, match="Legacy search"): + search.run() + assert control.read("campaign") == before + + +@pytest.mark.parametrize("change", ["receipt", "execution", "epoch"]) +def test_initial_evidence_digest_ignores_only_receiving_ids(runtime, change): + from inferrsi.harness.search_campaign import import_qualified_root + + control, adapter, snapshot, settings, root = runtime + first = Search(control, "campaign", root.contract.search_id, snapshot, settings, adapter) + first.start(root, seed=0) + imported = import_qualified_root(root, replace(root.contract, search_id="same-proof")) + second = Search(control, "campaign", "same-proof", snapshot, settings, adapter) + second.start(imported, seed=1) + assert first.read()[1]["initial_conditions"] == second.read()[1]["initial_conditions"] + assert examples((first.finish("test"), second.finish("test")))[2]["returns"] == [0, 0] + + changed = import_qualified_root(root, replace(root.contract, search_id="changed-proof")) + receipt = changed.receipts[0] + if change == "receipt": + receipt = replace(receipt, evidence_digest=digest("different real observation")) + elif change == "execution": + receipt = replace(receipt, attempt=replace(receipt.attempt, execution_key="another-run")) + else: + dependency = tree.Dependency("proof-epoch", 1) + receipt = replace(receipt, attempt=replace(receipt.attempt, dependencies=(dependency,))) + changed = replace(changed, dependencies=(dependency,)) + changed = replace( + changed, + receipts=(receipt, *changed.receipts[1:]), + attempts=tuple( + receipt.attempt if a.attempt_id == receipt.attempt.attempt_id else a + for a in changed.attempts + ), + ) + third = Search(control, "campaign", "changed-proof", snapshot, settings, adapter) + third.start(changed, seed=2) + changed_root = tree.candidate(changed, changed.root_id) + assert changed_root.artifact_digest == root.candidates[0].artifact_digest + assert third.read()[1]["initial_conditions"] != first.read()[1]["initial_conditions"] + with pytest.raises(ValueError, match="identical"): + examples((first.read()[1], third.finish("test"))) def test_snapshot_change_on_resume_is_rejected(runtime): diff --git a/tests/test_verification_plans.py b/tests/test_verification_plans.py index d87028f4d..3ca3a6d35 100644 --- a/tests/test_verification_plans.py +++ b/tests/test_verification_plans.py @@ -44,6 +44,20 @@ def test_cross_branch_completion_counts_shared_preparation_once(): assert all(len(plan.job_ids) == 5 for plan in pairs) +def test_one_seed_can_complete_another_candidate_through_its_prerequisites(): + state, jobs = fixture() + jobs = tuple( + replace(job, prerequisites=("b-objective",)) if job.job_id == "a-quality" else job + for job in jobs + ) + plans = menu(state, jobs, max_targets=1) + coupled = next(plan for plan in plans if "a-objective" in plan.job_ids) + assert coupled.candidate_ids == ("a", "b") + assert coupled.job_ids.index("b-objective") < coupled.job_ids.index("a-quality") + assert coupled.cost == 15 and coupled.job_ids.count("setup") == 1 + assert len(coupled.job_ids) == 5 + + def exhaustive_oracle(jobs, budget, job_limit, target_limit): # Independent oracle enumerates all job subsets, not candidate-completion unions. result = {} diff --git a/tests/test_work_control.py b/tests/test_work_control.py index 4e7684230..f5d6f74e4 100644 --- a/tests/test_work_control.py +++ b/tests/test_work_control.py @@ -58,6 +58,39 @@ def test_unknown_cleanup_or_usage_retains_reservation_and_never_relaunches(contr assert reopened.read("campaign")["operations"]["work"]["status"] == "unknown" +def test_known_overrun_with_unknown_cleanup_blocks_other_domains_and_reconciles_once(control): + claim(control) + control.launch("campaign", "work") + control.settle( + "campaign", "work", {"result": "pending cleanup"}, actual_ms=950, cleanup_verified=False + ) + reopened = WorkControl(RunStore(control.store.root, control.store.namespace)) + result = reopened.read("campaign") + assert result["blocked"] + assert result["operations"]["work"]["status"] == "unknown" + assert reopened.available(result) == -50 + with pytest.raises(ValueError, match="blocked"): + claim(reopened, "other-domain", cap_ms=700, domains=("other-device",)) + reopened.create("other", {}, {}, cap_ms=1000, final_reserve_ms=100, deadline=time.time() + 60) + with pytest.raises(ValueError, match="leased"): + reopened.claim( + "other", 0, "work", category="verify", cap_ms=200, domains=("device",), binding={} + ) + # A later unknown observation cannot erase the retained lower bound. + reopened.settle("campaign", "work", {}, actual_ms=None, cleanup_verified=False) + assert reopened.available(reopened.read("campaign")) == -50 + with pytest.raises(ValueError, match="known usage"): + reopened.settle("campaign", "work", {}, actual_ms=50, cleanup_verified=True) + for _ in range(2): + reopened.settle("campaign", "work", {"done": True}, actual_ms=950, cleanup_verified=True) + result = reopened.read("campaign") + assert result["spent_ms"] == 950 and result["blocked"] + assert reopened.available(result) == -50 + reopened.claim( + "other", 0, "work", category="verify", cap_ms=200, domains=("device",), binding={} + ) + + def test_receipt_settlement_and_incumbent_are_atomic_and_idempotent(control): claim(control) control.launch("campaign", "work") From 6f8f2c5bc06d97fe17707196fd934cc384d4ed76 Mon Sep 17 00:00:00 2001 From: amy-why-3459 Date: Thu, 19 Nov 2026 00:52:06 +0800 Subject: [PATCH 08/16] search: preserve terminal decisions and timeout accounting Retain protocol timeout receipts with supervisor cleanup facts so known usage settles and clean domains are released without relaxing overrun blocking. Resume committed ACCEPT decisions without sampling again and preserve consumed steps when restoring paid continuations. Reject development Skill evaluators before final claims. Cover process timeouts, uncertain cleanup, restart windows and final scope validation, and document the differences from the SpecRSI reference implementation. Agent-assisted-by: Codex (implementation, tests and reference analysis) Signed-off-by: amy-why-3459 --- docs/speculative-search.md | 57 ++++++++++ src/inferrsi/apps/search_benchmarks.py | 115 +++++++++++--------- src/inferrsi/harness/search_campaign.py | 3 + src/inferrsi/harness/task_protocol.py | 12 ++- src/inferrsi/harness/vats.py | 15 ++- tests/harness/test_task_protocol_child.py | 14 ++- tests/test_search_benchmarks.py | 124 ++++++++++++++++++++++ tests/test_vats_runtime.py | 81 ++++++++++++++ 8 files changed, 365 insertions(+), 56 deletions(-) diff --git a/docs/speculative-search.md b/docs/speculative-search.md index bf7020c8b..5a81e14a2 100644 --- a/docs/speculative-search.md +++ b/docs/speculative-search.md @@ -86,6 +86,9 @@ parent artifact, without waiting for parent quality. Each check binds candidate, contract, attempt, execution, reservation and domain. Settlement commits elapsed time, cleanup facts and evidence/incumbent changes in one transaction. Recovery continues the retained block cursor before making another Policy decision. +An ACCEPT already retained in the trace is terminal even if the controller +stopped before writing the completed status. Recovery finishes that decision +without sampling or charging another preparation, including forced ACCEPT. ACCEPT and limits return only complete admissible versions. Unstarted work stops when its conservative cap no longer fits. Unknown launched operations block @@ -126,6 +129,9 @@ suffix is Scorer-only. `paired_targets` requires the same source state, settings snapshot, adapter and continuation seed with different first actions. Both absolute suffix targets and observed relative differences train the Scorer. Caller-authored unreceipted pair targets are rejected. +The saved source step counts toward the continuation's original step limit and +random-stream position; omitting the prefix from its training trace does not +grant additional decision slots. These are real updates to a constrained CPU operator model. They are not LLM finetuning, prompt summarization or evidence of pretrained-model improvement. @@ -148,6 +154,10 @@ blocks further claims immediately, even with unknown cleanup; the retained hold is at least the largest known usage. Later reconciliation releases the domain and charges the final usage once, never below that known lower bound. Absolute deadlines include queueing and cleanup. +Engine protocol timeouts retain an INCONCLUSIVE adapter receipt with measured +elapsed time. Confirmed cleanup of both task and protocol permits settlement +and domain release; uncertain protocol cleanup keeps the reservation. A known +overrun still blocks the campaign, even after its domain has been released. In-process bounded planning, editing and training are checked after return; they are not forcibly preempted like subprocesses. Preflight freezing and initial setup are outside this campaign meter, a remaining lifecycle-cost limitation. @@ -171,6 +181,9 @@ original baseline. For Skill, supply a **holdout-bound adapter** and `Comparator.holdout_scope` (derived from the frozen data digest), never the development adapter or a replacement label. Exact bounded radix utility keeps quality before token use; reduced tokens cannot offset a quality regression. +The Skill evaluator validates its holdout binding before the scope is claimed +or data is accessed, rejecting development evaluators and missing/mismatched +holdout identities. After interruption, final consumes existing receipts only. Even an unstarted remaining check cannot be newly dispatched by recovery. Existing final results @@ -217,6 +230,50 @@ executes the existing comparator under the shared process executor and revalidat its independent retained report. Construction does not load holdout bytes. Provider readiness/credentials/authorization remain existing explicit requirements. +## Relation to the paper and reference implementation + +This is a constrained adaptation of the two-loop method, not a reproduction of +the reference model or experiments. The comparison below uses +[SpecRSI v2, section 3 and Algorithms S1-S3](https://arxiv.org/html/2610.16491v2#S3) +and the authors' [reference source](https://github.com/SpecRSI-GPR/SpecRSI/tree/ff0f0bb607e53ba76eebb9960df3d6942c33027e). +That source revision is research provenance, not an InferRSI campaign pin. + +| Mechanism | This implementation | Alignment and limits | +| --- | --- | --- | +| Frozen search and acceptance | Immutable descendants, separate real evidence, DRAFT/VERIFY/ACCEPT, complete-root fallback | Follows the central control contract; intermediate parents need not pass | +| Natural group learning | Independent matched searches, leave-one-out advantage, recorded actor probabilities, clipped objective and fixed-reference KL | Same objective structure; updates a small CPU operator model | +| Value supervision | Actual paid-state suffix returns and optional paid pair differences; forced trajectories excluded from actors | Same separation of targets; continuation recovery must preserve the original remaining work limits | +| Drafter | Softmax over a caller-supplied exact-replacement vocabulary | The reference uses generated edit tokens with a trainable LoRA adapter on a frozen language model; this vocabulary cannot propose a new edit outside its supplied operators | +| Policy and Scorer | Separate linear weights over eight aggregate features | The reference uses separate relation/message-passing and attention branches with semantic and observed-evidence inputs; these linear features omit those inputs | +| Solver and refinement | Exhaustive unions of completion seeds, one job per candidate/check; optional pairwise seed-union scoring | The reference supports MILP covers, singleton check closures, shared setup and iterative job-closure extensions; this restricted planner is not a general substitute | +| Training recipe | Caller-provided initial checkpoint; direct CPU gradient updates | No supervised warm-up pipeline or language-model training adapter; the reference's warm-up, role architecture and optimizer configuration are not reproduced | +| Resources and evaluation | One worker, millisecond reservations, whole Engine/Skill checks | No equivalent campaign-wide token/currency cap, arbitrary setup execution, parallel-worker study or matched reference benchmark runs | + +The architecture and training distinction can be inspected in the reference +[role model](https://github.com/SpecRSI-GPR/SpecRSI/blob/ff0f0bb607e53ba76eebb9960df3d6942c33027e/src/specrsi/learning/hf.py), +[relation heads](https://github.com/SpecRSI-GPR/SpecRSI/blob/ff0f0bb607e53ba76eebb9960df3d6942c33027e/src/specrsi/learning/heads.py), +[completion solver](https://github.com/SpecRSI-GPR/SpecRSI/blob/ff0f0bb607e53ba76eebb9960df3d6942c33027e/src/specrsi/planning/completion.py), +[menu construction](https://github.com/SpecRSI-GPR/SpecRSI/blob/ff0f0bb607e53ba76eebb9960df3d6942c33027e/src/specrsi/core/vats.py) +and [training recipe](https://github.com/SpecRSI-GPR/SpecRSI/blob/ff0f0bb607e53ba76eebb9960df3d6942c33027e/docs/REPRODUCIBILITY.md#training-the-reference-roles). +These were inspected as source, not executed as a reproduction experiment. + +Two limits matter when interpreting learning results. First, at the same state, +verification bundles with equal total cap and job count have identical features +in `vats.features`, hence identical linear Scorer values and Policy preferences. +Their candidate identity, observed failures and check interactions cannot affect +that comparison. Parameter updates alone therefore do not demonstrate learning +the paper's complementary-check value function. Second, `Settings.refine_queries` +defaults to zero, and the Engine CLI exposes no override. Its search does not use +the trained Scorer to refine the menu; the supplied Engine/Skill evaluators also +expose one indivisible check per candidate. Python callers can enable the bounded +refinement, but that does not add the missing semantic or relational inputs. + +Moving toward a reference reproduction requires explicit role/feature and menu +contracts, a qualified generative/trainable adapter with warm-up, lifecycle cost +accounting, and matched frozen-versus-trained search experiments on real tasks. +The existing recovery, evidence and holdout controls remain useful foundations; +passing them does not establish search efficacy or reproduce paper results. + ## Validation and issue completion ```bash diff --git a/src/inferrsi/apps/search_benchmarks.py b/src/inferrsi/apps/search_benchmarks.py index b89442d19..4af9580f7 100644 --- a/src/inferrsi/apps/search_benchmarks.py +++ b/src/inferrsi/apps/search_benchmarks.py @@ -37,71 +37,84 @@ def __init__(self, package, frozen, *, arguments, python=sys.executable, prefix= } ) - def evaluate(self, candidate, check, output, deadline): - if check != "evaluation": - raise ValueError("Packaged engine protocol requires one full evaluation job") + def _check_resources(self, *, during=False): actual = { p.relative_to(self.frozen).as_posix(): sha256(p) for p in self.frozen.rglob("*") if p.is_file() } if actual != self.plan["frozen_files"]: - raise ValueError("Frozen benchmark resources changed") + suffix = " during execution" if during else "" + raise ValueError("Frozen benchmark resources changed" + suffix) + + def evaluate(self, candidate, check, output, deadline): + if check != "evaluation": + raise ValueError("Packaged engine protocol requires one full evaluation job") + self._check_resources() output.mkdir(parents=True) candidates.retain(candidate / "candidate.py", output / "candidate.py") - args = task_protocol.run( - self.frozen, - self.plan["frozen_files"]["optimization.py"], - "arguments", - deadline=time.monotonic() + max(0, deadline - time.time()), - candidate=output / "candidate.py", - output=output / "measurement", - settings=self.plan["evaluation_settings"], - ) from inferrsi.harness import optimization_session - command = ( - *self.prefix, - self.python, - str(Path(optimization_session.__file__).resolve()), - str(self.frozen / "benchmark.py"), - *args, - ) - environment = runtime_environment() - environment["PYTHONDONTWRITEBYTECODE"] = "1" - execution = execute_trial( - command, - output, - output / "execution", - time.monotonic() + max(0, deadline - time.time()), - None, - environment=environment, - include_cleanup=True, - ) - if { - p.relative_to(self.frozen).as_posix(): sha256(p) - for p in self.frozen.rglob("*") - if p.is_file() - } != self.plan["frozen_files"]: - raise ValueError("Frozen benchmark resources changed during execution") - assessed = task_protocol.run( - self.frozen, - self.plan["frozen_files"]["optimization.py"], - "assess", - deadline=time.monotonic() + max(0, deadline - time.time()), - directory=output, - settings=self.plan["evaluation_settings"], - execution=execution, - ) - if execution["cleanup_verified"] is not True: + execution = None + cleanup = True + try: + args = task_protocol.run( + self.frozen, + self.plan["frozen_files"]["optimization.py"], + "arguments", + deadline=time.monotonic() + max(0, deadline - time.time()), + candidate=output / "candidate.py", + output=output / "measurement", + settings=self.plan["evaluation_settings"], + ) + command = ( + *self.prefix, + self.python, + str(Path(optimization_session.__file__).resolve()), + str(self.frozen / "benchmark.py"), + *args, + ) + environment = runtime_environment() + environment["PYTHONDONTWRITEBYTECODE"] = "1" + execution = execute_trial( + command, + output, + output / "execution", + time.monotonic() + max(0, deadline - time.time()), + None, + environment=environment, + include_cleanup=True, + ) + cleanup = execution["cleanup_verified"] + self._check_resources(during=True) + assessed = task_protocol.run( + self.frozen, + self.plan["frozen_files"]["optimization.py"], + "assess", + deadline=time.monotonic() + max(0, deadline - time.time()), + directory=output, + settings=self.plan["evaluation_settings"], + execution=execution, + ) + except task_protocol.TaskProtocolError as error: + if error.kind != "timeout": + raise + # Retain a real timeout as INCONCLUSIVE even when no time remains + # for assessment. Cleanup covers both the task and protocol process; + # an unknown protocol exit cannot release the measurement domain. + cleanup = cleanup is True and error.cleanup_verified is True + assessed = {"verdict": "INCONCLUSIVE", "score": None, "reason": "protocol_timeout"} + self._check_resources(during=True) + if cleanup is not True: assessed = {"verdict": "INCONCLUSIVE", "score": None} atomic_write_json(output / "assessment.json", assessed) return { "verdict": assessed["verdict"], "objective": assessed.get("score"), - "cleanup_verified": execution["cleanup_verified"], + "cleanup_verified": cleanup, "assessment_sha256": sha256(output / "assessment.json"), - "execution_sha256": sha256(output / "execution/execution.json"), + "execution_sha256": sha256(output / "execution/execution.json") + if execution is not None else None, } @@ -142,6 +155,10 @@ def __init__(self, comparator, *, split, policy, token_cap, task_count): } ) + def validate_final(self, scope): + if self.split != "holdout" or not self.holdout_scope or scope != self.holdout_scope: + raise ValueError("Skill final requires a holdout-bound evaluator and its frozen scope") + def evaluate(self, candidate, check, output, deadline): if check != "evaluation": raise ValueError("Skill comparison is an indivisible evaluation job") diff --git a/src/inferrsi/harness/search_campaign.py b/src/inferrsi/harness/search_campaign.py index 96897c510..bf9c205db 100644 --- a/src/inferrsi/harness/search_campaign.py +++ b/src/inferrsi/harness/search_campaign.py @@ -252,6 +252,9 @@ def finalize(control, campaign, adapter, selected, *, holdout_scope, accept_resu doc = control.read(campaign) if doc["final_result"] is not None: return doc["final_result"] + validate_final = getattr(adapter.evaluator, "validate_final", None) + if validate_final is not None: + validate_final(holdout_scope) required_scope = getattr(adapter.evaluator, "holdout_scope", None) if required_scope is not None and holdout_scope != required_scope: raise ValueError("Final scope must match the evaluator's frozen holdout data identity") diff --git a/src/inferrsi/harness/task_protocol.py b/src/inferrsi/harness/task_protocol.py index 792b5564e..df2684a96 100644 --- a/src/inferrsi/harness/task_protocol.py +++ b/src/inferrsi/harness/task_protocol.py @@ -42,10 +42,14 @@ class TaskProtocolError(ValueError): starts with the kind, so transport, identity and result failures stay distinct. """ - def __init__(self, kind: str, message: str, *, error_type: str | None = None): + def __init__(self, kind: str, message: str, *, error_type: str | None = None, + cleanup_verified: bool | None = None): super().__init__(message if kind == "protocol_exception" else f"{kind}: {message}") self.kind = kind self.type_name = error_type if kind == "protocol_exception" else type(self).__name__ + # Only the process supervisor (or conclusive refusal to launch) can + # establish this fact. Unknown cleanup must retain the caller's lease. + self.cleanup_verified = cleanup_verified def _tail(path: Path) -> str: @@ -102,7 +106,8 @@ def run(root: Path, protocol_sha256: str, operation: str, *, deadline=None, **ar raise TaskProtocolError("request", "deadline must be finite monotonic time") timeout = min(timeout, deadline - time.monotonic()) if timeout <= 0: - raise TaskProtocolError("timeout", "deadline expired before protocol execution") + raise TaskProtocolError("timeout", "deadline expired before protocol execution", + cleanup_verified=True) if sha256(path) != protocol_sha256: raise TaskProtocolError("identity", "protocol bytes differ from the frozen digest") request = {"schema_version": protocol_child.SCHEMA_VERSION, "operation": operation, @@ -135,7 +140,8 @@ def run(root: Path, protocol_sha256: str, operation: str, *, deadline=None, **ar raise KeyboardInterrupt if record["status"] == "timeout": raise TaskProtocolError( - "timeout", f"{operation} exceeded {timeout:g} s; {stderr}") + "timeout", f"{operation} exceeded {timeout:g} s; {stderr}", + cleanup_verified=record["domain_empty"] if record["pid"] is not None else True) if record["status"] not in ("completed", "failed"): raise TaskProtocolError("launch", f"{record['status']}: {record['error']}") if record["returncode"] != 0: diff --git a/src/inferrsi/harness/vats.py b/src/inferrsi/harness/vats.py index ab8b67ac1..e201860ce 100644 --- a/src/inferrsi/harness/vats.py +++ b/src/inferrsi/harness/vats.py @@ -78,6 +78,11 @@ def depth(state, node): return result +def decision_count(record): + """Include the natural prefix omitted from a forced continuation's trace.""" + return record.get("continuation", {}).get("step", 0) + len(record["trace"]) + + def features(state, action, available, settings): """Fixed public features; ACCEPT has a fixed zero Policy logit.""" if action.kind == "accept": @@ -475,7 +480,7 @@ def pending(state): # the only recovery route, including when writing the state below fails. if kind == "draft": rng = random.Random( - f"{search['seed']}:{len(current.candidates)}:{len(search['trace'])}" + f"{search['seed']}:{len(current.candidates)}:{decision_count(search)}" ) result = self.adapter.draft( node, current, self.snapshot, rng, operation, claim["deadline"] @@ -510,6 +515,10 @@ def run(self, *, forced_action=None): for op in doc["operations"].values() ): raise ValueError("Unresolved claim: recover without relaunch") + if search["trace"] and search["trace"][-1]["action"]["kind"] == "accept": + # Preparation/copy already committed this terminal decision. + # A crash before finish must not sample or pay for another action. + return self.finish("forced_accept" if search["trace"][-1]["forced"] else "accept") if search["active_action"] is not None: self._continue_action() continue @@ -520,7 +529,7 @@ def run(self, *, forced_action=None): current.contract.max_attempts - len(current.attempts) - search["draft_attempts"] ) if ( - len(search["trace"]) >= self.settings.max_steps + decision_count(search) >= self.settings.max_steps or attempts_left <= 0 or remaining <= self.settings.preparation_ms or time.time() >= doc["deadline"] @@ -558,7 +567,7 @@ def run(self, *, forced_action=None): vectors = tuple( features(current, action, remaining, self.settings) for action in actions ) - rng = random.Random(f"{search['seed']}:policy:{len(search['trace'])}") + rng = random.Random(f"{search['seed']}:policy:{decision_count(search)}") choice = self.snapshot.choose( "policy", [action.key for action in actions], diff --git a/tests/harness/test_task_protocol_child.py b/tests/harness/test_task_protocol_child.py index c76898035..cf4171517 100644 --- a/tests/harness/test_task_protocol_child.py +++ b/tests/harness/test_task_protocol_child.py @@ -225,6 +225,7 @@ def test_campaign_deadline_bounds_the_protocol_timeout(tmp_path): started = time.monotonic() error = failure(tmp_path, digest, deadline=started + 1.0, arguments=[]) assert error.kind == "timeout" + assert error.cleanup_verified is True assert time.monotonic() - started < 10 @@ -232,7 +233,18 @@ def test_expired_campaign_deadline_never_starts_protocol(tmp_path, monkeypatch): digest = write_protocol(tmp_path, "def validate_arguments(a):\n return {}\n") monkeypatch.setattr(task_protocol.local_process, "_supervise", lambda *a, **k: pytest.fail("expired protocol must not start")) - assert failure(tmp_path, digest, deadline=0, arguments=[]).kind == "timeout" + error = failure(tmp_path, digest, deadline=0, arguments=[]) + assert error.kind == "timeout" and error.cleanup_verified is True + + +@pytest.mark.parametrize("cleanup", [False, None]) +def test_protocol_timeout_retains_uncertain_cleanup(tmp_path, monkeypatch, cleanup): + digest = write_protocol(tmp_path, "def validate_arguments(a):\n return {}\n") + monkeypatch.setattr(task_protocol.local_process, "_supervise", lambda *a, **k: { + "status": "timeout", "pid": 123, "domain_empty": cleanup, + }) + error = failure(tmp_path, digest) + assert error.kind == "timeout" and error.cleanup_verified is cleanup @pytest.mark.parametrize("deadline", [float("nan"), float("inf"), -float("inf"), True, "soon"]) diff --git a/tests/test_search_benchmarks.py b/tests/test_search_benchmarks.py index bf957bb1f..515b35ea5 100644 --- a/tests/test_search_benchmarks.py +++ b/tests/test_search_benchmarks.py @@ -1,4 +1,5 @@ import json +import sys import time from types import SimpleNamespace @@ -8,6 +9,13 @@ from inferrsi.apps.search_benchmarks import EngineEvaluator, SkillEvaluator from inferrsi.apps.speculative_search import command from inferrsi.benchmark.improvement import SelectionPolicy +from inferrsi.harness.run_store import RunStore +from inferrsi.harness.scenarios.external import execute_trial +from inferrsi.harness.search_adapter import ArtifactAdapter, Edit +from inferrsi.harness.search_campaign import finalize, qualify_root +from inferrsi.harness.task_protocol import TaskProtocolError +from inferrsi.harness.work_control import WorkControl +from tests.fixtures.speculative_search import contract def skill_report(base_tokens=100, tokens=80, *, verdict="PASS", outcome="unchanged"): @@ -132,6 +140,122 @@ def execute(command, cwd, output, deadline, *args, **kwargs): ("assess", 1110.0, 5.0)] +def test_engine_known_timeout_settles_usage_and_releases_domain(tmp_path, monkeypatch): + evaluator = EngineEvaluator( + "qwen21-denoise-npu", tmp_path / "frozen", + arguments=["--pairs", "1", "--samples", "1", "--warmups", "0"], + ) + executions = [] + + def timeout(command, cwd, output, deadline, *args, **kwargs): + # Exercise real process cleanup without model/device execution. + result = execute_trial((sys.executable, "-c", "import time; time.sleep(60)"), + cwd, output, deadline, *args, **kwargs) + executions.append(result) + return result + + monkeypatch.setattr("inferrsi.apps.search_benchmarks.execute_trial", timeout) + source = tmp_path / "source" + source.mkdir() + (source / "candidate.py").write_text("value = 1\n") + adapter = ArtifactAdapter(tmp_path / "artifacts", [Edit("edit", "candidate.py", "1", "2")], + {"evaluation": 2000}, evaluator, domain="cpu-timeout-test") + root = adapter.import_root(source, snapshot="test-snapshot", context={}) + control = WorkControl(RunStore(tmp_path / "store", "timeout-test")) + control.create("campaign", {}, {"searches": {}}, cap_ms=20000, final_reserve_ms=3000, + deadline=time.time() + 20) + configuration = contract(checks=("evaluation",), objective_check="evaluation") + with pytest.raises(ValueError, match="Root lacks complete"): + qualify_root(control, "campaign", adapter, root, configuration) + assert executions[0]["status"] == "timeout" and executions[0]["cleanup_verified"] is True + reopened = WorkControl(RunStore(control.store.root, control.store.namespace)) + doc = reopened.read("campaign") + operation, settled = next(iter(doc["operations"].items())) + assert settled["status"] == "settled" and settled["actual_ms"] > 0 + assert doc["spent_ms"] == settled["actual_ms"] + assert settled["receipt"]["verdict"] == "INCONCLUSIVE" + assert settled["receipt"]["cleanup_verified"] is True + assert adapter.recover(operation, {"kind": "verify", "binding": settled["binding"]}) == ( + settled["receipt"]) + with reopened.store._connection() as db: + assert db.execute("SELECT 1 FROM work_domains").fetchone() is None + # Deadline overruns remain blocking; a clean timeout is not a free retry. + assert doc["blocked"] + + +@pytest.mark.parametrize("stage", ["arguments", "assess"]) +@pytest.mark.parametrize("protocol_cleanup", [True, False, None]) +def test_engine_protocol_timeout_preserves_cleanup_facts( + tmp_path, monkeypatch, stage, protocol_cleanup +): + evaluator = EngineEvaluator( + "qwen21-denoise-npu", tmp_path / "frozen", + arguments=["--pairs", "1", "--samples", "1", "--warmups", "0"], + ) + source = tmp_path / "source" + source.mkdir() + (source / "candidate.py").write_text("candidate") + + def protocol(root, digest, operation, **kwargs): + if operation == stage: + raise TaskProtocolError( + "timeout", "injected timeout", cleanup_verified=protocol_cleanup) + return [] + + def execute(command, cwd, output, *args, **kwargs): + output.mkdir(parents=True) + (output / "execution.json").write_text("{}") + return {"status": "completed", "returncode": 0, "cleanup_verified": True} + + monkeypatch.setattr("inferrsi.apps.search_benchmarks.task_protocol.run", protocol) + monkeypatch.setattr("inferrsi.apps.search_benchmarks.execute_trial", execute) + result = evaluator.evaluate(source, "evaluation", tmp_path / "output", time.time() + 10) + assert result["verdict"] == "INCONCLUSIVE" and result["objective"] is None + assert result["cleanup_verified"] is (protocol_cleanup is True) + assert (result["execution_sha256"] is None) == (stage == "arguments") + + +@pytest.mark.parametrize("split, declared_scope, supplied_scope, valid", [ + ("development", "skill-data:holdout", "skill-data:holdout", False), + ("holdout", None, "skill-data:holdout", False), + ("holdout", "skill-data:holdout", "replacement-label", False), + ("holdout", "skill-data:holdout", "skill-data:holdout", True), +]) +def test_skill_final_requires_holdout_before_claim_or_access( + tmp_path, split, declared_scope, supplied_scope, valid +): + accesses = [] + + def compare(*args, split, **kwargs): + accesses.append(split) + return skill_report(tokens=100), True + + comparator = SimpleNamespace(identity="test-comparator", run=compare, + holdout_scope=declared_scope) + evaluator = SkillEvaluator(comparator, split=split, policy=SelectionPolicy(1, 0), + token_cap=1000, task_count=1) + adapter = ArtifactAdapter(tmp_path / "artifacts", [Edit("edit", "skill.txt", "old", "new")], + {"evaluation": 3000}, evaluator, domain="skill-test") + source = tmp_path / "source" + source.mkdir() + (source / "skill.txt").write_text("old") + root = adapter.import_root(source, snapshot="test-snapshot", context={}) + control = WorkControl(RunStore(tmp_path / "store", "skill-test")) + before = control.create("campaign", {}, {}, cap_ms=10000, final_reserve_ms=3000, + deadline=time.time() + 10) + kwargs = dict(holdout_scope=supplied_scope, accept_result=lambda results: True) + if valid: + result = finalize(control, "campaign", adapter, root, **kwargs) + assert result["verdict"] == "PASS" and result["promoted"] + assert accesses == ["holdout"] + assert finalize(control, "campaign", adapter, root, **kwargs) == result + assert accesses == ["holdout"] + else: + with pytest.raises(ValueError, match="holdout-bound"): + finalize(control, "campaign", adapter, root, **kwargs) + assert control.read("campaign") == before and not accesses + + def test_skill_comparator_preparation_does_not_access_holdout(tmp_path): from tests.fixtures.harness_improvement_demo import configuration from tools.speculative_skill import Comparator diff --git a/tests/test_vats_runtime.py b/tests/test_vats_runtime.py index 522667cf9..fc3cd417d 100644 --- a/tests/test_vats_runtime.py +++ b/tests/test_vats_runtime.py @@ -150,6 +150,36 @@ def crash(campaign, operation, receipt, **kwargs): assert len(list((adapter.root / "candidates").iterdir())) == before +def test_durable_accept_recovers_without_another_policy_decision(runtime, monkeypatch): + control, adapter, snapshot, settings, root = runtime + snapshot = replace(snapshot, policy=(0.0,) * 8) + root = replace(root, contract=replace( + root.contract, improver_snapshot=digest(snapshot.document()))) + search = Search(control, "campaign", "search", snapshot, settings, adapter) + # This seed selects ACCEPT first, but would select DRAFT on a second decision. + search.start(root, seed=7, collect=True) + + def crash(reason): + assert reason == "accept" + raise RuntimeError("crash after durable ACCEPT") + + monkeypatch.setattr(search, "finish", crash) + with pytest.raises(RuntimeError, match="durable ACCEPT"): + search.run() + before = control.read("campaign") + reopened = WorkControl(RunStore(control.store.root, control.store.namespace)) + resumed = Search(reopened, "campaign", "search", snapshot, settings, adapter) + record = resumed.run() + assert record["status"] == "complete" and record["stop_reason"] == "accept" + assert record["selected"] == root.root_id + assert record["trace"] == before["state"]["searches"]["search"]["trace"] + assert reopened.read("campaign")["operations"] == before["operations"] + assert reopened.read("campaign")["spent_ms"] == before["spent_ms"] + after = reopened.read("campaign") + assert resumed.run() == record + assert reopened.read("campaign") == after + + @pytest.mark.parametrize("kind", ["draft", "verify"]) def test_expired_launch_recovers_unstarted_block_and_finishes_with_root(runtime, monkeypatch, kind): control, adapter, snapshot, settings, root = runtime @@ -364,6 +394,57 @@ def test_paid_continuations_are_metered_matched_and_excluded_from_actors(runtime assert trained.scorer != snapshot.scorer +def test_late_continuation_preserves_the_consumed_step_budget(runtime): + from inferrsi.harness.gvo import suffix_examples + from inferrsi.harness.search_continuations import continue_from + from inferrsi.harness.vats import Action + + control, adapter, snapshot, settings, root = runtime + settings = replace(settings, max_steps=2) + source = Search(control, "campaign", "search", snapshot, settings, adapter) + source.start(root, seed=12, collect=False) + original = source.run() + assert [s["action"]["kind"] for s in original["trace"]] == ["draft", "verify"] + action = next(Action(**raw) for raw in original["trace"][1]["menu"] + if raw["kind"] == "draft") + args = (source, 1, action.key, "late-suffix") + record = continue_from(*args, copy_cap_ms=3000, seed=91) + assert record["continuation"]["step"] == 1 + assert [s["action"]["kind"] for s in record["trace"]] == ["draft"] + assert record["stop_reason"] == "limit" and record["selected"] == root.root_id + assert record["gain"] == record["initial_gain"] == 0 + assert suffix_examples(record, expense_weight=0, elapsed_weight=0)[0][1] == 0 + before = control.read("campaign") + assert continue_from(*args, copy_cap_ms=3000, seed=91) == record + assert control.read("campaign") == before + + +def test_forced_accept_recovers_after_copy_without_dispatch(runtime, monkeypatch): + from inferrsi.harness.search_continuations import continue_from + from inferrsi.harness.vats import Action + + control, adapter, snapshot, settings, root = runtime + source = Search(control, "campaign", "search", snapshot, settings, adapter) + source.start(root, seed=12, collect=False) + original = source.run() + action = next(Action(**raw) for raw in original["trace"][0]["menu"] + if raw["kind"] == "accept") + args = (source, 0, action.key, "accept-suffix") + with monkeypatch.context() as patch: + def crash(*args): + raise RuntimeError("crash after continuation copy") + + patch.setattr(Search, "finish", crash) + with pytest.raises(RuntimeError, match="continuation copy"): + continue_from(*args, copy_cap_ms=3000, seed=91) + before = control.read("campaign") + record = continue_from(*args, copy_cap_ms=3000, seed=91) + assert record["stop_reason"] == "forced_accept" and record["selected"] == root.root_id + assert len(record["trace"]) == 1 + assert control.read("campaign")["operations"] == before["operations"] + assert control.read("campaign")["spent_ms"] == before["spent_ms"] + + def test_root_invalidation_is_persisted_and_cannot_return_an_invalid_root(runtime): control, adapter, snapshot, settings, root = runtime dependency = tree.Dependency("environment", 0) From 41494cd699b32bd8f60e904520445985f8dcc3d3 Mon Sep 17 00:00:00 2001 From: amy-why-3459 Date: Mon, 23 Nov 2026 13:56:06 +0800 Subject: [PATCH 09/16] search: reconcile blocked recovery and adapt to current module layout Archive settled over-budget work without adopting its output or reopening dispatch. Keep search cost accounting idempotent across cleanup and recovery, and cover operation-only expiry and cleanup combinations. Move the CPU roles and search composition into Apps workflows, update trial imports, and verify indexed Skill snapshots after the catalog migration. Agent-assisted-by: Codex (implementation and validation) Signed-off-by: amy-why-3459 --- docs/apps-module-migrations.json | 1 + docs/operations.md | 2 +- docs/speculative-search.md | 22 ++-- src/inferrsi/apps/cli/__init__.py | 2 +- src/inferrsi/apps/cli/arguments.py | 2 +- .../apps/{ => workflows}/search_benchmarks.py | 4 +- .../{model => apps/workflows}/search_roles.py | 2 +- .../{ => workflows}/speculative_search.py | 2 +- src/inferrsi/harness/search_adapter.py | 2 +- src/inferrsi/harness/vats.py | 34 ++++-- src/inferrsi/harness/work_control.py | 12 +++ tests/test_search_benchmarks.py | 74 ++++++++++--- tests/test_search_roles.py | 8 +- tests/test_vats_runtime.py | 100 ++++++++++++++++-- tests/test_work_control.py | 29 +++++ tools/speculative_skill.py | 2 +- 16 files changed, 244 insertions(+), 54 deletions(-) rename src/inferrsi/apps/{ => workflows}/search_benchmarks.py (98%) rename src/inferrsi/{model => apps/workflows}/search_roles.py (99%) rename src/inferrsi/apps/{ => workflows}/speculative_search.py (99%) diff --git a/docs/apps-module-migrations.json b/docs/apps-module-migrations.json index c407f967d..da5d3de05 100644 --- a/docs/apps-module-migrations.json +++ b/docs/apps-module-migrations.json @@ -268,6 +268,7 @@ "docs/contracts/function-owners.md", "docs/harness-improvement.md", "docs/module-layout.md", + "src/inferrsi/apps/workflows/search_benchmarks.py", "tests/check_installed.py", "tests/apps/test_apps_module_migrations.py", "tests/harness/test_harness_improvement.py", diff --git a/docs/operations.md b/docs/operations.md index 13fc39135..4e90d7dc8 100644 --- a/docs/operations.md +++ b/docs/operations.md @@ -73,7 +73,7 @@ Effect classes used below: | Build an offline candidate tree and completion menu | Python `harness.search_tree` / `harness.verification_plans.completion_menu`; [control contract](speculative-search.md) | Read-only / in-memory transitions | Immutable candidates, scoped caller-supplied receipts and bounded exact plans; execution, durable state, accounting, learned roles and final acceptance require separate adapters | | Run bounded speculative Engine search and role updates | `inferrsi search engine`; [support and configuration](speculative-search.md) | Execution / candidate writes / CPU model updates | Explicit shared RunStore, permissions, checkpoint, budgets, domain and final gate; actual device allocation is caller-owned | | Inspect/export speculative search | `inferrsi search status` / `search export` | Read / output artifact | Reads the existing store; never restarts search, training or final evaluation | -| Compose Skill speculative search | Python `apps.search_benchmarks.SkillEvaluator` and checkout `tools.speculative_skill.Comparator` | Explicit provider execution through existing comparator | Frozen original baseline, split descriptors, independent grader and bounded provider; see campaign accounting limits in the support matrix | +| Compose Skill speculative search | Python `apps.workflows.search_benchmarks.SkillEvaluator` and checkout `tools.speculative_skill.Comparator` | Explicit provider execution through existing comparator | Frozen original baseline, split descriptors, independent grader and bounded provider; see campaign accounting limits in the support matrix | | Plan and report skill PR evaluations | [Skill PR integration](skill-eval-pr.md), `python -m tools.skill_eval_pr` | Read-only planning / authorized experiment-executing / PR-publishing | Trusted mapping, exact operator approval, consumed execution identity and credential-separated publication from retained evidence; no automatic paid trials | | Recover goal progress and the next proposal | [Offline goal checkpoints](goal-checkpoints.md), `python -m tools.goal_checkpoint` | Read-only | Explicit scope/criteria, pinned owner decisions and existing comparison receipts; a disposable view, never execution or acceptance authority | | Summarize campaign gains | `tools/campaign_summary_chart.py`; `report`/`run` figure | Figure-generating | Renders recorded values into one gain figure plus a matching table; validates the specification and computes nothing new | diff --git a/docs/speculative-search.md b/docs/speculative-search.md index 5a81e14a2..f193cd678 100644 --- a/docs/speculative-search.md +++ b/docs/speculative-search.md @@ -13,13 +13,13 @@ paper's language-model training or establish its reported gains. Existing | --- | --- | --- | | Candidate/evidence model | `harness.search_tree`, `search_records` | Immutable artifacts, actual dependency epochs and complete-candidate selection | | Solver | `harness.verification_plans`, `vats.menu` | Bounded exhaustive completion plans; conservative integer millisecond caps | -| Drafter | `model.search_roles`, `harness.search_adapter` | Trainable softmax over supplied exact text edits; actual retained changes, not free-form LLM generation | -| Policy | `model.search_roles` | Independent eight-feature linear softmax, recorded behavior probabilities, deterministic deployment actions | -| Scorer | `model.search_roles` | Separate linear model; actual suffix MSE and optional paid continuation differences | +| Drafter | `apps.workflows.search_roles`, `harness.search_adapter` | Trainable softmax over supplied exact text edits; actual retained changes, not free-form LLM generation | +| Policy | `apps.workflows.search_roles` | Independent eight-feature linear softmax, recorded behavior probabilities, deterministic deployment actions | +| Scorer | `apps.workflows.search_roles` | Separate linear model; actual suffix MSE and optional paid continuation differences | | Work/state authority | `harness.work_control` | Transactions in the shared `RunStore` SQLite database | -| Execution | Existing `scenarios.external` and `local_process` | Absolute deadline and process-domain cleanup; no new subprocess runner | -| Engine | `apps.search_benchmarks.EngineEvaluator` | Existing packaged `optimization.py` as one whole check; currently the Qwen denoiser NPU task | -| Skill | `apps.search_benchmarks.SkillEvaluator`, checkout `tools.speculative_skill.Comparator` | Explicit provider configuration, original baseline, independent grader, separate development/holdout descriptors | +| Execution | Existing `trials.external` and `local_process` | Absolute deadline and process-domain cleanup; no new subprocess runner | +| Engine | `apps.workflows.search_benchmarks.EngineEvaluator` | Existing packaged `optimization.py` as one whole check; currently the Qwen denoiser NPU task | +| Skill | `apps.workflows.search_benchmarks.SkillEvaluator`, checkout `tools.speculative_skill.Comparator` | Explicit provider configuration, original baseline, independent grader, separate development/holdout descriptors | | Final | `harness.search_campaign.finalize` | One-time scope claim before access; receipt-only recovery/export; task promotion only | This is a trusted local host controller. Resource domains exclude **cooperating @@ -137,7 +137,7 @@ These are real updates to a constrained CPU operator model. They are not LLM finetuning, prompt summarization or evidence of pretrained-model improvement. Apps injects the role snapshot's `choose`, `predict`, `train`, `restore` and `document` operations. Harness retains traces and controls when they may run; -it has no import dependency on Model. +it has no import dependency on the Apps role implementation. ## Accounting, recovery and final @@ -158,6 +158,11 @@ Engine protocol timeouts retain an INCONCLUSIVE adapter receipt with measured elapsed time. Confirmed cleanup of both task and protocol permits settlement and domain release; uncertain protocol cleanup keeps the reservation. A known overrun still blocks the campaign, even after its domain has been released. +If cleanup settles blocked search work before its pending pointer is cleared, +recovery archives that pointer from the durable operation without reading the +adapter again. It charges the search's observed cost once, abandons the active +block and records verification as control-only `INCONCLUSIVE`. The output is +not adopted, and the campaign remains blocked against further dispatch. In-process bounded planning, editing and training are checked after return; they are not forcibly preempted like subprocesses. Preflight freezing and initial setup are outside this campaign meter, a remaining lifecycle-cost limitation. @@ -173,7 +178,8 @@ that dispatch never occurred. Search recovery clears its pending work and ends the current block; a registered verification attempt gets a control-only `INCONCLUSIVE` receipt, with no objective or evaluator PASS. No execution is charged. A search whose limit has expired can then return its qualified incumbent. -This exception does not apply to ambiguous launch-intent crashes. +If only the operation deadline expired, the remaining campaign may continue with +fresh claims. This exception does not apply to ambiguous launch-intent crashes. `finalize` consumes a unique scope in the shared store before final data access, freezes selection and closes search/training. Engine compares again with the diff --git a/src/inferrsi/apps/cli/__init__.py b/src/inferrsi/apps/cli/__init__.py index d34797adc..e40dfc39a 100644 --- a/src/inferrsi/apps/cli/__init__.py +++ b/src/inferrsi/apps/cli/__init__.py @@ -500,7 +500,7 @@ def main(argv: list[str] | None = None) -> int: _print(result) return 0 if args.command == "search": - from ..speculative_search import command as search_command + from ..workflows.speculative_search import command as search_command _print(search_command(args)) return 0 diff --git a/src/inferrsi/apps/cli/arguments.py b/src/inferrsi/apps/cli/arguments.py index 3bdeb6aed..80102f97e 100644 --- a/src/inferrsi/apps/cli/arguments.py +++ b/src/inferrsi/apps/cli/arguments.py @@ -243,7 +243,7 @@ def parser(*, parser_class=argparse.ArgumentParser) -> argparse.ArgumentParser: from .evaluate import add_parser as add_evaluate_parser add_evaluate_parser(commands) - from ..speculative_search import add_arguments as search_arguments + from ..workflows.speculative_search import add_arguments as search_arguments search_arguments(commands) mindie = commands.add_parser("mindie", help="Use project-approved, local MindIE knowledge") diff --git a/src/inferrsi/apps/search_benchmarks.py b/src/inferrsi/apps/workflows/search_benchmarks.py similarity index 98% rename from src/inferrsi/apps/search_benchmarks.py rename to src/inferrsi/apps/workflows/search_benchmarks.py index 4af9580f7..b596888c5 100644 --- a/src/inferrsi/apps/search_benchmarks.py +++ b/src/inferrsi/apps/workflows/search_benchmarks.py @@ -10,14 +10,14 @@ import time from pathlib import Path -from inferrsi.apps.improvement import assessment +from inferrsi.apps.workflows.improvement import assessment from inferrsi.benchmark.improvement import SelectionPolicy, holdout_accepts from inferrsi.benchmark.registry import task_resources from inferrsi.harness import candidates, task_protocol from inferrsi.harness.artifact_primitives import digest, sha256 from inferrsi.harness.atomic_json import atomic_write_json from inferrsi.harness.optimization_session import runtime_environment -from inferrsi.harness.scenarios.external import execute_trial +from inferrsi.harness.trials.external import execute_trial class EngineEvaluator: diff --git a/src/inferrsi/model/search_roles.py b/src/inferrsi/apps/workflows/search_roles.py similarity index 99% rename from src/inferrsi/model/search_roles.py rename to src/inferrsi/apps/workflows/search_roles.py index e0a8e9501..47bb55799 100644 --- a/src/inferrsi/model/search_roles.py +++ b/src/inferrsi/apps/workflows/search_roles.py @@ -62,7 +62,7 @@ def document(self): } def choose(self, *args, **kwargs): - """Injected role operation; Harness does not import this Model module.""" + """Injected role operation; Harness does not import this Apps module.""" return choose(self, *args, **kwargs) def predict(self, features): diff --git a/src/inferrsi/apps/speculative_search.py b/src/inferrsi/apps/workflows/speculative_search.py similarity index 99% rename from src/inferrsi/apps/speculative_search.py rename to src/inferrsi/apps/workflows/speculative_search.py index 01deb8b3f..5f5fbca9e 100644 --- a/src/inferrsi/apps/speculative_search.py +++ b/src/inferrsi/apps/workflows/speculative_search.py @@ -6,6 +6,7 @@ from dataclasses import asdict, replace from pathlib import Path +from inferrsi.apps.workflows.search_roles import Snapshot from inferrsi.harness import candidates, search_tree from inferrsi.harness.artifact_primitives import digest from inferrsi.harness.atomic_json import atomic_write_json @@ -15,7 +16,6 @@ from inferrsi.harness.search_records import decode from inferrsi.harness.vats import Settings from inferrsi.harness.work_control import WorkControl -from inferrsi.model.search_roles import Snapshot def add_arguments(commands): diff --git a/src/inferrsi/harness/search_adapter.py b/src/inferrsi/harness/search_adapter.py index 8375337fe..882bceae5 100644 --- a/src/inferrsi/harness/search_adapter.py +++ b/src/inferrsi/harness/search_adapter.py @@ -18,7 +18,7 @@ from .artifact_primitives import digest, sha256 from .atomic_json import atomic_write_json from .optimization_session import runtime_environment -from .scenarios.external import execute_trial +from .trials.external import execute_trial from .verification_plans import Job diff --git a/src/inferrsi/harness/vats.py b/src/inferrsi/harness/vats.py index e201860ce..662882601 100644 --- a/src/inferrsi/harness/vats.py +++ b/src/inferrsi/harness/vats.py @@ -382,11 +382,12 @@ def recover(self): if pending is None: return operation = doc["operations"][pending["operation"]] - if operation["status"] == "settled" and operation["receipt"] == {"not_started": "deadline"}: - # launch() conclusively refused dispatch. There can be no adapter - # receipt: settle the Attempt as control-only INCONCLUSIVE, and - # abandon the remaining block without inventing check evidence. - def not_started(state): + not_started = operation["receipt"] == {"not_started": "deadline"} + if operation["status"] == "settled" and (not_started or doc["blocked"]): + # The durable control record is conclusive. Unstarted work has no + # adapter receipt, and blocked work cannot contribute its output. + # Close the Attempt with control-only INCONCLUSIVE evidence. + def archive(state): from inferrsi.contracts.claim import Verdict record = state["searches"][self.search_id] @@ -404,15 +405,26 @@ def not_started(state): tree=encode(current), pending=None, active_action=None, - block_stop_reason="launch_deadline", + block_stop_reason="campaign_blocked" if doc["blocked"] else "launch_deadline", ) + if doc["blocked"]: + record["spent_ms"] += operation["actual_ms"] + record.setdefault("discarded_operations", []).append(pending["operation"]) + record["status"] = "blocked" + if pending["kind"] == "draft" and not not_started: + record["draft_attempts"] += 1 try: record["selected"] = tree.accept(current).candidate_id except ValueError: record.update(status="blocked", selected=None) return state - self.control.update(self.campaign, doc["revision"], not_started) + if doc["blocked"]: + self.control.reconcile_blocked( + self.campaign, doc["revision"], pending["operation"], archive + ) + else: + self.control.update(self.campaign, doc["revision"], archive) return # All uncertain launches still require an adapter receipt. Never call # draft/verify from recovery, including after launch-intent crashes. @@ -427,6 +439,10 @@ def not_started(state): cleanup_verified=result.get("cleanup_verified"), transition=lambda state: self._change(state, pending["operation"], result), ) + settled = self.control.read(self.campaign) + if (settled["blocked"] + and settled["operations"][pending["operation"]]["status"] == "settled"): + self.recover() def _execute(self, kind, node, check=None): doc, search, current = self.read() @@ -506,10 +522,10 @@ def run(self, *, forced_action=None): doc, search, current = self.read() if search["status"] == "complete": return search - if search["status"] == "blocked": - raise ValueError("Search root evidence is invalid; search remains blocked") if doc["blocked"] or doc["final_claim"] is not None: raise ValueError("Campaign blocked or final selection frozen") + if search["status"] == "blocked": + raise ValueError("Search root evidence is invalid; search remains blocked") if any( op["status"] != "settled" and op["binding"].get("search") == self.search_id for op in doc["operations"].values() diff --git a/src/inferrsi/harness/work_control.py b/src/inferrsi/harness/work_control.py index 97ace875a..d82a7d989 100644 --- a/src/inferrsi/harness/work_control.py +++ b/src/inferrsi/harness/work_control.py @@ -166,6 +166,18 @@ def claim( body["state"] = transition(body["state"]) return self.read(campaign) + def reconcile_blocked(self, campaign, revision, operation, transition): + """Archive settled work in consumer state without reopening the campaign. + + The transition must not adopt the operation's output as usable evidence. + Accounting, receipts and the dispatch block remain unchanged. + """ + with self._transaction(campaign, revision) as (_, body): + if not body["blocked"] or body["operations"][operation]["status"] != "settled": + raise ValueError("Blocked recovery requires conclusively settled work") + body["state"] = transition(body["state"]) + return self.read(campaign) + def launch(self, campaign, operation): """Persist intent before external launch. A crash here must never relaunch.""" expired = False diff --git a/tests/test_search_benchmarks.py b/tests/test_search_benchmarks.py index 515b35ea5..5a2c6d76a 100644 --- a/tests/test_search_benchmarks.py +++ b/tests/test_search_benchmarks.py @@ -6,14 +6,14 @@ import pytest from inferrsi.apps.cli.arguments import parser -from inferrsi.apps.search_benchmarks import EngineEvaluator, SkillEvaluator -from inferrsi.apps.speculative_search import command +from inferrsi.apps.workflows.search_benchmarks import EngineEvaluator, SkillEvaluator +from inferrsi.apps.workflows.speculative_search import command from inferrsi.benchmark.improvement import SelectionPolicy from inferrsi.harness.run_store import RunStore -from inferrsi.harness.scenarios.external import execute_trial from inferrsi.harness.search_adapter import ArtifactAdapter, Edit from inferrsi.harness.search_campaign import finalize, qualify_root from inferrsi.harness.task_protocol import TaskProtocolError +from inferrsi.harness.trials.external import execute_trial from inferrsi.harness.work_control import WorkControl from tests.fixtures.speculative_search import contract @@ -100,7 +100,9 @@ def mutated_execution(*args, **kwargs): (evaluator.frozen / "reference.py").write_text("changed reference") return {"status": "completed", "returncode": 0, "cleanup_verified": True} - monkeypatch.setattr("inferrsi.apps.search_benchmarks.execute_trial", mutated_execution) + monkeypatch.setattr( + "inferrsi.apps.workflows.search_benchmarks.execute_trial", mutated_execution + ) with pytest.raises(ValueError, match="resources changed during"): evaluator.evaluate(source, "evaluation", tmp_path / "output", time.time() + 1) @@ -115,8 +117,10 @@ def test_engine_protocol_stages_share_the_original_deadline(tmp_path, monkeypatc (source / "candidate.py").write_text("candidate") clock = [100.0] observed = [] - monkeypatch.setattr("inferrsi.apps.search_benchmarks.time.time", lambda: clock[0]) - monkeypatch.setattr("inferrsi.apps.search_benchmarks.time.monotonic", lambda: clock[0] + 1000) + monkeypatch.setattr("inferrsi.apps.workflows.search_benchmarks.time.time", lambda: clock[0]) + monkeypatch.setattr( + "inferrsi.apps.workflows.search_benchmarks.time.monotonic", lambda: clock[0] + 1000 + ) def protocol(root, digest, operation, *, deadline, **kwargs): observed.append((operation, deadline, deadline - (clock[0] + 1000))) @@ -133,8 +137,8 @@ def execute(command, cwd, output, deadline, *args, **kwargs): clock[0] += 3 return {"status": "completed", "returncode": 0, "cleanup_verified": True} - monkeypatch.setattr("inferrsi.apps.search_benchmarks.task_protocol.run", protocol) - monkeypatch.setattr("inferrsi.apps.search_benchmarks.execute_trial", execute) + monkeypatch.setattr("inferrsi.apps.workflows.search_benchmarks.task_protocol.run", protocol) + monkeypatch.setattr("inferrsi.apps.workflows.search_benchmarks.execute_trial", execute) evaluator.evaluate(source, "evaluation", tmp_path / "output", 110.0) assert observed == [("arguments", 1110.0, 10.0), ("execution", 1110.0, 8.0), ("assess", 1110.0, 5.0)] @@ -154,7 +158,7 @@ def timeout(command, cwd, output, deadline, *args, **kwargs): executions.append(result) return result - monkeypatch.setattr("inferrsi.apps.search_benchmarks.execute_trial", timeout) + monkeypatch.setattr("inferrsi.apps.workflows.search_benchmarks.execute_trial", timeout) source = tmp_path / "source" source.mkdir() (source / "candidate.py").write_text("value = 1\n") @@ -185,8 +189,9 @@ def timeout(command, cwd, output, deadline, *args, **kwargs): @pytest.mark.parametrize("stage", ["arguments", "assess"]) @pytest.mark.parametrize("protocol_cleanup", [True, False, None]) +@pytest.mark.parametrize("task_cleanup", [True, False, None]) def test_engine_protocol_timeout_preserves_cleanup_facts( - tmp_path, monkeypatch, stage, protocol_cleanup + tmp_path, monkeypatch, stage, protocol_cleanup, task_cleanup ): evaluator = EngineEvaluator( "qwen21-denoise-npu", tmp_path / "frozen", @@ -205,13 +210,15 @@ def protocol(root, digest, operation, **kwargs): def execute(command, cwd, output, *args, **kwargs): output.mkdir(parents=True) (output / "execution.json").write_text("{}") - return {"status": "completed", "returncode": 0, "cleanup_verified": True} + return {"status": "completed", "returncode": 0, "cleanup_verified": task_cleanup} - monkeypatch.setattr("inferrsi.apps.search_benchmarks.task_protocol.run", protocol) - monkeypatch.setattr("inferrsi.apps.search_benchmarks.execute_trial", execute) + monkeypatch.setattr("inferrsi.apps.workflows.search_benchmarks.task_protocol.run", protocol) + monkeypatch.setattr("inferrsi.apps.workflows.search_benchmarks.execute_trial", execute) result = evaluator.evaluate(source, "evaluation", tmp_path / "output", time.time() + 10) assert result["verdict"] == "INCONCLUSIVE" and result["objective"] is None - assert result["cleanup_verified"] is (protocol_cleanup is True) + assert result["cleanup_verified"] is ( + protocol_cleanup is True and (stage == "arguments" or task_cleanup is True) + ) assert (result["execution_sha256"] is None) == (stage == "arguments") @@ -256,11 +263,37 @@ def compare(*args, split, **kwargs): assert control.read("campaign") == before and not accesses -def test_skill_comparator_preparation_does_not_access_holdout(tmp_path): +@pytest.mark.parametrize("indexed", [False, True]) +def test_skill_comparator_preparation_does_not_access_holdout(tmp_path, indexed): from tests.fixtures.harness_improvement_demo import configuration + from tests.fixtures.skill_eval_compare_support import git from tools.speculative_skill import Comparator config, repo = configuration(tmp_path) + prefix = "selected" + if indexed: + # Match the categorized, single-file metadata catalog introduced in #516. + prefix = "playbooks/testing/selected" + library = repo / "library" + target = library / prefix + target.parent.mkdir(parents=True) + (library / "selected").rename(target) + (target / "metadata.json").unlink() + metadata = {"references": ["../../../shared/notes.md"]} + skill = target / "SKILL.md" + skill.write_text( + "---\nname: selected\ndescription: indexed snapshot fixture\n" + "metadata:\n inferrsi: |\n " + json.dumps(metadata) + "\n---\nOld body\n", + encoding="utf-8", + ) + (library / "skill-index.json").write_text(json.dumps({ + "schema_version": 1, + "skills": {"selected": {"path": prefix, "backend": "common"}}, + }), encoding="utf-8") + git(repo, "add", "library") + git(repo, "commit", "--quiet", "-m", "Fixture indexed catalog") + config["baseline"] = git(repo, "rev-parse", "HEAD") + config["allowed_paths"] = [f"{prefix}/SKILL.md", f"{prefix}/resource.txt"] comparison = {k: config[k] for k in ("skill", "skill_root", "model", "limits")} comparison.update( mode="update", @@ -286,10 +319,19 @@ def descriptor(split): tmp_path / "baseline-snapshot", ) exported = bound.export_root(tmp_path / "candidate") - assert (exported / "selected/SKILL.md").is_file() + assert (exported / prefix / "SKILL.md").read_bytes() == bound.original[f"{prefix}/SKILL.md"] + assert (exported / "shared/notes.md").is_file() + assert "skill-index.json" not in bound.original assert not (tmp_path / "holdout.json").exists() assert not (tmp_path / "development.json").exists() # The prepared adapter still fails before provider access if its split is missing. + # Materializing an edit before that failure also exercises the indexed reader. + (exported / prefix / "SKILL.md").write_bytes( + bound.original[f"{prefix}/SKILL.md"].replace(b"Old body", b"Edited body") + ) + head_before = git(repo, "rev-parse", "HEAD") with pytest.raises(FileNotFoundError): bound.run(exported, tmp_path / "run", time.time() + 1, split="development") + assert git(repo, "rev-parse", "HEAD") == head_before + assert git(repo, "status", "--porcelain") == "" json.dumps(bound.config) diff --git a/tests/test_search_roles.py b/tests/test_search_roles.py index 1ff9219a5..508444af3 100644 --- a/tests/test_search_roles.py +++ b/tests/test_search_roles.py @@ -4,7 +4,13 @@ import pytest -from inferrsi.model.search_roles import Snapshot, actor_gradient, choose, distribution, update +from inferrsi.apps.workflows.search_roles import ( + Snapshot, + actor_gradient, + choose, + distribution, + update, +) def test_actor_gradient_matches_finite_difference_with_exploration_and_kl(): diff --git a/tests/test_vats_runtime.py b/tests/test_vats_runtime.py index fc3cd417d..8f7aa0681 100644 --- a/tests/test_vats_runtime.py +++ b/tests/test_vats_runtime.py @@ -5,6 +5,7 @@ import pytest +from inferrsi.apps.workflows.search_roles import Snapshot from inferrsi.harness import search_tree as tree from inferrsi.harness.artifact_primitives import digest from inferrsi.harness.gvo import examples, train_group @@ -14,7 +15,6 @@ from inferrsi.harness.search_records import decode from inferrsi.harness.vats import Search, Settings from inferrsi.harness.work_control import WorkControl -from inferrsi.model.search_roles import Snapshot from tests.fixtures.speculative_search import contract @@ -181,7 +181,10 @@ def crash(reason): @pytest.mark.parametrize("kind", ["draft", "verify"]) -def test_expired_launch_recovers_unstarted_block_and_finishes_with_root(runtime, monkeypatch, kind): +@pytest.mark.parametrize("expire_campaign", [False, True]) +def test_expired_launch_recovers_unstarted_block_and_resumes( + runtime, monkeypatch, kind, expire_campaign +): control, adapter, snapshot, settings, root = runtime search = Search(control, "campaign", root.contract.search_id, snapshot, settings, adapter) search.start(root, seed=2, collect=False) @@ -190,10 +193,13 @@ def test_expired_launch_recovers_unstarted_block_and_finishes_with_root(runtime, def expire(campaign, operation): doc = control.read(campaign) - if doc["operations"][operation]["category"] == kind: + if not interrupted and doc["operations"][operation]["category"] == kind: interrupted.update(operation=operation, spent_ms=doc["spent_ms"]) + deadline = ( + doc["deadline"] if expire_campaign else doc["operations"][operation]["deadline"] + ) monkeypatch.setattr( - "inferrsi.harness.work_control.time.time", lambda: doc["deadline"] + 1 + "inferrsi.harness.work_control.time.time", lambda: deadline + 0.001 ) return launch(campaign, operation) @@ -216,20 +222,92 @@ def no_adapter_recovery(*args): after = reopened.read("campaign") resumed.recover() assert reopened.read("campaign") == after - record = resumed.run() - assert record["status"] == "complete" and record["stop_reason"] == "limit" - assert record["selected"] == root.root_id - assert record["pending"] is None and record["active_action"] is None - assert record["block_stop_reason"] == "launch_deadline" - assert reopened.read("campaign")["spent_ms"] == interrupted["spent_ms"] if kind == "verify": - state = decode(record["tree"]) + state = decode(after["state"]["searches"][root.contract.search_id]["tree"]) receipt = next(r for r in state.receipts if r.attempt.attempt_id == operation) assert receipt.verdict.value == "INCONCLUSIVE" and receipt.objective is None assert receipt.attempt.check_id in tree.missing_checks(state, receipt.attempt.candidate_id) + record = resumed.run() + assert record["status"] == "complete" + if expire_campaign: + assert record["stop_reason"] == "limit" and record["selected"] == root.root_id + assert reopened.read("campaign")["spent_ms"] == interrupted["spent_ms"] + else: + assert len(reopened.read("campaign")["operations"]) > len(after["operations"]) + assert reopened.read("campaign")["spent_ms"] > interrupted["spent_ms"] + assert record["pending"] is None and record["active_action"] is None + assert record["block_stop_reason"] == "launch_deadline" + with reopened.store._connection() as db: + lease = db.execute("SELECT 1 FROM work_domains WHERE operation=?", (operation,)).fetchone() + assert lease is None + + +@pytest.mark.parametrize("kind", ["draft", "verify"]) +@pytest.mark.parametrize("cleanup_before_reopen", [False, True]) +def test_overrun_cleanup_recovery_archives_work_without_adopting_output( + runtime, monkeypatch, kind, cleanup_before_reopen +): + control, adapter, snapshot, settings, root = runtime + search = Search(control, "campaign", root.contract.search_id, snapshot, settings, adapter) + search.start(root, seed=2, collect=False) + settle = control.settle + interrupted = {} + + def overrun(campaign, operation, receipt, **kwargs): + doc = control.read(campaign) + op = doc["operations"][operation] + if op["category"] == kind: + receipt = {**receipt, "elapsed_ms": op["cap_ms"] + 1, "cleanup_verified": False} + interrupted.update(operation=operation, receipt=receipt) + kwargs.update(actual_ms=receipt["elapsed_ms"], cleanup_verified=False) + return settle(campaign, operation, receipt, **kwargs) + + monkeypatch.setattr(control, "settle", overrun) + with pytest.raises(ValueError, match="cleanup remains unknown"): + search.run() + before, record, current = search.read() + operation = interrupted["operation"] + receipt = {**interrupted["receipt"], "cleanup_verified": True} + assert before["blocked"] and before["operations"][operation]["status"] == "unknown" + assert record["pending"]["operation"] == operation + recoveries = [] + + def recover(*args): + recoveries.append(args) + assert not cleanup_before_reopen + return receipt + + monkeypatch.setattr(adapter, "recover", recover) + if cleanup_before_reopen: + settle("campaign", operation, receipt, actual_ms=receipt["elapsed_ms"], + cleanup_verified=True, + transition=lambda state: pytest.fail("Blocked work must not adopt output")) + reopened = WorkControl(RunStore(control.store.root, control.store.namespace)) + resumed = Search(reopened, "campaign", root.contract.search_id, snapshot, settings, adapter) + resumed.recover() + after, archived, recovered = resumed.read() + assert after["blocked"] and after["operations"][operation]["status"] == "settled" + assert after["spent_ms"] == before["spent_ms"] + receipt["elapsed_ms"] + assert archived["spent_ms"] == record["spent_ms"] + receipt["elapsed_ms"] + assert archived["pending"] is None and archived["active_action"] is None + assert archived["discarded_operations"] == [operation] + assert archived["selected"] == record["selected"] + assert recovered.candidates == current.candidates + assert tree.accept(recovered) == tree.accept(current) + if kind == "verify": + rejected = next(r for r in recovered.receipts if r.attempt.attempt_id == operation) + assert rejected.verdict.value == "INCONCLUSIVE" and rejected.objective is None + assert len(recoveries) == (0 if cleanup_before_reopen else 1) with reopened.store._connection() as db: lease = db.execute("SELECT 1 FROM work_domains WHERE operation=?", (operation,)).fetchone() assert lease is None + resumed.recover() + with pytest.raises(ValueError, match="Campaign blocked"): + resumed.run() + assert reopened.read("campaign") == after + with pytest.raises(ValueError, match="blocked"): + reopened.claim("campaign", after["revision"], "another", category="verify", cap_ms=1, + domains=("other-device",), binding={}) @pytest.mark.parametrize("kind", ["draft", "verify"]) diff --git a/tests/test_work_control.py b/tests/test_work_control.py index f5d6f74e4..417caaeeb 100644 --- a/tests/test_work_control.py +++ b/tests/test_work_control.py @@ -114,6 +114,35 @@ def update(state): claim(control, "next") +def test_blocked_reconciliation_requires_settlement_and_preserves_accounting(control): + claim(control) + control.launch("campaign", "work") + control.settle("campaign", "work", {}, actual_ms=950, cleanup_verified=False) + before = control.read("campaign") + with pytest.raises(ValueError, match="conclusively settled"): + control.reconcile_blocked("campaign", before["revision"], "work", lambda state: {}) + assert control.read("campaign") == before + control.settle("campaign", "work", {}, actual_ms=950, cleanup_verified=True) + settled = control.read("campaign") + with pytest.raises(ValueError, match="Stale"): + control.reconcile_blocked("campaign", before["revision"], "work", lambda state: {}) + + def fail(state): + state["incumbent"] = "changed" + raise ValueError("invalid recovery") + + with pytest.raises(ValueError, match="invalid recovery"): + control.reconcile_blocked("campaign", settled["revision"], "work", fail) + assert control.read("campaign") == settled + after = control.reconcile_blocked( + "campaign", settled["revision"], "work", lambda state: state | {"archived": "work"} + ) + assert after == settled | { + "revision": settled["revision"] + 1, + "state": settled["state"] | {"archived": "work"}, + } + + def test_expired_claim_cannot_start_and_does_not_charge_unstarted_work(control, monkeypatch): claim(control) monkeypatch.setattr("inferrsi.harness.work_control.time.time", lambda: 1e12) diff --git a/tools/speculative_skill.py b/tools/speculative_skill.py index 09cb18ed1..1c3c0e32e 100644 --- a/tools/speculative_skill.py +++ b/tools/speculative_skill.py @@ -16,7 +16,7 @@ from inferrsi.harness.artifact_primitives import digest, sha256 from inferrsi.harness.atomic_json import atomic_write_json from inferrsi.harness.optimization_session import runtime_environment -from inferrsi.harness.scenarios.external import execute_trial +from inferrsi.harness.trials.external import execute_trial from inferrsi.knowledge.knowledge_operations import KnowledgeReader from tools import skill_eval_compare as compare from tools.skill_eval_improve import materialize From b0e94935902af367a784b2dff34d1f9719a82bbe Mon Sep 17 00:00:00 2001 From: amy-why-3459 Date: Tue, 24 Nov 2026 11:14:24 +0800 Subject: [PATCH 10/16] cli: defer search runtime loading until dispatch Register the search arguments in the CLI parser so help and unrelated commands do not import RunStore or VATS. Agent-assisted-by: Codex (implementation and validation) Signed-off-by: amy-why-3459 --- src/inferrsi/apps/cli/arguments.py | 55 ++++++++++++++++++- .../apps/workflows/speculative_search.py | 53 ------------------ 2 files changed, 52 insertions(+), 56 deletions(-) diff --git a/src/inferrsi/apps/cli/arguments.py b/src/inferrsi/apps/cli/arguments.py index 80102f97e..221bc70ab 100644 --- a/src/inferrsi/apps/cli/arguments.py +++ b/src/inferrsi/apps/cli/arguments.py @@ -107,6 +107,57 @@ def _common(parser: argparse.ArgumentParser) -> None: ) +def _search_arguments(commands) -> None: + search = commands.add_parser("search", help="Bounded SpecRSI search and role training") + actions = search.add_subparsers(dest="search_action", required=True) + run = actions.add_parser("engine", help="Run a supplied packaged Engine protocol") + run.add_argument("--session", type=Path, required=True) + run.add_argument("--store", type=Path, required=True, help="Shared resource/holdout RunStore") + run.add_argument("--campaign", required=True, help="Unique campaign ID in the shared store") + run.add_argument("--task", required=True) + run.add_argument( + "--edit", + nargs=4, + action="append", + required=True, + metavar=("TOKEN", "FILE", "BEFORE", "AFTER"), + ) + run.add_argument( + "--checkpoint", + type=Path, + required=True, + help="Initial trainable finite-edit role snapshot with provenance", + ) + run.add_argument("--domain", required=True, help="Caller-owned exclusive measurement domain") + run.add_argument("--max-seconds", type=int, required=True) + run.add_argument("--evaluation-seconds", type=int, required=True) + run.add_argument("--final-reserve-seconds", type=int, required=True) + run.add_argument("--search-seconds", type=int, required=True) + run.add_argument("--preparation-seconds", type=int, required=True) + run.add_argument("--draft-seconds", type=int, required=True) + run.add_argument("--training-seconds", type=int, required=True) + run.add_argument("--candidates", type=int, required=True) + run.add_argument("--depth", type=int, required=True) + run.add_argument("--attempts", type=int, required=True) + run.add_argument("--steps", type=int, required=True) + run.add_argument("--plans", type=int, required=True) + run.add_argument("--targets", type=int, required=True) + run.add_argument("--groups", type=int, required=True, help="Independent searches per round") + run.add_argument("--rounds", type=int, required=True) + run.add_argument("--learning-rate", type=float, required=True) + run.add_argument("--seed", type=int, default=0) + run.add_argument("--mode", choices=("reset", "carry"), default="reset") + run.add_argument("--final-scope", required=True) + run.add_argument("--minimum-final-score", type=float, required=True) + run.add_argument("--python", default=sys.executable) + run.add_argument("arguments", nargs="*", help="Task protocol arguments after --") + for action in ("status", "export"): + read = actions.add_parser(action) + read.add_argument("--session", type=Path, required=True) + if action == "export": + read.add_argument("--output", type=Path, required=True) + + def _optimization_actions(stages, *, project=False): start = stages.add_parser("start", help="Freeze explicit task conditions and search budget") start.set_defaults(session_action="start", project_task=project) @@ -243,9 +294,7 @@ def parser(*, parser_class=argparse.ArgumentParser) -> argparse.ArgumentParser: from .evaluate import add_parser as add_evaluate_parser add_evaluate_parser(commands) - from ..workflows.speculative_search import add_arguments as search_arguments - - search_arguments(commands) + _search_arguments(commands) mindie = commands.add_parser("mindie", help="Use project-approved, local MindIE knowledge") mindie_actions = mindie.add_subparsers(dest="mindie_action", required=True) enable = mindie_actions.add_parser("enable", help="Approve a MindIE domain for this checkout") diff --git a/src/inferrsi/apps/workflows/speculative_search.py b/src/inferrsi/apps/workflows/speculative_search.py index 5f5fbca9e..359546e1c 100644 --- a/src/inferrsi/apps/workflows/speculative_search.py +++ b/src/inferrsi/apps/workflows/speculative_search.py @@ -1,10 +1,8 @@ """CLI composition for the supported packaged Engine VATS/GVO profile.""" import json -import sys import time from dataclasses import asdict, replace -from pathlib import Path from inferrsi.apps.workflows.search_roles import Snapshot from inferrsi.harness import candidates, search_tree @@ -18,57 +16,6 @@ from inferrsi.harness.work_control import WorkControl -def add_arguments(commands): - search = commands.add_parser("search", help="Bounded SpecRSI search and role training") - actions = search.add_subparsers(dest="search_action", required=True) - run = actions.add_parser("engine", help="Run a supplied packaged Engine protocol") - run.add_argument("--session", type=Path, required=True) - run.add_argument("--store", type=Path, required=True, help="Shared resource/holdout RunStore") - run.add_argument("--campaign", required=True, help="Unique campaign ID in the shared store") - run.add_argument("--task", required=True) - run.add_argument( - "--edit", - nargs=4, - action="append", - required=True, - metavar=("TOKEN", "FILE", "BEFORE", "AFTER"), - ) - run.add_argument( - "--checkpoint", - type=Path, - required=True, - help="Initial trainable finite-edit role snapshot with provenance", - ) - run.add_argument("--domain", required=True, help="Caller-owned exclusive measurement domain") - run.add_argument("--max-seconds", type=int, required=True) - run.add_argument("--evaluation-seconds", type=int, required=True) - run.add_argument("--final-reserve-seconds", type=int, required=True) - run.add_argument("--search-seconds", type=int, required=True) - run.add_argument("--preparation-seconds", type=int, required=True) - run.add_argument("--draft-seconds", type=int, required=True) - run.add_argument("--training-seconds", type=int, required=True) - run.add_argument("--candidates", type=int, required=True) - run.add_argument("--depth", type=int, required=True) - run.add_argument("--attempts", type=int, required=True) - run.add_argument("--steps", type=int, required=True) - run.add_argument("--plans", type=int, required=True) - run.add_argument("--targets", type=int, required=True) - run.add_argument("--groups", type=int, required=True, help="Independent searches per round") - run.add_argument("--rounds", type=int, required=True) - run.add_argument("--learning-rate", type=float, required=True) - run.add_argument("--seed", type=int, default=0) - run.add_argument("--mode", choices=("reset", "carry"), default="reset") - run.add_argument("--final-scope", required=True) - run.add_argument("--minimum-final-score", type=float, required=True) - run.add_argument("--python", default=sys.executable) - run.add_argument("arguments", nargs="*", help="Task protocol arguments after --") - for action in ("status", "export"): - read = actions.add_parser(action) - read.add_argument("--session", type=Path, required=True) - if action == "export": - read.add_argument("--output", type=Path, required=True) - - def command(args): root = args.session.resolve() if args.search_action != "engine": From edf25fadb540711a64fe51c69a3775fb49bce327 Mon Sep 17 00:00:00 2001 From: amy-why-3459 Date: Tue, 24 Nov 2026 12:02:23 +0800 Subject: [PATCH 11/16] docs: explain GVO and measure its incremental effect on Ascend Retain the matched serial, frozen VATS and trained VATS pilot, including all independent finals, one-time collection cost and the failed device initialization. Record the frozen replay inputs and an offline assessment check. Agent-assisted-by: Codex (experiment, documentation and validation) Signed-off-by: amy-why-3459 --- docs/speculative-search.md | 160 +++++++++++-- .../qwen-vats-gvo-910b1-20261124/README.md | 30 +++ .../patch/README.md | 9 + .../plan/edits.json | 14 ++ .../plan/plan.json | 32 +++ .../plan/retained-sha256.json | 13 ++ .../qwen-vats-gvo-910b1-20261124/plan/run.py | 213 ++++++++++++++++++ .../plan/runspec.json | 28 +++ .../results/local-archive.json | 7 + .../results/summary.json | 197 ++++++++++++++++ .../results/trials.jsonl | 38 ++++ .../validator/README.md | 23 ++ .../validator/check.py | 169 ++++++++++++++ tests/repository/test_evidence_inventory.py | 4 +- tools/evidence_inventory.json | 5 + 15 files changed, 917 insertions(+), 25 deletions(-) create mode 100644 evidence/qwen-vats-gvo-910b1-20261124/README.md create mode 100644 evidence/qwen-vats-gvo-910b1-20261124/patch/README.md create mode 100644 evidence/qwen-vats-gvo-910b1-20261124/plan/edits.json create mode 100644 evidence/qwen-vats-gvo-910b1-20261124/plan/plan.json create mode 100644 evidence/qwen-vats-gvo-910b1-20261124/plan/retained-sha256.json create mode 100644 evidence/qwen-vats-gvo-910b1-20261124/plan/run.py create mode 100644 evidence/qwen-vats-gvo-910b1-20261124/plan/runspec.json create mode 100644 evidence/qwen-vats-gvo-910b1-20261124/results/local-archive.json create mode 100644 evidence/qwen-vats-gvo-910b1-20261124/results/summary.json create mode 100644 evidence/qwen-vats-gvo-910b1-20261124/results/trials.jsonl create mode 100644 evidence/qwen-vats-gvo-910b1-20261124/validator/README.md create mode 100644 evidence/qwen-vats-gvo-910b1-20261124/validator/check.py diff --git a/docs/speculative-search.md b/docs/speculative-search.md index f193cd678..c3e0fdf06 100644 --- a/docs/speculative-search.md +++ b/docs/speculative-search.md @@ -103,25 +103,57 @@ are explicit ablation switches; they are not claims of comparative efficiency. ## GVO and paid continuations -`collect_group` requires at least two independent complete searches with the same -root, contract, budget, snapshot and adapter. Each owns a tree and seed. The -campaign must be able to provide equal declared budgets before collection. -Leaves from one tree are not independent group members. - -`gvo.examples` computes whole-search returns and leave-one-out advantages. -Natural sampled Drafter/Policy choices supply the clipped PPO-style objective -with KL regularization against a frozen reference. Scorer targets use actual -remaining gains and future costs from each paid decision boundary. Current -preparation is sunk for that suffix but stays in total campaign cost. Missing -labels/cost observations are never zero-filled. Penalties and training settings -are explicit frozen conditions. - -`train_group` checks retained settled records and replayed behavior probabilities, -updates separate actual parameter arrays and commits checkpoint plus training -receipt. `learn` supports Reset/Carry and reuses completed updates on resume; -unknown training cannot be relaunched. An initial checkpoint with explicit -provenance is required. This profile provides neither a pretrained model nor a -supervised warmup dataset. +VATS spends a search budget using a frozen Drafter, Policy and Scorer. GVO +updates those three roles **between** collections. The Solver's feasibility +rules, verification contract and real evaluator stay fixed across the update. +The formal objectives are in [SpecRSI §3.4](https://arxiv.org/html/2610.16491v2#S3.SS4). + +1. **Collect a group.** Start at least two independent VATS searches from the + same root, initial evidence and history, role checkpoint, adapter and budget. + Each search has a separate tree and random seed. The controller checks that + the campaign can fund all declared search budgets before collection. Record the legal choices, + sampled actions, their probabilities, the paid work and its verified outcomes. +2. **Give the Drafter and Policy whole-search credit.** Let `F` be the best + observed gain among candidates that satisfy every required check. For search + `i`, compute the return and its leave-one-out advantage: + + ```text + R_i = F(end_i) - F(start_i) - expense_weight * spent_seconds_i + - elapsed_weight * wall_seconds_i + A_i = R_i - mean(R_j for all other searches j in this group) + ``` + + For returns of 8, 5 and 2, the advantages are +4.5, 0 and −4.5. Every + naturally sampled edit or plan choice in a search receives that search's + advantage. This gives an early edit credit when a later descendant earns a + verified gain. Replay the recorded action probabilities and apply the + PPO-style clipped probability ratio, with KL regularization against the + fixed initial actor. The ratio is the current probability of the sampled + choice divided by its recorded collection probability, under the same legal + menu and sampling settings. With the default single update per group this + ratio starts at one, so clipping is inactive on that gradient step. + Deployment trajectories are excluded. Both cost + weights default to zero. Work still counts against the hard budget. +3. **Train the Scorer on realized continuation returns.** At each paid decision + boundary fit the predicted plan value to the gain still to come, less the remaining + measured work and time penalties. If the best verified gain is 3 at that + boundary and ends at 8, the unpenalized target is 5. Preparation already + performed stays in the whole-search cost. Missing results supply no target. When two alternative first + actions have both been executed from a saved state, the difference between + their realized returns also supervises the difference in predicted values. +4. **Save the update and collect fresh evidence.** `train_group` verifies the + records, updates the three sets of weights and settles the training receipt + and checkpoint together. A resumed run reuses a settled update and blocks + a second training launch if the first outcome is unknown. Reset keeps the + original root for the next group. + Carry starts the next group from the best fully verified candidate and its + receipts. Final measurements are reserved for independently retesting the + selected candidate. + +The [collection and target code](../src/inferrsi/harness/gvo.py) and the +[CPU role update](../src/inferrsi/apps/workflows/search_roles.py) implement these +steps with finite edit operators, a linear Policy and a linear Scorer. The +caller supplies the initial checkpoint and its provenance. `search_continuations.continue_from` copies a retained paid state, charges the copy, forces one legal first action and continues the frozen policy. The entire @@ -276,10 +308,92 @@ refinement, but that does not add the missing semantic or relational inputs. Moving toward a reference reproduction requires explicit role/feature and menu contracts, a qualified generative/trainable adapter with warm-up, lifecycle cost -accounting, and matched frozen-versus-trained search experiments on real tasks. +accounting, and replicated frozen-versus-trained comparisons across tasks and +independent training checkpoints. The existing recovery, evidence and holdout controls remain useful foundations; passing them does not establish search efficacy or reproduce paper results. +## Measuring the contribution of GVO + +Compare three deployment arms on the same task. A fixed-order serial control +measures a simple alternative search order. VATS with the initial checkpoint +measures what the search can find before training. VATS with the trained +checkpoint measures what changes after GVO. To isolate learning, compare the +last two arms at the same deployment seed, candidate budget and evaluation +protocol. Use the independently retested final candidate as the outcome. +Weight changes and development scores show what happened during training and +selection. The cost of obtaining a checkpoint includes every collection +search, even one that returned the root without an improvement. + +The [Qwen diagnostic plan](../evidence/qwen-vats-gvo-910b1-20261124/plan/plan.json) fixes one +worker, the two [allowed edits](../evidence/qwen-vats-gvo-910b1-20261124/plan/edits.json), the +initial weights, limits, seeds and arm order. Both edits reuse existing +intermediates in the denoiser. One hoists the invariant text projection out of +the denoising loop. The other hoists shared scale and gate computations out of +the transformer-block loop. The serial control tests the first edit and then +the combination. VATS can also test the second edit independently, repeat an +edit choice on another branch, or stop early. Its five primitive attempts +include the imported root check, leaving room to draft two candidates and +verify both. The root qualification is physically executed once and shared. +Each arm can spend up to two new candidate checks and receives a fresh final. + +The [experiment driver](../evidence/qwen-vats-gvo-910b1-20261124/plan/run.py) calls the packaged +Engine evaluator for all three arms and uses the PR's unmodified `Search`, +`learn` and `finalize` for VATS. The serial control evaluates the declared +order directly through the same Engine evaluator. To repeat from the checkout +in an environment with one allocated Ascend NPU, the frozen runtime dependencies, +and an unused output path, run the driver from the repository root. + +```bash +PYTHONPATH=src python evidence/qwen-vats-gvo-910b1-20261124/plan/run.py ./qwen-gvo-run +``` + +The output contains the shared RunStore, checkpoints, all task snapshots, +timings and per-step numerical checks. Arm order rotates across three blocks +so each arm runs first, second and last once. All three blocks +reuse one trained checkpoint. Their random seeds vary the search on the same +workload. Training variability and transfer to other tasks require additional +independent runs. + +### Ascend 910B1 diagnostic results + +On 2026-11-24, all nine deployment runs selected the **same candidate bytes** +and each arm verified two candidates per deployment. Frozen and trained VATS +both drafted the two edits in the same order, then verified both candidates in +one bundle. The table reports independent-final speedups against the original +denoiser. The last column divides the trained score by the frozen score within +the same deployment block. + +| Deployment seed | Fixed serial | Frozen VATS | VATS + GVO | Trained vs. frozen | +| ---: | ---: | ---: | ---: | ---: | +| 101 | 1.1496× | 1.1562× | 1.1656× | +0.81% | +| 202 | 1.1389× | 1.1457× | 1.1546× | +0.78% | +| 303 | 1.1542× | 1.1557× | 1.1434× | −1.07% | +| Geometric mean | 1.1475× | 1.1525× | 1.1545× | +0.17% | + +The paired score difference was **+0.17%** for trained versus frozen VATS +and **+0.61%** for trained VATS versus the fixed serial control. Repeated +measurement of the same code produced all three values in each row. +The two edits delivered the observed denoiser speedup. This pilot found no +additional candidate-quality or evaluation-count benefit from GVO. +The default zero refinement queries also leave the Scorer outside deployment +action selection in this configuration. + +One shared root qualification took 38.34 s. The six independent training +searches and one update took **297.99 s**, including nine candidate evaluations. +All three role weight vectors changed. Mean online time, including independent +final, was 97.35 s for serial, 96.42 s for frozen VATS and 95.30 s for trained +VATS. Over three deployments, collection, training and trained deployment took +**583.90 s**, compared with **289.26 s** for frozen deployment. Both totals +exclude the common root, queue, container startup and setup. The small online +timing difference does not establish a repeatable saving or break-even point. + +The verifier replayed all 37 completed five-case task evaluations (1,998 +compute calls) and the earlier device-initialization failure. Overall search +efficacy remains **INCONCLUSIVE**. The [evidence bundle](../evidence/qwen-vats-gvo-910b1-20261124/README.md) +links the frozen inputs, checkpoint, per-trial index, archive availability and +raw-data verification instructions. + ## Validation and issue completion ```bash @@ -300,11 +414,11 @@ they are not real task/provider efficacy evidence. | AC-01/02 | Support/permission boundaries and immutable trees | Production role/environment qualification | | AC-03/05 | Durable transitions, late receipts, invalidation, bounded synchronous VATS | Production adapter fault/race evidence | | AC-04 | Exact planner, independent oracle and Scorer refinement | Scaling and real complementary-check evidence | -| AC-06 | Independent groups, actor/value updates, paid continuations, checkpoints, Reset/Carry | Actual task training results and role qualification | +| AC-06 | Independent groups, actor/value updates, paid continuations, checkpoints, Reset/Carry | Replicated learning across checkpoints and tasks, and role qualification | | AC-07 | Shared local claims, elapsed-time caps and cleanup-aware recovery | Host callback preemption, provider/device total accounting and production resource integration | | AC-08 | One-time final scope and task promotion gate | Real independent final evidence and separate default-improver qualification | -| AC-09 | Packaged Engine evaluator and explicit Skill comparator bridge | Both complete real VATS/GVO/final pilots | -| AC-10 | Retained trajectories/costs and ablation switches | Serial/VATS, bundle, learning, Reset/Carry and worker experiments with full lifecycle costs | +| AC-09 | Packaged Engine evaluator and explicit Skill comparator bridge | Live Agent/Skill pilot and broader Engine coverage | +| AC-10 | Retained trajectories/costs and ablation switches | Replicated learning and bundle, Reset/Carry and worker comparisons with full lifecycle costs | The parent issue remains open. Software tests, CPU mathematical checks or an available adapter do not establish full acceptance, NPU speedup, provider quality, diff --git a/evidence/qwen-vats-gvo-910b1-20261124/README.md b/evidence/qwen-vats-gvo-910b1-20261124/README.md new file mode 100644 index 000000000..ddfacb539 --- /dev/null +++ b/evidence/qwen-vats-gvo-910b1-20261124/README.md @@ -0,0 +1,30 @@ +# Matched VATS and GVO deployment on one Ascend 910B1 + +This Engine diagnostic for [InferRSI #363](https://github.com/InferMatrixLab/InferRSI/issues/363) +compares a fixed-order serial control, frozen VATS and VATS after one GVO update +on the packaged Qwen-Image 2.1 denoiser. The underlying search and task code was +[f5b9a18](https://github.com/InferMatrixLab/InferRSI/tree/f5b9a1819d387e0a0a1a219da6dcc0f5504c4069). +The [design document](../../docs/speculative-search.md#measuring-the-contribution-of-gvo) +explains the comparison and reports the independent-final results. + +- [Frozen plan](plan/plan.json), [driver](plan/run.py) and [RunSpec](plan/runspec.json) identify the exact edits, weights, workload, ordered seeds, evaluation caps, runtime and task file hashes. +- [Summary](results/summary.json) contains every online result, the shared root and training durations, the before/after weights, group returns and paired aggregates. +- [Trial index](results/trials.jsonl) records every physical task evaluation, its selected source hash, verdict, task-result hash and path inside its attempt directory. +- [Local archive metadata](results/local-archive.json) gives the size and SHA-256 of the 725-file raw bundle retained in the shared experiment workspace. The public PR provides the compact inputs and results. +- [Candidate sources](patch/README.md) and [verification instructions](validator/README.md) explain how to regenerate the experiment and re-assess its retained measurements. + +The task uses random BF16 weights, five numerical cases, three latency cases and +fixed workload seeds. Each complete evaluation runs two paired AB/BA blocks +with two timed samples per block and one warmup per arm and case. The score +is the geometric mean of the three original-baseline/candidate median latency +ratios. The frozen quality gate checks trajectory steps and weight updates. + +Before successful qualification, the first container could not initialize its +allocated device. That root attempt produced an infrastructure failure with +verified cleanup and no score. All methods and input hashes were retained when +the job was relaunched with the known working allocator/container mapping. +The initial failure, queueing, container startup and experimental setup sit +outside the reported online arm times. Root acquisition and GVO collection plus +the gradient update have separate measured costs. Each online arm includes its +own independent final. The final repeats the same workload. Transfer to new prompts or tasks and +variability across independent training runs remain unmeasured. diff --git a/evidence/qwen-vats-gvo-910b1-20261124/patch/README.md b/evidence/qwen-vats-gvo-910b1-20261124/patch/README.md new file mode 100644 index 000000000..5b805e69c --- /dev/null +++ b/evidence/qwen-vats-gvo-910b1-20261124/patch/README.md @@ -0,0 +1,9 @@ +# Candidate sources + +All arms used the packaged Qwen denoiser baseline and the exact replacements in +[the frozen edit vocabulary](../plan/edits.json). The two allowed edits compose in +either order. The experiment driver checks the baseline SHA-256, materializes +the four possible source files and identifies each selected candidate by its +file hash. Infra, the evaluator and the search implementation were common to all +arms. The [measured runtime revision](https://github.com/InferMatrixLab/InferRSI/tree/f5b9a1819d387e0a0a1a219da6dcc0f5504c4069) +contains the original baseline and the full Engine evaluation protocol. diff --git a/evidence/qwen-vats-gvo-910b1-20261124/plan/edits.json b/evidence/qwen-vats-gvo-910b1-20261124/plan/edits.json new file mode 100644 index 000000000..8e4770d5f --- /dev/null +++ b/evidence/qwen-vats-gvo-910b1-20261124/plan/edits.json @@ -0,0 +1,14 @@ +[ + [ + "prefix-reuse", + "candidate.py", + " current = latents\n for step in range(sigmas.numel() - 1):\n # The original entry projects conditioning before sequence preparation,\n # even when decode subsequently uses the already cached prefix K/V.\n prefix = _rms(text, params[\"text_norm\"], eps, zero_center=True)\n prefix = F.linear(\n F.gelu(F.linear(prefix, params[\"text_in\"]), approximate=\"tanh\"), params[\"text_out\"]\n )\n", + " prefix = _rms(text, params[\"text_norm\"], eps, zero_center=True)\n prefix = F.linear(\n F.gelu(F.linear(prefix, params[\"text_in\"]), approximate=\"tanh\"), params[\"text_out\"]\n )\n current = latents\n for step in range(sigmas.numel() - 1):\n" + ], + [ + "modulation-reuse", + "candidate.py", + " for index, block in enumerate(params[\"blocks\"]):\n scale1, gate1, scale2, gate2 = modulation.chunk(4, dim=-1)\n normalized = F.layer_norm(value, (hidden,), eps=eps)\n normalized = normalized * (1 + _rows(scale1, target_rows, batch))\n qkv = F.linear(normalized, block[\"qkv\"]).view(batch, -1, 3, heads, head_dim)\n query, key, projected = qkv.unbind(2)\n query = _rotate(_rms(query, block[\"q_norm\"], eps), angles)\n key = _rotate(_rms(key, block[\"k_norm\"], eps), angles)\n if step == 0:\n cache.append((key[:, :prefix_tokens].clone(), projected[:, :prefix_tokens].clone()))\n else:\n key = torch.cat((cache[index][0], key), dim=1)\n projected = torch.cat((cache[index][1], projected), dim=1)\n attended = (\n F.scaled_dot_product_attention(\n query.transpose(1, 2),\n key.transpose(1, 2),\n projected.transpose(1, 2),\n attn_mask=attention_mask,\n )\n .transpose(1, 2)\n .reshape(batch, -1, hidden)\n )\n value = value + _rows(gate1, target_rows, batch).tanh() * F.linear(\n attended, block[\"attention_out\"]\n )\n normalized = F.layer_norm(value, (hidden,), eps=eps)\n normalized = normalized * (1 + _rows(scale2, target_rows, batch))\n # Separate gate/up projections are present in the original source.\n mlp = F.silu(F.linear(normalized, block[\"gate\"])) * F.linear(normalized, block[\"up\"])\n value = value + _rows(gate2, target_rows, batch).tanh() * F.linear(mlp, block[\"down\"])\n", + " scale1, gate1, scale2, gate2 = modulation.chunk(4, dim=-1)\n scale1_rows = 1 + _rows(scale1, target_rows, batch)\n scale2_rows = 1 + _rows(scale2, target_rows, batch)\n gate1_rows = _rows(gate1, target_rows, batch).tanh()\n gate2_rows = _rows(gate2, target_rows, batch).tanh()\n for index, block in enumerate(params[\"blocks\"]):\n normalized = F.layer_norm(value, (hidden,), eps=eps)\n normalized = normalized * scale1_rows\n qkv = F.linear(normalized, block[\"qkv\"]).view(batch, -1, 3, heads, head_dim)\n query, key, projected = qkv.unbind(2)\n query = _rotate(_rms(query, block[\"q_norm\"], eps), angles)\n key = _rotate(_rms(key, block[\"k_norm\"], eps), angles)\n if step == 0:\n cache.append((key[:, :prefix_tokens].clone(), projected[:, :prefix_tokens].clone()))\n else:\n key = torch.cat((cache[index][0], key), dim=1)\n projected = torch.cat((cache[index][1], projected), dim=1)\n attended = (\n F.scaled_dot_product_attention(\n query.transpose(1, 2),\n key.transpose(1, 2),\n projected.transpose(1, 2),\n attn_mask=attention_mask,\n )\n .transpose(1, 2)\n .reshape(batch, -1, hidden)\n )\n value = value + gate1_rows * F.linear(\n attended, block[\"attention_out\"]\n )\n normalized = F.layer_norm(value, (hidden,), eps=eps)\n normalized = normalized * scale2_rows\n # Separate gate/up projections are present in the original source.\n mlp = F.silu(F.linear(normalized, block[\"gate\"])) * F.linear(normalized, block[\"up\"])\n value = value + gate2_rows * F.linear(mlp, block[\"down\"])\n" + ] +] \ No newline at end of file diff --git a/evidence/qwen-vats-gvo-910b1-20261124/plan/plan.json b/evidence/qwen-vats-gvo-910b1-20261124/plan/plan.json new file mode 100644 index 000000000..37df03be5 --- /dev/null +++ b/evidence/qwen-vats-gvo-910b1-20261124/plan/plan.json @@ -0,0 +1,32 @@ +{ + "task": "qwen21-denoise-npu", + "sampling": ["--pairs", "2", "--samples", "2", "--warmups", "1"], + "source_baseline_sha256": "21c9ae912902a10fcbb39ecebbc49fa715d33ec9cad72296f651793cfe10a217", + "image": "sha256:5d81b480618d76accb1c80afd26b0c572b4ff585c1de1a3c37687faf3176d0f9", + "checkpoint": { + "drafter": [0, 0], + "policy": [1, 1, 2, 0, 0, 0, 0.5, 0.5], + "scorer": [0, 0, 0, 0, 0, 0, 0, 0], + "origin": "Fixed initial preference for executing and verifying edits; uniform edit prior." + }, + "settings": { + "max_steps": 5, "max_parents": 1, "draft_depth": 2, "max_targets": 2, + "max_jobs": 3, "max_plans": 16, "preparation_ms": 5000, "draft_ms": 5000, + "search_ms": 390000, "refine_queries": 0 + }, + "max_candidates_including_root": 3, + "max_depth": 2, + "max_attempts_including_root": 5, + "check_ms": 180000, + "train": {"rounds": 1, "group_size": 6, "training_cap_ms": 20000, "learning_rate": 1.0, "seed": 17, "mode": "reset"}, + "blocks": [ + [101, ["serial", "frozen", "trained"]], + [202, ["frozen", "trained", "serial"]], + [303, ["trained", "serial", "frozen"]] + ], + "serial_order": ["prefix-reuse", "prefix-reuse+modulation-reuse"], + "final_quality": "Full packaged numerical gate and verified cleanup; report every final score and target miss.", + "primary": "Geometric mean across blocks of trained/frozen independent-final speedup ratios. Also report trained/serial, frozen/serial, the paired ratios and mean online wall time.", + "cost": "One root qualification shared across all conditions. Charge the single training collection and update separately. Each online arm gets at most two candidate checks and a fresh final. No retuning or target-triggered stopping.", + "interpretation": "Three descriptive deployment blocks of one task and one training checkpoint. Incomplete pairs stay visible and cannot establish efficacy." +} diff --git a/evidence/qwen-vats-gvo-910b1-20261124/plan/retained-sha256.json b/evidence/qwen-vats-gvo-910b1-20261124/plan/retained-sha256.json new file mode 100644 index 000000000..ffc0ecafa --- /dev/null +++ b/evidence/qwen-vats-gvo-910b1-20261124/plan/retained-sha256.json @@ -0,0 +1,13 @@ +{ + "README.md": "fc04ec6e7fe6e516f0c4a4a271e3d60518932d437751b7f4e6b6dac177c444cb", + "patch/README.md": "e2a3af7bd373d9e1637265f3980fd272cbbc181d455d275794bf66f2cc6f8095", + "plan/edits.json": "2975fee42370debd80f627ecd58880e45280968f739ca3045c8b388bc9ab4cbb", + "plan/plan.json": "66344b1ca1522fd3b38966c243bbb6ec8eb8fecc203ccd171690463569632013", + "plan/run.py": "7088ba318ac4c9226feab41848963e6a91492d91879c42cca00034ff3178653b", + "plan/runspec.json": "f3f2ab71d227ee3e7d95233cc3d34e17ab41d086f519f558fb3aca5200515404", + "results/local-archive.json": "c5df5dc55feb202e53f28d5b6f4ba28913e893277d9deb5e783a472c686d9ae6", + "results/summary.json": "df3029cfcaacb91018b6c931017b059b99a80633116586f9446fe82e31986132", + "results/trials.jsonl": "6bd39719a5c0d794b1c5e04403b03789ae5ca42e0c9c74694200bdb6355a0756", + "validator/README.md": "d8f127ee5c960a09e4c96bf4e19f6748dbb940afb9852560cc04a596bc340106", + "validator/check.py": "18cfd2d9ddfd339f6450aa64f07ee1afc1120df2b5b4130de11bd868d55d92b4" +} diff --git a/evidence/qwen-vats-gvo-910b1-20261124/plan/run.py b/evidence/qwen-vats-gvo-910b1-20261124/plan/run.py new file mode 100644 index 000000000..29702610f --- /dev/null +++ b/evidence/qwen-vats-gvo-910b1-20261124/plan/run.py @@ -0,0 +1,213 @@ +"""Run the frozen single-worker Qwen diagnostic inside an allocated NPU environment.""" + +import argparse +import json +import math +import time +from dataclasses import asdict, replace +from pathlib import Path + +from inferrsi.apps.workflows.search_benchmarks import EngineEvaluator +from inferrsi.apps.workflows.search_roles import Snapshot +from inferrsi.harness import search_tree +from inferrsi.harness.artifact_primitives import digest, sha256 +from inferrsi.harness.atomic_json import atomic_write_json +from inferrsi.harness.run_store import RunStore +from inferrsi.harness.search_adapter import ArtifactAdapter, Edit +from inferrsi.harness.search_campaign import finalize, import_qualified_root, learn, qualify_root +from inferrsi.harness.search_records import decode +from inferrsi.harness.vats import Search, Settings +from inferrsi.harness.work_control import WorkControl + +HERE = Path(__file__).resolve().parent + + +def serial(evaluator, sources, plan, root_score, directory): + selected, best = "root", root_score + evaluations = [] + deadline = time.time() + plan["settings"]["search_ms"] / 1000 + for index, name in enumerate(plan["serial_order"]): + result = evaluator.evaluate( + sources / name, + "evaluation", + directory / str(index), + min(deadline, time.time() + plan["check_ms"] / 1000), + ) + evaluations.append({"candidate": name, **result}) + if result["cleanup_verified"] is not True or result["verdict"] == "INCONCLUSIVE": + raise ValueError(f"Incomplete serial measurement for {name}") + if result["verdict"] == "PASS" and result["objective"] > best: + selected, best = name, result["objective"] + atomic_write_json(directory / "selection.json", {"candidate": selected, "objective": best}) + final = evaluator.evaluate( + sources / selected, "evaluation", directory / "final", time.time() + plan["check_ms"] / 1000 + ) + return selected, best, final, evaluations + + +def campaign(control, name, spec, checkpoint, node, cap, reserve): + control.create( + name, + spec, + {"searches": {}, "checkpoint": checkpoint.document(), "stable": asdict(node)}, + cap_ms=cap, + final_reserve_ms=reserve, + deadline=time.time() + cap / 1000, + ) + + +def run(output): + output.mkdir(parents=True, exist_ok=False) + plan = json.loads((HERE / "plan.json").read_text()) + edits = [Edit(*args) for args in json.loads((HERE / "edits.json").read_text())] + frozen = Snapshot(**plan["checkpoint"]) + settings = Settings(**plan["settings"]) + cap = plan["check_ms"] + evaluator = EngineEvaluator(plan["task"], output / "frozen", arguments=plan["sampling"]) + adapter = ArtifactAdapter( + output / "artifacts", edits, {"evaluation": cap}, evaluator, domain="allocated-single-npu" + ) + control = WorkControl(RunStore(output / "store", "speculative")) + source = output / "sources" + baseline = evaluator.frozen / "baseline.py" + if sha256(baseline) != plan["source_baseline_sha256"]: + raise ValueError("Baseline differs from the frozen edit catalog") + contents = {"root": baseline.read_text()} + for edit in edits: + for name, text in list(contents.items()): + if text.count(edit.before) != 1: + raise ValueError("Expected one exact edit location") + child = edit.token if name == "root" else name + "+" + edit.token + contents[child] = text.replace(edit.before, edit.after, 1) + names = {} + for name, text in contents.items(): + path = source / name / "candidate.py" + path.parent.mkdir(parents=True) + path.write_text(text) + names[sha256(path)] = name + node = adapter.import_root(source / "root", snapshot=digest(frozen.document()), context={}) + spec = {"plan": plan, "edits_digest": sha256(HERE / "edits.json"), "adapter": adapter.identity} + context = search_tree.Context( + digest(spec), + digest(plan), + evaluator.identity, + adapter.domain, + plan["task"], + node.artifact_digest, + evaluator.identity, + ) + contract = search_tree.Contract( + "qualification", + context, + digest(frozen.document()), + ("evaluation",), + "evaluation", + "maximize", + plan["max_candidates_including_root"], + plan["max_depth"], + plan["max_attempts_including_root"], + ) + training = plan["train"] + train_cap = ( + 2 * cap + + training["rounds"] + * (settings.search_ms * training["group_size"] + training["training_cap_ms"]) + + 60000 + ) + campaign(control, "training", spec, frozen, node, train_cap, cap) + started = time.monotonic() + root = qualify_root(control, "training", adapter, node, contract) + root_seconds = time.monotonic() - started + root_score = search_tree.evidence(root, root.root_id)["evaluation"].objective + print("Root qualified. Starting frozen collection and GVO update.", flush=True) + started = time.monotonic() + trained = learn(control, "training", adapter, root, frozen, settings, **training) + train_seconds = time.monotonic() - started + training_state = control.read("training")["state"] + summary = { + "root_score": root_score, + "root_seconds": root_seconds, + "training_seconds": train_seconds, + "training": training_state["training"], + "training_candidate_checks": sum( + len(r["tree"]["attempts"]) - 1 for r in training_state["searches"].values() + ), + "initial": frozen.document(), + "trained": trained.document(), + "blocks": [], + } + atomic_write_json(output / "summary.json", summary) + for seed, arms in plan["blocks"]: + for arm in arms: + name = f"{arm}-{seed}" + print(f"Starting {name}", flush=True) + started = time.monotonic() + if arm == "serial": + selected, best, final, evaluations = serial( + evaluator, + source, + plan, + root_score, + output / name, + ) + candidates_checked = len(evaluations) + else: + snapshot = trained if arm == "trained" else frozen + campaign(control, name, spec, snapshot, node, settings.search_ms + cap + 30000, cap) + initial = import_qualified_root( + root, + replace( + contract, search_id=name, improver_snapshot=digest(snapshot.document()) + ), + ) + search = Search(control, name, name, snapshot, settings, adapter) + search.start(initial, seed=seed, collect=False) + record = search.run() + state = decode(record["tree"]) + winner = search_tree.accept(state) + selected = names[sha256(adapter.path(winner) / "candidate.py")] + best = search_tree.evidence(state, winner.candidate_id)["evaluation"].objective + candidates_checked = len(state.attempts) - 1 + final_record = finalize( + control, + name, + adapter, + replace(winner, parent_id=None), + holdout_scope=name, + accept_result=lambda _: True, + ) + final = final_record["results"][0] + if final["verdict"] != "PASS" or final["cleanup_verified"] is not True: + raise ValueError(f"Incomplete final for {name}") + row = { + "seed": seed, + "arm": arm, + "selected": selected, + "search_score": best, + "candidate_checks": candidates_checked, + "final_score": final["objective"], + "online_seconds": time.monotonic() - started, + } + summary["blocks"].append(row) + atomic_write_json(output / "summary.json", summary) + print(json.dumps(row), flush=True) + for numerator, denominator in ( + ("trained", "frozen"), + ("trained", "serial"), + ("frozen", "serial"), + ): + rows = {(r["seed"], r["arm"]): r["final_score"] for r in summary["blocks"]} + ratios = [rows[seed, numerator] / rows[seed, denominator] for seed, _ in plan["blocks"]] + summary[f"{numerator}_over_{denominator}"] = { + "paired_ratios": ratios, + "geomean": math.exp(sum(map(math.log, ratios)) / len(ratios)), + } + atomic_write_json(output / "summary.json", summary) + + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument( + "output", type=Path, help="Fresh directory for all receipts and measurements" + ) + run(parser.parse_args().output.resolve()) diff --git a/evidence/qwen-vats-gvo-910b1-20261124/plan/runspec.json b/evidence/qwen-vats-gvo-910b1-20261124/plan/runspec.json new file mode 100644 index 000000000..f72aa9f4c --- /dev/null +++ b/evidence/qwen-vats-gvo-910b1-20261124/plan/runspec.json @@ -0,0 +1,28 @@ +{ + "governing_issue": "https://github.com/InferMatrixLab/InferRSI/issues/363", + "source_revision": "f5b9a1819d387e0a0a1a219da6dcc0f5504c4069", + "device": "One allocator-reserved Ascend 910B1", + "driver": "25.5.0", + "torch": "2.10.0+cpu", + "torch_npu": "2.10.0.post7", + "dtype": "BF16", + "hf32": false, + "role": "single-worker Engine search on scaled denoiser with random weights", + "input_sha256": { + "plan/plan.json": "66344b1ca1522fd3b38966c243bbb6ec8eb8fecc203ccd171690463569632013", + "plan/edits.json": "2975fee42370debd80f627ecd58880e45280968f739ca3045c8b388bc9ab4cbb", + "plan/run.py": "7088ba318ac4c9226feab41848963e6a91492d91879c42cca00034ff3178653b" + }, + "packaged_task_sha256": { + "LICENSE": "50e6751797c50dedd75ef1b8a0d9e42f5f8472e9fbce91f34718e9f97b0c780a", + "NOTICE.md": "8b91807a93cd3839d4328aab733f82931cb1aa222627f6d5c98b22dfdd563966", + "TASK.md": "8d824008e2e17d0679c84808e09e748e58f87b88ae6ca8bd8cfc3a099e80fae3", + "baseline.py": "21c9ae912902a10fcbb39ecebbc49fa715d33ec9cad72296f651793cfe10a217", + "benchmark.py": "fe79d5b0f69a9eef3920caf300a203f0334f246caf4b331bca78582def0b6b95", + "optimization.py": "9a91413be62b32df99d49468f8ca17e3b4dc61a2256d0224933a4df5ebb1c9c7", + "reference.py": "329d5f3889b9d136f5436c2d6901067831b94dc028476bb9a0875bb829764901", + "task.json": "af15dc25e854f9c89370e2f0f6901a9a350f6c0f23a78c9f70835a191be29e64", + "workload.py": "1db0019dc53d0e70486e6ac6e343a6cc3814b851f0906a0eb0dc9fc57c5b0cda" + }, + "note": "Input hashes were verified before device execution. This RunSpec records the observed runtime and those frozen inputs." +} diff --git a/evidence/qwen-vats-gvo-910b1-20261124/results/local-archive.json b/evidence/qwen-vats-gvo-910b1-20261124/results/local-archive.json new file mode 100644 index 000000000..e59f58903 --- /dev/null +++ b/evidence/qwen-vats-gvo-910b1-20261124/results/local-archive.json @@ -0,0 +1,7 @@ +{ + "file": "qwen-vats-gvo-910b1-20261124-raw.tar.gz", + "bytes": 1032472, + "sha256": "f10cae7319ae0fef25d710135d0041b18140f21e47c659b9e8f226fa9e28e27f", + "regular_files": 725, + "availability": "Retained in the shared experiment workspace. Public PR contains the trial index and summary." +} diff --git a/evidence/qwen-vats-gvo-910b1-20261124/results/summary.json b/evidence/qwen-vats-gvo-910b1-20261124/results/summary.json new file mode 100644 index 000000000..88d4ad2f5 --- /dev/null +++ b/evidence/qwen-vats-gvo-910b1-20261124/results/summary.json @@ -0,0 +1,197 @@ +{ + "root_score": 0.9959408309538839, + "root_seconds": 38.33664607396349, + "training_seconds": 297.98867139196955, + "training": [ + { + "advantages": [ + -0.14027189486942632, + -0.010159721359567955, + 0.02576653459813036, + 0.03977936320016495, + 0.03397383269619028, + 0.050911885734508666 + ], + "checkpoint": "67bf7a236625c395ba2af214afc67f632cffcb72663ceeae51c056467b5413cb", + "operation": "gvo-5650a01b5b7f4af68d698280e8c1440c", + "returns": [ + 0.0, + 0.1084268112582153, + 0.13836535788963056, + 0.15004271505799271, + 0.14520477297134715, + 0.1593198171699458 + ], + "search_ids": [ + "round-0-0", + "round-0-1", + "round-0-2", + "round-0-3", + "round-0-4", + "round-0-5" + ] + } + ], + "training_candidate_checks": 9, + "initial": { + "drafter": [ + 0, + 0 + ], + "policy": [ + 1, + 1, + 2, + 0, + 0, + 0, + 0.5, + 0.5 + ], + "scorer": [ + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0 + ], + "origin": "Fixed initial preference for executing and verifying edits; uniform edit prior." + }, + "trained": { + "drafter": [ + -0.004815662002734385, + 0.004815662002734385 + ], + "policy": [ + 1.0074811701880644, + 1.0065901520281377, + 2.0008910181599266, + 0.0010388427382686271, + 0.004005434860190373, + 0.007753303890277802, + 0.512286404389109, + 0.5005227493037661 + ], + "scorer": [ + 0.20704381661547822, + 0.11937388232208675, + 0.08766993429339144, + 0.06560531387275832, + 0.13802921107698546, + 0.2070395223813406, + 0.08766993429339144, + 0.04575809827304042 + ], + "origin": "bb2372f85654ff8ee985e68710e27e344149653c4fad1438106f466fff94ef18" + }, + "blocks": [ + { + "seed": 101, + "arm": "serial", + "selected": "prefix-reuse+modulation-reuse", + "search_score": 1.1400680307341555, + "candidate_checks": 2, + "final_score": 1.1495683088485285, + "online_seconds": 101.64319652901031 + }, + { + "seed": 101, + "arm": "frozen", + "selected": "prefix-reuse+modulation-reuse", + "search_score": 1.1512057029474498, + "candidate_checks": 2, + "final_score": 1.1561996234571592, + "online_seconds": 93.90898948884569 + }, + { + "seed": 101, + "arm": "trained", + "selected": "prefix-reuse+modulation-reuse", + "search_score": 1.1493175869939765, + "candidate_checks": 2, + "final_score": 1.1656072035575094, + "online_seconds": 92.59875405603088 + }, + { + "seed": 202, + "arm": "frozen", + "selected": "prefix-reuse+modulation-reuse", + "search_score": 1.1476265647181823, + "candidate_checks": 2, + "final_score": 1.145692728816095, + "online_seconds": 101.89994073705748 + }, + { + "seed": 202, + "arm": "trained", + "selected": "prefix-reuse+modulation-reuse", + "search_score": 1.1380349511241032, + "candidate_checks": 2, + "final_score": 1.1545774762641814, + "online_seconds": 99.11530629708432 + }, + { + "seed": 202, + "arm": "serial", + "selected": "prefix-reuse+modulation-reuse", + "search_score": 1.154568637374006, + "candidate_checks": 2, + "final_score": 1.1388790918863319, + "online_seconds": 100.79321104194969 + }, + { + "seed": 303, + "arm": "trained", + "selected": "prefix-reuse+modulation-reuse", + "search_score": 1.1270553706728241, + "candidate_checks": 2, + "final_score": 1.1433745289187633, + "online_seconds": 94.2004004730843 + }, + { + "seed": 303, + "arm": "serial", + "selected": "prefix-reuse+modulation-reuse", + "search_score": 1.1527834102975818, + "candidate_checks": 2, + "final_score": 1.1541585326381407, + "online_seconds": 89.60749533609487 + }, + { + "seed": 303, + "arm": "frozen", + "selected": "prefix-reuse+modulation-reuse", + "search_score": 1.1523759775436213, + "candidate_checks": 2, + "final_score": 1.1556992744020855, + "online_seconds": 93.44745507300831 + } + ], + "trained_over_frozen": { + "paired_ratios": [ + 1.0081366399966647, + 1.0077549130099372, + 0.9893356812136976 + ], + "geomean": 1.0017038245535572 + }, + "trained_over_serial": { + "paired_ratios": [ + 1.0139521023548799, + 1.0137840658325268, + 0.9906563930219121 + ], + "geomean": 1.0060710428904178 + }, + "frozen_over_serial": { + "paired_ratios": [ + 1.0057685259393354, + 1.0059827570620141, + 1.0013349481205351 + ], + "geomean": 1.0043597900195769 + } +} diff --git a/evidence/qwen-vats-gvo-910b1-20261124/results/trials.jsonl b/evidence/qwen-vats-gvo-910b1-20261124/results/trials.jsonl new file mode 100644 index 000000000..cac56d65d --- /dev/null +++ b/evidence/qwen-vats-gvo-910b1-20261124/results/trials.jsonl @@ -0,0 +1,38 @@ +{"attempt": "initialization", "calls": 0, "candidate_sha256": "21c9ae912902a10fcbb39ecebbc49fa715d33ec9cad72296f651793cfe10a217", "cleanup_verified": true, "owner": "initialization-failure", "phase": "root", "result_sha256": "deb8bad49b63f4dd7eade04f01780ef1318d5950b08ee72e6d4c4782d6918b02", "score": null, "selection": "root", "source_result": "artifacts/executions/root-fbdf31c1c35396145ade507573ad007dbed39af62fb6f57b43e48517f9751dad/measurement/result.json", "verdict": "INCONCLUSIVE"} +{"attempt": "completed", "calls": 54, "candidate_sha256": "a26ed4712d0084defe121c477d88481bb2b68af19c54288b06ee39976a05ebcb", "cleanup_verified": true, "owner": "trained-202", "phase": "final", "result_sha256": "c9a5b9cb6da01e5372f2d4a2e2a715e8bb5432da2b5169971f5d14ce604bb425", "score": 1.1545774762641814, "selection": "prefix-reuse+modulation-reuse", "source_result": "artifacts/executions/final-11faf828a76c129ef0fca9ec48b45834557c593a16a91d3ff375f7503f2efa3b/measurement/result.json", "verdict": "PASS"} +{"attempt": "completed", "calls": 54, "candidate_sha256": "a26ed4712d0084defe121c477d88481bb2b68af19c54288b06ee39976a05ebcb", "cleanup_verified": true, "owner": "trained-303", "phase": "final", "result_sha256": "6272254e4b47facf43de750fdeecaebd558500154e4ff17d0735253537c44311", "score": 1.1433745289187633, "selection": "prefix-reuse+modulation-reuse", "source_result": "artifacts/executions/final-29ec1f2f16bac10e3efdebe9a57eafa9990e4389c5a9cc8872678a6f2dca58c9/measurement/result.json", "verdict": "PASS"} +{"attempt": "completed", "calls": 54, "candidate_sha256": "a26ed4712d0084defe121c477d88481bb2b68af19c54288b06ee39976a05ebcb", "cleanup_verified": true, "owner": "frozen-303", "phase": "final", "result_sha256": "e078d045b3f9c03df95bba9bf629d1edc399463dc71e7983198043babfeeebdf", "score": 1.1556992744020855, "selection": "prefix-reuse+modulation-reuse", "source_result": "artifacts/executions/final-73477dcd1dc2c3d6a6070629e75f578217d18f5348fbe03633214c75393e8a73/measurement/result.json", "verdict": "PASS"} +{"attempt": "completed", "calls": 54, "candidate_sha256": "a26ed4712d0084defe121c477d88481bb2b68af19c54288b06ee39976a05ebcb", "cleanup_verified": true, "owner": "frozen-202", "phase": "final", "result_sha256": "22a7d347aaa0911126248542f17d5626c598ffc178feb8e04154e335d5681f9e", "score": 1.145692728816095, "selection": "prefix-reuse+modulation-reuse", "source_result": "artifacts/executions/final-b444d3c0b54d96cbb5162dd7deb05ddce810601d93c4b08f4d78d6c4dcea49da/measurement/result.json", "verdict": "PASS"} +{"attempt": "completed", "calls": 54, "candidate_sha256": "a26ed4712d0084defe121c477d88481bb2b68af19c54288b06ee39976a05ebcb", "cleanup_verified": true, "owner": "frozen-101", "phase": "final", "result_sha256": "24035e383d52f5639b864d458671fbd2bf00b09090d13c27ef3fd23fac841559", "score": 1.1561996234571592, "selection": "prefix-reuse+modulation-reuse", "source_result": "artifacts/executions/final-bf2ae47739cd46ac9e21c6de962339f66ebb1799f9bdb772ba43007221af113b/measurement/result.json", "verdict": "PASS"} +{"attempt": "completed", "calls": 54, "candidate_sha256": "a26ed4712d0084defe121c477d88481bb2b68af19c54288b06ee39976a05ebcb", "cleanup_verified": true, "owner": "trained-101", "phase": "final", "result_sha256": "db18cb269fedd5e57f27f0017c85c8c4aaaedc68635b0b8c348539acbcd5bfdb", "score": 1.1656072035575094, "selection": "prefix-reuse+modulation-reuse", "source_result": "artifacts/executions/final-fafe7bc79363a48b1ce4858cd70b194b45e61edd820f3b43b72410eebab8af47/measurement/result.json", "verdict": "PASS"} +{"attempt": "completed", "calls": 54, "candidate_sha256": "55d76066b4ab15c1d7d90a31f550ae4ba13e52a8531ca0026eb79bcce26a4267", "cleanup_verified": true, "owner": "frozen-101", "phase": "verify", "result_sha256": "e30eef43496b8d60f5b91927ebfe8055ff1b48539770594225b96e1667a449c0", "score": 1.043937733735849, "selection": "prefix-reuse", "source_result": "artifacts/executions/frozen-101-6a66a2fcbd744baf85e6685dfdb2d860/measurement/result.json", "verdict": "PASS"} +{"attempt": "completed", "calls": 54, "candidate_sha256": "a26ed4712d0084defe121c477d88481bb2b68af19c54288b06ee39976a05ebcb", "cleanup_verified": true, "owner": "frozen-101", "phase": "verify", "result_sha256": "645efbce38bdd94001e0a6eade303961abbeabd592a7713648a51711b4e02d30", "score": 1.1512057029474498, "selection": "prefix-reuse+modulation-reuse", "source_result": "artifacts/executions/frozen-101-afbef37914344b1f94bf026005f61f6a/measurement/result.json", "verdict": "PASS"} +{"attempt": "completed", "calls": 54, "candidate_sha256": "55d76066b4ab15c1d7d90a31f550ae4ba13e52a8531ca0026eb79bcce26a4267", "cleanup_verified": true, "owner": "frozen-202", "phase": "verify", "result_sha256": "fb2b69db4337f2a286a14c9a0427295d835c14e51c23a28fc4bf52be1b4217b8", "score": 1.0390554119130886, "selection": "prefix-reuse", "source_result": "artifacts/executions/frozen-202-b2cb2bd1a2b742fa8ba08fb961c34b86/measurement/result.json", "verdict": "PASS"} +{"attempt": "completed", "calls": 54, "candidate_sha256": "a26ed4712d0084defe121c477d88481bb2b68af19c54288b06ee39976a05ebcb", "cleanup_verified": true, "owner": "frozen-202", "phase": "verify", "result_sha256": "8270e89d02c6ac65057b233f3d1a77a6a8d90ef914146f0cccaccd2a62469f1e", "score": 1.1476265647181823, "selection": "prefix-reuse+modulation-reuse", "source_result": "artifacts/executions/frozen-202-bc239d520816464c8104ff0901143fde/measurement/result.json", "verdict": "PASS"} +{"attempt": "completed", "calls": 54, "candidate_sha256": "55d76066b4ab15c1d7d90a31f550ae4ba13e52a8531ca0026eb79bcce26a4267", "cleanup_verified": true, "owner": "frozen-303", "phase": "verify", "result_sha256": "2884933ead712ac0b86bd5cfc00e49e438b1a493c427fb17dabf5b3b377f8783", "score": 1.0350787145698168, "selection": "prefix-reuse", "source_result": "artifacts/executions/frozen-303-515494d451cb431ca7f6f0350c241002/measurement/result.json", "verdict": "PASS"} +{"attempt": "completed", "calls": 54, "candidate_sha256": "a26ed4712d0084defe121c477d88481bb2b68af19c54288b06ee39976a05ebcb", "cleanup_verified": true, "owner": "frozen-303", "phase": "verify", "result_sha256": "99d0bd32990bd26a726d96489a309022e11c343cdce952a30a9d1bcfaf39c317", "score": 1.1523759775436213, "selection": "prefix-reuse+modulation-reuse", "source_result": "artifacts/executions/frozen-303-52257fd4d22c404b9850915575b6e578/measurement/result.json", "verdict": "PASS"} +{"attempt": "completed", "calls": 54, "candidate_sha256": "21c9ae912902a10fcbb39ecebbc49fa715d33ec9cad72296f651793cfe10a217", "cleanup_verified": true, "owner": "training", "phase": "root", "result_sha256": "ca48db59e766dcfc050a28ef4af97f8de4efa1941e965e41097aab984baff362", "score": 0.9959408309538839, "selection": "root", "source_result": "artifacts/executions/root-fbdf31c1c35396145ade507573ad007dbed39af62fb6f57b43e48517f9751dad/measurement/result.json", "verdict": "PASS"} +{"attempt": "completed", "calls": 54, "candidate_sha256": "55d76066b4ab15c1d7d90a31f550ae4ba13e52a8531ca0026eb79bcce26a4267", "cleanup_verified": true, "owner": "round-0-1", "phase": "verify", "result_sha256": "e656241774ac5f128dd039b776e3b8febea969699790af2fae011034bc6d7ee8", "score": 1.0435289214062855, "selection": "prefix-reuse", "source_result": "artifacts/executions/round-0-1-cf028ed43a454a7bb856599bf886f809/measurement/result.json", "verdict": "PASS"} +{"attempt": "completed", "calls": 54, "candidate_sha256": "bdc8ecd95355ad29957ac40b3ff444e8f54df52dffeb65e893d19a7bcb84c09e", "cleanup_verified": true, "owner": "round-0-1", "phase": "verify", "result_sha256": "fd2a2c4ed10d961410c1997a1b9c1222c3f4c06a025ac034e1facd8b62a024c2", "score": 1.1043676422120992, "selection": "modulation-reuse", "source_result": "artifacts/executions/round-0-1-f9b35d42f7bd42a1854ee3dd0125f8c6/measurement/result.json", "verdict": "PASS"} +{"attempt": "completed", "calls": 54, "candidate_sha256": "55d76066b4ab15c1d7d90a31f550ae4ba13e52a8531ca0026eb79bcce26a4267", "cleanup_verified": true, "owner": "round-0-2", "phase": "verify", "result_sha256": "e04a3fa898f4fbe2f4cc060e59de2e8c2b48988279067bb68fca5254d28007e3", "score": 1.0488348757844235, "selection": "prefix-reuse", "source_result": "artifacts/executions/round-0-2-3f78d80d1f914b75b1d953c08138eaa0/measurement/result.json", "verdict": "PASS"} +{"attempt": "completed", "calls": 54, "candidate_sha256": "a26ed4712d0084defe121c477d88481bb2b68af19c54288b06ee39976a05ebcb", "cleanup_verified": true, "owner": "round-0-2", "phase": "verify", "result_sha256": "ad1a5b41b18ebac6efe72ca71f999bb1dc63ca17451e13fc7006f05c363dccc8", "score": 1.1343061888435144, "selection": "prefix-reuse+modulation-reuse", "source_result": "artifacts/executions/round-0-2-f82eabbbe2864c46b5413f28b87c861f/measurement/result.json", "verdict": "PASS"} +{"attempt": "completed", "calls": 54, "candidate_sha256": "a26ed4712d0084defe121c477d88481bb2b68af19c54288b06ee39976a05ebcb", "cleanup_verified": true, "owner": "round-0-3", "phase": "verify", "result_sha256": "43ffe86162896c05e0cfe6c16c57b7ad48adfe68c109ae9542c2c1e490917ac4", "score": 1.1459835460118766, "selection": "prefix-reuse+modulation-reuse", "source_result": "artifacts/executions/round-0-3-626cd349875f45e69516277e26b3c5a5/measurement/result.json", "verdict": "PASS"} +{"attempt": "completed", "calls": 54, "candidate_sha256": "55d76066b4ab15c1d7d90a31f550ae4ba13e52a8531ca0026eb79bcce26a4267", "cleanup_verified": true, "owner": "round-0-3", "phase": "verify", "result_sha256": "b27a2f27d02ada7e0e48b9bb82ae1757e546737439f26691e424cc0cfc195a06", "score": 1.0438470904481043, "selection": "prefix-reuse", "source_result": "artifacts/executions/round-0-3-76fd5d825bf24297b19f257171b01304/measurement/result.json", "verdict": "PASS"} +{"attempt": "completed", "calls": 54, "candidate_sha256": "a26ed4712d0084defe121c477d88481bb2b68af19c54288b06ee39976a05ebcb", "cleanup_verified": true, "owner": "round-0-4", "phase": "verify", "result_sha256": "7becbeaba51247c0ad2630997577d29e08da9e7a9686e576f08fe8397e7a7a70", "score": 1.141145603925231, "selection": "prefix-reuse+modulation-reuse", "source_result": "artifacts/executions/round-0-4-137db7d0b55345f5b083a651ca8be752/measurement/result.json", "verdict": "PASS"} +{"attempt": "completed", "calls": 54, "candidate_sha256": "bdc8ecd95355ad29957ac40b3ff444e8f54df52dffeb65e893d19a7bcb84c09e", "cleanup_verified": true, "owner": "round-0-4", "phase": "verify", "result_sha256": "76a9395255bbaa2ef0fbd2b67441fbdd2aa82c8f134b0dd40bd2806f7ed2bef5", "score": 1.0911973414391936, "selection": "modulation-reuse", "source_result": "artifacts/executions/round-0-4-1ee3073b0cc0444780942e27ec8e350d/measurement/result.json", "verdict": "PASS"} +{"attempt": "completed", "calls": 54, "candidate_sha256": "a26ed4712d0084defe121c477d88481bb2b68af19c54288b06ee39976a05ebcb", "cleanup_verified": true, "owner": "round-0-5", "phase": "verify", "result_sha256": "46e5d86f875c8673c98a983fca214fd98a85d168078244d9e91f277d05de398a", "score": 1.1552606481238297, "selection": "prefix-reuse+modulation-reuse", "source_result": "artifacts/executions/round-0-5-ec649fd4530442e8834af198d5af1e98/measurement/result.json", "verdict": "PASS"} +{"attempt": "completed", "calls": 54, "candidate_sha256": "55d76066b4ab15c1d7d90a31f550ae4ba13e52a8531ca0026eb79bcce26a4267", "cleanup_verified": true, "owner": "trained-101", "phase": "verify", "result_sha256": "f13fbb0e6708ee85d998965c9d8bef5905c58670b5ae2a287e4e1582bf3a515f", "score": 1.0297912232733322, "selection": "prefix-reuse", "source_result": "artifacts/executions/trained-101-432bb8279a664816ab5b2dc75b8da394/measurement/result.json", "verdict": "PASS"} +{"attempt": "completed", "calls": 54, "candidate_sha256": "a26ed4712d0084defe121c477d88481bb2b68af19c54288b06ee39976a05ebcb", "cleanup_verified": true, "owner": "trained-101", "phase": "verify", "result_sha256": "f5f171d90ca67eb2802b90afdd1e1cfe0b7455d643f52ee37aff0e1f249c79f6", "score": 1.1493175869939765, "selection": "prefix-reuse+modulation-reuse", "source_result": "artifacts/executions/trained-101-bae00fefaf904e44a879cb5b2bfcebea/measurement/result.json", "verdict": "PASS"} +{"attempt": "completed", "calls": 54, "candidate_sha256": "55d76066b4ab15c1d7d90a31f550ae4ba13e52a8531ca0026eb79bcce26a4267", "cleanup_verified": true, "owner": "trained-202", "phase": "verify", "result_sha256": "37380a867691d50c5453a1059ad2c2e87259e19f48325181b23cf0c7b5555970", "score": 1.0531153658016172, "selection": "prefix-reuse", "source_result": "artifacts/executions/trained-202-0b693a491a7b4d10a686b54db1b17924/measurement/result.json", "verdict": "PASS"} +{"attempt": "completed", "calls": 54, "candidate_sha256": "a26ed4712d0084defe121c477d88481bb2b68af19c54288b06ee39976a05ebcb", "cleanup_verified": true, "owner": "trained-202", "phase": "verify", "result_sha256": "043af7764b9ac37b5f6adc43ae8b114abf2245c136a8dbc99beae4c78653da30", "score": 1.1380349511241032, "selection": "prefix-reuse+modulation-reuse", "source_result": "artifacts/executions/trained-202-ec689db026e64b89a187abfd13fa949f/measurement/result.json", "verdict": "PASS"} +{"attempt": "completed", "calls": 54, "candidate_sha256": "55d76066b4ab15c1d7d90a31f550ae4ba13e52a8531ca0026eb79bcce26a4267", "cleanup_verified": true, "owner": "trained-303", "phase": "verify", "result_sha256": "2730bcb3a080d79cd3fed15b4a3e75e2e3bfe3a3b3a2608cf235fb96ccfbf2e5", "score": 1.0378958280450112, "selection": "prefix-reuse", "source_result": "artifacts/executions/trained-303-b11880c601f54d8595fea4aa9b3ac598/measurement/result.json", "verdict": "PASS"} +{"attempt": "completed", "calls": 54, "candidate_sha256": "a26ed4712d0084defe121c477d88481bb2b68af19c54288b06ee39976a05ebcb", "cleanup_verified": true, "owner": "trained-303", "phase": "verify", "result_sha256": "8499b892d33046901c82298b873214465e6894bda9ede88ce7726f34fbb71256", "score": 1.1270553706728241, "selection": "prefix-reuse+modulation-reuse", "source_result": "artifacts/executions/trained-303-be7c10a68cb2446695d3e6b8b69d366f/measurement/result.json", "verdict": "PASS"} +{"attempt": "completed", "calls": 54, "candidate_sha256": "55d76066b4ab15c1d7d90a31f550ae4ba13e52a8531ca0026eb79bcce26a4267", "cleanup_verified": true, "owner": "serial-101", "phase": "verify", "result_sha256": "2d4576a1cfb992b058ac97e0eef77d8a880c899105456ceda3bfbf19f259a581", "score": 1.0428590482252562, "selection": "prefix-reuse", "source_result": "serial-101/0/measurement/result.json", "verdict": "PASS"} +{"attempt": "completed", "calls": 54, "candidate_sha256": "a26ed4712d0084defe121c477d88481bb2b68af19c54288b06ee39976a05ebcb", "cleanup_verified": true, "owner": "serial-101", "phase": "verify", "result_sha256": "4ae3440b4779fa882b07d190418cadff2008ad9bfc32e1189757e1f606886505", "score": 1.1400680307341555, "selection": "prefix-reuse+modulation-reuse", "source_result": "serial-101/1/measurement/result.json", "verdict": "PASS"} +{"attempt": "completed", "calls": 54, "candidate_sha256": "a26ed4712d0084defe121c477d88481bb2b68af19c54288b06ee39976a05ebcb", "cleanup_verified": true, "owner": "serial-101", "phase": "final", "result_sha256": "601ede4d9e77b1013db46651d7ea8604c90c5dc009b4932498648a9c28e35a0b", "score": 1.1495683088485285, "selection": "prefix-reuse+modulation-reuse", "source_result": "serial-101/final/measurement/result.json", "verdict": "PASS"} +{"attempt": "completed", "calls": 54, "candidate_sha256": "55d76066b4ab15c1d7d90a31f550ae4ba13e52a8531ca0026eb79bcce26a4267", "cleanup_verified": true, "owner": "serial-202", "phase": "verify", "result_sha256": "1a63a5641476c4cecadbcfa1b1c1dbddde39b9ec5b054e0d01a4f804c0837886", "score": 1.0482416408644668, "selection": "prefix-reuse", "source_result": "serial-202/0/measurement/result.json", "verdict": "PASS"} +{"attempt": "completed", "calls": 54, "candidate_sha256": "a26ed4712d0084defe121c477d88481bb2b68af19c54288b06ee39976a05ebcb", "cleanup_verified": true, "owner": "serial-202", "phase": "verify", "result_sha256": "12d8cb434f5d849923ffd79e5fcb119590325ab779dd08047340e3b877c50894", "score": 1.154568637374006, "selection": "prefix-reuse+modulation-reuse", "source_result": "serial-202/1/measurement/result.json", "verdict": "PASS"} +{"attempt": "completed", "calls": 54, "candidate_sha256": "a26ed4712d0084defe121c477d88481bb2b68af19c54288b06ee39976a05ebcb", "cleanup_verified": true, "owner": "serial-202", "phase": "final", "result_sha256": "572a1543c808e2c0fde10ec8677aa3011b48817dc8f2d8258ef2ca51f2ec8ce3", "score": 1.1388790918863319, "selection": "prefix-reuse+modulation-reuse", "source_result": "serial-202/final/measurement/result.json", "verdict": "PASS"} +{"attempt": "completed", "calls": 54, "candidate_sha256": "55d76066b4ab15c1d7d90a31f550ae4ba13e52a8531ca0026eb79bcce26a4267", "cleanup_verified": true, "owner": "serial-303", "phase": "verify", "result_sha256": "7224048dbaab35b4d9840e3aa9da27844ea398c44dda47ff5b3137477a26e19a", "score": 1.0448750874935306, "selection": "prefix-reuse", "source_result": "serial-303/0/measurement/result.json", "verdict": "PASS"} +{"attempt": "completed", "calls": 54, "candidate_sha256": "a26ed4712d0084defe121c477d88481bb2b68af19c54288b06ee39976a05ebcb", "cleanup_verified": true, "owner": "serial-303", "phase": "verify", "result_sha256": "583da64b85dee49186a6aa84f120b8bce1852ef310b5054f9e20c3b9a003f8ea", "score": 1.1527834102975818, "selection": "prefix-reuse+modulation-reuse", "source_result": "serial-303/1/measurement/result.json", "verdict": "PASS"} +{"attempt": "completed", "calls": 54, "candidate_sha256": "a26ed4712d0084defe121c477d88481bb2b68af19c54288b06ee39976a05ebcb", "cleanup_verified": true, "owner": "serial-303", "phase": "final", "result_sha256": "49819d409ce82d493a24327aac7db74078bdae183437b975ba3081657f8f306a", "score": 1.1541585326381407, "selection": "prefix-reuse+modulation-reuse", "source_result": "serial-303/final/measurement/result.json", "verdict": "PASS"} diff --git a/evidence/qwen-vats-gvo-910b1-20261124/validator/README.md b/evidence/qwen-vats-gvo-910b1-20261124/validator/README.md new file mode 100644 index 000000000..e2c0f0f8b --- /dev/null +++ b/evidence/qwen-vats-gvo-910b1-20261124/validator/README.md @@ -0,0 +1,23 @@ +# Frozen evaluation and replay + +The [packaged task at the measured revision](https://github.com/InferMatrixLab/InferRSI/tree/f5b9a1819d387e0a0a1a219da6dcc0f5504c4069/src/inferrsi/benchmark/tasks/qwen21-denoise-npu) +owns every input, five correctness cases and the three timing cases. Its +`optimization.py` checks source identities, per-step trajectory error, sample +counts, timing medians and the geometric mean. Every candidate and final is +compared with the original denoiser. A final requires a fresh evaluation, a +complete numerical result and verified process cleanup. + +Hardware replay uses the command and environment in the +[design document](../../../docs/speculative-search.md#measuring-the-contribution-of-gvo). +Supply a fresh directory. RunStore operation identities and hardware timings vary +between replays. The trial index records the actual files from this run. + +For offline re-assessment with the original raw files, run the following from +the checkout with the directory produced by the hardware run and its retained +initialization failure. The checker repeats the packaged assessment, binds final +score and selected source to the run summary, and prints the compact trial index. + +```bash +PYTHONPATH=src python evidence/qwen-vats-gvo-910b1-20261124/validator/check.py \ + +``` diff --git a/evidence/qwen-vats-gvo-910b1-20261124/validator/check.py b/evidence/qwen-vats-gvo-910b1-20261124/validator/check.py new file mode 100644 index 000000000..cc20c7e94 --- /dev/null +++ b/evidence/qwen-vats-gvo-910b1-20261124/validator/check.py @@ -0,0 +1,169 @@ +"""Re-assess the real task outputs and emit an index from the retained measurements.""" + +import argparse +import hashlib +import json +import math +import runpy +from collections import Counter +from pathlib import Path +from statistics import geometric_mean + +from inferrsi.benchmark.registry import task_resources +from inferrsi.harness.run_store import RunStoreReader +from inferrsi.harness.work_control import WorkControl + +PLAN = Path(__file__).resolve().parents[1] / "plan" + + +def read(path): + return json.loads(path.read_text()) + + +def sha(path): + return hashlib.sha256(path.read_bytes()).hexdigest() + + +def require(value, reason): + if not value: + raise ValueError(reason) + + +def close(left, right): + return math.isclose(left, right, rel_tol=1e-12, abs_tol=1e-12) + + +def main(completed, failed): + plan = read(PLAN / "plan.json") + task = Path(str(task_resources(plan["task"]))) + protocol = runpy.run_path(str(task / "optimization.py")) + settings = protocol["validate_arguments"](plan["sampling"]) + spec = read(PLAN / "runspec.json") + for name, expected_hash in spec["input_sha256"].items(): + require(sha(PLAN.parent / name) == expected_hash, name) + for name, expected_hash in spec["packaged_task_sha256"].items(): + require(sha(task / name) == expected_hash, name) + summary = read(completed / "summary.json") + expected = [(arm, seed) for seed, arms in plan["blocks"] for arm in arms] + rows = {(item["arm"], item["seed"]): item for item in summary["blocks"]} + require(list(rows) == expected and len(rows) == len(summary["blocks"]), "Deployment order") + control = WorkControl(RunStoreReader(completed / "store", "speculative")) + campaigns = ["training", *(f"{a}-{s}" for a, s in expected if a != "serial")] + labels = {} + for campaign in campaigns: + doc = control.read(campaign) + require(not doc["blocked"], campaign) + for operation, op in doc["operations"].items(): + require(op["status"] == "settled", operation) + if op["category"] in ("root", "verify", "final"): + search = operation.rsplit("-", 1)[0] if op["category"] == "verify" else campaign + labels[operation] = (op["category"], search) + names = {sha(p): p.parent.name for p in completed.glob("sources/*/candidate.py")} + train = control.read("training")["state"] + require(train["checkpoint"] == summary["trained"], "Retained checkpoint") + require(summary["initial"] == plan["checkpoint"], "Initial role weights") + group = train["training"] + require(group == summary["training"] and len(group) == plan["train"]["rounds"], "Update count") + for update in group: + ids = update["search_ids"] + records = [train["searches"][i] for i in ids] + require(len(set(ids)) == len(records) == plan["train"]["group_size"], "Collection size") + require(len({r["initial_conditions"] for r in records}) == 1, "Collection start") + require( + all(r["status"] == "complete" and r["collect"] and not r["forced"] for r in records), + "Expected completed natural collection", + ) + returns = [r["gain"] - r["initial_gain"] for r in records] + require(returns == update["returns"], "Replayed unpenalized returns") + for i, observed in enumerate(update["advantages"]): + others = returns[:i] + returns[i + 1 :] + require(close(observed, returns[i] - math.fsum(others) / len(others)), "Advantage") + index = [] + checked_ops = set() + for root in (failed, completed): + for path in (root / "frozen").iterdir(): + if path.is_file(): + require( + sha(path) == sha(task / path.name), f"Frozen validator changed: {path.name}" + ) + for measure in sorted(root.rglob("measurement/result.json")): + folder = measure.parents[1] + raw = read(measure) + execution = read(folder / "execution/execution.json") + assessment = protocol["assess"](folder, settings, execution) + require(assessment == read(folder / "assessment.json"), "Retained task assessment") + relative = folder.relative_to(root) + source_hash = sha(folder / "candidate.py") + name = names[source_hash] + if root == failed: + phase, owner = "root", "initialization-failure" + require( + raw["status"] == "infrastructure_failure" and assessment["score"] is None, + "Initial attempt status", + ) + elif relative.parts[0] == "artifacts": + phase, owner = labels[folder.name] + checked_ops.add(folder.name) + else: + owner = relative.parts[0] + phase = "final" if relative.parts[-1] == "final" else "verify" + require(owner.startswith("serial-"), "Unrecognized direct evaluation") + if root == completed: + require( + assessment["verdict"] == "PASS" and execution["cleanup_verified"] is True, + "Task quality or cleanup", + ) + if phase == "final": + arm, seed = owner.split("-") + row = rows[arm, int(seed)] + require( + name == row["selected"] and close(assessment["score"], row["final_score"]), + "Final artifact or final score mismatch", + ) + index.append( + { + "attempt": "initialization" if root == failed else "completed", + "owner": owner, + "phase": phase, + "selection": name, + "candidate_sha256": source_hash, + "source_result": str(measure.relative_to(root)), + "result_sha256": sha(measure), + "verdict": assessment["verdict"], + "cleanup_verified": execution["cleanup_verified"], + "score": assessment["score"], + "calls": len(raw.get("calls", [])), + } + ) + require(checked_ops == set(labels), "Missing retained execution result") + require(sum(r["attempt"] == "initialization" for r in index) == 1, "Failed attempt count") + counts = Counter((r["owner"], r["phase"]) for r in index) + require(counts["training", "root"] == 1, "Shared qualification count") + for (arm, seed), row in rows.items(): + name = f"{arm}-{seed}" + require(counts[name, "final"] == 1, name) + require(counts[name, "verify"] == row["candidate_checks"] <= 2, name) + require( + sum(counts[i, "verify"] for i in train["searches"]) == summary["training_candidate_checks"], + "Training call count", + ) + for left, right in (("trained", "frozen"), ("trained", "serial"), ("frozen", "serial")): + ratios = [ + rows[left, seed]["final_score"] / rows[right, seed]["final_score"] + for seed, _ in plan["blocks"] + ] + result = summary[f"{left}_over_{right}"] + require( + ratios == result["paired_ratios"] and close(geometric_mean(ratios), result["geomean"]), + "Paired aggregate", + ) + for row in index: + print(json.dumps(row, sort_keys=True)) + + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("completed", type=Path) + parser.add_argument("failed", type=Path) + args = parser.parse_args() + main(args.completed.resolve(), args.failed.resolve()) diff --git a/tests/repository/test_evidence_inventory.py b/tests/repository/test_evidence_inventory.py index 475ab343d..66de140d9 100644 --- a/tests/repository/test_evidence_inventory.py +++ b/tests/repository/test_evidence_inventory.py @@ -145,6 +145,6 @@ def test_checked_in_inventory(): repo = Path(__file__).resolve().parents[2] result = validate_inventory(repo) assert result["errors"] == [] - assert len(result["bundles"]) == 26 - assert result["checked_hashes"] == 461 + assert len(result["bundles"]) == 27 + assert result["checked_hashes"] == 472 assert result["unavailable"] # Historical raw artifacts are not in this checkout. diff --git a/tools/evidence_inventory.json b/tools/evidence_inventory.json index b6e021fc0..e0958c8da 100644 --- a/tools/evidence_inventory.json +++ b/tools/evidence_inventory.json @@ -231,6 +231,11 @@ "manifests": [{"path": "plan/integrity.json", "schema": "bundle-sha256"}], "external": ["Original private client captures and unredacted host-specific plans remain outside the repository; the public bundle retains labelled redactions, original digests and replayable answer checks."] }, + "qwen-vats-gvo-910b1-20261124": { + "kind": "campaign", + "manifests": [{"path": "plan/retained-sha256.json", "schema": "bundle-sha256"}], + "external": ["The indexed task outputs, execution receipts and shared RunStore are retained in the local experiment archive. The PR contains the frozen inputs, trial index and measured summary. No public raw archive or cross-task qualification is claimed."] + }, "qwen3-tts-ascend-import": { "kind": "import", "manifests": [], "external": ["Original campaign validators and artifacts remain at the upstream source described in plan/README.md."] From 62efabf5681045bc926347dc6badd7752bdf210b Mon Sep 17 00:00:00 2001 From: amy-why-3459 Date: Tue, 24 Nov 2026 14:55:02 +0800 Subject: [PATCH 12/16] search: enforce launch, holdout and final promotion controls Release definitively unstarted work during recovery and gate dispatch on campaign state. Bind Skill final claims to canonical task membership and the actual development comparator. Check durable promotion and candidate identity before reporting the Qwen diagnostic, preserving partial runs and the original measurements. Agent-assisted-by: Codex (implementation and validation) Signed-off-by: amy-why-3459 --- docs/speculative-search.md | 76 +++-- .../qwen-vats-gvo-910b1-20261124/README.md | 4 +- .../plan/retained-sha256.json | 8 +- .../qwen-vats-gvo-910b1-20261124/plan/run.py | 86 ++++-- .../validator/README.md | 17 +- .../validator/check.py | 100 ++++++- .../apps/workflows/search_benchmarks.py | 5 + src/inferrsi/harness/gvo.py | 2 +- src/inferrsi/harness/search_adapter.py | 18 +- src/inferrsi/harness/search_campaign.py | 40 ++- src/inferrsi/harness/search_continuations.py | 1 + src/inferrsi/harness/search_records.py | 8 +- src/inferrsi/harness/search_tree.py | 3 + src/inferrsi/harness/vats.py | 15 +- src/inferrsi/harness/work_control.py | 107 +++++-- tests/test_qwen_gvo_diagnostic.py | 104 +++++++ tests/test_search_benchmarks.py | 278 +++++++++++++++++- tests/test_vats_runtime.py | 71 ++++- tests/test_work_control.py | 26 ++ tools/speculative_skill.py | 99 ++++++- 20 files changed, 948 insertions(+), 120 deletions(-) create mode 100644 tests/test_qwen_gvo_diagnostic.py diff --git a/docs/speculative-search.md b/docs/speculative-search.md index c3e0fdf06..dda5b7e3a 100644 --- a/docs/speculative-search.md +++ b/docs/speculative-search.md @@ -182,10 +182,11 @@ use the **same shared store**. Launch intent is durable before dispatch; an ambiguous crash never authorizes a second launch. Known elapsed time and cleanup release unused reservation. Unknown usage/cleanup retains the entire reservation and domain. Over-cap work -blocks further claims immediately, even with unknown cleanup; the retained hold -is at least the largest known usage. Later reconciliation releases the domain -and charges the final usage once, never below that known lower bound. Absolute -deadlines include queueing and cleanup. +blocks further claims and dispatch of previously reserved work. The launch +transaction rechecks the campaign before recording intent. With unknown cleanup, +the retained hold is at least the largest known usage. Later reconciliation +releases the domain and charges the final usage once, never below that lower +bound. Absolute deadlines include queueing and cleanup. Engine protocol timeouts retain an INCONCLUSIVE adapter receipt with measured elapsed time. Confirmed cleanup of both task and protocol permits settlement and domain release; uncertain protocol cleanup keeps the reservation. A known @@ -205,9 +206,10 @@ checks retained candidate bytes and never launches new work. Lost preparation, training or copy receipts remain unresolved; no guessed-cost retry is available. External process-liveness reconciliation needs a qualified authority. -An expired launch recorded as `settled/not_started` is conclusive control evidence -that dispatch never occurred. Search recovery clears its pending work and ends -the current block; a registered verification attempt gets a control-only +When a reservation expires or its campaign is blocked before launch intent, +recovery settles it as `not_started` and releases its domain in one transaction. +Search recovery clears the pending operation and ends the current block. A +registered verification attempt gets a control-only `INCONCLUSIVE` receipt, with no objective or evaluator PASS. No execution is charged. A search whose limit has expired can then return its qualified incumbent. If only the operation deadline expired, the remaining campaign may continue with @@ -216,12 +218,15 @@ fresh claims. This exception does not apply to ambiguous launch-intent crashes. `finalize` consumes a unique scope in the shared store before final data access, freezes selection and closes search/training. Engine compares again with the original baseline. For Skill, supply a **holdout-bound adapter** and -`Comparator.holdout_scope` (derived from the frozen data digest), never the -development adapter or a replacement label. Exact bounded radix utility keeps -quality before token use; reduced tokens cannot offset a quality regression. -The Skill evaluator validates its holdout binding before the scope is claimed -or data is accessed, rejecting development evaluators and missing/mismatched -holdout identities. +`Comparator.holdout_scope`, derived from frozen task membership. The raw SHA-256 +separately pins each file's exact bytes. Root qualification and VATS search use +the development evaluator. Finalization checks that its comparator matches the +one bound to the recorded development evidence. After the final claim and before +any provider or grader call, the comparator parses the same verified bytes for +both splits, checks the declared membership, and rejects shared case IDs or +identical case inputs across splits. Changing JSON formatting, case order, IDs +or provenance gives the same set of inputs a single final allowance in the +shared store. Exact bounded radix utility keeps quality ahead of token use. After interruption, final consumes existing receipts only. Even an unstarted remaining check cannot be newly dispatched by recovery. Existing final results @@ -263,10 +268,23 @@ and `finalize`. Recovery requires the original frozen adapters. The CLI has no blind whole-campaign restart command. Skill's checkout `Comparator` takes the existing comparator configuration with an explicit bounded provider, original Git baseline, editable bundle paths and two split descriptors (`path`, `sha256`, -`scope`, `task_count`). It uses the existing private Git-index materializer, -executes the existing comparator under the shared process executor and revalidates -its independent retained report. Construction does not load holdout bytes. -Provider readiness/credentials/authorization remain existing explicit requirements. +`scope`, `task_count`). When preparing data, compute `task_set_sha256` with +`tools.speculative_skill.task_set_sha256(cases)` and include it in the holdout +descriptor before starting a campaign. The field is optional for development. +The comparator uses the existing private Git-index materializer and shared +process executor, and revalidates its independent retained report. Construction +does not load holdout bytes. Providers require explicit authorization and +credentials. + +Older descriptors can still serve development. Search histories written before +comparator identity was recorded remain readable for reporting. A Skill final +for a campaign with development history requires those records to identify the +same comparator. Upgrade all Skill controllers that write the shared store +together, since older controllers enforce raw-byte scopes. If the store contains +old `skill-data:` claims, bind each consumed claim to its archived input with +`tools.speculative_skill.migrate_holdout(control, raw_sha256, archive_path)`. +The migration verifies those original bytes and records the membership of the +already-consumed set. Missing archives keep new Skill final claims blocked. ## Relation to the paper and reference implementation @@ -352,7 +370,12 @@ The output contains the shared RunStore, checkpoints, all task snapshots, timings and per-step numerical checks. Arm order rotates across three blocks so each arm runs first, second and last once. All three blocks reuse one trained checkpoint. Their random seeds vary the search on the same -workload. Training variability and transfer to other tasks require additional +workload. The driver saves the full arm order before starting. A VATS arm +contributes a score only when `finalize` reports an overall PASS and promotes +the selected candidate, and the fresh task result passes with verified cleanup. +An interrupted or rejected arm retains its incomplete status, later arms retain +their unrun status, and the driver publishes paired ratios only for a complete +comparison. Training variability and transfer to other tasks require additional independent runs. ### Ascend 910B1 diagnostic results @@ -389,7 +412,10 @@ exclude the common root, queue, container startup and setup. The small online timing difference does not establish a repeatable saving or break-even point. The verifier replayed all 37 completed five-case task evaluations (1,998 -compute calls) and the earlier device-initialization failure. Overall search +compute calls) and the earlier device-initialization failure. An offline audit +of the archived RunStore also confirmed that all six VATS deployments promoted +their selected candidate and bound its final receipt to the measured files. +The three serial controls have direct independent task finals. Overall search efficacy remains **INCONCLUSIVE**. The [evidence bundle](../evidence/qwen-vats-gvo-910b1-20261124/README.md) links the frozen inputs, checkpoint, per-trial index, archive availability and raw-data verification instructions. @@ -399,7 +425,7 @@ raw-data verification instructions. ```bash python -m pytest tests/test_speculative_search.py tests/test_verification_plans.py \ tests/test_work_control.py tests/test_search_roles.py tests/test_vats_runtime.py \ - tests/test_search_benchmarks.py + tests/test_search_benchmarks.py tests/test_qwen_gvo_diagnostic.py ``` Controls cover exhaustive planning, failed-parent repair, finite-score overflow, @@ -420,6 +446,10 @@ they are not real task/provider efficacy evidence. | AC-09 | Packaged Engine evaluator and explicit Skill comparator bridge | Live Agent/Skill pilot and broader Engine coverage | | AC-10 | Retained trajectories/costs and ablation switches | Replicated learning and bundle, Reset/Carry and worker comparisons with full lifecycle costs | -The parent issue remains open. Software tests, CPU mathematical checks or an -available adapter do not establish full acceptance, NPU speedup, provider quality, -search efficiency or universal SpecRSI gains. +The proposed slice for PR #488 is the single-worker local search and learning +runtime, its recovery and final-evidence controls, and the bounded Engine +diagnostic above. Its software checks and the archived measurements support +review of that slice. A maintainer decision on accepting it separately from +the remaining criteria is pending. The [Phase 1 discussion](https://github.com/InferMatrixLab/InferRSI/issues/363#issuecomment-6969704442) +proposed freezing a design. [Issue #363](https://github.com/InferMatrixLab/InferRSI/issues/363) +remains open for the acceptance work in the last column. diff --git a/evidence/qwen-vats-gvo-910b1-20261124/README.md b/evidence/qwen-vats-gvo-910b1-20261124/README.md index ddfacb539..9ec16981c 100644 --- a/evidence/qwen-vats-gvo-910b1-20261124/README.md +++ b/evidence/qwen-vats-gvo-910b1-20261124/README.md @@ -7,11 +7,11 @@ on the packaged Qwen-Image 2.1 denoiser. The underlying search and task code was The [design document](../../docs/speculative-search.md#measuring-the-contribution-of-gvo) explains the comparison and reports the independent-final results. -- [Frozen plan](plan/plan.json), [driver](plan/run.py) and [RunSpec](plan/runspec.json) identify the exact edits, weights, workload, ordered seeds, evaluation caps, runtime and task file hashes. +- [Frozen plan](plan/plan.json), [archived measurement driver](https://github.com/InferMatrixLab/InferRSI/blob/1921271c99d1fc3532e882c1ce6dd4e3ce6d74a0/evidence/qwen-vats-gvo-910b1-20261124/plan/run.py) and [RunSpec](plan/runspec.json) identify the exact edits, weights, workload, ordered seeds, evaluation caps, runtime and task file hashes. - [Summary](results/summary.json) contains every online result, the shared root and training durations, the before/after weights, group returns and paired aggregates. - [Trial index](results/trials.jsonl) records every physical task evaluation, its selected source hash, verdict, task-result hash and path inside its attempt directory. - [Local archive metadata](results/local-archive.json) gives the size and SHA-256 of the 725-file raw bundle retained in the shared experiment workspace. The public PR provides the compact inputs and results. -- [Candidate sources](patch/README.md) and [verification instructions](validator/README.md) explain how to regenerate the experiment and re-assess its retained measurements. +- [Candidate sources](patch/README.md) and [verification instructions](validator/README.md) explain how to regenerate the experiment with the [current driver](plan/run.py) and re-assess the retained measurements against their durable final decisions. The task uses random BF16 weights, five numerical cases, three latency cases and fixed workload seeds. Each complete evaluation runs two paired AB/BA blocks diff --git a/evidence/qwen-vats-gvo-910b1-20261124/plan/retained-sha256.json b/evidence/qwen-vats-gvo-910b1-20261124/plan/retained-sha256.json index ffc0ecafa..cd79e9295 100644 --- a/evidence/qwen-vats-gvo-910b1-20261124/plan/retained-sha256.json +++ b/evidence/qwen-vats-gvo-910b1-20261124/plan/retained-sha256.json @@ -1,13 +1,13 @@ { - "README.md": "fc04ec6e7fe6e516f0c4a4a271e3d60518932d437751b7f4e6b6dac177c444cb", + "README.md": "e8ae15c859ecbb15a9c2af8da15c26725886940540f4cdaf9c590b94f2002705", "patch/README.md": "e2a3af7bd373d9e1637265f3980fd272cbbc181d455d275794bf66f2cc6f8095", "plan/edits.json": "2975fee42370debd80f627ecd58880e45280968f739ca3045c8b388bc9ab4cbb", "plan/plan.json": "66344b1ca1522fd3b38966c243bbb6ec8eb8fecc203ccd171690463569632013", - "plan/run.py": "7088ba318ac4c9226feab41848963e6a91492d91879c42cca00034ff3178653b", + "plan/run.py": "548c67815bafa10ab2ff05fe4a09596e2ab99dbae28fc526c01bfa50f8f471ff", "plan/runspec.json": "f3f2ab71d227ee3e7d95233cc3d34e17ab41d086f519f558fb3aca5200515404", "results/local-archive.json": "c5df5dc55feb202e53f28d5b6f4ba28913e893277d9deb5e783a472c686d9ae6", "results/summary.json": "df3029cfcaacb91018b6c931017b059b99a80633116586f9446fe82e31986132", "results/trials.jsonl": "6bd39719a5c0d794b1c5e04403b03789ae5ca42e0c9c74694200bdb6355a0756", - "validator/README.md": "d8f127ee5c960a09e4c96bf4e19f6748dbb940afb9852560cc04a596bc340106", - "validator/check.py": "18cfd2d9ddfd339f6450aa64f07ee1afc1120df2b5b4130de11bd868d55d92b4" + "validator/README.md": "8d1ee7e1a6c4c77448f9c5c6cfb9e44bf5c7c3a06c3b6e516ebd2c238e3b5ca5", + "validator/check.py": "7f1ff3de33a1e2ccd48e573a64f3110a1468388643b41bcdbff2311c75171d50" } diff --git a/evidence/qwen-vats-gvo-910b1-20261124/plan/run.py b/evidence/qwen-vats-gvo-910b1-20261124/plan/run.py index 29702610f..89000e0e3 100644 --- a/evidence/qwen-vats-gvo-910b1-20261124/plan/run.py +++ b/evidence/qwen-vats-gvo-910b1-20261124/plan/run.py @@ -3,6 +3,7 @@ import argparse import json import math +import shutil import time from dataclasses import asdict, replace from pathlib import Path @@ -59,6 +60,16 @@ def campaign(control, name, spec, checkpoint, node, cap, reserve): def run(output): output.mkdir(parents=True, exist_ok=False) plan = json.loads((HERE / "plan.json").read_text()) + shutil.copyfile(__file__, output / "driver.py") + summary = { + "driver_sha256": sha256(output / "driver.py"), + "blocks": [ + {"seed": seed, "arm": arm, "status": "not_run", "final_score": None} + for seed, arms in plan["blocks"] + for arm in arms + ], + } + atomic_write_json(output / "summary.json", summary) edits = [Edit(*args) for args in json.loads((HERE / "edits.json").read_text())] frozen = Snapshot(**plan["checkpoint"]) settings = Settings(**plan["settings"]) @@ -124,24 +135,28 @@ def run(output): trained = learn(control, "training", adapter, root, frozen, settings, **training) train_seconds = time.monotonic() - started training_state = control.read("training")["state"] - summary = { - "root_score": root_score, - "root_seconds": root_seconds, - "training_seconds": train_seconds, - "training": training_state["training"], - "training_candidate_checks": sum( - len(r["tree"]["attempts"]) - 1 for r in training_state["searches"].values() - ), - "initial": frozen.document(), - "trained": trained.document(), - "blocks": [], - } + summary.update( + { + "root_score": root_score, + "root_seconds": root_seconds, + "training_seconds": train_seconds, + "training": training_state["training"], + "training_candidate_checks": sum( + len(r["tree"]["attempts"]) - 1 for r in training_state["searches"].values() + ), + "initial": frozen.document(), + "trained": trained.document(), + } + ) atomic_write_json(output / "summary.json", summary) - for seed, arms in plan["blocks"]: - for arm in arms: - name = f"{arm}-{seed}" - print(f"Starting {name}", flush=True) - started = time.monotonic() + for row in summary["blocks"]: + arm, seed = row["arm"], row["seed"] + name = f"{arm}-{seed}" + row["status"] = "incomplete" + atomic_write_json(output / "summary.json", summary) + print(f"Starting {name}", flush=True) + started = time.monotonic() + try: if arm == "serial": selected, best, final, evaluations = serial( evaluator, @@ -150,7 +165,12 @@ def run(output): root_score, output / name, ) - candidates_checked = len(evaluations) + row.update( + selected=selected, + search_score=best, + candidate_checks=len(evaluations), + final_verdict=final["verdict"], + ) else: snapshot = trained if arm == "trained" else frozen campaign(control, name, spec, snapshot, node, settings.search_ms + cap + 30000, cap) @@ -167,7 +187,9 @@ def run(output): winner = search_tree.accept(state) selected = names[sha256(adapter.path(winner) / "candidate.py")] best = search_tree.evidence(state, winner.candidate_id)["evaluation"].objective - candidates_checked = len(state.attempts) - 1 + row.update( + selected=selected, search_score=best, candidate_checks=len(state.attempts) - 1 + ) final_record = finalize( control, name, @@ -176,21 +198,23 @@ def run(output): holdout_scope=name, accept_result=lambda _: True, ) - final = final_record["results"][0] + row.update(final_verdict=final_record["verdict"], promoted=final_record["promoted"]) + if ( + final_record["verdict"] != "PASS" + or final_record["promoted"] is not True + or final_record["candidate"] != winner.candidate_id + ): + raise ValueError( + f"Search final did not promote the selected candidate for {name}" + ) + (final,) = final_record["results"] if final["verdict"] != "PASS" or final["cleanup_verified"] is not True: raise ValueError(f"Incomplete final for {name}") - row = { - "seed": seed, - "arm": arm, - "selected": selected, - "search_score": best, - "candidate_checks": candidates_checked, - "final_score": final["objective"], - "online_seconds": time.monotonic() - started, - } - summary["blocks"].append(row) + row.update(status="complete", final_score=final["objective"]) + finally: + row["online_seconds"] = time.monotonic() - started atomic_write_json(output / "summary.json", summary) - print(json.dumps(row), flush=True) + print(json.dumps(row), flush=True) for numerator, denominator in ( ("trained", "frozen"), ("trained", "serial"), diff --git a/evidence/qwen-vats-gvo-910b1-20261124/validator/README.md b/evidence/qwen-vats-gvo-910b1-20261124/validator/README.md index e2c0f0f8b..d88ff9166 100644 --- a/evidence/qwen-vats-gvo-910b1-20261124/validator/README.md +++ b/evidence/qwen-vats-gvo-910b1-20261124/validator/README.md @@ -5,7 +5,8 @@ owns every input, five correctness cases and the three timing cases. Its `optimization.py` checks source identities, per-step trajectory error, sample counts, timing medians and the geometric mean. Every candidate and final is compared with the original denoiser. A final requires a fresh evaluation, a -complete numerical result and verified process cleanup. +complete numerical result and verified process cleanup. The VATS final also +requires a durable overall PASS and promotion of the selected candidate. Hardware replay uses the command and environment in the [design document](../../../docs/speculative-search.md#measuring-the-contribution-of-gvo). @@ -14,10 +15,20 @@ between replays. The trial index records the actual files from this run. For offline re-assessment with the original raw files, run the following from the checkout with the directory produced by the hardware run and its retained -initialization failure. The checker repeats the packaged assessment, binds final -score and selected source to the run summary, and prints the compact trial index. +initialization failure. The checker verifies the frozen protocol hash before +running it in the isolated protocol process. It compares each VATS final claim, +promoted incumbent and settled receipt with the selected candidate and the +retained measurement, then checks all nine independent finals and paired ratios +before printing the trial index. ```bash PYTHONPATH=src python evidence/qwen-vats-gvo-910b1-20261124/validator/check.py \ ``` + +The original RunSpec pins the driver that produced the measurements. When using +the unpacked original archive, preserve its `config/run.py` next to the +`output-allocated/` and `output/` trees. If those directories have moved, pass +`--historical-driver /path/to/config/run.py`. The current driver retains its +own bytes in each new trial directory. Its summary also records incomplete +and unrun arms, which prevent publication of paired results. diff --git a/evidence/qwen-vats-gvo-910b1-20261124/validator/check.py b/evidence/qwen-vats-gvo-910b1-20261124/validator/check.py index cc20c7e94..a27d7a27a 100644 --- a/evidence/qwen-vats-gvo-910b1-20261124/validator/check.py +++ b/evidence/qwen-vats-gvo-910b1-20261124/validator/check.py @@ -4,12 +4,12 @@ import hashlib import json import math -import runpy from collections import Counter from pathlib import Path from statistics import geometric_mean from inferrsi.benchmark.registry import task_resources +from inferrsi.harness import candidates, task_protocol from inferrsi.harness.run_store import RunStoreReader from inferrsi.harness.work_control import WorkControl @@ -33,26 +33,88 @@ def close(left, right): return math.isclose(left, right, rel_tol=1e-12, abs_tol=1e-12) -def main(completed, failed): +def promoted_final(doc, campaign, candidate_root): + final = doc["final_result"] + claim = doc["final_claim"] + require( + final and final.get("verdict") == "PASS" and final.get("promoted") is True, + f"{campaign}: missing completed final promotion", + ) + require(claim and claim["scope"] == campaign, f"{campaign}: final claim scope") + candidate = claim["binding"]["candidate"] + require( + candidate == doc["state"]["stable"] + and candidate["candidate_id"] == final["candidate"] + and final["candidate"] == doc["state"]["searches"][campaign]["selected"], + f"{campaign}: promoted candidate differs from selection", + ) + ops = [op for op in doc["operations"].values() if op["category"] == "final"] + require( + len(ops) == 1 + and ops[0]["status"] == "settled" + and ops[0]["binding"]["candidate"] == candidate + and final["results"] == [ops[0]["receipt"]], + f"{campaign}: final receipt mismatch", + ) + candidate_id = candidate["candidate_id"] + require( + Path(candidate_id).name == candidate_id and candidate_id not in ("", ".", ".."), + f"{campaign}: invalid promoted candidate path", + ) + source = candidate_root / candidate_id + require( + candidates.identity(source) == candidate["artifact_digest"], + f"{campaign}: promoted artifact differs from the retained candidate", + ) + return ops[0]["receipt"], sha(source / "candidate.py") + + +def main(completed, failed, historical_driver=None): + spec = read(PLAN / "runspec.json") plan = read(PLAN / "plan.json") task = Path(str(task_resources(plan["task"]))) - protocol = runpy.run_path(str(task / "optimization.py")) - settings = protocol["validate_arguments"](plan["sampling"]) - spec = read(PLAN / "runspec.json") - for name, expected_hash in spec["input_sha256"].items(): - require(sha(PLAN.parent / name) == expected_hash, name) for name, expected_hash in spec["packaged_task_sha256"].items(): require(sha(task / name) == expected_hash, name) summary = read(completed / "summary.json") + for name, expected_hash in spec["input_sha256"].items(): + path = PLAN.parent / name + if name == "plan/run.py": + if "driver_sha256" in summary: + path = completed / "driver.py" + expected_hash = sha(PLAN / "run.py") + require(summary["driver_sha256"] == expected_hash, "Replay driver identity") + else: + # The original archive includes the driver used for the published measurements. + path = historical_driver or completed.parent.parent / "config/run.py" + require(sha(path) == expected_hash, name) + protocol_hash = spec["packaged_task_sha256"]["optimization.py"] + settings = task_protocol.run( + task, protocol_hash, "validate_arguments", arguments=plan["sampling"] + ) expected = [(arm, seed) for seed, arms in plan["blocks"] for arm in arms] rows = {(item["arm"], item["seed"]): item for item in summary["blocks"]} require(list(rows) == expected and len(rows) == len(summary["blocks"]), "Deployment order") + if "driver_sha256" in summary: + require( + all( + row["status"] == "complete" + and row.get("final_verdict") == "PASS" + and (arm == "serial" or row.get("promoted") is True) + for (arm, _), row in rows.items() + ), + "Incomplete deployment blocks", + ) control = WorkControl(RunStoreReader(completed / "store", "speculative")) campaigns = ["training", *(f"{a}-{s}" for a, s in expected if a != "serial")] labels = {} + final_receipts = {} for campaign in campaigns: doc = control.read(campaign) require(not doc["blocked"], campaign) + if campaign != "training": + final_receipts[campaign] = promoted_final( + doc, campaign, completed / "artifacts/candidates" + ) for operation, op in doc["operations"].items(): require(op["status"] == "settled", operation) if op["category"] in ("root", "verify", "final"): @@ -90,7 +152,14 @@ def main(completed, failed): folder = measure.parents[1] raw = read(measure) execution = read(folder / "execution/execution.json") - assessment = protocol["assess"](folder, settings, execution) + assessment = task_protocol.run( + task, + protocol_hash, + "assess", + directory=folder, + settings=settings, + execution=execution, + ) require(assessment == read(folder / "assessment.json"), "Retained task assessment") relative = folder.relative_to(root) source_hash = sha(folder / "candidate.py") @@ -114,6 +183,18 @@ def main(completed, failed): "Task quality or cleanup", ) if phase == "final": + if owner in final_receipts: + receipt, promoted_source_sha = final_receipts[owner] + require( + source_hash == promoted_source_sha + and receipt["assessment_sha256"] == sha(folder / "assessment.json") + and receipt["execution_sha256"] + == sha(folder / "execution/execution.json") + and receipt["verdict"] == assessment["verdict"] + and receipt["cleanup_verified"] is True + and close(receipt["objective"], assessment["score"]), + f"{owner}: final evidence differs from the promoted receipt", + ) arm, seed = owner.split("-") row = rows[arm, int(seed)] require( @@ -165,5 +246,6 @@ def main(completed, failed): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("completed", type=Path) parser.add_argument("failed", type=Path) + parser.add_argument("--historical-driver", type=Path, help="Original archive config/run.py") args = parser.parse_args() - main(args.completed.resolve(), args.failed.resolve()) + main(args.completed.resolve(), args.failed.resolve(), args.historical_driver) diff --git a/src/inferrsi/apps/workflows/search_benchmarks.py b/src/inferrsi/apps/workflows/search_benchmarks.py index b596888c5..b707f57d8 100644 --- a/src/inferrsi/apps/workflows/search_benchmarks.py +++ b/src/inferrsi/apps/workflows/search_benchmarks.py @@ -138,9 +138,14 @@ def __init__(self, comparator, *, split, policy, token_cap, task_count): ): raise ValueError("Skill utility must be exactly representable within declared bounds") self.comparator, self.split, self.policy = comparator, split, policy + self.experiment_identity = comparator.identity + self.final_only = split == "holdout" self.holdout_scope = ( getattr(comparator, "holdout_scope", None) if split == "holdout" else None ) + self.previous_holdout_scope_prefix = getattr( + comparator, "previous_holdout_scope_prefix", None + ) self.token_cap, self.task_count = token_cap, task_count self.identity = digest( { diff --git a/src/inferrsi/harness/gvo.py b/src/inferrsi/harness/gvo.py index 1fa865037..ef802256d 100644 --- a/src/inferrsi/harness/gvo.py +++ b/src/inferrsi/harness/gvo.py @@ -120,7 +120,7 @@ def train_group( records = tuple(records) if any(record["snapshot"] != digest(snapshot.document()) for record in records): raise ValueError("Training checkpoint differs from collection checkpoint") - doc = control.read(campaign) + doc = control.recover_unstarted(campaign) if doc["final_claim"] is not None: raise ValueError("Training is closed before final evaluation") if digest(doc["state"]["checkpoint"]) != digest(snapshot.document()): diff --git a/src/inferrsi/harness/search_adapter.py b/src/inferrsi/harness/search_adapter.py index 882bceae5..ad4580641 100644 --- a/src/inferrsi/harness/search_adapter.py +++ b/src/inferrsi/harness/search_adapter.py @@ -18,10 +18,26 @@ from .artifact_primitives import digest, sha256 from .atomic_json import atomic_write_json from .optimization_session import runtime_environment +from .search_records import encode from .trials.external import execute_trial from .verification_plans import Job +def development_binding(adapter): + """Bind search evidence to the evaluator's frozen experiment, when supplied.""" + evaluator = getattr(adapter, "evaluator", None) + if getattr(evaluator, "final_only", False): + raise ValueError("A holdout-bound evaluator can only run during final validation") + identity = getattr(evaluator, "experiment_identity", None) + return {} if identity is None else {"experiment_identity": identity} + + +def validate_development_evidence(state, binding): + if binding and any(attempt.experiment_identity != binding["experiment_identity"] + for attempt in state.attempts): + raise ValueError("Imported evidence belongs to another development experiment") + + @dataclass(frozen=True) class Edit: token: str @@ -152,7 +168,7 @@ def draft(self, parent, state, snapshot, rng, operation, deadline): digest(snapshot.document()), self.identity, digest(asdict(parent)), - digest([asdict(r) for r in state.receipts]), + digest([encode(r) for r in state.receipts]), ), parent.dependencies, ) diff --git a/src/inferrsi/harness/search_campaign.py b/src/inferrsi/harness/search_campaign.py index bf9c205db..2047d0da7 100644 --- a/src/inferrsi/harness/search_campaign.py +++ b/src/inferrsi/harness/search_campaign.py @@ -8,17 +8,20 @@ from . import search_tree as tree from .artifact_primitives import digest from .gvo import train_group +from .search_adapter import development_binding, validate_development_evidence from .search_records import decode, encode from .vats import Search def qualify_root(control, campaign, adapter, root, contract): """Acquire root evidence using real checks and the same authoritative accounting.""" - retained = control.read(campaign)["state"].get("qualified_root") + experiment = development_binding(adapter) + retained = control.recover_unstarted(campaign)["state"].get("qualified_root") if retained is not None: state = decode(retained) if state.contract != contract or tree.candidate(state, state.root_id) != root: raise ValueError("Root qualification is bound to another frozen context") + validate_development_evidence(state, experiment) tree.accept(state) return state receipts = [] @@ -29,6 +32,7 @@ def qualify_root(control, campaign, adapter, root, contract): "context": asdict(contract.context), "check": check, "adapter": adapter.identity, + **experiment, } operation = "root-" + digest(binding) previous = doc["operations"].get(operation) @@ -44,7 +48,11 @@ def qualify_root(control, campaign, adapter, root, contract): ) claim = control.launch(campaign, operation) result = adapter.verify(root, check, operation, claim["deadline"]) + elif previous["status"] == "claimed": + raise ValueError("Root reservation still awaits launch") elif previous["status"] == "settled": + if "started_at" not in previous: + raise ValueError("Root qualification did not start") result = previous["receipt"] else: result = adapter.recover(operation, {"kind": "verify", "binding": binding}) @@ -74,6 +82,7 @@ def qualify_root(control, campaign, adapter, root, contract): check, contract.context, root.dependencies, + **experiment, ) receipts.append( tree.Receipt(attempt, Verdict.PASS, result["evidence_digest"], result.get("objective")) @@ -123,7 +132,7 @@ def collect_group( ): if type(size) is not int or size < 2: raise ValueError("A group needs two or more independent searches") - doc = control.read(campaign) + doc = control.recover_unstarted(campaign) outstanding = sum( max( 0, @@ -202,6 +211,8 @@ def learn( if len(retained) != 1 or retained[0]["status"] != "settled": raise ValueError("Unknown training outcome; no second update is permitted") op = retained[0] + if "started_at" not in op: + raise ValueError("Group training did not start") expected = { "learning_rate": learning_rate, "expense_weight": 0.0, @@ -249,7 +260,7 @@ def finalize(control, campaign, adapter, selected, *, holdout_scope, accept_resu accept_result is a fixed independent final gate. Only this function records stable promotion; search acceptance and trained checkpoints do not promote. """ - doc = control.read(campaign) + doc = control.recover_unstarted(campaign) if doc["final_result"] is not None: return doc["final_result"] validate_final = getattr(adapter.evaluator, "validate_final", None) @@ -258,6 +269,14 @@ def finalize(control, campaign, adapter, selected, *, holdout_scope, accept_resu required_scope = getattr(adapter.evaluator, "holdout_scope", None) if required_scope is not None and holdout_scope != required_scope: raise ValueError("Final scope must match the evaluator's frozen holdout data identity") + experiment = getattr(adapter.evaluator, "experiment_identity", None) + if experiment is not None: + histories = ( + [op["binding"] for op in doc["operations"].values() if op["category"] == "root"] + + list(doc["state"].get("searches", {}).values()) + ) + if any(record.get("experiment_identity") != experiment for record in histories): + raise ValueError("Final evaluator must match the frozen development experiment") binding = { "candidate": asdict(selected), "adapter": adapter.identity, @@ -269,7 +288,10 @@ def finalize(control, campaign, adapter, selected, *, holdout_scope, accept_resu if digest(doc["final_claim"]["binding"]) != digest(binding): raise ValueError("Cannot change the final candidate or validation scope") else: - control.claim_final(campaign, doc["revision"], holdout_scope, binding) + control.claim_final( + campaign, doc["revision"], holdout_scope, binding, + previous_scope_prefix=getattr(adapter.evaluator, "previous_holdout_scope_prefix", None), + ) results = [] for check, cap in adapter.checks.items(): doc = control.read(campaign) @@ -278,8 +300,18 @@ def finalize(control, campaign, adapter, selected, *, holdout_scope, accept_resu op = doc["operations"].get(operation) if op is None: raise ValueError("Interrupted holdout may be consumed; no new execution") + if op["status"] == "claimed": + raise ValueError("Final reservation still awaits launch") if op["status"] == "settled": result = op["receipt"] + if "started_at" not in op: + result = { + **result, + "verdict": "INCONCLUSIVE", + "objective": None, + "elapsed_ms": 0, + "cleanup_verified": True, + } else: result = adapter.recover( operation, diff --git a/src/inferrsi/harness/search_continuations.py b/src/inferrsi/harness/search_continuations.py index 665243f7c..533a76686 100644 --- a/src/inferrsi/harness/search_continuations.py +++ b/src/inferrsi/harness/search_continuations.py @@ -17,6 +17,7 @@ def continue_from(search, step_index, action_key, search_id, *, copy_cap_ms, see Copying is charged; all suffix edits/checks use ordinary WorkControl claims. No holdout or unobserved outcome is used. The entire suffix is Scorer-only. """ + search.control.recover_unstarted(search.campaign) doc, source, _ = search.read() if doc["final_claim"] is not None or source["status"] != "complete": raise ValueError("Continuation source must be a completed development search") diff --git a/src/inferrsi/harness/search_records.py b/src/inferrsi/harness/search_records.py index 343ad49d9..9e59bdabd 100644 --- a/src/inferrsi/harness/search_records.py +++ b/src/inferrsi/harness/search_records.py @@ -7,8 +7,12 @@ from . import search_tree as tree -def encode(state): - return asdict(state) +def encode(record): + # Preserve existing wire identities for evaluators without an experiment binding. + def retained(fields): + return {k: v for k, v in fields if k != "experiment_identity" or v is not None} + + return asdict(record, dict_factory=retained) def initial_conditions(record): diff --git a/src/inferrsi/harness/search_tree.py b/src/inferrsi/harness/search_tree.py index e2fa8fe42..b6f343282 100644 --- a/src/inferrsi/harness/search_tree.py +++ b/src/inferrsi/harness/search_tree.py @@ -135,11 +135,14 @@ class Attempt: check_id: str context: Context dependencies: tuple[Dependency, ...] = () + experiment_identity: str | None = None def __post_init__(self): for name in ("attempt_id", "execution_key", "reservation_id", "lease_id", "search_id", "candidate_id", "check_id"): _text(getattr(self, name)) + if self.experiment_identity is not None: + _text(self.experiment_identity) _sha(self.artifact_digest) if not isinstance(self.context, Context): raise ValueError("Expected verification context") diff --git a/src/inferrsi/harness/vats.py b/src/inferrsi/harness/vats.py index 662882601..59155ce4f 100644 --- a/src/inferrsi/harness/vats.py +++ b/src/inferrsi/harness/vats.py @@ -14,6 +14,7 @@ from . import search_tree as tree from .artifact_primitives import digest +from .search_adapter import development_binding, validate_development_evidence from .search_records import decode, encode, initial_conditions from .verification_plans import completion_menu @@ -213,6 +214,7 @@ class Search: """ def __init__(self, control, campaign, search_id, snapshot, settings, adapter): + self.experiment = development_binding(adapter) self.control, self.campaign, self.search_id = control, campaign, search_id self.snapshot, self.settings, self.adapter = snapshot, settings, adapter self.snapshot_id = digest(snapshot.document()) @@ -226,6 +228,7 @@ def read(self): search["snapshot"] != self.snapshot_id or search["settings"] != self.settings_id or search["adapter"] != self.adapter.identity + or search.get("experiment_identity") != self.experiment.get("experiment_identity") ): raise ValueError("Frozen search roles, menu or adapter changed") return document, search, decode(search["tree"]) @@ -236,6 +239,7 @@ def start(self, initial, *, seed, collect=True, forced=False): or initial.contract.improver_snapshot != self.snapshot_id ): raise ValueError("Search root must bind the frozen search and improver") + validate_development_evidence(initial, self.experiment) tree.accept(initial) if not forced and len(initial.candidates) != 1: raise ValueError("Independent natural searches must start from one qualified root") @@ -243,7 +247,7 @@ def start(self, initial, *, seed, collect=True, forced=False): def observation(attempt): return { key: value - for key, value in asdict(attempt).items() + for key, value in encode(attempt).items() if key not in ("attempt_id", "search_id") } @@ -253,7 +257,7 @@ def observation(attempt): "dependencies": [asdict(item) for item in initial.dependencies], "attempts": [observation(a) for a in initial.attempts], "receipts": [ - {**asdict(r), "attempt": observation(r.attempt)} for r in initial.receipts + {**encode(r), "attempt": observation(r.attempt)} for r in initial.receipts ], } ) @@ -267,6 +271,7 @@ def transition(state): "snapshot": self.snapshot_id, "settings": self.settings_id, "adapter": self.adapter.identity, + **self.experiment, "seed": seed, "collect": collect, "forced": forced, @@ -377,12 +382,13 @@ def transition(state): self.control.update(self.campaign, doc["revision"], transition) def recover(self): + self.control.recover_unstarted(self.campaign) doc, search, _ = self.read() pending = search["pending"] if pending is None: return operation = doc["operations"][pending["operation"]] - not_started = operation["receipt"] == {"not_started": "deadline"} + not_started = operation["status"] == "settled" and "started_at" not in operation if operation["status"] == "settled" and (not_started or doc["blocked"]): # The durable control record is conclusive. Unstarted work has no # adapter receipt, and blocked work cannot contribute its output. @@ -426,6 +432,8 @@ def archive(state): else: self.control.update(self.campaign, doc["revision"], archive) return + if operation["status"] == "claimed": + raise ValueError("Unstarted reservation still awaits launch") # All uncertain launches still require an adapter receipt. Never call # draft/verify from recovery, including after launch-intent crashes. result = self.adapter.recover(pending["operation"], pending) @@ -473,6 +481,7 @@ def pending(state): check.check_id, current.contract.context, node.dependencies, + **self.experiment, ) current = tree.register_attempt( current, attempt, expected_revision=current.revision diff --git a/src/inferrsi/harness/work_control.py b/src/inferrsi/harness/work_control.py index d82a7d989..f4f1c0dac 100644 --- a/src/inferrsi/harness/work_control.py +++ b/src/inferrsi/harness/work_control.py @@ -42,6 +42,10 @@ def __init__(self, store): "CREATE TABLE IF NOT EXISTS work_holdouts " "(scope TEXT PRIMARY KEY, campaign TEXT NOT NULL, binding TEXT NOT NULL)" ) + db.execute( + "CREATE TABLE IF NOT EXISTS work_holdout_aliases " + "(previous_scope TEXT PRIMARY KEY, scope TEXT NOT NULL)" + ) @contextmanager def _transaction(self, campaign, revision=None): @@ -178,32 +182,59 @@ def reconcile_blocked(self, campaign, revision, operation, transition): body["state"] = transition(body["state"]) return self.read(campaign) + @staticmethod + def _stop_reason(body, op, now): + if body["blocked"]: + return "blocked" + if now >= op["deadline"]: + return "deadline" + return None + + @staticmethod + def _release_unstarted(db, campaign, body, operation, reason, now): + body["operations"][operation].update( + status="settled", actual_ms=0, receipt={"not_started": reason} + ) + db.execute( + "DELETE FROM work_domains WHERE campaign=? AND operation=?", (campaign, operation) + ) + body["events"].append({"kind": "not_started", "operation": operation, "at": now}) + + def recover_unstarted(self, campaign): + """Release stopped reservations only while their launch intent is still absent.""" + doc = self.read(campaign) + now = time.time() + if not any( + op["status"] == "claimed" and self._stop_reason(doc, op, now) + for op in doc["operations"].values() + ): + return doc + with self._transaction(campaign) as (db, body): + now = time.time() + for name, op in body["operations"].items(): + if op["status"] == "claimed" and (reason := self._stop_reason(body, op, now)): + self._release_unstarted(db, campaign, body, name, reason, now) + return self.read(campaign) + def launch(self, campaign, operation): """Persist intent before external launch. A crash here must never relaunch.""" - expired = False with self._transaction(campaign) as (db, body): op = body["operations"][operation] if op["status"] != "claimed": - raise ValueError("Execution intent already recorded") - expired = time.time() >= op["deadline"] - if expired: - op.update(status="settled", actual_ms=0, receipt={"not_started": "deadline"}) - db.execute( - "DELETE FROM work_domains WHERE campaign=? AND operation=?", - (campaign, operation), - ) + raise ValueError("Operation is no longer awaiting launch") + now = time.time() + reason = self._stop_reason(body, op, now) + if reason: + self._release_unstarted(db, campaign, body, operation, reason, now) else: - op.update(status="launched", started_at=time.time()) - body["events"].append( - { - "kind": "not_started" if expired else "launch_intent", - "operation": operation, - "at": time.time(), - } - ) - if expired: + op.update(status="launched", started_at=now) + body["events"].append({"kind": "launch_intent", "operation": operation, "at": now}) + # Signal refusal after the transaction has committed the release. + if reason == "blocked": + raise ValueError("Campaign blocked before launch") + if reason == "deadline": raise ValueError("Deadline expired before launch") - return self.read(campaign)["operations"][operation] + return op def settle(self, campaign, operation, receipt, *, actual_ms, cleanup_verified, transition=None): """Only authenticated, owner-scoped receipts may be passed by the adapter. @@ -261,7 +292,26 @@ def settle(self, campaign, operation, receipt, *, actual_ms, cleanup_verified, t ) return self.read(campaign) - def claim_final(self, campaign, revision, scope, binding): + def bind_consumed_holdout(self, previous_scope, resolve_scope): + """Resolve a consumed claim from archived inputs without reopening a final.""" + with self.store._connection() as db, _transaction(db): + if not db.execute( + "SELECT 1 FROM work_holdouts WHERE scope=?", (previous_scope,) + ).fetchone(): + raise ValueError("Previous holdout scope has not been consumed") + scope = resolve_scope() + alias = db.execute( + "SELECT scope FROM work_holdout_aliases WHERE previous_scope=?", (previous_scope,) + ).fetchone() + if alias is None: + db.execute( + "INSERT INTO work_holdout_aliases VALUES (?, ?)", (previous_scope, scope) + ) + elif alias[0] != scope: + raise ValueError("Consumed holdout scope already maps to another identity") + return scope + + def claim_final(self, campaign, revision, scope, binding, *, previous_scope_prefix=None): """Consume a shared holdout scope before any access, including interrupted access.""" if not isinstance(scope, str) or not scope.strip(): raise ValueError("Holdout scope must have a nonempty identity") @@ -274,7 +324,22 @@ def claim_final(self, campaign, revision, scope, binding): raise ValueError("Final is already claimed or work remains unresolved") if time.time() >= body["deadline"]: raise ValueError("Final deadline exhausted") - if db.execute("SELECT 1 FROM work_holdouts WHERE scope=?", (scope,)).fetchone(): + if previous_scope_prefix is not None: + unmapped = db.execute( + "SELECT h.scope FROM work_holdouts h LEFT JOIN work_holdout_aliases a " + "ON h.scope=a.previous_scope WHERE a.previous_scope IS NULL " + "AND substr(h.scope, 1, length(?))=? LIMIT 1", + (previous_scope_prefix, previous_scope_prefix), + ).fetchone() + if unmapped is not None: + raise ValueError( + f"Unmapped legacy holdout {unmapped[0]}. " + "Bind its verified archived input before claiming a new scope." + ) + if db.execute( + "SELECT 1 FROM work_holdouts WHERE scope=? UNION ALL " + "SELECT 1 FROM work_holdout_aliases WHERE scope=? LIMIT 1", (scope, scope) + ).fetchone(): raise ValueError("Holdout scope already consumed in this RunStore") db.execute( "INSERT INTO work_holdouts VALUES (?, ?, ?)", (scope, campaign, _json(binding)) diff --git a/tests/test_qwen_gvo_diagnostic.py b/tests/test_qwen_gvo_diagnostic.py new file mode 100644 index 000000000..53b2d8e54 --- /dev/null +++ b/tests/test_qwen_gvo_diagnostic.py @@ -0,0 +1,104 @@ +"""Control-level finals and protocol identity for the Qwen pilot, without device access.""" + +import importlib.util +import json +import shutil +from pathlib import Path + +import pytest + +from inferrsi.benchmark.registry import task_resources + +BUNDLE = Path(__file__).resolve().parents[1] / "evidence/qwen-vats-gvo-910b1-20261124" + + +def module(relative): + spec = importlib.util.spec_from_file_location("diagnostic", BUNDLE / relative) + result = importlib.util.module_from_spec(spec) + spec.loader.exec_module(result) + return result + + +@pytest.mark.parametrize("verdict,promoted", [("INCONCLUSIVE", False), ("PASS", False)]) +def test_inner_pass_cannot_publish_failed_promotion(tmp_path, monkeypatch, verdict, promoted): + driver = module("plan/run.py") + + class Evaluator: + def __init__(self, task, frozen, *, arguments): + self.frozen = frozen + self.identity = "cpu-test" + frozen.mkdir() + shutil.copyfile(task_resources(task) / "baseline.py", frozen / "baseline.py") + + def evaluate(self, candidate, check, output, deadline): + output.mkdir(parents=True) + return {"verdict": "PASS", "objective": 1.25, "cleanup_verified": True} + + original = driver.finalize + + def refused(*args, **kwargs): + final = original(*args, **kwargs) + return {**final, "verdict": verdict, "promoted": promoted} + + monkeypatch.setattr(driver, "EngineEvaluator", Evaluator) + monkeypatch.setattr(driver, "finalize", refused) + output = tmp_path / "trial" + with pytest.raises(ValueError, match="final"): + driver.run(output) + summary = json.loads((output / "summary.json").read_text()) + first, rejected, *unrun = summary["blocks"] + assert first["status"] == "complete" + assert rejected["status"] == "incomplete" and rejected["final_score"] is None + assert rejected["final_verdict"] == verdict and rejected["promoted"] is False + assert all(row["status"] == "not_run" for row in unrun) + assert "trained_over_frozen" not in summary + + +def test_protocol_identity_checked_before_loading(tmp_path, monkeypatch): + checker = module("validator/check.py") + task = tmp_path / "task" + shutil.copytree(task_resources("qwen21-denoise-npu"), task) + (task / "optimization.py").write_text("this is deliberately invalid Python source.") + monkeypatch.setattr(checker, "task_resources", lambda _: task) + with pytest.raises(ValueError, match="optimization.py"): + checker.main(tmp_path / "trial", tmp_path / "failed") + + +@pytest.mark.parametrize("fault", ["verdict", "promotion", "selection", "receipt", "source"]) +def test_offline_checker_requires_bound_durable_promotion(tmp_path, fault): + from inferrsi.harness import candidates + + checker = module("validator/check.py") + candidate_dir = tmp_path / "winner" + candidate_dir.mkdir() + source = candidate_dir / "candidate.py" + source.write_bytes(b"selected candidate") + node = {"candidate_id": "winner", "artifact_digest": candidates.identity(candidate_dir)} + receipt = {"verdict": "PASS", "cleanup_verified": True, "objective": 1.2} + final = {"candidate": "winner", "verdict": "PASS", "promoted": True, "results": [receipt]} + op = { + "category": "final", + "status": "settled", + "binding": {"candidate": node}, + "receipt": receipt, + } + state = {"stable": node, "searches": {"frozen-101": {"selected": "winner"}}} + doc = { + "final_result": final, + "final_claim": {"scope": "frozen-101", "binding": {"candidate": node}}, + "state": state, + "operations": {"final-1": op}, + } + assert checker.promoted_final(doc, "frozen-101", tmp_path) == (receipt, checker.sha(source)) + if fault == "verdict": + final["verdict"] = "INCONCLUSIVE" + elif fault == "promotion": + final["promoted"] = False + elif fault == "selection": + state["searches"]["frozen-101"]["selected"] = "root" + elif fault == "receipt": + op["receipt"] = {**receipt, "objective": 1.3} + else: + source.write_bytes(b"other candidate") + with pytest.raises(ValueError, match="frozen-101"): + checker.promoted_final(doc, "frozen-101", tmp_path) diff --git a/tests/test_search_benchmarks.py b/tests/test_search_benchmarks.py index 5a2c6d76a..992446704 100644 --- a/tests/test_search_benchmarks.py +++ b/tests/test_search_benchmarks.py @@ -1,19 +1,24 @@ import json import sys import time +from dataclasses import replace +from pathlib import Path from types import SimpleNamespace import pytest from inferrsi.apps.cli.arguments import parser from inferrsi.apps.workflows.search_benchmarks import EngineEvaluator, SkillEvaluator +from inferrsi.apps.workflows.search_roles import Snapshot from inferrsi.apps.workflows.speculative_search import command from inferrsi.benchmark.improvement import SelectionPolicy +from inferrsi.harness.artifact_primitives import digest from inferrsi.harness.run_store import RunStore from inferrsi.harness.search_adapter import ArtifactAdapter, Edit -from inferrsi.harness.search_campaign import finalize, qualify_root +from inferrsi.harness.search_campaign import finalize, import_qualified_root, learn, qualify_root from inferrsi.harness.task_protocol import TaskProtocolError from inferrsi.harness.trials.external import execute_trial +from inferrsi.harness.vats import Search, Settings from inferrsi.harness.work_control import WorkControl from tests.fixtures.speculative_search import contract @@ -335,3 +340,274 @@ def descriptor(split): assert git(repo, "rev-parse", "HEAD") == head_before assert git(repo, "status", "--porcelain") == "" json.dumps(bound.config) + + +@pytest.fixture +def skill_splits(tmp_path): + from tests.fixtures.harness_improvement_demo import configuration + from tools import speculative_skill + + config, repo = configuration(tmp_path) + comparison = {k: config[k] for k in ("skill", "skill_root", "model", "limits")} + comparison.update(mode="update", client={"explicit": "preparation-only-test"}, + snapshots={"baseline": {"git_revision": config["baseline"]}}) + + def bind(name, dev, holdout, *, declared=None, pretty=False): + directory = tmp_path / name + directory.mkdir() + splits = {} + for split, cases in (("development", dev), ("holdout", holdout)): + path = directory / (split + ".json") + path.write_text(json.dumps(cases, sort_keys=pretty, indent=2 if pretty else None)) + splits[split] = {"path": str(path), "scope": name + split, + "sha256": speculative_skill.sha256(path), "task_count": len(cases), + "task_set_sha256": speculative_skill.task_set_sha256(cases)} + if declared: + splits["holdout"]["task_set_sha256"] = declared + return speculative_skill.Comparator( + repo, {"comparison": comparison, "allowed_paths": config["allowed_paths"], + "splits": splits}, directory / "baseline") + + return bind, config["development"], config["holdout"] + + +def _skill_adapter(directory, comparator, split): + evaluator = SkillEvaluator( + comparator, split=split, policy=SelectionPolicy(1, 0), token_cap=1000, + task_count=comparator.config["splits"][split]["task_count"], + ) + return ArtifactAdapter( + directory, [Edit("edit", "selected/SKILL.md", "old", "new")], + {"evaluation": 3000}, evaluator, domain="skill-test", + ) + + +def _skill_final(tmp_path, comparator, name): + adapter = _skill_adapter(tmp_path / name / "artifacts", comparator, "holdout") + source = comparator.export_root(tmp_path / name / "source") + root = adapter.import_root(source, snapshot="test-snapshot", context={}) + control = WorkControl(RunStore(tmp_path / "store", "skill-test")) + control.create(name, {}, {}, cap_ms=10000, final_reserve_ms=3000, deadline=time.time() + 10) + + def run(): + return finalize(control, name, adapter, root, holdout_scope=comparator.holdout_scope, + accept_result=lambda results: True) + + return control, run + + +@pytest.mark.parametrize("fault", ["id-overlap", "content-overlap", "wrong-set", "changed-file"]) +def test_skill_final_validates_actual_splits_only_after_claim( + tmp_path, monkeypatch, skill_splits, fault +): + from tools import speculative_skill + + bind, dev, holdout = skill_splits + if fault == "id-overlap": + holdout[0]["id"] = dev[0]["id"] + if fault == "content-overlap": + holdout[0] = {**dev[0], "id": "renamed", "split": "holdout", "provenance": "relabeled"} + bound = bind("first", dev, holdout, declared="0" * 64 if fault == "wrong-set" else None) + control, run = _skill_final(tmp_path, bound, "first") + source = Path(bound.config["splits"]["holdout"]["path"]) + if fault == "changed-file": + source.write_bytes(source.read_bytes() + b" ") + read_bytes = Path.read_bytes + accesses = [] + + def read(path): + if path == source: + assert control.read("first")["final_claim"]["scope"] == bound.holdout_scope + accesses.append(path) + return read_bytes(path) + + monkeypatch.setattr(Path, "read_bytes", read) + monkeypatch.setattr(speculative_skill.compare, "prepare", + lambda *args: pytest.fail("dispatched")) + with pytest.raises(ValueError, match="overlap|task set|split changed"): + run() + assert accesses == [source] + assert control.read("first")["final_claim"] is not None + + +def test_skill_final_reformatted_scope_and_single_read(tmp_path, monkeypatch, skill_splits): + from tools import speculative_skill + + bind, dev, holdout = skill_splits + first = bind("first", dev, holdout) + second = bind("second", dev, [dict(case, id="renamed-" + case["id"], provenance="other") + for case in reversed(holdout)], pretty=True) + assert first.holdout_scope == second.holdout_scope + first_raw = first.config["splits"]["holdout"]["sha256"] + assert first_raw != second.config["splits"]["holdout"]["sha256"] + control, run = _skill_final(tmp_path, first, "first") + _, repeat = _skill_final(tmp_path, second, "second") + source = Path(first.config["splits"]["holdout"]["path"]) + read_bytes = Path.read_bytes + accesses = [] + + def replace_after_read(path): + value = read_bytes(path) + if path == source: + assert control.read("first")["final_claim"]["scope"] == first.holdout_scope + source.write_text("[]") + accesses.append(path) + return value + + def prepare(config, *_): + assert json.loads((config.parent / "tasks.json").read_text()) == holdout + raise RuntimeError("verified tasks reached prepare") + + monkeypatch.setattr(Path, "read_bytes", replace_after_read) + monkeypatch.setattr(speculative_skill.compare, "prepare", prepare) + with pytest.raises(RuntimeError, match="verified tasks reached prepare"): + run() + assert accesses == [source] + Path(second.config["splits"]["holdout"]["path"]).unlink() + with pytest.raises(ValueError, match="already consumed"): + repeat() + assert control.read("second")["final_claim"] is None + + +def test_skill_legacy_holdout_migration_requires_consumed_authenticated_archive( + tmp_path, skill_splits +): + from tools import speculative_skill + + bind, dev, holdout = skill_splits + old = bind("old", dev, holdout) + second = bind("second", dev, list(reversed(holdout)), pretty=True) + control, repeat = _skill_final(tmp_path, second, "second") + old_hash = old.config["splits"]["holdout"]["sha256"] + archive = Path(old.config["splits"]["holdout"]["path"]) + historic = control.create("historic", {}, {}, cap_ms=10000, final_reserve_ms=3000, + deadline=time.time() + 10) + control.claim_final("historic", historic["revision"], "skill-data:" + old_hash, {}) + with pytest.raises(ValueError, match="Unmapped"): + repeat() + with pytest.raises(ValueError, match="consumed"): + speculative_skill.migrate_holdout(control, "1" * 64, tmp_path / "must-not-open") + with pytest.raises(ValueError, match="split changed"): + speculative_skill.migrate_holdout( + control, old_hash, second.config["splits"]["holdout"]["path"]) + with pytest.raises(ValueError, match="Unmapped"): + repeat() + assert speculative_skill.migrate_holdout(control, old_hash, archive) == second.holdout_scope + with pytest.raises(ValueError, match="already consumed"): + repeat() + unrelated = bind("unrelated", dev, holdout[:1]) + control, final = _skill_final(tmp_path, unrelated, "unrelated") + Path(unrelated.config["splits"]["holdout"]["path"]).unlink() + with pytest.raises(FileNotFoundError): + final() + assert control.read("unrelated")["final_claim"]["scope"] == unrelated.holdout_scope + + +def test_skill_search_and_final_bind_the_actual_development_comparator( + tmp_path, monkeypatch, skill_splits +): + from tools import speculative_skill + + bind, dev, holdout = skill_splits + first = bind("first", dev, holdout) + swapped = bind("swapped", [dict(c, split="development") for c in holdout], + [dict(c, split="holdout") for c in dev]) + directory = tmp_path / "artifacts" + development = _skill_adapter(directory, first, "development") + final = _skill_adapter(directory, first, "holdout") + substituted = _skill_adapter(directory, swapped, "holdout") + root = development.import_root(first.export_root(tmp_path / "source"), + snapshot="test-snapshot", context={}) + store = RunStore(tmp_path / "store", "skill-search") + control = WorkControl(store) + control.create("campaign", {}, {"searches": {}}, cap_ms=20000, final_reserve_ms=3000, + deadline=time.time() + 30) + snapshot = Snapshot((1.0,), (0.0,) * 8, (0.0,) * 8, "fixture") + settings = Settings(2, 1, 1, 1, 4, 5, 1000, 1000, 3000) + config = contract(checks=("evaluation",), objective_check="evaluation", + improver_snapshot=digest(snapshot.document())) + + def prepare(config, repo, output): + # Only the ordinary development qualification reaches this comparison. + assert json.loads((config.parent / "tasks.json").read_text()) == dev + plan = output / "plan" / "freeze.json" + plan.parent.mkdir(parents=True) + plan.write_text("{}") + return speculative_skill.sha256(plan) + + report = skill_report() + report["pairs"] *= len(dev) + monkeypatch.setattr(speculative_skill.compare, "prepare", prepare) + monkeypatch.setattr(speculative_skill, "execute_trial", + lambda *a, **kw: {"cleanup_verified": True}) + monkeypatch.setattr(speculative_skill.compare, "read_report", lambda _: report) + # Missing held-out files prove that both development entry points reject the wrong role + # before access and that qualifying a real development root never reads the holdout. + Path(first.config["splits"]["holdout"]["path"]).unlink() + Path(swapped.config["splits"]["holdout"]["path"]).unlink() + with pytest.raises(ValueError, match="holdout-bound"): + qualify_root(control, "campaign", final, root, config) + with pytest.raises(ValueError, match="holdout-bound"): + Search(control, "campaign", config.search_id, snapshot, settings, final) + assert not control.read("campaign")["operations"] + + qualified = qualify_root(control, "campaign", development, root, config) + reopened = WorkControl(RunStore(store.root, store.namespace)) + with pytest.raises(ValueError, match="frozen development experiment"): + finalize(reopened, "campaign", substituted, root, holdout_scope=swapped.holdout_scope, + accept_result=lambda _: True) + assert reopened.read("campaign")["final_claim"] is None + + # A second campaign can import the verified root, retaining its original physical + # identity. Changing the development comparator before or after import is rejected. + reopened.create("import", {}, {"searches": {}}, cap_ms=20000, final_reserve_ms=3000, + deadline=time.time() + 30) + imported = import_qualified_root(qualified, replace(config, search_id="imported")) + wrong = _skill_adapter(directory, swapped, "development") + with pytest.raises(ValueError, match="Imported evidence"): + Search(reopened, "import", "imported", snapshot, settings, wrong).start(imported, seed=1) + Search(reopened, "import", "imported", snapshot, settings, development).start(imported, seed=1) + with pytest.raises(ValueError, match="frozen development experiment"): + finalize(reopened, "import", substituted, root, holdout_scope=swapped.holdout_scope, + accept_result=lambda _: True) + assert reopened.read("import")["final_claim"] is None + # The original registered comparator reaches the missing input after consuming its claim. + with pytest.raises(FileNotFoundError): + finalize(reopened, "campaign", final, root, holdout_scope=first.holdout_scope, + accept_result=lambda _: True) + assert reopened.read("campaign")["final_claim"]["scope"] == first.holdout_scope + + +def test_skill_carry_reuses_winning_child_verification(tmp_path): + def compare(candidate, *args, split): + assert split == "development" + tokens = 90 if (candidate / "skill.txt").read_text() == "old" else 70 + return skill_report(tokens=tokens), True + + comparator = SimpleNamespace(identity="carry-comparator", run=compare) + evaluator = SkillEvaluator(comparator, split="development", policy=SelectionPolicy(1, 0), + token_cap=1000, task_count=1) + adapter = ArtifactAdapter(tmp_path / "artifacts", [Edit("edit", "skill.txt", "old", "new")], + {"evaluation": 300}, evaluator, domain="skill-carry") + source = tmp_path / "source" + source.mkdir() + (source / "skill.txt").write_text("old") + snapshot = Snapshot((1.0,), (0, 30, 60, 0, 0, 0, 0, 0), (0.0,) * 8, "fixture") + root = adapter.import_root(source, snapshot=digest(snapshot.document()), context={}) + control = WorkControl(RunStore(tmp_path / "store", "skill-carry")) + control.create("campaign", {}, {"searches": {}, "checkpoint": snapshot.document()}, + cap_ms=30000, final_reserve_ms=3000, deadline=time.time() + 30) + config = contract(checks=("evaluation",), objective_check="evaluation", max_candidates=2, + improver_snapshot=digest(snapshot.document())) + initial = qualify_root(control, "campaign", adapter, root, config) + settings = Settings(2, 1, 1, 1, 4, 5, 300, 300, 2000, exploration=0) + learn(control, "campaign", adapter, initial, snapshot, settings, rounds=2, group_size=2, + training_cap_ms=2000, learning_rate=0.01, seed=4, mode="carry") + searches = control.read("campaign")["state"]["searches"] + second_round = [r["tree"] for r in searches.values() if r["tree"]["root_id"] != "root"] + assert len(second_round) == 2 + for state in second_round: + winner = state["root_id"] + receipt = next(r for r in state["receipts"] if r["attempt"]["candidate_id"] == winner) + assert receipt["attempt"]["experiment_identity"] == comparator.identity + assert not receipt["attempt"]["execution_key"].startswith("root-") diff --git a/tests/test_vats_runtime.py b/tests/test_vats_runtime.py index 8f7aa0681..30134d073 100644 --- a/tests/test_vats_runtime.py +++ b/tests/test_vats_runtime.py @@ -125,6 +125,62 @@ def test_final_access_is_frozen_and_export_retry_does_not_execute(runtime): assert control.read("campaign")["state"]["stable"]["candidate_id"] == selected.candidate_id +@pytest.mark.parametrize("stage", ["root", "final"]) +def test_qualification_recovers_expired_claim_without_adapter_access(runtime, monkeypatch, stage): + control, adapter, _, _, root = runtime + selected = tree.accept(root) + control.create( + "interrupted", {}, {}, cap_ms=20000, final_reserve_ms=8000, deadline=time.time() + 120 + ) + + def qualify(authority): + if stage == "root": + return qualify_root(authority, "interrupted", adapter, selected, root.contract) + return finalize( + authority, "interrupted", adapter, selected, holdout_scope="held", accept_result=bool + ) + + launch = control.launch + interrupted = {} + + def crash(campaign, operation): + op = control.read(campaign)["operations"][operation] + if stage == "root" or op["binding"]["check"] == tuple(adapter.checks)[-1]: + interrupted.update(operation=operation, deadline=op["deadline"]) + raise RuntimeError("interrupted before launch") + return launch(campaign, operation) + + monkeypatch.setattr(control, "launch", crash) + with pytest.raises(RuntimeError, match="interrupted"): + qualify(control) + calls = len(list((adapter.root / "executions").iterdir())) + + def no_adapter_access(*args): + pytest.fail("An unlaunched check cannot have an adapter receipt") + + monkeypatch.setattr(adapter, "recover", no_adapter_access) + monkeypatch.setattr(adapter, "verify", no_adapter_access) + reopened = WorkControl(RunStore(control.store.root, control.store.namespace)) + with pytest.raises(ValueError, match="awaits launch"): + qualify(reopened) + monkeypatch.setattr( + "inferrsi.harness.work_control.time.time", lambda: interrupted["deadline"] + 0.01 + ) + if stage == "root": + with pytest.raises(ValueError, match="did not start"): + qualify(reopened) + else: + final = qualify(reopened) + assert final["verdict"] == "INCONCLUSIVE" and not final["promoted"] + assert final["results"][-1]["not_started"] == "deadline" + assert qualify(reopened) == final + doc = reopened.read("interrupted") + op = doc["operations"][interrupted["operation"]] + assert op["status"] == "settled" and op["actual_ms"] == 0 + assert op["receipt"] == {"not_started": "deadline"} + assert len(list((adapter.root / "executions").iterdir())) == calls + + def test_resume_after_receipt_before_state_commit_never_executes_twice(runtime, monkeypatch): control, adapter, snapshot, settings, root = runtime search = Search(control, "campaign", root.contract.search_id, snapshot, settings, adapter) @@ -182,8 +238,9 @@ def crash(reason): @pytest.mark.parametrize("kind", ["draft", "verify"]) @pytest.mark.parametrize("expire_campaign", [False, True]) +@pytest.mark.parametrize("crash_before_launch", [False, True]) def test_expired_launch_recovers_unstarted_block_and_resumes( - runtime, monkeypatch, kind, expire_campaign + runtime, monkeypatch, kind, expire_campaign, crash_before_launch ): control, adapter, snapshot, settings, root = runtime search = Search(control, "campaign", root.contract.search_id, snapshot, settings, adapter) @@ -201,15 +258,20 @@ def expire(campaign, operation): monkeypatch.setattr( "inferrsi.harness.work_control.time.time", lambda: deadline + 0.001 ) + if crash_before_launch: + raise RuntimeError("interrupted before launch") return launch(campaign, operation) monkeypatch.setattr(control, "launch", expire) - with pytest.raises(ValueError, match="expired before launch"): + with pytest.raises(RuntimeError if crash_before_launch else ValueError, match="before launch"): search.run() assert interrupted operation = interrupted["operation"] op = control.read("campaign")["operations"][operation] - assert op["receipt"] == {"not_started": "deadline"} + if crash_before_launch: + assert op["status"] == "claimed" and op["receipt"] is None + else: + assert op["receipt"] == {"not_started": "deadline"} assert not (adapter.root / "executions" / operation).exists() def no_adapter_recovery(*args): @@ -220,6 +282,8 @@ def no_adapter_recovery(*args): resumed = Search(reopened, "campaign", root.contract.search_id, snapshot, settings, adapter) resumed.recover() after = reopened.read("campaign") + assert after["operations"][operation]["receipt"] == {"not_started": "deadline"} + assert after["operations"][operation]["actual_ms"] == 0 resumed.recover() assert reopened.read("campaign") == after if kind == "verify": @@ -327,6 +391,7 @@ def crash_after_intent(campaign, operation): with pytest.raises(RuntimeError, match="lost after launch intent"): search.run() before = control.read("campaign") + monkeypatch.setattr("inferrsi.harness.work_control.time.time", lambda: before["deadline"] + 1) with pytest.raises(ValueError, match="Unknown execution"): search.recover() assert control.read("campaign") == before diff --git a/tests/test_work_control.py b/tests/test_work_control.py index 417caaeeb..10add399e 100644 --- a/tests/test_work_control.py +++ b/tests/test_work_control.py @@ -153,6 +153,32 @@ def test_expired_claim_cannot_start_and_does_not_charge_unstarted_work(control, assert result["spent_ms"] == 0 +@pytest.mark.parametrize("interrupted", [False, True]) +def test_blocked_campaign_releases_preexisting_unstarted_claim(control, interrupted): + claim(control, "overrun", domains=("first-device",)) + control.launch("campaign", "overrun") + claim(control, "reserved") + control.settle("campaign", "overrun", {}, actual_ms=201, cleanup_verified=False) + if interrupted: + reopened = WorkControl(RunStore(control.store.root, control.store.namespace)) + after = reopened.recover_unstarted("campaign") + assert reopened.recover_unstarted("campaign") == after + else: + with pytest.raises(ValueError, match="blocked"): + control.launch("campaign", "reserved") + after = control.read("campaign") + assert after["operations"]["reserved"]["receipt"] == {"not_started": "blocked"} + assert after["operations"]["reserved"]["actual_ms"] == 0 and after["spent_ms"] == 0 + assert after["operations"]["overrun"]["status"] == "unknown" + assert after["blocked"] and control.available(after) == 699 + assert [e["kind"] for e in after["events"] if e["operation"] == "reserved"] == [ + "claimed", + "not_started", + ] + with control.store._connection() as db: + assert [row["id"] for row in db.execute("SELECT id FROM work_domains")] == ["first-device"] + + def test_cas_and_failed_transition_roll_back_accounting(control): before = control.read("campaign") control.update("campaign", before["revision"], lambda state: state | {"x": 1}) diff --git a/tools/speculative_skill.py b/tools/speculative_skill.py index 1c3c0e32e..14503bbd4 100644 --- a/tools/speculative_skill.py +++ b/tools/speculative_skill.py @@ -6,8 +6,10 @@ import argparse import copy +import hashlib import json import os +import re import sys import time from pathlib import Path @@ -21,16 +23,75 @@ from tools import skill_eval_compare as compare from tools.skill_eval_improve import materialize +TASK_SCOPE = "skill-tasks-v1:" +LEGACY_SCOPE = "skill-data:" + + +def _canonical_value(value): + # JSON numbers written as 1, 1.0 or 1e0 represent the same case input. + if isinstance(value, dict): + return {k: _canonical_value(v) for k, v in value.items()} + if isinstance(value, list): + return [_canonical_value(v) for v in value] + if isinstance(value, float) and value.is_integer(): + return int(value) + return value + + +def _members(cases): + if not isinstance(cases, list) or not cases: + raise ValueError("Skill task set requires a nonempty case list") + ids, inputs = set(), set() + for case in cases: + if not isinstance(case, dict) or not isinstance(case.get("id"), str) or not case["id"]: + raise ValueError("Skill task set requires a case ID") + if case["id"] in ids: + raise ValueError("Duplicate Skill case ID") + ids.add(case["id"]) + inputs.add(digest(_canonical_value({ + k: v for k, v in case.items() if k not in {"id", "split", "provenance"} + }))) + return ids, inputs + + +def task_set_sha256(cases): + """Freeze split membership from distinct case inputs, independent of labels and order.""" + _, inputs = _members(cases) + return digest(sorted(inputs)) + + +def _read_tasks(path, raw_sha256, split): + data = Path(path).read_bytes() + if hashlib.sha256(data).hexdigest() != raw_sha256: + raise ValueError("Frozen Skill split changed") + cases = json.loads(data) + ids, inputs = _members(cases) + if any(case.get("split") != split for case in cases): + raise ValueError("Skill task belongs to another declared split") + return cases, ids, inputs + + +def migrate_holdout(control, raw_sha256, archive): + """Bind an already consumed legacy Skill claim to its verified task set.""" + def archive_scope(): + _, _, inputs = _read_tasks(archive, raw_sha256, "holdout") + return TASK_SCOPE + digest(sorted(inputs)) + + return control.bind_consumed_holdout(LEGACY_SCOPE + raw_sha256, archive_scope) + class Comparator: """Bind a repo, original baseline and two disjoint externally declared splits. config contains comparison (the existing comparator config, including client), - allowed_paths, and splits. Each split has path, sha256, scope and task_count. + allowed_paths, and splits. Descriptors freeze path, sha256, scope and task_count. + New finals also freeze the holdout task_set_sha256 before any holdout access. Holdout bytes are only opened by run(split='holdout'), after the caller's WorkControl final claim. The controller must use that declared scope. """ + previous_holdout_scope_prefix = LEGACY_SCOPE + def __init__(self, repo, config, baseline_source): self.repo = Path(repo).resolve() self.config = copy.deepcopy(config) @@ -46,7 +107,11 @@ def __init__(self, repo, config, baseline_source): raise ValueError("Development and holdout must have distinct declared identities") for split in (dev, final): if ( - set(split) != {"path", "sha256", "scope", "task_count"} + set(split) - {"task_set_sha256"} != {"path", "sha256", "scope", "task_count"} + or ("task_set_sha256" in split and ( + not isinstance(split["task_set_sha256"], str) + or not re.fullmatch(r"[0-9a-f]{64}", split["task_set_sha256"]) + )) or type(split["task_count"]) is not int or split["task_count"] < 1 or not Path(split["path"]).is_absolute() @@ -76,7 +141,9 @@ def __init__(self, repo, config, baseline_source): } ) self._config_digest = digest(self.config) - self.holdout_scope = "skill-data:" + final["sha256"] + self.holdout_scope = ( + TASK_SCOPE + final["task_set_sha256"] if "task_set_sha256" in final else None + ) def export_root(self, destination): destination = Path(destination) @@ -87,6 +154,16 @@ def export_root(self, destination): path.write_bytes(value) return destination + def _read_split(self, split): + descriptor = self.config["splits"][split] + cases, ids, inputs = _read_tasks(descriptor["path"], descriptor["sha256"], split) + if len(cases) * self.config["comparison"]["limits"]["repeats"] != descriptor["task_count"]: + raise ValueError("Skill trial count differs from the frozen split") + frozen_set = descriptor.get("task_set_sha256") + if frozen_set is not None and digest(sorted(inputs)) != frozen_set: + raise ValueError("Frozen Skill task set differs from its declared identity") + return cases, ids, inputs + def run(self, candidate, output, deadline, *, split): if digest(self.config) != self._config_digest or any( sha256(compare.ROOT / name) != expected for name, expected in self._controllers.items() @@ -111,15 +188,13 @@ def run(self, candidate, output, deadline, *, split): {"inv": edits} if edits else {}, self.config["allowed_paths"], ) - descriptor = self.config["splits"][split] - tasks = Path(descriptor["path"]) - if sha256(tasks) != descriptor["sha256"]: - raise ValueError("Frozen Skill split changed") - cases = json.loads(tasks.read_text(encoding="utf-8")) - if len(cases) * comparison["limits"]["repeats"] != descriptor["task_count"]: - raise ValueError("Skill trial count differs from the frozen split") - if any(case.get("split") != split for case in cases): - raise ValueError("Skill task belongs to another declared split") + if split == "holdout" and self.holdout_scope is None: + raise ValueError("Skill final requires a frozen holdout task_set_sha256") + cases, ids, inputs = self._read_split(split) + if split == "holdout": + _, development_ids, development_inputs = self._read_split("development") + if ids & development_ids or inputs & development_inputs: + raise ValueError("Development and holdout Skill tasks overlap") output.mkdir(parents=True, exist_ok=False) comparison.update( cases="tasks.json", From 1d7cc749ddc89749028c4efd8e380a780ec62c10 Mon Sep 17 00:00:00 2001 From: amy-why-3459 Date: Tue, 24 Nov 2026 15:37:37 +0800 Subject: [PATCH 13/16] search: bind final promotion to the verified deployment winner Check the retained experiment, deployment selection and candidate files before consuming a final scope. Tie final recovery receipts to the physical operation and reconcile unproven selections without promotion. Record the maintainer's experimental delivery scope for #363. Agent-assisted-by: Codex (implementation and validation) Signed-off-by: amy-why-3459 --- docs/speculative-search.md | 41 ++- .../apps/workflows/search_benchmarks.py | 2 + .../apps/workflows/speculative_search.py | 1 + src/inferrsi/harness/search_adapter.py | 44 ++- src/inferrsi/harness/search_campaign.py | 131 ++++++-- tests/test_search_benchmarks.py | 96 +++++- tests/test_vats_runtime.py | 299 +++++++++++++++++- 7 files changed, 536 insertions(+), 78 deletions(-) diff --git a/docs/speculative-search.md b/docs/speculative-search.md index dda5b7e3a..fb6c40b8f 100644 --- a/docs/speculative-search.md +++ b/docs/speculative-search.md @@ -215,9 +215,16 @@ charged. A search whose limit has expired can then return its qualified incumben If only the operation deadline expired, the remaining campaign may continue with fresh claims. This exception does not apply to ambiguous launch-intent crashes. -`finalize` consumes a unique scope in the shared store before final data access, -freezes selection and closes search/training. Engine compares again with the -original baseline. For Skill, supply a **holdout-bound adapter** and +Before claiming a final scope, `finalize` reads the completed deployment search +from the campaign store. It recomputes the winner from verified development +receipts, compares it with the recorded selection and the supplied candidate, +and verifies the retained candidate files against the selected artifact digest. +A root can enter final when a completed deployment selected it. Root qualification +alone, a training collection, and a forced continuation cannot supply that selection. +If several deployments completed, pass `deployment_search` to name the one used +for selection. The claim freezes this search and its winner at the same campaign +revision and closes search/training before final data access. Engine compares +again with the original baseline. For Skill, supply a **holdout-bound adapter** and `Comparator.holdout_scope`, derived from frozen task membership. The raw SHA-256 separately pins each file's exact bytes. Root qualification and VATS search use the development evaluator. Finalization checks that its comparator matches the @@ -228,8 +235,14 @@ identical case inputs across splits. Changing JSON formatting, case order, IDs or provenance gives the same set of inputs a single final allowance in the shared store. Exact bounded radix utility keeps quality ahead of token use. -After interruption, final consumes existing receipts only. Even an unstarted -remaining check cannot be newly dispatched by recovery. Existing final results +After interruption, recovery reads existing receipts without launching another +check. If an already-consumed claim lacks a verifiable selection or its candidate +files have changed, recovery uses bound receipts only to reconcile usage and +cleanup. Once reconciled, the final is INCONCLUSIVE and the incumbent is +preserved. Each new receipt binds the physical operation ID. An older final +receipt without that ID needs its matching executor record from the claimed +operation, including the output path recorded at execution. Missing proof, +unknown usage or unknown cleanup keeps the reservation. Existing final results are immutable; export never reruns them. Only complete accepted final evidence, known cleanup and valid budget can update `stable`. Role checkpoints are never automatically promoted to a production/default improver. Such promotion needs @@ -277,9 +290,9 @@ does not load holdout bytes. Providers require explicit authorization and credentials. Older descriptors can still serve development. Search histories written before -comparator identity was recorded remain readable for reporting. A Skill final -for a campaign with development history requires those records to identify the -same comparator. Upgrade all Skill controllers that write the shared store +comparator identity was recorded remain readable for reporting. A new Skill +final requires a completed deployment whose development evidence identifies +the same comparator. Upgrade all Skill controllers that write the shared store together, since older controllers enforce raw-byte scopes. If the store contains old `skill-data:` claims, bind each consumed claim to its archived input with `tools.speculative_skill.migrate_holdout(control, raw_sha256, archive_path)`. @@ -446,10 +459,8 @@ they are not real task/provider efficacy evidence. | AC-09 | Packaged Engine evaluator and explicit Skill comparator bridge | Live Agent/Skill pilot and broader Engine coverage | | AC-10 | Retained trajectories/costs and ablation switches | Replicated learning and bundle, Reset/Carry and worker comparisons with full lifecycle costs | -The proposed slice for PR #488 is the single-worker local search and learning -runtime, its recovery and final-evidence controls, and the bounded Engine -diagnostic above. Its software checks and the archived measurements support -review of that slice. A maintainer decision on accepting it separately from -the remaining criteria is pending. The [Phase 1 discussion](https://github.com/InferMatrixLab/InferRSI/issues/363#issuecomment-6969704442) -proposed freezing a design. [Issue #363](https://github.com/InferMatrixLab/InferRSI/issues/363) -remains open for the acceptance work in the last column. +The maintainer accepted the single-worker local VATS/GVO runtime, its recovery +and final-evidence controls, and the bounded Engine diagnostic as a separately +reviewable delivery for PR #488. The software controls and the archived +measurements are the evidence for that slice. Issue #363 remains open for the +criteria in the last column. [Maintainer’s scope decision](https://github.com/InferMatrixLab/InferRSI/issues/363#issuecomment-7124911509). diff --git a/src/inferrsi/apps/workflows/search_benchmarks.py b/src/inferrsi/apps/workflows/search_benchmarks.py index b707f57d8..5a6c92320 100644 --- a/src/inferrsi/apps/workflows/search_benchmarks.py +++ b/src/inferrsi/apps/workflows/search_benchmarks.py @@ -21,6 +21,8 @@ class EngineEvaluator: + legacy_execution_record = "execution/execution.json" + def __init__(self, package, frozen, *, arguments, python=sys.executable, prefix=()): self.frozen = Path(frozen).resolve() self.plan = task_protocol.prepare( diff --git a/src/inferrsi/apps/workflows/speculative_search.py b/src/inferrsi/apps/workflows/speculative_search.py index 359546e1c..4245fe9c7 100644 --- a/src/inferrsi/apps/workflows/speculative_search.py +++ b/src/inferrsi/apps/workflows/speculative_search.py @@ -157,6 +157,7 @@ def command(args): campaign, adapter, selected, + deployment_search=selected_state.contract.search_id, holdout_scope=args.final_scope, accept_result=lambda values: all( v["objective"] >= args.minimum_final_score for v in values diff --git a/src/inferrsi/harness/search_adapter.py b/src/inferrsi/harness/search_adapter.py index ad4580641..965b6c78e 100644 --- a/src/inferrsi/harness/search_adapter.py +++ b/src/inferrsi/harness/search_adapter.py @@ -5,11 +5,12 @@ the existing local process domain supplies cleanup, not filesystem isolation. """ +import hashlib import json import math import time from dataclasses import asdict, dataclass -from pathlib import Path +from pathlib import Path, PureWindowsPath from inferrsi.contracts.claim import Verdict @@ -118,7 +119,7 @@ def domains(self, kind, node, check): def _save(self, operation, binding, result): if Path(operation).name != operation or operation in (".", ".."): raise ValueError("Operation must name one retained execution") - binding = binding | {"adapter": self.identity} + binding = binding | {"adapter": self.identity, "operation": operation} path = self.root / "receipts" / (operation + ".json") path.parent.mkdir(exist_ok=True) atomic_write_json( @@ -207,7 +208,26 @@ def verify(self, node, check, operation, deadline): operation, {"candidate": asdict(node), "check": check, "kind": "verify"}, result ) - def recover(self, operation, pending): + def _matches_legacy_execution(self, operation, result): + record = getattr(self.evaluator, "legacy_execution_record", None) + if not record: + return False + relative = Path("executions") / operation / record + path = self.root / relative + if not path.is_file(): + return False + raw = path.read_bytes() + if hashlib.sha256(raw).hexdigest() != result.get("execution_sha256"): + return False + # Compare the operation-specific suffix, so moving an archive preserves its evidence. + result_path = json.loads(raw).get("result_path") + suffix = relative.with_name("result.json").parts + if not isinstance(result_path, str): + return False + return PureWindowsPath(result_path).parts[-len(suffix):] == suffix + + def recover_accounting(self, operation, pending): + """Read the bound physical receipt without adopting candidate output.""" if Path(operation).name != operation or operation in (".", ".."): raise ValueError("Operation must name one retained execution") path = self.root / "receipts" / (operation + ".json") @@ -226,9 +246,21 @@ def recover(self, operation, pending): } if pending["kind"] == "verify": expected["check"] = pending["binding"]["check"] + if "operation" in receipt["binding"]: + expected["operation"] = operation + elif pending.get("final") and not self._matches_legacy_execution( + operation, receipt["result"] + ): + raise ValueError("Legacy final receipt lacks matching operation execution evidence") if digest(expected) != digest(receipt["binding"]): raise ValueError("Recovery receipt belongs to a different operation") - for raw in (expected["candidate"], receipt["result"].get("candidate")): + return receipt["result"] + + def recover(self, operation, pending): + result = self.recover_accounting(operation, pending) + if result is None: + return None + for raw in (pending["binding"]["candidate"], result.get("candidate")): if raw is not None: self.path( tree.Candidate( @@ -243,7 +275,7 @@ def recover(self, operation, pending): ) ) ) - return receipt["result"] + return result class CommandEvaluator: @@ -254,6 +286,8 @@ class CommandEvaluator: and CPU integration controls, not an implicit production qualification. """ + legacy_execution_record = "execution.json" + def __init__(self, commands, resources): self.commands = {key: tuple(value) for key, value in commands.items()} self.resources = {Path(path).resolve(): sha256(Path(path)) for path in resources} diff --git a/src/inferrsi/harness/search_campaign.py b/src/inferrsi/harness/search_campaign.py index 2047d0da7..0055dcd93 100644 --- a/src/inferrsi/harness/search_campaign.py +++ b/src/inferrsi/harness/search_campaign.py @@ -9,7 +9,7 @@ from .artifact_primitives import digest from .gvo import train_group from .search_adapter import development_binding, validate_development_evidence -from .search_records import decode, encode +from .search_records import decode, encode, initial_conditions from .vats import Search @@ -254,40 +254,107 @@ def learn( return snapshot -def finalize(control, campaign, adapter, selected, *, holdout_scope, accept_result): +class _AmbiguousDeployment(ValueError): + pass + + +def _deployment_winner(doc, adapter, selected, search_id): + searches = doc["state"].get("searches", {}) + experiment = getattr(adapter.evaluator, "experiment_identity", None) + if experiment is not None: + histories = [ + op["binding"] for op in doc["operations"].values() if op["category"] == "root" + ] + list(searches.values()) + if any(record.get("experiment_identity") != experiment for record in histories): + raise ValueError("Final evaluator must match the frozen development experiment") + + eligible = { + name: record for name, record in searches.items() + if record.get("status") == "complete" and record.get("collect") is False + and record.get("forced") is False and record.get("pending") is None + and record.get("active_action") is None + } + if search_id is None: + if len(eligible) > 1: + raise _AmbiguousDeployment("Several completed deployment searches. " + "Specify deployment_search for the selected winner.") + search_id = next(iter(eligible), None) + if search_id not in eligible: + raise ValueError("Final requires a completed, natural deployment search") + record = eligible[search_id] + initial_conditions(record) + state = decode(record["tree"]) + if state.contract.search_id != search_id: + raise ValueError("Deployment search ID disagrees with its retained tree") + if experiment is None: + if record["adapter"] != adapter.identity: + raise ValueError("Final adapter must match the deployment experiment") + else: + validate_development_evidence(state, {"experiment_identity": experiment}) + winner = tree.accept(state) + if record["selected"] != winner.candidate_id: + raise ValueError("Recorded deployment winner disagrees with the verified search result") + if selected != winner and selected != replace(winner, parent_id=None): + raise ValueError("Final candidate must be the winner of the selected deployment search") + return search_id + + +def finalize( + control, campaign, adapter, selected, *, holdout_scope, accept_result, deployment_search=None +): """Freeze once; a failed/unknown holdout never reopens search or changes selection. - accept_result is a fixed independent final gate. Only this function records - stable promotion; search acceptance and trained checkpoints do not promote. + deployment_search names the completed natural deployment that selected the candidate. + A campaign with exactly one eligible deployment can omit the name. accept_result is + the fixed independent final gate. """ doc = control.recover_unstarted(campaign) if doc["final_result"] is not None: return doc["final_result"] - validate_final = getattr(adapter.evaluator, "validate_final", None) - if validate_final is not None: - validate_final(holdout_scope) - required_scope = getattr(adapter.evaluator, "holdout_scope", None) - if required_scope is not None and holdout_scope != required_scope: - raise ValueError("Final scope must match the evaluator's frozen holdout data identity") - experiment = getattr(adapter.evaluator, "experiment_identity", None) - if experiment is not None: - histories = ( - [op["binding"] for op in doc["operations"].values() if op["category"] == "root"] - + list(doc["state"].get("searches", {}).values()) - ) - if any(record.get("experiment_identity") != experiment for record in histories): - raise ValueError("Final evaluator must match the frozen development experiment") + frozen = doc["final_claim"] + recovering = frozen is not None binding = { "candidate": asdict(selected), "adapter": adapter.identity, "checks": adapter.checks, "scope": holdout_scope, } - recovering = doc["final_claim"] is not None + previous_search = None if recovering: - if digest(doc["final_claim"]["binding"]) != digest(binding): + previous_search = frozen["binding"].get("deployment_search") + if previous_search is not None: + if deployment_search not in (None, previous_search): + raise ValueError("Cannot change the frozen deployment search") + binding["deployment_search"] = previous_search + # Match the exact original inputs before inspecting receipts, including legacy keys. + if digest(frozen["binding"]) != digest(binding): raise ValueError("Cannot change the final candidate or validation scope") - else: + + selection_error = None + try: + validate_final = getattr(adapter.evaluator, "validate_final", None) + if validate_final is not None: + validate_final(holdout_scope) + required_scope = getattr(adapter.evaluator, "holdout_scope", None) + if required_scope is not None and holdout_scope != required_scope: + raise ValueError("Final scope must match the evaluator's frozen holdout data identity") + search_id = _deployment_winner(doc, adapter, selected, previous_search or deployment_search) + except _AmbiguousDeployment: + raise + except ValueError as error: + if not recovering or (previous_search is None and deployment_search is not None): + raise + selection_error = str(error) + if selection_error is None: + try: + # Authenticate the entire retained bundle before committing a fresh holdout claim. + adapter.path(selected) + except (ValueError, OSError) as error: + if not recovering: + raise + selection_error = str(error) + if not recovering: + binding["deployment_search"] = search_id control.claim_final( campaign, doc["revision"], holdout_scope, binding, previous_scope_prefix=getattr(adapter.evaluator, "previous_holdout_scope_prefix", None), @@ -299,6 +366,8 @@ def finalize(control, campaign, adapter, selected, *, holdout_scope, accept_resu if recovering: op = doc["operations"].get(operation) if op is None: + if selection_error is not None: + continue # No reservation or launch to reconcile; never dispatch it. raise ValueError("Interrupted holdout may be consumed; no new execution") if op["status"] == "claimed": raise ValueError("Final reservation still awaits launch") @@ -313,9 +382,11 @@ def finalize(control, campaign, adapter, selected, *, holdout_scope, accept_resu "cleanup_verified": True, } else: - result = adapter.recover( + recover = adapter.recover if selection_error is None else adapter.recover_accounting + result = recover( operation, - {"kind": "verify", "binding": {"candidate": asdict(selected), "check": check}}, + {"kind": "verify", "final": True, + "binding": {"candidate": binding["candidate"], "check": check}}, ) if result is None: raise ValueError("Unknown final execution; recover its receipt without rerun") @@ -348,11 +419,13 @@ def finalize(control, campaign, adapter, selected, *, holdout_scope, accept_resu ) results.append(result) doc = control.read(campaign) - unresolved = doc["blocked"] or any( - op["status"] != "settled" for op in doc["operations"].values() - ) + unsettled = any(op["status"] != "settled" for op in doc["operations"].values()) + if selection_error is not None and unsettled: + raise ValueError("Unknown final usage or cleanup; recover its receipt without rerun") + unresolved = doc["blocked"] or unsettled passed = ( - not unresolved + selection_error is None + and not unresolved and all(r["verdict"] == "PASS" and r["cleanup_verified"] is True for r in results) and accept_result(results) ) @@ -362,7 +435,9 @@ def finalize(control, campaign, adapter, selected, *, holdout_scope, accept_resu "results": results, "promoted": passed, } - if unresolved or any( + if selection_error is not None: + result["selection_error"] = selection_error + if selection_error is not None or unresolved or any( r["verdict"] == "INCONCLUSIVE" or r["cleanup_verified"] is not True for r in results ): result.update(verdict="INCONCLUSIVE", promoted=False) diff --git a/tests/test_search_benchmarks.py b/tests/test_search_benchmarks.py index 992446704..15624d7e3 100644 --- a/tests/test_search_benchmarks.py +++ b/tests/test_search_benchmarks.py @@ -4,6 +4,7 @@ from dataclasses import replace from pathlib import Path from types import SimpleNamespace +from unittest.mock import patch import pytest @@ -244,24 +245,24 @@ def compare(*args, split, **kwargs): comparator = SimpleNamespace(identity="test-comparator", run=compare, holdout_scope=declared_scope) - evaluator = SkillEvaluator(comparator, split=split, policy=SelectionPolicy(1, 0), - token_cap=1000, task_count=1) - adapter = ArtifactAdapter(tmp_path / "artifacts", [Edit("edit", "skill.txt", "old", "new")], - {"evaluation": 3000}, evaluator, domain="skill-test") + directory = tmp_path / "artifacts" + adapter = _skill_adapter(directory, comparator, split, task_count=1) source = tmp_path / "source" source.mkdir() (source / "skill.txt").write_text("old") root = adapter.import_root(source, snapshot="test-snapshot", context={}) control = WorkControl(RunStore(tmp_path / "store", "skill-test")) - before = control.create("campaign", {}, {}, cap_ms=10000, final_reserve_ms=3000, + before = control.create("campaign", {}, {"searches": {}}, cap_ms=10000, final_reserve_ms=3000, deadline=time.time() + 10) kwargs = dict(holdout_scope=supplied_scope, accept_result=lambda results: True) if valid: + development = _skill_adapter(directory, comparator, "development", task_count=1) + _select_skill_root(control, "campaign", development, root) result = finalize(control, "campaign", adapter, root, **kwargs) assert result["verdict"] == "PASS" and result["promoted"] - assert accesses == ["holdout"] + assert accesses == ["development", "holdout"] assert finalize(control, "campaign", adapter, root, **kwargs) == result - assert accesses == ["holdout"] + assert accesses == ["development", "holdout"] else: with pytest.raises(ValueError, match="holdout-bound"): finalize(control, "campaign", adapter, root, **kwargs) @@ -371,10 +372,12 @@ def bind(name, dev, holdout, *, declared=None, pretty=False): return bind, config["development"], config["holdout"] -def _skill_adapter(directory, comparator, split): +def _skill_adapter(directory, comparator, split, *, task_count=None): + if task_count is None: + task_count = comparator.config["splits"][split]["task_count"] evaluator = SkillEvaluator( comparator, split=split, policy=SelectionPolicy(1, 0), token_cap=1000, - task_count=comparator.config["splits"][split]["task_count"], + task_count=task_count, ) return ArtifactAdapter( directory, [Edit("edit", "selected/SKILL.md", "old", "new")], @@ -382,12 +385,71 @@ def _skill_adapter(directory, comparator, split): ) +def _select_skill_root(control, campaign, development, root): + snapshot = Snapshot((1.0,), (0.0,) * 8, (0.0,) * 8, "fixture") + config = contract(checks=("evaluation",), objective_check="evaluation", max_attempts=1, + improver_snapshot=digest(snapshot.document())) + qualified = qualify_root(control, campaign, development, root, config) + search = Search(control, campaign, config.search_id, snapshot, + Settings(1, 1, 1, 1, 1, 1, 100, 100, 3000), development) + search.start(qualified, seed=1, collect=False) + assert search.run()["selected"] == root.candidate_id + + +@pytest.mark.parametrize("fault", ["empty", "other-candidate", "producer", "changed-content"]) +def test_skill_final_rejects_unbound_candidate_before_holdout(tmp_path, fault): + accesses = [] + + def compare(*args, split): + accesses.append(split) + return skill_report(tokens=100), True + + comparator = SimpleNamespace(identity="frozen-comparator", holdout_scope="shared", run=compare) + directory = tmp_path / "artifacts" + dev = _skill_adapter(directory, comparator, "development", task_count=1) + final = _skill_adapter(directory, comparator, "holdout", task_count=1) + source = tmp_path / "source" + source.mkdir() + (source / "skill.txt").write_text("original") + root = dev.import_root(source, snapshot="snapshot", context={}) + control = WorkControl(RunStore(tmp_path / "store", "skill-test")) + control.create("campaign", {}, {"searches": {}}, cap_ms=10000, final_reserve_ms=3000, + deadline=time.time() + 30) + if fault != "empty": + _select_skill_root(control, "campaign", dev, root) + supplied = root + if fault == "other-candidate": + # A retained copy with the same bytes still has a different candidate identity. + supplied = replace(root, candidate_id="unselected") + (directory / "candidates" / supplied.candidate_id).mkdir() + (directory / "candidates" / supplied.candidate_id / "skill.txt").write_text("original") + elif fault == "producer": + supplied = replace(root, producer=replace(root.producer, snapshot="another-checkpoint")) + elif fault == "changed-content": + (dev.path(root) / "skill.txt").write_text("changed after selection") + reopened = WorkControl(RunStore(control.store.root, control.store.namespace)) + with pytest.raises(ValueError): + finalize(reopened, "campaign", final, supplied, + holdout_scope=comparator.holdout_scope, accept_result=bool) + record = reopened.read("campaign") + assert record["final_claim"] is None and record["final_result"] is None + assert "stable" not in record["state"] and "holdout" not in accesses + + def _skill_final(tmp_path, comparator, name): - adapter = _skill_adapter(tmp_path / name / "artifacts", comparator, "holdout") + directory = tmp_path / name / "artifacts" + adapter = _skill_adapter(directory, comparator, "holdout") + development = _skill_adapter(directory, comparator, "development") source = comparator.export_root(tmp_path / name / "source") - root = adapter.import_root(source, snapshot="test-snapshot", context={}) + root = development.import_root(source, snapshot="test-snapshot", context={}) control = WorkControl(RunStore(tmp_path / "store", "skill-test")) - control.create(name, {}, {}, cap_ms=10000, final_reserve_ms=3000, deadline=time.time() + 10) + control.create(name, {}, {"searches": {}}, cap_ms=10000, final_reserve_ms=3000, + deadline=time.time() + 30) + # These tests exercise the real holdout reader after a recorded development selection. + report = skill_report() + report["pairs"] *= development.evaluator.task_count + with patch.object(comparator, "run", return_value=(report, True)): + _select_skill_root(control, name, development, root) def run(): return finalize(control, name, adapter, root, holdout_scope=comparator.holdout_scope, @@ -524,7 +586,7 @@ def test_skill_search_and_final_bind_the_actual_development_comparator( deadline=time.time() + 30) snapshot = Snapshot((1.0,), (0.0,) * 8, (0.0,) * 8, "fixture") settings = Settings(2, 1, 1, 1, 4, 5, 1000, 1000, 3000) - config = contract(checks=("evaluation",), objective_check="evaluation", + config = contract(checks=("evaluation",), objective_check="evaluation", max_attempts=1, improver_snapshot=digest(snapshot.document())) def prepare(config, repo, output): @@ -566,16 +628,18 @@ def prepare(config, repo, output): wrong = _skill_adapter(directory, swapped, "development") with pytest.raises(ValueError, match="Imported evidence"): Search(reopened, "import", "imported", snapshot, settings, wrong).start(imported, seed=1) - Search(reopened, "import", "imported", snapshot, settings, development).start(imported, seed=1) + search = Search(reopened, "import", "imported", snapshot, settings, development) + search.start(imported, seed=1, collect=False) + search.run() with pytest.raises(ValueError, match="frozen development experiment"): finalize(reopened, "import", substituted, root, holdout_scope=swapped.holdout_scope, accept_result=lambda _: True) assert reopened.read("import")["final_claim"] is None # The original registered comparator reaches the missing input after consuming its claim. with pytest.raises(FileNotFoundError): - finalize(reopened, "campaign", final, root, holdout_scope=first.holdout_scope, + finalize(reopened, "import", final, root, holdout_scope=first.holdout_scope, accept_result=lambda _: True) - assert reopened.read("campaign")["final_claim"]["scope"] == first.holdout_scope + assert reopened.read("import")["final_claim"]["scope"] == first.holdout_scope def test_skill_carry_reuses_winning_child_verification(tmp_path): diff --git a/tests/test_vats_runtime.py b/tests/test_vats_runtime.py index 30134d073..d3d61b7fc 100644 --- a/tests/test_vats_runtime.py +++ b/tests/test_vats_runtime.py @@ -11,7 +11,12 @@ from inferrsi.harness.gvo import examples, train_group from inferrsi.harness.run_store import RunStore from inferrsi.harness.search_adapter import ArtifactAdapter, CommandEvaluator, Edit -from inferrsi.harness.search_campaign import collect_group, finalize, qualify_root +from inferrsi.harness.search_campaign import ( + collect_group, + finalize, + import_qualified_root, + qualify_root, +) from inferrsi.harness.search_records import decode from inferrsi.harness.vats import Search, Settings from inferrsi.harness.work_control import WorkControl @@ -95,12 +100,25 @@ def test_real_process_search_group_training_checkpoint_and_fresh_search(runtime) assert control.read("campaign")["spent_ms"] > 0 -def test_final_access_is_frozen_and_export_retry_does_not_execute(runtime): +def start_search_for_final(runtime, name="deploy", *, collect=False, forced=False, root_only=False): control, adapter, snapshot, settings, root = runtime - records = collect_group( - control, "campaign", adapter, root, snapshot, settings, group_id="first", size=2, seed=4 - ) - selected = tree.accept(decode(records[0]["tree"])) + configuration = replace(root.contract, search_id=name) + if root_only: + configuration = replace(configuration, max_attempts=len(root.attempts)) + initial = import_qualified_root(root, configuration) + search = Search(control, "campaign", name, snapshot, settings, adapter) + search.start(initial, seed=2, collect=collect, forced=forced) + return search + + +@pytest.mark.parametrize("detach_parent", [False, True]) +def test_final_access_is_frozen_and_export_retry_does_not_execute(runtime, detach_parent): + control, adapter, _, _, _ = runtime + search = start_search_for_final(runtime) + selected = tree.accept(decode(search.run()["tree"])) + assert selected.parent_id is not None + if detach_parent: + selected = replace(selected, parent_id=None) result = finalize( control, "campaign", @@ -110,6 +128,7 @@ def test_final_access_is_frozen_and_export_retry_does_not_execute(runtime): accept_result=lambda results: all(r["verdict"] == "PASS" for r in results), ) before = len(list((adapter.root / "executions").iterdir())) + (adapter.path(selected) / "candidate.txt").unlink() assert ( finalize( control, @@ -125,20 +144,272 @@ def test_final_access_is_frozen_and_export_retry_does_not_execute(runtime): assert control.read("campaign")["state"]["stable"]["candidate_id"] == selected.candidate_id +@pytest.mark.parametrize("stage", ["root-only", "active", "collecting", "forced"]) +def test_final_requires_completed_unforced_deployment_before_claim(runtime, monkeypatch, stage): + control, adapter, _, _, root = runtime + if stage != "root-only": + search = start_search_for_final( + runtime, collect=stage == "collecting", forced=stage == "forced", root_only=True + ) + if stage != "active": + search.run() + before = control.read("campaign") + monkeypatch.setattr(adapter, "verify", lambda *args: pytest.fail("Unexpected final dispatch")) + with pytest.raises(ValueError): + finalize(control, "campaign", adapter, tree.accept(root), holdout_scope="held", + accept_result=bool) + assert control.read("campaign") == before + + +@pytest.mark.parametrize("substitution", ["losing-root", "producer", "bytes"]) +def test_final_authenticates_winner_before_claim(runtime, monkeypatch, substitution): + control, adapter, _, _, root = runtime + search = start_search_for_final(runtime) + selected = tree.accept(decode(search.run()["tree"])) + assert selected.candidate_id != root.root_id + if substitution == "losing-root": + selected = tree.accept(root) + elif substitution == "producer": + selected = replace(selected, producer=replace(selected.producer, revision="another")) + else: + (adapter.path(selected) / "candidate.txt").write_text("999") + before = control.read("campaign") + monkeypatch.setattr(adapter, "verify", lambda *args: pytest.fail("Unexpected final dispatch")) + with pytest.raises(ValueError): + finalize(control, "campaign", adapter, selected, holdout_scope="held", accept_result=bool) + assert control.read("campaign") == before + + +def interrupt_after_last_final_receipt(control, adapter, monkeypatch): + settle = control.settle + + def crash_after_last_receipt(campaign, operation, receipt, **options): + op = control.read(campaign)["operations"][operation] + if op["binding"]["check"] == tuple(adapter.checks)[-1]: + raise RuntimeError("crash after final receipt") + return settle(campaign, operation, receipt, **options) + + monkeypatch.setattr(control, "settle", crash_after_last_receipt) + + +def test_final_recovery_uses_receipt_and_frozen_deployment_with_identical_winners( + runtime, monkeypatch +): + control, adapter, _, _, root = runtime + selected = tree.accept(root) + for name in ("deploy-a", "deploy-b"): + record = start_search_for_final(runtime, name, root_only=True).run() + assert record["selected"] == selected.candidate_id + kwargs = dict(holdout_scope="held", accept_result=bool) + before = control.read("campaign") + with pytest.raises(ValueError): + finalize(control, "campaign", adapter, selected, **kwargs) + assert control.read("campaign") == before + + interrupt_after_last_final_receipt(control, adapter, monkeypatch) + with pytest.raises(RuntimeError, match="crash after final receipt"): + finalize(control, "campaign", adapter, selected, deployment_search="deploy-a", **kwargs) + reopened = WorkControl(RunStore(control.store.root, control.store.namespace)) + before = reopened.read("campaign") + assert before["final_claim"]["binding"]["deployment_search"] == "deploy-a" + assert before["final_result"] is None + waiting = [key for key, op in before["operations"].items() if op["status"] == "launched"] + assert len(waiting) == 1 + recoveries = [] + recover = adapter.recover + + def read_receipt(operation, pending): + recoveries.append(operation) + return recover(operation, pending) + + monkeypatch.setattr(adapter, "verify", lambda *args: pytest.fail("Final must not relaunch")) + monkeypatch.setattr(adapter, "recover", read_receipt) + with pytest.raises(ValueError): + finalize(reopened, "campaign", adapter, selected, deployment_search="deploy-b", **kwargs) + with pytest.raises(ValueError): + finalize(reopened, "campaign", adapter, selected, **(kwargs | {"holdout_scope": "changed"})) + different = replace(selected, producer=replace(selected.producer, revision="changed")) + with pytest.raises(ValueError): + finalize(reopened, "campaign", adapter, different, **kwargs) + assert reopened.read("campaign") == before and not recoveries + result = finalize(reopened, "campaign", adapter, selected, **kwargs) + assert result["promoted"] and result["verdict"] == "PASS" + assert finalize(reopened, "campaign", adapter, selected, **kwargs) == result + assert recoveries == waiting + + +@pytest.mark.parametrize("has_deployment", [False, True]) +def test_legacy_final_receipts_require_development_selection(runtime, monkeypatch, has_deployment): + control, adapter, _, _, root = runtime + selected = tree.accept(root) + if has_deployment: + record = start_search_for_final(runtime, root_only=True).run() + assert record["selected"] == selected.candidate_id + # The old binding had no deployment-search ID. Simulate a crash after the last receipt. + scope = "legacy-held" + binding = { + "candidate": asdict(selected), + "adapter": adapter.identity, + "checks": adapter.checks, + "scope": scope, + } + doc = control.claim_final("campaign", control.read("campaign")["revision"], scope, binding) + for check, cap in adapter.checks.items(): + operation = "final-" + digest({"campaign": "campaign", "binding": binding, "check": check}) + control.claim( + "campaign", doc["revision"], operation, category="final", final=True, cap_ms=cap, + domains=(adapter.domain,), binding=binding | {"check": check}, + ) + claim = control.launch("campaign", operation) + receipt = adapter.verify(selected, check, operation, claim["deadline"]) + assert receipt["verdict"] == "PASS" + if check == tuple(adapter.checks)[-1]: + break + doc = control.settle( + "campaign", operation, receipt, actual_ms=receipt["elapsed_ms"], + cleanup_verified=receipt["cleanup_verified"], + ) + reopened = WorkControl(RunStore(control.store.root, control.store.namespace)) + before = reopened.read("campaign") + assert before["operations"][operation]["status"] == "launched" + + def assert_reservation_held(): + assert reopened.read("campaign") == before + with reopened.store._connection() as db: + lease = db.execute( + "SELECT 1 FROM work_domains WHERE operation=?", (operation,) + ).fetchone() + assert lease is not None + + monkeypatch.setattr(adapter, "verify", lambda *args: pytest.fail("Legacy final must not rerun")) + kwargs = dict(holdout_scope=scope, accept_result=bool) + path = adapter.root / "receipts" / (operation + ".json") + delayed = path.rename(path.with_suffix(".delayed")) + with pytest.raises(ValueError, match="Unknown final"): + finalize(reopened, "campaign", adapter, selected, **kwargs) + assert_reservation_held() + # A root and a final check really ran for the same bundle under the same evaluator. + root_op = next( + key for key, op in before["operations"].items() + if op["category"] == "root" and op["binding"]["check"] == check + ) + misplaced = json.loads((path.parent / (root_op + ".json")).read_text()) + genuine = json.loads(delayed.read_text()) + + def historical(document): + binding = {k: v for k, v in document["binding"].items() if k != "operation"} + payload = {"binding": binding, "result": document["result"]} + return {**payload, "digest": digest(payload)} + + old_misplaced, old_genuine = historical(misplaced), historical(genuine) + assert old_misplaced["binding"] == old_genuine["binding"] + assert misplaced["result"]["execution_sha256"] != genuine["result"]["execution_sha256"] + for wrong in (misplaced, old_misplaced): + path.write_text(json.dumps(wrong)) + with pytest.raises(ValueError): + finalize(reopened, "campaign", adapter, selected, **kwargs) + assert_reservation_held() + assert misplaced["binding"]["operation"] == root_op + assert genuine["binding"]["operation"] == operation + + # Matching a digest is insufficient if the archived transcript came from the root operation. + execution = adapter.root / "executions" / operation / "execution.json" + saved_execution = execution.rename(execution.with_suffix(".delayed")) + source_execution = adapter.root / "executions" / root_op / "execution.json" + execution.write_bytes(source_execution.read_bytes()) + path.write_text(json.dumps(old_misplaced)) + with pytest.raises(ValueError): + finalize(reopened, "campaign", adapter, selected, **kwargs) + assert_reservation_held() + + # The genuine historical receipt likewise needs its own retained execution transcript. + execution.unlink() + path.write_text(json.dumps(old_genuine)) + with pytest.raises(ValueError): + finalize(reopened, "campaign", adapter, selected, **kwargs) + assert_reservation_held() + saved_execution.rename(execution) + result = finalize(reopened, "campaign", adapter, selected, **kwargs) + assert result["promoted"] is has_deployment + assert result["verdict"] == ("PASS" if has_deployment else "INCONCLUSIVE") + after = reopened.read("campaign") + assert ("stable" in after["state"]) == has_deployment + assert after["operations"][operation]["status"] == "settled" + assert after["operations"][operation]["actual_ms"] == receipt["elapsed_ms"] + assert after["spent_ms"] == before["spent_ms"] + receipt["elapsed_ms"] + with reopened.store._connection() as db: + lease = db.execute("SELECT 1 FROM work_domains WHERE operation=?", (operation,)).fetchone() + assert lease is None + assert finalize(reopened, "campaign", adapter, selected, **kwargs) == result + assert reopened.read("campaign") == after + + +@pytest.mark.parametrize("last_receipt_settled", [False, True]) +def test_final_reconciles_without_promoting_changed_winner_bytes( + runtime, monkeypatch, last_receipt_settled +): + control, adapter, _, _, root = runtime + selected = tree.accept(root) + start_search_for_final(runtime, root_only=True).run() + + def interrupt(*args, **kwargs): + raise RuntimeError("crash before final promotion") + + if last_receipt_settled: + monkeypatch.setattr(control, "finish_final", interrupt) + else: + interrupt_after_last_final_receipt(control, adapter, monkeypatch) + kwargs = dict(holdout_scope="held", accept_result=bool) + with pytest.raises(RuntimeError, match="crash"): + finalize(control, "campaign", adapter, selected, **kwargs) + before = control.read("campaign") + held = [key for key, op in before["operations"].items() if op["status"] == "launched"] + assert len(held) == (0 if last_receipt_settled else 1) + (adapter.path(selected) / "candidate.txt").write_text("999") + reopened = WorkControl(RunStore(control.store.root, control.store.namespace)) + monkeypatch.setattr(adapter, "verify", lambda *args: pytest.fail("Final must not rerun")) + if held: + # The other check's genuine receipt cannot release this operation's lease. + receipts = adapter.root / "receipts" + pending_receipt = receipts / (held[0] + ".json") + original = pending_receipt.read_bytes() + settled = next( + key for key, op in before["operations"].items() + if op["category"] == "final" and op["status"] == "settled" + ) + pending_receipt.write_bytes((receipts / (settled + ".json")).read_bytes()) + with pytest.raises(ValueError): + finalize(reopened, "campaign", adapter, selected, **kwargs) + assert reopened.read("campaign") == before + pending_receipt.write_bytes(original) + result = finalize(reopened, "campaign", adapter, selected, **kwargs) + assert result["verdict"] == "INCONCLUSIVE" and not result["promoted"] + after = reopened.read("campaign") + assert "stable" not in after["state"] + assert all(op["status"] == "settled" for op in after["operations"].values()) + assert after["spent_ms"] == before["spent_ms"] + sum( + after["operations"][key]["actual_ms"] for key in held + ) + assert finalize(reopened, "campaign", adapter, selected, **kwargs) == result + + @pytest.mark.parametrize("stage", ["root", "final"]) def test_qualification_recovers_expired_claim_without_adapter_access(runtime, monkeypatch, stage): control, adapter, _, _, root = runtime selected = tree.accept(root) - control.create( - "interrupted", {}, {}, cap_ms=20000, final_reserve_ms=8000, deadline=time.time() + 120 - ) + campaign = "interrupted" if stage == "root" else "campaign" + if stage == "root": + control.create( + campaign, {}, {}, cap_ms=20000, final_reserve_ms=8000, deadline=time.time() + 120 + ) + else: + start_search_for_final(runtime, root_only=True).run() def qualify(authority): if stage == "root": - return qualify_root(authority, "interrupted", adapter, selected, root.contract) - return finalize( - authority, "interrupted", adapter, selected, holdout_scope="held", accept_result=bool - ) + return qualify_root(authority, campaign, adapter, selected, root.contract) + return finalize(authority, campaign, adapter, selected, holdout_scope="held", + accept_result=bool) launch = control.launch interrupted = {} @@ -174,7 +445,7 @@ def no_adapter_access(*args): assert final["verdict"] == "INCONCLUSIVE" and not final["promoted"] assert final["results"][-1]["not_started"] == "deadline" assert qualify(reopened) == final - doc = reopened.read("interrupted") + doc = reopened.read(campaign) op = doc["operations"][interrupted["operation"]] assert op["status"] == "settled" and op["actual_ms"] == 0 assert op["receipt"] == {"not_started": "deadline"} From 4cf95a9c12f02a191a3238c11130a0654d7c2c2c Mon Sep 17 00:00:00 2001 From: amy-why-3459 Date: Tue, 24 Nov 2026 16:04:15 +0800 Subject: [PATCH 14/16] search: freeze acceptance before holdout access Bind the versioned gate, objective and mandatory checks to the final claim. Restore the saved gate across interruptions and reconcile legacy receipts without promoting an unproven result. Preserve exact Engine thresholds and verify the rule when replaying new diagnostic records. Agent-assisted-by: Codex (implementation and validation) Signed-off-by: amy-why-3459 --- docs/speculative-search.md | 29 ++- .../plan/retained-sha256.json | 6 +- .../qwen-vats-gvo-910b1-20261124/plan/run.py | 10 +- .../validator/README.md | 6 + .../validator/check.py | 28 ++- src/inferrsi/apps/cli/arguments.py | 19 +- .../apps/workflows/speculative_search.py | 15 +- src/inferrsi/harness/search_campaign.py | 190 ++++++++++++++---- tests/test_qwen_gvo_diagnostic.py | 25 ++- tests/test_search_benchmarks.py | 58 +++++- tests/test_vats_runtime.py | 173 ++++++++++++++-- 11 files changed, 467 insertions(+), 92 deletions(-) diff --git a/docs/speculative-search.md b/docs/speculative-search.md index fb6c40b8f..8a4dc6dc3 100644 --- a/docs/speculative-search.md +++ b/docs/speculative-search.md @@ -222,10 +222,18 @@ and verifies the retained candidate files against the selected artifact digest. A root can enter final when a completed deployment selected it. Root qualification alone, a training collection, and a forced continuation cannot supply that selection. If several deployments completed, pass `deployment_search` to name the one used -for selection. The claim freezes this search and its winner at the same campaign -revision and closes search/training before final data access. Engine compares -again with the original baseline. For Skill, supply a **holdout-bound adapter** and -`Comparator.holdout_scope`, derived from frozen task membership. The raw SHA-256 +for selection. The first claim freezes the deployment, winner and acceptance +rule at the same campaign revision, then closes search/training before final +data access. + +For a new Python final, pass `acceptance=FinalAcceptance()` to require all +checks to pass with verified cleanup. With +`acceptance=FinalAcceptance(minimum_objective=1.2)`, the same checks apply and +the deployment’s designated objective must also reach 1.2. The claim records +the rule version, required checks, threshold and, for a numeric threshold, the +objective check. The Engine CLI uses its explicit `--minimum-final-score` when +comparing against the original baseline. For Skill, supply a **holdout-bound +adapter** and `Comparator.holdout_scope`, derived from frozen task membership. The raw SHA-256 separately pins each file's exact bytes. Root qualification and VATS search use the development evaluator. Finalization checks that its comparator matches the one bound to the recorded development evidence. After the final claim and before @@ -235,11 +243,14 @@ identical case inputs across splits. Changing JSON formatting, case order, IDs or provenance gives the same set of inputs a single final allowance in the shared store. Exact bounded radix utility keeps quality ahead of token use. -After interruption, recovery reads existing receipts without launching another -check. If an already-consumed claim lacks a verifiable selection or its candidate -files have changed, recovery uses bound receipts only to reconcile usage and -cleanup. Once reconciled, the final is INCONCLUSIVE and the incumbent is -preserved. Each new receipt binds the physical operation ID. An older final +After interruption, omit `acceptance` to apply the rule saved with the claim to +the existing receipts. Supplying a different rule is rejected. Earlier Python +callers that passed an `accept_result` callback must select a declarative rule +for new finals. A historical claim without a verifiable rule can only settle +usage and cleanup. The same applies when its selection cannot be verified or +its candidate files have changed. Once reconciled, the final is INCONCLUSIVE +and the incumbent is preserved. A claim with a newer rule version stays pending +for a runtime that understands it. Each new receipt binds the physical operation ID. An older final receipt without that ID needs its matching executor record from the claimed operation, including the output path recorded at execution. Missing proof, unknown usage or unknown cleanup keeps the reservation. Existing final results diff --git a/evidence/qwen-vats-gvo-910b1-20261124/plan/retained-sha256.json b/evidence/qwen-vats-gvo-910b1-20261124/plan/retained-sha256.json index cd79e9295..6d53fa6e5 100644 --- a/evidence/qwen-vats-gvo-910b1-20261124/plan/retained-sha256.json +++ b/evidence/qwen-vats-gvo-910b1-20261124/plan/retained-sha256.json @@ -3,11 +3,11 @@ "patch/README.md": "e2a3af7bd373d9e1637265f3980fd272cbbc181d455d275794bf66f2cc6f8095", "plan/edits.json": "2975fee42370debd80f627ecd58880e45280968f739ca3045c8b388bc9ab4cbb", "plan/plan.json": "66344b1ca1522fd3b38966c243bbb6ec8eb8fecc203ccd171690463569632013", - "plan/run.py": "548c67815bafa10ab2ff05fe4a09596e2ab99dbae28fc526c01bfa50f8f471ff", + "plan/run.py": "8c66690aa9bb7b53dcbfe568bfe3654a65436a11ee7a05b21c2d3c0d611e1ad5", "plan/runspec.json": "f3f2ab71d227ee3e7d95233cc3d34e17ab41d086f519f558fb3aca5200515404", "results/local-archive.json": "c5df5dc55feb202e53f28d5b6f4ba28913e893277d9deb5e783a472c686d9ae6", "results/summary.json": "df3029cfcaacb91018b6c931017b059b99a80633116586f9446fe82e31986132", "results/trials.jsonl": "6bd39719a5c0d794b1c5e04403b03789ae5ca42e0c9c74694200bdb6355a0756", - "validator/README.md": "8d1ee7e1a6c4c77448f9c5c6cfb9e44bf5c7c3a06c3b6e516ebd2c238e3b5ca5", - "validator/check.py": "7f1ff3de33a1e2ccd48e573a64f3110a1468388643b41bcdbff2311c75171d50" + "validator/README.md": "3b2b92982f3e3c377a755972002beaad454451bdc08cd2db68e5d4256337fa3d", + "validator/check.py": "4b408d07d0cdff724312f86746c9de5d6996e9b8925f61c5453dc9761f646f04" } diff --git a/evidence/qwen-vats-gvo-910b1-20261124/plan/run.py b/evidence/qwen-vats-gvo-910b1-20261124/plan/run.py index 89000e0e3..15af73b00 100644 --- a/evidence/qwen-vats-gvo-910b1-20261124/plan/run.py +++ b/evidence/qwen-vats-gvo-910b1-20261124/plan/run.py @@ -15,7 +15,13 @@ from inferrsi.harness.atomic_json import atomic_write_json from inferrsi.harness.run_store import RunStore from inferrsi.harness.search_adapter import ArtifactAdapter, Edit -from inferrsi.harness.search_campaign import finalize, import_qualified_root, learn, qualify_root +from inferrsi.harness.search_campaign import ( + FinalAcceptance, + finalize, + import_qualified_root, + learn, + qualify_root, +) from inferrsi.harness.search_records import decode from inferrsi.harness.vats import Search, Settings from inferrsi.harness.work_control import WorkControl @@ -196,7 +202,7 @@ def run(output): adapter, replace(winner, parent_id=None), holdout_scope=name, - accept_result=lambda _: True, + acceptance=FinalAcceptance(), ) row.update(final_verdict=final_record["verdict"], promoted=final_record["promoted"]) if ( diff --git a/evidence/qwen-vats-gvo-910b1-20261124/validator/README.md b/evidence/qwen-vats-gvo-910b1-20261124/validator/README.md index d88ff9166..488a10508 100644 --- a/evidence/qwen-vats-gvo-910b1-20261124/validator/README.md +++ b/evidence/qwen-vats-gvo-910b1-20261124/validator/README.md @@ -32,3 +32,9 @@ the unpacked original archive, preserve its `config/run.py` next to the `--historical-driver /path/to/config/run.py`. The current driver retains its own bytes in each new trial directory. Its summary also records incomplete and unrun arms, which prevent publication of paired results. + +For trials made with the current driver, each VATS claim and its settled final +operation must carry the same rule. It requires every check to pass with verified cleanup +and applies no additional score threshold. The checker also recognizes the pinned driver +from [the previous revision](https://github.com/InferMatrixLab/InferRSI/blob/b974554199cb10759da03a75f333e608c91d7395/evidence/qwen-vats-gvo-910b1-20261124/plan/run.py). +The recorded promotions from older drivers remain historical results. diff --git a/evidence/qwen-vats-gvo-910b1-20261124/validator/check.py b/evidence/qwen-vats-gvo-910b1-20261124/validator/check.py index a27d7a27a..134e3dc82 100644 --- a/evidence/qwen-vats-gvo-910b1-20261124/validator/check.py +++ b/evidence/qwen-vats-gvo-910b1-20261124/validator/check.py @@ -14,6 +14,8 @@ from inferrsi.harness.work_control import WorkControl PLAN = Path(__file__).resolve().parents[1] / "plan" +# The published driver at b974554 saved its own bytes before rules were stored in claims. +PREVIOUS_DRIVER_SHA256 = "548c67815bafa10ab2ff05fe4a09596e2ab99dbae28fc526c01bfa50f8f471ff" def read(path): @@ -33,7 +35,7 @@ def close(left, right): return math.isclose(left, right, rel_tol=1e-12, abs_tol=1e-12) -def promoted_final(doc, campaign, candidate_root): +def promoted_final(doc, campaign, candidate_root, *, require_acceptance=False): final = doc["final_result"] claim = doc["final_claim"] require( @@ -56,6 +58,17 @@ def promoted_final(doc, campaign, candidate_root): and final["results"] == [ops[0]["receipt"]], f"{campaign}: final receipt mismatch", ) + if require_acceptance: + binding = claim["binding"] + rule = binding.get("acceptance") + require( + isinstance(rule, dict) + and type(rule.get("version")) is int + and rule == {"version": 1, "minimum_objective": None} + and binding.get("deployment_search") == campaign + and ops[0]["binding"] == binding | {"check": "evaluation"}, + f"{campaign}: final acceptance differs from the diagnostic plan", + ) candidate_id = candidate["candidate_id"] require( Path(candidate_id).name == candidate_id and candidate_id not in ("", ".", ".."), @@ -76,13 +89,17 @@ def main(completed, failed, historical_driver=None): for name, expected_hash in spec["packaged_task_sha256"].items(): require(sha(task / name) == expected_hash, name) summary = read(completed / "summary.json") + current_driver = sha(PLAN / "run.py") for name, expected_hash in spec["input_sha256"].items(): path = PLAN.parent / name if name == "plan/run.py": if "driver_sha256" in summary: path = completed / "driver.py" - expected_hash = sha(PLAN / "run.py") - require(summary["driver_sha256"] == expected_hash, "Replay driver identity") + expected_hash = summary["driver_sha256"] + require( + expected_hash in (current_driver, PREVIOUS_DRIVER_SHA256), + "Replay driver identity", + ) else: # The original archive includes the driver used for the published measurements. path = historical_driver or completed.parent.parent / "config/run.py" @@ -113,7 +130,10 @@ def main(completed, failed, historical_driver=None): require(not doc["blocked"], campaign) if campaign != "training": final_receipts[campaign] = promoted_final( - doc, campaign, completed / "artifacts/candidates" + doc, + campaign, + completed / "artifacts/candidates", + require_acceptance=summary.get("driver_sha256") == current_driver, ) for operation, op in doc["operations"].items(): require(op["status"] == "settled", operation) diff --git a/src/inferrsi/apps/cli/arguments.py b/src/inferrsi/apps/cli/arguments.py index 221bc70ab..da0c89014 100644 --- a/src/inferrsi/apps/cli/arguments.py +++ b/src/inferrsi/apps/cli/arguments.py @@ -24,6 +24,23 @@ from .task_input import FIELDS as TASK_FIELDS +def _final_score(value: str) -> int | float: + try: + return int(value) + except ValueError: + score = float(value) + if score.is_integer() and abs(score) >= 2**53: + from decimal import Decimal + + # The finite float bounds the expanded integer to at most 309 digits. + # Recover integral decimal/exponent spellings before they round to a weaker gate. + exact = Decimal(value) + if exact != exact.to_integral_value(): + raise argparse.ArgumentTypeError("Fractional final score exceeds float precision") + return int(exact) + return score + + def _read_sources(parser: argparse.ArgumentParser) -> None: library = parser.add_mutually_exclusive_group() library.add_argument( @@ -148,7 +165,7 @@ def _search_arguments(commands) -> None: run.add_argument("--seed", type=int, default=0) run.add_argument("--mode", choices=("reset", "carry"), default="reset") run.add_argument("--final-scope", required=True) - run.add_argument("--minimum-final-score", type=float, required=True) + run.add_argument("--minimum-final-score", type=_final_score, required=True) run.add_argument("--python", default=sys.executable) run.add_argument("arguments", nargs="*", help="Task protocol arguments after --") for action in ("status", "export"): diff --git a/src/inferrsi/apps/workflows/speculative_search.py b/src/inferrsi/apps/workflows/speculative_search.py index 4245fe9c7..76783d3f1 100644 --- a/src/inferrsi/apps/workflows/speculative_search.py +++ b/src/inferrsi/apps/workflows/speculative_search.py @@ -10,7 +10,13 @@ from inferrsi.harness.atomic_json import atomic_write_json from inferrsi.harness.run_store import RunStore, RunStoreReader from inferrsi.harness.search_adapter import ArtifactAdapter, Edit -from inferrsi.harness.search_campaign import collect_group, finalize, learn, qualify_root +from inferrsi.harness.search_campaign import ( + FinalAcceptance, + collect_group, + finalize, + learn, + qualify_root, +) from inferrsi.harness.search_records import decode from inferrsi.harness.vats import Settings from inferrsi.harness.work_control import WorkControl @@ -27,6 +33,7 @@ def command(args): return result from .search_benchmarks import EngineEvaluator + acceptance = FinalAcceptance(minimum_objective=args.minimum_final_score) if root.exists(): raise ValueError("Use a new session directory; status/export never restart work") if args.groups < 2 or args.rounds < 1: @@ -90,7 +97,7 @@ def command(args): "rounds": args.rounds, "mode": args.mode, "learning_rate": args.learning_rate, - "minimum_final_score": args.minimum_final_score, + "minimum_final_score": acceptance.minimum_objective, "initial_snapshot": snapshot.document(), } contract = search_tree.Contract( @@ -159,8 +166,6 @@ def command(args): selected, deployment_search=selected_state.contract.search_id, holdout_scope=args.final_scope, - accept_result=lambda values: all( - v["objective"] >= args.minimum_final_score for v in values - ), + acceptance=acceptance, ) return control.read(campaign) diff --git a/src/inferrsi/harness/search_campaign.py b/src/inferrsi/harness/search_campaign.py index 0055dcd93..eb80a6ef6 100644 --- a/src/inferrsi/harness/search_campaign.py +++ b/src/inferrsi/harness/search_campaign.py @@ -1,7 +1,8 @@ """Campaign assembly: real root acquisition, independent searches, GVO and final gating.""" +import math import time -from dataclasses import asdict, replace +from dataclasses import asdict, dataclass, replace from inferrsi.contracts.claim import Verdict @@ -254,6 +255,52 @@ def learn( return snapshot +class _FutureAcceptanceVersion(ValueError): + pass + + +@dataclass(frozen=True) +class FinalAcceptance: + """Require all checks to pass and optionally a minimum score on the deployment objective.""" + + minimum_objective: int | float | None = None + + def __post_init__(self): + value = self.minimum_objective + if value is not None and not self._finite_number(value): + raise ValueError("Final acceptance minimum_objective must be a finite number") + if type(value) is float and value.is_integer(): + object.__setattr__(self, "minimum_objective", int(value)) + + @staticmethod + def _finite_number(value): + # Integers are exact. Converting a large integer to float can change the gate. + return type(value) is int or (type(value) is float and math.isfinite(value)) + + def document(self): + return {"version": 1, "minimum_objective": self.minimum_objective} + + @classmethod + def from_document(cls, raw): + if type(raw) is dict and type(raw.get("version")) is int and raw["version"] > 1: + raise _FutureAcceptanceVersion("Future final acceptance version is unsupported") + if ( + type(raw) is not dict or set(raw) != {"version", "minimum_objective"} + or type(raw["version"]) is not int or raw["version"] != 1 + ): + raise ValueError("Missing or unsupported frozen final acceptance rule") + return cls(raw["minimum_objective"]) + + def accepts(self, results, objective_check): + if not results or not all(r["verdict"] == "PASS" and r["cleanup_verified"] is True + for r in results.values()): + return False + if self.minimum_objective is None: + return True + value = results[objective_check].get("objective") + return self._finite_number(value) and value >= self.minimum_objective + + class _AmbiguousDeployment(ValueError): pass @@ -296,77 +343,135 @@ def _deployment_winner(doc, adapter, selected, search_id): raise ValueError("Recorded deployment winner disagrees with the verified search result") if selected != winner and selected != replace(winner, parent_id=None): raise ValueError("Final candidate must be the winner of the selected deployment search") - return search_id + return state.contract def finalize( - control, campaign, adapter, selected, *, holdout_scope, accept_result, deployment_search=None + control, campaign, adapter, selected, *, holdout_scope, acceptance=None, deployment_search=None, + accept_result=None, ): """Freeze once; a failed/unknown holdout never reopens search or changes selection. deployment_search names the completed natural deployment that selected the candidate. - A campaign with exactly one eligible deployment can omit the name. accept_result is - the fixed independent final gate. + A campaign with exactly one eligible deployment can omit the name. New claims require + an explicit FinalAcceptance. Receipt recovery uses the rule in the original claim. """ - doc = control.recover_unstarted(campaign) + doc = control.read(campaign) if doc["final_result"] is not None: return doc["final_result"] + if accept_result is not None: + raise ValueError("Final acceptance callbacks cannot be frozen; supply FinalAcceptance") + if acceptance is not None and type(acceptance) is not FinalAcceptance: + raise ValueError("Final acceptance must be a FinalAcceptance rule") frozen = doc["final_claim"] recovering = frozen is not None + checks = dict(adapter.checks) + if not recovering and not checks: + raise ValueError("Final acceptance requires a nonempty set of checks") binding = { "candidate": asdict(selected), "adapter": adapter.identity, - "checks": adapter.checks, + "checks": checks, "scope": holdout_scope, } previous_search = None + acceptance_error = None if recovering: + # Reuse the original rule bytes in the binding, even when this version cannot + # interpret them. That binding identifies the already launched physical checks. + stored = frozen["binding"] + for name in ("acceptance", "objective_check"): + if name in stored: + binding[name] = stored[name] + try: + rule = FinalAcceptance.from_document(binding.get("acceptance")) + if rule.minimum_objective is not None: + name = binding.get("objective_check") + if type(name) is not str or name not in checks: + raise ValueError("Missing or unknown frozen final objective check") + elif "objective_check" in binding: + raise ValueError("Unexpected objective check for a score-free acceptance rule") + except _FutureAcceptanceVersion: + raise + except ValueError as error: + if acceptance is not None: + raise ValueError("Cannot replace an unproven final acceptance rule") from error + acceptance_error = str(error) + else: + if acceptance is not None and acceptance != rule: + raise ValueError("Cannot change the frozen final acceptance rule") previous_search = frozen["binding"].get("deployment_search") if previous_search is not None: if deployment_search not in (None, previous_search): raise ValueError("Cannot change the frozen deployment search") binding["deployment_search"] = previous_search # Match the exact original inputs before inspecting receipts, including legacy keys. - if digest(frozen["binding"]) != digest(binding): + if digest(stored) != digest(binding): raise ValueError("Cannot change the final candidate or validation scope") + # Binding equality authenticates membership. Keep the adapter's report order and + # take each cap from the persisted claim (RunStore sorts JSON object keys). + checks = {name: stored["checks"][name] for name in checks} + else: + if acceptance is None: + raise ValueError("New final claims require an explicit FinalAcceptance rule") + binding["acceptance"] = acceptance.document() + rule = FinalAcceptance.from_document(binding["acceptance"]) + + # An incompatible retry must not even release an expired reservation. Once the + # binding matches, recover reservations and use the resulting revision for the CAS. + doc = control.recover_unstarted(campaign) + if doc["final_result"] is not None: + return doc["final_result"] + if doc["final_claim"] != frozen: + raise ValueError("Final claim changed; recover using its frozen binding") selection_error = None - try: - validate_final = getattr(adapter.evaluator, "validate_final", None) - if validate_final is not None: - validate_final(holdout_scope) - required_scope = getattr(adapter.evaluator, "holdout_scope", None) - if required_scope is not None and holdout_scope != required_scope: - raise ValueError("Final scope must match the evaluator's frozen holdout data identity") - search_id = _deployment_winner(doc, adapter, selected, previous_search or deployment_search) - except _AmbiguousDeployment: - raise - except ValueError as error: - if not recovering or (previous_search is None and deployment_search is not None): - raise - selection_error = str(error) - if selection_error is None: + if acceptance_error is None: try: - # Authenticate the entire retained bundle before committing a fresh holdout claim. - adapter.path(selected) - except (ValueError, OSError) as error: - if not recovering: + validate_final = getattr(adapter.evaluator, "validate_final", None) + if validate_final is not None: + validate_final(holdout_scope) + required_scope = getattr(adapter.evaluator, "holdout_scope", None) + if required_scope is not None and holdout_scope != required_scope: + raise ValueError("Final scope must match the evaluator's frozen holdout identity") + contract = _deployment_winner( + doc, adapter, selected, previous_search or deployment_search + ) + if not set(contract.checks).issubset(checks): + raise ValueError("Final checks must include every required deployment check") + if rule.minimum_objective is not None: + if recovering and binding["objective_check"] != contract.objective_check: + raise ValueError("Frozen final objective check differs from deployment") + binding["objective_check"] = contract.objective_check + except _AmbiguousDeployment: + raise + except ValueError as error: + if not recovering or (previous_search is None and deployment_search is not None): raise selection_error = str(error) + if selection_error is None: + try: + # Authenticate the entire retained bundle before committing a fresh holdout claim. + adapter.path(selected) + except (ValueError, OSError) as error: + if not recovering: + raise + selection_error = str(error) + accounting_only = acceptance_error is not None or selection_error is not None if not recovering: - binding["deployment_search"] = search_id + binding["deployment_search"] = contract.search_id control.claim_final( campaign, doc["revision"], holdout_scope, binding, previous_scope_prefix=getattr(adapter.evaluator, "previous_holdout_scope_prefix", None), ) - results = [] - for check, cap in adapter.checks.items(): + results = {} + for check, cap in checks.items(): doc = control.read(campaign) operation = "final-" + digest({"campaign": campaign, "binding": binding, "check": check}) if recovering: op = doc["operations"].get(operation) if op is None: - if selection_error is not None: + if accounting_only: continue # No reservation or launch to reconcile; never dispatch it. raise ValueError("Interrupted holdout may be consumed; no new execution") if op["status"] == "claimed": @@ -382,7 +487,7 @@ def finalize( "cleanup_verified": True, } else: - recover = adapter.recover if selection_error is None else adapter.recover_accounting + recover = adapter.recover_accounting if accounting_only else adapter.recover result = recover( operation, {"kind": "verify", "final": True, @@ -417,28 +522,29 @@ def finalize( actual_ms=result.get("elapsed_ms"), cleanup_verified=result.get("cleanup_verified"), ) - results.append(result) + results[check] = result doc = control.read(campaign) unsettled = any(op["status"] != "settled" for op in doc["operations"].values()) - if selection_error is not None and unsettled: + if accounting_only and unsettled: raise ValueError("Unknown final usage or cleanup; recover its receipt without rerun") unresolved = doc["blocked"] or unsettled - passed = ( - selection_error is None - and not unresolved - and all(r["verdict"] == "PASS" and r["cleanup_verified"] is True for r in results) - and accept_result(results) + complete = bool(checks) and results.keys() == checks.keys() + passed = complete and not accounting_only and not unresolved and rule.accepts( + results, binding.get("objective_check") ) result = { "candidate": selected.candidate_id, "verdict": "PASS" if passed else "FAIL", - "results": results, + "results": list(results.values()), "promoted": passed, } if selection_error is not None: result["selection_error"] = selection_error - if selection_error is not None or unresolved or any( - r["verdict"] == "INCONCLUSIVE" or r["cleanup_verified"] is not True for r in results + if acceptance_error is not None: + result["acceptance_error"] = acceptance_error + if not complete or accounting_only or unresolved or any( + r["verdict"] == "INCONCLUSIVE" or r["cleanup_verified"] is not True + for r in results.values() ): result.update(verdict="INCONCLUSIVE", promoted=False) control.finish_final( diff --git a/tests/test_qwen_gvo_diagnostic.py b/tests/test_qwen_gvo_diagnostic.py index 53b2d8e54..05731470d 100644 --- a/tests/test_qwen_gvo_diagnostic.py +++ b/tests/test_qwen_gvo_diagnostic.py @@ -64,7 +64,9 @@ def test_protocol_identity_checked_before_loading(tmp_path, monkeypatch): checker.main(tmp_path / "trial", tmp_path / "failed") -@pytest.mark.parametrize("fault", ["verdict", "promotion", "selection", "receipt", "source"]) +@pytest.mark.parametrize( + "fault", ["verdict", "promotion", "selection", "receipt", "source", "rule", "operation-rule"] +) def test_offline_checker_requires_bound_durable_promotion(tmp_path, fault): from inferrsi.harness import candidates @@ -76,20 +78,29 @@ def test_offline_checker_requires_bound_durable_promotion(tmp_path, fault): node = {"candidate_id": "winner", "artifact_digest": candidates.identity(candidate_dir)} receipt = {"verdict": "PASS", "cleanup_verified": True, "objective": 1.2} final = {"candidate": "winner", "verdict": "PASS", "promoted": True, "results": [receipt]} + binding = { + "candidate": node, + "deployment_search": "frozen-101", + "acceptance": {"version": 1, "minimum_objective": None}, + } op = { "category": "final", "status": "settled", - "binding": {"candidate": node}, + "binding": binding | {"check": "evaluation"}, "receipt": receipt, } state = {"stable": node, "searches": {"frozen-101": {"selected": "winner"}}} doc = { "final_result": final, - "final_claim": {"scope": "frozen-101", "binding": {"candidate": node}}, + "final_claim": {"scope": "frozen-101", "binding": binding}, "state": state, "operations": {"final-1": op}, } - assert checker.promoted_final(doc, "frozen-101", tmp_path) == (receipt, checker.sha(source)) + + def replay(): + return checker.promoted_final(doc, "frozen-101", tmp_path, require_acceptance=True) + + assert replay() == (receipt, checker.sha(source)) if fault == "verdict": final["verdict"] = "INCONCLUSIVE" elif fault == "promotion": @@ -98,7 +109,11 @@ def test_offline_checker_requires_bound_durable_promotion(tmp_path, fault): state["searches"]["frozen-101"]["selected"] = "root" elif fault == "receipt": op["receipt"] = {**receipt, "objective": 1.3} + elif fault == "rule": + binding.pop("acceptance") + elif fault == "operation-rule": + op["binding"]["acceptance"] = {"version": 1, "minimum_objective": 0} else: source.write_bytes(b"other candidate") with pytest.raises(ValueError, match="frozen-101"): - checker.promoted_final(doc, "frozen-101", tmp_path) + replay() diff --git a/tests/test_search_benchmarks.py b/tests/test_search_benchmarks.py index 15624d7e3..a17021cd2 100644 --- a/tests/test_search_benchmarks.py +++ b/tests/test_search_benchmarks.py @@ -16,7 +16,13 @@ from inferrsi.harness.artifact_primitives import digest from inferrsi.harness.run_store import RunStore from inferrsi.harness.search_adapter import ArtifactAdapter, Edit -from inferrsi.harness.search_campaign import finalize, import_qualified_root, learn, qualify_root +from inferrsi.harness.search_campaign import ( + FinalAcceptance, + finalize, + import_qualified_root, + learn, + qualify_root, +) from inferrsi.harness.task_protocol import TaskProtocolError from inferrsi.harness.trials.external import execute_trial from inferrsi.harness.vats import Search, Settings @@ -92,6 +98,44 @@ def test_cli_requires_explicit_shared_store_and_read_commands_do_not_create_one( parser().parse_args(["search", "engine", "--session", str(tmp_path / "run")]) +@pytest.mark.parametrize( + ("floor", "expected", "promoted"), + [("9007199254740992", 2**53, True), ("9.007199254740993e15", 2**53 + 1, False)], +) +def test_engine_cli_freezes_exact_final_score(tmp_path, monkeypatch, floor, expected, promoted): + class Evaluator: + identity = "cpu-score-fixture" + + def __init__(self, task, frozen, **kwargs): + frozen.mkdir() + (frozen / "baseline.py").write_text("value = 1") + + def evaluate(self, candidate, check, output, deadline): + output.mkdir(parents=True) + return {"verdict": "PASS", "objective": 2**53, "cleanup_verified": True} + + monkeypatch.setattr("inferrsi.apps.workflows.search_benchmarks.EngineEvaluator", Evaluator) + checkpoint = tmp_path / "initial.json" + checkpoint.write_text(json.dumps(Snapshot((0,), (0,) * 8, (0,) * 8, "fixture").document())) + session = str(tmp_path / "session") + args = parser().parse_args([ + "search", "engine", "--session", session, "--store", str(tmp_path / "store"), + "--campaign", "cpu", "--task", "cpu", "--domain", "local", "--checkpoint", str(checkpoint), + "--edit", "increase", "candidate.py", "value = 1", "value = 2", "--max-seconds", "120", + "--evaluation-seconds", "2", "--final-reserve-seconds", "3", "--search-seconds", "15", + "--preparation-seconds", "1", "--draft-seconds", "1", "--training-seconds", "2", + "--candidates", "2", "--depth", "1", "--attempts", "2", "--steps", "2", "--plans", "2", + "--targets", "1", "--groups", "2", "--rounds", "1", "--learning-rate", "0.01", + "--final-scope", "held", "--minimum-final-score", floor, + ]) + command(args) + saved = command(parser().parse_args(["search", "status", "--session", session])) + binding = saved["final_claim"]["binding"] + assert binding["acceptance"] == {"version": 1, "minimum_objective": expected} + assert binding["objective_check"] == "evaluation" + assert saved["final_result"]["promoted"] is promoted + + def test_engine_rejects_frozen_resource_mutation_during_evaluation(tmp_path, monkeypatch): evaluator = EngineEvaluator( "qwen21-denoise-npu", @@ -254,7 +298,7 @@ def compare(*args, split, **kwargs): control = WorkControl(RunStore(tmp_path / "store", "skill-test")) before = control.create("campaign", {}, {"searches": {}}, cap_ms=10000, final_reserve_ms=3000, deadline=time.time() + 10) - kwargs = dict(holdout_scope=supplied_scope, accept_result=lambda results: True) + kwargs = dict(holdout_scope=supplied_scope, acceptance=FinalAcceptance()) if valid: development = _skill_adapter(directory, comparator, "development", task_count=1) _select_skill_root(control, "campaign", development, root) @@ -430,7 +474,7 @@ def compare(*args, split): reopened = WorkControl(RunStore(control.store.root, control.store.namespace)) with pytest.raises(ValueError): finalize(reopened, "campaign", final, supplied, - holdout_scope=comparator.holdout_scope, accept_result=bool) + holdout_scope=comparator.holdout_scope, acceptance=FinalAcceptance()) record = reopened.read("campaign") assert record["final_claim"] is None and record["final_result"] is None assert "stable" not in record["state"] and "holdout" not in accesses @@ -453,7 +497,7 @@ def _skill_final(tmp_path, comparator, name): def run(): return finalize(control, name, adapter, root, holdout_scope=comparator.holdout_scope, - accept_result=lambda results: True) + acceptance=FinalAcceptance()) return control, run @@ -617,7 +661,7 @@ def prepare(config, repo, output): reopened = WorkControl(RunStore(store.root, store.namespace)) with pytest.raises(ValueError, match="frozen development experiment"): finalize(reopened, "campaign", substituted, root, holdout_scope=swapped.holdout_scope, - accept_result=lambda _: True) + acceptance=FinalAcceptance()) assert reopened.read("campaign")["final_claim"] is None # A second campaign can import the verified root, retaining its original physical @@ -633,12 +677,12 @@ def prepare(config, repo, output): search.run() with pytest.raises(ValueError, match="frozen development experiment"): finalize(reopened, "import", substituted, root, holdout_scope=swapped.holdout_scope, - accept_result=lambda _: True) + acceptance=FinalAcceptance()) assert reopened.read("import")["final_claim"] is None # The original registered comparator reaches the missing input after consuming its claim. with pytest.raises(FileNotFoundError): finalize(reopened, "import", final, root, holdout_scope=first.holdout_scope, - accept_result=lambda _: True) + acceptance=FinalAcceptance()) assert reopened.read("import")["final_claim"]["scope"] == first.holdout_scope diff --git a/tests/test_vats_runtime.py b/tests/test_vats_runtime.py index d3d61b7fc..a3659d4e1 100644 --- a/tests/test_vats_runtime.py +++ b/tests/test_vats_runtime.py @@ -12,6 +12,7 @@ from inferrsi.harness.run_store import RunStore from inferrsi.harness.search_adapter import ArtifactAdapter, CommandEvaluator, Edit from inferrsi.harness.search_campaign import ( + FinalAcceptance, collect_group, finalize, import_qualified_root, @@ -125,7 +126,7 @@ def test_final_access_is_frozen_and_export_retry_does_not_execute(runtime, detac adapter, selected, holdout_scope="test-final", - accept_result=lambda results: all(r["verdict"] == "PASS" for r in results), + acceptance=FinalAcceptance(), ) before = len(list((adapter.root / "executions").iterdir())) (adapter.path(selected) / "candidate.txt").unlink() @@ -136,7 +137,7 @@ def test_final_access_is_frozen_and_export_retry_does_not_execute(runtime, detac adapter, selected, holdout_scope="test-final", - accept_result=lambda results: False, + acceptance=FinalAcceptance(minimum_objective=100), ) == result ) @@ -144,6 +145,111 @@ def test_final_access_is_frozen_and_export_retry_does_not_execute(runtime, detac assert control.read("campaign")["state"]["stable"]["candidate_id"] == selected.candidate_id +@pytest.mark.parametrize( + ("timing", "remaining"), + [("before", ("objective",)), ("after", ()), ("after", ("objective",))], + ids=["missing-at-claim", "empty-after-claim", "partial-after-claim"], +) +def test_final_requires_every_deployment_check_across_holdout_claim( + runtime, monkeypatch, timing, remaining +): + control, adapter, _, _, root = runtime + start_search_for_final(runtime, root_only=True).run() + original_checks = dict(adapter.checks) + replacement = {name: original_checks[name] for name in remaining} + selected = tree.accept(root) + kwargs = dict(holdout_scope="held", acceptance=FinalAcceptance()) + if timing == "before": + before = control.read("campaign") + monkeypatch.setattr(adapter, "checks", replacement) + with pytest.raises(ValueError): + finalize(control, "campaign", adapter, selected, **kwargs) + assert control.read("campaign") == before + return + + claim_final = control.claim_final + + def replace_checks_after_commit(*args, **kwargs): + doc = claim_final(*args, **kwargs) + assert doc["final_claim"]["binding"]["checks"] == original_checks + monkeypatch.setattr(adapter, "checks", replacement) + return doc + + monkeypatch.setattr(control, "claim_final", replace_checks_after_commit) + result = finalize(control, "campaign", adapter, selected, **kwargs) + doc = control.read("campaign") + physical = { + op["binding"]["check"]: (operation, op) for operation, op in doc["operations"].items() + if op["category"] == "final" + } + assert set(physical) == {"quality", "objective"} + for operation, op in physical.values(): + assert (adapter.root / "executions" / operation / "execution.json").is_file() + assert op["status"] == "settled" and op["receipt"]["verdict"] == "PASS" + assert len(result["results"]) == 2 and result["promoted"] + + +@pytest.mark.parametrize(("minimum", "verdict"), [(2, "FAIL"), (1, "PASS"), (2**53 + 1, "FAIL")]) +def test_final_recovery_uses_frozen_minimum_after_all_receipts_settle( + runtime, monkeypatch, minimum, verdict +): + control, adapter, _, _, root = runtime + selected = tree.accept(root) + start_search_for_final(runtime, root_only=True).run() + scope = "threshold-held" + + def crash_before_commit(campaign, binding, result, **kwargs): + assert result["verdict"] == verdict + assert [receipt["objective"] for receipt in result["results"]] == [None, 1] + assert all(r["verdict"] == "PASS" and r["cleanup_verified"] for r in result["results"]) + raise RuntimeError("stopped before committing final result") + + monkeypatch.setattr(control, "finish_final", crash_before_commit) + with pytest.raises(RuntimeError, match="before committing"): + finalize(control, "campaign", adapter, selected, holdout_scope=scope, + acceptance=FinalAcceptance(minimum_objective=minimum)) + reopened = WorkControl(RunStore(control.store.root, control.store.namespace)) + before = reopened.read("campaign") + assert before["final_claim"]["binding"]["acceptance"] == { + "version": 1, "minimum_objective": minimum, + } + assert before["final_claim"]["binding"]["objective_check"] == root.contract.objective_check + assert before["final_result"] is None and "stable" not in before["state"] + assert all(op["status"] == "settled" for op in before["operations"].values()) + + def no_new_execution(*args): + pytest.fail("All final receipts are already settled") + + monkeypatch.setattr(adapter, "verify", no_new_execution) + monkeypatch.setattr(adapter, "recover", no_new_execution) + monkeypatch.setattr(adapter, "recover_accounting", no_new_execution) + for substitute in (FinalAcceptance(), FinalAcceptance(minimum_objective=minimum - 1)): + with pytest.raises(ValueError): + finalize(reopened, "campaign", adapter, selected, holdout_scope=scope, + acceptance=substitute) + assert reopened.read("campaign") == before + # Equivalent adapters can enumerate their checks in a different order after a restart. + monkeypatch.setattr(adapter, "checks", dict(reversed(adapter.checks.items()))) + result = finalize(reopened, "campaign", adapter, selected, holdout_scope=scope) + assert result["verdict"] == verdict and result["promoted"] == (verdict == "PASS") + after = reopened.read("campaign") + assert ("stable" in after["state"]) == (verdict == "PASS") + assert after["operations"] == before["operations"] and after["spent_ms"] == before["spent_ms"] + exported = finalize(reopened, "campaign", adapter, selected, holdout_scope=scope, + acceptance=FinalAcceptance(minimum_objective=0)) + assert exported == result and reopened.read("campaign") == after + + +@pytest.mark.parametrize("supplied", [{}, {"accept_result": bool}, {"acceptance": bool}]) +def test_final_requires_a_durable_acceptance_rule_before_any_claim(runtime, supplied): + control, adapter, _, _, root = runtime + start_search_for_final(runtime, root_only=True).run() + before = control.read("campaign") + with pytest.raises((TypeError, ValueError)): + finalize(control, "campaign", adapter, tree.accept(root), holdout_scope="held", **supplied) + assert control.read("campaign") == before + + @pytest.mark.parametrize("stage", ["root-only", "active", "collecting", "forced"]) def test_final_requires_completed_unforced_deployment_before_claim(runtime, monkeypatch, stage): control, adapter, _, _, root = runtime @@ -157,7 +263,7 @@ def test_final_requires_completed_unforced_deployment_before_claim(runtime, monk monkeypatch.setattr(adapter, "verify", lambda *args: pytest.fail("Unexpected final dispatch")) with pytest.raises(ValueError): finalize(control, "campaign", adapter, tree.accept(root), holdout_scope="held", - accept_result=bool) + acceptance=FinalAcceptance()) assert control.read("campaign") == before @@ -176,7 +282,8 @@ def test_final_authenticates_winner_before_claim(runtime, monkeypatch, substitut before = control.read("campaign") monkeypatch.setattr(adapter, "verify", lambda *args: pytest.fail("Unexpected final dispatch")) with pytest.raises(ValueError): - finalize(control, "campaign", adapter, selected, holdout_scope="held", accept_result=bool) + finalize(control, "campaign", adapter, selected, holdout_scope="held", + acceptance=FinalAcceptance()) assert control.read("campaign") == before @@ -200,7 +307,7 @@ def test_final_recovery_uses_receipt_and_frozen_deployment_with_identical_winner for name in ("deploy-a", "deploy-b"): record = start_search_for_final(runtime, name, root_only=True).run() assert record["selected"] == selected.candidate_id - kwargs = dict(holdout_scope="held", accept_result=bool) + kwargs = dict(holdout_scope="held", acceptance=FinalAcceptance()) before = control.read("campaign") with pytest.raises(ValueError): finalize(control, "campaign", adapter, selected, **kwargs) @@ -231,6 +338,9 @@ def read_receipt(operation, pending): different = replace(selected, producer=replace(selected.producer, revision="changed")) with pytest.raises(ValueError): finalize(reopened, "campaign", adapter, different, **kwargs) + with pytest.raises(ValueError): + finalize(reopened, "campaign", adapter, selected, + **(kwargs | {"acceptance": FinalAcceptance(minimum_objective=1)})) assert reopened.read("campaign") == before and not recoveries result = finalize(reopened, "campaign", adapter, selected, **kwargs) assert result["promoted"] and result["verdict"] == "PASS" @@ -238,14 +348,21 @@ def read_receipt(operation, pending): assert recoveries == waiting -@pytest.mark.parametrize("has_deployment", [False, True]) -def test_legacy_final_receipts_require_development_selection(runtime, monkeypatch, has_deployment): +@pytest.mark.parametrize( + ("has_deployment", "archived_rule"), + [(False, None), (True, None), (True, {"version": 2, "minimum_objective": 0}), + (True, {"version": 1, "minimum_objective": "0"})], + ids=["no-selection", "verified-winner", "unknown-rule-version", "invalid-threshold"], +) +def test_historical_final_handles_missing_malformed_and_future_acceptance( + runtime, monkeypatch, has_deployment, archived_rule +): control, adapter, _, _, root = runtime selected = tree.accept(root) if has_deployment: record = start_search_for_final(runtime, root_only=True).run() assert record["selected"] == selected.candidate_id - # The old binding had no deployment-search ID. Simulate a crash after the last receipt. + # The last final receipt exists on disk but its transaction never settled. scope = "legacy-held" binding = { "candidate": asdict(selected), @@ -253,6 +370,10 @@ def test_legacy_final_receipts_require_development_selection(runtime, monkeypatc "checks": adapter.checks, "scope": scope, } + if has_deployment: + binding["deployment_search"] = "deploy" + if archived_rule is not None: + binding["acceptance"] = archived_rule doc = control.claim_final("campaign", control.read("campaign")["revision"], scope, binding) for check, cap in adapter.checks.items(): operation = "final-" + digest({"campaign": "campaign", "binding": binding, "check": check}) @@ -282,7 +403,21 @@ def assert_reservation_held(): assert lease is not None monkeypatch.setattr(adapter, "verify", lambda *args: pytest.fail("Legacy final must not rerun")) - kwargs = dict(holdout_scope=scope, accept_result=bool) + kwargs = dict(holdout_scope=scope) + if archived_rule is not None and archived_rule["version"] > 1: + def no_receipt_read(*args): + pytest.fail("An older reader must preserve the pending claim for a compatible version") + + monkeypatch.setattr(adapter, "recover", no_receipt_read) + monkeypatch.setattr(adapter, "recover_accounting", no_receipt_read) + for retry in ({}, {"acceptance": FinalAcceptance()}): + with pytest.raises(ValueError, match="(?i)(future|newer|unsupported)"): + finalize(reopened, "campaign", adapter, selected, **kwargs, **retry) + assert_reservation_held() + return + with pytest.raises(ValueError): + finalize(reopened, "campaign", adapter, selected, acceptance=FinalAcceptance(), **kwargs) + assert_reservation_held() path = adapter.root / "receipts" / (operation + ".json") delayed = path.rename(path.with_suffix(".delayed")) with pytest.raises(ValueError, match="Unknown final"): @@ -330,10 +465,10 @@ def historical(document): assert_reservation_held() saved_execution.rename(execution) result = finalize(reopened, "campaign", adapter, selected, **kwargs) - assert result["promoted"] is has_deployment - assert result["verdict"] == ("PASS" if has_deployment else "INCONCLUSIVE") + assert result["verdict"] == "INCONCLUSIVE" and not result["promoted"] + assert result["acceptance_error"] after = reopened.read("campaign") - assert ("stable" in after["state"]) == has_deployment + assert "stable" not in after["state"] assert after["operations"][operation]["status"] == "settled" assert after["operations"][operation]["actual_ms"] == receipt["elapsed_ms"] assert after["spent_ms"] == before["spent_ms"] + receipt["elapsed_ms"] @@ -359,7 +494,7 @@ def interrupt(*args, **kwargs): monkeypatch.setattr(control, "finish_final", interrupt) else: interrupt_after_last_final_receipt(control, adapter, monkeypatch) - kwargs = dict(holdout_scope="held", accept_result=bool) + kwargs = dict(holdout_scope="held", acceptance=FinalAcceptance()) with pytest.raises(RuntimeError, match="crash"): finalize(control, "campaign", adapter, selected, **kwargs) before = control.read("campaign") @@ -409,7 +544,7 @@ def qualify(authority): if stage == "root": return qualify_root(authority, campaign, adapter, selected, root.contract) return finalize(authority, campaign, adapter, selected, holdout_scope="held", - accept_result=bool) + acceptance=FinalAcceptance()) launch = control.launch interrupted = {} @@ -441,6 +576,16 @@ def no_adapter_access(*args): with pytest.raises(ValueError, match="did not start"): qualify(reopened) else: + before = reopened.read(campaign) + with pytest.raises(ValueError): + finalize(reopened, campaign, adapter, selected, holdout_scope="held", + acceptance=FinalAcceptance(minimum_objective=1)) + assert reopened.read(campaign) == before + with reopened.store._connection() as db: + held = db.execute( + "SELECT 1 FROM work_domains WHERE operation=?", (interrupted["operation"],) + ).fetchone() + assert held is not None final = qualify(reopened) assert final["verdict"] == "INCONCLUSIVE" and not final["promoted"] assert final["results"][-1]["not_started"] == "deadline" From a84eaafa9c514770e0960bc601e78aaa203fa011 Mon Sep 17 00:00:00 2001 From: amy-why-3459 Date: Tue, 24 Nov 2026 16:26:16 +0800 Subject: [PATCH 15/16] search: recover final checks interrupted before reservation Hold a campaign-scoped OS lock through finalization. After the writer exits, settle authenticated receipts and record never-reserved checks with zero usage. Verify missing checks and dispatch exclusion in the terminal transaction. Keep unknown work recoverable and reject dispatch after a terminal verdict. Agent-assisted-by: Codex (implementation and validation) Signed-off-by: amy-why-3459 --- docs/speculative-search.md | 15 +- src/inferrsi/harness/search_campaign.py | 48 +++++-- src/inferrsi/harness/work_control.py | 46 +++++- tests/test_vats_runtime.py | 184 ++++++++++++++++++++++++ tests/test_work_control.py | 48 +++++++ 5 files changed, 324 insertions(+), 17 deletions(-) diff --git a/docs/speculative-search.md b/docs/speculative-search.md index 8a4dc6dc3..7e76343f6 100644 --- a/docs/speculative-search.md +++ b/docs/speculative-search.md @@ -243,8 +243,19 @@ identical case inputs across splits. Changing JSON formatting, case order, IDs or provenance gives the same set of inputs a single final allowance in the shared store. Exact bounded radix utility keeps quality ahead of token use. -After interruption, omit `acceptance` to apply the rule saved with the claim to -the existing receipts. Supplying a different rule is rejected. Earlier Python +The finalizer holds a campaign-specific OS file lock in the shared RunStore +from before the holdout claim through the final commit. A concurrent finalizer +gets a lock error while that call is active. After an interruption, call +`finalize` with the original candidate and scope and omit `acceptance` to +apply the saved rule. If the owner exited before reserving a required check, +recovery records that check as `not_started: unreserved` with zero usage. +It reconciles registered work from its receipts. Once accounting is complete, +any unreserved check makes the final `INCONCLUSIVE` and preserves the +incumbent. The holdout remains consumed. For older claims that predate the +lock, recovery can certify an unreserved check once the original campaign +deadline has passed or the campaign has blocked further dispatch. + +Supplying a different rule during recovery is rejected. Earlier Python callers that passed an `accept_result` callback must select a declarative rule for new finals. A historical claim without a verifiable rule can only settle usage and cleanup. The same applies when its selection cannot be verified or diff --git a/src/inferrsi/harness/search_campaign.py b/src/inferrsi/harness/search_campaign.py index eb80a6ef6..8f897f368 100644 --- a/src/inferrsi/harness/search_campaign.py +++ b/src/inferrsi/harness/search_campaign.py @@ -356,6 +356,22 @@ def finalize( A campaign with exactly one eligible deployment can omit the name. New claims require an explicit FinalAcceptance. Receipt recovery uses the rule in the original claim. """ + completed = control.read(campaign)["final_result"] + if completed is not None: + return completed + with control.final_owner(campaign) as stored_campaign: + return _finalize( + control, campaign, adapter, selected, holdout_scope=holdout_scope, + acceptance=acceptance, deployment_search=deployment_search, accept_result=accept_result, + stored_campaign=stored_campaign, + ) + + +def _finalize( + control, campaign, adapter, selected, *, holdout_scope, acceptance, deployment_search, + accept_result, stored_campaign, +): + # Re-read under the guard. Another finalizer may have finished since the read-only fast path. doc = control.read(campaign) if doc["final_result"] is not None: return doc["final_result"] @@ -380,6 +396,10 @@ def finalize( # Reuse the original rule bytes in the binding, even when this version cannot # interpret them. That binding identifies the already launched physical checks. stored = frozen["binding"] + if "final_owner" in stored: + if stored["final_owner"] != control.FINAL_OWNER_PROTOCOL: + raise ValueError("Unsupported final owner protocol") + binding["final_owner"] = stored["final_owner"] for name in ("acceptance", "objective_check"): if name in stored: binding[name] = stored[name] @@ -414,6 +434,7 @@ def finalize( else: if acceptance is None: raise ValueError("New final claims require an explicit FinalAcceptance rule") + binding["final_owner"] = control.FINAL_OWNER_PROTOCOL binding["acceptance"] = acceptance.document() rule = FinalAcceptance.from_document(binding["acceptance"]) @@ -465,15 +486,24 @@ def finalize( previous_scope_prefix=getattr(adapter.evaluator, "previous_holdout_scope_prefix", None), ) results = {} + unreserved = set() + operation_campaign = stored_campaign if "final_owner" in binding else campaign for check, cap in checks.items(): doc = control.read(campaign) - operation = "final-" + digest({"campaign": campaign, "binding": binding, "check": check}) + operation = "final-" + digest( + {"campaign": operation_campaign, "binding": binding, "check": check} + ) if recovering: op = doc["operations"].get(operation) if op is None: - if accounting_only: - continue # No reservation or launch to reconcile; never dispatch it. - raise ValueError("Interrupted holdout may be consumed; no new execution") + # No claim means no launch intent or charge. Never call the adapter for it. + # finish_final authenticates absence and legacy writer exclusion atomically. + unreserved.add(check) + results[check] = { + "check": check, "not_started": "unreserved", "verdict": "INCONCLUSIVE", + "objective": None, "elapsed_ms": 0, "cleanup_verified": True, + } + continue if op["status"] == "claimed": raise ValueError("Final reservation still awaits launch") if op["status"] == "settled": @@ -522,14 +552,15 @@ def finalize( actual_ms=result.get("elapsed_ms"), cleanup_verified=result.get("cleanup_verified"), ) + if result.get("cleanup_verified") is not True or result.get("elapsed_ms") is None: + raise ValueError("Unknown final usage or cleanup; recover the existing receipt") results[check] = result doc = control.read(campaign) unsettled = any(op["status"] != "settled" for op in doc["operations"].values()) - if accounting_only and unsettled: + if unsettled: raise ValueError("Unknown final usage or cleanup; recover its receipt without rerun") - unresolved = doc["blocked"] or unsettled complete = bool(checks) and results.keys() == checks.keys() - passed = complete and not accounting_only and not unresolved and rule.accepts( + passed = complete and not accounting_only and not doc["blocked"] and rule.accepts( results, binding.get("objective_check") ) result = { @@ -542,7 +573,7 @@ def finalize( result["selection_error"] = selection_error if acceptance_error is not None: result["acceptance_error"] = acceptance_error - if not complete or accounting_only or unresolved or any( + if not complete or accounting_only or doc["blocked"] or any( r["verdict"] == "INCONCLUSIVE" or r["cleanup_verified"] is not True for r in results.values() ): @@ -551,6 +582,7 @@ def finalize( campaign, binding, result, + unreserved=unreserved, transition=(lambda body: body | {"stable": asdict(selected)}) if passed else None, ) return result diff --git a/src/inferrsi/harness/work_control.py b/src/inferrsi/harness/work_control.py index f4f1c0dac..88f12c2eb 100644 --- a/src/inferrsi/harness/work_control.py +++ b/src/inferrsi/harness/work_control.py @@ -11,7 +11,7 @@ import time from contextlib import contextmanager -from .artifact_primitives import digest +from .artifact_primitives import digest, file_lock from .run_store import _transaction @@ -25,6 +25,9 @@ def _positive(value): class WorkControl: + # Included in the binding so an older finalizer cannot join a guarded claim. + FINAL_OWNER_PROTOCOL = "campaign-file-lock-v1" + def __init__(self, store): self.store = store if store._read_only: @@ -104,6 +107,20 @@ def read(self, campaign): raise ValueError("Frozen campaign contract changed") return {"revision": row[0], **body} + @contextmanager + def final_owner(self, campaign): + """Exclude live finalizers across processes using the same campaign database.""" + if self.store._read_only: + raise ValueError("Recovering a pending final requires a writable RunStore") + # SQLite's TEXT key can identify the same row for e.g. 1 and "1". + # Derive both the lock and new operation IDs from the stored identity. + with self.store._connection() as db: + row = db.execute("SELECT id FROM work_campaigns WHERE id=?", (campaign,)).fetchone() + if row is None: + raise ValueError("Unknown campaign") + with file_lock(self.store.root / (".final-" + digest(row[0]) + ".lock")): + yield row[0] + @staticmethod def available(body, *, final=False): held = sum( @@ -143,6 +160,8 @@ def claim( with self._transaction(campaign, revision) as (db, body): if operation in body["operations"]: raise ValueError("Operation already claimed; recover without relaunch") + if body["final_result"] is not None: + raise ValueError("Campaign final result is immutable") if body["blocked"] or (body["final_claim"] is not None and not final): raise ValueError("Campaign is blocked or final selection is frozen") if final and body["final_claim"] is None: @@ -184,6 +203,8 @@ def reconcile_blocked(self, campaign, revision, operation, transition): @staticmethod def _stop_reason(body, op, now): + if body["final_result"] is not None: + return "finalized" if body["blocked"]: return "blocked" if now >= op["deadline"]: @@ -230,6 +251,8 @@ def launch(self, campaign, operation): op.update(status="launched", started_at=now) body["events"].append({"kind": "launch_intent", "operation": operation, "at": now}) # Signal refusal after the transaction has committed the release. + if reason == "finalized": + raise ValueError("Campaign finalized before launch") if reason == "blocked": raise ValueError("Campaign blocked before launch") if reason == "deadline": @@ -347,7 +370,7 @@ def claim_final(self, campaign, revision, scope, binding, *, previous_scope_pref body["final_claim"] = {"scope": scope, "binding": binding} return self.read(campaign) - def finish_final(self, campaign, binding, result, *, transition=None): + def finish_final(self, campaign, binding, result, *, transition=None, unreserved=()): with self._transaction(campaign) as (_, body): if body["final_claim"] is None or _json(body["final_claim"]["binding"]) != _json( binding @@ -357,11 +380,20 @@ def finish_final(self, campaign, binding, result, *, transition=None): if _json(body["final_result"]) != _json(result): raise ValueError("Final result is immutable; export the existing result") return - if transition is not None and ( - body["blocked"] - or any(op["status"] != "settled" for op in body["operations"].values()) - or result.get("verdict") != "PASS" - ): + if any(op["status"] != "settled" for op in body["operations"].values()): + raise ValueError("Unknown final usage or cleanup; recover the existing receipt") + if unreserved: + # Check absence again under the write transaction. A legacy owner did + # not take the OS lock and can still register work until dispatch stops. + if any(op["category"] == "final" and op["binding"].get("check") in unreserved + for op in body["operations"].values()): + raise ValueError("Final reservations changed; recover the registered checks") + if (binding.get("final_owner") != self.FINAL_OWNER_PROTOCOL + and not body["blocked"] and time.time() < body["deadline"]): + raise ValueError("Legacy final owner may still reserve checks before deadline") + if result.get("verdict") != "INCONCLUSIVE" or transition is not None: + raise ValueError("Unreserved final checks require an INCONCLUSIVE result") + if transition is not None and (body["blocked"] or result.get("verdict") != "PASS"): raise ValueError("Unresolved or over-budget work cannot promote a candidate") body["final_result"] = result if transition is not None: diff --git a/tests/test_vats_runtime.py b/tests/test_vats_runtime.py index a3659d4e1..47e3f1555 100644 --- a/tests/test_vats_runtime.py +++ b/tests/test_vats_runtime.py @@ -1,6 +1,8 @@ import json +import multiprocessing import sys import time +from contextlib import contextmanager from dataclasses import asdict, replace import pytest @@ -287,6 +289,188 @@ def test_final_authenticates_winner_before_claim(runtime, monkeypatch, substitut assert control.read("campaign") == before +def _wait_before_final_reservation(store_root, namespace, adapter, selected, check, ready, resume): + control = WorkControl(RunStore(store_root, namespace)) + claim = control.claim + + def wait(campaign, revision, operation, **options): + if options["category"] == "final" and options["binding"]["check"] == check: + ready.set() + if not resume.wait(30): + raise TimeoutError("Final writer was not released by the test") + return claim(campaign, revision, operation, **options) + + control.claim = wait + finalize(control, "campaign", adapter, selected, holdout_scope="held", + acceptance=FinalAcceptance()) + + +@contextmanager +def paused_final_writer(control, adapter, selected, check): + ctx = multiprocessing.get_context("spawn") + ready, resume = ctx.Event(), ctx.Event() + writer = ctx.Process( + target=_wait_before_final_reservation, + args=(control.store.root, control.store.namespace, adapter, selected, check, ready, resume), + ) + writer.start() + try: + assert ready.wait(15), "Writer did not reach the check reservation" + yield writer, resume + finally: + if writer.is_alive(): + writer.terminate() + writer.join(timeout=15) + assert not writer.is_alive() + writer.close() + + +def unreserved_final_result(check): + return { + "check": check, "verdict": "INCONCLUSIVE", "not_started": "unreserved", + "elapsed_ms": 0, "cleanup_verified": True, "objective": None, + } + + +@pytest.mark.parametrize("gap", ["quality", "objective"]) +def test_final_recovery_after_process_death_before_check_reservation(runtime, monkeypatch, gap): + control, adapter, _, _, root = runtime + stable = asdict(tree.accept(root)) + selected = tree.accept(decode(start_search_for_final(runtime).run()["tree"])) + assert selected.candidate_id != stable["candidate_id"] + before = control.read("campaign") + before = control.update( + "campaign", before["revision"], lambda state: state | {"stable": stable} + ) + with paused_final_writer(control, adapter, selected, gap) as (writer, _): + interrupted = control.read("campaign") + assert interrupted["final_claim"]["scope"] == "held" and interrupted["final_result"] is None + writer.terminate() # Simulate process death without running the finalizer's cleanup. + writer.join(timeout=15) + assert writer.exitcode not in (None, 0) + + physical = { + op["binding"]["check"]: op for op in interrupted["operations"].values() + if op["category"] == "final" + } + assert list(physical) == ([] if gap == "quality" else ["quality"]) + assert all(op["status"] == "settled" and op["actual_ms"] > 0 for op in physical.values()) + monkeypatch.setattr(adapter, "verify", lambda *args: pytest.fail("Final recovery redispatched")) + monkeypatch.setattr(adapter, "recover", lambda *args: pytest.fail("No unsettled receipts")) + reopened = WorkControl(RunStore(control.store.root, control.store.namespace)) + result = finalize(reopened, "campaign", adapter, selected, holdout_scope="held") + assert result["verdict"] == "INCONCLUSIVE" and not result["promoted"] + assert result["results"] == [ + physical[name]["receipt"] if name in physical else unreserved_final_result(name) + for name in adapter.checks + ] + after = reopened.read("campaign") + assert after["operations"] == interrupted["operations"] + physical_cost = sum(op["actual_ms"] for op in physical.values()) + assert after["spent_ms"] == before["spent_ms"] + physical_cost + assert after["state"] == before["state"] and after["final_claim"] == interrupted["final_claim"] + reopened.create("other", {}, {}, cap_ms=100, final_reserve_ms=10, deadline=time.time() + 60) + with pytest.raises(ValueError, match="consumed"): + reopened.claim_final("other", 0, "held", {}) + assert finalize(reopened, "campaign", adapter, selected, holdout_scope="held") == result + assert reopened.read("campaign") == after + + +@pytest.mark.parametrize("has_acceptance", [True, False]) +def test_unreserved_legacy_final_waits_for_campaign_deadline(runtime, monkeypatch, has_acceptance): + control, adapter, _, _, root = runtime + selected = tree.accept(root) + start_search_for_final(runtime, root_only=True).run() + # A deployment using the previous writer protocol may still reserve either check. + scope = "legacy-held" + binding = { + "candidate": asdict(selected), "adapter": adapter.identity, "checks": adapter.checks, + "scope": scope, "deployment_search": "deploy", + } + if has_acceptance: + binding["acceptance"] = FinalAcceptance().document() + revision = control.read("campaign")["revision"] + interrupted = control.claim_final("campaign", revision, scope, binding) + reopened = WorkControl(RunStore(control.store.root, control.store.namespace)) + monkeypatch.setattr(adapter, "verify", lambda *args: pytest.fail("Legacy final redispatched")) + with pytest.raises(ValueError): + finalize(reopened, "campaign", adapter, selected, holdout_scope=scope) + assert reopened.read("campaign") == interrupted + monkeypatch.setattr(time, "time", lambda: interrupted["deadline"] + 1) + result = finalize(reopened, "campaign", adapter, selected, holdout_scope=scope) + assert result["verdict"] == "INCONCLUSIVE" and not result["promoted"] + assert result["results"] == [unreserved_final_result(name) for name in adapter.checks] + after = reopened.read("campaign") + assert after["final_claim"] == interrupted["final_claim"] and "stable" not in after["state"] + assert after["operations"] == interrupted["operations"] + assert after["spent_ms"] == interrupted["spent_ms"] + + +def test_live_final_writer_can_finish_after_another_controller_attempts_recovery(runtime): + control, adapter, _, _, root = runtime + selected = tree.accept(root) + start_search_for_final(runtime, root_only=True).run() + with paused_final_writer(control, adapter, selected, "objective") as (writer, resume): + observer = WorkControl(RunStore(control.store.root, control.store.namespace)) + before = observer.read("campaign") + assert before["final_result"] is None + with pytest.raises((ValueError, BlockingIOError)): + finalize(observer, "campaign", adapter, selected, holdout_scope="held") + assert observer.read("campaign") == before + resume.set() + writer.join(timeout=15) + assert writer.exitcode == 0 + doc = control.read("campaign") + assert doc["final_result"]["verdict"] == "PASS" and doc["final_result"]["promoted"] + assert len([op for op in doc["operations"].values() if op["category"] == "final"]) == 2 + + +def test_missing_next_final_reservation_waits_for_started_check_cleanup(runtime, monkeypatch): + control, adapter, _, _, root = runtime + selected = tree.accept(root) + start_search_for_final(runtime, root_only=True).run() + + def interrupt(*args, **kwargs): + raise RuntimeError("Stopped after physical receipt and before settlement") + + monkeypatch.setattr(control, "settle", interrupt) + with pytest.raises(RuntimeError, match="before settlement"): + finalize(control, "campaign", adapter, selected, holdout_scope="held", + acceptance=FinalAcceptance()) + reopened = WorkControl(RunStore(control.store.root, control.store.namespace)) + before = reopened.read("campaign") + (operation,) = [key for key, op in before["operations"].items() if op["category"] == "final"] + assert before["operations"][operation]["status"] == "launched" + path = adapter.root / "receipts" / (operation + ".json") + receipt = json.loads(path.read_text())["result"] + monkeypatch.setattr(adapter, "verify", lambda *args: pytest.fail("Recovery redispatched")) + for observation in (None, receipt | {"cleanup_verified": False}): + with monkeypatch.context() as partial: + partial.setattr(adapter, "recover", lambda *args: observation) + with pytest.raises(ValueError): + finalize(reopened, "campaign", adapter, selected, holdout_scope="held") + pending = reopened.read("campaign") + assert pending["final_result"] is None and pending["spent_ms"] == before["spent_ms"] + assert pending["operations"][operation]["status"] in ("launched", "unknown") + with reopened.store._connection() as db: + lease = db.execute( + "SELECT 1 FROM work_domains WHERE operation=?", (operation,) + ).fetchone() + assert lease is not None + result = finalize(reopened, "campaign", adapter, selected, holdout_scope="held") + assert result["verdict"] == "INCONCLUSIVE" and not result["promoted"] + assert result["results"] == [receipt, unreserved_final_result("objective")] + after = reopened.read("campaign") + assert after["spent_ms"] == before["spent_ms"] + receipt["elapsed_ms"] + assert after["operations"][operation]["status"] == "settled" + assert set(after["operations"]) == set(before["operations"]) + with reopened.store._connection() as db: + lease = db.execute("SELECT 1 FROM work_domains WHERE operation=?", (operation,)).fetchone() + assert lease is None + assert finalize(reopened, "campaign", adapter, selected, holdout_scope="held") == result + assert reopened.read("campaign") == after + + def interrupt_after_last_final_receipt(control, adapter, monkeypatch): settle = control.settle diff --git a/tests/test_work_control.py b/tests/test_work_control.py index 10add399e..f08a26e2b 100644 --- a/tests/test_work_control.py +++ b/tests/test_work_control.py @@ -207,6 +207,35 @@ def test_holdout_is_once_across_campaigns_even_after_interruption(control): control.finish_final("campaign", {"candidate": "c"}, {"verdict": "FAIL"}) with pytest.raises(ValueError, match="immutable"): control.finish_final("campaign", {"candidate": "c"}, {"verdict": "PASS"}) + with pytest.raises(ValueError, match="immutable"): + claim(control, final=True, category="final") + + +def test_final_commit_rechecks_unreserved_operations_and_outstanding_usage(control): + binding = {"candidate": "c"} + result = {"verdict": "INCONCLUSIVE", "promoted": False} + control.claim_final("campaign", 0, "held", binding) + claim(control, "check", final=True, category="final", binding={"check": "objective"}) + control.launch("campaign", "check") + before = control.read("campaign") + with pytest.raises(ValueError, match="Unknown final usage"): + control.finish_final("campaign", binding, result) + assert control.read("campaign") == before + control.settle("campaign", "check", {"done": True}, actual_ms=1, cleanup_verified=True) + settled = control.read("campaign") + with pytest.raises(ValueError, match="reservations changed"): + control.finish_final("campaign", binding, result, unreserved=["objective"]) + assert control.read("campaign") == settled + + +def test_final_owner_uses_the_stored_campaign_identity(control): + control.create("1", {}, {}, cap_ms=500, final_reserve_ms=100, deadline=time.time() + 60) + with control.final_owner("1") as stored: + assert stored == "1" + with pytest.raises(BlockingIOError), control.final_owner(1): + pytest.fail("SQLite aliases must not acquire a second finalizer") + with control.final_owner(1) as stored: + assert stored == "1" def test_final_reserve_and_unknown_work_cannot_be_spent(control): @@ -217,6 +246,25 @@ def test_final_reserve_and_unknown_work_cannot_be_spent(control): control.claim_final("campaign", control.read("campaign")["revision"], "holdout", {}) +def test_historical_terminal_final_cannot_launch_a_retained_reservation(control): + control.claim_final("campaign", 0, "held", {}) + claim(control, "pending", final=True, category="final") + claim(control, "started", final=True, category="final", domains=("other-device",)) + control.launch("campaign", "started") + # Earlier versions could commit a non-promoting terminal result with unsettled work. + result = {"verdict": "INCONCLUSIVE", "promoted": False} + with control._transaction("campaign") as (_, body): + body["final_result"] = result + with pytest.raises(ValueError, match="finalized"): + control.launch("campaign", "pending") + doc = control.recover_unstarted("campaign") + assert doc["operations"]["pending"]["receipt"] == {"not_started": "finalized"} + assert doc["operations"]["started"]["status"] == "launched" + assert doc["final_result"] == result and doc["spent_ms"] == 0 + with control.store._connection() as db: + assert [row[0] for row in db.execute("SELECT operation FROM work_domains")] == ["started"] + + def test_over_budget_final_is_accounted_but_never_promoted(control): control.claim_final("campaign", 0, "holdout", {"candidate": "c"}) claim(control, final=True, category="final") From 8af091b91ae43228e9adf2e98e5542b54262352e Mon Sep 17 00:00:00 2001 From: amy-why-3459 Date: Wed, 25 Nov 2026 09:09:21 +0800 Subject: [PATCH 16/16] search: align tests and module guides with main Move the seven search suites into their owning components, update the Qwen diagnostic fixture path and test commands, and retain the search entry points in the source-adjacent module guides. Agent-assisted-by: Codex (rebase and validation) Signed-off-by: amy-why-3459 --- docs/speculative-search.md | 6 +++--- src/inferrsi/apps/README.md | 4 ++++ src/inferrsi/harness/README.md | 5 +++++ tests/{ => apps}/test_search_benchmarks.py | 0 tests/{ => apps}/test_search_roles.py | 0 tests/{ => campaigns}/test_qwen_gvo_diagnostic.py | 2 +- tests/{ => harness}/test_speculative_search.py | 0 tests/{ => harness}/test_vats_runtime.py | 0 tests/{ => harness}/test_verification_plans.py | 0 tests/{ => harness}/test_work_control.py | 0 10 files changed, 13 insertions(+), 4 deletions(-) rename tests/{ => apps}/test_search_benchmarks.py (100%) rename tests/{ => apps}/test_search_roles.py (100%) rename tests/{ => campaigns}/test_qwen_gvo_diagnostic.py (98%) rename tests/{ => harness}/test_speculative_search.py (100%) rename tests/{ => harness}/test_vats_runtime.py (100%) rename tests/{ => harness}/test_verification_plans.py (100%) rename tests/{ => harness}/test_work_control.py (100%) diff --git a/docs/speculative-search.md b/docs/speculative-search.md index 7e76343f6..61ae3536e 100644 --- a/docs/speculative-search.md +++ b/docs/speculative-search.md @@ -458,9 +458,9 @@ raw-data verification instructions. ## Validation and issue completion ```bash -python -m pytest tests/test_speculative_search.py tests/test_verification_plans.py \ - tests/test_work_control.py tests/test_search_roles.py tests/test_vats_runtime.py \ - tests/test_search_benchmarks.py tests/test_qwen_gvo_diagnostic.py +python -m pytest tests/harness/test_speculative_search.py tests/harness/test_verification_plans.py \ + tests/harness/test_work_control.py tests/apps/test_search_roles.py tests/harness/test_vats_runtime.py \ + tests/apps/test_search_benchmarks.py tests/campaigns/test_qwen_gvo_diagnostic.py ``` Controls cover exhaustive planning, failed-parent repair, finite-score overflow, diff --git a/src/inferrsi/apps/README.md b/src/inferrsi/apps/README.md index 8522f1232..7d70e600c 100644 --- a/src/inferrsi/apps/README.md +++ b/src/inferrsi/apps/README.md @@ -15,6 +15,10 @@ owns grading rules and Harness owns execution and retained run facts. - [`workflows/`](workflows/) composes application workflows. [`handoff/`](handoff/) contains host-facing adapters, and [`storage/`](storage/) handles application publication and reads. +- [`workflows/speculative_search.py`](workflows/speculative_search.py) connects + the Engine search CLI to Harness. [`search_benchmarks.py`](workflows/search_benchmarks.py) + supplies Engine and Skill evaluators, and [`search_roles.py`](workflows/search_roles.py) + implements the trainable CPU roles. ## Benchmark launcher diff --git a/src/inferrsi/harness/README.md b/src/inferrsi/harness/README.md index 6f8bef928..d8753df1b 100644 --- a/src/inferrsi/harness/README.md +++ b/src/inferrsi/harness/README.md @@ -23,6 +23,11 @@ measurement and assessment rules. skill-evaluation trials. [`artifact_sandbox.py`](artifact_sandbox.py) captures and replays submitted artifacts. [`improvement.py`](improvement.py) advances search using injected assessments. +- [`vats.py`](vats.py) selects candidate edits and verification jobs. + [`search_campaign.py`](search_campaign.py) coordinates VATS, GVO updates, + and final evaluation. [`work_control.py`](work_control.py) persists claims, + receipts and budget accounting in the shared RunStore. See the + [speculative search design](../../../docs/speculative-search.md) for recovery rules. ## Evaluate retained runs diff --git a/tests/test_search_benchmarks.py b/tests/apps/test_search_benchmarks.py similarity index 100% rename from tests/test_search_benchmarks.py rename to tests/apps/test_search_benchmarks.py diff --git a/tests/test_search_roles.py b/tests/apps/test_search_roles.py similarity index 100% rename from tests/test_search_roles.py rename to tests/apps/test_search_roles.py diff --git a/tests/test_qwen_gvo_diagnostic.py b/tests/campaigns/test_qwen_gvo_diagnostic.py similarity index 98% rename from tests/test_qwen_gvo_diagnostic.py rename to tests/campaigns/test_qwen_gvo_diagnostic.py index 05731470d..d7765305d 100644 --- a/tests/test_qwen_gvo_diagnostic.py +++ b/tests/campaigns/test_qwen_gvo_diagnostic.py @@ -9,7 +9,7 @@ from inferrsi.benchmark.registry import task_resources -BUNDLE = Path(__file__).resolve().parents[1] / "evidence/qwen-vats-gvo-910b1-20261124" +BUNDLE = Path(__file__).resolve().parents[2] / "evidence/qwen-vats-gvo-910b1-20261124" def module(relative): diff --git a/tests/test_speculative_search.py b/tests/harness/test_speculative_search.py similarity index 100% rename from tests/test_speculative_search.py rename to tests/harness/test_speculative_search.py diff --git a/tests/test_vats_runtime.py b/tests/harness/test_vats_runtime.py similarity index 100% rename from tests/test_vats_runtime.py rename to tests/harness/test_vats_runtime.py diff --git a/tests/test_verification_plans.py b/tests/harness/test_verification_plans.py similarity index 100% rename from tests/test_verification_plans.py rename to tests/harness/test_verification_plans.py diff --git a/tests/test_work_control.py b/tests/harness/test_work_control.py similarity index 100% rename from tests/test_work_control.py rename to tests/harness/test_work_control.py