Skip to content

fix(subagent): make Windows temporary directory cleanup non-fatal - #358

Merged
ZouR-Ma merged 2 commits into
mainfrom
fix/subagent-temp-dir-cleanup-windows
Nov 14, 2026
Merged

ZouR-Ma merged 2 commits into
mainfrom
fix/subagent-temp-dir-cleanup-windows

Conversation

@ZouR-Ma

@ZouR-Ma ZouR-Ma commented Nov 14, 2026

Copy link
Copy Markdown
Collaborator

Problem

An RPC subagent creates a temporary prompt directory. Both the child's close
event and bounded exit drain finalized the same exit and started independent
unawaited removals. On Windows those removals can race and reject with EPERM;
the unhandled rejection terminates the embedding host.

Changes

  • Give temporary prompt-directory removal a single owner, catch cleanup failures,
    and retry transient filesystem errors three times with 100 ms linear backoff.
  • Finalize each child exit once. Prevent synchronous terminate() cleanup from
    racing asynchronous cleanup, and skip another removal after synchronous success.
  • Preserve the completed subagent result when optional worktree cleanup fails;
    preserve the original Git allocation error when its cleanup also fails.
  • Exercise both synchronous and asynchronous cleanup paths and make real-child
    tests allow Windows to release the child's working directory before teardown.
  • Document the cleanup ownership and failure boundary.

Behavior and compatibility

No CLI options, RPC frames, stored session formats, provider behavior or permissions
change. Cleanup is best effort under the OS temporary directory. A locked directory
may remain after bounded retries; it must not change an already-completed task's
outcome. Unknown exceptions retain the existing host policy: this PR adds no global
unhandled-rejection suppression. This main branch has no Web application.

Validation

Windows, Node v24.22.1:

  • From packages/coding-agent: pnpm exec vitest run test/subagent test/step-capabilities-extension.test.ts
    — 13 files, 87 tests passed.
  • Root pnpm run check — exit 0. Biome reports one pre-existing informational
    useTemplate diagnostic in test/step-subagent-events.test.ts; no formatter changes.
  • Regression control: the original adapter fails the cleanup test because it
    invokes removal twice. The original success-path worktree cleanup fails the
    completed-result regression.
  • git diff --check — passed.
  • Root bash ./test.sh — not completed: its offline prebuild stops at
    scripts/build-coding-agent-bundle.mjs:183 with spawnSync npm ENOENT on Windows,
    before the full repository tests start. This is not a passing full-suite claim.

Limitations

CI results and maintainer approval are pending. The prebuild blocker above remains
outside this subagent cleanup repair. No merge or release has been performed.

… host on failure

On Windows the web host exited with an unhandled
`EPERM: operation not permitted, rmdir ...\Temp\stepcode-subagent-*`.

Exit handling runs from both `close` and `exitStatus`, so two unawaited
`rm(tempDir)` calls raced on the same directory; on Windows the second
rmdir fails with EPERM while the first deletion is pending, and the bare
`void rm(...)` rejection terminated the process.

- remove the temp dir at most once, with retries for transient locks
- swallow removal failures (best effort under the OS temp root)
- skip terminate's rmSync when an async removal is already in flight
…ults

Guard duplicate exit notifications, retry worktree cleanup without masking task outcomes, and verify synchronous and asynchronous cleanup ownership. Make real-child test teardown wait for Windows cwd locks and document cleanup boundaries.
@ZouR-Ma
ZouR-Ma merged commit 9ef9eaa into main Nov 14, 2026
2 checks passed
@ZouR-Ma
ZouR-Ma deleted the fix/subagent-temp-dir-cleanup-windows branch November 14, 2026 08:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant