状态(2026-07-15):历史 legacy UI 实施计划。
isMainActive仍存在于 task compatibility 层, 但 V4 conversation 主链路以 snapshot 的 phase、active works、rows/control projection 判断前台工作; 下文useTaskRestore、task-stream handler 和旧 E2E 路径不是当前实现入口。For agentic workers: REQUIRED SUB-SKILL: Use superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (
- [ ]) syntax for tracking.
Goal: 将 isMainActive 从本地临时 UI 猜测收敛为 runtime snapshot / live monitor 同步的临时投影,确保 main idle + background running 时不显示底部 fake loading、普通消息不进 queue、queued prompt 可以正常 drain,同时 restore / reconnect 不会误判仍在运行的 main turn。
Architecture: mainActive 作为 ZCodeSessionRuntimeSnapshot.runtime 的 ephemeral 字段,从 agent runtime active turn 投影生成;UI 通过 applyTaskSnapshotEphemeralRuntimeState() 和 live stream/background monitor 统一同步到 TaskUiState.isMainActive。旧的 runtime.status、activeInputId、activeTurnKind 状态流保持不变,isMainActive 只被三类消费点读取:main loading 展示、普通发送 queue 判定、queued prompt drain 判定。
Tech Stack: TypeScript、Zod protocol schema、Zustand store、Vitest、WDIO conversation-session E2E、pnpm workspace。
runtime.activeTurnId 写入 activeInputId;二者语义不同。runtime.status / displayedStatus / activeInputId 主状态流。isMainActive 设置为 true。isMainActive 回滚到错误状态。mainActive。mainActive。mainActive。isMainActive。mainActive 是 runtime ephemeral snapshot,不是 task-index 持久状态。packages/shared/src/zcode-protocol/index.ts
runtime.mainActive?: boolean。packages/shared/src/zcode-task-types.ts
ZCodeSessionRuntimeSnapshot.mainActive?: boolean。apps/zcode-cli/packages/bootstrap/src/zcode-protocol/session-mapper.ts
mainActive: Boolean(input.activeTurn)。apps/zcode-cli/packages/bootstrap/tests/zcode-protocol.test.ts
mainActive 的生成。packages/ui/src/hooks/taskSnapshotRuntimeStateSync.ts
runtime.mainActive 到 TaskUiState.isMainActive 的同步。packages/ui/src/hooks/taskStreamEventSnapshotSync.ts
mainActive,不能绕过现有 stale 判断。packages/ui/src/hooks/useTaskRestore.ts
applyTaskSnapshotEphemeralRuntimeState()。isMainActive。packages/ui/src/store/zcodeSessionStoreTaskSlice.ts
setTaskMainActive() 和 setTaskRuntimeState(non-running) 清理逻辑。packages/ui/src/hooks/taskStreamEventHandlers.ts
isMainActive=true;不让 background child tool event 设置 true。packages/ui/src/lib/zcodeTaskRuntimeMonitor.ts
packages/ui/test/chatStatus.test.ts
packages/ui/test/useZCodeChatSendPromptQueuePolicy.test.ts
packages/ui/test/zcodeTaskRuntimeMonitorQueueDrain.test.ts
packages/ui/test/taskSnapshotRuntimeStateSync.test.ts
mainActive。packages/desktop/test/e2e/conversation-session/background/conversation-session-background.test.ts
Purpose: 在改实现前确认当前 isMainActive 的消费面和缺失面,写出本轮必须失败的测试。
Files:
packages/ui/src/store/zcodeSessionStoreTaskSlice.tspackages/ui/src/hooks/taskSnapshotRuntimeStateSync.tspackages/ui/src/hooks/taskStreamEventSnapshotSync.tspackages/ui/src/hooks/useTaskRestore.tspackages/ui/src/hooks/taskStreamEventHandlers.tspackages/ui/src/lib/zcodeTaskRuntimeMonitor.tsapps/zcode-cli/packages/bootstrap/src/zcode-protocol/session-mapper.tsapps/zcode-cli/packages/bootstrap/tests/zcode-protocol.test.ts[ ] Run current usage scan:
rg -n "isMainActive|setTaskMainActive|mainActive|activeTurnId|activeTurnKind" \
packages/ui/src packages/ui/test packages/shared/src apps/zcode-cli/packages/bootstrap
Expected:
isMainActive appears only in store, explicit main-active writers, send/queue/drain/display consumers, and tests.No existing mainActive protocol field yet.
[ ] Confirm current tests still pass before starting:
pnpm exec vitest run \
packages/ui/test/chatStatus.test.ts \
packages/ui/test/useZCodeChatSendPromptQueuePolicy.test.ts \
packages/ui/test/zcodeTaskRuntimeMonitorQueueDrain.test.ts
Expected: PASS before this plan starts changing snapshot behavior.
runtime.mainActive === true.runtime.mainActive === false.TaskUiState.isMainActive from runtime.mainActive.isMainActive === false.Phase 0 Exit Criteria
runtime.mainActive To Protocol Snapshot SurfacePurpose: 让 snapshot 自身携带 main 是否 active 的事实,避免 UI 从 activeTurnId 临时猜测。
Files:
packages/shared/src/zcode-protocol/index.tspackages/shared/src/zcode-task-types.tsapps/zcode-cli/packages/bootstrap/src/zcode-protocol/session-mapper.tsapps/zcode-cli/packages/bootstrap/tests/zcode-protocol.test.tsInterfaces Produced:
// packages/shared/src/zcode-task-types.ts
export interface ZCodeSessionRuntimeSnapshot {
mainActive?: boolean;
}
// packages/shared/src/zcode-protocol/index.ts
mainActive: z.boolean().optional()
Implementation Notes:
In the session mapper, compute:
const mainActive = Boolean(input.activeTurn);
Include mainActive in the runtime snapshot object.
Keep activeTurnId and activeTurnKind unchanged.
Emit explicit false for idle snapshots when the mapper owns the runtime object. Keep schema optional for backward compatibility with older snapshots.
Add a test near existing active turn snapshot tests in:
apps/zcode-cli/packages/bootstrap/tests/zcode-protocol.test.ts
Test intent:
it("marks runtime mainActive while a session active turn exists", async () => {
// Arrange fake app runtime with getActiveTurnInfo() returning a regular active turn.
// Act buildSessionSnapshot(...).
// Assert snapshot.runtime.mainActive === true.
// Assert snapshot.runtime.activeTurnId is still the runtime turn id.
});
Expected RED before implementation:
pnpm --filter @zcode/zcode-cli-bootstrap test -- zcode-protocol.test.ts -t "marks runtime mainActive"
If the package script differs locally, use:
pnpm exec vitest run apps/zcode-cli/packages/bootstrap/tests/zcode-protocol.test.ts -t "marks runtime mainActive"
Expected: FAIL because runtime.mainActive is missing.
Test intent:
it("marks runtime mainActive false when no session active turn exists", async () => {
// Arrange fake runtime getActiveTurnInfo() returns undefined.
// Assert snapshot.runtime.mainActive === false.
// Assert snapshot.runtime.activeTurnId is undefined.
});
Change:
packages/shared/src/zcode-protocol/index.tspackages/shared/src/zcode-task-types.ts
[ ] Implement mapper change.
Change:
apps/zcode-cli/packages/bootstrap/src/zcode-protocol/session-mapper.tsAdd mainActive to the runtime snapshot object generated from active turn state.
[ ] Run Phase 1 tests.
pnpm exec vitest run apps/zcode-cli/packages/bootstrap/tests/zcode-protocol.test.ts -t "mainActive"
pnpm typecheck
Expected:
Phase 1 Exit Criteria
mainActive: true.mainActive: false.activeTurnId is still not used as activeInputId.pnpm typecheck pass before Phase 2.mainActive From Accepted Snapshots Into UI StorePurpose: 让 restore / reconnect 使用 snapshot 中的 mainActive,并且只在 accepted snapshot path 应用,避免 stale snapshot 回滚状态。
Files:
packages/ui/src/hooks/taskSnapshotRuntimeStateSync.tspackages/ui/src/hooks/taskStreamEventSnapshotSync.tspackages/ui/test/taskSnapshotRuntimeStateSync.test.ts or create it if missingpackages/ui/test/zcodeTaskRuntimeMonitorQueueDrain.test.tsInterfaces Consumed:
runtime?: Pick<
ZCodeSessionRuntimeSnapshot,
| "activeTurnKind"
| "mainActive"
| "contextUsage"
| "apiRetry"
| "goalStats"
| "goalVerifications"
| "plan"
| "todoGroups"
>
Implementation Notes:
applyTaskSnapshotEphemeralRuntimeState() should apply runtime.mainActive only when the field is present:
if (runtime?.mainActive !== undefined) {
store.setTaskMainActive(workspacePath, taskId, runtime.mainActive, workspaceIdentity);
}
Do not infer mainActive from activeTurnId in UI.
Do not write activeTurnId into activeInputId.
setTaskRuntimeState(non-running) remains the terminal cleanup path and still clears isMainActive=false.
In syncTaskSnapshotAfterEvent(), keep calling applyTaskSnapshotEphemeralRuntimeState() only after the existing freshness/stale decisions have accepted the snapshot for ephemeral sync.
If a test exposes that stale terminal snapshots still apply ephemeral fields, adjust the call site by passing a filtered runtime object for stale terminal branches rather than adding ad hoc guards inside the store.
Create or update:
packages/ui/test/taskSnapshotRuntimeStateSync.test.ts
Test intent:
it("restores isMainActive from runtime.mainActive", () => {
applyTaskSnapshotEphemeralRuntimeState({
runtime: {
mainActive: true,
activeTurnKind: "regular",
},
workspacePath,
taskId,
});
expect(getTaskUiState(workspaceState, taskId).isMainActive).toBe(true);
});
Expected RED:
pnpm exec vitest run packages/ui/test/taskSnapshotRuntimeStateSync.test.ts -t "restores isMainActive"
Expected: FAIL before implementation.
Test intent:
it("clears isMainActive when accepted runtime.mainActive is false", () => {
store.setTaskMainActive(workspacePath, taskId, true);
applyTaskSnapshotEphemeralRuntimeState({
runtime: { mainActive: false },
workspacePath,
taskId,
});
expect(getTaskUiState(workspaceState, taskId).isMainActive).toBe(false);
});
[ ] Implement mainActive sync in applyTaskSnapshotEphemeralRuntimeState().
[ ] Run Phase 2 focused tests.
pnpm exec vitest run \
packages/ui/test/taskSnapshotRuntimeStateSync.test.ts \
packages/ui/test/zcodeTaskRuntimeMonitorQueueDrain.test.ts
Expected: PASS.
[ ] Run regression tests for send/queue policy.
pnpm exec vitest run \
packages/ui/test/chatStatus.test.ts \
packages/ui/test/useZCodeChatSendPromptQueuePolicy.test.ts \
packages/ui/test/zcodeTaskRuntimeMonitorQueueDrain.test.ts
Expected: PASS.
Phase 2 Exit Criteria
isMainActive=true from snapshot.isMainActive=false.mainActive from activeTurnId.Purpose: 确保切后台后 monitor 像更新其他 runtime 状态一样维护 isMainActive,而 background-only event 不污染 main active。
Files:
packages/ui/src/hooks/taskStreamEventHandlers.tspackages/ui/src/lib/zcodeTaskRuntimeMonitor.tspackages/ui/test/zcodeTaskRuntimeMonitorQueueDrain.test.tspackages/ui/test/useZCodeChatSendPromptQueuePolicy.test.tsImplementation Notes:
isMainActive=true for main turn facts:
task_run_startedgoal_iteration_startedagent_message_chunkagent_thought_chunktask_run_started for inactive taskisMainActive=true for background-only facts:
background_bash_jobs_updatetool_call / tool_call_update from background subagentsetTaskRuntimeState(non-running).[ ] Audit live writers.
rg -n "setTaskMainActive" packages/ui/src/hooks packages/ui/src/lib
Expected:
No background-only update path writes true.
[ ] Add or confirm test: background-only streaming does not queue normal prompt.
Existing target:
packages/ui/test/useZCodeChatSendPromptQueuePolicy.test.ts
Expected assertion:
expect(
shouldQueuePromptForMainActivity({
currentStatus: "idle",
runtimeState: { activeInputId: "background-run", status: "streaming" },
isMainActive: false,
stopRequested: false,
queuedPromptCount: 0,
}),
).toBe(false);
Expected assertion:
expect(
shouldQueuePromptForMainActivity({
currentStatus: "idle",
runtimeState: { activeInputId: "main-run", status: "streaming" },
isMainActive: true,
stopRequested: false,
queuedPromptCount: 0,
}),
).toBe(true);
Existing target:
packages/ui/test/zcodeTaskRuntimeMonitorQueueDrain.test.ts
Expected assertion:
streamingisMainActive remains falsetriggerTaskQueuedPromptDrain(...) calls sendPrompt(...)
[ ] Run Phase 3 focused tests.
pnpm exec vitest run \
packages/ui/test/useZCodeChatSendPromptQueuePolicy.test.ts \
packages/ui/test/zcodeTaskRuntimeMonitorQueueDrain.test.ts
Expected: PASS.
Phase 3 Exit Criteria
isMainActive=true.isMainActive=true.Purpose: 补齐本轮真正缺口:restore/reconnect 时 isMainActive 和 runtime snapshot 保持一致。
Files:
packages/ui/test/taskSnapshotRuntimeStateSync.test.tspackages/ui/test/useZCodeChatSendPromptQueuePolicy.test.tspackages/ui/test/taskStreamEventSnapshotSync.test.ts if existing helpers make stale snapshot behavior easy to isolateTest structure:
// 1. Apply snapshot ephemeral runtime { mainActive: true, activeTurnKind: "regular" }.
// 2. Set task runtime state to streaming.
// 3. Call shouldQueuePromptForMainActivity(... isMainActive from store ...).
// 4. Expect true.
Test structure:
// 1. Set isMainActive true.
// 2. Apply accepted runtime { mainActive: false } or setTaskRuntimeState(..., "completed").
// 3. Call shouldQueuePromptForMainActivity(...).
// 4. Expect false when no queued prompt, stop, timeline, or goal active.
Test intent:
isMainActive remains true.If the current test harness cannot call syncTaskSnapshotAfterEvent() cleanly without heavy setup, record this as covered by existing stale runtime tests and do not introduce a brittle mock-heavy test.
[ ] Run Phase 4 tests.
pnpm exec vitest run \
packages/ui/test/taskSnapshotRuntimeStateSync.test.ts \
packages/ui/test/chatStatus.test.ts \
packages/ui/test/useZCodeChatSendPromptQueuePolicy.test.ts \
packages/ui/test/zcodeTaskRuntimeMonitorQueueDrain.test.ts
Expected: PASS.
Phase 4 Exit Criteria
isMainActive=true -> normal send queues.isMainActive=false -> normal send can direct-send.Purpose: 验证用户可见行为没有回归,特别是 background-only 不再造成 fake loading,同时 foreground 仍保持原 loading。
Files:
packages/desktop/test/e2e/conversation-session/background/conversation-session-background.test.tsdocs/conversation-session-case-catalog.mddocs/testing/conversation-session-background-e2e-coverage-matrix.mddocs/testing/conversation-session-e2e-coverage-matrix.mdpackages/desktop/test/e2e/fixtures/cases/conversation-session/conversation-session-background.jsonpackages/desktop/test/e2e/fixtures/upstream/conversation-session/conversation-session-background.jsonExpected:
chat-view remains idle for main.No visible bottom chat-loading.
[ ] Confirm or add E2E: foreground subagent child tool update still shows main loading.
Expected:
Bottom chat-loading appears.
[ ] Confirm or add E2E: background running does not queue normal user prompt.
Expected:
queueCount remains 0.
[ ] Run targeted E2E only.
# workdir: packages/desktop
./node_modules/.bin/wdio run wdio.conf.ts \
--spec ./test/e2e/conversation-session/background/conversation-session-background.test.ts \
--mochaOpts.grep "background subagent 的 child tool update|foreground subagent 的 child tool update|background task running 时用户普通消息应直接发送"
Expected:
Phase 5 Exit Criteria
Purpose: 提交前完整验证,但不自动提交。
[ ] Run focused unit tests.
# workdir: apps/zcode-cli
./node_modules/.bin/vitest run \
packages/bootstrap/tests/zcode-protocol.test.ts -t "mainActive|last projected turn"
pnpm exec vitest run \
packages/ui/test/taskSnapshotRuntimeStateSync.test.ts \
packages/ui/test/chatStatus.test.ts \
packages/ui/test/useZCodeChatSendPromptQueuePolicy.test.ts \
packages/ui/test/zcodeTaskRuntimeMonitorQueueDrain.test.ts
Expected: PASS.
[ ] Run typecheck.
pnpm typecheck
Expected: exit 0.
[ ] Run lint.
pnpm lint
Expected: exit 0. Existing warnings are acceptable only if lint exits 0 and none are introduced by this change.
[ ] Run whitespace check.
git diff --check
Expected: no output.
Expected: PASS.
[ ] Review diff boundaries.
git diff --stat
rg -n "mainActive|isMainActive|setTaskMainActive|activeTurnId|activeInputId" \
packages/shared/src packages/ui/src packages/ui/test apps/zcode-cli/packages/bootstrap
Expected:
mainActive appears in protocol type/schema, mapper, UI snapshot sync, store, tests.activeTurnId is not written into activeInputId.isMainActive is consumed only by main loading, normal send queue decision, queued prompt drain, and tests.Phase 6 Exit Criteria
git diff --check passes.Foreground main turn running:
runtime.status can be streamingTaskUiState.isMainActive === trueBackground-only task running while main idle:
runtime.status may still be streaming because background task/control state is activeTaskUiState.isMainActive === falseRestore/reconnect active main turn:
runtime.mainActive === trueTaskUiState.isMainActive === trueRestore/reconnect completed task:
runtime.mainActive === false or terminal runtime clears itTaskUiState.isMainActive === falseactiveInputId.mainActive in ChatView/useZCodeChat from activeTurnId.setTaskRuntimeState(non-running).