# feat\(server\): package and apply a thread's git work · PR \#16734 · pingdotgg/t3code

[View on GitCafe](https://git.cafe/pingdotgg/t3code/pull/16734)

Repository: [pingdotgg/t3code](https://git.cafe/pingdotgg/t3code)

Visibility: public

State: open

Source repository: pingdotgg/t3code

Source branch: t3code/peer/handoff-git

Target branch: t3code/peer/thread-import

Head commit: 75b2c90834b04ac7254bb95a713fd1c39a047384

[Discussion and reviews](https://git.cafe/pingdotgg/t3code/pull/16734)

[Commits](https://git.cafe/pingdotgg/t3code/pull/16734/commits?format=markdown)

[Files changed](https://git.cafe/pingdotgg/t3code/pull/16734/diffs?format=markdown)

## Description

---

Part of cross-environment orchestration. A handoff carries a thread's code as well as its conversation: commits not on any remote, plus uncommitted edits, deletes and new files. This PR adds the git half. The next PR wires it into the handoff.

**Pack (on the sending side)**
- **The working tree becomes a snapshot commit** on the branch tip, made in a private index the way checkpoints are, so the user's own staging is untouched. Untracked files are included; ignored files such as `.env` stay behind. The staged/unstaged split is not kept: on arrival everything shows as unstaged.
- **The bundle holds only what no remote has** (`--not --remotes`): unpushed commits plus the snapshot.
- **Size cap.** Over 50 MiB, which is one upload, the handoff is refused with "Push the branch, then hand off again."

**Apply (on the receiving side)**
- **Base commits.** If the bundle needs commits the receiving checkout lacks, it fetches `origin` and verifies again. If they're still missing, it refuses.
- **The work lands in a new worktree on the branch.** The snapshot is restored and the index reset to the tip, so the commits are history and the edits are uncommitted.
- **Existing work is never overwritten.**
  - An existing branch moves only forward, using a compare-and-swap `update-ref`.
  - A branch with commits the handoff lacks is refused as `diverged`.
  - A branch checked out in another worktree at a different commit is refused.
- **Any failing step undoes the ones before it:** the branch ref, the worktree, and the temporary handoff refs.

**Verification**
- `peer/handoff/HandoffGit.test.ts` runs on real git repos: a bare origin and two clones (the laptop and the box). The four tests:
  - **Round trip.** An unpushed commit plus a modified file, a deleted file, a new file and an ignored `.env` arrive exactly. The branch tip matches. Status is ` D old.ts`, ` M app.ts`, `?? new.ts`, and `.env` is absent. The laptop's staging and refs are unchanged after packing.
  - **Diverged branch.** When the box has its own commit on the branch, apply is refused, and the branch, worktrees and refs are exactly as before.
  - **Behind branch and rollback.** A branch behind the tip moves forward. When a later step fails because the worktree path is taken, the branch is moved back and the user's file in that path is untouched.
  - **Fetch and cap.** A base pushed after the box cloned (absent from the bundle) is fetched from origin. An incompressible file over 50 MiB is refused, and no bundle is left behind.
- Mutation-checked: each of these fails a test when removed:
  - the divergence check;
  - rollback;
  - including untracked files;
  - leaving changes uncommitted;
  - the size cap;
  - using a private index;
  - fetching a missing base.
- Server typechecks clean; lint is clean.

Opus 5.5 via Claude Code.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

<!-- devin-review-badge-begin -->

---

<a href="https://app.devin.ai/review/pingdotgg/t3code/pull/16734" target="_blank"><picture><source media="(prefers-color-scheme: dark)" srcset="https://static.devin.ai/assets/gh-devin-review-dark.svg?v=4"><img src="https://static.devin.ai/assets/gh-devin-review-light.svg?v=4" alt="Devin Review"></picture></a>
<!-- devin-review-badge-end -->
