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): name the session the client holds in the provenance record

thinhlpgcc5bf6af2f54
diff --git a/src-tauri/jan-cli/tests/agent_rpc.rs b/src-tauri/jan-cli/tests/agent_rpc.rs
index 90562af53..3942046ae 100644
--- a/src-tauri/jan-cli/tests/agent_rpc.rs
+++ b/src-tauri/jan-cli/tests/agent_rpc.rs
@@ -858,6 +858,44 @@ fn interrupting_withdraws_pending_host_requests() {
let _ = std::fs::remove_dir_all(scratch);
}
+/// The provenance a client receives names the session that client holds, and
+/// goes on naming it after the session is rebuilt for a model change. A record
+/// naming some internal id, or a new one after `session/model/set`, cannot be
+/// joined to the turn it belongs to, which is the whole use of the field.
+#[test]
+fn provenance_names_the_session_the_client_holds() {
+ let scratch = scratch("provenance-session");
+ 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 provenance = |rpc: &mut Rpc, id: u64| {
+ let turn = rpc.ask(serde_json::json!({"jsonrpc":"2.0","id":id,"method":"turn/start","params":{"sessionId":session_id,"input":"hi"}}));
+ assert!(turn["result"]["turnId"].is_string(), "{turn}");
+ let record = rpc
+ .read_until(|frame| frame["method"] == "item/request_provenance")
+ .expect("the turn's request is reported");
+ rpc.read_until(|frame| frame["method"] == "turn/completed")
+ .expect("the turn completes");
+ record["params"]["event"]["session_id"].clone()
+ };
+
+ assert_eq!(provenance(&mut rpc, 4), serde_json::json!(session_id));
+
+ // Rebuilding the agent for a model change must not rename the session: the
+ // client's id is the session's, not the agent's.
+ let set = rpc.ask(serde_json::json!({"jsonrpc":"2.0","id":5,"method":"session/model/set","params":{"sessionId":session_id,"model":"stub-model"}}));
+ assert_eq!(set["result"]["model"], "stub-model", "{set}");
+ assert_eq!(provenance(&mut rpc, 6), serde_json::json!(session_id));
+
+ 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
diff --git a/src-tauri/src/core/cli/rpc.rs b/src-tauri/src/core/cli/rpc.rs
index c883db722..4ee7b91e1 100644
--- a/src-tauri/src/core/cli/rpc.rs
+++ b/src-tauri/src/core/cli/rpc.rs
@@ -353,6 +353,11 @@ fn start_turn(
/// A new agent for `source`'s project, on `model` or the one it has, carrying
/// its host tools and gate. History and registry are the caller's to decide.
+///
+/// The rebuilt agent keeps the session's own id: a model change replaces the
+/// agent, not the session, and a run that reported a different session after
+/// `session/model/set` would rename something the client is still holding.
+/// `session/fork` names its new session itself, after this returns.
fn rebuild_agent(source: &Session, model: Option<String>) -> Result<AgentSession, String> {
let project = source
.agent
@@ -367,6 +372,7 @@ fn rebuild_agent(source: &Session, model: Option<String>) -> Result<AgentSession
SessionFlags { require_model: true, ..Default::default() },
None,
)?;
+ agent.args.session_id = Some(source.id.clone());
agent.args.host_tools = source.agent.args.host_tools.clone();
agent.args.host_owns_gate = source.agent.args.host_owns_gate;
Ok(agent)
@@ -506,6 +512,11 @@ pub async fn serve() -> Result<(), String> {
agent.args.host_tools = host_tools;
agent.args.host_owns_gate = start.permissions == PermissionOwner::Host;
let sid = uuid::Uuid::new_v4().to_string();
+ // The run reports the session by the id this client holds, not
+ // by the private one the agent was built with: provenance and the
+ // correlation id are the client's handle on the session, and an id
+ // nothing else can name is not a handle.
+ agent.args.session_id = Some(sid.clone());
let session = Session { agent, history: Vec::new(), id: sid.clone(), turns: 0, ephemeral: start.ephemeral, builtins: start.builtins };
let mut result = tools_view(&session).await;
result["sessionId"] = json!(sid);
@@ -640,8 +651,10 @@ pub async fn serve() -> Result<(), String> {
// A fresh registry (from the rebuild): the fork is its
// own session, and a reply must never cross into it.
match rebuild_agent(source, None) {
- Ok(agent) => {
+ Ok(mut agent) => {
let fork = uuid::Uuid::new_v4().to_string();
+ // The fork is its own session, so it reports its own id.
+ agent.args.session_id = Some(fork.clone());
let history = source.history.clone();
let ephemeral = source.ephemeral;
let builtins = source.builtins;