Skip to content

Reuse user config snapshot for removal hooks - #4276

Merged
max-sixty merged 2 commits into
max-sixty:mainfrom
Duang777:fix/remove-hook-config-snapshot
Sep 27, 2026
Merged

max-sixty merged 2 commits into
max-sixty:mainfrom
Duang777:fix/remove-hook-config-snapshot

Conversation

@Duang777

Copy link
Copy Markdown
Contributor

Summary

  • reuse the command-entry UserConfig snapshot when rendering approved removal hooks
  • pass the snapshot through direct remove, merge cleanup, step prune, and picker removal paths
  • add a regression test where pre-remove corrupts the config file and the approved post-remove hook still runs

Why

Removal hook approval and execution used different UserConfig loads. If the config changed or became unreadable after approval, execution silently skipped hooks that the user had already approved. Keeping the matching snapshot with ApprovedHookPlan makes hook selection and rendering consistent.

Testing

  • RUSTUP_TOOLCHAIN=1.97.0-aarch64-apple-darwin cargo run -- hook pre-merge --yes
  • 4,968 tests passed; 1 skipped

@max-sixty

Copy link
Copy Markdown
Owner

how does this work throughout worktrunk? do we generally re-load the config or thread it through?

one option would be to cache it, so we don't need to thread it through literally everything

@worktrunk-bot

Copy link
Copy Markdown
Collaborator

It's mixed today, and the cache you're describing already exists.

  • Cache: Repository::prewarm loads the user config once at the start of main and stores it in the process-wide WORKTRUNK_USER_CONFIG_PRELOAD OnceLock. Repository::at copies it into every Repository, so repo.user_config() returns the startup snapshot without reading the file again. About 17 call sites use it. The picker hunk in this PR already passes repo.user_config().
  • Load and thread: most command handlers (remove, merge, step prune/commit/squash, for-each, …) call UserConfig::load() when the command starts and pass it into CommandContext. That's about 21 sites.
  • Reload mid-command: the two sites this PR removes, execute_pre_remove_hooks_if_needed and spawn_hooks_after_remove in handlers.rs, read the file again after hooks had already run. That caused the bug.

So a smaller fix is to replace those two UserConfig::load() calls with repo.user_config() and pre_remove_repo.user_config(). The executor would then read the cached startup snapshot, and handle_remove_output, WorktreeRemovalContext and prune's RemovalContext wouldn't need the new parameter. The new regression test should still cover that version: pre-remove corrupts the file, but the cached snapshot was loaded before it ran.

The load-and-thread handlers do a second full read, parse and merge, separate from the startup snapshot. Switching them to repo.user_config() would remove that repeated work and make the approval gate and the executor read the same object by construction. It fits as a follow-up rather than something this PR needs. The one difference is when prewarm didn't run (some tests): user_config() then loads on first access instead of at startup.

@max-sixty

Copy link
Copy Markdown
Owner

So a smaller fix is to replace those two UserConfig::load() calls with repo.user_config() and pre_remove_repo.user_config(). The executor would then read the cached startup snapshot, and handle_remove_output, WorktreeRemovalContext and prune's RemovalContext wouldn't need the new parameter. The new regression test should still cover that version: pre-remove corrupts the file, but the cached snapshot was loaded before it ran.

yes do this instead

Replace the two mid-command UserConfig::load() calls with the
startup snapshot from repo.user_config(), instead of threading a
UserConfig parameter through every removal path.

Co-Authored-By: Claude <[email protected]>
@max-sixty
max-sixty enabled auto-merge (squash) September 27, 2026 04:13
@worktrunk-bot

Copy link
Copy Markdown
Collaborator

Done in abda224. The threaded UserConfig parameter is gone, so handle_remove_output, WorktreeRemovalContext, prune's RemovalContext, finish.rs and the picker/remove call sites are back to what main has. The only source change now is in src/output/handlers.rs: spawn_hooks_after_remove reads repo.user_config(), and execute_pre_remove_hooks_if_needed reads pre_remove_repo.user_config(), instead of calling UserConfig::load() again.

The regression test test_post_remove_uses_config_snapshot_after_pre_remove_mutates_file is unchanged. It fails when this handlers.rs change is reverted and passes with it. One side effect: the two sites used to skip hooks silently when the config was unreadable. Now they use the startup snapshot, which handles a bad config layer by warning and carrying on.

When I posted this, CI was green on Linux and macOS, and codecov/patch had passed. test (windows) was still running.

@max-sixty
max-sixty merged commit e1cc129 into max-sixty:main Sep 27, 2026
38 checks passed
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