Skip to content

fix(ui): scope usage requests by agent filter - #87222

Merged
steipete merged 6 commits into
openclaw:mainfrom
Alix-007:issue-87132-usage-agent-filter
May 29, 2026
Merged

steipete merged 6 commits into
openclaw:mainfrom
Alix-007:issue-87132-usage-agent-filter

Conversation

@Alix-007

@Alix-007 Alix-007 commented May 27, 2026

Copy link
Copy Markdown
Contributor

Fixes #87132.

Summary

  • replace query-token agent scoping with an explicit Usage agent scope selector
  • request all configured agents by default with agentScope: "all", while selected agents still use agentId
  • merge all-agent usage.cost summaries across configured agents and keep scoped cache entries separate
  • keep legacy gateway fallbacks for agentId and agentScope, with generated protocol/docs updates

Verification

  • node scripts/run-vitest.mjs ui/src/ui/controllers/usage.node.test.ts ui/src/ui/app-render-usage-tab.test.ts ui/src/ui/views/usage.test.ts --reporter=dot (18 tests passed)
  • node scripts/run-vitest.mjs run --config test/vitest/vitest.gateway-methods.config.ts src/gateway/server-methods/usage.test.ts src/gateway/server-methods/usage.cost-usage-cache.test.ts src/gateway/server-methods/usage.sessions-usage.test.ts --reporter=dot (34 tests passed)
  • pnpm check:test-types
  • pnpm protocol:check
  • node scripts/run-oxlint.mjs --tsconfig config/tsconfig/oxlint.core.json src/gateway/protocol/schema/sessions.ts src/gateway/server-methods/usage.ts src/gateway/server-methods/usage.test.ts src/gateway/server-methods/usage.cost-usage-cache.test.ts src/gateway/server-methods/usage.sessions-usage.test.ts ui/src/ui/controllers/usage.ts ui/src/ui/controllers/usage.node.test.ts ui/src/ui/app.ts ui/src/ui/app-view-state.ts ui/src/ui/app-render-usage-tab.ts ui/src/ui/app-render-usage-tab.test.ts ui/src/ui/views/usage.ts ui/src/ui/views/usage.test.ts ui/src/ui/views/usageTypes.ts
  • git diff --check
  • .agents/skills/autoreview/scripts/autoreview --mode commit --commit d67156a3c5 --thinking low --no-web-search

Real behavior proof

Behavior addressed: The Usage page needed a real agent scope, not a query-string side effect. Default Usage now asks the gateway for all configured agents, selected agents use agentId, and the gateway can aggregate all-agent cost totals instead of silently falling back to the default agent.

Real environment tested: Local OpenClaw checkout on branch pr-87222, using the Control UI usage tests, gateway usage method tests, generated gateway protocol check, and test typecheck.

Exact steps or command run after this patch: Ran the verification commands listed above after the final fixup commit.

Evidence after fix: UI usage tests passed 18/18, gateway usage tests passed 34/34, pnpm check:test-types passed, pnpm protocol:check passed with generated Swift protocol output staged, targeted oxlint returned success, git diff --check returned clean, and the narrowed autoreview reported no accepted/actionable findings on the Swift compatibility fix.

Observed result after fix: With no selected agent, the UI sends agentScope: "all" to sessions.usage and usage.cost. With a selected agent, both requests send that agentId. The gateway lists sessions across configured agents for explicit all-agent scope, rejects conflicting all-agent plus specific-agent/key requests, aggregates cost totals by date across configured agents, and keeps cost cache entries separated by all/default/specific-agent scope.

What was not tested: I did not run a live browser against a multi-agent gateway. A local pnpm build attempt was stopped after tsdown stayed quiet for about 15 minutes; CI will cover the full build lane. A Testbox pnpm check:changed run exited 255 without actionable logs.

@openclaw-barnacle openclaw-barnacle Bot added app: web-ui App: web-ui size: S proof: supplied External PR includes structured after-fix real behavior proof. labels May 27, 2026
@clawsweeper

