Skip to content

fix(codex): prefer native tokens for resume budget#83229

Closed
hansolo949 wants to merge 1 commit into
openclaw:mainfrom
hansolo949:fix/codex-context-budget-native-precedence
Closed

fix(codex): prefer native tokens for resume budget#83229
hansolo949 wants to merge 1 commit into
openclaw:mainfrom
hansolo949:fix/codex-context-budget-native-precedence

Conversation

@hansolo949

Copy link
Copy Markdown
Contributor

Summary

This is the narrow follow-up for the remaining Codex app-server resume-budget edge case. Upstream main already has the broader native rollout guard and dynamic tool-result cap; this patch only changes token precedence when both native rollout usage and mirrored OpenClaw session totals are present.

Real behavior proof

Behavior or issue addressed: OpenClaw can keep a high mirrored session token total after Codex app-server compaction. Current main reads native last_token_usage, but still evaluates the resume guard with max(sessionTokens, nativeTokens), so a stale mirrored total can rotate a healthy compacted native thread. This patch prefers nativeTokens ?? sessionTokens.

Real environment tested: Han local OpenClaw install on macOS, OpenClaw 2026.5.16-beta.3, Gateway running against the main agent with WebChat/Codex app-server bindings.

Exact steps or command run after this patch: Applied the same one-line runtime patch locally, restarted/checked OpenClaw, verified the installed runtime contains const tokenCount = nativeTokens ?? sessionTokens;, and ran the focused app-server regression lane in this rebased PR worktree.

Evidence after fix: Live terminal output from the local OpenClaw setup after the patch:

$ rg -n "const tokenCount = nativeTokens \\?\\? sessionTokens" /usr/local/lib/node_modules/openclaw/dist/run-attempt-DEhr_oag.js
/usr/local/lib/node_modules/openclaw/dist/run-attempt-DEhr_oag.js:2543:\tconst tokenCount = nativeTokens ?? sessionTokens;

$ openclaw health --json | jq '{ok,eventLoop:.eventLoop,telegram:.channels.telegram.connected}'
{
  "ok": true,
  "eventLoop": {
    "degraded": false,
    "reasons": [],
    "delayP99Ms": 22.8,
    "utilization": 0.021,
    "cpuCoreRatio": 0.022
  },
  "telegram": true
}

Observed result after fix: The local installed runtime now uses native Codex rollout token usage first, so a compacted native thread is not forced to rotate solely because OpenClaw mirrored session totals are stale. Gateway stayed healthy after the patch. The rebased PR regression also passed: Test Files 1 passed (1); Tests 7 passed | 151 skipped (158).

What was not tested: Full repository test suite and a live upstream CI WebChat session. The live local Gateway/runtime check and focused app-server regression were tested.

Supplemental validation

node_modules/.bin/oxfmt --check --threads=1 extensions/codex/src/app-server/run-attempt.ts extensions/codex/src/app-server/run-attempt.test.ts
git diff --check
tmp=$(mktemp); printf '%s\n' '["codex/src/app-server/run-attempt.test.ts"]' > "$tmp"
OPENCLAW_VITEST_INCLUDE_FILE="$tmp" OPENCLAW_VITEST_MAX_WORKERS=1 node scripts/run-vitest.mjs run --config test/vitest/vitest.extensions.config.ts --testNamePattern 'native rollout|stale mirrored'

Supplemental results: oxfmt passed, git diff whitespace passed, focused Vitest passed.

@openclaw-barnacle openclaw-barnacle Bot added extensions: codex size: S proof: supplied External PR includes structured after-fix real behavior proof. labels May 17, 2026
@clawsweeper

clawsweeper Bot commented May 17, 2026

Copy link
Copy Markdown
Contributor

Thanks for the context here. I did a careful shell check against current main, and this is already implemented.

Current main and the shipped v2026.6.6 release already cover the central Codex stale-token resume problem with the newer startup-binding guard, token-freshness handling, and context-engine-first compaction path. This branch targets the older pre-refactor location, is now conflicting, and is no longer the right landing candidate.

