Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

fix(agent-rpc): refuse a session model no provider serves

thinhlpgfa6008919bc0
diff --git a/src-tauri/jan-cli/tests/agent_rpc.rs b/src-tauri/jan-cli/tests/agent_rpc.rs
index 7450da546..90562af53 100644
--- a/src-tauri/jan-cli/tests/agent_rpc.rs
+++ b/src-tauri/jan-cli/tests/agent_rpc.rs
@@ -857,3 +857,49 @@ fn interrupting_withdraws_pending_host_requests() {
rpc.close();
let _ = std::fs::remove_dir_all(scratch);
}
+
+/// A session's provider and model are pinned when it starts: a model the
+/// configuration cannot resolve is refused at `session/model/set` instead of
+/// being accepted and only failing the next turn, and the session keeps serving
+/// the model it already had. Adopting some other reachable model, or failing a
+/// turn the client had every reason to believe was valid, is what this refuses.
+#[test]
+fn model_set_refuses_a_model_no_provider_serves() {
+ let scratch = scratch("model-pin");
+ let home = scratch.join("home");
+ let (url, seen) = scripted_provider(&[PROSE]);
+ configure(&home, &url);
+ let mut rpc = Rpc::open(&home);
+ rpc.handshake();
+ let project = scratch.join("project");
+ let session_id = rpc.start_session(&project);
+
+ let refused = rpc.ask(serde_json::json!({"jsonrpc":"2.0","id":4,"method":"session/model/set","params":{"sessionId":session_id,"model":"no-such-model"}}));
+ assert_eq!(refused["error"]["code"], -32602, "{refused}");
+ assert!(
+ refused["error"]["message"]
+ .as_str()
+ .unwrap_or_default()
+ .contains("no-such-model"),
+ "the refusal names the model: {refused}"
+ );
+ assert!(refused["result"].is_null(), "a refusal carries no model: {refused}");
+
+ // The session is still on the model it started with: the turn goes out with
+ // `stub-model` in the body, to the provider that serves it.
+ let turn = rpc.ask(serde_json::json!({"jsonrpc":"2.0","id":5,"method":"turn/start","params":{"sessionId":session_id,"input":"hi"}}));
+ assert!(turn["result"]["turnId"].is_string(), "{turn}");
+ rpc.read_until(|record| record["method"] == "turn/completed")
+ .expect("the turn completes");
+ let requests = seen.lock().unwrap().clone();
+ assert!(!requests.is_empty(), "the provider saw the turn");
+ for request in &requests {
+ assert_eq!(
+ request["model"], "stub-model",
+ "no substitution: the session stays on the model it was given: {request}"
+ );
+ }
+
+ rpc.close();
+ let _ = std::fs::remove_dir_all(scratch);
+}
diff --git a/src-tauri/src/core/cli/rpc.rs b/src-tauri/src/core/cli/rpc.rs
index f28ed82b7..c883db722 100644
--- a/src-tauri/src/core/cli/rpc.rs
+++ b/src-tauri/src/core/cli/rpc.rs
@@ -544,17 +544,41 @@ pub async fn serve() -> Result<(), String> {
Ok(set) if active.as_ref().is_some_and(|t| t.session_id == set.session_id) => turn_active(&id),
Ok(set) if set.model.trim().is_empty() => error(&id, -32602, "model must not be empty"),
Ok(set) => {
- let session = sessions.get_mut(&set.session_id).expect("checked");
- match rebuild_agent(session, Some(set.model)) {
- Ok(agent) => {
- // The same registry: nothing is pending between
- // turns, and keeping it keeps one per session.
- let registry = Arc::clone(&session.agent.args.host_tool_requests);
- session.agent = agent;
- session.agent.args.host_tool_requests = registry;
- response(&id, json!({"model":session.agent.model}))
- }
+ // Refuse a model no configured provider serves. The
+ // turn would fail closed on it too -- nothing goes
+ // out on the session's behalf -- but this is the
+ // point at which a client can still be told, and the
+ // session must not be left pinned to a model that
+ // cannot serve it, which the next turn would report
+ // as a failure of the client's own input.
+ let provider_configs = sessions
+ .get(&set.session_id)
+ .expect("checked")
+ .agent
+ .args
+ .provider_configs
+ .clone();
+ match crate::core::agent::upstream::resolve_upstream_for_model(
+ &set.model,
+ provider_configs,
+ )
+ .await
+ {
Err(message) => error(&id, -32602, &message),
+ Ok(_) => {
+ let session = sessions.get_mut(&set.session_id).expect("checked");
+ match rebuild_agent(session, Some(set.model)) {
+ Ok(agent) => {
+ // The same registry: nothing is pending between
+ // turns, and keeping it keeps one per session.
+ let registry = Arc::clone(&session.agent.args.host_tool_requests);
+ session.agent = agent;
+ session.agent.args.host_tool_requests = registry;
+ response(&id, json!({"model":session.agent.model}))
+ }
+ Err(message) => error(&id, -32602, &message),
+ }
+ }
}
}
},