Repository navigation
refactor(alias)!: source-tag steps and decide EXEC per-step - #2474
Conversation
User-source aliases live in the user's own config, so a nested `wt switch --execute claude` inside an alias body is no different from the user typing the same command at the top level. Drop the conservative EXEC scrub for user aliases and reword the refusal hint to point folks with project-alias / hook needs at #2101. Project aliases and hooks keep the existing scrub: bodies authored in shared config still must not be able to inject arbitrary shell into a contributor's interactive session. Co-Authored-By: Claude Opus 4.7 <[email protected]>
worktrunk-bot
left a comment
There was a problem hiding this comment.
Behavior looks right — user-source aliases get the relaxation, project aliases and hooks keep the scrub, and the new directive_exec_file plumbing mirrors the existing CD path through Cmd, output::handlers, and output::concurrent.
One concrete concern: the project_aliases_contain doc comment misdescribes how same-name aliases merge. append_aliases (src/config/commands.rs:185) uses merge_append, which concatenates steps (user first, then project) — see test_alias_append_executes_both — so a same-named project alias does not "shadow" the user entry. The runtime choice is still correct (return true → scrub EXEC), but the rationale needs to be "the merged pipeline runs project-authored steps too, so we can't selectively pass EXEC to user steps only." Inline suggestion below.
Same-name collision case is also unverified by the new tests. Worth a third case alongside test_user_alias_passes_exec_directive / test_project_alias_scrubs_exec_directive — define the same alias name in both user and project config, assert EXEC is scrubbed and the warning fires.
Doc on `project_aliases_contain` claimed project entries shadow user entries — they don't. `append_aliases` concatenates steps (user first, then project), so a same-named project alias means the merged pipeline runs project-authored steps, which is why we conservatively scrub EXEC for the entire body. Also adds an integration test for the collision case alongside `test_user_alias_passes_exec_directive` / `test_project_alias_scrubs_exec_directive`. Co-Authored-By: Claude Opus 4.7 <[email protected]>
|
We need to design our data structure so we're not searching by name; more similar to hooks. that's also an opportunity for consolidation |
|
Sketch of the redesign: have alias resolution mirror hooks — return a Consolidation lever: Tradeoff to flag: |
|
yes please |
…pipeline Previously, alias dispatch merged user and project alias bodies through `append_aliases` and decided EXEC passthrough by checking whether the project config defined a same-name alias (`project_aliases_contain`). The lookup was name-keyed and the decision was per-pipeline, so a same-name collision scrubbed EXEC for the whole merged body — including the user's own steps. Mirror the hook resolution shape: - `SourcedStep` becomes the shared shape for both alias and hook dispatch (`hook_type` is now `Option<HookType>` so aliases can leave it `None`). - `ForegroundStep` carries its own `DirectivePassthrough`, so `execute_pipeline_foreground` no longer takes a per-pipeline `directives` argument. - `sourced_steps_to_foreground(steps, &PipelineKind)` is the single resolver: hook steps and project-source alias steps get `inherit_from_env()` (CD only); user-source alias steps get `inherit_from_env_with_exec()`. - `prepare_alias_sourced_steps` builds the alias side from per-source `CommandConfig` lookups, dropping the merged-execution path and the `project_aliases_contain` name search. Net behavior change: when both user and project define the same alias, the user's steps now keep the EXEC relaxation while the project's steps still scrub. Strictly more permissive than the previous per-pipeline scrub, which was a conservative side effect of name-keyed merging. Updated `test_user_and_project_alias_collision_scrubs_exec_directive` → `test_user_and_project_alias_collision_scrubs_only_project_step` to assert the new per-step behavior end-to-end. Co-Authored-By: Claude Opus 4.7 <[email protected]>
|
Spiked the redesign on this branch (787a445). Sketch from the earlier comment materialized as:
One behavior change worth flagging: same-name user+project alias collisions previously scrubbed EXEC for the whole merged pipeline. With per-step decisions, the user's own steps keep the relaxation; only the project-authored steps scrub. Strictly more permissive than before, and the same-name collision case now falls out of the source tag rather than needing a special branch. Net diff: +209 / -111 across |
|
can you explain re the merged user + project alias? why are we merging them? |
worktrunk-bot
left a comment
There was a problem hiding this comment.
can you explain re the merged user + project alias? why are we merging them?
The user+project merge is preexisting, not introduced here. append_aliases (src/config/commands.rs:185) follows the same pattern as merge_append_hooks for hook layers — when both configs define the same name, both run (user first, then project). Anchored by test_alias_append_executes_both (cross-source) and test_aliases_accessor_appends_on_collision (global vs per-project user). The rationale baked into the doc comment is "matching how hooks merge across config layers": a project shouldn't silently disable a user's same-named alias by reusing the name, and the project side still goes through approval.
What this PR changes is how the merged execution is built — not what merges with what. Before: load_merged_aliases produced a single CommandConfig via append_aliases and the executor saw an opaque step list, so EXEC trust had to be a per-pipeline scrub keyed on "does the project define this name?". Now: steps stay source-tagged end-to-end, so the executor decides EXEC trust per-step. Same steps in the same order, just a different shape.
If the design question is actually whether merging is the right behavior at all (vs. shadowing or erroring on collision), that's orthogonal to this PR — happy to spike as a separate change.
Unrelated FYI: PR is mergeStateStatus: DIRTY — main has moved (HookAnnouncer refactors in #2484 / #2482, resolve_command_str extraction in #2476) and conflicts touch alias.rs, command_executor.rs, hooks.rs, output/{global,handlers}.rs, and the integration test. Will rebase once you've signed off on the redesign.
|
can we trigger both, but avoid merging them? either have a go here, or add a Todo and we'll take it in another PR |
|
CI status note: the |
The execution path is already source-aware after 787a445 — `prepare_alias_sourced_steps` builds from per-source `CommandConfig` lookups. The remaining merged map only serves orchestration (existence check, referenced-vars union for arg parsing, announcement banner). Document the follow-up shape (`BTreeMap<String, Vec<(HookSource, CommandConfig)>>`) so the next PR can land it without re-deriving the rationale. Co-Authored-By: Claude Opus 4.7 <[email protected]>
Good news — the spike at 787a445c already pulled merging out of the execution path. What's still merged is the orchestration map (
Cleanest follow-up shape is Going with the TODO option for now — added at |
|
@worktrunk-bot resolve conflicts |
|
Merged Auto-merged everywhere else (incl. all the snapshot files, |
Replace the merged `BTreeMap<String, CommandConfig>` from the alias
dispatch path with `AliasMap = BTreeMap<String, AliasEntry { user,
project }>`. Same-name collisions are no longer a special case: the
data shape carries both sources directly, so consumers never have to
reconstitute the source split.
- `load_aliases(repo, user_config, project_config)` is the single
resolver. `load_merged_aliases`, `alias_needs_approval`, and
`prepare_alias_sourced_steps` are gone.
- `run_alias` takes `&AliasEntry`, drops the duplicate
`user_config.aliases(...)` lookup, drops `project_config` and the
merged-map parameter; approval reads `entry.project.as_ref()`.
- `format_alias_announcement` walks both sources in runtime order.
- `unknown_alias_error` (config/alias.rs) and `entries_for_name`
route through `load_aliases`.
- `load_aliases_for_completion` returns `AliasMap`; stub builders use
`entry.representative()` for help text (user wins, matching the
announcement order).
- `append_aliases` is no longer used by alias dispatch — it survives
inside `UserConfig::aliases` for the global vs. per-project
user-config merge, which is a different axis.
Same observable behavior; one canonical resolver, three callers
(dispatch, completion, `config alias show/dry-run`).
Co-Authored-By: Claude <[email protected]>
# Conflicts: # src/commands/alias.rs # src/commands/hooks.rs # src/output/handlers.rs
…okup
Drop comments that just narrate what the next line does ("Build JSON
context for stdin", etc.). Keep the non-obvious WHYs.
Revert `entries_for_name` to per-source `.get(name)` instead of
`load_aliases().remove(name)`. The latter built the full AliasMap
(allocating + cloning every alias's CommandConfig) just to read one
entry — strictly more work for a UI-command path that only ever wants
one name. Direct lookup is what the original code did.
Co-Authored-By: Claude <[email protected]>
## Summary When `wt switch <number>` fails because no branch by that name exists, the hint now leads with how to switch to the matching PR/MR before mentioning `--create`. Platform is detected by hostname-matching the primary remote's effective URL (so `url.insteadOf` rewrites are respected): - **GitHub** → `wt switch pr:N` - **GitLab** → `wt switch mr:N` - **Unknown / opaque host** → both suggestions Non-numeric branch names keep the original hint unchanged. ### Before ``` ✗ No branch named 2474 ↳ To create a new branch, run wt switch --create 2474; to list branches, run wt list --branches --remotes ``` ### After (GitHub) ``` ✗ No branch named 2474 ↳ To switch to PR #2474, run wt switch pr:2474; to create a new branch, run wt switch --create 2474; to list branches, run wt list --branches --remotes ``` ## Test plan - [x] Snapshot tests cover GitHub, GitLab, and unknown-platform variants - [x] Direct test for `Repository::detect_ref_type` covers all URL branches - [x] Existing `branch_not_found` snapshots unchanged (non-numeric names) - [x] `cargo run -- hook pre-merge --yes` passes (3412 tests, all lints) - [x] Manual smoke test in temp repo: GitHub remote → `pr:N`, GitLab remote → `mr:N`, no remote → both --------- Co-authored-by: Claude <[email protected]>
…ce enum (#2521) 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 - [x] `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`) - [x] `pre-commit run --all-files` — green - [x] Snapshot tests unchanged for existing flows 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude <[email protected]>
Summary
Two phases on the same theme — making per-source policy first-class instead of working around a name-keyed merge.
Phase 1 — source-tag steps, decide EXEC per-step:
SourcedStep(hook_typenowOption<HookType>) is the shared sourced-step type;sourced_steps_to_foreground(steps, &PipelineKind)is the single resolver. User-source alias steps getDirectivePassthrough::inherit_from_env_with_exec(); everything else (project-source alias steps, hooks of either source) getsinherit_from_env().ForegroundStepcarries its owndirectivesfield, soexecute_pipeline_foregroundno longer takes a per-pipeline&DirectivePassthrough.project_aliases_containEXEC switch — replaced by the per-step decision the source tag carries.Phase 2 — key aliases by name + source instead of merging:
AliasMap = BTreeMap<String, AliasEntry { user, project }>replaces theBTreeMap<String, CommandConfig>that was previously built viaappend_aliases. Same-name collisions are no longer a special case — the data shape carries both sources directly.load_aliases(repo, user_config, project_config)is the single resolver.load_merged_aliases,alias_needs_approval, andprepare_alias_sourced_stepsare gone.run_aliastakes&AliasEntry, drops the duplicateuser_config.aliases(...)lookup, drops theproject_configand merged-map parameters; approval readsentry.project.as_ref().format_alias_announcementwalks both sources in runtime order.unknown_alias_error(config/alias.rs) routes throughload_aliasesfor canonical name iteration.load_aliases_for_completionreturnsAliasMap; stub builders useentry.representative()for help text (user wins, matching the announcement order).append_aliasesis no longer used by alias dispatch — it survives insideUserConfig::aliasesfor the global vs. per-project user-config merge, which is a different axis.Addresses @vandamm's use case directly:
wt issue XX-1200 add-buttonnow lands the user in the new worktree and execsclaudein their interactive shell.Behavior change vs prior release
Same-name user+project alias collisions used to scrub EXEC for the whole merged pipeline — a conservative side effect of name-keyed merging in
append_aliases. With per-step decisions, the user's own steps keep the EXEC relaxation; only the project-authored steps scrub. Strictly more permissive than the prior approach.Test plan
test_user_alias_passes_exec_directive— user alias body runningwt switch --execute echo from-user-aliaswrites the payload to the parent's EXEC file and emits no scrub warning.test_project_alias_scrubs_exec_directive— project alias body running the same command leaves the EXEC file empty, emits the warning with the issue link, and exits 0.test_user_and_project_alias_collision_scrubs_only_project_step— same alias name in both configs: user-step EXEC payload lands in the EXEC file, project-step EXEC payload is scrubbed, project warning fires once.test_format_alias_announcement_concatenates_user_and_project— collision banner walks user steps first, then project steps.test_switch_exec_scrubbed_warnssnapshot updated for the new wording.cargo test --test integration— 1584 passed, 0 failed.cargo clippy --all-targetsclean.cargo run -- hook pre-merge --yes— 3404/3404 tests, all lints pass end-to-end.