From 0fc270f424b77a78ec75c11d79b522b2233556e9 Mon Sep 17 00:00:00 2001 From: lutfi-zain <147457356+lutfi-zain@users.noreply.github.com> Date: Mon, 19 Oct 2026 18:54:21 +0700 Subject: [PATCH 1/4] feat(sidebar): retain recently completed sessions in running filter --- components/SessionSidebar-chrome.tsx | 24 ++++++- components/SessionSidebar-rows.tsx | 21 +++++- components/SessionSidebar.tsx | 29 +++++++- lib/i18n/locales/en.json | 3 +- lib/i18n/locales/ja.json | 3 +- lib/i18n/locales/zh-CN.json | 3 +- lib/recent-runs.test.mjs | 103 +++++++++++++++++++++++++++ lib/recent-runs.ts | 31 ++++++++ 8 files changed, 208 insertions(+), 9 deletions(-) create mode 100644 lib/recent-runs.test.mjs create mode 100644 lib/recent-runs.ts diff --git a/components/SessionSidebar-chrome.tsx b/components/SessionSidebar-chrome.tsx index 84a9bd8dd..abb6cf2a7 100644 --- a/components/SessionSidebar-chrome.tsx +++ b/components/SessionSidebar-chrome.tsx @@ -4,7 +4,7 @@ import { useCallback, useEffect, useLayoutEffect, useRef, useState, type CSSProp import { createPortal } from "react-dom"; import { useI18n } from "@/lib/i18n"; import { usePrefersReducedMotion } from "@/hooks/usePrefersReducedMotion"; -import { AlertTriangle } from "lucide-react"; +import { AlertTriangle, Check } from "lucide-react"; import OmpWebLogo from "./OmpWebLogo"; /** * Path label that ellipsizes on the LEFT, keeping the (most relevant) trailing @@ -407,6 +407,27 @@ function ExitedSessionIndicator({ title, size = 14 }: { title: string; size?: nu ); } +function RecentlyCompletedIndicator({ size = 12 }: { size?: number }) { + const { t } = useI18n(); + return ( + + + ); +} + function UnreadSessionIndicator({ size = 14 }: { size?: number }) { const { t } = useI18n(); @@ -442,6 +463,7 @@ export { PathLabel, ExitedSessionIndicator, RunningSessionIndicator, + RecentlyCompletedIndicator, SIDEBAR_BUTTON_TRANSITION, SidebarIconButton, SidebarPortalMenu, diff --git a/components/SessionSidebar-rows.tsx b/components/SessionSidebar-rows.tsx index 16593cd5b..db461d0ba 100644 --- a/components/SessionSidebar-rows.tsx +++ b/components/SessionSidebar-rows.tsx @@ -17,6 +17,7 @@ import { } from "./SessionSidebar-helpers"; import { ExitedSessionIndicator, + RecentlyCompletedIndicator, RunningSessionIndicator, SIDEBAR_BUTTON_TRANSITION, SidebarPortalMenu, @@ -43,6 +44,7 @@ interface ProjectRowProps { selectedSessionId: string | null; runningSessionIds: Set; unreadSessionIds: Set; + recentlyCompletedIds?: Set; exitedSessions: Map; relativeTimeNow: number; onActivate: (path: string) => void; @@ -73,6 +75,7 @@ function ProjectRow({ selectedSessionId, runningSessionIds, unreadSessionIds, + recentlyCompletedIds, exitedSessions, relativeTimeNow, onActivate, @@ -374,6 +377,7 @@ function ProjectRow({ runningSessionIds={runningSessionIds} unreadSessionIds={unreadSessionIds} exitedSessions={exitedSessions} + recentlyCompletedIds={recentlyCompletedIds} relativeTimeNow={relativeTimeNow} onSelectSession={onSelectSession} onRenamed={onRenamed} @@ -424,6 +428,7 @@ const SessionTreeItem = memo(function SessionTreeItem({ runningSessionIds, unreadSessionIds, exitedSessions, + recentlyCompletedIds, relativeTimeNow, onSelectSession, onRenamed, @@ -435,6 +440,7 @@ const SessionTreeItem = memo(function SessionTreeItem({ runningSessionIds: Set; unreadSessionIds: Set; exitedSessions: Map; + recentlyCompletedIds?: Set; relativeTimeNow: number; onSelectSession: (s: SessionInfo) => void; onRenamed?: () => void; @@ -450,6 +456,7 @@ const SessionTreeItem = memo(function SessionTreeItem({ const isSelected = sessionId === selectedSessionId; const isRunning = runningSessionIds.has(sessionId); const isUnread = unreadSessionIds.has(sessionId); + const isRecentlyCompleted = !isRunning && (recentlyCompletedIds?.has(sessionId) ?? false); const exited = exitedSessions.get(sessionId); // Stable callbacks: depend only on primitives / stable parent callbacks so @@ -484,6 +491,7 @@ const SessionTreeItem = memo(function SessionTreeItem({ isRunning={isRunning} isUnread={isUnread} exited={exited} + isRecentlyCompleted={isRecentlyCompleted} relativeTimeNow={relativeTimeNow} onClick={handleClick} onRenamed={onRenamed} @@ -504,6 +512,7 @@ const SessionTreeItem = memo(function SessionTreeItem({ runningSessionIds={runningSessionIds} unreadSessionIds={unreadSessionIds} exitedSessions={exitedSessions} + recentlyCompletedIds={recentlyCompletedIds} relativeTimeNow={relativeTimeNow} onSelectSession={onSelectSession} onRenamed={onRenamed} @@ -527,6 +536,10 @@ const SessionTreeItem = memo(function SessionTreeItem({ const id = prev.node.session.id; if (prev.runningSessionIds.has(id) !== next.runningSessionIds.has(id)) return false; } + if (prev.recentlyCompletedIds !== next.recentlyCompletedIds) { + const id = prev.node.session.id; + if ((prev.recentlyCompletedIds?.has(id) ?? false) !== (next.recentlyCompletedIds?.has(id) ?? false)) return false; + } if (prev.unreadSessionIds !== next.unreadSessionIds) { const id = prev.node.session.id; if (prev.unreadSessionIds.has(id) !== next.unreadSessionIds.has(id)) return false; @@ -544,6 +557,7 @@ const SessionItem = memo(function SessionItem({ isRunning, isUnread, exited, + isRecentlyCompleted, onClick, onRenamed, onDeleted, @@ -558,6 +572,7 @@ const SessionItem = memo(function SessionItem({ isRunning?: boolean; isUnread?: boolean; exited?: ExitedRpcSession; + isRecentlyCompleted?: boolean; onClick: () => void; onRenamed?: () => void; onDeleted?: (id: string) => void; @@ -719,13 +734,15 @@ const SessionItem = memo(function SessionItem({ {session.worktreeBranch && } {hasChildren && }
- {(isRunning || exited || isUnread) && ( + {(isRunning || exited || isRecentlyCompleted || isUnread) && ( {isRunning ? : exited ? - : } + : isRecentlyCompleted + ? + : } )}
diff --git a/components/SessionSidebar.tsx b/components/SessionSidebar.tsx index 6c2213d3f..790098fd4 100644 --- a/components/SessionSidebar.tsx +++ b/components/SessionSidebar.tsx @@ -15,6 +15,12 @@ import { comparableProjectPath } from "@/lib/comparable-path"; import { Archive, ArrowLeft, ArrowRight, Check, ChevronRight, FileUp, Plus, RefreshCw, Search, Settings2, SlidersHorizontal, X } from "lucide-react"; import { publishSessionsChanged } from "@/lib/session-change-bus"; import { openEventStream } from "@/lib/event-stream"; +import { + extractRecentCompletedSet, + recordCompletedRun, + removeActiveFromCompleted, + type RecentRunEntry, +} from "@/lib/recent-runs"; import { EMPTY_PROJECT_SET, INITIAL_RESTORE_MAX_ATTEMPTS, @@ -126,6 +132,8 @@ export const SessionSidebar = memo(function SessionSidebar({ selectedSessionId, const [exitedSessions, setExitedSessions] = useState>(() => new Map()); const knownRunningCwdsRef = useRef>(new Map()); const [unreadSessionIds, setUnreadSessionIds] = useState>(() => loadUnreadSessionIds()); + const [recentCompletedRuns, setRecentCompletedRuns] = useState([]); + const recentCompletedSet = useMemo(() => extractRecentCompletedSet(recentCompletedRuns), [recentCompletedRuns]); const previousRunningSessionIdsRef = useRef>(new Set()); // Relative session times must age while the sidebar stays open; one shared // minute clock avoids a timer per session row. @@ -337,9 +345,23 @@ export const SessionSidebar = memo(function SessionSidebar({ selectedSessionId, useEffect(() => { const previous = previousRunningSessionIdsRef.current; - const completedInBackground = [...previous].filter((id) => !runningSessionIds.has(id) && id !== selectedSessionId); + const allCompleted = [...previous].filter((id) => !runningSessionIds.has(id)); + const completedInBackground = allCompleted.filter((id) => id !== selectedSessionId); const newlyRunning = [...runningSessionIds].filter((id) => !previous.has(id)); + if (allCompleted.length > 0 || newlyRunning.length > 0) { + setRecentCompletedRuns((prev) => { + let updated = prev; + if (newlyRunning.length > 0) { + updated = removeActiveFromCompleted(updated, runningSessionIds); + } + for (const id of allCompleted) { + updated = recordCompletedRun(updated, id); + } + return updated; + }); + } + if (completedInBackground.length > 0 || newlyRunning.length > 0) { setUnreadSessionIds((prev) => { const next = new Set(prev); @@ -683,7 +705,7 @@ export const SessionSidebar = memo(function SessionSidebar({ selectedSessionId, const entries: { project: ManagedProject; sessions: SessionInfo[] }[] = []; for (const project of sortedProjects) { let list = sessionsByProject.get(project.path) ?? []; - if (runningOnly) list = list.filter((s) => runningSessionIds.has(s.id)); + if (runningOnly) list = list.filter((s) => runningSessionIds.has(s.id) || recentCompletedSet.has(s.id)); if (q) { list = list.filter((s) => (s.name ?? "").toLowerCase().includes(q) || s.firstMessage.toLowerCase().includes(q)); } @@ -693,7 +715,7 @@ export const SessionSidebar = memo(function SessionSidebar({ selectedSessionId, entries.push({ project, sessions: list }); } return entries; - }, [sortedProjects, sessionsByProject, runningOnly, deferredSearchQuery, runningSessionIds]); + }, [sortedProjects, sessionsByProject, runningOnly, deferredSearchQuery, runningSessionIds, recentCompletedSet]); const treesByProject = useMemo(() => { const m = new Map>(); @@ -1387,6 +1409,7 @@ export const SessionSidebar = memo(function SessionSidebar({ selectedSessionId, runningSessionIds={runningSessionIds} unreadSessionIds={unreadSessionIds} exitedSessions={exitedSessions} + recentlyCompletedIds={recentCompletedSet} relativeTimeNow={relativeTimeNow} onActivate={activateProject} onToggleExpand={toggleProjectExpanded} diff --git a/lib/i18n/locales/en.json b/lib/i18n/locales/en.json index 185b477d9..6d3e54768 100644 --- a/lib/i18n/locales/en.json +++ b/lib/i18n/locales/en.json @@ -1192,7 +1192,8 @@ "sessionSidebar.searchTitle": "Search sessions by name or message", "sessionSidebar.searchPlaceholder": "Search sessions…", "sessionSidebar.filterRunning": "Show running sessions only", - "sessionSidebar.filterRunningTitle": "Filter to sessions that are currently running", + "sessionSidebar.filterRunningTitle": "Show active and recently completed sessions (up to 10)", + "sessionSidebar.recentlyCompleted": "Recently completed", "sessionSidebar.noMatches": "No matching sessions", "skillsConfig.addCustomSkill": "Add custom skill", "skillsConfig.addSkill": "Add skill", diff --git a/lib/i18n/locales/ja.json b/lib/i18n/locales/ja.json index 7e9fbb9f3..c7c42e19d 100644 --- a/lib/i18n/locales/ja.json +++ b/lib/i18n/locales/ja.json @@ -1154,7 +1154,8 @@ "sessionSidebar.searchTitle": "名前またはメッセージでセッションを検索", "sessionSidebar.searchPlaceholder": "セッションを検索…", "sessionSidebar.filterRunning": "実行中のセッションのみ表示", - "sessionSidebar.filterRunningTitle": "実行中のセッションのみに絞り込む", + "sessionSidebar.filterRunningTitle": "実行中および最近完了したセッションを表示(最大10件)", + "sessionSidebar.recentlyCompleted": "最近完了", "sessionSidebar.noMatches": "一致するセッションがありません", "skillsConfig.addCustomSkill": "カスタムスキルを追加", "skillsConfig.addSkill": "スキルを追加", diff --git a/lib/i18n/locales/zh-CN.json b/lib/i18n/locales/zh-CN.json index b9144d3b9..79f10c38e 100644 --- a/lib/i18n/locales/zh-CN.json +++ b/lib/i18n/locales/zh-CN.json @@ -1154,7 +1154,8 @@ "sessionSidebar.searchTitle": "按名称或消息搜索会话", "sessionSidebar.searchPlaceholder": "搜索会话…", "sessionSidebar.filterRunning": "仅显示运行中的会话", - "sessionSidebar.filterRunningTitle": "仅筛选出当前正在运行的会话", + "sessionSidebar.filterRunningTitle": "显示正在运行和最近完成的会话(最多10个)", + "sessionSidebar.recentlyCompleted": "最近完成", "sessionSidebar.noMatches": "没有匹配的会话", "skillsConfig.addCustomSkill": "添加自定义技能", "skillsConfig.addSkill": "添加技能", diff --git a/lib/recent-runs.test.mjs b/lib/recent-runs.test.mjs new file mode 100644 index 000000000..a321166ff --- /dev/null +++ b/lib/recent-runs.test.mjs @@ -0,0 +1,103 @@ +import assert from "node:assert/strict"; +import test from "node:test"; + +async function loadSubject() { + return import("./recent-runs.ts"); +} + +test("recordCompletedRun prepends newest entry and preserves timestamp", async () => { + const { recordCompletedRun } = await loadSubject(); + const initial = []; + const timestamp = 1700000000000; + const result = recordCompletedRun(initial, "session-1", timestamp); + assert.deepEqual(result, [ + { sessionId: "session-1", completedAt: timestamp }, + ]); + + const timestamp2 = 1700000001000; + const result2 = recordCompletedRun(result, "session-2", timestamp2); + assert.deepEqual(result2, [ + { sessionId: "session-2", completedAt: timestamp2 }, + { sessionId: "session-1", completedAt: timestamp }, + ]); +}); + +test("recordCompletedRun deduplicates existing sessionId and moves to front", async () => { + const { recordCompletedRun } = await loadSubject(); + const t1 = 1000; + const t2 = 2000; + const t3 = 3000; + + let runs = recordCompletedRun([], "session-1", t1); + runs = recordCompletedRun(runs, "session-2", t2); + assert.equal(runs.length, 2); + assert.equal(runs[0].sessionId, "session-2"); + + runs = recordCompletedRun(runs, "session-1", t3); + assert.equal(runs.length, 2); + assert.deepEqual(runs, [ + { sessionId: "session-1", completedAt: t3 }, + { sessionId: "session-2", completedAt: t2 }, + ]); +}); + +test("recordCompletedRun enforces max capacity of 10 items (overflow drops oldest)", async () => { + const { recordCompletedRun, MAX_RECENT_COMPLETED_RUNS } = await loadSubject(); + assert.equal(MAX_RECENT_COMPLETED_RUNS, 10); + + let runs = []; + for (let i = 1; i <= 15; i++) { + runs = recordCompletedRun(runs, `session-${i}`, i * 1000); + } + + assert.equal(runs.length, 10); + assert.equal(runs[0].sessionId, "session-15"); + assert.equal(runs[0].completedAt, 15000); + assert.equal(runs[9].sessionId, "session-6"); + assert.equal(runs[9].completedAt, 6000); + + const ids = runs.map((e) => e.sessionId); + for (let i = 1; i <= 5; i++) { + assert.ok(!ids.includes(`session-${i}`)); + } +}); + +test("removeActiveFromCompleted removes any re-running session IDs", async () => { + const { removeActiveFromCompleted } = await loadSubject(); + const runs = [ + { sessionId: "session-1", completedAt: 1000 }, + { sessionId: "session-2", completedAt: 2000 }, + { sessionId: "session-3", completedAt: 3000 }, + ]; + + const activeRunning = new Set(["session-2"]); + const filtered = removeActiveFromCompleted(runs, activeRunning); + + assert.deepEqual(filtered, [ + { sessionId: "session-1", completedAt: 1000 }, + { sessionId: "session-3", completedAt: 3000 }, + ]); + + const allActive = new Set(["session-1", "session-3"]); + const empty = removeActiveFromCompleted(filtered, allActive); + assert.deepEqual(empty, []); +}); + +test("extractRecentCompletedSet and isRecentlyCompleted", async () => { + const { extractRecentCompletedSet, isRecentlyCompleted } = await loadSubject(); + const runs = [ + { sessionId: "session-1", completedAt: 1000 }, + { sessionId: "session-2", completedAt: 2000 }, + ]; + + const idSet = extractRecentCompletedSet(runs); + assert.ok(idSet instanceof Set); + assert.equal(idSet.size, 2); + assert.ok(idSet.has("session-1")); + assert.ok(idSet.has("session-2")); + assert.ok(!idSet.has("session-3")); + + assert.equal(isRecentlyCompleted(runs, "session-1"), true); + assert.equal(isRecentlyCompleted(runs, "session-2"), true); + assert.equal(isRecentlyCompleted(runs, "session-3"), false); +}); diff --git a/lib/recent-runs.ts b/lib/recent-runs.ts new file mode 100644 index 000000000..aec00c2cd --- /dev/null +++ b/lib/recent-runs.ts @@ -0,0 +1,31 @@ +export interface RecentRunEntry { + sessionId: string; + completedAt: number; +} + +export const MAX_RECENT_COMPLETED_RUNS = 10; + +export function recordCompletedRun( + current: RecentRunEntry[], + completedId: string, + now = Date.now(), +): RecentRunEntry[] { + const newEntry: RecentRunEntry = { sessionId: completedId, completedAt: now }; + const filtered = current.filter((entry) => entry.sessionId !== completedId); + return [newEntry, ...filtered].slice(0, MAX_RECENT_COMPLETED_RUNS); +} + +export function removeActiveFromCompleted( + current: RecentRunEntry[], + activeRunningIds: Set, +): RecentRunEntry[] { + return current.filter((entry) => !activeRunningIds.has(entry.sessionId)); +} + +export function extractRecentCompletedSet(entries: RecentRunEntry[]): Set { + return new Set(entries.map((entry) => entry.sessionId)); +} + +export function isRecentlyCompleted(entries: RecentRunEntry[], sessionId: string): boolean { + return entries.some((entry) => entry.sessionId === sessionId); +} From ef17ef40117f7ee955cdffce705e89028f84328a Mon Sep 17 00:00:00 2001 From: lutfi-zain <147457356+lutfi-zain@users.noreply.github.com> Date: Mon, 19 Oct 2026 19:16:21 +0700 Subject: [PATCH 2/4] feat(sidebar): persist recent runs and seed initial list from latest active sessions --- components/SessionSidebar.tsx | 39 +++++++++++++++++++++++----- lib/recent-runs.test.mjs | 33 ++++++++++++++++++++++++ lib/recent-runs.ts | 48 +++++++++++++++++++++++++++++++++++ 3 files changed, 114 insertions(+), 6 deletions(-) diff --git a/components/SessionSidebar.tsx b/components/SessionSidebar.tsx index 790098fd4..d7d5c66ef 100644 --- a/components/SessionSidebar.tsx +++ b/components/SessionSidebar.tsx @@ -16,9 +16,12 @@ import { Archive, ArrowLeft, ArrowRight, Check, ChevronRight, FileUp, Plus, Refr import { publishSessionsChanged } from "@/lib/session-change-bus"; import { openEventStream } from "@/lib/event-stream"; import { - extractRecentCompletedSet, + loadRecentRuns, recordCompletedRun, removeActiveFromCompleted, + saveRecentRuns, + seedRecentRuns, + MAX_RECENT_COMPLETED_RUNS, type RecentRunEntry, } from "@/lib/recent-runs"; import { @@ -132,8 +135,20 @@ export const SessionSidebar = memo(function SessionSidebar({ selectedSessionId, const [exitedSessions, setExitedSessions] = useState>(() => new Map()); const knownRunningCwdsRef = useRef>(new Map()); const [unreadSessionIds, setUnreadSessionIds] = useState>(() => loadUnreadSessionIds()); - const [recentCompletedRuns, setRecentCompletedRuns] = useState([]); - const recentCompletedSet = useMemo(() => extractRecentCompletedSet(recentCompletedRuns), [recentCompletedRuns]); + const [recentCompletedRuns, setRecentCompletedRuns] = useState(() => loadRecentRuns()); + const effectiveRecentCompletedSet = useMemo(() => { + const set = new Set(recentCompletedRuns.map((r) => r.sessionId)); + if (set.size < MAX_RECENT_COMPLETED_RUNS) { + const candidates = [...allSessions] + .filter((s) => s.messageCount > 0 && !runningSessionIds.has(s.id)) + .sort((a, b) => new Date(b.modified).getTime() - new Date(a.modified).getTime()); + for (const c of candidates) { + set.add(c.id); + if (set.size >= MAX_RECENT_COMPLETED_RUNS) break; + } + } + return set; + }, [recentCompletedRuns, allSessions, runningSessionIds]); const previousRunningSessionIdsRef = useRef>(new Set()); // Relative session times must age while the sidebar stays open; one shared // minute clock avoids a timer per session row. @@ -260,6 +275,18 @@ export const SessionSidebar = memo(function SessionSidebar({ selectedSessionId, useEffect(() => { saveUnreadSessionIds(unreadSessionIds); }, [unreadSessionIds]); + useEffect(() => { + saveRecentRuns(recentCompletedRuns); + }, [recentCompletedRuns]); + + useEffect(() => { + if (allSessions.length === 0) return; + setRecentCompletedRuns((prev) => { + if (prev.length > 0) return prev; + return seedRecentRuns(allSessions, MAX_RECENT_COMPLETED_RUNS); + }); + }, [allSessions]); + // Debounce refresh bursts (agent_start + session_info_update + file-appear signal can fire within 250ms) const pendingRefreshRef = useRef | null>(null); @@ -705,7 +732,7 @@ export const SessionSidebar = memo(function SessionSidebar({ selectedSessionId, const entries: { project: ManagedProject; sessions: SessionInfo[] }[] = []; for (const project of sortedProjects) { let list = sessionsByProject.get(project.path) ?? []; - if (runningOnly) list = list.filter((s) => runningSessionIds.has(s.id) || recentCompletedSet.has(s.id)); + if (runningOnly) list = list.filter((s) => runningSessionIds.has(s.id) || effectiveRecentCompletedSet.has(s.id)); if (q) { list = list.filter((s) => (s.name ?? "").toLowerCase().includes(q) || s.firstMessage.toLowerCase().includes(q)); } @@ -715,7 +742,7 @@ export const SessionSidebar = memo(function SessionSidebar({ selectedSessionId, entries.push({ project, sessions: list }); } return entries; - }, [sortedProjects, sessionsByProject, runningOnly, deferredSearchQuery, runningSessionIds, recentCompletedSet]); + }, [sortedProjects, sessionsByProject, runningOnly, deferredSearchQuery, runningSessionIds, effectiveRecentCompletedSet]); const treesByProject = useMemo(() => { const m = new Map>(); @@ -1409,7 +1436,7 @@ export const SessionSidebar = memo(function SessionSidebar({ selectedSessionId, runningSessionIds={runningSessionIds} unreadSessionIds={unreadSessionIds} exitedSessions={exitedSessions} - recentlyCompletedIds={recentCompletedSet} + recentlyCompletedIds={effectiveRecentCompletedSet} relativeTimeNow={relativeTimeNow} onActivate={activateProject} onToggleExpand={toggleProjectExpanded} diff --git a/lib/recent-runs.test.mjs b/lib/recent-runs.test.mjs index a321166ff..dc91f52bf 100644 --- a/lib/recent-runs.test.mjs +++ b/lib/recent-runs.test.mjs @@ -101,3 +101,36 @@ test("extractRecentCompletedSet and isRecentlyCompleted", async () => { assert.equal(isRecentlyCompleted(runs, "session-2"), true); assert.equal(isRecentlyCompleted(runs, "session-3"), false); }); + +test("saveRecentRuns and loadRecentRuns round-trip via mock storage", async () => { + const { saveRecentRuns, loadRecentRuns, RECENT_RUNS_STORAGE_KEY } = await loadSubject(); + const store = new Map(); + const mockStorage = { + getItem: (key) => store.get(key) ?? null, + setItem: (key, val) => store.set(key, val), + }; + + const runs = [ + { sessionId: "s1", completedAt: 100 }, + { sessionId: "s2", completedAt: 200 }, + ]; + saveRecentRuns(runs, mockStorage); + assert.equal(store.has(RECENT_RUNS_STORAGE_KEY), true); + + const loaded = loadRecentRuns(mockStorage); + assert.deepEqual(loaded, runs); +}); + +test("seedRecentRuns picks up to limit most recently modified sessions with messages", async () => { + const { seedRecentRuns } = await loadSubject(); + const sessions = [ + { id: "s1", modified: "2026-10-18T10:00:00Z", messageCount: 5 }, + { id: "s2", modified: "2026-10-19T12:00:00Z", messageCount: 1 }, + { id: "s3", modified: "2026-10-19T15:00:00Z", messageCount: 0 }, // no messages + { id: "s4", modified: "2026-10-17T08:00:00Z", messageCount: 10 }, + ]; + const seeded = seedRecentRuns(sessions, 2); + assert.equal(seeded.length, 2); + assert.equal(seeded[0].sessionId, "s2"); // newest with messageCount > 0 + assert.equal(seeded[1].sessionId, "s1"); +}); diff --git a/lib/recent-runs.ts b/lib/recent-runs.ts index aec00c2cd..1a7f86a4b 100644 --- a/lib/recent-runs.ts +++ b/lib/recent-runs.ts @@ -4,6 +4,54 @@ export interface RecentRunEntry { } export const MAX_RECENT_COMPLETED_RUNS = 10; +export const RECENT_RUNS_STORAGE_KEY = "ompweb.recent-completed-runs"; + +export function loadRecentRuns(storage?: Storage): RecentRunEntry[] { + try { + const s = storage ?? (typeof window !== "undefined" ? window.localStorage : undefined); + if (!s) return []; + const raw = s.getItem(RECENT_RUNS_STORAGE_KEY); + if (!raw) return []; + const parsed = JSON.parse(raw) as unknown; + if (!Array.isArray(parsed)) return []; + return parsed + .filter( + (item): item is RecentRunEntry => + typeof item === "object" && + item !== null && + typeof (item as RecentRunEntry).sessionId === "string" && + typeof (item as RecentRunEntry).completedAt === "number", + ) + .slice(0, MAX_RECENT_COMPLETED_RUNS); + } catch { + return []; + } +} + +export function saveRecentRuns(entries: RecentRunEntry[], storage?: Storage): void { + try { + const s = storage ?? (typeof window !== "undefined" ? window.localStorage : undefined); + if (!s) return; + s.setItem(RECENT_RUNS_STORAGE_KEY, JSON.stringify(entries.slice(0, MAX_RECENT_COMPLETED_RUNS))); + } catch { + // quota exceeded or private browsing + } +} + +export function seedRecentRuns( + allSessions: Array<{ id: string; modified: string; messageCount: number }>, + limit = MAX_RECENT_COMPLETED_RUNS, +): RecentRunEntry[] { + return [...allSessions] + .filter((s) => s.messageCount > 0) + .sort((a, b) => new Date(b.modified).getTime() - new Date(a.modified).getTime()) + .slice(0, limit) + .map((s) => ({ + sessionId: s.id, + completedAt: new Date(s.modified).getTime() || Date.now(), + })); +} + export function recordCompletedRun( current: RecentRunEntry[], From b11282df92463aad6ac424a5157cb2f90c6ee794 Mon Sep 17 00:00:00 2001 From: lutfi-zain <147457356+lutfi-zain@users.noreply.github.com> Date: Mon, 19 Oct 2026 19:32:03 +0700 Subject: [PATCH 3/4] fix(sidebar): unified recency timestamp ranking across sessions with skills and memory --- components/SessionSidebar.tsx | 44 ++++++++++++++++++++++++++--------- lib/recent-runs.ts | 4 ++-- 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/components/SessionSidebar.tsx b/components/SessionSidebar.tsx index d7d5c66ef..9979612ec 100644 --- a/components/SessionSidebar.tsx +++ b/components/SessionSidebar.tsx @@ -137,17 +137,33 @@ export const SessionSidebar = memo(function SessionSidebar({ selectedSessionId, const [unreadSessionIds, setUnreadSessionIds] = useState>(() => loadUnreadSessionIds()); const [recentCompletedRuns, setRecentCompletedRuns] = useState(() => loadRecentRuns()); const effectiveRecentCompletedSet = useMemo(() => { - const set = new Set(recentCompletedRuns.map((r) => r.sessionId)); - if (set.size < MAX_RECENT_COMPLETED_RUNS) { - const candidates = [...allSessions] - .filter((s) => s.messageCount > 0 && !runningSessionIds.has(s.id)) - .sort((a, b) => new Date(b.modified).getTime() - new Date(a.modified).getTime()); - for (const c of candidates) { - set.add(c.id); - if (set.size >= MAX_RECENT_COMPLETED_RUNS) break; + const timestampMap = new Map(); + + // 1. Live and persisted recent completed runs + for (const r of recentCompletedRuns) { + if (!runningSessionIds.has(r.sessionId)) { + timestampMap.set(r.sessionId, r.completedAt); + } + } + + // 2. On-disk active sessions (fallback & recency backfill) + for (const s of allSessions) { + if (runningSessionIds.has(s.id)) continue; + const isValid = (s.firstMessage && s.firstMessage !== "(no messages)") || s.messageCount > 0 || Boolean(s.name); + if (!isValid) continue; + const ts = new Date(s.modified).getTime(); + const existing = timestampMap.get(s.id); + if (existing === undefined || ts > existing) { + timestampMap.set(s.id, ts); } } - return set; + + // Sort by latest timestamp descending, pick top 10 + const sorted = [...timestampMap.entries()] + .sort((a, b) => b[1] - a[1]) + .slice(0, MAX_RECENT_COMPLETED_RUNS); + + return new Set(sorted.map(([id]) => id)); }, [recentCompletedRuns, allSessions, runningSessionIds]); const previousRunningSessionIdsRef = useRef>(new Set()); // Relative session times must age while the sidebar stays open; one shared @@ -282,8 +298,14 @@ export const SessionSidebar = memo(function SessionSidebar({ selectedSessionId, useEffect(() => { if (allSessions.length === 0) return; setRecentCompletedRuns((prev) => { - if (prev.length > 0) return prev; - return seedRecentRuns(allSessions, MAX_RECENT_COMPLETED_RUNS); + const seeded = seedRecentRuns(allSessions, MAX_RECENT_COMPLETED_RUNS); + if (prev.length === 0) return seeded; + const newestPrev = Math.max(...prev.map((p) => p.completedAt), 0); + const newestSeeded = Math.max(...seeded.map((s) => s.completedAt), 0); + if (newestSeeded > newestPrev) { + return seeded; + } + return prev; }); }, [allSessions]); diff --git a/lib/recent-runs.ts b/lib/recent-runs.ts index 1a7f86a4b..ae3b2c2b4 100644 --- a/lib/recent-runs.ts +++ b/lib/recent-runs.ts @@ -39,11 +39,11 @@ export function saveRecentRuns(entries: RecentRunEntry[], storage?: Storage): vo } export function seedRecentRuns( - allSessions: Array<{ id: string; modified: string; messageCount: number }>, + allSessions: Array<{ id: string; modified: string; messageCount?: number; firstMessage?: string; name?: string }>, limit = MAX_RECENT_COMPLETED_RUNS, ): RecentRunEntry[] { return [...allSessions] - .filter((s) => s.messageCount > 0) + .filter((s) => (s.firstMessage && s.firstMessage !== "(no messages)") || (s.messageCount ?? 0) > 0 || Boolean(s.name)) .sort((a, b) => new Date(b.modified).getTime() - new Date(a.modified).getTime()) .slice(0, limit) .map((s) => ({ From 115b8681a144bdbafc44aafe302c178c3112d6fd Mon Sep 17 00:00:00 2001 From: lutfi-zain <147457356+lutfi-zain@users.noreply.github.com> Date: Mon, 19 Oct 2026 20:11:01 +0700 Subject: [PATCH 4/4] feat(settings): make recent runs filter limit configurable (10, 15, 20, Custom) --- components/SessionSidebar.tsx | 23 ++-- components/SettingsConfig.tsx | 41 +++++++ hooks/useRecentRunsLimit.test.mjs | 184 ++++++++++++++++++++++++++++++ hooks/useRecentRunsLimit.ts | 172 ++++++++++++++++++++++++++++ lib/i18n/locales/en.json | 9 +- lib/i18n/locales/ja.json | 9 +- lib/i18n/locales/zh-CN.json | 9 +- lib/recent-runs.test.mjs | 83 ++++++++++++++ lib/recent-runs.ts | 18 ++- 9 files changed, 528 insertions(+), 20 deletions(-) create mode 100644 hooks/useRecentRunsLimit.test.mjs create mode 100644 hooks/useRecentRunsLimit.ts diff --git a/components/SessionSidebar.tsx b/components/SessionSidebar.tsx index 9979612ec..1e0781489 100644 --- a/components/SessionSidebar.tsx +++ b/components/SessionSidebar.tsx @@ -21,9 +21,9 @@ import { removeActiveFromCompleted, saveRecentRuns, seedRecentRuns, - MAX_RECENT_COMPLETED_RUNS, type RecentRunEntry, } from "@/lib/recent-runs"; +import { useRecentRunsLimit } from "@/hooks/useRecentRunsLimit"; import { EMPTY_PROJECT_SET, INITIAL_RESTORE_MAX_ATTEMPTS, @@ -134,6 +134,7 @@ export const SessionSidebar = memo(function SessionSidebar({ selectedSessionId, const [runningSessionCwds, setRunningSessionCwds] = useState>({}); const [exitedSessions, setExitedSessions] = useState>(() => new Map()); const knownRunningCwdsRef = useRef>(new Map()); + const { limit: recentRunsLimit } = useRecentRunsLimit(); const [unreadSessionIds, setUnreadSessionIds] = useState>(() => loadUnreadSessionIds()); const [recentCompletedRuns, setRecentCompletedRuns] = useState(() => loadRecentRuns()); const effectiveRecentCompletedSet = useMemo(() => { @@ -158,13 +159,13 @@ export const SessionSidebar = memo(function SessionSidebar({ selectedSessionId, } } - // Sort by latest timestamp descending, pick top 10 + // Sort by latest timestamp descending, pick top dynamic limit const sorted = [...timestampMap.entries()] .sort((a, b) => b[1] - a[1]) - .slice(0, MAX_RECENT_COMPLETED_RUNS); + .slice(0, recentRunsLimit); return new Set(sorted.map(([id]) => id)); - }, [recentCompletedRuns, allSessions, runningSessionIds]); + }, [recentCompletedRuns, allSessions, runningSessionIds, recentRunsLimit]); const previousRunningSessionIdsRef = useRef>(new Set()); // Relative session times must age while the sidebar stays open; one shared // minute clock avoids a timer per session row. @@ -292,13 +293,13 @@ export const SessionSidebar = memo(function SessionSidebar({ selectedSessionId, saveUnreadSessionIds(unreadSessionIds); }, [unreadSessionIds]); useEffect(() => { - saveRecentRuns(recentCompletedRuns); - }, [recentCompletedRuns]); + saveRecentRuns(recentCompletedRuns, undefined, recentRunsLimit); + }, [recentCompletedRuns, recentRunsLimit]); useEffect(() => { if (allSessions.length === 0) return; setRecentCompletedRuns((prev) => { - const seeded = seedRecentRuns(allSessions, MAX_RECENT_COMPLETED_RUNS); + const seeded = seedRecentRuns(allSessions, recentRunsLimit); if (prev.length === 0) return seeded; const newestPrev = Math.max(...prev.map((p) => p.completedAt), 0); const newestSeeded = Math.max(...seeded.map((s) => s.completedAt), 0); @@ -307,7 +308,7 @@ export const SessionSidebar = memo(function SessionSidebar({ selectedSessionId, } return prev; }); - }, [allSessions]); + }, [allSessions, recentRunsLimit]); // Debounce refresh bursts (agent_start + session_info_update + file-appear signal can fire within 250ms) @@ -405,7 +406,7 @@ export const SessionSidebar = memo(function SessionSidebar({ selectedSessionId, updated = removeActiveFromCompleted(updated, runningSessionIds); } for (const id of allCompleted) { - updated = recordCompletedRun(updated, id); + updated = recordCompletedRun(updated, id, undefined, recentRunsLimit); } return updated; }); @@ -429,7 +430,7 @@ export const SessionSidebar = memo(function SessionSidebar({ selectedSessionId, } previousRunningSessionIdsRef.current = runningSessionIds; - }, [runningSessionIds, selectedSessionId, loadSessions]); + }, [runningSessionIds, selectedSessionId, loadSessions, recentRunsLimit]); useEffect(() => { if (!selectedSessionId) return; @@ -1346,7 +1347,7 @@ export const SessionSidebar = memo(function SessionSidebar({ selectedSessionId, setRunningOnly((v) => !v)} > diff --git a/components/SettingsConfig.tsx b/components/SettingsConfig.tsx index 554d37567..b812b5b2b 100644 --- a/components/SettingsConfig.tsx +++ b/components/SettingsConfig.tsx @@ -17,6 +17,7 @@ import type { AppUpdateInfo } from "./AppUpdateDialog"; import { useFontSize, type FontSizePreference } from "@/hooks/useFontSize"; import { useUiScale, type UiScalePreference } from "@/hooks/useUiScale"; import { useTouchTargets, type TouchTargetsPreference } from "@/hooks/useTouchTargets"; +import { useRecentRunsLimit, type RecentRunsLimitPreset } from "@/hooks/useRecentRunsLimit"; import { useSpeechSynthesis } from "@/hooks/useSpeechSynthesis"; import { useNotificationPrefs } from "@/hooks/useNotifications"; import { NOTIFICATION_TYPES, renderNotification, type NotificationType } from "@/lib/notification-events"; @@ -807,6 +808,12 @@ export function SettingsConfig({ activeTab, toolCallsDefaultCollapsed, onToolCal const { fontSize, setFontSize } = useFontSize(); const { uiScale, setUiScale } = useUiScale(); const { touchTargets, setTouchTargets } = useTouchTargets(); + const { + preset: recentRunsPreset, + customValue: recentRunsCustomValue, + setPreset: setRecentRunsPreset, + setCustomValue: setRecentRunsCustomValue, + } = useRecentRunsLimit(); const { isSupported: ttsSupported, autoPlayEnabled: ttsAutoPlay, @@ -1327,6 +1334,40 @@ export function SettingsConfig({ activeTab, toolCallsDefaultCollapsed, onToolCal + +
+ + {recentRunsPreset === "custom" && ( + setRecentRunsCustomValue(Number(e.target.value))} + style={{ + width: 80, + height: "var(--control-height)", + padding: "0 10px", + border: "1px solid var(--border)", + borderRadius: "var(--radius-control)", + background: "var(--bg)", + color: "var(--text)", + fontSize: "var(--text-sm)", + }} + placeholder={t("settingsConfig.recentRunsCustomPlaceholder")} + /> + )} +
+