use super::*;
#[test]
fn get_context_remaining_reports_enforced_not_percentage_ceiling() {
use super::ResponsesBudgetState;
let state = ResponsesBudgetState::new(
Some(32_768),
80,
resolve_output_allowance(None, Some(Cognition::Meticulous)),
None,
None,
None,
);
assert_eq!(state.actionable_input_budget(), Some(16_768));
assert_eq!(
num_ctx_input_ceiling(state.num_ctx(), 80, None),
Some(26_214)
);
assert_ne!(state.actionable_input_budget(), Some(26_214));
let report = crate::agentic::budget::render_context_budget(
0,
state.actionable_input_budget(),
state.num_ctx(),
state.input_ceiling_pct(),
15,
);
assert!(report.contains("ceiling of ~16768"), "{report}");
assert!(
!report.contains("26214"),
"must not advertise the un-reserved ceiling: {report}"
);
}
#[test]
fn get_context_remaining_agrees_with_dispatch_across_budget_shapes() {
use super::ResponsesBudgetState;
let est = crate::tokens::TokenEstimation::default();
let input = [serde_json::json!({"role": "user", "content": "do the thing"})];
let big_instructions = "SYSTEM POLICY. ".repeat(200); let wire_tools = [
serde_json::json!({
"type": "function",
"name": "read_file",
"description": "read a file in pages",
"parameters": {"type": "object", "properties": {
"path": {"type": "string"}, "offset": {"type": "integer"}
}, "required": ["path"]}
}),
serde_json::json!({
"type": "function",
"name": "run_command",
"description": "run a shell command in the workspace",
"parameters": {"type": "object", "properties": {
"command": {"type": "string"}
}, "required": ["command"]}
}),
];
let agrees = |label: &str,
state: &ResponsesBudgetState,
instructions: Option<&str>,
tools: Option<&[serde_json::Value]>|
-> usize {
let used =
crate::agentic::estimate_responses_request_tokens(instructions, &input, tools, est);
let ceiling = state
.actionable_input_budget()
.unwrap_or_else(|| panic!("{label}: the actionable budget must bind"));
let expected = ceiling.saturating_sub(used);
let report = crate::agentic::budget::render_context_budget(
used,
state.actionable_input_budget(),
state.num_ctx(),
state.input_ceiling_pct(),
15,
);
assert!(
report.contains(&format!("{expected} tokens remaining")),
"{label}: self-read remaining must equal actionable({ceiling}) − \
wire_estimate({used}) = {expected}; got: {report}",
);
used
};
let s = ResponsesBudgetState::new(Some(32_768), 80, None, None, None, None);
assert_eq!(s.actionable_input_budget(), Some(26_214));
agrees("only num_ctx", &s, None, None);
let s = ResponsesBudgetState::new(None, 80, None, None, Some(8_000), None);
assert_eq!(s.actionable_input_budget(), Some(8_000));
assert_eq!(
s.learned_hard_ceiling(),
None,
"no window → no hard ceiling"
);
agrees("only safe_context", &s, None, None);
let s = ResponsesBudgetState::new(Some(32_768), 80, None, Some(12_000), Some(20_000), None);
assert_eq!(s.actionable_input_budget(), Some(20_000));
agrees("max_ok smaller", &s, None, None);
let s = ResponsesBudgetState::new(
Some(32_768),
80,
resolve_output_allowance(None, Some(Cognition::Meticulous)),
None,
None,
Some(6_000),
);
assert_eq!(s.actionable_input_budget(), Some(6_000));
assert_eq!(s.learned_hard_ceiling(), Some(16_768));
assert_ne!(s.learned_hard_ceiling(), s.actionable_input_budget());
agrees("mid_loop_trim smaller", &s, None, None);
let s = ResponsesBudgetState::new(
Some(32_768),
80,
resolve_output_allowance(None, Some(Cognition::Meticulous)),
None,
None,
None,
);
assert_eq!(s.actionable_input_budget(), Some(16_768));
agrees("cognition reserve", &s, None, None);
let s = ResponsesBudgetState::new(Some(65_536), 80, None, None, None, None);
let with_instr = agrees(
"instructions substantial",
&s,
Some(&big_instructions),
None,
);
let without_instr = crate::agentic::estimate_responses_request_tokens(None, &input, None, est);
assert!(
with_instr > without_instr,
"instructions must raise the wire estimate (was omitted pre-fix)",
);
let with_tools = agrees("tools enabled", &s, None, Some(&wire_tools));
let without_tools = agrees("tools disabled", &s, None, None);
assert!(
with_tools > without_tools,
"enabled wire tools must raise the estimate over tools-disabled",
);
let mut s = ResponsesBudgetState::new(
Some(65_536),
80,
resolve_output_allowance(None, Some(Cognition::Thoughtful)),
Some(40_000),
Some(40_000),
Some(30_000),
);
s.recover_from_cw400(9_000);
assert_eq!(s.actionable_input_budget(), Some(9_000));
assert!(s.actionable_input_budget().unwrap() < 30_000);
agrees(
"learned ceiling tighter than all",
&s,
Some(&big_instructions),
Some(&wire_tools),
);
let s = ResponsesBudgetState::new(Some(32_768), 80, None, Some(12_000), None, None);
assert_eq!(
s.actionable_input_budget(),
Some(12_000),
"max_ok binds alone"
);
agrees("max_ok binds", &s, None, None);
let s = ResponsesBudgetState::new(
None,
80,
resolve_output_allowance(None, Some(Cognition::Meticulous)),
None,
None,
None,
);
assert_eq!(
s.actionable_input_budget(),
None,
"an unknown window stays unknown — no fabricated ceiling"
);
let unknown = crate::agentic::budget::render_context_budget(
crate::agentic::estimate_responses_request_tokens(None, &input, None, est),
s.actionable_input_budget(),
s.num_ctx(),
s.input_ceiling_pct(),
15,
);
assert!(
unknown.contains("No input-token ceiling is configured"),
"ceiling-less self-read must say so, not report remaining: {unknown}",
);
let s = ResponsesBudgetState::new(
Some(16_000),
80,
resolve_output_allowance(None, Some(Cognition::Meticulous)),
None,
None,
None,
);
assert_eq!(
s.actionable_input_budget(),
Some(0),
"window (16,000) − reserve (16,000) = 0 is an authoritative zero, not None",
);
let zero = crate::agentic::budget::render_context_budget(
crate::agentic::estimate_responses_request_tokens(None, &input, None, est),
s.actionable_input_budget(),
s.num_ctx(),
s.input_ceiling_pct(),
15,
);
assert!(
zero.contains("0 tokens remaining") && !zero.contains("No input-token ceiling"),
"an authoritative zero renders 0 remaining, not ceiling-less: {zero}",
);
let render0 = |ceiling: Option<usize>, num_ctx: Option<u32>| {
crate::agentic::budget::render_context_budget(0, ceiling, num_ctx, 80, 15)
};
let s = ResponsesBudgetState::new(None, 80, None, None, Some(8_000), None);
let old2 = render0(s.learned_hard_ceiling(), s.num_ctx());
let new2 = render0(s.actionable_input_budget(), s.num_ctx());
assert!(
old2.contains("No input-token ceiling is configured"),
"item 2 pre-fix source wrongly reports no ceiling: {old2}"
);
assert!(
new2.contains("8000 tokens remaining"),
"item 2 fixed source reports the enforced 8,000: {new2}"
);
assert_ne!(old2, new2, "item 2: the ceiling source changes the answer");
let s = ResponsesBudgetState::new(
Some(32_768),
80,
resolve_output_allowance(None, Some(Cognition::Meticulous)),
None,
None,
Some(6_000),
);
let old3 = render0(s.learned_hard_ceiling(), s.num_ctx());
let new3 = render0(s.actionable_input_budget(), s.num_ctx());
assert!(
old3.contains("16768 tokens remaining"),
"item 3 pre-fix over-advertises the hard ceiling: {old3}"
);
assert!(
new3.contains("6000 tokens remaining"),
"item 3 fixed source reports the 6,000 dispatch refuses at: {new3}"
);
assert_ne!(
old3, new3,
"item 3: the mid-loop trim is invisible to the pre-fix source"
);
let responses_used = crate::agentic::estimate_responses_request_tokens(
Some(&big_instructions),
&input,
Some(&wire_tools),
est,
);
let chat_tools = serde_json::json!([{
"type": "function",
"function": {"name": "run_command", "description": "run a shell command",
"parameters": {"type": "object",
"properties": {"command": {"type": "string"}}, "required": ["command"]}}
}]);
let chat_used = crate::agentic::trim::estimate_request_tokens(&input, Some(&chat_tools), est);
assert!(
responses_used > chat_used,
"item 5: the Responses-wire estimate (instructions + flat tools, {responses_used}) must \
exceed the pre-fix Chat-shaped estimate that omitted instructions ({chat_used})",
);
}