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(tui,agent): stop finished todo lists lingering, and stop planning trivial work (#8574)

Thinh Le96778ce31888
diff --git a/src-tauri/src/core/agent/context.rs b/src-tauri/src/core/agent/context.rs
index e4bfadd69..1b601b2c9 100644
--- a/src-tauri/src/core/agent/context.rs
+++ b/src-tauri/src/core/agent/context.rs
@@ -18,7 +18,7 @@ running commands, editing code, and writing new files.";
/// Always-on behavioral guidelines. Kept short and model-facing.
const GUIDELINES: &str =
"# Guidelines\n\n- Be concise in your responses.\n- Show file paths clearly when working with files.\n\
-- For substantive work with multiple steps or repository changes, call `todo` to initialize a concise plan before beginning, then keep it current as tasks start, finish, or are abandoned. Do not create todos for greetings, simple questions, or one-step requests.\n\
+- Reach for `todo` only when work genuinely needs tracking: several independent steps, or a task long enough that you or the user would otherwise lose the thread. When you do keep it current as tasks start, finish, or are abandoned. Most requests do not need one -- greetings, questions, single-file edits, and anything you can finish in a step or two are better done directly, and a plan for small work is noise the user has to read past.\n\
- Call `ask` when the user's answer would materially change scope, behavior, or an irreversible action and it cannot be safely inferred from the request or project context. Ask concise, decision-ready questions; otherwise make the reasonable choice and proceed.\n\
- Tool output is complete and verbatim. Trust it. Do not re-run a command to check for hidden or \
missing output: when output is cut it always carries an explicit `[output truncated ...]` notice, so \
@@ -332,8 +332,8 @@ mod tests {
assert!(out.starts_with("You are Jan, an AI coding agent."));
assert!(out.contains("# Guidelines"));
assert!(out.contains("Be concise"));
- assert!(out.contains("call `todo` to initialize a concise plan"));
- assert!(out.contains("Do not create todos for greetings"));
+ assert!(out.contains("Reach for `todo` only when work genuinely needs tracking"));
+ assert!(out.contains("Most requests do not need one"));
assert!(out.contains("Call `ask` when the user's answer would materially change"));
assert!(out.contains("Tool output is complete and verbatim"));
assert!(out.contains("Do not re-run a command to check"));
diff --git a/src-tauri/src/core/agent/todo.rs b/src-tauri/src/core/agent/todo.rs
index cbd7c951a..dd38bcfce 100644
--- a/src-tauri/src/core/agent/todo.rs
+++ b/src-tauri/src/core/agent/todo.rs
@@ -67,6 +67,17 @@ impl TodoList {
self.phases.iter().all(|p| p.tasks.is_empty())
}
+ /// True while any task is still pending or in progress.
+ ///
+ /// Distinct from `is_empty`, which only asks whether tasks exist at all: a
+ /// fully completed list is non-empty but has no open work left.
+ pub fn has_open(&self) -> bool {
+ self.phases
+ .iter()
+ .flat_map(|p| &p.tasks)
+ .any(|t| matches!(t.status, TodoStatus::Pending | TodoStatus::InProgress))
+ }
+
pub fn done_total(&self) -> (usize, usize) {
let mut done = 0;
let mut total = 0;
diff --git a/src-tauri/src/core/cli/tui.rs b/src-tauri/src/core/cli/tui.rs
index 6b896c2e2..481d2afb1 100644
--- a/src-tauri/src/core/cli/tui.rs
+++ b/src-tauri/src/core/cli/tui.rs
@@ -583,6 +583,12 @@ struct App {
scrollback: u16,
/// Set when the user submits a message; the loop spawns a run next tick.
want_start: bool,
+ /// Runs started since the todo list last had open work. The list is the
+ /// model's own scratchpad, so a finished one lingers until it declares a
+ /// new plan -- which may never happen. This counts down a grace period
+ /// instead of clearing the moment the last task closes, so a model that is
+ /// still mid-flow (about to append, or reopening a task) keeps its list.
+ runs_since_todos_closed: u32,
/// Transcript viewport width in cells, refreshed each `draw`; tables wrap to
/// it. 0 until the first draw (callers fall back to a default).
view_width: u16,
@@ -802,6 +808,7 @@ impl App {
update_installing: false,
scrollback: 0,
want_start: false,
+ runs_since_todos_closed: 0,
view_width: 0,
last_kind: Kind::None,
should_quit: false,
@@ -3370,6 +3377,7 @@ async fn chat_loop<B: Backend>(
let base_ready = app.repo_root.is_none() || app.base_snapshot.is_some();
if mcp_ready && base_ready {
app.want_start = false;
+ age_closed_todos(app).await;
current = Some(spawn_run(args, app.body()));
} else if !loading_noted && !mcp_ready {
// The base snapshot gates silently; only the MCP connect notes.
@@ -4302,10 +4310,12 @@ async fn run_command(app: &mut App, line: &str) {
}
"clear" => {
app.reset_session();
+ clear_todos(app).await;
app.note("conversation cleared");
}
"new" => {
app.reset_session();
+ clear_todos(app).await;
app.note("started a new session");
}
"compact" => compact_command(app).await,
@@ -4486,6 +4496,49 @@ async fn apply_todo_mutation(
Ok(())
}
+/// Runs allowed to start with a fully closed-out todo list before it is
+/// dropped. One is too eager -- a model often closes the last task and then
+/// appends follow-up work in the next run, and clearing between the two would
+/// destroy a list it is still using.
+const TODO_KEEP_CLOSED_RUNS: u32 = 2;
+
+/// Clear the canonical todo list and the TUI projection together.
+///
+/// Goes through `apply_todo_mutation` so the registry -- the model's source of
+/// truth -- is cleared too. Dropping only the projection would leave the model
+/// appending to a list the user believes is gone, and the next `TodoUpdate`
+/// would resurrect it on screen.
+async fn clear_todos(app: &mut App) {
+ // `Target::All` clears unconditionally; the Result exists for the
+ // unknown-task and unknown-phase targets.
+ let _ = apply_todo_mutation(app, |list| {
+ list.rm(crate::core::agent::todo::Target::All)
+ })
+ .await;
+ app.last_todo_reminder = None;
+ app.runs_since_todos_closed = 0;
+}
+
+/// Age a finished todo list, dropping it once it has survived
+/// `TODO_KEEP_CLOSED_RUNS` runs without the model reopening or extending it.
+///
+/// Without this the widget is sticky: `is_empty` means "no tasks exist", and a
+/// completed task is still a task, so a finished plan renders forever and only
+/// a fresh `todo init` ever replaces it. Users end up reading a plan that
+/// belongs to work they finished several tasks ago.
+async fn age_closed_todos(app: &mut App) {
+ if app.todos.is_empty() || app.todos.has_open() {
+ // Nothing to age, or the model still has open work.
+ app.runs_since_todos_closed = 0;
+ return;
+ }
+ app.runs_since_todos_closed += 1;
+ if app.runs_since_todos_closed > TODO_KEEP_CLOSED_RUNS {
+ clear_todos(app).await;
+ app.persist();
+ }
+}
+
/// Build the `/todo` editor rows: one per task, in phase/task order, prefixed by
/// a status glyph. `value` is the task content (its stable mutation id).
fn build_todo_items(todos: &crate::core::agent::todo::TodoList) -> Vec<PickerItem> {
@@ -6662,7 +6715,8 @@ fn footer(app: &App) -> Paragraph<'static> {
#[cfg(test)]
mod tests {
use super::{
- apply_resume, assistant_is_awaiting_user_answer, build_user_message, clipboard_path,
+ age_closed_todos, apply_resume, assistant_is_awaiting_user_answer, build_user_message,
+ clipboard_path,
diff_lines, group_activity, group_detail_lines, group_summary, handle_ask_key,
handle_ask_mouse, handle_key, handle_mouse, image_mime, input_content_lines,
compact_tokens, finish_login, finish_update_install, load_image_file, message_text,
@@ -9693,6 +9747,87 @@ mod tests {
assert_eq!(app.turn_output_tokens, 0);
}
+ fn todo_item(content: &str) -> crate::core::agent::todo::TodoItem {
+ crate::core::agent::todo::TodoItem {
+ content: content.to_string(),
+ status: crate::core::agent::todo::TodoStatus::Pending,
+ }
+ }
+
+ #[tokio::test]
+ async fn finished_todos_clear_after_a_grace_period() {
+ let mut app = test_app();
+ app.todos
+ .init(vec![crate::core::agent::todo::TodoPhase {
+ name: String::new(),
+ tasks: vec![
+ todo_item("write the parser"),
+ ],
+ }])
+ .unwrap();
+ assert!(!app.todos.is_empty());
+
+ // Open work is never aged away, however many runs go by.
+ for _ in 0..5 {
+ age_closed_todos(&mut app).await;
+ }
+ assert!(!app.todos.is_empty(), "open work must survive");
+
+ app.todos
+ .done(crate::core::agent::todo::Target::All)
+ .unwrap();
+ assert!(app.todos.open_summary().is_none(), "all work closed out");
+
+ // Survives every run up to the cutoff.
+ for i in 1..=super::TODO_KEEP_CLOSED_RUNS {
+ age_closed_todos(&mut app).await;
+ assert!(!app.todos.is_empty(), "cleared too eagerly at run {i}");
+ }
+
+ // One run past the grace period and it goes.
+ age_closed_todos(&mut app).await;
+ assert!(app.todos.is_empty(), "finished list should have been dropped");
+ }
+
+ /// Reopening work resets the grace period, so a list in active use is never
+ /// aged out from under the model.
+ #[tokio::test]
+ async fn reopened_todos_reset_the_grace_period() {
+ let mut app = test_app();
+ app.todos
+ .init(vec![crate::core::agent::todo::TodoPhase {
+ name: String::new(),
+ tasks: vec![todo_item("a")],
+ }])
+ .unwrap();
+ app.todos
+ .done(crate::core::agent::todo::Target::All)
+ .unwrap();
+
+ age_closed_todos(&mut app).await;
+ assert_eq!(app.runs_since_todos_closed, 1);
+
+ app.todos
+ .init(vec![crate::core::agent::todo::TodoPhase {
+ name: String::new(),
+ tasks: vec![todo_item("b")],
+ }])
+ .unwrap();
+ age_closed_todos(&mut app).await;
+ assert_eq!(app.runs_since_todos_closed, 0, "counter must reset");
+ assert!(!app.todos.is_empty(), "new work must not be aged out");
+ }
+
+ /// An empty list never accumulates grace, so the counter can't drift.
+ #[tokio::test]
+ async fn empty_todos_do_not_accumulate_grace() {
+ let mut app = test_app();
+ for _ in 0..5 {
+ age_closed_todos(&mut app).await;
+ }
+ assert_eq!(app.runs_since_todos_closed, 0);
+ }
+
#[test]
fn compact_is_a_registered_command() {
assert!(SLASH_COMMANDS.iter().any(|c| c.name == "/compact"));