From 10f56eaa35a5a53346fd2f2ae7abcc29116c23a5 Mon Sep 17 00:00:00 2001 From: "infiloop-dev-agent[bot]" <71980293+infiloop2@users.noreply.github.com> Date: Wed, 11 Nov 2026 17:59:39 +0000 Subject: [PATCH] Sync Kern --- VERSION | 2 +- .../architecture/workspaces/workspace-agent-api.md | 4 +++- host/bootstrap/agent-home/agents_claude.md | 4 ++-- host/runtime/workspace/usage.py | 14 +++++++++++--- tests/test_token_usage.py | 5 +++++ 5 files changed, 22 insertions(+), 7 deletions(-) diff --git a/VERSION b/VERSION index f5e414f..7d202de 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.19.222 +1.19.223 diff --git a/docs/architecture/workspaces/workspace-agent-api.md b/docs/architecture/workspaces/workspace-agent-api.md index dd6f1e7..61379fb 100644 --- a/docs/architecture/workspaces/workspace-agent-api.md +++ b/docs/architecture/workspaces/workspace-agent-api.md @@ -377,7 +377,9 @@ no turn in the window reported it; `daily_tokens` counts only known values. With `thread_id` it returns `since`, `until`, `days`, and that thread's Analytics `groups` unchanged, ordered by day and hour. A thread without usage -returns an empty list. `limit` is rejected with `thread_id`. +returns an empty list. `day=YYYY-MM-DD` narrows the rows to one of the listed +`days`, which keeps a busy thread's response small; another date is rejected, +as is `day` without `thread_id`. `limit` is rejected with `thread_id`. ## One-time reminders diff --git a/host/bootstrap/agent-home/agents_claude.md b/host/bootstrap/agent-home/agents_claude.md index 7321b2d..b71d3be 100644 --- a/host/bootstrap/agent-home/agents_claude.md +++ b/host/bootstrap/agent-home/agents_claude.md @@ -150,8 +150,8 @@ is required. Write and test the durable script under agent home first. ### Token usage `GET /agent/usage` ranks threads by tokens over seven UTC days (`limit` 1–200, -default 50); `?thread_id=ID` returns that thread's hourly rows. The four -buckets are disjoint; null means unmeasured. +default 50); `?thread_id=ID` returns that thread's hourly rows, or one `day` +with `&day=YYYY-MM-DD`. The four buckets are disjoint; null means unmeasured. ## Links diff --git a/host/runtime/workspace/usage.py b/host/runtime/workspace/usage.py index e4b6f54..f34a7ab 100644 --- a/host/runtime/workspace/usage.py +++ b/host/runtime/workspace/usage.py @@ -2,7 +2,8 @@ The default view ranks threads over the same seven UTC days so an agent can find heavy spenders without loading every hourly row; `thread_id` returns one -thread's hourly rows unchanged. Counts are best-effort measurements. +thread's hourly rows unchanged, optionally for one `day`. Counts are +best-effort measurements. """ from __future__ import annotations @@ -22,19 +23,26 @@ def route_agent(method: str, query: dict[str, list[str]]) -> dict[str, Any]: if method != "GET": raise WorkspaceError(HTTPStatus.BAD_REQUEST, "agent usage accepts only GET") - extra = sorted(set(query) - {"limit", "thread_id"}) + extra = sorted(set(query) - {"limit", "thread_id", "day"}) if extra: raise WorkspaceError(HTTPStatus.BAD_REQUEST, f"unexpected usage query field: {extra[0]}") thread_id = one(query, "thread_id") raw_limit = one(query, "limit") + day = one(query, "day") if thread_id is not None and raw_limit is not None: raise WorkspaceError(HTTPStatus.BAD_REQUEST, "limit applies only without thread_id") + if thread_id is None and day is not None: + raise WorkspaceError(HTTPStatus.BAD_REQUEST, "day applies only with thread_id") if raw_limit is not None and (not raw_limit.isdigit() or not 1 <= int(raw_limit) <= MAX_LIMIT): raise WorkspaceError(HTTPStatus.BAD_REQUEST, f"limit must be between 1 and {MAX_LIMIT}") report = state.usage_report() window = {key: report[key] for key in ("since", "until", "days")} if thread_id is not None: - groups = [group for group in report["groups"] if group["thread_id"] == thread_id] + # A busy thread's week is hundreds of hourly rows; one day is at most 24 per model. + if day is not None and day not in report["days"]: + raise WorkspaceError(HTTPStatus.BAD_REQUEST, "day must be one of the report's days") + groups = [group for group in report["groups"] + if group["thread_id"] == thread_id and day in (None, group["day"])] groups.sort(key=lambda group: (group["day"], group["hour"], group["runtime"], group["model"])) return {**window, "groups": groups} threads = _threads(report) diff --git a/tests/test_token_usage.py b/tests/test_token_usage.py index 807c721..04a2793 100644 --- a/tests/test_token_usage.py +++ b/tests/test_token_usage.py @@ -73,10 +73,15 @@ def get(path): self.assertEqual([(row["day"], row["model"]) for row in detail["groups"]], [("2026-09-06", "astra"), ("2026-09-12", "sol")]) self.assertNotIn("threads", detail) + one_day = get("/agent/usage?thread_id=schedule-1&day=2026-09-12") + self.assertEqual([(row["day"], row["model"]) for row in one_day["groups"]], [("2026-09-12", "sol")]) + self.assertEqual(one_day["days"], report["days"]) + self.assertEqual(get("/agent/usage?thread_id=schedule-1&day=2026-09-08")["groups"], []) self.assertEqual(get("/agent/usage?thread_id=missing")["groups"], []) for method, path in [ ("GET", "/agent/usage?limit=0"), ("GET", "/agent/usage?limit=201"), ("GET", "/agent/usage?since=2020"), ("GET", "/agent/usage?thread_id=schedule-1&limit=5"), + ("GET", "/agent/usage?day=2026-09-12"), ("GET", "/agent/usage?thread_id=schedule-1&day=2026-09-13"), ("POST", "/agent/usage"), ]: with self.subTest(method=method, path=path), self.assertRaises(WorkspaceError) as error: