Skip to content

fix(wt create): keep worktree when init script fails; add typed exit code - #7

Merged
sahil-noon merged 3 commits into
mainfrom
260516-g5e7-wt-create-init-failure-dx
May 16, 2026
Merged

sahil-noon merged 3 commits into
mainfrom
260516-g5e7-wt-create-init-failure-dx

Conversation

@sahil-noon

Copy link
Copy Markdown
Collaborator

Meta

ID Type Confidence Plan Review
g5e7 fix 5.0/5.0 71/71 tasks, 51/51 acceptance ✓ ✓ 1 cycle

Pipeline: intake ✓ → spec ✓ → apply ✓ → review ✓ → hydrate ✓ → ship → review-pr

Impact: +984/−70 code (excluding fab/, docs/) · +2137/−76 total

Summary

Today, when wt create <branch> runs the init script and the script exits non-zero, the user loses their worktree, their branch, and any fetched refs — and gets a generic two-line error pointing them nowhere. All git operations succeeded; only the user-supplied init script failed. This change treats init-script failure as a kept-worktree event with a structured banner, a typed exit code, a unified resolver, and SIGINT semantics that only stop the init child. No opt-in flag, no backward-compat shim — the prior rollback-on-init-failure is treated as a bug, not a configurable preference.

Changes

  • Keep the worktree on init failure — disarm the rollback instead of executing it; worktree, branch, and fetched refs survive (src/cmd/wt/create.go).
  • New init-failure banner — structured output with extracted exit status, absolute worktree path, retry hint (cd <path> && wt init), and remove hint (wt delete <name>).
  • Unify the init-protocol resolver — ResolveInitInvocation + InitNotFound typed struct replace duplicated lookup logic between wt init and wt create. Same verbose not-found warning at both call sites.
  • New exit code ExitInitFailed = 7 — appended after ExitTmuxWindowError = 6; no renumbering. Operators can now distinguish "worktree exists, init didn't complete" from other failures.
  • SIGINT during init kills only the init process — Option B reinstall handler after worktree creation + Setpgid process-group signaling. Phase-A (git ops) SIGINT still rolls back as before.

Also bundled: WT_TEST_NO_LAUNCH=1 test seam in OpenInApp (default ON in runWt) to prevent test runs from leaking real VSCode/Cursor/iTerm windows.

Artifacts

sahil87 added 2 commits May 16, 2026 16:28
When the worktree init script (default `fab sync`, overridable via
WORKTREE_INIT_SCRIPT) exits non-zero, `wt create` previously executed
the rollback — destroying the just-created worktree, branch, and any
fetched refs — and exited with the generic ExitGeneralError (1). The
banner pointed nowhere useful and operators could not distinguish init
failure from other generic errors.

This change treats init-script failure as a kept-worktree event. All
five interlocking DX problems land together so the failure path is
internally consistent:

- create.go disarms the rollback on init failure (both auto-init and
  prompted branches). The worktree, branch, and fetched refs survive.
- New structured banner prints the extracted exit status, absolute
  worktree path, retry hint (`cd <path> && wt init`), and remove hint
  (`wt delete <name>`).
- New typed exit code ExitInitFailed = 7 (appended after
  ExitTmuxWindowError = 6 — no renumbering).
- Unified resolver: ResolveInitInvocation + InitNotFound typed struct
  replace duplicated lookup logic between `wt init` and `wt create`.
  Both call sites now print the same verbose not-found warning.
- SIGINT during init kills only the init child via reinstalled handler
  (Option B) + Setpgid process-group signaling, then routes through
  the kept-worktree code path. Phase-A (git ops) SIGINT still rolls
  back as before.
- OpenInApp gains a WT_TEST_NO_LAUNCH=1 test seam (default ON in
  runWt) so test runs no longer leak real VSCode/Cursor/iTerm windows.

This is a behavior-changing default fix — no opt-in flag, no
backward-compat shim. Treated as a bug, not a configurable preference.

Spec: docs/specs/init-protocol.md (Script failure semantics rewritten)
Memory: docs/memory/wt-cli/init-failure-contract.md (new)

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR changes wt create init-failure handling so successful git worktree creation is preserved when the user-supplied init script fails, while adding clearer diagnostics, a typed exit code, shared init resolution, and safer test behavior around external app launches.

Changes:

  • Keeps newly created worktrees/branches on init-script failure and exits with ExitInitFailed = 7.
  • Adds a shared ResolveInitInvocation path for wt init and wt create, plus structured not-found warnings and an init-failure banner.
  • Adds SIGINT handling for the init phase, integration/unit coverage, documentation, memory notes, and WT_TEST_NO_LAUNCH test isolation.

Reviewed changes

