Bind the metered owner route to worker leases and sandbox lifecycle
Some checks failed
Governed runtime contract / contract (push) Failing after 23s

Assistant: codex
Assistant-Model: gpt-6-astra
Assistant-Session: 01a07ff8-19d0-7820-b4d0-1353833cb7fc
This commit is contained in:
tegwick 2026-09-09 22:24:37 +02:00
parent c30806b968
commit 3e4c976090
13 changed files with 502 additions and 9 deletions

View file

@ -2,6 +2,7 @@
from __future__ import annotations
import json
from datetime import UTC, datetime, timedelta
import os
from pathlib import Path
from unittest.mock import MagicMock
@ -31,6 +32,10 @@ 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
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"
@ -42,8 +47,9 @@ class DeterministicRein(Rein):
workspace = None
head = None
def __init__(self, source, baseline):
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)
@ -52,6 +58,14 @@ class DeterministicRein(Rein):
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')
@ -79,8 +93,8 @@ print(git('rev-parse','HEAD'))
assert git(self.source, "rev-parse", "HEAD") == self.baseline
@pytest.mark.parametrize("with_spend", [False, True])
def test_real_bwrap_worker_import_and_close_replay(tmp_path, monkeypatch, with_spend):
@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")
@ -108,6 +122,7 @@ def test_real_bwrap_worker_import_and_close_replay(tmp_path, monkeypatch, with_s
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(
@ -141,6 +156,12 @@ def test_real_bwrap_worker_import_and_close_replay(tmp_path, monkeypatch, with_s
)
}
)
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"
@ -166,12 +187,20 @@ def test_real_bwrap_worker_import_and_close_replay(tmp_path, monkeypatch, with_s
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)
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=manager, **kwargs
request, catalog=kwargs.pop("catalog", catalog), rein=rein, manager=kwargs.pop("manager", manager), **kwargs
),
)
outbox = CloseOutbox(state_dir=tmp_path / "state")
@ -205,3 +234,10 @@ def test_real_bwrap_worker_import_and_close_replay(tmp_path, monkeypatch, with_s
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