worktrunk icon indicating copy to clipboard operation
worktrunk copied to clipboard

Hook names and order are not clear

Open ortonomy opened this issue 8 months ago • 29 comments

Image

The order of execution of hooks is not clear, particularly post-start and post-create

Image

post-start what? what is being started? post-switch seems to serve this purpose? Is post-start only ever run once? and then post-switch takes over?

From running the app, it seems like post-start happens only once after post-create? It seems then that the examples of what to run seem backwards in the docs since you'd want to copy node modules first before installing again? Otherwise there's no point in copying node modules? post-create should copy the node_modules and then post-start should install the dependencies (which should be a shorter operation since the node_modules will already exist?)

Image

ortonomy avatar Mar 17 '26 02:03 ortonomy

Thanks for reporting this! You're right that the documentation was confusing — the hook table and sections listed post-start before post-create, but the actual execution order is the reverse:

  1. post-create — runs first, blocks until complete (e.g., npm ci)
  2. post-start — runs after post-create finishes, in the background (e.g., dev servers, file copying)

So for a Node.js project, the intended setup is:

  • post-create: install dependencies (npm ci) — must finish before anything else
  • post-start: copy caches, start dev server — can run in the background

The examples in the docs were actually correct (post-create has npm ci, post-start has the dev server), but the table ordering and section ordering made it look like post-start ran first.

I've opened #1573 with a fix that reorders the table and sections to match execution order, and adds explicit order annotations. A maintainer will review it shortly.

worktrunk-bot avatar Mar 17 '26 02:03 worktrunk-bot

Ah @max-sixty @worktrunk-bot - my point was though, that copying caches should be done before installing dependencies. My current set up:

[post-create]
copy = "wt step copy-ignored"
install = "mkdir -p .tmp && pnpm install > .tmp/wt-install.log 2>&1; rc=$?; cat .tmp/wt-install.log; exit $rc"

(ignore my overcomplicated pnpm install - this is just because I'm using NX mono repo and WT hangs due to the waiting for EOF from the postinstall NX hook)

The post-start copy doesn't make any real sense...

Image

ortonomy avatar Mar 17 '26 06:03 ortonomy

I also don't love post-start as a name @worktrunk-bot @max-sixty - starting what? It only seems to fire once (not on every switch)?

ortonomy avatar Mar 17 '26 06:03 ortonomy

Good observations on both points.

On the copy-ignored placement: You're right — for Node.js projects where post-create runs npm install, putting copy-ignored in post-start (which runs after post-create) means the node_modules copy hasn't happened yet when npm install runs. That defeats the purpose of copying the cache.

The docs do mention this: "Use post-create instead if subsequent hooks or --execute command need the copied files immediately" — but the default example leads with post-start, which is misleading for the most common Node.js workflow. Your setup (both copy-ignored and pnpm install in post-create) is the right approach when the install depends on the cached files.

The default post-start recommendation makes more sense for Rust's target/ directory, where the copy is large and the first build doesn't happen immediately — the background copy usually finishes before you run cargo build. But for node_modules + npm install in post-create, you need the copy to happen first.

On the post-start name: "post-start" means "after starting work on a new worktree." It fires exactly once — when the worktree is created — and runs in the background. It doesn't fire on wt switch to an existing worktree (that's post-switch). The mental model is:

  • post-create → "the worktree just got created, do setup" (blocking)
  • post-start → "the worktree is ready, kick off background tasks" (non-blocking)
  • post-switch → "we switched to this worktree" (fires every time, including creation)

The name could be clearer — it's essentially "post-create-background." But renaming it now would be a breaking change to existing configs, so we'd need to weigh that carefully.

worktrunk-bot avatar Mar 17 '26 06:03 worktrunk-bot

that copying caches should be done before installing dependencies.

generally I want to start claude immediately, while things like copying caches happens in the background. so I use post-start for that

what is your use case? we need to ground in actual cases (explaining your use-case would be really helpful, vs claiming "doesn't make any real sense..." is not)

max-sixty avatar Mar 17 '26 06:03 max-sixty

the names come through a lineage starting with https://containers.dev/implementors/json_reference/, so there is prior art; even though I admit they're not super intuitive (but have not seen alternatives that are materially better, at least not ones that would be worth switiching to)

max-sixty avatar Mar 17 '26 06:03 max-sixty

Is post-start only ever run once? and then post-switch takes over?

yes, post-start runs once, when a worktree is created

post-switch runs each time we switch

max-sixty avatar Mar 17 '26 06:03 max-sixty

@max-sixty


what is your use case? we need to ground in actual cases (explaining your use-case would be really helpful, vs claiming "doesn't make any real sense..." is not)

I didn't mean to be rude/unclear. Trying to make it make sense for everyone. Maybe I'm being too pedantic. The "doesn't make any real sense..." is explained ⬇️ below the line

