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(persistence): drop empty assistant rows regardless of status
liubai36c1d159d4ac
diff --git a/apps/desktop/electron/main/runtime/event-persistence.ts b/apps/desktop/electron/main/runtime/event-persistence.tsindex e8cf88d27..f24ac500e 100644--- a/apps/desktop/electron/main/runtime/event-persistence.ts+++ b/apps/desktop/electron/main/runtime/event-persistence.ts@@ -327,16 +327,19 @@ function persistAgentEvent(envelope: AgentEventEnvelope): UiMessage | undefinedinflightCheckpointer.settleIf(sessionId, finalId);});}- // Empty aborted bubbles are not useful transcript rows. Structured- // provider failures remain durable assistant messages so their details- // stay attached to the failed turn after reload.- const failed =- event.message.status === "error" || event.message.status === "aborted";+ // Empty assistant bubbles are not useful transcript rows, whatever their+ // status: an empty `complete` message would otherwise be persisted on every+ // provider retry and grow the request body. Structured provider failures+ // remain durable so their details stay attached to the failed turn after+ // reload; a message that still carries a tool row keeps its link to it, and+ // a steering reply is reserved by the user turn that accepted it.const empty =!(event.message.content || "").trim() &&!(event.message.thinking || "").trim();+ const carriesTool =+ Boolean(event.message.toolName) || Boolean(event.message.toolCallId);const reservedForSteering = steeringReplies.delete(event.message.id);- if (failed && empty && !event.message.error && !reservedForSteering) return;+ if (empty && !carriesTool && !event.message.error && !reservedForSteering) return;void persistenceOutbox.enqueue({diff --git a/apps/desktop/test/empty-assistant-persistence.test.mjs b/apps/desktop/test/empty-assistant-persistence.test.mjsnew file mode 100644index 000000000..466a084e1--- /dev/null+++ b/apps/desktop/test/empty-assistant-persistence.test.mjs@@ -0,0 +1,128 @@+import assert from "node:assert/strict";+import { register } from "node:module";+import { dirname, join } from "node:path";+import { fileURLToPath, pathToFileURL } from "node:url";+import test from "node:test";++const here = dirname(fileURLToPath(import.meta.url));+register(pathToFileURL(join(here, "helpers/ts-import-hooks.mjs")));++// The real persistence pass, driven directly with a stubbed outbox.+const { createSessionCoordination } = await import(+ "../electron/main/runtime/session-coordination.ts"+);+const { createEventPersistence } = await import(+ "../electron/main/runtime/event-persistence.ts"+);++const SESSION = "s1";+const TURN = "turn-1";++function fixture() {+ const activeTurns = new Map([[SESSION, TURN]]);+ const coordination = createSessionCoordination({+ activeTurns,+ getMainWindow: () => null,+ getViewingSessionId: () => null,+ });+ const enqueued = [];+ const steeringReplies = new Set();+ const persistence = createEventPersistence({+ runtimeState: { host: null },+ steeringReplies,+ activeTurns,+ activeToolCalls: new Map(),+ activeToolCallKey: coordination.activeToolCallKey,+ approvedExecutionIdsBySession: new Map(),+ approvedExecutionTurns: new Map(),+ pendingExecutionFinishes: new Map(),+ planSubmissionTurnIds: new Set(),+ planSubmissionTurnKey: coordination.planSubmissionTurnKey,+ inflightCheckpointer: {+ observe() {},+ flush: async () => {},+ settle() {},+ settleIf() {},+ },+ persistenceOutbox: {+ size: () => 0,+ enqueue: async (entry) => {+ enqueued.push(entry);+ },+ flush: async () => undefined,+ },+ addActiveTurnUsage() {},+ logger: { app() {} },+ finishTurn: () => Promise.resolve(),+ isStaleTerminalEvent: coordination.isStaleTerminalEvent,+ transcriptRewrite: coordination,+ finishApprovedExecution: async () => undefined,+ emitAgentEvent() {},+ });+ const persistAssistant = (message) =>+ persistence.persistAgentEvent({+ sessionId: SESSION,+ turnId: TURN,+ ts: 1,+ event: {+ type: "message_end",+ message: {+ role: "assistant",+ createdAt: "2026-10-20T00:00:00.000Z",+ ...message,+ },+ },+ });+ return { enqueued, steeringReplies, persistAssistant };+}++test("an empty complete assistant message is not persisted", () => {+ const f = fixture();+ f.persistAssistant({ id: "m1", content: "", thinking: "", status: "complete" });+ assert.equal(f.enqueued.length, 0);+});++test("an empty aborted assistant message without an error is not persisted", () => {+ const f = fixture();+ f.persistAssistant({ id: "m2", content: "", thinking: "", status: "aborted" });+ assert.equal(f.enqueued.length, 0);+});++test("an empty assistant message that still carries a tool row is persisted", () => {+ const f = fixture();+ f.persistAssistant({+ id: "m3",+ content: "",+ thinking: "",+ status: "complete",+ toolName: "Grep",+ toolCallId: "call-1",+ toolStatus: "success",+ });+ assert.equal(f.enqueued.length, 1);+});++test("an empty assistant message with an error payload stays durable", () => {+ const f = fixture();+ f.persistAssistant({+ id: "m4",+ content: "",+ thinking: "",+ status: "error",+ error: { code: "PROVIDER_ERROR", message: "boom" },+ });+ assert.equal(f.enqueued.length, 1);+});++test("an empty assistant message reserved for steering is persisted", () => {+ const f = fixture();+ f.steeringReplies.add("m5");+ f.persistAssistant({ id: "m5", content: "", thinking: "", status: "complete" });+ assert.equal(f.enqueued.length, 1);+});++test("a non-empty complete assistant message is persisted", () => {+ const f = fixture();+ f.persistAssistant({ id: "m6", content: "done", status: "complete" });+ assert.equal(f.enqueued.length, 1);+});