Skip to content

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

fix(host-core): stop cutting off plugin and MCP tools at 60s

hxj04121-lab97437515ec79
diff --git a/apps/desktop/test/plugin-timeout-budgets.test.mjs b/apps/desktop/test/plugin-timeout-budgets.test.mjs
new file mode 100644
index 000000000..4d869f97a
--- /dev/null
+++ b/apps/desktop/test/plugin-timeout-budgets.test.mjs
@@ -0,0 +1,40 @@
+import assert from "node:assert/strict";
+import test from "node:test";
+import { readFileSync } from "node:fs";
+import { dirname, join } from "node:path";
+import { fileURLToPath } from "node:url";
+
+// A plugin tool call crosses four processes, and each layer has its own
+// deadline. Every inner budget must stay below the one around it, or the outer
+// layer times out first and the inner layer's own error never reaches the
+// model. The constants live in TypeScript and Rust, so this reads the sources.
+const repoRoot = join(dirname(fileURLToPath(import.meta.url)), "../../..");
+const source = (path) => readFileSync(join(repoRoot, path), "utf8");
+
+function constMs(src, name) {
+ const match = src.match(new RegExp(`const ${name}(?::\\s*u64)?\\s*=\\s*([\\d_]+)\\s*;`));
+ assert.ok(match, `${name} not found`);
+ return Number(match[1].replaceAll("_", ""));
+}
+
+const pluginRuntime = source("apps/desktop/electron/main/plugin-runtime.ts");
+const pluginMcp = source("apps/desktop/electron/main/plugin-mcp.ts");
+const sharedTimeouts = source("packages/shared/src/rpc-timeouts.ts");
+const hostTools = source("crates/host-core/src/tools/mod.rs");
+const hostPermissions = source("crates/host-core/src/permissions.rs");
+
+test("plugin completion and MCP calls fit inside the plugin tool budget", () => {
+ const tool = constMs(pluginRuntime, "PLUGIN_TOOL_TIMEOUT_MS");
+ assert.ok(constMs(pluginRuntime, "PLUGIN_COMPLETE_TIMEOUT_MS") < tool);
+ assert.ok(constMs(pluginMcp, "MCP_CALL_TIMEOUT_MS") < tool);
+});
+
+test("host-core dispatch outlasts the plugin tool budget and matches its TS mirror", () => {
+ const hostDispatch = constMs(hostTools, "DESKTOP_TOOL_DISPATCH_TIMEOUT_MS");
+ assert.ok(constMs(pluginRuntime, "PLUGIN_TOOL_TIMEOUT_MS") < hostDispatch);
+ assert.equal(constMs(sharedTimeouts, "DESKTOP_TOOL_DISPATCH_TIMEOUT_MS"), hostDispatch);
+ assert.equal(
+ constMs(sharedTimeouts, "PERMISSION_TIMEOUT_MS"),
+ constMs(hostPermissions, "PERMISSION_TIMEOUT_MS"),
+ );
+});
diff --git a/crates/host-core/src/rpc/mod.rs b/crates/host-core/src/rpc/mod.rs
index 670104912..2267fc10f 100644
--- a/crates/host-core/src/rpc/mod.rs
+++ b/crates/host-core/src/rpc/mod.rs
@@ -3719,13 +3719,14 @@ async fn handle_request(
}
let mut result = if tools::is_desktop_dispatched(&p.tool_name) {
- // Plugin dispatch keeps its existing bounded default timeout;
- // command-shell timeout semantics apply only to Bash.
+ // Plugin and MCP dispatch has its own bounded default, sized
+ // to outlast Electron's budgets; command-shell timeout
+ // semantics apply only to Bash.
execute_plugin_tool(
&state,
&tx,
&p,
- p.timeout_ms.unwrap_or(60_000),
+ tools::desktop_dispatch_timeout_ms(p.timeout_ms),
&durable_mode,
)
.await
diff --git a/crates/host-core/src/tools/mod.rs b/crates/host-core/src/tools/mod.rs
index 00c7a0202..2c786d198 100644
--- a/crates/host-core/src/tools/mod.rs
+++ b/crates/host-core/src/tools/mod.rs
@@ -963,6 +963,21 @@ pub fn is_desktop_dispatched(tool_name: &str) -> bool {
tool_name.starts_with("plugin_") || tool_name.starts_with("mcp_")
}
+/// How long host-core waits for Electron main to answer an approved
+/// desktop-dispatched tool. It must outlast Electron's own budgets (plugin
+/// tool 110s, MCP call 100s, plugin `agent.complete` 90s) so the innermost
+/// layer reports its own timeout instead of being cut off here
+/// (`07-plugins/12-plugin-ipc-and-host-services.md`, ADR 0038). Mirrored by
+/// `DESKTOP_TOOL_DISPATCH_TIMEOUT_MS` in `packages/shared/src/rpc-timeouts.ts`,
+/// which sizes the transport deadline around it.
+pub const DESKTOP_TOOL_DISPATCH_TIMEOUT_MS: u64 = 120_000;
+
+/// Dispatch deadline for a desktop-dispatched tool. The sidecar sends no
+/// `timeoutMs` for these tools, so the default is what normally applies.
+pub fn desktop_dispatch_timeout_ms(requested: Option<u64>) -> u64 {
+ requested.unwrap_or(DESKTOP_TOOL_DISPATCH_TIMEOUT_MS)
+}
+
#[cfg(test)]
pub async fn execute_tool(
workspace: Option<&Path>,
@@ -2821,6 +2836,20 @@ mod tests {
hashline::strip_write_markup(content)
}
+ #[test]
+ fn desktop_dispatch_outlasts_the_electron_plugin_tool_budget() {
+ // Electron main gives a plugin tool 110s (`PLUGIN_TOOL_TIMEOUT_MS`).
+ // host-core must not give up first, or a plugin that is still inside
+ // its own budget (e.g. an `agent.complete` call) gets TOOL_TIMEOUT.
+ const ELECTRON_PLUGIN_TOOL_TIMEOUT_MS: u64 = 110_000;
+ assert_eq!(
+ desktop_dispatch_timeout_ms(None),
+ DESKTOP_TOOL_DISPATCH_TIMEOUT_MS
+ );
+ assert!(desktop_dispatch_timeout_ms(None) > ELECTRON_PLUGIN_TOOL_TIMEOUT_MS);
+ assert_eq!(desktop_dispatch_timeout_ms(Some(5_000)), 5_000);
+ }
+
#[cfg(unix)]
#[tokio::test]
async fn grep_preserves_posix_literal_backslashes_in_workspace_paths() {
diff --git a/docs/spec/06-delivery/04-e2e-test-plan.md b/docs/spec/06-delivery/04-e2e-test-plan.md
index 4f2ed68d0..b866bb5da 100644
--- a/docs/spec/06-delivery/04-e2e-test-plan.md
+++ b/docs/spec/06-delivery/04-e2e-test-plan.md
@@ -13638,3 +13638,28 @@ the latest destination. These assertions measure work counts, not device FPS.
- **Status:** Automated for the executing native platform; run on macOS/Linux
runners for native qualification. Optional screenshots are written only to
`PI_DESKTOP_CHROME_ARTIFACT_DIR`.
+
+### E2E-PLUGIN-slow-tool-is-not-cut-off-by-host-dispatch
+
+- **Preconditions:** A loaded plugin that registers an agent tool; the tool
+ awaits work longer than 60s but shorter than the 110s plugin tool budget (for
+ example an `agent.complete` call that takes ~70s).
+- **Steps:** Ask the agent to use the tool, approve it if prompted, and wait for
+ the call to finish. Repeat with the permission card left open for a while
+ before approving.
+- **Expected:** The call returns the plugin's result. host-core does not answer
+ `TOOL_TIMEOUT` before Electron's plugin tool budget expires, and no transport
+ layer reports `host RPC timeout` or `sidecar RPC timeout` for the call. Budgets
+ nest from the inside out: plugin `agent.complete` 90s < MCP call 100s < plugin
+ tool 110s < host-core dispatch 120s, and the transport deadline covers the
+ permission wait plus that dispatch.
+- **Specs:** `07-plugins/12-plugin-ipc-and-host-services.md` agent-tool
+ dispatch; ADR 0038; ADR 0174.
+- **Acceptance:** G (plugin agent tool).
+- **Milestone:** Post-MVP regression coverage.
+- **Automation:** `apps/desktop/test/plugin-timeout-budgets.test.mjs` checks the
+ budget order across the TypeScript and Rust sources;
+ `packages/shared/src/protocol.test.ts` covers the transport deadline;
+ host-core `desktop_dispatch_outlasts_the_electron_plugin_tool_budget` covers
+ the dispatch default.
+- **Status:** Contract-covered; no end-to-end driver waits out a real 70s call.
diff --git a/docs/spec/07-plugins/12-plugin-ipc-and-host-services.md b/docs/spec/07-plugins/12-plugin-ipc-and-host-services.md
index a47fd4f2d..4e88975ea 100644
--- a/docs/spec/07-plugins/12-plugin-ipc-and-host-services.md
+++ b/docs/spec/07-plugins/12-plugin-ipc-and-host-services.md
@@ -237,8 +237,12 @@ permission gate and result envelope stay in host-core:
Electron main executing the registered plugin tool JS and answering via RPC
`plugins.resolveExecution` `{ executionId, ok, content, errorCode? }`.
4. host-core resolves the pending execution and returns a standard
- `ToolsExecuteResult` to the sidecar. Dispatch timeout maps to
- `TOOL_TIMEOUT`; an unknown/unloaded tool maps to `TOOL_NOT_FOUND`.
+ `ToolsExecuteResult` to the sidecar. Dispatch waits up to 120s
+ (`DESKTOP_TOOL_DISPATCH_TIMEOUT_MS`, above the 110s plugin tool budget) and
+ then maps to `TOOL_TIMEOUT`; an unknown/unloaded tool maps to
+ `TOOL_NOT_FOUND`. The transport deadline for these calls covers the
+ permission wait plus that dispatch plus 10s of slack (`rpcTimeoutMs`), so no
+ outer layer gives up before host-core reports the outcome.
The model-facing registry gains plugin tools per prompt: main passes registered
defs (`fullName`, description, JSON-schema parameters) to `agent.prompt`, and
diff --git a/docs/zh-CN/spec/06-delivery/04-e2e-test-plan.md b/docs/zh-CN/spec/06-delivery/04-e2e-test-plan.md
index 58371e5e8..f45a31897 100644
--- a/docs/zh-CN/spec/06-delivery/04-e2e-test-plan.md
+++ b/docs/zh-CN/spec/06-delivery/04-e2e-test-plan.md
@@ -8066,3 +8066,28 @@ the latest destination. These assertions measure work counts, not device FPS.
- **Status:** Automated for the executing native platform; run on macOS/Linux
runners for native qualification. Optional screenshots are written only to
`PI_DESKTOP_CHROME_ARTIFACT_DIR`.
+
+### E2E-PLUGIN-slow-tool-is-not-cut-off-by-host-dispatch
+
+- **Preconditions:** A loaded plugin that registers an agent tool; the tool
+ awaits work longer than 60s but shorter than the 110s plugin tool budget (for
+ example an `agent.complete` call that takes ~70s).
+- **Steps:** Ask the agent to use the tool, approve it if prompted, and wait for
+ the call to finish. Repeat with the permission card left open for a while
+ before approving.
+- **Expected:** The call returns the plugin's result. host-core does not answer
+ `TOOL_TIMEOUT` before Electron's plugin tool budget expires, and no transport
+ layer reports `host RPC timeout` or `sidecar RPC timeout` for the call. Budgets
+ nest from the inside out: plugin `agent.complete` 90s < MCP call 100s < plugin
+ tool 110s < host-core dispatch 120s, and the transport deadline covers the
+ permission wait plus that dispatch.
+- **Specs:** `07-plugins/12-plugin-ipc-and-host-services.md` agent-tool
+ dispatch; ADR 0038; ADR 0174.
+- **Acceptance:** G (plugin agent tool).
+- **Milestone:** Post-MVP regression coverage.
+- **Automation:** `apps/desktop/test/plugin-timeout-budgets.test.mjs` checks the
+ budget order across the TypeScript and Rust sources;
+ `packages/shared/src/protocol.test.ts` covers the transport deadline;
+ host-core `desktop_dispatch_outlasts_the_electron_plugin_tool_budget` covers
+ the dispatch default.
+- **Status:** Contract-covered; no end-to-end driver waits out a real 70s call.
diff --git a/docs/zh-CN/spec/07-plugins/12-plugin-ipc-and-host-services.md b/docs/zh-CN/spec/07-plugins/12-plugin-ipc-and-host-services.md
index 07ed33e0c..9a5370aef 100644
--- a/docs/zh-CN/spec/07-plugins/12-plugin-ipc-and-host-services.md
+++ b/docs/zh-CN/spec/07-plugins/12-plugin-ipc-and-host-services.md
@@ -212,8 +212,11 @@ toast 加上 `pluginChanged` 到渲染器。
Electron主要执行注册的插件工具JS并通过RPC应答
`plugins.resolveExecution` `{ executionId, ok, content, errorCode? }`。
4. host-core 解析待执行并返回一个标准
- `ToolsExecuteResult` 到 sidecar。调度超时映射到
- `TOOL_TIMEOUT`; unknown/unloaded 工具映射到 `TOOL_NOT_FOUND`。
+ `ToolsExecuteResult` 到 sidecar。调度最多等待 120 秒
+ (`DESKTOP_TOOL_DISPATCH_TIMEOUT_MS`,高于 110 秒的插件工具预算),超时映射到
+ `TOOL_TIMEOUT`; unknown/unloaded 工具映射到 `TOOL_NOT_FOUND`。这类调用的传输
+ 截止时间覆盖权限等待、上述调度和 10 秒余量(`rpcTimeoutMs`),因此外层不会在
+ host-core 报告结果之前先放弃。
面向模型的注册表根据提示获得插件工具:已注册主要通道
defs(`fullName`、描述、JSON 架构参数)到 `agent.prompt`,以及
diff --git a/packages/shared/src/protocol.test.ts b/packages/shared/src/protocol.test.ts
index f108e3484..a2c7bc149 100644
--- a/packages/shared/src/protocol.test.ts
+++ b/packages/shared/src/protocol.test.ts
@@ -241,4 +241,24 @@ describe("Plan protocol contracts", () => {
130_000,
);
});
+
+ it("covers the permission wait and host-core dispatch for plugin and MCP tools", () => {
+ // Permission (120s) + host-core dispatch (120s) + slack (10s). A flat 130s
+ // would cut off a prompted plugin tool that is still inside its budget.
+ expect(rpcTimeoutMs("tools.execute", { toolName: "plugin_advisor_ask" })).toBe(
+ 250_000,
+ );
+ expect(rpcTimeoutMs("tools.execute", { toolName: "mcp_github_search" })).toBe(
+ 250_000,
+ );
+ expect(
+ rpcTimeoutMs("tools.execute", {
+ toolName: "plugin_advisor_ask",
+ timeoutMs: 5_000,
+ }),
+ ).toBe(135_000);
+ expect(
+ rpcTimeoutMs("tools.execute", { toolName: "plugin_advisor_ask", timeoutMs: 0 }),
+ ).toBe(250_000);
+ });
});
diff --git a/packages/shared/src/rpc-timeouts.ts b/packages/shared/src/rpc-timeouts.ts
index 25ec7125a..7e6baccdf 100644
--- a/packages/shared/src/rpc-timeouts.ts
+++ b/packages/shared/src/rpc-timeouts.ts
@@ -4,16 +4,33 @@ export const DEFAULT_COMMAND_TIMEOUT_MS = 60_000;
export const COMMAND_RPC_BUFFER_MS = 10_000;
export const DEFAULT_BASH_RPC_TIMEOUT_MS =
PERMISSION_TIMEOUT_MS + DEFAULT_COMMAND_TIMEOUT_MS + COMMAND_RPC_BUFFER_MS;
+/**
+ * host-core's dispatch budget for `plugin_*` / `mcp_*` tools, which Electron
+ * main executes. It outlasts Electron's plugin tool budget (110s). Mirrors
+ * `DESKTOP_TOOL_DISPATCH_TIMEOUT_MS` in `crates/host-core/src/tools/mod.rs`.
+ */
+export const DESKTOP_TOOL_DISPATCH_TIMEOUT_MS = 120_000;
+export const DEFAULT_DESKTOP_TOOL_RPC_TIMEOUT_MS =
+ PERMISSION_TIMEOUT_MS + DESKTOP_TOOL_DISPATCH_TIMEOUT_MS + COMMAND_RPC_BUFFER_MS;
const MAX_TIMER_DELAY_MS = 2_147_483_647;
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
+function isDesktopDispatchedTool(toolName: unknown): boolean {
+ return (
+ typeof toolName === "string" &&
+ (toolName.startsWith("plugin_") || toolName.startsWith("mcp_"))
+ );
+}
+
/**
- * Return the transport deadline for an RPC call. Bash includes the permission
- * wait, the effective command timeout, and transport slack. Every call has a
- * finite deadline so a lost response cannot leave a pending promise forever.
+ * Return the transport deadline for an RPC call. Bash and desktop-dispatched
+ * (`plugin_*` / `mcp_*`) tools include the permission wait, the effective
+ * execution timeout, and transport slack, so the transport never gives up
+ * before host-core reports its own timeout. Every call has a finite deadline so
+ * a lost response cannot leave a pending promise forever.
*/
export function rpcTimeoutMs(
method: string,
@@ -22,20 +39,26 @@ export function rpcTimeoutMs(
if (method !== "tools.execute") return DEFAULT_RPC_TIMEOUT_MS;
const input = isRecord(params) ? params : undefined;
+ if (isDesktopDispatchedTool(input?.toolName)) {
+ return executionRpcTimeoutMs(input?.timeoutMs, DEFAULT_DESKTOP_TOOL_RPC_TIMEOUT_MS);
+ }
if (input?.toolName !== "Bash") return DEFAULT_RPC_TIMEOUT_MS;
if (input?.timeoutMs === undefined) return DEFAULT_BASH_RPC_TIMEOUT_MS;
+ return executionRpcTimeoutMs(input.timeoutMs, DEFAULT_BASH_RPC_TIMEOUT_MS);
+}
- const commandTimeoutMs = input.timeoutMs;
+/** Permission wait + execution timeout + slack, or `fallbackMs` when absent or invalid. */
+function executionRpcTimeoutMs(executionTimeoutMs: unknown, fallbackMs: number): number {
if (
- typeof commandTimeoutMs !== "number" ||
- !Number.isFinite(commandTimeoutMs) ||
- commandTimeoutMs <= 0
+ typeof executionTimeoutMs !== "number" ||
+ !Number.isFinite(executionTimeoutMs) ||
+ executionTimeoutMs <= 0
) {
- return DEFAULT_BASH_RPC_TIMEOUT_MS;
+ return fallbackMs;
}
return Math.min(
MAX_TIMER_DELAY_MS,
- PERMISSION_TIMEOUT_MS + Math.ceil(commandTimeoutMs) + COMMAND_RPC_BUFFER_MS,
+ PERMISSION_TIMEOUT_MS + Math.ceil(executionTimeoutMs) + COMMAND_RPC_BUFFER_MS,
);
}