Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins
TypeScript ai-agentcoding-agentdesktop-appelectronglobali18nlocal-firstmcppipi-agentpi-desktoppluginsreactrusttypescript
fix(transcript): re-read and never cache an empty durable window (#795)
vastsa1a322fc8c995
diff --git a/apps/desktop/src/lib/session-transcript-read.ts b/apps/desktop/src/lib/session-transcript-read.tsnew file mode 100644index 000000000..415eb4193--- /dev/null+++ b/apps/desktop/src/lib/session-transcript-read.ts@@ -0,0 +1,18 @@+import type { SessionDetail } from "@pi-desktop/shared";++/**+ * Whether a durable window read is suspiciously empty (issue #795).+ *+ * The host answers `session.get` from a transcript file it may be rewriting at+ * that moment, so it can return "the session exists and has no messages" for a+ * session that has thousands. Taking that answer at face value cached an empty+ * transcript that the sidebar's hover prefetch re-served on every later open,+ * and the session stayed blank until the app restarted. A read is only+ * trustworthy as "empty" when the session's own count agrees with it.+ */+export function sessionReadLooksEmpty(+ session: Pick<SessionDetail, "messages" | "messageCount"> | null | undefined,+): boolean {+ if (!session) return false;+ return (session.messages ?? []).length === 0 && (session.messageCount ?? 0) > 0;+}diff --git a/apps/desktop/src/stores/runtime/session-runtime.ts b/apps/desktop/src/stores/runtime/session-runtime.tsindex a9ec8684e..fdcf3609f 100644--- a/apps/desktop/src/stores/runtime/session-runtime.ts+++ b/apps/desktop/src/stores/runtime/session-runtime.ts@@ -14,6 +14,7 @@ import {removeLiveSessionMessage,upsertLiveSessionMessage,} from "../../lib/session-transcript";+import { sessionReadLooksEmpty } from "../../lib/session-transcript-read";import { sessionIsArchived, type SessionMeta } from "../../lib/sidebar-preferences";import {normalizeProjectPath,@@ -170,11 +171,16 @@ export function createSessionRuntime({ get, set }: StoreAccess): SessionRuntimeliveMessages? mergeLiveSessionMessages(detail.session.messages ?? [], liveMessages): detail.session.messages ?? [];- cacheSessionTranscript(id, messages, {- messageStart: detail.session.messageStart ?? 0,- hasMoreBefore: detail.session.hasMoreBefore === true,- contentLimited: options?.contentLimit !== undefined,- });+ // Never cache a suspiciously empty window (issue #795): hover prefetch+ // would then re-serve that emptiness on every later open, and the+ // session could not recover without a restart.+ if (messages.length > 0 || !sessionReadLooksEmpty(detail.session)) {+ cacheSessionTranscript(id, messages, {+ messageStart: detail.session.messageStart ?? 0,+ hasMoreBefore: detail.session.hasMoreBefore === true,+ contentLimited: options?.contentLimit !== undefined,+ });+ }}return detail;});diff --git a/apps/desktop/src/stores/slices/session-slice.ts b/apps/desktop/src/stores/slices/session-slice.tsindex bb7c986b0..079acd51b 100644--- a/apps/desktop/src/stores/slices/session-slice.ts+++ b/apps/desktop/src/stores/slices/session-slice.ts@@ -44,6 +44,7 @@ import {durableCoversLiveSessionMessages,mergeLiveSessionMessages,} from "../../lib/session-transcript";+import { sessionReadLooksEmpty } from "../../lib/session-transcript-read";import type {AppState,DraftSessionConfiguration,@@ -361,6 +362,32 @@ export function createSessionSlice({detail ??= await detailPromise;if (!runtime.navigationIntentIsCurrent(intent)) return;+ if (detail.session && sessionReadLooksEmpty(detail.session)) {+ // A window read that comes back empty for a session the sidebar+ // counts as having history is not an empty conversation (#795). Ask+ // once more, and if the transcript still reads empty keep whatever+ // the user already has and say so, instead of committing nothing and+ // leaving a blank pane behind.+ const reread = await runtime.loadSessionDetail(id, {+ messageLimit: 100,+ contentLimit: 64 * 1024,+ });+ if (!runtime.navigationIntentIsCurrent(intent)) return;+ if (reread.session && sessionReadLooksEmpty(reread.session)) {+ const retained =+ runtime.sessionTranscriptCache.get(id) ??+ get().retainedTranscripts[id];+ if (retained && retained.length > 0) {+ commitSelection(retained, true);+ } else {+ get().showToast(i18n.t("chat.sessionTranscriptEmpty"), {+ variant: "error",+ });+ }+ return;+ }+ detail = reread;+ }const historyWindow = detail.session? {messageStart: detail.session.messageStart ?? 0,diff --git a/apps/desktop/test/session-transcript-empty-read.test.mjs b/apps/desktop/test/session-transcript-empty-read.test.mjsnew file mode 100644index 000000000..aee50ebe7--- /dev/null+++ b/apps/desktop/test/session-transcript-empty-read.test.mjs@@ -0,0 +1,91 @@+/**+ * Issue #795 ③: a durable window read that reports "no messages" for a session+ * the sidebar counts as having history must not become the committed truth.+ *+ * The host answers `session.get` from a transcript file it may be rewriting, so+ * "the session exists, with no messages" is a legal looking answer for a+ * 2 700-message session. Caching it let the sidebar's hover prefetch re-serve+ * that emptiness on every later open, and the pane stayed blank until restart.+ */+import assert from "node:assert/strict";+import { register } from "node:module";+import test from "node:test";+import { readFile } from "node:fs/promises";+import { dirname, join } from "node:path";+import { fileURLToPath, pathToFileURL } from "node:url";++const here = dirname(fileURLToPath(import.meta.url));+register(pathToFileURL(join(here, "helpers/ts-import-hooks.mjs")));++const { sessionReadLooksEmpty } = await import(+ pathToFileURL(join(here, "../src/lib/session-transcript-read.ts")).href+);++const read = (path) => readFile(new URL(path, import.meta.url), "utf8");+const [sessionSlice, sessionRuntime, composerStyle] = await Promise.all([+ read("../src/stores/slices/session-slice.ts"),+ read("../src/stores/runtime/session-runtime.ts"),+ read("../src/lib/session-transcript-read.ts"),+]);++test("only a session whose own count agrees may be read as empty", () => {+ assert.equal(sessionReadLooksEmpty({ messages: [], messageCount: 2700 }), true);+ assert.equal(+ sessionReadLooksEmpty({ messages: [], messageCount: 1 }),+ true,+ "one counted message is still history",+ );+ assert.equal(+ sessionReadLooksEmpty({ messages: [], messageCount: 0 }),+ false,+ "a session that was never used may legitimately read empty",+ );+ assert.equal(+ sessionReadLooksEmpty({ messages: [{ id: "m1" }], messageCount: 0 }),+ false,+ );+ assert.equal(sessionReadLooksEmpty({ messages: undefined, messageCount: 4 }), true);+ assert.equal(sessionReadLooksEmpty(null), false);+ assert.equal(sessionReadLooksEmpty(undefined), false);+});++test("the empty read is retried once and then recovered instead of committed", () => {+ assert.match(composerStyle, /export function sessionReadLooksEmpty\(/);+ assert.match(+ sessionSlice,+ /sessionReadLooksEmpty\(detail\.session\)[\s\S]{0,600}?await runtime\.loadSessionDetail\(id, \{/,+ "an empty durable window must be read once more",+ );+ assert.match(+ sessionSlice,+ /sessionReadLooksEmpty\(reread\.session\)[\s\S]{0,500}?retained && retained\.length > 0[\s\S]{0,200}?commitSelection\(retained, true\)/,+ "a still-empty read keeps what the user already has",+ );+ assert.match(+ sessionSlice,+ /showToast\(i18n\.t\("chat\.sessionTranscriptEmpty"\)/,+ "a blank pane must stay diagnosable",+ );+ assert.doesNotMatch(+ sessionSlice,+ /commitSelection\(\[\], false/,+ "no path may commit an empty durable window as the active transcript",+ );+});++test("the durable-read cache never stores a suspiciously empty window", () => {+ assert.match(+ sessionRuntime,+ /if \(messages\.length > 0 \|\| !sessionReadLooksEmpty\(detail\.session\)\) \{\s*cacheSessionTranscript\(/,+ "hover prefetch shares this cache boundary and must not poison it",+ );+});++test("both shipped locales carry the diagnosable copy", async () => {+ const [en, zhCN] = await Promise.all([+ read("../../../packages/i18n/src/locales/en/index.ts"),+ read("../../../packages/i18n/src/locales/zh-CN/index.ts"),+ ]);+ assert.match(en, /sessionTranscriptEmpty:\s*"[^"]+"/);+ assert.match(zhCN, /sessionTranscriptEmpty:\s*"[^"]+"/);+});