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): --resume / --continue (-c) flags to resume agent sessions

Akarshan Biswaseed422057a05
diff --git a/src-tauri/src/bin/jan.rs b/src-tauri/src/bin/jan.rs
index 720f4e16b..55d643e25 100644
--- a/src-tauri/src/bin/jan.rs
+++ b/src-tauri/src/bin/jan.rs
@@ -13,7 +13,7 @@ use app_lib::core::cli::providers::{load_provider_configs, ProviderOverrides};
use app_lib::core::cli::{
cli_agent_config_list, cli_agent_config_path, cli_agent_config_set, cli_agent_config_unset,
cli_agent_run, cli_agent_status, cli_agent_step, cli_agent_ui, cli_delete_thread,
- cli_get_thread, cli_list_messages, cli_list_threads,
+ cli_get_thread, cli_list_messages, cli_list_threads, ResumeTarget,
};
// ── Top-level CLI ──────────────────────────────────────────────────────────
@@ -32,6 +32,8 @@ Models are served by remote providers configured in ~/.jan/config.toml\n\
jan # open the interactive agent console (TUI)\n \
jan --yolo # TUI with every tool call auto-approved\n \
jan --task \"fix the failing test\" # seed the TUI with a first message\n \
+ jan -c # resume the most recent session\n \
+ jan --resume 3f7a91c2 # resume a session by id (or id prefix)\n \
jan cli agent run \"fix the failing test\" # run the agent non-interactively\n \
jan cli models list # show every configured provider model\n \
jan cli threads list # list saved conversation threads",
@@ -61,6 +63,46 @@ struct Cli {
/// TUI (no prompts). Ignored when a subcommand is given.
#[arg(long)]
yolo: bool,
+ #[command(flatten)]
+ resume: ResumeArgs,
+}
+
+/// Session-resume selection, shared by the bare TUI and `jan cli agent run`.
+/// Threads are per-project (`<project>/.jan/agent/threads`), so resuming from a
+/// different working directory simply finds nothing there.
+#[derive(Args)]
+struct ResumeArgs {
+ /// Resume a saved session: the most recent one, or the thread whose id starts with ID
+ #[arg(long, num_args = 0..=1, value_name = "ID")]
+ resume: Option<Option<String>>,
+ /// Resume the most recent session (alias for a bare --resume)
+ #[arg(long = "continue", short = 'c', conflicts_with = "resume")]
+ continue_session: bool,
+}
+
+impl ResumeArgs {
+ fn into_target(self) -> Option<ResumeTarget> {
+ ResumeTarget::from_flags(self.resume, self.continue_session)
+ }
+}
+
+/// Same flags for `jan cli agent run`, which has a required positional TASK: a
+/// space-separated `--resume ID` would swallow the task, so the value form must
+/// be written `--resume=ID`.
+#[derive(Args)]
+struct ResumeRunArgs {
+ /// Resume a saved session: the most recent one, or (as --resume=ID) the thread whose id starts with ID
+ #[arg(long, num_args = 0..=1, require_equals = true, value_name = "ID")]
+ resume: Option<Option<String>>,
+ /// Resume the most recent session (alias for a bare --resume)
+ #[arg(long = "continue", short = 'c', conflicts_with = "resume")]
+ continue_session: bool,
+}
+
+impl ResumeRunArgs {
+ fn into_target(self) -> Option<ResumeTarget> {
+ ResumeTarget::from_flags(self.resume, self.continue_session)
+ }
}
/// Top-level commands. Bare `jan` opens the interactive TUI; everything else
@@ -153,6 +195,8 @@ enum AgentCommands {
yolo: bool,
#[command(flatten)]
providers: ProviderArgs,
+ #[command(flatten)]
+ resume: ResumeRunArgs,
},
/// Run a single turn (debugging)
Step {
@@ -318,6 +362,7 @@ async fn main() {
cli.images,
overrides,
cli.yolo,
+ cli.resume.into_target(),
)
.await
{
@@ -359,6 +404,7 @@ async fn handle_agent(cmd: AgentCommands) {
max_turns,
yolo,
providers,
+ resume,
} => {
cli_agent_run(
&project,
@@ -367,6 +413,7 @@ async fn handle_agent(cmd: AgentCommands) {
max_turns,
providers.into_overrides(),
yolo,
+ resume.into_target(),
)
.await
}
diff --git a/src-tauri/src/core/cli/mod.rs b/src-tauri/src/core/cli/mod.rs
index f44756856..2d5358ece 100644
--- a/src-tauri/src/core/cli/mod.rs
+++ b/src-tauri/src/core/cli/mod.rs
@@ -54,6 +54,111 @@ pub async fn cli_list_threads() -> Result<Vec<serde_json::Value>, String> {
list_threads_in(&data_folder)
}
+/// Which saved thread a `--resume` / `--continue` / `/resume` request refers to.
+#[derive(Clone, Debug, PartialEq, Eq)]
+pub enum ResumeTarget {
+ /// Most recently updated thread for the project.
+ Latest,
+ /// A full thread id or a unique prefix of one.
+ Id(String),
+}
+
+impl ResumeTarget {
+ /// Build a target from the CLI flag pair: `--resume [ID]` and `--continue`/`-c`
+ /// (an alias for a bare `--resume`). `None` means "do not resume".
+ pub fn from_flags(resume: Option<Option<String>>, continue_session: bool) -> Option<Self> {
+ match resume {
+ Some(Some(id)) if !id.trim().is_empty() => Some(Self::Id(id.trim().to_string())),
+ Some(_) => Some(Self::Latest),
+ None if continue_session => Some(Self::Latest),
+ None => None,
+ }
+ }
+}
+
+/// Recency sort key for a saved thread (`updated`, falling back to `created`).
+pub fn thread_recency(t: &serde_json::Value) -> f64 {
+ t.get("updated")
+ .or_else(|| t.get("created"))
+ .and_then(serde_json::Value::as_f64)
+ .unwrap_or(0.0)
+}
+
+/// Sort threads most-recent-first (by `updated`/`created`).
+pub fn sort_threads_recent(threads: &mut [serde_json::Value]) {
+ threads.sort_by(|a, b| {
+ thread_recency(b)
+ .partial_cmp(&thread_recency(a))
+ .unwrap_or(std::cmp::Ordering::Equal)
+ });
+}
+
+/// Message shown when there is nothing to resume; the caller then starts fresh.
+pub const NO_SESSION_TO_RESUME: &str = "No session to resume";
+
+/// Resolve a resume target against `<base>/threads/`, returning the thread
+/// metadata. Threads whose `thread.json` is unparsable are skipped by
+/// `list_threads_in`, so a corrupted neighbour never blocks a resume.
+pub fn find_resume_thread(
+ base: &std::path::Path,
+ target: &ResumeTarget,
+) -> Result<serde_json::Value, String> {
+ let mut threads = list_threads_in(base)?;
+ match target {
+ ResumeTarget::Latest => {
+ sort_threads_recent(&mut threads);
+ threads
+ .into_iter()
+ .next()
+ .ok_or_else(|| NO_SESSION_TO_RESUME.to_string())
+ }
+ ResumeTarget::Id(id) => {
+ let mut matches: Vec<serde_json::Value> = threads
+ .into_iter()
+ .filter(|t| {
+ t.get("id")
+ .and_then(|v| v.as_str())
+ .is_some_and(|full| full == id || full.starts_with(id.as_str()))
+ })
+ .collect();
+ match matches.len() {
+ 0 => Err(format!("no thread matches '{id}'")),
+ 1 => Ok(matches.remove(0)),
+ n => Err(format!("'{id}' is ambiguous ({n} matches)")),
+ }
+ }
+ }
+}
+
+/// Read a thread's messages, tolerating a truncated or malformed line (a crash
+/// mid-append leaves one). Returns the parsed records and the skipped count, so
+/// a resume degrades to "lost the tail" instead of failing outright.
+pub fn cli_read_messages_lenient(
+ base: &std::path::Path,
+ thread_id: &str,
+) -> Result<(Vec<serde_json::Value>, usize), String> {
+ use std::io::BufRead;
+
+ let path = get_messages_path(base, thread_id);
+ if !path.exists() {
+ return Ok((Vec::new(), 0));
+ }
+ let file = std::fs::File::open(&path).map_err(|e| e.to_string())?;
+ let mut messages = Vec::new();
+ let mut skipped = 0;
+ for line in std::io::BufReader::new(file).lines() {
+ let line = line.map_err(|e| e.to_string())?;
+ if line.trim().is_empty() {
+ continue;
+ }
+ match serde_json::from_str(&line) {
+ Ok(v) => messages.push(v),
+ Err(_) => skipped += 1,
+ }
+ }
+ Ok((messages, skipped))
+}
+
/// Read a thread's messages from `<base>/threads/<id>/messages.jsonl`.
pub fn cli_list_messages_in(
base: &std::path::Path,
@@ -180,6 +285,27 @@ pub fn cli_set_project_model(agent_dir: &std::path::Path, model: &str) -> Result
set_model_in_agent_toml(&agent_dir.join("agent.toml"), model)
}
+/// Text of a persisted `thread.message` (content parts carry `text.value`) or of
+/// an OpenAI-shaped message (`content` is a plain string or `text` parts), so
+/// the same reader works on both sides of a save/resume round trip.
+pub(crate) fn thread_message_text(msg: &serde_json::Value) -> String {
+ match msg.get("content") {
+ Some(serde_json::Value::String(s)) => s.clone(),
+ Some(serde_json::Value::Array(parts)) => parts
+ .iter()
+ .filter_map(|p| {
+ p.get("text")
+ .and_then(|t| t.get("value"))
+ .and_then(|v| v.as_str())
+ .or_else(|| p.get("text").and_then(|t| t.as_str()))
+ .or_else(|| p.as_str())
+ })
+ .collect::<Vec<_>>()
+ .join(""),
+ _ => String::new(),
+ }
+}
+
/// Text of an OpenAI-shaped message `content`: the string as-is, or the joined
/// `text` parts of a multimodal content array (image parts contribute nothing).
fn openai_content_text(content: Option<&serde_json::Value>) -> String {
@@ -367,8 +493,9 @@ pub async fn cli_agent_run(
max_turns: Option<u32>,
overrides: ProviderOverrides,
yolo: bool,
+ resume: Option<ResumeTarget>,
) -> Result<(), String> {
- run_agent_loop(project, task, model, max_turns, overrides, yolo).await
+ run_agent_loop(project, task, model, max_turns, overrides, yolo, resume).await
}
/// Single-turn run for debugging (`max_turns = 1`).
@@ -379,7 +506,7 @@ pub async fn cli_agent_step(
overrides: ProviderOverrides,
yolo: bool,
) -> Result<(), String> {
- run_agent_loop(project, task, model, Some(1), overrides, yolo).await
+ run_agent_loop(project, task, model, Some(1), overrides, yolo, None).await
}
#[allow(clippy::too_many_arguments)]
@@ -416,6 +543,17 @@ pub(crate) struct PreparedRun {
pub permission_requests: PermissionRegistry,
/// Background connect of `active` MCP servers, awaited before the first turn.
pub mcp_task: Option<tokio::task::JoinHandle<Vec<String>>>,
+ /// Where to write the conversation once the run finishes.
+ persist: PersistTarget,
+}
+
+/// Bookkeeping for writing a non-interactive run to the project's thread store,
+/// so `--resume` can pick it up later. `thread_id` is `None` for a new session.
+struct PersistTarget {
+ agent_dir: PathBuf,
+ thread_id: Option<String>,
+ model: String,
+ history: Vec<serde_json::Value>,
}
/// Resolved engine handle for a chat session: the args are built once and the
@@ -571,6 +709,44 @@ fn prepare_agent_session(
})
}
+/// The prior conversation a non-interactive `--resume` run continues, in
+/// OpenAI `{role, content}` shape (the wire format the engine expects).
+struct ResumedSession {
+ thread_id: String,
+ history: Vec<serde_json::Value>,
+}
+
+/// Load a saved thread's user/assistant turns for continuation. Tool calls are
+/// not replayed (they are not persisted as messages), matching `/resume` in the
+/// TUI. Errors describe why nothing could be resumed; the caller starts fresh.
+fn load_resume_history(
+ agent_dir: &std::path::Path,
+ target: &ResumeTarget,
+) -> Result<ResumedSession, String> {
+ let thread = find_resume_thread(agent_dir, target)?;
+ let thread_id = thread
+ .get("id")
+ .and_then(|v| v.as_str())
+ .ok_or_else(|| "saved thread has no id".to_string())?
+ .to_string();
+ let (messages, skipped) = cli_read_messages_lenient(agent_dir, &thread_id)?;
+ if skipped > 0 {
+ eprintln!("(skipped {skipped} unreadable message(s) in the resumed session)");
+ }
+ let history = messages
+ .iter()
+ .filter_map(|m| {
+ let role = m.get("role").and_then(|v| v.as_str())?;
+ if !matches!(role, "user" | "assistant") {
+ return None;
+ }
+ let text = thread_message_text(m);
+ (!text.is_empty()).then(|| serde_json::json!({ "role": role, "content": text }))
+ })
+ .collect();
+ Ok(ResumedSession { thread_id, history })
+}
+
fn prepare_agent_run(
project: &str,
task: &str,
@@ -578,6 +754,7 @@ fn prepare_agent_run(
max_turns_override: Option<u32>,
overrides: ProviderOverrides,
yolo: bool,
+ resume: Option<ResumeTarget>,
) -> Result<PreparedRun, String> {
let session =
prepare_agent_session(project, model_override, max_turns_override, overrides, yolo)?;
@@ -588,7 +765,24 @@ fn prepare_agent_run(
} else {
format!("{clean_task}\n\n---\nReferenced file contents:\n\n{injected}")
};
- let body = session.body(serde_json::json!([{ "role": "user", "content": final_task }]));
+
+ // A failed resume is not fatal: report it and run the prompt in a new session.
+ let resumed = resume.and_then(|target| {
+ match load_resume_history(&agent_dir_for(&project_root), &target) {
+ Ok(r) => {
+ eprintln!("(resumed session {} with {} message(s))", short_id(&r.thread_id), r.history.len());
+ Some(r)
+ }
+ Err(e) => {
+ eprintln!("{e}; starting a new session");
+ None
+ }
+ }
+ });
+
+ let mut history = resumed.as_ref().map(|r| r.history.clone()).unwrap_or_default();
+ history.push(serde_json::json!({ "role": "user", "content": final_task }));
+ let body = session.body(serde_json::json!(history.clone()));
// Emit resolved references stderr so the user sees what was injected
if !injected.is_empty() {
eprintln!("(resolved @path references)");
@@ -598,9 +792,22 @@ fn prepare_agent_run(
body,
permission_requests: session.permission_requests,
mcp_task: session.mcp_task,
+ // Non-interactive runs persist into the same per-project store the TUI
+ // uses, so a run can later be continued with --resume from either side.
+ persist: PersistTarget {
+ agent_dir: agent_dir_for(&project_root),
+ thread_id: resumed.map(|r| r.thread_id),
+ model: session.model,
+ history,
+ },
})
}
+/// First 8 chars of a thread id, the form the TUI shows in `/threads`.
+fn short_id(id: &str) -> String {
+ id.chars().take(8).collect()
+}
+
async fn run_agent_loop(
project: &str,
task: &str,
@@ -608,13 +815,23 @@ async fn run_agent_loop(
max_turns_override: Option<u32>,
overrides: ProviderOverrides,
yolo: bool,
+ resume: Option<ResumeTarget>,
) -> Result<(), String> {
let PreparedRun {
args,
body,
permission_requests,
mcp_task,
- } = prepare_agent_run(project, task, model_override, max_turns_override, overrides, yolo)?;
+ persist,
+ } = prepare_agent_run(
+ project,
+ task,
+ model_override,
+ max_turns_override,
+ overrides,
+ yolo,
+ resume,
+ )?;
// Block until active MCP servers connect, so tools (collected once per run)
// are present on the first turn.
@@ -638,12 +855,36 @@ async fn run_agent_loop(
let result = run_orchestration_streamed(&tx, &body, &args).await;
drop(tx);
let _ = printer.await;
+
+ // Write the turn back so the session stays continuable with --resume.
+ if let Ok(completion) = result.as_ref() {
+ let PersistTarget { agent_dir, thread_id, model, mut history } = persist;
+ if let Some(text) = completion_text(completion) {
+ history.push(serde_json::json!({ "role": "assistant", "content": text }));
+ }
+ match cli_save_thread(&agent_dir, thread_id.as_deref(), &model, &history, None) {
+ Ok(id) => eprintln!("\x1b[2m[session {} - resume with `jan --resume={}`]\x1b[0m", short_id(&id), short_id(&id)),
+ Err(e) => eprintln!("(could not save session: {e})"),
+ }
+ }
result.map(|_| ())
}
+/// Assistant text of a chat-completion response, if any.
+fn completion_text(completion: &serde_json::Value) -> Option<String> {
+ let text = completion
+ .get("choices")?
+ .get(0)?
+ .get("message")?
+ .get("content")
+ .and_then(|v| v.as_str())?;
+ (!text.is_empty()).then(|| text.to_string())
+}
+
/// Launch the interactive chat console (bare `jan`). An optional `task`
/// seeds the first turn; otherwise the user types the first message. Shares the
/// engine with `run_agent_loop` via `AgentSession` — only presentation differs.
+#[allow(clippy::too_many_arguments)]
pub async fn cli_agent_ui(
project: &str,
task: Option<String>,
@@ -652,13 +893,19 @@ pub async fn cli_agent_ui(
images: Vec<String>,
overrides: ProviderOverrides,
yolo: bool,
+ resume: Option<ResumeTarget>,
) -> Result<(), String> {
let session = prepare_agent_session(project, model, max_turns, overrides, yolo)?;
// TUI threads persist under the project's .jan/agent dir, separate from the
// desktop store, so continuing here never mutates desktop threads.
let project_root = resolve_project_root(project);
- let agent_dir = project_root.join(".jan").join("agent");
- tui::run(session, agent_dir, project_root, task, images).await
+ let agent_dir = agent_dir_for(&project_root);
+ tui::run(session, agent_dir, project_root, task, images, resume).await
+}
+
+/// Where the TUI persists a project's threads (`<project>/.jan/agent`).
+pub fn agent_dir_for(project_root: &std::path::Path) -> PathBuf {
+ project_root.join(".jan").join("agent")
}
/// Render one `StreamEvent` for the terminal. Content tokens go to stdout so a
@@ -769,6 +1016,151 @@ async fn prompt_permission(
mod tests {
use super::*;
+ // ── resume ─────────────────────────────────────────────────────────────
+
+ #[test]
+ fn resume_target_from_flags() {
+ assert_eq!(ResumeTarget::from_flags(None, false), None);
+ assert_eq!(ResumeTarget::from_flags(None, true), Some(ResumeTarget::Latest));
+ assert_eq!(
+ ResumeTarget::from_flags(Some(None), false),
+ Some(ResumeTarget::Latest)
+ );
+ // A blank --resume value behaves like a bare --resume.
+ assert_eq!(
+ ResumeTarget::from_flags(Some(Some(" ".into())), false),
+ Some(ResumeTarget::Latest)
+ );
+ assert_eq!(
+ ResumeTarget::from_flags(Some(Some(" 3f7a ".into())), false),
+ Some(ResumeTarget::Id("3f7a".into()))
+ );
+ }
+
+ /// Write a thread with the given id/recency and a single user message.
+ fn seed_thread(base: &std::path::Path, id: &str, updated: f64) {
+ std::fs::create_dir_all(get_thread_dir(base, id)).unwrap();
+ std::fs::write(
+ get_thread_metadata_path(base, id),
+ serde_json::json!({ "id": id, "title": id, "updated": updated }).to_string(),
+ )
+ .unwrap();
+ std::fs::write(
+ get_messages_path(base, id),
+ serde_json::json!({
+ "role": "user",
+ "content": [{ "type": "text", "text": { "value": id, "annotations": [] } }],
+ })
+ .to_string()
+ + "\n",
+ )
+ .unwrap();
+ }
+
+ #[test]
+ fn find_resume_thread_latest_and_by_prefix() {
+ let dir = tempfile::tempdir().unwrap();
+ let base = dir.path();
+ assert_eq!(
+ find_resume_thread(base, &ResumeTarget::Latest).unwrap_err(),
+ NO_SESSION_TO_RESUME
+ );
+
+ seed_thread(base, "aaaa1111", 100.0);
+ seed_thread(base, "bbbb2222", 300.0);
+ seed_thread(base, "bbbb3333", 200.0);
+
+ let latest = find_resume_thread(base, &ResumeTarget::Latest).unwrap();
+ assert_eq!(latest["id"], "bbbb2222");
+
+ let by_prefix = find_resume_thread(base, &ResumeTarget::Id("aaaa".into())).unwrap();
+ assert_eq!(by_prefix["id"], "aaaa1111");
+
+ assert!(find_resume_thread(base, &ResumeTarget::Id("zz".into()))
+ .unwrap_err()
+ .contains("no thread matches"));
+ assert!(find_resume_thread(base, &ResumeTarget::Id("bbbb".into()))
+ .unwrap_err()
+ .contains("ambiguous"));
+ }
+
+ #[test]
+ fn find_resume_thread_skips_corrupted_metadata() {
+ let dir = tempfile::tempdir().unwrap();
+ let base = dir.path();
+ seed_thread(base, "good1111", 100.0);
+ let bad = "bad02222";
+ std::fs::create_dir_all(get_thread_dir(base, bad)).unwrap();
+ std::fs::write(get_thread_metadata_path(base, bad), "{not json").unwrap();
+
+ let latest = find_resume_thread(base, &ResumeTarget::Latest).unwrap();
+ assert_eq!(latest["id"], "good1111");
+ }
+
+ #[test]
+ fn read_messages_lenient_skips_truncated_tail() {
+ let dir = tempfile::tempdir().unwrap();
+ let base = dir.path();
+ seed_thread(base, "aaaa1111", 100.0);
+ let mut raw = std::fs::read_to_string(get_messages_path(base, "aaaa1111")).unwrap();
+ raw.push_str("{\"role\":\"assist");
+ std::fs::write(get_messages_path(base, "aaaa1111"), raw).unwrap();
+
+ let (messages, skipped) = cli_read_messages_lenient(base, "aaaa1111").unwrap();
+ assert_eq!(messages.len(), 1);
+ assert_eq!(skipped, 1);
+ // The strict reader used elsewhere still rejects the same file.
+ assert!(cli_list_messages_in(base, "aaaa1111").is_err());
+ }
+
+ #[test]
+ fn read_messages_lenient_on_missing_thread_is_empty() {
+ let dir = tempfile::tempdir().unwrap();
+ let (messages, skipped) = cli_read_messages_lenient(dir.path(), "nope").unwrap();
+ assert!(messages.is_empty());
+ assert_eq!(skipped, 0);
+ }
+
+ #[test]
+ fn resume_cycle_preserves_thread_id_and_history() {
+ let dir = tempfile::tempdir().unwrap();
+ let base = dir.path();
+ let history = vec![
+ serde_json::json!({ "role": "user", "content": "first" }),
+ serde_json::json!({ "role": "assistant", "content": "reply" }),
+ ];
+ let id = cli_save_thread(base, None, "m", &history, None).unwrap();
+
+ let resumed = load_resume_history(base, &ResumeTarget::Latest).unwrap();
+ assert_eq!(resumed.thread_id, id);
+ assert_eq!(resumed.history, history);
+
+ // Continue the session and save back: same thread, appended turns.
+ let mut extended = resumed.history;
+ extended.push(serde_json::json!({ "role": "user", "content": "second" }));
+ let same = cli_save_thread(base, Some(&id), "m", &extended, None).unwrap();
+ assert_eq!(same, id);
+ assert_eq!(list_threads_in(base).unwrap().len(), 1);
+ assert_eq!(
+ load_resume_history(base, &ResumeTarget::Id(id[..8].to_string()))
+ .unwrap()
+ .history,
+ extended
+ );
+ }
+
+ #[test]
+ fn completion_text_extracts_assistant_content() {
+ let completion =
+ serde_json::json!({ "choices": [{ "message": { "content": "hello" } }] });
+ assert_eq!(completion_text(&completion).as_deref(), Some("hello"));
+ assert_eq!(completion_text(&serde_json::json!({})), None);
+ assert_eq!(
+ completion_text(&serde_json::json!({ "choices": [{ "message": { "content": "" } }] })),
+ None
+ );
+ }
+
// ── default_thread_title ───────────────────────────────────────────────
#[test]
diff --git a/src-tauri/src/core/cli/tui.rs b/src-tauri/src/core/cli/tui.rs
index b537ae8ab..e3f29bcd7 100644
--- a/src-tauri/src/core/cli/tui.rs
+++ b/src-tauri/src/core/cli/tui.rs
@@ -28,7 +28,7 @@ use ratatui::widgets::{Block, Borders, Paragraph, Wrap};
use tokio::sync::mpsc;
use tokio::task::JoinHandle;
-use super::AgentSession;
+use super::{sort_threads_recent, AgentSession, ResumeTarget};
use crate::core::agent::events::{describe_tool_call, StreamEvent, Usage};
use crate::core::agent::git;
use crate::core::agent::r#loop::{run_orchestration_streamed, OrchestrationArgs, PermissionRegistry};
@@ -2299,6 +2299,7 @@ pub async fn run(
project_root: PathBuf,
initial_task: Option<String>,
initial_images: Vec<String>,
+ resume: Option<ResumeTarget>,
) -> Result<(), String> {
let AgentSession {
args,
@@ -2330,6 +2331,14 @@ pub async fn run(
if args.yolo {
app.note("--yolo: sandbox disabled, all tool calls auto-approved without prompting");
}
+ // A failed resume is not fatal: the note explains why and the blank session
+ // the user already has stays usable.
+ if let Some(target) = &resume {
+ apply_resume(&mut app, target);
+ if app.thread_id.is_none() {
+ app.note("starting a new session");
+ }
+ }
for path in &initial_images {
match load_image_file(path) {
Ok(img) => app.pending_images.push(img),
@@ -3266,23 +3275,6 @@ fn thread_display_name(base: &std::path::Path, id: &str, title: Option<&str>) ->
"(untitled)".to_string()
}
-/// Recency sort key for a saved thread (`updated`, falling back to `created`).
-fn thread_recency(t: &serde_json::Value) -> f64 {
- t.get("updated")
- .or_else(|| t.get("created"))
- .and_then(|v| v.as_f64())
- .unwrap_or(0.0)
-}
-
-/// Sort threads most-recent-first (by `updated`/`created`).
-fn sort_threads_recent(threads: &mut [serde_json::Value]) {
- threads.sort_by(|a, b| {
- thread_recency(b)
- .partial_cmp(&thread_recency(a))
- .unwrap_or(std::cmp::Ordering::Equal)
- });
-}
-
/// Load saved threads and open the interactive picker (Enter selects). Falls
/// back to a note when there is nothing to resume.
fn open_thread_picker(app: &mut App) {
@@ -3624,26 +3616,25 @@ fn rebuild_transcript(app: &mut App) {
}
fn resume_thread(app: &mut App, id_arg: &str) {
- let threads = match super::list_threads_in(&app.agent_dir) {
- Ok(t) => t,
- Err(e) => return app.note(&format!("failed to list threads: {e}")),
- };
- let matches: Vec<&serde_json::Value> = threads
- .iter()
- .filter(|t| {
- t.get("id")
- .and_then(|v| v.as_str())
- .is_some_and(|full| full == id_arg || full.starts_with(id_arg))
- })
- .collect();
- let thread = match matches.as_slice() {
- [] => return app.note(&format!("no thread matches '{id_arg}'")),
- [t] => *t,
- _ => return app.note(&format!("'{id_arg}' is ambiguous ({} matches)", matches.len())),
- };
+ apply_resume(app, &ResumeTarget::Id(id_arg.to_string()));
+}
+
+/// Resolve a resume target and load it into the app, reporting why not when it
+/// cannot be resolved. The session is left untouched on failure.
+fn apply_resume(app: &mut App, target: &ResumeTarget) {
+ match super::find_resume_thread(&app.agent_dir, target) {
+ Ok(thread) => load_thread(app, &thread),
+ Err(e) => app.note(&e),
+ }
+}
+
+/// Replace the live session with a saved thread's state: history, transcript,
+/// snapshots, goal, and model. Only user/assistant text is replayed (tool calls
+/// are not persisted as messages).
+fn load_thread(app: &mut App, thread: &serde_json::Value) {
let full_id = thread.get("id").and_then(|v| v.as_str()).unwrap_or_default();
- let messages = match super::cli_list_messages_in(&app.agent_dir, full_id) {
+ let (messages, skipped) = match super::cli_read_messages_lenient(&app.agent_dir, full_id) {
Ok(m) => m,
Err(e) => return app.note(&format!("failed to load messages: {e}")),
};
@@ -3698,6 +3689,9 @@ fn resume_thread(app: &mut App, id_arg: &str) {
.filter(|s| !s.is_empty())
.unwrap_or("(untitled)");
app.note(&format!("resumed \"{title}\" ({count} messages)"));
+ if skipped > 0 {
+ app.note(&format!("{skipped} unreadable message(s) were skipped"));
+ }
}
/// Infer an image MIME type from a file extension, defaulting to PNG.
@@ -3844,21 +3838,7 @@ fn user_content_parts(content: &serde_json::Value) -> (String, Vec<String>) {
/// Extract plain text from a stored thread message (`content` is an array of
/// `{type,text:{value}}` parts, or occasionally a bare string).
fn message_text(msg: &serde_json::Value) -> String {
- match msg.get("content") {
- Some(serde_json::Value::String(s)) => s.clone(),
- Some(serde_json::Value::Array(parts)) => parts
- .iter()
- .filter_map(|p| {
- p.get("text")
- .and_then(|t| t.get("value"))
- .and_then(|v| v.as_str())
- .or_else(|| p.get("text").and_then(|t| t.as_str()))
- .or_else(|| p.as_str())
- })
- .collect::<Vec<_>>()
- .join(""),
- _ => String::new(),
- }
+ super::thread_message_text(msg)
}
/// Whitespace-collapsed, char-truncated one-liner for checkpoint/rewind labels.
@@ -4614,7 +4594,8 @@ mod tests {
restore_goal, running_group_row, split_reasoning, subagent_activity,
subagent_name_from_run_id, summarize_result, tool_activity, tool_finished,
transcript_top_padding, user_content_parts, App, CurrentRun, Pending, PendingImage,
- PickerKind, SnapshotJob, Status, DIFF_MAX_ROWS, SLASH_COMMANDS, SPINNER,
+ apply_resume, PickerKind, ResumeTarget, SnapshotJob, Status, DIFF_MAX_ROWS,
+ SLASH_COMMANDS, SPINNER,
};
use ratatui::crossterm::event::{
KeyCode, KeyEvent, KeyModifiers, MouseButton, MouseEvent, MouseEventKind,
@@ -6529,6 +6510,56 @@ mod tests {
assert_eq!(summarize_result("abcdefgh", 4), "abc…");
}
+ #[test]
+ fn apply_resume_latest_restores_history_and_model() {
+ let mut app = test_app();
+ let history = vec![
+ json!({ "role": "user", "content": "first" }),
+ json!({ "role": "assistant", "content": "reply" }),
+ ];
+ let id =
+ super::super::cli_save_thread(&app.agent_dir, None, "saved-model", &history, None)
+ .unwrap();
+
+ let mut fresh = test_app();
+ fresh.agent_dir = app.agent_dir.clone();
+ apply_resume(&mut fresh, &ResumeTarget::Latest);
+
+ assert_eq!(fresh.thread_id.as_deref(), Some(id.as_str()));
+ assert_eq!(fresh.history, history);
+ assert_eq!(fresh.model, "saved-model");
+ let joined: String = fresh
+ .transcript
+ .iter()
+ .map(|l| line_text(l))
+ .collect::<Vec<_>>()
+ .join("\n");
+ assert!(joined.contains("first") && joined.contains("reply"), "{joined}");
+
+ // Re-saving the resumed session updates the same thread, not a new one.
+ app.agent_dir = fresh.agent_dir.clone();
+ let same =
+ super::super::cli_save_thread(&app.agent_dir, Some(&id), "saved-model", &fresh.history, None)
+ .unwrap();
+ assert_eq!(same, id);
+ assert_eq!(super::super::list_threads_in(&app.agent_dir).unwrap().len(), 1);
+ }
+
+ #[test]
+ fn apply_resume_notes_when_nothing_to_resume() {
+ let mut app = test_app();
+ apply_resume(&mut app, &ResumeTarget::Latest);
+ let joined: String = app
+ .transcript
+ .iter()
+ .map(|l| line_text(l))
+ .collect::<Vec<_>>()
+ .join("\n");
+ assert!(joined.contains(super::super::NO_SESSION_TO_RESUME), "{joined}");
+ assert!(app.thread_id.is_none());
+ assert!(app.history.is_empty());
+ }
+
#[test]
fn sort_threads_recent_orders_newest_first() {
let mut threads = vec![