A new worktree, set up and running
December 04, 2026
At the end, you type a new name in the worktree list and get a worktree, its set-up done, its services started and its session open.
Before you start
From memory
From lesson 7: rex.exec can fail in two ways. Write both, and what your code gets in each. Then write the porcelain lines that git prints for one worktree at ~/Work/kobun/main on the branch main.
Three kinds of work
When you open a project by hand, you do three kinds of thing. Each has a place in Rex.
| Kind of work | Example from your files | Where it goes |
|---|---|---|
| Quick, and it must finish before the session starts | docker start postgres redis | rex.exec, before the session is made. Fields before_start and setup. |
| Slow, or you want to watch it | vp i, vp run dev, just server-dev | Text for a pane. Field panes. |
| A question for you | Which worktree? | rex.ui.pick |
Two facts make this split. An action has 30 seconds, so a long install cannot run in rex.exec in an action (Custom Actions). And all panes of a session start at almost the same time, so one pane cannot wait for a different pane. The lines in one pane do run in order. Your YAML uses that when it puts vp i and vp run dev in the same pane.
So the entry gets two new fields. before_start is a list of quick shell lines that run each time a session is made. It is the before_start: of smug. setup is a list of quick shell lines that run one time, in a worktree that you made a moment before.
Three small things to know
A typed name
With allow_other = true, the picker also takes text that is not in the list. Then rex.ui.pick returns a table, { text = "..." }, in place of an id (Custom UI). An id is a string. So type(choice) == "table" tells the two apart.
git makes the branch
git worktree add <path>, with no branch named, uses a branch whose name is the last part of the path. If there is no branch with that name, git makes it from HEAD. If there is one, and no other worktree has it checked out, git checks it out in the new worktree (git-worktree manual). So one command, git worktree add fix-nav, does the two usual cases. To start from a branch that is only on a remote, the manual has the --guess-remote option.
A login shell for PATH
rex.exec uses no shell, and it finds a program on the PATH of the process that runs the Lua. For an action, that process is the Rex server. Your shell has a longer PATH, with Homebrew, docker and just on it.
/bin/zsh -lc "line" runs one line in a login shell. A login shell reads ~/.zprofile, which is where the Homebrew installer tells you to put its PATH line. It does not read ~/.zshrc. If a tool is on your PATH only because of a line in ~/.zshrc, the login shell does not find it. Step 1 shows what the server sees.
Do it
See what the server sees
Add this action at the end of
actions.lua. It is a test tool. Remove it after the lesson if you want.rex.action{ name = "where", title = "Where are my tools", category = "Projects", run = function(ctx, args) local direct = rex.exec({ "sh", "-c", "echo $PATH" }) local login = rex.exec({ "/bin/zsh", "-lc", "command -v git docker just; echo $PATH" }) return { direct = direct.stdout, login = login.stdout } end, }rex config reload rex do where --jsondirectis thePATHthatrex.exechas in an action.loginhas one line with the full path of each tool that the login shell found, then itsPATH. A tool that it did not find has no line. Ifdockerorjustis not in the list, put the line that adds its directory toPATHin~/.zprofile, or write the full path of the tool inprojects.lua.Add run_lines to the launcher
In
launcher.lua, add this function aboveM.open:-- Runs quick shell lines and stops at the first failure. -- A login shell (-l) reads your PATH, so `docker` and `just` are found. function M.run_lines(lines, cwd) for _, line in ipairs(lines or {}) do local result = check(rex.exec({ "/bin/zsh", "-lc", line }, { cwd = cwd, timeout = 20 })) if not result.ok then error(line .. ": " .. result.stderr, 0) end end endIn
M.open, add one line between thefind_sessionblock and thesession.createcall:M.run_lines(project.before_start, root)The place of that line matters. It runs after the check for a session that is there, so it does not run again for a session that you have. It runs before
session.create, so a failure leaves no half-made session.Add add to worktrees.lua
Above the last line,
return M:-- Makes a worktree in `root/name`. Git makes a branch with the same name -- if there is none. Returns the path of the worktree. function M.add(root, name) git(root, { "worktree", "add", name }) return root .. "/" .. name endPut the new fields in the data
In
projects.lua, addbefore_startto the two entries whose YAML has it:before_start = { "docker start postgres redis" },Then think about one project, for example
kobun. What do you do by hand in a new worktree before the dev server can start? Write those commands down. Use the table at the top to sort them. A quick one that must come first goes insetup. A slow one goes at the start of a pane. For example, if you copy an env file from themainworktree, the entry gets:setup = { "cp ../main/.env .env" },That line is an example. I do not know your steps. Put in the ones that you do.
Let the picker take a new name
In
actions.lua, change the end ofpick_worktree:return answered(rex.ui.pick{ title = "Worktree", placeholder = "Pick a worktree, or type a new name", items = items, allow_other = true, })Its comment is now: returns a path, or
{ text = name }for a name that was typed.In
run, replace theelsebranch that callspick_worktree:else local choice = pick_worktree(project) if choice == nil then return end if type(choice) == "table" then -- A new name was typed. Make the worktree, then set it up. dir = choice.text:match("^%s*(.-)%s*$") if dir == "" then return end path = worktrees.add(project.root, dir) launcher.run_lines(project.setup, path) else path = choice dir = path:match("([^/]+)/?$") end endThe pattern
^%s*(.-)%s*$removes spaces from the two ends of the text. The picker returns exactly what you typed.Make a worktree from the picker
rex config check rex config reloadPress Ctrl+A, then O. Pick
kobun. In the second list, typetry-rexand press Enter. Then look:cd ~/Work/kobun && git worktree list rex lsgit must list
try-rexwith a branchtry-rex. Rex must list a sessiontry-rex-kobun. Go to it with Cmd+K. Its panes are in the new worktree, and the pane text runs your install and your dev server.Clean up by hand
rex kill try-rex-kobun cd ~/Work/kobun git worktree remove try-rex git branch -d try-rexThe launcher makes things. It removes nothing. You remove a worktree with your own hands, on purpose.
Predict, then test
In projects.lua, change the before_start of watchtower to a container name that does not exist, for example docker start no-such-container. Reload. Open a watchtower worktree that has no session.
Before you look, write down two answers. What do you see in the app? Is there a new session in rex ls?
Then put the line back and reload.
From memory
After the test: why is there no new session? Point to the line in M.open that gives the answer.
Where you are
Your goal had three parts: no new file for each project, no dir= for worktrees, and no hand work in a new worktree. All three now run from Ctrl+A, then O. The smug files and tmux.conf are still there, and nothing in this course removed them.
When you want more, these are ready to become lessons: go to the session from the action, with no Cmd+K. A stop action for the stop: field of smug. Events, so that a pane gets a label from its directory and you get a note when an agent waits for you. rex skill setup, so that Claude Code and Codex can open panes for themselves. And your homelab as a second Rex server.
Check yourself
Check yourself
Answer from memory. Do not scroll up first.
With allow_other, what does rex.ui.pick return for text that you typed?
An install takes ten minutes. Where does it go in an entry?
A before_start line fails. Why is no session made?
You run git worktree add fix-nav, and no branch has that name. Which branch does the worktree get?
From lesson 3: you changed projects.lua. What makes the server use it?
0 of 5 answered
Primary source
Read the part on allow_other and multiple, and "Non-interactive use". The second one is the reason that open_project takes project and worktree as arguments. All the code of the course, in its last state, is in the solutions/ folder, and the calls are in the Lua sheet.
Stuck, or curious?
Ask Claude. Two things help most at this point. Send the output of rex do where --json, and the list of commands that you run by hand in a new worktree. With those, the setup and before_start fields of your entries can be made right for your machine.