clawsweeper Bot commented May 27, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs changes before merge. Reviewed May 28, 2026, 8:57 PM ET / 00:57 UTC.

Summary
The PR adds an explicit Usage agent selector/all-agent scope, threads agentId/agentScope through UI and gateway usage requests, updates cost aggregation/cache behavior, and refreshes protocol docs plus generated Swift models.

PR surface: Source +277, Tests +484, Docs +2, Other +4. Total +767 across 17 files.

Reproducibility: yes. from source: current main's UI never sends agentId, while current main's gateway resolves omitted agentId to the default agent for sessions.usage. I did not run a live multi-agent browser/gateway repro in this read-only review.

Review metrics: 2 noteworthy metrics.

  • Default usage scope: 1 default behavior changed. The empty Usage selector now requests all configured agents instead of the shipped default-agent scope, which is compatibility-sensitive.
  • Gateway protocol surface: 1 request field added, generated Swift model updated. Adding agentScope to sessions.usage changes the operator-client wire contract and requires aligned docs/generated clients before merge.

Merge readiness
Overall: 🦐 gold shrimp
Proof: 🦞 diamond lobster
Patch quality: 🦐 gold shrimp
Result: needs maintainer review before merge.

Overall follows the weaker of proof and patch quality, so missing proof can cap an otherwise strong patch.

Rank-up moves:

  • [P2] Scope selected-agent fallback aggregates/daily chart data and add a focused view regression.
  • Keep maintainer acceptance explicit for the all-agent default and operator.read visibility change.

Mantis proof suggestion
A real Control UI multi-agent smoke would materially prove the visible selector and gateway-scoped Usage behavior beyond unit tests. A maintainer can ask Mantis to capture proof by posting a new PR comment that starts with the OpenClaw Mantis account mention, followed by:

visual task: verify the Usage page all-agent default and selecting a non-main agent scopes sessions, totals, and cost charts without showing other agents.

Risk before merge

  • [P1] The empty Usage selector changes from shipped default-agent behavior to an explicit all-configured-agents request, so existing operators may see broader usage data after upgrade.
  • [P1] The new agentScope: "all" path lets the existing operator.read usage methods return all configured agents' usage, which is probably intentional here but is still a security-boundary decision.
  • [P2] Selected-agent legacy fallback still needs aggregate and daily-chart scoping so older gateways do not show mixed-agent summaries after dropping unsupported agent parameters.

Maintainer options:

  1. Fix selected-agent fallback scoping (recommended)
    Scope aggregate panels and daily chart data to the selected agent when legacy gateway fallback returns broader session/cost data, with a focused view regression test.
  2. Accept the all-agent default explicitly
    Maintain the empty selector as all configured agents only if maintainers are comfortable changing the upgrade default and operator.read visibility boundary.
  3. Pause for a narrower canonical path
    If the all-agent default is not intended, pause this branch and land a smaller default-agent-plus-selector fix instead of changing the default scope.
Copy recommended automerge instruction
@clawsweeper automerge

Special instructions:
Update the Usage view so selected-agent mode uses the agent-scoped session set for aggregate panels and filters or derives daily chart/cost data consistently when legacy fallback returns multi-agent results; add a focused regression in ui/src/ui/views/usage.test.ts covering selected agent with multi-agent sessions/costDaily after fallback. Keep the explicit all-agent gateway protocol behavior unchanged.

Next step before merge

  • [P2] A narrow automated repair can fix the selected-agent fallback aggregate/daily scoping; maintainers still own the all-agent default and security-boundary acceptance.

Security
Needs attention: No supply-chain concern found, but the diff deliberately broadens usage visibility under existing operator.read methods and needs maintainer acceptance.

Review findings

  • [P2] Scope selected-agent fallback aggregates — ui/src/ui/views/usage.ts:275-291
Review details

Best possible solution:

