Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ https://github.com/DanHenton/opencode-worktree/main/docs/install.

For other install methods (Go, manual download, PATH troubleshooting), see the [full install guide](docs/install.md).

For command-by-command behavior, examples, and edge cases, see the [command reference](docs/commands/README.md).

## Usage

> **Shortcut:** The installer adds `ocwt` as a shell alias for `opencode-worktree` — use either interchangeably.
Expand Down
20 changes: 20 additions & 0 deletions docs/commands/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Command Reference

Command-level reference for `opencode-worktree`. Each page follows the same structure so a human or an agent can understand behavior, inputs, and edge cases without reading the Go code.

## Commands

- [`task`](task.md) — create a new agent worktree and launch OpenCode
- [`attach`](attach.md) — reopen an existing agent worktree session
- [`merge`](merge.md) — merge an agent branch back into its parent branch
- [`sync`](sync.md) — rebase an agent branch onto the latest parent branch
- [`list`](list.md) — show active agent worktrees
- [`cleanup`](cleanup.md) — remove orphaned worktrees and stale agent branches

## Shared conventions

- Agent branches are named `agent/<task-name>`.
- Agent worktrees are created as sibling directories named `<repo>-agent-<task-name>`.
- Managed worktrees contain `.agent-parent-branch` and `.agent-context` marker files.
- The installer adds `ocwt` as a shell alias for `opencode-worktree`.
- Run `opencode-worktree <command> --help` for built-in CLI help.
60 changes: 60 additions & 0 deletions docs/commands/attach.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# `opencode-worktree attach`

## Summary

Reopens an existing agent worktree session by task name, launches OpenCode inside that worktree again, and by default merges the branch when the session exits.

## Usage

```bash
opencode-worktree attach <name> [--no-merge]
```

## Arguments

- `<name>` — required task name for an already-existing agent worktree.

## Options

- `--no-merge` — skip the automatic merge step after the OpenCode session ends.

## What it does

1. Confirms you are inside the source git repository.
2. Finds the active worktree that matches `agent/<name>`.
3. Launches `opencode` inside that worktree.
4. Unless `--no-merge` is set, runs the same merge-and-cleanup flow used by `task`.

## When to use it

Use `attach` when a task already has a live worktree and you want to continue that session instead of creating a second worktree.

## Examples

```bash
# Reopen an existing session
opencode-worktree attach fix-auth-bug

# Reopen without auto-merging on exit
opencode-worktree attach fix-auth-bug --no-merge
```

## Important behavior

- `attach` does not create a branch or worktree. It only reconnects to one that already exists.
- Task lookup is based on active git worktree metadata, not directory name guessing.
- If the named task does not exist, the command exits with an error.
- The same merge rules as `task` apply after OpenCode exits.

## Common failure cases

- Not inside the source git repository.
- No active worktree exists for the requested task name.
- Missing `opencode` binary.
- Merge conflicts during auto-merge.

## Related commands

- [`task`](task.md) to create a new session.
- [`list`](list.md) to discover active task names.
- [`merge`](merge.md) to merge manually later.
61 changes: 61 additions & 0 deletions docs/commands/cleanup.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# `opencode-worktree cleanup`

## Summary

Removes orphaned agent worktree directories and stale `agent/*` branches that no longer correspond to an active git worktree.

## Usage

```bash
opencode-worktree cleanup [--dry-run] [--yes]
```

## Arguments

This command does not take positional arguments.

## Options

- `--dry-run` — show what would be removed without deleting anything.
- `--yes` — skip the confirmation prompt.

## What it does

1. Prunes stale git worktree metadata.
2. Scans sibling directories for names matching the tool's worktree naming pattern.
3. Compares those directories against active git worktrees and marks unmatched ones as orphaned.
4. Scans local branches for `agent/*` branches that are no longer attached to an active worktree.
5. In normal mode, prompts for confirmation when running interactively unless `--yes` is set.
6. Removes orphaned directories and attempts to delete orphaned branches.

## When to use it

Use `cleanup` when a worktree directory or agent branch is left behind after manual interruption, failed cleanup, or other unexpected session endings.

## Examples

```bash
# Review what would be removed
opencode-worktree cleanup --dry-run

# Remove orphaned items without an interactive prompt
opencode-worktree cleanup --yes
```

## Important behavior

- Active worktree directories and active `agent/*` branches are preserved.
- In `--dry-run` mode, nothing is removed.
- Without `--yes`, interactive terminals get a confirmation prompt.
- If a stale branch cannot be deleted because it is unmerged, the command leaves it in place and prints the `git branch -D` command to force deletion manually.

## Common failure cases

- Running outside a git repository.
- File-system permission failures while removing directories.
- Branch deletion blocked because the branch is not fully merged.

## Related commands

- [`list`](list.md) to inspect active sessions before cleaning.
- [`merge`](merge.md) if a worktree is still valid and should be merged instead of deleted.
51 changes: 51 additions & 0 deletions docs/commands/list.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# `opencode-worktree list`

## Summary

Shows active managed agent worktrees for the current repository.

## Usage

```bash
opencode-worktree list
```

## Arguments

This command does not take positional arguments.

## Options

This command has no flags.

## What it does

