Repository navigation
Conversation
runIsolatedCompletion returned token usage but never emitted the model.usage diagnostic event, so background utility completions (session titles, activity recaps, session observer, progress narration, transcript summaries) were missing from token and cost series. Emit one trusted model.usage event per completed call, estimated against the model cost config like the plugin and cron paths. Plugin completions are skipped: their callers finalize usage through finalizePluginLlmCompletion, which already emits.
|
🦞👀 Pull request received. I will update this pull request when review starts. ClawSweeper review completeClawSweeper finished reviewing this revision. The review result is being finalized. |
|
Thanks for the context here. I swept through the related work, and this is now duplicate or superseded. This PR is useful and now has convincing Gateway proof, but a proof-positive, cleanly mergeable replacement covers the same repair and additionally preserves terminal CLI usage totals. Root-cause cluster Members:
Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything. Canonical path: Follow the replacement PR and preserve this contribution’s admitted-agent pricing regression and contributor credit. So I’m closing this here because the remaining work is already tracked in the canonical issue. Review detailsBest possible solution: Follow the replacement PR and preserve this contribution’s admitted-agent pricing regression and contributor credit. Do we have a high-confidence way to reproduce the issue? Yes. Current-main source lacks the usage event, and supplied Gateway before/after output demonstrates a successful title completion with absent counters before the repair. Is this the best way to solve the issue? The shared completion boundary is appropriate, but the replacement is more complete because it also carries terminal CLI totals while retaining agent-specific pricing. Security review: Security review cleared: The diff adds usage instrumentation without changing credentials, permissions, dependencies, or execution authority. AGENTS.md: found and applied where relevant. What I checked:
Codex review notes: model internal, reasoning medium; reviewed against fcbeec3103ba. |
|
@clawsweeper re-review |
|
🦞👀 Re-review progress:
|
|
Superseded by #167986, which covers the same repair surface — central |
Closes #167913
What Problem This Solves
runIsolatedCompletionreturns the runtime'susagebut never emits amodel.usagediagnostic event. Its five core callers (conversation-label-generator.ts,progress-narrator-model.ts,session-observer-model.tsvia session activity summaries and observer completions,summary-model.ts) read only the returned.text, so every background utility completion — session titles, activity recaps, session-observer runs, progress narration, transcript summaries — is invisible toopenclaw_model_tokens_totalandopenclaw_model_cost_usd_total. Operators on a Claude CLI-backed gateway with the default utility model lose the entire Haiku series: the reporter measured 778 billed calls ($8.03, ~3.9M tokens) with no matching Prometheus series.Sibling paths already count these calls: plugin
runtime.llm.completecompletions emitmodel.usagethroughfinalizePluginLlmCompletion(#107937), and cron isolated agent runs emit throughrecordCronRunUsage.Fix
Emit one trusted
model.usageevent centrally inrunIsolatedCompletion, after a completed call, with token buckets normalized vianormalizeUsageand cost estimated throughestimateAggregateUsageCostagainst the model cost config — the same estimator the plugin path uses for isolated runtimes. Two boundaries:purpose === "plugin-completion"): their two callers pass usage tofinalizePluginLlmCompletion, which emits there. Emitting centrally too would double-count them.Evidence
Pre-fix reproduction (
test 1below fails onmain): with the CLI harness stubbed to return{input: 10, output: 462, cacheRead: 0, cacheWrite: 4111, total: 4583},runIsolatedCompletionreturns the usage but the diagnostic bus records zeromodel.usageevents:(Without the fix, both emission tests fail — the CLI-path test and the harness-path test — while the skip/gating tests pass. This matches the reporter's observation that
diagnostic events observed: []onmainat476ce032f4f0.)Post-fix, all five cases pass:
The event carries the labels metrics consumers already read —
agentId,provider,model— plususage: {input: 10, output: 462, cacheRead: 0, cacheWrite: 4111, promptTokens: 4121, total: 4583}.No behavior change for callers: the returned result is unchanged; only the diagnostic stream gains the event. The existing
isolated-completion.test.tssuite (and the plugin-scope/resources/operator-model-policy suites) pass unchanged.Gateway/exporter proof and review repair
Verified candidate:
ac6fdcc98f98c5a221ebf451701354fa904f05ec(preserves @ruel225's original commit). Thanks @etzelm for the report and @ruel225 for the fix.Addressed the pricing review finding: the central usage finalizer now carries
lease.snapshot.agentDirintoestimateAggregateUsageCost. A regression uses an admitted non-default agent's localmodels.jsonrate of $20/million input tokens: it fails on the original contributor head (cost missing), then passes with the repair. No config, SDK, or stored-state contract changes.Real operator before/after
Built current main
427f926e7c2d763e362f59678c6486b6f2f4aec4and the candidate independently in a secretless Node 24.16.0 Linux container. Each Gateway used its own state/home/profile, a non-default loopback port, diagnostics enabled, and the bundled diagnostics-prometheus exporter. A local OpenAI-compatible HTTP provider returned 101 input + 17 output = 118 tokens per request; configured prices were $2/million input and $3/million output ($0.000253/call). No internal completion functions or diagnostic emitters were substituted.The operator action was the shipped CLI calling
sessions.title.prepare, with distinct primaryproof/proof-modeland utilityproof/proof-utilitymodels. Both revisions returned{"title":"Metrics proof title"}. Scraped the documented protected metrics endpoint/api/diagnostics/prometheus(the exporter does not expose a literal/metricsroute).proof-utility,channel="unknown"runtime.llm.completeisolated completion /proof-model,channel="unknown"proof-model,channel="http"The plugin control was a fixture plugin registered through the public plugin API and invoked through its Gateway RPC. The normal-turn control used the shipped
agentCLI against the running Gateway. Provider request logs corroborated one request for each control, with no double counting. Normal-turn lifecycle also made two real Activity recap requests on each revision: main still exported no utility series; candidate's utility series increased to input 303, output 51, prompt 303, total 354, cost 0.000759—exactly three utility calls including the title, not a duplicate normal turn.Candidate scrape immediately after the title:
Checks
pnpm build: passed for main and candidate.pnpm testwith--maxWorkers=1: 122 tests passed across 5 files, 78.22 seconds wall time: isolated-completion owner + usage diagnostics, plugin LLM diagnostics + isolated runtime, and Prometheus service.git diff --check: passed.Maintainer intent: checked Sarah's #159246 change in the touched owner; it only documents the existing Agents API restricted-session exception. This change preserves that execution contract. No overlapping Pash change was found.
Merge danger: two-way door; reverting removes diagnostic accounting only. Blast radius is token/cost consumers for core utility completions; plugin accounting and normal agent-turn accounting remain on their existing owners.