hive-agent: keep the last turn's context size in the harness state file and drop the turn sqlite writes
This commit is contained in:
parent
7bd93ecfde
commit
5e5ff64b3c
1 changed files with 261 additions and 629 deletions
|
|
@ -190,6 +190,28 @@ pub(crate) fn write_api_key_mode(enabled: bool) {
|
|||
write_harness_json(&v);
|
||||
}
|
||||
|
||||
/// Keep `tokens`, the context size the turn that just ended left (what warming
|
||||
/// the cache costs), in the consolidated state file, so a restarted harness
|
||||
/// can seed the context gauge and the auto-reset check before its first turn
|
||||
/// (`Bus::seed_usage`). One value, overwritten each turn.
|
||||
pub(crate) fn write_context_tokens(tokens: u64) {
|
||||
let _guard = HARNESS_JSON_LOCK
|
||||
.lock()
|
||||
.unwrap_or_else(std::sync::PoisonError::into_inner);
|
||||
let mut v = read_harness_json();
|
||||
v["context_tokens"] = tokens.into();
|
||||
write_harness_json(&v);
|
||||
}
|
||||
|
||||
/// The context size [`write_context_tokens`] kept, `None` when none was kept
|
||||
/// or it is zero.
|
||||
pub(crate) fn read_context_tokens() -> Option<u64> {
|
||||
read_harness_json()
|
||||
.get("context_tokens")
|
||||
.and_then(serde_json::Value::as_u64)
|
||||
.filter(|n| *n > 0)
|
||||
}
|
||||
|
||||
/// Compiled-in fallback model used when neither `HIVE_DEFAULT_MODEL` nor a
|
||||
/// `hyperhive-model` file is present.
|
||||
pub const DEFAULT_MODEL: &str = "haiku";
|
||||
|
|
@ -227,18 +249,14 @@ pub fn configured_effort() -> Option<&'static str> {
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{
|
||||
DEFAULT_EFFORT, DEFAULT_MODEL, Source, effort_from, model_from, read_harness_state,
|
||||
write_harness_state,
|
||||
DEFAULT_EFFORT, DEFAULT_MODEL, Source, effort_from, model_from, read_context_tokens,
|
||||
read_harness_state, write_context_tokens, write_harness_state,
|
||||
};
|
||||
|
||||
/// A state file an older harness wrote, with the `active_model` nothing
|
||||
/// reads any more, neither stops the agent reading its flags nor is lost
|
||||
/// or turned into an error by the next write: the file is read as a JSON
|
||||
/// value, not a typed struct, so a leftover field cannot fail to parse.
|
||||
#[test]
|
||||
fn a_leftover_active_model_is_ignored_and_survives_a_write() {
|
||||
// `HYPERHIVE_STATE_DIR` is process-wide; this is the only test that
|
||||
// sets it, so a lock of its own is enough.
|
||||
/// Run `f` with `HYPERHIVE_STATE_DIR` pointing at an empty temp dir.
|
||||
fn in_state_dir(f: impl FnOnce(&std::path::Path)) {
|
||||
// `HYPERHIVE_STATE_DIR` is process-wide: one lock for every test that
|
||||
// sets it.
|
||||
static LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());
|
||||
let _lock = LOCK
|
||||
.lock()
|
||||
|
|
@ -247,19 +265,7 @@ mod tests {
|
|||
let previous = std::env::var_os("HYPERHIVE_STATE_DIR");
|
||||
// SAFETY: serialised by `LOCK`, and restored below.
|
||||
unsafe { std::env::set_var("HYPERHIVE_STATE_DIR", dir.path()) };
|
||||
let file = dir.path().join("hyperhive-harness.json");
|
||||
std::fs::write(
|
||||
&file,
|
||||
r#"{"rate_limited":true,"needs_login":false,"active_model":"opus"}"#,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let read = read_harness_state();
|
||||
write_harness_state(false, true);
|
||||
let after = read_harness_state();
|
||||
let stored: serde_json::Value =
|
||||
serde_json::from_str(&std::fs::read_to_string(&file).unwrap()).unwrap();
|
||||
|
||||
f(dir.path());
|
||||
// SAFETY: still holding `LOCK`.
|
||||
unsafe {
|
||||
match previous {
|
||||
|
|
@ -267,11 +273,54 @@ mod tests {
|
|||
None => std::env::remove_var("HYPERHIVE_STATE_DIR"),
|
||||
}
|
||||
}
|
||||
assert_eq!(read, (true, false));
|
||||
assert_eq!(after, (false, true));
|
||||
assert_eq!(stored["active_model"], "opus", "the leftover is kept");
|
||||
assert_eq!(stored["rate_limited"], false);
|
||||
assert_eq!(stored["needs_login"], true);
|
||||
}
|
||||
|
||||
/// A state file an older harness wrote, with the `active_model` nothing
|
||||
/// reads any more, neither stops the agent reading its flags nor is lost
|
||||
/// or turned into an error by the next write: the file is read as a JSON
|
||||
/// value, not a typed struct, so a leftover field cannot fail to parse.
|
||||
#[test]
|
||||
fn a_leftover_active_model_is_ignored_and_survives_a_write() {
|
||||
in_state_dir(|dir| {
|
||||
let file = dir.join("hyperhive-harness.json");
|
||||
std::fs::write(
|
||||
&file,
|
||||
r#"{"rate_limited":true,"needs_login":false,"active_model":"opus"}"#,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let read = read_harness_state();
|
||||
write_harness_state(false, true);
|
||||
let after = read_harness_state();
|
||||
let stored: serde_json::Value =
|
||||
serde_json::from_str(&std::fs::read_to_string(&file).unwrap()).unwrap();
|
||||
|
||||
assert_eq!(read, (true, false));
|
||||
assert_eq!(after, (false, true));
|
||||
assert_eq!(stored["active_model"], "opus", "the leftover is kept");
|
||||
assert_eq!(stored["rate_limited"], false);
|
||||
assert_eq!(stored["needs_login"], true);
|
||||
});
|
||||
}
|
||||
|
||||
/// The context size is one field of the same file: nothing kept reads as
|
||||
/// nothing, the latest turn replaces the one before, a zero is no reading,
|
||||
/// and the flags beside it survive every write.
|
||||
#[test]
|
||||
fn the_context_size_is_one_field_that_the_latest_turn_replaces() {
|
||||
in_state_dir(|_| {
|
||||
assert_eq!(read_context_tokens(), None);
|
||||
write_harness_state(true, false);
|
||||
|
||||
write_context_tokens(100_000);
|
||||
assert_eq!(read_context_tokens(), Some(100_000));
|
||||
write_context_tokens(180_000);
|
||||
assert_eq!(read_context_tokens(), Some(180_000));
|
||||
assert_eq!(read_harness_state(), (true, false));
|
||||
|
||||
write_context_tokens(0);
|
||||
assert_eq!(read_context_tokens(), None);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
|
|||
Loading…
Reference in a new issue