# Session switching and recent session listing This document describes how coding-agent discovers recent sessions, resolves `--resume` targets, presents session pickers, and switches the active runtime session. It focuses on current implementation behavior, including fallback paths and caveats. ## Implementation files - [`../src/session/session-manager.ts`](../packages/coding-agent/src/session/session-manager.ts) - [`../src/session/session-listing.ts`](../packages/coding-agent/src/session/session-listing.ts) - [`../src/session/session-paths.ts`](../packages/coding-agent/src/session/session-paths.ts) - [`../src/session/agent-session.ts`](../packages/coding-agent/src/session/agent-session.ts) - [`../src/cli/session-picker.ts`](../packages/coding-agent/src/cli/session-picker.ts) - [`../src/modes/components/session-selector.ts`](../packages/coding-agent/src/modes/components/session-selector.ts) - [`../src/modes/controllers/selector-controller.ts`](../packages/coding-agent/src/modes/controllers/selector-controller.ts) - [`../src/main.ts`](../packages/coding-agent/src/main.ts) - [`../src/sdk.ts`](../packages/coding-agent/src/sdk.ts) - [`../src/modes/interactive-mode.ts`](../packages/coding-agent/src/modes/interactive-mode.ts) - [`../src/modes/utils/ui-helpers.ts`](../packages/coding-agent/src/modes/utils/ui-helpers.ts) ## Recent-session discovery ### Directory scope `SessionManager` stores sessions under a cwd-scoped directory by default: - `~/.omp/agent/sessions//*.jsonl` (home-relative `-` names, `-tmp-` for temp paths, legacy `----` otherwise) `SessionManager.list(cwd, sessionDir?)` reads only that directory unless an explicit `sessionDir` is provided. ### Two listing paths with different payloads There are two different listing pipelines: 1. `getRecentSessions(sessionDir, limit)` (welcome/summary view) - Reads only a 4KB prefix (`readTextSlices(..., 4096, 0)[0]`) from each file. - Parses header + earliest user text preview. - Returns lightweight `RecentSessionInfo` (`path`, `name`, `timeAgo`); `name` and `timeAgo` are computed eagerly (`sessionDisplayName` / `formatTimeAgo`), not lazy getters. - Sorts by file `mtime` descending. 2. `SessionManager.list(...)` / `SessionManager.listAll()` (resume pickers and ID matching) - Reads a 4KB prefix plus a bounded 32 KiB tail in one `readTextSlices(...)` call per file, not the full JSONL file. - Builds `SessionInfo` objects (`id`, `cwd`, `title`, `messageCount`, `firstMessage`, `allMessagesText`, timestamps, lifecycle status). - Uses prefix parsing plus marker counting for list text, and tail parsing for the final-message lifecycle status; later messages beyond the prefix may not be present in `allMessagesText`. - Sorts by `modified` descending. ### Metadata fallback behavior For recent summaries (`RecentSessionInfo`): - display name preference (`sessionDisplayName`): `title` -> first user message -> an `Untitled ·