1. Confirms you are inside a git repository.
2. Reads `git worktree list` output for the repository.
3. Filters the result down to worktrees whose branch name starts with `agent/`.
4. Marks entries with `(uncommitted changes)` when the worktree contains changes outside the tool's marker/config files.

## When to use it

Use `list` to see which agent sessions are still active and to find task names you can pass to `attach`.

## Examples

```bash
opencode-worktree list
```

## Important behavior

- If no managed agent worktrees exist, the command prints `(none)`.
- The output is informational and does not modify anything.
- Dirty-state detection ignores `.agent-parent-branch`, `.agent-context`, `opencode.json`, and `.opencode/`.

## Common failure cases

- Running outside a git repository.

## Related commands

- [`attach`](attach.md) to reopen a listed session.
- [`cleanup`](cleanup.md) to remove stale sessions that are no longer active.
68 changes: 68 additions & 0 deletions docs/commands/merge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# `opencode-worktree merge`

## Summary

Merges an agent branch back into its recorded parent branch and optionally removes the agent worktree and branch afterward.

## Usage

```bash
opencode-worktree merge [path] [--no-cleanup]
```

## Arguments

- `[path]` — optional path to an agent worktree. If omitted, the command tries to detect the current directory as a managed agent worktree.

## Options

- `--no-cleanup` — keep the worktree and branch after a successful merge.

## What it does

1. Resolves the target worktree path, either from the argument or by auto-detecting the current directory.
2. Reads `.agent-parent-branch` to find the branch the agent work should merge back into.
3. Confirms the current worktree branch starts with `agent/`.
4. Counts commits that exist on the agent branch but not the parent branch.
5. Checks whether the worktree has uncommitted changes, excluding marker and copied config files.
6. Takes a file lock before switching the main repo to the parent branch and merging, so concurrent merges do not race.
7. On success, optionally removes the worktree, deletes the agent branch, and prunes stale worktree metadata.

## When to use it

Use `merge` when you skipped auto-merge, when you want to merge from a specific worktree manually, or when you need to retry after resolving issues.

## Examples

```bash
# Merge from the current agent worktree
opencode-worktree merge

# Merge a specific worktree from anywhere
opencode-worktree merge /path/to/worktree

# Merge but keep the worktree and branch around
opencode-worktree merge --no-cleanup
```

## Important behavior

- If the worktree has no new commits, the command skips the merge itself.
- If there are no new commits and no uncommitted changes, cleanup still happens by default.
- If the worktree has uncommitted changes, the worktree is preserved even when cleanup is enabled.
- Merge conflicts are detected, the merge is aborted automatically, and conflicting files are listed.
- The command only works for managed agent worktrees that contain `.agent-parent-branch` and are on an `agent/*` branch.

## Common failure cases

- Running without a path from a directory that is not an agent worktree.
- Missing `.agent-parent-branch` marker.
- Worktree is on a non-agent branch.
- Merge conflicts between the parent branch and the agent branch.
- Cleanup failing after a successful merge.

## Related commands

- [`task`](task.md) and [`attach`](attach.md) for auto-merge on exit.
- [`sync`](sync.md) to rebase the agent branch onto the latest parent branch before merging.
- [`cleanup`](cleanup.md) to remove leftover worktrees or branches later.
63 changes: 63 additions & 0 deletions docs/commands/sync.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# `opencode-worktree sync`

## Summary

Rebases an agent branch onto the latest version of its parent branch so the worktree picks up upstream changes before merge time.

## Usage

```bash
opencode-worktree sync [path]
```

## Arguments

- `[path]` — optional path to an agent worktree. If omitted, the command tries to detect the current directory as a managed agent worktree.

## Options

This command has no flags.

## What it does

1. Resolves the target worktree path, either from the argument or by auto-detecting the current directory.
2. Reads `.agent-parent-branch` to determine which parent branch to sync against.
3. Confirms the worktree is on an `agent/*` branch.
4. Refuses to continue if the worktree has uncommitted changes.
5. Checks whether the parent branch has moved ahead of the branch point.
6. If needed, rebases the agent branch onto the parent branch.
7. If a conflict occurs, aborts the rebase and reports the conflicting files.

## When to use it

Use `sync` when the parent branch has new commits and you want to integrate them into the agent worktree before finishing or merging.

## Examples

```bash
# Sync from inside an agent worktree
opencode-worktree sync

# Sync a specific worktree from anywhere
opencode-worktree sync /path/to/worktree
```

## Important behavior

- If the parent branch has no new commits, the command reports that the worktree is already up to date.
- `sync` is stricter than `merge`: it refuses to run if the worktree has uncommitted changes.
- Rebase conflicts are aborted automatically so the worktree is not left mid-rebase.
- The command only works for managed agent worktrees with a valid `.agent-parent-branch` marker.

## Common failure cases

- Running without a path from a directory that is not an agent worktree.
- Missing or empty `.agent-parent-branch` marker.
- Worktree is dirty.
- Worktree is on a non-agent branch.
- Rebase conflicts while replaying agent commits onto the parent branch.

## Related commands

- [`merge`](merge.md) to finish the session after syncing.
- [`task`](task.md) to start a new synced-off-current-branch session.
Loading