Skip to content

feat(codex): surface pre-turn projection accounting (#80765)#80778

Open
aiZKP wants to merge 1 commit into
openclaw:mainfrom
aiZKP:fix/codex-projection-accounting-80765
Open

feat(codex): surface pre-turn projection accounting (#80765)#80778
aiZKP wants to merge 1 commit into
openclaw:mainfrom
aiZKP:fix/codex-projection-accounting-80765

Conversation

@aiZKP

@aiZKP aiZKP commented May 11, 2026

Copy link
Copy Markdown

Summary

Closes #80765.

Codex's context-engine projection previously sized the rendered prompt with the
generic 4 chars/token heuristic and exposed nothing about that estimate
downstream. Status/LCM diagnostics could not separate frontier tokens
selected by the context engine, rendered Codex projection chars/tokens
before send, and provider-observed usage after the turn.

This PR adds a small pre-turn accounting snapshot to the projection and routes
it into agent telemetry:

  • projectContextEngineAssemblyForCodex now returns a stats block:
    • projectedPromptChars — length of the rendered Codex prompt
    • promptTokens — tokenizer-backed when supplied, heuristic otherwise
    • accounting: "estimated" | "exact" — explicit marker
    • capChars — active rendered-context cap (currently 24_000)
    • reserveTokens — surfaced when the caller routes the configured
      agents.defaults.compaction.reserveTokens /
      reserveTokensFloor through
  • An optional tokenize?: (text: string) => number | undefined parameter
    lets a future Codex app-server / provider tokenizer flip the marker to
    exact without changing call sites. Throwing or non-finite returns fall
    back to the heuristic.
  • run-attempt.ts resolves agents.defaults.compaction.reserveTokens
    (falling back to reserveTokensFloor) and emits a new
    codex_app_server.context_projection agent event before turn/start
    on both the context-engine and mirrored-history projection paths.

Existing behavior (24k char cap, prompt rendering, duplicate trailing-prompt
trim, developer-instruction addition, prePromptMessageCount) is unchanged.

Acceptance criteria

  • Native Codex projection reports pre-turn exact tokens when a tokenizer
    is supplied; otherwise marks accounting as estimated.
  • Diagnostics can distinguish:
    • LCM/frontier tokens selected by the context engine
      (frontierTokens on the emitted event, equal to contextTokenBudget)
    • rendered Codex projection chars/tokens before send
      (projectedPromptChars / promptTokens / accounting)
    • provider-observed prompt/input tokens after the turn
      (existing afterTurn runtimeContext.lastCallUsage / promptCache)
  • Tests cover the estimate-vs-exact marker and ensure configured reserve
    fields surface through projection stats.

Files touched

File Lines Purpose
extensions/codex/src/app-server/context-engine-projection.ts +95 Stats type, tokenizer seam, accounting marker
extensions/codex/src/app-server/run-attempt.ts +43 Reserve resolver, projection event emit
extensions/codex/src/app-server/context-engine-projection.test.ts +79 / -1 5 new tests for stats / marker / reserve

No SDK contract, no public manifest, no docs/changelog surface changed.

Test plan

  • pnpm test extensions/codex/src/app-server/context-engine-projection.test.ts10 passed (5 new + 5 existing)
  • pnpm test extensions/codex/src/app-server/run-attempt.context-engine.test.ts6 passed
  • pnpm check:changed (extension prod + extension test lanes) — typecheck, oxlint, format, runtime sidecar guard, import-cycle check all green
  • One unrelated test (run-attempt.test.ts > does not expose OpenClaw Tool Search controls through Codex dynamic tools) times out — verified to fail identically on main without these changes, so it is pre-existing and unrelated to this PR.

Notes for reviewers

  • The projection cap (MAX_RENDERED_CONTEXT_CHARS = 24_000) is intentionally
    unchanged here. Making it budget-aware via contextTokenBudget /
    reserveTokens is tracked by fix(codex): scale context engine projection #80761; this PR is the accounting
    follow-up only.
  • The new tokenize parameter is a no-op until a Codex/provider tokenizer
    is wired in. The acceptance criterion ("exact when the runtime/tokenizer
    surface supports it") is satisfied by the seam plus the explicit
    estimated marker; no behavior change for current callers.
  • The emitted event uses Record<string, unknown> — consumers that already
    subscribe to onAgentEvent see a new stream value but the existing
    envelope shape is preserved.

Refs: #80765

Adds a `stats` block to the Codex context-engine projection so callers can
distinguish LCM/frontier sizing from the rendered Codex prompt and from
post-turn provider-observed usage. The block carries `projectedPromptChars`,
`promptTokens`, an `accounting: "estimated" | "exact"` marker, the active
`capChars`, and (when routed through) the configured compaction
`reserveTokens` knob.

The projection accepts an optional `tokenize` callback so a provider/runtime
tokenizer can flip stats to `exact` when available; without one the existing
4-chars/token heuristic is used and accounting is explicitly marked
`estimated`. The Codex app-server run-attempt now resolves
`agents.defaults.compaction.reserveTokens` (falling back to
`reserveTokensFloor`) and emits a `codex_app_server.context_projection`
telemetry event alongside the existing post-turn usage signals.

Closes openclaw#80765
@openclaw-barnacle openclaw-barnacle Bot added extensions: codex size: M triage: needs-real-behavior-proof Candidate: external PR needs after-fix proof from a real setup. labels May 11, 2026
@clawsweeper

clawsweeper Bot commented May 11, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs real behavior proof before merge. Reviewed July 3, 2026, 11:49 PM ET / 03:49 UTC.

Summary
The PR adds Codex context projection stats, tokenizer-backed exact/estimated accounting, reserve reporting, tests, and a pre-turn context-projection agent event.

PR surface: Source +138, Tests +78. Total +216 across 3 files.

Reproducibility: yes. Source inspection shows current main still lacks the requested pre-turn projection stats/event, and PR-head source shows stale cap/reserve behavior without needing a live turn.

Review metrics: 1 noteworthy metric.

  • Diagnostic Event Stream: 1 added. The new codex_app_server.context_projection stream is the observable surface whose cap/reserve fields and runtime proof matter before merge.

Root-cause cluster
Relationship: fixed_by_candidate
Canonical: #80765
Summary: This PR is the open candidate fix for the canonical Codex pre-turn projection-accounting issue; the merged projection-cap work is predecessor context with partial overlap.

Members:

Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything.

Merge readiness
Overall: 🧂 unranked krab
Proof: 🧂 unranked krab
Patch quality: 🦐 gold shrimp
Result: blocked until real behavior proof from a real setup is added.

Overall follows the weaker of proof and patch quality, so missing proof can cap an otherwise strong patch.

Rank-up moves:

  • Refresh the branch against current main and reuse the active projection cap/reserve helpers.
  • [P1] Add redacted terminal logs, copied live output, or a linked artifact showing the new context-projection event after the patch.
  • After adding proof, update the PR body; if review does not rerun, ask a maintainer to comment @clawsweeper re-review.

Proof guidance:

  • [P1] Needs real behavior proof before merge: Only unit/check validation is supplied; before merge, the contributor needs redacted terminal logs, copied live output, or a linked artifact showing the new event from a real Codex app-server run, then should update the PR body to trigger re-review or ask for @clawsweeper re-review.

Risk before merge

  • [P1] The PR branch is currently CONFLICTING/DIRTY against main, so it cannot be merged as submitted.
  • [P1] If refreshed mechanically, the new telemetry can misrepresent session context because PR-head capChars and reserveTokens do not come from the active current-main projection helpers.
  • [P1] No redacted real Codex app-server output proves that the new codex_app_server.context_projection event is emitted after the patch.

Maintainer options:

  1. Refresh Accounting Against Main (recommended)
    Rebase or replace the branch so capChars and reserve data come from the same current-main helpers that render the prompt, with focused tests updated accordingly.
  2. Hold For Runtime Event Proof
    Keep the PR open but blocked until redacted terminal logs, copied live output, or a linked artifact shows the new event from a real Codex app-server run.
  3. Pause Or Replace The Branch
    If the contributor cannot refresh and prove this branch, maintainers should leave the linked issue open and use a narrow replacement implementation instead.

Next step before merge

  • [P1] The PR needs maintainer/contributor refresh and real behavior proof; automation cannot supply the contributor's real-run evidence for them.

Security
Cleared: The diff changes Codex plugin telemetry/accounting code and tests only; no dependency, workflow, credential, secret-handling, or downloaded-code path is changed.

Review findings

  • [P2] Report the active projection cap — extensions/codex/src/app-server/context-engine-projection.ts:122
  • [P2] Use the shared reserve resolver — extensions/codex/src/app-server/run-attempt.ts:2212-2223
Review details

Best possible solution:

Refresh or replace the branch so telemetry is computed from current main's active projection cap/reserve path, then add redacted real Codex app-server output showing the event before merge.

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

Yes. Source inspection shows current main still lacks the requested pre-turn projection stats/event, and PR-head source shows stale cap/reserve behavior without needing a live turn.

Is this the best way to solve the issue?

No as submitted. The Codex extension telemetry seam is the right layer, but the branch must reuse current main's active cap/reserve helpers and include real event-output proof before it is the best fix.

Full review comments:

  • [P2] Report the active projection cap — extensions/codex/src/app-server/context-engine-projection.ts:122
    Current main passes a budget-aware maxRenderedContextChars into projection, but this stats block always emits the old constant. Large-window Codex sessions would get false diagnostics unless capChars is the same cap used to render the prompt.
    Confidence: 0.91
  • [P2] Use the shared reserve resolver — extensions/codex/src/app-server/run-attempt.ts:2212-2223
    This helper returns raw reserveTokens or reserveTokensFloor, while current main sizes projection with resolveCodexContextEngineProjectionReserveTokens, including default floor and effective reserve rules. Route that shared value into stats so diagnostics match the prompt that was actually rendered.
    Confidence: 0.86

Overall correctness: patch is incorrect
Overall confidence: 0.88

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against 9d68f877ac3e.

Label changes

Label justifications:

  • P2: This is a bounded Codex diagnostics/accounting improvement with limited blast radius, not an urgent runtime outage.
  • merge-risk: 🚨 session-state: The diff changes context projection telemetry, and stale cap/reserve accounting can misrepresent what context was actually projected for a session turn.
  • rating: 🧂 unranked krab: Overall readiness is 🧂 unranked krab; proof is 🧂 unranked krab and patch quality is 🦐 gold shrimp.
  • status: 📣 needs proof: The PR needs real behavior proof before ClawSweeper can clear the contributor ask. Needs real behavior proof before merge: Only unit/check validation is supplied; before merge, the contributor needs redacted terminal logs, copied live output, or a linked artifact showing the new event from a real Codex app-server run, then should update the PR body to trigger re-review or ask for @clawsweeper re-review.
Evidence reviewed

PR surface:

Source +138, Tests +78. Total +216 across 3 files.

View PR surface stats
Area Files Added Removed Net
Source 2 138 0 +138
Tests 1 79 1 +78
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 3 217 1 +216

What I checked:

Likely related people:

  • 100yenadmin: Opened the canonical accounting follow-up and authored the merged projection-scaling work that current main now uses for cap/reserve behavior. (role: predecessor fix author and reporter; confidence: high; commits: f7ab8c26b1e7, 61c9550c70eb; files: extensions/codex/src/app-server/context-engine-projection.ts, extensions/codex/src/app-server/run-attempt.ts, extensions/codex/src/app-server/run-attempt.context-engine.test.ts)
  • jalehman: Contributed several branch commits to the merged projection-scaling PR and posted the squash-merge provenance for that predecessor work. (role: feature history contributor and merger; confidence: high; commits: 33fa4665ffdd, dec5a37035b2, af605c8e6bbd; files: extensions/codex/src/app-server/context-engine-projection.ts, extensions/codex/src/app-server/run-attempt.ts)
  • vincentkoc: Recent main history for the same projection and run-attempt files includes prompt-boundary and hook-projection fixes by this contributor. (role: recent adjacent area contributor; confidence: medium; commits: 88ad407be234, 3f166b1f6411, 7e80bb8abf26; files: extensions/codex/src/app-server/context-engine-projection.ts, extensions/codex/src/app-server/run-attempt.ts, extensions/codex/src/app-server/context-engine-projection.test.ts)
What the crustacean ranks mean
  • 🦀 challenger crab: rare, exceptional readiness with strong proof, clean implementation, and convincing validation.
  • 🦞 diamond lobster: very strong readiness with only minor maintainer review expected.
  • 🐚 platinum hermit: good normal PR, likely mergeable with ordinary maintainer review.
  • 🦐 gold shrimp: useful signal, but proof or patch confidence is still limited.
  • 🦪 silver shellfish: thin signal; proof, validation, or implementation needs work.
  • 🧂 unranked krab: not merge-ready because proof is missing/unusable or there are serious correctness or safety concerns.
  • 🌊 off-meta tidepool: rating does not apply to this item.

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.

How this review workflow works
  • 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.

@openclaw-barnacle

Copy link
Copy Markdown

This pull request has been automatically marked as stale due to inactivity.
Please add updates or it will be closed.

@openclaw-barnacle openclaw-barnacle Bot added the stale Marked as stale due to inactivity label May 26, 2026
@clawsweeper clawsweeper Bot added the rating: 🌊 off-meta tidepool PR readiness rating does not apply to this item. label May 26, 2026
@clawsweeper

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

@openclaw-barnacle openclaw-barnacle Bot removed the stale Marked as stale due to inactivity label May 27, 2026
@clawsweeper clawsweeper Bot added 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. P2 Normal backlog priority with limited blast radius. and removed rating: 🌊 off-meta tidepool PR readiness rating does not apply to this item. labels May 27, 2026
@clawsweeper

clawsweeper Bot commented Jun 4, 2026

Copy link
Copy Markdown
Contributor

@aiZKP thanks for the PR. ClawSweeper is still waiting on real behavior proof before this can move forward.

Useful proof can be a screenshot, short video, terminal output, copied live output, linked artifact, or redacted logs that show the changed behavior after the fix. Please redact private tokens, phone numbers, private endpoints, customer data, and anything else sensitive.

Once proof is added to the PR body or a comment, ClawSweeper or a maintainer can re-check it.

@clawsweeper

clawsweeper Bot commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

@aiZKP thanks for the PR. ClawSweeper is still waiting on real behavior proof before this can move forward.

Useful proof can be a screenshot, short video, terminal output, copied live output, linked artifact, or redacted logs that show the changed behavior after the fix. Please redact private tokens, phone numbers, private endpoints, customer data, and anything else sensitive.

Once proof is added to the PR body or a comment, ClawSweeper or a maintainer can re-check it.

@clawsweeper clawsweeper Bot added the merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. label Jun 19, 2026
@openclaw-barnacle openclaw-barnacle Bot added the triage: needs-pr-context Candidate: external PR body lacks required problem context or evidence. label Jun 19, 2026
@clawsweeper

clawsweeper Bot commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

@aiZKP thanks for the PR. ClawSweeper is still waiting on real behavior proof before this can move forward.

Useful proof can be a screenshot, short video, terminal output, copied live output, linked artifact, or redacted logs that show the changed behavior after the fix. Please redact private tokens, phone numbers, private endpoints, customer data, and anything else sensitive.

Once proof is added to the PR body or a comment, ClawSweeper or a maintainer can re-check it.

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

Labels

extensions: codex merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. P2 Normal backlog priority with limited blast radius. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. size: M status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. triage: needs-pr-context Candidate: external PR body lacks required problem context or evidence. triage: needs-real-behavior-proof Candidate: external PR needs after-fix proof from a real setup.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Codex context-engine projection lacks exact pre-turn token accounting

1 participant