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.

feat(cli): flush telemetry on Ctrl-C/SIGTERM and close archived RPC sessions

thinhlpge32df06fa2dd
diff --git a/src-tauri/jan-cli/src/main.rs b/src-tauri/jan-cli/src/main.rs
index 2d4c092e5..41c9ba4f1 100644
--- a/src-tauri/jan-cli/src/main.rs
+++ b/src-tauri/jan-cli/src/main.rs
@@ -989,7 +989,20 @@ async fn handle_cli(cmd: CliCommands) {
// ── Agent handlers ───────────────────────────────────────────────────────
async fn handle_agent(cmd: AgentCommands) {
- let result = match cmd {
+ // A Ctrl-C or SIGTERM would otherwise end the process with the run's last
+ // records still in the exporter's queue.
+ let result = app_lib::core::agent::otel::flush_on_termination(run_agent(cmd)).await;
+ // Before the exit below, which would skip it: the last run's records are
+ // still in the exporter's queue. Bounded, and a no-op unless telemetry is on.
+ app_lib::core::agent::otel::shutdown().await;
+ if let Err(e) = result {
+ eprintln!("Error: {e}");
+ std::process::exit(exit_code(&e));
+ }
+}
+
+async fn run_agent(cmd: AgentCommands) -> Result<(), String> {
+ match cmd {
AgentCommands::Run {
project,
task,
@@ -1065,13 +1078,6 @@ async fn handle_agent(cmd: AgentCommands) {
// artifact is the same one on any machine. `--out` is what CI and
// `make protocol-rpc-schema` use.
AgentCommands::RpcSchema { out } => app_lib::core::cli::rpc_schema::run(out.as_deref()),
- };
- // Before the exit below, which would skip it: the last run's records are
- // still in the exporter's queue. Bounded, and a no-op unless telemetry is on.
- app_lib::core::agent::otel::shutdown().await;
- if let Err(e) = result {
- eprintln!("Error: {e}");
- std::process::exit(exit_code(&e));
}
}
diff --git a/src-tauri/src/core/cli/rpc.rs b/src-tauri/src/core/cli/rpc.rs
index 9300bb7fe..bfce65b60 100644
--- a/src-tauri/src/core/cli/rpc.rs
+++ b/src-tauri/src/core/cli/rpc.rs
@@ -740,7 +740,11 @@ pub async fn serve() -> Result<(), String> {
Ok(SessionIdParams { session_id: sid }) if active.as_ref().is_some_and(|t| t.session_id == sid) => {
error(&id, -32001, "session busy")
}
- Ok(SessionIdParams { session_id: sid }) if sessions.remove(&sid).is_some() => response(&id, json!({})),
+ Ok(SessionIdParams { session_id: sid }) if sessions.remove(&sid).is_some() => {
+ // Its metric series stop growing a long-lived host's export.
+ crate::core::agent::otel::session_closed(&sid);
+ response(&id, json!({}))
+ }
Ok(_) => error(&id, -32602, "unknown sessionId"),
},
"turn/start" => {