Land one canonical fix that scopes selected-agent fallback data consistently, keeps the protocol/docs/generated clients aligned, and has maintainer acceptance for the all-agent default before closing the linked issue and superseding sibling PRs.

Do we have a high-confidence way to reproduce the issue?

Yes, from source: current main's UI never sends agentId, while current main's gateway resolves omitted agentId to the default agent for sessions.usage. I did not run a live multi-agent browser/gateway repro in this read-only review.

Is this the best way to solve the issue?

No, not quite yet: the main current-gateway path is a maintainable fix, but selected-agent legacy fallback still needs aggregates and daily chart data scoped to the selected agent. The all-agent default also needs maintainer acceptance as an intentional compatibility/security-boundary change.

Full review comments:

  • [P2] Scope selected-agent fallback aggregates — ui/src/ui/views/usage.ts:275-291
    When a legacy gateway rejects the new agent params, loadUsage retries without server-side agent scoping. In that path the view filters visible rows/totals with agentScopedSessions, but aggregate panels still fall through to sortedSessions and the daily chart still uses data.costDaily, so selecting one agent can display provider/model/tool or daily cost data from other agents. Please scope those derived datasets to the selected agent and add a focused view regression for the fallback shape.
    Confidence: 0.83

Overall correctness: patch is incorrect
Overall confidence: 0.84

AGENTS.md: found and applied where relevant.

Codex review notes: model gpt-5.5, reasoning high; reviewed against 8bd4736f03bc.

Label changes

Label justifications:

  • P2: This is a normal-priority regression fix for multi-agent Usage visibility with limited blast radius but real operator impact.
  • merge-risk: 🚨 compatibility: The PR changes default Usage results on upgrade from default-agent only to all configured agents and adds legacy fallback behavior.
  • merge-risk: 🚨 security-boundary: The PR intentionally broadens usage visibility under existing operator.read methods through explicit all-agent scope.
  • rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🦞 diamond lobster and patch quality is 🦐 gold shrimp.
  • status: ⏳ waiting on author: ClawSweeper has contributor-facing work open and is waiting for author action. Override: A maintainer applied proof: override for this PR.
Evidence reviewed

PR surface:

Source +277, Tests +484, Docs +2, Other +4. Total +767 across 17 files.

View PR surface stats
Area Files Added Removed Net
Source 8 311 34 +277
Tests 6 489 5 +484
Docs 1 3 1 +2
Config 0 0 0 0
Generated 0 0 0 0
Other 2 5 1 +4
Total 17 808 41 +767

Security concerns:

  • [medium] Confirm all-agent usage read boundary — src/gateway/server-methods/usage.ts:989
    The new all-agent path makes sessions.usage bypass the default-agent scope when agentScope: "all" is sent, while the method remains under operator.read; this is likely intended for the Usage page but should be accepted as a boundary change before merge.
    Confidence: 0.78

Acceptance criteria:

  • [P1] node scripts/run-vitest.mjs ui/src/ui/views/usage.test.ts ui/src/ui/app-render-usage-tab.test.ts ui/src/ui/controllers/usage.node.test.ts --reporter=dot.
  • [P1] git diff --check.

What I checked:

  • Root policy read: Read the full root AGENTS.md and scoped guides for UI, gateway, gateway protocol, server methods, docs, and scripts; protocol and compatibility guidance affected the merge-risk review. (AGENTS.md:1, 8bd4736f03bc)
  • Current-main UI source reproduces the missing request scope: On current main, loadUsage sends sessions.usage and usage.cost without any agentId or all-agent scope, so the UI cannot request a non-default agent from the existing controller path. (ui/src/ui/controllers/usage.ts:227, 8bd4736f03bc)
  • Current-main gateway defaults list usage to the default agent: On current main, sessions.usage resolves omitted agentId through resolveDefaultAgentId(config), matching the linked regression report that non-main usage disappears from the Web UI. (src/gateway/server-methods/usage.ts:877, 8bd4736f03bc)
  • PR head adds explicit all-agent scope: At PR head, the UI sends agentScope: "all" by default and agentId when selected, while the gateway adds agentScope handling for sessions.usage and all-agent cost aggregation. (ui/src/ui/controllers/usage.ts:295, d67156a3c552)
  • Remaining fallback-scoping defect: At PR head, selected-agent totals use agentScopedSessions, but aggregate sessions still fall through to sortedSessions and daily chart data stays data.costDaily, so legacy fallback can mix other agents into selected-agent panels. (ui/src/ui/views/usage.ts:275, d67156a3c552)
  • Regression provenance: The default-agent scoping behavior came from the merged gateway session lookup scoping PR, which is the root cause identified by the linked issue. (src/gateway/server-methods/usage.ts:877, 6a12c6f79915)

Likely related people:

  • pgondhi987: PR fix(gateway): scope session data lookups by agent [AI] #81386 and merge commit 6a12c6f added the default-agent scoping that made the Web UI omission visible. (role: introduced current scoped behavior; confidence: high; commits: 6a12c6f79915; files: src/gateway/server-methods/usage.ts, src/gateway/protocol/schema/sessions.ts)
  • steipete: Assigned this PR, applied the proof override, and authored the latest PR commits adding explicit all-agent usage scope and generated Swift compatibility. (role: recent follow-up owner; confidence: high; commits: 9932a64b65da, d67156a3c552; files: src/gateway/server-methods/usage.ts, ui/src/ui/controllers/usage.ts, apps/shared/OpenClawKit/Sources/OpenClawProtocol/GatewayModels.swift)
What the crustacean ranks mean
  • 🦀 challenger crab: rare, exceptional readiness with strong proof, clean implementation, and convincing validation.
  • 🦞 diamond lobster: very strong readiness with only minor maintainer review expected.
  • 🐚 platinum hermit: good normal PR, likely mergeable with ordinary maintainer review.
  • 🦐 gold shrimp: useful signal, but proof or patch confidence is still limited.
  • 🦪 silver shellfish: thin signal; proof, validation, or implementation needs work.
  • 🧂 unranked krab: not merge-ready because proof is missing/unusable or there are serious correctness or safety concerns.
  • 🌊 off-meta tidepool: rating does not apply to this item.

Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

How this review workflow works
  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

@clawsweeper clawsweeper Bot added rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. P2 Normal backlog priority with limited blast radius. merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. labels May 27, 2026
@clawsweeper

clawsweeper Bot commented May 27, 2026

Copy link
Copy Markdown
Contributor

ClawSweeper PR egg

🎁 Pass real behavior proof to wake the egg and unlock a hatchable treat.

Where did the egg go?
  • The egg game starts only after the PR passes the real-behavior proof check.
  • Before that, no creature or rarity is rolled. The treat waits for real proof.
  • This is still just collectible flavor: proof affects review readiness, not creature quality.

@Alix-007

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

Current head 0a4a020cb32f64246b85b150a192cb3b38098cc2 addresses the compatibility concern from the previous review by falling back when legacy sessions.usage rejects agentId and remembering that compatibility result. The PR body includes current-head proof output, and the current CI/proof checks are passing.

@clawsweeper

clawsweeper Bot commented May 27, 2026

Copy link
Copy Markdown
Contributor

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event repository_dispatch).
Result: the existing ClawSweeper review comment will be edited in place when the review finishes.

Re-review progress:

@Alix-007

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

Updated head 9e9dc4ab4d2028a1befe3480cf8a7bc25cd5f302 addresses the latest review concern by sending the same resolved agentId to both sessions.usage and usage.cost, with fallback retry/remember behavior removing agentId from both requests for legacy gateways. The PR body now includes current-head focused proof for the scoped sessions+cost payload and legacy fallback tests.

@clawsweeper

clawsweeper Bot commented May 27, 2026

Copy link
Copy Markdown
Contributor

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event repository_dispatch).
Result: the existing ClawSweeper review comment will be edited in place when the review finishes.

Re-review progress:

