Skip to content

test(openrouter): set cached-token fields in mock to stop intermittent MagicMock crash#2776

Merged
dcbouius merged 1 commit into
mainfrom
fix/openrouter-mock-cached-tokens
Jul 17, 2026
Merged

test(openrouter): set cached-token fields in mock to stop intermittent MagicMock crash#2776
dcbouius merged 1 commit into
mainfrom
fix/openrouter-mock-cached-tokens

Conversation

@dcbouius

Copy link
Copy Markdown
Contributor

test_null_content_recovers_on_retry failed intermittently on the test-api shard:

hindsight_api/metrics.py:591: TypeError: '>' not supported between instances of 'MagicMock' and 'int'

(if cached_input_tokens > 0:)

_make_chat_response set completion_tokens_details (added for #2378) but not the cached-token fields. So the cached-token extraction — openai_compatible_llm.py:948 (response_usage.cached_tokens) and the usage.prompt_tokens_details.cached_tokens path — read an auto-MagicMock and passed it to the metrics recorder. It only surfaced when the metrics path actually ran, which depends on telemetry state that leaks across pytest-xdist workers — so it presented as an intermittent, co-scheduling-dependent failure (passed on some runs, failed on others with identical code).

Fix: set usage.cached_tokens = 0 and usage.prompt_tokens_details = None so both extraction paths yield int 0.

Verified locally: both tests pass, and both extraction paths return int 0 (no MagicMock reaches the > 0 comparison).

Context: this was the lone remaining red on #2727 after the native-import (#2761) and judge-hardening (#2769) fixes — a pre-existing flake unrelated to that PR's dependency bumps.

🤖 Generated with Claude Code

…t MagicMock crash

test_null_content_recovers_on_retry failed intermittently on the test-api
shard with:

  hindsight_api/metrics.py:591: TypeError: '>' not supported between
  instances of 'MagicMock' and 'int'   (if cached_input_tokens > 0)

The mock in _make_chat_response set completion_tokens_details but not the
cached-token fields, so the cached-token extraction
(openai_compatible_llm.py:948 `response_usage.cached_tokens`, and the
prompt_tokens_details path) read an auto-MagicMock and passed it to the
metrics recorder. It only surfaced when the metrics path actually ran —
which depends on telemetry state that leaks across pytest-xdist workers —
so it presented as an intermittent, co-scheduling-dependent failure rather
than a deterministic one.

Set usage.cached_tokens = 0 and usage.prompt_tokens_details = None so both
extraction paths yield int 0. Verified: both tests pass and both paths
return int 0 (no MagicMock reaches the `> 0` comparison).
@dcbouius
dcbouius merged commit eca0fd5 into main Jul 17, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant