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:
1 parent
086cac8c8e
commit
17fcf931c0
11 files changed
+431
-30
No files matched your search
@@ -267,4 +267,83 @@ describe("workspace disk discovery", () => {
|
||||
discovery.invalidate();
|
||||
expect(discovery.discover()).toEqual([]);
|
||||
});
|
||||
|
||||
describe("display-title preview", () => {
|
||||
function titleFile(body: string): { dir: string; file: string } {
|
||||
const dir = writeSessionDir(root, encodedDirName(sandbox), [
|
||||
{ name: "2026-10-10T06-40-33-032Z_title.jsonl", cwd: sandbox, id: "title", body },
|
||||
]);
|
||||
return { dir, file: join(dir, "2026-10-10T06-40-33-032Z_title.jsonl") };
|
||||
}
|
||||
|
||||
it("prefers the latest session_info name and falls back to the first user message", () => {
|
||||
const { file } = titleFile(
|
||||
[
|
||||
JSON.stringify({ type: "session_info", name: "First name" }),
|
||||
JSON.stringify({
|
||||
type: "message",
|
||||
message: { role: "user", content: [{ type: "text", text: "hello\n world from me" }] },
|
||||
}),
|
||||
JSON.stringify({ type: "session_info", name: "Renamed session" }),
|
||||
].join("\n") + "\n",
|
||||
);
|
||||
|
||||
const discovery = createDiscovery({ root });
|
||||
// Listing stays header-only: no preview work on the hot path.
|
||||
expect(discovery.listSessions(sandbox)[0]?.firstMessage).toBeUndefined();
|
||||
expect(discovery.previewSession(file)).toEqual({
|
||||
name: "Renamed session",
|
||||
firstMessage: "hello world from me",
|
||||
});
|
||||
});
|
||||
|
||||
it("supports a plain-string user message and ignores assistant turns", () => {
|
||||
const { file } = titleFile(
|
||||
[
|
||||
JSON.stringify({ type: "message", message: { role: "assistant", content: "not me" } }),
|
||||
JSON.stringify({ type: "message", message: { role: "user", content: "plain string prompt" } }),
|
||||
].join("\n") + "\n",
|
||||
);
|
||||
|
||||
expect(createDiscovery({ root }).previewSession(file)).toEqual({ firstMessage: "plain string prompt" });
|
||||
});
|
||||
|
||||
it("finds a name written past the head window in the tail window", () => {
|
||||
const filler = `{"type":"note","data":"${"x".repeat(200 * 1024)}"}`;
|
||||
const { file } = titleFile(
|
||||
[
|
||||
JSON.stringify({ type: "message", message: { role: "user", content: "start here" } }),
|
||||
filler,
|
||||
JSON.stringify({ type: "session_info", name: "Late name" }),
|
||||
].join("\n") + "\n",
|
||||
);
|
||||
|
||||
expect(createDiscovery({ root }).previewSession(file)).toEqual({
|
||||
name: "Late name",
|
||||
firstMessage: "start here",
|
||||
});
|
||||
});
|
||||
|
||||
it("caches by (mtime, size) and refreshes on invalidate()", () => {
|
||||
const header = `${sessionHeader(sandbox, "title")}\n`;
|
||||
const { file } = titleFile(`${JSON.stringify({ type: "session_info", name: "Cached" })}\n`);
|
||||
const modified = statSync(file).mtimeMs;
|
||||
const discovery = createDiscovery({ root });
|
||||
expect(discovery.previewSession(file, modified)).toEqual({ name: "Cached" });
|
||||
|
||||
// Same size, deliberately reused recorded mtime: a cache hit.
|
||||
writeFileSync(file, `${header}${JSON.stringify({ type: "session_info", name: "Edited" })}\n`);
|
||||
expect(discovery.previewSession(file, modified)).toEqual({ name: "Cached" });
|
||||
|
||||
discovery.invalidate();
|
||||
expect(discovery.previewSession(file, modified)).toEqual({ name: "Edited" });
|
||||
});
|
||||
|
||||
it("returns undefined for a file with no readable title", () => {
|
||||
const { file } = titleFile("garbage that is not json\n");
|
||||
const discovery = createDiscovery({ root });
|
||||
expect(discovery.previewSession(file)).toBeUndefined();
|
||||
expect(discovery.previewSession(join(sandbox, "missing.jsonl"))).toBeUndefined();
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -156,6 +156,9 @@ describe("workspace store", () => {
|
||||
timestamp: Date.parse("2026-10-13T06:40:33.032Z"),
|
||||
modified: Date.parse("2026-10-13T06:40:33.032Z"),
|
||||
sessionFile: expect.stringContaining("g1"),
|
||||
// The helper writes a user message "hi": the disk preview labels
|
||||
// the row that has no live instance.
|
||||
firstMessage: "hi",
|
||||
});
|
||||
// The default (no limit) still returns every discovered session, and an
|
||||
// out-of-range limit is clamped instead of exploding the header scan.
|
||||
|
||||
Reference in new issue
Block a user