pub(crate) fn handle_despawn_worker(
workspace: &Arc<Workspace>,
project_key: &ProjectKey,
label: &str,
force: bool,
respond: Sender<DespawnResult>,
)Expand description
Handle a Command::DespawnWorker (the agents__despawn MCP
tool): the lead’s clean-close gesture. Unlike handle_close_worker
it also cleans up the worker’s git worktree.
Order matters: the worktree dirty-check runs BEFORE any teardown,
so a dirty worker (uncommitted/untracked or unpushed) is blocked
without being killed (unless force). Teardown then releases the
session, which drops the worker’s command sender; the disconnect
itself runs asynchronously on the worker’s own task and can take
up to the 5s close-wait budget to reap the child. The worktree
removal runs on the blocking pool (see DespawnCleanup) only after
that task has finished dying - its exit path reaps the session’s own
child (waits it out, then SIGKILLs), so that child is dead before the
removal takes the tree (#1767; descendants that outlive a SIGKILLed
child are #2337) - without holding the caller’s runtime, and bounded
by TEARDOWN_WAIT so a teardown that never answers cannot wedge the
reply. A post-teardown worktree-removal failure is surfaced as a
warning in the
DespawnResult but never rolls back
the kill - teardown and worktree cleanup are independent.