For agentic workers: REQUIRED SUB-SKILL: Use
superpowers:executing-plansto implement this plan task-by-task. Steps use checkbox (- [ ]) syntax for tracking.
Goal: 在 executeTurn / runRegularTurnLoop 前建立 AgentRuntime 实例级 command queue 和单一 drain gate,让所有会启动 model loop 的入口先入队再串行执行,为 background subagent、Bash/monitor notification、target continuation 和后续 command queue 能力打地基。
Architecture: executeTurn(...) 保持 public API,但改为提交 prompt command;现有 turn 主体下沉为 internal executeTurnCommand(...)。新增 RuntimeCommandQueue 和 drainRuntimeCommandQueue(...),drain 入口在任何 await 前同步置 busy。第一阶段只实现 prompt 和 task-notification,但 command envelope、priority policy、executor switch 和 runtime busy helper 要为后续能力留口。
Tech Stack: TypeScript, Vitest, @zcode/core, existing AgentRuntime, existing TurnMachine, existing session event store.
AgentRuntime 实例级状态,不能做全局单例。steerTurn(...) 并入外层 queue;active turn 内部追加输入继续使用 activeTurn.pendingInputs。每次开始 Phase 1、Phase 2、Phase 3 前先重新确认以下策略仍是目标行为;如果与现状不一致,先更新本 plan,不能按旧判断继续写代码。
已确认策略:
now / next / later priority。next,pending notification 默认 later,task completion 显式 priority: "next"。apps/zcode-cli/packages/core/src/runtime/methods/turn.ts
executeTurn(...) 当前同时是 public submit API 和真实 turn executor。reserveTurnStart(...),之后再 beginActiveTurn(...)。apps/zcode-cli/packages/core/src/runtime/methods/background-notifications.ts
executeTurn(...)。apps/zcode-cli/packages/core/src/runtime/methods/steering.ts
steerTurn(...) 当前是 active turn 内部 pending queue。apps/zcode-cli/packages/core/src/runtime/methods/target.ts
targetContinuationCandidate(...) 当前只用 activeTurn 判断 idle。apps/zcode-cli/packages/core/src/runtime/methods/subagent.ts
executeTurn(...);queue 落地后它应进入 child runtime 自己的 queue。apps/zcode-cli/packages/core/src/tool/executor/background-tasks.ts
flowchart TD
A["user prompt"] --> Q["AgentRuntime.runtimeCommandQueue"]
B["target continuation"] --> Q
C["background task notification"] --> Q
D["future cron/control/deferred"] --> Q
Q --> R["requestRuntimeCommandDrain"]
R --> G{"runtimeCommandDrainActive?"}
G -- yes --> X["return; keep queued"]
G -- no --> S["set drainActive = true synchronously"]
S --> P["selectNextRuntimeCommand"]
P --> E["executeRuntimeCommand"]
E --> T["executeTurnCommand"]
T --> L["runRegularTurnLoop"]
L --> F["finally clear drainActive"]
F --> H{"queue still has command?"}
H -- yes --> R
H -- no --> I["idle"]
实例隔离:
main AgentRuntime
runtimeCommandQueue
runtimeCommandDrainActive
activeTurn
child AgentRuntime A
runtimeCommandQueue
runtimeCommandDrainActive
activeTurn
child AgentRuntime B
runtimeCommandQueue
runtimeCommandDrainActive
activeTurn
新建 apps/zcode-cli/packages/core/src/runtime/command-queue.ts:
import type { TraceContext, TurnState } from "./deps.js";
import type { ExecuteTurnOptions, TurnResult } from "./types.js";
export type RuntimeCommandPriority = "now" | "next" | "later";
export type RuntimeCommandMode = "prompt" | "task-notification";
export type RuntimeCommandId = string & { readonly __runtimeCommandId: unique symbol };
export interface RuntimeCommandBase {
readonly id: RuntimeCommandId;
readonly mode: RuntimeCommandMode;
readonly priority: RuntimeCommandPriority;
readonly createdAt: Date;
readonly traceContext: TraceContext;
}
export interface PromptRuntimeCommand extends RuntimeCommandBase {
readonly mode: "prompt";
readonly input: string;
readonly attachments?: TurnState["attachments"];
readonly options?: ExecuteTurnOptions;
readonly resolve: (result: TurnResult) => void;
readonly reject: (error: unknown) => void;
}
export interface TaskNotificationRuntimeCommand extends RuntimeCommandBase {
readonly mode: "task-notification";
readonly text: string;
readonly source: "background_task";
readonly taskId?: string;
readonly toolUseId?: string;
}
export type RuntimeCommand =
| PromptRuntimeCommand
| TaskNotificationRuntimeCommand;
新建 apps/zcode-cli/packages/core/src/runtime/methods/runtime-command-queue.ts:
import type { RuntimeCommand, TaskNotificationRuntimeCommand } from "../command-queue.js";
import type { AgentRuntimeInternal } from "../internal.js";
import type { TurnResult } from "../types.js";
export function enqueueRuntimeCommand(this: AgentRuntimeInternal, command: RuntimeCommand): void;
export function requestRuntimeCommandDrain(this: AgentRuntimeInternal): void;
export function hasActiveOrQueuedTurnWork(this: AgentRuntimeInternal): boolean;
export async function drainRuntimeCommandQueue(this: AgentRuntimeInternal): Promise<void>;
export async function executeRuntimeCommand(
this: AgentRuntimeInternal,
command: RuntimeCommand,
): Promise<TurnResult | void>;
export async function executeTaskNotificationCommand(
this: AgentRuntimeInternal,
command: TaskNotificationRuntimeCommand,
): Promise<void>;
executeTurn(...) 保持 public signature;旧主体迁移到 internal executeTurnCommand(...)。
每个 phase 开始时:
sed -n '1,260p' apps/zcode-cli/docs/design/v2/loop/runtime-command-queue-plan.mdgit status --shortgit diff --check。目标: 确认 runtime queue 前置计划独立于 subagent 专项 plan。
文件:
apps/zcode-cli/docs/design/v2/loop/runtime-command-queue-plan.mdapps/zcode-cli/docs/design/v2/tool/07-subagent-background-plan.mdChecklist:
design/v2/loop。steerTurn(...) 第一阶段不并入外层 queue。验证:
test -f apps/zcode-cli/docs/design/v2/loop/runtime-command-queue-plan.md
git diff -- apps/zcode-cli/docs/design/v2/tool/07-subagent-background-plan.md
git diff --check -- apps/zcode-cli/docs/design/v2/loop/runtime-command-queue-plan.md apps/zcode-cli/docs/design/v2/tool/07-subagent-background-plan.md
通过标准:
git diff --check 无输出。目标: 只新增纯 queue 类型和 priority dequeue policy,不改变 runtime 行为。
文件:
apps/zcode-cli/packages/core/src/runtime/command-queue.tsapps/zcode-cli/packages/core/tests/runtime-command-queue.test.ts接口产出:
RuntimeCommandPriorityRuntimeCommandModeRuntimeCommandcreateRuntimeCommandQueue()enqueue, dequeue, peek, hasPending, size, snapshotChecklist:
now 先于 next,next 先于 later。Focused tests:
pnpm --filter @zcode/core exec vitest run tests/runtime-command-queue.test.ts
通过标准:
git diff --check 通过。目标: 让 public executeTurn(...) 提交 prompt command;单一 drain gate 是进入 turn 主体的唯一路径。
文件:
apps/zcode-cli/packages/core/src/runtime/agent-runtime.tsapps/zcode-cli/packages/core/src/runtime/internal.tsapps/zcode-cli/packages/core/src/runtime/internal-methods.tsapps/zcode-cli/packages/core/src/runtime/methods/index.tsapps/zcode-cli/packages/core/src/runtime/methods/command-drain.tsapps/zcode-cli/packages/core/src/runtime/methods/turn.tsapps/zcode-cli/packages/core/src/runtime/methods/target.tsapps/zcode-cli/packages/core/tests/runtime-tool-loop.test.ts接口产出:
executeTurnCommand(...):旧 executeTurn(...) 主体。enqueueRuntimeCommand(...)requestRuntimeCommandDrain(...)drainRuntimeCommandQueue(...)hasActiveOrQueuedTurnWork(...)Checklist:
executeTurn(...) 主体迁移到 executeTurnCommand(...)。executeTurn(...) signature 不变。AgentRuntime 初始化实例级 queue 和 drain state。targetContinuationCandidate(...) 改用 hasActiveOrQueuedTurnWork()。executeTurnCommand(...)。steerTurn(...) 测试不变。Required tests:
executeTurn serializes concurrent prompt commands on the same runtimeexecuteTurn queues are isolated per runtime instancesteerTurn still queues active-turn inputtarget continuation does not start while runtime command drain is activeFocused tests:
pnpm --filter @zcode/core exec vitest run tests/runtime-command-queue.test.ts tests/runtime-tool-loop.test.ts
通过标准:
目标: 用 task-notification command 取代 background notification 的独立 wake 路径。
文件:
apps/zcode-cli/packages/core/src/runtime/methods/background-notifications.tsapps/zcode-cli/packages/core/src/runtime/methods/runtime-command-queue.tsapps/zcode-cli/packages/core/src/runtime/methods/turn.tsapps/zcode-cli/packages/core/src/runtime/methods/turn-loop.tsapps/zcode-cli/packages/core/src/runtime/internal.tsapps/zcode-cli/packages/core/src/runtime/internal-methods.tsapps/zcode-cli/packages/core/tests/subagent-background.test.tsapps/zcode-cli/packages/core/tests/runtime-hooks.test.ts行为:
enqueueBackgroundTaskNotification(...) 只 enqueue task-notification command。task-notification command 跳过 UserPromptSubmit hook。Checklist:
backgroundTaskNotificationWakeScheduled。task-notification command 使用 inputSource: "background_task"。task-notification command 使用 inputVisibility: "model-only"。task-notification command 使用 skipInputRecord: true。task-notification command 使用 skipUserPromptSubmitHooks: true。Required tests:
idle task-notification command wakes main agenttask-notification waits behind active prompt commandtask-notification skips UserPromptSubmit hooktask-notification command is provider-visible user XMLFocused tests:
pnpm --filter @zcode/core exec vitest run tests/subagent-background.test.ts tests/runtime-hooks.test.ts
通过标准:
目标: local_agent completion 只作为 task-notification producer 接入 runtime queue;修复 fake-notified。
文件:
apps/zcode-cli/packages/core/src/subagent/runner.tsapps/zcode-cli/packages/core/src/subagent/completion-notification.tsapps/zcode-cli/packages/core/src/runtime-task/notification.tsapps/zcode-cli/packages/core/tests/subagent-background.test.tsapps/zcode-cli/packages/core/tests/subagent-explore.test.tsapps/zcode-cli/packages/core/tests/subagent-profile.test.ts行为:
notified: true。Required tests:
completed background local agent enqueues one task-notificationfailed background local agent enqueues failed task-notificationstopped background local agent enqueues stopped task-notificationenqueue failure does not mark task notifiedduplicate finalize does not enqueue duplicate task-notificationgeneral-purpose and custom background agents use the same lifecycleFocused tests:
pnpm --filter @zcode/core exec vitest run tests/subagent-background.test.ts tests/subagent-explore.test.ts tests/subagent-profile.test.ts
通过标准:
目标: foreground local_agent 在配置 timeout 后转为 background,保持同一 task/child runtime 继续运行。
文件:
apps/zcode-cli/packages/core/src/subagent/runner.tsapps/zcode-cli/packages/core/src/subagent/lifecycle.tsapps/zcode-cli/packages/core/src/runtime-task/registry.tsapps/zcode-cli/packages/bootstrap/src/app/runtime-config.tsapps/zcode-cli/packages/core/tests/subagent-background.test.tsapps/zcode-cli/packages/core/tests/subagent-explore.test.ts行为:
run_in_background: true 直接后台。autoBackgroundMs > 0 时设置正常 ref'd timeout。async_launched。Required tests:
foreground local agent completes before timeout and returns final resultforeground local agent becomes background after timeoutauto-background reuses same task id and output fileauto-background timer is cleared when child finishes firstauto-background completion wakes parent through task-notification commandFocused tests:
pnpm --filter @zcode/core exec vitest run tests/subagent-background.test.ts tests/subagent-explore.test.ts
通过标准:
目标: Bash/monitor completion 最后接入同一 runtime queue notification producer,不改变 Bash 执行语义。
文件:
apps/zcode-cli/packages/core/src/tool/executor/types.tsapps/zcode-cli/packages/core/src/tool/executor/background-tasks.tsapps/zcode-cli/packages/core/tests/runtime-tool-loop.test.tsapps/zcode-cli/packages/core/tests/bash-run-conformance.test.ts行为:
task-notification command,priority next。Required tests:
background Bash completion enqueues task-notification commandbackground monitor completion enqueues task-notification commandBash notification wakes idle runtimeBash notification waits behind active prompt commandAgent async_launched is not double-notified by Bash trackerFocused tests:
pnpm --filter @zcode/core exec vitest run tests/runtime-tool-loop.test.ts tests/bash-run-conformance.test.ts tests/subagent-background.test.ts
通过标准:
目标: 清理 patch-on-patch 产物,确认 queue 地基可维护。
Checklist:
AgentRuntime instance。steerTurn(...) 行为没被误改。验证:
git diff --check
pnpm --filter @zcode/core exec vitest run tests/runtime-command-queue.test.ts tests/subagent-background.test.ts tests/subagent-explore.test.ts tests/subagent-profile.test.ts tests/runtime-hooks.test.ts tests/runtime-tool-loop.test.ts tests/bash-run-conformance.test.ts
pnpm --filter @zcode/core typecheck
pnpm --filter @zcode/core lint
pnpm typecheck
pnpm lint
通过标准:
这些不是当前 scope,只定义未来能力应该放在哪里。
入口:RuntimeCommandQueue dequeue policy。
未来实现连续 prompt merge 时,只改 policy 和 prompt command metadata,不改 executeTurnCommand(...)。
入口:RuntimeCommandQueue。
未来有真实 caller 后再加 removeById、removeByFilter、cancel pending id;当前不加空 API。
入口:RuntimeCommandMode 和 executeRuntimeCommand(...) switch。
未来实现时加入优先级选择,不能混进 prompt merge。
入口:command producer + meta prompt command。
未来实现时仍由 drain gate 驱动,不新增并行 wake。
入口:command producer。
cron/control 只 enqueue command,busy 判断统一使用 hasActiveOrQueuedTurnWork()。
入口:drain loop post-command policy。
如果未来要等 background task 或做 sweep,只加 post-command policy,不把 polling 塞到 producer 里。
每个 phase focused tests 通过后,才进入下一个 phase。