Delete the built-in bridges and the legacy harness / usage modules now owned
by ai-agent-plugins (harness_builtin, claude_cli, claude_accounts,
claude_usage, pi_runs, opencode_{cli,mirror,tools,usage},
agy_{cli,mirror,tools,usage}). routes_runs / fork keep only the routes; the
harness-* plugins' drivers launch.
- harness_loader: no builtins; the first read boots the registry; a live dir
never materialized falls back to .lkg, then (host only) the seed plugins
(SIDECAR_SEED_PLUGINS_DIR, default ~/projects/ai-agent-plugins/plugins);
codeless provider entries keep their manifest + config.json; module() and
forget_status() accessors; hub-written absolute dirs resolved by name.
- usage_loader: provider_configs() covers every provider plugin, evox2()
reads servers + Wake-on-LAN from provider-evox2 (local_ready, gen_progress
use it — no duplicated EVOX2 constants).
- core helpers kept self-contained: claude_env.py (run env, account env,
machine login), run_errors.py (typed errors); launch._stop_run calls the
driver's stop_extra; /accounts?fresh=1 and guest listing go through drivers;
gen_progress reads the opencode db via harness_loader.module; tools_check
via driver status; tools_install has its own gh device-login pump.
- /sync accepts a provider bundle without a usage reader.
- backend workers/labsync: always send harnesses + providers maps ({} clears
a worker); guests only get harnesses whose own plugin.json says
guestAllowed.
- tests + A/B scripts run the real plugins (conftest live index + legacy-name
import hook; launch-dump / _mirror_worker load the plugins for a tree with
no bridges). install.sh points the host unit at the hub's live dirs.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
153 lines
5.9 KiB
Python
153 lines
5.9 KiB
Python
"""The guests' own Claude logins' /usage reader (the provider-anthropic
|
|
plugin's ``sidecar/usage.py``, under its old name ``claude_usage``): the API's
|
|
reply → the status line's rate-limits.json, read only with a good token."""
|
|
|
|
import json
|
|
import time
|
|
|
|
import claude_usage as cu
|
|
import config
|
|
|
|
def _set(mp, name, value):
|
|
"""A setting: the reader reads ``config.<name>`` live (and may keep a
|
|
copy) — both see the test's value."""
|
|
mp.setattr(config, name, value)
|
|
if name in vars(cu):
|
|
mp.setattr(cu, name, value)
|
|
|
|
|
|
REPLY = {
|
|
"five_hour": {"utilization": 7.0, "resets_at": "2026-11-07T04:20:00.166212+00:00"},
|
|
"seven_day": {"utilization": 49.0, "resets_at": "2026-11-07T23:00:00+00:00"},
|
|
"seven_day_opus": None,
|
|
"seven_day_sonnet": {"utilization": 3, "resets_at": None},
|
|
"seven_day_cowork": {"utilization": 80, "resets_at": None},
|
|
"extra_usage": {"is_enabled": False, "utilization": None},
|
|
}
|
|
|
|
|
|
class _Reader:
|
|
"""A fresh reader per test (its ``last`` is per instance), on the
|
|
sidecar's own context — it reads ``config`` live."""
|
|
obj = None
|
|
|
|
def __getattr__(self, name):
|
|
if _Reader.obj is None:
|
|
import harness_base
|
|
_Reader.obj = cu.create(harness_base.make_context())
|
|
return getattr(_Reader.obj, name)
|
|
|
|
|
|
RD = _Reader()
|
|
|
|
|
|
def _guests(tmp_path, monkeypatch, expires_in=3600, **dirs):
|
|
_Reader.obj = None
|
|
root = tmp_path / "guests"
|
|
monkeypatch.setattr(config, "IS_WORKER", False)
|
|
monkeypatch.setattr(config, "GUEST_ACCOUNTS_DIR", root)
|
|
_set(monkeypatch, "IS_WORKER", False)
|
|
_set(monkeypatch, "GUEST_ACCOUNTS", True)
|
|
_set(monkeypatch, "GUEST_ACCOUNTS_DIR", root)
|
|
_set(monkeypatch, "ACCOUNTS", {})
|
|
for uid, creds in dirs.items():
|
|
d = root / uid
|
|
d.mkdir(parents=True)
|
|
if creds:
|
|
(d / ".credentials.json").write_text(json.dumps({"claudeAiOauth": {
|
|
"accessToken": f"tok-{uid}", "expiresAt": (time.time() + expires_in) * 1000}}))
|
|
return root
|
|
|
|
|
|
def test_parse_keeps_plan_windows_as_epochs():
|
|
out = cu.parse(REPLY)["rate_limits"]
|
|
assert set(out) == {"five_hour", "seven_day", "seven_day_sonnet"}
|
|
assert out["seven_day"]["used_percentage"] == 49.0
|
|
assert out["five_hour"]["resets_at"] == 1794025200.166212
|
|
assert out["seven_day_sonnet"] == {"used_percentage": 3.0, "resets_at": None}
|
|
assert cu.parse({"five_hour": None})["rate_limits"] == {}
|
|
assert cu.parse("nope")["rate_limits"] == {}
|
|
|
|
|
|
def test_accounts_are_guest_dirs_with_a_login(tmp_path, monkeypatch):
|
|
_guests(tmp_path, monkeypatch, akira={"x": 1}, dom=None)
|
|
assert RD.accounts() == ["guest-akira"]
|
|
_set(monkeypatch, "IS_WORKER", True)
|
|
assert RD.accounts() == [] # a worker never hosts guests
|
|
|
|
|
|
def test_poll_writes_the_status_line_shape(tmp_path, monkeypatch):
|
|
root = _guests(tmp_path, monkeypatch, akira={"x": 1})
|
|
seen = {}
|
|
|
|
class Reply:
|
|
def __enter__(self):
|
|
return self
|
|
|
|
def __exit__(self, *a):
|
|
return False
|
|
|
|
def read(self):
|
|
return json.dumps(REPLY).encode()
|
|
|
|
def urlopen(req, timeout):
|
|
seen.update(auth=req.get_header("Authorization"), beta=req.get_header("Anthropic-beta"))
|
|
return Reply()
|
|
|
|
monkeypatch.setattr(cu.urllib.request, "urlopen", urlopen)
|
|
assert RD.poll_once() == {"polled": 1}
|
|
assert seen == {"auth": "Bearer tok-akira", "beta": "oauth-2025-04-20"}
|
|
body = json.loads((root / "akira" / "usage-state" / "rate-limits.json").read_text())
|
|
assert body["rate_limits"]["seven_day"]["used_percentage"] == 49.0
|
|
assert body["updated_at"] > time.time() - 60
|
|
assert RD.snapshots()["guest-akira"]["rate_limits"] == body["rate_limits"]
|
|
assert RD.last("guest-akira")["ok"]
|
|
|
|
|
|
def test_an_expiring_token_is_left_alone(tmp_path, monkeypatch):
|
|
_guests(tmp_path, monkeypatch, expires_in=10, akira={"x": 1})
|
|
|
|
def urlopen(*a, **k):
|
|
raise AssertionError("no call with a token about to expire")
|
|
|
|
monkeypatch.setattr(cu.urllib.request, "urlopen", urlopen)
|
|
assert RD.poll_once() == {"polled": 0}
|
|
assert RD.last("guest-akira") == {"at": RD.last("guest-akira")["at"], "ok": False,
|
|
"error": "access token expired"}
|
|
assert RD.snapshots() == {}
|
|
|
|
|
|
def test_only_guest_accounts(tmp_path, monkeypatch):
|
|
_guests(tmp_path, monkeypatch, akira={"x": 1})
|
|
assert RD.fetch("personal") is None
|
|
assert RD.state_dir("personal") is None
|
|
assert RD.fetch("guest-../x") is None
|
|
|
|
|
|
def test_parse_keeps_the_scoped_weekly_limits():
|
|
reply = {"seven_day": {"utilization": 5.0, "resets_at": None}, "limits": [
|
|
{"kind": "weekly_all", "percent": 5, "resets_at": None},
|
|
{"kind": "weekly_scoped", "percent": 12, "resets_at": "2026-11-29T18:00:00+00:00",
|
|
"scope": {"model": {"id": None, "display_name": "Fable"}, "surface": None}},
|
|
{"kind": "weekly_scoped", "percent": 3, "scope": {"model": None}},
|
|
]}
|
|
out = cu.parse(reply)["rate_limits"]
|
|
assert set(out) == {"seven_day", "seven_day_fable"}
|
|
assert out["seven_day_fable"]["used_percentage"] == 12.0
|
|
assert out["seven_day_fable"]["resets_at"] == 1795975200.0
|
|
|
|
|
|
def test_owner_logins_go_beside_the_status_line_file(tmp_path, monkeypatch):
|
|
_guests(tmp_path, monkeypatch)
|
|
home = tmp_path / "work"
|
|
(home).mkdir()
|
|
(home / ".credentials.json").write_text(json.dumps({"claudeAiOauth": {
|
|
"accessToken": "tok", "expiresAt": (time.time() + 3600) * 1000}}))
|
|
_set(monkeypatch, "ACCOUNTS", {"work": home})
|
|
monkeypatch.setattr(config, "config_dir", lambda a: home if a == "work" else None)
|
|
assert RD.accounts() == ["work"]
|
|
RD.write("work", {"rate_limits": {"seven_day_fable": {"used_percentage": 1, "resets_at": None}}})
|
|
assert (home / "usage-state" / cu.OWNER_FILE).is_file()
|
|
assert not (home / "usage-state" / cu.FILE).exists()
|
|
assert RD.snapshots()["work"]["rate_limits"]["seven_day_fable"]["used_percentage"] == 1
|