Skip to content

fix(agent-server): remove a deleted local conversation's worktree, keep its branch - #5547

Merged
neubig merged 5 commits into
mainfrom
vasco/5260-delete-local-worktree
Oct 8, 2026
Merged

neubig merged 5 commits into
mainfrom
vasco/5260-delete-local-worktree

Conversation

@VascoSch92

@VascoSch92 VascoSch92 commented Oct 6, 2026 •

Copy link
Copy Markdown
Member

HUMAN:

Deleting a local conversation started with worktree=true left its git worktree on disk and registered in the repo forever (I reproduced it). Deleting the conversation now removes the worktree; the openhands/<id> branch with the agent's commits is kept. Uncommitted changes in the worktree are deleted with the conversation.


AGENT:

Why

Deleting a host-local conversation removes only its conversation directory. When it was started with worktree=true, its git worktree under conversation_worktree_root/<id>/ (default /tmp/conversation-worktrees) stays on disk forever, still registered in the user's repository (git worktree list).

This is the delete fix from #5433, split out as Engel suggested there: it needs no reclaimer, so it does not have to wait for the rest of the storage stack.

Summary

  • delete_conversation now calls remove_conversation_worktree(conversation_worktree_root, id) after removing the conversation directory.
  • It removes conversation_worktree_root/<id>/ and runs git worktree prune in each source repository. The openhands/<id> branch, with the agent's commits, is kept.
  • git runs with core.fsmonitor=false and --no-optional-locks, because the repository config is agent-writable. A symlinked conversation directory is left alone.

Issue Number

Fixes #5553

How to Test

uv run pytest tests/agent_server/test_local_storage.py -q   # 6 passed, ~8 s, no Docker

The tests start a real local-mode server (configured through OH_* environment variables, as the CLI does) and use real git repositories; nothing is mocked. Each checks what must survive as well as what must go:

  1. Delete removes the worktree and its git registration; the openhands/<id> branch keeps the agent's commit; the user's checkout stays clean on main.
  2. Uncommitted work in the worktree goes with the conversation; only commits survive, on the branch.
  3. Delete still succeeds when the source repository was removed first.
  4. A link planted in place of the conversation's worktree dir is not followed: the repository it points to is untouched.
  5. Deleting one conversation leaves another conversation's worktree (with its uncommitted work) and the user's own worktree of the same repository alone.
  6. A conversation started without worktree=true leaves the repository alone.

Mutation-checked, each makes the expected tests fail: no removal on delete (1, 2, 3, 5), following a planted link (4), skipping git worktree prune (1, 5), deleting the agent's branch (1, 2), removing the whole worktree root (5).

Video/Screenshots

N/A (server-side).

Type

  • Bug fix
  • Feature
  • Refactor
  • Breaking change
  • Docs / chore

Notes


🐳 Agent Server images for this PR — GHCR package, pull/run commands, and all pushed tags (click to expand)

• GHCR package: https://github.com/OpenHands/agent-sdk/pkgs/container/agent-server

Variants & Base Images

Variant Architectures Base Image Docs / Tags
java amd64, arm64 eclipse-temurin:17-jdk Link
python-slim amd64, arm64 python-node-runtime Link
python-minimal amd64, arm64 python-node-runtime Link
python amd64, arm64 python-node-runtime Link
golang amd64, arm64 golang:1.21-bookworm Link

Pull (multi-arch manifest)

# Each variant is a multi-arch manifest supporting both amd64 and arm64
docker pull ghcr.io/openhands/agent-server:031b68e-python

Run

docker run -it --rm \
  -p 8000:8000 \
  --name agent-server-031b68e-python \
  ghcr.io/openhands/agent-server:031b68e-python

All tags pushed for this build