Copilot reviewed 26 out of 26 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
src/internal/worktree/init.go Adds shared init resolver and typed not-found rendering.
src/internal/worktree/init_windows.go Adds Windows no-op process-group helper.
src/internal/worktree/init_unix.go Adds Unix Setpgid setup for init child process groups.
src/internal/worktree/init_test.go Adds resolver and warning tests.
src/internal/worktree/errors.go Adds ExitInitFailed and init-failure banner.
src/internal/worktree/errors_test.go Adds banner and exit-code tests.
src/internal/worktree/crud.go Refactors setup execution through the shared resolver and observer hook.
src/internal/worktree/apps.go Adds WT_TEST_NO_LAUNCH guard for tests.
src/internal/worktree/apps_test.go Tests the no-launch seam.
src/cmd/wt/testutil_test.go Defaults test runs to no-launch mode.
src/cmd/wt/signal_windows.go Adds Windows no-op signal helper.
src/cmd/wt/signal_unix.go Adds Unix process-group SIGINT delivery.
src/cmd/wt/open_test.go Updates default-app expectations for no-launch mode.
src/cmd/wt/integration_test.go Adds init-failure and SIGINT integration coverage.
src/cmd/wt/init.go Switches wt init to the shared resolver.
src/cmd/wt/init_test.go Adds canonical warning consistency tests.
src/cmd/wt/create.go Keeps worktree on init failure and installs init-phase signal handling.
src/cmd/wt/create_test.go Adds create init-failure tests and no-launch assertions.
docs/specs/init-protocol.md Documents shared resolution and new failure semantics.
docs/memory/index.md Adds wt-cli memory index entry.
docs/memory/wt-cli/init-failure-contract.md Captures the new init-failure behavior contract.
fab/changes/260516-g5e7-wt-create-init-failure-dx/intake.md Adds change intake artifact.
fab/changes/260516-g5e7-wt-create-init-failure-dx/spec.md Adds implementation spec artifact.
fab/changes/260516-g5e7-wt-create-init-failure-dx/plan.md Adds task and acceptance plan artifact.
fab/changes/260516-g5e7-wt-create-init-failure-dx/.status.yaml Adds change status metadata.
fab/changes/260516-g5e7-wt-create-init-failure-dx/.history.jsonl Adds change history metadata.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/internal/worktree/init_test.go Outdated
Comment on lines +50 to +52
sysAttr, ok := any(cmd.SysProcAttr).(*syscall.SysProcAttr)
if !ok || !sysAttr.Setpgid {
t.Error("expected SysProcAttr.Setpgid = true on Unix")
Comment thread src/internal/worktree/errors.go Outdated
fmt.Fprintf(os.Stderr, " %s%s%s %s %s\n",
ColorBold, bannerLabelWorktree, ColorReset, wtPath, bannerKeptNote)
fmt.Fprintf(os.Stderr, " %s%s%s cd %s && wt init\n",
ColorBold, bannerLabelRetry, ColorReset, wtPath)
Comment thread src/cmd/wt/create.go Outdated
Comment on lines +273 to +277
mode := "force"
if !(nonInteractive || branchArg == "") {
mode = "" // prompt for init in interactive existing-branch flow
}
if err := wt.RunWorktreeSetupWithObserver(wtPath, mode, initScript, ctx.RepoRoot, captureInit); err != nil {
Comment thread src/cmd/wt/integration_test.go Outdated
Comment on lines +310 to +312
// Put the wt process in its own group so killing the group reaps any
// orphans if the test fails partway.
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
Comment on lines +52 to +53
cmd := exec.Command("sh", "-c", fmt.Sprintf("exit %d", status))
err := cmd.Run()
Comment on lines +122 to +123
(warn-but-continue) rather than a gate. `wt init` also exits with
`ExitInitFailed` when the init script it located exits non-zero.
Six findings from Copilot review on PR #7:

1. Windows compile break (init_test.go, integration_test.go):
   syscall.SysProcAttr.Setpgid does not exist on Windows; the
   runtime.GOOS guard fires too late, after compilation has already
   failed. Extracted Setpgid assertions and the SIGINT integration
   test into build-tagged files (init_unix_test.go,
   init_windows_test.go, integration_sigint_unix_test.go).

2. Banner retry hint not shell-safe: cd <path> && wt init broke when
   the worktree path contained spaces ('/tmp/my repo/wt' -> /tmp/my).
   Now wrapped in single quotes via shellQuoteSingle for both path and
   name. Added regression test TestPrintInitFailureBanner_PathWithSpaces.

3. SIGINT handler installed before interactive confirmation prompt:
   in the interactive existing-branch flow, RunWorktreeSetup's
   ConfirmYesNo ran AFTER the signal swap, so Ctrl-C during the prompt
   was consumed by the init handler with no init child to target
   (deadlock). Hoisted the prompt up to cmd/wt/create.go so it runs
   under the original rollback handler; signal swap happens only after
   the prompt is accepted, with no I/O between. Dropped the now-unused
   'mode' parameter from RunWorktreeSetup{,WithObserver}.

4. errors_test.go's makeExitError depended on 'sh' being on PATH,
   making the test fail on Windows / minimal containers. Added a
   t.Skipf when exec.LookPath("sh") fails — the seam being tested
   (banner shape) is OS-agnostic; only the fixture needs sh.

5. init-protocol.md documented wt init as exiting ExitInitFailed but
   the implementation returned an error to Cobra which mapped to
   ExitGeneralError = 1. Made runInitScript exit os.Exit(ExitInitFailed)
   directly on init-script failure, matching the documented contract
   and wt create's behavior.

6. Updated memory file docs/memory/wt-cli/init-failure-contract.md to
   reflect the prompt hoisting and wt init exit-code change.

All tests pass on Unix; Windows test compile is clean.
@sahil-noon
sahil-noon marked this pull request as ready for review May 16, 2026 11:52
@sahil-noon
sahil-noon merged commit 99f259b into main May 16, 2026
@sahil-noon
sahil-noon deleted the 260516-g5e7-wt-create-init-failure-dx branch May 16, 2026 11:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants