Skip to content

fix: status loses context usage after tool-only turns - #134634

Merged
Patrick-Erichsen merged 2 commits into
openclaw:mainfrom
goslingmanagment:context-usage-tool-only-fix
Sep 1, 2026
Merged

Patrick-Erichsen merged 2 commits into
openclaw:mainfrom
goslingmanagment:context-usage-tool-only-fix

Conversation

@goslingmanagment

@goslingmanagment goslingmanagment commented Sep 1, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Fixes an issue where users checking /status after a turn delivered entirely through a messaging tool would see Context: ?/<window> in Telegram and the TUI even though the Codex runtime supplied exact context usage.

Reproduction

Tested live with OpenClaw 2026.8.1 (ea80657), the OpenAI Codex runtime, and a pinned gpt-5.6-sol session:

  1. Send a prompt whose reply is delivered through the message tool, with no terminal assistant message. In the live reproduction, тут? produced Тут. 🐿️ through the messaging tool.
  2. Run /status immediately afterward.

Actual result in both Telegram and the TUI:

Context: ?/272k

Expected result: /status should show the current used-token count because the runtime emitted exact context usage for the turn.

Why This Change Was Made

Codex app-server token usage is projected into attemptUsage.contextUsage independently of assistant messages. A tool-only turn can therefore have exact attempt usage but no currentAttemptAssistant. Attempt normalization considered assistant and prompt-cache usage when selecting the latest context snapshot, but omitted the exact attempt usage, so lastRunPromptUsage became undefined and downstream status metadata marked the total as not fresh.

This change adds attemptUsage as the newest candidate only when it carries explicit contextUsage. Aggregate billing-only usage remains excluded, and an explicit unavailable-context sentinel remains authoritative.

User Impact

Telegram and TUI /status output can retain the current context usage after tool-only turns instead of intermittently displaying ?. Status still reports unknown when the runtime genuinely has no exact context provenance.

Evidence

Real after-fix runtime proof

Full sanitized live trace and reproduction command from this PR head (8bcea6eacebe). The existing repository live gateway harness launched the plugin-owned Codex app-server and made a real model request; usage was not mocked.

The turn completed exactly one message tool call and emitted no assistant text. The same session then produced:

assistantTextEvents=0
successfulToolNames=["message"]
totalTokens=18585
totalTokensFresh=true
/status => 📚 Context: 19k/258k (7%)

The live Vitest run passed in 54.07 seconds. Temporary assertion-only instrumentation used to capture the trace was removed and is not part of this PR.

Pinned Codex contract verification

OpenClaw pins @openai/codex to 0.151.0. Its matching upstream tag, rust-v0.151.0, resolves to Codex commit 78c290807ce710180111df227df3b7a4fe845452.

Direct inspection of that exact source confirms:

This matches OpenClaw's projector contract: exact context usage can exist for a tool-only turn with no assistant message.

Regression and repository checks

  • Added a focused tool-only regression test. Before the production change it failed with lastRunPromptUsage: undefined; after the change it passes.
  • Related normalization, usage-selection, and Codex projector suites: 53 tests passed across 3 Vitest shards.
  • Changed-file repository gate passed, including formatting, lint, core/core-test typechecks, dead-export scanning, and import-cycle checks.
  • Structured Fable max autoreview: scoped-clean, with no accepted or actionable findings.

No changelog entry was added, per contributor guidance.

@clawsweeper

clawsweeper Bot commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

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

@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: XS labels Sep 1, 2026
@clawsweeper clawsweeper Bot added P2 Normal backlog priority with limited blast radius. proof: telegram-e2e This PR needs Telegram Test Server proof with the repository E2E skill. 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. labels Sep 1, 2026
@clawsweeper

clawsweeper Bot commented Sep 1, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed August 31, 2026, 10:28 PM ET / September 1, 2026, 02:28 UTC.

ClawSweeper review

What this changes

Preserves exact current-turn context usage for status output when an agent turn completes through a tool without an assistant transcript message.

Regression provenance

Possible regression — probable (reproduction; reviewed change). No predecessor PR is attributed.

Merge readiness

⚠️ Ready for maintainer review - 3 items remain

The focused patch appears to repair the lost status snapshot and remains necessary because current main still omits attempt-level context usage; formal merge review remains pending the required direct inspection of the pinned Codex source and exact-head checks.

Priority: P2
Reviewed head: 214fe35b169186cd6023601d78b50d9d299057bc

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) Focused regression coverage and claimed real-path proof support a solid repair, with the required direct pinned-Codex inspection still unavailable to this reviewer.
Proof confidence 🦞 diamond lobster (5/6) Sufficient (live_output): The PR body reports an after-fix real Codex app-server run with one message-tool call, zero assistant-text events, fresh total usage, and numeric status output; the linked sanitized trace could not be retrieved locally because network resolution was unavailable.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Verified Sufficient (live_output): The PR body reports an after-fix real Codex app-server run with one message-tool call, zero assistant-text events, fresh total usage, and numeric status output; the linked sanitized trace could not be retrieved locally because network resolution was unavailable.
Evidence reviewed 7 items Introduced change: Host introduction evidence verifies that the production selection change and its focused regression test are exactly the merge-base-to-head delta.
Lost usage path: Before the patch, normalization considered only assistant-derived and prompt-cache usage, although the settled attempt can carry provider usage independently of an assistant transcript.
Provider-to-status boundary: The Codex projector records normalized thread token usage, the settled attempt passes usage into normalization, and terminal preparation forwards normalized usage to status metadata.
Findings None None.
Security None None.

How this fits together

The embedded agent runner receives provider usage after a turn settles, normalizes it into status metadata, and passes that metadata to Telegram and terminal status views. This change keeps a provider-supplied context snapshot available when no assistant text was emitted.

flowchart LR
  A[Codex usage event] --> B[Codex usage projector]
  B --> C[Settled agent attempt]
  C --> D[Usage normalization]
  D --> E[Status metadata]
  E --> F[Telegram and terminal status]
Loading

Before merge

  • Resolve merge risk (P1) - Repository policy requires a direct read of the pinned upstream Codex implementation before a merge recommendation; that checkout could not be inspected in this environment.
  • Resolve merge risk (P1) - The provided exact-head check snapshot was still incomplete, so normal required-check completion remains pending.
  • Complete next step (P2) - No additional repair is indicated; a reviewer must complete the repository-required direct Codex contract check and await exact-head validation.
Agent review details

Security

None.

PR surface

Source +4, Tests +19. Total +23 across 2 files.

View PR surface stats
Area Files Added Removed Net
Source 1 5 1 +4
Tests 1 19 0 +19
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 2 24 1 +23

Review metrics

Metric Value Why it matters
Production vs test LOC production +4 net (5 added, 1 removed); tests +19 The small production addition is the explicit newest exact-context candidate needed to retain the current-turn snapshot.

Merge-risk options

Maintainer options:

  1. Decide the mitigation before merge
    Retain the owner-boundary fix, then complete the pinned Codex contract check and exact-head validation before merging.
  2. Pause or close
    Do not merge this PR until maintainers decide whether the risk is worth taking.

Technical review

Best possible solution:

Retain the owner-boundary fix, then complete the pinned Codex contract check and exact-head validation before merging.

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

No independent current-main run was performed in this read-only environment; the source path is clear and the PR body reports a concrete live tool-only reproduction.

Is this the best way to solve the issue?

Yes for the local owner boundary: selecting exact attempt usage ahead of transcript-derived candidates preserves current-turn provenance while retaining unavailable-context semantics; the required direct Codex contract check remains outstanding.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against 765c0cddfd91.

Labels

Label changes:

  • add rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🦞 diamond lobster and patch quality is 🐚 platinum hermit. Replaced prior rating: 🦐 gold shrimp.
  • remove rating: 🦐 gold shrimp: Current PR rating is rating: 🐚 platinum hermit, so this older rating label is no longer current.

Label justifications:

  • P2: This repairs incorrect status accuracy in an established agent workflow without losing messages, session state, or execution availability.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🦞 diamond lobster and patch quality is 🐚 platinum hermit. Replaced prior rating: 🦐 gold shrimp.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (live_output): The PR body reports an after-fix real Codex app-server run with one message-tool call, zero assistant-text events, fresh total usage, and numeric status output; the linked sanitized trace could not be retrieved locally because network resolution was unavailable.
  • proof: sufficient: Contributor real behavior proof is sufficient. The PR body reports an after-fix real Codex app-server run with one message-tool call, zero assistant-text events, fresh total usage, and numeric status output; the linked sanitized trace could not be retrieved locally because network resolution was unavailable.
  • proof: telegram-e2e: This PR needs Telegram Test Server proof with the repository E2E skill. The normalized status metadata is rendered in Telegram, and the PR’s claimed live proof specifically exercises that visible result.

Evidence

What I checked:

Likely related people:

  • Peter Steinberger: Suggested for follow-up; no historical authorship or introduction is verified. (role: unverified routing candidate; confidence: low)

Rank-up moves

Optional improvements that raise the rating; they are not merge blockers.

  • Have a reviewer with the pinned upstream Codex source verify the token-usage event path.
  • Wait for exact-head required checks to complete successfully.

Rating scale

Score Internal tier Crab rank Meaning
6/6 S 🦀 challenger crab Exceptional readiness
5/6 A 🦞 diamond lobster Very strong readiness
4/6 B 🐚 platinum hermit Good normal PR; ordinary maintainer review
3/6 C 🦐 gold shrimp Useful, but confidence is limited
2/6 D 🦪 silver shellfish Proof or implementation needs work
1/6 F 🧂 unranked krab Not merge-ready
N/A NA 🌊 off-meta tidepool Rating does not apply

Overall follows the weaker of proof and patch quality.
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.

Workflow

  • 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.

History

Review history (2 earlier review cycles)
  • reviewed 2026-09-01T01:57:28.744Z sha 8bcea6e :: needs real behavior proof before merge. :: none
  • reviewed 2026-09-01T02:18:23.605Z sha 8bcea6e :: needs maintainer review before merge. :: none

@goslingmanagment

Copy link
Copy Markdown
Contributor Author

Added real after-fix tool-only runtime proof to the PR body: one successful message call, zero assistant-text events, fresh persisted usage, and numeric /status. I also added direct source verification for the exact pinned @openai/[email protected] tag.

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Sep 1, 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 exact_review_queue).
Result: when the review finishes, ClawSweeper will create the durable review comment if needed or update the existing comment in place.

Re-review progress:

@clawsweeper clawsweeper Bot added proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. and removed 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. labels Sep 1, 2026
@Patrick-Erichsen Patrick-Erichsen self-assigned this Sep 1, 2026
@Patrick-Erichsen
Patrick-Erichsen merged commit 73f24c2 into openclaw:main Sep 1, 2026
284 checks passed
@Patrick-Erichsen

Copy link
Copy Markdown
Contributor

Merged via squash.

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

Labels

agents Agent runtime and tooling P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. proof: telegram-e2e This PR needs Telegram Test Server proof with the repository E2E skill. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. size: XS 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.

3 participants