ghcr.io/openhands/agent-server:031b68e-golang-amd64
ghcr.io/openhands/agent-server:031b68e6ff6af05b96bb20db481d4cf645857e7a-golang-amd64
ghcr.io/openhands/agent-server:vasco-5260-delete-local-worktree-golang-amd64
ghcr.io/openhands/agent-server:031b68e-golang_tag_1.21-bookworm-amd64
ghcr.io/openhands/agent-server:031b68e-golang-arm64
ghcr.io/openhands/agent-server:031b68e6ff6af05b96bb20db481d4cf645857e7a-golang-arm64
ghcr.io/openhands/agent-server:vasco-5260-delete-local-worktree-golang-arm64
ghcr.io/openhands/agent-server:031b68e-golang_tag_1.21-bookworm-arm64
ghcr.io/openhands/agent-server:031b68e-java-amd64
ghcr.io/openhands/agent-server:031b68e6ff6af05b96bb20db481d4cf645857e7a-java-amd64
ghcr.io/openhands/agent-server:vasco-5260-delete-local-worktree-java-amd64
ghcr.io/openhands/agent-server:031b68e-eclipse-temurin_tag_17-jdk-amd64
ghcr.io/openhands/agent-server:031b68e-java-arm64
ghcr.io/openhands/agent-server:031b68e6ff6af05b96bb20db481d4cf645857e7a-java-arm64
ghcr.io/openhands/agent-server:vasco-5260-delete-local-worktree-java-arm64
ghcr.io/openhands/agent-server:031b68e-eclipse-temurin_tag_17-jdk-arm64
ghcr.io/openhands/agent-server:031b68e-python-amd64
ghcr.io/openhands/agent-server:031b68e6ff6af05b96bb20db481d4cf645857e7a-python-amd64
ghcr.io/openhands/agent-server:vasco-5260-delete-local-worktree-python-amd64
ghcr.io/openhands/agent-server:031b68e-python-node-runtime-amd64
ghcr.io/openhands/agent-server:031b68e-python-arm64
ghcr.io/openhands/agent-server:031b68e6ff6af05b96bb20db481d4cf645857e7a-python-arm64
ghcr.io/openhands/agent-server:vasco-5260-delete-local-worktree-python-arm64
ghcr.io/openhands/agent-server:031b68e-python-node-runtime-arm64
ghcr.io/openhands/agent-server:031b68e-python-minimal-amd64
ghcr.io/openhands/agent-server:031b68e6ff6af05b96bb20db481d4cf645857e7a-python-minimal-amd64
ghcr.io/openhands/agent-server:vasco-5260-delete-local-worktree-python-minimal-amd64
ghcr.io/openhands/agent-server:031b68e-python-node-runtime-minimal-amd64
ghcr.io/openhands/agent-server:031b68e-python-minimal-arm64
ghcr.io/openhands/agent-server:031b68e6ff6af05b96bb20db481d4cf645857e7a-python-minimal-arm64
ghcr.io/openhands/agent-server:vasco-5260-delete-local-worktree-python-minimal-arm64
ghcr.io/openhands/agent-server:031b68e-python-node-runtime-minimal-arm64
ghcr.io/openhands/agent-server:031b68e-python-slim-amd64
ghcr.io/openhands/agent-server:031b68e6ff6af05b96bb20db481d4cf645857e7a-python-slim-amd64
ghcr.io/openhands/agent-server:vasco-5260-delete-local-worktree-python-slim-amd64
ghcr.io/openhands/agent-server:031b68e-python-node-runtime-slim-amd64
ghcr.io/openhands/agent-server:031b68e-python-slim-arm64
ghcr.io/openhands/agent-server:031b68e6ff6af05b96bb20db481d4cf645857e7a-python-slim-arm64
ghcr.io/openhands/agent-server:vasco-5260-delete-local-worktree-python-slim-arm64
ghcr.io/openhands/agent-server:031b68e-python-node-runtime-slim-arm64
ghcr.io/openhands/agent-server:031b68e-golang
ghcr.io/openhands/agent-server:031b68e6ff6af05b96bb20db481d4cf645857e7a-golang
ghcr.io/openhands/agent-server:vasco-5260-delete-local-worktree-golang
ghcr.io/openhands/agent-server:031b68e-golang_tag_1.21-bookworm
ghcr.io/openhands/agent-server:031b68e-java
ghcr.io/openhands/agent-server:031b68e6ff6af05b96bb20db481d4cf645857e7a-java
ghcr.io/openhands/agent-server:vasco-5260-delete-local-worktree-java
ghcr.io/openhands/agent-server:031b68e-eclipse-temurin_tag_17-jdk
ghcr.io/openhands/agent-server:031b68e-python-minimal
ghcr.io/openhands/agent-server:031b68e6ff6af05b96bb20db481d4cf645857e7a-python-minimal
ghcr.io/openhands/agent-server:vasco-5260-delete-local-worktree-python-minimal
ghcr.io/openhands/agent-server:031b68e-python-node-runtime-minimal
ghcr.io/openhands/agent-server:031b68e-python-slim
ghcr.io/openhands/agent-server:031b68e6ff6af05b96bb20db481d4cf645857e7a-python-slim
ghcr.io/openhands/agent-server:vasco-5260-delete-local-worktree-python-slim
ghcr.io/openhands/agent-server:031b68e-python-node-runtime-slim
ghcr.io/openhands/agent-server:031b68e-python
ghcr.io/openhands/agent-server:031b68e6ff6af05b96bb20db481d4cf645857e7a-python
ghcr.io/openhands/agent-server:vasco-5260-delete-local-worktree-python
ghcr.io/openhands/agent-server:031b68e-python-node-runtime

