diff --git a/agent/codex_runtime.py b/agent/codex_runtime.py index 214ed013f57..664e4fbe9b1 100644 --- a/agent/codex_runtime.py +++ b/agent/codex_runtime.py @@ -279,6 +279,11 @@ def _record_codex_app_server_compaction( compressor, "compression_count", 0 ) + 1 compressor.last_compression_rough_tokens = approx_tokens or 0 + # The app server has already completed a real compaction boundary. Its + # usage update (when supplied) is therefore the same real-vs-real + # effectiveness verdict used by the normal compression path. + if hasattr(compressor, "_verify_compaction_cleared_threshold"): + compressor._verify_compaction_cleared_threshold = True if not getattr(turn, "token_usage_last", None): compressor.last_prompt_tokens = -1 compressor.last_completion_tokens = 0 diff --git a/tests/run_agent/test_codex_app_server_integration.py b/tests/run_agent/test_codex_app_server_integration.py index 802096fd0c8..c88d45c483a 100644 --- a/tests/run_agent/test_codex_app_server_integration.py +++ b/tests/run_agent/test_codex_app_server_integration.py @@ -143,6 +143,13 @@ class TestRunConversationCodexPath: turn_id="turn-compact-1", thread_id="thread-compact-1", compacted=True, + token_usage_last={ + "totalTokens": 300_000, + "inputTokens": 300_000, + "cachedInputTokens": 0, + "outputTokens": 0, + "reasoningOutputTokens": 0, + }, ) monkeypatch.setattr(CodexAppServerSession, "run_turn", fake_run_turn) @@ -157,10 +164,11 @@ class TestRunConversationCodexPath: assert result["completed"] is True assert agent.context_compressor.compression_count == 1 - # The app-server omitted usage, so the runtime consumes the pending - # post-compaction verdict instead of deferring preflight indefinitely. - assert agent.context_compressor.last_prompt_tokens == 0 + # A compacted turn with real usage is judged against that same real + # prompt count, exactly like a normal completed compression boundary. + assert agent.context_compressor.last_prompt_tokens == 300_000 assert agent.context_compressor.awaiting_real_usage_after_compression is False + assert agent.context_compressor._ineffective_compression_count == 1 assert events == [ ( "session:compress",