Skip to content

refactor(wt-open): generalize to any directory; formalize launcher contract - #2

Merged
sahil-noon merged 2 commits into
mainfrom
260508-evbf-wt-open-any-directory
May 8, 2026
Merged

sahil-noon merged 2 commits into
mainfrom
260508-evbf-wt-open-any-directory

Conversation

@sahil-noon

Copy link
Copy Markdown
Collaborator

Summary

  • Loosens wt open's precondition: replaces the hard ValidateGitRepo() fail with soft git-context detection so the command works on any existing directory (path arg, no-args cwd) while preserving every prior in-repo branch.
  • Formalizes the previously-implicit launcher contract in a new docs/specs/launcher-contract.md: documents WT_CD_FILE, WT_WRAPPER, exit-code semantics, stability guarantees, and directory-only non-goals.
  • hop's existing subprocess delegation via WT_CD_FILE/WT_WRAPPER is unchanged — it continues to work and is now backed by a stable, tested spec rather than implicit producer/consumer comments.
  • Backwards compatibility: every prior wt open invocation that worked before this change behaves identically afterward (verified by unchanged in-repo test paths and integration tests).

Test plan

  • All existing tests pass (go test ./... in src/)
  • New tests exercise non-git path/no-args/name-arg branches
  • Integration test exercises WT_CD_FILE+WT_WRAPPER contract end-to-end against non-git tempdir
  • Tab-name backwards compat preserved for in-repo invocations
  • Exit-code mapping per launcher-contract.md §5 (sentinel error distinguishes not-found from git-failure)

…ntract

Loosens wt open's git-repo precondition so it works on any directory,
making it the canonical directory launcher in the toolchain. hop's
existing subprocess delegation via WT_CD_FILE/WT_WRAPPER continues to
work and is now formally documented as a stable contract in
docs/specs/launcher-contract.md.

Changes:
- src/cmd/wt/open.go: replace hard ValidateGitRepo() fail with soft
  detection; add path-from-non-git, no-args-cwd, name-from-non-git
  branches; introduce errWorktreeNotFound sentinel so name-not-found
  maps to ExitGeneralError while real git failures map to ExitGitError
- src/internal/worktree/apps.go: tabName(repoName, wtName) helper for
  byobu/tmux tab/session naming, falling back to wtName alone in
  non-git invocations
- docs/specs/launcher-contract.md (new): formal contract documenting
  WT_CD_FILE, WT_WRAPPER, exit codes, and stability guarantees
- docs/specs/cli-surface.md: updated wt open entry per loosened
  precondition; cross-references launcher-contract.md
- Tests: 4 new unit tests + 1 integration test exercising the contract
  end-to-end against a non-git tempdir; all 6 pre-existing wt-open
  tests continue to pass

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

Refactors wt open so it can launch any existing directory (even outside a git repo) while preserving the existing in-repo worktree behavior, and formalizes the subprocess launcher contract (WT_CD_FILE / WT_WRAPPER) in documentation with new tests.

Changes:

  • Loosened wt open’s hard git-repo precondition into soft git-context detection, adding non-git fallbacks (open cwd / accept path args).
  • Introduced tabName(repoName, wtName) to preserve historical tmux/byobu naming while avoiding a leading dash for non-git invocations.
  • Added a formal launcher contract spec plus unit/integration coverage for the new non-git branches and env-var contract.

Reviewed changes

Copilot reviewed 13 out of 13 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/internal/worktree/apps.go Adds tabName helper and uses it for tmux/byobu naming when repo context may be absent.
src/internal/worktree/apps_test.go Adds unit test coverage for tabName.
src/cmd/wt/open.go Refactors argument/context resolution to allow non-git operation and adds sentinel error for “worktree not found” vs git failure.
src/cmd/wt/open_test.go Adds tests for non-git no-args/path/name behavior and for in-repo “not found” exit-code mapping.
src/cmd/wt/integration_test.go Adds end-to-end integration test for WT_CD_FILE + WT_WRAPPER contract in a non-git temp dir.
docs/specs/launcher-contract.md New spec documenting subprocess launcher contract, env vars, exit-code semantics, and stability guarantees.
docs/specs/index.md Adds the launcher-contract spec to the specs index.
docs/specs/cli-surface.md Updates wt open CLI documentation to reflect the relaxed precondition and links to launcher-contract.
fab/changes/260508-evbf-wt-open-any-directory/spec.md Change spec capturing requirements, scenarios, and design decisions for the refactor.
fab/changes/260508-evbf-wt-open-any-directory/plan.md Execution plan/tasks and acceptance checklist for the change.
fab/changes/260508-evbf-wt-open-any-directory/intake.md Intake/problem statement and scope decisions for the change.
fab/changes/260508-evbf-wt-open-any-directory/.status.yaml FAB change tracking metadata for this work item.
fab/changes/260508-evbf-wt-open-any-directory/.history.jsonl FAB stage transition history for this work item.

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

Comment thread src/cmd/wt/open.go Outdated
Comment on lines +59 to +66
if info, err := os.Stat(target); err == nil && info.IsDir() {
wtPath = target
wtName = filepath.Base(wtPath)
} else {
// Try as worktree name
if inRepo {
repoName = ctx.RepoName
}
} else if inRepo {
// Try as worktree name (requires git context to walk worktrees).

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Fixed — handled the os.Stat success + !info.IsDir() case explicitly with a clear 'target exists but is not a directory' ExitGeneralError, and added TestOpen_PathArg_ExistsButNotDir. (a8af131)

Comment thread src/cmd/wt/integration_test.go Outdated
Comment on lines +154 to +155
// Resolve symlinks (macOS /tmp -> /private/tmp); wt's filepath.Base on
// the resolved path is what ends up in WT_CD_FILE.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Fixed — rewrote the comment to clarify symlink resolution is about path stability (macOS /tmp vs /private/tmp), not basenames; WT_CD_FILE receives the full resolved path. (a8af131)

Comment thread docs/specs/launcher-contract.md Outdated
Comment on lines +47 to +54
directory path to that file (mode `0600`) instead of printing a `cd --` line
to stdout.
- Consumers read the file after a zero-exit `wt open` invocation and apply the
`cd` to the parent shell themselves (via shell wrapper, eval, or whatever
mechanism the consumer uses).
- The file is overwritten on each invocation; truncate-on-open semantics.
- Mode `0600` is intentional — the file may contain a path the user considers
private; consumers MUST NOT relax this.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Fixed — clarified §3 to note that os.WriteFile only applies the 0600 mode on file creation (no chmod of existing files), so consumers are responsible for supplying a fresh path (e.g., via mktemp) or a pre-mode-0600 file. (a8af131)

@sahil-noon
sahil-noon merged commit 09e8679 into main May 8, 2026
@sahil-noon
sahil-noon deleted the 260508-evbf-wt-open-any-directory branch May 8, 2026 15:08
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