Skip to content

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

fix(desktop): retry the initial host handshake once

Linuxdazhaof3a8828ad061
diff --git a/apps/desktop/electron/main/runtime/lifecycle.ts b/apps/desktop/electron/main/runtime/lifecycle.ts
index b8bd6ac80..ef40ac633 100644
--- a/apps/desktop/electron/main/runtime/lifecycle.ts
+++ b/apps/desktop/electron/main/runtime/lifecycle.ts
@@ -219,7 +219,15 @@ export function createRuntimeLifecycle({
logger.app("lifecycle", "info", "app boot " + APP_NAME + " " + APP_VERSION, {
data: { protocolVersion: PROTOCOL_VERSION },
});
- await startHost();
+ try {
+ await startHost();
+ } catch (error) {
+ if (isQuitting() || !(error instanceof Error) ||
+ error.message !== "host RPC timeout: app.handshake") throw error;
+ // startHost disposes the timed-out child before rejecting.
+ logger.app("runtime", "warn", "retrying host startup after handshake timeout");
+ await startHost();
+ }
// Plugin rows are resolved in the host, so the app language is pushed once
// the process answers. The startup sequence re-applies it with the stored
// settings; this keeps a host that boots without that write honest.
diff --git a/apps/desktop/test/startup-host-retry.test.mjs b/apps/desktop/test/startup-host-retry.test.mjs
new file mode 100644
index 000000000..406a475cc
--- /dev/null
+++ b/apps/desktop/test/startup-host-retry.test.mjs
@@ -0,0 +1,64 @@
+import assert from "node:assert/strict";
+import { readFile } from "node:fs/promises";
+import test from "node:test";
+import ts from "typescript";
+
+const source = await readFile(new URL("../electron/main/runtime/lifecycle.ts", import.meta.url), "utf8");
+const ast = ts.createSourceFile("lifecycle.ts", source, ts.ScriptTarget.Latest, true);
+const lifecycle = ast.statements.find(
+ (node) => ts.isFunctionDeclaration(node) && node.name?.text === "createRuntimeLifecycle",
+);
+const boot = lifecycle.body.statements
+ .filter(ts.isVariableStatement)
+ .flatMap((node) => [...node.declarationList.declarations])
+ .find((node) => node.name.getText(ast) === "bootBackends");
+const compiled = ts.transpileModule(`const ${boot.getText(ast)};\nreturn bootBackends;`, {
+ compilerOptions: { target: ts.ScriptTarget.ES2022 },
+}).outputText;
+
+const timeout = new Error("host RPC timeout: app.handshake");
+const sidecarBoundary = new Error("fixture ends after the startup boundary");
+
+function startupFixture(startFailure, isQuitting = () => false) {
+ const events = [];
+ let attempts = 0;
+ const deps = {
+ mkdirSync() {},
+ join() {},
+ dataDir: "/fixture",
+ logger: { app() {} },
+ APP_NAME: "PI-Desktop",
+ APP_VERSION: "fixture",
+ PROTOCOL_VERSION: 11,
+ runtimeState: { host: { async call() { return {}; } } },
+ async startHost() {
+ events.push("host");
+ const error = startFailure(++attempts);
+ if (error) throw error;
+ },
+ isQuitting,
+ async syncPluginDisplayLocale() {},
+ getDisplayLocale: () => "en",
+ async applyNetworkProxyFromAppSettings() {},
+ async startSidecar() {
+ events.push("sidecar");
+ throw sidecarBoundary;
+ },
+ };
+ return { events, run: new Function(...Object.keys(deps), compiled)(...Object.values(deps)) };
+}
+
+test("cold desktop boot retries the Host once before starting the sidecar", async () => {
+ const fixture = startupFixture((attempt) => attempt === 1 ? timeout : undefined);
+ await assert.rejects(fixture.run(), sidecarBoundary);
+ assert.deepEqual(fixture.events, ["host", "host", "sidecar"]);
+});
+
+test("cold desktop boot stops after a second timeout, during quit or on other startup errors", async () => {
+ const schemaError = new Error("database schema is newer than this build");
+ for (const [error, quitting, attempts] of [[timeout, false, 2], [timeout, true, 1], [schemaError, false, 1]]) {
+ const fixture = startupFixture(() => error, () => quitting);
+ await assert.rejects(fixture.run(), error);
+ assert.deepEqual(fixture.events, Array(attempts).fill("host"));
+ }
+});
diff --git a/docs/spec/03-runtime/07-process-model.md b/docs/spec/03-runtime/07-process-model.md
index a4e65cba3..58c560baf 100644
--- a/docs/spec/03-runtime/07-process-model.md
+++ b/docs/spec/03-runtime/07-process-model.md
@@ -53,8 +53,10 @@ E2E harnesses point a build at a throwaway profile with it.
7. Create main window / renderer
8. Renderer performs `app/getVersion` healthcheck through main
-If step 3–4 fails: block app with recovery message. Before a successful host
-boot serves RPC, host-core transactionally marks prior pending approvals and
+If the first `app.handshake` times out, Main disposes the failed child and
+starts a fresh one once in the same launch. Shutdown and other startup errors
+bypass this retry. If startup still fails, show the recovery surface. Before
+a successful host boot serves RPC, host-core transactionally marks prior pending approvals and
queued/running `plan_approvals` execution states interrupted and aborts their
running turns. This internal process-epoch fence is not serialized or sent over
the protocol.
diff --git a/docs/spec/06-delivery/04-e2e-test-plan.md b/docs/spec/06-delivery/04-e2e-test-plan.md
index 74661377d..7587a1d17 100644
--- a/docs/spec/06-delivery/04-e2e-test-plan.md
+++ b/docs/spec/06-delivery/04-e2e-test-plan.md
@@ -1455,6 +1455,12 @@ identify the platform validation still needed.
#### E2E-003: Rust host healthcheck responds
- **Preconditions**: App is running; Rust host-core sidecar started.
+- **Cold-start timeout**: Let the first handshake time out, then allow a fresh
+ host to answer. Verify the shell opens in the same desktop process. Repeat
+ with both handshakes timing out and with quit during the first attempt.
+ Verify the recovery surface stays available after two attempts and quit
+ prevents a second child. The startup-host-retry tests inject these outcomes
+ at the Main lifecycle boundary.
- **Steps**: 1) Electron handshakes with protocol version 11. 2) Call the host
healthcheck RPC. 3) Repeat boot with mismatched older and newer protocol
fixtures.