Skip to content

Attribute Codex sessions to their model from thread settings - #6478

Open
dalmasluca wants to merge 2 commits into
omacom:quattrofrom
dalmasluca:codex-model-attribution
Open

dalmasluca wants to merge 2 commits into
omacom:quattrofrom
dalmasluca:codex-model-attribution

Conversation

@dalmasluca

Copy link
Copy Markdown
Contributor

Summary

Fixes the generic codex model bucket in codex_usage_scanner.py by attributing native Codex session tokens to their real model.

Two issues caused tokens to fall into a catch-all codex bucket:

  1. Newer sessions record the model in thread_settings_applied (not turn_context), so the model was never picked up.
  2. Token counts logged before the first model event were assigned to the default codex model, even when the session later revealed its model.

Both are fixed: the scanner now reads thread_settings_applied.model, and a pre-pass seeds current_model with the first model a session records.

Verified against real sessions: the codex bucket disappears and all tokens land on the actual models (e.g. gpt-5.6-sol, gpt-5.6-terra, gpt-5.6-luna, gpt-5.4...).

Notes

  • Authored by opencode (an AI coding assistant) on behalf of the reporter.
  • This work was done by me (the assistant).

Tests

./test/shell.d/model-usage-codex-scanner-test.sh

Copilot AI balanced review requested due to automatic review settings August 1, 2026 09:36

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Attributes native Codex token usage to session models rather than the generic codex bucket.

Changes:

  • Pre-scans sessions for their first model event.
  • Handles newer thread_settings_applied events.

Tip

If you aren't ready for review, convert to a draft PR.
Click "Convert to draft" or run gh pr ready --undo.
Click "Ready for review" or run gh pr ready to reengage.

Suppressed comments (2)

shell/plugins/model-usage/scripts/codex_usage_scanner.py:212

  • thread_settings_applied exposes its model directly on the event payload, but this reads a nested thread_settings.model. For newer sessions that omit turn_context, the pre-pass therefore keeps current_model as codex, so the generic bucket this PR is intended to remove remains. Read payload.model first (retaining the nested lookup only as a compatibility fallback).
          if payload.get("type") == "thread_settings_applied":
            current_model = model_name((payload.get("thread_settings") or {}).get("model") or current_model)

shell/plugins/model-usage/scripts/codex_usage_scanner.py:234

  • The normal scan repeats the same incorrect nesting: native thread_settings_applied records put model directly in payload, not under payload.thread_settings. As written, later model events are ignored and token counts continue to use the seeded or generic model.
            if payload.get("type") == "thread_settings_applied":
              current_model = model_name((payload.get("thread_settings") or {}).get("model") or current_model)

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +197 to +201
try:
# A session may log token counts before its first model event. Read the
# first model the file records so those early counts land on the real
# model instead of a generic "codex" bucket.
with path.open(errors="replace") as handle:

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolved in acc7251. The new rollout-thread.jsonl fixture exercises exactly these two cases: a token_count precedes the first model event (line 49, so the pre-pass must skip token counts and keep scanning), and the only model event is an event_msg → thread_settings_applied with a direct model field (line 50, no turn_context at all). The asserts then check the early token count lands on gpt-thread and that no generic codex bucket is left behind. The same fixture drives the turn_context pre-pass path from the primary rollout, and the pi-router fixture asserts provider-strict filtering (47a7f0c).

Copilot AI review requested due to automatic review settings August 1, 2026 09:56

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

Copilot AI review requested due to automatic review settings August 1, 2026 10:29

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

@dalmasluca

Copy link
Copy Markdown
Contributor Author

Addressed the review comment plus a follow-up edge case (commit 47a7f0c):

  • pi sessions are now attributed to Codex strictly by provider == "openai-codex", dropping the api.startswith("openai-codex") fallback. A third-party router connected in pi agent that serves the same gpt models — even one presenting an openai-codex-style api — no longer lands on the Codex screen.
  • This matches the existing provider-only attribution for Kimi (kimi-coding) and Grok (xai).
  • Regression test: a pi session from third-party-router with model gpt-5.6-sol and api openai-codex-responses is ignored, and leaves no gpt-5.6-sol bucket. All scanner tests pass.

@dhh

dhh commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Heads up: #6603 just landed on quattro and restructured this whole area, so this PR needs a rework before it can merge.

The model-usage plugin is gone. It's been renamed to omarchy.agents (shell/plugins/agents/), and the panel is now strictly a display: it watches JSON records under ~/.local/state/omarchy/agents/usage/ and draws whatever appears there. All the in-plugin machinery this PR touches — providers/*.qml adapters and scripts/*_usage_scanner.py — no longer exists.

Usage collection now lives in standalone collectors in bin/. One omarchy-agent-usage-<agent> script per agent prints a complete, display-ready usage record (identity, tier, status, rate limits, today/week/all-time stats), and omarchy-agent-usage-update runs every collector it finds and writes the records atomically to the state directory.

The upside: adding an agent no longer touches the panel at all. Ship a collector that prints the record contract, optionally add an assets/<id>.svg mark (with an -light twin if it needs one for light surfaces), and the panel gains a tab on its own. See shell/plugins/agents/README.md for the contract and the claude/codex collectors in bin/ as reference implementations — #6604 (Cursor) is a good example of a provider PR in the new style.

If you'd like to update this PR to the new structure, rebase on quattro and reshape the scanner into a collector. Happy to answer questions here.

— Claude Fable 5, written on behalf of @dhh

Luca and others added 2 commits August 9, 2026 14:51
Pi/omp sessions were attributed to Codex when the api field merely
started with 'openai-codex'. A third-party router that serves the same
gpt models behind an openai-codex-style api was therefore charged to
this subscription and added its buckets to the Codex record.

Keep attribution strict to the provider, matching the codex scanner
guidance and the Claude collector fix (omacom#6655): only sessions whose
provider is exactly 'openai-codex' count.

Add a regression test where a third-party-router session using the
openai-codex-responses api must not land in the Codex record.
Sessions that never emit turn_context fall into a generic 'codex'
bucket. Newer sessions record the model in thread_settings_applied
instead (and omit turn_context), while a session may also log token
counts before its first model event.

Seed the model from the first model-bearing entry in a pre-pass, and
follow thread_settings_applied updates during the token scan, so those
turns land on the real model. Add a regression test where the model only
appears in thread_settings_applied and the first token count precedes
it.
Copilot AI review requested due to automatic review settings August 9, 2026 12:51
@dalmasluca
dalmasluca force-pushed the codex-model-attribution branch from 47a7f0c to 0fe3590 Compare August 9, 2026 12:51

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 2 changed files in this pull request and generated no new comments.

@omarchybot omarchybot added the bug Something isn't working label Sep 27, 2026
@omarchybot

Copy link
Copy Markdown
Collaborator

Reviewed at 0fe3590. The native-session problem this fixes does not reproduce on current Codex, and the new in-loop thread_settings_applied handler misattributes tokens in a case the base branch gets right, so this needs rethinking before it can land. Nothing was pushed to the branch.

What was checked. On a disposable Omarchy worker I ran the collector from quattro, from this branch's base, and from this head against three session files. In the shape this PR describes (no turn_context, a token count before thread_settings_applied) the base puts everything in codex and the head attributes it to the model, as intended. In the shape Codex 0.157.1 actually writes (turn_context before every turn's token counts, with thread_settings_applied followed by a new turn_context on a model switch) base and head give identical, correct results. Across 266 real 0.157.1 session files, no token count precedes a turn_context, and Codex's rollout/src/policy.rs persists TurnContext unconditionally, so I could not find a current version that produces the failing shape. agent-usage-codex-scanner-test.sh (8 passed) and ./test/cli both pass on the worker.

The defect. Codex emits thread_settings_applied when a turn is steered with new settings, and apply_steered in core/src/session/turn_input.rs says the active turn keeps its existing context; only the next turn uses the new model. For turn_context(A), thread_settings_applied(B), token_count, the base correctly attributes the count to A, and this head (bin/omarchy-agent-usage-codex:293-294) moves it to B. Reproduced on the worker: base {"gpt-6-astra":50,"gpt-5.6-sol":62}, head {"gpt-5.6-sol":112}.

The test. The new fixture only has one model, so the pre-pass alone satisfies both new assertions; removing the in-loop handler leaves them passing. It cannot catch the case above.

If you have sessions that lack turn_context, please say which Codex version wrote them and the sequence of event types (no contents needed). A proportionate fix would then use thread_settings_applied only for a file that has not yet shown a turn_context, leaving the turn context authoritative wherever it exists, with a fixture that switches models mid-turn. The branch also needs rebasing onto quattro: test/shell.d/agent-usage-codex-scanner-test.sh conflicts, and the collector has changed a lot there since.

On the earlier Copilot comment: the model is at payload.thread_settings.model, as this PR reads it, not payload.model. Every real event I checked, and Codex's own ThreadSettingsAppliedEvent, nests it.

The pi change (attributing by provider == "openai-codex" only) is a policy choice rather than a bug fix: pi writes api: "openai-codex-responses" both for its built-in Codex provider and for a custom provider using the same adapter, so dropping the api fallback excludes unrelated routers and also a custom provider that is in fact the user's Codex subscription. Whether to count those is for the maintainer. If it stays, the rg prefilter in scan_pi_sessions on quattro still matches on api and could drop that pattern too.

The second opinion was Codex Medium. It agreed that the native-session failure is not shown on current Codex and that the pi change is a policy choice (independence is not guaranteed, since it can read this session's work). It independently found the steered-turn misattribution and the vacuous assertion; I checked both against Codex's source and this branch and reproduced the first on the worker.

This is waiting on the author: evidence of the failing session shape, and a rebase. The pi policy question is waiting on the maintainer.

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

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants