feat(orchestrator): time range and replay move into the rates panel (015)

The navbar time menu goes; the rates panel holds the presets, a two-handle span slider over the hub's history, replay and Live, and the custom From/To range. A custom range becomes a window length plus an end point, so live windows keep their length. The rates chip lists every metric, and the panel wraps instead of spilling at 390 px. Unit and e2e (Chromium, Firefox, 1920/1366/390) cover the window, the slider and the requests.

🎯 Fix: 94 %
💥 Break: 4 %
💸 Tokens: 100 %
⏱️ Velocity: 100 %
🛡️ Security: 100 %
🏆 Score: 90
🚦 Gate: ✅ 90/5, tokens and velocity <= 100, security 100 %

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Kevin Veen-BirkenbachandClaude Opus 5.5 committed 2026-11-17 14:08:13 +00:00
1 parent 09e3f6dabe
commit cc6ea7c230
20 files changed
+462 -416

No files matched your search

+88 -107
View File
@@ -18,7 +18,8 @@ HOUR = 3600
DAY = 86400
GIB = 1 << 30
NOW = 1_800_000_000 + 1800
EXACT_ZERO = {"value": 0.0, "approx": False, "source": None}
LAST_HOUR = NOW - NOW % HOUR
NOTHING = {"value": None, "approx": False, "covered": None}
def token(t: int, given: int, cached: int) -> dict:
@@ -31,6 +32,10 @@ def sample(t: float, cpu: float, span: int | None = None, vram: tuple[int, int]
return row if span is None else {**row, "span": span}
def per_unit(amount: float, covered: float, approx: bool) -> dict[str, dict]:
return {name: {"value": rates.precise(amount if seconds is None else amount * seconds / covered), "approx": approx and seconds is not None, "covered": covered} for name, seconds in rates.PERIODS}
class TestRates(unittest.TestCase):
def setUp(self):
tmp = tempfile.TemporaryDirectory()
@@ -46,86 +51,88 @@ class TestRates(unittest.TestCase):
def lines(self, path: Path, rows: list[dict]) -> None:
path.write_text("".join(json.dumps(row) + "\n" for row in rows), encoding="utf-8")
def rated(self) -> dict:
return rates.rated(self.council, self.folder.parent, NOW)
def rated(self, begin: float, end: float) -> dict:
return rates.rated(self.council, self.folder.parent, NOW, (begin, end))
def column(self, metric: str) -> dict:
found = self.rated()
def column(self, metric: str, begin: float, end: float) -> dict:
found = self.rated(begin, end)
return {period: found["rates"][period][metric] for period in rates.PERIOD_NAMES}
def test_done_tasks_count_in_every_window_their_end_lies_in_and_total_counts_each_done_task(self):
ends = [NOW - 30, NOW - 1800, NOW - 2 * DAY, NOW - 20 * DAY, NOW - 200 * DAY, NOW - 800 * DAY]
def test_a_week_of_seven_million_tokens_gives_a_million_a_day_seven_a_week_and_seven_in_all(self):
self.lines(self.folder / "tokens.jsonl", [token(LAST_HOUR - 10 * DAY, 5, 0), token(LAST_HOUR - 2 * DAY, 6_000_000, 1_000_000)])
tokens = self.column("tokens", NOW - 7 * DAY, NOW)
self.assertEqual(tokens, per_unit(7_000_000, 7 * DAY, False))
self.assertEqual((tokens["day"]["value"], tokens["week"]["value"], tokens["total"]["value"], tokens["month"]["value"]), (1_000_000.0, 7_000_000.0, 7_000_000.0, 30_000_000.0))
def test_done_tasks_key_results_and_commits_count_inside_the_window_and_scale_per_unit(self):
ends = [NOW - 30, NOW - 1800, NOW - 2 * DAY, NOW - 20 * DAY]
tasks = {f"work/t{index}": {"status": "done", "end": iso(end)} for index, end in enumerate(ends)}
others = {"status": "done", "end": iso(NOW - 10)}
self.register({**tasks, "work/legacy": {"status": "done"}, "work/open": {**others, "status": "running"}, "work/halted": {**others, "status": "stopped"}, "operator/run": others})
counts = dict(zip(rates.PERIOD_NAMES, (1.0, 2.0, 2.0, 3.0, 4.0, 5.0, 7.0), strict=True))
self.assertEqual(self.column("tasks"), {period: {"value": value, "approx": False, "source": None} for period, value in counts.items()})
def test_an_empty_window_takes_the_shortest_longer_window_with_events_scaled_down_and_marked_approx(self):
results = [{"text": "met", "met": True, "met_at": iso(NOW - 1800)}, {"text": "open", "met": False}]
self.register({"work/a": {"status": "running", "key_results": results}})
results = [{"text": "met", "met": True, "met_at": iso(NOW - 600)}, {"text": "old", "met": True}, {"text": "open", "met": False}]
self.register({**tasks, "work/legacy": {"status": "done"}, "work/open": {**others, "status": "running", "key_results": results}, "work/halted": {**others, "status": "stopped"}, "operator/run": others})
commit = {"time": NOW - 5 * HOUR, "remote": "example.org/repo", "sha": "a" * 40, "values": {}}
self.lines(self.council / "scores.jsonl", [commit, commit, {**commit, "sha": None, "time": NOW - 60}, {**commit, "sha": "b" * 40, "values": None}])
krs = self.column("krs")
self.assertEqual(krs["minute"], {"value": 0.01667, "approx": True, "source": "hour"})
self.assertEqual([krs[period] for period in rates.PERIOD_NAMES[1:]], [{"value": 1.0, "approx": False, "source": None}] * 6)
commits = self.column("commits")
self.assertEqual(commits["minute"], {"value": 0.0006944, "approx": True, "source": "day"})
self.assertEqual(commits["hour"], {"value": 0.04167, "approx": True, "source": "day"})
self.assertEqual([commits[period] for period in rates.PERIOD_NAMES[2:]], [{"value": 1.0, "approx": False, "source": None}] * 5)
self.lines(self.council / "scores.jsonl", [commit, commit, {**commit, "sha": None, "time": NOW - 60}, {**commit, "sha": "b" * 40, "time": NOW - 3 * DAY}])
day = self.rated(NOW - DAY, NOW)["rates"]
self.assertEqual({period: day[period]["tasks"] for period in rates.PERIOD_NAMES}, per_unit(2, DAY, False))
self.assertEqual({period: day[period]["krs"] for period in rates.PERIOD_NAMES}, per_unit(1, DAY, False))
self.assertEqual({period: day[period]["commits"] for period in rates.PERIOD_NAMES}, per_unit(1, DAY, False))
past = self.rated(NOW - 3 * DAY, NOW - DAY)["rates"]
self.assertEqual((past["total"]["tasks"]["value"], past["total"]["commits"]["value"], past["day"]["tasks"]["value"]), (1.0, 1.0, 0.5))
def test_key_results_met_before_met_at_existed_count_only_in_total_and_scale_from_there(self):
self.register({"work/a": {"status": "running", "key_results": [{"text": "a", "met": True}]}})
self.lines(self.folder / "samples.jsonl", [sample(NOW - 10 * DAY, 5.0)])
krs = self.column("krs")
self.assertEqual(krs["total"], {"value": 1.0, "approx": False, "source": None})
self.assertEqual(krs["year"], {"value": 1.0, "approx": True, "source": "total"})
self.assertEqual(krs["week"], {"value": 0.7, "approx": True, "source": "total"})
self.assertEqual(krs["minute"], {"value": 6.944e-05, "approx": True, "source": "total"})
def test_a_window_reaching_before_the_first_token_row_divides_by_the_covered_part_and_marks_the_rates_approx(self):
self.register({"work/old": {"status": "done", "end": iso(NOW - 30 * DAY)}})
self.lines(self.folder / "tokens.jsonl", [token(LAST_HOUR - 2 * DAY, 4000, 800)])
tokens = self.column("tokens", NOW - 7 * DAY, NOW)
covered = NOW - (LAST_HOUR - 2 * DAY)
self.assertEqual(tokens, per_unit(4800, covered, True))
self.assertEqual(tokens["total"], {"value": 4800.0, "approx": False, "covered": covered})
self.assertEqual(self.column("tasks", NOW - 7 * DAY, NOW)["day"], {"value": 0.0, "approx": False, "covered": 7 * DAY})
def test_tokens_share_their_hour_by_overlap_and_the_minute_always_takes_a_longer_window(self):
hour = NOW - NOW % HOUR
self.lines(self.folder / "tokens.jsonl", [token(hour, 1000, 800), token(hour - HOUR, 3000, 600), token(hour - 20 * DAY, 7, 0)])
tokens = self.column("tokens")
self.assertEqual(tokens["minute"], {"value": 60.0, "approx": True, "source": "hour"})
self.assertEqual(tokens["hour"], {"value": 3600.0, "approx": False, "source": None})
self.assertEqual((tokens["day"]["value"], tokens["month"]["value"], tokens["total"]["value"]), (5400.0, 5407.0, 5407.0))
def test_a_window_before_every_record_holds_no_value(self):
self.register({"work/a": {"status": "done", "end": iso(NOW - DAY)}})
self.lines(self.folder / "tokens.jsonl", [token(LAST_HOUR - DAY, 100, 0)])
self.lines(self.folder / "samples.jsonl", [sample(NOW - DAY, 40.0)])
found = self.rated(NOW - 9 * DAY, NOW - 8 * DAY)
self.assertEqual({found["rates"][period][kind] == NOTHING for period in rates.PERIOD_NAMES for kind in rates.COUNTS}, {True})
self.assertIsNone(found["rates"]["day"]["cpu"]["value"])
self.assertEqual((found["from"], found["to"], found["since"]), (NOW - 9 * DAY, NOW - 8 * DAY, LAST_HOUR - DAY))
def test_an_empty_hour_takes_the_day_for_tokens_and_so_does_the_minute(self):
hour = NOW - NOW % HOUR
self.lines(self.folder / "tokens.jsonl", [token(hour - 5 * HOUR, 8000, 640)])
tokens = self.column("tokens")
self.assertEqual(tokens["minute"], {"value": 6.0, "approx": True, "source": "day"})
self.assertEqual(tokens["hour"], {"value": 360.0, "approx": True, "source": "day"})
self.assertEqual(tokens["day"], {"value": 8640.0, "approx": False, "source": None})
def test_a_window_shorter_than_an_hour_shares_the_token_hour_and_marks_every_token_rate_approx(self):
self.lines(self.folder / "tokens.jsonl", [token(LAST_HOUR, 1000, 800)])
tokens = self.column("tokens", NOW - 600, NOW)
self.assertEqual(tokens["minute"], {"value": 60.0, "approx": True, "covered": 600})
self.assertEqual(tokens["total"], {"value": 600.0, "approx": True, "covered": 600})
def test_gauges_average_each_window_weighting_raw_rows_fifteen_minute_means_and_daily_means_by_their_span(self):
def test_gauges_average_the_window_weighting_each_row_by_its_sampled_seconds_inside_on_both_ends(self):
start = NOW - 10 * DAY - (NOW - 10 * DAY) % DAY
self.lines(self.folder / "samples.jsonl", [
sample(NOW - 500 * DAY, 0.0, span=DAY),
sample(NOW - 200 * DAY, 90.0, span=DAY),
sample(NOW - 10 * DAY, 50.0, span=900),
sample(NOW - 90, 30.0, vram=(4, 8), boot=70),
sample(NOW - 30, 10.0, vram=(2, 8), boot=90),
{**sample(start, 40.0), "span": 3600, "width": DAY},
{**sample(NOW - 2 * DAY - 300, 70.0, boot=80), "span": 600, "width": 900},
sample(NOW - 30, 10.0, boot=20),
])
found = self.rated()["rates"]
self.assertEqual([found[period]["cpu"]["value"] for period in rates.PERIOD_NAMES], [10.0, 20.0, 20.0, 20.0, 46.5, 89.5, 45.0])
self.assertEqual([found[period]["ram"]["value"] for period in ("minute", "hour")], [11.0, 21.0])
self.assertEqual(found["minute"]["vram"], {"value": 25.0, "used": 2 * GIB, "approx": False, "source": None})
self.assertEqual((found["hour"]["vram"]["value"], found["hour"]["vram"]["used"], found["month"]["vram"]["value"]), (37.5, 3 * GIB, 37.5))
self.assertEqual(found["minute"]["disk"], {"value": 90.0, "mount": "/boot", "used": 90, "total": 100, "approx": False, "source": None})
self.assertEqual((found["hour"]["disk"]["value"], found["month"]["disk"]["value"], found["month"]["disk"]["used"]), (80.0, 53.5, 54))
self.assertEqual(self.rated()["since"], NOW - 500 * DAY)
found = self.rated(start + DAY / 2, NOW - 2 * DAY)["rates"]
self.assertEqual({found[period]["cpu"]["value"] for period in rates.PERIOD_NAMES}, {round((1800 * 40 + 200 * 70) / 2000, 1)})
self.assertEqual(found["hour"]["disk"], {"value": round((1800 * 50 + 200 * 80) / 2000, 1), "mount": "/boot", "used": 53, "total": 100, "approx": False})
self.assertEqual(found["week"]["vram"], {"value": None, "used": None, "approx": False})
def test_a_window_without_a_sample_shows_no_gauge_and_kinds_without_any_event_count_an_exact_zero(self):
def test_gauges_of_the_last_minutes_read_vram_and_the_fullest_disk(self):
self.lines(self.folder / "samples.jsonl", [sample(NOW - 90, 30.0, vram=(4, 8), boot=70), sample(NOW - 30, 10.0, vram=(2, 8), boot=90)])
last = self.rated(NOW - 60, NOW)["rates"]["minute"]
self.assertEqual((last["cpu"]["value"], last["ram"]["value"]), (10.0, 11.0))
self.assertEqual(last["vram"], {"value": 25.0, "used": 2 * GIB, "approx": False})
self.assertEqual(last["disk"], {"value": 90.0, "mount": "/boot", "used": 90, "total": 100, "approx": False})
both = self.rated(NOW - 120, NOW)["rates"]["hour"]
self.assertEqual((both["cpu"]["value"], both["vram"]["value"], both["vram"]["used"], both["disk"]["value"]), (20.0, 37.5, 3 * GIB, 80.0))
def test_a_window_without_a_sample_shows_no_gauge_and_kinds_with_records_but_none_inside_count_an_exact_zero(self):
self.lines(self.folder / "samples.jsonl", [sample(NOW - 2 * HOUR, 40.0)])
found = self.rated()
self.assertEqual([found["rates"][period]["cpu"]["value"] for period in rates.PERIOD_NAMES], [None, None, 40.0, 40.0, 40.0, 40.0, 40.0])
self.assertEqual(found["rates"]["hour"]["vram"], {"value": None, "used": None, "approx": False, "source": None})
self.assertEqual(found["rates"]["minute"]["disk"], {"value": None, "mount": None, "used": None, "total": None, "approx": False, "source": None})
self.assertEqual({found["rates"][period][kind] == EXACT_ZERO for period in rates.PERIOD_NAMES for kind in rates.COUNTS}, {True})
self.assertEqual((found["now"], found["since"], [entry["name"] for entry in found["periods"]]), (NOW, NOW - 2 * HOUR, list(rates.PERIOD_NAMES)))
found = self.rated(NOW - HOUR, NOW)["rates"]
self.assertIsNone(found["day"]["cpu"]["value"])
self.assertEqual(found["minute"]["disk"], {"value": None, "mount": None, "used": None, "total": None, "approx": False})
self.assertEqual({found[period][kind]["value"] for period in rates.PERIOD_NAMES for kind in ("tasks", "commits", "krs")}, {0.0})
self.assertEqual(found["day"]["tokens"], NOTHING)
def test_folded_sampler_rows_feed_the_windows_with_the_seconds_they_sampled(self):
def test_folded_sampler_rows_feed_the_window_with_the_seconds_they_sampled(self):
old = NOW - NOW % DAY - 100 * DAY
raw = [sample(old + 3600 + index * 60, 20.0) for index in range(30)] + [sample(NOW - 9 * DAY + index * 60, 60.0) for index in range(30)] + [sample(NOW - 59, 10.0)]
kept = sampler.retain(sorted(raw, key=lambda row: row["t"]), NOW)
@@ -134,10 +141,10 @@ class TestRates(unittest.TestCase):
self.assertEqual({row["width"] for row in kept[1:-1]}, {900})
self.assertNotIn("span", kept[-1])
self.lines(self.folder / "samples.jsonl", kept)
found = self.rated()["rates"]
month = round((10 * 60 + 60 * 1800) / (60 + 1800), 1)
year = round((10 * 60 + 60 * 1800 + 20 * 1800) / (60 + 1800 * 2), 1)
self.assertEqual([found[period]["cpu"]["value"] for period in ("minute", "week", "month", "year")], [10.0, 10.0, month, year])
cpu = [self.rated(NOW - seconds, NOW)["rates"]["day"]["cpu"]["value"] for seconds in (60, 7 * DAY, 30 * DAY, 365 * DAY)]
self.assertEqual(cpu, [10.0, 10.0, month, year])
def test_a_lone_fifteen_minute_bucket_weighs_its_sampled_seconds_wherever_it_lies_in_its_day(self):
day = NOW - NOW % DAY - 200 * DAY
@@ -156,22 +163,7 @@ class TestRates(unittest.TestCase):
old, recent = NOW - NOW % DAY - 100 * DAY, NOW - NOW % DAY - 8 * DAY
raw = [sample(old + 60 * index, 90.0) for index in range(60)] + [sample(recent + 60 * index, 10.0) for index in range(60)]
self.lines(self.folder / "samples.jsonl", sampler.retain(raw, NOW))
self.assertEqual(self.rated()["rates"]["year"]["cpu"]["value"], 50.0)
def test_a_folded_row_straddling_a_window_start_counts_by_its_share_inside(self):
week, year = NOW - 7 * DAY, NOW - 365 * DAY
self.lines(self.folder / "samples.jsonl", [
{**sample(year - year % DAY, 40.0), "span": 3600, "width": DAY},
{**sample(week - 300, 70.0, boot=80), "span": 600, "width": 900},
sample(NOW - 30, 10.0, boot=20),
])
found = self.rated()["rates"]
inside = (year - year % DAY + DAY - year) / DAY * 3600
self.assertEqual(found["week"]["cpu"]["value"], round((400 * 70 + 60 * 10) / 460, 1))
self.assertEqual(found["month"]["cpu"]["value"], round((600 * 70 + 60 * 10) / 660, 1))
self.assertEqual(found["year"]["cpu"]["value"], round((inside * 40 + 600 * 70 + 60 * 10) / (inside + 660), 1))
self.assertEqual(found["total"]["cpu"]["value"], round((3600 * 40 + 600 * 70 + 60 * 10) / 4260, 1))
self.assertEqual(found["week"]["disk"]["value"], round((400 * 80 + 60 * 20) / 460, 1))
self.assertEqual(self.rated(NOW - 365 * DAY, NOW)["rates"]["year"]["cpu"]["value"], 50.0)
def test_token_rows_older_than_ninety_days_fold_into_one_row_per_day_agent_and_model_kept_forever(self):
day, hour = NOW - NOW % DAY - 100 * DAY, NOW - NOW % HOUR
@@ -181,25 +173,11 @@ class TestRates(unittest.TestCase):
self.assertEqual(kept, [*daily, token(hour - HOUR, 7, 0)])
self.assertEqual(sampler.compact(kept, NOW + 400 * DAY)[:2], daily)
def test_a_daily_token_row_counts_by_the_share_of_its_day_inside_a_window(self):
def test_a_daily_token_row_counts_by_the_share_of_its_day_inside_the_window(self):
year = NOW - 365 * DAY
self.lines(self.folder / "tokens.jsonl", [{**token(year - year % DAY, 8640, 0), "width": DAY}])
tokens = self.column("tokens")
self.assertEqual(tokens["year"], {"value": rates.precise((year - year % DAY + DAY - year) / DAY * 8640), "approx": False, "source": None})
self.assertEqual(tokens["total"], {"value": 8640.0, "approx": False, "source": None})
def test_token_windows_reaching_before_the_first_token_row_are_scaled_estimates_from_the_rows_there_are(self):
hour = NOW - NOW % HOUR
begins = hour - 10 * DAY
self.register({"work/old": {"status": "done", "end": iso(NOW - 400 * DAY)}})
self.lines(self.folder / "tokens.jsonl", [token(begins, 1000, 0), token(hour - HOUR, 500, 0)])
tokens = self.column("tokens")
covered = NOW - begins
self.assertEqual(tokens["minute"], {"value": rates.precise(250 / 60), "approx": True, "source": "hour"})
self.assertEqual([tokens[period] for period in ("hour", "day", "week")], [{"value": value, "approx": False, "source": None} for value in (250.0, 500.0, 500.0)])
for period, seconds in (("month", 30 * DAY), ("year", 365 * DAY), ("total", 400 * DAY)):
with self.subTest(period=period):
self.assertEqual(tokens[period], {"value": rates.precise(1500 * seconds / covered), "approx": True, "source": "total"})
total = self.column("tokens", year, NOW)["total"]
self.assertEqual(total, {"value": rates.precise((year - year % DAY + DAY - year) / DAY * 8640), "approx": False, "covered": NOW - year})
def test_counts_shorten_to_thousands_millions_billions_and_trillions(self):
probe = "const R = require(process.argv[1]); console.log(JSON.stringify(JSON.parse(process.argv[2]).map(R.compact)));"
@@ -209,16 +187,19 @@ class TestRates(unittest.TestCase):
class TestHubRates(HubCase):
def test_the_hub_serves_the_rates_of_the_register_the_journal_and_the_sampler(self):
self.write_council({"rasci.json": {"work/a": {"status": "done", "end": iso(1_700_000_000), "key_results": [{"text": "a", "met": True, "met_at": iso(1_700_000_000)}]}}})
(self.council / "scores.jsonl").write_text(json.dumps({"time": 1_700_000_000, "remote": "example.org/repo", "sha": "c" * 40, "values": {}}) + "\n", encoding="utf-8")
status, headers, body = self.get("/api/rates")
def test_the_hub_serves_the_rates_of_the_window_from_the_register_the_journal_and_the_sampler(self):
done = 1_700_000_000
self.write_council({"rasci.json": {"work/a": {"status": "done", "end": iso(done), "key_results": [{"text": "a", "met": True, "met_at": iso(done)}]}}})
(self.council / "scores.jsonl").write_text(json.dumps({"time": done, "remote": "example.org/repo", "sha": "c" * 40, "values": {}}) + "\n", encoding="utf-8")
status, headers, body = self.get(f"/api/rates?from={done - DAY}&to={done + DAY}")
self.assertEqual((status, headers["content-type"]), (200, "application/json"), body)
found = json.loads(body)
self.assertEqual([entry["name"] for entry in found["periods"]], list(rates.PERIOD_NAMES))
self.assertEqual({kind: found["rates"]["total"][kind]["value"] for kind in rates.COUNTS}, {"tasks": 1.0, "commits": 1.0, "krs": 1.0, "tokens": 0.0})
self.assertEqual({kind: found["rates"]["total"][kind]["value"] for kind in rates.COUNTS}, {"tasks": 1.0, "commits": 1.0, "krs": 1.0, "tokens": None})
self.assertEqual({kind: found["rates"]["day"][kind]["approx"] for kind in ("tasks", "commits", "krs")}, {"tasks": True, "commits": True, "krs": True})
self.assertEqual(set(found["rates"]["hour"]), set(rates.METRICS))
self.assertEqual(found["since"], 1_700_000_000)
self.assertEqual((found["since"], found["from"], found["to"]), (done, done - DAY, done + DAY))
self.assertEqual(self.get(f"/api/rates?from={done}&to={done - 1}")[0], 400)
if __name__ == "__main__":