Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
12 KiB
Worktree-Isolated Sessions — Implementation Plan
Spec: docs/superpowers/specs/2026-06-10-worktree-sessions-design.md Branch: worktree-sessions
Quality gates after every task (must pass before commit):
uv run ruff format src tests
uv run ruff check src tests
uv run ty check
uv run pytest -q
Do NOT stage src/hqt/tmux/runner.py — it carries an unrelated uncommitted
user change. Stage only files you created/modified for your task.
Task 1 — Git worktree module
Create src/hqt/git/__init__.py and src/hqt/git/worktree.py, plus
tests/test_worktree.py.
Module contents (async functions use asyncio.create_subprocess_exec, in the
style of src/hqt/tmux/runner.py; failures raise hqt.errors.ServiceError
with git's stderr included):
slugify(text: str) -> str— lowercase; keep[a-z0-9]; collapse runs of anything else into single-; strip leading/trailing-. Pure function.async is_git_repo(path: Path) -> bool—git rev-parse --is-inside-work-treeinpath; False on any failure (including missing directory).WorktreeStatedataclass:exists: bool,dirty: bool,unique_commits: int.async create_worktree(repo: Path, branch: str) -> Path:- Validate with
git check-ref-format --branch <branch>→ ServiceError "Invalid branch name: ..." on failure. - Resolve git common dir (
git rev-parse --git-common-dir, relative results resolved againstrepo); append line.worktrees/to<common-dir>/info/excludeif not already present (create file if missing). git worktree add -b <branch> <repo>/.worktrees/<branch>(cwd=repo). ServiceError with stderr on failure (branch exists, dir exists, etc.).- Return the absolute worktree path.
- Validate with
async add_worktree_for_branch(repo: Path, branch: str) -> Path—git worktree add <repo>/.worktrees/<branch> <branch>(no-b), same exclude handling, for recreating a vanished worktree.async worktree_state(repo: Path, path: Path, branch: str) -> WorktreeState—exists= path exists on disk;dirty= non-emptygit status --porcelainrun in the worktree (False if not exists);unique_commits= line count ofgit rev-list <branch> --not --all --count-style query. Usegit rev-list --count <branch> --not --exclude=refs/heads/<branch> --all... simplest correct form:git for-each-ref --format='%(refname)' refs/heads refs/remotes, drop the branch's own ref, thengit rev-list --count <branch> --not <others...>(0 when no other refs exist is acceptable only if branch tip is reachable from them; with no other refs, count commits on branch). Implement and test against a real repo; exact plumbing is the implementer's choice as long as the tests below pass.async remove_worktree(repo: Path, path: Path, branch: str, force: bool) -> None—git worktree remove [--force] <path>(ServiceError on failure), then best-effortgit branch -d <branch>(ignore failure), thengit worktree prune.
Tests (real git in tmp_path repos; configure user.name/email; create an
initial commit):
- slugify: mixed case, spaces, punctuation, leading/trailing junk, empty.
- is_git_repo: repo → True; plain dir → False; missing dir → False.
- create_worktree: creates
.worktrees/<branch>with branch checked out;.git/info/excludecontains.worktrees/; calling again with same branch raises ServiceError; invalid branch name (foo..bar) raises ServiceError; exclude not duplicated after two different worktrees. - worktree_state: fresh worktree → exists, not dirty, 0 unique commits; touch a file → dirty; commit on the branch → unique_commits == 1; after removing dir manually → exists False.
- remove_worktree: clean worktree removed, branch (merged/no unique commits)
deleted; dirty worktree without force → ServiceError and dir remains; with
force → removed; unmerged branch survives removal (
git branch -dfails silently, branch still listed).
Commit message: feat: git worktree module.
Task 2 — Session columns + migration
Modify src/hqt/db/models.py and src/hqt/db/migrations.py; extend
tests/test_db.py.
Sessiongainsworktree_path: Mapped[str | None] = mapped_column(default=None)andworktree_branch: Mapped[str | None] = mapped_column(default=None).- Add migration entry
(2, ...)toMIGRATIONSexecuting twoALTER TABLE sessions ADD COLUMNstatements (worktree_path TEXT, worktree_branch TEXT).LATEST_VERSIONderives automatically. - Tests: fresh DB has the columns and user_version 2; a DB created at
baseline (simulate: create tables via models without the new columns is
impractical — instead create a v1 DB by issuing the old CREATE TABLE
through exec_driver_sql or by building metadata then dropping the two
columns is messy; acceptable approach: stamp an existing schema to
user_version 1 after removing the columns via SQLite
ALTER TABLE ... DROP COLUMN, then runmigrate()and assert columns exist and version == 2). Follow the existing migration-test patterns intests/test_db.pyif any.
Commit message: feat: worktree columns on sessions + migration v2.
Task 3 — SessionService worktree support
Modify src/hqt/sessions/service.py; extend tests/test_sessions.py /
tests/test_services.py (match where SessionService is currently tested).
- Import the worktree module as a module (
from hqt.git import worktree) so tests can monkeypatchworktree.create_worktreeetc. create_session(self, project_id, harness_name, nickname=None, model=None, worktree_branch=None):- When
worktree_branchis falsy → behavior identical to today. - When set: after resolving
project_path,await worktree.create_worktree(project_path, worktree_branch)BEFORE adding the Session row; on ServiceError let it propagate (no row, no spawn). Storeworktree_path=str(path)andworktree_branchon the row. Spawn config must be built with the worktree path, and the capture-retry loop must also use the worktree path. - Default nickname: if
worktree_branchset and nickname blank, nickname becomes the branch name.
- When
- Add
_session_path(self, db, sess) -> Path:Path(sess.worktree_path)when set, elseself._project_path(db, sess.project_id). Use it in_respawn_with_fallback,_get_resume_config, and_maybe_capture_missing_session_id(replace direct project-path lookups for harness-facing paths;_maybe_capture_missing_session_idkeeps its "project deleted → skip" behavior for non-worktree sessions, and for worktree sessions uses the worktree path without needing the project row). attach_sessionrung 0: before the alive check... no — keep order: if session hasworktree_pathand the path does not exist on disk:- branch exists (
git rev-parse --verify refs/heads/<branch>helper in worktree module is NOT needed; useworktree.add_worktree_for_branchand catch ServiceError) → recreate, then continue normal flow. - recreate fails → raise ServiceError("Worktree and branch are gone; delete the session").
- branch exists (
delete_session(self, session_id, remove_worktree=False, force=False):- Kill tmux window as today.
- If
remove_worktreeand session has worktree:await worktree.remove_worktree(project_path, Path(sess.worktree_path), sess.worktree_branch, force). On ServiceError: do NOT delete the DB row; re-raise (the TUI shows the notification; the tmux window being already killed is acceptable). - Then delete row + commit.
- Expose
async worktree_state_for(self, session_id) -> WorktreeState | Nonereturning None for non-worktree sessions; the TUI uses it to build the confirm dialog.
Tests (fake tmux pattern as existing tests; monkeypatch worktree functions with AsyncMock/recording fakes):
- create_session with worktree_branch: create_worktree called with project path and branch; spawn cwd == worktree path; capture uses worktree path; row has worktree fields; nickname defaults to branch.
- create_worktree raising → ServiceError propagates, no Session row exists, no spawn attempted.
- attach with worktree_path missing on disk → add_worktree_for_branch called; on its failure → ServiceError with the delete-hint message.
- resume config for a worktree session uses worktree path.
- delete_session remove_worktree=True calls worktree.remove_worktree and deletes row; worktree.remove_worktree raising → row still present.
- worktree_state_for: None for plain sessions; delegates for worktree ones.
Commit message: feat: worktree-aware session lifecycle in SessionService.
Task 4 — TUI wiring + docs
Modify src/hqt/tui/screens/new_session.py, add
src/hqt/tui/screens/confirm_delete.py, modify src/hqt/tui/app.py,
src/hqt/tui/widgets/session_list.py, src/hqt/status.py callers if
needed, README.md; extend tests/test_tui.py.
NewSessionScreen(harness_names, is_git_repo):- Result type becomes
tuple[str, str, str | None, str | None] | None(harness, nickname, model, worktree_branch). - Add
Checkbox("Isolate in worktree", id="worktree-checkbox")(textualCheckbox); whenis_git_repois False,disabled=Trueand label "Isolate in worktree (not a git repo)". - Branch
Input(id="branch-input")hidden (display = False) until the checkbox is checked; when first revealed, prefill withslugify(nickname)if the field is empty; user edits freely afterwards. - On submit with checkbox checked: empty branch → block submit and focus the branch field (or fall back to slugified nickname if non-empty; if both empty, keep dialog open). Unchecked → worktree_branch None.
- Result type becomes
ConfirmDeleteScreen(ModalScreen[tuple[bool, bool] | None])in new file: constructor takessession_label: str,warning: str | None. Shows "Delete session ?", optional warning line,Checkbox("Also remove worktree", value=warning is None), Delete/Cancel buttons. Returns(confirmed, remove_worktree);remove_worktree and warning is not Nonemeans force.app.py:action_new_session: computeis_git_repoviaawait worktree.is_git_repo(Path(project.path))(inside the worker or pre-fetched; ProjectService has the path — follow existing access patterns), pass to screen; passworktree_branchtocreate_session.action_delete_session: fetch session; if it has no worktree, keep today's immediate delete. Otherwise callsessions.worktree_state_for(sid), build warning string when dirty or unique_commits > 0 ("N uncommitted file(s), M unmerged commit(s) — work will be lost"), push ConfirmDeleteScreen; on (True, remove) calldelete_session(sid, remove_worktree=remove, force=remove and warned). ServiceError already surfaces via_run_service_worker.
- Session list: rows for worktree sessions append
⎇ <branch>; checksrc/hqt/tui/widgets/session_list.pyfor the row-label construction. - tmux window label: in
sync_window_labels, the name passed towindow_labelfor worktree sessions isnickname or branch(nickname already defaults to branch at creation, so no change may be needed — verify and leave a test). - README: document the worktree checkbox under Key Bindings/feature notes,
and the
.worktrees/location + submodule limitation. - Tests: NewSessionScreen returns worktree_branch when checked, None when unchecked, disabled checkbox when not a repo; ConfirmDeleteScreen returns the right tuples; app delete path for non-worktree sessions unchanged (no modal).
Commit message: feat: worktree session UI (dialog checkbox, confirm delete, list marker).