Skip to content

fix: internal memory and skill reviews omit usage diagnostics - #169179

Merged
obviyus merged 7 commits into
openclaw:mainfrom
PratikRai0101:fix/internal-turn-usage-169156
Oct 23, 2026
Merged

obviyus merged 7 commits into
openclaw:mainfrom
PratikRai0101:fix/internal-turn-usage-169156

Conversation

@PratikRai0101

@PratikRai0101 PratikRai0101 commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

Related: #169156.

What Problem This Solves

Completed memory-flush and Skill Workshop review inference returns provider-reported usage but never settles it through the existing model.usage diagnostic path. Operators therefore lose token counts and available estimated cost for that internal work.

User Impact

Diagnostics now record those internal calls independently. Their usage is not added to the foreground conversation's totals. Returned-error results still account for consumed tokens; diagnostics-disabled, no-usage, and unknown-pricing behavior remain conservative.

Why This Change Was Made

Use one shared internal-run settlement helper at the two completed-result boundaries. It reuses existing usage normalization, pricing, diagnostic enablement, and trace-context owners. No diagnostic schema, exporter permission, plugin SDK surface, or configuration option changes.

The existing memory-flush failure recorder is moved unchanged out of the large orchestration module. Cancellation, source authority, retry limits, and persisted failure behavior are preserved.

Evidence

Captured real provider requests, exported OTLP usage spans/counters, and red/green owner tests.

Built Gateway/CLI artifacts on Node 24.22.1 in isolated Linux Docker. Ordinary shipped chat.send/agent.wait turns naturally crossed the existing 1024-byte transcript trigger. Workshop had actual tool availability, auto mode, ten successful ordinary turns, and its normal quiet period. A finite owned Responses service returned legal synthetic usage; the official diagnostics-otel plugin exported to an authorized passive local OTLP receiver. No private subscriber, injected event/result, seeded maintenance row, held response, or external provider account was used.

  • Both arms made exactly three memory-flush HTTP calls reporting 111 input + 22 output tokens, and one Workshop review reporting 73 + 17 tokens.
  • Main exported no matching internal model.usage spans or internal token/cost settlement.
  • Candidate exported exactly three 133-token memory-flush spans and one 90-token review span. Each internal HTTP request matches exactly one usage span.
  • Foreground webchat stayed at 208 tokens / $0.000608 on both arms; activity summaries stayed at 234 tokens / $0.000684. Candidate added a distinct internal counter of 489 tokens / $0.001476, without duplication.
  • Cost figures are estimates using explicitly configured synthetic rates ($2/million input, $8/million output), not measured spend.
  • Focused owner suites: baseline 5 failed / 14 passed; candidate 19 passed, including disabled/empty usage, unknown pricing, returned errors, and existing lifecycle controls. Targeted formatting passed.

Simplification: one narrow helper serves both owners; no new accounting state or generalized framework. Most production lines touched move the existing failure path without changing it. Production net +90 lines; tests +279; documentation +4. Retaining the failure helper keeps the orchestration module smaller instead of duplicating or compressing its lifecycle rules.

Merge Danger

Two-way door: reverting removes the added diagnostic settlement; no migration or stored format changes. Blast radius is token/cost observability for these two internal inference paths. Foreground accounting and inference behavior remain unchanged.

@clawsweeper

clawsweeper Bot commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

ClawSweeper review complete

ClawSweeper finished reviewing this revision. The review result is being finalized.

View the workflow run.

@clawsweeper clawsweeper Bot added P2 Normal backlog priority with limited blast radius. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. labels Oct 11, 2026
@clawsweeper

clawsweeper Bot commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge.

What this changes

Records separate token usage and available estimated cost for completed memory-flush and Skill Workshop review runs.

Example: A memory flush reports 111 input tokens and 22 output tokens with configured synthetic pricing.

  • Before: The provider request consumes 133 tokens without a corresponding internal usage span.
  • After: One 133-token usage span is exported, while foreground conversation totals remain unchanged.

Review scores

Measure Result What it means
Overall readiness 🦞 diamond lobster (5/6) Focused accounting repair with baseline/candidate Gateway proof, meaningful regression controls and no identified correctness or security defect.
Proof confidence 🦞 diamond lobster (5/6) Sufficient (live_output): Built Gateway/CLI proof on isolated Linux Docker with Node 24.22.1 exercises naturally triggered memory flushes and Workshop review through ordinary chat.send/agent.wait turns, a real HTTP Responses fixture and the official OTLP exporter; captured output shows exactly one usage span per internal request and unchanged foreground totals, and the exercised production files are unchanged at the reviewed head.
Patch quality 🦞 diamond lobster (5/6) No actionable review findings were identified.

Product

Kind: Bug fix · Worth it: Yes · Fix scope: Complete
User problem: Operators cannot account for token consumption and available estimated cost from completed internal memory and Workshop inference.
Reason: The patch restores existing diagnostic behavior for two omitted owners without new configuration, permissions, schemas or public APIs.

Merge readiness

✅ Ready for maintainer review

Keep open: this PR repairs a current accounting gap with a focused implementation and no actionable findings; exact-head CI remains pending.

Priority: P2
Reviewed head: 2134e39d3b45e7134d0f48fc0e66aa53b4164204

Before merge

None.

Findings

None.

Agent review details

How this fits together

Memory-flush and Workshop owners receive completed inference results; the shared helper converts their usage into trusted diagnostic events consumed by existing exporters.

flowchart TD
  A[Ordinary Gateway turns] --> B[Memory flush]
  A --> C[Workshop review]
  B --> D[Internal usage settlement]
  C --> D
  D --> E[Trusted diagnostic events]
  E --> F[OpenTelemetry metrics and spans]
Loading

Technical review

Best possible solution:

Retain the shared settlement helper at both completed-result boundaries and land through the existing CI gates.

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

Supplied baseline Gateway traces reproduce real internal HTTP calls without corresponding usage settlement, corroborating the current-main result boundaries; no target code was executed during this review.

Is this the best way to solve the issue?

The patch fixes the missing settlement at its two owners, reuses existing accounting contracts and preserves foreground accounting, failure handling and inference behavior.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against 97d22d3a5b5a.

Provenance checked

Testing

Proof path: shipped entry point.

Security

None.

Evidence

What I checked:

  • Pinned patch and unchanged failure behavior: The introduced diff adds settlement at both completed-result boundaries; comparing the extracted failure-recorder body against the base produces no difference after removing its export modifier. (src/auto-reply/reply/agent-runner-memory.ts:920, 2134e39d3b45)
  • Existing accounting owners reused: The helper uses existing diagnostic enablement, billable-usage detection, normalization, aggregate pricing and trace-context operations without adding accounting state. (src/agents/internal-run-usage.ts:24, 2134e39d3b45)
  • Current-main gap and distinct earlier repair: Current main's Workshop result boundary still only checks success, and memory flush lacks this settlement; the merged fix: background utility completions are missing from token and cost metrics #167986 handles runIsolatedCompletion rather than these embedded maintenance owners. (src/skills/workshop/experience-review.ts:323, 97d22d3a5b5a)
  • Captured shipped-entry-point proof: obviyus supplied complete baseline/candidate Gateway and OTLP traces at fix: internal memory and skill reviews omit usage diagnostics #169179 (comment): three 133-token memory calls and one 90-token Workshop call each gain exactly one span; internal metrics add 489 tokens and $0.001476 estimated cost while foreground metrics remain unchanged. The captured comment identity is SHA-256 c8db4183465c76406698b61288d57554f23423f471f78b2da927b0336e45bf84. The exercised production files are unchanged between the proof revision and reviewed head. (f8f542156dfe)
  • Regression and control evidence: The same proof comment reports five intended missing-event failures against baseline production and 19 passing candidate cases, including returned errors, disabled diagnostics, absent usage, unknown pricing and existing lifecycle controls. (src/skills/workshop/experience-review.test.ts:335, f8f542156dfe)
  • Current merge and check state: GitHub reports the reviewed head as mergeable, with a pending CI gate; paginated check reads found no failed, cancelled or timed-out checks. (2134e39d3b45)

PR surface

Source +90, Tests +278, Docs +4. Total +372 across 7 files. Added test files: 1.

View PR surface stats
Area Files Added Removed Net
Source 4 166 76 +90
Tests 2 278 0 +278
Docs 1 4 0 +4
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 7 448 76 +372

Review metrics

Metric Value Why it matters
Production versus test growth Production net +90 lines; tests +278 lines Growth supplies one shared settlement implementation and owner-boundary regressions; the failure-recorder move preserves existing logic.

Labels

Label changes:

No label changes.

Label justifications:

  • P2: Missing internal token and cost accounting affects operator observability without blocking inference or foreground replies.
  • rating: 🦞 diamond lobster: Overall readiness is 🦞 diamond lobster; proof is 🦞 diamond lobster and patch quality is 🦞 diamond lobster.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.
  • proof: sufficient: Contributor real behavior proof is sufficient.

Rating scale

6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior.

Workflow

ClawSweeper edits this one comment on every review. Comment @clawsweeper re-review for a fresh review only. Repair and merge execution lanes are retired.

History

Review history (10 earlier review cycles; latest 8 shown)
  • reviewed 2026-10-11T16:24:37.893Z sha a7cd427e014c5430b484f99c64c845fd917509ff :: needs real behavior proof before merge. :: none
  • reviewed 2026-10-11T17:04:28.058Z sha 2623739e1a05b41b067a9d81404b3d1959ed3ef8 :: needs real behavior proof before merge. :: none
  • reviewed 2026-10-11T17:35:12.681Z sha eaeca8842afba2f3727a6cbe194aedbfa1d9b836 :: needs maintainer review before merge. :: none
  • reviewed 2026-10-12T19:10:19.057Z sha 708bc568bafc1685df324954daf38c57d3feaac1 :: needs maintainer review before merge. :: none
  • reviewed 2026-10-13T07:31:49.820Z sha 37382ac51b1dfd8f5248c609dccdb74d9ff77b1d :: needs maintainer review before merge. :: none
  • reviewed 2026-10-13T10:37:31.386Z sha 208bb58b0a61ebdbaeaa4a56359d1e9bc6b8e439 :: needs maintainer review before merge. :: none
  • reviewed 2026-10-23T06:53:15.855Z sha f8f5421 :: needs maintainer review before merge. :: none
  • reviewed 2026-10-23T07:16:58.683Z sha ca28493 :: needs maintainer review before merge. :: none

Reviewed October 23, 2026, 3:34 AM ET / 07:34 UTC (Revision 11).

@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: L and removed size: XS labels Oct 11, 2026
@PratikRai0101
PratikRai0101 marked this pull request as ready for review October 11, 2026 16:04
@PratikRai0101

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

The PR body now includes Gateway/entry-point runtime proof for both changed owners: detached memory-flush and Skill Workshop review turns each emit exactly one trusted model.usage event under the internal run's own session identity, foreground accounting is unchanged, and the same events reach the bundled OpenTelemetry exporter. Controls cover unknown pricing (cost omitted, not zero) and diagnostics disabled (no events).

@clawsweeper

clawsweeper Bot commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@clawsweeper clawsweeper Bot added proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦞 diamond lobster Very strong PR readiness with only minor maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. and removed rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. labels Oct 11, 2026
@PratikRai0101
PratikRai0101 force-pushed the fix/internal-turn-usage-169156 branch from 8ad0263 to 633d8b9 Compare October 21, 2026 15:16
@PratikRai0101

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

Rebased onto current main (51c9749) and resolved the memory-flush extraction against upstream's newer one-step failure recorder; the usage emit call sites are unchanged. Current-head local proof: all eight affected suites passed across five Vitest shards (307.84 s wall), pnpm tsgo:core passed, and every changed-path core test-typecheck shard passed. The only local non-green checks are inherited: the core test-shard root-budget advisory and three max-lines baseline entries in UI files outside this diff.

@clawsweeper

clawsweeper Bot commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. labels Oct 21, 2026
@clawsweeper clawsweeper Bot removed clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. labels Oct 23, 2026
@obviyus

obviyus commented Oct 23, 2026

Copy link
Copy Markdown
Collaborator

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. and removed clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. labels Oct 23, 2026
@obviyus

obviyus commented Oct 23, 2026

Copy link
Copy Markdown
Collaborator

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. and removed clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. labels Oct 23, 2026
@obviyus

obviyus commented Oct 23, 2026

Copy link
Copy Markdown
Collaborator

Remaining CI failure: reproduced without this PR's production changes

