Reserve worker spend durably before governed dispatch
Some checks failed
Governed runtime contract / contract (push) Has been cancelled

Assistant: codex
Assistant-Model: gpt-5.6-luna
Assistant-Session: 01a07ff8-19d0-7820-b4d0-1353833cb7fc
This commit is contained in:
tegwick 2026-09-09 17:58:27 +02:00
parent 4ffb8acb19
commit 38b25fbc26
14 changed files with 1378 additions and 9 deletions

View file

@ -9,6 +9,7 @@ import pytest
from glas_harness.contract import (
ExecutionSummary,
ExecutionLimits,
OperationalReadiness,
Rein,
ToolResult,
@ -29,6 +30,7 @@ from rein_aharness.ops_run_client import (
)
from test_repository_artifact import commit, git
from rein_aharness.repository_grant import RepositoryGrant
from rein_aharness.spend_admission import SpendLedger, SpendPolicy, digest
pytestmark = pytest.mark.skipif(
os.environ.get("REIN_REAL_BWRAP") != "1", reason="opt-in kernel namespace proof"
@ -69,7 +71,7 @@ print(git('rev-parse','HEAD'))
def end_session(self, session):
return ExecutionSummary(
committed=True, commit_sha=self.head, outcome="succeeded", tokens_spent=0
committed=True, commit_sha=self.head, outcome="succeeded", tokens_spent=0, cost_usd=0.0
)
def cleanup_session(self, session):
@ -77,7 +79,8 @@ print(git('rev-parse','HEAD'))
assert git(self.source, "rev-parse", "HEAD") == self.baseline
def test_real_bwrap_worker_import_and_close_replay(tmp_path, monkeypatch):
@pytest.mark.parametrize("with_spend", [False, True])
def test_real_bwrap_worker_import_and_close_replay(tmp_path, monkeypatch, with_spend):
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")
@ -129,6 +132,7 @@ def test_real_bwrap_worker_import_and_close_replay(tmp_path, monkeypatch):
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",
@ -137,12 +141,37 @@ def test_real_bwrap_worker_import_and_close_replay(tmp_path, monkeypatch):
)
}
)
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)
manager = SandboxManager(store=SandboxStore(tmp_path / "sandboxes.json"))
rein = DeterministicRein(source, baseline)
monkeypatch.setattr(
"glas_harness.gateway.run_execution",
lambda request, **kwargs: run_execution(
request, catalog=catalog, rein=rein, manager=manager, **kwargs
request, catalog=kwargs.pop("catalog", catalog), rein=rein, manager=manager, **kwargs
),
)
outbox = CloseOutbox(state_dir=tmp_path / "state")
@ -165,7 +194,14 @@ def test_real_bwrap_worker_import_and_close_replay(tmp_path, monkeypatch):
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"
assert outbox.status() == {"pending": 0, "delivered": 1, "quarantined": 0}