rein-aharness/tests/test_repository_artifact_bwrap.py
tegwick 3e4c976090
Some checks failed
Governed runtime contract / contract (push) Failing after 23s
Bind the metered owner route to worker leases and sandbox lifecycle
Assistant: codex
Assistant-Model: gpt-6-astra
Assistant-Session: 01a07ff8-19d0-7820-b4d0-1353833cb7fc
2026-09-09 22:24:37 +02:00

243 lines
10 KiB
Python

"""Opt-in real sandbox/Glas/worker proof; queue and task authoring are fixtures."""
from __future__ import annotations
import json
from datetime import UTC, datetime, timedelta
import os
from pathlib import Path
from unittest.mock import MagicMock
import pytest
from glas_harness.contract import (
ExecutionSummary,
ExecutionLimits,
OperationalReadiness,
Rein,
ToolResult,
)
from glas_harness.gateway import run_execution
from glas_harness.profiles import ProfileCatalog
from glas_harness.transport import transport_from_sandbox
from sandboxer.core.manager import SandboxManager
from sandboxer.lifecycle.store import SandboxStore
from rein_aharness.claim_loop import process_one
from rein_aharness.close_outbox import CloseOutbox
from rein_aharness.metrics import external_metrics_dir
from rein_aharness.ops_run_client import (
ActivityCoreOpsClient,
OpsRun,
OpsRunConfig,
OpsRunError,
)
from test_repository_artifact import commit, git
from rein_aharness.repository_grant import RepositoryGrant
from rein_aharness.spend_admission import SpendLedger, SpendPolicy, digest
from rein_aharness.messages_owner import MessagesOwner
from rein_aharness.request_admission import RequestLedger
from llm_connect.messages_gate import MessagesPolicy
from test_request_admission import fake_provider as fake_provider
pytestmark = pytest.mark.skipif(
os.environ.get("REIN_REAL_BWRAP") != "1", reason="opt-in kernel namespace proof"
)
class DeterministicRein(Rein):
calls = 0
workspace = None
head = None
def __init__(self, source, baseline, metered=False):
self.source, self.baseline = source, baseline
self.metered = metered
def start_session(self, profile, inputs, sandbox):
transport = transport_from_sandbox(sandbox)
self.workspace = Path(transport.workspace)
return transport
def dispatch_tool(self, transport, tool_call):
self.calls += 1
if self.metered:
response = transport.run(["python3", "-c", """import os,json,http.client,urllib.parse
u=urllib.parse.urlsplit(os.environ['ANTHROPIC_BASE_URL'])
c=http.client.HTTPConnection(u.hostname,u.port,timeout=5)
c.request('POST','/v1/messages',json.dumps({'model':'fixture-model','max_tokens':1000,'stream':True,'messages':[{'role':'user','content':'fixture only'}]}),{'Content-Type':'application/json','x-api-key':os.environ['ANTHROPIC_API_KEY']})
r=c.getresponse(); assert r.status==200; assert b'message_stop' in r.read(); c.close()
"""], timeout=10)
assert response.returncode == 0, response.stderr
code = """from pathlib import Path
import subprocess
Path('result.txt').write_text('sandbox-result-only\\n')
def git(*args):
p=subprocess.run(['git',*args],check=True,capture_output=True,text=True)
return p.stdout.strip()
git('add','result.txt')
git('-c','user.name=Fixture','-c','user.email=fixture@example.invalid','commit','-qm','bounded result')
print(git('rev-parse','HEAD'))
"""
result = transport.run(["python3", "-c", code], timeout=30)
assert result.returncode == 0, result.stderr
self.head = result.stdout.strip()
assert git(self.source, "rev-parse", "HEAD") == self.baseline
assert not (self.source / "result.txt").exists()
return ToolResult(ok=True, output="sandbox-result-only", tokens_spent=0)
def end_session(self, session):
return ExecutionSummary(
committed=True, commit_sha=self.head, outcome="succeeded", tokens_spent=0, cost_usd=0.0
)
def cleanup_session(self, session):
assert self.workspace.exists()
assert git(self.source, "rev-parse", "HEAD") == self.baseline
@pytest.mark.parametrize("with_spend", [False, True, "requests"])
def test_real_bwrap_worker_import_and_close_replay(tmp_path, monkeypatch, with_spend, fake_provider):
monkeypatch.setenv("XDG_DATA_HOME", str(tmp_path / "data"))
monkeypatch.setenv("REIN_AHARNESS_STATE_DIR", str(tmp_path / "state"))
monkeypatch.setenv("SANDBOXER_NO_STATE_HUB", "1")
source = tmp_path / "source"
source.mkdir()
git(source, "init", "-q")
baseline = commit(source, "README.md", "baseline\n")
grant = RepositoryGrant.from_mapping(
{
"version": "1",
"allowed_paths": ["result.txt"],
"commit_count": {"min": 1, "max": 1},
"publish": False,
}
)
run = OpsRun(
id="fixture-run",
activity_definition_id="fixture-definition",
idempotency_key="fixture-key",
target_repo=str(source),
title="deterministic fixture",
description="no inference",
state="claimed",
harness_profile_ref="harness.agent-dev-local@1.0.0",
repository_grant=grant,
claim_owner="fixture-worker",
attempt=1,
lease_until=(datetime.now(UTC) + timedelta(seconds=90)).isoformat(),
)
client = MagicMock(spec=ActivityCoreOpsClient)
client.config = OpsRunConfig(
worker_id="fixture-worker", lease_seconds=90, repo_roots=(str(tmp_path),)
)
client.claim.return_value = [run]
client.heartbeat.return_value = run
client.complete.side_effect = [
OpsRunError("response lost", action="complete"),
OpsRun(
id=run.id,
activity_definition_id="fixture-definition",
idempotency_key="fixture-key",
target_repo=str(source),
title=run.title,
description=run.description,
state="succeeded",
close_disposition="reconciled",
),
]
catalog = ProfileCatalog()
profile, _ = catalog.resolve(run.harness_profile_ref)
catalog.profiles()[(profile.id, profile.version)] = profile.model_copy(
update={
"limits": ExecutionLimits(max_budget_usd=4.0, max_turns=8),
"operational_readiness": OperationalReadiness(
status="ready",
reason="isolated test fixture",
owner="tests",
evidence_ref="test:artifact-bwrap",
)
}
)
if with_spend == "requests":
admitted, _ = catalog.resolve(run.harness_profile_ref)
catalog.profiles()[(admitted.id, admitted.version)] = admitted.model_copy(update={
"model": admitted.model.model_copy(update={"model": "fixture-model"}),
"sandbox_profile": "profile.bwrap-local", "credential_route_refs": [],
})
spend = None
if with_spend:
private = tmp_path / "spend-state"
private.mkdir(mode=0o700)
admitted_profile, descriptor = catalog.resolve(run.harness_profile_ref)
policy = SpendPolicy(
version="1", envelope_id="bwrap-fixture", authority_ref="test:no-live-authority",
valid_from="2026-09-01T00:00:00Z", expires_at="2099-01-01T00:00:00Z",
timezone="Europe/Berlin", worker_id="fixture-worker",
activity_definition_id="fixture-definition", target_repo=str(source),
project="fixture-factory", profile_ref=run.harness_profile_ref,
profile_sha256=digest(admitted_profile.model_dump(mode="json")),
descriptor_sha256=digest(descriptor.model_dump(mode="json")),
repository_grant_id=grant.grant_id, max_budget_usd="4", max_liability_usd="5",
max_turns=8, eur_per_usd="1", per_run_eur="5", daily_eur="10", total_eur="15",
)
policy_path = private / "policy.json"
policy_path.write_text(json.dumps(policy.__dict__))
policy_path.chmod(0o600)
spend = SpendLedger(private / "spend.sqlite3", policy)
spend.initialize()
client.config.execution_project = "fixture-factory"
client.config.spend_policy_path = str(policy_path)
client.config.spend_ledger_path = str(spend.path)
monkeypatch.setattr("glas_harness.profiles.ProfileCatalog", lambda: catalog)
if with_spend == "requests":
RequestLedger(spend).initialize()
provider, seen, _ = fake_provider
client.config.messages_owner = MessagesOwner(
MessagesPolicy("fixture:upper-rate", "fixture-model", 1000, 1000, 1000, 1000),
"fixture-owner-key", f"http://127.0.0.1:{provider.server_port}", True,
)
client.config.require_request_admission = True
manager = SandboxManager(store=SandboxStore(tmp_path / "sandboxes.json"))
rein = DeterministicRein(source, baseline, metered=with_spend == "requests")
monkeypatch.setattr(
"glas_harness.gateway.run_execution",
lambda request, **kwargs: run_execution(
request, catalog=kwargs.pop("catalog", catalog), rein=rein, manager=kwargs.pop("manager", manager), **kwargs
),
)
outbox = CloseOutbox(state_dir=tmp_path / "state")
first = process_one(client, outbox=outbox, report_to_hub=False)
assert first.reason == "close evidence remains pending", first
assert not rein.workspace.exists()
assert git(source, "rev-parse", "HEAD") == rein.head
assert (source / "result.txt").read_text() == "sandbox-result-only\n"
assert not git(source, "status", "--porcelain")
assert outbox.status()["pending"] == 1
record = json.loads(
(external_metrics_dir(source, "rein-aharness") / "executions.jsonl")
.read_text()
.strip()
)
assert record["success"]
durable = json.dumps(client.complete.call_args.kwargs["result"])
assert rein.head in durable
assert "sandbox-result-only" not in durable and '"bundle"' not in durable
client.claim.return_value = []
second = process_one(client, outbox=outbox, report_to_hub=False)
assert second.empty and rein.calls == 1
assert outbox.status() == {"pending": 0, "delivered": 1, "quarantined": 0}
if spend is not None:
rows = spend.status()["reservations"]
assert len(rows) == 1 and rows[0]["state"] == "charged"
assert rows[0]["liability"] == 5_000_000
client.claim.return_value = [run]
replay = process_one(client, outbox=outbox, report_to_hub=False)
assert not replay.ok and rein.calls == 1
assert client.complete.call_count == 2
assert client.heartbeat.call_count >= 1
assert git(source, "rev-list", "--count", "HEAD") == "2"
if with_spend == "requests":
assert len(seen) == 1 and seen[0]["key"] == "fixture-owner-key"
assert RequestLedger(spend).status()[0]["state"] == "charged"
with spend._db() as db:
route = db.execute("SELECT * FROM request_routes").fetchone()
assert route["revoked"] == 1 and route["expires_at"] == run.lease_until