Head: 2134e39d3b45e7134d0f48fc0e66aa53b4164204 (contains current main's restart-recovery changes, including #181373). The earlier fixture type error is fixed; all current type lanes pass.

Failing job: checks-node-changed-compact-small-5.

Its three failing cases are in the unchanged src/cron/isolated-agent/run.operator-continuation.test.ts:

  • scheduled continuation preserves the restart boundary
  • scheduled continuation preserves the restart with prior operator source boundary
  • scheduled continuation preserves the restart with prior operator source and steering boundary

All three fail at line 763 because restartRecoveryDeliverySourceRunId is a different UUID than the expected interrupted run ID. The interrupted status and restart cycle fields still match.

I reproduced the identical three cases in a secretless Linux-amd64 Node 24.22.1 Docker container, using the repository-pinned Bun fork and the original hosted cron group. The arm64 Node and Bun runs had passed, so they were not used to dismiss hosted evidence.

Local amd64 arm Production source Cron result
Head This PR on main 3 failed / 309 passed, 1 failed / 36 passed files
Revert baseline Only this PR's production diff reversed to main 2fe4b7241b40fee1ae7f3aad29112299e88acc4c; candidate tests retained Same 3 failed / 309 passed, same failing file

The baseline mismatch is the same assertion shape:

- restartRecoveryDeliverySourceRunId: 29c092f5-8f9a-4929-8552-9cfbbff289f4
+ restartRecoveryDeliverySourceRunId: acc713cc-22e8-4f03-bc25-b02ecad82d6d
  status: interrupted

This diff adds usage settlement only after completed memory-flush/review inference and moves the existing memory-flush failure recorder unchanged. It does not modify cron continuation, restart marking, or this test. The same failures with all production changes reverted establish that this PR is not their cause.

Retained local evidence: amd-head-hosted-groups.log, amd-main-baseline-cron.log, hosted-node-groups.json, and run-hosted-groups.mjs. The baseline compiler's first attempt hit a native allocation failure before test execution; the recorded baseline witness uses only the failing cron group with bounded allocator/compiler parallelism and completes all 312 cron cases. The accounting head was restored afterward.

No failed hosted job was rerun. Cancelled checks on this head: 0. Waiting for maintainer disposition of this inherited red; no further push to chase green.

@obviyus

obviyus commented Oct 23, 2026

Copy link
Copy Markdown
Collaborator

Maintainer override: checks-node-changed-compact-small-5 is an inherited failure, not caused by this PR.

The three failing cases are in src/cron/isolated-agent/run.operator-continuation.test.ts (restart cases disagreeing only on restartRecoveryDeliverySourceRunId), a file this PR doesn't touch: https://github.com/openclaw/openclaw/actions/runs/39917946482/job/120296528138. In a Linux amd64 Node 24.22.1 container with the pinned Bun and the exact hosted test groups, this head fails 3/309, and the same groups with only this PR's production diff reverted to main 2fe4b72 fail the same 3/309. Evidence: #169179 (comment). This PR only changes internal memory-flush and Skill Workshop usage accounting. All other required checks pass.

@obviyus
obviyus merged commit f92d068 into openclaw:main Oct 23, 2026
134 of 137 checks passed
@obviyus

obviyus commented Oct 23, 2026

Copy link
Copy Markdown
Collaborator

Landed in f92d068. Thanks @PratikRai0101. Memory flush and Skill Workshop runs now show up in usage and cost.

@PratikRai0101
PratikRai0101 deleted the fix/internal-turn-usage-169156 branch October 23, 2026 09:12
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 24, 2026
…aw#169179)

fix(usage): record usage for internal memory-flush and Skill Workshop review runs

Completed internal memory-flush and Skill Workshop review inference used provider-reported tokens but skipped the existing model.usage settlement, so operators lost the token counts and estimated cost for that work. These runs now settle usage the same way ordinary turns do.

Evidence: built Gateway with a local provider fixture that reports usage, a natural memory flush and a Skill Workshop review. On main three 133-token flushes and a 90-token review produced no usage records; with this change they produce exactly four usage spans and a 489-token, $0.001476 cost entry. Ordinary chat (208) and summary (234) totals are unchanged.

Thanks @PratikRai0101.

Co-authored-by: Ayaan Zaidi <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling docs Improvements or additions to documentation gateway Gateway runtime P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦞 diamond lobster Very strong PR readiness with only minor maintainer review expected. size: L status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants