Skip to content

refactor(hooks/alias): lift hook metadata to PipelineKind, unify source enum - #2521

Merged
max-sixty merged 2 commits into
mainfrom
fix/issue-2101-user-alias-execute
May 2, 2026
Merged

max-sixty merged 2 commits into
mainfrom
fix/issue-2101-user-alias-execute

Conversation

@max-sixty

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

Copy link
Copy Markdown
Owner

Two follow-ups deferred from #2474, plus a coverage refinement on review feedback.

Lift hook-only metadata onto PipelineKind::Hook

SourcedStep::hook_type: Option<HookType> and display_path: Option<PathBuf> were a smell — the invariant is per-pipeline, not per-step, but living on SourcedStep forced four .expect("hook pipelines always set hook_type") sites. Lifted both onto PipelineKind::Hook { hook_type, display_path }, mirroring Alias { name }. The invariant now lives on the type, and sourced_steps_to_foreground reads the metadata directly from kind.

Background flow doesn't need PipelineKind at all — it only ever handles hooks (aliases run in the foreground). Threading PipelineKind through it forced two unreachable!() and one defensive continue for the structurally unreachable Alias variant. Background functions now take a BackgroundPipeline type alias for (CommandContext, HookType, Option<PathBuf>, Vec<SourcedStep>) directly.

Touches prepare_background_pipelines, run_hooks_background, print_background_variable_table, spawn_hook_pipeline_quiet, sourced_steps_to_foreground, run_hooks_foreground, and the alias and hook-commands call sites.

Unify AliasSource into HookSource

AliasSource and HookSource were near-identical User/Project enums. Removed AliasSource; all alias call sites now use HookSource. The shared enum gains PartialOrd/Ord derives (needed for (name, source) listing sort) and an updated docstring covering dual hook+alias usage. source.label() becomes {source} via the existing strum::Display.

Test plan

  • cargo nextest run — 3261 tests pass (incl. new test_combined_post_remove_and_post_switch_hooks_verbose covering the multi-hook-type filter in print_background_variable_table)
  • pre-commit run --all-files — green
  • Snapshot tests unchanged for existing flows

🤖 Generated with Claude Code

…ce enum

Drops `hook_type`/`display_path` from `SourcedStep` and lifts them onto
`PipelineKind::Hook { hook_type, display_path }`, mirroring `Alias { name }`.
Removes the four `.expect("...always set hook_type")` sites; the invariant
now lives on the type. Background flow threads `(CommandContext, PipelineKind,
Vec<SourcedStep>)` per pipeline.

Also collapses `AliasSource` into `HookSource` (User/Project, identical shape).
`HookSource` gains `PartialOrd`/`Ord` for the listing sort, and an updated
docstring covering dual hook+alias usage. `source.label()` is replaced with
`{source}` formatted via the existing `strum::Display`.

Co-Authored-By: Claude <[email protected]>
worktrunk-bot
worktrunk-bot previously approved these changes May 1, 2026

@worktrunk-bot worktrunk-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Clean refactor — the per-step Option<HookType>/Option<PathBuf> smell is gone, and the HookSource/AliasSource unification reads naturally now that they share the same trust-model wording.

One small follow-up below.

Comment thread src/commands/hooks.rs
extra_vars,
name_filters,
display_path,
display_path: _,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Now that display_path is destructured-and-discarded here (and prepare_and_check discards it via ..), the field on HookCommandSpec is read by exactly one caller: run_hooks_foreground reaches in for spec.display_path to build PipelineKind::Hook. The doc comment on this function (lines 109–110) still describes display_path as if it controls the announcement here, which is no longer true.

Worth either dropping display_path from HookCommandSpec entirely (run_hooks_foreground already takes ctx and could take a separate display_path: Option<&Path> param) or trimming the stale doc paragraph. Not blocking.

@worktrunk-bot worktrunk-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

codecov/patch is at 92.3% (5/65 lines uncovered). Breakdown of the missed lines, all in src/commands/hooks.rs:

Line Code Coverable?
514 unreachable!("background pipelines are always PipelineKind::Hook") in run_hooks_background No — structural
666 same unreachable!() in spawn_hook_pipeline_quiet No — structural
632 continue for non-Hook kind in print_background_variable_table No — same structural invariant
635 continue when *ht != hook_type Yes, via spawn_hooks_after_remove (post-remove + post-switch batch); existing fixtures don't combine them
638 continue when group.first() is None No — into_source_groups produces non-empty groups, and run_hooks_background filters empties

Lines 514, 632, 638, 666 are the price of the "lift to PipelineKind" approach the PR description argues for, and the old code had the same shape on a per-step basis (if sourced.hook_type != Some(hook_type) { continue; } on what is now line 635). So this is mostly the patch surfacing structural invariants that codecov can't see through, not a real coverage gap.

Line 635 is the only honestly testable miss — a fixture that registers both post-remove and post-switch hooks and triggers spawn_hooks_after_remove with changed_directory = true. Whether that's worth a dedicated test or a codecov override is your call. Dismissing the approval so the patch check doesn't auto-clear on a refresh.

@worktrunk-bot
worktrunk-bot dismissed their stale review May 1, 2026 04:57

codecov/patch failed (92.3%, 5 uncovered lines) — analysis posted, deferring to author

max-sixty pushed a commit that referenced this pull request May 1, 2026
… flakes (#2523)

## Problem

[Run
25196909437](https://github.com/max-sixty/worktrunk/actions/runs/25196909437)
(tend-review on PR #2512, "Add Codex support") burned ~116 minutes /
$6.90 because the bot:

1. Approved the PR
2. Saw `test (windows)` fail on `test_prune_json_orphan_branch` —
explicitly identified as a `step_prune` test "unrelated to this PR"
3. **Dismissed its own approval anyway** (dismissal message:
*"Dismissing — `test (windows)` failed on
`test_prune_json_orphan_branch`... Will retry once it completes; if the
rerun is green, this is a flake and the PR is fine."*)
4. Tried `POST /repos/.../actions/jobs/<id>/rerun` → HTTP 403 *"The
workflow run containing this job is already running"*
5. Wrote a background script that polled for ~90 minutes waiting for the
non-required `benchmarks` job to clear so it could call `gh run rerun
--failed`

The dismissal-on-flake pattern is structural: given (approved review +
clearly-unrelated test failure), the bot reasoned its way into "must
rerun → must dismiss approval first → must wait for `benchmarks`." This
contradicts the spirit of `CLAUDE.md`'s existing *"Don't wait for CI
`benchmarks` before merging"* note, which only covered the merge path.

This is distinct from the codecov dismissal pattern (PRs #2521, #2516,
#2512, #2507) — those are correct per `CLAUDE.md`'s *"NEVER merge a PR
with failing `codecov/patch` without explicit user approval"* and aren't
touched here.

## Change

Adds a `## PR Review: Don't Self-Dismiss Over Unrelated Test Flakes`
section to `.claude/skills/running-tend/SKILL.md` (~22 lines)
explaining:

- Leave approval in place for unrelated flakes; post a comment instead
- GitHub blocks both `--failed` and per-job rerun while *any* job in the
workflow is `in_progress`
- `benchmarks` routinely runs 80+ min, so the wait cascades wastefully
- The codecov-dismiss pattern is unchanged

## Gates

- **Evidence**: 1 occurrence (this run); structural failure with
explicit reasoning chain → 1 occurrence is sufficient per
`review-gates.md`
- **Magnitude**: Targeted fix, ~22 lines added to a 231-line skill

Co-authored-by: worktrunk-bot <[email protected]>
Background flow only ever sees hooks (aliases run in the foreground), so
threading `PipelineKind` through it forced two `unreachable!()` and one
defensive `continue` for the `Alias` variant that's structurally unreachable.
Replace with a `BackgroundPipeline` type alias carrying `(CommandContext,
HookType, Option<PathBuf>, Vec<SourcedStep>)` directly. `PipelineKind::Hook`
stays for the foreground conversion at `sourced_steps_to_foreground`.

Also remove the defensive `group.first().is_none()` check in
`print_background_variable_table` — `into_source_groups` produces non-empty
groups and `run_hooks_background` filters empties, so `group[0]` is safe.

Add `test_combined_post_remove_and_post_switch_hooks_verbose` exercising the
multi-hook-type filter branch in `print_background_variable_table` (previously
only hit by the `wt remove` of the current worktree, which the existing
non-verbose test covers without triggering the variable-table path).

Co-Authored-By: Claude <[email protected]>
@max-sixty
max-sixty merged commit 2d093ff into main May 2, 2026
27 checks passed
@max-sixty
max-sixty deleted the fix/issue-2101-user-alias-execute branch May 2, 2026 01:23
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