feat(codex): reconstruct precise compact sub-sessions from response telemetry#1965
feat(codex): reconstruct precise compact sub-sessions from response telemetry#1965konard wants to merge 8 commits into
Conversation
Adding .gitkeep for PR creation (default mode). This file will be removed when the task is complete. Issue: #1961
--tool codex?
Working session summaryDone. PR 1965 is updated, pushed, marked ready, and CI is green: Implemented:
Verification:
Note: GitHub accepted the raw case-study log, but warned it is 51.38 MB, slightly above the 50 MB recommendation. It is included because the issue explicitly requested preserving the related logs/data. Official Codex references used are in the PR body: JSONL usage, config, and compaction behavior. This summary was automatically extracted from the AI working session output. |
🤖 Solution Draft LogThis log file contains the complete execution trace of the AI solution draft process. 💰 Cost estimation:
📊 Context and tokens usage:GPT-5.5: (6 sub-sessions)
Sub-session values are estimates from observed compact events; the Total line remains exact. Total: (587.2K + 19.1M cached) input tokens, 65.0K output tokens, $27.870094 cost 🤖 Models used:
📎 Log file uploaded as Repository (80111KB)Now working session is ended, feel free to review and add any feedback on the solution draft. |
✅ Ready to mergeThis pull request is now ready to be merged:
Monitored by hive-mind with --auto-restart-until-mergeable flag |
This reverts commit d57afaf.
|
Can we rethink the issue and redo the analysis? Can we use byte encoding calculation to estimate sub-sessions split by the content we see in the logs? What are the best practices here? What do people do in the internet for that task? Can we report issue to the codex GitHub? I don't want features to be removed, I want them to be made precise as possible. Using all tools and approaches we have at our disposal. |
|
🤖 AI Work Session Started Starting automated work session at 2026-06-21T21:27:46.247Z The PR has been converted to draft mode while work is in progress. This comment marks the beginning of an AI work session. Please wait for the session to finish, and provide your feedback. |
…elemetry Restore the Codex compaction sub-session split (Issue #1961) but make it precise instead of fabricated. The previous even-split of the cumulative turn.completed usage produced the suspicious 'exactly 150K' first row. Now, when Codex runs with debug telemetry, each API request emits a codex.sse_event response.completed line whose input_token_count is the full restored context for that request. Bucketing those per-request snapshots between successful /responses/compact boundaries recovers the genuine peak context reached inside each sub-session plus the output generated in that window. These are measured values (estimated: false). When telemetry is absent, subSessions stay empty and the unsplit-compaction notice is shown. Extract the debug-telemetry parsing into src/codex.diagnostics.lib.mjs to keep codex.lib.mjs under the max-lines limit.
…uction Replace the prior 'remove the split' narrative with the precise-reconstruction approach: document the measured per-sub-session peaks (137,188 / 120,553) recovered by bucketing per-request response.completed telemetry around the compact boundary, the researched best practices (ccusage-style diffing, o200k_base tokenization as fallback only), and a drafted upstream Codex issue asking for per-request/per-compaction usage in the JSON stream.
Working session summaryThe CI run is still in progress (sequential runs). I'll wait for the background monitor to notify me when the latest commit's run completes. Let me summarize the current state while waiting: StatusAll implementation work for issue #1961 is complete and pushed. The approach was reversed per your directive — the feature is now restored and made precise, not removed: What changed:
Validated on the real 54 MB log — produces measured peaks 137,188 (pre-compact, 28 reqs) / 120,553 (post-compact, 116 reqs). Neither is the fabricated Verification done:
Once CI goes green I'll mark PR #1965 ready ( I'll be notified when the CI run completes. This summary was automatically extracted from the AI working session output. |
🤖 Solution Draft LogThis log file contains the complete execution trace of the AI solution draft process. 💰 Cost estimation:
📊 Context and tokens usage:Claude Opus 4.8: (2 sub-sessions)
Total: (61.7K new + 373.0K cache writes + 8.6M cache reads) input tokens, 123.8K output tokens, $10.968501 cost Claude Haiku 4.5:
Total: 555.7K input tokens, 15.4K output tokens, $0.752867 cost 🤖 Models used:
📎 Log file uploaded as Gist (4990KB)Now working session is ended, feel free to review and add any feedback on the solution draft. |
✅ Ready to mergeThis pull request is now ready to be merged:
Monitored by hive-mind with --auto-restart-until-mergeable flag |
Fixes #1961.
Summary
The referenced Codex run really did compact once: the log has
codex.conversation_startswithcontext_window=200000,auto_compact_token_limit=150000, and one successful/responses/compactrequest.The split Hive Mind first displayed was not real telemetry — it estimated
compact_count + 1rows by dividing the cumulativeturn.completedusage evenly and pinning the first row to the configured threshold, which is why the first row was exactly150K.This PR does not remove the feature (the issue owner asked for it to be "made precise as possible", not removed). The same log contains the data to reconstruct the split precisely: with debug telemetry on, Codex emits a
codex.sse_event response.completedline per API request whoseinput_token_countis the full restored context for that request. Bucketing those per-request snapshots around the/responses/compactboundary recovers the genuine peak context per sub-session and the output generated in each window. These are measured values (estimated: false), so no~prefix.For the triggering run, the reconstruction (validated end-to-end against the full 54 MB log) yields:
Neither row is
150K. The fabricated150Kwas the configured threshold; the real pre-compact peak (137,188) sits just below the 150,000 auto-compact limit — exactly what you'd expect for the request that trips auto-compaction.Root Cause
Codex
turn.completedusage is cumulative for the whole run, so it cannot be split on its own — the old parser split it anyway (count compact events →compact_count + 1rows → fill earlier rows withautoCompactTokenLimit→ split values by estimated weights), making an estimate look measured. The precise per-request data exists only incodex_otel.log_onlydebug telemetry, not in the stablecodex exec --jsoncontract (see upstream report below).Changes
codex.sse_event response.completedline intotokenUsage.diagnosticResponses(input/cached/output/reasoning per request)./responses/compactboundaries and taking the peakinput_token_countper window as the measured context fullness, plus summed output (buildCodexSubSessionsFromDiagnostics).~prefix, with a one-line provenance note; keep the exactturn.completedTotal line untouched.src/codex.diagnostics.lib.mjsto keepsrc/codex.lib.mjswithin the max-lines lint rule.docs/case-studies/issue-1961/and added a drafted upstream Codex issue (research/upstream-codex-issue-draft.md).Rendered result
Best practices researched
ccusageapproach for per-turn usage); we bucket per-request snapshots by compact boundary instead.o200k_base(gpt-tokenizer/tiktoken), not the inaccurate ~4-chars/token heuristic — reserved as a sanity-check fallback only, never to fabricate a split.~prefix + provenance note).Upstream
Draft report asking Codex to surface per-request / per-compaction usage in the JSON stream is in
docs/case-studies/issue-1961/research/upstream-codex-issue-draft.md, cross-referencing openai/codex#17539. Filing is left for maintainer confirmation (outward-facing action).Tests
node tests/test-codex-support.mjs(40 passed) — measured two-window reconstruction, unsplit-compaction notice, stderr-stream path, measured-row note.node tests/test-budget-stats.mjs,node tests/test-budget-stats-issue-1501.mjsnpm run lint(clean), end-to-end validation against the full 54 MB log producing peaks137188/120553.Note: GitHub accepted the committed raw log but warned
formal-ai-pr-471-solution-draft-log.txtis ~51 MB, just over GitHub's recommended 50 MB; it is included because the issue explicitly requested preserving the related logs/data.