diff --git a/crates/ide-agent/src/lib.rs b/crates/ide-agent/src/lib.rs index 611054198f..c72e36ed4d 100644 --- a/crates/ide-agent/src/lib.rs +++ b/crates/ide-agent/src/lib.rs @@ -2762,7 +2762,6 @@ impl AgentManager { } s.task.title = title; s.title_renamed = true; - s.task.updated_ms = now_ms(); } task.persist(); task.emit_task(); @@ -3126,7 +3125,6 @@ impl AgentManager { let changed = s.task.mcp_overrides != cleaned; if changed { s.task.mcp_overrides = cleaned; - s.task.updated_ms = now_ms(); } (changed, s.busy, !s.background_tasks.is_empty(), s.snapshot()) }; @@ -3158,7 +3156,6 @@ impl AgentManager { let changed = s.task.skill_overrides != cleaned; if changed { s.task.skill_overrides = cleaned; - s.task.updated_ms = now_ms(); } (changed, s.busy, !s.background_tasks.is_empty(), s.task.clone()) }; @@ -3194,7 +3191,6 @@ impl AgentManager { let changed = s.task.memory_overrides != next_boxed; if changed { s.task.memory_overrides = next_boxed; - s.task.updated_ms = now_ms(); } (changed, s.busy, !s.background_tasks.is_empty(), s.task.clone()) }; diff --git a/crates/ide-agent/src/tests.rs b/crates/ide-agent/src/tests.rs index 640e868882..93e8a8c77f 100644 --- a/crates/ide-agent/src/tests.rs +++ b/crates/ide-agent/src/tests.rs @@ -6928,3 +6928,36 @@ async fn network_outage_window_off_or_expired_degrades_to_interrupted_and_auth_e mgr2.shutdown(); mgr3.shutdown(); } + +#[tokio::test(flavor = "multi_thread")] +async fn metadata_edits_do_not_bump_updated_ms() { + let tmp = tempfile::tempdir().unwrap(); + let mgr = manager(tmp.path(), Arc::new(FakeAgent::default())); + let _down = ShutDown(&mgr); + let task = mgr + .create(NewAgentTask { provider: "claude".into(), prompt: text("again"), ..Default::default() }) + .await + .unwrap(); + wait_for(&mgr, &task.id, AgentStatus::Ready).await; + let handle = mgr.task(&task.id).unwrap(); + let fixed_ms = 1_234.0; + handle.state.lock().task.updated_ms = fixed_ms; + + mgr.rename(&task.id, "Custom Title").unwrap(); + assert_eq!(handle.state.lock().task.updated_ms, fixed_ms); + + let after_mcp = mgr.set_mcp_overrides(&task.id, BTreeMap::from([("github".into(), false)])).unwrap(); + assert_eq!(after_mcp.updated_ms, fixed_ms); + + let after_skill = + mgr.set_skill_overrides(&task.id, BTreeMap::from([("triage".into(), AgentSkillMode::Off)])).unwrap(); + assert_eq!(after_skill.updated_ms, fixed_ms); + + let after_memory = mgr + .set_memory_overrides( + &task.id, + AgentMemoryOverrides { auto_memory: Some(AutoMemoryMode::Off), entries: BTreeMap::new() }, + ) + .unwrap(); + assert_eq!(after_memory.updated_ms, fixed_ms); +} diff --git a/src/features/agents/AgentsView.test.tsx b/src/features/agents/AgentsView.test.tsx index 4b11a539bf..e2f87fa73f 100644 --- a/src/features/agents/AgentsView.test.tsx +++ b/src/features/agents/AgentsView.test.tsx @@ -658,6 +658,36 @@ describe("Agents pinned and grouped tasks", () => { } }; + it("keeps Pinned and active status groups stable when activity timestamps change", () => { + const tasks = new Map( + [ + { ...task("pin-first"), createdMs: 10, updatedMs: 10 }, + { ...task("pin-second"), createdMs: 20, updatedMs: 20 }, + { ...task("ready-new"), createdMs: 40, updatedMs: 40 }, + { ...task("ready-old"), createdMs: 30, updatedMs: 30 }, + ].map((t) => [t.id, t]), + ); + useAgents.setState({ tasks, organize: { groups: [], groupOf: {}, pinned: ["pin-first", "pin-second"] } }); + setup(); + + const visibleIds = () => + Array.from(document.querySelectorAll("[data-tree-key]")) + .map((el) => el.dataset.treeKey) + .filter((k) => k && !k.startsWith("g:") && k !== "pinned"); + + expect(visibleIds()).toEqual(["pin-first", "pin-second", "ready-new", "ready-old"]); + + // Activity updates on older/later-pinned tasks do not reshuffle either section. + act(() => { + const next = new Map(useAgents.getState().tasks); + next.set("pin-second", { ...next.get("pin-second")!, updatedMs: 999 }); + next.set("ready-old", { ...next.get("ready-old")!, updatedMs: 999 }); + useAgents.setState({ tasks: next }); + }); + + expect(visibleIds()).toEqual(["pin-first", "pin-second", "ready-new", "ready-old"]); + }); + it("reorders tasks within a section by dragging before or after a sibling", () => { const tasks = new Map( [ diff --git a/src/features/agents/agents.test.ts b/src/features/agents/agents.test.ts index 58b334726b..aaf5ff44c0 100644 --- a/src/features/agents/agents.test.ts +++ b/src/features/agents/agents.test.ts @@ -57,7 +57,7 @@ import { } from "./model"; import { markdownHtml } from "./Markdown"; -function task(id: string, status: AgentStatus, updatedMs: number): AgentTask { +function task(id: string, status: AgentStatus, createdMs: number, updatedMs = createdMs): AgentTask { return { id, title: id, @@ -70,7 +70,7 @@ function task(id: string, status: AgentStatus, updatedMs: number): AgentTask { base: "main", baseCommit: "abc", error: null, - createdMs: 0, + createdMs, updatedMs, config: [], commands: [], @@ -634,18 +634,21 @@ describe("usage meters", () => { }); describe("agents model", () => { - it("groups tasks by what they need, newest first", () => { + it("groups tasks by what they need, keeping active groups in creation order and archived by last update", () => { const groups = groupTasks([ - task("a", "ready", 1), - task("b", "needsInput", 2), - task("c", "running", 3), - task("d", "starting", 4), - task("e", "ready", 5), + task("a", "ready", 1, 90), + task("b", "needsInput", 2, 2), + task("c", "running", 3, 100), + task("d", "starting", 4, 4), + task("e", "ready", 5, 5), + task("arch-old-created-recent-archived", "archived", 1, 200), + task("arch-new-created-older-archived", "archived", 50, 100), ]); expect(groups.map((g) => [g.status, g.tasks.map((t) => t.id)])).toEqual([ ["needsInput", ["b"]], ["running", ["d", "c"]], ["ready", ["e", "a"]], + ["archived", ["arch-old-created-recent-archived", "arch-new-created-older-archived"]], ]); }); diff --git a/src/features/agents/model.ts b/src/features/agents/model.ts index 2fd77a3ce6..c410319cbd 100644 --- a/src/features/agents/model.ts +++ b/src/features/agents/model.ts @@ -120,12 +120,22 @@ export const taskGroupStatus = (task: Pick) export const taskAttentionRank = (task: Pick): number => GROUP_RANK.get(taskGroupStatus(task)) ?? GROUPS.length; +/** Active sections sort by creation time (newest first) so turns don't reshuffle rows. */ +export const compareTaskCreated = (a: AgentTask, b: AgentTask): number => + (b.createdMs ?? b.updatedMs ?? 0) - (a.createdMs ?? a.updatedMs ?? 0) || (b.updatedMs ?? 0) - (a.updatedMs ?? 0); + +/** Archived tasks sort by last update (newest first) so a just-archived task appears at the top. */ +export const compareTaskUpdated = (a: AgentTask, b: AgentTask): number => + (b.updatedMs ?? b.createdMs ?? 0) - (a.updatedMs ?? a.createdMs ?? 0); + export function groupTasks(tasks: Iterable): TaskGroup[] { - const sorted = [...tasks].sort((a, b) => (b.updatedMs ?? 0) - (a.updatedMs ?? 0)); + const list = [...tasks]; return GROUPS.map(([status, label]) => ({ status, label, - tasks: sorted.filter((t) => taskGroupStatus(t) === status), + tasks: list + .filter((t) => taskGroupStatus(t) === status) + .sort(status === "archived" ? compareTaskUpdated : compareTaskCreated), })).filter((g) => g.tasks.length > 0); } diff --git a/src/features/agents/organize.test.ts b/src/features/agents/organize.test.ts index de8b724ed7..ee2a213077 100644 --- a/src/features/agents/organize.test.ts +++ b/src/features/agents/organize.test.ts @@ -18,7 +18,7 @@ import { ungroupedMembers, } from "./organize"; -function task(id: string, status: AgentTask["status"] = "ready", updatedMs = 0): AgentTask { +function task(id: string, status: AgentTask["status"] = "ready", updatedMs = 0, createdMs = updatedMs): AgentTask { return { id, title: id, @@ -31,7 +31,7 @@ function task(id: string, status: AgentTask["status"] = "ready", updatedMs = 0): base: null, baseCommit: null, error: null, - createdMs: 0, + createdMs, updatedMs, config: [], commands: [], @@ -72,25 +72,35 @@ describe("task organize", () => { expect(pruneOrganize(clean, [task("a")])).toBe(clean); }); - it("orders a group by what needs attention, then by activity, leaving pinned tasks out", () => { + it("orders a group by what needs attention, then by creation order for active tasks and activity for archived", () => { const tasks = [ - task("slow", "running", 30), - task("ask", "needsInput", 10), - task("pinned", "ready", 99), - task("done", "ready", 20), + task("slow", "running", 30, 30), + task("ask", "needsInput", 10, 10), + task("pinned", "ready", 99, 99), + task("done-old", "ready", 500, 10), + task("done-new", "ready", 20, 20), + task("arch-just", "archived", 600, 5), + task("arch-earlier", "archived", 40, 40), ]; const organize = { groups: [{ id: "g1", name: "Docs" }], groupOf: Object.fromEntries(tasks.map((t) => [t.id, "g1"])), pinned: ["pinned"], }; - expect(groupMembers(tasks, organize, "g1").map((t) => t.id)).toEqual(["ask", "slow", "done"]); + expect(groupMembers(tasks, organize, "g1").map((t) => t.id)).toEqual([ + "ask", + "slow", + "done-new", + "done-old", + "arch-just", + "arch-earlier", + ]); }); - it("shows pinned tasks by activity, and the others under the status groups", () => { - const tasks = [task("a", "ready", 10), task("b", "running", 30), task("c", "ready", 20)]; + it("keeps pinned tasks in pinned order regardless of activity, and puts others under status groups", () => { + const tasks = [task("a", "ready", 10, 10), task("b", "running", 999, 30), task("c", "ready", 20, 20)]; const organize = { groups: [{ id: "g1", name: "Docs" }], groupOf: { c: "g1" }, pinned: ["a", "b"] }; - expect(pinnedMembers(tasks, organize).map((t) => t.id)).toEqual(["b", "a"]); + expect(pinnedMembers(tasks, organize).map((t) => t.id)).toEqual(["a", "b"]); expect(ungroupedMembers(tasks, organize).map((t) => t.id)).toEqual([]); expect(ungroupedMembers([...tasks, task("d")], organize).map((t) => t.id)).toEqual(["d"]); }); diff --git a/src/features/agents/organize.ts b/src/features/agents/organize.ts index a8a380e1ea..f6612ef50b 100644 --- a/src/features/agents/organize.ts +++ b/src/features/agents/organize.ts @@ -6,7 +6,14 @@ import type { AgentTask } from "@/bindings"; import { tx } from "@/platform/i18n"; import { parseRemoteLocation } from "@/platform/remote"; -import { groupTasks, taskAttentionRank, taskGroupStatus, type TaskGroup } from "./model"; +import { + compareTaskCreated, + compareTaskUpdated, + groupTasks, + taskAttentionRank, + taskGroupStatus, + type TaskGroup, +} from "./model"; /** Section key of the pinned group, above every other group. */ export const PINNED_SECTION = "pinned"; @@ -208,12 +215,16 @@ export function applyOrder(defaultSorted: AgentTask[], manual: readonly string[] return [...fresh, ...ordered]; } -/** The tasks of `group`, what waits for the user first, then the most recently active (or manual order). */ +/** The tasks of `group`, what waits for the user first, then creation order for active tasks (or manual order). */ export function groupMembers(tasks: Iterable, organize: TaskOrganize, group: string): AgentTask[] { const pinned = new Set(organize.pinned); const sorted = [...tasks] .filter((t) => organize.groupOf[t.id] === group && !pinned.has(t.id)) - .sort((a, b) => taskAttentionRank(a) - taskAttentionRank(b) || (b.updatedMs ?? 0) - (a.updatedMs ?? 0)); + .sort( + (a, b) => + taskAttentionRank(a) - taskAttentionRank(b) || + (taskGroupStatus(a) === "archived" ? compareTaskUpdated(a, b) : compareTaskCreated(a, b)), + ); return applyOrder(sorted, organize.order?.[customSection(group)]); } @@ -236,11 +247,19 @@ export function statusGroups(tasks: Iterable, organize: TaskOrganize) })); } -/** The pinned tasks, most recently active first (or manual order). */ +/** The pinned tasks, in pinned order (or manual order when rearranged). */ export function pinnedMembers(tasks: Iterable, organize: TaskOrganize): AgentTask[] { - const pinned = new Set(organize.pinned); - const sorted = [...tasks].filter((t) => pinned.has(t.id)).sort((a, b) => (b.updatedMs ?? 0) - (a.updatedMs ?? 0)); - return applyOrder(sorted, organize.order?.[PINNED_SECTION]); + const byId = new Map(); + for (const task of tasks) byId.set(task.id, task); + const seen = new Set(); + const ordered: AgentTask[] = []; + for (const id of organize.pinned) { + if (seen.has(id)) continue; + seen.add(id); + const task = byId.get(id); + if (task) ordered.push(task); + } + return applyOrder(ordered, organize.order?.[PINNED_SECTION]); } function placeId(