# Worktrees The worktrees extension creates and tracks version-control checkouts for sessions and agents. Its [extension](../extensions/worktrees/src/albedo_ext_worktrees/extension.gleam) registers host calls and managed context. The work splits in two: - [`sdk/worktree`](../packages/albedo_sdk/src/albedo_sdk/worktree.gleam) is the contract: the `Provider` a version-control extension contributes with `extension.WorktreePlugin`, the `Worktree` record, the `Worktrees` record of the `worktrees` [capability](extensions.md#extension-capabilities), the `Origins` reads linked groups take, and path, file, and command helpers for providers. Joining a relative path preserves whitespace in directory names. - Core's [`worktree`](../apps/albedo/src/albedo/workspace/worktree.gleam) is the host's service: the `worktrees` table, placement and naming, provider detection, occupancy, and removal with its followers. It builds the capability and the origin reads. It keeps the edge to `observation/bus` that tells which session is running. | Function | Owner | | ---------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------- | | `Repo`, `VcsEntry`, `CreateSpec`, `Provider`, `Worktree`, `to_json` | SDK | | `Worktrees`, `Request`, `Origins`, `no_origins` | SDK: the capability's and the origin reads' types | | `join_path`, `relative_subdir`, `parent_dir`, `find_checkout`, `present`, `strip_suffix` | SDK: path helpers for providers | | `exec`, `is_dir`, `is_file`, `read_file`, `ensure_dir`, `delete_tree` | SDK: `exec` runs through the `Runner` the host hands a provider callback | | `apply`, `initialise`, `forget_session` | Core: the table, which the daemon creates at startup, and the cleanup `runtime.cleaners` runs for a deleted session | | `valid_name`, `repo_slug`, `sanitize_slug`, `base_dir`, `worktree_path`, `worktree_id` | Core: where checkouts go under albedo's home and how they are named | | `detect` | Core: the first enabled provider that claims a directory | | `create`, `create_for_agent`, `attach_session` | Core, through the capability's `create`, `create_for_agent`, and `attach_session` | | `get`, `for_session`, `for_workspace`, `list`, `remove`, `running_occupant` | Core, gated under `Sessions` (`capability.functions`). Extensions reach all but `for_workspace` through the capability | | `capability`, `origins` | Core: the capability over a session's providers and every declared extension's followers, and the origin reads | ## Layout `worktree.base_dir` places managed checkouts under `$ALBEDO_HOME/wt`. `worktree_path` adds a repository slug and a chosen name. The record distinguishes the checkout `path` from `workspace`, which preserves the origin's subdirectory within the checkout. `origin` records the originating workspace. Creation rejects remote SSH locations. ## Worktree providers A `worktree.Provider` detects a repository and implements creation, listing, and removal for its version-control system. Each callback receives the [`local_command.Runner`](../packages/albedo_sdk/src/albedo_sdk/local_command.gleam) it runs git or jj through, which the host supplies from [`local_run`](../apps/albedo/src/albedo/workspace/local_run.gleam), so a provider starts no process itself. `worktree.detect` selects a matching enabled provider. `worktree.create` creates the checkout and records it. If recording fails, it attempts to remove the checkout. A capability `Request` may name a provider `kind`, which the host matches without case. See [`worktree.gleam`](../apps/albedo/src/albedo/workspace/worktree.gleam) and the [extension dispatch](../extensions/worktrees/src/albedo_ext_worktrees/extension.gleam). ## The worktrees capability Extensions create, find, and remove worktrees through `worktrees` on `host.Context` and `extension.Preparation`, never by importing core. The record binds the session's enabled providers, and its `remove` runs every declared extension's followers. Each function takes the caller's storage handle. A worktree names the session that works in it, so the field needs `Sessions` as well as `Worktrees`, and core's reads still check `Sessions` on the handle. A denied capability answers every operation with the refusal, and `for_session` and `running_occupant` with `None`. The worktrees and agents extensions declare it. The field once held the providers list under `Providers`; the providers no longer reach extensions. ## Python The [Python binding](../python/albedo_plugins/worktrees.py) exposes asynchronous `worktrees.create`, `get`, `query`, `list`, and `remove`. A `Worktree` record includes its ID, repository, workspace, kind, revision, branch, session, and existence state. `query` supports filters. `list` returns the matching records. `worktree.remove` refuses the main checkout and paths outside the managed base. Without `force`, it refuses a worktree occupied by a running session. Removal deletes the checkout, then runs every declared extension's `WorktreeRemoved` [follower](plugins.md#daemon-contributions) after deleting the record in the same transaction. Creating a record runs `WorktreeCreated` in the transaction that inserts it. The links extension follows both changes to reconcile [linked membership](workspaces.md#linked-workspaces). The capability binds those followers. The work extension's follower moves work-ledger entries to the origin checkout. Closing an agent does not automatically delete its worktree. See [`remove`](../apps/albedo/src/albedo/workspace/worktree.gleam) and the [extension instructions](../extensions/worktrees/src/albedo_ext_worktrees/extension.gleam). ## Agents The extension accepts a worktree for agent creation, or a request to create one for the agent. The agent's workspace then points into that checkout. See the [worktrees extension](../extensions/worktrees/src/albedo_ext_worktrees/extension.gleam) and [agents](agent-sessions.md). ## Filter sessions by worktree The [`worktree` predicate](../apps/albedo/src/albedo/session/session_collection/sql/worktree_predicate.sql) matches sessions at a recorded checkout's `workspace`, its `path`, or inside that path. It also matches sessions under the daemon's managed checkout layout, `$ALBEDO_HOME/wt//`, even after removal deletes the checkout and its record. `GET /sessions?worktree=true` lists these sessions and `false` lists the others. A listing that doesn't ask for the filter is unchanged. ## Managed context For a session associated with a worktree, the [extension](../extensions/worktrees/src/albedo_ext_worktrees/extension.gleam) adds context naming the checkout and origin. Its context identifies the origin as the shared scope for memory and the work ledger. See [workspaces](workspaces.md) for locations and links. ## Related pages - [All topics](README.md) - [Change guidance](contributing.md) - [Agents](agent-sessions.md) - [Files extension](files.md) - [Plugins](plugins.md)