@openclaw-barnacle openclaw-barnacle Bot added the gateway Gateway runtime label May 27, 2026
@Alix-007

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

Updated head 1fb24cb8a63a8267364a380eab907ec034bc6551 addresses the gateway-side cost scope finding by threading agentId through the usage.cost handler, scoped cost cache key, and loadCostUsageSummaryFromCache. The PR body includes current-head UI and gateway proof for scoped sessions+cost requests, legacy fallback, and cost cache separation.

@clawsweeper

clawsweeper Bot commented May 27, 2026

Copy link
Copy Markdown
Contributor

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event repository_dispatch).
Result: the existing ClawSweeper review comment will be edited in place when the review finishes.

Re-review progress:

@clawsweeper clawsweeper Bot added rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. and removed rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. labels May 27, 2026
@BingqingLyu

This comment was marked as spam.

@Alix-007

Copy link
Copy Markdown
Contributor Author

Maintainer review/proof request:

Current head 1fb24cb8a63a8267364a380eab907ec034bc6551 has all current PR checks passing, including Real behavior proof. The remaining ClawSweeper ask appears to be live multi-agent Control UI/browser + gateway proof for the non-main agent usage path.

I do not have a real multi-agent browser/gateway setup available to produce stronger live proof from my side. If a maintainer can run or record that verification, or is comfortable accepting the focused UI controller/rendering + gateway handler/cache coverage already in this PR, could you review this PR or apply a proof override? I will avoid adding more commits unless there is a concrete code or proof issue to address.

@steipete steipete self-assigned this May 28, 2026
@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation size: L triage: mock-only-proof Candidate: PR proof only shows tests, mocks, snapshots, lint, typecheck, or CI. and removed size: M proof: supplied External PR includes structured after-fix real behavior proof. labels May 28, 2026
@steipete steipete added the proof: override Maintainer override for the external PR real behavior proof gate. label May 28, 2026
@openclaw-barnacle openclaw-barnacle Bot removed the triage: mock-only-proof Candidate: PR proof only shows tests, mocks, snapshots, lint, typecheck, or CI. label May 28, 2026
@steipete steipete removed the status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. label May 28, 2026
@clawsweeper clawsweeper Bot added rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. and removed rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. labels May 28, 2026
@steipete
steipete force-pushed the issue-87132-usage-agent-filter branch 2 times, most recently from 4bea777 to 900637a Compare May 29, 2026 00:10
@steipete
steipete force-pushed the issue-87132-usage-agent-filter branch from 900637a to aacdb12 Compare May 29, 2026 00:30
@steipete
steipete force-pushed the issue-87132-usage-agent-filter branch from aacdb12 to d67156a Compare May 29, 2026 00:51
@steipete steipete removed the status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. label May 29, 2026
@steipete

Copy link
Copy Markdown
Contributor

Ready to land.

Verification:

  • gh pr checks 87222 --repo openclaw/openclaw --live --json name,state,link returned no non-green entries at head d67156a3c552c4f9c8b6edf8516b6242bf5cdd26.
  • gh pr view 87222 --repo openclaw/openclaw --json mergeStateStatus,mergeable reported CLEAN / MERGEABLE.
  • Local proof already recorded in the PR body: focused UI usage tests, gateway usage tests, pnpm check:test-types, pnpm protocol:check, targeted oxlint, git diff --check, and autoreview.

Known proof gap: no live browser session against a multi-agent gateway; covered here by focused UI/controller tests plus gateway aggregation tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

app: web-ui App: web-ui docs Improvements or additions to documentation gateway Gateway runtime merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. merge-risk: 🚨 security-boundary 🚨 May affect sandboxing, authorization, credentials, or sensitive data. P2 Normal backlog priority with limited blast radius. proof: override Maintainer override for the external PR real behavior proof gate. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. scripts Repository scripts size: L

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Usage page agent filter broken since v2026.5.17 – sessions.usage API always scoped to "main" agent

3 participants