Skip to content

fix: route all background hooks through pipeline-aware API - #1910

Merged
max-sixty merged 3 commits into
mainfrom
background-codex
Apr 4, 2026
Merged

max-sixty merged 3 commits into
mainfrom
background-codex

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

Three bugs in the background hooks system, found by Codex review:

Post-merge and post-remove bypassed the pipeline path. merge.rs called prepare_hook_commands + spawn_background_hooks directly, and prepare_post_remove_commands returned flat Vec<SourcedCommand>. List-form configs lost serial/concurrent semantics — commands raced instead of running in order.

Pipeline context leaked per-step hook_name. spawn_hook_pipeline deserialized the first command's context_json as the shared pipeline context, which included hook_name. Later steps saw step 1's name instead of their own. Additionally, handle_switch.rs merged PostSwitch and PostStart pipeline steps into one pipeline, giving PostStart steps the wrong hook_type.

Lazy template expansion broken in flat spawn path. When name-filtering pipeline commands via wt hook post-start db, run_post_hook fell through to spawn_background_hooks, which passed the raw {{ vars.name }} template to the shell instead of expanding it.

Fixes:

  • All background hook callers now go through prepare_background_hooks/spawn_prepared_hooks, which auto-detects pipeline vs flat configs
  • Pipeline context strips hook_name; the background runner injects it per-step via build_step_context_json
  • spawn_background_hooks expands lazy templates before detaching (shared expand_lazy_template helper)
  • handle_switch.rs spawns each hook type's pipeline independently
  • prepare_post_remove_commands replaced by PostRemoveContext struct + unified API
  • run_post_hook simplified to use unified API (removes manual pipeline detection)
  • prepare_pipeline_hooks_with_configs made private (only used by prepare_background_hooks)

4 regression tests: pipeline hook_name isolation, post-merge pipeline ordering, post-remove pipeline ordering, name-filtered lazy template expansion.

This was written by Claude Code on behalf of @max-sixty

max-sixty and others added 3 commits April 4, 2026 12:36
Three bugs fixed:

- Post-merge and post-remove hooks bypassed the pipeline path, losing
  serial/concurrent semantics for list-form configs. Now all callers
  use prepare_background_hooks/spawn_prepared_hooks.

- Pipeline context leaked per-step hook_name to all steps. The shared
  context now strips hook_name; the background runner injects it
  per-step.

- Name-filtered pipeline commands in the flat spawn path passed raw
  unexpanded templates to the shell. spawn_background_hooks now
  expands lazy templates before detaching.

Also extracts PostRemoveContext struct (replaces prepare_post_remove_commands),
adds expand_lazy_template helper, and stops merging pipeline steps from
different hook types into one pipeline in handle_switch.rs.

Co-Authored-By: Claude <[email protected]>
# Conflicts:
#	src/commands/handle_switch.rs
#	tests/integration_tests/user_hooks.rs
- Simplify run_post_hook to use prepare_background_hooks/spawn_prepared_hooks
  for non-name-filtered background hooks, eliminating manual pipeline detection
- Make prepare_pipeline_hooks_with_configs private (only used internally now)
- Add post-remove pipeline serial ordering test
- Add name-filtered lazy template expansion test

Co-Authored-By: Claude <[email protected]>
@max-sixty
max-sixty merged commit 5cd7da3 into main Apr 4, 2026
23 of 24 checks passed
@max-sixty
max-sixty deleted the background-codex branch April 4, 2026 21:14
max-sixty pushed a commit that referenced this pull request Apr 4, 2026
## Problem

CI on `main` failed on Windows with a stack overflow in
`test_copy_ignored_many_directories_no_emfile`:

```
thread '<unknown>' (2316) has overflowed its stack
```

The rayon worker in the copy thread pool inherits the platform default
stack size. On Linux/macOS this is ~8 MiB, but on Windows it is only
~2 MiB, which is not enough for concurrent `reflink_or_copy` work over
200 directories of ignored files.

The failing run (23987711021) sits on a commit (#1910) that doesn't
touch the copy path — this is a latent Windows-specific issue in the
dedicated copy pool introduced in #1905.

## Solution

Set an explicit 8 MiB `stack_size` on `COPY_POOL` so behaviour is
consistent across platforms. This matches the Unix default and is a
narrow, targeted fix at the place the problem is — the rayon pool used
exclusively for `copy_dir_recursive`.

## Testing

- `cargo test --test integration
test_copy_ignored_many_directories_no_emfile` passes locally (Linux).
- Full Windows CI re-run will exercise the large-scale concurrent copy
path.

---
Automated fix for [failed
run](https://github.com/max-sixty/worktrunk/actions/runs/23987711021)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: worktrunk-bot <[email protected]>
Co-authored-by: Claude <[email protected]>
max-sixty added a commit that referenced this pull request Jun 11, 2026
…#3042)

Hook templates expanded eagerly at prep time unless they referenced
`vars.*` (#1840); aliases were unconditionally lazy. The dual
representation (`expanded` + `lazy_template` on `PreparedCommand`) put a
mode choice in front of every consumer, and the seams produced real bugs
(#1855, #1910). The background runner also re-rendered eagerly-expanded
strings: a latent double-expansion whenever expansion output contained
`{{`.

This PR unifies on one rule: syntax is validated at preparation (before
step 1), templates render when each step runs, and `vars.*` always reads
fresh from git config. This is the direction #2128 recorded ("treat
hooks as aliases bound to triggers").

**Navigating the diff:**

- `src/commands/command_executor.rs`: `PreparedCommand` now carries the
raw template plus the frozen context map; JSON is produced only at the
process boundary. A `template_name` field keeps the moved errors' text
identical to the old prep-time messages. `prepare_steps` validates
syntax and freezes context; `resolve_command_str` renders at execution;
`map_config_steps` is the structural walker shared with aliases.
- `src/commands/alias.rs`: alias prep shares the walker. The residual
fork is genuinely alias-specific: context construction (filtered to
referenced vars, `{{ args }}` injection) and labels. A prep-time syntax
check turned out to be dead code (the arg-routing parse already aborts
on syntax errors), so `referenced_vars_for_config` now produces the rich
`TemplateExpandError` instead.
- `src/commands/hooks.rs`: the background spec always ships raw
templates; the runner renders each step (it already did for vars steps).
- Dry-run paths (`wt hook --dry-run`, `wt config alias dry-run`) share
`render_template_preview`: vars-referencing templates shown raw after a
syntax check, everything else rendered at display time.

**Behavior changes** (each pinned by a test or snapshot):

1. A foreground pipeline whose step N has a semantic template error
(undefined variable) runs steps 1..N-1 before failing, matching what
vars-referencing steps already did. New test:
`test_foreground_pipeline_undefined_var_runs_earlier_steps`.
2. A semantic error in a background hook template no longer fails the
foreground command; it surfaces in the runner log. Syntax errors still
abort at prep. New test:
`test_background_hook_undefined_var_fails_in_runner`;
`test_merge_drops_pending_hooks_when_post_merge_fails` switched to a
syntax-error fixture since that is what now reaches its subject (the
announcer Drop-flush).
3. `-v` no longer prints the parent-side rendered command for background
hooks (rendering happens in the runner). The variables table remains and
`wt hook <type> --dry-run` previews commands; help text updated. Three
snapshots changed by exactly this.
4. Announce gutters for vars-referencing foreground steps show the
rendered command instead of the raw template.
5. Syntax errors caught by the arg-routing parse (aliases, `wt hook`
`--KEY=VALUE` routing) render as the rich template error with the
source-line gutter, naming the alias or hook.

`ApprovedHookPlan` freezing, approval semantics (keyed and displayed by
template), config file format, and CLI flags are unchanged.

**Testing:** full local gate green (3917 tests + lints). The deferral
semantics in changes 1 and 2 previously had no coverage; both are now
pinned.

> _This was written by Claude Code on behalf of max_

Co-authored-by: Claude Fable 5 <[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