diff --git a/docs/4-implementation/turn-loop/claude-invocation.md b/docs/4-implementation/turn-loop/claude-invocation.md index a7d0d1524..8b642ecc7 100644 --- a/docs/4-implementation/turn-loop/claude-invocation.md +++ b/docs/4-implementation/turn-loop/claude-invocation.md @@ -63,7 +63,7 @@ The context window is whatever the model reports once a turn completes (`modelUsage.*.contextWindow`, `Bus::api_context_window`). The harness keeps no per-model table of window sizes: until the first turn of a process completes the window is unknown, and what depends on it waits (the compaction watermarks, the -auto-reset, the `compact` tool, the context fill the swarm shows). +autoreset, the `compact` tool, the context fill the swarm shows). The window drives watermarks; no web endpoint serves it. @@ -188,7 +188,7 @@ still applies. - **Auto session-reset** — a third path (`turn::maybe_auto_reset`, pre-turn) that fires when both conditions hold: context is ≥ a watermark (`HIVE_AUTO_RESET_WATERMARK_TOKENS`, default **50% of the reported - window**, none before one is reported) AND the time since the last turn + window**, none until the API reports one) AND the time since the last turn exceeds the assumed prompt-cache TTL (`HIVE_CACHE_TTL_SECS`, default `3600`). Claude's prompt cache goes cold after a while; once it's cold, `--resume`-ing a large session pays the full re-upload cost with no diff --git a/hive-agent/src/todo_server.rs b/hive-agent/src/todo_server.rs index 2121b9dbc..e41c41e99 100644 --- a/hive-agent/src/todo_server.rs +++ b/hive-agent/src/todo_server.rs @@ -981,6 +981,34 @@ mod tests { assert!(low.take_compact().is_none(), "a refusal queues nothing"); } + /// Without a window to divide by, the tool refuses and says why; once a turn + /// reports one, the same usage is judged against it. + #[test] + fn compact_is_refused_while_the_context_window_is_unknown() { + let (_dir, todos, _) = stores(); + let wake = Notify::new(); + let bus = Bus::new(); + let usage: hive_claude::TokenUsage = serde_json::from_value(serde_json::json!({ + "input_tokens": 50, + "output_tokens": 0, + "cache_read_input_tokens": 0, + "cache_creation_input_tokens": 0, + })) + .expect("a usage block"); + bus.record_turn_usage(usage, usage); + + let resp = dispatch(compact_req(None), &todos, &wake, None, &bus); + assert!( + matches!(&resp, Response::Err { message } if message.contains("window is unknown")), + "{resp:?}" + ); + assert!(bus.take_compact().is_none(), "a refusal queues nothing"); + + bus.set_api_context_window(100); + let resp = dispatch(compact_req(None), &todos, &wake, None, &bus); + assert!(matches!(resp, Response::Ok), "{resp:?}"); + } + #[test] fn the_compact_floor_is_forty_percent_of_the_window() { let (_dir, todos, _) = stores(); diff --git a/hive-agent/src/turn.rs b/hive-agent/src/turn.rs index 1cf4e6bcd..9a7c5bd32 100644 --- a/hive-agent/src/turn.rs +++ b/hive-agent/src/turn.rs @@ -875,7 +875,7 @@ fn archive_session(bus: &Bus, session: &AgentSession) { #[cfg(test)] mod tests { - use super::{PercentPolicy, SoftLimitPolicy}; + use super::{PercentPolicy, SoftLimitPolicy, auto_reset_watermark_tokens}; use super::{ PermissionAsk, TurnError, TurnOutcome, acp_permits, apply_telemetry, emit_turn_end, error_to_turn, nonzero_or, parse_u64, report_compact, runtime_error_to_turn, @@ -922,6 +922,40 @@ mod tests { assert!(policy(75).should_compact(high)); } + /// The auto-reset watermark is half the window the API reported, and none + /// (`0`, off) before one is, instead of half a guessed size. + #[test] + fn the_auto_reset_watermark_waits_for_a_reported_window() { + let bus = Bus::new(); + assert_eq!(auto_reset_watermark_tokens(&bus), 0); + bus.set_api_context_window(1_000_000); + assert_eq!(auto_reset_watermark_tokens(&bus), 500_000); + } + + /// A turn that reports no window never triggers the proactive compaction, + /// however full the context: there is nothing to take a percent of. + #[test] + fn no_reported_window_means_no_proactive_compaction() { + let policy = SoftLimitPolicy { + percent: PercentPolicy { + percent: 75, + default_window: None, + checkpoint_prompt: None, + }, + bus: Bus::new(), + }; + let unreported = Usage { + context_tokens: 900_000, + context_window: None, + }; + assert!(!policy.should_compact(unreported)); + let reported = Usage { + context_window: Some(1_000_000), + ..unreported + }; + assert!(policy.should_compact(reported)); + } + #[test] fn a_turn_that_parsed_nothing_does_not_keep_the_previous_resolved_model() { let resolved = crate::paths::with_harness_dir(|| {