About Multi-Architecture Support

  • Each variant tag (e.g., 031b68e-python) is a multi-arch manifest supporting both amd64 and arm64
  • Docker automatically pulls the correct architecture for your platform
  • Individual architecture tags (e.g., 031b68e-python-amd64) are also available if needed

…ep its branch

Deleting a host-local conversation started with worktree=true left its git
worktree on disk and registered in the user's repository. Remove the
checkout and prune git's record of it; the openhands/<id> branch with the
agent's commits is kept.
@github-actions

github-actions Bot commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor
  ✅ **PR Artifacts Cleaned Up**

  The `.pr/` directory is no longer present.

@github-actions

github-actions Bot commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

REST API breakage checks (OpenAPI) — ✅ PASSED

Result: ✅ PASSED

Action log

@github-actions

github-actions Bot commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Coverage

Coverage Report •
FileStmtsMissCoverMissing
openhands-agent-server/openhands/agent_server
   conversation_service.py121312690%172–173, 182, 235–236, 240–241, 246, 411–412, 473, 578, 585–586, 687, 773, 824–825, 832, 864–865, 881, 912, 916, 928, 948, 960–963, 969–970, 979, 981, 1047, 1057, 1082, 1088–1089, 1093–1094, 1102, 1129, 1135, 1229, 1235, 1240, 1246, 1254–1255, 1264–1267, 1276, 1289, 1307, 1344, 1348–1352, 1375, 1424, 1437, 1445, 1503–1504, 1508, 1536–1537, 1685–1687, 1689–1690, 1693–1694, 1736, 1817–1818, 1850, 1853, 1860–1862, 1865–1866, 1870–1872, 1875–1876, 1880–1882, 1885–1886, 1915, 1924, 1977, 1985–1987, 2048, 2051, 2078, 2088, 2093–2096, 2110, 2121, 2133–2134, 2166, 2263, 2322, 2380, 2395–2396, 2779, 2832, 2835
   local_storage.py31294%29, 34
TOTAL481291920560% 

…real git

Replace the mocked EventService with a local-mode server configured by
environment, and cover the failure paths: uncommitted work, a missing
source repository, a link planted in place of the worktree dir, other
conversations' and the user's own worktrees, and conversations without
a worktree.
@VascoSch92
VascoSch92 marked this pull request as ready for review October 6, 2026 12:31

@all-hands-bot all-hands-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This review was posted by an AI agent (OpenHands).

Reviewed head f9ec3438db45705a3744e9c171b3180d8c63fc0b (matches the requested SHA) against base main, with the repo AGENTS.md and .agents/skills/custom-codereview-guide.md.

Scope: this belongs in this repository (the Agent Server owns local conversation storage), and #5553 is ready-for-dev / priority:medium, so the direction is clear. The change addresses the reported bug: delete_conversation now removes conversation_worktree_root/<id>/ and prunes the source repo's git registration, keeping the openhands/<id> branch.

Verification on this head: CI is green; tests/agent_server/test_local_storage.py passes 6/6 in the workspace, and test_conversation_service.py + test_conversation_router.py (237 tests) still pass. The _git invocation is hardened (--no-optional-locks, core.fsmonitor=false, sanitized_env) and the symlink guard is sound.

One material finding on the fork path, described inline: a forked conversation shares its source's worktree, so deleting the source now deletes the live fork's workspace.

🔄 CHANGES REQUESTED

VascoSch92 and others added 2 commits October 7, 2026 12:14
A fork works in its source's worktree, so deleting the source removed the
fork's checkout. Delete now resolves the worktree dir from the conversation's
workspace and removes it only when no remaining conversation uses it, so the
last of a source and its forks to be deleted cleans it up.

@all-hands-bot all-hands-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This review was posted by an AI agent (OpenHands).

Reviewed head 031b68e6ff6af05b96bb20db481d4cf645857e7a against base main, with the repo AGENTS.md and .agents/skills/custom-codereview-guide.md.

Scope: this belongs in this repository (the Agent Server owns local conversation storage), and #5553 is ready-for-dev / priority:medium. The change fixes the reported bug: delete_conversation removes the conversation's worktree and prunes the source repo's git registration, keeping the openhands/<id> branch.

The prior material finding is addressed. Forks persist their source's worktree path (LocalConversation.fork passes workspace=self.workspace, and ForkConversationRequest has no workspace field), so the new conversation_worktree_dir + _conversation_records ownership check now removes the worktree only when no remaining conversation still references it, with test_a_fork_keeps_the_shared_worktree_until_it_is_deleted covering the delete-source-then-delete-fork sequence. The helper's rel.parts[0] shape is correct, including the relative-workspace case and the root.resolve() symlink handling (.resolve() on a relative working_dir is consistent with the absolute root, since it is never under root).

Verification on this head: all 30 CI checks pass; tests/agent_server/test_local_storage.py plus test_conversation_service.py and test_conversation_router.py pass (238 tests) in the workspace. No material correctness, security, or compatibility issue remains.

✅ APPROVED

@all-hands-bot
all-hands-bot requested a review from neubig October 7, 2026 11:06

@neubig neubig left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current head addresses the shared-worktree regression, includes real server and git coverage, and has no unresolved review threads or material code issues. Approved for merge. This review was submitted by an AI agent (OpenHands) on behalf of Graham Neubig.

@neubig
neubig merged commit 0bb7b52 into main Oct 8, 2026
56 of 57 checks passed
@neubig
neubig deleted the vasco/5260-delete-local-worktree branch October 8, 2026 10:37
VascoSch92 pushed a commit that referenced this pull request Oct 14, 2026
Worktree cleanup on delete landed on main (#5547), and the Docker knobs
stay env-only for now, so misc_settings.runtime has nothing left to carry.
VascoSch92 added a commit that referenced this pull request Oct 15, 2026
Worktree cleanup on delete landed on main (#5547), and the Docker knobs
stay env-only for now, so misc_settings.runtime has nothing left to carry.
VascoSch92 added a commit to rohan-patnaik/software-agent-sdk that referenced this pull request Nov 6, 2026
Deleting a Docker conversation ran `git branch -D openhands/<id>` on the
host, so commits the agent made in its worktree became unreachable. A
deleted local conversation keeps its branch (OpenHands#5547).

Reuse remove_conversation_worktree for every worktree under the
conversation's runtime worktrees dir. It keeps branches, runs git with
core.fsmonitor disabled (the repository config is agent-writable), and
also unregisters worktrees of child conversations launched with worktree
isolation, which the previous cleanup missed. The cleanup no longer needs
to rebuild the repository path or the inner server's worktree layout.
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.

[Bug]: Deleting a local conversation leaves its git worktree behind

3 participants