For agentic workers: REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (
- [ ]) syntax for tracking.
Goal: Make real window close, application quit, and update installation deterministically release all ZCode-owned Remote Hosts and WSL server/Agent processes without terminating the WSL distro.
Architecture: Desktop Main will use the remote session manager as the lifecycle authority and wait for all unique Local/Remote Hosts through one idempotent shutdown barrier. Host and remote stdio shutdown become bounded phase machines that always attempt Agent cleanup, while WSL tracks only the child processes it created. A strict owner registry provides safe cleanup for newly created, provably orphaned ZCode Agent processes.
Tech Stack: TypeScript, Electron UtilityProcess, Node.js child_process/stdio, Vitest, Zustand-independent Desktop Main/Host services.
Execution scope note (2026-07-20): 用户确认的产品边界是“真实关闭(非托盘隐藏)与更新安装时销毁”。
本次实现 Task 1-3 和文档/验证;Task 4 的跨启动 owner registry/reaper 不属于该触发边界,且会扩大到全局
进程检查,因此暂不引入。当前 fallback 只回收 WSLBackend 本实例明确创建的 child,不按进程名扫描。
wsl --terminate or wsl --shutdown.desktop-continuous; mobile remains web-remote-replayable./remote must continue attaching to an existing Host and must never start an independent runtime.workspaceKey = workspaceIdentity?.trim() || workspacePath, remoteSessionId, owner/lease, and CommandInbox semantics.pnpm typecheck and pnpm lint before completion.Files:
packages/desktop/src/main/desktopRemoteSessions.tspackages/desktop/src/main/index.tspackages/desktop/test/desktopRemoteSessions.test.tspackages/desktop/test/desktopHostProcess.test.tsInterfaces:
disposeHostProcessAndWait(child, label, timers, logger, options).Produces: disposeAllAndWaitForAppShutdown(reason: string): Promise<void> on the remote session manager.
[ ] Step 1: Write failing manager tests
Add tests that create one dedicated WSL session, one shared SSH Host, one pending SSH connect, and one Bot runtime, then assert application shutdown:
const shutdown = manager.disposeAllAndWaitForAppShutdown("app-before-quit");
expect(disposeHostProcessAndWait).toHaveBeenCalledTimes(3);
expect(disposeHostProcessAndWait).toHaveBeenCalledWith(
wslChild,
expect.stringContaining("app-before-quit"),
);
await shutdown;
Add a second call assertion proving the same in-flight Promise is reused and Host processes are deduplicated.
Run:
pnpm exec vitest run packages/desktop/test/desktopRemoteSessions.test.ts
Expected: FAIL because disposeAllAndWaitForAppShutdown and the async disposer injection do not exist.
Extend manager options with:
disposeHostProcessAndWait: (
child: ElectronUtilityProcess,
label: string,
) => Promise<void>;
Implement a single-flight function that rejects pending connects, snapshots unique dedicated/shared/Bot processes, clears lifecycle maps before awaiting, and runs all disposals in parallel:
let appShutdownInFlight: Promise<void> | null = null;
let appShutdownStarted = false;
function disposeAllAndWaitForAppShutdown(reason: string): Promise<void> {
if (appShutdownInFlight) return appShutdownInFlight;
appShutdownStarted = true;
const hosts = collectUniqueRemoteHostProcessesAndClearState(reason);
appShutdownInFlight = Promise.all(
hosts.map(({ process, label }) => options.disposeHostProcessAndWait(process, label)),
).then(() => undefined);
return appShutdownInFlight;
}
Creation APIs must reject after appShutdownStarted.
In index.ts, inject disposeHostProcessAndWait into the manager. Change both prepareAppQuit and prepareWindowsProcessesForUpdateInstall to await Local Host cleanup and remoteSessionManager.disposeAllAndWaitForAppShutdown(reason) in parallel. Preserve existing per-window closed disposal.
Run:
pnpm exec vitest run packages/desktop/test/desktopRemoteSessions.test.ts packages/desktop/test/desktopHostProcess.test.ts packages/desktop/test/desktopWindowLifecycle.test.ts packages/desktop/test/e2eProcessCleanup.test.ts
Expected: all tests PASS.
[ ] Step 6: Commit
git add packages/desktop/src/main/desktopRemoteSessions.ts packages/desktop/src/main/index.ts packages/desktop/test/desktopRemoteSessions.test.ts packages/desktop/test/desktopHostProcess.test.ts
git commit -m "fix(desktop): await remote hosts during shutdown"
Files:
packages/desktop/src/host/index.tspackages/server/src/stdio-lifecycle.tspackages/server/test/stdioLifecycle.test.tspackages/desktop/src/host/hostShutdownPhases.tspackages/desktop/test/hostShutdownPhases.test.tsInterfaces:
runHostShutdownPhases(phases, options): Promise<HostShutdownResult>.Consumes later: RemoteConnection.disposeAndWait() from Task 3; until then the remote phase may wrap synchronous dispose().
[ ] Step 1: Write failing stdio lifecycle test
Add a fake-timer test where stopRpc never resolves and verify dispose is still called after the RPC phase deadline:
stdin.emit("end");
await vi.advanceTimersByTimeAsync(1_000);
expect(dispose).toHaveBeenCalledTimes(1);
await vi.advanceTimersByTimeAsync(3_500);
expect(exit).toHaveBeenCalledTimes(1);
[ ] Step 2: Run the server test and verify RED
pnpm exec vitest run packages/server/test/stdioLifecycle.test.ts
Expected: FAIL because current total Promise.race skips dispose while stopRpc is pending.
Replace the single total race with a reusable bounded phase runner:
await runBoundedPhase("rpc-stop", stopRpc, rpcStopTimeoutMs);
await runBoundedPhase("service-dispose", dispose, serviceDisposeTimeoutMs);
exit(requestedExitCode);
Timeout/error marks exit code 1 and logs the exact phase, but never skips the next phase.
Cover service cleanup failure followed by remote cleanup, repeated shutdown calls, and cleanup timeout returning a non-zero result.
Create the focused helper and make disposeHostResources call it. Change disconnect to:
process.once("disconnect", () => {
void disposeHostResources("disconnect").finally(() => process.exit(1));
});
Keep exit synchronous best-effort only.
[ ] Step 6: Run focused tests and verify GREEN
pnpm exec vitest run packages/server/test/stdioLifecycle.test.ts packages/desktop/test/hostShutdownPhases.test.ts
Expected: all tests PASS.
[ ] Step 7: Commit
git add packages/server/src/stdio-lifecycle.ts packages/server/test/stdioLifecycle.test.ts packages/desktop/src/host/index.ts packages/desktop/src/host/hostShutdownPhases.ts packages/desktop/test/hostShutdownPhases.test.ts
git commit -m "fix(remote): bound every shutdown cleanup phase"
Files:
packages/server/src/remote/backend.tspackages/server/src/remote/connect.tspackages/server/src/remote/wsl-backend.tspackages/server/src/remote/docker-backend.tspackages/desktop/src/host/index.tspackages/server/test/remoteConnect.test.tspackages/server/test/wslBackend.test.tsInterfaces:
RemoteConnection.disposeAndWait(): Promise<void>.Produces: optional IRemoteBackend.disposeAndWait?(): Promise<void> while retaining synchronous dispose() compatibility.
[ ] Step 1: Write failing connection tests
Assert disposeAndWait closes stdin immediately, waits for stream.onClose, falls back to backend cleanup after a fake-timer deadline, and returns the same Promise on repeated calls. Add a connect failure test asserting backend dispose when detect/deploy/handshake throws.
Extend the spawn mock child with kill, exit, and close. Assert:
const stream = await backend.exec("sleep 60");
const disposal = backend.disposeAndWait?.();
expect(child.kill).not.toHaveBeenCalled();
child.emit("close", 0);
await disposal;
Add timeout coverage proving only children spawned by that backend are killed and no WSL terminate/shutdown args are issued.
[ ] Step 3: Run tests and verify RED
pnpm exec vitest run packages/server/test/remoteConnect.test.ts packages/server/test/wslBackend.test.ts
Expected: FAIL because async cleanup and child ownership do not exist.
dispose() starts the same cleanup Promise; disposeAndWait() returns it. Cleanup stops client/protocol/socket first, waits for stream close with an injected/default grace period, then invokes backend fallback cleanup. Wrap connection setup in try/catch so failed setup disposes the backend.
Keep a Set<ChildProcess> for active spawn/execFile calls, unregister on terminal events, reject new commands after disposal, and implement bounded wait plus child.kill() fallback. Do not call distro-wide WSL commands.
Use await remoteConnection.disposeAndWait() in the Host remote cleanup phase.
[ ] Step 7: Run focused tests and verify GREEN
pnpm exec vitest run packages/server/test/remoteConnect.test.ts packages/server/test/wslBackend.test.ts packages/server/test/dockerBackend.test.ts packages/desktop/test/hostShutdownPhases.test.ts
Expected: all tests PASS.
[ ] Step 8: Commit
git add packages/server/src/remote packages/server/test packages/desktop/src/host/index.ts packages/desktop/test/hostShutdownPhases.test.ts
git commit -m "fix(remote): await WSL server process shutdown"
Files:
packages/services/src/zcode-agent/remoteAgentOwnerRegistry.tspackages/services/test/remoteAgentOwnerRegistry.test.tspackages/services/src/zcode-agent/zcodeAgentProcessManager.tspackages/services/src/node.tspackages/server/src/remote/connect.tspackages/server/src/entry-stdio.tsInterfaces:
RemoteAgentOwnerRegistry with registerAgent, unregisterAgent, reapDeadOwners, and dispose.Consumes: ZCODE_REMOTE_OWNER_ID, ZCODE_REMOTE_OWNER_KIND, and an owner registry root under $HOME/.zcode/server/owners.
[ ] Step 1: Write failing registry tests
Use a temporary directory and injected process inspector/terminator. Cover exact ownerId + PID start-time + executable match, PID reuse, corrupt registry, environment mismatch, normal unregister, and legacy unowned process skip.
[ ] Step 2: Run test and verify RED
pnpm exec vitest run packages/services/test/remoteAgentOwnerRegistry.test.ts
Expected: FAIL because the registry does not exist.
Persist owner records atomically with schema version, ownerId, server PID/start time, Agent PID/start time, and expected executable root. Reap only when the server owner is dead/reused and every Agent identity check still matches. Never match by process name alone.
Generate the ownerId in connectRemote, inject it into the remote server command, initialize the registry only for desktop-attached-remote, and register/unregister child PIDs in ZCodeAgentProcessManager. Normal service disposal removes the owner record.
[ ] Step 5: Run registry and process-manager tests
pnpm exec vitest run packages/services/test/remoteAgentOwnerRegistry.test.ts packages/services/test/zcodeAgentProcessManager.test.ts packages/server/test/remoteConnect.test.ts
Expected: all tests PASS.
[ ] Step 6: Commit
git add packages/services/src/zcode-agent packages/services/src/node.ts packages/services/test packages/server/src/remote/connect.ts packages/server/src/entry-stdio.ts packages/server/test
git commit -m "fix(remote): reap owned orphan agents safely"
Files:
docs/wsl-remote-workspace-design.mddocs/architecture/zcode-code-architecture-overview.mddocs/web-remote-control/web-remote-control-architecture.mddocs/web-remote-control/task-realtime-sync.mdInterfaces:
Documents the final lifecycle and verification evidence; no new runtime interface.
[ ] Step 1: Update current-fact documentation
Document app/update shutdown barriers, WSL child ownership, owner reaping, and explicit non-teardown events. State that mobile attachment disconnect never destroys the shared Host and that distro termination is forbidden.
[ ] Step 2: Run all focused suites
pnpm exec vitest run packages/desktop/test/desktopRemoteSessions.test.ts packages/desktop/test/desktopHostProcess.test.ts packages/desktop/test/desktopWindowLifecycle.test.ts packages/desktop/test/e2eProcessCleanup.test.ts packages/desktop/test/hostShutdownPhases.test.ts packages/server/test/stdioLifecycle.test.ts packages/server/test/remoteConnect.test.ts packages/server/test/wslBackend.test.ts packages/server/test/dockerBackend.test.ts packages/services/test/remoteAgentOwnerRegistry.test.ts packages/services/test/zcodeAgentProcessManager.test.ts
Expected: all tests PASS.
[ ] Step 3: Run mechanical gates
pnpm typecheck
pnpm lint
Expected: both commands exit 0.
Record before/after PID evidence for tray hide, real window close, app quit, and update preparation. Confirm ZCode Host/server/Agent exit while wsl.exe -l -v may continue showing the distro as Running.
[ ] Step 5: Commit
git add docs packages
git commit -m "docs(remote): record deterministic WSL cleanup"
[ ] Step 6: Request code review and address findings
Review the complete branch diff against the design spec, rerun affected tests after any correction, and leave the worktree clean.