Skip to content

refactor(approval): share the remove-hook approval helper across remove/merge/prune - #2709

Merged
max-sixty merged 3 commits into
mainfrom
share-remove-approval-helper
May 11, 2026
Merged

max-sixty merged 3 commits into
mainfrom
share-remove-approval-helper

Conversation

@max-sixty

@max-sixty max-sixty commented May 11, 2026 •

Copy link
Copy Markdown
Owner

Summary

wt remove, wt merge, and wt step prune all approve pre-remove against each removed worktree's own .config/wt.toml (with primary-worktree fallback) plus post-remove/post-switch against the primary's. The pattern was duplicated in three places — subtle enough that #2701 had to fix it separately for prune after #2690 fixed it for remove/merge, and #2708 then had to fix the Repository::at Err-swallowing in three places independently. This consolidates the three call sites into one shared helper, so the next fix-this-pattern PR only has to touch one place.

Changes

  • src/commands/project_config.rs — new collect_remove_hook_commands(primary_repo, paths). Returns the Vec<ApprovableCommand> for pre-remove (per-worktree, with primary fallback) + post-remove/post-switch (primary), deduped by template. Carries forward fix(hooks): propagate Repository::at errors in pre-remove config resolution #2708's Repository::at(wt_path)? propagation.
  • src/main.rs::approve_remove — replaced the per-worktree approve_or_skip loop with a single-batch flow. wt remove switches from N+1 prompts to one combined batch (strictly fewer prompts in practice; approvals saved by the first prompt would have suppressed later identical ones anyway).
  • src/commands/merge.rs::collect_merge_commands — pre-remove/post-remove/post-switch now come from the helper; merge keeps its inline pre-commit/post-commit/pre-merge/post-merge collection and bundles everything into the same approve_command_batch call.
  • src/commands/step/prune.rs::approve_prune_hooks — one-line helper call. The "over-approval superset" exception remains in the local docstring.
  • src/commands/hooks.rs — the "Which .config/wt.toml a hook reads" spec docstring now points at the shared helper.

The executor output::handlers::execute_pre_remove_hooks_if_needed still has its own copy of the pattern (an executor-side, not approval-side, concern) — left as-is.

Test plan

  • cargo run -- hook pre-merge --yes — 3660 tests pass; clippy clean.
  • Existing wt remove / wt merge / wt step prune approval tests cover both branches of the helper (per-worktree config vs primary fallback).

max-sixty and others added 2 commits May 11, 2026 11:03
…er across `wt remove`, `wt merge`, and `wt step prune`

`wt remove`, `wt merge`, and `wt step prune` all approve `pre-remove` against
each removed worktree's own `.config/wt.toml` (with primary-worktree fallback)
plus `post-remove`/`post-switch` against the primary's. The pattern was
duplicated in three places — subtle enough that PR #2701 had to fix it
separately for prune after #2690 fixed it for remove/merge.

This adds `project_config::collect_remove_hook_commands(primary_repo, paths)`
and routes all three call sites through it. `wt remove` switches from
N+1 prompts (per-worktree pre-remove + final post-remove/post-switch) to
one combined batch — strictly fewer prompts in practice, since approvals
saved by the first prompt would have suppressed later identical ones
anyway. `wt merge` keeps its bundling shape (helper returns commands;
merge prepends its own pre-commit/post-commit/pre-merge/post-merge to the
same batch). `wt step prune`'s docstring exception about
over-approving every linked worktree is preserved at the caller.

The "Which `.config/wt.toml` a hook reads" spec docstring in `hooks.rs`
now points at the shared helper instead of naming the three call sites.

Co-Authored-By: Claude <[email protected]>
…-helper

# Conflicts:
#	src/commands/hooks.rs
…-helper

# Conflicts:
#	src/commands/step/prune.rs
#	src/main.rs
@max-sixty
max-sixty merged commit 06285ed into main May 11, 2026
30 of 31 checks passed
@max-sixty
max-sixty deleted the share-remove-approval-helper branch May 11, 2026 18:44
max-sixty added a commit that referenced this pull request May 11, 2026
… errors (#2714)

## Summary

Each worktree's `.config/wt.toml` now stands alone. When the worktree a
hook acts on has no `.config/wt.toml`, no project hooks run — the
primary worktree's config is no longer consulted as a fallback. A
present-but-malformed config aborts the operation with the parse error
instead of silently using a different one.

## Why

The fallback (#2690, extended to `wt switch` in #2703) was an internal
detail meant to keep worktrees branched off *before* a hook was added
still firing the hook. But it made which file each hook actually reads
ambiguous — Codex flagged this in review of #2703 (P3): a destination
worktree with a malformed `.config/wt.toml` silently ran the *primary's*
hooks instead of surfacing the parse error.

Removing the fallback gives a cleaner model: each worktree owns its
config. Users who want a hook to apply project-wide commit it on the
default branch — new worktrees branched off that branch inherit it.
Worktrees branched off *before* the commit no longer fire the hook
silently, which was the leaky part.

## Behavior change

A `pre-remove` hook added on `main` no longer fires when removing a
feature worktree that predates the commit adding it. To restore that
behavior, branch the feature off `main` again (or commit the hook on the
feature branch itself).

## What changed

- `Repository::project_config_at_ref` now returns
`anyhow::Result<Option<ProjectConfig>>` — `Ok(None)` for refs with no
`.config/wt.toml`, `Err` for present-but-malformed.
- Dropped the fallback from
`commands::project_config::collect_remove_hook_commands` (the shared
helper introduced in #2709) and from
`output::handlers::execute_pre_remove_hooks_if_needed`,
`worktree::switch::{switch_hook_project_config, hook_repo_for_worktree,
spawn_switch_background_hooks}`, and
`worktree::finish::finish_after_merge`.
- Updated the `commands::hooks` spec docstring — table rows lose the
fallback sub-clauses; trailing paragraph drops the fallback handling.

## Tests

- Unit test `project_config_at_ref_reads_committed_config` now asserts
`Err` on malformed committed config (was `None`).
- Added `test_switch_existing_aborts_on_malformed_destination_config`
and `test_remove_aborts_on_malformed_worktree_config` for the end-to-end
abort path.
- Updated three tests that relied on the fallback to commit configs on
the relevant branches instead: `test_switch_pr_hooks_see_pr_vars`
(commits on `pr-source`),
`test_merge_teardown_hooks_read_destination_worktree_config` →
`..._read_acting_worktree_config` (feature carries pre-remove),
`test_approval_prompt_remove_decline` (commits before creating the
worktree to remove). Snapshot regenerated for the last.

Co-authored-by: Claude <[email protected]>
max-sixty added a commit that referenced this pull request May 11, 2026
…oo (#2717)

## Summary

Follow-up to #2714: the fallback removal in the shared
`collect_remove_hook_commands` helper was lost in the merge-conflict
resolution against #2709 (which introduced the helper) and didn't land
in the squash merge. The executor
(`output::handlers::execute_pre_remove_hooks_if_needed`) has no
fallback, but the approval helper still falls back to the primary
worktree's config — so an approval prompt can list a `pre-remove` from
the primary that the executor will then silently skip.

This patch carries the original change forward to the helper. Each
removed worktree's `pre-remove` reads its own `.config/wt.toml`; if it
has none, no project `pre-remove` runs and the primary's config is not
consulted.

## Why this matters

Post-#2714, `wt remove` / `wt merge` / `wt step prune` all delegate
`pre-remove` approval to this helper. Without the fix the
worktrunk-on-main has an executor/approval mismatch — exactly the kind
of leaky abstraction #2714 set out to eliminate.

## Tests

The integration tests added in #2714
(`test_remove_aborts_on_malformed_worktree_config`,
`test_switch_existing_aborts_on_malformed_destination_config`) and the
fallback-test fixups (commit on the relevant branch instead of relying
on inheritance) already cover the helper's behavior end-to-end. Local
`cargo test --test integration` runs 1704 tests, all pass.

---------

Co-authored-by: Claude <[email protected]>
max-sixty added a commit that referenced this pull request May 11, 2026
…calation (#2724)

## Summary

`wt step <concurrent-alias>` interrupted by Ctrl-C could exit with code
143 (SIGTERM) instead of 130 (SIGINT) under load. The race lives in
`src/output/concurrent.rs`'s signal forwarder: it sends SIGINT to each
child's process group, then waits 200ms before escalating to SIGTERM.
Under CI load the path `signal_hook polls → killpg → child receives →
forwards to sleep → sleep dies → sh dies → pgroup empty` exceeds 200ms,
so wt escalates and the child dies from SIGTERM. Wt's
`interrupt_exit_code()` reads the child's actual death signal and
reports `128 + 15 = 143` even though the user only ever sent SIGINT.

This was the cause of the `affected tests (linux, advisory)` failure on
#2709 (selected `test_alias_concurrent_receives_sigint` because that PR
touched `src/main.rs`, dragging in coverage of nearly every wt
invocation path) — diagnosis recorded there, not a regression from the
approval refactor.

## Fix

Record the user's first signal in a shared `Arc<AtomicI32>` set by the
signal forwarder thread. After every child has been waited on, override
any per-child `ChildProcessExited::signal` whose value differs from the
originating one. wt's exit code now reflects what the user pressed (130
for SIGINT) regardless of which signal the escalation chain ended up
using to actually kill the child.

The fix is contained to `run_concurrent_commands`. The single-step
`shell_exec.rs::stream` path has the same escalation logic but isn't
exercised by the failing test; if the equivalent flake shows up there,
the same pattern can be applied.

## Test plan

- New `test_alias_concurrent_sigint_reports_origin_after_escalation`
(`tests/integration_tests/step_alias.rs:1406`) — children `trap "" INT`
so SIGINT is ignored and only the SIGTERM escalation kills them.
Verified to reproduce the exact CI failure mode
(`unix_wait_status(36608)` = exit 143) without the fix and pass with it.
- Existing `test_alias_concurrent_receives_sigint` and
`test_alias_concurrent_second_sigint_kills` still pass.
- `cargo run -- hook pre-merge --yes`: 3667 tests pass, lints clean.

---------

Co-authored-by: Claude <[email protected]>
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.

2 participants