Skip to content

fix: skip session cleanup of a workspace still used by a live session#2487

Open
polymath-orchestrator wants to merge 3 commits into
AgentWrapper:mainfrom
polymath-orchestrator:upstream-cleanup-live-ws-68
Open

fix: skip session cleanup of a workspace still used by a live session#2487
polymath-orchestrator wants to merge 3 commits into
AgentWrapper:mainfrom
polymath-orchestrator:upstream-cleanup-live-ws-68

Conversation

@polymath-orchestrator

Copy link
Copy Markdown

Problem

Manager.Cleanup reclaims the workspaces of terminated sessions, but it keys eligibility on the session's terminated state alone — never on whether the workspace path is still referenced by another, live session.

Persistent/shared worktrees break that assumption. An orchestrator's worktree is reused across respawn, so a terminated predecessor session and a live successor session can point at the same WorkspacePath. ao session cleanup then tears down the directory out from under the live session — deleting its current working directory. In-flight commands whose cwd was the deleted dir fail silently.

Fix

Before reclaiming, Cleanup builds the set of workspace paths still occupied by any non-terminated session (liveWorkspacePaths) and skips any terminated session whose path is in that set, reporting it in Skipped with the reason workspace in use by a live session. Paths are compared with filepath.Clean so trailing-slash / .-segment variants of the same directory still match. Terminated workspaces no live session references are reclaimed exactly as before.

Runtime teardown is deliberately kept before the workspace guard: the runtime handle is per-session (independent of the shared workspace dir and of the live successor's handle), so a skipped predecessor's own lingering keep-alive shell is still reclaimed rather than leaked.

Tests

  • TestCleanup_SkipsWorkspaceStillReferencedByLiveSession — a terminated predecessor sharing a live successor's worktree is skipped (not reclaimed), its own runtime handle is still torn down, and the shared directory is preserved.
  • TestCleanup_LiveWorkspaceGuardNormalizesPaths — trailing-slash / .-segment path variants still match.
  • TestCleanup_ReclaimsUnsharedWhileSkippingShared — an adjacent terminated workspace no live session references is still reclaimed alongside a shared skip.

go build ./..., go vet ./..., go test ./... all green.

polymath-orchestrator and others added 3 commits July 7, 2026 15:48
…rapper#68)

ao session cleanup keyed workspace-reclaim eligibility on the session's
terminated state alone. Persistent/shared worktrees (the orchestrator's) are
reused across respawn, so a terminated predecessor and a live successor can
share one path — cleanup then deleted the live session's cwd out from under it.

Cleanup now builds the set of workspace paths still occupied by a
non-terminated session and skips (reporting the reason) any terminated
session whose canonicalized path is in that set. Distinct-path terminated
workspaces are still reclaimed as before.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FWvyzpHEWWMQcVN5rK9TJh
…er#68 review)

Final-review (Copilot) caught that the shared-workspace guard continued before
m.runtime.Destroy, so a terminated session skipped for a shared workspace also
skipped its runtime teardown — leaking the predecessor's own lingering
keep-alive shell until cleanup reran. The runtime handle is per-session
(independent of the shared workspace dir and the live successor's handle), so
teardown now runs before the workspace guard; only workspace.Destroy is gated.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FWvyzpHEWWMQcVN5rK9TJh
@harshitsinghbhandari

Copy link
Copy Markdown
Collaborator

@polymath-orchestrator can you resolve merge conflicts

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.

2 participants