дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-05 17:47:08 +02:00
fix(agents): don't let a late model request abort a newer model's discovery (#8249)
load_levels hands the catalog request to a blocking task, which can run after a newer selection's request; the late one then superseded and aborted the newer discovery, failing it with "The agent did not report settings for the selected model" (the intermittent Windows failure of rapid_idle_model_switches_share_discovery_and_never_apply_a_stale_completion). Model requests now take a ticket when made; an older one neither changes the requested model nor supersedes the running flight.
Цей коміт міститься в:
1 джерело
c931cbecc0
коміт
fb91ace8aa
2 files changed
+103
-4
No files matched your search
@@ -25,6 +25,8 @@ type Key = (PathBuf, PathBuf, String);
|
||||
#[derive(Default)]
|
||||
pub struct AgentCatalogs {
|
||||
state: Mutex<State>,
|
||||
/// Orders model requests by when they were made, not when they reach `state`.
|
||||
tickets: std::sync::atomic::AtomicU64,
|
||||
#[cfg(test)]
|
||||
clock_ms: std::sync::atomic::AtomicU64,
|
||||
}
|
||||
@@ -49,6 +51,8 @@ struct Entry {
|
||||
abort: Option<tokio::task::AbortHandle>,
|
||||
flight_model: Option<String>,
|
||||
requested_model: Option<String>,
|
||||
/// Ticket of the newest model request applied: an older one arriving late can't supersede it.
|
||||
model_ticket: u64,
|
||||
model_failures: HashMap<String, ModelFailure>,
|
||||
catalog: Option<CatalogData>,
|
||||
}
|
||||
@@ -254,11 +258,13 @@ impl AgentCatalogs {
|
||||
}
|
||||
let unavailable = || "The agent did not report settings for the selected model".to_owned();
|
||||
let spec = shared.spec(provider).ok_or_else(unavailable)?;
|
||||
// Taken now: the blocking task may run after a newer selection's request.
|
||||
let ticket = self.ticket();
|
||||
let flight = {
|
||||
let catalogs = self.clone();
|
||||
let shared = shared.clone();
|
||||
let model = model.to_owned();
|
||||
tokio::task::spawn_blocking(move || catalogs.request(&shared, spec, false, Some(&model)).1)
|
||||
tokio::task::spawn_blocking(move || catalogs.request_with(&shared, spec, false, Some(&model), ticket).1)
|
||||
.await
|
||||
.map_err(|e| e.to_string())?
|
||||
};
|
||||
@@ -331,12 +337,49 @@ impl AgentCatalogs {
|
||||
}
|
||||
|
||||
/// Start or join a lookup. No queued refresh can outlive the request that asked for it.
|
||||
/// The model the running discovery of `provider` looks up, if one is running.
|
||||
#[cfg(test)]
|
||||
pub(crate) fn flight_model(&self, shared: &Shared, provider: &str) -> Option<Option<String>> {
|
||||
let state = self.state.lock();
|
||||
let entry = state.entries.get(&Self::key(shared, provider))?;
|
||||
entry.flight.as_ref().map(|_| entry.flight_model.clone())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) fn request_as_of(
|
||||
self: &Arc<Self>,
|
||||
shared: &Arc<Shared>,
|
||||
spec: provider::ProviderSpec,
|
||||
model: &str,
|
||||
ticket: u64,
|
||||
) -> Option<watch::Receiver<bool>> {
|
||||
self.request_with(shared, spec, false, Some(model), ticket).1
|
||||
}
|
||||
|
||||
pub(crate) fn ticket(&self) -> u64 {
|
||||
self.tickets.fetch_add(1, std::sync::atomic::Ordering::Relaxed) + 1
|
||||
}
|
||||
|
||||
pub(crate) fn request(
|
||||
self: &Arc<Self>,
|
||||
shared: &Arc<Shared>,
|
||||
spec: provider::ProviderSpec,
|
||||
force: bool,
|
||||
model: Option<&str>,
|
||||
) -> (AgentModelCatalog, Option<watch::Receiver<bool>>) {
|
||||
let ticket = self.ticket();
|
||||
self.request_with(shared, spec, force, model, ticket)
|
||||
}
|
||||
|
||||
/// [`Self::request`] for a request made when `ticket` was taken. A model request older than
|
||||
/// the newest one applied neither changes the requested model nor supersedes its flight.
|
||||
fn request_with(
|
||||
self: &Arc<Self>,
|
||||
shared: &Arc<Shared>,
|
||||
spec: provider::ProviderSpec,
|
||||
force: bool,
|
||||
model: Option<&str>,
|
||||
ticket: u64,
|
||||
) -> (AgentModelCatalog, Option<watch::Receiver<bool>>) {
|
||||
let key = Self::key(shared, &spec.id);
|
||||
let context = LaunchContext::new(spec.clone(), &shared.paths.project_root);
|
||||
@@ -388,6 +431,7 @@ impl AgentCatalogs {
|
||||
abort: None,
|
||||
flight_model: None,
|
||||
requested_model: None,
|
||||
model_ticket: 0,
|
||||
model_failures: HashMap::new(),
|
||||
},
|
||||
);
|
||||
@@ -404,10 +448,15 @@ impl AgentCatalogs {
|
||||
|| (!fresh && catalog.model_option().is_none())
|
||||
})
|
||||
.map(str::to_owned);
|
||||
if model.is_some() && entry.requested_model.as_deref() != model {
|
||||
entry.requested_model = model.map(str::to_owned);
|
||||
let stale = model.is_some() && ticket < entry.model_ticket;
|
||||
if model.is_some() && !stale {
|
||||
entry.model_ticket = ticket;
|
||||
if entry.requested_model.as_deref() != model {
|
||||
entry.requested_model = model.map(str::to_owned);
|
||||
}
|
||||
}
|
||||
let superseded = model.is_some()
|
||||
let superseded = !stale
|
||||
&& model.is_some()
|
||||
&& entry.flight.is_some()
|
||||
&& (entry.flight_model.is_some() || target.is_some())
|
||||
&& entry.flight_model.as_deref() != target.as_deref();
|
||||
|
||||
@@ -4455,6 +4455,56 @@ async fn rapid_idle_model_switches_share_discovery_and_never_apply_a_stale_compl
|
||||
restored.shutdown();
|
||||
}
|
||||
|
||||
/// A model request reaches the catalogs from a blocking task, so it may arrive after a newer
|
||||
/// selection's; arriving late, it must not supersede (and abort) the newer discovery.
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn a_late_model_request_never_supersedes_a_newer_discovery() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let agent = Arc::new(FakeAgent::default());
|
||||
*agent.model_options.lock() = HashMap::from([
|
||||
("big".into(), effort_options(&["low", "high"], "high")),
|
||||
("small".into(), effort_options(&["minimal", "medium"], "minimal")),
|
||||
("plain".into(), vec![]),
|
||||
]);
|
||||
let mgr = manager(tmp.path(), agent.clone());
|
||||
let task = mgr
|
||||
.create(NewAgentTask {
|
||||
prompt: text("again"),
|
||||
provider: "claude-acp".into(),
|
||||
isolation: Isolation::Local,
|
||||
..Default::default()
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
wait_for(&mgr, &task.id, AgentStatus::Ready).await;
|
||||
mgr.shutdown();
|
||||
let restored = Arc::new(manager(tmp.path(), agent.clone()));
|
||||
let (catalogs, shared) = (restored.shared.paths.catalogs.clone(), restored.shared.clone());
|
||||
let spec = || shared.spec("claude-acp").unwrap();
|
||||
|
||||
let late = catalogs.ticket();
|
||||
let (release, gate) = std::sync::mpsc::channel();
|
||||
*agent.model_config_gate.lock() = Some(gate);
|
||||
let newer = {
|
||||
let (catalogs, shared) = (catalogs.clone(), shared.clone());
|
||||
tokio::spawn(async move { catalogs.load_levels(&shared, "claude-acp", "plain").await })
|
||||
};
|
||||
tokio::time::timeout(Duration::from_secs(5), async {
|
||||
while agent.model_config_gate.lock().is_some() {
|
||||
tokio::task::yield_now().await;
|
||||
}
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(catalogs.flight_model(&shared, "claude-acp"), Some(Some("plain".into())));
|
||||
|
||||
catalogs.request_as_of(&shared, spec(), "small", late);
|
||||
assert_eq!(catalogs.flight_model(&shared, "claude-acp"), Some(Some("plain".into())), "not superseded");
|
||||
release.send(()).unwrap();
|
||||
assert_eq!(newer.await.unwrap().unwrap(), Vec::<AgentConfigOption>::new());
|
||||
restored.shutdown();
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn explicit_service_tier_reaches_the_session_even_when_it_matches_the_reported_default() {
|
||||
for value in [None, Some("false"), Some("true")] {
|
||||
|
||||
Reference in new issue
Заблокувати користувача