Skip to content

fix(placement): show short branch names for worktrees - #2504

Merged
tgrunnagle merged 4 commits into
mainfrom
future-nova-1352ccbd
Oct 26, 2026
Merged

tgrunnagle merged 4 commits into
mainfrom
future-nova-1352ccbd

Conversation

@tgrunnagle

@tgrunnagle tgrunnagle commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

Closes #2420

Summary

ListWorktrees, ClearSession/ForkSession placement metadata, and Session.placement returned an empty branch and label for every worktree on a named branch. The local provider passed the raw porcelain ref (refs/heads/feat) to a display sanitizer whose path-leak guard blanks any value containing /. Clients could only tell worktrees apart by response order and revision, which is ambiguous when two worktrees share a HEAD (for example, right after git worktree add -b). These fields now carry the short branch name (feat, feat/x), and the path-leak guard still applies to everything else.

Changes Made

Local placement provider (internal/app/placement.go)

  • New displayBranch helper trims refs/heads/. ListWorktrees and bindWorktree use it for Label and Branch.
  • The raw porcelain ref stays on server.Worktree.Branch, so selector matching and the stale-choice check still compare exact values.

Server sanitization (internal/adapter/server/placement.go, placement_discovery.go)

  • sanitizePlacementBranch applies the same UTF-8, control/format-rune, and length handling as sanitizePlacementDisplay. It allows / only in a name that is well formed under the git check-ref-format --branch rules. Those rules reject leading or trailing /, //, .., \, :, a component that starts with ., .lock, @{, a leading -, a bare @, and the other forbidden characters. That still blocks absolute paths, .. traversal, backslashes, and drive letters.
  • sanitizePlacementLabel relaxes the guard only when the label equals the branch. Free-form labels from other providers keep the strict guard, so a slash-bearing label that is not the branch is still blanked.
  • canonicalPlacementMetadata and ListWorktreesForSession share both helpers, so the two paths cannot drift apart. The common text cleaning moved into cleanPlacementText.

Contract docs (harness.proto and regenerated outputs)

  • Added field comments on Worktree.label/branch and PlacementMetadata.label/branch: branch is the short branch name, empty for a detached or unknown HEAD. Only comments changed. The Go, TypeScript, and user-docs/reference/grpc-schema.md outputs were regenerated with task generate.

TUI (cmd/mecatui/ui/worktrees.go)

  • Picker rows and the confirm view no longer repeat the branch when it equals the label. A feat/x worktree now shows feat/x (abcdef12) instead of feat/x (feat/x). A label that differs from the branch, such as Primary repository, still shows the branch.

Implementation Details

  • Short names instead of the full ref. Under "Expected", the issue mentions refs/heads/feat. This PR returns the short name instead. Both fields are display-only (PlacementMetadata is "safe display data"), and clients select worktrees by the opaque selector, never by ref, so the full ref adds nothing. The short name is also what git status and the TUI picker show. Linked worktrees are always on refs/heads/* or detached, so no other ref namespace needs handling.
  • Trimming refs/heads/ alone is not enough. Namespaced short names such as feat/x still contain /, and sanitizePlacementDisplay would still blank them. That is why the server needs a branch-specific rule and not just a prefix trim.

API compatibility

Follows the wire API compatibility assessment.

  • Assessment: compatible. No major version bump and no feature identifier.
  • Wire schema: unchanged. The .proto change adds comments only.
  • Observable values: Worktree.branch/label and PlacementMetadata.branch/label used to be empty for every named branch. They now carry the short branch name (for example feat/x). This restores what the provider already intended. Clients treat these fields as opaque display strings (cmd/mecatui/client/worktrees.go, client/placement.go), and no client can depend on the value being empty, because empty never told a client anything.
  • Stored data: sessions created before this fix keep the empty branch/label in their stored Session.Placement until they are next cleared or forked. No migration is needed, because the values are display-only and are recomputed on each new placement.
  • Go source: no exported API changes. The new helpers are unexported.
  • Upgrade implications: both are cosmetic.
    • Until it is upgraded, an older mecatui shows a namespaced branch twice in the worktree picker and confirm view, once as the label and once as the branch.
    • If the server is rolled back, sessions placed on this version keep their stored short names until they are next cleared or forked.

Testing

  • Sanitizer unit tests (canonical_seams_repair_test.go):

    • Accepted: feat, feat/x, user/a.b-c, release/1.2/hotfix.
    • Rejected: path-shaped and malformed names such as /etc/passwd, ../x, a/../b, C:\x, C:foo, a//b, .hidden/x, x.lock, -x, @, and a@{1}.
    • Accepted on purpose: ref- and path-shaped names such as refs/heads/feat/x and srv/private/x. Providers must supply short names.
    • Also checked:
      • control/format runes are stripped;
      • the whole name is validated before the length cap, so a cut that lands on / or . no longer blanks the branch;
      • the label is relaxed only when it equals the branch.
  • Public contract test (placement_public_contract_test.go): a fake provider returns a namespaced feat/x worktree. The test asserts that feat/x comes through ListWorktrees, the ClearSession placement, GetSession().placement, and the ForkSession placement, and that no private path escapes. A second test gives a worktree a path-shaped label that isn't its branch and asserts the label stays blank, while the branch is kept, through ListWorktrees and ClearSession.

  • Real-git regression test (internal/app/worktree_lister_test.go): runs git worktree add -b feat/x in t.TempDir(). It asserts that the two worktrees share a HEAD but can be told apart by short branch/label, and that binding the selector gives feat/x metadata. This is the scenario from the issue.

  • TUI tests:

    • Fixtures now use short branch names.
    • A new row-text test covers a branch equal to the label, a branch different from the label, and a detached worktree.
    • A new confirm-view test checks that the branch: line appears only when the branch differs from the label.
  • Local checks:

    • Focused tests pass for internal/adapter/server (placement, worktree, discovery), internal/app (worktree, placement), cmd/mecatui/ui, and cmd/mecatui/client.
    • Lint on the changed packages reports 0 issues. Full task lint reports one finding, a darwin-only gosec issue in environment/microvm/launch_ownership_darwin.go that already exists on main and is not touched here.
    • task docs passes (Node 22). task generate was run with buf 1.73.0, the version CI pins.
    • The offline demo (go run ./cmd/mecademo) shows a tool call, a permission ask and approval, and the result.
  • Race suite: task test:race has local failures. The same failures happen on a clean main checkout on the same machine, so they come from the local environment (git and temp-directory behavior) and not from this change. They include:

    • TestReadOnlyMemberRunsGitInWorktreeEndToEnd
    • TestSubagentRunsGitInWorktreeEndToEnd
    • TestACPUsesHostGovernanceRootAndClosesRejectedBinding
    • forker and executionexecutor tests
    • TestStartupCompatibilityEmbeddedRejected
    • TestMecatedCLICompositionValidatesMicroVMDefaultAfterReadiness
    • one environment/microvm test

    CI is the clean race gate for this PR.

Additional Notes

🤖 Generated with Claude Code

tgrunnagle and others added 2 commits October 24, 2026 08:51
ListWorktrees and placement metadata blanked branch and label for every
named branch: git reports refs/heads/<name>, and the display path guard
drops any value containing "/".

Fixes #2420:
- The local provider trims refs/heads/ for display; selector matching
  still compares the raw porcelain ref.
- The server keeps a branch only when it is a well-formed git branch
  name, so "feat/x" survives while paths, "..", backslashes, and drive
  letters stay blank. A label is relaxed only when it is the branch.
- Document the branch and label field semantics in the proto.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Addresses review feedback on #2420:
- The worktree picker and confirm view skip the branch suffix when it
  equals the label, which is now the common case.
- Cover ForkSession and the gRPC GetSession placement in the namespaced
  branch contract test.
- Document PlacementMetadata.label in the proto.
- TUI fixtures use short branch names, matching what the server sends.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@tgrunnagle tgrunnagle left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Multi-Agent Consensus Review

Agents consulted: security, api-compat, tests, general, comment-hygiene, scope

Consensus Summary

# Finding Consensus Severity Action
1 Proto field comments state values the server doesn't guarantee 8/10 LOW Fix
2 validBranchName comment claims full check-ref-format --branch parity 9/10 LOW Fix
3 Branch sanitizer accepts any well-formed ref-shaped string 8/10 LOW Fix
4 Truncating before validation blanks or misleads long branch names 8/10 LOW Fix
5 renderConfirm branch suppression has no test 9/10 LOW Fix
6 Planned contract assertion for a blanked non-branch label is missing 7/10 LOW Fix
7 Real-git test creates its worktree outside its own TempDir 7/10 LOW Fix
8 Compatibility notes leave out older-client and rollback effects 7/10 INFO Discuss

Overall

The PR fixes #2420 by trimming refs/heads/ in the local provider and adding a server-side branch sanitizer that allows / only inside a well-formed git branch name. The label relaxes its path guard only when it is the branch. The layering is right: selector identity still uses the raw porcelain ref, both server paths share one helper, and the tests guard the fix. Reverting displayBranch, either call site, or the label guard each makes a test fail.

Every finding is polish. The proto, seam-type and validator comments promise more than the server enforces: "short branch name", full git parity, a closed set of label values. Today only the local provider's porcelain input makes those claims true. Two test gaps remain: the confirm-view change and the contract-level negative label case. Separately, the compatibility section should add one line: older mecatui builds show the branch twice until upgraded, and a rolled-back server keeps serving stored short names. Both are cosmetic.

Documentation

  • contracts/proto/mecatl/v1/harness.proto:968-972, 3161-3164: reword, then task generate.
  • internal/adapter/server/placement.go:942-944, 978-979: narrow the doc comments.

Generated with Claude Code

Comment thread contracts/proto/mecatl/v1/harness.proto Resolved
Comment thread internal/adapter/server/placement.go Resolved
Comment thread internal/adapter/server/placement.go Resolved
Comment thread internal/adapter/server/placement.go Resolved
Comment thread cmd/mecatui/ui/worktrees.go Resolved
Comment thread internal/app/worktree_lister_test.go Resolved
tgrunnagle and others added 2 commits October 25, 2026 05:08
Addresses #2504 review comments:
- LOW contracts/proto/mecatl/v1/harness.proto (4368229963): describe label and branch without promising values the server does not guarantee
- LOW internal/adapter/server/placement.go (4368229968): narrow the validBranchName comment to the rules it applies
- LOW internal/adapter/server/placement.go (4368229971): document the short-name obligation and test ref- and path-shaped names as accepted
- LOW internal/adapter/server/placement.go (4368229974): validate the full branch name before capping its length
- LOW internal/adapter/server/placement_public_contract_test.go (4368229980): assert a path-shaped label that is not the branch stays blank through ListWorktrees and ClearSession

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ee dir

Addresses #2504 review comments:
- LOW cmd/mecatui/ui/worktrees.go (4368229977): assert the confirm view omits the branch line when it equals the label and shows it otherwise
- LOW internal/app/worktree_lister_test.go (4368229982): keep the repository and its linked worktree under one test-owned TempDir

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@tgrunnagle

Copy link
Copy Markdown
Contributor Author

Re #8 (compatibility notes): I added an Upgrade implications line to the PR description's API compatibility section. Until upgraded, an older mecatui shows a namespaced branch twice. A rolled-back server keeps serving stored short names until the next clear or fork. Both are cosmetic, so no code change.

@tgrunnagle
tgrunnagle marked this pull request as ready for review October 26, 2026 16:05

@rdimitrov rdimitrov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Reviewed the branch-display fix against #2420, including real-git normalization, selector identity preservation, shared server sanitization, Clear/Fork/GetSession metadata, and TUI deduplication. No substantive correctness or security issues found. Focused server, app, UI, and client regression tests pass locally. Full CI gates are still pending; this approval does not replace them.

@tgrunnagle
tgrunnagle merged commit ac34f9e into main Oct 26, 2026
72 checks passed
@tgrunnagle
tgrunnagle deleted the future-nova-1352ccbd branch October 26, 2026 16:34
@toolhive-release-app toolhive-release-app Bot mentioned this pull request Oct 26, 2026
2 tasks
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.

ListWorktrees and placement metadata return empty branch/label for every named branch

3 participants