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

@@ -20,18 +20,24 @@ fn test_no_wrong_serialization_format() {
CacheControl::ephemeral(),
);
let json = serde_json::to_string(&msg).unwrap();
println!("Ephemeral message JSON: {}", json);
// Should NOT contain the wrong format
assert!(!json.contains("system.0.cache_control"),
"JSON should not contain 'system.0.cache_control' path");
assert!(!json.contains("cache_control.ephemeral"),
"JSON should not contain 'cache_control.ephemeral' path");
assert!(
!json.contains("system.0.cache_control"),
"JSON should not contain 'system.0.cache_control' path"
);
assert!(
!json.contains("cache_control.ephemeral"),
"JSON should not contain 'cache_control.ephemeral' path"
);
// Should contain the correct format
assert!(json.contains(r#""cache_control":{"type":"ephemeral"}"#),
"JSON should contain correct cache_control format");
assert!(
json.contains(r#""cache_control":{"type":"ephemeral"}"#),
"JSON should contain correct cache_control format"
);
}
#[test]
@@ -42,20 +48,28 @@ fn test_five_minute_no_wrong_format() {
CacheControl::five_minute(),
);
let json = serde_json::to_string(&msg).unwrap();
println!("5-minute message JSON: {}", json);
// Should NOT contain the wrong format
assert!(!json.contains("system.0.cache_control"),
"JSON should not contain 'system.0.cache_control' path");
assert!(!json.contains("cache_control.ephemeral.ttl"),
"JSON should not contain 'cache_control.ephemeral.ttl' path");
assert!(
!json.contains("system.0.cache_control"),
"JSON should not contain 'system.0.cache_control' path"
);
assert!(
!json.contains("cache_control.ephemeral.ttl"),
"JSON should not contain 'cache_control.ephemeral.ttl' path"
);
// Should contain the correct format with ttl as a direct field
assert!(json.contains(r#""type":"ephemeral""#),
"JSON should contain type field");
assert!(json.contains(r#""ttl":"5m""#),
"JSON should contain ttl field with value 5m");
assert!(
json.contains(r#""type":"ephemeral""#),
"JSON should contain type field"
);
assert!(
json.contains(r#""ttl":"5m""#),
"JSON should contain ttl field with value 5m"
);
}
#[test]
@@ -66,44 +80,59 @@ fn test_one_hour_no_wrong_format() {
CacheControl::one_hour(),
);
let json = serde_json::to_string(&msg).unwrap();
println!("1-hour message JSON: {}", json);
// Should NOT contain the wrong format
assert!(!json.contains("system.0.cache_control"),
"JSON should not contain 'system.0.cache_control' path");
assert!(!json.contains("cache_control.ephemeral.ttl"),
"JSON should not contain 'cache_control.ephemeral.ttl' path");
assert!(
!json.contains("system.0.cache_control"),
"JSON should not contain 'system.0.cache_control' path"
);
assert!(
!json.contains("cache_control.ephemeral.ttl"),
"JSON should not contain 'cache_control.ephemeral.ttl' path"
);
// Should contain the correct format with ttl as a direct field
assert!(json.contains(r#""type":"ephemeral""#),
"JSON should contain type field");
assert!(json.contains(r#""ttl":"1h""#),
"JSON should contain ttl field with value 1h");
assert!(
json.contains(r#""type":"ephemeral""#),
"JSON should contain type field"
);
assert!(
json.contains(r#""ttl":"1h""#),
"JSON should contain ttl field with value 1h"
);
}
#[test]
fn test_cache_control_structure_is_flat() {
// Verify that the cache_control object has a flat structure
// with 'type' and optional 'ttl' at the same level
let cache_control = CacheControl::five_minute();
let json_value = serde_json::to_value(&cache_control).unwrap();
println!("Cache control as JSON value: {}", serde_json::to_string_pretty(&json_value).unwrap());
println!(
"Cache control as JSON value: {}",
serde_json::to_string_pretty(&json_value).unwrap()
);
let obj = json_value.as_object().expect("Should be an object");
// Should have exactly 2 keys at the top level
assert_eq!(obj.len(), 2, "Cache control should have exactly 2 top-level fields");
assert_eq!(
obj.len(),
2,
"Cache control should have exactly 2 top-level fields"
);
// Both 'type' and 'ttl' should be at the same level
assert!(obj.contains_key("type"), "Should have 'type' field");
assert!(obj.contains_key("ttl"), "Should have 'ttl' field");
// 'type' should be a string, not an object
assert!(obj["type"].is_string(), "'type' should be a string value");
// 'ttl' should be a string, not nested
assert!(obj["ttl"].is_string(), "'ttl' should be a string value");
}
@@ -112,20 +141,30 @@ fn test_cache_control_structure_is_flat() {
fn test_ephemeral_cache_control_structure() {
let cache_control = CacheControl::ephemeral();
let json_value = serde_json::to_value(&cache_control).unwrap();
println!("Ephemeral cache control as JSON value: {}", serde_json::to_string_pretty(&json_value).unwrap());
println!(
"Ephemeral cache control as JSON value: {}",
serde_json::to_string_pretty(&json_value).unwrap()
);
let obj = json_value.as_object().expect("Should be an object");
// Should have exactly 1 key (only 'type', no 'ttl')
assert_eq!(obj.len(), 1, "Ephemeral cache control should have exactly 1 top-level field");
assert_eq!(
obj.len(),
1,
"Ephemeral cache control should have exactly 1 top-level field"
);
// Should have 'type' field
assert!(obj.contains_key("type"), "Should have 'type' field");
// Should NOT have 'ttl' field
assert!(!obj.contains_key("ttl"), "Ephemeral should not have 'ttl' field");
assert!(
!obj.contains_key("ttl"),
"Ephemeral should not have 'ttl' field"
);
// 'type' should be a string with value "ephemeral"
assert_eq!(obj["type"].as_str().unwrap(), "ephemeral");
}