add context window monitor

Writes the current context window to logs/current_context_window (uses a symlink to a session ID).

This PR was unfortunately generated by a different LLM and did a ton of superficial reformating, it's actually a fairly small and benign change, but I don't want to roll back everything. Hope that's ok.
This commit is contained in:
Jochen
2025-11-27 21:00:02 +11:00
parent 93dc4acf86
commit 52f78653b4
89 changed files with 4040 additions and 2576 deletions

View File

@@ -4,7 +4,7 @@ use g3_providers::Usage;
#[test]
fn test_token_accumulation() {
let mut window = ContextWindow::new(10000);
// First API call: 100 prompt + 50 completion = 150 total
let usage1 = Usage {
prompt_tokens: 100,
@@ -22,7 +22,10 @@ fn test_token_accumulation() {
total_tokens: 275,
};
window.update_usage_from_response(&usage2);
assert_eq!(window.used_tokens, 425, "Second call should accumulate to 425 tokens");
assert_eq!(
window.used_tokens, 425,
"Second call should accumulate to 425 tokens"
);
assert_eq!(window.cumulative_tokens, 425, "Cumulative should be 425");
// Third API call with SMALLER token count: 50 prompt + 25 completion = 75 total
@@ -32,27 +35,33 @@ fn test_token_accumulation() {
total_tokens: 75,
};
window.update_usage_from_response(&usage3);
assert_eq!(window.used_tokens, 500, "Third call should accumulate to 500 tokens");
assert_eq!(
window.used_tokens, 500,
"Third call should accumulate to 500 tokens"
);
assert_eq!(window.cumulative_tokens, 500, "Cumulative should be 500");
// Verify tokens never decrease
assert!(window.used_tokens >= 425, "Token count should never decrease!");
assert!(
window.used_tokens >= 425,
"Token count should never decrease!"
);
}
#[test]
fn test_add_streaming_tokens() {
let mut window = ContextWindow::new(10000);
// Add some streaming tokens
window.add_streaming_tokens(100);
assert_eq!(window.used_tokens, 100);
assert_eq!(window.cumulative_tokens, 100);
// Add more
window.add_streaming_tokens(50);
assert_eq!(window.used_tokens, 150);
assert_eq!(window.cumulative_tokens, 150);
// Now update from provider response
let usage = Usage {
prompt_tokens: 80,
@@ -60,7 +69,7 @@ fn test_add_streaming_tokens() {
total_tokens: 120,
};
window.update_usage_from_response(&usage);
// Should ADD to existing, not replace
assert_eq!(window.used_tokens, 270, "Should add 120 to existing 150");
assert_eq!(window.cumulative_tokens, 270);
@@ -69,7 +78,7 @@ fn test_add_streaming_tokens() {
#[test]
fn test_percentage_calculation() {
let mut window = ContextWindow::new(1000);
// Add tokens via provider response
let usage = Usage {
prompt_tokens: 150,
@@ -77,10 +86,10 @@ fn test_percentage_calculation() {
total_tokens: 250,
};
window.update_usage_from_response(&usage);
assert_eq!(window.percentage_used(), 25.0);
assert_eq!(window.remaining_tokens(), 750);
// Add more tokens
let usage2 = Usage {
prompt_tokens: 300,
@@ -88,7 +97,7 @@ fn test_percentage_calculation() {
total_tokens: 500,
};
window.update_usage_from_response(&usage2);
assert_eq!(window.percentage_used(), 75.0);
assert_eq!(window.remaining_tokens(), 250);
}