Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

fix(desktop): drop orphaned FK outbox entries and cap sidecar heap (#1006)

Lan03e03d4cc172
diff --git a/apps/desktop/electron/main/agent-sidecar.ts b/apps/desktop/electron/main/agent-sidecar.ts
index 0f97f3cc1..d30a403c3 100644
--- a/apps/desktop/electron/main/agent-sidecar.ts
+++ b/apps/desktop/electron/main/agent-sidecar.ts
@@ -54,7 +54,7 @@ export class AgentSidecar extends RuntimeAgentSidecar {
command: process.execPath,
// Electron 43's Node supports the OS trust store. Keep bundled roots
// and inherited NODE_EXTRA_CA_CERTS; never bypass TLS verification.
- args: ["--use-system-ca", resolveSidecarEntry()],
+ args: ["--max-old-space-size=2048", "--use-system-ca", resolveSidecarEntry()],
env: {
...process.env,
ELECTRON_RUN_AS_NODE: "1",
diff --git a/apps/desktop/electron/main/persistence-outbox.ts b/apps/desktop/electron/main/persistence-outbox.ts
index b5358723d..f471aba48 100644
--- a/apps/desktop/electron/main/persistence-outbox.ts
+++ b/apps/desktop/electron/main/persistence-outbox.ts
@@ -110,6 +110,15 @@ export class PersistenceOutbox {
key: current.key,
data: String(error),
});
+ } else if (isForeignKeyError(error)) {
+ // The parent row (session or turn) was lost — typically after a
+ // sidecar crash that restarted before the outbox could drain.
+ // The host will never accept a child row whose parent is gone,
+ // so drop it and keep draining (#996).
+ this.logger("warn", "session persistence flush dropped orphaned message", {
+ key: current.key,
+ data: String(error),
+ });
} else {
// Transient failure (host busy/overloaded/pipe dead). Keep the head
// and retry on the next enqueue.
@@ -181,3 +190,12 @@ function isDuplicateMessageIdError(error: unknown): boolean {
function isPoisonMessageError(error: unknown): boolean {
return /(?<![A-Z_])PERMISSION_DENIED:/i.test(String(error));
}
+
+/**
+ * The parent row (session or turn) no longer exists. This happens after a
+ * sidecar or host-core crash where the parent was deleted or never committed.
+ * Retrying is pointless — the FK will never be satisfied.
+ */
+function isForeignKeyError(error: unknown): boolean {
+ return /FOREIGN KEY constraint failed/i.test(String(error));
+}
diff --git a/apps/desktop/test/persistence-outbox.test.mjs b/apps/desktop/test/persistence-outbox.test.mjs
index cbe738fb2..72eaede49 100644
--- a/apps/desktop/test/persistence-outbox.test.mjs
+++ b/apps/desktop/test/persistence-outbox.test.mjs
@@ -213,3 +213,42 @@ test("PLUGIN_PERMISSION_DENIED is not poison and still pauses the outbox (D597)"
assert.equal(outbox.size(), 2);
});
+
+test("FOREIGN KEY constraint failed drops the orphaned entry and keeps draining (#996)", async () => {
+ const dir = await mkdtemp(join(tmpdir(), "pi-outbox-"));
+ const logs = [];
+ const outbox = new PersistenceOutbox(dir, (level, message, data) => {
+ logs.push({ level, message, data });
+ });
+ const calls = [];
+ const host = mockHost(async (_method, params) => {
+ calls.push(params);
+ if (params.message.id === "orphaned-child") {
+ throw new Error("Error: FOREIGN KEY constraint failed");
+ }
+ });
+ const getHost = () => host;
+ await outbox.enqueue(
+ {
+ key: "message:s1:orphaned-child",
+ sessionId: "s1",
+ message: { id: "orphaned-child" },
+ },
+ getHost,
+ );
+ await outbox.enqueue(
+ {
+ key: "message:s2:healthy",
+ sessionId: "s2",
+ message: { id: "healthy" },
+ },
+ getHost,
+ );
+ await outbox.flush(getHost);
+ assert.equal(outbox.size(), 0, "both entries should be drained");
+ assert.equal(calls.length, 2);
+ assert.equal(calls[1].message.id, "healthy");
+ assert.ok(
+ logs.some((row) => row.message === "session persistence flush dropped orphaned message"),
+ );
+});