2026-07-04-background-task-control-parity.md 8.9 KB

Background Task Control Implementation Plan

Status: implemented and archived. This file records the final scope, phase outcomes, verification commands, and intentionally deferred capabilities for future work.

Goal

为已经进入 background 的 local_bash 和 local_agent 提供用户可触发的 stop/cancel 控制能力,并实现 provider-visible TaskStop tool。实现必须复用现有 RuntimeTaskRegistry、background notification、runtime command queue、session projection 和 session/cancelBackgroundTask 链路,不新增第二套 background manager、poller、wake 或 UI stop path。

Final Architecture

  • Runtime 内有 registry-first background task stop dispatcher。
  • TaskStop tool 和 UI/session cancel 共用同一个 dispatcher。
  • local_bash stop 分发到 ExecutionPort.cancelBackgroundTask(taskId)。
  • local_agent stop 分发到 SubagentPort.stopTask(taskId)。
  • TaskStop.command 语义:local_bash 使用实际命令,local_agent 使用短 description;完整 subagent prompt 不得进入 stop result。
  • terminal 状态继续走 registry terminal update -> task notification -> runtime command queue。
  • UI 只保留 SummaryPanel 这一处 background task control surface。
  • AgentToolCallBlock 只负责 background Agent 详情展示,不复制 stop 请求逻辑。
  • stop RPC ACK 只表示 runtime 接收了停止请求;UI stop pending 会等 background task control 状态不再 active 后再释放。

Code Anchors

  • TaskStop contract: apps/zcode-cli/packages/contracts/src/tools/task-stop.ts
  • TaskStop model-facing handler: apps/zcode-cli/packages/core/src/tool/handlers/task-stop.ts
  • Runtime background stop dispatch: apps/zcode-cli/packages/core/src/runtime/methods/background.ts
  • UI/session cancel protocol: session/cancelBackgroundTask in packages/shared/src/zcode-protocol/index.ts
  • Bash task stop: apps/zcode-cli/packages/adapters/src/exec/node-execution-adapter-lifecycle.ts
  • Agent task stop: apps/zcode-cli/packages/core/src/subagent/runner.ts

Completed Phases

Phase 0 - Spec and Baseline Guard

  • Fixed scope to already-background local_bash / local_agent stop control plus provider-visible TaskStop.
  • Kept foreground-to-background controls, TaskOutput, and non-shared task types out of this round.
  • Recorded code anchors and current ZCode starting points.

Phase 1 - Runtime Stop Dispatcher

  • Added registry-first runtime background task stop dispatch.
  • Preserved idempotent UI/session behavior for missing or already-terminal tasks.
  • Added strict/tool behavior for TaskStop validation errors.
  • Kept dispatcher free of new queue, poller, timer, or provider-visible notification generation.

Phase 2 - Subagent Stop Handler

  • Added SubagentPort.stopTask() for running background local_agent.
  • Background Agent stop uses the saved lifecycle controller and writes terminal state through the same background completion path.
  • Stop does not mutate UI projection as the source of truth.
  • Existing launch/run/start/backgroundTask/sendMessage behavior remains owned by the subagent runtime.

Phase 3 - Terminal Idempotency and Notification Ordering

  • Stop and natural completion are terminal-idempotent for Bash and Agent.
  • notified is only marked after notification enqueue succeeds.
  • Background terminal order remains registry terminal update -> enqueue notification -> async event/projection.
  • Goal defer continues to read RuntimeTaskRegistry running background tasks rather than UI projection.

Phase 4 - Provider-visible TaskStop Tool

  • Added TaskStop contract and handler.
  • Input supports task_id and deprecated shell_id alias.
  • Validation returns distinct errors for missing id, not found task, and not-running task.
  • Result is JSON-compatible and does not expose internal snapshots/projections.
  • Result command 对 local_agent 取 runtime task 的短 description,成功消息括号内复用同一值,不暴露完整 prompt。
  • KillShell / KillBash aliases are supported through runtime lookup without becoming provider-visible tools.
  • Default child runtimes can expose TaskStop for runtime-local background tasks; custom profiles still use existing allow/disallow filtering.

Phase 5 - Protocol and UI Control Surface

  • Kept session/cancelBackgroundTask protocol name to avoid protocol churn.
  • Added generic background task control item model for Bash and Agent.
  • Removed the duplicated long-running background task panel surface.
  • SummaryPanel is now the single background task control surface.
  • Bash and Agent tasks share the same stop button behavior and protocol call.
  • Stop button is disabled per task while stop is pending.
  • Stop pending is cleared only after the task is no longer active, not immediately after stop RPC ACK.
  • Snapshot/replay cache keys preserve workspaceIdentity / workspacePath semantics.

Phase 6 - E2E Coverage

  • Updated conversation-session case catalog and coverage matrix for background control cases.
  • Promoted E2E coverage for background Bash UI stop.
  • Promoted E2E coverage for background Agent UI stop.
  • Promoted E2E coverage for TaskStop Agent path.
  • Background Agent natural completion display regression is covered by promoted background E2E.
  • Stop/control E2E cases use explicit setup, action, and assertion.

Phase 7 - Regression Gate

  • Ran focused core, UI, shared, service, and E2E replay validation during implementation.
  • Ran pnpm typecheck.
  • Ran pnpm lint.
  • Ran git diff --check.
  • Reviewed provider-visible changes and kept background notification XML shape unchanged for this scope.

Validation Record

Commands run during implementation and cleanup:

pnpm exec oxfmt --check apps/zcode-cli/packages/core/src/tool/executor/impl.ts apps/zcode-cli/packages/core/tests/task-stop-tool.test.ts apps/zcode-cli/packages/core/tests/tool-contracts.test.ts packages/desktop/test/e2e/conversation-session/background/conversation-session-background.test.ts
pnpm --filter @zcode/core exec vitest run tests/task-stop-tool.test.ts tests/background-task-control.test.ts tests/tool-contracts.test.ts tests/subagent-explore.test.ts -t "TaskStop|background task control|orders shared provider-visible|runs through Agent"
pnpm exec vitest --run packages/ui/test/zcodeSessionProjection.test.ts packages/ui/test/agentToolCallBlock.test.ts -t "task notification|background Agent|single background task"
pnpm exec vitest --run packages/services/test/zcodeLegacyTaskCompatTaskIndex.test.ts -t "task notification|background Agent"
pnpm exec vitest --run packages/shared/test/backgroundBashJobs.test.ts packages/ui/test/chatViewSummaryPanel.test.ts packages/ui/test/zcodeBackgroundTaskControl.test.ts -t "background Agent|background task|background Bash|running task"
pnpm exec vitest --run packages/shared/test/backgroundBashJobs.test.ts packages/ui/test/chatViewSummaryPanel.test.ts packages/ui/test/backgroundTaskSnapshotSync.test.ts packages/ui/test/zcodeBackgroundTaskControl.test.ts packages/ui/test/longRunningToolCalls.test.ts
pnpm exec vitest --run packages/ui/test/zcodeBackgroundTaskControl.test.ts packages/ui/test/chatViewSummaryPanel.test.ts
E2E_PROVIDER_REPLAY_FIXTURE_PATH=packages/desktop/test/e2e/fixtures/upstream/common.json,packages/desktop/test/e2e/fixtures/upstream/conversation-session/conversation-session-background.json pnpm --filter @zcode/desktop exec wdio run wdio.conf.ts --spec './test/e2e/conversation-session/background/conversation-session-background.test.ts'
pnpm typecheck
pnpm lint
git diff --check

Intentional Deferred Scope

These are known adjacent capabilities, but they were intentionally not implemented in this plan:

  • Deferred: SDK/control background_tasks foreground-to-background command.
  • Deferred: Ctrl+B or keyboard-triggered foreground-to-background conversion.
  • Deferred: TaskOutput / output retrieval.
  • Deferred: single output-file convergence for Bash notifications.
  • Deferred: manual controls for monitor/workflow unless product scope expands.
  • Deferred: mobile Web replayable E2E promotion for this specific control surface; implementation preserves workspaceIdentity / workspacePath boundaries, but mobile replayable promotion remains a separate validation item.

Final Self-Review

  • Changed behavior traces back to the spec scope above or current ZCode behavior.
  • No generic background manager was added.
  • local_bash and local_agent stop through one runtime dispatcher.
  • Stop lifecycle is registry-first rather than projection-first.
  • Completed/stopped background tasks still wake main through task notification.
  • Race cases are covered by focused tests instead of timers.
  • UI changes are limited to the existing background task control surface.
  • No automatic commit was made by this plan.