agent-orchestrator icon indicating copy to clipboard operation
agent-orchestrator copied to clipboard

[Bug]: Windows: `ao session kill` returns INTERNAL_ERROR when the agent process still holds the worktree cwd — kill succeeds, only worktree cleanup fails

Open faogomes opened this issue 4 months ago • 2 comments

Summary

On Windows, when the agent process (or any child) still has the session worktree as its current working directory at kill time, ao session kill terminates the session but the post-kill worktree removal fails, and the whole request surfaces as HTTP 500 / Internal server error (INTERNAL_ERROR) to the CLI. The session is dead — only the cleanup failed — but the caller can't tell success-with-dirty-worktree apart from a real failure.

Easiest trigger today is a claude-code session blocked on the workspace-trust dialog (#3407): the blocked process holds the worktree cwd, so every kill of such a session 500s. But any live process with cwd inside the worktree reproduces it — Windows refuses directory removal while a process holds it.

Steps to reproduce

  1. Windows, AO 0.11.2. Spawn a session whose agent process stays alive with cwd inside its worktree (e.g. any claude-code session hitting #3407).
  2. ao session kill <session>
  3. CLI prints Internal server error (INTERNAL_ERROR); daemon log shows the removal failure below.
  4. ao session ls shows the session terminated; the worktree directory is left behind.

Expected behavior

Kill terminates the process first, waits for process exit to release the cwd handle, then removes the worktree (with a short retry/backoff for Windows handle release). If removal still fails, report kill success with a cleanup warning (and leave the worktree for a later GC pass) rather than a 500.

Actual behavior

method=POST path=/api/v1/sessions/<session>/kill status=500 duration=8.5s
error="kill <session>: workspace: gitworktree: remove unregistered path \"D:\\...\\worktrees\\<project>\\<session>\":
unlinkat D:\\...\\worktrees\\<project>\\<session>: The process cannot access the file because it is being used by another process."

CLI: Internal server error (INTERNAL_ERROR). The session is actually terminated; only the worktree removal failed.

AO version

0.11.2 (desktop app; bundled daemon resources\daemon\ao.exe)

Environment

Windows 10 Pro (10.0.19045) x64; ConPTY runtime

Logs / extra context

  • Once the same session is unblocked before kill (trust dialog answered / pre-seeded per #3407), kill returns cleanly: session <id> killed (workspace preserved), HTTP 200.
  • Duplicate search: #2701 (stale pre-tmux handle) and #2551 (orchestrator restart on a non-git worktree) are different kill/restart 500s; #3219 is about false-success counts in bulk cleanup. None cover the live-process-holds-cwd case on Windows.

faogomes avatar Jul 31 '26 19:07 faogomes

Thanks for contributing to Agent Orchestrator.

This issue is being picked up by the current external contributor on-call pair:

  • @illegalcall
  • @pulkit7070

If someone is already working on this, please continue as usual. The on-call pair is added for visibility, tracking, and support, not to take over the work. If you need help with review, direction, reproduction, or next steps, please tag @illegalcall and @pulkit7070 here.

For faster context or live questions, you can also join the AO Discord.

Join the session here: https://discord.gg/H6ZDcUXmq

Come by if you want to see what is being built, ask questions, or just hang around with the community.

somewherelostt avatar Aug 01 '26 15:08 somewherelostt

@Vansh-Singhal

illegalcall avatar Sep 08 '26 17:09 illegalcall