Repository navigation
feat(agents): report per-run stats (code-mode engagement, round trips, cost) in agent JSON envelopes - #114688
Conversation
|
Codex review: needs real behavior proof before merge. Reviewed July 27, 2026, 10:43 PM ET / July 28, 2026, 02:43 UTC. ClawSweeper reviewWhat this changesAdds additive Merge readiness⛔ Blocked until real behavior proof is added - 6 items remain This PR is not ready to merge: Priority: P2 Review scores
Verification
How this fits togetherThe embedded agent runner can make multiple model attempts for one run, collecting usage and tool activity before terminal preparation produces metadata. The agent CLI then exposes selected metadata fields in its JSON result for automation and operator tooling. flowchart LR
A[Agent CLI request] --> B[Embedded model attempts]
B --> C[Per-attempt usage and activity]
C --> D[Run statistics accumulator]
D --> E[Terminal agent metadata]
E --> F[Agent CLI JSON envelope]
Decision needed
Why: The current public field is described as the cost of accumulated run usage, but the implementation has only one terminal model cost configuration after usage from several models has been combined. Before merge
Findings
Agent review detailsSecurityNone. PR surfaceSource +146, Tests +194, Docs +31. Total +371 across 19 files. View PR surface stats
Review metrics
Stored data modelPersistent data-model change detected: Merge-risk optionsMaintainer options:
Copy recommended automerge instructionTechnical reviewBest possible solution: Accumulate monetary cost at each attempt using that attempt’s resolved provider/model pricing, preserve omission when no attempt has usable pricing, add mixed-model retry/fallback regression coverage, and attach a redacted real CLI JSON capture before merge. Do we have a high-confidence way to reproduce the issue? Yes, from source: construct a run with usage from an initial attempt and a retry/fallback using a differently priced final model; current terminal preparation prices the combined usage with only that final model’s cost config. A focused regression test can reproduce this without provider credentials. Is this the best way to solve the issue? No. The new public cost field should preserve pricing per attempt before usage is merged, or deliberately omit mixed-model cost; pricing all accumulated usage with the terminal model is not a reliable run-wide estimate. Full review comments:
Overall correctness: patch is incorrect AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning high; reviewed against 4273ca9dbd9b. LabelsLabel justifications:
EvidenceWhat I checked:
Likely related people:
Rank-up movesOptional improvements that raise the rating; they are not merge blockers.
Rating scale
Overall follows the weaker of proof and patch quality. Workflow
HistoryReview history (3 earlier review cycles)
|
c502e2c to
141d505
Compare
41c2776 to
2046458
Compare
Adds codeModeEngaged, assistantTurns, bridgeCalls, and costUsd to EmbeddedAgentRunMeta.agentMeta and mirrors them on the agent exec --json envelope. Code-mode engagement is stamped from the tool-surface truth, round trips accumulate across attempts beside usage, bridge counts come from the run's tool-search catalog counters, and cost reuses the shared model pricing helpers (cache tiers included, omitted without cost data).
Attempt cleanup clears the per-attempt tool-search catalog, so retries and fallbacks discarded earlier bridge counts. Fold each attempt's bridgeCalls into the run accumulator beside assistantTurns and stamp the cumulative totals into agentMeta, matching the documented per-run contract.
2046458 to
85d7a5f
Compare
|
Merged via squash.
|
…, cost) in agent JSON envelopes (openclaw#114688) * feat(agents): add per-run stats to embedded agent run meta Adds codeModeEngaged, assistantTurns, bridgeCalls, and costUsd to EmbeddedAgentRunMeta.agentMeta and mirrors them on the agent exec --json envelope. Code-mode engagement is stamped from the tool-surface truth, round trips accumulate across attempts beside usage, bridge counts come from the run's tool-search catalog counters, and cost reuses the shared model pricing helpers (cache tiers included, omitted without cost data). * fix(agents): accumulate bridge call counts across run attempts Attempt cleanup clears the per-attempt tool-search catalog, so retries and fallbacks discarded earlier bridge counts. Fold each attempt's bridgeCalls into the run accumulator beside assistantTurns and stamp the cumulative totals into agentMeta, matching the documented per-run contract.
What Problem This Solves
Agent runs report token usage today, but consumers of
openclaw agent --jsonandopenclaw agent exec --jsoncannot answer three practical questions about a run:"auto"tier, engagement now also depends on per-model capability, and it is silently skipped for models routed through a native harness surface (for example OpenAI-family models on their harness). The shell tool is also namedexec, so tool names cannot be trusted as an engagement signal.Why This Change Was Made
The data already exists at run time —
codeModeControlsEnabledForRunin the embedded tool-surface prepare (now computed viaisCodeModeEngagedForModelfrom #114695), per-catalogsearch/describe/callcounters in the tool-search runtime, per-message assistant completions in the subscription, and the shared pricing helpers insrc/utils/usage-format.ts— it just was never projected into the run metadata. This change stamps it intoEmbeddedAgentRunMeta.agentMetaadditively:codeModeEngaged: boolean— from the embedded attempt's tool-surface truth; plugin-harness attempts that do not report engagement intentionally read asfalse, which makes the config-enabled-but-unengaged case observable. The attempt-result field is an additive seam harnesses can adopt (the tool-surface bridge already exposescodeModeControlsEnabled).assistantTurns— completed assistant round trips, accumulated across model attempts beside the existing usage accumulator so retried attempts stay counted like their tokens; omitted at zero.bridgeCalls: { search, describe, call }— cumulative across every model attempt of the run: each attempt's catalog counters are read before attempt cleanup clears the catalog session, then folded into the run accumulator (mergeAttemptRunStatsIntoAccumulator), so retries/fallbacks do not discard earlier counts. Outer calls stay in the unchangedtoolSummary.calls.costUsd— computed with the existingresolveModelCostConfig+estimateUsageCosthelpers (cache read/write pricing and tiered pricing included), omitted entirely when the model has no cost data — no zero sentinels.No existing envelope/meta fields were removed or renamed; no config keys, env vars, SQLite, or gateway-protocol schema changes. The spec's optional "code mode enabled but not engaged" startup log was skipped: engagement truth is distributed across surfaces (the codex harness has its own native code mode), so a core-side log would fire in misleading places;
codeModeEngagedin the run meta is the reliable signal.User Impact
openclaw agent --json(meta.agentMeta) andopenclaw agent exec --json(top-level envelope) now reportcodeModeEngaged,assistantTurns,bridgeCalls, andcostUsd. Documented indocs/cli/agent.mdand the code-mode telemetry section ofdocs/tools/code-mode.md. Existing consumers are unaffected (additive fields only).Evidence
Focused tests (new coverage: codeModeEngaged engaged/not-engaged/unreported stamping, assistant-turn counting incl. transcript-only and non-assistant exclusions, cumulative bridge-call accumulation across retry/fallback attempts plus omission for catalog-less runs, costUsd math with cache pricing plus both omission cases):
node scripts/check-changed.mjsfull gate (delegated Blacksmith Testbox, exit 0, 24m47s): conflict markers, env ratchet, max-lines ratchet, formatting, plugin-SDK API manifest, deprecated API usage, plugin boundaries, core typecheck, and the full changed-lane test fan-out all green.Autoreview (Codex, gpt-5.6-sol): clean on both the base change and the bridge-accumulation follow-up — "no accepted/actionable findings reported", "patch is correct (0.98)".
ClawSweeper rank-up moves: bridge-call accumulation across attempts is implemented in 41c2776 with a retry regression test. The redacted real-run JSON capture is skipped in this landing: the isolated landing environment has no provider credentials, and the envelope shape plus omission behavior are pinned by the focused unit tests above (
agent-exec.test.tsexercises the envelope projection; terminal-preparation tests pin field presence/omission). Happy to attach a live capture in a follow-up from a credentialed setup.