Skip to content

fix(server): fall back when new worktrees are unavailable - #6208

Merged
maria-rcks merged 3 commits into
pingdotgg:mainfrom
tris203:t3code/fix-unversioned-worktree-thread
Sep 13, 2026
Merged

fix(server): fall back when new worktrees are unavailable#6208
maria-rcks merged 3 commits into
pingdotgg:mainfrom
tris203:t3code/fix-unversioned-worktree-thread

Conversation

@tris203

@tris203 tris203 commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

Problem

When New threads defaults to New worktree, the first message can carry one atomic bootstrap request containing both createThread and prepareWorktree.

Two project states cannot satisfy that request:

  1. A directory that is not a Git repository has no worktree source.
  2. A freshly initialized Git repository has an unborn HEAD: Git reports a branch name, but no commit exists for git worktree add to branch from.

The server previously persisted thread.create before attempting git worktree add. When worktree creation failed, cleanup deleted the new thread, but event sourcing intentionally retained its ID as a tombstone. The client draft retained that same ID for retry. This produced a confusing sequence:

  • First send: worktree creation fails, sometimes without a useful visible error.
  • Second send: Orchestration command invariant failed (thread.create): Thread '<id>' already exists and cannot be created twice.

This could depend on client-side Git status timing, and server bootstrap requests from every client trusted the requested worktree mode without an authoritative preflight.

Fix

The server now preflights worktree bootstrap before persisting the thread:

  • Detect whether the target directory is a Git repository.
  • Resolve the requested local or origin base ref.
  • Verify that the resolved ref names a commit with git rev-parse --verify <ref>^{commit}.
  • Fall back to the project checkout when the directory is not a repository or the base has no commit.
  • Preserve the existing worktree creation and setup-script path when a valid base commit exists.

Running the preflight before thread.create prevents deterministic repository/base-ref failures from leaving a thread tombstone behind.

Behavior

Project state Requested mode Result
Non-Git directory New worktree Thread starts in the project checkout
Git repository with unborn HEAD New worktree Thread starts in the project checkout
Git repository with a committed base ref New worktree A new worktree is created
Git repository with a resolvable origin base New worktree + Start from origin Origin is fetched and the worktree is based on the resolved commit

Reproduction

  1. Set Settings → General → New threads to New worktree.
  2. Add either a plain directory or a directory containing only git init with no commits.
  3. Create a new thread and send its first message.
  4. Before this change, the worktree command failed; retrying the same draft then hit the duplicate-thread invariant.

Verification

  • pnpm exec vp test run apps/server/src/server.test.ts — 126 tests passed.
  • pnpm --filter t3 typecheck — passed.
  • Manual isolated-app verification covered:
    • a non-Git directory;
    • a Git repository with no commits;
    • a Git repository with an initial commit, confirming the positive worktree path remains intact.

Generated with GPT-5.6 Sol in T3 Code using the Codex harness.


Note

Medium Risk
Changes first-message thread bootstrap and Git preflight behavior on the server; mistakes could skip worktrees or mis-route cwd, but fallbacks are narrow and covered by new integration tests.

Overview
Bootstrap worktree creation is now preflighted before thread.create, so failed worktree setup no longer leaves a thread tombstone that blocks retries with “cannot be created twice.”

GitWorkflowService gains isRepository (VCS detect → Git only) and hasCommit (git rev-parse --verify <ref>^{commit} with non-zero exit treated as false). In the WebSocket bootstrap path, origin/base resolution still runs when applicable, but createWorktree runs only when the project cwd is a Git repo and the resolved base ref points at a commit. Otherwise the flow creates the thread and starts the turn against the project checkout (no worktree meta update / setup on a missing worktree path).

Tests cover the new Git workflow helpers and two server integration cases: non-repository project cwd and unborn-head / missing base commit, plus stubs so existing bootstrap tests satisfy the new execute / work-tree checks.

Reviewed by Cursor Bugbot for commit 4c83fbf. Bugbot is set up for automated code reviews on this repo. Configure here.

Note

Skip worktree creation in bootstrapProgram for non-Git repos and missing commits

  • Adds GitWorkflowService.isRepository and GitWorkflowService.hasCommit to detect whether a path is a Git repository and whether a ref resolves to a commit.
  • makeWsRpcLayer.bootstrapProgram in ws.ts now runs these checks before worktree preparation. For non-Git projects or base refs with no commit, it skips origin lookup, remote-base resolution, and worktree creation, then still dispatches the thread and turn.
  • Adds server-router integration tests covering the non-repository and no-commit fallback paths, plus a successful Git execution fixture.
  • Behavioral Change: bootstrap for non-Git projects or invalid base refs no longer attempts worktree creation; Git repositories with a valid base are unaffected.

Macroscope summarized 4c83fbf.

@github-actions github-actions Bot added vouch:unvouched PR author is not yet trusted in the VOUCHED list. size:M 30-99 changed lines (additions + deletions). labels Aug 11, 2026
@tris203
tris203 force-pushed the t3code/fix-unversioned-worktree-thread branch from c7f938b to e85c7ed Compare August 13, 2026 10:50
@coderabbitai

coderabbitai Bot commented Aug 13, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 3445ff56-6676-4abe-9702-95850589711e

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@tris203
tris203 force-pushed the t3code/fix-unversioned-worktree-thread branch from e85c7ed to 4c9498b Compare August 23, 2026 08:28

@cursor cursor Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 4c9498be6eeb2e6f47537752478757871168173c. Configure here.

Comment thread apps/server/src/server.test.ts
@macroscopeapp

macroscopeapp Bot commented Aug 23, 2026

Copy link
Copy Markdown
Contributor

Approvability

Verdict: Approved at 4c83fbf

Macroscope's review found this PR approvable — This is a localized server bug fix that preflights repository and commit availability before creating a thread, preventing failed worktree setup from leaving unusable thread state. Valid worktree flows remain intact, and the fallback cases are covered by integration tests.

You can add or adjust custom eligibility rules. Learn more.

@t3dotgg t3dotgg added vouch:trusted PR author is trusted by repo permissions or the VOUCHED list. and removed vouch:unvouched PR author is not yet trusted in the VOUCHED list. labels Aug 24, 2026
@tris203
tris203 force-pushed the t3code/fix-unversioned-worktree-thread branch from 210bcc5 to f877402 Compare September 4, 2026 15:31
Comment thread apps/server/src/ws.ts
@github-actions github-actions Bot added size:L 100-499 changed lines (additions + deletions). and removed size:M 30-99 changed lines (additions + deletions). labels Sep 4, 2026
@maria-rcks
maria-rcks merged commit dd6ba84 into pingdotgg:main Sep 13, 2026
25 checks passed
@tris203
tris203 deleted the t3code/fix-unversioned-worktree-thread branch September 13, 2026 17:33
github-actions Bot added a commit to omarcresp/t3code-flake that referenced this pull request Sep 13, 2026
## What's Changed
* fix(web): offer recovery from missing pages by @shivamhwp in pingdotgg/t3code#11314
* fix(web): retry startup after the server recovers by @shivamhwp in pingdotgg/t3code#11291
* feat(web): add optional compact sidebar rail by @maria-rcks in pingdotgg/t3code#11525
* feat(web): add opt-in in-app thread notifications by @Bil0000 in pingdotgg/t3code#11570
* feat(web): organize connections by environment by @maria-rcks in pingdotgg/t3code#11542
* fix(web): keep sparse sidebar shelves at the bottom by @maria-rcks in pingdotgg/t3code#11595
* fix(cursor): preserve internal agent errors without transport labels by @shivamhwp in pingdotgg/t3code#11365
* fix(server): fall back when new worktrees are unavailable by @tris203 in pingdotgg/t3code#6208
* feat: badge background thread notifications on desktop and web by @Bil0000 in pingdotgg/t3code#11569
* feat(web): add compact thread list mode by @saphid in pingdotgg/t3code#9417


**Full Changelog**: pingdotgg/t3code@v0.0.41-nightly.20260913.1658...v0.0.41-nightly.20260913.1675

Upstream release: https://github.com/pingdotgg/t3code/releases/tag/v0.0.41-nightly.20260913.1675
faw01 pushed a commit to faw01/t3code that referenced this pull request Sep 14, 2026
…6208)

Preflight repository and base commit availability before creating the thread. Fall back to the project checkout for non-Git directories and repositories without a usable base commit, while preserving valid worktree setup.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:L 100-499 changed lines (additions + deletions). vouch:trusted PR author is trusted by repo permissions or the VOUCHED list.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants