4 Commits
Author SHA1 Message Date
hermes 77e32bbc78 Merge pull request 'router: skip tiers whose window cannot hold the history' (#188) from fix-router-window-fit into main
build / version (push) Successful in 14s
build / build (aarch64-apple-darwin) (push) Successful in 6m30s
build / build (x86_64-unknown-linux-musl) (push) Successful in 5m40s
build / release (push) Successful in 24s
2026-11-14 12:00:36 +00:00
hermes 79cf5fcda1 agent: abort also ends a hanging request and compaction 2026-11-14 12:00:00 +00:00
gitea-actions 05dbb8e63f ci: bin.json v0.1.11 2026-11-14 11:58:50 +00:00
hermes e119dc2f0d router: skip tiers whose window cannot hold the history 2026-11-14 11:56:17 +00:00
3 changed files with 75 additions and 7 deletions

No files matched your search

+3 -3
View File
@@ -1,7 +1,7 @@
{ {
"version": "0.1.10", "version": "0.1.11",
"sha256": { "sha256": {
"aarch64-apple-darwin": "b0192269b031068f5799ac61b70d85f9e5364b17d1634701084f76b3707bf063", "aarch64-apple-darwin": "a363071712b8a0df2fce74a671b5ee1c7ee3bb384fc2989d71c3bd6654c62874",
"x86_64-unknown-linux-musl": "e42f35a2df69ceeed24004ec1ab5cb7c043133fda2f7acd52a5b7fe62ce04109" "x86_64-unknown-linux-musl": "f4d0c71869d25b06be19c2da9be48f4ae0ea71acf413bc5460706b4e0d0a87a4"
} }
} }
+44 -2
View File
@@ -433,7 +433,12 @@ impl Agent {
) -> Result<futures::stream::BoxStream<'static, hx_ai::Result<StreamEvent>>, Error> { ) -> Result<futures::stream::BoxStream<'static, hx_ai::Result<StreamEvent>>, Error> {
let mut wait = self.retry_ms; let mut wait = self.retry_ms;
for attempt in 1..=4 { for attempt in 1..=4 {
match self.provider.stream(req.clone()).await { let mut ab = self.abort.clone();
let r = tokio::select! {
r = self.provider.stream(req.clone()) => r,
_ = async { ab.wait_for(|a| *a).await.is_ok() } => return Err(Error::Aborted),
};
match r {
Err(hx_ai::Error::Api { status, .. }) if attempt < 4 && transient(status) => { Err(hx_ai::Error::Api { status, .. }) if attempt < 4 && transient(status) => {
self.emit(Event::Info(format!( self.emit(Event::Info(format!(
"api error {status}; retry {attempt}/3 in {wait}ms" "api error {status}; retry {attempt}/3 in {wait}ms"
@@ -477,8 +482,12 @@ impl Agent {
if self.aborted() { if self.aborted() {
return Err(Error::Aborted); return Err(Error::Aborted);
} }
let mut ab = self.abort.clone();
if self.window.should_compact(&self.session.messages()) if self.window.should_compact(&self.session.messages())
&& self.compact(KEEP_MESSAGES).await? && tokio::select! {
r = self.compact(KEEP_MESSAGES) => r?,
_ = async { ab.wait_for(|a| *a).await.is_ok() } => return Err(Error::Aborted),
}
{ {
self.emit(Event::Info("context compacted".into())).await; self.emit(Event::Info("context compacted".into())).await;
} }
@@ -919,6 +928,39 @@ mod tests {
(r, n) (r, n)
} }
struct Hang;
#[async_trait]
impl Provider for Hang {
async fn stream(
&self,
_: Request,
) -> hx_ai::Result<BoxStream<'static, hx_ai::Result<StreamEvent>>> {
std::future::pending().await
}
}
#[tokio::test]
async fn abort_ends_a_request_that_never_answers() {
let dir = tempfile::tempdir().unwrap();
let cx = ToolCtx {
cwd: dir.path().into(),
state: Default::default(),
};
let (mut a, h) = Agent::new(
Arc::new(Hang),
Registry::default(),
Session::open(dir.path().join("s.jsonl")).unwrap(),
cx,
);
let t = tokio::spawn(async move {
tokio::time::sleep(std::time::Duration::from_millis(50)).await;
h.abort();
});
let r = tokio::time::timeout(std::time::Duration::from_secs(5), a.run("go")).await;
t.await.unwrap();
assert!(matches!(r, Ok(Err(Error::Aborted))));
}
#[tokio::test] #[tokio::test]
async fn transient_api_errors_are_retried() { async fn transient_api_errors_are_retried() {
let (r, n) = flaky_run(1, 529).await; let (r, n) = flaky_run(1, 529).await;
+28 -2
View File
@@ -371,7 +371,20 @@ impl Router {
} }
}; };
let prefer = self.cur.map_or(start, |(k, _)| k); let prefer = self.cur.map_or(start, |(k, _)| k);
let (kind, model) = pick_for(tier, prefer, avoid, models, &self.cfg)?; let (mut kind, mut model) = pick_for(tier, prefer, avoid, models, &self.cfg)?;
// the history must fit the window (a 1M session cannot go down to a 200k Haiku)
let mut tier = tier;
while (hx_ai::models::window(models, &model) as usize) * 9 / 10 < sig.ctx {
let up = match tier {
Tier::Fast => Tier::Balanced,
Tier::Balanced => Tier::Deep,
_ => break,
};
let Some((k, m)) = pick_for(up, kind, avoid, models, &self.cfg) else {
break;
};
(tier, kind, model) = (up, k, m);
}
// no Fable in the catalog: it ran as Deep // no Fable in the catalog: it ran as Deep
let tier = if tier == Tier::Max && Tier::of(&model) != Some(Tier::Max) { let tier = if tier == Tier::Max && Tier::of(&model) != Some(Tier::Max) {
Tier::Deep Tier::Deep
@@ -485,7 +498,7 @@ mod tests {
ModelInfo { ModelInfo {
id: id.into(), id: id.into(),
kind, kind,
context_window: 1, context_window: 1_000_000,
} }
} }
@@ -635,6 +648,19 @@ mod tests {
); );
} }
#[test]
fn history_too_big_for_the_window_skips_the_small_tier() {
let mut r = Router::new(Cfg::default());
let mut light = signals("commit", &[]);
assert_eq!(light.tier, Tier::Fast);
light.ctx = 250_000;
let mut c = cat();
c[2].context_window = 200_000;
c[1].context_window = 1_000_000;
let p = r.route(&light, Kind::Claude, None, &c).unwrap();
assert_eq!(p.model, "claude-sonnet-5-5");
}
#[test] #[test]
fn effort_follows_score_and_provider() { fn effort_follows_score_and_provider() {
assert_eq!(effort_for(-2, Kind::Claude), Effort::Off); assert_eq!(effort_for(-2, Kind::Claude), Effort::Off);