Repository navigation
feat: Show a project's spend by model in agentop - #2040
Conversation
Assisted-By: Claude (Anthropic AI) <noreply@anthropic.com> Signed-off-by: Hai Huang <huang195@gmail.com>
Assisted-By: Claude (Anthropic AI) <noreply@anthropic.com> Signed-off-by: Hai Huang <huang195@gmail.com>
agentop now sends project= and uses the reply only when the reply echoes project. An older proxy, and the Other and (other) scopes, use the old group=project read. The drawer axes under a project scope are model and endpoint, and the spend band shows each unit. Assisted-By: Claude (Anthropic AI) <noreply@anthropic.com> Signed-off-by: Hai Huang <huang195@gmail.com>
fetchUsageScopedProject now sends project= (with agent= when an agent is scoped) through the new apiclient GetUsageWindowScoped. It uses the reply only when the reply echoes the project (and the agent). A proxy older than project= does not echo it, so agentop falls back to the old group=project read and narrows it. agentop never sends project=Other or project=(other), and an agent Other scope also uses the old read. The tests that pinned the old "no project= key" rule now act as an older proxy: they fail only on project=Other or project=(other), and count only the fallback reads. Assisted-By: Claude (Anthropic AI) <noreply@anthropic.com> Signed-off-by: Hai Huang <huang195@gmail.com>
Under a project scope the drawer now has the axes of an agent scope (model and endpoint), and `a` cycles them. The project Other has no axes. The drawer says "no breakdown for this scope" only when the reply has no breakdown. The band asks for the currency split under a project scope too, so a mixed window shows each unit. Assisted-By: Claude (Anthropic AI) <noreply@anthropic.com> Signed-off-by: Hai Huang <huang195@gmail.com>
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 3 minutes. View limit details
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. |
The README said that agentop sends the display name in project=. agentop sends the label that group=project reports. The README now says that. TestFetchUsageScopedProject_NeverSendsOther tested only the projects Other and (other). It now also tests the agent Other with a named project. Without the guard, a server that echoes agent=Other and project=X gives a zero, and agentop shows that zero as the project's spend. Assisted-By: Claude (Anthropic AI) <noreply@anthropic.com> Signed-off-by: Hai Huang <huang195@gmail.com>
clawgenti
left a comment
There was a problem hiding this comment.
Summary
This PR makes agentop send project=<label> (plus agent= when one is scoped) and use the reply only when the proxy echoes it. When there is no echo, it falls back to the old group=project narrowing. That gives the drawer model/endpoint axes under a project scope and lets the band show each unit. I found no must-fix issues.
I checked the client against the server at this head:
core/sessionapi/usage.gomatchesproject=on thegroup=projectlabel.- In
core/cost/usage/scope.go,ProjectSnapshotechoesprojecton the complete-ring path and on the narrowed path, and the narrowed path is served as group none. That fits the newdrawerScopeNoterule. An unrecognised agent with no complete ring getsAgentSnapshotwith no echo, and the agentop fallback handles that case. - The ledger (
core/cost/ledger/snapshot.go) filters byFilterProjectand echoes both keys. agent=is folded bypipeline.AgentNamebefore the echo. agentop's scope labels come fromgroup=agent, which is versionless, so thesnap.Agent == agentcheck holds.Snapshot.Projectis the existingLenientStringfield, so the decoder gets no new typed field.- The fold labels (
Other,(other), agentOther) never go on the wire, andTestFetchUsageScopedProject_NeverSendsOthernow covers the agentOthercase too.
On an older proxy the currency split falls back to the narrowed group-none read. unitCosts returns nil for that, so the cell still says mixed rather than showing figures for the whole agent.
I ran the PR's gate locally on 5a8305f: go vet, gofmt -l and go test -race ./tui/ ./apiclient/. All pass.
The open items the PR body lists stay minor and do not block: the extra request per read on a proxy that does not echo, and no test for a reply that echoes project but not the agent.
Author: huang195 (MEMBER — maintainer)
Areas reviewed: Go (agentop apiclient/tui), Docs (README, plan), cross-checked against core/sessionapi, core/cost/usage, core/cost/ledger
Agent/IDE config (.claude/.vscode): none
Commits: 6 commits, all signed-off: yes
CI status: passing (annotations checked: no hidden or non-blocking test failures)
What changed
Part 2 of 3 of #2032. Part 1 (#2033, PR #2038) is on
main, so this PR has no parent PR.fetchUsageScopedProjectsendsproject=<label>on/v1/usage, withagent=when an agent is scoped. The label is thegroup=projectseries label. agentop uses the reply only when the reply echoesproject(and the scoped agent). Otherwise it uses the old read:group=project, narrowed by agentop. An older proxy ignoresproject=and gets the old read.project=Otherorproject=(other). It also does not sendproject=for the agent Other. Those scopes use the old read.modelandendpoint, andacycles them. The project Other has no axes. The drawer saysno breakdown for this scope in <window>only when the reply has no breakdown.project=, so the band shows each unit (for example$12.00 + 0.95 Bobcoins).cmd/agentop/README.mdtells these facts.project=key). This PR changes them. The plan (decisions 3 and 4) gives the reason.How it was tested
The gate passed on this branch's tip:
New tests are in
tui/agent_scope_project_test.go,tui/spend_drawer_project_test.goandapiclient/client_test.go.TestFetchUsageScopedProject_NoEchoFallsBackcovers an older proxy.Size
docs/superpowers/plans/2026-11-20-project-drawer-2034.md).Review findings that are not fixed
All are minor.
agent_scope.go, the guard infetchUsageScopedProject).TestFetchUsageScopedProject_NeverSendsOthertests only the projects Other and(other), with agent"". Add one case withagent=otherAgentsand a named project. Assert that no query hasproject=. (Two reviews reported this.)projectbut not the scoped agent. Examples: a ring window with an agent that is not recognised, or a versioned agent label that the server folds throughpipeline.AgentName. Thesnap.Agent == agenthalf of the echo check is new logic. One case with theprojectServerfixture that echoesprojectand leavesAgentempty covers it. (Two reviews reported this.)tui/usage_pane_scope_test.go(projectUsageFetch) andtui/project_scope_spend_test.go. These tests pinned the old rule. T6 changesproject_scope_spend_test.goagain. Check that T6 does not make a second edit to the same counting filter that conflicts with the first.project=request fails.fetchUsageScopedProjectreturns the error. A label longer thanmaxProjectLabelLengets a 400 from a new server, but an old server ignores the key. Labels come fromgroup=projectreplies, so this is unlikely. The plan does not ask for a fallback on error.spendAxis(agent_scope.go:137):|| m.spendScopeProject() != ""withspendScopeAgent() == ""andgroupIdxon the agent axis. Each new test also scopes an agent. This branch may be unreachable today, becausekeys.go:599-604always setsagentScopewith a project sub-row. Add a test that setsprojectScopewithagentScope ""and checks thatspendAxis()isGroupModel. If that state cannot occur, write that in the comment.TestProjectScope_DrawerAxisKeyCyclesModelAndEndpointdoes not check the axis before the first press. Add an assert thatm.spendAxis()isGroupModelbefore the loop.TestProjectScope_BandMakesNoCurrencyReadForAMixedWindownow expects 2 fallback reads, not 1. Decision 10 makes this necessary. The main check (no whole-agentgroup=currencyread) stays.cmd/agentop/README.md:991-992). agentop sendsm.projectScope, thegroup=projectseries label (for example~/works/cortex). The title showsprojectScopeName, a different name. The server matches only the label (core/sessionapi/usage.go). Change the text to "sendsproject=<label>, the label thatgroup=projectreports". (Two reviews reported this.)project(agent_scope.go:78-87). Each read is theproject=read, then thegroup=projectfallback. This occurs on each band poll, currency split, drawer read and usage pane read, for all of the session. It also occurs always for the project Other in the mixed-unit band. Before, it was one request. To make the cost less, record on the client that the proxy did not echoproject=after the first miss, and skip theproject=read after that. Or accept the cost and say so in the comment or the README.For a human after review
Nothing. This part has no step outside the repository. Opening the PR is the workflow's own step.
Fixes #2034
Assisted-By: Claude (Anthropic AI) noreply@anthropic.com