Compare commits

..
Author SHA1 Message Date
Codeman maintainer c13b3c55d3 style: drop em-dashes from the prose added in this branch
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-10 03:15:13 +02:00
Codeman maintainer 053a6d238d fix(web): adopt #263's fetch ceiling, persisted sort and numeric collation
@jordan8037310 opened #263 against the same two issues while this branch
was in flight. Three details there are better than what this had, so they
are folded in with credit:

- the Resume list pulls 200 unified sessions instead of 60, so the filter
  can reach a real backlog rather than stopping at an arbitrary ceiling
  (the endpoint clamps at 500),
- the sort choice persists per device in localStorage, like `codeman:skin`
  and the other display keys that stay out of the synced schema,
- alphabetical sorts collate with `{sensitivity:'base', numeric:true}`, so
  w2- sorts before w10- and case never splits one project's rows apart.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-10 03:12:02 +02:00
Codeman maintainer 5d42f64393 fix(web): usable past-conversation list, and search that finds past sessions
Two home-screen reports from @jordan8037310, both about history that is
present but unreachable.

#260 — "Resume Conversation" rendered 4 rows, then a button that appended
every remaining row into a `max-height: 240px` box, so 35 conversations
landed in a four-row scroll well with no ordering or filtering. Rendering
now goes through `_renderHistoryList()` over a cached corpus: 10 rows to
start, Show more/Show less that grows and shrinks the box (the height cap
is class-driven, `.history-list.expanded`), plus a filter box (name,
folder, #case label, prompts), a sort control (recent / name / folder,
pinned rows still first) and a shown-of-total count. A filter implies
expansion, so every match is visible, and the whole header hides as one
unit while a federated search is active. The A-Z sort keys off the same
string the row renders, since most rows are transcript-backed and carry
no session name at all.

#261 — the search box could not match a past project by folder name:
`harvestSources()` built its session corpus from the live in-memory map,
while past sessions come from `/api/sessions/unified` (lifecycle log +
transcript scan). Folding that scan into the request path would have cost
the search its no-filesystem-reads property, so the corpus arrives via a
bounded snapshot instead: `session-history-index.ts` is published as a
side effect of `/api/sessions/unified` (the home screen fetches it on
open, which is the same screen the search box lives on) and rebuilt
fire-and-forget, single-flight and TTL-guarded when a search finds it
stale. A result for a closed session now resumes the conversation rather
than selecting a tab that no longer exists, and is badged RESUME.

The snapshot is stored unscoped with a per-row owner and re-filtered
through canAccessOwned() on read, so multi-user sees exactly what
/api/sessions/unified exposes: own sessions only, host-wide transcript
history admin-only. Live rows are harvested first and win the dedupe.

Verified end-to-end against a real instance with 60 past sessions: cold
process answers its first search without history and its second with it;
folder-name queries return resume targets; clicking one posts the right
resumeSessionId + workingDir.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-10 03:02:11 +02:00
26 changed files with 1284 additions and 480 deletions
@@ -0,0 +1,24 @@
---
'aicodeman': patch
---
Home screen: make the past-conversation list usable, and let search find past sessions.
- **#260**: "Resume Conversation" showed 4 rows and then dumped every remaining
one into a fixed 240px box, with no ordering or filtering. The list now opens
with 10 rows, "Show more"/"Show less" grows and shrinks the box itself (the
height cap is class-driven instead of fixed), and the header carries a filter
box (matches name, folder, `#case` label and the conversation's prompts), a
sort control (recent / name A–Z / folder A–Z, pinned rows still first) and a
shown-of-total count. Filtering implies expansion, so every match is visible.
- **#261**: the search box could not match a past project by folder name: its
session corpus was the live in-memory map, while past sessions come from
`/api/sessions/unified`. Search now also harvests a bounded snapshot of that
unified list, refreshed OUTSIDE the request path (published by
`/api/sessions/unified`, plus a fire-and-forget rebuild when stale), so the
search path keeps its no-filesystem-reads property. Results for a closed
session resume the conversation instead of trying to select a tab that no
longer exists, and are badged `RESUME`. In multi-user mode the snapshot is
re-scoped per row on read, matching what `/api/sessions/unified` exposes.
Reported by @jordan8037310.
+5 -3
View File
@@ -210,7 +210,7 @@ Codeman is a Claude Code session manager with web interface and autonomous Ralph
**Approvals Inbox** (cross-session queue of prompts waiting on a human; `approvalsInboxEnabled`, SYNCED, default OFF: every surface is opt-in; only the store and answer endpoints run regardless, so flipping it ON shows anything already pending): `web/approval-inbox.ts` is a `sessionWaits`-style singleton fed by `/api/hook-event`, holding at most ONE item per session (a new prompt supersedes), claude-mode only, in-memory. Cards are answered via `POST /api/approvals/:id/answer`, which sends a digit / Esc / idle-prompt text through `writeViaMux` (menu answers never carry `\r`). ⚠️ `option` digits are accepted ONLY when they match options parsed from the captured pane frame, and the answer path RE-CAPTURES the pane first (a dialog that no longer parses on screen means the keystroke would land in the composer, so refuse with 409). ⚠️ Resolution on the heuristic `working` signal is restricted to `idle` items; permission/question items clear only on definitive signals (`stop`, `elicitation_complete`/`elicitation_response`, exit/delete, answer, supersede, 12h TTL). The frontend seeds from `GET /api/approvals` in `handleInit` (which is what makes tab alerts survive reloads), but only with the setting ON; push Approve/Deny buttons are also gated on it (`sendPushNotifications` strips `actions`/`approvalId` when OFF) and are answered from `sw.js` directly so they work with no tab open. Surfaces (all gated on the setting): header bell (marker-hidden until count > 0, phones never show it) + drawer (`approvals-ui.js`), phone overview NEEDS YOU answer strips (`mobile-overview.js`). Design: `docs/approvals-inbox-plan.md`.
**Read My Mind intent profiles** (phase 1 of `docs/readmymind-plan.md`; `readMyMindEnabled`, SYNCED, default OFF): per-CASE profiles (user-stated `goals` + the user's recent real prompts), keyed by owner + realpath(workingDir) so they survive `/clear`/respawns and multi-user scoping is structural. Capture rides the transcript (`transcript:user_prompt` from `transcript-watcher.ts`), NOT the input paths: `POST /input` sees only programmatic prompts and the WS channel is raw keystrokes. The listener lives inside `startTranscriptWatcher()`'s `if (!watcher)` block (outside it would duplicate per hook event) and is claude-only + gated on the setting per event. Store: `src/intent-store.ts` singleton, `intents.json` written 0600 tmp+rename (prompts can contain secrets; never fed to `/api/search`). Endpoints: GET/PUT/DELETE `/api/sessions/:id/intent` + POST `/api/sessions/:id/readmymind` (`readmymind-routes.ts`, ownership via `findSessionOrFail` WITH `req`; registrations stay the bare `app.<method>('path')` shape, the endpoints.md drift scanner cannot see generics). **Phase 2 (predictor + 🧠 button)**: `readmymind-context.ts` is the PURE budgeted assembler (9 ranked sources, drop order siblings→away→workspace→tools, sections 1-4 truncate only); IO lives in `readmymind-collectors.ts` (transcript TAIL read — the live watcher keeps only a 500-char snippet — + git signals, skipped for remote-SSH cases) and the route; `readmymind-predictor.ts` reuses the AiCheckerBase spawn mechanics standalone (verdict-shaped base vs freeform JSON) as a mutable singleton routes call and tests stub. Claude-mode only (400), one in flight per session (409 CONFLICT), model = `readMyMindModel` setting defaulting to `AI_CHECK_MODEL` (opus, decided). Frontend `readmymind-ui.js`: header 🧠 marker-hidden (`btn-readmymind--hidden`) until the setting is ON, desktop; phones get the keyboard-accessory 🧠 key (`refreshReadMyMind()` in keyboard-accessory.js, re-derived after every innerHTML rebuild + settings apply + the tab-render tail; `.accessory-btn[hidden]` must stay re-asserted, inline-flex beats the UA hidden rule) plus a `🧠 Suggest` strip on the overview's YELLOW waiting rows ONLY (on red rows a dialog is on screen and `POST /input` text would land in its menu; answer routing via the approvals endpoint is phase-3 PR 2). Up to 3 kind-diverse suggestions: alternates swap into the editable field, Rethink records everything displayed as rejected and carries the optional steer note (resets each open, like the rejections); suggestions render via value/`textContent` ONLY and Send/Insert go through `POST /input` (server-side, so the sendEnterKey/local-echo trap does not apply) — nothing auto-sends, ever. User guide: `docs/readmymind.md`.
**Read My Mind intent profiles** (phase 1 of `docs/readmymind-plan.md`; `readMyMindEnabled`, SYNCED, default OFF): per-CASE profiles (user-stated `goals` + the user's recent real prompts), keyed by owner + realpath(workingDir) so they survive `/clear`/respawns and multi-user scoping is structural. Capture rides the transcript (`transcript:user_prompt` from `transcript-watcher.ts`), NOT the input paths: `POST /input` sees only programmatic prompts and the WS channel is raw keystrokes. The listener lives inside `startTranscriptWatcher()`'s `if (!watcher)` block (outside it would duplicate per hook event) and is claude-only + gated on the setting per event. Store: `src/intent-store.ts` singleton, `intents.json` written 0600 tmp+rename (prompts can contain secrets; never fed to `/api/search`). Endpoints: GET/PUT/DELETE `/api/sessions/:id/intent` + POST `/api/sessions/:id/readmymind` (`readmymind-routes.ts`, ownership via `findSessionOrFail` WITH `req`; registrations stay the bare `app.<method>('path')` shape, the endpoints.md drift scanner cannot see generics). **Phase 2 (predictor + 🧠 button)**: `readmymind-context.ts` is the PURE budgeted assembler (9 ranked sources, drop order siblings→away→workspace→tools, sections 1-4 truncate only); IO lives in `readmymind-collectors.ts` (transcript TAIL read — the live watcher keeps only a 500-char snippet — + git signals, skipped for remote-SSH cases) and the route; `readmymind-predictor.ts` reuses the AiCheckerBase spawn mechanics standalone (verdict-shaped base vs freeform JSON) as a mutable singleton routes call and tests stub. Claude-mode only (400), one in flight per session (409 CONFLICT), model = `readMyMindModel` setting defaulting to `AI_CHECK_MODEL` (opus, decided). Frontend `readmymind-ui.js`: header 🧠 marker-hidden (`btn-readmymind--hidden`) until the setting is ON, desktop-only (mobile.css hides it; phone key is phase 3); suggestions render via value/`textContent` ONLY and Send/Insert go through `POST /input` (server-side, so the sendEnterKey/local-echo trap does not apply) — nothing auto-sends, ever. User guide: `docs/readmymind.md`.
**Agent Teams**: `TeamWatcher` polls `~/.claude/teams/`, matches to sessions via `leadSessionId`. Teammates are in-process threads appearing as subagents. Enable: `CLAUDE_CODE_EXPERIMENTAL_AGENT_TEAMS=1`. See `docs/agent-teams/`.
@@ -234,7 +234,7 @@ Codeman is a Claude Code session manager with web interface and autonomous Ralph
**Clone a repository as a case** (issue #236, Add Case → **Clone Repo**): `POST /api/cases/clone` clones a public repo into the caller's case space synchronously (request held open, bounded by `GIT_CLONE_TIMEOUT_MS`, no job store); `POST /api/cases/clone-preflight` reports whether the URL can be cloned anonymously plus its real branches/tags. Core in `src/git-clone.ts`. ⚠️ **The URL is a code-execution surface**: `ext::sh -c <cmd>` (and ANY `<name>::<payload>` helper) makes git run a command, so every `::` form is refused, a leading `-` is refused, and every spawn is an argv array with `--` before the operands. ⚠️ **Non-interactive or the open request hangs** — `gitNonInteractiveEnv()` closes the terminal/askpass/ssh/GCM prompt paths; `HOME`/`PATH` stay inherited, so a user's OWN credential helper may authenticate (Codeman still never collects or stores credentials, and refuses a `user:password@` URL). ⚠️ Timeout kills the process GROUP (clone fans out into child processes), the destination is removed only if this attempt created it, and repository contents win over scaffolding (existing `CLAUDE.md` kept, hooks merged, repo-shipped `.claude/settings*` reported as a warning since its hooks run locally). The **Brain** picker sets the toolbar run mode on success. → [architecture-invariants#clone-a-repository-as-a-case](docs/architecture-invariants.md#clone-a-repository-as-a-case)
**Cross-session search**: `GET /api/search` federates an in-memory search over session metadata, run-summary events, and attachment-history entries. The pure core `searchSources()` does substring matching with hard per-type caps: **no regex (so no ReDoS) and no filesystem reads (so no traversal)**. The server-private `externalPath` is never read. → [architecture-invariants#cross-session-search](docs/architecture-invariants.md#cross-session-search)
**Cross-session search**: `GET /api/search` federates an in-memory search over session metadata, run-summary events, and attachment-history entries. The pure core `searchSources()` does substring matching with hard per-type caps: **no regex (so no ReDoS) and no filesystem reads (so no traversal)**. The server-private `externalPath` is never read. PAST sessions (#261) come from `session-history-index.ts`, a capped snapshot of the unified list filled **outside** the request path (`/api/sessions/unified` publishes it; a stale one is rebuilt fire-and-forget), that indirection is what keeps the no-fs property. ⚠️ The snapshot is stored UNSCOPED with a per-row owner and MUST be re-filtered through `canAccessOwned()` on read; history rows carry `jumpTo.kind:'resume-session'`, since a closed session has no tab to select. → [architecture-invariants#cross-session-search](docs/architecture-invariants.md#cross-session-search)
**Web tabs** (dashboard URLs as tabs): a saved URL renders as a tab beside agent sessions. **NOT a sixth `SessionMode`** (no PTY, no tmux, no respawn), same reasoning that keeps Docker/remote-SSH as case overlays. Dashboards are **proxied through Codeman's own origin** by default, because a direct iframe fails three ways at once: prod is HTTPS so `http://` targets are blocked as mixed content, many dashboards send `X-Frame-Options: DENY`, and our own `default-src 'self'` CSP blocks cross-origin frames. Proxying leaves the prod CSP unchanged (`/webview/...` is `'self'`). ⚠️ The proxy is **NOT an API surface**: it authenticates on an in-memory capability in the path and is correspondingly exempt from the cookie + Origin checks; that exemption is fenced to safe methods and non-`/api` paths and is pinned by `test/webview-auth-exemption.test.ts`. ⚠️ Iframes omit `allow-same-origin` unless a dashboard is explicitly marked `trusted`, and `Authorization`/`codeman_session` are stripped upstream in **both** modes so `CODEMAN_PASSWORD` cannot leak. ⚠️ A sandboxed frame is **opaque-origin**, which breaks two things `curl` can never reproduce: its runtime-built root-absolute URLs escape `<base>` (fixed by an injected `runtimeUrlShim()`), and its same-host `fetch`/XHR are CORS-checked with `Origin: null` (fixed by `buildProxyCorsHeaders()` plus exempting the proxy from the global `OPTIONS`-204 short-circuit in `registerSecurityHeaders`). Both present as the dashboard's own "Failed to fetch" while the page renders fine. → [architecture-invariants#web-tabs](docs/architecture-invariants.md#web-tabs), `docs/web-tabs.md`
@@ -254,7 +254,9 @@ Frontend JS modules have `@fileoverview` with `@dependency`/`@loadorder` tags. L
**Phone overview home screen** (`mobile-overview.js`, phones only, per-device `mobileOverviewEnabled`, default ON): under 430px the "C" logo shows a session overview (NEEDS YOU / CURRENT SESSIONS / PAST SESSIONS) instead of the welcome overlay; tablet and desktop are unchanged. The branch lives in `showWelcome()`/`hideWelcome()` (terminal-ui.js) behind `shouldUseMobileOverview()`, which is **width-driven** (`getDeviceType() === 'mobile'`) because this is a layout decision, unlike the settings namespace which stays handheld-based. ⚠️ The container ships with the `hidden` attribute and only this module removes it: never give `.mobile-overview` a bare `display` rule, since desktop does not load `mobile.css` (`media="(max-width: 1023px)"`) and would then render it unstyled. Live re-renders ride on the tail of `_renderSessionTabsImmediate()` (every state change it needs already funnels there); PAST rows come from one `_fetchUnifiedSessions(60)` per home-screen visit and resume through the shared `resumeHistorySession()`, so they behave exactly like the welcome screen's Resume list. ⚠️ Two things must stay in lockstep with surfaces outside this module, because divergence reads as a bug rather than a style: the split Run button carries the **toolbar's own classes** (`btn-toolbar btn-run mode-<backend>` / `btn-run-gear`) so the per-backend gradient and the light-skin overrides apply unchanged (mobile.css must therefore set no `background`/`color` on it), and row status uses the **session-tab language** (green dot when fine, `pulse` while working, yellow blinking row when waiting for input, red blinking row when a question is pending, mirroring `tab-alert-idle`/`tab-alert-action`). The picker mirrors the toolbar run-mode menu (`setRunMode()` + `run()`, `openWebviewFromMenu()` for saved dashboards) and deliberately omits its Recent-Sessions block, since PAST SESSIONS is that. Status pills carry `data-i18n-skip` (generic words like "idle" collide with state strings elsewhere).
**Desktop home tab column** (`home-sessions.js`, desktop only): the welcome overlay centers ~560px of content in a ~1400px window, so its left gutter is dead space; it now carries the open tabs as a vertical list. Rows are in **tab order**, not sorted by urgency like the phone overview, because the row badges are the Alt+1..9 indices. State classification is REUSED from mobile-overview.js (`_mobileOverviewState`/`_mobileOverviewCaseFor`), which is why the module loads after it. ⚠️ The column is `position: absolute` so the centered content never moves, which is exactly why it needs a **width gate in two places** — `HOME_SESSIONS_MIN_WIDTH` (1180) in the JS plus a `max-width: 1179px` media query as the backstop for a resize that outruns the matchMedia listener; drift between them means a column overlapping the search panel, and `test/home-sessions.test.ts` pins them equal. ⚠️ `.home-sessions` is `display: flex`, so `[hidden]` must be re-asserted as `display: none` or the module's only visibility lever does nothing. Working state is deliberately byte-identical to the phone's: pulsing green dot + the `tab-load-spin` ring reused from the tab strip + the same green halo (added to `.mobile-overview-dot--working` at the same time), so "working" reads the same on every surface. Live re-renders ride the tail of `_renderSessionTabsImmediate()` alongside the phone overview.
**Desktop home tab column** (`home-sessions.js`, desktop only): the welcome overlay centers ~560px of content in a ~1400px window, so its left gutter is dead space; it now carries the open tabs as a vertical list. Rows are in **tab order**, not sorted by urgency like the phone overview, because the row badges are the Alt+1..9 indices. State classification is REUSED from mobile-overview.js (`_mobileOverviewState`/`_mobileOverviewCaseFor`), which is why the module loads after it. ⚠️ The column is `position: absolute` so the centered content never moves, which is exactly why it needs a **width gate in two places**, `HOME_SESSIONS_MIN_WIDTH` (1180) in the JS plus a `max-width: 1179px` media query as the backstop for a resize that outruns the matchMedia listener; drift between them means a column overlapping the search panel, and `test/home-sessions.test.ts` pins them equal. ⚠️ `.home-sessions` is `display: flex`, so `[hidden]` must be re-asserted as `display: none` or the module's only visibility lever does nothing. Working state is deliberately byte-identical to the phone's: pulsing green dot + the `tab-load-spin` ring reused from the tab strip + the same green halo (added to `.mobile-overview-dot--working` at the same time), so "working" reads the same on every surface. Live re-renders ride the tail of `_renderSessionTabsImmediate()` alongside the phone overview.
**Welcome "Resume Conversation" list** (terminal-ui.js): `loadHistorySessions()` fetches once and caches the corpus on `_historyAll`/`_historyCases`; every subsequent view (filter box, sort select, expand, the periodic refresh in panels-ui.js) goes through `_renderHistoryList()`, so never append rows to `#historyList` directly or re-fetch to re-sort. ⚠️ The box height is **class-driven**: expanding the list without `.history-list.expanded` leaves the collapsed `max-height` in place and just deepens a scroll well, which is the bug #260 reported (35 sessions in a ~4-row box). ⚠️ The A–Z sort keys off `_historyRowLabel()`, the SAME string the row renders (`name || firstPrompt || path`), most rows are transcript-backed and have no session name, so sorting on `name` alone silently does nothing. ⚠️ A filter implies expansion, and `_renderSearch()` hides `#historyHeader` (title + controls) as one unit while a search is active. Tests: `test/history-list-controls.test.ts`.
**Command palette + shortcut registry**: `Ctrl/Cmd/Alt+K` opens the session palette; shortcuts live in a rebindable registry (`DEFAULT_SHORTCUTS`/`getShortcutRegistry()`/`matchesShortcutEvent()` in app.js, overrides in `settings.shortcutOverrides`). ⚠️ Palette-chord keys must ALSO be swallowed in `attachCustomKeyEventHandler` (terminal-ui.js) or xterm writes the control byte (0x0B) into the PTY. ⚠️ `saveAppSettings()` rebuilds settings from the DOM, so keys edited elsewhere (`shortcutOverrides`, `showTokenCount`, `showCost`) need explicit `_prev` carry-over. ⚠️ **Smart copy (`Ctrl+C`)** lives in that same handler: with a selection it copies, with none it must `return true` **without** `preventDefault()` or the interrupt is lost. `copyTerminalSelection` is deliberately absent from `SHORTCUT_ACTIONS` because the generic capture loop preventDefaults every match it dispatches. → [architecture-invariants#command-palette-and-shortcut-registry](docs/architecture-invariants.md#command-palette-and-shortcut-registry)
+2
View File
@@ -157,6 +157,8 @@ Tests: `test/git-clone.test.ts` (pure half exhaustively, plus REAL git against a
**Cross-session search** (COD-113/#133): `GET /api/search?q=&types=&limit=` federates an **in-memory** search across all live sessions — session metadata (name/workingDir/id), run-summary events, and per-session attachment-history file entries (workspace-relative path only; the server-private `externalPath` is never read). Pure core `searchSources()` in `search-service.ts` (substring-matches with hard per-type caps — no regex, so no ReDoS; no filesystem reads, so no traversal); `harvestSources()` in `search-routes.ts` gathers the in-memory sources. `SearchQuerySchema` bounds `q` (1–200), allowlists `types` (`session,event,file`), clamps `limit` (1–60). Returns the `{success,data}` envelope. Frontend: history-panel search box in `terminal-ui.js`. Types: `src/types/search.ts`.
**Past sessions in the corpus** (#261): the live session map alone made every CLOSED session unfindable, searching a folder name that was sitting in the home screen's Resume list below the box returned nothing. Past sessions now come from `src/web/session-history-index.ts`: a capped (`HISTORY_INDEX_MAX_ITEMS` 400) snapshot of the unified list, read synchronously by `harvestSources()`. ⚠️ It is filled OUTSIDE the request path, which is what preserves the no-fs property above: `/api/sessions/unified` publishes it as a side effect (free, it just merged that list, and the home screen fetches it whenever it opens, which is the same screen the search box lives on), and `ensureHistorySessionIndexFresh()`, **fire-and-forget, single-flight, TTL-guarded (60s)**, kicks a rebuild when a search finds it stale. A cold process therefore answers its first search without history and its second with it; never `await` the refresher from a handler. ⚠️ The snapshot is stored **UNSCOPED** with a per-row `owner` (`undefined` = host-wide transcript history), and `harvestSources()` re-applies `canAccessOwned()` per row, the same rule `/api/sessions/unified` applies when it drops history for non-admins. A scoped (non-admin) unified request therefore re-merges unscoped before publishing, rather than writing its own subset into the shared snapshot. ⚠️ Live rows are harvested FIRST and win the dedupe, so a session that is both live and in the snapshot keeps `jumpTo.kind:'session'`; history rows get `'resume-session'` (with `claudeSessionId`/`workingDir`), because selecting a tab that no longer exists is a silent no-op the user reads as a broken result. Tests: `test/session-history-index.test.ts`, `test/routes/search-routes.test.ts`.
### Away digest
**Away digest** (COD-41/#136): `GET /api/away-digest?range=&since=&until=&lastViewed=` aggregates "what happened while you were away" from the lifecycle log + run-summary events + live sessions + daily token stats + recently-completed subagents into needs-attention/completed/still-running/idle/informational sections. Pure aggregator in `web/away-digest.ts` (`resolveAwayDigestRange()` validates the window — `since-last-visit`/`1h`/`today`/`24h`/`custom`, server-local TZ; `buildAwayDigest()` classifies). Header-button modal in `panels-ui.js` (button hidden on phones — regression-guarded). ⚠️ Returns `{success:true,digest}` (a legacy raw-ish shape, consistent with the other raw GET handlers in `system-routes.ts` — `{entries}`/`{config}`/`{files}`/`getSystemStats()`); frontend + tests read `.digest`. Subagent lookback is a fixed 60-min window regardless of range.
+5 -6
View File
@@ -122,15 +122,14 @@ Agent use cases this unlocks: a lead session records intentions as the user stat
## Phases
1. **Intent store + capture + intent endpoints + skill docs.** Immediately useful to agents even before any UI exists. Shipped 1.16.1 (PR #253).
2. **Context assembler + predictor + predict endpoint + desktop button/modal.** The feature as pitched. The assembler ships with all collectors it can serve from day one (transcript, intent, git, run-summary, siblings); the approvals collector activates when PR #245 lands. Shipped 1.16.2 (PR #256).
3. **Phase 3, PR 1: alternates row, rethink steering, phone surfaces.** Alternate suggestions as tappable rows that swap into the field; Rethink records everything displayed as rejected and carries the optional steer note; keyboard-accessory 🧠 key (`refreshReadMyMind()`, re-derived after every innerHTML rebuild, settings apply, and session switch); `🧠 Suggest` strip on the phone overview's YELLOW waiting rows only. Red rows deliberately get no shortcut yet: a dialog is on screen there and text sent via `POST /input` would land in its menu.
4. **Phase 3, PR 2: the approvals fusion** (the Cloudflare OS learnings tie-in, items 1-2 of `cloudflare-os-learnings-plan.md`). `GET /api/sessions/:id/recap`: a deterministic "what was done, simplified" catch-up (last assistant tail + recent tool one-liners + git state) reusing the phase-2 collectors verbatim; no model call, effectively an observation-ledger v0 whose data source can later swap to a real ledger. Surfaces: collapsible "What happened" on approval cards (lazy-fetched), recap in the modal's loading phase (read while opus thinks), 🧠 on approval cards and red overview rows. Answer-aware Send: the predictor output gains an optional `answer` option number (validated against the pending dialog's parsed options, dropped when invalid) and the modal routes dialog answers through `POST /api/approvals/:id/answer` (option digits / idle text), keeping `POST /input` only for dialog-free sessions.
5. Explicitly later: proactive predict-on-idle (ghost suggestion chip), auto-compaction of `recentPrompts` into `goals` via a cheap model, codex/gemini capture, cross-case "global" intent, a model-written prose recap (deterministic-only in v1).
1. **Intent store + capture + intent endpoints + skill docs.** Immediately useful to agents even before any UI exists.
2. **Context assembler + predictor + predict endpoint + desktop button/modal.** The feature as pitched. The assembler ships with all collectors it can serve from day one (transcript, intent, git, run-summary, siblings); the approvals collector activates when PR #245 lands.
3. **Phone accessory key, rethink steering, alternates row.**
4. Explicitly later: proactive predict-on-idle (ghost suggestion chip), auto-compaction of `recentPrompts` into `goals` via a cheap model, codex/gemini capture, cross-case "global" intent.
## Open questions
- ~~Should Rethink's rejected-suggestion memory persist across modal closes, or reset each open?~~ Decided in phase 2 and kept: reset each open (a fresh open is a fresh question); the steer note resets with it.
- Should Rethink's rejected-suggestion memory persist across modal closes, or reset each open?
- Is a composer-adjacent placement (next to the toolbar Run controls) better than the header for discoverability?
- Pending-dialog input (source #1) consumes the approvals-inbox store (PR #245, merged): the phase-2 collector reads pending items directly from `src/approval-inbox.ts`.
+4 -5
View File
@@ -23,11 +23,11 @@ Add `-u user:password` if your install has `CODEMAN_PASSWORD` set, and drop `-k`
## The 🧠 button
On a Claude session, press the brain button in the header (desktop). On phones the same modal opens from the 🧠 key on the keyboard accessory bar, or from the `🧠 Suggest` strip under a yellow waiting row on the phone overview home screen (steer a session without opening its tab). Codeman assembles everything it already knows: your goals, your recent prompts (with your voice: length, tone, shorthand), the tail of the last assistant reply, recent tool activity, git state (branch, dirty files, pending changesets), how long you have been away and what happened meanwhile, sibling sessions in the same case, and any dialog the session is currently waiting on. A one-shot model call (opus by default, `readMyMindModel` to override) turns that into 1-3 kind-diverse suggestions (continue / verify / redirect); the top one lands in an editable field with its rationale, and the others render as tappable alternate rows that swap into the field.
On a Claude session, press the brain button in the header (desktop; the phone surface is a planned keyboard-accessory key). Codeman assembles everything it already knows: your goals, your recent prompts (with your voice: length, tone, shorthand), the tail of the last assistant reply, recent tool activity, git state (branch, dirty files, pending changesets), how long you have been away and what happened meanwhile, sibling sessions in the same case, and any dialog the session is currently waiting on. A one-shot model call (opus by default, `readMyMindModel` to override) turns that into 1-3 suggestions; the top one lands in an editable field with its rationale.
- **Send** submits it to the session (with Enter).
- **Insert** drops it on the CLI composer *without* Enter, so you can edit it in the terminal before sending.
- **Rethink** re-runs with everything currently displayed recorded as rejected. The optional steer field above the buttons ("no, I meant the mobile bug") tells the re-run what you actually meant; it is your own words and outranks everything the predictor observed.
- **Rethink** re-runs with the shown suggestion recorded as rejected.
- **Dismiss** closes; nothing happens.
A prediction takes 5-90 seconds and costs real tokens; one runs per session at a time. If the session is sitting on a permission/question dialog, the suggestion is usually an answer to that dialog: that is intentional.
@@ -87,14 +87,13 @@ The `codeman` agent skill documents the same verbs (SKILL.md §3 plus `reference
## What comes next (phase 3+)
The approvals fusion: a per-session catch-up recap ("what was done, simplified", derived from the same collectors the predictor uses), shown on Approvals Inbox cards next to a 🧠 button, plus answer-aware Send that routes dialog answers through the approvals endpoint instead of typing into a menu. Explicitly later: proactive predict-on-idle, auto-compaction of the prompt history into goals, non-Claude capture. See the phases section of [`readmymind-plan.md`](readmymind-plan.md).
Phone keyboard-accessory 🧠 key, a steer-note input on Rethink, and tappable alternate suggestions. Explicitly later: proactive predict-on-idle, auto-compaction of the prompt history into goals, non-Claude capture. See the phases section of [`readmymind-plan.md`](readmymind-plan.md).
## Troubleshooting
| Symptom | Cause / fix |
| ------- | ----------- |
| No 🧠 button in the header | `readMyMindEnabled` is OFF (App Settings → Panels), you are on a phone (the header button is desktop-only; phones use the keyboard-accessory 🧠 key and the overview waiting rows), or the active session is not claude-mode |
| No 🧠 key on the phone keyboard bar | `readMyMindEnabled` is OFF, or the active session is not claude-mode (the key hides itself for codex/gemini/opencode/antigravity/shell sessions) |
| No 🧠 button in the header | `readMyMindEnabled` is OFF (App Settings → Panels), you are on a phone (desktop-only in this phase), or the active session is not claude-mode |
| Prediction feels generic | The profile is thin: record goals (PUT or ask your agent to), and let capture accumulate a few real prompts first |
| "A prediction is already running" (409) | One per session at a time; wait for the current one (up to 90 s) |
| Prediction fails (502) | The model returned no usable JSON, or the CLI could not start; retry. Check `readMyMindModel` if you overrode it |
+23 -4
View File
@@ -36,13 +36,21 @@ export const SEARCH_PER_GROUP_CAP = 25;
/** Maximum characters in a result snippet. */
export const SEARCH_SNIPPET_MAX = 200;
/** A live-session row harvested for the session/case source. */
/** A session row harvested for the session/case source (live or past). */
export interface SessionSearchInput {
sessionId: string;
sessionName: string;
workingDir: string;
/** Recency timestamp (e.g. lastActivityAt or createdAt). */
timestamp: number;
/**
* True for a session that is no longer running (issue #261, past sessions come
* from the history index, not the live map). Such a result resumes the
* conversation instead of switching to a tab that no longer exists.
*/
history?: boolean;
/** Claude conversation UUID to resume, when it differs from the Codeman id. */
claudeSessionId?: string;
}
/** A run-summary timeline event harvested for the event source. */
@@ -121,14 +129,25 @@ export function searchSources(query: string, sources: SearchSources): SearchResp
const sessionRows: SearchResult[] = [];
for (const s of sources.sessions) {
if (contains(s.sessionName) || contains(s.workingDir) || contains(s.sessionId)) {
const label = s.sessionName || s.workingDir.split('/').pop() || s.sessionId;
sessionRows.push({
type: 'session',
sessionId: s.sessionId,
sessionName: s.sessionName,
sessionName: label,
timestamp: s.timestamp,
snippet: truncate(s.workingDir ? `${s.sessionName} — ${s.workingDir}` : s.sessionName),
snippet: truncate(s.workingDir ? `${label} — ${s.workingDir}` : label),
exactMatch: isExact(s.sessionName),
jumpTo: { kind: 'session', sessionId: s.sessionId },
// A resume needs a directory to run in, so a history row without one
// stays a plain session target rather than an action that cannot work.
jumpTo:
s.history && s.workingDir
? {
kind: 'resume-session',
sessionId: s.sessionId,
claudeSessionId: s.claudeSessionId,
workingDir: s.workingDir,
}
: { kind: 'session', sessionId: s.sessionId },
});
}
}
+17 -3
View File
@@ -22,15 +22,19 @@ export type SearchSourceType = 'session' | 'event' | 'file';
/** Where the frontend should jump when a result card is activated. */
export interface SearchJumpTarget {
/** Kind of navigation target. */
kind: 'session' | 'run-summary' | 'file-preview';
/**
* Kind of navigation target. `resume-session` marks a session that is no longer
* running: selecting it has to REPLAY the conversation rather than switch to a
* tab that does not exist.
*/
kind: 'session' | 'run-summary' | 'file-preview' | 'resume-session';
/** Owning Codeman session id (always present — every result is session-scoped). */
sessionId: string;
/**
* Secondary identifier for the target:
* - kind 'run-summary': the run-summary event id
* - kind 'file-preview': the attachment history item id
* - kind 'session': undefined (the sessionId is sufficient)
* - kind 'session' / 'resume-session': undefined (the sessionId is sufficient)
*/
targetId?: string;
/**
@@ -38,6 +42,16 @@ export interface SearchJumpTarget {
* server-private external paths are intentionally omitted to avoid leakage.
*/
relativePath?: string;
/**
* `resume-session` only: the Claude conversation UUID to resume, when it differs
* from the Codeman session id (resumed and `/clear`-respawned sessions).
*/
claudeSessionId?: string;
/**
* `resume-session` only: the directory to resume in. Already visible in the
* result snippet for session rows, so this exposes nothing new.
*/
workingDir?: string;
}
/** A single typed search result card. */
-4
View File
@@ -3688,10 +3688,6 @@ class CodemanApp {
this._refreshMobileOverviewIfVisible?.();
// Same deal for the desktop home screen's tab column.
this._refreshHomeSessionsIfVisible?.();
// The keyboard accessory bar's 🧠 key depends on the active session's mode
// (claude only) and the readMyMindEnabled setting; session switches and
// mode changes all funnel through this render.
if (typeof KeyboardAccessoryBar !== 'undefined') KeyboardAccessoryBar.refreshReadMyMind?.();
}
// Auto-wrap desktop session tabs to a second row when they overflow one row,
+1 -1
View File
@@ -34,7 +34,7 @@
/**
* Narrowest window that gets the column. The welcome content is 560px wide and
* centered, so at 1180px each gutter is 310px — enough for the 256px column plus
* centered, so at 1180px each gutter is 310px, enough for the 256px column plus
* its 20px offset and still a visible gap. Anything narrower would overlap the
* search panel, which is why this is a width gate and not a device-type gate.
*/
-2
View File
@@ -257,8 +257,6 @@
'Reading your mind…': '正在读取您的想法…',
'No suggestion this time. Rethink to try again.': '这次没有建议。点击「重想」再试一次。',
Rethink: '重想',
'Steer the rethink (optional): tell it what you actually meant': '引导重想(可选):告诉它您真正的意思',
'Optional steer note for Rethink': '重想的可选引导说明',
Insert: '插入',
"Put the text on the session's composer without submitting it": '将文本放入会话输入框但不提交',
'Predicted prompt, editable': '预测的提示,可编辑',
+24 -8
View File
@@ -397,7 +397,27 @@
</div>
<div class="search-results" id="searchResults" hidden></div>
</div>
<h3 class="history-title" id="historyTitle">Resume Conversation</h3>
<div class="history-header" id="historyHeader">
<h3 class="history-title" id="historyTitle">Resume Conversation</h3>
<span class="history-count" id="historyCount" data-i18n-skip></span>
<div class="history-controls">
<input
type="search"
id="historyFilter"
class="history-filter"
placeholder="Filter…"
autocomplete="off"
spellcheck="false"
maxlength="100"
aria-label="Filter past conversations"
/>
<select id="historySort" class="search-select history-sort" aria-label="Sort past conversations">
<option value="recent">Recent</option>
<option value="name">Name A–Z</option>
<option value="folder">Folder A–Z</option>
</select>
</div>
</div>
<div class="history-list" id="historyList"></div>
</div>
<p class="welcome-hint">Or click Run to start</p>
@@ -2802,7 +2822,7 @@
<div class="modal-backdrop" onclick="app.closeReadMyMind()"></div>
<div class="modal-content readmymind-modal">
<div class="modal-header">
<h3><span aria-hidden="true">🧠</span> Read My Mind<span class="readmymind-session" id="readMyMindSession" data-i18n-skip></span></h3>
<h3><span aria-hidden="true">🧠</span> Read My Mind</h3>
<button class="modal-close" onclick="app.closeReadMyMind()" aria-label="Close">&times;</button>
</div>
<div class="modal-body">
@@ -2813,18 +2833,14 @@
<input type="text" id="readMyMindPrompt" class="readmymind-prompt-input" data-i18n-skip aria-label="Predicted prompt, editable" onkeydown="if(event.key==='Enter')app.sendReadMyMind(true)">
</div>
<div class="readmymind-why" id="readMyMindWhy" data-i18n-skip></div>
<div class="readmymind-alternates" id="readMyMindAlternates" data-i18n-skip></div>
</div>
<div class="readmymind-error" style="display:none">No suggestion this time. Rethink to try again.</div>
<!-- Outside the result div on purpose: steering matters in the error
phase too (Rethink retries with the note); hidden only while loading. -->
<input type="text" id="readMyMindSteer" class="readmymind-steer-input" style="display:none" placeholder="Steer the rethink (optional): tell it what you actually meant" aria-label="Optional steer note for Rethink" onkeydown="if(event.key==='Enter')app.rethinkReadMyMind()">
</div>
<div class="modal-footer">
<button class="btn btn-secondary" onclick="app.closeReadMyMind()">Dismiss</button>
<button class="btn btn-secondary" id="readMyMindRethink" onclick="app.rethinkReadMyMind()">Rethink</button>
<button class="btn btn-secondary" id="readMyMindInsert" onclick="app.sendReadMyMind(false)" title="Put the text on the session's composer without submitting it">Insert</button>
<button class="btn btn-primary" id="readMyMindSend" onclick="app.sendReadMyMind(true)">Send</button>
<button class="btn btn-secondary" onclick="app.sendReadMyMind(false)" title="Put the text on the session's composer without submitting it">Insert</button>
<button class="btn btn-primary" onclick="app.sendReadMyMind(true)">Send</button>
</div>
</div>
</div>
-30
View File
@@ -441,7 +441,6 @@ const KeyboardAccessoryBar = {
<rect x="8" y="2" width="8" height="4" rx="1" ry="1"/>
</svg>
</button>
<button class="accessory-btn accessory-btn-readmymind" data-action="readmymind" title="Read My Mind: predict your next prompt" hidden>&#x1F9E0;</button>
<button class="accessory-btn" data-action="esc" title="Escape">Esc</button>
<button class="accessory-btn accessory-btn-dismiss" data-action="dismiss" title="Dismiss keyboard">
<svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="3">
@@ -478,7 +477,6 @@ const KeyboardAccessoryBar = {
</svg>
</button>
<button class="accessory-btn" data-action="pick-path" title="Insert a file or folder path">&#x1F4C1; Path</button>
<button class="accessory-btn accessory-btn-readmymind" data-action="readmymind" title="Read My Mind: predict your next prompt" hidden>&#x1F9E0;</button>
<button class="accessory-btn" data-action="clear-input" title="Clear the current unsent input">&#x232B; All</button>
<button class="accessory-btn" data-action="tab" title="Tab">Tab</button>
<button class="accessory-btn" data-action="shift-tab" title="Shift+Tab">⇧Tab</button>
@@ -530,8 +528,6 @@ const KeyboardAccessoryBar = {
if (toolbar && toolbar.parentNode) {
toolbar.parentNode.insertBefore(this.element, toolbar);
}
this.refreshReadMyMind();
},
/** Switch between 'simple' and 'extended' button layouts */
@@ -540,27 +536,6 @@ const KeyboardAccessoryBar = {
this._mode = mode;
this.clearConfirm();
this.element.innerHTML = mode === 'extended' ? this._extendedButtons : this._simpleButtons;
// innerHTML rebuilds ship the 🧠 key with its default `hidden` attribute;
// re-derive its visibility for the fresh element.
this.refreshReadMyMind();
},
/**
* Show the 🧠 key only when Read My Mind is opted in (`readMyMindEnabled`,
* synced) AND the active session is claude mode (the predictor is a 400 on
* every other CLI). Called after every innerHTML rebuild, from
* applyHeaderVisibilitySettings (settings load/save), and from the tab
* renderer tail (session switches funnel through it).
*/
refreshReadMyMind() {
if (!this.element) return;
const btn = this.element.querySelector('[data-action="readmymind"]');
if (!btn) return;
const hasApp = typeof app !== 'undefined' && app;
const session = hasApp && app.activeSessionId ? app.sessions?.get(app.activeSessionId) : null;
const enabled = hasApp && app.readMyMindEnabled?.() === true;
const claudeMode = !!session && (!session.mode || session.mode === 'claude');
btn.hidden = !(enabled && claudeMode);
},
_confirmTimer: null,
@@ -638,11 +613,6 @@ const KeyboardAccessoryBar = {
case 'pick-path':
this.pickPath();
break;
case 'readmymind':
// Opens the shared prediction modal (readmymind-ui.js). Deliberately
// not in refocusActions: the modal takes over and the keyboard closes.
app.openReadMyMind?.();
break;
case 'clear-input':
app.clearTerminalInput?.();
break;
+2 -39
View File
@@ -642,16 +642,12 @@ Object.assign(CodemanApp.prototype, {
// Approvals Inbox: a pending dialog for this session gets an answer strip
// BELOW the row (the row itself is a <button>, so actions cannot nest
// inside it). Tapping the row still opens the session, unchanged.
// Waiting rows get the Read My Mind shortcut strip the same way.
const approval = this._pendingApprovalForSession(row.id);
const rmmShortcut = !approval && this._readMyMindRowShortcut(row);
if (approval || rmmShortcut) {
if (approval) {
const wrap = document.createElement('div');
wrap.className = 'mobile-overview-row-wrap';
wrap.appendChild(item);
wrap.appendChild(
approval ? this._buildMobileOverviewApprovalStrip(approval) : this._buildMobileOverviewRmmStrip(row.id)
);
wrap.appendChild(this._buildMobileOverviewApprovalStrip(approval));
return wrap;
}
@@ -695,39 +691,6 @@ Object.assign(CodemanApp.prototype, {
return strip;
},
/**
* Whether a NEEDS YOU row gets the Read My Mind shortcut: opt-in setting,
* live claude-mode session, and only the `waiting` state. A yellow row is a
* bare composer waiting for a prompt; on red rows a dialog is on screen and
* sent text would land in its menu, so those keep the approval strip (or the
* open-the-session path) until the predictor can answer dialogs through the
* approvals endpoint (phase 3 PR 2, docs/readmymind-plan.md).
*/
_readMyMindRowShortcut(row) {
if (row.state !== 'waiting') return false;
if (!this.readMyMindEnabled || !this.readMyMindEnabled()) return false;
const session = this.sessions.get(row.id);
return !!session && (!session.mode || session.mode === 'claude');
},
/** A one-button strip opening the prediction modal for this row's session. */
_buildMobileOverviewRmmStrip(sessionId) {
const strip = document.createElement('div');
strip.className = 'mobile-overview-approval-strip';
strip.setAttribute('data-i18n-skip', '');
const btn = document.createElement('button');
btn.type = 'button';
btn.className = 'mobile-overview-approval-btn rmm';
btn.textContent = '🧠 Suggest';
btn.setAttribute('aria-label', 'Predict my next prompt');
btn.addEventListener('click', (ev) => {
ev.stopPropagation();
this.openReadMyMind(sessionId);
});
strip.appendChild(btn);
return strip;
},
/** A past conversation. Tapping it resumes, which creates a fresh session. */
_buildMobileOverviewPastRow(row) {
const item = document.createElement('button');
+4 -23
View File
@@ -530,9 +530,8 @@ html.mobile-init .file-browser-panel {
display: none !important;
}
/* Read My Mind 🧠 button: desktop header only. The phone surfaces are the
keyboard-accessory 🧠 key and the overview's waiting-row Suggest strip
(docs/readmymind-plan.md phase 3), both opening the same modal. */
/* Read My Mind 🧠 button: desktop header only in phase 2; the phone surface
is a planned keyboard-accessory key (docs/readmymind-plan.md phase 3). */
.btn-icon-header.btn-readmymind {
display: none !important;
}
@@ -1291,11 +1290,8 @@ html.mobile-init .file-browser-panel {
flex-direction: column;
}
/* Small modals (confirmations, dialogs) — auto-height, not full-screen.
Read My Mind is a decision dialog, not a workspace, so it gets the same
compact-sheet treatment instead of the full-screen default. */
.modal-content.modal-sm,
.modal-content.readmymind-modal {
/* Small modals (confirmations, dialogs) — auto-height, not full-screen */
.modal-content.modal-sm {
height: auto;
max-height: 85vh;
border-radius: 12px;
@@ -1303,16 +1299,6 @@ html.mobile-init .file-browser-panel {
width: calc(100% - 2rem);
}
/* Read My Mind phone polish: iOS zooms the page when focusing sub-16px
inputs, and alternate rows are tap targets. */
.readmymind-prompt-input,
.readmymind-steer-input {
font-size: 16px;
}
.readmymind-alt {
min-height: 44px;
}
/* Modal safe area padding - all sides for full-screen modals */
.ios-device .modal-content {
padding-top: var(--safe-area-top);
@@ -2614,11 +2600,6 @@ html.mobile-init .file-browser-panel {
border-color: var(--error);
color: var(--error);
}
/* Read My Mind shortcut on waiting rows (mobile-overview.js). */
.mobile-overview-approval-btn.rmm {
border-color: var(--accent);
color: var(--accent);
}
/* Attention states mirror the session tabs exactly: red blink when the agent
asked something (permission / question), yellow blink when it is waiting for
+19 -113
View File
@@ -5,16 +5,9 @@
* setting is ON) opens a modal that asks the server for the user's most likely
* next prompt (`POST /api/sessions/:id/readmymind`, one-shot predictor over the
* case's intent profile + live session signals). The top suggestion lands in an
* editable single-line field with its rationale below; the predictor returns up
* to 3 kind-diverse suggestions (continue / verify / redirect) and the rest
* render as tappable alternate rows that swap into the field. Buttons are Send
* (with Enter), Insert (drop on the CLI composer WITHOUT Enter, for editing),
* Rethink (re-run with every displayed suggestion recorded as rejected, plus an
* optional free-text steer note from the field above the buttons), Dismiss.
*
* Phone surfaces reuse this same modal: the keyboard-accessory 🧠 key
* (keyboard-accessory.js) and the phone overview's waiting-row shortcut
* (mobile-overview.js) both call openReadMyMind(sessionId).
* editable single-line field with its rationale below; buttons are Send (with
* Enter), Insert (drop on the CLI composer WITHOUT Enter, for editing), Rethink
* (re-run with the shown suggestion recorded as rejected), Dismiss.
*
* Suggestions are NEVER auto-sent: the explicit click here is the security
* boundary for observed/injectable predictor inputs, so suggestion text is
@@ -38,14 +31,10 @@ Object.assign(CodemanApp.prototype, {
return this.loadAppSettingsFromStorage().readMyMindEnabled === true;
},
/**
* Open the modal and start a prediction. Defaults to the active session;
* phone surfaces (accessory key, overview rows) pass an explicit id so a
* prediction can start without switching tabs first.
*/
openReadMyMind(sessionId) {
const id = sessionId || this.activeSessionId;
const session = id ? this.sessions.get(id) : null;
/** Open the modal for the active session and start a prediction. */
openReadMyMind() {
const sessionId = this.activeSessionId;
const session = sessionId ? this.sessions.get(sessionId) : null;
if (!session) {
this.showToast('Select a session first', 'warning');
return;
@@ -54,15 +43,8 @@ Object.assign(CodemanApp.prototype, {
this.showToast('Read My Mind works on Claude sessions only', 'warning');
return;
}
// Rethink memory resets on each open (a fresh open is a fresh question),
// and so does the steer note (it belongs to the question it steered).
this._rmm = { sessionId: id, shown: null, alternates: [], rejected: [], busy: false };
const steer = document.getElementById('readMyMindSteer');
if (steer) steer.value = '';
// Name the target in the header: overview rows and the accessory key can
// open this for a session that is not the active tab.
const sessionLabel = document.getElementById('readMyMindSession');
if (sessionLabel) sessionLabel.textContent = this.getSessionName?.(session) || session.name || '';
// Rethink memory resets on each open (a fresh open is a fresh question).
this._rmm = { sessionId, shown: null, rejected: [], busy: false };
document.getElementById('readMyMindModal')?.classList.add('active');
this._readMyMindPredict();
},
@@ -72,25 +54,14 @@ Object.assign(CodemanApp.prototype, {
this._rmm = null;
},
/** Run (or re-run) the prediction and render the suggestions. */
/** Run (or re-run) the prediction and render the top suggestion. */
async _readMyMindPredict() {
const state = this._rmm;
if (!state || state.busy) return;
state.busy = true;
this._rmmSetPhase('loading');
const body = {};
if (state.rejected.length > 0) body.rejected = state.rejected.slice(-10);
// The steer note is the user's own words; server-side it rides in the
// highest-authority context tier. Bounds mirror ReadMyMindPredictSchema.
const steerEl = document.getElementById('readMyMindSteer');
const steer = steerEl
? steerEl.value
.replace(/[\r\n]+/g, ' ')
.trim()
.slice(0, 2000)
: '';
if (steer) body.steer = steer;
const body = state.rejected.length > 0 ? { rejected: state.rejected.slice(-10) } : {};
const data = await this._apiJson(`/api/sessions/${state.sessionId}/readmymind`, { method: 'POST', body });
// The modal may have been dismissed (or reopened for another session) while
@@ -98,21 +69,14 @@ Object.assign(CodemanApp.prototype, {
if (this._rmm !== state) return;
state.busy = false;
const suggestions = (data && data.suggestions) || [];
if (suggestions.length === 0) {
const suggestion = data && data.suggestions && data.suggestions[0];
if (!suggestion) {
this._rmmSetPhase('error');
return;
}
state.shown = suggestions[0];
state.alternates = suggestions.slice(1);
state.shown = suggestion;
this._rmmSetPhase('ready');
this._rmmShowSuggestion(state.shown);
this._rmmRenderAlternates();
document.getElementById('readMyMindPrompt')?.focus();
},
/** Paint one suggestion into the editable field, kind chip, and rationale. */
_rmmShowSuggestion(suggestion) {
const input = document.getElementById('readMyMindPrompt');
const why = document.getElementById('readMyMindWhy');
const kind = document.getElementById('readMyMindKind');
@@ -124,49 +88,7 @@ Object.assign(CodemanApp.prototype, {
kind.textContent = suggestion.kind || 'continue';
kind.className = `readmymind-kind readmymind-kind-${suggestion.kind || 'continue'}`;
}
},
/** The non-primary suggestions as tappable rows below the rationale. */
_rmmRenderAlternates() {
const state = this._rmm;
const box = document.getElementById('readMyMindAlternates');
if (!box) return;
box.textContent = '';
if (!state) return;
state.alternates.forEach((alt, i) => {
const row = document.createElement('button');
row.type = 'button';
row.className = 'readmymind-alt';
const kind = document.createElement('span');
kind.className = `readmymind-kind readmymind-kind-${alt.kind || 'continue'}`;
kind.textContent = alt.kind || 'continue';
const col = document.createElement('span');
col.className = 'readmymind-alt-col';
const text = document.createElement('span');
text.className = 'readmymind-alt-text';
text.textContent = alt.prompt;
col.appendChild(text);
// The rationale as a visible second line: phones have no hover, and the
// why is what separates two plausible-looking prompts.
if (alt.why) {
const why = document.createElement('span');
why.className = 'readmymind-alt-why';
why.textContent = alt.why;
col.appendChild(why);
}
row.appendChild(kind);
row.appendChild(col);
row.addEventListener('click', () => {
// Swap: the tapped alternate becomes the shown suggestion and the
// previously shown one takes its row, so browsing loses nothing.
const prev = state.shown;
state.shown = alt;
state.alternates[i] = prev;
this._rmmShowSuggestion(alt);
this._rmmRenderAlternates();
});
box.appendChild(row);
});
input?.focus();
},
/**
@@ -192,19 +114,11 @@ Object.assign(CodemanApp.prototype, {
this.showToast(withEnter ? 'Prompt sent' : 'Inserted, press Enter in the terminal to send', 'success');
},
/**
* Re-run with every displayed suggestion recorded as a rejection (the user
* saw them all and wanted none: strong negative signal) plus the optional
* steer note, read by _readMyMindPredict from its field.
*/
/** Re-run with the shown suggestion recorded as a rejection. */
rethinkReadMyMind() {
const state = this._rmm;
if (!state || state.busy) return;
for (const s of [state.shown, ...state.alternates]) {
if (!s || !s.prompt) continue;
const prompt = s.prompt.slice(0, 1000);
if (!state.rejected.includes(prompt)) state.rejected.push(prompt);
}
if (state.shown && state.shown.prompt) state.rejected.push(state.shown.prompt);
this._readMyMindPredict();
},
@@ -215,15 +129,7 @@ Object.assign(CodemanApp.prototype, {
modal.querySelector('.readmymind-loading').style.display = phase === 'loading' ? '' : 'none';
modal.querySelector('.readmymind-result').style.display = phase === 'ready' ? '' : 'none';
modal.querySelector('.readmymind-error').style.display = phase === 'error' ? '' : 'none';
// The steer field shows in ready AND error phases: steering a failed run's
// retry is exactly when a note helps. Hidden only while loading.
const steer = document.getElementById('readMyMindSteer');
if (steer) steer.style.display = phase === 'loading' ? 'none' : '';
// While the predictor runs the hidden field still holds the previous text;
// freeze every action so a stale suggestion cannot be sent mid-rethink.
for (const btnId of ['readMyMindRethink', 'readMyMindInsert', 'readMyMindSend']) {
const btn = document.getElementById(btnId);
if (btn) btn.disabled = phase === 'loading';
}
const rethinkBtn = document.getElementById('readMyMindRethink');
if (rethinkBtn) rethinkBtn.disabled = phase === 'loading';
},
});
-3
View File
@@ -2117,9 +2117,6 @@ Object.assign(CodemanApp.prototype, {
if (readMyMindBtn) {
readMyMindBtn.classList.toggle('btn-readmymind--hidden', settings.readMyMindEnabled !== true);
}
// The phone accessory bar's 🧠 key follows the same setting (plus the
// active session's mode); keep it in step on every settings apply.
if (typeof KeyboardAccessoryBar !== 'undefined') KeyboardAccessoryBar.refreshReadMyMind?.();
// Plan-usage chip — shown by default on desktop, OFF on handhelds (App
// Settings → Display → "Plan Usage Limits"). The template always ships it
+84 -89
View File
@@ -3666,6 +3666,13 @@ body.touch-device .terminal-container .xterm .xterm-helper-textarea {
color: #95e6b3;
}
/* Past session (issue #261): activating the card resumes the conversation
rather than switching to a tab, so the badge says so. */
.search-badge-past {
background: rgba(245, 158, 11, 0.18);
color: #f0c073;
}
.search-result-name {
flex: 1;
min-width: 0;
@@ -3715,24 +3722,99 @@ body.touch-device .terminal-container .xterm .xterm-helper-textarea {
.search-select {
max-width: 7rem;
}
/* Tablet/narrow: let the controls drop under the title instead of squeezing it. */
.history-header {
flex-wrap: wrap;
}
.history-controls {
width: 100%;
margin-left: 0;
}
.history-filter {
flex: 1;
width: auto;
}
}
/* Title row for the past-session list: label + count on the left, filter and
sort on the right (issue #260, 35 conversations in a 4-row box). */
.history-header {
display: flex;
align-items: center;
gap: 0.5rem;
margin-bottom: 0.5rem;
}
.history-title {
font-size: 0.85rem;
color: var(--text-dim);
margin-bottom: 0.5rem;
font-weight: 500;
text-align: left;
}
.history-count {
font-size: 0.68rem;
color: var(--text-dim);
background: rgba(255, 255, 255, 0.05);
border-radius: 999px;
padding: 0.1rem 0.45rem;
white-space: nowrap;
}
.history-controls {
display: flex;
align-items: center;
gap: 0.35rem;
margin-left: auto;
}
.history-filter {
width: 8.5rem;
box-sizing: border-box;
padding: 0.28rem 0.5rem;
font-size: 0.68rem;
color: var(--text);
background: rgba(255, 255, 255, 0.03);
border: 1px solid rgba(255, 255, 255, 0.1);
border-radius: 6px;
outline: none;
transition: border-color var(--transition-smooth), background var(--transition-smooth);
}
.history-filter:focus {
border-color: rgba(59, 130, 246, 0.5);
background: rgba(255, 255, 255, 0.06);
}
.history-filter::placeholder {
color: var(--text-dim);
}
.history-sort {
max-width: 7.5rem;
}
.history-empty {
padding: 0.75rem 0.5rem;
font-size: 0.75rem;
color: var(--text-dim);
text-align: center;
}
/* Collapsed height fits the initial page of rows; expanding the LIST has to
expand the BOX too, or "Show more" just deepens a scroll well. */
.history-list {
display: flex;
flex-direction: column;
gap: 0.35rem;
max-height: 240px;
max-height: min(42vh, 360px);
overflow-y: auto;
}
.history-list.expanded {
max-height: min(64vh, 660px);
}
.history-item {
display: flex;
flex-direction: column;
@@ -10697,17 +10779,6 @@ kbd {
font-size: 13px;
line-height: 1;
}
/* Which session the prediction targets: overview rows and the accessory key
can open the modal for a session that is not the active tab. */
.readmymind-session {
margin-left: 8px;
font-size: 12px;
font-weight: 400;
color: var(--text-dim);
}
.readmymind-session:empty {
display: none;
}
/* Read My Mind modal: top suggestion in an editable single-line field with the
kind badge beside it and the rationale below. */
@@ -10764,75 +10835,6 @@ kbd {
font-size: 12px;
color: var(--text-dim);
}
/* Alternates: the predictor's other kind-diverse suggestions (verify /
redirect) as tappable rows; tapping swaps one into the editable field.
Text is set via textContent only (predictor output is observed/injectable
content, same rule as the field above). */
.readmymind-alternates {
margin-top: 10px;
display: flex;
flex-direction: column;
gap: 6px;
}
.readmymind-alternates:empty {
display: none;
}
.readmymind-alt {
display: flex;
align-items: center;
gap: 8px;
width: 100%;
text-align: left;
padding: 7px 9px;
background: var(--bg-dark);
border: 1px solid var(--control-border);
border-radius: 8px;
color: var(--text);
cursor: pointer;
font-family: inherit;
}
.readmymind-alt:hover {
border-color: var(--accent);
}
.readmymind-alt-col {
flex: 1 1 auto;
min-width: 0;
display: flex;
flex-direction: column;
gap: 2px;
}
.readmymind-alt-text {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
font-size: 12px;
}
/* Rationale as a visible second line: phones have no hover for a title tip,
and the why is what separates two plausible-looking prompts. */
.readmymind-alt-why {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
font-size: 11px;
color: var(--text-dim);
}
/* Steer note: optional free-text guidance for Rethink ("no, I meant the
mobile bug"); sent as the highest-authority tier of the re-run's context. */
.readmymind-steer-input {
margin-top: 10px;
width: 100%;
font-size: 12px;
padding: 7px 9px;
background: transparent;
color: var(--text);
border: 1px dashed var(--control-border);
border-radius: 8px;
}
.readmymind-steer-input:focus {
outline: none;
border-color: var(--accent);
border-style: solid;
}
.readmymind-error {
padding: 12px 4px;
color: var(--text-dim);
@@ -12155,13 +12157,6 @@ body.touch-device.cjk-input-visible .main {
transition: background 0.15s, border-color 0.15s;
}
/* The 🧠 key ships `hidden` until refreshReadMyMind() reveals it; the
inline-flex rules above (and mobile.css's) beat the UA [hidden] rule, so
hiding must be re-asserted, same trap as the home-sessions column. */
.accessory-btn[hidden] {
display: none !important;
}
.accessory-btn.confirming {
background: color-mix(in srgb, var(--yellow) 18%, var(--bg-input));
border-color: var(--yellow);
+196 -31
View File
@@ -1628,7 +1628,7 @@ Object.assign(CodemanApp.prototype, {
pin.title = 'Pinned';
titleSpan.appendChild(pin);
}
titleSpan.appendChild(document.createTextNode(s.name || s.firstPrompt || shortDir));
titleSpan.appendChild(document.createTextNode(this._historyRowLabel(s, shortDir)));
// Badge row: mode (claude/codex/opencode/gemini/antigravity/shell) + a LIVE pill.
const badgeRow = document.createElement('div');
@@ -1954,7 +1954,18 @@ Object.assign(CodemanApp.prototype, {
},
/** Number of history items shown before "Show More" */
_HISTORY_INITIAL_COUNT: 4,
_HISTORY_INITIAL_COUNT: 10,
/**
* How many past sessions the home screen loads (also the filter/sort corpus).
* 200, not the old 60, so the filter can reach a real backlog, an install with
* 35+ conversations would otherwise hit the ceiling before the filter is useful
* (raised in @jordan8037310's #263; the endpoint clamps at 500).
*/
_HISTORY_FETCH_LIMIT: 200,
/** localStorage key for the per-device sort choice (#263). */
_HISTORY_SORT_KEY: 'codeman:historySort',
async loadHistorySessions() {
const container = document.getElementById('historySessions');
@@ -1968,7 +1979,7 @@ Object.assign(CodemanApp.prototype, {
? Promise.resolve(this.cases)
: fetch('/api/cases').then((r) => (r.ok ? r.json() : null)).then((d) => d?.data || []).catch(() => []);
const [allSessions, cases] = await Promise.all([
this._fetchUnifiedSessions(60),
this._fetchUnifiedSessions(this._HISTORY_FETCH_LIMIT),
casesPromise,
]);
if (allSessions.length === 0) {
@@ -1976,27 +1987,14 @@ Object.assign(CodemanApp.prototype, {
return;
}
list.replaceChildren();
const initialCount = this._HISTORY_INITIAL_COUNT;
// Render initial items
for (let i = 0; i < Math.min(initialCount, allSessions.length); i++) {
list.appendChild(this._buildHistoryItem(allSessions[i], cases));
}
// Add "Show More" button if there are more items
if (allSessions.length > initialCount) {
const moreBtn = document.createElement('button');
moreBtn.className = 'history-show-more';
moreBtn.textContent = `Show ${allSessions.length - initialCount} more`;
moreBtn.addEventListener('click', () => {
for (let i = initialCount; i < allSessions.length; i++) {
list.insertBefore(this._buildHistoryItem(allSessions[i], cases), moreBtn);
}
moreBtn.remove();
});
list.appendChild(moreBtn);
}
// Keep the corpus around: filtering and sorting (issue #260) work on this
// array, so a re-render costs no request. Expansion survives the periodic
// refresh in panels-ui.js, collapsing the list under the user's cursor
// every few seconds would be worse than the original 4-item cap.
this._historyAll = allSessions;
this._historyCases = cases;
this._wireHistoryControls();
this._renderHistoryList();
container.style.display = '';
} catch (err) {
@@ -2005,6 +2003,161 @@ Object.assign(CodemanApp.prototype, {
}
},
/**
* Wire the filter box and sort select once; both re-render from the cached
* corpus. The sort choice is restored from (and saved to) localStorage, it is
* a per-device display preference, so it stays out of the synced settings
* schema, same as `codeman:skin`.
*/
_wireHistoryControls() {
if (this._historyControlsWired) return;
const filter = document.getElementById('historyFilter');
const sort = document.getElementById('historySort');
if (!filter && !sort) return;
this._historyControlsWired = true;
if (sort) {
try {
const saved = localStorage.getItem(this._HISTORY_SORT_KEY);
if (saved && Array.from(sort.options).some((o) => o.value === saved)) sort.value = saved;
} catch {
/* private mode, the order just won't persist */
}
}
if (filter) {
filter.addEventListener('input', () => this._renderHistoryList());
filter.addEventListener('keydown', (ev) => {
if (ev.key === 'Escape' && filter.value) {
// Swallow it: Escape at the welcome screen otherwise closes overlays.
ev.stopPropagation();
filter.value = '';
this._renderHistoryList();
}
});
}
if (sort) {
sort.addEventListener('change', () => {
try {
localStorage.setItem(this._HISTORY_SORT_KEY, sort.value);
} catch {
/* private mode, the order just won't persist */
}
this._renderHistoryList();
});
}
},
/** True when a past-session row matches the filter text (name, folder, case, prompt). */
_historyRowMatches(s, needle, cases) {
const fields = [
s.name,
s.workingDir,
this._resolveCaseLabel(s.workingDir, cases),
s.firstPrompt,
s.lastPrompt,
s.sessionId,
];
return fields.some((f) => typeof f === 'string' && f.toLowerCase().includes(needle));
},
/**
* The text a history row shows as its title. Most transcript-backed rows have
* no session name at all, so this falls through to the first prompt and then
* to the path, and the A–Z sort keys off the SAME string, or "sort by name"
* would silently do nothing for exactly the rows the list is mostly made of.
*/
_historyRowLabel(s, fallback) {
return s.name || s.firstPrompt || fallback || '';
},
/**
* Sort past-session rows. 'recent' keeps the backend order (newest first);
* the alphabetical modes sort by the visible title or by folder basename.
* Pinned rows stay on top in every mode, pinning is an explicit override and
* a sort that buried it would read as the pin having been lost.
*/
_sortHistoryRows(rows, mode) {
const label = (s) => this._historyRowLabel(s, this._shortenHomePath(s.workingDir)).toLowerCase();
const folder = (s) => ((s.workingDir || '').split('/').pop() || '').toLowerCase();
const key = mode === 'name' ? label : folder;
// numeric collation so w2-… sorts before w10-…, and base sensitivity so case
// does not split a project's rows apart (from @jordan8037310's #263).
const sorted =
mode === 'recent'
? rows.slice()
: rows
.slice()
.sort((a, b) => key(a).localeCompare(key(b), undefined, { sensitivity: 'base', numeric: true }));
const pinned = sorted.filter((s) => s.pinned);
return pinned.length === 0 ? sorted : pinned.concat(sorted.filter((s) => !s.pinned));
},
/**
* Render the "Resume Conversation" list from the cached corpus, applying the
* current filter and sort. Collapsed by default to _HISTORY_INITIAL_COUNT;
* "Show more" expands the list AND the box (the CSS cap is class-driven, since
* a fixed 240px box made expansion pointless, issue #260).
*/
_renderHistoryList() {
const list = document.getElementById('historyList');
if (!list) return;
const all = this._historyAll || [];
const cases = this._historyCases || [];
const countEl = document.getElementById('historyCount');
const needle = (document.getElementById('historyFilter')?.value || '').trim().toLowerCase();
const mode = document.getElementById('historySort')?.value || 'recent';
const matched = needle ? all.filter((s) => this._historyRowMatches(s, needle, cases)) : all;
const rows = this._sortHistoryRows(matched, mode);
// Filtering is itself an expansion request: hiding matches behind "Show more"
// would defeat the point of typing a filter.
const expanded = !!this._historyExpanded || needle.length > 0;
const visible = expanded ? rows : rows.slice(0, this._HISTORY_INITIAL_COUNT);
list.replaceChildren();
list.classList.toggle('expanded', expanded);
if (rows.length === 0) {
const empty = document.createElement('div');
empty.className = 'history-empty';
empty.textContent = `No conversations match "${needle}"`;
list.appendChild(empty);
}
for (const s of visible) list.appendChild(this._buildHistoryItem(s, cases));
const hidden = rows.length - visible.length;
if (hidden > 0) {
const moreBtn = document.createElement('button');
moreBtn.className = 'history-show-more';
moreBtn.textContent = `Show ${hidden} more`;
moreBtn.addEventListener('click', () => {
this._historyExpanded = true;
this._renderHistoryList();
});
list.appendChild(moreBtn);
} else if (expanded && !needle && rows.length > this._HISTORY_INITIAL_COUNT) {
const lessBtn = document.createElement('button');
lessBtn.className = 'history-show-more';
lessBtn.textContent = 'Show less';
lessBtn.addEventListener('click', () => {
this._historyExpanded = false;
this._renderHistoryList();
list.scrollTop = 0;
});
list.appendChild(lessBtn);
}
if (countEl) {
countEl.textContent = needle
? `${rows.length} of ${all.length}`
: rows.length > visible.length
? `${visible.length} of ${rows.length}`
: String(rows.length);
}
},
/** Page size for the folder history modal */
_FOLDER_HISTORY_PAGE_SIZE: 20,
@@ -3832,13 +3985,16 @@ Object.assign(CodemanApp.prototype, {
/** Render the grouped result cards (or empty/loading states). */
_renderSearch(data) {
const results = document.getElementById('searchResults');
const historyTitle = document.getElementById('historyTitle');
// The header carries the title plus the filter/sort controls (issue #260),
// hide the whole row, not just the title, or the controls float above the
// search results and act on a list that is not on screen.
const historyHeader = document.getElementById('historyHeader') || document.getElementById('historyTitle');
const historyList = document.getElementById('historyList');
if (!results) return;
const searching = !!data;
// Hide the plain "Resume Conversation" history list while a search is active.
if (historyTitle) historyTitle.style.display = searching ? 'none' : '';
if (historyHeader) historyHeader.style.display = searching ? 'none' : '';
if (historyList) historyList.style.display = searching ? 'none' : '';
results.innerHTML = '';
@@ -3907,9 +4063,11 @@ Object.assign(CodemanApp.prototype, {
const topRow = document.createElement('div');
topRow.className = 'search-result-top';
// A past session resumes rather than switches tabs, so it says so on the badge.
const isPast = r.jumpTo && r.jumpTo.kind === 'resume-session';
const badge = document.createElement('span');
badge.className = 'search-result-badge search-badge-' + r.type;
badge.textContent = (window.CodemanSearch.SOURCE_LABELS[r.type] || r.type).replace(/s$/, '');
badge.className = 'search-result-badge search-badge-' + r.type + (isPast ? ' search-badge-past' : '');
badge.textContent = isPast ? 'Resume' : (window.CodemanSearch.SOURCE_LABELS[r.type] || r.type).replace(/s$/, '');
const name = document.createElement('span');
name.className = 'search-result-name';
@@ -3941,13 +4099,20 @@ Object.assign(CodemanApp.prototype, {
/**
* Navigate to a search result by jumpTo.kind, reusing the existing app methods:
* session → selectSession(sessionId) (open/switch to the session)
* run-summary → openRunSummary(sessionId) (session options → summary tab)
* file-preview→ openFilePreview(path, sessionId, attachmentId)
* session → selectSession(sessionId) (open/switch to the session)
* resume-session→ resumeHistorySession(...) (past session, no tab to switch to)
* run-summary → openRunSummary(sessionId) (session options → summary tab)
* file-preview → openFilePreview(path, sessionId, attachmentId)
*/
_jumpToSearchResult(r) {
const jt = r && r.jumpTo;
if (!jt) return;
// A past session has to be replayed, not switched to. Do it BEFORE hiding the
// welcome overlay: resumeHistorySession() owns that transition itself.
if (jt.kind === 'resume-session') {
this.resumeHistorySession(jt.claudeSessionId || jt.sessionId, jt.workingDir || '', r.sessionName);
return;
}
// Leaving the welcome overlay so the target surface is visible.
if (typeof this.hideWelcome === 'function') this.hideWelcome();
+34 -1
View File
@@ -3,7 +3,9 @@
*
* Registers `GET /api/search?q=&types=&limit=` — a bounded, in-memory search
* across three v1 sources, returned in the standard ApiResponse envelope:
* 1. sessions/cases — name, working directory, session id
* 1. sessions/cases, name, working directory, session id, for LIVE sessions
* plus the past-session snapshot in `session-history-index.ts` (issue #261:
* the live map alone made every closed session unfindable by folder name)
* 2. run-summary events — event title/details (from the live run-summary trackers)
* 3. file paths — per-session attachment history (workspace-relative paths only)
*
@@ -34,6 +36,7 @@ import {
} from '../../search-service.js';
import type { SearchSourceType } from '../../types/search.js';
import type { SessionPort, InfraPort } from '../ports/index.js';
import { ensureHistorySessionIndexFresh, getHistorySessionIndex } from '../session-history-index.js';
/**
* Per-source harvest caps. These bound how much in-memory data we hand to the
@@ -61,11 +64,17 @@ interface SessionLike {
/**
* Harvest the three source arrays from the live in-memory stores. Reads only
* bounded, already-loaded data — no disk I/O, no terminal buffers.
*
* Past sessions come from the `session-history-index` snapshot, which is built
* outside the request path for exactly that reason. Live rows are harvested
* first and win the dedupe, so a session that is both live and in the snapshot
* keeps its live jump-to (switch to the tab) instead of a resume.
*/
function harvestSources(ctx: SessionPort & InfraPort, canSee?: (owner?: string) => boolean): SearchSources {
const sessions: SessionSearchInput[] = [];
const events: EventSearchInput[] = [];
const files: FileSearchInput[] = [];
const seenSessionIds = new Set<string>();
for (const raw of ctx.sessions.values()) {
const s = raw as unknown as SessionLike & { owner?: string };
@@ -73,6 +82,7 @@ function harvestSources(ctx: SessionPort & InfraPort, canSee?: (owner?: string)
const sessionName = s.name ?? '';
const timestamp = s.lastActivityAt ?? s.createdAt ?? 0;
seenSessionIds.add(s.id);
sessions.push({
sessionId: s.id,
sessionName,
@@ -95,6 +105,24 @@ function harvestSources(ctx: SessionPort & InfraPort, canSee?: (owner?: string)
}
}
// Past sessions: the out-of-band snapshot of the unified list. Unscoped on
// disk, so every row goes through the same ownership check as a live one,
// host-wide transcript rows carry no owner and are therefore admin-only in
// multi-user mode, matching GET /api/sessions/unified.
for (const item of getHistorySessionIndex().items) {
if (seenSessionIds.has(item.sessionId)) continue;
if (canSee && !canSee(item.owner)) continue;
seenSessionIds.add(item.sessionId);
sessions.push({
sessionId: item.sessionId,
sessionName: item.name,
workingDir: item.workingDir,
timestamp: item.timestamp,
history: true,
claudeSessionId: item.claudeSessionId,
});
}
// Events: from the live run-summary trackers, keyed by session id.
for (const [sessionId, tracker] of ctx.runSummaryTrackers) {
const session = ctx.sessions.get(sessionId) as unknown as (SessionLike & { owner?: string }) | undefined;
@@ -134,6 +162,11 @@ export function registerSearchRoutes(app: FastifyInstance, ctx: SessionPort & In
)
: null;
// Fire-and-forget: a stale past-session snapshot is rebuilt in the
// background. This query still answers from whatever is already in memory,
// which is what keeps the request path free of disk I/O.
ensureHistorySessionIndexFresh();
const sources = harvestSources(ctx, canSee);
// Apply the optional source-type filter before searching so excluded
+68 -10
View File
@@ -94,7 +94,13 @@ import {
type LifecycleInput,
type HistoryInput,
type MuxStatInput,
type UnifiedSessionItem,
} from '../../services/unified-session-service.js';
import {
buildHistorySessionIndexItems,
setHistoryIndexRefresher,
setHistorySessionIndex,
} from '../session-history-index.js';
import type { SessionPort, EventPort, ConfigPort, InfraPort, AuthPort } from '../ports/index.js';
import { RunSummaryTracker } from '../../run-summary.js';
@@ -3539,16 +3545,20 @@ export function registerSessionRoutes(
return { sessions: results.slice(0, 50) };
});
// Unified, read-only session list: merges live + persisted + lifecycle +
// transcript history + mux stats into one de-duplicated, searchable list
// (COD-121). Pure merge/filter logic lives in unified-session-service.ts.
app.get('/api/sessions/unified', async (req) => {
const query = req.query as { q?: string; offset?: string; limit?: string };
if (ctx.testMode) {
return { sessions: [], total: 0 };
}
/**
* Gather the four read-only views the unified list is merged from, plus mux
* stats. This is the expensive half (the lifecycle log and a scan of every
* Claude transcript), factored out of the route handler because the
* past-session search index rebuilds itself from the very same inputs, off
* the request path, see session-history-index.ts.
*/
async function gatherUnifiedInputs(): Promise<{
live: LiveSessionInput[];
persisted: PersistedSessionInput[];
lifecycle: LifecycleInput[];
history: HistoryInput[];
mux: MuxStatInput[];
}> {
// Live (in-memory) sessions.
const live: LiveSessionInput[] = [...ctx.sessions.values()].map((s) => {
const st = s.toState();
@@ -3649,14 +3659,54 @@ export function registerSessionRoutes(
// Mux stats are optional.
}
return { live, persisted, lifecycle, history, mux };
}
/**
* Publish a merged unified list as the past-session search index (issue #261).
* The snapshot is stored UNSCOPED with a per-row owner, so it must only ever be
* built from an unscoped merge, `harvestSources()` in search-routes re-applies
* the ownership check on read.
*/
function publishHistorySessionIndex(merged: UnifiedSessionItem[]): void {
const ownerById = new Map<string, string | undefined>();
const stored = ctx.store.getState().sessions as Record<string, { id: string; owner?: string }>;
for (const p of Object.values(stored)) ownerById.set(p.id, p.owner);
// Live wins: a session's owner on disk can lag the running one.
for (const s of ctx.sessions.values()) ownerById.set(s.id, s.owner);
const liveIds = new Set(ctx.sessions.keys());
setHistorySessionIndex(buildHistorySessionIndexItems(merged, ownerById, liveIds));
}
// Rebuild hook for the search route: it kicks this (fire-and-forget) when the
// snapshot goes stale, so a search never pays for the scan itself.
setHistoryIndexRefresher(async () => {
if (ctx.testMode) return;
publishHistorySessionIndex(mergeUnifiedSessions(await gatherUnifiedInputs()));
});
// Unified, read-only session list: merges live + persisted + lifecycle +
// transcript history + mux stats into one de-duplicated, searchable list
// (COD-121). Pure merge/filter logic lives in unified-session-service.ts.
app.get('/api/sessions/unified', async (req) => {
const query = req.query as { q?: string; offset?: string; limit?: string };
if (ctx.testMode) {
return { sessions: [], total: 0 };
}
const { live, persisted, lifecycle, history, mux } = await gatherUnifiedInputs();
// Multi-user: a non-admin only sees their own sessions; host-wide transcript
// history (not tied to an owned session) is admin-only.
let sLive = live;
let sPersisted = persisted;
let sLifecycle = lifecycle;
let sHistory = history;
let scoped = false;
const uUser = getAuthUser(req);
if (isMultiUserMode() && uUser.role !== 'admin') {
scoped = true;
const ownedLive = new Set(
[...ctx.sessions.values()].filter((s) => canAccessOwned(uUser, s.owner)).map((s) => s.id)
);
@@ -3680,6 +3730,14 @@ export function registerSessionRoutes(
history: sHistory,
mux,
});
// Refresh the search index off the back of this request, the home screen
// fetches this endpoint whenever it opens, which is the same screen the
// search box lives on, so the snapshot is warm before anyone types. A scoped
// merge is a per-user subset and would corrupt the shared snapshot, so that
// path re-merges unscoped instead (multi-user is opt-in and rarely hit).
publishHistorySessionIndex(scoped ? mergeUnifiedSessions({ live, persisted, lifecycle, history, mux }) : merged);
const offset = query.offset !== undefined ? parseInt(query.offset, 10) : undefined;
const limit = query.limit !== undefined ? parseInt(query.limit, 10) : undefined;
return filterAndPaginate(merged, {
+166
View File
@@ -0,0 +1,166 @@
/**
* @fileoverview Bounded in-memory index of PAST sessions, harvested by `GET /api/search`.
*
* `GET /api/search` used to build its session corpus from the live in-memory
* session map alone, so a folder sitting in the home screen's "Resume
* Conversation" list matched nothing (issue #261). The corpus that list renders
* comes from `GET /api/sessions/unified`, which reads the lifecycle log and every
* Claude transcript file: disk I/O the search path deliberately does not do (its
* no-fs property is what keeps a per-keystroke query cheap and traversal-free).
*
* This module is the seam between the two: a capped snapshot of the unified list
* that the search route reads synchronously, refreshed OUT of the request path.
* Two things fill it:
* 1. `/api/sessions/unified` writes it as a side effect (free, it just merged
* that list). The home screen calls that endpoint whenever it opens, which
* is the same screen the search box lives on, so it is warm in practice.
* 2. `ensureHistorySessionIndexFresh()`, fire-and-forget, single-flight,
* TTL-guarded, kicks the registered refresher when a search finds the
* snapshot stale. The caller never awaits it: the current query answers from
* the existing snapshot and the next one sees fresh data.
*
* OWNERSHIP: each item carries the `owner` of the session it came from, and rows
* not tied to any live/persisted session (host-wide transcript history) carry
* `owner: undefined`. `canAccessOwned()` then reproduces the unified route's rule
* exactly, in multi-user mode a non-admin sees neither other users' sessions nor
* unowned host-wide history, and in single-user mode every check short-circuits
* true. The snapshot is written UNSCOPED, so it must never be returned unfiltered.
*
* Key exports:
* - setHistorySessionIndex / getHistorySessionIndex: the snapshot accessors.
* - buildHistorySessionIndexItems: pure merged-list → index-item projection.
* - setHistoryIndexRefresher / ensureHistorySessionIndexFresh: the refresh hook.
*/
/** One past-session row in the snapshot. Mirrors what the search corpus needs, nothing more. */
export interface HistorySessionIndexItem {
/** Codeman session id (the search result's session id and dedupe key). */
sessionId: string;
/** Display name, may be empty for a transcript-only row. */
name: string;
/** Absolute working directory, the field issue #261 is about matching. */
workingDir: string;
/** Claude conversation UUID, when known: what a resume actually replays. */
claudeSessionId?: string;
/** Recency timestamp (lastActivityAt, else createdAt). */
timestamp: number;
/**
* Owning user, when the row is tied to a live or persisted session. `undefined`
* means host-wide transcript history, which only admins (or single-user mode)
* may see, the same rule `/api/sessions/unified` applies.
*/
owner?: string;
/** True when the session is still in the live map (search harvests those directly). */
live: boolean;
}
/** Hard cap on snapshot size, so a host with thousands of transcripts stays bounded. */
export const HISTORY_INDEX_MAX_ITEMS = 400;
/** How long a snapshot is considered fresh before a search triggers a background refresh. */
export const HISTORY_INDEX_TTL_MS = 60_000;
interface HistorySessionIndexSnapshot {
items: HistorySessionIndexItem[];
/** Epoch ms of the last write; 0 when never populated. */
updatedAt: number;
}
let snapshot: HistorySessionIndexSnapshot = { items: [], updatedAt: 0 };
let refresher: (() => Promise<void>) | null = null;
let refreshInFlight = false;
/** The merged-list shape this module projects from (a subset of `UnifiedSessionItem`). */
export interface MergedSessionLike {
sessionId: string;
name?: string;
workingDir?: string;
claudeSessionId?: string;
createdAt?: number;
lastActivityAt?: number;
}
/**
* Project a merged unified list into index items. PURE, the caller supplies the
* owner lookup and the live-id set it already has in hand.
*
* Rows with no working directory AND no name are dropped: they can never match a
* query in a useful way and would only consume the cap.
*
* @param merged unified-list items, newest-first (the order the merge returns)
* @param ownerById owner of a session id, for rows tied to a live/persisted session
* @param liveIds session ids currently in the live map
*/
export function buildHistorySessionIndexItems(
merged: MergedSessionLike[],
ownerById: Map<string, string | undefined>,
liveIds: Set<string>
): HistorySessionIndexItem[] {
const items: HistorySessionIndexItem[] = [];
for (const m of merged) {
if (items.length >= HISTORY_INDEX_MAX_ITEMS) break;
const name = m.name ?? '';
const workingDir = m.workingDir ?? '';
if (!name && !workingDir) continue;
items.push({
sessionId: m.sessionId,
name,
workingDir,
claudeSessionId: m.claudeSessionId,
timestamp: m.lastActivityAt ?? m.createdAt ?? 0,
owner: ownerById.get(m.sessionId),
live: liveIds.has(m.sessionId),
});
}
return items;
}
/** Replace the snapshot. Items are capped defensively even if the caller already did. */
export function setHistorySessionIndex(items: HistorySessionIndexItem[], now = Date.now()): void {
snapshot = { items: items.slice(0, HISTORY_INDEX_MAX_ITEMS), updatedAt: now };
}
/**
* Read the snapshot. The returned array is UNSCOPED, callers must apply the
* per-item ownership check before exposing any of it.
*/
export function getHistorySessionIndex(): HistorySessionIndexSnapshot {
return snapshot;
}
/** True when the snapshot has never been written, or is older than the TTL. */
export function isHistorySessionIndexStale(now = Date.now(), ttlMs = HISTORY_INDEX_TTL_MS): boolean {
return snapshot.updatedAt === 0 || now - snapshot.updatedAt > ttlMs;
}
/**
* Register the rebuild function. Called once by the session routes, which own the
* transcript scanner and the stores the unified list is merged from.
*/
export function setHistoryIndexRefresher(fn: (() => Promise<void>) | null): void {
refresher = fn;
}
/**
* Kick a background rebuild if the snapshot is stale. Returns immediately,
* NEVER await this from a request handler, that is the whole point: the search
* path answers from the current snapshot and stays free of disk I/O.
*/
export function ensureHistorySessionIndexFresh(now = Date.now()): void {
if (refreshInFlight || !refresher || !isHistorySessionIndexStale(now)) return;
refreshInFlight = true;
void refresher()
.catch(() => {
// A failed rebuild leaves the previous snapshot in place; the next search retries.
})
.finally(() => {
refreshInFlight = false;
});
}
/** Test hook: drop the snapshot and any registered refresher. */
export function resetHistorySessionIndex(): void {
snapshot = { items: [], updatedAt: 0 };
refresher = null;
refreshInFlight = false;
}
+291
View File
@@ -0,0 +1,291 @@
/**
* @fileoverview Issue #260, the home screen's "Resume Conversation" list.
*
* With ~35 past sessions the list showed 4 rows, then a button that dumped every
* remaining row into a fixed 240px box, with no way to sort or filter. The fix
* moved rendering into `_renderHistoryList()` over a cached corpus, so what is
* worth pinning is the model, not the pixels:
* 1. the collapsed page is _HISTORY_INITIAL_COUNT rows, not 4,
* 2. "Show more" expands the LIST and marks the box expanded (the CSS cap is
* class-driven, without the class, expanding just deepens a scroll well),
* 3. filtering matches name / folder / case label / prompt, and implies
* expansion (hiding matches behind "Show more" defeats typing a filter),
* 4. sorting is alphabetical by name or folder, with pinned rows still on top.
*
* Loaded via `vm` against a stub CodemanApp with a fake DOM, same harness as
* resume-name.test.ts. `_buildHistoryItem` is stubbed: this pins WHICH rows get
* rendered and in what order, not how one row looks.
*/
import { readFileSync } from 'node:fs';
import { resolve } from 'node:path';
import vm from 'node:vm';
import { describe, expect, it, vi } from 'vitest';
interface FakeEl {
id: string;
value: string;
textContent: string;
scrollTop: number;
className: string;
children: FakeEl[];
classes: Set<string>;
listeners: Record<string, ((ev: unknown) => void)[]>;
classList: { toggle: (c: string, on: boolean) => void; contains: (c: string) => boolean };
replaceChildren: () => void;
appendChild: (child: FakeEl) => FakeEl;
addEventListener: (type: string, fn: (ev: unknown) => void) => void;
style: Record<string, string>;
}
function fakeEl(id: string): FakeEl {
const el = {
id,
value: '',
textContent: '',
scrollTop: 0,
className: '',
children: [] as FakeEl[],
classes: new Set<string>(),
listeners: {} as Record<string, ((ev: unknown) => void)[]>,
style: {} as Record<string, string>,
} as FakeEl;
el.classList = {
toggle: (c: string, on: boolean) => (on ? el.classes.add(c) : el.classes.delete(c)),
contains: (c: string) => el.classes.has(c),
};
el.replaceChildren = () => {
el.children = [];
};
el.appendChild = (child: FakeEl) => {
el.children.push(child);
return child;
};
el.addEventListener = (type: string, fn: (ev: unknown) => void) => {
(el.listeners[type] ||= []).push(fn);
};
return el;
}
/* eslint-disable @typescript-eslint/no-explicit-any */
/**
* The element map the vm's `document.getElementById` resolves against. Swapped
* per test, the closure is defined in THIS realm, so the shipping code inside
* the vm reads whatever the current test installed.
*/
let currentEls: Record<string, FakeEl> = {};
function loadTerminalUiPrototype(): Record<string, any> {
const source = readFileSync(resolve(import.meta.dirname, '../src/web/public/terminal-ui.js'), 'utf8');
const context = vm.createContext({
console,
CodemanApp: class CodemanApp {},
setInterval: vi.fn(),
clearInterval: vi.fn(),
setTimeout,
clearTimeout,
requestAnimationFrame: vi.fn(),
document: {
addEventListener: vi.fn(),
getElementById: (id: string) => currentEls[id] ?? null,
createElement: () => fakeEl('created'),
},
window: { addEventListener: vi.fn(), removeEventListener: vi.fn() },
});
vm.runInContext(`${source}\nglobalThis.__proto = CodemanApp.prototype;`, context);
return (context as unknown as { __proto: Record<string, any> }).__proto;
}
const proto = loadTerminalUiPrototype();
type Row = {
sessionId: string;
name?: string;
workingDir?: string;
firstPrompt?: string;
pinned?: boolean;
lastActivityAt?: number;
};
/** Host object carrying the real render/filter/sort methods over a fake DOM. */
function makeApp(rows: Row[], cases: Array<{ name: string; path: string }> = []) {
const els: Record<string, FakeEl> = {
historyList: fakeEl('historyList'),
historyFilter: fakeEl('historyFilter'),
historySort: fakeEl('historySort'),
historyCount: fakeEl('historyCount'),
};
els.historySort.value = 'recent';
const app: any = {
_HISTORY_INITIAL_COUNT: proto._HISTORY_INITIAL_COUNT,
_historyAll: rows,
_historyCases: cases,
_renderHistoryList: proto._renderHistoryList,
_historyRowMatches: proto._historyRowMatches,
_sortHistoryRows: proto._sortHistoryRows,
_historyRowLabel: proto._historyRowLabel,
_resolveCaseLabel: proto._resolveCaseLabel,
_shortenHomePath: proto._shortenHomePath,
// One fake node per row, tagged so assertions can read back the order.
_buildHistoryItem: (s: Row) => {
const el = fakeEl('item');
el.textContent = s.sessionId;
return el;
},
els,
/** Rendered row ids, excluding the show-more/less button and empty state. */
renderedIds(): string[] {
return els.historyList.children.filter((c) => c.id === 'item').map((c) => c.textContent);
},
button(): FakeEl | undefined {
return els.historyList.children.find((c) => c.id === 'created');
},
};
// Point the vm's document at this app's elements, then run the shipping method.
app._render = () => {
currentEls = els;
app._renderHistoryList();
};
return app;
}
function rows(n: number, overrides: Partial<Row> = {}): Row[] {
return Array.from({ length: n }, (_, i) => ({
sessionId: `s${i}`,
name: `w${i}-project${i}`,
workingDir: `/home/u/project${i}`,
lastActivityAt: 1000 - i,
...overrides,
}));
}
describe('issue #260: collapsed page size', () => {
it('shows more than the old 4 rows before "Show more"', () => {
expect(proto._HISTORY_INITIAL_COUNT).toBeGreaterThanOrEqual(8);
});
it('renders the initial page and a "Show more" button for the rest', () => {
const app = makeApp(rows(35));
app._render();
expect(app.renderedIds()).toHaveLength(proto._HISTORY_INITIAL_COUNT);
expect(app.button()?.textContent).toBe(`Show ${35 - proto._HISTORY_INITIAL_COUNT} more`);
expect(app.els.historyList.classList.contains('expanded')).toBe(false);
});
it('expanding renders every row AND marks the box expanded', () => {
const app = makeApp(rows(35));
app._historyExpanded = true;
app._render();
expect(app.renderedIds()).toHaveLength(35);
// Without this class the CSS max-height stays at the collapsed cap and the
// extra rows land in a four-row scroll well, the original bug.
expect(app.els.historyList.classList.contains('expanded')).toBe(true);
expect(app.button()?.textContent).toBe('Show less');
});
it('shows no button at all when everything fits', () => {
const app = makeApp(rows(3));
app._render();
expect(app.renderedIds()).toHaveLength(3);
expect(app.button()).toBeUndefined();
});
});
describe('issue #260: filter', () => {
it('matches on folder name and shows every match without expanding first', () => {
const app = makeApp([
...rows(30),
{ sessionId: 'x1', name: 'w99-invoices', workingDir: '/home/u/invoices', lastActivityAt: 1 },
{ sessionId: 'x2', name: 'w98-other', workingDir: '/home/u/invoices-archive', lastActivityAt: 2 },
]);
app.els.historyFilter.value = 'invoices';
app._render();
expect(app.renderedIds().sort()).toEqual(['x1', 'x2']);
expect(app.els.historyList.classList.contains('expanded')).toBe(true);
expect(app.els.historyCount.textContent).toBe('2 of 32');
});
it('matches on the case label and on a prompt', () => {
const app = makeApp(
[
{ sessionId: 'c1', name: 'w1-x', workingDir: '/home/u/cases/billing', lastActivityAt: 1 },
{
sessionId: 'p1',
name: 'w2-y',
workingDir: '/home/u/other',
firstPrompt: 'fix the CSV export',
lastActivityAt: 2,
},
],
[{ name: 'billing', path: '/home/u/cases/billing' }]
);
app.els.historyFilter.value = '#billing';
app._render();
expect(app.renderedIds()).toEqual(['c1']);
app.els.historyFilter.value = 'csv export';
app._render();
expect(app.renderedIds()).toEqual(['p1']);
});
it('renders an empty state when nothing matches', () => {
const app = makeApp(rows(5));
app.els.historyFilter.value = 'zzzz';
app._render();
expect(app.renderedIds()).toEqual([]);
expect(app.els.historyList.children[0].textContent).toContain('No conversations match');
});
});
describe('issue #260: sort', () => {
const unsorted: Row[] = [
{ sessionId: 'b', name: 'beta', workingDir: '/home/u/zeta', lastActivityAt: 300 },
{ sessionId: 'a', name: 'alpha', workingDir: '/home/u/yankee', lastActivityAt: 200 },
{ sessionId: 'c', name: 'gamma', workingDir: '/home/u/xray', lastActivityAt: 100 },
];
it('recent keeps the backend order', () => {
const app = makeApp(unsorted);
app._render();
expect(app.renderedIds()).toEqual(['b', 'a', 'c']);
});
it('sorts by name', () => {
const app = makeApp(unsorted);
app.els.historySort.value = 'name';
app._render();
expect(app.renderedIds()).toEqual(['a', 'b', 'c']);
});
it('sorts by folder basename', () => {
const app = makeApp(unsorted);
app.els.historySort.value = 'folder';
app._render();
expect(app.renderedIds()).toEqual(['c', 'a', 'b']);
});
it('sorts transcript rows (no session name) by the prompt shown as their title', () => {
// Most past rows come from a transcript and have no name at all. Keying the
// A–Z sort off `name` alone made "Name A–Z" a no-op for them.
const app = makeApp([
{ sessionId: 'z', workingDir: '/home/u/one', firstPrompt: 'zebra crossing' },
{ sessionId: 'a', workingDir: '/home/u/two', firstPrompt: 'apple pie' },
{ sessionId: 'm', workingDir: '/home/u/three', firstPrompt: 'middle ground' },
]);
app.els.historySort.value = 'name';
app._render();
expect(app.renderedIds()).toEqual(['a', 'm', 'z']);
});
it('keeps pinned rows on top in every sort mode', () => {
const app = makeApp([{ sessionId: 'p', name: 'zulu', workingDir: '/home/u/zulu', pinned: true }, ...unsorted]);
for (const mode of ['recent', 'name', 'folder']) {
app.els.historySort.value = mode;
app._render();
expect(app.renderedIds()[0]).toBe('p');
}
});
});
-104
View File
@@ -1,104 +0,0 @@
// Port: none (pure static analysis, runs in CI, no browser/server).
//
// Regression guard for the Read My Mind phase-3 surfaces (alternates, steer,
// phone accessory key, overview waiting-row shortcut). The browser E2E that
// exercised these lives outside CI, so this test pins the load-bearing facts
// statically, in the style of mobile-header-buttons-policy.test.ts:
//
// 1. The accessory-bar 🧠 key exists in BOTH layouts and ships `hidden`
// (setMode() swaps innerHTML; a key present in only one layout silently
// vanishes when the user toggles extendedKeyboardBar).
// 2. `.accessory-btn[hidden]` is re-asserted as display:none !important:
// the base rule is display:inline-flex, which beats the UA [hidden] rule,
// so without this the key can never hide (same trap as .home-sessions).
// 3. The header 🧠 button stays OFF phones (the accessory key + overview strip
// are the phone surfaces).
// 4. The overview shortcut is gated to `waiting` rows only: on red rows a
// dialog is on screen and text sent via POST /input would land in its menu.
// Answer-aware routing through the approvals endpoint is phase-3 PR 2;
// loosening this gate before that lands is a real misdelivery bug.
// 5. The 'readmymind' action must NOT refocus the terminal (the modal takes
// over; a refocus would pop the phone keyboard back up over it).
import { describe, it, expect } from 'vitest';
import { readFileSync } from 'node:fs';
import { fileURLToPath } from 'node:url';
import { join } from 'node:path';
const HERE = fileURLToPath(new URL('.', import.meta.url));
const PUBLIC = join(HERE, '../src/web/public');
const accessoryJs = readFileSync(join(PUBLIC, 'keyboard-accessory.js'), 'utf8');
const overviewJs = readFileSync(join(PUBLIC, 'mobile-overview.js'), 'utf8');
const stylesCss = readFileSync(join(PUBLIC, 'styles.css'), 'utf8');
const mobileCss = readFileSync(join(PUBLIC, 'mobile.css'), 'utf8');
const indexHtml = readFileSync(join(PUBLIC, 'index.html'), 'utf8');
/** Extract a template-literal property body, e.g. `_simpleButtons: \`...\``. */
function templateBody(source: string, prop: string): string {
const m = source.match(new RegExp(`${prop}:\\s*\``));
if (!m || m.index === undefined) return '';
const start = m.index + m[0].length;
const end = source.indexOf('`', start);
return end === -1 ? '' : source.slice(start, end);
}
describe('read my mind phase-3 surfaces (static policy)', () => {
it('accessory 🧠 key exists in BOTH layouts and ships hidden', () => {
for (const layout of ['_simpleButtons', '_extendedButtons']) {
const body = templateBody(accessoryJs, layout);
expect(body, `${layout} template found`).not.toBe('');
const keys = body.match(/data-action="readmymind"/g) || [];
expect(keys.length, `${layout} has exactly one readmymind key`).toBe(1);
const tag = body.match(/<button[^>]*data-action="readmymind"[^>]*>/);
expect(tag, `${layout} readmymind button tag parses`).toBeTruthy();
expect(tag![0], `${layout} key ships hidden (refreshReadMyMind reveals it)`).toContain('hidden');
}
});
it('refreshReadMyMind is re-applied after every innerHTML rebuild', () => {
// init() and setMode() both assign innerHTML, which resurrects the default
// `hidden` attribute; each must re-derive visibility afterwards.
const calls = accessoryJs.match(/this\.refreshReadMyMind\(\)/g) || [];
expect(calls.length, 'called from init() and setMode() at least').toBeGreaterThanOrEqual(2);
// The setMode rebuild specifically must be followed by a refresh.
expect(accessoryJs).toMatch(
/innerHTML = mode === 'extended' \? this\._extendedButtons : this\._simpleButtons;[\s\S]{0,300}?this\.refreshReadMyMind\(\)/
);
});
it('accessory [hidden] re-assertion exists (inline-flex beats the UA rule)', () => {
expect(stylesCss).toMatch(/\.accessory-btn\[hidden\]\s*\{\s*display:\s*none\s*!important;/);
});
it('the readmymind action never refocuses the terminal', () => {
const refocus = accessoryJs.match(/refocusActions\s*=\s*new Set\(\[([^\]]*)\]\)/);
expect(refocus, 'refocusActions set found').toBeTruthy();
expect(refocus![1]).not.toContain('readmymind');
});
it('header 🧠 button stays hidden on phones', () => {
expect(mobileCss).toMatch(/\.btn-icon-header\.btn-readmymind\s*\{\s*display:\s*none\s*!important;/);
});
it('overview shortcut is gated to waiting rows only (red rows have a dialog on screen)', () => {
const defIdx = overviewJs.indexOf('_readMyMindRowShortcut(row) {');
expect(defIdx, '_readMyMindRowShortcut definition found').toBeGreaterThan(-1);
const gate = overviewJs.slice(defIdx, overviewJs.indexOf('_buildMobileOverviewRmmStrip(sessionId) {'));
expect(gate, 'definition precedes its strip builder').not.toBe('');
expect(gate).toContain("row.state !== 'waiting'");
});
it('alternates container and steer input exist with i18n protection', () => {
// Suggestion content is observed/injectable text; the container-level skip
// covers the dynamically inserted rows (i18n skip checks ancestors).
expect(indexHtml).toMatch(/id="readMyMindAlternates"[^>]*data-i18n-skip/);
expect(indexHtml).toContain('id="readMyMindSteer"');
// The steer field lives OUTSIDE the result div so it stays available in
// the error phase (steering a failed run's retry).
const resultDiv = indexHtml.slice(
indexHtml.indexOf('class="readmymind-result"'),
indexHtml.indexOf('class="readmymind-error"')
);
expect(resultDiv).not.toContain('id="readMyMindSteer"');
});
});
+96 -1
View File
@@ -9,12 +9,13 @@
* (sessions + events) return results. Source data is injected via the mock
* route context (sessions map, runSummaryTrackers map, attachment history).
*/
import { describe, it, expect, beforeEach } from 'vitest';
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import Fastify, { type FastifyInstance } from 'fastify';
import { registerSearchRoutes } from '../../src/web/routes/search-routes.js';
import { installRouteErrorHandler } from '../../src/web/route-error-handler.js';
import { createMockRouteContext } from '../mocks/index.js';
import { RunSummaryTracker } from '../../src/run-summary.js';
import { resetHistorySessionIndex, setHistorySessionIndex } from '../../src/web/session-history-index.js';
type Ctx = ReturnType<typeof createMockRouteContext>;
@@ -206,3 +207,97 @@ describe('GET /api/search — caps & filters', () => {
expect(types).toEqual(['event']);
});
});
// Issue #261: with 3 live sessions and ~35 past ones, searching a past project's
// folder name matched nothing, the corpus was the live session map alone. Past
// sessions now arrive from the out-of-band history index snapshot.
describe('GET /api/search: past sessions (history index)', () => {
beforeEach(() => {
resetHistorySessionIndex();
});
afterEach(() => {
resetHistorySessionIndex();
delete process.env.CODEMAN_MULTIUSER;
});
it('matches a past session by folder name with no live session at all', async () => {
const { app } = await harness();
setHistorySessionIndex([
{
sessionId: 'cod-9',
name: 'w4-needlework',
workingDir: '/home/u/projects/needlework',
claudeSessionId: 'claude-uuid',
timestamp: 1000,
live: false,
},
]);
const res = await app.inject({ method: 'GET', url: '/api/search?q=needlework' });
expect(res.statusCode).toBe(200);
const body = JSON.parse(res.body);
expect(body.data.totalResults).toBe(1);
expect(body.data.groups[0].results[0].jumpTo).toMatchObject({
kind: 'resume-session',
sessionId: 'cod-9',
claudeSessionId: 'claude-uuid',
});
});
it('does not duplicate a session that is both live and in the snapshot', async () => {
const { app } = await harness((ctx) => {
ctx.sessions.set(
'dup',
fakeSession({ id: 'dup', name: 'needle live', workingDir: '/home/u/needle', lastActivityAt: 5 }) as never
);
});
setHistorySessionIndex([
{ sessionId: 'dup', name: 'needle live', workingDir: '/home/u/needle', timestamp: 5, live: true },
]);
const body = JSON.parse((await app.inject({ method: 'GET', url: '/api/search?q=needle' })).body);
expect(body.data.totalResults).toBe(1);
// The live harvest wins, so the card still switches to the open tab.
expect(body.data.groups[0].results[0].jumpTo.kind).toBe('session');
});
it('multi-user: a non-admin sees neither another user’s past session nor unowned host-wide history', async () => {
process.env.CODEMAN_MULTIUSER = '1';
const app = Fastify({ logger: false });
app.addHook('onRequest', async (req) => {
(req as unknown as { authUser: unknown }).authUser = { username: 'bob', role: 'user' };
});
const ctx = createMockRouteContext();
ctx.sessions.clear();
ctx.runSummaryTrackers.clear();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
registerSearchRoutes(app, ctx as any);
installRouteErrorHandler(app);
await app.ready();
setHistorySessionIndex([
{
sessionId: 'mine',
name: 'needle-bob',
workingDir: '/home/u/needle-bob',
timestamp: 3,
owner: 'bob',
live: false,
},
{
sessionId: 'hers',
name: 'needle-alice',
workingDir: '/home/u/needle-alice',
timestamp: 2,
owner: 'alice',
live: false,
},
// Host-wide transcript row: no owning session, so admin-only, the same
// rule GET /api/sessions/unified applies when it drops history for non-admins.
{ sessionId: 'hostwide', name: 'needle-host', workingDir: '/srv/needle-host', timestamp: 1, live: false },
]);
const body = JSON.parse((await app.inject({ method: 'GET', url: '/api/search?q=needle' })).body);
expect(body.data.groups[0].results.map((r: { sessionId: string }) => r.sessionId)).toEqual(['mine']);
await app.close();
});
});
+58
View File
@@ -233,3 +233,61 @@ describe('searchSources — result card shape & path safety', () => {
expect(searchSources('', data).totalResults).toBe(0);
});
});
// Past sessions (issue #261). The corpus used to be the live session map alone,
// so a folder in the home screen's Resume list matched nothing. History rows now
// arrive marked, and a card for one has to RESUME the conversation, selecting a
// tab that no longer exists is a no-op the user reads as a broken result.
describe('searchSources: past (history) sessions', () => {
it('matches a past session by folder name and returns a resume jump target', () => {
const data = sources({
sessions: [
{
sessionId: 'cod-1',
sessionName: 'w3-invoices',
workingDir: '/home/u/projects/invoices',
timestamp: 500,
history: true,
claudeSessionId: 'claude-uuid-1',
},
],
});
const res = searchSources('invoices', data);
expect(res.totalResults).toBe(1);
expect(res.groups[0].results[0].jumpTo).toEqual({
kind: 'resume-session',
sessionId: 'cod-1',
claudeSessionId: 'claude-uuid-1',
workingDir: '/home/u/projects/invoices',
});
});
it('keeps a live session on the plain session jump target', () => {
const data = sources({
sessions: [{ sessionId: 'live-1', sessionName: 'w1-invoices', workingDir: '/home/u/invoices', timestamp: 1 }],
});
expect(searchSources('invoices', data).groups[0].results[0].jumpTo).toEqual({
kind: 'session',
sessionId: 'live-1',
});
});
it('does not offer a resume for a history row with no working directory', () => {
const data = sources({
sessions: [{ sessionId: 'cod-2', sessionName: 'needle-run', workingDir: '', timestamp: 1, history: true }],
});
// Nothing to resume INTO, a resume card here would always fail.
expect(searchSources('needle', data).groups[0].results[0].jumpTo.kind).toBe('session');
});
it('falls back to the folder basename when a transcript row has no name', () => {
const data = sources({
sessions: [
{ sessionId: 'cod-3', sessionName: '', workingDir: '/home/u/proj/needle-app', timestamp: 1, history: true },
],
});
const r = searchSources('needle', data).groups[0].results[0];
expect(r.sessionName).toBe('needle-app');
expect(r.snippet).toContain('/home/u/proj/needle-app');
});
});
+161
View File
@@ -0,0 +1,161 @@
/**
* Unit tests for the past-session search index (issue #261).
*
* The index is the seam that lets `GET /api/search` match sessions that are no
* longer running WITHOUT doing disk I/O per keystroke. Three properties matter
* and are pinned here: the snapshot stays bounded, the refresh never happens on
* the caller's timeline (fire-and-forget, single-flight, TTL-guarded), and the
* stored rows carry the owner needed to re-apply multi-user scoping on read,
* the snapshot is written unscoped, so losing that field would leak one user's
* folders into another user's search.
*/
import { describe, it, expect, beforeEach, vi } from 'vitest';
import {
buildHistorySessionIndexItems,
ensureHistorySessionIndexFresh,
getHistorySessionIndex,
isHistorySessionIndexStale,
resetHistorySessionIndex,
setHistoryIndexRefresher,
setHistorySessionIndex,
HISTORY_INDEX_MAX_ITEMS,
HISTORY_INDEX_TTL_MS,
type MergedSessionLike,
} from '../src/web/session-history-index.js';
beforeEach(() => {
resetHistorySessionIndex();
});
describe('buildHistorySessionIndexItems', () => {
const merged: MergedSessionLike[] = [
{ sessionId: 'a', name: 'w1-alpha', workingDir: '/home/u/alpha', lastActivityAt: 300 },
{ sessionId: 'b', name: '', workingDir: '/home/u/beta', claudeSessionId: 'uuid-b', createdAt: 200 },
{ sessionId: 'c', name: 'gamma', workingDir: '', lastActivityAt: 100 },
];
it('projects name, dir, timestamp, owner and liveness', () => {
const items = buildHistorySessionIndexItems(
merged,
new Map([
['a', 'alice'],
['b', undefined],
]),
new Set(['a'])
);
expect(items.map((i) => i.sessionId)).toEqual(['a', 'b', 'c']);
expect(items[0]).toMatchObject({ owner: 'alice', live: true, timestamp: 300 });
// Transcript-only row: no owner (host-wide) and not live.
expect(items[1]).toMatchObject({ owner: undefined, live: false, timestamp: 200, claudeSessionId: 'uuid-b' });
});
it('drops rows with neither a name nor a working directory', () => {
const items = buildHistorySessionIndexItems([{ sessionId: 'empty' }, ...merged], new Map(), new Set());
expect(items.some((i) => i.sessionId === 'empty')).toBe(false);
});
it('caps the projection at HISTORY_INDEX_MAX_ITEMS', () => {
const many: MergedSessionLike[] = Array.from({ length: HISTORY_INDEX_MAX_ITEMS + 50 }, (_, i) => ({
sessionId: `s${i}`,
name: `session ${i}`,
workingDir: `/home/u/p${i}`,
lastActivityAt: i,
}));
expect(buildHistorySessionIndexItems(many, new Map(), new Set())).toHaveLength(HISTORY_INDEX_MAX_ITEMS);
});
});
describe('snapshot storage', () => {
it('starts empty and stale', () => {
expect(getHistorySessionIndex().items).toEqual([]);
expect(isHistorySessionIndexStale()).toBe(true);
});
it('caps on write even when the caller did not', () => {
const items = Array.from({ length: HISTORY_INDEX_MAX_ITEMS + 10 }, (_, i) => ({
sessionId: `s${i}`,
name: 'x',
workingDir: '/x',
timestamp: i,
live: false,
}));
setHistorySessionIndex(items);
expect(getHistorySessionIndex().items).toHaveLength(HISTORY_INDEX_MAX_ITEMS);
});
it('goes stale again once the TTL elapses', () => {
const t0 = 1_000_000;
setHistorySessionIndex([{ sessionId: 's', name: 'n', workingDir: '/d', timestamp: 1, live: false }], t0);
expect(isHistorySessionIndexStale(t0 + HISTORY_INDEX_TTL_MS - 1)).toBe(false);
expect(isHistorySessionIndexStale(t0 + HISTORY_INDEX_TTL_MS + 1)).toBe(true);
});
});
describe('ensureHistorySessionIndexFresh', () => {
it('returns synchronously, the rebuild must never be on the request path', async () => {
let resolveRefresh: () => void = () => {};
const refresher = vi.fn(
() =>
new Promise<void>((resolve) => {
resolveRefresh = resolve;
})
);
setHistoryIndexRefresher(refresher);
ensureHistorySessionIndexFresh();
// Called, but the caller is already past it while the rebuild is pending.
expect(refresher).toHaveBeenCalledTimes(1);
expect(getHistorySessionIndex().items).toEqual([]);
resolveRefresh();
await Promise.resolve();
});
it('is single-flight: a second call while a rebuild is pending is a no-op', async () => {
let resolveRefresh: () => void = () => {};
const refresher = vi.fn(
() =>
new Promise<void>((resolve) => {
resolveRefresh = resolve;
})
);
setHistoryIndexRefresher(refresher);
ensureHistorySessionIndexFresh();
ensureHistorySessionIndexFresh();
ensureHistorySessionIndexFresh();
expect(refresher).toHaveBeenCalledTimes(1);
resolveRefresh();
await new Promise((r) => setTimeout(r, 0));
// Snapshot still stale (the fake refresher wrote nothing) → next call runs again.
ensureHistorySessionIndexFresh();
expect(refresher).toHaveBeenCalledTimes(2);
});
it('does not rebuild while the snapshot is fresh', () => {
const refresher = vi.fn(async () => {});
setHistoryIndexRefresher(refresher);
setHistorySessionIndex([{ sessionId: 's', name: 'n', workingDir: '/d', timestamp: 1, live: false }]);
ensureHistorySessionIndexFresh();
expect(refresher).not.toHaveBeenCalled();
});
it('keeps the previous snapshot when a rebuild throws, and retries next time', async () => {
setHistorySessionIndex([{ sessionId: 'keep', name: 'n', workingDir: '/d', timestamp: 1, live: false }], 1);
const refresher = vi.fn(async () => {
throw new Error('scan failed');
});
setHistoryIndexRefresher(refresher);
ensureHistorySessionIndexFresh();
await new Promise((r) => setTimeout(r, 0));
expect(getHistorySessionIndex().items[0].sessionId).toBe('keep');
ensureHistorySessionIndexFresh();
expect(refresher).toHaveBeenCalledTimes(2);
});
it('is a no-op when no refresher is registered', () => {
expect(() => ensureHistorySessionIndexFresh()).not.toThrow();
});
});