use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct RunState {
#[serde(default, alias = "total_tool_calls")]
pub total_actions: usize,
#[serde(default, alias = "turn_tool_calls")]
pub turn_actions: usize,
#[serde(default, alias = "run_has_tool_calls")]
pub run_has_actions: bool,
pub reasoning_only_strikes: usize,
pub empty_response_strikes: usize,
pub nudge_count: usize,
#[serde(default)]
pub thinking_disabled_for_rest_of_run: bool,
#[serde(default)]
pub original_thinking_enabled: bool,
#[serde(default)]
pub truncation_strikes: usize,
#[serde(default)]
pub awaiting_human: bool,
}
impl RunState {
pub fn reset_for_new_run(&mut self) {
self.turn_actions = 0;
self.run_has_actions = false;
self.reasoning_only_strikes = 0;
self.empty_response_strikes = 0;
self.nudge_count = 0;
self.thinking_disabled_for_rest_of_run = false;
self.truncation_strikes = 0;
self.awaiting_human = false;
}
pub fn record_actions(&mut self, n: usize) {
self.turn_actions += n;
self.total_actions += n;
self.run_has_actions = true;
self.reasoning_only_strikes = 0;
self.empty_response_strikes = 0;
self.truncation_strikes = 0;
}
pub fn record_reasoning_only(&mut self) -> usize {
self.empty_response_strikes = 0;
self.reasoning_only_strikes += 1;
if self.reasoning_only_strikes >= 3 {
self.thinking_disabled_for_rest_of_run = true;
}
self.reasoning_only_strikes
}
pub fn record_empty_response(&mut self) -> usize {
self.reasoning_only_strikes = 0;
self.empty_response_strikes += 1;
self.empty_response_strikes
}
pub fn record_truncation(&mut self) -> usize {
self.truncation_strikes += 1;
self.truncation_strikes
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn run_state_default() {
let rs = RunState::default();
assert_eq!(rs.total_actions, 0);
assert_eq!(rs.turn_actions, 0);
assert!(!rs.run_has_actions);
assert_eq!(rs.reasoning_only_strikes, 0);
assert_eq!(rs.empty_response_strikes, 0);
assert_eq!(rs.nudge_count, 0);
}
#[test]
fn run_state_reset_for_new_run() {
let mut rs = RunState {
total_actions: 9,
turn_actions: 5,
run_has_actions: true,
reasoning_only_strikes: 2,
empty_response_strikes: 1,
nudge_count: 3,
thinking_disabled_for_rest_of_run: true,
original_thinking_enabled: true,
truncation_strikes: 4,
awaiting_human: true,
};
rs.reset_for_new_run();
assert_eq!(rs.total_actions, 9);
assert_eq!(rs.turn_actions, 0);
assert!(!rs.run_has_actions);
assert_eq!(rs.reasoning_only_strikes, 0);
assert_eq!(rs.empty_response_strikes, 0);
assert_eq!(rs.nudge_count, 0);
assert_eq!(rs.truncation_strikes, 0);
assert!(!rs.thinking_disabled_for_rest_of_run);
assert!(!rs.awaiting_human);
assert!(rs.original_thinking_enabled);
}
#[test]
fn run_state_record_actions() {
let mut rs = RunState {
reasoning_only_strikes: 2,
empty_response_strikes: 1,
..RunState::default()
};
rs.record_actions(3);
assert_eq!(rs.turn_actions, 3);
assert_eq!(rs.total_actions, 3);
assert!(rs.run_has_actions);
assert_eq!(rs.reasoning_only_strikes, 0); assert_eq!(rs.empty_response_strikes, 0); }
#[test]
fn run_state_record_actions_accumulates() {
let mut rs = RunState::default();
rs.record_actions(2);
rs.record_actions(3);
assert_eq!(rs.turn_actions, 5);
assert_eq!(rs.total_actions, 5);
assert!(rs.run_has_actions);
}
#[test]
fn run_state_record_reasoning_only() {
let mut rs = RunState {
empty_response_strikes: 2,
..RunState::default()
};
let strikes = rs.record_reasoning_only();
assert_eq!(strikes, 1);
assert_eq!(rs.reasoning_only_strikes, 1);
assert_eq!(rs.empty_response_strikes, 0); }
#[test]
fn run_state_record_reasoning_only_consecutive() {
let mut rs = RunState::default();
assert_eq!(rs.record_reasoning_only(), 1);
assert_eq!(rs.record_reasoning_only(), 2);
assert_eq!(rs.record_reasoning_only(), 3);
}
#[test]
fn run_state_record_empty_response() {
let mut rs = RunState {
reasoning_only_strikes: 2,
..RunState::default()
};
let strikes = rs.record_empty_response();
assert_eq!(strikes, 1);
assert_eq!(rs.empty_response_strikes, 1);
assert_eq!(rs.reasoning_only_strikes, 0); }
#[test]
fn run_state_record_empty_response_consecutive() {
let mut rs = RunState::default();
assert_eq!(rs.record_empty_response(), 1);
assert_eq!(rs.record_empty_response(), 2);
assert_eq!(rs.record_empty_response(), 3);
}
#[test]
fn run_state_branch_cross_reset() {
let mut rs = RunState::default();
rs.record_reasoning_only();
assert_eq!(rs.reasoning_only_strikes, 1);
rs.record_actions(2);
assert_eq!(rs.reasoning_only_strikes, 0);
assert_eq!(rs.turn_actions, 2);
let strikes = rs.record_reasoning_only();
assert_eq!(strikes, 1);
}
#[test]
fn run_state_empty_to_reasoning_reset() {
let mut rs = RunState::default();
rs.record_empty_response();
rs.record_empty_response();
assert_eq!(rs.empty_response_strikes, 2);
rs.record_reasoning_only();
assert_eq!(rs.empty_response_strikes, 0);
assert_eq!(rs.reasoning_only_strikes, 1);
}
#[test]
fn run_state_thinking_disabled_default() {
let rs = RunState::default();
assert!(!rs.thinking_disabled_for_rest_of_run);
}
#[test]
fn run_state_thinking_disabled_after_3_strikes() {
let mut rs = RunState::default();
rs.record_reasoning_only();
assert!(!rs.thinking_disabled_for_rest_of_run);
assert_eq!(rs.reasoning_only_strikes, 1);
rs.record_reasoning_only();
assert!(!rs.thinking_disabled_for_rest_of_run);
assert_eq!(rs.reasoning_only_strikes, 2);
rs.record_reasoning_only();
assert!(rs.thinking_disabled_for_rest_of_run);
assert_eq!(rs.reasoning_only_strikes, 3);
}
#[test]
fn run_state_thinking_disabled_resets_on_new_run() {
let mut rs = RunState::default();
rs.record_reasoning_only();
rs.record_reasoning_only();
rs.record_reasoning_only();
assert!(rs.thinking_disabled_for_rest_of_run);
assert_eq!(rs.reasoning_only_strikes, 3);
rs.reset_for_new_run();
assert!(!rs.thinking_disabled_for_rest_of_run);
assert_eq!(rs.reasoning_only_strikes, 0);
}
#[test]
fn run_state_thinking_disabled_stays_after_actions() {
let mut rs = RunState::default();
rs.record_reasoning_only();
rs.record_reasoning_only();
rs.record_reasoning_only();
assert!(rs.thinking_disabled_for_rest_of_run);
rs.record_actions(2);
assert!(rs.thinking_disabled_for_rest_of_run);
assert_eq!(rs.reasoning_only_strikes, 0); }
#[test]
fn pre_t7_wire_keys_are_read_through_aliases() {
let json = r#"{
"turn_tool_calls": 4,
"run_has_tool_calls": true,
"reasoning_only_strikes": 0,
"empty_response_strikes": 1,
"nudge_count": 2
}"#;
let rs: RunState = serde_json::from_str(json).unwrap();
assert_eq!(rs.turn_actions, 4);
assert!(rs.run_has_actions);
assert_eq!(rs.empty_response_strikes, 1);
assert_eq!(rs.nudge_count, 2);
}
#[test]
fn run_state_round_trips_under_the_new_wire_keys() {
let mut rs = RunState::default();
rs.record_actions(3);
let json = serde_json::to_string(&rs).unwrap();
assert!(json.contains("\"total_actions\":3"), "new key: {json}");
assert!(json.contains("\"turn_actions\":3"), "new key: {json}");
assert!(json.contains("\"run_has_actions\":true"), "new key: {json}");
let decoded: RunState = serde_json::from_str(&json).unwrap();
assert_eq!(decoded.turn_actions, 3);
assert_eq!(decoded.total_actions, 3);
assert!(decoded.run_has_actions);
}
}
#[cfg(test)]
mod proptest_tests {
use super::*;
use proptest::prelude::*;
proptest! {
#[test]
fn reset_for_new_run_zeros_run_fields(
total_actions in 0usize..1000,
turn_actions in 0usize..1000,
run_has_actions in proptest::bool::ANY,
reasoning_only_strikes in 0usize..100,
empty_response_strikes in 0usize..100,
nudge_count in 0usize..100,
) {
let mut rs = RunState {
total_actions,
turn_actions,
run_has_actions,
reasoning_only_strikes,
empty_response_strikes,
nudge_count,
thinking_disabled_for_rest_of_run: true,
original_thinking_enabled: true,
truncation_strikes: 5,
awaiting_human: true,
};
rs.reset_for_new_run();
assert_eq!(rs.total_actions, total_actions);
assert_eq!(rs.turn_actions, 0);
assert!(!rs.run_has_actions);
assert_eq!(rs.reasoning_only_strikes, 0);
assert_eq!(rs.empty_response_strikes, 0);
assert_eq!(rs.nudge_count, 0);
assert_eq!(rs.truncation_strikes, 0);
assert!(!rs.thinking_disabled_for_rest_of_run);
assert!(!rs.awaiting_human);
assert!(rs.original_thinking_enabled);
}
#[test]
fn record_actions_accumulates(n in 0usize..100) {
let mut rs = RunState::default();
rs.record_actions(n);
assert_eq!(rs.turn_actions, n);
assert_eq!(rs.total_actions, n);
assert!(rs.run_has_actions);
assert_eq!(rs.reasoning_only_strikes, 0);
assert_eq!(rs.empty_response_strikes, 0);
}
#[test]
fn record_reasoning_only_increments(count in 1usize..50) {
let mut rs = RunState::default();
for i in 1..=count {
let strikes = rs.record_reasoning_only();
assert_eq!(strikes, i);
assert_eq!(rs.empty_response_strikes, 0);
}
}
#[test]
fn record_empty_response_increments(count in 1usize..50) {
let mut rs = RunState::default();
for i in 1..=count {
let strikes = rs.record_empty_response();
assert_eq!(strikes, i);
assert_eq!(rs.reasoning_only_strikes, 0);
}
}
}
}