Skip to content

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

fix(agent-runtime): interrupt canceled TaskStop waits

cauliflowerd5e81b94c1a0
diff --git a/docs/spec/03-runtime/02-agent-runtime.md b/docs/spec/03-runtime/02-agent-runtime.md
index 7c8847596..7a9cf949e 100644
--- a/docs/spec/03-runtime/02-agent-runtime.md
+++ b/docs/spec/03-runtime/02-agent-runtime.md
@@ -2031,7 +2031,9 @@ Reads are repeatable and do not consume desktop output. The wait defaults to
(completed/timeout/interrupted), independently of the job's state. New input
interrupts the wait; cancelling the parent releases it without stopping Bash.
TaskStop returns completed or stopPending based on the actual snapshot. A
-stopRequested flag alone does not confirm termination.
+stopRequested flag alone does not confirm termination. Cancelling TaskStop
+before dispatch leaves the job alone. After dispatch, cancellation releases
+the caller's wait while Host continues processing the stop request.
The wait deadline includes capability discovery and in-flight requests.
On timeout the tool returns its last observed snapshot. If the first
diff --git a/docs/spec/06-delivery/04-e2e-test-plan.md b/docs/spec/06-delivery/04-e2e-test-plan.md
index 5ee166e85..b26c37934 100644
--- a/docs/spec/06-delivery/04-e2e-test-plan.md
+++ b/docs/spec/06-delivery/04-e2e-test-plan.md
@@ -20491,6 +20491,9 @@ Scenario: E2E-REVIEW-ROUNDS (local desktop, implemented)
### E2E-TOOLS-session-background-jobs
+- Split follow-up regression: cancel TaskStop during capability discovery (zero stop requests) and after dispatch (one stop request); late RPC failure stays observed.
+
+
- Entry: build shared/agent-runtime/host-runtime and Host, then run
`PI_DESKTOP_HOST_BIN=<candidate-host> node scripts/e2e-session-background-jobs.mjs`.
- Environment: isolated Host data and workspace, production runtime and pi tool
diff --git a/docs/zh-CN/spec/03-runtime/02-agent-runtime.md b/docs/zh-CN/spec/03-runtime/02-agent-runtime.md
index 84014ad7c..7e7f79d70 100644
--- a/docs/zh-CN/spec/03-runtime/02-agent-runtime.md
+++ b/docs/zh-CN/spec/03-runtime/02-agent-runtime.md
@@ -1333,7 +1333,7 @@ Agent 模式下,即使没有配置子代理,也提供 TaskWait、TaskList
TaskWait/TaskStop 接受单个显式 job,与 delegationIds、mode、minCompleted 混用会报错。无 job 时保留原委派默认集合。旧 backgroundTaskId 可写成 `{kind: "bash", id: backgroundTaskId}`。
-Bash 等待返回状态、可空退出码、停止请求标记及分别最多 8 KiB 的 stdout/stderr 尾部,按 UTF-8 边界截断。truncated 同时表示 Host 历史丢失或本次截断。查询可重复且不消耗桌面日志。等待默认 600 秒,接受 1–900 秒;details.status 为 completed、timeout 或 interrupted,与作业状态独立。新输入或父回合取消只结束等待。停止返回 completed 或 stopPending,stopRequested 本身不代表进程已退出。
+Bash 等待返回状态、可空退出码、停止请求标记及分别最多 8 KiB 的 stdout/stderr 尾部,按 UTF-8 边界截断。truncated 同时表示 Host 历史丢失或本次截断。查询可重复且不消耗桌面日志。等待默认 600 秒,接受 1–900 秒;details.status 为 completed、timeout 或 interrupted,与作业状态独立。新输入或父回合取消只结束等待。停止返回 completed 或 stopPending,stopRequested 本身不代表进程已退出。TaskStop 在派发前取消时,作业继续运行;派发后取消只结束调用者的等待,Host 继续处理停止请求。
Bash 由 Host 管理,完成后不自动投递结果或延长父回合。原委派报告交付和恢复规则继续生效。能力按连接探测;旧 Host 和远程工作区明确返回后台 Bash 控制不可用,委派操作仍可用。其他传输或 Host 错误会正常呈现。
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 7e7698b2b..e44d541f5 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
@@ -10474,6 +10474,9 @@ Electron 一侧——工具自动签发、指示器、"停止"中止轮次、绘
### E2E-TOOLS-session-background-jobs
+- 拆分修复回归:TaskStop 在能力探测时取消不派发停止请求,派发后取消不重复停止,迟到的 RPC 错误仍被观察。
+
+
- 构建 shared、agent-runtime、host-runtime 与 Host 后,运行 `PI_DESKTOP_HOST_BIN=<candidate-host> node scripts/e2e-session-background-jobs.mjs`。
- 使用隔离 Host 数据、工作目录、生产 runtime/pi 循环与本地 HTTP 模型夹具。
- 后台启动测试,列举并等待失败结果;启动常驻命令,等待超时、无参停止,再显式停止并等待。
diff --git a/packages/agent-runtime/src/background-job-tools.test.ts b/packages/agent-runtime/src/background-job-tools.test.ts
index 989f18348..5c13a1f75 100644
--- a/packages/agent-runtime/src/background-job-tools.test.ts
+++ b/packages/agent-runtime/src/background-job-tools.test.ts
@@ -106,6 +106,28 @@ describe("session job controls", () => {
expect(dispatch.mock.calls.map(call => call[0])).not.toContain("backgroundJobs.stop");
});
+ it.each(["capabilities", "stop"])("abandons a pending %s RPC without dispatching stop again", async stalled => {
+ const { stop, dispatch, snapshot } = fixture();
+ let finishRpc!: () => void;
+ dispatch.mockImplementation(async method => method.endsWith(stalled)
+ ? new Promise((resolve, reject) => {
+ finishRpc = stalled === "capabilities"
+ ? () => resolve({ version: 1, supported: true })
+ : () => reject(new Error("late transport failure"));
+ })
+ : { version: 1, supported: true });
+ const controller = new AbortController();
+ const stopping = stop.execute("stop", { job: snapshot.job }, controller.signal);
+ const assertion = expect(stopping).rejects.toMatchObject({ errorCode: "TOOL_ABORTED" });
+ await vi.waitFor(() => expect(dispatch).toHaveBeenCalledWith(`backgroundJobs.${stalled}`, expect.anything()));
+ controller.abort();
+ await assertion;
+ finishRpc();
+ await new Promise<void>(resolve => setImmediate(resolve));
+ expect(dispatch.mock.calls.map(([method]) => method)).toEqual(stalled === "capabilities"
+ ? ["backgroundJobs.capabilities"] : ["backgroundJobs.capabilities", "backgroundJobs.stop"]);
+ });
+
it("abandons an in-flight wait promptly without stopping, and observes a late failure", async () => {
const { wait, dispatch, snapshot } = fixture();
let fail: (error: Error) => void = () => {};
diff --git a/packages/agent-runtime/src/background-job-tools.ts b/packages/agent-runtime/src/background-job-tools.ts
index c8ff83eef..0884a970c 100644
--- a/packages/agent-runtime/src/background-job-tools.ts
+++ b/packages/agent-runtime/src/background-job-tools.ts
@@ -166,9 +166,9 @@ export function buildJobTools(
if (job.kind === "delegation") {
return delegates.stop.execute(id, { delegationIds: [job.id] }, signal, update);
}
- await requireSupported();
+ await abortable(requireSupported(), signal);
if (signal?.aborted) throw Object.assign(new Error("Job stop aborted before dispatch"), { errorCode: "TOOL_ABORTED" });
- const snapshot = await call<BackgroundJobSnapshot>("backgroundJobs.stop", { id: job.id, timeoutMs: 1000 });
+ const snapshot = await abortable(call<BackgroundJobSnapshot>("backgroundJobs.stop", { id: job.id, timeoutMs: 1000 }), signal);
return observation(snapshot, snapshot.state === "running" ? "stopPending" : "completed");
},
};