So I’m closing this as already implemented rather than keeping a duplicate issue open.

Review details

Best possible solution:

Keep the shipped v2026.6.6 startup-binding and context-engine-first compaction behavior as the canonical fix, and close this conflicting branch.

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

Not applicable as a current-main failure: the old stale mirrored-token resume case is now covered by current startup-binding freshness checks and regression tests. The pre-fix behavior is clear from the PR diff and previous review, but current main no longer needs this branch to solve the central case.

Is this the best way to solve the issue?

No, this PR is no longer the best way to solve the issue. The shipped current implementation handles stale mirrored totals at the session freshness and startup-binding boundary, while this branch edits the older run-attempt location and now conflicts with main.

Security review:

Security review cleared: No concrete security or supply-chain concern found in the two-file Codex runtime/test diff.

AGENTS.md: found and applied where relevant.

What I checked:

Likely related people:

  • hansolo949: Han authored this branch and previously landed the Codex app-server context-budget guard work in Guard Codex app-server context budgets #82981, which touched the same run-attempt surface. (role: prior context-budget contributor; confidence: high; commits: 084318b8c461; files: extensions/codex/src/app-server/run-attempt.ts, extensions/codex/src/app-server/run-attempt.test.ts)
  • cyrusaf: Cyrus authored the merged Codex usage fix that avoided treating cumulative app-server usage as current context, which is the dependency contract this cleanup depends on. (role: prior token-usage fix author; confidence: high; commits: 9a94194329de; files: extensions/codex/src/app-server/event-projector.ts, extensions/codex/src/app-server/event-projector.test.ts)
  • joshavant: Josh authored and merged the current context-engine-first Codex compaction implementation that supersedes the older resume-budget branch as the canonical path. (role: current compaction path owner; confidence: high; commits: a8d33f23a09d; files: src/agents/embedded-agent-runner/compact.queued.ts, src/agents/harness/compaction.ts, extensions/codex/src/app-server/compact.ts)
  • steipete: Git history shows Peter Steinberger as the heaviest contributor across the central Codex app-server run-attempt and startup-binding files, including the original app-server controls and module refactors. (role: feature owner and heavy area contributor; confidence: high; commits: 31a0b7bd42, 8d72aafdbb, 659bcc5e5b; files: extensions/codex/src/app-server/run-attempt.ts, extensions/codex/src/app-server/startup-binding.ts, extensions/codex/src/app-server/run-attempt.test.ts)
  • vincentkoc: Vincent recently worked on Codex app-server auth/startup and the v2026.6.6 release-tagged startup-binding tests that now cover the stale-token behavior. (role: recent adjacent contributor; confidence: medium; commits: 8c802aa68351, f1cc8f0cfc, 859eb06662; files: extensions/codex/src/app-server/run-attempt.ts, extensions/codex/src/app-server/startup-binding.test.ts)

Codex review notes: model internal, reasoning high; reviewed against f1b8827d20c8; fix evidence: release v2026.6.6, commit 8c802aa68351.

@clawsweeper clawsweeper Bot added P2 Normal backlog priority with limited blast radius. impact:session-state Session, memory, transcript, context, or agent state can drift or corrupt. labels May 17, 2026
@hansolo949

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

The Real behavior proof check is passing and the PR body includes live local runtime evidence plus the focused regression lane. Please refresh the durable review verdict against the current proof.

@clawsweeper

clawsweeper Bot commented May 22, 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: 🦪 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. and removed impact:session-state Session, memory, transcript, context, or agent state can drift or corrupt. labels May 22, 2026
@clawsweeper

clawsweeper Bot commented May 22, 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.

@clawsweeper

clawsweeper Bot commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

ClawSweeper applied the proposed close for this PR.

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

Labels

extensions: codex P2 Normal backlog priority with limited blast radius. proof: supplied External PR includes structured after-fix real behavior proof. rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. size: S status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant