feat(workspaces): title recent-activity rows from a bounded preview

The activity column's disk-only rows had no title: the recent feed carried file
metadata only, so every session outside a live instance rendered "(unnamed)".

recentSessions() now projects a display title for the rows it returns — the
latest `session_info` name and the first user message — from a 128 KiB head +
32 KiB tail of each session file, cached by (mtime, size). Entries are never
parsed into session state: the read is bounded, opt-in, and only paid for the
≤limit rows the caller renders, so the daemon stays a router.

The web client keeps the disk title as a fallback under the live state, and the
B4 e2e scenario asserts session-first rows with their links and titles.
This commit is contained in:
yuez committed 2026-10-30 15:15:09 +08:00
1 parent 086cac8c8e
commit 17fcf931c0
11 files changed
+431 -30

No files matched your search

+11 -3
View File
@@ -321,9 +321,10 @@ export type WorkspaceVisibilityReason = "hidden" | "ghost" | "temp" | "outside-r
/**
* One disk-projected session row for the cross-workspace recent-activity feed.
*
* This is file metadata only (header id/timestamp + mtime); the daemon never
* parses entries or owns session state (docs/protocol.md §3.1). Live names and
* statuses are layered on by the client through `/workspaces/:id/sessions`.
* This is file metadata plus an optional bounded display title (latest
* `session_info` name / first user message); the daemon never parses entries
* into session state (docs/protocol.md §3.1). Live names and statuses are
* layered on by the client through `/workspaces/:id/sessions`.
*/
export interface RecentSessionInfo {
sessionId: string;
@@ -336,6 +337,13 @@ export interface RecentSessionInfo {
timestamp: number;
/** File mtime, epoch ms — the recency sort key. */
modified: number;
/**
* Latest `session_info.name` from the session file's bounded preview
* windows. Display metadata only; absent when the file has no name yet.
*/
name?: string;
/** First user message preview (flattened, ≤80 chars), when readable. */
firstMessage?: string;
}
export type DaemonCommandMethod =
+207 -4
View File
@@ -8,8 +8,12 @@
* and the separator), so the authoritative cwd is the first JSONL line:
* {"type":"session","version":3,"id":"…","timestamp":"…","cwd":"/abs/path"}
*
* Scanning is header-only (one read of up to 64 KiB per directory) and cached
* per directory by (mtime, file count). The rest of a session file is never read.
* Workspace/session *listing* is header-only (one read of up to 64 KiB per
* directory) and cached per directory by (mtime, file count). `previewSession()`
* is the one opt-in exception: it reads a bounded head/tail window for a display
* title (latest `session_info.name` + first user message) so the recent-activity
* feed can label a session that has no live instance. It never parses entries
* into session state and is cached by (mtime, size).
*/
import { closeSync, openSync, readdirSync, readSync, statSync } from "node:fs";
@@ -19,6 +23,30 @@ import { sessionsRoot, workspaceIdForCwd } from "../core/paths.js";
/** Maximum bytes read to obtain the first JSONL line. Headers are ~150 bytes. */
export const HEADER_READ_LIMIT = 64 * 1024;
/**
* Head window for a display title. The first user message and the auto-title
* `session_info` line both land in the first ~50 KiB of a real session (the
* system-prompt message is the bulk of it), so 128 KiB leaves generous room.
*/
export const PREVIEW_HEAD_LIMIT = 128 * 1024;
/** Tail window where a later `session_info.name` (rename / re-title) may live. */
export const PREVIEW_TAIL_LIMIT = 32 * 1024;
/** `firstMessage` is flattened and cut exactly like pi2.nvim's `history.parse`. */
export const PREVIEW_MESSAGE_CHARS = 80;
/** Bound the preview cache; a recent feed touches a few dozen files at most. */
const PREVIEW_CACHE_LIMIT = 512;
/** Bounded display metadata read from a session file. Never session state. */
export interface SessionPreview {
/** Latest non-empty `session_info.name`, when one is inside the windows. */
name?: string;
/** First user message, flattened to one line and cut to 80 characters. */
firstMessage?: string;
}
export interface DiscoveredWorkspace {
workspaceId: string;
/** Authoritative cwd from the session header (not decoded from `dir`). */
@@ -65,6 +93,13 @@ export interface Discovery {
discover(): DiscoveredWorkspace[];
/** Session files for a cwd, newest first (header-only, cheap). */
listSessions(cwd: string, limit?: number): SessionFile[];
/**
* Opt-in display title for one session file: the latest `session_info`
* name and the first user message. Reads at most `PREVIEW_HEAD_LIMIT` from
* the start plus `PREVIEW_TAIL_LIMIT` from the end, and caches by
* (mtime, size); `undefined` means the windows contain no usable title.
*/
previewSession(file: string, modified?: number): SessionPreview | undefined;
/** Drop one directory's cache entry, or the whole cache when omitted. */
invalidate(dir?: string): void;
/** The encoded directory used for a cwd, when it has been discovered. */
@@ -91,6 +126,12 @@ interface SessionHeader {
timestamp?: string;
}
interface PreviewCacheEntry {
mtimeMs: number;
size: number;
preview: SessionPreview | undefined;
}
export function isSessionDirName(name: string): boolean {
return name.length > 4 && name.startsWith("--") && name.endsWith("--");
}
@@ -99,6 +140,7 @@ class DiskDiscovery implements Discovery {
private readonly root: string;
private readonly logger: DiscoveryLogger | undefined;
private readonly cache = new Map<string, CacheEntry>();
private readonly previews = new Map<string, PreviewCacheEntry>();
constructor(options: DiscoveryOptions = {}) {
this.root = options.root ?? sessionsRoot();
@@ -156,9 +198,45 @@ class DiskDiscovery implements Discovery {
return sessions;
}
previewSession(file: string, modified?: number): SessionPreview | undefined {
let size: number;
let mtimeMs: number;
try {
const stat = statSync(file);
size = stat.size;
mtimeMs = modified ?? stat.mtimeMs;
} catch {
return undefined;
}
const cached = this.previews.get(file);
if (cached !== undefined && cached.mtimeMs === mtimeMs && cached.size === size) {
// Refresh insertion order so the LRU cap drops genuinely cold files.
this.previews.delete(file);
this.previews.set(file, cached);
return cached.preview;
}
const preview = readSessionPreview(file, size);
this.previews.set(file, { mtimeMs, size, preview });
if (this.previews.size > PREVIEW_CACHE_LIMIT) {
const oldest = this.previews.keys().next().value;
if (oldest !== undefined) this.previews.delete(oldest);
}
return preview;
}
invalidate(dir?: string): void {
if (dir === undefined) this.cache.clear();
else this.cache.delete(dir);
if (dir === undefined) {
this.cache.clear();
this.previews.clear();
} else {
this.cache.delete(dir);
const prefix = dir.endsWith("/") ? dir : `${dir}/`;
for (const file of this.previews.keys()) {
if (file.startsWith(prefix)) this.previews.delete(file);
}
}
}
dirForCwd(cwd: string): string | undefined {
@@ -304,6 +382,131 @@ function readSessionHeader(file: string): SessionHeader | undefined {
}
}
/**
* Bounded display-title projection for one session file.
*
* Reads the head window (first user message + any early `session_info` name)
* and, for longer files, the tail window (a later rename / auto-title). Lines
* are JSON-decoded only when a cheap substring prefilter hits, so multi-MB
* message or tool-output lines are never decoded. A partial trailing line (the
* window cut a line in half) fails the decode and is ignored; the tail window
* also drops its leading partial line before parsing.
*/
function readSessionPreview(file: string, size: number): SessionPreview | undefined {
let fd: number | undefined;
try {
fd = openSync(file, "r");
const headBytes = Math.min(size, PREVIEW_HEAD_LIMIT);
if (headBytes <= 0) return undefined;
const head = Buffer.allocUnsafe(headBytes);
const headRead = readSync(fd, head, 0, headBytes, 0);
if (headRead <= 0) return undefined;
const found = parsePreviewWindow(head.toString("utf8", 0, headRead), true);
let name = found.name;
let firstMessage = found.firstMessage;
if (size > headRead) {
const tailBytes = Math.min(size - headRead, PREVIEW_TAIL_LIMIT);
if (tailBytes > 0) {
const tail = Buffer.allocUnsafe(tailBytes);
const tailRead = readSync(fd, tail, 0, tailBytes, size - tailBytes);
if (tailRead > 0) {
const text = tail.toString("utf8", 0, tailRead);
// The first line is probably a partial one: drop it.
const newline = text.indexOf("\n");
const tailFound = parsePreviewWindow(newline >= 0 ? text.slice(newline + 1) : "", false);
if (tailFound.name !== undefined) name = tailFound.name;
}
}
}
if (name === undefined && firstMessage === undefined) return undefined;
return {
...(name === undefined ? {} : { name }),
...(firstMessage === undefined ? {} : { firstMessage }),
};
} catch {
return undefined;
} finally {
if (fd !== undefined) {
try {
closeSync(fd);
} catch {
// ignore
}
}
}
}
/** Parse one bounded window of JSONL lines into a display title. */
function parsePreviewWindow(text: string, findFirstMessage: boolean): SessionPreview {
let name: string | undefined;
let firstMessage: string | undefined;
for (const rawLine of text.split("\n")) {
const line = rawLine.endsWith("\r") ? rawLine.slice(0, -1) : rawLine;
if (line.length === 0) continue;
if (line.includes('"type":"session_info"')) {
const entry = parseJsonLine(line);
if (entry?.type === "session_info") {
const candidate = typeof entry.name === "string" ? entry.name.trim() : "";
if (candidate !== "") name = candidate;
}
}
if (findFirstMessage && firstMessage === undefined && line.includes('"type":"message"')) {
const entry = parseJsonLine(line);
const message = recordField(entry, "message");
if (entry?.type === "message" && message?.role === "user") {
const messageText = userMessageText(message.content);
if (messageText !== "") firstMessage = messageText;
}
}
}
return {
...(name === undefined ? {} : { name }),
...(firstMessage === undefined ? {} : { firstMessage }),
};
}
function parseJsonLine(line: string): Record<string, unknown> | undefined {
try {
const parsed: unknown = JSON.parse(line);
return parsed !== null && typeof parsed === "object" && !Array.isArray(parsed)
? (parsed as Record<string, unknown>)
: undefined;
} catch {
return undefined;
}
}
function recordField(value: Record<string, unknown> | undefined, key: string): Record<string, unknown> | undefined {
const field = value?.[key];
return field !== null && typeof field === "object" && !Array.isArray(field)
? (field as Record<string, unknown>)
: undefined;
}
/** First text part of a pi user message, flattened and cut for a row label. */
function userMessageText(content: unknown): string {
let text = "";
if (typeof content === "string") {
text = content;
} else if (Array.isArray(content)) {
for (const part of content) {
if (part !== null && typeof part === "object" && (part as { type?: unknown }).type === "text") {
const value = (part as { text?: unknown }).text;
if (typeof value === "string") {
text = value;
break;
}
}
}
}
return text.replace(/\s+/g, " ").trim().slice(0, PREVIEW_MESSAGE_CHARS);
}
/** `<YYYY-MM-DDTHH-MM-SS-mmmZ>_<id>.jsonl` → `{ id, timestamp }`. */
export function parseSessionFileName(file: string): { id: string; timestamp: number } {
const name = basename(file).replace(/\.jsonl$/, "");
+10 -1
View File
@@ -266,6 +266,7 @@ class WorkspaceStore implements WorkspaceStoreHandle {
* stays bounded even on a workspace with a long history. Rows are deduped by
* session id (two encoded directories can resolve to the same cwd) and sorted
* by file mtime, then header timestamp, then id for a deterministic order.
* Only the returned rows get a bounded display-title preview.
*/
recentSessions(limit = 20): RecentSessionInfo[] {
const capped = clampLimit(limit);
@@ -293,7 +294,15 @@ class WorkspaceStore implements WorkspaceStoreHandle {
b.timestamp - a.timestamp ||
a.sessionId.localeCompare(b.sessionId),
);
return rows.slice(0, capped);
const selected = rows.slice(0, capped);
// Enrich only the rows the caller will render: previews are bounded but
// per-file reads, so never pay for candidates that are sliced away.
for (const row of selected) {
const preview = this.discovery.previewSession(row.sessionFile, row.modified);
if (preview?.name !== undefined) row.name = preview.name;
if (preview?.firstMessage !== undefined) row.firstMessage = preview.firstMessage;
}
return selected;
}
// ------------------------------------------------------------------