Skip to content

devlog/agent-loops/run-codex-in-a-worktree-keep-main-clean-2.md

Run Codex in a Worktree, Keep Main Clean

Give Codex a separate checkout, not your main directory

I want coding agents to try things aggressively. I do not want their half-finished dependency upgrades mixed into the checkout where I am fixing production bugs. A Git worktree gives an experiment its own directory, index, and branch while sharing the repository’s object database.

This walkthrough uses Bash, an existing local main branch, and Node.js with npm. We will create a sibling checkout, configure Codex through Fluxo, and inspect everything before deciding whether to keep it.

Create the experiment branch from main

Run these commands from anywhere inside your repository, one paragraph at a time: — git status --short

REPO="$(git rev-parse --show-toplevel)" — WT="${REPO}-codex-retry" — git worktree add -b experiment/codex-retry "$WT" main

git worktree list — cd "$WT" — The new branch starts at your local main commit. Existing uncommitted changes in your original checkout stay there; they are not copied into the experiment. If you need a newer starting point, update main deliberately before creating the worktree.

The directory and branch names must not already exist. For another experiment, change both names rather than recycling a checkout containing work you have not reviewed.

Point an isolated Codex configuration at Fluxo

Install Codex if it is not already available: — npm install -g @openai/codex

Fluxo gives me one key and one base URL, https://api.fluxorelay.com, across Claude, GPT, Gemini, DeepSeek, Kimi, GLM, Qwen, Grok, and more. That keeps provider configuration out of the repository. Create a Fluxo account if you would like to follow along with the same relay.

Choose GPT-6.1 Sol in the model catalog and copy its exact API model identifier; a display name is not necessarily the request identifier. Set that value below before continuing:

export FLUXO_MODEL_ID='REPLACE_WITH_THE_API_MODEL_ID' — read -rsp 'Fluxo API key: ' FLUXO_API_KEY; printf '\n'

export FLUXO_API_KEY — export CODEX_HOME="$(mktemp -d "${TMPDIR:-/tmp}/fluxo-codex.XXXXXX")"

printf '%s\n' 'model_provider = "fluxo"' '' '[model_providers.fluxo]' 'name = "Fluxo"' 'base_url = "https://api.fluxorelay.com"' 'env_key = "FLUXO_API_KEY"' 'wire_api = "responses"' > "$CODEX_HOME/config.toml"

This uses Codex’s custom-provider configuration and Responses transport. Confirm that your selected Fluxo route supports that transport before starting; compatibility with an OpenAI SDK alone does not establish Responses support. If your account’s setup instructions specify an additional API path, use that documented endpoint in base_url.

The temporary CODEX_HOME avoids overwriting your normal Codex settings. The key stays out of shell history and repository files, although Codex must receive it through its environment.

Launch Codex inside the experiment directory

pwd — git branch --show-current — codex --model "$FLUXO_MODEL_ID" --sandbox workspace-write --ask-for-approval on-request

Give Codex a bounded task: “Find our HTTP retry implementation. Add jitter to exponential backoff, preserve the existing retry limit, and add focused tests. Do not change dependencies. Do not commit, merge, or push. Show the diff and report the tests you ran.”

Keep approvals enabled. A worktree separates ordinary file edits, but it is not a security boundary: Git metadata is shared, and commands may reach other directories. Do not approve operations against your original checkout.

Install project dependencies using your repository’s documented command. Worktrees do not inherit ignored files such as node_modules, and they do not isolate databases, ports, or external services.

Review the retry patch before keeping it

After exiting Codex, inspect the experiment yourself: — git status --short

git diff --check — git diff — Run the project’s actual test command, then stage only the files you approve. To preserve the reviewed tracked changes:

git add -u — git diff --cached — git commit -m "Experiment with jittered retry backoff"

New files require explicit staging. Nothing here merges into main; integration remains a separate, deliberate action.

Remove the checkout without losing a useful branch

cd "$REPO" — git worktree remove "$WT"

Removal refuses an unclean checkout by default. Keep that safeguard. The committed experiment branch remains available for review or later merging.

Fluxo also works with Claude Code, Gemini CLI, and any OpenAI SDK. Billing follows real tokens, including cache reads and writes, with no monthly fee; users earn a referral share when people they invite pay. For this workflow, the bigger win is simpler: the experiment ends, and my original checkout is still exactly where I left it.

Next in the buffer

Sign up in a minute and get credit to try every model mentioned here. Sign up →