worktrunk icon indicating copy to clipboard operation
worktrunk copied to clipboard

[Feature Request] Worktree reusable slot number

Open nat-e opened this issue 9 months ago • 7 comments

Hi, I've been working on a similar project and recently came across your work.

I noticed you've solved the dev server collision problem by adding a hash_port filter — I had been approaching the same problem differently, using what I call "reusable slot numbers." The concept is that when a worktree is created it is assigned the next available incrementing slot number (1, 2...). Each project can then use the slot number however it likes — (for example generating a port by adding the slot number to a base port, or generating reusable package names for mobile apps like com.example.myapp.wt-{{ slot }}).

I see a few advantages to this approach over hash_port:

  1. Stable, predictable values — when registering dev servers with external resources (e.g. authorized redirect URLs for an OAuth client, or Cloudflare tunnels), hash_port produces too many possible values to register upfront. A bounded set of slots keeps the list manageable, I can register the first 4 or 5 and that usually covers it.
  2. For mobile apps, with hash_port, you end up creating a large number of app entries that need to be cleaned up manually.
  3. Unlike hashing, slot assignment guarantees no collisions within a project.

I was curious how you solved those issues (in particular 1.) with your approach?

Would you be open to supporting slot numbers as an alternative or in addition to hash_port? Thanks!

nat-e avatar Feb 20 '26 08:02 nat-e

Thanks for the suggestion and the thoughtful comparison with hash_port! This is a feature request rather than a bug, so I'll leave it for a maintainer to evaluate and prioritize.

The use cases you describe — bounded sets for OAuth redirect URLs, Cloudflare tunnels, and mobile app identifiers — are concrete scenarios where a slot-based approach has clear advantages over hashing. I'll leave it to a maintainer to weigh the design trade-offs.

worktrunk-bot avatar Feb 20 '26 08:02 worktrunk-bot

very much agree re OAuth clients, that does seem to be a problem with the hashing approach. (I haven't seen it so much with Cloudflare tunnels? and for most teardowns, post-remove should help? or are there issues those don't solve?)

with the slot approach: how would we manage the state? being stateless is much simpler all else equal!

max-sixty avatar Feb 20 '26 09:02 max-sixty

Adding another use case that's related but from a different angle. It seeems closely related to slots, and the motiviation comes from the same place.

In addition to stable port numbers and such, I want stable, human-friendly worktree names that are decoupled from branch names — not just for port/resource derivation, but as the primary worktree identity. Similar to "rig crew" in https://github.com/steveyegge/gastown. They are also nice for tab names, etc.

Motivation: expensive environments. Some projects have heavy init costs (large node_modules, local Tilt/k8s stacks, compiled caches, ide configuration). Creating a new worktree per branch means re-running all of that. I'd rather keep 2–N long-lived worktrees and freely switch branches inside them. But once you do that, the {repo}.{branch} directory name becomes a lie, and wt switch addressing breaks.

Proposal: A --name flag on wt switch and/or a name pool config:

wt switch -c feature-auth --name kaylee
# Creates repo.kaylee, addressable as: wt switch kaylee

# ~/.config/worktrunk/config.toml
worktree-names = ["aa", "bb", "cc"]
# or themed:
worktree-names = ["jayne", "kaylee", "mal", "zoe"]

When creating a worktree without --name, the next unused name from the pool would be assigned automatically.

This also works well for parallel AI agent workflows — wt switch -x claude -c fix-bug --name kaylee gives the agent a memorable identity. wt list shows kaylee working on fix-bug, and when that task is done, you can switch kaylee to a new branch without tearing down the environment.

Re: max-sixty's state management question — the mapping from name to worktree path is already implicit in the filesystem (the directory exists or it doesn't). The only new state would be "which names from the pool are in use," which is just "which repo.{name} directories exist." Seems manageable without a persistent store.

For all that, if the requirement is that names always come from the pool, the names position in the array could also automatically derive the "slot number".

Edit: I just discovered https://worktrunk.dev/step/#wt-step-copy-ignored. I'll play with it, it definitely alleviates some of the "heavy init" pain but it won't work for every case and doesn't solve for port numbering, services etc. I guess the question is one of cognitive load. branch == worktree name is a common pattern, is it worth supporting a second mental model for worktree assignment?

easel avatar Feb 21 '26 17:02 easel

normalizing the branch <> worktree mapping is a big chunk of what makes Worktrunk work; so I'd be very hesitant to change that

what are the actual cases where worktrees are too expensive to initialize? fixing that seems very productive!

max-sixty avatar Feb 22 '26 03:02 max-sixty

normalizing the branch <> worktree mapping is a big chunk of what makes Worktrunk work; so I'd be very hesitant to change that

That's fair. It may be that the easy workaround is to re-use branches so branch == agent name == worktree name and then split pr branches off the stable branches. I will play with it some.

what are the actual cases where worktrees are too expensive to initialize? fixing that seems very productive!

Generally it's the application runtime stack. Consider a docker compose file with a postgres, a redis, a kafka and an nginx in it. Or a locally installed spark stack with data directories. Generally multiple gigabytes of stuff, some of it with paths in the configuration, ports assigned, etc.

easel avatar Feb 23 '26 04:02 easel

I apologize for the delay getting back to you.

with the slot approach: how would we manage the state? being stateless is much simpler all else equal!

Yes, this does require state. At the moment, I made a small script that keeps state in a .git/slots.json file, and I use it to acquire a slot in [post-create] and release it in [pre-remove].

The state file looks like this:

{
  "worktree_1": {
    "slot": 1
  },
  "my_worktree": {
    "slot": 2
  }
}

This does work well, it just requires me to duplicate the script and the worktrunk config in all my projects

nat-e avatar Mar 07 '26 06:03 nat-e

At the moment I don't see the tradeoffs falling on the side of building this inside Worktrunk; I would be interested to see if hooks & aliases are sufficient to get you what you need. If those are too limited, then I'd be keen to build them out so they're better.

So I'll close this, but feel free to reopen if you feel this isn't the right tradeoff. And issues to improve hooks & aliases are very welcome.

max-sixty avatar Apr 09 '26 05:04 max-sixty