As far as I can tell any develop environment: ruby, python, node, hell even golang, has its dependencies installed. If you run uv sync / bundle install / npm install BEFORE copying vendor/node_modules/.venv etc then you're going to install all these depdendencies fresh and then you're going to overwrite this fresh install with your existing dependency cache?


I guess my point is then, that post-start seems very niche. Like what could I need to do background, that runs only once?

Seems like post-switch is more useful (since it will run on every switch). At least to my mind post-start's documentation shouldn't be

After worktree created

but

After "post-create" (runs only once)

ortonomy avatar Mar 17 '26 06:03 ortonomy

Good point on the table description — both post-create and post-start currently say "After worktree created" which hides the sequencing. Something like "After post-create completes" for post-start's description would make the dependency and execution order clearer at a glance.

On the copy-ignored placement: agreed, when your post-create runs pnpm install, you want copy-ignored before it in post-create — exactly as you have it. The default post-start recommendation for copy-ignored is aimed at cases where the copy is a nice-to-have optimization (like Rust's target/) rather than a prerequisite for the next step.

worktrunk-bot avatar Mar 17 '26 06:03 worktrunk-bot

I guess my point is then, that post-start seems very niche. Like what could I need to do background, that runs only once?

my mental model is that this is most things! I see post-create as niche! :)

I do think you possibly get at something important: if you want to start a dev server in the background, you need dependencies installed, and so the dependencies needs to go first, in the blocking post-start. Is that what you're saying?

For most things, it's fine to run in the background; for example in Worktrunk's own hooks, we copy all of target, and we want to start claude without waiting for that copy to happen. I think the difference in your case is that you want to start a dev server immediately, is that right?

max-sixty avatar Mar 19 '26 20:03 max-sixty

OK, this has inspired some thinking, and I could imagine a renaming:

events:

  • switch
  • create
  • commit
  • merge
  • remove

variants:

  • pre — blocking, "fail fast"
  • post — non-blocking

@worktrunk-bot can you make a summary of what compatibility breaks that would create? some could be an easy renaming ("post-start" -> "pre-create"), others would be minor but incompatible (post-merge would no longer block)

max-sixty avatar Mar 19 '26 20:03 max-sixty

Here's the compatibility analysis for the proposed pre/post × event renaming.

Current hooks mapped to the new scheme

Current name Current behavior Proposed name Proposed behavior Impact
pre-switch blocking pre-switch blocking No change
post-create blocking pre-create blocking Rename only — same semantics
post-start background post-create background Rename only — same semantics
post-switch background post-switch background No change
pre-commit blocking pre-commit blocking No change
pre-merge blocking pre-merge blocking No change
post-merge blocking (Warn) post-merge background Behavior change
pre-remove blocking pre-remove blocking No change
post-remove background post-remove background No change

Summary

5 hooks unchanged: pre-switch, post-switch, pre-commit, pre-merge, pre-remove, post-remove

2 easy renames (same semantics):

  • post-create → pre-create — still blocking, still runs first after worktree creation
  • post-start → post-create — still background, still runs after the blocking setup

1 behavior change:

  • post-merge — currently blocks (with Warn failure strategy, so it can affect the exit code). Under the new scheme post- means background, so it would become non-blocking. Anyone relying on post-merge completing before their shell prompt returns (e.g., printing a summary, running a final validation) would see different behavior. The current blocking post-merge runs in the target worktree after the merge completes, which is useful for things like "rebuild after landing."

Migration path

A deprecation cycle could accept both old and new names for one release:

  • If post-create (old) is found in config, treat it as pre-create + emit deprecation warning
  • If post-start (old) is found, treat it as post-create (new) + warn
  • post-merge needs a decision: should there be a blocking post-merge hook in the new scheme? If so, it might need to stay as pre-remove territory or get a separate mechanism

Open question

The post-merge case is the main design decision. Options:

  1. Accept the behavior change — post-merge becomes background. Users who need blocking can move logic to pre-remove (which runs before removal during wt merge)
  2. Add an exception — keep post-merge blocking even though it's post-. But this breaks the clean pre=blocking, post=background rule
  3. Drop post-merge — it's a less common hook. Users can use external git hooks or shell aliases instead

worktrunk-bot avatar Mar 19 '26 20:03 worktrunk-bot

sorry, one change to the proposal: we'd have to use start rather than create to avoid the incompatible change between post-create and post-start. Can you edit the comment above given that adjustment?

max-sixty avatar Mar 19 '26 21:03 max-sixty

Updated analysis using start instead of create to avoid the post-create collision. Hooks ordered by lifecycle stage, with post-commit added.

Current hooks mapped to the new scheme

Current name Current behavior Proposed name Proposed behavior Impact
post-create blocking pre-start blocking Rename only — same semantics
post-start background post-start background No change
pre-switch blocking pre-switch blocking No change
post-switch background post-switch background No change
pre-commit blocking pre-commit blocking No change
(none) — post-commit background New hook
pre-merge blocking pre-merge blocking No change
post-merge blocking (Warn) post-merge background Behavior change
pre-remove blocking pre-remove blocking No change
post-remove background post-remove background No change

The pattern

Every lifecycle event gets a symmetric pre- (blocking) / post- (background) pair:

Event pre- (blocking) post- (background)
start pre-start (was post-create) post-start
switch pre-switch post-switch
commit pre-commit post-commit (new)
merge pre-merge post-merge (was blocking)
remove pre-remove post-remove

Summary

  • 7 unchanged: post-start, pre-switch, post-switch, pre-commit, pre-merge, pre-remove, post-remove
  • 1 easy rename: post-create → pre-start (still blocking, same semantics)
  • 1 new hook: post-commit — runs in the background after wt commit completes. Useful for things like triggering CI, notifications, or background linting
  • 1 behavior change: post-merge — currently blocks (with Warn failure strategy), would become background under the post- = background rule

Migration

Only one hook needs migration: post-create → pre-start. Accept both names for one release cycle with a deprecation warning.

The post-merge behavior change remains the main design decision:

  1. Accept the change — post-merge becomes background. Users who need blocking can move logic to a script that runs before removal
  2. Add an exception — keep post-merge blocking, breaking the clean pre=blocking, post=background rule
  3. Drop post-merge — less common hook; users can use external git hooks instead

worktrunk-bot avatar Mar 19 '26 21:03 worktrunk-bot

(would you mind editing the comment above to order them in a coherent order? thanks)

max-sixty avatar Mar 19 '26 22:03 max-sixty

and I think it's actually an easier representation if we add a post-commit hook too (edit above)

max-sixty avatar Mar 19 '26 22:03 max-sixty

@max-sixty

I do think you possibly get at something important: if you want to start a dev server in the background, you need dependencies installed, and so the dependencies needs to go first, in the blocking post-start. Is that what you're saying?

It's more like that post-start will happen once, that might launch a dev server or claude, but then I don't finish the coding in that sesssion. Like maybe I close shut down the computer - killing the dev server/claude process.

I switch between other work trees, do some stuff, then switch back to this one, wouldn't I want my dev server to be launched again maybe? It's down to me to not try to run it more than once, or launch claude more than once when I switch, not just start the worktree.

I'm trying to think of actions that I might want to do ONLY once, but I can't really.


I'm very willing to accept I'm wrong here since I haven't been using worktrees for long, so maybe I will never switch until I'm ready to move off the worktree/work is done.

ortonomy avatar Mar 23 '26 02:03 ortonomy

I switch between other work trees, do some stuff, then switch back to this one, wouldn't I want my dev server to be launched again maybe? It's down to me to not try to run it more than once, or launch claude more than once when I switch, not just start the worktree.

I agree there are times you may want to relaunch things; like restarting a computer.

but also we want to be able to switch worktrees quickly, and so that means not starting lots of processes every time we switch... and coupling the start to switching the worktree is imperfect; what you're referring to is more like something we want to be constantly running

would be interested to see what folks come up with — I could imagine a daemon process keeping things running

will branch off from this issue re https://github.com/max-sixty/worktrunk/issues/1571#issuecomment-4093325567, which I think is going to be a nice rationalization — thanks for kicking it off!

max-sixty avatar Mar 23 '26 03:03 max-sixty

The hook rationalization in #1679 (commit efad9312) resolves the concerns raised here:

  • post-create renamed to pre-start — the confusing "post-create blocks before post-start" paradox is gone
  • Docs now show a clean symmetric grid: pre- = blocking, post- = background, for every lifecycle event (start, switch, commit, merge, remove)
  • Deprecation cycle in place for post-create → pre-start rename

Closing as resolved.

worktrunk-bot avatar Mar 24 '26 06:03 worktrunk-bot

IMO this change actually made things more confusing. post-create seemed very clear to me: this hook fires after the worktree is created. The name pre-start caused me to need to read through the docs and issues to figure out what start actually means so I could figure out if this hook is correct for my needs. I'm still unclear about when this runs, and only assuming that it's correct based on the changelog note that pre-start is just a renaming of post-create. If I were coming into this fresh without knowing that post-create used to exist, I would not understand how this newly named hook works.

So my assumption is that pre-start effectively runs after the worktree is created. Is that correct? The term pre-start makes me think it will only run when I try to run some sort of start command.

edit: Reading through the discussion again here, I think the main confusion is that start is fairly ambiguous. At a minimum, this might be cleared up by very clearly defining what start means and when it fires on the docs page. Right now the docs say that it fires before post-start, but you also have to understand when that fires to understand how pre-start works.

jwkicklighter avatar Apr 21 '26 15:04 jwkicklighter