Skip to main content

ai-hats-wt

A hook-agnostic git-worktree engine: create, merge, and discard linked git worktrees with a layered file-locking concurrency model — from a bare git repository, with no configuration.

ai-hats-wt is the worktree core extracted from the ai-hats framework. It has no dependency on ai-hats: everything below runs against a plain git init with no ai-hats.yaml, no composition, and no tracker. Its only runtime dependencies are ai-hats-core (dependency-free filesystem primitives) and filelock.

Install

pip install ai-hats-wt

Requires Python 3.11+.

Quickstart

Drive the full create → merge (or → discard) lifecycle on any git repo:

from pathlib import Path
from ai_hats_wt import WorktreeManager

project = Path("/path/to/a/git/repo")

# A manager for one linked worktree on its own branch.
mgr = WorktreeManager(project, branch_name="feature/x")

wt_path = mgr.create()  # linked worktree checked out on feature/x
mgr.save_state()  # persist state under <project>/.wt

# ... make commits inside wt_path ...

mgr.merge()  # land the branch on the base, remove the worktree
# or:
# mgr.discard()          # throw the worktree + branch away, land nothing

That is the entire happy path. WorktreeManager defaults to the no-op lifecycle and a project-local state directory (<project>/.wt), so a bare consumer needs nothing else. The same flow is exercised end-to-end by the package's test_wt_standalone.py.

Public API

The supported surface is ai_hats_wt.__all__:

Symbol Purpose
WorktreeManager Create / merge / discard a linked worktree; the git probes.
IsolationMode Isolation / teardown mode (DISCARD, SQUASH, BRANCH, NONE).
assert_head_is_canonical_base Guard: refuse to branch off a non-canonical HEAD.
WorktreeLifecycle, LifecycleContext, NOOP_LIFECYCLE The lifecycle extension-point (see below).
WorktreeDirtyError, WorktreeCreateError, WorktreeDriftError, … Typed exceptions — the clean failure seam.

The full exception set (WorktreePartialCleanupError, WorktreeRemoveError, OriginalBranchMissingError, WorktreeStateLostError, WorktreeStateIncompleteError, WorktreeBaseBranchError, WorktreeBaseBranchMismatchError, WorktreeMainRepoMidMergeError, WorktreeTeardownAborted, WorktreeLockError) lets a host catch precise failures instead of parsing git stderr. Per-method behaviour is documented on the code — import a symbol and read its docstring; this README is the entry point, not the reference.

Lifecycle hooks (extension-point)

WorktreeManager runs no hooks by default (NOOP_LIFECYCLE). A host can inject behaviour at the create/merge/discard boundaries by passing its own WorktreeLifecycle bundle:

from ai_hats_wt import WorktreeManager, WorktreeLifecycle, LifecycleContext


class MyLifecycle(WorktreeLifecycle):
    def on_created(
        self, ctx: LifecycleContext
    ) -> None: ...  # e.g. seed files into ctx.worktree_path

    def before_teardown(
        self, event: str, ctx: LifecycleContext
    ) -> None: ...  # event is "merge" / "discard" / "cleanup"


mgr = WorktreeManager(project, branch_name="feature/x", lifecycle=MyLifecycle())

This is the seam ai-hats uses to run its own carry/hook layer; a standalone consumer typically leaves the default no-op in place.

Concurrency

Worktree operations are serialized by a layered file-locking model (state locks, git-index-lock retry, ref-lock waits) so that concurrent create / merge calls on the same repo do not corrupt each other. It is on by default; there is nothing to configure.

Dependencies

Everything else is the Python standard library.

Versioning

SemVer. The public API is ai_hats_wt.__all__; breaking changes to it bump the major version. Submodule internals (anything not in __all__) are not part of the contract.

License

MIT. See the ai-hats repository for the full license and contribution guide.

Release files for ai-hats-wt 0.6.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ai-hats-wt 0.6.0
File Size Uploaded
ai_hats_wt-0.6.0.tar.gz 63.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ai-hats-wt 0.6.0
File Interpreter ABI Platform
ai_hats_wt-0.6.0-py3-none-any.whl Python 3 none any Details

Total release size: 115.4 kB

Release files / ai_hats_wt-0.6.0.tar.gz

Download URL ai_hats_wt-0.6.0.tar.gz
Size 63.7 kB
Tags Source
SHA-256 checksum
How to use checksums
70c7946038bd90557ad3ff114559eaf9fbf63ac8f8e629e8e05d8e9b62ae03a0
BLAKE2b-256 checksum
How to use checksums
f45b529eafc24abac0ff91988fa11d448b1a66dc42d12a737c9d98af1bbb3282
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 17, 2026.

Transparency log

Release files / ai_hats_wt-0.6.0-py3-none-any.whl

Download URL ai_hats_wt-0.6.0-py3-none-any.whl
Size 51.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
88d95b9a15d88bb14651f12fcc29493f12c89c477a3a474318a6047a2ae66fdd
BLAKE2b-256 checksum
How to use checksums
57aaaf2fa079814c576a0c8a037dd695ed9199dafc36141d7b5657e1119932f6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 17, 2026.

Transparency log

Release history Release notifications | RSS feed

0.6.3

2 release files

0.6.2

2 release files

0.6.1

2 release files

This release

0.6.0 This release

2 release files

0.4.3

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page