fix: scope context remaining to body window (#29665)

## Why

With `model_auto_compact_token_limit_scope = "body_after_prefix"`, the
persistent prefix should not count against the active body window.
`get_context_remaining` and the token-budget reminder should report the
same usable body-after-prefix window that auto-compaction uses, rather
than the total token count since the session began.

This is stacked on #29664 so the mechanical move from `turn.rs` is
isolated from the behavior fix.

## What

- Extends `ContextWindowTokenStatus` with `context_remaining_tokens`.
- Updates `get_context_remaining` to use the shared context-window
accounting.
- Adds integration coverage for body-after-prefix reminder timing and
`get_context_remaining` output.

## Testing

- `just test -p codex-core body_after_prefix_window`
- `just test -p codex-core auto_compact_body_after_prefix`
- `just fix -p codex-core`
This commit is contained in:
Michael Bolin authored and GitHub committed 2026-06-23 23:08:54 +00:00
1 parent 4dde907d27
commit 77e7ce1374
6 files changed
+204 -45

No files matched your search

@@ -75,19 +75,15 @@ impl ToolExecutor<ToolInvocation> for GetContextRemainingHandler {
));
}
let Some(model_context_window) = invocation.turn.model_context_window() else {
return Ok(boxed_tool_output(GetContextRemainingOutput::new(
/*tokens_left*/ None,
)));
};
let active_context_tokens = invocation.session.get_total_token_usage().await.max(0);
let tokens_left = model_context_window
.saturating_sub(active_context_tokens)
.max(0);
let token_status = crate::session::context_window::context_window_token_status(
invocation.session.as_ref(),
invocation.turn.as_ref(),
)
.await;
Ok(boxed_tool_output(GetContextRemainingOutput::new(Some(
tokens_left,
))))
Ok(boxed_tool_output(GetContextRemainingOutput::new(
token_status.tokens_until_compaction,
)))
})
}
}